ACPI: EC: do transaction from interrupt context
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / ec.c
blob7f0d81c0ec99924ec4928fabad1b6e817e1a99a1
1 /*
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 */
30 /* #define DEBUG */
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>
42 #include <asm/io.h>
43 #include <acpi/acpi_bus.h>
44 #include <acpi/acpi_drivers.h>
45 #include <acpi/actypes.h>
47 #define ACPI_EC_CLASS "embedded_controller"
48 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
49 #define ACPI_EC_FILE_INFO "info"
51 #undef PREFIX
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 */
60 /* EC commands */
61 enum ec_command {
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_UDELAY 100 /* Wait 100us before polling EC again */
73 #define ACPI_EC_STORM_THRESHOLD 20 /* number of false interrupts
74 per one transaction */
76 enum {
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;
93 acpi_handle handle;
94 void *data;
95 u8 query_bit;
98 struct transaction_data {
99 const u8 *wdata;
100 u8 *rdata;
101 unsigned short irq_count;
102 u8 wlen;
103 u8 rlen;
106 static struct acpi_ec {
107 acpi_handle handle;
108 unsigned long gpe;
109 unsigned long command_addr;
110 unsigned long data_addr;
111 unsigned long global_lock;
112 unsigned long flags;
113 struct mutex lock;
114 wait_queue_head_t wait;
115 struct list_head list;
116 struct transaction_data *t;
117 spinlock_t t_lock;
118 } *boot_ec, *first_ec;
121 * Some Asus system have exchanged ECDT data/command IO addresses.
123 static int print_ecdt_error(const struct dmi_system_id *id)
125 printk(KERN_NOTICE PREFIX "%s detected - "
126 "ECDT has exchanged control/data I/O address\n",
127 id->ident);
128 return 0;
131 static struct dmi_system_id __cpuinitdata ec_dmi_table[] = {
133 print_ecdt_error, "Asus L4R", {
134 DMI_MATCH(DMI_BIOS_VERSION, "1008.006"),
135 DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),
136 DMI_MATCH(DMI_BOARD_NAME, "L4R") }, NULL},
138 print_ecdt_error, "Asus M6R", {
139 DMI_MATCH(DMI_BIOS_VERSION, "0207"),
140 DMI_MATCH(DMI_PRODUCT_NAME, "M6R"),
141 DMI_MATCH(DMI_BOARD_NAME, "M6R") }, NULL},
145 /* --------------------------------------------------------------------------
146 Transaction Management
147 -------------------------------------------------------------------------- */
149 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
151 u8 x = inb(ec->command_addr);
152 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
153 return x;
156 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
158 u8 x = inb(ec->data_addr);
159 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
160 return x;
163 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
165 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
166 outb(command, ec->command_addr);
169 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
171 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
172 outb(data, ec->data_addr);
175 static int ec_transaction_done(struct acpi_ec *ec)
177 unsigned long flags;
178 int ret = 0;
179 spin_lock_irqsave(&ec->t_lock, flags);
180 if (!ec->t || (!ec->t->wlen && !ec->t->rlen))
181 ret = 1;
182 spin_unlock_irqrestore(&ec->t_lock, flags);
183 return ret;
186 static void gpe_transaction(struct acpi_ec *ec, u8 status)
188 unsigned long flags;
189 spin_lock_irqsave(&ec->t_lock, flags);
190 if (!ec->t)
191 goto unlock;
192 if (ec->t->wlen > 0) {
193 if ((status & ACPI_EC_FLAG_IBF) == 0) {
194 acpi_ec_write_data(ec, *(ec->t->wdata++));
195 --ec->t->wlen;
196 } else
197 /* false interrupt, state didn't change */
198 ++ec->t->irq_count;
200 } else if (ec->t->rlen > 0) {
201 if ((status & ACPI_EC_FLAG_OBF) == 1) {
202 *(ec->t->rdata++) = acpi_ec_read_data(ec);
203 --ec->t->rlen;
204 } else
205 /* false interrupt, state didn't change */
206 ++ec->t->irq_count;
208 unlock:
209 spin_unlock_irqrestore(&ec->t_lock, flags);
212 static int acpi_ec_wait(struct acpi_ec *ec)
214 if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
215 msecs_to_jiffies(ACPI_EC_DELAY)))
216 return 0;
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);
223 return 1;
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);
235 return 0;
238 static int ec_poll(struct acpi_ec *ec)
240 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
241 msleep(1);
242 while (time_before(jiffies, delay)) {
243 gpe_transaction(ec, acpi_ec_read_status(ec));
244 msleep(1);
245 if (ec_transaction_done(ec))
246 return 0;
248 return -ETIME;
251 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
252 const u8 * wdata, unsigned wdata_len,
253 u8 * rdata, unsigned rdata_len,
254 int force_poll)
256 unsigned long tmp;
257 struct transaction_data t = {.wdata = wdata, .rdata = rdata,
258 .wlen = wdata_len, .rlen = rdata_len,
259 .irq_count = 0};
260 int ret = 0;
261 pr_debug(PREFIX "transaction start\n");
262 /* disable GPE during transaction if storm is detected */
263 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
264 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
265 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
267 /* start transaction */
268 spin_lock_irqsave(&ec->t_lock, tmp);
269 /* following two actions should be kept atomic */
270 ec->t = &t;
271 acpi_ec_write_cmd(ec, command);
272 if (command == ACPI_EC_COMMAND_QUERY)
273 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
274 spin_unlock_irqrestore(&ec->t_lock, tmp);
275 /* if we selected poll mode or failed in GPE-mode do a poll loop */
276 if (force_poll ||
277 !test_bit(EC_FLAGS_GPE_MODE, &ec->flags) ||
278 acpi_ec_wait(ec))
279 ret = ec_poll(ec);
280 pr_debug(PREFIX "transaction end\n");
281 spin_lock_irqsave(&ec->t_lock, tmp);
282 ec->t = NULL;
283 spin_unlock_irqrestore(&ec->t_lock, tmp);
284 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
285 /* check if we received SCI during transaction */
286 ec_check_sci(ec, acpi_ec_read_status(ec));
287 /* it is safe to enable GPE outside of transaction */
288 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
289 } else if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
290 t.irq_count > ACPI_EC_STORM_THRESHOLD) {
291 pr_debug(PREFIX "GPE storm detected\n");
292 set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
294 return ret;
297 static int ec_check_ibf0(struct acpi_ec *ec)
299 u8 status = acpi_ec_read_status(ec);
300 return (status & ACPI_EC_FLAG_IBF) == 0;
303 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
304 const u8 * wdata, unsigned wdata_len,
305 u8 * rdata, unsigned rdata_len,
306 int force_poll)
308 int status;
309 u32 glk;
310 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
311 return -EINVAL;
312 if (rdata)
313 memset(rdata, 0, rdata_len);
314 mutex_lock(&ec->lock);
315 if (ec->global_lock) {
316 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
317 if (ACPI_FAILURE(status)) {
318 status = -ENODEV;
319 goto unlock;
322 if (!wait_event_timeout(ec->wait, ec_check_ibf0(ec),
323 msecs_to_jiffies(ACPI_EC_DELAY))) {
324 pr_err(PREFIX "input buffer is not empty, "
325 "aborting transaction\n");
326 status = -ETIME;
327 goto end;
329 status = acpi_ec_transaction_unlocked(ec, command,
330 wdata, wdata_len,
331 rdata, rdata_len,
332 force_poll);
333 end:
334 if (ec->global_lock)
335 acpi_release_global_lock(glk);
336 unlock:
337 mutex_unlock(&ec->lock);
338 return status;
342 * Note: samsung nv5000 doesn't work with ec burst mode.
343 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
345 int acpi_ec_burst_enable(struct acpi_ec *ec)
347 u8 d;
348 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
351 int acpi_ec_burst_disable(struct acpi_ec *ec)
353 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
354 acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE,
355 NULL, 0, NULL, 0, 0) : 0;
358 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
360 int result;
361 u8 d;
363 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
364 &address, 1, &d, 1, 0);
365 *data = d;
366 return result;
369 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
371 u8 wdata[2] = { address, data };
372 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
373 wdata, 2, NULL, 0, 0);
377 * Externally callable EC access functions. For now, assume 1 EC only
379 int ec_burst_enable(void)
381 if (!first_ec)
382 return -ENODEV;
383 return acpi_ec_burst_enable(first_ec);
386 EXPORT_SYMBOL(ec_burst_enable);
388 int ec_burst_disable(void)
390 if (!first_ec)
391 return -ENODEV;
392 return acpi_ec_burst_disable(first_ec);
395 EXPORT_SYMBOL(ec_burst_disable);
397 int ec_read(u8 addr, u8 * val)
399 int err;
400 u8 temp_data;
402 if (!first_ec)
403 return -ENODEV;
405 err = acpi_ec_read(first_ec, addr, &temp_data);
407 if (!err) {
408 *val = temp_data;
409 return 0;
410 } else
411 return err;
414 EXPORT_SYMBOL(ec_read);
416 int ec_write(u8 addr, u8 val)
418 int err;
420 if (!first_ec)
421 return -ENODEV;
423 err = acpi_ec_write(first_ec, addr, val);
425 return err;
428 EXPORT_SYMBOL(ec_write);
430 int ec_transaction(u8 command,
431 const u8 * wdata, unsigned wdata_len,
432 u8 * rdata, unsigned rdata_len,
433 int force_poll)
435 if (!first_ec)
436 return -ENODEV;
438 return acpi_ec_transaction(first_ec, command, wdata,
439 wdata_len, rdata, rdata_len,
440 force_poll);
443 EXPORT_SYMBOL(ec_transaction);
445 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
447 int result;
448 u8 d;
450 if (!ec || !data)
451 return -EINVAL;
454 * Query the EC to find out which _Qxx method we need to evaluate.
455 * Note that successful completion of the query causes the ACPI_EC_SCI
456 * bit to be cleared (and thus clearing the interrupt source).
459 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
460 if (result)
461 return result;
463 if (!d)
464 return -ENODATA;
466 *data = d;
467 return 0;
470 /* --------------------------------------------------------------------------
471 Event Management
472 -------------------------------------------------------------------------- */
473 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
474 acpi_handle handle, acpi_ec_query_func func,
475 void *data)
477 struct acpi_ec_query_handler *handler =
478 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
479 if (!handler)
480 return -ENOMEM;
482 handler->query_bit = query_bit;
483 handler->handle = handle;
484 handler->func = func;
485 handler->data = data;
486 mutex_lock(&ec->lock);
487 list_add(&handler->node, &ec->list);
488 mutex_unlock(&ec->lock);
489 return 0;
492 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
494 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
496 struct acpi_ec_query_handler *handler, *tmp;
497 mutex_lock(&ec->lock);
498 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
499 if (query_bit == handler->query_bit) {
500 list_del(&handler->node);
501 kfree(handler);
504 mutex_unlock(&ec->lock);
507 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
509 static void acpi_ec_gpe_query(void *ec_cxt)
511 struct acpi_ec *ec = ec_cxt;
512 u8 value = 0;
513 struct acpi_ec_query_handler *handler, copy;
515 if (!ec || acpi_ec_query(ec, &value))
516 return;
517 mutex_lock(&ec->lock);
518 list_for_each_entry(handler, &ec->list, node) {
519 if (value == handler->query_bit) {
520 /* have custom handler for this bit */
521 memcpy(&copy, handler, sizeof(copy));
522 mutex_unlock(&ec->lock);
523 if (copy.func) {
524 copy.func(copy.data);
525 } else if (copy.handle) {
526 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
528 return;
531 mutex_unlock(&ec->lock);
534 static u32 acpi_ec_gpe_handler(void *data)
536 struct acpi_ec *ec = data;
537 u8 status;
539 pr_debug(PREFIX "~~~> interrupt\n");
540 status = acpi_ec_read_status(ec);
542 gpe_transaction(ec, status);
543 if (ec_transaction_done(ec) && (status & ACPI_EC_FLAG_IBF) == 0)
544 wake_up(&ec->wait);
546 ec_check_sci(ec, status);
547 if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
548 !test_bit(EC_FLAGS_NO_GPE, &ec->flags)) {
549 /* this is non-query, must be confirmation */
550 if (printk_ratelimit())
551 pr_info(PREFIX "non-query interrupt received,"
552 " switching to interrupt mode\n");
553 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
555 return ACPI_INTERRUPT_HANDLED;
558 /* --------------------------------------------------------------------------
559 Address Space Management
560 -------------------------------------------------------------------------- */
562 static acpi_status
563 acpi_ec_space_handler(u32 function, acpi_physical_address address,
564 u32 bits, acpi_integer *value,
565 void *handler_context, void *region_context)
567 struct acpi_ec *ec = handler_context;
568 int result = 0, i;
569 u8 temp = 0;
571 if ((address > 0xFF) || !value || !handler_context)
572 return AE_BAD_PARAMETER;
574 if (function != ACPI_READ && function != ACPI_WRITE)
575 return AE_BAD_PARAMETER;
577 if (bits != 8 && acpi_strict)
578 return AE_BAD_PARAMETER;
580 acpi_ec_burst_enable(ec);
582 if (function == ACPI_READ) {
583 result = acpi_ec_read(ec, address, &temp);
584 *value = temp;
585 } else {
586 temp = 0xff & (*value);
587 result = acpi_ec_write(ec, address, temp);
590 for (i = 8; unlikely(bits - i > 0); i += 8) {
591 ++address;
592 if (function == ACPI_READ) {
593 result = acpi_ec_read(ec, address, &temp);
594 (*value) |= ((acpi_integer)temp) << i;
595 } else {
596 temp = 0xff & ((*value) >> i);
597 result = acpi_ec_write(ec, address, temp);
601 acpi_ec_burst_disable(ec);
603 switch (result) {
604 case -EINVAL:
605 return AE_BAD_PARAMETER;
606 break;
607 case -ENODEV:
608 return AE_NOT_FOUND;
609 break;
610 case -ETIME:
611 return AE_TIME;
612 break;
613 default:
614 return AE_OK;
618 /* --------------------------------------------------------------------------
619 FS Interface (/proc)
620 -------------------------------------------------------------------------- */
622 static struct proc_dir_entry *acpi_ec_dir;
624 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
626 struct acpi_ec *ec = seq->private;
628 if (!ec)
629 goto end;
631 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
632 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
633 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
634 seq_printf(seq, "use global lock:\t%s\n",
635 ec->global_lock ? "yes" : "no");
636 end:
637 return 0;
640 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
642 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
645 static struct file_operations acpi_ec_info_ops = {
646 .open = acpi_ec_info_open_fs,
647 .read = seq_read,
648 .llseek = seq_lseek,
649 .release = single_release,
650 .owner = THIS_MODULE,
653 static int acpi_ec_add_fs(struct acpi_device *device)
655 struct proc_dir_entry *entry = NULL;
657 if (!acpi_device_dir(device)) {
658 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
659 acpi_ec_dir);
660 if (!acpi_device_dir(device))
661 return -ENODEV;
664 entry = proc_create_data(ACPI_EC_FILE_INFO, S_IRUGO,
665 acpi_device_dir(device),
666 &acpi_ec_info_ops, acpi_driver_data(device));
667 if (!entry)
668 return -ENODEV;
669 return 0;
672 static int acpi_ec_remove_fs(struct acpi_device *device)
675 if (acpi_device_dir(device)) {
676 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
677 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
678 acpi_device_dir(device) = NULL;
681 return 0;
684 /* --------------------------------------------------------------------------
685 Driver Interface
686 -------------------------------------------------------------------------- */
687 static acpi_status
688 ec_parse_io_ports(struct acpi_resource *resource, void *context);
690 static struct acpi_ec *make_acpi_ec(void)
692 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
693 if (!ec)
694 return NULL;
695 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
696 mutex_init(&ec->lock);
697 init_waitqueue_head(&ec->wait);
698 INIT_LIST_HEAD(&ec->list);
699 spin_lock_init(&ec->t_lock);
700 return ec;
703 static acpi_status
704 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
705 void *context, void **return_value)
707 struct acpi_namespace_node *node = handle;
708 struct acpi_ec *ec = context;
709 int value = 0;
710 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
711 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
713 return AE_OK;
716 static acpi_status
717 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
719 acpi_status status;
721 struct acpi_ec *ec = context;
722 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
723 ec_parse_io_ports, ec);
724 if (ACPI_FAILURE(status))
725 return status;
727 /* Get GPE bit assignment (EC events). */
728 /* TODO: Add support for _GPE returning a package */
729 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
730 if (ACPI_FAILURE(status))
731 return status;
732 /* Use the global lock for all EC transactions? */
733 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
734 ec->handle = handle;
735 return AE_CTRL_TERMINATE;
738 static void ec_remove_handlers(struct acpi_ec *ec)
740 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
741 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
742 pr_err(PREFIX "failed to remove space handler\n");
743 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
744 &acpi_ec_gpe_handler)))
745 pr_err(PREFIX "failed to remove gpe handler\n");
746 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
749 static int acpi_ec_add(struct acpi_device *device)
751 struct acpi_ec *ec = NULL;
753 if (!device)
754 return -EINVAL;
755 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
756 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
758 /* Check for boot EC */
759 if (boot_ec &&
760 (boot_ec->handle == device->handle ||
761 boot_ec->handle == ACPI_ROOT_OBJECT)) {
762 ec = boot_ec;
763 boot_ec = NULL;
764 } else {
765 ec = make_acpi_ec();
766 if (!ec)
767 return -ENOMEM;
768 if (ec_parse_device(device->handle, 0, ec, NULL) !=
769 AE_CTRL_TERMINATE) {
770 kfree(ec);
771 return -EINVAL;
775 ec->handle = device->handle;
777 /* Find and register all query methods */
778 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
779 acpi_ec_register_query_methods, ec, NULL);
781 if (!first_ec)
782 first_ec = ec;
783 acpi_driver_data(device) = ec;
784 acpi_ec_add_fs(device);
785 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
786 ec->gpe, ec->command_addr, ec->data_addr);
787 pr_info(PREFIX "driver started in %s mode\n",
788 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
789 return 0;
792 static int acpi_ec_remove(struct acpi_device *device, int type)
794 struct acpi_ec *ec;
795 struct acpi_ec_query_handler *handler, *tmp;
797 if (!device)
798 return -EINVAL;
800 ec = acpi_driver_data(device);
801 mutex_lock(&ec->lock);
802 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
803 list_del(&handler->node);
804 kfree(handler);
806 mutex_unlock(&ec->lock);
807 acpi_ec_remove_fs(device);
808 acpi_driver_data(device) = NULL;
809 if (ec == first_ec)
810 first_ec = NULL;
811 kfree(ec);
812 return 0;
815 static acpi_status
816 ec_parse_io_ports(struct acpi_resource *resource, void *context)
818 struct acpi_ec *ec = context;
820 if (resource->type != ACPI_RESOURCE_TYPE_IO)
821 return AE_OK;
824 * The first address region returned is the data port, and
825 * the second address region returned is the status/command
826 * port.
828 if (ec->data_addr == 0)
829 ec->data_addr = resource->data.io.minimum;
830 else if (ec->command_addr == 0)
831 ec->command_addr = resource->data.io.minimum;
832 else
833 return AE_CTRL_TERMINATE;
835 return AE_OK;
838 static int ec_install_handlers(struct acpi_ec *ec)
840 acpi_status status;
841 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
842 return 0;
843 status = acpi_install_gpe_handler(NULL, ec->gpe,
844 ACPI_GPE_EDGE_TRIGGERED,
845 &acpi_ec_gpe_handler, ec);
846 if (ACPI_FAILURE(status))
847 return -ENODEV;
848 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
849 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
850 status = acpi_install_address_space_handler(ec->handle,
851 ACPI_ADR_SPACE_EC,
852 &acpi_ec_space_handler,
853 NULL, ec);
854 if (ACPI_FAILURE(status)) {
855 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
856 return -ENODEV;
859 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
860 return 0;
863 static int acpi_ec_start(struct acpi_device *device)
865 struct acpi_ec *ec;
866 int ret = 0;
868 if (!device)
869 return -EINVAL;
871 ec = acpi_driver_data(device);
873 if (!ec)
874 return -EINVAL;
876 ret = ec_install_handlers(ec);
878 /* EC is fully operational, allow queries */
879 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
880 return ret;
883 static int acpi_ec_stop(struct acpi_device *device, int type)
885 struct acpi_ec *ec;
886 if (!device)
887 return -EINVAL;
888 ec = acpi_driver_data(device);
889 if (!ec)
890 return -EINVAL;
891 ec_remove_handlers(ec);
893 return 0;
896 int __init acpi_boot_ec_enable(void)
898 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
899 return 0;
900 if (!ec_install_handlers(boot_ec)) {
901 first_ec = boot_ec;
902 return 0;
904 return -EFAULT;
907 static const struct acpi_device_id ec_device_ids[] = {
908 {"PNP0C09", 0},
909 {"", 0},
912 int __init acpi_ec_ecdt_probe(void)
914 int ret;
915 acpi_status status;
916 struct acpi_table_ecdt *ecdt_ptr;
918 boot_ec = make_acpi_ec();
919 if (!boot_ec)
920 return -ENOMEM;
922 * Generate a boot ec context
924 status = acpi_get_table(ACPI_SIG_ECDT, 1,
925 (struct acpi_table_header **)&ecdt_ptr);
926 if (ACPI_SUCCESS(status)) {
927 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
928 boot_ec->command_addr = ecdt_ptr->control.address;
929 boot_ec->data_addr = ecdt_ptr->data.address;
930 if (dmi_check_system(ec_dmi_table)) {
932 * If the board falls into ec_dmi_table, it means
933 * that ECDT table gives the incorrect command/status
934 * & data I/O address. Just fix it.
936 boot_ec->data_addr = ecdt_ptr->control.address;
937 boot_ec->command_addr = ecdt_ptr->data.address;
939 boot_ec->gpe = ecdt_ptr->gpe;
940 boot_ec->handle = ACPI_ROOT_OBJECT;
941 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
942 } else {
943 /* This workaround is needed only on some broken machines,
944 * which require early EC, but fail to provide ECDT */
945 acpi_handle x;
946 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
947 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
948 boot_ec, NULL);
949 /* Check that acpi_get_devices actually find something */
950 if (ACPI_FAILURE(status) || !boot_ec->handle)
951 goto error;
952 /* We really need to limit this workaround, the only ASUS,
953 * which needs it, has fake EC._INI method, so use it as flag.
954 * Keep boot_ec struct as it will be needed soon.
956 if (ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI", &x)))
957 return -ENODEV;
960 ret = ec_install_handlers(boot_ec);
961 if (!ret) {
962 first_ec = boot_ec;
963 return 0;
965 error:
966 kfree(boot_ec);
967 boot_ec = NULL;
968 return -ENODEV;
971 static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
973 struct acpi_ec *ec = acpi_driver_data(device);
974 /* Stop using GPE */
975 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
976 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
977 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
978 return 0;
981 static int acpi_ec_resume(struct acpi_device *device)
983 struct acpi_ec *ec = acpi_driver_data(device);
984 /* Enable use of GPE back */
985 clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
986 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
987 return 0;
990 static struct acpi_driver acpi_ec_driver = {
991 .name = "ec",
992 .class = ACPI_EC_CLASS,
993 .ids = ec_device_ids,
994 .ops = {
995 .add = acpi_ec_add,
996 .remove = acpi_ec_remove,
997 .start = acpi_ec_start,
998 .stop = acpi_ec_stop,
999 .suspend = acpi_ec_suspend,
1000 .resume = acpi_ec_resume,
1004 static int __init acpi_ec_init(void)
1006 int result = 0;
1008 if (acpi_disabled)
1009 return 0;
1011 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1012 if (!acpi_ec_dir)
1013 return -ENODEV;
1015 /* Now register the driver for the EC */
1016 result = acpi_bus_register_driver(&acpi_ec_driver);
1017 if (result < 0) {
1018 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1019 return -ENODEV;
1022 return result;
1025 subsys_initcall(acpi_ec_init);
1027 /* EC driver currently not unloadable */
1028 #if 0
1029 static void __exit acpi_ec_exit(void)
1032 acpi_bus_unregister_driver(&acpi_ec_driver);
1034 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1036 return;
1038 #endif /* 0 */