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