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 static int acpi_ec_remove(struct acpi_device *device, int type);
87 static int acpi_ec_start(struct acpi_device *device);
88 static int acpi_ec_stop(struct acpi_device *device, int type);
89 static int acpi_ec_add(struct acpi_device *device);
91 static const struct acpi_device_id ec_device_ids[] = {
96 static struct acpi_driver acpi_ec_driver = {
98 .class = ACPI_EC_CLASS,
102 .remove = acpi_ec_remove,
103 .start = acpi_ec_start,
104 .stop = acpi_ec_stop,
108 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
109 /* External interfaces use first EC only, so remember */
110 typedef int (*acpi_ec_query_func) (void *data);
112 struct acpi_ec_query_handler {
113 struct list_head node;
114 acpi_ec_query_func func;
120 static struct acpi_ec {
123 unsigned long command_addr;
124 unsigned long data_addr;
125 unsigned long global_lock;
128 wait_queue_head_t wait;
129 struct list_head list;
130 struct delayed_work work;
131 u8 handlers_installed;
132 } *boot_ec, *first_ec;
134 /* --------------------------------------------------------------------------
135 Transaction Management
136 -------------------------------------------------------------------------- */
138 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
140 u8 x = inb(ec->command_addr);
141 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
145 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
147 u8 x = inb(ec->data_addr);
148 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
149 return inb(ec->data_addr);
152 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
154 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
155 outb(command, ec->command_addr);
158 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
160 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
161 outb(data, ec->data_addr);
164 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
166 if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
168 if (event == ACPI_EC_EVENT_OBF_1) {
169 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
171 } else if (event == ACPI_EC_EVENT_IBF_0) {
172 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
179 static void ec_schedule_ec_poll(struct acpi_ec *ec)
181 if (test_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags))
182 schedule_delayed_work(&ec->work,
183 msecs_to_jiffies(ACPI_EC_DELAY));
186 static void ec_switch_to_poll_mode(struct acpi_ec *ec)
188 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
189 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
190 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
191 set_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
194 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
196 if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
197 likely(!force_poll)) {
198 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
199 msecs_to_jiffies(ACPI_EC_DELAY)))
201 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
202 if (acpi_ec_check_status(ec, event)) {
203 /* missing GPEs, switch back to poll mode */
204 if (printk_ratelimit())
205 pr_info(PREFIX "missing confirmations, "
206 "switch off interrupt mode.\n");
207 ec_switch_to_poll_mode(ec);
208 ec_schedule_ec_poll(ec);
212 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
213 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
214 while (time_before(jiffies, delay)) {
215 if (acpi_ec_check_status(ec, event))
217 udelay(ACPI_EC_UDELAY);
220 pr_err(PREFIX "acpi_ec_wait timeout, status = 0x%2.2x, event = %s\n",
221 acpi_ec_read_status(ec),
222 (event == ACPI_EC_EVENT_OBF_1) ? "\"b0=1\"" : "\"b1=0\"");
226 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
227 const u8 * wdata, unsigned wdata_len,
228 u8 * rdata, unsigned rdata_len,
232 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
233 pr_debug(PREFIX "transaction start\n");
234 acpi_ec_write_cmd(ec, command);
235 for (; wdata_len > 0; --wdata_len) {
236 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
239 "write_cmd timeout, command = %d\n", command);
242 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
243 acpi_ec_write_data(ec, *(wdata++));
247 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
250 "finish-write timeout, command = %d\n", command);
253 } else if (command == ACPI_EC_COMMAND_QUERY)
254 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
256 for (; rdata_len > 0; --rdata_len) {
257 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
259 pr_err(PREFIX "read timeout, command = %d\n", command);
262 /* Don't expect GPE after last read */
264 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
265 *(rdata++) = acpi_ec_read_data(ec);
268 pr_debug(PREFIX "transaction end\n");
272 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
273 const u8 * wdata, unsigned wdata_len,
274 u8 * rdata, unsigned rdata_len,
280 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
284 memset(rdata, 0, rdata_len);
286 mutex_lock(&ec->lock);
287 if (ec->global_lock) {
288 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
289 if (ACPI_FAILURE(status)) {
290 mutex_unlock(&ec->lock);
295 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
297 pr_err(PREFIX "input buffer is not empty, "
298 "aborting transaction\n");
302 status = acpi_ec_transaction_unlocked(ec, command,
310 acpi_release_global_lock(glk);
311 mutex_unlock(&ec->lock);
317 * Note: samsung nv5000 doesn't work with ec burst mode.
318 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
320 int acpi_ec_burst_enable(struct acpi_ec *ec)
323 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
326 int acpi_ec_burst_disable(struct acpi_ec *ec)
328 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
331 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
336 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
337 &address, 1, &d, 1, 0);
342 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
344 u8 wdata[2] = { address, data };
345 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
346 wdata, 2, NULL, 0, 0);
350 * Externally callable EC access functions. For now, assume 1 EC only
352 int ec_burst_enable(void)
356 return acpi_ec_burst_enable(first_ec);
359 EXPORT_SYMBOL(ec_burst_enable);
361 int ec_burst_disable(void)
365 return acpi_ec_burst_disable(first_ec);
368 EXPORT_SYMBOL(ec_burst_disable);
370 int ec_read(u8 addr, u8 * val)
378 err = acpi_ec_read(first_ec, addr, &temp_data);
387 EXPORT_SYMBOL(ec_read);
389 int ec_write(u8 addr, u8 val)
396 err = acpi_ec_write(first_ec, addr, val);
401 EXPORT_SYMBOL(ec_write);
403 int ec_transaction(u8 command,
404 const u8 * wdata, unsigned wdata_len,
405 u8 * rdata, unsigned rdata_len,
411 return acpi_ec_transaction(first_ec, command, wdata,
412 wdata_len, rdata, rdata_len,
416 EXPORT_SYMBOL(ec_transaction);
418 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
427 * Query the EC to find out which _Qxx method we need to evaluate.
428 * Note that successful completion of the query causes the ACPI_EC_SCI
429 * bit to be cleared (and thus clearing the interrupt source).
432 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
443 /* --------------------------------------------------------------------------
445 -------------------------------------------------------------------------- */
446 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
447 acpi_handle handle, acpi_ec_query_func func,
450 struct acpi_ec_query_handler *handler =
451 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
455 handler->query_bit = query_bit;
456 handler->handle = handle;
457 handler->func = func;
458 handler->data = data;
459 mutex_lock(&ec->lock);
460 list_add(&handler->node, &ec->list);
461 mutex_unlock(&ec->lock);
465 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
467 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
469 struct acpi_ec_query_handler *handler, *tmp;
470 mutex_lock(&ec->lock);
471 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
472 if (query_bit == handler->query_bit) {
473 list_del(&handler->node);
477 mutex_unlock(&ec->lock);
480 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
482 static void acpi_ec_gpe_query(void *ec_cxt)
484 struct acpi_ec *ec = ec_cxt;
486 struct acpi_ec_query_handler *handler, copy;
488 if (!ec || acpi_ec_query(ec, &value))
490 mutex_lock(&ec->lock);
491 list_for_each_entry(handler, &ec->list, node) {
492 if (value == handler->query_bit) {
493 /* have custom handler for this bit */
494 memcpy(©, handler, sizeof(copy));
495 mutex_unlock(&ec->lock);
497 copy.func(copy.data);
498 } else if (copy.handle) {
499 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
504 mutex_unlock(&ec->lock);
507 static u32 acpi_ec_gpe_handler(void *data)
509 acpi_status status = AE_OK;
510 struct acpi_ec *ec = data;
511 u8 state = acpi_ec_read_status(ec);
513 pr_debug(PREFIX "~~~> interrupt\n");
514 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
515 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
518 if (state & ACPI_EC_FLAG_SCI) {
519 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
520 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
521 acpi_ec_gpe_query, ec);
522 } else if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
523 !test_bit(EC_FLAGS_NO_GPE, &ec->flags) &&
525 /* this is non-query, must be confirmation */
526 if (printk_ratelimit())
527 pr_info(PREFIX "non-query interrupt received,"
528 " switching to interrupt mode\n");
529 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
530 clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
532 ec_schedule_ec_poll(ec);
533 return ACPI_SUCCESS(status) ?
534 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
537 static void do_ec_poll(struct work_struct *work)
539 struct acpi_ec *ec = container_of(work, struct acpi_ec, work.work);
540 (void)acpi_ec_gpe_handler(ec);
543 /* --------------------------------------------------------------------------
544 Address Space Management
545 -------------------------------------------------------------------------- */
548 acpi_ec_space_setup(acpi_handle region_handle,
549 u32 function, void *handler_context, void **return_context)
552 * The EC object is in the handler context and is needed
553 * when calling the acpi_ec_space_handler.
555 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
556 handler_context : NULL;
562 acpi_ec_space_handler(u32 function, acpi_physical_address address,
563 u32 bits, acpi_integer *value,
564 void *handler_context, void *region_context)
566 struct acpi_ec *ec = handler_context;
570 if ((address > 0xFF) || !value || !handler_context)
571 return AE_BAD_PARAMETER;
573 if (function != ACPI_READ && function != ACPI_WRITE)
574 return AE_BAD_PARAMETER;
576 if (bits != 8 && acpi_strict)
577 return AE_BAD_PARAMETER;
579 acpi_ec_burst_enable(ec);
581 if (function == ACPI_READ) {
582 result = acpi_ec_read(ec, address, &temp);
585 temp = 0xff & (*value);
586 result = acpi_ec_write(ec, address, temp);
589 for (i = 8; unlikely(bits - i > 0); i += 8) {
591 if (function == ACPI_READ) {
592 result = acpi_ec_read(ec, address, &temp);
593 (*value) |= ((acpi_integer)temp) << i;
595 temp = 0xff & ((*value) >> i);
596 result = acpi_ec_write(ec, address, temp);
600 acpi_ec_burst_disable(ec);
604 return AE_BAD_PARAMETER;
617 /* --------------------------------------------------------------------------
619 -------------------------------------------------------------------------- */
621 static struct proc_dir_entry *acpi_ec_dir;
623 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
625 struct acpi_ec *ec = seq->private;
630 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
631 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
632 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
633 seq_printf(seq, "use global lock:\t%s\n",
634 ec->global_lock ? "yes" : "no");
639 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
641 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
644 static struct file_operations acpi_ec_info_ops = {
645 .open = acpi_ec_info_open_fs,
648 .release = single_release,
649 .owner = THIS_MODULE,
652 static int acpi_ec_add_fs(struct acpi_device *device)
654 struct proc_dir_entry *entry = NULL;
656 if (!acpi_device_dir(device)) {
657 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
659 if (!acpi_device_dir(device))
663 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
664 acpi_device_dir(device));
668 entry->proc_fops = &acpi_ec_info_ops;
669 entry->data = acpi_driver_data(device);
670 entry->owner = THIS_MODULE;
676 static int acpi_ec_remove_fs(struct acpi_device *device)
679 if (acpi_device_dir(device)) {
680 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
681 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
682 acpi_device_dir(device) = NULL;
688 /* --------------------------------------------------------------------------
690 -------------------------------------------------------------------------- */
692 ec_parse_io_ports(struct acpi_resource *resource, void *context);
694 static struct acpi_ec *make_acpi_ec(void)
696 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
699 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
700 mutex_init(&ec->lock);
701 init_waitqueue_head(&ec->wait);
702 INIT_LIST_HEAD(&ec->list);
703 INIT_DELAYED_WORK_DEFERRABLE(&ec->work, do_ec_poll);
708 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
709 void *context, void **return_value)
711 struct acpi_namespace_node *node = handle;
712 struct acpi_ec *ec = context;
714 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
715 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
721 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
725 struct acpi_ec *ec = context;
726 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
727 ec_parse_io_ports, ec);
728 if (ACPI_FAILURE(status))
731 /* Get GPE bit assignment (EC events). */
732 /* TODO: Add support for _GPE returning a package */
733 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
734 if (ACPI_FAILURE(status))
736 /* Find and register all query methods */
737 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
738 acpi_ec_register_query_methods, ec, NULL);
739 /* Use the global lock for all EC transactions? */
740 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
742 return AE_CTRL_TERMINATE;
745 static void ec_poll_stop(struct acpi_ec *ec)
747 clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
748 cancel_delayed_work(&ec->work);
751 static void ec_remove_handlers(struct acpi_ec *ec)
754 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
755 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
756 pr_err(PREFIX "failed to remove space handler\n");
757 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
758 &acpi_ec_gpe_handler)))
759 pr_err(PREFIX "failed to remove gpe handler\n");
760 ec->handlers_installed = 0;
763 static int acpi_ec_add(struct acpi_device *device)
765 struct acpi_ec *ec = NULL;
769 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
770 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
772 /* Check for boot EC */
774 if (boot_ec->handle == device->handle) {
775 /* Pre-loaded EC from DSDT, just move pointer */
779 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
780 /* ECDT-based EC, time to shut it down */
781 ec_remove_handlers(boot_ec);
783 first_ec = boot_ec = NULL;
791 if (ec_parse_device(device->handle, 0, ec, NULL) !=
796 ec->handle = device->handle;
800 acpi_driver_data(device) = ec;
801 acpi_ec_add_fs(device);
802 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
803 ec->gpe, ec->command_addr, ec->data_addr);
804 pr_info(PREFIX "driver started in %s mode\n",
805 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
809 static int acpi_ec_remove(struct acpi_device *device, int type)
812 struct acpi_ec_query_handler *handler, *tmp;
817 ec = acpi_driver_data(device);
818 mutex_lock(&ec->lock);
819 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
820 list_del(&handler->node);
823 mutex_unlock(&ec->lock);
824 acpi_ec_remove_fs(device);
825 acpi_driver_data(device) = NULL;
833 ec_parse_io_ports(struct acpi_resource *resource, void *context)
835 struct acpi_ec *ec = context;
837 if (resource->type != ACPI_RESOURCE_TYPE_IO)
841 * The first address region returned is the data port, and
842 * the second address region returned is the status/command
845 if (ec->data_addr == 0)
846 ec->data_addr = resource->data.io.minimum;
847 else if (ec->command_addr == 0)
848 ec->command_addr = resource->data.io.minimum;
850 return AE_CTRL_TERMINATE;
855 static int ec_install_handlers(struct acpi_ec *ec)
858 if (ec->handlers_installed)
860 status = acpi_install_gpe_handler(NULL, ec->gpe,
861 ACPI_GPE_EDGE_TRIGGERED,
862 &acpi_ec_gpe_handler, ec);
863 if (ACPI_FAILURE(status))
866 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
867 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
869 status = acpi_install_address_space_handler(ec->handle,
871 &acpi_ec_space_handler,
872 &acpi_ec_space_setup, ec);
873 if (ACPI_FAILURE(status)) {
874 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
878 ec->handlers_installed = 1;
882 static int acpi_ec_start(struct acpi_device *device)
890 ec = acpi_driver_data(device);
895 ret = ec_install_handlers(ec);
897 /* EC is fully operational, allow queries */
898 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
899 ec_schedule_ec_poll(ec);
903 static int acpi_ec_stop(struct acpi_device *device, int type)
908 ec = acpi_driver_data(device);
911 ec_remove_handlers(ec);
916 int __init acpi_boot_ec_enable(void)
918 if (!boot_ec || boot_ec->handlers_installed)
920 if (!ec_install_handlers(boot_ec)) {
927 int __init acpi_ec_ecdt_probe(void)
931 struct acpi_table_ecdt *ecdt_ptr;
933 boot_ec = make_acpi_ec();
937 * Generate a boot ec context
939 status = acpi_get_table(ACPI_SIG_ECDT, 1,
940 (struct acpi_table_header **)&ecdt_ptr);
941 if (ACPI_SUCCESS(status)) {
942 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
943 boot_ec->command_addr = ecdt_ptr->control.address;
944 boot_ec->data_addr = ecdt_ptr->data.address;
945 boot_ec->gpe = ecdt_ptr->gpe;
946 boot_ec->handle = ACPI_ROOT_OBJECT;
948 /* This workaround is needed only on some broken machines,
949 * which require early EC, but fail to provide ECDT */
951 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
952 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
954 /* Check that acpi_get_devices actually find something */
955 if (ACPI_FAILURE(status) || !boot_ec->handle)
957 /* We really need to limit this workaround, the only ASUS,
958 * which needs it, has fake EC._INI method, so use it as flag.
959 * Keep boot_ec struct as it will be needed soon.
961 if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
965 ret = ec_install_handlers(boot_ec);
976 static int __init acpi_ec_init(void)
983 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
987 /* Now register the driver for the EC */
988 result = acpi_bus_register_driver(&acpi_ec_driver);
990 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
997 subsys_initcall(acpi_ec_init);
999 /* EC driver currently not unloadable */
1001 static void __exit acpi_ec_exit(void)
1004 acpi_bus_unregister_driver(&acpi_ec_driver);
1006 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);