ACPI: thinkpad-acpi: store ThinkPad model information
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob428289ac5a534e606f7b95af5ccaf8155215bd25
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 IBM_VERSION "0.14"
25 #define TPACPI_SYSFS_VERSION 0x010000
28 * Changelog:
29 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
30 * drivers/misc.
32 * 2006-11-22 0.13 new maintainer
33 * changelog now lives in git commit history, and will
34 * not be updated further in-file.
36 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
37 * 2005-03-17 0.11 support for 600e, 770x
38 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
39 * support for 770e, G41
40 * G40 and G41 don't have a thinklight
41 * temperatures no longer experimental
42 * experimental brightness control
43 * experimental volume control
44 * experimental fan enable/disable
45 * 2005-01-16 0.10 fix module loading on R30, R31
46 * 2005-01-16 0.9 support for 570, R30, R31
47 * ultrabay support on A22p, A3x
48 * limit arg for cmos, led, beep, drop experimental status
49 * more capable led control on A21e, A22p, T20-22, X20
50 * experimental temperatures and fan speed
51 * experimental embedded controller register dump
52 * mark more functions as __init, drop incorrect __exit
53 * use MODULE_VERSION
54 * thanks to Henrik Brix Andersen <brix@gentoo.org>
55 * fix parameter passing on module loading
56 * thanks to Rusty Russell <rusty@rustcorp.com.au>
57 * thanks to Jim Radford <radford@blackbean.org>
58 * 2004-11-08 0.8 fix init error case, don't return from a macro
59 * thanks to Chris Wright <chrisw@osdl.org>
60 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
61 * fix led control on A21e
62 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
63 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
64 * proc file format changed
65 * video_switch command
66 * experimental cmos control
67 * experimental led control
68 * experimental acpi sounds
69 * 2004-09-16 0.4 support for module parameters
70 * hotkey mask can be prefixed by 0x
71 * video output switching
72 * video expansion control
73 * ultrabay eject support
74 * removed lcd brightness/on/off control, didn't work
75 * 2004-08-17 0.3 support for R40
76 * lcd off, brightness control
77 * thinklight on/off
78 * 2004-08-14 0.2 support for T series, X20
79 * bluetooth enable/disable
80 * hotkey events disabled by default
81 * removed fan control, currently useless
82 * 2004-08-09 0.1 initial release, support for X series
85 #include "thinkpad_acpi.h"
87 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
88 MODULE_DESCRIPTION(IBM_DESC);
89 MODULE_VERSION(IBM_VERSION);
90 MODULE_LICENSE("GPL");
92 /* Please remove this in year 2009 */
93 MODULE_ALIAS("ibm_acpi");
96 * DMI matching for module autoloading
98 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
99 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
101 * Only models listed in thinkwiki will be supported, so add yours
102 * if it is not there yet.
104 #define IBM_BIOS_MODULE_ALIAS(__type) \
105 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
107 /* Non-ancient thinkpads */
108 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
109 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
111 /* Ancient thinkpad BIOSes have to be identified by
112 * BIOS type or model number, and there are far less
113 * BIOS types than model numbers... */
114 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
115 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
116 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
118 #define __unused __attribute__ ((unused))
120 /****************************************************************************
121 ****************************************************************************
123 * ACPI Helpers and device model
125 ****************************************************************************
126 ****************************************************************************/
128 /*************************************************************************
129 * ACPI basic handles
132 static acpi_handle root_handle;
134 #define IBM_HANDLE(object, parent, paths...) \
135 static acpi_handle object##_handle; \
136 static acpi_handle *object##_parent = &parent##_handle; \
137 static char *object##_path; \
138 static char *object##_paths[] = { paths }
140 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
141 "\\_SB.PCI.ISA.EC", /* 570 */
142 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
143 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
144 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
145 "\\_SB.PCI0.ICH3.EC0", /* R31 */
146 "\\_SB.PCI0.LPC.EC", /* all others */
149 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
150 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
153 /*************************************************************************
154 * Misc ACPI handles
157 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
158 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
159 "\\CMS", /* R40, R40e */
160 ); /* all others */
162 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
163 "^HKEY", /* R30, R31 */
164 "HKEY", /* all others */
165 ); /* 570 */
168 /*************************************************************************
169 * ACPI helpers
172 static int acpi_evalf(acpi_handle handle,
173 void *res, char *method, char *fmt, ...)
175 char *fmt0 = fmt;
176 struct acpi_object_list params;
177 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
178 struct acpi_buffer result, *resultp;
179 union acpi_object out_obj;
180 acpi_status status;
181 va_list ap;
182 char res_type;
183 int success;
184 int quiet;
186 if (!*fmt) {
187 printk(IBM_ERR "acpi_evalf() called with empty format\n");
188 return 0;
191 if (*fmt == 'q') {
192 quiet = 1;
193 fmt++;
194 } else
195 quiet = 0;
197 res_type = *(fmt++);
199 params.count = 0;
200 params.pointer = &in_objs[0];
202 va_start(ap, fmt);
203 while (*fmt) {
204 char c = *(fmt++);
205 switch (c) {
206 case 'd': /* int */
207 in_objs[params.count].integer.value = va_arg(ap, int);
208 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
209 break;
210 /* add more types as needed */
211 default:
212 printk(IBM_ERR "acpi_evalf() called "
213 "with invalid format character '%c'\n", c);
214 return 0;
217 va_end(ap);
219 if (res_type != 'v') {
220 result.length = sizeof(out_obj);
221 result.pointer = &out_obj;
222 resultp = &result;
223 } else
224 resultp = NULL;
226 status = acpi_evaluate_object(handle, method, &params, resultp);
228 switch (res_type) {
229 case 'd': /* int */
230 if (res)
231 *(int *)res = out_obj.integer.value;
232 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
233 break;
234 case 'v': /* void */
235 success = status == AE_OK;
236 break;
237 /* add more types as needed */
238 default:
239 printk(IBM_ERR "acpi_evalf() called "
240 "with invalid format character '%c'\n", res_type);
241 return 0;
244 if (!success && !quiet)
245 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
246 method, fmt0, status);
248 return success;
251 static void __unused acpi_print_int(acpi_handle handle, char *method)
253 int i;
255 if (acpi_evalf(handle, &i, method, "d"))
256 printk(IBM_INFO "%s = 0x%x\n", method, i);
257 else
258 printk(IBM_ERR "error calling %s\n", method);
261 static int acpi_ec_read(int i, u8 * p)
263 int v;
265 if (ecrd_handle) {
266 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
267 return 0;
268 *p = v;
269 } else {
270 if (ec_read(i, p) < 0)
271 return 0;
274 return 1;
277 static int acpi_ec_write(int i, u8 v)
279 if (ecwr_handle) {
280 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
281 return 0;
282 } else {
283 if (ec_write(i, v) < 0)
284 return 0;
287 return 1;
290 static int _sta(acpi_handle handle)
292 int status;
294 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
295 status = 0;
297 return status;
300 static int issue_thinkpad_cmos_command(int cmos_cmd)
302 if (!cmos_handle)
303 return -ENXIO;
305 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
306 return -EIO;
308 return 0;
311 /*************************************************************************
312 * ACPI device model
315 static void drv_acpi_handle_init(char *name,
316 acpi_handle *handle, acpi_handle parent,
317 char **paths, int num_paths, char **path)
319 int i;
320 acpi_status status;
322 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
323 name);
325 for (i = 0; i < num_paths; i++) {
326 status = acpi_get_handle(parent, paths[i], handle);
327 if (ACPI_SUCCESS(status)) {
328 *path = paths[i];
329 dbg_printk(TPACPI_DBG_INIT,
330 "Found ACPI handle %s for %s\n",
331 *path, name);
332 return;
336 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
337 name);
338 *handle = NULL;
341 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
343 struct ibm_struct *ibm = data;
345 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
346 return;
348 ibm->acpi->notify(ibm, event);
351 static int __init setup_acpi_notify(struct ibm_struct *ibm)
353 acpi_status status;
354 int rc;
356 BUG_ON(!ibm->acpi);
358 if (!*ibm->acpi->handle)
359 return 0;
361 vdbg_printk(TPACPI_DBG_INIT,
362 "setting up ACPI notify for %s\n", ibm->name);
364 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
365 if (rc < 0) {
366 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
367 ibm->name, rc);
368 return -ENODEV;
371 acpi_driver_data(ibm->acpi->device) = ibm;
372 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
373 IBM_ACPI_EVENT_PREFIX,
374 ibm->name);
376 status = acpi_install_notify_handler(*ibm->acpi->handle,
377 ibm->acpi->type, dispatch_acpi_notify, ibm);
378 if (ACPI_FAILURE(status)) {
379 if (status == AE_ALREADY_EXISTS) {
380 printk(IBM_NOTICE "another device driver is already handling %s events\n",
381 ibm->name);
382 } else {
383 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
384 ibm->name, status);
386 return -ENODEV;
388 ibm->flags.acpi_notify_installed = 1;
389 return 0;
392 static int __init tpacpi_device_add(struct acpi_device *device)
394 return 0;
397 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
399 int rc;
401 dbg_printk(TPACPI_DBG_INIT,
402 "registering %s as an ACPI driver\n", ibm->name);
404 BUG_ON(!ibm->acpi);
406 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
407 if (!ibm->acpi->driver) {
408 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
409 return -ENOMEM;
412 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
413 ibm->acpi->driver->ids = ibm->acpi->hid;
414 ibm->acpi->driver->ops.add = &tpacpi_device_add;
416 rc = acpi_bus_register_driver(ibm->acpi->driver);
417 if (rc < 0) {
418 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
419 ibm->acpi->hid, rc);
420 kfree(ibm->acpi->driver);
421 ibm->acpi->driver = NULL;
422 } else if (!rc)
423 ibm->flags.acpi_driver_registered = 1;
425 return rc;
429 /****************************************************************************
430 ****************************************************************************
432 * Procfs Helpers
434 ****************************************************************************
435 ****************************************************************************/
437 static int dispatch_procfs_read(char *page, char **start, off_t off,
438 int count, int *eof, void *data)
440 struct ibm_struct *ibm = data;
441 int len;
443 if (!ibm || !ibm->read)
444 return -EINVAL;
446 len = ibm->read(page);
447 if (len < 0)
448 return len;
450 if (len <= off + count)
451 *eof = 1;
452 *start = page + off;
453 len -= off;
454 if (len > count)
455 len = count;
456 if (len < 0)
457 len = 0;
459 return len;
462 static int dispatch_procfs_write(struct file *file,
463 const char __user * userbuf,
464 unsigned long count, void *data)
466 struct ibm_struct *ibm = data;
467 char *kernbuf;
468 int ret;
470 if (!ibm || !ibm->write)
471 return -EINVAL;
473 kernbuf = kmalloc(count + 2, GFP_KERNEL);
474 if (!kernbuf)
475 return -ENOMEM;
477 if (copy_from_user(kernbuf, userbuf, count)) {
478 kfree(kernbuf);
479 return -EFAULT;
482 kernbuf[count] = 0;
483 strcat(kernbuf, ",");
484 ret = ibm->write(kernbuf);
485 if (ret == 0)
486 ret = count;
488 kfree(kernbuf);
490 return ret;
493 static char *next_cmd(char **cmds)
495 char *start = *cmds;
496 char *end;
498 while ((end = strchr(start, ',')) && end == start)
499 start = end + 1;
501 if (!end)
502 return NULL;
504 *end = 0;
505 *cmds = end + 1;
506 return start;
510 /****************************************************************************
511 ****************************************************************************
513 * Device model: input, hwmon and platform
515 ****************************************************************************
516 ****************************************************************************/
518 static struct platform_device *tpacpi_pdev;
519 static struct class_device *tpacpi_hwmon;
520 static struct input_dev *tpacpi_inputdev;
523 static int tpacpi_resume_handler(struct platform_device *pdev)
525 struct ibm_struct *ibm, *itmp;
527 list_for_each_entry_safe(ibm, itmp,
528 &tpacpi_all_drivers,
529 all_drivers) {
530 if (ibm->resume)
531 (ibm->resume)();
534 return 0;
537 static struct platform_driver tpacpi_pdriver = {
538 .driver = {
539 .name = IBM_DRVR_NAME,
540 .owner = THIS_MODULE,
542 .resume = tpacpi_resume_handler,
546 /*************************************************************************
547 * thinkpad-acpi driver attributes
550 /* interface_version --------------------------------------------------- */
551 static ssize_t tpacpi_driver_interface_version_show(
552 struct device_driver *drv,
553 char *buf)
555 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
558 static DRIVER_ATTR(interface_version, S_IRUGO,
559 tpacpi_driver_interface_version_show, NULL);
561 /* debug_level --------------------------------------------------------- */
562 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
563 char *buf)
565 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
568 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
569 const char *buf, size_t count)
571 unsigned long t;
573 if (parse_strtoul(buf, 0xffff, &t))
574 return -EINVAL;
576 dbg_level = t;
578 return count;
581 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
582 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
584 /* version ------------------------------------------------------------- */
585 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
586 char *buf)
588 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
591 static DRIVER_ATTR(version, S_IRUGO,
592 tpacpi_driver_version_show, NULL);
594 /* --------------------------------------------------------------------- */
596 static struct driver_attribute* tpacpi_driver_attributes[] = {
597 &driver_attr_debug_level, &driver_attr_version,
598 &driver_attr_interface_version,
601 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
603 int i, res;
605 i = 0;
606 res = 0;
607 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
608 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
609 i++;
612 return res;
615 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
617 int i;
619 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
620 driver_remove_file(drv, tpacpi_driver_attributes[i]);
623 /*************************************************************************
624 * sysfs support helpers
627 struct attribute_set_obj {
628 struct attribute_set s;
629 struct attribute *a;
630 } __attribute__((packed));
632 static struct attribute_set *create_attr_set(unsigned int max_members,
633 const char* name)
635 struct attribute_set_obj *sobj;
637 if (max_members == 0)
638 return NULL;
640 /* Allocates space for implicit NULL at the end too */
641 sobj = kzalloc(sizeof(struct attribute_set_obj) +
642 max_members * sizeof(struct attribute *),
643 GFP_KERNEL);
644 if (!sobj)
645 return NULL;
646 sobj->s.max_members = max_members;
647 sobj->s.group.attrs = &sobj->a;
648 sobj->s.group.name = name;
650 return &sobj->s;
653 /* not multi-threaded safe, use it in a single thread per set */
654 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
656 if (!s || !attr)
657 return -EINVAL;
659 if (s->members >= s->max_members)
660 return -ENOMEM;
662 s->group.attrs[s->members] = attr;
663 s->members++;
665 return 0;
668 static int add_many_to_attr_set(struct attribute_set* s,
669 struct attribute **attr,
670 unsigned int count)
672 int i, res;
674 for (i = 0; i < count; i++) {
675 res = add_to_attr_set(s, attr[i]);
676 if (res)
677 return res;
680 return 0;
683 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
685 sysfs_remove_group(kobj, &s->group);
686 destroy_attr_set(s);
689 static int parse_strtoul(const char *buf,
690 unsigned long max, unsigned long *value)
692 char *endp;
694 *value = simple_strtoul(buf, &endp, 0);
695 while (*endp && isspace(*endp))
696 endp++;
697 if (*endp || *value > max)
698 return -EINVAL;
700 return 0;
703 /****************************************************************************
704 ****************************************************************************
706 * Subdrivers
708 ****************************************************************************
709 ****************************************************************************/
711 /*************************************************************************
712 * thinkpad-acpi init subdriver
715 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
717 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
718 printk(IBM_INFO "%s\n", IBM_URL);
720 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
721 (thinkpad_id.bios_version_str) ?
722 thinkpad_id.bios_version_str : "unknown",
723 (thinkpad_id.ec_version_str) ?
724 thinkpad_id.ec_version_str : "unknown");
726 if (thinkpad_id.vendor && thinkpad_id.model_str)
727 printk(IBM_INFO "%s %s\n",
728 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
729 "IBM" : ((thinkpad_id.vendor ==
730 PCI_VENDOR_ID_LENOVO) ?
731 "Lenovo" : "Unknown vendor"),
732 thinkpad_id.model_str);
734 return 0;
737 static int thinkpad_acpi_driver_read(char *p)
739 int len = 0;
741 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
742 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
744 return len;
747 static struct ibm_struct thinkpad_acpi_driver_data = {
748 .name = "driver",
749 .read = thinkpad_acpi_driver_read,
752 /*************************************************************************
753 * Hotkey subdriver
756 static int hotkey_orig_status;
757 static u32 hotkey_orig_mask;
758 static u32 hotkey_all_mask;
759 static u32 hotkey_reserved_mask;
761 static u16 hotkey_keycode_map[] = {
762 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
763 KEY_FN_F1, KEY_FN_F2, KEY_FN_F3, KEY_SLEEP,
764 KEY_FN_F5, KEY_FN_F6, KEY_FN_F7, KEY_FN_F8,
765 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
766 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
767 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
768 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
769 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
770 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
771 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
772 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
773 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
774 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
775 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
776 KEY_RESERVED, /* 0x14: VOLUME UP */
777 KEY_RESERVED, /* 0x15: VOLUME DOWN */
778 KEY_RESERVED, /* 0x16: MUTE */
779 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
780 /* (assignments unknown, please report if found) */
781 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
782 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
785 static struct attribute_set *hotkey_dev_attributes;
787 static int hotkey_get_wlsw(int *status)
789 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
790 return -EIO;
791 return 0;
794 /* sysfs hotkey enable ------------------------------------------------- */
795 static ssize_t hotkey_enable_show(struct device *dev,
796 struct device_attribute *attr,
797 char *buf)
799 int res, status;
800 u32 mask;
802 res = hotkey_get(&status, &mask);
803 if (res)
804 return res;
806 return snprintf(buf, PAGE_SIZE, "%d\n", status);
809 static ssize_t hotkey_enable_store(struct device *dev,
810 struct device_attribute *attr,
811 const char *buf, size_t count)
813 unsigned long t;
814 int res, status;
815 u32 mask;
817 if (parse_strtoul(buf, 1, &t))
818 return -EINVAL;
820 res = hotkey_get(&status, &mask);
821 if (!res)
822 res = hotkey_set(t, mask);
824 return (res) ? res : count;
827 static struct device_attribute dev_attr_hotkey_enable =
828 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
829 hotkey_enable_show, hotkey_enable_store);
831 /* sysfs hotkey mask --------------------------------------------------- */
832 static ssize_t hotkey_mask_show(struct device *dev,
833 struct device_attribute *attr,
834 char *buf)
836 int res, status;
837 u32 mask;
839 res = hotkey_get(&status, &mask);
840 if (res)
841 return res;
843 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
846 static ssize_t hotkey_mask_store(struct device *dev,
847 struct device_attribute *attr,
848 const char *buf, size_t count)
850 unsigned long t;
851 int res, status;
852 u32 mask;
854 if (parse_strtoul(buf, 0xffffffffUL, &t))
855 return -EINVAL;
857 res = hotkey_get(&status, &mask);
858 if (!res)
859 hotkey_set(status, t);
861 return (res) ? res : count;
864 static struct device_attribute dev_attr_hotkey_mask =
865 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
866 hotkey_mask_show, hotkey_mask_store);
868 /* sysfs hotkey bios_enabled ------------------------------------------- */
869 static ssize_t hotkey_bios_enabled_show(struct device *dev,
870 struct device_attribute *attr,
871 char *buf)
873 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
876 static struct device_attribute dev_attr_hotkey_bios_enabled =
877 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
879 /* sysfs hotkey bios_mask ---------------------------------------------- */
880 static ssize_t hotkey_bios_mask_show(struct device *dev,
881 struct device_attribute *attr,
882 char *buf)
884 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
887 static struct device_attribute dev_attr_hotkey_bios_mask =
888 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
890 /* sysfs hotkey all_mask ----------------------------------------------- */
891 static ssize_t hotkey_all_mask_show(struct device *dev,
892 struct device_attribute *attr,
893 char *buf)
895 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
898 static struct device_attribute dev_attr_hotkey_all_mask =
899 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
901 /* sysfs hotkey recommended_mask --------------------------------------- */
902 static ssize_t hotkey_recommended_mask_show(struct device *dev,
903 struct device_attribute *attr,
904 char *buf)
906 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
907 hotkey_all_mask & ~hotkey_reserved_mask);
910 static struct device_attribute dev_attr_hotkey_recommended_mask =
911 __ATTR(hotkey_recommended_mask, S_IRUGO,
912 hotkey_recommended_mask_show, NULL);
914 /* sysfs hotkey radio_sw ----------------------------------------------- */
915 static ssize_t hotkey_radio_sw_show(struct device *dev,
916 struct device_attribute *attr,
917 char *buf)
919 int res, s;
920 res = hotkey_get_wlsw(&s);
921 if (res < 0)
922 return res;
924 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
927 static struct device_attribute dev_attr_hotkey_radio_sw =
928 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
930 /* --------------------------------------------------------------------- */
932 static struct attribute *hotkey_mask_attributes[] = {
933 &dev_attr_hotkey_mask.attr,
934 &dev_attr_hotkey_bios_enabled.attr,
935 &dev_attr_hotkey_bios_mask.attr,
936 &dev_attr_hotkey_all_mask.attr,
937 &dev_attr_hotkey_recommended_mask.attr,
940 static int __init hotkey_init(struct ibm_init_struct *iibm)
942 int res, i;
943 int status;
945 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
947 BUG_ON(!tpacpi_inputdev);
949 IBM_ACPIHANDLE_INIT(hkey);
950 mutex_init(&hotkey_mutex);
952 /* hotkey not supported on 570 */
953 tp_features.hotkey = hkey_handle != NULL;
955 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
956 str_supported(tp_features.hotkey));
958 if (tp_features.hotkey) {
959 hotkey_dev_attributes = create_attr_set(7, NULL);
960 if (!hotkey_dev_attributes)
961 return -ENOMEM;
962 res = add_to_attr_set(hotkey_dev_attributes,
963 &dev_attr_hotkey_enable.attr);
964 if (res)
965 return res;
967 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
968 A30, R30, R31, T20-22, X20-21, X22-24 */
969 tp_features.hotkey_mask =
970 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
972 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
973 str_supported(tp_features.hotkey_mask));
975 if (tp_features.hotkey_mask) {
976 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
977 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
978 "MHKA", "qd"))
979 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
982 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
983 if (!res && tp_features.hotkey_mask) {
984 res = add_many_to_attr_set(hotkey_dev_attributes,
985 hotkey_mask_attributes,
986 ARRAY_SIZE(hotkey_mask_attributes));
989 /* Not all thinkpads have a hardware radio switch */
990 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
991 tp_features.hotkey_wlsw = 1;
992 printk(IBM_INFO
993 "radio switch found; radios are %s\n",
994 enabled(status, 0));
995 res = add_to_attr_set(hotkey_dev_attributes,
996 &dev_attr_hotkey_radio_sw.attr);
999 if (!res)
1000 res = register_attr_set_with_sysfs(
1001 hotkey_dev_attributes,
1002 &tpacpi_pdev->dev.kobj);
1003 if (res)
1004 return res;
1006 #ifndef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
1007 for (i = 0; i < 12; i++)
1008 hotkey_keycode_map[i] = KEY_UNKNOWN;
1009 #endif /* ! CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
1011 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1012 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1013 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1014 tpacpi_inputdev->keycodesize = sizeof(hotkey_keycode_map[0]);
1015 tpacpi_inputdev->keycodemax = ARRAY_SIZE(hotkey_keycode_map);
1016 tpacpi_inputdev->keycode = &hotkey_keycode_map;
1017 for (i = 0; i < ARRAY_SIZE(hotkey_keycode_map); i++) {
1018 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1019 set_bit(hotkey_keycode_map[i],
1020 tpacpi_inputdev->keybit);
1021 } else {
1022 if (i < sizeof(hotkey_reserved_mask)*8)
1023 hotkey_reserved_mask |= 1 << i;
1027 if (tp_features.hotkey_wlsw) {
1028 set_bit(EV_SW, tpacpi_inputdev->evbit);
1029 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1032 #ifdef CONFIG_THINKPAD_ACPI_INPUT_ENABLED
1033 dbg_printk(TPACPI_DBG_INIT,
1034 "enabling hot key handling\n");
1035 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1036 | hotkey_orig_mask);
1037 if (res)
1038 return res;
1039 #endif /* CONFIG_THINKPAD_ACPI_INPUT_ENABLED */
1042 return (tp_features.hotkey)? 0 : 1;
1045 static void hotkey_exit(void)
1047 int res;
1049 if (tp_features.hotkey) {
1050 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
1051 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1052 if (res)
1053 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
1056 if (hotkey_dev_attributes) {
1057 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1058 hotkey_dev_attributes = NULL;
1062 static void tpacpi_input_send_key(unsigned int scancode,
1063 unsigned int keycode)
1065 if (keycode != KEY_RESERVED) {
1066 input_report_key(tpacpi_inputdev, keycode, 1);
1067 if (keycode == KEY_UNKNOWN)
1068 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1069 scancode);
1070 input_sync(tpacpi_inputdev);
1072 input_report_key(tpacpi_inputdev, keycode, 0);
1073 if (keycode == KEY_UNKNOWN)
1074 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1075 scancode);
1076 input_sync(tpacpi_inputdev);
1080 static void tpacpi_input_send_radiosw(void)
1082 int wlsw;
1084 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw))
1085 input_report_switch(tpacpi_inputdev,
1086 SW_RADIO, !!wlsw);
1089 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1091 u32 hkey;
1092 unsigned int keycode, scancode;
1093 int sendacpi = 1;
1095 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1096 if (tpacpi_inputdev->users > 0) {
1097 switch (hkey >> 12) {
1098 case 1:
1099 /* 0x1000-0x1FFF: key presses */
1100 scancode = hkey & 0xfff;
1101 if (scancode > 0 && scancode < 0x21) {
1102 scancode--;
1103 keycode = hotkey_keycode_map[scancode];
1104 tpacpi_input_send_key(scancode, keycode);
1105 sendacpi = (keycode == KEY_RESERVED
1106 || keycode == KEY_UNKNOWN);
1107 } else {
1108 printk(IBM_ERR
1109 "hotkey 0x%04x out of range for keyboard map\n",
1110 hkey);
1112 break;
1113 case 5:
1114 /* 0x5000-0x5FFF: LID */
1115 /* we don't handle it through this path, just
1116 * eat up known LID events */
1117 if (hkey != 0x5001 && hkey != 0x5002) {
1118 printk(IBM_ERR
1119 "unknown LID-related hotkey event: 0x%04x\n",
1120 hkey);
1122 break;
1123 case 7:
1124 /* 0x7000-0x7FFF: misc */
1125 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1126 tpacpi_input_send_radiosw();
1127 sendacpi = 0;
1128 break;
1130 /* fallthrough to default */
1131 default:
1132 /* case 2: dock-related */
1133 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1134 /* case 3: ultra-bay related. maybe bay in dock? */
1135 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1136 printk(IBM_NOTICE "unhandled hotkey event 0x%04x\n", hkey);
1140 if (sendacpi)
1141 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
1142 } else {
1143 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
1144 acpi_bus_generate_event(ibm->acpi->device, event, 0);
1148 static void hotkey_resume(void)
1150 tpacpi_input_send_radiosw();
1154 * Call with hotkey_mutex held
1156 static int hotkey_get(int *status, u32 *mask)
1158 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1159 return -EIO;
1161 if (tp_features.hotkey_mask)
1162 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
1163 return -EIO;
1165 return 0;
1169 * Call with hotkey_mutex held
1171 static int hotkey_set(int status, u32 mask)
1173 int i;
1175 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1176 return -EIO;
1178 if (tp_features.hotkey_mask)
1179 for (i = 0; i < 32; i++) {
1180 int bit = ((1 << i) & mask) != 0;
1181 if (!acpi_evalf(hkey_handle,
1182 NULL, "MHKM", "vdd", i + 1, bit))
1183 return -EIO;
1186 return 0;
1189 /* procfs -------------------------------------------------------------- */
1190 static int hotkey_read(char *p)
1192 int res, status;
1193 u32 mask;
1194 int len = 0;
1196 if (!tp_features.hotkey) {
1197 len += sprintf(p + len, "status:\t\tnot supported\n");
1198 return len;
1201 res = mutex_lock_interruptible(&hotkey_mutex);
1202 if (res < 0)
1203 return res;
1204 res = hotkey_get(&status, &mask);
1205 mutex_unlock(&hotkey_mutex);
1206 if (res)
1207 return res;
1209 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1210 if (tp_features.hotkey_mask) {
1211 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1212 len += sprintf(p + len,
1213 "commands:\tenable, disable, reset, <mask>\n");
1214 } else {
1215 len += sprintf(p + len, "mask:\t\tnot supported\n");
1216 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1219 return len;
1222 static int hotkey_write(char *buf)
1224 int res, status;
1225 u32 mask;
1226 char *cmd;
1227 int do_cmd = 0;
1229 if (!tp_features.hotkey)
1230 return -ENODEV;
1232 res = mutex_lock_interruptible(&hotkey_mutex);
1233 if (res < 0)
1234 return res;
1236 res = hotkey_get(&status, &mask);
1237 if (res)
1238 goto errexit;
1240 res = 0;
1241 while ((cmd = next_cmd(&buf))) {
1242 if (strlencmp(cmd, "enable") == 0) {
1243 status = 1;
1244 } else if (strlencmp(cmd, "disable") == 0) {
1245 status = 0;
1246 } else if (strlencmp(cmd, "reset") == 0) {
1247 status = hotkey_orig_status;
1248 mask = hotkey_orig_mask;
1249 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1250 /* mask set */
1251 } else if (sscanf(cmd, "%x", &mask) == 1) {
1252 /* mask set */
1253 } else {
1254 res = -EINVAL;
1255 goto errexit;
1257 do_cmd = 1;
1260 if (do_cmd)
1261 res = hotkey_set(status, mask);
1263 errexit:
1264 mutex_unlock(&hotkey_mutex);
1265 return res;
1268 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1269 .hid = IBM_HKEY_HID,
1270 .notify = hotkey_notify,
1271 .handle = &hkey_handle,
1272 .type = ACPI_DEVICE_NOTIFY,
1275 static struct ibm_struct hotkey_driver_data = {
1276 .name = "hotkey",
1277 .read = hotkey_read,
1278 .write = hotkey_write,
1279 .exit = hotkey_exit,
1280 .resume = hotkey_resume,
1281 .acpi = &ibm_hotkey_acpidriver,
1284 /*************************************************************************
1285 * Bluetooth subdriver
1288 /* sysfs bluetooth enable ---------------------------------------------- */
1289 static ssize_t bluetooth_enable_show(struct device *dev,
1290 struct device_attribute *attr,
1291 char *buf)
1293 int status;
1295 status = bluetooth_get_radiosw();
1296 if (status < 0)
1297 return status;
1299 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1302 static ssize_t bluetooth_enable_store(struct device *dev,
1303 struct device_attribute *attr,
1304 const char *buf, size_t count)
1306 unsigned long t;
1307 int res;
1309 if (parse_strtoul(buf, 1, &t))
1310 return -EINVAL;
1312 res = bluetooth_set_radiosw(t);
1314 return (res) ? res : count;
1317 static struct device_attribute dev_attr_bluetooth_enable =
1318 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1319 bluetooth_enable_show, bluetooth_enable_store);
1321 /* --------------------------------------------------------------------- */
1323 static struct attribute *bluetooth_attributes[] = {
1324 &dev_attr_bluetooth_enable.attr,
1325 NULL
1328 static const struct attribute_group bluetooth_attr_group = {
1329 .attrs = bluetooth_attributes,
1332 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1334 int res;
1335 int status = 0;
1337 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1339 IBM_ACPIHANDLE_INIT(hkey);
1341 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1342 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1343 tp_features.bluetooth = hkey_handle &&
1344 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1346 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1347 str_supported(tp_features.bluetooth),
1348 status);
1350 if (tp_features.bluetooth) {
1351 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1352 /* no bluetooth hardware present in system */
1353 tp_features.bluetooth = 0;
1354 dbg_printk(TPACPI_DBG_INIT,
1355 "bluetooth hardware not installed\n");
1356 } else {
1357 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1358 &bluetooth_attr_group);
1359 if (res)
1360 return res;
1364 return (tp_features.bluetooth)? 0 : 1;
1367 static void bluetooth_exit(void)
1369 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1370 &bluetooth_attr_group);
1373 static int bluetooth_get_radiosw(void)
1375 int status;
1377 if (!tp_features.bluetooth)
1378 return -ENODEV;
1380 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1381 return -EIO;
1383 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1386 static int bluetooth_set_radiosw(int radio_on)
1388 int status;
1390 if (!tp_features.bluetooth)
1391 return -ENODEV;
1393 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1394 return -EIO;
1395 if (radio_on)
1396 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1397 else
1398 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1399 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1400 return -EIO;
1402 return 0;
1405 /* procfs -------------------------------------------------------------- */
1406 static int bluetooth_read(char *p)
1408 int len = 0;
1409 int status = bluetooth_get_radiosw();
1411 if (!tp_features.bluetooth)
1412 len += sprintf(p + len, "status:\t\tnot supported\n");
1413 else {
1414 len += sprintf(p + len, "status:\t\t%s\n",
1415 (status)? "enabled" : "disabled");
1416 len += sprintf(p + len, "commands:\tenable, disable\n");
1419 return len;
1422 static int bluetooth_write(char *buf)
1424 char *cmd;
1426 if (!tp_features.bluetooth)
1427 return -ENODEV;
1429 while ((cmd = next_cmd(&buf))) {
1430 if (strlencmp(cmd, "enable") == 0) {
1431 bluetooth_set_radiosw(1);
1432 } else if (strlencmp(cmd, "disable") == 0) {
1433 bluetooth_set_radiosw(0);
1434 } else
1435 return -EINVAL;
1438 return 0;
1441 static struct ibm_struct bluetooth_driver_data = {
1442 .name = "bluetooth",
1443 .read = bluetooth_read,
1444 .write = bluetooth_write,
1445 .exit = bluetooth_exit,
1448 /*************************************************************************
1449 * Wan subdriver
1452 /* sysfs wan enable ---------------------------------------------------- */
1453 static ssize_t wan_enable_show(struct device *dev,
1454 struct device_attribute *attr,
1455 char *buf)
1457 int status;
1459 status = wan_get_radiosw();
1460 if (status < 0)
1461 return status;
1463 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1466 static ssize_t wan_enable_store(struct device *dev,
1467 struct device_attribute *attr,
1468 const char *buf, size_t count)
1470 unsigned long t;
1471 int res;
1473 if (parse_strtoul(buf, 1, &t))
1474 return -EINVAL;
1476 res = wan_set_radiosw(t);
1478 return (res) ? res : count;
1481 static struct device_attribute dev_attr_wan_enable =
1482 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1483 wan_enable_show, wan_enable_store);
1485 /* --------------------------------------------------------------------- */
1487 static struct attribute *wan_attributes[] = {
1488 &dev_attr_wan_enable.attr,
1489 NULL
1492 static const struct attribute_group wan_attr_group = {
1493 .attrs = wan_attributes,
1496 static int __init wan_init(struct ibm_init_struct *iibm)
1498 int res;
1499 int status = 0;
1501 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1503 IBM_ACPIHANDLE_INIT(hkey);
1505 tp_features.wan = hkey_handle &&
1506 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1508 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1509 str_supported(tp_features.wan),
1510 status);
1512 if (tp_features.wan) {
1513 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1514 /* no wan hardware present in system */
1515 tp_features.wan = 0;
1516 dbg_printk(TPACPI_DBG_INIT,
1517 "wan hardware not installed\n");
1518 } else {
1519 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1520 &wan_attr_group);
1521 if (res)
1522 return res;
1526 return (tp_features.wan)? 0 : 1;
1529 static void wan_exit(void)
1531 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1532 &wan_attr_group);
1535 static int wan_get_radiosw(void)
1537 int status;
1539 if (!tp_features.wan)
1540 return -ENODEV;
1542 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1543 return -EIO;
1545 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1548 static int wan_set_radiosw(int radio_on)
1550 int status;
1552 if (!tp_features.wan)
1553 return -ENODEV;
1555 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1556 return -EIO;
1557 if (radio_on)
1558 status |= TP_ACPI_WANCARD_RADIOSSW;
1559 else
1560 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1561 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1562 return -EIO;
1564 return 0;
1567 /* procfs -------------------------------------------------------------- */
1568 static int wan_read(char *p)
1570 int len = 0;
1571 int status = wan_get_radiosw();
1573 if (!tp_features.wan)
1574 len += sprintf(p + len, "status:\t\tnot supported\n");
1575 else {
1576 len += sprintf(p + len, "status:\t\t%s\n",
1577 (status)? "enabled" : "disabled");
1578 len += sprintf(p + len, "commands:\tenable, disable\n");
1581 return len;
1584 static int wan_write(char *buf)
1586 char *cmd;
1588 if (!tp_features.wan)
1589 return -ENODEV;
1591 while ((cmd = next_cmd(&buf))) {
1592 if (strlencmp(cmd, "enable") == 0) {
1593 wan_set_radiosw(1);
1594 } else if (strlencmp(cmd, "disable") == 0) {
1595 wan_set_radiosw(0);
1596 } else
1597 return -EINVAL;
1600 return 0;
1603 static struct ibm_struct wan_driver_data = {
1604 .name = "wan",
1605 .read = wan_read,
1606 .write = wan_write,
1607 .exit = wan_exit,
1608 .flags.experimental = 1,
1611 /*************************************************************************
1612 * Video subdriver
1615 static enum video_access_mode video_supported;
1616 static int video_orig_autosw;
1618 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1619 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1620 "\\_SB.PCI0.VID0", /* 770e */
1621 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1622 "\\_SB.PCI0.AGP.VID", /* all others */
1623 ); /* R30, R31 */
1625 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1627 static int __init video_init(struct ibm_init_struct *iibm)
1629 int ivga;
1631 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1633 IBM_ACPIHANDLE_INIT(vid);
1634 IBM_ACPIHANDLE_INIT(vid2);
1636 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1637 /* G41, assume IVGA doesn't change */
1638 vid_handle = vid2_handle;
1640 if (!vid_handle)
1641 /* video switching not supported on R30, R31 */
1642 video_supported = TPACPI_VIDEO_NONE;
1643 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1644 /* 570 */
1645 video_supported = TPACPI_VIDEO_570;
1646 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1647 /* 600e/x, 770e, 770x */
1648 video_supported = TPACPI_VIDEO_770;
1649 else
1650 /* all others */
1651 video_supported = TPACPI_VIDEO_NEW;
1653 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1654 str_supported(video_supported != TPACPI_VIDEO_NONE),
1655 video_supported);
1657 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1660 static void video_exit(void)
1662 dbg_printk(TPACPI_DBG_EXIT,
1663 "restoring original video autoswitch mode\n");
1664 if (video_autosw_set(video_orig_autosw))
1665 printk(IBM_ERR "error while trying to restore original "
1666 "video autoswitch mode\n");
1669 static int video_outputsw_get(void)
1671 int status = 0;
1672 int i;
1674 switch (video_supported) {
1675 case TPACPI_VIDEO_570:
1676 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1677 TP_ACPI_VIDEO_570_PHSCMD))
1678 return -EIO;
1679 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1680 break;
1681 case TPACPI_VIDEO_770:
1682 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1683 return -EIO;
1684 if (i)
1685 status |= TP_ACPI_VIDEO_S_LCD;
1686 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1687 return -EIO;
1688 if (i)
1689 status |= TP_ACPI_VIDEO_S_CRT;
1690 break;
1691 case TPACPI_VIDEO_NEW:
1692 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1693 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1694 return -EIO;
1695 if (i)
1696 status |= TP_ACPI_VIDEO_S_CRT;
1698 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1699 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1700 return -EIO;
1701 if (i)
1702 status |= TP_ACPI_VIDEO_S_LCD;
1703 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1704 return -EIO;
1705 if (i)
1706 status |= TP_ACPI_VIDEO_S_DVI;
1707 break;
1708 default:
1709 return -ENOSYS;
1712 return status;
1715 static int video_outputsw_set(int status)
1717 int autosw;
1718 int res = 0;
1720 switch (video_supported) {
1721 case TPACPI_VIDEO_570:
1722 res = acpi_evalf(NULL, NULL,
1723 "\\_SB.PHS2", "vdd",
1724 TP_ACPI_VIDEO_570_PHS2CMD,
1725 status | TP_ACPI_VIDEO_570_PHS2SET);
1726 break;
1727 case TPACPI_VIDEO_770:
1728 autosw = video_autosw_get();
1729 if (autosw < 0)
1730 return autosw;
1732 res = video_autosw_set(1);
1733 if (res)
1734 return res;
1735 res = acpi_evalf(vid_handle, NULL,
1736 "ASWT", "vdd", status * 0x100, 0);
1737 if (!autosw && video_autosw_set(autosw)) {
1738 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1739 return -EIO;
1741 break;
1742 case TPACPI_VIDEO_NEW:
1743 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1744 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1745 break;
1746 default:
1747 return -ENOSYS;
1750 return (res)? 0 : -EIO;
1753 static int video_autosw_get(void)
1755 int autosw = 0;
1757 switch (video_supported) {
1758 case TPACPI_VIDEO_570:
1759 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1760 return -EIO;
1761 break;
1762 case TPACPI_VIDEO_770:
1763 case TPACPI_VIDEO_NEW:
1764 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1765 return -EIO;
1766 break;
1767 default:
1768 return -ENOSYS;
1771 return autosw & 1;
1774 static int video_autosw_set(int enable)
1776 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1777 return -EIO;
1778 return 0;
1781 static int video_outputsw_cycle(void)
1783 int autosw = video_autosw_get();
1784 int res;
1786 if (autosw < 0)
1787 return autosw;
1789 switch (video_supported) {
1790 case TPACPI_VIDEO_570:
1791 res = video_autosw_set(1);
1792 if (res)
1793 return res;
1794 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1795 break;
1796 case TPACPI_VIDEO_770:
1797 case TPACPI_VIDEO_NEW:
1798 res = video_autosw_set(1);
1799 if (res)
1800 return res;
1801 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1802 break;
1803 default:
1804 return -ENOSYS;
1806 if (!autosw && video_autosw_set(autosw)) {
1807 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1808 return -EIO;
1811 return (res)? 0 : -EIO;
1814 static int video_expand_toggle(void)
1816 switch (video_supported) {
1817 case TPACPI_VIDEO_570:
1818 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1819 0 : -EIO;
1820 case TPACPI_VIDEO_770:
1821 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1822 0 : -EIO;
1823 case TPACPI_VIDEO_NEW:
1824 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1825 0 : -EIO;
1826 default:
1827 return -ENOSYS;
1829 /* not reached */
1832 static int video_read(char *p)
1834 int status, autosw;
1835 int len = 0;
1837 if (video_supported == TPACPI_VIDEO_NONE) {
1838 len += sprintf(p + len, "status:\t\tnot supported\n");
1839 return len;
1842 status = video_outputsw_get();
1843 if (status < 0)
1844 return status;
1846 autosw = video_autosw_get();
1847 if (autosw < 0)
1848 return autosw;
1850 len += sprintf(p + len, "status:\t\tsupported\n");
1851 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1852 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1853 if (video_supported == TPACPI_VIDEO_NEW)
1854 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1855 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1856 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1857 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1858 if (video_supported == TPACPI_VIDEO_NEW)
1859 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1860 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1861 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1863 return len;
1866 static int video_write(char *buf)
1868 char *cmd;
1869 int enable, disable, status;
1870 int res;
1872 if (video_supported == TPACPI_VIDEO_NONE)
1873 return -ENODEV;
1875 enable = 0;
1876 disable = 0;
1878 while ((cmd = next_cmd(&buf))) {
1879 if (strlencmp(cmd, "lcd_enable") == 0) {
1880 enable |= TP_ACPI_VIDEO_S_LCD;
1881 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1882 disable |= TP_ACPI_VIDEO_S_LCD;
1883 } else if (strlencmp(cmd, "crt_enable") == 0) {
1884 enable |= TP_ACPI_VIDEO_S_CRT;
1885 } else if (strlencmp(cmd, "crt_disable") == 0) {
1886 disable |= TP_ACPI_VIDEO_S_CRT;
1887 } else if (video_supported == TPACPI_VIDEO_NEW &&
1888 strlencmp(cmd, "dvi_enable") == 0) {
1889 enable |= TP_ACPI_VIDEO_S_DVI;
1890 } else if (video_supported == TPACPI_VIDEO_NEW &&
1891 strlencmp(cmd, "dvi_disable") == 0) {
1892 disable |= TP_ACPI_VIDEO_S_DVI;
1893 } else if (strlencmp(cmd, "auto_enable") == 0) {
1894 res = video_autosw_set(1);
1895 if (res)
1896 return res;
1897 } else if (strlencmp(cmd, "auto_disable") == 0) {
1898 res = video_autosw_set(0);
1899 if (res)
1900 return res;
1901 } else if (strlencmp(cmd, "video_switch") == 0) {
1902 res = video_outputsw_cycle();
1903 if (res)
1904 return res;
1905 } else if (strlencmp(cmd, "expand_toggle") == 0) {
1906 res = video_expand_toggle();
1907 if (res)
1908 return res;
1909 } else
1910 return -EINVAL;
1913 if (enable || disable) {
1914 status = video_outputsw_get();
1915 if (status < 0)
1916 return status;
1917 res = video_outputsw_set((status & ~disable) | enable);
1918 if (res)
1919 return res;
1922 return 0;
1925 static struct ibm_struct video_driver_data = {
1926 .name = "video",
1927 .read = video_read,
1928 .write = video_write,
1929 .exit = video_exit,
1932 /*************************************************************************
1933 * Light (thinklight) subdriver
1936 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1937 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1939 static int __init light_init(struct ibm_init_struct *iibm)
1941 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1943 IBM_ACPIHANDLE_INIT(ledb);
1944 IBM_ACPIHANDLE_INIT(lght);
1945 IBM_ACPIHANDLE_INIT(cmos);
1947 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1948 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
1950 if (tp_features.light)
1951 /* light status not supported on
1952 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1953 tp_features.light_status =
1954 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1956 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
1957 str_supported(tp_features.light));
1959 return (tp_features.light)? 0 : 1;
1962 static int light_read(char *p)
1964 int len = 0;
1965 int status = 0;
1967 if (!tp_features.light) {
1968 len += sprintf(p + len, "status:\t\tnot supported\n");
1969 } else if (!tp_features.light_status) {
1970 len += sprintf(p + len, "status:\t\tunknown\n");
1971 len += sprintf(p + len, "commands:\ton, off\n");
1972 } else {
1973 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1974 return -EIO;
1975 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1976 len += sprintf(p + len, "commands:\ton, off\n");
1979 return len;
1982 static int light_write(char *buf)
1984 int cmos_cmd, lght_cmd;
1985 char *cmd;
1986 int success;
1988 if (!tp_features.light)
1989 return -ENODEV;
1991 while ((cmd = next_cmd(&buf))) {
1992 if (strlencmp(cmd, "on") == 0) {
1993 cmos_cmd = 0x0c;
1994 lght_cmd = 1;
1995 } else if (strlencmp(cmd, "off") == 0) {
1996 cmos_cmd = 0x0d;
1997 lght_cmd = 0;
1998 } else
1999 return -EINVAL;
2001 success = cmos_handle ?
2002 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2003 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2004 if (!success)
2005 return -EIO;
2008 return 0;
2011 static struct ibm_struct light_driver_data = {
2012 .name = "light",
2013 .read = light_read,
2014 .write = light_write,
2017 /*************************************************************************
2018 * Dock subdriver
2021 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2023 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2024 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2025 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2026 "\\_SB.PCI.ISA.SLCE", /* 570 */
2027 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2029 /* don't list other alternatives as we install a notify handler on the 570 */
2030 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2032 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2034 .notify = dock_notify,
2035 .handle = &dock_handle,
2036 .type = ACPI_SYSTEM_NOTIFY,
2039 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2040 * We just use it to get notifications of dock hotplug
2041 * in very old thinkpads */
2042 .hid = PCI_ROOT_HID_STRING,
2043 .notify = dock_notify,
2044 .handle = &pci_handle,
2045 .type = ACPI_SYSTEM_NOTIFY,
2049 static struct ibm_struct dock_driver_data[2] = {
2051 .name = "dock",
2052 .read = dock_read,
2053 .write = dock_write,
2054 .acpi = &ibm_dock_acpidriver[0],
2057 .name = "dock",
2058 .acpi = &ibm_dock_acpidriver[1],
2062 #define dock_docked() (_sta(dock_handle) & 1)
2064 static int __init dock_init(struct ibm_init_struct *iibm)
2066 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2068 IBM_ACPIHANDLE_INIT(dock);
2070 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2071 str_supported(dock_handle != NULL));
2073 return (dock_handle)? 0 : 1;
2076 static int __init dock_init2(struct ibm_init_struct *iibm)
2078 int dock2_needed;
2080 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2082 if (dock_driver_data[0].flags.acpi_driver_registered &&
2083 dock_driver_data[0].flags.acpi_notify_installed) {
2084 IBM_ACPIHANDLE_INIT(pci);
2085 dock2_needed = (pci_handle != NULL);
2086 vdbg_printk(TPACPI_DBG_INIT,
2087 "dock PCI handler for the TP 570 is %s\n",
2088 str_supported(dock2_needed));
2089 } else {
2090 vdbg_printk(TPACPI_DBG_INIT,
2091 "dock subdriver part 2 not required\n");
2092 dock2_needed = 0;
2095 return (dock2_needed)? 0 : 1;
2098 static void dock_notify(struct ibm_struct *ibm, u32 event)
2100 int docked = dock_docked();
2101 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, PCI_ROOT_HID_STRING);
2103 if (event == 1 && !pci) /* 570 */
2104 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2105 else if (event == 1 && pci) /* 570 */
2106 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2107 else if (event == 3 && docked)
2108 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2109 else if (event == 3 && !docked)
2110 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
2111 else if (event == 0 && docked)
2112 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2113 else {
2114 printk(IBM_ERR "unknown dock event %d, status %d\n",
2115 event, _sta(dock_handle));
2116 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
2120 static int dock_read(char *p)
2122 int len = 0;
2123 int docked = dock_docked();
2125 if (!dock_handle)
2126 len += sprintf(p + len, "status:\t\tnot supported\n");
2127 else if (!docked)
2128 len += sprintf(p + len, "status:\t\tundocked\n");
2129 else {
2130 len += sprintf(p + len, "status:\t\tdocked\n");
2131 len += sprintf(p + len, "commands:\tdock, undock\n");
2134 return len;
2137 static int dock_write(char *buf)
2139 char *cmd;
2141 if (!dock_docked())
2142 return -ENODEV;
2144 while ((cmd = next_cmd(&buf))) {
2145 if (strlencmp(cmd, "undock") == 0) {
2146 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2147 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2148 return -EIO;
2149 } else if (strlencmp(cmd, "dock") == 0) {
2150 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2151 return -EIO;
2152 } else
2153 return -EINVAL;
2156 return 0;
2159 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2161 /*************************************************************************
2162 * Bay subdriver
2165 #ifdef CONFIG_THINKPAD_ACPI_BAY
2166 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2167 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2168 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2169 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2170 ); /* A21e, R30, R31 */
2171 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2172 "_EJ0", /* all others */
2173 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2174 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2175 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2176 ); /* all others */
2177 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2178 "_EJ0", /* 770x */
2179 ); /* all others */
2181 static int __init bay_init(struct ibm_init_struct *iibm)
2183 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2185 IBM_ACPIHANDLE_INIT(bay);
2186 if (bay_handle)
2187 IBM_ACPIHANDLE_INIT(bay_ej);
2188 IBM_ACPIHANDLE_INIT(bay2);
2189 if (bay2_handle)
2190 IBM_ACPIHANDLE_INIT(bay2_ej);
2192 tp_features.bay_status = bay_handle &&
2193 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2194 tp_features.bay_status2 = bay2_handle &&
2195 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2197 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2198 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2199 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2200 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2202 vdbg_printk(TPACPI_DBG_INIT,
2203 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2204 str_supported(tp_features.bay_status),
2205 str_supported(tp_features.bay_eject),
2206 str_supported(tp_features.bay_status2),
2207 str_supported(tp_features.bay_eject2));
2209 return (tp_features.bay_status || tp_features.bay_eject ||
2210 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2213 static void bay_notify(struct ibm_struct *ibm, u32 event)
2215 acpi_bus_generate_event(ibm->acpi->device, event, 0);
2218 #define bay_occupied(b) (_sta(b##_handle) & 1)
2220 static int bay_read(char *p)
2222 int len = 0;
2223 int occupied = bay_occupied(bay);
2224 int occupied2 = bay_occupied(bay2);
2225 int eject, eject2;
2227 len += sprintf(p + len, "status:\t\t%s\n",
2228 tp_features.bay_status ?
2229 (occupied ? "occupied" : "unoccupied") :
2230 "not supported");
2231 if (tp_features.bay_status2)
2232 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2233 "occupied" : "unoccupied");
2235 eject = tp_features.bay_eject && occupied;
2236 eject2 = tp_features.bay_eject2 && occupied2;
2238 if (eject && eject2)
2239 len += sprintf(p + len, "commands:\teject, eject2\n");
2240 else if (eject)
2241 len += sprintf(p + len, "commands:\teject\n");
2242 else if (eject2)
2243 len += sprintf(p + len, "commands:\teject2\n");
2245 return len;
2248 static int bay_write(char *buf)
2250 char *cmd;
2252 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2253 return -ENODEV;
2255 while ((cmd = next_cmd(&buf))) {
2256 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2257 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2258 return -EIO;
2259 } else if (tp_features.bay_eject2 &&
2260 strlencmp(cmd, "eject2") == 0) {
2261 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2262 return -EIO;
2263 } else
2264 return -EINVAL;
2267 return 0;
2270 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2271 .notify = bay_notify,
2272 .handle = &bay_handle,
2273 .type = ACPI_SYSTEM_NOTIFY,
2276 static struct ibm_struct bay_driver_data = {
2277 .name = "bay",
2278 .read = bay_read,
2279 .write = bay_write,
2280 .acpi = &ibm_bay_acpidriver,
2283 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2285 /*************************************************************************
2286 * CMOS subdriver
2289 /* sysfs cmos_command -------------------------------------------------- */
2290 static ssize_t cmos_command_store(struct device *dev,
2291 struct device_attribute *attr,
2292 const char *buf, size_t count)
2294 unsigned long cmos_cmd;
2295 int res;
2297 if (parse_strtoul(buf, 21, &cmos_cmd))
2298 return -EINVAL;
2300 res = issue_thinkpad_cmos_command(cmos_cmd);
2301 return (res)? res : count;
2304 static struct device_attribute dev_attr_cmos_command =
2305 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2307 /* --------------------------------------------------------------------- */
2309 static int __init cmos_init(struct ibm_init_struct *iibm)
2311 int res;
2313 vdbg_printk(TPACPI_DBG_INIT,
2314 "initializing cmos commands subdriver\n");
2316 IBM_ACPIHANDLE_INIT(cmos);
2318 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2319 str_supported(cmos_handle != NULL));
2321 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2322 if (res)
2323 return res;
2325 return (cmos_handle)? 0 : 1;
2328 static void cmos_exit(void)
2330 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2333 static int cmos_read(char *p)
2335 int len = 0;
2337 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2338 R30, R31, T20-22, X20-21 */
2339 if (!cmos_handle)
2340 len += sprintf(p + len, "status:\t\tnot supported\n");
2341 else {
2342 len += sprintf(p + len, "status:\t\tsupported\n");
2343 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2346 return len;
2349 static int cmos_write(char *buf)
2351 char *cmd;
2352 int cmos_cmd, res;
2354 while ((cmd = next_cmd(&buf))) {
2355 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2356 cmos_cmd >= 0 && cmos_cmd <= 21) {
2357 /* cmos_cmd set */
2358 } else
2359 return -EINVAL;
2361 res = issue_thinkpad_cmos_command(cmos_cmd);
2362 if (res)
2363 return res;
2366 return 0;
2369 static struct ibm_struct cmos_driver_data = {
2370 .name = "cmos",
2371 .read = cmos_read,
2372 .write = cmos_write,
2373 .exit = cmos_exit,
2376 /*************************************************************************
2377 * LED subdriver
2380 static enum led_access_mode led_supported;
2382 IBM_HANDLE(led, ec, "SLED", /* 570 */
2383 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2384 "LED", /* all others */
2385 ); /* R30, R31 */
2387 static int __init led_init(struct ibm_init_struct *iibm)
2389 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2391 IBM_ACPIHANDLE_INIT(led);
2393 if (!led_handle)
2394 /* led not supported on R30, R31 */
2395 led_supported = TPACPI_LED_NONE;
2396 else if (strlencmp(led_path, "SLED") == 0)
2397 /* 570 */
2398 led_supported = TPACPI_LED_570;
2399 else if (strlencmp(led_path, "SYSL") == 0)
2400 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2401 led_supported = TPACPI_LED_OLD;
2402 else
2403 /* all others */
2404 led_supported = TPACPI_LED_NEW;
2406 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2407 str_supported(led_supported), led_supported);
2409 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2412 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2414 static int led_read(char *p)
2416 int len = 0;
2418 if (!led_supported) {
2419 len += sprintf(p + len, "status:\t\tnot supported\n");
2420 return len;
2422 len += sprintf(p + len, "status:\t\tsupported\n");
2424 if (led_supported == TPACPI_LED_570) {
2425 /* 570 */
2426 int i, status;
2427 for (i = 0; i < 8; i++) {
2428 if (!acpi_evalf(ec_handle,
2429 &status, "GLED", "dd", 1 << i))
2430 return -EIO;
2431 len += sprintf(p + len, "%d:\t\t%s\n",
2432 i, led_status(status));
2436 len += sprintf(p + len, "commands:\t"
2437 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2439 return len;
2442 /* off, on, blink */
2443 static const int led_sled_arg1[] = { 0, 1, 3 };
2444 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2445 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2446 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2448 static int led_write(char *buf)
2450 char *cmd;
2451 int led, ind, ret;
2453 if (!led_supported)
2454 return -ENODEV;
2456 while ((cmd = next_cmd(&buf))) {
2457 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2458 return -EINVAL;
2460 if (strstr(cmd, "off")) {
2461 ind = 0;
2462 } else if (strstr(cmd, "on")) {
2463 ind = 1;
2464 } else if (strstr(cmd, "blink")) {
2465 ind = 2;
2466 } else
2467 return -EINVAL;
2469 if (led_supported == TPACPI_LED_570) {
2470 /* 570 */
2471 led = 1 << led;
2472 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2473 led, led_sled_arg1[ind]))
2474 return -EIO;
2475 } else if (led_supported == TPACPI_LED_OLD) {
2476 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2477 led = 1 << led;
2478 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2479 if (ret >= 0)
2480 ret =
2481 ec_write(TPACPI_LED_EC_HLBL,
2482 led * led_exp_hlbl[ind]);
2483 if (ret >= 0)
2484 ret =
2485 ec_write(TPACPI_LED_EC_HLCL,
2486 led * led_exp_hlcl[ind]);
2487 if (ret < 0)
2488 return ret;
2489 } else {
2490 /* all others */
2491 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2492 led, led_led_arg1[ind]))
2493 return -EIO;
2497 return 0;
2500 static struct ibm_struct led_driver_data = {
2501 .name = "led",
2502 .read = led_read,
2503 .write = led_write,
2506 /*************************************************************************
2507 * Beep subdriver
2510 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2512 static int __init beep_init(struct ibm_init_struct *iibm)
2514 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2516 IBM_ACPIHANDLE_INIT(beep);
2518 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2519 str_supported(beep_handle != NULL));
2521 return (beep_handle)? 0 : 1;
2524 static int beep_read(char *p)
2526 int len = 0;
2528 if (!beep_handle)
2529 len += sprintf(p + len, "status:\t\tnot supported\n");
2530 else {
2531 len += sprintf(p + len, "status:\t\tsupported\n");
2532 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2535 return len;
2538 static int beep_write(char *buf)
2540 char *cmd;
2541 int beep_cmd;
2543 if (!beep_handle)
2544 return -ENODEV;
2546 while ((cmd = next_cmd(&buf))) {
2547 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2548 beep_cmd >= 0 && beep_cmd <= 17) {
2549 /* beep_cmd set */
2550 } else
2551 return -EINVAL;
2552 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2553 return -EIO;
2556 return 0;
2559 static struct ibm_struct beep_driver_data = {
2560 .name = "beep",
2561 .read = beep_read,
2562 .write = beep_write,
2565 /*************************************************************************
2566 * Thermal subdriver
2569 static enum thermal_access_mode thermal_read_mode;
2571 /* sysfs temp##_input -------------------------------------------------- */
2573 static ssize_t thermal_temp_input_show(struct device *dev,
2574 struct device_attribute *attr,
2575 char *buf)
2577 struct sensor_device_attribute *sensor_attr =
2578 to_sensor_dev_attr(attr);
2579 int idx = sensor_attr->index;
2580 s32 value;
2581 int res;
2583 res = thermal_get_sensor(idx, &value);
2584 if (res)
2585 return res;
2586 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2587 return -ENXIO;
2589 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2592 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2593 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2595 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2596 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2597 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2598 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2599 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2600 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2601 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2602 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2603 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2604 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2605 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2606 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2607 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2608 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2609 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2610 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2611 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2614 #define THERMAL_ATTRS(X) \
2615 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2617 static struct attribute *thermal_temp_input_attr[] = {
2618 THERMAL_ATTRS(8),
2619 THERMAL_ATTRS(9),
2620 THERMAL_ATTRS(10),
2621 THERMAL_ATTRS(11),
2622 THERMAL_ATTRS(12),
2623 THERMAL_ATTRS(13),
2624 THERMAL_ATTRS(14),
2625 THERMAL_ATTRS(15),
2626 THERMAL_ATTRS(0),
2627 THERMAL_ATTRS(1),
2628 THERMAL_ATTRS(2),
2629 THERMAL_ATTRS(3),
2630 THERMAL_ATTRS(4),
2631 THERMAL_ATTRS(5),
2632 THERMAL_ATTRS(6),
2633 THERMAL_ATTRS(7),
2634 NULL
2637 static const struct attribute_group thermal_temp_input16_group = {
2638 .attrs = thermal_temp_input_attr
2641 static const struct attribute_group thermal_temp_input8_group = {
2642 .attrs = &thermal_temp_input_attr[8]
2645 #undef THERMAL_SENSOR_ATTR_TEMP
2646 #undef THERMAL_ATTRS
2648 /* --------------------------------------------------------------------- */
2650 static int __init thermal_init(struct ibm_init_struct *iibm)
2652 u8 t, ta1, ta2;
2653 int i;
2654 int acpi_tmp7;
2655 int res;
2657 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2659 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2661 if (thinkpad_id.ec_model && experimental) {
2663 * Direct EC access mode: sensors at registers
2664 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2665 * non-implemented, thermal sensors return 0x80 when
2666 * not available
2669 ta1 = ta2 = 0;
2670 for (i = 0; i < 8; i++) {
2671 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2672 ta1 |= t;
2673 } else {
2674 ta1 = 0;
2675 break;
2677 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2678 ta2 |= t;
2679 } else {
2680 ta1 = 0;
2681 break;
2684 if (ta1 == 0) {
2685 /* This is sheer paranoia, but we handle it anyway */
2686 if (acpi_tmp7) {
2687 printk(IBM_ERR
2688 "ThinkPad ACPI EC access misbehaving, "
2689 "falling back to ACPI TMPx access mode\n");
2690 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2691 } else {
2692 printk(IBM_ERR
2693 "ThinkPad ACPI EC access misbehaving, "
2694 "disabling thermal sensors access\n");
2695 thermal_read_mode = TPACPI_THERMAL_NONE;
2697 } else {
2698 thermal_read_mode =
2699 (ta2 != 0) ?
2700 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2702 } else if (acpi_tmp7) {
2703 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2704 /* 600e/x, 770e, 770x */
2705 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2706 } else {
2707 /* Standard ACPI TMPx access, max 8 sensors */
2708 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2710 } else {
2711 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2712 thermal_read_mode = TPACPI_THERMAL_NONE;
2715 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2716 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2717 thermal_read_mode);
2719 switch(thermal_read_mode) {
2720 case TPACPI_THERMAL_TPEC_16:
2721 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2722 &thermal_temp_input16_group);
2723 if (res)
2724 return res;
2725 break;
2726 case TPACPI_THERMAL_TPEC_8:
2727 case TPACPI_THERMAL_ACPI_TMP07:
2728 case TPACPI_THERMAL_ACPI_UPDT:
2729 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2730 &thermal_temp_input8_group);
2731 if (res)
2732 return res;
2733 break;
2734 case TPACPI_THERMAL_NONE:
2735 default:
2736 return 1;
2739 return 0;
2742 static void thermal_exit(void)
2744 switch(thermal_read_mode) {
2745 case TPACPI_THERMAL_TPEC_16:
2746 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2747 &thermal_temp_input16_group);
2748 break;
2749 case TPACPI_THERMAL_TPEC_8:
2750 case TPACPI_THERMAL_ACPI_TMP07:
2751 case TPACPI_THERMAL_ACPI_UPDT:
2752 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2753 &thermal_temp_input16_group);
2754 break;
2755 case TPACPI_THERMAL_NONE:
2756 default:
2757 break;
2761 /* idx is zero-based */
2762 static int thermal_get_sensor(int idx, s32 *value)
2764 int t;
2765 s8 tmp;
2766 char tmpi[5];
2768 t = TP_EC_THERMAL_TMP0;
2770 switch (thermal_read_mode) {
2771 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2772 case TPACPI_THERMAL_TPEC_16:
2773 if (idx >= 8 && idx <= 15) {
2774 t = TP_EC_THERMAL_TMP8;
2775 idx -= 8;
2777 /* fallthrough */
2778 #endif
2779 case TPACPI_THERMAL_TPEC_8:
2780 if (idx <= 7) {
2781 if (!acpi_ec_read(t + idx, &tmp))
2782 return -EIO;
2783 *value = tmp * 1000;
2784 return 0;
2786 break;
2788 case TPACPI_THERMAL_ACPI_UPDT:
2789 if (idx <= 7) {
2790 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2791 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2792 return -EIO;
2793 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2794 return -EIO;
2795 *value = (t - 2732) * 100;
2796 return 0;
2798 break;
2800 case TPACPI_THERMAL_ACPI_TMP07:
2801 if (idx <= 7) {
2802 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2803 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2804 return -EIO;
2805 *value = t * 1000;
2806 return 0;
2808 break;
2810 case TPACPI_THERMAL_NONE:
2811 default:
2812 return -ENOSYS;
2815 return -EINVAL;
2818 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2820 int res, i;
2821 int n;
2823 n = 8;
2824 i = 0;
2826 if (!s)
2827 return -EINVAL;
2829 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2830 n = 16;
2832 for(i = 0 ; i < n; i++) {
2833 res = thermal_get_sensor(i, &s->temp[i]);
2834 if (res)
2835 return res;
2838 return n;
2841 static int thermal_read(char *p)
2843 int len = 0;
2844 int n, i;
2845 struct ibm_thermal_sensors_struct t;
2847 n = thermal_get_sensors(&t);
2848 if (unlikely(n < 0))
2849 return n;
2851 len += sprintf(p + len, "temperatures:\t");
2853 if (n > 0) {
2854 for (i = 0; i < (n - 1); i++)
2855 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2856 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2857 } else
2858 len += sprintf(p + len, "not supported\n");
2860 return len;
2863 static struct ibm_struct thermal_driver_data = {
2864 .name = "thermal",
2865 .read = thermal_read,
2866 .exit = thermal_exit,
2869 /*************************************************************************
2870 * EC Dump subdriver
2873 static u8 ecdump_regs[256];
2875 static int ecdump_read(char *p)
2877 int len = 0;
2878 int i, j;
2879 u8 v;
2881 len += sprintf(p + len, "EC "
2882 " +00 +01 +02 +03 +04 +05 +06 +07"
2883 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2884 for (i = 0; i < 256; i += 16) {
2885 len += sprintf(p + len, "EC 0x%02x:", i);
2886 for (j = 0; j < 16; j++) {
2887 if (!acpi_ec_read(i + j, &v))
2888 break;
2889 if (v != ecdump_regs[i + j])
2890 len += sprintf(p + len, " *%02x", v);
2891 else
2892 len += sprintf(p + len, " %02x", v);
2893 ecdump_regs[i + j] = v;
2895 len += sprintf(p + len, "\n");
2896 if (j != 16)
2897 break;
2900 /* These are way too dangerous to advertise openly... */
2901 #if 0
2902 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2903 " (<offset> is 00-ff, <value> is 00-ff)\n");
2904 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2905 " (<offset> is 00-ff, <value> is 0-255)\n");
2906 #endif
2907 return len;
2910 static int ecdump_write(char *buf)
2912 char *cmd;
2913 int i, v;
2915 while ((cmd = next_cmd(&buf))) {
2916 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2917 /* i and v set */
2918 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2919 /* i and v set */
2920 } else
2921 return -EINVAL;
2922 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2923 if (!acpi_ec_write(i, v))
2924 return -EIO;
2925 } else
2926 return -EINVAL;
2929 return 0;
2932 static struct ibm_struct ecdump_driver_data = {
2933 .name = "ecdump",
2934 .read = ecdump_read,
2935 .write = ecdump_write,
2936 .flags.experimental = 1,
2939 /*************************************************************************
2940 * Backlight/brightness subdriver
2943 static struct backlight_device *ibm_backlight_device;
2945 static struct backlight_ops ibm_backlight_data = {
2946 .get_brightness = brightness_get,
2947 .update_status = brightness_update_status,
2950 static int __init brightness_init(struct ibm_init_struct *iibm)
2952 int b;
2954 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2956 b = brightness_get(NULL);
2957 if (b < 0)
2958 return b;
2960 ibm_backlight_device = backlight_device_register(
2961 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
2962 &ibm_backlight_data);
2963 if (IS_ERR(ibm_backlight_device)) {
2964 printk(IBM_ERR "Could not register backlight device\n");
2965 return PTR_ERR(ibm_backlight_device);
2967 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
2969 ibm_backlight_device->props.max_brightness = 7;
2970 ibm_backlight_device->props.brightness = b;
2971 backlight_update_status(ibm_backlight_device);
2973 return 0;
2976 static void brightness_exit(void)
2978 if (ibm_backlight_device) {
2979 vdbg_printk(TPACPI_DBG_EXIT,
2980 "calling backlight_device_unregister()\n");
2981 backlight_device_unregister(ibm_backlight_device);
2982 ibm_backlight_device = NULL;
2986 static int brightness_update_status(struct backlight_device *bd)
2988 return brightness_set(
2989 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2990 bd->props.power == FB_BLANK_UNBLANK) ?
2991 bd->props.brightness : 0);
2994 static int brightness_get(struct backlight_device *bd)
2996 u8 level;
2997 if (!acpi_ec_read(brightness_offset, &level))
2998 return -EIO;
3000 level &= 0x7;
3002 return level;
3005 static int brightness_set(int value)
3007 int cmos_cmd, inc, i;
3008 int current_value = brightness_get(NULL);
3010 value &= 7;
3012 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
3013 inc = value > current_value ? 1 : -1;
3014 for (i = current_value; i != value; i += inc) {
3015 if (issue_thinkpad_cmos_command(cmos_cmd))
3016 return -EIO;
3017 if (!acpi_ec_write(brightness_offset, i + inc))
3018 return -EIO;
3021 return 0;
3024 static int brightness_read(char *p)
3026 int len = 0;
3027 int level;
3029 if ((level = brightness_get(NULL)) < 0) {
3030 len += sprintf(p + len, "level:\t\tunreadable\n");
3031 } else {
3032 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3033 len += sprintf(p + len, "commands:\tup, down\n");
3034 len += sprintf(p + len, "commands:\tlevel <level>"
3035 " (<level> is 0-7)\n");
3038 return len;
3041 static int brightness_write(char *buf)
3043 int level;
3044 int new_level;
3045 char *cmd;
3047 while ((cmd = next_cmd(&buf))) {
3048 if ((level = brightness_get(NULL)) < 0)
3049 return level;
3050 level &= 7;
3052 if (strlencmp(cmd, "up") == 0) {
3053 new_level = level == 7 ? 7 : level + 1;
3054 } else if (strlencmp(cmd, "down") == 0) {
3055 new_level = level == 0 ? 0 : level - 1;
3056 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3057 new_level >= 0 && new_level <= 7) {
3058 /* new_level set */
3059 } else
3060 return -EINVAL;
3062 brightness_set(new_level);
3065 return 0;
3068 static struct ibm_struct brightness_driver_data = {
3069 .name = "brightness",
3070 .read = brightness_read,
3071 .write = brightness_write,
3072 .exit = brightness_exit,
3075 /*************************************************************************
3076 * Volume subdriver
3079 static int volume_read(char *p)
3081 int len = 0;
3082 u8 level;
3084 if (!acpi_ec_read(volume_offset, &level)) {
3085 len += sprintf(p + len, "level:\t\tunreadable\n");
3086 } else {
3087 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3088 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3089 len += sprintf(p + len, "commands:\tup, down, mute\n");
3090 len += sprintf(p + len, "commands:\tlevel <level>"
3091 " (<level> is 0-15)\n");
3094 return len;
3097 static int volume_write(char *buf)
3099 int cmos_cmd, inc, i;
3100 u8 level, mute;
3101 int new_level, new_mute;
3102 char *cmd;
3104 while ((cmd = next_cmd(&buf))) {
3105 if (!acpi_ec_read(volume_offset, &level))
3106 return -EIO;
3107 new_mute = mute = level & 0x40;
3108 new_level = level = level & 0xf;
3110 if (strlencmp(cmd, "up") == 0) {
3111 if (mute)
3112 new_mute = 0;
3113 else
3114 new_level = level == 15 ? 15 : level + 1;
3115 } else if (strlencmp(cmd, "down") == 0) {
3116 if (mute)
3117 new_mute = 0;
3118 else
3119 new_level = level == 0 ? 0 : level - 1;
3120 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3121 new_level >= 0 && new_level <= 15) {
3122 /* new_level set */
3123 } else if (strlencmp(cmd, "mute") == 0) {
3124 new_mute = 0x40;
3125 } else
3126 return -EINVAL;
3128 if (new_level != level) { /* mute doesn't change */
3129 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3130 inc = new_level > level ? 1 : -1;
3132 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3133 !acpi_ec_write(volume_offset, level)))
3134 return -EIO;
3136 for (i = level; i != new_level; i += inc)
3137 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3138 !acpi_ec_write(volume_offset, i + inc))
3139 return -EIO;
3141 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3142 !acpi_ec_write(volume_offset,
3143 new_level + mute)))
3144 return -EIO;
3147 if (new_mute != mute) { /* level doesn't change */
3148 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3150 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3151 !acpi_ec_write(volume_offset, level + new_mute))
3152 return -EIO;
3156 return 0;
3159 static struct ibm_struct volume_driver_data = {
3160 .name = "volume",
3161 .read = volume_read,
3162 .write = volume_write,
3165 /*************************************************************************
3166 * Fan subdriver
3170 * FAN ACCESS MODES
3172 * TPACPI_FAN_RD_ACPI_GFAN:
3173 * ACPI GFAN method: returns fan level
3175 * see TPACPI_FAN_WR_ACPI_SFAN
3176 * EC 0x2f (HFSP) not available if GFAN exists
3178 * TPACPI_FAN_WR_ACPI_SFAN:
3179 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3181 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3182 * it for writing.
3184 * TPACPI_FAN_WR_TPEC:
3185 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3186 * Supported on almost all ThinkPads
3188 * Fan speed changes of any sort (including those caused by the
3189 * disengaged mode) are usually done slowly by the firmware as the
3190 * maximum ammount of fan duty cycle change per second seems to be
3191 * limited.
3193 * Reading is not available if GFAN exists.
3194 * Writing is not available if SFAN exists.
3196 * Bits
3197 * 7 automatic mode engaged;
3198 * (default operation mode of the ThinkPad)
3199 * fan level is ignored in this mode.
3200 * 6 full speed mode (takes precedence over bit 7);
3201 * not available on all thinkpads. May disable
3202 * the tachometer while the fan controller ramps up
3203 * the speed (which can take up to a few *minutes*).
3204 * Speeds up fan to 100% duty-cycle, which is far above
3205 * the standard RPM levels. It is not impossible that
3206 * it could cause hardware damage.
3207 * 5-3 unused in some models. Extra bits for fan level
3208 * in others, but still useless as all values above
3209 * 7 map to the same speed as level 7 in these models.
3210 * 2-0 fan level (0..7 usually)
3211 * 0x00 = stop
3212 * 0x07 = max (set when temperatures critical)
3213 * Some ThinkPads may have other levels, see
3214 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3216 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3217 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3218 * does so, its initial value is meaningless (0x07).
3220 * For firmware bugs, refer to:
3221 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3223 * ----
3225 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3226 * Main fan tachometer reading (in RPM)
3228 * This register is present on all ThinkPads with a new-style EC, and
3229 * it is known not to be present on the A21m/e, and T22, as there is
3230 * something else in offset 0x84 according to the ACPI DSDT. Other
3231 * ThinkPads from this same time period (and earlier) probably lack the
3232 * tachometer as well.
3234 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3235 * was never fixed by IBM to report the EC firmware version string
3236 * probably support the tachometer (like the early X models), so
3237 * detecting it is quite hard. We need more data to know for sure.
3239 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3240 * might result.
3242 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3243 * mode.
3245 * For firmware bugs, refer to:
3246 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3248 * TPACPI_FAN_WR_ACPI_FANS:
3249 * ThinkPad X31, X40, X41. Not available in the X60.
3251 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3252 * high speed. ACPI DSDT seems to map these three speeds to levels
3253 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3254 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3256 * The speeds are stored on handles
3257 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3259 * There are three default speed sets, acessible as handles:
3260 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3262 * ACPI DSDT switches which set is in use depending on various
3263 * factors.
3265 * TPACPI_FAN_WR_TPEC is also available and should be used to
3266 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3267 * but the ACPI tables just mention level 7.
3270 static enum fan_status_access_mode fan_status_access_mode;
3271 static enum fan_control_access_mode fan_control_access_mode;
3272 static enum fan_control_commands fan_control_commands;
3274 static u8 fan_control_initial_status;
3275 static u8 fan_control_desired_level;
3277 static void fan_watchdog_fire(struct work_struct *ignored);
3278 static int fan_watchdog_maxinterval;
3279 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3281 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3282 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3283 "\\FSPD", /* 600e/x, 770e, 770x */
3284 ); /* all others */
3285 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3286 "JFNS", /* 770x-JL */
3287 ); /* all others */
3290 * SYSFS fan layout: hwmon compatible (device)
3292 * pwm*_enable:
3293 * 0: "disengaged" mode
3294 * 1: manual mode
3295 * 2: native EC "auto" mode (recommended, hardware default)
3297 * pwm*: set speed in manual mode, ignored otherwise.
3298 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3299 * interpolation.
3301 * fan*_input: tachometer reading, RPM
3304 * SYSFS fan layout: extensions
3306 * fan_watchdog (driver):
3307 * fan watchdog interval in seconds, 0 disables (default), max 120
3310 /* sysfs fan pwm1_enable ----------------------------------------------- */
3311 static ssize_t fan_pwm1_enable_show(struct device *dev,
3312 struct device_attribute *attr,
3313 char *buf)
3315 int res, mode;
3316 u8 status;
3318 res = fan_get_status_safe(&status);
3319 if (res)
3320 return res;
3322 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3323 if (status != fan_control_initial_status) {
3324 tp_features.fan_ctrl_status_undef = 0;
3325 } else {
3326 /* Return most likely status. In fact, it
3327 * might be the only possible status */
3328 status = TP_EC_FAN_AUTO;
3332 if (status & TP_EC_FAN_FULLSPEED) {
3333 mode = 0;
3334 } else if (status & TP_EC_FAN_AUTO) {
3335 mode = 2;
3336 } else
3337 mode = 1;
3339 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3342 static ssize_t fan_pwm1_enable_store(struct device *dev,
3343 struct device_attribute *attr,
3344 const char *buf, size_t count)
3346 unsigned long t;
3347 int res, level;
3349 if (parse_strtoul(buf, 2, &t))
3350 return -EINVAL;
3352 switch (t) {
3353 case 0:
3354 level = TP_EC_FAN_FULLSPEED;
3355 break;
3356 case 1:
3357 level = TPACPI_FAN_LAST_LEVEL;
3358 break;
3359 case 2:
3360 level = TP_EC_FAN_AUTO;
3361 break;
3362 case 3:
3363 /* reserved for software-controlled auto mode */
3364 return -ENOSYS;
3365 default:
3366 return -EINVAL;
3369 res = fan_set_level_safe(level);
3370 if (res == -ENXIO)
3371 return -EINVAL;
3372 else if (res < 0)
3373 return res;
3375 fan_watchdog_reset();
3377 return count;
3380 static struct device_attribute dev_attr_fan_pwm1_enable =
3381 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3382 fan_pwm1_enable_show, fan_pwm1_enable_store);
3384 /* sysfs fan pwm1 ------------------------------------------------------ */
3385 static ssize_t fan_pwm1_show(struct device *dev,
3386 struct device_attribute *attr,
3387 char *buf)
3389 int res;
3390 u8 status;
3392 res = fan_get_status_safe(&status);
3393 if (res)
3394 return res;
3396 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3397 if (status != fan_control_initial_status) {
3398 tp_features.fan_ctrl_status_undef = 0;
3399 } else {
3400 status = TP_EC_FAN_AUTO;
3404 if ((status &
3405 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3406 status = fan_control_desired_level;
3408 if (status > 7)
3409 status = 7;
3411 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3414 static ssize_t fan_pwm1_store(struct device *dev,
3415 struct device_attribute *attr,
3416 const char *buf, size_t count)
3418 unsigned long s;
3419 int rc;
3420 u8 status, newlevel;
3422 if (parse_strtoul(buf, 255, &s))
3423 return -EINVAL;
3425 /* scale down from 0-255 to 0-7 */
3426 newlevel = (s >> 5) & 0x07;
3428 rc = mutex_lock_interruptible(&fan_mutex);
3429 if (rc < 0)
3430 return rc;
3432 rc = fan_get_status(&status);
3433 if (!rc && (status &
3434 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3435 rc = fan_set_level(newlevel);
3436 if (rc == -ENXIO)
3437 rc = -EINVAL;
3438 else if (!rc) {
3439 fan_update_desired_level(newlevel);
3440 fan_watchdog_reset();
3444 mutex_unlock(&fan_mutex);
3445 return (rc)? rc : count;
3448 static struct device_attribute dev_attr_fan_pwm1 =
3449 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3450 fan_pwm1_show, fan_pwm1_store);
3452 /* sysfs fan fan1_input ------------------------------------------------ */
3453 static ssize_t fan_fan1_input_show(struct device *dev,
3454 struct device_attribute *attr,
3455 char *buf)
3457 int res;
3458 unsigned int speed;
3460 res = fan_get_speed(&speed);
3461 if (res < 0)
3462 return res;
3464 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3467 static struct device_attribute dev_attr_fan_fan1_input =
3468 __ATTR(fan1_input, S_IRUGO,
3469 fan_fan1_input_show, NULL);
3471 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3472 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3473 char *buf)
3475 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3478 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3479 const char *buf, size_t count)
3481 unsigned long t;
3483 if (parse_strtoul(buf, 120, &t))
3484 return -EINVAL;
3486 if (!fan_control_allowed)
3487 return -EPERM;
3489 fan_watchdog_maxinterval = t;
3490 fan_watchdog_reset();
3492 return count;
3495 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3496 fan_fan_watchdog_show, fan_fan_watchdog_store);
3498 /* --------------------------------------------------------------------- */
3499 static struct attribute *fan_attributes[] = {
3500 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3501 &dev_attr_fan_fan1_input.attr,
3502 NULL
3505 static const struct attribute_group fan_attr_group = {
3506 .attrs = fan_attributes,
3509 static int __init fan_init(struct ibm_init_struct *iibm)
3511 int rc;
3513 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3515 mutex_init(&fan_mutex);
3516 fan_status_access_mode = TPACPI_FAN_NONE;
3517 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3518 fan_control_commands = 0;
3519 fan_watchdog_maxinterval = 0;
3520 tp_features.fan_ctrl_status_undef = 0;
3521 fan_control_desired_level = 7;
3523 IBM_ACPIHANDLE_INIT(fans);
3524 IBM_ACPIHANDLE_INIT(gfan);
3525 IBM_ACPIHANDLE_INIT(sfan);
3527 if (gfan_handle) {
3528 /* 570, 600e/x, 770e, 770x */
3529 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3530 } else {
3531 /* all other ThinkPads: note that even old-style
3532 * ThinkPad ECs supports the fan control register */
3533 if (likely(acpi_ec_read(fan_status_offset,
3534 &fan_control_initial_status))) {
3535 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3537 /* In some ThinkPads, neither the EC nor the ACPI
3538 * DSDT initialize the fan status, and it ends up
3539 * being set to 0x07 when it *could* be either
3540 * 0x07 or 0x80.
3542 * Enable for TP-1Y (T43), TP-78 (R51e),
3543 * TP-76 (R52), TP-70 (T43, R52), which are known
3544 * to be buggy. */
3545 if (fan_control_initial_status == 0x07) {
3546 switch (thinkpad_id.ec_model) {
3547 case 0x5931: /* TP-1Y */
3548 case 0x3837: /* TP-78 */
3549 case 0x3637: /* TP-76 */
3550 case 0x3037: /* TP-70 */
3551 printk(IBM_NOTICE
3552 "fan_init: initial fan status is "
3553 "unknown, assuming it is in auto "
3554 "mode\n");
3555 tp_features.fan_ctrl_status_undef = 1;
3559 } else {
3560 printk(IBM_ERR
3561 "ThinkPad ACPI EC access misbehaving, "
3562 "fan status and control unavailable\n");
3563 return 1;
3567 if (sfan_handle) {
3568 /* 570, 770x-JL */
3569 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3570 fan_control_commands |=
3571 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3572 } else {
3573 if (!gfan_handle) {
3574 /* gfan without sfan means no fan control */
3575 /* all other models implement TP EC 0x2f control */
3577 if (fans_handle) {
3578 /* X31, X40, X41 */
3579 fan_control_access_mode =
3580 TPACPI_FAN_WR_ACPI_FANS;
3581 fan_control_commands |=
3582 TPACPI_FAN_CMD_SPEED |
3583 TPACPI_FAN_CMD_LEVEL |
3584 TPACPI_FAN_CMD_ENABLE;
3585 } else {
3586 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3587 fan_control_commands |=
3588 TPACPI_FAN_CMD_LEVEL |
3589 TPACPI_FAN_CMD_ENABLE;
3594 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3595 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3596 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3597 fan_status_access_mode, fan_control_access_mode);
3599 /* fan control master switch */
3600 if (!fan_control_allowed) {
3601 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3602 fan_control_commands = 0;
3603 dbg_printk(TPACPI_DBG_INIT,
3604 "fan control features disabled by parameter\n");
3607 /* update fan_control_desired_level */
3608 if (fan_status_access_mode != TPACPI_FAN_NONE)
3609 fan_get_status_safe(NULL);
3611 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3612 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3613 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3614 &fan_attr_group);
3615 if (!(rc < 0))
3616 rc = driver_create_file(&tpacpi_pdriver.driver,
3617 &driver_attr_fan_watchdog);
3618 if (rc < 0)
3619 return rc;
3620 return 0;
3621 } else
3622 return 1;
3626 * Call with fan_mutex held
3628 static void fan_update_desired_level(u8 status)
3630 if ((status &
3631 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3632 if (status > 7)
3633 fan_control_desired_level = 7;
3634 else
3635 fan_control_desired_level = status;
3639 static int fan_get_status(u8 *status)
3641 u8 s;
3643 /* TODO:
3644 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3646 switch (fan_status_access_mode) {
3647 case TPACPI_FAN_RD_ACPI_GFAN:
3648 /* 570, 600e/x, 770e, 770x */
3650 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3651 return -EIO;
3653 if (likely(status))
3654 *status = s & 0x07;
3656 break;
3658 case TPACPI_FAN_RD_TPEC:
3659 /* all except 570, 600e/x, 770e, 770x */
3660 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3661 return -EIO;
3663 if (likely(status))
3664 *status = s;
3666 break;
3668 default:
3669 return -ENXIO;
3672 return 0;
3675 static int fan_get_status_safe(u8 *status)
3677 int rc;
3678 u8 s;
3680 rc = mutex_lock_interruptible(&fan_mutex);
3681 if (rc < 0)
3682 return rc;
3683 rc = fan_get_status(&s);
3684 if (!rc)
3685 fan_update_desired_level(s);
3686 mutex_unlock(&fan_mutex);
3688 if (status)
3689 *status = s;
3691 return rc;
3694 static void fan_exit(void)
3696 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3698 /* FIXME: can we really do this unconditionally? */
3699 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3700 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3702 cancel_delayed_work(&fan_watchdog_task);
3703 flush_scheduled_work();
3706 static int fan_get_speed(unsigned int *speed)
3708 u8 hi, lo;
3710 switch (fan_status_access_mode) {
3711 case TPACPI_FAN_RD_TPEC:
3712 /* all except 570, 600e/x, 770e, 770x */
3713 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3714 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3715 return -EIO;
3717 if (likely(speed))
3718 *speed = (hi << 8) | lo;
3720 break;
3722 default:
3723 return -ENXIO;
3726 return 0;
3729 static void fan_watchdog_fire(struct work_struct *ignored)
3731 int rc;
3733 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3734 rc = fan_set_enable();
3735 if (rc < 0) {
3736 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3737 "will try again later...\n", -rc);
3738 /* reschedule for later */
3739 fan_watchdog_reset();
3743 static void fan_watchdog_reset(void)
3745 static int fan_watchdog_active;
3747 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3748 return;
3750 if (fan_watchdog_active)
3751 cancel_delayed_work(&fan_watchdog_task);
3753 if (fan_watchdog_maxinterval > 0) {
3754 fan_watchdog_active = 1;
3755 if (!schedule_delayed_work(&fan_watchdog_task,
3756 msecs_to_jiffies(fan_watchdog_maxinterval
3757 * 1000))) {
3758 printk(IBM_ERR "failed to schedule the fan watchdog, "
3759 "watchdog will not trigger\n");
3761 } else
3762 fan_watchdog_active = 0;
3765 static int fan_set_level(int level)
3767 if (!fan_control_allowed)
3768 return -EPERM;
3770 switch (fan_control_access_mode) {
3771 case TPACPI_FAN_WR_ACPI_SFAN:
3772 if (level >= 0 && level <= 7) {
3773 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3774 return -EIO;
3775 } else
3776 return -EINVAL;
3777 break;
3779 case TPACPI_FAN_WR_ACPI_FANS:
3780 case TPACPI_FAN_WR_TPEC:
3781 if ((level != TP_EC_FAN_AUTO) &&
3782 (level != TP_EC_FAN_FULLSPEED) &&
3783 ((level < 0) || (level > 7)))
3784 return -EINVAL;
3786 /* safety net should the EC not support AUTO
3787 * or FULLSPEED mode bits and just ignore them */
3788 if (level & TP_EC_FAN_FULLSPEED)
3789 level |= 7; /* safety min speed 7 */
3790 else if (level & TP_EC_FAN_FULLSPEED)
3791 level |= 4; /* safety min speed 4 */
3793 if (!acpi_ec_write(fan_status_offset, level))
3794 return -EIO;
3795 else
3796 tp_features.fan_ctrl_status_undef = 0;
3797 break;
3799 default:
3800 return -ENXIO;
3802 return 0;
3805 static int fan_set_level_safe(int level)
3807 int rc;
3809 if (!fan_control_allowed)
3810 return -EPERM;
3812 rc = mutex_lock_interruptible(&fan_mutex);
3813 if (rc < 0)
3814 return rc;
3816 if (level == TPACPI_FAN_LAST_LEVEL)
3817 level = fan_control_desired_level;
3819 rc = fan_set_level(level);
3820 if (!rc)
3821 fan_update_desired_level(level);
3823 mutex_unlock(&fan_mutex);
3824 return rc;
3827 static int fan_set_enable(void)
3829 u8 s;
3830 int rc;
3832 if (!fan_control_allowed)
3833 return -EPERM;
3835 rc = mutex_lock_interruptible(&fan_mutex);
3836 if (rc < 0)
3837 return rc;
3839 switch (fan_control_access_mode) {
3840 case TPACPI_FAN_WR_ACPI_FANS:
3841 case TPACPI_FAN_WR_TPEC:
3842 rc = fan_get_status(&s);
3843 if (rc < 0)
3844 break;
3846 /* Don't go out of emergency fan mode */
3847 if (s != 7) {
3848 s &= 0x07;
3849 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3852 if (!acpi_ec_write(fan_status_offset, s))
3853 rc = -EIO;
3854 else {
3855 tp_features.fan_ctrl_status_undef = 0;
3856 rc = 0;
3858 break;
3860 case TPACPI_FAN_WR_ACPI_SFAN:
3861 rc = fan_get_status(&s);
3862 if (rc < 0)
3863 break;
3865 s &= 0x07;
3867 /* Set fan to at least level 4 */
3868 s |= 4;
3870 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
3871 rc= -EIO;
3872 else
3873 rc = 0;
3874 break;
3876 default:
3877 rc = -ENXIO;
3880 mutex_unlock(&fan_mutex);
3881 return rc;
3884 static int fan_set_disable(void)
3886 int rc;
3888 if (!fan_control_allowed)
3889 return -EPERM;
3891 rc = mutex_lock_interruptible(&fan_mutex);
3892 if (rc < 0)
3893 return rc;
3895 rc = 0;
3896 switch (fan_control_access_mode) {
3897 case TPACPI_FAN_WR_ACPI_FANS:
3898 case TPACPI_FAN_WR_TPEC:
3899 if (!acpi_ec_write(fan_status_offset, 0x00))
3900 rc = -EIO;
3901 else {
3902 fan_control_desired_level = 0;
3903 tp_features.fan_ctrl_status_undef = 0;
3905 break;
3907 case TPACPI_FAN_WR_ACPI_SFAN:
3908 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
3909 rc = -EIO;
3910 else
3911 fan_control_desired_level = 0;
3912 break;
3914 default:
3915 rc = -ENXIO;
3919 mutex_unlock(&fan_mutex);
3920 return rc;
3923 static int fan_set_speed(int speed)
3925 int rc;
3927 if (!fan_control_allowed)
3928 return -EPERM;
3930 rc = mutex_lock_interruptible(&fan_mutex);
3931 if (rc < 0)
3932 return rc;
3934 rc = 0;
3935 switch (fan_control_access_mode) {
3936 case TPACPI_FAN_WR_ACPI_FANS:
3937 if (speed >= 0 && speed <= 65535) {
3938 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3939 speed, speed, speed))
3940 rc = -EIO;
3941 } else
3942 rc = -EINVAL;
3943 break;
3945 default:
3946 rc = -ENXIO;
3949 mutex_unlock(&fan_mutex);
3950 return rc;
3953 static int fan_read(char *p)
3955 int len = 0;
3956 int rc;
3957 u8 status;
3958 unsigned int speed = 0;
3960 switch (fan_status_access_mode) {
3961 case TPACPI_FAN_RD_ACPI_GFAN:
3962 /* 570, 600e/x, 770e, 770x */
3963 if ((rc = fan_get_status_safe(&status)) < 0)
3964 return rc;
3966 len += sprintf(p + len, "status:\t\t%s\n"
3967 "level:\t\t%d\n",
3968 (status != 0) ? "enabled" : "disabled", status);
3969 break;
3971 case TPACPI_FAN_RD_TPEC:
3972 /* all except 570, 600e/x, 770e, 770x */
3973 if ((rc = fan_get_status_safe(&status)) < 0)
3974 return rc;
3976 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3977 if (status != fan_control_initial_status)
3978 tp_features.fan_ctrl_status_undef = 0;
3979 else
3980 /* Return most likely status. In fact, it
3981 * might be the only possible status */
3982 status = TP_EC_FAN_AUTO;
3985 len += sprintf(p + len, "status:\t\t%s\n",
3986 (status != 0) ? "enabled" : "disabled");
3988 if ((rc = fan_get_speed(&speed)) < 0)
3989 return rc;
3991 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3993 if (status & TP_EC_FAN_FULLSPEED)
3994 /* Disengaged mode takes precedence */
3995 len += sprintf(p + len, "level:\t\tdisengaged\n");
3996 else if (status & TP_EC_FAN_AUTO)
3997 len += sprintf(p + len, "level:\t\tauto\n");
3998 else
3999 len += sprintf(p + len, "level:\t\t%d\n", status);
4000 break;
4002 case TPACPI_FAN_NONE:
4003 default:
4004 len += sprintf(p + len, "status:\t\tnot supported\n");
4007 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
4008 len += sprintf(p + len, "commands:\tlevel <level>");
4010 switch (fan_control_access_mode) {
4011 case TPACPI_FAN_WR_ACPI_SFAN:
4012 len += sprintf(p + len, " (<level> is 0-7)\n");
4013 break;
4015 default:
4016 len += sprintf(p + len, " (<level> is 0-7, "
4017 "auto, disengaged, full-speed)\n");
4018 break;
4022 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
4023 len += sprintf(p + len, "commands:\tenable, disable\n"
4024 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4025 "1-120 (seconds))\n");
4027 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
4028 len += sprintf(p + len, "commands:\tspeed <speed>"
4029 " (<speed> is 0-65535)\n");
4031 return len;
4034 static int fan_write_cmd_level(const char *cmd, int *rc)
4036 int level;
4038 if (strlencmp(cmd, "level auto") == 0)
4039 level = TP_EC_FAN_AUTO;
4040 else if ((strlencmp(cmd, "level disengaged") == 0) |
4041 (strlencmp(cmd, "level full-speed") == 0))
4042 level = TP_EC_FAN_FULLSPEED;
4043 else if (sscanf(cmd, "level %d", &level) != 1)
4044 return 0;
4046 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4047 printk(IBM_ERR "level command accepted for unsupported "
4048 "access mode %d", fan_control_access_mode);
4050 return 1;
4053 static int fan_write_cmd_enable(const char *cmd, int *rc)
4055 if (strlencmp(cmd, "enable") != 0)
4056 return 0;
4058 if ((*rc = fan_set_enable()) == -ENXIO)
4059 printk(IBM_ERR "enable command accepted for unsupported "
4060 "access mode %d", fan_control_access_mode);
4062 return 1;
4065 static int fan_write_cmd_disable(const char *cmd, int *rc)
4067 if (strlencmp(cmd, "disable") != 0)
4068 return 0;
4070 if ((*rc = fan_set_disable()) == -ENXIO)
4071 printk(IBM_ERR "disable command accepted for unsupported "
4072 "access mode %d", fan_control_access_mode);
4074 return 1;
4077 static int fan_write_cmd_speed(const char *cmd, int *rc)
4079 int speed;
4081 /* TODO:
4082 * Support speed <low> <medium> <high> ? */
4084 if (sscanf(cmd, "speed %d", &speed) != 1)
4085 return 0;
4087 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4088 printk(IBM_ERR "speed command accepted for unsupported "
4089 "access mode %d", fan_control_access_mode);
4091 return 1;
4094 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4096 int interval;
4098 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4099 return 0;
4101 if (interval < 0 || interval > 120)
4102 *rc = -EINVAL;
4103 else
4104 fan_watchdog_maxinterval = interval;
4106 return 1;
4109 static int fan_write(char *buf)
4111 char *cmd;
4112 int rc = 0;
4114 while (!rc && (cmd = next_cmd(&buf))) {
4115 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4116 fan_write_cmd_level(cmd, &rc)) &&
4117 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4118 (fan_write_cmd_enable(cmd, &rc) ||
4119 fan_write_cmd_disable(cmd, &rc) ||
4120 fan_write_cmd_watchdog(cmd, &rc))) &&
4121 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4122 fan_write_cmd_speed(cmd, &rc))
4124 rc = -EINVAL;
4125 else if (!rc)
4126 fan_watchdog_reset();
4129 return rc;
4132 static struct ibm_struct fan_driver_data = {
4133 .name = "fan",
4134 .read = fan_read,
4135 .write = fan_write,
4136 .exit = fan_exit,
4139 /****************************************************************************
4140 ****************************************************************************
4142 * Infrastructure
4144 ****************************************************************************
4145 ****************************************************************************/
4147 /* /proc support */
4148 static struct proc_dir_entry *proc_dir;
4150 /* Subdriver registry */
4151 static LIST_HEAD(tpacpi_all_drivers);
4155 * Module and infrastructure proble, init and exit handling
4158 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4159 static const char * __init str_supported(int is_supported)
4161 static char text_unsupported[] __initdata = "not supported";
4163 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4165 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4167 static int __init ibm_init(struct ibm_init_struct *iibm)
4169 int ret;
4170 struct ibm_struct *ibm = iibm->data;
4171 struct proc_dir_entry *entry;
4173 BUG_ON(ibm == NULL);
4175 INIT_LIST_HEAD(&ibm->all_drivers);
4177 if (ibm->flags.experimental && !experimental)
4178 return 0;
4180 dbg_printk(TPACPI_DBG_INIT,
4181 "probing for %s\n", ibm->name);
4183 if (iibm->init) {
4184 ret = iibm->init(iibm);
4185 if (ret > 0)
4186 return 0; /* probe failed */
4187 if (ret)
4188 return ret;
4190 ibm->flags.init_called = 1;
4193 if (ibm->acpi) {
4194 if (ibm->acpi->hid) {
4195 ret = register_tpacpi_subdriver(ibm);
4196 if (ret)
4197 goto err_out;
4200 if (ibm->acpi->notify) {
4201 ret = setup_acpi_notify(ibm);
4202 if (ret == -ENODEV) {
4203 printk(IBM_NOTICE "disabling subdriver %s\n",
4204 ibm->name);
4205 ret = 0;
4206 goto err_out;
4208 if (ret < 0)
4209 goto err_out;
4213 dbg_printk(TPACPI_DBG_INIT,
4214 "%s installed\n", ibm->name);
4216 if (ibm->read) {
4217 entry = create_proc_entry(ibm->name,
4218 S_IFREG | S_IRUGO | S_IWUSR,
4219 proc_dir);
4220 if (!entry) {
4221 printk(IBM_ERR "unable to create proc entry %s\n",
4222 ibm->name);
4223 ret = -ENODEV;
4224 goto err_out;
4226 entry->owner = THIS_MODULE;
4227 entry->data = ibm;
4228 entry->read_proc = &dispatch_procfs_read;
4229 if (ibm->write)
4230 entry->write_proc = &dispatch_procfs_write;
4231 ibm->flags.proc_created = 1;
4234 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4236 return 0;
4238 err_out:
4239 dbg_printk(TPACPI_DBG_INIT,
4240 "%s: at error exit path with result %d\n",
4241 ibm->name, ret);
4243 ibm_exit(ibm);
4244 return (ret < 0)? ret : 0;
4247 static void ibm_exit(struct ibm_struct *ibm)
4249 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4251 list_del_init(&ibm->all_drivers);
4253 if (ibm->flags.acpi_notify_installed) {
4254 dbg_printk(TPACPI_DBG_EXIT,
4255 "%s: acpi_remove_notify_handler\n", ibm->name);
4256 BUG_ON(!ibm->acpi);
4257 acpi_remove_notify_handler(*ibm->acpi->handle,
4258 ibm->acpi->type,
4259 dispatch_acpi_notify);
4260 ibm->flags.acpi_notify_installed = 0;
4261 ibm->flags.acpi_notify_installed = 0;
4264 if (ibm->flags.proc_created) {
4265 dbg_printk(TPACPI_DBG_EXIT,
4266 "%s: remove_proc_entry\n", ibm->name);
4267 remove_proc_entry(ibm->name, proc_dir);
4268 ibm->flags.proc_created = 0;
4271 if (ibm->flags.acpi_driver_registered) {
4272 dbg_printk(TPACPI_DBG_EXIT,
4273 "%s: acpi_bus_unregister_driver\n", ibm->name);
4274 BUG_ON(!ibm->acpi);
4275 acpi_bus_unregister_driver(ibm->acpi->driver);
4276 kfree(ibm->acpi->driver);
4277 ibm->acpi->driver = NULL;
4278 ibm->flags.acpi_driver_registered = 0;
4281 if (ibm->flags.init_called && ibm->exit) {
4282 ibm->exit();
4283 ibm->flags.init_called = 0;
4286 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4289 /* Probing */
4291 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
4293 struct dmi_device *dev = NULL;
4294 char ec_fw_string[18];
4296 if (!tp)
4297 return;
4299 memset(tp, 0, sizeof(*tp));
4301 if (dmi_name_in_vendors("IBM"))
4302 tp->vendor = PCI_VENDOR_ID_IBM;
4303 else if (dmi_name_in_vendors("LENOVO"))
4304 tp->vendor = PCI_VENDOR_ID_LENOVO;
4305 else
4306 return;
4308 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4309 GFP_KERNEL);
4310 if (!tp->bios_version_str)
4311 return;
4312 tp->bios_model = tp->bios_version_str[0]
4313 | (tp->bios_version_str[1] << 8);
4316 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4317 * X32 or newer, all Z series; Some models must have an
4318 * up-to-date BIOS or they will not be detected.
4320 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4322 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4323 if (sscanf(dev->name,
4324 "IBM ThinkPad Embedded Controller -[%17c",
4325 ec_fw_string) == 1) {
4326 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4327 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4329 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4330 tp->ec_model = ec_fw_string[0]
4331 | (ec_fw_string[1] << 8);
4332 break;
4336 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4337 GFP_KERNEL);
4338 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4339 kfree(tp->model_str);
4340 tp->model_str = NULL;
4344 static int __init probe_for_thinkpad(void)
4346 int is_thinkpad;
4348 if (acpi_disabled)
4349 return -ENODEV;
4352 * Non-ancient models have better DMI tagging, but very old models
4353 * don't.
4355 is_thinkpad = (thinkpad_id.model_str != NULL);
4357 /* ec is required because many other handles are relative to it */
4358 IBM_ACPIHANDLE_INIT(ec);
4359 if (!ec_handle) {
4360 if (is_thinkpad)
4361 printk(IBM_ERR
4362 "Not yet supported ThinkPad detected!\n");
4363 return -ENODEV;
4367 * Risks a regression on very old machines, but reduces potential
4368 * false positives a damn great deal
4370 if (!is_thinkpad)
4371 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
4373 if (!is_thinkpad && !force_load)
4374 return -ENODEV;
4376 return 0;
4380 /* Module init, exit, parameters */
4382 static struct ibm_init_struct ibms_init[] __initdata = {
4384 .init = thinkpad_acpi_driver_init,
4385 .data = &thinkpad_acpi_driver_data,
4388 .init = hotkey_init,
4389 .data = &hotkey_driver_data,
4392 .init = bluetooth_init,
4393 .data = &bluetooth_driver_data,
4396 .init = wan_init,
4397 .data = &wan_driver_data,
4400 .init = video_init,
4401 .data = &video_driver_data,
4404 .init = light_init,
4405 .data = &light_driver_data,
4407 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4409 .init = dock_init,
4410 .data = &dock_driver_data[0],
4413 .init = dock_init2,
4414 .data = &dock_driver_data[1],
4416 #endif
4417 #ifdef CONFIG_THINKPAD_ACPI_BAY
4419 .init = bay_init,
4420 .data = &bay_driver_data,
4422 #endif
4424 .init = cmos_init,
4425 .data = &cmos_driver_data,
4428 .init = led_init,
4429 .data = &led_driver_data,
4432 .init = beep_init,
4433 .data = &beep_driver_data,
4436 .init = thermal_init,
4437 .data = &thermal_driver_data,
4440 .data = &ecdump_driver_data,
4443 .init = brightness_init,
4444 .data = &brightness_driver_data,
4447 .data = &volume_driver_data,
4450 .init = fan_init,
4451 .data = &fan_driver_data,
4455 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4457 unsigned int i;
4458 struct ibm_struct *ibm;
4460 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4461 ibm = ibms_init[i].data;
4462 BUG_ON(ibm == NULL);
4464 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4465 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4466 return -ENOSPC;
4467 strcpy(ibms_init[i].param, val);
4468 strcat(ibms_init[i].param, ",");
4469 return 0;
4473 return -EINVAL;
4476 static int experimental;
4477 module_param(experimental, int, 0);
4479 static u32 dbg_level;
4480 module_param_named(debug, dbg_level, uint, 0);
4482 static int force_load;
4483 module_param(force_load, bool, 0);
4485 static int fan_control_allowed;
4486 module_param_named(fan_control, fan_control_allowed, bool, 0);
4488 #define IBM_PARAM(feature) \
4489 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4491 IBM_PARAM(hotkey);
4492 IBM_PARAM(bluetooth);
4493 IBM_PARAM(video);
4494 IBM_PARAM(light);
4495 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4496 IBM_PARAM(dock);
4497 #endif
4498 #ifdef CONFIG_THINKPAD_ACPI_BAY
4499 IBM_PARAM(bay);
4500 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4501 IBM_PARAM(cmos);
4502 IBM_PARAM(led);
4503 IBM_PARAM(beep);
4504 IBM_PARAM(ecdump);
4505 IBM_PARAM(brightness);
4506 IBM_PARAM(volume);
4507 IBM_PARAM(fan);
4509 static int __init thinkpad_acpi_module_init(void)
4511 int ret, i;
4513 /* Driver-level probe */
4515 get_thinkpad_model_data(&thinkpad_id);
4516 ret = probe_for_thinkpad();
4517 if (ret) {
4518 thinkpad_acpi_module_exit();
4519 return ret;
4522 /* Driver initialization */
4524 IBM_ACPIHANDLE_INIT(ecrd);
4525 IBM_ACPIHANDLE_INIT(ecwr);
4527 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4528 if (!proc_dir) {
4529 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4530 thinkpad_acpi_module_exit();
4531 return -ENODEV;
4533 proc_dir->owner = THIS_MODULE;
4535 ret = platform_driver_register(&tpacpi_pdriver);
4536 if (ret) {
4537 printk(IBM_ERR "unable to register platform driver\n");
4538 thinkpad_acpi_module_exit();
4539 return ret;
4541 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4542 if (ret) {
4543 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4544 thinkpad_acpi_module_exit();
4545 return ret;
4549 /* Device initialization */
4550 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4551 NULL, 0);
4552 if (IS_ERR(tpacpi_pdev)) {
4553 ret = PTR_ERR(tpacpi_pdev);
4554 tpacpi_pdev = NULL;
4555 printk(IBM_ERR "unable to register platform device\n");
4556 thinkpad_acpi_module_exit();
4557 return ret;
4559 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4560 if (IS_ERR(tpacpi_hwmon)) {
4561 ret = PTR_ERR(tpacpi_hwmon);
4562 tpacpi_hwmon = NULL;
4563 printk(IBM_ERR "unable to register hwmon device\n");
4564 thinkpad_acpi_module_exit();
4565 return ret;
4567 tpacpi_inputdev = input_allocate_device();
4568 if (!tpacpi_inputdev) {
4569 printk(IBM_ERR "unable to allocate input device\n");
4570 thinkpad_acpi_module_exit();
4571 return -ENOMEM;
4572 } else {
4573 /* Prepare input device, but don't register */
4574 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4575 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4576 tpacpi_inputdev->id.bustype = BUS_HOST;
4577 tpacpi_inputdev->id.vendor = TPACPI_HKEY_INPUT_VENDOR;
4578 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4579 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4581 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4582 ret = ibm_init(&ibms_init[i]);
4583 if (ret >= 0 && *ibms_init[i].param)
4584 ret = ibms_init[i].data->write(ibms_init[i].param);
4585 if (ret < 0) {
4586 thinkpad_acpi_module_exit();
4587 return ret;
4590 ret = input_register_device(tpacpi_inputdev);
4591 if (ret < 0) {
4592 printk(IBM_ERR "unable to register input device\n");
4593 thinkpad_acpi_module_exit();
4594 return ret;
4595 } else {
4596 tp_features.input_device_registered = 1;
4599 return 0;
4602 static void thinkpad_acpi_module_exit(void)
4604 struct ibm_struct *ibm, *itmp;
4606 list_for_each_entry_safe_reverse(ibm, itmp,
4607 &tpacpi_all_drivers,
4608 all_drivers) {
4609 ibm_exit(ibm);
4612 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4614 if (tpacpi_inputdev) {
4615 if (tp_features.input_device_registered)
4616 input_unregister_device(tpacpi_inputdev);
4617 else
4618 input_free_device(tpacpi_inputdev);
4621 if (tpacpi_hwmon)
4622 hwmon_device_unregister(tpacpi_hwmon);
4624 if (tpacpi_pdev)
4625 platform_device_unregister(tpacpi_pdev);
4627 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4628 platform_driver_unregister(&tpacpi_pdriver);
4630 if (proc_dir)
4631 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4633 kfree(thinkpad_id.bios_version_str);
4634 kfree(thinkpad_id.ec_version_str);
4635 kfree(thinkpad_id.model_str);
4638 module_init(thinkpad_acpi_module_init);
4639 module_exit(thinkpad_acpi_module_exit);