ACPI: thinkpad-acpi: hotkey_report_mode read-write support
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob249cdcbf18dfec85c445e391ddab8e86c986a000
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.15"
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_ops ibm_backlight_data = {
3046 .get_brightness = brightness_get,
3047 .update_status = brightness_update_status,
3050 static struct mutex brightness_mutex;
3052 static int __init brightness_init(struct ibm_init_struct *iibm)
3054 int b;
3056 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3058 mutex_init(&brightness_mutex);
3060 if (!brightness_mode) {
3061 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3062 brightness_mode = 2;
3063 else
3064 brightness_mode = 3;
3066 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3067 brightness_mode);
3070 if (brightness_mode > 3)
3071 return -EINVAL;
3073 b = brightness_get(NULL);
3074 if (b < 0)
3075 return 1;
3077 ibm_backlight_device = backlight_device_register(
3078 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3079 &ibm_backlight_data);
3080 if (IS_ERR(ibm_backlight_device)) {
3081 printk(IBM_ERR "Could not register backlight device\n");
3082 return PTR_ERR(ibm_backlight_device);
3084 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
3086 ibm_backlight_device->props.max_brightness = 7;
3087 ibm_backlight_device->props.brightness = b;
3088 backlight_update_status(ibm_backlight_device);
3090 return 0;
3093 static void brightness_exit(void)
3095 if (ibm_backlight_device) {
3096 vdbg_printk(TPACPI_DBG_EXIT,
3097 "calling backlight_device_unregister()\n");
3098 backlight_device_unregister(ibm_backlight_device);
3099 ibm_backlight_device = NULL;
3103 static int brightness_update_status(struct backlight_device *bd)
3105 return brightness_set(
3106 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3107 bd->props.power == FB_BLANK_UNBLANK) ?
3108 bd->props.brightness : 0);
3112 * ThinkPads can read brightness from two places: EC 0x31, or
3113 * CMOS NVRAM byte 0x5E, bits 0-3.
3115 static int brightness_get(struct backlight_device *bd)
3117 u8 lec = 0, lcmos = 0, level = 0;
3119 if (brightness_mode & 1) {
3120 if (!acpi_ec_read(brightness_offset, &lec))
3121 return -EIO;
3122 lec &= 7;
3123 level = lec;
3125 if (brightness_mode & 2) {
3126 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3127 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3128 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3129 level = lcmos;
3132 if (brightness_mode == 3 && lec != lcmos) {
3133 printk(IBM_ERR
3134 "CMOS NVRAM (%u) and EC (%u) do not agree "
3135 "on display brightness level\n",
3136 (unsigned int) lcmos,
3137 (unsigned int) lec);
3138 return -EIO;
3141 return level;
3144 static int brightness_set(int value)
3146 int cmos_cmd, inc, i, res;
3147 int current_value;
3149 if (value > 7)
3150 return -EINVAL;
3152 res = mutex_lock_interruptible(&brightness_mutex);
3153 if (res < 0)
3154 return res;
3156 current_value = brightness_get(NULL);
3157 if (current_value < 0) {
3158 res = current_value;
3159 goto errout;
3162 cmos_cmd = value > current_value ?
3163 TP_CMOS_BRIGHTNESS_UP :
3164 TP_CMOS_BRIGHTNESS_DOWN;
3165 inc = value > current_value ? 1 : -1;
3167 res = 0;
3168 for (i = current_value; i != value; i += inc) {
3169 if ((brightness_mode & 2) &&
3170 issue_thinkpad_cmos_command(cmos_cmd)) {
3171 res = -EIO;
3172 goto errout;
3174 if ((brightness_mode & 1) &&
3175 !acpi_ec_write(brightness_offset, i + inc)) {
3176 res = -EIO;
3177 goto errout;;
3181 errout:
3182 mutex_unlock(&brightness_mutex);
3183 return res;
3186 static int brightness_read(char *p)
3188 int len = 0;
3189 int level;
3191 if ((level = brightness_get(NULL)) < 0) {
3192 len += sprintf(p + len, "level:\t\tunreadable\n");
3193 } else {
3194 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3195 len += sprintf(p + len, "commands:\tup, down\n");
3196 len += sprintf(p + len, "commands:\tlevel <level>"
3197 " (<level> is 0-7)\n");
3200 return len;
3203 static int brightness_write(char *buf)
3205 int level;
3206 int new_level;
3207 char *cmd;
3209 while ((cmd = next_cmd(&buf))) {
3210 if ((level = brightness_get(NULL)) < 0)
3211 return level;
3212 level &= 7;
3214 if (strlencmp(cmd, "up") == 0) {
3215 new_level = level == 7 ? 7 : level + 1;
3216 } else if (strlencmp(cmd, "down") == 0) {
3217 new_level = level == 0 ? 0 : level - 1;
3218 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3219 new_level >= 0 && new_level <= 7) {
3220 /* new_level set */
3221 } else
3222 return -EINVAL;
3224 brightness_set(new_level);
3227 return 0;
3230 static struct ibm_struct brightness_driver_data = {
3231 .name = "brightness",
3232 .read = brightness_read,
3233 .write = brightness_write,
3234 .exit = brightness_exit,
3237 /*************************************************************************
3238 * Volume subdriver
3241 static int volume_read(char *p)
3243 int len = 0;
3244 u8 level;
3246 if (!acpi_ec_read(volume_offset, &level)) {
3247 len += sprintf(p + len, "level:\t\tunreadable\n");
3248 } else {
3249 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3250 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3251 len += sprintf(p + len, "commands:\tup, down, mute\n");
3252 len += sprintf(p + len, "commands:\tlevel <level>"
3253 " (<level> is 0-15)\n");
3256 return len;
3259 static int volume_write(char *buf)
3261 int cmos_cmd, inc, i;
3262 u8 level, mute;
3263 int new_level, new_mute;
3264 char *cmd;
3266 while ((cmd = next_cmd(&buf))) {
3267 if (!acpi_ec_read(volume_offset, &level))
3268 return -EIO;
3269 new_mute = mute = level & 0x40;
3270 new_level = level = level & 0xf;
3272 if (strlencmp(cmd, "up") == 0) {
3273 if (mute)
3274 new_mute = 0;
3275 else
3276 new_level = level == 15 ? 15 : level + 1;
3277 } else if (strlencmp(cmd, "down") == 0) {
3278 if (mute)
3279 new_mute = 0;
3280 else
3281 new_level = level == 0 ? 0 : level - 1;
3282 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3283 new_level >= 0 && new_level <= 15) {
3284 /* new_level set */
3285 } else if (strlencmp(cmd, "mute") == 0) {
3286 new_mute = 0x40;
3287 } else
3288 return -EINVAL;
3290 if (new_level != level) { /* mute doesn't change */
3291 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3292 inc = new_level > level ? 1 : -1;
3294 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3295 !acpi_ec_write(volume_offset, level)))
3296 return -EIO;
3298 for (i = level; i != new_level; i += inc)
3299 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3300 !acpi_ec_write(volume_offset, i + inc))
3301 return -EIO;
3303 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3304 !acpi_ec_write(volume_offset,
3305 new_level + mute)))
3306 return -EIO;
3309 if (new_mute != mute) { /* level doesn't change */
3310 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3312 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3313 !acpi_ec_write(volume_offset, level + new_mute))
3314 return -EIO;
3318 return 0;
3321 static struct ibm_struct volume_driver_data = {
3322 .name = "volume",
3323 .read = volume_read,
3324 .write = volume_write,
3327 /*************************************************************************
3328 * Fan subdriver
3332 * FAN ACCESS MODES
3334 * TPACPI_FAN_RD_ACPI_GFAN:
3335 * ACPI GFAN method: returns fan level
3337 * see TPACPI_FAN_WR_ACPI_SFAN
3338 * EC 0x2f (HFSP) not available if GFAN exists
3340 * TPACPI_FAN_WR_ACPI_SFAN:
3341 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3343 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3344 * it for writing.
3346 * TPACPI_FAN_WR_TPEC:
3347 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3348 * Supported on almost all ThinkPads
3350 * Fan speed changes of any sort (including those caused by the
3351 * disengaged mode) are usually done slowly by the firmware as the
3352 * maximum ammount of fan duty cycle change per second seems to be
3353 * limited.
3355 * Reading is not available if GFAN exists.
3356 * Writing is not available if SFAN exists.
3358 * Bits
3359 * 7 automatic mode engaged;
3360 * (default operation mode of the ThinkPad)
3361 * fan level is ignored in this mode.
3362 * 6 full speed mode (takes precedence over bit 7);
3363 * not available on all thinkpads. May disable
3364 * the tachometer while the fan controller ramps up
3365 * the speed (which can take up to a few *minutes*).
3366 * Speeds up fan to 100% duty-cycle, which is far above
3367 * the standard RPM levels. It is not impossible that
3368 * it could cause hardware damage.
3369 * 5-3 unused in some models. Extra bits for fan level
3370 * in others, but still useless as all values above
3371 * 7 map to the same speed as level 7 in these models.
3372 * 2-0 fan level (0..7 usually)
3373 * 0x00 = stop
3374 * 0x07 = max (set when temperatures critical)
3375 * Some ThinkPads may have other levels, see
3376 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3378 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3379 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3380 * does so, its initial value is meaningless (0x07).
3382 * For firmware bugs, refer to:
3383 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3385 * ----
3387 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3388 * Main fan tachometer reading (in RPM)
3390 * This register is present on all ThinkPads with a new-style EC, and
3391 * it is known not to be present on the A21m/e, and T22, as there is
3392 * something else in offset 0x84 according to the ACPI DSDT. Other
3393 * ThinkPads from this same time period (and earlier) probably lack the
3394 * tachometer as well.
3396 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3397 * was never fixed by IBM to report the EC firmware version string
3398 * probably support the tachometer (like the early X models), so
3399 * detecting it is quite hard. We need more data to know for sure.
3401 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3402 * might result.
3404 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3405 * mode.
3407 * For firmware bugs, refer to:
3408 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3410 * TPACPI_FAN_WR_ACPI_FANS:
3411 * ThinkPad X31, X40, X41. Not available in the X60.
3413 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3414 * high speed. ACPI DSDT seems to map these three speeds to levels
3415 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3416 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3418 * The speeds are stored on handles
3419 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3421 * There are three default speed sets, acessible as handles:
3422 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3424 * ACPI DSDT switches which set is in use depending on various
3425 * factors.
3427 * TPACPI_FAN_WR_TPEC is also available and should be used to
3428 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3429 * but the ACPI tables just mention level 7.
3432 static enum fan_status_access_mode fan_status_access_mode;
3433 static enum fan_control_access_mode fan_control_access_mode;
3434 static enum fan_control_commands fan_control_commands;
3436 static u8 fan_control_initial_status;
3437 static u8 fan_control_desired_level;
3439 static void fan_watchdog_fire(struct work_struct *ignored);
3440 static int fan_watchdog_maxinterval;
3441 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3443 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3444 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3445 "\\FSPD", /* 600e/x, 770e, 770x */
3446 ); /* all others */
3447 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3448 "JFNS", /* 770x-JL */
3449 ); /* all others */
3452 * SYSFS fan layout: hwmon compatible (device)
3454 * pwm*_enable:
3455 * 0: "disengaged" mode
3456 * 1: manual mode
3457 * 2: native EC "auto" mode (recommended, hardware default)
3459 * pwm*: set speed in manual mode, ignored otherwise.
3460 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3461 * interpolation.
3463 * fan*_input: tachometer reading, RPM
3466 * SYSFS fan layout: extensions
3468 * fan_watchdog (driver):
3469 * fan watchdog interval in seconds, 0 disables (default), max 120
3472 /* sysfs fan pwm1_enable ----------------------------------------------- */
3473 static ssize_t fan_pwm1_enable_show(struct device *dev,
3474 struct device_attribute *attr,
3475 char *buf)
3477 int res, mode;
3478 u8 status;
3480 res = fan_get_status_safe(&status);
3481 if (res)
3482 return res;
3484 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3485 if (status != fan_control_initial_status) {
3486 tp_features.fan_ctrl_status_undef = 0;
3487 } else {
3488 /* Return most likely status. In fact, it
3489 * might be the only possible status */
3490 status = TP_EC_FAN_AUTO;
3494 if (status & TP_EC_FAN_FULLSPEED) {
3495 mode = 0;
3496 } else if (status & TP_EC_FAN_AUTO) {
3497 mode = 2;
3498 } else
3499 mode = 1;
3501 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3504 static ssize_t fan_pwm1_enable_store(struct device *dev,
3505 struct device_attribute *attr,
3506 const char *buf, size_t count)
3508 unsigned long t;
3509 int res, level;
3511 if (parse_strtoul(buf, 2, &t))
3512 return -EINVAL;
3514 switch (t) {
3515 case 0:
3516 level = TP_EC_FAN_FULLSPEED;
3517 break;
3518 case 1:
3519 level = TPACPI_FAN_LAST_LEVEL;
3520 break;
3521 case 2:
3522 level = TP_EC_FAN_AUTO;
3523 break;
3524 case 3:
3525 /* reserved for software-controlled auto mode */
3526 return -ENOSYS;
3527 default:
3528 return -EINVAL;
3531 res = fan_set_level_safe(level);
3532 if (res == -ENXIO)
3533 return -EINVAL;
3534 else if (res < 0)
3535 return res;
3537 fan_watchdog_reset();
3539 return count;
3542 static struct device_attribute dev_attr_fan_pwm1_enable =
3543 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3544 fan_pwm1_enable_show, fan_pwm1_enable_store);
3546 /* sysfs fan pwm1 ------------------------------------------------------ */
3547 static ssize_t fan_pwm1_show(struct device *dev,
3548 struct device_attribute *attr,
3549 char *buf)
3551 int res;
3552 u8 status;
3554 res = fan_get_status_safe(&status);
3555 if (res)
3556 return res;
3558 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3559 if (status != fan_control_initial_status) {
3560 tp_features.fan_ctrl_status_undef = 0;
3561 } else {
3562 status = TP_EC_FAN_AUTO;
3566 if ((status &
3567 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3568 status = fan_control_desired_level;
3570 if (status > 7)
3571 status = 7;
3573 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3576 static ssize_t fan_pwm1_store(struct device *dev,
3577 struct device_attribute *attr,
3578 const char *buf, size_t count)
3580 unsigned long s;
3581 int rc;
3582 u8 status, newlevel;
3584 if (parse_strtoul(buf, 255, &s))
3585 return -EINVAL;
3587 /* scale down from 0-255 to 0-7 */
3588 newlevel = (s >> 5) & 0x07;
3590 rc = mutex_lock_interruptible(&fan_mutex);
3591 if (rc < 0)
3592 return rc;
3594 rc = fan_get_status(&status);
3595 if (!rc && (status &
3596 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3597 rc = fan_set_level(newlevel);
3598 if (rc == -ENXIO)
3599 rc = -EINVAL;
3600 else if (!rc) {
3601 fan_update_desired_level(newlevel);
3602 fan_watchdog_reset();
3606 mutex_unlock(&fan_mutex);
3607 return (rc)? rc : count;
3610 static struct device_attribute dev_attr_fan_pwm1 =
3611 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3612 fan_pwm1_show, fan_pwm1_store);
3614 /* sysfs fan fan1_input ------------------------------------------------ */
3615 static ssize_t fan_fan1_input_show(struct device *dev,
3616 struct device_attribute *attr,
3617 char *buf)
3619 int res;
3620 unsigned int speed;
3622 res = fan_get_speed(&speed);
3623 if (res < 0)
3624 return res;
3626 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3629 static struct device_attribute dev_attr_fan_fan1_input =
3630 __ATTR(fan1_input, S_IRUGO,
3631 fan_fan1_input_show, NULL);
3633 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3634 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3635 char *buf)
3637 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3640 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3641 const char *buf, size_t count)
3643 unsigned long t;
3645 if (parse_strtoul(buf, 120, &t))
3646 return -EINVAL;
3648 if (!fan_control_allowed)
3649 return -EPERM;
3651 fan_watchdog_maxinterval = t;
3652 fan_watchdog_reset();
3654 return count;
3657 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3658 fan_fan_watchdog_show, fan_fan_watchdog_store);
3660 /* --------------------------------------------------------------------- */
3661 static struct attribute *fan_attributes[] = {
3662 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3663 &dev_attr_fan_fan1_input.attr,
3664 NULL
3667 static const struct attribute_group fan_attr_group = {
3668 .attrs = fan_attributes,
3671 static int __init fan_init(struct ibm_init_struct *iibm)
3673 int rc;
3675 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3677 mutex_init(&fan_mutex);
3678 fan_status_access_mode = TPACPI_FAN_NONE;
3679 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3680 fan_control_commands = 0;
3681 fan_watchdog_maxinterval = 0;
3682 tp_features.fan_ctrl_status_undef = 0;
3683 fan_control_desired_level = 7;
3685 IBM_ACPIHANDLE_INIT(fans);
3686 IBM_ACPIHANDLE_INIT(gfan);
3687 IBM_ACPIHANDLE_INIT(sfan);
3689 if (gfan_handle) {
3690 /* 570, 600e/x, 770e, 770x */
3691 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3692 } else {
3693 /* all other ThinkPads: note that even old-style
3694 * ThinkPad ECs supports the fan control register */
3695 if (likely(acpi_ec_read(fan_status_offset,
3696 &fan_control_initial_status))) {
3697 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3699 /* In some ThinkPads, neither the EC nor the ACPI
3700 * DSDT initialize the fan status, and it ends up
3701 * being set to 0x07 when it *could* be either
3702 * 0x07 or 0x80.
3704 * Enable for TP-1Y (T43), TP-78 (R51e),
3705 * TP-76 (R52), TP-70 (T43, R52), which are known
3706 * to be buggy. */
3707 if (fan_control_initial_status == 0x07) {
3708 switch (thinkpad_id.ec_model) {
3709 case 0x5931: /* TP-1Y */
3710 case 0x3837: /* TP-78 */
3711 case 0x3637: /* TP-76 */
3712 case 0x3037: /* TP-70 */
3713 printk(IBM_NOTICE
3714 "fan_init: initial fan status is "
3715 "unknown, assuming it is in auto "
3716 "mode\n");
3717 tp_features.fan_ctrl_status_undef = 1;
3721 } else {
3722 printk(IBM_ERR
3723 "ThinkPad ACPI EC access misbehaving, "
3724 "fan status and control unavailable\n");
3725 return 1;
3729 if (sfan_handle) {
3730 /* 570, 770x-JL */
3731 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3732 fan_control_commands |=
3733 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3734 } else {
3735 if (!gfan_handle) {
3736 /* gfan without sfan means no fan control */
3737 /* all other models implement TP EC 0x2f control */
3739 if (fans_handle) {
3740 /* X31, X40, X41 */
3741 fan_control_access_mode =
3742 TPACPI_FAN_WR_ACPI_FANS;
3743 fan_control_commands |=
3744 TPACPI_FAN_CMD_SPEED |
3745 TPACPI_FAN_CMD_LEVEL |
3746 TPACPI_FAN_CMD_ENABLE;
3747 } else {
3748 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3749 fan_control_commands |=
3750 TPACPI_FAN_CMD_LEVEL |
3751 TPACPI_FAN_CMD_ENABLE;
3756 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3757 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3758 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3759 fan_status_access_mode, fan_control_access_mode);
3761 /* fan control master switch */
3762 if (!fan_control_allowed) {
3763 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3764 fan_control_commands = 0;
3765 dbg_printk(TPACPI_DBG_INIT,
3766 "fan control features disabled by parameter\n");
3769 /* update fan_control_desired_level */
3770 if (fan_status_access_mode != TPACPI_FAN_NONE)
3771 fan_get_status_safe(NULL);
3773 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3774 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3775 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3776 &fan_attr_group);
3777 if (!(rc < 0))
3778 rc = driver_create_file(&tpacpi_pdriver.driver,
3779 &driver_attr_fan_watchdog);
3780 if (rc < 0)
3781 return rc;
3782 return 0;
3783 } else
3784 return 1;
3788 * Call with fan_mutex held
3790 static void fan_update_desired_level(u8 status)
3792 if ((status &
3793 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3794 if (status > 7)
3795 fan_control_desired_level = 7;
3796 else
3797 fan_control_desired_level = status;
3801 static int fan_get_status(u8 *status)
3803 u8 s;
3805 /* TODO:
3806 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3808 switch (fan_status_access_mode) {
3809 case TPACPI_FAN_RD_ACPI_GFAN:
3810 /* 570, 600e/x, 770e, 770x */
3812 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3813 return -EIO;
3815 if (likely(status))
3816 *status = s & 0x07;
3818 break;
3820 case TPACPI_FAN_RD_TPEC:
3821 /* all except 570, 600e/x, 770e, 770x */
3822 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3823 return -EIO;
3825 if (likely(status))
3826 *status = s;
3828 break;
3830 default:
3831 return -ENXIO;
3834 return 0;
3837 static int fan_get_status_safe(u8 *status)
3839 int rc;
3840 u8 s;
3842 rc = mutex_lock_interruptible(&fan_mutex);
3843 if (rc < 0)
3844 return rc;
3845 rc = fan_get_status(&s);
3846 if (!rc)
3847 fan_update_desired_level(s);
3848 mutex_unlock(&fan_mutex);
3850 if (status)
3851 *status = s;
3853 return rc;
3856 static void fan_exit(void)
3858 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3860 /* FIXME: can we really do this unconditionally? */
3861 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3862 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3864 cancel_delayed_work(&fan_watchdog_task);
3865 flush_scheduled_work();
3868 static int fan_get_speed(unsigned int *speed)
3870 u8 hi, lo;
3872 switch (fan_status_access_mode) {
3873 case TPACPI_FAN_RD_TPEC:
3874 /* all except 570, 600e/x, 770e, 770x */
3875 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3876 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3877 return -EIO;
3879 if (likely(speed))
3880 *speed = (hi << 8) | lo;
3882 break;
3884 default:
3885 return -ENXIO;
3888 return 0;
3891 static void fan_watchdog_fire(struct work_struct *ignored)
3893 int rc;
3895 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3896 rc = fan_set_enable();
3897 if (rc < 0) {
3898 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3899 "will try again later...\n", -rc);
3900 /* reschedule for later */
3901 fan_watchdog_reset();
3905 static void fan_watchdog_reset(void)
3907 static int fan_watchdog_active;
3909 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3910 return;
3912 if (fan_watchdog_active)
3913 cancel_delayed_work(&fan_watchdog_task);
3915 if (fan_watchdog_maxinterval > 0) {
3916 fan_watchdog_active = 1;
3917 if (!schedule_delayed_work(&fan_watchdog_task,
3918 msecs_to_jiffies(fan_watchdog_maxinterval
3919 * 1000))) {
3920 printk(IBM_ERR "failed to schedule the fan watchdog, "
3921 "watchdog will not trigger\n");
3923 } else
3924 fan_watchdog_active = 0;
3927 static int fan_set_level(int level)
3929 if (!fan_control_allowed)
3930 return -EPERM;
3932 switch (fan_control_access_mode) {
3933 case TPACPI_FAN_WR_ACPI_SFAN:
3934 if (level >= 0 && level <= 7) {
3935 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3936 return -EIO;
3937 } else
3938 return -EINVAL;
3939 break;
3941 case TPACPI_FAN_WR_ACPI_FANS:
3942 case TPACPI_FAN_WR_TPEC:
3943 if ((level != TP_EC_FAN_AUTO) &&
3944 (level != TP_EC_FAN_FULLSPEED) &&
3945 ((level < 0) || (level > 7)))
3946 return -EINVAL;
3948 /* safety net should the EC not support AUTO
3949 * or FULLSPEED mode bits and just ignore them */
3950 if (level & TP_EC_FAN_FULLSPEED)
3951 level |= 7; /* safety min speed 7 */
3952 else if (level & TP_EC_FAN_FULLSPEED)
3953 level |= 4; /* safety min speed 4 */
3955 if (!acpi_ec_write(fan_status_offset, level))
3956 return -EIO;
3957 else
3958 tp_features.fan_ctrl_status_undef = 0;
3959 break;
3961 default:
3962 return -ENXIO;
3964 return 0;
3967 static int fan_set_level_safe(int level)
3969 int rc;
3971 if (!fan_control_allowed)
3972 return -EPERM;
3974 rc = mutex_lock_interruptible(&fan_mutex);
3975 if (rc < 0)
3976 return rc;
3978 if (level == TPACPI_FAN_LAST_LEVEL)
3979 level = fan_control_desired_level;
3981 rc = fan_set_level(level);
3982 if (!rc)
3983 fan_update_desired_level(level);
3985 mutex_unlock(&fan_mutex);
3986 return rc;
3989 static int fan_set_enable(void)
3991 u8 s;
3992 int rc;
3994 if (!fan_control_allowed)
3995 return -EPERM;
3997 rc = mutex_lock_interruptible(&fan_mutex);
3998 if (rc < 0)
3999 return rc;
4001 switch (fan_control_access_mode) {
4002 case TPACPI_FAN_WR_ACPI_FANS:
4003 case TPACPI_FAN_WR_TPEC:
4004 rc = fan_get_status(&s);
4005 if (rc < 0)
4006 break;
4008 /* Don't go out of emergency fan mode */
4009 if (s != 7) {
4010 s &= 0x07;
4011 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4014 if (!acpi_ec_write(fan_status_offset, s))
4015 rc = -EIO;
4016 else {
4017 tp_features.fan_ctrl_status_undef = 0;
4018 rc = 0;
4020 break;
4022 case TPACPI_FAN_WR_ACPI_SFAN:
4023 rc = fan_get_status(&s);
4024 if (rc < 0)
4025 break;
4027 s &= 0x07;
4029 /* Set fan to at least level 4 */
4030 s |= 4;
4032 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4033 rc= -EIO;
4034 else
4035 rc = 0;
4036 break;
4038 default:
4039 rc = -ENXIO;
4042 mutex_unlock(&fan_mutex);
4043 return rc;
4046 static int fan_set_disable(void)
4048 int rc;
4050 if (!fan_control_allowed)
4051 return -EPERM;
4053 rc = mutex_lock_interruptible(&fan_mutex);
4054 if (rc < 0)
4055 return rc;
4057 rc = 0;
4058 switch (fan_control_access_mode) {
4059 case TPACPI_FAN_WR_ACPI_FANS:
4060 case TPACPI_FAN_WR_TPEC:
4061 if (!acpi_ec_write(fan_status_offset, 0x00))
4062 rc = -EIO;
4063 else {
4064 fan_control_desired_level = 0;
4065 tp_features.fan_ctrl_status_undef = 0;
4067 break;
4069 case TPACPI_FAN_WR_ACPI_SFAN:
4070 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4071 rc = -EIO;
4072 else
4073 fan_control_desired_level = 0;
4074 break;
4076 default:
4077 rc = -ENXIO;
4081 mutex_unlock(&fan_mutex);
4082 return rc;
4085 static int fan_set_speed(int speed)
4087 int rc;
4089 if (!fan_control_allowed)
4090 return -EPERM;
4092 rc = mutex_lock_interruptible(&fan_mutex);
4093 if (rc < 0)
4094 return rc;
4096 rc = 0;
4097 switch (fan_control_access_mode) {
4098 case TPACPI_FAN_WR_ACPI_FANS:
4099 if (speed >= 0 && speed <= 65535) {
4100 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4101 speed, speed, speed))
4102 rc = -EIO;
4103 } else
4104 rc = -EINVAL;
4105 break;
4107 default:
4108 rc = -ENXIO;
4111 mutex_unlock(&fan_mutex);
4112 return rc;
4115 static int fan_read(char *p)
4117 int len = 0;
4118 int rc;
4119 u8 status;
4120 unsigned int speed = 0;
4122 switch (fan_status_access_mode) {
4123 case TPACPI_FAN_RD_ACPI_GFAN:
4124 /* 570, 600e/x, 770e, 770x */
4125 if ((rc = fan_get_status_safe(&status)) < 0)
4126 return rc;
4128 len += sprintf(p + len, "status:\t\t%s\n"
4129 "level:\t\t%d\n",
4130 (status != 0) ? "enabled" : "disabled", status);
4131 break;
4133 case TPACPI_FAN_RD_TPEC:
4134 /* all except 570, 600e/x, 770e, 770x */
4135 if ((rc = fan_get_status_safe(&status)) < 0)
4136 return rc;
4138 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4139 if (status != fan_control_initial_status)
4140 tp_features.fan_ctrl_status_undef = 0;
4141 else
4142 /* Return most likely status. In fact, it
4143 * might be the only possible status */
4144 status = TP_EC_FAN_AUTO;
4147 len += sprintf(p + len, "status:\t\t%s\n",
4148 (status != 0) ? "enabled" : "disabled");
4150 if ((rc = fan_get_speed(&speed)) < 0)
4151 return rc;
4153 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4155 if (status & TP_EC_FAN_FULLSPEED)
4156 /* Disengaged mode takes precedence */
4157 len += sprintf(p + len, "level:\t\tdisengaged\n");
4158 else if (status & TP_EC_FAN_AUTO)
4159 len += sprintf(p + len, "level:\t\tauto\n");
4160 else
4161 len += sprintf(p + len, "level:\t\t%d\n", status);
4162 break;
4164 case TPACPI_FAN_NONE:
4165 default:
4166 len += sprintf(p + len, "status:\t\tnot supported\n");
4169 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
4170 len += sprintf(p + len, "commands:\tlevel <level>");
4172 switch (fan_control_access_mode) {
4173 case TPACPI_FAN_WR_ACPI_SFAN:
4174 len += sprintf(p + len, " (<level> is 0-7)\n");
4175 break;
4177 default:
4178 len += sprintf(p + len, " (<level> is 0-7, "
4179 "auto, disengaged, full-speed)\n");
4180 break;
4184 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
4185 len += sprintf(p + len, "commands:\tenable, disable\n"
4186 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4187 "1-120 (seconds))\n");
4189 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
4190 len += sprintf(p + len, "commands:\tspeed <speed>"
4191 " (<speed> is 0-65535)\n");
4193 return len;
4196 static int fan_write_cmd_level(const char *cmd, int *rc)
4198 int level;
4200 if (strlencmp(cmd, "level auto") == 0)
4201 level = TP_EC_FAN_AUTO;
4202 else if ((strlencmp(cmd, "level disengaged") == 0) |
4203 (strlencmp(cmd, "level full-speed") == 0))
4204 level = TP_EC_FAN_FULLSPEED;
4205 else if (sscanf(cmd, "level %d", &level) != 1)
4206 return 0;
4208 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4209 printk(IBM_ERR "level command accepted for unsupported "
4210 "access mode %d", fan_control_access_mode);
4212 return 1;
4215 static int fan_write_cmd_enable(const char *cmd, int *rc)
4217 if (strlencmp(cmd, "enable") != 0)
4218 return 0;
4220 if ((*rc = fan_set_enable()) == -ENXIO)
4221 printk(IBM_ERR "enable command accepted for unsupported "
4222 "access mode %d", fan_control_access_mode);
4224 return 1;
4227 static int fan_write_cmd_disable(const char *cmd, int *rc)
4229 if (strlencmp(cmd, "disable") != 0)
4230 return 0;
4232 if ((*rc = fan_set_disable()) == -ENXIO)
4233 printk(IBM_ERR "disable command accepted for unsupported "
4234 "access mode %d", fan_control_access_mode);
4236 return 1;
4239 static int fan_write_cmd_speed(const char *cmd, int *rc)
4241 int speed;
4243 /* TODO:
4244 * Support speed <low> <medium> <high> ? */
4246 if (sscanf(cmd, "speed %d", &speed) != 1)
4247 return 0;
4249 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4250 printk(IBM_ERR "speed command accepted for unsupported "
4251 "access mode %d", fan_control_access_mode);
4253 return 1;
4256 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4258 int interval;
4260 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4261 return 0;
4263 if (interval < 0 || interval > 120)
4264 *rc = -EINVAL;
4265 else
4266 fan_watchdog_maxinterval = interval;
4268 return 1;
4271 static int fan_write(char *buf)
4273 char *cmd;
4274 int rc = 0;
4276 while (!rc && (cmd = next_cmd(&buf))) {
4277 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4278 fan_write_cmd_level(cmd, &rc)) &&
4279 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4280 (fan_write_cmd_enable(cmd, &rc) ||
4281 fan_write_cmd_disable(cmd, &rc) ||
4282 fan_write_cmd_watchdog(cmd, &rc))) &&
4283 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4284 fan_write_cmd_speed(cmd, &rc))
4286 rc = -EINVAL;
4287 else if (!rc)
4288 fan_watchdog_reset();
4291 return rc;
4294 static struct ibm_struct fan_driver_data = {
4295 .name = "fan",
4296 .read = fan_read,
4297 .write = fan_write,
4298 .exit = fan_exit,
4301 /****************************************************************************
4302 ****************************************************************************
4304 * Infrastructure
4306 ****************************************************************************
4307 ****************************************************************************/
4309 /* /proc support */
4310 static struct proc_dir_entry *proc_dir;
4312 /* Subdriver registry */
4313 static LIST_HEAD(tpacpi_all_drivers);
4317 * Module and infrastructure proble, init and exit handling
4320 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4321 static const char * __init str_supported(int is_supported)
4323 static char text_unsupported[] __initdata = "not supported";
4325 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4327 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4329 static int __init ibm_init(struct ibm_init_struct *iibm)
4331 int ret;
4332 struct ibm_struct *ibm = iibm->data;
4333 struct proc_dir_entry *entry;
4335 BUG_ON(ibm == NULL);
4337 INIT_LIST_HEAD(&ibm->all_drivers);
4339 if (ibm->flags.experimental && !experimental)
4340 return 0;
4342 dbg_printk(TPACPI_DBG_INIT,
4343 "probing for %s\n", ibm->name);
4345 if (iibm->init) {
4346 ret = iibm->init(iibm);
4347 if (ret > 0)
4348 return 0; /* probe failed */
4349 if (ret)
4350 return ret;
4352 ibm->flags.init_called = 1;
4355 if (ibm->acpi) {
4356 if (ibm->acpi->hid) {
4357 ret = register_tpacpi_subdriver(ibm);
4358 if (ret)
4359 goto err_out;
4362 if (ibm->acpi->notify) {
4363 ret = setup_acpi_notify(ibm);
4364 if (ret == -ENODEV) {
4365 printk(IBM_NOTICE "disabling subdriver %s\n",
4366 ibm->name);
4367 ret = 0;
4368 goto err_out;
4370 if (ret < 0)
4371 goto err_out;
4375 dbg_printk(TPACPI_DBG_INIT,
4376 "%s installed\n", ibm->name);
4378 if (ibm->read) {
4379 entry = create_proc_entry(ibm->name,
4380 S_IFREG | S_IRUGO | S_IWUSR,
4381 proc_dir);
4382 if (!entry) {
4383 printk(IBM_ERR "unable to create proc entry %s\n",
4384 ibm->name);
4385 ret = -ENODEV;
4386 goto err_out;
4388 entry->owner = THIS_MODULE;
4389 entry->data = ibm;
4390 entry->read_proc = &dispatch_procfs_read;
4391 if (ibm->write)
4392 entry->write_proc = &dispatch_procfs_write;
4393 ibm->flags.proc_created = 1;
4396 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4398 return 0;
4400 err_out:
4401 dbg_printk(TPACPI_DBG_INIT,
4402 "%s: at error exit path with result %d\n",
4403 ibm->name, ret);
4405 ibm_exit(ibm);
4406 return (ret < 0)? ret : 0;
4409 static void ibm_exit(struct ibm_struct *ibm)
4411 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4413 list_del_init(&ibm->all_drivers);
4415 if (ibm->flags.acpi_notify_installed) {
4416 dbg_printk(TPACPI_DBG_EXIT,
4417 "%s: acpi_remove_notify_handler\n", ibm->name);
4418 BUG_ON(!ibm->acpi);
4419 acpi_remove_notify_handler(*ibm->acpi->handle,
4420 ibm->acpi->type,
4421 dispatch_acpi_notify);
4422 ibm->flags.acpi_notify_installed = 0;
4423 ibm->flags.acpi_notify_installed = 0;
4426 if (ibm->flags.proc_created) {
4427 dbg_printk(TPACPI_DBG_EXIT,
4428 "%s: remove_proc_entry\n", ibm->name);
4429 remove_proc_entry(ibm->name, proc_dir);
4430 ibm->flags.proc_created = 0;
4433 if (ibm->flags.acpi_driver_registered) {
4434 dbg_printk(TPACPI_DBG_EXIT,
4435 "%s: acpi_bus_unregister_driver\n", ibm->name);
4436 BUG_ON(!ibm->acpi);
4437 acpi_bus_unregister_driver(ibm->acpi->driver);
4438 kfree(ibm->acpi->driver);
4439 ibm->acpi->driver = NULL;
4440 ibm->flags.acpi_driver_registered = 0;
4443 if (ibm->flags.init_called && ibm->exit) {
4444 ibm->exit();
4445 ibm->flags.init_called = 0;
4448 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4451 /* Probing */
4453 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
4455 struct dmi_device *dev = NULL;
4456 char ec_fw_string[18];
4458 if (!tp)
4459 return;
4461 memset(tp, 0, sizeof(*tp));
4463 if (dmi_name_in_vendors("IBM"))
4464 tp->vendor = PCI_VENDOR_ID_IBM;
4465 else if (dmi_name_in_vendors("LENOVO"))
4466 tp->vendor = PCI_VENDOR_ID_LENOVO;
4467 else
4468 return;
4470 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4471 GFP_KERNEL);
4472 if (!tp->bios_version_str)
4473 return;
4474 tp->bios_model = tp->bios_version_str[0]
4475 | (tp->bios_version_str[1] << 8);
4478 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4479 * X32 or newer, all Z series; Some models must have an
4480 * up-to-date BIOS or they will not be detected.
4482 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4484 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4485 if (sscanf(dev->name,
4486 "IBM ThinkPad Embedded Controller -[%17c",
4487 ec_fw_string) == 1) {
4488 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4489 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4491 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4492 tp->ec_model = ec_fw_string[0]
4493 | (ec_fw_string[1] << 8);
4494 break;
4498 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4499 GFP_KERNEL);
4500 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4501 kfree(tp->model_str);
4502 tp->model_str = NULL;
4506 static int __init probe_for_thinkpad(void)
4508 int is_thinkpad;
4510 if (acpi_disabled)
4511 return -ENODEV;
4514 * Non-ancient models have better DMI tagging, but very old models
4515 * don't.
4517 is_thinkpad = (thinkpad_id.model_str != NULL);
4519 /* ec is required because many other handles are relative to it */
4520 IBM_ACPIHANDLE_INIT(ec);
4521 if (!ec_handle) {
4522 if (is_thinkpad)
4523 printk(IBM_ERR
4524 "Not yet supported ThinkPad detected!\n");
4525 return -ENODEV;
4529 * Risks a regression on very old machines, but reduces potential
4530 * false positives a damn great deal
4532 if (!is_thinkpad)
4533 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
4535 if (!is_thinkpad && !force_load)
4536 return -ENODEV;
4538 return 0;
4542 /* Module init, exit, parameters */
4544 static struct ibm_init_struct ibms_init[] __initdata = {
4546 .init = thinkpad_acpi_driver_init,
4547 .data = &thinkpad_acpi_driver_data,
4550 .init = hotkey_init,
4551 .data = &hotkey_driver_data,
4554 .init = bluetooth_init,
4555 .data = &bluetooth_driver_data,
4558 .init = wan_init,
4559 .data = &wan_driver_data,
4562 .init = video_init,
4563 .data = &video_driver_data,
4566 .init = light_init,
4567 .data = &light_driver_data,
4569 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4571 .init = dock_init,
4572 .data = &dock_driver_data[0],
4575 .init = dock_init2,
4576 .data = &dock_driver_data[1],
4578 #endif
4579 #ifdef CONFIG_THINKPAD_ACPI_BAY
4581 .init = bay_init,
4582 .data = &bay_driver_data,
4584 #endif
4586 .init = cmos_init,
4587 .data = &cmos_driver_data,
4590 .init = led_init,
4591 .data = &led_driver_data,
4594 .init = beep_init,
4595 .data = &beep_driver_data,
4598 .init = thermal_init,
4599 .data = &thermal_driver_data,
4602 .data = &ecdump_driver_data,
4605 .init = brightness_init,
4606 .data = &brightness_driver_data,
4609 .data = &volume_driver_data,
4612 .init = fan_init,
4613 .data = &fan_driver_data,
4617 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4619 unsigned int i;
4620 struct ibm_struct *ibm;
4622 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4623 ibm = ibms_init[i].data;
4624 BUG_ON(ibm == NULL);
4626 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4627 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4628 return -ENOSPC;
4629 strcpy(ibms_init[i].param, val);
4630 strcat(ibms_init[i].param, ",");
4631 return 0;
4635 return -EINVAL;
4638 static int experimental;
4639 module_param(experimental, int, 0);
4641 static u32 dbg_level;
4642 module_param_named(debug, dbg_level, uint, 0);
4644 static int force_load;
4645 module_param(force_load, bool, 0);
4647 static int fan_control_allowed;
4648 module_param_named(fan_control, fan_control_allowed, bool, 0);
4650 static int brightness_mode;
4651 module_param_named(brightness_mode, brightness_mode, int, 0);
4653 static unsigned int hotkey_report_mode;
4654 module_param(hotkey_report_mode, uint, 0);
4656 #define IBM_PARAM(feature) \
4657 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4659 IBM_PARAM(hotkey);
4660 IBM_PARAM(bluetooth);
4661 IBM_PARAM(video);
4662 IBM_PARAM(light);
4663 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4664 IBM_PARAM(dock);
4665 #endif
4666 #ifdef CONFIG_THINKPAD_ACPI_BAY
4667 IBM_PARAM(bay);
4668 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4669 IBM_PARAM(cmos);
4670 IBM_PARAM(led);
4671 IBM_PARAM(beep);
4672 IBM_PARAM(ecdump);
4673 IBM_PARAM(brightness);
4674 IBM_PARAM(volume);
4675 IBM_PARAM(fan);
4677 static int __init thinkpad_acpi_module_init(void)
4679 int ret, i;
4681 /* Parameter checking */
4682 if (hotkey_report_mode > 2)
4683 return -EINVAL;
4685 /* Driver-level probe */
4687 get_thinkpad_model_data(&thinkpad_id);
4688 ret = probe_for_thinkpad();
4689 if (ret) {
4690 thinkpad_acpi_module_exit();
4691 return ret;
4694 /* Driver initialization */
4696 IBM_ACPIHANDLE_INIT(ecrd);
4697 IBM_ACPIHANDLE_INIT(ecwr);
4699 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4700 if (!proc_dir) {
4701 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4702 thinkpad_acpi_module_exit();
4703 return -ENODEV;
4705 proc_dir->owner = THIS_MODULE;
4707 ret = platform_driver_register(&tpacpi_pdriver);
4708 if (ret) {
4709 printk(IBM_ERR "unable to register platform driver\n");
4710 thinkpad_acpi_module_exit();
4711 return ret;
4713 tp_features.platform_drv_registered = 1;
4715 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4716 if (ret) {
4717 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4718 thinkpad_acpi_module_exit();
4719 return ret;
4721 tp_features.platform_drv_attrs_registered = 1;
4724 /* Device initialization */
4725 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4726 NULL, 0);
4727 if (IS_ERR(tpacpi_pdev)) {
4728 ret = PTR_ERR(tpacpi_pdev);
4729 tpacpi_pdev = NULL;
4730 printk(IBM_ERR "unable to register platform device\n");
4731 thinkpad_acpi_module_exit();
4732 return ret;
4734 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4735 if (IS_ERR(tpacpi_hwmon)) {
4736 ret = PTR_ERR(tpacpi_hwmon);
4737 tpacpi_hwmon = NULL;
4738 printk(IBM_ERR "unable to register hwmon device\n");
4739 thinkpad_acpi_module_exit();
4740 return ret;
4742 tpacpi_inputdev = input_allocate_device();
4743 if (!tpacpi_inputdev) {
4744 printk(IBM_ERR "unable to allocate input device\n");
4745 thinkpad_acpi_module_exit();
4746 return -ENOMEM;
4747 } else {
4748 /* Prepare input device, but don't register */
4749 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4750 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4751 tpacpi_inputdev->id.bustype = BUS_HOST;
4752 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
4753 thinkpad_id.vendor :
4754 PCI_VENDOR_ID_IBM;
4755 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4756 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4758 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4759 ret = ibm_init(&ibms_init[i]);
4760 if (ret >= 0 && *ibms_init[i].param)
4761 ret = ibms_init[i].data->write(ibms_init[i].param);
4762 if (ret < 0) {
4763 thinkpad_acpi_module_exit();
4764 return ret;
4767 ret = input_register_device(tpacpi_inputdev);
4768 if (ret < 0) {
4769 printk(IBM_ERR "unable to register input device\n");
4770 thinkpad_acpi_module_exit();
4771 return ret;
4772 } else {
4773 tp_features.input_device_registered = 1;
4776 return 0;
4779 static void thinkpad_acpi_module_exit(void)
4781 struct ibm_struct *ibm, *itmp;
4783 list_for_each_entry_safe_reverse(ibm, itmp,
4784 &tpacpi_all_drivers,
4785 all_drivers) {
4786 ibm_exit(ibm);
4789 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4791 if (tpacpi_inputdev) {
4792 if (tp_features.input_device_registered)
4793 input_unregister_device(tpacpi_inputdev);
4794 else
4795 input_free_device(tpacpi_inputdev);
4798 if (tpacpi_hwmon)
4799 hwmon_device_unregister(tpacpi_hwmon);
4801 if (tpacpi_pdev)
4802 platform_device_unregister(tpacpi_pdev);
4804 if (tp_features.platform_drv_attrs_registered)
4805 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4807 if (tp_features.platform_drv_registered)
4808 platform_driver_unregister(&tpacpi_pdriver);
4810 if (proc_dir)
4811 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4813 kfree(thinkpad_id.bios_version_str);
4814 kfree(thinkpad_id.ec_version_str);
4815 kfree(thinkpad_id.model_str);
4818 module_init(thinkpad_acpi_module_init);
4819 module_exit(thinkpad_acpi_module_exit);