Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[sfrench/cifs-2.6.git] / drivers / acpi / ec.c
1 /*
2  *  ec.c - ACPI Embedded Controller Driver (v2.1)
3  *
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>
9  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  *
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.
16  *
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.
21  *
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.
25  *
26  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27  */
28
29 /* Uncomment next line to get verbose printout */
30 /* #define DEBUG */
31
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>
45
46 #include "internal.h"
47
48 #define ACPI_EC_CLASS                   "embedded_controller"
49 #define ACPI_EC_DEVICE_NAME             "Embedded Controller"
50 #define ACPI_EC_FILE_INFO               "info"
51
52 #undef PREFIX
53 #define PREFIX                          "ACPI: EC: "
54
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 */
60
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,
68 };
69
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_CDELAY          10      /* Wait 10us before polling EC */
73 #define ACPI_EC_MSI_UDELAY      550     /* Wait 550us for MSI EC */
74
75 #define ACPI_EC_STORM_THRESHOLD 8       /* number of false interrupts
76                                            per one transaction */
77
78 enum {
79         EC_FLAGS_QUERY_PENDING,         /* Query is pending */
80         EC_FLAGS_GPE_STORM,             /* GPE storm detected */
81         EC_FLAGS_HANDLERS_INSTALLED,    /* Handlers for GPE and
82                                          * OpReg are installed */
83         EC_FLAGS_BLOCKED,               /* Transactions are blocked */
84 };
85
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);
89
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;
96 };
97
98 struct transaction {
99         const u8 *wdata;
100         u8 *rdata;
101         unsigned short irq_count;
102         u8 command;
103         u8 wi;
104         u8 ri;
105         u8 wlen;
106         u8 rlen;
107         bool done;
108 };
109
110 struct acpi_ec *boot_ec, *first_ec;
111 EXPORT_SYMBOL(first_ec);
112
113 static int EC_FLAGS_MSI; /* Out-of-spec MSI controller */
114 static int EC_FLAGS_VALIDATE_ECDT; /* ASUStec ECDTs need to be validated */
115 static int EC_FLAGS_SKIP_DSDT_SCAN; /* Not all BIOS survive early DSDT scan */
116
117 /* --------------------------------------------------------------------------
118                              Transaction Management
119    -------------------------------------------------------------------------- */
120
121 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
122 {
123         u8 x = inb(ec->command_addr);
124         pr_debug(PREFIX "---> status = 0x%2.2x\n", x);
125         return x;
126 }
127
128 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
129 {
130         u8 x = inb(ec->data_addr);
131         pr_debug(PREFIX "---> data = 0x%2.2x\n", x);
132         return x;
133 }
134
135 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
136 {
137         pr_debug(PREFIX "<--- command = 0x%2.2x\n", command);
138         outb(command, ec->command_addr);
139 }
140
141 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
142 {
143         pr_debug(PREFIX "<--- data = 0x%2.2x\n", data);
144         outb(data, ec->data_addr);
145 }
146
147 static int ec_transaction_done(struct acpi_ec *ec)
148 {
149         unsigned long flags;
150         int ret = 0;
151         spin_lock_irqsave(&ec->curr_lock, flags);
152         if (!ec->curr || ec->curr->done)
153                 ret = 1;
154         spin_unlock_irqrestore(&ec->curr_lock, flags);
155         return ret;
156 }
157
158 static void start_transaction(struct acpi_ec *ec)
159 {
160         ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
161         ec->curr->done = false;
162         acpi_ec_write_cmd(ec, ec->curr->command);
163 }
164
165 static void advance_transaction(struct acpi_ec *ec, u8 status)
166 {
167         unsigned long flags;
168         spin_lock_irqsave(&ec->curr_lock, flags);
169         if (!ec->curr)
170                 goto unlock;
171         if (ec->curr->wlen > ec->curr->wi) {
172                 if ((status & ACPI_EC_FLAG_IBF) == 0)
173                         acpi_ec_write_data(ec,
174                                 ec->curr->wdata[ec->curr->wi++]);
175                 else
176                         goto err;
177         } else if (ec->curr->rlen > ec->curr->ri) {
178                 if ((status & ACPI_EC_FLAG_OBF) == 1) {
179                         ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
180                         if (ec->curr->rlen == ec->curr->ri)
181                                 ec->curr->done = true;
182                 } else
183                         goto err;
184         } else if (ec->curr->wlen == ec->curr->wi &&
185                    (status & ACPI_EC_FLAG_IBF) == 0)
186                 ec->curr->done = true;
187         goto unlock;
188 err:
189         /* false interrupt, state didn't change */
190         if (in_interrupt())
191                 ++ec->curr->irq_count;
192 unlock:
193         spin_unlock_irqrestore(&ec->curr_lock, flags);
194 }
195
196 static int acpi_ec_sync_query(struct acpi_ec *ec);
197
198 static int ec_check_sci_sync(struct acpi_ec *ec, u8 state)
199 {
200         if (state & ACPI_EC_FLAG_SCI) {
201                 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
202                         return acpi_ec_sync_query(ec);
203         }
204         return 0;
205 }
206
207 static int ec_poll(struct acpi_ec *ec)
208 {
209         unsigned long flags;
210         int repeat = 2; /* number of command restarts */
211         while (repeat--) {
212                 unsigned long delay = jiffies +
213                         msecs_to_jiffies(ACPI_EC_DELAY);
214                 do {
215                         /* don't sleep with disabled interrupts */
216                         if (EC_FLAGS_MSI || irqs_disabled()) {
217                                 udelay(ACPI_EC_MSI_UDELAY);
218                                 if (ec_transaction_done(ec))
219                                         return 0;
220                         } else {
221                                 if (wait_event_timeout(ec->wait,
222                                                 ec_transaction_done(ec),
223                                                 msecs_to_jiffies(1)))
224                                         return 0;
225                         }
226                         advance_transaction(ec, acpi_ec_read_status(ec));
227                 } while (time_before(jiffies, delay));
228                 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
229                         break;
230                 pr_debug(PREFIX "controller reset, restart transaction\n");
231                 spin_lock_irqsave(&ec->curr_lock, flags);
232                 start_transaction(ec);
233                 spin_unlock_irqrestore(&ec->curr_lock, flags);
234         }
235         return -ETIME;
236 }
237
238 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
239                                         struct transaction *t)
240 {
241         unsigned long tmp;
242         int ret = 0;
243         if (EC_FLAGS_MSI)
244                 udelay(ACPI_EC_MSI_UDELAY);
245         /* start transaction */
246         spin_lock_irqsave(&ec->curr_lock, tmp);
247         /* following two actions should be kept atomic */
248         ec->curr = t;
249         start_transaction(ec);
250         if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
251                 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
252         spin_unlock_irqrestore(&ec->curr_lock, tmp);
253         ret = ec_poll(ec);
254         spin_lock_irqsave(&ec->curr_lock, tmp);
255         ec->curr = NULL;
256         spin_unlock_irqrestore(&ec->curr_lock, tmp);
257         return ret;
258 }
259
260 static int ec_check_ibf0(struct acpi_ec *ec)
261 {
262         u8 status = acpi_ec_read_status(ec);
263         return (status & ACPI_EC_FLAG_IBF) == 0;
264 }
265
266 static int ec_wait_ibf0(struct acpi_ec *ec)
267 {
268         unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
269         /* interrupt wait manually if GPE mode is not active */
270         while (time_before(jiffies, delay))
271                 if (wait_event_timeout(ec->wait, ec_check_ibf0(ec),
272                                         msecs_to_jiffies(1)))
273                         return 0;
274         return -ETIME;
275 }
276
277 static int acpi_ec_transaction(struct acpi_ec *ec, struct transaction *t)
278 {
279         int status;
280         u32 glk;
281         if (!ec || (!t) || (t->wlen && !t->wdata) || (t->rlen && !t->rdata))
282                 return -EINVAL;
283         if (t->rdata)
284                 memset(t->rdata, 0, t->rlen);
285         mutex_lock(&ec->lock);
286         if (test_bit(EC_FLAGS_BLOCKED, &ec->flags)) {
287                 status = -EINVAL;
288                 goto unlock;
289         }
290         if (ec->global_lock) {
291                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
292                 if (ACPI_FAILURE(status)) {
293                         status = -ENODEV;
294                         goto unlock;
295                 }
296         }
297         if (ec_wait_ibf0(ec)) {
298                 pr_err(PREFIX "input buffer is not empty, "
299                                 "aborting transaction\n");
300                 status = -ETIME;
301                 goto end;
302         }
303         pr_debug(PREFIX "transaction start\n");
304         /* disable GPE during transaction if storm is detected */
305         if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
306                 /*
307                  * It has to be disabled at the hardware level regardless of the
308                  * GPE reference counting, so that it doesn't trigger.
309                  */
310                 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
311         }
312
313         status = acpi_ec_transaction_unlocked(ec, t);
314
315         /* check if we received SCI during transaction */
316         ec_check_sci_sync(ec, acpi_ec_read_status(ec));
317         if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
318                 msleep(1);
319                 /*
320                  * It is safe to enable the GPE outside of the transaction.  Use
321                  * acpi_set_gpe() for that, since we used it to disable the GPE
322                  * above.
323                  */
324                 acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
325         } else if (t->irq_count > ACPI_EC_STORM_THRESHOLD) {
326                 pr_info(PREFIX "GPE storm detected, "
327                         "transactions will use polling mode\n");
328                 set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
329         }
330         pr_debug(PREFIX "transaction end\n");
331 end:
332         if (ec->global_lock)
333                 acpi_release_global_lock(glk);
334 unlock:
335         mutex_unlock(&ec->lock);
336         return status;
337 }
338
339 static int acpi_ec_burst_enable(struct acpi_ec *ec)
340 {
341         u8 d;
342         struct transaction t = {.command = ACPI_EC_BURST_ENABLE,
343                                 .wdata = NULL, .rdata = &d,
344                                 .wlen = 0, .rlen = 1};
345
346         return acpi_ec_transaction(ec, &t);
347 }
348
349 static int acpi_ec_burst_disable(struct acpi_ec *ec)
350 {
351         struct transaction t = {.command = ACPI_EC_BURST_DISABLE,
352                                 .wdata = NULL, .rdata = NULL,
353                                 .wlen = 0, .rlen = 0};
354
355         return (acpi_ec_read_status(ec) & ACPI_EC_FLAG_BURST) ?
356                                 acpi_ec_transaction(ec, &t) : 0;
357 }
358
359 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
360 {
361         int result;
362         u8 d;
363         struct transaction t = {.command = ACPI_EC_COMMAND_READ,
364                                 .wdata = &address, .rdata = &d,
365                                 .wlen = 1, .rlen = 1};
366
367         result = acpi_ec_transaction(ec, &t);
368         *data = d;
369         return result;
370 }
371
372 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
373 {
374         u8 wdata[2] = { address, data };
375         struct transaction t = {.command = ACPI_EC_COMMAND_WRITE,
376                                 .wdata = wdata, .rdata = NULL,
377                                 .wlen = 2, .rlen = 0};
378
379         return acpi_ec_transaction(ec, &t);
380 }
381
382 /*
383  * Externally callable EC access functions. For now, assume 1 EC only
384  */
385 int ec_burst_enable(void)
386 {
387         if (!first_ec)
388                 return -ENODEV;
389         return acpi_ec_burst_enable(first_ec);
390 }
391
392 EXPORT_SYMBOL(ec_burst_enable);
393
394 int ec_burst_disable(void)
395 {
396         if (!first_ec)
397                 return -ENODEV;
398         return acpi_ec_burst_disable(first_ec);
399 }
400
401 EXPORT_SYMBOL(ec_burst_disable);
402
403 int ec_read(u8 addr, u8 * val)
404 {
405         int err;
406         u8 temp_data;
407
408         if (!first_ec)
409                 return -ENODEV;
410
411         err = acpi_ec_read(first_ec, addr, &temp_data);
412
413         if (!err) {
414                 *val = temp_data;
415                 return 0;
416         } else
417                 return err;
418 }
419
420 EXPORT_SYMBOL(ec_read);
421
422 int ec_write(u8 addr, u8 val)
423 {
424         int err;
425
426         if (!first_ec)
427                 return -ENODEV;
428
429         err = acpi_ec_write(first_ec, addr, val);
430
431         return err;
432 }
433
434 EXPORT_SYMBOL(ec_write);
435
436 int ec_transaction(u8 command,
437                    const u8 * wdata, unsigned wdata_len,
438                    u8 * rdata, unsigned rdata_len,
439                    int force_poll)
440 {
441         struct transaction t = {.command = command,
442                                 .wdata = wdata, .rdata = rdata,
443                                 .wlen = wdata_len, .rlen = rdata_len};
444         if (!first_ec)
445                 return -ENODEV;
446
447         return acpi_ec_transaction(first_ec, &t);
448 }
449
450 EXPORT_SYMBOL(ec_transaction);
451
452 void acpi_ec_block_transactions(void)
453 {
454         struct acpi_ec *ec = first_ec;
455
456         if (!ec)
457                 return;
458
459         mutex_lock(&ec->lock);
460         /* Prevent transactions from being carried out */
461         set_bit(EC_FLAGS_BLOCKED, &ec->flags);
462         mutex_unlock(&ec->lock);
463 }
464
465 void acpi_ec_unblock_transactions(void)
466 {
467         struct acpi_ec *ec = first_ec;
468
469         if (!ec)
470                 return;
471
472         mutex_lock(&ec->lock);
473         /* Allow transactions to be carried out again */
474         clear_bit(EC_FLAGS_BLOCKED, &ec->flags);
475         mutex_unlock(&ec->lock);
476 }
477
478 void acpi_ec_unblock_transactions_early(void)
479 {
480         /*
481          * Allow transactions to happen again (this function is called from
482          * atomic context during wakeup, so we don't need to acquire the mutex).
483          */
484         if (first_ec)
485                 clear_bit(EC_FLAGS_BLOCKED, &first_ec->flags);
486 }
487
488 static int acpi_ec_query_unlocked(struct acpi_ec *ec, u8 * data)
489 {
490         int result;
491         u8 d;
492         struct transaction t = {.command = ACPI_EC_COMMAND_QUERY,
493                                 .wdata = NULL, .rdata = &d,
494                                 .wlen = 0, .rlen = 1};
495         if (!ec || !data)
496                 return -EINVAL;
497         /*
498          * Query the EC to find out which _Qxx method we need to evaluate.
499          * Note that successful completion of the query causes the ACPI_EC_SCI
500          * bit to be cleared (and thus clearing the interrupt source).
501          */
502         result = acpi_ec_transaction_unlocked(ec, &t);
503         if (result)
504                 return result;
505         if (!d)
506                 return -ENODATA;
507         *data = d;
508         return 0;
509 }
510
511 /* --------------------------------------------------------------------------
512                                 Event Management
513    -------------------------------------------------------------------------- */
514 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
515                               acpi_handle handle, acpi_ec_query_func func,
516                               void *data)
517 {
518         struct acpi_ec_query_handler *handler =
519             kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
520         if (!handler)
521                 return -ENOMEM;
522
523         handler->query_bit = query_bit;
524         handler->handle = handle;
525         handler->func = func;
526         handler->data = data;
527         mutex_lock(&ec->lock);
528         list_add(&handler->node, &ec->list);
529         mutex_unlock(&ec->lock);
530         return 0;
531 }
532
533 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
534
535 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
536 {
537         struct acpi_ec_query_handler *handler, *tmp;
538         mutex_lock(&ec->lock);
539         list_for_each_entry_safe(handler, tmp, &ec->list, node) {
540                 if (query_bit == handler->query_bit) {
541                         list_del(&handler->node);
542                         kfree(handler);
543                 }
544         }
545         mutex_unlock(&ec->lock);
546 }
547
548 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
549
550 static void acpi_ec_run(void *cxt)
551 {
552         struct acpi_ec_query_handler *handler = cxt;
553         if (!handler)
554                 return;
555         pr_debug(PREFIX "start query execution\n");
556         if (handler->func)
557                 handler->func(handler->data);
558         else if (handler->handle)
559                 acpi_evaluate_object(handler->handle, NULL, NULL, NULL);
560         pr_debug(PREFIX "stop query execution\n");
561         kfree(handler);
562 }
563
564 static int acpi_ec_sync_query(struct acpi_ec *ec)
565 {
566         u8 value = 0;
567         int status;
568         struct acpi_ec_query_handler *handler, *copy;
569         if ((status = acpi_ec_query_unlocked(ec, &value)))
570                 return status;
571         list_for_each_entry(handler, &ec->list, node) {
572                 if (value == handler->query_bit) {
573                         /* have custom handler for this bit */
574                         copy = kmalloc(sizeof(*handler), GFP_KERNEL);
575                         if (!copy)
576                                 return -ENOMEM;
577                         memcpy(copy, handler, sizeof(*copy));
578                         pr_debug(PREFIX "push query execution (0x%2x) on queue\n", value);
579                         return acpi_os_execute((copy->func) ?
580                                 OSL_NOTIFY_HANDLER : OSL_GPE_HANDLER,
581                                 acpi_ec_run, copy);
582                 }
583         }
584         return 0;
585 }
586
587 static void acpi_ec_gpe_query(void *ec_cxt)
588 {
589         struct acpi_ec *ec = ec_cxt;
590         if (!ec)
591                 return;
592         mutex_lock(&ec->lock);
593         acpi_ec_sync_query(ec);
594         mutex_unlock(&ec->lock);
595 }
596
597 static void acpi_ec_gpe_query(void *ec_cxt);
598
599 static int ec_check_sci(struct acpi_ec *ec, u8 state)
600 {
601         if (state & ACPI_EC_FLAG_SCI) {
602                 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
603                         pr_debug(PREFIX "push gpe query to the queue\n");
604                         return acpi_os_execute(OSL_NOTIFY_HANDLER,
605                                 acpi_ec_gpe_query, ec);
606                 }
607         }
608         return 0;
609 }
610
611 static u32 acpi_ec_gpe_handler(void *data)
612 {
613         struct acpi_ec *ec = data;
614
615         pr_debug(PREFIX "~~~> interrupt\n");
616
617         advance_transaction(ec, acpi_ec_read_status(ec));
618         if (ec_transaction_done(ec) &&
619             (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
620                 wake_up(&ec->wait);
621                 ec_check_sci(ec, acpi_ec_read_status(ec));
622         }
623         return ACPI_INTERRUPT_HANDLED;
624 }
625
626 /* --------------------------------------------------------------------------
627                              Address Space Management
628    -------------------------------------------------------------------------- */
629
630 static acpi_status
631 acpi_ec_space_handler(u32 function, acpi_physical_address address,
632                       u32 bits, u64 *value64,
633                       void *handler_context, void *region_context)
634 {
635         struct acpi_ec *ec = handler_context;
636         int result = 0, i, bytes = bits / 8;
637         u8 *value = (u8 *)value64;
638
639         if ((address > 0xFF) || !value || !handler_context)
640                 return AE_BAD_PARAMETER;
641
642         if (function != ACPI_READ && function != ACPI_WRITE)
643                 return AE_BAD_PARAMETER;
644
645         if (EC_FLAGS_MSI || bits > 8)
646                 acpi_ec_burst_enable(ec);
647
648         for (i = 0; i < bytes; ++i, ++address, ++value)
649                 result = (function == ACPI_READ) ?
650                         acpi_ec_read(ec, address, value) :
651                         acpi_ec_write(ec, address, *value);
652
653         if (EC_FLAGS_MSI || bits > 8)
654                 acpi_ec_burst_disable(ec);
655
656         switch (result) {
657         case -EINVAL:
658                 return AE_BAD_PARAMETER;
659                 break;
660         case -ENODEV:
661                 return AE_NOT_FOUND;
662                 break;
663         case -ETIME:
664                 return AE_TIME;
665                 break;
666         default:
667                 return AE_OK;
668         }
669 }
670
671 /* --------------------------------------------------------------------------
672                                Driver Interface
673    -------------------------------------------------------------------------- */
674 static acpi_status
675 ec_parse_io_ports(struct acpi_resource *resource, void *context);
676
677 static struct acpi_ec *make_acpi_ec(void)
678 {
679         struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
680         if (!ec)
681                 return NULL;
682         ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
683         mutex_init(&ec->lock);
684         init_waitqueue_head(&ec->wait);
685         INIT_LIST_HEAD(&ec->list);
686         spin_lock_init(&ec->curr_lock);
687         return ec;
688 }
689
690 static acpi_status
691 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
692                                void *context, void **return_value)
693 {
694         char node_name[5];
695         struct acpi_buffer buffer = { sizeof(node_name), node_name };
696         struct acpi_ec *ec = context;
697         int value = 0;
698         acpi_status status;
699
700         status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
701
702         if (ACPI_SUCCESS(status) && sscanf(node_name, "_Q%x", &value) == 1) {
703                 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
704         }
705         return AE_OK;
706 }
707
708 static acpi_status
709 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
710 {
711         acpi_status status;
712         unsigned long long tmp = 0;
713
714         struct acpi_ec *ec = context;
715
716         /* clear addr values, ec_parse_io_ports depend on it */
717         ec->command_addr = ec->data_addr = 0;
718
719         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
720                                      ec_parse_io_ports, ec);
721         if (ACPI_FAILURE(status))
722                 return status;
723
724         /* Get GPE bit assignment (EC events). */
725         /* TODO: Add support for _GPE returning a package */
726         status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
727         if (ACPI_FAILURE(status))
728                 return status;
729         ec->gpe = tmp;
730         /* Use the global lock for all EC transactions? */
731         tmp = 0;
732         acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
733         ec->global_lock = tmp;
734         ec->handle = handle;
735         return AE_CTRL_TERMINATE;
736 }
737
738 static int ec_install_handlers(struct acpi_ec *ec)
739 {
740         acpi_status status;
741         if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
742                 return 0;
743         status = acpi_install_gpe_handler(NULL, ec->gpe,
744                                   ACPI_GPE_EDGE_TRIGGERED,
745                                   &acpi_ec_gpe_handler, ec);
746         if (ACPI_FAILURE(status))
747                 return -ENODEV;
748
749         acpi_enable_gpe(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
750         status = acpi_install_address_space_handler(ec->handle,
751                                                     ACPI_ADR_SPACE_EC,
752                                                     &acpi_ec_space_handler,
753                                                     NULL, ec);
754         if (ACPI_FAILURE(status)) {
755                 if (status == AE_NOT_FOUND) {
756                         /*
757                          * Maybe OS fails in evaluating the _REG object.
758                          * The AE_NOT_FOUND error will be ignored and OS
759                          * continue to initialize EC.
760                          */
761                         printk(KERN_ERR "Fail in evaluating the _REG object"
762                                 " of EC device. Broken bios is suspected.\n");
763                 } else {
764                         acpi_remove_gpe_handler(NULL, ec->gpe,
765                                 &acpi_ec_gpe_handler);
766                         acpi_disable_gpe(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
767                         return -ENODEV;
768                 }
769         }
770
771         set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
772         return 0;
773 }
774
775 static void ec_remove_handlers(struct acpi_ec *ec)
776 {
777         acpi_disable_gpe(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
778         if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
779                                 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
780                 pr_err(PREFIX "failed to remove space handler\n");
781         if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
782                                 &acpi_ec_gpe_handler)))
783                 pr_err(PREFIX "failed to remove gpe handler\n");
784         clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
785 }
786
787 static int acpi_ec_add(struct acpi_device *device)
788 {
789         struct acpi_ec *ec = NULL;
790         int ret;
791
792         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
793         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
794
795         /* Check for boot EC */
796         if (boot_ec &&
797             (boot_ec->handle == device->handle ||
798              boot_ec->handle == ACPI_ROOT_OBJECT)) {
799                 ec = boot_ec;
800                 boot_ec = NULL;
801         } else {
802                 ec = make_acpi_ec();
803                 if (!ec)
804                         return -ENOMEM;
805         }
806         if (ec_parse_device(device->handle, 0, ec, NULL) !=
807                 AE_CTRL_TERMINATE) {
808                         kfree(ec);
809                         return -EINVAL;
810         }
811
812         ec->handle = device->handle;
813
814         /* Find and register all query methods */
815         acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
816                             acpi_ec_register_query_methods, NULL, ec, NULL);
817
818         if (!first_ec)
819                 first_ec = ec;
820         device->driver_data = ec;
821
822         WARN(!request_region(ec->data_addr, 1, "EC data"),
823              "Could not request EC data io port 0x%lx", ec->data_addr);
824         WARN(!request_region(ec->command_addr, 1, "EC cmd"),
825              "Could not request EC cmd io port 0x%lx", ec->command_addr);
826
827         pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
828                           ec->gpe, ec->command_addr, ec->data_addr);
829
830         ret = ec_install_handlers(ec);
831
832         /* EC is fully operational, allow queries */
833         clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
834         return ret;
835 }
836
837 static int acpi_ec_remove(struct acpi_device *device, int type)
838 {
839         struct acpi_ec *ec;
840         struct acpi_ec_query_handler *handler, *tmp;
841
842         if (!device)
843                 return -EINVAL;
844
845         ec = acpi_driver_data(device);
846         ec_remove_handlers(ec);
847         mutex_lock(&ec->lock);
848         list_for_each_entry_safe(handler, tmp, &ec->list, node) {
849                 list_del(&handler->node);
850                 kfree(handler);
851         }
852         mutex_unlock(&ec->lock);
853         release_region(ec->data_addr, 1);
854         release_region(ec->command_addr, 1);
855         device->driver_data = NULL;
856         if (ec == first_ec)
857                 first_ec = NULL;
858         kfree(ec);
859         return 0;
860 }
861
862 static acpi_status
863 ec_parse_io_ports(struct acpi_resource *resource, void *context)
864 {
865         struct acpi_ec *ec = context;
866
867         if (resource->type != ACPI_RESOURCE_TYPE_IO)
868                 return AE_OK;
869
870         /*
871          * The first address region returned is the data port, and
872          * the second address region returned is the status/command
873          * port.
874          */
875         if (ec->data_addr == 0)
876                 ec->data_addr = resource->data.io.minimum;
877         else if (ec->command_addr == 0)
878                 ec->command_addr = resource->data.io.minimum;
879         else
880                 return AE_CTRL_TERMINATE;
881
882         return AE_OK;
883 }
884
885 int __init acpi_boot_ec_enable(void)
886 {
887         if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
888                 return 0;
889         if (!ec_install_handlers(boot_ec)) {
890                 first_ec = boot_ec;
891                 return 0;
892         }
893         return -EFAULT;
894 }
895
896 static const struct acpi_device_id ec_device_ids[] = {
897         {"PNP0C09", 0},
898         {"", 0},
899 };
900
901 /* Some BIOS do not survive early DSDT scan, skip it */
902 static int ec_skip_dsdt_scan(const struct dmi_system_id *id)
903 {
904         EC_FLAGS_SKIP_DSDT_SCAN = 1;
905         return 0;
906 }
907
908 /* ASUStek often supplies us with broken ECDT, validate it */
909 static int ec_validate_ecdt(const struct dmi_system_id *id)
910 {
911         EC_FLAGS_VALIDATE_ECDT = 1;
912         return 0;
913 }
914
915 /* MSI EC needs special treatment, enable it */
916 static int ec_flag_msi(const struct dmi_system_id *id)
917 {
918         printk(KERN_DEBUG PREFIX "Detected MSI hardware, enabling workarounds.\n");
919         EC_FLAGS_MSI = 1;
920         EC_FLAGS_VALIDATE_ECDT = 1;
921         return 0;
922 }
923
924 static struct dmi_system_id __initdata ec_dmi_table[] = {
925         {
926         ec_skip_dsdt_scan, "Compal JFL92", {
927         DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
928         DMI_MATCH(DMI_BOARD_NAME, "JFL92") }, NULL},
929         {
930         ec_flag_msi, "MSI hardware", {
931         DMI_MATCH(DMI_BIOS_VENDOR, "Micro-Star")}, NULL},
932         {
933         ec_flag_msi, "MSI hardware", {
934         DMI_MATCH(DMI_SYS_VENDOR, "Micro-Star")}, NULL},
935         {
936         ec_flag_msi, "MSI hardware", {
937         DMI_MATCH(DMI_CHASSIS_VENDOR, "MICRO-Star")}, NULL},
938         {
939         ec_validate_ecdt, "ASUS hardware", {
940         DMI_MATCH(DMI_BIOS_VENDOR, "ASUS") }, NULL},
941         {},
942 };
943
944
945 int __init acpi_ec_ecdt_probe(void)
946 {
947         acpi_status status;
948         struct acpi_ec *saved_ec = NULL;
949         struct acpi_table_ecdt *ecdt_ptr;
950
951         boot_ec = make_acpi_ec();
952         if (!boot_ec)
953                 return -ENOMEM;
954         /*
955          * Generate a boot ec context
956          */
957         dmi_check_system(ec_dmi_table);
958         status = acpi_get_table(ACPI_SIG_ECDT, 1,
959                                 (struct acpi_table_header **)&ecdt_ptr);
960         if (ACPI_SUCCESS(status)) {
961                 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
962                 boot_ec->command_addr = ecdt_ptr->control.address;
963                 boot_ec->data_addr = ecdt_ptr->data.address;
964                 boot_ec->gpe = ecdt_ptr->gpe;
965                 boot_ec->handle = ACPI_ROOT_OBJECT;
966                 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
967                 /* Don't trust ECDT, which comes from ASUSTek */
968                 if (!EC_FLAGS_VALIDATE_ECDT)
969                         goto install;
970                 saved_ec = kmemdup(boot_ec, sizeof(struct acpi_ec), GFP_KERNEL);
971                 if (!saved_ec)
972                         return -ENOMEM;
973         /* fall through */
974         }
975
976         if (EC_FLAGS_SKIP_DSDT_SCAN)
977                 return -ENODEV;
978
979         /* This workaround is needed only on some broken machines,
980          * which require early EC, but fail to provide ECDT */
981         printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
982         status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
983                                         boot_ec, NULL);
984         /* Check that acpi_get_devices actually find something */
985         if (ACPI_FAILURE(status) || !boot_ec->handle)
986                 goto error;
987         if (saved_ec) {
988                 /* try to find good ECDT from ASUSTek */
989                 if (saved_ec->command_addr != boot_ec->command_addr ||
990                     saved_ec->data_addr != boot_ec->data_addr ||
991                     saved_ec->gpe != boot_ec->gpe ||
992                     saved_ec->handle != boot_ec->handle)
993                         pr_info(PREFIX "ASUSTek keeps feeding us with broken "
994                         "ECDT tables, which are very hard to workaround. "
995                         "Trying to use DSDT EC info instead. Please send "
996                         "output of acpidump to linux-acpi@vger.kernel.org\n");
997                 kfree(saved_ec);
998                 saved_ec = NULL;
999         } else {
1000                 /* We really need to limit this workaround, the only ASUS,
1001                 * which needs it, has fake EC._INI method, so use it as flag.
1002                 * Keep boot_ec struct as it will be needed soon.
1003                 */
1004                 acpi_handle dummy;
1005                 if (!dmi_name_in_vendors("ASUS") ||
1006                     ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
1007                                                         &dummy)))
1008                         return -ENODEV;
1009         }
1010 install:
1011         if (!ec_install_handlers(boot_ec)) {
1012                 first_ec = boot_ec;
1013                 return 0;
1014         }
1015 error:
1016         kfree(boot_ec);
1017         boot_ec = NULL;
1018         return -ENODEV;
1019 }
1020
1021 static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
1022 {
1023         struct acpi_ec *ec = acpi_driver_data(device);
1024         /* Stop using the GPE, but keep it reference counted. */
1025         acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_DISABLE);
1026         return 0;
1027 }
1028
1029 static int acpi_ec_resume(struct acpi_device *device)
1030 {
1031         struct acpi_ec *ec = acpi_driver_data(device);
1032         /* Enable the GPE again, but don't reference count it once more. */
1033         acpi_set_gpe(NULL, ec->gpe, ACPI_GPE_ENABLE);
1034         return 0;
1035 }
1036
1037 static struct acpi_driver acpi_ec_driver = {
1038         .name = "ec",
1039         .class = ACPI_EC_CLASS,
1040         .ids = ec_device_ids,
1041         .ops = {
1042                 .add = acpi_ec_add,
1043                 .remove = acpi_ec_remove,
1044                 .suspend = acpi_ec_suspend,
1045                 .resume = acpi_ec_resume,
1046                 },
1047 };
1048
1049 int __init acpi_ec_init(void)
1050 {
1051         int result = 0;
1052
1053         /* Now register the driver for the EC */
1054         result = acpi_bus_register_driver(&acpi_ec_driver);
1055         if (result < 0)
1056                 return -ENODEV;
1057
1058         return result;
1059 }
1060
1061 /* EC driver currently not unloadable */
1062 #if 0
1063 static void __exit acpi_ec_exit(void)
1064 {
1065
1066         acpi_bus_unregister_driver(&acpi_ec_driver);
1067         return;
1068 }
1069 #endif  /* 0 */