ACPI: EC: use printk_ratelimit(), add some DEBUG mode messages
[safe/jmp/linux-2.6] / 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 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/types.h>
33 #include <linux/delay.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36 #include <linux/interrupt.h>
37 #include <linux/list.h>
38 #include <asm/io.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.h>
41 #include <acpi/actypes.h>
42
43 #define ACPI_EC_CLASS                   "embedded_controller"
44 #define ACPI_EC_DEVICE_NAME             "Embedded Controller"
45 #define ACPI_EC_FILE_INFO               "info"
46
47 #undef PREFIX
48 #define PREFIX                          "ACPI: EC: "
49
50 /* Uncomment next line to get verbose print outs*/
51 /* #define DEBUG */
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
77 enum {
78         EC_FLAGS_WAIT_GPE = 0,          /* Don't check status until GPE arrives */
79         EC_FLAGS_QUERY_PENDING,         /* Query is pending */
80         EC_FLAGS_GPE_MODE,              /* Expect GPE to be sent for status change */
81         EC_FLAGS_ONLY_IBF_GPE,          /* Expect GPE only for IBF = 0 event */
82 };
83
84 static int acpi_ec_remove(struct acpi_device *device, int type);
85 static int acpi_ec_start(struct acpi_device *device);
86 static int acpi_ec_stop(struct acpi_device *device, int type);
87 static int acpi_ec_add(struct acpi_device *device);
88
89 static const struct acpi_device_id ec_device_ids[] = {
90         {"PNP0C09", 0},
91         {"", 0},
92 };
93
94 static struct acpi_driver acpi_ec_driver = {
95         .name = "ec",
96         .class = ACPI_EC_CLASS,
97         .ids = ec_device_ids,
98         .ops = {
99                 .add = acpi_ec_add,
100                 .remove = acpi_ec_remove,
101                 .start = acpi_ec_start,
102                 .stop = acpi_ec_stop,
103                 },
104 };
105
106 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
107 /* External interfaces use first EC only, so remember */
108 typedef int (*acpi_ec_query_func) (void *data);
109
110 struct acpi_ec_query_handler {
111         struct list_head node;
112         acpi_ec_query_func func;
113         acpi_handle handle;
114         void *data;
115         u8 query_bit;
116 };
117
118 static struct acpi_ec {
119         acpi_handle handle;
120         unsigned long gpe;
121         unsigned long command_addr;
122         unsigned long data_addr;
123         unsigned long global_lock;
124         unsigned long flags;
125         struct mutex lock;
126         wait_queue_head_t wait;
127         struct list_head list;
128         u8 handlers_installed;
129 } *boot_ec, *first_ec;
130
131 /* --------------------------------------------------------------------------
132                              Transaction Management
133    -------------------------------------------------------------------------- */
134
135 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
136 {
137         u8 x = inb(ec->command_addr);
138         pr_debug(PREFIX "---> status = 0x%2x\n", x);
139         return x;
140 }
141
142 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
143 {
144         u8 x = inb(ec->data_addr);
145         pr_debug(PREFIX "---> data = 0x%2x\n", x);
146         return inb(ec->data_addr);
147 }
148
149 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
150 {
151         pr_debug(PREFIX "<--- command = 0x%2x\n", command);
152         outb(command, ec->command_addr);
153 }
154
155 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
156 {
157         pr_debug(PREFIX "<--- data = 0x%2x\n", data);
158         outb(data, ec->data_addr);
159 }
160
161 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
162 {
163         if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
164                 return 0;
165         if (event == ACPI_EC_EVENT_OBF_1) {
166                 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
167                         return 1;
168         } else if (event == ACPI_EC_EVENT_IBF_0) {
169                 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
170                         return 1;
171         }
172
173         return 0;
174 }
175
176 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
177 {
178         if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
179             likely(!force_poll)) {
180                 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
181                                        msecs_to_jiffies(ACPI_EC_DELAY)))
182                         return 0;
183                 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
184                 if (acpi_ec_check_status(ec, event)) {
185                         if (event == ACPI_EC_EVENT_OBF_1) {
186                                 /* miss OBF = 1 GPE, don't expect it anymore */
187                                 pr_info(PREFIX "missing OBF_1 confirmation,"
188                                         "switching to degraded mode.\n");
189                                 set_bit(EC_FLAGS_ONLY_IBF_GPE, &ec->flags);
190                         } else {
191                                 /* missing GPEs, switch back to poll mode */
192                                 if (printk_ratelimit())
193                                         pr_info(PREFIX "missing IBF_1 confirmations,"
194                                                 "switch off interrupt mode.\n");
195                                 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
196                         }
197                         return 0;
198                 }
199         } else {
200                 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
201                 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
202                 while (time_before(jiffies, delay)) {
203                         if (acpi_ec_check_status(ec, event))
204                                 return 0;
205                 }
206         }
207         pr_err(PREFIX "acpi_ec_wait timeout,"
208                                " status = %d, expect_event = %d\n",
209                                acpi_ec_read_status(ec), event);
210         return -ETIME;
211 }
212
213 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
214                                         const u8 * wdata, unsigned wdata_len,
215                                         u8 * rdata, unsigned rdata_len,
216                                         int force_poll)
217 {
218         int result = 0;
219         set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
220         acpi_ec_write_cmd(ec, command);
221         pr_debug(PREFIX "transaction start\n");
222         for (; wdata_len > 0; --wdata_len) {
223                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
224                 if (result) {
225                         pr_err(PREFIX
226                                "write_cmd timeout, command = %d\n", command);
227                         goto end;
228                 }
229                 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
230                 acpi_ec_write_data(ec, *(wdata++));
231         }
232
233         if (!rdata_len) {
234                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
235                 if (result) {
236                         pr_err(PREFIX
237                                "finish-write timeout, command = %d\n", command);
238                         goto end;
239                 }
240         } else if (command == ACPI_EC_COMMAND_QUERY)
241                 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
242
243         for (; rdata_len > 0; --rdata_len) {
244                 if (test_bit(EC_FLAGS_ONLY_IBF_GPE, &ec->flags))
245                         force_poll = 1;
246                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
247                 if (result) {
248                         pr_err(PREFIX "read timeout, command = %d\n", command);
249                         goto end;
250                 }
251                 /* Don't expect GPE after last read */
252                 if (rdata_len > 1)
253                         set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
254                 *(rdata++) = acpi_ec_read_data(ec);
255         }
256       end:
257         pr_debug(PREFIX "transaction end\n");
258         return result;
259 }
260
261 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
262                                const u8 * wdata, unsigned wdata_len,
263                                u8 * rdata, unsigned rdata_len,
264                                int force_poll)
265 {
266         int status;
267         u32 glk;
268
269         if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
270                 return -EINVAL;
271
272         if (rdata)
273                 memset(rdata, 0, rdata_len);
274
275         mutex_lock(&ec->lock);
276         if (ec->global_lock) {
277                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
278                 if (ACPI_FAILURE(status)) {
279                         mutex_unlock(&ec->lock);
280                         return -ENODEV;
281                 }
282         }
283
284         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
285         if (status) {
286                 pr_err(PREFIX "input buffer is not empty, "
287                                 "aborting transaction\n");
288                 goto end;
289         }
290
291         status = acpi_ec_transaction_unlocked(ec, command,
292                                               wdata, wdata_len,
293                                               rdata, rdata_len,
294                                               force_poll);
295
296       end:
297
298         if (ec->global_lock)
299                 acpi_release_global_lock(glk);
300         mutex_unlock(&ec->lock);
301
302         return status;
303 }
304
305 /*
306  * Note: samsung nv5000 doesn't work with ec burst mode.
307  * http://bugzilla.kernel.org/show_bug.cgi?id=4980
308  */
309 int acpi_ec_burst_enable(struct acpi_ec *ec)
310 {
311         u8 d;
312         return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
313 }
314
315 int acpi_ec_burst_disable(struct acpi_ec *ec)
316 {
317         return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
318 }
319
320 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
321 {
322         int result;
323         u8 d;
324
325         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
326                                      &address, 1, &d, 1, 0);
327         *data = d;
328         return result;
329 }
330
331 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
332 {
333         u8 wdata[2] = { address, data };
334         return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
335                                    wdata, 2, NULL, 0, 0);
336 }
337
338 /*
339  * Externally callable EC access functions. For now, assume 1 EC only
340  */
341 int ec_burst_enable(void)
342 {
343         if (!first_ec)
344                 return -ENODEV;
345         return acpi_ec_burst_enable(first_ec);
346 }
347
348 EXPORT_SYMBOL(ec_burst_enable);
349
350 int ec_burst_disable(void)
351 {
352         if (!first_ec)
353                 return -ENODEV;
354         return acpi_ec_burst_disable(first_ec);
355 }
356
357 EXPORT_SYMBOL(ec_burst_disable);
358
359 int ec_read(u8 addr, u8 * val)
360 {
361         int err;
362         u8 temp_data;
363
364         if (!first_ec)
365                 return -ENODEV;
366
367         err = acpi_ec_read(first_ec, addr, &temp_data);
368
369         if (!err) {
370                 *val = temp_data;
371                 return 0;
372         } else
373                 return err;
374 }
375
376 EXPORT_SYMBOL(ec_read);
377
378 int ec_write(u8 addr, u8 val)
379 {
380         int err;
381
382         if (!first_ec)
383                 return -ENODEV;
384
385         err = acpi_ec_write(first_ec, addr, val);
386
387         return err;
388 }
389
390 EXPORT_SYMBOL(ec_write);
391
392 int ec_transaction(u8 command,
393                    const u8 * wdata, unsigned wdata_len,
394                    u8 * rdata, unsigned rdata_len,
395                    int force_poll)
396 {
397         if (!first_ec)
398                 return -ENODEV;
399
400         return acpi_ec_transaction(first_ec, command, wdata,
401                                    wdata_len, rdata, rdata_len,
402                                    force_poll);
403 }
404
405 EXPORT_SYMBOL(ec_transaction);
406
407 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
408 {
409         int result;
410         u8 d;
411
412         if (!ec || !data)
413                 return -EINVAL;
414
415         /*
416          * Query the EC to find out which _Qxx method we need to evaluate.
417          * Note that successful completion of the query causes the ACPI_EC_SCI
418          * bit to be cleared (and thus clearing the interrupt source).
419          */
420
421         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
422         if (result)
423                 return result;
424
425         if (!d)
426                 return -ENODATA;
427
428         *data = d;
429         return 0;
430 }
431
432 /* --------------------------------------------------------------------------
433                                 Event Management
434    -------------------------------------------------------------------------- */
435 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
436                               acpi_handle handle, acpi_ec_query_func func,
437                               void *data)
438 {
439         struct acpi_ec_query_handler *handler =
440             kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
441         if (!handler)
442                 return -ENOMEM;
443
444         handler->query_bit = query_bit;
445         handler->handle = handle;
446         handler->func = func;
447         handler->data = data;
448         mutex_lock(&ec->lock);
449         list_add(&handler->node, &ec->list);
450         mutex_unlock(&ec->lock);
451         return 0;
452 }
453
454 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
455
456 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
457 {
458         struct acpi_ec_query_handler *handler, *tmp;
459         mutex_lock(&ec->lock);
460         list_for_each_entry_safe(handler, tmp, &ec->list, node) {
461                 if (query_bit == handler->query_bit) {
462                         list_del(&handler->node);
463                         kfree(handler);
464                 }
465         }
466         mutex_unlock(&ec->lock);
467 }
468
469 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
470
471 static void acpi_ec_gpe_query(void *ec_cxt)
472 {
473         struct acpi_ec *ec = ec_cxt;
474         u8 value = 0;
475         struct acpi_ec_query_handler *handler, copy;
476
477         if (!ec || acpi_ec_query(ec, &value))
478                 return;
479         mutex_lock(&ec->lock);
480         list_for_each_entry(handler, &ec->list, node) {
481                 if (value == handler->query_bit) {
482                         /* have custom handler for this bit */
483                         memcpy(&copy, handler, sizeof(copy));
484                         mutex_unlock(&ec->lock);
485                         if (copy.func) {
486                                 copy.func(copy.data);
487                         } else if (copy.handle) {
488                                 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
489                         }
490                         return;
491                 }
492         }
493         mutex_unlock(&ec->lock);
494 }
495
496 static u32 acpi_ec_gpe_handler(void *data)
497 {
498         acpi_status status = AE_OK;
499         struct acpi_ec *ec = data;
500
501         pr_debug(PREFIX "~~~> interrupt\n");
502         clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
503         if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
504                 wake_up(&ec->wait);
505
506         if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
507                 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
508                         status = acpi_os_execute(OSL_EC_BURST_HANDLER,
509                                 acpi_ec_gpe_query, ec);
510         } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE, &ec->flags))) {
511                 /* this is non-query, must be confirmation */
512                 if (printk_ratelimit())
513                         pr_info(PREFIX "non-query interrupt received,"
514                                 " switching to interrupt mode\n");
515                 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
516         }
517
518         return ACPI_SUCCESS(status) ?
519             ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
520 }
521
522 /* --------------------------------------------------------------------------
523                              Address Space Management
524    -------------------------------------------------------------------------- */
525
526 static acpi_status
527 acpi_ec_space_setup(acpi_handle region_handle,
528                     u32 function, void *handler_context, void **return_context)
529 {
530         /*
531          * The EC object is in the handler context and is needed
532          * when calling the acpi_ec_space_handler.
533          */
534         *return_context = (function != ACPI_REGION_DEACTIVATE) ?
535             handler_context : NULL;
536
537         return AE_OK;
538 }
539
540 static acpi_status
541 acpi_ec_space_handler(u32 function, acpi_physical_address address,
542                       u32 bits, acpi_integer *value,
543                       void *handler_context, void *region_context)
544 {
545         struct acpi_ec *ec = handler_context;
546         int result = 0, i = 0;
547         u8 temp = 0;
548
549         if ((address > 0xFF) || !value || !handler_context)
550                 return AE_BAD_PARAMETER;
551
552         if (function != ACPI_READ && function != ACPI_WRITE)
553                 return AE_BAD_PARAMETER;
554
555         if (bits != 8 && acpi_strict)
556                 return AE_BAD_PARAMETER;
557
558         while (bits - i > 0) {
559                 if (function == ACPI_READ) {
560                         result = acpi_ec_read(ec, address, &temp);
561                         (*value) |= ((acpi_integer)temp) << i;
562                 } else {
563                         temp = 0xff & ((*value) >> i);
564                         result = acpi_ec_write(ec, address, temp);
565                 }
566                 i += 8;
567                 ++address;
568         }
569
570         switch (result) {
571         case -EINVAL:
572                 return AE_BAD_PARAMETER;
573                 break;
574         case -ENODEV:
575                 return AE_NOT_FOUND;
576                 break;
577         case -ETIME:
578                 return AE_TIME;
579                 break;
580         default:
581                 return AE_OK;
582         }
583 }
584
585 /* --------------------------------------------------------------------------
586                               FS Interface (/proc)
587    -------------------------------------------------------------------------- */
588
589 static struct proc_dir_entry *acpi_ec_dir;
590
591 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
592 {
593         struct acpi_ec *ec = seq->private;
594
595         if (!ec)
596                 goto end;
597
598         seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
599         seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
600                    (unsigned)ec->command_addr, (unsigned)ec->data_addr);
601         seq_printf(seq, "use global lock:\t%s\n",
602                    ec->global_lock ? "yes" : "no");
603       end:
604         return 0;
605 }
606
607 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
608 {
609         return single_open(file, acpi_ec_read_info, PDE(inode)->data);
610 }
611
612 static struct file_operations acpi_ec_info_ops = {
613         .open = acpi_ec_info_open_fs,
614         .read = seq_read,
615         .llseek = seq_lseek,
616         .release = single_release,
617         .owner = THIS_MODULE,
618 };
619
620 static int acpi_ec_add_fs(struct acpi_device *device)
621 {
622         struct proc_dir_entry *entry = NULL;
623
624         if (!acpi_device_dir(device)) {
625                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
626                                                      acpi_ec_dir);
627                 if (!acpi_device_dir(device))
628                         return -ENODEV;
629         }
630
631         entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
632                                   acpi_device_dir(device));
633         if (!entry)
634                 return -ENODEV;
635         else {
636                 entry->proc_fops = &acpi_ec_info_ops;
637                 entry->data = acpi_driver_data(device);
638                 entry->owner = THIS_MODULE;
639         }
640
641         return 0;
642 }
643
644 static int acpi_ec_remove_fs(struct acpi_device *device)
645 {
646
647         if (acpi_device_dir(device)) {
648                 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
649                 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
650                 acpi_device_dir(device) = NULL;
651         }
652
653         return 0;
654 }
655
656 /* --------------------------------------------------------------------------
657                                Driver Interface
658    -------------------------------------------------------------------------- */
659 static acpi_status
660 ec_parse_io_ports(struct acpi_resource *resource, void *context);
661
662 static struct acpi_ec *make_acpi_ec(void)
663 {
664         struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
665         if (!ec)
666                 return NULL;
667         ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
668         mutex_init(&ec->lock);
669         init_waitqueue_head(&ec->wait);
670         INIT_LIST_HEAD(&ec->list);
671         return ec;
672 }
673
674 static acpi_status
675 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
676                                void *context, void **return_value)
677 {
678         struct acpi_namespace_node *node = handle;
679         struct acpi_ec *ec = context;
680         int value = 0;
681         if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
682                 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
683         }
684         return AE_OK;
685 }
686
687 static acpi_status
688 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
689 {
690         acpi_status status;
691
692         struct acpi_ec *ec = context;
693         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
694                                      ec_parse_io_ports, ec);
695         if (ACPI_FAILURE(status))
696                 return status;
697
698         /* Get GPE bit assignment (EC events). */
699         /* TODO: Add support for _GPE returning a package */
700         status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
701         if (ACPI_FAILURE(status))
702                 return status;
703         /* Find and register all query methods */
704         acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
705                             acpi_ec_register_query_methods, ec, NULL);
706         /* Use the global lock for all EC transactions? */
707         acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
708         ec->handle = handle;
709         return AE_CTRL_TERMINATE;
710 }
711
712 static void ec_remove_handlers(struct acpi_ec *ec)
713 {
714         if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
715                                 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
716                 pr_err(PREFIX "failed to remove space handler\n");
717         if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
718                                 &acpi_ec_gpe_handler)))
719                 pr_err(PREFIX "failed to remove gpe handler\n");
720         ec->handlers_installed = 0;
721 }
722
723 static int acpi_ec_add(struct acpi_device *device)
724 {
725         struct acpi_ec *ec = NULL;
726
727         if (!device)
728                 return -EINVAL;
729         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
730         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
731
732         /* Check for boot EC */
733         if (boot_ec) {
734                 if (boot_ec->handle == device->handle) {
735                         /* Pre-loaded EC from DSDT, just move pointer */
736                         ec = boot_ec;
737                         boot_ec = NULL;
738                         goto end;
739                 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
740                         /* ECDT-based EC, time to shut it down */
741                         ec_remove_handlers(boot_ec);
742                         kfree(boot_ec);
743                         first_ec = boot_ec = NULL;
744                 }
745         }
746
747         ec = make_acpi_ec();
748         if (!ec)
749                 return -ENOMEM;
750
751         if (ec_parse_device(device->handle, 0, ec, NULL) !=
752             AE_CTRL_TERMINATE) {
753                 kfree(ec);
754                 return -EINVAL;
755         }
756         ec->handle = device->handle;
757       end:
758         if (!first_ec)
759                 first_ec = ec;
760         acpi_driver_data(device) = ec;
761         acpi_ec_add_fs(device);
762         pr_info(PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
763                           ec->gpe, ec->command_addr, ec->data_addr);
764         pr_info(PREFIX "driver started in %s mode\n",
765                 (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))?"interrupt":"poll");
766         return 0;
767 }
768
769 static int acpi_ec_remove(struct acpi_device *device, int type)
770 {
771         struct acpi_ec *ec;
772         struct acpi_ec_query_handler *handler, *tmp;
773
774         if (!device)
775                 return -EINVAL;
776
777         ec = acpi_driver_data(device);
778         mutex_lock(&ec->lock);
779         list_for_each_entry_safe(handler, tmp, &ec->list, node) {
780                 list_del(&handler->node);
781                 kfree(handler);
782         }
783         mutex_unlock(&ec->lock);
784         acpi_ec_remove_fs(device);
785         acpi_driver_data(device) = NULL;
786         if (ec == first_ec)
787                 first_ec = NULL;
788         kfree(ec);
789         return 0;
790 }
791
792 static acpi_status
793 ec_parse_io_ports(struct acpi_resource *resource, void *context)
794 {
795         struct acpi_ec *ec = context;
796
797         if (resource->type != ACPI_RESOURCE_TYPE_IO)
798                 return AE_OK;
799
800         /*
801          * The first address region returned is the data port, and
802          * the second address region returned is the status/command
803          * port.
804          */
805         if (ec->data_addr == 0)
806                 ec->data_addr = resource->data.io.minimum;
807         else if (ec->command_addr == 0)
808                 ec->command_addr = resource->data.io.minimum;
809         else
810                 return AE_CTRL_TERMINATE;
811
812         return AE_OK;
813 }
814
815 static int ec_install_handlers(struct acpi_ec *ec)
816 {
817         acpi_status status;
818         if (ec->handlers_installed)
819                 return 0;
820         status = acpi_install_gpe_handler(NULL, ec->gpe,
821                                           ACPI_GPE_EDGE_TRIGGERED,
822                                           &acpi_ec_gpe_handler, ec);
823         if (ACPI_FAILURE(status))
824                 return -ENODEV;
825
826         acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
827         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
828
829         status = acpi_install_address_space_handler(ec->handle,
830                                                     ACPI_ADR_SPACE_EC,
831                                                     &acpi_ec_space_handler,
832                                                     &acpi_ec_space_setup, ec);
833         if (ACPI_FAILURE(status)) {
834                 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
835                 return -ENODEV;
836         }
837
838         ec->handlers_installed = 1;
839         return 0;
840 }
841
842 static int acpi_ec_start(struct acpi_device *device)
843 {
844         struct acpi_ec *ec;
845         int ret = 0;
846
847         if (!device)
848                 return -EINVAL;
849
850         ec = acpi_driver_data(device);
851
852         if (!ec)
853                 return -EINVAL;
854
855         ret = ec_install_handlers(ec);
856
857         /* EC is fully operational, allow queries */
858         clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
859         return ret;
860 }
861
862 static int acpi_ec_stop(struct acpi_device *device, int type)
863 {
864         struct acpi_ec *ec;
865         if (!device)
866                 return -EINVAL;
867         ec = acpi_driver_data(device);
868         if (!ec)
869                 return -EINVAL;
870         ec_remove_handlers(ec);
871
872         return 0;
873 }
874
875 int __init acpi_ec_ecdt_probe(void)
876 {
877         int ret;
878         acpi_status status;
879         struct acpi_table_ecdt *ecdt_ptr;
880
881         boot_ec = make_acpi_ec();
882         if (!boot_ec)
883                 return -ENOMEM;
884         /*
885          * Generate a boot ec context
886          */
887         status = acpi_get_table(ACPI_SIG_ECDT, 1,
888                                 (struct acpi_table_header **)&ecdt_ptr);
889         if (ACPI_SUCCESS(status)) {
890                 pr_info(PREFIX "EC description table is found, configuring boot EC\n");
891                 boot_ec->command_addr = ecdt_ptr->control.address;
892                 boot_ec->data_addr = ecdt_ptr->data.address;
893                 boot_ec->gpe = ecdt_ptr->gpe;
894                 boot_ec->handle = ACPI_ROOT_OBJECT;
895         } else {
896                 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
897                 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
898                                                 boot_ec, NULL);
899                 /* Check that acpi_get_devices actually find something */
900                 if (ACPI_FAILURE(status) || !boot_ec->handle)
901                         goto error;
902         }
903
904         ret = ec_install_handlers(boot_ec);
905         if (!ret) {
906                 first_ec = boot_ec;
907                 return 0;
908         }
909       error:
910         kfree(boot_ec);
911         boot_ec = NULL;
912         return -ENODEV;
913 }
914
915 static int __init acpi_ec_init(void)
916 {
917         int result = 0;
918
919         if (acpi_disabled)
920                 return 0;
921
922         acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
923         if (!acpi_ec_dir)
924                 return -ENODEV;
925
926         /* Now register the driver for the EC */
927         result = acpi_bus_register_driver(&acpi_ec_driver);
928         if (result < 0) {
929                 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
930                 return -ENODEV;
931         }
932
933         return result;
934 }
935
936 subsys_initcall(acpi_ec_init);
937
938 /* EC driver currently not unloadable */
939 #if 0
940 static void __exit acpi_ec_exit(void)
941 {
942
943         acpi_bus_unregister_driver(&acpi_ec_driver);
944
945         remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
946
947         return;
948 }
949 #endif  /* 0 */