ACPI: thinkpad-acpi: fix regression on HKEY LID event handling
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob0a33c6ee4508e2eda2b51f09df9c5557b561fa00
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.16"
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 static enum {
121 TPACPI_LIFE_INIT = 0,
122 TPACPI_LIFE_RUNNING,
123 TPACPI_LIFE_EXITING,
124 } tpacpi_lifecycle;
126 /****************************************************************************
127 ****************************************************************************
129 * ACPI Helpers and device model
131 ****************************************************************************
132 ****************************************************************************/
134 /*************************************************************************
135 * ACPI basic handles
138 static acpi_handle root_handle;
140 #define IBM_HANDLE(object, parent, paths...) \
141 static acpi_handle object##_handle; \
142 static acpi_handle *object##_parent = &parent##_handle; \
143 static char *object##_path; \
144 static char *object##_paths[] = { paths }
146 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
147 "\\_SB.PCI.ISA.EC", /* 570 */
148 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
149 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
150 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
151 "\\_SB.PCI0.ICH3.EC0", /* R31 */
152 "\\_SB.PCI0.LPC.EC", /* all others */
155 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
156 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
159 /*************************************************************************
160 * Misc ACPI handles
163 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
164 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
165 "\\CMS", /* R40, R40e */
166 ); /* all others */
168 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
169 "^HKEY", /* R30, R31 */
170 "HKEY", /* all others */
171 ); /* 570 */
174 /*************************************************************************
175 * ACPI helpers
178 static int acpi_evalf(acpi_handle handle,
179 void *res, char *method, char *fmt, ...)
181 char *fmt0 = fmt;
182 struct acpi_object_list params;
183 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
184 struct acpi_buffer result, *resultp;
185 union acpi_object out_obj;
186 acpi_status status;
187 va_list ap;
188 char res_type;
189 int success;
190 int quiet;
192 if (!*fmt) {
193 printk(IBM_ERR "acpi_evalf() called with empty format\n");
194 return 0;
197 if (*fmt == 'q') {
198 quiet = 1;
199 fmt++;
200 } else
201 quiet = 0;
203 res_type = *(fmt++);
205 params.count = 0;
206 params.pointer = &in_objs[0];
208 va_start(ap, fmt);
209 while (*fmt) {
210 char c = *(fmt++);
211 switch (c) {
212 case 'd': /* int */
213 in_objs[params.count].integer.value = va_arg(ap, int);
214 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
215 break;
216 /* add more types as needed */
217 default:
218 printk(IBM_ERR "acpi_evalf() called "
219 "with invalid format character '%c'\n", c);
220 return 0;
223 va_end(ap);
225 if (res_type != 'v') {
226 result.length = sizeof(out_obj);
227 result.pointer = &out_obj;
228 resultp = &result;
229 } else
230 resultp = NULL;
232 status = acpi_evaluate_object(handle, method, &params, resultp);
234 switch (res_type) {
235 case 'd': /* int */
236 if (res)
237 *(int *)res = out_obj.integer.value;
238 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
239 break;
240 case 'v': /* void */
241 success = status == AE_OK;
242 break;
243 /* add more types as needed */
244 default:
245 printk(IBM_ERR "acpi_evalf() called "
246 "with invalid format character '%c'\n", res_type);
247 return 0;
250 if (!success && !quiet)
251 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
252 method, fmt0, status);
254 return success;
257 static void __unused acpi_print_int(acpi_handle handle, char *method)
259 int i;
261 if (acpi_evalf(handle, &i, method, "d"))
262 printk(IBM_INFO "%s = 0x%x\n", method, i);
263 else
264 printk(IBM_ERR "error calling %s\n", method);
267 static int acpi_ec_read(int i, u8 * p)
269 int v;
271 if (ecrd_handle) {
272 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
273 return 0;
274 *p = v;
275 } else {
276 if (ec_read(i, p) < 0)
277 return 0;
280 return 1;
283 static int acpi_ec_write(int i, u8 v)
285 if (ecwr_handle) {
286 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
287 return 0;
288 } else {
289 if (ec_write(i, v) < 0)
290 return 0;
293 return 1;
296 static int _sta(acpi_handle handle)
298 int status;
300 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
301 status = 0;
303 return status;
306 static int issue_thinkpad_cmos_command(int cmos_cmd)
308 if (!cmos_handle)
309 return -ENXIO;
311 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
312 return -EIO;
314 return 0;
317 /*************************************************************************
318 * ACPI device model
321 static void drv_acpi_handle_init(char *name,
322 acpi_handle *handle, acpi_handle parent,
323 char **paths, int num_paths, char **path)
325 int i;
326 acpi_status status;
328 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
329 name);
331 for (i = 0; i < num_paths; i++) {
332 status = acpi_get_handle(parent, paths[i], handle);
333 if (ACPI_SUCCESS(status)) {
334 *path = paths[i];
335 dbg_printk(TPACPI_DBG_INIT,
336 "Found ACPI handle %s for %s\n",
337 *path, name);
338 return;
342 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
343 name);
344 *handle = NULL;
347 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
349 struct ibm_struct *ibm = data;
351 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
352 return;
354 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
355 return;
357 ibm->acpi->notify(ibm, event);
360 static int __init setup_acpi_notify(struct ibm_struct *ibm)
362 acpi_status status;
363 int rc;
365 BUG_ON(!ibm->acpi);
367 if (!*ibm->acpi->handle)
368 return 0;
370 vdbg_printk(TPACPI_DBG_INIT,
371 "setting up ACPI notify for %s\n", ibm->name);
373 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
374 if (rc < 0) {
375 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
376 ibm->name, rc);
377 return -ENODEV;
380 acpi_driver_data(ibm->acpi->device) = ibm;
381 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
382 IBM_ACPI_EVENT_PREFIX,
383 ibm->name);
385 status = acpi_install_notify_handler(*ibm->acpi->handle,
386 ibm->acpi->type, dispatch_acpi_notify, ibm);
387 if (ACPI_FAILURE(status)) {
388 if (status == AE_ALREADY_EXISTS) {
389 printk(IBM_NOTICE "another device driver is already handling %s events\n",
390 ibm->name);
391 } else {
392 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
393 ibm->name, status);
395 return -ENODEV;
397 ibm->flags.acpi_notify_installed = 1;
398 return 0;
401 static int __init tpacpi_device_add(struct acpi_device *device)
403 return 0;
406 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
408 int rc;
410 dbg_printk(TPACPI_DBG_INIT,
411 "registering %s as an ACPI driver\n", ibm->name);
413 BUG_ON(!ibm->acpi);
415 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
416 if (!ibm->acpi->driver) {
417 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
418 return -ENOMEM;
421 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
422 ibm->acpi->driver->ids = ibm->acpi->hid;
424 ibm->acpi->driver->ops.add = &tpacpi_device_add;
426 rc = acpi_bus_register_driver(ibm->acpi->driver);
427 if (rc < 0) {
428 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
429 ibm->name, rc);
430 kfree(ibm->acpi->driver);
431 ibm->acpi->driver = NULL;
432 } else if (!rc)
433 ibm->flags.acpi_driver_registered = 1;
435 return rc;
439 /****************************************************************************
440 ****************************************************************************
442 * Procfs Helpers
444 ****************************************************************************
445 ****************************************************************************/
447 static int dispatch_procfs_read(char *page, char **start, off_t off,
448 int count, int *eof, void *data)
450 struct ibm_struct *ibm = data;
451 int len;
453 if (!ibm || !ibm->read)
454 return -EINVAL;
456 len = ibm->read(page);
457 if (len < 0)
458 return len;
460 if (len <= off + count)
461 *eof = 1;
462 *start = page + off;
463 len -= off;
464 if (len > count)
465 len = count;
466 if (len < 0)
467 len = 0;
469 return len;
472 static int dispatch_procfs_write(struct file *file,
473 const char __user * userbuf,
474 unsigned long count, void *data)
476 struct ibm_struct *ibm = data;
477 char *kernbuf;
478 int ret;
480 if (!ibm || !ibm->write)
481 return -EINVAL;
483 kernbuf = kmalloc(count + 2, GFP_KERNEL);
484 if (!kernbuf)
485 return -ENOMEM;
487 if (copy_from_user(kernbuf, userbuf, count)) {
488 kfree(kernbuf);
489 return -EFAULT;
492 kernbuf[count] = 0;
493 strcat(kernbuf, ",");
494 ret = ibm->write(kernbuf);
495 if (ret == 0)
496 ret = count;
498 kfree(kernbuf);
500 return ret;
503 static char *next_cmd(char **cmds)
505 char *start = *cmds;
506 char *end;
508 while ((end = strchr(start, ',')) && end == start)
509 start = end + 1;
511 if (!end)
512 return NULL;
514 *end = 0;
515 *cmds = end + 1;
516 return start;
520 /****************************************************************************
521 ****************************************************************************
523 * Device model: input, hwmon and platform
525 ****************************************************************************
526 ****************************************************************************/
528 static struct platform_device *tpacpi_pdev;
529 static struct class_device *tpacpi_hwmon;
530 static struct input_dev *tpacpi_inputdev;
531 static struct mutex tpacpi_inputdev_send_mutex;
534 static int tpacpi_resume_handler(struct platform_device *pdev)
536 struct ibm_struct *ibm, *itmp;
538 list_for_each_entry_safe(ibm, itmp,
539 &tpacpi_all_drivers,
540 all_drivers) {
541 if (ibm->resume)
542 (ibm->resume)();
545 return 0;
548 static struct platform_driver tpacpi_pdriver = {
549 .driver = {
550 .name = IBM_DRVR_NAME,
551 .owner = THIS_MODULE,
553 .resume = tpacpi_resume_handler,
557 /*************************************************************************
558 * thinkpad-acpi driver attributes
561 /* interface_version --------------------------------------------------- */
562 static ssize_t tpacpi_driver_interface_version_show(
563 struct device_driver *drv,
564 char *buf)
566 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
569 static DRIVER_ATTR(interface_version, S_IRUGO,
570 tpacpi_driver_interface_version_show, NULL);
572 /* debug_level --------------------------------------------------------- */
573 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
574 char *buf)
576 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
579 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
580 const char *buf, size_t count)
582 unsigned long t;
584 if (parse_strtoul(buf, 0xffff, &t))
585 return -EINVAL;
587 dbg_level = t;
589 return count;
592 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
593 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
595 /* version ------------------------------------------------------------- */
596 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
597 char *buf)
599 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
602 static DRIVER_ATTR(version, S_IRUGO,
603 tpacpi_driver_version_show, NULL);
605 /* --------------------------------------------------------------------- */
607 static struct driver_attribute* tpacpi_driver_attributes[] = {
608 &driver_attr_debug_level, &driver_attr_version,
609 &driver_attr_interface_version,
612 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
614 int i, res;
616 i = 0;
617 res = 0;
618 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
619 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
620 i++;
623 return res;
626 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
628 int i;
630 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
631 driver_remove_file(drv, tpacpi_driver_attributes[i]);
634 /*************************************************************************
635 * sysfs support helpers
638 struct attribute_set_obj {
639 struct attribute_set s;
640 struct attribute *a;
641 } __attribute__((packed));
643 static struct attribute_set *create_attr_set(unsigned int max_members,
644 const char* name)
646 struct attribute_set_obj *sobj;
648 if (max_members == 0)
649 return NULL;
651 /* Allocates space for implicit NULL at the end too */
652 sobj = kzalloc(sizeof(struct attribute_set_obj) +
653 max_members * sizeof(struct attribute *),
654 GFP_KERNEL);
655 if (!sobj)
656 return NULL;
657 sobj->s.max_members = max_members;
658 sobj->s.group.attrs = &sobj->a;
659 sobj->s.group.name = name;
661 return &sobj->s;
664 /* not multi-threaded safe, use it in a single thread per set */
665 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
667 if (!s || !attr)
668 return -EINVAL;
670 if (s->members >= s->max_members)
671 return -ENOMEM;
673 s->group.attrs[s->members] = attr;
674 s->members++;
676 return 0;
679 static int add_many_to_attr_set(struct attribute_set* s,
680 struct attribute **attr,
681 unsigned int count)
683 int i, res;
685 for (i = 0; i < count; i++) {
686 res = add_to_attr_set(s, attr[i]);
687 if (res)
688 return res;
691 return 0;
694 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
696 sysfs_remove_group(kobj, &s->group);
697 destroy_attr_set(s);
700 static int parse_strtoul(const char *buf,
701 unsigned long max, unsigned long *value)
703 char *endp;
705 *value = simple_strtoul(buf, &endp, 0);
706 while (*endp && isspace(*endp))
707 endp++;
708 if (*endp || *value > max)
709 return -EINVAL;
711 return 0;
714 /****************************************************************************
715 ****************************************************************************
717 * Subdrivers
719 ****************************************************************************
720 ****************************************************************************/
722 /*************************************************************************
723 * thinkpad-acpi init subdriver
726 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
728 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
729 printk(IBM_INFO "%s\n", IBM_URL);
731 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
732 (thinkpad_id.bios_version_str) ?
733 thinkpad_id.bios_version_str : "unknown",
734 (thinkpad_id.ec_version_str) ?
735 thinkpad_id.ec_version_str : "unknown");
737 if (thinkpad_id.vendor && thinkpad_id.model_str)
738 printk(IBM_INFO "%s %s\n",
739 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
740 "IBM" : ((thinkpad_id.vendor ==
741 PCI_VENDOR_ID_LENOVO) ?
742 "Lenovo" : "Unknown vendor"),
743 thinkpad_id.model_str);
745 return 0;
748 static int thinkpad_acpi_driver_read(char *p)
750 int len = 0;
752 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
753 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
755 return len;
758 static struct ibm_struct thinkpad_acpi_driver_data = {
759 .name = "driver",
760 .read = thinkpad_acpi_driver_read,
763 /*************************************************************************
764 * Hotkey subdriver
767 static int hotkey_orig_status;
768 static u32 hotkey_orig_mask;
769 static u32 hotkey_all_mask;
770 static u32 hotkey_reserved_mask;
772 static u16 *hotkey_keycode_map;
774 static struct attribute_set *hotkey_dev_attributes;
776 static int hotkey_get_wlsw(int *status)
778 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
779 return -EIO;
780 return 0;
783 /* sysfs hotkey enable ------------------------------------------------- */
784 static ssize_t hotkey_enable_show(struct device *dev,
785 struct device_attribute *attr,
786 char *buf)
788 int res, status;
789 u32 mask;
791 res = hotkey_get(&status, &mask);
792 if (res)
793 return res;
795 return snprintf(buf, PAGE_SIZE, "%d\n", status);
798 static ssize_t hotkey_enable_store(struct device *dev,
799 struct device_attribute *attr,
800 const char *buf, size_t count)
802 unsigned long t;
803 int res, status;
804 u32 mask;
806 if (parse_strtoul(buf, 1, &t))
807 return -EINVAL;
809 res = hotkey_get(&status, &mask);
810 if (!res)
811 res = hotkey_set(t, mask);
813 return (res) ? res : count;
816 static struct device_attribute dev_attr_hotkey_enable =
817 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
818 hotkey_enable_show, hotkey_enable_store);
820 /* sysfs hotkey mask --------------------------------------------------- */
821 static ssize_t hotkey_mask_show(struct device *dev,
822 struct device_attribute *attr,
823 char *buf)
825 int res, status;
826 u32 mask;
828 res = hotkey_get(&status, &mask);
829 if (res)
830 return res;
832 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
835 static ssize_t hotkey_mask_store(struct device *dev,
836 struct device_attribute *attr,
837 const char *buf, size_t count)
839 unsigned long t;
840 int res, status;
841 u32 mask;
843 if (parse_strtoul(buf, 0xffffffffUL, &t))
844 return -EINVAL;
846 res = hotkey_get(&status, &mask);
847 if (!res)
848 hotkey_set(status, t);
850 return (res) ? res : count;
853 static struct device_attribute dev_attr_hotkey_mask =
854 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
855 hotkey_mask_show, hotkey_mask_store);
857 /* sysfs hotkey bios_enabled ------------------------------------------- */
858 static ssize_t hotkey_bios_enabled_show(struct device *dev,
859 struct device_attribute *attr,
860 char *buf)
862 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
865 static struct device_attribute dev_attr_hotkey_bios_enabled =
866 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
868 /* sysfs hotkey bios_mask ---------------------------------------------- */
869 static ssize_t hotkey_bios_mask_show(struct device *dev,
870 struct device_attribute *attr,
871 char *buf)
873 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
876 static struct device_attribute dev_attr_hotkey_bios_mask =
877 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
879 /* sysfs hotkey all_mask ----------------------------------------------- */
880 static ssize_t hotkey_all_mask_show(struct device *dev,
881 struct device_attribute *attr,
882 char *buf)
884 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
887 static struct device_attribute dev_attr_hotkey_all_mask =
888 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
890 /* sysfs hotkey recommended_mask --------------------------------------- */
891 static ssize_t hotkey_recommended_mask_show(struct device *dev,
892 struct device_attribute *attr,
893 char *buf)
895 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
896 hotkey_all_mask & ~hotkey_reserved_mask);
899 static struct device_attribute dev_attr_hotkey_recommended_mask =
900 __ATTR(hotkey_recommended_mask, S_IRUGO,
901 hotkey_recommended_mask_show, NULL);
903 /* sysfs hotkey radio_sw ----------------------------------------------- */
904 static ssize_t hotkey_radio_sw_show(struct device *dev,
905 struct device_attribute *attr,
906 char *buf)
908 int res, s;
909 res = hotkey_get_wlsw(&s);
910 if (res < 0)
911 return res;
913 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
916 static struct device_attribute dev_attr_hotkey_radio_sw =
917 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
919 /* sysfs hotkey report_mode -------------------------------------------- */
920 static ssize_t hotkey_report_mode_show(struct device *dev,
921 struct device_attribute *attr,
922 char *buf)
924 return snprintf(buf, PAGE_SIZE, "%d\n",
925 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
928 static struct device_attribute dev_attr_hotkey_report_mode =
929 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
931 /* --------------------------------------------------------------------- */
933 static struct attribute *hotkey_attributes[] __initdata = {
934 &dev_attr_hotkey_enable.attr,
935 &dev_attr_hotkey_report_mode.attr,
938 static struct attribute *hotkey_mask_attributes[] __initdata = {
939 &dev_attr_hotkey_mask.attr,
940 &dev_attr_hotkey_bios_enabled.attr,
941 &dev_attr_hotkey_bios_mask.attr,
942 &dev_attr_hotkey_all_mask.attr,
943 &dev_attr_hotkey_recommended_mask.attr,
946 static int __init hotkey_init(struct ibm_init_struct *iibm)
949 static u16 ibm_keycode_map[] __initdata = {
950 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
951 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
952 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
953 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
954 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
955 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
956 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
957 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
958 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
959 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
960 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
961 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
962 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
963 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
964 KEY_RESERVED, /* 0x14: VOLUME UP */
965 KEY_RESERVED, /* 0x15: VOLUME DOWN */
966 KEY_RESERVED, /* 0x16: MUTE */
967 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
968 /* (assignments unknown, please report if found) */
969 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
970 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
972 static u16 lenovo_keycode_map[] __initdata = {
973 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
974 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
975 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
976 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
977 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
978 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
979 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
980 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
981 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
982 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
983 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
984 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
985 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
986 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
987 KEY_RESERVED, /* 0x14: VOLUME UP */
988 KEY_RESERVED, /* 0x15: VOLUME DOWN */
989 KEY_RESERVED, /* 0x16: MUTE */
990 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
991 /* (assignments unknown, please report if found) */
992 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
993 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
996 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
997 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
998 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1000 int res, i;
1001 int status;
1002 int hkeyv;
1004 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1006 BUG_ON(!tpacpi_inputdev);
1008 IBM_ACPIHANDLE_INIT(hkey);
1009 mutex_init(&hotkey_mutex);
1011 /* hotkey not supported on 570 */
1012 tp_features.hotkey = hkey_handle != NULL;
1014 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1015 str_supported(tp_features.hotkey));
1017 if (tp_features.hotkey) {
1018 hotkey_dev_attributes = create_attr_set(8, NULL);
1019 if (!hotkey_dev_attributes)
1020 return -ENOMEM;
1021 res = add_many_to_attr_set(hotkey_dev_attributes,
1022 hotkey_attributes,
1023 ARRAY_SIZE(hotkey_attributes));
1024 if (res)
1025 return res;
1027 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1028 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1029 for HKEY interface version 0x100 */
1030 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1031 if ((hkeyv >> 8) != 1) {
1032 printk(IBM_ERR "unknown version of the "
1033 "HKEY interface: 0x%x\n", hkeyv);
1034 printk(IBM_ERR "please report this to %s\n",
1035 IBM_MAIL);
1036 } else {
1038 * MHKV 0x100 in A31, R40, R40e,
1039 * T4x, X31, and later
1040 * */
1041 tp_features.hotkey_mask = 1;
1045 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1046 str_supported(tp_features.hotkey_mask));
1048 if (tp_features.hotkey_mask) {
1049 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1050 "MHKA", "qd")) {
1051 printk(IBM_ERR
1052 "missing MHKA handler, "
1053 "please report this to %s\n",
1054 IBM_MAIL);
1055 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1059 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
1060 if (!res && tp_features.hotkey_mask) {
1061 res = add_many_to_attr_set(hotkey_dev_attributes,
1062 hotkey_mask_attributes,
1063 ARRAY_SIZE(hotkey_mask_attributes));
1066 /* Not all thinkpads have a hardware radio switch */
1067 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1068 tp_features.hotkey_wlsw = 1;
1069 printk(IBM_INFO
1070 "radio switch found; radios are %s\n",
1071 enabled(status, 0));
1072 res = add_to_attr_set(hotkey_dev_attributes,
1073 &dev_attr_hotkey_radio_sw.attr);
1076 if (!res)
1077 res = register_attr_set_with_sysfs(
1078 hotkey_dev_attributes,
1079 &tpacpi_pdev->dev.kobj);
1080 if (res)
1081 return res;
1083 /* Set up key map */
1085 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1086 GFP_KERNEL);
1087 if (!hotkey_keycode_map) {
1088 printk(IBM_ERR "failed to allocate memory for key map\n");
1089 return -ENOMEM;
1092 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1093 dbg_printk(TPACPI_DBG_INIT,
1094 "using Lenovo default hot key map\n");
1095 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1096 TPACPI_HOTKEY_MAP_SIZE);
1097 } else {
1098 dbg_printk(TPACPI_DBG_INIT,
1099 "using IBM default hot key map\n");
1100 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1101 TPACPI_HOTKEY_MAP_SIZE);
1104 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1105 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1106 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1107 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1108 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1109 tpacpi_inputdev->keycode = hotkey_keycode_map;
1110 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1111 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1112 set_bit(hotkey_keycode_map[i],
1113 tpacpi_inputdev->keybit);
1114 } else {
1115 if (i < sizeof(hotkey_reserved_mask)*8)
1116 hotkey_reserved_mask |= 1 << i;
1120 if (tp_features.hotkey_wlsw) {
1121 set_bit(EV_SW, tpacpi_inputdev->evbit);
1122 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1125 dbg_printk(TPACPI_DBG_INIT,
1126 "enabling hot key handling\n");
1127 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1128 | hotkey_orig_mask);
1129 if (res)
1130 return res;
1132 dbg_printk(TPACPI_DBG_INIT,
1133 "legacy hot key reporting over procfs %s\n",
1134 (hotkey_report_mode < 2) ?
1135 "enabled" : "disabled");
1138 return (tp_features.hotkey)? 0 : 1;
1141 static void hotkey_exit(void)
1143 int res;
1145 if (tp_features.hotkey) {
1146 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
1147 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1148 if (res)
1149 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
1152 if (hotkey_dev_attributes) {
1153 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1154 hotkey_dev_attributes = NULL;
1158 static void tpacpi_input_send_key(unsigned int scancode,
1159 unsigned int keycode)
1161 if (keycode != KEY_RESERVED) {
1162 mutex_lock(&tpacpi_inputdev_send_mutex);
1164 input_report_key(tpacpi_inputdev, keycode, 1);
1165 if (keycode == KEY_UNKNOWN)
1166 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1167 scancode);
1168 input_sync(tpacpi_inputdev);
1170 input_report_key(tpacpi_inputdev, keycode, 0);
1171 if (keycode == KEY_UNKNOWN)
1172 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1173 scancode);
1174 input_sync(tpacpi_inputdev);
1176 mutex_unlock(&tpacpi_inputdev_send_mutex);
1180 static void tpacpi_input_send_radiosw(void)
1182 int wlsw;
1184 mutex_lock(&tpacpi_inputdev_send_mutex);
1186 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1187 input_report_switch(tpacpi_inputdev,
1188 SW_RADIO, !!wlsw);
1189 input_sync(tpacpi_inputdev);
1192 mutex_unlock(&tpacpi_inputdev_send_mutex);
1195 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1197 u32 hkey;
1198 unsigned int keycode, scancode;
1199 int send_acpi_ev;
1200 int ignore_acpi_ev;
1202 if (event != 0x80) {
1203 printk(IBM_ERR "unknown HKEY notification event %d\n", event);
1204 /* forward it to userspace, maybe it knows how to handle it */
1205 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1206 ibm->acpi->device->dev.bus_id,
1207 event, 0);
1208 return;
1211 while (1) {
1212 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1213 printk(IBM_ERR "failed to retrieve HKEY event\n");
1214 return;
1217 if (hkey == 0) {
1218 /* queue empty */
1219 return;
1222 send_acpi_ev = 0;
1223 ignore_acpi_ev = 0;
1225 switch (hkey >> 12) {
1226 case 1:
1227 /* 0x1000-0x1FFF: key presses */
1228 scancode = hkey & 0xfff;
1229 if (scancode > 0 && scancode < 0x21) {
1230 scancode--;
1231 keycode = hotkey_keycode_map[scancode];
1232 tpacpi_input_send_key(scancode, keycode);
1233 } else {
1234 printk(IBM_ERR
1235 "hotkey 0x%04x out of range for keyboard map\n",
1236 hkey);
1237 send_acpi_ev = 1;
1239 break;
1240 case 5:
1241 /* 0x5000-0x5FFF: LID */
1242 /* we don't handle it through this path, just
1243 * eat up known LID events */
1244 if (hkey != 0x5001 && hkey != 0x5002) {
1245 printk(IBM_ERR
1246 "unknown LID-related HKEY event: 0x%04x\n",
1247 hkey);
1248 send_acpi_ev = 1;
1249 } else {
1250 ignore_acpi_ev = 1;
1252 break;
1253 case 7:
1254 /* 0x7000-0x7FFF: misc */
1255 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1256 tpacpi_input_send_radiosw();
1257 break;
1259 /* fallthrough to default */
1260 default:
1261 /* case 2: dock-related */
1262 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1263 /* case 3: ultra-bay related. maybe bay in dock? */
1264 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1265 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1266 send_acpi_ev = 1;
1269 /* Legacy events */
1270 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
1271 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
1274 /* netlink events */
1275 if (!ignore_acpi_ev && send_acpi_ev) {
1276 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1277 ibm->acpi->device->dev.bus_id,
1278 event, hkey);
1283 static void hotkey_resume(void)
1285 tpacpi_input_send_radiosw();
1289 * Call with hotkey_mutex held
1291 static int hotkey_get(int *status, u32 *mask)
1293 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1294 return -EIO;
1296 if (tp_features.hotkey_mask)
1297 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
1298 return -EIO;
1300 return 0;
1304 * Call with hotkey_mutex held
1306 static int hotkey_set(int status, u32 mask)
1308 int i;
1310 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1311 return -EIO;
1313 if (tp_features.hotkey_mask)
1314 for (i = 0; i < 32; i++) {
1315 int bit = ((1 << i) & mask) != 0;
1316 if (!acpi_evalf(hkey_handle,
1317 NULL, "MHKM", "vdd", i + 1, bit))
1318 return -EIO;
1321 return 0;
1324 /* procfs -------------------------------------------------------------- */
1325 static int hotkey_read(char *p)
1327 int res, status;
1328 u32 mask;
1329 int len = 0;
1331 if (!tp_features.hotkey) {
1332 len += sprintf(p + len, "status:\t\tnot supported\n");
1333 return len;
1336 res = mutex_lock_interruptible(&hotkey_mutex);
1337 if (res < 0)
1338 return res;
1339 res = hotkey_get(&status, &mask);
1340 mutex_unlock(&hotkey_mutex);
1341 if (res)
1342 return res;
1344 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1345 if (tp_features.hotkey_mask) {
1346 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1347 len += sprintf(p + len,
1348 "commands:\tenable, disable, reset, <mask>\n");
1349 } else {
1350 len += sprintf(p + len, "mask:\t\tnot supported\n");
1351 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1354 return len;
1357 static int hotkey_write(char *buf)
1359 int res, status;
1360 u32 mask;
1361 char *cmd;
1362 int do_cmd = 0;
1364 if (!tp_features.hotkey)
1365 return -ENODEV;
1367 res = mutex_lock_interruptible(&hotkey_mutex);
1368 if (res < 0)
1369 return res;
1371 res = hotkey_get(&status, &mask);
1372 if (res)
1373 goto errexit;
1375 res = 0;
1376 while ((cmd = next_cmd(&buf))) {
1377 if (strlencmp(cmd, "enable") == 0) {
1378 status = 1;
1379 } else if (strlencmp(cmd, "disable") == 0) {
1380 status = 0;
1381 } else if (strlencmp(cmd, "reset") == 0) {
1382 status = hotkey_orig_status;
1383 mask = hotkey_orig_mask;
1384 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1385 /* mask set */
1386 } else if (sscanf(cmd, "%x", &mask) == 1) {
1387 /* mask set */
1388 } else {
1389 res = -EINVAL;
1390 goto errexit;
1392 do_cmd = 1;
1395 if (do_cmd)
1396 res = hotkey_set(status, mask);
1398 errexit:
1399 mutex_unlock(&hotkey_mutex);
1400 return res;
1403 static const struct acpi_device_id ibm_htk_device_ids[] = {
1404 {IBM_HKEY_HID, 0},
1405 {"", 0},
1408 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1409 .hid = ibm_htk_device_ids,
1410 .notify = hotkey_notify,
1411 .handle = &hkey_handle,
1412 .type = ACPI_DEVICE_NOTIFY,
1415 static struct ibm_struct hotkey_driver_data = {
1416 .name = "hotkey",
1417 .read = hotkey_read,
1418 .write = hotkey_write,
1419 .exit = hotkey_exit,
1420 .resume = hotkey_resume,
1421 .acpi = &ibm_hotkey_acpidriver,
1424 /*************************************************************************
1425 * Bluetooth subdriver
1428 /* sysfs bluetooth enable ---------------------------------------------- */
1429 static ssize_t bluetooth_enable_show(struct device *dev,
1430 struct device_attribute *attr,
1431 char *buf)
1433 int status;
1435 status = bluetooth_get_radiosw();
1436 if (status < 0)
1437 return status;
1439 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1442 static ssize_t bluetooth_enable_store(struct device *dev,
1443 struct device_attribute *attr,
1444 const char *buf, size_t count)
1446 unsigned long t;
1447 int res;
1449 if (parse_strtoul(buf, 1, &t))
1450 return -EINVAL;
1452 res = bluetooth_set_radiosw(t);
1454 return (res) ? res : count;
1457 static struct device_attribute dev_attr_bluetooth_enable =
1458 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1459 bluetooth_enable_show, bluetooth_enable_store);
1461 /* --------------------------------------------------------------------- */
1463 static struct attribute *bluetooth_attributes[] = {
1464 &dev_attr_bluetooth_enable.attr,
1465 NULL
1468 static const struct attribute_group bluetooth_attr_group = {
1469 .attrs = bluetooth_attributes,
1472 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1474 int res;
1475 int status = 0;
1477 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1479 IBM_ACPIHANDLE_INIT(hkey);
1481 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1482 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1483 tp_features.bluetooth = hkey_handle &&
1484 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1486 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1487 str_supported(tp_features.bluetooth),
1488 status);
1490 if (tp_features.bluetooth) {
1491 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1492 /* no bluetooth hardware present in system */
1493 tp_features.bluetooth = 0;
1494 dbg_printk(TPACPI_DBG_INIT,
1495 "bluetooth hardware not installed\n");
1496 } else {
1497 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1498 &bluetooth_attr_group);
1499 if (res)
1500 return res;
1504 return (tp_features.bluetooth)? 0 : 1;
1507 static void bluetooth_exit(void)
1509 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1510 &bluetooth_attr_group);
1513 static int bluetooth_get_radiosw(void)
1515 int status;
1517 if (!tp_features.bluetooth)
1518 return -ENODEV;
1520 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1521 return -EIO;
1523 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1526 static int bluetooth_set_radiosw(int radio_on)
1528 int status;
1530 if (!tp_features.bluetooth)
1531 return -ENODEV;
1533 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1534 return -EIO;
1535 if (radio_on)
1536 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1537 else
1538 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1539 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1540 return -EIO;
1542 return 0;
1545 /* procfs -------------------------------------------------------------- */
1546 static int bluetooth_read(char *p)
1548 int len = 0;
1549 int status = bluetooth_get_radiosw();
1551 if (!tp_features.bluetooth)
1552 len += sprintf(p + len, "status:\t\tnot supported\n");
1553 else {
1554 len += sprintf(p + len, "status:\t\t%s\n",
1555 (status)? "enabled" : "disabled");
1556 len += sprintf(p + len, "commands:\tenable, disable\n");
1559 return len;
1562 static int bluetooth_write(char *buf)
1564 char *cmd;
1566 if (!tp_features.bluetooth)
1567 return -ENODEV;
1569 while ((cmd = next_cmd(&buf))) {
1570 if (strlencmp(cmd, "enable") == 0) {
1571 bluetooth_set_radiosw(1);
1572 } else if (strlencmp(cmd, "disable") == 0) {
1573 bluetooth_set_radiosw(0);
1574 } else
1575 return -EINVAL;
1578 return 0;
1581 static struct ibm_struct bluetooth_driver_data = {
1582 .name = "bluetooth",
1583 .read = bluetooth_read,
1584 .write = bluetooth_write,
1585 .exit = bluetooth_exit,
1588 /*************************************************************************
1589 * Wan subdriver
1592 /* sysfs wan enable ---------------------------------------------------- */
1593 static ssize_t wan_enable_show(struct device *dev,
1594 struct device_attribute *attr,
1595 char *buf)
1597 int status;
1599 status = wan_get_radiosw();
1600 if (status < 0)
1601 return status;
1603 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1606 static ssize_t wan_enable_store(struct device *dev,
1607 struct device_attribute *attr,
1608 const char *buf, size_t count)
1610 unsigned long t;
1611 int res;
1613 if (parse_strtoul(buf, 1, &t))
1614 return -EINVAL;
1616 res = wan_set_radiosw(t);
1618 return (res) ? res : count;
1621 static struct device_attribute dev_attr_wan_enable =
1622 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1623 wan_enable_show, wan_enable_store);
1625 /* --------------------------------------------------------------------- */
1627 static struct attribute *wan_attributes[] = {
1628 &dev_attr_wan_enable.attr,
1629 NULL
1632 static const struct attribute_group wan_attr_group = {
1633 .attrs = wan_attributes,
1636 static int __init wan_init(struct ibm_init_struct *iibm)
1638 int res;
1639 int status = 0;
1641 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1643 IBM_ACPIHANDLE_INIT(hkey);
1645 tp_features.wan = hkey_handle &&
1646 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1648 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1649 str_supported(tp_features.wan),
1650 status);
1652 if (tp_features.wan) {
1653 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1654 /* no wan hardware present in system */
1655 tp_features.wan = 0;
1656 dbg_printk(TPACPI_DBG_INIT,
1657 "wan hardware not installed\n");
1658 } else {
1659 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1660 &wan_attr_group);
1661 if (res)
1662 return res;
1666 return (tp_features.wan)? 0 : 1;
1669 static void wan_exit(void)
1671 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1672 &wan_attr_group);
1675 static int wan_get_radiosw(void)
1677 int status;
1679 if (!tp_features.wan)
1680 return -ENODEV;
1682 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1683 return -EIO;
1685 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1688 static int wan_set_radiosw(int radio_on)
1690 int status;
1692 if (!tp_features.wan)
1693 return -ENODEV;
1695 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1696 return -EIO;
1697 if (radio_on)
1698 status |= TP_ACPI_WANCARD_RADIOSSW;
1699 else
1700 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1701 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1702 return -EIO;
1704 return 0;
1707 /* procfs -------------------------------------------------------------- */
1708 static int wan_read(char *p)
1710 int len = 0;
1711 int status = wan_get_radiosw();
1713 if (!tp_features.wan)
1714 len += sprintf(p + len, "status:\t\tnot supported\n");
1715 else {
1716 len += sprintf(p + len, "status:\t\t%s\n",
1717 (status)? "enabled" : "disabled");
1718 len += sprintf(p + len, "commands:\tenable, disable\n");
1721 return len;
1724 static int wan_write(char *buf)
1726 char *cmd;
1728 if (!tp_features.wan)
1729 return -ENODEV;
1731 while ((cmd = next_cmd(&buf))) {
1732 if (strlencmp(cmd, "enable") == 0) {
1733 wan_set_radiosw(1);
1734 } else if (strlencmp(cmd, "disable") == 0) {
1735 wan_set_radiosw(0);
1736 } else
1737 return -EINVAL;
1740 return 0;
1743 static struct ibm_struct wan_driver_data = {
1744 .name = "wan",
1745 .read = wan_read,
1746 .write = wan_write,
1747 .exit = wan_exit,
1748 .flags.experimental = 1,
1751 /*************************************************************************
1752 * Video subdriver
1755 static enum video_access_mode video_supported;
1756 static int video_orig_autosw;
1758 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1759 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1760 "\\_SB.PCI0.VID0", /* 770e */
1761 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1762 "\\_SB.PCI0.AGP.VID", /* all others */
1763 ); /* R30, R31 */
1765 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1767 static int __init video_init(struct ibm_init_struct *iibm)
1769 int ivga;
1771 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1773 IBM_ACPIHANDLE_INIT(vid);
1774 IBM_ACPIHANDLE_INIT(vid2);
1776 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1777 /* G41, assume IVGA doesn't change */
1778 vid_handle = vid2_handle;
1780 if (!vid_handle)
1781 /* video switching not supported on R30, R31 */
1782 video_supported = TPACPI_VIDEO_NONE;
1783 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1784 /* 570 */
1785 video_supported = TPACPI_VIDEO_570;
1786 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1787 /* 600e/x, 770e, 770x */
1788 video_supported = TPACPI_VIDEO_770;
1789 else
1790 /* all others */
1791 video_supported = TPACPI_VIDEO_NEW;
1793 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1794 str_supported(video_supported != TPACPI_VIDEO_NONE),
1795 video_supported);
1797 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1800 static void video_exit(void)
1802 dbg_printk(TPACPI_DBG_EXIT,
1803 "restoring original video autoswitch mode\n");
1804 if (video_autosw_set(video_orig_autosw))
1805 printk(IBM_ERR "error while trying to restore original "
1806 "video autoswitch mode\n");
1809 static int video_outputsw_get(void)
1811 int status = 0;
1812 int i;
1814 switch (video_supported) {
1815 case TPACPI_VIDEO_570:
1816 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1817 TP_ACPI_VIDEO_570_PHSCMD))
1818 return -EIO;
1819 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1820 break;
1821 case TPACPI_VIDEO_770:
1822 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1823 return -EIO;
1824 if (i)
1825 status |= TP_ACPI_VIDEO_S_LCD;
1826 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1827 return -EIO;
1828 if (i)
1829 status |= TP_ACPI_VIDEO_S_CRT;
1830 break;
1831 case TPACPI_VIDEO_NEW:
1832 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1833 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1834 return -EIO;
1835 if (i)
1836 status |= TP_ACPI_VIDEO_S_CRT;
1838 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1839 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1840 return -EIO;
1841 if (i)
1842 status |= TP_ACPI_VIDEO_S_LCD;
1843 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1844 return -EIO;
1845 if (i)
1846 status |= TP_ACPI_VIDEO_S_DVI;
1847 break;
1848 default:
1849 return -ENOSYS;
1852 return status;
1855 static int video_outputsw_set(int status)
1857 int autosw;
1858 int res = 0;
1860 switch (video_supported) {
1861 case TPACPI_VIDEO_570:
1862 res = acpi_evalf(NULL, NULL,
1863 "\\_SB.PHS2", "vdd",
1864 TP_ACPI_VIDEO_570_PHS2CMD,
1865 status | TP_ACPI_VIDEO_570_PHS2SET);
1866 break;
1867 case TPACPI_VIDEO_770:
1868 autosw = video_autosw_get();
1869 if (autosw < 0)
1870 return autosw;
1872 res = video_autosw_set(1);
1873 if (res)
1874 return res;
1875 res = acpi_evalf(vid_handle, NULL,
1876 "ASWT", "vdd", status * 0x100, 0);
1877 if (!autosw && video_autosw_set(autosw)) {
1878 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1879 return -EIO;
1881 break;
1882 case TPACPI_VIDEO_NEW:
1883 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1884 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1885 break;
1886 default:
1887 return -ENOSYS;
1890 return (res)? 0 : -EIO;
1893 static int video_autosw_get(void)
1895 int autosw = 0;
1897 switch (video_supported) {
1898 case TPACPI_VIDEO_570:
1899 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1900 return -EIO;
1901 break;
1902 case TPACPI_VIDEO_770:
1903 case TPACPI_VIDEO_NEW:
1904 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1905 return -EIO;
1906 break;
1907 default:
1908 return -ENOSYS;
1911 return autosw & 1;
1914 static int video_autosw_set(int enable)
1916 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1917 return -EIO;
1918 return 0;
1921 static int video_outputsw_cycle(void)
1923 int autosw = video_autosw_get();
1924 int res;
1926 if (autosw < 0)
1927 return autosw;
1929 switch (video_supported) {
1930 case TPACPI_VIDEO_570:
1931 res = video_autosw_set(1);
1932 if (res)
1933 return res;
1934 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1935 break;
1936 case TPACPI_VIDEO_770:
1937 case TPACPI_VIDEO_NEW:
1938 res = video_autosw_set(1);
1939 if (res)
1940 return res;
1941 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1942 break;
1943 default:
1944 return -ENOSYS;
1946 if (!autosw && video_autosw_set(autosw)) {
1947 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1948 return -EIO;
1951 return (res)? 0 : -EIO;
1954 static int video_expand_toggle(void)
1956 switch (video_supported) {
1957 case TPACPI_VIDEO_570:
1958 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1959 0 : -EIO;
1960 case TPACPI_VIDEO_770:
1961 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1962 0 : -EIO;
1963 case TPACPI_VIDEO_NEW:
1964 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1965 0 : -EIO;
1966 default:
1967 return -ENOSYS;
1969 /* not reached */
1972 static int video_read(char *p)
1974 int status, autosw;
1975 int len = 0;
1977 if (video_supported == TPACPI_VIDEO_NONE) {
1978 len += sprintf(p + len, "status:\t\tnot supported\n");
1979 return len;
1982 status = video_outputsw_get();
1983 if (status < 0)
1984 return status;
1986 autosw = video_autosw_get();
1987 if (autosw < 0)
1988 return autosw;
1990 len += sprintf(p + len, "status:\t\tsupported\n");
1991 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1992 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1993 if (video_supported == TPACPI_VIDEO_NEW)
1994 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1995 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1996 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1997 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1998 if (video_supported == TPACPI_VIDEO_NEW)
1999 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2000 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2001 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2003 return len;
2006 static int video_write(char *buf)
2008 char *cmd;
2009 int enable, disable, status;
2010 int res;
2012 if (video_supported == TPACPI_VIDEO_NONE)
2013 return -ENODEV;
2015 enable = 0;
2016 disable = 0;
2018 while ((cmd = next_cmd(&buf))) {
2019 if (strlencmp(cmd, "lcd_enable") == 0) {
2020 enable |= TP_ACPI_VIDEO_S_LCD;
2021 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2022 disable |= TP_ACPI_VIDEO_S_LCD;
2023 } else if (strlencmp(cmd, "crt_enable") == 0) {
2024 enable |= TP_ACPI_VIDEO_S_CRT;
2025 } else if (strlencmp(cmd, "crt_disable") == 0) {
2026 disable |= TP_ACPI_VIDEO_S_CRT;
2027 } else if (video_supported == TPACPI_VIDEO_NEW &&
2028 strlencmp(cmd, "dvi_enable") == 0) {
2029 enable |= TP_ACPI_VIDEO_S_DVI;
2030 } else if (video_supported == TPACPI_VIDEO_NEW &&
2031 strlencmp(cmd, "dvi_disable") == 0) {
2032 disable |= TP_ACPI_VIDEO_S_DVI;
2033 } else if (strlencmp(cmd, "auto_enable") == 0) {
2034 res = video_autosw_set(1);
2035 if (res)
2036 return res;
2037 } else if (strlencmp(cmd, "auto_disable") == 0) {
2038 res = video_autosw_set(0);
2039 if (res)
2040 return res;
2041 } else if (strlencmp(cmd, "video_switch") == 0) {
2042 res = video_outputsw_cycle();
2043 if (res)
2044 return res;
2045 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2046 res = video_expand_toggle();
2047 if (res)
2048 return res;
2049 } else
2050 return -EINVAL;
2053 if (enable || disable) {
2054 status = video_outputsw_get();
2055 if (status < 0)
2056 return status;
2057 res = video_outputsw_set((status & ~disable) | enable);
2058 if (res)
2059 return res;
2062 return 0;
2065 static struct ibm_struct video_driver_data = {
2066 .name = "video",
2067 .read = video_read,
2068 .write = video_write,
2069 .exit = video_exit,
2072 /*************************************************************************
2073 * Light (thinklight) subdriver
2076 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2077 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2079 static int __init light_init(struct ibm_init_struct *iibm)
2081 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2083 IBM_ACPIHANDLE_INIT(ledb);
2084 IBM_ACPIHANDLE_INIT(lght);
2085 IBM_ACPIHANDLE_INIT(cmos);
2087 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2088 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2090 if (tp_features.light)
2091 /* light status not supported on
2092 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2093 tp_features.light_status =
2094 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2096 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2097 str_supported(tp_features.light));
2099 return (tp_features.light)? 0 : 1;
2102 static int light_read(char *p)
2104 int len = 0;
2105 int status = 0;
2107 if (!tp_features.light) {
2108 len += sprintf(p + len, "status:\t\tnot supported\n");
2109 } else if (!tp_features.light_status) {
2110 len += sprintf(p + len, "status:\t\tunknown\n");
2111 len += sprintf(p + len, "commands:\ton, off\n");
2112 } else {
2113 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2114 return -EIO;
2115 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2116 len += sprintf(p + len, "commands:\ton, off\n");
2119 return len;
2122 static int light_write(char *buf)
2124 int cmos_cmd, lght_cmd;
2125 char *cmd;
2126 int success;
2128 if (!tp_features.light)
2129 return -ENODEV;
2131 while ((cmd = next_cmd(&buf))) {
2132 if (strlencmp(cmd, "on") == 0) {
2133 cmos_cmd = 0x0c;
2134 lght_cmd = 1;
2135 } else if (strlencmp(cmd, "off") == 0) {
2136 cmos_cmd = 0x0d;
2137 lght_cmd = 0;
2138 } else
2139 return -EINVAL;
2141 success = cmos_handle ?
2142 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2143 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2144 if (!success)
2145 return -EIO;
2148 return 0;
2151 static struct ibm_struct light_driver_data = {
2152 .name = "light",
2153 .read = light_read,
2154 .write = light_write,
2157 /*************************************************************************
2158 * Dock subdriver
2161 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2163 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2164 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2165 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2166 "\\_SB.PCI.ISA.SLCE", /* 570 */
2167 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2169 /* don't list other alternatives as we install a notify handler on the 570 */
2170 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2172 static const struct acpi_device_id ibm_pci_device_ids[] = {
2173 {PCI_ROOT_HID_STRING, 0},
2174 {"", 0},
2177 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2179 .notify = dock_notify,
2180 .handle = &dock_handle,
2181 .type = ACPI_SYSTEM_NOTIFY,
2184 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2185 * We just use it to get notifications of dock hotplug
2186 * in very old thinkpads */
2187 .hid = ibm_pci_device_ids,
2188 .notify = dock_notify,
2189 .handle = &pci_handle,
2190 .type = ACPI_SYSTEM_NOTIFY,
2194 static struct ibm_struct dock_driver_data[2] = {
2196 .name = "dock",
2197 .read = dock_read,
2198 .write = dock_write,
2199 .acpi = &ibm_dock_acpidriver[0],
2202 .name = "dock",
2203 .acpi = &ibm_dock_acpidriver[1],
2207 #define dock_docked() (_sta(dock_handle) & 1)
2209 static int __init dock_init(struct ibm_init_struct *iibm)
2211 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2213 IBM_ACPIHANDLE_INIT(dock);
2215 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2216 str_supported(dock_handle != NULL));
2218 return (dock_handle)? 0 : 1;
2221 static int __init dock_init2(struct ibm_init_struct *iibm)
2223 int dock2_needed;
2225 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2227 if (dock_driver_data[0].flags.acpi_driver_registered &&
2228 dock_driver_data[0].flags.acpi_notify_installed) {
2229 IBM_ACPIHANDLE_INIT(pci);
2230 dock2_needed = (pci_handle != NULL);
2231 vdbg_printk(TPACPI_DBG_INIT,
2232 "dock PCI handler for the TP 570 is %s\n",
2233 str_supported(dock2_needed));
2234 } else {
2235 vdbg_printk(TPACPI_DBG_INIT,
2236 "dock subdriver part 2 not required\n");
2237 dock2_needed = 0;
2240 return (dock2_needed)? 0 : 1;
2243 static void dock_notify(struct ibm_struct *ibm, u32 event)
2245 int docked = dock_docked();
2246 int pci = ibm->acpi->hid && ibm->acpi->device &&
2247 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
2248 int data;
2250 if (event == 1 && !pci) /* 570 */
2251 data = 1; /* button */
2252 else if (event == 1 && pci) /* 570 */
2253 data = 3; /* dock */
2254 else if (event == 3 && docked)
2255 data = 1; /* button */
2256 else if (event == 3 && !docked)
2257 data = 2; /* undock */
2258 else if (event == 0 && docked)
2259 data = 3; /* dock */
2260 else {
2261 printk(IBM_ERR "unknown dock event %d, status %d\n",
2262 event, _sta(dock_handle));
2263 data = 0; /* unknown */
2265 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
2266 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2267 ibm->acpi->device->dev.bus_id,
2268 event, data);
2271 static int dock_read(char *p)
2273 int len = 0;
2274 int docked = dock_docked();
2276 if (!dock_handle)
2277 len += sprintf(p + len, "status:\t\tnot supported\n");
2278 else if (!docked)
2279 len += sprintf(p + len, "status:\t\tundocked\n");
2280 else {
2281 len += sprintf(p + len, "status:\t\tdocked\n");
2282 len += sprintf(p + len, "commands:\tdock, undock\n");
2285 return len;
2288 static int dock_write(char *buf)
2290 char *cmd;
2292 if (!dock_docked())
2293 return -ENODEV;
2295 while ((cmd = next_cmd(&buf))) {
2296 if (strlencmp(cmd, "undock") == 0) {
2297 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2298 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2299 return -EIO;
2300 } else if (strlencmp(cmd, "dock") == 0) {
2301 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2302 return -EIO;
2303 } else
2304 return -EINVAL;
2307 return 0;
2310 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2312 /*************************************************************************
2313 * Bay subdriver
2316 #ifdef CONFIG_THINKPAD_ACPI_BAY
2317 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2318 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2319 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2320 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2321 ); /* A21e, R30, R31 */
2322 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2323 "_EJ0", /* all others */
2324 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2325 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2326 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2327 ); /* all others */
2328 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2329 "_EJ0", /* 770x */
2330 ); /* all others */
2332 static int __init bay_init(struct ibm_init_struct *iibm)
2334 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2336 IBM_ACPIHANDLE_INIT(bay);
2337 if (bay_handle)
2338 IBM_ACPIHANDLE_INIT(bay_ej);
2339 IBM_ACPIHANDLE_INIT(bay2);
2340 if (bay2_handle)
2341 IBM_ACPIHANDLE_INIT(bay2_ej);
2343 tp_features.bay_status = bay_handle &&
2344 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2345 tp_features.bay_status2 = bay2_handle &&
2346 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2348 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2349 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2350 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2351 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2353 vdbg_printk(TPACPI_DBG_INIT,
2354 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2355 str_supported(tp_features.bay_status),
2356 str_supported(tp_features.bay_eject),
2357 str_supported(tp_features.bay_status2),
2358 str_supported(tp_features.bay_eject2));
2360 return (tp_features.bay_status || tp_features.bay_eject ||
2361 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2364 static void bay_notify(struct ibm_struct *ibm, u32 event)
2366 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
2367 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2368 ibm->acpi->device->dev.bus_id,
2369 event, 0);
2372 #define bay_occupied(b) (_sta(b##_handle) & 1)
2374 static int bay_read(char *p)
2376 int len = 0;
2377 int occupied = bay_occupied(bay);
2378 int occupied2 = bay_occupied(bay2);
2379 int eject, eject2;
2381 len += sprintf(p + len, "status:\t\t%s\n",
2382 tp_features.bay_status ?
2383 (occupied ? "occupied" : "unoccupied") :
2384 "not supported");
2385 if (tp_features.bay_status2)
2386 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2387 "occupied" : "unoccupied");
2389 eject = tp_features.bay_eject && occupied;
2390 eject2 = tp_features.bay_eject2 && occupied2;
2392 if (eject && eject2)
2393 len += sprintf(p + len, "commands:\teject, eject2\n");
2394 else if (eject)
2395 len += sprintf(p + len, "commands:\teject\n");
2396 else if (eject2)
2397 len += sprintf(p + len, "commands:\teject2\n");
2399 return len;
2402 static int bay_write(char *buf)
2404 char *cmd;
2406 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2407 return -ENODEV;
2409 while ((cmd = next_cmd(&buf))) {
2410 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2411 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2412 return -EIO;
2413 } else if (tp_features.bay_eject2 &&
2414 strlencmp(cmd, "eject2") == 0) {
2415 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2416 return -EIO;
2417 } else
2418 return -EINVAL;
2421 return 0;
2424 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2425 .notify = bay_notify,
2426 .handle = &bay_handle,
2427 .type = ACPI_SYSTEM_NOTIFY,
2430 static struct ibm_struct bay_driver_data = {
2431 .name = "bay",
2432 .read = bay_read,
2433 .write = bay_write,
2434 .acpi = &ibm_bay_acpidriver,
2437 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2439 /*************************************************************************
2440 * CMOS subdriver
2443 /* sysfs cmos_command -------------------------------------------------- */
2444 static ssize_t cmos_command_store(struct device *dev,
2445 struct device_attribute *attr,
2446 const char *buf, size_t count)
2448 unsigned long cmos_cmd;
2449 int res;
2451 if (parse_strtoul(buf, 21, &cmos_cmd))
2452 return -EINVAL;
2454 res = issue_thinkpad_cmos_command(cmos_cmd);
2455 return (res)? res : count;
2458 static struct device_attribute dev_attr_cmos_command =
2459 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2461 /* --------------------------------------------------------------------- */
2463 static int __init cmos_init(struct ibm_init_struct *iibm)
2465 int res;
2467 vdbg_printk(TPACPI_DBG_INIT,
2468 "initializing cmos commands subdriver\n");
2470 IBM_ACPIHANDLE_INIT(cmos);
2472 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2473 str_supported(cmos_handle != NULL));
2475 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2476 if (res)
2477 return res;
2479 return (cmos_handle)? 0 : 1;
2482 static void cmos_exit(void)
2484 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2487 static int cmos_read(char *p)
2489 int len = 0;
2491 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2492 R30, R31, T20-22, X20-21 */
2493 if (!cmos_handle)
2494 len += sprintf(p + len, "status:\t\tnot supported\n");
2495 else {
2496 len += sprintf(p + len, "status:\t\tsupported\n");
2497 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2500 return len;
2503 static int cmos_write(char *buf)
2505 char *cmd;
2506 int cmos_cmd, res;
2508 while ((cmd = next_cmd(&buf))) {
2509 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2510 cmos_cmd >= 0 && cmos_cmd <= 21) {
2511 /* cmos_cmd set */
2512 } else
2513 return -EINVAL;
2515 res = issue_thinkpad_cmos_command(cmos_cmd);
2516 if (res)
2517 return res;
2520 return 0;
2523 static struct ibm_struct cmos_driver_data = {
2524 .name = "cmos",
2525 .read = cmos_read,
2526 .write = cmos_write,
2527 .exit = cmos_exit,
2530 /*************************************************************************
2531 * LED subdriver
2534 static enum led_access_mode led_supported;
2536 IBM_HANDLE(led, ec, "SLED", /* 570 */
2537 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2538 "LED", /* all others */
2539 ); /* R30, R31 */
2541 static int __init led_init(struct ibm_init_struct *iibm)
2543 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2545 IBM_ACPIHANDLE_INIT(led);
2547 if (!led_handle)
2548 /* led not supported on R30, R31 */
2549 led_supported = TPACPI_LED_NONE;
2550 else if (strlencmp(led_path, "SLED") == 0)
2551 /* 570 */
2552 led_supported = TPACPI_LED_570;
2553 else if (strlencmp(led_path, "SYSL") == 0)
2554 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2555 led_supported = TPACPI_LED_OLD;
2556 else
2557 /* all others */
2558 led_supported = TPACPI_LED_NEW;
2560 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2561 str_supported(led_supported), led_supported);
2563 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2566 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2568 static int led_read(char *p)
2570 int len = 0;
2572 if (!led_supported) {
2573 len += sprintf(p + len, "status:\t\tnot supported\n");
2574 return len;
2576 len += sprintf(p + len, "status:\t\tsupported\n");
2578 if (led_supported == TPACPI_LED_570) {
2579 /* 570 */
2580 int i, status;
2581 for (i = 0; i < 8; i++) {
2582 if (!acpi_evalf(ec_handle,
2583 &status, "GLED", "dd", 1 << i))
2584 return -EIO;
2585 len += sprintf(p + len, "%d:\t\t%s\n",
2586 i, led_status(status));
2590 len += sprintf(p + len, "commands:\t"
2591 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2593 return len;
2596 /* off, on, blink */
2597 static const int led_sled_arg1[] = { 0, 1, 3 };
2598 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2599 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2600 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2602 static int led_write(char *buf)
2604 char *cmd;
2605 int led, ind, ret;
2607 if (!led_supported)
2608 return -ENODEV;
2610 while ((cmd = next_cmd(&buf))) {
2611 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2612 return -EINVAL;
2614 if (strstr(cmd, "off")) {
2615 ind = 0;
2616 } else if (strstr(cmd, "on")) {
2617 ind = 1;
2618 } else if (strstr(cmd, "blink")) {
2619 ind = 2;
2620 } else
2621 return -EINVAL;
2623 if (led_supported == TPACPI_LED_570) {
2624 /* 570 */
2625 led = 1 << led;
2626 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2627 led, led_sled_arg1[ind]))
2628 return -EIO;
2629 } else if (led_supported == TPACPI_LED_OLD) {
2630 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2631 led = 1 << led;
2632 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2633 if (ret >= 0)
2634 ret =
2635 ec_write(TPACPI_LED_EC_HLBL,
2636 led * led_exp_hlbl[ind]);
2637 if (ret >= 0)
2638 ret =
2639 ec_write(TPACPI_LED_EC_HLCL,
2640 led * led_exp_hlcl[ind]);
2641 if (ret < 0)
2642 return ret;
2643 } else {
2644 /* all others */
2645 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2646 led, led_led_arg1[ind]))
2647 return -EIO;
2651 return 0;
2654 static struct ibm_struct led_driver_data = {
2655 .name = "led",
2656 .read = led_read,
2657 .write = led_write,
2660 /*************************************************************************
2661 * Beep subdriver
2664 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2666 static int __init beep_init(struct ibm_init_struct *iibm)
2668 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2670 IBM_ACPIHANDLE_INIT(beep);
2672 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2673 str_supported(beep_handle != NULL));
2675 return (beep_handle)? 0 : 1;
2678 static int beep_read(char *p)
2680 int len = 0;
2682 if (!beep_handle)
2683 len += sprintf(p + len, "status:\t\tnot supported\n");
2684 else {
2685 len += sprintf(p + len, "status:\t\tsupported\n");
2686 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2689 return len;
2692 static int beep_write(char *buf)
2694 char *cmd;
2695 int beep_cmd;
2697 if (!beep_handle)
2698 return -ENODEV;
2700 while ((cmd = next_cmd(&buf))) {
2701 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2702 beep_cmd >= 0 && beep_cmd <= 17) {
2703 /* beep_cmd set */
2704 } else
2705 return -EINVAL;
2706 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2707 return -EIO;
2710 return 0;
2713 static struct ibm_struct beep_driver_data = {
2714 .name = "beep",
2715 .read = beep_read,
2716 .write = beep_write,
2719 /*************************************************************************
2720 * Thermal subdriver
2723 static enum thermal_access_mode thermal_read_mode;
2725 /* sysfs temp##_input -------------------------------------------------- */
2727 static ssize_t thermal_temp_input_show(struct device *dev,
2728 struct device_attribute *attr,
2729 char *buf)
2731 struct sensor_device_attribute *sensor_attr =
2732 to_sensor_dev_attr(attr);
2733 int idx = sensor_attr->index;
2734 s32 value;
2735 int res;
2737 res = thermal_get_sensor(idx, &value);
2738 if (res)
2739 return res;
2740 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2741 return -ENXIO;
2743 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2746 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2747 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2749 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2750 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2751 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2752 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2753 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2754 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2755 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2756 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2757 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2758 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2759 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2760 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2761 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2762 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2763 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2764 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2765 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2768 #define THERMAL_ATTRS(X) \
2769 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2771 static struct attribute *thermal_temp_input_attr[] = {
2772 THERMAL_ATTRS(8),
2773 THERMAL_ATTRS(9),
2774 THERMAL_ATTRS(10),
2775 THERMAL_ATTRS(11),
2776 THERMAL_ATTRS(12),
2777 THERMAL_ATTRS(13),
2778 THERMAL_ATTRS(14),
2779 THERMAL_ATTRS(15),
2780 THERMAL_ATTRS(0),
2781 THERMAL_ATTRS(1),
2782 THERMAL_ATTRS(2),
2783 THERMAL_ATTRS(3),
2784 THERMAL_ATTRS(4),
2785 THERMAL_ATTRS(5),
2786 THERMAL_ATTRS(6),
2787 THERMAL_ATTRS(7),
2788 NULL
2791 static const struct attribute_group thermal_temp_input16_group = {
2792 .attrs = thermal_temp_input_attr
2795 static const struct attribute_group thermal_temp_input8_group = {
2796 .attrs = &thermal_temp_input_attr[8]
2799 #undef THERMAL_SENSOR_ATTR_TEMP
2800 #undef THERMAL_ATTRS
2802 /* --------------------------------------------------------------------- */
2804 static int __init thermal_init(struct ibm_init_struct *iibm)
2806 u8 t, ta1, ta2;
2807 int i;
2808 int acpi_tmp7;
2809 int res;
2811 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2813 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2815 if (thinkpad_id.ec_model) {
2817 * Direct EC access mode: sensors at registers
2818 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2819 * non-implemented, thermal sensors return 0x80 when
2820 * not available
2823 ta1 = ta2 = 0;
2824 for (i = 0; i < 8; i++) {
2825 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2826 ta1 |= t;
2827 } else {
2828 ta1 = 0;
2829 break;
2831 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2832 ta2 |= t;
2833 } else {
2834 ta1 = 0;
2835 break;
2838 if (ta1 == 0) {
2839 /* This is sheer paranoia, but we handle it anyway */
2840 if (acpi_tmp7) {
2841 printk(IBM_ERR
2842 "ThinkPad ACPI EC access misbehaving, "
2843 "falling back to ACPI TMPx access mode\n");
2844 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2845 } else {
2846 printk(IBM_ERR
2847 "ThinkPad ACPI EC access misbehaving, "
2848 "disabling thermal sensors access\n");
2849 thermal_read_mode = TPACPI_THERMAL_NONE;
2851 } else {
2852 thermal_read_mode =
2853 (ta2 != 0) ?
2854 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2856 } else if (acpi_tmp7) {
2857 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2858 /* 600e/x, 770e, 770x */
2859 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2860 } else {
2861 /* Standard ACPI TMPx access, max 8 sensors */
2862 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2864 } else {
2865 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2866 thermal_read_mode = TPACPI_THERMAL_NONE;
2869 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2870 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2871 thermal_read_mode);
2873 switch(thermal_read_mode) {
2874 case TPACPI_THERMAL_TPEC_16:
2875 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2876 &thermal_temp_input16_group);
2877 if (res)
2878 return res;
2879 break;
2880 case TPACPI_THERMAL_TPEC_8:
2881 case TPACPI_THERMAL_ACPI_TMP07:
2882 case TPACPI_THERMAL_ACPI_UPDT:
2883 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2884 &thermal_temp_input8_group);
2885 if (res)
2886 return res;
2887 break;
2888 case TPACPI_THERMAL_NONE:
2889 default:
2890 return 1;
2893 return 0;
2896 static void thermal_exit(void)
2898 switch(thermal_read_mode) {
2899 case TPACPI_THERMAL_TPEC_16:
2900 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2901 &thermal_temp_input16_group);
2902 break;
2903 case TPACPI_THERMAL_TPEC_8:
2904 case TPACPI_THERMAL_ACPI_TMP07:
2905 case TPACPI_THERMAL_ACPI_UPDT:
2906 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2907 &thermal_temp_input16_group);
2908 break;
2909 case TPACPI_THERMAL_NONE:
2910 default:
2911 break;
2915 /* idx is zero-based */
2916 static int thermal_get_sensor(int idx, s32 *value)
2918 int t;
2919 s8 tmp;
2920 char tmpi[5];
2922 t = TP_EC_THERMAL_TMP0;
2924 switch (thermal_read_mode) {
2925 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2926 case TPACPI_THERMAL_TPEC_16:
2927 if (idx >= 8 && idx <= 15) {
2928 t = TP_EC_THERMAL_TMP8;
2929 idx -= 8;
2931 /* fallthrough */
2932 #endif
2933 case TPACPI_THERMAL_TPEC_8:
2934 if (idx <= 7) {
2935 if (!acpi_ec_read(t + idx, &tmp))
2936 return -EIO;
2937 *value = tmp * 1000;
2938 return 0;
2940 break;
2942 case TPACPI_THERMAL_ACPI_UPDT:
2943 if (idx <= 7) {
2944 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2945 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2946 return -EIO;
2947 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2948 return -EIO;
2949 *value = (t - 2732) * 100;
2950 return 0;
2952 break;
2954 case TPACPI_THERMAL_ACPI_TMP07:
2955 if (idx <= 7) {
2956 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2957 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2958 return -EIO;
2959 if (t > 127 || t < -127)
2960 t = TP_EC_THERMAL_TMP_NA;
2961 *value = t * 1000;
2962 return 0;
2964 break;
2966 case TPACPI_THERMAL_NONE:
2967 default:
2968 return -ENOSYS;
2971 return -EINVAL;
2974 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2976 int res, i;
2977 int n;
2979 n = 8;
2980 i = 0;
2982 if (!s)
2983 return -EINVAL;
2985 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2986 n = 16;
2988 for(i = 0 ; i < n; i++) {
2989 res = thermal_get_sensor(i, &s->temp[i]);
2990 if (res)
2991 return res;
2994 return n;
2997 static int thermal_read(char *p)
2999 int len = 0;
3000 int n, i;
3001 struct ibm_thermal_sensors_struct t;
3003 n = thermal_get_sensors(&t);
3004 if (unlikely(n < 0))
3005 return n;
3007 len += sprintf(p + len, "temperatures:\t");
3009 if (n > 0) {
3010 for (i = 0; i < (n - 1); i++)
3011 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3012 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3013 } else
3014 len += sprintf(p + len, "not supported\n");
3016 return len;
3019 static struct ibm_struct thermal_driver_data = {
3020 .name = "thermal",
3021 .read = thermal_read,
3022 .exit = thermal_exit,
3025 /*************************************************************************
3026 * EC Dump subdriver
3029 static u8 ecdump_regs[256];
3031 static int ecdump_read(char *p)
3033 int len = 0;
3034 int i, j;
3035 u8 v;
3037 len += sprintf(p + len, "EC "
3038 " +00 +01 +02 +03 +04 +05 +06 +07"
3039 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3040 for (i = 0; i < 256; i += 16) {
3041 len += sprintf(p + len, "EC 0x%02x:", i);
3042 for (j = 0; j < 16; j++) {
3043 if (!acpi_ec_read(i + j, &v))
3044 break;
3045 if (v != ecdump_regs[i + j])
3046 len += sprintf(p + len, " *%02x", v);
3047 else
3048 len += sprintf(p + len, " %02x", v);
3049 ecdump_regs[i + j] = v;
3051 len += sprintf(p + len, "\n");
3052 if (j != 16)
3053 break;
3056 /* These are way too dangerous to advertise openly... */
3057 #if 0
3058 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3059 " (<offset> is 00-ff, <value> is 00-ff)\n");
3060 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3061 " (<offset> is 00-ff, <value> is 0-255)\n");
3062 #endif
3063 return len;
3066 static int ecdump_write(char *buf)
3068 char *cmd;
3069 int i, v;
3071 while ((cmd = next_cmd(&buf))) {
3072 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3073 /* i and v set */
3074 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3075 /* i and v set */
3076 } else
3077 return -EINVAL;
3078 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3079 if (!acpi_ec_write(i, v))
3080 return -EIO;
3081 } else
3082 return -EINVAL;
3085 return 0;
3088 static struct ibm_struct ecdump_driver_data = {
3089 .name = "ecdump",
3090 .read = ecdump_read,
3091 .write = ecdump_write,
3092 .flags.experimental = 1,
3095 /*************************************************************************
3096 * Backlight/brightness subdriver
3099 static struct backlight_device *ibm_backlight_device;
3101 static struct backlight_ops ibm_backlight_data = {
3102 .get_brightness = brightness_get,
3103 .update_status = brightness_update_status,
3106 static struct mutex brightness_mutex;
3108 static int __init brightness_init(struct ibm_init_struct *iibm)
3110 int b;
3112 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3114 mutex_init(&brightness_mutex);
3116 if (!brightness_mode) {
3117 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3118 brightness_mode = 2;
3119 else
3120 brightness_mode = 3;
3122 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3123 brightness_mode);
3126 if (brightness_mode > 3)
3127 return -EINVAL;
3129 b = brightness_get(NULL);
3130 if (b < 0)
3131 return 1;
3133 ibm_backlight_device = backlight_device_register(
3134 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3135 &ibm_backlight_data);
3136 if (IS_ERR(ibm_backlight_device)) {
3137 printk(IBM_ERR "Could not register backlight device\n");
3138 return PTR_ERR(ibm_backlight_device);
3140 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
3142 ibm_backlight_device->props.max_brightness = 7;
3143 ibm_backlight_device->props.brightness = b;
3144 backlight_update_status(ibm_backlight_device);
3146 return 0;
3149 static void brightness_exit(void)
3151 if (ibm_backlight_device) {
3152 vdbg_printk(TPACPI_DBG_EXIT,
3153 "calling backlight_device_unregister()\n");
3154 backlight_device_unregister(ibm_backlight_device);
3155 ibm_backlight_device = NULL;
3159 static int brightness_update_status(struct backlight_device *bd)
3161 return brightness_set(
3162 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3163 bd->props.power == FB_BLANK_UNBLANK) ?
3164 bd->props.brightness : 0);
3168 * ThinkPads can read brightness from two places: EC 0x31, or
3169 * CMOS NVRAM byte 0x5E, bits 0-3.
3171 static int brightness_get(struct backlight_device *bd)
3173 u8 lec = 0, lcmos = 0, level = 0;
3175 if (brightness_mode & 1) {
3176 if (!acpi_ec_read(brightness_offset, &lec))
3177 return -EIO;
3178 lec &= 7;
3179 level = lec;
3181 if (brightness_mode & 2) {
3182 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3183 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3184 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3185 level = lcmos;
3188 if (brightness_mode == 3 && lec != lcmos) {
3189 printk(IBM_ERR
3190 "CMOS NVRAM (%u) and EC (%u) do not agree "
3191 "on display brightness level\n",
3192 (unsigned int) lcmos,
3193 (unsigned int) lec);
3194 return -EIO;
3197 return level;
3200 static int brightness_set(int value)
3202 int cmos_cmd, inc, i, res;
3203 int current_value;
3205 if (value > 7)
3206 return -EINVAL;
3208 res = mutex_lock_interruptible(&brightness_mutex);
3209 if (res < 0)
3210 return res;
3212 current_value = brightness_get(NULL);
3213 if (current_value < 0) {
3214 res = current_value;
3215 goto errout;
3218 cmos_cmd = value > current_value ?
3219 TP_CMOS_BRIGHTNESS_UP :
3220 TP_CMOS_BRIGHTNESS_DOWN;
3221 inc = value > current_value ? 1 : -1;
3223 res = 0;
3224 for (i = current_value; i != value; i += inc) {
3225 if ((brightness_mode & 2) &&
3226 issue_thinkpad_cmos_command(cmos_cmd)) {
3227 res = -EIO;
3228 goto errout;
3230 if ((brightness_mode & 1) &&
3231 !acpi_ec_write(brightness_offset, i + inc)) {
3232 res = -EIO;
3233 goto errout;;
3237 errout:
3238 mutex_unlock(&brightness_mutex);
3239 return res;
3242 static int brightness_read(char *p)
3244 int len = 0;
3245 int level;
3247 if ((level = brightness_get(NULL)) < 0) {
3248 len += sprintf(p + len, "level:\t\tunreadable\n");
3249 } else {
3250 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3251 len += sprintf(p + len, "commands:\tup, down\n");
3252 len += sprintf(p + len, "commands:\tlevel <level>"
3253 " (<level> is 0-7)\n");
3256 return len;
3259 static int brightness_write(char *buf)
3261 int level;
3262 int new_level;
3263 char *cmd;
3265 while ((cmd = next_cmd(&buf))) {
3266 if ((level = brightness_get(NULL)) < 0)
3267 return level;
3268 level &= 7;
3270 if (strlencmp(cmd, "up") == 0) {
3271 new_level = level == 7 ? 7 : level + 1;
3272 } else if (strlencmp(cmd, "down") == 0) {
3273 new_level = level == 0 ? 0 : level - 1;
3274 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3275 new_level >= 0 && new_level <= 7) {
3276 /* new_level set */
3277 } else
3278 return -EINVAL;
3280 brightness_set(new_level);
3283 return 0;
3286 static struct ibm_struct brightness_driver_data = {
3287 .name = "brightness",
3288 .read = brightness_read,
3289 .write = brightness_write,
3290 .exit = brightness_exit,
3293 /*************************************************************************
3294 * Volume subdriver
3297 static int volume_read(char *p)
3299 int len = 0;
3300 u8 level;
3302 if (!acpi_ec_read(volume_offset, &level)) {
3303 len += sprintf(p + len, "level:\t\tunreadable\n");
3304 } else {
3305 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3306 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3307 len += sprintf(p + len, "commands:\tup, down, mute\n");
3308 len += sprintf(p + len, "commands:\tlevel <level>"
3309 " (<level> is 0-15)\n");
3312 return len;
3315 static int volume_write(char *buf)
3317 int cmos_cmd, inc, i;
3318 u8 level, mute;
3319 int new_level, new_mute;
3320 char *cmd;
3322 while ((cmd = next_cmd(&buf))) {
3323 if (!acpi_ec_read(volume_offset, &level))
3324 return -EIO;
3325 new_mute = mute = level & 0x40;
3326 new_level = level = level & 0xf;
3328 if (strlencmp(cmd, "up") == 0) {
3329 if (mute)
3330 new_mute = 0;
3331 else
3332 new_level = level == 15 ? 15 : level + 1;
3333 } else if (strlencmp(cmd, "down") == 0) {
3334 if (mute)
3335 new_mute = 0;
3336 else
3337 new_level = level == 0 ? 0 : level - 1;
3338 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3339 new_level >= 0 && new_level <= 15) {
3340 /* new_level set */
3341 } else if (strlencmp(cmd, "mute") == 0) {
3342 new_mute = 0x40;
3343 } else
3344 return -EINVAL;
3346 if (new_level != level) { /* mute doesn't change */
3347 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3348 inc = new_level > level ? 1 : -1;
3350 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3351 !acpi_ec_write(volume_offset, level)))
3352 return -EIO;
3354 for (i = level; i != new_level; i += inc)
3355 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3356 !acpi_ec_write(volume_offset, i + inc))
3357 return -EIO;
3359 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3360 !acpi_ec_write(volume_offset,
3361 new_level + mute)))
3362 return -EIO;
3365 if (new_mute != mute) { /* level doesn't change */
3366 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3368 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3369 !acpi_ec_write(volume_offset, level + new_mute))
3370 return -EIO;
3374 return 0;
3377 static struct ibm_struct volume_driver_data = {
3378 .name = "volume",
3379 .read = volume_read,
3380 .write = volume_write,
3383 /*************************************************************************
3384 * Fan subdriver
3388 * FAN ACCESS MODES
3390 * TPACPI_FAN_RD_ACPI_GFAN:
3391 * ACPI GFAN method: returns fan level
3393 * see TPACPI_FAN_WR_ACPI_SFAN
3394 * EC 0x2f (HFSP) not available if GFAN exists
3396 * TPACPI_FAN_WR_ACPI_SFAN:
3397 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3399 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3400 * it for writing.
3402 * TPACPI_FAN_WR_TPEC:
3403 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3404 * Supported on almost all ThinkPads
3406 * Fan speed changes of any sort (including those caused by the
3407 * disengaged mode) are usually done slowly by the firmware as the
3408 * maximum ammount of fan duty cycle change per second seems to be
3409 * limited.
3411 * Reading is not available if GFAN exists.
3412 * Writing is not available if SFAN exists.
3414 * Bits
3415 * 7 automatic mode engaged;
3416 * (default operation mode of the ThinkPad)
3417 * fan level is ignored in this mode.
3418 * 6 full speed mode (takes precedence over bit 7);
3419 * not available on all thinkpads. May disable
3420 * the tachometer while the fan controller ramps up
3421 * the speed (which can take up to a few *minutes*).
3422 * Speeds up fan to 100% duty-cycle, which is far above
3423 * the standard RPM levels. It is not impossible that
3424 * it could cause hardware damage.
3425 * 5-3 unused in some models. Extra bits for fan level
3426 * in others, but still useless as all values above
3427 * 7 map to the same speed as level 7 in these models.
3428 * 2-0 fan level (0..7 usually)
3429 * 0x00 = stop
3430 * 0x07 = max (set when temperatures critical)
3431 * Some ThinkPads may have other levels, see
3432 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3434 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3435 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3436 * does so, its initial value is meaningless (0x07).
3438 * For firmware bugs, refer to:
3439 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3441 * ----
3443 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3444 * Main fan tachometer reading (in RPM)
3446 * This register is present on all ThinkPads with a new-style EC, and
3447 * it is known not to be present on the A21m/e, and T22, as there is
3448 * something else in offset 0x84 according to the ACPI DSDT. Other
3449 * ThinkPads from this same time period (and earlier) probably lack the
3450 * tachometer as well.
3452 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3453 * was never fixed by IBM to report the EC firmware version string
3454 * probably support the tachometer (like the early X models), so
3455 * detecting it is quite hard. We need more data to know for sure.
3457 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3458 * might result.
3460 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3461 * mode.
3463 * For firmware bugs, refer to:
3464 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3466 * TPACPI_FAN_WR_ACPI_FANS:
3467 * ThinkPad X31, X40, X41. Not available in the X60.
3469 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3470 * high speed. ACPI DSDT seems to map these three speeds to levels
3471 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3472 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3474 * The speeds are stored on handles
3475 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3477 * There are three default speed sets, acessible as handles:
3478 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3480 * ACPI DSDT switches which set is in use depending on various
3481 * factors.
3483 * TPACPI_FAN_WR_TPEC is also available and should be used to
3484 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3485 * but the ACPI tables just mention level 7.
3488 static enum fan_status_access_mode fan_status_access_mode;
3489 static enum fan_control_access_mode fan_control_access_mode;
3490 static enum fan_control_commands fan_control_commands;
3492 static u8 fan_control_initial_status;
3493 static u8 fan_control_desired_level;
3495 static void fan_watchdog_fire(struct work_struct *ignored);
3496 static int fan_watchdog_maxinterval;
3497 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3499 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3500 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3501 "\\FSPD", /* 600e/x, 770e, 770x */
3502 ); /* all others */
3503 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3504 "JFNS", /* 770x-JL */
3505 ); /* all others */
3508 * SYSFS fan layout: hwmon compatible (device)
3510 * pwm*_enable:
3511 * 0: "disengaged" mode
3512 * 1: manual mode
3513 * 2: native EC "auto" mode (recommended, hardware default)
3515 * pwm*: set speed in manual mode, ignored otherwise.
3516 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3517 * interpolation.
3519 * fan*_input: tachometer reading, RPM
3522 * SYSFS fan layout: extensions
3524 * fan_watchdog (driver):
3525 * fan watchdog interval in seconds, 0 disables (default), max 120
3528 /* sysfs fan pwm1_enable ----------------------------------------------- */
3529 static ssize_t fan_pwm1_enable_show(struct device *dev,
3530 struct device_attribute *attr,
3531 char *buf)
3533 int res, mode;
3534 u8 status;
3536 res = fan_get_status_safe(&status);
3537 if (res)
3538 return res;
3540 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3541 if (status != fan_control_initial_status) {
3542 tp_features.fan_ctrl_status_undef = 0;
3543 } else {
3544 /* Return most likely status. In fact, it
3545 * might be the only possible status */
3546 status = TP_EC_FAN_AUTO;
3550 if (status & TP_EC_FAN_FULLSPEED) {
3551 mode = 0;
3552 } else if (status & TP_EC_FAN_AUTO) {
3553 mode = 2;
3554 } else
3555 mode = 1;
3557 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3560 static ssize_t fan_pwm1_enable_store(struct device *dev,
3561 struct device_attribute *attr,
3562 const char *buf, size_t count)
3564 unsigned long t;
3565 int res, level;
3567 if (parse_strtoul(buf, 2, &t))
3568 return -EINVAL;
3570 switch (t) {
3571 case 0:
3572 level = TP_EC_FAN_FULLSPEED;
3573 break;
3574 case 1:
3575 level = TPACPI_FAN_LAST_LEVEL;
3576 break;
3577 case 2:
3578 level = TP_EC_FAN_AUTO;
3579 break;
3580 case 3:
3581 /* reserved for software-controlled auto mode */
3582 return -ENOSYS;
3583 default:
3584 return -EINVAL;
3587 res = fan_set_level_safe(level);
3588 if (res == -ENXIO)
3589 return -EINVAL;
3590 else if (res < 0)
3591 return res;
3593 fan_watchdog_reset();
3595 return count;
3598 static struct device_attribute dev_attr_fan_pwm1_enable =
3599 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3600 fan_pwm1_enable_show, fan_pwm1_enable_store);
3602 /* sysfs fan pwm1 ------------------------------------------------------ */
3603 static ssize_t fan_pwm1_show(struct device *dev,
3604 struct device_attribute *attr,
3605 char *buf)
3607 int res;
3608 u8 status;
3610 res = fan_get_status_safe(&status);
3611 if (res)
3612 return res;
3614 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3615 if (status != fan_control_initial_status) {
3616 tp_features.fan_ctrl_status_undef = 0;
3617 } else {
3618 status = TP_EC_FAN_AUTO;
3622 if ((status &
3623 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3624 status = fan_control_desired_level;
3626 if (status > 7)
3627 status = 7;
3629 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3632 static ssize_t fan_pwm1_store(struct device *dev,
3633 struct device_attribute *attr,
3634 const char *buf, size_t count)
3636 unsigned long s;
3637 int rc;
3638 u8 status, newlevel;
3640 if (parse_strtoul(buf, 255, &s))
3641 return -EINVAL;
3643 /* scale down from 0-255 to 0-7 */
3644 newlevel = (s >> 5) & 0x07;
3646 rc = mutex_lock_interruptible(&fan_mutex);
3647 if (rc < 0)
3648 return rc;
3650 rc = fan_get_status(&status);
3651 if (!rc && (status &
3652 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3653 rc = fan_set_level(newlevel);
3654 if (rc == -ENXIO)
3655 rc = -EINVAL;
3656 else if (!rc) {
3657 fan_update_desired_level(newlevel);
3658 fan_watchdog_reset();
3662 mutex_unlock(&fan_mutex);
3663 return (rc)? rc : count;
3666 static struct device_attribute dev_attr_fan_pwm1 =
3667 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3668 fan_pwm1_show, fan_pwm1_store);
3670 /* sysfs fan fan1_input ------------------------------------------------ */
3671 static ssize_t fan_fan1_input_show(struct device *dev,
3672 struct device_attribute *attr,
3673 char *buf)
3675 int res;
3676 unsigned int speed;
3678 res = fan_get_speed(&speed);
3679 if (res < 0)
3680 return res;
3682 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3685 static struct device_attribute dev_attr_fan_fan1_input =
3686 __ATTR(fan1_input, S_IRUGO,
3687 fan_fan1_input_show, NULL);
3689 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3690 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3691 char *buf)
3693 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3696 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3697 const char *buf, size_t count)
3699 unsigned long t;
3701 if (parse_strtoul(buf, 120, &t))
3702 return -EINVAL;
3704 if (!fan_control_allowed)
3705 return -EPERM;
3707 fan_watchdog_maxinterval = t;
3708 fan_watchdog_reset();
3710 return count;
3713 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3714 fan_fan_watchdog_show, fan_fan_watchdog_store);
3716 /* --------------------------------------------------------------------- */
3717 static struct attribute *fan_attributes[] = {
3718 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3719 &dev_attr_fan_fan1_input.attr,
3720 NULL
3723 static const struct attribute_group fan_attr_group = {
3724 .attrs = fan_attributes,
3727 static int __init fan_init(struct ibm_init_struct *iibm)
3729 int rc;
3731 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3733 mutex_init(&fan_mutex);
3734 fan_status_access_mode = TPACPI_FAN_NONE;
3735 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3736 fan_control_commands = 0;
3737 fan_watchdog_maxinterval = 0;
3738 tp_features.fan_ctrl_status_undef = 0;
3739 fan_control_desired_level = 7;
3741 IBM_ACPIHANDLE_INIT(fans);
3742 IBM_ACPIHANDLE_INIT(gfan);
3743 IBM_ACPIHANDLE_INIT(sfan);
3745 if (gfan_handle) {
3746 /* 570, 600e/x, 770e, 770x */
3747 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3748 } else {
3749 /* all other ThinkPads: note that even old-style
3750 * ThinkPad ECs supports the fan control register */
3751 if (likely(acpi_ec_read(fan_status_offset,
3752 &fan_control_initial_status))) {
3753 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3755 /* In some ThinkPads, neither the EC nor the ACPI
3756 * DSDT initialize the fan status, and it ends up
3757 * being set to 0x07 when it *could* be either
3758 * 0x07 or 0x80.
3760 * Enable for TP-1Y (T43), TP-78 (R51e),
3761 * TP-76 (R52), TP-70 (T43, R52), which are known
3762 * to be buggy. */
3763 if (fan_control_initial_status == 0x07) {
3764 switch (thinkpad_id.ec_model) {
3765 case 0x5931: /* TP-1Y */
3766 case 0x3837: /* TP-78 */
3767 case 0x3637: /* TP-76 */
3768 case 0x3037: /* TP-70 */
3769 printk(IBM_NOTICE
3770 "fan_init: initial fan status is "
3771 "unknown, assuming it is in auto "
3772 "mode\n");
3773 tp_features.fan_ctrl_status_undef = 1;
3777 } else {
3778 printk(IBM_ERR
3779 "ThinkPad ACPI EC access misbehaving, "
3780 "fan status and control unavailable\n");
3781 return 1;
3785 if (sfan_handle) {
3786 /* 570, 770x-JL */
3787 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3788 fan_control_commands |=
3789 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3790 } else {
3791 if (!gfan_handle) {
3792 /* gfan without sfan means no fan control */
3793 /* all other models implement TP EC 0x2f control */
3795 if (fans_handle) {
3796 /* X31, X40, X41 */
3797 fan_control_access_mode =
3798 TPACPI_FAN_WR_ACPI_FANS;
3799 fan_control_commands |=
3800 TPACPI_FAN_CMD_SPEED |
3801 TPACPI_FAN_CMD_LEVEL |
3802 TPACPI_FAN_CMD_ENABLE;
3803 } else {
3804 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3805 fan_control_commands |=
3806 TPACPI_FAN_CMD_LEVEL |
3807 TPACPI_FAN_CMD_ENABLE;
3812 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3813 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3814 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3815 fan_status_access_mode, fan_control_access_mode);
3817 /* fan control master switch */
3818 if (!fan_control_allowed) {
3819 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3820 fan_control_commands = 0;
3821 dbg_printk(TPACPI_DBG_INIT,
3822 "fan control features disabled by parameter\n");
3825 /* update fan_control_desired_level */
3826 if (fan_status_access_mode != TPACPI_FAN_NONE)
3827 fan_get_status_safe(NULL);
3829 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3830 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3831 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3832 &fan_attr_group);
3833 if (!(rc < 0))
3834 rc = driver_create_file(&tpacpi_pdriver.driver,
3835 &driver_attr_fan_watchdog);
3836 if (rc < 0)
3837 return rc;
3838 return 0;
3839 } else
3840 return 1;
3844 * Call with fan_mutex held
3846 static void fan_update_desired_level(u8 status)
3848 if ((status &
3849 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3850 if (status > 7)
3851 fan_control_desired_level = 7;
3852 else
3853 fan_control_desired_level = status;
3857 static int fan_get_status(u8 *status)
3859 u8 s;
3861 /* TODO:
3862 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3864 switch (fan_status_access_mode) {
3865 case TPACPI_FAN_RD_ACPI_GFAN:
3866 /* 570, 600e/x, 770e, 770x */
3868 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3869 return -EIO;
3871 if (likely(status))
3872 *status = s & 0x07;
3874 break;
3876 case TPACPI_FAN_RD_TPEC:
3877 /* all except 570, 600e/x, 770e, 770x */
3878 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3879 return -EIO;
3881 if (likely(status))
3882 *status = s;
3884 break;
3886 default:
3887 return -ENXIO;
3890 return 0;
3893 static int fan_get_status_safe(u8 *status)
3895 int rc;
3896 u8 s;
3898 rc = mutex_lock_interruptible(&fan_mutex);
3899 if (rc < 0)
3900 return rc;
3901 rc = fan_get_status(&s);
3902 if (!rc)
3903 fan_update_desired_level(s);
3904 mutex_unlock(&fan_mutex);
3906 if (status)
3907 *status = s;
3909 return rc;
3912 static void fan_exit(void)
3914 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3916 /* FIXME: can we really do this unconditionally? */
3917 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3918 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3920 cancel_delayed_work(&fan_watchdog_task);
3921 flush_scheduled_work();
3924 static int fan_get_speed(unsigned int *speed)
3926 u8 hi, lo;
3928 switch (fan_status_access_mode) {
3929 case TPACPI_FAN_RD_TPEC:
3930 /* all except 570, 600e/x, 770e, 770x */
3931 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3932 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3933 return -EIO;
3935 if (likely(speed))
3936 *speed = (hi << 8) | lo;
3938 break;
3940 default:
3941 return -ENXIO;
3944 return 0;
3947 static void fan_watchdog_fire(struct work_struct *ignored)
3949 int rc;
3951 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
3952 return;
3954 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3955 rc = fan_set_enable();
3956 if (rc < 0) {
3957 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3958 "will try again later...\n", -rc);
3959 /* reschedule for later */
3960 fan_watchdog_reset();
3964 static void fan_watchdog_reset(void)
3966 static int fan_watchdog_active;
3968 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3969 return;
3971 if (fan_watchdog_active)
3972 cancel_delayed_work(&fan_watchdog_task);
3974 if (fan_watchdog_maxinterval > 0 &&
3975 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
3976 fan_watchdog_active = 1;
3977 if (!schedule_delayed_work(&fan_watchdog_task,
3978 msecs_to_jiffies(fan_watchdog_maxinterval
3979 * 1000))) {
3980 printk(IBM_ERR "failed to schedule the fan watchdog, "
3981 "watchdog will not trigger\n");
3983 } else
3984 fan_watchdog_active = 0;
3987 static int fan_set_level(int level)
3989 if (!fan_control_allowed)
3990 return -EPERM;
3992 switch (fan_control_access_mode) {
3993 case TPACPI_FAN_WR_ACPI_SFAN:
3994 if (level >= 0 && level <= 7) {
3995 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3996 return -EIO;
3997 } else
3998 return -EINVAL;
3999 break;
4001 case TPACPI_FAN_WR_ACPI_FANS:
4002 case TPACPI_FAN_WR_TPEC:
4003 if ((level != TP_EC_FAN_AUTO) &&
4004 (level != TP_EC_FAN_FULLSPEED) &&
4005 ((level < 0) || (level > 7)))
4006 return -EINVAL;
4008 /* safety net should the EC not support AUTO
4009 * or FULLSPEED mode bits and just ignore them */
4010 if (level & TP_EC_FAN_FULLSPEED)
4011 level |= 7; /* safety min speed 7 */
4012 else if (level & TP_EC_FAN_FULLSPEED)
4013 level |= 4; /* safety min speed 4 */
4015 if (!acpi_ec_write(fan_status_offset, level))
4016 return -EIO;
4017 else
4018 tp_features.fan_ctrl_status_undef = 0;
4019 break;
4021 default:
4022 return -ENXIO;
4024 return 0;
4027 static int fan_set_level_safe(int level)
4029 int rc;
4031 if (!fan_control_allowed)
4032 return -EPERM;
4034 rc = mutex_lock_interruptible(&fan_mutex);
4035 if (rc < 0)
4036 return rc;
4038 if (level == TPACPI_FAN_LAST_LEVEL)
4039 level = fan_control_desired_level;
4041 rc = fan_set_level(level);
4042 if (!rc)
4043 fan_update_desired_level(level);
4045 mutex_unlock(&fan_mutex);
4046 return rc;
4049 static int fan_set_enable(void)
4051 u8 s;
4052 int rc;
4054 if (!fan_control_allowed)
4055 return -EPERM;
4057 rc = mutex_lock_interruptible(&fan_mutex);
4058 if (rc < 0)
4059 return rc;
4061 switch (fan_control_access_mode) {
4062 case TPACPI_FAN_WR_ACPI_FANS:
4063 case TPACPI_FAN_WR_TPEC:
4064 rc = fan_get_status(&s);
4065 if (rc < 0)
4066 break;
4068 /* Don't go out of emergency fan mode */
4069 if (s != 7) {
4070 s &= 0x07;
4071 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4074 if (!acpi_ec_write(fan_status_offset, s))
4075 rc = -EIO;
4076 else {
4077 tp_features.fan_ctrl_status_undef = 0;
4078 rc = 0;
4080 break;
4082 case TPACPI_FAN_WR_ACPI_SFAN:
4083 rc = fan_get_status(&s);
4084 if (rc < 0)
4085 break;
4087 s &= 0x07;
4089 /* Set fan to at least level 4 */
4090 s |= 4;
4092 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4093 rc= -EIO;
4094 else
4095 rc = 0;
4096 break;
4098 default:
4099 rc = -ENXIO;
4102 mutex_unlock(&fan_mutex);
4103 return rc;
4106 static int fan_set_disable(void)
4108 int rc;
4110 if (!fan_control_allowed)
4111 return -EPERM;
4113 rc = mutex_lock_interruptible(&fan_mutex);
4114 if (rc < 0)
4115 return rc;
4117 rc = 0;
4118 switch (fan_control_access_mode) {
4119 case TPACPI_FAN_WR_ACPI_FANS:
4120 case TPACPI_FAN_WR_TPEC:
4121 if (!acpi_ec_write(fan_status_offset, 0x00))
4122 rc = -EIO;
4123 else {
4124 fan_control_desired_level = 0;
4125 tp_features.fan_ctrl_status_undef = 0;
4127 break;
4129 case TPACPI_FAN_WR_ACPI_SFAN:
4130 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4131 rc = -EIO;
4132 else
4133 fan_control_desired_level = 0;
4134 break;
4136 default:
4137 rc = -ENXIO;
4141 mutex_unlock(&fan_mutex);
4142 return rc;
4145 static int fan_set_speed(int speed)
4147 int rc;
4149 if (!fan_control_allowed)
4150 return -EPERM;
4152 rc = mutex_lock_interruptible(&fan_mutex);
4153 if (rc < 0)
4154 return rc;
4156 rc = 0;
4157 switch (fan_control_access_mode) {
4158 case TPACPI_FAN_WR_ACPI_FANS:
4159 if (speed >= 0 && speed <= 65535) {
4160 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4161 speed, speed, speed))
4162 rc = -EIO;
4163 } else
4164 rc = -EINVAL;
4165 break;
4167 default:
4168 rc = -ENXIO;
4171 mutex_unlock(&fan_mutex);
4172 return rc;
4175 static int fan_read(char *p)
4177 int len = 0;
4178 int rc;
4179 u8 status;
4180 unsigned int speed = 0;
4182 switch (fan_status_access_mode) {
4183 case TPACPI_FAN_RD_ACPI_GFAN:
4184 /* 570, 600e/x, 770e, 770x */
4185 if ((rc = fan_get_status_safe(&status)) < 0)
4186 return rc;
4188 len += sprintf(p + len, "status:\t\t%s\n"
4189 "level:\t\t%d\n",
4190 (status != 0) ? "enabled" : "disabled", status);
4191 break;
4193 case TPACPI_FAN_RD_TPEC:
4194 /* all except 570, 600e/x, 770e, 770x */
4195 if ((rc = fan_get_status_safe(&status)) < 0)
4196 return rc;
4198 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4199 if (status != fan_control_initial_status)
4200 tp_features.fan_ctrl_status_undef = 0;
4201 else
4202 /* Return most likely status. In fact, it
4203 * might be the only possible status */
4204 status = TP_EC_FAN_AUTO;
4207 len += sprintf(p + len, "status:\t\t%s\n",
4208 (status != 0) ? "enabled" : "disabled");
4210 if ((rc = fan_get_speed(&speed)) < 0)
4211 return rc;
4213 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4215 if (status & TP_EC_FAN_FULLSPEED)
4216 /* Disengaged mode takes precedence */
4217 len += sprintf(p + len, "level:\t\tdisengaged\n");
4218 else if (status & TP_EC_FAN_AUTO)
4219 len += sprintf(p + len, "level:\t\tauto\n");
4220 else
4221 len += sprintf(p + len, "level:\t\t%d\n", status);
4222 break;
4224 case TPACPI_FAN_NONE:
4225 default:
4226 len += sprintf(p + len, "status:\t\tnot supported\n");
4229 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
4230 len += sprintf(p + len, "commands:\tlevel <level>");
4232 switch (fan_control_access_mode) {
4233 case TPACPI_FAN_WR_ACPI_SFAN:
4234 len += sprintf(p + len, " (<level> is 0-7)\n");
4235 break;
4237 default:
4238 len += sprintf(p + len, " (<level> is 0-7, "
4239 "auto, disengaged, full-speed)\n");
4240 break;
4244 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
4245 len += sprintf(p + len, "commands:\tenable, disable\n"
4246 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4247 "1-120 (seconds))\n");
4249 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
4250 len += sprintf(p + len, "commands:\tspeed <speed>"
4251 " (<speed> is 0-65535)\n");
4253 return len;
4256 static int fan_write_cmd_level(const char *cmd, int *rc)
4258 int level;
4260 if (strlencmp(cmd, "level auto") == 0)
4261 level = TP_EC_FAN_AUTO;
4262 else if ((strlencmp(cmd, "level disengaged") == 0) |
4263 (strlencmp(cmd, "level full-speed") == 0))
4264 level = TP_EC_FAN_FULLSPEED;
4265 else if (sscanf(cmd, "level %d", &level) != 1)
4266 return 0;
4268 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4269 printk(IBM_ERR "level command accepted for unsupported "
4270 "access mode %d", fan_control_access_mode);
4272 return 1;
4275 static int fan_write_cmd_enable(const char *cmd, int *rc)
4277 if (strlencmp(cmd, "enable") != 0)
4278 return 0;
4280 if ((*rc = fan_set_enable()) == -ENXIO)
4281 printk(IBM_ERR "enable command accepted for unsupported "
4282 "access mode %d", fan_control_access_mode);
4284 return 1;
4287 static int fan_write_cmd_disable(const char *cmd, int *rc)
4289 if (strlencmp(cmd, "disable") != 0)
4290 return 0;
4292 if ((*rc = fan_set_disable()) == -ENXIO)
4293 printk(IBM_ERR "disable command accepted for unsupported "
4294 "access mode %d", fan_control_access_mode);
4296 return 1;
4299 static int fan_write_cmd_speed(const char *cmd, int *rc)
4301 int speed;
4303 /* TODO:
4304 * Support speed <low> <medium> <high> ? */
4306 if (sscanf(cmd, "speed %d", &speed) != 1)
4307 return 0;
4309 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4310 printk(IBM_ERR "speed command accepted for unsupported "
4311 "access mode %d", fan_control_access_mode);
4313 return 1;
4316 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4318 int interval;
4320 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4321 return 0;
4323 if (interval < 0 || interval > 120)
4324 *rc = -EINVAL;
4325 else
4326 fan_watchdog_maxinterval = interval;
4328 return 1;
4331 static int fan_write(char *buf)
4333 char *cmd;
4334 int rc = 0;
4336 while (!rc && (cmd = next_cmd(&buf))) {
4337 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4338 fan_write_cmd_level(cmd, &rc)) &&
4339 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4340 (fan_write_cmd_enable(cmd, &rc) ||
4341 fan_write_cmd_disable(cmd, &rc) ||
4342 fan_write_cmd_watchdog(cmd, &rc))) &&
4343 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4344 fan_write_cmd_speed(cmd, &rc))
4346 rc = -EINVAL;
4347 else if (!rc)
4348 fan_watchdog_reset();
4351 return rc;
4354 static struct ibm_struct fan_driver_data = {
4355 .name = "fan",
4356 .read = fan_read,
4357 .write = fan_write,
4358 .exit = fan_exit,
4361 /****************************************************************************
4362 ****************************************************************************
4364 * Infrastructure
4366 ****************************************************************************
4367 ****************************************************************************/
4369 /* /proc support */
4370 static struct proc_dir_entry *proc_dir;
4372 /* Subdriver registry */
4373 static LIST_HEAD(tpacpi_all_drivers);
4377 * Module and infrastructure proble, init and exit handling
4380 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4381 static const char * __init str_supported(int is_supported)
4383 static char text_unsupported[] __initdata = "not supported";
4385 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4387 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4389 static int __init ibm_init(struct ibm_init_struct *iibm)
4391 int ret;
4392 struct ibm_struct *ibm = iibm->data;
4393 struct proc_dir_entry *entry;
4395 BUG_ON(ibm == NULL);
4397 INIT_LIST_HEAD(&ibm->all_drivers);
4399 if (ibm->flags.experimental && !experimental)
4400 return 0;
4402 dbg_printk(TPACPI_DBG_INIT,
4403 "probing for %s\n", ibm->name);
4405 if (iibm->init) {
4406 ret = iibm->init(iibm);
4407 if (ret > 0)
4408 return 0; /* probe failed */
4409 if (ret)
4410 return ret;
4412 ibm->flags.init_called = 1;
4415 if (ibm->acpi) {
4416 if (ibm->acpi->hid) {
4417 ret = register_tpacpi_subdriver(ibm);
4418 if (ret)
4419 goto err_out;
4422 if (ibm->acpi->notify) {
4423 ret = setup_acpi_notify(ibm);
4424 if (ret == -ENODEV) {
4425 printk(IBM_NOTICE "disabling subdriver %s\n",
4426 ibm->name);
4427 ret = 0;
4428 goto err_out;
4430 if (ret < 0)
4431 goto err_out;
4435 dbg_printk(TPACPI_DBG_INIT,
4436 "%s installed\n", ibm->name);
4438 if (ibm->read) {
4439 entry = create_proc_entry(ibm->name,
4440 S_IFREG | S_IRUGO | S_IWUSR,
4441 proc_dir);
4442 if (!entry) {
4443 printk(IBM_ERR "unable to create proc entry %s\n",
4444 ibm->name);
4445 ret = -ENODEV;
4446 goto err_out;
4448 entry->owner = THIS_MODULE;
4449 entry->data = ibm;
4450 entry->read_proc = &dispatch_procfs_read;
4451 if (ibm->write)
4452 entry->write_proc = &dispatch_procfs_write;
4453 ibm->flags.proc_created = 1;
4456 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4458 return 0;
4460 err_out:
4461 dbg_printk(TPACPI_DBG_INIT,
4462 "%s: at error exit path with result %d\n",
4463 ibm->name, ret);
4465 ibm_exit(ibm);
4466 return (ret < 0)? ret : 0;
4469 static void ibm_exit(struct ibm_struct *ibm)
4471 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4473 list_del_init(&ibm->all_drivers);
4475 if (ibm->flags.acpi_notify_installed) {
4476 dbg_printk(TPACPI_DBG_EXIT,
4477 "%s: acpi_remove_notify_handler\n", ibm->name);
4478 BUG_ON(!ibm->acpi);
4479 acpi_remove_notify_handler(*ibm->acpi->handle,
4480 ibm->acpi->type,
4481 dispatch_acpi_notify);
4482 ibm->flags.acpi_notify_installed = 0;
4483 ibm->flags.acpi_notify_installed = 0;
4486 if (ibm->flags.proc_created) {
4487 dbg_printk(TPACPI_DBG_EXIT,
4488 "%s: remove_proc_entry\n", ibm->name);
4489 remove_proc_entry(ibm->name, proc_dir);
4490 ibm->flags.proc_created = 0;
4493 if (ibm->flags.acpi_driver_registered) {
4494 dbg_printk(TPACPI_DBG_EXIT,
4495 "%s: acpi_bus_unregister_driver\n", ibm->name);
4496 BUG_ON(!ibm->acpi);
4497 acpi_bus_unregister_driver(ibm->acpi->driver);
4498 kfree(ibm->acpi->driver);
4499 ibm->acpi->driver = NULL;
4500 ibm->flags.acpi_driver_registered = 0;
4503 if (ibm->flags.init_called && ibm->exit) {
4504 ibm->exit();
4505 ibm->flags.init_called = 0;
4508 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4511 /* Probing */
4513 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
4515 struct dmi_device *dev = NULL;
4516 char ec_fw_string[18];
4518 if (!tp)
4519 return;
4521 memset(tp, 0, sizeof(*tp));
4523 if (dmi_name_in_vendors("IBM"))
4524 tp->vendor = PCI_VENDOR_ID_IBM;
4525 else if (dmi_name_in_vendors("LENOVO"))
4526 tp->vendor = PCI_VENDOR_ID_LENOVO;
4527 else
4528 return;
4530 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4531 GFP_KERNEL);
4532 if (!tp->bios_version_str)
4533 return;
4534 tp->bios_model = tp->bios_version_str[0]
4535 | (tp->bios_version_str[1] << 8);
4538 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4539 * X32 or newer, all Z series; Some models must have an
4540 * up-to-date BIOS or they will not be detected.
4542 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4544 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4545 if (sscanf(dev->name,
4546 "IBM ThinkPad Embedded Controller -[%17c",
4547 ec_fw_string) == 1) {
4548 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4549 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4551 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4552 tp->ec_model = ec_fw_string[0]
4553 | (ec_fw_string[1] << 8);
4554 break;
4558 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4559 GFP_KERNEL);
4560 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4561 kfree(tp->model_str);
4562 tp->model_str = NULL;
4566 static int __init probe_for_thinkpad(void)
4568 int is_thinkpad;
4570 if (acpi_disabled)
4571 return -ENODEV;
4574 * Non-ancient models have better DMI tagging, but very old models
4575 * don't.
4577 is_thinkpad = (thinkpad_id.model_str != NULL);
4579 /* ec is required because many other handles are relative to it */
4580 IBM_ACPIHANDLE_INIT(ec);
4581 if (!ec_handle) {
4582 if (is_thinkpad)
4583 printk(IBM_ERR
4584 "Not yet supported ThinkPad detected!\n");
4585 return -ENODEV;
4589 * Risks a regression on very old machines, but reduces potential
4590 * false positives a damn great deal
4592 if (!is_thinkpad)
4593 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
4595 if (!is_thinkpad && !force_load)
4596 return -ENODEV;
4598 return 0;
4602 /* Module init, exit, parameters */
4604 static struct ibm_init_struct ibms_init[] __initdata = {
4606 .init = thinkpad_acpi_driver_init,
4607 .data = &thinkpad_acpi_driver_data,
4610 .init = hotkey_init,
4611 .data = &hotkey_driver_data,
4614 .init = bluetooth_init,
4615 .data = &bluetooth_driver_data,
4618 .init = wan_init,
4619 .data = &wan_driver_data,
4622 .init = video_init,
4623 .data = &video_driver_data,
4626 .init = light_init,
4627 .data = &light_driver_data,
4629 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4631 .init = dock_init,
4632 .data = &dock_driver_data[0],
4635 .init = dock_init2,
4636 .data = &dock_driver_data[1],
4638 #endif
4639 #ifdef CONFIG_THINKPAD_ACPI_BAY
4641 .init = bay_init,
4642 .data = &bay_driver_data,
4644 #endif
4646 .init = cmos_init,
4647 .data = &cmos_driver_data,
4650 .init = led_init,
4651 .data = &led_driver_data,
4654 .init = beep_init,
4655 .data = &beep_driver_data,
4658 .init = thermal_init,
4659 .data = &thermal_driver_data,
4662 .data = &ecdump_driver_data,
4665 .init = brightness_init,
4666 .data = &brightness_driver_data,
4669 .data = &volume_driver_data,
4672 .init = fan_init,
4673 .data = &fan_driver_data,
4677 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4679 unsigned int i;
4680 struct ibm_struct *ibm;
4682 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4683 ibm = ibms_init[i].data;
4684 BUG_ON(ibm == NULL);
4686 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4687 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4688 return -ENOSPC;
4689 strcpy(ibms_init[i].param, val);
4690 strcat(ibms_init[i].param, ",");
4691 return 0;
4695 return -EINVAL;
4698 static int experimental;
4699 module_param(experimental, int, 0);
4701 static u32 dbg_level;
4702 module_param_named(debug, dbg_level, uint, 0);
4704 static int force_load;
4705 module_param(force_load, bool, 0);
4707 static int fan_control_allowed;
4708 module_param_named(fan_control, fan_control_allowed, bool, 0);
4710 static int brightness_mode;
4711 module_param_named(brightness_mode, brightness_mode, int, 0);
4713 static unsigned int hotkey_report_mode;
4714 module_param(hotkey_report_mode, uint, 0);
4716 #define IBM_PARAM(feature) \
4717 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4719 IBM_PARAM(hotkey);
4720 IBM_PARAM(bluetooth);
4721 IBM_PARAM(video);
4722 IBM_PARAM(light);
4723 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4724 IBM_PARAM(dock);
4725 #endif
4726 #ifdef CONFIG_THINKPAD_ACPI_BAY
4727 IBM_PARAM(bay);
4728 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4729 IBM_PARAM(cmos);
4730 IBM_PARAM(led);
4731 IBM_PARAM(beep);
4732 IBM_PARAM(ecdump);
4733 IBM_PARAM(brightness);
4734 IBM_PARAM(volume);
4735 IBM_PARAM(fan);
4737 static int __init thinkpad_acpi_module_init(void)
4739 int ret, i;
4741 tpacpi_lifecycle = TPACPI_LIFE_INIT;
4743 /* Parameter checking */
4744 if (hotkey_report_mode > 2)
4745 return -EINVAL;
4747 /* Driver-level probe */
4749 get_thinkpad_model_data(&thinkpad_id);
4750 ret = probe_for_thinkpad();
4751 if (ret) {
4752 thinkpad_acpi_module_exit();
4753 return ret;
4756 /* Driver initialization */
4758 IBM_ACPIHANDLE_INIT(ecrd);
4759 IBM_ACPIHANDLE_INIT(ecwr);
4761 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4762 if (!proc_dir) {
4763 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4764 thinkpad_acpi_module_exit();
4765 return -ENODEV;
4767 proc_dir->owner = THIS_MODULE;
4769 ret = platform_driver_register(&tpacpi_pdriver);
4770 if (ret) {
4771 printk(IBM_ERR "unable to register platform driver\n");
4772 thinkpad_acpi_module_exit();
4773 return ret;
4775 tp_features.platform_drv_registered = 1;
4777 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4778 if (ret) {
4779 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4780 thinkpad_acpi_module_exit();
4781 return ret;
4783 tp_features.platform_drv_attrs_registered = 1;
4786 /* Device initialization */
4787 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4788 NULL, 0);
4789 if (IS_ERR(tpacpi_pdev)) {
4790 ret = PTR_ERR(tpacpi_pdev);
4791 tpacpi_pdev = NULL;
4792 printk(IBM_ERR "unable to register platform device\n");
4793 thinkpad_acpi_module_exit();
4794 return ret;
4796 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4797 if (IS_ERR(tpacpi_hwmon)) {
4798 ret = PTR_ERR(tpacpi_hwmon);
4799 tpacpi_hwmon = NULL;
4800 printk(IBM_ERR "unable to register hwmon device\n");
4801 thinkpad_acpi_module_exit();
4802 return ret;
4804 mutex_init(&tpacpi_inputdev_send_mutex);
4805 tpacpi_inputdev = input_allocate_device();
4806 if (!tpacpi_inputdev) {
4807 printk(IBM_ERR "unable to allocate input device\n");
4808 thinkpad_acpi_module_exit();
4809 return -ENOMEM;
4810 } else {
4811 /* Prepare input device, but don't register */
4812 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4813 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4814 tpacpi_inputdev->id.bustype = BUS_HOST;
4815 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
4816 thinkpad_id.vendor :
4817 PCI_VENDOR_ID_IBM;
4818 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4819 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4821 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4822 ret = ibm_init(&ibms_init[i]);
4823 if (ret >= 0 && *ibms_init[i].param)
4824 ret = ibms_init[i].data->write(ibms_init[i].param);
4825 if (ret < 0) {
4826 thinkpad_acpi_module_exit();
4827 return ret;
4830 ret = input_register_device(tpacpi_inputdev);
4831 if (ret < 0) {
4832 printk(IBM_ERR "unable to register input device\n");
4833 thinkpad_acpi_module_exit();
4834 return ret;
4835 } else {
4836 tp_features.input_device_registered = 1;
4839 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
4840 return 0;
4843 static void thinkpad_acpi_module_exit(void)
4845 struct ibm_struct *ibm, *itmp;
4847 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
4849 list_for_each_entry_safe_reverse(ibm, itmp,
4850 &tpacpi_all_drivers,
4851 all_drivers) {
4852 ibm_exit(ibm);
4855 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4857 if (tpacpi_inputdev) {
4858 if (tp_features.input_device_registered)
4859 input_unregister_device(tpacpi_inputdev);
4860 else
4861 input_free_device(tpacpi_inputdev);
4864 if (tpacpi_hwmon)
4865 hwmon_device_unregister(tpacpi_hwmon);
4867 if (tpacpi_pdev)
4868 platform_device_unregister(tpacpi_pdev);
4870 if (tp_features.platform_drv_attrs_registered)
4871 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4873 if (tp_features.platform_drv_registered)
4874 platform_driver_unregister(&tpacpi_pdriver);
4876 if (proc_dir)
4877 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4879 kfree(thinkpad_id.bios_version_str);
4880 kfree(thinkpad_id.ec_version_str);
4881 kfree(thinkpad_id.model_str);
4884 module_init(thinkpad_acpi_module_init);
4885 module_exit(thinkpad_acpi_module_exit);