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
blobbb90462e41d17050051b02da146fc45b742a9860
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_properties ibm_backlight_data = {
3057 .owner = THIS_MODULE,
3058 .get_brightness = brightness_get,
3059 .update_status = brightness_update_status,
3060 .max_brightness = 7,
3063 static struct mutex brightness_mutex;
3065 static int __init brightness_init(struct ibm_init_struct *iibm)
3067 int b;
3069 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3071 mutex_init(&brightness_mutex);
3073 if (!brightness_mode) {
3074 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3075 brightness_mode = 2;
3076 else
3077 brightness_mode = 3;
3079 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3080 brightness_mode);
3083 if (brightness_mode > 3)
3084 return -EINVAL;
3086 b = brightness_get(NULL);
3087 if (b < 0)
3088 return 1;
3090 ibm_backlight_device = backlight_device_register(
3091 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3092 &ibm_backlight_data);
3093 if (IS_ERR(ibm_backlight_device)) {
3094 printk(IBM_ERR "Could not register backlight device\n");
3095 return PTR_ERR(ibm_backlight_device);
3097 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
3099 ibm_backlight_device->props->brightness = b;
3100 brightness_update_status(ibm_backlight_device);
3102 return 0;
3105 static void brightness_exit(void)
3107 if (ibm_backlight_device) {
3108 vdbg_printk(TPACPI_DBG_EXIT,
3109 "calling backlight_device_unregister()\n");
3110 backlight_device_unregister(ibm_backlight_device);
3111 ibm_backlight_device = NULL;
3115 static int brightness_update_status(struct backlight_device *bd)
3117 return brightness_set(
3118 (bd->props->fb_blank == FB_BLANK_UNBLANK &&
3119 bd->props->power == FB_BLANK_UNBLANK) ?
3120 bd->props->brightness : 0);
3124 * ThinkPads can read brightness from two places: EC 0x31, or
3125 * CMOS NVRAM byte 0x5E, bits 0-3.
3127 static int brightness_get(struct backlight_device *bd)
3129 u8 lec = 0, lcmos = 0, level = 0;
3131 if (brightness_mode & 1) {
3132 if (!acpi_ec_read(brightness_offset, &lec))
3133 return -EIO;
3134 lec &= 7;
3135 level = lec;
3137 if (brightness_mode & 2) {
3138 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3139 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3140 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3141 level = lcmos;
3144 if (brightness_mode == 3 && lec != lcmos) {
3145 printk(IBM_ERR
3146 "CMOS NVRAM (%u) and EC (%u) do not agree "
3147 "on display brightness level\n",
3148 (unsigned int) lcmos,
3149 (unsigned int) lec);
3150 return -EIO;
3153 return level;
3156 static int brightness_set(int value)
3158 int cmos_cmd, inc, i, res;
3159 int current_value;
3161 if (value > 7)
3162 return -EINVAL;
3164 res = mutex_lock_interruptible(&brightness_mutex);
3165 if (res < 0)
3166 return res;
3168 current_value = brightness_get(NULL);
3169 if (current_value < 0) {
3170 res = current_value;
3171 goto errout;
3174 cmos_cmd = value > current_value ?
3175 TP_CMOS_BRIGHTNESS_UP :
3176 TP_CMOS_BRIGHTNESS_DOWN;
3177 inc = value > current_value ? 1 : -1;
3179 res = 0;
3180 for (i = current_value; i != value; i += inc) {
3181 if ((brightness_mode & 2) &&
3182 issue_thinkpad_cmos_command(cmos_cmd)) {
3183 res = -EIO;
3184 goto errout;
3186 if ((brightness_mode & 1) &&
3187 !acpi_ec_write(brightness_offset, i + inc)) {
3188 res = -EIO;
3189 goto errout;;
3193 errout:
3194 mutex_unlock(&brightness_mutex);
3195 return res;
3198 static int brightness_read(char *p)
3200 int len = 0;
3201 int level;
3203 if ((level = brightness_get(NULL)) < 0) {
3204 len += sprintf(p + len, "level:\t\tunreadable\n");
3205 } else {
3206 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3207 len += sprintf(p + len, "commands:\tup, down\n");
3208 len += sprintf(p + len, "commands:\tlevel <level>"
3209 " (<level> is 0-7)\n");
3212 return len;
3215 static int brightness_write(char *buf)
3217 int level;
3218 int new_level;
3219 char *cmd;
3221 while ((cmd = next_cmd(&buf))) {
3222 if ((level = brightness_get(NULL)) < 0)
3223 return level;
3224 level &= 7;
3226 if (strlencmp(cmd, "up") == 0) {
3227 new_level = level == 7 ? 7 : level + 1;
3228 } else if (strlencmp(cmd, "down") == 0) {
3229 new_level = level == 0 ? 0 : level - 1;
3230 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3231 new_level >= 0 && new_level <= 7) {
3232 /* new_level set */
3233 } else
3234 return -EINVAL;
3236 brightness_set(new_level);
3239 return 0;
3242 static struct ibm_struct brightness_driver_data = {
3243 .name = "brightness",
3244 .read = brightness_read,
3245 .write = brightness_write,
3246 .exit = brightness_exit,
3249 /*************************************************************************
3250 * Volume subdriver
3253 static int volume_read(char *p)
3255 int len = 0;
3256 u8 level;
3258 if (!acpi_ec_read(volume_offset, &level)) {
3259 len += sprintf(p + len, "level:\t\tunreadable\n");
3260 } else {
3261 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3262 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3263 len += sprintf(p + len, "commands:\tup, down, mute\n");
3264 len += sprintf(p + len, "commands:\tlevel <level>"
3265 " (<level> is 0-15)\n");
3268 return len;
3271 static int volume_write(char *buf)
3273 int cmos_cmd, inc, i;
3274 u8 level, mute;
3275 int new_level, new_mute;
3276 char *cmd;
3278 while ((cmd = next_cmd(&buf))) {
3279 if (!acpi_ec_read(volume_offset, &level))
3280 return -EIO;
3281 new_mute = mute = level & 0x40;
3282 new_level = level = level & 0xf;
3284 if (strlencmp(cmd, "up") == 0) {
3285 if (mute)
3286 new_mute = 0;
3287 else
3288 new_level = level == 15 ? 15 : level + 1;
3289 } else if (strlencmp(cmd, "down") == 0) {
3290 if (mute)
3291 new_mute = 0;
3292 else
3293 new_level = level == 0 ? 0 : level - 1;
3294 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3295 new_level >= 0 && new_level <= 15) {
3296 /* new_level set */
3297 } else if (strlencmp(cmd, "mute") == 0) {
3298 new_mute = 0x40;
3299 } else
3300 return -EINVAL;
3302 if (new_level != level) { /* mute doesn't change */
3303 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3304 inc = new_level > level ? 1 : -1;
3306 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3307 !acpi_ec_write(volume_offset, level)))
3308 return -EIO;
3310 for (i = level; i != new_level; i += inc)
3311 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3312 !acpi_ec_write(volume_offset, i + inc))
3313 return -EIO;
3315 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3316 !acpi_ec_write(volume_offset,
3317 new_level + mute)))
3318 return -EIO;
3321 if (new_mute != mute) { /* level doesn't change */
3322 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3324 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3325 !acpi_ec_write(volume_offset, level + new_mute))
3326 return -EIO;
3330 return 0;
3333 static struct ibm_struct volume_driver_data = {
3334 .name = "volume",
3335 .read = volume_read,
3336 .write = volume_write,
3339 /*************************************************************************
3340 * Fan subdriver
3344 * FAN ACCESS MODES
3346 * TPACPI_FAN_RD_ACPI_GFAN:
3347 * ACPI GFAN method: returns fan level
3349 * see TPACPI_FAN_WR_ACPI_SFAN
3350 * EC 0x2f (HFSP) not available if GFAN exists
3352 * TPACPI_FAN_WR_ACPI_SFAN:
3353 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3355 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3356 * it for writing.
3358 * TPACPI_FAN_WR_TPEC:
3359 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3360 * Supported on almost all ThinkPads
3362 * Fan speed changes of any sort (including those caused by the
3363 * disengaged mode) are usually done slowly by the firmware as the
3364 * maximum ammount of fan duty cycle change per second seems to be
3365 * limited.
3367 * Reading is not available if GFAN exists.
3368 * Writing is not available if SFAN exists.
3370 * Bits
3371 * 7 automatic mode engaged;
3372 * (default operation mode of the ThinkPad)
3373 * fan level is ignored in this mode.
3374 * 6 full speed mode (takes precedence over bit 7);
3375 * not available on all thinkpads. May disable
3376 * the tachometer while the fan controller ramps up
3377 * the speed (which can take up to a few *minutes*).
3378 * Speeds up fan to 100% duty-cycle, which is far above
3379 * the standard RPM levels. It is not impossible that
3380 * it could cause hardware damage.
3381 * 5-3 unused in some models. Extra bits for fan level
3382 * in others, but still useless as all values above
3383 * 7 map to the same speed as level 7 in these models.
3384 * 2-0 fan level (0..7 usually)
3385 * 0x00 = stop
3386 * 0x07 = max (set when temperatures critical)
3387 * Some ThinkPads may have other levels, see
3388 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3390 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3391 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3392 * does so, its initial value is meaningless (0x07).
3394 * For firmware bugs, refer to:
3395 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3397 * ----
3399 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3400 * Main fan tachometer reading (in RPM)
3402 * This register is present on all ThinkPads with a new-style EC, and
3403 * it is known not to be present on the A21m/e, and T22, as there is
3404 * something else in offset 0x84 according to the ACPI DSDT. Other
3405 * ThinkPads from this same time period (and earlier) probably lack the
3406 * tachometer as well.
3408 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3409 * was never fixed by IBM to report the EC firmware version string
3410 * probably support the tachometer (like the early X models), so
3411 * detecting it is quite hard. We need more data to know for sure.
3413 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3414 * might result.
3416 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3417 * mode.
3419 * For firmware bugs, refer to:
3420 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3422 * TPACPI_FAN_WR_ACPI_FANS:
3423 * ThinkPad X31, X40, X41. Not available in the X60.
3425 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3426 * high speed. ACPI DSDT seems to map these three speeds to levels
3427 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3428 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3430 * The speeds are stored on handles
3431 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3433 * There are three default speed sets, acessible as handles:
3434 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3436 * ACPI DSDT switches which set is in use depending on various
3437 * factors.
3439 * TPACPI_FAN_WR_TPEC is also available and should be used to
3440 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3441 * but the ACPI tables just mention level 7.
3444 static enum fan_status_access_mode fan_status_access_mode;
3445 static enum fan_control_access_mode fan_control_access_mode;
3446 static enum fan_control_commands fan_control_commands;
3448 static u8 fan_control_initial_status;
3449 static u8 fan_control_desired_level;
3451 static void fan_watchdog_fire(struct work_struct *ignored);
3452 static int fan_watchdog_maxinterval;
3453 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3455 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3456 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3457 "\\FSPD", /* 600e/x, 770e, 770x */
3458 ); /* all others */
3459 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3460 "JFNS", /* 770x-JL */
3461 ); /* all others */
3464 * SYSFS fan layout: hwmon compatible (device)
3466 * pwm*_enable:
3467 * 0: "disengaged" mode
3468 * 1: manual mode
3469 * 2: native EC "auto" mode (recommended, hardware default)
3471 * pwm*: set speed in manual mode, ignored otherwise.
3472 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3473 * interpolation.
3475 * fan*_input: tachometer reading, RPM
3478 * SYSFS fan layout: extensions
3480 * fan_watchdog (driver):
3481 * fan watchdog interval in seconds, 0 disables (default), max 120
3484 /* sysfs fan pwm1_enable ----------------------------------------------- */
3485 static ssize_t fan_pwm1_enable_show(struct device *dev,
3486 struct device_attribute *attr,
3487 char *buf)
3489 int res, mode;
3490 u8 status;
3492 res = fan_get_status_safe(&status);
3493 if (res)
3494 return res;
3496 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3497 if (status != fan_control_initial_status) {
3498 tp_features.fan_ctrl_status_undef = 0;
3499 } else {
3500 /* Return most likely status. In fact, it
3501 * might be the only possible status */
3502 status = TP_EC_FAN_AUTO;
3506 if (status & TP_EC_FAN_FULLSPEED) {
3507 mode = 0;
3508 } else if (status & TP_EC_FAN_AUTO) {
3509 mode = 2;
3510 } else
3511 mode = 1;
3513 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3516 static ssize_t fan_pwm1_enable_store(struct device *dev,
3517 struct device_attribute *attr,
3518 const char *buf, size_t count)
3520 unsigned long t;
3521 int res, level;
3523 if (parse_strtoul(buf, 2, &t))
3524 return -EINVAL;
3526 switch (t) {
3527 case 0:
3528 level = TP_EC_FAN_FULLSPEED;
3529 break;
3530 case 1:
3531 level = TPACPI_FAN_LAST_LEVEL;
3532 break;
3533 case 2:
3534 level = TP_EC_FAN_AUTO;
3535 break;
3536 case 3:
3537 /* reserved for software-controlled auto mode */
3538 return -ENOSYS;
3539 default:
3540 return -EINVAL;
3543 res = fan_set_level_safe(level);
3544 if (res == -ENXIO)
3545 return -EINVAL;
3546 else if (res < 0)
3547 return res;
3549 fan_watchdog_reset();
3551 return count;
3554 static struct device_attribute dev_attr_fan_pwm1_enable =
3555 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3556 fan_pwm1_enable_show, fan_pwm1_enable_store);
3558 /* sysfs fan pwm1 ------------------------------------------------------ */
3559 static ssize_t fan_pwm1_show(struct device *dev,
3560 struct device_attribute *attr,
3561 char *buf)
3563 int res;
3564 u8 status;
3566 res = fan_get_status_safe(&status);
3567 if (res)
3568 return res;
3570 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3571 if (status != fan_control_initial_status) {
3572 tp_features.fan_ctrl_status_undef = 0;
3573 } else {
3574 status = TP_EC_FAN_AUTO;
3578 if ((status &
3579 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3580 status = fan_control_desired_level;
3582 if (status > 7)
3583 status = 7;
3585 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3588 static ssize_t fan_pwm1_store(struct device *dev,
3589 struct device_attribute *attr,
3590 const char *buf, size_t count)
3592 unsigned long s;
3593 int rc;
3594 u8 status, newlevel;
3596 if (parse_strtoul(buf, 255, &s))
3597 return -EINVAL;
3599 /* scale down from 0-255 to 0-7 */
3600 newlevel = (s >> 5) & 0x07;
3602 rc = mutex_lock_interruptible(&fan_mutex);
3603 if (rc < 0)
3604 return rc;
3606 rc = fan_get_status(&status);
3607 if (!rc && (status &
3608 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3609 rc = fan_set_level(newlevel);
3610 if (rc == -ENXIO)
3611 rc = -EINVAL;
3612 else if (!rc) {
3613 fan_update_desired_level(newlevel);
3614 fan_watchdog_reset();
3618 mutex_unlock(&fan_mutex);
3619 return (rc)? rc : count;
3622 static struct device_attribute dev_attr_fan_pwm1 =
3623 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3624 fan_pwm1_show, fan_pwm1_store);
3626 /* sysfs fan fan1_input ------------------------------------------------ */
3627 static ssize_t fan_fan1_input_show(struct device *dev,
3628 struct device_attribute *attr,
3629 char *buf)
3631 int res;
3632 unsigned int speed;
3634 res = fan_get_speed(&speed);
3635 if (res < 0)
3636 return res;
3638 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3641 static struct device_attribute dev_attr_fan_fan1_input =
3642 __ATTR(fan1_input, S_IRUGO,
3643 fan_fan1_input_show, NULL);
3645 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3646 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3647 char *buf)
3649 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3652 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3653 const char *buf, size_t count)
3655 unsigned long t;
3657 if (parse_strtoul(buf, 120, &t))
3658 return -EINVAL;
3660 if (!fan_control_allowed)
3661 return -EPERM;
3663 fan_watchdog_maxinterval = t;
3664 fan_watchdog_reset();
3666 return count;
3669 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3670 fan_fan_watchdog_show, fan_fan_watchdog_store);
3672 /* --------------------------------------------------------------------- */
3673 static struct attribute *fan_attributes[] = {
3674 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3675 &dev_attr_fan_fan1_input.attr,
3676 NULL
3679 static const struct attribute_group fan_attr_group = {
3680 .attrs = fan_attributes,
3683 static int __init fan_init(struct ibm_init_struct *iibm)
3685 int rc;
3687 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3689 mutex_init(&fan_mutex);
3690 fan_status_access_mode = TPACPI_FAN_NONE;
3691 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3692 fan_control_commands = 0;
3693 fan_watchdog_maxinterval = 0;
3694 tp_features.fan_ctrl_status_undef = 0;
3695 fan_control_desired_level = 7;
3697 IBM_ACPIHANDLE_INIT(fans);
3698 IBM_ACPIHANDLE_INIT(gfan);
3699 IBM_ACPIHANDLE_INIT(sfan);
3701 if (gfan_handle) {
3702 /* 570, 600e/x, 770e, 770x */
3703 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3704 } else {
3705 /* all other ThinkPads: note that even old-style
3706 * ThinkPad ECs supports the fan control register */
3707 if (likely(acpi_ec_read(fan_status_offset,
3708 &fan_control_initial_status))) {
3709 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3711 /* In some ThinkPads, neither the EC nor the ACPI
3712 * DSDT initialize the fan status, and it ends up
3713 * being set to 0x07 when it *could* be either
3714 * 0x07 or 0x80.
3716 * Enable for TP-1Y (T43), TP-78 (R51e),
3717 * TP-76 (R52), TP-70 (T43, R52), which are known
3718 * to be buggy. */
3719 if (fan_control_initial_status == 0x07) {
3720 switch (thinkpad_id.ec_model) {
3721 case 0x5931: /* TP-1Y */
3722 case 0x3837: /* TP-78 */
3723 case 0x3637: /* TP-76 */
3724 case 0x3037: /* TP-70 */
3725 printk(IBM_NOTICE
3726 "fan_init: initial fan status is "
3727 "unknown, assuming it is in auto "
3728 "mode\n");
3729 tp_features.fan_ctrl_status_undef = 1;
3733 } else {
3734 printk(IBM_ERR
3735 "ThinkPad ACPI EC access misbehaving, "
3736 "fan status and control unavailable\n");
3737 return 1;
3741 if (sfan_handle) {
3742 /* 570, 770x-JL */
3743 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3744 fan_control_commands |=
3745 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3746 } else {
3747 if (!gfan_handle) {
3748 /* gfan without sfan means no fan control */
3749 /* all other models implement TP EC 0x2f control */
3751 if (fans_handle) {
3752 /* X31, X40, X41 */
3753 fan_control_access_mode =
3754 TPACPI_FAN_WR_ACPI_FANS;
3755 fan_control_commands |=
3756 TPACPI_FAN_CMD_SPEED |
3757 TPACPI_FAN_CMD_LEVEL |
3758 TPACPI_FAN_CMD_ENABLE;
3759 } else {
3760 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3761 fan_control_commands |=
3762 TPACPI_FAN_CMD_LEVEL |
3763 TPACPI_FAN_CMD_ENABLE;
3768 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3769 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3770 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3771 fan_status_access_mode, fan_control_access_mode);
3773 /* fan control master switch */
3774 if (!fan_control_allowed) {
3775 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3776 fan_control_commands = 0;
3777 dbg_printk(TPACPI_DBG_INIT,
3778 "fan control features disabled by parameter\n");
3781 /* update fan_control_desired_level */
3782 if (fan_status_access_mode != TPACPI_FAN_NONE)
3783 fan_get_status_safe(NULL);
3785 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3786 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3787 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3788 &fan_attr_group);
3789 if (!(rc < 0))
3790 rc = driver_create_file(&tpacpi_pdriver.driver,
3791 &driver_attr_fan_watchdog);
3792 if (rc < 0)
3793 return rc;
3794 return 0;
3795 } else
3796 return 1;
3800 * Call with fan_mutex held
3802 static void fan_update_desired_level(u8 status)
3804 if ((status &
3805 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3806 if (status > 7)
3807 fan_control_desired_level = 7;
3808 else
3809 fan_control_desired_level = status;
3813 static int fan_get_status(u8 *status)
3815 u8 s;
3817 /* TODO:
3818 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3820 switch (fan_status_access_mode) {
3821 case TPACPI_FAN_RD_ACPI_GFAN:
3822 /* 570, 600e/x, 770e, 770x */
3824 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3825 return -EIO;
3827 if (likely(status))
3828 *status = s & 0x07;
3830 break;
3832 case TPACPI_FAN_RD_TPEC:
3833 /* all except 570, 600e/x, 770e, 770x */
3834 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3835 return -EIO;
3837 if (likely(status))
3838 *status = s;
3840 break;
3842 default:
3843 return -ENXIO;
3846 return 0;
3849 static int fan_get_status_safe(u8 *status)
3851 int rc;
3852 u8 s;
3854 rc = mutex_lock_interruptible(&fan_mutex);
3855 if (rc < 0)
3856 return rc;
3857 rc = fan_get_status(&s);
3858 if (!rc)
3859 fan_update_desired_level(s);
3860 mutex_unlock(&fan_mutex);
3862 if (status)
3863 *status = s;
3865 return rc;
3868 static void fan_exit(void)
3870 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3872 /* FIXME: can we really do this unconditionally? */
3873 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3874 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3876 cancel_delayed_work(&fan_watchdog_task);
3877 flush_scheduled_work();
3880 static int fan_get_speed(unsigned int *speed)
3882 u8 hi, lo;
3884 switch (fan_status_access_mode) {
3885 case TPACPI_FAN_RD_TPEC:
3886 /* all except 570, 600e/x, 770e, 770x */
3887 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3888 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3889 return -EIO;
3891 if (likely(speed))
3892 *speed = (hi << 8) | lo;
3894 break;
3896 default:
3897 return -ENXIO;
3900 return 0;
3903 static void fan_watchdog_fire(struct work_struct *ignored)
3905 int rc;
3907 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3908 rc = fan_set_enable();
3909 if (rc < 0) {
3910 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3911 "will try again later...\n", -rc);
3912 /* reschedule for later */
3913 fan_watchdog_reset();
3917 static void fan_watchdog_reset(void)
3919 static int fan_watchdog_active;
3921 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3922 return;
3924 if (fan_watchdog_active)
3925 cancel_delayed_work(&fan_watchdog_task);
3927 if (fan_watchdog_maxinterval > 0) {
3928 fan_watchdog_active = 1;
3929 if (!schedule_delayed_work(&fan_watchdog_task,
3930 msecs_to_jiffies(fan_watchdog_maxinterval
3931 * 1000))) {
3932 printk(IBM_ERR "failed to schedule the fan watchdog, "
3933 "watchdog will not trigger\n");
3935 } else
3936 fan_watchdog_active = 0;
3939 static int fan_set_level(int level)
3941 if (!fan_control_allowed)
3942 return -EPERM;
3944 switch (fan_control_access_mode) {
3945 case TPACPI_FAN_WR_ACPI_SFAN:
3946 if (level >= 0 && level <= 7) {
3947 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3948 return -EIO;
3949 } else
3950 return -EINVAL;
3951 break;
3953 case TPACPI_FAN_WR_ACPI_FANS:
3954 case TPACPI_FAN_WR_TPEC:
3955 if ((level != TP_EC_FAN_AUTO) &&
3956 (level != TP_EC_FAN_FULLSPEED) &&
3957 ((level < 0) || (level > 7)))
3958 return -EINVAL;
3960 /* safety net should the EC not support AUTO
3961 * or FULLSPEED mode bits and just ignore them */
3962 if (level & TP_EC_FAN_FULLSPEED)
3963 level |= 7; /* safety min speed 7 */
3964 else if (level & TP_EC_FAN_FULLSPEED)
3965 level |= 4; /* safety min speed 4 */
3967 if (!acpi_ec_write(fan_status_offset, level))
3968 return -EIO;
3969 else
3970 tp_features.fan_ctrl_status_undef = 0;
3971 break;
3973 default:
3974 return -ENXIO;
3976 return 0;
3979 static int fan_set_level_safe(int level)
3981 int rc;
3983 if (!fan_control_allowed)
3984 return -EPERM;
3986 rc = mutex_lock_interruptible(&fan_mutex);
3987 if (rc < 0)
3988 return rc;
3990 if (level == TPACPI_FAN_LAST_LEVEL)
3991 level = fan_control_desired_level;
3993 rc = fan_set_level(level);
3994 if (!rc)
3995 fan_update_desired_level(level);
3997 mutex_unlock(&fan_mutex);
3998 return rc;
4001 static int fan_set_enable(void)
4003 u8 s;
4004 int rc;
4006 if (!fan_control_allowed)
4007 return -EPERM;
4009 rc = mutex_lock_interruptible(&fan_mutex);
4010 if (rc < 0)
4011 return rc;
4013 switch (fan_control_access_mode) {
4014 case TPACPI_FAN_WR_ACPI_FANS:
4015 case TPACPI_FAN_WR_TPEC:
4016 rc = fan_get_status(&s);
4017 if (rc < 0)
4018 break;
4020 /* Don't go out of emergency fan mode */
4021 if (s != 7) {
4022 s &= 0x07;
4023 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4026 if (!acpi_ec_write(fan_status_offset, s))
4027 rc = -EIO;
4028 else {
4029 tp_features.fan_ctrl_status_undef = 0;
4030 rc = 0;
4032 break;
4034 case TPACPI_FAN_WR_ACPI_SFAN:
4035 rc = fan_get_status(&s);
4036 if (rc < 0)
4037 break;
4039 s &= 0x07;
4041 /* Set fan to at least level 4 */
4042 s |= 4;
4044 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4045 rc= -EIO;
4046 else
4047 rc = 0;
4048 break;
4050 default:
4051 rc = -ENXIO;
4054 mutex_unlock(&fan_mutex);
4055 return rc;
4058 static int fan_set_disable(void)
4060 int rc;
4062 if (!fan_control_allowed)
4063 return -EPERM;
4065 rc = mutex_lock_interruptible(&fan_mutex);
4066 if (rc < 0)
4067 return rc;
4069 rc = 0;
4070 switch (fan_control_access_mode) {
4071 case TPACPI_FAN_WR_ACPI_FANS:
4072 case TPACPI_FAN_WR_TPEC:
4073 if (!acpi_ec_write(fan_status_offset, 0x00))
4074 rc = -EIO;
4075 else {
4076 fan_control_desired_level = 0;
4077 tp_features.fan_ctrl_status_undef = 0;
4079 break;
4081 case TPACPI_FAN_WR_ACPI_SFAN:
4082 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4083 rc = -EIO;
4084 else
4085 fan_control_desired_level = 0;
4086 break;
4088 default:
4089 rc = -ENXIO;
4093 mutex_unlock(&fan_mutex);
4094 return rc;
4097 static int fan_set_speed(int speed)
4099 int rc;
4101 if (!fan_control_allowed)
4102 return -EPERM;
4104 rc = mutex_lock_interruptible(&fan_mutex);
4105 if (rc < 0)
4106 return rc;
4108 rc = 0;
4109 switch (fan_control_access_mode) {
4110 case TPACPI_FAN_WR_ACPI_FANS:
4111 if (speed >= 0 && speed <= 65535) {
4112 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4113 speed, speed, speed))
4114 rc = -EIO;
4115 } else
4116 rc = -EINVAL;
4117 break;
4119 default:
4120 rc = -ENXIO;
4123 mutex_unlock(&fan_mutex);
4124 return rc;
4127 static int fan_read(char *p)
4129 int len = 0;
4130 int rc;
4131 u8 status;
4132 unsigned int speed = 0;
4134 switch (fan_status_access_mode) {
4135 case TPACPI_FAN_RD_ACPI_GFAN:
4136 /* 570, 600e/x, 770e, 770x */
4137 if ((rc = fan_get_status_safe(&status)) < 0)
4138 return rc;
4140 len += sprintf(p + len, "status:\t\t%s\n"
4141 "level:\t\t%d\n",
4142 (status != 0) ? "enabled" : "disabled", status);
4143 break;
4145 case TPACPI_FAN_RD_TPEC:
4146 /* all except 570, 600e/x, 770e, 770x */
4147 if ((rc = fan_get_status_safe(&status)) < 0)
4148 return rc;
4150 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4151 if (status != fan_control_initial_status)
4152 tp_features.fan_ctrl_status_undef = 0;
4153 else
4154 /* Return most likely status. In fact, it
4155 * might be the only possible status */
4156 status = TP_EC_FAN_AUTO;
4159 len += sprintf(p + len, "status:\t\t%s\n",
4160 (status != 0) ? "enabled" : "disabled");
4162 if ((rc = fan_get_speed(&speed)) < 0)
4163 return rc;
4165 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4167 if (status & TP_EC_FAN_FULLSPEED)
4168 /* Disengaged mode takes precedence */
4169 len += sprintf(p + len, "level:\t\tdisengaged\n");
4170 else if (status & TP_EC_FAN_AUTO)
4171 len += sprintf(p + len, "level:\t\tauto\n");
4172 else
4173 len += sprintf(p + len, "level:\t\t%d\n", status);
4174 break;
4176 case TPACPI_FAN_NONE:
4177 default:
4178 len += sprintf(p + len, "status:\t\tnot supported\n");
4181 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
4182 len += sprintf(p + len, "commands:\tlevel <level>");
4184 switch (fan_control_access_mode) {
4185 case TPACPI_FAN_WR_ACPI_SFAN:
4186 len += sprintf(p + len, " (<level> is 0-7)\n");
4187 break;
4189 default:
4190 len += sprintf(p + len, " (<level> is 0-7, "
4191 "auto, disengaged, full-speed)\n");
4192 break;
4196 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
4197 len += sprintf(p + len, "commands:\tenable, disable\n"
4198 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4199 "1-120 (seconds))\n");
4201 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
4202 len += sprintf(p + len, "commands:\tspeed <speed>"
4203 " (<speed> is 0-65535)\n");
4205 return len;
4208 static int fan_write_cmd_level(const char *cmd, int *rc)
4210 int level;
4212 if (strlencmp(cmd, "level auto") == 0)
4213 level = TP_EC_FAN_AUTO;
4214 else if ((strlencmp(cmd, "level disengaged") == 0) |
4215 (strlencmp(cmd, "level full-speed") == 0))
4216 level = TP_EC_FAN_FULLSPEED;
4217 else if (sscanf(cmd, "level %d", &level) != 1)
4218 return 0;
4220 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4221 printk(IBM_ERR "level command accepted for unsupported "
4222 "access mode %d", fan_control_access_mode);
4224 return 1;
4227 static int fan_write_cmd_enable(const char *cmd, int *rc)
4229 if (strlencmp(cmd, "enable") != 0)
4230 return 0;
4232 if ((*rc = fan_set_enable()) == -ENXIO)
4233 printk(IBM_ERR "enable command accepted for unsupported "
4234 "access mode %d", fan_control_access_mode);
4236 return 1;
4239 static int fan_write_cmd_disable(const char *cmd, int *rc)
4241 if (strlencmp(cmd, "disable") != 0)
4242 return 0;
4244 if ((*rc = fan_set_disable()) == -ENXIO)
4245 printk(IBM_ERR "disable command accepted for unsupported "
4246 "access mode %d", fan_control_access_mode);
4248 return 1;
4251 static int fan_write_cmd_speed(const char *cmd, int *rc)
4253 int speed;
4255 /* TODO:
4256 * Support speed <low> <medium> <high> ? */
4258 if (sscanf(cmd, "speed %d", &speed) != 1)
4259 return 0;
4261 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4262 printk(IBM_ERR "speed command accepted for unsupported "
4263 "access mode %d", fan_control_access_mode);
4265 return 1;
4268 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4270 int interval;
4272 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4273 return 0;
4275 if (interval < 0 || interval > 120)
4276 *rc = -EINVAL;
4277 else
4278 fan_watchdog_maxinterval = interval;
4280 return 1;
4283 static int fan_write(char *buf)
4285 char *cmd;
4286 int rc = 0;
4288 while (!rc && (cmd = next_cmd(&buf))) {
4289 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4290 fan_write_cmd_level(cmd, &rc)) &&
4291 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4292 (fan_write_cmd_enable(cmd, &rc) ||
4293 fan_write_cmd_disable(cmd, &rc) ||
4294 fan_write_cmd_watchdog(cmd, &rc))) &&
4295 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4296 fan_write_cmd_speed(cmd, &rc))
4298 rc = -EINVAL;
4299 else if (!rc)
4300 fan_watchdog_reset();
4303 return rc;
4306 static struct ibm_struct fan_driver_data = {
4307 .name = "fan",
4308 .read = fan_read,
4309 .write = fan_write,
4310 .exit = fan_exit,
4313 /****************************************************************************
4314 ****************************************************************************
4316 * Infrastructure
4318 ****************************************************************************
4319 ****************************************************************************/
4321 /* /proc support */
4322 static struct proc_dir_entry *proc_dir;
4324 /* Subdriver registry */
4325 static LIST_HEAD(tpacpi_all_drivers);
4329 * Module and infrastructure proble, init and exit handling
4332 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4333 static const char * __init str_supported(int is_supported)
4335 static char text_unsupported[] __initdata = "not supported";
4337 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4339 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4341 static int __init ibm_init(struct ibm_init_struct *iibm)
4343 int ret;
4344 struct ibm_struct *ibm = iibm->data;
4345 struct proc_dir_entry *entry;
4347 BUG_ON(ibm == NULL);
4349 INIT_LIST_HEAD(&ibm->all_drivers);
4351 if (ibm->flags.experimental && !experimental)
4352 return 0;
4354 dbg_printk(TPACPI_DBG_INIT,
4355 "probing for %s\n", ibm->name);
4357 if (iibm->init) {
4358 ret = iibm->init(iibm);
4359 if (ret > 0)
4360 return 0; /* probe failed */
4361 if (ret)
4362 return ret;
4364 ibm->flags.init_called = 1;
4367 if (ibm->acpi) {
4368 if (ibm->acpi->hid) {
4369 ret = register_tpacpi_subdriver(ibm);
4370 if (ret)
4371 goto err_out;
4374 if (ibm->acpi->notify) {
4375 ret = setup_acpi_notify(ibm);
4376 if (ret == -ENODEV) {
4377 printk(IBM_NOTICE "disabling subdriver %s\n",
4378 ibm->name);
4379 ret = 0;
4380 goto err_out;
4382 if (ret < 0)
4383 goto err_out;
4387 dbg_printk(TPACPI_DBG_INIT,
4388 "%s installed\n", ibm->name);
4390 if (ibm->read) {
4391 entry = create_proc_entry(ibm->name,
4392 S_IFREG | S_IRUGO | S_IWUSR,
4393 proc_dir);
4394 if (!entry) {
4395 printk(IBM_ERR "unable to create proc entry %s\n",
4396 ibm->name);
4397 ret = -ENODEV;
4398 goto err_out;
4400 entry->owner = THIS_MODULE;
4401 entry->data = ibm;
4402 entry->read_proc = &dispatch_procfs_read;
4403 if (ibm->write)
4404 entry->write_proc = &dispatch_procfs_write;
4405 ibm->flags.proc_created = 1;
4408 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4410 return 0;
4412 err_out:
4413 dbg_printk(TPACPI_DBG_INIT,
4414 "%s: at error exit path with result %d\n",
4415 ibm->name, ret);
4417 ibm_exit(ibm);
4418 return (ret < 0)? ret : 0;
4421 static void ibm_exit(struct ibm_struct *ibm)
4423 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4425 list_del_init(&ibm->all_drivers);
4427 if (ibm->flags.acpi_notify_installed) {
4428 dbg_printk(TPACPI_DBG_EXIT,
4429 "%s: acpi_remove_notify_handler\n", ibm->name);
4430 BUG_ON(!ibm->acpi);
4431 acpi_remove_notify_handler(*ibm->acpi->handle,
4432 ibm->acpi->type,
4433 dispatch_acpi_notify);
4434 ibm->flags.acpi_notify_installed = 0;
4435 ibm->flags.acpi_notify_installed = 0;
4438 if (ibm->flags.proc_created) {
4439 dbg_printk(TPACPI_DBG_EXIT,
4440 "%s: remove_proc_entry\n", ibm->name);
4441 remove_proc_entry(ibm->name, proc_dir);
4442 ibm->flags.proc_created = 0;
4445 if (ibm->flags.acpi_driver_registered) {
4446 dbg_printk(TPACPI_DBG_EXIT,
4447 "%s: acpi_bus_unregister_driver\n", ibm->name);
4448 BUG_ON(!ibm->acpi);
4449 acpi_bus_unregister_driver(ibm->acpi->driver);
4450 kfree(ibm->acpi->driver);
4451 ibm->acpi->driver = NULL;
4452 ibm->flags.acpi_driver_registered = 0;
4455 if (ibm->flags.init_called && ibm->exit) {
4456 ibm->exit();
4457 ibm->flags.init_called = 0;
4460 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4463 /* Probing */
4465 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
4467 struct dmi_device *dev = NULL;
4468 char ec_fw_string[18];
4470 if (!tp)
4471 return;
4473 memset(tp, 0, sizeof(*tp));
4475 if (dmi_name_in_vendors("IBM"))
4476 tp->vendor = PCI_VENDOR_ID_IBM;
4477 else if (dmi_name_in_vendors("LENOVO"))
4478 tp->vendor = PCI_VENDOR_ID_LENOVO;
4479 else
4480 return;
4482 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4483 GFP_KERNEL);
4484 if (!tp->bios_version_str)
4485 return;
4486 tp->bios_model = tp->bios_version_str[0]
4487 | (tp->bios_version_str[1] << 8);
4490 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4491 * X32 or newer, all Z series; Some models must have an
4492 * up-to-date BIOS or they will not be detected.
4494 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4496 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4497 if (sscanf(dev->name,
4498 "IBM ThinkPad Embedded Controller -[%17c",
4499 ec_fw_string) == 1) {
4500 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4501 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4503 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4504 tp->ec_model = ec_fw_string[0]
4505 | (ec_fw_string[1] << 8);
4506 break;
4510 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4511 GFP_KERNEL);
4512 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4513 kfree(tp->model_str);
4514 tp->model_str = NULL;
4518 static int __init probe_for_thinkpad(void)
4520 int is_thinkpad;
4522 if (acpi_disabled)
4523 return -ENODEV;
4526 * Non-ancient models have better DMI tagging, but very old models
4527 * don't.
4529 is_thinkpad = (thinkpad_id.model_str != NULL);
4531 /* ec is required because many other handles are relative to it */
4532 IBM_ACPIHANDLE_INIT(ec);
4533 if (!ec_handle) {
4534 if (is_thinkpad)
4535 printk(IBM_ERR
4536 "Not yet supported ThinkPad detected!\n");
4537 return -ENODEV;
4541 * Risks a regression on very old machines, but reduces potential
4542 * false positives a damn great deal
4544 if (!is_thinkpad)
4545 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
4547 if (!is_thinkpad && !force_load)
4548 return -ENODEV;
4550 return 0;
4554 /* Module init, exit, parameters */
4556 static struct ibm_init_struct ibms_init[] __initdata = {
4558 .init = thinkpad_acpi_driver_init,
4559 .data = &thinkpad_acpi_driver_data,
4562 .init = hotkey_init,
4563 .data = &hotkey_driver_data,
4566 .init = bluetooth_init,
4567 .data = &bluetooth_driver_data,
4570 .init = wan_init,
4571 .data = &wan_driver_data,
4574 .init = video_init,
4575 .data = &video_driver_data,
4578 .init = light_init,
4579 .data = &light_driver_data,
4581 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4583 .init = dock_init,
4584 .data = &dock_driver_data[0],
4587 .init = dock_init2,
4588 .data = &dock_driver_data[1],
4590 #endif
4591 #ifdef CONFIG_THINKPAD_ACPI_BAY
4593 .init = bay_init,
4594 .data = &bay_driver_data,
4596 #endif
4598 .init = cmos_init,
4599 .data = &cmos_driver_data,
4602 .init = led_init,
4603 .data = &led_driver_data,
4606 .init = beep_init,
4607 .data = &beep_driver_data,
4610 .init = thermal_init,
4611 .data = &thermal_driver_data,
4614 .data = &ecdump_driver_data,
4617 .init = brightness_init,
4618 .data = &brightness_driver_data,
4621 .data = &volume_driver_data,
4624 .init = fan_init,
4625 .data = &fan_driver_data,
4629 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4631 unsigned int i;
4632 struct ibm_struct *ibm;
4634 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4635 ibm = ibms_init[i].data;
4636 BUG_ON(ibm == NULL);
4638 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4639 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4640 return -ENOSPC;
4641 strcpy(ibms_init[i].param, val);
4642 strcat(ibms_init[i].param, ",");
4643 return 0;
4647 return -EINVAL;
4650 static int experimental;
4651 module_param(experimental, int, 0);
4653 static u32 dbg_level;
4654 module_param_named(debug, dbg_level, uint, 0);
4656 static int force_load;
4657 module_param(force_load, bool, 0);
4659 static int fan_control_allowed;
4660 module_param_named(fan_control, fan_control_allowed, bool, 0);
4662 static int brightness_mode;
4663 module_param_named(brightness_mode, brightness_mode, int, 0);
4665 static unsigned int hotkey_report_mode;
4666 module_param(hotkey_report_mode, uint, 0);
4668 #define IBM_PARAM(feature) \
4669 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4671 IBM_PARAM(hotkey);
4672 IBM_PARAM(bluetooth);
4673 IBM_PARAM(video);
4674 IBM_PARAM(light);
4675 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4676 IBM_PARAM(dock);
4677 #endif
4678 #ifdef CONFIG_THINKPAD_ACPI_BAY
4679 IBM_PARAM(bay);
4680 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4681 IBM_PARAM(cmos);
4682 IBM_PARAM(led);
4683 IBM_PARAM(beep);
4684 IBM_PARAM(ecdump);
4685 IBM_PARAM(brightness);
4686 IBM_PARAM(volume);
4687 IBM_PARAM(fan);
4689 static int __init thinkpad_acpi_module_init(void)
4691 int ret, i;
4693 /* Parameter checking */
4694 if (hotkey_report_mode > 2)
4695 return -EINVAL;
4697 /* Driver-level probe */
4699 get_thinkpad_model_data(&thinkpad_id);
4700 ret = probe_for_thinkpad();
4701 if (ret) {
4702 thinkpad_acpi_module_exit();
4703 return ret;
4706 /* Driver initialization */
4708 IBM_ACPIHANDLE_INIT(ecrd);
4709 IBM_ACPIHANDLE_INIT(ecwr);
4711 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4712 if (!proc_dir) {
4713 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4714 thinkpad_acpi_module_exit();
4715 return -ENODEV;
4717 proc_dir->owner = THIS_MODULE;
4719 ret = platform_driver_register(&tpacpi_pdriver);
4720 if (ret) {
4721 printk(IBM_ERR "unable to register platform driver\n");
4722 thinkpad_acpi_module_exit();
4723 return ret;
4725 tp_features.platform_drv_registered = 1;
4727 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4728 if (ret) {
4729 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4730 thinkpad_acpi_module_exit();
4731 return ret;
4733 tp_features.platform_drv_attrs_registered = 1;
4736 /* Device initialization */
4737 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4738 NULL, 0);
4739 if (IS_ERR(tpacpi_pdev)) {
4740 ret = PTR_ERR(tpacpi_pdev);
4741 tpacpi_pdev = NULL;
4742 printk(IBM_ERR "unable to register platform device\n");
4743 thinkpad_acpi_module_exit();
4744 return ret;
4746 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4747 if (IS_ERR(tpacpi_hwmon)) {
4748 ret = PTR_ERR(tpacpi_hwmon);
4749 tpacpi_hwmon = NULL;
4750 printk(IBM_ERR "unable to register hwmon device\n");
4751 thinkpad_acpi_module_exit();
4752 return ret;
4754 mutex_init(&tpacpi_inputdev_send_mutex);
4755 tpacpi_inputdev = input_allocate_device();
4756 if (!tpacpi_inputdev) {
4757 printk(IBM_ERR "unable to allocate input device\n");
4758 thinkpad_acpi_module_exit();
4759 return -ENOMEM;
4760 } else {
4761 /* Prepare input device, but don't register */
4762 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4763 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4764 tpacpi_inputdev->id.bustype = BUS_HOST;
4765 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
4766 thinkpad_id.vendor :
4767 PCI_VENDOR_ID_IBM;
4768 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4769 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4771 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4772 ret = ibm_init(&ibms_init[i]);
4773 if (ret >= 0 && *ibms_init[i].param)
4774 ret = ibms_init[i].data->write(ibms_init[i].param);
4775 if (ret < 0) {
4776 thinkpad_acpi_module_exit();
4777 return ret;
4780 ret = input_register_device(tpacpi_inputdev);
4781 if (ret < 0) {
4782 printk(IBM_ERR "unable to register input device\n");
4783 thinkpad_acpi_module_exit();
4784 return ret;
4785 } else {
4786 tp_features.input_device_registered = 1;
4789 return 0;
4792 static void thinkpad_acpi_module_exit(void)
4794 struct ibm_struct *ibm, *itmp;
4796 list_for_each_entry_safe_reverse(ibm, itmp,
4797 &tpacpi_all_drivers,
4798 all_drivers) {
4799 ibm_exit(ibm);
4802 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4804 if (tpacpi_inputdev) {
4805 if (tp_features.input_device_registered)
4806 input_unregister_device(tpacpi_inputdev);
4807 else
4808 input_free_device(tpacpi_inputdev);
4811 if (tpacpi_hwmon)
4812 hwmon_device_unregister(tpacpi_hwmon);
4814 if (tpacpi_pdev)
4815 platform_device_unregister(tpacpi_pdev);
4817 if (tp_features.platform_drv_attrs_registered)
4818 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4820 if (tp_features.platform_drv_registered)
4821 platform_driver_unregister(&tpacpi_pdriver);
4823 if (proc_dir)
4824 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4826 kfree(thinkpad_id.bios_version_str);
4827 kfree(thinkpad_id.ec_version_str);
4828 kfree(thinkpad_id.model_str);
4831 module_init(thinkpad_acpi_module_init);
4832 module_exit(thinkpad_acpi_module_exit);