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>
46 #define ACPI_EC_CLASS "embedded_controller"
47 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
48 #define ACPI_EC_FILE_INFO "info"
50 #define PREFIX "ACPI: EC: "
52 /* EC status register */
53 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
54 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
55 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
56 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
60 ACPI_EC_COMMAND_READ = 0x80,
61 ACPI_EC_COMMAND_WRITE = 0x81,
62 ACPI_EC_BURST_ENABLE = 0x82,
63 ACPI_EC_BURST_DISABLE = 0x83,
64 ACPI_EC_COMMAND_QUERY = 0x84,
67 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
68 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
69 #define ACPI_EC_CDELAY 10 /* Wait 10us before polling EC */
71 #define ACPI_EC_STORM_THRESHOLD 8 /* number of false interrupts
72 per one transaction */
75 EC_FLAGS_QUERY_PENDING, /* Query is pending */
76 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent
77 * for status change */
78 EC_FLAGS_NO_GPE, /* Don't use GPE mode */
79 EC_FLAGS_GPE_STORM, /* GPE storm detected */
80 EC_FLAGS_HANDLERS_INSTALLED /* Handlers for GPE and
81 * OpReg are installed */
84 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
85 /* External interfaces use first EC only, so remember */
86 typedef int (*acpi_ec_query_func) (void *data);
88 struct acpi_ec_query_handler {
89 struct list_head node;
90 acpi_ec_query_func func;
99 unsigned short irq_count;
108 static struct acpi_ec {
111 unsigned long command_addr;
112 unsigned long data_addr;
113 unsigned long global_lock;
116 wait_queue_head_t wait;
117 struct list_head list;
118 struct transaction *curr;
119 spinlock_t curr_lock;
120 } *boot_ec, *first_ec;
122 static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
124 /* --------------------------------------------------------------------------
125 Transaction Management
126 -------------------------------------------------------------------------- */
128 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
130 u8 x = inb(ec->command_addr);
131 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
135 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
137 u8 x = inb(ec->data_addr);
138 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
142 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
144 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
145 outb(command, ec->command_addr);
148 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
150 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
151 outb(data, ec->data_addr);
154 static int ec_transaction_done(struct acpi_ec *ec)
158 spin_lock_irqsave(&ec->curr_lock, flags);
159 if (!ec->curr || ec->curr->done)
161 spin_unlock_irqrestore(&ec->curr_lock, flags);
165 static void start_transaction(struct acpi_ec *ec)
167 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
168 ec->curr->done = false;
169 acpi_ec_write_cmd(ec, ec->curr->command);
172 static void gpe_transaction(struct acpi_ec *ec, u8 status)
175 spin_lock_irqsave(&ec->curr_lock, flags);
178 if (ec->curr->wlen > ec->curr->wi) {
179 if ((status & ACPI_EC_FLAG_IBF) == 0)
180 acpi_ec_write_data(ec,
181 ec->curr->wdata[ec->curr->wi++]);
184 } else if (ec->curr->rlen > ec->curr->ri) {
185 if ((status & ACPI_EC_FLAG_OBF) == 1) {
186 ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
187 if (ec->curr->rlen == ec->curr->ri)
188 ec->curr->done = true;
191 } else if (ec->curr->wlen == ec->curr->wi &&
192 (status & ACPI_EC_FLAG_IBF) == 0)
193 ec->curr->done = true;
196 /* false interrupt, state didn't change */
198 ++ec->curr->irq_count;
200 spin_unlock_irqrestore(&ec->curr_lock, flags);
203 static int acpi_ec_wait(struct acpi_ec *ec)
205 if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
206 msecs_to_jiffies(ACPI_EC_DELAY)))
208 /* try restart command if we get any false interrupts */
209 if (ec->curr->irq_count &&
210 (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
211 pr_debug(PREFIX "controller reset, restart transaction\n");
212 start_transaction(ec);
213 if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
214 msecs_to_jiffies(ACPI_EC_DELAY)))
217 /* missing GPEs, switch back to poll mode */
218 if (printk_ratelimit())
219 pr_info(PREFIX "missing confirmations, "
220 "switch off interrupt mode.\n");
221 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
222 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
226 static void acpi_ec_gpe_query(void *ec_cxt);
228 static int ec_check_sci(struct acpi_ec *ec, u8 state)
230 if (state & ACPI_EC_FLAG_SCI) {
231 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
232 return acpi_os_execute(OSL_EC_BURST_HANDLER,
233 acpi_ec_gpe_query, ec);
238 static void ec_delay(void)
240 /* EC in MSI notebooks don't tolerate delays other than 550 usec */
242 udelay(ACPI_EC_DELAY);
244 /* Use shortest sleep available */
248 static int ec_poll(struct acpi_ec *ec)
250 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
251 udelay(ACPI_EC_CDELAY);
252 while (time_before(jiffies, delay)) {
253 gpe_transaction(ec, acpi_ec_read_status(ec));
255 if (ec_transaction_done(ec))
261 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
262 struct transaction *t,
267 pr_debug(PREFIX "transaction start\n");
268 /* disable GPE during transaction if storm is detected */
269 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
270 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
271 acpi_disable_gpe(NULL, ec->gpe);
274 udelay(ACPI_EC_DELAY);
275 /* start transaction */
276 spin_lock_irqsave(&ec->curr_lock, tmp);
277 /* following two actions should be kept atomic */
279 start_transaction(ec);
280 if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
281 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
282 spin_unlock_irqrestore(&ec->curr_lock, tmp);
283 /* if we selected poll mode or failed in GPE-mode do a poll loop */
285 !test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ||
288 pr_debug(PREFIX "transaction end\n");
289 spin_lock_irqsave(&ec->curr_lock, tmp);
291 spin_unlock_irqrestore(&ec->curr_lock, tmp);
292 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
293 /* check if we received SCI during transaction */
294 ec_check_sci(ec, acpi_ec_read_status(ec));
295 /* it is safe to enable GPE outside of transaction */
296 acpi_enable_gpe(NULL, ec->gpe);
297 } else if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
298 t->irq_count > ACPI_EC_STORM_THRESHOLD) {
299 pr_info(PREFIX "GPE storm detected, "
300 "transactions will use polling mode\n");
301 set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
306 static int ec_check_ibf0(struct acpi_ec *ec)
308 u8 status = acpi_ec_read_status(ec);
309 return (status & ACPI_EC_FLAG_IBF) == 0;
312 static int ec_wait_ibf0(struct acpi_ec *ec)
314 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
315 /* interrupt wait manually if GPE mode is not active */
316 unsigned long timeout = test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ?
317 msecs_to_jiffies(ACPI_EC_DELAY) : msecs_to_jiffies(1);
318 while (time_before(jiffies, delay))
319 if (wait_event_timeout(ec->wait, ec_check_ibf0(ec), timeout))
324 static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t,
329 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
332 memset(t->rdata, 0, t->rlen);
333 mutex_lock(&ec->lock);
334 if (ec->global_lock) {
335 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
336 if (ACPI_FAILURE(status)) {
341 if (ec_wait_ibf0(ec)) {
342 pr_err(PREFIX "input buffer is not empty, "
343 "aborting transaction\n");
347 status = acpi_ec_transaction_unlocked(ec, t, force_poll);
350 acpi_release_global_lock(glk);
352 mutex_unlock(&ec->lock);
357 * Note: samsung nv5000 doesn't work with ec burst mode.
358 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
360 static int acpi_ec_burst_enable(struct acpi_ec *ec)
363 struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
364 .wdata = NULL, .rdata = &d,
365 .wlen = 0, .rlen = 1};
367 return acpi_ec_transaction(ec, &t, 0);
370 static int acpi_ec_burst_disable(struct acpi_ec *ec)
372 struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
373 .wdata = NULL, .rdata = NULL,
374 .wlen = 0, .rlen = 0};
376 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
377 acpi_ec_transaction(ec, &t, 0) : 0;
380 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
384 struct transaction t = {.command = ACPI_EC_COMMAND_READ,
385 .wdata = &address, .rdata = &d,
386 .wlen = 1, .rlen = 1};
388 result = acpi_ec_transaction(ec, &t, 0);
393 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
395 u8 wdata[2] = { address, data };
396 struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
397 .wdata = wdata, .rdata = NULL,
398 .wlen = 2, .rlen = 0};
400 return acpi_ec_transaction(ec, &t, 0);
404 * Externally callable EC access functions. For now, assume 1 EC only
406 int ec_burst_enable(void)
410 return acpi_ec_burst_enable(first_ec);
413 EXPORT_SYMBOL(ec_burst_enable);
415 int ec_burst_disable(void)
419 return acpi_ec_burst_disable(first_ec);
422 EXPORT_SYMBOL(ec_burst_disable);
424 int ec_read(u8 addr, u8 * val)
432 err = acpi_ec_read(first_ec, addr, &temp_data);
441 EXPORT_SYMBOL(ec_read);
443 int ec_write(u8 addr, u8 val)
450 err = acpi_ec_write(first_ec, addr, val);
455 EXPORT_SYMBOL(ec_write);
457 int ec_transaction(u8 command,
458 const u8 * wdata, unsigned wdata_len,
459 u8 * rdata, unsigned rdata_len,
462 struct transaction t = {.command = command,
463 .wdata = wdata, .rdata = rdata,
464 .wlen = wdata_len, .rlen = rdata_len};
468 return acpi_ec_transaction(first_ec, &t, force_poll);
471 EXPORT_SYMBOL(ec_transaction);
473 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
477 struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
478 .wdata = NULL, .rdata = &d,
479 .wlen = 0, .rlen = 1};
484 * Query the EC to find out which _Qxx method we need to evaluate.
485 * Note that successful completion of the query causes the ACPI_EC_SCI
486 * bit to be cleared (and thus clearing the interrupt source).
489 result = acpi_ec_transaction(ec, &t, 0);
500 /* --------------------------------------------------------------------------
502 -------------------------------------------------------------------------- */
503 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
504 acpi_handle handle, acpi_ec_query_func func,
507 struct acpi_ec_query_handler *handler =
508 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
512 handler->query_bit = query_bit;
513 handler->handle = handle;
514 handler->func = func;
515 handler->data = data;
516 mutex_lock(&ec->lock);
517 list_add(&handler->node, &ec->list);
518 mutex_unlock(&ec->lock);
522 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
524 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
526 struct acpi_ec_query_handler *handler, *tmp;
527 mutex_lock(&ec->lock);
528 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
529 if (query_bit == handler->query_bit) {
530 list_del(&handler->node);
534 mutex_unlock(&ec->lock);
537 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
539 static void acpi_ec_gpe_query(void *ec_cxt)
541 struct acpi_ec *ec = ec_cxt;
543 struct acpi_ec_query_handler *handler, copy;
545 if (!ec || acpi_ec_query(ec, &value))
547 mutex_lock(&ec->lock);
548 list_for_each_entry(handler, &ec->list, node) {
549 if (value == handler->query_bit) {
550 /* have custom handler for this bit */
551 memcpy(©, handler, sizeof(copy));
552 mutex_unlock(&ec->lock);
554 copy.func(copy.data);
555 } else if (copy.handle) {
556 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
561 mutex_unlock(&ec->lock);
564 static u32 acpi_ec_gpe_handler(void *data)
566 struct acpi_ec *ec = data;
569 pr_debug(PREFIX "~~~> interrupt\n");
570 status = acpi_ec_read_status(ec);
572 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) {
573 gpe_transaction(ec, status);
574 if (ec_transaction_done(ec) &&
575 (status & ACPI_EC_FLAG_IBF) == 0)
579 ec_check_sci(ec, status);
580 if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
581 !test_bit(EC_FLAGS_NO_GPE, &ec->flags)) {
582 /* this is non-query, must be confirmation */
583 if (!test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
584 if (printk_ratelimit())
585 pr_info(PREFIX "non-query interrupt received,"
586 " switching to interrupt mode\n");
588 /* hush, STORM switches the mode every transaction */
589 pr_debug(PREFIX "non-query interrupt received,"
590 " switching to interrupt mode\n");
592 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
594 return ACPI_INTERRUPT_HANDLED;
597 /* --------------------------------------------------------------------------
598 Address Space Management
599 -------------------------------------------------------------------------- */
602 acpi_ec_space_handler(u32 function, acpi_physical_address address,
603 u32 bits, acpi_integer *value,
604 void *handler_context, void *region_context)
606 struct acpi_ec *ec = handler_context;
610 if ((address > 0xFF) || !value || !handler_context)
611 return AE_BAD_PARAMETER;
613 if (function != ACPI_READ && function != ACPI_WRITE)
614 return AE_BAD_PARAMETER;
616 if (bits != 8 && acpi_strict)
617 return AE_BAD_PARAMETER;
619 acpi_ec_burst_enable(ec);
621 if (function == ACPI_READ) {
622 result = acpi_ec_read(ec, address, &temp);
625 temp = 0xff & (*value);
626 result = acpi_ec_write(ec, address, temp);
629 for (i = 8; unlikely(bits - i > 0); i += 8) {
631 if (function == ACPI_READ) {
632 result = acpi_ec_read(ec, address, &temp);
633 (*value) |= ((acpi_integer)temp) << i;
635 temp = 0xff & ((*value) >> i);
636 result = acpi_ec_write(ec, address, temp);
640 acpi_ec_burst_disable(ec);
644 return AE_BAD_PARAMETER;
657 /* --------------------------------------------------------------------------
659 -------------------------------------------------------------------------- */
661 static struct proc_dir_entry *acpi_ec_dir;
663 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
665 struct acpi_ec *ec = seq->private;
670 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
671 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
672 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
673 seq_printf(seq, "use global lock:\t%s\n",
674 ec->global_lock ? "yes" : "no");
679 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
681 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
684 static const struct file_operations acpi_ec_info_ops = {
685 .open = acpi_ec_info_open_fs,
688 .release = single_release,
689 .owner = THIS_MODULE,
692 static int acpi_ec_add_fs(struct acpi_device *device)
694 struct proc_dir_entry *entry = NULL;
696 if (!acpi_device_dir(device)) {
697 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
699 if (!acpi_device_dir(device))
703 entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
704 acpi_device_dir(device),
705 &acpi_ec_info_ops, acpi_driver_data(device));
711 static int acpi_ec_remove_fs(struct acpi_device *device)
714 if (acpi_device_dir(device)) {
715 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
716 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
717 acpi_device_dir(device) = NULL;
723 /* --------------------------------------------------------------------------
725 -------------------------------------------------------------------------- */
727 ec_parse_io_ports(struct acpi_resource *resource, void *context);
729 static struct acpi_ec *make_acpi_ec(void)
731 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
734 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
735 mutex_init(&ec->lock);
736 init_waitqueue_head(&ec->wait);
737 INIT_LIST_HEAD(&ec->list);
738 spin_lock_init(&ec->curr_lock);
743 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
744 void *context, void **return_value)
747 struct acpi_buffer buffer = { sizeof(node_name), node_name };
748 struct acpi_ec *ec = context;
752 status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
754 if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
755 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
761 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
764 unsigned long long tmp = 0;
766 struct acpi_ec *ec = context;
768 /* clear addr values, ec_parse_io_ports depend on it */
769 ec->command_addr = ec->data_addr = 0;
771 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
772 ec_parse_io_ports, ec);
773 if (ACPI_FAILURE(status))
776 /* Get GPE bit assignment (EC events). */
777 /* TODO: Add support for _GPE returning a package */
778 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
779 if (ACPI_FAILURE(status))
782 /* Use the global lock for all EC transactions? */
784 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
785 ec->global_lock = tmp;
787 return AE_CTRL_TERMINATE;
790 static int ec_install_handlers(struct acpi_ec *ec)
793 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
795 status = acpi_install_gpe_handler(NULL, ec->gpe,
796 ACPI_GPE_EDGE_TRIGGERED,
797 &acpi_ec_gpe_handler, ec);
798 if (ACPI_FAILURE(status))
800 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
801 acpi_enable_gpe(NULL, ec->gpe);
802 status = acpi_install_address_space_handler(ec->handle,
804 &acpi_ec_space_handler,
806 if (ACPI_FAILURE(status)) {
807 if (status == AE_NOT_FOUND) {
809 * Maybe OS fails in evaluating the _REG object.
810 * The AE_NOT_FOUND error will be ignored and OS
811 * continue to initialize EC.
813 printk(KERN_ERR "Fail in evaluating the _REG object"
814 " of EC device. Broken bios is suspected.\n");
816 acpi_remove_gpe_handler(NULL, ec->gpe,
817 &acpi_ec_gpe_handler);
822 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
826 static void ec_remove_handlers(struct acpi_ec *ec)
828 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
829 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
830 pr_err(PREFIX "failed to remove space handler\n");
831 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
832 &acpi_ec_gpe_handler)))
833 pr_err(PREFIX "failed to remove gpe handler\n");
834 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
837 static int acpi_ec_add(struct acpi_device *device)
839 struct acpi_ec *ec = NULL;
842 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
843 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
845 /* Check for boot EC */
847 (boot_ec->handle == device->handle ||
848 boot_ec->handle == ACPI_ROOT_OBJECT)) {
856 if (ec_parse_device(device->handle, 0, ec, NULL) !=
862 ec->handle = device->handle;
864 /* Find and register all query methods */
865 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
866 acpi_ec_register_query_methods, ec, NULL);
870 device->driver_data = ec;
871 acpi_ec_add_fs(device);
872 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
873 ec->gpe, ec->command_addr, ec->data_addr);
874 pr_info(PREFIX "driver started in %s mode\n",
875 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
877 ret = ec_install_handlers(ec);
879 /* EC is fully operational, allow queries */
880 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
884 static int acpi_ec_remove(struct acpi_device *device, int type)
887 struct acpi_ec_query_handler *handler, *tmp;
892 ec = acpi_driver_data(device);
893 ec_remove_handlers(ec);
894 mutex_lock(&ec->lock);
895 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
896 list_del(&handler->node);
899 mutex_unlock(&ec->lock);
900 acpi_ec_remove_fs(device);
901 device->driver_data = NULL;
909 ec_parse_io_ports(struct acpi_resource *resource, void *context)
911 struct acpi_ec *ec = context;
913 if (resource->type != ACPI_RESOURCE_TYPE_IO)
917 * The first address region returned is the data port, and
918 * the second address region returned is the status/command
921 if (ec->data_addr == 0)
922 ec->data_addr = resource->data.io.minimum;
923 else if (ec->command_addr == 0)
924 ec->command_addr = resource->data.io.minimum;
926 return AE_CTRL_TERMINATE;
931 int __init acpi_boot_ec_enable(void)
933 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
935 if (!ec_install_handlers(boot_ec)) {
942 static const struct acpi_device_id ec_device_ids[] = {
947 int __init acpi_ec_ecdt_probe(void)
950 struct acpi_ec *saved_ec = NULL;
951 struct acpi_table_ecdt *ecdt_ptr;
953 boot_ec = make_acpi_ec();
957 * Generate a boot ec context
959 if (dmi_name_in_vendors("Micro-Star") ||
960 dmi_name_in_vendors("Notebook")) {
961 pr_info(PREFIX "Enabling special treatment for EC from MSI.\n");
964 status = acpi_get_table(ACPI_SIG_ECDT, 1,
965 (struct acpi_table_header **)&ecdt_ptr);
966 if (ACPI_SUCCESS(status)) {
967 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
968 boot_ec->command_addr = ecdt_ptr->control.address;
969 boot_ec->data_addr = ecdt_ptr->data.address;
970 boot_ec->gpe = ecdt_ptr->gpe;
971 boot_ec->handle = ACPI_ROOT_OBJECT;
972 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
973 /* Don't trust ECDT, which comes from ASUSTek */
974 if (!dmi_name_in_vendors("ASUS") && EC_FLAGS_MSI == 0)
976 saved_ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
979 memcpy(saved_ec, boot_ec, sizeof(struct acpi_ec));
982 /* This workaround is needed only on some broken machines,
983 * which require early EC, but fail to provide ECDT */
984 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
985 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
987 /* Check that acpi_get_devices actually find something */
988 if (ACPI_FAILURE(status) || !boot_ec->handle)
991 /* try to find good ECDT from ASUSTek */
992 if (saved_ec->command_addr != boot_ec->command_addr ||
993 saved_ec->data_addr != boot_ec->data_addr ||
994 saved_ec->gpe != boot_ec->gpe ||
995 saved_ec->handle != boot_ec->handle)
996 pr_info(PREFIX "ASUSTek keeps feeding us with broken "
997 "ECDT tables, which are very hard to workaround. "
998 "Trying to use DSDT EC info instead. Please send "
999 "output of acpidump to linux-acpi@vger.kernel.org\n");
1003 /* We really need to limit this workaround, the only ASUS,
1004 * which needs it, has fake EC._INI method, so use it as flag.
1005 * Keep boot_ec struct as it will be needed soon.
1008 if (!dmi_name_in_vendors("ASUS") ||
1009 ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
1014 if (!ec_install_handlers(boot_ec)) {
1024 static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
1026 struct acpi_ec *ec = acpi_driver_data(device);
1027 /* Stop using GPE */
1028 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
1029 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
1030 acpi_disable_gpe(NULL, ec->gpe);
1034 static int acpi_ec_resume(struct acpi_device *device)
1036 struct acpi_ec *ec = acpi_driver_data(device);
1037 /* Enable use of GPE back */
1038 clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
1039 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
1040 acpi_enable_gpe(NULL, ec->gpe);
1044 static struct acpi_driver acpi_ec_driver = {
1046 .class = ACPI_EC_CLASS,
1047 .ids = ec_device_ids,
1050 .remove = acpi_ec_remove,
1051 .suspend = acpi_ec_suspend,
1052 .resume = acpi_ec_resume,
1056 int __init acpi_ec_init(void)
1060 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1064 /* Now register the driver for the EC */
1065 result = acpi_bus_register_driver(&acpi_ec_driver);
1067 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1074 /* EC driver currently not unloadable */
1076 static void __exit acpi_ec_exit(void)
1079 acpi_bus_unregister_driver(&acpi_ec_driver);
1081 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);