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
24 #define IBM_VERSION "0.14"
25 #define TPACPI_SYSFS_VERSION 0x000100
29 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
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
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
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 /****************************************************************************
121 ****************************************************************************
123 * ACPI Helpers and device model
125 ****************************************************************************
126 ****************************************************************************/
128 /*************************************************************************
132 static acpi_handle root_handle;
134 #define IBM_HANDLE(object, parent, paths...) \
135 static acpi_handle object##_handle; \
136 static acpi_handle *object##_parent = &parent##_handle; \
137 static char *object##_path; \
138 static char *object##_paths[] = { paths }
140 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
141 "\\_SB.PCI.ISA.EC", /* 570 */
142 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
143 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
144 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
145 "\\_SB.PCI0.ICH3.EC0", /* R31 */
146 "\\_SB.PCI0.LPC.EC", /* all others */
149 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
150 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
153 /*************************************************************************
157 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
158 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
159 "\\CMS", /* R40, R40e */
162 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
163 "^HKEY", /* R30, R31 */
164 "HKEY", /* all others */
168 /*************************************************************************
172 static int acpi_evalf(acpi_handle handle,
173 void *res, char *method, char *fmt, ...)
176 struct acpi_object_list params;
177 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
178 struct acpi_buffer result, *resultp;
179 union acpi_object out_obj;
187 printk(IBM_ERR "acpi_evalf() called with empty format\n");
200 params.pointer = &in_objs[0];
207 in_objs[params.count].integer.value = va_arg(ap, int);
208 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
210 /* add more types as needed */
212 printk(IBM_ERR "acpi_evalf() called "
213 "with invalid format character '%c'\n", c);
219 if (res_type != 'v') {
220 result.length = sizeof(out_obj);
221 result.pointer = &out_obj;
226 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
231 *(int *)res = out_obj.integer.value;
232 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
235 success = status == AE_OK;
237 /* add more types as needed */
239 printk(IBM_ERR "acpi_evalf() called "
240 "with invalid format character '%c'\n", res_type);
244 if (!success && !quiet)
245 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
246 method, fmt0, status);
251 static void __unused acpi_print_int(acpi_handle handle, char *method)
255 if (acpi_evalf(handle, &i, method, "d"))
256 printk(IBM_INFO "%s = 0x%x\n", method, i);
258 printk(IBM_ERR "error calling %s\n", method);
261 static int acpi_ec_read(int i, u8 * p)
266 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
270 if (ec_read(i, p) < 0)
277 static int acpi_ec_write(int i, u8 v)
280 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
283 if (ec_write(i, v) < 0)
290 static int _sta(acpi_handle handle)
294 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
300 static int issue_thinkpad_cmos_command(int cmos_cmd)
305 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
311 /*************************************************************************
315 static void drv_acpi_handle_init(char *name,
316 acpi_handle *handle, acpi_handle parent,
317 char **paths, int num_paths, char **path)
322 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
325 for (i = 0; i < num_paths; i++) {
326 status = acpi_get_handle(parent, paths[i], handle);
327 if (ACPI_SUCCESS(status)) {
329 dbg_printk(TPACPI_DBG_INIT,
330 "Found ACPI handle %s for %s\n",
336 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
341 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
343 struct ibm_struct *ibm = data;
345 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
348 ibm->acpi->notify(ibm, event);
351 static int __init setup_acpi_notify(struct ibm_struct *ibm)
358 if (!*ibm->acpi->handle)
361 vdbg_printk(TPACPI_DBG_INIT,
362 "setting up ACPI notify for %s\n", ibm->name);
364 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
366 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
371 acpi_driver_data(ibm->acpi->device) = ibm;
372 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
373 IBM_ACPI_EVENT_PREFIX,
376 status = acpi_install_notify_handler(*ibm->acpi->handle,
377 ibm->acpi->type, dispatch_acpi_notify, ibm);
378 if (ACPI_FAILURE(status)) {
379 if (status == AE_ALREADY_EXISTS) {
380 printk(IBM_NOTICE "another device driver is already handling %s events\n",
383 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
388 ibm->flags.acpi_notify_installed = 1;
392 static int __init tpacpi_device_add(struct acpi_device *device)
397 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
401 dbg_printk(TPACPI_DBG_INIT,
402 "registering %s as an ACPI driver\n", ibm->name);
406 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
407 if (!ibm->acpi->driver) {
408 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
412 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
413 ibm->acpi->driver->ids = ibm->acpi->hid;
414 ibm->acpi->driver->ops.add = &tpacpi_device_add;
416 rc = acpi_bus_register_driver(ibm->acpi->driver);
418 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
420 kfree(ibm->acpi->driver);
421 ibm->acpi->driver = NULL;
423 ibm->flags.acpi_driver_registered = 1;
429 /****************************************************************************
430 ****************************************************************************
434 ****************************************************************************
435 ****************************************************************************/
437 static int dispatch_procfs_read(char *page, char **start, off_t off,
438 int count, int *eof, void *data)
440 struct ibm_struct *ibm = data;
443 if (!ibm || !ibm->read)
446 len = ibm->read(page);
450 if (len <= off + count)
462 static int dispatch_procfs_write(struct file *file,
463 const char __user * userbuf,
464 unsigned long count, void *data)
466 struct ibm_struct *ibm = data;
470 if (!ibm || !ibm->write)
473 kernbuf = kmalloc(count + 2, GFP_KERNEL);
477 if (copy_from_user(kernbuf, userbuf, count)) {
483 strcat(kernbuf, ",");
484 ret = ibm->write(kernbuf);
493 static char *next_cmd(char **cmds)
498 while ((end = strchr(start, ',')) && end == start)
510 /****************************************************************************
511 ****************************************************************************
513 * Device model: hwmon and platform
515 ****************************************************************************
516 ****************************************************************************/
518 static struct platform_device *tpacpi_pdev;
519 static struct class_device *tpacpi_hwmon;
521 static struct platform_driver tpacpi_pdriver = {
523 .name = IBM_DRVR_NAME,
524 .owner = THIS_MODULE,
529 /*************************************************************************
530 * thinkpad-acpi driver attributes
533 /* interface_version --------------------------------------------------- */
534 static ssize_t tpacpi_driver_interface_version_show(
535 struct device_driver *drv,
538 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
541 static DRIVER_ATTR(interface_version, S_IRUGO,
542 tpacpi_driver_interface_version_show, NULL);
544 /* debug_level --------------------------------------------------------- */
545 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
548 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
551 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
552 const char *buf, size_t count)
556 if (parse_strtoul(buf, 0xffff, &t))
564 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
565 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
567 /* version ------------------------------------------------------------- */
568 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
571 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
574 static DRIVER_ATTR(version, S_IRUGO,
575 tpacpi_driver_version_show, NULL);
577 /* --------------------------------------------------------------------- */
579 static struct driver_attribute* tpacpi_driver_attributes[] = {
580 &driver_attr_debug_level, &driver_attr_version,
581 &driver_attr_interface_version,
584 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
590 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
591 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
598 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
602 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
603 driver_remove_file(drv, tpacpi_driver_attributes[i]);
606 /*************************************************************************
607 * sysfs support helpers
610 struct attribute_set_obj {
611 struct attribute_set s;
613 } __attribute__((packed));
615 static struct attribute_set *create_attr_set(unsigned int max_members,
618 struct attribute_set_obj *sobj;
620 if (max_members == 0)
623 /* Allocates space for implicit NULL at the end too */
624 sobj = kzalloc(sizeof(struct attribute_set_obj) +
625 max_members * sizeof(struct attribute *),
629 sobj->s.max_members = max_members;
630 sobj->s.group.attrs = &sobj->a;
631 sobj->s.group.name = name;
636 /* not multi-threaded safe, use it in a single thread per set */
637 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
642 if (s->members >= s->max_members)
645 s->group.attrs[s->members] = attr;
651 static int add_many_to_attr_set(struct attribute_set* s,
652 struct attribute **attr,
657 for (i = 0; i < count; i++) {
658 res = add_to_attr_set(s, attr[i]);
666 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
668 sysfs_remove_group(kobj, &s->group);
672 static int parse_strtoul(const char *buf,
673 unsigned long max, unsigned long *value)
677 *value = simple_strtoul(buf, &endp, 0);
678 while (*endp && isspace(*endp))
680 if (*endp || *value > max)
686 /****************************************************************************
687 ****************************************************************************
691 ****************************************************************************
692 ****************************************************************************/
694 /*************************************************************************
695 * thinkpad-acpi init subdriver
698 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
700 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
701 printk(IBM_INFO "%s\n", IBM_URL);
703 if (ibm_thinkpad_ec_found)
704 printk(IBM_INFO "ThinkPad EC firmware %s\n",
705 ibm_thinkpad_ec_found);
710 static int thinkpad_acpi_driver_read(char *p)
714 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
715 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
720 static struct ibm_struct thinkpad_acpi_driver_data = {
722 .read = thinkpad_acpi_driver_read,
725 /*************************************************************************
729 static int hotkey_orig_status;
730 static u32 hotkey_orig_mask;
731 static u32 hotkey_all_mask;
732 static u32 hotkey_reserved_mask = 0x00778000;
734 static struct attribute_set *hotkey_dev_attributes;
736 /* sysfs hotkey enable ------------------------------------------------- */
737 static ssize_t hotkey_enable_show(struct device *dev,
738 struct device_attribute *attr,
744 res = hotkey_get(&status, &mask);
748 return snprintf(buf, PAGE_SIZE, "%d\n", status);
751 static ssize_t hotkey_enable_store(struct device *dev,
752 struct device_attribute *attr,
753 const char *buf, size_t count)
759 if (parse_strtoul(buf, 1, &t))
762 res = hotkey_get(&status, &mask);
764 res = hotkey_set(t, mask);
766 return (res) ? res : count;
769 static struct device_attribute dev_attr_hotkey_enable =
770 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
771 hotkey_enable_show, hotkey_enable_store);
773 /* sysfs hotkey mask --------------------------------------------------- */
774 static ssize_t hotkey_mask_show(struct device *dev,
775 struct device_attribute *attr,
781 res = hotkey_get(&status, &mask);
785 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
788 static ssize_t hotkey_mask_store(struct device *dev,
789 struct device_attribute *attr,
790 const char *buf, size_t count)
796 if (parse_strtoul(buf, 0xffffffffUL, &t))
799 res = hotkey_get(&status, &mask);
801 hotkey_set(status, t);
803 return (res) ? res : count;
806 static struct device_attribute dev_attr_hotkey_mask =
807 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
808 hotkey_mask_show, hotkey_mask_store);
810 /* sysfs hotkey bios_enabled ------------------------------------------- */
811 static ssize_t hotkey_bios_enabled_show(struct device *dev,
812 struct device_attribute *attr,
815 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
818 static struct device_attribute dev_attr_hotkey_bios_enabled =
819 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
821 /* sysfs hotkey bios_mask ---------------------------------------------- */
822 static ssize_t hotkey_bios_mask_show(struct device *dev,
823 struct device_attribute *attr,
826 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
829 static struct device_attribute dev_attr_hotkey_bios_mask =
830 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
832 /* sysfs hotkey all_mask ----------------------------------------------- */
833 static ssize_t hotkey_all_mask_show(struct device *dev,
834 struct device_attribute *attr,
837 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
840 static struct device_attribute dev_attr_hotkey_all_mask =
841 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
843 /* sysfs hotkey recommended_mask --------------------------------------- */
844 static ssize_t hotkey_recommended_mask_show(struct device *dev,
845 struct device_attribute *attr,
848 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
849 hotkey_all_mask & ~hotkey_reserved_mask);
852 static struct device_attribute dev_attr_hotkey_recommended_mask =
853 __ATTR(hotkey_recommended_mask, S_IRUGO,
854 hotkey_recommended_mask_show, NULL);
856 /* --------------------------------------------------------------------- */
858 static struct attribute *hotkey_mask_attributes[] = {
859 &dev_attr_hotkey_mask.attr,
860 &dev_attr_hotkey_bios_enabled.attr,
861 &dev_attr_hotkey_bios_mask.attr,
862 &dev_attr_hotkey_all_mask.attr,
863 &dev_attr_hotkey_recommended_mask.attr,
866 static int __init hotkey_init(struct ibm_init_struct *iibm)
870 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
872 IBM_ACPIHANDLE_INIT(hkey);
873 mutex_init(&hotkey_mutex);
875 /* hotkey not supported on 570 */
876 tp_features.hotkey = hkey_handle != NULL;
878 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
879 str_supported(tp_features.hotkey));
881 if (tp_features.hotkey) {
882 hotkey_dev_attributes = create_attr_set(6, NULL);
883 if (!hotkey_dev_attributes)
885 res = add_to_attr_set(hotkey_dev_attributes,
886 &dev_attr_hotkey_enable.attr);
890 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
891 A30, R30, R31, T20-22, X20-21, X22-24 */
892 tp_features.hotkey_mask =
893 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
895 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
896 str_supported(tp_features.hotkey_mask));
898 if (tp_features.hotkey_mask) {
899 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
900 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
902 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
905 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
906 if (!res && tp_features.hotkey_mask) {
907 res = add_many_to_attr_set(hotkey_dev_attributes,
908 hotkey_mask_attributes,
909 ARRAY_SIZE(hotkey_mask_attributes));
912 res = register_attr_set_with_sysfs(
913 hotkey_dev_attributes,
914 &tpacpi_pdev->dev.kobj);
920 return (tp_features.hotkey)? 0 : 1;
923 static void hotkey_exit(void)
927 if (tp_features.hotkey) {
928 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
929 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
931 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
934 if (hotkey_dev_attributes) {
935 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
936 hotkey_dev_attributes = NULL;
940 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
944 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
945 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
947 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
948 acpi_bus_generate_event(ibm->acpi->device, event, 0);
953 * Call with hotkey_mutex held
955 static int hotkey_get(int *status, u32 *mask)
957 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
960 if (tp_features.hotkey_mask)
961 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
968 * Call with hotkey_mutex held
970 static int hotkey_set(int status, u32 mask)
974 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
977 if (tp_features.hotkey_mask)
978 for (i = 0; i < 32; i++) {
979 int bit = ((1 << i) & mask) != 0;
980 if (!acpi_evalf(hkey_handle,
981 NULL, "MHKM", "vdd", i + 1, bit))
988 /* procfs -------------------------------------------------------------- */
989 static int hotkey_read(char *p)
995 if (!tp_features.hotkey) {
996 len += sprintf(p + len, "status:\t\tnot supported\n");
1000 res = mutex_lock_interruptible(&hotkey_mutex);
1003 res = hotkey_get(&status, &mask);
1004 mutex_unlock(&hotkey_mutex);
1008 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1009 if (tp_features.hotkey_mask) {
1010 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1011 len += sprintf(p + len,
1012 "commands:\tenable, disable, reset, <mask>\n");
1014 len += sprintf(p + len, "mask:\t\tnot supported\n");
1015 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1021 static int hotkey_write(char *buf)
1028 if (!tp_features.hotkey)
1031 res = mutex_lock_interruptible(&hotkey_mutex);
1035 res = hotkey_get(&status, &mask);
1040 while ((cmd = next_cmd(&buf))) {
1041 if (strlencmp(cmd, "enable") == 0) {
1043 } else if (strlencmp(cmd, "disable") == 0) {
1045 } else if (strlencmp(cmd, "reset") == 0) {
1046 status = hotkey_orig_status;
1047 mask = hotkey_orig_mask;
1048 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1050 } else if (sscanf(cmd, "%x", &mask) == 1) {
1060 res = hotkey_set(status, mask);
1063 mutex_unlock(&hotkey_mutex);
1067 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1068 .hid = IBM_HKEY_HID,
1069 .notify = hotkey_notify,
1070 .handle = &hkey_handle,
1071 .type = ACPI_DEVICE_NOTIFY,
1074 static struct ibm_struct hotkey_driver_data = {
1076 .read = hotkey_read,
1077 .write = hotkey_write,
1078 .exit = hotkey_exit,
1079 .acpi = &ibm_hotkey_acpidriver,
1082 /*************************************************************************
1083 * Bluetooth subdriver
1086 /* sysfs bluetooth enable ---------------------------------------------- */
1087 static ssize_t bluetooth_enable_show(struct device *dev,
1088 struct device_attribute *attr,
1093 status = bluetooth_get_radiosw();
1097 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1100 static ssize_t bluetooth_enable_store(struct device *dev,
1101 struct device_attribute *attr,
1102 const char *buf, size_t count)
1107 if (parse_strtoul(buf, 1, &t))
1110 res = bluetooth_set_radiosw(t);
1112 return (res) ? res : count;
1115 static struct device_attribute dev_attr_bluetooth_enable =
1116 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1117 bluetooth_enable_show, bluetooth_enable_store);
1119 /* --------------------------------------------------------------------- */
1121 static struct attribute *bluetooth_attributes[] = {
1122 &dev_attr_bluetooth_enable.attr,
1126 static const struct attribute_group bluetooth_attr_group = {
1127 .attrs = bluetooth_attributes,
1130 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1135 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1137 IBM_ACPIHANDLE_INIT(hkey);
1139 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1140 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1141 tp_features.bluetooth = hkey_handle &&
1142 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1144 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1145 str_supported(tp_features.bluetooth),
1148 if (tp_features.bluetooth) {
1149 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1150 /* no bluetooth hardware present in system */
1151 tp_features.bluetooth = 0;
1152 dbg_printk(TPACPI_DBG_INIT,
1153 "bluetooth hardware not installed\n");
1155 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1156 &bluetooth_attr_group);
1162 return (tp_features.bluetooth)? 0 : 1;
1165 static void bluetooth_exit(void)
1167 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1168 &bluetooth_attr_group);
1171 static int bluetooth_get_radiosw(void)
1175 if (!tp_features.bluetooth)
1178 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1181 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1184 static int bluetooth_set_radiosw(int radio_on)
1188 if (!tp_features.bluetooth)
1191 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1194 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1196 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1197 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1203 /* procfs -------------------------------------------------------------- */
1204 static int bluetooth_read(char *p)
1207 int status = bluetooth_get_radiosw();
1209 if (!tp_features.bluetooth)
1210 len += sprintf(p + len, "status:\t\tnot supported\n");
1212 len += sprintf(p + len, "status:\t\t%s\n",
1213 (status)? "enabled" : "disabled");
1214 len += sprintf(p + len, "commands:\tenable, disable\n");
1220 static int bluetooth_write(char *buf)
1224 if (!tp_features.bluetooth)
1227 while ((cmd = next_cmd(&buf))) {
1228 if (strlencmp(cmd, "enable") == 0) {
1229 bluetooth_set_radiosw(1);
1230 } else if (strlencmp(cmd, "disable") == 0) {
1231 bluetooth_set_radiosw(0);
1239 static struct ibm_struct bluetooth_driver_data = {
1240 .name = "bluetooth",
1241 .read = bluetooth_read,
1242 .write = bluetooth_write,
1243 .exit = bluetooth_exit,
1246 /*************************************************************************
1250 /* sysfs wan enable ---------------------------------------------------- */
1251 static ssize_t wan_enable_show(struct device *dev,
1252 struct device_attribute *attr,
1257 status = wan_get_radiosw();
1261 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1264 static ssize_t wan_enable_store(struct device *dev,
1265 struct device_attribute *attr,
1266 const char *buf, size_t count)
1271 if (parse_strtoul(buf, 1, &t))
1274 res = wan_set_radiosw(t);
1276 return (res) ? res : count;
1279 static struct device_attribute dev_attr_wan_enable =
1280 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1281 wan_enable_show, wan_enable_store);
1283 /* --------------------------------------------------------------------- */
1285 static struct attribute *wan_attributes[] = {
1286 &dev_attr_wan_enable.attr,
1290 static const struct attribute_group wan_attr_group = {
1291 .attrs = wan_attributes,
1294 static int __init wan_init(struct ibm_init_struct *iibm)
1299 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1301 IBM_ACPIHANDLE_INIT(hkey);
1303 tp_features.wan = hkey_handle &&
1304 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1306 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1307 str_supported(tp_features.wan),
1310 if (tp_features.wan) {
1311 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1312 /* no wan hardware present in system */
1313 tp_features.wan = 0;
1314 dbg_printk(TPACPI_DBG_INIT,
1315 "wan hardware not installed\n");
1317 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1324 return (tp_features.wan)? 0 : 1;
1327 static void wan_exit(void)
1329 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1333 static int wan_get_radiosw(void)
1337 if (!tp_features.wan)
1340 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1343 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1346 static int wan_set_radiosw(int radio_on)
1350 if (!tp_features.wan)
1353 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1356 status |= TP_ACPI_WANCARD_RADIOSSW;
1358 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1359 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1365 /* procfs -------------------------------------------------------------- */
1366 static int wan_read(char *p)
1369 int status = wan_get_radiosw();
1371 if (!tp_features.wan)
1372 len += sprintf(p + len, "status:\t\tnot supported\n");
1374 len += sprintf(p + len, "status:\t\t%s\n",
1375 (status)? "enabled" : "disabled");
1376 len += sprintf(p + len, "commands:\tenable, disable\n");
1382 static int wan_write(char *buf)
1386 if (!tp_features.wan)
1389 while ((cmd = next_cmd(&buf))) {
1390 if (strlencmp(cmd, "enable") == 0) {
1392 } else if (strlencmp(cmd, "disable") == 0) {
1401 static struct ibm_struct wan_driver_data = {
1406 .flags.experimental = 1,
1409 /*************************************************************************
1413 static enum video_access_mode video_supported;
1414 static int video_orig_autosw;
1416 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1417 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1418 "\\_SB.PCI0.VID0", /* 770e */
1419 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1420 "\\_SB.PCI0.AGP.VID", /* all others */
1423 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1425 static int __init video_init(struct ibm_init_struct *iibm)
1429 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1431 IBM_ACPIHANDLE_INIT(vid);
1432 IBM_ACPIHANDLE_INIT(vid2);
1434 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1435 /* G41, assume IVGA doesn't change */
1436 vid_handle = vid2_handle;
1439 /* video switching not supported on R30, R31 */
1440 video_supported = TPACPI_VIDEO_NONE;
1441 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1443 video_supported = TPACPI_VIDEO_570;
1444 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1445 /* 600e/x, 770e, 770x */
1446 video_supported = TPACPI_VIDEO_770;
1449 video_supported = TPACPI_VIDEO_NEW;
1451 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1452 str_supported(video_supported != TPACPI_VIDEO_NONE),
1455 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1458 static void video_exit(void)
1460 dbg_printk(TPACPI_DBG_EXIT,
1461 "restoring original video autoswitch mode\n");
1462 if (video_autosw_set(video_orig_autosw))
1463 printk(IBM_ERR "error while trying to restore original "
1464 "video autoswitch mode\n");
1467 static int video_outputsw_get(void)
1472 switch (video_supported) {
1473 case TPACPI_VIDEO_570:
1474 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1475 TP_ACPI_VIDEO_570_PHSCMD))
1477 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1479 case TPACPI_VIDEO_770:
1480 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1483 status |= TP_ACPI_VIDEO_S_LCD;
1484 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1487 status |= TP_ACPI_VIDEO_S_CRT;
1489 case TPACPI_VIDEO_NEW:
1490 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1491 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1494 status |= TP_ACPI_VIDEO_S_CRT;
1496 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1497 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1500 status |= TP_ACPI_VIDEO_S_LCD;
1501 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1504 status |= TP_ACPI_VIDEO_S_DVI;
1513 static int video_outputsw_set(int status)
1518 switch (video_supported) {
1519 case TPACPI_VIDEO_570:
1520 res = acpi_evalf(NULL, NULL,
1521 "\\_SB.PHS2", "vdd",
1522 TP_ACPI_VIDEO_570_PHS2CMD,
1523 status | TP_ACPI_VIDEO_570_PHS2SET);
1525 case TPACPI_VIDEO_770:
1526 autosw = video_autosw_get();
1530 res = video_autosw_set(1);
1533 res = acpi_evalf(vid_handle, NULL,
1534 "ASWT", "vdd", status * 0x100, 0);
1535 if (!autosw && video_autosw_set(autosw)) {
1536 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1540 case TPACPI_VIDEO_NEW:
1541 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1542 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1548 return (res)? 0 : -EIO;
1551 static int video_autosw_get(void)
1555 switch (video_supported) {
1556 case TPACPI_VIDEO_570:
1557 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1560 case TPACPI_VIDEO_770:
1561 case TPACPI_VIDEO_NEW:
1562 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1572 static int video_autosw_set(int enable)
1574 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1579 static int video_outputsw_cycle(void)
1581 int autosw = video_autosw_get();
1587 switch (video_supported) {
1588 case TPACPI_VIDEO_570:
1589 res = video_autosw_set(1);
1592 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1594 case TPACPI_VIDEO_770:
1595 case TPACPI_VIDEO_NEW:
1596 res = video_autosw_set(1);
1599 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1604 if (!autosw && video_autosw_set(autosw)) {
1605 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1609 return (res)? 0 : -EIO;
1612 static int video_expand_toggle(void)
1614 switch (video_supported) {
1615 case TPACPI_VIDEO_570:
1616 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1618 case TPACPI_VIDEO_770:
1619 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1621 case TPACPI_VIDEO_NEW:
1622 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1630 static int video_read(char *p)
1635 if (video_supported == TPACPI_VIDEO_NONE) {
1636 len += sprintf(p + len, "status:\t\tnot supported\n");
1640 status = video_outputsw_get();
1644 autosw = video_autosw_get();
1648 len += sprintf(p + len, "status:\t\tsupported\n");
1649 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1650 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1651 if (video_supported == TPACPI_VIDEO_NEW)
1652 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1653 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1654 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1655 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1656 if (video_supported == TPACPI_VIDEO_NEW)
1657 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1658 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1659 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1664 static int video_write(char *buf)
1667 int enable, disable, status;
1670 if (video_supported == TPACPI_VIDEO_NONE)
1676 while ((cmd = next_cmd(&buf))) {
1677 if (strlencmp(cmd, "lcd_enable") == 0) {
1678 enable |= TP_ACPI_VIDEO_S_LCD;
1679 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1680 disable |= TP_ACPI_VIDEO_S_LCD;
1681 } else if (strlencmp(cmd, "crt_enable") == 0) {
1682 enable |= TP_ACPI_VIDEO_S_CRT;
1683 } else if (strlencmp(cmd, "crt_disable") == 0) {
1684 disable |= TP_ACPI_VIDEO_S_CRT;
1685 } else if (video_supported == TPACPI_VIDEO_NEW &&
1686 strlencmp(cmd, "dvi_enable") == 0) {
1687 enable |= TP_ACPI_VIDEO_S_DVI;
1688 } else if (video_supported == TPACPI_VIDEO_NEW &&
1689 strlencmp(cmd, "dvi_disable") == 0) {
1690 disable |= TP_ACPI_VIDEO_S_DVI;
1691 } else if (strlencmp(cmd, "auto_enable") == 0) {
1692 res = video_autosw_set(1);
1695 } else if (strlencmp(cmd, "auto_disable") == 0) {
1696 res = video_autosw_set(0);
1699 } else if (strlencmp(cmd, "video_switch") == 0) {
1700 res = video_outputsw_cycle();
1703 } else if (strlencmp(cmd, "expand_toggle") == 0) {
1704 res = video_expand_toggle();
1711 if (enable || disable) {
1712 status = video_outputsw_get();
1715 res = video_outputsw_set((status & ~disable) | enable);
1723 static struct ibm_struct video_driver_data = {
1726 .write = video_write,
1730 /*************************************************************************
1731 * Light (thinklight) subdriver
1734 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
1735 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
1737 static int __init light_init(struct ibm_init_struct *iibm)
1739 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
1741 IBM_ACPIHANDLE_INIT(ledb);
1742 IBM_ACPIHANDLE_INIT(lght);
1743 IBM_ACPIHANDLE_INIT(cmos);
1745 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
1746 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
1748 if (tp_features.light)
1749 /* light status not supported on
1750 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
1751 tp_features.light_status =
1752 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
1754 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
1755 str_supported(tp_features.light));
1757 return (tp_features.light)? 0 : 1;
1760 static int light_read(char *p)
1765 if (!tp_features.light) {
1766 len += sprintf(p + len, "status:\t\tnot supported\n");
1767 } else if (!tp_features.light_status) {
1768 len += sprintf(p + len, "status:\t\tunknown\n");
1769 len += sprintf(p + len, "commands:\ton, off\n");
1771 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
1773 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
1774 len += sprintf(p + len, "commands:\ton, off\n");
1780 static int light_write(char *buf)
1782 int cmos_cmd, lght_cmd;
1786 if (!tp_features.light)
1789 while ((cmd = next_cmd(&buf))) {
1790 if (strlencmp(cmd, "on") == 0) {
1793 } else if (strlencmp(cmd, "off") == 0) {
1799 success = cmos_handle ?
1800 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
1801 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
1809 static struct ibm_struct light_driver_data = {
1812 .write = light_write,
1815 /*************************************************************************
1819 #ifdef CONFIG_THINKPAD_ACPI_DOCK
1821 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
1822 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
1823 "\\_SB.PCI0.PCI1.DOCK", /* all others */
1824 "\\_SB.PCI.ISA.SLCE", /* 570 */
1825 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
1827 /* don't list other alternatives as we install a notify handler on the 570 */
1828 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
1830 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
1832 .notify = dock_notify,
1833 .handle = &dock_handle,
1834 .type = ACPI_SYSTEM_NOTIFY,
1838 .notify = dock_notify,
1839 .handle = &pci_handle,
1840 .type = ACPI_SYSTEM_NOTIFY,
1844 static struct ibm_struct dock_driver_data[2] = {
1848 .write = dock_write,
1849 .acpi = &ibm_dock_acpidriver[0],
1853 .acpi = &ibm_dock_acpidriver[1],
1857 #define dock_docked() (_sta(dock_handle) & 1)
1859 static int __init dock_init(struct ibm_init_struct *iibm)
1861 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
1863 IBM_ACPIHANDLE_INIT(dock);
1865 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
1866 str_supported(dock_handle != NULL));
1868 return (dock_handle)? 0 : 1;
1871 static int __init dock_init2(struct ibm_init_struct *iibm)
1875 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
1877 if (dock_driver_data[0].flags.acpi_driver_registered &&
1878 dock_driver_data[0].flags.acpi_notify_installed) {
1879 IBM_ACPIHANDLE_INIT(pci);
1880 dock2_needed = (pci_handle != NULL);
1881 vdbg_printk(TPACPI_DBG_INIT,
1882 "dock PCI handler for the TP 570 is %s\n",
1883 str_supported(dock2_needed));
1885 vdbg_printk(TPACPI_DBG_INIT,
1886 "dock subdriver part 2 not required\n");
1890 return (dock2_needed)? 0 : 1;
1893 static void dock_notify(struct ibm_struct *ibm, u32 event)
1895 int docked = dock_docked();
1896 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, IBM_PCI_HID);
1898 if (event == 1 && !pci) /* 570 */
1899 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
1900 else if (event == 1 && pci) /* 570 */
1901 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
1902 else if (event == 3 && docked)
1903 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
1904 else if (event == 3 && !docked)
1905 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
1906 else if (event == 0 && docked)
1907 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
1909 printk(IBM_ERR "unknown dock event %d, status %d\n",
1910 event, _sta(dock_handle));
1911 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
1915 static int dock_read(char *p)
1918 int docked = dock_docked();
1921 len += sprintf(p + len, "status:\t\tnot supported\n");
1923 len += sprintf(p + len, "status:\t\tundocked\n");
1925 len += sprintf(p + len, "status:\t\tdocked\n");
1926 len += sprintf(p + len, "commands:\tdock, undock\n");
1932 static int dock_write(char *buf)
1939 while ((cmd = next_cmd(&buf))) {
1940 if (strlencmp(cmd, "undock") == 0) {
1941 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
1942 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
1944 } else if (strlencmp(cmd, "dock") == 0) {
1945 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
1954 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
1956 /*************************************************************************
1960 #ifdef CONFIG_THINKPAD_ACPI_BAY
1961 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
1962 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
1963 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
1964 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
1965 ); /* A21e, R30, R31 */
1966 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
1967 "_EJ0", /* all others */
1968 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
1969 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
1970 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
1972 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
1976 static int __init bay_init(struct ibm_init_struct *iibm)
1978 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
1980 IBM_ACPIHANDLE_INIT(bay);
1982 IBM_ACPIHANDLE_INIT(bay_ej);
1983 IBM_ACPIHANDLE_INIT(bay2);
1985 IBM_ACPIHANDLE_INIT(bay2_ej);
1987 tp_features.bay_status = bay_handle &&
1988 acpi_evalf(bay_handle, NULL, "_STA", "qv");
1989 tp_features.bay_status2 = bay2_handle &&
1990 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
1992 tp_features.bay_eject = bay_handle && bay_ej_handle &&
1993 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
1994 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
1995 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
1997 vdbg_printk(TPACPI_DBG_INIT,
1998 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
1999 str_supported(tp_features.bay_status),
2000 str_supported(tp_features.bay_eject),
2001 str_supported(tp_features.bay_status2),
2002 str_supported(tp_features.bay_eject2));
2004 return (tp_features.bay_status || tp_features.bay_eject ||
2005 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2008 static void bay_notify(struct ibm_struct *ibm, u32 event)
2010 acpi_bus_generate_event(ibm->acpi->device, event, 0);
2013 #define bay_occupied(b) (_sta(b##_handle) & 1)
2015 static int bay_read(char *p)
2018 int occupied = bay_occupied(bay);
2019 int occupied2 = bay_occupied(bay2);
2022 len += sprintf(p + len, "status:\t\t%s\n",
2023 tp_features.bay_status ?
2024 (occupied ? "occupied" : "unoccupied") :
2026 if (tp_features.bay_status2)
2027 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2028 "occupied" : "unoccupied");
2030 eject = tp_features.bay_eject && occupied;
2031 eject2 = tp_features.bay_eject2 && occupied2;
2033 if (eject && eject2)
2034 len += sprintf(p + len, "commands:\teject, eject2\n");
2036 len += sprintf(p + len, "commands:\teject\n");
2038 len += sprintf(p + len, "commands:\teject2\n");
2043 static int bay_write(char *buf)
2047 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2050 while ((cmd = next_cmd(&buf))) {
2051 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2052 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2054 } else if (tp_features.bay_eject2 &&
2055 strlencmp(cmd, "eject2") == 0) {
2056 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2065 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2066 .notify = bay_notify,
2067 .handle = &bay_handle,
2068 .type = ACPI_SYSTEM_NOTIFY,
2071 static struct ibm_struct bay_driver_data = {
2075 .acpi = &ibm_bay_acpidriver,
2078 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2080 /*************************************************************************
2084 /* sysfs cmos_command -------------------------------------------------- */
2085 static ssize_t cmos_command_store(struct device *dev,
2086 struct device_attribute *attr,
2087 const char *buf, size_t count)
2089 unsigned long cmos_cmd;
2092 if (parse_strtoul(buf, 21, &cmos_cmd))
2095 res = issue_thinkpad_cmos_command(cmos_cmd);
2096 return (res)? res : count;
2099 static struct device_attribute dev_attr_cmos_command =
2100 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2102 /* --------------------------------------------------------------------- */
2104 static int __init cmos_init(struct ibm_init_struct *iibm)
2108 vdbg_printk(TPACPI_DBG_INIT,
2109 "initializing cmos commands subdriver\n");
2111 IBM_ACPIHANDLE_INIT(cmos);
2113 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2114 str_supported(cmos_handle != NULL));
2116 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2120 return (cmos_handle)? 0 : 1;
2123 static void cmos_exit(void)
2125 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2128 static int cmos_read(char *p)
2132 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2133 R30, R31, T20-22, X20-21 */
2135 len += sprintf(p + len, "status:\t\tnot supported\n");
2137 len += sprintf(p + len, "status:\t\tsupported\n");
2138 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2144 static int cmos_write(char *buf)
2149 while ((cmd = next_cmd(&buf))) {
2150 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2151 cmos_cmd >= 0 && cmos_cmd <= 21) {
2156 res = issue_thinkpad_cmos_command(cmos_cmd);
2164 static struct ibm_struct cmos_driver_data = {
2167 .write = cmos_write,
2171 /*************************************************************************
2175 static enum led_access_mode led_supported;
2177 IBM_HANDLE(led, ec, "SLED", /* 570 */
2178 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2179 "LED", /* all others */
2182 static int __init led_init(struct ibm_init_struct *iibm)
2184 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2186 IBM_ACPIHANDLE_INIT(led);
2189 /* led not supported on R30, R31 */
2190 led_supported = TPACPI_LED_NONE;
2191 else if (strlencmp(led_path, "SLED") == 0)
2193 led_supported = TPACPI_LED_570;
2194 else if (strlencmp(led_path, "SYSL") == 0)
2195 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2196 led_supported = TPACPI_LED_OLD;
2199 led_supported = TPACPI_LED_NEW;
2201 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2202 str_supported(led_supported), led_supported);
2204 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2207 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2209 static int led_read(char *p)
2213 if (!led_supported) {
2214 len += sprintf(p + len, "status:\t\tnot supported\n");
2217 len += sprintf(p + len, "status:\t\tsupported\n");
2219 if (led_supported == TPACPI_LED_570) {
2222 for (i = 0; i < 8; i++) {
2223 if (!acpi_evalf(ec_handle,
2224 &status, "GLED", "dd", 1 << i))
2226 len += sprintf(p + len, "%d:\t\t%s\n",
2227 i, led_status(status));
2231 len += sprintf(p + len, "commands:\t"
2232 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2237 /* off, on, blink */
2238 static const int led_sled_arg1[] = { 0, 1, 3 };
2239 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2240 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2241 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2243 static int led_write(char *buf)
2251 while ((cmd = next_cmd(&buf))) {
2252 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2255 if (strstr(cmd, "off")) {
2257 } else if (strstr(cmd, "on")) {
2259 } else if (strstr(cmd, "blink")) {
2264 if (led_supported == TPACPI_LED_570) {
2267 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2268 led, led_sled_arg1[ind]))
2270 } else if (led_supported == TPACPI_LED_OLD) {
2271 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2273 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2276 ec_write(TPACPI_LED_EC_HLBL,
2277 led * led_exp_hlbl[ind]);
2280 ec_write(TPACPI_LED_EC_HLCL,
2281 led * led_exp_hlcl[ind]);
2286 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2287 led, led_led_arg1[ind]))
2295 static struct ibm_struct led_driver_data = {
2301 /*************************************************************************
2305 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2307 static int __init beep_init(struct ibm_init_struct *iibm)
2309 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2311 IBM_ACPIHANDLE_INIT(beep);
2313 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2314 str_supported(beep_handle != NULL));
2316 return (beep_handle)? 0 : 1;
2319 static int beep_read(char *p)
2324 len += sprintf(p + len, "status:\t\tnot supported\n");
2326 len += sprintf(p + len, "status:\t\tsupported\n");
2327 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2333 static int beep_write(char *buf)
2341 while ((cmd = next_cmd(&buf))) {
2342 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2343 beep_cmd >= 0 && beep_cmd <= 17) {
2347 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2354 static struct ibm_struct beep_driver_data = {
2357 .write = beep_write,
2360 /*************************************************************************
2364 static enum thermal_access_mode thermal_read_mode;
2366 /* sysfs temp##_input -------------------------------------------------- */
2368 static ssize_t thermal_temp_input_show(struct device *dev,
2369 struct device_attribute *attr,
2372 struct sensor_device_attribute *sensor_attr =
2373 to_sensor_dev_attr(attr);
2374 int idx = sensor_attr->index;
2378 res = thermal_get_sensor(idx, &value);
2381 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2384 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2387 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2388 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2390 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2391 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2392 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2393 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2394 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2395 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2396 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2397 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2398 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2399 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2400 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2401 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2402 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2403 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2404 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2405 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2406 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2409 #define THERMAL_ATTRS(X) \
2410 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2412 static struct attribute *thermal_temp_input_attr[] = {
2432 static const struct attribute_group thermal_temp_input16_group = {
2433 .attrs = thermal_temp_input_attr
2436 static const struct attribute_group thermal_temp_input8_group = {
2437 .attrs = &thermal_temp_input_attr[8]
2440 #undef THERMAL_SENSOR_ATTR_TEMP
2441 #undef THERMAL_ATTRS
2443 /* --------------------------------------------------------------------- */
2445 static int __init thermal_init(struct ibm_init_struct *iibm)
2452 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2454 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2456 if (ibm_thinkpad_ec_found && experimental) {
2458 * Direct EC access mode: sensors at registers
2459 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2460 * non-implemented, thermal sensors return 0x80 when
2465 for (i = 0; i < 8; i++) {
2466 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2472 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2480 /* This is sheer paranoia, but we handle it anyway */
2483 "ThinkPad ACPI EC access misbehaving, "
2484 "falling back to ACPI TMPx access mode\n");
2485 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2488 "ThinkPad ACPI EC access misbehaving, "
2489 "disabling thermal sensors access\n");
2490 thermal_read_mode = TPACPI_THERMAL_NONE;
2495 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2497 } else if (acpi_tmp7) {
2498 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2499 /* 600e/x, 770e, 770x */
2500 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2502 /* Standard ACPI TMPx access, max 8 sensors */
2503 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2506 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2507 thermal_read_mode = TPACPI_THERMAL_NONE;
2510 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2511 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2514 switch(thermal_read_mode) {
2515 case TPACPI_THERMAL_TPEC_16:
2516 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2517 &thermal_temp_input16_group);
2521 case TPACPI_THERMAL_TPEC_8:
2522 case TPACPI_THERMAL_ACPI_TMP07:
2523 case TPACPI_THERMAL_ACPI_UPDT:
2524 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2525 &thermal_temp_input8_group);
2529 case TPACPI_THERMAL_NONE:
2537 static void thermal_exit(void)
2539 switch(thermal_read_mode) {
2540 case TPACPI_THERMAL_TPEC_16:
2541 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2542 &thermal_temp_input16_group);
2544 case TPACPI_THERMAL_TPEC_8:
2545 case TPACPI_THERMAL_ACPI_TMP07:
2546 case TPACPI_THERMAL_ACPI_UPDT:
2547 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2548 &thermal_temp_input16_group);
2550 case TPACPI_THERMAL_NONE:
2556 /* idx is zero-based */
2557 static int thermal_get_sensor(int idx, s32 *value)
2563 t = TP_EC_THERMAL_TMP0;
2565 switch (thermal_read_mode) {
2566 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2567 case TPACPI_THERMAL_TPEC_16:
2568 if (idx >= 8 && idx <= 15) {
2569 t = TP_EC_THERMAL_TMP8;
2574 case TPACPI_THERMAL_TPEC_8:
2576 if (!acpi_ec_read(t + idx, &tmp))
2578 *value = tmp * 1000;
2583 case TPACPI_THERMAL_ACPI_UPDT:
2585 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2586 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2588 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2590 *value = (t - 2732) * 100;
2595 case TPACPI_THERMAL_ACPI_TMP07:
2597 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2598 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2605 case TPACPI_THERMAL_NONE:
2613 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2624 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2627 for(i = 0 ; i < n; i++) {
2628 res = thermal_get_sensor(i, &s->temp[i]);
2636 static int thermal_read(char *p)
2640 struct ibm_thermal_sensors_struct t;
2642 n = thermal_get_sensors(&t);
2643 if (unlikely(n < 0))
2646 len += sprintf(p + len, "temperatures:\t");
2649 for (i = 0; i < (n - 1); i++)
2650 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2651 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2653 len += sprintf(p + len, "not supported\n");
2658 static struct ibm_struct thermal_driver_data = {
2660 .read = thermal_read,
2661 .exit = thermal_exit,
2664 /*************************************************************************
2668 static u8 ecdump_regs[256];
2670 static int ecdump_read(char *p)
2676 len += sprintf(p + len, "EC "
2677 " +00 +01 +02 +03 +04 +05 +06 +07"
2678 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2679 for (i = 0; i < 256; i += 16) {
2680 len += sprintf(p + len, "EC 0x%02x:", i);
2681 for (j = 0; j < 16; j++) {
2682 if (!acpi_ec_read(i + j, &v))
2684 if (v != ecdump_regs[i + j])
2685 len += sprintf(p + len, " *%02x", v);
2687 len += sprintf(p + len, " %02x", v);
2688 ecdump_regs[i + j] = v;
2690 len += sprintf(p + len, "\n");
2695 /* These are way too dangerous to advertise openly... */
2697 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2698 " (<offset> is 00-ff, <value> is 00-ff)\n");
2699 len += sprintf(p + len, "commands:\t0x<offset> <value> "
2700 " (<offset> is 00-ff, <value> is 0-255)\n");
2705 static int ecdump_write(char *buf)
2710 while ((cmd = next_cmd(&buf))) {
2711 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
2713 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
2717 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
2718 if (!acpi_ec_write(i, v))
2727 static struct ibm_struct ecdump_driver_data = {
2729 .read = ecdump_read,
2730 .write = ecdump_write,
2731 .flags.experimental = 1,
2734 /*************************************************************************
2735 * Backlight/brightness subdriver
2738 static struct backlight_device *ibm_backlight_device;
2740 static struct backlight_ops ibm_backlight_data = {
2741 .get_brightness = brightness_get,
2742 .update_status = brightness_update_status,
2745 static int __init brightness_init(struct ibm_init_struct *iibm)
2749 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
2751 b = brightness_get(NULL);
2755 ibm_backlight_device = backlight_device_register(
2756 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
2757 &ibm_backlight_data);
2758 if (IS_ERR(ibm_backlight_device)) {
2759 printk(IBM_ERR "Could not register backlight device\n");
2760 return PTR_ERR(ibm_backlight_device);
2762 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
2764 ibm_backlight_device->props.max_brightness = 7;
2765 ibm_backlight_device->props.brightness = b;
2766 backlight_update_status(ibm_backlight_device);
2771 static void brightness_exit(void)
2773 if (ibm_backlight_device) {
2774 vdbg_printk(TPACPI_DBG_EXIT,
2775 "calling backlight_device_unregister()\n");
2776 backlight_device_unregister(ibm_backlight_device);
2777 ibm_backlight_device = NULL;
2781 static int brightness_update_status(struct backlight_device *bd)
2783 return brightness_set(
2784 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
2785 bd->props.power == FB_BLANK_UNBLANK) ?
2786 bd->props.brightness : 0);
2789 static int brightness_get(struct backlight_device *bd)
2792 if (!acpi_ec_read(brightness_offset, &level))
2800 static int brightness_set(int value)
2802 int cmos_cmd, inc, i;
2803 int current_value = brightness_get(NULL);
2807 cmos_cmd = value > current_value ? TP_CMOS_BRIGHTNESS_UP : TP_CMOS_BRIGHTNESS_DOWN;
2808 inc = value > current_value ? 1 : -1;
2809 for (i = current_value; i != value; i += inc) {
2810 if (issue_thinkpad_cmos_command(cmos_cmd))
2812 if (!acpi_ec_write(brightness_offset, i + inc))
2819 static int brightness_read(char *p)
2824 if ((level = brightness_get(NULL)) < 0) {
2825 len += sprintf(p + len, "level:\t\tunreadable\n");
2827 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
2828 len += sprintf(p + len, "commands:\tup, down\n");
2829 len += sprintf(p + len, "commands:\tlevel <level>"
2830 " (<level> is 0-7)\n");
2836 static int brightness_write(char *buf)
2842 while ((cmd = next_cmd(&buf))) {
2843 if ((level = brightness_get(NULL)) < 0)
2847 if (strlencmp(cmd, "up") == 0) {
2848 new_level = level == 7 ? 7 : level + 1;
2849 } else if (strlencmp(cmd, "down") == 0) {
2850 new_level = level == 0 ? 0 : level - 1;
2851 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2852 new_level >= 0 && new_level <= 7) {
2857 brightness_set(new_level);
2863 static struct ibm_struct brightness_driver_data = {
2864 .name = "brightness",
2865 .read = brightness_read,
2866 .write = brightness_write,
2867 .exit = brightness_exit,
2870 /*************************************************************************
2874 static int volume_read(char *p)
2879 if (!acpi_ec_read(volume_offset, &level)) {
2880 len += sprintf(p + len, "level:\t\tunreadable\n");
2882 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
2883 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
2884 len += sprintf(p + len, "commands:\tup, down, mute\n");
2885 len += sprintf(p + len, "commands:\tlevel <level>"
2886 " (<level> is 0-15)\n");
2892 static int volume_write(char *buf)
2894 int cmos_cmd, inc, i;
2896 int new_level, new_mute;
2899 while ((cmd = next_cmd(&buf))) {
2900 if (!acpi_ec_read(volume_offset, &level))
2902 new_mute = mute = level & 0x40;
2903 new_level = level = level & 0xf;
2905 if (strlencmp(cmd, "up") == 0) {
2909 new_level = level == 15 ? 15 : level + 1;
2910 } else if (strlencmp(cmd, "down") == 0) {
2914 new_level = level == 0 ? 0 : level - 1;
2915 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
2916 new_level >= 0 && new_level <= 15) {
2918 } else if (strlencmp(cmd, "mute") == 0) {
2923 if (new_level != level) { /* mute doesn't change */
2924 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
2925 inc = new_level > level ? 1 : -1;
2927 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
2928 !acpi_ec_write(volume_offset, level)))
2931 for (i = level; i != new_level; i += inc)
2932 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2933 !acpi_ec_write(volume_offset, i + inc))
2936 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
2937 !acpi_ec_write(volume_offset,
2942 if (new_mute != mute) { /* level doesn't change */
2943 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
2945 if (issue_thinkpad_cmos_command(cmos_cmd) ||
2946 !acpi_ec_write(volume_offset, level + new_mute))
2954 static struct ibm_struct volume_driver_data = {
2956 .read = volume_read,
2957 .write = volume_write,
2960 /*************************************************************************
2967 * TPACPI_FAN_RD_ACPI_GFAN:
2968 * ACPI GFAN method: returns fan level
2970 * see TPACPI_FAN_WR_ACPI_SFAN
2971 * EC 0x2f (HFSP) not available if GFAN exists
2973 * TPACPI_FAN_WR_ACPI_SFAN:
2974 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
2976 * EC 0x2f (HFSP) might be available *for reading*, but do not use
2979 * TPACPI_FAN_WR_TPEC:
2980 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
2981 * Supported on almost all ThinkPads
2983 * Fan speed changes of any sort (including those caused by the
2984 * disengaged mode) are usually done slowly by the firmware as the
2985 * maximum ammount of fan duty cycle change per second seems to be
2988 * Reading is not available if GFAN exists.
2989 * Writing is not available if SFAN exists.
2992 * 7 automatic mode engaged;
2993 * (default operation mode of the ThinkPad)
2994 * fan level is ignored in this mode.
2995 * 6 full speed mode (takes precedence over bit 7);
2996 * not available on all thinkpads. May disable
2997 * the tachometer while the fan controller ramps up
2998 * the speed (which can take up to a few *minutes*).
2999 * Speeds up fan to 100% duty-cycle, which is far above
3000 * the standard RPM levels. It is not impossible that
3001 * it could cause hardware damage.
3002 * 5-3 unused in some models. Extra bits for fan level
3003 * in others, but still useless as all values above
3004 * 7 map to the same speed as level 7 in these models.
3005 * 2-0 fan level (0..7 usually)
3007 * 0x07 = max (set when temperatures critical)
3008 * Some ThinkPads may have other levels, see
3009 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
3011 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
3012 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
3013 * does so, its initial value is meaningless (0x07).
3015 * For firmware bugs, refer to:
3016 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3020 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
3021 * Main fan tachometer reading (in RPM)
3023 * This register is present on all ThinkPads with a new-style EC, and
3024 * it is known not to be present on the A21m/e, and T22, as there is
3025 * something else in offset 0x84 according to the ACPI DSDT. Other
3026 * ThinkPads from this same time period (and earlier) probably lack the
3027 * tachometer as well.
3029 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
3030 * was never fixed by IBM to report the EC firmware version string
3031 * probably support the tachometer (like the early X models), so
3032 * detecting it is quite hard. We need more data to know for sure.
3034 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
3037 * FIRMWARE BUG: may go stale while the EC is switching to full speed
3040 * For firmware bugs, refer to:
3041 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
3043 * TPACPI_FAN_WR_ACPI_FANS:
3044 * ThinkPad X31, X40, X41. Not available in the X60.
3046 * FANS ACPI handle: takes three arguments: low speed, medium speed,
3047 * high speed. ACPI DSDT seems to map these three speeds to levels
3048 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
3049 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
3051 * The speeds are stored on handles
3052 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
3054 * There are three default speed sets, acessible as handles:
3055 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
3057 * ACPI DSDT switches which set is in use depending on various
3060 * TPACPI_FAN_WR_TPEC is also available and should be used to
3061 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
3062 * but the ACPI tables just mention level 7.
3065 static enum fan_status_access_mode fan_status_access_mode;
3066 static enum fan_control_access_mode fan_control_access_mode;
3067 static enum fan_control_commands fan_control_commands;
3069 static u8 fan_control_initial_status;
3070 static u8 fan_control_desired_level;
3072 static void fan_watchdog_fire(struct work_struct *ignored);
3073 static int fan_watchdog_maxinterval;
3074 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
3076 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
3077 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
3078 "\\FSPD", /* 600e/x, 770e, 770x */
3080 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
3081 "JFNS", /* 770x-JL */
3085 * SYSFS fan layout: hwmon compatible (device)
3088 * 0: "disengaged" mode
3090 * 2: native EC "auto" mode (recommended, hardware default)
3092 * pwm*: set speed in manual mode, ignored otherwise.
3093 * 0 is level 0; 255 is level 7. Intermediate points done with linear
3096 * fan*_input: tachometer reading, RPM
3099 * SYSFS fan layout: extensions
3101 * fan_watchdog (driver):
3102 * fan watchdog interval in seconds, 0 disables (default), max 120
3105 /* sysfs fan pwm1_enable ----------------------------------------------- */
3106 static ssize_t fan_pwm1_enable_show(struct device *dev,
3107 struct device_attribute *attr,
3113 res = fan_get_status_safe(&status);
3117 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3118 if (status != fan_control_initial_status) {
3119 tp_features.fan_ctrl_status_undef = 0;
3121 /* Return most likely status. In fact, it
3122 * might be the only possible status */
3123 status = TP_EC_FAN_AUTO;
3127 if (status & TP_EC_FAN_FULLSPEED) {
3129 } else if (status & TP_EC_FAN_AUTO) {
3134 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
3137 static ssize_t fan_pwm1_enable_store(struct device *dev,
3138 struct device_attribute *attr,
3139 const char *buf, size_t count)
3144 if (parse_strtoul(buf, 2, &t))
3149 level = TP_EC_FAN_FULLSPEED;
3152 level = TPACPI_FAN_LAST_LEVEL;
3155 level = TP_EC_FAN_AUTO;
3158 /* reserved for software-controlled auto mode */
3164 res = fan_set_level_safe(level);
3170 fan_watchdog_reset();
3175 static struct device_attribute dev_attr_fan_pwm1_enable =
3176 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
3177 fan_pwm1_enable_show, fan_pwm1_enable_store);
3179 /* sysfs fan pwm1 ------------------------------------------------------ */
3180 static ssize_t fan_pwm1_show(struct device *dev,
3181 struct device_attribute *attr,
3187 res = fan_get_status_safe(&status);
3191 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3192 if (status != fan_control_initial_status) {
3193 tp_features.fan_ctrl_status_undef = 0;
3195 status = TP_EC_FAN_AUTO;
3200 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
3201 status = fan_control_desired_level;
3206 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
3209 static ssize_t fan_pwm1_store(struct device *dev,
3210 struct device_attribute *attr,
3211 const char *buf, size_t count)
3215 u8 status, newlevel;
3217 if (parse_strtoul(buf, 255, &s))
3220 /* scale down from 0-255 to 0-7 */
3221 newlevel = (s >> 5) & 0x07;
3223 rc = mutex_lock_interruptible(&fan_mutex);
3227 rc = fan_get_status(&status);
3228 if (!rc && (status &
3229 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3230 rc = fan_set_level(newlevel);
3234 fan_update_desired_level(newlevel);
3235 fan_watchdog_reset();
3239 mutex_unlock(&fan_mutex);
3240 return (rc)? rc : count;
3243 static struct device_attribute dev_attr_fan_pwm1 =
3244 __ATTR(pwm1, S_IWUSR | S_IRUGO,
3245 fan_pwm1_show, fan_pwm1_store);
3247 /* sysfs fan fan1_input ------------------------------------------------ */
3248 static ssize_t fan_fan1_input_show(struct device *dev,
3249 struct device_attribute *attr,
3255 res = fan_get_speed(&speed);
3259 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
3262 static struct device_attribute dev_attr_fan_fan1_input =
3263 __ATTR(fan1_input, S_IRUGO,
3264 fan_fan1_input_show, NULL);
3266 /* sysfs fan fan_watchdog (driver) ------------------------------------- */
3267 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
3270 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
3273 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
3274 const char *buf, size_t count)
3278 if (parse_strtoul(buf, 120, &t))
3281 if (!fan_control_allowed)
3284 fan_watchdog_maxinterval = t;
3285 fan_watchdog_reset();
3290 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
3291 fan_fan_watchdog_show, fan_fan_watchdog_store);
3293 /* --------------------------------------------------------------------- */
3294 static struct attribute *fan_attributes[] = {
3295 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
3296 &dev_attr_fan_fan1_input.attr,
3300 static const struct attribute_group fan_attr_group = {
3301 .attrs = fan_attributes,
3304 static int __init fan_init(struct ibm_init_struct *iibm)
3308 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
3310 mutex_init(&fan_mutex);
3311 fan_status_access_mode = TPACPI_FAN_NONE;
3312 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3313 fan_control_commands = 0;
3314 fan_watchdog_maxinterval = 0;
3315 tp_features.fan_ctrl_status_undef = 0;
3316 fan_control_desired_level = 7;
3318 IBM_ACPIHANDLE_INIT(fans);
3319 IBM_ACPIHANDLE_INIT(gfan);
3320 IBM_ACPIHANDLE_INIT(sfan);
3323 /* 570, 600e/x, 770e, 770x */
3324 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
3326 /* all other ThinkPads: note that even old-style
3327 * ThinkPad ECs supports the fan control register */
3328 if (likely(acpi_ec_read(fan_status_offset,
3329 &fan_control_initial_status))) {
3330 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
3332 /* In some ThinkPads, neither the EC nor the ACPI
3333 * DSDT initialize the fan status, and it ends up
3334 * being set to 0x07 when it *could* be either
3337 * Enable for TP-1Y (T43), TP-78 (R51e),
3338 * TP-76 (R52), TP-70 (T43, R52), which are known
3340 if (fan_control_initial_status == 0x07 &&
3341 ibm_thinkpad_ec_found &&
3342 ((ibm_thinkpad_ec_found[0] == '1' &&
3343 ibm_thinkpad_ec_found[1] == 'Y') ||
3344 (ibm_thinkpad_ec_found[0] == '7' &&
3345 (ibm_thinkpad_ec_found[1] == '6' ||
3346 ibm_thinkpad_ec_found[1] == '8' ||
3347 ibm_thinkpad_ec_found[1] == '0'))
3350 "fan_init: initial fan status is "
3351 "unknown, assuming it is in auto "
3353 tp_features.fan_ctrl_status_undef = 1;
3357 "ThinkPad ACPI EC access misbehaving, "
3358 "fan status and control unavailable\n");
3365 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
3366 fan_control_commands |=
3367 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
3370 /* gfan without sfan means no fan control */
3371 /* all other models implement TP EC 0x2f control */
3375 fan_control_access_mode =
3376 TPACPI_FAN_WR_ACPI_FANS;
3377 fan_control_commands |=
3378 TPACPI_FAN_CMD_SPEED |
3379 TPACPI_FAN_CMD_LEVEL |
3380 TPACPI_FAN_CMD_ENABLE;
3382 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
3383 fan_control_commands |=
3384 TPACPI_FAN_CMD_LEVEL |
3385 TPACPI_FAN_CMD_ENABLE;
3390 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
3391 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
3392 fan_control_access_mode != TPACPI_FAN_WR_NONE),
3393 fan_status_access_mode, fan_control_access_mode);
3395 /* fan control master switch */
3396 if (!fan_control_allowed) {
3397 fan_control_access_mode = TPACPI_FAN_WR_NONE;
3398 fan_control_commands = 0;
3399 dbg_printk(TPACPI_DBG_INIT,
3400 "fan control features disabled by parameter\n");
3403 /* update fan_control_desired_level */
3404 if (fan_status_access_mode != TPACPI_FAN_NONE)
3405 fan_get_status_safe(NULL);
3407 if (fan_status_access_mode != TPACPI_FAN_NONE ||
3408 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
3409 rc = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3412 rc = driver_create_file(&tpacpi_pdriver.driver,
3413 &driver_attr_fan_watchdog);
3422 * Call with fan_mutex held
3424 static void fan_update_desired_level(u8 status)
3427 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
3429 fan_control_desired_level = 7;
3431 fan_control_desired_level = status;
3435 static int fan_get_status(u8 *status)
3440 * Add TPACPI_FAN_RD_ACPI_FANS ? */
3442 switch (fan_status_access_mode) {
3443 case TPACPI_FAN_RD_ACPI_GFAN:
3444 /* 570, 600e/x, 770e, 770x */
3446 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
3454 case TPACPI_FAN_RD_TPEC:
3455 /* all except 570, 600e/x, 770e, 770x */
3456 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
3471 static int fan_get_status_safe(u8 *status)
3476 rc = mutex_lock_interruptible(&fan_mutex);
3479 rc = fan_get_status(&s);
3481 fan_update_desired_level(s);
3482 mutex_unlock(&fan_mutex);
3490 static void fan_exit(void)
3492 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
3494 /* FIXME: can we really do this unconditionally? */
3495 sysfs_remove_group(&tpacpi_pdev->dev.kobj, &fan_attr_group);
3496 driver_remove_file(&tpacpi_pdriver.driver, &driver_attr_fan_watchdog);
3498 cancel_delayed_work(&fan_watchdog_task);
3499 flush_scheduled_work();
3502 static int fan_get_speed(unsigned int *speed)
3506 switch (fan_status_access_mode) {
3507 case TPACPI_FAN_RD_TPEC:
3508 /* all except 570, 600e/x, 770e, 770x */
3509 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
3510 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
3514 *speed = (hi << 8) | lo;
3525 static void fan_watchdog_fire(struct work_struct *ignored)
3529 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
3530 rc = fan_set_enable();
3532 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
3533 "will try again later...\n", -rc);
3534 /* reschedule for later */
3535 fan_watchdog_reset();
3539 static void fan_watchdog_reset(void)
3541 static int fan_watchdog_active;
3543 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
3546 if (fan_watchdog_active)
3547 cancel_delayed_work(&fan_watchdog_task);
3549 if (fan_watchdog_maxinterval > 0) {
3550 fan_watchdog_active = 1;
3551 if (!schedule_delayed_work(&fan_watchdog_task,
3552 msecs_to_jiffies(fan_watchdog_maxinterval
3554 printk(IBM_ERR "failed to schedule the fan watchdog, "
3555 "watchdog will not trigger\n");
3558 fan_watchdog_active = 0;
3561 static int fan_set_level(int level)
3563 if (!fan_control_allowed)
3566 switch (fan_control_access_mode) {
3567 case TPACPI_FAN_WR_ACPI_SFAN:
3568 if (level >= 0 && level <= 7) {
3569 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
3575 case TPACPI_FAN_WR_ACPI_FANS:
3576 case TPACPI_FAN_WR_TPEC:
3577 if ((level != TP_EC_FAN_AUTO) &&
3578 (level != TP_EC_FAN_FULLSPEED) &&
3579 ((level < 0) || (level > 7)))
3582 /* safety net should the EC not support AUTO
3583 * or FULLSPEED mode bits and just ignore them */
3584 if (level & TP_EC_FAN_FULLSPEED)
3585 level |= 7; /* safety min speed 7 */
3586 else if (level & TP_EC_FAN_FULLSPEED)
3587 level |= 4; /* safety min speed 4 */
3589 if (!acpi_ec_write(fan_status_offset, level))
3592 tp_features.fan_ctrl_status_undef = 0;
3601 static int fan_set_level_safe(int level)
3605 if (!fan_control_allowed)
3608 rc = mutex_lock_interruptible(&fan_mutex);
3612 if (level == TPACPI_FAN_LAST_LEVEL)
3613 level = fan_control_desired_level;
3615 rc = fan_set_level(level);
3617 fan_update_desired_level(level);
3619 mutex_unlock(&fan_mutex);
3623 static int fan_set_enable(void)
3628 if (!fan_control_allowed)
3631 rc = mutex_lock_interruptible(&fan_mutex);
3635 switch (fan_control_access_mode) {
3636 case TPACPI_FAN_WR_ACPI_FANS:
3637 case TPACPI_FAN_WR_TPEC:
3638 rc = fan_get_status(&s);
3642 /* Don't go out of emergency fan mode */
3645 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
3648 if (!acpi_ec_write(fan_status_offset, s))
3651 tp_features.fan_ctrl_status_undef = 0;
3656 case TPACPI_FAN_WR_ACPI_SFAN:
3657 rc = fan_get_status(&s);
3663 /* Set fan to at least level 4 */
3666 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
3676 mutex_unlock(&fan_mutex);
3680 static int fan_set_disable(void)
3684 if (!fan_control_allowed)
3687 rc = mutex_lock_interruptible(&fan_mutex);
3692 switch (fan_control_access_mode) {
3693 case TPACPI_FAN_WR_ACPI_FANS:
3694 case TPACPI_FAN_WR_TPEC:
3695 if (!acpi_ec_write(fan_status_offset, 0x00))
3698 fan_control_desired_level = 0;
3699 tp_features.fan_ctrl_status_undef = 0;
3703 case TPACPI_FAN_WR_ACPI_SFAN:
3704 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
3707 fan_control_desired_level = 0;
3715 mutex_unlock(&fan_mutex);
3719 static int fan_set_speed(int speed)
3723 if (!fan_control_allowed)
3726 rc = mutex_lock_interruptible(&fan_mutex);
3731 switch (fan_control_access_mode) {
3732 case TPACPI_FAN_WR_ACPI_FANS:
3733 if (speed >= 0 && speed <= 65535) {
3734 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
3735 speed, speed, speed))
3745 mutex_unlock(&fan_mutex);
3749 static int fan_read(char *p)
3754 unsigned int speed = 0;
3756 switch (fan_status_access_mode) {
3757 case TPACPI_FAN_RD_ACPI_GFAN:
3758 /* 570, 600e/x, 770e, 770x */
3759 if ((rc = fan_get_status_safe(&status)) < 0)
3762 len += sprintf(p + len, "status:\t\t%s\n"
3764 (status != 0) ? "enabled" : "disabled", status);
3767 case TPACPI_FAN_RD_TPEC:
3768 /* all except 570, 600e/x, 770e, 770x */
3769 if ((rc = fan_get_status_safe(&status)) < 0)
3772 if (unlikely(tp_features.fan_ctrl_status_undef)) {
3773 if (status != fan_control_initial_status)
3774 tp_features.fan_ctrl_status_undef = 0;
3776 /* Return most likely status. In fact, it
3777 * might be the only possible status */
3778 status = TP_EC_FAN_AUTO;
3781 len += sprintf(p + len, "status:\t\t%s\n",
3782 (status != 0) ? "enabled" : "disabled");
3784 if ((rc = fan_get_speed(&speed)) < 0)
3787 len += sprintf(p + len, "speed:\t\t%d\n", speed);
3789 if (status & TP_EC_FAN_FULLSPEED)
3790 /* Disengaged mode takes precedence */
3791 len += sprintf(p + len, "level:\t\tdisengaged\n");
3792 else if (status & TP_EC_FAN_AUTO)
3793 len += sprintf(p + len, "level:\t\tauto\n");
3795 len += sprintf(p + len, "level:\t\t%d\n", status);
3798 case TPACPI_FAN_NONE:
3800 len += sprintf(p + len, "status:\t\tnot supported\n");
3803 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
3804 len += sprintf(p + len, "commands:\tlevel <level>");
3806 switch (fan_control_access_mode) {
3807 case TPACPI_FAN_WR_ACPI_SFAN:
3808 len += sprintf(p + len, " (<level> is 0-7)\n");
3812 len += sprintf(p + len, " (<level> is 0-7, "
3813 "auto, disengaged, full-speed)\n");
3818 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
3819 len += sprintf(p + len, "commands:\tenable, disable\n"
3820 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
3821 "1-120 (seconds))\n");
3823 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
3824 len += sprintf(p + len, "commands:\tspeed <speed>"
3825 " (<speed> is 0-65535)\n");
3830 static int fan_write_cmd_level(const char *cmd, int *rc)
3834 if (strlencmp(cmd, "level auto") == 0)
3835 level = TP_EC_FAN_AUTO;
3836 else if ((strlencmp(cmd, "level disengaged") == 0) |
3837 (strlencmp(cmd, "level full-speed") == 0))
3838 level = TP_EC_FAN_FULLSPEED;
3839 else if (sscanf(cmd, "level %d", &level) != 1)
3842 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
3843 printk(IBM_ERR "level command accepted for unsupported "
3844 "access mode %d", fan_control_access_mode);
3849 static int fan_write_cmd_enable(const char *cmd, int *rc)
3851 if (strlencmp(cmd, "enable") != 0)
3854 if ((*rc = fan_set_enable()) == -ENXIO)
3855 printk(IBM_ERR "enable command accepted for unsupported "
3856 "access mode %d", fan_control_access_mode);
3861 static int fan_write_cmd_disable(const char *cmd, int *rc)
3863 if (strlencmp(cmd, "disable") != 0)
3866 if ((*rc = fan_set_disable()) == -ENXIO)
3867 printk(IBM_ERR "disable command accepted for unsupported "
3868 "access mode %d", fan_control_access_mode);
3873 static int fan_write_cmd_speed(const char *cmd, int *rc)
3878 * Support speed <low> <medium> <high> ? */
3880 if (sscanf(cmd, "speed %d", &speed) != 1)
3883 if ((*rc = fan_set_speed(speed)) == -ENXIO)
3884 printk(IBM_ERR "speed command accepted for unsupported "
3885 "access mode %d", fan_control_access_mode);
3890 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
3894 if (sscanf(cmd, "watchdog %d", &interval) != 1)
3897 if (interval < 0 || interval > 120)
3900 fan_watchdog_maxinterval = interval;
3905 static int fan_write(char *buf)
3910 while (!rc && (cmd = next_cmd(&buf))) {
3911 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
3912 fan_write_cmd_level(cmd, &rc)) &&
3913 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
3914 (fan_write_cmd_enable(cmd, &rc) ||
3915 fan_write_cmd_disable(cmd, &rc) ||
3916 fan_write_cmd_watchdog(cmd, &rc))) &&
3917 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
3918 fan_write_cmd_speed(cmd, &rc))
3922 fan_watchdog_reset();
3928 static struct ibm_struct fan_driver_data = {
3935 /****************************************************************************
3936 ****************************************************************************
3940 ****************************************************************************
3941 ****************************************************************************/
3944 static struct proc_dir_entry *proc_dir;
3946 /* Subdriver registry */
3947 static LIST_HEAD(tpacpi_all_drivers);
3951 * Module and infrastructure proble, init and exit handling
3954 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
3955 static const char * __init str_supported(int is_supported)
3957 static char text_unsupported[] __initdata = "not supported";
3959 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
3961 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
3963 static int __init ibm_init(struct ibm_init_struct *iibm)
3966 struct ibm_struct *ibm = iibm->data;
3967 struct proc_dir_entry *entry;
3969 BUG_ON(ibm == NULL);
3971 INIT_LIST_HEAD(&ibm->all_drivers);
3973 if (ibm->flags.experimental && !experimental)
3976 dbg_printk(TPACPI_DBG_INIT,
3977 "probing for %s\n", ibm->name);
3980 ret = iibm->init(iibm);
3982 return 0; /* probe failed */
3986 ibm->flags.init_called = 1;
3990 if (ibm->acpi->hid) {
3991 ret = register_tpacpi_subdriver(ibm);
3996 if (ibm->acpi->notify) {
3997 ret = setup_acpi_notify(ibm);
3998 if (ret == -ENODEV) {
3999 printk(IBM_NOTICE "disabling subdriver %s\n",
4009 dbg_printk(TPACPI_DBG_INIT,
4010 "%s installed\n", ibm->name);
4013 entry = create_proc_entry(ibm->name,
4014 S_IFREG | S_IRUGO | S_IWUSR,
4017 printk(IBM_ERR "unable to create proc entry %s\n",
4022 entry->owner = THIS_MODULE;
4024 entry->read_proc = &dispatch_procfs_read;
4026 entry->write_proc = &dispatch_procfs_write;
4027 ibm->flags.proc_created = 1;
4030 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
4035 dbg_printk(TPACPI_DBG_INIT,
4036 "%s: at error exit path with result %d\n",
4040 return (ret < 0)? ret : 0;
4043 static void ibm_exit(struct ibm_struct *ibm)
4045 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
4047 list_del_init(&ibm->all_drivers);
4049 if (ibm->flags.acpi_notify_installed) {
4050 dbg_printk(TPACPI_DBG_EXIT,
4051 "%s: acpi_remove_notify_handler\n", ibm->name);
4053 acpi_remove_notify_handler(*ibm->acpi->handle,
4055 dispatch_acpi_notify);
4056 ibm->flags.acpi_notify_installed = 0;
4057 ibm->flags.acpi_notify_installed = 0;
4060 if (ibm->flags.proc_created) {
4061 dbg_printk(TPACPI_DBG_EXIT,
4062 "%s: remove_proc_entry\n", ibm->name);
4063 remove_proc_entry(ibm->name, proc_dir);
4064 ibm->flags.proc_created = 0;
4067 if (ibm->flags.acpi_driver_registered) {
4068 dbg_printk(TPACPI_DBG_EXIT,
4069 "%s: acpi_bus_unregister_driver\n", ibm->name);
4071 acpi_bus_unregister_driver(ibm->acpi->driver);
4072 kfree(ibm->acpi->driver);
4073 ibm->acpi->driver = NULL;
4074 ibm->flags.acpi_driver_registered = 0;
4077 if (ibm->flags.init_called && ibm->exit) {
4079 ibm->flags.init_called = 0;
4082 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
4087 static char *ibm_thinkpad_ec_found;
4089 static char* __init check_dmi_for_ec(void)
4091 struct dmi_device *dev = NULL;
4092 char ec_fw_string[18];
4095 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
4096 * X32 or newer, all Z series; Some models must have an
4097 * up-to-date BIOS or they will not be detected.
4099 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
4101 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
4102 if (sscanf(dev->name,
4103 "IBM ThinkPad Embedded Controller -[%17c",
4104 ec_fw_string) == 1) {
4105 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
4106 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
4107 return kstrdup(ec_fw_string, GFP_KERNEL);
4113 static int __init probe_for_thinkpad(void)
4121 * Non-ancient models have better DMI tagging, but very old models
4124 is_thinkpad = dmi_name_in_vendors("ThinkPad");
4126 /* ec is required because many other handles are relative to it */
4127 IBM_ACPIHANDLE_INIT(ec);
4131 "Not yet supported ThinkPad detected!\n");
4136 * Risks a regression on very old machines, but reduces potential
4137 * false positives a damn great deal
4140 is_thinkpad = dmi_name_in_vendors("IBM");
4142 if (!is_thinkpad && !force_load)
4149 /* Module init, exit, parameters */
4151 static struct ibm_init_struct ibms_init[] __initdata = {
4153 .init = thinkpad_acpi_driver_init,
4154 .data = &thinkpad_acpi_driver_data,
4157 .init = hotkey_init,
4158 .data = &hotkey_driver_data,
4161 .init = bluetooth_init,
4162 .data = &bluetooth_driver_data,
4166 .data = &wan_driver_data,
4170 .data = &video_driver_data,
4174 .data = &light_driver_data,
4176 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4179 .data = &dock_driver_data[0],
4183 .data = &dock_driver_data[1],
4186 #ifdef CONFIG_THINKPAD_ACPI_BAY
4189 .data = &bay_driver_data,
4194 .data = &cmos_driver_data,
4198 .data = &led_driver_data,
4202 .data = &beep_driver_data,
4205 .init = thermal_init,
4206 .data = &thermal_driver_data,
4209 .data = &ecdump_driver_data,
4212 .init = brightness_init,
4213 .data = &brightness_driver_data,
4216 .data = &volume_driver_data,
4220 .data = &fan_driver_data,
4224 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
4227 struct ibm_struct *ibm;
4229 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4230 ibm = ibms_init[i].data;
4231 BUG_ON(ibm == NULL);
4233 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
4234 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
4236 strcpy(ibms_init[i].param, val);
4237 strcat(ibms_init[i].param, ",");
4245 static int experimental;
4246 module_param(experimental, int, 0);
4248 static u32 dbg_level;
4249 module_param_named(debug, dbg_level, uint, 0);
4251 static int force_load;
4252 module_param(force_load, int, 0);
4254 static int fan_control_allowed;
4255 module_param_named(fan_control, fan_control_allowed, int, 0);
4257 #define IBM_PARAM(feature) \
4258 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
4261 IBM_PARAM(bluetooth);
4264 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4267 #ifdef CONFIG_THINKPAD_ACPI_BAY
4269 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4274 IBM_PARAM(brightness);
4278 static int __init thinkpad_acpi_module_init(void)
4282 /* Driver-level probe */
4283 ret = probe_for_thinkpad();
4287 /* Driver initialization */
4288 ibm_thinkpad_ec_found = check_dmi_for_ec();
4289 IBM_ACPIHANDLE_INIT(ecrd);
4290 IBM_ACPIHANDLE_INIT(ecwr);
4292 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
4294 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
4295 thinkpad_acpi_module_exit();
4298 proc_dir->owner = THIS_MODULE;
4300 ret = platform_driver_register(&tpacpi_pdriver);
4302 printk(IBM_ERR "unable to register platform driver\n");
4303 thinkpad_acpi_module_exit();
4306 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
4308 printk(IBM_ERR "unable to create sysfs driver attributes\n");
4309 thinkpad_acpi_module_exit();
4314 /* Device initialization */
4315 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
4317 if (IS_ERR(tpacpi_pdev)) {
4318 ret = PTR_ERR(tpacpi_pdev);
4320 printk(IBM_ERR "unable to register platform device\n");
4321 thinkpad_acpi_module_exit();
4324 tpacpi_hwmon = hwmon_device_register(&tpacpi_pdev->dev);
4325 if (IS_ERR(tpacpi_hwmon)) {
4326 ret = PTR_ERR(tpacpi_hwmon);
4327 tpacpi_hwmon = NULL;
4328 printk(IBM_ERR "unable to register hwmon device\n");
4329 thinkpad_acpi_module_exit();
4332 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
4333 ret = ibm_init(&ibms_init[i]);
4334 if (ret >= 0 && *ibms_init[i].param)
4335 ret = ibms_init[i].data->write(ibms_init[i].param);
4337 thinkpad_acpi_module_exit();
4345 static void thinkpad_acpi_module_exit(void)
4347 struct ibm_struct *ibm, *itmp;
4349 list_for_each_entry_safe_reverse(ibm, itmp,
4350 &tpacpi_all_drivers,
4355 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
4358 hwmon_device_unregister(tpacpi_hwmon);
4361 platform_device_unregister(tpacpi_pdev);
4363 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
4364 platform_driver_unregister(&tpacpi_pdriver);
4367 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
4369 kfree(ibm_thinkpad_ec_found);
4372 module_init(thinkpad_acpi_module_init);
4373 module_exit(thinkpad_acpi_module_exit);