2 * ec.c - ACPI Embedded Controller Driver (v2.1)
4 * Copyright (C) 2006-2008 Alexey Starikovskiy <astarikovskiy@suse.de>
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 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_MSI_UDELAY 550 /* Wait 550us for MSI EC */
73 #define ACPI_EC_CLEAR_MAX 100 /* Maximum number of events to query
74 * when trying to clear the EC */
77 EC_FLAGS_QUERY_PENDING, /* Query is pending */
78 EC_FLAGS_GPE_STORM, /* GPE storm detected */
79 EC_FLAGS_HANDLERS_INSTALLED, /* Handlers for GPE and
80 * OpReg are installed */
81 EC_FLAGS_BLOCKED, /* Transactions are blocked */
84 #define ACPI_EC_COMMAND_POLL 0x01 /* Available for command byte */
85 #define ACPI_EC_COMMAND_COMPLETE 0x02 /* Completed last byte */
87 /* ec.c is compiled in acpi namespace so this shows up as acpi.ec_delay param */
88 static unsigned int ec_delay __read_mostly = ACPI_EC_DELAY;
89 module_param(ec_delay, uint, 0644);
90 MODULE_PARM_DESC(ec_delay, "Timeout(ms) waited until an EC command completes");
93 * If the number of false interrupts per one transaction exceeds
94 * this threshold, will think there is a GPE storm happened and
95 * will disable the GPE for normal transaction.
97 static unsigned int ec_storm_threshold __read_mostly = 8;
98 module_param(ec_storm_threshold, uint, 0644);
99 MODULE_PARM_DESC(ec_storm_threshold, "Maxim false GPE numbers not considered as GPE storm");
101 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
102 /* External interfaces use first EC only, so remember */
103 typedef int (*acpi_ec_query_func) (void *data);
105 struct acpi_ec_query_handler {
106 struct list_head node;
107 acpi_ec_query_func func;
116 unsigned short irq_count;
125 struct acpi_ec *boot_ec, *first_ec;
126 EXPORT_SYMBOL(first_ec);
128 static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
129 static int EC_FLAGS_VALIDATE_ECDT; /* ASUStec ECDTs need to be validated */
130 static int EC_FLAGS_SKIP_DSDT_SCAN; /* Not all BIOS survive early DSDT scan */
131 static int EC_FLAGS_CLEAR_ON_RESUME; /* Needs acpi_ec_clear() on boot/resume */
133 /* --------------------------------------------------------------------------
134 Transaction Management
135 -------------------------------------------------------------------------- */
137 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
139 u8 x = inb(ec->command_addr);
140 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
144 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
146 u8 x = inb(ec->data_addr);
147 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
151 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
153 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
154 outb(command, ec->command_addr);
157 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
159 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
160 outb(data, ec->data_addr);
163 static int ec_transaction_completed(struct acpi_ec *ec)
167 spin_lock_irqsave(&ec->curr_lock, flags);
168 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE))
170 spin_unlock_irqrestore(&ec->curr_lock, flags);
174 static bool advance_transaction(struct acpi_ec *ec)
176 struct transaction *t;
180 pr_debug(PREFIX "===== %s =====\n", in_interrupt() ? "IRQ" : "TASK");
181 status = acpi_ec_read_status(ec);
185 if (t->flags & ACPI_EC_COMMAND_POLL) {
186 if (t->wlen > t->wi) {
187 if ((status & ACPI_EC_FLAG_IBF) == 0)
188 acpi_ec_write_data(ec, t->wdata[t->wi++]);
191 } else if (t->rlen > t->ri) {
192 if ((status & ACPI_EC_FLAG_OBF) == 1) {
193 t->rdata[t->ri++] = acpi_ec_read_data(ec);
194 if (t->rlen == t->ri) {
195 t->flags |= ACPI_EC_COMMAND_COMPLETE;
196 if (t->command == ACPI_EC_COMMAND_QUERY)
197 pr_debug("hardware QR_EC completion\n");
202 } else if (t->wlen == t->wi &&
203 (status & ACPI_EC_FLAG_IBF) == 0) {
204 t->flags |= ACPI_EC_COMMAND_COMPLETE;
210 * There is firmware refusing to respond QR_EC when SCI_EVT
211 * is not set, for which case, we complete the QR_EC
212 * without issuing it to the firmware.
213 * https://bugzilla.kernel.org/show_bug.cgi?id=86211
215 if (!(status & ACPI_EC_FLAG_SCI) &&
216 (t->command == ACPI_EC_COMMAND_QUERY)) {
217 t->flags |= ACPI_EC_COMMAND_POLL;
218 t->rdata[t->ri++] = 0x00;
219 t->flags |= ACPI_EC_COMMAND_COMPLETE;
220 pr_debug("software QR_EC completion\n");
222 } else if ((status & ACPI_EC_FLAG_IBF) == 0) {
223 acpi_ec_write_cmd(ec, t->command);
224 t->flags |= ACPI_EC_COMMAND_POLL;
231 * If SCI bit is set, then don't think it's a false IRQ
232 * otherwise will take a not handled IRQ as a false one.
234 if (!(status & ACPI_EC_FLAG_SCI)) {
235 if (in_interrupt() && t)
241 static void start_transaction(struct acpi_ec *ec)
243 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
245 (void)advance_transaction(ec);
248 static int acpi_ec_sync_query(struct acpi_ec *ec, u8 *data);
250 static int ec_check_sci_sync(struct acpi_ec *ec, u8 state)
252 if (state & ACPI_EC_FLAG_SCI) {
253 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
254 return acpi_ec_sync_query(ec, NULL);
259 static int ec_poll(struct acpi_ec *ec)
262 int repeat = 5; /* number of command restarts */
264 unsigned long delay = jiffies +
265 msecs_to_jiffies(ec_delay);
267 /* don't sleep with disabled interrupts */
268 if (EC_FLAGS_MSI || irqs_disabled()) {
269 udelay(ACPI_EC_MSI_UDELAY);
270 if (ec_transaction_completed(ec))
273 if (wait_event_timeout(ec->wait,
274 ec_transaction_completed(ec),
275 msecs_to_jiffies(1)))
278 spin_lock_irqsave(&ec->curr_lock, flags);
279 (void)advance_transaction(ec);
280 spin_unlock_irqrestore(&ec->curr_lock, flags);
281 } while (time_before(jiffies, delay));
282 pr_debug(PREFIX "controller reset, restart transaction\n");
283 spin_lock_irqsave(&ec->curr_lock, flags);
284 start_transaction(ec);
285 spin_unlock_irqrestore(&ec->curr_lock, flags);
290 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
291 struct transaction *t)
296 udelay(ACPI_EC_MSI_UDELAY);
297 /* start transaction */
298 spin_lock_irqsave(&ec->curr_lock, tmp);
299 /* following two actions should be kept atomic */
301 start_transaction(ec);
302 if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
303 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
304 spin_unlock_irqrestore(&ec->curr_lock, tmp);
306 spin_lock_irqsave(&ec->curr_lock, tmp);
308 spin_unlock_irqrestore(&ec->curr_lock, tmp);
312 static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
316 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
319 memset(t->rdata, 0, t->rlen);
320 mutex_lock(&ec->lock);
321 if (test_bit(EC_FLAGS_BLOCKED, &ec->flags)) {
325 if (ec->global_lock) {
326 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
327 if (ACPI_FAILURE(status)) {
332 pr_debug(PREFIX "transaction start (cmd=0x%02x, addr=0x%02x)\n",
333 t->command, t->wdata ? t->wdata[0] : 0);
334 /* disable GPE during transaction if storm is detected */
335 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
336 /* It has to be disabled, so that it doesn't trigger. */
337 acpi_disable_gpe(NULL, ec->gpe);
340 status = acpi_ec_transaction_unlocked(ec, t);
342 /* check if we received SCI during transaction */
343 ec_check_sci_sync(ec, acpi_ec_read_status(ec));
344 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
346 /* It is safe to enable the GPE outside of the transaction. */
347 acpi_enable_gpe(NULL, ec->gpe);
348 } else if (t->irq_count > ec_storm_threshold) {
349 pr_info(PREFIX "GPE storm detected(%d GPEs), "
350 "transactions will use polling mode\n",
352 set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
354 pr_debug(PREFIX "transaction end\n");
356 acpi_release_global_lock(glk);
358 mutex_unlock(&ec->lock);
362 static int acpi_ec_burst_enable(struct acpi_ec *ec)
365 struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
366 .wdata = NULL, .rdata = &d,
367 .wlen = 0, .rlen = 1};
369 return acpi_ec_transaction(ec, &t);
372 static int acpi_ec_burst_disable(struct acpi_ec *ec)
374 struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
375 .wdata = NULL, .rdata = NULL,
376 .wlen = 0, .rlen = 0};
378 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
379 acpi_ec_transaction(ec, &t) : 0;
382 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
386 struct transaction t = {.command = ACPI_EC_COMMAND_READ,
387 .wdata = &address, .rdata = &d,
388 .wlen = 1, .rlen = 1};
390 result = acpi_ec_transaction(ec, &t);
395 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
397 u8 wdata[2] = { address, data };
398 struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
399 .wdata = wdata, .rdata = NULL,
400 .wlen = 2, .rlen = 0};
402 return acpi_ec_transaction(ec, &t);
406 * Externally callable EC access functions. For now, assume 1 EC only
408 int ec_burst_enable(void)
412 return acpi_ec_burst_enable(first_ec);
415 EXPORT_SYMBOL(ec_burst_enable);
417 int ec_burst_disable(void)
421 return acpi_ec_burst_disable(first_ec);
424 EXPORT_SYMBOL(ec_burst_disable);
426 int ec_read(u8 addr, u8 *val)
434 err = acpi_ec_read(first_ec, addr, &temp_data);
443 EXPORT_SYMBOL(ec_read);
445 int ec_write(u8 addr, u8 val)
452 err = acpi_ec_write(first_ec, addr, val);
457 EXPORT_SYMBOL(ec_write);
459 int ec_transaction(u8 command,
460 const u8 * wdata, unsigned wdata_len,
461 u8 * rdata, unsigned rdata_len)
463 struct transaction t = {.command = command,
464 .wdata = wdata, .rdata = rdata,
465 .wlen = wdata_len, .rlen = rdata_len};
469 return acpi_ec_transaction(first_ec, &t);
472 EXPORT_SYMBOL(ec_transaction);
475 * Process _Q events that might have accumulated in the EC.
476 * Run with locked ec mutex.
478 static void acpi_ec_clear(struct acpi_ec *ec)
483 for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) {
484 status = acpi_ec_sync_query(ec, &value);
485 if (status || !value)
489 if (unlikely(i == ACPI_EC_CLEAR_MAX))
490 pr_warn("Warning: Maximum of %d stale EC events cleared\n", i);
492 pr_info("%d stale EC events cleared\n", i);
495 void acpi_ec_block_transactions(void)
497 struct acpi_ec *ec = first_ec;
502 mutex_lock(&ec->lock);
503 /* Prevent transactions from being carried out */
504 set_bit(EC_FLAGS_BLOCKED, &ec->flags);
505 mutex_unlock(&ec->lock);
508 void acpi_ec_unblock_transactions(void)
510 struct acpi_ec *ec = first_ec;
515 mutex_lock(&ec->lock);
516 /* Allow transactions to be carried out again */
517 clear_bit(EC_FLAGS_BLOCKED, &ec->flags);
519 if (EC_FLAGS_CLEAR_ON_RESUME)
522 mutex_unlock(&ec->lock);
525 void acpi_ec_unblock_transactions_early(void)
528 * Allow transactions to happen again (this function is called from
529 * atomic context during wakeup, so we don't need to acquire the mutex).
532 clear_bit(EC_FLAGS_BLOCKED, &first_ec->flags);
535 static int acpi_ec_query_unlocked(struct acpi_ec *ec, u8 * data)
539 struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
540 .wdata = NULL, .rdata = &d,
541 .wlen = 0, .rlen = 1};
545 * Query the EC to find out which _Qxx method we need to evaluate.
546 * Note that successful completion of the query causes the ACPI_EC_SCI
547 * bit to be cleared (and thus clearing the interrupt source).
549 result = acpi_ec_transaction_unlocked(ec, &t);
558 /* --------------------------------------------------------------------------
560 -------------------------------------------------------------------------- */
561 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
562 acpi_handle handle, acpi_ec_query_func func,
565 struct acpi_ec_query_handler *handler =
566 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
570 handler->query_bit = query_bit;
571 handler->handle = handle;
572 handler->func = func;
573 handler->data = data;
574 mutex_lock(&ec->lock);
575 list_add(&handler->node, &ec->list);
576 mutex_unlock(&ec->lock);
580 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
582 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
584 struct acpi_ec_query_handler *handler, *tmp;
585 mutex_lock(&ec->lock);
586 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
587 if (query_bit == handler->query_bit) {
588 list_del(&handler->node);
592 mutex_unlock(&ec->lock);
595 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
597 static void acpi_ec_run(void *cxt)
599 struct acpi_ec_query_handler *handler = cxt;
602 pr_debug(PREFIX "start query execution\n");
604 handler->func(handler->data);
605 else if (handler->handle)
606 acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
607 pr_debug(PREFIX "stop query execution\n");
611 static int acpi_ec_sync_query(struct acpi_ec *ec, u8 *data)
615 struct acpi_ec_query_handler *handler, *copy;
617 status = acpi_ec_query_unlocked(ec, &value);
623 list_for_each_entry(handler, &ec->list, node) {
624 if (value == handler->query_bit) {
625 /* have custom handler for this bit */
626 copy = kmalloc(sizeof(*handler), GFP_KERNEL);
629 memcpy(copy, handler, sizeof(*copy));
630 pr_debug(PREFIX "push query execution (0x%2x) on queue\n", value);
631 return acpi_os_execute((copy->func) ?
632 OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
639 static void acpi_ec_gpe_query(void *ec_cxt)
641 struct acpi_ec *ec = ec_cxt;
644 mutex_lock(&ec->lock);
645 acpi_ec_sync_query(ec, NULL);
646 mutex_unlock(&ec->lock);
649 static int ec_check_sci(struct acpi_ec *ec, u8 state)
651 if (state & ACPI_EC_FLAG_SCI) {
652 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
653 pr_debug(PREFIX "push gpe query to the queue\n");
654 return acpi_os_execute(OSL_NOTIFY_HANDLER,
655 acpi_ec_gpe_query, ec);
661 static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
662 u32 gpe_number, void *data)
665 struct acpi_ec *ec = data;
667 spin_lock_irqsave(&ec->curr_lock, flags);
668 if (advance_transaction(ec))
670 spin_unlock_irqrestore(&ec->curr_lock, flags);
671 ec_check_sci(ec, acpi_ec_read_status(ec));
672 return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
675 /* --------------------------------------------------------------------------
676 Address Space Management
677 -------------------------------------------------------------------------- */
680 acpi_ec_space_handler(u32 function, acpi_physical_address address,
681 u32 bits, u64 *value64,
682 void *handler_context, void *region_context)
684 struct acpi_ec *ec = handler_context;
685 int result = 0, i, bytes = bits / 8;
686 u8 *value = (u8 *)value64;
688 if ((address > 0xFF) || !value || !handler_context)
689 return AE_BAD_PARAMETER;
691 if (function != ACPI_READ && function != ACPI_WRITE)
692 return AE_BAD_PARAMETER;
694 if (EC_FLAGS_MSI || bits > 8)
695 acpi_ec_burst_enable(ec);
697 for (i = 0; i < bytes; ++i, ++address, ++value)
698 result = (function == ACPI_READ) ?
699 acpi_ec_read(ec, address, value) :
700 acpi_ec_write(ec, address, *value);
702 if (EC_FLAGS_MSI || bits > 8)
703 acpi_ec_burst_disable(ec);
707 return AE_BAD_PARAMETER;
720 /* --------------------------------------------------------------------------
722 -------------------------------------------------------------------------- */
724 ec_parse_io_ports(struct acpi_resource *resource, void *context);
726 static struct acpi_ec *make_acpi_ec(void)
728 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
731 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
732 mutex_init(&ec->lock);
733 init_waitqueue_head(&ec->wait);
734 INIT_LIST_HEAD(&ec->list);
735 spin_lock_init(&ec->curr_lock);
740 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
741 void *context, void **return_value)
744 struct acpi_buffer buffer = { sizeof(node_name), node_name };
745 struct acpi_ec *ec = context;
749 status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
751 if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
752 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
758 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
761 unsigned long long tmp = 0;
763 struct acpi_ec *ec = context;
765 /* clear addr values, ec_parse_io_ports depend on it */
766 ec->command_addr = ec->data_addr = 0;
768 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
769 ec_parse_io_ports, ec);
770 if (ACPI_FAILURE(status))
773 /* Get GPE bit assignment (EC events). */
774 /* TODO: Add support for _GPE returning a package */
775 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
776 if (ACPI_FAILURE(status))
779 /* Use the global lock for all EC transactions? */
781 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
782 ec->global_lock = tmp;
784 return AE_CTRL_TERMINATE;
787 static int ec_install_handlers(struct acpi_ec *ec)
790 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
792 status = acpi_install_gpe_handler(NULL, ec->gpe,
793 ACPI_GPE_EDGE_TRIGGERED,
794 &acpi_ec_gpe_handler, ec);
795 if (ACPI_FAILURE(status))
798 acpi_enable_gpe(NULL, ec->gpe);
799 status = acpi_install_address_space_handler(ec->handle,
801 &acpi_ec_space_handler,
803 if (ACPI_FAILURE(status)) {
804 if (status == AE_NOT_FOUND) {
806 * Maybe OS fails in evaluating the _REG object.
807 * The AE_NOT_FOUND error will be ignored and OS
808 * continue to initialize EC.
810 printk(KERN_ERR "Fail in evaluating the _REG object"
811 " of EC device. Broken bios is suspected.\n");
813 acpi_remove_gpe_handler(NULL, ec->gpe,
814 &acpi_ec_gpe_handler);
815 acpi_disable_gpe(NULL, ec->gpe);
820 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
824 static void ec_remove_handlers(struct acpi_ec *ec)
826 acpi_disable_gpe(NULL, ec->gpe);
827 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
828 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
829 pr_err(PREFIX "failed to remove space handler\n");
830 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
831 &acpi_ec_gpe_handler)))
832 pr_err(PREFIX "failed to remove gpe handler\n");
833 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
836 static int acpi_ec_add(struct acpi_device *device)
838 struct acpi_ec *ec = NULL;
841 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
842 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
844 /* Check for boot EC */
846 (boot_ec->handle == device->handle ||
847 boot_ec->handle == ACPI_ROOT_OBJECT)) {
855 if (ec_parse_device(device->handle, 0, ec, NULL) !=
861 /* Find and register all query methods */
862 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
863 acpi_ec_register_query_methods, NULL, ec, NULL);
867 device->driver_data = ec;
869 WARN(!request_region(ec->data_addr, 1, "EC data"),
870 "Could not request EC data io port 0x%lx", ec->data_addr);
871 WARN(!request_region(ec->command_addr, 1, "EC cmd"),
872 "Could not request EC cmd io port 0x%lx", ec->command_addr);
874 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
875 ec->gpe, ec->command_addr, ec->data_addr);
877 ret = ec_install_handlers(ec);
879 /* EC is fully operational, allow queries */
880 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
882 /* Clear stale _Q events if hardware might require that */
883 if (EC_FLAGS_CLEAR_ON_RESUME) {
884 mutex_lock(&ec->lock);
886 mutex_unlock(&ec->lock);
891 static int acpi_ec_remove(struct acpi_device *device, int type)
894 struct acpi_ec_query_handler *handler, *tmp;
899 ec = acpi_driver_data(device);
900 ec_remove_handlers(ec);
901 mutex_lock(&ec->lock);
902 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
903 list_del(&handler->node);
906 mutex_unlock(&ec->lock);
907 release_region(ec->data_addr, 1);
908 release_region(ec->command_addr, 1);
909 device->driver_data = NULL;
917 ec_parse_io_ports(struct acpi_resource *resource, void *context)
919 struct acpi_ec *ec = context;
921 if (resource->type != ACPI_RESOURCE_TYPE_IO)
925 * The first address region returned is the data port, and
926 * the second address region returned is the status/command
929 if (ec->data_addr == 0)
930 ec->data_addr = resource->data.io.minimum;
931 else if (ec->command_addr == 0)
932 ec->command_addr = resource->data.io.minimum;
934 return AE_CTRL_TERMINATE;
939 int __init acpi_boot_ec_enable(void)
941 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
943 if (!ec_install_handlers(boot_ec)) {
950 static const struct acpi_device_id ec_device_ids[] = {
955 /* Some BIOS do not survive early DSDT scan, skip it */
956 static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
958 EC_FLAGS_SKIP_DSDT_SCAN = 1;
962 /* ASUStek often supplies us with broken ECDT, validate it */
963 static int ec_validate_ecdt(const struct dmi_system_id *id)
965 EC_FLAGS_VALIDATE_ECDT = 1;
969 /* MSI EC needs special treatment, enable it */
970 static int ec_flag_msi(const struct dmi_system_id *id)
972 printk(KERN_DEBUG PREFIX "Detected MSI hardware, enabling workarounds.\n");
974 EC_FLAGS_VALIDATE_ECDT = 1;
979 * Clevo M720 notebook actually works ok with IRQ mode, if we lifted
980 * the GPE storm threshold back to 20
982 static int ec_enlarge_storm_threshold(const struct dmi_system_id *id)
984 pr_debug("Setting the EC GPE storm threshold to 20\n");
985 ec_storm_threshold = 20;
990 * On some hardware it is necessary to clear events accumulated by the EC during
991 * sleep. These ECs stop reporting GPEs until they are manually polled, if too
992 * many events are accumulated. (e.g. Samsung Series 5/9 notebooks)
994 * https://bugzilla.kernel.org/show_bug.cgi?id=44161
996 * Ideally, the EC should also be instructed NOT to accumulate events during
997 * sleep (which Windows seems to do somehow), but the interface to control this
998 * behaviour is not known at this time.
1000 * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx,
1001 * however it is very likely that other Samsung models are affected.
1003 * On systems which don't accumulate _Q events during sleep, this extra check
1004 * should be harmless.
1006 static int ec_clear_on_resume(const struct dmi_system_id *id)
1008 pr_debug("Detected system needing EC poll on resume.\n");
1009 EC_FLAGS_CLEAR_ON_RESUME = 1;
1013 static struct dmi_system_id __initdata ec_dmi_table[] = {
1015 ec_skip_dsdt_scan, "Compal JFL92", {
1016 DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
1017 DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
1019 ec_flag_msi, "MSI hardware", {
1020 DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
1022 ec_flag_msi, "MSI hardware", {
1023 DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
1025 ec_flag_msi, "MSI hardware", {
1026 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
1028 ec_flag_msi, "MSI hardware", {
1029 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR")}, NULL},
1031 ec_flag_msi, "Quanta hardware", {
1032 DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
1033 DMI_MATCH(DMI_PRODUCT_NAME, "TW8/SW8/DW8"),}, NULL},
1035 ec_flag_msi, "Quanta hardware", {
1036 DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
1037 DMI_MATCH(DMI_PRODUCT_NAME, "TW9/SW9"),}, NULL},
1039 ec_validate_ecdt, "ASUS hardware", {
1040 DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
1042 ec_validate_ecdt, "ASUS hardware", {
1043 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc.") }, NULL},
1045 ec_enlarge_storm_threshold, "CLEVO hardware", {
1046 DMI_MATCH(DMI_SYS_VENDOR, "CLEVO Co."),
1047 DMI_MATCH(DMI_PRODUCT_NAME, "M720T/M730T"),}, NULL},
1049 ec_validate_ecdt, "ASUS hardware", {
1050 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTek Computer Inc."),
1051 DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),}, NULL},
1053 ec_clear_on_resume, "Samsung hardware", {
1054 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD.")}, NULL},
1058 int __init acpi_ec_ecdt_probe(void)
1061 struct acpi_ec *saved_ec = NULL;
1062 struct acpi_table_ecdt *ecdt_ptr;
1064 boot_ec = make_acpi_ec();
1068 * Generate a boot ec context
1070 dmi_check_system(ec_dmi_table);
1071 status = acpi_get_table(ACPI_SIG_ECDT, 1,
1072 (struct acpi_table_header **)&ecdt_ptr);
1073 if (ACPI_SUCCESS(status)) {
1074 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
1075 boot_ec->command_addr = ecdt_ptr->control.address;
1076 boot_ec->data_addr = ecdt_ptr->data.address;
1077 boot_ec->gpe = ecdt_ptr->gpe;
1078 boot_ec->handle = ACPI_ROOT_OBJECT;
1079 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
1080 /* Don't trust ECDT, which comes from ASUSTek */
1081 if (!EC_FLAGS_VALIDATE_ECDT)
1083 saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
1089 if (EC_FLAGS_SKIP_DSDT_SCAN)
1092 /* This workaround is needed only on some broken machines,
1093 * which require early EC, but fail to provide ECDT */
1094 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
1095 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
1097 /* Check that acpi_get_devices actually find something */
1098 if (ACPI_FAILURE(status) || !boot_ec->handle)
1101 /* try to find good ECDT from ASUSTek */
1102 if (saved_ec->command_addr != boot_ec->command_addr ||
1103 saved_ec->data_addr != boot_ec->data_addr ||
1104 saved_ec->gpe != boot_ec->gpe ||
1105 saved_ec->handle != boot_ec->handle)
1106 pr_info(PREFIX "ASUSTek keeps feeding us with broken "
1107 "ECDT tables, which are very hard to workaround. "
1108 "Trying to use DSDT EC info instead. Please send "
1109 "output of acpidump to linux-acpi@vger.kernel.org\n");
1113 /* We really need to limit this workaround, the only ASUS,
1114 * which needs it, has fake EC._INI method, so use it as flag.
1115 * Keep boot_ec struct as it will be needed soon.
1118 if (!dmi_name_in_vendors("ASUS") ||
1119 ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
1124 if (!ec_install_handlers(boot_ec)) {
1134 static struct acpi_driver acpi_ec_driver = {
1136 .class = ACPI_EC_CLASS,
1137 .ids = ec_device_ids,
1140 .remove = acpi_ec_remove,
1144 int __init acpi_ec_init(void)
1148 /* Now register the driver for the EC */
1149 result = acpi_bus_register_driver(&acpi_ec_driver);
1156 /* EC driver currently not unloadable */
1158 static void __exit acpi_ec_exit(void)
1161 acpi_bus_unregister_driver(&acpi_ec_driver);