2 * ec.c - ACPI Embedded Controller Driver (v2.0)
4 * Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
6 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
7 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 /* Uncomment next line to get verbose print outs*/
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/types.h>
36 #include <linux/delay.h>
37 #include <linux/proc_fs.h>
38 #include <linux/seq_file.h>
39 #include <linux/interrupt.h>
40 #include <linux/list.h>
42 #include <acpi/acpi_bus.h>
43 #include <acpi/acpi_drivers.h>
44 #include <acpi/actypes.h>
46 #define ACPI_EC_CLASS "embedded_controller"
47 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
48 #define ACPI_EC_FILE_INFO "info"
51 #define PREFIX "ACPI: EC: "
53 /* EC status register */
54 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
55 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
56 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
57 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
61 ACPI_EC_COMMAND_READ = 0x80,
62 ACPI_EC_COMMAND_WRITE = 0x81,
63 ACPI_EC_BURST_ENABLE = 0x82,
64 ACPI_EC_BURST_DISABLE = 0x83,
65 ACPI_EC_COMMAND_QUERY = 0x84,
70 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
71 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
74 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
75 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
76 #define ACPI_EC_UDELAY 100 /* Wait 100us before polling EC again */
79 EC_FLAGS_WAIT_GPE = 0, /* Don't check status until GPE arrives */
80 EC_FLAGS_QUERY_PENDING, /* Query is pending */
81 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent for status change */
82 EC_FLAGS_NO_GPE, /* Don't use GPE mode */
83 EC_FLAGS_RESCHEDULE_POLL /* Re-schedule poll */
86 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
87 /* External interfaces use first EC only, so remember */
88 typedef int (*acpi_ec_query_func) (void *data);
90 struct acpi_ec_query_handler {
91 struct list_head node;
92 acpi_ec_query_func func;
98 static struct acpi_ec {
101 unsigned long command_addr;
102 unsigned long data_addr;
103 unsigned long global_lock;
106 wait_queue_head_t wait;
107 struct list_head list;
108 struct delayed_work work;
110 u8 handlers_installed;
111 } *boot_ec, *first_ec;
114 * Some Asus system have exchanged ECDT data/command IO addresses.
116 static int print_ecdt_error(const struct dmi_system_id *id)
118 printk(KERN_NOTICE PREFIX "%s detected - "
119 "ECDT has exchanged control/data I/O address\n",
124 static struct dmi_system_id __cpuinitdata ec_dmi_table[] = {
126 print_ecdt_error, "Asus L4R", {
127 DMI_MATCH(DMI_BIOS_VERSION, "1008.006"),
128 DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),
129 DMI_MATCH(DMI_BOARD_NAME, "L4R") }, NULL},
131 print_ecdt_error, "Asus M6R", {
132 DMI_MATCH(DMI_BIOS_VERSION, "0207"),
133 DMI_MATCH(DMI_PRODUCT_NAME, "M6R"),
134 DMI_MATCH(DMI_BOARD_NAME, "M6R") }, NULL},
138 /* --------------------------------------------------------------------------
139 Transaction Management
140 -------------------------------------------------------------------------- */
142 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
144 u8 x = inb(ec->command_addr);
145 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
149 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
151 u8 x = inb(ec->data_addr);
152 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
153 return inb(ec->data_addr);
156 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
158 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
159 outb(command, ec->command_addr);
162 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
164 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
165 outb(data, ec->data_addr);
168 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
170 if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
172 if (event == ACPI_EC_EVENT_OBF_1) {
173 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
175 } else if (event == ACPI_EC_EVENT_IBF_0) {
176 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
183 static void ec_schedule_ec_poll(struct acpi_ec *ec)
185 if (test_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags))
186 schedule_delayed_work(&ec->work,
187 msecs_to_jiffies(ACPI_EC_DELAY));
190 static void ec_switch_to_poll_mode(struct acpi_ec *ec)
192 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
193 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
194 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
195 set_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
198 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
200 atomic_set(&ec->irq_count, 0);
201 if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
202 likely(!force_poll)) {
203 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
204 msecs_to_jiffies(ACPI_EC_DELAY)))
206 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
207 if (acpi_ec_check_status(ec, event)) {
208 /* missing GPEs, switch back to poll mode */
209 if (printk_ratelimit())
210 pr_info(PREFIX "missing confirmations, "
211 "switch off interrupt mode.\n");
212 ec_switch_to_poll_mode(ec);
213 ec_schedule_ec_poll(ec);
217 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
218 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
219 while (time_before(jiffies, delay)) {
220 if (acpi_ec_check_status(ec, event))
225 pr_err(PREFIX "acpi_ec_wait timeout, status = 0x%2.2x, event = %s\n",
226 acpi_ec_read_status(ec),
227 (event == ACPI_EC_EVENT_OBF_1) ? "\"b0=1\"" : "\"b1=0\"");
231 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
232 const u8 * wdata, unsigned wdata_len,
233 u8 * rdata, unsigned rdata_len,
237 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
238 pr_debug(PREFIX "transaction start\n");
239 acpi_ec_write_cmd(ec, command);
240 for (; wdata_len > 0; --wdata_len) {
241 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
244 "write_cmd timeout, command = %d\n", command);
247 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
248 acpi_ec_write_data(ec, *(wdata++));
252 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
255 "finish-write timeout, command = %d\n", command);
258 } else if (command == ACPI_EC_COMMAND_QUERY)
259 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
261 for (; rdata_len > 0; --rdata_len) {
262 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
264 pr_err(PREFIX "read timeout, command = %d\n", command);
267 /* Don't expect GPE after last read */
269 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
270 *(rdata++) = acpi_ec_read_data(ec);
273 pr_debug(PREFIX "transaction end\n");
277 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
278 const u8 * wdata, unsigned wdata_len,
279 u8 * rdata, unsigned rdata_len,
285 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
289 memset(rdata, 0, rdata_len);
291 mutex_lock(&ec->lock);
292 if (ec->global_lock) {
293 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
294 if (ACPI_FAILURE(status)) {
295 mutex_unlock(&ec->lock);
300 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
302 pr_err(PREFIX "input buffer is not empty, "
303 "aborting transaction\n");
307 status = acpi_ec_transaction_unlocked(ec, command,
315 acpi_release_global_lock(glk);
316 mutex_unlock(&ec->lock);
322 * Note: samsung nv5000 doesn't work with ec burst mode.
323 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
325 int acpi_ec_burst_enable(struct acpi_ec *ec)
328 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
331 int acpi_ec_burst_disable(struct acpi_ec *ec)
333 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
336 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
341 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
342 &address, 1, &d, 1, 0);
347 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
349 u8 wdata[2] = { address, data };
350 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
351 wdata, 2, NULL, 0, 0);
355 * Externally callable EC access functions. For now, assume 1 EC only
357 int ec_burst_enable(void)
361 return acpi_ec_burst_enable(first_ec);
364 EXPORT_SYMBOL(ec_burst_enable);
366 int ec_burst_disable(void)
370 return acpi_ec_burst_disable(first_ec);
373 EXPORT_SYMBOL(ec_burst_disable);
375 int ec_read(u8 addr, u8 * val)
383 err = acpi_ec_read(first_ec, addr, &temp_data);
392 EXPORT_SYMBOL(ec_read);
394 int ec_write(u8 addr, u8 val)
401 err = acpi_ec_write(first_ec, addr, val);
406 EXPORT_SYMBOL(ec_write);
408 int ec_transaction(u8 command,
409 const u8 * wdata, unsigned wdata_len,
410 u8 * rdata, unsigned rdata_len,
416 return acpi_ec_transaction(first_ec, command, wdata,
417 wdata_len, rdata, rdata_len,
421 EXPORT_SYMBOL(ec_transaction);
423 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
432 * Query the EC to find out which _Qxx method we need to evaluate.
433 * Note that successful completion of the query causes the ACPI_EC_SCI
434 * bit to be cleared (and thus clearing the interrupt source).
437 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
448 /* --------------------------------------------------------------------------
450 -------------------------------------------------------------------------- */
451 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
452 acpi_handle handle, acpi_ec_query_func func,
455 struct acpi_ec_query_handler *handler =
456 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
460 handler->query_bit = query_bit;
461 handler->handle = handle;
462 handler->func = func;
463 handler->data = data;
464 mutex_lock(&ec->lock);
465 list_add(&handler->node, &ec->list);
466 mutex_unlock(&ec->lock);
470 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
472 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
474 struct acpi_ec_query_handler *handler, *tmp;
475 mutex_lock(&ec->lock);
476 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
477 if (query_bit == handler->query_bit) {
478 list_del(&handler->node);
482 mutex_unlock(&ec->lock);
485 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
487 static void acpi_ec_gpe_query(void *ec_cxt)
489 struct acpi_ec *ec = ec_cxt;
491 struct acpi_ec_query_handler *handler, copy;
493 if (!ec || acpi_ec_query(ec, &value))
495 mutex_lock(&ec->lock);
496 list_for_each_entry(handler, &ec->list, node) {
497 if (value == handler->query_bit) {
498 /* have custom handler for this bit */
499 memcpy(©, handler, sizeof(copy));
500 mutex_unlock(&ec->lock);
502 copy.func(copy.data);
503 } else if (copy.handle) {
504 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
509 mutex_unlock(&ec->lock);
512 static u32 acpi_ec_gpe_handler(void *data)
514 acpi_status status = AE_OK;
515 struct acpi_ec *ec = data;
516 u8 state = acpi_ec_read_status(ec);
518 pr_debug(PREFIX "~~~> interrupt\n");
519 atomic_inc(&ec->irq_count);
520 if (atomic_read(&ec->irq_count) > 5) {
521 pr_err(PREFIX "GPE storm detected, disabling EC GPE\n");
522 ec_switch_to_poll_mode(ec);
525 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
526 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
529 if (state & ACPI_EC_FLAG_SCI) {
530 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
531 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
532 acpi_ec_gpe_query, ec);
533 } else if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
534 !test_bit(EC_FLAGS_NO_GPE, &ec->flags) &&
536 /* this is non-query, must be confirmation */
537 if (printk_ratelimit())
538 pr_info(PREFIX "non-query interrupt received,"
539 " switching to interrupt mode\n");
540 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
541 clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
544 ec_schedule_ec_poll(ec);
545 return ACPI_SUCCESS(status) ?
546 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
549 static void do_ec_poll(struct work_struct *work)
551 struct acpi_ec *ec = container_of(work, struct acpi_ec, work.work);
552 atomic_set(&ec->irq_count, 0);
553 (void)acpi_ec_gpe_handler(ec);
556 /* --------------------------------------------------------------------------
557 Address Space Management
558 -------------------------------------------------------------------------- */
561 acpi_ec_space_handler(u32 function, acpi_physical_address address,
562 u32 bits, acpi_integer *value,
563 void *handler_context, void *region_context)
565 struct acpi_ec *ec = handler_context;
569 if ((address > 0xFF) || !value || !handler_context)
570 return AE_BAD_PARAMETER;
572 if (function != ACPI_READ && function != ACPI_WRITE)
573 return AE_BAD_PARAMETER;
575 if (bits != 8 && acpi_strict)
576 return AE_BAD_PARAMETER;
578 acpi_ec_burst_enable(ec);
580 if (function == ACPI_READ) {
581 result = acpi_ec_read(ec, address, &temp);
584 temp = 0xff & (*value);
585 result = acpi_ec_write(ec, address, temp);
588 for (i = 8; unlikely(bits - i > 0); i += 8) {
590 if (function == ACPI_READ) {
591 result = acpi_ec_read(ec, address, &temp);
592 (*value) |= ((acpi_integer)temp) << i;
594 temp = 0xff & ((*value) >> i);
595 result = acpi_ec_write(ec, address, temp);
599 acpi_ec_burst_disable(ec);
603 return AE_BAD_PARAMETER;
616 /* --------------------------------------------------------------------------
618 -------------------------------------------------------------------------- */
620 static struct proc_dir_entry *acpi_ec_dir;
622 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
624 struct acpi_ec *ec = seq->private;
629 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
630 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
631 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
632 seq_printf(seq, "use global lock:\t%s\n",
633 ec->global_lock ? "yes" : "no");
638 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
640 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
643 static struct file_operations acpi_ec_info_ops = {
644 .open = acpi_ec_info_open_fs,
647 .release = single_release,
648 .owner = THIS_MODULE,
651 static int acpi_ec_add_fs(struct acpi_device *device)
653 struct proc_dir_entry *entry = NULL;
655 if (!acpi_device_dir(device)) {
656 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
658 if (!acpi_device_dir(device))
662 entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
663 acpi_device_dir(device),
664 &acpi_ec_info_ops, acpi_driver_data(device));
670 static int acpi_ec_remove_fs(struct acpi_device *device)
673 if (acpi_device_dir(device)) {
674 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
675 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
676 acpi_device_dir(device) = NULL;
682 /* --------------------------------------------------------------------------
684 -------------------------------------------------------------------------- */
686 ec_parse_io_ports(struct acpi_resource *resource, void *context);
688 static struct acpi_ec *make_acpi_ec(void)
690 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
693 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
694 mutex_init(&ec->lock);
695 init_waitqueue_head(&ec->wait);
696 INIT_LIST_HEAD(&ec->list);
697 INIT_DELAYED_WORK_DEFERRABLE(&ec->work, do_ec_poll);
698 atomic_set(&ec->irq_count, 0);
703 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
704 void *context, void **return_value)
706 struct acpi_namespace_node *node = handle;
707 struct acpi_ec *ec = context;
709 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
710 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
716 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
720 struct acpi_ec *ec = context;
721 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
722 ec_parse_io_ports, ec);
723 if (ACPI_FAILURE(status))
726 /* Get GPE bit assignment (EC events). */
727 /* TODO: Add support for _GPE returning a package */
728 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
729 if (ACPI_FAILURE(status))
731 /* Use the global lock for all EC transactions? */
732 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
734 return AE_CTRL_TERMINATE;
737 static void ec_poll_stop(struct acpi_ec *ec)
739 clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
740 cancel_delayed_work(&ec->work);
743 static void ec_remove_handlers(struct acpi_ec *ec)
746 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
747 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
748 pr_err(PREFIX "failed to remove space handler\n");
749 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
750 &acpi_ec_gpe_handler)))
751 pr_err(PREFIX "failed to remove gpe handler\n");
752 ec->handlers_installed = 0;
755 static int acpi_ec_add(struct acpi_device *device)
757 struct acpi_ec *ec = NULL;
761 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
762 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
764 /* Check for boot EC */
766 (boot_ec->handle == device->handle ||
767 boot_ec->handle == ACPI_ROOT_OBJECT)) {
774 if (ec_parse_device(device->handle, 0, ec, NULL) !=
781 ec->handle = device->handle;
783 /* Find and register all query methods */
784 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
785 acpi_ec_register_query_methods, ec, NULL);
789 acpi_driver_data(device) = ec;
790 acpi_ec_add_fs(device);
791 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
792 ec->gpe, ec->command_addr, ec->data_addr);
793 pr_info(PREFIX "driver started in %s mode\n",
794 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
798 static int acpi_ec_remove(struct acpi_device *device, int type)
801 struct acpi_ec_query_handler *handler, *tmp;
806 ec = acpi_driver_data(device);
807 mutex_lock(&ec->lock);
808 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
809 list_del(&handler->node);
812 mutex_unlock(&ec->lock);
813 acpi_ec_remove_fs(device);
814 acpi_driver_data(device) = NULL;
822 ec_parse_io_ports(struct acpi_resource *resource, void *context)
824 struct acpi_ec *ec = context;
826 if (resource->type != ACPI_RESOURCE_TYPE_IO)
830 * The first address region returned is the data port, and
831 * the second address region returned is the status/command
834 if (ec->data_addr == 0)
835 ec->data_addr = resource->data.io.minimum;
836 else if (ec->command_addr == 0)
837 ec->command_addr = resource->data.io.minimum;
839 return AE_CTRL_TERMINATE;
844 static int ec_install_handlers(struct acpi_ec *ec)
847 if (ec->handlers_installed)
849 status = acpi_install_gpe_handler(NULL, ec->gpe,
850 ACPI_GPE_EDGE_TRIGGERED,
851 &acpi_ec_gpe_handler, ec);
852 if (ACPI_FAILURE(status))
855 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
856 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
858 status = acpi_install_address_space_handler(ec->handle,
860 &acpi_ec_space_handler,
862 if (ACPI_FAILURE(status)) {
863 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
867 ec->handlers_installed = 1;
871 static int acpi_ec_start(struct acpi_device *device)
879 ec = acpi_driver_data(device);
884 ret = ec_install_handlers(ec);
886 /* EC is fully operational, allow queries */
887 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
888 ec_schedule_ec_poll(ec);
892 static int acpi_ec_stop(struct acpi_device *device, int type)
897 ec = acpi_driver_data(device);
900 ec_remove_handlers(ec);
905 int __init acpi_boot_ec_enable(void)
907 if (!boot_ec || boot_ec->handlers_installed)
909 if (!ec_install_handlers(boot_ec)) {
916 static const struct acpi_device_id ec_device_ids[] = {
921 int __init acpi_ec_ecdt_probe(void)
925 struct acpi_table_ecdt *ecdt_ptr;
927 boot_ec = make_acpi_ec();
931 * Generate a boot ec context
933 status = acpi_get_table(ACPI_SIG_ECDT, 1,
934 (struct acpi_table_header **)&ecdt_ptr);
935 if (ACPI_SUCCESS(status)) {
936 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
937 boot_ec->command_addr = ecdt_ptr->control.address;
938 boot_ec->data_addr = ecdt_ptr->data.address;
939 if (dmi_check_system(ec_dmi_table)) {
941 * If the board falls into ec_dmi_table, it means
942 * that ECDT table gives the incorrect command/status
943 * & data I/O address. Just fix it.
945 boot_ec->data_addr = ecdt_ptr->control.address;
946 boot_ec->command_addr = ecdt_ptr->data.address;
948 boot_ec->gpe = ecdt_ptr->gpe;
949 boot_ec->handle = ACPI_ROOT_OBJECT;
950 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
952 /* This workaround is needed only on some broken machines,
953 * which require early EC, but fail to provide ECDT */
955 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
956 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
958 /* Check that acpi_get_devices actually find something */
959 if (ACPI_FAILURE(status) || !boot_ec->handle)
961 /* We really need to limit this workaround, the only ASUS,
962 * which needs it, has fake EC._INI method, so use it as flag.
963 * Keep boot_ec struct as it will be needed soon.
965 if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
969 ret = ec_install_handlers(boot_ec);
980 static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
982 struct acpi_ec *ec = acpi_driver_data(device);
984 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
985 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
986 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
990 static int acpi_ec_resume(struct acpi_device *device)
992 struct acpi_ec *ec = acpi_driver_data(device);
993 /* Enable use of GPE back */
994 clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
995 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
999 static struct acpi_driver acpi_ec_driver = {
1001 .class = ACPI_EC_CLASS,
1002 .ids = ec_device_ids,
1005 .remove = acpi_ec_remove,
1006 .start = acpi_ec_start,
1007 .stop = acpi_ec_stop,
1008 .suspend = acpi_ec_suspend,
1009 .resume = acpi_ec_resume,
1013 static int __init acpi_ec_init(void)
1020 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1024 /* Now register the driver for the EC */
1025 result = acpi_bus_register_driver(&acpi_ec_driver);
1027 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1034 subsys_initcall(acpi_ec_init);
1036 /* EC driver currently not unloadable */
1038 static void __exit acpi_ec_exit(void)
1041 acpi_bus_unregister_driver(&acpi_ec_driver);
1043 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);