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