ACPI: thinkpad-acpi: add CMOS NVRAM polling for hot keys
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob3cd30bb6eb03b66847e34115fa74ac64a75c669c
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define IBM_VERSION "0.16"
25 #define TPACPI_SYSFS_VERSION 0x010000
28 * Changelog:
29 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
30 * drivers/misc.
32 * 2006-11-22 0.13 new maintainer
33 * changelog now lives in git commit history, and will
34 * not be updated further in-file.
36 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
37 * 2005-03-17 0.11 support for 600e, 770x
38 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
39 * support for 770e, G41
40 * G40 and G41 don't have a thinklight
41 * temperatures no longer experimental
42 * experimental brightness control
43 * experimental volume control
44 * experimental fan enable/disable
45 * 2005-01-16 0.10 fix module loading on R30, R31
46 * 2005-01-16 0.9 support for 570, R30, R31
47 * ultrabay support on A22p, A3x
48 * limit arg for cmos, led, beep, drop experimental status
49 * more capable led control on A21e, A22p, T20-22, X20
50 * experimental temperatures and fan speed
51 * experimental embedded controller register dump
52 * mark more functions as __init, drop incorrect __exit
53 * use MODULE_VERSION
54 * thanks to Henrik Brix Andersen <brix@gentoo.org>
55 * fix parameter passing on module loading
56 * thanks to Rusty Russell <rusty@rustcorp.com.au>
57 * thanks to Jim Radford <radford@blackbean.org>
58 * 2004-11-08 0.8 fix init error case, don't return from a macro
59 * thanks to Chris Wright <chrisw@osdl.org>
60 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
61 * fix led control on A21e
62 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
63 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
64 * proc file format changed
65 * video_switch command
66 * experimental cmos control
67 * experimental led control
68 * experimental acpi sounds
69 * 2004-09-16 0.4 support for module parameters
70 * hotkey mask can be prefixed by 0x
71 * video output switching
72 * video expansion control
73 * ultrabay eject support
74 * removed lcd brightness/on/off control, didn't work
75 * 2004-08-17 0.3 support for R40
76 * lcd off, brightness control
77 * thinklight on/off
78 * 2004-08-14 0.2 support for T series, X20
79 * bluetooth enable/disable
80 * hotkey events disabled by default
81 * removed fan control, currently useless
82 * 2004-08-09 0.1 initial release, support for X series
85 #include "thinkpad_acpi.h"
87 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
88 MODULE_DESCRIPTION(IBM_DESC);
89 MODULE_VERSION(IBM_VERSION);
90 MODULE_LICENSE("GPL");
92 /* Please remove this in year 2009 */
93 MODULE_ALIAS("ibm_acpi");
96 * DMI matching for module autoloading
98 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
99 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
101 * Only models listed in thinkwiki will be supported, so add yours
102 * if it is not there yet.
104 #define IBM_BIOS_MODULE_ALIAS(__type) \
105 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
107 /* Non-ancient thinkpads */
108 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
109 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
111 /* Ancient thinkpad BIOSes have to be identified by
112 * BIOS type or model number, and there are far less
113 * BIOS types than model numbers... */
114 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
115 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
116 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
118 #define __unused __attribute__ ((unused))
120 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 class_device *tpacpi_hwmon;
530 static struct input_dev *tpacpi_inputdev;
531 static struct mutex tpacpi_inputdev_send_mutex;
534 static int tpacpi_resume_handler(struct platform_device *pdev)
536 struct ibm_struct *ibm, *itmp;
538 list_for_each_entry_safe(ibm, itmp,
539 &tpacpi_all_drivers,
540 all_drivers) {
541 if (ibm->resume)
542 (ibm->resume)();
545 return 0;
548 static struct platform_driver tpacpi_pdriver = {
549 .driver = {
550 .name = IBM_DRVR_NAME,
551 .owner = THIS_MODULE,
553 .resume = tpacpi_resume_handler,
557 /*************************************************************************
558 * thinkpad-acpi driver attributes
561 /* interface_version --------------------------------------------------- */
562 static ssize_t tpacpi_driver_interface_version_show(
563 struct device_driver *drv,
564 char *buf)
566 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
569 static DRIVER_ATTR(interface_version, S_IRUGO,
570 tpacpi_driver_interface_version_show, NULL);
572 /* debug_level --------------------------------------------------------- */
573 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
574 char *buf)
576 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
579 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
580 const char *buf, size_t count)
582 unsigned long t;
584 if (parse_strtoul(buf, 0xffff, &t))
585 return -EINVAL;
587 dbg_level = t;
589 return count;
592 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
593 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
595 /* version ------------------------------------------------------------- */
596 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
597 char *buf)
599 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
602 static DRIVER_ATTR(version, S_IRUGO,
603 tpacpi_driver_version_show, NULL);
605 /* --------------------------------------------------------------------- */
607 static struct driver_attribute* tpacpi_driver_attributes[] = {
608 &driver_attr_debug_level, &driver_attr_version,
609 &driver_attr_interface_version,
612 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
614 int i, res;
616 i = 0;
617 res = 0;
618 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
619 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
620 i++;
623 return res;
626 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
628 int i;
630 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
631 driver_remove_file(drv, tpacpi_driver_attributes[i]);
634 /*************************************************************************
635 * sysfs support helpers
638 struct attribute_set_obj {
639 struct attribute_set s;
640 struct attribute *a;
641 } __attribute__((packed));
643 static struct attribute_set *create_attr_set(unsigned int max_members,
644 const char* name)
646 struct attribute_set_obj *sobj;
648 if (max_members == 0)
649 return NULL;
651 /* Allocates space for implicit NULL at the end too */
652 sobj = kzalloc(sizeof(struct attribute_set_obj) +
653 max_members * sizeof(struct attribute *),
654 GFP_KERNEL);
655 if (!sobj)
656 return NULL;
657 sobj->s.max_members = max_members;
658 sobj->s.group.attrs = &sobj->a;
659 sobj->s.group.name = name;
661 return &sobj->s;
664 /* not multi-threaded safe, use it in a single thread per set */
665 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
667 if (!s || !attr)
668 return -EINVAL;
670 if (s->members >= s->max_members)
671 return -ENOMEM;
673 s->group.attrs[s->members] = attr;
674 s->members++;
676 return 0;
679 static int add_many_to_attr_set(struct attribute_set* s,
680 struct attribute **attr,
681 unsigned int count)
683 int i, res;
685 for (i = 0; i < count; i++) {
686 res = add_to_attr_set(s, attr[i]);
687 if (res)
688 return res;
691 return 0;
694 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
696 sysfs_remove_group(kobj, &s->group);
697 destroy_attr_set(s);
700 static int parse_strtoul(const char *buf,
701 unsigned long max, unsigned long *value)
703 char *endp;
705 *value = simple_strtoul(buf, &endp, 0);
706 while (*endp && isspace(*endp))
707 endp++;
708 if (*endp || *value > max)
709 return -EINVAL;
711 return 0;
714 /****************************************************************************
715 ****************************************************************************
717 * Subdrivers
719 ****************************************************************************
720 ****************************************************************************/
722 /*************************************************************************
723 * thinkpad-acpi init subdriver
726 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
728 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
729 printk(IBM_INFO "%s\n", IBM_URL);
731 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
732 (thinkpad_id.bios_version_str) ?
733 thinkpad_id.bios_version_str : "unknown",
734 (thinkpad_id.ec_version_str) ?
735 thinkpad_id.ec_version_str : "unknown");
737 if (thinkpad_id.vendor && thinkpad_id.model_str)
738 printk(IBM_INFO "%s %s\n",
739 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
740 "IBM" : ((thinkpad_id.vendor ==
741 PCI_VENDOR_ID_LENOVO) ?
742 "Lenovo" : "Unknown vendor"),
743 thinkpad_id.model_str);
745 return 0;
748 static int thinkpad_acpi_driver_read(char *p)
750 int len = 0;
752 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
753 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
755 return len;
758 static struct ibm_struct thinkpad_acpi_driver_data = {
759 .name = "driver",
760 .read = thinkpad_acpi_driver_read,
763 /*************************************************************************
764 * Hotkey subdriver
767 enum { /* Keys available through NVRAM polling */
768 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb8000U,
771 enum { /* Positions of some of the keys in hotkey masks */
772 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
773 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
774 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
775 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
776 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
777 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
778 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
779 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
780 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
781 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
782 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
785 enum { /* NVRAM to ACPI HKEY group map */
786 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
787 TP_ACPI_HKEY_ZOOM_MASK |
788 TP_ACPI_HKEY_DISPSWTCH_MASK |
789 TP_ACPI_HKEY_HIBERNATE_MASK,
790 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
791 TP_ACPI_HKEY_BRGHTDWN_MASK,
792 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
793 TP_ACPI_HKEY_VOLDWN_MASK |
794 TP_ACPI_HKEY_MUTE_MASK,
797 struct tp_nvram_state {
798 u16 thinkpad_toggle:1;
799 u16 zoom_toggle:1;
800 u16 display_toggle:1;
801 u16 thinklight_toggle:1;
802 u16 hibernate_toggle:1;
803 u16 displayexp_toggle:1;
804 u16 display_state:1;
805 u16 brightness_toggle:1;
806 u16 volume_toggle:1;
807 u16 mute:1;
809 u8 brightness_level;
810 u8 volume_level;
813 static int hotkey_orig_status;
814 static u32 hotkey_orig_mask;
815 static u32 hotkey_all_mask;
816 static u32 hotkey_reserved_mask;
818 static struct task_struct *tpacpi_hotkey_task;
819 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
820 static int hotkey_poll_freq = 4; /* Hz */
821 static struct mutex hotkey_poll_mutex;
822 static struct mutex thinkpad_nvram_mutex;
823 static struct mutex hotkey_thread_mutex;
825 static u16 *hotkey_keycode_map;
827 static struct attribute_set *hotkey_dev_attributes;
829 static void tpacpi_input_send_key(unsigned int scancode)
831 unsigned int keycode;
833 keycode = hotkey_keycode_map[scancode];
835 if (keycode != KEY_RESERVED) {
836 mutex_lock(&tpacpi_inputdev_send_mutex);
838 input_report_key(tpacpi_inputdev, keycode, 1);
839 if (keycode == KEY_UNKNOWN)
840 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
841 scancode);
842 input_sync(tpacpi_inputdev);
844 input_report_key(tpacpi_inputdev, keycode, 0);
845 if (keycode == KEY_UNKNOWN)
846 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
847 scancode);
848 input_sync(tpacpi_inputdev);
850 mutex_unlock(&tpacpi_inputdev_send_mutex);
854 static int hotkey_get_wlsw(int *status)
856 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
857 return -EIO;
858 return 0;
861 static void tpacpi_input_send_radiosw(void)
863 int wlsw;
865 mutex_lock(&tpacpi_inputdev_send_mutex);
867 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
868 input_report_switch(tpacpi_inputdev,
869 SW_RADIO, !!wlsw);
870 input_sync(tpacpi_inputdev);
873 mutex_unlock(&tpacpi_inputdev_send_mutex);
876 static void hotkey_read_nvram(struct tp_nvram_state *n)
878 u8 d;
880 if (hotkey_source_mask & TP_NVRAM_HKEY_GROUP_HK2) {
881 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
882 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
883 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
884 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
885 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
887 if (hotkey_source_mask & TP_ACPI_HKEY_THNKLGHT_MASK) {
888 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
889 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
891 if (hotkey_source_mask & TP_ACPI_HKEY_DISPXPAND_MASK) {
892 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
893 n->displayexp_toggle =
894 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
896 if (hotkey_source_mask & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
897 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
898 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
899 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
900 n->brightness_toggle =
901 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
903 if (hotkey_source_mask & TP_NVRAM_HKEY_GROUP_VOLUME) {
904 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
905 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
906 >> TP_NVRAM_POS_LEVEL_VOLUME;
907 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
908 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
912 #define TPACPI_COMPARE_KEY(__scancode, __member) \
913 if (hotkey_source_mask & (1 << __scancode) && \
914 oldn->__member != newn->__member) \
915 tpacpi_input_send_key(__scancode);
917 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
918 struct tp_nvram_state *newn)
920 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle)
921 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle)
922 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle)
923 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle)
925 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle)
927 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle)
929 /* handle volume */
930 if (oldn->volume_toggle != newn->volume_toggle) {
931 if (oldn->mute != newn->mute) {
932 tpacpi_input_send_key(TP_ACPI_HOTKEYSCAN_MUTE);
934 if (oldn->volume_level > newn->volume_level) {
935 tpacpi_input_send_key(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
936 } else if (oldn->volume_level < newn->volume_level) {
937 tpacpi_input_send_key(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
940 /* we could "else" here and issue mute regardless of a change
941 * in the mute bit (and therefore notify of multiple presses of
942 * the mute button), but that is *incorrect* semanthics for
943 * a thinkpad mixer, where "mute" always mutes, and a volume
944 * key unmutes.
948 /* handle brightness */
949 if (oldn->brightness_toggle != newn->brightness_toggle) {
950 if (oldn->brightness_level < newn->brightness_level) {
951 tpacpi_input_send_key(TP_ACPI_HOTKEYSCAN_FNHOME);
952 } else if (oldn->brightness_level > newn->brightness_level) {
953 tpacpi_input_send_key(TP_ACPI_HOTKEYSCAN_FNEND);
958 #undef TPACPI_COMPARE_KEY
960 static int hotkey_kthread(void *data)
962 struct tp_nvram_state s[2];
963 unsigned int si, so;
964 unsigned long t;
966 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
967 return 0;
969 mutex_lock(&hotkey_thread_mutex);
971 si = 1;
972 so = 0;
973 t = 0;
975 /* Initial state for compares */
976 hotkey_read_nvram(&s[so]);
978 while (!kthread_should_stop() && hotkey_poll_freq) {
979 t = msleep_interruptible(1000/hotkey_poll_freq + t);
980 if (unlikely(kthread_should_stop()))
981 break;
982 if (unlikely(try_to_freeze())) {
983 si = so; /* forget old state on thaw */
984 t = 0;
986 if (t > 0)
987 continue;
989 hotkey_read_nvram(&s[si]);
990 hotkey_compare_and_issue_event(&s[so], &s[si]);
991 so = si;
992 si ^= 1;
995 mutex_unlock(&hotkey_thread_mutex);
996 return 0;
999 static void hotkey_poll_stop_sync(void)
1001 if (tpacpi_hotkey_task) {
1002 kthread_stop(tpacpi_hotkey_task);
1003 tpacpi_hotkey_task = NULL;
1004 mutex_lock(&hotkey_thread_mutex);
1005 /* at this point, the thread did exit */
1006 mutex_unlock(&hotkey_thread_mutex);
1010 /* call with hotkey_poll_mutex held */
1011 static void hotkey_poll_setup(int force)
1013 if (force)
1014 hotkey_poll_stop_sync();
1016 if (hotkey_source_mask != 0 && hotkey_poll_freq > 0 &&
1017 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1018 if (!tpacpi_hotkey_task) {
1019 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1020 NULL, IBM_FILE "d");
1021 if (IS_ERR(tpacpi_hotkey_task)) {
1022 tpacpi_hotkey_task = NULL;
1023 printk(IBM_ERR "could not create kernel thread "
1024 "for hotkey polling\n");
1027 } else {
1028 hotkey_poll_stop_sync();
1032 static int hotkey_poll_setup_safe(int force)
1034 int rc;
1036 rc = mutex_lock_interruptible(&hotkey_poll_mutex);
1037 if (rc < 0)
1038 return rc;
1040 hotkey_poll_setup(force);
1041 mutex_unlock(&hotkey_poll_mutex);
1043 return 0;
1046 static void hotkey_warn_no_poll(void)
1048 if (hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1049 printk(IBM_NOTICE "hot keys 0x%08x require polling, "
1050 "which is currently disabled\n",
1051 hotkey_source_mask);
1056 static int hotkey_inputdev_open(struct input_dev *dev)
1058 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
1059 return -EBUSY;
1060 /* enable hotkey polling when required */
1061 return hotkey_poll_setup_safe(0);
1064 static void hotkey_inputdev_close(struct input_dev *dev)
1066 /* disable hotkey polling when possible */
1067 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
1068 hotkey_poll_setup_safe(0);
1071 /* sysfs hotkey enable ------------------------------------------------- */
1072 static ssize_t hotkey_enable_show(struct device *dev,
1073 struct device_attribute *attr,
1074 char *buf)
1076 int res, status;
1077 u32 mask;
1079 res = hotkey_get(&status, &mask);
1080 if (res)
1081 return res;
1083 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1086 static ssize_t hotkey_enable_store(struct device *dev,
1087 struct device_attribute *attr,
1088 const char *buf, size_t count)
1090 unsigned long t;
1091 int res, status;
1092 u32 mask;
1094 if (parse_strtoul(buf, 1, &t))
1095 return -EINVAL;
1097 res = hotkey_get(&status, &mask);
1098 if (!res)
1099 res = hotkey_set(t, mask);
1101 return (res) ? res : count;
1104 static struct device_attribute dev_attr_hotkey_enable =
1105 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1106 hotkey_enable_show, hotkey_enable_store);
1108 /* sysfs hotkey mask --------------------------------------------------- */
1109 static ssize_t hotkey_mask_show(struct device *dev,
1110 struct device_attribute *attr,
1111 char *buf)
1113 int res, status;
1114 u32 mask;
1116 res = hotkey_get(&status, &mask);
1117 if (res)
1118 return res;
1120 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
1123 static ssize_t hotkey_mask_store(struct device *dev,
1124 struct device_attribute *attr,
1125 const char *buf, size_t count)
1127 unsigned long t;
1128 int res, status;
1129 u32 mask;
1131 if (parse_strtoul(buf, 0xffffffffUL, &t))
1132 return -EINVAL;
1134 res = hotkey_get(&status, &mask);
1135 if (!res)
1136 hotkey_set(status, t);
1138 return (res) ? res : count;
1141 static struct device_attribute dev_attr_hotkey_mask =
1142 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1143 hotkey_mask_show, hotkey_mask_store);
1145 /* sysfs hotkey bios_enabled ------------------------------------------- */
1146 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1147 struct device_attribute *attr,
1148 char *buf)
1150 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1153 static struct device_attribute dev_attr_hotkey_bios_enabled =
1154 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1156 /* sysfs hotkey bios_mask ---------------------------------------------- */
1157 static ssize_t hotkey_bios_mask_show(struct device *dev,
1158 struct device_attribute *attr,
1159 char *buf)
1161 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1164 static struct device_attribute dev_attr_hotkey_bios_mask =
1165 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1167 /* sysfs hotkey all_mask ----------------------------------------------- */
1168 static ssize_t hotkey_all_mask_show(struct device *dev,
1169 struct device_attribute *attr,
1170 char *buf)
1172 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1173 hotkey_all_mask | hotkey_source_mask);
1176 static struct device_attribute dev_attr_hotkey_all_mask =
1177 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1179 /* sysfs hotkey recommended_mask --------------------------------------- */
1180 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1181 struct device_attribute *attr,
1182 char *buf)
1184 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1185 (hotkey_all_mask | hotkey_source_mask)
1186 & ~hotkey_reserved_mask);
1189 static struct device_attribute dev_attr_hotkey_recommended_mask =
1190 __ATTR(hotkey_recommended_mask, S_IRUGO,
1191 hotkey_recommended_mask_show, NULL);
1193 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1194 static ssize_t hotkey_source_mask_show(struct device *dev,
1195 struct device_attribute *attr,
1196 char *buf)
1198 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1201 static ssize_t hotkey_source_mask_store(struct device *dev,
1202 struct device_attribute *attr,
1203 const char *buf, size_t count)
1205 unsigned long t;
1206 int rc;
1208 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1209 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1210 return -EINVAL;
1212 rc = mutex_lock_interruptible(&hotkey_poll_mutex);
1213 if (rc < 0)
1214 return rc;
1216 hotkey_source_mask = t;
1218 hotkey_poll_setup(1);
1219 hotkey_warn_no_poll();
1220 mutex_unlock(&hotkey_poll_mutex);
1222 return count;
1225 static struct device_attribute dev_attr_hotkey_source_mask =
1226 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1227 hotkey_source_mask_show, hotkey_source_mask_store);
1229 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1230 static ssize_t hotkey_poll_freq_show(struct device *dev,
1231 struct device_attribute *attr,
1232 char *buf)
1234 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1237 static ssize_t hotkey_poll_freq_store(struct device *dev,
1238 struct device_attribute *attr,
1239 const char *buf, size_t count)
1241 unsigned long t;
1242 int rc;
1244 if (parse_strtoul(buf, 25, &t))
1245 return -EINVAL;
1247 rc = mutex_lock_interruptible(&hotkey_poll_mutex);
1248 if (rc < 0)
1249 return rc;
1251 hotkey_poll_freq = t;
1253 hotkey_poll_setup(0);
1254 hotkey_warn_no_poll();
1255 mutex_unlock(&hotkey_poll_mutex);
1257 return count;
1260 static struct device_attribute dev_attr_hotkey_poll_freq =
1261 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1262 hotkey_poll_freq_show, hotkey_poll_freq_store);
1264 /* sysfs hotkey radio_sw ----------------------------------------------- */
1265 static ssize_t hotkey_radio_sw_show(struct device *dev,
1266 struct device_attribute *attr,
1267 char *buf)
1269 int res, s;
1270 res = hotkey_get_wlsw(&s);
1271 if (res < 0)
1272 return res;
1274 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1277 static struct device_attribute dev_attr_hotkey_radio_sw =
1278 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1280 /* sysfs hotkey report_mode -------------------------------------------- */
1281 static ssize_t hotkey_report_mode_show(struct device *dev,
1282 struct device_attribute *attr,
1283 char *buf)
1285 return snprintf(buf, PAGE_SIZE, "%d\n",
1286 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1289 static struct device_attribute dev_attr_hotkey_report_mode =
1290 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1292 /* --------------------------------------------------------------------- */
1294 static struct attribute *hotkey_attributes[] __initdata = {
1295 &dev_attr_hotkey_enable.attr,
1296 &dev_attr_hotkey_report_mode.attr,
1299 static struct attribute *hotkey_mask_attributes[] __initdata = {
1300 &dev_attr_hotkey_mask.attr,
1301 &dev_attr_hotkey_bios_enabled.attr,
1302 &dev_attr_hotkey_bios_mask.attr,
1303 &dev_attr_hotkey_all_mask.attr,
1304 &dev_attr_hotkey_recommended_mask.attr,
1305 &dev_attr_hotkey_source_mask.attr,
1306 &dev_attr_hotkey_poll_freq.attr,
1309 static int __init hotkey_init(struct ibm_init_struct *iibm)
1312 static u16 ibm_keycode_map[] __initdata = {
1313 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1314 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1315 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1316 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1317 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
1318 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1319 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1320 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1321 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1322 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
1323 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1324 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1325 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1326 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1327 KEY_RESERVED, /* 0x14: VOLUME UP */
1328 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1329 KEY_RESERVED, /* 0x16: MUTE */
1330 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1331 /* (assignments unknown, please report if found) */
1332 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1333 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1335 static u16 lenovo_keycode_map[] __initdata = {
1336 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1337 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1338 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1339 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1340 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
1341 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1342 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1343 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1344 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
1345 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
1346 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
1347 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1348 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1349 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1350 KEY_RESERVED, /* 0x14: VOLUME UP */
1351 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1352 KEY_RESERVED, /* 0x16: MUTE */
1353 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1354 /* (assignments unknown, please report if found) */
1355 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1356 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1359 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1360 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1361 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1363 int res, i;
1364 int status;
1365 int hkeyv;
1367 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1369 BUG_ON(!tpacpi_inputdev);
1370 BUG_ON(tpacpi_inputdev->open != NULL ||
1371 tpacpi_inputdev->close != NULL);
1373 IBM_ACPIHANDLE_INIT(hkey);
1374 mutex_init(&hotkey_mutex);
1375 mutex_init(&hotkey_poll_mutex);
1376 mutex_init(&hotkey_thread_mutex);
1378 /* hotkey not supported on 570 */
1379 tp_features.hotkey = hkey_handle != NULL;
1381 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1382 str_supported(tp_features.hotkey));
1384 if (tp_features.hotkey) {
1385 hotkey_dev_attributes = create_attr_set(10, NULL);
1386 if (!hotkey_dev_attributes)
1387 return -ENOMEM;
1388 res = add_many_to_attr_set(hotkey_dev_attributes,
1389 hotkey_attributes,
1390 ARRAY_SIZE(hotkey_attributes));
1391 if (res)
1392 return res;
1394 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1395 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1396 for HKEY interface version 0x100 */
1397 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1398 if ((hkeyv >> 8) != 1) {
1399 printk(IBM_ERR "unknown version of the "
1400 "HKEY interface: 0x%x\n", hkeyv);
1401 printk(IBM_ERR "please report this to %s\n",
1402 IBM_MAIL);
1403 } else {
1405 * MHKV 0x100 in A31, R40, R40e,
1406 * T4x, X31, and later
1407 * */
1408 tp_features.hotkey_mask = 1;
1412 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1413 str_supported(tp_features.hotkey_mask));
1415 if (tp_features.hotkey_mask) {
1416 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1417 "MHKA", "qd")) {
1418 printk(IBM_ERR
1419 "missing MHKA handler, "
1420 "please report this to %s\n",
1421 IBM_MAIL);
1422 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1426 hotkey_source_mask = TPACPI_HKEY_NVRAM_KNOWN_MASK
1427 & ~hotkey_all_mask;
1429 vdbg_printk(TPACPI_DBG_INIT,
1430 "hotkey source mask 0x%08x, polling freq %d\n",
1431 hotkey_source_mask, hotkey_poll_freq);
1433 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
1434 if (!res && tp_features.hotkey_mask) {
1435 res = add_many_to_attr_set(hotkey_dev_attributes,
1436 hotkey_mask_attributes,
1437 ARRAY_SIZE(hotkey_mask_attributes));
1440 /* Not all thinkpads have a hardware radio switch */
1441 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1442 tp_features.hotkey_wlsw = 1;
1443 printk(IBM_INFO
1444 "radio switch found; radios are %s\n",
1445 enabled(status, 0));
1446 res = add_to_attr_set(hotkey_dev_attributes,
1447 &dev_attr_hotkey_radio_sw.attr);
1450 if (!res)
1451 res = register_attr_set_with_sysfs(
1452 hotkey_dev_attributes,
1453 &tpacpi_pdev->dev.kobj);
1454 if (res)
1455 return res;
1457 /* Set up key map */
1459 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1460 GFP_KERNEL);
1461 if (!hotkey_keycode_map) {
1462 printk(IBM_ERR "failed to allocate memory for key map\n");
1463 return -ENOMEM;
1466 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1467 dbg_printk(TPACPI_DBG_INIT,
1468 "using Lenovo default hot key map\n");
1469 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1470 TPACPI_HOTKEY_MAP_SIZE);
1471 } else {
1472 dbg_printk(TPACPI_DBG_INIT,
1473 "using IBM default hot key map\n");
1474 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1475 TPACPI_HOTKEY_MAP_SIZE);
1478 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1479 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1480 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1481 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1482 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1483 tpacpi_inputdev->keycode = hotkey_keycode_map;
1484 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1485 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1486 set_bit(hotkey_keycode_map[i],
1487 tpacpi_inputdev->keybit);
1488 } else {
1489 if (i < sizeof(hotkey_reserved_mask)*8)
1490 hotkey_reserved_mask |= 1 << i;
1493 tpacpi_inputdev->open = &hotkey_inputdev_open;
1494 tpacpi_inputdev->close = &hotkey_inputdev_close;
1496 if (tp_features.hotkey_wlsw) {
1497 set_bit(EV_SW, tpacpi_inputdev->evbit);
1498 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1501 dbg_printk(TPACPI_DBG_INIT,
1502 "enabling hot key handling\n");
1503 res = hotkey_set(1, ((hotkey_all_mask | hotkey_source_mask)
1504 & ~hotkey_reserved_mask)
1505 | hotkey_orig_mask);
1506 if (res)
1507 return res;
1509 dbg_printk(TPACPI_DBG_INIT,
1510 "legacy hot key reporting over procfs %s\n",
1511 (hotkey_report_mode < 2) ?
1512 "enabled" : "disabled");
1514 mutex_lock(&hotkey_poll_mutex);
1515 hotkey_poll_setup(0);
1516 hotkey_warn_no_poll();
1517 mutex_unlock(&hotkey_poll_mutex);
1520 return (tp_features.hotkey)? 0 : 1;
1523 static void hotkey_exit(void)
1525 int res;
1527 hotkey_poll_stop_sync();
1529 if (tp_features.hotkey) {
1530 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
1531 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1532 if (res)
1533 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
1536 if (hotkey_dev_attributes) {
1537 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1538 hotkey_dev_attributes = NULL;
1542 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1544 u32 hkey;
1545 unsigned int scancode;
1546 int send_acpi_ev;
1547 int ignore_acpi_ev;
1549 if (event != 0x80) {
1550 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
1551 /* forward it to userspace, maybe it knows how to handle it */
1552 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
1553 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1554 ibm->acpi->device->dev.bus_id,
1555 event, 0);
1556 return;
1559 while (1) {
1560 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1561 printk(IBM_ERR "failed to retrieve hot key event\n");
1562 return;
1565 if (hkey == 0) {
1566 /* queue empty */
1567 return;
1570 send_acpi_ev = 0;
1571 ignore_acpi_ev = 0;
1573 switch (hkey >> 12) {
1574 case 1:
1575 /* 0x1000-0x1FFF: key presses */
1576 scancode = hkey & 0xfff;
1577 if (scancode > 0 && scancode < 0x21) {
1578 scancode--;
1579 if (!(hotkey_source_mask & (1 << scancode))) {
1580 tpacpi_input_send_key(scancode);
1582 } else {
1583 printk(IBM_ERR
1584 "hotkey 0x%04x out of range for keyboard map\n",
1585 hkey);
1586 send_acpi_ev = 1;
1588 break;
1589 case 5:
1590 /* 0x5000-0x5FFF: LID */
1591 /* we don't handle it through this path, just
1592 * eat up known LID events */
1593 if (hkey != 0x5001 && hkey != 0x5002) {
1594 printk(IBM_ERR
1595 "unknown LID-related hotkey event: 0x%04x\n",
1596 hkey);
1597 send_acpi_ev = 1;
1598 } else {
1599 ignore_acpi_ev = 1;
1601 break;
1602 case 7:
1603 /* 0x7000-0x7FFF: misc */
1604 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1605 tpacpi_input_send_radiosw();
1606 break;
1608 /* fallthrough to default */
1609 default:
1610 /* case 2: dock-related */
1611 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1612 /* case 3: ultra-bay related. maybe bay in dock? */
1613 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1614 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1615 send_acpi_ev = 1;
1618 /* Legacy events */
1619 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
1620 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
1623 /* netlink events */
1624 if (!ignore_acpi_ev && send_acpi_ev) {
1625 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1626 ibm->acpi->device->dev.bus_id,
1627 event, hkey);
1632 static void hotkey_resume(void)
1634 tpacpi_input_send_radiosw();
1635 hotkey_poll_setup_safe(1);
1639 * Call with hotkey_mutex held
1641 static int hotkey_get(int *status, u32 *mask)
1643 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1644 return -EIO;
1646 if (tp_features.hotkey_mask)
1647 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
1648 return -EIO;
1650 return 0;
1654 * Call with hotkey_mutex held
1656 static int hotkey_set(int status, u32 mask)
1658 int i;
1660 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1661 return -EIO;
1663 if (tp_features.hotkey_mask)
1664 for (i = 0; i < 32; i++) {
1665 int bit = ((1 << i) & mask) != 0;
1666 if (!acpi_evalf(hkey_handle,
1667 NULL, "MHKM", "vdd", i + 1, bit))
1668 return -EIO;
1671 return 0;
1674 /* procfs -------------------------------------------------------------- */
1675 static int hotkey_read(char *p)
1677 int res, status;
1678 u32 mask;
1679 int len = 0;
1681 if (!tp_features.hotkey) {
1682 len += sprintf(p + len, "status:\t\tnot supported\n");
1683 return len;
1686 res = mutex_lock_interruptible(&hotkey_mutex);
1687 if (res < 0)
1688 return res;
1689 res = hotkey_get(&status, &mask);
1690 mutex_unlock(&hotkey_mutex);
1691 if (res)
1692 return res;
1694 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1695 if (tp_features.hotkey_mask) {
1696 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1697 len += sprintf(p + len,
1698 "commands:\tenable, disable, reset, <mask>\n");
1699 } else {
1700 len += sprintf(p + len, "mask:\t\tnot supported\n");
1701 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1704 return len;
1707 static int hotkey_write(char *buf)
1709 int res, status;
1710 u32 mask;
1711 char *cmd;
1712 int do_cmd = 0;
1714 if (!tp_features.hotkey)
1715 return -ENODEV;
1717 res = mutex_lock_interruptible(&hotkey_mutex);
1718 if (res < 0)
1719 return res;
1721 res = hotkey_get(&status, &mask);
1722 if (res)
1723 goto errexit;
1725 res = 0;
1726 while ((cmd = next_cmd(&buf))) {
1727 if (strlencmp(cmd, "enable") == 0) {
1728 status = 1;
1729 } else if (strlencmp(cmd, "disable") == 0) {
1730 status = 0;
1731 } else if (strlencmp(cmd, "reset") == 0) {
1732 status = hotkey_orig_status;
1733 mask = hotkey_orig_mask;
1734 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1735 /* mask set */
1736 } else if (sscanf(cmd, "%x", &mask) == 1) {
1737 /* mask set */
1738 } else {
1739 res = -EINVAL;
1740 goto errexit;
1742 do_cmd = 1;
1745 if (do_cmd)
1746 res = hotkey_set(status, mask);
1748 errexit:
1749 mutex_unlock(&hotkey_mutex);
1750 return res;
1753 static const struct acpi_device_id ibm_htk_device_ids[] = {
1754 {IBM_HKEY_HID, 0},
1755 {"", 0},
1758 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1759 .hid = ibm_htk_device_ids,
1760 .notify = hotkey_notify,
1761 .handle = &hkey_handle,
1762 .type = ACPI_DEVICE_NOTIFY,
1765 static struct ibm_struct hotkey_driver_data = {
1766 .name = "hotkey",
1767 .read = hotkey_read,
1768 .write = hotkey_write,
1769 .exit = hotkey_exit,
1770 .resume = hotkey_resume,
1771 .acpi = &ibm_hotkey_acpidriver,
1774 /*************************************************************************
1775 * Bluetooth subdriver
1778 /* sysfs bluetooth enable ---------------------------------------------- */
1779 static ssize_t bluetooth_enable_show(struct device *dev,
1780 struct device_attribute *attr,
1781 char *buf)
1783 int status;
1785 status = bluetooth_get_radiosw();
1786 if (status < 0)
1787 return status;
1789 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1792 static ssize_t bluetooth_enable_store(struct device *dev,
1793 struct device_attribute *attr,
1794 const char *buf, size_t count)
1796 unsigned long t;
1797 int res;
1799 if (parse_strtoul(buf, 1, &t))
1800 return -EINVAL;
1802 res = bluetooth_set_radiosw(t);
1804 return (res) ? res : count;
1807 static struct device_attribute dev_attr_bluetooth_enable =
1808 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1809 bluetooth_enable_show, bluetooth_enable_store);
1811 /* --------------------------------------------------------------------- */
1813 static struct attribute *bluetooth_attributes[] = {
1814 &dev_attr_bluetooth_enable.attr,
1815 NULL
1818 static const struct attribute_group bluetooth_attr_group = {
1819 .attrs = bluetooth_attributes,
1822 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1824 int res;
1825 int status = 0;
1827 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1829 IBM_ACPIHANDLE_INIT(hkey);
1831 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1832 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1833 tp_features.bluetooth = hkey_handle &&
1834 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1836 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1837 str_supported(tp_features.bluetooth),
1838 status);
1840 if (tp_features.bluetooth) {
1841 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1842 /* no bluetooth hardware present in system */
1843 tp_features.bluetooth = 0;
1844 dbg_printk(TPACPI_DBG_INIT,
1845 "bluetooth hardware not installed\n");
1846 } else {
1847 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1848 &bluetooth_attr_group);
1849 if (res)
1850 return res;
1854 return (tp_features.bluetooth)? 0 : 1;
1857 static void bluetooth_exit(void)
1859 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1860 &bluetooth_attr_group);
1863 static int bluetooth_get_radiosw(void)
1865 int status;
1867 if (!tp_features.bluetooth)
1868 return -ENODEV;
1870 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1871 return -EIO;
1873 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1876 static int bluetooth_set_radiosw(int radio_on)
1878 int status;
1880 if (!tp_features.bluetooth)
1881 return -ENODEV;
1883 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1884 return -EIO;
1885 if (radio_on)
1886 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1887 else
1888 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1889 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1890 return -EIO;
1892 return 0;
1895 /* procfs -------------------------------------------------------------- */
1896 static int bluetooth_read(char *p)
1898 int len = 0;
1899 int status = bluetooth_get_radiosw();
1901 if (!tp_features.bluetooth)
1902 len += sprintf(p + len, "status:\t\tnot supported\n");
1903 else {
1904 len += sprintf(p + len, "status:\t\t%s\n",
1905 (status)? "enabled" : "disabled");
1906 len += sprintf(p + len, "commands:\tenable, disable\n");
1909 return len;
1912 static int bluetooth_write(char *buf)
1914 char *cmd;
1916 if (!tp_features.bluetooth)
1917 return -ENODEV;
1919 while ((cmd = next_cmd(&buf))) {
1920 if (strlencmp(cmd, "enable") == 0) {
1921 bluetooth_set_radiosw(1);
1922 } else if (strlencmp(cmd, "disable") == 0) {
1923 bluetooth_set_radiosw(0);
1924 } else
1925 return -EINVAL;
1928 return 0;
1931 static struct ibm_struct bluetooth_driver_data = {
1932 .name = "bluetooth",
1933 .read = bluetooth_read,
1934 .write = bluetooth_write,
1935 .exit = bluetooth_exit,
1938 /*************************************************************************
1939 * Wan subdriver
1942 /* sysfs wan enable ---------------------------------------------------- */
1943 static ssize_t wan_enable_show(struct device *dev,
1944 struct device_attribute *attr,
1945 char *buf)
1947 int status;
1949 status = wan_get_radiosw();
1950 if (status < 0)
1951 return status;
1953 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1956 static ssize_t wan_enable_store(struct device *dev,
1957 struct device_attribute *attr,
1958 const char *buf, size_t count)
1960 unsigned long t;
1961 int res;
1963 if (parse_strtoul(buf, 1, &t))
1964 return -EINVAL;
1966 res = wan_set_radiosw(t);
1968 return (res) ? res : count;
1971 static struct device_attribute dev_attr_wan_enable =
1972 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1973 wan_enable_show, wan_enable_store);
1975 /* --------------------------------------------------------------------- */
1977 static struct attribute *wan_attributes[] = {
1978 &dev_attr_wan_enable.attr,
1979 NULL
1982 static const struct attribute_group wan_attr_group = {
1983 .attrs = wan_attributes,
1986 static int __init wan_init(struct ibm_init_struct *iibm)
1988 int res;
1989 int status = 0;
1991 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1993 IBM_ACPIHANDLE_INIT(hkey);
1995 tp_features.wan = hkey_handle &&
1996 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1998 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1999 str_supported(tp_features.wan),
2000 status);
2002 if (tp_features.wan) {
2003 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2004 /* no wan hardware present in system */
2005 tp_features.wan = 0;
2006 dbg_printk(TPACPI_DBG_INIT,
2007 "wan hardware not installed\n");
2008 } else {
2009 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2010 &wan_attr_group);
2011 if (res)
2012 return res;
2016 return (tp_features.wan)? 0 : 1;
2019 static void wan_exit(void)
2021 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2022 &wan_attr_group);
2025 static int wan_get_radiosw(void)
2027 int status;
2029 if (!tp_features.wan)
2030 return -ENODEV;
2032 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2033 return -EIO;
2035 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2038 static int wan_set_radiosw(int radio_on)
2040 int status;
2042 if (!tp_features.wan)
2043 return -ENODEV;
2045 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2046 return -EIO;
2047 if (radio_on)
2048 status |= TP_ACPI_WANCARD_RADIOSSW;
2049 else
2050 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2051 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2052 return -EIO;
2054 return 0;
2057 /* procfs -------------------------------------------------------------- */
2058 static int wan_read(char *p)
2060 int len = 0;
2061 int status = wan_get_radiosw();
2063 if (!tp_features.wan)
2064 len += sprintf(p + len, "status:\t\tnot supported\n");
2065 else {
2066 len += sprintf(p + len, "status:\t\t%s\n",
2067 (status)? "enabled" : "disabled");
2068 len += sprintf(p + len, "commands:\tenable, disable\n");
2071 return len;
2074 static int wan_write(char *buf)
2076 char *cmd;
2078 if (!tp_features.wan)
2079 return -ENODEV;
2081 while ((cmd = next_cmd(&buf))) {
2082 if (strlencmp(cmd, "enable") == 0) {
2083 wan_set_radiosw(1);
2084 } else if (strlencmp(cmd, "disable") == 0) {
2085 wan_set_radiosw(0);
2086 } else
2087 return -EINVAL;
2090 return 0;
2093 static struct ibm_struct wan_driver_data = {
2094 .name = "wan",
2095 .read = wan_read,
2096 .write = wan_write,
2097 .exit = wan_exit,
2098 .flags.experimental = 1,
2101 /*************************************************************************
2102 * Video subdriver
2105 static enum video_access_mode video_supported;
2106 static int video_orig_autosw;
2108 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2109 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2110 "\\_SB.PCI0.VID0", /* 770e */
2111 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2112 "\\_SB.PCI0.AGP.VID", /* all others */
2113 ); /* R30, R31 */
2115 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2117 static int __init video_init(struct ibm_init_struct *iibm)
2119 int ivga;
2121 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2123 IBM_ACPIHANDLE_INIT(vid);
2124 IBM_ACPIHANDLE_INIT(vid2);
2126 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2127 /* G41, assume IVGA doesn't change */
2128 vid_handle = vid2_handle;
2130 if (!vid_handle)
2131 /* video switching not supported on R30, R31 */
2132 video_supported = TPACPI_VIDEO_NONE;
2133 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2134 /* 570 */
2135 video_supported = TPACPI_VIDEO_570;
2136 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2137 /* 600e/x, 770e, 770x */
2138 video_supported = TPACPI_VIDEO_770;
2139 else
2140 /* all others */
2141 video_supported = TPACPI_VIDEO_NEW;
2143 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2144 str_supported(video_supported != TPACPI_VIDEO_NONE),
2145 video_supported);
2147 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2150 static void video_exit(void)
2152 dbg_printk(TPACPI_DBG_EXIT,
2153 "restoring original video autoswitch mode\n");
2154 if (video_autosw_set(video_orig_autosw))
2155 printk(IBM_ERR "error while trying to restore original "
2156 "video autoswitch mode\n");
2159 static int video_outputsw_get(void)
2161 int status = 0;
2162 int i;
2164 switch (video_supported) {
2165 case TPACPI_VIDEO_570:
2166 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2167 TP_ACPI_VIDEO_570_PHSCMD))
2168 return -EIO;
2169 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2170 break;
2171 case TPACPI_VIDEO_770:
2172 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2173 return -EIO;
2174 if (i)
2175 status |= TP_ACPI_VIDEO_S_LCD;
2176 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2177 return -EIO;
2178 if (i)
2179 status |= TP_ACPI_VIDEO_S_CRT;
2180 break;
2181 case TPACPI_VIDEO_NEW:
2182 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2183 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2184 return -EIO;
2185 if (i)
2186 status |= TP_ACPI_VIDEO_S_CRT;
2188 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2189 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2190 return -EIO;
2191 if (i)
2192 status |= TP_ACPI_VIDEO_S_LCD;
2193 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2194 return -EIO;
2195 if (i)
2196 status |= TP_ACPI_VIDEO_S_DVI;
2197 break;
2198 default:
2199 return -ENOSYS;
2202 return status;
2205 static int video_outputsw_set(int status)
2207 int autosw;
2208 int res = 0;
2210 switch (video_supported) {
2211 case TPACPI_VIDEO_570:
2212 res = acpi_evalf(NULL, NULL,
2213 "\\_SB.PHS2", "vdd",
2214 TP_ACPI_VIDEO_570_PHS2CMD,
2215 status | TP_ACPI_VIDEO_570_PHS2SET);
2216 break;
2217 case TPACPI_VIDEO_770:
2218 autosw = video_autosw_get();
2219 if (autosw < 0)
2220 return autosw;
2222 res = video_autosw_set(1);
2223 if (res)
2224 return res;
2225 res = acpi_evalf(vid_handle, NULL,
2226 "ASWT", "vdd", status * 0x100, 0);
2227 if (!autosw && video_autosw_set(autosw)) {
2228 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2229 return -EIO;
2231 break;
2232 case TPACPI_VIDEO_NEW:
2233 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2234 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2235 break;
2236 default:
2237 return -ENOSYS;
2240 return (res)? 0 : -EIO;
2243 static int video_autosw_get(void)
2245 int autosw = 0;
2247 switch (video_supported) {
2248 case TPACPI_VIDEO_570:
2249 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2250 return -EIO;
2251 break;
2252 case TPACPI_VIDEO_770:
2253 case TPACPI_VIDEO_NEW:
2254 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2255 return -EIO;
2256 break;
2257 default:
2258 return -ENOSYS;
2261 return autosw & 1;
2264 static int video_autosw_set(int enable)
2266 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2267 return -EIO;
2268 return 0;
2271 static int video_outputsw_cycle(void)
2273 int autosw = video_autosw_get();
2274 int res;
2276 if (autosw < 0)
2277 return autosw;
2279 switch (video_supported) {
2280 case TPACPI_VIDEO_570:
2281 res = video_autosw_set(1);
2282 if (res)
2283 return res;
2284 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2285 break;
2286 case TPACPI_VIDEO_770:
2287 case TPACPI_VIDEO_NEW:
2288 res = video_autosw_set(1);
2289 if (res)
2290 return res;
2291 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2292 break;
2293 default:
2294 return -ENOSYS;
2296 if (!autosw && video_autosw_set(autosw)) {
2297 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2298 return -EIO;
2301 return (res)? 0 : -EIO;
2304 static int video_expand_toggle(void)
2306 switch (video_supported) {
2307 case TPACPI_VIDEO_570:
2308 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2309 0 : -EIO;
2310 case TPACPI_VIDEO_770:
2311 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2312 0 : -EIO;
2313 case TPACPI_VIDEO_NEW:
2314 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2315 0 : -EIO;
2316 default:
2317 return -ENOSYS;
2319 /* not reached */
2322 static int video_read(char *p)
2324 int status, autosw;
2325 int len = 0;
2327 if (video_supported == TPACPI_VIDEO_NONE) {
2328 len += sprintf(p + len, "status:\t\tnot supported\n");
2329 return len;
2332 status = video_outputsw_get();
2333 if (status < 0)
2334 return status;
2336 autosw = video_autosw_get();
2337 if (autosw < 0)
2338 return autosw;
2340 len += sprintf(p + len, "status:\t\tsupported\n");
2341 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2342 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2343 if (video_supported == TPACPI_VIDEO_NEW)
2344 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2345 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2346 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2347 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2348 if (video_supported == TPACPI_VIDEO_NEW)
2349 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2350 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2351 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2353 return len;
2356 static int video_write(char *buf)
2358 char *cmd;
2359 int enable, disable, status;
2360 int res;
2362 if (video_supported == TPACPI_VIDEO_NONE)
2363 return -ENODEV;
2365 enable = 0;
2366 disable = 0;
2368 while ((cmd = next_cmd(&buf))) {
2369 if (strlencmp(cmd, "lcd_enable") == 0) {
2370 enable |= TP_ACPI_VIDEO_S_LCD;
2371 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2372 disable |= TP_ACPI_VIDEO_S_LCD;
2373 } else if (strlencmp(cmd, "crt_enable") == 0) {
2374 enable |= TP_ACPI_VIDEO_S_CRT;
2375 } else if (strlencmp(cmd, "crt_disable") == 0) {
2376 disable |= TP_ACPI_VIDEO_S_CRT;
2377 } else if (video_supported == TPACPI_VIDEO_NEW &&
2378 strlencmp(cmd, "dvi_enable") == 0) {
2379 enable |= TP_ACPI_VIDEO_S_DVI;
2380 } else if (video_supported == TPACPI_VIDEO_NEW &&
2381 strlencmp(cmd, "dvi_disable") == 0) {
2382 disable |= TP_ACPI_VIDEO_S_DVI;
2383 } else if (strlencmp(cmd, "auto_enable") == 0) {
2384 res = video_autosw_set(1);
2385 if (res)
2386 return res;
2387 } else if (strlencmp(cmd, "auto_disable") == 0) {
2388 res = video_autosw_set(0);
2389 if (res)
2390 return res;
2391 } else if (strlencmp(cmd, "video_switch") == 0) {
2392 res = video_outputsw_cycle();
2393 if (res)
2394 return res;
2395 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2396 res = video_expand_toggle();
2397 if (res)
2398 return res;
2399 } else
2400 return -EINVAL;
2403 if (enable || disable) {
2404 status = video_outputsw_get();
2405 if (status < 0)
2406 return status;
2407 res = video_outputsw_set((status & ~disable) | enable);
2408 if (res)
2409 return res;
2412 return 0;
2415 static struct ibm_struct video_driver_data = {
2416 .name = "video",
2417 .read = video_read,
2418 .write = video_write,
2419 .exit = video_exit,
2422 /*************************************************************************
2423 * Light (thinklight) subdriver
2426 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2427 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2429 static int __init light_init(struct ibm_init_struct *iibm)
2431 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2433 IBM_ACPIHANDLE_INIT(ledb);
2434 IBM_ACPIHANDLE_INIT(lght);
2435 IBM_ACPIHANDLE_INIT(cmos);
2437 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2438 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2440 if (tp_features.light)
2441 /* light status not supported on
2442 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2443 tp_features.light_status =
2444 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2446 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2447 str_supported(tp_features.light));
2449 return (tp_features.light)? 0 : 1;
2452 static int light_read(char *p)
2454 int len = 0;
2455 int status = 0;
2457 if (!tp_features.light) {
2458 len += sprintf(p + len, "status:\t\tnot supported\n");
2459 } else if (!tp_features.light_status) {
2460 len += sprintf(p + len, "status:\t\tunknown\n");
2461 len += sprintf(p + len, "commands:\ton, off\n");
2462 } else {
2463 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2464 return -EIO;
2465 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2466 len += sprintf(p + len, "commands:\ton, off\n");
2469 return len;
2472 static int light_write(char *buf)
2474 int cmos_cmd, lght_cmd;
2475 char *cmd;
2476 int success;
2478 if (!tp_features.light)
2479 return -ENODEV;
2481 while ((cmd = next_cmd(&buf))) {
2482 if (strlencmp(cmd, "on") == 0) {
2483 cmos_cmd = 0x0c;
2484 lght_cmd = 1;
2485 } else if (strlencmp(cmd, "off") == 0) {
2486 cmos_cmd = 0x0d;
2487 lght_cmd = 0;
2488 } else
2489 return -EINVAL;
2491 success = cmos_handle ?
2492 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2493 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2494 if (!success)
2495 return -EIO;
2498 return 0;
2501 static struct ibm_struct light_driver_data = {
2502 .name = "light",
2503 .read = light_read,
2504 .write = light_write,
2507 /*************************************************************************
2508 * Dock subdriver
2511 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2513 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2514 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2515 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2516 "\\_SB.PCI.ISA.SLCE", /* 570 */
2517 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2519 /* don't list other alternatives as we install a notify handler on the 570 */
2520 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2522 static const struct acpi_device_id ibm_pci_device_ids[] = {
2523 {PCI_ROOT_HID_STRING, 0},
2524 {"", 0},
2527 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2529 .notify = dock_notify,
2530 .handle = &dock_handle,
2531 .type = ACPI_SYSTEM_NOTIFY,
2534 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2535 * We just use it to get notifications of dock hotplug
2536 * in very old thinkpads */
2537 .hid = ibm_pci_device_ids,
2538 .notify = dock_notify,
2539 .handle = &pci_handle,
2540 .type = ACPI_SYSTEM_NOTIFY,
2544 static struct ibm_struct dock_driver_data[2] = {
2546 .name = "dock",
2547 .read = dock_read,
2548 .write = dock_write,
2549 .acpi = &ibm_dock_acpidriver[0],
2552 .name = "dock",
2553 .acpi = &ibm_dock_acpidriver[1],
2557 #define dock_docked() (_sta(dock_handle) & 1)
2559 static int __init dock_init(struct ibm_init_struct *iibm)
2561 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2563 IBM_ACPIHANDLE_INIT(dock);
2565 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2566 str_supported(dock_handle != NULL));
2568 return (dock_handle)? 0 : 1;
2571 static int __init dock_init2(struct ibm_init_struct *iibm)
2573 int dock2_needed;
2575 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2577 if (dock_driver_data[0].flags.acpi_driver_registered &&
2578 dock_driver_data[0].flags.acpi_notify_installed) {
2579 IBM_ACPIHANDLE_INIT(pci);
2580 dock2_needed = (pci_handle != NULL);
2581 vdbg_printk(TPACPI_DBG_INIT,
2582 "dock PCI handler for the TP 570 is %s\n",
2583 str_supported(dock2_needed));
2584 } else {
2585 vdbg_printk(TPACPI_DBG_INIT,
2586 "dock subdriver part 2 not required\n");
2587 dock2_needed = 0;
2590 return (dock2_needed)? 0 : 1;
2593 static void dock_notify(struct ibm_struct *ibm, u32 event)
2595 int docked = dock_docked();
2596 int pci = ibm->acpi->hid && ibm->acpi->device &&
2597 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
2598 int data;
2600 if (event == 1 && !pci) /* 570 */
2601 data = 1; /* button */
2602 else if (event == 1 && pci) /* 570 */
2603 data = 3; /* dock */
2604 else if (event == 3 && docked)
2605 data = 1; /* button */
2606 else if (event == 3 && !docked)
2607 data = 2; /* undock */
2608 else if (event == 0 && docked)
2609 data = 3; /* dock */
2610 else {
2611 printk(IBM_ERR "unknown dock event %d, status %d\n",
2612 event, _sta(dock_handle));
2613 data = 0; /* unknown */
2615 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
2616 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2617 ibm->acpi->device->dev.bus_id,
2618 event, data);
2621 static int dock_read(char *p)
2623 int len = 0;
2624 int docked = dock_docked();
2626 if (!dock_handle)
2627 len += sprintf(p + len, "status:\t\tnot supported\n");
2628 else if (!docked)
2629 len += sprintf(p + len, "status:\t\tundocked\n");
2630 else {
2631 len += sprintf(p + len, "status:\t\tdocked\n");
2632 len += sprintf(p + len, "commands:\tdock, undock\n");
2635 return len;
2638 static int dock_write(char *buf)
2640 char *cmd;
2642 if (!dock_docked())
2643 return -ENODEV;
2645 while ((cmd = next_cmd(&buf))) {
2646 if (strlencmp(cmd, "undock") == 0) {
2647 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2648 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2649 return -EIO;
2650 } else if (strlencmp(cmd, "dock") == 0) {
2651 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2652 return -EIO;
2653 } else
2654 return -EINVAL;
2657 return 0;
2660 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2662 /*************************************************************************
2663 * Bay subdriver
2666 #ifdef CONFIG_THINKPAD_ACPI_BAY
2667 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2668 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2669 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2670 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2671 ); /* A21e, R30, R31 */
2672 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2673 "_EJ0", /* all others */
2674 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2675 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2676 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2677 ); /* all others */
2678 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2679 "_EJ0", /* 770x */
2680 ); /* all others */
2682 static int __init bay_init(struct ibm_init_struct *iibm)
2684 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2686 IBM_ACPIHANDLE_INIT(bay);
2687 if (bay_handle)
2688 IBM_ACPIHANDLE_INIT(bay_ej);
2689 IBM_ACPIHANDLE_INIT(bay2);
2690 if (bay2_handle)
2691 IBM_ACPIHANDLE_INIT(bay2_ej);
2693 tp_features.bay_status = bay_handle &&
2694 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2695 tp_features.bay_status2 = bay2_handle &&
2696 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2698 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2699 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2700 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2701 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2703 vdbg_printk(TPACPI_DBG_INIT,
2704 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2705 str_supported(tp_features.bay_status),
2706 str_supported(tp_features.bay_eject),
2707 str_supported(tp_features.bay_status2),
2708 str_supported(tp_features.bay_eject2));
2710 return (tp_features.bay_status || tp_features.bay_eject ||
2711 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2714 static void bay_notify(struct ibm_struct *ibm, u32 event)
2716 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
2717 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2718 ibm->acpi->device->dev.bus_id,
2719 event, 0);
2722 #define bay_occupied(b) (_sta(b##_handle) & 1)
2724 static int bay_read(char *p)
2726 int len = 0;
2727 int occupied = bay_occupied(bay);
2728 int occupied2 = bay_occupied(bay2);
2729 int eject, eject2;
2731 len += sprintf(p + len, "status:\t\t%s\n",
2732 tp_features.bay_status ?
2733 (occupied ? "occupied" : "unoccupied") :
2734 "not supported");
2735 if (tp_features.bay_status2)
2736 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2737 "occupied" : "unoccupied");
2739 eject = tp_features.bay_eject && occupied;
2740 eject2 = tp_features.bay_eject2 && occupied2;
2742 if (eject && eject2)
2743 len += sprintf(p + len, "commands:\teject, eject2\n");
2744 else if (eject)
2745 len += sprintf(p + len, "commands:\teject\n");
2746 else if (eject2)
2747 len += sprintf(p + len, "commands:\teject2\n");
2749 return len;
2752 static int bay_write(char *buf)
2754 char *cmd;
2756 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2757 return -ENODEV;
2759 while ((cmd = next_cmd(&buf))) {
2760 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2761 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2762 return -EIO;
2763 } else if (tp_features.bay_eject2 &&
2764 strlencmp(cmd, "eject2") == 0) {
2765 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2766 return -EIO;
2767 } else
2768 return -EINVAL;
2771 return 0;
2774 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2775 .notify = bay_notify,
2776 .handle = &bay_handle,
2777 .type = ACPI_SYSTEM_NOTIFY,
2780 static struct ibm_struct bay_driver_data = {
2781 .name = "bay",
2782 .read = bay_read,
2783 .write = bay_write,
2784 .acpi = &ibm_bay_acpidriver,
2787 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2789 /*************************************************************************
2790 * CMOS subdriver
2793 /* sysfs cmos_command -------------------------------------------------- */
2794 static ssize_t cmos_command_store(struct device *dev,
2795 struct device_attribute *attr,
2796 const char *buf, size_t count)
2798 unsigned long cmos_cmd;
2799 int res;
2801 if (parse_strtoul(buf, 21, &cmos_cmd))
2802 return -EINVAL;
2804 res = issue_thinkpad_cmos_command(cmos_cmd);
2805 return (res)? res : count;
2808 static struct device_attribute dev_attr_cmos_command =
2809 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2811 /* --------------------------------------------------------------------- */
2813 static int __init cmos_init(struct ibm_init_struct *iibm)
2815 int res;
2817 vdbg_printk(TPACPI_DBG_INIT,
2818 "initializing cmos commands subdriver\n");
2820 IBM_ACPIHANDLE_INIT(cmos);
2822 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2823 str_supported(cmos_handle != NULL));
2825 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2826 if (res)
2827 return res;
2829 return (cmos_handle)? 0 : 1;
2832 static void cmos_exit(void)
2834 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2837 static int cmos_read(char *p)
2839 int len = 0;
2841 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2842 R30, R31, T20-22, X20-21 */
2843 if (!cmos_handle)
2844 len += sprintf(p + len, "status:\t\tnot supported\n");
2845 else {
2846 len += sprintf(p + len, "status:\t\tsupported\n");
2847 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2850 return len;
2853 static int cmos_write(char *buf)
2855 char *cmd;
2856 int cmos_cmd, res;
2858 while ((cmd = next_cmd(&buf))) {
2859 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2860 cmos_cmd >= 0 && cmos_cmd <= 21) {
2861 /* cmos_cmd set */
2862 } else
2863 return -EINVAL;
2865 res = issue_thinkpad_cmos_command(cmos_cmd);
2866 if (res)
2867 return res;
2870 return 0;
2873 static struct ibm_struct cmos_driver_data = {
2874 .name = "cmos",
2875 .read = cmos_read,
2876 .write = cmos_write,
2877 .exit = cmos_exit,
2880 /*************************************************************************
2881 * LED subdriver
2884 static enum led_access_mode led_supported;
2886 IBM_HANDLE(led, ec, "SLED", /* 570 */
2887 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2888 "LED", /* all others */
2889 ); /* R30, R31 */
2891 static int __init led_init(struct ibm_init_struct *iibm)
2893 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2895 IBM_ACPIHANDLE_INIT(led);
2897 if (!led_handle)
2898 /* led not supported on R30, R31 */
2899 led_supported = TPACPI_LED_NONE;
2900 else if (strlencmp(led_path, "SLED") == 0)
2901 /* 570 */
2902 led_supported = TPACPI_LED_570;
2903 else if (strlencmp(led_path, "SYSL") == 0)
2904 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2905 led_supported = TPACPI_LED_OLD;
2906 else
2907 /* all others */
2908 led_supported = TPACPI_LED_NEW;
2910 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2911 str_supported(led_supported), led_supported);
2913 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2916 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2918 static int led_read(char *p)
2920 int len = 0;
2922 if (!led_supported) {
2923 len += sprintf(p + len, "status:\t\tnot supported\n");
2924 return len;
2926 len += sprintf(p + len, "status:\t\tsupported\n");
2928 if (led_supported == TPACPI_LED_570) {
2929 /* 570 */
2930 int i, status;
2931 for (i = 0; i < 8; i++) {
2932 if (!acpi_evalf(ec_handle,
2933 &status, "GLED", "dd", 1 << i))
2934 return -EIO;
2935 len += sprintf(p + len, "%d:\t\t%s\n",
2936 i, led_status(status));
2940 len += sprintf(p + len, "commands:\t"
2941 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2943 return len;
2946 /* off, on, blink */
2947 static const int led_sled_arg1[] = { 0, 1, 3 };
2948 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2949 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2950 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2952 static int led_write(char *buf)
2954 char *cmd;
2955 int led, ind, ret;
2957 if (!led_supported)
2958 return -ENODEV;
2960 while ((cmd = next_cmd(&buf))) {
2961 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2962 return -EINVAL;
2964 if (strstr(cmd, "off")) {
2965 ind = 0;
2966 } else if (strstr(cmd, "on")) {
2967 ind = 1;
2968 } else if (strstr(cmd, "blink")) {
2969 ind = 2;
2970 } else
2971 return -EINVAL;
2973 if (led_supported == TPACPI_LED_570) {
2974 /* 570 */
2975 led = 1 << led;
2976 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2977 led, led_sled_arg1[ind]))
2978 return -EIO;
2979 } else if (led_supported == TPACPI_LED_OLD) {
2980 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2981 led = 1 << led;
2982 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2983 if (ret >= 0)
2984 ret =
2985 ec_write(TPACPI_LED_EC_HLBL,
2986 led * led_exp_hlbl[ind]);
2987 if (ret >= 0)
2988 ret =
2989 ec_write(TPACPI_LED_EC_HLCL,
2990 led * led_exp_hlcl[ind]);
2991 if (ret < 0)
2992 return ret;
2993 } else {
2994 /* all others */
2995 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2996 led, led_led_arg1[ind]))
2997 return -EIO;
3001 return 0;
3004 static struct ibm_struct led_driver_data = {
3005 .name = "led",
3006 .read = led_read,
3007 .write = led_write,
3010 /*************************************************************************
3011 * Beep subdriver
3014 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3016 static int __init beep_init(struct ibm_init_struct *iibm)
3018 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3020 IBM_ACPIHANDLE_INIT(beep);
3022 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3023 str_supported(beep_handle != NULL));
3025 return (beep_handle)? 0 : 1;
3028 static int beep_read(char *p)
3030 int len = 0;
3032 if (!beep_handle)
3033 len += sprintf(p + len, "status:\t\tnot supported\n");
3034 else {
3035 len += sprintf(p + len, "status:\t\tsupported\n");
3036 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3039 return len;
3042 static int beep_write(char *buf)
3044 char *cmd;
3045 int beep_cmd;
3047 if (!beep_handle)
3048 return -ENODEV;
3050 while ((cmd = next_cmd(&buf))) {
3051 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3052 beep_cmd >= 0 && beep_cmd <= 17) {
3053 /* beep_cmd set */
3054 } else
3055 return -EINVAL;
3056 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3057 return -EIO;
3060 return 0;
3063 static struct ibm_struct beep_driver_data = {
3064 .name = "beep",
3065 .read = beep_read,
3066 .write = beep_write,
3069 /*************************************************************************
3070 * Thermal subdriver
3073 static enum thermal_access_mode thermal_read_mode;
3075 /* sysfs temp##_input -------------------------------------------------- */
3077 static ssize_t thermal_temp_input_show(struct device *dev,
3078 struct device_attribute *attr,
3079 char *buf)
3081 struct sensor_device_attribute *sensor_attr =
3082 to_sensor_dev_attr(attr);
3083 int idx = sensor_attr->index;
3084 s32 value;
3085 int res;
3087 res = thermal_get_sensor(idx, &value);
3088 if (res)
3089 return res;
3090 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3091 return -ENXIO;
3093 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3096 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3097 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
3099 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3100 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3101 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3102 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3103 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3104 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3105 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3106 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3107 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3108 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3109 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3110 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3111 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3112 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3113 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3114 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3115 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3118 #define THERMAL_ATTRS(X) \
3119 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3121 static struct attribute *thermal_temp_input_attr[] = {
3122 THERMAL_ATTRS(8),
3123 THERMAL_ATTRS(9),
3124 THERMAL_ATTRS(10),
3125 THERMAL_ATTRS(11),
3126 THERMAL_ATTRS(12),
3127 THERMAL_ATTRS(13),
3128 THERMAL_ATTRS(14),
3129 THERMAL_ATTRS(15),
3130 THERMAL_ATTRS(0),
3131 THERMAL_ATTRS(1),
3132 THERMAL_ATTRS(2),
3133 THERMAL_ATTRS(3),
3134 THERMAL_ATTRS(4),
3135 THERMAL_ATTRS(5),
3136 THERMAL_ATTRS(6),
3137 THERMAL_ATTRS(7),
3138 NULL
3141 static const struct attribute_group thermal_temp_input16_group = {
3142 .attrs = thermal_temp_input_attr
3145 static const struct attribute_group thermal_temp_input8_group = {
3146 .attrs = &thermal_temp_input_attr[8]
3149 #undef THERMAL_SENSOR_ATTR_TEMP
3150 #undef THERMAL_ATTRS
3152 /* --------------------------------------------------------------------- */
3154 static int __init thermal_init(struct ibm_init_struct *iibm)
3156 u8 t, ta1, ta2;
3157 int i;
3158 int acpi_tmp7;
3159 int res;
3161 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3163 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3165 if (thinkpad_id.ec_model) {
3167 * Direct EC access mode: sensors at registers
3168 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3169 * non-implemented, thermal sensors return 0x80 when
3170 * not available
3173 ta1 = ta2 = 0;
3174 for (i = 0; i < 8; i++) {
3175 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3176 ta1 |= t;
3177 } else {
3178 ta1 = 0;
3179 break;
3181 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3182 ta2 |= t;
3183 } else {
3184 ta1 = 0;
3185 break;
3188 if (ta1 == 0) {
3189 /* This is sheer paranoia, but we handle it anyway */
3190 if (acpi_tmp7) {
3191 printk(IBM_ERR
3192 "ThinkPad ACPI EC access misbehaving, "
3193 "falling back to ACPI TMPx access mode\n");
3194 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3195 } else {
3196 printk(IBM_ERR
3197 "ThinkPad ACPI EC access misbehaving, "
3198 "disabling thermal sensors access\n");
3199 thermal_read_mode = TPACPI_THERMAL_NONE;
3201 } else {
3202 thermal_read_mode =
3203 (ta2 != 0) ?
3204 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3206 } else if (acpi_tmp7) {
3207 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3208 /* 600e/x, 770e, 770x */
3209 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3210 } else {
3211 /* Standard ACPI TMPx access, max 8 sensors */
3212 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3214 } else {
3215 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3216 thermal_read_mode = TPACPI_THERMAL_NONE;
3219 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3220 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3221 thermal_read_mode);
3223 switch(thermal_read_mode) {
3224 case TPACPI_THERMAL_TPEC_16:
3225 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3226 &thermal_temp_input16_group);
3227 if (res)
3228 return res;
3229 break;
3230 case TPACPI_THERMAL_TPEC_8:
3231 case TPACPI_THERMAL_ACPI_TMP07:
3232 case TPACPI_THERMAL_ACPI_UPDT:
3233 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3234 &thermal_temp_input8_group);
3235 if (res)
3236 return res;
3237 break;
3238 case TPACPI_THERMAL_NONE:
3239 default:
3240 return 1;
3243 return 0;
3246 static void thermal_exit(void)
3248 switch(thermal_read_mode) {
3249 case TPACPI_THERMAL_TPEC_16:
3250 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3251 &thermal_temp_input16_group);
3252 break;
3253 case TPACPI_THERMAL_TPEC_8:
3254 case TPACPI_THERMAL_ACPI_TMP07:
3255 case TPACPI_THERMAL_ACPI_UPDT:
3256 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3257 &thermal_temp_input16_group);
3258 break;
3259 case TPACPI_THERMAL_NONE:
3260 default:
3261 break;
3265 /* idx is zero-based */
3266 static int thermal_get_sensor(int idx, s32 *value)
3268 int t;
3269 s8 tmp;
3270 char tmpi[5];
3272 t = TP_EC_THERMAL_TMP0;
3274 switch (thermal_read_mode) {
3275 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3276 case TPACPI_THERMAL_TPEC_16:
3277 if (idx >= 8 && idx <= 15) {
3278 t = TP_EC_THERMAL_TMP8;
3279 idx -= 8;
3281 /* fallthrough */
3282 #endif
3283 case TPACPI_THERMAL_TPEC_8:
3284 if (idx <= 7) {
3285 if (!acpi_ec_read(t + idx, &tmp))
3286 return -EIO;
3287 *value = tmp * 1000;
3288 return 0;
3290 break;
3292 case TPACPI_THERMAL_ACPI_UPDT:
3293 if (idx <= 7) {
3294 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3295 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3296 return -EIO;
3297 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3298 return -EIO;
3299 *value = (t - 2732) * 100;
3300 return 0;
3302 break;
3304 case TPACPI_THERMAL_ACPI_TMP07:
3305 if (idx <= 7) {
3306 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3307 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3308 return -EIO;
3309 if (t > 127 || t < -127)
3310 t = TP_EC_THERMAL_TMP_NA;
3311 *value = t * 1000;
3312 return 0;
3314 break;
3316 case TPACPI_THERMAL_NONE:
3317 default:
3318 return -ENOSYS;
3321 return -EINVAL;
3324 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3326 int res, i;
3327 int n;
3329 n = 8;
3330 i = 0;
3332 if (!s)
3333 return -EINVAL;
3335 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3336 n = 16;
3338 for(i = 0 ; i < n; i++) {
3339 res = thermal_get_sensor(i, &s->temp[i]);
3340 if (res)
3341 return res;
3344 return n;
3347 static int thermal_read(char *p)
3349 int len = 0;
3350 int n, i;
3351 struct ibm_thermal_sensors_struct t;
3353 n = thermal_get_sensors(&t);
3354 if (unlikely(n < 0))
3355 return n;
3357 len += sprintf(p + len, "temperatures:\t");
3359 if (n > 0) {
3360 for (i = 0; i < (n - 1); i++)
3361 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3362 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3363 } else
3364 len += sprintf(p + len, "not supported\n");
3366 return len;
3369 static struct ibm_struct thermal_driver_data = {
3370 .name = "thermal",
3371 .read = thermal_read,
3372 .exit = thermal_exit,
3375 /*************************************************************************
3376 * EC Dump subdriver
3379 static u8 ecdump_regs[256];
3381 static int ecdump_read(char *p)
3383 int len = 0;
3384 int i, j;
3385 u8 v;
3387 len += sprintf(p + len, "EC "
3388 " +00 +01 +02 +03 +04 +05 +06 +07"
3389 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3390 for (i = 0; i < 256; i += 16) {
3391 len += sprintf(p + len, "EC 0x%02x:", i);
3392 for (j = 0; j < 16; j++) {
3393 if (!acpi_ec_read(i + j, &v))
3394 break;
3395 if (v != ecdump_regs[i + j])
3396 len += sprintf(p + len, " *%02x", v);
3397 else
3398 len += sprintf(p + len, " %02x", v);
3399 ecdump_regs[i + j] = v;
3401 len += sprintf(p + len, "\n");
3402 if (j != 16)
3403 break;
3406 /* These are way too dangerous to advertise openly... */
3407 #if 0
3408 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3409 " (<offset> is 00-ff, <value> is 00-ff)\n");
3410 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3411 " (<offset> is 00-ff, <value> is 0-255)\n");
3412 #endif
3413 return len;
3416 static int ecdump_write(char *buf)
3418 char *cmd;
3419 int i, v;
3421 while ((cmd = next_cmd(&buf))) {
3422 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3423 /* i and v set */
3424 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3425 /* i and v set */
3426 } else
3427 return -EINVAL;
3428 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3429 if (!acpi_ec_write(i, v))
3430 return -EIO;
3431 } else
3432 return -EINVAL;
3435 return 0;
3438 static struct ibm_struct ecdump_driver_data = {
3439 .name = "ecdump",
3440 .read = ecdump_read,
3441 .write = ecdump_write,
3442 .flags.experimental = 1,
3445 /*************************************************************************
3446 * Backlight/brightness subdriver
3449 static struct backlight_device *ibm_backlight_device;
3451 static struct backlight_ops ibm_backlight_data = {
3452 .get_brightness = brightness_get,
3453 .update_status = brightness_update_status,
3456 static struct mutex brightness_mutex;
3458 static int __init brightness_init(struct ibm_init_struct *iibm)
3460 int b;
3462 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3464 mutex_init(&brightness_mutex);
3466 if (!brightness_mode) {
3467 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3468 brightness_mode = 2;
3469 else
3470 brightness_mode = 3;
3472 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3473 brightness_mode);
3476 if (brightness_mode > 3)
3477 return -EINVAL;
3479 b = brightness_get(NULL);
3480 if (b < 0)
3481 return 1;
3483 ibm_backlight_device = backlight_device_register(
3484 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3485 &ibm_backlight_data);
3486 if (IS_ERR(ibm_backlight_device)) {
3487 printk(IBM_ERR "Could not register backlight device\n");
3488 return PTR_ERR(ibm_backlight_device);
3490 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
3492 ibm_backlight_device->props.max_brightness = 7;
3493 ibm_backlight_device->props.brightness = b;
3494 backlight_update_status(ibm_backlight_device);
3496 return 0;
3499 static void brightness_exit(void)
3501 if (ibm_backlight_device) {
3502 vdbg_printk(TPACPI_DBG_EXIT,
3503 "calling backlight_device_unregister()\n");
3504 backlight_device_unregister(ibm_backlight_device);
3505 ibm_backlight_device = NULL;
3509 static int brightness_update_status(struct backlight_device *bd)
3511 return brightness_set(
3512 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3513 bd->props.power == FB_BLANK_UNBLANK) ?
3514 bd->props.brightness : 0);
3518 * ThinkPads can read brightness from two places: EC 0x31, or
3519 * CMOS NVRAM byte 0x5E, bits 0-3.
3521 static int brightness_get(struct backlight_device *bd)
3523 u8 lec = 0, lcmos = 0, level = 0;
3525 if (brightness_mode & 1) {
3526 if (!acpi_ec_read(brightness_offset, &lec))
3527 return -EIO;
3528 lec &= 7;
3529 level = lec;
3531 if (brightness_mode & 2) {
3532 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3533 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3534 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3535 level = lcmos;
3538 if (brightness_mode == 3 && lec != lcmos) {
3539 printk(IBM_ERR
3540 "CMOS NVRAM (%u) and EC (%u) do not agree "
3541 "on display brightness level\n",
3542 (unsigned int) lcmos,
3543 (unsigned int) lec);
3544 return -EIO;
3547 return level;
3550 static int brightness_set(int value)
3552 int cmos_cmd, inc, i, res;
3553 int current_value;
3555 if (value > 7)
3556 return -EINVAL;
3558 res = mutex_lock_interruptible(&brightness_mutex);
3559 if (res < 0)
3560 return res;
3562 current_value = brightness_get(NULL);
3563 if (current_value < 0) {
3564 res = current_value;
3565 goto errout;
3568 cmos_cmd = value > current_value ?
3569 TP_CMOS_BRIGHTNESS_UP :
3570 TP_CMOS_BRIGHTNESS_DOWN;
3571 inc = value > current_value ? 1 : -1;
3573 res = 0;
3574 for (i = current_value; i != value; i += inc) {
3575 if ((brightness_mode & 2) &&
3576 issue_thinkpad_cmos_command(cmos_cmd)) {
3577 res = -EIO;
3578 goto errout;
3580 if ((brightness_mode & 1) &&
3581 !acpi_ec_write(brightness_offset, i + inc)) {
3582 res = -EIO;
3583 goto errout;;
3587 errout:
3588 mutex_unlock(&brightness_mutex);
3589 return res;
3592 static int brightness_read(char *p)
3594 int len = 0;
3595 int level;
3597 if ((level = brightness_get(NULL)) < 0) {
3598 len += sprintf(p + len, "level:\t\tunreadable\n");
3599 } else {
3600 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3601 len += sprintf(p + len, "commands:\tup, down\n");
3602 len += sprintf(p + len, "commands:\tlevel <level>"
3603 " (<level> is 0-7)\n");
3606 return len;
3609 static int brightness_write(char *buf)
3611 int level;
3612 int new_level;
3613 char *cmd;
3615 while ((cmd = next_cmd(&buf))) {
3616 if ((level = brightness_get(NULL)) < 0)
3617 return level;
3618 level &= 7;
3620 if (strlencmp(cmd, "up") == 0) {
3621 new_level = level == 7 ? 7 : level + 1;
3622 } else if (strlencmp(cmd, "down") == 0) {
3623 new_level = level == 0 ? 0 : level - 1;
3624 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3625 new_level >= 0 && new_level <= 7) {
3626 /* new_level set */
3627 } else
3628 return -EINVAL;
3630 brightness_set(new_level);
3633 return 0;
3636 static struct ibm_struct brightness_driver_data = {
3637 .name = "brightness",
3638 .read = brightness_read,
3639 .write = brightness_write,
3640 .exit = brightness_exit,
3643 /*************************************************************************
3644 * Volume subdriver
3647 static int volume_read(char *p)
3649 int len = 0;
3650 u8 level;
3652 if (!acpi_ec_read(volume_offset, &level)) {
3653 len += sprintf(p + len, "level:\t\tunreadable\n");
3654 } else {
3655 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3656 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3657 len += sprintf(p + len, "commands:\tup, down, mute\n");
3658 len += sprintf(p + len, "commands:\tlevel <level>"
3659 " (<level> is 0-15)\n");
3662 return len;
3665 static int volume_write(char *buf)
3667 int cmos_cmd, inc, i;
3668 u8 level, mute;
3669 int new_level, new_mute;
3670 char *cmd;
3672 while ((cmd = next_cmd(&buf))) {
3673 if (!acpi_ec_read(volume_offset, &level))
3674 return -EIO;
3675 new_mute = mute = level & 0x40;
3676 new_level = level = level & 0xf;
3678 if (strlencmp(cmd, "up") == 0) {
3679 if (mute)
3680 new_mute = 0;
3681 else
3682 new_level = level == 15 ? 15 : level + 1;
3683 } else if (strlencmp(cmd, "down") == 0) {
3684 if (mute)
3685 new_mute = 0;
3686 else
3687 new_level = level == 0 ? 0 : level - 1;
3688 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3689 new_level >= 0 && new_level <= 15) {
3690 /* new_level set */
3691 } else if (strlencmp(cmd, "mute") == 0) {
3692 new_mute = 0x40;
3693 } else
3694 return -EINVAL;
3696 if (new_level != level) { /* mute doesn't change */
3697 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3698 inc = new_level > level ? 1 : -1;
3700 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3701 !acpi_ec_write(volume_offset, level)))
3702 return -EIO;
3704 for (i = level; i != new_level; i += inc)
3705 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3706 !acpi_ec_write(volume_offset, i + inc))
3707 return -EIO;
3709 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3710 !acpi_ec_write(volume_offset,
3711 new_level + mute)))
3712 return -EIO;
3715 if (new_mute != mute) { /* level doesn't change */
3716 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3718 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3719 !acpi_ec_write(volume_offset, level + new_mute))
3720 return -EIO;
3724 return 0;
3727 static struct ibm_struct volume_driver_data = {
3728 .name = "volume",
3729 .read = volume_read,
3730 .write = volume_write,
3733 /*************************************************************************
3734 * Fan subdriver
3738 * FAN ACCESS MODES
3740 * TPACPI_FAN_RD_ACPI_GFAN:
3741 * ACPI GFAN method: returns fan level
3743 * see TPACPI_FAN_WR_ACPI_SFAN
3744 * EC 0x2f (HFSP) not available if GFAN exists
3746 * TPACPI_FAN_WR_ACPI_SFAN:
3747 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3749 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3750 * it for writing.
3752 * TPACPI_FAN_WR_TPEC:
3753 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3754 * Supported on almost all ThinkPads
3756 * Fan speed changes of any sort (including those caused by the
3757 * disengaged mode) are usually done slowly by the firmware as the
3758 * maximum ammount of fan duty cycle change per second seems to be
3759 * limited.
3761 * Reading is not available if GFAN exists.
3762 * Writing is not available if SFAN exists.
3764 * Bits
3765 * 7 automatic mode engaged;
3766 * (default operation mode of the ThinkPad)
3767 * fan level is ignored in this mode.
3768 * 6 full speed mode (takes precedence over bit 7);
3769 * not available on all thinkpads. May disable
3770 * the tachometer while the fan controller ramps up
3771 * the speed (which can take up to a few *minutes*).
3772 * Speeds up fan to 100% duty-cycle, which is far above
3773 * the standard RPM levels. It is not impossible that
3774 * it could cause hardware damage.
3775 * 5-3 unused in some models. Extra bits for fan level
3776 * in others, but still useless as all values above
3777 * 7 map to the same speed as level 7 in these models.
3778 * 2-0 fan level (0..7 usually)
3779 * 0x00 = stop
3780 * 0x07 = max (set when temperatures critical)
3781 * Some ThinkPads may have other levels, see
3782 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3784 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3785 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3786 * does so, its initial value is meaningless (0x07).
3788 * For firmware bugs, refer to:
3789 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3791 * ----
3793 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3794 * Main fan tachometer reading (in RPM)
3796 * This register is present on all ThinkPads with a new-style EC, and
3797 * it is known not to be present on the A21m/e, and T22, as there is
3798 * something else in offset 0x84 according to the ACPI DSDT. Other
3799 * ThinkPads from this same time period (and earlier) probably lack the
3800 * tachometer as well.
3802 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3803 * was never fixed by IBM to report the EC firmware version string
3804 * probably support the tachometer (like the early X models), so
3805 * detecting it is quite hard. We need more data to know for sure.
3807 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3808 * might result.
3810 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3811 * mode.
3813 * For firmware bugs, refer to:
3814 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3816 * TPACPI_FAN_WR_ACPI_FANS:
3817 * ThinkPad X31, X40, X41. Not available in the X60.
3819 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3820 * high speed. ACPI DSDT seems to map these three speeds to levels
3821 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3822 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3824 * The speeds are stored on handles
3825 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3827 * There are three default speed sets, acessible as handles:
3828 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3830 * ACPI DSDT switches which set is in use depending on various
3831 * factors.
3833 * TPACPI_FAN_WR_TPEC is also available and should be used to
3834 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3835 * but the ACPI tables just mention level 7.
3838 static enum fan_status_access_mode fan_status_access_mode;
3839 static enum fan_control_access_mode fan_control_access_mode;
3840 static enum fan_control_commands fan_control_commands;
3842 static u8 fan_control_initial_status;
3843 static u8 fan_control_desired_level;
3845 static void fan_watchdog_fire(struct work_struct *ignored);
3846 static int fan_watchdog_maxinterval;
3847 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3849 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3850 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3851 "\\FSPD", /* 600e/x, 770e, 770x */
3852 ); /* all others */
3853 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3854 "JFNS", /* 770x-JL */
3855 ); /* all others */
3858 * SYSFS fan layout: hwmon compatible (device)
3860 * pwm*_enable:
3861 * 0: "disengaged" mode
3862 * 1: manual mode
3863 * 2: native EC "auto" mode (recommended, hardware default)
3865 * pwm*: set speed in manual mode, ignored otherwise.
3866 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3867 * interpolation.
3869 * fan*_input: tachometer reading, RPM
3872 * SYSFS fan layout: extensions
3874 * fan_watchdog (driver):
3875 * fan watchdog interval in seconds, 0 disables (default), max 120
3878 /* sysfs fan pwm1_enable ----------------------------------------------- */
3879 static ssize_t fan_pwm1_enable_show(struct device *dev,
3880 struct device_attribute *attr,
3881 char *buf)
3883 int res, mode;
3884 u8 status;
3886 res = fan_get_status_safe(&status);
3887 if (res)
3888 return res;
3890 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3891 if (status != fan_control_initial_status) {
3892 tp_features.fan_ctrl_status_undef = 0;
3893 } else {
3894 /* Return most likely status. In fact, it
3895 * might be the only possible status */
3896 status = TP_EC_FAN_AUTO;
3900 if (status & TP_EC_FAN_FULLSPEED) {
3901 mode = 0;
3902 } else if (status & TP_EC_FAN_AUTO) {
3903 mode = 2;
3904 } else
3905 mode = 1;
3907 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3910 static ssize_t fan_pwm1_enable_store(struct device *dev,
3911 struct device_attribute *attr,
3912 const char *buf, size_t count)
3914 unsigned long t;
3915 int res, level;
3917 if (parse_strtoul(buf, 2, &t))
3918 return -EINVAL;
3920 switch (t) {
3921 case 0:
3922 level = TP_EC_FAN_FULLSPEED;
3923 break;
3924 case 1:
3925 level = TPACPI_FAN_LAST_LEVEL;
3926 break;
3927 case 2:
3928 level = TP_EC_FAN_AUTO;
3929 break;
3930 case 3:
3931 /* reserved for software-controlled auto mode */
3932 return -ENOSYS;
3933 default:
3934 return -EINVAL;
3937 res = fan_set_level_safe(level);
3938 if (res == -ENXIO)
3939 return -EINVAL;
3940 else if (res < 0)
3941 return res;
3943 fan_watchdog_reset();
3945 return count;
3948 static struct device_attribute dev_attr_fan_pwm1_enable =
3949 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3950 fan_pwm1_enable_show, fan_pwm1_enable_store);
3952 /* sysfs fan pwm1 ------------------------------------------------------ */
3953 static ssize_t fan_pwm1_show(struct device *dev,
3954 struct device_attribute *attr,
3955 char *buf)
3957 int res;
3958 u8 status;
3960 res = fan_get_status_safe(&status);
3961 if (res)
3962 return res;
3964 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3965 if (status != fan_control_initial_status) {
3966 tp_features.fan_ctrl_status_undef = 0;
3967 } else {
3968 status = TP_EC_FAN_AUTO;
3972 if ((status &
3973 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3974 status = fan_control_desired_level;
3976 if (status > 7)
3977 status = 7;
3979 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3982 static ssize_t fan_pwm1_store(struct device *dev,
3983 struct device_attribute *attr,
3984 const char *buf, size_t count)
3986 unsigned long s;
3987 int rc;
3988 u8 status, newlevel;
3990 if (parse_strtoul(buf, 255, &s))
3991 return -EINVAL;
3993 /* scale down from 0-255 to 0-7 */
3994 newlevel = (s >> 5) & 0x07;
3996 rc = mutex_lock_interruptible(&fan_mutex);
3997 if (rc < 0)
3998 return rc;
4000 rc = fan_get_status(&status);
4001 if (!rc && (status &
4002 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4003 rc = fan_set_level(newlevel);
4004 if (rc == -ENXIO)
4005 rc = -EINVAL;
4006 else if (!rc) {
4007 fan_update_desired_level(newlevel);
4008 fan_watchdog_reset();
4012 mutex_unlock(&fan_mutex);
4013 return (rc)? rc : count;
4016 static struct device_attribute dev_attr_fan_pwm1 =
4017 __ATTR(pwm1, S_IWUSR | S_IRUGO,
4018 fan_pwm1_show, fan_pwm1_store);
4020 /* sysfs fan fan1_input ------------------------------------------------ */
4021 static ssize_t fan_fan1_input_show(struct device *dev,
4022 struct device_attribute *attr,
4023 char *buf)
4025 int res;
4026 unsigned int speed;
4028 res = fan_get_speed(&speed);
4029 if (res < 0)
4030 return res;
4032 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
4035 static struct device_attribute dev_attr_fan_fan1_input =
4036 __ATTR(fan1_input, S_IRUGO,
4037 fan_fan1_input_show, NULL);
4039 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
4040 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
4041 char *buf)
4043 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
4046 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
4047 const char *buf, size_t count)
4049 unsigned long t;
4051 if (parse_strtoul(buf, 120, &t))
4052 return -EINVAL;
4054 if (!fan_control_allowed)
4055 return -EPERM;
4057 fan_watchdog_maxinterval = t;
4058 fan_watchdog_reset();
4060 return count;
4063 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
4064 fan_fan_watchdog_show, fan_fan_watchdog_store);
4066 /* --------------------------------------------------------------------- */
4067 static struct attribute *fan_attributes[] = {
4068 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
4069 &dev_attr_fan_fan1_input.attr,
4070 NULL
4073 static const struct attribute_group fan_attr_group = {
4074 .attrs = fan_attributes,
4077 static int __init fan_init(struct ibm_init_struct *iibm)
4079 int rc;
4081 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
4083 mutex_init(&fan_mutex);
4084 fan_status_access_mode = TPACPI_FAN_NONE;
4085 fan_control_access_mode = TPACPI_FAN_WR_NONE;
4086 fan_control_commands = 0;
4087 fan_watchdog_maxinterval = 0;
4088 tp_features.fan_ctrl_status_undef = 0;
4089 fan_control_desired_level = 7;
4091 IBM_ACPIHANDLE_INIT(fans);
4092 IBM_ACPIHANDLE_INIT(gfan);
4093 IBM_ACPIHANDLE_INIT(sfan);
4095 if (gfan_handle) {
4096 /* 570, 600e/x, 770e, 770x */
4097 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
4098 } else {
4099 /* all other ThinkPads: note that even old-style
4100 * ThinkPad ECs supports the fan control register */
4101 if (likely(acpi_ec_read(fan_status_offset,
4102 &fan_control_initial_status))) {
4103 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
4105 /* In some ThinkPads, neither the EC nor the ACPI
4106 * DSDT initialize the fan status, and it ends up
4107 * being set to 0x07 when it *could* be either
4108 * 0x07 or 0x80.
4110 * Enable for TP-1Y (T43), TP-78 (R51e),
4111 * TP-76 (R52), TP-70 (T43, R52), which are known
4112 * to be buggy. */
4113 if (fan_control_initial_status == 0x07) {
4114 switch (thinkpad_id.ec_model) {
4115 case 0x5931: /* TP-1Y */
4116 case 0x3837: /* TP-78 */
4117 case 0x3637: /* TP-76 */
4118 case 0x3037: /* TP-70 */
4119 printk(IBM_NOTICE
4120 "fan_init: initial fan status is "
4121 "unknown, assuming it is in auto "
4122 "mode\n");
4123 tp_features.fan_ctrl_status_undef = 1;
4127 } else {
4128 printk(IBM_ERR
4129 "ThinkPad ACPI EC access misbehaving, "
4130 "fan status and control unavailable\n");
4131 return 1;
4135 if (sfan_handle) {
4136 /* 570, 770x-JL */
4137 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
4138 fan_control_commands |=
4139 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
4140 } else {
4141 if (!gfan_handle) {
4142 /* gfan without sfan means no fan control */
4143 /* all other models implement TP EC 0x2f control */
4145 if (fans_handle) {
4146 /* X31, X40, X41 */
4147 fan_control_access_mode =
4148 TPACPI_FAN_WR_ACPI_FANS;
4149 fan_control_commands |=
4150 TPACPI_FAN_CMD_SPEED |
4151 TPACPI_FAN_CMD_LEVEL |
4152 TPACPI_FAN_CMD_ENABLE;
4153 } else {
4154 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
4155 fan_control_commands |=
4156 TPACPI_FAN_CMD_LEVEL |
4157 TPACPI_FAN_CMD_ENABLE;
4162 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
4163 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
4164 fan_control_access_mode != TPACPI_FAN_WR_NONE),
4165 fan_status_access_mode, fan_control_access_mode);
4167 /* fan control master switch */
4168 if (!fan_control_allowed) {
4169 fan_control_access_mode = TPACPI_FAN_WR_NONE;
4170 fan_control_commands = 0;
4171 dbg_printk(TPACPI_DBG_INIT,
4172 "fan control features disabled by parameter\n");
4175 /* update fan_control_desired_level */
4176 if (fan_status_access_mode != TPACPI_FAN_NONE)
4177 fan_get_status_safe(NULL);
4179 if (fan_status_access_mode != TPACPI_FAN_NONE ||
4180 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
4181 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4182 &fan_attr_group);
4183 if (!(rc < 0))
4184 rc = driver_create_file(&tpacpi_pdriver.driver,
4185 &driver_attr_fan_watchdog);
4186 if (rc < 0)
4187 return rc;
4188 return 0;
4189 } else
4190 return 1;
4194 * Call with fan_mutex held
4196 static void fan_update_desired_level(u8 status)
4198 if ((status &
4199 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4200 if (status > 7)
4201 fan_control_desired_level = 7;
4202 else
4203 fan_control_desired_level = status;
4207 static int fan_get_status(u8 *status)
4209 u8 s;
4211 /* TODO:
4212 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4214 switch (fan_status_access_mode) {
4215 case TPACPI_FAN_RD_ACPI_GFAN:
4216 /* 570, 600e/x, 770e, 770x */
4218 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4219 return -EIO;
4221 if (likely(status))
4222 *status = s & 0x07;
4224 break;
4226 case TPACPI_FAN_RD_TPEC:
4227 /* all except 570, 600e/x, 770e, 770x */
4228 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4229 return -EIO;
4231 if (likely(status))
4232 *status = s;
4234 break;
4236 default:
4237 return -ENXIO;
4240 return 0;
4243 static int fan_get_status_safe(u8 *status)
4245 int rc;
4246 u8 s;
4248 rc = mutex_lock_interruptible(&fan_mutex);
4249 if (rc < 0)
4250 return rc;
4251 rc = fan_get_status(&s);
4252 if (!rc)
4253 fan_update_desired_level(s);
4254 mutex_unlock(&fan_mutex);
4256 if (status)
4257 *status = s;
4259 return rc;
4262 static void fan_exit(void)
4264 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
4266 /* FIXME: can we really do this unconditionally? */
4267 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
4268 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
4270 cancel_delayed_work(&fan_watchdog_task);
4271 flush_scheduled_work();
4274 static int fan_get_speed(unsigned int *speed)
4276 u8 hi, lo;
4278 switch (fan_status_access_mode) {
4279 case TPACPI_FAN_RD_TPEC:
4280 /* all except 570, 600e/x, 770e, 770x */
4281 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4282 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4283 return -EIO;
4285 if (likely(speed))
4286 *speed = (hi << 8) | lo;
4288 break;
4290 default:
4291 return -ENXIO;
4294 return 0;
4297 static void fan_watchdog_fire(struct work_struct *ignored)
4299 int rc;
4301 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4302 return;
4304 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
4305 rc = fan_set_enable();
4306 if (rc < 0) {
4307 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
4308 "will try again later...\n", -rc);
4309 /* reschedule for later */
4310 fan_watchdog_reset();
4314 static void fan_watchdog_reset(void)
4316 static int fan_watchdog_active;
4318 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4319 return;
4321 if (fan_watchdog_active)
4322 cancel_delayed_work(&fan_watchdog_task);
4324 if (fan_watchdog_maxinterval > 0 &&
4325 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4326 fan_watchdog_active = 1;
4327 if (!schedule_delayed_work(&fan_watchdog_task,
4328 msecs_to_jiffies(fan_watchdog_maxinterval
4329 * 1000))) {
4330 printk(IBM_ERR "failed to schedule the fan watchdog, "
4331 "watchdog will not trigger\n");
4333 } else
4334 fan_watchdog_active = 0;
4337 static int fan_set_level(int level)
4339 if (!fan_control_allowed)
4340 return -EPERM;
4342 switch (fan_control_access_mode) {
4343 case TPACPI_FAN_WR_ACPI_SFAN:
4344 if (level >= 0 && level <= 7) {
4345 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4346 return -EIO;
4347 } else
4348 return -EINVAL;
4349 break;
4351 case TPACPI_FAN_WR_ACPI_FANS:
4352 case TPACPI_FAN_WR_TPEC:
4353 if ((level != TP_EC_FAN_AUTO) &&
4354 (level != TP_EC_FAN_FULLSPEED) &&
4355 ((level < 0) || (level > 7)))
4356 return -EINVAL;
4358 /* safety net should the EC not support AUTO
4359 * or FULLSPEED mode bits and just ignore them */
4360 if (level & TP_EC_FAN_FULLSPEED)
4361 level |= 7; /* safety min speed 7 */
4362 else if (level & TP_EC_FAN_FULLSPEED)
4363 level |= 4; /* safety min speed 4 */
4365 if (!acpi_ec_write(fan_status_offset, level))
4366 return -EIO;
4367 else
4368 tp_features.fan_ctrl_status_undef = 0;
4369 break;
4371 default:
4372 return -ENXIO;
4374 return 0;
4377 static int fan_set_level_safe(int level)
4379 int rc;
4381 if (!fan_control_allowed)
4382 return -EPERM;
4384 rc = mutex_lock_interruptible(&fan_mutex);
4385 if (rc < 0)
4386 return rc;
4388 if (level == TPACPI_FAN_LAST_LEVEL)
4389 level = fan_control_desired_level;
4391 rc = fan_set_level(level);
4392 if (!rc)
4393 fan_update_desired_level(level);
4395 mutex_unlock(&fan_mutex);
4396 return rc;
4399 static int fan_set_enable(void)
4401 u8 s;
4402 int rc;
4404 if (!fan_control_allowed)
4405 return -EPERM;
4407 rc = mutex_lock_interruptible(&fan_mutex);
4408 if (rc < 0)
4409 return rc;
4411 switch (fan_control_access_mode) {
4412 case TPACPI_FAN_WR_ACPI_FANS:
4413 case TPACPI_FAN_WR_TPEC:
4414 rc = fan_get_status(&s);
4415 if (rc < 0)
4416 break;
4418 /* Don't go out of emergency fan mode */
4419 if (s != 7) {
4420 s &= 0x07;
4421 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4424 if (!acpi_ec_write(fan_status_offset, s))
4425 rc = -EIO;
4426 else {
4427 tp_features.fan_ctrl_status_undef = 0;
4428 rc = 0;
4430 break;
4432 case TPACPI_FAN_WR_ACPI_SFAN:
4433 rc = fan_get_status(&s);
4434 if (rc < 0)
4435 break;
4437 s &= 0x07;
4439 /* Set fan to at least level 4 */
4440 s |= 4;
4442 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4443 rc= -EIO;
4444 else
4445 rc = 0;
4446 break;
4448 default:
4449 rc = -ENXIO;
4452 mutex_unlock(&fan_mutex);
4453 return rc;
4456 static int fan_set_disable(void)
4458 int rc;
4460 if (!fan_control_allowed)
4461 return -EPERM;
4463 rc = mutex_lock_interruptible(&fan_mutex);
4464 if (rc < 0)
4465 return rc;
4467 rc = 0;
4468 switch (fan_control_access_mode) {
4469 case TPACPI_FAN_WR_ACPI_FANS:
4470 case TPACPI_FAN_WR_TPEC:
4471 if (!acpi_ec_write(fan_status_offset, 0x00))
4472 rc = -EIO;
4473 else {
4474 fan_control_desired_level = 0;
4475 tp_features.fan_ctrl_status_undef = 0;
4477 break;
4479 case TPACPI_FAN_WR_ACPI_SFAN:
4480 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4481 rc = -EIO;
4482 else
4483 fan_control_desired_level = 0;
4484 break;
4486 default:
4487 rc = -ENXIO;
4491 mutex_unlock(&fan_mutex);
4492 return rc;
4495 static int fan_set_speed(int speed)
4497 int rc;
4499 if (!fan_control_allowed)
4500 return -EPERM;
4502 rc = mutex_lock_interruptible(&fan_mutex);
4503 if (rc < 0)
4504 return rc;
4506 rc = 0;
4507 switch (fan_control_access_mode) {
4508 case TPACPI_FAN_WR_ACPI_FANS:
4509 if (speed >= 0 && speed <= 65535) {
4510 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4511 speed, speed, speed))
4512 rc = -EIO;
4513 } else
4514 rc = -EINVAL;
4515 break;
4517 default:
4518 rc = -ENXIO;
4521 mutex_unlock(&fan_mutex);
4522 return rc;
4525 static int fan_read(char *p)
4527 int len = 0;
4528 int rc;
4529 u8 status;
4530 unsigned int speed = 0;
4532 switch (fan_status_access_mode) {
4533 case TPACPI_FAN_RD_ACPI_GFAN:
4534 /* 570, 600e/x, 770e, 770x */
4535 if ((rc = fan_get_status_safe(&status)) < 0)
4536 return rc;
4538 len += sprintf(p + len, "status:\t\t%s\n"
4539 "level:\t\t%d\n",
4540 (status != 0) ? "enabled" : "disabled", status);
4541 break;
4543 case TPACPI_FAN_RD_TPEC:
4544 /* all except 570, 600e/x, 770e, 770x */
4545 if ((rc = fan_get_status_safe(&status)) < 0)
4546 return rc;
4548 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4549 if (status != fan_control_initial_status)
4550 tp_features.fan_ctrl_status_undef = 0;
4551 else
4552 /* Return most likely status. In fact, it
4553 * might be the only possible status */
4554 status = TP_EC_FAN_AUTO;
4557 len += sprintf(p + len, "status:\t\t%s\n",
4558 (status != 0) ? "enabled" : "disabled");
4560 if ((rc = fan_get_speed(&speed)) < 0)
4561 return rc;
4563 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4565 if (status & TP_EC_FAN_FULLSPEED)
4566 /* Disengaged mode takes precedence */
4567 len += sprintf(p + len, "level:\t\tdisengaged\n");
4568 else if (status & TP_EC_FAN_AUTO)
4569 len += sprintf(p + len, "level:\t\tauto\n");
4570 else
4571 len += sprintf(p + len, "level:\t\t%d\n", status);
4572 break;
4574 case TPACPI_FAN_NONE:
4575 default:
4576 len += sprintf(p + len, "status:\t\tnot supported\n");
4579 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
4580 len += sprintf(p + len, "commands:\tlevel <level>");
4582 switch (fan_control_access_mode) {
4583 case TPACPI_FAN_WR_ACPI_SFAN:
4584 len += sprintf(p + len, " (<level> is 0-7)\n");
4585 break;
4587 default:
4588 len += sprintf(p + len, " (<level> is 0-7, "
4589 "auto, disengaged, full-speed)\n");
4590 break;
4594 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
4595 len += sprintf(p + len, "commands:\tenable, disable\n"
4596 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4597 "1-120 (seconds))\n");
4599 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
4600 len += sprintf(p + len, "commands:\tspeed <speed>"
4601 " (<speed> is 0-65535)\n");
4603 return len;
4606 static int fan_write_cmd_level(const char *cmd, int *rc)
4608 int level;
4610 if (strlencmp(cmd, "level auto") == 0)
4611 level = TP_EC_FAN_AUTO;
4612 else if ((strlencmp(cmd, "level disengaged") == 0) |
4613 (strlencmp(cmd, "level full-speed") == 0))
4614 level = TP_EC_FAN_FULLSPEED;
4615 else if (sscanf(cmd, "level %d", &level) != 1)
4616 return 0;
4618 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4619 printk(IBM_ERR "level command accepted for unsupported "
4620 "access mode %d", fan_control_access_mode);
4622 return 1;
4625 static int fan_write_cmd_enable(const char *cmd, int *rc)
4627 if (strlencmp(cmd, "enable") != 0)
4628 return 0;
4630 if ((*rc = fan_set_enable()) == -ENXIO)
4631 printk(IBM_ERR "enable command accepted for unsupported "
4632 "access mode %d", fan_control_access_mode);
4634 return 1;
4637 static int fan_write_cmd_disable(const char *cmd, int *rc)
4639 if (strlencmp(cmd, "disable") != 0)
4640 return 0;
4642 if ((*rc = fan_set_disable()) == -ENXIO)
4643 printk(IBM_ERR "disable command accepted for unsupported "
4644 "access mode %d", fan_control_access_mode);
4646 return 1;
4649 static int fan_write_cmd_speed(const char *cmd, int *rc)
4651 int speed;
4653 /* TODO:
4654 * Support speed <low> <medium> <high> ? */
4656 if (sscanf(cmd, "speed %d", &speed) != 1)
4657 return 0;
4659 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4660 printk(IBM_ERR "speed command accepted for unsupported "
4661 "access mode %d", fan_control_access_mode);
4663 return 1;
4666 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4668 int interval;
4670 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4671 return 0;
4673 if (interval < 0 || interval > 120)
4674 *rc = -EINVAL;
4675 else
4676 fan_watchdog_maxinterval = interval;
4678 return 1;
4681 static int fan_write(char *buf)
4683 char *cmd;
4684 int rc = 0;
4686 while (!rc && (cmd = next_cmd(&buf))) {
4687 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4688 fan_write_cmd_level(cmd, &rc)) &&
4689 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4690 (fan_write_cmd_enable(cmd, &rc) ||
4691 fan_write_cmd_disable(cmd, &rc) ||
4692 fan_write_cmd_watchdog(cmd, &rc))) &&
4693 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4694 fan_write_cmd_speed(cmd, &rc))
4696 rc = -EINVAL;
4697 else if (!rc)
4698 fan_watchdog_reset();
4701 return rc;
4704 static struct ibm_struct fan_driver_data = {
4705 .name = "fan",
4706 .read = fan_read,
4707 .write = fan_write,
4708 .exit = fan_exit,
4711 /****************************************************************************
4712 ****************************************************************************
4714 * Infrastructure
4716 ****************************************************************************
4717 ****************************************************************************/
4719 /* /proc support */
4720 static struct proc_dir_entry *proc_dir;
4722 /* Subdriver registry */
4723 static LIST_HEAD(tpacpi_all_drivers);
4727 * Module and infrastructure proble, init and exit handling
4730 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4731 static const char * __init str_supported(int is_supported)
4733 static char text_unsupported[] __initdata = "not supported";
4735 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4737 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4739 static int __init ibm_init(struct ibm_init_struct *iibm)
4741 int ret;
4742 struct ibm_struct *ibm = iibm->data;
4743 struct proc_dir_entry *entry;
4745 BUG_ON(ibm == NULL);
4747 INIT_LIST_HEAD(&ibm->all_drivers);
4749 if (ibm->flags.experimental && !experimental)
4750 return 0;
4752 dbg_printk(TPACPI_DBG_INIT,
4753 "probing for %s\n", ibm->name);
4755 if (iibm->init) {
4756 ret = iibm->init(iibm);
4757 if (ret > 0)
4758 return 0; /* probe failed */
4759 if (ret)
4760 return ret;
4762 ibm->flags.init_called = 1;
4765 if (ibm->acpi) {
4766 if (ibm->acpi->hid) {
4767 ret = register_tpacpi_subdriver(ibm);
4768 if (ret)
4769 goto err_out;
4772 if (ibm->acpi->notify) {
4773 ret = setup_acpi_notify(ibm);
4774 if (ret == -ENODEV) {
4775 printk(IBM_NOTICE "disabling subdriver %s\n",
4776 ibm->name);
4777 ret = 0;
4778 goto err_out;
4780 if (ret < 0)
4781 goto err_out;
4785 dbg_printk(TPACPI_DBG_INIT,
4786 "%s installed\n", ibm->name);
4788 if (ibm->read) {
4789 entry = create_proc_entry(ibm->name,
4790 S_IFREG | S_IRUGO | S_IWUSR,
4791 proc_dir);
4792 if (!entry) {
4793 printk(IBM_ERR "unable to create proc entry %s\n",
4794 ibm->name);
4795 ret = -ENODEV;
4796 goto err_out;
4798 entry->owner = THIS_MODULE;
4799 entry->data = ibm;
4800 entry->read_proc = &dispatch_procfs_read;
4801 if (ibm->write)
4802 entry->write_proc = &dispatch_procfs_write;
4803 ibm->flags.proc_created = 1;
4806 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4808 return 0;
4810 err_out:
4811 dbg_printk(TPACPI_DBG_INIT,
4812 "%s: at error exit path with result %d\n",
4813 ibm->name, ret);
4815 ibm_exit(ibm);
4816 return (ret < 0)? ret : 0;
4819 static void ibm_exit(struct ibm_struct *ibm)
4821 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4823 list_del_init(&ibm->all_drivers);
4825 if (ibm->flags.acpi_notify_installed) {
4826 dbg_printk(TPACPI_DBG_EXIT,
4827 "%s: acpi_remove_notify_handler\n", ibm->name);
4828 BUG_ON(!ibm->acpi);
4829 acpi_remove_notify_handler(*ibm->acpi->handle,
4830 ibm->acpi->type,
4831 dispatch_acpi_notify);
4832 ibm->flags.acpi_notify_installed = 0;
4833 ibm->flags.acpi_notify_installed = 0;
4836 if (ibm->flags.proc_created) {
4837 dbg_printk(TPACPI_DBG_EXIT,
4838 "%s: remove_proc_entry\n", ibm->name);
4839 remove_proc_entry(ibm->name, proc_dir);
4840 ibm->flags.proc_created = 0;
4843 if (ibm->flags.acpi_driver_registered) {
4844 dbg_printk(TPACPI_DBG_EXIT,
4845 "%s: acpi_bus_unregister_driver\n", ibm->name);
4846 BUG_ON(!ibm->acpi);
4847 acpi_bus_unregister_driver(ibm->acpi->driver);
4848 kfree(ibm->acpi->driver);
4849 ibm->acpi->driver = NULL;
4850 ibm->flags.acpi_driver_registered = 0;
4853 if (ibm->flags.init_called && ibm->exit) {
4854 ibm->exit();
4855 ibm->flags.init_called = 0;
4858 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4861 /* Probing */
4863 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
4865 struct dmi_device *dev = NULL;
4866 char ec_fw_string[18];
4868 if (!tp)
4869 return;
4871 memset(tp, 0, sizeof(*tp));
4873 if (dmi_name_in_vendors("IBM"))
4874 tp->vendor = PCI_VENDOR_ID_IBM;
4875 else if (dmi_name_in_vendors("LENOVO"))
4876 tp->vendor = PCI_VENDOR_ID_LENOVO;
4877 else
4878 return;
4880 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4881 GFP_KERNEL);
4882 if (!tp->bios_version_str)
4883 return;
4884 tp->bios_model = tp->bios_version_str[0]
4885 | (tp->bios_version_str[1] << 8);
4888 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4889 * X32 or newer, all Z series; Some models must have an
4890 * up-to-date BIOS or they will not be detected.
4892 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4894 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4895 if (sscanf(dev->name,
4896 "IBM ThinkPad Embedded Controller -[%17c",
4897 ec_fw_string) == 1) {
4898 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4899 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4901 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4902 tp->ec_model = ec_fw_string[0]
4903 | (ec_fw_string[1] << 8);
4904 break;
4908 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4909 GFP_KERNEL);
4910 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4911 kfree(tp->model_str);
4912 tp->model_str = NULL;
4916 static int __init probe_for_thinkpad(void)
4918 int is_thinkpad;
4920 if (acpi_disabled)
4921 return -ENODEV;
4924 * Non-ancient models have better DMI tagging, but very old models
4925 * don't.
4927 is_thinkpad = (thinkpad_id.model_str != NULL);
4929 /* ec is required because many other handles are relative to it */
4930 IBM_ACPIHANDLE_INIT(ec);
4931 if (!ec_handle) {
4932 if (is_thinkpad)
4933 printk(IBM_ERR
4934 "Not yet supported ThinkPad detected!\n");
4935 return -ENODEV;
4939 * Risks a regression on very old machines, but reduces potential
4940 * false positives a damn great deal
4942 if (!is_thinkpad)
4943 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
4945 if (!is_thinkpad && !force_load)
4946 return -ENODEV;
4948 return 0;
4952 /* Module init, exit, parameters */
4954 static struct ibm_init_struct ibms_init[] __initdata = {
4956 .init = thinkpad_acpi_driver_init,
4957 .data = &thinkpad_acpi_driver_data,
4960 .init = hotkey_init,
4961 .data = &hotkey_driver_data,
4964 .init = bluetooth_init,
4965 .data = &bluetooth_driver_data,
4968 .init = wan_init,
4969 .data = &wan_driver_data,
4972 .init = video_init,
4973 .data = &video_driver_data,
4976 .init = light_init,
4977 .data = &light_driver_data,
4979 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4981 .init = dock_init,
4982 .data = &dock_driver_data[0],
4985 .init = dock_init2,
4986 .data = &dock_driver_data[1],
4988 #endif
4989 #ifdef CONFIG_THINKPAD_ACPI_BAY
4991 .init = bay_init,
4992 .data = &bay_driver_data,
4994 #endif
4996 .init = cmos_init,
4997 .data = &cmos_driver_data,
5000 .init = led_init,
5001 .data = &led_driver_data,
5004 .init = beep_init,
5005 .data = &beep_driver_data,
5008 .init = thermal_init,
5009 .data = &thermal_driver_data,
5012 .data = &ecdump_driver_data,
5015 .init = brightness_init,
5016 .data = &brightness_driver_data,
5019 .data = &volume_driver_data,
5022 .init = fan_init,
5023 .data = &fan_driver_data,
5027 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5029 unsigned int i;
5030 struct ibm_struct *ibm;
5032 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5033 ibm = ibms_init[i].data;
5034 BUG_ON(ibm == NULL);
5036 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5037 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5038 return -ENOSPC;
5039 strcpy(ibms_init[i].param, val);
5040 strcat(ibms_init[i].param, ",");
5041 return 0;
5045 return -EINVAL;
5048 static int experimental;
5049 module_param(experimental, int, 0);
5051 static u32 dbg_level;
5052 module_param_named(debug, dbg_level, uint, 0);
5054 static int force_load;
5055 module_param(force_load, bool, 0);
5057 static int fan_control_allowed;
5058 module_param_named(fan_control, fan_control_allowed, bool, 0);
5060 static int brightness_mode;
5061 module_param_named(brightness_mode, brightness_mode, int, 0);
5063 static unsigned int hotkey_report_mode;
5064 module_param(hotkey_report_mode, uint, 0);
5066 #define IBM_PARAM(feature) \
5067 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
5069 IBM_PARAM(hotkey);
5070 IBM_PARAM(bluetooth);
5071 IBM_PARAM(video);
5072 IBM_PARAM(light);
5073 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5074 IBM_PARAM(dock);
5075 #endif
5076 #ifdef CONFIG_THINKPAD_ACPI_BAY
5077 IBM_PARAM(bay);
5078 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5079 IBM_PARAM(cmos);
5080 IBM_PARAM(led);
5081 IBM_PARAM(beep);
5082 IBM_PARAM(ecdump);
5083 IBM_PARAM(brightness);
5084 IBM_PARAM(volume);
5085 IBM_PARAM(fan);
5087 static int __init thinkpad_acpi_module_init(void)
5089 int ret, i;
5091 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5093 /* Parameter checking */
5094 if (hotkey_report_mode > 2)
5095 return -EINVAL;
5097 /* Driver-level probe */
5099 get_thinkpad_model_data(&thinkpad_id);
5100 ret = probe_for_thinkpad();
5101 if (ret) {
5102 thinkpad_acpi_module_exit();
5103 return ret;
5106 /* Driver initialization */
5108 IBM_ACPIHANDLE_INIT(ecrd);
5109 IBM_ACPIHANDLE_INIT(ecwr);
5111 mutex_init(&thinkpad_nvram_mutex);
5113 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
5114 if (!proc_dir) {
5115 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
5116 thinkpad_acpi_module_exit();
5117 return -ENODEV;
5119 proc_dir->owner = THIS_MODULE;
5121 ret = platform_driver_register(&tpacpi_pdriver);
5122 if (ret) {
5123 printk(IBM_ERR "unable to register platform driver\n");
5124 thinkpad_acpi_module_exit();
5125 return ret;
5127 tp_features.platform_drv_registered = 1;
5129 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
5130 if (ret) {
5131 printk(IBM_ERR "unable to create sysfs driver attributes\n");
5132 thinkpad_acpi_module_exit();
5133 return ret;
5135 tp_features.platform_drv_attrs_registered = 1;
5138 /* Device initialization */
5139 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
5140 NULL, 0);
5141 if (IS_ERR(tpacpi_pdev)) {
5142 ret = PTR_ERR(tpacpi_pdev);
5143 tpacpi_pdev = NULL;
5144 printk(IBM_ERR "unable to register platform device\n");
5145 thinkpad_acpi_module_exit();
5146 return ret;
5148 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
5149 if (IS_ERR(tpacpi_hwmon)) {
5150 ret = PTR_ERR(tpacpi_hwmon);
5151 tpacpi_hwmon = NULL;
5152 printk(IBM_ERR "unable to register hwmon device\n");
5153 thinkpad_acpi_module_exit();
5154 return ret;
5156 mutex_init(&tpacpi_inputdev_send_mutex);
5157 tpacpi_inputdev = input_allocate_device();
5158 if (!tpacpi_inputdev) {
5159 printk(IBM_ERR "unable to allocate input device\n");
5160 thinkpad_acpi_module_exit();
5161 return -ENOMEM;
5162 } else {
5163 /* Prepare input device, but don't register */
5164 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5165 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
5166 tpacpi_inputdev->id.bustype = BUS_HOST;
5167 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5168 thinkpad_id.vendor :
5169 PCI_VENDOR_ID_IBM;
5170 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5171 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5173 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5174 ret = ibm_init(&ibms_init[i]);
5175 if (ret >= 0 && *ibms_init[i].param)
5176 ret = ibms_init[i].data->write(ibms_init[i].param);
5177 if (ret < 0) {
5178 thinkpad_acpi_module_exit();
5179 return ret;
5182 ret = input_register_device(tpacpi_inputdev);
5183 if (ret < 0) {
5184 printk(IBM_ERR "unable to register input device\n");
5185 thinkpad_acpi_module_exit();
5186 return ret;
5187 } else {
5188 tp_features.input_device_registered = 1;
5191 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
5192 return 0;
5195 static void thinkpad_acpi_module_exit(void)
5197 struct ibm_struct *ibm, *itmp;
5199 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5201 list_for_each_entry_safe_reverse(ibm, itmp,
5202 &tpacpi_all_drivers,
5203 all_drivers) {
5204 ibm_exit(ibm);
5207 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5209 if (tpacpi_inputdev) {
5210 if (tp_features.input_device_registered)
5211 input_unregister_device(tpacpi_inputdev);
5212 else
5213 input_free_device(tpacpi_inputdev);
5216 if (tpacpi_hwmon)
5217 hwmon_device_unregister(tpacpi_hwmon);
5219 if (tpacpi_pdev)
5220 platform_device_unregister(tpacpi_pdev);
5222 if (tp_features.platform_drv_attrs_registered)
5223 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5225 if (tp_features.platform_drv_registered)
5226 platform_driver_unregister(&tpacpi_pdriver);
5228 if (proc_dir)
5229 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
5231 kfree(thinkpad_id.bios_version_str);
5232 kfree(thinkpad_id.ec_version_str);
5233 kfree(thinkpad_id.model_str);
5236 module_init(thinkpad_acpi_module_init);
5237 module_exit(thinkpad_acpi_module_exit);