Linux 3.12.49
[linux/fpc-iii.git] / drivers / acpi / ec.c
blob85752c668473fa45f125125cd1517bc69029b22a
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/interrupt.h>
38 #include <linux/list.h>
39 #include <linux/spinlock.h>
40 #include <linux/slab.h>
41 #include <asm/io.h>
42 #include <acpi/acpi_bus.h>
43 #include <acpi/acpi_drivers.h>
44 #include <linux/dmi.h>
46 #include "internal.h"
48 #define ACPI_EC_CLASS "embedded_controller"
49 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
50 #define ACPI_EC_FILE_INFO "info"
52 #undef PREFIX
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 */
61 /* EC commands */
62 enum ec_command {
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 */
76 enum {
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;
108 acpi_handle handle;
109 void *data;
110 u8 query_bit;
113 struct transaction {
114 const u8 *wdata;
115 u8 *rdata;
116 unsigned short irq_count;
117 u8 command;
118 u8 wi;
119 u8 ri;
120 u8 wlen;
121 u8 rlen;
122 u8 flags;
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 */
132 static int EC_FLAGS_QUERY_HANDSHAKE; /* Needs QR_EC issued when SCI_EVT set */
134 /* --------------------------------------------------------------------------
135 Transaction Management
136 -------------------------------------------------------------------------- */
138 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
140 u8 x = inb(ec->command_addr);
141 pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
142 return x;
145 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
147 u8 x = inb(ec->data_addr);
148 pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
149 return x;
152 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
154 pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
155 outb(command, ec->command_addr);
158 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
160 pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
161 outb(data, ec->data_addr);
164 static int ec_transaction_completed(struct acpi_ec *ec)
166 unsigned long flags;
167 int ret = 0;
168 spin_lock_irqsave(&ec->lock, flags);
169 if (ec->curr && (ec->curr->flags & ACPI_EC_COMMAND_COMPLETE))
170 ret = 1;
171 spin_unlock_irqrestore(&ec->lock, flags);
172 return ret;
175 static bool advance_transaction(struct acpi_ec *ec)
177 struct transaction *t;
178 u8 status;
179 bool wakeup = false;
181 pr_debug("===== %s =====\n", in_interrupt() ? "IRQ" : "TASK");
182 status = acpi_ec_read_status(ec);
183 t = ec->curr;
184 if (!t)
185 goto err;
186 if (t->flags & ACPI_EC_COMMAND_POLL) {
187 if (t->wlen > t->wi) {
188 if ((status & ACPI_EC_FLAG_IBF) == 0)
189 acpi_ec_write_data(ec, t->wdata[t->wi++]);
190 else
191 goto err;
192 } else if (t->rlen > t->ri) {
193 if ((status & ACPI_EC_FLAG_OBF) == 1) {
194 t->rdata[t->ri++] = acpi_ec_read_data(ec);
195 if (t->rlen == t->ri) {
196 t->flags |= ACPI_EC_COMMAND_COMPLETE;
197 if (t->command == ACPI_EC_COMMAND_QUERY)
198 pr_debug("hardware QR_EC completion\n");
199 wakeup = true;
201 } else
202 goto err;
203 } else if (t->wlen == t->wi &&
204 (status & ACPI_EC_FLAG_IBF) == 0) {
205 t->flags |= ACPI_EC_COMMAND_COMPLETE;
206 wakeup = true;
208 return wakeup;
209 } else {
210 if (EC_FLAGS_QUERY_HANDSHAKE &&
211 !(status & ACPI_EC_FLAG_SCI) &&
212 (t->command == ACPI_EC_COMMAND_QUERY)) {
213 t->flags |= ACPI_EC_COMMAND_POLL;
214 t->rdata[t->ri++] = 0x00;
215 t->flags |= ACPI_EC_COMMAND_COMPLETE;
216 pr_debug("software QR_EC completion\n");
217 wakeup = true;
218 } else if ((status & ACPI_EC_FLAG_IBF) == 0) {
219 acpi_ec_write_cmd(ec, t->command);
220 t->flags |= ACPI_EC_COMMAND_POLL;
221 } else
222 goto err;
223 return wakeup;
225 err:
227 * If SCI bit is set, then don't think it's a false IRQ
228 * otherwise will take a not handled IRQ as a false one.
230 if (!(status & ACPI_EC_FLAG_SCI)) {
231 if (in_interrupt() && t)
232 ++t->irq_count;
234 return wakeup;
237 static void start_transaction(struct acpi_ec *ec)
239 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
240 ec->curr->flags = 0;
241 (void)advance_transaction(ec);
244 static int acpi_ec_sync_query(struct acpi_ec *ec, u8 *data);
246 static int ec_check_sci_sync(struct acpi_ec *ec, u8 state)
248 if (state & ACPI_EC_FLAG_SCI) {
249 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
250 return acpi_ec_sync_query(ec, NULL);
252 return 0;
255 static int ec_poll(struct acpi_ec *ec)
257 unsigned long flags;
258 int repeat = 5; /* number of command restarts */
259 while (repeat--) {
260 unsigned long delay = jiffies +
261 msecs_to_jiffies(ec_delay);
262 do {
263 /* don't sleep with disabled interrupts */
264 if (EC_FLAGS_MSI || irqs_disabled()) {
265 udelay(ACPI_EC_MSI_UDELAY);
266 if (ec_transaction_completed(ec))
267 return 0;
268 } else {
269 if (wait_event_timeout(ec->wait,
270 ec_transaction_completed(ec),
271 msecs_to_jiffies(1)))
272 return 0;
274 spin_lock_irqsave(&ec->lock, flags);
275 (void)advance_transaction(ec);
276 spin_unlock_irqrestore(&ec->lock, flags);
277 } while (time_before(jiffies, delay));
278 pr_debug(PREFIX "controller reset, restart transaction\n");
279 spin_lock_irqsave(&ec->lock, flags);
280 start_transaction(ec);
281 spin_unlock_irqrestore(&ec->lock, flags);
283 return -ETIME;
286 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
287 struct transaction *t)
289 unsigned long tmp;
290 int ret = 0;
291 if (EC_FLAGS_MSI)
292 udelay(ACPI_EC_MSI_UDELAY);
293 /* start transaction */
294 spin_lock_irqsave(&ec->lock, tmp);
295 /* following two actions should be kept atomic */
296 ec->curr = t;
297 start_transaction(ec);
298 if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
299 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
300 spin_unlock_irqrestore(&ec->lock, tmp);
301 ret = ec_poll(ec);
302 spin_lock_irqsave(&ec->lock, tmp);
303 ec->curr = NULL;
304 spin_unlock_irqrestore(&ec->lock, tmp);
305 return ret;
308 static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
310 int status;
311 u32 glk;
312 if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
313 return -EINVAL;
314 if (t->rdata)
315 memset(t->rdata, 0, t->rlen);
316 mutex_lock(&ec->mutex);
317 if (test_bit(EC_FLAGS_BLOCKED, &ec->flags)) {
318 status = -EINVAL;
319 goto unlock;
321 if (ec->global_lock) {
322 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
323 if (ACPI_FAILURE(status)) {
324 status = -ENODEV;
325 goto unlock;
328 pr_debug(PREFIX "transaction start (cmd=0x%02x, addr=0x%02x)\n",
329 t->command, t->wdata ? t->wdata[0] : 0);
330 /* disable GPE during transaction if storm is detected */
331 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
332 /* It has to be disabled, so that it doesn't trigger. */
333 acpi_disable_gpe(NULL, ec->gpe);
336 status = acpi_ec_transaction_unlocked(ec, t);
338 /* check if we received SCI during transaction */
339 ec_check_sci_sync(ec, acpi_ec_read_status(ec));
340 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
341 msleep(1);
342 /* It is safe to enable the GPE outside of the transaction. */
343 acpi_enable_gpe(NULL, ec->gpe);
344 } else if (t->irq_count > ec_storm_threshold) {
345 pr_info(PREFIX "GPE storm detected(%d GPEs), "
346 "transactions will use polling mode\n",
347 t->irq_count);
348 set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
350 pr_debug(PREFIX "transaction end\n");
351 if (ec->global_lock)
352 acpi_release_global_lock(glk);
353 unlock:
354 mutex_unlock(&ec->mutex);
355 return status;
358 static int acpi_ec_burst_enable(struct acpi_ec *ec)
360 u8 d;
361 struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
362 .wdata = NULL, .rdata = &d,
363 .wlen = 0, .rlen = 1};
365 return acpi_ec_transaction(ec, &t);
368 static int acpi_ec_burst_disable(struct acpi_ec *ec)
370 struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
371 .wdata = NULL, .rdata = NULL,
372 .wlen = 0, .rlen = 0};
374 return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
375 acpi_ec_transaction(ec, &t) : 0;
378 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
380 int result;
381 u8 d;
382 struct transaction t = {.command = ACPI_EC_COMMAND_READ,
383 .wdata = &address, .rdata = &d,
384 .wlen = 1, .rlen = 1};
386 result = acpi_ec_transaction(ec, &t);
387 *data = d;
388 return result;
391 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
393 u8 wdata[2] = { address, data };
394 struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
395 .wdata = wdata, .rdata = NULL,
396 .wlen = 2, .rlen = 0};
398 return acpi_ec_transaction(ec, &t);
402 * Externally callable EC access functions. For now, assume 1 EC only
404 int ec_burst_enable(void)
406 if (!first_ec)
407 return -ENODEV;
408 return acpi_ec_burst_enable(first_ec);
411 EXPORT_SYMBOL(ec_burst_enable);
413 int ec_burst_disable(void)
415 if (!first_ec)
416 return -ENODEV;
417 return acpi_ec_burst_disable(first_ec);
420 EXPORT_SYMBOL(ec_burst_disable);
422 int ec_read(u8 addr, u8 *val)
424 int err;
425 u8 temp_data;
427 if (!first_ec)
428 return -ENODEV;
430 err = acpi_ec_read(first_ec, addr, &temp_data);
432 if (!err) {
433 *val = temp_data;
434 return 0;
435 } else
436 return err;
439 EXPORT_SYMBOL(ec_read);
441 int ec_write(u8 addr, u8 val)
443 int err;
445 if (!first_ec)
446 return -ENODEV;
448 err = acpi_ec_write(first_ec, addr, val);
450 return err;
453 EXPORT_SYMBOL(ec_write);
455 int ec_transaction(u8 command,
456 const u8 * wdata, unsigned wdata_len,
457 u8 * rdata, unsigned rdata_len)
459 struct transaction t = {.command = command,
460 .wdata = wdata, .rdata = rdata,
461 .wlen = wdata_len, .rlen = rdata_len};
462 if (!first_ec)
463 return -ENODEV;
465 return acpi_ec_transaction(first_ec, &t);
468 EXPORT_SYMBOL(ec_transaction);
470 /* Get the handle to the EC device */
471 acpi_handle ec_get_handle(void)
473 if (!first_ec)
474 return NULL;
475 return first_ec->handle;
478 EXPORT_SYMBOL(ec_get_handle);
481 * Process _Q events that might have accumulated in the EC.
482 * Run with locked ec mutex.
484 static void acpi_ec_clear(struct acpi_ec *ec)
486 int i, status;
487 u8 value = 0;
489 for (i = 0; i < ACPI_EC_CLEAR_MAX; i++) {
490 status = acpi_ec_sync_query(ec, &value);
491 if (status || !value)
492 break;
495 if (unlikely(i == ACPI_EC_CLEAR_MAX))
496 pr_warn("Warning: Maximum of %d stale EC events cleared\n", i);
497 else
498 pr_info("%d stale EC events cleared\n", i);
501 void acpi_ec_block_transactions(void)
503 struct acpi_ec *ec = first_ec;
505 if (!ec)
506 return;
508 mutex_lock(&ec->mutex);
509 /* Prevent transactions from being carried out */
510 set_bit(EC_FLAGS_BLOCKED, &ec->flags);
511 mutex_unlock(&ec->mutex);
514 void acpi_ec_unblock_transactions(void)
516 struct acpi_ec *ec = first_ec;
518 if (!ec)
519 return;
521 mutex_lock(&ec->mutex);
522 /* Allow transactions to be carried out again */
523 clear_bit(EC_FLAGS_BLOCKED, &ec->flags);
525 if (EC_FLAGS_CLEAR_ON_RESUME)
526 acpi_ec_clear(ec);
528 mutex_unlock(&ec->mutex);
531 void acpi_ec_unblock_transactions_early(void)
534 * Allow transactions to happen again (this function is called from
535 * atomic context during wakeup, so we don't need to acquire the mutex).
537 if (first_ec)
538 clear_bit(EC_FLAGS_BLOCKED, &first_ec->flags);
541 static int acpi_ec_query_unlocked(struct acpi_ec *ec, u8 * data)
543 int result;
544 u8 d;
545 struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
546 .wdata = NULL, .rdata = &d,
547 .wlen = 0, .rlen = 1};
548 if (!ec || !data)
549 return -EINVAL;
551 * Query the EC to find out which _Qxx method we need to evaluate.
552 * Note that successful completion of the query causes the ACPI_EC_SCI
553 * bit to be cleared (and thus clearing the interrupt source).
555 result = acpi_ec_transaction_unlocked(ec, &t);
556 if (result)
557 return result;
558 if (!d)
559 return -ENODATA;
560 *data = d;
561 return 0;
564 /* --------------------------------------------------------------------------
565 Event Management
566 -------------------------------------------------------------------------- */
567 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
568 acpi_handle handle, acpi_ec_query_func func,
569 void *data)
571 struct acpi_ec_query_handler *handler =
572 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
573 if (!handler)
574 return -ENOMEM;
576 handler->query_bit = query_bit;
577 handler->handle = handle;
578 handler->func = func;
579 handler->data = data;
580 mutex_lock(&ec->mutex);
581 list_add(&handler->node, &ec->list);
582 mutex_unlock(&ec->mutex);
583 return 0;
586 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
588 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
590 struct acpi_ec_query_handler *handler, *tmp;
591 mutex_lock(&ec->mutex);
592 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
593 if (query_bit == handler->query_bit) {
594 list_del(&handler->node);
595 kfree(handler);
598 mutex_unlock(&ec->mutex);
601 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
603 static void acpi_ec_run(void *cxt)
605 struct acpi_ec_query_handler *handler = cxt;
606 if (!handler)
607 return;
608 pr_debug(PREFIX "start query execution\n");
609 if (handler->func)
610 handler->func(handler->data);
611 else if (handler->handle)
612 acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
613 pr_debug(PREFIX "stop query execution\n");
614 kfree(handler);
617 static int acpi_ec_sync_query(struct acpi_ec *ec, u8 *data)
619 u8 value = 0;
620 int status;
621 struct acpi_ec_query_handler *handler, *copy;
623 status = acpi_ec_query_unlocked(ec, &value);
624 if (data)
625 *data = value;
626 if (status)
627 return status;
629 list_for_each_entry(handler, &ec->list, node) {
630 if (value == handler->query_bit) {
631 /* have custom handler for this bit */
632 copy = kmalloc(sizeof(*handler), GFP_KERNEL);
633 if (!copy)
634 return -ENOMEM;
635 memcpy(copy, handler, sizeof(*copy));
636 pr_debug(PREFIX "push query execution (0x%2x) on queue\n", value);
637 return acpi_os_execute((copy->func) ?
638 OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
639 acpi_ec_run, copy);
642 return 0;
645 static void acpi_ec_gpe_query(void *ec_cxt)
647 struct acpi_ec *ec = ec_cxt;
648 if (!ec)
649 return;
650 mutex_lock(&ec->mutex);
651 acpi_ec_sync_query(ec, NULL);
652 mutex_unlock(&ec->mutex);
655 static int ec_check_sci(struct acpi_ec *ec, u8 state)
657 if (state & ACPI_EC_FLAG_SCI) {
658 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
659 pr_debug(PREFIX "push gpe query to the queue\n");
660 return acpi_os_execute(OSL_NOTIFY_HANDLER,
661 acpi_ec_gpe_query, ec);
664 return 0;
667 static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
668 u32 gpe_number, void *data)
670 unsigned long flags;
671 struct acpi_ec *ec = data;
673 spin_lock_irqsave(&ec->lock, flags);
674 if (advance_transaction(ec))
675 wake_up(&ec->wait);
676 spin_unlock_irqrestore(&ec->lock, flags);
677 ec_check_sci(ec, acpi_ec_read_status(ec));
678 return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
681 /* --------------------------------------------------------------------------
682 Address Space Management
683 -------------------------------------------------------------------------- */
685 static acpi_status
686 acpi_ec_space_handler(u32 function, acpi_physical_address address,
687 u32 bits, u64 *value64,
688 void *handler_context, void *region_context)
690 struct acpi_ec *ec = handler_context;
691 int result = 0, i, bytes = bits / 8;
692 u8 *value = (u8 *)value64;
694 if ((address > 0xFF) || !value || !handler_context)
695 return AE_BAD_PARAMETER;
697 if (function != ACPI_READ && function != ACPI_WRITE)
698 return AE_BAD_PARAMETER;
700 if (EC_FLAGS_MSI || bits > 8)
701 acpi_ec_burst_enable(ec);
703 for (i = 0; i < bytes; ++i, ++address, ++value)
704 result = (function == ACPI_READ) ?
705 acpi_ec_read(ec, address, value) :
706 acpi_ec_write(ec, address, *value);
708 if (EC_FLAGS_MSI || bits > 8)
709 acpi_ec_burst_disable(ec);
711 switch (result) {
712 case -EINVAL:
713 return AE_BAD_PARAMETER;
714 break;
715 case -ENODEV:
716 return AE_NOT_FOUND;
717 break;
718 case -ETIME:
719 return AE_TIME;
720 break;
721 default:
722 return AE_OK;
726 /* --------------------------------------------------------------------------
727 Driver Interface
728 -------------------------------------------------------------------------- */
729 static acpi_status
730 ec_parse_io_ports(struct acpi_resource *resource, void *context);
732 static struct acpi_ec *make_acpi_ec(void)
734 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
735 if (!ec)
736 return NULL;
737 ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
738 mutex_init(&ec->mutex);
739 init_waitqueue_head(&ec->wait);
740 INIT_LIST_HEAD(&ec->list);
741 spin_lock_init(&ec->lock);
742 return ec;
745 static acpi_status
746 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
747 void *context, void **return_value)
749 char node_name[5];
750 struct acpi_buffer buffer = { sizeof(node_name), node_name };
751 struct acpi_ec *ec = context;
752 int value = 0;
753 acpi_status status;
755 status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
757 if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
758 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
760 return AE_OK;
763 static acpi_status
764 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
766 acpi_status status;
767 unsigned long long tmp = 0;
769 struct acpi_ec *ec = context;
771 /* clear addr values, ec_parse_io_ports depend on it */
772 ec->command_addr = ec->data_addr = 0;
774 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
775 ec_parse_io_ports, ec);
776 if (ACPI_FAILURE(status))
777 return status;
779 /* Get GPE bit assignment (EC events). */
780 /* TODO: Add support for _GPE returning a package */
781 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
782 if (ACPI_FAILURE(status))
783 return status;
784 ec->gpe = tmp;
785 /* Use the global lock for all EC transactions? */
786 tmp = 0;
787 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
788 ec->global_lock = tmp;
789 ec->handle = handle;
790 return AE_CTRL_TERMINATE;
793 static int ec_install_handlers(struct acpi_ec *ec)
795 acpi_status status;
796 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
797 return 0;
798 status = acpi_install_gpe_handler(NULL, ec->gpe,
799 ACPI_GPE_EDGE_TRIGGERED,
800 &acpi_ec_gpe_handler, ec);
801 if (ACPI_FAILURE(status))
802 return -ENODEV;
804 acpi_enable_gpe(NULL, ec->gpe);
805 status = acpi_install_address_space_handler(ec->handle,
806 ACPI_ADR_SPACE_EC,
807 &acpi_ec_space_handler,
808 NULL, ec);
809 if (ACPI_FAILURE(status)) {
810 if (status == AE_NOT_FOUND) {
812 * Maybe OS fails in evaluating the _REG object.
813 * The AE_NOT_FOUND error will be ignored and OS
814 * continue to initialize EC.
816 printk(KERN_ERR "Fail in evaluating the _REG object"
817 " of EC device. Broken bios is suspected.\n");
818 } else {
819 acpi_remove_gpe_handler(NULL, ec->gpe,
820 &acpi_ec_gpe_handler);
821 acpi_disable_gpe(NULL, ec->gpe);
822 return -ENODEV;
826 set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
827 return 0;
830 static void ec_remove_handlers(struct acpi_ec *ec)
832 acpi_disable_gpe(NULL, ec->gpe);
833 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
834 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
835 pr_err(PREFIX "failed to remove space handler\n");
836 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
837 &acpi_ec_gpe_handler)))
838 pr_err(PREFIX "failed to remove gpe handler\n");
839 clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
842 static int acpi_ec_add(struct acpi_device *device)
844 struct acpi_ec *ec = NULL;
845 int ret;
847 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
848 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
850 /* Check for boot EC */
851 if (boot_ec &&
852 (boot_ec->handle == device->handle ||
853 boot_ec->handle == ACPI_ROOT_OBJECT)) {
854 ec = boot_ec;
855 boot_ec = NULL;
856 } else {
857 ec = make_acpi_ec();
858 if (!ec)
859 return -ENOMEM;
861 if (ec_parse_device(device->handle, 0, ec, NULL) !=
862 AE_CTRL_TERMINATE) {
863 kfree(ec);
864 return -EINVAL;
867 /* Find and register all query methods */
868 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
869 acpi_ec_register_query_methods, NULL, ec, NULL);
871 if (!first_ec)
872 first_ec = ec;
873 device->driver_data = ec;
875 ret = !!request_region(ec->data_addr, 1, "EC data");
876 WARN(!ret, "Could not request EC data io port 0x%lx", ec->data_addr);
877 ret = !!request_region(ec->command_addr, 1, "EC cmd");
878 WARN(!ret, "Could not request EC cmd io port 0x%lx", ec->command_addr);
880 pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
881 ec->gpe, ec->command_addr, ec->data_addr);
883 ret = ec_install_handlers(ec);
885 /* EC is fully operational, allow queries */
886 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
888 /* Clear stale _Q events if hardware might require that */
889 if (EC_FLAGS_CLEAR_ON_RESUME) {
890 mutex_lock(&ec->mutex);
891 acpi_ec_clear(ec);
892 mutex_unlock(&ec->mutex);
894 return ret;
897 static int acpi_ec_remove(struct acpi_device *device)
899 struct acpi_ec *ec;
900 struct acpi_ec_query_handler *handler, *tmp;
902 if (!device)
903 return -EINVAL;
905 ec = acpi_driver_data(device);
906 ec_remove_handlers(ec);
907 mutex_lock(&ec->mutex);
908 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
909 list_del(&handler->node);
910 kfree(handler);
912 mutex_unlock(&ec->mutex);
913 release_region(ec->data_addr, 1);
914 release_region(ec->command_addr, 1);
915 device->driver_data = NULL;
916 if (ec == first_ec)
917 first_ec = NULL;
918 kfree(ec);
919 return 0;
922 static acpi_status
923 ec_parse_io_ports(struct acpi_resource *resource, void *context)
925 struct acpi_ec *ec = context;
927 if (resource->type != ACPI_RESOURCE_TYPE_IO)
928 return AE_OK;
931 * The first address region returned is the data port, and
932 * the second address region returned is the status/command
933 * port.
935 if (ec->data_addr == 0)
936 ec->data_addr = resource->data.io.minimum;
937 else if (ec->command_addr == 0)
938 ec->command_addr = resource->data.io.minimum;
939 else
940 return AE_CTRL_TERMINATE;
942 return AE_OK;
945 int __init acpi_boot_ec_enable(void)
947 if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
948 return 0;
949 if (!ec_install_handlers(boot_ec)) {
950 first_ec = boot_ec;
951 return 0;
953 return -EFAULT;
956 static const struct acpi_device_id ec_device_ids[] = {
957 {"PNP0C09", 0},
958 {"", 0},
961 /* Some BIOS do not survive early DSDT scan, skip it */
962 static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
964 EC_FLAGS_SKIP_DSDT_SCAN = 1;
965 return 0;
968 /* ASUStek often supplies us with broken ECDT, validate it */
969 static int ec_validate_ecdt(const struct dmi_system_id *id)
971 EC_FLAGS_VALIDATE_ECDT = 1;
972 return 0;
975 /* MSI EC needs special treatment, enable it */
976 static int ec_flag_msi(const struct dmi_system_id *id)
978 printk(KERN_DEBUG PREFIX "Detected MSI hardware, enabling workarounds.\n");
979 EC_FLAGS_MSI = 1;
980 EC_FLAGS_VALIDATE_ECDT = 1;
981 return 0;
985 * Clevo M720 notebook actually works ok with IRQ mode, if we lifted
986 * the GPE storm threshold back to 20
988 static int ec_enlarge_storm_threshold(const struct dmi_system_id *id)
990 pr_debug("Setting the EC GPE storm threshold to 20\n");
991 ec_storm_threshold = 20;
992 return 0;
996 * Acer EC firmware refuses to respond QR_EC when SCI_EVT is not set, for
997 * which case, we complete the QR_EC without issuing it to the firmware.
998 * https://bugzilla.kernel.org/show_bug.cgi?id=86211
1000 static int ec_flag_query_handshake(const struct dmi_system_id *id)
1002 pr_debug("Detected the EC firmware requiring QR_EC issued when SCI_EVT set\n");
1003 EC_FLAGS_QUERY_HANDSHAKE = 1;
1004 return 0;
1008 * On some hardware it is necessary to clear events accumulated by the EC during
1009 * sleep. These ECs stop reporting GPEs until they are manually polled, if too
1010 * many events are accumulated. (e.g. Samsung Series 5/9 notebooks)
1012 * https://bugzilla.kernel.org/show_bug.cgi?id=44161
1014 * Ideally, the EC should also be instructed NOT to accumulate events during
1015 * sleep (which Windows seems to do somehow), but the interface to control this
1016 * behaviour is not known at this time.
1018 * Models known to be affected are Samsung 530Uxx/535Uxx/540Uxx/550Pxx/900Xxx,
1019 * however it is very likely that other Samsung models are affected.
1021 * On systems which don't accumulate _Q events during sleep, this extra check
1022 * should be harmless.
1024 static int ec_clear_on_resume(const struct dmi_system_id *id)
1026 pr_debug("Detected system needing EC poll on resume.\n");
1027 EC_FLAGS_CLEAR_ON_RESUME = 1;
1028 return 0;
1031 static struct dmi_system_id ec_dmi_table[] __initdata = {
1033 ec_skip_dsdt_scan, "Compal JFL92", {
1034 DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
1035 DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
1037 ec_flag_msi, "MSI hardware", {
1038 DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
1040 ec_flag_msi, "MSI hardware", {
1041 DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
1043 ec_flag_msi, "MSI hardware", {
1044 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
1046 ec_flag_msi, "MSI hardware", {
1047 DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-STAR")}, NULL},
1049 ec_flag_msi, "Quanta hardware", {
1050 DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
1051 DMI_MATCH(DMI_PRODUCT_NAME, "TW8/SW8/DW8"),}, NULL},
1053 ec_flag_msi, "Quanta hardware", {
1054 DMI_MATCH(DMI_SYS_VENDOR, "Quanta"),
1055 DMI_MATCH(DMI_PRODUCT_NAME, "TW9/SW9"),}, NULL},
1057 ec_validate_ecdt, "ASUS hardware", {
1058 DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
1060 ec_validate_ecdt, "ASUS hardware", {
1061 DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer Inc.") }, NULL},
1063 ec_enlarge_storm_threshold, "CLEVO hardware", {
1064 DMI_MATCH(DMI_SYS_VENDOR, "CLEVO Co."),
1065 DMI_MATCH(DMI_PRODUCT_NAME, "M720T/M730T"),}, NULL},
1067 ec_skip_dsdt_scan, "HP Folio 13", {
1068 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1069 DMI_MATCH(DMI_PRODUCT_NAME, "HP Folio 13"),}, NULL},
1071 ec_validate_ecdt, "ASUS hardware", {
1072 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTek Computer Inc."),
1073 DMI_MATCH(DMI_PRODUCT_NAME, "L4R"),}, NULL},
1075 ec_clear_on_resume, "Samsung hardware", {
1076 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD.")}, NULL},
1078 ec_flag_query_handshake, "Acer hardware", {
1079 DMI_MATCH(DMI_SYS_VENDOR, "Acer"), }, NULL},
1083 int __init acpi_ec_ecdt_probe(void)
1085 acpi_status status;
1086 struct acpi_ec *saved_ec = NULL;
1087 struct acpi_table_ecdt *ecdt_ptr;
1089 boot_ec = make_acpi_ec();
1090 if (!boot_ec)
1091 return -ENOMEM;
1093 * Generate a boot ec context
1095 dmi_check_system(ec_dmi_table);
1096 status = acpi_get_table(ACPI_SIG_ECDT, 1,
1097 (struct acpi_table_header **)&ecdt_ptr);
1098 if (ACPI_SUCCESS(status)) {
1099 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
1100 boot_ec->command_addr = ecdt_ptr->control.address;
1101 boot_ec->data_addr = ecdt_ptr->data.address;
1102 boot_ec->gpe = ecdt_ptr->gpe;
1103 boot_ec->handle = ACPI_ROOT_OBJECT;
1104 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
1105 /* Don't trust ECDT, which comes from ASUSTek */
1106 if (!EC_FLAGS_VALIDATE_ECDT)
1107 goto install;
1108 saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
1109 if (!saved_ec)
1110 return -ENOMEM;
1111 /* fall through */
1114 if (EC_FLAGS_SKIP_DSDT_SCAN)
1115 return -ENODEV;
1117 /* This workaround is needed only on some broken machines,
1118 * which require early EC, but fail to provide ECDT */
1119 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
1120 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
1121 boot_ec, NULL);
1122 /* Check that acpi_get_devices actually find something */
1123 if (ACPI_FAILURE(status) || !boot_ec->handle)
1124 goto error;
1125 if (saved_ec) {
1126 /* try to find good ECDT from ASUSTek */
1127 if (saved_ec->command_addr != boot_ec->command_addr ||
1128 saved_ec->data_addr != boot_ec->data_addr ||
1129 saved_ec->gpe != boot_ec->gpe ||
1130 saved_ec->handle != boot_ec->handle)
1131 pr_info(PREFIX "ASUSTek keeps feeding us with broken "
1132 "ECDT tables, which are very hard to workaround. "
1133 "Trying to use DSDT EC info instead. Please send "
1134 "output of acpidump to linux-acpi@vger.kernel.org\n");
1135 kfree(saved_ec);
1136 saved_ec = NULL;
1137 } else {
1138 /* We really need to limit this workaround, the only ASUS,
1139 * which needs it, has fake EC._INI method, so use it as flag.
1140 * Keep boot_ec struct as it will be needed soon.
1142 if (!dmi_name_in_vendors("ASUS") ||
1143 !acpi_has_method(boot_ec->handle, "_INI"))
1144 return -ENODEV;
1146 install:
1147 if (!ec_install_handlers(boot_ec)) {
1148 first_ec = boot_ec;
1149 return 0;
1151 error:
1152 kfree(boot_ec);
1153 boot_ec = NULL;
1154 return -ENODEV;
1157 static struct acpi_driver acpi_ec_driver = {
1158 .name = "ec",
1159 .class = ACPI_EC_CLASS,
1160 .ids = ec_device_ids,
1161 .ops = {
1162 .add = acpi_ec_add,
1163 .remove = acpi_ec_remove,
1167 int __init acpi_ec_init(void)
1169 int result = 0;
1171 /* Now register the driver for the EC */
1172 result = acpi_bus_register_driver(&acpi_ec_driver);
1173 if (result < 0)
1174 return -ENODEV;
1176 return result;
1179 /* EC driver currently not unloadable */
1180 #if 0
1181 static void __exit acpi_ec_exit(void)
1184 acpi_bus_unregister_driver(&acpi_ec_driver);
1185 return;
1187 #endif /* 0 */