ACPI: thinkpad-acpi: refactor hotkey_get and hotkey_set (v2)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob49d4f4af759e4ec3361118c8cb5793f685877d1a
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 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;
780 static u32 hotkey_mask;
782 static u16 *hotkey_keycode_map;
784 static struct attribute_set *hotkey_dev_attributes;
786 static int hotkey_get_wlsw(int *status)
788 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
789 return -EIO;
790 return 0;
794 * Call with hotkey_mutex held
796 static int hotkey_mask_get(void)
798 if (tp_features.hotkey_mask) {
799 if (!acpi_evalf(hkey_handle, &hotkey_mask, "DHKN", "d"))
800 return -EIO;
803 return 0;
807 * Call with hotkey_mutex held
809 static int hotkey_mask_set(u32 mask)
811 int i;
812 int rc = 0;
814 if (tp_features.hotkey_mask) {
815 for (i = 0; i < 32; i++) {
816 u32 m = 1 << i;
817 if (!acpi_evalf(hkey_handle,
818 NULL, "MHKM", "vdd", i + 1,
819 !!(mask & m))) {
820 rc = -EIO;
821 break;
822 } else {
823 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
827 /* hotkey_mask_get must be called unconditionally below */
828 if (!hotkey_mask_get() && !rc && hotkey_mask != mask) {
829 printk(IBM_NOTICE
830 "requested hot key mask 0x%08x, but "
831 "firmware forced it to 0x%08x\n",
832 mask, hotkey_mask);
836 return rc;
839 static int hotkey_status_get(int *status)
841 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
842 return -EIO;
844 return 0;
847 static int hotkey_status_set(int status)
849 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
850 return -EIO;
852 return 0;
855 /* sysfs hotkey enable ------------------------------------------------- */
856 static ssize_t hotkey_enable_show(struct device *dev,
857 struct device_attribute *attr,
858 char *buf)
860 int res, status;
862 res = hotkey_status_get(&status);
863 if (res)
864 return res;
866 return snprintf(buf, PAGE_SIZE, "%d\n", status);
869 static ssize_t hotkey_enable_store(struct device *dev,
870 struct device_attribute *attr,
871 const char *buf, size_t count)
873 unsigned long t;
874 int res;
876 if (parse_strtoul(buf, 1, &t))
877 return -EINVAL;
879 res = hotkey_status_set(t);
881 return (res) ? res : count;
884 static struct device_attribute dev_attr_hotkey_enable =
885 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
886 hotkey_enable_show, hotkey_enable_store);
888 /* sysfs hotkey mask --------------------------------------------------- */
889 static ssize_t hotkey_mask_show(struct device *dev,
890 struct device_attribute *attr,
891 char *buf)
893 int res;
895 if (mutex_lock_interruptible(&hotkey_mutex))
896 return -ERESTARTSYS;
897 res = hotkey_mask_get();
898 mutex_unlock(&hotkey_mutex);
900 return (res)?
901 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
904 static ssize_t hotkey_mask_store(struct device *dev,
905 struct device_attribute *attr,
906 const char *buf, size_t count)
908 unsigned long t;
909 int res;
911 if (parse_strtoul(buf, 0xffffffffUL, &t))
912 return -EINVAL;
914 if (mutex_lock_interruptible(&hotkey_mutex))
915 return -ERESTARTSYS;
917 res = hotkey_mask_set(t);
918 mutex_unlock(&hotkey_mutex);
920 return (res) ? res : count;
923 static struct device_attribute dev_attr_hotkey_mask =
924 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
925 hotkey_mask_show, hotkey_mask_store);
927 /* sysfs hotkey bios_enabled ------------------------------------------- */
928 static ssize_t hotkey_bios_enabled_show(struct device *dev,
929 struct device_attribute *attr,
930 char *buf)
932 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
935 static struct device_attribute dev_attr_hotkey_bios_enabled =
936 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
938 /* sysfs hotkey bios_mask ---------------------------------------------- */
939 static ssize_t hotkey_bios_mask_show(struct device *dev,
940 struct device_attribute *attr,
941 char *buf)
943 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
946 static struct device_attribute dev_attr_hotkey_bios_mask =
947 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
949 /* sysfs hotkey all_mask ----------------------------------------------- */
950 static ssize_t hotkey_all_mask_show(struct device *dev,
951 struct device_attribute *attr,
952 char *buf)
954 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
957 static struct device_attribute dev_attr_hotkey_all_mask =
958 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
960 /* sysfs hotkey recommended_mask --------------------------------------- */
961 static ssize_t hotkey_recommended_mask_show(struct device *dev,
962 struct device_attribute *attr,
963 char *buf)
965 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
966 hotkey_all_mask & ~hotkey_reserved_mask);
969 static struct device_attribute dev_attr_hotkey_recommended_mask =
970 __ATTR(hotkey_recommended_mask, S_IRUGO,
971 hotkey_recommended_mask_show, NULL);
973 /* sysfs hotkey radio_sw ----------------------------------------------- */
974 static ssize_t hotkey_radio_sw_show(struct device *dev,
975 struct device_attribute *attr,
976 char *buf)
978 int res, s;
979 res = hotkey_get_wlsw(&s);
980 if (res < 0)
981 return res;
983 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
986 static struct device_attribute dev_attr_hotkey_radio_sw =
987 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
989 /* sysfs hotkey report_mode -------------------------------------------- */
990 static ssize_t hotkey_report_mode_show(struct device *dev,
991 struct device_attribute *attr,
992 char *buf)
994 return snprintf(buf, PAGE_SIZE, "%d\n",
995 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
998 static struct device_attribute dev_attr_hotkey_report_mode =
999 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1001 /* --------------------------------------------------------------------- */
1003 static struct attribute *hotkey_attributes[] __initdata = {
1004 &dev_attr_hotkey_enable.attr,
1005 &dev_attr_hotkey_report_mode.attr,
1008 static struct attribute *hotkey_mask_attributes[] __initdata = {
1009 &dev_attr_hotkey_mask.attr,
1010 &dev_attr_hotkey_bios_enabled.attr,
1011 &dev_attr_hotkey_bios_mask.attr,
1012 &dev_attr_hotkey_all_mask.attr,
1013 &dev_attr_hotkey_recommended_mask.attr,
1016 static int __init hotkey_init(struct ibm_init_struct *iibm)
1018 /* Requirements for changing the default keymaps:
1020 * 1. Many of the keys are mapped to KEY_RESERVED for very
1021 * good reasons. Do not change them unless you have deep
1022 * knowledge on the IBM and Lenovo ThinkPad firmware for
1023 * the various ThinkPad models. The driver behaves
1024 * differently for KEY_RESERVED: such keys have their
1025 * hot key mask *unset* in mask_recommended, and also
1026 * in the initial hot key mask programmed into the
1027 * firmware at driver load time, which means the firm-
1028 * ware may react very differently if you change them to
1029 * something else;
1031 * 2. You must be subscribed to the linux-thinkpad and
1032 * ibm-acpi-devel mailing lists, and you should read the
1033 * list archives since 2007 if you want to change the
1034 * keymaps. This requirement exists so that you will
1035 * know the past history of problems with the thinkpad-
1036 * acpi driver keymaps, and also that you will be
1037 * listening to any bug reports;
1039 * 3. Do not send thinkpad-acpi specific patches directly to
1040 * for merging, *ever*. Send them to the linux-acpi
1041 * mailinglist for comments. Merging is to be done only
1042 * through acpi-test and the ACPI maintainer.
1044 * If the above is too much to ask, don't change the keymap.
1045 * Ask the thinkpad-acpi maintainer to do it, instead.
1047 static u16 ibm_keycode_map[] __initdata = {
1048 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1049 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1050 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1051 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1053 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1054 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1055 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1056 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1058 /* brightness: firmware always reacts to them, unless
1059 * X.org did some tricks in the radeon BIOS scratch
1060 * registers of *some* models */
1061 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1062 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1064 /* Thinklight: firmware always react to it */
1065 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1067 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1068 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1070 /* Volume: firmware always react to it and reprograms
1071 * the built-in *extra* mixer. Never map it to control
1072 * another mixer by default. */
1073 KEY_RESERVED, /* 0x14: VOLUME UP */
1074 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1075 KEY_RESERVED, /* 0x16: MUTE */
1077 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1079 /* (assignments unknown, please report if found) */
1080 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1081 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1083 static u16 lenovo_keycode_map[] __initdata = {
1084 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1085 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1086 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1087 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1089 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1090 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1091 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1092 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1094 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1095 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1097 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1099 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1100 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1102 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1103 * react to it and reprograms the built-in *extra* mixer.
1104 * Never map it to control another mixer by default.
1106 * T60?, T61, R60?, R61: firmware and EC tries to send
1107 * these over the regular keyboard, so these are no-ops,
1108 * but there are still weird bugs re. MUTE, so do not
1109 * change unless you get test reports from all Lenovo
1110 * models. May cause the BIOS to interfere with the
1111 * HDA mixer.
1113 KEY_RESERVED, /* 0x14: VOLUME UP */
1114 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1115 KEY_RESERVED, /* 0x16: MUTE */
1117 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1119 /* (assignments unknown, please report if found) */
1120 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1121 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1124 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1125 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1126 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1128 int res, i;
1129 int status;
1130 int hkeyv;
1132 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1134 BUG_ON(!tpacpi_inputdev);
1136 IBM_ACPIHANDLE_INIT(hkey);
1137 mutex_init(&hotkey_mutex);
1139 /* hotkey not supported on 570 */
1140 tp_features.hotkey = hkey_handle != NULL;
1142 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1143 str_supported(tp_features.hotkey));
1145 if (tp_features.hotkey) {
1146 hotkey_dev_attributes = create_attr_set(8, NULL);
1147 if (!hotkey_dev_attributes)
1148 return -ENOMEM;
1149 res = add_many_to_attr_set(hotkey_dev_attributes,
1150 hotkey_attributes,
1151 ARRAY_SIZE(hotkey_attributes));
1152 if (res)
1153 return res;
1155 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1156 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1157 for HKEY interface version 0x100 */
1158 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1159 if ((hkeyv >> 8) != 1) {
1160 printk(IBM_ERR "unknown version of the "
1161 "HKEY interface: 0x%x\n", hkeyv);
1162 printk(IBM_ERR "please report this to %s\n",
1163 IBM_MAIL);
1164 } else {
1166 * MHKV 0x100 in A31, R40, R40e,
1167 * T4x, X31, and later
1168 * */
1169 tp_features.hotkey_mask = 1;
1173 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1174 str_supported(tp_features.hotkey_mask));
1176 if (tp_features.hotkey_mask) {
1177 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1178 "MHKA", "qd")) {
1179 printk(IBM_ERR
1180 "missing MHKA handler, "
1181 "please report this to %s\n",
1182 IBM_MAIL);
1183 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1187 res = hotkey_status_get(&hotkey_orig_status);
1188 if (!res && tp_features.hotkey_mask) {
1189 res = hotkey_mask_get();
1190 hotkey_orig_mask = hotkey_mask;
1191 if (!res) {
1192 res = add_many_to_attr_set(
1193 hotkey_dev_attributes,
1194 hotkey_mask_attributes,
1195 ARRAY_SIZE(hotkey_mask_attributes));
1199 /* Not all thinkpads have a hardware radio switch */
1200 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1201 tp_features.hotkey_wlsw = 1;
1202 printk(IBM_INFO
1203 "radio switch found; radios are %s\n",
1204 enabled(status, 0));
1205 res = add_to_attr_set(hotkey_dev_attributes,
1206 &dev_attr_hotkey_radio_sw.attr);
1209 if (!res)
1210 res = register_attr_set_with_sysfs(
1211 hotkey_dev_attributes,
1212 &tpacpi_pdev->dev.kobj);
1213 if (res)
1214 return res;
1216 /* Set up key map */
1218 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1219 GFP_KERNEL);
1220 if (!hotkey_keycode_map) {
1221 printk(IBM_ERR "failed to allocate memory for key map\n");
1222 return -ENOMEM;
1225 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1226 dbg_printk(TPACPI_DBG_INIT,
1227 "using Lenovo default hot key map\n");
1228 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1229 TPACPI_HOTKEY_MAP_SIZE);
1230 } else {
1231 dbg_printk(TPACPI_DBG_INIT,
1232 "using IBM default hot key map\n");
1233 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1234 TPACPI_HOTKEY_MAP_SIZE);
1237 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1238 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1239 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1240 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1241 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1242 tpacpi_inputdev->keycode = hotkey_keycode_map;
1243 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1244 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1245 set_bit(hotkey_keycode_map[i],
1246 tpacpi_inputdev->keybit);
1247 } else {
1248 if (i < sizeof(hotkey_reserved_mask)*8)
1249 hotkey_reserved_mask |= 1 << i;
1253 if (tp_features.hotkey_wlsw) {
1254 set_bit(EV_SW, tpacpi_inputdev->evbit);
1255 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1258 dbg_printk(TPACPI_DBG_INIT,
1259 "enabling hot key handling\n");
1260 res = hotkey_status_set(1);
1261 if (res)
1262 return res;
1263 res = hotkey_mask_set((hotkey_all_mask & ~hotkey_reserved_mask)
1264 | hotkey_orig_mask);
1265 if (res)
1266 return res;
1268 dbg_printk(TPACPI_DBG_INIT,
1269 "legacy hot key reporting over procfs %s\n",
1270 (hotkey_report_mode < 2) ?
1271 "enabled" : "disabled");
1274 return (tp_features.hotkey)? 0 : 1;
1277 static void hotkey_exit(void)
1279 if (tp_features.hotkey) {
1280 dbg_printk(TPACPI_DBG_EXIT, "restoring original hot key mask\n");
1281 /* no short-circuit boolean operator below! */
1282 if ((hotkey_mask_set(hotkey_orig_mask) |
1283 hotkey_status_set(hotkey_orig_status)) != 0)
1284 printk(IBM_ERR "failed to restore hot key mask to BIOS defaults\n");
1287 if (hotkey_dev_attributes) {
1288 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1289 hotkey_dev_attributes = NULL;
1293 static void tpacpi_input_send_key(unsigned int scancode,
1294 unsigned int keycode)
1296 if (keycode != KEY_RESERVED) {
1297 mutex_lock(&tpacpi_inputdev_send_mutex);
1299 input_report_key(tpacpi_inputdev, keycode, 1);
1300 if (keycode == KEY_UNKNOWN)
1301 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1302 scancode);
1303 input_sync(tpacpi_inputdev);
1305 input_report_key(tpacpi_inputdev, keycode, 0);
1306 if (keycode == KEY_UNKNOWN)
1307 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1308 scancode);
1309 input_sync(tpacpi_inputdev);
1311 mutex_unlock(&tpacpi_inputdev_send_mutex);
1315 static void tpacpi_input_send_radiosw(void)
1317 int wlsw;
1319 mutex_lock(&tpacpi_inputdev_send_mutex);
1321 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1322 input_report_switch(tpacpi_inputdev,
1323 SW_RADIO, !!wlsw);
1324 input_sync(tpacpi_inputdev);
1327 mutex_unlock(&tpacpi_inputdev_send_mutex);
1330 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1332 u32 hkey;
1333 unsigned int keycode, scancode;
1334 int send_acpi_ev;
1335 int ignore_acpi_ev;
1337 if (event != 0x80) {
1338 printk(IBM_ERR "unknown HKEY notification event %d\n", event);
1339 /* forward it to userspace, maybe it knows how to handle it */
1340 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1341 ibm->acpi->device->dev.bus_id,
1342 event, 0);
1343 return;
1346 while (1) {
1347 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1348 printk(IBM_ERR "failed to retrieve HKEY event\n");
1349 return;
1352 if (hkey == 0) {
1353 /* queue empty */
1354 return;
1357 send_acpi_ev = 0;
1358 ignore_acpi_ev = 0;
1360 switch (hkey >> 12) {
1361 case 1:
1362 /* 0x1000-0x1FFF: key presses */
1363 scancode = hkey & 0xfff;
1364 if (scancode > 0 && scancode < 0x21) {
1365 scancode--;
1366 keycode = hotkey_keycode_map[scancode];
1367 tpacpi_input_send_key(scancode, keycode);
1368 } else {
1369 printk(IBM_ERR
1370 "hotkey 0x%04x out of range for keyboard map\n",
1371 hkey);
1372 send_acpi_ev = 1;
1374 break;
1375 case 5:
1376 /* 0x5000-0x5FFF: LID */
1377 /* we don't handle it through this path, just
1378 * eat up known LID events */
1379 if (hkey != 0x5001 && hkey != 0x5002) {
1380 printk(IBM_ERR
1381 "unknown LID-related HKEY event: 0x%04x\n",
1382 hkey);
1383 send_acpi_ev = 1;
1384 } else {
1385 ignore_acpi_ev = 1;
1387 break;
1388 case 7:
1389 /* 0x7000-0x7FFF: misc */
1390 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1391 tpacpi_input_send_radiosw();
1392 break;
1394 /* fallthrough to default */
1395 default:
1396 /* case 2: dock-related */
1397 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1398 /* case 3: ultra-bay related. maybe bay in dock? */
1399 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1400 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1401 send_acpi_ev = 1;
1404 /* Legacy events */
1405 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
1406 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
1409 /* netlink events */
1410 if (!ignore_acpi_ev && send_acpi_ev) {
1411 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1412 ibm->acpi->device->dev.bus_id,
1413 event, hkey);
1418 static void hotkey_resume(void)
1420 if (hotkey_mask_get())
1421 printk(IBM_ERR "error while trying to read hot key mask from firmware\n");
1422 tpacpi_input_send_radiosw();
1425 /* procfs -------------------------------------------------------------- */
1426 static int hotkey_read(char *p)
1428 int res, status;
1429 int len = 0;
1431 if (!tp_features.hotkey) {
1432 len += sprintf(p + len, "status:\t\tnot supported\n");
1433 return len;
1436 if (mutex_lock_interruptible(&hotkey_mutex))
1437 return -ERESTARTSYS;
1438 res = hotkey_status_get(&status);
1439 if (!res)
1440 res = hotkey_mask_get();
1441 mutex_unlock(&hotkey_mutex);
1442 if (res)
1443 return res;
1445 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1446 if (tp_features.hotkey_mask) {
1447 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
1448 len += sprintf(p + len,
1449 "commands:\tenable, disable, reset, <mask>\n");
1450 } else {
1451 len += sprintf(p + len, "mask:\t\tnot supported\n");
1452 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1455 return len;
1458 static int hotkey_write(char *buf)
1460 int res, status;
1461 u32 mask;
1462 char *cmd;
1464 if (!tp_features.hotkey)
1465 return -ENODEV;
1467 if (mutex_lock_interruptible(&hotkey_mutex))
1468 return -ERESTARTSYS;
1470 status = -1;
1471 mask = hotkey_mask;
1473 res = 0;
1474 while ((cmd = next_cmd(&buf))) {
1475 if (strlencmp(cmd, "enable") == 0) {
1476 status = 1;
1477 } else if (strlencmp(cmd, "disable") == 0) {
1478 status = 0;
1479 } else if (strlencmp(cmd, "reset") == 0) {
1480 status = hotkey_orig_status;
1481 mask = hotkey_orig_mask;
1482 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1483 /* mask set */
1484 } else if (sscanf(cmd, "%x", &mask) == 1) {
1485 /* mask set */
1486 } else {
1487 res = -EINVAL;
1488 goto errexit;
1491 if (status != -1)
1492 res = hotkey_status_set(status);
1494 if (!res && mask != hotkey_mask)
1495 res = hotkey_mask_set(mask);
1497 errexit:
1498 mutex_unlock(&hotkey_mutex);
1499 return res;
1502 static const struct acpi_device_id ibm_htk_device_ids[] = {
1503 {IBM_HKEY_HID, 0},
1504 {"", 0},
1507 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1508 .hid = ibm_htk_device_ids,
1509 .notify = hotkey_notify,
1510 .handle = &hkey_handle,
1511 .type = ACPI_DEVICE_NOTIFY,
1514 static struct ibm_struct hotkey_driver_data = {
1515 .name = "hotkey",
1516 .read = hotkey_read,
1517 .write = hotkey_write,
1518 .exit = hotkey_exit,
1519 .resume = hotkey_resume,
1520 .acpi = &ibm_hotkey_acpidriver,
1523 /*************************************************************************
1524 * Bluetooth subdriver
1527 /* sysfs bluetooth enable ---------------------------------------------- */
1528 static ssize_t bluetooth_enable_show(struct device *dev,
1529 struct device_attribute *attr,
1530 char *buf)
1532 int status;
1534 status = bluetooth_get_radiosw();
1535 if (status < 0)
1536 return status;
1538 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1541 static ssize_t bluetooth_enable_store(struct device *dev,
1542 struct device_attribute *attr,
1543 const char *buf, size_t count)
1545 unsigned long t;
1546 int res;
1548 if (parse_strtoul(buf, 1, &t))
1549 return -EINVAL;
1551 res = bluetooth_set_radiosw(t);
1553 return (res) ? res : count;
1556 static struct device_attribute dev_attr_bluetooth_enable =
1557 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1558 bluetooth_enable_show, bluetooth_enable_store);
1560 /* --------------------------------------------------------------------- */
1562 static struct attribute *bluetooth_attributes[] = {
1563 &dev_attr_bluetooth_enable.attr,
1564 NULL
1567 static const struct attribute_group bluetooth_attr_group = {
1568 .attrs = bluetooth_attributes,
1571 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1573 int res;
1574 int status = 0;
1576 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1578 IBM_ACPIHANDLE_INIT(hkey);
1580 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1581 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1582 tp_features.bluetooth = hkey_handle &&
1583 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1585 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1586 str_supported(tp_features.bluetooth),
1587 status);
1589 if (tp_features.bluetooth) {
1590 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1591 /* no bluetooth hardware present in system */
1592 tp_features.bluetooth = 0;
1593 dbg_printk(TPACPI_DBG_INIT,
1594 "bluetooth hardware not installed\n");
1595 } else {
1596 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1597 &bluetooth_attr_group);
1598 if (res)
1599 return res;
1603 return (tp_features.bluetooth)? 0 : 1;
1606 static void bluetooth_exit(void)
1608 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1609 &bluetooth_attr_group);
1612 static int bluetooth_get_radiosw(void)
1614 int status;
1616 if (!tp_features.bluetooth)
1617 return -ENODEV;
1619 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1620 return -EIO;
1622 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1625 static int bluetooth_set_radiosw(int radio_on)
1627 int status;
1629 if (!tp_features.bluetooth)
1630 return -ENODEV;
1632 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1633 return -EIO;
1634 if (radio_on)
1635 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1636 else
1637 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1638 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1639 return -EIO;
1641 return 0;
1644 /* procfs -------------------------------------------------------------- */
1645 static int bluetooth_read(char *p)
1647 int len = 0;
1648 int status = bluetooth_get_radiosw();
1650 if (!tp_features.bluetooth)
1651 len += sprintf(p + len, "status:\t\tnot supported\n");
1652 else {
1653 len += sprintf(p + len, "status:\t\t%s\n",
1654 (status)? "enabled" : "disabled");
1655 len += sprintf(p + len, "commands:\tenable, disable\n");
1658 return len;
1661 static int bluetooth_write(char *buf)
1663 char *cmd;
1665 if (!tp_features.bluetooth)
1666 return -ENODEV;
1668 while ((cmd = next_cmd(&buf))) {
1669 if (strlencmp(cmd, "enable") == 0) {
1670 bluetooth_set_radiosw(1);
1671 } else if (strlencmp(cmd, "disable") == 0) {
1672 bluetooth_set_radiosw(0);
1673 } else
1674 return -EINVAL;
1677 return 0;
1680 static struct ibm_struct bluetooth_driver_data = {
1681 .name = "bluetooth",
1682 .read = bluetooth_read,
1683 .write = bluetooth_write,
1684 .exit = bluetooth_exit,
1687 /*************************************************************************
1688 * Wan subdriver
1691 /* sysfs wan enable ---------------------------------------------------- */
1692 static ssize_t wan_enable_show(struct device *dev,
1693 struct device_attribute *attr,
1694 char *buf)
1696 int status;
1698 status = wan_get_radiosw();
1699 if (status < 0)
1700 return status;
1702 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1705 static ssize_t wan_enable_store(struct device *dev,
1706 struct device_attribute *attr,
1707 const char *buf, size_t count)
1709 unsigned long t;
1710 int res;
1712 if (parse_strtoul(buf, 1, &t))
1713 return -EINVAL;
1715 res = wan_set_radiosw(t);
1717 return (res) ? res : count;
1720 static struct device_attribute dev_attr_wan_enable =
1721 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1722 wan_enable_show, wan_enable_store);
1724 /* --------------------------------------------------------------------- */
1726 static struct attribute *wan_attributes[] = {
1727 &dev_attr_wan_enable.attr,
1728 NULL
1731 static const struct attribute_group wan_attr_group = {
1732 .attrs = wan_attributes,
1735 static int __init wan_init(struct ibm_init_struct *iibm)
1737 int res;
1738 int status = 0;
1740 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1742 IBM_ACPIHANDLE_INIT(hkey);
1744 tp_features.wan = hkey_handle &&
1745 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1747 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1748 str_supported(tp_features.wan),
1749 status);
1751 if (tp_features.wan) {
1752 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1753 /* no wan hardware present in system */
1754 tp_features.wan = 0;
1755 dbg_printk(TPACPI_DBG_INIT,
1756 "wan hardware not installed\n");
1757 } else {
1758 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1759 &wan_attr_group);
1760 if (res)
1761 return res;
1765 return (tp_features.wan)? 0 : 1;
1768 static void wan_exit(void)
1770 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1771 &wan_attr_group);
1774 static int wan_get_radiosw(void)
1776 int status;
1778 if (!tp_features.wan)
1779 return -ENODEV;
1781 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1782 return -EIO;
1784 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1787 static int wan_set_radiosw(int radio_on)
1789 int status;
1791 if (!tp_features.wan)
1792 return -ENODEV;
1794 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1795 return -EIO;
1796 if (radio_on)
1797 status |= TP_ACPI_WANCARD_RADIOSSW;
1798 else
1799 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1800 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1801 return -EIO;
1803 return 0;
1806 /* procfs -------------------------------------------------------------- */
1807 static int wan_read(char *p)
1809 int len = 0;
1810 int status = wan_get_radiosw();
1812 if (!tp_features.wan)
1813 len += sprintf(p + len, "status:\t\tnot supported\n");
1814 else {
1815 len += sprintf(p + len, "status:\t\t%s\n",
1816 (status)? "enabled" : "disabled");
1817 len += sprintf(p + len, "commands:\tenable, disable\n");
1820 return len;
1823 static int wan_write(char *buf)
1825 char *cmd;
1827 if (!tp_features.wan)
1828 return -ENODEV;
1830 while ((cmd = next_cmd(&buf))) {
1831 if (strlencmp(cmd, "enable") == 0) {
1832 wan_set_radiosw(1);
1833 } else if (strlencmp(cmd, "disable") == 0) {
1834 wan_set_radiosw(0);
1835 } else
1836 return -EINVAL;
1839 return 0;
1842 static struct ibm_struct wan_driver_data = {
1843 .name = "wan",
1844 .read = wan_read,
1845 .write = wan_write,
1846 .exit = wan_exit,
1847 .flags.experimental = 1,
1850 /*************************************************************************
1851 * Video subdriver
1854 static enum video_access_mode video_supported;
1855 static int video_orig_autosw;
1857 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1858 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1859 "\\_SB.PCI0.VID0", /* 770e */
1860 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1861 "\\_SB.PCI0.AGP.VID", /* all others */
1862 ); /* R30, R31 */
1864 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1866 static int __init video_init(struct ibm_init_struct *iibm)
1868 int ivga;
1870 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1872 IBM_ACPIHANDLE_INIT(vid);
1873 IBM_ACPIHANDLE_INIT(vid2);
1875 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1876 /* G41, assume IVGA doesn't change */
1877 vid_handle = vid2_handle;
1879 if (!vid_handle)
1880 /* video switching not supported on R30, R31 */
1881 video_supported = TPACPI_VIDEO_NONE;
1882 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1883 /* 570 */
1884 video_supported = TPACPI_VIDEO_570;
1885 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1886 /* 600e/x, 770e, 770x */
1887 video_supported = TPACPI_VIDEO_770;
1888 else
1889 /* all others */
1890 video_supported = TPACPI_VIDEO_NEW;
1892 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1893 str_supported(video_supported != TPACPI_VIDEO_NONE),
1894 video_supported);
1896 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1899 static void video_exit(void)
1901 dbg_printk(TPACPI_DBG_EXIT,
1902 "restoring original video autoswitch mode\n");
1903 if (video_autosw_set(video_orig_autosw))
1904 printk(IBM_ERR "error while trying to restore original "
1905 "video autoswitch mode\n");
1908 static int video_outputsw_get(void)
1910 int status = 0;
1911 int i;
1913 switch (video_supported) {
1914 case TPACPI_VIDEO_570:
1915 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1916 TP_ACPI_VIDEO_570_PHSCMD))
1917 return -EIO;
1918 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1919 break;
1920 case TPACPI_VIDEO_770:
1921 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1922 return -EIO;
1923 if (i)
1924 status |= TP_ACPI_VIDEO_S_LCD;
1925 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1926 return -EIO;
1927 if (i)
1928 status |= TP_ACPI_VIDEO_S_CRT;
1929 break;
1930 case TPACPI_VIDEO_NEW:
1931 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1932 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1933 return -EIO;
1934 if (i)
1935 status |= TP_ACPI_VIDEO_S_CRT;
1937 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1938 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1939 return -EIO;
1940 if (i)
1941 status |= TP_ACPI_VIDEO_S_LCD;
1942 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1943 return -EIO;
1944 if (i)
1945 status |= TP_ACPI_VIDEO_S_DVI;
1946 break;
1947 default:
1948 return -ENOSYS;
1951 return status;
1954 static int video_outputsw_set(int status)
1956 int autosw;
1957 int res = 0;
1959 switch (video_supported) {
1960 case TPACPI_VIDEO_570:
1961 res = acpi_evalf(NULL, NULL,
1962 "\\_SB.PHS2", "vdd",
1963 TP_ACPI_VIDEO_570_PHS2CMD,
1964 status | TP_ACPI_VIDEO_570_PHS2SET);
1965 break;
1966 case TPACPI_VIDEO_770:
1967 autosw = video_autosw_get();
1968 if (autosw < 0)
1969 return autosw;
1971 res = video_autosw_set(1);
1972 if (res)
1973 return res;
1974 res = acpi_evalf(vid_handle, NULL,
1975 "ASWT", "vdd", status * 0x100, 0);
1976 if (!autosw && video_autosw_set(autosw)) {
1977 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1978 return -EIO;
1980 break;
1981 case TPACPI_VIDEO_NEW:
1982 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1983 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1984 break;
1985 default:
1986 return -ENOSYS;
1989 return (res)? 0 : -EIO;
1992 static int video_autosw_get(void)
1994 int autosw = 0;
1996 switch (video_supported) {
1997 case TPACPI_VIDEO_570:
1998 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1999 return -EIO;
2000 break;
2001 case TPACPI_VIDEO_770:
2002 case TPACPI_VIDEO_NEW:
2003 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2004 return -EIO;
2005 break;
2006 default:
2007 return -ENOSYS;
2010 return autosw & 1;
2013 static int video_autosw_set(int enable)
2015 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2016 return -EIO;
2017 return 0;
2020 static int video_outputsw_cycle(void)
2022 int autosw = video_autosw_get();
2023 int res;
2025 if (autosw < 0)
2026 return autosw;
2028 switch (video_supported) {
2029 case TPACPI_VIDEO_570:
2030 res = video_autosw_set(1);
2031 if (res)
2032 return res;
2033 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2034 break;
2035 case TPACPI_VIDEO_770:
2036 case TPACPI_VIDEO_NEW:
2037 res = video_autosw_set(1);
2038 if (res)
2039 return res;
2040 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2041 break;
2042 default:
2043 return -ENOSYS;
2045 if (!autosw && video_autosw_set(autosw)) {
2046 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2047 return -EIO;
2050 return (res)? 0 : -EIO;
2053 static int video_expand_toggle(void)
2055 switch (video_supported) {
2056 case TPACPI_VIDEO_570:
2057 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2058 0 : -EIO;
2059 case TPACPI_VIDEO_770:
2060 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2061 0 : -EIO;
2062 case TPACPI_VIDEO_NEW:
2063 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2064 0 : -EIO;
2065 default:
2066 return -ENOSYS;
2068 /* not reached */
2071 static int video_read(char *p)
2073 int status, autosw;
2074 int len = 0;
2076 if (video_supported == TPACPI_VIDEO_NONE) {
2077 len += sprintf(p + len, "status:\t\tnot supported\n");
2078 return len;
2081 status = video_outputsw_get();
2082 if (status < 0)
2083 return status;
2085 autosw = video_autosw_get();
2086 if (autosw < 0)
2087 return autosw;
2089 len += sprintf(p + len, "status:\t\tsupported\n");
2090 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2091 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2092 if (video_supported == TPACPI_VIDEO_NEW)
2093 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2094 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2095 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2096 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2097 if (video_supported == TPACPI_VIDEO_NEW)
2098 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2099 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2100 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2102 return len;
2105 static int video_write(char *buf)
2107 char *cmd;
2108 int enable, disable, status;
2109 int res;
2111 if (video_supported == TPACPI_VIDEO_NONE)
2112 return -ENODEV;
2114 enable = 0;
2115 disable = 0;
2117 while ((cmd = next_cmd(&buf))) {
2118 if (strlencmp(cmd, "lcd_enable") == 0) {
2119 enable |= TP_ACPI_VIDEO_S_LCD;
2120 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2121 disable |= TP_ACPI_VIDEO_S_LCD;
2122 } else if (strlencmp(cmd, "crt_enable") == 0) {
2123 enable |= TP_ACPI_VIDEO_S_CRT;
2124 } else if (strlencmp(cmd, "crt_disable") == 0) {
2125 disable |= TP_ACPI_VIDEO_S_CRT;
2126 } else if (video_supported == TPACPI_VIDEO_NEW &&
2127 strlencmp(cmd, "dvi_enable") == 0) {
2128 enable |= TP_ACPI_VIDEO_S_DVI;
2129 } else if (video_supported == TPACPI_VIDEO_NEW &&
2130 strlencmp(cmd, "dvi_disable") == 0) {
2131 disable |= TP_ACPI_VIDEO_S_DVI;
2132 } else if (strlencmp(cmd, "auto_enable") == 0) {
2133 res = video_autosw_set(1);
2134 if (res)
2135 return res;
2136 } else if (strlencmp(cmd, "auto_disable") == 0) {
2137 res = video_autosw_set(0);
2138 if (res)
2139 return res;
2140 } else if (strlencmp(cmd, "video_switch") == 0) {
2141 res = video_outputsw_cycle();
2142 if (res)
2143 return res;
2144 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2145 res = video_expand_toggle();
2146 if (res)
2147 return res;
2148 } else
2149 return -EINVAL;
2152 if (enable || disable) {
2153 status = video_outputsw_get();
2154 if (status < 0)
2155 return status;
2156 res = video_outputsw_set((status & ~disable) | enable);
2157 if (res)
2158 return res;
2161 return 0;
2164 static struct ibm_struct video_driver_data = {
2165 .name = "video",
2166 .read = video_read,
2167 .write = video_write,
2168 .exit = video_exit,
2171 /*************************************************************************
2172 * Light (thinklight) subdriver
2175 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2176 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2178 static int __init light_init(struct ibm_init_struct *iibm)
2180 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2182 IBM_ACPIHANDLE_INIT(ledb);
2183 IBM_ACPIHANDLE_INIT(lght);
2184 IBM_ACPIHANDLE_INIT(cmos);
2186 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2187 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2189 if (tp_features.light)
2190 /* light status not supported on
2191 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2192 tp_features.light_status =
2193 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2195 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2196 str_supported(tp_features.light));
2198 return (tp_features.light)? 0 : 1;
2201 static int light_read(char *p)
2203 int len = 0;
2204 int status = 0;
2206 if (!tp_features.light) {
2207 len += sprintf(p + len, "status:\t\tnot supported\n");
2208 } else if (!tp_features.light_status) {
2209 len += sprintf(p + len, "status:\t\tunknown\n");
2210 len += sprintf(p + len, "commands:\ton, off\n");
2211 } else {
2212 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2213 return -EIO;
2214 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2215 len += sprintf(p + len, "commands:\ton, off\n");
2218 return len;
2221 static int light_write(char *buf)
2223 int cmos_cmd, lght_cmd;
2224 char *cmd;
2225 int success;
2227 if (!tp_features.light)
2228 return -ENODEV;
2230 while ((cmd = next_cmd(&buf))) {
2231 if (strlencmp(cmd, "on") == 0) {
2232 cmos_cmd = 0x0c;
2233 lght_cmd = 1;
2234 } else if (strlencmp(cmd, "off") == 0) {
2235 cmos_cmd = 0x0d;
2236 lght_cmd = 0;
2237 } else
2238 return -EINVAL;
2240 success = cmos_handle ?
2241 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2242 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2243 if (!success)
2244 return -EIO;
2247 return 0;
2250 static struct ibm_struct light_driver_data = {
2251 .name = "light",
2252 .read = light_read,
2253 .write = light_write,
2256 /*************************************************************************
2257 * Dock subdriver
2260 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2262 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2263 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2264 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2265 "\\_SB.PCI.ISA.SLCE", /* 570 */
2266 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2268 /* don't list other alternatives as we install a notify handler on the 570 */
2269 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2271 static const struct acpi_device_id ibm_pci_device_ids[] = {
2272 {PCI_ROOT_HID_STRING, 0},
2273 {"", 0},
2276 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2278 .notify = dock_notify,
2279 .handle = &dock_handle,
2280 .type = ACPI_SYSTEM_NOTIFY,
2283 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2284 * We just use it to get notifications of dock hotplug
2285 * in very old thinkpads */
2286 .hid = ibm_pci_device_ids,
2287 .notify = dock_notify,
2288 .handle = &pci_handle,
2289 .type = ACPI_SYSTEM_NOTIFY,
2293 static struct ibm_struct dock_driver_data[2] = {
2295 .name = "dock",
2296 .read = dock_read,
2297 .write = dock_write,
2298 .acpi = &ibm_dock_acpidriver[0],
2301 .name = "dock",
2302 .acpi = &ibm_dock_acpidriver[1],
2306 #define dock_docked() (_sta(dock_handle) & 1)
2308 static int __init dock_init(struct ibm_init_struct *iibm)
2310 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2312 IBM_ACPIHANDLE_INIT(dock);
2314 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2315 str_supported(dock_handle != NULL));
2317 return (dock_handle)? 0 : 1;
2320 static int __init dock_init2(struct ibm_init_struct *iibm)
2322 int dock2_needed;
2324 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2326 if (dock_driver_data[0].flags.acpi_driver_registered &&
2327 dock_driver_data[0].flags.acpi_notify_installed) {
2328 IBM_ACPIHANDLE_INIT(pci);
2329 dock2_needed = (pci_handle != NULL);
2330 vdbg_printk(TPACPI_DBG_INIT,
2331 "dock PCI handler for the TP 570 is %s\n",
2332 str_supported(dock2_needed));
2333 } else {
2334 vdbg_printk(TPACPI_DBG_INIT,
2335 "dock subdriver part 2 not required\n");
2336 dock2_needed = 0;
2339 return (dock2_needed)? 0 : 1;
2342 static void dock_notify(struct ibm_struct *ibm, u32 event)
2344 int docked = dock_docked();
2345 int pci = ibm->acpi->hid && ibm->acpi->device &&
2346 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
2347 int data;
2349 if (event == 1 && !pci) /* 570 */
2350 data = 1; /* button */
2351 else if (event == 1 && pci) /* 570 */
2352 data = 3; /* dock */
2353 else if (event == 3 && docked)
2354 data = 1; /* button */
2355 else if (event == 3 && !docked)
2356 data = 2; /* undock */
2357 else if (event == 0 && docked)
2358 data = 3; /* dock */
2359 else {
2360 printk(IBM_ERR "unknown dock event %d, status %d\n",
2361 event, _sta(dock_handle));
2362 data = 0; /* unknown */
2364 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
2365 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2366 ibm->acpi->device->dev.bus_id,
2367 event, data);
2370 static int dock_read(char *p)
2372 int len = 0;
2373 int docked = dock_docked();
2375 if (!dock_handle)
2376 len += sprintf(p + len, "status:\t\tnot supported\n");
2377 else if (!docked)
2378 len += sprintf(p + len, "status:\t\tundocked\n");
2379 else {
2380 len += sprintf(p + len, "status:\t\tdocked\n");
2381 len += sprintf(p + len, "commands:\tdock, undock\n");
2384 return len;
2387 static int dock_write(char *buf)
2389 char *cmd;
2391 if (!dock_docked())
2392 return -ENODEV;
2394 while ((cmd = next_cmd(&buf))) {
2395 if (strlencmp(cmd, "undock") == 0) {
2396 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2397 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2398 return -EIO;
2399 } else if (strlencmp(cmd, "dock") == 0) {
2400 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2401 return -EIO;
2402 } else
2403 return -EINVAL;
2406 return 0;
2409 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2411 /*************************************************************************
2412 * Bay subdriver
2415 #ifdef CONFIG_THINKPAD_ACPI_BAY
2416 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2417 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2418 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2419 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2420 ); /* A21e, R30, R31 */
2421 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2422 "_EJ0", /* all others */
2423 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2424 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2425 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2426 ); /* all others */
2427 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2428 "_EJ0", /* 770x */
2429 ); /* all others */
2431 static int __init bay_init(struct ibm_init_struct *iibm)
2433 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2435 IBM_ACPIHANDLE_INIT(bay);
2436 if (bay_handle)
2437 IBM_ACPIHANDLE_INIT(bay_ej);
2438 IBM_ACPIHANDLE_INIT(bay2);
2439 if (bay2_handle)
2440 IBM_ACPIHANDLE_INIT(bay2_ej);
2442 tp_features.bay_status = bay_handle &&
2443 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2444 tp_features.bay_status2 = bay2_handle &&
2445 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2447 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2448 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2449 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2450 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2452 vdbg_printk(TPACPI_DBG_INIT,
2453 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2454 str_supported(tp_features.bay_status),
2455 str_supported(tp_features.bay_eject),
2456 str_supported(tp_features.bay_status2),
2457 str_supported(tp_features.bay_eject2));
2459 return (tp_features.bay_status || tp_features.bay_eject ||
2460 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2463 static void bay_notify(struct ibm_struct *ibm, u32 event)
2465 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
2466 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2467 ibm->acpi->device->dev.bus_id,
2468 event, 0);
2471 #define bay_occupied(b) (_sta(b##_handle) & 1)
2473 static int bay_read(char *p)
2475 int len = 0;
2476 int occupied = bay_occupied(bay);
2477 int occupied2 = bay_occupied(bay2);
2478 int eject, eject2;
2480 len += sprintf(p + len, "status:\t\t%s\n",
2481 tp_features.bay_status ?
2482 (occupied ? "occupied" : "unoccupied") :
2483 "not supported");
2484 if (tp_features.bay_status2)
2485 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2486 "occupied" : "unoccupied");
2488 eject = tp_features.bay_eject && occupied;
2489 eject2 = tp_features.bay_eject2 && occupied2;
2491 if (eject && eject2)
2492 len += sprintf(p + len, "commands:\teject, eject2\n");
2493 else if (eject)
2494 len += sprintf(p + len, "commands:\teject\n");
2495 else if (eject2)
2496 len += sprintf(p + len, "commands:\teject2\n");
2498 return len;
2501 static int bay_write(char *buf)
2503 char *cmd;
2505 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2506 return -ENODEV;
2508 while ((cmd = next_cmd(&buf))) {
2509 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2510 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2511 return -EIO;
2512 } else if (tp_features.bay_eject2 &&
2513 strlencmp(cmd, "eject2") == 0) {
2514 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2515 return -EIO;
2516 } else
2517 return -EINVAL;
2520 return 0;
2523 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2524 .notify = bay_notify,
2525 .handle = &bay_handle,
2526 .type = ACPI_SYSTEM_NOTIFY,
2529 static struct ibm_struct bay_driver_data = {
2530 .name = "bay",
2531 .read = bay_read,
2532 .write = bay_write,
2533 .acpi = &ibm_bay_acpidriver,
2536 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2538 /*************************************************************************
2539 * CMOS subdriver
2542 /* sysfs cmos_command -------------------------------------------------- */
2543 static ssize_t cmos_command_store(struct device *dev,
2544 struct device_attribute *attr,
2545 const char *buf, size_t count)
2547 unsigned long cmos_cmd;
2548 int res;
2550 if (parse_strtoul(buf, 21, &cmos_cmd))
2551 return -EINVAL;
2553 res = issue_thinkpad_cmos_command(cmos_cmd);
2554 return (res)? res : count;
2557 static struct device_attribute dev_attr_cmos_command =
2558 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2560 /* --------------------------------------------------------------------- */
2562 static int __init cmos_init(struct ibm_init_struct *iibm)
2564 int res;
2566 vdbg_printk(TPACPI_DBG_INIT,
2567 "initializing cmos commands subdriver\n");
2569 IBM_ACPIHANDLE_INIT(cmos);
2571 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2572 str_supported(cmos_handle != NULL));
2574 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2575 if (res)
2576 return res;
2578 return (cmos_handle)? 0 : 1;
2581 static void cmos_exit(void)
2583 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2586 static int cmos_read(char *p)
2588 int len = 0;
2590 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2591 R30, R31, T20-22, X20-21 */
2592 if (!cmos_handle)
2593 len += sprintf(p + len, "status:\t\tnot supported\n");
2594 else {
2595 len += sprintf(p + len, "status:\t\tsupported\n");
2596 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2599 return len;
2602 static int cmos_write(char *buf)
2604 char *cmd;
2605 int cmos_cmd, res;
2607 while ((cmd = next_cmd(&buf))) {
2608 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2609 cmos_cmd >= 0 && cmos_cmd <= 21) {
2610 /* cmos_cmd set */
2611 } else
2612 return -EINVAL;
2614 res = issue_thinkpad_cmos_command(cmos_cmd);
2615 if (res)
2616 return res;
2619 return 0;
2622 static struct ibm_struct cmos_driver_data = {
2623 .name = "cmos",
2624 .read = cmos_read,
2625 .write = cmos_write,
2626 .exit = cmos_exit,
2629 /*************************************************************************
2630 * LED subdriver
2633 static enum led_access_mode led_supported;
2635 IBM_HANDLE(led, ec, "SLED", /* 570 */
2636 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2637 "LED", /* all others */
2638 ); /* R30, R31 */
2640 static int __init led_init(struct ibm_init_struct *iibm)
2642 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2644 IBM_ACPIHANDLE_INIT(led);
2646 if (!led_handle)
2647 /* led not supported on R30, R31 */
2648 led_supported = TPACPI_LED_NONE;
2649 else if (strlencmp(led_path, "SLED") == 0)
2650 /* 570 */
2651 led_supported = TPACPI_LED_570;
2652 else if (strlencmp(led_path, "SYSL") == 0)
2653 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2654 led_supported = TPACPI_LED_OLD;
2655 else
2656 /* all others */
2657 led_supported = TPACPI_LED_NEW;
2659 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2660 str_supported(led_supported), led_supported);
2662 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2665 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2667 static int led_read(char *p)
2669 int len = 0;
2671 if (!led_supported) {
2672 len += sprintf(p + len, "status:\t\tnot supported\n");
2673 return len;
2675 len += sprintf(p + len, "status:\t\tsupported\n");
2677 if (led_supported == TPACPI_LED_570) {
2678 /* 570 */
2679 int i, status;
2680 for (i = 0; i < 8; i++) {
2681 if (!acpi_evalf(ec_handle,
2682 &status, "GLED", "dd", 1 << i))
2683 return -EIO;
2684 len += sprintf(p + len, "%d:\t\t%s\n",
2685 i, led_status(status));
2689 len += sprintf(p + len, "commands:\t"
2690 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2692 return len;
2695 /* off, on, blink */
2696 static const int led_sled_arg1[] = { 0, 1, 3 };
2697 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2698 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2699 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2701 static int led_write(char *buf)
2703 char *cmd;
2704 int led, ind, ret;
2706 if (!led_supported)
2707 return -ENODEV;
2709 while ((cmd = next_cmd(&buf))) {
2710 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2711 return -EINVAL;
2713 if (strstr(cmd, "off")) {
2714 ind = 0;
2715 } else if (strstr(cmd, "on")) {
2716 ind = 1;
2717 } else if (strstr(cmd, "blink")) {
2718 ind = 2;
2719 } else
2720 return -EINVAL;
2722 if (led_supported == TPACPI_LED_570) {
2723 /* 570 */
2724 led = 1 << led;
2725 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2726 led, led_sled_arg1[ind]))
2727 return -EIO;
2728 } else if (led_supported == TPACPI_LED_OLD) {
2729 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2730 led = 1 << led;
2731 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2732 if (ret >= 0)
2733 ret =
2734 ec_write(TPACPI_LED_EC_HLBL,
2735 led * led_exp_hlbl[ind]);
2736 if (ret >= 0)
2737 ret =
2738 ec_write(TPACPI_LED_EC_HLCL,
2739 led * led_exp_hlcl[ind]);
2740 if (ret < 0)
2741 return ret;
2742 } else {
2743 /* all others */
2744 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2745 led, led_led_arg1[ind]))
2746 return -EIO;
2750 return 0;
2753 static struct ibm_struct led_driver_data = {
2754 .name = "led",
2755 .read = led_read,
2756 .write = led_write,
2759 /*************************************************************************
2760 * Beep subdriver
2763 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2765 static int __init beep_init(struct ibm_init_struct *iibm)
2767 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2769 IBM_ACPIHANDLE_INIT(beep);
2771 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2772 str_supported(beep_handle != NULL));
2774 return (beep_handle)? 0 : 1;
2777 static int beep_read(char *p)
2779 int len = 0;
2781 if (!beep_handle)
2782 len += sprintf(p + len, "status:\t\tnot supported\n");
2783 else {
2784 len += sprintf(p + len, "status:\t\tsupported\n");
2785 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2788 return len;
2791 static int beep_write(char *buf)
2793 char *cmd;
2794 int beep_cmd;
2796 if (!beep_handle)
2797 return -ENODEV;
2799 while ((cmd = next_cmd(&buf))) {
2800 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2801 beep_cmd >= 0 && beep_cmd <= 17) {
2802 /* beep_cmd set */
2803 } else
2804 return -EINVAL;
2805 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2806 return -EIO;
2809 return 0;
2812 static struct ibm_struct beep_driver_data = {
2813 .name = "beep",
2814 .read = beep_read,
2815 .write = beep_write,
2818 /*************************************************************************
2819 * Thermal subdriver
2822 static enum thermal_access_mode thermal_read_mode;
2824 /* sysfs temp##_input -------------------------------------------------- */
2826 static ssize_t thermal_temp_input_show(struct device *dev,
2827 struct device_attribute *attr,
2828 char *buf)
2830 struct sensor_device_attribute *sensor_attr =
2831 to_sensor_dev_attr(attr);
2832 int idx = sensor_attr->index;
2833 s32 value;
2834 int res;
2836 res = thermal_get_sensor(idx, &value);
2837 if (res)
2838 return res;
2839 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2840 return -ENXIO;
2842 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2845 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2846 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2848 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2849 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2850 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2851 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2852 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2853 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2854 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2855 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2856 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2857 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2858 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2859 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2860 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2861 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2862 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2863 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2864 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2867 #define THERMAL_ATTRS(X) \
2868 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2870 static struct attribute *thermal_temp_input_attr[] = {
2871 THERMAL_ATTRS(8),
2872 THERMAL_ATTRS(9),
2873 THERMAL_ATTRS(10),
2874 THERMAL_ATTRS(11),
2875 THERMAL_ATTRS(12),
2876 THERMAL_ATTRS(13),
2877 THERMAL_ATTRS(14),
2878 THERMAL_ATTRS(15),
2879 THERMAL_ATTRS(0),
2880 THERMAL_ATTRS(1),
2881 THERMAL_ATTRS(2),
2882 THERMAL_ATTRS(3),
2883 THERMAL_ATTRS(4),
2884 THERMAL_ATTRS(5),
2885 THERMAL_ATTRS(6),
2886 THERMAL_ATTRS(7),
2887 NULL
2890 static const struct attribute_group thermal_temp_input16_group = {
2891 .attrs = thermal_temp_input_attr
2894 static const struct attribute_group thermal_temp_input8_group = {
2895 .attrs = &thermal_temp_input_attr[8]
2898 #undef THERMAL_SENSOR_ATTR_TEMP
2899 #undef THERMAL_ATTRS
2901 /* --------------------------------------------------------------------- */
2903 static int __init thermal_init(struct ibm_init_struct *iibm)
2905 u8 t, ta1, ta2;
2906 int i;
2907 int acpi_tmp7;
2908 int res;
2910 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2912 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2914 if (thinkpad_id.ec_model) {
2916 * Direct EC access mode: sensors at registers
2917 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2918 * non-implemented, thermal sensors return 0x80 when
2919 * not available
2922 ta1 = ta2 = 0;
2923 for (i = 0; i < 8; i++) {
2924 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2925 ta1 |= t;
2926 } else {
2927 ta1 = 0;
2928 break;
2930 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2931 ta2 |= t;
2932 } else {
2933 ta1 = 0;
2934 break;
2937 if (ta1 == 0) {
2938 /* This is sheer paranoia, but we handle it anyway */
2939 if (acpi_tmp7) {
2940 printk(IBM_ERR
2941 "ThinkPad ACPI EC access misbehaving, "
2942 "falling back to ACPI TMPx access mode\n");
2943 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2944 } else {
2945 printk(IBM_ERR
2946 "ThinkPad ACPI EC access misbehaving, "
2947 "disabling thermal sensors access\n");
2948 thermal_read_mode = TPACPI_THERMAL_NONE;
2950 } else {
2951 thermal_read_mode =
2952 (ta2 != 0) ?
2953 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2955 } else if (acpi_tmp7) {
2956 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2957 /* 600e/x, 770e, 770x */
2958 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2959 } else {
2960 /* Standard ACPI TMPx access, max 8 sensors */
2961 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2963 } else {
2964 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2965 thermal_read_mode = TPACPI_THERMAL_NONE;
2968 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2969 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2970 thermal_read_mode);
2972 switch(thermal_read_mode) {
2973 case TPACPI_THERMAL_TPEC_16:
2974 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
2975 &thermal_temp_input16_group);
2976 if (res)
2977 return res;
2978 break;
2979 case TPACPI_THERMAL_TPEC_8:
2980 case TPACPI_THERMAL_ACPI_TMP07:
2981 case TPACPI_THERMAL_ACPI_UPDT:
2982 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
2983 &thermal_temp_input8_group);
2984 if (res)
2985 return res;
2986 break;
2987 case TPACPI_THERMAL_NONE:
2988 default:
2989 return 1;
2992 return 0;
2995 static void thermal_exit(void)
2997 switch(thermal_read_mode) {
2998 case TPACPI_THERMAL_TPEC_16:
2999 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3000 &thermal_temp_input16_group);
3001 break;
3002 case TPACPI_THERMAL_TPEC_8:
3003 case TPACPI_THERMAL_ACPI_TMP07:
3004 case TPACPI_THERMAL_ACPI_UPDT:
3005 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3006 &thermal_temp_input16_group);
3007 break;
3008 case TPACPI_THERMAL_NONE:
3009 default:
3010 break;
3014 /* idx is zero-based */
3015 static int thermal_get_sensor(int idx, s32 *value)
3017 int t;
3018 s8 tmp;
3019 char tmpi[5];
3021 t = TP_EC_THERMAL_TMP0;
3023 switch (thermal_read_mode) {
3024 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3025 case TPACPI_THERMAL_TPEC_16:
3026 if (idx >= 8 && idx <= 15) {
3027 t = TP_EC_THERMAL_TMP8;
3028 idx -= 8;
3030 /* fallthrough */
3031 #endif
3032 case TPACPI_THERMAL_TPEC_8:
3033 if (idx <= 7) {
3034 if (!acpi_ec_read(t + idx, &tmp))
3035 return -EIO;
3036 *value = tmp * 1000;
3037 return 0;
3039 break;
3041 case TPACPI_THERMAL_ACPI_UPDT:
3042 if (idx <= 7) {
3043 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3044 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3045 return -EIO;
3046 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3047 return -EIO;
3048 *value = (t - 2732) * 100;
3049 return 0;
3051 break;
3053 case TPACPI_THERMAL_ACPI_TMP07:
3054 if (idx <= 7) {
3055 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3056 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3057 return -EIO;
3058 if (t > 127 || t < -127)
3059 t = TP_EC_THERMAL_TMP_NA;
3060 *value = t * 1000;
3061 return 0;
3063 break;
3065 case TPACPI_THERMAL_NONE:
3066 default:
3067 return -ENOSYS;
3070 return -EINVAL;
3073 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3075 int res, i;
3076 int n;
3078 n = 8;
3079 i = 0;
3081 if (!s)
3082 return -EINVAL;
3084 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3085 n = 16;
3087 for(i = 0 ; i < n; i++) {
3088 res = thermal_get_sensor(i, &s->temp[i]);
3089 if (res)
3090 return res;
3093 return n;
3096 static int thermal_read(char *p)
3098 int len = 0;
3099 int n, i;
3100 struct ibm_thermal_sensors_struct t;
3102 n = thermal_get_sensors(&t);
3103 if (unlikely(n < 0))
3104 return n;
3106 len += sprintf(p + len, "temperatures:\t");
3108 if (n > 0) {
3109 for (i = 0; i < (n - 1); i++)
3110 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3111 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3112 } else
3113 len += sprintf(p + len, "not supported\n");
3115 return len;
3118 static struct ibm_struct thermal_driver_data = {
3119 .name = "thermal",
3120 .read = thermal_read,
3121 .exit = thermal_exit,
3124 /*************************************************************************
3125 * EC Dump subdriver
3128 static u8 ecdump_regs[256];
3130 static int ecdump_read(char *p)
3132 int len = 0;
3133 int i, j;
3134 u8 v;
3136 len += sprintf(p + len, "EC "
3137 " +00 +01 +02 +03 +04 +05 +06 +07"
3138 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3139 for (i = 0; i < 256; i += 16) {
3140 len += sprintf(p + len, "EC 0x%02x:", i);
3141 for (j = 0; j < 16; j++) {
3142 if (!acpi_ec_read(i + j, &v))
3143 break;
3144 if (v != ecdump_regs[i + j])
3145 len += sprintf(p + len, " *%02x", v);
3146 else
3147 len += sprintf(p + len, " %02x", v);
3148 ecdump_regs[i + j] = v;
3150 len += sprintf(p + len, "\n");
3151 if (j != 16)
3152 break;
3155 /* These are way too dangerous to advertise openly... */
3156 #if 0
3157 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3158 " (<offset> is 00-ff, <value> is 00-ff)\n");
3159 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3160 " (<offset> is 00-ff, <value> is 0-255)\n");
3161 #endif
3162 return len;
3165 static int ecdump_write(char *buf)
3167 char *cmd;
3168 int i, v;
3170 while ((cmd = next_cmd(&buf))) {
3171 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3172 /* i and v set */
3173 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3174 /* i and v set */
3175 } else
3176 return -EINVAL;
3177 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3178 if (!acpi_ec_write(i, v))
3179 return -EIO;
3180 } else
3181 return -EINVAL;
3184 return 0;
3187 static struct ibm_struct ecdump_driver_data = {
3188 .name = "ecdump",
3189 .read = ecdump_read,
3190 .write = ecdump_write,
3191 .flags.experimental = 1,
3194 /*************************************************************************
3195 * Backlight/brightness subdriver
3198 static struct backlight_device *ibm_backlight_device;
3200 static struct backlight_ops ibm_backlight_data = {
3201 .get_brightness = brightness_get,
3202 .update_status = brightness_update_status,
3205 static struct mutex brightness_mutex;
3207 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
3209 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
3210 union acpi_object *obj;
3211 int rc;
3213 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
3214 obj = (union acpi_object *)buffer.pointer;
3215 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
3216 printk(IBM_ERR "Unknown BCLL data, "
3217 "please report this to %s\n", IBM_MAIL);
3218 rc = 0;
3219 } else {
3220 rc = obj->package.count;
3222 } else {
3223 return 0;
3226 kfree(buffer.pointer);
3227 return rc;
3230 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
3231 void *context, void **rv)
3233 char name[ACPI_PATH_SEGMENT_LENGTH];
3234 struct acpi_buffer buffer = { sizeof(name), &name };
3236 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
3237 !strncmp("BCLL", name, sizeof(name) - 1)) {
3238 if (tpacpi_query_bcll_levels(handle) == 16) {
3239 *rv = handle;
3240 return AE_CTRL_TERMINATE;
3241 } else {
3242 return AE_OK;
3244 } else {
3245 return AE_OK;
3249 static int __init brightness_check_levels(void)
3251 int status;
3252 void *found_node = NULL;
3254 if (!vid_handle) {
3255 IBM_ACPIHANDLE_INIT(vid);
3257 if (!vid_handle)
3258 return 0;
3260 /* Search for a BCLL package with 16 levels */
3261 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
3262 brightness_find_bcll, NULL, &found_node);
3264 return (ACPI_SUCCESS(status) && found_node != NULL);
3267 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
3268 void *context, void **rv)
3270 char name[ACPI_PATH_SEGMENT_LENGTH];
3271 struct acpi_buffer buffer = { sizeof(name), &name };
3273 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
3274 !strncmp("_BCL", name, sizeof(name) - 1)) {
3275 *rv = handle;
3276 return AE_CTRL_TERMINATE;
3277 } else {
3278 return AE_OK;
3282 static int __init brightness_check_std_acpi_support(void)
3284 int status;
3285 void *found_node = NULL;
3287 if (!vid_handle) {
3288 IBM_ACPIHANDLE_INIT(vid);
3290 if (!vid_handle)
3291 return 0;
3293 /* Search for a _BCL method, but don't execute it */
3294 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
3295 brightness_find_bcl, NULL, &found_node);
3297 return (ACPI_SUCCESS(status) && found_node != NULL);
3300 static int __init brightness_init(struct ibm_init_struct *iibm)
3302 int b;
3304 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3306 mutex_init(&brightness_mutex);
3308 if (!brightness_enable) {
3309 dbg_printk(TPACPI_DBG_INIT,
3310 "brightness support disabled by module parameter\n");
3311 return 1;
3312 } else if (brightness_enable > 1) {
3313 if (brightness_check_std_acpi_support()) {
3314 printk(IBM_NOTICE
3315 "standard ACPI backlight interface available, not loading native one...\n");
3316 return 1;
3320 if (!brightness_mode) {
3321 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3322 brightness_mode = 2;
3323 else
3324 brightness_mode = 3;
3326 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3327 brightness_mode);
3330 if (brightness_mode > 3)
3331 return -EINVAL;
3333 tp_features.bright_16levels =
3334 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
3335 brightness_check_levels();
3337 b = brightness_get(NULL);
3338 if (b < 0)
3339 return 1;
3341 if (tp_features.bright_16levels)
3342 printk(IBM_INFO "detected a 16-level brightness capable ThinkPad\n");
3344 ibm_backlight_device = backlight_device_register(
3345 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3346 &ibm_backlight_data);
3347 if (IS_ERR(ibm_backlight_device)) {
3348 printk(IBM_ERR "Could not register backlight device\n");
3349 return PTR_ERR(ibm_backlight_device);
3351 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
3353 ibm_backlight_device->props.max_brightness =
3354 (tp_features.bright_16levels)? 15 : 7;
3355 ibm_backlight_device->props.brightness = b;
3356 backlight_update_status(ibm_backlight_device);
3358 return 0;
3361 static void brightness_exit(void)
3363 if (ibm_backlight_device) {
3364 vdbg_printk(TPACPI_DBG_EXIT,
3365 "calling backlight_device_unregister()\n");
3366 backlight_device_unregister(ibm_backlight_device);
3367 ibm_backlight_device = NULL;
3371 static int brightness_update_status(struct backlight_device *bd)
3373 /* it is the backlight class's job (caller) to handle
3374 * EINTR and other errors properly */
3375 return brightness_set(
3376 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3377 bd->props.power == FB_BLANK_UNBLANK) ?
3378 bd->props.brightness : 0);
3382 * ThinkPads can read brightness from two places: EC 0x31, or
3383 * CMOS NVRAM byte 0x5E, bits 0-3.
3385 static int brightness_get(struct backlight_device *bd)
3387 u8 lec = 0, lcmos = 0, level = 0;
3389 if (brightness_mode & 1) {
3390 if (!acpi_ec_read(brightness_offset, &lec))
3391 return -EIO;
3392 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
3393 level = lec;
3395 if (brightness_mode & 2) {
3396 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3397 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3398 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3399 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
3400 level = lcmos;
3403 if (brightness_mode == 3 && lec != lcmos) {
3404 printk(IBM_ERR
3405 "CMOS NVRAM (%u) and EC (%u) do not agree "
3406 "on display brightness level\n",
3407 (unsigned int) lcmos,
3408 (unsigned int) lec);
3409 return -EIO;
3412 return level;
3415 /* May return EINTR which can always be mapped to ERESTARTSYS */
3416 static int brightness_set(int value)
3418 int cmos_cmd, inc, i, res;
3419 int current_value;
3421 if (value > ((tp_features.bright_16levels)? 15 : 7))
3422 return -EINVAL;
3424 res = mutex_lock_interruptible(&brightness_mutex);
3425 if (res < 0)
3426 return res;
3428 current_value = brightness_get(NULL);
3429 if (current_value < 0) {
3430 res = current_value;
3431 goto errout;
3434 cmos_cmd = value > current_value ?
3435 TP_CMOS_BRIGHTNESS_UP :
3436 TP_CMOS_BRIGHTNESS_DOWN;
3437 inc = (value > current_value)? 1 : -1;
3439 res = 0;
3440 for (i = current_value; i != value; i += inc) {
3441 if ((brightness_mode & 2) &&
3442 issue_thinkpad_cmos_command(cmos_cmd)) {
3443 res = -EIO;
3444 goto errout;
3446 if ((brightness_mode & 1) &&
3447 !acpi_ec_write(brightness_offset, i + inc)) {
3448 res = -EIO;
3449 goto errout;;
3453 errout:
3454 mutex_unlock(&brightness_mutex);
3455 return res;
3458 static int brightness_read(char *p)
3460 int len = 0;
3461 int level;
3463 if ((level = brightness_get(NULL)) < 0) {
3464 len += sprintf(p + len, "level:\t\tunreadable\n");
3465 } else {
3466 len += sprintf(p + len, "level:\t\t%d\n", level);
3467 len += sprintf(p + len, "commands:\tup, down\n");
3468 len += sprintf(p + len, "commands:\tlevel <level>"
3469 " (<level> is 0-%d)\n",
3470 (tp_features.bright_16levels) ? 15 : 7);
3473 return len;
3476 static int brightness_write(char *buf)
3478 int level;
3479 int rc;
3480 char *cmd;
3481 int max_level = (tp_features.bright_16levels) ? 15 : 7;
3483 level = brightness_get(NULL);
3484 if (level < 0)
3485 return level;
3487 while ((cmd = next_cmd(&buf))) {
3488 if (strlencmp(cmd, "up") == 0) {
3489 if (level < max_level)
3490 level++;
3491 } else if (strlencmp(cmd, "down") == 0) {
3492 if (level > 0)
3493 level--;
3494 } else if (sscanf(cmd, "level %d", &level) == 1 &&
3495 level >= 0 && level <= max_level) {
3496 /* new level set */
3497 } else
3498 return -EINVAL;
3502 * Now we know what the final level should be, so we try to set it.
3503 * Doing it this way makes the syscall restartable in case of EINTR
3505 rc = brightness_set(level);
3506 return (rc == -EINTR)? ERESTARTSYS : rc;
3509 static struct ibm_struct brightness_driver_data = {
3510 .name = "brightness",
3511 .read = brightness_read,
3512 .write = brightness_write,
3513 .exit = brightness_exit,
3516 /*************************************************************************
3517 * Volume subdriver
3520 static int volume_read(char *p)
3522 int len = 0;
3523 u8 level;
3525 if (!acpi_ec_read(volume_offset, &level)) {
3526 len += sprintf(p + len, "level:\t\tunreadable\n");
3527 } else {
3528 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3529 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3530 len += sprintf(p + len, "commands:\tup, down, mute\n");
3531 len += sprintf(p + len, "commands:\tlevel <level>"
3532 " (<level> is 0-15)\n");
3535 return len;
3538 static int volume_write(char *buf)
3540 int cmos_cmd, inc, i;
3541 u8 level, mute;
3542 int new_level, new_mute;
3543 char *cmd;
3545 while ((cmd = next_cmd(&buf))) {
3546 if (!acpi_ec_read(volume_offset, &level))
3547 return -EIO;
3548 new_mute = mute = level & 0x40;
3549 new_level = level = level & 0xf;
3551 if (strlencmp(cmd, "up") == 0) {
3552 if (mute)
3553 new_mute = 0;
3554 else
3555 new_level = level == 15 ? 15 : level + 1;
3556 } else if (strlencmp(cmd, "down") == 0) {
3557 if (mute)
3558 new_mute = 0;
3559 else
3560 new_level = level == 0 ? 0 : level - 1;
3561 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3562 new_level >= 0 && new_level <= 15) {
3563 /* new_level set */
3564 } else if (strlencmp(cmd, "mute") == 0) {
3565 new_mute = 0x40;
3566 } else
3567 return -EINVAL;
3569 if (new_level != level) { /* mute doesn't change */
3570 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3571 inc = new_level > level ? 1 : -1;
3573 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3574 !acpi_ec_write(volume_offset, level)))
3575 return -EIO;
3577 for (i = level; i != new_level; i += inc)
3578 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3579 !acpi_ec_write(volume_offset, i + inc))
3580 return -EIO;
3582 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3583 !acpi_ec_write(volume_offset,
3584 new_level + mute)))
3585 return -EIO;
3588 if (new_mute != mute) { /* level doesn't change */
3589 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3591 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3592 !acpi_ec_write(volume_offset, level + new_mute))
3593 return -EIO;
3597 return 0;
3600 static struct ibm_struct volume_driver_data = {
3601 .name = "volume",
3602 .read = volume_read,
3603 .write = volume_write,
3606 /*************************************************************************
3607 * Fan subdriver
3611 * FAN ACCESS MODES
3613 * TPACPI_FAN_RD_ACPI_GFAN:
3614 * ACPI GFAN method: returns fan level
3616 * see TPACPI_FAN_WR_ACPI_SFAN
3617 * EC 0x2f (HFSP) not available if GFAN exists
3619 * TPACPI_FAN_WR_ACPI_SFAN:
3620 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3622 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3623 * it for writing.
3625 * TPACPI_FAN_WR_TPEC:
3626 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3627 * Supported on almost all ThinkPads
3629 * Fan speed changes of any sort (including those caused by the
3630 * disengaged mode) are usually done slowly by the firmware as the
3631 * maximum ammount of fan duty cycle change per second seems to be
3632 * limited.
3634 * Reading is not available if GFAN exists.
3635 * Writing is not available if SFAN exists.
3637 * Bits
3638 * 7 automatic mode engaged;
3639 * (default operation mode of the ThinkPad)
3640 * fan level is ignored in this mode.
3641 * 6 full speed mode (takes precedence over bit 7);
3642 * not available on all thinkpads. May disable
3643 * the tachometer while the fan controller ramps up
3644 * the speed (which can take up to a few *minutes*).
3645 * Speeds up fan to 100% duty-cycle, which is far above
3646 * the standard RPM levels. It is not impossible that
3647 * it could cause hardware damage.
3648 * 5-3 unused in some models. Extra bits for fan level
3649 * in others, but still useless as all values above
3650 * 7 map to the same speed as level 7 in these models.
3651 * 2-0 fan level (0..7 usually)
3652 * 0x00 = stop
3653 * 0x07 = max (set when temperatures critical)
3654 * Some ThinkPads may have other levels, see
3655 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3657 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3658 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3659 * does so, its initial value is meaningless (0x07).
3661 * For firmware bugs, refer to:
3662 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3664 * ----
3666 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3667 * Main fan tachometer reading (in RPM)
3669 * This register is present on all ThinkPads with a new-style EC, and
3670 * it is known not to be present on the A21m/e, and T22, as there is
3671 * something else in offset 0x84 according to the ACPI DSDT. Other
3672 * ThinkPads from this same time period (and earlier) probably lack the
3673 * tachometer as well.
3675 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3676 * was never fixed by IBM to report the EC firmware version string
3677 * probably support the tachometer (like the early X models), so
3678 * detecting it is quite hard. We need more data to know for sure.
3680 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3681 * might result.
3683 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3684 * mode.
3686 * For firmware bugs, refer to:
3687 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3689 * TPACPI_FAN_WR_ACPI_FANS:
3690 * ThinkPad X31, X40, X41. Not available in the X60.
3692 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3693 * high speed. ACPI DSDT seems to map these three speeds to levels
3694 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3695 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3697 * The speeds are stored on handles
3698 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3700 * There are three default speed sets, acessible as handles:
3701 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3703 * ACPI DSDT switches which set is in use depending on various
3704 * factors.
3706 * TPACPI_FAN_WR_TPEC is also available and should be used to
3707 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3708 * but the ACPI tables just mention level 7.
3711 static enum fan_status_access_mode fan_status_access_mode;
3712 static enum fan_control_access_mode fan_control_access_mode;
3713 static enum fan_control_commands fan_control_commands;
3715 static u8 fan_control_initial_status;
3716 static u8 fan_control_desired_level;
3718 static void fan_watchdog_fire(struct work_struct *ignored);
3719 static int fan_watchdog_maxinterval;
3720 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3722 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3723 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3724 "\\FSPD", /* 600e/x, 770e, 770x */
3725 ); /* all others */
3726 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3727 "JFNS", /* 770x-JL */
3728 ); /* all others */
3731 * SYSFS fan layout: hwmon compatible (device)
3733 * pwm*_enable:
3734 * 0: "disengaged" mode
3735 * 1: manual mode
3736 * 2: native EC "auto" mode (recommended, hardware default)
3738 * pwm*: set speed in manual mode, ignored otherwise.
3739 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3740 * interpolation.
3742 * fan*_input: tachometer reading, RPM
3745 * SYSFS fan layout: extensions
3747 * fan_watchdog (driver):
3748 * fan watchdog interval in seconds, 0 disables (default), max 120
3751 /* sysfs fan pwm1_enable ----------------------------------------------- */
3752 static ssize_t fan_pwm1_enable_show(struct device *dev,
3753 struct device_attribute *attr,
3754 char *buf)
3756 int res, mode;
3757 u8 status;
3759 res = fan_get_status_safe(&status);
3760 if (res)
3761 return res;
3763 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3764 if (status != fan_control_initial_status) {
3765 tp_features.fan_ctrl_status_undef = 0;
3766 } else {
3767 /* Return most likely status. In fact, it
3768 * might be the only possible status */
3769 status = TP_EC_FAN_AUTO;
3773 if (status & TP_EC_FAN_FULLSPEED) {
3774 mode = 0;
3775 } else if (status & TP_EC_FAN_AUTO) {
3776 mode = 2;
3777 } else
3778 mode = 1;
3780 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3783 static ssize_t fan_pwm1_enable_store(struct device *dev,
3784 struct device_attribute *attr,
3785 const char *buf, size_t count)
3787 unsigned long t;
3788 int res, level;
3790 if (parse_strtoul(buf, 2, &t))
3791 return -EINVAL;
3793 switch (t) {
3794 case 0:
3795 level = TP_EC_FAN_FULLSPEED;
3796 break;
3797 case 1:
3798 level = TPACPI_FAN_LAST_LEVEL;
3799 break;
3800 case 2:
3801 level = TP_EC_FAN_AUTO;
3802 break;
3803 case 3:
3804 /* reserved for software-controlled auto mode */
3805 return -ENOSYS;
3806 default:
3807 return -EINVAL;
3810 res = fan_set_level_safe(level);
3811 if (res == -ENXIO)
3812 return -EINVAL;
3813 else if (res < 0)
3814 return res;
3816 fan_watchdog_reset();
3818 return count;
3821 static struct device_attribute dev_attr_fan_pwm1_enable =
3822 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3823 fan_pwm1_enable_show, fan_pwm1_enable_store);
3825 /* sysfs fan pwm1 ------------------------------------------------------ */
3826 static ssize_t fan_pwm1_show(struct device *dev,
3827 struct device_attribute *attr,
3828 char *buf)
3830 int res;
3831 u8 status;
3833 res = fan_get_status_safe(&status);
3834 if (res)
3835 return res;
3837 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3838 if (status != fan_control_initial_status) {
3839 tp_features.fan_ctrl_status_undef = 0;
3840 } else {
3841 status = TP_EC_FAN_AUTO;
3845 if ((status &
3846 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3847 status = fan_control_desired_level;
3849 if (status > 7)
3850 status = 7;
3852 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3855 static ssize_t fan_pwm1_store(struct device *dev,
3856 struct device_attribute *attr,
3857 const char *buf, size_t count)
3859 unsigned long s;
3860 int rc;
3861 u8 status, newlevel;
3863 if (parse_strtoul(buf, 255, &s))
3864 return -EINVAL;
3866 /* scale down from 0-255 to 0-7 */
3867 newlevel = (s >> 5) & 0x07;
3869 if (mutex_lock_interruptible(&fan_mutex))
3870 return -ERESTARTSYS;
3872 rc = fan_get_status(&status);
3873 if (!rc && (status &
3874 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3875 rc = fan_set_level(newlevel);
3876 if (rc == -ENXIO)
3877 rc = -EINVAL;
3878 else if (!rc) {
3879 fan_update_desired_level(newlevel);
3880 fan_watchdog_reset();
3884 mutex_unlock(&fan_mutex);
3885 return (rc)? rc : count;
3888 static struct device_attribute dev_attr_fan_pwm1 =
3889 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3890 fan_pwm1_show, fan_pwm1_store);
3892 /* sysfs fan fan1_input ------------------------------------------------ */
3893 static ssize_t fan_fan1_input_show(struct device *dev,
3894 struct device_attribute *attr,
3895 char *buf)
3897 int res;
3898 unsigned int speed;
3900 res = fan_get_speed(&speed);
3901 if (res < 0)
3902 return res;
3904 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3907 static struct device_attribute dev_attr_fan_fan1_input =
3908 __ATTR(fan1_input, S_IRUGO,
3909 fan_fan1_input_show, NULL);
3911 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
3912 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3913 char *buf)
3915 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3918 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3919 const char *buf, size_t count)
3921 unsigned long t;
3923 if (parse_strtoul(buf, 120, &t))
3924 return -EINVAL;
3926 if (!fan_control_allowed)
3927 return -EPERM;
3929 fan_watchdog_maxinterval = t;
3930 fan_watchdog_reset();
3932 return count;
3935 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3936 fan_fan_watchdog_show, fan_fan_watchdog_store);
3938 /* --------------------------------------------------------------------- */
3939 static struct attribute *fan_attributes[] = {
3940 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3941 &dev_attr_fan_fan1_input.attr,
3942 NULL
3945 static const struct attribute_group fan_attr_group = {
3946 .attrs = fan_attributes,
3949 static int __init fan_init(struct ibm_init_struct *iibm)
3951 int rc;
3953 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3955 mutex_init(&fan_mutex);
3956 fan_status_access_mode = TPACPI_FAN_NONE;
3957 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3958 fan_control_commands = 0;
3959 fan_watchdog_maxinterval = 0;
3960 tp_features.fan_ctrl_status_undef = 0;
3961 fan_control_desired_level = 7;
3963 IBM_ACPIHANDLE_INIT(fans);
3964 IBM_ACPIHANDLE_INIT(gfan);
3965 IBM_ACPIHANDLE_INIT(sfan);
3967 if (gfan_handle) {
3968 /* 570, 600e/x, 770e, 770x */
3969 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3970 } else {
3971 /* all other ThinkPads: note that even old-style
3972 * ThinkPad ECs supports the fan control register */
3973 if (likely(acpi_ec_read(fan_status_offset,
3974 &fan_control_initial_status))) {
3975 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3977 /* In some ThinkPads, neither the EC nor the ACPI
3978 * DSDT initialize the fan status, and it ends up
3979 * being set to 0x07 when it *could* be either
3980 * 0x07 or 0x80.
3982 * Enable for TP-1Y (T43), TP-78 (R51e),
3983 * TP-76 (R52), TP-70 (T43, R52), which are known
3984 * to be buggy. */
3985 if (fan_control_initial_status == 0x07) {
3986 switch (thinkpad_id.ec_model) {
3987 case 0x5931: /* TP-1Y */
3988 case 0x3837: /* TP-78 */
3989 case 0x3637: /* TP-76 */
3990 case 0x3037: /* TP-70 */
3991 printk(IBM_NOTICE
3992 "fan_init: initial fan status is "
3993 "unknown, assuming it is in auto "
3994 "mode\n");
3995 tp_features.fan_ctrl_status_undef = 1;
3999 } else {
4000 printk(IBM_ERR
4001 "ThinkPad ACPI EC access misbehaving, "
4002 "fan status and control unavailable\n");
4003 return 1;
4007 if (sfan_handle) {
4008 /* 570, 770x-JL */
4009 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
4010 fan_control_commands |=
4011 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
4012 } else {
4013 if (!gfan_handle) {
4014 /* gfan without sfan means no fan control */
4015 /* all other models implement TP EC 0x2f control */
4017 if (fans_handle) {
4018 /* X31, X40, X41 */
4019 fan_control_access_mode =
4020 TPACPI_FAN_WR_ACPI_FANS;
4021 fan_control_commands |=
4022 TPACPI_FAN_CMD_SPEED |
4023 TPACPI_FAN_CMD_LEVEL |
4024 TPACPI_FAN_CMD_ENABLE;
4025 } else {
4026 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
4027 fan_control_commands |=
4028 TPACPI_FAN_CMD_LEVEL |
4029 TPACPI_FAN_CMD_ENABLE;
4034 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
4035 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
4036 fan_control_access_mode != TPACPI_FAN_WR_NONE),
4037 fan_status_access_mode, fan_control_access_mode);
4039 /* fan control master switch */
4040 if (!fan_control_allowed) {
4041 fan_control_access_mode = TPACPI_FAN_WR_NONE;
4042 fan_control_commands = 0;
4043 dbg_printk(TPACPI_DBG_INIT,
4044 "fan control features disabled by parameter\n");
4047 /* update fan_control_desired_level */
4048 if (fan_status_access_mode != TPACPI_FAN_NONE)
4049 fan_get_status_safe(NULL);
4051 if (fan_status_access_mode != TPACPI_FAN_NONE ||
4052 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
4053 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4054 &fan_attr_group);
4055 if (!(rc < 0))
4056 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
4057 &driver_attr_fan_watchdog);
4058 if (rc < 0)
4059 return rc;
4060 return 0;
4061 } else
4062 return 1;
4066 * Call with fan_mutex held
4068 static void fan_update_desired_level(u8 status)
4070 if ((status &
4071 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4072 if (status > 7)
4073 fan_control_desired_level = 7;
4074 else
4075 fan_control_desired_level = status;
4079 static int fan_get_status(u8 *status)
4081 u8 s;
4083 /* TODO:
4084 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4086 switch (fan_status_access_mode) {
4087 case TPACPI_FAN_RD_ACPI_GFAN:
4088 /* 570, 600e/x, 770e, 770x */
4090 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4091 return -EIO;
4093 if (likely(status))
4094 *status = s & 0x07;
4096 break;
4098 case TPACPI_FAN_RD_TPEC:
4099 /* all except 570, 600e/x, 770e, 770x */
4100 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4101 return -EIO;
4103 if (likely(status))
4104 *status = s;
4106 break;
4108 default:
4109 return -ENXIO;
4112 return 0;
4115 static int fan_get_status_safe(u8 *status)
4117 int rc;
4118 u8 s;
4120 if (mutex_lock_interruptible(&fan_mutex))
4121 return -ERESTARTSYS;
4122 rc = fan_get_status(&s);
4123 if (!rc)
4124 fan_update_desired_level(s);
4125 mutex_unlock(&fan_mutex);
4127 if (status)
4128 *status = s;
4130 return rc;
4133 static void fan_exit(void)
4135 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
4137 /* FIXME: can we really do this unconditionally? */
4138 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
4139 driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
4141 cancel_delayed_work(&fan_watchdog_task);
4142 flush_scheduled_work();
4145 static int fan_get_speed(unsigned int *speed)
4147 u8 hi, lo;
4149 switch (fan_status_access_mode) {
4150 case TPACPI_FAN_RD_TPEC:
4151 /* all except 570, 600e/x, 770e, 770x */
4152 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4153 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4154 return -EIO;
4156 if (likely(speed))
4157 *speed = (hi << 8) | lo;
4159 break;
4161 default:
4162 return -ENXIO;
4165 return 0;
4168 static void fan_watchdog_fire(struct work_struct *ignored)
4170 int rc;
4172 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4173 return;
4175 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
4176 rc = fan_set_enable();
4177 if (rc < 0) {
4178 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
4179 "will try again later...\n", -rc);
4180 /* reschedule for later */
4181 fan_watchdog_reset();
4185 static void fan_watchdog_reset(void)
4187 static int fan_watchdog_active;
4189 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4190 return;
4192 if (fan_watchdog_active)
4193 cancel_delayed_work(&fan_watchdog_task);
4195 if (fan_watchdog_maxinterval > 0 &&
4196 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4197 fan_watchdog_active = 1;
4198 if (!schedule_delayed_work(&fan_watchdog_task,
4199 msecs_to_jiffies(fan_watchdog_maxinterval
4200 * 1000))) {
4201 printk(IBM_ERR "failed to schedule the fan watchdog, "
4202 "watchdog will not trigger\n");
4204 } else
4205 fan_watchdog_active = 0;
4208 static int fan_set_level(int level)
4210 if (!fan_control_allowed)
4211 return -EPERM;
4213 switch (fan_control_access_mode) {
4214 case TPACPI_FAN_WR_ACPI_SFAN:
4215 if (level >= 0 && level <= 7) {
4216 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4217 return -EIO;
4218 } else
4219 return -EINVAL;
4220 break;
4222 case TPACPI_FAN_WR_ACPI_FANS:
4223 case TPACPI_FAN_WR_TPEC:
4224 if ((level != TP_EC_FAN_AUTO) &&
4225 (level != TP_EC_FAN_FULLSPEED) &&
4226 ((level < 0) || (level > 7)))
4227 return -EINVAL;
4229 /* safety net should the EC not support AUTO
4230 * or FULLSPEED mode bits and just ignore them */
4231 if (level & TP_EC_FAN_FULLSPEED)
4232 level |= 7; /* safety min speed 7 */
4233 else if (level & TP_EC_FAN_FULLSPEED)
4234 level |= 4; /* safety min speed 4 */
4236 if (!acpi_ec_write(fan_status_offset, level))
4237 return -EIO;
4238 else
4239 tp_features.fan_ctrl_status_undef = 0;
4240 break;
4242 default:
4243 return -ENXIO;
4245 return 0;
4248 static int fan_set_level_safe(int level)
4250 int rc;
4252 if (!fan_control_allowed)
4253 return -EPERM;
4255 if (mutex_lock_interruptible(&fan_mutex))
4256 return -ERESTARTSYS;
4258 if (level == TPACPI_FAN_LAST_LEVEL)
4259 level = fan_control_desired_level;
4261 rc = fan_set_level(level);
4262 if (!rc)
4263 fan_update_desired_level(level);
4265 mutex_unlock(&fan_mutex);
4266 return rc;
4269 static int fan_set_enable(void)
4271 u8 s;
4272 int rc;
4274 if (!fan_control_allowed)
4275 return -EPERM;
4277 if (mutex_lock_interruptible(&fan_mutex))
4278 return -ERESTARTSYS;
4280 switch (fan_control_access_mode) {
4281 case TPACPI_FAN_WR_ACPI_FANS:
4282 case TPACPI_FAN_WR_TPEC:
4283 rc = fan_get_status(&s);
4284 if (rc < 0)
4285 break;
4287 /* Don't go out of emergency fan mode */
4288 if (s != 7) {
4289 s &= 0x07;
4290 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4293 if (!acpi_ec_write(fan_status_offset, s))
4294 rc = -EIO;
4295 else {
4296 tp_features.fan_ctrl_status_undef = 0;
4297 rc = 0;
4299 break;
4301 case TPACPI_FAN_WR_ACPI_SFAN:
4302 rc = fan_get_status(&s);
4303 if (rc < 0)
4304 break;
4306 s &= 0x07;
4308 /* Set fan to at least level 4 */
4309 s |= 4;
4311 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4312 rc= -EIO;
4313 else
4314 rc = 0;
4315 break;
4317 default:
4318 rc = -ENXIO;
4321 mutex_unlock(&fan_mutex);
4322 return rc;
4325 static int fan_set_disable(void)
4327 int rc;
4329 if (!fan_control_allowed)
4330 return -EPERM;
4332 if (mutex_lock_interruptible(&fan_mutex))
4333 return -ERESTARTSYS;
4335 rc = 0;
4336 switch (fan_control_access_mode) {
4337 case TPACPI_FAN_WR_ACPI_FANS:
4338 case TPACPI_FAN_WR_TPEC:
4339 if (!acpi_ec_write(fan_status_offset, 0x00))
4340 rc = -EIO;
4341 else {
4342 fan_control_desired_level = 0;
4343 tp_features.fan_ctrl_status_undef = 0;
4345 break;
4347 case TPACPI_FAN_WR_ACPI_SFAN:
4348 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4349 rc = -EIO;
4350 else
4351 fan_control_desired_level = 0;
4352 break;
4354 default:
4355 rc = -ENXIO;
4359 mutex_unlock(&fan_mutex);
4360 return rc;
4363 static int fan_set_speed(int speed)
4365 int rc;
4367 if (!fan_control_allowed)
4368 return -EPERM;
4370 if (mutex_lock_interruptible(&fan_mutex))
4371 return -ERESTARTSYS;
4373 rc = 0;
4374 switch (fan_control_access_mode) {
4375 case TPACPI_FAN_WR_ACPI_FANS:
4376 if (speed >= 0 && speed <= 65535) {
4377 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4378 speed, speed, speed))
4379 rc = -EIO;
4380 } else
4381 rc = -EINVAL;
4382 break;
4384 default:
4385 rc = -ENXIO;
4388 mutex_unlock(&fan_mutex);
4389 return rc;
4392 static int fan_read(char *p)
4394 int len = 0;
4395 int rc;
4396 u8 status;
4397 unsigned int speed = 0;
4399 switch (fan_status_access_mode) {
4400 case TPACPI_FAN_RD_ACPI_GFAN:
4401 /* 570, 600e/x, 770e, 770x */
4402 if ((rc = fan_get_status_safe(&status)) < 0)
4403 return rc;
4405 len += sprintf(p + len, "status:\t\t%s\n"
4406 "level:\t\t%d\n",
4407 (status != 0) ? "enabled" : "disabled", status);
4408 break;
4410 case TPACPI_FAN_RD_TPEC:
4411 /* all except 570, 600e/x, 770e, 770x */
4412 if ((rc = fan_get_status_safe(&status)) < 0)
4413 return rc;
4415 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4416 if (status != fan_control_initial_status)
4417 tp_features.fan_ctrl_status_undef = 0;
4418 else
4419 /* Return most likely status. In fact, it
4420 * might be the only possible status */
4421 status = TP_EC_FAN_AUTO;
4424 len += sprintf(p + len, "status:\t\t%s\n",
4425 (status != 0) ? "enabled" : "disabled");
4427 if ((rc = fan_get_speed(&speed)) < 0)
4428 return rc;
4430 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4432 if (status & TP_EC_FAN_FULLSPEED)
4433 /* Disengaged mode takes precedence */
4434 len += sprintf(p + len, "level:\t\tdisengaged\n");
4435 else if (status & TP_EC_FAN_AUTO)
4436 len += sprintf(p + len, "level:\t\tauto\n");
4437 else
4438 len += sprintf(p + len, "level:\t\t%d\n", status);
4439 break;
4441 case TPACPI_FAN_NONE:
4442 default:
4443 len += sprintf(p + len, "status:\t\tnot supported\n");
4446 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
4447 len += sprintf(p + len, "commands:\tlevel <level>");
4449 switch (fan_control_access_mode) {
4450 case TPACPI_FAN_WR_ACPI_SFAN:
4451 len += sprintf(p + len, " (<level> is 0-7)\n");
4452 break;
4454 default:
4455 len += sprintf(p + len, " (<level> is 0-7, "
4456 "auto, disengaged, full-speed)\n");
4457 break;
4461 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
4462 len += sprintf(p + len, "commands:\tenable, disable\n"
4463 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4464 "1-120 (seconds))\n");
4466 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
4467 len += sprintf(p + len, "commands:\tspeed <speed>"
4468 " (<speed> is 0-65535)\n");
4470 return len;
4473 static int fan_write_cmd_level(const char *cmd, int *rc)
4475 int level;
4477 if (strlencmp(cmd, "level auto") == 0)
4478 level = TP_EC_FAN_AUTO;
4479 else if ((strlencmp(cmd, "level disengaged") == 0) |
4480 (strlencmp(cmd, "level full-speed") == 0))
4481 level = TP_EC_FAN_FULLSPEED;
4482 else if (sscanf(cmd, "level %d", &level) != 1)
4483 return 0;
4485 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4486 printk(IBM_ERR "level command accepted for unsupported "
4487 "access mode %d", fan_control_access_mode);
4489 return 1;
4492 static int fan_write_cmd_enable(const char *cmd, int *rc)
4494 if (strlencmp(cmd, "enable") != 0)
4495 return 0;
4497 if ((*rc = fan_set_enable()) == -ENXIO)
4498 printk(IBM_ERR "enable command accepted for unsupported "
4499 "access mode %d", fan_control_access_mode);
4501 return 1;
4504 static int fan_write_cmd_disable(const char *cmd, int *rc)
4506 if (strlencmp(cmd, "disable") != 0)
4507 return 0;
4509 if ((*rc = fan_set_disable()) == -ENXIO)
4510 printk(IBM_ERR "disable command accepted for unsupported "
4511 "access mode %d", fan_control_access_mode);
4513 return 1;
4516 static int fan_write_cmd_speed(const char *cmd, int *rc)
4518 int speed;
4520 /* TODO:
4521 * Support speed <low> <medium> <high> ? */
4523 if (sscanf(cmd, "speed %d", &speed) != 1)
4524 return 0;
4526 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4527 printk(IBM_ERR "speed command accepted for unsupported "
4528 "access mode %d", fan_control_access_mode);
4530 return 1;
4533 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4535 int interval;
4537 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4538 return 0;
4540 if (interval < 0 || interval > 120)
4541 *rc = -EINVAL;
4542 else
4543 fan_watchdog_maxinterval = interval;
4545 return 1;
4548 static int fan_write(char *buf)
4550 char *cmd;
4551 int rc = 0;
4553 while (!rc && (cmd = next_cmd(&buf))) {
4554 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4555 fan_write_cmd_level(cmd, &rc)) &&
4556 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4557 (fan_write_cmd_enable(cmd, &rc) ||
4558 fan_write_cmd_disable(cmd, &rc) ||
4559 fan_write_cmd_watchdog(cmd, &rc))) &&
4560 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4561 fan_write_cmd_speed(cmd, &rc))
4563 rc = -EINVAL;
4564 else if (!rc)
4565 fan_watchdog_reset();
4568 return rc;
4571 static struct ibm_struct fan_driver_data = {
4572 .name = "fan",
4573 .read = fan_read,
4574 .write = fan_write,
4575 .exit = fan_exit,
4578 /****************************************************************************
4579 ****************************************************************************
4581 * Infrastructure
4583 ****************************************************************************
4584 ****************************************************************************/
4586 /* sysfs name ---------------------------------------------------------- */
4587 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
4588 struct device_attribute *attr,
4589 char *buf)
4591 return snprintf(buf, PAGE_SIZE, "%s\n", IBM_NAME);
4594 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
4595 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
4597 /* --------------------------------------------------------------------- */
4599 /* /proc support */
4600 static struct proc_dir_entry *proc_dir;
4602 /* Subdriver registry */
4603 static LIST_HEAD(tpacpi_all_drivers);
4607 * Module and infrastructure proble, init and exit handling
4610 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4611 static const char * __init str_supported(int is_supported)
4613 static char text_unsupported[] __initdata = "not supported";
4615 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4617 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4619 static int __init ibm_init(struct ibm_init_struct *iibm)
4621 int ret;
4622 struct ibm_struct *ibm = iibm->data;
4623 struct proc_dir_entry *entry;
4625 BUG_ON(ibm == NULL);
4627 INIT_LIST_HEAD(&ibm->all_drivers);
4629 if (ibm->flags.experimental && !experimental)
4630 return 0;
4632 dbg_printk(TPACPI_DBG_INIT,
4633 "probing for %s\n", ibm->name);
4635 if (iibm->init) {
4636 ret = iibm->init(iibm);
4637 if (ret > 0)
4638 return 0; /* probe failed */
4639 if (ret)
4640 return ret;
4642 ibm->flags.init_called = 1;
4645 if (ibm->acpi) {
4646 if (ibm->acpi->hid) {
4647 ret = register_tpacpi_subdriver(ibm);
4648 if (ret)
4649 goto err_out;
4652 if (ibm->acpi->notify) {
4653 ret = setup_acpi_notify(ibm);
4654 if (ret == -ENODEV) {
4655 printk(IBM_NOTICE "disabling subdriver %s\n",
4656 ibm->name);
4657 ret = 0;
4658 goto err_out;
4660 if (ret < 0)
4661 goto err_out;
4665 dbg_printk(TPACPI_DBG_INIT,
4666 "%s installed\n", ibm->name);
4668 if (ibm->read) {
4669 entry = create_proc_entry(ibm->name,
4670 S_IFREG | S_IRUGO | S_IWUSR,
4671 proc_dir);
4672 if (!entry) {
4673 printk(IBM_ERR "unable to create proc entry %s\n",
4674 ibm->name);
4675 ret = -ENODEV;
4676 goto err_out;
4678 entry->owner = THIS_MODULE;
4679 entry->data = ibm;
4680 entry->read_proc = &dispatch_procfs_read;
4681 if (ibm->write)
4682 entry->write_proc = &dispatch_procfs_write;
4683 ibm->flags.proc_created = 1;
4686 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4688 return 0;
4690 err_out:
4691 dbg_printk(TPACPI_DBG_INIT,
4692 "%s: at error exit path with result %d\n",
4693 ibm->name, ret);
4695 ibm_exit(ibm);
4696 return (ret < 0)? ret : 0;
4699 static void ibm_exit(struct ibm_struct *ibm)
4701 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4703 list_del_init(&ibm->all_drivers);
4705 if (ibm->flags.acpi_notify_installed) {
4706 dbg_printk(TPACPI_DBG_EXIT,
4707 "%s: acpi_remove_notify_handler\n", ibm->name);
4708 BUG_ON(!ibm->acpi);
4709 acpi_remove_notify_handler(*ibm->acpi->handle,
4710 ibm->acpi->type,
4711 dispatch_acpi_notify);
4712 ibm->flags.acpi_notify_installed = 0;
4713 ibm->flags.acpi_notify_installed = 0;
4716 if (ibm->flags.proc_created) {
4717 dbg_printk(TPACPI_DBG_EXIT,
4718 "%s: remove_proc_entry\n", ibm->name);
4719 remove_proc_entry(ibm->name, proc_dir);
4720 ibm->flags.proc_created = 0;
4723 if (ibm->flags.acpi_driver_registered) {
4724 dbg_printk(TPACPI_DBG_EXIT,
4725 "%s: acpi_bus_unregister_driver\n", ibm->name);
4726 BUG_ON(!ibm->acpi);
4727 acpi_bus_unregister_driver(ibm->acpi->driver);
4728 kfree(ibm->acpi->driver);
4729 ibm->acpi->driver = NULL;
4730 ibm->flags.acpi_driver_registered = 0;
4733 if (ibm->flags.init_called && ibm->exit) {
4734 ibm->exit();
4735 ibm->flags.init_called = 0;
4738 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4741 /* Probing */
4743 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
4745 const struct dmi_device *dev = NULL;
4746 char ec_fw_string[18];
4748 if (!tp)
4749 return;
4751 memset(tp, 0, sizeof(*tp));
4753 if (dmi_name_in_vendors("IBM"))
4754 tp->vendor = PCI_VENDOR_ID_IBM;
4755 else if (dmi_name_in_vendors("LENOVO"))
4756 tp->vendor = PCI_VENDOR_ID_LENOVO;
4757 else
4758 return;
4760 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4761 GFP_KERNEL);
4762 if (!tp->bios_version_str)
4763 return;
4764 tp->bios_model = tp->bios_version_str[0]
4765 | (tp->bios_version_str[1] << 8);
4768 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4769 * X32 or newer, all Z series; Some models must have an
4770 * up-to-date BIOS or they will not be detected.
4772 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4774 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4775 if (sscanf(dev->name,
4776 "IBM ThinkPad Embedded Controller -[%17c",
4777 ec_fw_string) == 1) {
4778 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4779 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4781 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4782 tp->ec_model = ec_fw_string[0]
4783 | (ec_fw_string[1] << 8);
4784 break;
4788 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4789 GFP_KERNEL);
4790 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4791 kfree(tp->model_str);
4792 tp->model_str = NULL;
4796 static int __init probe_for_thinkpad(void)
4798 int is_thinkpad;
4800 if (acpi_disabled)
4801 return -ENODEV;
4804 * Non-ancient models have better DMI tagging, but very old models
4805 * don't.
4807 is_thinkpad = (thinkpad_id.model_str != NULL);
4809 /* ec is required because many other handles are relative to it */
4810 IBM_ACPIHANDLE_INIT(ec);
4811 if (!ec_handle) {
4812 if (is_thinkpad)
4813 printk(IBM_ERR
4814 "Not yet supported ThinkPad detected!\n");
4815 return -ENODEV;
4819 * Risks a regression on very old machines, but reduces potential
4820 * false positives a damn great deal
4822 if (!is_thinkpad)
4823 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
4825 if (!is_thinkpad && !force_load)
4826 return -ENODEV;
4828 return 0;
4832 /* Module init, exit, parameters */
4834 static struct ibm_init_struct ibms_init[] __initdata = {
4836 .init = thinkpad_acpi_driver_init,
4837 .data = &thinkpad_acpi_driver_data,
4840 .init = hotkey_init,
4841 .data = &hotkey_driver_data,
4844 .init = bluetooth_init,
4845 .data = &bluetooth_driver_data,
4848 .init = wan_init,
4849 .data = &wan_driver_data,
4852 .init = video_init,
4853 .data = &video_driver_data,
4856 .init = light_init,
4857 .data = &light_driver_data,
4859 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4861 .init = dock_init,
4862 .data = &dock_driver_data[0],
4865 .init = dock_init2,
4866 .data = &dock_driver_data[1],
4868 #endif
4869 #ifdef CONFIG_THINKPAD_ACPI_BAY
4871 .init = bay_init,
4872 .data = &bay_driver_data,
4874 #endif
4876 .init = cmos_init,
4877 .data = &cmos_driver_data,
4880 .init = led_init,
4881 .data = &led_driver_data,
4884 .init = beep_init,
4885 .data = &beep_driver_data,
4888 .init = thermal_init,
4889 .data = &thermal_driver_data,
4892 .data = &ecdump_driver_data,
4895 .init = brightness_init,
4896 .data = &brightness_driver_data,
4899 .data = &volume_driver_data,
4902 .init = fan_init,
4903 .data = &fan_driver_data,
4907 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4909 unsigned int i;
4910 struct ibm_struct *ibm;
4912 if (!kp || !kp->name || !val)
4913 return -EINVAL;
4915 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4916 ibm = ibms_init[i].data;
4917 WARN_ON(ibm == NULL);
4919 if (!ibm || !ibm->name)
4920 continue;
4922 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4923 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4924 return -ENOSPC;
4925 strcpy(ibms_init[i].param, val);
4926 strcat(ibms_init[i].param, ",");
4927 return 0;
4931 return -EINVAL;
4934 static int experimental;
4935 module_param(experimental, int, 0);
4937 static u32 dbg_level;
4938 module_param_named(debug, dbg_level, uint, 0);
4940 static int force_load;
4941 module_param(force_load, bool, 0);
4943 static int fan_control_allowed;
4944 module_param_named(fan_control, fan_control_allowed, bool, 0);
4946 static int brightness_mode;
4947 module_param_named(brightness_mode, brightness_mode, int, 0);
4949 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4950 module_param(brightness_enable, uint, 0);
4952 static unsigned int hotkey_report_mode;
4953 module_param(hotkey_report_mode, uint, 0);
4955 #define IBM_PARAM(feature) \
4956 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4958 IBM_PARAM(hotkey);
4959 IBM_PARAM(bluetooth);
4960 IBM_PARAM(video);
4961 IBM_PARAM(light);
4962 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4963 IBM_PARAM(dock);
4964 #endif
4965 #ifdef CONFIG_THINKPAD_ACPI_BAY
4966 IBM_PARAM(bay);
4967 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4968 IBM_PARAM(cmos);
4969 IBM_PARAM(led);
4970 IBM_PARAM(beep);
4971 IBM_PARAM(ecdump);
4972 IBM_PARAM(brightness);
4973 IBM_PARAM(volume);
4974 IBM_PARAM(fan);
4976 static int __init thinkpad_acpi_module_init(void)
4978 int ret, i;
4980 tpacpi_lifecycle = TPACPI_LIFE_INIT;
4982 /* Parameter checking */
4983 if (hotkey_report_mode > 2)
4984 return -EINVAL;
4986 /* Driver-level probe */
4988 get_thinkpad_model_data(&thinkpad_id);
4989 ret = probe_for_thinkpad();
4990 if (ret) {
4991 thinkpad_acpi_module_exit();
4992 return ret;
4995 /* Driver initialization */
4997 IBM_ACPIHANDLE_INIT(ecrd);
4998 IBM_ACPIHANDLE_INIT(ecwr);
5000 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
5001 if (!proc_dir) {
5002 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
5003 thinkpad_acpi_module_exit();
5004 return -ENODEV;
5006 proc_dir->owner = THIS_MODULE;
5008 ret = platform_driver_register(&tpacpi_pdriver);
5009 if (ret) {
5010 printk(IBM_ERR "unable to register main platform driver\n");
5011 thinkpad_acpi_module_exit();
5012 return ret;
5014 tp_features.platform_drv_registered = 1;
5016 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
5017 if (ret) {
5018 printk(IBM_ERR "unable to register hwmon platform driver\n");
5019 thinkpad_acpi_module_exit();
5020 return ret;
5022 tp_features.sensors_pdrv_registered = 1;
5024 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
5025 if (!ret) {
5026 tp_features.platform_drv_attrs_registered = 1;
5027 ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5029 if (ret) {
5030 printk(IBM_ERR "unable to create sysfs driver attributes\n");
5031 thinkpad_acpi_module_exit();
5032 return ret;
5034 tp_features.sensors_pdrv_attrs_registered = 1;
5037 /* Device initialization */
5038 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
5039 NULL, 0);
5040 if (IS_ERR(tpacpi_pdev)) {
5041 ret = PTR_ERR(tpacpi_pdev);
5042 tpacpi_pdev = NULL;
5043 printk(IBM_ERR "unable to register platform device\n");
5044 thinkpad_acpi_module_exit();
5045 return ret;
5047 tpacpi_sensors_pdev = platform_device_register_simple(
5048 IBM_HWMON_DRVR_NAME,
5049 -1, NULL, 0);
5050 if (IS_ERR(tpacpi_sensors_pdev)) {
5051 ret = PTR_ERR(tpacpi_sensors_pdev);
5052 tpacpi_sensors_pdev = NULL;
5053 printk(IBM_ERR "unable to register hwmon platform device\n");
5054 thinkpad_acpi_module_exit();
5055 return ret;
5057 ret = device_create_file(&tpacpi_sensors_pdev->dev,
5058 &dev_attr_thinkpad_acpi_pdev_name);
5059 if (ret) {
5060 printk(IBM_ERR
5061 "unable to create sysfs hwmon device attributes\n");
5062 thinkpad_acpi_module_exit();
5063 return ret;
5065 tp_features.sensors_pdev_attrs_registered = 1;
5066 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
5067 if (IS_ERR(tpacpi_hwmon)) {
5068 ret = PTR_ERR(tpacpi_hwmon);
5069 tpacpi_hwmon = NULL;
5070 printk(IBM_ERR "unable to register hwmon device\n");
5071 thinkpad_acpi_module_exit();
5072 return ret;
5074 mutex_init(&tpacpi_inputdev_send_mutex);
5075 tpacpi_inputdev = input_allocate_device();
5076 if (!tpacpi_inputdev) {
5077 printk(IBM_ERR "unable to allocate input device\n");
5078 thinkpad_acpi_module_exit();
5079 return -ENOMEM;
5080 } else {
5081 /* Prepare input device, but don't register */
5082 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5083 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
5084 tpacpi_inputdev->id.bustype = BUS_HOST;
5085 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5086 thinkpad_id.vendor :
5087 PCI_VENDOR_ID_IBM;
5088 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5089 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5091 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5092 ret = ibm_init(&ibms_init[i]);
5093 if (ret >= 0 && *ibms_init[i].param)
5094 ret = ibms_init[i].data->write(ibms_init[i].param);
5095 if (ret < 0) {
5096 thinkpad_acpi_module_exit();
5097 return ret;
5100 ret = input_register_device(tpacpi_inputdev);
5101 if (ret < 0) {
5102 printk(IBM_ERR "unable to register input device\n");
5103 thinkpad_acpi_module_exit();
5104 return ret;
5105 } else {
5106 tp_features.input_device_registered = 1;
5109 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
5110 return 0;
5113 static void thinkpad_acpi_module_exit(void)
5115 struct ibm_struct *ibm, *itmp;
5117 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5119 list_for_each_entry_safe_reverse(ibm, itmp,
5120 &tpacpi_all_drivers,
5121 all_drivers) {
5122 ibm_exit(ibm);
5125 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5127 if (tpacpi_inputdev) {
5128 if (tp_features.input_device_registered)
5129 input_unregister_device(tpacpi_inputdev);
5130 else
5131 input_free_device(tpacpi_inputdev);
5134 if (tpacpi_hwmon)
5135 hwmon_device_unregister(tpacpi_hwmon);
5137 if (tp_features.sensors_pdev_attrs_registered)
5138 device_remove_file(&tpacpi_sensors_pdev->dev,
5139 &dev_attr_thinkpad_acpi_pdev_name);
5140 if (tpacpi_sensors_pdev)
5141 platform_device_unregister(tpacpi_sensors_pdev);
5142 if (tpacpi_pdev)
5143 platform_device_unregister(tpacpi_pdev);
5145 if (tp_features.sensors_pdrv_attrs_registered)
5146 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5147 if (tp_features.platform_drv_attrs_registered)
5148 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5150 if (tp_features.sensors_pdrv_registered)
5151 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5153 if (tp_features.platform_drv_registered)
5154 platform_driver_unregister(&tpacpi_pdriver);
5156 if (proc_dir)
5157 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
5159 kfree(thinkpad_id.bios_version_str);
5160 kfree(thinkpad_id.ec_version_str);
5161 kfree(thinkpad_id.model_str);
5164 module_init(thinkpad_acpi_module_init);
5165 module_exit(thinkpad_acpi_module_exit);