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