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.16"
25 #define TPACPI_SYSFS_VERSION 0x010000
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;
415 ibm->acpi->driver->ops.add = &tpacpi_device_add;
417 rc = acpi_bus_register_driver(ibm->acpi->driver);
419 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
421 kfree(ibm->acpi->driver);
422 ibm->acpi->driver = NULL;
424 ibm->flags.acpi_driver_registered = 1;
430 /****************************************************************************
431 ****************************************************************************
435 ****************************************************************************
436 ****************************************************************************/
438 static int dispatch_procfs_read(char *page, char **start, off_t off,
439 int count, int *eof, void *data)
441 struct ibm_struct *ibm = data;
444 if (!ibm || !ibm->read)
447 len = ibm->read(page);
451 if (len <= off + count)
463 static int dispatch_procfs_write(struct file *file,
464 const char __user * userbuf,
465 unsigned long count, void *data)
467 struct ibm_struct *ibm = data;
471 if (!ibm || !ibm->write)
474 kernbuf = kmalloc(count + 2, GFP_KERNEL);
478 if (copy_from_user(kernbuf, userbuf, count)) {
484 strcat(kernbuf, ",");
485 ret = ibm->write(kernbuf);
494 static char *next_cmd(char **cmds)
499 while ((end = strchr(start, ',')) && end == start)
511 /****************************************************************************
512 ****************************************************************************
514 * Device model: input, hwmon and platform
516 ****************************************************************************
517 ****************************************************************************/
519 static struct platform_device *tpacpi_pdev;
520 static struct class_device *tpacpi_hwmon;
521 static struct input_dev *tpacpi_inputdev;
524 static int tpacpi_resume_handler(struct platform_device *pdev)
526 struct ibm_struct *ibm, *itmp;
528 list_for_each_entry_safe(ibm, itmp,
538 static struct platform_driver tpacpi_pdriver = {
540 .name = IBM_DRVR_NAME,
541 .owner = THIS_MODULE,
543 .resume = tpacpi_resume_handler,
547 /*************************************************************************
548 * thinkpad-acpi driver attributes
551 /* interface_version --------------------------------------------------- */
552 static ssize_t tpacpi_driver_interface_version_show(
553 struct device_driver *drv,
556 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
559 static DRIVER_ATTR(interface_version, S_IRUGO,
560 tpacpi_driver_interface_version_show, NULL);
562 /* debug_level --------------------------------------------------------- */
563 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
566 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
569 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
570 const char *buf, size_t count)
574 if (parse_strtoul(buf, 0xffff, &t))
582 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
583 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
585 /* version ------------------------------------------------------------- */
586 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
589 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
592 static DRIVER_ATTR(version, S_IRUGO,
593 tpacpi_driver_version_show, NULL);
595 /* --------------------------------------------------------------------- */
597 static struct driver_attribute* tpacpi_driver_attributes[] = {
598 &driver_attr_debug_level, &driver_attr_version,
599 &driver_attr_interface_version,
602 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
608 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
609 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
616 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
620 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
621 driver_remove_file(drv, tpacpi_driver_attributes[i]);
624 /*************************************************************************
625 * sysfs support helpers
628 struct attribute_set_obj {
629 struct attribute_set s;
631 } __attribute__((packed));
633 static struct attribute_set *create_attr_set(unsigned int max_members,
636 struct attribute_set_obj *sobj;
638 if (max_members == 0)
641 /* Allocates space for implicit NULL at the end too */
642 sobj = kzalloc(sizeof(struct attribute_set_obj) +
643 max_members * sizeof(struct attribute *),
647 sobj->s.max_members = max_members;
648 sobj->s.group.attrs = &sobj->a;
649 sobj->s.group.name = name;
654 /* not multi-threaded safe, use it in a single thread per set */
655 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
660 if (s->members >= s->max_members)
663 s->group.attrs[s->members] = attr;
669 static int add_many_to_attr_set(struct attribute_set* s,
670 struct attribute **attr,
675 for (i = 0; i < count; i++) {
676 res = add_to_attr_set(s, attr[i]);
684 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
686 sysfs_remove_group(kobj, &s->group);
690 static int parse_strtoul(const char *buf,
691 unsigned long max, unsigned long *value)
695 *value = simple_strtoul(buf, &endp, 0);
696 while (*endp && isspace(*endp))
698 if (*endp || *value > max)
704 /****************************************************************************
705 ****************************************************************************
709 ****************************************************************************
710 ****************************************************************************/
712 /*************************************************************************
713 * thinkpad-acpi init subdriver
716 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
718 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
719 printk(IBM_INFO "%s\n", IBM_URL);
721 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
722 (thinkpad_id.bios_version_str) ?
723 thinkpad_id.bios_version_str : "unknown",
724 (thinkpad_id.ec_version_str) ?
725 thinkpad_id.ec_version_str : "unknown");
727 if (thinkpad_id.vendor && thinkpad_id.model_str)
728 printk(IBM_INFO "%s %s\n",
729 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
730 "IBM" : ((thinkpad_id.vendor ==
731 PCI_VENDOR_ID_LENOVO) ?
732 "Lenovo" : "Unknown vendor"),
733 thinkpad_id.model_str);
738 static int thinkpad_acpi_driver_read(char *p)
742 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
743 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
748 static struct ibm_struct thinkpad_acpi_driver_data = {
750 .read = thinkpad_acpi_driver_read,
753 /*************************************************************************
757 static int hotkey_orig_status;
758 static u32 hotkey_orig_mask;
759 static u32 hotkey_all_mask;
760 static u32 hotkey_reserved_mask;
762 static u16 *hotkey_keycode_map;
764 static struct attribute_set *hotkey_dev_attributes;
766 static int hotkey_get_wlsw(int *status)
768 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
773 /* sysfs hotkey enable ------------------------------------------------- */
774 static ssize_t hotkey_enable_show(struct device *dev,
775 struct device_attribute *attr,
781 res = hotkey_get(&status, &mask);
785 return snprintf(buf, PAGE_SIZE, "%d\n", status);
788 static ssize_t hotkey_enable_store(struct device *dev,
789 struct device_attribute *attr,
790 const char *buf, size_t count)
796 if (parse_strtoul(buf, 1, &t))
799 res = hotkey_get(&status, &mask);
801 res = hotkey_set(t, mask);
803 return (res) ? res : count;
806 static struct device_attribute dev_attr_hotkey_enable =
807 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
808 hotkey_enable_show, hotkey_enable_store);
810 /* sysfs hotkey mask --------------------------------------------------- */
811 static ssize_t hotkey_mask_show(struct device *dev,
812 struct device_attribute *attr,
818 res = hotkey_get(&status, &mask);
822 return snprintf(buf, PAGE_SIZE, "0x%08x\n", mask);
825 static ssize_t hotkey_mask_store(struct device *dev,
826 struct device_attribute *attr,
827 const char *buf, size_t count)
833 if (parse_strtoul(buf, 0xffffffffUL, &t))
836 res = hotkey_get(&status, &mask);
838 hotkey_set(status, t);
840 return (res) ? res : count;
843 static struct device_attribute dev_attr_hotkey_mask =
844 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
845 hotkey_mask_show, hotkey_mask_store);
847 /* sysfs hotkey bios_enabled ------------------------------------------- */
848 static ssize_t hotkey_bios_enabled_show(struct device *dev,
849 struct device_attribute *attr,
852 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
855 static struct device_attribute dev_attr_hotkey_bios_enabled =
856 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
858 /* sysfs hotkey bios_mask ---------------------------------------------- */
859 static ssize_t hotkey_bios_mask_show(struct device *dev,
860 struct device_attribute *attr,
863 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
866 static struct device_attribute dev_attr_hotkey_bios_mask =
867 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
869 /* sysfs hotkey all_mask ----------------------------------------------- */
870 static ssize_t hotkey_all_mask_show(struct device *dev,
871 struct device_attribute *attr,
874 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_all_mask);
877 static struct device_attribute dev_attr_hotkey_all_mask =
878 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
880 /* sysfs hotkey recommended_mask --------------------------------------- */
881 static ssize_t hotkey_recommended_mask_show(struct device *dev,
882 struct device_attribute *attr,
885 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
886 hotkey_all_mask & ~hotkey_reserved_mask);
889 static struct device_attribute dev_attr_hotkey_recommended_mask =
890 __ATTR(hotkey_recommended_mask, S_IRUGO,
891 hotkey_recommended_mask_show, NULL);
893 /* sysfs hotkey radio_sw ----------------------------------------------- */
894 static ssize_t hotkey_radio_sw_show(struct device *dev,
895 struct device_attribute *attr,
899 res = hotkey_get_wlsw(&s);
903 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
906 static struct device_attribute dev_attr_hotkey_radio_sw =
907 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
909 /* sysfs hotkey report_mode -------------------------------------------- */
910 static ssize_t hotkey_report_mode_show(struct device *dev,
911 struct device_attribute *attr,
914 return snprintf(buf, PAGE_SIZE, "%d\n",
915 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
918 static struct device_attribute dev_attr_hotkey_report_mode =
919 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
921 /* --------------------------------------------------------------------- */
923 static struct attribute *hotkey_attributes[] __initdata = {
924 &dev_attr_hotkey_enable.attr,
925 &dev_attr_hotkey_report_mode.attr,
928 static struct attribute *hotkey_mask_attributes[] __initdata = {
929 &dev_attr_hotkey_mask.attr,
930 &dev_attr_hotkey_bios_enabled.attr,
931 &dev_attr_hotkey_bios_mask.attr,
932 &dev_attr_hotkey_all_mask.attr,
933 &dev_attr_hotkey_recommended_mask.attr,
936 static int __init hotkey_init(struct ibm_init_struct *iibm)
939 static u16 ibm_keycode_map[] __initdata = {
940 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
941 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
942 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
943 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
944 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
945 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
946 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
947 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
948 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
949 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
950 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
951 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
952 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
953 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
954 KEY_RESERVED, /* 0x14: VOLUME UP */
955 KEY_RESERVED, /* 0x15: VOLUME DOWN */
956 KEY_RESERVED, /* 0x16: MUTE */
957 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
958 /* (assignments unknown, please report if found) */
959 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
960 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
962 static u16 lenovo_keycode_map[] __initdata = {
963 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
964 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
965 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
966 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
967 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
968 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
969 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
970 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
971 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
972 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
973 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
974 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
975 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
976 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
977 KEY_RESERVED, /* 0x14: VOLUME UP */
978 KEY_RESERVED, /* 0x15: VOLUME DOWN */
979 KEY_RESERVED, /* 0x16: MUTE */
980 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
981 /* (assignments unknown, please report if found) */
982 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
983 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
986 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
987 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
988 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
993 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
995 BUG_ON(!tpacpi_inputdev);
997 IBM_ACPIHANDLE_INIT(hkey);
998 mutex_init(&hotkey_mutex);
1000 /* hotkey not supported on 570 */
1001 tp_features.hotkey = hkey_handle != NULL;
1003 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1004 str_supported(tp_features.hotkey));
1006 if (tp_features.hotkey) {
1007 hotkey_dev_attributes = create_attr_set(8, NULL);
1008 if (!hotkey_dev_attributes)
1010 res = add_many_to_attr_set(hotkey_dev_attributes,
1012 ARRAY_SIZE(hotkey_attributes));
1016 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1017 A30, R30, R31, T20-22, X20-21, X22-24 */
1018 tp_features.hotkey_mask =
1019 acpi_evalf(hkey_handle, NULL, "DHKN", "qv");
1021 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1022 str_supported(tp_features.hotkey_mask));
1024 if (tp_features.hotkey_mask) {
1025 /* MHKA available in A31, R40, R40e, T4x, X31, and later */
1026 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1028 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1031 res = hotkey_get(&hotkey_orig_status, &hotkey_orig_mask);
1032 if (!res && tp_features.hotkey_mask) {
1033 res = add_many_to_attr_set(hotkey_dev_attributes,
1034 hotkey_mask_attributes,
1035 ARRAY_SIZE(hotkey_mask_attributes));
1038 /* Not all thinkpads have a hardware radio switch */
1039 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1040 tp_features.hotkey_wlsw = 1;
1042 "radio switch found; radios are %s\n",
1043 enabled(status, 0));
1044 res = add_to_attr_set(hotkey_dev_attributes,
1045 &dev_attr_hotkey_radio_sw.attr);
1049 res = register_attr_set_with_sysfs(
1050 hotkey_dev_attributes,
1051 &tpacpi_pdev->dev.kobj);
1055 /* Set up key map */
1057 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1059 if (!hotkey_keycode_map) {
1060 printk(IBM_ERR "failed to allocate memory for key map\n");
1064 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1065 dbg_printk(TPACPI_DBG_INIT,
1066 "using Lenovo default hot key map\n");
1067 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1068 TPACPI_HOTKEY_MAP_SIZE);
1070 dbg_printk(TPACPI_DBG_INIT,
1071 "using IBM default hot key map\n");
1072 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1073 TPACPI_HOTKEY_MAP_SIZE);
1076 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1077 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1078 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1079 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1080 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1081 tpacpi_inputdev->keycode = hotkey_keycode_map;
1082 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1083 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1084 set_bit(hotkey_keycode_map[i],
1085 tpacpi_inputdev->keybit);
1087 if (i < sizeof(hotkey_reserved_mask)*8)
1088 hotkey_reserved_mask |= 1 << i;
1092 if (tp_features.hotkey_wlsw) {
1093 set_bit(EV_SW, tpacpi_inputdev->evbit);
1094 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
1097 dbg_printk(TPACPI_DBG_INIT,
1098 "enabling hot key handling\n");
1099 res = hotkey_set(1, (hotkey_all_mask & ~hotkey_reserved_mask)
1100 | hotkey_orig_mask);
1104 dbg_printk(TPACPI_DBG_INIT,
1105 "legacy hot key reporting over procfs %s\n",
1106 (hotkey_report_mode < 2) ?
1107 "enabled" : "disabled");
1110 return (tp_features.hotkey)? 0 : 1;
1113 static void hotkey_exit(void)
1117 if (tp_features.hotkey) {
1118 dbg_printk(TPACPI_DBG_EXIT, "restoring original hotkey mask\n");
1119 res = hotkey_set(hotkey_orig_status, hotkey_orig_mask);
1121 printk(IBM_ERR "failed to restore hotkey to BIOS defaults\n");
1124 if (hotkey_dev_attributes) {
1125 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1126 hotkey_dev_attributes = NULL;
1130 static void tpacpi_input_send_key(unsigned int scancode,
1131 unsigned int keycode)
1133 if (keycode != KEY_RESERVED) {
1134 input_report_key(tpacpi_inputdev, keycode, 1);
1135 if (keycode == KEY_UNKNOWN)
1136 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1138 input_sync(tpacpi_inputdev);
1140 input_report_key(tpacpi_inputdev, keycode, 0);
1141 if (keycode == KEY_UNKNOWN)
1142 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1144 input_sync(tpacpi_inputdev);
1148 static void tpacpi_input_send_radiosw(void)
1152 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw))
1153 input_report_switch(tpacpi_inputdev,
1157 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
1160 unsigned int keycode, scancode;
1161 int send_acpi_ev = 0;
1163 if (event == 0x80 && acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
1164 switch (hkey >> 12) {
1166 /* 0x1000-0x1FFF: key presses */
1167 scancode = hkey & 0xfff;
1168 if (scancode > 0 && scancode < 0x21) {
1170 keycode = hotkey_keycode_map[scancode];
1171 tpacpi_input_send_key(scancode, keycode);
1174 "hotkey 0x%04x out of range for keyboard map\n",
1180 /* 0x5000-0x5FFF: LID */
1181 /* we don't handle it through this path, just
1182 * eat up known LID events */
1183 if (hkey != 0x5001 && hkey != 0x5002) {
1185 "unknown LID-related hotkey event: 0x%04x\n",
1191 /* 0x7000-0x7FFF: misc */
1192 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
1193 tpacpi_input_send_radiosw();
1196 /* fallthrough to default */
1198 /* case 2: dock-related */
1199 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
1200 /* case 3: ultra-bay related. maybe bay in dock? */
1201 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
1202 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
1206 printk(IBM_ERR "unknown hotkey notification event %d\n", event);
1212 if (send_acpi_ev || hotkey_report_mode < 2)
1213 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
1215 /* netlink events */
1217 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
1218 ibm->acpi->device->dev.bus_id,
1223 static void hotkey_resume(void)
1225 tpacpi_input_send_radiosw();
1229 * Call with hotkey_mutex held
1231 static int hotkey_get(int *status, u32 *mask)
1233 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1236 if (tp_features.hotkey_mask)
1237 if (!acpi_evalf(hkey_handle, mask, "DHKN", "d"))
1244 * Call with hotkey_mutex held
1246 static int hotkey_set(int status, u32 mask)
1250 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1253 if (tp_features.hotkey_mask)
1254 for (i = 0; i < 32; i++) {
1255 int bit = ((1 << i) & mask) != 0;
1256 if (!acpi_evalf(hkey_handle,
1257 NULL, "MHKM", "vdd", i + 1, bit))
1264 /* procfs -------------------------------------------------------------- */
1265 static int hotkey_read(char *p)
1271 if (!tp_features.hotkey) {
1272 len += sprintf(p + len, "status:\t\tnot supported\n");
1276 res = mutex_lock_interruptible(&hotkey_mutex);
1279 res = hotkey_get(&status, &mask);
1280 mutex_unlock(&hotkey_mutex);
1284 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
1285 if (tp_features.hotkey_mask) {
1286 len += sprintf(p + len, "mask:\t\t0x%08x\n", mask);
1287 len += sprintf(p + len,
1288 "commands:\tenable, disable, reset, <mask>\n");
1290 len += sprintf(p + len, "mask:\t\tnot supported\n");
1291 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
1297 static int hotkey_write(char *buf)
1304 if (!tp_features.hotkey)
1307 res = mutex_lock_interruptible(&hotkey_mutex);
1311 res = hotkey_get(&status, &mask);
1316 while ((cmd = next_cmd(&buf))) {
1317 if (strlencmp(cmd, "enable") == 0) {
1319 } else if (strlencmp(cmd, "disable") == 0) {
1321 } else if (strlencmp(cmd, "reset") == 0) {
1322 status = hotkey_orig_status;
1323 mask = hotkey_orig_mask;
1324 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
1326 } else if (sscanf(cmd, "%x", &mask) == 1) {
1336 res = hotkey_set(status, mask);
1339 mutex_unlock(&hotkey_mutex);
1343 static const struct acpi_device_id ibm_htk_device_ids[] = {
1348 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
1349 .hid = ibm_htk_device_ids,
1350 .notify = hotkey_notify,
1351 .handle = &hkey_handle,
1352 .type = ACPI_DEVICE_NOTIFY,
1355 static struct ibm_struct hotkey_driver_data = {
1357 .read = hotkey_read,
1358 .write = hotkey_write,
1359 .exit = hotkey_exit,
1360 .resume = hotkey_resume,
1361 .acpi = &ibm_hotkey_acpidriver,
1364 /*************************************************************************
1365 * Bluetooth subdriver
1368 /* sysfs bluetooth enable ---------------------------------------------- */
1369 static ssize_t bluetooth_enable_show(struct device *dev,
1370 struct device_attribute *attr,
1375 status = bluetooth_get_radiosw();
1379 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1382 static ssize_t bluetooth_enable_store(struct device *dev,
1383 struct device_attribute *attr,
1384 const char *buf, size_t count)
1389 if (parse_strtoul(buf, 1, &t))
1392 res = bluetooth_set_radiosw(t);
1394 return (res) ? res : count;
1397 static struct device_attribute dev_attr_bluetooth_enable =
1398 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
1399 bluetooth_enable_show, bluetooth_enable_store);
1401 /* --------------------------------------------------------------------- */
1403 static struct attribute *bluetooth_attributes[] = {
1404 &dev_attr_bluetooth_enable.attr,
1408 static const struct attribute_group bluetooth_attr_group = {
1409 .attrs = bluetooth_attributes,
1412 static int __init bluetooth_init(struct ibm_init_struct *iibm)
1417 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
1419 IBM_ACPIHANDLE_INIT(hkey);
1421 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1422 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
1423 tp_features.bluetooth = hkey_handle &&
1424 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
1426 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
1427 str_supported(tp_features.bluetooth),
1430 if (tp_features.bluetooth) {
1431 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
1432 /* no bluetooth hardware present in system */
1433 tp_features.bluetooth = 0;
1434 dbg_printk(TPACPI_DBG_INIT,
1435 "bluetooth hardware not installed\n");
1437 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1438 &bluetooth_attr_group);
1444 return (tp_features.bluetooth)? 0 : 1;
1447 static void bluetooth_exit(void)
1449 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1450 &bluetooth_attr_group);
1453 static int bluetooth_get_radiosw(void)
1457 if (!tp_features.bluetooth)
1460 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1463 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
1466 static int bluetooth_set_radiosw(int radio_on)
1470 if (!tp_features.bluetooth)
1473 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
1476 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
1478 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
1479 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
1485 /* procfs -------------------------------------------------------------- */
1486 static int bluetooth_read(char *p)
1489 int status = bluetooth_get_radiosw();
1491 if (!tp_features.bluetooth)
1492 len += sprintf(p + len, "status:\t\tnot supported\n");
1494 len += sprintf(p + len, "status:\t\t%s\n",
1495 (status)? "enabled" : "disabled");
1496 len += sprintf(p + len, "commands:\tenable, disable\n");
1502 static int bluetooth_write(char *buf)
1506 if (!tp_features.bluetooth)
1509 while ((cmd = next_cmd(&buf))) {
1510 if (strlencmp(cmd, "enable") == 0) {
1511 bluetooth_set_radiosw(1);
1512 } else if (strlencmp(cmd, "disable") == 0) {
1513 bluetooth_set_radiosw(0);
1521 static struct ibm_struct bluetooth_driver_data = {
1522 .name = "bluetooth",
1523 .read = bluetooth_read,
1524 .write = bluetooth_write,
1525 .exit = bluetooth_exit,
1528 /*************************************************************************
1532 /* sysfs wan enable ---------------------------------------------------- */
1533 static ssize_t wan_enable_show(struct device *dev,
1534 struct device_attribute *attr,
1539 status = wan_get_radiosw();
1543 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
1546 static ssize_t wan_enable_store(struct device *dev,
1547 struct device_attribute *attr,
1548 const char *buf, size_t count)
1553 if (parse_strtoul(buf, 1, &t))
1556 res = wan_set_radiosw(t);
1558 return (res) ? res : count;
1561 static struct device_attribute dev_attr_wan_enable =
1562 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
1563 wan_enable_show, wan_enable_store);
1565 /* --------------------------------------------------------------------- */
1567 static struct attribute *wan_attributes[] = {
1568 &dev_attr_wan_enable.attr,
1572 static const struct attribute_group wan_attr_group = {
1573 .attrs = wan_attributes,
1576 static int __init wan_init(struct ibm_init_struct *iibm)
1581 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
1583 IBM_ACPIHANDLE_INIT(hkey);
1585 tp_features.wan = hkey_handle &&
1586 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
1588 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
1589 str_supported(tp_features.wan),
1592 if (tp_features.wan) {
1593 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
1594 /* no wan hardware present in system */
1595 tp_features.wan = 0;
1596 dbg_printk(TPACPI_DBG_INIT,
1597 "wan hardware not installed\n");
1599 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
1606 return (tp_features.wan)? 0 : 1;
1609 static void wan_exit(void)
1611 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
1615 static int wan_get_radiosw(void)
1619 if (!tp_features.wan)
1622 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1625 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
1628 static int wan_set_radiosw(int radio_on)
1632 if (!tp_features.wan)
1635 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
1638 status |= TP_ACPI_WANCARD_RADIOSSW;
1640 status &= ~TP_ACPI_WANCARD_RADIOSSW;
1641 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
1647 /* procfs -------------------------------------------------------------- */
1648 static int wan_read(char *p)
1651 int status = wan_get_radiosw();
1653 if (!tp_features.wan)
1654 len += sprintf(p + len, "status:\t\tnot supported\n");
1656 len += sprintf(p + len, "status:\t\t%s\n",
1657 (status)? "enabled" : "disabled");
1658 len += sprintf(p + len, "commands:\tenable, disable\n");
1664 static int wan_write(char *buf)
1668 if (!tp_features.wan)
1671 while ((cmd = next_cmd(&buf))) {
1672 if (strlencmp(cmd, "enable") == 0) {
1674 } else if (strlencmp(cmd, "disable") == 0) {
1683 static struct ibm_struct wan_driver_data = {
1688 .flags.experimental = 1,
1691 /*************************************************************************
1695 static enum video_access_mode video_supported;
1696 static int video_orig_autosw;
1698 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
1699 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
1700 "\\_SB.PCI0.VID0", /* 770e */
1701 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
1702 "\\_SB.PCI0.AGP.VID", /* all others */
1705 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
1707 static int __init video_init(struct ibm_init_struct *iibm)
1711 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
1713 IBM_ACPIHANDLE_INIT(vid);
1714 IBM_ACPIHANDLE_INIT(vid2);
1716 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
1717 /* G41, assume IVGA doesn't change */
1718 vid_handle = vid2_handle;
1721 /* video switching not supported on R30, R31 */
1722 video_supported = TPACPI_VIDEO_NONE;
1723 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
1725 video_supported = TPACPI_VIDEO_570;
1726 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
1727 /* 600e/x, 770e, 770x */
1728 video_supported = TPACPI_VIDEO_770;
1731 video_supported = TPACPI_VIDEO_NEW;
1733 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
1734 str_supported(video_supported != TPACPI_VIDEO_NONE),
1737 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
1740 static void video_exit(void)
1742 dbg_printk(TPACPI_DBG_EXIT,
1743 "restoring original video autoswitch mode\n");
1744 if (video_autosw_set(video_orig_autosw))
1745 printk(IBM_ERR "error while trying to restore original "
1746 "video autoswitch mode\n");
1749 static int video_outputsw_get(void)
1754 switch (video_supported) {
1755 case TPACPI_VIDEO_570:
1756 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
1757 TP_ACPI_VIDEO_570_PHSCMD))
1759 status = i & TP_ACPI_VIDEO_570_PHSMASK;
1761 case TPACPI_VIDEO_770:
1762 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
1765 status |= TP_ACPI_VIDEO_S_LCD;
1766 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
1769 status |= TP_ACPI_VIDEO_S_CRT;
1771 case TPACPI_VIDEO_NEW:
1772 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
1773 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
1776 status |= TP_ACPI_VIDEO_S_CRT;
1778 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
1779 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
1782 status |= TP_ACPI_VIDEO_S_LCD;
1783 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
1786 status |= TP_ACPI_VIDEO_S_DVI;
1795 static int video_outputsw_set(int status)
1800 switch (video_supported) {
1801 case TPACPI_VIDEO_570:
1802 res = acpi_evalf(NULL, NULL,
1803 "\\_SB.PHS2", "vdd",
1804 TP_ACPI_VIDEO_570_PHS2CMD,
1805 status | TP_ACPI_VIDEO_570_PHS2SET);
1807 case TPACPI_VIDEO_770:
1808 autosw = video_autosw_get();
1812 res = video_autosw_set(1);
1815 res = acpi_evalf(vid_handle, NULL,
1816 "ASWT", "vdd", status * 0x100, 0);
1817 if (!autosw && video_autosw_set(autosw)) {
1818 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1822 case TPACPI_VIDEO_NEW:
1823 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
1824 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
1830 return (res)? 0 : -EIO;
1833 static int video_autosw_get(void)
1837 switch (video_supported) {
1838 case TPACPI_VIDEO_570:
1839 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
1842 case TPACPI_VIDEO_770:
1843 case TPACPI_VIDEO_NEW:
1844 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
1854 static int video_autosw_set(int enable)
1856 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
1861 static int video_outputsw_cycle(void)
1863 int autosw = video_autosw_get();
1869 switch (video_supported) {
1870 case TPACPI_VIDEO_570:
1871 res = video_autosw_set(1);
1874 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
1876 case TPACPI_VIDEO_770:
1877 case TPACPI_VIDEO_NEW:
1878 res = video_autosw_set(1);
1881 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
1886 if (!autosw && video_autosw_set(autosw)) {
1887 printk(IBM_ERR "video auto-switch left enabled due to error\n");
1891 return (res)? 0 : -EIO;
1894 static int video_expand_toggle(void)
1896 switch (video_supported) {
1897 case TPACPI_VIDEO_570:
1898 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
1900 case TPACPI_VIDEO_770:
1901 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
1903 case TPACPI_VIDEO_NEW:
1904 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
1912 static int video_read(char *p)
1917 if (video_supported == TPACPI_VIDEO_NONE) {
1918 len += sprintf(p + len, "status:\t\tnot supported\n");
1922 status = video_outputsw_get();
1926 autosw = video_autosw_get();
1930 len += sprintf(p + len, "status:\t\tsupported\n");
1931 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
1932 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
1933 if (video_supported == TPACPI_VIDEO_NEW)
1934 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
1935 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
1936 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
1937 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
1938 if (video_supported == TPACPI_VIDEO_NEW)
1939 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
1940 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
1941 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
1946 static int video_write(char *buf)
1949 int enable, disable, status;
1952 if (video_supported == TPACPI_VIDEO_NONE)
1958 while ((cmd = next_cmd(&buf))) {
1959 if (strlencmp(cmd, "lcd_enable") == 0) {
1960 enable |= TP_ACPI_VIDEO_S_LCD;
1961 } else if (strlencmp(cmd, "lcd_disable") == 0) {
1962 disable |= TP_ACPI_VIDEO_S_LCD;
1963 } else if (strlencmp(cmd, "crt_enable") == 0) {
1964 enable |= TP_ACPI_VIDEO_S_CRT;
1965 } else if (strlencmp(cmd, "crt_disable") == 0) {
1966 disable |= TP_ACPI_VIDEO_S_CRT;
1967 } else if (video_supported == TPACPI_VIDEO_NEW &&
1968 strlencmp(cmd, "dvi_enable") == 0) {
1969 enable |= TP_ACPI_VIDEO_S_DVI;
1970 } else if (video_supported == TPACPI_VIDEO_NEW &&
1971 strlencmp(cmd, "dvi_disable") == 0) {
1972 disable |= TP_ACPI_VIDEO_S_DVI;
1973 } else if (strlencmp(cmd, "auto_enable") == 0) {
1974 res = video_autosw_set(1);
1977 } else if (strlencmp(cmd, "auto_disable") == 0) {
1978 res = video_autosw_set(0);
1981 } else if (strlencmp(cmd, "video_switch") == 0) {
1982 res = video_outputsw_cycle();
1985 } else if (strlencmp(cmd, "expand_toggle") == 0) {
1986 res = video_expand_toggle();
1993 if (enable || disable) {
1994 status = video_outputsw_get();
1997 res = video_outputsw_set((status & ~disable) | enable);
2005 static struct ibm_struct video_driver_data = {
2008 .write = video_write,
2012 /*************************************************************************
2013 * Light (thinklight) subdriver
2016 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2017 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2019 static int __init light_init(struct ibm_init_struct *iibm)
2021 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2023 IBM_ACPIHANDLE_INIT(ledb);
2024 IBM_ACPIHANDLE_INIT(lght);
2025 IBM_ACPIHANDLE_INIT(cmos);
2027 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2028 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2030 if (tp_features.light)
2031 /* light status not supported on
2032 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2033 tp_features.light_status =
2034 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2036 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2037 str_supported(tp_features.light));
2039 return (tp_features.light)? 0 : 1;
2042 static int light_read(char *p)
2047 if (!tp_features.light) {
2048 len += sprintf(p + len, "status:\t\tnot supported\n");
2049 } else if (!tp_features.light_status) {
2050 len += sprintf(p + len, "status:\t\tunknown\n");
2051 len += sprintf(p + len, "commands:\ton, off\n");
2053 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2055 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2056 len += sprintf(p + len, "commands:\ton, off\n");
2062 static int light_write(char *buf)
2064 int cmos_cmd, lght_cmd;
2068 if (!tp_features.light)
2071 while ((cmd = next_cmd(&buf))) {
2072 if (strlencmp(cmd, "on") == 0) {
2075 } else if (strlencmp(cmd, "off") == 0) {
2081 success = cmos_handle ?
2082 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
2083 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
2091 static struct ibm_struct light_driver_data = {
2094 .write = light_write,
2097 /*************************************************************************
2101 #ifdef CONFIG_THINKPAD_ACPI_DOCK
2103 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
2104 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
2105 "\\_SB.PCI0.PCI1.DOCK", /* all others */
2106 "\\_SB.PCI.ISA.SLCE", /* 570 */
2107 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
2109 /* don't list other alternatives as we install a notify handler on the 570 */
2110 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
2112 static const struct acpi_device_id ibm_pci_device_ids[] = {
2113 {PCI_ROOT_HID_STRING, 0},
2117 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
2119 .notify = dock_notify,
2120 .handle = &dock_handle,
2121 .type = ACPI_SYSTEM_NOTIFY,
2124 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
2125 * We just use it to get notifications of dock hotplug
2126 * in very old thinkpads */
2127 .hid = ibm_pci_device_ids,
2128 .notify = dock_notify,
2129 .handle = &pci_handle,
2130 .type = ACPI_SYSTEM_NOTIFY,
2134 static struct ibm_struct dock_driver_data[2] = {
2138 .write = dock_write,
2139 .acpi = &ibm_dock_acpidriver[0],
2143 .acpi = &ibm_dock_acpidriver[1],
2147 #define dock_docked() (_sta(dock_handle) & 1)
2149 static int __init dock_init(struct ibm_init_struct *iibm)
2151 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
2153 IBM_ACPIHANDLE_INIT(dock);
2155 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
2156 str_supported(dock_handle != NULL));
2158 return (dock_handle)? 0 : 1;
2161 static int __init dock_init2(struct ibm_init_struct *iibm)
2165 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
2167 if (dock_driver_data[0].flags.acpi_driver_registered &&
2168 dock_driver_data[0].flags.acpi_notify_installed) {
2169 IBM_ACPIHANDLE_INIT(pci);
2170 dock2_needed = (pci_handle != NULL);
2171 vdbg_printk(TPACPI_DBG_INIT,
2172 "dock PCI handler for the TP 570 is %s\n",
2173 str_supported(dock2_needed));
2175 vdbg_printk(TPACPI_DBG_INIT,
2176 "dock subdriver part 2 not required\n");
2180 return (dock2_needed)? 0 : 1;
2183 static void dock_notify(struct ibm_struct *ibm, u32 event)
2185 int docked = dock_docked();
2186 int pci = ibm->acpi->hid && ibm->acpi->device &&
2187 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
2190 if (event == 1 && !pci) /* 570 */
2191 data = 1; /* button */
2192 else if (event == 1 && pci) /* 570 */
2193 data = 3; /* dock */
2194 else if (event == 3 && docked)
2195 data = 1; /* button */
2196 else if (event == 3 && !docked)
2197 data = 2; /* undock */
2198 else if (event == 0 && docked)
2199 data = 3; /* dock */
2201 printk(IBM_ERR "unknown dock event %d, status %d\n",
2202 event, _sta(dock_handle));
2203 data = 0; /* unknown */
2205 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
2206 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2207 ibm->acpi->device->dev.bus_id,
2211 static int dock_read(char *p)
2214 int docked = dock_docked();
2217 len += sprintf(p + len, "status:\t\tnot supported\n");
2219 len += sprintf(p + len, "status:\t\tundocked\n");
2221 len += sprintf(p + len, "status:\t\tdocked\n");
2222 len += sprintf(p + len, "commands:\tdock, undock\n");
2228 static int dock_write(char *buf)
2235 while ((cmd = next_cmd(&buf))) {
2236 if (strlencmp(cmd, "undock") == 0) {
2237 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
2238 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
2240 } else if (strlencmp(cmd, "dock") == 0) {
2241 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
2250 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
2252 /*************************************************************************
2256 #ifdef CONFIG_THINKPAD_ACPI_BAY
2257 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
2258 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
2259 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
2260 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
2261 ); /* A21e, R30, R31 */
2262 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
2263 "_EJ0", /* all others */
2264 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
2265 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
2266 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
2268 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
2272 static int __init bay_init(struct ibm_init_struct *iibm)
2274 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
2276 IBM_ACPIHANDLE_INIT(bay);
2278 IBM_ACPIHANDLE_INIT(bay_ej);
2279 IBM_ACPIHANDLE_INIT(bay2);
2281 IBM_ACPIHANDLE_INIT(bay2_ej);
2283 tp_features.bay_status = bay_handle &&
2284 acpi_evalf(bay_handle, NULL, "_STA", "qv");
2285 tp_features.bay_status2 = bay2_handle &&
2286 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
2288 tp_features.bay_eject = bay_handle && bay_ej_handle &&
2289 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
2290 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
2291 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
2293 vdbg_printk(TPACPI_DBG_INIT,
2294 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
2295 str_supported(tp_features.bay_status),
2296 str_supported(tp_features.bay_eject),
2297 str_supported(tp_features.bay_status2),
2298 str_supported(tp_features.bay_eject2));
2300 return (tp_features.bay_status || tp_features.bay_eject ||
2301 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
2304 static void bay_notify(struct ibm_struct *ibm, u32 event)
2306 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
2307 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2308 ibm->acpi->device->dev.bus_id,
2312 #define bay_occupied(b) (_sta(b##_handle) & 1)
2314 static int bay_read(char *p)
2317 int occupied = bay_occupied(bay);
2318 int occupied2 = bay_occupied(bay2);
2321 len += sprintf(p + len, "status:\t\t%s\n",
2322 tp_features.bay_status ?
2323 (occupied ? "occupied" : "unoccupied") :
2325 if (tp_features.bay_status2)
2326 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
2327 "occupied" : "unoccupied");
2329 eject = tp_features.bay_eject && occupied;
2330 eject2 = tp_features.bay_eject2 && occupied2;
2332 if (eject && eject2)
2333 len += sprintf(p + len, "commands:\teject, eject2\n");
2335 len += sprintf(p + len, "commands:\teject\n");
2337 len += sprintf(p + len, "commands:\teject2\n");
2342 static int bay_write(char *buf)
2346 if (!tp_features.bay_eject && !tp_features.bay_eject2)
2349 while ((cmd = next_cmd(&buf))) {
2350 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
2351 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
2353 } else if (tp_features.bay_eject2 &&
2354 strlencmp(cmd, "eject2") == 0) {
2355 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
2364 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
2365 .notify = bay_notify,
2366 .handle = &bay_handle,
2367 .type = ACPI_SYSTEM_NOTIFY,
2370 static struct ibm_struct bay_driver_data = {
2374 .acpi = &ibm_bay_acpidriver,
2377 #endif /* CONFIG_THINKPAD_ACPI_BAY */
2379 /*************************************************************************
2383 /* sysfs cmos_command -------------------------------------------------- */
2384 static ssize_t cmos_command_store(struct device *dev,
2385 struct device_attribute *attr,
2386 const char *buf, size_t count)
2388 unsigned long cmos_cmd;
2391 if (parse_strtoul(buf, 21, &cmos_cmd))
2394 res = issue_thinkpad_cmos_command(cmos_cmd);
2395 return (res)? res : count;
2398 static struct device_attribute dev_attr_cmos_command =
2399 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
2401 /* --------------------------------------------------------------------- */
2403 static int __init cmos_init(struct ibm_init_struct *iibm)
2407 vdbg_printk(TPACPI_DBG_INIT,
2408 "initializing cmos commands subdriver\n");
2410 IBM_ACPIHANDLE_INIT(cmos);
2412 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
2413 str_supported(cmos_handle != NULL));
2415 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2419 return (cmos_handle)? 0 : 1;
2422 static void cmos_exit(void)
2424 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
2427 static int cmos_read(char *p)
2431 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2432 R30, R31, T20-22, X20-21 */
2434 len += sprintf(p + len, "status:\t\tnot supported\n");
2436 len += sprintf(p + len, "status:\t\tsupported\n");
2437 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
2443 static int cmos_write(char *buf)
2448 while ((cmd = next_cmd(&buf))) {
2449 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
2450 cmos_cmd >= 0 && cmos_cmd <= 21) {
2455 res = issue_thinkpad_cmos_command(cmos_cmd);
2463 static struct ibm_struct cmos_driver_data = {
2466 .write = cmos_write,
2470 /*************************************************************************
2474 static enum led_access_mode led_supported;
2476 IBM_HANDLE(led, ec, "SLED", /* 570 */
2477 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2478 "LED", /* all others */
2481 static int __init led_init(struct ibm_init_struct *iibm)
2483 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
2485 IBM_ACPIHANDLE_INIT(led);
2488 /* led not supported on R30, R31 */
2489 led_supported = TPACPI_LED_NONE;
2490 else if (strlencmp(led_path, "SLED") == 0)
2492 led_supported = TPACPI_LED_570;
2493 else if (strlencmp(led_path, "SYSL") == 0)
2494 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
2495 led_supported = TPACPI_LED_OLD;
2498 led_supported = TPACPI_LED_NEW;
2500 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
2501 str_supported(led_supported), led_supported);
2503 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
2506 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
2508 static int led_read(char *p)
2512 if (!led_supported) {
2513 len += sprintf(p + len, "status:\t\tnot supported\n");
2516 len += sprintf(p + len, "status:\t\tsupported\n");
2518 if (led_supported == TPACPI_LED_570) {
2521 for (i = 0; i < 8; i++) {
2522 if (!acpi_evalf(ec_handle,
2523 &status, "GLED", "dd", 1 << i))
2525 len += sprintf(p + len, "%d:\t\t%s\n",
2526 i, led_status(status));
2530 len += sprintf(p + len, "commands:\t"
2531 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
2536 /* off, on, blink */
2537 static const int led_sled_arg1[] = { 0, 1, 3 };
2538 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
2539 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
2540 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
2542 static int led_write(char *buf)
2550 while ((cmd = next_cmd(&buf))) {
2551 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
2554 if (strstr(cmd, "off")) {
2556 } else if (strstr(cmd, "on")) {
2558 } else if (strstr(cmd, "blink")) {
2563 if (led_supported == TPACPI_LED_570) {
2566 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2567 led, led_sled_arg1[ind]))
2569 } else if (led_supported == TPACPI_LED_OLD) {
2570 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
2572 ret = ec_write(TPACPI_LED_EC_HLMS, led);
2575 ec_write(TPACPI_LED_EC_HLBL,
2576 led * led_exp_hlbl[ind]);
2579 ec_write(TPACPI_LED_EC_HLCL,
2580 led * led_exp_hlcl[ind]);
2585 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
2586 led, led_led_arg1[ind]))
2594 static struct ibm_struct led_driver_data = {
2600 /*************************************************************************
2604 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
2606 static int __init beep_init(struct ibm_init_struct *iibm)
2608 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
2610 IBM_ACPIHANDLE_INIT(beep);
2612 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
2613 str_supported(beep_handle != NULL));
2615 return (beep_handle)? 0 : 1;
2618 static int beep_read(char *p)
2623 len += sprintf(p + len, "status:\t\tnot supported\n");
2625 len += sprintf(p + len, "status:\t\tsupported\n");
2626 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
2632 static int beep_write(char *buf)
2640 while ((cmd = next_cmd(&buf))) {
2641 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
2642 beep_cmd >= 0 && beep_cmd <= 17) {
2646 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
2653 static struct ibm_struct beep_driver_data = {
2656 .write = beep_write,
2659 /*************************************************************************
2663 static enum thermal_access_mode thermal_read_mode;
2665 /* sysfs temp##_input -------------------------------------------------- */
2667 static ssize_t thermal_temp_input_show(struct device *dev,
2668 struct device_attribute *attr,
2671 struct sensor_device_attribute *sensor_attr =
2672 to_sensor_dev_attr(attr);
2673 int idx = sensor_attr->index;
2677 res = thermal_get_sensor(idx, &value);
2680 if (value == TP_EC_THERMAL_TMP_NA * 1000)
2683 return snprintf(buf, PAGE_SIZE, "%d\n", value);
2686 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
2687 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
2689 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
2690 THERMAL_SENSOR_ATTR_TEMP(1, 0),
2691 THERMAL_SENSOR_ATTR_TEMP(2, 1),
2692 THERMAL_SENSOR_ATTR_TEMP(3, 2),
2693 THERMAL_SENSOR_ATTR_TEMP(4, 3),
2694 THERMAL_SENSOR_ATTR_TEMP(5, 4),
2695 THERMAL_SENSOR_ATTR_TEMP(6, 5),
2696 THERMAL_SENSOR_ATTR_TEMP(7, 6),
2697 THERMAL_SENSOR_ATTR_TEMP(8, 7),
2698 THERMAL_SENSOR_ATTR_TEMP(9, 8),
2699 THERMAL_SENSOR_ATTR_TEMP(10, 9),
2700 THERMAL_SENSOR_ATTR_TEMP(11, 10),
2701 THERMAL_SENSOR_ATTR_TEMP(12, 11),
2702 THERMAL_SENSOR_ATTR_TEMP(13, 12),
2703 THERMAL_SENSOR_ATTR_TEMP(14, 13),
2704 THERMAL_SENSOR_ATTR_TEMP(15, 14),
2705 THERMAL_SENSOR_ATTR_TEMP(16, 15),
2708 #define THERMAL_ATTRS(X) \
2709 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
2711 static struct attribute *thermal_temp_input_attr[] = {
2731 static const struct attribute_group thermal_temp_input16_group = {
2732 .attrs = thermal_temp_input_attr
2735 static const struct attribute_group thermal_temp_input8_group = {
2736 .attrs = &thermal_temp_input_attr[8]
2739 #undef THERMAL_SENSOR_ATTR_TEMP
2740 #undef THERMAL_ATTRS
2742 /* --------------------------------------------------------------------- */
2744 static int __init thermal_init(struct ibm_init_struct *iibm)
2751 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
2753 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
2755 if (thinkpad_id.ec_model) {
2757 * Direct EC access mode: sensors at registers
2758 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
2759 * non-implemented, thermal sensors return 0x80 when
2764 for (i = 0; i < 8; i++) {
2765 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
2771 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
2779 /* This is sheer paranoia, but we handle it anyway */
2782 "ThinkPad ACPI EC access misbehaving, "
2783 "falling back to ACPI TMPx access mode\n");
2784 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2787 "ThinkPad ACPI EC access misbehaving, "
2788 "disabling thermal sensors access\n");
2789 thermal_read_mode = TPACPI_THERMAL_NONE;
2794 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
2796 } else if (acpi_tmp7) {
2797 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
2798 /* 600e/x, 770e, 770x */
2799 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
2801 /* Standard ACPI TMPx access, max 8 sensors */
2802 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
2805 /* temperatures not supported on 570, G4x, R30, R31, R32 */
2806 thermal_read_mode = TPACPI_THERMAL_NONE;
2809 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
2810 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
2813 switch(thermal_read_mode) {
2814 case TPACPI_THERMAL_TPEC_16:
2815 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2816 &thermal_temp_input16_group);
2820 case TPACPI_THERMAL_TPEC_8:
2821 case TPACPI_THERMAL_ACPI_TMP07:
2822 case TPACPI_THERMAL_ACPI_UPDT:
2823 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2824 &thermal_temp_input8_group);
2828 case TPACPI_THERMAL_NONE:
2836 static void thermal_exit(void)
2838 switch(thermal_read_mode) {
2839 case TPACPI_THERMAL_TPEC_16:
2840 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2841 &thermal_temp_input16_group);
2843 case TPACPI_THERMAL_TPEC_8:
2844 case TPACPI_THERMAL_ACPI_TMP07:
2845 case TPACPI_THERMAL_ACPI_UPDT:
2846 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2847 &thermal_temp_input16_group);
2849 case TPACPI_THERMAL_NONE:
2855 /* idx is zero-based */
2856 static int thermal_get_sensor(int idx, s32 *value)
2862 t = TP_EC_THERMAL_TMP0;
2864 switch (thermal_read_mode) {
2865 #if TPACPI_MAX_THERMAL_SENSORS >= 16
2866 case TPACPI_THERMAL_TPEC_16:
2867 if (idx >= 8 && idx <= 15) {
2868 t = TP_EC_THERMAL_TMP8;
2873 case TPACPI_THERMAL_TPEC_8:
2875 if (!acpi_ec_read(t + idx, &tmp))
2877 *value = tmp * 1000;
2882 case TPACPI_THERMAL_ACPI_UPDT:
2884 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2885 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
2887 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2889 *value = (t - 2732) * 100;
2894 case TPACPI_THERMAL_ACPI_TMP07:
2896 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
2897 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
2899 if (t > 127 || t < -127)
2900 t = TP_EC_THERMAL_TMP_NA;
2906 case TPACPI_THERMAL_NONE:
2914 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
2925 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
2928 for(i = 0 ; i < n; i++) {
2929 res = thermal_get_sensor(i, &s->temp[i]);
2937 static int thermal_read(char *p)
2941 struct ibm_thermal_sensors_struct t;
2943 n = thermal_get_sensors(&t);
2944 if (unlikely(n < 0))
2947 len += sprintf(p + len, "temperatures:\t");
2950 for (i = 0; i < (n - 1); i++)
2951 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
2952 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
2954 len += sprintf(p + len, "not supported\n");
2959 static struct ibm_struct thermal_driver_data = {
2961 .read = thermal_read,
2962 .exit = thermal_exit,
2965 /*************************************************************************
2969 static u8 ecdump_regs[256];
2971 static int ecdump_read(char *p)
2977 len += sprintf(p + len, "EC "
2978 " +00 +01 +02 +03 +04 +05 +06 +07"
2979 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
2980 for (i = 0; i < 256; i += 16) {
2981 len += sprintf(p + len, "EC 0x%02x:", i);
2982 for (j = 0; j < 16; j++) {
2983 if (!acpi_ec_read(i + j, &v))
2985 if (v != ecdump_regs[i + j])
2986 len += sprintf(p + len, " *%02x", v);
2988 len += sprintf(p + len, " %02x", v);
2989 ecdump_regs[i + j] = v;
2991 len += sprintf(p + len, "\n");
2996 /* These are way too dangerous to advertise openly... */
2998 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
2999 " (<offset> is 00-ff, <value> is 00-ff)\n");
3000 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3001 " (<offset> is 00-ff, <value> is 0-255)\n");
3006 static int ecdump_write(char *buf)
3011 while ((cmd = next_cmd(&buf))) {
3012 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3014 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3018 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3019 if (!acpi_ec_write(i, v))
3028 static struct ibm_struct ecdump_driver_data = {
3030 .read = ecdump_read,
3031 .write = ecdump_write,
3032 .flags.experimental = 1,
3035 /*************************************************************************
3036 * Backlight/brightness subdriver
3039 static struct backlight_device *ibm_backlight_device;
3041 static struct backlight_ops ibm_backlight_data = {
3042 .get_brightness = brightness_get,
3043 .update_status = brightness_update_status,
3046 static struct mutex brightness_mutex;
3048 static int __init brightness_init(struct ibm_init_struct *iibm)
3052 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
3054 mutex_init(&brightness_mutex);
3056 if (!brightness_mode) {
3057 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
3058 brightness_mode = 2;
3060 brightness_mode = 3;
3062 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
3066 if (brightness_mode > 3)
3069 b = brightness_get(NULL);
3073 ibm_backlight_device = backlight_device_register(
3074 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
3075 &ibm_backlight_data);
3076 if (IS_ERR(ibm_backlight_device)) {
3077 printk(IBM_ERR "Could not register backlight device\n");
3078 return PTR_ERR(ibm_backlight_device);
3080 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
3082 ibm_backlight_device->props.max_brightness = 7;
3083 ibm_backlight_device->props.brightness = b;
3084 backlight_update_status(ibm_backlight_device);
3089 static void brightness_exit(void)
3091 if (ibm_backlight_device) {
3092 vdbg_printk(TPACPI_DBG_EXIT,
3093 "calling backlight_device_unregister()\n");
3094 backlight_device_unregister(ibm_backlight_device);
3095 ibm_backlight_device = NULL;
3099 static int brightness_update_status(struct backlight_device *bd)
3101 return brightness_set(
3102 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
3103 bd->props.power == FB_BLANK_UNBLANK) ?
3104 bd->props.brightness : 0);
3108 * ThinkPads can read brightness from two places: EC 0x31, or
3109 * CMOS NVRAM byte 0x5E, bits 0-3.
3111 static int brightness_get(struct backlight_device *bd)
3113 u8 lec = 0, lcmos = 0, level = 0;
3115 if (brightness_mode & 1) {
3116 if (!acpi_ec_read(brightness_offset, &lec))
3121 if (brightness_mode & 2) {
3122 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
3123 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
3124 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
3128 if (brightness_mode == 3 && lec != lcmos) {
3130 "CMOS NVRAM (%u) and EC (%u) do not agree "
3131 "on display brightness level\n",
3132 (unsigned int) lcmos,
3133 (unsigned int) lec);
3140 static int brightness_set(int value)
3142 int cmos_cmd, inc, i, res;
3148 res = mutex_lock_interruptible(&brightness_mutex);
3152 current_value = brightness_get(NULL);
3153 if (current_value < 0) {
3154 res = current_value;
3158 cmos_cmd = value > current_value ?
3159 TP_CMOS_BRIGHTNESS_UP :
3160 TP_CMOS_BRIGHTNESS_DOWN;
3161 inc = value > current_value ? 1 : -1;
3164 for (i = current_value; i != value; i += inc) {
3165 if ((brightness_mode & 2) &&
3166 issue_thinkpad_cmos_command(cmos_cmd)) {
3170 if ((brightness_mode & 1) &&
3171 !acpi_ec_write(brightness_offset, i + inc)) {
3178 mutex_unlock(&brightness_mutex);
3182 static int brightness_read(char *p)
3187 if ((level = brightness_get(NULL)) < 0) {
3188 len += sprintf(p + len, "level:\t\tunreadable\n");
3190 len += sprintf(p + len, "level:\t\t%d\n", level & 0x7);
3191 len += sprintf(p + len, "commands:\tup, down\n");
3192 len += sprintf(p + len, "commands:\tlevel <level>"
3193 " (<level> is 0-7)\n");
3199 static int brightness_write(char *buf)
3205 while ((cmd = next_cmd(&buf))) {
3206 if ((level = brightness_get(NULL)) < 0)
3210 if (strlencmp(cmd, "up") == 0) {
3211 new_level = level == 7 ? 7 : level + 1;
3212 } else if (strlencmp(cmd, "down") == 0) {
3213 new_level = level == 0 ? 0 : level - 1;
3214 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3215 new_level >= 0 && new_level <= 7) {
3220 brightness_set(new_level);
3226 static struct ibm_struct brightness_driver_data = {
3227 .name = "brightness",
3228 .read = brightness_read,
3229 .write = brightness_write,
3230 .exit = brightness_exit,
3233 /*************************************************************************
3237 static int volume_read(char *p)
3242 if (!acpi_ec_read(volume_offset, &level)) {
3243 len += sprintf(p + len, "level:\t\tunreadable\n");
3245 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
3246 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
3247 len += sprintf(p + len, "commands:\tup, down, mute\n");
3248 len += sprintf(p + len, "commands:\tlevel <level>"
3249 " (<level> is 0-15)\n");
3255 static int volume_write(char *buf)
3257 int cmos_cmd, inc, i;
3259 int new_level, new_mute;
3262 while ((cmd = next_cmd(&buf))) {
3263 if (!acpi_ec_read(volume_offset, &level))
3265 new_mute = mute = level & 0x40;
3266 new_level = level = level & 0xf;
3268 if (strlencmp(cmd, "up") == 0) {
3272 new_level = level == 15 ? 15 : level + 1;
3273 } else if (strlencmp(cmd, "down") == 0) {
3277 new_level = level == 0 ? 0 : level - 1;
3278 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
3279 new_level >= 0 && new_level <= 15) {
3281 } else if (strlencmp(cmd, "mute") == 0) {
3286 if (new_level != level) { /* mute doesn't change */
3287 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
3288 inc = new_level > level ? 1 : -1;