ACPI: thinkpad-acpi: document keymap gotcha's (v2)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob9a7032466234f7bdf8d50382c18c2535ebd6ccfe
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.17"
25 #define TPACPI_SYSFS_VERSION 0x020000
28 * Changelog:
29 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
30 * drivers/misc.
32 * 2006-11-22 0.13 new maintainer
33 * changelog now lives in git commit history, and will
34 * not be updated further in-file.
36 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
37 * 2005-03-17 0.11 support for 600e, 770x
38 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
39 * support for 770e, G41
40 * G40 and G41 don't have a thinklight
41 * temperatures no longer experimental
42 * experimental brightness control
43 * experimental volume control
44 * experimental fan enable/disable
45 * 2005-01-16 0.10 fix module loading on R30, R31
46 * 2005-01-16 0.9 support for 570, R30, R31
47 * ultrabay support on A22p, A3x
48 * limit arg for cmos, led, beep, drop experimental status
49 * more capable led control on A21e, A22p, T20-22, X20
50 * experimental temperatures and fan speed
51 * experimental embedded controller register dump
52 * mark more functions as __init, drop incorrect __exit
53 * use MODULE_VERSION
54 * thanks to Henrik Brix Andersen <brix@gentoo.org>
55 * fix parameter passing on module loading
56 * thanks to Rusty Russell <rusty@rustcorp.com.au>
57 * thanks to Jim Radford <radford@blackbean.org>
58 * 2004-11-08 0.8 fix init error case, don't return from a macro
59 * thanks to Chris Wright <chrisw@osdl.org>
60 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
61 * fix led control on A21e
62 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
63 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
64 * proc file format changed
65 * video_switch command
66 * experimental cmos control
67 * experimental led control
68 * experimental acpi sounds
69 * 2004-09-16 0.4 support for module parameters
70 * hotkey mask can be prefixed by 0x
71 * video output switching
72 * video expansion control
73 * ultrabay eject support
74 * removed lcd brightness/on/off control, didn't work
75 * 2004-08-17 0.3 support for R40
76 * lcd off, brightness control
77 * thinklight on/off
78 * 2004-08-14 0.2 support for T series, X20
79 * bluetooth enable/disable
80 * hotkey events disabled by default
81 * removed fan control, currently useless
82 * 2004-08-09 0.1 initial release, support for X series
85 #include "thinkpad_acpi.h"
87 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
88 MODULE_DESCRIPTION(IBM_DESC);
89 MODULE_VERSION(IBM_VERSION);
90 MODULE_LICENSE("GPL");
92 /* Please remove this in year 2009 */
93 MODULE_ALIAS("ibm_acpi");
96 * DMI matching for module autoloading
98 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
99 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
101 * Only models listed in thinkwiki will be supported, so add yours
102 * if it is not there yet.
104 #define IBM_BIOS_MODULE_ALIAS(__type) \
105 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
107 /* Non-ancient thinkpads */
108 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
109 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
111 /* Ancient thinkpad BIOSes have to be identified by
112 * BIOS type or model number, and there are far less
113 * BIOS types than model numbers... */
114 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
115 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
116 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
118 #define __unused __attribute__ ((unused))
120 static enum {
121 TPACPI_LIFE_INIT = 0,
122 TPACPI_LIFE_RUNNING,
123 TPACPI_LIFE_EXITING,
124 } tpacpi_lifecycle;
126 /****************************************************************************
127 ****************************************************************************
129 * ACPI Helpers and device model
131 ****************************************************************************
132 ****************************************************************************/
134 /*************************************************************************
135 * ACPI basic handles
138 static acpi_handle root_handle;
140 #define IBM_HANDLE(object, parent, paths...) \
141 static acpi_handle object##_handle; \
142 static acpi_handle *object##_parent = &parent##_handle; \
143 static char *object##_path; \
144 static char *object##_paths[] = { paths }
146 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
147 "\\_SB.PCI.ISA.EC", /* 570 */
148 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
149 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
150 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
151 "\\_SB.PCI0.ICH3.EC0", /* R31 */
152 "\\_SB.PCI0.LPC.EC", /* all others */
155 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
156 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
159 /*************************************************************************
160 * Misc ACPI handles
163 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
164 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
165 "\\CMS", /* R40, R40e */
166 ); /* all others */
168 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
169 "^HKEY", /* R30, R31 */
170 "HKEY", /* all others */
171 ); /* 570 */
174 /*************************************************************************
175 * ACPI helpers
178 static int acpi_evalf(acpi_handle handle,
179 void *res, char *method, char *fmt, ...)
181 char *fmt0 = fmt;
182 struct acpi_object_list params;
183 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
184 struct acpi_buffer result, *resultp;
185 union acpi_object out_obj;
186 acpi_status status;
187 va_list ap;
188 char res_type;
189 int success;
190 int quiet;
192 if (!*fmt) {
193 printk(IBM_ERR "acpi_evalf() called with empty format\n");
194 return 0;
197 if (*fmt == 'q') {
198 quiet = 1;
199 fmt++;
200 } else
201 quiet = 0;
203 res_type = *(fmt++);
205 params.count = 0;
206 params.pointer = &in_objs[0];
208 va_start(ap, fmt);
209 while (*fmt) {
210 char c = *(fmt++);
211 switch (c) {
212 case 'd': /* int */
213 in_objs[params.count].integer.value = va_arg(ap, int);
214 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
215 break;
216 /* add more types as needed */
217 default:
218 printk(IBM_ERR "acpi_evalf() called "
219 "with invalid format character '%c'\n", c);
220 return 0;
223 va_end(ap);
225 if (res_type != 'v') {
226 result.length = sizeof(out_obj);
227 result.pointer = &out_obj;
228 resultp = &result;
229 } else
230 resultp = NULL;
232 status = acpi_evaluate_object(handle, method, &params, resultp);
234 switch (res_type) {
235 case 'd': /* int */
236 if (res)
237 *(int *)res = out_obj.integer.value;
238 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
239 break;
240 case 'v': /* void */
241 success = status == AE_OK;
242 break;
243 /* add more types as needed */
244 default:
245 printk(IBM_ERR "acpi_evalf() called "
246 "with invalid format character '%c'\n", res_type);
247 return 0;
250 if (!success && !quiet)
251 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
252 method, fmt0, status);
254 return success;
257 static void __unused acpi_print_int(acpi_handle handle, char *method)
259 int i;
261 if (acpi_evalf(handle, &i, method, "d"))
262 printk(IBM_INFO "%s = 0x%x\n", method, i);
263 else
264 printk(IBM_ERR "error calling %s\n", method);
267 static int acpi_ec_read(int i, u8 * p)
269 int v;
271 if (ecrd_handle) {
272 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
273 return 0;
274 *p = v;
275 } else {
276 if (ec_read(i, p) < 0)
277 return 0;
280 return 1;
283 static int acpi_ec_write(int i, u8 v)
285 if (ecwr_handle) {
286 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
287 return 0;
288 } else {
289 if (ec_write(i, v) < 0)
290 return 0;
293 return 1;
296 static int _sta(acpi_handle handle)
298 int status;
300 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
301 status = 0;
303 return status;
306 static int issue_thinkpad_cmos_command(int cmos_cmd)
308 if (!cmos_handle)
309 return -ENXIO;
311 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
312 return -EIO;
314 return 0;
317 /*************************************************************************
318 * ACPI device model
321 static void drv_acpi_handle_init(char *name,
322 acpi_handle *handle, acpi_handle parent,
323 char **paths, int num_paths, char **path)
325 int i;
326 acpi_status status;
328 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
329 name);
331 for (i = 0; i < num_paths; i++) {
332 status = acpi_get_handle(parent, paths[i], handle);
333 if (ACPI_SUCCESS(status)) {
334 *path = paths[i];
335 dbg_printk(TPACPI_DBG_INIT,
336 "Found ACPI handle %s for %s\n",
337 *path, name);
338 return;
342 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
343 name);
344 *handle = NULL;
347 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
349 struct ibm_struct *ibm = data;
351 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
352 return;
354 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
355 return;
357 ibm->acpi->notify(ibm, event);
360 static int __init setup_acpi_notify(struct ibm_struct *ibm)
362 acpi_status status;
363 int rc;
365 BUG_ON(!ibm->acpi);
367 if (!*ibm->acpi->handle)
368 return 0;
370 vdbg_printk(TPACPI_DBG_INIT,
371 "setting up ACPI notify for %s\n", ibm->name);
373 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
374 if (rc < 0) {
375 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
376 ibm->name, rc);
377 return -ENODEV;
380 acpi_driver_data(ibm->acpi->device) = ibm;
381 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
382 IBM_ACPI_EVENT_PREFIX,
383 ibm->name);
385 status = acpi_install_notify_handler(*ibm->acpi->handle,
386 ibm->acpi->type, dispatch_acpi_notify, ibm);
387 if (ACPI_FAILURE(status)) {
388 if (status == AE_ALREADY_EXISTS) {
389 printk(IBM_NOTICE "another device driver is already handling %s events\n",
390 ibm->name);
391 } else {
392 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
393 ibm->name, status);
395 return -ENODEV;
397 ibm->flags.acpi_notify_installed = 1;
398 return 0;
401 static int __init tpacpi_device_add(struct acpi_device *device)
403 return 0;
406 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
408 int rc;
410 dbg_printk(TPACPI_DBG_INIT,
411 "registering %s as an ACPI driver\n", ibm->name);
413 BUG_ON(!ibm->acpi);
415 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
416 if (!ibm->acpi->driver) {
417 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
418 return -ENOMEM;
421 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
422 ibm->acpi->driver->ids = ibm->acpi->hid;
424 ibm->acpi->driver->ops.add = &tpacpi_device_add;
426 rc = acpi_bus_register_driver(ibm->acpi->driver);
427 if (rc < 0) {
428 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
429 ibm->name, rc);
430 kfree(ibm->acpi->driver);
431 ibm->acpi->driver = NULL;
432 } else if (!rc)
433 ibm->flags.acpi_driver_registered = 1;
435 return rc;
439 /****************************************************************************
440 ****************************************************************************
442 * Procfs Helpers
444 ****************************************************************************
445 ****************************************************************************/
447 static int dispatch_procfs_read(char *page, char **start, off_t off,
448 int count, int *eof, void *data)
450 struct ibm_struct *ibm = data;
451 int len;
453 if (!ibm || !ibm->read)
454 return -EINVAL;
456 len = ibm->read(page);
457 if (len < 0)
458 return len;
460 if (len <= off + count)
461 *eof = 1;
462 *start = page + off;
463 len -= off;
464 if (len > count)
465 len = count;
466 if (len < 0)
467 len = 0;
469 return len;
472 static int dispatch_procfs_write(struct file *file,
473 const char __user * userbuf,
474 unsigned long count, void *data)
476 struct ibm_struct *ibm = data;
477 char *kernbuf;
478 int ret;
480 if (!ibm || !ibm->write)
481 return -EINVAL;
483 kernbuf = kmalloc(count + 2, GFP_KERNEL);
484 if (!kernbuf)
485 return -ENOMEM;
487 if (copy_from_user(kernbuf, userbuf, count)) {
488 kfree(kernbuf);
489 return -EFAULT;
492 kernbuf[count] = 0;
493 strcat(kernbuf, ",");
494 ret = ibm->write(kernbuf);
495 if (ret == 0)
496 ret = count;
498 kfree(kernbuf);
500 return ret;
503 static char *next_cmd(char **cmds)
505 char *start = *cmds;
506 char *end;
508 while ((end = strchr(start, ',')) && end == start)
509 start = end + 1;
511 if (!end)
512 return NULL;
514 *end = 0;
515 *cmds = end + 1;
516 return start;
520 /****************************************************************************
521 ****************************************************************************
523 * Device model: input, hwmon and platform
525 ****************************************************************************
526 ****************************************************************************/
528 static struct platform_device *tpacpi_pdev;
529 static struct platform_device *tpacpi_sensors_pdev;
530 static struct class_device *tpacpi_hwmon;
531 static struct input_dev *tpacpi_inputdev;
532 static struct mutex tpacpi_inputdev_send_mutex;
535 static int tpacpi_resume_handler(struct platform_device *pdev)
537 struct ibm_struct *ibm, *itmp;
539 list_for_each_entry_safe(ibm, itmp,
540 &tpacpi_all_drivers,
541 all_drivers) {
542 if (ibm->resume)
543 (ibm->resume)();
546 return 0;
549 static struct platform_driver tpacpi_pdriver = {
550 .driver = {
551 .name = IBM_DRVR_NAME,
552 .owner = THIS_MODULE,
554 .resume = tpacpi_resume_handler,
557 static struct platform_driver tpacpi_hwmon_pdriver = {
558 .driver = {
559 .name = IBM_HWMON_DRVR_NAME,
560 .owner = THIS_MODULE,
564 /*************************************************************************
565 * thinkpad-acpi driver attributes
568 /* interface_version --------------------------------------------------- */
569 static ssize_t tpacpi_driver_interface_version_show(
570 struct device_driver *drv,
571 char *buf)
573 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
576 static DRIVER_ATTR(interface_version, S_IRUGO,
577 tpacpi_driver_interface_version_show, NULL);
579 /* debug_level --------------------------------------------------------- */
580 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
581 char *buf)
583 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
586 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
587 const char *buf, size_t count)
589 unsigned long t;
591 if (parse_strtoul(buf, 0xffff, &t))
592 return -EINVAL;
594 dbg_level = t;
596 return count;
599 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
600 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
602 /* version ------------------------------------------------------------- */
603 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
604 char *buf)
606 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
609 static DRIVER_ATTR(version, S_IRUGO,
610 tpacpi_driver_version_show, NULL);
612 /* --------------------------------------------------------------------- */
614 static struct driver_attribute* tpacpi_driver_attributes[] = {
615 &driver_attr_debug_level, &driver_attr_version,
616 &driver_attr_interface_version,
619 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
621 int i, res;
623 i = 0;
624 res = 0;
625 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
626 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
627 i++;
630 return res;
633 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
635 int i;
637 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
638 driver_remove_file(drv, tpacpi_driver_attributes[i]);
641 /*************************************************************************
642 * sysfs support helpers
645 struct attribute_set_obj {
646 struct attribute_set s;
647 struct attribute *a;
648 } __attribute__((packed));
650 static struct attribute_set *create_attr_set(unsigned int max_members,
651 const char* name)
653 struct attribute_set_obj *sobj;
655 if (max_members == 0)
656 return NULL;
658 /* Allocates space for implicit NULL at the end too */
659 sobj = kzalloc(sizeof(struct attribute_set_obj) +
660 max_members * sizeof(struct attribute *),
661 GFP_KERNEL);
662 if (!sobj)
663 return NULL;
664 sobj->s.max_members = max_members;
665 sobj->s.group.attrs = &sobj->a;
666 sobj->s.group.name = name;
668 return &sobj->s;
671 /* not multi-threaded safe, use it in a single thread per set */
672 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
674 if (!s || !attr)
675 return -EINVAL;
677 if (s->members >= s->max_members)
678 return -ENOMEM;
680 s->group.attrs[s->members] = attr;
681 s->members++;
683 return 0;
686 static int add_many_to_attr_set(struct attribute_set* s,
687 struct attribute **attr,
688 unsigned int count)
690 int i, res;
692 for (i = 0; i < count; i++) {
693 res = add_to_attr_set(s, attr[i]);
694 if (res)
695 return res;
698 return 0;
701 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
703 sysfs_remove_group(kobj, &s->group);
704 destroy_attr_set(s);
707 static int parse_strtoul(const char *buf,
708 unsigned long max, unsigned long *value)
710 char *endp;
712 while (*buf && isspace(*buf))
713 buf++;
714 *value = simple_strtoul(buf, &endp, 0);
715 while (*endp && isspace(*endp))
716 endp++;
717 if (*endp || *value > max)
718 return -EINVAL;
720 return 0;
723 /****************************************************************************
724 ****************************************************************************
726 * Subdrivers
728 ****************************************************************************
729 ****************************************************************************/
731 /*************************************************************************
732 * thinkpad-acpi init subdriver
735 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
737 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
738 printk(IBM_INFO "%s\n", IBM_URL);
740 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
741 (thinkpad_id.bios_version_str) ?
742 thinkpad_id.bios_version_str : "unknown",
743 (thinkpad_id.ec_version_str) ?
744 thinkpad_id.ec_version_str : "unknown");
746 if (thinkpad_id.vendor && thinkpad_id.model_str)
747 printk(IBM_INFO "%s %s\n",
748 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
749 "IBM" : ((thinkpad_id.vendor ==
750 PCI_VENDOR_ID_LENOVO) ?
751 "Lenovo" : "Unknown vendor"),
752 thinkpad_id.model_str);
754 return 0;
757 static int thinkpad_acpi_driver_read(char *p)
759 int len = 0;
761 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
762 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
764 return len;
767 static struct ibm_struct thinkpad_acpi_driver_data = {
768 .name = "driver",
769 .read = thinkpad_acpi_driver_read,
772 /*************************************************************************
773 * Hotkey subdriver
776 static int hotkey_orig_status;
777 static u32 hotkey_orig_mask;
778 static u32 hotkey_all_mask;
779 static u32 hotkey_reserved_mask;
781 static u16 *hotkey_keycode_map;
783 static struct attribute_set *hotkey_dev_attributes;
785 static int hotkey_get_wlsw(int *status)
787 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
788 return -EIO;
789 return 0;
792 /* sysfs hotkey enable ------------------------------------------------- */
793 static ssize_t hotkey_enable_show(struct device *dev,
794 struct device_attribute *attr,
795 char *buf)
797 int res, status;
798 u32 mask;
800 res = hotkey_get(&status, &mask);
801 if (res)
802 return res;
804 return snprintf(buf, PAGE_SIZE, "%d\n", status);
807 static ssize_t hotkey_enable_store(struct device *dev,
808 struct device_attribute *attr,
809 const char *buf, size_t count)
811 unsigned long t;
812 int res, status;
813 u32 mask;
815 if (parse_strtoul(buf, 1, &t))
816 return -EINVAL;
818 res = hotkey_get(&status, &mask);
819 if (!res)
820 res = hotkey_set(t, mask);
822 return (res) ? res : count;
825 static struct device_attribute dev_attr_hotkey_enable =
826 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
827 hotkey_enable_show, hotkey_enable_store);
829 /* sysfs hotkey mask --------------------------------------------------- */
830 static ssize_t hotkey_mask_show(struct device *dev,
831 struct device_attribute *attr,
832 char *buf)
834 int res, status;
835 u32 mask;
837 res = hotkey_get(&status, &mask);
838 if (res)
839 return res;
841 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
844 static ssize_t hotkey_mask_store(struct device *dev,
845 struct device_attribute *attr,
846 const char *buf, size_t count)
848 unsigned long t;
849 int res, status;
850 u32 mask;
852 if (parse_strtoul(buf, 0xffffffffUL, &t))
853 return -EINVAL;
855 res = hotkey_get(&status, &mask);
856 if (!res)
857 hotkey_set(status, t);
859 return (res) ? res : count;
862 static struct device_attribute dev_attr_hotkey_mask =
863 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
864 hotkey_mask_show, hotkey_mask_store);
866 /* sysfs hotkey bios_enabled ------------------------------------------- */
867 static ssize_t hotkey_bios_enabled_show(struct device *dev,
868 struct device_attribute *attr,
869 char *buf)
871 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
874 static struct device_attribute dev_attr_hotkey_bios_enabled =
875 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
877 /* sysfs hotkey bios_mask ---------------------------------------------- */
878 static ssize_t hotkey_bios_mask_show(struct device *dev,
879 struct device_attribute *attr,
880 char *buf)
882 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
885 static struct device_attribute dev_attr_hotkey_bios_mask =
886 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
888 /* sysfs hotkey all_mask ----------------------------------------------- */
889 static ssize_t hotkey_all_mask_show(struct device *dev,
890 struct device_attribute *attr,
891 char *buf)
893 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
896 static struct device_attribute dev_attr_hotkey_all_mask =
897 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
899 /* sysfs hotkey recommended_mask --------------------------------------- */
900 static ssize_t hotkey_recommended_mask_show(struct device *dev,
901 struct device_attribute *attr,
902 char *buf)
904 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
905 hotkey_all_mask & ~hotkey_reserved_mask);
908 static struct device_attribute dev_attr_hotkey_recommended_mask =
909 __ATTR(hotkey_recommended_mask, S_IRUGO,
910 hotkey_recommended_mask_show, NULL);
912 /* sysfs hotkey radio_sw ----------------------------------------------- */
913 static ssize_t hotkey_radio_sw_show(struct device *dev,
914 struct device_attribute *attr,
915 char *buf)
917 int res, s;
918 res = hotkey_get_wlsw(&s);
919 if (res < 0)
920 return res;
922 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
925 static struct device_attribute dev_attr_hotkey_radio_sw =
926 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
928 /* sysfs hotkey report_mode -------------------------------------------- */
929 static ssize_t hotkey_report_mode_show(struct device *dev,
930 struct device_attribute *attr,
931 char *buf)
933 return snprintf(buf, PAGE_SIZE, "%d\n",
934 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
937 static struct device_attribute dev_attr_hotkey_report_mode =
938 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
940 /* --------------------------------------------------------------------- */
942 static struct attribute *hotkey_attributes[] __initdata = {
943 &dev_attr_hotkey_enable.attr,
944 &dev_attr_hotkey_report_mode.attr,
947 static struct attribute *hotkey_mask_attributes[] __initdata = {
948 &dev_attr_hotkey_mask.attr,
949 &dev_attr_hotkey_bios_enabled.attr,
950 &dev_attr_hotkey_bios_mask.attr,
951 &dev_attr_hotkey_all_mask.attr,
952 &dev_attr_hotkey_recommended_mask.attr,
955 static int __init hotkey_init(struct ibm_init_struct *iibm)
957 /* Requirements for changing the default keymaps:
959 * 1. Many of the keys are mapped to KEY_RESERVED for very
960 * good reasons. Do not change them unless you have deep
961 * knowledge on the IBM and Lenovo ThinkPad firmware for
962 * the various ThinkPad models. The driver behaves
963 * differently for KEY_RESERVED: such keys have their
964 * hot key mask *unset* in mask_recommended, and also
965 * in the initial hot key mask programmed into the
966 * firmware at driver load time, which means the firm-
967 * ware may react very differently if you change them to
968 * something else;
970 * 2. You must be subscribed to the linux-thinkpad and
971 * ibm-acpi-devel mailing lists, and you should read the
972 * list archives since 2007 if you want to change the
973 * keymaps. This requirement exists so that you will
974 * know the past history of problems with the thinkpad-
975 * acpi driver keymaps, and also that you will be
976 * listening to any bug reports;
978 * 3. Do not send thinkpad-acpi specific patches directly to
979 * for merging, *ever*. Send them to the linux-acpi
980 * mailinglist for comments. Merging is to be done only
981 * through acpi-test and the ACPI maintainer.
983 * If the above is too much to ask, don't change the keymap.
984 * Ask the thinkpad-acpi maintainer to do it, instead.
986 static u16 ibm_keycode_map[] __initdata = {
987 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
988 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
989 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
990 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
992 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
993 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
994 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
995 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
997 /* brightness: firmware always reacts to them, unless
998 * X.org did some tricks in the radeon BIOS scratch
999 * registers of *some* models */
1000 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1001 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1003 /* Thinklight: firmware always react to it */
1004 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1006 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1007 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1009 /* Volume: firmware always react to it and reprograms
1010 * the built-in *extra* mixer. Never map it to control
1011 * another mixer by default. */
1012 KEY_RESERVED, /* 0x14: VOLUME UP */
1013 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1014 KEY_RESERVED, /* 0x16: MUTE */
1016 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1018 /* (assignments unknown, please report if found) */
1019 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1020 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1022 static u16 lenovo_keycode_map[] __initdata = {
1023 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1024 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1025 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1026 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1028 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1029 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1030 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1031 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1033 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1034 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1036 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1038 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1039 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1041 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1042 * react to it and reprograms the built-in *extra* mixer.
1043 * Never map it to control another mixer by default.
1045 * T60?, T61, R60?, R61: firmware and EC tries to send
1046 * these over the regular keyboard, so these are no-ops,
1047 * but there are still weird bugs re. MUTE, so do not
1048 * change unless you get test reports from all Lenovo
1049 * models. May cause the BIOS to interfere with the
1050 * HDA mixer.
1052 KEY_RESERVED, /* 0x14: VOLUME UP */
1053 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1054 KEY_RESERVED, /* 0x16: MUTE */
1056 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1058 /* (assignments unknown, please report if found) */
1059 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1060 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1063 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1064 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1065 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1067 int res, i;
1068 int status;
1069 int hkeyv;
1071 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1073 BUG_ON(!tpacpi_inputdev);
1075 IBM_ACPIHANDLE_INIT(hkey);
1076 mutex_init(&hotkey_mutex);
1078 /* hotkey not supported on 570 */
1079 tp_features.hotkey = hkey_handle != NULL;
1081 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1082 str_supported(tp_features.hotkey));
1084 if (tp_features.hotkey) {
1085 hotkey_dev_attributes = create_attr_set(8, NULL);
1086 if (!hotkey_dev_attributes)
1087 return -ENOMEM;
1088 res = add_many_to_attr_set(hotkey_dev_attributes,
1089 hotkey_attributes,
1090 ARRAY_SIZE(hotkey_attributes));
1091 if (res)
1092 return res;
1094 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1095 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1096 for HKEY interface version 0x100 */
1097 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1098 if ((hkeyv >> 8) != 1) {
1099 printk(IBM_ERR "unknown version of the "
1100 "HKEY interface: 0x%x\n", hkeyv);
1101 printk(IBM_ERR "please report this to %s\n",
1102 IBM_MAIL);
1103 } else {
1105 * MHKV 0x100 in A31, R40, R40e,
1106 * T4x, X31, and later
1107 * */
1108 tp_features.hotkey_mask = 1;
1112 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1113 str_supported(tp_features.hotkey_mask));
1115 if (tp_features.hotkey_mask) {
1116 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1117 "MHKA", "qd")) {
1118 printk(IBM_ERR
1119 "missing MHKA handler, "
1120 "please report this to %s\n",
1121 IBM_MAIL);
1122 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1126 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
1127 if (!res && tp_features.hotkey_mask) {
1128 res = add_many_to_attr_set(hotkey_dev_attributes,
1129 hotkey_mask_attributes,
1130 ARRAY_SIZE(hotkey_mask_attributes));
1133 /* Not all thinkpads have a hardware radio switch */
1134 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1135 tp_features.hotkey_wlsw = 1;
1136 printk(IBM_INFO
1137 "radio switch found; radios are %s\n",
1138 enabled(status, 0));
1139 res = add_to_attr_set(hotkey_dev_attributes,
1140 &dev_attr_hotkey_radio_sw.attr);
1143 if (!res)
1144 res = register_attr_set_with_sysfs(
1145 hotkey_dev_attributes,
1146 &tpacpi_pdev->dev.kobj);
1147 if (res)
1148 return res;
1150 /* Set up key map */
1152 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1153 GFP_KERNEL);
1154 if (!hotkey_keycode_map) {
1155 printk(IBM_ERR "failed to allocate memory for key map\n");
1156 return -ENOMEM;
1159 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1160 dbg_printk(TPACPI_DBG_INIT,
1161 "using Lenovo default hot key map\n");
1162 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1163 TPACPI_HOTKEY_MAP_SIZE);
1164 } else {
1165 dbg_printk(TPACPI_DBG_INIT,
1166 "using IBM default hot key map\n");
1167 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1168 TPACPI_HOTKEY_MAP_SIZE);
1171 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1172 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1173 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1174 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1175 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1176 tpacpi_inputdev->keycode = hotkey_keycode_map;
1177 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1178 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1179 set_bit(hotkey_keycode_map[i],
1180 tpacpi_inputdev->keybit);
1181 } else {
1182 if (i < sizeof(hotkey_reserved_mask)*8)
1183 hotkey_reserved_mask |= 1 << i;
1187 if (tp_features.hotkey_wlsw) {
1188 set_bit(EV_SW, tpacpi_inputdev->evbit);
1189 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1192 dbg_printk(TPACPI_DBG_INIT,
1193 "enabling hot key handling\n");
1194 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1195 | hotkey_orig_mask);
1196 if (res)
1197 return res;
1199 dbg_printk(TPACPI_DBG_INIT,
1200 "legacy hot key reporting over procfs %s\n",
1201 (hotkey_report_mode < 2) ?
1202 "enabled" : "disabled");
1205 return (tp_features.hotkey)? 0 : 1;
1208 static void hotkey_exit(void)
1210 int res;
1212 if (tp_features.hotkey) {
1213 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
1214 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1215 if (res)
1216 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
1219 if (hotkey_dev_attributes) {
1220 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1221 hotkey_dev_attributes = NULL;
1225 static void tpacpi_input_send_key(unsigned int scancode,
1226 unsigned int keycode)
1228 if (keycode != KEY_RESERVED) {
1229 mutex_lock(&tpacpi_inputdev_send_mutex);
1231 input_report_key(tpacpi_inputdev, keycode, 1);
1232 if (keycode == KEY_UNKNOWN)
1233 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1234 scancode);
1235 input_sync(tpacpi_inputdev);
1237 input_report_key(tpacpi_inputdev, keycode, 0);
1238 if (keycode == KEY_UNKNOWN)
1239 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1240 scancode);
1241 input_sync(tpacpi_inputdev);
1243 mutex_unlock(&tpacpi_inputdev_send_mutex);
1247 static void tpacpi_input_send_radiosw(void)
1249 int wlsw;
1251 mutex_lock(&tpacpi_inputdev_send_mutex);
1253 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1254 input_report_switch(tpacpi_inputdev,
1255 SW_RADIO, !!wlsw);
1256 input_sync(tpacpi_inputdev);
1259 mutex_unlock(&tpacpi_inputdev_send_mutex);
1262 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1264 u32 hkey;
1265 unsigned int keycode, scancode;
1266 int send_acpi_ev;
1267 int ignore_acpi_ev;
1269 if (event != 0x80) {
1270 printk(IBM_ERR "unknown HKEY notification event %d\n", event);
1271 /* forward it to userspace, maybe it knows how to handle it */
1272 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1273 ibm->acpi->device->dev.bus_id,
1274 event, 0);
1275 return;
1278 while (1) {
1279 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1280 printk(IBM_ERR "failed to retrieve HKEY event\n");
1281 return;
1284 if (hkey == 0) {
1285 /* queue empty */
1286 return;
1289 send_acpi_ev = 0;
1290 ignore_acpi_ev = 0;
1292 switch (hkey >> 12) {
1293 case 1:
1294 /* 0x1000-0x1FFF: key presses */
1295 scancode = hkey & 0xfff;
1296 if (scancode > 0 && scancode < 0x21) {
1297 scancode--;
1298 keycode = hotkey_keycode_map[scancode];
1299 tpacpi_input_send_key(scancode, keycode);
1300 } else {
1301 printk(IBM_ERR
1302 "hotkey 0x%04x out of range for keyboard map\n",
1303 hkey);
1304 send_acpi_ev = 1;
1306 break;
1307 case 5:
1308 /* 0x5000-0x5FFF: LID */
1309 /* we don't handle it through this path, just
1310 * eat up known LID events */
1311 if (hkey != 0x5001 && hkey != 0x5002) {
1312 printk(IBM_ERR
1313 "unknown LID-related HKEY event: 0x%04x\n",
1314 hkey);
1315 send_acpi_ev = 1;
1316 } else {
1317 ignore_acpi_ev = 1;
1319 break;
1320 case 7:
1321 /* 0x7000-0x7FFF: misc */
1322 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1323 tpacpi_input_send_radiosw();
1324 break;
1326 /* fallthrough to default */
1327 default:
1328 /* case 2: dock-related */
1329 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1330 /* case 3: ultra-bay related. maybe bay in dock? */
1331 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1332 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1333 send_acpi_ev = 1;
1336 /* Legacy events */
1337 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
1338 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
1341 /* netlink events */
1342 if (!ignore_acpi_ev && send_acpi_ev) {
1343 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1344 ibm->acpi->device->dev.bus_id,
1345 event, hkey);
1350 static void hotkey_resume(void)
1352 tpacpi_input_send_radiosw();
1356 * Call with hotkey_mutex held
1358 static int hotkey_get(int *status, u32 *mask)
1360 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1361 return -EIO;
1363 if (tp_features.hotkey_mask)
1364 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
1365 return -EIO;
1367 return 0;
1371 * Call with hotkey_mutex held
1373 static int hotkey_set(int status, u32 mask)
1375 int i;
1377 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1378 return -EIO;
1380 if (tp_features.hotkey_mask)
1381 for (i = 0; i < 32; i++) {
1382 int bit = ((1 << i) & mask) != 0;
1383 if (!acpi_evalf(hkey_handle,
1384 NULL, "MHKM", "vdd", i + 1, bit))
1385 return -EIO;
1388 return 0;
1391 /* procfs -------------------------------------------------------------- */
1392 static int hotkey_read(char *p)
1394 int res, status;
1395 u32 mask;
1396 int len = 0;
1398 if (!tp_features.hotkey) {
1399 len += sprintf(p + len, "status:\t\tnot supported\n");
1400 return len;
1403 if (mutex_lock_interruptible(&hotkey_mutex))
1404 return -ERESTARTSYS;
1405 res = hotkey_get(&status, &mask);
1406 mutex_unlock(&hotkey_mutex);
1407 if (res)
1408 return res;
1410 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1411 if (tp_features.hotkey_mask) {
1412 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1413 len += sprintf(p + len,
1414 "commands:\tenable, disable, reset, <mask>\n");
1415 } else {
1416 len += sprintf(p + len, "mask:\t\tnot supported\n");
1417 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1420 return len;
1423 static int hotkey_write(char *buf)
1425 int res, status;
1426 u32 mask;
1427 char *cmd;
1428 int do_cmd = 0;
1430 if (!tp_features.hotkey)
1431 return -ENODEV;
1433 if (mutex_lock_interruptible(&hotkey_mutex))
1434 return -ERESTARTSYS;
1436 res = hotkey_get(&status, &mask);
1437 if (res)
1438 goto errexit;
1440 res = 0;
1441 while ((cmd = next_cmd(&buf))) {
1442 if (strlencmp(cmd, "enable") == 0) {
1443 status = 1;
1444 } else if (strlencmp(cmd, "disable") == 0) {
1445 status = 0;
1446 } else if (strlencmp(cmd, "reset") == 0) {
1447 status = hotkey_orig_status;
1448 mask = hotkey_orig_mask;
1449 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1450 /* mask set */
1451 } else if (sscanf(cmd, "%x", &mask) == 1) {
1452 /* mask set */
1453 } else {
1454 res = -EINVAL;
1455 goto errexit;
1457 do_cmd = 1;
1460 if (do_cmd)
1461 res = hotkey_set(status, mask);
1463 errexit:
1464 mutex_unlock(&hotkey_mutex);
1465 return res;
1468 static const struct acpi_device_id ibm_htk_device_ids[] = {
1469 {IBM_HKEY_HID, 0},
1470 {"", 0},
1473 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1474 .hid = ibm_htk_device_ids,
1475 .notify = hotkey_notify,
1476 .handle = &hkey_handle,
1477 .type = ACPI_DEVICE_NOTIFY,
1480 static struct ibm_struct hotkey_driver_data = {
1481 .name = "hotkey",
1482 .read = hotkey_read,
1483 .write = hotkey_write,
1484 .exit = hotkey_exit,
1485 .resume = hotkey_resume,
1486 .acpi = &ibm_hotkey_acpidriver,
1489 /*************************************************************************
1490 * Bluetooth subdriver
1493 /* sysfs bluetooth enable ---------------------------------------------- */
1494 static ssize_t bluetooth_enable_show(struct device *dev,
1495 struct device_attribute *attr,
1496 char *buf)
1498 int status;
1500 status = bluetooth_get_radiosw();
1501 if (status < 0)
1502 return status;
1504 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1507 static ssize_t bluetooth_enable_store(struct device *dev,
1508 struct device_attribute *attr,
1509 const char *buf, size_t count)
1511 unsigned long t;
1512 int res;
1514 if (parse_strtoul(buf, 1, &t))
1515 return -EINVAL;
1517 res = bluetooth_set_radiosw(t);
1519 return (res) ? res : count;
1522 static struct device_attribute dev_attr_bluetooth_enable =
1523 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1524 bluetooth_enable_show, bluetooth_enable_store);
1526 /* --------------------------------------------------------------------- */
1528 static struct attribute *bluetooth_attributes[] = {
1529 &dev_attr_bluetooth_enable.attr,
1530 NULL
1533 static const struct attribute_group bluetooth_attr_group = {
1534 .attrs = bluetooth_attributes,
1537 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1539 int res;
1540 int status = 0;
1542 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1544 IBM_ACPIHANDLE_INIT(hkey);
1546 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1547 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1548 tp_features.bluetooth = hkey_handle &&
1549 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1551 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1552 str_supported(tp_features.bluetooth),
1553 status);
1555 if (tp_features.bluetooth) {
1556 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1557 /* no bluetooth hardware present in system */
1558 tp_features.bluetooth = 0;
1559 dbg_printk(TPACPI_DBG_INIT,
1560 "bluetooth hardware not installed\n");
1561 } else {
1562 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1563 &bluetooth_attr_group);
1564 if (res)
1565 return res;
1569 return (tp_features.bluetooth)? 0 : 1;
1572 static void bluetooth_exit(void)
1574 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1575 &bluetooth_attr_group);
1578 static int bluetooth_get_radiosw(void)
1580 int status;
1582 if (!tp_features.bluetooth)
1583 return -ENODEV;
1585 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1586 return -EIO;
1588 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1591 static int bluetooth_set_radiosw(int radio_on)
1593 int status;
1595 if (!tp_features.bluetooth)
1596 return -ENODEV;
1598 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1599 return -EIO;
1600 if (radio_on)
1601 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1602 else
1603 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1604 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1605 return -EIO;
1607 return 0;
1610 /* procfs -------------------------------------------------------------- */
1611 static int bluetooth_read(char *p)
1613 int len = 0;
1614 int status = bluetooth_get_radiosw();
1616 if (!tp_features.bluetooth)
1617 len += sprintf(p + len, "status:\t\tnot supported\n");
1618 else {
1619 len += sprintf(p + len, "status:\t\t%s\n",
1620 (status)? "enabled" : "disabled");
1621 len += sprintf(p + len, "commands:\tenable, disable\n");
1624 return len;
1627 static int bluetooth_write(char *buf)
1629 char *cmd;
1631 if (!tp_features.bluetooth)
1632 return -ENODEV;
1634 while ((cmd = next_cmd(&buf))) {
1635 if (strlencmp(cmd, "enable") == 0) {
1636 bluetooth_set_radiosw(1);
1637 } else if (strlencmp(cmd, "disable") == 0) {
1638 bluetooth_set_radiosw(0);
1639 } else
1640 return -EINVAL;
1643 return 0;
1646 static struct ibm_struct bluetooth_driver_data = {
1647 .name = "bluetooth",
1648 .read = bluetooth_read,
1649 .write = bluetooth_write,
1650 .exit = bluetooth_exit,
1653 /*************************************************************************
1654 * Wan subdriver
1657 /* sysfs wan enable ---------------------------------------------------- */
1658 static ssize_t wan_enable_show(struct device *dev,
1659 struct device_attribute *attr,
1660 char *buf)
1662 int status;
1664 status = wan_get_radiosw();
1665 if (status < 0)
1666 return status;
1668 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1671 static ssize_t wan_enable_store(struct device *dev,
1672 struct device_attribute *attr,
1673 const char *buf, size_t count)
1675 unsigned long t;
1676 int res;
1678 if (parse_strtoul(buf, 1, &t))
1679 return -EINVAL;
1681 res = wan_set_radiosw(t);
1683 return (res) ? res : count;
1686 static struct device_attribute dev_attr_wan_enable =
1687 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1688 wan_enable_show, wan_enable_store);
1690 /* --------------------------------------------------------------------- */
1692 static struct attribute *wan_attributes[] = {
1693 &dev_attr_wan_enable.attr,
1694 NULL
1697 static const struct attribute_group wan_attr_group = {
1698 .attrs = wan_attributes,
1701 static int __init wan_init(struct ibm_init_struct *iibm)
1703 int res;
1704 int status = 0;
1706 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1708 IBM_ACPIHANDLE_INIT(hkey);
1710 tp_features.wan = hkey_handle &&
1711 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1713 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1714 str_supported(tp_features.wan),
1715 status);
1717 if (tp_features.wan) {
1718 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1719 /* no wan hardware present in system */
1720 tp_features.wan = 0;
1721 dbg_printk(TPACPI_DBG_INIT,
1722 "wan hardware not installed\n");
1723 } else {
1724 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1725 &wan_attr_group);
1726 if (res)
1727 return res;
1731 return (tp_features.wan)? 0 : 1;
1734 static void wan_exit(void)
1736 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1737 &wan_attr_group);
1740 static int wan_get_radiosw(void)
1742 int status;
1744 if (!tp_features.wan)
1745 return -ENODEV;
1747 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1748 return -EIO;
1750 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1753 static int wan_set_radiosw(int radio_on)
1755 int status;
1757 if (!tp_features.wan)
1758 return -ENODEV;
1760 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1761 return -EIO;
1762 if (radio_on)
1763 status |= TP_ACPI_WANCARD_RADIOSSW;
1764 else
1765 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1766 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1767 return -EIO;
1769 return 0;
1772 /* procfs -------------------------------------------------------------- */
1773 static int wan_read(char *p)
1775 int len = 0;
1776 int status = wan_get_radiosw();
1778 if (!tp_features.wan)
1779 len += sprintf(p + len, "status:\t\tnot supported\n");
1780 else {
1781 len += sprintf(p + len, "status:\t\t%s\n",
1782 (status)? "enabled" : "disabled");
1783 len += sprintf(p + len, "commands:\tenable, disable\n");
1786 return len;
1789 static int wan_write(char *buf)
1791 char *cmd;
1793 if (!tp_features.wan)
1794 return -ENODEV;
1796 while ((cmd = next_cmd(&buf))) {
1797 if (strlencmp(cmd, "enable") == 0) {
1798 wan_set_radiosw(1);
1799 } else if (strlencmp(cmd, "disable") == 0) {
1800 wan_set_radiosw(0);
1801 } else
1802 return -EINVAL;
1805 return 0;
1808 static struct ibm_struct wan_driver_data = {
1809 .name = "wan",
1810 .read = wan_read,
1811 .write = wan_write,
1812 .exit = wan_exit,
1813 .flags.experimental = 1,
1816 /*************************************************************************
1817 * Video subdriver
1820 static enum video_access_mode video_supported;
1821 static int video_orig_autosw;
1823 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1824 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1825 "\\_SB.PCI0.VID0", /* 770e */
1826 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1827 "\\_SB.PCI0.AGP.VID", /* all others */
1828 ); /* R30, R31 */
1830 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1832 static int __init video_init(struct ibm_init_struct *iibm)
1834 int ivga;
1836 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1838 IBM_ACPIHANDLE_INIT(vid);
1839 IBM_ACPIHANDLE_INIT(vid2);
1841 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1842 /* G41, assume IVGA doesn't change */
1843 vid_handle = vid2_handle;
1845 if (!vid_handle)
1846 /* video switching not supported on R30, R31 */
1847 video_supported = TPACPI_VIDEO_NONE;
1848 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1849 /* 570 */
1850 video_supported = TPACPI_VIDEO_570;
1851 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1852 /* 600e/x, 770e, 770x */
1853 video_supported = TPACPI_VIDEO_770;
1854 else
1855 /* all others */
1856 video_supported = TPACPI_VIDEO_NEW;
1858 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1859 str_supported(video_supported != TPACPI_VIDEO_NONE),
1860 video_supported);
1862 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1865 static void video_exit(void)
1867 dbg_printk(TPACPI_DBG_EXIT,
1868 "restoring original video autoswitch mode\n");
1869 if (video_autosw_set(video_orig_autosw))
1870 printk(IBM_ERR "error while trying to restore original "
1871 "video autoswitch mode\n");
1874 static int video_outputsw_get(void)
1876 int status = 0;
1877 int i;
1879 switch (video_supported) {
1880 case TPACPI_VIDEO_570:
1881 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1882 TP_ACPI_VIDEO_570_PHSCMD))
1883 return -EIO;
1884 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1885 break;
1886 case TPACPI_VIDEO_770:
1887 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1888 return -EIO;
1889 if (i)
1890 status |= TP_ACPI_VIDEO_S_LCD;
1891 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1892 return -EIO;
1893 if (i)
1894 status |= TP_ACPI_VIDEO_S_CRT;
1895 break;
1896 case TPACPI_VIDEO_NEW:
1897 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1898 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1899 return -EIO;
1900 if (i)
1901 status |= TP_ACPI_VIDEO_S_CRT;
1903 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1904 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1905 return -EIO;
1906 if (i)
1907 status |= TP_ACPI_VIDEO_S_LCD;
1908 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1909 return -EIO;
1910 if (i)
1911 status |= TP_ACPI_VIDEO_S_DVI;
1912 break;
1913 default:
1914 return -ENOSYS;
1917 return status;
1920 static int video_outputsw_set(int status)
1922 int autosw;
1923 int res = 0;
1925 switch (video_supported) {
1926 case TPACPI_VIDEO_570:
1927 res = acpi_evalf(NULL, NULL,
1928 "\\_SB.PHS2", "vdd",
1929 TP_ACPI_VIDEO_570_PHS2CMD,
1930 status | TP_ACPI_VIDEO_570_PHS2SET);
1931 break;
1932 case TPACPI_VIDEO_770:
1933 autosw = video_autosw_get();
1934 if (autosw < 0)
1935 return autosw;
1937 res = video_autosw_set(1);
1938 if (res)
1939 return res;
1940 res = acpi_evalf(vid_handle, NULL,
1941 "ASWT", "vdd", status * 0x100, 0);
1942 if (!autosw && video_autosw_set(autosw)) {
1943 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1944 return -EIO;
1946 break;
1947 case TPACPI_VIDEO_NEW:
1948 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1949 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1950 break;
1951 default:
1952 return -ENOSYS;
1955 return (res)? 0 : -EIO;
1958 static int video_autosw_get(void)
1960 int autosw = 0;
1962 switch (video_supported) {
1963 case TPACPI_VIDEO_570:
1964 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1965 return -EIO;
1966 break;
1967 case TPACPI_VIDEO_770:
1968 case TPACPI_VIDEO_NEW:
1969 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1970 return -EIO;
1971 break;
1972 default:
1973 return -ENOSYS;
1976 return autosw & 1;
1979 static int video_autosw_set(int enable)
1981 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1982 return -EIO;
1983 return 0;
1986 static int video_outputsw_cycle(void)
1988 int autosw = video_autosw_get();
1989 int res;
1991 if (autosw < 0)
1992 return autosw;
1994 switch (video_supported) {
1995 case TPACPI_VIDEO_570:
1996 res = video_autosw_set(1);
1997 if (res)
1998 return res;
1999 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2000 break;
2001 case TPACPI_VIDEO_770:
2002 case TPACPI_VIDEO_NEW:
2003 res = video_autosw_set(1);
2004 if (res)
2005 return res;
2006 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2007 break;
2008 default:
2009 return -ENOSYS;
2011 if (!autosw && video_autosw_set(autosw)) {
2012 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2013 return -EIO;
2016 return (res)? 0 : -EIO;
2019 static int video_expand_toggle(void)
2021 switch (video_supported) {
2022 case TPACPI_VIDEO_570:
2023 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2024 0 : -EIO;
2025 case TPACPI_VIDEO_770:
2026 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2027 0 : -EIO;
2028 case TPACPI_VIDEO_NEW:
2029 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2030 0 : -EIO;
2031 default:
2032 return -ENOSYS;
2034 /* not reached */
2037 static int video_read(char *p)
2039 int status, autosw;
2040 int len = 0;
2042 if (video_supported == TPACPI_VIDEO_NONE) {
2043 len += sprintf(p + len, "status:\t\tnot supported\n");
2044 return len;
2047 status = video_outputsw_get();
2048 if (status < 0)
2049 return status;
2051 autosw = video_autosw_get();
2052 if (autosw < 0)
2053 return autosw;
2055 len += sprintf(p + len, "status:\t\tsupported\n");
2056 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2057 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2058 if (video_supported == TPACPI_VIDEO_NEW)
2059 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2060 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2061 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2062 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2063 if (video_supported == TPACPI_VIDEO_NEW)
2064 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2065 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2066 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2068 return len;
2071 static int video_write(char *buf)
2073 char *cmd;
2074 int enable, disable, status;
2075 int res;
2077 if (video_supported == TPACPI_VIDEO_NONE)
2078 return -ENODEV;
2080 enable = 0;
2081 disable = 0;
2083 while ((cmd = next_cmd(&buf))) {
2084 if (strlencmp(cmd, "lcd_enable") == 0) {
2085 enable |= TP_ACPI_VIDEO_S_LCD;
2086 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2087 disable |= TP_ACPI_VIDEO_S_LCD;
2088 } else if (strlencmp(cmd, "crt_enable") == 0) {
2089 enable |= TP_ACPI_VIDEO_S_CRT;
2090 } else if (strlencmp(cmd, "crt_disable") == 0) {
2091 disable |= TP_ACPI_VIDEO_S_CRT;
2092 } else if (video_supported == TPACPI_VIDEO_NEW &&
2093 strlencmp(cmd, "dvi_enable") == 0) {
2094 enable |= TP_ACPI_VIDEO_S_DVI;
2095 } else if (video_supported == TPACPI_VIDEO_NEW &&
2096 strlencmp(cmd, "dvi_disable") == 0) {
2097 disable |= TP_ACPI_VIDEO_S_DVI;
2098 } else if (strlencmp(cmd, "auto_enable") == 0) {
2099 res = video_autosw_set(1);
2100 if (res)
2101 return res;
2102 } else if (strlencmp(cmd, "auto_disable") == 0) {
2103 res = video_autosw_set(0);
2104 if (res)
2105 return res;
2106 } else if (strlencmp(cmd, "video_switch") == 0) {
2107 res = video_outputsw_cycle();
2108 if (res)
2109 return res;
2110 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2111 res = video_expand_toggle();
2112 if (res)
2113 return res;
2114 } else
2115 return -EINVAL;
2118 if (enable || disable) {
2119 status = video_outputsw_get();
2120 if (status < 0)
2121 return status;
2122 res = video_outputsw_set((status & ~disable) | enable);
2123 if (res)
2124 return res;
2127 return 0;
2130 static struct ibm_struct video_driver_data = {
2131 .name = "video",
2132 .read = video_read,
2133 .write = video_write,
2134 .exit = video_exit,
2137 /*************************************************************************
2138 * Light (thinklight) subdriver
2141 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2142 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2144 static int __init light_init(struct ibm_init_struct *iibm)
2146 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2148 IBM_ACPIHANDLE_INIT(ledb);
2149 IBM_ACPIHANDLE_INIT(lght);
2150 IBM_ACPIHANDLE_INIT(cmos);
2152 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2153 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2155 if (tp_features.light)
2156 /* light status not supported on
2157 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2158 tp_features.light_status =
2159 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2161 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2162 str_supported(tp_features.light));
2164 return (tp_features.light)? 0 : 1;
2167 static int light_read(char *p)
2169 int len = 0;
2170 int status = 0;
2172 if (!tp_features.light) {
2173 len += sprintf(p + len, "status:\t\tnot supported\n");
2174 } else if (!tp_features.light_status) {
2175 len += sprintf(p + len, "status:\t\tunknown\n");
2176 len += sprintf(p + len, "commands:\ton, off\n");
2177 } else {
2178 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2179 return -EIO;
2180 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2181 len += sprintf(p + len, "commands:\ton, off\n");
2184 return len;
2187 static int light_write(char *buf)
2189 int cmos_cmd, lght_cmd;
2190 char *cmd;
2191 int success;
2193 if (!tp_features.light)
2194 return -ENODEV;
2196 while ((cmd = next_cmd(&buf))) {
2197 if (strlencmp(cmd, "on") == 0) {
2198 cmos_cmd = 0x0c;
2199 lght_cmd = 1;
2200 } else if (strlencmp(cmd, "off") == 0) {
2201 cmos_cmd = 0x0d;
2202 lght_cmd = 0;
2203 } else
2204 return -EINVAL;
2206 success = cmos_handle ?
2207 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2208 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2209 if (!success)
2210 return -EIO;
2213 return 0;
2216 static struct ibm_struct light_driver_data = {
2217 .name = "light",
2218 .read = light_read,
2219 .write = light_write,
2222 /*************************************************************************
2223 * Dock subdriver
2226 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2228 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2229 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2230 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2231 "\\_SB.PCI.ISA.SLCE", /* 570 */
2232 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2234 /* don't list other alternatives as we install a notify handler on the 570 */
2235 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2237 static const struct acpi_device_id ibm_pci_device_ids[] = {
2238 {PCI_ROOT_HID_STRING, 0},
2239 {"", 0},
2242 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2244 .notify = dock_notify,
2245 .handle = &dock_handle,
2246 .type = ACPI_SYSTEM_NOTIFY,
2249 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2250 * We just use it to get notifications of dock hotplug
2251 * in very old thinkpads */
2252 .hid = ibm_pci_device_ids,
2253 .notify = dock_notify,
2254 .handle = &pci_handle,
2255 .type = ACPI_SYSTEM_NOTIFY,
2259 static struct ibm_struct dock_driver_data[2] = {
2261 .name = "dock",
2262 .read = dock_read,
2263 .write = dock_write,
2264 .acpi = &ibm_dock_acpidriver[0],
2267 .name = "dock",
2268 .acpi = &ibm_dock_acpidriver[1],
2272 #define dock_docked() (_sta(dock_handle) & 1)
2274 static int __init dock_init(struct ibm_init_struct *iibm)
2276 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2278 IBM_ACPIHANDLE_INIT(dock);
2280 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2281 str_supported(dock_handle != NULL));
2283 return (dock_handle)? 0 : 1;
2286 static int __init dock_init2(struct ibm_init_struct *iibm)
2288 int dock2_needed;
2290 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2292 if (dock_driver_data[0].flags.acpi_driver_registered &&
2293 dock_driver_data[0].flags.acpi_notify_installed) {
2294 IBM_ACPIHANDLE_INIT(pci);
2295 dock2_needed = (pci_handle != NULL);
2296 vdbg_printk(TPACPI_DBG_INIT,
2297 "dock PCI handler for the TP 570 is %s\n",
2298 str_supported(dock2_needed));
2299 } else {
2300 vdbg_printk(TPACPI_DBG_INIT,
2301 "dock subdriver part 2 not required\n");
2302 dock2_needed = 0;
2305 return (dock2_needed)? 0 : 1;
2308 static void dock_notify(struct ibm_struct *ibm, u32 event)
2310 int docked = dock_docked();
2311 int pci = ibm->acpi->hid && ibm->acpi->device &&
2312 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
2313 int data;
2315 if (event == 1 && !pci) /* 570 */
2316 data = 1; /* button */
2317 else if (event == 1 && pci) /* 570 */
2318 data = 3; /* dock */
2319 else if (event == 3 && docked)
2320 data = 1; /* button */
2321 else if (event == 3 && !docked)
2322 data = 2; /* undock */
2323 else if (event == 0 && docked)
2324 data = 3; /* dock */
2325 else {
2326 printk(IBM_ERR "unknown dock event %d, status %d\n",
2327 event, _sta(dock_handle));
2328 data = 0; /* unknown */
2330 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
2331 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2332 ibm->acpi->device->dev.bus_id,
2333 event, data);
2336 static int dock_read(char *p)
2338 int len = 0;
2339 int docked = dock_docked();
2341 if (!dock_handle)
2342 len += sprintf(p + len, "status:\t\tnot supported\n");
2343 else if (!docked)
2344 len += sprintf(p + len, "status:\t\tundocked\n");
2345 else {
2346 len += sprintf(p + len, "status:\t\tdocked\n");
2347 len += sprintf(p + len, "commands:\tdock, undock\n");
2350 return len;
2353 static int dock_write(char *buf)
2355 char *cmd;
2357 if (!dock_docked())
2358 return -ENODEV;
2360 while ((cmd = next_cmd(&buf))) {
2361 if (strlencmp(cmd, "undock") == 0) {
2362 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2363 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2364 return -EIO;
2365 } else if (strlencmp(cmd, "dock") == 0) {
2366 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2367 return -EIO;
2368 } else
2369 return -EINVAL;
2372 return 0;
2375 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2377 /*************************************************************************
2378 * Bay subdriver
2381 #ifdef CONFIG_THINKPAD_ACPI_BAY
2382 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2383 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2384 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2385 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2386 ); /* A21e, R30, R31 */
2387 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2388 "_EJ0", /* all others */
2389 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2390 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2391 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2392 ); /* all others */
2393 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2394 "_EJ0", /* 770x */
2395 ); /* all others */
2397 static int __init bay_init(struct ibm_init_struct *iibm)
2399 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2401 IBM_ACPIHANDLE_INIT(bay);
2402 if (bay_handle)
2403 IBM_ACPIHANDLE_INIT(bay_ej);
2404 IBM_ACPIHANDLE_INIT(bay2);
2405 if (bay2_handle)
2406 IBM_ACPIHANDLE_INIT(bay2_ej);
2408 tp_features.bay_status = bay_handle &&
2409 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2410 tp_features.bay_status2 = bay2_handle &&
2411 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2413 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2414 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2415 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2416 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2418 vdbg_printk(TPACPI_DBG_INIT,
2419 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2420 str_supported(tp_features.bay_status),
2421 str_supported(tp_features.bay_eject),
2422 str_supported(tp_features.bay_status2),
2423 str_supported(tp_features.bay_eject2));
2425 return (tp_features.bay_status || tp_features.bay_eject ||
2426 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2429 static void bay_notify(struct ibm_struct *ibm, u32 event)
2431 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
2432 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2433 ibm->acpi->device->dev.bus_id,
2434 event, 0);
2437 #define bay_occupied(b) (_sta(b##_handle) & 1)
2439 static int bay_read(char *p)
2441 int len = 0;
2442 int occupied = bay_occupied(bay);
2443 int occupied2 = bay_occupied(bay2);
2444 int eject, eject2;
2446 len += sprintf(p + len, "status:\t\t%s\n",
2447 tp_features.bay_status ?
2448 (occupied ? "occupied" : "unoccupied") :
2449 "not supported");
2450 if (tp_features.bay_status2)
2451 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2452 "occupied" : "unoccupied");
2454 eject = tp_features.bay_eject && occupied;
2455 eject2 = tp_features.bay_eject2 && occupied2;
2457 if (eject && eject2)
2458 len += sprintf(p + len, "commands:\teject, eject2\n");
2459 else if (eject)
2460 len += sprintf(p + len, "commands:\teject\n");
2461 else if (eject2)
2462 len += sprintf(p + len, "commands:\teject2\n");
2464 return len;
2467 static int bay_write(char *buf)
2469 char *cmd;
2471 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2472 return -ENODEV;
2474 while ((cmd = next_cmd(&buf))) {
2475 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2476 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2477 return -EIO;
2478 } else if (tp_features.bay_eject2 &&
2479 strlencmp(cmd, "eject2") == 0) {
2480 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2481 return -EIO;
2482 } else
2483 return -EINVAL;
2486 return 0;
2489 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2490 .notify = bay_notify,
2491 .handle = &bay_handle,
2492 .type = ACPI_SYSTEM_NOTIFY,
2495 static struct ibm_struct bay_driver_data = {
2496 .name = "bay",
2497 .read = bay_read,
2498 .write = bay_write,
2499 .acpi = &ibm_bay_acpidriver,
2502 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2504 /*************************************************************************
2505 * CMOS subdriver
2508 /* sysfs cmos_command -------------------------------------------------- */
2509 static ssize_t cmos_command_store(struct device *dev,
2510 struct device_attribute *attr,
2511 const char *buf, size_t count)
2513 unsigned long cmos_cmd;
2514 int res;
2516 if (parse_strtoul(buf, 21, &cmos_cmd))
2517 return -EINVAL;
2519 res = issue_thinkpad_cmos_command(cmos_cmd);
2520 return (res)? res : count;
2523 static struct device_attribute dev_attr_cmos_command =
2524 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2526 /* --------------------------------------------------------------------- */
2528 static int __init cmos_init(struct ibm_init_struct *iibm)
2530 int res;
2532 vdbg_printk(TPACPI_DBG_INIT,
2533 "initializing cmos commands subdriver\n");
2535 IBM_ACPIHANDLE_INIT(cmos);
2537 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2538 str_supported(cmos_handle != NULL));
2540 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2541 if (res)
2542 return res;
2544 return (cmos_handle)? 0 : 1;
2547 static void cmos_exit(void)
2549 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2552 static int cmos_read(char *p)
2554 int len = 0;
2556 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2557 R30, R31, T20-22, X20-21 */
2558 if (!cmos_handle)
2559 len += sprintf(p + len, "status:\t\tnot supported\n");
2560 else {
2561 len += sprintf(p + len, "status:\t\tsupported\n");
2562 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2565 return len;
2568 static int cmos_write(char *buf)
2570 char *cmd;
2571 int cmos_cmd, res;
2573 while ((cmd = next_cmd(&buf))) {
2574 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2575 cmos_cmd >= 0 && cmos_cmd <= 21) {
2576 /* cmos_cmd set */
2577 } else
2578 return -EINVAL;
2580 res = issue_thinkpad_cmos_command(cmos_cmd);
2581 if (res)
2582 return res;
2585 return 0;
2588 static struct ibm_struct cmos_driver_data = {
2589 .name = "cmos",
2590 .read = cmos_read,
2591 .write = cmos_write,
2592 .exit = cmos_exit,
2595 /*************************************************************************
2596 * LED subdriver
2599 static enum led_access_mode led_supported;
2601 IBM_HANDLE(led, ec, "SLED", /* 570 */
2602 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2603 "LED", /* all others */
2604 ); /* R30, R31 */
2606 static int __init led_init(struct ibm_init_struct *iibm)
2608 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2610 IBM_ACPIHANDLE_INIT(led);
2612 if (!led_handle)
2613 /* led not supported on R30, R31 */
2614 led_supported = TPACPI_LED_NONE;
2615 else if (strlencmp(led_path, "SLED") == 0)
2616 /* 570 */
2617 led_supported = TPACPI_LED_570;
2618 else if (strlencmp(led_path, "SYSL") == 0)
2619 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2620 led_supported = TPACPI_LED_OLD;
2621 else
2622 /* all others */
2623 led_supported = TPACPI_LED_NEW;
2625 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2626 str_supported(led_supported), led_supported);
2628 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2631 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2633 static int led_read(char *p)
2635 int len = 0;
2637 if (!led_supported) {
2638 len += sprintf(p + len, "status:\t\tnot supported\n");
2639 return len;
2641 len += sprintf(p + len, "status:\t\tsupported\n");
2643 if (led_supported == TPACPI_LED_570) {
2644 /* 570 */
2645 int i, status;
2646 for (i = 0; i < 8; i++) {
2647 if (!acpi_evalf(ec_handle,
2648 &status, "GLED", "dd", 1 << i))
2649 return -EIO;
2650 len += sprintf(p + len, "%d:\t\t%s\n",
2651 i, led_status(status));
2655 len += sprintf(p + len, "commands:\t"
2656 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2658 return len;
2661 /* off, on, blink */
2662 static const int led_sled_arg1[] = { 0, 1, 3 };
2663 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2664 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2665 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2667 static int led_write(char *buf)
2669 char *cmd;
2670 int led, ind, ret;
2672 if (!led_supported)
2673 return -ENODEV;
2675 while ((cmd = next_cmd(&buf))) {
2676 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2677 return -EINVAL;
2679 if (strstr(cmd, "off")) {
2680 ind = 0;
2681 } else if (strstr(cmd, "on")) {
2682 ind = 1;
2683 } else if (strstr(cmd, "blink")) {
2684 ind = 2;
2685 } else
2686 return -EINVAL;
2688 if (led_supported == TPACPI_LED_570) {
2689 /* 570 */
2690 led = 1 << led;
2691 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2692 led, led_sled_arg1[ind]))
2693 return -EIO;
2694 } else if (led_supported == TPACPI_LED_OLD) {
2695 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2696 led = 1 << led;
2697 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2698 if (ret >= 0)
2699 ret =
2700 ec_write(TPACPI_LED_EC_HLBL,
2701 led * led_exp_hlbl[ind]);
2702 if (ret >= 0)
2703 ret =
2704 ec_write(TPACPI_LED_EC_HLCL,
2705 led * led_exp_hlcl[ind]);
2706 if (ret < 0)
2707 return ret;
2708 } else {
2709 /* all others */
2710 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2711 led, led_led_arg1[ind]))
2712 return -EIO;
2716 return 0;
2719 static struct ibm_struct led_driver_data = {
2720 .name = "led",
2721 .read = led_read,
2722 .write = led_write,
2725 /*************************************************************************
2726 * Beep subdriver
2729 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2731 static int __init beep_init(struct ibm_init_struct *iibm)
2733 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2735 IBM_ACPIHANDLE_INIT(beep);
2737 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2738 str_supported(beep_handle != NULL));
2740 return (beep_handle)? 0 : 1;
2743 static int beep_read(char *p)
2745 int len = 0;
2747 if (!beep_handle)
2748 len += sprintf(p + len, "status:\t\tnot supported\n");
2749 else {
2750 len += sprintf(p + len, "status:\t\tsupported\n");
2751 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2754 return len;
2757 static int beep_write(char *buf)
2759 char *cmd;
2760 int beep_cmd;
2762 if (!beep_handle)
2763 return -ENODEV;
2765 while ((cmd = next_cmd(&buf))) {
2766 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2767 beep_cmd >= 0 && beep_cmd <= 17) {
2768 /* beep_cmd set */
2769 } else
2770 return -EINVAL;
2771 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2772 return -EIO;
2775 return 0;
2778 static struct ibm_struct beep_driver_data = {
2779 .name = "beep",
2780 .read = beep_read,
2781 .write = beep_write,
2784 /*************************************************************************
2785 * Thermal subdriver
2788 static enum thermal_access_mode thermal_read_mode;
2790 /* sysfs temp##_input -------------------------------------------------- */
2792 static ssize_t thermal_temp_input_show(struct device *dev,
2793 struct device_attribute *attr,
2794 char *buf)
2796 struct sensor_device_attribute *sensor_attr =
2797 to_sensor_dev_attr(attr);
2798 int idx = sensor_attr->index;
2799 s32 value;
2800 int res;
2802 res = thermal_get_sensor(idx, &value);
2803 if (res)
2804 return res;
2805 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2806 return -ENXIO;
2808 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2811 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2812 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2814 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2815 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2816 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2817 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2818 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2819 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2820 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2821 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2822 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2823 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2824 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2825 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2826 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2827 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2828 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2829 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2830 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2833 #define THERMAL_ATTRS(X) \
2834 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2836 static struct attribute *thermal_temp_input_attr[] = {
2837 THERMAL_ATTRS(8),
2838 THERMAL_ATTRS(9),
2839 THERMAL_ATTRS(10),
2840 THERMAL_ATTRS(11),
2841 THERMAL_ATTRS(12),
2842 THERMAL_ATTRS(13),
2843 THERMAL_ATTRS(14),
2844 THERMAL_ATTRS(15),
2845 THERMAL_ATTRS(0),
2846 THERMAL_ATTRS(1),
2847 THERMAL_ATTRS(2),
2848 THERMAL_ATTRS(3),
2849 THERMAL_ATTRS(4),
2850 THERMAL_ATTRS(5),
2851 THERMAL_ATTRS(6),
2852 THERMAL_ATTRS(7),
2853 NULL
2856 static const struct attribute_group thermal_temp_input16_group = {
2857 .attrs = thermal_temp_input_attr
2860 static const struct attribute_group thermal_temp_input8_group = {
2861 .attrs = &thermal_temp_input_attr[8]
2864 #undef THERMAL_SENSOR_ATTR_TEMP
2865 #undef THERMAL_ATTRS
2867 /* --------------------------------------------------------------------- */
2869 static int __init thermal_init(struct ibm_init_struct *iibm)
2871 u8 t, ta1, ta2;
2872 int i;
2873 int acpi_tmp7;
2874 int res;
2876 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2878 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2880 if (thinkpad_id.ec_model) {
2882 * Direct EC access mode: sensors at registers
2883 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2884 * non-implemented, thermal sensors return 0x80 when
2885 * not available
2888 ta1 = ta2 = 0;
2889 for (i = 0; i < 8; i++) {
2890 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2891 ta1 |= t;
2892 } else {
2893 ta1 = 0;
2894 break;
2896 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2897 ta2 |= t;
2898 } else {
2899 ta1 = 0;
2900 break;
2903 if (ta1 == 0) {
2904 /* This is sheer paranoia, but we handle it anyway */
2905 if (acpi_tmp7) {
2906 printk(IBM_ERR
2907 "ThinkPad ACPI EC access misbehaving, "
2908 "falling back to ACPI TMPx access mode\n");
2909 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2910 } else {
2911 printk(IBM_ERR
2912 "ThinkPad ACPI EC access misbehaving, "
2913 "disabling thermal sensors access\n");
2914 thermal_read_mode = TPACPI_THERMAL_NONE;
2916 } else {
2917 thermal_read_mode =
2918 (ta2 != 0) ?
2919 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2921 } else if (acpi_tmp7) {
2922 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2923 /* 600e/x, 770e, 770x */
2924 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2925 } else {
2926 /* Standard ACPI TMPx access, max 8 sensors */
2927 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2929 } else {
2930 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2931 thermal_read_mode = TPACPI_THERMAL_NONE;
2934 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2935 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2936 thermal_read_mode);
2938 switch(thermal_read_mode) {
2939 case TPACPI_THERMAL_TPEC_16:
2940 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
2941 &thermal_temp_input16_group);
2942 if (res)
2943 return res;
2944 break;
2945 case TPACPI_THERMAL_TPEC_8:
2946 case TPACPI_THERMAL_ACPI_TMP07:
2947 case TPACPI_THERMAL_ACPI_UPDT:
2948 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
2949 &thermal_temp_input8_group);
2950 if (res)
2951 return res;
2952 break;
2953 case TPACPI_THERMAL_NONE:
2954 default:
2955 return 1;
2958 return 0;
2961 static void thermal_exit(void)
2963 switch(thermal_read_mode) {
2964 case TPACPI_THERMAL_TPEC_16:
2965 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
2966 &thermal_temp_input16_group);
2967 break;
2968 case TPACPI_THERMAL_TPEC_8:
2969 case TPACPI_THERMAL_ACPI_TMP07:
2970 case TPACPI_THERMAL_ACPI_UPDT:
2971 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
2972 &thermal_temp_input16_group);
2973 break;
2974 case TPACPI_THERMAL_NONE:
2975 default:
2976 break;
2980 /* idx is zero-based */
2981 static int thermal_get_sensor(int idx, s32 *value)
2983 int t;
2984 s8 tmp;
2985 char tmpi[5];
2987 t = TP_EC_THERMAL_TMP0;
2989 switch (thermal_read_mode) {
2990 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2991 case TPACPI_THERMAL_TPEC_16:
2992 if (idx >= 8 && idx <= 15) {
2993 t = TP_EC_THERMAL_TMP8;
2994 idx -= 8;
2996 /* fallthrough */
2997 #endif
2998 case TPACPI_THERMAL_TPEC_8:
2999 if (idx <= 7) {
3000 if (!acpi_ec_read(t + idx, &tmp))
3001 return -EIO;
3002 *value = tmp * 1000;
3003 return 0;
3005 break;
3007 case TPACPI_THERMAL_ACPI_UPDT:
3008 if (idx <= 7) {
3009 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3010 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3011 return -EIO;
3012 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3013 return -EIO;
3014 *value = (t - 2732) * 100;
3015 return 0;
3017 break;
3019 case TPACPI_THERMAL_ACPI_TMP07:
3020 if (idx <= 7) {
3021 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3022 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3023 return -EIO;
3024 if (t > 127 || t < -127)
3025 t = TP_EC_THERMAL_TMP_NA;
3026 *value = t * 1000;
3027 return 0;
3029 break;
3031 case TPACPI_THERMAL_NONE:
3032 default:
3033 return -ENOSYS;
3036 return -EINVAL;
3039 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3041 int res, i;
3042 int n;
3044 n = 8;
3045 i = 0;
3047 if (!s)
3048 return -EINVAL;
3050 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3051 n = 16;
3053 for(i = 0 ; i < n; i++) {
3054 res = thermal_get_sensor(i, &s->temp[i]);
3055 if (res)
3056 return res;
3059 return n;
3062 static int thermal_read(char *p)
3064 int len = 0;
3065 int n, i;
3066 struct ibm_thermal_sensors_struct t;
3068 n = thermal_get_sensors(&t);
3069 if (unlikely(n < 0))
3070 return n;
3072 len += sprintf(p + len, "temperatures:\t");
3074 if (n > 0) {
3075 for (i = 0; i < (n - 1); i++)
3076 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3077 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3078 } else
3079 len += sprintf(p + len, "not supported\n");
3081 return len;
3084 static struct ibm_struct thermal_driver_data = {
3085 .name = "thermal",
3086 .read = thermal_read,
3087 .exit = thermal_exit,
3090 /*************************************************************************
3091 * EC Dump subdriver
3094 static u8 ecdump_regs[256];
3096 static int ecdump_read(char *p)
3098 int len = 0;
3099 int i, j;
3100 u8 v;
3102 len += sprintf(p + len, "EC "
3103 " +00 +01 +02 +03 +04 +05 +06 +07"
3104 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3105 for (i = 0; i < 256; i += 16) {
3106 len += sprintf(p + len, "EC 0x%02x:", i);
3107 for (j = 0; j < 16; j++) {
3108 if (!acpi_ec_read(i + j, &v))
3109 break;
3110 if (v != ecdump_regs[i + j])
3111 len += sprintf(p + len, " *%02x", v);
3112 else
3113 len += sprintf(p + len, " %02x", v);
3114 ecdump_regs[i + j] = v;
3116 len += sprintf(p + len, "\n");
3117 if (j != 16)
3118 break;
3121 /* These are way too dangerous to advertise openly... */
3122 #if 0
3123 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3124 " (<offset> is 00-ff, <value> is 00-ff)\n");
3125 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3126 " (<offset> is 00-ff, <value> is 0-255)\n");
3127 #endif
3128 return len;
3131 static int ecdump_write(char *buf)
3133 char *cmd;
3134 int i, v;
3136 while ((cmd = next_cmd(&buf))) {
3137 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3138 /* i and v set */
3139 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3140 /* i and v set */
3141 } else
3142 return -EINVAL;
3143 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3144 if (!acpi_ec_write(i, v))
3145 return -EIO;
3146 } else
3147 return -EINVAL;
3150 return 0;
3153 static struct ibm_struct ecdump_driver_data = {
3154 .name = "ecdump",
3155 .read = ecdump_read,
3156 .write = ecdump_write,
3157 .flags.experimental = 1,
3160 /*************************************************************************
3161 * Backlight/brightness subdriver
3164 static struct backlight_device *ibm_backlight_device;
3166 static struct backlight_ops ibm_backlight_data = {
3167 .get_brightness = brightness_get,
3168 .update_status = brightness_update_status,
3171 static struct mutex brightness_mutex;
3173 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
3175 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
3176 union acpi_object *obj;
3177 int rc;
3179 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
3180 obj = (union acpi_object *)buffer.pointer;
3181 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
3182 printk(IBM_ERR "Unknown BCLL data, "
3183 "please report this to %s\n", IBM_MAIL);
3184 rc = 0;
3185 } else {
3186 rc = obj->package.count;
3188 } else {
3189 return 0;
3192 kfree(buffer.pointer);
3193 return rc;
3196 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
3197 void *context, void **rv)
3199 char name[ACPI_PATH_SEGMENT_LENGTH];
3200 struct acpi_buffer buffer = { sizeof(name), &name };
3202 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
3203 !strncmp("BCLL", name, sizeof(name) - 1)) {
3204 if (tpacpi_query_bcll_levels(handle) == 16) {
3205 *rv = handle;
3206 return AE_CTRL_TERMINATE;
3207 } else {
3208 return AE_OK;
3210 } else {
3211 return AE_OK;
3215 static int __init brightness_check_levels(void)
3217 int status;
3218 void *found_node = NULL;
3220 if (!vid_handle) {
3221 IBM_ACPIHANDLE_INIT(vid);
3223 if (!vid_handle)
3224 return 0;
3226 /* Search for a BCLL package with 16 levels */
3227 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
3228 brightness_find_bcll, NULL, &found_node);
3230 return (ACPI_SUCCESS(status) && found_node != NULL);
3233 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
3234 void *context, void **rv)
3236 char name[ACPI_PATH_SEGMENT_LENGTH];
3237 struct acpi_buffer buffer = { sizeof(name), &name };
3239 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
3240 !strncmp("_BCL", name, sizeof(name) - 1)) {
3241 *rv = handle;
3242 return AE_CTRL_TERMINATE;
3243 } else {
3244 return AE_OK;
3248 static int __init brightness_check_std_acpi_support(void)
3250 int status;
3251 void *found_node = NULL;
3253 if (!vid_handle) {
3254 IBM_ACPIHANDLE_INIT(vid);
3256 if (!vid_handle)
3257 return 0;
3259 /* Search for a _BCL method, but don't execute it */
3260 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
3261 brightness_find_bcl, NULL, &found_node);
3263 return (ACPI_SUCCESS(status) && found_node != NULL);
3266 static int __init brightness_init(struct ibm_init_struct *iibm)
3268 int b;
3270 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3272 mutex_init(&brightness_mutex);
3274 if (!brightness_enable) {
3275 dbg_printk(TPACPI_DBG_INIT,
3276 "brightness support disabled by module parameter\n");
3277 return 1;
3278 } else if (brightness_enable > 1) {
3279 if (brightness_check_std_acpi_support()) {
3280 printk(IBM_NOTICE
3281 "standard ACPI backlight interface available, not loading native one...\n");
3282 return 1;
3286 if (!brightness_mode) {
3287 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3288 brightness_mode = 2;
3289 else
3290 brightness_mode = 3;
3292 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3293 brightness_mode);
3296 if (brightness_mode > 3)
3297 return -EINVAL;
3299 tp_features.bright_16levels =
3300 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
3301 brightness_check_levels();
3303 b = brightness_get(NULL);
3304 if (b < 0)
3305 return 1;
3307 if (tp_features.bright_16levels)
3308 printk(IBM_INFO "detected a 16-level brightness capable ThinkPad\n");
3310 ibm_backlight_device = backlight_device_register(
3311 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3312 &ibm_backlight_data);
3313 if (IS_ERR(ibm_backlight_device)) {
3314 printk(IBM_ERR "Could not register backlight device\n");
3315 return PTR_ERR(ibm_backlight_device);
3317 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
3319 ibm_backlight_device->props.max_brightness =
3320 (tp_features.bright_16levels)? 15 : 7;
3321 ibm_backlight_device->props.brightness = b;
3322 backlight_update_status(ibm_backlight_device);
3324 return 0;
3327 static void brightness_exit(void)
3329 if (ibm_backlight_device) {
3330 vdbg_printk(TPACPI_DBG_EXIT,
3331 "calling backlight_device_unregister()\n");
3332 backlight_device_unregister(ibm_backlight_device);
3333 ibm_backlight_device = NULL;
3337 static int brightness_update_status(struct backlight_device *bd)
3339 /* it is the backlight class's job (caller) to handle
3340 * EINTR and other errors properly */
3341 return brightness_set(
3342 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3343 bd->props.power == FB_BLANK_UNBLANK) ?
3344 bd->props.brightness : 0);
3348 * ThinkPads can read brightness from two places: EC 0x31, or
3349 * CMOS NVRAM byte 0x5E, bits 0-3.
3351 static int brightness_get(struct backlight_device *bd)
3353 u8 lec = 0, lcmos = 0, level = 0;
3355 if (brightness_mode & 1) {
3356 if (!acpi_ec_read(brightness_offset, &lec))
3357 return -EIO;
3358 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
3359 level = lec;
3361 if (brightness_mode & 2) {
3362 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3363 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3364 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3365 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
3366 level = lcmos;
3369 if (brightness_mode == 3 && lec != lcmos) {
3370 printk(IBM_ERR
3371 "CMOS NVRAM (%u) and EC (%u) do not agree "
3372 "on display brightness level\n",
3373 (unsigned int) lcmos,
3374 (unsigned int) lec);
3375 return -EIO;
3378 return level;
3381 /* May return EINTR which can always be mapped to ERESTARTSYS */
3382 static int brightness_set(int value)
3384 int cmos_cmd, inc, i, res;
3385 int current_value;
3387 if (value > ((tp_features.bright_16levels)? 15 : 7))
3388 return -EINVAL;
3390 res = mutex_lock_interruptible(&brightness_mutex);
3391 if (res < 0)
3392 return res;
3394 current_value = brightness_get(NULL);
3395 if (current_value < 0) {
3396 res = current_value;
3397 goto errout;
3400 cmos_cmd = value > current_value ?
3401 TP_CMOS_BRIGHTNESS_UP :
3402 TP_CMOS_BRIGHTNESS_DOWN;
3403 inc = (value > current_value)? 1 : -1;
3405 res = 0;
3406 for (i = current_value; i != value; i += inc) {
3407 if ((brightness_mode & 2) &&
3408 issue_thinkpad_cmos_command(cmos_cmd)) {
3409 res = -EIO;
3410 goto errout;
3412 if ((brightness_mode & 1) &&
3413 !acpi_ec_write(brightness_offset, i + inc)) {
3414 res = -EIO;
3415 goto errout;;
3419 errout:
3420 mutex_unlock(&brightness_mutex);
3421 return res;
3424 static int brightness_read(char *p)
3426 int len = 0;
3427 int level;
3429 if ((level = brightness_get(NULL)) < 0) {
3430 len += sprintf(p + len, "level:\t\tunreadable\n");
3431 } else {
3432 len += sprintf(p + len, "level:\t\t%d\n", level);
3433 len += sprintf(p + len, "commands:\tup, down\n");
3434 len += sprintf(p + len, "commands:\tlevel <level>"
3435 " (<level> is 0-%d)\n",
3436 (tp_features.bright_16levels) ? 15 : 7);
3439 return len;
3442 static int brightness_write(char *buf)
3444 int level;
3445 int rc;
3446 char *cmd;
3447 int max_level = (tp_features.bright_16levels) ? 15 : 7;
3449 level = brightness_get(NULL);
3450 if (level < 0)
3451 return level;
3453 while ((cmd = next_cmd(&buf))) {
3454 if (strlencmp(cmd, "up") == 0) {
3455 if (level < max_level)
3456 level++;
3457 } else if (strlencmp(cmd, "down") == 0) {
3458 if (level > 0)
3459 level--;
3460 } else if (sscanf(cmd, "level %d", &level) == 1 &&
3461 level >= 0 && level <= max_level) {
3462 /* new level set */
3463 } else
3464 return -EINVAL;
3468 * Now we know what the final level should be, so we try to set it.
3469 * Doing it this way makes the syscall restartable in case of EINTR
3471 rc = brightness_set(level);
3472 return (rc == -EINTR)? ERESTARTSYS : rc;
3475 static struct ibm_struct brightness_driver_data = {
3476 .name = "brightness",
3477 .read = brightness_read,
3478 .write = brightness_write,
3479 .exit = brightness_exit,
3482 /*************************************************************************
3483 * Volume subdriver
3486 static int volume_read(char *p)
3488 int len = 0;
3489 u8 level;
3491 if (!acpi_ec_read(volume_offset, &level)) {
3492 len += sprintf(p + len, "level:\t\tunreadable\n");
3493 } else {
3494 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3495 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3496 len += sprintf(p + len, "commands:\tup, down, mute\n");
3497 len += sprintf(p + len, "commands:\tlevel <level>"
3498 " (<level> is 0-15)\n");
3501 return len;
3504 static int volume_write(char *buf)
3506 int cmos_cmd, inc, i;
3507 u8 level, mute;
3508 int new_level, new_mute;
3509 char *cmd;
3511 while ((cmd = next_cmd(&buf))) {
3512 if (!acpi_ec_read(volume_offset, &level))
3513 return -EIO;
3514 new_mute = mute = level & 0x40;
3515 new_level = level = level & 0xf;
3517 if (strlencmp(cmd, "up") == 0) {
3518 if (mute)
3519 new_mute = 0;
3520 else
3521 new_level = level == 15 ? 15 : level + 1;
3522 } else if (strlencmp(cmd, "down") == 0) {
3523 if (mute)
3524 new_mute = 0;
3525 else
3526 new_level = level == 0 ? 0 : level - 1;
3527 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3528 new_level >= 0 && new_level <= 15) {
3529 /* new_level set */
3530 } else if (strlencmp(cmd, "mute") == 0) {
3531 new_mute = 0x40;
3532 } else
3533 return -EINVAL;
3535 if (new_level != level) { /* mute doesn't change */
3536 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3537 inc = new_level > level ? 1 : -1;
3539 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3540 !acpi_ec_write(volume_offset, level)))
3541 return -EIO;
3543 for (i = level; i != new_level; i += inc)
3544 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3545 !acpi_ec_write(volume_offset, i + inc))
3546 return -EIO;
3548 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3549 !acpi_ec_write(volume_offset,
3550 new_level + mute)))
3551 return -EIO;
3554 if (new_mute != mute) { /* level doesn't change */
3555 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3557 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3558 !acpi_ec_write(volume_offset, level + new_mute))
3559 return -EIO;
3563 return 0;
3566 static struct ibm_struct volume_driver_data = {
3567 .name = "volume",
3568 .read = volume_read,
3569 .write = volume_write,
3572 /*************************************************************************
3573 * Fan subdriver
3577 * FAN ACCESS MODES
3579 * TPACPI_FAN_RD_ACPI_GFAN:
3580 * ACPI GFAN method: returns fan level
3582 * see TPACPI_FAN_WR_ACPI_SFAN
3583 * EC 0x2f (HFSP) not available if GFAN exists
3585 * TPACPI_FAN_WR_ACPI_SFAN:
3586 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3588 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3589 * it for writing.
3591 * TPACPI_FAN_WR_TPEC:
3592 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3593 * Supported on almost all ThinkPads
3595 * Fan speed changes of any sort (including those caused by the
3596 * disengaged mode) are usually done slowly by the firmware as the
3597 * maximum ammount of fan duty cycle change per second seems to be
3598 * limited.
3600 * Reading is not available if GFAN exists.
3601 * Writing is not available if SFAN exists.
3603 * Bits
3604 * 7 automatic mode engaged;
3605 * (default operation mode of the ThinkPad)
3606 * fan level is ignored in this mode.
3607 * 6 full speed mode (takes precedence over bit 7);
3608 * not available on all thinkpads. May disable
3609 * the tachometer while the fan controller ramps up
3610 * the speed (which can take up to a few *minutes*).
3611 * Speeds up fan to 100% duty-cycle, which is far above
3612 * the standard RPM levels. It is not impossible that
3613 * it could cause hardware damage.
3614 * 5-3 unused in some models. Extra bits for fan level
3615 * in others, but still useless as all values above
3616 * 7 map to the same speed as level 7 in these models.
3617 * 2-0 fan level (0..7 usually)
3618 * 0x00 = stop
3619 * 0x07 = max (set when temperatures critical)
3620 * Some ThinkPads may have other levels, see
3621 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3623 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3624 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3625 * does so, its initial value is meaningless (0x07).
3627 * For firmware bugs, refer to:
3628 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3630 * ----
3632 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3633 * Main fan tachometer reading (in RPM)
3635 * This register is present on all ThinkPads with a new-style EC, and
3636 * it is known not to be present on the A21m/e, and T22, as there is
3637 * something else in offset 0x84 according to the ACPI DSDT. Other
3638 * ThinkPads from this same time period (and earlier) probably lack the
3639 * tachometer as well.
3641 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3642 * was never fixed by IBM to report the EC firmware version string
3643 * probably support the tachometer (like the early X models), so
3644 * detecting it is quite hard. We need more data to know for sure.
3646 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3647 * might result.
3649 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3650 * mode.
3652 * For firmware bugs, refer to:
3653 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3655 * TPACPI_FAN_WR_ACPI_FANS:
3656 * ThinkPad X31, X40, X41. Not available in the X60.
3658 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3659 * high speed. ACPI DSDT seems to map these three speeds to levels
3660 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3661 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3663 * The speeds are stored on handles
3664 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3666 * There are three default speed sets, acessible as handles:
3667 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3669 * ACPI DSDT switches which set is in use depending on various
3670 * factors.
3672 * TPACPI_FAN_WR_TPEC is also available and should be used to
3673 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3674 * but the ACPI tables just mention level 7.
3677 static enum fan_status_access_mode fan_status_access_mode;
3678 static enum fan_control_access_mode fan_control_access_mode;
3679 static enum fan_control_commands fan_control_commands;
3681 static u8 fan_control_initial_status;
3682 static u8 fan_control_desired_level;
3684 static void fan_watchdog_fire(struct work_struct *ignored);
3685 static int fan_watchdog_maxinterval;
3686 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3688 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3689 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3690 "\\FSPD", /* 600e/x, 770e, 770x */
3691 ); /* all others */
3692 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3693 "JFNS", /* 770x-JL */
3694 ); /* all others */
3697 * SYSFS fan layout: hwmon compatible (device)
3699 * pwm*_enable:
3700 * 0: "disengaged" mode
3701 * 1: manual mode
3702 * 2: native EC "auto" mode (recommended, hardware default)
3704 * pwm*: set speed in manual mode, ignored otherwise.
3705 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3706 * interpolation.
3708 * fan*_input: tachometer reading, RPM
3711 * SYSFS fan layout: extensions
3713 * fan_watchdog (driver):
3714 * fan watchdog interval in seconds, 0 disables (default), max 120
3717 /* sysfs fan pwm1_enable ----------------------------------------------- */
3718 static ssize_t fan_pwm1_enable_show(struct device *dev,
3719 struct device_attribute *attr,
3720 char *buf)
3722 int res, mode;
3723 u8 status;
3725 res = fan_get_status_safe(&status);
3726 if (res)
3727 return res;
3729 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3730 if (status != fan_control_initial_status) {
3731 tp_features.fan_ctrl_status_undef = 0;
3732 } else {
3733 /* Return most likely status. In fact, it
3734 * might be the only possible status */
3735 status = TP_EC_FAN_AUTO;
3739 if (status & TP_EC_FAN_FULLSPEED) {
3740 mode = 0;
3741 } else if (status & TP_EC_FAN_AUTO) {
3742 mode = 2;
3743 } else
3744 mode = 1;
3746 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3749 static ssize_t fan_pwm1_enable_store(struct device *dev,
3750 struct device_attribute *attr,
3751 const char *buf, size_t count)
3753 unsigned long t;
3754 int res, level;
3756 if (parse_strtoul(buf, 2, &t))
3757 return -EINVAL;
3759 switch (t) {
3760 case 0:
3761 level = TP_EC_FAN_FULLSPEED;
3762 break;
3763 case 1:
3764 level = TPACPI_FAN_LAST_LEVEL;
3765 break;
3766 case 2:
3767 level = TP_EC_FAN_AUTO;
3768 break;
3769 case 3:
3770 /* reserved for software-controlled auto mode */
3771 return -ENOSYS;
3772 default:
3773 return -EINVAL;
3776 res = fan_set_level_safe(level);
3777 if (res == -ENXIO)
3778 return -EINVAL;
3779 else if (res < 0)
3780 return res;
3782 fan_watchdog_reset();
3784 return count;
3787 static struct device_attribute dev_attr_fan_pwm1_enable =
3788 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3789 fan_pwm1_enable_show, fan_pwm1_enable_store);
3791 /* sysfs fan pwm1 ------------------------------------------------------ */
3792 static ssize_t fan_pwm1_show(struct device *dev,
3793 struct device_attribute *attr,
3794 char *buf)
3796 int res;
3797 u8 status;
3799 res = fan_get_status_safe(&status);
3800 if (res)
3801 return res;
3803 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3804 if (status != fan_control_initial_status) {
3805 tp_features.fan_ctrl_status_undef = 0;
3806 } else {
3807 status = TP_EC_FAN_AUTO;
3811 if ((status &
3812 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3813 status = fan_control_desired_level;
3815 if (status > 7)
3816 status = 7;
3818 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3821 static ssize_t fan_pwm1_store(struct device *dev,
3822 struct device_attribute *attr,
3823 const char *buf, size_t count)
3825 unsigned long s;
3826 int rc;
3827 u8 status, newlevel;
3829 if (parse_strtoul(buf, 255, &s))
3830 return -EINVAL;
3832 /* scale down from 0-255 to 0-7 */
3833 newlevel = (s >> 5) & 0x07;
3835 if (mutex_lock_interruptible(&fan_mutex))
3836 return -ERESTARTSYS;
3838 rc = fan_get_status(&status);
3839 if (!rc && (status &
3840 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3841 rc = fan_set_level(newlevel);
3842 if (rc == -ENXIO)
3843 rc = -EINVAL;
3844 else if (!rc) {
3845 fan_update_desired_level(newlevel);
3846 fan_watchdog_reset();
3850 mutex_unlock(&fan_mutex);
3851 return (rc)? rc : count;
3854 static struct device_attribute dev_attr_fan_pwm1 =
3855 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3856 fan_pwm1_show, fan_pwm1_store);
3858 /* sysfs fan fan1_input ------------------------------------------------ */
3859 static ssize_t fan_fan1_input_show(struct device *dev,
3860 struct device_attribute *attr,
3861 char *buf)
3863 int res;
3864 unsigned int speed;
3866 res = fan_get_speed(&speed);
3867 if (res < 0)
3868 return res;
3870 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3873 static struct device_attribute dev_attr_fan_fan1_input =
3874 __ATTR(fan1_input, S_IRUGO,
3875 fan_fan1_input_show, NULL);
3877 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
3878 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3879 char *buf)
3881 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3884 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3885 const char *buf, size_t count)
3887 unsigned long t;
3889 if (parse_strtoul(buf, 120, &t))
3890 return -EINVAL;
3892 if (!fan_control_allowed)
3893 return -EPERM;
3895 fan_watchdog_maxinterval = t;
3896 fan_watchdog_reset();
3898 return count;
3901 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3902 fan_fan_watchdog_show, fan_fan_watchdog_store);
3904 /* --------------------------------------------------------------------- */
3905 static struct attribute *fan_attributes[] = {
3906 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3907 &dev_attr_fan_fan1_input.attr,
3908 NULL
3911 static const struct attribute_group fan_attr_group = {
3912 .attrs = fan_attributes,
3915 static int __init fan_init(struct ibm_init_struct *iibm)
3917 int rc;
3919 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3921 mutex_init(&fan_mutex);
3922 fan_status_access_mode = TPACPI_FAN_NONE;
3923 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3924 fan_control_commands = 0;
3925 fan_watchdog_maxinterval = 0;
3926 tp_features.fan_ctrl_status_undef = 0;
3927 fan_control_desired_level = 7;
3929 IBM_ACPIHANDLE_INIT(fans);
3930 IBM_ACPIHANDLE_INIT(gfan);
3931 IBM_ACPIHANDLE_INIT(sfan);
3933 if (gfan_handle) {
3934 /* 570, 600e/x, 770e, 770x */
3935 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3936 } else {
3937 /* all other ThinkPads: note that even old-style
3938 * ThinkPad ECs supports the fan control register */
3939 if (likely(acpi_ec_read(fan_status_offset,
3940 &fan_control_initial_status))) {
3941 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3943 /* In some ThinkPads, neither the EC nor the ACPI
3944 * DSDT initialize the fan status, and it ends up
3945 * being set to 0x07 when it *could* be either
3946 * 0x07 or 0x80.
3948 * Enable for TP-1Y (T43), TP-78 (R51e),
3949 * TP-76 (R52), TP-70 (T43, R52), which are known
3950 * to be buggy. */
3951 if (fan_control_initial_status == 0x07) {
3952 switch (thinkpad_id.ec_model) {
3953 case 0x5931: /* TP-1Y */
3954 case 0x3837: /* TP-78 */
3955 case 0x3637: /* TP-76 */
3956 case 0x3037: /* TP-70 */
3957 printk(IBM_NOTICE
3958 "fan_init: initial fan status is "
3959 "unknown, assuming it is in auto "
3960 "mode\n");
3961 tp_features.fan_ctrl_status_undef = 1;
3965 } else {
3966 printk(IBM_ERR
3967 "ThinkPad ACPI EC access misbehaving, "
3968 "fan status and control unavailable\n");
3969 return 1;
3973 if (sfan_handle) {
3974 /* 570, 770x-JL */
3975 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3976 fan_control_commands |=
3977 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3978 } else {
3979 if (!gfan_handle) {
3980 /* gfan without sfan means no fan control */
3981 /* all other models implement TP EC 0x2f control */
3983 if (fans_handle) {
3984 /* X31, X40, X41 */
3985 fan_control_access_mode =
3986 TPACPI_FAN_WR_ACPI_FANS;
3987 fan_control_commands |=
3988 TPACPI_FAN_CMD_SPEED |
3989 TPACPI_FAN_CMD_LEVEL |
3990 TPACPI_FAN_CMD_ENABLE;
3991 } else {
3992 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3993 fan_control_commands |=
3994 TPACPI_FAN_CMD_LEVEL |
3995 TPACPI_FAN_CMD_ENABLE;
4000 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
4001 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
4002 fan_control_access_mode != TPACPI_FAN_WR_NONE),
4003 fan_status_access_mode, fan_control_access_mode);
4005 /* fan control master switch */
4006 if (!fan_control_allowed) {
4007 fan_control_access_mode = TPACPI_FAN_WR_NONE;
4008 fan_control_commands = 0;
4009 dbg_printk(TPACPI_DBG_INIT,
4010 "fan control features disabled by parameter\n");
4013 /* update fan_control_desired_level */
4014 if (fan_status_access_mode != TPACPI_FAN_NONE)
4015 fan_get_status_safe(NULL);
4017 if (fan_status_access_mode != TPACPI_FAN_NONE ||
4018 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
4019 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4020 &fan_attr_group);
4021 if (!(rc < 0))
4022 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
4023 &driver_attr_fan_watchdog);
4024 if (rc < 0)
4025 return rc;
4026 return 0;
4027 } else
4028 return 1;
4032 * Call with fan_mutex held
4034 static void fan_update_desired_level(u8 status)
4036 if ((status &
4037 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4038 if (status > 7)
4039 fan_control_desired_level = 7;
4040 else
4041 fan_control_desired_level = status;
4045 static int fan_get_status(u8 *status)
4047 u8 s;
4049 /* TODO:
4050 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4052 switch (fan_status_access_mode) {
4053 case TPACPI_FAN_RD_ACPI_GFAN:
4054 /* 570, 600e/x, 770e, 770x */
4056 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4057 return -EIO;
4059 if (likely(status))
4060 *status = s & 0x07;
4062 break;
4064 case TPACPI_FAN_RD_TPEC:
4065 /* all except 570, 600e/x, 770e, 770x */
4066 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4067 return -EIO;
4069 if (likely(status))
4070 *status = s;
4072 break;
4074 default:
4075 return -ENXIO;
4078 return 0;
4081 static int fan_get_status_safe(u8 *status)
4083 int rc;
4084 u8 s;
4086 if (mutex_lock_interruptible(&fan_mutex))
4087 return -ERESTARTSYS;
4088 rc = fan_get_status(&s);
4089 if (!rc)
4090 fan_update_desired_level(s);
4091 mutex_unlock(&fan_mutex);
4093 if (status)
4094 *status = s;
4096 return rc;
4099 static void fan_exit(void)
4101 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
4103 /* FIXME: can we really do this unconditionally? */
4104 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
4105 driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
4107 cancel_delayed_work(&fan_watchdog_task);
4108 flush_scheduled_work();
4111 static int fan_get_speed(unsigned int *speed)
4113 u8 hi, lo;
4115 switch (fan_status_access_mode) {
4116 case TPACPI_FAN_RD_TPEC:
4117 /* all except 570, 600e/x, 770e, 770x */
4118 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4119 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4120 return -EIO;
4122 if (likely(speed))
4123 *speed = (hi << 8) | lo;
4125 break;
4127 default:
4128 return -ENXIO;
4131 return 0;
4134 static void fan_watchdog_fire(struct work_struct *ignored)
4136 int rc;
4138 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4139 return;
4141 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
4142 rc = fan_set_enable();
4143 if (rc < 0) {
4144 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
4145 "will try again later...\n", -rc);
4146 /* reschedule for later */
4147 fan_watchdog_reset();
4151 static void fan_watchdog_reset(void)
4153 static int fan_watchdog_active;
4155 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4156 return;
4158 if (fan_watchdog_active)
4159 cancel_delayed_work(&fan_watchdog_task);
4161 if (fan_watchdog_maxinterval > 0 &&
4162 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4163 fan_watchdog_active = 1;
4164 if (!schedule_delayed_work(&fan_watchdog_task,
4165 msecs_to_jiffies(fan_watchdog_maxinterval
4166 * 1000))) {
4167 printk(IBM_ERR "failed to schedule the fan watchdog, "
4168 "watchdog will not trigger\n");
4170 } else
4171 fan_watchdog_active = 0;
4174 static int fan_set_level(int level)
4176 if (!fan_control_allowed)
4177 return -EPERM;
4179 switch (fan_control_access_mode) {
4180 case TPACPI_FAN_WR_ACPI_SFAN:
4181 if (level >= 0 && level <= 7) {
4182 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4183 return -EIO;
4184 } else
4185 return -EINVAL;
4186 break;
4188 case TPACPI_FAN_WR_ACPI_FANS:
4189 case TPACPI_FAN_WR_TPEC:
4190 if ((level != TP_EC_FAN_AUTO) &&
4191 (level != TP_EC_FAN_FULLSPEED) &&
4192 ((level < 0) || (level > 7)))
4193 return -EINVAL;
4195 /* safety net should the EC not support AUTO
4196 * or FULLSPEED mode bits and just ignore them */
4197 if (level & TP_EC_FAN_FULLSPEED)
4198 level |= 7; /* safety min speed 7 */
4199 else if (level & TP_EC_FAN_FULLSPEED)
4200 level |= 4; /* safety min speed 4 */
4202 if (!acpi_ec_write(fan_status_offset, level))
4203 return -EIO;
4204 else
4205 tp_features.fan_ctrl_status_undef = 0;
4206 break;
4208 default:
4209 return -ENXIO;
4211 return 0;
4214 static int fan_set_level_safe(int level)
4216 int rc;
4218 if (!fan_control_allowed)
4219 return -EPERM;
4221 if (mutex_lock_interruptible(&fan_mutex))
4222 return -ERESTARTSYS;
4224 if (level == TPACPI_FAN_LAST_LEVEL)
4225 level = fan_control_desired_level;
4227 rc = fan_set_level(level);
4228 if (!rc)
4229 fan_update_desired_level(level);
4231 mutex_unlock(&fan_mutex);
4232 return rc;
4235 static int fan_set_enable(void)
4237 u8 s;
4238 int rc;
4240 if (!fan_control_allowed)
4241 return -EPERM;
4243 if (mutex_lock_interruptible(&fan_mutex))
4244 return -ERESTARTSYS;
4246 switch (fan_control_access_mode) {
4247 case TPACPI_FAN_WR_ACPI_FANS:
4248 case TPACPI_FAN_WR_TPEC:
4249 rc = fan_get_status(&s);
4250 if (rc < 0)
4251 break;
4253 /* Don't go out of emergency fan mode */
4254 if (s != 7) {
4255 s &= 0x07;
4256 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4259 if (!acpi_ec_write(fan_status_offset, s))
4260 rc = -EIO;
4261 else {
4262 tp_features.fan_ctrl_status_undef = 0;
4263 rc = 0;
4265 break;
4267 case TPACPI_FAN_WR_ACPI_SFAN:
4268 rc = fan_get_status(&s);
4269 if (rc < 0)
4270 break;
4272 s &= 0x07;
4274 /* Set fan to at least level 4 */
4275 s |= 4;
4277 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4278 rc= -EIO;
4279 else
4280 rc = 0;
4281 break;
4283 default:
4284 rc = -ENXIO;
4287 mutex_unlock(&fan_mutex);
4288 return rc;
4291 static int fan_set_disable(void)
4293 int rc;
4295 if (!fan_control_allowed)
4296 return -EPERM;
4298 if (mutex_lock_interruptible(&fan_mutex))
4299 return -ERESTARTSYS;
4301 rc = 0;
4302 switch (fan_control_access_mode) {
4303 case TPACPI_FAN_WR_ACPI_FANS:
4304 case TPACPI_FAN_WR_TPEC:
4305 if (!acpi_ec_write(fan_status_offset, 0x00))
4306 rc = -EIO;
4307 else {
4308 fan_control_desired_level = 0;
4309 tp_features.fan_ctrl_status_undef = 0;
4311 break;
4313 case TPACPI_FAN_WR_ACPI_SFAN:
4314 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4315 rc = -EIO;
4316 else
4317 fan_control_desired_level = 0;
4318 break;
4320 default:
4321 rc = -ENXIO;
4325 mutex_unlock(&fan_mutex);
4326 return rc;
4329 static int fan_set_speed(int speed)
4331 int rc;
4333 if (!fan_control_allowed)
4334 return -EPERM;
4336 if (mutex_lock_interruptible(&fan_mutex))
4337 return -ERESTARTSYS;
4339 rc = 0;
4340 switch (fan_control_access_mode) {
4341 case TPACPI_FAN_WR_ACPI_FANS:
4342 if (speed >= 0 && speed <= 65535) {
4343 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4344 speed, speed, speed))
4345 rc = -EIO;
4346 } else
4347 rc = -EINVAL;
4348 break;
4350 default:
4351 rc = -ENXIO;
4354 mutex_unlock(&fan_mutex);
4355 return rc;
4358 static int fan_read(char *p)
4360 int len = 0;
4361 int rc;
4362 u8 status;
4363 unsigned int speed = 0;
4365 switch (fan_status_access_mode) {
4366 case TPACPI_FAN_RD_ACPI_GFAN:
4367 /* 570, 600e/x, 770e, 770x */
4368 if ((rc = fan_get_status_safe(&status)) < 0)
4369 return rc;
4371 len += sprintf(p + len, "status:\t\t%s\n"
4372 "level:\t\t%d\n",
4373 (status != 0) ? "enabled" : "disabled", status);
4374 break;
4376 case TPACPI_FAN_RD_TPEC:
4377 /* all except 570, 600e/x, 770e, 770x */
4378 if ((rc = fan_get_status_safe(&status)) < 0)
4379 return rc;
4381 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4382 if (status != fan_control_initial_status)
4383 tp_features.fan_ctrl_status_undef = 0;
4384 else
4385 /* Return most likely status. In fact, it
4386 * might be the only possible status */
4387 status = TP_EC_FAN_AUTO;
4390 len += sprintf(p + len, "status:\t\t%s\n",
4391 (status != 0) ? "enabled" : "disabled");
4393 if ((rc = fan_get_speed(&speed)) < 0)
4394 return rc;
4396 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4398 if (status & TP_EC_FAN_FULLSPEED)
4399 /* Disengaged mode takes precedence */
4400 len += sprintf(p + len, "level:\t\tdisengaged\n");
4401 else if (status & TP_EC_FAN_AUTO)
4402 len += sprintf(p + len, "level:\t\tauto\n");
4403 else
4404 len += sprintf(p + len, "level:\t\t%d\n", status);
4405 break;
4407 case TPACPI_FAN_NONE:
4408 default:
4409 len += sprintf(p + len, "status:\t\tnot supported\n");
4412 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
4413 len += sprintf(p + len, "commands:\tlevel <level>");
4415 switch (fan_control_access_mode) {
4416 case TPACPI_FAN_WR_ACPI_SFAN:
4417 len += sprintf(p + len, " (<level> is 0-7)\n");
4418 break;
4420 default:
4421 len += sprintf(p + len, " (<level> is 0-7, "
4422 "auto, disengaged, full-speed)\n");
4423 break;
4427 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
4428 len += sprintf(p + len, "commands:\tenable, disable\n"
4429 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4430 "1-120 (seconds))\n");
4432 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
4433 len += sprintf(p + len, "commands:\tspeed <speed>"
4434 " (<speed> is 0-65535)\n");
4436 return len;
4439 static int fan_write_cmd_level(const char *cmd, int *rc)
4441 int level;
4443 if (strlencmp(cmd, "level auto") == 0)
4444 level = TP_EC_FAN_AUTO;
4445 else if ((strlencmp(cmd, "level disengaged") == 0) |
4446 (strlencmp(cmd, "level full-speed") == 0))
4447 level = TP_EC_FAN_FULLSPEED;
4448 else if (sscanf(cmd, "level %d", &level) != 1)
4449 return 0;
4451 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4452 printk(IBM_ERR "level command accepted for unsupported "
4453 "access mode %d", fan_control_access_mode);
4455 return 1;
4458 static int fan_write_cmd_enable(const char *cmd, int *rc)
4460 if (strlencmp(cmd, "enable") != 0)
4461 return 0;
4463 if ((*rc = fan_set_enable()) == -ENXIO)
4464 printk(IBM_ERR "enable command accepted for unsupported "
4465 "access mode %d", fan_control_access_mode);
4467 return 1;
4470 static int fan_write_cmd_disable(const char *cmd, int *rc)
4472 if (strlencmp(cmd, "disable") != 0)
4473 return 0;
4475 if ((*rc = fan_set_disable()) == -ENXIO)
4476 printk(IBM_ERR "disable command accepted for unsupported "
4477 "access mode %d", fan_control_access_mode);
4479 return 1;
4482 static int fan_write_cmd_speed(const char *cmd, int *rc)
4484 int speed;
4486 /* TODO:
4487 * Support speed <low> <medium> <high> ? */
4489 if (sscanf(cmd, "speed %d", &speed) != 1)
4490 return 0;
4492 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4493 printk(IBM_ERR "speed command accepted for unsupported "
4494 "access mode %d", fan_control_access_mode);
4496 return 1;
4499 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4501 int interval;
4503 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4504 return 0;
4506 if (interval < 0 || interval > 120)
4507 *rc = -EINVAL;
4508 else
4509 fan_watchdog_maxinterval = interval;
4511 return 1;
4514 static int fan_write(char *buf)
4516 char *cmd;
4517 int rc = 0;
4519 while (!rc && (cmd = next_cmd(&buf))) {
4520 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4521 fan_write_cmd_level(cmd, &rc)) &&
4522 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4523 (fan_write_cmd_enable(cmd, &rc) ||
4524 fan_write_cmd_disable(cmd, &rc) ||
4525 fan_write_cmd_watchdog(cmd, &rc))) &&
4526 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4527 fan_write_cmd_speed(cmd, &rc))
4529 rc = -EINVAL;
4530 else if (!rc)
4531 fan_watchdog_reset();
4534 return rc;
4537 static struct ibm_struct fan_driver_data = {
4538 .name = "fan",
4539 .read = fan_read,
4540 .write = fan_write,
4541 .exit = fan_exit,
4544 /****************************************************************************
4545 ****************************************************************************
4547 * Infrastructure
4549 ****************************************************************************
4550 ****************************************************************************/
4552 /* sysfs name ---------------------------------------------------------- */
4553 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
4554 struct device_attribute *attr,
4555 char *buf)
4557 return snprintf(buf, PAGE_SIZE, "%s\n", IBM_NAME);
4560 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
4561 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
4563 /* --------------------------------------------------------------------- */
4565 /* /proc support */
4566 static struct proc_dir_entry *proc_dir;
4568 /* Subdriver registry */
4569 static LIST_HEAD(tpacpi_all_drivers);
4573 * Module and infrastructure proble, init and exit handling
4576 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4577 static const char * __init str_supported(int is_supported)
4579 static char text_unsupported[] __initdata = "not supported";
4581 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4583 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4585 static int __init ibm_init(struct ibm_init_struct *iibm)
4587 int ret;
4588 struct ibm_struct *ibm = iibm->data;
4589 struct proc_dir_entry *entry;
4591 BUG_ON(ibm == NULL);
4593 INIT_LIST_HEAD(&ibm->all_drivers);
4595 if (ibm->flags.experimental && !experimental)
4596 return 0;
4598 dbg_printk(TPACPI_DBG_INIT,
4599 "probing for %s\n", ibm->name);
4601 if (iibm->init) {
4602 ret = iibm->init(iibm);
4603 if (ret > 0)
4604 return 0; /* probe failed */
4605 if (ret)
4606 return ret;
4608 ibm->flags.init_called = 1;
4611 if (ibm->acpi) {
4612 if (ibm->acpi->hid) {
4613 ret = register_tpacpi_subdriver(ibm);
4614 if (ret)
4615 goto err_out;
4618 if (ibm->acpi->notify) {
4619 ret = setup_acpi_notify(ibm);
4620 if (ret == -ENODEV) {
4621 printk(IBM_NOTICE "disabling subdriver %s\n",
4622 ibm->name);
4623 ret = 0;
4624 goto err_out;
4626 if (ret < 0)
4627 goto err_out;
4631 dbg_printk(TPACPI_DBG_INIT,
4632 "%s installed\n", ibm->name);
4634 if (ibm->read) {
4635 entry = create_proc_entry(ibm->name,
4636 S_IFREG | S_IRUGO | S_IWUSR,
4637 proc_dir);
4638 if (!entry) {
4639 printk(IBM_ERR "unable to create proc entry %s\n",
4640 ibm->name);
4641 ret = -ENODEV;
4642 goto err_out;
4644 entry->owner = THIS_MODULE;
4645 entry->data = ibm;
4646 entry->read_proc = &dispatch_procfs_read;
4647 if (ibm->write)
4648 entry->write_proc = &dispatch_procfs_write;
4649 ibm->flags.proc_created = 1;
4652 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4654 return 0;
4656 err_out:
4657 dbg_printk(TPACPI_DBG_INIT,
4658 "%s: at error exit path with result %d\n",
4659 ibm->name, ret);
4661 ibm_exit(ibm);
4662 return (ret < 0)? ret : 0;
4665 static void ibm_exit(struct ibm_struct *ibm)
4667 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4669 list_del_init(&ibm->all_drivers);
4671 if (ibm->flags.acpi_notify_installed) {
4672 dbg_printk(TPACPI_DBG_EXIT,
4673 "%s: acpi_remove_notify_handler\n", ibm->name);
4674 BUG_ON(!ibm->acpi);
4675 acpi_remove_notify_handler(*ibm->acpi->handle,
4676 ibm->acpi->type,
4677 dispatch_acpi_notify);
4678 ibm->flags.acpi_notify_installed = 0;
4679 ibm->flags.acpi_notify_installed = 0;
4682 if (ibm->flags.proc_created) {
4683 dbg_printk(TPACPI_DBG_EXIT,
4684 "%s: remove_proc_entry\n", ibm->name);
4685 remove_proc_entry(ibm->name, proc_dir);
4686 ibm->flags.proc_created = 0;
4689 if (ibm->flags.acpi_driver_registered) {
4690 dbg_printk(TPACPI_DBG_EXIT,
4691 "%s: acpi_bus_unregister_driver\n", ibm->name);
4692 BUG_ON(!ibm->acpi);
4693 acpi_bus_unregister_driver(ibm->acpi->driver);
4694 kfree(ibm->acpi->driver);
4695 ibm->acpi->driver = NULL;
4696 ibm->flags.acpi_driver_registered = 0;
4699 if (ibm->flags.init_called && ibm->exit) {
4700 ibm->exit();
4701 ibm->flags.init_called = 0;
4704 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4707 /* Probing */
4709 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
4711 struct dmi_device *dev = NULL;
4712 char ec_fw_string[18];
4714 if (!tp)
4715 return;
4717 memset(tp, 0, sizeof(*tp));
4719 if (dmi_name_in_vendors("IBM"))
4720 tp->vendor = PCI_VENDOR_ID_IBM;
4721 else if (dmi_name_in_vendors("LENOVO"))
4722 tp->vendor = PCI_VENDOR_ID_LENOVO;
4723 else
4724 return;
4726 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4727 GFP_KERNEL);
4728 if (!tp->bios_version_str)
4729 return;
4730 tp->bios_model = tp->bios_version_str[0]
4731 | (tp->bios_version_str[1] << 8);
4734 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4735 * X32 or newer, all Z series; Some models must have an
4736 * up-to-date BIOS or they will not be detected.
4738 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4740 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4741 if (sscanf(dev->name,
4742 "IBM ThinkPad Embedded Controller -[%17c",
4743 ec_fw_string) == 1) {
4744 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4745 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4747 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4748 tp->ec_model = ec_fw_string[0]
4749 | (ec_fw_string[1] << 8);
4750 break;
4754 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4755 GFP_KERNEL);
4756 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4757 kfree(tp->model_str);
4758 tp->model_str = NULL;
4762 static int __init probe_for_thinkpad(void)
4764 int is_thinkpad;
4766 if (acpi_disabled)
4767 return -ENODEV;
4770 * Non-ancient models have better DMI tagging, but very old models
4771 * don't.
4773 is_thinkpad = (thinkpad_id.model_str != NULL);
4775 /* ec is required because many other handles are relative to it */
4776 IBM_ACPIHANDLE_INIT(ec);
4777 if (!ec_handle) {
4778 if (is_thinkpad)
4779 printk(IBM_ERR
4780 "Not yet supported ThinkPad detected!\n");
4781 return -ENODEV;
4785 * Risks a regression on very old machines, but reduces potential
4786 * false positives a damn great deal
4788 if (!is_thinkpad)
4789 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
4791 if (!is_thinkpad && !force_load)
4792 return -ENODEV;
4794 return 0;
4798 /* Module init, exit, parameters */
4800 static struct ibm_init_struct ibms_init[] __initdata = {
4802 .init = thinkpad_acpi_driver_init,
4803 .data = &thinkpad_acpi_driver_data,
4806 .init = hotkey_init,
4807 .data = &hotkey_driver_data,
4810 .init = bluetooth_init,
4811 .data = &bluetooth_driver_data,
4814 .init = wan_init,
4815 .data = &wan_driver_data,
4818 .init = video_init,
4819 .data = &video_driver_data,
4822 .init = light_init,
4823 .data = &light_driver_data,
4825 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4827 .init = dock_init,
4828 .data = &dock_driver_data[0],
4831 .init = dock_init2,
4832 .data = &dock_driver_data[1],
4834 #endif
4835 #ifdef CONFIG_THINKPAD_ACPI_BAY
4837 .init = bay_init,
4838 .data = &bay_driver_data,
4840 #endif
4842 .init = cmos_init,
4843 .data = &cmos_driver_data,
4846 .init = led_init,
4847 .data = &led_driver_data,
4850 .init = beep_init,
4851 .data = &beep_driver_data,
4854 .init = thermal_init,
4855 .data = &thermal_driver_data,
4858 .data = &ecdump_driver_data,
4861 .init = brightness_init,
4862 .data = &brightness_driver_data,
4865 .data = &volume_driver_data,
4868 .init = fan_init,
4869 .data = &fan_driver_data,
4873 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4875 unsigned int i;
4876 struct ibm_struct *ibm;
4878 if (!kp || !kp->name || !val)
4879 return -EINVAL;
4881 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4882 ibm = ibms_init[i].data;
4883 WARN_ON(ibm == NULL);
4885 if (!ibm || !ibm->name)
4886 continue;
4888 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4889 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4890 return -ENOSPC;
4891 strcpy(ibms_init[i].param, val);
4892 strcat(ibms_init[i].param, ",");
4893 return 0;
4897 return -EINVAL;
4900 static int experimental;
4901 module_param(experimental, int, 0);
4903 static u32 dbg_level;
4904 module_param_named(debug, dbg_level, uint, 0);
4906 static int force_load;
4907 module_param(force_load, bool, 0);
4909 static int fan_control_allowed;
4910 module_param_named(fan_control, fan_control_allowed, bool, 0);
4912 static int brightness_mode;
4913 module_param_named(brightness_mode, brightness_mode, int, 0);
4915 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4916 module_param(brightness_enable, uint, 0);
4918 static unsigned int hotkey_report_mode;
4919 module_param(hotkey_report_mode, uint, 0);
4921 #define IBM_PARAM(feature) \
4922 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4924 IBM_PARAM(hotkey);
4925 IBM_PARAM(bluetooth);
4926 IBM_PARAM(video);
4927 IBM_PARAM(light);
4928 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4929 IBM_PARAM(dock);
4930 #endif
4931 #ifdef CONFIG_THINKPAD_ACPI_BAY
4932 IBM_PARAM(bay);
4933 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4934 IBM_PARAM(cmos);
4935 IBM_PARAM(led);
4936 IBM_PARAM(beep);
4937 IBM_PARAM(ecdump);
4938 IBM_PARAM(brightness);
4939 IBM_PARAM(volume);
4940 IBM_PARAM(fan);
4942 static int __init thinkpad_acpi_module_init(void)
4944 int ret, i;
4946 tpacpi_lifecycle = TPACPI_LIFE_INIT;
4948 /* Parameter checking */
4949 if (hotkey_report_mode > 2)
4950 return -EINVAL;
4952 /* Driver-level probe */
4954 get_thinkpad_model_data(&thinkpad_id);
4955 ret = probe_for_thinkpad();
4956 if (ret) {
4957 thinkpad_acpi_module_exit();
4958 return ret;
4961 /* Driver initialization */
4963 IBM_ACPIHANDLE_INIT(ecrd);
4964 IBM_ACPIHANDLE_INIT(ecwr);
4966 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4967 if (!proc_dir) {
4968 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4969 thinkpad_acpi_module_exit();
4970 return -ENODEV;
4972 proc_dir->owner = THIS_MODULE;
4974 ret = platform_driver_register(&tpacpi_pdriver);
4975 if (ret) {
4976 printk(IBM_ERR "unable to register main platform driver\n");
4977 thinkpad_acpi_module_exit();
4978 return ret;
4980 tp_features.platform_drv_registered = 1;
4982 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
4983 if (ret) {
4984 printk(IBM_ERR "unable to register hwmon platform driver\n");
4985 thinkpad_acpi_module_exit();
4986 return ret;
4988 tp_features.sensors_pdrv_registered = 1;
4990 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4991 if (!ret) {
4992 tp_features.platform_drv_attrs_registered = 1;
4993 ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
4995 if (ret) {
4996 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4997 thinkpad_acpi_module_exit();
4998 return ret;
5000 tp_features.sensors_pdrv_attrs_registered = 1;
5003 /* Device initialization */
5004 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
5005 NULL, 0);
5006 if (IS_ERR(tpacpi_pdev)) {
5007 ret = PTR_ERR(tpacpi_pdev);
5008 tpacpi_pdev = NULL;
5009 printk(IBM_ERR "unable to register platform device\n");
5010 thinkpad_acpi_module_exit();
5011 return ret;
5013 tpacpi_sensors_pdev = platform_device_register_simple(
5014 IBM_HWMON_DRVR_NAME,
5015 -1, NULL, 0);
5016 if (IS_ERR(tpacpi_sensors_pdev)) {
5017 ret = PTR_ERR(tpacpi_sensors_pdev);
5018 tpacpi_sensors_pdev = NULL;
5019 printk(IBM_ERR "unable to register hwmon platform device\n");
5020 thinkpad_acpi_module_exit();
5021 return ret;
5023 ret = device_create_file(&tpacpi_sensors_pdev->dev,
5024 &dev_attr_thinkpad_acpi_pdev_name);
5025 if (ret) {
5026 printk(IBM_ERR
5027 "unable to create sysfs hwmon device attributes\n");
5028 thinkpad_acpi_module_exit();
5029 return ret;
5031 tp_features.sensors_pdev_attrs_registered = 1;
5032 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
5033 if (IS_ERR(tpacpi_hwmon)) {
5034 ret = PTR_ERR(tpacpi_hwmon);
5035 tpacpi_hwmon = NULL;
5036 printk(IBM_ERR "unable to register hwmon device\n");
5037 thinkpad_acpi_module_exit();
5038 return ret;
5040 mutex_init(&tpacpi_inputdev_send_mutex);
5041 tpacpi_inputdev = input_allocate_device();
5042 if (!tpacpi_inputdev) {
5043 printk(IBM_ERR "unable to allocate input device\n");
5044 thinkpad_acpi_module_exit();
5045 return -ENOMEM;
5046 } else {
5047 /* Prepare input device, but don't register */
5048 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5049 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
5050 tpacpi_inputdev->id.bustype = BUS_HOST;
5051 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5052 thinkpad_id.vendor :
5053 PCI_VENDOR_ID_IBM;
5054 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5055 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5057 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5058 ret = ibm_init(&ibms_init[i]);
5059 if (ret >= 0 && *ibms_init[i].param)
5060 ret = ibms_init[i].data->write(ibms_init[i].param);
5061 if (ret < 0) {
5062 thinkpad_acpi_module_exit();
5063 return ret;
5066 ret = input_register_device(tpacpi_inputdev);
5067 if (ret < 0) {
5068 printk(IBM_ERR "unable to register input device\n");
5069 thinkpad_acpi_module_exit();
5070 return ret;
5071 } else {
5072 tp_features.input_device_registered = 1;
5075 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
5076 return 0;
5079 static void thinkpad_acpi_module_exit(void)
5081 struct ibm_struct *ibm, *itmp;
5083 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5085 list_for_each_entry_safe_reverse(ibm, itmp,
5086 &tpacpi_all_drivers,
5087 all_drivers) {
5088 ibm_exit(ibm);
5091 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5093 if (tpacpi_inputdev) {
5094 if (tp_features.input_device_registered)
5095 input_unregister_device(tpacpi_inputdev);
5096 else
5097 input_free_device(tpacpi_inputdev);
5100 if (tpacpi_hwmon)
5101 hwmon_device_unregister(tpacpi_hwmon);
5103 if (tp_features.sensors_pdev_attrs_registered)
5104 device_remove_file(&tpacpi_sensors_pdev->dev,
5105 &dev_attr_thinkpad_acpi_pdev_name);
5106 if (tpacpi_sensors_pdev)
5107 platform_device_unregister(tpacpi_sensors_pdev);
5108 if (tpacpi_pdev)
5109 platform_device_unregister(tpacpi_pdev);
5111 if (tp_features.sensors_pdrv_attrs_registered)
5112 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5113 if (tp_features.platform_drv_attrs_registered)
5114 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5116 if (tp_features.sensors_pdrv_registered)
5117 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5119 if (tp_features.platform_drv_registered)
5120 platform_driver_unregister(&tpacpi_pdriver);
5122 if (proc_dir)
5123 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
5125 kfree(thinkpad_id.bios_version_str);
5126 kfree(thinkpad_id.ec_version_str);
5127 kfree(thinkpad_id.model_str);
5130 module_init(thinkpad_acpi_module_init);
5131 module_exit(thinkpad_acpi_module_exit);