ACPI: thinkpad-acpi: add mutex-based locking to input device event send path
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blobe592bec3f8d897bccc22cb33730e7bc484f2dd84
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 /****************************************************************************
121 ****************************************************************************
123 * ACPI Helpers and device model
125 ****************************************************************************
126 ****************************************************************************/
128 /*************************************************************************
129 * ACPI basic handles
132 static acpi_handle root_handle;
134 #define IBM_HANDLE(object, parent, paths...) \
135 static acpi_handle object##_handle; \
136 static acpi_handle *object##_parent = &parent##_handle; \
137 static char *object##_path; \
138 static char *object##_paths[] = { paths }
140 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
141 "\\_SB.PCI.ISA.EC", /* 570 */
142 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
143 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
144 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
145 "\\_SB.PCI0.ICH3.EC0", /* R31 */
146 "\\_SB.PCI0.LPC.EC", /* all others */
149 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
150 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
153 /*************************************************************************
154 * Misc ACPI handles
157 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
158 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
159 "\\CMS", /* R40, R40e */
160 ); /* all others */
162 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
163 "^HKEY", /* R30, R31 */
164 "HKEY", /* all others */
165 ); /* 570 */
168 /*************************************************************************
169 * ACPI helpers
172 static int acpi_evalf(acpi_handle handle,
173 void *res, char *method, char *fmt, ...)
175 char *fmt0 = fmt;
176 struct acpi_object_list params;
177 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
178 struct acpi_buffer result, *resultp;
179 union acpi_object out_obj;
180 acpi_status status;
181 va_list ap;
182 char res_type;
183 int success;
184 int quiet;
186 if (!*fmt) {
187 printk(IBM_ERR "acpi_evalf() called with empty format\n");
188 return 0;
191 if (*fmt == 'q') {
192 quiet = 1;
193 fmt++;
194 } else
195 quiet = 0;
197 res_type = *(fmt++);
199 params.count = 0;
200 params.pointer = &in_objs[0];
202 va_start(ap, fmt);
203 while (*fmt) {
204 char c = *(fmt++);
205 switch (c) {
206 case 'd': /* int */
207 in_objs[params.count].integer.value = va_arg(ap, int);
208 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
209 break;
210 /* add more types as needed */
211 default:
212 printk(IBM_ERR "acpi_evalf() called "
213 "with invalid format character '%c'\n", c);
214 return 0;
217 va_end(ap);
219 if (res_type != 'v') {
220 result.length = sizeof(out_obj);
221 result.pointer = &out_obj;
222 resultp = &result;
223 } else
224 resultp = NULL;
226 status = acpi_evaluate_object(handle, method, &params, resultp);
228 switch (res_type) {
229 case 'd': /* int */
230 if (res)
231 *(int *)res = out_obj.integer.value;
232 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
233 break;
234 case 'v': /* void */
235 success = status == AE_OK;
236 break;
237 /* add more types as needed */
238 default:
239 printk(IBM_ERR "acpi_evalf() called "
240 "with invalid format character '%c'\n", res_type);
241 return 0;
244 if (!success && !quiet)
245 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
246 method, fmt0, status);
248 return success;
251 static void __unused acpi_print_int(acpi_handle handle, char *method)
253 int i;
255 if (acpi_evalf(handle, &i, method, "d"))
256 printk(IBM_INFO "%s = 0x%x\n", method, i);
257 else
258 printk(IBM_ERR "error calling %s\n", method);
261 static int acpi_ec_read(int i, u8 * p)
263 int v;
265 if (ecrd_handle) {
266 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
267 return 0;
268 *p = v;
269 } else {
270 if (ec_read(i, p) < 0)
271 return 0;
274 return 1;
277 static int acpi_ec_write(int i, u8 v)
279 if (ecwr_handle) {
280 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
281 return 0;
282 } else {
283 if (ec_write(i, v) < 0)
284 return 0;
287 return 1;
290 static int _sta(acpi_handle handle)
292 int status;
294 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
295 status = 0;
297 return status;
300 static int issue_thinkpad_cmos_command(int cmos_cmd)
302 if (!cmos_handle)
303 return -ENXIO;
305 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
306 return -EIO;
308 return 0;
311 /*************************************************************************
312 * ACPI device model
315 static void drv_acpi_handle_init(char *name,
316 acpi_handle *handle, acpi_handle parent,
317 char **paths, int num_paths, char **path)
319 int i;
320 acpi_status status;
322 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
323 name);
325 for (i = 0; i < num_paths; i++) {
326 status = acpi_get_handle(parent, paths[i], handle);
327 if (ACPI_SUCCESS(status)) {
328 *path = paths[i];
329 dbg_printk(TPACPI_DBG_INIT,
330 "Found ACPI handle %s for %s\n",
331 *path, name);
332 return;
336 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
337 name);
338 *handle = NULL;
341 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
343 struct ibm_struct *ibm = data;
345 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
346 return;
348 ibm->acpi->notify(ibm, event);
351 static int __init setup_acpi_notify(struct ibm_struct *ibm)
353 acpi_status status;
354 int rc;
356 BUG_ON(!ibm->acpi);
358 if (!*ibm->acpi->handle)
359 return 0;
361 vdbg_printk(TPACPI_DBG_INIT,
362 "setting up ACPI notify for %s\n", ibm->name);
364 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
365 if (rc < 0) {
366 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
367 ibm->name, rc);
368 return -ENODEV;
371 acpi_driver_data(ibm->acpi->device) = ibm;
372 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
373 IBM_ACPI_EVENT_PREFIX,
374 ibm->name);
376 status = acpi_install_notify_handler(*ibm->acpi->handle,
377 ibm->acpi->type, dispatch_acpi_notify, ibm);
378 if (ACPI_FAILURE(status)) {
379 if (status == AE_ALREADY_EXISTS) {
380 printk(IBM_NOTICE "another device driver is already handling %s events\n",
381 ibm->name);
382 } else {
383 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
384 ibm->name, status);
386 return -ENODEV;
388 ibm->flags.acpi_notify_installed = 1;
389 return 0;
392 static int __init tpacpi_device_add(struct acpi_device *device)
394 return 0;
397 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
399 int rc;
401 dbg_printk(TPACPI_DBG_INIT,
402 "registering %s as an ACPI driver\n", ibm->name);
404 BUG_ON(!ibm->acpi);
406 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
407 if (!ibm->acpi->driver) {
408 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
409 return -ENOMEM;
412 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
413 ibm->acpi->driver->ids = ibm->acpi->hid;
414 ibm->acpi->driver->ops.add = &tpacpi_device_add;
416 rc = acpi_bus_register_driver(ibm->acpi->driver);
417 if (rc < 0) {
418 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
419 ibm->acpi->hid, rc);
420 kfree(ibm->acpi->driver);
421 ibm->acpi->driver = NULL;
422 } else if (!rc)
423 ibm->flags.acpi_driver_registered = 1;
425 return rc;
429 /****************************************************************************
430 ****************************************************************************
432 * Procfs Helpers
434 ****************************************************************************
435 ****************************************************************************/
437 static int dispatch_procfs_read(char *page, char **start, off_t off,
438 int count, int *eof, void *data)
440 struct ibm_struct *ibm = data;
441 int len;
443 if (!ibm || !ibm->read)
444 return -EINVAL;
446 len = ibm->read(page);
447 if (len < 0)
448 return len;
450 if (len <= off + count)
451 *eof = 1;
452 *start = page + off;
453 len -= off;
454 if (len > count)
455 len = count;
456 if (len < 0)
457 len = 0;
459 return len;
462 static int dispatch_procfs_write(struct file *file,
463 const char __user * userbuf,
464 unsigned long count, void *data)
466 struct ibm_struct *ibm = data;
467 char *kernbuf;
468 int ret;
470 if (!ibm || !ibm->write)
471 return -EINVAL;
473 kernbuf = kmalloc(count + 2, GFP_KERNEL);
474 if (!kernbuf)
475 return -ENOMEM;
477 if (copy_from_user(kernbuf, userbuf, count)) {
478 kfree(kernbuf);
479 return -EFAULT;
482 kernbuf[count] = 0;
483 strcat(kernbuf, ",");
484 ret = ibm->write(kernbuf);
485 if (ret == 0)
486 ret = count;
488 kfree(kernbuf);
490 return ret;
493 static char *next_cmd(char **cmds)
495 char *start = *cmds;
496 char *end;
498 while ((end = strchr(start, ',')) && end == start)
499 start = end + 1;
501 if (!end)
502 return NULL;
504 *end = 0;
505 *cmds = end + 1;
506 return start;
510 /****************************************************************************
511 ****************************************************************************
513 * Device model: input, hwmon and platform
515 ****************************************************************************
516 ****************************************************************************/
518 static struct platform_device *tpacpi_pdev;
519 static struct class_device *tpacpi_hwmon;
520 static struct input_dev *tpacpi_inputdev;
521 static struct mutex tpacpi_inputdev_send_mutex;
524 static int tpacpi_resume_handler(struct platform_device *pdev)
526 struct ibm_struct *ibm, *itmp;
528 list_for_each_entry_safe(ibm, itmp,
529 &tpacpi_all_drivers,
530 all_drivers) {
531 if (ibm->resume)
532 (ibm->resume)();
535 return 0;
538 static struct platform_driver tpacpi_pdriver = {
539 .driver = {
540 .name = IBM_DRVR_NAME,
541 .owner = THIS_MODULE,
543 .resume = tpacpi_resume_handler,
547 /*************************************************************************
548 * thinkpad-acpi driver attributes
551 /* interface_version --------------------------------------------------- */
552 static ssize_t tpacpi_driver_interface_version_show(
553 struct device_driver *drv,
554 char *buf)
556 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
559 static DRIVER_ATTR(interface_version, S_IRUGO,
560 tpacpi_driver_interface_version_show, NULL);
562 /* debug_level --------------------------------------------------------- */
563 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
564 char *buf)
566 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
569 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
570 const char *buf, size_t count)
572 unsigned long t;
574 if (parse_strtoul(buf, 0xffff, &t))
575 return -EINVAL;
577 dbg_level = t;
579 return count;
582 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
583 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
585 /* version ------------------------------------------------------------- */
586 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
587 char *buf)
589 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
592 static DRIVER_ATTR(version, S_IRUGO,
593 tpacpi_driver_version_show, NULL);
595 /* --------------------------------------------------------------------- */
597 static struct driver_attribute* tpacpi_driver_attributes[] = {
598 &driver_attr_debug_level, &driver_attr_version,
599 &driver_attr_interface_version,
602 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
604 int i, res;
606 i = 0;
607 res = 0;
608 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
609 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
610 i++;
613 return res;
616 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
618 int i;
620 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
621 driver_remove_file(drv, tpacpi_driver_attributes[i]);
624 /*************************************************************************
625 * sysfs support helpers
628 struct attribute_set_obj {
629 struct attribute_set s;
630 struct attribute *a;
631 } __attribute__((packed));
633 static struct attribute_set *create_attr_set(unsigned int max_members,
634 const char* name)
636 struct attribute_set_obj *sobj;
638 if (max_members == 0)
639 return NULL;
641 /* Allocates space for implicit NULL at the end too */
642 sobj = kzalloc(sizeof(struct attribute_set_obj) +
643 max_members * sizeof(struct attribute *),
644 GFP_KERNEL);
645 if (!sobj)
646 return NULL;
647 sobj->s.max_members = max_members;
648 sobj->s.group.attrs = &sobj->a;
649 sobj->s.group.name = name;
651 return &sobj->s;
654 /* not multi-threaded safe, use it in a single thread per set */
655 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
657 if (!s || !attr)
658 return -EINVAL;
660 if (s->members >= s->max_members)
661 return -ENOMEM;
663 s->group.attrs[s->members] = attr;
664 s->members++;
666 return 0;
669 static int add_many_to_attr_set(struct attribute_set* s,
670 struct attribute **attr,
671 unsigned int count)
673 int i, res;
675 for (i = 0; i < count; i++) {
676 res = add_to_attr_set(s, attr[i]);
677 if (res)
678 return res;
681 return 0;
684 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
686 sysfs_remove_group(kobj, &s->group);
687 destroy_attr_set(s);
690 static int parse_strtoul(const char *buf,
691 unsigned long max, unsigned long *value)
693 char *endp;
695 *value = simple_strtoul(buf, &endp, 0);
696 while (*endp && isspace(*endp))
697 endp++;
698 if (*endp || *value > max)
699 return -EINVAL;
701 return 0;
704 /****************************************************************************
705 ****************************************************************************
707 * Subdrivers
709 ****************************************************************************
710 ****************************************************************************/
712 /*************************************************************************
713 * thinkpad-acpi init subdriver
716 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
718 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
719 printk(IBM_INFO "%s\n", IBM_URL);
721 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
722 (thinkpad_id.bios_version_str) ?
723 thinkpad_id.bios_version_str : "unknown",
724 (thinkpad_id.ec_version_str) ?
725 thinkpad_id.ec_version_str : "unknown");
727 if (thinkpad_id.vendor && thinkpad_id.model_str)
728 printk(IBM_INFO "%s %s\n",
729 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
730 "IBM" : ((thinkpad_id.vendor ==
731 PCI_VENDOR_ID_LENOVO) ?
732 "Lenovo" : "Unknown vendor"),
733 thinkpad_id.model_str);
735 return 0;
738 static int thinkpad_acpi_driver_read(char *p)
740 int len = 0;
742 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
743 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
745 return len;
748 static struct ibm_struct thinkpad_acpi_driver_data = {
749 .name = "driver",
750 .read = thinkpad_acpi_driver_read,
753 /*************************************************************************
754 * Hotkey subdriver
757 static int hotkey_orig_status;
758 static u32 hotkey_orig_mask;
759 static u32 hotkey_all_mask;
760 static u32 hotkey_reserved_mask;
762 static u16 *hotkey_keycode_map;
764 static struct attribute_set *hotkey_dev_attributes;
766 static int hotkey_get_wlsw(int *status)
768 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
769 return -EIO;
770 return 0;
773 /* sysfs hotkey enable ------------------------------------------------- */
774 static ssize_t hotkey_enable_show(struct device *dev,
775 struct device_attribute *attr,
776 char *buf)
778 int res, status;
779 u32 mask;
781 res = hotkey_get(&status, &mask);
782 if (res)
783 return res;
785 return snprintf(buf, PAGE_SIZE, "%d\n", status);
788 static ssize_t hotkey_enable_store(struct device *dev,
789 struct device_attribute *attr,
790 const char *buf, size_t count)
792 unsigned long t;
793 int res, status;
794 u32 mask;
796 if (parse_strtoul(buf, 1, &t))
797 return -EINVAL;
799 res = hotkey_get(&status, &mask);
800 if (!res)
801 res = hotkey_set(t, mask);
803 return (res) ? res : count;
806 static struct device_attribute dev_attr_hotkey_enable =
807 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
808 hotkey_enable_show, hotkey_enable_store);
810 /* sysfs hotkey mask --------------------------------------------------- */
811 static ssize_t hotkey_mask_show(struct device *dev,
812 struct device_attribute *attr,
813 char *buf)
815 int res, status;
816 u32 mask;
818 res = hotkey_get(&status, &mask);
819 if (res)
820 return res;
822 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
825 static ssize_t hotkey_mask_store(struct device *dev,
826 struct device_attribute *attr,
827 const char *buf, size_t count)
829 unsigned long t;
830 int res, status;
831 u32 mask;
833 if (parse_strtoul(buf, 0xffffffffUL, &t))
834 return -EINVAL;
836 res = hotkey_get(&status, &mask);
837 if (!res)
838 hotkey_set(status, t);
840 return (res) ? res : count;
843 static struct device_attribute dev_attr_hotkey_mask =
844 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
845 hotkey_mask_show, hotkey_mask_store);
847 /* sysfs hotkey bios_enabled ------------------------------------------- */
848 static ssize_t hotkey_bios_enabled_show(struct device *dev,
849 struct device_attribute *attr,
850 char *buf)
852 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
855 static struct device_attribute dev_attr_hotkey_bios_enabled =
856 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
858 /* sysfs hotkey bios_mask ---------------------------------------------- */
859 static ssize_t hotkey_bios_mask_show(struct device *dev,
860 struct device_attribute *attr,
861 char *buf)
863 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
866 static struct device_attribute dev_attr_hotkey_bios_mask =
867 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
869 /* sysfs hotkey all_mask ----------------------------------------------- */
870 static ssize_t hotkey_all_mask_show(struct device *dev,
871 struct device_attribute *attr,
872 char *buf)
874 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
877 static struct device_attribute dev_attr_hotkey_all_mask =
878 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
880 /* sysfs hotkey recommended_mask --------------------------------------- */
881 static ssize_t hotkey_recommended_mask_show(struct device *dev,
882 struct device_attribute *attr,
883 char *buf)
885 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
886 hotkey_all_mask & ~hotkey_reserved_mask);
889 static struct device_attribute dev_attr_hotkey_recommended_mask =
890 __ATTR(hotkey_recommended_mask, S_IRUGO,
891 hotkey_recommended_mask_show, NULL);
893 /* sysfs hotkey radio_sw ----------------------------------------------- */
894 static ssize_t hotkey_radio_sw_show(struct device *dev,
895 struct device_attribute *attr,
896 char *buf)
898 int res, s;
899 res = hotkey_get_wlsw(&s);
900 if (res < 0)
901 return res;
903 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
906 static struct device_attribute dev_attr_hotkey_radio_sw =
907 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
909 /* sysfs hotkey report_mode -------------------------------------------- */
910 static ssize_t hotkey_report_mode_show(struct device *dev,
911 struct device_attribute *attr,
912 char *buf)
914 return snprintf(buf, PAGE_SIZE, "%d\n",
915 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
918 static ssize_t hotkey_report_mode_store(struct device *dev,
919 struct device_attribute *attr,
920 const char *buf, size_t count)
922 unsigned long t;
923 int res = 0;
925 if (parse_strtoul(buf, 2, &t) || (t < 1))
926 return -EINVAL;
928 if (t != hotkey_report_mode) {
929 if (tp_features.hotkey_report_mode_locked)
930 return -EPERM;
931 hotkey_report_mode = t;
932 printk(IBM_NOTICE "hot key report mode set to %d\n",
933 hotkey_report_mode);
936 return (res) ? res : count;
939 static struct device_attribute dev_attr_hotkey_report_mode =
940 __ATTR(hotkey_report_mode, S_IWUSR | S_IRUGO,
941 hotkey_report_mode_show, hotkey_report_mode_store);
943 /* --------------------------------------------------------------------- */
945 static struct attribute *hotkey_attributes[] __initdata = {
946 &dev_attr_hotkey_enable.attr,
947 &dev_attr_hotkey_report_mode.attr,
950 static struct attribute *hotkey_mask_attributes[] __initdata = {
951 &dev_attr_hotkey_mask.attr,
952 &dev_attr_hotkey_bios_enabled.attr,
953 &dev_attr_hotkey_bios_mask.attr,
954 &dev_attr_hotkey_all_mask.attr,
955 &dev_attr_hotkey_recommended_mask.attr,
958 static int __init hotkey_init(struct ibm_init_struct *iibm)
961 static u16 ibm_keycode_map[] __initdata = {
962 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
963 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
964 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
965 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
966 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
967 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
968 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
969 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
970 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
971 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
972 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
973 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
974 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
975 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
976 KEY_RESERVED, /* 0x14: VOLUME UP */
977 KEY_RESERVED, /* 0x15: VOLUME DOWN */
978 KEY_RESERVED, /* 0x16: MUTE */
979 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
980 /* (assignments unknown, please report if found) */
981 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
982 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
984 static u16 lenovo_keycode_map[] __initdata = {
985 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
986 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
987 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
988 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
989 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
990 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
991 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
992 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
993 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
994 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
995 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
996 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
997 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
998 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
999 KEY_RESERVED, /* 0x14: VOLUME UP */
1000 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1001 KEY_RESERVED, /* 0x16: MUTE */
1002 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1003 /* (assignments unknown, please report if found) */
1004 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1005 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1008 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1009 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1010 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1012 int res, i;
1013 int status;
1015 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1017 BUG_ON(!tpacpi_inputdev);
1019 IBM_ACPIHANDLE_INIT(hkey);
1020 mutex_init(&hotkey_mutex);
1022 /* hotkey not supported on 570 */
1023 tp_features.hotkey = hkey_handle != NULL;
1025 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1026 str_supported(tp_features.hotkey));
1028 if (tp_features.hotkey) {
1029 hotkey_dev_attributes = create_attr_set(8, NULL);
1030 if (!hotkey_dev_attributes)
1031 return -ENOMEM;
1032 res = add_many_to_attr_set(hotkey_dev_attributes,
1033 hotkey_attributes,
1034 ARRAY_SIZE(hotkey_attributes));
1035 if (res)
1036 return res;
1038 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1039 A30, R30, R31, T20-22, X20-21, X22-24 */
1040 tp_features.hotkey_mask =
1041 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
1043 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1044 str_supported(tp_features.hotkey_mask));
1046 if (tp_features.hotkey_mask) {
1047 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
1048 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1049 "MHKA", "qd"))
1050 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1053 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
1054 if (!res && tp_features.hotkey_mask) {
1055 res = add_many_to_attr_set(hotkey_dev_attributes,
1056 hotkey_mask_attributes,
1057 ARRAY_SIZE(hotkey_mask_attributes));
1060 /* Not all thinkpads have a hardware radio switch */
1061 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1062 tp_features.hotkey_wlsw = 1;
1063 printk(IBM_INFO
1064 "radio switch found; radios are %s\n",
1065 enabled(status, 0));
1066 res = add_to_attr_set(hotkey_dev_attributes,
1067 &dev_attr_hotkey_radio_sw.attr);
1070 if (!res)
1071 res = register_attr_set_with_sysfs(
1072 hotkey_dev_attributes,
1073 &tpacpi_pdev->dev.kobj);
1074 if (res)
1075 return res;
1077 /* Set up key map */
1079 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1080 GFP_KERNEL);
1081 if (!hotkey_keycode_map) {
1082 printk(IBM_ERR "failed to allocate memory for key map\n");
1083 return -ENOMEM;
1086 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1087 dbg_printk(TPACPI_DBG_INIT,
1088 "using Lenovo default hot key map\n");
1089 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1090 TPACPI_HOTKEY_MAP_SIZE);
1091 } else {
1092 dbg_printk(TPACPI_DBG_INIT,
1093 "using IBM default hot key map\n");
1094 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1095 TPACPI_HOTKEY_MAP_SIZE);
1098 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1099 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1100 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1101 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1102 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1103 tpacpi_inputdev->keycode = hotkey_keycode_map;
1104 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1105 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1106 set_bit(hotkey_keycode_map[i],
1107 tpacpi_inputdev->keybit);
1108 } else {
1109 if (i < sizeof(hotkey_reserved_mask)*8)
1110 hotkey_reserved_mask |= 1 << i;
1114 if (tp_features.hotkey_wlsw) {
1115 set_bit(EV_SW, tpacpi_inputdev->evbit);
1116 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1119 dbg_printk(TPACPI_DBG_INIT,
1120 "enabling hot key handling\n");
1121 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1122 | hotkey_orig_mask);
1123 if (res)
1124 return res;
1126 if (hotkey_report_mode > 0) {
1127 tp_features.hotkey_report_mode_locked = 1;
1128 } else {
1129 hotkey_report_mode = 1;
1130 vdbg_printk(TPACPI_DBG_INIT,
1131 "hot key reporting mode can be "
1132 "changed at runtime\n");
1135 dbg_printk(TPACPI_DBG_INIT,
1136 "legacy hot key reporting over procfs %s\n",
1137 (hotkey_report_mode < 2) ?
1138 "enabled" : "disabled");
1141 return (tp_features.hotkey)? 0 : 1;
1144 static void hotkey_exit(void)
1146 int res;
1148 if (tp_features.hotkey) {
1149 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
1150 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1151 if (res)
1152 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
1155 if (hotkey_dev_attributes) {
1156 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1157 hotkey_dev_attributes = NULL;
1161 static void tpacpi_input_send_key(unsigned int scancode,
1162 unsigned int keycode)
1164 if (keycode != KEY_RESERVED) {
1165 mutex_lock(&tpacpi_inputdev_send_mutex);
1167 input_report_key(tpacpi_inputdev, keycode, 1);
1168 if (keycode == KEY_UNKNOWN)
1169 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1170 scancode);
1171 input_sync(tpacpi_inputdev);
1173 input_report_key(tpacpi_inputdev, keycode, 0);
1174 if (keycode == KEY_UNKNOWN)
1175 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1176 scancode);
1177 input_sync(tpacpi_inputdev);
1179 mutex_unlock(&tpacpi_inputdev_send_mutex);
1183 static void tpacpi_input_send_radiosw(void)
1185 int wlsw;
1187 mutex_lock(&tpacpi_inputdev_send_mutex);
1189 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1190 input_report_switch(tpacpi_inputdev,
1191 SW_RADIO, !!wlsw);
1192 input_sync(tpacpi_inputdev);
1195 mutex_unlock(&tpacpi_inputdev_send_mutex);
1198 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1200 u32 hkey;
1201 unsigned int keycode, scancode;
1202 int send_acpi_ev = 0;
1204 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1205 switch (hkey >> 12) {
1206 case 1:
1207 /* 0x1000-0x1FFF: key presses */
1208 scancode = hkey & 0xfff;
1209 if (scancode > 0 && scancode < 0x21) {
1210 scancode--;
1211 keycode = hotkey_keycode_map[scancode];
1212 tpacpi_input_send_key(scancode, keycode);
1213 } else {
1214 printk(IBM_ERR
1215 "hotkey 0x%04x out of range for keyboard map\n",
1216 hkey);
1217 send_acpi_ev = 1;
1219 break;
1220 case 5:
1221 /* 0x5000-0x5FFF: LID */
1222 /* we don't handle it through this path, just
1223 * eat up known LID events */
1224 if (hkey != 0x5001 && hkey != 0x5002) {
1225 printk(IBM_ERR
1226 "unknown LID-related hotkey event: 0x%04x\n",
1227 hkey);
1228 send_acpi_ev = 1;
1230 break;
1231 case 7:
1232 /* 0x7000-0x7FFF: misc */
1233 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1234 tpacpi_input_send_radiosw();
1235 break;
1237 /* fallthrough to default */
1238 default:
1239 /* case 2: dock-related */
1240 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1241 /* case 3: ultra-bay related. maybe bay in dock? */
1242 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1243 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1244 send_acpi_ev = 1;
1246 } else {
1247 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
1248 hkey = 0;
1249 send_acpi_ev = 1;
1252 if (send_acpi_ev || hotkey_report_mode < 2) {
1253 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
1257 static void hotkey_resume(void)
1259 tpacpi_input_send_radiosw();
1263 * Call with hotkey_mutex held
1265 static int hotkey_get(int *status, u32 *mask)
1267 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1268 return -EIO;
1270 if (tp_features.hotkey_mask)
1271 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
1272 return -EIO;
1274 return 0;
1278 * Call with hotkey_mutex held
1280 static int hotkey_set(int status, u32 mask)
1282 int i;
1284 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1285 return -EIO;
1287 if (tp_features.hotkey_mask)
1288 for (i = 0; i < 32; i++) {
1289 int bit = ((1 << i) & mask) != 0;
1290 if (!acpi_evalf(hkey_handle,
1291 NULL, "MHKM", "vdd", i + 1, bit))
1292 return -EIO;
1295 return 0;
1298 /* procfs -------------------------------------------------------------- */
1299 static int hotkey_read(char *p)
1301 int res, status;
1302 u32 mask;
1303 int len = 0;
1305 if (!tp_features.hotkey) {
1306 len += sprintf(p + len, "status:\t\tnot supported\n");
1307 return len;
1310 res = mutex_lock_interruptible(&hotkey_mutex);
1311 if (res < 0)
1312 return res;
1313 res = hotkey_get(&status, &mask);
1314 mutex_unlock(&hotkey_mutex);
1315 if (res)
1316 return res;
1318 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1319 if (tp_features.hotkey_mask) {
1320 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1321 len += sprintf(p + len,
1322 "commands:\tenable, disable, reset, <mask>\n");
1323 } else {
1324 len += sprintf(p + len, "mask:\t\tnot supported\n");
1325 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1328 return len;
1331 static int hotkey_write(char *buf)
1333 int res, status;
1334 u32 mask;
1335 char *cmd;
1336 int do_cmd = 0;
1338 if (!tp_features.hotkey)
1339 return -ENODEV;
1341 res = mutex_lock_interruptible(&hotkey_mutex);
1342 if (res < 0)
1343 return res;
1345 res = hotkey_get(&status, &mask);
1346 if (res)
1347 goto errexit;
1349 res = 0;
1350 while ((cmd = next_cmd(&buf))) {
1351 if (strlencmp(cmd, "enable") == 0) {
1352 status = 1;
1353 } else if (strlencmp(cmd, "disable") == 0) {
1354 status = 0;
1355 } else if (strlencmp(cmd, "reset") == 0) {
1356 status = hotkey_orig_status;
1357 mask = hotkey_orig_mask;
1358 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1359 /* mask set */
1360 } else if (sscanf(cmd, "%x", &mask) == 1) {
1361 /* mask set */
1362 } else {
1363 res = -EINVAL;
1364 goto errexit;
1366 do_cmd = 1;
1369 if (do_cmd)
1370 res = hotkey_set(status, mask);
1372 errexit:
1373 mutex_unlock(&hotkey_mutex);
1374 return res;
1377 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1378 .hid = IBM_HKEY_HID,
1379 .notify = hotkey_notify,
1380 .handle = &hkey_handle,
1381 .type = ACPI_DEVICE_NOTIFY,
1384 static struct ibm_struct hotkey_driver_data = {
1385 .name = "hotkey",
1386 .read = hotkey_read,
1387 .write = hotkey_write,
1388 .exit = hotkey_exit,
1389 .resume = hotkey_resume,
1390 .acpi = &ibm_hotkey_acpidriver,
1393 /*************************************************************************
1394 * Bluetooth subdriver
1397 /* sysfs bluetooth enable ---------------------------------------------- */
1398 static ssize_t bluetooth_enable_show(struct device *dev,
1399 struct device_attribute *attr,
1400 char *buf)
1402 int status;
1404 status = bluetooth_get_radiosw();
1405 if (status < 0)
1406 return status;
1408 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1411 static ssize_t bluetooth_enable_store(struct device *dev,
1412 struct device_attribute *attr,
1413 const char *buf, size_t count)
1415 unsigned long t;
1416 int res;
1418 if (parse_strtoul(buf, 1, &t))
1419 return -EINVAL;
1421 res = bluetooth_set_radiosw(t);
1423 return (res) ? res : count;
1426 static struct device_attribute dev_attr_bluetooth_enable =
1427 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1428 bluetooth_enable_show, bluetooth_enable_store);
1430 /* --------------------------------------------------------------------- */
1432 static struct attribute *bluetooth_attributes[] = {
1433 &dev_attr_bluetooth_enable.attr,
1434 NULL
1437 static const struct attribute_group bluetooth_attr_group = {
1438 .attrs = bluetooth_attributes,
1441 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1443 int res;
1444 int status = 0;
1446 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1448 IBM_ACPIHANDLE_INIT(hkey);
1450 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1451 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1452 tp_features.bluetooth = hkey_handle &&
1453 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1455 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1456 str_supported(tp_features.bluetooth),
1457 status);
1459 if (tp_features.bluetooth) {
1460 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1461 /* no bluetooth hardware present in system */
1462 tp_features.bluetooth = 0;
1463 dbg_printk(TPACPI_DBG_INIT,
1464 "bluetooth hardware not installed\n");
1465 } else {
1466 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1467 &bluetooth_attr_group);
1468 if (res)
1469 return res;
1473 return (tp_features.bluetooth)? 0 : 1;
1476 static void bluetooth_exit(void)
1478 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1479 &bluetooth_attr_group);
1482 static int bluetooth_get_radiosw(void)
1484 int status;
1486 if (!tp_features.bluetooth)
1487 return -ENODEV;
1489 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1490 return -EIO;
1492 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1495 static int bluetooth_set_radiosw(int radio_on)
1497 int status;
1499 if (!tp_features.bluetooth)
1500 return -ENODEV;
1502 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1503 return -EIO;
1504 if (radio_on)
1505 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1506 else
1507 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1508 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1509 return -EIO;
1511 return 0;
1514 /* procfs -------------------------------------------------------------- */
1515 static int bluetooth_read(char *p)
1517 int len = 0;
1518 int status = bluetooth_get_radiosw();
1520 if (!tp_features.bluetooth)
1521 len += sprintf(p + len, "status:\t\tnot supported\n");
1522 else {
1523 len += sprintf(p + len, "status:\t\t%s\n",
1524 (status)? "enabled" : "disabled");
1525 len += sprintf(p + len, "commands:\tenable, disable\n");
1528 return len;
1531 static int bluetooth_write(char *buf)
1533 char *cmd;
1535 if (!tp_features.bluetooth)
1536 return -ENODEV;
1538 while ((cmd = next_cmd(&buf))) {
1539 if (strlencmp(cmd, "enable") == 0) {
1540 bluetooth_set_radiosw(1);
1541 } else if (strlencmp(cmd, "disable") == 0) {
1542 bluetooth_set_radiosw(0);
1543 } else
1544 return -EINVAL;
1547 return 0;
1550 static struct ibm_struct bluetooth_driver_data = {
1551 .name = "bluetooth",
1552 .read = bluetooth_read,
1553 .write = bluetooth_write,
1554 .exit = bluetooth_exit,
1557 /*************************************************************************
1558 * Wan subdriver
1561 /* sysfs wan enable ---------------------------------------------------- */
1562 static ssize_t wan_enable_show(struct device *dev,
1563 struct device_attribute *attr,
1564 char *buf)
1566 int status;
1568 status = wan_get_radiosw();
1569 if (status < 0)
1570 return status;
1572 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1575 static ssize_t wan_enable_store(struct device *dev,
1576 struct device_attribute *attr,
1577 const char *buf, size_t count)
1579 unsigned long t;
1580 int res;
1582 if (parse_strtoul(buf, 1, &t))
1583 return -EINVAL;
1585 res = wan_set_radiosw(t);
1587 return (res) ? res : count;
1590 static struct device_attribute dev_attr_wan_enable =
1591 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1592 wan_enable_show, wan_enable_store);
1594 /* --------------------------------------------------------------------- */
1596 static struct attribute *wan_attributes[] = {
1597 &dev_attr_wan_enable.attr,
1598 NULL
1601 static const struct attribute_group wan_attr_group = {
1602 .attrs = wan_attributes,
1605 static int __init wan_init(struct ibm_init_struct *iibm)
1607 int res;
1608 int status = 0;
1610 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1612 IBM_ACPIHANDLE_INIT(hkey);
1614 tp_features.wan = hkey_handle &&
1615 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1617 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1618 str_supported(tp_features.wan),
1619 status);
1621 if (tp_features.wan) {
1622 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1623 /* no wan hardware present in system */
1624 tp_features.wan = 0;
1625 dbg_printk(TPACPI_DBG_INIT,
1626 "wan hardware not installed\n");
1627 } else {
1628 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1629 &wan_attr_group);
1630 if (res)
1631 return res;
1635 return (tp_features.wan)? 0 : 1;
1638 static void wan_exit(void)
1640 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1641 &wan_attr_group);
1644 static int wan_get_radiosw(void)
1646 int status;
1648 if (!tp_features.wan)
1649 return -ENODEV;
1651 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1652 return -EIO;
1654 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1657 static int wan_set_radiosw(int radio_on)
1659 int status;
1661 if (!tp_features.wan)
1662 return -ENODEV;
1664 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1665 return -EIO;
1666 if (radio_on)
1667 status |= TP_ACPI_WANCARD_RADIOSSW;
1668 else
1669 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1670 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1671 return -EIO;
1673 return 0;
1676 /* procfs -------------------------------------------------------------- */
1677 static int wan_read(char *p)
1679 int len = 0;
1680 int status = wan_get_radiosw();
1682 if (!tp_features.wan)
1683 len += sprintf(p + len, "status:\t\tnot supported\n");
1684 else {
1685 len += sprintf(p + len, "status:\t\t%s\n",
1686 (status)? "enabled" : "disabled");
1687 len += sprintf(p + len, "commands:\tenable, disable\n");
1690 return len;
1693 static int wan_write(char *buf)
1695 char *cmd;
1697 if (!tp_features.wan)
1698 return -ENODEV;
1700 while ((cmd = next_cmd(&buf))) {
1701 if (strlencmp(cmd, "enable") == 0) {
1702 wan_set_radiosw(1);
1703 } else if (strlencmp(cmd, "disable") == 0) {
1704 wan_set_radiosw(0);
1705 } else
1706 return -EINVAL;
1709 return 0;
1712 static struct ibm_struct wan_driver_data = {
1713 .name = "wan",
1714 .read = wan_read,
1715 .write = wan_write,
1716 .exit = wan_exit,
1717 .flags.experimental = 1,
1720 /*************************************************************************
1721 * Video subdriver
1724 static enum video_access_mode video_supported;
1725 static int video_orig_autosw;
1727 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1728 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1729 "\\_SB.PCI0.VID0", /* 770e */
1730 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1731 "\\_SB.PCI0.AGP.VID", /* all others */
1732 ); /* R30, R31 */
1734 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1736 static int __init video_init(struct ibm_init_struct *iibm)
1738 int ivga;
1740 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1742 IBM_ACPIHANDLE_INIT(vid);
1743 IBM_ACPIHANDLE_INIT(vid2);
1745 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1746 /* G41, assume IVGA doesn't change */
1747 vid_handle = vid2_handle;
1749 if (!vid_handle)
1750 /* video switching not supported on R30, R31 */
1751 video_supported = TPACPI_VIDEO_NONE;
1752 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1753 /* 570 */
1754 video_supported = TPACPI_VIDEO_570;
1755 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1756 /* 600e/x, 770e, 770x */
1757 video_supported = TPACPI_VIDEO_770;
1758 else
1759 /* all others */
1760 video_supported = TPACPI_VIDEO_NEW;
1762 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1763 str_supported(video_supported != TPACPI_VIDEO_NONE),
1764 video_supported);
1766 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1769 static void video_exit(void)
1771 dbg_printk(TPACPI_DBG_EXIT,
1772 "restoring original video autoswitch mode\n");
1773 if (video_autosw_set(video_orig_autosw))
1774 printk(IBM_ERR "error while trying to restore original "
1775 "video autoswitch mode\n");
1778 static int video_outputsw_get(void)
1780 int status = 0;
1781 int i;
1783 switch (video_supported) {
1784 case TPACPI_VIDEO_570:
1785 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1786 TP_ACPI_VIDEO_570_PHSCMD))
1787 return -EIO;
1788 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1789 break;
1790 case TPACPI_VIDEO_770:
1791 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1792 return -EIO;
1793 if (i)
1794 status |= TP_ACPI_VIDEO_S_LCD;
1795 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1796 return -EIO;
1797 if (i)
1798 status |= TP_ACPI_VIDEO_S_CRT;
1799 break;
1800 case TPACPI_VIDEO_NEW:
1801 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1802 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1803 return -EIO;
1804 if (i)
1805 status |= TP_ACPI_VIDEO_S_CRT;
1807 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1808 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1809 return -EIO;
1810 if (i)
1811 status |= TP_ACPI_VIDEO_S_LCD;
1812 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1813 return -EIO;
1814 if (i)
1815 status |= TP_ACPI_VIDEO_S_DVI;
1816 break;
1817 default:
1818 return -ENOSYS;
1821 return status;
1824 static int video_outputsw_set(int status)
1826 int autosw;
1827 int res = 0;
1829 switch (video_supported) {
1830 case TPACPI_VIDEO_570:
1831 res = acpi_evalf(NULL, NULL,
1832 "\\_SB.PHS2", "vdd",
1833 TP_ACPI_VIDEO_570_PHS2CMD,
1834 status | TP_ACPI_VIDEO_570_PHS2SET);
1835 break;
1836 case TPACPI_VIDEO_770:
1837 autosw = video_autosw_get();
1838 if (autosw < 0)
1839 return autosw;
1841 res = video_autosw_set(1);
1842 if (res)
1843 return res;
1844 res = acpi_evalf(vid_handle, NULL,
1845 "ASWT", "vdd", status * 0x100, 0);
1846 if (!autosw && video_autosw_set(autosw)) {
1847 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1848 return -EIO;
1850 break;
1851 case TPACPI_VIDEO_NEW:
1852 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1853 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1854 break;
1855 default:
1856 return -ENOSYS;
1859 return (res)? 0 : -EIO;
1862 static int video_autosw_get(void)
1864 int autosw = 0;
1866 switch (video_supported) {
1867 case TPACPI_VIDEO_570:
1868 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1869 return -EIO;
1870 break;
1871 case TPACPI_VIDEO_770:
1872 case TPACPI_VIDEO_NEW:
1873 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1874 return -EIO;
1875 break;
1876 default:
1877 return -ENOSYS;
1880 return autosw & 1;
1883 static int video_autosw_set(int enable)
1885 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1886 return -EIO;
1887 return 0;
1890 static int video_outputsw_cycle(void)
1892 int autosw = video_autosw_get();
1893 int res;
1895 if (autosw < 0)
1896 return autosw;
1898 switch (video_supported) {
1899 case TPACPI_VIDEO_570:
1900 res = video_autosw_set(1);
1901 if (res)
1902 return res;
1903 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1904 break;
1905 case TPACPI_VIDEO_770:
1906 case TPACPI_VIDEO_NEW:
1907 res = video_autosw_set(1);
1908 if (res)
1909 return res;
1910 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1911 break;
1912 default:
1913 return -ENOSYS;
1915 if (!autosw && video_autosw_set(autosw)) {
1916 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1917 return -EIO;
1920 return (res)? 0 : -EIO;
1923 static int video_expand_toggle(void)
1925 switch (video_supported) {
1926 case TPACPI_VIDEO_570:
1927 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1928 0 : -EIO;
1929 case TPACPI_VIDEO_770:
1930 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1931 0 : -EIO;
1932 case TPACPI_VIDEO_NEW:
1933 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1934 0 : -EIO;
1935 default:
1936 return -ENOSYS;
1938 /* not reached */
1941 static int video_read(char *p)
1943 int status, autosw;
1944 int len = 0;
1946 if (video_supported == TPACPI_VIDEO_NONE) {
1947 len += sprintf(p + len, "status:\t\tnot supported\n");
1948 return len;
1951 status = video_outputsw_get();
1952 if (status < 0)
1953 return status;
1955 autosw = video_autosw_get();
1956 if (autosw < 0)
1957 return autosw;
1959 len += sprintf(p + len, "status:\t\tsupported\n");
1960 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1961 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1962 if (video_supported == TPACPI_VIDEO_NEW)
1963 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1964 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1965 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1966 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1967 if (video_supported == TPACPI_VIDEO_NEW)
1968 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1969 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1970 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1972 return len;
1975 static int video_write(char *buf)
1977 char *cmd;
1978 int enable, disable, status;
1979 int res;
1981 if (video_supported == TPACPI_VIDEO_NONE)
1982 return -ENODEV;
1984 enable = 0;
1985 disable = 0;
1987 while ((cmd = next_cmd(&buf))) {
1988 if (strlencmp(cmd, "lcd_enable") == 0) {
1989 enable |= TP_ACPI_VIDEO_S_LCD;
1990 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1991 disable |= TP_ACPI_VIDEO_S_LCD;
1992 } else if (strlencmp(cmd, "crt_enable") == 0) {
1993 enable |= TP_ACPI_VIDEO_S_CRT;
1994 } else if (strlencmp(cmd, "crt_disable") == 0) {
1995 disable |= TP_ACPI_VIDEO_S_CRT;
1996 } else if (video_supported == TPACPI_VIDEO_NEW &&
1997 strlencmp(cmd, "dvi_enable") == 0) {
1998 enable |= TP_ACPI_VIDEO_S_DVI;
1999 } else if (video_supported == TPACPI_VIDEO_NEW &&
2000 strlencmp(cmd, "dvi_disable") == 0) {
2001 disable |= TP_ACPI_VIDEO_S_DVI;
2002 } else if (strlencmp(cmd, "auto_enable") == 0) {
2003 res = video_autosw_set(1);
2004 if (res)
2005 return res;
2006 } else if (strlencmp(cmd, "auto_disable") == 0) {
2007 res = video_autosw_set(0);
2008 if (res)
2009 return res;
2010 } else if (strlencmp(cmd, "video_switch") == 0) {
2011 res = video_outputsw_cycle();
2012 if (res)
2013 return res;
2014 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2015 res = video_expand_toggle();
2016 if (res)
2017 return res;
2018 } else
2019 return -EINVAL;
2022 if (enable || disable) {
2023 status = video_outputsw_get();
2024 if (status < 0)
2025 return status;
2026 res = video_outputsw_set((status & ~disable) | enable);
2027 if (res)
2028 return res;
2031 return 0;
2034 static struct ibm_struct video_driver_data = {
2035 .name = "video",
2036 .read = video_read,
2037 .write = video_write,
2038 .exit = video_exit,
2041 /*************************************************************************
2042 * Light (thinklight) subdriver
2045 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2046 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2048 static int __init light_init(struct ibm_init_struct *iibm)
2050 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2052 IBM_ACPIHANDLE_INIT(ledb);
2053 IBM_ACPIHANDLE_INIT(lght);
2054 IBM_ACPIHANDLE_INIT(cmos);
2056 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2057 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2059 if (tp_features.light)
2060 /* light status not supported on
2061 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2062 tp_features.light_status =
2063 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2065 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2066 str_supported(tp_features.light));
2068 return (tp_features.light)? 0 : 1;
2071 static int light_read(char *p)
2073 int len = 0;
2074 int status = 0;
2076 if (!tp_features.light) {
2077 len += sprintf(p + len, "status:\t\tnot supported\n");
2078 } else if (!tp_features.light_status) {
2079 len += sprintf(p + len, "status:\t\tunknown\n");
2080 len += sprintf(p + len, "commands:\ton, off\n");
2081 } else {
2082 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2083 return -EIO;
2084 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2085 len += sprintf(p + len, "commands:\ton, off\n");
2088 return len;
2091 static int light_write(char *buf)
2093 int cmos_cmd, lght_cmd;
2094 char *cmd;
2095 int success;
2097 if (!tp_features.light)
2098 return -ENODEV;
2100 while ((cmd = next_cmd(&buf))) {
2101 if (strlencmp(cmd, "on") == 0) {
2102 cmos_cmd = 0x0c;
2103 lght_cmd = 1;
2104 } else if (strlencmp(cmd, "off") == 0) {
2105 cmos_cmd = 0x0d;
2106 lght_cmd = 0;
2107 } else
2108 return -EINVAL;
2110 success = cmos_handle ?
2111 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2112 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2113 if (!success)
2114 return -EIO;
2117 return 0;
2120 static struct ibm_struct light_driver_data = {
2121 .name = "light",
2122 .read = light_read,
2123 .write = light_write,
2126 /*************************************************************************
2127 * Dock subdriver
2130 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2132 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2133 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2134 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2135 "\\_SB.PCI.ISA.SLCE", /* 570 */
2136 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2138 /* don't list other alternatives as we install a notify handler on the 570 */
2139 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2141 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2143 .notify = dock_notify,
2144 .handle = &dock_handle,
2145 .type = ACPI_SYSTEM_NOTIFY,
2148 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2149 * We just use it to get notifications of dock hotplug
2150 * in very old thinkpads */
2151 .hid = PCI_ROOT_HID_STRING,
2152 .notify = dock_notify,
2153 .handle = &pci_handle,
2154 .type = ACPI_SYSTEM_NOTIFY,
2158 static struct ibm_struct dock_driver_data[2] = {
2160 .name = "dock",
2161 .read = dock_read,
2162 .write = dock_write,
2163 .acpi = &ibm_dock_acpidriver[0],
2166 .name = "dock",
2167 .acpi = &ibm_dock_acpidriver[1],
2171 #define dock_docked() (_sta(dock_handle) & 1)
2173 static int __init dock_init(struct ibm_init_struct *iibm)
2175 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2177 IBM_ACPIHANDLE_INIT(dock);
2179 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2180 str_supported(dock_handle != NULL));
2182 return (dock_handle)? 0 : 1;
2185 static int __init dock_init2(struct ibm_init_struct *iibm)
2187 int dock2_needed;
2189 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2191 if (dock_driver_data[0].flags.acpi_driver_registered &&
2192 dock_driver_data[0].flags.acpi_notify_installed) {
2193 IBM_ACPIHANDLE_INIT(pci);
2194 dock2_needed = (pci_handle != NULL);
2195 vdbg_printk(TPACPI_DBG_INIT,
2196 "dock PCI handler for the TP 570 is %s\n",
2197 str_supported(dock2_needed));
2198 } else {
2199 vdbg_printk(TPACPI_DBG_INIT,
2200 "dock subdriver part 2 not required\n");
2201 dock2_needed = 0;
2204 return (dock2_needed)? 0 : 1;
2207 static void dock_notify(struct ibm_struct *ibm, u32 event)
2209 int docked = dock_docked();
2210 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, PCI_ROOT_HID_STRING);
2212 if (event == 1 && !pci) /* 570 */
2213 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2214 else if (event == 1 && pci) /* 570 */
2215 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2216 else if (event == 3 && docked)
2217 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2218 else if (event == 3 && !docked)
2219 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
2220 else if (event == 0 && docked)
2221 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2222 else {
2223 printk(IBM_ERR "unknown dock event %d, status %d\n",
2224 event, _sta(dock_handle));
2225 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
2229 static int dock_read(char *p)
2231 int len = 0;
2232 int docked = dock_docked();
2234 if (!dock_handle)
2235 len += sprintf(p + len, "status:\t\tnot supported\n");
2236 else if (!docked)
2237 len += sprintf(p + len, "status:\t\tundocked\n");
2238 else {
2239 len += sprintf(p + len, "status:\t\tdocked\n");
2240 len += sprintf(p + len, "commands:\tdock, undock\n");
2243 return len;
2246 static int dock_write(char *buf)
2248 char *cmd;
2250 if (!dock_docked())
2251 return -ENODEV;
2253 while ((cmd = next_cmd(&buf))) {
2254 if (strlencmp(cmd, "undock") == 0) {
2255 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2256 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2257 return -EIO;
2258 } else if (strlencmp(cmd, "dock") == 0) {
2259 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2260 return -EIO;
2261 } else
2262 return -EINVAL;
2265 return 0;
2268 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2270 /*************************************************************************
2271 * Bay subdriver
2274 #ifdef CONFIG_THINKPAD_ACPI_BAY
2275 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2276 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2277 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2278 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2279 ); /* A21e, R30, R31 */
2280 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2281 "_EJ0", /* all others */
2282 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2283 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2284 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2285 ); /* all others */
2286 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2287 "_EJ0", /* 770x */
2288 ); /* all others */
2290 static int __init bay_init(struct ibm_init_struct *iibm)
2292 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2294 IBM_ACPIHANDLE_INIT(bay);
2295 if (bay_handle)
2296 IBM_ACPIHANDLE_INIT(bay_ej);
2297 IBM_ACPIHANDLE_INIT(bay2);
2298 if (bay2_handle)
2299 IBM_ACPIHANDLE_INIT(bay2_ej);
2301 tp_features.bay_status = bay_handle &&
2302 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2303 tp_features.bay_status2 = bay2_handle &&
2304 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2306 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2307 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2308 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2309 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2311 vdbg_printk(TPACPI_DBG_INIT,
2312 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2313 str_supported(tp_features.bay_status),
2314 str_supported(tp_features.bay_eject),
2315 str_supported(tp_features.bay_status2),
2316 str_supported(tp_features.bay_eject2));
2318 return (tp_features.bay_status || tp_features.bay_eject ||
2319 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2322 static void bay_notify(struct ibm_struct *ibm, u32 event)
2324 acpi_bus_generate_event(ibm->acpi->device, event, 0);
2327 #define bay_occupied(b) (_sta(b##_handle) & 1)
2329 static int bay_read(char *p)
2331 int len = 0;
2332 int occupied = bay_occupied(bay);
2333 int occupied2 = bay_occupied(bay2);
2334 int eject, eject2;
2336 len += sprintf(p + len, "status:\t\t%s\n",
2337 tp_features.bay_status ?
2338 (occupied ? "occupied" : "unoccupied") :
2339 "not supported");
2340 if (tp_features.bay_status2)
2341 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2342 "occupied" : "unoccupied");
2344 eject = tp_features.bay_eject && occupied;
2345 eject2 = tp_features.bay_eject2 && occupied2;
2347 if (eject && eject2)
2348 len += sprintf(p + len, "commands:\teject, eject2\n");
2349 else if (eject)
2350 len += sprintf(p + len, "commands:\teject\n");
2351 else if (eject2)
2352 len += sprintf(p + len, "commands:\teject2\n");
2354 return len;
2357 static int bay_write(char *buf)
2359 char *cmd;
2361 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2362 return -ENODEV;
2364 while ((cmd = next_cmd(&buf))) {
2365 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2366 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2367 return -EIO;
2368 } else if (tp_features.bay_eject2 &&
2369 strlencmp(cmd, "eject2") == 0) {
2370 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2371 return -EIO;
2372 } else
2373 return -EINVAL;
2376 return 0;
2379 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2380 .notify = bay_notify,
2381 .handle = &bay_handle,
2382 .type = ACPI_SYSTEM_NOTIFY,
2385 static struct ibm_struct bay_driver_data = {
2386 .name = "bay",
2387 .read = bay_read,
2388 .write = bay_write,
2389 .acpi = &ibm_bay_acpidriver,
2392 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2394 /*************************************************************************
2395 * CMOS subdriver
2398 /* sysfs cmos_command -------------------------------------------------- */
2399 static ssize_t cmos_command_store(struct device *dev,
2400 struct device_attribute *attr,
2401 const char *buf, size_t count)
2403 unsigned long cmos_cmd;
2404 int res;
2406 if (parse_strtoul(buf, 21, &cmos_cmd))
2407 return -EINVAL;
2409 res = issue_thinkpad_cmos_command(cmos_cmd);
2410 return (res)? res : count;
2413 static struct device_attribute dev_attr_cmos_command =
2414 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2416 /* --------------------------------------------------------------------- */
2418 static int __init cmos_init(struct ibm_init_struct *iibm)
2420 int res;
2422 vdbg_printk(TPACPI_DBG_INIT,
2423 "initializing cmos commands subdriver\n");
2425 IBM_ACPIHANDLE_INIT(cmos);
2427 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2428 str_supported(cmos_handle != NULL));
2430 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2431 if (res)
2432 return res;
2434 return (cmos_handle)? 0 : 1;
2437 static void cmos_exit(void)
2439 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2442 static int cmos_read(char *p)
2444 int len = 0;
2446 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2447 R30, R31, T20-22, X20-21 */
2448 if (!cmos_handle)
2449 len += sprintf(p + len, "status:\t\tnot supported\n");
2450 else {
2451 len += sprintf(p + len, "status:\t\tsupported\n");
2452 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2455 return len;
2458 static int cmos_write(char *buf)
2460 char *cmd;
2461 int cmos_cmd, res;
2463 while ((cmd = next_cmd(&buf))) {
2464 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2465 cmos_cmd >= 0 && cmos_cmd <= 21) {
2466 /* cmos_cmd set */
2467 } else
2468 return -EINVAL;
2470 res = issue_thinkpad_cmos_command(cmos_cmd);
2471 if (res)
2472 return res;
2475 return 0;
2478 static struct ibm_struct cmos_driver_data = {
2479 .name = "cmos",
2480 .read = cmos_read,
2481 .write = cmos_write,
2482 .exit = cmos_exit,
2485 /*************************************************************************
2486 * LED subdriver
2489 static enum led_access_mode led_supported;
2491 IBM_HANDLE(led, ec, "SLED", /* 570 */
2492 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2493 "LED", /* all others */
2494 ); /* R30, R31 */
2496 static int __init led_init(struct ibm_init_struct *iibm)
2498 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2500 IBM_ACPIHANDLE_INIT(led);
2502 if (!led_handle)
2503 /* led not supported on R30, R31 */
2504 led_supported = TPACPI_LED_NONE;
2505 else if (strlencmp(led_path, "SLED") == 0)
2506 /* 570 */
2507 led_supported = TPACPI_LED_570;
2508 else if (strlencmp(led_path, "SYSL") == 0)
2509 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2510 led_supported = TPACPI_LED_OLD;
2511 else
2512 /* all others */
2513 led_supported = TPACPI_LED_NEW;
2515 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2516 str_supported(led_supported), led_supported);
2518 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2521 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2523 static int led_read(char *p)
2525 int len = 0;
2527 if (!led_supported) {
2528 len += sprintf(p + len, "status:\t\tnot supported\n");
2529 return len;
2531 len += sprintf(p + len, "status:\t\tsupported\n");
2533 if (led_supported == TPACPI_LED_570) {
2534 /* 570 */
2535 int i, status;
2536 for (i = 0; i < 8; i++) {
2537 if (!acpi_evalf(ec_handle,
2538 &status, "GLED", "dd", 1 << i))
2539 return -EIO;
2540 len += sprintf(p + len, "%d:\t\t%s\n",
2541 i, led_status(status));
2545 len += sprintf(p + len, "commands:\t"
2546 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2548 return len;
2551 /* off, on, blink */
2552 static const int led_sled_arg1[] = { 0, 1, 3 };
2553 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2554 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2555 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2557 static int led_write(char *buf)
2559 char *cmd;
2560 int led, ind, ret;
2562 if (!led_supported)
2563 return -ENODEV;
2565 while ((cmd = next_cmd(&buf))) {
2566 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2567 return -EINVAL;
2569 if (strstr(cmd, "off")) {
2570 ind = 0;
2571 } else if (strstr(cmd, "on")) {
2572 ind = 1;
2573 } else if (strstr(cmd, "blink")) {
2574 ind = 2;
2575 } else
2576 return -EINVAL;
2578 if (led_supported == TPACPI_LED_570) {
2579 /* 570 */
2580 led = 1 << led;
2581 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2582 led, led_sled_arg1[ind]))
2583 return -EIO;
2584 } else if (led_supported == TPACPI_LED_OLD) {
2585 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2586 led = 1 << led;
2587 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2588 if (ret >= 0)
2589 ret =
2590 ec_write(TPACPI_LED_EC_HLBL,
2591 led * led_exp_hlbl[ind]);
2592 if (ret >= 0)
2593 ret =
2594 ec_write(TPACPI_LED_EC_HLCL,
2595 led * led_exp_hlcl[ind]);
2596 if (ret < 0)
2597 return ret;
2598 } else {
2599 /* all others */
2600 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2601 led, led_led_arg1[ind]))
2602 return -EIO;
2606 return 0;
2609 static struct ibm_struct led_driver_data = {
2610 .name = "led",
2611 .read = led_read,
2612 .write = led_write,
2615 /*************************************************************************
2616 * Beep subdriver
2619 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2621 static int __init beep_init(struct ibm_init_struct *iibm)
2623 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2625 IBM_ACPIHANDLE_INIT(beep);
2627 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2628 str_supported(beep_handle != NULL));
2630 return (beep_handle)? 0 : 1;
2633 static int beep_read(char *p)
2635 int len = 0;
2637 if (!beep_handle)
2638 len += sprintf(p + len, "status:\t\tnot supported\n");
2639 else {
2640 len += sprintf(p + len, "status:\t\tsupported\n");
2641 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2644 return len;
2647 static int beep_write(char *buf)
2649 char *cmd;
2650 int beep_cmd;
2652 if (!beep_handle)
2653 return -ENODEV;
2655 while ((cmd = next_cmd(&buf))) {
2656 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2657 beep_cmd >= 0 && beep_cmd <= 17) {
2658 /* beep_cmd set */
2659 } else
2660 return -EINVAL;
2661 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2662 return -EIO;
2665 return 0;
2668 static struct ibm_struct beep_driver_data = {
2669 .name = "beep",
2670 .read = beep_read,
2671 .write = beep_write,
2674 /*************************************************************************
2675 * Thermal subdriver
2678 static enum thermal_access_mode thermal_read_mode;
2680 /* sysfs temp##_input -------------------------------------------------- */
2682 static ssize_t thermal_temp_input_show(struct device *dev,
2683 struct device_attribute *attr,
2684 char *buf)
2686 struct sensor_device_attribute *sensor_attr =
2687 to_sensor_dev_attr(attr);
2688 int idx = sensor_attr->index;
2689 s32 value;
2690 int res;
2692 res = thermal_get_sensor(idx, &value);
2693 if (res)
2694 return res;
2695 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2696 return -ENXIO;
2698 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2701 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2702 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2704 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2705 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2706 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2707 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2708 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2709 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2710 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2711 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2712 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2713 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2714 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2715 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2716 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2717 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2718 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2719 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2720 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2723 #define THERMAL_ATTRS(X) \
2724 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2726 static struct attribute *thermal_temp_input_attr[] = {
2727 THERMAL_ATTRS(8),
2728 THERMAL_ATTRS(9),
2729 THERMAL_ATTRS(10),
2730 THERMAL_ATTRS(11),
2731 THERMAL_ATTRS(12),
2732 THERMAL_ATTRS(13),
2733 THERMAL_ATTRS(14),
2734 THERMAL_ATTRS(15),
2735 THERMAL_ATTRS(0),
2736 THERMAL_ATTRS(1),
2737 THERMAL_ATTRS(2),
2738 THERMAL_ATTRS(3),
2739 THERMAL_ATTRS(4),
2740 THERMAL_ATTRS(5),
2741 THERMAL_ATTRS(6),
2742 THERMAL_ATTRS(7),
2743 NULL
2746 static const struct attribute_group thermal_temp_input16_group = {
2747 .attrs = thermal_temp_input_attr
2750 static const struct attribute_group thermal_temp_input8_group = {
2751 .attrs = &thermal_temp_input_attr[8]
2754 #undef THERMAL_SENSOR_ATTR_TEMP
2755 #undef THERMAL_ATTRS
2757 /* --------------------------------------------------------------------- */
2759 static int __init thermal_init(struct ibm_init_struct *iibm)
2761 u8 t, ta1, ta2;
2762 int i;
2763 int acpi_tmp7;
2764 int res;
2766 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2768 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2770 if (thinkpad_id.ec_model) {
2772 * Direct EC access mode: sensors at registers
2773 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2774 * non-implemented, thermal sensors return 0x80 when
2775 * not available
2778 ta1 = ta2 = 0;
2779 for (i = 0; i < 8; i++) {
2780 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2781 ta1 |= t;
2782 } else {
2783 ta1 = 0;
2784 break;
2786 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2787 ta2 |= t;
2788 } else {
2789 ta1 = 0;
2790 break;
2793 if (ta1 == 0) {
2794 /* This is sheer paranoia, but we handle it anyway */
2795 if (acpi_tmp7) {
2796 printk(IBM_ERR
2797 "ThinkPad ACPI EC access misbehaving, "
2798 "falling back to ACPI TMPx access mode\n");
2799 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2800 } else {
2801 printk(IBM_ERR
2802 "ThinkPad ACPI EC access misbehaving, "
2803 "disabling thermal sensors access\n");
2804 thermal_read_mode = TPACPI_THERMAL_NONE;
2806 } else {
2807 thermal_read_mode =
2808 (ta2 != 0) ?
2809 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2811 } else if (acpi_tmp7) {
2812 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2813 /* 600e/x, 770e, 770x */
2814 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2815 } else {
2816 /* Standard ACPI TMPx access, max 8 sensors */
2817 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2819 } else {
2820 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2821 thermal_read_mode = TPACPI_THERMAL_NONE;
2824 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2825 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2826 thermal_read_mode);
2828 switch(thermal_read_mode) {
2829 case TPACPI_THERMAL_TPEC_16:
2830 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2831 &thermal_temp_input16_group);
2832 if (res)
2833 return res;
2834 break;
2835 case TPACPI_THERMAL_TPEC_8:
2836 case TPACPI_THERMAL_ACPI_TMP07:
2837 case TPACPI_THERMAL_ACPI_UPDT:
2838 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2839 &thermal_temp_input8_group);
2840 if (res)
2841 return res;
2842 break;
2843 case TPACPI_THERMAL_NONE:
2844 default:
2845 return 1;
2848 return 0;
2851 static void thermal_exit(void)
2853 switch(thermal_read_mode) {
2854 case TPACPI_THERMAL_TPEC_16:
2855 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2856 &thermal_temp_input16_group);
2857 break;
2858 case TPACPI_THERMAL_TPEC_8:
2859 case TPACPI_THERMAL_ACPI_TMP07:
2860 case TPACPI_THERMAL_ACPI_UPDT:
2861 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2862 &thermal_temp_input16_group);
2863 break;
2864 case TPACPI_THERMAL_NONE:
2865 default:
2866 break;
2870 /* idx is zero-based */
2871 static int thermal_get_sensor(int idx, s32 *value)
2873 int t;
2874 s8 tmp;
2875 char tmpi[5];
2877 t = TP_EC_THERMAL_TMP0;
2879 switch (thermal_read_mode) {
2880 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2881 case TPACPI_THERMAL_TPEC_16:
2882 if (idx >= 8 && idx <= 15) {
2883 t = TP_EC_THERMAL_TMP8;
2884 idx -= 8;
2886 /* fallthrough */
2887 #endif
2888 case TPACPI_THERMAL_TPEC_8:
2889 if (idx <= 7) {
2890 if (!acpi_ec_read(t + idx, &tmp))
2891 return -EIO;
2892 *value = tmp * 1000;
2893 return 0;
2895 break;
2897 case TPACPI_THERMAL_ACPI_UPDT:
2898 if (idx <= 7) {
2899 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2900 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2901 return -EIO;
2902 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2903 return -EIO;
2904 *value = (t - 2732) * 100;
2905 return 0;
2907 break;
2909 case TPACPI_THERMAL_ACPI_TMP07:
2910 if (idx <= 7) {
2911 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2912 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2913 return -EIO;
2914 if (t > 127 || t < -127)
2915 t = TP_EC_THERMAL_TMP_NA;
2916 *value = t * 1000;
2917 return 0;
2919 break;
2921 case TPACPI_THERMAL_NONE:
2922 default:
2923 return -ENOSYS;
2926 return -EINVAL;
2929 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2931 int res, i;
2932 int n;
2934 n = 8;
2935 i = 0;
2937 if (!s)
2938 return -EINVAL;
2940 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2941 n = 16;
2943 for(i = 0 ; i < n; i++) {
2944 res = thermal_get_sensor(i, &s->temp[i]);
2945 if (res)
2946 return res;
2949 return n;
2952 static int thermal_read(char *p)
2954 int len = 0;
2955 int n, i;
2956 struct ibm_thermal_sensors_struct t;
2958 n = thermal_get_sensors(&t);
2959 if (unlikely(n < 0))
2960 return n;
2962 len += sprintf(p + len, "temperatures:\t");
2964 if (n > 0) {
2965 for (i = 0; i < (n - 1); i++)
2966 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2967 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2968 } else
2969 len += sprintf(p + len, "not supported\n");
2971 return len;
2974 static struct ibm_struct thermal_driver_data = {
2975 .name = "thermal",
2976 .read = thermal_read,
2977 .exit = thermal_exit,
2980 /*************************************************************************
2981 * EC Dump subdriver
2984 static u8 ecdump_regs[256];
2986 static int ecdump_read(char *p)
2988 int len = 0;
2989 int i, j;
2990 u8 v;
2992 len += sprintf(p + len, "EC "
2993 " +00 +01 +02 +03 +04 +05 +06 +07"
2994 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2995 for (i = 0; i < 256; i += 16) {
2996 len += sprintf(p + len, "EC 0x%02x:", i);
2997 for (j = 0; j < 16; j++) {
2998 if (!acpi_ec_read(i + j, &v))
2999 break;
3000 if (v != ecdump_regs[i + j])
3001 len += sprintf(p + len, " *%02x", v);
3002 else
3003 len += sprintf(p + len, " %02x", v);
3004 ecdump_regs[i + j] = v;
3006 len += sprintf(p + len, "\n");
3007 if (j != 16)
3008 break;
3011 /* These are way too dangerous to advertise openly... */
3012 #if 0
3013 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3014 " (<offset> is 00-ff, <value> is 00-ff)\n");
3015 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3016 " (<offset> is 00-ff, <value> is 0-255)\n");
3017 #endif
3018 return len;
3021 static int ecdump_write(char *buf)
3023 char *cmd;
3024 int i, v;
3026 while ((cmd = next_cmd(&buf))) {
3027 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3028 /* i and v set */
3029 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3030 /* i and v set */
3031 } else
3032 return -EINVAL;
3033 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3034 if (!acpi_ec_write(i, v))
3035 return -EIO;
3036 } else
3037 return -EINVAL;
3040 return 0;
3043 static struct ibm_struct ecdump_driver_data = {
3044 .name = "ecdump",
3045 .read = ecdump_read,
3046 .write = ecdump_write,
3047 .flags.experimental = 1,
3050 /*************************************************************************
3051 * Backlight/brightness subdriver
3054 static struct backlight_device *ibm_backlight_device;
3056 static struct backlight_ops ibm_backlight_data = {
3057 .get_brightness = brightness_get,
3058 .update_status = brightness_update_status,
3061 static struct mutex brightness_mutex;
3063 static int __init brightness_init(struct ibm_init_struct *iibm)
3065 int b;
3067 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3069 mutex_init(&brightness_mutex);
3071 if (!brightness_mode) {
3072 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3073 brightness_mode = 2;
3074 else
3075 brightness_mode = 3;
3077 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3078 brightness_mode);
3081 if (brightness_mode > 3)
3082 return -EINVAL;
3084 b = brightness_get(NULL);
3085 if (b < 0)
3086 return 1;
3088 ibm_backlight_device = backlight_device_register(
3089 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3090 &ibm_backlight_data);
3091 if (IS_ERR(ibm_backlight_device)) {
3092 printk(IBM_ERR "Could not register backlight device\n");
3093 return PTR_ERR(ibm_backlight_device);
3095 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
3097 ibm_backlight_device->props.max_brightness = 7;
3098 ibm_backlight_device->props.brightness = b;
3099 backlight_update_status(ibm_backlight_device);
3101 return 0;
3104 static void brightness_exit(void)
3106 if (ibm_backlight_device) {
3107 vdbg_printk(TPACPI_DBG_EXIT,
3108 "calling backlight_device_unregister()\n");
3109 backlight_device_unregister(ibm_backlight_device);
3110 ibm_backlight_device = NULL;
3114 static int brightness_update_status(struct backlight_device *bd)
3116 return brightness_set(
3117 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3118 bd->props.power == FB_BLANK_UNBLANK) ?
3119 bd->props.brightness : 0);
3123 * ThinkPads can read brightness from two places: EC 0x31, or
3124 * CMOS NVRAM byte 0x5E, bits 0-3.
3126 static int brightness_get(struct backlight_device *bd)
3128 u8 lec = 0, lcmos = 0, level = 0;
3130 if (brightness_mode & 1) {
3131 if (!acpi_ec_read(brightness_offset, &lec))
3132 return -EIO;
3133 lec &= 7;
3134 level = lec;
3136 if (brightness_mode & 2) {
3137 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3138 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3139 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3140 level = lcmos;
3143 if (brightness_mode == 3 && lec != lcmos) {
3144 printk(IBM_ERR
3145 "CMOS NVRAM (%u) and EC (%u) do not agree "
3146 "on display brightness level\n",
3147 (unsigned int) lcmos,
3148 (unsigned int) lec);
3149 return -EIO;
3152 return level;
3155 static int brightness_set(int value)
3157 int cmos_cmd, inc, i, res;
3158 int current_value;
3160 if (value > 7)
3161 return -EINVAL;
3163 res = mutex_lock_interruptible(&brightness_mutex);
3164 if (res < 0)
3165 return res;
3167 current_value = brightness_get(NULL);
3168 if (current_value < 0) {
3169 res = current_value;
3170 goto errout;
3173 cmos_cmd = value > current_value ?
3174 TP_CMOS_BRIGHTNESS_UP :
3175 TP_CMOS_BRIGHTNESS_DOWN;
3176 inc = value > current_value ? 1 : -1;
3178 res = 0;
3179 for (i = current_value; i != value; i += inc) {
3180 if ((brightness_mode & 2) &&
3181 issue_thinkpad_cmos_command(cmos_cmd)) {
3182 res = -EIO;
3183 goto errout;
3185 if ((brightness_mode & 1) &&
3186 !acpi_ec_write(brightness_offset, i + inc)) {
3187 res = -EIO;
3188 goto errout;;
3192 errout:
3193 mutex_unlock(&brightness_mutex);
3194 return res;
3197 static int brightness_read(char *p)
3199 int len = 0;
3200 int level;
3202 if ((level = brightness_get(NULL)) < 0) {
3203 len += sprintf(p + len, "level:\t\tunreadable\n");
3204 } else {
3205 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3206 len += sprintf(p + len, "commands:\tup, down\n");
3207 len += sprintf(p + len, "commands:\tlevel <level>"
3208 " (<level> is 0-7)\n");
3211 return len;
3214 static int brightness_write(char *buf)
3216 int level;
3217 int new_level;
3218 char *cmd;
3220 while ((cmd = next_cmd(&buf))) {
3221 if ((level = brightness_get(NULL)) < 0)
3222 return level;
3223 level &= 7;
3225 if (strlencmp(cmd, "up") == 0) {
3226 new_level = level == 7 ? 7 : level + 1;
3227 } else if (strlencmp(cmd, "down") == 0) {
3228 new_level = level == 0 ? 0 : level - 1;
3229 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3230 new_level >= 0 && new_level <= 7) {
3231 /* new_level set */
3232 } else
3233 return -EINVAL;
3235 brightness_set(new_level);
3238 return 0;
3241 static struct ibm_struct brightness_driver_data = {
3242 .name = "brightness",
3243 .read = brightness_read,
3244 .write = brightness_write,
3245 .exit = brightness_exit,
3248 /*************************************************************************
3249 * Volume subdriver
3252 static int volume_read(char *p)
3254 int len = 0;
3255 u8 level;
3257 if (!acpi_ec_read(volume_offset, &level)) {
3258 len += sprintf(p + len, "level:\t\tunreadable\n");
3259 } else {
3260 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3261 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3262 len += sprintf(p + len, "commands:\tup, down, mute\n");
3263 len += sprintf(p + len, "commands:\tlevel <level>"
3264 " (<level> is 0-15)\n");
3267 return len;
3270 static int volume_write(char *buf)
3272 int cmos_cmd, inc, i;
3273 u8 level, mute;
3274 int new_level, new_mute;
3275 char *cmd;
3277 while ((cmd = next_cmd(&buf))) {
3278 if (!acpi_ec_read(volume_offset, &level))
3279 return -EIO;
3280 new_mute = mute = level & 0x40;
3281 new_level = level = level & 0xf;
3283 if (strlencmp(cmd, "up") == 0) {
3284 if (mute)
3285 new_mute = 0;
3286 else
3287 new_level = level == 15 ? 15 : level + 1;
3288 } else if (strlencmp(cmd, "down") == 0) {
3289 if (mute)
3290 new_mute = 0;
3291 else
3292 new_level = level == 0 ? 0 : level - 1;
3293 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3294 new_level >= 0 && new_level <= 15) {
3295 /* new_level set */
3296 } else if (strlencmp(cmd, "mute") == 0) {
3297 new_mute = 0x40;
3298 } else
3299 return -EINVAL;
3301 if (new_level != level) { /* mute doesn't change */
3302 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3303 inc = new_level > level ? 1 : -1;
3305 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3306 !acpi_ec_write(volume_offset, level)))
3307 return -EIO;
3309 for (i = level; i != new_level; i += inc)
3310 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3311 !acpi_ec_write(volume_offset, i + inc))
3312 return -EIO;
3314 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3315 !acpi_ec_write(volume_offset,
3316 new_level + mute)))
3317 return -EIO;
3320 if (new_mute != mute) { /* level doesn't change */
3321 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3323 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3324 !acpi_ec_write(volume_offset, level + new_mute))
3325 return -EIO;
3329 return 0;
3332 static struct ibm_struct volume_driver_data = {
3333 .name = "volume",
3334 .read = volume_read,
3335 .write = volume_write,
3338 /*************************************************************************
3339 * Fan subdriver
3343 * FAN ACCESS MODES
3345 * TPACPI_FAN_RD_ACPI_GFAN:
3346 * ACPI GFAN method: returns fan level
3348 * see TPACPI_FAN_WR_ACPI_SFAN
3349 * EC 0x2f (HFSP) not available if GFAN exists
3351 * TPACPI_FAN_WR_ACPI_SFAN:
3352 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3354 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3355 * it for writing.
3357 * TPACPI_FAN_WR_TPEC:
3358 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3359 * Supported on almost all ThinkPads
3361 * Fan speed changes of any sort (including those caused by the
3362 * disengaged mode) are usually done slowly by the firmware as the
3363 * maximum ammount of fan duty cycle change per second seems to be
3364 * limited.
3366 * Reading is not available if GFAN exists.
3367 * Writing is not available if SFAN exists.
3369 * Bits
3370 * 7 automatic mode engaged;
3371 * (default operation mode of the ThinkPad)
3372 * fan level is ignored in this mode.
3373 * 6 full speed mode (takes precedence over bit 7);
3374 * not available on all thinkpads. May disable
3375 * the tachometer while the fan controller ramps up
3376 * the speed (which can take up to a few *minutes*).
3377 * Speeds up fan to 100% duty-cycle, which is far above
3378 * the standard RPM levels. It is not impossible that
3379 * it could cause hardware damage.
3380 * 5-3 unused in some models. Extra bits for fan level
3381 * in others, but still useless as all values above
3382 * 7 map to the same speed as level 7 in these models.
3383 * 2-0 fan level (0..7 usually)
3384 * 0x00 = stop
3385 * 0x07 = max (set when temperatures critical)
3386 * Some ThinkPads may have other levels, see
3387 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3389 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3390 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3391 * does so, its initial value is meaningless (0x07).
3393 * For firmware bugs, refer to:
3394 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3396 * ----
3398 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3399 * Main fan tachometer reading (in RPM)
3401 * This register is present on all ThinkPads with a new-style EC, and
3402 * it is known not to be present on the A21m/e, and T22, as there is
3403 * something else in offset 0x84 according to the ACPI DSDT. Other
3404 * ThinkPads from this same time period (and earlier) probably lack the
3405 * tachometer as well.
3407 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3408 * was never fixed by IBM to report the EC firmware version string
3409 * probably support the tachometer (like the early X models), so
3410 * detecting it is quite hard. We need more data to know for sure.
3412 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3413 * might result.
3415 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3416 * mode.
3418 * For firmware bugs, refer to:
3419 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3421 * TPACPI_FAN_WR_ACPI_FANS:
3422 * ThinkPad X31, X40, X41. Not available in the X60.
3424 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3425 * high speed. ACPI DSDT seems to map these three speeds to levels
3426 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3427 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3429 * The speeds are stored on handles
3430 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3432 * There are three default speed sets, acessible as handles:
3433 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3435 * ACPI DSDT switches which set is in use depending on various
3436 * factors.
3438 * TPACPI_FAN_WR_TPEC is also available and should be used to
3439 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3440 * but the ACPI tables just mention level 7.
3443 static enum fan_status_access_mode fan_status_access_mode;
3444 static enum fan_control_access_mode fan_control_access_mode;
3445 static enum fan_control_commands fan_control_commands;
3447 static u8 fan_control_initial_status;
3448 static u8 fan_control_desired_level;
3450 static void fan_watchdog_fire(struct work_struct *ignored);
3451 static int fan_watchdog_maxinterval;
3452 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3454 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3455 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3456 "\\FSPD", /* 600e/x, 770e, 770x */
3457 ); /* all others */
3458 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3459 "JFNS", /* 770x-JL */
3460 ); /* all others */
3463 * SYSFS fan layout: hwmon compatible (device)
3465 * pwm*_enable:
3466 * 0: "disengaged" mode
3467 * 1: manual mode
3468 * 2: native EC "auto" mode (recommended, hardware default)
3470 * pwm*: set speed in manual mode, ignored otherwise.
3471 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3472 * interpolation.
3474 * fan*_input: tachometer reading, RPM
3477 * SYSFS fan layout: extensions
3479 * fan_watchdog (driver):
3480 * fan watchdog interval in seconds, 0 disables (default), max 120
3483 /* sysfs fan pwm1_enable ----------------------------------------------- */
3484 static ssize_t fan_pwm1_enable_show(struct device *dev,
3485 struct device_attribute *attr,
3486 char *buf)
3488 int res, mode;
3489 u8 status;
3491 res = fan_get_status_safe(&status);
3492 if (res)
3493 return res;
3495 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3496 if (status != fan_control_initial_status) {
3497 tp_features.fan_ctrl_status_undef = 0;
3498 } else {
3499 /* Return most likely status. In fact, it
3500 * might be the only possible status */
3501 status = TP_EC_FAN_AUTO;
3505 if (status & TP_EC_FAN_FULLSPEED) {
3506 mode = 0;
3507 } else if (status & TP_EC_FAN_AUTO) {
3508 mode = 2;
3509 } else
3510 mode = 1;
3512 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3515 static ssize_t fan_pwm1_enable_store(struct device *dev,
3516 struct device_attribute *attr,
3517 const char *buf, size_t count)
3519 unsigned long t;
3520 int res, level;
3522 if (parse_strtoul(buf, 2, &t))
3523 return -EINVAL;
3525 switch (t) {
3526 case 0:
3527 level = TP_EC_FAN_FULLSPEED;
3528 break;
3529 case 1:
3530 level = TPACPI_FAN_LAST_LEVEL;
3531 break;
3532 case 2:
3533 level = TP_EC_FAN_AUTO;
3534 break;
3535 case 3:
3536 /* reserved for software-controlled auto mode */
3537 return -ENOSYS;
3538 default:
3539 return -EINVAL;
3542 res = fan_set_level_safe(level);
3543 if (res == -ENXIO)
3544 return -EINVAL;
3545 else if (res < 0)
3546 return res;
3548 fan_watchdog_reset();
3550 return count;
3553 static struct device_attribute dev_attr_fan_pwm1_enable =
3554 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3555 fan_pwm1_enable_show, fan_pwm1_enable_store);
3557 /* sysfs fan pwm1 ------------------------------------------------------ */
3558 static ssize_t fan_pwm1_show(struct device *dev,
3559 struct device_attribute *attr,
3560 char *buf)
3562 int res;
3563 u8 status;
3565 res = fan_get_status_safe(&status);
3566 if (res)
3567 return res;
3569 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3570 if (status != fan_control_initial_status) {
3571 tp_features.fan_ctrl_status_undef = 0;
3572 } else {
3573 status = TP_EC_FAN_AUTO;
3577 if ((status &
3578 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3579 status = fan_control_desired_level;
3581 if (status > 7)
3582 status = 7;
3584 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3587 static ssize_t fan_pwm1_store(struct device *dev,
3588 struct device_attribute *attr,
3589 const char *buf, size_t count)
3591 unsigned long s;
3592 int rc;
3593 u8 status, newlevel;
3595 if (parse_strtoul(buf, 255, &s))
3596 return -EINVAL;
3598 /* scale down from 0-255 to 0-7 */
3599 newlevel = (s >> 5) & 0x07;
3601 rc = mutex_lock_interruptible(&fan_mutex);
3602 if (rc < 0)
3603 return rc;
3605 rc = fan_get_status(&status);
3606 if (!rc && (status &
3607 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3608 rc = fan_set_level(newlevel);
3609 if (rc == -ENXIO)
3610 rc = -EINVAL;
3611 else if (!rc) {
3612 fan_update_desired_level(newlevel);
3613 fan_watchdog_reset();
3617 mutex_unlock(&fan_mutex);
3618 return (rc)? rc : count;
3621 static struct device_attribute dev_attr_fan_pwm1 =
3622 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3623 fan_pwm1_show, fan_pwm1_store);
3625 /* sysfs fan fan1_input ------------------------------------------------ */
3626 static ssize_t fan_fan1_input_show(struct device *dev,
3627 struct device_attribute *attr,
3628 char *buf)
3630 int res;
3631 unsigned int speed;
3633 res = fan_get_speed(&speed);
3634 if (res < 0)
3635 return res;
3637 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3640 static struct device_attribute dev_attr_fan_fan1_input =
3641 __ATTR(fan1_input, S_IRUGO,
3642 fan_fan1_input_show, NULL);
3644 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3645 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3646 char *buf)
3648 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3651 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3652 const char *buf, size_t count)
3654 unsigned long t;
3656 if (parse_strtoul(buf, 120, &t))
3657 return -EINVAL;
3659 if (!fan_control_allowed)
3660 return -EPERM;
3662 fan_watchdog_maxinterval = t;
3663 fan_watchdog_reset();
3665 return count;
3668 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3669 fan_fan_watchdog_show, fan_fan_watchdog_store);
3671 /* --------------------------------------------------------------------- */
3672 static struct attribute *fan_attributes[] = {
3673 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3674 &dev_attr_fan_fan1_input.attr,
3675 NULL
3678 static const struct attribute_group fan_attr_group = {
3679 .attrs = fan_attributes,
3682 static int __init fan_init(struct ibm_init_struct *iibm)
3684 int rc;
3686 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3688 mutex_init(&fan_mutex);
3689 fan_status_access_mode = TPACPI_FAN_NONE;
3690 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3691 fan_control_commands = 0;
3692 fan_watchdog_maxinterval = 0;
3693 tp_features.fan_ctrl_status_undef = 0;
3694 fan_control_desired_level = 7;
3696 IBM_ACPIHANDLE_INIT(fans);
3697 IBM_ACPIHANDLE_INIT(gfan);
3698 IBM_ACPIHANDLE_INIT(sfan);
3700 if (gfan_handle) {
3701 /* 570, 600e/x, 770e, 770x */
3702 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3703 } else {
3704 /* all other ThinkPads: note that even old-style
3705 * ThinkPad ECs supports the fan control register */
3706 if (likely(acpi_ec_read(fan_status_offset,
3707 &fan_control_initial_status))) {
3708 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3710 /* In some ThinkPads, neither the EC nor the ACPI
3711 * DSDT initialize the fan status, and it ends up
3712 * being set to 0x07 when it *could* be either
3713 * 0x07 or 0x80.
3715 * Enable for TP-1Y (T43), TP-78 (R51e),
3716 * TP-76 (R52), TP-70 (T43, R52), which are known
3717 * to be buggy. */
3718 if (fan_control_initial_status == 0x07) {
3719 switch (thinkpad_id.ec_model) {
3720 case 0x5931: /* TP-1Y */
3721 case 0x3837: /* TP-78 */
3722 case 0x3637: /* TP-76 */
3723 case 0x3037: /* TP-70 */
3724 printk(IBM_NOTICE
3725 "fan_init: initial fan status is "
3726 "unknown, assuming it is in auto "
3727 "mode\n");
3728 tp_features.fan_ctrl_status_undef = 1;
3732 } else {
3733 printk(IBM_ERR
3734 "ThinkPad ACPI EC access misbehaving, "
3735 "fan status and control unavailable\n");
3736 return 1;
3740 if (sfan_handle) {
3741 /* 570, 770x-JL */
3742 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3743 fan_control_commands |=
3744 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3745 } else {
3746 if (!gfan_handle) {
3747 /* gfan without sfan means no fan control */
3748 /* all other models implement TP EC 0x2f control */
3750 if (fans_handle) {
3751 /* X31, X40, X41 */
3752 fan_control_access_mode =
3753 TPACPI_FAN_WR_ACPI_FANS;
3754 fan_control_commands |=
3755 TPACPI_FAN_CMD_SPEED |
3756 TPACPI_FAN_CMD_LEVEL |
3757 TPACPI_FAN_CMD_ENABLE;
3758 } else {
3759 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3760 fan_control_commands |=
3761 TPACPI_FAN_CMD_LEVEL |
3762 TPACPI_FAN_CMD_ENABLE;
3767 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3768 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3769 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3770 fan_status_access_mode, fan_control_access_mode);
3772 /* fan control master switch */
3773 if (!fan_control_allowed) {
3774 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3775 fan_control_commands = 0;
3776 dbg_printk(TPACPI_DBG_INIT,
3777 "fan control features disabled by parameter\n");
3780 /* update fan_control_desired_level */
3781 if (fan_status_access_mode != TPACPI_FAN_NONE)
3782 fan_get_status_safe(NULL);
3784 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3785 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3786 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3787 &fan_attr_group);
3788 if (!(rc < 0))
3789 rc = driver_create_file(&tpacpi_pdriver.driver,
3790 &driver_attr_fan_watchdog);
3791 if (rc < 0)
3792 return rc;
3793 return 0;
3794 } else
3795 return 1;
3799 * Call with fan_mutex held
3801 static void fan_update_desired_level(u8 status)
3803 if ((status &
3804 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3805 if (status > 7)
3806 fan_control_desired_level = 7;
3807 else
3808 fan_control_desired_level = status;
3812 static int fan_get_status(u8 *status)
3814 u8 s;
3816 /* TODO:
3817 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3819 switch (fan_status_access_mode) {
3820 case TPACPI_FAN_RD_ACPI_GFAN:
3821 /* 570, 600e/x, 770e, 770x */
3823 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3824 return -EIO;
3826 if (likely(status))
3827 *status = s & 0x07;
3829 break;
3831 case TPACPI_FAN_RD_TPEC:
3832 /* all except 570, 600e/x, 770e, 770x */
3833 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3834 return -EIO;
3836 if (likely(status))
3837 *status = s;
3839 break;
3841 default:
3842 return -ENXIO;
3845 return 0;
3848 static int fan_get_status_safe(u8 *status)
3850 int rc;
3851 u8 s;
3853 rc = mutex_lock_interruptible(&fan_mutex);
3854 if (rc < 0)
3855 return rc;
3856 rc = fan_get_status(&s);
3857 if (!rc)
3858 fan_update_desired_level(s);
3859 mutex_unlock(&fan_mutex);
3861 if (status)
3862 *status = s;
3864 return rc;
3867 static void fan_exit(void)
3869 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3871 /* FIXME: can we really do this unconditionally? */
3872 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3873 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3875 cancel_delayed_work(&fan_watchdog_task);
3876 flush_scheduled_work();
3879 static int fan_get_speed(unsigned int *speed)
3881 u8 hi, lo;
3883 switch (fan_status_access_mode) {
3884 case TPACPI_FAN_RD_TPEC:
3885 /* all except 570, 600e/x, 770e, 770x */
3886 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3887 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3888 return -EIO;
3890 if (likely(speed))
3891 *speed = (hi << 8) | lo;
3893 break;
3895 default:
3896 return -ENXIO;
3899 return 0;
3902 static void fan_watchdog_fire(struct work_struct *ignored)
3904 int rc;
3906 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3907 rc = fan_set_enable();
3908 if (rc < 0) {
3909 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3910 "will try again later...\n", -rc);
3911 /* reschedule for later */
3912 fan_watchdog_reset();
3916 static void fan_watchdog_reset(void)
3918 static int fan_watchdog_active;
3920 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3921 return;
3923 if (fan_watchdog_active)
3924 cancel_delayed_work(&fan_watchdog_task);
3926 if (fan_watchdog_maxinterval > 0) {
3927 fan_watchdog_active = 1;
3928 if (!schedule_delayed_work(&fan_watchdog_task,
3929 msecs_to_jiffies(fan_watchdog_maxinterval
3930 * 1000))) {
3931 printk(IBM_ERR "failed to schedule the fan watchdog, "
3932 "watchdog will not trigger\n");
3934 } else
3935 fan_watchdog_active = 0;
3938 static int fan_set_level(int level)
3940 if (!fan_control_allowed)
3941 return -EPERM;
3943 switch (fan_control_access_mode) {
3944 case TPACPI_FAN_WR_ACPI_SFAN:
3945 if (level >= 0 && level <= 7) {
3946 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3947 return -EIO;
3948 } else
3949 return -EINVAL;
3950 break;
3952 case TPACPI_FAN_WR_ACPI_FANS:
3953 case TPACPI_FAN_WR_TPEC:
3954 if ((level != TP_EC_FAN_AUTO) &&
3955 (level != TP_EC_FAN_FULLSPEED) &&
3956 ((level < 0) || (level > 7)))
3957 return -EINVAL;
3959 /* safety net should the EC not support AUTO
3960 * or FULLSPEED mode bits and just ignore them */
3961 if (level & TP_EC_FAN_FULLSPEED)
3962 level |= 7; /* safety min speed 7 */
3963 else if (level & TP_EC_FAN_FULLSPEED)
3964 level |= 4; /* safety min speed 4 */
3966 if (!acpi_ec_write(fan_status_offset, level))
3967 return -EIO;
3968 else
3969 tp_features.fan_ctrl_status_undef = 0;
3970 break;
3972 default:
3973 return -ENXIO;
3975 return 0;
3978 static int fan_set_level_safe(int level)
3980 int rc;
3982 if (!fan_control_allowed)
3983 return -EPERM;
3985 rc = mutex_lock_interruptible(&fan_mutex);
3986 if (rc < 0)
3987 return rc;
3989 if (level == TPACPI_FAN_LAST_LEVEL)
3990 level = fan_control_desired_level;
3992 rc = fan_set_level(level);
3993 if (!rc)
3994 fan_update_desired_level(level);
3996 mutex_unlock(&fan_mutex);
3997 return rc;
4000 static int fan_set_enable(void)
4002 u8 s;
4003 int rc;
4005 if (!fan_control_allowed)
4006 return -EPERM;
4008 rc = mutex_lock_interruptible(&fan_mutex);
4009 if (rc < 0)
4010 return rc;
4012 switch (fan_control_access_mode) {
4013 case TPACPI_FAN_WR_ACPI_FANS:
4014 case TPACPI_FAN_WR_TPEC:
4015 rc = fan_get_status(&s);
4016 if (rc < 0)
4017 break;
4019 /* Don't go out of emergency fan mode */
4020 if (s != 7) {
4021 s &= 0x07;
4022 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4025 if (!acpi_ec_write(fan_status_offset, s))
4026 rc = -EIO;
4027 else {
4028 tp_features.fan_ctrl_status_undef = 0;
4029 rc = 0;
4031 break;
4033 case TPACPI_FAN_WR_ACPI_SFAN:
4034 rc = fan_get_status(&s);
4035 if (rc < 0)
4036 break;
4038 s &= 0x07;
4040 /* Set fan to at least level 4 */
4041 s |= 4;
4043 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4044 rc= -EIO;
4045 else
4046 rc = 0;
4047 break;
4049 default:
4050 rc = -ENXIO;
4053 mutex_unlock(&fan_mutex);
4054 return rc;
4057 static int fan_set_disable(void)
4059 int rc;
4061 if (!fan_control_allowed)
4062 return -EPERM;
4064 rc = mutex_lock_interruptible(&fan_mutex);
4065 if (rc < 0)
4066 return rc;
4068 rc = 0;
4069 switch (fan_control_access_mode) {
4070 case TPACPI_FAN_WR_ACPI_FANS:
4071 case TPACPI_FAN_WR_TPEC:
4072 if (!acpi_ec_write(fan_status_offset, 0x00))
4073 rc = -EIO;
4074 else {
4075 fan_control_desired_level = 0;
4076 tp_features.fan_ctrl_status_undef = 0;
4078 break;
4080 case TPACPI_FAN_WR_ACPI_SFAN:
4081 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4082 rc = -EIO;
4083 else
4084 fan_control_desired_level = 0;
4085 break;
4087 default:
4088 rc = -ENXIO;
4092 mutex_unlock(&fan_mutex);
4093 return rc;
4096 static int fan_set_speed(int speed)
4098 int rc;
4100 if (!fan_control_allowed)
4101 return -EPERM;
4103 rc = mutex_lock_interruptible(&fan_mutex);
4104 if (rc < 0)
4105 return rc;
4107 rc = 0;
4108 switch (fan_control_access_mode) {
4109 case TPACPI_FAN_WR_ACPI_FANS:
4110 if (speed >= 0 && speed <= 65535) {
4111 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4112 speed, speed, speed))
4113 rc = -EIO;
4114 } else
4115 rc = -EINVAL;
4116 break;
4118 default:
4119 rc = -ENXIO;
4122 mutex_unlock(&fan_mutex);
4123 return rc;
4126 static int fan_read(char *p)
4128 int len = 0;
4129 int rc;
4130 u8 status;
4131 unsigned int speed = 0;
4133 switch (fan_status_access_mode) {
4134 case TPACPI_FAN_RD_ACPI_GFAN:
4135 /* 570, 600e/x, 770e, 770x */
4136 if ((rc = fan_get_status_safe(&status)) < 0)
4137 return rc;
4139 len += sprintf(p + len, "status:\t\t%s\n"
4140 "level:\t\t%d\n",
4141 (status != 0) ? "enabled" : "disabled", status);
4142 break;
4144 case TPACPI_FAN_RD_TPEC:
4145 /* all except 570, 600e/x, 770e, 770x */
4146 if ((rc = fan_get_status_safe(&status)) < 0)
4147 return rc;
4149 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4150 if (status != fan_control_initial_status)
4151 tp_features.fan_ctrl_status_undef = 0;
4152 else
4153 /* Return most likely status. In fact, it
4154 * might be the only possible status */
4155 status = TP_EC_FAN_AUTO;
4158 len += sprintf(p + len, "status:\t\t%s\n",
4159 (status != 0) ? "enabled" : "disabled");
4161 if ((rc = fan_get_speed(&speed)) < 0)
4162 return rc;
4164 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4166 if (status & TP_EC_FAN_FULLSPEED)
4167 /* Disengaged mode takes precedence */
4168 len += sprintf(p + len, "level:\t\tdisengaged\n");
4169 else if (status & TP_EC_FAN_AUTO)
4170 len += sprintf(p + len, "level:\t\tauto\n");
4171 else
4172 len += sprintf(p + len, "level:\t\t%d\n", status);
4173 break;
4175 case TPACPI_FAN_NONE:
4176 default:
4177 len += sprintf(p + len, "status:\t\tnot supported\n");
4180 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
4181 len += sprintf(p + len, "commands:\tlevel <level>");
4183 switch (fan_control_access_mode) {
4184 case TPACPI_FAN_WR_ACPI_SFAN:
4185 len += sprintf(p + len, " (<level> is 0-7)\n");
4186 break;
4188 default:
4189 len += sprintf(p + len, " (<level> is 0-7, "
4190 "auto, disengaged, full-speed)\n");
4191 break;
4195 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
4196 len += sprintf(p + len, "commands:\tenable, disable\n"
4197 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4198 "1-120 (seconds))\n");
4200 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
4201 len += sprintf(p + len, "commands:\tspeed <speed>"
4202 " (<speed> is 0-65535)\n");
4204 return len;
4207 static int fan_write_cmd_level(const char *cmd, int *rc)
4209 int level;
4211 if (strlencmp(cmd, "level auto") == 0)
4212 level = TP_EC_FAN_AUTO;
4213 else if ((strlencmp(cmd, "level disengaged") == 0) |
4214 (strlencmp(cmd, "level full-speed") == 0))
4215 level = TP_EC_FAN_FULLSPEED;
4216 else if (sscanf(cmd, "level %d", &level) != 1)
4217 return 0;
4219 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4220 printk(IBM_ERR "level command accepted for unsupported "
4221 "access mode %d", fan_control_access_mode);
4223 return 1;
4226 static int fan_write_cmd_enable(const char *cmd, int *rc)
4228 if (strlencmp(cmd, "enable") != 0)
4229 return 0;
4231 if ((*rc = fan_set_enable()) == -ENXIO)
4232 printk(IBM_ERR "enable command accepted for unsupported "
4233 "access mode %d", fan_control_access_mode);
4235 return 1;
4238 static int fan_write_cmd_disable(const char *cmd, int *rc)
4240 if (strlencmp(cmd, "disable") != 0)
4241 return 0;
4243 if ((*rc = fan_set_disable()) == -ENXIO)
4244 printk(IBM_ERR "disable command accepted for unsupported "
4245 "access mode %d", fan_control_access_mode);
4247 return 1;
4250 static int fan_write_cmd_speed(const char *cmd, int *rc)
4252 int speed;
4254 /* TODO:
4255 * Support speed <low> <medium> <high> ? */
4257 if (sscanf(cmd, "speed %d", &speed) != 1)
4258 return 0;
4260 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4261 printk(IBM_ERR "speed command accepted for unsupported "
4262 "access mode %d", fan_control_access_mode);
4264 return 1;
4267 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4269 int interval;
4271 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4272 return 0;
4274 if (interval < 0 || interval > 120)
4275 *rc = -EINVAL;
4276 else
4277 fan_watchdog_maxinterval = interval;
4279 return 1;
4282 static int fan_write(char *buf)
4284 char *cmd;
4285 int rc = 0;
4287 while (!rc && (cmd = next_cmd(&buf))) {
4288 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4289 fan_write_cmd_level(cmd, &rc)) &&
4290 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4291 (fan_write_cmd_enable(cmd, &rc) ||
4292 fan_write_cmd_disable(cmd, &rc) ||
4293 fan_write_cmd_watchdog(cmd, &rc))) &&
4294 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4295 fan_write_cmd_speed(cmd, &rc))
4297 rc = -EINVAL;
4298 else if (!rc)
4299 fan_watchdog_reset();
4302 return rc;
4305 static struct ibm_struct fan_driver_data = {
4306 .name = "fan",
4307 .read = fan_read,
4308 .write = fan_write,
4309 .exit = fan_exit,
4312 /****************************************************************************
4313 ****************************************************************************
4315 * Infrastructure
4317 ****************************************************************************
4318 ****************************************************************************/
4320 /* /proc support */
4321 static struct proc_dir_entry *proc_dir;
4323 /* Subdriver registry */
4324 static LIST_HEAD(tpacpi_all_drivers);
4328 * Module and infrastructure proble, init and exit handling
4331 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4332 static const char * __init str_supported(int is_supported)
4334 static char text_unsupported[] __initdata = "not supported";
4336 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4338 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4340 static int __init ibm_init(struct ibm_init_struct *iibm)
4342 int ret;
4343 struct ibm_struct *ibm = iibm->data;
4344 struct proc_dir_entry *entry;
4346 BUG_ON(ibm == NULL);
4348 INIT_LIST_HEAD(&ibm->all_drivers);
4350 if (ibm->flags.experimental && !experimental)
4351 return 0;
4353 dbg_printk(TPACPI_DBG_INIT,
4354 "probing for %s\n", ibm->name);
4356 if (iibm->init) {
4357 ret = iibm->init(iibm);
4358 if (ret > 0)
4359 return 0; /* probe failed */
4360 if (ret)
4361 return ret;
4363 ibm->flags.init_called = 1;
4366 if (ibm->acpi) {
4367 if (ibm->acpi->hid) {
4368 ret = register_tpacpi_subdriver(ibm);
4369 if (ret)
4370 goto err_out;
4373 if (ibm->acpi->notify) {
4374 ret = setup_acpi_notify(ibm);
4375 if (ret == -ENODEV) {
4376 printk(IBM_NOTICE "disabling subdriver %s\n",
4377 ibm->name);
4378 ret = 0;
4379 goto err_out;
4381 if (ret < 0)
4382 goto err_out;
4386 dbg_printk(TPACPI_DBG_INIT,
4387 "%s installed\n", ibm->name);
4389 if (ibm->read) {
4390 entry = create_proc_entry(ibm->name,
4391 S_IFREG | S_IRUGO | S_IWUSR,
4392 proc_dir);
4393 if (!entry) {
4394 printk(IBM_ERR "unable to create proc entry %s\n",
4395 ibm->name);
4396 ret = -ENODEV;
4397 goto err_out;
4399 entry->owner = THIS_MODULE;
4400 entry->data = ibm;
4401 entry->read_proc = &dispatch_procfs_read;
4402 if (ibm->write)
4403 entry->write_proc = &dispatch_procfs_write;
4404 ibm->flags.proc_created = 1;
4407 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4409 return 0;
4411 err_out:
4412 dbg_printk(TPACPI_DBG_INIT,
4413 "%s: at error exit path with result %d\n",
4414 ibm->name, ret);
4416 ibm_exit(ibm);
4417 return (ret < 0)? ret : 0;
4420 static void ibm_exit(struct ibm_struct *ibm)
4422 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4424 list_del_init(&ibm->all_drivers);
4426 if (ibm->flags.acpi_notify_installed) {
4427 dbg_printk(TPACPI_DBG_EXIT,
4428 "%s: acpi_remove_notify_handler\n", ibm->name);
4429 BUG_ON(!ibm->acpi);
4430 acpi_remove_notify_handler(*ibm->acpi->handle,
4431 ibm->acpi->type,
4432 dispatch_acpi_notify);
4433 ibm->flags.acpi_notify_installed = 0;
4434 ibm->flags.acpi_notify_installed = 0;
4437 if (ibm->flags.proc_created) {
4438 dbg_printk(TPACPI_DBG_EXIT,
4439 "%s: remove_proc_entry\n", ibm->name);
4440 remove_proc_entry(ibm->name, proc_dir);
4441 ibm->flags.proc_created = 0;
4444 if (ibm->flags.acpi_driver_registered) {
4445 dbg_printk(TPACPI_DBG_EXIT,
4446 "%s: acpi_bus_unregister_driver\n", ibm->name);
4447 BUG_ON(!ibm->acpi);
4448 acpi_bus_unregister_driver(ibm->acpi->driver);
4449 kfree(ibm->acpi->driver);
4450 ibm->acpi->driver = NULL;
4451 ibm->flags.acpi_driver_registered = 0;
4454 if (ibm->flags.init_called && ibm->exit) {
4455 ibm->exit();
4456 ibm->flags.init_called = 0;
4459 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4462 /* Probing */
4464 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
4466 struct dmi_device *dev = NULL;
4467 char ec_fw_string[18];
4469 if (!tp)
4470 return;
4472 memset(tp, 0, sizeof(*tp));
4474 if (dmi_name_in_vendors("IBM"))
4475 tp->vendor = PCI_VENDOR_ID_IBM;
4476 else if (dmi_name_in_vendors("LENOVO"))
4477 tp->vendor = PCI_VENDOR_ID_LENOVO;
4478 else
4479 return;
4481 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4482 GFP_KERNEL);
4483 if (!tp->bios_version_str)
4484 return;
4485 tp->bios_model = tp->bios_version_str[0]
4486 | (tp->bios_version_str[1] << 8);
4489 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4490 * X32 or newer, all Z series; Some models must have an
4491 * up-to-date BIOS or they will not be detected.
4493 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4495 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4496 if (sscanf(dev->name,
4497 "IBM ThinkPad Embedded Controller -[%17c",
4498 ec_fw_string) == 1) {
4499 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4500 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4502 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4503 tp->ec_model = ec_fw_string[0]
4504 | (ec_fw_string[1] << 8);
4505 break;
4509 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4510 GFP_KERNEL);
4511 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4512 kfree(tp->model_str);
4513 tp->model_str = NULL;
4517 static int __init probe_for_thinkpad(void)
4519 int is_thinkpad;
4521 if (acpi_disabled)
4522 return -ENODEV;
4525 * Non-ancient models have better DMI tagging, but very old models
4526 * don't.
4528 is_thinkpad = (thinkpad_id.model_str != NULL);
4530 /* ec is required because many other handles are relative to it */
4531 IBM_ACPIHANDLE_INIT(ec);
4532 if (!ec_handle) {
4533 if (is_thinkpad)
4534 printk(IBM_ERR
4535 "Not yet supported ThinkPad detected!\n");
4536 return -ENODEV;
4540 * Risks a regression on very old machines, but reduces potential
4541 * false positives a damn great deal
4543 if (!is_thinkpad)
4544 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
4546 if (!is_thinkpad && !force_load)
4547 return -ENODEV;
4549 return 0;
4553 /* Module init, exit, parameters */
4555 static struct ibm_init_struct ibms_init[] __initdata = {
4557 .init = thinkpad_acpi_driver_init,
4558 .data = &thinkpad_acpi_driver_data,
4561 .init = hotkey_init,
4562 .data = &hotkey_driver_data,
4565 .init = bluetooth_init,
4566 .data = &bluetooth_driver_data,
4569 .init = wan_init,
4570 .data = &wan_driver_data,
4573 .init = video_init,
4574 .data = &video_driver_data,
4577 .init = light_init,
4578 .data = &light_driver_data,
4580 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4582 .init = dock_init,
4583 .data = &dock_driver_data[0],
4586 .init = dock_init2,
4587 .data = &dock_driver_data[1],
4589 #endif
4590 #ifdef CONFIG_THINKPAD_ACPI_BAY
4592 .init = bay_init,
4593 .data = &bay_driver_data,
4595 #endif
4597 .init = cmos_init,
4598 .data = &cmos_driver_data,
4601 .init = led_init,
4602 .data = &led_driver_data,
4605 .init = beep_init,
4606 .data = &beep_driver_data,
4609 .init = thermal_init,
4610 .data = &thermal_driver_data,
4613 .data = &ecdump_driver_data,
4616 .init = brightness_init,
4617 .data = &brightness_driver_data,
4620 .data = &volume_driver_data,
4623 .init = fan_init,
4624 .data = &fan_driver_data,
4628 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4630 unsigned int i;
4631 struct ibm_struct *ibm;
4633 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4634 ibm = ibms_init[i].data;
4635 BUG_ON(ibm == NULL);
4637 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4638 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4639 return -ENOSPC;
4640 strcpy(ibms_init[i].param, val);
4641 strcat(ibms_init[i].param, ",");
4642 return 0;
4646 return -EINVAL;
4649 static int experimental;
4650 module_param(experimental, int, 0);
4652 static u32 dbg_level;
4653 module_param_named(debug, dbg_level, uint, 0);
4655 static int force_load;
4656 module_param(force_load, bool, 0);
4658 static int fan_control_allowed;
4659 module_param_named(fan_control, fan_control_allowed, bool, 0);
4661 static int brightness_mode;
4662 module_param_named(brightness_mode, brightness_mode, int, 0);
4664 static unsigned int hotkey_report_mode;
4665 module_param(hotkey_report_mode, uint, 0);
4667 #define IBM_PARAM(feature) \
4668 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4670 IBM_PARAM(hotkey);
4671 IBM_PARAM(bluetooth);
4672 IBM_PARAM(video);
4673 IBM_PARAM(light);
4674 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4675 IBM_PARAM(dock);
4676 #endif
4677 #ifdef CONFIG_THINKPAD_ACPI_BAY
4678 IBM_PARAM(bay);
4679 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4680 IBM_PARAM(cmos);
4681 IBM_PARAM(led);
4682 IBM_PARAM(beep);
4683 IBM_PARAM(ecdump);
4684 IBM_PARAM(brightness);
4685 IBM_PARAM(volume);
4686 IBM_PARAM(fan);
4688 static int __init thinkpad_acpi_module_init(void)
4690 int ret, i;
4692 /* Parameter checking */
4693 if (hotkey_report_mode > 2)
4694 return -EINVAL;
4696 /* Driver-level probe */
4698 get_thinkpad_model_data(&thinkpad_id);
4699 ret = probe_for_thinkpad();
4700 if (ret) {
4701 thinkpad_acpi_module_exit();
4702 return ret;
4705 /* Driver initialization */
4707 IBM_ACPIHANDLE_INIT(ecrd);
4708 IBM_ACPIHANDLE_INIT(ecwr);
4710 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4711 if (!proc_dir) {
4712 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4713 thinkpad_acpi_module_exit();
4714 return -ENODEV;
4716 proc_dir->owner = THIS_MODULE;
4718 ret = platform_driver_register(&tpacpi_pdriver);
4719 if (ret) {
4720 printk(IBM_ERR "unable to register platform driver\n");
4721 thinkpad_acpi_module_exit();
4722 return ret;
4724 tp_features.platform_drv_registered = 1;
4726 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4727 if (ret) {
4728 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4729 thinkpad_acpi_module_exit();
4730 return ret;
4732 tp_features.platform_drv_attrs_registered = 1;
4735 /* Device initialization */
4736 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4737 NULL, 0);
4738 if (IS_ERR(tpacpi_pdev)) {
4739 ret = PTR_ERR(tpacpi_pdev);
4740 tpacpi_pdev = NULL;
4741 printk(IBM_ERR "unable to register platform device\n");
4742 thinkpad_acpi_module_exit();
4743 return ret;
4745 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4746 if (IS_ERR(tpacpi_hwmon)) {
4747 ret = PTR_ERR(tpacpi_hwmon);
4748 tpacpi_hwmon = NULL;
4749 printk(IBM_ERR "unable to register hwmon device\n");
4750 thinkpad_acpi_module_exit();
4751 return ret;
4753 mutex_init(&tpacpi_inputdev_send_mutex);
4754 tpacpi_inputdev = input_allocate_device();
4755 if (!tpacpi_inputdev) {
4756 printk(IBM_ERR "unable to allocate input device\n");
4757 thinkpad_acpi_module_exit();
4758 return -ENOMEM;
4759 } else {
4760 /* Prepare input device, but don't register */
4761 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4762 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4763 tpacpi_inputdev->id.bustype = BUS_HOST;
4764 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
4765 thinkpad_id.vendor :
4766 PCI_VENDOR_ID_IBM;
4767 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4768 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4770 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4771 ret = ibm_init(&ibms_init[i]);
4772 if (ret >= 0 && *ibms_init[i].param)
4773 ret = ibms_init[i].data->write(ibms_init[i].param);
4774 if (ret < 0) {
4775 thinkpad_acpi_module_exit();
4776 return ret;
4779 ret = input_register_device(tpacpi_inputdev);
4780 if (ret < 0) {
4781 printk(IBM_ERR "unable to register input device\n");
4782 thinkpad_acpi_module_exit();
4783 return ret;
4784 } else {
4785 tp_features.input_device_registered = 1;
4788 return 0;
4791 static void thinkpad_acpi_module_exit(void)
4793 struct ibm_struct *ibm, *itmp;
4795 list_for_each_entry_safe_reverse(ibm, itmp,
4796 &tpacpi_all_drivers,
4797 all_drivers) {
4798 ibm_exit(ibm);
4801 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4803 if (tpacpi_inputdev) {
4804 if (tp_features.input_device_registered)
4805 input_unregister_device(tpacpi_inputdev);
4806 else
4807 input_free_device(tpacpi_inputdev);
4810 if (tpacpi_hwmon)
4811 hwmon_device_unregister(tpacpi_hwmon);
4813 if (tpacpi_pdev)
4814 platform_device_unregister(tpacpi_pdev);
4816 if (tp_features.platform_drv_attrs_registered)
4817 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4819 if (tp_features.platform_drv_registered)
4820 platform_driver_unregister(&tpacpi_pdriver);
4822 if (proc_dir)
4823 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4825 kfree(thinkpad_id.bios_version_str);
4826 kfree(thinkpad_id.ec_version_str);
4827 kfree(thinkpad_id.model_str);
4830 module_init(thinkpad_acpi_module_init);
4831 module_exit(thinkpad_acpi_module_exit);