ACPI: thinkpad-acpi: check version of hot key firmware
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blobd2cf29dfd0f7b4c574ec7450eac16cb06fdb3dfb
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2007 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define IBM_VERSION "0.16"
25 #define TPACPI_SYSFS_VERSION 0x010000
28 * Changelog:
29 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
30 * drivers/misc.
32 * 2006-11-22 0.13 new maintainer
33 * changelog now lives in git commit history, and will
34 * not be updated further in-file.
36 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
37 * 2005-03-17 0.11 support for 600e, 770x
38 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
39 * support for 770e, G41
40 * G40 and G41 don't have a thinklight
41 * temperatures no longer experimental
42 * experimental brightness control
43 * experimental volume control
44 * experimental fan enable/disable
45 * 2005-01-16 0.10 fix module loading on R30, R31
46 * 2005-01-16 0.9 support for 570, R30, R31
47 * ultrabay support on A22p, A3x
48 * limit arg for cmos, led, beep, drop experimental status
49 * more capable led control on A21e, A22p, T20-22, X20
50 * experimental temperatures and fan speed
51 * experimental embedded controller register dump
52 * mark more functions as __init, drop incorrect __exit
53 * use MODULE_VERSION
54 * thanks to Henrik Brix Andersen <brix@gentoo.org>
55 * fix parameter passing on module loading
56 * thanks to Rusty Russell <rusty@rustcorp.com.au>
57 * thanks to Jim Radford <radford@blackbean.org>
58 * 2004-11-08 0.8 fix init error case, don't return from a macro
59 * thanks to Chris Wright <chrisw@osdl.org>
60 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
61 * fix led control on A21e
62 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
63 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
64 * proc file format changed
65 * video_switch command
66 * experimental cmos control
67 * experimental led control
68 * experimental acpi sounds
69 * 2004-09-16 0.4 support for module parameters
70 * hotkey mask can be prefixed by 0x
71 * video output switching
72 * video expansion control
73 * ultrabay eject support
74 * removed lcd brightness/on/off control, didn't work
75 * 2004-08-17 0.3 support for R40
76 * lcd off, brightness control
77 * thinklight on/off
78 * 2004-08-14 0.2 support for T series, X20
79 * bluetooth enable/disable
80 * hotkey events disabled by default
81 * removed fan control, currently useless
82 * 2004-08-09 0.1 initial release, support for X series
85 #include "thinkpad_acpi.h"
87 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
88 MODULE_DESCRIPTION(IBM_DESC);
89 MODULE_VERSION(IBM_VERSION);
90 MODULE_LICENSE("GPL");
92 /* Please remove this in year 2009 */
93 MODULE_ALIAS("ibm_acpi");
96 * DMI matching for module autoloading
98 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
99 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
101 * Only models listed in thinkwiki will be supported, so add yours
102 * if it is not there yet.
104 #define IBM_BIOS_MODULE_ALIAS(__type) \
105 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
107 /* Non-ancient thinkpads */
108 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
109 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
111 /* Ancient thinkpad BIOSes have to be identified by
112 * BIOS type or model number, and there are far less
113 * BIOS types than model numbers... */
114 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
115 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
116 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
118 #define __unused __attribute__ ((unused))
120 static enum {
121 TPACPI_LIFE_INIT = 0,
122 TPACPI_LIFE_RUNNING,
123 TPACPI_LIFE_EXITING,
124 } tpacpi_lifecycle;
126 /****************************************************************************
127 ****************************************************************************
129 * ACPI Helpers and device model
131 ****************************************************************************
132 ****************************************************************************/
134 /*************************************************************************
135 * ACPI basic handles
138 static acpi_handle root_handle;
140 #define IBM_HANDLE(object, parent, paths...) \
141 static acpi_handle object##_handle; \
142 static acpi_handle *object##_parent = &parent##_handle; \
143 static char *object##_path; \
144 static char *object##_paths[] = { paths }
146 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
147 "\\_SB.PCI.ISA.EC", /* 570 */
148 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
149 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
150 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
151 "\\_SB.PCI0.ICH3.EC0", /* R31 */
152 "\\_SB.PCI0.LPC.EC", /* all others */
155 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
156 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
159 /*************************************************************************
160 * Misc ACPI handles
163 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
164 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
165 "\\CMS", /* R40, R40e */
166 ); /* all others */
168 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
169 "^HKEY", /* R30, R31 */
170 "HKEY", /* all others */
171 ); /* 570 */
174 /*************************************************************************
175 * ACPI helpers
178 static int acpi_evalf(acpi_handle handle,
179 void *res, char *method, char *fmt, ...)
181 char *fmt0 = fmt;
182 struct acpi_object_list params;
183 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
184 struct acpi_buffer result, *resultp;
185 union acpi_object out_obj;
186 acpi_status status;
187 va_list ap;
188 char res_type;
189 int success;
190 int quiet;
192 if (!*fmt) {
193 printk(IBM_ERR "acpi_evalf() called with empty format\n");
194 return 0;
197 if (*fmt == 'q') {
198 quiet = 1;
199 fmt++;
200 } else
201 quiet = 0;
203 res_type = *(fmt++);
205 params.count = 0;
206 params.pointer = &in_objs[0];
208 va_start(ap, fmt);
209 while (*fmt) {
210 char c = *(fmt++);
211 switch (c) {
212 case 'd': /* int */
213 in_objs[params.count].integer.value = va_arg(ap, int);
214 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
215 break;
216 /* add more types as needed */
217 default:
218 printk(IBM_ERR "acpi_evalf() called "
219 "with invalid format character '%c'\n", c);
220 return 0;
223 va_end(ap);
225 if (res_type != 'v') {
226 result.length = sizeof(out_obj);
227 result.pointer = &out_obj;
228 resultp = &result;
229 } else
230 resultp = NULL;
232 status = acpi_evaluate_object(handle, method, &params, resultp);
234 switch (res_type) {
235 case 'd': /* int */
236 if (res)
237 *(int *)res = out_obj.integer.value;
238 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
239 break;
240 case 'v': /* void */
241 success = status == AE_OK;
242 break;
243 /* add more types as needed */
244 default:
245 printk(IBM_ERR "acpi_evalf() called "
246 "with invalid format character '%c'\n", res_type);
247 return 0;
250 if (!success && !quiet)
251 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
252 method, fmt0, status);
254 return success;
257 static void __unused acpi_print_int(acpi_handle handle, char *method)
259 int i;
261 if (acpi_evalf(handle, &i, method, "d"))
262 printk(IBM_INFO "%s = 0x%x\n", method, i);
263 else
264 printk(IBM_ERR "error calling %s\n", method);
267 static int acpi_ec_read(int i, u8 * p)
269 int v;
271 if (ecrd_handle) {
272 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
273 return 0;
274 *p = v;
275 } else {
276 if (ec_read(i, p) < 0)
277 return 0;
280 return 1;
283 static int acpi_ec_write(int i, u8 v)
285 if (ecwr_handle) {
286 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
287 return 0;
288 } else {
289 if (ec_write(i, v) < 0)
290 return 0;
293 return 1;
296 static int _sta(acpi_handle handle)
298 int status;
300 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
301 status = 0;
303 return status;
306 static int issue_thinkpad_cmos_command(int cmos_cmd)
308 if (!cmos_handle)
309 return -ENXIO;
311 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
312 return -EIO;
314 return 0;
317 /*************************************************************************
318 * ACPI device model
321 static void drv_acpi_handle_init(char *name,
322 acpi_handle *handle, acpi_handle parent,
323 char **paths, int num_paths, char **path)
325 int i;
326 acpi_status status;
328 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
329 name);
331 for (i = 0; i < num_paths; i++) {
332 status = acpi_get_handle(parent, paths[i], handle);
333 if (ACPI_SUCCESS(status)) {
334 *path = paths[i];
335 dbg_printk(TPACPI_DBG_INIT,
336 "Found ACPI handle %s for %s\n",
337 *path, name);
338 return;
342 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
343 name);
344 *handle = NULL;
347 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
349 struct ibm_struct *ibm = data;
351 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
352 return;
354 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
355 return;
357 ibm->acpi->notify(ibm, event);
360 static int __init setup_acpi_notify(struct ibm_struct *ibm)
362 acpi_status status;
363 int rc;
365 BUG_ON(!ibm->acpi);
367 if (!*ibm->acpi->handle)
368 return 0;
370 vdbg_printk(TPACPI_DBG_INIT,
371 "setting up ACPI notify for %s\n", ibm->name);
373 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
374 if (rc < 0) {
375 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
376 ibm->name, rc);
377 return -ENODEV;
380 acpi_driver_data(ibm->acpi->device) = ibm;
381 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
382 IBM_ACPI_EVENT_PREFIX,
383 ibm->name);
385 status = acpi_install_notify_handler(*ibm->acpi->handle,
386 ibm->acpi->type, dispatch_acpi_notify, ibm);
387 if (ACPI_FAILURE(status)) {
388 if (status == AE_ALREADY_EXISTS) {
389 printk(IBM_NOTICE "another device driver is already handling %s events\n",
390 ibm->name);
391 } else {
392 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
393 ibm->name, status);
395 return -ENODEV;
397 ibm->flags.acpi_notify_installed = 1;
398 return 0;
401 static int __init tpacpi_device_add(struct acpi_device *device)
403 return 0;
406 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
408 int rc;
410 dbg_printk(TPACPI_DBG_INIT,
411 "registering %s as an ACPI driver\n", ibm->name);
413 BUG_ON(!ibm->acpi);
415 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
416 if (!ibm->acpi->driver) {
417 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
418 return -ENOMEM;
421 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
422 ibm->acpi->driver->ids = ibm->acpi->hid;
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 static int hotkey_orig_status;
767 static u32 hotkey_orig_mask;
768 static u32 hotkey_all_mask;
769 static u32 hotkey_reserved_mask;
771 static u16 *hotkey_keycode_map;
773 static struct attribute_set *hotkey_dev_attributes;
775 static int hotkey_get_wlsw(int *status)
777 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
778 return -EIO;
779 return 0;
782 /* sysfs hotkey enable ------------------------------------------------- */
783 static ssize_t hotkey_enable_show(struct device *dev,
784 struct device_attribute *attr,
785 char *buf)
787 int res, status;
788 u32 mask;
790 res = hotkey_get(&status, &mask);
791 if (res)
792 return res;
794 return snprintf(buf, PAGE_SIZE, "%d\n", status);
797 static ssize_t hotkey_enable_store(struct device *dev,
798 struct device_attribute *attr,
799 const char *buf, size_t count)
801 unsigned long t;
802 int res, status;
803 u32 mask;
805 if (parse_strtoul(buf, 1, &t))
806 return -EINVAL;
808 res = hotkey_get(&status, &mask);
809 if (!res)
810 res = hotkey_set(t, mask);
812 return (res) ? res : count;
815 static struct device_attribute dev_attr_hotkey_enable =
816 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
817 hotkey_enable_show, hotkey_enable_store);
819 /* sysfs hotkey mask --------------------------------------------------- */
820 static ssize_t hotkey_mask_show(struct device *dev,
821 struct device_attribute *attr,
822 char *buf)
824 int res, status;
825 u32 mask;
827 res = hotkey_get(&status, &mask);
828 if (res)
829 return res;
831 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
834 static ssize_t hotkey_mask_store(struct device *dev,
835 struct device_attribute *attr,
836 const char *buf, size_t count)
838 unsigned long t;
839 int res, status;
840 u32 mask;
842 if (parse_strtoul(buf, 0xffffffffUL, &t))
843 return -EINVAL;
845 res = hotkey_get(&status, &mask);
846 if (!res)
847 hotkey_set(status, t);
849 return (res) ? res : count;
852 static struct device_attribute dev_attr_hotkey_mask =
853 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
854 hotkey_mask_show, hotkey_mask_store);
856 /* sysfs hotkey bios_enabled ------------------------------------------- */
857 static ssize_t hotkey_bios_enabled_show(struct device *dev,
858 struct device_attribute *attr,
859 char *buf)
861 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
864 static struct device_attribute dev_attr_hotkey_bios_enabled =
865 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
867 /* sysfs hotkey bios_mask ---------------------------------------------- */
868 static ssize_t hotkey_bios_mask_show(struct device *dev,
869 struct device_attribute *attr,
870 char *buf)
872 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
875 static struct device_attribute dev_attr_hotkey_bios_mask =
876 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
878 /* sysfs hotkey all_mask ----------------------------------------------- */
879 static ssize_t hotkey_all_mask_show(struct device *dev,
880 struct device_attribute *attr,
881 char *buf)
883 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
886 static struct device_attribute dev_attr_hotkey_all_mask =
887 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
889 /* sysfs hotkey recommended_mask --------------------------------------- */
890 static ssize_t hotkey_recommended_mask_show(struct device *dev,
891 struct device_attribute *attr,
892 char *buf)
894 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
895 hotkey_all_mask & ~hotkey_reserved_mask);
898 static struct device_attribute dev_attr_hotkey_recommended_mask =
899 __ATTR(hotkey_recommended_mask, S_IRUGO,
900 hotkey_recommended_mask_show, NULL);
902 /* sysfs hotkey radio_sw ----------------------------------------------- */
903 static ssize_t hotkey_radio_sw_show(struct device *dev,
904 struct device_attribute *attr,
905 char *buf)
907 int res, s;
908 res = hotkey_get_wlsw(&s);
909 if (res < 0)
910 return res;
912 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
915 static struct device_attribute dev_attr_hotkey_radio_sw =
916 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
918 /* sysfs hotkey report_mode -------------------------------------------- */
919 static ssize_t hotkey_report_mode_show(struct device *dev,
920 struct device_attribute *attr,
921 char *buf)
923 return snprintf(buf, PAGE_SIZE, "%d\n",
924 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
927 static ssize_t hotkey_report_mode_store(struct device *dev,
928 struct device_attribute *attr,
929 const char *buf, size_t count)
931 unsigned long t;
932 int res = 0;
934 if (parse_strtoul(buf, 2, &t) || (t < 1))
935 return -EINVAL;
937 if (t != hotkey_report_mode) {
938 if (tp_features.hotkey_report_mode_locked)
939 return -EPERM;
940 hotkey_report_mode = t;
941 printk(IBM_NOTICE "hot key report mode set to %d\n",
942 hotkey_report_mode);
945 return (res) ? res : count;
948 static struct device_attribute dev_attr_hotkey_report_mode =
949 __ATTR(hotkey_report_mode, S_IWUSR | S_IRUGO,
950 hotkey_report_mode_show, hotkey_report_mode_store);
952 /* --------------------------------------------------------------------- */
954 static struct attribute *hotkey_attributes[] __initdata = {
955 &dev_attr_hotkey_enable.attr,
956 &dev_attr_hotkey_report_mode.attr,
959 static struct attribute *hotkey_mask_attributes[] __initdata = {
960 &dev_attr_hotkey_mask.attr,
961 &dev_attr_hotkey_bios_enabled.attr,
962 &dev_attr_hotkey_bios_mask.attr,
963 &dev_attr_hotkey_all_mask.attr,
964 &dev_attr_hotkey_recommended_mask.attr,
967 static int __init hotkey_init(struct ibm_init_struct *iibm)
970 static u16 ibm_keycode_map[] __initdata = {
971 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
972 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
973 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
974 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
975 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
976 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
977 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
978 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
979 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
980 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
981 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
982 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
983 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
984 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
985 KEY_RESERVED, /* 0x14: VOLUME UP */
986 KEY_RESERVED, /* 0x15: VOLUME DOWN */
987 KEY_RESERVED, /* 0x16: MUTE */
988 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
989 /* (assignments unknown, please report if found) */
990 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
991 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
993 static u16 lenovo_keycode_map[] __initdata = {
994 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
995 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
996 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
997 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
998 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
999 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1000 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1001 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1002 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
1003 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
1004 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
1005 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1006 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1007 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1008 KEY_RESERVED, /* 0x14: VOLUME UP */
1009 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1010 KEY_RESERVED, /* 0x16: MUTE */
1011 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1012 /* (assignments unknown, please report if found) */
1013 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1014 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1017 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1018 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1019 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1021 int res, i;
1022 int status;
1023 int hkeyv;
1025 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1027 BUG_ON(!tpacpi_inputdev);
1029 IBM_ACPIHANDLE_INIT(hkey);
1030 mutex_init(&hotkey_mutex);
1032 /* hotkey not supported on 570 */
1033 tp_features.hotkey = hkey_handle != NULL;
1035 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1036 str_supported(tp_features.hotkey));
1038 if (tp_features.hotkey) {
1039 hotkey_dev_attributes = create_attr_set(8, NULL);
1040 if (!hotkey_dev_attributes)
1041 return -ENOMEM;
1042 res = add_many_to_attr_set(hotkey_dev_attributes,
1043 hotkey_attributes,
1044 ARRAY_SIZE(hotkey_attributes));
1045 if (res)
1046 return res;
1048 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1049 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1050 for HKEY interface version 0x100 */
1051 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1052 if ((hkeyv >> 8) != 1) {
1053 printk(IBM_ERR "unknown version of the "
1054 "HKEY interface: 0x%x\n", hkeyv);
1055 printk(IBM_ERR "please report this to %s\n",
1056 IBM_MAIL);
1057 } else {
1059 * MHKV 0x100 in A31, R40, R40e,
1060 * T4x, X31, and later
1061 * */
1062 tp_features.hotkey_mask = 1;
1066 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1067 str_supported(tp_features.hotkey_mask));
1069 if (tp_features.hotkey_mask) {
1070 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1071 "MHKA", "qd")) {
1072 printk(IBM_ERR
1073 "missing MHKA handler, "
1074 "please report this to %s\n",
1075 IBM_MAIL);
1076 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1080 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
1081 if (!res && tp_features.hotkey_mask) {
1082 res = add_many_to_attr_set(hotkey_dev_attributes,
1083 hotkey_mask_attributes,
1084 ARRAY_SIZE(hotkey_mask_attributes));
1087 /* Not all thinkpads have a hardware radio switch */
1088 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1089 tp_features.hotkey_wlsw = 1;
1090 printk(IBM_INFO
1091 "radio switch found; radios are %s\n",
1092 enabled(status, 0));
1093 res = add_to_attr_set(hotkey_dev_attributes,
1094 &dev_attr_hotkey_radio_sw.attr);
1097 if (!res)
1098 res = register_attr_set_with_sysfs(
1099 hotkey_dev_attributes,
1100 &tpacpi_pdev->dev.kobj);
1101 if (res)
1102 return res;
1104 /* Set up key map */
1106 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1107 GFP_KERNEL);
1108 if (!hotkey_keycode_map) {
1109 printk(IBM_ERR "failed to allocate memory for key map\n");
1110 return -ENOMEM;
1113 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1114 dbg_printk(TPACPI_DBG_INIT,
1115 "using Lenovo default hot key map\n");
1116 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1117 TPACPI_HOTKEY_MAP_SIZE);
1118 } else {
1119 dbg_printk(TPACPI_DBG_INIT,
1120 "using IBM default hot key map\n");
1121 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1122 TPACPI_HOTKEY_MAP_SIZE);
1125 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1126 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1127 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1128 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1129 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1130 tpacpi_inputdev->keycode = hotkey_keycode_map;
1131 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1132 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1133 set_bit(hotkey_keycode_map[i],
1134 tpacpi_inputdev->keybit);
1135 } else {
1136 if (i < sizeof(hotkey_reserved_mask)*8)
1137 hotkey_reserved_mask |= 1 << i;
1141 if (tp_features.hotkey_wlsw) {
1142 set_bit(EV_SW, tpacpi_inputdev->evbit);
1143 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1146 dbg_printk(TPACPI_DBG_INIT,
1147 "enabling hot key handling\n");
1148 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1149 | hotkey_orig_mask);
1150 if (res)
1151 return res;
1153 if (hotkey_report_mode > 0) {
1154 tp_features.hotkey_report_mode_locked = 1;
1155 } else {
1156 hotkey_report_mode = 1;
1157 vdbg_printk(TPACPI_DBG_INIT,
1158 "hot key reporting mode can be "
1159 "changed at runtime\n");
1162 dbg_printk(TPACPI_DBG_INIT,
1163 "legacy hot key reporting over procfs %s\n",
1164 (hotkey_report_mode < 2) ?
1165 "enabled" : "disabled");
1168 return (tp_features.hotkey)? 0 : 1;
1171 static void hotkey_exit(void)
1173 int res;
1175 if (tp_features.hotkey) {
1176 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
1177 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1178 if (res)
1179 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
1182 if (hotkey_dev_attributes) {
1183 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1184 hotkey_dev_attributes = NULL;
1188 static void tpacpi_input_send_key(unsigned int scancode,
1189 unsigned int keycode)
1191 if (keycode != KEY_RESERVED) {
1192 mutex_lock(&tpacpi_inputdev_send_mutex);
1194 input_report_key(tpacpi_inputdev, keycode, 1);
1195 if (keycode == KEY_UNKNOWN)
1196 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1197 scancode);
1198 input_sync(tpacpi_inputdev);
1200 input_report_key(tpacpi_inputdev, keycode, 0);
1201 if (keycode == KEY_UNKNOWN)
1202 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1203 scancode);
1204 input_sync(tpacpi_inputdev);
1206 mutex_unlock(&tpacpi_inputdev_send_mutex);
1210 static void tpacpi_input_send_radiosw(void)
1212 int wlsw;
1214 mutex_lock(&tpacpi_inputdev_send_mutex);
1216 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1217 input_report_switch(tpacpi_inputdev,
1218 SW_RADIO, !!wlsw);
1219 input_sync(tpacpi_inputdev);
1222 mutex_unlock(&tpacpi_inputdev_send_mutex);
1225 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1227 u32 hkey;
1228 unsigned int keycode, scancode;
1229 int send_acpi_ev = 0;
1231 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1232 switch (hkey >> 12) {
1233 case 1:
1234 /* 0x1000-0x1FFF: key presses */
1235 scancode = hkey & 0xfff;
1236 if (scancode > 0 && scancode < 0x21) {
1237 scancode--;
1238 keycode = hotkey_keycode_map[scancode];
1239 tpacpi_input_send_key(scancode, keycode);
1240 } else {
1241 printk(IBM_ERR
1242 "hotkey 0x%04x out of range for keyboard map\n",
1243 hkey);
1244 send_acpi_ev = 1;
1246 break;
1247 case 5:
1248 /* 0x5000-0x5FFF: LID */
1249 /* we don't handle it through this path, just
1250 * eat up known LID events */
1251 if (hkey != 0x5001 && hkey != 0x5002) {
1252 printk(IBM_ERR
1253 "unknown LID-related hotkey event: 0x%04x\n",
1254 hkey);
1255 send_acpi_ev = 1;
1257 break;
1258 case 7:
1259 /* 0x7000-0x7FFF: misc */
1260 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1261 tpacpi_input_send_radiosw();
1262 break;
1264 /* fallthrough to default */
1265 default:
1266 /* case 2: dock-related */
1267 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1268 /* case 3: ultra-bay related. maybe bay in dock? */
1269 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1270 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1271 send_acpi_ev = 1;
1273 } else {
1274 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
1275 hkey = 0;
1276 send_acpi_ev = 1;
1279 if (send_acpi_ev || hotkey_report_mode < 2) {
1280 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
1284 static void hotkey_resume(void)
1286 tpacpi_input_send_radiosw();
1290 * Call with hotkey_mutex held
1292 static int hotkey_get(int *status, u32 *mask)
1294 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1295 return -EIO;
1297 if (tp_features.hotkey_mask)
1298 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
1299 return -EIO;
1301 return 0;
1305 * Call with hotkey_mutex held
1307 static int hotkey_set(int status, u32 mask)
1309 int i;
1311 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1312 return -EIO;
1314 if (tp_features.hotkey_mask)
1315 for (i = 0; i < 32; i++) {
1316 int bit = ((1 << i) & mask) != 0;
1317 if (!acpi_evalf(hkey_handle,
1318 NULL, "MHKM", "vdd", i + 1, bit))
1319 return -EIO;
1322 return 0;
1325 /* procfs -------------------------------------------------------------- */
1326 static int hotkey_read(char *p)
1328 int res, status;
1329 u32 mask;
1330 int len = 0;
1332 if (!tp_features.hotkey) {
1333 len += sprintf(p + len, "status:\t\tnot supported\n");
1334 return len;
1337 res = mutex_lock_interruptible(&hotkey_mutex);
1338 if (res < 0)
1339 return res;
1340 res = hotkey_get(&status, &mask);
1341 mutex_unlock(&hotkey_mutex);
1342 if (res)
1343 return res;
1345 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1346 if (tp_features.hotkey_mask) {
1347 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1348 len += sprintf(p + len,
1349 "commands:\tenable, disable, reset, <mask>\n");
1350 } else {
1351 len += sprintf(p + len, "mask:\t\tnot supported\n");
1352 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1355 return len;
1358 static int hotkey_write(char *buf)
1360 int res, status;
1361 u32 mask;
1362 char *cmd;
1363 int do_cmd = 0;
1365 if (!tp_features.hotkey)
1366 return -ENODEV;
1368 res = mutex_lock_interruptible(&hotkey_mutex);
1369 if (res < 0)
1370 return res;
1372 res = hotkey_get(&status, &mask);
1373 if (res)
1374 goto errexit;
1376 res = 0;
1377 while ((cmd = next_cmd(&buf))) {
1378 if (strlencmp(cmd, "enable") == 0) {
1379 status = 1;
1380 } else if (strlencmp(cmd, "disable") == 0) {
1381 status = 0;
1382 } else if (strlencmp(cmd, "reset") == 0) {
1383 status = hotkey_orig_status;
1384 mask = hotkey_orig_mask;
1385 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1386 /* mask set */
1387 } else if (sscanf(cmd, "%x", &mask) == 1) {
1388 /* mask set */
1389 } else {
1390 res = -EINVAL;
1391 goto errexit;
1393 do_cmd = 1;
1396 if (do_cmd)
1397 res = hotkey_set(status, mask);
1399 errexit:
1400 mutex_unlock(&hotkey_mutex);
1401 return res;
1404 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1405 .hid = IBM_HKEY_HID,
1406 .notify = hotkey_notify,
1407 .handle = &hkey_handle,
1408 .type = ACPI_DEVICE_NOTIFY,
1411 static struct ibm_struct hotkey_driver_data = {
1412 .name = "hotkey",
1413 .read = hotkey_read,
1414 .write = hotkey_write,
1415 .exit = hotkey_exit,
1416 .resume = hotkey_resume,
1417 .acpi = &ibm_hotkey_acpidriver,
1420 /*************************************************************************
1421 * Bluetooth subdriver
1424 /* sysfs bluetooth enable ---------------------------------------------- */
1425 static ssize_t bluetooth_enable_show(struct device *dev,
1426 struct device_attribute *attr,
1427 char *buf)
1429 int status;
1431 status = bluetooth_get_radiosw();
1432 if (status < 0)
1433 return status;
1435 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1438 static ssize_t bluetooth_enable_store(struct device *dev,
1439 struct device_attribute *attr,
1440 const char *buf, size_t count)
1442 unsigned long t;
1443 int res;
1445 if (parse_strtoul(buf, 1, &t))
1446 return -EINVAL;
1448 res = bluetooth_set_radiosw(t);
1450 return (res) ? res : count;
1453 static struct device_attribute dev_attr_bluetooth_enable =
1454 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1455 bluetooth_enable_show, bluetooth_enable_store);
1457 /* --------------------------------------------------------------------- */
1459 static struct attribute *bluetooth_attributes[] = {
1460 &dev_attr_bluetooth_enable.attr,
1461 NULL
1464 static const struct attribute_group bluetooth_attr_group = {
1465 .attrs = bluetooth_attributes,
1468 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1470 int res;
1471 int status = 0;
1473 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1475 IBM_ACPIHANDLE_INIT(hkey);
1477 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1478 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1479 tp_features.bluetooth = hkey_handle &&
1480 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1482 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1483 str_supported(tp_features.bluetooth),
1484 status);
1486 if (tp_features.bluetooth) {
1487 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1488 /* no bluetooth hardware present in system */
1489 tp_features.bluetooth = 0;
1490 dbg_printk(TPACPI_DBG_INIT,
1491 "bluetooth hardware not installed\n");
1492 } else {
1493 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1494 &bluetooth_attr_group);
1495 if (res)
1496 return res;
1500 return (tp_features.bluetooth)? 0 : 1;
1503 static void bluetooth_exit(void)
1505 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1506 &bluetooth_attr_group);
1509 static int bluetooth_get_radiosw(void)
1511 int status;
1513 if (!tp_features.bluetooth)
1514 return -ENODEV;
1516 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1517 return -EIO;
1519 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1522 static int bluetooth_set_radiosw(int radio_on)
1524 int status;
1526 if (!tp_features.bluetooth)
1527 return -ENODEV;
1529 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1530 return -EIO;
1531 if (radio_on)
1532 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1533 else
1534 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1535 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1536 return -EIO;
1538 return 0;
1541 /* procfs -------------------------------------------------------------- */
1542 static int bluetooth_read(char *p)
1544 int len = 0;
1545 int status = bluetooth_get_radiosw();
1547 if (!tp_features.bluetooth)
1548 len += sprintf(p + len, "status:\t\tnot supported\n");
1549 else {
1550 len += sprintf(p + len, "status:\t\t%s\n",
1551 (status)? "enabled" : "disabled");
1552 len += sprintf(p + len, "commands:\tenable, disable\n");
1555 return len;
1558 static int bluetooth_write(char *buf)
1560 char *cmd;
1562 if (!tp_features.bluetooth)
1563 return -ENODEV;
1565 while ((cmd = next_cmd(&buf))) {
1566 if (strlencmp(cmd, "enable") == 0) {
1567 bluetooth_set_radiosw(1);
1568 } else if (strlencmp(cmd, "disable") == 0) {
1569 bluetooth_set_radiosw(0);
1570 } else
1571 return -EINVAL;
1574 return 0;
1577 static struct ibm_struct bluetooth_driver_data = {
1578 .name = "bluetooth",
1579 .read = bluetooth_read,
1580 .write = bluetooth_write,
1581 .exit = bluetooth_exit,
1584 /*************************************************************************
1585 * Wan subdriver
1588 /* sysfs wan enable ---------------------------------------------------- */
1589 static ssize_t wan_enable_show(struct device *dev,
1590 struct device_attribute *attr,
1591 char *buf)
1593 int status;
1595 status = wan_get_radiosw();
1596 if (status < 0)
1597 return status;
1599 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1602 static ssize_t wan_enable_store(struct device *dev,
1603 struct device_attribute *attr,
1604 const char *buf, size_t count)
1606 unsigned long t;
1607 int res;
1609 if (parse_strtoul(buf, 1, &t))
1610 return -EINVAL;
1612 res = wan_set_radiosw(t);
1614 return (res) ? res : count;
1617 static struct device_attribute dev_attr_wan_enable =
1618 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1619 wan_enable_show, wan_enable_store);
1621 /* --------------------------------------------------------------------- */
1623 static struct attribute *wan_attributes[] = {
1624 &dev_attr_wan_enable.attr,
1625 NULL
1628 static const struct attribute_group wan_attr_group = {
1629 .attrs = wan_attributes,
1632 static int __init wan_init(struct ibm_init_struct *iibm)
1634 int res;
1635 int status = 0;
1637 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1639 IBM_ACPIHANDLE_INIT(hkey);
1641 tp_features.wan = hkey_handle &&
1642 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1644 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1645 str_supported(tp_features.wan),
1646 status);
1648 if (tp_features.wan) {
1649 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1650 /* no wan hardware present in system */
1651 tp_features.wan = 0;
1652 dbg_printk(TPACPI_DBG_INIT,
1653 "wan hardware not installed\n");
1654 } else {
1655 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1656 &wan_attr_group);
1657 if (res)
1658 return res;
1662 return (tp_features.wan)? 0 : 1;
1665 static void wan_exit(void)
1667 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1668 &wan_attr_group);
1671 static int wan_get_radiosw(void)
1673 int status;
1675 if (!tp_features.wan)
1676 return -ENODEV;
1678 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1679 return -EIO;
1681 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1684 static int wan_set_radiosw(int radio_on)
1686 int status;
1688 if (!tp_features.wan)
1689 return -ENODEV;
1691 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1692 return -EIO;
1693 if (radio_on)
1694 status |= TP_ACPI_WANCARD_RADIOSSW;
1695 else
1696 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1697 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1698 return -EIO;
1700 return 0;
1703 /* procfs -------------------------------------------------------------- */
1704 static int wan_read(char *p)
1706 int len = 0;
1707 int status = wan_get_radiosw();
1709 if (!tp_features.wan)
1710 len += sprintf(p + len, "status:\t\tnot supported\n");
1711 else {
1712 len += sprintf(p + len, "status:\t\t%s\n",
1713 (status)? "enabled" : "disabled");
1714 len += sprintf(p + len, "commands:\tenable, disable\n");
1717 return len;
1720 static int wan_write(char *buf)
1722 char *cmd;
1724 if (!tp_features.wan)
1725 return -ENODEV;
1727 while ((cmd = next_cmd(&buf))) {
1728 if (strlencmp(cmd, "enable") == 0) {
1729 wan_set_radiosw(1);
1730 } else if (strlencmp(cmd, "disable") == 0) {
1731 wan_set_radiosw(0);
1732 } else
1733 return -EINVAL;
1736 return 0;
1739 static struct ibm_struct wan_driver_data = {
1740 .name = "wan",
1741 .read = wan_read,
1742 .write = wan_write,
1743 .exit = wan_exit,
1744 .flags.experimental = 1,
1747 /*************************************************************************
1748 * Video subdriver
1751 static enum video_access_mode video_supported;
1752 static int video_orig_autosw;
1754 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1755 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1756 "\\_SB.PCI0.VID0", /* 770e */
1757 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1758 "\\_SB.PCI0.AGP.VID", /* all others */
1759 ); /* R30, R31 */
1761 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1763 static int __init video_init(struct ibm_init_struct *iibm)
1765 int ivga;
1767 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1769 IBM_ACPIHANDLE_INIT(vid);
1770 IBM_ACPIHANDLE_INIT(vid2);
1772 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1773 /* G41, assume IVGA doesn't change */
1774 vid_handle = vid2_handle;
1776 if (!vid_handle)
1777 /* video switching not supported on R30, R31 */
1778 video_supported = TPACPI_VIDEO_NONE;
1779 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1780 /* 570 */
1781 video_supported = TPACPI_VIDEO_570;
1782 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1783 /* 600e/x, 770e, 770x */
1784 video_supported = TPACPI_VIDEO_770;
1785 else
1786 /* all others */
1787 video_supported = TPACPI_VIDEO_NEW;
1789 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1790 str_supported(video_supported != TPACPI_VIDEO_NONE),
1791 video_supported);
1793 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1796 static void video_exit(void)
1798 dbg_printk(TPACPI_DBG_EXIT,
1799 "restoring original video autoswitch mode\n");
1800 if (video_autosw_set(video_orig_autosw))
1801 printk(IBM_ERR "error while trying to restore original "
1802 "video autoswitch mode\n");
1805 static int video_outputsw_get(void)
1807 int status = 0;
1808 int i;
1810 switch (video_supported) {
1811 case TPACPI_VIDEO_570:
1812 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1813 TP_ACPI_VIDEO_570_PHSCMD))
1814 return -EIO;
1815 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1816 break;
1817 case TPACPI_VIDEO_770:
1818 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1819 return -EIO;
1820 if (i)
1821 status |= TP_ACPI_VIDEO_S_LCD;
1822 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1823 return -EIO;
1824 if (i)
1825 status |= TP_ACPI_VIDEO_S_CRT;
1826 break;
1827 case TPACPI_VIDEO_NEW:
1828 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1829 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1830 return -EIO;
1831 if (i)
1832 status |= TP_ACPI_VIDEO_S_CRT;
1834 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1835 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1836 return -EIO;
1837 if (i)
1838 status |= TP_ACPI_VIDEO_S_LCD;
1839 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1840 return -EIO;
1841 if (i)
1842 status |= TP_ACPI_VIDEO_S_DVI;
1843 break;
1844 default:
1845 return -ENOSYS;
1848 return status;
1851 static int video_outputsw_set(int status)
1853 int autosw;
1854 int res = 0;
1856 switch (video_supported) {
1857 case TPACPI_VIDEO_570:
1858 res = acpi_evalf(NULL, NULL,
1859 "\\_SB.PHS2", "vdd",
1860 TP_ACPI_VIDEO_570_PHS2CMD,
1861 status | TP_ACPI_VIDEO_570_PHS2SET);
1862 break;
1863 case TPACPI_VIDEO_770:
1864 autosw = video_autosw_get();
1865 if (autosw < 0)
1866 return autosw;
1868 res = video_autosw_set(1);
1869 if (res)
1870 return res;
1871 res = acpi_evalf(vid_handle, NULL,
1872 "ASWT", "vdd", status * 0x100, 0);
1873 if (!autosw && video_autosw_set(autosw)) {
1874 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1875 return -EIO;
1877 break;
1878 case TPACPI_VIDEO_NEW:
1879 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1880 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1881 break;
1882 default:
1883 return -ENOSYS;
1886 return (res)? 0 : -EIO;
1889 static int video_autosw_get(void)
1891 int autosw = 0;
1893 switch (video_supported) {
1894 case TPACPI_VIDEO_570:
1895 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1896 return -EIO;
1897 break;
1898 case TPACPI_VIDEO_770:
1899 case TPACPI_VIDEO_NEW:
1900 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1901 return -EIO;
1902 break;
1903 default:
1904 return -ENOSYS;
1907 return autosw & 1;
1910 static int video_autosw_set(int enable)
1912 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1913 return -EIO;
1914 return 0;
1917 static int video_outputsw_cycle(void)
1919 int autosw = video_autosw_get();
1920 int res;
1922 if (autosw < 0)
1923 return autosw;
1925 switch (video_supported) {
1926 case TPACPI_VIDEO_570:
1927 res = video_autosw_set(1);
1928 if (res)
1929 return res;
1930 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1931 break;
1932 case TPACPI_VIDEO_770:
1933 case TPACPI_VIDEO_NEW:
1934 res = video_autosw_set(1);
1935 if (res)
1936 return res;
1937 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1938 break;
1939 default:
1940 return -ENOSYS;
1942 if (!autosw && video_autosw_set(autosw)) {
1943 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1944 return -EIO;
1947 return (res)? 0 : -EIO;
1950 static int video_expand_toggle(void)
1952 switch (video_supported) {
1953 case TPACPI_VIDEO_570:
1954 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1955 0 : -EIO;
1956 case TPACPI_VIDEO_770:
1957 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1958 0 : -EIO;
1959 case TPACPI_VIDEO_NEW:
1960 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1961 0 : -EIO;
1962 default:
1963 return -ENOSYS;
1965 /* not reached */
1968 static int video_read(char *p)
1970 int status, autosw;
1971 int len = 0;
1973 if (video_supported == TPACPI_VIDEO_NONE) {
1974 len += sprintf(p + len, "status:\t\tnot supported\n");
1975 return len;
1978 status = video_outputsw_get();
1979 if (status < 0)
1980 return status;
1982 autosw = video_autosw_get();
1983 if (autosw < 0)
1984 return autosw;
1986 len += sprintf(p + len, "status:\t\tsupported\n");
1987 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1988 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1989 if (video_supported == TPACPI_VIDEO_NEW)
1990 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1991 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1992 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1993 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1994 if (video_supported == TPACPI_VIDEO_NEW)
1995 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1996 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1997 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1999 return len;
2002 static int video_write(char *buf)
2004 char *cmd;
2005 int enable, disable, status;
2006 int res;
2008 if (video_supported == TPACPI_VIDEO_NONE)
2009 return -ENODEV;
2011 enable = 0;
2012 disable = 0;
2014 while ((cmd = next_cmd(&buf))) {
2015 if (strlencmp(cmd, "lcd_enable") == 0) {
2016 enable |= TP_ACPI_VIDEO_S_LCD;
2017 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2018 disable |= TP_ACPI_VIDEO_S_LCD;
2019 } else if (strlencmp(cmd, "crt_enable") == 0) {
2020 enable |= TP_ACPI_VIDEO_S_CRT;
2021 } else if (strlencmp(cmd, "crt_disable") == 0) {
2022 disable |= TP_ACPI_VIDEO_S_CRT;
2023 } else if (video_supported == TPACPI_VIDEO_NEW &&
2024 strlencmp(cmd, "dvi_enable") == 0) {
2025 enable |= TP_ACPI_VIDEO_S_DVI;
2026 } else if (video_supported == TPACPI_VIDEO_NEW &&
2027 strlencmp(cmd, "dvi_disable") == 0) {
2028 disable |= TP_ACPI_VIDEO_S_DVI;
2029 } else if (strlencmp(cmd, "auto_enable") == 0) {
2030 res = video_autosw_set(1);
2031 if (res)
2032 return res;
2033 } else if (strlencmp(cmd, "auto_disable") == 0) {
2034 res = video_autosw_set(0);
2035 if (res)
2036 return res;
2037 } else if (strlencmp(cmd, "video_switch") == 0) {
2038 res = video_outputsw_cycle();
2039 if (res)
2040 return res;
2041 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2042 res = video_expand_toggle();
2043 if (res)
2044 return res;
2045 } else
2046 return -EINVAL;
2049 if (enable || disable) {
2050 status = video_outputsw_get();
2051 if (status < 0)
2052 return status;
2053 res = video_outputsw_set((status & ~disable) | enable);
2054 if (res)
2055 return res;
2058 return 0;
2061 static struct ibm_struct video_driver_data = {
2062 .name = "video",
2063 .read = video_read,
2064 .write = video_write,
2065 .exit = video_exit,
2068 /*************************************************************************
2069 * Light (thinklight) subdriver
2072 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2073 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2075 static int __init light_init(struct ibm_init_struct *iibm)
2077 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2079 IBM_ACPIHANDLE_INIT(ledb);
2080 IBM_ACPIHANDLE_INIT(lght);
2081 IBM_ACPIHANDLE_INIT(cmos);
2083 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2084 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2086 if (tp_features.light)
2087 /* light status not supported on
2088 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2089 tp_features.light_status =
2090 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2092 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2093 str_supported(tp_features.light));
2095 return (tp_features.light)? 0 : 1;
2098 static int light_read(char *p)
2100 int len = 0;
2101 int status = 0;
2103 if (!tp_features.light) {
2104 len += sprintf(p + len, "status:\t\tnot supported\n");
2105 } else if (!tp_features.light_status) {
2106 len += sprintf(p + len, "status:\t\tunknown\n");
2107 len += sprintf(p + len, "commands:\ton, off\n");
2108 } else {
2109 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2110 return -EIO;
2111 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2112 len += sprintf(p + len, "commands:\ton, off\n");
2115 return len;
2118 static int light_write(char *buf)
2120 int cmos_cmd, lght_cmd;
2121 char *cmd;
2122 int success;
2124 if (!tp_features.light)
2125 return -ENODEV;
2127 while ((cmd = next_cmd(&buf))) {
2128 if (strlencmp(cmd, "on") == 0) {
2129 cmos_cmd = 0x0c;
2130 lght_cmd = 1;
2131 } else if (strlencmp(cmd, "off") == 0) {
2132 cmos_cmd = 0x0d;
2133 lght_cmd = 0;
2134 } else
2135 return -EINVAL;
2137 success = cmos_handle ?
2138 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2139 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2140 if (!success)
2141 return -EIO;
2144 return 0;
2147 static struct ibm_struct light_driver_data = {
2148 .name = "light",
2149 .read = light_read,
2150 .write = light_write,
2153 /*************************************************************************
2154 * Dock subdriver
2157 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2159 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2160 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2161 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2162 "\\_SB.PCI.ISA.SLCE", /* 570 */
2163 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2165 /* don't list other alternatives as we install a notify handler on the 570 */
2166 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2168 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2170 .notify = dock_notify,
2171 .handle = &dock_handle,
2172 .type = ACPI_SYSTEM_NOTIFY,
2175 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2176 * We just use it to get notifications of dock hotplug
2177 * in very old thinkpads */
2178 .hid = PCI_ROOT_HID_STRING,
2179 .notify = dock_notify,
2180 .handle = &pci_handle,
2181 .type = ACPI_SYSTEM_NOTIFY,
2185 static struct ibm_struct dock_driver_data[2] = {
2187 .name = "dock",
2188 .read = dock_read,
2189 .write = dock_write,
2190 .acpi = &ibm_dock_acpidriver[0],
2193 .name = "dock",
2194 .acpi = &ibm_dock_acpidriver[1],
2198 #define dock_docked() (_sta(dock_handle) & 1)
2200 static int __init dock_init(struct ibm_init_struct *iibm)
2202 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2204 IBM_ACPIHANDLE_INIT(dock);
2206 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2207 str_supported(dock_handle != NULL));
2209 return (dock_handle)? 0 : 1;
2212 static int __init dock_init2(struct ibm_init_struct *iibm)
2214 int dock2_needed;
2216 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2218 if (dock_driver_data[0].flags.acpi_driver_registered &&
2219 dock_driver_data[0].flags.acpi_notify_installed) {
2220 IBM_ACPIHANDLE_INIT(pci);
2221 dock2_needed = (pci_handle != NULL);
2222 vdbg_printk(TPACPI_DBG_INIT,
2223 "dock PCI handler for the TP 570 is %s\n",
2224 str_supported(dock2_needed));
2225 } else {
2226 vdbg_printk(TPACPI_DBG_INIT,
2227 "dock subdriver part 2 not required\n");
2228 dock2_needed = 0;
2231 return (dock2_needed)? 0 : 1;
2234 static void dock_notify(struct ibm_struct *ibm, u32 event)
2236 int docked = dock_docked();
2237 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, PCI_ROOT_HID_STRING);
2239 if (event == 1 && !pci) /* 570 */
2240 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2241 else if (event == 1 && pci) /* 570 */
2242 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2243 else if (event == 3 && docked)
2244 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
2245 else if (event == 3 && !docked)
2246 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
2247 else if (event == 0 && docked)
2248 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
2249 else {
2250 printk(IBM_ERR "unknown dock event %d, status %d\n",
2251 event, _sta(dock_handle));
2252 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
2256 static int dock_read(char *p)
2258 int len = 0;
2259 int docked = dock_docked();
2261 if (!dock_handle)
2262 len += sprintf(p + len, "status:\t\tnot supported\n");
2263 else if (!docked)
2264 len += sprintf(p + len, "status:\t\tundocked\n");
2265 else {
2266 len += sprintf(p + len, "status:\t\tdocked\n");
2267 len += sprintf(p + len, "commands:\tdock, undock\n");
2270 return len;
2273 static int dock_write(char *buf)
2275 char *cmd;
2277 if (!dock_docked())
2278 return -ENODEV;
2280 while ((cmd = next_cmd(&buf))) {
2281 if (strlencmp(cmd, "undock") == 0) {
2282 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2283 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2284 return -EIO;
2285 } else if (strlencmp(cmd, "dock") == 0) {
2286 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2287 return -EIO;
2288 } else
2289 return -EINVAL;
2292 return 0;
2295 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2297 /*************************************************************************
2298 * Bay subdriver
2301 #ifdef CONFIG_THINKPAD_ACPI_BAY
2302 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2303 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2304 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2305 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2306 ); /* A21e, R30, R31 */
2307 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2308 "_EJ0", /* all others */
2309 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2310 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2311 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2312 ); /* all others */
2313 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2314 "_EJ0", /* 770x */
2315 ); /* all others */
2317 static int __init bay_init(struct ibm_init_struct *iibm)
2319 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2321 IBM_ACPIHANDLE_INIT(bay);
2322 if (bay_handle)
2323 IBM_ACPIHANDLE_INIT(bay_ej);
2324 IBM_ACPIHANDLE_INIT(bay2);
2325 if (bay2_handle)
2326 IBM_ACPIHANDLE_INIT(bay2_ej);
2328 tp_features.bay_status = bay_handle &&
2329 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2330 tp_features.bay_status2 = bay2_handle &&
2331 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2333 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2334 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2335 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2336 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2338 vdbg_printk(TPACPI_DBG_INIT,
2339 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2340 str_supported(tp_features.bay_status),
2341 str_supported(tp_features.bay_eject),
2342 str_supported(tp_features.bay_status2),
2343 str_supported(tp_features.bay_eject2));
2345 return (tp_features.bay_status || tp_features.bay_eject ||
2346 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2349 static void bay_notify(struct ibm_struct *ibm, u32 event)
2351 acpi_bus_generate_event(ibm->acpi->device, event, 0);
2354 #define bay_occupied(b) (_sta(b##_handle) & 1)
2356 static int bay_read(char *p)
2358 int len = 0;
2359 int occupied = bay_occupied(bay);
2360 int occupied2 = bay_occupied(bay2);
2361 int eject, eject2;
2363 len += sprintf(p + len, "status:\t\t%s\n",
2364 tp_features.bay_status ?
2365 (occupied ? "occupied" : "unoccupied") :
2366 "not supported");
2367 if (tp_features.bay_status2)
2368 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2369 "occupied" : "unoccupied");
2371 eject = tp_features.bay_eject && occupied;
2372 eject2 = tp_features.bay_eject2 && occupied2;
2374 if (eject && eject2)
2375 len += sprintf(p + len, "commands:\teject, eject2\n");
2376 else if (eject)
2377 len += sprintf(p + len, "commands:\teject\n");
2378 else if (eject2)
2379 len += sprintf(p + len, "commands:\teject2\n");
2381 return len;
2384 static int bay_write(char *buf)
2386 char *cmd;
2388 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2389 return -ENODEV;
2391 while ((cmd = next_cmd(&buf))) {
2392 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2393 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2394 return -EIO;
2395 } else if (tp_features.bay_eject2 &&
2396 strlencmp(cmd, "eject2") == 0) {
2397 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2398 return -EIO;
2399 } else
2400 return -EINVAL;
2403 return 0;
2406 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2407 .notify = bay_notify,
2408 .handle = &bay_handle,
2409 .type = ACPI_SYSTEM_NOTIFY,
2412 static struct ibm_struct bay_driver_data = {
2413 .name = "bay",
2414 .read = bay_read,
2415 .write = bay_write,
2416 .acpi = &ibm_bay_acpidriver,
2419 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2421 /*************************************************************************
2422 * CMOS subdriver
2425 /* sysfs cmos_command -------------------------------------------------- */
2426 static ssize_t cmos_command_store(struct device *dev,
2427 struct device_attribute *attr,
2428 const char *buf, size_t count)
2430 unsigned long cmos_cmd;
2431 int res;
2433 if (parse_strtoul(buf, 21, &cmos_cmd))
2434 return -EINVAL;
2436 res = issue_thinkpad_cmos_command(cmos_cmd);
2437 return (res)? res : count;
2440 static struct device_attribute dev_attr_cmos_command =
2441 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2443 /* --------------------------------------------------------------------- */
2445 static int __init cmos_init(struct ibm_init_struct *iibm)
2447 int res;
2449 vdbg_printk(TPACPI_DBG_INIT,
2450 "initializing cmos commands subdriver\n");
2452 IBM_ACPIHANDLE_INIT(cmos);
2454 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2455 str_supported(cmos_handle != NULL));
2457 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2458 if (res)
2459 return res;
2461 return (cmos_handle)? 0 : 1;
2464 static void cmos_exit(void)
2466 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2469 static int cmos_read(char *p)
2471 int len = 0;
2473 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2474 R30, R31, T20-22, X20-21 */
2475 if (!cmos_handle)
2476 len += sprintf(p + len, "status:\t\tnot supported\n");
2477 else {
2478 len += sprintf(p + len, "status:\t\tsupported\n");
2479 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2482 return len;
2485 static int cmos_write(char *buf)
2487 char *cmd;
2488 int cmos_cmd, res;
2490 while ((cmd = next_cmd(&buf))) {
2491 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2492 cmos_cmd >= 0 && cmos_cmd <= 21) {
2493 /* cmos_cmd set */
2494 } else
2495 return -EINVAL;
2497 res = issue_thinkpad_cmos_command(cmos_cmd);
2498 if (res)
2499 return res;
2502 return 0;
2505 static struct ibm_struct cmos_driver_data = {
2506 .name = "cmos",
2507 .read = cmos_read,
2508 .write = cmos_write,
2509 .exit = cmos_exit,
2512 /*************************************************************************
2513 * LED subdriver
2516 static enum led_access_mode led_supported;
2518 IBM_HANDLE(led, ec, "SLED", /* 570 */
2519 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2520 "LED", /* all others */
2521 ); /* R30, R31 */
2523 static int __init led_init(struct ibm_init_struct *iibm)
2525 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2527 IBM_ACPIHANDLE_INIT(led);
2529 if (!led_handle)
2530 /* led not supported on R30, R31 */
2531 led_supported = TPACPI_LED_NONE;
2532 else if (strlencmp(led_path, "SLED") == 0)
2533 /* 570 */
2534 led_supported = TPACPI_LED_570;
2535 else if (strlencmp(led_path, "SYSL") == 0)
2536 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2537 led_supported = TPACPI_LED_OLD;
2538 else
2539 /* all others */
2540 led_supported = TPACPI_LED_NEW;
2542 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2543 str_supported(led_supported), led_supported);
2545 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2548 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2550 static int led_read(char *p)
2552 int len = 0;
2554 if (!led_supported) {
2555 len += sprintf(p + len, "status:\t\tnot supported\n");
2556 return len;
2558 len += sprintf(p + len, "status:\t\tsupported\n");
2560 if (led_supported == TPACPI_LED_570) {
2561 /* 570 */
2562 int i, status;
2563 for (i = 0; i < 8; i++) {
2564 if (!acpi_evalf(ec_handle,
2565 &status, "GLED", "dd", 1 << i))
2566 return -EIO;
2567 len += sprintf(p + len, "%d:\t\t%s\n",
2568 i, led_status(status));
2572 len += sprintf(p + len, "commands:\t"
2573 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2575 return len;
2578 /* off, on, blink */
2579 static const int led_sled_arg1[] = { 0, 1, 3 };
2580 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2581 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2582 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2584 static int led_write(char *buf)
2586 char *cmd;
2587 int led, ind, ret;
2589 if (!led_supported)
2590 return -ENODEV;
2592 while ((cmd = next_cmd(&buf))) {
2593 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2594 return -EINVAL;
2596 if (strstr(cmd, "off")) {
2597 ind = 0;
2598 } else if (strstr(cmd, "on")) {
2599 ind = 1;
2600 } else if (strstr(cmd, "blink")) {
2601 ind = 2;
2602 } else
2603 return -EINVAL;
2605 if (led_supported == TPACPI_LED_570) {
2606 /* 570 */
2607 led = 1 << led;
2608 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2609 led, led_sled_arg1[ind]))
2610 return -EIO;
2611 } else if (led_supported == TPACPI_LED_OLD) {
2612 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2613 led = 1 << led;
2614 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2615 if (ret >= 0)
2616 ret =
2617 ec_write(TPACPI_LED_EC_HLBL,
2618 led * led_exp_hlbl[ind]);
2619 if (ret >= 0)
2620 ret =
2621 ec_write(TPACPI_LED_EC_HLCL,
2622 led * led_exp_hlcl[ind]);
2623 if (ret < 0)
2624 return ret;
2625 } else {
2626 /* all others */
2627 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2628 led, led_led_arg1[ind]))
2629 return -EIO;
2633 return 0;
2636 static struct ibm_struct led_driver_data = {
2637 .name = "led",
2638 .read = led_read,
2639 .write = led_write,
2642 /*************************************************************************
2643 * Beep subdriver
2646 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2648 static int __init beep_init(struct ibm_init_struct *iibm)
2650 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2652 IBM_ACPIHANDLE_INIT(beep);
2654 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2655 str_supported(beep_handle != NULL));
2657 return (beep_handle)? 0 : 1;
2660 static int beep_read(char *p)
2662 int len = 0;
2664 if (!beep_handle)
2665 len += sprintf(p + len, "status:\t\tnot supported\n");
2666 else {
2667 len += sprintf(p + len, "status:\t\tsupported\n");
2668 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2671 return len;
2674 static int beep_write(char *buf)
2676 char *cmd;
2677 int beep_cmd;
2679 if (!beep_handle)
2680 return -ENODEV;
2682 while ((cmd = next_cmd(&buf))) {
2683 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2684 beep_cmd >= 0 && beep_cmd <= 17) {
2685 /* beep_cmd set */
2686 } else
2687 return -EINVAL;
2688 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2689 return -EIO;
2692 return 0;
2695 static struct ibm_struct beep_driver_data = {
2696 .name = "beep",
2697 .read = beep_read,
2698 .write = beep_write,
2701 /*************************************************************************
2702 * Thermal subdriver
2705 static enum thermal_access_mode thermal_read_mode;
2707 /* sysfs temp##_input -------------------------------------------------- */
2709 static ssize_t thermal_temp_input_show(struct device *dev,
2710 struct device_attribute *attr,
2711 char *buf)
2713 struct sensor_device_attribute *sensor_attr =
2714 to_sensor_dev_attr(attr);
2715 int idx = sensor_attr->index;
2716 s32 value;
2717 int res;
2719 res = thermal_get_sensor(idx, &value);
2720 if (res)
2721 return res;
2722 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2723 return -ENXIO;
2725 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2728 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2729 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2731 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2732 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2733 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2734 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2735 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2736 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2737 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2738 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2739 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2740 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2741 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2742 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2743 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2744 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2745 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2746 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2747 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2750 #define THERMAL_ATTRS(X) \
2751 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2753 static struct attribute *thermal_temp_input_attr[] = {
2754 THERMAL_ATTRS(8),
2755 THERMAL_ATTRS(9),
2756 THERMAL_ATTRS(10),
2757 THERMAL_ATTRS(11),
2758 THERMAL_ATTRS(12),
2759 THERMAL_ATTRS(13),
2760 THERMAL_ATTRS(14),
2761 THERMAL_ATTRS(15),
2762 THERMAL_ATTRS(0),
2763 THERMAL_ATTRS(1),
2764 THERMAL_ATTRS(2),
2765 THERMAL_ATTRS(3),
2766 THERMAL_ATTRS(4),
2767 THERMAL_ATTRS(5),
2768 THERMAL_ATTRS(6),
2769 THERMAL_ATTRS(7),
2770 NULL
2773 static const struct attribute_group thermal_temp_input16_group = {
2774 .attrs = thermal_temp_input_attr
2777 static const struct attribute_group thermal_temp_input8_group = {
2778 .attrs = &thermal_temp_input_attr[8]
2781 #undef THERMAL_SENSOR_ATTR_TEMP
2782 #undef THERMAL_ATTRS
2784 /* --------------------------------------------------------------------- */
2786 static int __init thermal_init(struct ibm_init_struct *iibm)
2788 u8 t, ta1, ta2;
2789 int i;
2790 int acpi_tmp7;
2791 int res;
2793 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2795 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2797 if (thinkpad_id.ec_model) {
2799 * Direct EC access mode: sensors at registers
2800 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2801 * non-implemented, thermal sensors return 0x80 when
2802 * not available
2805 ta1 = ta2 = 0;
2806 for (i = 0; i < 8; i++) {
2807 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2808 ta1 |= t;
2809 } else {
2810 ta1 = 0;
2811 break;
2813 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2814 ta2 |= t;
2815 } else {
2816 ta1 = 0;
2817 break;
2820 if (ta1 == 0) {
2821 /* This is sheer paranoia, but we handle it anyway */
2822 if (acpi_tmp7) {
2823 printk(IBM_ERR
2824 "ThinkPad ACPI EC access misbehaving, "
2825 "falling back to ACPI TMPx access mode\n");
2826 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2827 } else {
2828 printk(IBM_ERR
2829 "ThinkPad ACPI EC access misbehaving, "
2830 "disabling thermal sensors access\n");
2831 thermal_read_mode = TPACPI_THERMAL_NONE;
2833 } else {
2834 thermal_read_mode =
2835 (ta2 != 0) ?
2836 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2838 } else if (acpi_tmp7) {
2839 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2840 /* 600e/x, 770e, 770x */
2841 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2842 } else {
2843 /* Standard ACPI TMPx access, max 8 sensors */
2844 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2846 } else {
2847 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2848 thermal_read_mode = TPACPI_THERMAL_NONE;
2851 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2852 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2853 thermal_read_mode);
2855 switch(thermal_read_mode) {
2856 case TPACPI_THERMAL_TPEC_16:
2857 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2858 &thermal_temp_input16_group);
2859 if (res)
2860 return res;
2861 break;
2862 case TPACPI_THERMAL_TPEC_8:
2863 case TPACPI_THERMAL_ACPI_TMP07:
2864 case TPACPI_THERMAL_ACPI_UPDT:
2865 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2866 &thermal_temp_input8_group);
2867 if (res)
2868 return res;
2869 break;
2870 case TPACPI_THERMAL_NONE:
2871 default:
2872 return 1;
2875 return 0;
2878 static void thermal_exit(void)
2880 switch(thermal_read_mode) {
2881 case TPACPI_THERMAL_TPEC_16:
2882 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2883 &thermal_temp_input16_group);
2884 break;
2885 case TPACPI_THERMAL_TPEC_8:
2886 case TPACPI_THERMAL_ACPI_TMP07:
2887 case TPACPI_THERMAL_ACPI_UPDT:
2888 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2889 &thermal_temp_input16_group);
2890 break;
2891 case TPACPI_THERMAL_NONE:
2892 default:
2893 break;
2897 /* idx is zero-based */
2898 static int thermal_get_sensor(int idx, s32 *value)
2900 int t;
2901 s8 tmp;
2902 char tmpi[5];
2904 t = TP_EC_THERMAL_TMP0;
2906 switch (thermal_read_mode) {
2907 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2908 case TPACPI_THERMAL_TPEC_16:
2909 if (idx >= 8 && idx <= 15) {
2910 t = TP_EC_THERMAL_TMP8;
2911 idx -= 8;
2913 /* fallthrough */
2914 #endif
2915 case TPACPI_THERMAL_TPEC_8:
2916 if (idx <= 7) {
2917 if (!acpi_ec_read(t + idx, &tmp))
2918 return -EIO;
2919 *value = tmp * 1000;
2920 return 0;
2922 break;
2924 case TPACPI_THERMAL_ACPI_UPDT:
2925 if (idx <= 7) {
2926 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2927 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2928 return -EIO;
2929 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2930 return -EIO;
2931 *value = (t - 2732) * 100;
2932 return 0;
2934 break;
2936 case TPACPI_THERMAL_ACPI_TMP07:
2937 if (idx <= 7) {
2938 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2939 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2940 return -EIO;
2941 if (t > 127 || t < -127)
2942 t = TP_EC_THERMAL_TMP_NA;
2943 *value = t * 1000;
2944 return 0;
2946 break;
2948 case TPACPI_THERMAL_NONE:
2949 default:
2950 return -ENOSYS;
2953 return -EINVAL;
2956 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2958 int res, i;
2959 int n;
2961 n = 8;
2962 i = 0;
2964 if (!s)
2965 return -EINVAL;
2967 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2968 n = 16;
2970 for(i = 0 ; i < n; i++) {
2971 res = thermal_get_sensor(i, &s->temp[i]);
2972 if (res)
2973 return res;
2976 return n;
2979 static int thermal_read(char *p)
2981 int len = 0;
2982 int n, i;
2983 struct ibm_thermal_sensors_struct t;
2985 n = thermal_get_sensors(&t);
2986 if (unlikely(n < 0))
2987 return n;
2989 len += sprintf(p + len, "temperatures:\t");
2991 if (n > 0) {
2992 for (i = 0; i < (n - 1); i++)
2993 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2994 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2995 } else
2996 len += sprintf(p + len, "not supported\n");
2998 return len;
3001 static struct ibm_struct thermal_driver_data = {
3002 .name = "thermal",
3003 .read = thermal_read,
3004 .exit = thermal_exit,
3007 /*************************************************************************
3008 * EC Dump subdriver
3011 static u8 ecdump_regs[256];
3013 static int ecdump_read(char *p)
3015 int len = 0;
3016 int i, j;
3017 u8 v;
3019 len += sprintf(p + len, "EC "
3020 " +00 +01 +02 +03 +04 +05 +06 +07"
3021 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3022 for (i = 0; i < 256; i += 16) {
3023 len += sprintf(p + len, "EC 0x%02x:", i);
3024 for (j = 0; j < 16; j++) {
3025 if (!acpi_ec_read(i + j, &v))
3026 break;
3027 if (v != ecdump_regs[i + j])
3028 len += sprintf(p + len, " *%02x", v);
3029 else
3030 len += sprintf(p + len, " %02x", v);
3031 ecdump_regs[i + j] = v;
3033 len += sprintf(p + len, "\n");
3034 if (j != 16)
3035 break;
3038 /* These are way too dangerous to advertise openly... */
3039 #if 0
3040 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3041 " (<offset> is 00-ff, <value> is 00-ff)\n");
3042 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3043 " (<offset> is 00-ff, <value> is 0-255)\n");
3044 #endif
3045 return len;
3048 static int ecdump_write(char *buf)
3050 char *cmd;
3051 int i, v;
3053 while ((cmd = next_cmd(&buf))) {
3054 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3055 /* i and v set */
3056 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3057 /* i and v set */
3058 } else
3059 return -EINVAL;
3060 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3061 if (!acpi_ec_write(i, v))
3062 return -EIO;
3063 } else
3064 return -EINVAL;
3067 return 0;
3070 static struct ibm_struct ecdump_driver_data = {
3071 .name = "ecdump",
3072 .read = ecdump_read,
3073 .write = ecdump_write,
3074 .flags.experimental = 1,
3077 /*************************************************************************
3078 * Backlight/brightness subdriver
3081 static struct backlight_device *ibm_backlight_device;
3083 static struct backlight_ops ibm_backlight_data = {
3084 .get_brightness = brightness_get,
3085 .update_status = brightness_update_status,
3088 static struct mutex brightness_mutex;
3090 static int __init brightness_init(struct ibm_init_struct *iibm)
3092 int b;
3094 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3096 mutex_init(&brightness_mutex);
3098 if (!brightness_mode) {
3099 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3100 brightness_mode = 2;
3101 else
3102 brightness_mode = 3;
3104 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3105 brightness_mode);
3108 if (brightness_mode > 3)
3109 return -EINVAL;
3111 b = brightness_get(NULL);
3112 if (b < 0)
3113 return 1;
3115 ibm_backlight_device = backlight_device_register(
3116 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3117 &ibm_backlight_data);
3118 if (IS_ERR(ibm_backlight_device)) {
3119 printk(IBM_ERR "Could not register backlight device\n");
3120 return PTR_ERR(ibm_backlight_device);
3122 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
3124 ibm_backlight_device->props.max_brightness = 7;
3125 ibm_backlight_device->props.brightness = b;
3126 backlight_update_status(ibm_backlight_device);
3128 return 0;
3131 static void brightness_exit(void)
3133 if (ibm_backlight_device) {
3134 vdbg_printk(TPACPI_DBG_EXIT,
3135 "calling backlight_device_unregister()\n");
3136 backlight_device_unregister(ibm_backlight_device);
3137 ibm_backlight_device = NULL;
3141 static int brightness_update_status(struct backlight_device *bd)
3143 return brightness_set(
3144 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3145 bd->props.power == FB_BLANK_UNBLANK) ?
3146 bd->props.brightness : 0);
3150 * ThinkPads can read brightness from two places: EC 0x31, or
3151 * CMOS NVRAM byte 0x5E, bits 0-3.
3153 static int brightness_get(struct backlight_device *bd)
3155 u8 lec = 0, lcmos = 0, level = 0;
3157 if (brightness_mode & 1) {
3158 if (!acpi_ec_read(brightness_offset, &lec))
3159 return -EIO;
3160 lec &= 7;
3161 level = lec;
3163 if (brightness_mode & 2) {
3164 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3165 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3166 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3167 level = lcmos;
3170 if (brightness_mode == 3 && lec != lcmos) {
3171 printk(IBM_ERR
3172 "CMOS NVRAM (%u) and EC (%u) do not agree "
3173 "on display brightness level\n",
3174 (unsigned int) lcmos,
3175 (unsigned int) lec);
3176 return -EIO;
3179 return level;
3182 static int brightness_set(int value)
3184 int cmos_cmd, inc, i, res;
3185 int current_value;
3187 if (value > 7)
3188 return -EINVAL;
3190 res = mutex_lock_interruptible(&brightness_mutex);
3191 if (res < 0)
3192 return res;
3194 current_value = brightness_get(NULL);
3195 if (current_value < 0) {
3196 res = current_value;
3197 goto errout;
3200 cmos_cmd = value > current_value ?
3201 TP_CMOS_BRIGHTNESS_UP :
3202 TP_CMOS_BRIGHTNESS_DOWN;
3203 inc = value > current_value ? 1 : -1;
3205 res = 0;
3206 for (i = current_value; i != value; i += inc) {
3207 if ((brightness_mode & 2) &&
3208 issue_thinkpad_cmos_command(cmos_cmd)) {
3209 res = -EIO;
3210 goto errout;
3212 if ((brightness_mode & 1) &&
3213 !acpi_ec_write(brightness_offset, i + inc)) {
3214 res = -EIO;
3215 goto errout;;
3219 errout:
3220 mutex_unlock(&brightness_mutex);
3221 return res;
3224 static int brightness_read(char *p)
3226 int len = 0;
3227 int level;
3229 if ((level = brightness_get(NULL)) < 0) {
3230 len += sprintf(p + len, "level:\t\tunreadable\n");
3231 } else {
3232 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3233 len += sprintf(p + len, "commands:\tup, down\n");
3234 len += sprintf(p + len, "commands:\tlevel <level>"
3235 " (<level> is 0-7)\n");
3238 return len;
3241 static int brightness_write(char *buf)
3243 int level;
3244 int new_level;
3245 char *cmd;
3247 while ((cmd = next_cmd(&buf))) {
3248 if ((level = brightness_get(NULL)) < 0)
3249 return level;
3250 level &= 7;
3252 if (strlencmp(cmd, "up") == 0) {
3253 new_level = level == 7 ? 7 : level + 1;
3254 } else if (strlencmp(cmd, "down") == 0) {
3255 new_level = level == 0 ? 0 : level - 1;
3256 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3257 new_level >= 0 && new_level <= 7) {
3258 /* new_level set */
3259 } else
3260 return -EINVAL;
3262 brightness_set(new_level);
3265 return 0;
3268 static struct ibm_struct brightness_driver_data = {
3269 .name = "brightness",
3270 .read = brightness_read,
3271 .write = brightness_write,
3272 .exit = brightness_exit,
3275 /*************************************************************************
3276 * Volume subdriver
3279 static int volume_read(char *p)
3281 int len = 0;
3282 u8 level;
3284 if (!acpi_ec_read(volume_offset, &level)) {
3285 len += sprintf(p + len, "level:\t\tunreadable\n");
3286 } else {
3287 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3288 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3289 len += sprintf(p + len, "commands:\tup, down, mute\n");
3290 len += sprintf(p + len, "commands:\tlevel <level>"
3291 " (<level> is 0-15)\n");
3294 return len;
3297 static int volume_write(char *buf)
3299 int cmos_cmd, inc, i;
3300 u8 level, mute;
3301 int new_level, new_mute;
3302 char *cmd;
3304 while ((cmd = next_cmd(&buf))) {
3305 if (!acpi_ec_read(volume_offset, &level))
3306 return -EIO;
3307 new_mute = mute = level & 0x40;
3308 new_level = level = level & 0xf;
3310 if (strlencmp(cmd, "up") == 0) {
3311 if (mute)
3312 new_mute = 0;
3313 else
3314 new_level = level == 15 ? 15 : level + 1;
3315 } else if (strlencmp(cmd, "down") == 0) {
3316 if (mute)
3317 new_mute = 0;
3318 else
3319 new_level = level == 0 ? 0 : level - 1;
3320 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3321 new_level >= 0 && new_level <= 15) {
3322 /* new_level set */
3323 } else if (strlencmp(cmd, "mute") == 0) {
3324 new_mute = 0x40;
3325 } else
3326 return -EINVAL;
3328 if (new_level != level) { /* mute doesn't change */
3329 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3330 inc = new_level > level ? 1 : -1;
3332 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
3333 !acpi_ec_write(volume_offset, level)))
3334 return -EIO;
3336 for (i = level; i != new_level; i += inc)
3337 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3338 !acpi_ec_write(volume_offset, i + inc))
3339 return -EIO;
3341 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
3342 !acpi_ec_write(volume_offset,
3343 new_level + mute)))
3344 return -EIO;
3347 if (new_mute != mute) { /* level doesn't change */
3348 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
3350 if (issue_thinkpad_cmos_command(cmos_cmd) ||
3351 !acpi_ec_write(volume_offset, level + new_mute))
3352 return -EIO;
3356 return 0;
3359 static struct ibm_struct volume_driver_data = {
3360 .name = "volume",
3361 .read = volume_read,
3362 .write = volume_write,
3365 /*************************************************************************
3366 * Fan subdriver
3370 * FAN ACCESS MODES
3372 * TPACPI_FAN_RD_ACPI_GFAN:
3373 * ACPI GFAN method: returns fan level
3375 * see TPACPI_FAN_WR_ACPI_SFAN
3376 * EC 0x2f (HFSP) not available if GFAN exists
3378 * TPACPI_FAN_WR_ACPI_SFAN:
3379 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
3381 * EC 0x2f (HFSP) might be available *for reading*, but do not use
3382 * it for writing.
3384 * TPACPI_FAN_WR_TPEC:
3385 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
3386 * Supported on almost all ThinkPads
3388 * Fan speed changes of any sort (including those caused by the
3389 * disengaged mode) are usually done slowly by the firmware as the
3390 * maximum ammount of fan duty cycle change per second seems to be
3391 * limited.
3393 * Reading is not available if GFAN exists.
3394 * Writing is not available if SFAN exists.
3396 * Bits
3397 * 7 automatic mode engaged;
3398 * (default operation mode of the ThinkPad)
3399 * fan level is ignored in this mode.
3400 * 6 full speed mode (takes precedence over bit 7);
3401 * not available on all thinkpads. May disable
3402 * the tachometer while the fan controller ramps up
3403 * the speed (which can take up to a few *minutes*).
3404 * Speeds up fan to 100% duty-cycle, which is far above
3405 * the standard RPM levels. It is not impossible that
3406 * it could cause hardware damage.
3407 * 5-3 unused in some models. Extra bits for fan level
3408 * in others, but still useless as all values above
3409 * 7 map to the same speed as level 7 in these models.
3410 * 2-0 fan level (0..7 usually)
3411 * 0x00 = stop
3412 * 0x07 = max (set when temperatures critical)
3413 * Some ThinkPads may have other levels, see
3414 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3416 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3417 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3418 * does so, its initial value is meaningless (0x07).
3420 * For firmware bugs, refer to:
3421 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3423 * ----
3425 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3426 * Main fan tachometer reading (in RPM)
3428 * This register is present on all ThinkPads with a new-style EC, and
3429 * it is known not to be present on the A21m/e, and T22, as there is
3430 * something else in offset 0x84 according to the ACPI DSDT. Other
3431 * ThinkPads from this same time period (and earlier) probably lack the
3432 * tachometer as well.
3434 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3435 * was never fixed by IBM to report the EC firmware version string
3436 * probably support the tachometer (like the early X models), so
3437 * detecting it is quite hard. We need more data to know for sure.
3439 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3440 * might result.
3442 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3443 * mode.
3445 * For firmware bugs, refer to:
3446 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3448 * TPACPI_FAN_WR_ACPI_FANS:
3449 * ThinkPad X31, X40, X41. Not available in the X60.
3451 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3452 * high speed. ACPI DSDT seems to map these three speeds to levels
3453 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3454 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3456 * The speeds are stored on handles
3457 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3459 * There are three default speed sets, acessible as handles:
3460 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3462 * ACPI DSDT switches which set is in use depending on various
3463 * factors.
3465 * TPACPI_FAN_WR_TPEC is also available and should be used to
3466 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3467 * but the ACPI tables just mention level 7.
3470 static enum fan_status_access_mode fan_status_access_mode;
3471 static enum fan_control_access_mode fan_control_access_mode;
3472 static enum fan_control_commands fan_control_commands;
3474 static u8 fan_control_initial_status;
3475 static u8 fan_control_desired_level;
3477 static void fan_watchdog_fire(struct work_struct *ignored);
3478 static int fan_watchdog_maxinterval;
3479 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3481 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3482 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3483 "\\FSPD", /* 600e/x, 770e, 770x */
3484 ); /* all others */
3485 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3486 "JFNS", /* 770x-JL */
3487 ); /* all others */
3490 * SYSFS fan layout: hwmon compatible (device)
3492 * pwm*_enable:
3493 * 0: "disengaged" mode
3494 * 1: manual mode
3495 * 2: native EC "auto" mode (recommended, hardware default)
3497 * pwm*: set speed in manual mode, ignored otherwise.
3498 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3499 * interpolation.
3501 * fan*_input: tachometer reading, RPM
3504 * SYSFS fan layout: extensions
3506 * fan_watchdog (driver):
3507 * fan watchdog interval in seconds, 0 disables (default), max 120
3510 /* sysfs fan pwm1_enable ----------------------------------------------- */
3511 static ssize_t fan_pwm1_enable_show(struct device *dev,
3512 struct device_attribute *attr,
3513 char *buf)
3515 int res, mode;
3516 u8 status;
3518 res = fan_get_status_safe(&status);
3519 if (res)
3520 return res;
3522 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3523 if (status != fan_control_initial_status) {
3524 tp_features.fan_ctrl_status_undef = 0;
3525 } else {
3526 /* Return most likely status. In fact, it
3527 * might be the only possible status */
3528 status = TP_EC_FAN_AUTO;
3532 if (status & TP_EC_FAN_FULLSPEED) {
3533 mode = 0;
3534 } else if (status & TP_EC_FAN_AUTO) {
3535 mode = 2;
3536 } else
3537 mode = 1;
3539 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3542 static ssize_t fan_pwm1_enable_store(struct device *dev,
3543 struct device_attribute *attr,
3544 const char *buf, size_t count)
3546 unsigned long t;
3547 int res, level;
3549 if (parse_strtoul(buf, 2, &t))
3550 return -EINVAL;
3552 switch (t) {
3553 case 0:
3554 level = TP_EC_FAN_FULLSPEED;
3555 break;
3556 case 1:
3557 level = TPACPI_FAN_LAST_LEVEL;
3558 break;
3559 case 2:
3560 level = TP_EC_FAN_AUTO;
3561 break;
3562 case 3:
3563 /* reserved for software-controlled auto mode */
3564 return -ENOSYS;
3565 default:
3566 return -EINVAL;
3569 res = fan_set_level_safe(level);
3570 if (res == -ENXIO)
3571 return -EINVAL;
3572 else if (res < 0)
3573 return res;
3575 fan_watchdog_reset();
3577 return count;
3580 static struct device_attribute dev_attr_fan_pwm1_enable =
3581 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3582 fan_pwm1_enable_show, fan_pwm1_enable_store);
3584 /* sysfs fan pwm1 ------------------------------------------------------ */
3585 static ssize_t fan_pwm1_show(struct device *dev,
3586 struct device_attribute *attr,
3587 char *buf)
3589 int res;
3590 u8 status;
3592 res = fan_get_status_safe(&status);
3593 if (res)
3594 return res;
3596 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3597 if (status != fan_control_initial_status) {
3598 tp_features.fan_ctrl_status_undef = 0;
3599 } else {
3600 status = TP_EC_FAN_AUTO;
3604 if ((status &
3605 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3606 status = fan_control_desired_level;
3608 if (status > 7)
3609 status = 7;
3611 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3614 static ssize_t fan_pwm1_store(struct device *dev,
3615 struct device_attribute *attr,
3616 const char *buf, size_t count)
3618 unsigned long s;
3619 int rc;
3620 u8 status, newlevel;
3622 if (parse_strtoul(buf, 255, &s))
3623 return -EINVAL;
3625 /* scale down from 0-255 to 0-7 */
3626 newlevel = (s >> 5) & 0x07;
3628 rc = mutex_lock_interruptible(&fan_mutex);
3629 if (rc < 0)
3630 return rc;
3632 rc = fan_get_status(&status);
3633 if (!rc && (status &
3634 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3635 rc = fan_set_level(newlevel);
3636 if (rc == -ENXIO)
3637 rc = -EINVAL;
3638 else if (!rc) {
3639 fan_update_desired_level(newlevel);
3640 fan_watchdog_reset();
3644 mutex_unlock(&fan_mutex);
3645 return (rc)? rc : count;
3648 static struct device_attribute dev_attr_fan_pwm1 =
3649 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3650 fan_pwm1_show, fan_pwm1_store);
3652 /* sysfs fan fan1_input ------------------------------------------------ */
3653 static ssize_t fan_fan1_input_show(struct device *dev,
3654 struct device_attribute *attr,
3655 char *buf)
3657 int res;
3658 unsigned int speed;
3660 res = fan_get_speed(&speed);
3661 if (res < 0)
3662 return res;
3664 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3667 static struct device_attribute dev_attr_fan_fan1_input =
3668 __ATTR(fan1_input, S_IRUGO,
3669 fan_fan1_input_show, NULL);
3671 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3672 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3673 char *buf)
3675 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3678 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3679 const char *buf, size_t count)
3681 unsigned long t;
3683 if (parse_strtoul(buf, 120, &t))
3684 return -EINVAL;
3686 if (!fan_control_allowed)
3687 return -EPERM;
3689 fan_watchdog_maxinterval = t;
3690 fan_watchdog_reset();
3692 return count;
3695 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3696 fan_fan_watchdog_show, fan_fan_watchdog_store);
3698 /* --------------------------------------------------------------------- */
3699 static struct attribute *fan_attributes[] = {
3700 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3701 &dev_attr_fan_fan1_input.attr,
3702 NULL
3705 static const struct attribute_group fan_attr_group = {
3706 .attrs = fan_attributes,
3709 static int __init fan_init(struct ibm_init_struct *iibm)
3711 int rc;
3713 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3715 mutex_init(&fan_mutex);
3716 fan_status_access_mode = TPACPI_FAN_NONE;
3717 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3718 fan_control_commands = 0;
3719 fan_watchdog_maxinterval = 0;
3720 tp_features.fan_ctrl_status_undef = 0;
3721 fan_control_desired_level = 7;
3723 IBM_ACPIHANDLE_INIT(fans);
3724 IBM_ACPIHANDLE_INIT(gfan);
3725 IBM_ACPIHANDLE_INIT(sfan);
3727 if (gfan_handle) {
3728 /* 570, 600e/x, 770e, 770x */
3729 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3730 } else {
3731 /* all other ThinkPads: note that even old-style
3732 * ThinkPad ECs supports the fan control register */
3733 if (likely(acpi_ec_read(fan_status_offset,
3734 &fan_control_initial_status))) {
3735 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3737 /* In some ThinkPads, neither the EC nor the ACPI
3738 * DSDT initialize the fan status, and it ends up
3739 * being set to 0x07 when it *could* be either
3740 * 0x07 or 0x80.
3742 * Enable for TP-1Y (T43), TP-78 (R51e),
3743 * TP-76 (R52), TP-70 (T43, R52), which are known
3744 * to be buggy. */
3745 if (fan_control_initial_status == 0x07) {
3746 switch (thinkpad_id.ec_model) {
3747 case 0x5931: /* TP-1Y */
3748 case 0x3837: /* TP-78 */
3749 case 0x3637: /* TP-76 */
3750 case 0x3037: /* TP-70 */
3751 printk(IBM_NOTICE
3752 "fan_init: initial fan status is "
3753 "unknown, assuming it is in auto "
3754 "mode\n");
3755 tp_features.fan_ctrl_status_undef = 1;
3759 } else {
3760 printk(IBM_ERR
3761 "ThinkPad ACPI EC access misbehaving, "
3762 "fan status and control unavailable\n");
3763 return 1;
3767 if (sfan_handle) {
3768 /* 570, 770x-JL */
3769 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3770 fan_control_commands |=
3771 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3772 } else {
3773 if (!gfan_handle) {
3774 /* gfan without sfan means no fan control */
3775 /* all other models implement TP EC 0x2f control */
3777 if (fans_handle) {
3778 /* X31, X40, X41 */
3779 fan_control_access_mode =
3780 TPACPI_FAN_WR_ACPI_FANS;
3781 fan_control_commands |=
3782 TPACPI_FAN_CMD_SPEED |
3783 TPACPI_FAN_CMD_LEVEL |
3784 TPACPI_FAN_CMD_ENABLE;
3785 } else {
3786 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3787 fan_control_commands |=
3788 TPACPI_FAN_CMD_LEVEL |
3789 TPACPI_FAN_CMD_ENABLE;
3794 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3795 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3796 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3797 fan_status_access_mode, fan_control_access_mode);
3799 /* fan control master switch */
3800 if (!fan_control_allowed) {
3801 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3802 fan_control_commands = 0;
3803 dbg_printk(TPACPI_DBG_INIT,
3804 "fan control features disabled by parameter\n");
3807 /* update fan_control_desired_level */
3808 if (fan_status_access_mode != TPACPI_FAN_NONE)
3809 fan_get_status_safe(NULL);
3811 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3812 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3813 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3814 &fan_attr_group);
3815 if (!(rc < 0))
3816 rc = driver_create_file(&tpacpi_pdriver.driver,
3817 &driver_attr_fan_watchdog);
3818 if (rc < 0)
3819 return rc;
3820 return 0;
3821 } else
3822 return 1;
3826 * Call with fan_mutex held
3828 static void fan_update_desired_level(u8 status)
3830 if ((status &
3831 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3832 if (status > 7)
3833 fan_control_desired_level = 7;
3834 else
3835 fan_control_desired_level = status;
3839 static int fan_get_status(u8 *status)
3841 u8 s;
3843 /* TODO:
3844 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3846 switch (fan_status_access_mode) {
3847 case TPACPI_FAN_RD_ACPI_GFAN:
3848 /* 570, 600e/x, 770e, 770x */
3850 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3851 return -EIO;
3853 if (likely(status))
3854 *status = s & 0x07;
3856 break;
3858 case TPACPI_FAN_RD_TPEC:
3859 /* all except 570, 600e/x, 770e, 770x */
3860 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3861 return -EIO;
3863 if (likely(status))
3864 *status = s;
3866 break;
3868 default:
3869 return -ENXIO;
3872 return 0;
3875 static int fan_get_status_safe(u8 *status)
3877 int rc;
3878 u8 s;
3880 rc = mutex_lock_interruptible(&fan_mutex);
3881 if (rc < 0)
3882 return rc;
3883 rc = fan_get_status(&s);
3884 if (!rc)
3885 fan_update_desired_level(s);
3886 mutex_unlock(&fan_mutex);
3888 if (status)
3889 *status = s;
3891 return rc;
3894 static void fan_exit(void)
3896 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3898 /* FIXME: can we really do this unconditionally? */
3899 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3900 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3902 cancel_delayed_work(&fan_watchdog_task);
3903 flush_scheduled_work();
3906 static int fan_get_speed(unsigned int *speed)
3908 u8 hi, lo;
3910 switch (fan_status_access_mode) {
3911 case TPACPI_FAN_RD_TPEC:
3912 /* all except 570, 600e/x, 770e, 770x */
3913 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3914 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3915 return -EIO;
3917 if (likely(speed))
3918 *speed = (hi << 8) | lo;
3920 break;
3922 default:
3923 return -ENXIO;
3926 return 0;
3929 static void fan_watchdog_fire(struct work_struct *ignored)
3931 int rc;
3933 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
3934 return;
3936 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3937 rc = fan_set_enable();
3938 if (rc < 0) {
3939 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3940 "will try again later...\n", -rc);
3941 /* reschedule for later */
3942 fan_watchdog_reset();
3946 static void fan_watchdog_reset(void)
3948 static int fan_watchdog_active;
3950 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3951 return;
3953 if (fan_watchdog_active)
3954 cancel_delayed_work(&fan_watchdog_task);
3956 if (fan_watchdog_maxinterval > 0 &&
3957 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
3958 fan_watchdog_active = 1;
3959 if (!schedule_delayed_work(&fan_watchdog_task,
3960 msecs_to_jiffies(fan_watchdog_maxinterval
3961 * 1000))) {
3962 printk(IBM_ERR "failed to schedule the fan watchdog, "
3963 "watchdog will not trigger\n");
3965 } else
3966 fan_watchdog_active = 0;
3969 static int fan_set_level(int level)
3971 if (!fan_control_allowed)
3972 return -EPERM;
3974 switch (fan_control_access_mode) {
3975 case TPACPI_FAN_WR_ACPI_SFAN:
3976 if (level >= 0 && level <= 7) {
3977 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3978 return -EIO;
3979 } else
3980 return -EINVAL;
3981 break;
3983 case TPACPI_FAN_WR_ACPI_FANS:
3984 case TPACPI_FAN_WR_TPEC:
3985 if ((level != TP_EC_FAN_AUTO) &&
3986 (level != TP_EC_FAN_FULLSPEED) &&
3987 ((level < 0) || (level > 7)))
3988 return -EINVAL;
3990 /* safety net should the EC not support AUTO
3991 * or FULLSPEED mode bits and just ignore them */
3992 if (level & TP_EC_FAN_FULLSPEED)
3993 level |= 7; /* safety min speed 7 */
3994 else if (level & TP_EC_FAN_FULLSPEED)
3995 level |= 4; /* safety min speed 4 */
3997 if (!acpi_ec_write(fan_status_offset, level))
3998 return -EIO;
3999 else
4000 tp_features.fan_ctrl_status_undef = 0;
4001 break;
4003 default:
4004 return -ENXIO;
4006 return 0;
4009 static int fan_set_level_safe(int level)
4011 int rc;
4013 if (!fan_control_allowed)
4014 return -EPERM;
4016 rc = mutex_lock_interruptible(&fan_mutex);
4017 if (rc < 0)
4018 return rc;
4020 if (level == TPACPI_FAN_LAST_LEVEL)
4021 level = fan_control_desired_level;
4023 rc = fan_set_level(level);
4024 if (!rc)
4025 fan_update_desired_level(level);
4027 mutex_unlock(&fan_mutex);
4028 return rc;
4031 static int fan_set_enable(void)
4033 u8 s;
4034 int rc;
4036 if (!fan_control_allowed)
4037 return -EPERM;
4039 rc = mutex_lock_interruptible(&fan_mutex);
4040 if (rc < 0)
4041 return rc;
4043 switch (fan_control_access_mode) {
4044 case TPACPI_FAN_WR_ACPI_FANS:
4045 case TPACPI_FAN_WR_TPEC:
4046 rc = fan_get_status(&s);
4047 if (rc < 0)
4048 break;
4050 /* Don't go out of emergency fan mode */
4051 if (s != 7) {
4052 s &= 0x07;
4053 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4056 if (!acpi_ec_write(fan_status_offset, s))
4057 rc = -EIO;
4058 else {
4059 tp_features.fan_ctrl_status_undef = 0;
4060 rc = 0;
4062 break;
4064 case TPACPI_FAN_WR_ACPI_SFAN:
4065 rc = fan_get_status(&s);
4066 if (rc < 0)
4067 break;
4069 s &= 0x07;
4071 /* Set fan to at least level 4 */
4072 s |= 4;
4074 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4075 rc= -EIO;
4076 else
4077 rc = 0;
4078 break;
4080 default:
4081 rc = -ENXIO;
4084 mutex_unlock(&fan_mutex);
4085 return rc;
4088 static int fan_set_disable(void)
4090 int rc;
4092 if (!fan_control_allowed)
4093 return -EPERM;
4095 rc = mutex_lock_interruptible(&fan_mutex);
4096 if (rc < 0)
4097 return rc;
4099 rc = 0;
4100 switch (fan_control_access_mode) {
4101 case TPACPI_FAN_WR_ACPI_FANS:
4102 case TPACPI_FAN_WR_TPEC:
4103 if (!acpi_ec_write(fan_status_offset, 0x00))
4104 rc = -EIO;
4105 else {
4106 fan_control_desired_level = 0;
4107 tp_features.fan_ctrl_status_undef = 0;
4109 break;
4111 case TPACPI_FAN_WR_ACPI_SFAN:
4112 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4113 rc = -EIO;
4114 else
4115 fan_control_desired_level = 0;
4116 break;
4118 default:
4119 rc = -ENXIO;
4123 mutex_unlock(&fan_mutex);
4124 return rc;
4127 static int fan_set_speed(int speed)
4129 int rc;
4131 if (!fan_control_allowed)
4132 return -EPERM;
4134 rc = mutex_lock_interruptible(&fan_mutex);
4135 if (rc < 0)
4136 return rc;
4138 rc = 0;
4139 switch (fan_control_access_mode) {
4140 case TPACPI_FAN_WR_ACPI_FANS:
4141 if (speed >= 0 && speed <= 65535) {
4142 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4143 speed, speed, speed))
4144 rc = -EIO;
4145 } else
4146 rc = -EINVAL;
4147 break;
4149 default:
4150 rc = -ENXIO;
4153 mutex_unlock(&fan_mutex);
4154 return rc;
4157 static int fan_read(char *p)
4159 int len = 0;
4160 int rc;
4161 u8 status;
4162 unsigned int speed = 0;
4164 switch (fan_status_access_mode) {
4165 case TPACPI_FAN_RD_ACPI_GFAN:
4166 /* 570, 600e/x, 770e, 770x */
4167 if ((rc = fan_get_status_safe(&status)) < 0)
4168 return rc;
4170 len += sprintf(p + len, "status:\t\t%s\n"
4171 "level:\t\t%d\n",
4172 (status != 0) ? "enabled" : "disabled", status);
4173 break;
4175 case TPACPI_FAN_RD_TPEC:
4176 /* all except 570, 600e/x, 770e, 770x */
4177 if ((rc = fan_get_status_safe(&status)) < 0)
4178 return rc;
4180 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4181 if (status != fan_control_initial_status)
4182 tp_features.fan_ctrl_status_undef = 0;
4183 else
4184 /* Return most likely status. In fact, it
4185 * might be the only possible status */
4186 status = TP_EC_FAN_AUTO;
4189 len += sprintf(p + len, "status:\t\t%s\n",
4190 (status != 0) ? "enabled" : "disabled");
4192 if ((rc = fan_get_speed(&speed)) < 0)
4193 return rc;
4195 len += sprintf(p + len, "speed:\t\t%d\n", speed);
4197 if (status & TP_EC_FAN_FULLSPEED)
4198 /* Disengaged mode takes precedence */
4199 len += sprintf(p + len, "level:\t\tdisengaged\n");
4200 else if (status & TP_EC_FAN_AUTO)
4201 len += sprintf(p + len, "level:\t\tauto\n");
4202 else
4203 len += sprintf(p + len, "level:\t\t%d\n", status);
4204 break;
4206 case TPACPI_FAN_NONE:
4207 default:
4208 len += sprintf(p + len, "status:\t\tnot supported\n");
4211 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
4212 len += sprintf(p + len, "commands:\tlevel <level>");
4214 switch (fan_control_access_mode) {
4215 case TPACPI_FAN_WR_ACPI_SFAN:
4216 len += sprintf(p + len, " (<level> is 0-7)\n");
4217 break;
4219 default:
4220 len += sprintf(p + len, " (<level> is 0-7, "
4221 "auto, disengaged, full-speed)\n");
4222 break;
4226 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
4227 len += sprintf(p + len, "commands:\tenable, disable\n"
4228 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
4229 "1-120 (seconds))\n");
4231 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
4232 len += sprintf(p + len, "commands:\tspeed <speed>"
4233 " (<speed> is 0-65535)\n");
4235 return len;
4238 static int fan_write_cmd_level(const char *cmd, int *rc)
4240 int level;
4242 if (strlencmp(cmd, "level auto") == 0)
4243 level = TP_EC_FAN_AUTO;
4244 else if ((strlencmp(cmd, "level disengaged") == 0) |
4245 (strlencmp(cmd, "level full-speed") == 0))
4246 level = TP_EC_FAN_FULLSPEED;
4247 else if (sscanf(cmd, "level %d", &level) != 1)
4248 return 0;
4250 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
4251 printk(IBM_ERR "level command accepted for unsupported "
4252 "access mode %d", fan_control_access_mode);
4254 return 1;
4257 static int fan_write_cmd_enable(const char *cmd, int *rc)
4259 if (strlencmp(cmd, "enable") != 0)
4260 return 0;
4262 if ((*rc = fan_set_enable()) == -ENXIO)
4263 printk(IBM_ERR "enable command accepted for unsupported "
4264 "access mode %d", fan_control_access_mode);
4266 return 1;
4269 static int fan_write_cmd_disable(const char *cmd, int *rc)
4271 if (strlencmp(cmd, "disable") != 0)
4272 return 0;
4274 if ((*rc = fan_set_disable()) == -ENXIO)
4275 printk(IBM_ERR "disable command accepted for unsupported "
4276 "access mode %d", fan_control_access_mode);
4278 return 1;
4281 static int fan_write_cmd_speed(const char *cmd, int *rc)
4283 int speed;
4285 /* TODO:
4286 * Support speed <low> <medium> <high> ? */
4288 if (sscanf(cmd, "speed %d", &speed) != 1)
4289 return 0;
4291 if ((*rc = fan_set_speed(speed)) == -ENXIO)
4292 printk(IBM_ERR "speed command accepted for unsupported "
4293 "access mode %d", fan_control_access_mode);
4295 return 1;
4298 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
4300 int interval;
4302 if (sscanf(cmd, "watchdog %d", &interval) != 1)
4303 return 0;
4305 if (interval < 0 || interval > 120)
4306 *rc = -EINVAL;
4307 else
4308 fan_watchdog_maxinterval = interval;
4310 return 1;
4313 static int fan_write(char *buf)
4315 char *cmd;
4316 int rc = 0;
4318 while (!rc && (cmd = next_cmd(&buf))) {
4319 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
4320 fan_write_cmd_level(cmd, &rc)) &&
4321 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
4322 (fan_write_cmd_enable(cmd, &rc) ||
4323 fan_write_cmd_disable(cmd, &rc) ||
4324 fan_write_cmd_watchdog(cmd, &rc))) &&
4325 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
4326 fan_write_cmd_speed(cmd, &rc))
4328 rc = -EINVAL;
4329 else if (!rc)
4330 fan_watchdog_reset();
4333 return rc;
4336 static struct ibm_struct fan_driver_data = {
4337 .name = "fan",
4338 .read = fan_read,
4339 .write = fan_write,
4340 .exit = fan_exit,
4343 /****************************************************************************
4344 ****************************************************************************
4346 * Infrastructure
4348 ****************************************************************************
4349 ****************************************************************************/
4351 /* /proc support */
4352 static struct proc_dir_entry *proc_dir;
4354 /* Subdriver registry */
4355 static LIST_HEAD(tpacpi_all_drivers);
4359 * Module and infrastructure proble, init and exit handling
4362 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
4363 static const char * __init str_supported(int is_supported)
4365 static char text_unsupported[] __initdata = "not supported";
4367 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
4369 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
4371 static int __init ibm_init(struct ibm_init_struct *iibm)
4373 int ret;
4374 struct ibm_struct *ibm = iibm->data;
4375 struct proc_dir_entry *entry;
4377 BUG_ON(ibm == NULL);
4379 INIT_LIST_HEAD(&ibm->all_drivers);
4381 if (ibm->flags.experimental && !experimental)
4382 return 0;
4384 dbg_printk(TPACPI_DBG_INIT,
4385 "probing for %s\n", ibm->name);
4387 if (iibm->init) {
4388 ret = iibm->init(iibm);
4389 if (ret > 0)
4390 return 0; /* probe failed */
4391 if (ret)
4392 return ret;
4394 ibm->flags.init_called = 1;
4397 if (ibm->acpi) {
4398 if (ibm->acpi->hid) {
4399 ret = register_tpacpi_subdriver(ibm);
4400 if (ret)
4401 goto err_out;
4404 if (ibm->acpi->notify) {
4405 ret = setup_acpi_notify(ibm);
4406 if (ret == -ENODEV) {
4407 printk(IBM_NOTICE "disabling subdriver %s\n",
4408 ibm->name);
4409 ret = 0;
4410 goto err_out;
4412 if (ret < 0)
4413 goto err_out;
4417 dbg_printk(TPACPI_DBG_INIT,
4418 "%s installed\n", ibm->name);
4420 if (ibm->read) {
4421 entry = create_proc_entry(ibm->name,
4422 S_IFREG | S_IRUGO | S_IWUSR,
4423 proc_dir);
4424 if (!entry) {
4425 printk(IBM_ERR "unable to create proc entry %s\n",
4426 ibm->name);
4427 ret = -ENODEV;
4428 goto err_out;
4430 entry->owner = THIS_MODULE;
4431 entry->data = ibm;
4432 entry->read_proc = &dispatch_procfs_read;
4433 if (ibm->write)
4434 entry->write_proc = &dispatch_procfs_write;
4435 ibm->flags.proc_created = 1;
4438 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4440 return 0;
4442 err_out:
4443 dbg_printk(TPACPI_DBG_INIT,
4444 "%s: at error exit path with result %d\n",
4445 ibm->name, ret);
4447 ibm_exit(ibm);
4448 return (ret < 0)? ret : 0;
4451 static void ibm_exit(struct ibm_struct *ibm)
4453 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4455 list_del_init(&ibm->all_drivers);
4457 if (ibm->flags.acpi_notify_installed) {
4458 dbg_printk(TPACPI_DBG_EXIT,
4459 "%s: acpi_remove_notify_handler\n", ibm->name);
4460 BUG_ON(!ibm->acpi);
4461 acpi_remove_notify_handler(*ibm->acpi->handle,
4462 ibm->acpi->type,
4463 dispatch_acpi_notify);
4464 ibm->flags.acpi_notify_installed = 0;
4465 ibm->flags.acpi_notify_installed = 0;
4468 if (ibm->flags.proc_created) {
4469 dbg_printk(TPACPI_DBG_EXIT,
4470 "%s: remove_proc_entry\n", ibm->name);
4471 remove_proc_entry(ibm->name, proc_dir);
4472 ibm->flags.proc_created = 0;
4475 if (ibm->flags.acpi_driver_registered) {
4476 dbg_printk(TPACPI_DBG_EXIT,
4477 "%s: acpi_bus_unregister_driver\n", ibm->name);
4478 BUG_ON(!ibm->acpi);
4479 acpi_bus_unregister_driver(ibm->acpi->driver);
4480 kfree(ibm->acpi->driver);
4481 ibm->acpi->driver = NULL;
4482 ibm->flags.acpi_driver_registered = 0;
4485 if (ibm->flags.init_called && ibm->exit) {
4486 ibm->exit();
4487 ibm->flags.init_called = 0;
4490 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4493 /* Probing */
4495 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
4497 struct dmi_device *dev = NULL;
4498 char ec_fw_string[18];
4500 if (!tp)
4501 return;
4503 memset(tp, 0, sizeof(*tp));
4505 if (dmi_name_in_vendors("IBM"))
4506 tp->vendor = PCI_VENDOR_ID_IBM;
4507 else if (dmi_name_in_vendors("LENOVO"))
4508 tp->vendor = PCI_VENDOR_ID_LENOVO;
4509 else
4510 return;
4512 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
4513 GFP_KERNEL);
4514 if (!tp->bios_version_str)
4515 return;
4516 tp->bios_model = tp->bios_version_str[0]
4517 | (tp->bios_version_str[1] << 8);
4520 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4521 * X32 or newer, all Z series; Some models must have an
4522 * up-to-date BIOS or they will not be detected.
4524 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4526 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4527 if (sscanf(dev->name,
4528 "IBM ThinkPad Embedded Controller -[%17c",
4529 ec_fw_string) == 1) {
4530 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4531 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4533 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
4534 tp->ec_model = ec_fw_string[0]
4535 | (ec_fw_string[1] << 8);
4536 break;
4540 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
4541 GFP_KERNEL);
4542 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
4543 kfree(tp->model_str);
4544 tp->model_str = NULL;
4548 static int __init probe_for_thinkpad(void)
4550 int is_thinkpad;
4552 if (acpi_disabled)
4553 return -ENODEV;
4556 * Non-ancient models have better DMI tagging, but very old models
4557 * don't.
4559 is_thinkpad = (thinkpad_id.model_str != NULL);
4561 /* ec is required because many other handles are relative to it */
4562 IBM_ACPIHANDLE_INIT(ec);
4563 if (!ec_handle) {
4564 if (is_thinkpad)
4565 printk(IBM_ERR
4566 "Not yet supported ThinkPad detected!\n");
4567 return -ENODEV;
4571 * Risks a regression on very old machines, but reduces potential
4572 * false positives a damn great deal
4574 if (!is_thinkpad)
4575 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
4577 if (!is_thinkpad && !force_load)
4578 return -ENODEV;
4580 return 0;
4584 /* Module init, exit, parameters */
4586 static struct ibm_init_struct ibms_init[] __initdata = {
4588 .init = thinkpad_acpi_driver_init,
4589 .data = &thinkpad_acpi_driver_data,
4592 .init = hotkey_init,
4593 .data = &hotkey_driver_data,
4596 .init = bluetooth_init,
4597 .data = &bluetooth_driver_data,
4600 .init = wan_init,
4601 .data = &wan_driver_data,
4604 .init = video_init,
4605 .data = &video_driver_data,
4608 .init = light_init,
4609 .data = &light_driver_data,
4611 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4613 .init = dock_init,
4614 .data = &dock_driver_data[0],
4617 .init = dock_init2,
4618 .data = &dock_driver_data[1],
4620 #endif
4621 #ifdef CONFIG_THINKPAD_ACPI_BAY
4623 .init = bay_init,
4624 .data = &bay_driver_data,
4626 #endif
4628 .init = cmos_init,
4629 .data = &cmos_driver_data,
4632 .init = led_init,
4633 .data = &led_driver_data,
4636 .init = beep_init,
4637 .data = &beep_driver_data,
4640 .init = thermal_init,
4641 .data = &thermal_driver_data,
4644 .data = &ecdump_driver_data,
4647 .init = brightness_init,
4648 .data = &brightness_driver_data,
4651 .data = &volume_driver_data,
4654 .init = fan_init,
4655 .data = &fan_driver_data,
4659 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4661 unsigned int i;
4662 struct ibm_struct *ibm;
4664 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4665 ibm = ibms_init[i].data;
4666 BUG_ON(ibm == NULL);
4668 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4669 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4670 return -ENOSPC;
4671 strcpy(ibms_init[i].param, val);
4672 strcat(ibms_init[i].param, ",");
4673 return 0;
4677 return -EINVAL;
4680 static int experimental;
4681 module_param(experimental, int, 0);
4683 static u32 dbg_level;
4684 module_param_named(debug, dbg_level, uint, 0);
4686 static int force_load;
4687 module_param(force_load, bool, 0);
4689 static int fan_control_allowed;
4690 module_param_named(fan_control, fan_control_allowed, bool, 0);
4692 static int brightness_mode;
4693 module_param_named(brightness_mode, brightness_mode, int, 0);
4695 static unsigned int hotkey_report_mode;
4696 module_param(hotkey_report_mode, uint, 0);
4698 #define IBM_PARAM(feature) \
4699 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4701 IBM_PARAM(hotkey);
4702 IBM_PARAM(bluetooth);
4703 IBM_PARAM(video);
4704 IBM_PARAM(light);
4705 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4706 IBM_PARAM(dock);
4707 #endif
4708 #ifdef CONFIG_THINKPAD_ACPI_BAY
4709 IBM_PARAM(bay);
4710 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4711 IBM_PARAM(cmos);
4712 IBM_PARAM(led);
4713 IBM_PARAM(beep);
4714 IBM_PARAM(ecdump);
4715 IBM_PARAM(brightness);
4716 IBM_PARAM(volume);
4717 IBM_PARAM(fan);
4719 static int __init thinkpad_acpi_module_init(void)
4721 int ret, i;
4723 tpacpi_lifecycle = TPACPI_LIFE_INIT;
4725 /* Parameter checking */
4726 if (hotkey_report_mode > 2)
4727 return -EINVAL;
4729 /* Driver-level probe */
4731 get_thinkpad_model_data(&thinkpad_id);
4732 ret = probe_for_thinkpad();
4733 if (ret) {
4734 thinkpad_acpi_module_exit();
4735 return ret;
4738 /* Driver initialization */
4740 IBM_ACPIHANDLE_INIT(ecrd);
4741 IBM_ACPIHANDLE_INIT(ecwr);
4743 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4744 if (!proc_dir) {
4745 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4746 thinkpad_acpi_module_exit();
4747 return -ENODEV;
4749 proc_dir->owner = THIS_MODULE;
4751 ret = platform_driver_register(&tpacpi_pdriver);
4752 if (ret) {
4753 printk(IBM_ERR "unable to register platform driver\n");
4754 thinkpad_acpi_module_exit();
4755 return ret;
4757 tp_features.platform_drv_registered = 1;
4759 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4760 if (ret) {
4761 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4762 thinkpad_acpi_module_exit();
4763 return ret;
4765 tp_features.platform_drv_attrs_registered = 1;
4768 /* Device initialization */
4769 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4770 NULL, 0);
4771 if (IS_ERR(tpacpi_pdev)) {
4772 ret = PTR_ERR(tpacpi_pdev);
4773 tpacpi_pdev = NULL;
4774 printk(IBM_ERR "unable to register platform device\n");
4775 thinkpad_acpi_module_exit();
4776 return ret;
4778 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4779 if (IS_ERR(tpacpi_hwmon)) {
4780 ret = PTR_ERR(tpacpi_hwmon);
4781 tpacpi_hwmon = NULL;
4782 printk(IBM_ERR "unable to register hwmon device\n");
4783 thinkpad_acpi_module_exit();
4784 return ret;
4786 mutex_init(&tpacpi_inputdev_send_mutex);
4787 tpacpi_inputdev = input_allocate_device();
4788 if (!tpacpi_inputdev) {
4789 printk(IBM_ERR "unable to allocate input device\n");
4790 thinkpad_acpi_module_exit();
4791 return -ENOMEM;
4792 } else {
4793 /* Prepare input device, but don't register */
4794 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
4795 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
4796 tpacpi_inputdev->id.bustype = BUS_HOST;
4797 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
4798 thinkpad_id.vendor :
4799 PCI_VENDOR_ID_IBM;
4800 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
4801 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
4803 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4804 ret = ibm_init(&ibms_init[i]);
4805 if (ret >= 0 && *ibms_init[i].param)
4806 ret = ibms_init[i].data->write(ibms_init[i].param);
4807 if (ret < 0) {
4808 thinkpad_acpi_module_exit();
4809 return ret;
4812 ret = input_register_device(tpacpi_inputdev);
4813 if (ret < 0) {
4814 printk(IBM_ERR "unable to register input device\n");
4815 thinkpad_acpi_module_exit();
4816 return ret;
4817 } else {
4818 tp_features.input_device_registered = 1;
4821 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
4822 return 0;
4825 static void thinkpad_acpi_module_exit(void)
4827 struct ibm_struct *ibm, *itmp;
4829 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
4831 list_for_each_entry_safe_reverse(ibm, itmp,
4832 &tpacpi_all_drivers,
4833 all_drivers) {
4834 ibm_exit(ibm);
4837 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4839 if (tpacpi_inputdev) {
4840 if (tp_features.input_device_registered)
4841 input_unregister_device(tpacpi_inputdev);
4842 else
4843 input_free_device(tpacpi_inputdev);
4846 if (tpacpi_hwmon)
4847 hwmon_device_unregister(tpacpi_hwmon);
4849 if (tpacpi_pdev)
4850 platform_device_unregister(tpacpi_pdev);
4852 if (tp_features.platform_drv_attrs_registered)
4853 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4855 if (tp_features.platform_drv_registered)
4856 platform_driver_unregister(&tpacpi_pdriver);
4858 if (proc_dir)
4859 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4861 kfree(thinkpad_id.bios_version_str);
4862 kfree(thinkpad_id.ec_version_str);
4863 kfree(thinkpad_id.model_str);
4866 module_init(thinkpad_acpi_module_init);
4867 module_exit(thinkpad_acpi_module_exit);