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/proc_fs.h>
38 #include <linux/seq_file.h>
39 #include <linux/interrupt.h>
40 #include <linux/list.h>
41 #include <linux/spinlock.h>
43 #include <acpi/acpi_bus.h>
44 #include <acpi/acpi_drivers.h>
45 #include <linux/dmi.h>
47 #define ACPI_EC_CLASS "embedded_controller"
48 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
49 #define ACPI_EC_FILE_INFO "info"
52 #define PREFIX "ACPI: EC: "
54 /* EC status register */
55 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
56 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
57 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
58 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
62 ACPI_EC_COMMAND_READ = 0x80,
63 ACPI_EC_COMMAND_WRITE = 0x81,
64 ACPI_EC_BURST_ENABLE = 0x82,
65 ACPI_EC_BURST_DISABLE = 0x83,
66 ACPI_EC_COMMAND_QUERY = 0x84,
69 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
70 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
71 #define ACPI_EC_CDELAY 10 /* Wait 10us before polling EC */
73 #define ACPI_EC_STORM_THRESHOLD 8 /* number of false interrupts
74 per one transaction */
77 EC_FLAGS_QUERY_PENDING, /* Query is pending */
78 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent
79 * for status change */
80 EC_FLAGS_NO_GPE, /* Don't use GPE mode */
81 EC_FLAGS_GPE_STORM, /* GPE storm detected */
82 EC_FLAGS_HANDLERS_INSTALLED /* Handlers for GPE and
83 * OpReg are installed */
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 static struct acpi_ec {
113 unsigned long command_addr;
114 unsigned long data_addr;
115 unsigned long global_lock;
118 wait_queue_head_t wait;
119 struct list_head list;
120 struct transaction *curr;
121 spinlock_t curr_lock;
122 } *boot_ec, *first_ec;
124 static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
126 /* --------------------------------------------------------------------------
127 Transaction Management
128 -------------------------------------------------------------------------- */
130 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
132 u8 x = inb(ec->command_addr);
133 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
137 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
139 u8 x = inb(ec->data_addr);
140 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
144 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
146 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
147 outb(command, ec->command_addr);
150 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
152 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
153 outb(data, ec->data_addr);
156 static int ec_transaction_done(struct acpi_ec *ec)
160 spin_lock_irqsave(&ec->curr_lock, flags);
161 if (!ec->curr || ec->curr->done)
163 spin_unlock_irqrestore(&ec->curr_lock, flags);
167 static void start_transaction(struct acpi_ec *ec)
169 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
170 ec->curr->done = false;
171 acpi_ec_write_cmd(ec, ec->curr->command);
174 static void gpe_transaction(struct acpi_ec *ec, u8 status)
177 spin_lock_irqsave(&ec->curr_lock, flags);
180 if (ec->curr->wlen > ec->curr->wi) {
181 if ((status & ACPI_EC_FLAG_IBF) == 0)
182 acpi_ec_write_data(ec,
183 ec->curr->wdata[ec->curr->wi++]);
186 } else if (ec->curr->rlen > ec->curr->ri) {
187 if ((status & ACPI_EC_FLAG_OBF) == 1) {
188 ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
189 if (ec->curr->rlen == ec->curr->ri)
190 ec->curr->done = true;
193 } else if (ec->curr->wlen == ec->curr->wi &&
194 (status & ACPI_EC_FLAG_IBF) == 0)
195 ec->curr->done = true;
198 /* false interrupt, state didn't change */
200 ++ec->curr->irq_count;
202 spin_unlock_irqrestore(&ec->curr_lock, flags);
205 static int acpi_ec_wait(struct acpi_ec *ec)
207 if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
208 msecs_to_jiffies(ACPI_EC_DELAY)))
210 /* try restart command if we get any false interrupts */
211 if (ec->curr->irq_count &&
212 (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
213 pr_debug(PREFIX "controller reset, restart transaction\n");
214 start_transaction(ec);
215 if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
216 msecs_to_jiffies(ACPI_EC_DELAY)))
219 /* missing GPEs, switch back to poll mode */
220 if (printk_ratelimit())
221 pr_info(PREFIX "missing confirmations, "
222 "switch off interrupt mode.\n");
223 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
224 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
228 static void acpi_ec_gpe_query(void *ec_cxt);
230 static int ec_check_sci(struct acpi_ec *ec, u8 state)
232 if (state & ACPI_EC_FLAG_SCI) {
233 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
234 return acpi_os_execute(OSL_EC_BURST_HANDLER,
235 acpi_ec_gpe_query, ec);
240 static void ec_delay(void)
242 /* EC in MSI notebooks don't tolerate delays other than 550 usec */
244 udelay(ACPI_EC_DELAY);
246 /* Use shortest sleep available */
250 static int ec_poll(struct acpi_ec *ec)
252 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
253 udelay(ACPI_EC_CDELAY);
254 while (time_before(jiffies, delay)) {
255 gpe_transaction(ec, acpi_ec_read_status(ec));
257 if (ec_transaction_done(ec))
263 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
264 struct transaction *t,
269 pr_debug(PREFIX "transaction start\n");
270 /* disable GPE during transaction if storm is detected */
271 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
272 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
273 acpi_disable_gpe(NULL, ec->gpe);
276 udelay(ACPI_EC_DELAY);
277 /* start transaction */
278 spin_lock_irqsave(&ec->curr_lock, tmp);
279 /* following two actions should be kept atomic */
281 start_transaction(ec);
282 if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
283 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
284 spin_unlock_irqrestore(&ec->curr_lock, tmp);
285 /* if we selected poll mode or failed in GPE-mode do a poll loop */
287 !test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ||
290 pr_debug(PREFIX "transaction end\n");
291 spin_lock_irqsave(&ec->curr_lock, tmp);
293 spin_unlock_irqrestore(&ec->curr_lock, tmp);
294 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
295 /* check if we received SCI during transaction */
296 ec_check_sci(ec, acpi_ec_read_status(ec));
297 /* it is safe to enable GPE outside of transaction */
298 acpi_enable_gpe(NULL, ec->gpe);
299 } else if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
300 t->irq_count > ACPI_EC_STORM_THRESHOLD) {
301 pr_info(PREFIX "GPE storm detected, "
302 "transactions will use polling mode\n");
303 set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
308 static int ec_check_ibf0(struct acpi_ec *ec)
310 u8 status = acpi_ec_read_status(ec);
311 return (status & ACPI_EC_FLAG_IBF) == 0;
314 static int ec_wait_ibf0(struct acpi_ec *ec)
316 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
317 /* interrupt wait manually if GPE mode is not active */
318 unsigned long timeout = test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ?
319 msecs_to_jiffies(ACPI_EC_DELAY) : msecs_to_jiffies(1);
320 while (time_before(jiffies, delay))
321 if (wait_event_timeout(ec->wait, ec_check_ibf0(ec), timeout))
326 static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t,
331 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
334 memset(t->rdata, 0, t->rlen);
335 mutex_lock(&ec->lock);
336 if (ec->global_lock) {
337 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
338 if (ACPI_FAILURE(status)) {
343 if (ec_wait_ibf0(ec)) {
344 pr_err(PREFIX "input buffer is not empty, "
345 "aborting transaction\n");
349 status = acpi_ec_transaction_unlocked(ec, t, force_poll);
352 acpi_release_global_lock(glk);
354 mutex_unlock(&ec->lock);
359 * Note: samsung nv5000 doesn't work with ec burst mode.
360 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
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, 0);
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) : 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, 0);
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, 0);
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,
464 struct transaction t = {.command = command,
465 .wdata = wdata, .rdata = rdata,
466 .wlen = wdata_len, .rlen = rdata_len};
470 return acpi_ec_transaction(first_ec, &t, force_poll);
473 EXPORT_SYMBOL(ec_transaction);
475 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
479 struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
480 .wdata = NULL, .rdata = &d,
481 .wlen = 0, .rlen = 1};
486 * Query the EC to find out which _Qxx method we need to evaluate.
487 * Note that successful completion of the query causes the ACPI_EC_SCI
488 * bit to be cleared (and thus clearing the interrupt source).
491 result = acpi_ec_transaction(ec, &t, 0);
502 /* --------------------------------------------------------------------------
504 -------------------------------------------------------------------------- */
505 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
506 acpi_handle handle, acpi_ec_query_func func,
509 struct acpi_ec_query_handler *handler =
510 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
514 handler->query_bit = query_bit;
515 handler->handle = handle;
516 handler->func = func;
517 handler->data = data;
518 mutex_lock(&ec->lock);
519 list_add(&handler->node, &ec->list);
520 mutex_unlock(&ec->lock);
524 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
526 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
528 struct acpi_ec_query_handler *handler, *tmp;
529 mutex_lock(&ec->lock);
530 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
531 if (query_bit == handler->query_bit) {
532 list_del(&handler->node);
536 mutex_unlock(&ec->lock);
539 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
541 static void acpi_ec_gpe_query(void *ec_cxt)
543 struct acpi_ec *ec = ec_cxt;
545 struct acpi_ec_query_handler *handler, copy;
547 if (!ec || acpi_ec_query(ec, &value))
549 mutex_lock(&ec->lock);
550 list_for_each_entry(handler, &ec->list, node) {
551 if (value == handler->query_bit) {
552 /* have custom handler for this bit */
553 memcpy(©, handler, sizeof(copy));
554 mutex_unlock(&ec->lock);
556 copy.func(copy.data);
557 } else if (copy.handle) {
558 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
563 mutex_unlock(&ec->lock);
566 static u32 acpi_ec_gpe_handler(void *data)
568 struct acpi_ec *ec = data;
571 pr_debug(PREFIX "~~~> interrupt\n");
572 status = acpi_ec_read_status(ec);
574 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) {
575 gpe_transaction(ec, status);
576 if (ec_transaction_done(ec) &&
577 (status & ACPI_EC_FLAG_IBF) == 0)
581 ec_check_sci(ec, status);
582 if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
583 !test_bit(EC_FLAGS_NO_GPE, &ec->flags)) {
584 /* this is non-query, must be confirmation */
585 if (!test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
586 if (printk_ratelimit())
587 pr_info(PREFIX "non-query interrupt received,"
588 " switching to interrupt mode\n");
590 /* hush, STORM switches the mode every transaction */
591 pr_debug(PREFIX "non-query interrupt received,"
592 " switching to interrupt mode\n");
594 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
596 return ACPI_INTERRUPT_HANDLED;
599 /* --------------------------------------------------------------------------
600 Address Space Management
601 -------------------------------------------------------------------------- */
604 acpi_ec_space_handler(u32 function, acpi_physical_address address,
605 u32 bits, acpi_integer *value,
606 void *handler_context, void *region_context)
608 struct acpi_ec *ec = handler_context;
612 if ((address > 0xFF) || !value || !handler_context)
613 return AE_BAD_PARAMETER;
615 if (function != ACPI_READ && function != ACPI_WRITE)
616 return AE_BAD_PARAMETER;
618 if (bits != 8 && acpi_strict)
619 return AE_BAD_PARAMETER;
621 acpi_ec_burst_enable(ec);
623 if (function == ACPI_READ) {
624 result = acpi_ec_read(ec, address, &temp);
627 temp = 0xff & (*value);
628 result = acpi_ec_write(ec, address, temp);
631 for (i = 8; unlikely(bits - i > 0); i += 8) {
633 if (function == ACPI_READ) {
634 result = acpi_ec_read(ec, address, &temp);
635 (*value) |= ((acpi_integer)temp) << i;
637 temp = 0xff & ((*value) >> i);
638 result = acpi_ec_write(ec, address, temp);
642 acpi_ec_burst_disable(ec);
646 return AE_BAD_PARAMETER;
659 /* --------------------------------------------------------------------------
661 -------------------------------------------------------------------------- */
663 static struct proc_dir_entry *acpi_ec_dir;
665 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
667 struct acpi_ec *ec = seq->private;
672 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
673 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
674 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
675 seq_printf(seq, "use global lock:\t%s\n",
676 ec->global_lock ? "yes" : "no");
681 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
683 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
686 static const struct file_operations acpi_ec_info_ops = {
687 .open = acpi_ec_info_open_fs,
690 .release = single_release,
691 .owner = THIS_MODULE,
694 static int acpi_ec_add_fs(struct acpi_device *device)
696 struct proc_dir_entry *entry = NULL;
698 if (!acpi_device_dir(device)) {
699 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
701 if (!acpi_device_dir(device))
705 entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
706 acpi_device_dir(device),
707 &acpi_ec_info_ops, acpi_driver_data(device));
713 static int acpi_ec_remove_fs(struct acpi_device *device)
716 if (acpi_device_dir(device)) {
717 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
718 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
719 acpi_device_dir(device) = NULL;
725 /* --------------------------------------------------------------------------
727 -------------------------------------------------------------------------- */
729 ec_parse_io_ports(struct acpi_resource *resource, void *context);
731 static struct acpi_ec *make_acpi_ec(void)
733 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
736 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
737 mutex_init(&ec->lock);
738 init_waitqueue_head(&ec->wait);
739 INIT_LIST_HEAD(&ec->list);
740 spin_lock_init(&ec->curr_lock);
745 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
746 void *context, void **return_value)
749 struct acpi_buffer buffer = { sizeof(node_name), node_name };
750 struct acpi_ec *ec = context;
754 status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
756 if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
757 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
763 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
766 unsigned long long tmp = 0;
768 struct acpi_ec *ec = context;
770 /* clear addr values, ec_parse_io_ports depend on it */
771 ec->command_addr = ec->data_addr = 0;
773 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
774 ec_parse_io_ports, ec);
775 if (ACPI_FAILURE(status))
778 /* Get GPE bit assignment (EC events). */
779 /* TODO: Add support for _GPE returning a package */
780 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
781 if (ACPI_FAILURE(status))
784 /* Use the global lock for all EC transactions? */
786 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
787 ec->global_lock = tmp;
789 return AE_CTRL_TERMINATE;
792 static void ec_remove_handlers(struct acpi_ec *ec)
794 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
795 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
796 pr_err(PREFIX "failed to remove space handler\n");
797 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
798 &acpi_ec_gpe_handler)))
799 pr_err(PREFIX "failed to remove gpe handler\n");
800 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
803 static int acpi_ec_add(struct acpi_device *device)
805 struct acpi_ec *ec = NULL;
809 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
810 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
812 /* Check for boot EC */
814 (boot_ec->handle == device->handle ||
815 boot_ec->handle == ACPI_ROOT_OBJECT)) {
823 if (ec_parse_device(device->handle, 0, ec, NULL) !=
829 ec->handle = device->handle;
831 /* Find and register all query methods */
832 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
833 acpi_ec_register_query_methods, ec, NULL);
837 device->driver_data = ec;
838 acpi_ec_add_fs(device);
839 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
840 ec->gpe, ec->command_addr, ec->data_addr);
841 pr_info(PREFIX "driver started in %s mode\n",
842 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
846 static int acpi_ec_remove(struct acpi_device *device, int type)
849 struct acpi_ec_query_handler *handler, *tmp;
854 ec = acpi_driver_data(device);
855 mutex_lock(&ec->lock);
856 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
857 list_del(&handler->node);
860 mutex_unlock(&ec->lock);
861 acpi_ec_remove_fs(device);
862 device->driver_data = NULL;
870 ec_parse_io_ports(struct acpi_resource *resource, void *context)
872 struct acpi_ec *ec = context;
874 if (resource->type != ACPI_RESOURCE_TYPE_IO)
878 * The first address region returned is the data port, and
879 * the second address region returned is the status/command
882 if (ec->data_addr == 0)
883 ec->data_addr = resource->data.io.minimum;
884 else if (ec->command_addr == 0)
885 ec->command_addr = resource->data.io.minimum;
887 return AE_CTRL_TERMINATE;
892 static int ec_install_handlers(struct acpi_ec *ec)
895 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
897 status = acpi_install_gpe_handler(NULL, ec->gpe,
898 ACPI_GPE_EDGE_TRIGGERED,
899 &acpi_ec_gpe_handler, ec);
900 if (ACPI_FAILURE(status))
902 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
903 acpi_enable_gpe(NULL, ec->gpe);
904 status = acpi_install_address_space_handler(ec->handle,
906 &acpi_ec_space_handler,
908 if (ACPI_FAILURE(status)) {
909 if (status == AE_NOT_FOUND) {
911 * Maybe OS fails in evaluating the _REG object.
912 * The AE_NOT_FOUND error will be ignored and OS
913 * continue to initialize EC.
915 printk(KERN_ERR "Fail in evaluating the _REG object"
916 " of EC device. Broken bios is suspected.\n");
918 acpi_remove_gpe_handler(NULL, ec->gpe,
919 &acpi_ec_gpe_handler);
924 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
928 static int acpi_ec_start(struct acpi_device *device)
936 ec = acpi_driver_data(device);
941 ret = ec_install_handlers(ec);
943 /* EC is fully operational, allow queries */
944 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
948 static int acpi_ec_stop(struct acpi_device *device, int type)
953 ec = acpi_driver_data(device);
956 ec_remove_handlers(ec);
961 int __init acpi_boot_ec_enable(void)
963 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
965 if (!ec_install_handlers(boot_ec)) {
972 static const struct acpi_device_id ec_device_ids[] = {
977 int __init acpi_ec_ecdt_probe(void)
980 struct acpi_ec *saved_ec = NULL;
981 struct acpi_table_ecdt *ecdt_ptr;
983 boot_ec = make_acpi_ec();
987 * Generate a boot ec context
989 if (dmi_name_in_vendors("Micro-Star") ||
990 dmi_name_in_vendors("Notebook")) {
991 pr_info(PREFIX "Enabling special treatment for EC from MSI.\n");
994 status = acpi_get_table(ACPI_SIG_ECDT, 1,
995 (struct acpi_table_header **)&ecdt_ptr);
996 if (ACPI_SUCCESS(status)) {
997 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
998 boot_ec->command_addr = ecdt_ptr->control.address;
999 boot_ec->data_addr = ecdt_ptr->data.address;
1000 boot_ec->gpe = ecdt_ptr->gpe;
1001 boot_ec->handle = ACPI_ROOT_OBJECT;
1002 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
1003 /* Don't trust ECDT, which comes from ASUSTek */
1004 if (!dmi_name_in_vendors("ASUS") && EC_FLAGS_MSI == 0)
1006 saved_ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1009 memcpy(saved_ec, boot_ec, sizeof(struct acpi_ec));
1012 /* This workaround is needed only on some broken machines,
1013 * which require early EC, but fail to provide ECDT */
1014 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
1015 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
1017 /* Check that acpi_get_devices actually find something */
1018 if (ACPI_FAILURE(status) || !boot_ec->handle)
1021 /* try to find good ECDT from ASUSTek */
1022 if (saved_ec->command_addr != boot_ec->command_addr ||
1023 saved_ec->data_addr != boot_ec->data_addr ||
1024 saved_ec->gpe != boot_ec->gpe ||
1025 saved_ec->handle != boot_ec->handle)
1026 pr_info(PREFIX "ASUSTek keeps feeding us with broken "
1027 "ECDT tables, which are very hard to workaround. "
1028 "Trying to use DSDT EC info instead. Please send "
1029 "output of acpidump to linux-acpi@vger.kernel.org\n");
1033 /* We really need to limit this workaround, the only ASUS,
1034 * which needs it, has fake EC._INI method, so use it as flag.
1035 * Keep boot_ec struct as it will be needed soon.
1038 if (!dmi_name_in_vendors("ASUS") ||
1039 ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
1044 if (!ec_install_handlers(boot_ec)) {
1054 static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
1056 struct acpi_ec *ec = acpi_driver_data(device);
1057 /* Stop using GPE */
1058 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
1059 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
1060 acpi_disable_gpe(NULL, ec->gpe);
1064 static int acpi_ec_resume(struct acpi_device *device)
1066 struct acpi_ec *ec = acpi_driver_data(device);
1067 /* Enable use of GPE back */
1068 clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
1069 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
1070 acpi_enable_gpe(NULL, ec->gpe);
1074 static struct acpi_driver acpi_ec_driver = {
1076 .class = ACPI_EC_CLASS,
1077 .ids = ec_device_ids,
1080 .remove = acpi_ec_remove,
1081 .start = acpi_ec_start,
1082 .stop = acpi_ec_stop,
1083 .suspend = acpi_ec_suspend,
1084 .resume = acpi_ec_resume,
1088 int __init acpi_ec_init(void)
1092 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1096 /* Now register the driver for the EC */
1097 result = acpi_bus_register_driver(&acpi_ec_driver);
1099 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1106 /* EC driver currently not unloadable */
1108 static void __exit acpi_ec_exit(void)
1111 acpi_bus_unregister_driver(&acpi_ec_driver);
1113 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);