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