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.18"
25 #define TPACPI_SYSFS_VERSION 0x020101
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 <linux/kernel.h>
86 #include <linux/module.h>
87 #include <linux/init.h>
88 #include <linux/types.h>
89 #include <linux/string.h>
90 #include <linux/list.h>
91 #include <linux/mutex.h>
92 #include <linux/kthread.h>
93 #include <linux/freezer.h>
94 #include <linux/delay.h>
96 #include <linux/nvram.h>
97 #include <linux/proc_fs.h>
98 #include <linux/sysfs.h>
99 #include <linux/backlight.h>
100 #include <linux/fb.h>
101 #include <linux/platform_device.h>
102 #include <linux/hwmon.h>
103 #include <linux/hwmon-sysfs.h>
104 #include <linux/input.h>
105 #include <asm/uaccess.h>
107 #include <linux/dmi.h>
108 #include <linux/jiffies.h>
109 #include <linux/workqueue.h>
111 #include <acpi/acpi_drivers.h>
112 #include <acpi/acnamesp.h>
114 #include <linux/pci_ids.h>
117 /* ThinkPad CMOS commands */
118 #define TP_CMOS_VOLUME_DOWN 0
119 #define TP_CMOS_VOLUME_UP 1
120 #define TP_CMOS_VOLUME_MUTE 2
121 #define TP_CMOS_BRIGHTNESS_UP 4
122 #define TP_CMOS_BRIGHTNESS_DOWN 5
124 /* NVRAM Addresses */
126 TP_NVRAM_ADDR_HK2 = 0x57,
127 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
128 TP_NVRAM_ADDR_VIDEO = 0x59,
129 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
130 TP_NVRAM_ADDR_MIXER = 0x60,
133 /* NVRAM bit masks */
135 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
136 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
137 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
138 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
139 TP_NVRAM_MASK_THINKLIGHT = 0x10,
140 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
141 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
142 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
143 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
144 TP_NVRAM_MASK_MUTE = 0x40,
145 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
146 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
147 TP_NVRAM_POS_LEVEL_VOLUME = 0,
151 #define IBM_HKEY_HID "IBM0068"
154 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
155 #define TPACPI_HKEY_INPUT_VERSION 0x4101
158 /****************************************************************************
163 #define IBM_NAME "thinkpad"
164 #define IBM_DESC "ThinkPad ACPI Extras"
165 #define IBM_FILE IBM_NAME "_acpi"
166 #define IBM_URL "http://ibm-acpi.sf.net/"
167 #define IBM_MAIL "ibm-acpi-devel@lists.sourceforge.net"
169 #define IBM_PROC_DIR "ibm"
170 #define IBM_ACPI_EVENT_PREFIX "ibm"
171 #define IBM_DRVR_NAME IBM_FILE
172 #define IBM_HWMON_DRVR_NAME IBM_NAME "_hwmon"
174 #define IBM_MAX_ACPI_ARGS 3
176 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
177 MODULE_DESCRIPTION(IBM_DESC);
178 MODULE_VERSION(IBM_VERSION);
179 MODULE_LICENSE("GPL");
181 /* Please remove this in year 2009 */
182 MODULE_ALIAS("ibm_acpi");
185 * DMI matching for module autoloading
187 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
188 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
190 * Only models listed in thinkwiki will be supported, so add yours
191 * if it is not there yet.
193 #define IBM_BIOS_MODULE_ALIAS(__type) \
194 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
196 /* Non-ancient thinkpads */
197 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
198 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
200 /* Ancient thinkpad BIOSes have to be identified by
201 * BIOS type or model number, and there are far less
202 * BIOS types than model numbers... */
203 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
204 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
205 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
208 #define IBM_LOG IBM_FILE ": "
209 #define IBM_ERR KERN_ERR IBM_LOG
210 #define IBM_NOTICE KERN_NOTICE IBM_LOG
211 #define IBM_INFO KERN_INFO IBM_LOG
212 #define IBM_DEBUG KERN_DEBUG IBM_LOG
214 #define TPACPI_DBG_ALL 0xffff
215 #define TPACPI_DBG_ALL 0xffff
216 #define TPACPI_DBG_INIT 0x0001
217 #define TPACPI_DBG_EXIT 0x0002
218 #define dbg_printk(a_dbg_level, format, arg...) \
219 do { if (dbg_level & a_dbg_level) \
220 printk(IBM_DEBUG "%s: " format, __func__ , ## arg); } while (0)
221 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
222 #define vdbg_printk(a_dbg_level, format, arg...) \
223 dbg_printk(a_dbg_level, format, ## arg)
224 static const char *str_supported(int is_supported);
226 #define vdbg_printk(a_dbg_level, format, arg...)
229 #define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
230 #define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
231 #define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
234 /****************************************************************************
235 * Driver-wide structs and misc. variables
240 struct tp_acpi_drv_struct {
241 const struct acpi_device_id *hid;
242 struct acpi_driver *driver;
244 void (*notify) (struct ibm_struct *, u32);
247 struct acpi_device *device;
253 int (*read) (char *);
254 int (*write) (char *);
256 void (*resume) (void);
258 struct list_head all_drivers;
260 struct tp_acpi_drv_struct *acpi;
263 u8 acpi_driver_registered:1;
264 u8 acpi_notify_installed:1;
271 struct ibm_init_struct {
274 int (*init) (struct ibm_init_struct *);
275 struct ibm_struct *data;
279 #ifdef CONFIG_THINKPAD_ACPI_BAY
291 u32 bright_16levels:1;
293 u32 fan_ctrl_status_undef:1;
294 u32 input_device_registered:1;
295 u32 platform_drv_registered:1;
296 u32 platform_drv_attrs_registered:1;
297 u32 sensors_pdrv_registered:1;
298 u32 sensors_pdrv_attrs_registered:1;
299 u32 sensors_pdev_attrs_registered:1;
300 u32 hotkey_poll_active:1;
303 struct thinkpad_id_data {
304 unsigned int vendor; /* ThinkPad vendor:
305 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
307 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
308 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
310 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
315 static struct thinkpad_id_data thinkpad_id;
317 #define __unused __attribute__ ((unused))
320 TPACPI_LIFE_INIT = 0,
325 static int experimental;
326 static u32 dbg_level;
328 /****************************************************************************
329 ****************************************************************************
331 * ACPI Helpers and device model
333 ****************************************************************************
334 ****************************************************************************/
336 /*************************************************************************
340 static acpi_handle root_handle;
342 #define IBM_HANDLE(object, parent, paths...) \
343 static acpi_handle object##_handle; \
344 static acpi_handle *object##_parent = &parent##_handle; \
345 static char *object##_path; \
346 static char *object##_paths[] = { paths }
348 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
349 "\\_SB.PCI.ISA.EC", /* 570 */
350 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
351 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
352 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
353 "\\_SB.PCI0.ICH3.EC0", /* R31 */
354 "\\_SB.PCI0.LPC.EC", /* all others */
357 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
358 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
360 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
361 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
362 "\\CMS", /* R40, R40e */
365 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
366 "^HKEY", /* R30, R31 */
367 "HKEY", /* all others */
371 /*************************************************************************
375 static int acpi_evalf(acpi_handle handle,
376 void *res, char *method, char *fmt, ...)
379 struct acpi_object_list params;
380 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
381 struct acpi_buffer result, *resultp;
382 union acpi_object out_obj;
390 printk(IBM_ERR "acpi_evalf() called with empty format\n");
403 params.pointer = &in_objs[0];
410 in_objs[params.count].integer.value = va_arg(ap, int);
411 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
413 /* add more types as needed */
415 printk(IBM_ERR "acpi_evalf() called "
416 "with invalid format character '%c'\n", c);
422 if (res_type != 'v') {
423 result.length = sizeof(out_obj);
424 result.pointer = &out_obj;
429 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
434 *(int *)res = out_obj.integer.value;
435 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
438 success = status == AE_OK;
440 /* add more types as needed */
442 printk(IBM_ERR "acpi_evalf() called "
443 "with invalid format character '%c'\n", res_type);
447 if (!success && !quiet)
448 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
449 method, fmt0, status);
454 static void __unused acpi_print_int(acpi_handle handle, char *method)
458 if (acpi_evalf(handle, &i, method, "d"))
459 printk(IBM_INFO "%s = 0x%x\n", method, i);
461 printk(IBM_ERR "error calling %s\n", method);
464 static int acpi_ec_read(int i, u8 * p)
469 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
473 if (ec_read(i, p) < 0)
480 static int acpi_ec_write(int i, u8 v)
483 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
486 if (ec_write(i, v) < 0)
493 static int _sta(acpi_handle handle)
497 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
503 static int issue_thinkpad_cmos_command(int cmos_cmd)
508 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
514 /*************************************************************************
518 #define IBM_ACPIHANDLE_INIT(object) \
519 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
520 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
522 static void drv_acpi_handle_init(char *name,
523 acpi_handle *handle, acpi_handle parent,
524 char **paths, int num_paths, char **path)
529 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
532 for (i = 0; i < num_paths; i++) {
533 status = acpi_get_handle(parent, paths[i], handle);
534 if (ACPI_SUCCESS(status)) {
536 dbg_printk(TPACPI_DBG_INIT,
537 "Found ACPI handle %s for %s\n",
543 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
548 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
550 struct ibm_struct *ibm = data;
552 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
555 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
558 ibm->acpi->notify(ibm, event);
561 static int __init setup_acpi_notify(struct ibm_struct *ibm)
568 if (!*ibm->acpi->handle)
571 vdbg_printk(TPACPI_DBG_INIT,
572 "setting up ACPI notify for %s\n", ibm->name);
574 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
576 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
581 acpi_driver_data(ibm->acpi->device) = ibm;
582 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
583 IBM_ACPI_EVENT_PREFIX,
586 status = acpi_install_notify_handler(*ibm->acpi->handle,
587 ibm->acpi->type, dispatch_acpi_notify, ibm);
588 if (ACPI_FAILURE(status)) {
589 if (status == AE_ALREADY_EXISTS) {
590 printk(IBM_NOTICE "another device driver is already handling %s events\n",
593 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
598 ibm->flags.acpi_notify_installed = 1;
602 static int __init tpacpi_device_add(struct acpi_device *device)
607 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
611 dbg_printk(TPACPI_DBG_INIT,
612 "registering %s as an ACPI driver\n", ibm->name);
616 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
617 if (!ibm->acpi->driver) {
618 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
622 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
623 ibm->acpi->driver->ids = ibm->acpi->hid;
625 ibm->acpi->driver->ops.add = &tpacpi_device_add;
627 rc = acpi_bus_register_driver(ibm->acpi->driver);
629 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
631 kfree(ibm->acpi->driver);
632 ibm->acpi->driver = NULL;
634 ibm->flags.acpi_driver_registered = 1;
640 /****************************************************************************
641 ****************************************************************************
645 ****************************************************************************
646 ****************************************************************************/
648 static int dispatch_procfs_read(char *page, char **start, off_t off,
649 int count, int *eof, void *data)
651 struct ibm_struct *ibm = data;
654 if (!ibm || !ibm->read)
657 len = ibm->read(page);
661 if (len <= off + count)
673 static int dispatch_procfs_write(struct file *file,
674 const char __user * userbuf,
675 unsigned long count, void *data)
677 struct ibm_struct *ibm = data;
681 if (!ibm || !ibm->write)
684 kernbuf = kmalloc(count + 2, GFP_KERNEL);
688 if (copy_from_user(kernbuf, userbuf, count)) {
694 strcat(kernbuf, ",");
695 ret = ibm->write(kernbuf);
704 static char *next_cmd(char **cmds)
709 while ((end = strchr(start, ',')) && end == start)
721 /****************************************************************************
722 ****************************************************************************
724 * Device model: input, hwmon and platform
726 ****************************************************************************
727 ****************************************************************************/
729 static struct platform_device *tpacpi_pdev;
730 static struct platform_device *tpacpi_sensors_pdev;
731 static struct device *tpacpi_hwmon;
732 static struct input_dev *tpacpi_inputdev;
733 static struct mutex tpacpi_inputdev_send_mutex;
734 static LIST_HEAD(tpacpi_all_drivers);
736 static int tpacpi_resume_handler(struct platform_device *pdev)
738 struct ibm_struct *ibm, *itmp;
740 list_for_each_entry_safe(ibm, itmp,
750 static struct platform_driver tpacpi_pdriver = {
752 .name = IBM_DRVR_NAME,
753 .owner = THIS_MODULE,
755 .resume = tpacpi_resume_handler,
758 static struct platform_driver tpacpi_hwmon_pdriver = {
760 .name = IBM_HWMON_DRVR_NAME,
761 .owner = THIS_MODULE,
765 /*************************************************************************
766 * sysfs support helpers
769 struct attribute_set {
770 unsigned int members, max_members;
771 struct attribute_group group;
774 struct attribute_set_obj {
775 struct attribute_set s;
777 } __attribute__((packed));
779 static struct attribute_set *create_attr_set(unsigned int max_members,
782 struct attribute_set_obj *sobj;
784 if (max_members == 0)
787 /* Allocates space for implicit NULL at the end too */
788 sobj = kzalloc(sizeof(struct attribute_set_obj) +
789 max_members * sizeof(struct attribute *),
793 sobj->s.max_members = max_members;
794 sobj->s.group.attrs = &sobj->a;
795 sobj->s.group.name = name;
800 #define destroy_attr_set(_set) \
803 /* not multi-threaded safe, use it in a single thread per set */
804 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
809 if (s->members >= s->max_members)
812 s->group.attrs[s->members] = attr;
818 static int add_many_to_attr_set(struct attribute_set* s,
819 struct attribute **attr,
824 for (i = 0; i < count; i++) {
825 res = add_to_attr_set(s, attr[i]);
833 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
835 sysfs_remove_group(kobj, &s->group);
839 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
840 sysfs_create_group(_kobj, &_attr_set->group)
842 static int parse_strtoul(const char *buf,
843 unsigned long max, unsigned long *value)
847 while (*buf && isspace(*buf))
849 *value = simple_strtoul(buf, &endp, 0);
850 while (*endp && isspace(*endp))
852 if (*endp || *value > max)
858 /*************************************************************************
859 * thinkpad-acpi driver attributes
862 /* interface_version --------------------------------------------------- */
863 static ssize_t tpacpi_driver_interface_version_show(
864 struct device_driver *drv,
867 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
870 static DRIVER_ATTR(interface_version, S_IRUGO,
871 tpacpi_driver_interface_version_show, NULL);
873 /* debug_level --------------------------------------------------------- */
874 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
877 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
880 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
881 const char *buf, size_t count)
885 if (parse_strtoul(buf, 0xffff, &t))
893 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
894 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
896 /* version ------------------------------------------------------------- */
897 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
900 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
903 static DRIVER_ATTR(version, S_IRUGO,
904 tpacpi_driver_version_show, NULL);
906 /* --------------------------------------------------------------------- */
908 static struct driver_attribute* tpacpi_driver_attributes[] = {
909 &driver_attr_debug_level, &driver_attr_version,
910 &driver_attr_interface_version,
913 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
919 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
920 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
927 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
931 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
932 driver_remove_file(drv, tpacpi_driver_attributes[i]);
935 /****************************************************************************
936 ****************************************************************************
940 ****************************************************************************
941 ****************************************************************************/
943 /*************************************************************************
944 * thinkpad-acpi init subdriver
947 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
949 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
950 printk(IBM_INFO "%s\n", IBM_URL);
952 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
953 (thinkpad_id.bios_version_str) ?
954 thinkpad_id.bios_version_str : "unknown",
955 (thinkpad_id.ec_version_str) ?
956 thinkpad_id.ec_version_str : "unknown");
958 if (thinkpad_id.vendor && thinkpad_id.model_str)
959 printk(IBM_INFO "%s %s\n",
960 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
961 "IBM" : ((thinkpad_id.vendor ==
962 PCI_VENDOR_ID_LENOVO) ?
963 "Lenovo" : "Unknown vendor"),
964 thinkpad_id.model_str);
969 static int thinkpad_acpi_driver_read(char *p)
973 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
974 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
979 static struct ibm_struct thinkpad_acpi_driver_data = {
981 .read = thinkpad_acpi_driver_read,
984 /*************************************************************************
988 enum { /* hot key scan codes (derived from ACPI DSDT) */
989 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
990 TP_ACPI_HOTKEYSCAN_FNF2,
991 TP_ACPI_HOTKEYSCAN_FNF3,
992 TP_ACPI_HOTKEYSCAN_FNF4,
993 TP_ACPI_HOTKEYSCAN_FNF5,
994 TP_ACPI_HOTKEYSCAN_FNF6,
995 TP_ACPI_HOTKEYSCAN_FNF7,
996 TP_ACPI_HOTKEYSCAN_FNF8,
997 TP_ACPI_HOTKEYSCAN_FNF9,
998 TP_ACPI_HOTKEYSCAN_FNF10,
999 TP_ACPI_HOTKEYSCAN_FNF11,
1000 TP_ACPI_HOTKEYSCAN_FNF12,
1001 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1002 TP_ACPI_HOTKEYSCAN_FNINSERT,
1003 TP_ACPI_HOTKEYSCAN_FNDELETE,
1004 TP_ACPI_HOTKEYSCAN_FNHOME,
1005 TP_ACPI_HOTKEYSCAN_FNEND,
1006 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1007 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1008 TP_ACPI_HOTKEYSCAN_FNSPACE,
1009 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1010 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1011 TP_ACPI_HOTKEYSCAN_MUTE,
1012 TP_ACPI_HOTKEYSCAN_THINKPAD,
1015 enum { /* Keys available through NVRAM polling */
1016 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1017 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1020 enum { /* Positions of some of the keys in hotkey masks */
1021 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1022 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1023 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1024 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1025 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1026 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1027 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1028 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1029 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1030 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1031 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1034 enum { /* NVRAM to ACPI HKEY group map */
1035 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1036 TP_ACPI_HKEY_ZOOM_MASK |
1037 TP_ACPI_HKEY_DISPSWTCH_MASK |
1038 TP_ACPI_HKEY_HIBERNATE_MASK,
1039 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1040 TP_ACPI_HKEY_BRGHTDWN_MASK,
1041 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1042 TP_ACPI_HKEY_VOLDWN_MASK |
1043 TP_ACPI_HKEY_MUTE_MASK,
1046 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1047 struct tp_nvram_state {
1048 u16 thinkpad_toggle:1;
1050 u16 display_toggle:1;
1051 u16 thinklight_toggle:1;
1052 u16 hibernate_toggle:1;
1053 u16 displayexp_toggle:1;
1054 u16 display_state:1;
1055 u16 brightness_toggle:1;
1056 u16 volume_toggle:1;
1059 u8 brightness_level;
1063 static struct task_struct *tpacpi_hotkey_task;
1064 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1065 static int hotkey_poll_freq = 10; /* Hz */
1066 static struct mutex hotkey_thread_mutex;
1067 static struct mutex hotkey_thread_data_mutex;
1068 static unsigned int hotkey_config_change;
1070 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1072 #define hotkey_source_mask 0U
1074 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1076 static struct mutex hotkey_mutex;
1078 static int hotkey_orig_status;
1079 static u32 hotkey_orig_mask;
1080 static u32 hotkey_all_mask;
1081 static u32 hotkey_reserved_mask;
1082 static u32 hotkey_mask;
1084 static unsigned int hotkey_report_mode;
1086 static u16 *hotkey_keycode_map;
1088 static struct attribute_set *hotkey_dev_attributes;
1090 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1091 #define HOTKEY_CONFIG_CRITICAL_START \
1092 mutex_lock(&hotkey_thread_data_mutex); \
1093 hotkey_config_change++;
1094 #define HOTKEY_CONFIG_CRITICAL_END \
1095 mutex_unlock(&hotkey_thread_data_mutex);
1097 #define HOTKEY_CONFIG_CRITICAL_START
1098 #define HOTKEY_CONFIG_CRITICAL_END
1099 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1101 static int hotkey_get_wlsw(int *status)
1103 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1109 * Call with hotkey_mutex held
1111 static int hotkey_mask_get(void)
1115 if (tp_features.hotkey_mask) {
1116 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1119 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1125 * Call with hotkey_mutex held
1127 static int hotkey_mask_set(u32 mask)
1132 if (tp_features.hotkey_mask) {
1133 HOTKEY_CONFIG_CRITICAL_START
1134 for (i = 0; i < 32; i++) {
1136 /* enable in firmware mask only keys not in NVRAM
1137 * mode, but enable the key in the cached hotkey_mask
1138 * regardless of mode, or the key will end up
1139 * disabled by hotkey_mask_get() */
1140 if (!acpi_evalf(hkey_handle,
1141 NULL, "MHKM", "vdd", i + 1,
1142 !!((mask & ~hotkey_source_mask) & m))) {
1146 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1149 HOTKEY_CONFIG_CRITICAL_END
1151 /* hotkey_mask_get must be called unconditionally below */
1152 if (!hotkey_mask_get() && !rc &&
1153 (hotkey_mask & ~hotkey_source_mask) !=
1154 (mask & ~hotkey_source_mask)) {
1156 "requested hot key mask 0x%08x, but "
1157 "firmware forced it to 0x%08x\n",
1161 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1162 HOTKEY_CONFIG_CRITICAL_START
1163 hotkey_mask = mask & hotkey_source_mask;
1164 HOTKEY_CONFIG_CRITICAL_END
1166 if (hotkey_mask != mask) {
1168 "requested hot key mask 0x%08x, "
1169 "forced to 0x%08x (NVRAM poll mask is "
1170 "0x%08x): no firmware mask support\n",
1171 mask, hotkey_mask, hotkey_source_mask);
1176 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1182 static int hotkey_status_get(int *status)
1184 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1190 static int hotkey_status_set(int status)
1192 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1198 static void tpacpi_input_send_radiosw(void)
1202 mutex_lock(&tpacpi_inputdev_send_mutex);
1204 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1205 input_report_switch(tpacpi_inputdev,
1207 input_sync(tpacpi_inputdev);
1210 mutex_unlock(&tpacpi_inputdev_send_mutex);
1213 static void tpacpi_input_send_key(unsigned int scancode)
1215 unsigned int keycode;
1217 keycode = hotkey_keycode_map[scancode];
1219 if (keycode != KEY_RESERVED) {
1220 mutex_lock(&tpacpi_inputdev_send_mutex);
1222 input_report_key(tpacpi_inputdev, keycode, 1);
1223 if (keycode == KEY_UNKNOWN)
1224 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1226 input_sync(tpacpi_inputdev);
1228 input_report_key(tpacpi_inputdev, keycode, 0);
1229 if (keycode == KEY_UNKNOWN)
1230 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1232 input_sync(tpacpi_inputdev);
1234 mutex_unlock(&tpacpi_inputdev_send_mutex);
1238 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1239 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1241 static void tpacpi_hotkey_send_key(unsigned int scancode)
1243 tpacpi_input_send_key(scancode);
1244 if (hotkey_report_mode < 2) {
1245 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1246 0x80, 0x1001 + scancode);
1250 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1254 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1255 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1256 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1257 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1258 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1259 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1261 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1262 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1263 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1265 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1266 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1267 n->displayexp_toggle =
1268 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1270 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1271 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1272 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1273 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1274 n->brightness_toggle =
1275 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1277 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1278 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1279 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1280 >> TP_NVRAM_POS_LEVEL_VOLUME;
1281 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1282 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1286 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1287 do { if ((mask & (1 << __scancode)) && oldn->__member != newn->__member) \
1288 tpacpi_hotkey_send_key(__scancode); } while (0)
1290 #define TPACPI_MAY_SEND_KEY(__scancode) \
1291 do { if (mask & (1 << __scancode)) \
1292 tpacpi_hotkey_send_key(__scancode); } while (0)
1294 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1295 struct tp_nvram_state *newn,
1298 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1299 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1300 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1301 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1303 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1305 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1308 if (oldn->volume_toggle != newn->volume_toggle) {
1309 if (oldn->mute != newn->mute) {
1310 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1312 if (oldn->volume_level > newn->volume_level) {
1313 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1314 } else if (oldn->volume_level < newn->volume_level) {
1315 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1316 } else if (oldn->mute == newn->mute) {
1317 /* repeated key presses that didn't change state */
1319 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1320 } else if (newn->volume_level != 0) {
1321 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1323 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1328 /* handle brightness */
1329 if (oldn->brightness_toggle != newn->brightness_toggle) {
1330 if (oldn->brightness_level < newn->brightness_level) {
1331 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1332 } else if (oldn->brightness_level > newn->brightness_level) {
1333 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1335 /* repeated key presses that didn't change state */
1336 if (newn->brightness_level != 0) {
1337 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1339 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1345 #undef TPACPI_COMPARE_KEY
1346 #undef TPACPI_MAY_SEND_KEY
1348 static int hotkey_kthread(void *data)
1350 struct tp_nvram_state s[2];
1352 unsigned int si, so;
1354 unsigned int change_detector, must_reset;
1356 mutex_lock(&hotkey_thread_mutex);
1358 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1367 /* Initial state for compares */
1368 mutex_lock(&hotkey_thread_data_mutex);
1369 change_detector = hotkey_config_change;
1370 mask = hotkey_source_mask & hotkey_mask;
1371 mutex_unlock(&hotkey_thread_data_mutex);
1372 hotkey_read_nvram(&s[so], mask);
1374 while (!kthread_should_stop() && hotkey_poll_freq) {
1376 t = 1000/hotkey_poll_freq;
1377 t = msleep_interruptible(t);
1378 if (unlikely(kthread_should_stop()))
1380 must_reset = try_to_freeze();
1381 if (t > 0 && !must_reset)
1384 mutex_lock(&hotkey_thread_data_mutex);
1385 if (must_reset || hotkey_config_change != change_detector) {
1386 /* forget old state on thaw or config change */
1389 change_detector = hotkey_config_change;
1391 mask = hotkey_source_mask & hotkey_mask;
1392 mutex_unlock(&hotkey_thread_data_mutex);
1395 hotkey_read_nvram(&s[si], mask);
1396 if (likely(si != so)) {
1397 hotkey_compare_and_issue_event(&s[so], &s[si],
1407 mutex_unlock(&hotkey_thread_mutex);
1411 static void hotkey_poll_stop_sync(void)
1413 if (tpacpi_hotkey_task) {
1414 if (frozen(tpacpi_hotkey_task) ||
1415 freezing(tpacpi_hotkey_task))
1416 thaw_process(tpacpi_hotkey_task);
1418 kthread_stop(tpacpi_hotkey_task);
1419 tpacpi_hotkey_task = NULL;
1420 mutex_lock(&hotkey_thread_mutex);
1421 /* at this point, the thread did exit */
1422 mutex_unlock(&hotkey_thread_mutex);
1426 /* call with hotkey_mutex held */
1427 static void hotkey_poll_setup(int may_warn)
1429 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1430 hotkey_poll_freq > 0 &&
1431 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1432 if (!tpacpi_hotkey_task) {
1433 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1434 NULL, IBM_FILE "d");
1435 if (IS_ERR(tpacpi_hotkey_task)) {
1436 tpacpi_hotkey_task = NULL;
1437 printk(IBM_ERR "could not create kernel thread "
1438 "for hotkey polling\n");
1442 hotkey_poll_stop_sync();
1444 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1445 printk(IBM_NOTICE "hot keys 0x%08x require polling, "
1446 "which is currently disabled\n",
1447 hotkey_source_mask);
1452 static void hotkey_poll_setup_safe(int may_warn)
1454 mutex_lock(&hotkey_mutex);
1455 hotkey_poll_setup(may_warn);
1456 mutex_unlock(&hotkey_mutex);
1459 static int hotkey_inputdev_open(struct input_dev *dev)
1461 switch (tpacpi_lifecycle) {
1462 case TPACPI_LIFE_INIT:
1464 * hotkey_init will call hotkey_poll_setup_safe
1465 * at the appropriate moment
1468 case TPACPI_LIFE_EXITING:
1470 case TPACPI_LIFE_RUNNING:
1471 hotkey_poll_setup_safe(0);
1475 /* Should only happen if tpacpi_lifecycle is corrupt */
1480 static void hotkey_inputdev_close(struct input_dev *dev)
1482 /* disable hotkey polling when possible */
1483 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1484 hotkey_poll_setup_safe(0);
1486 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1488 /* sysfs hotkey enable ------------------------------------------------- */
1489 static ssize_t hotkey_enable_show(struct device *dev,
1490 struct device_attribute *attr,
1495 res = hotkey_status_get(&status);
1499 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1502 static ssize_t hotkey_enable_store(struct device *dev,
1503 struct device_attribute *attr,
1504 const char *buf, size_t count)
1509 if (parse_strtoul(buf, 1, &t))
1512 res = hotkey_status_set(t);
1514 return (res) ? res : count;
1517 static struct device_attribute dev_attr_hotkey_enable =
1518 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1519 hotkey_enable_show, hotkey_enable_store);
1521 /* sysfs hotkey mask --------------------------------------------------- */
1522 static ssize_t hotkey_mask_show(struct device *dev,
1523 struct device_attribute *attr,
1528 if (mutex_lock_interruptible(&hotkey_mutex))
1529 return -ERESTARTSYS;
1530 res = hotkey_mask_get();
1531 mutex_unlock(&hotkey_mutex);
1534 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1537 static ssize_t hotkey_mask_store(struct device *dev,
1538 struct device_attribute *attr,
1539 const char *buf, size_t count)
1544 if (parse_strtoul(buf, 0xffffffffUL, &t))
1547 if (mutex_lock_interruptible(&hotkey_mutex))
1548 return -ERESTARTSYS;
1550 res = hotkey_mask_set(t);
1552 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1553 hotkey_poll_setup(1);
1556 mutex_unlock(&hotkey_mutex);
1558 return (res) ? res : count;
1561 static struct device_attribute dev_attr_hotkey_mask =
1562 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1563 hotkey_mask_show, hotkey_mask_store);
1565 /* sysfs hotkey bios_enabled ------------------------------------------- */
1566 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1567 struct device_attribute *attr,
1570 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1573 static struct device_attribute dev_attr_hotkey_bios_enabled =
1574 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1576 /* sysfs hotkey bios_mask ---------------------------------------------- */
1577 static ssize_t hotkey_bios_mask_show(struct device *dev,
1578 struct device_attribute *attr,
1581 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1584 static struct device_attribute dev_attr_hotkey_bios_mask =
1585 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1587 /* sysfs hotkey all_mask ----------------------------------------------- */
1588 static ssize_t hotkey_all_mask_show(struct device *dev,
1589 struct device_attribute *attr,
1592 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1593 hotkey_all_mask | hotkey_source_mask);
1596 static struct device_attribute dev_attr_hotkey_all_mask =
1597 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1599 /* sysfs hotkey recommended_mask --------------------------------------- */
1600 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1601 struct device_attribute *attr,
1604 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1605 (hotkey_all_mask | hotkey_source_mask)
1606 & ~hotkey_reserved_mask);
1609 static struct device_attribute dev_attr_hotkey_recommended_mask =
1610 __ATTR(hotkey_recommended_mask, S_IRUGO,
1611 hotkey_recommended_mask_show, NULL);
1613 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1615 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1616 static ssize_t hotkey_source_mask_show(struct device *dev,
1617 struct device_attribute *attr,
1620 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1623 static ssize_t hotkey_source_mask_store(struct device *dev,
1624 struct device_attribute *attr,
1625 const char *buf, size_t count)
1629 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1630 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1633 if (mutex_lock_interruptible(&hotkey_mutex))
1634 return -ERESTARTSYS;
1636 HOTKEY_CONFIG_CRITICAL_START
1637 hotkey_source_mask = t;
1638 HOTKEY_CONFIG_CRITICAL_END
1640 hotkey_poll_setup(1);
1642 mutex_unlock(&hotkey_mutex);
1647 static struct device_attribute dev_attr_hotkey_source_mask =
1648 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1649 hotkey_source_mask_show, hotkey_source_mask_store);
1651 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1652 static ssize_t hotkey_poll_freq_show(struct device *dev,
1653 struct device_attribute *attr,
1656 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1659 static ssize_t hotkey_poll_freq_store(struct device *dev,
1660 struct device_attribute *attr,
1661 const char *buf, size_t count)
1665 if (parse_strtoul(buf, 25, &t))
1668 if (mutex_lock_interruptible(&hotkey_mutex))
1669 return -ERESTARTSYS;
1671 hotkey_poll_freq = t;
1673 hotkey_poll_setup(1);
1674 mutex_unlock(&hotkey_mutex);
1679 static struct device_attribute dev_attr_hotkey_poll_freq =
1680 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1681 hotkey_poll_freq_show, hotkey_poll_freq_store);
1683 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1685 /* sysfs hotkey radio_sw ----------------------------------------------- */
1686 static ssize_t hotkey_radio_sw_show(struct device *dev,
1687 struct device_attribute *attr,
1691 res = hotkey_get_wlsw(&s);
1695 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1698 static struct device_attribute dev_attr_hotkey_radio_sw =
1699 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1701 /* sysfs hotkey report_mode -------------------------------------------- */
1702 static ssize_t hotkey_report_mode_show(struct device *dev,
1703 struct device_attribute *attr,
1706 return snprintf(buf, PAGE_SIZE, "%d\n",
1707 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1710 static struct device_attribute dev_attr_hotkey_report_mode =
1711 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1713 /* --------------------------------------------------------------------- */
1715 static struct attribute *hotkey_attributes[] __initdata = {
1716 &dev_attr_hotkey_enable.attr,
1717 &dev_attr_hotkey_bios_enabled.attr,
1718 &dev_attr_hotkey_report_mode.attr,
1719 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1720 &dev_attr_hotkey_mask.attr,
1721 &dev_attr_hotkey_all_mask.attr,
1722 &dev_attr_hotkey_recommended_mask.attr,
1723 &dev_attr_hotkey_source_mask.attr,
1724 &dev_attr_hotkey_poll_freq.attr,
1728 static struct attribute *hotkey_mask_attributes[] __initdata = {
1729 &dev_attr_hotkey_bios_mask.attr,
1730 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1731 &dev_attr_hotkey_mask.attr,
1732 &dev_attr_hotkey_all_mask.attr,
1733 &dev_attr_hotkey_recommended_mask.attr,
1737 static int __init hotkey_init(struct ibm_init_struct *iibm)
1739 /* Requirements for changing the default keymaps:
1741 * 1. Many of the keys are mapped to KEY_RESERVED for very
1742 * good reasons. Do not change them unless you have deep
1743 * knowledge on the IBM and Lenovo ThinkPad firmware for
1744 * the various ThinkPad models. The driver behaves
1745 * differently for KEY_RESERVED: such keys have their
1746 * hot key mask *unset* in mask_recommended, and also
1747 * in the initial hot key mask programmed into the
1748 * firmware at driver load time, which means the firm-
1749 * ware may react very differently if you change them to
1752 * 2. You must be subscribed to the linux-thinkpad and
1753 * ibm-acpi-devel mailing lists, and you should read the
1754 * list archives since 2007 if you want to change the
1755 * keymaps. This requirement exists so that you will
1756 * know the past history of problems with the thinkpad-
1757 * acpi driver keymaps, and also that you will be
1758 * listening to any bug reports;
1760 * 3. Do not send thinkpad-acpi specific patches directly to
1761 * for merging, *ever*. Send them to the linux-acpi
1762 * mailinglist for comments. Merging is to be done only
1763 * through acpi-test and the ACPI maintainer.
1765 * If the above is too much to ask, don't change the keymap.
1766 * Ask the thinkpad-acpi maintainer to do it, instead.
1768 static u16 ibm_keycode_map[] __initdata = {
1769 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1770 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1771 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1772 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1774 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1775 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1776 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1777 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1779 /* brightness: firmware always reacts to them, unless
1780 * X.org did some tricks in the radeon BIOS scratch
1781 * registers of *some* models */
1782 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1783 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1785 /* Thinklight: firmware always react to it */
1786 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1788 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1789 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1791 /* Volume: firmware always react to it and reprograms
1792 * the built-in *extra* mixer. Never map it to control
1793 * another mixer by default. */
1794 KEY_RESERVED, /* 0x14: VOLUME UP */
1795 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1796 KEY_RESERVED, /* 0x16: MUTE */
1798 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1800 /* (assignments unknown, please report if found) */
1801 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1802 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1804 static u16 lenovo_keycode_map[] __initdata = {
1805 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1806 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1807 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1808 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
1810 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
1811 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1812 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1813 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
1815 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
1816 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
1818 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
1820 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1821 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
1823 /* Volume: z60/z61, T60 (BIOS version?): firmware always
1824 * react to it and reprograms the built-in *extra* mixer.
1825 * Never map it to control another mixer by default.
1827 * T60?, T61, R60?, R61: firmware and EC tries to send
1828 * these over the regular keyboard, so these are no-ops,
1829 * but there are still weird bugs re. MUTE, so do not
1830 * change unless you get test reports from all Lenovo
1831 * models. May cause the BIOS to interfere with the
1834 KEY_RESERVED, /* 0x14: VOLUME UP */
1835 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1836 KEY_RESERVED, /* 0x16: MUTE */
1838 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
1840 /* (assignments unknown, please report if found) */
1841 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1842 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1845 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
1846 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
1847 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
1853 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
1855 BUG_ON(!tpacpi_inputdev);
1856 BUG_ON(tpacpi_inputdev->open != NULL ||
1857 tpacpi_inputdev->close != NULL);
1859 IBM_ACPIHANDLE_INIT(hkey);
1860 mutex_init(&hotkey_mutex);
1862 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1863 mutex_init(&hotkey_thread_mutex);
1864 mutex_init(&hotkey_thread_data_mutex);
1867 /* hotkey not supported on 570 */
1868 tp_features.hotkey = hkey_handle != NULL;
1870 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
1871 str_supported(tp_features.hotkey));
1873 if (tp_features.hotkey) {
1874 hotkey_dev_attributes = create_attr_set(10, NULL);
1875 if (!hotkey_dev_attributes)
1877 res = add_many_to_attr_set(hotkey_dev_attributes,
1879 ARRAY_SIZE(hotkey_attributes));
1883 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
1884 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
1885 for HKEY interface version 0x100 */
1886 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
1887 if ((hkeyv >> 8) != 1) {
1888 printk(IBM_ERR "unknown version of the "
1889 "HKEY interface: 0x%x\n", hkeyv);
1890 printk(IBM_ERR "please report this to %s\n",
1894 * MHKV 0x100 in A31, R40, R40e,
1895 * T4x, X31, and later
1897 tp_features.hotkey_mask = 1;
1901 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
1902 str_supported(tp_features.hotkey_mask));
1904 if (tp_features.hotkey_mask) {
1905 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
1908 "missing MHKA handler, "
1909 "please report this to %s\n",
1911 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
1915 /* hotkey_source_mask *must* be zero for
1916 * the first hotkey_mask_get */
1917 res = hotkey_status_get(&hotkey_orig_status);
1918 if (!res && tp_features.hotkey_mask) {
1919 res = hotkey_mask_get();
1920 hotkey_orig_mask = hotkey_mask;
1922 res = add_many_to_attr_set(
1923 hotkey_dev_attributes,
1924 hotkey_mask_attributes,
1925 ARRAY_SIZE(hotkey_mask_attributes));
1929 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1930 if (tp_features.hotkey_mask) {
1931 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
1934 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
1937 vdbg_printk(TPACPI_DBG_INIT,
1938 "hotkey source mask 0x%08x, polling freq %d\n",
1939 hotkey_source_mask, hotkey_poll_freq);
1942 /* Not all thinkpads have a hardware radio switch */
1943 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
1944 tp_features.hotkey_wlsw = 1;
1946 "radio switch found; radios are %s\n",
1947 enabled(status, 0));
1948 res = add_to_attr_set(hotkey_dev_attributes,
1949 &dev_attr_hotkey_radio_sw.attr);
1953 res = register_attr_set_with_sysfs(
1954 hotkey_dev_attributes,
1955 &tpacpi_pdev->dev.kobj);
1959 /* Set up key map */
1961 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
1963 if (!hotkey_keycode_map) {
1964 printk(IBM_ERR "failed to allocate memory for key map\n");
1968 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
1969 dbg_printk(TPACPI_DBG_INIT,
1970 "using Lenovo default hot key map\n");
1971 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
1972 TPACPI_HOTKEY_MAP_SIZE);
1974 dbg_printk(TPACPI_DBG_INIT,
1975 "using IBM default hot key map\n");
1976 memcpy(hotkey_keycode_map, &ibm_keycode_map,
1977 TPACPI_HOTKEY_MAP_SIZE);
1980 set_bit(EV_KEY, tpacpi_inputdev->evbit);
1981 set_bit(EV_MSC, tpacpi_inputdev->evbit);
1982 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
1983 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
1984 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
1985 tpacpi_inputdev->keycode = hotkey_keycode_map;
1986 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
1987 if (hotkey_keycode_map[i] != KEY_RESERVED) {
1988 set_bit(hotkey_keycode_map[i],
1989 tpacpi_inputdev->keybit);
1991 if (i < sizeof(hotkey_reserved_mask)*8)
1992 hotkey_reserved_mask |= 1 << i;
1996 if (tp_features.hotkey_wlsw) {
1997 set_bit(EV_SW, tpacpi_inputdev->evbit);
1998 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2001 dbg_printk(TPACPI_DBG_INIT,
2002 "enabling hot key handling\n");
2003 res = hotkey_status_set(1);
2006 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2007 & ~hotkey_reserved_mask)
2008 | hotkey_orig_mask);
2009 if (res < 0 && res != -ENXIO)
2012 dbg_printk(TPACPI_DBG_INIT,
2013 "legacy hot key reporting over procfs %s\n",
2014 (hotkey_report_mode < 2) ?
2015 "enabled" : "disabled");
2017 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2018 tpacpi_inputdev->open = &hotkey_inputdev_open;
2019 tpacpi_inputdev->close = &hotkey_inputdev_close;
2021 hotkey_poll_setup_safe(1);
2025 return (tp_features.hotkey)? 0 : 1;
2028 static void hotkey_exit(void)
2030 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2031 hotkey_poll_stop_sync();
2034 if (tp_features.hotkey) {
2035 dbg_printk(TPACPI_DBG_EXIT, "restoring original hot key mask\n");
2036 /* no short-circuit boolean operator below! */
2037 if ((hotkey_mask_set(hotkey_orig_mask) |
2038 hotkey_status_set(hotkey_orig_status)) != 0)
2039 printk(IBM_ERR "failed to restore hot key mask to BIOS defaults\n");
2042 if (hotkey_dev_attributes) {
2043 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2044 hotkey_dev_attributes = NULL;
2048 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2051 unsigned int scancode;
2055 if (event != 0x80) {
2056 printk(IBM_ERR "unknown HKEY notification event %d\n", event);
2057 /* forward it to userspace, maybe it knows how to handle it */
2058 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2059 ibm->acpi->device->dev.bus_id,
2065 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2066 printk(IBM_ERR "failed to retrieve HKEY event\n");
2078 switch (hkey >> 12) {
2080 /* 0x1000-0x1FFF: key presses */
2081 scancode = hkey & 0xfff;
2082 if (scancode > 0 && scancode < 0x21) {
2084 if (!(hotkey_source_mask & (1 << scancode))) {
2085 tpacpi_input_send_key(scancode);
2091 "hotkey 0x%04x out of range for keyboard map\n",
2097 /* 0x5000-0x5FFF: LID */
2098 /* we don't handle it through this path, just
2099 * eat up known LID events */
2100 if (hkey != 0x5001 && hkey != 0x5002) {
2102 "unknown LID-related HKEY event: 0x%04x\n",
2110 /* 0x7000-0x7FFF: misc */
2111 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2112 tpacpi_input_send_radiosw();
2115 /* fallthrough to default */
2117 /* case 2: dock-related */
2118 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
2119 /* case 3: ultra-bay related. maybe bay in dock? */
2120 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
2121 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
2126 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
2127 acpi_bus_generate_proc_event(ibm->acpi->device, event, hkey);
2130 /* netlink events */
2131 if (!ignore_acpi_ev && send_acpi_ev) {
2132 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
2133 ibm->acpi->device->dev.bus_id,
2139 static void hotkey_resume(void)
2141 if (hotkey_mask_get())
2142 printk(IBM_ERR "error while trying to read hot key mask from firmware\n");
2143 tpacpi_input_send_radiosw();
2144 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2145 hotkey_poll_setup_safe(0);
2149 /* procfs -------------------------------------------------------------- */
2150 static int hotkey_read(char *p)
2155 if (!tp_features.hotkey) {
2156 len += sprintf(p + len, "status:\t\tnot supported\n");
2160 if (mutex_lock_interruptible(&hotkey_mutex))
2161 return -ERESTARTSYS;
2162 res = hotkey_status_get(&status);
2164 res = hotkey_mask_get();
2165 mutex_unlock(&hotkey_mutex);
2169 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2170 if (tp_features.hotkey_mask) {
2171 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2172 len += sprintf(p + len,
2173 "commands:\tenable, disable, reset, <mask>\n");
2175 len += sprintf(p + len, "mask:\t\tnot supported\n");
2176 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2182 static int hotkey_write(char *buf)
2188 if (!tp_features.hotkey)
2191 if (mutex_lock_interruptible(&hotkey_mutex))
2192 return -ERESTARTSYS;
2198 while ((cmd = next_cmd(&buf))) {
2199 if (strlencmp(cmd, "enable") == 0) {
2201 } else if (strlencmp(cmd, "disable") == 0) {
2203 } else if (strlencmp(cmd, "reset") == 0) {
2204 status = hotkey_orig_status;
2205 mask = hotkey_orig_mask;
2206 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2208 } else if (sscanf(cmd, "%x", &mask) == 1) {
2216 res = hotkey_status_set(status);
2218 if (!res && mask != hotkey_mask)
2219 res = hotkey_mask_set(mask);
2222 mutex_unlock(&hotkey_mutex);
2226 static const struct acpi_device_id ibm_htk_device_ids[] = {
2231 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2232 .hid = ibm_htk_device_ids,
2233 .notify = hotkey_notify,
2234 .handle = &hkey_handle,
2235 .type = ACPI_DEVICE_NOTIFY,
2238 static struct ibm_struct hotkey_driver_data = {
2240 .read = hotkey_read,
2241 .write = hotkey_write,
2242 .exit = hotkey_exit,
2243 .resume = hotkey_resume,
2244 .acpi = &ibm_hotkey_acpidriver,
2247 /*************************************************************************
2248 * Bluetooth subdriver
2252 /* ACPI GBDC/SBDC bits */
2253 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2254 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2255 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2258 static int bluetooth_get_radiosw(void);
2259 static int bluetooth_set_radiosw(int radio_on);
2261 /* sysfs bluetooth enable ---------------------------------------------- */
2262 static ssize_t bluetooth_enable_show(struct device *dev,
2263 struct device_attribute *attr,
2268 status = bluetooth_get_radiosw();
2272 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2275 static ssize_t bluetooth_enable_store(struct device *dev,
2276 struct device_attribute *attr,
2277 const char *buf, size_t count)
2282 if (parse_strtoul(buf, 1, &t))
2285 res = bluetooth_set_radiosw(t);
2287 return (res) ? res : count;
2290 static struct device_attribute dev_attr_bluetooth_enable =
2291 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2292 bluetooth_enable_show, bluetooth_enable_store);
2294 /* --------------------------------------------------------------------- */
2296 static struct attribute *bluetooth_attributes[] = {
2297 &dev_attr_bluetooth_enable.attr,
2301 static const struct attribute_group bluetooth_attr_group = {
2302 .attrs = bluetooth_attributes,
2305 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2310 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2312 IBM_ACPIHANDLE_INIT(hkey);
2314 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2315 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2316 tp_features.bluetooth = hkey_handle &&
2317 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2319 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2320 str_supported(tp_features.bluetooth),
2323 if (tp_features.bluetooth) {
2324 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2325 /* no bluetooth hardware present in system */
2326 tp_features.bluetooth = 0;
2327 dbg_printk(TPACPI_DBG_INIT,
2328 "bluetooth hardware not installed\n");
2330 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2331 &bluetooth_attr_group);
2337 return (tp_features.bluetooth)? 0 : 1;
2340 static void bluetooth_exit(void)
2342 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2343 &bluetooth_attr_group);
2346 static int bluetooth_get_radiosw(void)
2350 if (!tp_features.bluetooth)
2353 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2356 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2359 static int bluetooth_set_radiosw(int radio_on)
2363 if (!tp_features.bluetooth)
2366 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2369 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2371 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2372 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2378 /* procfs -------------------------------------------------------------- */
2379 static int bluetooth_read(char *p)
2382 int status = bluetooth_get_radiosw();
2384 if (!tp_features.bluetooth)
2385 len += sprintf(p + len, "status:\t\tnot supported\n");
2387 len += sprintf(p + len, "status:\t\t%s\n",
2388 (status)? "enabled" : "disabled");
2389 len += sprintf(p + len, "commands:\tenable, disable\n");
2395 static int bluetooth_write(char *buf)
2399 if (!tp_features.bluetooth)
2402 while ((cmd = next_cmd(&buf))) {
2403 if (strlencmp(cmd, "enable") == 0) {
2404 bluetooth_set_radiosw(1);
2405 } else if (strlencmp(cmd, "disable") == 0) {
2406 bluetooth_set_radiosw(0);
2414 static struct ibm_struct bluetooth_driver_data = {
2415 .name = "bluetooth",
2416 .read = bluetooth_read,
2417 .write = bluetooth_write,
2418 .exit = bluetooth_exit,
2421 /*************************************************************************
2426 /* ACPI GWAN/SWAN bits */
2427 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2428 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2429 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2432 static int wan_get_radiosw(void);
2433 static int wan_set_radiosw(int radio_on);
2435 /* sysfs wan enable ---------------------------------------------------- */
2436 static ssize_t wan_enable_show(struct device *dev,
2437 struct device_attribute *attr,
2442 status = wan_get_radiosw();
2446 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2449 static ssize_t wan_enable_store(struct device *dev,
2450 struct device_attribute *attr,
2451 const char *buf, size_t count)
2456 if (parse_strtoul(buf, 1, &t))
2459 res = wan_set_radiosw(t);
2461 return (res) ? res : count;
2464 static struct device_attribute dev_attr_wan_enable =
2465 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2466 wan_enable_show, wan_enable_store);
2468 /* --------------------------------------------------------------------- */
2470 static struct attribute *wan_attributes[] = {
2471 &dev_attr_wan_enable.attr,
2475 static const struct attribute_group wan_attr_group = {
2476 .attrs = wan_attributes,
2479 static int __init wan_init(struct ibm_init_struct *iibm)
2484 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2486 IBM_ACPIHANDLE_INIT(hkey);
2488 tp_features.wan = hkey_handle &&
2489 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2491 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2492 str_supported(tp_features.wan),
2495 if (tp_features.wan) {
2496 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2497 /* no wan hardware present in system */
2498 tp_features.wan = 0;
2499 dbg_printk(TPACPI_DBG_INIT,
2500 "wan hardware not installed\n");
2502 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2509 return (tp_features.wan)? 0 : 1;
2512 static void wan_exit(void)
2514 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2518 static int wan_get_radiosw(void)
2522 if (!tp_features.wan)
2525 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2528 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2531 static int wan_set_radiosw(int radio_on)
2535 if (!tp_features.wan)
2538 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2541 status |= TP_ACPI_WANCARD_RADIOSSW;
2543 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2544 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2550 /* procfs -------------------------------------------------------------- */
2551 static int wan_read(char *p)
2554 int status = wan_get_radiosw();
2556 if (!tp_features.wan)
2557 len += sprintf(p + len, "status:\t\tnot supported\n");
2559 len += sprintf(p + len, "status:\t\t%s\n",
2560 (status)? "enabled" : "disabled");
2561 len += sprintf(p + len, "commands:\tenable, disable\n");
2567 static int wan_write(char *buf)
2571 if (!tp_features.wan)
2574 while ((cmd = next_cmd(&buf))) {
2575 if (strlencmp(cmd, "enable") == 0) {
2577 } else if (strlencmp(cmd, "disable") == 0) {
2586 static struct ibm_struct wan_driver_data = {
2591 .flags.experimental = 1,
2594 /*************************************************************************
2598 enum video_access_mode {
2599 TPACPI_VIDEO_NONE = 0,
2600 TPACPI_VIDEO_570, /* 570 */
2601 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2602 TPACPI_VIDEO_NEW, /* all others */
2605 enum { /* video status flags, based on VIDEO_570 */
2606 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2607 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2608 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2611 enum { /* TPACPI_VIDEO_570 constants */
2612 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2613 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2614 * video_status_flags */
2615 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2616 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2619 static enum video_access_mode video_supported;
2620 static int video_orig_autosw;
2622 static int video_autosw_get(void);
2623 static int video_autosw_set(int enable);
2625 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2626 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2627 "\\_SB.PCI0.VID0", /* 770e */
2628 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2629 "\\_SB.PCI0.AGP.VID", /* all others */
2632 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2634 static int __init video_init(struct ibm_init_struct *iibm)
2638 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2640 IBM_ACPIHANDLE_INIT(vid);
2641 IBM_ACPIHANDLE_INIT(vid2);
2643 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2644 /* G41, assume IVGA doesn't change */
2645 vid_handle = vid2_handle;
2648 /* video switching not supported on R30, R31 */
2649 video_supported = TPACPI_VIDEO_NONE;
2650 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2652 video_supported = TPACPI_VIDEO_570;
2653 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2654 /* 600e/x, 770e, 770x */
2655 video_supported = TPACPI_VIDEO_770;
2658 video_supported = TPACPI_VIDEO_NEW;
2660 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2661 str_supported(video_supported != TPACPI_VIDEO_NONE),
2664 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2667 static void video_exit(void)
2669 dbg_printk(TPACPI_DBG_EXIT,
2670 "restoring original video autoswitch mode\n");
2671 if (video_autosw_set(video_orig_autosw))
2672 printk(IBM_ERR "error while trying to restore original "
2673 "video autoswitch mode\n");
2676 static int video_outputsw_get(void)
2681 switch (video_supported) {
2682 case TPACPI_VIDEO_570:
2683 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2684 TP_ACPI_VIDEO_570_PHSCMD))
2686 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2688 case TPACPI_VIDEO_770:
2689 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2692 status |= TP_ACPI_VIDEO_S_LCD;
2693 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2696 status |= TP_ACPI_VIDEO_S_CRT;
2698 case TPACPI_VIDEO_NEW:
2699 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2700 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2703 status |= TP_ACPI_VIDEO_S_CRT;
2705 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2706 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2709 status |= TP_ACPI_VIDEO_S_LCD;
2710 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2713 status |= TP_ACPI_VIDEO_S_DVI;
2722 static int video_outputsw_set(int status)
2727 switch (video_supported) {
2728 case TPACPI_VIDEO_570:
2729 res = acpi_evalf(NULL, NULL,
2730 "\\_SB.PHS2", "vdd",
2731 TP_ACPI_VIDEO_570_PHS2CMD,
2732 status | TP_ACPI_VIDEO_570_PHS2SET);
2734 case TPACPI_VIDEO_770:
2735 autosw = video_autosw_get();
2739 res = video_autosw_set(1);
2742 res = acpi_evalf(vid_handle, NULL,
2743 "ASWT", "vdd", status * 0x100, 0);
2744 if (!autosw && video_autosw_set(autosw)) {
2745 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2749 case TPACPI_VIDEO_NEW:
2750 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
2751 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
2757 return (res)? 0 : -EIO;
2760 static int video_autosw_get(void)
2764 switch (video_supported) {
2765 case TPACPI_VIDEO_570:
2766 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
2769 case TPACPI_VIDEO_770:
2770 case TPACPI_VIDEO_NEW:
2771 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
2781 static int video_autosw_set(int enable)
2783 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
2788 static int video_outputsw_cycle(void)
2790 int autosw = video_autosw_get();
2796 switch (video_supported) {
2797 case TPACPI_VIDEO_570:
2798 res = video_autosw_set(1);
2801 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
2803 case TPACPI_VIDEO_770:
2804 case TPACPI_VIDEO_NEW:
2805 res = video_autosw_set(1);
2808 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
2813 if (!autosw && video_autosw_set(autosw)) {
2814 printk(IBM_ERR "video auto-switch left enabled due to error\n");
2818 return (res)? 0 : -EIO;
2821 static int video_expand_toggle(void)
2823 switch (video_supported) {
2824 case TPACPI_VIDEO_570:
2825 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
2827 case TPACPI_VIDEO_770:
2828 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
2830 case TPACPI_VIDEO_NEW:
2831 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
2839 static int video_read(char *p)
2844 if (video_supported == TPACPI_VIDEO_NONE) {
2845 len += sprintf(p + len, "status:\t\tnot supported\n");
2849 status = video_outputsw_get();
2853 autosw = video_autosw_get();
2857 len += sprintf(p + len, "status:\t\tsupported\n");
2858 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
2859 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
2860 if (video_supported == TPACPI_VIDEO_NEW)
2861 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
2862 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
2863 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
2864 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
2865 if (video_supported == TPACPI_VIDEO_NEW)
2866 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
2867 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
2868 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
2873 static int video_write(char *buf)
2876 int enable, disable, status;
2879 if (video_supported == TPACPI_VIDEO_NONE)
2885 while ((cmd = next_cmd(&buf))) {
2886 if (strlencmp(cmd, "lcd_enable") == 0) {
2887 enable |= TP_ACPI_VIDEO_S_LCD;
2888 } else if (strlencmp(cmd, "lcd_disable") == 0) {
2889 disable |= TP_ACPI_VIDEO_S_LCD;
2890 } else if (strlencmp(cmd, "crt_enable") == 0) {
2891 enable |= TP_ACPI_VIDEO_S_CRT;
2892 } else if (strlencmp(cmd, "crt_disable") == 0) {
2893 disable |= TP_ACPI_VIDEO_S_CRT;
2894 } else if (video_supported == TPACPI_VIDEO_NEW &&
2895 strlencmp(cmd, "dvi_enable") == 0) {
2896 enable |= TP_ACPI_VIDEO_S_DVI;
2897 } else if (video_supported == TPACPI_VIDEO_NEW &&
2898 strlencmp(cmd, "dvi_disable") == 0) {
2899 disable |= TP_ACPI_VIDEO_S_DVI;
2900 } else if (strlencmp(cmd, "auto_enable") == 0) {
2901 res = video_autosw_set(1);
2904 } else if (strlencmp(cmd, "auto_disable") == 0) {
2905 res = video_autosw_set(0);
2908 } else if (strlencmp(cmd, "video_switch") == 0) {
2909 res = video_outputsw_cycle();
2912 } else if (strlencmp(cmd, "expand_toggle") == 0) {
2913 res = video_expand_toggle();
2920 if (enable || disable) {
2921 status = video_outputsw_get();
2924 res = video_outputsw_set((status & ~disable) | enable);
2932 static struct ibm_struct video_driver_data = {
2935 .write = video_write,
2939 /*************************************************************************
2940 * Light (thinklight) subdriver
2943 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
2944 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
2946 static int __init light_init(struct ibm_init_struct *iibm)
2948 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
2950 IBM_ACPIHANDLE_INIT(ledb);
2951 IBM_ACPIHANDLE_INIT(lght);
2952 IBM_ACPIHANDLE_INIT(cmos);
2954 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
2955 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
2957 if (tp_features.light)
2958 /* light status not supported on
2959 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
2960 tp_features.light_status =
2961 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
2963 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
2964 str_supported(tp_features.light));
2966 return (tp_features.light)? 0 : 1;
2969 static int light_read(char *p)
2974 if (!tp_features.light) {
2975 len += sprintf(p + len, "status:\t\tnot supported\n");
2976 } else if (!tp_features.light_status) {
2977 len += sprintf(p + len, "status:\t\tunknown\n");
2978 len += sprintf(p + len, "commands:\ton, off\n");
2980 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
2982 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
2983 len += sprintf(p + len, "commands:\ton, off\n");
2989 static int light_write(char *buf)
2991 int cmos_cmd, lght_cmd;
2995 if (!tp_features.light)
2998 while ((cmd = next_cmd(&buf))) {
2999 if (strlencmp(cmd, "on") == 0) {
3002 } else if (strlencmp(cmd, "off") == 0) {
3008 success = cmos_handle ?
3009 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3010 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
3018 static struct ibm_struct light_driver_data = {
3021 .write = light_write,
3024 /*************************************************************************
3028 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3030 static void dock_notify(struct ibm_struct *ibm, u32 event);
3031 static int dock_read(char *p);
3032 static int dock_write(char *buf);
3034 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3035 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3036 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3037 "\\_SB.PCI.ISA.SLCE", /* 570 */
3038 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3040 /* don't list other alternatives as we install a notify handler on the 570 */
3041 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3043 static const struct acpi_device_id ibm_pci_device_ids[] = {
3044 {PCI_ROOT_HID_STRING, 0},
3048 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3050 .notify = dock_notify,
3051 .handle = &dock_handle,
3052 .type = ACPI_SYSTEM_NOTIFY,
3055 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3056 * We just use it to get notifications of dock hotplug
3057 * in very old thinkpads */
3058 .hid = ibm_pci_device_ids,
3059 .notify = dock_notify,
3060 .handle = &pci_handle,
3061 .type = ACPI_SYSTEM_NOTIFY,
3065 static struct ibm_struct dock_driver_data[2] = {
3069 .write = dock_write,
3070 .acpi = &ibm_dock_acpidriver[0],
3074 .acpi = &ibm_dock_acpidriver[1],
3078 #define dock_docked() (_sta(dock_handle) & 1)
3080 static int __init dock_init(struct ibm_init_struct *iibm)
3082 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3084 IBM_ACPIHANDLE_INIT(dock);
3086 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3087 str_supported(dock_handle != NULL));
3089 return (dock_handle)? 0 : 1;
3092 static int __init dock_init2(struct ibm_init_struct *iibm)
3096 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3098 if (dock_driver_data[0].flags.acpi_driver_registered &&
3099 dock_driver_data[0].flags.acpi_notify_installed) {
3100 IBM_ACPIHANDLE_INIT(pci);
3101 dock2_needed = (pci_handle != NULL);
3102 vdbg_printk(TPACPI_DBG_INIT,
3103 "dock PCI handler for the TP 570 is %s\n",
3104 str_supported(dock2_needed));
3106 vdbg_printk(TPACPI_DBG_INIT,
3107 "dock subdriver part 2 not required\n");
3111 return (dock2_needed)? 0 : 1;
3114 static void dock_notify(struct ibm_struct *ibm, u32 event)
3116 int docked = dock_docked();
3117 int pci = ibm->acpi->hid && ibm->acpi->device &&
3118 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3121 if (event == 1 && !pci) /* 570 */
3122 data = 1; /* button */
3123 else if (event == 1 && pci) /* 570 */
3124 data = 3; /* dock */
3125 else if (event == 3 && docked)
3126 data = 1; /* button */
3127 else if (event == 3 && !docked)
3128 data = 2; /* undock */
3129 else if (event == 0 && docked)
3130 data = 3; /* dock */
3132 printk(IBM_ERR "unknown dock event %d, status %d\n",
3133 event, _sta(dock_handle));
3134 data = 0; /* unknown */
3136 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3137 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3138 ibm->acpi->device->dev.bus_id,
3142 static int dock_read(char *p)
3145 int docked = dock_docked();
3148 len += sprintf(p + len, "status:\t\tnot supported\n");
3150 len += sprintf(p + len, "status:\t\tundocked\n");
3152 len += sprintf(p + len, "status:\t\tdocked\n");
3153 len += sprintf(p + len, "commands:\tdock, undock\n");
3159 static int dock_write(char *buf)
3166 while ((cmd = next_cmd(&buf))) {
3167 if (strlencmp(cmd, "undock") == 0) {
3168 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3169 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3171 } else if (strlencmp(cmd, "dock") == 0) {
3172 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3181 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3183 /*************************************************************************
3187 #ifdef CONFIG_THINKPAD_ACPI_BAY
3189 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3190 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3191 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3192 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3193 ); /* A21e, R30, R31 */
3194 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3195 "_EJ0", /* all others */
3196 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3197 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3198 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3200 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3204 static int __init bay_init(struct ibm_init_struct *iibm)
3206 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3208 IBM_ACPIHANDLE_INIT(bay);
3210 IBM_ACPIHANDLE_INIT(bay_ej);
3211 IBM_ACPIHANDLE_INIT(bay2);
3213 IBM_ACPIHANDLE_INIT(bay2_ej);
3215 tp_features.bay_status = bay_handle &&
3216 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3217 tp_features.bay_status2 = bay2_handle &&
3218 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3220 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3221 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3222 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3223 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3225 vdbg_printk(TPACPI_DBG_INIT,
3226 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3227 str_supported(tp_features.bay_status),
3228 str_supported(tp_features.bay_eject),
3229 str_supported(tp_features.bay_status2),
3230 str_supported(tp_features.bay_eject2));
3232 return (tp_features.bay_status || tp_features.bay_eject ||
3233 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3236 static void bay_notify(struct ibm_struct *ibm, u32 event)
3238 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3239 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3240 ibm->acpi->device->dev.bus_id,
3244 #define bay_occupied(b) (_sta(b##_handle) & 1)
3246 static int bay_read(char *p)
3249 int occupied = bay_occupied(bay);
3250 int occupied2 = bay_occupied(bay2);
3253 len += sprintf(p + len, "status:\t\t%s\n",
3254 tp_features.bay_status ?
3255 (occupied ? "occupied" : "unoccupied") :
3257 if (tp_features.bay_status2)
3258 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3259 "occupied" : "unoccupied");
3261 eject = tp_features.bay_eject && occupied;
3262 eject2 = tp_features.bay_eject2 && occupied2;
3264 if (eject && eject2)
3265 len += sprintf(p + len, "commands:\teject, eject2\n");
3267 len += sprintf(p + len, "commands:\teject\n");
3269 len += sprintf(p + len, "commands:\teject2\n");
3274 static int bay_write(char *buf)
3278 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3281 while ((cmd = next_cmd(&buf))) {
3282 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3283 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3285 } else if (tp_features.bay_eject2 &&
3286 strlencmp(cmd, "eject2") == 0) {
3287 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3296 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3297 .notify = bay_notify,
3298 .handle = &bay_handle,
3299 .type = ACPI_SYSTEM_NOTIFY,
3302 static struct ibm_struct bay_driver_data = {
3306 .acpi = &ibm_bay_acpidriver,
3309 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3311 /*************************************************************************
3315 /* sysfs cmos_command -------------------------------------------------- */
3316 static ssize_t cmos_command_store(struct device *dev,
3317 struct device_attribute *attr,
3318 const char *buf, size_t count)
3320 unsigned long cmos_cmd;
3323 if (parse_strtoul(buf, 21, &cmos_cmd))
3326 res = issue_thinkpad_cmos_command(cmos_cmd);
3327 return (res)? res : count;
3330 static struct device_attribute dev_attr_cmos_command =
3331 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3333 /* --------------------------------------------------------------------- */
3335 static int __init cmos_init(struct ibm_init_struct *iibm)
3339 vdbg_printk(TPACPI_DBG_INIT,
3340 "initializing cmos commands subdriver\n");
3342 IBM_ACPIHANDLE_INIT(cmos);
3344 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3345 str_supported(cmos_handle != NULL));
3347 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3351 return (cmos_handle)? 0 : 1;
3354 static void cmos_exit(void)
3356 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3359 static int cmos_read(char *p)
3363 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3364 R30, R31, T20-22, X20-21 */
3366 len += sprintf(p + len, "status:\t\tnot supported\n");
3368 len += sprintf(p + len, "status:\t\tsupported\n");
3369 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3375 static int cmos_write(char *buf)
3380 while ((cmd = next_cmd(&buf))) {
3381 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3382 cmos_cmd >= 0 && cmos_cmd <= 21) {
3387 res = issue_thinkpad_cmos_command(cmos_cmd);
3395 static struct ibm_struct cmos_driver_data = {
3398 .write = cmos_write,
3402 /*************************************************************************
3406 enum led_access_mode {
3407 TPACPI_LED_NONE = 0,
3408 TPACPI_LED_570, /* 570 */
3409 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3410 TPACPI_LED_NEW, /* all others */
3413 enum { /* For TPACPI_LED_OLD */
3414 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3415 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3416 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3419 static enum led_access_mode led_supported;
3421 IBM_HANDLE(led, ec, "SLED", /* 570 */
3422 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3423 "LED", /* all others */
3426 static int __init led_init(struct ibm_init_struct *iibm)
3428 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3430 IBM_ACPIHANDLE_INIT(led);
3433 /* led not supported on R30, R31 */
3434 led_supported = TPACPI_LED_NONE;
3435 else if (strlencmp(led_path, "SLED") == 0)
3437 led_supported = TPACPI_LED_570;
3438 else if (strlencmp(led_path, "SYSL") == 0)
3439 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3440 led_supported = TPACPI_LED_OLD;
3443 led_supported = TPACPI_LED_NEW;
3445 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3446 str_supported(led_supported), led_supported);
3448 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3451 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3453 static int led_read(char *p)
3457 if (!led_supported) {
3458 len += sprintf(p + len, "status:\t\tnot supported\n");
3461 len += sprintf(p + len, "status:\t\tsupported\n");
3463 if (led_supported == TPACPI_LED_570) {
3466 for (i = 0; i < 8; i++) {
3467 if (!acpi_evalf(ec_handle,
3468 &status, "GLED", "dd", 1 << i))
3470 len += sprintf(p + len, "%d:\t\t%s\n",
3471 i, led_status(status));
3475 len += sprintf(p + len, "commands:\t"
3476 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3481 /* off, on, blink */
3482 static const int led_sled_arg1[] = { 0, 1, 3 };
3483 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3484 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3485 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3487 static int led_write(char *buf)
3495 while ((cmd = next_cmd(&buf))) {
3496 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3499 if (strstr(cmd, "off")) {
3501 } else if (strstr(cmd, "on")) {
3503 } else if (strstr(cmd, "blink")) {
3508 if (led_supported == TPACPI_LED_570) {
3511 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3512 led, led_sled_arg1[ind]))
3514 } else if (led_supported == TPACPI_LED_OLD) {
3515 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3517 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3520 ec_write(TPACPI_LED_EC_HLBL,
3521 led * led_exp_hlbl[ind]);
3524 ec_write(TPACPI_LED_EC_HLCL,
3525 led * led_exp_hlcl[ind]);
3530 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3531 led, led_led_arg1[ind]))
3539 static struct ibm_struct led_driver_data = {
3545 /*************************************************************************
3549 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3551 static int __init beep_init(struct ibm_init_struct *iibm)
3553 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3555 IBM_ACPIHANDLE_INIT(beep);
3557 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3558 str_supported(beep_handle != NULL));
3560 return (beep_handle)? 0 : 1;
3563 static int beep_read(char *p)
3568 len += sprintf(p + len, "status:\t\tnot supported\n");
3570 len += sprintf(p + len, "status:\t\tsupported\n");
3571 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3577 static int beep_write(char *buf)
3585 while ((cmd = next_cmd(&buf))) {
3586 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3587 beep_cmd >= 0 && beep_cmd <= 17) {
3591 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3598 static struct ibm_struct beep_driver_data = {
3601 .write = beep_write,
3604 /*************************************************************************
3608 enum thermal_access_mode {
3609 TPACPI_THERMAL_NONE = 0, /* No thermal support */
3610 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
3611 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
3612 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
3613 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
3616 enum { /* TPACPI_THERMAL_TPEC_* */
3617 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
3618 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
3619 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
3622 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
3623 struct ibm_thermal_sensors_struct {
3624 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
3627 static enum thermal_access_mode thermal_read_mode;
3629 /* idx is zero-based */
3630 static int thermal_get_sensor(int idx, s32 *value)
3636 t = TP_EC_THERMAL_TMP0;
3638 switch (thermal_read_mode) {
3639 #if TPACPI_MAX_THERMAL_SENSORS >= 16
3640 case TPACPI_THERMAL_TPEC_16:
3641 if (idx >= 8 && idx <= 15) {
3642 t = TP_EC_THERMAL_TMP8;
3647 case TPACPI_THERMAL_TPEC_8:
3649 if (!acpi_ec_read(t + idx, &tmp))
3651 *value = tmp * 1000;
3656 case TPACPI_THERMAL_ACPI_UPDT:
3658 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3659 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
3661 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3663 *value = (t - 2732) * 100;
3668 case TPACPI_THERMAL_ACPI_TMP07:
3670 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
3671 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
3673 if (t > 127 || t < -127)
3674 t = TP_EC_THERMAL_TMP_NA;
3680 case TPACPI_THERMAL_NONE:
3688 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
3699 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
3702 for(i = 0 ; i < n; i++) {
3703 res = thermal_get_sensor(i, &s->temp[i]);
3711 /* sysfs temp##_input -------------------------------------------------- */
3713 static ssize_t thermal_temp_input_show(struct device *dev,
3714 struct device_attribute *attr,
3717 struct sensor_device_attribute *sensor_attr =
3718 to_sensor_dev_attr(attr);
3719 int idx = sensor_attr->index;
3723 res = thermal_get_sensor(idx, &value);
3726 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3729 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3732 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3733 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
3735 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3736 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3737 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3738 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3739 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3740 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3741 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3742 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3743 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3744 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3745 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3746 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3747 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3748 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3749 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3750 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3751 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3754 #define THERMAL_ATTRS(X) \
3755 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3757 static struct attribute *thermal_temp_input_attr[] = {
3777 static const struct attribute_group thermal_temp_input16_group = {
3778 .attrs = thermal_temp_input_attr
3781 static const struct attribute_group thermal_temp_input8_group = {
3782 .attrs = &thermal_temp_input_attr[8]
3785 #undef THERMAL_SENSOR_ATTR_TEMP
3786 #undef THERMAL_ATTRS
3788 /* --------------------------------------------------------------------- */
3790 static int __init thermal_init(struct ibm_init_struct *iibm)
3797 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3799 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3801 if (thinkpad_id.ec_model) {
3803 * Direct EC access mode: sensors at registers
3804 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3805 * non-implemented, thermal sensors return 0x80 when
3810 for (i = 0; i < 8; i++) {
3811 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3817 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3825 /* This is sheer paranoia, but we handle it anyway */
3828 "ThinkPad ACPI EC access misbehaving, "
3829 "falling back to ACPI TMPx access mode\n");
3830 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3833 "ThinkPad ACPI EC access misbehaving, "
3834 "disabling thermal sensors access\n");
3835 thermal_read_mode = TPACPI_THERMAL_NONE;
3840 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3842 } else if (acpi_tmp7) {
3843 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3844 /* 600e/x, 770e, 770x */
3845 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3847 /* Standard ACPI TMPx access, max 8 sensors */
3848 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3851 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3852 thermal_read_mode = TPACPI_THERMAL_NONE;
3855 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3856 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3859 switch(thermal_read_mode) {
3860 case TPACPI_THERMAL_TPEC_16:
3861 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3862 &thermal_temp_input16_group);
3866 case TPACPI_THERMAL_TPEC_8:
3867 case TPACPI_THERMAL_ACPI_TMP07:
3868 case TPACPI_THERMAL_ACPI_UPDT:
3869 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3870 &thermal_temp_input8_group);
3874 case TPACPI_THERMAL_NONE:
3882 static void thermal_exit(void)
3884 switch(thermal_read_mode) {
3885 case TPACPI_THERMAL_TPEC_16:
3886 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3887 &thermal_temp_input16_group);
3889 case TPACPI_THERMAL_TPEC_8:
3890 case TPACPI_THERMAL_ACPI_TMP07:
3891 case TPACPI_THERMAL_ACPI_UPDT:
3892 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
3893 &thermal_temp_input16_group);
3895 case TPACPI_THERMAL_NONE:
3901 static int thermal_read(char *p)
3905 struct ibm_thermal_sensors_struct t;
3907 n = thermal_get_sensors(&t);
3908 if (unlikely(n < 0))
3911 len += sprintf(p + len, "temperatures:\t");
3914 for (i = 0; i < (n - 1); i++)
3915 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
3916 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
3918 len += sprintf(p + len, "not supported\n");
3923 static struct ibm_struct thermal_driver_data = {
3925 .read = thermal_read,
3926 .exit = thermal_exit,
3929 /*************************************************************************
3933 static u8 ecdump_regs[256];
3935 static int ecdump_read(char *p)
3941 len += sprintf(p + len, "EC "
3942 " +00 +01 +02 +03 +04 +05 +06 +07"
3943 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
3944 for (i = 0; i < 256; i += 16) {
3945 len += sprintf(p + len, "EC 0x%02x:", i);
3946 for (j = 0; j < 16; j++) {
3947 if (!acpi_ec_read(i + j, &v))
3949 if (v != ecdump_regs[i + j])
3950 len += sprintf(p + len, " *%02x", v);
3952 len += sprintf(p + len, " %02x", v);
3953 ecdump_regs[i + j] = v;
3955 len += sprintf(p + len, "\n");
3960 /* These are way too dangerous to advertise openly... */
3962 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
3963 " (<offset> is 00-ff, <value> is 00-ff)\n");
3964 len += sprintf(p + len, "commands:\t0x<offset> <value> "
3965 " (<offset> is 00-ff, <value> is 0-255)\n");
3970 static int ecdump_write(char *buf)
3975 while ((cmd = next_cmd(&buf))) {
3976 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
3978 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
3982 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
3983 if (!acpi_ec_write(i, v))
3992 static struct ibm_struct ecdump_driver_data = {
3994 .read = ecdump_read,
3995 .write = ecdump_write,
3996 .flags.experimental = 1,
3999 /*************************************************************************
4000 * Backlight/brightness subdriver
4003 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4005 static struct backlight_device *ibm_backlight_device;
4006 static int brightness_offset = 0x31;
4007 static int brightness_mode;
4008 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4010 static struct mutex brightness_mutex;
4013 * ThinkPads can read brightness from two places: EC 0x31, or
4014 * CMOS NVRAM byte 0x5E, bits 0-3.
4016 static int brightness_get(struct backlight_device *bd)
4018 u8 lec = 0, lcmos = 0, level = 0;
4020 if (brightness_mode & 1) {
4021 if (!acpi_ec_read(brightness_offset, &lec))
4023 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4026 if (brightness_mode & 2) {
4027 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4028 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4029 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4030 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4034 if (brightness_mode == 3 && lec != lcmos) {
4036 "CMOS NVRAM (%u) and EC (%u) do not agree "
4037 "on display brightness level\n",
4038 (unsigned int) lcmos,
4039 (unsigned int) lec);
4046 /* May return EINTR which can always be mapped to ERESTARTSYS */
4047 static int brightness_set(int value)
4049 int cmos_cmd, inc, i, res;
4052 if (value > ((tp_features.bright_16levels)? 15 : 7))
4055 res = mutex_lock_interruptible(&brightness_mutex);
4059 current_value = brightness_get(NULL);
4060 if (current_value < 0) {
4061 res = current_value;
4065 cmos_cmd = value > current_value ?
4066 TP_CMOS_BRIGHTNESS_UP :
4067 TP_CMOS_BRIGHTNESS_DOWN;
4068 inc = (value > current_value)? 1 : -1;
4071 for (i = current_value; i != value; i += inc) {
4072 if ((brightness_mode & 2) &&
4073 issue_thinkpad_cmos_command(cmos_cmd)) {
4077 if ((brightness_mode & 1) &&
4078 !acpi_ec_write(brightness_offset, i + inc)) {
4085 mutex_unlock(&brightness_mutex);
4089 /* sysfs backlight class ----------------------------------------------- */
4091 static int brightness_update_status(struct backlight_device *bd)
4093 /* it is the backlight class's job (caller) to handle
4094 * EINTR and other errors properly */
4095 return brightness_set(
4096 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4097 bd->props.power == FB_BLANK_UNBLANK) ?
4098 bd->props.brightness : 0);
4101 static struct backlight_ops ibm_backlight_data = {
4102 .get_brightness = brightness_get,
4103 .update_status = brightness_update_status,
4106 /* --------------------------------------------------------------------- */
4108 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4110 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4111 union acpi_object *obj;
4114 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4115 obj = (union acpi_object *)buffer.pointer;
4116 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4117 printk(IBM_ERR "Unknown BCLL data, "
4118 "please report this to %s\n", IBM_MAIL);
4121 rc = obj->package.count;
4127 kfree(buffer.pointer);
4131 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4132 void *context, void **rv)
4134 char name[ACPI_PATH_SEGMENT_LENGTH];
4135 struct acpi_buffer buffer = { sizeof(name), &name };
4137 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4138 !strncmp("BCLL", name, sizeof(name) - 1)) {
4139 if (tpacpi_query_bcll_levels(handle) == 16) {
4141 return AE_CTRL_TERMINATE;
4150 static int __init brightness_check_levels(void)
4153 void *found_node = NULL;
4156 IBM_ACPIHANDLE_INIT(vid);
4161 /* Search for a BCLL package with 16 levels */
4162 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4163 brightness_find_bcll, NULL, &found_node);
4165 return (ACPI_SUCCESS(status) && found_node != NULL);
4168 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4169 void *context, void **rv)
4171 char name[ACPI_PATH_SEGMENT_LENGTH];
4172 struct acpi_buffer buffer = { sizeof(name), &name };
4174 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4175 !strncmp("_BCL", name, sizeof(name) - 1)) {
4177 return AE_CTRL_TERMINATE;
4183 static int __init brightness_check_std_acpi_support(void)
4186 void *found_node = NULL;
4189 IBM_ACPIHANDLE_INIT(vid);
4194 /* Search for a _BCL method, but don't execute it */
4195 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4196 brightness_find_bcl, NULL, &found_node);
4198 return (ACPI_SUCCESS(status) && found_node != NULL);
4201 static int __init brightness_init(struct ibm_init_struct *iibm)
4205 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4207 mutex_init(&brightness_mutex);
4209 if (!brightness_enable) {
4210 dbg_printk(TPACPI_DBG_INIT,
4211 "brightness support disabled by module parameter\n");
4213 } else if (brightness_enable > 1) {
4214 if (brightness_check_std_acpi_support()) {
4216 "standard ACPI backlight interface available, not loading native one...\n");
4221 if (!brightness_mode) {
4222 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4223 brightness_mode = 2;
4225 brightness_mode = 3;
4227 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4231 if (brightness_mode > 3)
4234 tp_features.bright_16levels =
4235 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4236 brightness_check_levels();
4238 b = brightness_get(NULL);
4242 if (tp_features.bright_16levels)
4243 printk(IBM_INFO "detected a 16-level brightness capable ThinkPad\n");
4245 ibm_backlight_device = backlight_device_register(
4246 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4247 &ibm_backlight_data);
4248 if (IS_ERR(ibm_backlight_device)) {
4249 printk(IBM_ERR "Could not register backlight device\n");
4250 return PTR_ERR(ibm_backlight_device);
4252 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4254 ibm_backlight_device->props.max_brightness =
4255 (tp_features.bright_16levels)? 15 : 7;
4256 ibm_backlight_device->props.brightness = b;
4257 backlight_update_status(ibm_backlight_device);
4262 static void brightness_exit(void)
4264 if (ibm_backlight_device) {
4265 vdbg_printk(TPACPI_DBG_EXIT,
4266 "calling backlight_device_unregister()\n");
4267 backlight_device_unregister(ibm_backlight_device);
4268 ibm_backlight_device = NULL;
4272 static int brightness_read(char *p)
4277 if ((level = brightness_get(NULL)) < 0) {
4278 len += sprintf(p + len, "level:\t\tunreadable\n");
4280 len += sprintf(p + len, "level:\t\t%d\n", level);
4281 len += sprintf(p + len, "commands:\tup, down\n");
4282 len += sprintf(p + len, "commands:\tlevel <level>"
4283 " (<level> is 0-%d)\n",
4284 (tp_features.bright_16levels) ? 15 : 7);
4290 static int brightness_write(char *buf)
4295 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4297 level = brightness_get(NULL);
4301 while ((cmd = next_cmd(&buf))) {
4302 if (strlencmp(cmd, "up") == 0) {
4303 if (level < max_level)
4305 } else if (strlencmp(cmd, "down") == 0) {
4308 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4309 level >= 0 && level <= max_level) {
4316 * Now we know what the final level should be, so we try to set it.
4317 * Doing it this way makes the syscall restartable in case of EINTR
4319 rc = brightness_set(level);
4320 return (rc == -EINTR)? ERESTARTSYS : rc;
4323 static struct ibm_struct brightness_driver_data = {
4324 .name = "brightness",
4325 .read = brightness_read,
4326 .write = brightness_write,
4327 .exit = brightness_exit,
4330 /*************************************************************************
4334 static int volume_offset = 0x30;
4336 static int volume_read(char *p)
4341 if (!acpi_ec_read(volume_offset, &level)) {
4342 len += sprintf(p + len, "level:\t\tunreadable\n");
4344 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4345 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4346 len += sprintf(p + len, "commands:\tup, down, mute\n");
4347 len += sprintf(p + len, "commands:\tlevel <level>"
4348 " (<level> is 0-15)\n");
4354 static int volume_write(char *buf)
4356 int cmos_cmd, inc, i;
4358 int new_level, new_mute;
4361 while ((cmd = next_cmd(&buf))) {
4362 if (!acpi_ec_read(volume_offset, &level))
4364 new_mute = mute = level & 0x40;
4365 new_level = level = level & 0xf;
4367 if (strlencmp(cmd, "up") == 0) {
4371 new_level = level == 15 ? 15 : level + 1;
4372 } else if (strlencmp(cmd, "down") == 0) {
4376 new_level = level == 0 ? 0 : level - 1;
4377 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4378 new_level >= 0 && new_level <= 15) {
4380 } else if (strlencmp(cmd, "mute") == 0) {
4385 if (new_level != level) { /* mute doesn't change */
4386 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4387 inc = new_level > level ? 1 : -1;
4389 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4390 !acpi_ec_write(volume_offset, level)))
4393 for (i = level; i != new_level; i += inc)
4394 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4395 !acpi_ec_write(volume_offset, i + inc))
4398 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4399 !acpi_ec_write(volume_offset,
4404 if (new_mute != mute) { /* level doesn't change */
4405 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4407 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4408 !acpi_ec_write(volume_offset, level + new_mute))
4416 static struct ibm_struct volume_driver_data = {
4418 .read = volume_read,
4419 .write = volume_write,
4422 /*************************************************************************
4429 * TPACPI_FAN_RD_ACPI_GFAN:
4430 * ACPI GFAN method: returns fan level
4432 * see TPACPI_FAN_WR_ACPI_SFAN
4433 * EC 0x2f (HFSP) not available if GFAN exists
4435 * TPACPI_FAN_WR_ACPI_SFAN:
4436 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4438 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4441 * TPACPI_FAN_WR_TPEC:
4442 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4443 * Supported on almost all ThinkPads
4445 * Fan speed changes of any sort (including those caused by the
4446 * disengaged mode) are usually done slowly by the firmware as the
4447 * maximum ammount of fan duty cycle change per second seems to be
4450 * Reading is not available if GFAN exists.
4451 * Writing is not available if SFAN exists.
4454 * 7 automatic mode engaged;
4455 * (default operation mode of the ThinkPad)
4456 * fan level is ignored in this mode.
4457 * 6 full speed mode (takes precedence over bit 7);
4458 * not available on all thinkpads. May disable
4459 * the tachometer while the fan controller ramps up
4460 * the speed (which can take up to a few *minutes*).
4461 * Speeds up fan to 100% duty-cycle, which is far above
4462 * the standard RPM levels. It is not impossible that
4463 * it could cause hardware damage.
4464 * 5-3 unused in some models. Extra bits for fan level
4465 * in others, but still useless as all values above
4466 * 7 map to the same speed as level 7 in these models.
4467 * 2-0 fan level (0..7 usually)
4469 * 0x07 = max (set when temperatures critical)
4470 * Some ThinkPads may have other levels, see
4471 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4473 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4474 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4475 * does so, its initial value is meaningless (0x07).
4477 * For firmware bugs, refer to:
4478 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4482 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4483 * Main fan tachometer reading (in RPM)
4485 * This register is present on all ThinkPads with a new-style EC, and
4486 * it is known not to be present on the A21m/e, and T22, as there is
4487 * something else in offset 0x84 according to the ACPI DSDT. Other
4488 * ThinkPads from this same time period (and earlier) probably lack the
4489 * tachometer as well.
4491 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4492 * was never fixed by IBM to report the EC firmware version string
4493 * probably support the tachometer (like the early X models), so
4494 * detecting it is quite hard. We need more data to know for sure.
4496 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4499 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4502 * For firmware bugs, refer to:
4503 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4505 * TPACPI_FAN_WR_ACPI_FANS:
4506 * ThinkPad X31, X40, X41. Not available in the X60.
4508 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4509 * high speed. ACPI DSDT seems to map these three speeds to levels
4510 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4511 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4513 * The speeds are stored on handles
4514 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4516 * There are three default speed sets, acessible as handles:
4517 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4519 * ACPI DSDT switches which set is in use depending on various
4522 * TPACPI_FAN_WR_TPEC is also available and should be used to
4523 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4524 * but the ACPI tables just mention level 7.
4527 enum { /* Fan control constants */
4528 fan_status_offset = 0x2f, /* EC register 0x2f */
4529 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
4530 * 0x84 must be read before 0x85 */
4532 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
4533 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
4535 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
4538 enum fan_status_access_mode {
4539 TPACPI_FAN_NONE = 0, /* No fan status or control */
4540 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
4541 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
4544 enum fan_control_access_mode {
4545 TPACPI_FAN_WR_NONE = 0, /* No fan control */
4546 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
4547 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
4548 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
4551 enum fan_control_commands {
4552 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
4553 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
4554 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
4555 * and also watchdog cmd */
4558 static int fan_control_allowed;
4560 static enum fan_status_access_mode fan_status_access_mode;
4561 static enum fan_control_access_mode fan_control_access_mode;
4562 static enum fan_control_commands fan_control_commands;
4564 static u8 fan_control_initial_status;
4565 static u8 fan_control_desired_level;
4566 static int fan_watchdog_maxinterval;
4568 static struct mutex fan_mutex;
4570 static void fan_watchdog_fire(struct work_struct *ignored);
4571 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4573 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4574 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
4575 "\\FSPD", /* 600e/x, 770e, 770x */
4577 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
4578 "JFNS", /* 770x-JL */
4582 * Call with fan_mutex held
4584 static void fan_update_desired_level(u8 status)
4587 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4589 fan_control_desired_level = 7;
4591 fan_control_desired_level = status;
4595 static int fan_get_status(u8 *status)
4600 * Add TPACPI_FAN_RD_ACPI_FANS ? */
4602 switch (fan_status_access_mode) {
4603 case TPACPI_FAN_RD_ACPI_GFAN:
4604 /* 570, 600e/x, 770e, 770x */
4606 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
4614 case TPACPI_FAN_RD_TPEC:
4615 /* all except 570, 600e/x, 770e, 770x */
4616 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
4631 static int fan_get_status_safe(u8 *status)
4636 if (mutex_lock_interruptible(&fan_mutex))
4637 return -ERESTARTSYS;
4638 rc = fan_get_status(&s);
4640 fan_update_desired_level(s);
4641 mutex_unlock(&fan_mutex);
4649 static int fan_get_speed(unsigned int *speed)
4653 switch (fan_status_access_mode) {
4654 case TPACPI_FAN_RD_TPEC:
4655 /* all except 570, 600e/x, 770e, 770x */
4656 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
4657 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
4661 *speed = (hi << 8) | lo;
4672 static int fan_set_level(int level)
4674 if (!fan_control_allowed)
4677 switch (fan_control_access_mode) {
4678 case TPACPI_FAN_WR_ACPI_SFAN:
4679 if (level >= 0 && level <= 7) {
4680 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
4686 case TPACPI_FAN_WR_ACPI_FANS:
4687 case TPACPI_FAN_WR_TPEC:
4688 if ((level != TP_EC_FAN_AUTO) &&
4689 (level != TP_EC_FAN_FULLSPEED) &&
4690 ((level < 0) || (level > 7)))
4693 /* safety net should the EC not support AUTO
4694 * or FULLSPEED mode bits and just ignore them */
4695 if (level & TP_EC_FAN_FULLSPEED)
4696 level |= 7; /* safety min speed 7 */
4697 else if (level & TP_EC_FAN_FULLSPEED)
4698 level |= 4; /* safety min speed 4 */
4700 if (!acpi_ec_write(fan_status_offset, level))
4703 tp_features.fan_ctrl_status_undef = 0;
4712 static int fan_set_level_safe(int level)
4716 if (!fan_control_allowed)
4719 if (mutex_lock_interruptible(&fan_mutex))
4720 return -ERESTARTSYS;
4722 if (level == TPACPI_FAN_LAST_LEVEL)
4723 level = fan_control_desired_level;
4725 rc = fan_set_level(level);
4727 fan_update_desired_level(level);
4729 mutex_unlock(&fan_mutex);
4733 static int fan_set_enable(void)
4738 if (!fan_control_allowed)
4741 if (mutex_lock_interruptible(&fan_mutex))
4742 return -ERESTARTSYS;
4744 switch (fan_control_access_mode) {
4745 case TPACPI_FAN_WR_ACPI_FANS:
4746 case TPACPI_FAN_WR_TPEC:
4747 rc = fan_get_status(&s);
4751 /* Don't go out of emergency fan mode */
4754 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
4757 if (!acpi_ec_write(fan_status_offset, s))
4760 tp_features.fan_ctrl_status_undef = 0;
4765 case TPACPI_FAN_WR_ACPI_SFAN:
4766 rc = fan_get_status(&s);
4772 /* Set fan to at least level 4 */
4775 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
4785 mutex_unlock(&fan_mutex);
4789 static int fan_set_disable(void)
4793 if (!fan_control_allowed)
4796 if (mutex_lock_interruptible(&fan_mutex))
4797 return -ERESTARTSYS;
4800 switch (fan_control_access_mode) {
4801 case TPACPI_FAN_WR_ACPI_FANS:
4802 case TPACPI_FAN_WR_TPEC:
4803 if (!acpi_ec_write(fan_status_offset, 0x00))
4806 fan_control_desired_level = 0;
4807 tp_features.fan_ctrl_status_undef = 0;
4811 case TPACPI_FAN_WR_ACPI_SFAN:
4812 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
4815 fan_control_desired_level = 0;
4823 mutex_unlock(&fan_mutex);
4827 static int fan_set_speed(int speed)
4831 if (!fan_control_allowed)
4834 if (mutex_lock_interruptible(&fan_mutex))
4835 return -ERESTARTSYS;
4838 switch (fan_control_access_mode) {
4839 case TPACPI_FAN_WR_ACPI_FANS:
4840 if (speed >= 0 && speed <= 65535) {
4841 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
4842 speed, speed, speed))
4852 mutex_unlock(&fan_mutex);
4856 static void fan_watchdog_reset(void)
4858 static int fan_watchdog_active;
4860 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
4863 if (fan_watchdog_active)
4864 cancel_delayed_work(&fan_watchdog_task);
4866 if (fan_watchdog_maxinterval > 0 &&
4867 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
4868 fan_watchdog_active = 1;
4869 if (!schedule_delayed_work(&fan_watchdog_task,
4870 msecs_to_jiffies(fan_watchdog_maxinterval
4872 printk(IBM_ERR "failed to schedule the fan watchdog, "
4873 "watchdog will not trigger\n");
4876 fan_watchdog_active = 0;
4879 static void fan_watchdog_fire(struct work_struct *ignored)
4883 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
4886 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
4887 rc = fan_set_enable();
4889 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
4890 "will try again later...\n", -rc);
4891 /* reschedule for later */
4892 fan_watchdog_reset();
4897 * SYSFS fan layout: hwmon compatible (device)
4900 * 0: "disengaged" mode
4902 * 2: native EC "auto" mode (recommended, hardware default)
4904 * pwm*: set speed in manual mode, ignored otherwise.
4905 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4908 * fan*_input: tachometer reading, RPM
4911 * SYSFS fan layout: extensions
4913 * fan_watchdog (driver):
4914 * fan watchdog interval in seconds, 0 disables (default), max 120
4917 /* sysfs fan pwm1_enable ----------------------------------------------- */
4918 static ssize_t fan_pwm1_enable_show(struct device *dev,
4919 struct device_attribute *attr,
4925 res = fan_get_status_safe(&status);
4929 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4930 if (status != fan_control_initial_status) {
4931 tp_features.fan_ctrl_status_undef = 0;
4933 /* Return most likely status. In fact, it
4934 * might be the only possible status */
4935 status = TP_EC_FAN_AUTO;
4939 if (status & TP_EC_FAN_FULLSPEED) {
4941 } else if (status & TP_EC_FAN_AUTO) {
4946 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
4949 static ssize_t fan_pwm1_enable_store(struct device *dev,
4950 struct device_attribute *attr,
4951 const char *buf, size_t count)
4956 if (parse_strtoul(buf, 2, &t))
4961 level = TP_EC_FAN_FULLSPEED;
4964 level = TPACPI_FAN_LAST_LEVEL;
4967 level = TP_EC_FAN_AUTO;
4970 /* reserved for software-controlled auto mode */
4976 res = fan_set_level_safe(level);
4982 fan_watchdog_reset();
4987 static struct device_attribute dev_attr_fan_pwm1_enable =
4988 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
4989 fan_pwm1_enable_show, fan_pwm1_enable_store);
4991 /* sysfs fan pwm1 ------------------------------------------------------ */
4992 static ssize_t fan_pwm1_show(struct device *dev,
4993 struct device_attribute *attr,
4999 res = fan_get_status_safe(&status);
5003 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5004 if (status != fan_control_initial_status) {
5005 tp_features.fan_ctrl_status_undef = 0;
5007 status = TP_EC_FAN_AUTO;
5012 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5013 status = fan_control_desired_level;
5018 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5021 static ssize_t fan_pwm1_store(struct device *dev,
5022 struct device_attribute *attr,
5023 const char *buf, size_t count)
5027 u8 status, newlevel;
5029 if (parse_strtoul(buf, 255, &s))
5032 /* scale down from 0-255 to 0-7 */
5033 newlevel = (s >> 5) & 0x07;
5035 if (mutex_lock_interruptible(&fan_mutex))
5036 return -ERESTARTSYS;
5038 rc = fan_get_status(&status);
5039 if (!rc && (status &
5040 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5041 rc = fan_set_level(newlevel);
5045 fan_update_desired_level(newlevel);
5046 fan_watchdog_reset();
5050 mutex_unlock(&fan_mutex);
5051 return (rc)? rc : count;
5054 static struct device_attribute dev_attr_fan_pwm1 =
5055 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5056 fan_pwm1_show, fan_pwm1_store);
5058 /* sysfs fan fan1_input ------------------------------------------------ */
5059 static ssize_t fan_fan1_input_show(struct device *dev,
5060 struct device_attribute *attr,
5066 res = fan_get_speed(&speed);
5070 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5073 static struct device_attribute dev_attr_fan_fan1_input =
5074 __ATTR(fan1_input, S_IRUGO,
5075 fan_fan1_input_show, NULL);
5077 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5078 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5081 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5084 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5085 const char *buf, size_t count)
5089 if (parse_strtoul(buf, 120, &t))
5092 if (!fan_control_allowed)
5095 fan_watchdog_maxinterval = t;
5096 fan_watchdog_reset();
5101 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5102 fan_fan_watchdog_show, fan_fan_watchdog_store);
5104 /* --------------------------------------------------------------------- */
5105 static struct attribute *fan_attributes[] = {
5106 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5107 &dev_attr_fan_fan1_input.attr,
5111 static const struct attribute_group fan_attr_group = {
5112 .attrs = fan_attributes,
5115 static int __init fan_init(struct ibm_init_struct *iibm)
5119 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5121 mutex_init(&fan_mutex);
5122 fan_status_access_mode = TPACPI_FAN_NONE;
5123 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5124 fan_control_commands = 0;
5125 fan_watchdog_maxinterval = 0;
5126 tp_features.fan_ctrl_status_undef = 0;
5127 fan_control_desired_level = 7;
5129 IBM_ACPIHANDLE_INIT(fans);
5130 IBM_ACPIHANDLE_INIT(gfan);
5131 IBM_ACPIHANDLE_INIT(sfan);
5134 /* 570, 600e/x, 770e, 770x */
5135 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5137 /* all other ThinkPads: note that even old-style
5138 * ThinkPad ECs supports the fan control register */
5139 if (likely(acpi_ec_read(fan_status_offset,
5140 &fan_control_initial_status))) {
5141 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5143 /* In some ThinkPads, neither the EC nor the ACPI
5144 * DSDT initialize the fan status, and it ends up
5145 * being set to 0x07 when it *could* be either
5148 * Enable for TP-1Y (T43), TP-78 (R51e),
5149 * TP-76 (R52), TP-70 (T43, R52), which are known
5151 if (fan_control_initial_status == 0x07) {
5152 switch (thinkpad_id.ec_model) {
5153 case 0x5931: /* TP-1Y */
5154 case 0x3837: /* TP-78 */
5155 case 0x3637: /* TP-76 */
5156 case 0x3037: /* TP-70 */
5158 "fan_init: initial fan status is "
5159 "unknown, assuming it is in auto "
5161 tp_features.fan_ctrl_status_undef = 1;
5167 "ThinkPad ACPI EC access misbehaving, "
5168 "fan status and control unavailable\n");
5175 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5176 fan_control_commands |=
5177 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5180 /* gfan without sfan means no fan control */
5181 /* all other models implement TP EC 0x2f control */
5185 fan_control_access_mode =
5186 TPACPI_FAN_WR_ACPI_FANS;
5187 fan_control_commands |=
5188 TPACPI_FAN_CMD_SPEED |
5189 TPACPI_FAN_CMD_LEVEL |
5190 TPACPI_FAN_CMD_ENABLE;
5192 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5193 fan_control_commands |=
5194 TPACPI_FAN_CMD_LEVEL |
5195 TPACPI_FAN_CMD_ENABLE;
5200 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5201 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5202 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5203 fan_status_access_mode, fan_control_access_mode);
5205 /* fan control master switch */
5206 if (!fan_control_allowed) {
5207 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5208 fan_control_commands = 0;
5209 dbg_printk(TPACPI_DBG_INIT,
5210 "fan control features disabled by parameter\n");
5213 /* update fan_control_desired_level */
5214 if (fan_status_access_mode != TPACPI_FAN_NONE)
5215 fan_get_status_safe(NULL);
5217 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5218 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5219 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5222 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5223 &driver_attr_fan_watchdog);
5231 static void fan_exit(void)
5233 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
5235 /* FIXME: can we really do this unconditionally? */
5236 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5237 driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
5239 cancel_delayed_work(&fan_watchdog_task);
5240 flush_scheduled_work();
5243 static int fan_read(char *p)
5248 unsigned int speed = 0;
5250 switch (fan_status_access_mode) {
5251 case TPACPI_FAN_RD_ACPI_GFAN:
5252 /* 570, 600e/x, 770e, 770x */
5253 if ((rc = fan_get_status_safe(&status)) < 0)
5256 len += sprintf(p + len, "status:\t\t%s\n"
5258 (status != 0) ? "enabled" : "disabled", status);
5261 case TPACPI_FAN_RD_TPEC:
5262 /* all except 570, 600e/x, 770e, 770x */
5263 if ((rc = fan_get_status_safe(&status)) < 0)
5266 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5267 if (status != fan_control_initial_status)
5268 tp_features.fan_ctrl_status_undef = 0;
5270 /* Return most likely status. In fact, it
5271 * might be the only possible status */
5272 status = TP_EC_FAN_AUTO;
5275 len += sprintf(p + len, "status:\t\t%s\n",
5276 (status != 0) ? "enabled" : "disabled");
5278 if ((rc = fan_get_speed(&speed)) < 0)
5281 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5283 if (status & TP_EC_FAN_FULLSPEED)
5284 /* Disengaged mode takes precedence */
5285 len += sprintf(p + len, "level:\t\tdisengaged\n");
5286 else if (status & TP_EC_FAN_AUTO)
5287 len += sprintf(p + len, "level:\t\tauto\n");
5289 len += sprintf(p + len, "level:\t\t%d\n", status);
5292 case TPACPI_FAN_NONE:
5294 len += sprintf(p + len, "status:\t\tnot supported\n");
5297 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5298 len += sprintf(p + len, "commands:\tlevel <level>");
5300 switch (fan_control_access_mode) {
5301 case TPACPI_FAN_WR_ACPI_SFAN:
5302 len += sprintf(p + len, " (<level> is 0-7)\n");
5306 len += sprintf(p + len, " (<level> is 0-7, "
5307 "auto, disengaged, full-speed)\n");
5312 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5313 len += sprintf(p + len, "commands:\tenable, disable\n"
5314 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
5315 "1-120 (seconds))\n");
5317 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5318 len += sprintf(p + len, "commands:\tspeed <speed>"
5319 " (<speed> is 0-65535)\n");
5324 static int fan_write_cmd_level(const char *cmd, int *rc)
5328 if (strlencmp(cmd, "level auto") == 0)
5329 level = TP_EC_FAN_AUTO;
5330 else if ((strlencmp(cmd, "level disengaged") == 0) |
5331 (strlencmp(cmd, "level full-speed") == 0))
5332 level = TP_EC_FAN_FULLSPEED;
5333 else if (sscanf(cmd, "level %d", &level) != 1)
5336 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
5337 printk(IBM_ERR "level command accepted for unsupported "
5338 "access mode %d", fan_control_access_mode);
5343 static int fan_write_cmd_enable(const char *cmd, int *rc)
5345 if (strlencmp(cmd, "enable") != 0)
5348 if ((*rc = fan_set_enable()) == -ENXIO)
5349 printk(IBM_ERR "enable command accepted for unsupported "
5350 "access mode %d", fan_control_access_mode);
5355 static int fan_write_cmd_disable(const char *cmd, int *rc)
5357 if (strlencmp(cmd, "disable") != 0)
5360 if ((*rc = fan_set_disable()) == -ENXIO)
5361 printk(IBM_ERR "disable command accepted for unsupported "
5362 "access mode %d", fan_control_access_mode);
5367 static int fan_write_cmd_speed(const char *cmd, int *rc)
5372 * Support speed <low> <medium> <high> ? */
5374 if (sscanf(cmd, "speed %d", &speed) != 1)
5377 if ((*rc = fan_set_speed(speed)) == -ENXIO)
5378 printk(IBM_ERR "speed command accepted for unsupported "
5379 "access mode %d", fan_control_access_mode);
5384 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5388 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5391 if (interval < 0 || interval > 120)
5394 fan_watchdog_maxinterval = interval;
5399 static int fan_write(char *buf)
5404 while (!rc && (cmd = next_cmd(&buf))) {
5405 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5406 fan_write_cmd_level(cmd, &rc)) &&
5407 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5408 (fan_write_cmd_enable(cmd, &rc) ||
5409 fan_write_cmd_disable(cmd, &rc) ||
5410 fan_write_cmd_watchdog(cmd, &rc))) &&
5411 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5412 fan_write_cmd_speed(cmd, &rc))
5416 fan_watchdog_reset();
5422 static struct ibm_struct fan_driver_data = {
5429 /****************************************************************************
5430 ****************************************************************************
5434 ****************************************************************************
5435 ****************************************************************************/
5437 /* sysfs name ---------------------------------------------------------- */
5438 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5439 struct device_attribute *attr,
5442 return snprintf(buf, PAGE_SIZE, "%s\n", IBM_NAME);
5445 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5446 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5448 /* --------------------------------------------------------------------- */
5451 static struct proc_dir_entry *proc_dir;
5454 * Module and infrastructure proble, init and exit handling
5457 static int force_load;
5459 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5460 static const char * __init str_supported(int is_supported)
5462 static char text_unsupported[] __initdata = "not supported";
5464 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5466 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5468 static void ibm_exit(struct ibm_struct *ibm)
5470 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5472 list_del_init(&ibm->all_drivers);
5474 if (ibm->flags.acpi_notify_installed) {
5475 dbg_printk(TPACPI_DBG_EXIT,
5476 "%s: acpi_remove_notify_handler\n", ibm->name);
5478 acpi_remove_notify_handler(*ibm->acpi->handle,
5480 dispatch_acpi_notify);
5481 ibm->flags.acpi_notify_installed = 0;
5482 ibm->flags.acpi_notify_installed = 0;
5485 if (ibm->flags.proc_created) {
5486 dbg_printk(TPACPI_DBG_EXIT,
5487 "%s: remove_proc_entry\n", ibm->name);
5488 remove_proc_entry(ibm->name, proc_dir);
5489 ibm->flags.proc_created = 0;
5492 if (ibm->flags.acpi_driver_registered) {
5493 dbg_printk(TPACPI_DBG_EXIT,
5494 "%s: acpi_bus_unregister_driver\n", ibm->name);
5496 acpi_bus_unregister_driver(ibm->acpi->driver);
5497 kfree(ibm->acpi->driver);
5498 ibm->acpi->driver = NULL;
5499 ibm->flags.acpi_driver_registered = 0;
5502 if (ibm->flags.init_called && ibm->exit) {
5504 ibm->flags.init_called = 0;
5507 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5510 static int __init ibm_init(struct ibm_init_struct *iibm)
5513 struct ibm_struct *ibm = iibm->data;
5514 struct proc_dir_entry *entry;
5516 BUG_ON(ibm == NULL);
5518 INIT_LIST_HEAD(&ibm->all_drivers);
5520 if (ibm->flags.experimental && !experimental)
5523 dbg_printk(TPACPI_DBG_INIT,
5524 "probing for %s\n", ibm->name);
5527 ret = iibm->init(iibm);
5529 return 0; /* probe failed */
5533 ibm->flags.init_called = 1;
5537 if (ibm->acpi->hid) {
5538 ret = register_tpacpi_subdriver(ibm);
5543 if (ibm->acpi->notify) {
5544 ret = setup_acpi_notify(ibm);
5545 if (ret == -ENODEV) {
5546 printk(IBM_NOTICE "disabling subdriver %s\n",
5556 dbg_printk(TPACPI_DBG_INIT,
5557 "%s installed\n", ibm->name);
5560 entry = create_proc_entry(ibm->name,
5561 S_IFREG | S_IRUGO | S_IWUSR,
5564 printk(IBM_ERR "unable to create proc entry %s\n",
5569 entry->owner = THIS_MODULE;
5571 entry->read_proc = &dispatch_procfs_read;
5573 entry->write_proc = &dispatch_procfs_write;
5574 ibm->flags.proc_created = 1;
5577 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5582 dbg_printk(TPACPI_DBG_INIT,
5583 "%s: at error exit path with result %d\n",
5587 return (ret < 0)? ret : 0;
5592 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5594 const struct dmi_device *dev = NULL;
5595 char ec_fw_string[18];
5600 memset(tp, 0, sizeof(*tp));
5602 if (dmi_name_in_vendors("IBM"))
5603 tp->vendor = PCI_VENDOR_ID_IBM;
5604 else if (dmi_name_in_vendors("LENOVO"))
5605 tp->vendor = PCI_VENDOR_ID_LENOVO;
5609 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5611 if (!tp->bios_version_str)
5613 tp->bios_model = tp->bios_version_str[0]
5614 | (tp->bios_version_str[1] << 8);
5617 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5618 * X32 or newer, all Z series; Some models must have an
5619 * up-to-date BIOS or they will not be detected.
5621 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5623 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5624 if (sscanf(dev->name,
5625 "IBM ThinkPad Embedded Controller -[%17c",
5626 ec_fw_string) == 1) {
5627 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5628 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5630 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5631 tp->ec_model = ec_fw_string[0]
5632 | (ec_fw_string[1] << 8);
5637 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5639 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5640 kfree(tp->model_str);
5641 tp->model_str = NULL;
5645 static int __init probe_for_thinkpad(void)
5653 * Non-ancient models have better DMI tagging, but very old models
5656 is_thinkpad = (thinkpad_id.model_str != NULL);
5658 /* ec is required because many other handles are relative to it */
5659 IBM_ACPIHANDLE_INIT(ec);
5663 "Not yet supported ThinkPad detected!\n");
5668 * Risks a regression on very old machines, but reduces potential
5669 * false positives a damn great deal
5672 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5674 if (!is_thinkpad && !force_load)
5681 /* Module init, exit, parameters */
5683 static struct ibm_init_struct ibms_init[] __initdata = {
5685 .init = thinkpad_acpi_driver_init,
5686 .data = &thinkpad_acpi_driver_data,
5689 .init = hotkey_init,
5690 .data = &hotkey_driver_data,
5693 .init = bluetooth_init,
5694 .data = &bluetooth_driver_data,
5698 .data = &wan_driver_data,
5702 .data = &video_driver_data,
5706 .data = &light_driver_data,
5708 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5711 .data = &dock_driver_data[0],
5715 .data = &dock_driver_data[1],
5718 #ifdef CONFIG_THINKPAD_ACPI_BAY
5721 .data = &bay_driver_data,
5726 .data = &cmos_driver_data,
5730 .data = &led_driver_data,
5734 .data = &beep_driver_data,
5737 .init = thermal_init,
5738 .data = &thermal_driver_data,
5741 .data = &ecdump_driver_data,
5744 .init = brightness_init,
5745 .data = &brightness_driver_data,
5748 .data = &volume_driver_data,
5752 .data = &fan_driver_data,
5756 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5759 struct ibm_struct *ibm;
5761 if (!kp || !kp->name || !val)
5764 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5765 ibm = ibms_init[i].data;
5766 WARN_ON(ibm == NULL);
5768 if (!ibm || !ibm->name)
5771 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5772 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5774 strcpy(ibms_init[i].param, val);
5775 strcat(ibms_init[i].param, ",");
5783 module_param(experimental, int, 0);
5785 module_param_named(debug, dbg_level, uint, 0);
5787 module_param(force_load, bool, 0);
5789 module_param_named(fan_control, fan_control_allowed, bool, 0);
5791 module_param_named(brightness_mode, brightness_mode, int, 0);
5793 module_param(brightness_enable, uint, 0);
5795 module_param(hotkey_report_mode, uint, 0);
5797 #define IBM_PARAM(feature) \
5798 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
5801 IBM_PARAM(bluetooth);
5804 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5807 #ifdef CONFIG_THINKPAD_ACPI_BAY
5809 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5814 IBM_PARAM(brightness);
5818 static void thinkpad_acpi_module_exit(void)
5820 struct ibm_struct *ibm, *itmp;
5822 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
5824 list_for_each_entry_safe_reverse(ibm, itmp,
5825 &tpacpi_all_drivers,
5830 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
5832 if (tpacpi_inputdev) {
5833 if (tp_features.input_device_registered)
5834 input_unregister_device(tpacpi_inputdev);
5836 input_free_device(tpacpi_inputdev);
5840 hwmon_device_unregister(tpacpi_hwmon);
5842 if (tp_features.sensors_pdev_attrs_registered)
5843 device_remove_file(&tpacpi_sensors_pdev->dev,
5844 &dev_attr_thinkpad_acpi_pdev_name);
5845 if (tpacpi_sensors_pdev)
5846 platform_device_unregister(tpacpi_sensors_pdev);
5848 platform_device_unregister(tpacpi_pdev);
5850 if (tp_features.sensors_pdrv_attrs_registered)
5851 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5852 if (tp_features.platform_drv_attrs_registered)
5853 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
5855 if (tp_features.sensors_pdrv_registered)
5856 platform_driver_unregister(&tpacpi_hwmon_pdriver);
5858 if (tp_features.platform_drv_registered)
5859 platform_driver_unregister(&tpacpi_pdriver);
5862 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
5864 kfree(thinkpad_id.bios_version_str);
5865 kfree(thinkpad_id.ec_version_str);
5866 kfree(thinkpad_id.model_str);
5870 static int __init thinkpad_acpi_module_init(void)
5874 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5876 /* Parameter checking */
5877 if (hotkey_report_mode > 2)
5880 /* Driver-level probe */
5882 get_thinkpad_model_data(&thinkpad_id);
5883 ret = probe_for_thinkpad();
5885 thinkpad_acpi_module_exit();
5889 /* Driver initialization */
5891 IBM_ACPIHANDLE_INIT(ecrd);
5892 IBM_ACPIHANDLE_INIT(ecwr);
5894 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
5896 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
5897 thinkpad_acpi_module_exit();
5900 proc_dir->owner = THIS_MODULE;
5902 ret = platform_driver_register(&tpacpi_pdriver);
5904 printk(IBM_ERR "unable to register main platform driver\n");
5905 thinkpad_acpi_module_exit();
5908 tp_features.platform_drv_registered = 1;
5910 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
5912 printk(IBM_ERR "unable to register hwmon platform driver\n");
5913 thinkpad_acpi_module_exit();
5916 tp_features.sensors_pdrv_registered = 1;
5918 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
5920 tp_features.platform_drv_attrs_registered = 1;
5921 ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
5924 printk(IBM_ERR "unable to create sysfs driver attributes\n");
5925 thinkpad_acpi_module_exit();
5928 tp_features.sensors_pdrv_attrs_registered = 1;
5931 /* Device initialization */
5932 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
5934 if (IS_ERR(tpacpi_pdev)) {
5935 ret = PTR_ERR(tpacpi_pdev);
5937 printk(IBM_ERR "unable to register platform device\n");
5938 thinkpad_acpi_module_exit();
5941 tpacpi_sensors_pdev = platform_device_register_simple(
5942 IBM_HWMON_DRVR_NAME,
5944 if (IS_ERR(tpacpi_sensors_pdev)) {
5945 ret = PTR_ERR(tpacpi_sensors_pdev);
5946 tpacpi_sensors_pdev = NULL;
5947 printk(IBM_ERR "unable to register hwmon platform device\n");
5948 thinkpad_acpi_module_exit();
5951 ret = device_create_file(&tpacpi_sensors_pdev->dev,
5952 &dev_attr_thinkpad_acpi_pdev_name);
5955 "unable to create sysfs hwmon device attributes\n");
5956 thinkpad_acpi_module_exit();
5959 tp_features.sensors_pdev_attrs_registered = 1;
5960 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
5961 if (IS_ERR(tpacpi_hwmon)) {
5962 ret = PTR_ERR(tpacpi_hwmon);
5963 tpacpi_hwmon = NULL;
5964 printk(IBM_ERR "unable to register hwmon device\n");
5965 thinkpad_acpi_module_exit();
5968 mutex_init(&tpacpi_inputdev_send_mutex);
5969 tpacpi_inputdev = input_allocate_device();
5970 if (!tpacpi_inputdev) {
5971 printk(IBM_ERR "unable to allocate input device\n");
5972 thinkpad_acpi_module_exit();
5975 /* Prepare input device, but don't register */
5976 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
5977 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
5978 tpacpi_inputdev->id.bustype = BUS_HOST;
5979 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
5980 thinkpad_id.vendor :
5982 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
5983 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
5985 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5986 ret = ibm_init(&ibms_init[i]);
5987 if (ret >= 0 && *ibms_init[i].param)
5988 ret = ibms_init[i].data->write(ibms_init[i].param);
5990 thinkpad_acpi_module_exit();
5994 ret = input_register_device(tpacpi_inputdev);
5996 printk(IBM_ERR "unable to register input device\n");
5997 thinkpad_acpi_module_exit();
6000 tp_features.input_device_registered = 1;
6003 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6007 module_init(thinkpad_acpi_module_init);
6008 module_exit(thinkpad_acpi_module_exit);