2 * ec.c - ACPI Embedded Controller Driver (v2.1)
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 printout */
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/interrupt.h>
38 #include <linux/list.h>
39 #include <linux/spinlock.h>
40 #include <linux/slab.h>
42 #include <acpi/acpi_bus.h>
43 #include <acpi/acpi_drivers.h>
44 #include <linux/dmi.h>
48 #define ACPI_EC_CLASS "embedded_controller"
49 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
50 #define ACPI_EC_FILE_INFO "info"
53 #define PREFIX "ACPI: EC: "
55 /* EC status register */
56 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
57 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
58 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
59 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
63 ACPI_EC_COMMAND_READ = 0x80,
64 ACPI_EC_COMMAND_WRITE = 0x81,
65 ACPI_EC_BURST_ENABLE = 0x82,
66 ACPI_EC_BURST_DISABLE = 0x83,
67 ACPI_EC_COMMAND_QUERY = 0x84,
70 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
71 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
72 #define ACPI_EC_CDELAY 10 /* Wait 10us before polling EC */
73 #define ACPI_EC_MSI_UDELAY 550 /* Wait 550us for MSI EC */
75 #define ACPI_EC_STORM_THRESHOLD 8 /* number of false interrupts
76 per one transaction */
79 EC_FLAGS_QUERY_PENDING, /* Query is pending */
80 EC_FLAGS_GPE_STORM, /* GPE storm detected */
81 EC_FLAGS_HANDLERS_INSTALLED, /* Handlers for GPE and
82 * OpReg are installed */
83 EC_FLAGS_BLOCKED, /* Transactions are blocked */
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;
101 unsigned short irq_count;
110 struct acpi_ec *boot_ec, *first_ec;
111 EXPORT_SYMBOL(first_ec);
113 static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
114 static int EC_FLAGS_VALIDATE_ECDT; /* ASUStec ECDTs need to be validated */
115 static int EC_FLAGS_SKIP_DSDT_SCAN; /* Not all BIOS survive early DSDT scan */
117 /* --------------------------------------------------------------------------
118 Transaction Management
119 -------------------------------------------------------------------------- */
121 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
123 u8 x = inb(ec->command_addr);
124 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
128 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
130 u8 x = inb(ec->data_addr);
131 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
135 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
137 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
138 outb(command, ec->command_addr);
141 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
143 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
144 outb(data, ec->data_addr);
147 static int ec_transaction_done(struct acpi_ec *ec)
151 spin_lock_irqsave(&ec->curr_lock, flags);
152 if (!ec->curr || ec->curr->done)
154 spin_unlock_irqrestore(&ec->curr_lock, flags);
158 static void start_transaction(struct acpi_ec *ec)
160 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
161 ec->curr->done = false;
162 acpi_ec_write_cmd(ec, ec->curr->command);
165 static void advance_transaction(struct acpi_ec *ec, u8 status)
168 spin_lock_irqsave(&ec->curr_lock, flags);
171 if (ec->curr->wlen > ec->curr->wi) {
172 if ((status & ACPI_EC_FLAG_IBF) == 0)
173 acpi_ec_write_data(ec,
174 ec->curr->wdata[ec->curr->wi++]);
177 } else if (ec->curr->rlen > ec->curr->ri) {
178 if ((status & ACPI_EC_FLAG_OBF) == 1) {
179 ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
180 if (ec->curr->rlen == ec->curr->ri)
181 ec->curr->done = true;
184 } else if (ec->curr->wlen == ec->curr->wi &&
185 (status & ACPI_EC_FLAG_IBF) == 0)
186 ec->curr->done = true;
189 /* false interrupt, state didn't change */
191 ++ec->curr->irq_count;
193 spin_unlock_irqrestore(&ec->curr_lock, flags);
196 static int acpi_ec_sync_query(struct acpi_ec *ec);
198 static int ec_check_sci_sync(struct acpi_ec *ec, u8 state)
200 if (state & ACPI_EC_FLAG_SCI) {
201 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
202 return acpi_ec_sync_query(ec);
207 static int ec_poll(struct acpi_ec *ec)
210 int repeat = 2; /* number of command restarts */
212 unsigned long delay = jiffies +
213 msecs_to_jiffies(ACPI_EC_DELAY);
215 /* don't sleep with disabled interrupts */
216 if (EC_FLAGS_MSI || irqs_disabled()) {
217 udelay(ACPI_EC_MSI_UDELAY);
218 if (ec_transaction_done(ec))
221 if (wait_event_timeout(ec->wait,
222 ec_transaction_done(ec),
223 msecs_to_jiffies(1)))
226 advance_transaction(ec, acpi_ec_read_status(ec));
227 } while (time_before(jiffies, delay));
228 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
230 pr_debug(PREFIX "controller reset, restart transaction\n");
231 spin_lock_irqsave(&ec->curr_lock, flags);
232 start_transaction(ec);
233 spin_unlock_irqrestore(&ec->curr_lock, flags);
238 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
239 struct transaction *t)
244 udelay(ACPI_EC_MSI_UDELAY);
245 /* start transaction */
246 spin_lock_irqsave(&ec->curr_lock, tmp);
247 /* following two actions should be kept atomic */
249 start_transaction(ec);
250 if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
251 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
252 spin_unlock_irqrestore(&ec->curr_lock, tmp);
254 spin_lock_irqsave(&ec->curr_lock, tmp);
256 spin_unlock_irqrestore(&ec->curr_lock, tmp);
260 static int ec_check_ibf0(struct acpi_ec *ec)
262 u8 status = acpi_ec_read_status(ec);
263 return (status & ACPI_EC_FLAG_IBF) == 0;
266 static int ec_wait_ibf0(struct acpi_ec *ec)
268 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
269 /* interrupt wait manually if GPE mode is not active */
270 while (time_before(jiffies, delay))
271 if (wait_event_timeout(ec->wait, ec_check_ibf0(ec),
272 msecs_to_jiffies(1)))
277 static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
281 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
284 memset(t->rdata, 0, t->rlen);
285 mutex_lock(&ec->lock);
286 if (test_bit(EC_FLAGS_BLOCKED, &ec->flags)) {
290 if (ec->global_lock) {
291 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
292 if (ACPI_FAILURE(status)) {
297 if (ec_wait_ibf0(ec)) {
298 pr_err(PREFIX "input buffer is not empty, "
299 "aborting transaction\n");
303 pr_debug(PREFIX "transaction start\n");
304 /* disable GPE during transaction if storm is detected */
305 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
307 * It has to be disabled at the hardware level regardless of the
308 * GPE reference counting, so that it doesn't trigger.
310 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
313 status = acpi_ec_transaction_unlocked(ec, t);
315 /* check if we received SCI during transaction */
316 ec_check_sci_sync(ec, acpi_ec_read_status(ec));
317 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
320 * It is safe to enable the GPE outside of the transaction. Use
321 * acpi_set_gpe() for that, since we used it to disable the GPE
324 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
325 } else if (t->irq_count > ACPI_EC_STORM_THRESHOLD) {
326 pr_info(PREFIX "GPE storm detected, "
327 "transactions will use polling mode\n");
328 set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
330 pr_debug(PREFIX "transaction end\n");
333 acpi_release_global_lock(glk);
335 mutex_unlock(&ec->lock);
339 static int acpi_ec_burst_enable(struct acpi_ec *ec)
342 struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
343 .wdata = NULL, .rdata = &d,
344 .wlen = 0, .rlen = 1};
346 return acpi_ec_transaction(ec, &t);
349 static int acpi_ec_burst_disable(struct acpi_ec *ec)
351 struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
352 .wdata = NULL, .rdata = NULL,
353 .wlen = 0, .rlen = 0};
355 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
356 acpi_ec_transaction(ec, &t) : 0;
359 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
363 struct transaction t = {.command = ACPI_EC_COMMAND_READ,
364 .wdata = &address, .rdata = &d,
365 .wlen = 1, .rlen = 1};
367 result = acpi_ec_transaction(ec, &t);
372 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
374 u8 wdata[2] = { address, data };
375 struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
376 .wdata = wdata, .rdata = NULL,
377 .wlen = 2, .rlen = 0};
379 return acpi_ec_transaction(ec, &t);
383 * Externally callable EC access functions. For now, assume 1 EC only
385 int ec_burst_enable(void)
389 return acpi_ec_burst_enable(first_ec);
392 EXPORT_SYMBOL(ec_burst_enable);
394 int ec_burst_disable(void)
398 return acpi_ec_burst_disable(first_ec);
401 EXPORT_SYMBOL(ec_burst_disable);
403 int ec_read(u8 addr, u8 * val)
411 err = acpi_ec_read(first_ec, addr, &temp_data);
420 EXPORT_SYMBOL(ec_read);
422 int ec_write(u8 addr, u8 val)
429 err = acpi_ec_write(first_ec, addr, val);
434 EXPORT_SYMBOL(ec_write);
436 int ec_transaction(u8 command,
437 const u8 * wdata, unsigned wdata_len,
438 u8 * rdata, unsigned rdata_len,
441 struct transaction t = {.command = command,
442 .wdata = wdata, .rdata = rdata,
443 .wlen = wdata_len, .rlen = rdata_len};
447 return acpi_ec_transaction(first_ec, &t);
450 EXPORT_SYMBOL(ec_transaction);
452 void acpi_ec_block_transactions(void)
454 struct acpi_ec *ec = first_ec;
459 mutex_lock(&ec->lock);
460 /* Prevent transactions from being carried out */
461 set_bit(EC_FLAGS_BLOCKED, &ec->flags);
462 mutex_unlock(&ec->lock);
465 void acpi_ec_unblock_transactions(void)
467 struct acpi_ec *ec = first_ec;
472 mutex_lock(&ec->lock);
473 /* Allow transactions to be carried out again */
474 clear_bit(EC_FLAGS_BLOCKED, &ec->flags);
475 mutex_unlock(&ec->lock);
478 void acpi_ec_unblock_transactions_early(void)
481 * Allow transactions to happen again (this function is called from
482 * atomic context during wakeup, so we don't need to acquire the mutex).
485 clear_bit(EC_FLAGS_BLOCKED, &first_ec->flags);
488 static int acpi_ec_query_unlocked(struct acpi_ec *ec, u8 * data)
492 struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
493 .wdata = NULL, .rdata = &d,
494 .wlen = 0, .rlen = 1};
498 * Query the EC to find out which _Qxx method we need to evaluate.
499 * Note that successful completion of the query causes the ACPI_EC_SCI
500 * bit to be cleared (and thus clearing the interrupt source).
502 result = acpi_ec_transaction_unlocked(ec, &t);
511 /* --------------------------------------------------------------------------
513 -------------------------------------------------------------------------- */
514 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
515 acpi_handle handle, acpi_ec_query_func func,
518 struct acpi_ec_query_handler *handler =
519 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
523 handler->query_bit = query_bit;
524 handler->handle = handle;
525 handler->func = func;
526 handler->data = data;
527 mutex_lock(&ec->lock);
528 list_add(&handler->node, &ec->list);
529 mutex_unlock(&ec->lock);
533 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
535 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
537 struct acpi_ec_query_handler *handler, *tmp;
538 mutex_lock(&ec->lock);
539 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
540 if (query_bit == handler->query_bit) {
541 list_del(&handler->node);
545 mutex_unlock(&ec->lock);
548 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
550 static void acpi_ec_run(void *cxt)
552 struct acpi_ec_query_handler *handler = cxt;
555 pr_debug(PREFIX "start query execution\n");
557 handler->func(handler->data);
558 else if (handler->handle)
559 acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
560 pr_debug(PREFIX "stop query execution\n");
564 static int acpi_ec_sync_query(struct acpi_ec *ec)
568 struct acpi_ec_query_handler *handler, *copy;
569 if ((status = acpi_ec_query_unlocked(ec, &value)))
571 list_for_each_entry(handler, &ec->list, node) {
572 if (value == handler->query_bit) {
573 /* have custom handler for this bit */
574 copy = kmalloc(sizeof(*handler), GFP_KERNEL);
577 memcpy(copy, handler, sizeof(*copy));
578 pr_debug(PREFIX "push query execution (0x%2x) on queue\n", value);
579 return acpi_os_execute((copy->func) ?
580 OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
587 static void acpi_ec_gpe_query(void *ec_cxt)
589 struct acpi_ec *ec = ec_cxt;
592 mutex_lock(&ec->lock);
593 acpi_ec_sync_query(ec);
594 mutex_unlock(&ec->lock);
597 static void acpi_ec_gpe_query(void *ec_cxt);
599 static int ec_check_sci(struct acpi_ec *ec, u8 state)
601 if (state & ACPI_EC_FLAG_SCI) {
602 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
603 pr_debug(PREFIX "push gpe query to the queue\n");
604 return acpi_os_execute(OSL_NOTIFY_HANDLER,
605 acpi_ec_gpe_query, ec);
611 static u32 acpi_ec_gpe_handler(void *data)
613 struct acpi_ec *ec = data;
615 pr_debug(PREFIX "~~~> interrupt\n");
617 advance_transaction(ec, acpi_ec_read_status(ec));
618 if (ec_transaction_done(ec) &&
619 (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
621 ec_check_sci(ec, acpi_ec_read_status(ec));
623 return ACPI_INTERRUPT_HANDLED;
626 /* --------------------------------------------------------------------------
627 Address Space Management
628 -------------------------------------------------------------------------- */
631 acpi_ec_space_handler(u32 function, acpi_physical_address address,
632 u32 bits, u64 *value64,
633 void *handler_context, void *region_context)
635 struct acpi_ec *ec = handler_context;
636 int result = 0, i, bytes = bits / 8;
637 u8 *value = (u8 *)value64;
639 if ((address > 0xFF) || !value || !handler_context)
640 return AE_BAD_PARAMETER;
642 if (function != ACPI_READ && function != ACPI_WRITE)
643 return AE_BAD_PARAMETER;
645 if (EC_FLAGS_MSI || bits > 8)
646 acpi_ec_burst_enable(ec);
648 for (i = 0; i < bytes; ++i, ++address, ++value)
649 result = (function == ACPI_READ) ?
650 acpi_ec_read(ec, address, value) :
651 acpi_ec_write(ec, address, *value);
653 if (EC_FLAGS_MSI || bits > 8)
654 acpi_ec_burst_disable(ec);
658 return AE_BAD_PARAMETER;
671 /* --------------------------------------------------------------------------
673 -------------------------------------------------------------------------- */
675 ec_parse_io_ports(struct acpi_resource *resource, void *context);
677 static struct acpi_ec *make_acpi_ec(void)
679 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
682 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
683 mutex_init(&ec->lock);
684 init_waitqueue_head(&ec->wait);
685 INIT_LIST_HEAD(&ec->list);
686 spin_lock_init(&ec->curr_lock);
691 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
692 void *context, void **return_value)
695 struct acpi_buffer buffer = { sizeof(node_name), node_name };
696 struct acpi_ec *ec = context;
700 status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
702 if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
703 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
709 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
712 unsigned long long tmp = 0;
714 struct acpi_ec *ec = context;
716 /* clear addr values, ec_parse_io_ports depend on it */
717 ec->command_addr = ec->data_addr = 0;
719 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
720 ec_parse_io_ports, ec);
721 if (ACPI_FAILURE(status))
724 /* Get GPE bit assignment (EC events). */
725 /* TODO: Add support for _GPE returning a package */
726 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
727 if (ACPI_FAILURE(status))
730 /* Use the global lock for all EC transactions? */
732 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
733 ec->global_lock = tmp;
735 return AE_CTRL_TERMINATE;
738 static int ec_install_handlers(struct acpi_ec *ec)
741 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
743 status = acpi_install_gpe_handler(NULL, ec->gpe,
744 ACPI_GPE_EDGE_TRIGGERED,
745 &acpi_ec_gpe_handler, ec);
746 if (ACPI_FAILURE(status))
749 acpi_enable_gpe(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
750 status = acpi_install_address_space_handler(ec->handle,
752 &acpi_ec_space_handler,
754 if (ACPI_FAILURE(status)) {
755 if (status == AE_NOT_FOUND) {
757 * Maybe OS fails in evaluating the _REG object.
758 * The AE_NOT_FOUND error will be ignored and OS
759 * continue to initialize EC.
761 printk(KERN_ERR "Fail in evaluating the _REG object"
762 " of EC device. Broken bios is suspected.\n");
764 acpi_remove_gpe_handler(NULL, ec->gpe,
765 &acpi_ec_gpe_handler);
766 acpi_disable_gpe(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
771 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
775 static void ec_remove_handlers(struct acpi_ec *ec)
777 acpi_disable_gpe(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
778 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
779 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
780 pr_err(PREFIX "failed to remove space handler\n");
781 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
782 &acpi_ec_gpe_handler)))
783 pr_err(PREFIX "failed to remove gpe handler\n");
784 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
787 static int acpi_ec_add(struct acpi_device *device)
789 struct acpi_ec *ec = NULL;
792 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
793 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
795 /* Check for boot EC */
797 (boot_ec->handle == device->handle ||
798 boot_ec->handle == ACPI_ROOT_OBJECT)) {
806 if (ec_parse_device(device->handle, 0, ec, NULL) !=
812 ec->handle = device->handle;
814 /* Find and register all query methods */
815 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
816 acpi_ec_register_query_methods, NULL, ec, NULL);
820 device->driver_data = ec;
821 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
822 ec->gpe, ec->command_addr, ec->data_addr);
824 ret = ec_install_handlers(ec);
826 /* EC is fully operational, allow queries */
827 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
831 static int acpi_ec_remove(struct acpi_device *device, int type)
834 struct acpi_ec_query_handler *handler, *tmp;
839 ec = acpi_driver_data(device);
840 ec_remove_handlers(ec);
841 mutex_lock(&ec->lock);
842 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
843 list_del(&handler->node);
846 mutex_unlock(&ec->lock);
847 device->driver_data = NULL;
855 ec_parse_io_ports(struct acpi_resource *resource, void *context)
857 struct acpi_ec *ec = context;
859 if (resource->type != ACPI_RESOURCE_TYPE_IO)
863 * The first address region returned is the data port, and
864 * the second address region returned is the status/command
867 if (ec->data_addr == 0)
868 ec->data_addr = resource->data.io.minimum;
869 else if (ec->command_addr == 0)
870 ec->command_addr = resource->data.io.minimum;
872 return AE_CTRL_TERMINATE;
877 int __init acpi_boot_ec_enable(void)
879 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
881 if (!ec_install_handlers(boot_ec)) {
888 static const struct acpi_device_id ec_device_ids[] = {
893 /* Some BIOS do not survive early DSDT scan, skip it */
894 static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
896 EC_FLAGS_SKIP_DSDT_SCAN = 1;
900 /* ASUStek often supplies us with broken ECDT, validate it */
901 static int ec_validate_ecdt(const struct dmi_system_id *id)
903 EC_FLAGS_VALIDATE_ECDT = 1;
907 /* MSI EC needs special treatment, enable it */
908 static int ec_flag_msi(const struct dmi_system_id *id)
910 printk(KERN_DEBUG PREFIX "Detected MSI hardware, enabling workarounds.\n");
912 EC_FLAGS_VALIDATE_ECDT = 1;
916 static struct dmi_system_id __initdata ec_dmi_table[] = {
918 ec_skip_dsdt_scan, "Compal JFL92", {
919 DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
920 DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
922 ec_flag_msi, "MSI hardware", {
923 DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
925 ec_flag_msi, "MSI hardware", {
926 DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
928 ec_flag_msi, "MSI hardware", {
929 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
931 ec_validate_ecdt, "ASUS hardware", {
932 DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
937 int __init acpi_ec_ecdt_probe(void)
940 struct acpi_ec *saved_ec = NULL;
941 struct acpi_table_ecdt *ecdt_ptr;
943 boot_ec = make_acpi_ec();
947 * Generate a boot ec context
949 dmi_check_system(ec_dmi_table);
950 status = acpi_get_table(ACPI_SIG_ECDT, 1,
951 (struct acpi_table_header **)&ecdt_ptr);
952 if (ACPI_SUCCESS(status)) {
953 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
954 boot_ec->command_addr = ecdt_ptr->control.address;
955 boot_ec->data_addr = ecdt_ptr->data.address;
956 boot_ec->gpe = ecdt_ptr->gpe;
957 boot_ec->handle = ACPI_ROOT_OBJECT;
958 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
959 /* Don't trust ECDT, which comes from ASUSTek */
960 if (!EC_FLAGS_VALIDATE_ECDT)
962 saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
968 if (EC_FLAGS_SKIP_DSDT_SCAN)
971 /* This workaround is needed only on some broken machines,
972 * which require early EC, but fail to provide ECDT */
973 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
974 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
976 /* Check that acpi_get_devices actually find something */
977 if (ACPI_FAILURE(status) || !boot_ec->handle)
980 /* try to find good ECDT from ASUSTek */
981 if (saved_ec->command_addr != boot_ec->command_addr ||
982 saved_ec->data_addr != boot_ec->data_addr ||
983 saved_ec->gpe != boot_ec->gpe ||
984 saved_ec->handle != boot_ec->handle)
985 pr_info(PREFIX "ASUSTek keeps feeding us with broken "
986 "ECDT tables, which are very hard to workaround. "
987 "Trying to use DSDT EC info instead. Please send "
992 /* We really need to limit this workaround, the only ASUS,
993 * which needs it, has fake EC._INI method, so use it as flag.
994 * Keep boot_ec struct as it will be needed soon.
997 if (!dmi_name_in_vendors("ASUS") ||
998 ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
1003 if (!ec_install_handlers(boot_ec)) {
1013 static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
1015 struct acpi_ec *ec = acpi_driver_data(device);
1016 /* Stop using the GPE, but keep it reference counted. */
1017 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
1021 static int acpi_ec_resume(struct acpi_device *device)
1023 struct acpi_ec *ec = acpi_driver_data(device);
1024 /* Enable the GPE again, but don't reference count it once more. */
1025 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
1029 static struct acpi_driver acpi_ec_driver = {
1031 .class = ACPI_EC_CLASS,
1032 .ids = ec_device_ids,
1035 .remove = acpi_ec_remove,
1036 .suspend = acpi_ec_suspend,
1037 .resume = acpi_ec_resume,
1041 int __init acpi_ec_init(void)
1045 /* Now register the driver for the EC */
1046 result = acpi_bus_register_driver(&acpi_ec_driver);
1053 /* EC driver currently not unloadable */
1055 static void __exit acpi_ec_exit(void)
1058 acpi_bus_unregister_driver(&acpi_ec_driver);