ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
bloba09c41f096c52dfb2e90b35cf86568653ba0b834
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-20070916"
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;
423 ibm->acpi->driver->ops.add = &tpacpi_device_add;
425 rc = acpi_bus_register_driver(ibm->acpi->driver);
426 if (rc < 0) {
427 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
428 ibm->acpi->hid, rc);
429 kfree(ibm->acpi->driver);
430 ibm->acpi->driver = NULL;
431 } else if (!rc)
432 ibm->flags.acpi_driver_registered = 1;
434 return rc;
438 /****************************************************************************
439 ****************************************************************************
441 * Procfs Helpers
443 ****************************************************************************
444 ****************************************************************************/
446 static int dispatch_procfs_read(char *page, char **start, off_t off,
447 int count, int *eof, void *data)
449 struct ibm_struct *ibm = data;
450 int len;
452 if (!ibm || !ibm->read)
453 return -EINVAL;
455 len = ibm->read(page);
456 if (len < 0)
457 return len;
459 if (len <= off + count)
460 *eof = 1;
461 *start = page + off;
462 len -= off;
463 if (len > count)
464 len = count;
465 if (len < 0)
466 len = 0;
468 return len;
471 static int dispatch_procfs_write(struct file *file,
472 const char __user * userbuf,
473 unsigned long count, void *data)
475 struct ibm_struct *ibm = data;
476 char *kernbuf;
477 int ret;
479 if (!ibm || !ibm->write)
480 return -EINVAL;
482 kernbuf = kmalloc(count + 2, GFP_KERNEL);
483 if (!kernbuf)
484 return -ENOMEM;
486 if (copy_from_user(kernbuf, userbuf, count)) {
487 kfree(kernbuf);
488 return -EFAULT;
491 kernbuf[count] = 0;
492 strcat(kernbuf, ",");
493 ret = ibm->write(kernbuf);
494 if (ret == 0)
495 ret = count;
497 kfree(kernbuf);
499 return ret;
502 static char *next_cmd(char **cmds)
504 char *start = *cmds;
505 char *end;
507 while ((end = strchr(start, ',')) && end == start)
508 start = end + 1;
510 if (!end)
511 return NULL;
513 *end = 0;
514 *cmds = end + 1;
515 return start;
519 /****************************************************************************
520 ****************************************************************************
522 * Device model: input, hwmon and platform
524 ****************************************************************************
525 ****************************************************************************/
527 static struct platform_device *tpacpi_pdev;
528 static struct class_device *tpacpi_hwmon;
529 static struct input_dev *tpacpi_inputdev;
530 static struct mutex tpacpi_inputdev_send_mutex;
533 static int tpacpi_resume_handler(struct platform_device *pdev)
535 struct ibm_struct *ibm, *itmp;
537 list_for_each_entry_safe(ibm, itmp,
538 &tpacpi_all_drivers,
539 all_drivers) {
540 if (ibm->resume)
541 (ibm->resume)();
544 return 0;
547 static struct platform_driver tpacpi_pdriver = {
548 .driver = {
549 .name = IBM_DRVR_NAME,
550 .owner = THIS_MODULE,
552 .resume = tpacpi_resume_handler,
556 /*************************************************************************
557 * thinkpad-acpi driver attributes
560 /* interface_version --------------------------------------------------- */
561 static ssize_t tpacpi_driver_interface_version_show(
562 struct device_driver *drv,
563 char *buf)
565 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
568 static DRIVER_ATTR(interface_version, S_IRUGO,
569 tpacpi_driver_interface_version_show, NULL);
571 /* debug_level --------------------------------------------------------- */
572 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
573 char *buf)
575 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
578 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
579 const char *buf, size_t count)
581 unsigned long t;
583 if (parse_strtoul(buf, 0xffff, &t))
584 return -EINVAL;
586 dbg_level = t;
588 return count;
591 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
592 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
594 /* version ------------------------------------------------------------- */
595 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
596 char *buf)
598 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
601 static DRIVER_ATTR(version, S_IRUGO,
602 tpacpi_driver_version_show, NULL);
604 /* --------------------------------------------------------------------- */
606 static struct driver_attribute* tpacpi_driver_attributes[] = {
607 &driver_attr_debug_level, &driver_attr_version,
608 &driver_attr_interface_version,
611 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
613 int i, res;
615 i = 0;
616 res = 0;
617 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
618 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
619 i++;
622 return res;
625 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
627 int i;
629 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
630 driver_remove_file(drv, tpacpi_driver_attributes[i]);
633 /*************************************************************************
634 * sysfs support helpers
637 struct attribute_set_obj {
638 struct attribute_set s;
639 struct attribute *a;
640 } __attribute__((packed));
642 static struct attribute_set *create_attr_set(unsigned int max_members,
643 const char* name)
645 struct attribute_set_obj *sobj;
647 if (max_members == 0)
648 return NULL;
650 /* Allocates space for implicit NULL at the end too */
651 sobj = kzalloc(sizeof(struct attribute_set_obj) +
652 max_members * sizeof(struct attribute *),
653 GFP_KERNEL);
654 if (!sobj)
655 return NULL;
656 sobj->s.max_members = max_members;
657 sobj->s.group.attrs = &sobj->a;
658 sobj->s.group.name = name;
660 return &sobj->s;
663 /* not multi-threaded safe, use it in a single thread per set */
664 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
666 if (!s || !attr)
667 return -EINVAL;
669 if (s->members >= s->max_members)
670 return -ENOMEM;
672 s->group.attrs[s->members] = attr;
673 s->members++;
675 return 0;
678 static int add_many_to_attr_set(struct attribute_set* s,
679 struct attribute **attr,
680 unsigned int count)
682 int i, res;
684 for (i = 0; i < count; i++) {
685 res = add_to_attr_set(s, attr[i]);
686 if (res)
687 return res;
690 return 0;
693 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
695 sysfs_remove_group(kobj, &s->group);
696 destroy_attr_set(s);
699 static int parse_strtoul(const char *buf,
700 unsigned long max, unsigned long *value)
702 char *endp;
704 *value = simple_strtoul(buf, &endp, 0);
705 while (*endp && isspace(*endp))
706 endp++;
707 if (*endp || *value > max)
708 return -EINVAL;
710 return 0;
713 /****************************************************************************
714 ****************************************************************************
716 * Subdrivers
718 ****************************************************************************
719 ****************************************************************************/
721 /*************************************************************************
722 * thinkpad-acpi init subdriver
725 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
727 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
728 printk(IBM_INFO "%s\n", IBM_URL);
730 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
731 (thinkpad_id.bios_version_str) ?
732 thinkpad_id.bios_version_str : "unknown",
733 (thinkpad_id.ec_version_str) ?
734 thinkpad_id.ec_version_str : "unknown");
736 if (thinkpad_id.vendor && thinkpad_id.model_str)
737 printk(IBM_INFO "%s %s\n",
738 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
739 "IBM" : ((thinkpad_id.vendor ==
740 PCI_VENDOR_ID_LENOVO) ?
741 "Lenovo" : "Unknown vendor"),
742 thinkpad_id.model_str);
744 return 0;
747 static int thinkpad_acpi_driver_read(char *p)
749 int len = 0;
751 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
752 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
754 return len;
757 static struct ibm_struct thinkpad_acpi_driver_data = {
758 .name = "driver",
759 .read = thinkpad_acpi_driver_read,
762 /*************************************************************************
763 * Hotkey subdriver
766 enum { /* Keys available through NVRAM polling */
767 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb8000U,
770 enum { /* Positions of some of the keys in hotkey masks */
771 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
772 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
773 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
774 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
775 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
776 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
777 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
778 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
779 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
780 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
781 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
784 enum { /* NVRAM to ACPI HKEY group map */
785 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
786 TP_ACPI_HKEY_ZOOM_MASK |
787 TP_ACPI_HKEY_DISPSWTCH_MASK |
788 TP_ACPI_HKEY_HIBERNATE_MASK,
789 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
790 TP_ACPI_HKEY_BRGHTDWN_MASK,
791 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
792 TP_ACPI_HKEY_VOLDWN_MASK |
793 TP_ACPI_HKEY_MUTE_MASK,
796 struct tp_nvram_state {
797 u16 thinkpad_toggle:1;
798 u16 zoom_toggle:1;
799 u16 display_toggle:1;
800 u16 thinklight_toggle:1;
801 u16 hibernate_toggle:1;
802 u16 displayexp_toggle:1;
803 u16 display_state:1;
804 u16 brightness_toggle:1;
805 u16 volume_toggle:1;
806 u16 mute:1;
808 u8 brightness_level;
809 u8 volume_level;
812 static int hotkey_orig_status;
813 static u32 hotkey_orig_mask;
814 static u32 hotkey_all_mask;
815 static u32 hotkey_reserved_mask;
817 static struct task_struct *tpacpi_hotkey_task;
818 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
819 static int hotkey_poll_freq = 4; /* Hz */
820 static struct mutex hotkey_poll_mutex;
821 static struct mutex thinkpad_nvram_mutex;
822 static struct mutex hotkey_thread_mutex;
824 static u16 *hotkey_keycode_map;
826 static struct attribute_set *hotkey_dev_attributes;
828 static void tpacpi_input_send_key(unsigned int scancode)
830 unsigned int keycode;
832 keycode = hotkey_keycode_map[scancode];
834 if (keycode != KEY_RESERVED) {
835 mutex_lock(&tpacpi_inputdev_send_mutex);
837 input_report_key(tpacpi_inputdev, keycode, 1);
838 if (keycode == KEY_UNKNOWN)
839 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
840 scancode);
841 input_sync(tpacpi_inputdev);
843 input_report_key(tpacpi_inputdev, keycode, 0);
844 if (keycode == KEY_UNKNOWN)
845 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
846 scancode);
847 input_sync(tpacpi_inputdev);
849 mutex_unlock(&tpacpi_inputdev_send_mutex);
853 static int hotkey_get_wlsw(int *status)
855 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
856 return -EIO;
857 return 0;
860 static void tpacpi_input_send_radiosw(void)
862 int wlsw;
864 mutex_lock(&tpacpi_inputdev_send_mutex);
866 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
867 input_report_switch(tpacpi_inputdev,
868 SW_RADIO, !!wlsw);
869 input_sync(tpacpi_inputdev);
872 mutex_unlock(&tpacpi_inputdev_send_mutex);
875 static void hotkey_read_nvram(struct tp_nvram_state *n)
877 u8 d;
879 if (hotkey_source_mask & TP_NVRAM_HKEY_GROUP_HK2) {
880 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
881 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
882 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
883 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
884 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
886 if (hotkey_source_mask & TP_ACPI_HKEY_THNKLGHT_MASK) {
887 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
888 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
890 if (hotkey_source_mask & TP_ACPI_HKEY_DISPXPAND_MASK) {
891 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
892 n->displayexp_toggle =
893 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
895 if (hotkey_source_mask & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
896 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
897 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
898 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
899 n->brightness_toggle =
900 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
902 if (hotkey_source_mask & TP_NVRAM_HKEY_GROUP_VOLUME) {
903 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
904 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
905 >> TP_NVRAM_POS_LEVEL_VOLUME;
906 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
907 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
911 #define TPACPI_COMPARE_KEY(__scancode, __member) \
912 if (hotkey_source_mask & (1 << __scancode) && \
913 oldn->__member != newn->__member) \
914 tpacpi_input_send_key(__scancode);
916 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
917 struct tp_nvram_state *newn)
919 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle)
920 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle)
921 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle)
922 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle)
924 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle)
926 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle)
928 /* handle volume */
929 if (oldn->volume_toggle != newn->volume_toggle) {
930 if (oldn->mute != newn->mute) {
931 tpacpi_input_send_key(TP_ACPI_HOTKEYSCAN_MUTE);
933 if (oldn->volume_level > newn->volume_level) {
934 tpacpi_input_send_key(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
935 } else if (oldn->volume_level < newn->volume_level) {
936 tpacpi_input_send_key(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
939 /* we could "else" here and issue mute regardless of a change
940 * in the mute bit (and therefore notify of multiple presses of
941 * the mute button), but that is *incorrect* semanthics for
942 * a thinkpad mixer, where "mute" always mutes, and a volume
943 * key unmutes.
947 /* handle brightness */
948 if (oldn->brightness_toggle != newn->brightness_toggle) {
949 if (oldn->brightness_level < newn->brightness_level) {
950 tpacpi_input_send_key(TP_ACPI_HOTKEYSCAN_FNHOME);
951 } else if (oldn->brightness_level > newn->brightness_level) {
952 tpacpi_input_send_key(TP_ACPI_HOTKEYSCAN_FNEND);
957 #undef TPACPI_COMPARE_KEY
959 static int hotkey_kthread(void *data)
961 struct tp_nvram_state s[2];
962 unsigned int si, so;
963 unsigned long t;
965 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
966 return 0;
968 mutex_lock(&hotkey_thread_mutex);
970 si = 1;
971 so = 0;
972 t = 0;
974 /* Initial state for compares */
975 hotkey_read_nvram(&s[so]);
977 while (!kthread_should_stop() && hotkey_poll_freq) {
978 t = msleep_interruptible(1000/hotkey_poll_freq + t);
979 if (unlikely(kthread_should_stop()))
980 break;
981 if (unlikely(try_to_freeze())) {
982 si = so; /* forget old state on thaw */
983 t = 0;
985 if (t > 0)
986 continue;
988 hotkey_read_nvram(&s[si]);
989 hotkey_compare_and_issue_event(&s[so], &s[si]);
990 so = si;
991 si ^= 1;
994 mutex_unlock(&hotkey_thread_mutex);
995 return 0;
998 static void hotkey_poll_stop_sync(void)
1000 if (tpacpi_hotkey_task) {
1001 kthread_stop(tpacpi_hotkey_task);
1002 tpacpi_hotkey_task = NULL;
1003 mutex_lock(&hotkey_thread_mutex);
1004 /* at this point, the thread did exit */
1005 mutex_unlock(&hotkey_thread_mutex);
1009 /* call with hotkey_poll_mutex held */
1010 static void hotkey_poll_setup(int force)
1012 if (force)
1013 hotkey_poll_stop_sync();
1015 if (hotkey_source_mask != 0 && hotkey_poll_freq > 0 &&
1016 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1017 if (!tpacpi_hotkey_task) {
1018 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1019 NULL, IBM_FILE "d");
1020 if (IS_ERR(tpacpi_hotkey_task)) {
1021 tpacpi_hotkey_task = NULL;
1022 printk(IBM_ERR "could not create kernel thread "
1023 "for hotkey polling\n");
1026 } else {
1027 hotkey_poll_stop_sync();
1031 static int hotkey_poll_setup_safe(int force)
1033 int rc;
1035 rc = mutex_lock_interruptible(&hotkey_poll_mutex);
1036 if (rc < 0)
1037 return rc;
1039 hotkey_poll_setup(force);
1040 mutex_unlock(&hotkey_poll_mutex);
1042 return 0;
1045 static void hotkey_warn_no_poll(void)
1047 if (hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1048 printk(IBM_NOTICE "hot keys 0x%08x require polling, "
1049 "which is currently disabled\n",
1050 hotkey_source_mask);
1055 static int hotkey_inputdev_open(struct input_dev *dev)
1057 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
1058 return -EBUSY;
1059 /* enable hotkey polling when required */
1060 return hotkey_poll_setup_safe(0);
1063 static void hotkey_inputdev_close(struct input_dev *dev)
1065 /* disable hotkey polling when possible */
1066 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
1067 hotkey_poll_setup_safe(0);
1070 /* sysfs hotkey enable ------------------------------------------------- */
1071 static ssize_t hotkey_enable_show(struct device *dev,
1072 struct device_attribute *attr,
1073 char *buf)
1075 int res, status;
1076 u32 mask;
1078 res = hotkey_get(&status, &mask);
1079 if (res)
1080 return res;
1082 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1085 static ssize_t hotkey_enable_store(struct device *dev,
1086 struct device_attribute *attr,
1087 const char *buf, size_t count)
1089 unsigned long t;
1090 int res, status;
1091 u32 mask;
1093 if (parse_strtoul(buf, 1, &t))
1094 return -EINVAL;
1096 res = hotkey_get(&status, &mask);
1097 if (!res)
1098 res = hotkey_set(t, mask);
1100 return (res) ? res : count;
1103 static struct device_attribute dev_attr_hotkey_enable =
1104 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1105 hotkey_enable_show, hotkey_enable_store);
1107 /* sysfs hotkey mask --------------------------------------------------- */
1108 static ssize_t hotkey_mask_show(struct device *dev,
1109 struct device_attribute *attr,
1110 char *buf)
1112 int res, status;
1113 u32 mask;
1115 res = hotkey_get(&status, &mask);
1116 if (res)
1117 return res;
1119 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
1122 static ssize_t hotkey_mask_store(struct device *dev,
1123 struct device_attribute *attr,
1124 const char *buf, size_t count)
1126 unsigned long t;
1127 int res, status;
1128 u32 mask;
1130 if (parse_strtoul(buf, 0xffffffffUL, &t))
1131 return -EINVAL;
1133 res = hotkey_get(&status, &mask);
1134 if (!res)
1135 hotkey_set(status, t);
1137 return (res) ? res : count;
1140 static struct device_attribute dev_attr_hotkey_mask =
1141 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1142 hotkey_mask_show, hotkey_mask_store);
1144 /* sysfs hotkey bios_enabled ------------------------------------------- */
1145 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1146 struct device_attribute *attr,
1147 char *buf)
1149 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1152 static struct device_attribute dev_attr_hotkey_bios_enabled =
1153 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1155 /* sysfs hotkey bios_mask ---------------------------------------------- */
1156 static ssize_t hotkey_bios_mask_show(struct device *dev,
1157 struct device_attribute *attr,
1158 char *buf)
1160 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1163 static struct device_attribute dev_attr_hotkey_bios_mask =
1164 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1166 /* sysfs hotkey all_mask ----------------------------------------------- */
1167 static ssize_t hotkey_all_mask_show(struct device *dev,
1168 struct device_attribute *attr,
1169 char *buf)
1171 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1172 hotkey_all_mask | hotkey_source_mask);
1175 static struct device_attribute dev_attr_hotkey_all_mask =
1176 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1178 /* sysfs hotkey recommended_mask --------------------------------------- */
1179 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1180 struct device_attribute *attr,
1181 char *buf)
1183 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1184 (hotkey_all_mask | hotkey_source_mask)
1185 & ~hotkey_reserved_mask);
1188 static struct device_attribute dev_attr_hotkey_recommended_mask =
1189 __ATTR(hotkey_recommended_mask, S_IRUGO,
1190 hotkey_recommended_mask_show, NULL);
1192 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1193 static ssize_t hotkey_source_mask_show(struct device *dev,
1194 struct device_attribute *attr,
1195 char *buf)
1197 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1200 static ssize_t hotkey_source_mask_store(struct device *dev,
1201 struct device_attribute *attr,
1202 const char *buf, size_t count)
1204 unsigned long t;
1205 int rc;
1207 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1208 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1209 return -EINVAL;
1211 rc = mutex_lock_interruptible(&hotkey_poll_mutex);
1212 if (rc < 0)
1213 return rc;
1215 hotkey_source_mask = t;
1217 hotkey_poll_setup(1);
1218 hotkey_warn_no_poll();
1219 mutex_unlock(&hotkey_poll_mutex);
1221 return count;
1224 static struct device_attribute dev_attr_hotkey_source_mask =
1225 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1226 hotkey_source_mask_show, hotkey_source_mask_store);
1228 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1229 static ssize_t hotkey_poll_freq_show(struct device *dev,
1230 struct device_attribute *attr,
1231 char *buf)
1233 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1236 static ssize_t hotkey_poll_freq_store(struct device *dev,
1237 struct device_attribute *attr,
1238 const char *buf, size_t count)
1240 unsigned long t;
1241 int rc;
1243 if (parse_strtoul(buf, 25, &t))
1244 return -EINVAL;
1246 rc = mutex_lock_interruptible(&hotkey_poll_mutex);
1247 if (rc < 0)
1248 return rc;
1250 hotkey_poll_freq = t;
1252 hotkey_poll_setup(0);
1253 hotkey_warn_no_poll();
1254 mutex_unlock(&hotkey_poll_mutex);
1256 return count;
1259 static struct device_attribute dev_attr_hotkey_poll_freq =
1260 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1261 hotkey_poll_freq_show, hotkey_poll_freq_store);
1263 /* sysfs hotkey radio_sw ----------------------------------------------- */
1264 static ssize_t hotkey_radio_sw_show(struct device *dev,
1265 struct device_attribute *attr,
1266 char *buf)
1268 int res, s;
1269 res = hotkey_get_wlsw(&s);
1270 if (res < 0)
1271 return res;
1273 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1276 static struct device_attribute dev_attr_hotkey_radio_sw =
1277 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1279 /* sysfs hotkey report_mode -------------------------------------------- */
1280 static ssize_t hotkey_report_mode_show(struct device *dev,
1281 struct device_attribute *attr,
1282 char *buf)
1284 return snprintf(buf, PAGE_SIZE, "%d\n",
1285 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1288 static ssize_t hotkey_report_mode_store(struct device *dev,
1289 struct device_attribute *attr,
1290 const char *buf, size_t count)
1292 unsigned long t;
1293 int res = 0;
1295 if (parse_strtoul(buf, 2, &t) || (t < 1))
1296 return -EINVAL;
1298 if (t != hotkey_report_mode) {
1299 if (tp_features.hotkey_report_mode_locked)
1300 return -EPERM;
1301 hotkey_report_mode = t;
1302 printk(IBM_NOTICE "hot key report mode set to %d\n",
1303 hotkey_report_mode);
1306 return (res) ? res : count;
1309 static struct device_attribute dev_attr_hotkey_report_mode =
1310 __ATTR(hotkey_report_mode, S_IWUSR | S_IRUGO,
1311 hotkey_report_mode_show, hotkey_report_mode_store);
1313 /* --------------------------------------------------------------------- */
1315 static struct attribute *hotkey_attributes[] __initdata = {
1316 &dev_attr_hotkey_enable.attr,
1317 &dev_attr_hotkey_report_mode.attr,
1320 static struct attribute *hotkey_mask_attributes[] __initdata = {
1321 &dev_attr_hotkey_mask.attr,
1322 &dev_attr_hotkey_bios_enabled.attr,
1323 &dev_attr_hotkey_bios_mask.attr,
1324 &dev_attr_hotkey_all_mask.attr,
1325 &dev_attr_hotkey_recommended_mask.attr,
1326 &dev_attr_hotkey_source_mask.attr,
1327 &dev_attr_hotkey_poll_freq.attr,
1330 static int __init hotkey_init(struct ibm_init_struct *iibm)
1333 static u16 ibm_keycode_map[] __initdata = {
1334 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1335 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1336 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1337 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1338 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
1339 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1340 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1341 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1342 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1343 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
1344 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1345 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1346 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1347 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1348 KEY_RESERVED, /* 0x14: VOLUME UP */
1349 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1350 KEY_RESERVED, /* 0x16: MUTE */
1351 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1352 /* (assignments unknown, please report if found) */
1353 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1354 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1356 static u16 lenovo_keycode_map[] __initdata = {
1357 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1358 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1359 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1360 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1361 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
1362 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1363 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1364 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1365 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
1366 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
1367 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
1368 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1369 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1370 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1371 KEY_RESERVED, /* 0x14: VOLUME UP */
1372 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1373 KEY_RESERVED, /* 0x16: MUTE */
1374 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1375 /* (assignments unknown, please report if found) */
1376 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1377 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1380 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1381 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1382 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1384 int res, i;
1385 int status;
1386 int hkeyv;
1388 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1390 BUG_ON(!tpacpi_inputdev);
1391 BUG_ON(tpacpi_inputdev->open != NULL ||
1392 tpacpi_inputdev->close != NULL);
1394 IBM_ACPIHANDLE_INIT(hkey);
1395 mutex_init(&hotkey_mutex);
1396 mutex_init(&hotkey_poll_mutex);
1397 mutex_init(&hotkey_thread_mutex);
1399 /* hotkey not supported on 570 */
1400 tp_features.hotkey = hkey_handle != NULL;
1402 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1403 str_supported(tp_features.hotkey));
1405 if (tp_features.hotkey) {
1406 hotkey_dev_attributes = create_attr_set(10, NULL);
1407 if (!hotkey_dev_attributes)
1408 return -ENOMEM;
1409 res = add_many_to_attr_set(hotkey_dev_attributes,
1410 hotkey_attributes,
1411 ARRAY_SIZE(hotkey_attributes));
1412 if (res)
1413 return res;
1415 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1416 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1417 for HKEY interface version 0x100 */
1418 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1419 if ((hkeyv >> 8) != 1) {
1420 printk(IBM_ERR "unknown version of the "
1421 "HKEY interface: 0x%x\n", hkeyv);
1422 printk(IBM_ERR "please report this to %s\n",
1423 IBM_MAIL);
1424 } else {
1426 * MHKV 0x100 in A31, R40, R40e,
1427 * T4x, X31, and later
1428 * */
1429 tp_features.hotkey_mask = 1;
1433 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1434 str_supported(tp_features.hotkey_mask));
1436 if (tp_features.hotkey_mask) {
1437 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1438 "MHKA", "qd")) {
1439 printk(IBM_ERR
1440 "missing MHKA handler, "
1441 "please report this to %s\n",
1442 IBM_MAIL);
1443 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1447 hotkey_source_mask = TPACPI_HKEY_NVRAM_KNOWN_MASK
1448 & ~hotkey_all_mask;
1450 vdbg_printk(TPACPI_DBG_INIT,
1451 "hotkey source mask 0x%08x, polling freq %d\n",
1452 hotkey_source_mask, hotkey_poll_freq);
1454 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
1455 if (!res && tp_features.hotkey_mask) {
1456 res = add_many_to_attr_set(hotkey_dev_attributes,
1457 hotkey_mask_attributes,
1458 ARRAY_SIZE(hotkey_mask_attributes));
1461 /* Not all thinkpads have a hardware radio switch */
1462 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1463 tp_features.hotkey_wlsw = 1;
1464 printk(IBM_INFO
1465 "radio switch found; radios are %s\n",
1466 enabled(status, 0));
1467 res = add_to_attr_set(hotkey_dev_attributes,
1468 &dev_attr_hotkey_radio_sw.attr);
1471 if (!res)
1472 res = register_attr_set_with_sysfs(
1473 hotkey_dev_attributes,
1474 &tpacpi_pdev->dev.kobj);
1475 if (res)
1476 return res;
1478 /* Set up key map */
1480 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1481 GFP_KERNEL);
1482 if (!hotkey_keycode_map) {
1483 printk(IBM_ERR "failed to allocate memory for key map\n");
1484 return -ENOMEM;
1487 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1488 dbg_printk(TPACPI_DBG_INIT,
1489 "using Lenovo default hot key map\n");
1490 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1491 TPACPI_HOTKEY_MAP_SIZE);
1492 } else {
1493 dbg_printk(TPACPI_DBG_INIT,
1494 "using IBM default hot key map\n");
1495 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1496 TPACPI_HOTKEY_MAP_SIZE);
1499 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1500 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1501 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1502 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1503 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1504 tpacpi_inputdev->keycode = hotkey_keycode_map;
1505 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1506 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1507 set_bit(hotkey_keycode_map[i],
1508 tpacpi_inputdev->keybit);
1509 } else {
1510 if (i < sizeof(hotkey_reserved_mask)*8)
1511 hotkey_reserved_mask |= 1 << i;
1514 tpacpi_inputdev->open = &hotkey_inputdev_open;
1515 tpacpi_inputdev->close = &hotkey_inputdev_close;
1517 if (tp_features.hotkey_wlsw) {
1518 set_bit(EV_SW, tpacpi_inputdev->evbit);
1519 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1522 dbg_printk(TPACPI_DBG_INIT,
1523 "enabling hot key handling\n");
1524 res = hotkey_set(1, ((hotkey_all_mask | hotkey_source_mask)
1525 & ~hotkey_reserved_mask)
1526 | hotkey_orig_mask);
1527 if (res)
1528 return res;
1530 if (hotkey_report_mode > 0) {
1531 tp_features.hotkey_report_mode_locked = 1;
1532 } else {
1533 hotkey_report_mode = 1;
1534 vdbg_printk(TPACPI_DBG_INIT,
1535 "hot key reporting mode can be "
1536 "changed at runtime\n");
1539 dbg_printk(TPACPI_DBG_INIT,
1540 "legacy hot key reporting over procfs %s\n",
1541 (hotkey_report_mode < 2) ?
1542 "enabled" : "disabled");
1544 mutex_lock(&hotkey_poll_mutex);
1545 hotkey_poll_setup(0);
1546 hotkey_warn_no_poll();
1547 mutex_unlock(&hotkey_poll_mutex);
1550 return (tp_features.hotkey)? 0 : 1;
1553 static void hotkey_exit(void)
1555 int res;
1557 hotkey_poll_stop_sync();
1559 if (tp_features.hotkey) {
1560 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
1561 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1562 if (res)
1563 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
1566 if (hotkey_dev_attributes) {
1567 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1568 hotkey_dev_attributes = NULL;
1572 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1574 u32 hkey;
1575 unsigned int scancode;
1576 int send_acpi_ev;
1577 int ignore_acpi_ev;
1579 if (event != 0x80) {
1580 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
1581 /* forward it to userspace, maybe it knows how to handle it */
1582 acpi_bus_generate_event(ibm->acpi->device, event, 0);
1584 return;
1587 while (1) {
1588 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1589 printk(IBM_ERR "failed to retrieve hot key event\n");
1590 return;
1593 if (hkey == 0) {
1594 /* queue empty */
1595 return;
1598 send_acpi_ev = 0;
1599 ignore_acpi_ev = 0;
1601 switch (hkey >> 12) {
1602 case 1:
1603 /* 0x1000-0x1FFF: key presses */
1604 scancode = hkey & 0xfff;
1605 if (scancode > 0 && scancode < 0x21) {
1606 scancode--;
1607 if (!(hotkey_source_mask & (1 << scancode))) {
1608 tpacpi_input_send_key(scancode);
1610 } else {
1611 printk(IBM_ERR
1612 "hotkey 0x%04x out of range for keyboard map\n",
1613 hkey);
1614 send_acpi_ev = 1;
1616 break;
1617 case 5:
1618 /* 0x5000-0x5FFF: LID */
1619 /* we don't handle it through this path, just
1620 * eat up known LID events */
1621 if (hkey != 0x5001 && hkey != 0x5002) {
1622 printk(IBM_ERR
1623 "unknown LID-related hotkey event: 0x%04x\n",
1624 hkey);
1625 send_acpi_ev = 1;
1626 } else {
1627 ignore_acpi_ev = 1;
1629 break;
1630 case 7:
1631 /* 0x7000-0x7FFF: misc */
1632 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1633 tpacpi_input_send_radiosw();
1634 break;
1636 /* fallthrough to default */
1637 default:
1638 /* case 2: dock-related */
1639 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1640 /* case 3: ultra-bay related. maybe bay in dock? */
1641 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1642 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1643 send_acpi_ev = 1;
1646 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
1647 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
1652 static void hotkey_resume(void)
1654 tpacpi_input_send_radiosw();
1655 hotkey_poll_setup_safe(1);
1659 * Call with hotkey_mutex held
1661 static int hotkey_get(int *status, u32 *mask)
1663 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1664 return -EIO;
1666 if (tp_features.hotkey_mask)
1667 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
1668 return -EIO;
1670 return 0;
1674 * Call with hotkey_mutex held
1676 static int hotkey_set(int status, u32 mask)
1678 int i;
1680 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1681 return -EIO;
1683 if (tp_features.hotkey_mask)
1684 for (i = 0; i < 32; i++) {
1685 int bit = ((1 << i) & mask) != 0;
1686 if (!acpi_evalf(hkey_handle,
1687 NULL, "MHKM", "vdd", i + 1, bit))
1688 return -EIO;
1691 return 0;
1694 /* procfs -------------------------------------------------------------- */
1695 static int hotkey_read(char *p)
1697 int res, status;
1698 u32 mask;
1699 int len = 0;
1701 if (!tp_features.hotkey) {
1702 len += sprintf(p + len, "status:\t\tnot supported\n");
1703 return len;
1706 res = mutex_lock_interruptible(&hotkey_mutex);
1707 if (res < 0)
1708 return res;
1709 res = hotkey_get(&status, &mask);
1710 mutex_unlock(&hotkey_mutex);
1711 if (res)
1712 return res;
1714 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1715 if (tp_features.hotkey_mask) {
1716 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1717 len += sprintf(p + len,
1718 "commands:\tenable, disable, reset, <mask>\n");
1719 } else {
1720 len += sprintf(p + len, "mask:\t\tnot supported\n");
1721 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1724 return len;
1727 static int hotkey_write(char *buf)
1729 int res, status;
1730 u32 mask;
1731 char *cmd;
1732 int do_cmd = 0;
1734 if (!tp_features.hotkey)
1735 return -ENODEV;
1737 res = mutex_lock_interruptible(&hotkey_mutex);
1738 if (res < 0)
1739 return res;
1741 res = hotkey_get(&status, &mask);
1742 if (res)
1743 goto errexit;
1745 res = 0;
1746 while ((cmd = next_cmd(&buf))) {
1747 if (strlencmp(cmd, "enable") == 0) {
1748 status = 1;
1749 } else if (strlencmp(cmd, "disable") == 0) {
1750 status = 0;
1751 } else if (strlencmp(cmd, "reset") == 0) {
1752 status = hotkey_orig_status;
1753 mask = hotkey_orig_mask;
1754 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1755 /* mask set */
1756 } else if (sscanf(cmd, "%x", &mask) == 1) {
1757 /* mask set */
1758 } else {
1759 res = -EINVAL;
1760 goto errexit;
1762 do_cmd = 1;
1765 if (do_cmd)
1766 res = hotkey_set(status, mask);
1768 errexit:
1769 mutex_unlock(&hotkey_mutex);
1770 return res;
1773 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1774 .hid = IBM_HKEY_HID,
1775 .notify = hotkey_notify,
1776 .handle = &hkey_handle,
1777 .type = ACPI_DEVICE_NOTIFY,
1780 static struct ibm_struct hotkey_driver_data = {
1781 .name = "hotkey",
1782 .read = hotkey_read,
1783 .write = hotkey_write,
1784 .exit = hotkey_exit,
1785 .resume = hotkey_resume,
1786 .acpi = &ibm_hotkey_acpidriver,
1789 /*************************************************************************
1790 * Bluetooth subdriver
1793 /* sysfs bluetooth enable ---------------------------------------------- */
1794 static ssize_t bluetooth_enable_show(struct device *dev,
1795 struct device_attribute *attr,
1796 char *buf)
1798 int status;
1800 status = bluetooth_get_radiosw();
1801 if (status < 0)
1802 return status;
1804 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1807 static ssize_t bluetooth_enable_store(struct device *dev,
1808 struct device_attribute *attr,
1809 const char *buf, size_t count)
1811 unsigned long t;
1812 int res;
1814 if (parse_strtoul(buf, 1, &t))
1815 return -EINVAL;
1817 res = bluetooth_set_radiosw(t);
1819 return (res) ? res : count;
1822 static struct device_attribute dev_attr_bluetooth_enable =
1823 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1824 bluetooth_enable_show, bluetooth_enable_store);
1826 /* --------------------------------------------------------------------- */
1828 static struct attribute *bluetooth_attributes[] = {
1829 &dev_attr_bluetooth_enable.attr,
1830 NULL
1833 static const struct attribute_group bluetooth_attr_group = {
1834 .attrs = bluetooth_attributes,
1837 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1839 int res;
1840 int status = 0;
1842 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1844 IBM_ACPIHANDLE_INIT(hkey);
1846 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1847 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1848 tp_features.bluetooth = hkey_handle &&
1849 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1851 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1852 str_supported(tp_features.bluetooth),
1853 status);
1855 if (tp_features.bluetooth) {
1856 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1857 /* no bluetooth hardware present in system */
1858 tp_features.bluetooth = 0;
1859 dbg_printk(TPACPI_DBG_INIT,
1860 "bluetooth hardware not installed\n");
1861 } else {
1862 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1863 &bluetooth_attr_group);
1864 if (res)
1865 return res;
1869 return (tp_features.bluetooth)? 0 : 1;
1872 static void bluetooth_exit(void)
1874 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1875 &bluetooth_attr_group);
1878 static int bluetooth_get_radiosw(void)
1880 int status;
1882 if (!tp_features.bluetooth)
1883 return -ENODEV;
1885 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1886 return -EIO;
1888 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1891 static int bluetooth_set_radiosw(int radio_on)
1893 int status;
1895 if (!tp_features.bluetooth)
1896 return -ENODEV;
1898 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1899 return -EIO;
1900 if (radio_on)
1901 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1902 else
1903 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1904 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1905 return -EIO;
1907 return 0;
1910 /* procfs -------------------------------------------------------------- */
1911 static int bluetooth_read(char *p)
1913 int len = 0;
1914 int status = bluetooth_get_radiosw();
1916 if (!tp_features.bluetooth)
1917 len += sprintf(p + len, "status:\t\tnot supported\n");
1918 else {
1919 len += sprintf(p + len, "status:\t\t%s\n",
1920 (status)? "enabled" : "disabled");
1921 len += sprintf(p + len, "commands:\tenable, disable\n");
1924 return len;
1927 static int bluetooth_write(char *buf)
1929 char *cmd;
1931 if (!tp_features.bluetooth)
1932 return -ENODEV;
1934 while ((cmd = next_cmd(&buf))) {
1935 if (strlencmp(cmd, "enable") == 0) {
1936 bluetooth_set_radiosw(1);
1937 } else if (strlencmp(cmd, "disable") == 0) {
1938 bluetooth_set_radiosw(0);
1939 } else
1940 return -EINVAL;
1943 return 0;
1946 static struct ibm_struct bluetooth_driver_data = {
1947 .name = "bluetooth",
1948 .read = bluetooth_read,
1949 .write = bluetooth_write,
1950 .exit = bluetooth_exit,
1953 /*************************************************************************
1954 * Wan subdriver
1957 /* sysfs wan enable ---------------------------------------------------- */
1958 static ssize_t wan_enable_show(struct device *dev,
1959 struct device_attribute *attr,
1960 char *buf)
1962 int status;
1964 status = wan_get_radiosw();
1965 if (status < 0)
1966 return status;
1968 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1971 static ssize_t wan_enable_store(struct device *dev,
1972 struct device_attribute *attr,
1973 const char *buf, size_t count)
1975 unsigned long t;
1976 int res;
1978 if (parse_strtoul(buf, 1, &t))
1979 return -EINVAL;
1981 res = wan_set_radiosw(t);
1983 return (res) ? res : count;
1986 static struct device_attribute dev_attr_wan_enable =
1987 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1988 wan_enable_show, wan_enable_store);
1990 /* --------------------------------------------------------------------- */
1992 static struct attribute *wan_attributes[] = {
1993 &dev_attr_wan_enable.attr,
1994 NULL
1997 static const struct attribute_group wan_attr_group = {
1998 .attrs = wan_attributes,
2001 static int __init wan_init(struct ibm_init_struct *iibm)
2003 int res;
2004 int status = 0;
2006 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2008 IBM_ACPIHANDLE_INIT(hkey);
2010 tp_features.wan = hkey_handle &&
2011 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2013 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2014 str_supported(tp_features.wan),
2015 status);
2017 if (tp_features.wan) {
2018 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2019 /* no wan hardware present in system */
2020 tp_features.wan = 0;
2021 dbg_printk(TPACPI_DBG_INIT,
2022 "wan hardware not installed\n");
2023 } else {
2024 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2025 &wan_attr_group);
2026 if (res)
2027 return res;
2031 return (tp_features.wan)? 0 : 1;
2034 static void wan_exit(void)
2036 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2037 &wan_attr_group);
2040 static int wan_get_radiosw(void)
2042 int status;
2044 if (!tp_features.wan)
2045 return -ENODEV;
2047 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2048 return -EIO;
2050 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2053 static int wan_set_radiosw(int radio_on)
2055 int status;
2057 if (!tp_features.wan)
2058 return -ENODEV;
2060 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2061 return -EIO;
2062 if (radio_on)
2063 status |= TP_ACPI_WANCARD_RADIOSSW;
2064 else
2065 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2066 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2067 return -EIO;
2069 return 0;
2072 /* procfs -------------------------------------------------------------- */
2073 static int wan_read(char *p)
2075 int len = 0;
2076 int status = wan_get_radiosw();
2078 if (!tp_features.wan)
2079 len += sprintf(p + len, "status:\t\tnot supported\n");
2080 else {
2081 len += sprintf(p + len, "status:\t\t%s\n",
2082 (status)? "enabled" : "disabled");
2083 len += sprintf(p + len, "commands:\tenable, disable\n");
2086 return len;
2089 static int wan_write(char *buf)
2091 char *cmd;
2093 if (!tp_features.wan)
2094 return -ENODEV;
2096 while ((cmd = next_cmd(&buf))) {
2097 if (strlencmp(cmd, "enable") == 0) {
2098 wan_set_radiosw(1);
2099 } else if (strlencmp(cmd, "disable") == 0) {
2100 wan_set_radiosw(0);
2101 } else
2102 return -EINVAL;
2105 return 0;
2108 static struct ibm_struct wan_driver_data = {
2109 .name = "wan",
2110 .read = wan_read,
2111 .write = wan_write,
2112 .exit = wan_exit,
2113 .flags.experimental = 1,
2116 /*************************************************************************
2117 * Video subdriver
2120 static enum video_access_mode video_supported;
2121 static int video_orig_autosw;
2123 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2124 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2125 "\\_SB.PCI0.VID0", /* 770e */
2126 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2127 "\\_SB.PCI0.AGP.VID", /* all others */
2128 ); /* R30, R31 */
2130 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2132 static int __init video_init(struct ibm_init_struct *iibm)
2134 int ivga;
2136 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2138 IBM_ACPIHANDLE_INIT(vid);
2139 IBM_ACPIHANDLE_INIT(vid2);
2141 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2142 /* G41, assume IVGA doesn't change */
2143 vid_handle = vid2_handle;
2145 if (!vid_handle)
2146 /* video switching not supported on R30, R31 */
2147 video_supported = TPACPI_VIDEO_NONE;
2148 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2149 /* 570 */
2150 video_supported = TPACPI_VIDEO_570;
2151 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2152 /* 600e/x, 770e, 770x */
2153 video_supported = TPACPI_VIDEO_770;
2154 else
2155 /* all others */
2156 video_supported = TPACPI_VIDEO_NEW;
2158 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2159 str_supported(video_supported != TPACPI_VIDEO_NONE),
2160 video_supported);
2162 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2165 static void video_exit(void)
2167 dbg_printk(TPACPI_DBG_EXIT,
2168 "restoring original video autoswitch mode\n");
2169 if (video_autosw_set(video_orig_autosw))
2170 printk(IBM_ERR "error while trying to restore original "
2171 "video autoswitch mode\n");
2174 static int video_outputsw_get(void)
2176 int status = 0;
2177 int i;
2179 switch (video_supported) {
2180 case TPACPI_VIDEO_570:
2181 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2182 TP_ACPI_VIDEO_570_PHSCMD))
2183 return -EIO;
2184 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2185 break;
2186 case TPACPI_VIDEO_770:
2187 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2188 return -EIO;
2189 if (i)
2190 status |= TP_ACPI_VIDEO_S_LCD;
2191 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2192 return -EIO;
2193 if (i)
2194 status |= TP_ACPI_VIDEO_S_CRT;
2195 break;
2196 case TPACPI_VIDEO_NEW:
2197 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2198 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2199 return -EIO;
2200 if (i)
2201 status |= TP_ACPI_VIDEO_S_CRT;
2203 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2204 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2205 return -EIO;
2206 if (i)
2207 status |= TP_ACPI_VIDEO_S_LCD;
2208 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2209 return -EIO;
2210 if (i)
2211 status |= TP_ACPI_VIDEO_S_DVI;
2212 break;
2213 default:
2214 return -ENOSYS;
2217 return status;
2220 static int video_outputsw_set(int status)
2222 int autosw;
2223 int res = 0;
2225 switch (video_supported) {
2226 case TPACPI_VIDEO_570:
2227 res = acpi_evalf(NULL, NULL,
2228 "\\_SB.PHS2", "vdd",
2229 TP_ACPI_VIDEO_570_PHS2CMD,
2230 status | TP_ACPI_VIDEO_570_PHS2SET);
2231 break;
2232 case TPACPI_VIDEO_770:
2233 autosw = video_autosw_get();
2234 if (autosw < 0)
2235 return autosw;
2237 res = video_autosw_set(1);
2238 if (res)
2239 return res;
2240 res = acpi_evalf(vid_handle, NULL,
2241 "ASWT", "vdd", status * 0x100, 0);
2242 if (!autosw && video_autosw_set(autosw)) {
2243 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2244 return -EIO;
2246 break;
2247 case TPACPI_VIDEO_NEW:
2248 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2249 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2250 break;
2251 default:
2252 return -ENOSYS;
2255 return (res)? 0 : -EIO;
2258 static int video_autosw_get(void)
2260 int autosw = 0;
2262 switch (video_supported) {
2263 case TPACPI_VIDEO_570:
2264 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2265 return -EIO;
2266 break;
2267 case TPACPI_VIDEO_770:
2268 case TPACPI_VIDEO_NEW:
2269 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2270 return -EIO;
2271 break;
2272 default:
2273 return -ENOSYS;
2276 return autosw & 1;
2279 static int video_autosw_set(int enable)
2281 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2282 return -EIO;
2283 return 0;
2286 static int video_outputsw_cycle(void)
2288 int autosw = video_autosw_get();
2289 int res;
2291 if (autosw < 0)
2292 return autosw;
2294 switch (video_supported) {
2295 case TPACPI_VIDEO_570:
2296 res = video_autosw_set(1);
2297 if (res)
2298 return res;
2299 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2300 break;
2301 case TPACPI_VIDEO_770:
2302 case TPACPI_VIDEO_NEW:
2303 res = video_autosw_set(1);
2304 if (res)
2305 return res;
2306 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2307 break;
2308 default:
2309 return -ENOSYS;
2311 if (!autosw && video_autosw_set(autosw)) {
2312 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2313 return -EIO;
2316 return (res)? 0 : -EIO;
2319 static int video_expand_toggle(void)
2321 switch (video_supported) {
2322 case TPACPI_VIDEO_570:
2323 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2324 0 : -EIO;
2325 case TPACPI_VIDEO_770:
2326 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2327 0 : -EIO;
2328 case TPACPI_VIDEO_NEW:
2329 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2330 0 : -EIO;
2331 default:
2332 return -ENOSYS;
2334 /* not reached */
2337 static int video_read(char *p)
2339 int status, autosw;
2340 int len = 0;
2342 if (video_supported == TPACPI_VIDEO_NONE) {
2343 len += sprintf(p + len, "status:\t\tnot supported\n");
2344 return len;
2347 status = video_outputsw_get();
2348 if (status < 0)
2349 return status;
2351 autosw = video_autosw_get();
2352 if (autosw < 0)
2353 return autosw;
2355 len += sprintf(p + len, "status:\t\tsupported\n");
2356 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2357 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2358 if (video_supported == TPACPI_VIDEO_NEW)
2359 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2360 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2361 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2362 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2363 if (video_supported == TPACPI_VIDEO_NEW)
2364 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2365 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2366 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2368 return len;
2371 static int video_write(char *buf)
2373 char *cmd;
2374 int enable, disable, status;
2375 int res;
2377 if (video_supported == TPACPI_VIDEO_NONE)
2378 return -ENODEV;
2380 enable = 0;
2381 disable = 0;
2383 while ((cmd = next_cmd(&buf))) {
2384 if (strlencmp(cmd, "lcd_enable") == 0) {
2385 enable |= TP_ACPI_VIDEO_S_LCD;
2386 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2387 disable |= TP_ACPI_VIDEO_S_LCD;
2388 } else if (strlencmp(cmd, "crt_enable") == 0) {
2389 enable |= TP_ACPI_VIDEO_S_CRT;
2390 } else if (strlencmp(cmd, "crt_disable") == 0) {
2391 disable |= TP_ACPI_VIDEO_S_CRT;
2392 } else if (video_supported == TPACPI_VIDEO_NEW &&
2393 strlencmp(cmd, "dvi_enable") == 0) {
2394 enable |= TP_ACPI_VIDEO_S_DVI;
2395 } else if (video_supported == TPACPI_VIDEO_NEW &&
2396 strlencmp(cmd, "dvi_disable") == 0) {
2397 disable |= TP_ACPI_VIDEO_S_DVI;
2398 } else if (strlencmp(cmd, "auto_enable") == 0) {
2399 res = video_autosw_set(1);
2400 if (res)
2401 return res;
2402 } else if (strlencmp(cmd, "auto_disable") == 0) {
2403 res = video_autosw_set(0);
2404 if (res)
2405 return res;
2406 } else if (strlencmp(cmd, "video_switch") == 0) {
2407 res = video_outputsw_cycle();
2408 if (res)
2409 return res;
2410 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2411 res = video_expand_toggle();
2412 if (res)
2413 return res;
2414 } else
2415 return -EINVAL;
2418 if (enable || disable) {
2419 status = video_outputsw_get();
2420 if (status < 0)
2421 return status;
2422 res = video_outputsw_set((status & ~disable) | enable);
2423 if (res)
2424 return res;
2427 return 0;
2430 static struct ibm_struct video_driver_data = {
2431 .name = "video",
2432 .read = video_read,
2433 .write = video_write,
2434 .exit = video_exit,
2437 /*************************************************************************
2438 * Light (thinklight) subdriver
2441 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2442 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2444 static int __init light_init(struct ibm_init_struct *iibm)
2446 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2448 IBM_ACPIHANDLE_INIT(ledb);
2449 IBM_ACPIHANDLE_INIT(lght);
2450 IBM_ACPIHANDLE_INIT(cmos);
2452 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2453 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2455 if (tp_features.light)
2456 /* light status not supported on
2457 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2458 tp_features.light_status =
2459 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2461 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2462 str_supported(tp_features.light));
2464 return (tp_features.light)? 0 : 1;
2467 static int light_read(char *p)
2469 int len = 0;
2470 int status = 0;
2472 if (!tp_features.light) {
2473 len += sprintf(p + len, "status:\t\tnot supported\n");
2474 } else if (!tp_features.light_status) {
2475 len += sprintf(p + len, "status:\t\tunknown\n");
2476 len += sprintf(p + len, "commands:\ton, off\n");
2477 } else {
2478 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2479 return -EIO;
2480 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2481 len += sprintf(p + len, "commands:\ton, off\n");
2484 return len;
2487 static int light_write(char *buf)
2489 int cmos_cmd, lght_cmd;
2490 char *cmd;
2491 int success;
2493 if (!tp_features.light)
2494 return -ENODEV;
2496 while ((cmd = next_cmd(&buf))) {
2497 if (strlencmp(cmd, "on") == 0) {
2498 cmos_cmd = 0x0c;
2499 lght_cmd = 1;
2500 } else if (strlencmp(cmd, "off") == 0) {
2501 cmos_cmd = 0x0d;
2502 lght_cmd = 0;
2503 } else
2504 return -EINVAL;
2506 success = cmos_handle ?
2507 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2508 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2509 if (!success)
2510 return -EIO;
2513 return 0;
2516 static struct ibm_struct light_driver_data = {
2517 .name = "light",
2518 .read = light_read,
2519 .write = light_write,
2522 /*************************************************************************
2523 * Dock subdriver
2526 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2528 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2529 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2530 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2531 "\\_SB.PCI.ISA.SLCE", /* 570 */
2532 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2534 /* don't list other alternatives as we install a notify handler on the 570 */
2535 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2537 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2539 .notify = dock_notify,
2540 .handle = &dock_handle,
2541 .type = ACPI_SYSTEM_NOTIFY,
2544 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2545 * We just use it to get notifications of dock hotplug
2546 * in very old thinkpads */
2547 .hid = PCI_ROOT_HID_STRING,
2548 .notify = dock_notify,
2549 .handle = &pci_handle,
2550 .type = ACPI_SYSTEM_NOTIFY,
2554 static struct ibm_struct dock_driver_data[2] = {
2556 .name = "dock",
2557 .read = dock_read,
2558 .write = dock_write,
2559 .acpi = &ibm_dock_acpidriver[0],
2562 .name = "dock",
2563 .acpi = &ibm_dock_acpidriver[1],
2567 #define dock_docked() (_sta(dock_handle) & 1)
2569 static int __init dock_init(struct ibm_init_struct *iibm)
2571 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2573 IBM_ACPIHANDLE_INIT(dock);
2575 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2576 str_supported(dock_handle != NULL));
2578 return (dock_handle)? 0 : 1;
2581 static int __init dock_init2(struct ibm_init_struct *iibm)
2583 int dock2_needed;
2585 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2587 if (dock_driver_data[0].flags.acpi_driver_registered &&
2588 dock_driver_data[0].flags.acpi_notify_installed) {
2589 IBM_ACPIHANDLE_INIT(pci);
2590 dock2_needed = (pci_handle != NULL);
2591 vdbg_printk(TPACPI_DBG_INIT,
2592 "dock PCI handler for the TP 570 is %s\n",
2593 str_supported(dock2_needed));
2594 } else {
2595 vdbg_printk(TPACPI_DBG_INIT,
2596 "dock subdriver part 2 not required\n");
2597 dock2_needed = 0;
2600 return (dock2_needed)? 0 : 1;
2603 static void dock_notify(struct ibm_struct *ibm, u32 event)
2605 int docked = dock_docked();
2606 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, PCI_ROOT_HID_STRING);
2608 if (event == 1 && !pci) /* 570 */
2609 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2610 else if (event == 1 && pci) /* 570 */
2611 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2612 else if (event == 3 && docked)
2613 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2614 else if (event == 3 && !docked)
2615 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
2616 else if (event == 0 && docked)
2617 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2618 else {
2619 printk(IBM_ERR "unknown dock event %d, status %d\n",
2620 event, _sta(dock_handle));
2621 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
2625 static int dock_read(char *p)
2627 int len = 0;
2628 int docked = dock_docked();
2630 if (!dock_handle)
2631 len += sprintf(p + len, "status:\t\tnot supported\n");
2632 else if (!docked)
2633 len += sprintf(p + len, "status:\t\tundocked\n");
2634 else {
2635 len += sprintf(p + len, "status:\t\tdocked\n");
2636 len += sprintf(p + len, "commands:\tdock, undock\n");
2639 return len;
2642 static int dock_write(char *buf)
2644 char *cmd;
2646 if (!dock_docked())
2647 return -ENODEV;
2649 while ((cmd = next_cmd(&buf))) {
2650 if (strlencmp(cmd, "undock") == 0) {
2651 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2652 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2653 return -EIO;
2654 } else if (strlencmp(cmd, "dock") == 0) {
2655 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2656 return -EIO;
2657 } else
2658 return -EINVAL;
2661 return 0;
2664 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2666 /*************************************************************************
2667 * Bay subdriver
2670 #ifdef CONFIG_THINKPAD_ACPI_BAY
2671 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2672 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2673 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2674 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2675 ); /* A21e, R30, R31 */
2676 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2677 "_EJ0", /* all others */
2678 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2679 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2680 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2681 ); /* all others */
2682 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2683 "_EJ0", /* 770x */
2684 ); /* all others */
2686 static int __init bay_init(struct ibm_init_struct *iibm)
2688 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2690 IBM_ACPIHANDLE_INIT(bay);
2691 if (bay_handle)
2692 IBM_ACPIHANDLE_INIT(bay_ej);
2693 IBM_ACPIHANDLE_INIT(bay2);
2694 if (bay2_handle)
2695 IBM_ACPIHANDLE_INIT(bay2_ej);
2697 tp_features.bay_status = bay_handle &&
2698 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2699 tp_features.bay_status2 = bay2_handle &&
2700 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2702 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2703 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2704 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2705 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2707 vdbg_printk(TPACPI_DBG_INIT,
2708 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2709 str_supported(tp_features.bay_status),
2710 str_supported(tp_features.bay_eject),
2711 str_supported(tp_features.bay_status2),
2712 str_supported(tp_features.bay_eject2));
2714 return (tp_features.bay_status || tp_features.bay_eject ||
2715 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2718 static void bay_notify(struct ibm_struct *ibm, u32 event)
2720 acpi_bus_generate_event(ibm->acpi->device, event, 0);
2723 #define bay_occupied(b) (_sta(b##_handle) & 1)
2725 static int bay_read(char *p)
2727 int len = 0;
2728 int occupied = bay_occupied(bay);
2729 int occupied2 = bay_occupied(bay2);
2730 int eject, eject2;
2732 len += sprintf(p + len, "status:\t\t%s\n",
2733 tp_features.bay_status ?
2734 (occupied ? "occupied" : "unoccupied") :
2735 "not supported");
2736 if (tp_features.bay_status2)
2737 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2738 "occupied" : "unoccupied");
2740 eject = tp_features.bay_eject && occupied;
2741 eject2 = tp_features.bay_eject2 && occupied2;
2743 if (eject && eject2)
2744 len += sprintf(p + len, "commands:\teject, eject2\n");
2745 else if (eject)
2746 len += sprintf(p + len, "commands:\teject\n");
2747 else if (eject2)
2748 len += sprintf(p + len, "commands:\teject2\n");
2750 return len;
2753 static int bay_write(char *buf)
2755 char *cmd;
2757 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2758 return -ENODEV;
2760 while ((cmd = next_cmd(&buf))) {
2761 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2762 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2763 return -EIO;
2764 } else if (tp_features.bay_eject2 &&
2765 strlencmp(cmd, "eject2") == 0) {
2766 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2767 return -EIO;
2768 } else
2769 return -EINVAL;
2772 return 0;
2775 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2776 .notify = bay_notify,
2777 .handle = &bay_handle,
2778 .type = ACPI_SYSTEM_NOTIFY,
2781 static struct ibm_struct bay_driver_data = {
2782 .name = "bay",
2783 .read = bay_read,
2784 .write = bay_write,
2785 .acpi = &ibm_bay_acpidriver,
2788 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2790 /*************************************************************************
2791 * CMOS subdriver
2794 /* sysfs cmos_command -------------------------------------------------- */
2795 static ssize_t cmos_command_store(struct device *dev,
2796 struct device_attribute *attr,
2797 const char *buf, size_t count)
2799 unsigned long cmos_cmd;
2800 int res;
2802 if (parse_strtoul(buf, 21, &cmos_cmd))
2803 return -EINVAL;
2805 res = issue_thinkpad_cmos_command(cmos_cmd);
2806 return (res)? res : count;
2809 static struct device_attribute dev_attr_cmos_command =
2810 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2812 /* --------------------------------------------------------------------- */
2814 static int __init cmos_init(struct ibm_init_struct *iibm)
2816 int res;
2818 vdbg_printk(TPACPI_DBG_INIT,
2819 "initializing cmos commands subdriver\n");
2821 IBM_ACPIHANDLE_INIT(cmos);
2823 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2824 str_supported(cmos_handle != NULL));
2826 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2827 if (res)
2828 return res;
2830 return (cmos_handle)? 0 : 1;
2833 static void cmos_exit(void)
2835 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2838 static int cmos_read(char *p)
2840 int len = 0;
2842 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2843 R30, R31, T20-22, X20-21 */
2844 if (!cmos_handle)
2845 len += sprintf(p + len, "status:\t\tnot supported\n");
2846 else {
2847 len += sprintf(p + len, "status:\t\tsupported\n");
2848 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2851 return len;
2854 static int cmos_write(char *buf)
2856 char *cmd;
2857 int cmos_cmd, res;
2859 while ((cmd = next_cmd(&buf))) {
2860 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2861 cmos_cmd >= 0 && cmos_cmd <= 21) {
2862 /* cmos_cmd set */
2863 } else
2864 return -EINVAL;
2866 res = issue_thinkpad_cmos_command(cmos_cmd);
2867 if (res)
2868 return res;
2871 return 0;
2874 static struct ibm_struct cmos_driver_data = {
2875 .name = "cmos",
2876 .read = cmos_read,
2877 .write = cmos_write,
2878 .exit = cmos_exit,
2881 /*************************************************************************
2882 * LED subdriver
2885 static enum led_access_mode led_supported;
2887 IBM_HANDLE(led, ec, "SLED", /* 570 */
2888 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2889 "LED", /* all others */
2890 ); /* R30, R31 */
2892 static int __init led_init(struct ibm_init_struct *iibm)
2894 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2896 IBM_ACPIHANDLE_INIT(led);
2898 if (!led_handle)
2899 /* led not supported on R30, R31 */
2900 led_supported = TPACPI_LED_NONE;
2901 else if (strlencmp(led_path, "SLED") == 0)
2902 /* 570 */
2903 led_supported = TPACPI_LED_570;
2904 else if (strlencmp(led_path, "SYSL") == 0)
2905 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2906 led_supported = TPACPI_LED_OLD;
2907 else
2908 /* all others */
2909 led_supported = TPACPI_LED_NEW;
2911 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2912 str_supported(led_supported), led_supported);
2914 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2917 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2919 static int led_read(char *p)
2921 int len = 0;
2923 if (!led_supported) {
2924 len += sprintf(p + len, "status:\t\tnot supported\n");
2925 return len;
2927 len += sprintf(p + len, "status:\t\tsupported\n");
2929 if (led_supported == TPACPI_LED_570) {
2930 /* 570 */
2931 int i, status;
2932 for (i = 0; i < 8; i++) {
2933 if (!acpi_evalf(ec_handle,
2934 &status, "GLED", "dd", 1 << i))
2935 return -EIO;
2936 len += sprintf(p + len, "%d:\t\t%s\n",
2937 i, led_status(status));
2941 len += sprintf(p + len, "commands:\t"
2942 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2944 return len;
2947 /* off, on, blink */
2948 static const int led_sled_arg1[] = { 0, 1, 3 };
2949 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2950 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2951 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2953 static int led_write(char *buf)
2955 char *cmd;
2956 int led, ind, ret;
2958 if (!led_supported)
2959 return -ENODEV;
2961 while ((cmd = next_cmd(&buf))) {
2962 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2963 return -EINVAL;
2965 if (strstr(cmd, "off")) {
2966 ind = 0;
2967 } else if (strstr(cmd, "on")) {
2968 ind = 1;
2969 } else if (strstr(cmd, "blink")) {
2970 ind = 2;
2971 } else
2972 return -EINVAL;
2974 if (led_supported == TPACPI_LED_570) {
2975 /* 570 */
2976 led = 1 << led;
2977 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2978 led, led_sled_arg1[ind]))
2979 return -EIO;
2980 } else if (led_supported == TPACPI_LED_OLD) {
2981 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2982 led = 1 << led;
2983 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2984 if (ret >= 0)
2985 ret =
2986 ec_write(TPACPI_LED_EC_HLBL,
2987 led * led_exp_hlbl[ind]);
2988 if (ret >= 0)
2989 ret =
2990 ec_write(TPACPI_LED_EC_HLCL,
2991 led * led_exp_hlcl[ind]);
2992 if (ret < 0)
2993 return ret;
2994 } else {
2995 /* all others */
2996 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2997 led, led_led_arg1[ind]))
2998 return -EIO;
3002 return 0;
3005 static struct ibm_struct led_driver_data = {
3006 .name = "led",
3007 .read = led_read,
3008 .write = led_write,
3011 /*************************************************************************
3012 * Beep subdriver
3015 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3017 static int __init beep_init(struct ibm_init_struct *iibm)
3019 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3021 IBM_ACPIHANDLE_INIT(beep);
3023 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3024 str_supported(beep_handle != NULL));
3026 return (beep_handle)? 0 : 1;
3029 static int beep_read(char *p)
3031 int len = 0;
3033 if (!beep_handle)
3034 len += sprintf(p + len, "status:\t\tnot supported\n");
3035 else {
3036 len += sprintf(p + len, "status:\t\tsupported\n");
3037 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3040 return len;
3043 static int beep_write(char *buf)
3045 char *cmd;
3046 int beep_cmd;
3048 if (!beep_handle)
3049 return -ENODEV;
3051 while ((cmd = next_cmd(&buf))) {
3052 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3053 beep_cmd >= 0 && beep_cmd <= 17) {
3054 /* beep_cmd set */
3055 } else
3056 return -EINVAL;
3057 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3058 return -EIO;
3061 return 0;
3064 static struct ibm_struct beep_driver_data = {
3065 .name = "beep",
3066 .read = beep_read,
3067 .write = beep_write,
3070 /*************************************************************************
3071 * Thermal subdriver
3074 static enum thermal_access_mode thermal_read_mode;
3076 /* sysfs temp##_input -------------------------------------------------- */
3078 static ssize_t thermal_temp_input_show(struct device *dev,
3079 struct device_attribute *attr,
3080 char *buf)
3082 struct sensor_device_attribute *sensor_attr =
3083 to_sensor_dev_attr(attr);
3084 int idx = sensor_attr->index;
3085 s32 value;
3086 int res;
3088 res = thermal_get_sensor(idx, &value);
3089 if (res)
3090 return res;
3091 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3092 return -ENXIO;
3094 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3097 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3098 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
3100 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3101 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3102 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3103 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3104 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3105 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3106 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3107 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3108 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3109 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3110 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3111 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3112 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3113 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3114 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3115 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3116 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3119 #define THERMAL_ATTRS(X) \
3120 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3122 static struct attribute *thermal_temp_input_attr[] = {
3123 THERMAL_ATTRS(8),
3124 THERMAL_ATTRS(9),
3125 THERMAL_ATTRS(10),
3126 THERMAL_ATTRS(11),
3127 THERMAL_ATTRS(12),
3128 THERMAL_ATTRS(13),
3129 THERMAL_ATTRS(14),
3130 THERMAL_ATTRS(15),
3131 THERMAL_ATTRS(0),
3132 THERMAL_ATTRS(1),
3133 THERMAL_ATTRS(2),
3134 THERMAL_ATTRS(3),
3135 THERMAL_ATTRS(4),
3136 THERMAL_ATTRS(5),
3137 THERMAL_ATTRS(6),
3138 THERMAL_ATTRS(7),
3139 NULL
3142 static const struct attribute_group thermal_temp_input16_group = {
3143 .attrs = thermal_temp_input_attr
3146 static const struct attribute_group thermal_temp_input8_group = {
3147 .attrs = &thermal_temp_input_attr[8]
3150 #undef THERMAL_SENSOR_ATTR_TEMP
3151 #undef THERMAL_ATTRS
3153 /* --------------------------------------------------------------------- */
3155 static int __init thermal_init(struct ibm_init_struct *iibm)
3157 u8 t, ta1, ta2;
3158 int i;
3159 int acpi_tmp7;
3160 int res;
3162 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3164 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3166 if (thinkpad_id.ec_model) {
3168 * Direct EC access mode: sensors at registers
3169 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3170 * non-implemented, thermal sensors return 0x80 when
3171 * not available
3174 ta1 = ta2 = 0;
3175 for (i = 0; i < 8; i++) {
3176 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3177 ta1 |= t;
3178 } else {
3179 ta1 = 0;
3180 break;
3182 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3183 ta2 |= t;
3184 } else {
3185 ta1 = 0;
3186 break;
3189 if (ta1 == 0) {
3190 /* This is sheer paranoia, but we handle it anyway */
3191 if (acpi_tmp7) {
3192 printk(IBM_ERR
3193 "ThinkPad ACPI EC access misbehaving, "
3194 "falling back to ACPI TMPx access mode\n");
3195 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3196 } else {
3197 printk(IBM_ERR
3198 "ThinkPad ACPI EC access misbehaving, "
3199 "disabling thermal sensors access\n");
3200 thermal_read_mode = TPACPI_THERMAL_NONE;
3202 } else {
3203 thermal_read_mode =
3204 (ta2 != 0) ?
3205 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3207 } else if (acpi_tmp7) {
3208 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3209 /* 600e/x, 770e, 770x */
3210 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3211 } else {
3212 /* Standard ACPI TMPx access, max 8 sensors */
3213 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3215 } else {
3216 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3217 thermal_read_mode = TPACPI_THERMAL_NONE;
3220 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3221 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3222 thermal_read_mode);
3224 switch(thermal_read_mode) {
3225 case TPACPI_THERMAL_TPEC_16:
3226 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3227 &thermal_temp_input16_group);
3228 if (res)
3229 return res;
3230 break;
3231 case TPACPI_THERMAL_TPEC_8:
3232 case TPACPI_THERMAL_ACPI_TMP07:
3233 case TPACPI_THERMAL_ACPI_UPDT:
3234 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3235 &thermal_temp_input8_group);
3236 if (res)
3237 return res;
3238 break;
3239 case TPACPI_THERMAL_NONE:
3240 default:
3241 return 1;
3244 return 0;
3247 static void thermal_exit(void)
3249 switch(thermal_read_mode) {
3250 case TPACPI_THERMAL_TPEC_16:
3251 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3252 &thermal_temp_input16_group);
3253 break;
3254 case TPACPI_THERMAL_TPEC_8:
3255 case TPACPI_THERMAL_ACPI_TMP07:
3256 case TPACPI_THERMAL_ACPI_UPDT:
3257 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3258 &thermal_temp_input16_group);
3259 break;
3260 case TPACPI_THERMAL_NONE:
3261 default:
3262 break;
3266 /* idx is zero-based */
3267 static int thermal_get_sensor(int idx, s32 *value)
3269 int t;
3270 s8 tmp;
3271 char tmpi[5];
3273 t = TP_EC_THERMAL_TMP0;
3275 switch (thermal_read_mode) {
3276 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3277 case TPACPI_THERMAL_TPEC_16:
3278 if (idx >= 8 && idx <= 15) {
3279 t = TP_EC_THERMAL_TMP8;
3280 idx -= 8;
3282 /* fallthrough */
3283 #endif
3284 case TPACPI_THERMAL_TPEC_8:
3285 if (idx <= 7) {
3286 if (!acpi_ec_read(t + idx, &tmp))
3287 return -EIO;
3288 *value = tmp * 1000;
3289 return 0;
3291 break;
3293 case TPACPI_THERMAL_ACPI_UPDT:
3294 if (idx <= 7) {
3295 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3296 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3297 return -EIO;
3298 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3299 return -EIO;
3300 *value = (t - 2732) * 100;
3301 return 0;
3303 break;
3305 case TPACPI_THERMAL_ACPI_TMP07:
3306 if (idx <= 7) {
3307 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3308 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3309 return -EIO;
3310 if (t > 127 || t < -127)
3311 t = TP_EC_THERMAL_TMP_NA;
3312 *value = t * 1000;
3313 return 0;
3315 break;
3317 case TPACPI_THERMAL_NONE:
3318 default:
3319 return -ENOSYS;
3322 return -EINVAL;
3325 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3327 int res, i;
3328 int n;
3330 n = 8;
3331 i = 0;
3333 if (!s)
3334 return -EINVAL;
3336 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3337 n = 16;
3339 for(i = 0 ; i < n; i++) {
3340 res = thermal_get_sensor(i, &s->temp[i]);
3341 if (res)
3342 return res;
3345 return n;
3348 static int thermal_read(char *p)
3350 int len = 0;
3351 int n, i;
3352 struct ibm_thermal_sensors_struct t;
3354 n = thermal_get_sensors(&t);
3355 if (unlikely(n < 0))
3356 return n;
3358 len += sprintf(p + len, "temperatures:\t");
3360 if (n > 0) {
3361 for (i = 0; i < (n - 1); i++)
3362 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3363 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3364 } else
3365 len += sprintf(p + len, "not supported\n");
3367 return len;
3370 static struct ibm_struct thermal_driver_data = {
3371 .name = "thermal",
3372 .read = thermal_read,
3373 .exit = thermal_exit,
3376 /*************************************************************************
3377 * EC Dump subdriver
3380 static u8 ecdump_regs[256];
3382 static int ecdump_read(char *p)
3384 int len = 0;
3385 int i, j;
3386 u8 v;
3388 len += sprintf(p + len, "EC "
3389 " +00 +01 +02 +03 +04 +05 +06 +07"
3390 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3391 for (i = 0; i < 256; i += 16) {
3392 len += sprintf(p + len, "EC 0x%02x:", i);
3393 for (j = 0; j < 16; j++) {
3394 if (!acpi_ec_read(i + j, &v))
3395 break;
3396 if (v != ecdump_regs[i + j])
3397 len += sprintf(p + len, " *%02x", v);
3398 else
3399 len += sprintf(p + len, " %02x", v);
3400 ecdump_regs[i + j] = v;
3402 len += sprintf(p + len, "\n");
3403 if (j != 16)
3404 break;
3407 /* These are way too dangerous to advertise openly... */
3408 #if 0
3409 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3410 " (<offset> is 00-ff, <value> is 00-ff)\n");
3411 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3412 " (<offset> is 00-ff, <value> is 0-255)\n");
3413 #endif
3414 return len;
3417 static int ecdump_write(char *buf)
3419 char *cmd;
3420 int i, v;
3422 while ((cmd = next_cmd(&buf))) {
3423 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3424 /* i and v set */
3425 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3426 /* i and v set */
3427 } else
3428 return -EINVAL;
3429 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3430 if (!acpi_ec_write(i, v))
3431 return -EIO;
3432 } else
3433 return -EINVAL;
3436 return 0;
3439 static struct ibm_struct ecdump_driver_data = {
3440 .name = "ecdump",
3441 .read = ecdump_read,
3442 .write = ecdump_write,
3443 .flags.experimental = 1,
3446 /*************************************************************************
3447 * Backlight/brightness subdriver
3450 static struct backlight_device *ibm_backlight_device;
3452 static struct backlight_properties ibm_backlight_data = {
3453 .owner = THIS_MODULE,
3454 .get_brightness = brightness_get,
3455 .update_status = brightness_update_status,
3456 .max_brightness = 7,
3459 static struct mutex brightness_mutex;
3461 static int __init brightness_init(struct ibm_init_struct *iibm)
3463 int b;
3465 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3467 mutex_init(&brightness_mutex);
3469 if (!brightness_mode) {
3470 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3471 brightness_mode = 2;
3472 else
3473 brightness_mode = 3;
3475 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3476 brightness_mode);
3479 if (brightness_mode > 3)
3480 return -EINVAL;
3482 b = brightness_get(NULL);
3483 if (b < 0)
3484 return 1;
3486 ibm_backlight_device = backlight_device_register(
3487 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3488 &ibm_backlight_data);
3489 if (IS_ERR(ibm_backlight_device)) {
3490 printk(IBM_ERR "Could not register backlight device\n");
3491 return PTR_ERR(ibm_backlight_device);
3493 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
3495 ibm_backlight_device->props->brightness = b;
3496 brightness_update_status(ibm_backlight_device);
3498 return 0;
3501 static void brightness_exit(void)
3503 if (ibm_backlight_device) {
3504 vdbg_printk(TPACPI_DBG_EXIT,
3505 "calling backlight_device_unregister()\n");
3506 backlight_device_unregister(ibm_backlight_device);
3507 ibm_backlight_device = NULL;
3511 static int brightness_update_status(struct backlight_device *bd)
3513 return brightness_set(
3514 (bd->props->fb_blank == FB_BLANK_UNBLANK &&
3515 bd->props->power == FB_BLANK_UNBLANK) ?
3516 bd->props->brightness : 0);
3520 * ThinkPads can read brightness from two places: EC 0x31, or
3521 * CMOS NVRAM byte 0x5E, bits 0-3.
3523 static int brightness_get(struct backlight_device *bd)
3525 u8 lec = 0, lcmos = 0, level = 0;
3527 if (brightness_mode & 1) {
3528 if (!acpi_ec_read(brightness_offset, &lec))
3529 return -EIO;
3530 lec &= 7;
3531 level = lec;
3533 if (brightness_mode & 2) {
3534 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3535 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3536 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3537 level = lcmos;
3540 if (brightness_mode == 3 && lec != lcmos) {
3541 printk(IBM_ERR
3542 "CMOS NVRAM (%u) and EC (%u) do not agree "
3543 "on display brightness level\n",
3544 (unsigned int) lcmos,
3545 (unsigned int) lec);
3546 return -EIO;
3549 return level;
3552 static int brightness_set(int value)
3554 int cmos_cmd, inc, i, res;
3555 int current_value;
3557 if (value > 7)
3558 return -EINVAL;
3560 res = mutex_lock_interruptible(&brightness_mutex);
3561 if (res < 0)
3562 return res;
3564 current_value = brightness_get(NULL);
3565 if (current_value < 0) {
3566 res = current_value;
3567 goto errout;
3570 cmos_cmd = value > current_value ?
3571 TP_CMOS_BRIGHTNESS_UP :
3572 TP_CMOS_BRIGHTNESS_DOWN;
3573 inc = value > current_value ? 1 : -1;
3575 res = 0;
3576 for (i = current_value; i != value; i += inc) {
3577 if ((brightness_mode & 2) &&
3578 issue_thinkpad_cmos_command(cmos_cmd)) {
3579 res = -EIO;
3580 goto errout;
3582 if ((brightness_mode & 1) &&
3583 !acpi_ec_write(brightness_offset, i + inc)) {
3584 res = -EIO;
3585 goto errout;;
3589 errout:
3590 mutex_unlock(&brightness_mutex);
3591 return res;
3594 static int brightness_read(char *p)
3596 int len = 0;
3597 int level;
3599 if ((level = brightness_get(NULL)) < 0) {
3600 len += sprintf(p + len, "level:\t\tunreadable\n");
3601 } else {
3602 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3603 len += sprintf(p + len, "commands:\tup, down\n");
3604 len += sprintf(p + len, "commands:\tlevel <level>"
3605 " (<level> is 0-7)\n");
3608 return len;
3611 static int brightness_write(char *buf)
3613 int level;
3614 int new_level;
3615 char *cmd;
3617 while ((cmd = next_cmd(&buf))) {
3618 if ((level = brightness_get(NULL)) < 0)
3619 return level;
3620 level &= 7;
3622 if (strlencmp(cmd, "up") == 0) {
3623 new_level = level == 7 ? 7 : level + 1;
3624 } else if (strlencmp(cmd, "down") == 0) {
3625 new_level = level == 0 ? 0 : level - 1;
3626 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3627 new_level >= 0 && new_level <= 7) {
3628 /* new_level set */
3629 } else
3630 return -EINVAL;
3632 brightness_set(new_level);
3635 return 0;
3638 static struct ibm_struct brightness_driver_data = {
3639 .name = "brightness",
3640 .read = brightness_read,
3641 .write = brightness_write,
3642 .exit = brightness_exit,
3645 /*************************************************************************
3646 * Volume subdriver
3649 static int volume_read(char *p)
3651 int len = 0;
3652 u8 level;
3654 if (!acpi_ec_read(volume_offset, &level)) {
3655 len += sprintf(p + len, "level:\t\tunreadable\n");
3656 } else {
3657 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3658 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3659 len += sprintf(p + len, "commands:\tup, down, mute\n");
3660 len += sprintf(p + len, "commands:\tlevel <level>"
3661 " (<level> is 0-15)\n");
3664 return len;
3667 static int volume_write(char *buf)
3669 int cmos_cmd, inc, i;
3670 u8 level, mute;
3671 int new_level, new_mute;
3672 char *cmd;
3674 while ((cmd = next_cmd(&buf))) {
3675 if (!acpi_ec_read(volume_offset, &level))
3676 return -EIO;
3677 new_mute = mute = level & 0x40;
3678 new_level = level = level & 0xf;
3680 if (strlencmp(cmd, "up") == 0) {
3681 if (mute)
3682 new_mute = 0;
3683 else
3684 new_level = level == 15 ? 15 : level + 1;
3685 } else if (strlencmp(cmd, "down") == 0) {
3686 if (mute)
3687 new_mute = 0;
3688 else
3689 new_level = level == 0 ? 0 : level - 1;
3690 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3691 new_level >= 0 && new_level <= 15) {
3692 /* new_level set */
3693 } else if (strlencmp(cmd, "mute") == 0) {
3694 new_mute = 0x40;
3695 } else
3696 return -EINVAL;
3698 if (new_level != level) { /* mute doesn't change */
3699 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3700 inc = new_level > level ? 1 : -1;
3702 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3703 !acpi_ec_write(volume_offset, level)))
3704 return -EIO;
3706 for (i = level; i != new_level; i += inc)
3707 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3708 !acpi_ec_write(volume_offset, i + inc))
3709 return -EIO;
3711 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3712 !acpi_ec_write(volume_offset,
3713 new_level + mute)))
3714 return -EIO;
3717 if (new_mute != mute) { /* level doesn't change */
3718 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3720 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3721 !acpi_ec_write(volume_offset, level + new_mute))
3722 return -EIO;
3726 return 0;
3729 static struct ibm_struct volume_driver_data = {
3730 .name = "volume",
3731 .read = volume_read,
3732 .write = volume_write,
3735 /*************************************************************************
3736 * Fan subdriver
3740 * FAN ACCESS MODES
3742 * TPACPI_FAN_RD_ACPI_GFAN:
3743 * ACPI GFAN method: returns fan level
3745 * see TPACPI_FAN_WR_ACPI_SFAN
3746 * EC 0x2f (HFSP) not available if GFAN exists
3748 * TPACPI_FAN_WR_ACPI_SFAN:
3749 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3751 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3752 * it for writing.
3754 * TPACPI_FAN_WR_TPEC:
3755 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3756 * Supported on almost all ThinkPads
3758 * Fan speed changes of any sort (including those caused by the
3759 * disengaged mode) are usually done slowly by the firmware as the
3760 * maximum ammount of fan duty cycle change per second seems to be
3761 * limited.
3763 * Reading is not available if GFAN exists.
3764 * Writing is not available if SFAN exists.
3766 * Bits
3767 * 7 automatic mode engaged;
3768 * (default operation mode of the ThinkPad)
3769 * fan level is ignored in this mode.
3770 * 6 full speed mode (takes precedence over bit 7);
3771 * not available on all thinkpads. May disable
3772 * the tachometer while the fan controller ramps up
3773 * the speed (which can take up to a few *minutes*).
3774 * Speeds up fan to 100% duty-cycle, which is far above
3775 * the standard RPM levels. It is not impossible that
3776 * it could cause hardware damage.
3777 * 5-3 unused in some models. Extra bits for fan level
3778 * in others, but still useless as all values above
3779 * 7 map to the same speed as level 7 in these models.
3780 * 2-0 fan level (0..7 usually)
3781 * 0x00 = stop
3782 * 0x07 = max (set when temperatures critical)
3783 * Some ThinkPads may have other levels, see
3784 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3786 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3787 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3788 * does so, its initial value is meaningless (0x07).
3790 * For firmware bugs, refer to:
3791 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3793 * ----
3795 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3796 * Main fan tachometer reading (in RPM)
3798 * This register is present on all ThinkPads with a new-style EC, and
3799 * it is known not to be present on the A21m/e, and T22, as there is
3800 * something else in offset 0x84 according to the ACPI DSDT. Other
3801 * ThinkPads from this same time period (and earlier) probably lack the
3802 * tachometer as well.
3804 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3805 * was never fixed by IBM to report the EC firmware version string
3806 * probably support the tachometer (like the early X models), so
3807 * detecting it is quite hard. We need more data to know for sure.
3809 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3810 * might result.
3812 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3813 * mode.
3815 * For firmware bugs, refer to:
3816 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3818 * TPACPI_FAN_WR_ACPI_FANS:
3819 * ThinkPad X31, X40, X41. Not available in the X60.
3821 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3822 * high speed. ACPI DSDT seems to map these three speeds to levels
3823 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3824 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3826 * The speeds are stored on handles
3827 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3829 * There are three default speed sets, acessible as handles:
3830 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3832 * ACPI DSDT switches which set is in use depending on various
3833 * factors.
3835 * TPACPI_FAN_WR_TPEC is also available and should be used to
3836 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3837 * but the ACPI tables just mention level 7.
3840 static enum fan_status_access_mode fan_status_access_mode;
3841 static enum fan_control_access_mode fan_control_access_mode;
3842 static enum fan_control_commands fan_control_commands;
3844 static u8 fan_control_initial_status;
3845 static u8 fan_control_desired_level;
3847 static void fan_watchdog_fire(struct work_struct *ignored);
3848 static int fan_watchdog_maxinterval;
3849 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3851 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3852 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3853 "\\FSPD", /* 600e/x, 770e, 770x */
3854 ); /* all others */
3855 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3856 "JFNS", /* 770x-JL */
3857 ); /* all others */
3860 * SYSFS fan layout: hwmon compatible (device)
3862 * pwm*_enable:
3863 * 0: "disengaged" mode
3864 * 1: manual mode
3865 * 2: native EC "auto" mode (recommended, hardware default)
3867 * pwm*: set speed in manual mode, ignored otherwise.
3868 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3869 * interpolation.
3871 * fan*_input: tachometer reading, RPM
3874 * SYSFS fan layout: extensions
3876 * fan_watchdog (driver):
3877 * fan watchdog interval in seconds, 0 disables (default), max 120
3880 /* sysfs fan pwm1_enable ----------------------------------------------- */
3881 static ssize_t fan_pwm1_enable_show(struct device *dev,
3882 struct device_attribute *attr,
3883 char *buf)
3885 int res, mode;
3886 u8 status;
3888 res = fan_get_status_safe(&status);
3889 if (res)
3890 return res;
3892 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3893 if (status != fan_control_initial_status) {
3894 tp_features.fan_ctrl_status_undef = 0;
3895 } else {
3896 /* Return most likely status. In fact, it
3897 * might be the only possible status */
3898 status = TP_EC_FAN_AUTO;
3902 if (status & TP_EC_FAN_FULLSPEED) {
3903 mode = 0;
3904 } else if (status & TP_EC_FAN_AUTO) {
3905 mode = 2;
3906 } else
3907 mode = 1;
3909 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3912 static ssize_t fan_pwm1_enable_store(struct device *dev,
3913 struct device_attribute *attr,
3914 const char *buf, size_t count)
3916 unsigned long t;
3917 int res, level;
3919 if (parse_strtoul(buf, 2, &t))
3920 return -EINVAL;
3922 switch (t) {
3923 case 0:
3924 level = TP_EC_FAN_FULLSPEED;
3925 break;
3926 case 1:
3927 level = TPACPI_FAN_LAST_LEVEL;
3928 break;
3929 case 2:
3930 level = TP_EC_FAN_AUTO;
3931 break;
3932 case 3:
3933 /* reserved for software-controlled auto mode */
3934 return -ENOSYS;
3935 default:
3936 return -EINVAL;
3939 res = fan_set_level_safe(level);
3940 if (res == -ENXIO)
3941 return -EINVAL;
3942 else if (res < 0)
3943 return res;
3945 fan_watchdog_reset();
3947 return count;
3950 static struct device_attribute dev_attr_fan_pwm1_enable =
3951 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3952 fan_pwm1_enable_show, fan_pwm1_enable_store);
3954 /* sysfs fan pwm1 ------------------------------------------------------ */
3955 static ssize_t fan_pwm1_show(struct device *dev,
3956 struct device_attribute *attr,
3957 char *buf)
3959 int res;
3960 u8 status;
3962 res = fan_get_status_safe(&status);
3963 if (res)
3964 return res;
3966 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3967 if (status != fan_control_initial_status) {
3968 tp_features.fan_ctrl_status_undef = 0;
3969 } else {
3970 status = TP_EC_FAN_AUTO;
3974 if ((status &
3975 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3976 status = fan_control_desired_level;
3978 if (status > 7)
3979 status = 7;
3981 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3984 static ssize_t fan_pwm1_store(struct device *dev,
3985 struct device_attribute *attr,
3986 const char *buf, size_t count)
3988 unsigned long s;
3989 int rc;
3990 u8 status, newlevel;
3992 if (parse_strtoul(buf, 255, &s))
3993 return -EINVAL;
3995 /* scale down from 0-255 to 0-7 */
3996 newlevel = (s >> 5) & 0x07;
3998 rc = mutex_lock_interruptible(&fan_mutex);
3999 if (rc < 0)
4000 return rc;
4002 rc = fan_get_status(&status);
4003 if (!rc && (status &
4004 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4005 rc = fan_set_level(newlevel);
4006 if (rc == -ENXIO)
4007 rc = -EINVAL;
4008 else if (!rc) {
4009 fan_update_desired_level(newlevel);
4010 fan_watchdog_reset();
4014 mutex_unlock(&fan_mutex);
4015 return (rc)? rc : count;
4018 static struct device_attribute dev_attr_fan_pwm1 =
4019 __ATTR(pwm1, S_IWUSR | S_IRUGO,
4020 fan_pwm1_show, fan_pwm1_store);
4022 /* sysfs fan fan1_input ------------------------------------------------ */
4023 static ssize_t fan_fan1_input_show(struct device *dev,
4024 struct device_attribute *attr,
4025 char *buf)
4027 int res;
4028 unsigned int speed;
4030 res = fan_get_speed(&speed);
4031 if (res < 0)
4032 return res;
4034 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
4037 static struct device_attribute dev_attr_fan_fan1_input =
4038 __ATTR(fan1_input, S_IRUGO,
4039 fan_fan1_input_show, NULL);
4041 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
4042 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
4043 char *buf)
4045 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
4048 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
4049 const char *buf, size_t count)
4051 unsigned long t;
4053 if (parse_strtoul(buf, 120, &t))
4054 return -EINVAL;
4056 if (!fan_control_allowed)
4057 return -EPERM;
4059 fan_watchdog_maxinterval = t;
4060 fan_watchdog_reset();
4062 return count;
4065 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
4066 fan_fan_watchdog_show, fan_fan_watchdog_store);
4068 /* --------------------------------------------------------------------- */
4069 static struct attribute *fan_attributes[] = {
4070 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
4071 &dev_attr_fan_fan1_input.attr,
4072 NULL
4075 static const struct attribute_group fan_attr_group = {
4076 .attrs = fan_attributes,
4079 static int __init fan_init(struct ibm_init_struct *iibm)
4081 int rc;
4083 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
4085 mutex_init(&fan_mutex);
4086 fan_status_access_mode = TPACPI_FAN_NONE;
4087 fan_control_access_mode = TPACPI_FAN_WR_NONE;
4088 fan_control_commands = 0;
4089 fan_watchdog_maxinterval = 0;
4090 tp_features.fan_ctrl_status_undef = 0;
4091 fan_control_desired_level = 7;
4093 IBM_ACPIHANDLE_INIT(fans);
4094 IBM_ACPIHANDLE_INIT(gfan);
4095 IBM_ACPIHANDLE_INIT(sfan);
4097 if (gfan_handle) {
4098 /* 570, 600e/x, 770e, 770x */
4099 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
4100 } else {
4101 /* all other ThinkPads: note that even old-style
4102 * ThinkPad ECs supports the fan control register */
4103 if (likely(acpi_ec_read(fan_status_offset,
4104 &fan_control_initial_status))) {
4105 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
4107 /* In some ThinkPads, neither the EC nor the ACPI
4108 * DSDT initialize the fan status, and it ends up
4109 * being set to 0x07 when it *could* be either
4110 * 0x07 or 0x80.
4112 * Enable for TP-1Y (T43), TP-78 (R51e),
4113 * TP-76 (R52), TP-70 (T43, R52), which are known
4114 * to be buggy. */
4115 if (fan_control_initial_status == 0x07) {
4116 switch (thinkpad_id.ec_model) {
4117 case 0x5931: /* TP-1Y */
4118 case 0x3837: /* TP-78 */
4119 case 0x3637: /* TP-76 */
4120 case 0x3037: /* TP-70 */
4121 printk(IBM_NOTICE
4122 "fan_init: initial fan status is "
4123 "unknown, assuming it is in auto "
4124 "mode\n");
4125 tp_features.fan_ctrl_status_undef = 1;
4129 } else {
4130 printk(IBM_ERR
4131 "ThinkPad ACPI EC access misbehaving, "
4132 "fan status and control unavailable\n");
4133 return 1;
4137 if (sfan_handle) {
4138 /* 570, 770x-JL */
4139 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
4140 fan_control_commands |=
4141 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
4142 } else {
4143 if (!gfan_handle) {
4144 /* gfan without sfan means no fan control */
4145 /* all other models implement TP EC 0x2f control */
4147 if (fans_handle) {
4148 /* X31, X40, X41 */
4149 fan_control_access_mode =
4150 TPACPI_FAN_WR_ACPI_FANS;
4151 fan_control_commands |=
4152 TPACPI_FAN_CMD_SPEED |
4153 TPACPI_FAN_CMD_LEVEL |
4154 TPACPI_FAN_CMD_ENABLE;
4155 } else {
4156 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
4157 fan_control_commands |=
4158 TPACPI_FAN_CMD_LEVEL |
4159 TPACPI_FAN_CMD_ENABLE;
4164 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
4165 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
4166 fan_control_access_mode != TPACPI_FAN_WR_NONE),
4167 fan_status_access_mode, fan_control_access_mode);
4169 /* fan control master switch */
4170 if (!fan_control_allowed) {
4171 fan_control_access_mode = TPACPI_FAN_WR_NONE;
4172 fan_control_commands = 0;
4173 dbg_printk(TPACPI_DBG_INIT,
4174 "fan control features disabled by parameter\n");
4177 /* update fan_control_desired_level */
4178 if (fan_status_access_mode != TPACPI_FAN_NONE)
4179 fan_get_status_safe(NULL);
4181 if (fan_status_access_mode != TPACPI_FAN_NONE ||
4182 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
4183 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4184 &fan_attr_group);
4185 if (!(rc < 0))
4186 rc = driver_create_file(&tpacpi_pdriver.driver,
4187 &driver_attr_fan_watchdog);
4188 if (rc < 0)
4189 return rc;
4190 return 0;
4191 } else
4192 return 1;
4196 * Call with fan_mutex held
4198 static void fan_update_desired_level(u8 status)
4200 if ((status &
4201 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4202 if (status > 7)
4203 fan_control_desired_level = 7;
4204 else
4205 fan_control_desired_level = status;
4209 static int fan_get_status(u8 *status)
4211 u8 s;
4213 /* TODO:
4214 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4216 switch (fan_status_access_mode) {
4217 case TPACPI_FAN_RD_ACPI_GFAN:
4218 /* 570, 600e/x, 770e, 770x */
4220 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4221 return -EIO;
4223 if (likely(status))
4224 *status = s & 0x07;
4226 break;
4228 case TPACPI_FAN_RD_TPEC:
4229 /* all except 570, 600e/x, 770e, 770x */
4230 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4231 return -EIO;
4233 if (likely(status))
4234 *status = s;
4236 break;
4238 default:
4239 return -ENXIO;
4242 return 0;
4245 static int fan_get_status_safe(u8 *status)
4247 int rc;
4248 u8 s;
4250 rc = mutex_lock_interruptible(&fan_mutex);
4251 if (rc < 0)
4252 return rc;
4253 rc = fan_get_status(&s);
4254 if (!rc)
4255 fan_update_desired_level(s);
4256 mutex_unlock(&fan_mutex);
4258 if (status)
4259 *status = s;
4261 return rc;
4264 static void fan_exit(void)
4266 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
4268 /* FIXME: can we really do this unconditionally? */
4269 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
4270 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
4272 cancel_delayed_work(&fan_watchdog_task);
4273 flush_scheduled_work();
4276 static int fan_get_speed(unsigned int *speed)
4278 u8 hi, lo;
4280 switch (fan_status_access_mode) {
4281 case TPACPI_FAN_RD_TPEC:
4282 /* all except 570, 600e/x, 770e, 770x */
4283 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4284 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4285 return -EIO;
4287 if (likely(speed))
4288 *speed = (hi << 8) | lo;
4290 break;
4292 default:
4293 return -ENXIO;
4296 return 0;
4299 static void fan_watchdog_fire(struct work_struct *ignored)
4301 int rc;
4303 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4304 return;
4306 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
4307 rc = fan_set_enable();
4308 if (rc < 0) {
4309 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
4310 "will try again later...\n", -rc);
4311 /* reschedule for later */
4312 fan_watchdog_reset();
4316 static void fan_watchdog_reset(void)
4318 static int fan_watchdog_active;
4320 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4321 return;
4323 if (fan_watchdog_active)
4324 cancel_delayed_work(&fan_watchdog_task);
4326 if (fan_watchdog_maxinterval > 0 &&
4327 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4328 fan_watchdog_active = 1;
4329 if (!schedule_delayed_work(&fan_watchdog_task,
4330 msecs_to_jiffies(fan_watchdog_maxinterval
4331 * 1000))) {
4332 printk(IBM_ERR "failed to schedule the fan watchdog, "
4333 "watchdog will not trigger\n");
4335 } else
4336 fan_watchdog_active = 0;
4339 static int fan_set_level(int level)
4341 if (!fan_control_allowed)
4342 return -EPERM;
4344 switch (fan_control_access_mode) {
4345 case TPACPI_FAN_WR_ACPI_SFAN:
4346 if (level >= 0 && level <= 7) {
4347 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4348 return -EIO;
4349 } else
4350 return -EINVAL;
4351 break;
4353 case TPACPI_FAN_WR_ACPI_FANS:
4354 case TPACPI_FAN_WR_TPEC:
4355 if ((level != TP_EC_FAN_AUTO) &&
4356 (level != TP_EC_FAN_FULLSPEED) &&
4357 ((level < 0) || (level > 7)))
4358 return -EINVAL;
4360 /* safety net should the EC not support AUTO
4361 * or FULLSPEED mode bits and just ignore them */
4362 if (level & TP_EC_FAN_FULLSPEED)
4363 level |= 7; /* safety min speed 7 */
4364 else if (level & TP_EC_FAN_FULLSPEED)
4365 level |= 4; /* safety min speed 4 */
4367 if (!acpi_ec_write(fan_status_offset, level))
4368 return -EIO;
4369 else
4370 tp_features.fan_ctrl_status_undef = 0;
4371 break;
4373 default:
4374 return -ENXIO;
4376 return 0;
4379 static int fan_set_level_safe(int level)
4381 int rc;
4383 if (!fan_control_allowed)
4384 return -EPERM;
4386 rc = mutex_lock_interruptible(&fan_mutex);
4387 if (rc < 0)
4388 return rc;
4390 if (level == TPACPI_FAN_LAST_LEVEL)
4391 level = fan_control_desired_level;
4393 rc = fan_set_level(level);
4394 if (!rc)
4395 fan_update_desired_level(level);
4397 mutex_unlock(&fan_mutex);
4398 return rc;
4401 static int fan_set_enable(void)
4403 u8 s;
4404 int rc;
4406 if (!fan_control_allowed)
4407 return -EPERM;
4409 rc = mutex_lock_interruptible(&fan_mutex);
4410 if (rc < 0)
4411 return rc;
4413 switch (fan_control_access_mode) {
4414 case TPACPI_FAN_WR_ACPI_FANS:
4415 case TPACPI_FAN_WR_TPEC:
4416 rc = fan_get_status(&s);
4417 if (rc < 0)
4418 break;
4420 /* Don't go out of emergency fan mode */
4421 if (s != 7) {
4422 s &= 0x07;
4423 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4426 if (!acpi_ec_write(fan_status_offset, s))
4427 rc = -EIO;
4428 else {
4429 tp_features.fan_ctrl_status_undef = 0;
4430 rc = 0;
4432 break;
4434 case TPACPI_FAN_WR_ACPI_SFAN:
4435 rc = fan_get_status(&s);
4436 if (rc < 0)
4437 break;
4439 s &= 0x07;
4441 /* Set fan to at least level 4 */
4442 s |= 4;
4444 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4445 rc= -EIO;
4446 else
4447 rc = 0;
4448 break;
4450 default:
4451 rc = -ENXIO;
4454 mutex_unlock(&fan_mutex);
4455 return rc;
4458 static int fan_set_disable(void)
4460 int rc;
4462 if (!fan_control_allowed)
4463 return -EPERM;
4465 rc = mutex_lock_interruptible(&fan_mutex);
4466 if (rc < 0)
4467 return rc;
4469 rc = 0;
4470 switch (fan_control_access_mode) {
4471 case TPACPI_FAN_WR_ACPI_FANS:
4472 case TPACPI_FAN_WR_TPEC:
4473 if (!acpi_ec_write(fan_status_offset, 0x00))
4474 rc = -EIO;
4475 else {
4476 fan_control_desired_level = 0;
4477 tp_features.fan_ctrl_status_undef = 0;
4479 break;
4481 case TPACPI_FAN_WR_ACPI_SFAN:
4482 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4483 rc = -EIO;
4484 else
4485 fan_control_desired_level = 0;
4486 break;
4488 default:
4489 rc = -ENXIO;
4493 mutex_unlock(&fan_mutex);
4494 return rc;
4497 static int fan_set_speed(int speed)
4499 int rc;
4501 if (!fan_control_allowed)
4502 return -EPERM;
4504 rc = mutex_lock_interruptible(&fan_mutex);
4505 if (rc < 0)
4506 return rc;
4508 rc = 0;
4509 switch (fan_control_access_mode) {
4510 case TPACPI_FAN_WR_ACPI_FANS:
4511 if (speed >= 0 && speed <= 65535) {
4512 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4513 speed, speed, speed))
4514 rc = -EIO;
4515 } else
4516 rc = -EINVAL;
4517 break;
4519 default:
4520 rc = -ENXIO;
4523 mutex_unlock(&fan_mutex);
4524 return rc;
4527 static int fan_read(char *p)
4529 int len = 0;
4530 int rc;
4531 u8 status;
4532 unsigned int speed = 0;
4534 switch (fan_status_access_mode) {
4535 case TPACPI_FAN_RD_ACPI_GFAN:
4536 /* 570, 600e/x, 770e, 770x */
4537 if ((rc = fan_get_status_safe(&status)) < 0)
4538 return rc;
4540 len += sprintf(p + len, "status:\t\t%s\n"
4541 "level:\t\t%d\n",
4542 (status != 0) ? "enabled" : "disabled", status);
4543 break;
4545 case TPACPI_FAN_RD_TPEC:
4546 /* all except 570, 600e/x, 770e, 770x */
4547 if ((rc = fan_get_status_safe(&status)) < 0)
4548 return rc;
4550 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4551 if (status != fan_control_initial_status)
4552 tp_features.fan_ctrl_status_undef = 0;
4553 else
4554 /* Return most likely status. In fact, it
4555 * might be the only possible status */
4556 status = TP_EC_FAN_AUTO;
4559 len += sprintf(p + len, "status:\t\t%s\n",
4560 (status != 0) ? "enabled" : "disabled");
4562 if ((rc = fan_get_speed(&speed)) < 0)
4563 return rc;
4565 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4567 if (status & TP_EC_FAN_FULLSPEED)
4568 /* Disengaged mode takes precedence */
4569 len += sprintf(p + len, "level:\t\tdisengaged\n");
4570 else if (status & TP_EC_FAN_AUTO)
4571 len += sprintf(p + len, "level:\t\tauto\n");
4572 else
4573 len += sprintf(p + len, "level:\t\t%d\n", status);
4574 break;
4576 case TPACPI_FAN_NONE:
4577 default:
4578 len += sprintf(p + len, "status:\t\tnot supported\n");
4581 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
4582 len += sprintf(p + len, "commands:\tlevel <level>");
4584 switch (fan_control_access_mode) {
4585 case TPACPI_FAN_WR_ACPI_SFAN:
4586 len += sprintf(p + len, " (<level> is 0-7)\n");
4587 break;
4589 default:
4590 len += sprintf(p + len, " (<level> is 0-7, "
4591 "auto, disengaged, full-speed)\n");
4592 break;
4596 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
4597 len += sprintf(p + len, "commands:\tenable, disable\n"
4598 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4599 "1-120 (seconds))\n");
4601 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
4602 len += sprintf(p + len, "commands:\tspeed <speed>"
4603 " (<speed> is 0-65535)\n");
4605 return len;
4608 static int fan_write_cmd_level(const char *cmd, int *rc)
4610 int level;
4612 if (strlencmp(cmd, "level auto") == 0)
4613 level = TP_EC_FAN_AUTO;
4614 else if ((strlencmp(cmd, "level disengaged") == 0) |
4615 (strlencmp(cmd, "level full-speed") == 0))
4616 level = TP_EC_FAN_FULLSPEED;
4617 else if (sscanf(cmd, "level %d", &level) != 1)
4618 return 0;
4620 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4621 printk(IBM_ERR "level command accepted for unsupported "
4622 "access mode %d", fan_control_access_mode);
4624 return 1;
4627 static int fan_write_cmd_enable(const char *cmd, int *rc)
4629 if (strlencmp(cmd, "enable") != 0)
4630 return 0;
4632 if ((*rc = fan_set_enable()) == -ENXIO)
4633 printk(IBM_ERR "enable command accepted for unsupported "
4634 "access mode %d", fan_control_access_mode);
4636 return 1;
4639 static int fan_write_cmd_disable(const char *cmd, int *rc)
4641 if (strlencmp(cmd, "disable") != 0)
4642 return 0;
4644 if ((*rc = fan_set_disable()) == -ENXIO)
4645 printk(IBM_ERR "disable command accepted for unsupported "
4646 "access mode %d", fan_control_access_mode);
4648 return 1;
4651 static int fan_write_cmd_speed(const char *cmd, int *rc)
4653 int speed;
4655 /* TODO:
4656 * Support speed <low> <medium> <high> ? */
4658 if (sscanf(cmd, "speed %d", &speed) != 1)
4659 return 0;
4661 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4662 printk(IBM_ERR "speed command accepted for unsupported "
4663 "access mode %d", fan_control_access_mode);
4665 return 1;
4668 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4670 int interval;
4672 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4673 return 0;
4675 if (interval < 0 || interval > 120)
4676 *rc = -EINVAL;
4677 else
4678 fan_watchdog_maxinterval = interval;
4680 return 1;
4683 static int fan_write(char *buf)
4685 char *cmd;
4686 int rc = 0;
4688 while (!rc && (cmd = next_cmd(&buf))) {
4689 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4690 fan_write_cmd_level(cmd, &rc)) &&
4691 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4692 (fan_write_cmd_enable(cmd, &rc) ||
4693 fan_write_cmd_disable(cmd, &rc) ||
4694 fan_write_cmd_watchdog(cmd, &rc))) &&
4695 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4696 fan_write_cmd_speed(cmd, &rc))
4698 rc = -EINVAL;
4699 else if (!rc)
4700 fan_watchdog_reset();
4703 return rc;
4706 static struct ibm_struct fan_driver_data = {
4707 .name = "fan",
4708 .read = fan_read,
4709 .write = fan_write,
4710 .exit = fan_exit,
4713 /****************************************************************************
4714 ****************************************************************************
4716 * Infrastructure
4718 ****************************************************************************
4719 ****************************************************************************/
4721 /* /proc support */
4722 static struct proc_dir_entry *proc_dir;
4724 /* Subdriver registry */
4725 static LIST_HEAD(tpacpi_all_drivers);
4729 * Module and infrastructure proble, init and exit handling
4732 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4733 static const char * __init str_supported(int is_supported)
4735 static char text_unsupported[] __initdata = "not supported";
4737 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4739 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4741 static int __init ibm_init(struct ibm_init_struct *iibm)
4743 int ret;
4744 struct ibm_struct *ibm = iibm->data;
4745 struct proc_dir_entry *entry;
4747 BUG_ON(ibm == NULL);
4749 INIT_LIST_HEAD(&ibm->all_drivers);
4751 if (ibm->flags.experimental && !experimental)
4752 return 0;
4754 dbg_printk(TPACPI_DBG_INIT,
4755 "probing for %s\n", ibm->name);
4757 if (iibm->init) {
4758 ret = iibm->init(iibm);
4759 if (ret > 0)
4760 return 0; /* probe failed */
4761 if (ret)
4762 return ret;
4764 ibm->flags.init_called = 1;
4767 if (ibm->acpi) {
4768 if (ibm->acpi->hid) {
4769 ret = register_tpacpi_subdriver(ibm);
4770 if (ret)
4771 goto err_out;
4774 if (ibm->acpi->notify) {
4775 ret = setup_acpi_notify(ibm);
4776 if (ret == -ENODEV) {
4777 printk(IBM_NOTICE "disabling subdriver %s\n",
4778 ibm->name);
4779 ret = 0;
4780 goto err_out;
4782 if (ret < 0)
4783 goto err_out;
4787 dbg_printk(TPACPI_DBG_INIT,
4788 "%s installed\n", ibm->name);
4790 if (ibm->read) {
4791 entry = create_proc_entry(ibm->name,
4792 S_IFREG | S_IRUGO | S_IWUSR,
4793 proc_dir);
4794 if (!entry) {
4795 printk(IBM_ERR "unable to create proc entry %s\n",
4796 ibm->name);
4797 ret = -ENODEV;
4798 goto err_out;
4800 entry->owner = THIS_MODULE;
4801 entry->data = ibm;
4802 entry->read_proc = &dispatch_procfs_read;
4803 if (ibm->write)
4804 entry->write_proc = &dispatch_procfs_write;
4805 ibm->flags.proc_created = 1;
4808 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4810 return 0;
4812 err_out:
4813 dbg_printk(TPACPI_DBG_INIT,
4814 "%s: at error exit path with result %d\n",
4815 ibm->name, ret);
4817 ibm_exit(ibm);
4818 return (ret < 0)? ret : 0;
4821 static void ibm_exit(struct ibm_struct *ibm)
4823 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4825 list_del_init(&ibm->all_drivers);
4827 if (ibm->flags.acpi_notify_installed) {
4828 dbg_printk(TPACPI_DBG_EXIT,
4829 "%s: acpi_remove_notify_handler\n", ibm->name);
4830 BUG_ON(!ibm->acpi);
4831 acpi_remove_notify_handler(*ibm->acpi->handle,
4832 ibm->acpi->type,
4833 dispatch_acpi_notify);
4834 ibm->flags.acpi_notify_installed = 0;
4835 ibm->flags.acpi_notify_installed = 0;
4838 if (ibm->flags.proc_created) {
4839 dbg_printk(TPACPI_DBG_EXIT,
4840 "%s: remove_proc_entry\n", ibm->name);
4841 remove_proc_entry(ibm->name, proc_dir);
4842 ibm->flags.proc_created = 0;
4845 if (ibm->flags.acpi_driver_registered) {
4846 dbg_printk(TPACPI_DBG_EXIT,
4847 "%s: acpi_bus_unregister_driver\n", ibm->name);
4848 BUG_ON(!ibm->acpi);
4849 acpi_bus_unregister_driver(ibm->acpi->driver);
4850 kfree(ibm->acpi->driver);
4851 ibm->acpi->driver = NULL;
4852 ibm->flags.acpi_driver_registered = 0;
4855 if (ibm->flags.init_called && ibm->exit) {
4856 ibm->exit();
4857 ibm->flags.init_called = 0;
4860 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4863 /* Probing */
4865 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
4867 struct dmi_device *dev = NULL;
4868 char ec_fw_string[18];
4870 if (!tp)
4871 return;
4873 memset(tp, 0, sizeof(*tp));
4875 if (dmi_name_in_vendors("IBM"))
4876 tp->vendor = PCI_VENDOR_ID_IBM;
4877 else if (dmi_name_in_vendors("LENOVO"))
4878 tp->vendor = PCI_VENDOR_ID_LENOVO;
4879 else
4880 return;
4882 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4883 GFP_KERNEL);
4884 if (!tp->bios_version_str)
4885 return;
4886 tp->bios_model = tp->bios_version_str[0]
4887 | (tp->bios_version_str[1] << 8);
4890 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4891 * X32 or newer, all Z series; Some models must have an
4892 * up-to-date BIOS or they will not be detected.
4894 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4896 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4897 if (sscanf(dev->name,
4898 "IBM ThinkPad Embedded Controller -[%17c",
4899 ec_fw_string) == 1) {
4900 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4901 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4903 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4904 tp->ec_model = ec_fw_string[0]
4905 | (ec_fw_string[1] << 8);
4906 break;
4910 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4911 GFP_KERNEL);
4912 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4913 kfree(tp->model_str);
4914 tp->model_str = NULL;
4918 static int __init probe_for_thinkpad(void)
4920 int is_thinkpad;
4922 if (acpi_disabled)
4923 return -ENODEV;
4926 * Non-ancient models have better DMI tagging, but very old models
4927 * don't.
4929 is_thinkpad = (thinkpad_id.model_str != NULL);
4931 /* ec is required because many other handles are relative to it */
4932 IBM_ACPIHANDLE_INIT(ec);
4933 if (!ec_handle) {
4934 if (is_thinkpad)
4935 printk(IBM_ERR
4936 "Not yet supported ThinkPad detected!\n");
4937 return -ENODEV;
4941 * Risks a regression on very old machines, but reduces potential
4942 * false positives a damn great deal
4944 if (!is_thinkpad)
4945 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
4947 if (!is_thinkpad && !force_load)
4948 return -ENODEV;
4950 return 0;
4954 /* Module init, exit, parameters */
4956 static struct ibm_init_struct ibms_init[] __initdata = {
4958 .init = thinkpad_acpi_driver_init,
4959 .data = &thinkpad_acpi_driver_data,
4962 .init = hotkey_init,
4963 .data = &hotkey_driver_data,
4966 .init = bluetooth_init,
4967 .data = &bluetooth_driver_data,
4970 .init = wan_init,
4971 .data = &wan_driver_data,
4974 .init = video_init,
4975 .data = &video_driver_data,
4978 .init = light_init,
4979 .data = &light_driver_data,
4981 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4983 .init = dock_init,
4984 .data = &dock_driver_data[0],
4987 .init = dock_init2,
4988 .data = &dock_driver_data[1],
4990 #endif
4991 #ifdef CONFIG_THINKPAD_ACPI_BAY
4993 .init = bay_init,
4994 .data = &bay_driver_data,
4996 #endif
4998 .init = cmos_init,
4999 .data = &cmos_driver_data,
5002 .init = led_init,
5003 .data = &led_driver_data,
5006 .init = beep_init,
5007 .data = &beep_driver_data,
5010 .init = thermal_init,
5011 .data = &thermal_driver_data,
5014 .data = &ecdump_driver_data,
5017 .init = brightness_init,
5018 .data = &brightness_driver_data,
5021 .data = &volume_driver_data,
5024 .init = fan_init,
5025 .data = &fan_driver_data,
5029 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5031 unsigned int i;
5032 struct ibm_struct *ibm;
5034 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5035 ibm = ibms_init[i].data;
5036 BUG_ON(ibm == NULL);
5038 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5039 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5040 return -ENOSPC;
5041 strcpy(ibms_init[i].param, val);
5042 strcat(ibms_init[i].param, ",");
5043 return 0;
5047 return -EINVAL;
5050 static int experimental;
5051 module_param(experimental, int, 0);
5053 static u32 dbg_level;
5054 module_param_named(debug, dbg_level, uint, 0);
5056 static int force_load;
5057 module_param(force_load, bool, 0);
5059 static int fan_control_allowed;
5060 module_param_named(fan_control, fan_control_allowed, bool, 0);
5062 static int brightness_mode;
5063 module_param_named(brightness_mode, brightness_mode, int, 0);
5065 static unsigned int hotkey_report_mode;
5066 module_param(hotkey_report_mode, uint, 0);
5068 #define IBM_PARAM(feature) \
5069 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
5071 IBM_PARAM(hotkey);
5072 IBM_PARAM(bluetooth);
5073 IBM_PARAM(video);
5074 IBM_PARAM(light);
5075 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5076 IBM_PARAM(dock);
5077 #endif
5078 #ifdef CONFIG_THINKPAD_ACPI_BAY
5079 IBM_PARAM(bay);
5080 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5081 IBM_PARAM(cmos);
5082 IBM_PARAM(led);
5083 IBM_PARAM(beep);
5084 IBM_PARAM(ecdump);
5085 IBM_PARAM(brightness);
5086 IBM_PARAM(volume);
5087 IBM_PARAM(fan);
5089 static int __init thinkpad_acpi_module_init(void)
5091 int ret, i;
5093 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5095 /* Parameter checking */
5096 if (hotkey_report_mode > 2)
5097 return -EINVAL;
5099 /* Driver-level probe */
5101 get_thinkpad_model_data(&thinkpad_id);
5102 ret = probe_for_thinkpad();
5103 if (ret) {
5104 thinkpad_acpi_module_exit();
5105 return ret;
5108 /* Driver initialization */
5110 IBM_ACPIHANDLE_INIT(ecrd);
5111 IBM_ACPIHANDLE_INIT(ecwr);
5113 mutex_init(&thinkpad_nvram_mutex);
5115 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
5116 if (!proc_dir) {
5117 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
5118 thinkpad_acpi_module_exit();
5119 return -ENODEV;
5121 proc_dir->owner = THIS_MODULE;
5123 ret = platform_driver_register(&tpacpi_pdriver);
5124 if (ret) {
5125 printk(IBM_ERR "unable to register platform driver\n");
5126 thinkpad_acpi_module_exit();
5127 return ret;
5129 tp_features.platform_drv_registered = 1;
5131 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
5132 if (ret) {
5133 printk(IBM_ERR "unable to create sysfs driver attributes\n");
5134 thinkpad_acpi_module_exit();
5135 return ret;
5137 tp_features.platform_drv_attrs_registered = 1;
5140 /* Device initialization */
5141 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
5142 NULL, 0);
5143 if (IS_ERR(tpacpi_pdev)) {
5144 ret = PTR_ERR(tpacpi_pdev);
5145 tpacpi_pdev = NULL;
5146 printk(IBM_ERR "unable to register platform device\n");
5147 thinkpad_acpi_module_exit();
5148 return ret;
5150 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
5151 if (IS_ERR(tpacpi_hwmon)) {
5152 ret = PTR_ERR(tpacpi_hwmon);
5153 tpacpi_hwmon = NULL;
5154 printk(IBM_ERR "unable to register hwmon device\n");
5155 thinkpad_acpi_module_exit();
5156 return ret;
5158 mutex_init(&tpacpi_inputdev_send_mutex);
5159 tpacpi_inputdev = input_allocate_device();
5160 if (!tpacpi_inputdev) {
5161 printk(IBM_ERR "unable to allocate input device\n");
5162 thinkpad_acpi_module_exit();
5163 return -ENOMEM;
5164 } else {
5165 /* Prepare input device, but don't register */
5166 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5167 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
5168 tpacpi_inputdev->id.bustype = BUS_HOST;
5169 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5170 thinkpad_id.vendor :
5171 PCI_VENDOR_ID_IBM;
5172 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5173 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5175 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5176 ret = ibm_init(&ibms_init[i]);
5177 if (ret >= 0 && *ibms_init[i].param)
5178 ret = ibms_init[i].data->write(ibms_init[i].param);
5179 if (ret < 0) {
5180 thinkpad_acpi_module_exit();
5181 return ret;
5184 ret = input_register_device(tpacpi_inputdev);
5185 if (ret < 0) {
5186 printk(IBM_ERR "unable to register input device\n");
5187 thinkpad_acpi_module_exit();
5188 return ret;
5189 } else {
5190 tp_features.input_device_registered = 1;
5193 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
5194 return 0;
5197 static void thinkpad_acpi_module_exit(void)
5199 struct ibm_struct *ibm, *itmp;
5201 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5203 list_for_each_entry_safe_reverse(ibm, itmp,
5204 &tpacpi_all_drivers,
5205 all_drivers) {
5206 ibm_exit(ibm);
5209 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5211 if (tpacpi_inputdev) {
5212 if (tp_features.input_device_registered)
5213 input_unregister_device(tpacpi_inputdev);
5214 else
5215 input_free_device(tpacpi_inputdev);
5218 if (tpacpi_hwmon)
5219 hwmon_device_unregister(tpacpi_hwmon);
5221 if (tpacpi_pdev)
5222 platform_device_unregister(tpacpi_pdev);
5224 if (tp_features.platform_drv_attrs_registered)
5225 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5227 if (tp_features.platform_drv_registered)
5228 platform_driver_unregister(&tpacpi_pdriver);
5230 if (proc_dir)
5231 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
5233 kfree(thinkpad_id.bios_version_str);
5234 kfree(thinkpad_id.ec_version_str);
5235 kfree(thinkpad_id.model_str);
5238 module_init(thinkpad_acpi_module_init);
5239 module_exit(thinkpad_acpi_module_exit);