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