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