ACPI: thinkpad-acpi: bump up version to 0.16
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob2909082a2652200ce89db09d764683e3418cf3f9
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;
523 static int tpacpi_resume_handler(struct platform_device *pdev)
525 struct ibm_struct *ibm, *itmp;
527 list_for_each_entry_safe(ibm, itmp,
528 &tpacpi_all_drivers,
529 all_drivers) {
530 if (ibm->resume)
531 (ibm->resume)();
534 return 0;
537 static struct platform_driver tpacpi_pdriver = {
538 .driver = {
539 .name = IBM_DRVR_NAME,
540 .owner = THIS_MODULE,
542 .resume = tpacpi_resume_handler,
546 /*************************************************************************
547 * thinkpad-acpi driver attributes
550 /* interface_version --------------------------------------------------- */
551 static ssize_t tpacpi_driver_interface_version_show(
552 struct device_driver *drv,
553 char *buf)
555 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
558 static DRIVER_ATTR(interface_version, S_IRUGO,
559 tpacpi_driver_interface_version_show, NULL);
561 /* debug_level --------------------------------------------------------- */
562 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
563 char *buf)
565 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
568 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
569 const char *buf, size_t count)
571 unsigned long t;
573 if (parse_strtoul(buf, 0xffff, &t))
574 return -EINVAL;
576 dbg_level = t;
578 return count;
581 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
582 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
584 /* version ------------------------------------------------------------- */
585 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
586 char *buf)
588 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
591 static DRIVER_ATTR(version, S_IRUGO,
592 tpacpi_driver_version_show, NULL);
594 /* --------------------------------------------------------------------- */
596 static struct driver_attribute* tpacpi_driver_attributes[] = {
597 &driver_attr_debug_level, &driver_attr_version,
598 &driver_attr_interface_version,
601 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
603 int i, res;
605 i = 0;
606 res = 0;
607 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
608 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
609 i++;
612 return res;
615 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
617 int i;
619 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
620 driver_remove_file(drv, tpacpi_driver_attributes[i]);
623 /*************************************************************************
624 * sysfs support helpers
627 struct attribute_set_obj {
628 struct attribute_set s;
629 struct attribute *a;
630 } __attribute__((packed));
632 static struct attribute_set *create_attr_set(unsigned int max_members,
633 const char* name)
635 struct attribute_set_obj *sobj;
637 if (max_members == 0)
638 return NULL;
640 /* Allocates space for implicit NULL at the end too */
641 sobj = kzalloc(sizeof(struct attribute_set_obj) +
642 max_members * sizeof(struct attribute *),
643 GFP_KERNEL);
644 if (!sobj)
645 return NULL;
646 sobj->s.max_members = max_members;
647 sobj->s.group.attrs = &sobj->a;
648 sobj->s.group.name = name;
650 return &sobj->s;
653 /* not multi-threaded safe, use it in a single thread per set */
654 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
656 if (!s || !attr)
657 return -EINVAL;
659 if (s->members >= s->max_members)
660 return -ENOMEM;
662 s->group.attrs[s->members] = attr;
663 s->members++;
665 return 0;
668 static int add_many_to_attr_set(struct attribute_set* s,
669 struct attribute **attr,
670 unsigned int count)
672 int i, res;
674 for (i = 0; i < count; i++) {
675 res = add_to_attr_set(s, attr[i]);
676 if (res)
677 return res;
680 return 0;
683 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
685 sysfs_remove_group(kobj, &s->group);
686 destroy_attr_set(s);
689 static int parse_strtoul(const char *buf,
690 unsigned long max, unsigned long *value)
692 char *endp;
694 *value = simple_strtoul(buf, &endp, 0);
695 while (*endp && isspace(*endp))
696 endp++;
697 if (*endp || *value > max)
698 return -EINVAL;
700 return 0;
703 /****************************************************************************
704 ****************************************************************************
706 * Subdrivers
708 ****************************************************************************
709 ****************************************************************************/
711 /*************************************************************************
712 * thinkpad-acpi init subdriver
715 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
717 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
718 printk(IBM_INFO "%s\n", IBM_URL);
720 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
721 (thinkpad_id.bios_version_str) ?
722 thinkpad_id.bios_version_str : "unknown",
723 (thinkpad_id.ec_version_str) ?
724 thinkpad_id.ec_version_str : "unknown");
726 if (thinkpad_id.vendor && thinkpad_id.model_str)
727 printk(IBM_INFO "%s %s\n",
728 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
729 "IBM" : ((thinkpad_id.vendor ==
730 PCI_VENDOR_ID_LENOVO) ?
731 "Lenovo" : "Unknown vendor"),
732 thinkpad_id.model_str);
734 return 0;
737 static int thinkpad_acpi_driver_read(char *p)
739 int len = 0;
741 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
742 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
744 return len;
747 static struct ibm_struct thinkpad_acpi_driver_data = {
748 .name = "driver",
749 .read = thinkpad_acpi_driver_read,
752 /*************************************************************************
753 * Hotkey subdriver
756 static int hotkey_orig_status;
757 static u32 hotkey_orig_mask;
758 static u32 hotkey_all_mask;
759 static u32 hotkey_reserved_mask;
761 static u16 *hotkey_keycode_map;
763 static struct attribute_set *hotkey_dev_attributes;
765 static int hotkey_get_wlsw(int *status)
767 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
768 return -EIO;
769 return 0;
772 /* sysfs hotkey enable ------------------------------------------------- */
773 static ssize_t hotkey_enable_show(struct device *dev,
774 struct device_attribute *attr,
775 char *buf)
777 int res, status;
778 u32 mask;
780 res = hotkey_get(&status, &mask);
781 if (res)
782 return res;
784 return snprintf(buf, PAGE_SIZE, "%d\n", status);
787 static ssize_t hotkey_enable_store(struct device *dev,
788 struct device_attribute *attr,
789 const char *buf, size_t count)
791 unsigned long t;
792 int res, status;
793 u32 mask;
795 if (parse_strtoul(buf, 1, &t))
796 return -EINVAL;
798 res = hotkey_get(&status, &mask);
799 if (!res)
800 res = hotkey_set(t, mask);
802 return (res) ? res : count;
805 static struct device_attribute dev_attr_hotkey_enable =
806 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
807 hotkey_enable_show, hotkey_enable_store);
809 /* sysfs hotkey mask --------------------------------------------------- */
810 static ssize_t hotkey_mask_show(struct device *dev,
811 struct device_attribute *attr,
812 char *buf)
814 int res, status;
815 u32 mask;
817 res = hotkey_get(&status, &mask);
818 if (res)
819 return res;
821 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
824 static ssize_t hotkey_mask_store(struct device *dev,
825 struct device_attribute *attr,
826 const char *buf, size_t count)
828 unsigned long t;
829 int res, status;
830 u32 mask;
832 if (parse_strtoul(buf, 0xffffffffUL, &t))
833 return -EINVAL;
835 res = hotkey_get(&status, &mask);
836 if (!res)
837 hotkey_set(status, t);
839 return (res) ? res : count;
842 static struct device_attribute dev_attr_hotkey_mask =
843 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
844 hotkey_mask_show, hotkey_mask_store);
846 /* sysfs hotkey bios_enabled ------------------------------------------- */
847 static ssize_t hotkey_bios_enabled_show(struct device *dev,
848 struct device_attribute *attr,
849 char *buf)
851 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
854 static struct device_attribute dev_attr_hotkey_bios_enabled =
855 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
857 /* sysfs hotkey bios_mask ---------------------------------------------- */
858 static ssize_t hotkey_bios_mask_show(struct device *dev,
859 struct device_attribute *attr,
860 char *buf)
862 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
865 static struct device_attribute dev_attr_hotkey_bios_mask =
866 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
868 /* sysfs hotkey all_mask ----------------------------------------------- */
869 static ssize_t hotkey_all_mask_show(struct device *dev,
870 struct device_attribute *attr,
871 char *buf)
873 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
876 static struct device_attribute dev_attr_hotkey_all_mask =
877 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
879 /* sysfs hotkey recommended_mask --------------------------------------- */
880 static ssize_t hotkey_recommended_mask_show(struct device *dev,
881 struct device_attribute *attr,
882 char *buf)
884 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
885 hotkey_all_mask & ~hotkey_reserved_mask);
888 static struct device_attribute dev_attr_hotkey_recommended_mask =
889 __ATTR(hotkey_recommended_mask, S_IRUGO,
890 hotkey_recommended_mask_show, NULL);
892 /* sysfs hotkey radio_sw ----------------------------------------------- */
893 static ssize_t hotkey_radio_sw_show(struct device *dev,
894 struct device_attribute *attr,
895 char *buf)
897 int res, s;
898 res = hotkey_get_wlsw(&s);
899 if (res < 0)
900 return res;
902 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
905 static struct device_attribute dev_attr_hotkey_radio_sw =
906 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
908 /* sysfs hotkey report_mode -------------------------------------------- */
909 static ssize_t hotkey_report_mode_show(struct device *dev,
910 struct device_attribute *attr,
911 char *buf)
913 return snprintf(buf, PAGE_SIZE, "%d\n",
914 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
917 static ssize_t hotkey_report_mode_store(struct device *dev,
918 struct device_attribute *attr,
919 const char *buf, size_t count)
921 unsigned long t;
922 int res = 0;
924 if (parse_strtoul(buf, 2, &t) || (t < 1))
925 return -EINVAL;
927 if (t != hotkey_report_mode) {
928 if (tp_features.hotkey_report_mode_locked)
929 return -EPERM;
930 hotkey_report_mode = t;
931 printk(IBM_NOTICE "hot key report mode set to %d\n",
932 hotkey_report_mode);
935 return (res) ? res : count;
938 static struct device_attribute dev_attr_hotkey_report_mode =
939 __ATTR(hotkey_report_mode, S_IWUSR | S_IRUGO,
940 hotkey_report_mode_show, hotkey_report_mode_store);
942 /* --------------------------------------------------------------------- */
944 static struct attribute *hotkey_attributes[] __initdata = {
945 &dev_attr_hotkey_enable.attr,
946 &dev_attr_hotkey_report_mode.attr,
949 static struct attribute *hotkey_mask_attributes[] __initdata = {
950 &dev_attr_hotkey_mask.attr,
951 &dev_attr_hotkey_bios_enabled.attr,
952 &dev_attr_hotkey_bios_mask.attr,
953 &dev_attr_hotkey_all_mask.attr,
954 &dev_attr_hotkey_recommended_mask.attr,
957 static int __init hotkey_init(struct ibm_init_struct *iibm)
960 static u16 ibm_keycode_map[] __initdata = {
961 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
962 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
963 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
964 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
965 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
966 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
967 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
968 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
969 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
970 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
971 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
972 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
973 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
974 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
975 KEY_RESERVED, /* 0x14: VOLUME UP */
976 KEY_RESERVED, /* 0x15: VOLUME DOWN */
977 KEY_RESERVED, /* 0x16: MUTE */
978 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
979 /* (assignments unknown, please report if found) */
980 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
981 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
983 static u16 lenovo_keycode_map[] __initdata = {
984 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
985 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
986 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
987 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
988 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
989 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
990 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
991 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
992 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
993 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
994 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
995 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
996 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
997 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
998 KEY_RESERVED, /* 0x14: VOLUME UP */
999 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1000 KEY_RESERVED, /* 0x16: MUTE */
1001 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1002 /* (assignments unknown, please report if found) */
1003 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1004 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1007 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1008 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1009 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1011 int res, i;
1012 int status;
1014 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1016 BUG_ON(!tpacpi_inputdev);
1018 IBM_ACPIHANDLE_INIT(hkey);
1019 mutex_init(&hotkey_mutex);
1021 /* hotkey not supported on 570 */
1022 tp_features.hotkey = hkey_handle != NULL;
1024 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1025 str_supported(tp_features.hotkey));
1027 if (tp_features.hotkey) {
1028 hotkey_dev_attributes = create_attr_set(8, NULL);
1029 if (!hotkey_dev_attributes)
1030 return -ENOMEM;
1031 res = add_many_to_attr_set(hotkey_dev_attributes,
1032 hotkey_attributes,
1033 ARRAY_SIZE(hotkey_attributes));
1034 if (res)
1035 return res;
1037 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1038 A30, R30, R31, T20-22, X20-21, X22-24 */
1039 tp_features.hotkey_mask =
1040 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
1042 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1043 str_supported(tp_features.hotkey_mask));
1045 if (tp_features.hotkey_mask) {
1046 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
1047 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1048 "MHKA", "qd"))
1049 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1052 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
1053 if (!res && tp_features.hotkey_mask) {
1054 res = add_many_to_attr_set(hotkey_dev_attributes,
1055 hotkey_mask_attributes,
1056 ARRAY_SIZE(hotkey_mask_attributes));
1059 /* Not all thinkpads have a hardware radio switch */
1060 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1061 tp_features.hotkey_wlsw = 1;
1062 printk(IBM_INFO
1063 "radio switch found; radios are %s\n",
1064 enabled(status, 0));
1065 res = add_to_attr_set(hotkey_dev_attributes,
1066 &dev_attr_hotkey_radio_sw.attr);
1069 if (!res)
1070 res = register_attr_set_with_sysfs(
1071 hotkey_dev_attributes,
1072 &tpacpi_pdev->dev.kobj);
1073 if (res)
1074 return res;
1076 /* Set up key map */
1078 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1079 GFP_KERNEL);
1080 if (!hotkey_keycode_map) {
1081 printk(IBM_ERR "failed to allocate memory for key map\n");
1082 return -ENOMEM;
1085 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1086 dbg_printk(TPACPI_DBG_INIT,
1087 "using Lenovo default hot key map\n");
1088 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1089 TPACPI_HOTKEY_MAP_SIZE);
1090 } else {
1091 dbg_printk(TPACPI_DBG_INIT,
1092 "using IBM default hot key map\n");
1093 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1094 TPACPI_HOTKEY_MAP_SIZE);
1097 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1098 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1099 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1100 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1101 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1102 tpacpi_inputdev->keycode = hotkey_keycode_map;
1103 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1104 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1105 set_bit(hotkey_keycode_map[i],
1106 tpacpi_inputdev->keybit);
1107 } else {
1108 if (i < sizeof(hotkey_reserved_mask)*8)
1109 hotkey_reserved_mask |= 1 << i;
1113 if (tp_features.hotkey_wlsw) {
1114 set_bit(EV_SW, tpacpi_inputdev->evbit);
1115 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1118 dbg_printk(TPACPI_DBG_INIT,
1119 "enabling hot key handling\n");
1120 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1121 | hotkey_orig_mask);
1122 if (res)
1123 return res;
1125 if (hotkey_report_mode > 0) {
1126 tp_features.hotkey_report_mode_locked = 1;
1127 } else {
1128 hotkey_report_mode = 1;
1129 vdbg_printk(TPACPI_DBG_INIT,
1130 "hot key reporting mode can be "
1131 "changed at runtime\n");
1134 dbg_printk(TPACPI_DBG_INIT,
1135 "legacy hot key reporting over procfs %s\n",
1136 (hotkey_report_mode < 2) ?
1137 "enabled" : "disabled");
1140 return (tp_features.hotkey)? 0 : 1;
1143 static void hotkey_exit(void)
1145 int res;
1147 if (tp_features.hotkey) {
1148 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
1149 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1150 if (res)
1151 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
1154 if (hotkey_dev_attributes) {
1155 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1156 hotkey_dev_attributes = NULL;
1160 static void tpacpi_input_send_key(unsigned int scancode,
1161 unsigned int keycode)
1163 if (keycode != KEY_RESERVED) {
1164 input_report_key(tpacpi_inputdev, keycode, 1);
1165 if (keycode == KEY_UNKNOWN)
1166 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1167 scancode);
1168 input_sync(tpacpi_inputdev);
1170 input_report_key(tpacpi_inputdev, keycode, 0);
1171 if (keycode == KEY_UNKNOWN)
1172 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1173 scancode);
1174 input_sync(tpacpi_inputdev);
1178 static void tpacpi_input_send_radiosw(void)
1180 int wlsw;
1182 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw))
1183 input_report_switch(tpacpi_inputdev,
1184 SW_RADIO, !!wlsw);
1187 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1189 u32 hkey;
1190 unsigned int keycode, scancode;
1191 int send_acpi_ev = 0;
1193 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1194 switch (hkey >> 12) {
1195 case 1:
1196 /* 0x1000-0x1FFF: key presses */
1197 scancode = hkey & 0xfff;
1198 if (scancode > 0 && scancode < 0x21) {
1199 scancode--;
1200 keycode = hotkey_keycode_map[scancode];
1201 tpacpi_input_send_key(scancode, keycode);
1202 } else {
1203 printk(IBM_ERR
1204 "hotkey 0x%04x out of range for keyboard map\n",
1205 hkey);
1206 send_acpi_ev = 1;
1208 break;
1209 case 5:
1210 /* 0x5000-0x5FFF: LID */
1211 /* we don't handle it through this path, just
1212 * eat up known LID events */
1213 if (hkey != 0x5001 && hkey != 0x5002) {
1214 printk(IBM_ERR
1215 "unknown LID-related hotkey event: 0x%04x\n",
1216 hkey);
1217 send_acpi_ev = 1;
1219 break;
1220 case 7:
1221 /* 0x7000-0x7FFF: misc */
1222 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1223 tpacpi_input_send_radiosw();
1224 break;
1226 /* fallthrough to default */
1227 default:
1228 /* case 2: dock-related */
1229 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1230 /* case 3: ultra-bay related. maybe bay in dock? */
1231 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1232 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1233 send_acpi_ev = 1;
1235 } else {
1236 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
1237 hkey = 0;
1238 send_acpi_ev = 1;
1241 if (send_acpi_ev || hotkey_report_mode < 2) {
1242 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
1246 static void hotkey_resume(void)
1248 tpacpi_input_send_radiosw();
1252 * Call with hotkey_mutex held
1254 static int hotkey_get(int *status, u32 *mask)
1256 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1257 return -EIO;
1259 if (tp_features.hotkey_mask)
1260 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
1261 return -EIO;
1263 return 0;
1267 * Call with hotkey_mutex held
1269 static int hotkey_set(int status, u32 mask)
1271 int i;
1273 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1274 return -EIO;
1276 if (tp_features.hotkey_mask)
1277 for (i = 0; i < 32; i++) {
1278 int bit = ((1 << i) & mask) != 0;
1279 if (!acpi_evalf(hkey_handle,
1280 NULL, "MHKM", "vdd", i + 1, bit))
1281 return -EIO;
1284 return 0;
1287 /* procfs -------------------------------------------------------------- */
1288 static int hotkey_read(char *p)
1290 int res, status;
1291 u32 mask;
1292 int len = 0;
1294 if (!tp_features.hotkey) {
1295 len += sprintf(p + len, "status:\t\tnot supported\n");
1296 return len;
1299 res = mutex_lock_interruptible(&hotkey_mutex);
1300 if (res < 0)
1301 return res;
1302 res = hotkey_get(&status, &mask);
1303 mutex_unlock(&hotkey_mutex);
1304 if (res)
1305 return res;
1307 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1308 if (tp_features.hotkey_mask) {
1309 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1310 len += sprintf(p + len,
1311 "commands:\tenable, disable, reset, <mask>\n");
1312 } else {
1313 len += sprintf(p + len, "mask:\t\tnot supported\n");
1314 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1317 return len;
1320 static int hotkey_write(char *buf)
1322 int res, status;
1323 u32 mask;
1324 char *cmd;
1325 int do_cmd = 0;
1327 if (!tp_features.hotkey)
1328 return -ENODEV;
1330 res = mutex_lock_interruptible(&hotkey_mutex);
1331 if (res < 0)
1332 return res;
1334 res = hotkey_get(&status, &mask);
1335 if (res)
1336 goto errexit;
1338 res = 0;
1339 while ((cmd = next_cmd(&buf))) {
1340 if (strlencmp(cmd, "enable") == 0) {
1341 status = 1;
1342 } else if (strlencmp(cmd, "disable") == 0) {
1343 status = 0;
1344 } else if (strlencmp(cmd, "reset") == 0) {
1345 status = hotkey_orig_status;
1346 mask = hotkey_orig_mask;
1347 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1348 /* mask set */
1349 } else if (sscanf(cmd, "%x", &mask) == 1) {
1350 /* mask set */
1351 } else {
1352 res = -EINVAL;
1353 goto errexit;
1355 do_cmd = 1;
1358 if (do_cmd)
1359 res = hotkey_set(status, mask);
1361 errexit:
1362 mutex_unlock(&hotkey_mutex);
1363 return res;
1366 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1367 .hid = IBM_HKEY_HID,
1368 .notify = hotkey_notify,
1369 .handle = &hkey_handle,
1370 .type = ACPI_DEVICE_NOTIFY,
1373 static struct ibm_struct hotkey_driver_data = {
1374 .name = "hotkey",
1375 .read = hotkey_read,
1376 .write = hotkey_write,
1377 .exit = hotkey_exit,
1378 .resume = hotkey_resume,
1379 .acpi = &ibm_hotkey_acpidriver,
1382 /*************************************************************************
1383 * Bluetooth subdriver
1386 /* sysfs bluetooth enable ---------------------------------------------- */
1387 static ssize_t bluetooth_enable_show(struct device *dev,
1388 struct device_attribute *attr,
1389 char *buf)
1391 int status;
1393 status = bluetooth_get_radiosw();
1394 if (status < 0)
1395 return status;
1397 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1400 static ssize_t bluetooth_enable_store(struct device *dev,
1401 struct device_attribute *attr,
1402 const char *buf, size_t count)
1404 unsigned long t;
1405 int res;
1407 if (parse_strtoul(buf, 1, &t))
1408 return -EINVAL;
1410 res = bluetooth_set_radiosw(t);
1412 return (res) ? res : count;
1415 static struct device_attribute dev_attr_bluetooth_enable =
1416 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1417 bluetooth_enable_show, bluetooth_enable_store);
1419 /* --------------------------------------------------------------------- */
1421 static struct attribute *bluetooth_attributes[] = {
1422 &dev_attr_bluetooth_enable.attr,
1423 NULL
1426 static const struct attribute_group bluetooth_attr_group = {
1427 .attrs = bluetooth_attributes,
1430 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1432 int res;
1433 int status = 0;
1435 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1437 IBM_ACPIHANDLE_INIT(hkey);
1439 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1440 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1441 tp_features.bluetooth = hkey_handle &&
1442 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1444 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1445 str_supported(tp_features.bluetooth),
1446 status);
1448 if (tp_features.bluetooth) {
1449 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1450 /* no bluetooth hardware present in system */
1451 tp_features.bluetooth = 0;
1452 dbg_printk(TPACPI_DBG_INIT,
1453 "bluetooth hardware not installed\n");
1454 } else {
1455 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1456 &bluetooth_attr_group);
1457 if (res)
1458 return res;
1462 return (tp_features.bluetooth)? 0 : 1;
1465 static void bluetooth_exit(void)
1467 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1468 &bluetooth_attr_group);
1471 static int bluetooth_get_radiosw(void)
1473 int status;
1475 if (!tp_features.bluetooth)
1476 return -ENODEV;
1478 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1479 return -EIO;
1481 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1484 static int bluetooth_set_radiosw(int radio_on)
1486 int status;
1488 if (!tp_features.bluetooth)
1489 return -ENODEV;
1491 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1492 return -EIO;
1493 if (radio_on)
1494 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1495 else
1496 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1497 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1498 return -EIO;
1500 return 0;
1503 /* procfs -------------------------------------------------------------- */
1504 static int bluetooth_read(char *p)
1506 int len = 0;
1507 int status = bluetooth_get_radiosw();
1509 if (!tp_features.bluetooth)
1510 len += sprintf(p + len, "status:\t\tnot supported\n");
1511 else {
1512 len += sprintf(p + len, "status:\t\t%s\n",
1513 (status)? "enabled" : "disabled");
1514 len += sprintf(p + len, "commands:\tenable, disable\n");
1517 return len;
1520 static int bluetooth_write(char *buf)
1522 char *cmd;
1524 if (!tp_features.bluetooth)
1525 return -ENODEV;
1527 while ((cmd = next_cmd(&buf))) {
1528 if (strlencmp(cmd, "enable") == 0) {
1529 bluetooth_set_radiosw(1);
1530 } else if (strlencmp(cmd, "disable") == 0) {
1531 bluetooth_set_radiosw(0);
1532 } else
1533 return -EINVAL;
1536 return 0;
1539 static struct ibm_struct bluetooth_driver_data = {
1540 .name = "bluetooth",
1541 .read = bluetooth_read,
1542 .write = bluetooth_write,
1543 .exit = bluetooth_exit,
1546 /*************************************************************************
1547 * Wan subdriver
1550 /* sysfs wan enable ---------------------------------------------------- */
1551 static ssize_t wan_enable_show(struct device *dev,
1552 struct device_attribute *attr,
1553 char *buf)
1555 int status;
1557 status = wan_get_radiosw();
1558 if (status < 0)
1559 return status;
1561 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1564 static ssize_t wan_enable_store(struct device *dev,
1565 struct device_attribute *attr,
1566 const char *buf, size_t count)
1568 unsigned long t;
1569 int res;
1571 if (parse_strtoul(buf, 1, &t))
1572 return -EINVAL;
1574 res = wan_set_radiosw(t);
1576 return (res) ? res : count;
1579 static struct device_attribute dev_attr_wan_enable =
1580 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1581 wan_enable_show, wan_enable_store);
1583 /* --------------------------------------------------------------------- */
1585 static struct attribute *wan_attributes[] = {
1586 &dev_attr_wan_enable.attr,
1587 NULL
1590 static const struct attribute_group wan_attr_group = {
1591 .attrs = wan_attributes,
1594 static int __init wan_init(struct ibm_init_struct *iibm)
1596 int res;
1597 int status = 0;
1599 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1601 IBM_ACPIHANDLE_INIT(hkey);
1603 tp_features.wan = hkey_handle &&
1604 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1606 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1607 str_supported(tp_features.wan),
1608 status);
1610 if (tp_features.wan) {
1611 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1612 /* no wan hardware present in system */
1613 tp_features.wan = 0;
1614 dbg_printk(TPACPI_DBG_INIT,
1615 "wan hardware not installed\n");
1616 } else {
1617 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1618 &wan_attr_group);
1619 if (res)
1620 return res;
1624 return (tp_features.wan)? 0 : 1;
1627 static void wan_exit(void)
1629 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1630 &wan_attr_group);
1633 static int wan_get_radiosw(void)
1635 int status;
1637 if (!tp_features.wan)
1638 return -ENODEV;
1640 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1641 return -EIO;
1643 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1646 static int wan_set_radiosw(int radio_on)
1648 int status;
1650 if (!tp_features.wan)
1651 return -ENODEV;
1653 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1654 return -EIO;
1655 if (radio_on)
1656 status |= TP_ACPI_WANCARD_RADIOSSW;
1657 else
1658 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1659 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1660 return -EIO;
1662 return 0;
1665 /* procfs -------------------------------------------------------------- */
1666 static int wan_read(char *p)
1668 int len = 0;
1669 int status = wan_get_radiosw();
1671 if (!tp_features.wan)
1672 len += sprintf(p + len, "status:\t\tnot supported\n");
1673 else {
1674 len += sprintf(p + len, "status:\t\t%s\n",
1675 (status)? "enabled" : "disabled");
1676 len += sprintf(p + len, "commands:\tenable, disable\n");
1679 return len;
1682 static int wan_write(char *buf)
1684 char *cmd;
1686 if (!tp_features.wan)
1687 return -ENODEV;
1689 while ((cmd = next_cmd(&buf))) {
1690 if (strlencmp(cmd, "enable") == 0) {
1691 wan_set_radiosw(1);
1692 } else if (strlencmp(cmd, "disable") == 0) {
1693 wan_set_radiosw(0);
1694 } else
1695 return -EINVAL;
1698 return 0;
1701 static struct ibm_struct wan_driver_data = {
1702 .name = "wan",
1703 .read = wan_read,
1704 .write = wan_write,
1705 .exit = wan_exit,
1706 .flags.experimental = 1,
1709 /*************************************************************************
1710 * Video subdriver
1713 static enum video_access_mode video_supported;
1714 static int video_orig_autosw;
1716 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1717 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1718 "\\_SB.PCI0.VID0", /* 770e */
1719 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1720 "\\_SB.PCI0.AGP.VID", /* all others */
1721 ); /* R30, R31 */
1723 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1725 static int __init video_init(struct ibm_init_struct *iibm)
1727 int ivga;
1729 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1731 IBM_ACPIHANDLE_INIT(vid);
1732 IBM_ACPIHANDLE_INIT(vid2);
1734 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1735 /* G41, assume IVGA doesn't change */
1736 vid_handle = vid2_handle;
1738 if (!vid_handle)
1739 /* video switching not supported on R30, R31 */
1740 video_supported = TPACPI_VIDEO_NONE;
1741 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1742 /* 570 */
1743 video_supported = TPACPI_VIDEO_570;
1744 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1745 /* 600e/x, 770e, 770x */
1746 video_supported = TPACPI_VIDEO_770;
1747 else
1748 /* all others */
1749 video_supported = TPACPI_VIDEO_NEW;
1751 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1752 str_supported(video_supported != TPACPI_VIDEO_NONE),
1753 video_supported);
1755 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1758 static void video_exit(void)
1760 dbg_printk(TPACPI_DBG_EXIT,
1761 "restoring original video autoswitch mode\n");
1762 if (video_autosw_set(video_orig_autosw))
1763 printk(IBM_ERR "error while trying to restore original "
1764 "video autoswitch mode\n");
1767 static int video_outputsw_get(void)
1769 int status = 0;
1770 int i;
1772 switch (video_supported) {
1773 case TPACPI_VIDEO_570:
1774 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1775 TP_ACPI_VIDEO_570_PHSCMD))
1776 return -EIO;
1777 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1778 break;
1779 case TPACPI_VIDEO_770:
1780 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1781 return -EIO;
1782 if (i)
1783 status |= TP_ACPI_VIDEO_S_LCD;
1784 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1785 return -EIO;
1786 if (i)
1787 status |= TP_ACPI_VIDEO_S_CRT;
1788 break;
1789 case TPACPI_VIDEO_NEW:
1790 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1791 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1792 return -EIO;
1793 if (i)
1794 status |= TP_ACPI_VIDEO_S_CRT;
1796 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1797 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1798 return -EIO;
1799 if (i)
1800 status |= TP_ACPI_VIDEO_S_LCD;
1801 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1802 return -EIO;
1803 if (i)
1804 status |= TP_ACPI_VIDEO_S_DVI;
1805 break;
1806 default:
1807 return -ENOSYS;
1810 return status;
1813 static int video_outputsw_set(int status)
1815 int autosw;
1816 int res = 0;
1818 switch (video_supported) {
1819 case TPACPI_VIDEO_570:
1820 res = acpi_evalf(NULL, NULL,
1821 "\\_SB.PHS2", "vdd",
1822 TP_ACPI_VIDEO_570_PHS2CMD,
1823 status | TP_ACPI_VIDEO_570_PHS2SET);
1824 break;
1825 case TPACPI_VIDEO_770:
1826 autosw = video_autosw_get();
1827 if (autosw < 0)
1828 return autosw;
1830 res = video_autosw_set(1);
1831 if (res)
1832 return res;
1833 res = acpi_evalf(vid_handle, NULL,
1834 "ASWT", "vdd", status * 0x100, 0);
1835 if (!autosw && video_autosw_set(autosw)) {
1836 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1837 return -EIO;
1839 break;
1840 case TPACPI_VIDEO_NEW:
1841 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1842 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1843 break;
1844 default:
1845 return -ENOSYS;
1848 return (res)? 0 : -EIO;
1851 static int video_autosw_get(void)
1853 int autosw = 0;
1855 switch (video_supported) {
1856 case TPACPI_VIDEO_570:
1857 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1858 return -EIO;
1859 break;
1860 case TPACPI_VIDEO_770:
1861 case TPACPI_VIDEO_NEW:
1862 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1863 return -EIO;
1864 break;
1865 default:
1866 return -ENOSYS;
1869 return autosw & 1;
1872 static int video_autosw_set(int enable)
1874 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1875 return -EIO;
1876 return 0;
1879 static int video_outputsw_cycle(void)
1881 int autosw = video_autosw_get();
1882 int res;
1884 if (autosw < 0)
1885 return autosw;
1887 switch (video_supported) {
1888 case TPACPI_VIDEO_570:
1889 res = video_autosw_set(1);
1890 if (res)
1891 return res;
1892 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1893 break;
1894 case TPACPI_VIDEO_770:
1895 case TPACPI_VIDEO_NEW:
1896 res = video_autosw_set(1);
1897 if (res)
1898 return res;
1899 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1900 break;
1901 default:
1902 return -ENOSYS;
1904 if (!autosw && video_autosw_set(autosw)) {
1905 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1906 return -EIO;
1909 return (res)? 0 : -EIO;
1912 static int video_expand_toggle(void)
1914 switch (video_supported) {
1915 case TPACPI_VIDEO_570:
1916 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1917 0 : -EIO;
1918 case TPACPI_VIDEO_770:
1919 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1920 0 : -EIO;
1921 case TPACPI_VIDEO_NEW:
1922 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1923 0 : -EIO;
1924 default:
1925 return -ENOSYS;
1927 /* not reached */
1930 static int video_read(char *p)
1932 int status, autosw;
1933 int len = 0;
1935 if (video_supported == TPACPI_VIDEO_NONE) {
1936 len += sprintf(p + len, "status:\t\tnot supported\n");
1937 return len;
1940 status = video_outputsw_get();
1941 if (status < 0)
1942 return status;
1944 autosw = video_autosw_get();
1945 if (autosw < 0)
1946 return autosw;
1948 len += sprintf(p + len, "status:\t\tsupported\n");
1949 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1950 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1951 if (video_supported == TPACPI_VIDEO_NEW)
1952 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1953 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1954 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1955 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1956 if (video_supported == TPACPI_VIDEO_NEW)
1957 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1958 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1959 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1961 return len;
1964 static int video_write(char *buf)
1966 char *cmd;
1967 int enable, disable, status;
1968 int res;
1970 if (video_supported == TPACPI_VIDEO_NONE)
1971 return -ENODEV;
1973 enable = 0;
1974 disable = 0;
1976 while ((cmd = next_cmd(&buf))) {
1977 if (strlencmp(cmd, "lcd_enable") == 0) {
1978 enable |= TP_ACPI_VIDEO_S_LCD;
1979 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1980 disable |= TP_ACPI_VIDEO_S_LCD;
1981 } else if (strlencmp(cmd, "crt_enable") == 0) {
1982 enable |= TP_ACPI_VIDEO_S_CRT;
1983 } else if (strlencmp(cmd, "crt_disable") == 0) {
1984 disable |= TP_ACPI_VIDEO_S_CRT;
1985 } else if (video_supported == TPACPI_VIDEO_NEW &&
1986 strlencmp(cmd, "dvi_enable") == 0) {
1987 enable |= TP_ACPI_VIDEO_S_DVI;
1988 } else if (video_supported == TPACPI_VIDEO_NEW &&
1989 strlencmp(cmd, "dvi_disable") == 0) {
1990 disable |= TP_ACPI_VIDEO_S_DVI;
1991 } else if (strlencmp(cmd, "auto_enable") == 0) {
1992 res = video_autosw_set(1);
1993 if (res)
1994 return res;
1995 } else if (strlencmp(cmd, "auto_disable") == 0) {
1996 res = video_autosw_set(0);
1997 if (res)
1998 return res;
1999 } else if (strlencmp(cmd, "video_switch") == 0) {
2000 res = video_outputsw_cycle();
2001 if (res)
2002 return res;
2003 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2004 res = video_expand_toggle();
2005 if (res)
2006 return res;
2007 } else
2008 return -EINVAL;
2011 if (enable || disable) {
2012 status = video_outputsw_get();
2013 if (status < 0)
2014 return status;
2015 res = video_outputsw_set((status & ~disable) | enable);
2016 if (res)
2017 return res;
2020 return 0;
2023 static struct ibm_struct video_driver_data = {
2024 .name = "video",
2025 .read = video_read,
2026 .write = video_write,
2027 .exit = video_exit,
2030 /*************************************************************************
2031 * Light (thinklight) subdriver
2034 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2035 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2037 static int __init light_init(struct ibm_init_struct *iibm)
2039 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2041 IBM_ACPIHANDLE_INIT(ledb);
2042 IBM_ACPIHANDLE_INIT(lght);
2043 IBM_ACPIHANDLE_INIT(cmos);
2045 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2046 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2048 if (tp_features.light)
2049 /* light status not supported on
2050 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2051 tp_features.light_status =
2052 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2054 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2055 str_supported(tp_features.light));
2057 return (tp_features.light)? 0 : 1;
2060 static int light_read(char *p)
2062 int len = 0;
2063 int status = 0;
2065 if (!tp_features.light) {
2066 len += sprintf(p + len, "status:\t\tnot supported\n");
2067 } else if (!tp_features.light_status) {
2068 len += sprintf(p + len, "status:\t\tunknown\n");
2069 len += sprintf(p + len, "commands:\ton, off\n");
2070 } else {
2071 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2072 return -EIO;
2073 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2074 len += sprintf(p + len, "commands:\ton, off\n");
2077 return len;
2080 static int light_write(char *buf)
2082 int cmos_cmd, lght_cmd;
2083 char *cmd;
2084 int success;
2086 if (!tp_features.light)
2087 return -ENODEV;
2089 while ((cmd = next_cmd(&buf))) {
2090 if (strlencmp(cmd, "on") == 0) {
2091 cmos_cmd = 0x0c;
2092 lght_cmd = 1;
2093 } else if (strlencmp(cmd, "off") == 0) {
2094 cmos_cmd = 0x0d;
2095 lght_cmd = 0;
2096 } else
2097 return -EINVAL;
2099 success = cmos_handle ?
2100 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2101 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2102 if (!success)
2103 return -EIO;
2106 return 0;
2109 static struct ibm_struct light_driver_data = {
2110 .name = "light",
2111 .read = light_read,
2112 .write = light_write,
2115 /*************************************************************************
2116 * Dock subdriver
2119 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2121 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2122 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2123 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2124 "\\_SB.PCI.ISA.SLCE", /* 570 */
2125 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2127 /* don't list other alternatives as we install a notify handler on the 570 */
2128 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2130 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2132 .notify = dock_notify,
2133 .handle = &dock_handle,
2134 .type = ACPI_SYSTEM_NOTIFY,
2137 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2138 * We just use it to get notifications of dock hotplug
2139 * in very old thinkpads */
2140 .hid = PCI_ROOT_HID_STRING,
2141 .notify = dock_notify,
2142 .handle = &pci_handle,
2143 .type = ACPI_SYSTEM_NOTIFY,
2147 static struct ibm_struct dock_driver_data[2] = {
2149 .name = "dock",
2150 .read = dock_read,
2151 .write = dock_write,
2152 .acpi = &ibm_dock_acpidriver[0],
2155 .name = "dock",
2156 .acpi = &ibm_dock_acpidriver[1],
2160 #define dock_docked() (_sta(dock_handle) & 1)
2162 static int __init dock_init(struct ibm_init_struct *iibm)
2164 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2166 IBM_ACPIHANDLE_INIT(dock);
2168 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2169 str_supported(dock_handle != NULL));
2171 return (dock_handle)? 0 : 1;
2174 static int __init dock_init2(struct ibm_init_struct *iibm)
2176 int dock2_needed;
2178 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2180 if (dock_driver_data[0].flags.acpi_driver_registered &&
2181 dock_driver_data[0].flags.acpi_notify_installed) {
2182 IBM_ACPIHANDLE_INIT(pci);
2183 dock2_needed = (pci_handle != NULL);
2184 vdbg_printk(TPACPI_DBG_INIT,
2185 "dock PCI handler for the TP 570 is %s\n",
2186 str_supported(dock2_needed));
2187 } else {
2188 vdbg_printk(TPACPI_DBG_INIT,
2189 "dock subdriver part 2 not required\n");
2190 dock2_needed = 0;
2193 return (dock2_needed)? 0 : 1;
2196 static void dock_notify(struct ibm_struct *ibm, u32 event)
2198 int docked = dock_docked();
2199 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, PCI_ROOT_HID_STRING);
2201 if (event == 1 && !pci) /* 570 */
2202 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2203 else if (event == 1 && pci) /* 570 */
2204 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2205 else if (event == 3 && docked)
2206 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2207 else if (event == 3 && !docked)
2208 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
2209 else if (event == 0 && docked)
2210 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2211 else {
2212 printk(IBM_ERR "unknown dock event %d, status %d\n",
2213 event, _sta(dock_handle));
2214 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
2218 static int dock_read(char *p)
2220 int len = 0;
2221 int docked = dock_docked();
2223 if (!dock_handle)
2224 len += sprintf(p + len, "status:\t\tnot supported\n");
2225 else if (!docked)
2226 len += sprintf(p + len, "status:\t\tundocked\n");
2227 else {
2228 len += sprintf(p + len, "status:\t\tdocked\n");
2229 len += sprintf(p + len, "commands:\tdock, undock\n");
2232 return len;
2235 static int dock_write(char *buf)
2237 char *cmd;
2239 if (!dock_docked())
2240 return -ENODEV;
2242 while ((cmd = next_cmd(&buf))) {
2243 if (strlencmp(cmd, "undock") == 0) {
2244 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2245 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2246 return -EIO;
2247 } else if (strlencmp(cmd, "dock") == 0) {
2248 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2249 return -EIO;
2250 } else
2251 return -EINVAL;
2254 return 0;
2257 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2259 /*************************************************************************
2260 * Bay subdriver
2263 #ifdef CONFIG_THINKPAD_ACPI_BAY
2264 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2265 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2266 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2267 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2268 ); /* A21e, R30, R31 */
2269 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2270 "_EJ0", /* all others */
2271 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2272 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2273 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2274 ); /* all others */
2275 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2276 "_EJ0", /* 770x */
2277 ); /* all others */
2279 static int __init bay_init(struct ibm_init_struct *iibm)
2281 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2283 IBM_ACPIHANDLE_INIT(bay);
2284 if (bay_handle)
2285 IBM_ACPIHANDLE_INIT(bay_ej);
2286 IBM_ACPIHANDLE_INIT(bay2);
2287 if (bay2_handle)
2288 IBM_ACPIHANDLE_INIT(bay2_ej);
2290 tp_features.bay_status = bay_handle &&
2291 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2292 tp_features.bay_status2 = bay2_handle &&
2293 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2295 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2296 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2297 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2298 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2300 vdbg_printk(TPACPI_DBG_INIT,
2301 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2302 str_supported(tp_features.bay_status),
2303 str_supported(tp_features.bay_eject),
2304 str_supported(tp_features.bay_status2),
2305 str_supported(tp_features.bay_eject2));
2307 return (tp_features.bay_status || tp_features.bay_eject ||
2308 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2311 static void bay_notify(struct ibm_struct *ibm, u32 event)
2313 acpi_bus_generate_event(ibm->acpi->device, event, 0);
2316 #define bay_occupied(b) (_sta(b##_handle) & 1)
2318 static int bay_read(char *p)
2320 int len = 0;
2321 int occupied = bay_occupied(bay);
2322 int occupied2 = bay_occupied(bay2);
2323 int eject, eject2;
2325 len += sprintf(p + len, "status:\t\t%s\n",
2326 tp_features.bay_status ?
2327 (occupied ? "occupied" : "unoccupied") :
2328 "not supported");
2329 if (tp_features.bay_status2)
2330 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2331 "occupied" : "unoccupied");
2333 eject = tp_features.bay_eject && occupied;
2334 eject2 = tp_features.bay_eject2 && occupied2;
2336 if (eject && eject2)
2337 len += sprintf(p + len, "commands:\teject, eject2\n");
2338 else if (eject)
2339 len += sprintf(p + len, "commands:\teject\n");
2340 else if (eject2)
2341 len += sprintf(p + len, "commands:\teject2\n");
2343 return len;
2346 static int bay_write(char *buf)
2348 char *cmd;
2350 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2351 return -ENODEV;
2353 while ((cmd = next_cmd(&buf))) {
2354 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2355 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2356 return -EIO;
2357 } else if (tp_features.bay_eject2 &&
2358 strlencmp(cmd, "eject2") == 0) {
2359 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2360 return -EIO;
2361 } else
2362 return -EINVAL;
2365 return 0;
2368 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2369 .notify = bay_notify,
2370 .handle = &bay_handle,
2371 .type = ACPI_SYSTEM_NOTIFY,
2374 static struct ibm_struct bay_driver_data = {
2375 .name = "bay",
2376 .read = bay_read,
2377 .write = bay_write,
2378 .acpi = &ibm_bay_acpidriver,
2381 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2383 /*************************************************************************
2384 * CMOS subdriver
2387 /* sysfs cmos_command -------------------------------------------------- */
2388 static ssize_t cmos_command_store(struct device *dev,
2389 struct device_attribute *attr,
2390 const char *buf, size_t count)
2392 unsigned long cmos_cmd;
2393 int res;
2395 if (parse_strtoul(buf, 21, &cmos_cmd))
2396 return -EINVAL;
2398 res = issue_thinkpad_cmos_command(cmos_cmd);
2399 return (res)? res : count;
2402 static struct device_attribute dev_attr_cmos_command =
2403 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2405 /* --------------------------------------------------------------------- */
2407 static int __init cmos_init(struct ibm_init_struct *iibm)
2409 int res;
2411 vdbg_printk(TPACPI_DBG_INIT,
2412 "initializing cmos commands subdriver\n");
2414 IBM_ACPIHANDLE_INIT(cmos);
2416 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2417 str_supported(cmos_handle != NULL));
2419 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2420 if (res)
2421 return res;
2423 return (cmos_handle)? 0 : 1;
2426 static void cmos_exit(void)
2428 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2431 static int cmos_read(char *p)
2433 int len = 0;
2435 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2436 R30, R31, T20-22, X20-21 */
2437 if (!cmos_handle)
2438 len += sprintf(p + len, "status:\t\tnot supported\n");
2439 else {
2440 len += sprintf(p + len, "status:\t\tsupported\n");
2441 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2444 return len;
2447 static int cmos_write(char *buf)
2449 char *cmd;
2450 int cmos_cmd, res;
2452 while ((cmd = next_cmd(&buf))) {
2453 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2454 cmos_cmd >= 0 && cmos_cmd <= 21) {
2455 /* cmos_cmd set */
2456 } else
2457 return -EINVAL;
2459 res = issue_thinkpad_cmos_command(cmos_cmd);
2460 if (res)
2461 return res;
2464 return 0;
2467 static struct ibm_struct cmos_driver_data = {
2468 .name = "cmos",
2469 .read = cmos_read,
2470 .write = cmos_write,
2471 .exit = cmos_exit,
2474 /*************************************************************************
2475 * LED subdriver
2478 static enum led_access_mode led_supported;
2480 IBM_HANDLE(led, ec, "SLED", /* 570 */
2481 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2482 "LED", /* all others */
2483 ); /* R30, R31 */
2485 static int __init led_init(struct ibm_init_struct *iibm)
2487 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2489 IBM_ACPIHANDLE_INIT(led);
2491 if (!led_handle)
2492 /* led not supported on R30, R31 */
2493 led_supported = TPACPI_LED_NONE;
2494 else if (strlencmp(led_path, "SLED") == 0)
2495 /* 570 */
2496 led_supported = TPACPI_LED_570;
2497 else if (strlencmp(led_path, "SYSL") == 0)
2498 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2499 led_supported = TPACPI_LED_OLD;
2500 else
2501 /* all others */
2502 led_supported = TPACPI_LED_NEW;
2504 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2505 str_supported(led_supported), led_supported);
2507 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2510 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2512 static int led_read(char *p)
2514 int len = 0;
2516 if (!led_supported) {
2517 len += sprintf(p + len, "status:\t\tnot supported\n");
2518 return len;
2520 len += sprintf(p + len, "status:\t\tsupported\n");
2522 if (led_supported == TPACPI_LED_570) {
2523 /* 570 */
2524 int i, status;
2525 for (i = 0; i < 8; i++) {
2526 if (!acpi_evalf(ec_handle,
2527 &status, "GLED", "dd", 1 << i))
2528 return -EIO;
2529 len += sprintf(p + len, "%d:\t\t%s\n",
2530 i, led_status(status));
2534 len += sprintf(p + len, "commands:\t"
2535 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2537 return len;
2540 /* off, on, blink */
2541 static const int led_sled_arg1[] = { 0, 1, 3 };
2542 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2543 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2544 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2546 static int led_write(char *buf)
2548 char *cmd;
2549 int led, ind, ret;
2551 if (!led_supported)
2552 return -ENODEV;
2554 while ((cmd = next_cmd(&buf))) {
2555 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2556 return -EINVAL;
2558 if (strstr(cmd, "off")) {
2559 ind = 0;
2560 } else if (strstr(cmd, "on")) {
2561 ind = 1;
2562 } else if (strstr(cmd, "blink")) {
2563 ind = 2;
2564 } else
2565 return -EINVAL;
2567 if (led_supported == TPACPI_LED_570) {
2568 /* 570 */
2569 led = 1 << led;
2570 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2571 led, led_sled_arg1[ind]))
2572 return -EIO;
2573 } else if (led_supported == TPACPI_LED_OLD) {
2574 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2575 led = 1 << led;
2576 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2577 if (ret >= 0)
2578 ret =
2579 ec_write(TPACPI_LED_EC_HLBL,
2580 led * led_exp_hlbl[ind]);
2581 if (ret >= 0)
2582 ret =
2583 ec_write(TPACPI_LED_EC_HLCL,
2584 led * led_exp_hlcl[ind]);
2585 if (ret < 0)
2586 return ret;
2587 } else {
2588 /* all others */
2589 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2590 led, led_led_arg1[ind]))
2591 return -EIO;
2595 return 0;
2598 static struct ibm_struct led_driver_data = {
2599 .name = "led",
2600 .read = led_read,
2601 .write = led_write,
2604 /*************************************************************************
2605 * Beep subdriver
2608 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2610 static int __init beep_init(struct ibm_init_struct *iibm)
2612 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2614 IBM_ACPIHANDLE_INIT(beep);
2616 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2617 str_supported(beep_handle != NULL));
2619 return (beep_handle)? 0 : 1;
2622 static int beep_read(char *p)
2624 int len = 0;
2626 if (!beep_handle)
2627 len += sprintf(p + len, "status:\t\tnot supported\n");
2628 else {
2629 len += sprintf(p + len, "status:\t\tsupported\n");
2630 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2633 return len;
2636 static int beep_write(char *buf)
2638 char *cmd;
2639 int beep_cmd;
2641 if (!beep_handle)
2642 return -ENODEV;
2644 while ((cmd = next_cmd(&buf))) {
2645 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2646 beep_cmd >= 0 && beep_cmd <= 17) {
2647 /* beep_cmd set */
2648 } else
2649 return -EINVAL;
2650 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2651 return -EIO;
2654 return 0;
2657 static struct ibm_struct beep_driver_data = {
2658 .name = "beep",
2659 .read = beep_read,
2660 .write = beep_write,
2663 /*************************************************************************
2664 * Thermal subdriver
2667 static enum thermal_access_mode thermal_read_mode;
2669 /* sysfs temp##_input -------------------------------------------------- */
2671 static ssize_t thermal_temp_input_show(struct device *dev,
2672 struct device_attribute *attr,
2673 char *buf)
2675 struct sensor_device_attribute *sensor_attr =
2676 to_sensor_dev_attr(attr);
2677 int idx = sensor_attr->index;
2678 s32 value;
2679 int res;
2681 res = thermal_get_sensor(idx, &value);
2682 if (res)
2683 return res;
2684 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2685 return -ENXIO;
2687 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2690 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2691 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2693 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2694 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2695 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2696 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2697 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2698 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2699 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2700 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2701 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2702 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2703 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2704 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2705 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2706 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2707 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2708 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2709 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2712 #define THERMAL_ATTRS(X) \
2713 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2715 static struct attribute *thermal_temp_input_attr[] = {
2716 THERMAL_ATTRS(8),
2717 THERMAL_ATTRS(9),
2718 THERMAL_ATTRS(10),
2719 THERMAL_ATTRS(11),
2720 THERMAL_ATTRS(12),
2721 THERMAL_ATTRS(13),
2722 THERMAL_ATTRS(14),
2723 THERMAL_ATTRS(15),
2724 THERMAL_ATTRS(0),
2725 THERMAL_ATTRS(1),
2726 THERMAL_ATTRS(2),
2727 THERMAL_ATTRS(3),
2728 THERMAL_ATTRS(4),
2729 THERMAL_ATTRS(5),
2730 THERMAL_ATTRS(6),
2731 THERMAL_ATTRS(7),
2732 NULL
2735 static const struct attribute_group thermal_temp_input16_group = {
2736 .attrs = thermal_temp_input_attr
2739 static const struct attribute_group thermal_temp_input8_group = {
2740 .attrs = &thermal_temp_input_attr[8]
2743 #undef THERMAL_SENSOR_ATTR_TEMP
2744 #undef THERMAL_ATTRS
2746 /* --------------------------------------------------------------------- */
2748 static int __init thermal_init(struct ibm_init_struct *iibm)
2750 u8 t, ta1, ta2;
2751 int i;
2752 int acpi_tmp7;
2753 int res;
2755 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2757 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2759 if (thinkpad_id.ec_model) {
2761 * Direct EC access mode: sensors at registers
2762 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2763 * non-implemented, thermal sensors return 0x80 when
2764 * not available
2767 ta1 = ta2 = 0;
2768 for (i = 0; i < 8; i++) {
2769 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2770 ta1 |= t;
2771 } else {
2772 ta1 = 0;
2773 break;
2775 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2776 ta2 |= t;
2777 } else {
2778 ta1 = 0;
2779 break;
2782 if (ta1 == 0) {
2783 /* This is sheer paranoia, but we handle it anyway */
2784 if (acpi_tmp7) {
2785 printk(IBM_ERR
2786 "ThinkPad ACPI EC access misbehaving, "
2787 "falling back to ACPI TMPx access mode\n");
2788 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2789 } else {
2790 printk(IBM_ERR
2791 "ThinkPad ACPI EC access misbehaving, "
2792 "disabling thermal sensors access\n");
2793 thermal_read_mode = TPACPI_THERMAL_NONE;
2795 } else {
2796 thermal_read_mode =
2797 (ta2 != 0) ?
2798 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2800 } else if (acpi_tmp7) {
2801 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2802 /* 600e/x, 770e, 770x */
2803 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2804 } else {
2805 /* Standard ACPI TMPx access, max 8 sensors */
2806 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2808 } else {
2809 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2810 thermal_read_mode = TPACPI_THERMAL_NONE;
2813 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2814 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2815 thermal_read_mode);
2817 switch(thermal_read_mode) {
2818 case TPACPI_THERMAL_TPEC_16:
2819 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2820 &thermal_temp_input16_group);
2821 if (res)
2822 return res;
2823 break;
2824 case TPACPI_THERMAL_TPEC_8:
2825 case TPACPI_THERMAL_ACPI_TMP07:
2826 case TPACPI_THERMAL_ACPI_UPDT:
2827 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2828 &thermal_temp_input8_group);
2829 if (res)
2830 return res;
2831 break;
2832 case TPACPI_THERMAL_NONE:
2833 default:
2834 return 1;
2837 return 0;
2840 static void thermal_exit(void)
2842 switch(thermal_read_mode) {
2843 case TPACPI_THERMAL_TPEC_16:
2844 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2845 &thermal_temp_input16_group);
2846 break;
2847 case TPACPI_THERMAL_TPEC_8:
2848 case TPACPI_THERMAL_ACPI_TMP07:
2849 case TPACPI_THERMAL_ACPI_UPDT:
2850 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2851 &thermal_temp_input16_group);
2852 break;
2853 case TPACPI_THERMAL_NONE:
2854 default:
2855 break;
2859 /* idx is zero-based */
2860 static int thermal_get_sensor(int idx, s32 *value)
2862 int t;
2863 s8 tmp;
2864 char tmpi[5];
2866 t = TP_EC_THERMAL_TMP0;
2868 switch (thermal_read_mode) {
2869 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2870 case TPACPI_THERMAL_TPEC_16:
2871 if (idx >= 8 && idx <= 15) {
2872 t = TP_EC_THERMAL_TMP8;
2873 idx -= 8;
2875 /* fallthrough */
2876 #endif
2877 case TPACPI_THERMAL_TPEC_8:
2878 if (idx <= 7) {
2879 if (!acpi_ec_read(t + idx, &tmp))
2880 return -EIO;
2881 *value = tmp * 1000;
2882 return 0;
2884 break;
2886 case TPACPI_THERMAL_ACPI_UPDT:
2887 if (idx <= 7) {
2888 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2889 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2890 return -EIO;
2891 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2892 return -EIO;
2893 *value = (t - 2732) * 100;
2894 return 0;
2896 break;
2898 case TPACPI_THERMAL_ACPI_TMP07:
2899 if (idx <= 7) {
2900 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2901 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2902 return -EIO;
2903 if (t > 127 || t < -127)
2904 t = TP_EC_THERMAL_TMP_NA;
2905 *value = t * 1000;
2906 return 0;
2908 break;
2910 case TPACPI_THERMAL_NONE:
2911 default:
2912 return -ENOSYS;
2915 return -EINVAL;
2918 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2920 int res, i;
2921 int n;
2923 n = 8;
2924 i = 0;
2926 if (!s)
2927 return -EINVAL;
2929 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2930 n = 16;
2932 for(i = 0 ; i < n; i++) {
2933 res = thermal_get_sensor(i, &s->temp[i]);
2934 if (res)
2935 return res;
2938 return n;
2941 static int thermal_read(char *p)
2943 int len = 0;
2944 int n, i;
2945 struct ibm_thermal_sensors_struct t;
2947 n = thermal_get_sensors(&t);
2948 if (unlikely(n < 0))
2949 return n;
2951 len += sprintf(p + len, "temperatures:\t");
2953 if (n > 0) {
2954 for (i = 0; i < (n - 1); i++)
2955 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2956 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2957 } else
2958 len += sprintf(p + len, "not supported\n");
2960 return len;
2963 static struct ibm_struct thermal_driver_data = {
2964 .name = "thermal",
2965 .read = thermal_read,
2966 .exit = thermal_exit,
2969 /*************************************************************************
2970 * EC Dump subdriver
2973 static u8 ecdump_regs[256];
2975 static int ecdump_read(char *p)
2977 int len = 0;
2978 int i, j;
2979 u8 v;
2981 len += sprintf(p + len, "EC "
2982 " +00 +01 +02 +03 +04 +05 +06 +07"
2983 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2984 for (i = 0; i < 256; i += 16) {
2985 len += sprintf(p + len, "EC 0x%02x:", i);
2986 for (j = 0; j < 16; j++) {
2987 if (!acpi_ec_read(i + j, &v))
2988 break;
2989 if (v != ecdump_regs[i + j])
2990 len += sprintf(p + len, " *%02x", v);
2991 else
2992 len += sprintf(p + len, " %02x", v);
2993 ecdump_regs[i + j] = v;
2995 len += sprintf(p + len, "\n");
2996 if (j != 16)
2997 break;
3000 /* These are way too dangerous to advertise openly... */
3001 #if 0
3002 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3003 " (<offset> is 00-ff, <value> is 00-ff)\n");
3004 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3005 " (<offset> is 00-ff, <value> is 0-255)\n");
3006 #endif
3007 return len;
3010 static int ecdump_write(char *buf)
3012 char *cmd;
3013 int i, v;
3015 while ((cmd = next_cmd(&buf))) {
3016 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3017 /* i and v set */
3018 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3019 /* i and v set */
3020 } else
3021 return -EINVAL;
3022 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3023 if (!acpi_ec_write(i, v))
3024 return -EIO;
3025 } else
3026 return -EINVAL;
3029 return 0;
3032 static struct ibm_struct ecdump_driver_data = {
3033 .name = "ecdump",
3034 .read = ecdump_read,
3035 .write = ecdump_write,
3036 .flags.experimental = 1,
3039 /*************************************************************************
3040 * Backlight/brightness subdriver
3043 static struct backlight_device *ibm_backlight_device;
3045 static struct backlight_properties ibm_backlight_data = {
3046 .owner = THIS_MODULE,
3047 .get_brightness = brightness_get,
3048 .update_status = brightness_update_status,
3049 .max_brightness = 7,
3052 static struct mutex brightness_mutex;
3054 static int __init brightness_init(struct ibm_init_struct *iibm)
3056 int b;
3058 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3060 mutex_init(&brightness_mutex);
3062 if (!brightness_mode) {
3063 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3064 brightness_mode = 2;
3065 else
3066 brightness_mode = 3;
3068 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3069 brightness_mode);
3072 if (brightness_mode > 3)
3073 return -EINVAL;
3075 b = brightness_get(NULL);
3076 if (b < 0)
3077 return 1;
3079 ibm_backlight_device = backlight_device_register(
3080 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3081 &ibm_backlight_data);
3082 if (IS_ERR(ibm_backlight_device)) {
3083 printk(IBM_ERR "Could not register backlight device\n");
3084 return PTR_ERR(ibm_backlight_device);
3086 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
3088 ibm_backlight_device->props->brightness = b;
3089 brightness_update_status(ibm_backlight_device);
3091 return 0;
3094 static void brightness_exit(void)
3096 if (ibm_backlight_device) {
3097 vdbg_printk(TPACPI_DBG_EXIT,
3098 "calling backlight_device_unregister()\n");
3099 backlight_device_unregister(ibm_backlight_device);
3100 ibm_backlight_device = NULL;
3104 static int brightness_update_status(struct backlight_device *bd)
3106 return brightness_set(
3107 (bd->props->fb_blank == FB_BLANK_UNBLANK &&
3108 bd->props->power == FB_BLANK_UNBLANK) ?
3109 bd->props->brightness : 0);
3113 * ThinkPads can read brightness from two places: EC 0x31, or
3114 * CMOS NVRAM byte 0x5E, bits 0-3.
3116 static int brightness_get(struct backlight_device *bd)
3118 u8 lec = 0, lcmos = 0, level = 0;
3120 if (brightness_mode & 1) {
3121 if (!acpi_ec_read(brightness_offset, &lec))
3122 return -EIO;
3123 lec &= 7;
3124 level = lec;
3126 if (brightness_mode & 2) {
3127 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3128 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3129 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3130 level = lcmos;
3133 if (brightness_mode == 3 && lec != lcmos) {
3134 printk(IBM_ERR
3135 "CMOS NVRAM (%u) and EC (%u) do not agree "
3136 "on display brightness level\n",
3137 (unsigned int) lcmos,
3138 (unsigned int) lec);
3139 return -EIO;
3142 return level;
3145 static int brightness_set(int value)
3147 int cmos_cmd, inc, i, res;
3148 int current_value;
3150 if (value > 7)
3151 return -EINVAL;
3153 res = mutex_lock_interruptible(&brightness_mutex);
3154 if (res < 0)
3155 return res;
3157 current_value = brightness_get(NULL);
3158 if (current_value < 0) {
3159 res = current_value;
3160 goto errout;
3163 cmos_cmd = value > current_value ?
3164 TP_CMOS_BRIGHTNESS_UP :
3165 TP_CMOS_BRIGHTNESS_DOWN;
3166 inc = value > current_value ? 1 : -1;
3168 res = 0;
3169 for (i = current_value; i != value; i += inc) {
3170 if ((brightness_mode & 2) &&
3171 issue_thinkpad_cmos_command(cmos_cmd)) {
3172 res = -EIO;
3173 goto errout;
3175 if ((brightness_mode & 1) &&
3176 !acpi_ec_write(brightness_offset, i + inc)) {
3177 res = -EIO;
3178 goto errout;;
3182 errout:
3183 mutex_unlock(&brightness_mutex);
3184 return res;
3187 static int brightness_read(char *p)
3189 int len = 0;
3190 int level;
3192 if ((level = brightness_get(NULL)) < 0) {
3193 len += sprintf(p + len, "level:\t\tunreadable\n");
3194 } else {
3195 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3196 len += sprintf(p + len, "commands:\tup, down\n");
3197 len += sprintf(p + len, "commands:\tlevel <level>"
3198 " (<level> is 0-7)\n");
3201 return len;
3204 static int brightness_write(char *buf)
3206 int level;
3207 int new_level;
3208 char *cmd;
3210 while ((cmd = next_cmd(&buf))) {
3211 if ((level = brightness_get(NULL)) < 0)
3212 return level;
3213 level &= 7;
3215 if (strlencmp(cmd, "up") == 0) {
3216 new_level = level == 7 ? 7 : level + 1;
3217 } else if (strlencmp(cmd, "down") == 0) {
3218 new_level = level == 0 ? 0 : level - 1;
3219 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3220 new_level >= 0 && new_level <= 7) {
3221 /* new_level set */
3222 } else
3223 return -EINVAL;
3225 brightness_set(new_level);
3228 return 0;
3231 static struct ibm_struct brightness_driver_data = {
3232 .name = "brightness",
3233 .read = brightness_read,
3234 .write = brightness_write,
3235 .exit = brightness_exit,
3238 /*************************************************************************
3239 * Volume subdriver
3242 static int volume_read(char *p)
3244 int len = 0;
3245 u8 level;
3247 if (!acpi_ec_read(volume_offset, &level)) {
3248 len += sprintf(p + len, "level:\t\tunreadable\n");
3249 } else {
3250 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3251 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3252 len += sprintf(p + len, "commands:\tup, down, mute\n");
3253 len += sprintf(p + len, "commands:\tlevel <level>"
3254 " (<level> is 0-15)\n");
3257 return len;
3260 static int volume_write(char *buf)
3262 int cmos_cmd, inc, i;
3263 u8 level, mute;
3264 int new_level, new_mute;
3265 char *cmd;
3267 while ((cmd = next_cmd(&buf))) {
3268 if (!acpi_ec_read(volume_offset, &level))
3269 return -EIO;
3270 new_mute = mute = level & 0x40;
3271 new_level = level = level & 0xf;
3273 if (strlencmp(cmd, "up") == 0) {
3274 if (mute)
3275 new_mute = 0;
3276 else
3277 new_level = level == 15 ? 15 : level + 1;
3278 } else if (strlencmp(cmd, "down") == 0) {
3279 if (mute)
3280 new_mute = 0;
3281 else
3282 new_level = level == 0 ? 0 : level - 1;
3283 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3284 new_level >= 0 && new_level <= 15) {
3285 /* new_level set */
3286 } else if (strlencmp(cmd, "mute") == 0) {
3287 new_mute = 0x40;
3288 } else
3289 return -EINVAL;
3291 if (new_level != level) { /* mute doesn't change */
3292 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3293 inc = new_level > level ? 1 : -1;
3295 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3296 !acpi_ec_write(volume_offset, level)))
3297 return -EIO;
3299 for (i = level; i != new_level; i += inc)
3300 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3301 !acpi_ec_write(volume_offset, i + inc))
3302 return -EIO;
3304 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3305 !acpi_ec_write(volume_offset,
3306 new_level + mute)))
3307 return -EIO;
3310 if (new_mute != mute) { /* level doesn't change */
3311 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3313 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3314 !acpi_ec_write(volume_offset, level + new_mute))
3315 return -EIO;
3319 return 0;
3322 static struct ibm_struct volume_driver_data = {
3323 .name = "volume",
3324 .read = volume_read,
3325 .write = volume_write,
3328 /*************************************************************************
3329 * Fan subdriver
3333 * FAN ACCESS MODES
3335 * TPACPI_FAN_RD_ACPI_GFAN:
3336 * ACPI GFAN method: returns fan level
3338 * see TPACPI_FAN_WR_ACPI_SFAN
3339 * EC 0x2f (HFSP) not available if GFAN exists
3341 * TPACPI_FAN_WR_ACPI_SFAN:
3342 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3344 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3345 * it for writing.
3347 * TPACPI_FAN_WR_TPEC:
3348 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3349 * Supported on almost all ThinkPads
3351 * Fan speed changes of any sort (including those caused by the
3352 * disengaged mode) are usually done slowly by the firmware as the
3353 * maximum ammount of fan duty cycle change per second seems to be
3354 * limited.
3356 * Reading is not available if GFAN exists.
3357 * Writing is not available if SFAN exists.
3359 * Bits
3360 * 7 automatic mode engaged;
3361 * (default operation mode of the ThinkPad)
3362 * fan level is ignored in this mode.
3363 * 6 full speed mode (takes precedence over bit 7);
3364 * not available on all thinkpads. May disable
3365 * the tachometer while the fan controller ramps up
3366 * the speed (which can take up to a few *minutes*).
3367 * Speeds up fan to 100% duty-cycle, which is far above
3368 * the standard RPM levels. It is not impossible that
3369 * it could cause hardware damage.
3370 * 5-3 unused in some models. Extra bits for fan level
3371 * in others, but still useless as all values above
3372 * 7 map to the same speed as level 7 in these models.
3373 * 2-0 fan level (0..7 usually)
3374 * 0x00 = stop
3375 * 0x07 = max (set when temperatures critical)
3376 * Some ThinkPads may have other levels, see
3377 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3379 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3380 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3381 * does so, its initial value is meaningless (0x07).
3383 * For firmware bugs, refer to:
3384 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3386 * ----
3388 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3389 * Main fan tachometer reading (in RPM)
3391 * This register is present on all ThinkPads with a new-style EC, and
3392 * it is known not to be present on the A21m/e, and T22, as there is
3393 * something else in offset 0x84 according to the ACPI DSDT. Other
3394 * ThinkPads from this same time period (and earlier) probably lack the
3395 * tachometer as well.
3397 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3398 * was never fixed by IBM to report the EC firmware version string
3399 * probably support the tachometer (like the early X models), so
3400 * detecting it is quite hard. We need more data to know for sure.
3402 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3403 * might result.
3405 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3406 * mode.
3408 * For firmware bugs, refer to:
3409 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3411 * TPACPI_FAN_WR_ACPI_FANS:
3412 * ThinkPad X31, X40, X41. Not available in the X60.
3414 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3415 * high speed. ACPI DSDT seems to map these three speeds to levels
3416 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3417 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3419 * The speeds are stored on handles
3420 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3422 * There are three default speed sets, acessible as handles:
3423 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3425 * ACPI DSDT switches which set is in use depending on various
3426 * factors.
3428 * TPACPI_FAN_WR_TPEC is also available and should be used to
3429 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3430 * but the ACPI tables just mention level 7.
3433 static enum fan_status_access_mode fan_status_access_mode;
3434 static enum fan_control_access_mode fan_control_access_mode;
3435 static enum fan_control_commands fan_control_commands;
3437 static u8 fan_control_initial_status;
3438 static u8 fan_control_desired_level;
3440 static void fan_watchdog_fire(struct work_struct *ignored);
3441 static int fan_watchdog_maxinterval;
3442 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3444 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3445 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3446 "\\FSPD", /* 600e/x, 770e, 770x */
3447 ); /* all others */
3448 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3449 "JFNS", /* 770x-JL */
3450 ); /* all others */
3453 * SYSFS fan layout: hwmon compatible (device)
3455 * pwm*_enable:
3456 * 0: "disengaged" mode
3457 * 1: manual mode
3458 * 2: native EC "auto" mode (recommended, hardware default)
3460 * pwm*: set speed in manual mode, ignored otherwise.
3461 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3462 * interpolation.
3464 * fan*_input: tachometer reading, RPM
3467 * SYSFS fan layout: extensions
3469 * fan_watchdog (driver):
3470 * fan watchdog interval in seconds, 0 disables (default), max 120
3473 /* sysfs fan pwm1_enable ----------------------------------------------- */
3474 static ssize_t fan_pwm1_enable_show(struct device *dev,
3475 struct device_attribute *attr,
3476 char *buf)
3478 int res, mode;
3479 u8 status;
3481 res = fan_get_status_safe(&status);
3482 if (res)
3483 return res;
3485 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3486 if (status != fan_control_initial_status) {
3487 tp_features.fan_ctrl_status_undef = 0;
3488 } else {
3489 /* Return most likely status. In fact, it
3490 * might be the only possible status */
3491 status = TP_EC_FAN_AUTO;
3495 if (status & TP_EC_FAN_FULLSPEED) {
3496 mode = 0;
3497 } else if (status & TP_EC_FAN_AUTO) {
3498 mode = 2;
3499 } else
3500 mode = 1;
3502 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3505 static ssize_t fan_pwm1_enable_store(struct device *dev,
3506 struct device_attribute *attr,
3507 const char *buf, size_t count)
3509 unsigned long t;
3510 int res, level;
3512 if (parse_strtoul(buf, 2, &t))
3513 return -EINVAL;
3515 switch (t) {
3516 case 0:
3517 level = TP_EC_FAN_FULLSPEED;
3518 break;
3519 case 1:
3520 level = TPACPI_FAN_LAST_LEVEL;
3521 break;
3522 case 2:
3523 level = TP_EC_FAN_AUTO;
3524 break;
3525 case 3:
3526 /* reserved for software-controlled auto mode */
3527 return -ENOSYS;
3528 default:
3529 return -EINVAL;
3532 res = fan_set_level_safe(level);
3533 if (res == -ENXIO)
3534 return -EINVAL;
3535 else if (res < 0)
3536 return res;
3538 fan_watchdog_reset();
3540 return count;
3543 static struct device_attribute dev_attr_fan_pwm1_enable =
3544 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3545 fan_pwm1_enable_show, fan_pwm1_enable_store);
3547 /* sysfs fan pwm1 ------------------------------------------------------ */
3548 static ssize_t fan_pwm1_show(struct device *dev,
3549 struct device_attribute *attr,
3550 char *buf)
3552 int res;
3553 u8 status;
3555 res = fan_get_status_safe(&status);
3556 if (res)
3557 return res;
3559 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3560 if (status != fan_control_initial_status) {
3561 tp_features.fan_ctrl_status_undef = 0;
3562 } else {
3563 status = TP_EC_FAN_AUTO;
3567 if ((status &
3568 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3569 status = fan_control_desired_level;
3571 if (status > 7)
3572 status = 7;
3574 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3577 static ssize_t fan_pwm1_store(struct device *dev,
3578 struct device_attribute *attr,
3579 const char *buf, size_t count)
3581 unsigned long s;
3582 int rc;
3583 u8 status, newlevel;
3585 if (parse_strtoul(buf, 255, &s))
3586 return -EINVAL;
3588 /* scale down from 0-255 to 0-7 */
3589 newlevel = (s >> 5) & 0x07;
3591 rc = mutex_lock_interruptible(&fan_mutex);
3592 if (rc < 0)
3593 return rc;
3595 rc = fan_get_status(&status);
3596 if (!rc && (status &
3597 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3598 rc = fan_set_level(newlevel);
3599 if (rc == -ENXIO)
3600 rc = -EINVAL;
3601 else if (!rc) {
3602 fan_update_desired_level(newlevel);
3603 fan_watchdog_reset();
3607 mutex_unlock(&fan_mutex);
3608 return (rc)? rc : count;
3611 static struct device_attribute dev_attr_fan_pwm1 =
3612 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3613 fan_pwm1_show, fan_pwm1_store);
3615 /* sysfs fan fan1_input ------------------------------------------------ */
3616 static ssize_t fan_fan1_input_show(struct device *dev,
3617 struct device_attribute *attr,
3618 char *buf)
3620 int res;
3621 unsigned int speed;
3623 res = fan_get_speed(&speed);
3624 if (res < 0)
3625 return res;
3627 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3630 static struct device_attribute dev_attr_fan_fan1_input =
3631 __ATTR(fan1_input, S_IRUGO,
3632 fan_fan1_input_show, NULL);
3634 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3635 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3636 char *buf)
3638 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3641 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3642 const char *buf, size_t count)
3644 unsigned long t;
3646 if (parse_strtoul(buf, 120, &t))
3647 return -EINVAL;
3649 if (!fan_control_allowed)
3650 return -EPERM;
3652 fan_watchdog_maxinterval = t;
3653 fan_watchdog_reset();
3655 return count;
3658 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3659 fan_fan_watchdog_show, fan_fan_watchdog_store);
3661 /* --------------------------------------------------------------------- */
3662 static struct attribute *fan_attributes[] = {
3663 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3664 &dev_attr_fan_fan1_input.attr,
3665 NULL
3668 static const struct attribute_group fan_attr_group = {
3669 .attrs = fan_attributes,
3672 static int __init fan_init(struct ibm_init_struct *iibm)
3674 int rc;
3676 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3678 mutex_init(&fan_mutex);
3679 fan_status_access_mode = TPACPI_FAN_NONE;
3680 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3681 fan_control_commands = 0;
3682 fan_watchdog_maxinterval = 0;
3683 tp_features.fan_ctrl_status_undef = 0;
3684 fan_control_desired_level = 7;
3686 IBM_ACPIHANDLE_INIT(fans);
3687 IBM_ACPIHANDLE_INIT(gfan);
3688 IBM_ACPIHANDLE_INIT(sfan);
3690 if (gfan_handle) {
3691 /* 570, 600e/x, 770e, 770x */
3692 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3693 } else {
3694 /* all other ThinkPads: note that even old-style
3695 * ThinkPad ECs supports the fan control register */
3696 if (likely(acpi_ec_read(fan_status_offset,
3697 &fan_control_initial_status))) {
3698 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3700 /* In some ThinkPads, neither the EC nor the ACPI
3701 * DSDT initialize the fan status, and it ends up
3702 * being set to 0x07 when it *could* be either
3703 * 0x07 or 0x80.
3705 * Enable for TP-1Y (T43), TP-78 (R51e),
3706 * TP-76 (R52), TP-70 (T43, R52), which are known
3707 * to be buggy. */
3708 if (fan_control_initial_status == 0x07) {
3709 switch (thinkpad_id.ec_model) {
3710 case 0x5931: /* TP-1Y */
3711 case 0x3837: /* TP-78 */
3712 case 0x3637: /* TP-76 */
3713 case 0x3037: /* TP-70 */
3714 printk(IBM_NOTICE
3715 "fan_init: initial fan status is "
3716 "unknown, assuming it is in auto "
3717 "mode\n");
3718 tp_features.fan_ctrl_status_undef = 1;
3722 } else {
3723 printk(IBM_ERR
3724 "ThinkPad ACPI EC access misbehaving, "
3725 "fan status and control unavailable\n");
3726 return 1;
3730 if (sfan_handle) {
3731 /* 570, 770x-JL */
3732 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3733 fan_control_commands |=
3734 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3735 } else {
3736 if (!gfan_handle) {
3737 /* gfan without sfan means no fan control */
3738 /* all other models implement TP EC 0x2f control */
3740 if (fans_handle) {
3741 /* X31, X40, X41 */
3742 fan_control_access_mode =
3743 TPACPI_FAN_WR_ACPI_FANS;
3744 fan_control_commands |=
3745 TPACPI_FAN_CMD_SPEED |
3746 TPACPI_FAN_CMD_LEVEL |
3747 TPACPI_FAN_CMD_ENABLE;
3748 } else {
3749 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3750 fan_control_commands |=
3751 TPACPI_FAN_CMD_LEVEL |
3752 TPACPI_FAN_CMD_ENABLE;
3757 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3758 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3759 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3760 fan_status_access_mode, fan_control_access_mode);
3762 /* fan control master switch */
3763 if (!fan_control_allowed) {
3764 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3765 fan_control_commands = 0;
3766 dbg_printk(TPACPI_DBG_INIT,
3767 "fan control features disabled by parameter\n");
3770 /* update fan_control_desired_level */
3771 if (fan_status_access_mode != TPACPI_FAN_NONE)
3772 fan_get_status_safe(NULL);
3774 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3775 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3776 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3777 &fan_attr_group);
3778 if (!(rc < 0))
3779 rc = driver_create_file(&tpacpi_pdriver.driver,
3780 &driver_attr_fan_watchdog);
3781 if (rc < 0)
3782 return rc;
3783 return 0;
3784 } else
3785 return 1;
3789 * Call with fan_mutex held
3791 static void fan_update_desired_level(u8 status)
3793 if ((status &
3794 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3795 if (status > 7)
3796 fan_control_desired_level = 7;
3797 else
3798 fan_control_desired_level = status;
3802 static int fan_get_status(u8 *status)
3804 u8 s;
3806 /* TODO:
3807 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3809 switch (fan_status_access_mode) {
3810 case TPACPI_FAN_RD_ACPI_GFAN:
3811 /* 570, 600e/x, 770e, 770x */
3813 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3814 return -EIO;
3816 if (likely(status))
3817 *status = s & 0x07;
3819 break;
3821 case TPACPI_FAN_RD_TPEC:
3822 /* all except 570, 600e/x, 770e, 770x */
3823 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3824 return -EIO;
3826 if (likely(status))
3827 *status = s;
3829 break;
3831 default:
3832 return -ENXIO;
3835 return 0;
3838 static int fan_get_status_safe(u8 *status)
3840 int rc;
3841 u8 s;
3843 rc = mutex_lock_interruptible(&fan_mutex);
3844 if (rc < 0)
3845 return rc;
3846 rc = fan_get_status(&s);
3847 if (!rc)
3848 fan_update_desired_level(s);
3849 mutex_unlock(&fan_mutex);
3851 if (status)
3852 *status = s;
3854 return rc;
3857 static void fan_exit(void)
3859 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3861 /* FIXME: can we really do this unconditionally? */
3862 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3863 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3865 cancel_delayed_work(&fan_watchdog_task);
3866 flush_scheduled_work();
3869 static int fan_get_speed(unsigned int *speed)
3871 u8 hi, lo;
3873 switch (fan_status_access_mode) {
3874 case TPACPI_FAN_RD_TPEC:
3875 /* all except 570, 600e/x, 770e, 770x */
3876 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3877 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3878 return -EIO;
3880 if (likely(speed))
3881 *speed = (hi << 8) | lo;
3883 break;
3885 default:
3886 return -ENXIO;
3889 return 0;
3892 static void fan_watchdog_fire(struct work_struct *ignored)
3894 int rc;
3896 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3897 rc = fan_set_enable();
3898 if (rc < 0) {
3899 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3900 "will try again later...\n", -rc);
3901 /* reschedule for later */
3902 fan_watchdog_reset();
3906 static void fan_watchdog_reset(void)
3908 static int fan_watchdog_active;
3910 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3911 return;
3913 if (fan_watchdog_active)
3914 cancel_delayed_work(&fan_watchdog_task);
3916 if (fan_watchdog_maxinterval > 0) {
3917 fan_watchdog_active = 1;
3918 if (!schedule_delayed_work(&fan_watchdog_task,
3919 msecs_to_jiffies(fan_watchdog_maxinterval
3920 * 1000))) {
3921 printk(IBM_ERR "failed to schedule the fan watchdog, "
3922 "watchdog will not trigger\n");
3924 } else
3925 fan_watchdog_active = 0;
3928 static int fan_set_level(int level)
3930 if (!fan_control_allowed)
3931 return -EPERM;
3933 switch (fan_control_access_mode) {
3934 case TPACPI_FAN_WR_ACPI_SFAN:
3935 if (level >= 0 && level <= 7) {
3936 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3937 return -EIO;
3938 } else
3939 return -EINVAL;
3940 break;
3942 case TPACPI_FAN_WR_ACPI_FANS:
3943 case TPACPI_FAN_WR_TPEC:
3944 if ((level != TP_EC_FAN_AUTO) &&
3945 (level != TP_EC_FAN_FULLSPEED) &&
3946 ((level < 0) || (level > 7)))
3947 return -EINVAL;
3949 /* safety net should the EC not support AUTO
3950 * or FULLSPEED mode bits and just ignore them */
3951 if (level & TP_EC_FAN_FULLSPEED)
3952 level |= 7; /* safety min speed 7 */
3953 else if (level & TP_EC_FAN_FULLSPEED)
3954 level |= 4; /* safety min speed 4 */
3956 if (!acpi_ec_write(fan_status_offset, level))
3957 return -EIO;
3958 else
3959 tp_features.fan_ctrl_status_undef = 0;
3960 break;
3962 default:
3963 return -ENXIO;
3965 return 0;
3968 static int fan_set_level_safe(int level)
3970 int rc;
3972 if (!fan_control_allowed)
3973 return -EPERM;
3975 rc = mutex_lock_interruptible(&fan_mutex);
3976 if (rc < 0)
3977 return rc;
3979 if (level == TPACPI_FAN_LAST_LEVEL)
3980 level = fan_control_desired_level;
3982 rc = fan_set_level(level);
3983 if (!rc)
3984 fan_update_desired_level(level);
3986 mutex_unlock(&fan_mutex);
3987 return rc;
3990 static int fan_set_enable(void)
3992 u8 s;
3993 int rc;
3995 if (!fan_control_allowed)
3996 return -EPERM;
3998 rc = mutex_lock_interruptible(&fan_mutex);
3999 if (rc < 0)
4000 return rc;
4002 switch (fan_control_access_mode) {
4003 case TPACPI_FAN_WR_ACPI_FANS:
4004 case TPACPI_FAN_WR_TPEC:
4005 rc = fan_get_status(&s);
4006 if (rc < 0)
4007 break;
4009 /* Don't go out of emergency fan mode */
4010 if (s != 7) {
4011 s &= 0x07;
4012 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4015 if (!acpi_ec_write(fan_status_offset, s))
4016 rc = -EIO;
4017 else {
4018 tp_features.fan_ctrl_status_undef = 0;
4019 rc = 0;
4021 break;
4023 case TPACPI_FAN_WR_ACPI_SFAN:
4024 rc = fan_get_status(&s);
4025 if (rc < 0)
4026 break;
4028 s &= 0x07;
4030 /* Set fan to at least level 4 */
4031 s |= 4;
4033 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4034 rc= -EIO;
4035 else
4036 rc = 0;
4037 break;
4039 default:
4040 rc = -ENXIO;
4043 mutex_unlock(&fan_mutex);
4044 return rc;
4047 static int fan_set_disable(void)
4049 int rc;
4051 if (!fan_control_allowed)
4052 return -EPERM;
4054 rc = mutex_lock_interruptible(&fan_mutex);
4055 if (rc < 0)
4056 return rc;
4058 rc = 0;
4059 switch (fan_control_access_mode) {
4060 case TPACPI_FAN_WR_ACPI_FANS:
4061 case TPACPI_FAN_WR_TPEC:
4062 if (!acpi_ec_write(fan_status_offset, 0x00))
4063 rc = -EIO;
4064 else {
4065 fan_control_desired_level = 0;
4066 tp_features.fan_ctrl_status_undef = 0;
4068 break;
4070 case TPACPI_FAN_WR_ACPI_SFAN:
4071 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4072 rc = -EIO;
4073 else
4074 fan_control_desired_level = 0;
4075 break;
4077 default:
4078 rc = -ENXIO;
4082 mutex_unlock(&fan_mutex);
4083 return rc;
4086 static int fan_set_speed(int speed)
4088 int rc;
4090 if (!fan_control_allowed)
4091 return -EPERM;
4093 rc = mutex_lock_interruptible(&fan_mutex);
4094 if (rc < 0)
4095 return rc;
4097 rc = 0;
4098 switch (fan_control_access_mode) {
4099 case TPACPI_FAN_WR_ACPI_FANS:
4100 if (speed >= 0 && speed <= 65535) {
4101 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4102 speed, speed, speed))
4103 rc = -EIO;
4104 } else
4105 rc = -EINVAL;
4106 break;
4108 default:
4109 rc = -ENXIO;
4112 mutex_unlock(&fan_mutex);
4113 return rc;
4116 static int fan_read(char *p)
4118 int len = 0;
4119 int rc;
4120 u8 status;
4121 unsigned int speed = 0;
4123 switch (fan_status_access_mode) {
4124 case TPACPI_FAN_RD_ACPI_GFAN:
4125 /* 570, 600e/x, 770e, 770x */
4126 if ((rc = fan_get_status_safe(&status)) < 0)
4127 return rc;
4129 len += sprintf(p + len, "status:\t\t%s\n"
4130 "level:\t\t%d\n",
4131 (status != 0) ? "enabled" : "disabled", status);
4132 break;
4134 case TPACPI_FAN_RD_TPEC:
4135 /* all except 570, 600e/x, 770e, 770x */
4136 if ((rc = fan_get_status_safe(&status)) < 0)
4137 return rc;
4139 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4140 if (status != fan_control_initial_status)
4141 tp_features.fan_ctrl_status_undef = 0;
4142 else
4143 /* Return most likely status. In fact, it
4144 * might be the only possible status */
4145 status = TP_EC_FAN_AUTO;
4148 len += sprintf(p + len, "status:\t\t%s\n",
4149 (status != 0) ? "enabled" : "disabled");
4151 if ((rc = fan_get_speed(&speed)) < 0)
4152 return rc;
4154 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4156 if (status & TP_EC_FAN_FULLSPEED)
4157 /* Disengaged mode takes precedence */
4158 len += sprintf(p + len, "level:\t\tdisengaged\n");
4159 else if (status & TP_EC_FAN_AUTO)
4160 len += sprintf(p + len, "level:\t\tauto\n");
4161 else
4162 len += sprintf(p + len, "level:\t\t%d\n", status);
4163 break;
4165 case TPACPI_FAN_NONE:
4166 default:
4167 len += sprintf(p + len, "status:\t\tnot supported\n");
4170 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
4171 len += sprintf(p + len, "commands:\tlevel <level>");
4173 switch (fan_control_access_mode) {
4174 case TPACPI_FAN_WR_ACPI_SFAN:
4175 len += sprintf(p + len, " (<level> is 0-7)\n");
4176 break;
4178 default:
4179 len += sprintf(p + len, " (<level> is 0-7, "
4180 "auto, disengaged, full-speed)\n");
4181 break;
4185 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
4186 len += sprintf(p + len, "commands:\tenable, disable\n"
4187 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4188 "1-120 (seconds))\n");
4190 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
4191 len += sprintf(p + len, "commands:\tspeed <speed>"
4192 " (<speed> is 0-65535)\n");
4194 return len;
4197 static int fan_write_cmd_level(const char *cmd, int *rc)
4199 int level;
4201 if (strlencmp(cmd, "level auto") == 0)
4202 level = TP_EC_FAN_AUTO;
4203 else if ((strlencmp(cmd, "level disengaged") == 0) |
4204 (strlencmp(cmd, "level full-speed") == 0))
4205 level = TP_EC_FAN_FULLSPEED;
4206 else if (sscanf(cmd, "level %d", &level) != 1)
4207 return 0;
4209 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4210 printk(IBM_ERR "level command accepted for unsupported "
4211 "access mode %d", fan_control_access_mode);
4213 return 1;
4216 static int fan_write_cmd_enable(const char *cmd, int *rc)
4218 if (strlencmp(cmd, "enable") != 0)
4219 return 0;
4221 if ((*rc = fan_set_enable()) == -ENXIO)
4222 printk(IBM_ERR "enable command accepted for unsupported "
4223 "access mode %d", fan_control_access_mode);
4225 return 1;
4228 static int fan_write_cmd_disable(const char *cmd, int *rc)
4230 if (strlencmp(cmd, "disable") != 0)
4231 return 0;
4233 if ((*rc = fan_set_disable()) == -ENXIO)
4234 printk(IBM_ERR "disable command accepted for unsupported "
4235 "access mode %d", fan_control_access_mode);
4237 return 1;
4240 static int fan_write_cmd_speed(const char *cmd, int *rc)
4242 int speed;
4244 /* TODO:
4245 * Support speed <low> <medium> <high> ? */
4247 if (sscanf(cmd, "speed %d", &speed) != 1)
4248 return 0;
4250 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4251 printk(IBM_ERR "speed command accepted for unsupported "
4252 "access mode %d", fan_control_access_mode);
4254 return 1;
4257 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4259 int interval;
4261 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4262 return 0;
4264 if (interval < 0 || interval > 120)
4265 *rc = -EINVAL;
4266 else
4267 fan_watchdog_maxinterval = interval;
4269 return 1;
4272 static int fan_write(char *buf)
4274 char *cmd;
4275 int rc = 0;
4277 while (!rc && (cmd = next_cmd(&buf))) {
4278 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4279 fan_write_cmd_level(cmd, &rc)) &&
4280 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4281 (fan_write_cmd_enable(cmd, &rc) ||
4282 fan_write_cmd_disable(cmd, &rc) ||
4283 fan_write_cmd_watchdog(cmd, &rc))) &&
4284 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4285 fan_write_cmd_speed(cmd, &rc))
4287 rc = -EINVAL;
4288 else if (!rc)
4289 fan_watchdog_reset();
4292 return rc;
4295 static struct ibm_struct fan_driver_data = {
4296 .name = "fan",
4297 .read = fan_read,
4298 .write = fan_write,
4299 .exit = fan_exit,
4302 /****************************************************************************
4303 ****************************************************************************
4305 * Infrastructure
4307 ****************************************************************************
4308 ****************************************************************************/
4310 /* /proc support */
4311 static struct proc_dir_entry *proc_dir;
4313 /* Subdriver registry */
4314 static LIST_HEAD(tpacpi_all_drivers);
4318 * Module and infrastructure proble, init and exit handling
4321 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4322 static const char * __init str_supported(int is_supported)
4324 static char text_unsupported[] __initdata = "not supported";
4326 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4328 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4330 static int __init ibm_init(struct ibm_init_struct *iibm)
4332 int ret;
4333 struct ibm_struct *ibm = iibm->data;
4334 struct proc_dir_entry *entry;
4336 BUG_ON(ibm == NULL);
4338 INIT_LIST_HEAD(&ibm->all_drivers);
4340 if (ibm->flags.experimental && !experimental)
4341 return 0;
4343 dbg_printk(TPACPI_DBG_INIT,
4344 "probing for %s\n", ibm->name);
4346 if (iibm->init) {
4347 ret = iibm->init(iibm);
4348 if (ret > 0)
4349 return 0; /* probe failed */
4350 if (ret)
4351 return ret;
4353 ibm->flags.init_called = 1;
4356 if (ibm->acpi) {
4357 if (ibm->acpi->hid) {
4358 ret = register_tpacpi_subdriver(ibm);
4359 if (ret)
4360 goto err_out;
4363 if (ibm->acpi->notify) {
4364 ret = setup_acpi_notify(ibm);
4365 if (ret == -ENODEV) {
4366 printk(IBM_NOTICE "disabling subdriver %s\n",
4367 ibm->name);
4368 ret = 0;
4369 goto err_out;
4371 if (ret < 0)
4372 goto err_out;
4376 dbg_printk(TPACPI_DBG_INIT,
4377 "%s installed\n", ibm->name);
4379 if (ibm->read) {
4380 entry = create_proc_entry(ibm->name,
4381 S_IFREG | S_IRUGO | S_IWUSR,
4382 proc_dir);
4383 if (!entry) {
4384 printk(IBM_ERR "unable to create proc entry %s\n",
4385 ibm->name);
4386 ret = -ENODEV;
4387 goto err_out;
4389 entry->owner = THIS_MODULE;
4390 entry->data = ibm;
4391 entry->read_proc = &dispatch_procfs_read;
4392 if (ibm->write)
4393 entry->write_proc = &dispatch_procfs_write;
4394 ibm->flags.proc_created = 1;
4397 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4399 return 0;
4401 err_out:
4402 dbg_printk(TPACPI_DBG_INIT,
4403 "%s: at error exit path with result %d\n",
4404 ibm->name, ret);
4406 ibm_exit(ibm);
4407 return (ret < 0)? ret : 0;
4410 static void ibm_exit(struct ibm_struct *ibm)
4412 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4414 list_del_init(&ibm->all_drivers);
4416 if (ibm->flags.acpi_notify_installed) {
4417 dbg_printk(TPACPI_DBG_EXIT,
4418 "%s: acpi_remove_notify_handler\n", ibm->name);
4419 BUG_ON(!ibm->acpi);
4420 acpi_remove_notify_handler(*ibm->acpi->handle,
4421 ibm->acpi->type,
4422 dispatch_acpi_notify);
4423 ibm->flags.acpi_notify_installed = 0;
4424 ibm->flags.acpi_notify_installed = 0;
4427 if (ibm->flags.proc_created) {
4428 dbg_printk(TPACPI_DBG_EXIT,
4429 "%s: remove_proc_entry\n", ibm->name);
4430 remove_proc_entry(ibm->name, proc_dir);
4431 ibm->flags.proc_created = 0;
4434 if (ibm->flags.acpi_driver_registered) {
4435 dbg_printk(TPACPI_DBG_EXIT,
4436 "%s: acpi_bus_unregister_driver\n", ibm->name);
4437 BUG_ON(!ibm->acpi);
4438 acpi_bus_unregister_driver(ibm->acpi->driver);
4439 kfree(ibm->acpi->driver);
4440 ibm->acpi->driver = NULL;
4441 ibm->flags.acpi_driver_registered = 0;
4444 if (ibm->flags.init_called && ibm->exit) {
4445 ibm->exit();
4446 ibm->flags.init_called = 0;
4449 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4452 /* Probing */
4454 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
4456 struct dmi_device *dev = NULL;
4457 char ec_fw_string[18];
4459 if (!tp)
4460 return;
4462 memset(tp, 0, sizeof(*tp));
4464 if (dmi_name_in_vendors("IBM"))
4465 tp->vendor = PCI_VENDOR_ID_IBM;
4466 else if (dmi_name_in_vendors("LENOVO"))
4467 tp->vendor = PCI_VENDOR_ID_LENOVO;
4468 else
4469 return;
4471 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4472 GFP_KERNEL);
4473 if (!tp->bios_version_str)
4474 return;
4475 tp->bios_model = tp->bios_version_str[0]
4476 | (tp->bios_version_str[1] << 8);
4479 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4480 * X32 or newer, all Z series; Some models must have an
4481 * up-to-date BIOS or they will not be detected.
4483 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4485 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4486 if (sscanf(dev->name,
4487 "IBM ThinkPad Embedded Controller -[%17c",
4488 ec_fw_string) == 1) {
4489 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4490 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4492 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4493 tp->ec_model = ec_fw_string[0]
4494 | (ec_fw_string[1] << 8);
4495 break;
4499 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4500 GFP_KERNEL);
4501 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4502 kfree(tp->model_str);
4503 tp->model_str = NULL;
4507 static int __init probe_for_thinkpad(void)
4509 int is_thinkpad;
4511 if (acpi_disabled)
4512 return -ENODEV;
4515 * Non-ancient models have better DMI tagging, but very old models
4516 * don't.
4518 is_thinkpad = (thinkpad_id.model_str != NULL);
4520 /* ec is required because many other handles are relative to it */
4521 IBM_ACPIHANDLE_INIT(ec);
4522 if (!ec_handle) {
4523 if (is_thinkpad)
4524 printk(IBM_ERR
4525 "Not yet supported ThinkPad detected!\n");
4526 return -ENODEV;
4530 * Risks a regression on very old machines, but reduces potential
4531 * false positives a damn great deal
4533 if (!is_thinkpad)
4534 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
4536 if (!is_thinkpad && !force_load)
4537 return -ENODEV;
4539 return 0;
4543 /* Module init, exit, parameters */
4545 static struct ibm_init_struct ibms_init[] __initdata = {
4547 .init = thinkpad_acpi_driver_init,
4548 .data = &thinkpad_acpi_driver_data,
4551 .init = hotkey_init,
4552 .data = &hotkey_driver_data,
4555 .init = bluetooth_init,
4556 .data = &bluetooth_driver_data,
4559 .init = wan_init,
4560 .data = &wan_driver_data,
4563 .init = video_init,
4564 .data = &video_driver_data,
4567 .init = light_init,
4568 .data = &light_driver_data,
4570 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4572 .init = dock_init,
4573 .data = &dock_driver_data[0],
4576 .init = dock_init2,
4577 .data = &dock_driver_data[1],
4579 #endif
4580 #ifdef CONFIG_THINKPAD_ACPI_BAY
4582 .init = bay_init,
4583 .data = &bay_driver_data,
4585 #endif
4587 .init = cmos_init,
4588 .data = &cmos_driver_data,
4591 .init = led_init,
4592 .data = &led_driver_data,
4595 .init = beep_init,
4596 .data = &beep_driver_data,
4599 .init = thermal_init,
4600 .data = &thermal_driver_data,
4603 .data = &ecdump_driver_data,
4606 .init = brightness_init,
4607 .data = &brightness_driver_data,
4610 .data = &volume_driver_data,
4613 .init = fan_init,
4614 .data = &fan_driver_data,
4618 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4620 unsigned int i;
4621 struct ibm_struct *ibm;
4623 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4624 ibm = ibms_init[i].data;
4625 BUG_ON(ibm == NULL);
4627 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4628 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4629 return -ENOSPC;
4630 strcpy(ibms_init[i].param, val);
4631 strcat(ibms_init[i].param, ",");
4632 return 0;
4636 return -EINVAL;
4639 static int experimental;
4640 module_param(experimental, int, 0);
4642 static u32 dbg_level;
4643 module_param_named(debug, dbg_level, uint, 0);
4645 static int force_load;
4646 module_param(force_load, bool, 0);
4648 static int fan_control_allowed;
4649 module_param_named(fan_control, fan_control_allowed, bool, 0);
4651 static int brightness_mode;
4652 module_param_named(brightness_mode, brightness_mode, int, 0);
4654 static unsigned int hotkey_report_mode;
4655 module_param(hotkey_report_mode, uint, 0);
4657 #define IBM_PARAM(feature) \
4658 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4660 IBM_PARAM(hotkey);
4661 IBM_PARAM(bluetooth);
4662 IBM_PARAM(video);
4663 IBM_PARAM(light);
4664 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4665 IBM_PARAM(dock);
4666 #endif
4667 #ifdef CONFIG_THINKPAD_ACPI_BAY
4668 IBM_PARAM(bay);
4669 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4670 IBM_PARAM(cmos);
4671 IBM_PARAM(led);
4672 IBM_PARAM(beep);
4673 IBM_PARAM(ecdump);
4674 IBM_PARAM(brightness);
4675 IBM_PARAM(volume);
4676 IBM_PARAM(fan);
4678 static int __init thinkpad_acpi_module_init(void)
4680 int ret, i;
4682 /* Parameter checking */
4683 if (hotkey_report_mode > 2)
4684 return -EINVAL;
4686 /* Driver-level probe */
4688 get_thinkpad_model_data(&thinkpad_id);
4689 ret = probe_for_thinkpad();
4690 if (ret) {
4691 thinkpad_acpi_module_exit();
4692 return ret;
4695 /* Driver initialization */
4697 IBM_ACPIHANDLE_INIT(ecrd);
4698 IBM_ACPIHANDLE_INIT(ecwr);
4700 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4701 if (!proc_dir) {
4702 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4703 thinkpad_acpi_module_exit();
4704 return -ENODEV;
4706 proc_dir->owner = THIS_MODULE;
4708 ret = platform_driver_register(&tpacpi_pdriver);
4709 if (ret) {
4710 printk(IBM_ERR "unable to register platform driver\n");
4711 thinkpad_acpi_module_exit();
4712 return ret;
4714 tp_features.platform_drv_registered = 1;
4716 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4717 if (ret) {
4718 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4719 thinkpad_acpi_module_exit();
4720 return ret;
4722 tp_features.platform_drv_attrs_registered = 1;
4725 /* Device initialization */
4726 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4727 NULL, 0);
4728 if (IS_ERR(tpacpi_pdev)) {
4729 ret = PTR_ERR(tpacpi_pdev);
4730 tpacpi_pdev = NULL;
4731 printk(IBM_ERR "unable to register platform device\n");
4732 thinkpad_acpi_module_exit();
4733 return ret;
4735 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4736 if (IS_ERR(tpacpi_hwmon)) {
4737 ret = PTR_ERR(tpacpi_hwmon);
4738 tpacpi_hwmon = NULL;
4739 printk(IBM_ERR "unable to register hwmon device\n");
4740 thinkpad_acpi_module_exit();
4741 return ret;
4743 tpacpi_inputdev = input_allocate_device();
4744 if (!tpacpi_inputdev) {
4745 printk(IBM_ERR "unable to allocate input device\n");
4746 thinkpad_acpi_module_exit();
4747 return -ENOMEM;
4748 } else {
4749 /* Prepare input device, but don't register */
4750 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4751 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4752 tpacpi_inputdev->id.bustype = BUS_HOST;
4753 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
4754 thinkpad_id.vendor :
4755 PCI_VENDOR_ID_IBM;
4756 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4757 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4759 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4760 ret = ibm_init(&ibms_init[i]);
4761 if (ret >= 0 && *ibms_init[i].param)
4762 ret = ibms_init[i].data->write(ibms_init[i].param);
4763 if (ret < 0) {
4764 thinkpad_acpi_module_exit();
4765 return ret;
4768 ret = input_register_device(tpacpi_inputdev);
4769 if (ret < 0) {
4770 printk(IBM_ERR "unable to register input device\n");
4771 thinkpad_acpi_module_exit();
4772 return ret;
4773 } else {
4774 tp_features.input_device_registered = 1;
4777 return 0;
4780 static void thinkpad_acpi_module_exit(void)
4782 struct ibm_struct *ibm, *itmp;
4784 list_for_each_entry_safe_reverse(ibm, itmp,
4785 &tpacpi_all_drivers,
4786 all_drivers) {
4787 ibm_exit(ibm);
4790 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4792 if (tpacpi_inputdev) {
4793 if (tp_features.input_device_registered)
4794 input_unregister_device(tpacpi_inputdev);
4795 else
4796 input_free_device(tpacpi_inputdev);
4799 if (tpacpi_hwmon)
4800 hwmon_device_unregister(tpacpi_hwmon);
4802 if (tpacpi_pdev)
4803 platform_device_unregister(tpacpi_pdev);
4805 if (tp_features.platform_drv_attrs_registered)
4806 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4808 if (tp_features.platform_drv_registered)
4809 platform_driver_unregister(&tpacpi_pdriver);
4811 if (proc_dir)
4812 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4814 kfree(thinkpad_id.bios_version_str);
4815 kfree(thinkpad_id.ec_version_str);
4816 kfree(thinkpad_id.model_str);
4819 module_init(thinkpad_acpi_module_init);
4820 module_exit(thinkpad_acpi_module_exit);