Merge branch 'bjorn-start-stop-2.6.32' into release
[linux-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/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 int ec_install_handlers(struct acpi_ec *ec)
791 {
792         acpi_status status;
793         if (test_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags))
794                 return 0;
795         status = acpi_install_gpe_handler(NULL, ec->gpe,
796                                   ACPI_GPE_EDGE_TRIGGERED,
797                                   &acpi_ec_gpe_handler, ec);
798         if (ACPI_FAILURE(status))
799                 return -ENODEV;
800         acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
801         acpi_enable_gpe(NULL, ec->gpe);
802         status = acpi_install_address_space_handler(ec->handle,
803                                                     ACPI_ADR_SPACE_EC,
804                                                     &acpi_ec_space_handler,
805                                                     NULL, ec);
806         if (ACPI_FAILURE(status)) {
807                 if (status == AE_NOT_FOUND) {
808                         /*
809                          * Maybe OS fails in evaluating the _REG object.
810                          * The AE_NOT_FOUND error will be ignored and OS
811                          * continue to initialize EC.
812                          */
813                         printk(KERN_ERR "Fail in evaluating the _REG object"
814                                 " of EC device. Broken bios is suspected.\n");
815                 } else {
816                         acpi_remove_gpe_handler(NULL, ec->gpe,
817                                 &acpi_ec_gpe_handler);
818                         return -ENODEV;
819                 }
820         }
821
822         set_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
823         return 0;
824 }
825
826 static void ec_remove_handlers(struct acpi_ec *ec)
827 {
828         if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
829                                 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
830                 pr_err(PREFIX "failed to remove space handler\n");
831         if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
832                                 &acpi_ec_gpe_handler)))
833                 pr_err(PREFIX "failed to remove gpe handler\n");
834         clear_bit(EC_FLAGS_HANDLERS_INSTALLED, &ec->flags);
835 }
836
837 static int acpi_ec_add(struct acpi_device *device)
838 {
839         struct acpi_ec *ec = NULL;
840         int ret;
841
842         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
843         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
844
845         /* Check for boot EC */
846         if (boot_ec &&
847             (boot_ec->handle == device->handle ||
848              boot_ec->handle == ACPI_ROOT_OBJECT)) {
849                 ec = boot_ec;
850                 boot_ec = NULL;
851         } else {
852                 ec = make_acpi_ec();
853                 if (!ec)
854                         return -ENOMEM;
855         }
856         if (ec_parse_device(device->handle, 0, ec, NULL) !=
857                 AE_CTRL_TERMINATE) {
858                         kfree(ec);
859                         return -EINVAL;
860         }
861
862         ec->handle = device->handle;
863
864         /* Find and register all query methods */
865         acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
866                             acpi_ec_register_query_methods, ec, NULL);
867
868         if (!first_ec)
869                 first_ec = ec;
870         device->driver_data = ec;
871         acpi_ec_add_fs(device);
872         pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
873                           ec->gpe, ec->command_addr, ec->data_addr);
874         pr_info(PREFIX "driver started in %s mode\n",
875                 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
876
877         ret = ec_install_handlers(ec);
878
879         /* EC is fully operational, allow queries */
880         clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
881         return ret;
882 }
883
884 static int acpi_ec_remove(struct acpi_device *device, int type)
885 {
886         struct acpi_ec *ec;
887         struct acpi_ec_query_handler *handler, *tmp;
888
889         if (!device)
890                 return -EINVAL;
891
892         ec = acpi_driver_data(device);
893         ec_remove_handlers(ec);
894         mutex_lock(&ec->lock);
895         list_for_each_entry_safe(handler, tmp, &ec->list, node) {
896                 list_del(&handler->node);
897                 kfree(handler);
898         }
899         mutex_unlock(&ec->lock);
900         acpi_ec_remove_fs(device);
901         device->driver_data = NULL;
902         if (ec == first_ec)
903                 first_ec = NULL;
904         kfree(ec);
905         return 0;
906 }
907
908 static acpi_status
909 ec_parse_io_ports(struct acpi_resource *resource, void *context)
910 {
911         struct acpi_ec *ec = context;
912
913         if (resource->type != ACPI_RESOURCE_TYPE_IO)
914                 return AE_OK;
915
916         /*
917          * The first address region returned is the data port, and
918          * the second address region returned is the status/command
919          * port.
920          */
921         if (ec->data_addr == 0)
922                 ec->data_addr = resource->data.io.minimum;
923         else if (ec->command_addr == 0)
924                 ec->command_addr = resource->data.io.minimum;
925         else
926                 return AE_CTRL_TERMINATE;
927
928         return AE_OK;
929 }
930
931 int __init acpi_boot_ec_enable(void)
932 {
933         if (!boot_ec || test_bit(EC_FLAGS_HANDLERS_INSTALLED, &boot_ec->flags))
934                 return 0;
935         if (!ec_install_handlers(boot_ec)) {
936                 first_ec = boot_ec;
937                 return 0;
938         }
939         return -EFAULT;
940 }
941
942 static const struct acpi_device_id ec_device_ids[] = {
943         {"PNP0C09", 0},
944         {"", 0},
945 };
946
947 int __init acpi_ec_ecdt_probe(void)
948 {
949         acpi_status status;
950         struct acpi_ec *saved_ec = NULL;
951         struct acpi_table_ecdt *ecdt_ptr;
952
953         boot_ec = make_acpi_ec();
954         if (!boot_ec)
955                 return -ENOMEM;
956         /*
957          * Generate a boot ec context
958          */
959         if (dmi_name_in_vendors("Micro-Star") ||
960             dmi_name_in_vendors("Notebook")) {
961                 pr_info(PREFIX "Enabling special treatment for EC from MSI.\n");
962                 EC_FLAGS_MSI = 1;
963         }
964         status = acpi_get_table(ACPI_SIG_ECDT, 1,
965                                 (struct acpi_table_header **)&ecdt_ptr);
966         if (ACPI_SUCCESS(status)) {
967                 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
968                 boot_ec->command_addr = ecdt_ptr->control.address;
969                 boot_ec->data_addr = ecdt_ptr->data.address;
970                 boot_ec->gpe = ecdt_ptr->gpe;
971                 boot_ec->handle = ACPI_ROOT_OBJECT;
972                 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
973                 /* Don't trust ECDT, which comes from ASUSTek */
974                 if (!dmi_name_in_vendors("ASUS") && EC_FLAGS_MSI == 0)
975                         goto install;
976                 saved_ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
977                 if (!saved_ec)
978                         return -ENOMEM;
979                 memcpy(saved_ec, boot_ec, sizeof(struct acpi_ec));
980         /* fall through */
981         }
982         /* This workaround is needed only on some broken machines,
983          * which require early EC, but fail to provide ECDT */
984         printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
985         status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
986                                         boot_ec, NULL);
987         /* Check that acpi_get_devices actually find something */
988         if (ACPI_FAILURE(status) || !boot_ec->handle)
989                 goto error;
990         if (saved_ec) {
991                 /* try to find good ECDT from ASUSTek */
992                 if (saved_ec->command_addr != boot_ec->command_addr ||
993                     saved_ec->data_addr != boot_ec->data_addr ||
994                     saved_ec->gpe != boot_ec->gpe ||
995                     saved_ec->handle != boot_ec->handle)
996                         pr_info(PREFIX "ASUSTek keeps feeding us with broken "
997                         "ECDT tables, which are very hard to workaround. "
998                         "Trying to use DSDT EC info instead. Please send "
999                         "output of acpidump to linux-acpi@vger.kernel.org\n");
1000                 kfree(saved_ec);
1001                 saved_ec = NULL;
1002         } else {
1003                 /* We really need to limit this workaround, the only ASUS,
1004                 * which needs it, has fake EC._INI method, so use it as flag.
1005                 * Keep boot_ec struct as it will be needed soon.
1006                 */
1007                 acpi_handle dummy;
1008                 if (!dmi_name_in_vendors("ASUS") ||
1009                     ACPI_FAILURE(acpi_get_handle(boot_ec->handle, "_INI",
1010                                                         &dummy)))
1011                         return -ENODEV;
1012         }
1013 install:
1014         if (!ec_install_handlers(boot_ec)) {
1015                 first_ec = boot_ec;
1016                 return 0;
1017         }
1018 error:
1019         kfree(boot_ec);
1020         boot_ec = NULL;
1021         return -ENODEV;
1022 }
1023
1024 static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
1025 {
1026         struct acpi_ec *ec = acpi_driver_data(device);
1027         /* Stop using GPE */
1028         set_bit(EC_FLAGS_NO_GPE, &ec->flags);
1029         clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
1030         acpi_disable_gpe(NULL, ec->gpe);
1031         return 0;
1032 }
1033
1034 static int acpi_ec_resume(struct acpi_device *device)
1035 {
1036         struct acpi_ec *ec = acpi_driver_data(device);
1037         /* Enable use of GPE back */
1038         clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
1039         set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
1040         acpi_enable_gpe(NULL, ec->gpe);
1041         return 0;
1042 }
1043
1044 static struct acpi_driver acpi_ec_driver = {
1045         .name = "ec",
1046         .class = ACPI_EC_CLASS,
1047         .ids = ec_device_ids,
1048         .ops = {
1049                 .add = acpi_ec_add,
1050                 .remove = acpi_ec_remove,
1051                 .suspend = acpi_ec_suspend,
1052                 .resume = acpi_ec_resume,
1053                 },
1054 };
1055
1056 int __init acpi_ec_init(void)
1057 {
1058         int result = 0;
1059
1060         acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1061         if (!acpi_ec_dir)
1062                 return -ENODEV;
1063
1064         /* Now register the driver for the EC */
1065         result = acpi_bus_register_driver(&acpi_ec_driver);
1066         if (result < 0) {
1067                 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1068                 return -ENODEV;
1069         }
1070
1071         return result;
1072 }
1073
1074 /* EC driver currently not unloadable */
1075 #if 0
1076 static void __exit acpi_ec_exit(void)
1077 {
1078
1079         acpi_bus_unregister_driver(&acpi_ec_driver);
1080
1081         remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1082
1083         return;
1084 }
1085 #endif  /* 0 */