ACPI: EC: Drop ECDT-based boot_ec as soon as we find DSDT-based one.
[safe/jmp/linux-2.6] / drivers / acpi / ec.c
1 /*
2  *  ec.c - ACPI Embedded Controller Driver (v2.0)
3  *
4  *  Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5  *  Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
6  *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
7  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
9  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or (at
15  *  your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful, but
18  *  WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  *  General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License along
23  *  with this program; if not, write to the Free Software Foundation, Inc.,
24  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25  *
26  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27  */
28
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/types.h>
33 #include <linux/delay.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36 #include <linux/interrupt.h>
37 #include <linux/list.h>
38 #include <asm/io.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.h>
41 #include <acpi/actypes.h>
42
43 #define ACPI_EC_CLASS                   "embedded_controller"
44 #define ACPI_EC_DEVICE_NAME             "Embedded Controller"
45 #define ACPI_EC_FILE_INFO               "info"
46
47 #undef PREFIX
48 #define PREFIX                          "ACPI: EC: "
49
50 /* 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         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                                        unsigned old_count)
153 {
154         u8 status = acpi_ec_read_status(ec);
155         if (old_count == atomic_read(&ec->event_count))
156                 return 0;
157         if (event == ACPI_EC_EVENT_OBF_1) {
158                 if (status & ACPI_EC_FLAG_OBF)
159                         return 1;
160         } else if (event == ACPI_EC_EVENT_IBF_0) {
161                 if (!(status & ACPI_EC_FLAG_IBF))
162                         return 1;
163         }
164
165         return 0;
166 }
167
168 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event,
169                         unsigned count, int force_poll)
170 {
171         if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) {
172                 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
173                 while (time_before(jiffies, delay)) {
174                         if (acpi_ec_check_status(ec, event, 0))
175                                 return 0;
176                 }
177         } else {
178                 if (wait_event_timeout(ec->wait,
179                                        acpi_ec_check_status(ec, event, count),
180                                        msecs_to_jiffies(ACPI_EC_DELAY)) ||
181                     acpi_ec_check_status(ec, event, 0)) {
182                         return 0;
183                 } else {
184                         printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
185                                " status = %d, expect_event = %d\n",
186                                acpi_ec_read_status(ec), event);
187                 }
188         }
189
190         return -ETIME;
191 }
192
193 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
194                                         const u8 * wdata, unsigned wdata_len,
195                                         u8 * rdata, unsigned rdata_len,
196                                         int force_poll)
197 {
198         int result = 0;
199         unsigned count = atomic_read(&ec->event_count);
200         acpi_ec_write_cmd(ec, command);
201
202         for (; wdata_len > 0; --wdata_len) {
203                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
204                 if (result) {
205                         printk(KERN_ERR PREFIX
206                                "write_cmd timeout, command = %d\n", command);
207                         goto end;
208                 }
209                 count = atomic_read(&ec->event_count);
210                 acpi_ec_write_data(ec, *(wdata++));
211         }
212
213         if (!rdata_len) {
214                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
215                 if (result) {
216                         printk(KERN_ERR PREFIX
217                                "finish-write timeout, command = %d\n", command);
218                         goto end;
219                 }
220         } else if (command == ACPI_EC_COMMAND_QUERY) {
221                 atomic_set(&ec->query_pending, 0);
222         }
223
224         for (; rdata_len > 0; --rdata_len) {
225                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count, force_poll);
226                 if (result) {
227                         printk(KERN_ERR PREFIX "read timeout, command = %d\n",
228                                command);
229                         goto end;
230                 }
231                 count = atomic_read(&ec->event_count);
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, 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_tail(&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                         break;
445                 }
446         }
447         mutex_unlock(&ec->lock);
448 }
449
450 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
451
452 static void acpi_ec_gpe_query(void *ec_cxt)
453 {
454         struct acpi_ec *ec = ec_cxt;
455         u8 value = 0;
456         struct acpi_ec_query_handler *handler, copy;
457
458         if (!ec || acpi_ec_query(ec, &value))
459                 return;
460         mutex_lock(&ec->lock);
461         list_for_each_entry(handler, &ec->list, node) {
462                 if (value == handler->query_bit) {
463                         /* have custom handler for this bit */
464                         memcpy(&copy, handler, sizeof(copy));
465                         mutex_unlock(&ec->lock);
466                         if (copy.func) {
467                                 copy.func(copy.data);
468                         } else if (copy.handle) {
469                                 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
470                         }
471                         return;
472                 }
473         }
474         mutex_unlock(&ec->lock);
475 }
476
477 static u32 acpi_ec_gpe_handler(void *data)
478 {
479         acpi_status status = AE_OK;
480         u8 value;
481         struct acpi_ec *ec = data;
482
483         atomic_inc(&ec->event_count);
484
485         if (acpi_ec_mode == EC_INTR) {
486                 wake_up(&ec->wait);
487         }
488
489         value = acpi_ec_read_status(ec);
490         if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
491                 atomic_set(&ec->query_pending, 1);
492                 status =
493                     acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
494         }
495
496         return status == AE_OK ?
497             ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
498 }
499
500 /* --------------------------------------------------------------------------
501                              Address Space Management
502    -------------------------------------------------------------------------- */
503
504 static acpi_status
505 acpi_ec_space_setup(acpi_handle region_handle,
506                     u32 function, void *handler_context, void **return_context)
507 {
508         /*
509          * The EC object is in the handler context and is needed
510          * when calling the acpi_ec_space_handler.
511          */
512         *return_context = (function != ACPI_REGION_DEACTIVATE) ?
513             handler_context : NULL;
514
515         return AE_OK;
516 }
517
518 static acpi_status
519 acpi_ec_space_handler(u32 function, acpi_physical_address address,
520                       u32 bits, acpi_integer *value,
521                       void *handler_context, void *region_context)
522 {
523         struct acpi_ec *ec = handler_context;
524         int result = 0, i = 0;
525         u8 temp = 0;
526
527         if ((address > 0xFF) || !value || !handler_context)
528                 return AE_BAD_PARAMETER;
529
530         if (function != ACPI_READ && function != ACPI_WRITE)
531                 return AE_BAD_PARAMETER;
532
533         if (bits != 8 && acpi_strict)
534                 return AE_BAD_PARAMETER;
535
536         while (bits - i > 0) {
537                 if (function == ACPI_READ) {
538                         result = acpi_ec_read(ec, address, &temp);
539                         (*value) |= ((acpi_integer)temp) << i;
540                 } else {
541                         temp = 0xff & ((*value) >> i);
542                         result = acpi_ec_write(ec, address, temp);
543                 }
544                 i += 8;
545                 ++address;
546         }
547
548         switch (result) {
549         case -EINVAL:
550                 return AE_BAD_PARAMETER;
551                 break;
552         case -ENODEV:
553                 return AE_NOT_FOUND;
554                 break;
555         case -ETIME:
556                 return AE_TIME;
557                 break;
558         default:
559                 return AE_OK;
560         }
561 }
562
563 /* --------------------------------------------------------------------------
564                               FS Interface (/proc)
565    -------------------------------------------------------------------------- */
566
567 static struct proc_dir_entry *acpi_ec_dir;
568
569 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
570 {
571         struct acpi_ec *ec = seq->private;
572
573         if (!ec)
574                 goto end;
575
576         seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
577         seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
578                    (unsigned)ec->command_addr, (unsigned)ec->data_addr);
579         seq_printf(seq, "use global lock:\t%s\n",
580                    ec->global_lock ? "yes" : "no");
581       end:
582         return 0;
583 }
584
585 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
586 {
587         return single_open(file, acpi_ec_read_info, PDE(inode)->data);
588 }
589
590 static struct file_operations acpi_ec_info_ops = {
591         .open = acpi_ec_info_open_fs,
592         .read = seq_read,
593         .llseek = seq_lseek,
594         .release = single_release,
595         .owner = THIS_MODULE,
596 };
597
598 static int acpi_ec_add_fs(struct acpi_device *device)
599 {
600         struct proc_dir_entry *entry = NULL;
601
602         if (!acpi_device_dir(device)) {
603                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
604                                                      acpi_ec_dir);
605                 if (!acpi_device_dir(device))
606                         return -ENODEV;
607         }
608
609         entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
610                                   acpi_device_dir(device));
611         if (!entry)
612                 return -ENODEV;
613         else {
614                 entry->proc_fops = &acpi_ec_info_ops;
615                 entry->data = acpi_driver_data(device);
616                 entry->owner = THIS_MODULE;
617         }
618
619         return 0;
620 }
621
622 static int acpi_ec_remove_fs(struct acpi_device *device)
623 {
624
625         if (acpi_device_dir(device)) {
626                 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
627                 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
628                 acpi_device_dir(device) = NULL;
629         }
630
631         return 0;
632 }
633
634 /* --------------------------------------------------------------------------
635                                Driver Interface
636    -------------------------------------------------------------------------- */
637 static acpi_status
638 ec_parse_io_ports(struct acpi_resource *resource, void *context);
639
640 static struct acpi_ec *make_acpi_ec(void)
641 {
642         struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
643         if (!ec)
644                 return NULL;
645
646         atomic_set(&ec->query_pending, 1);
647         atomic_set(&ec->event_count, 1);
648         mutex_init(&ec->lock);
649         init_waitqueue_head(&ec->wait);
650         INIT_LIST_HEAD(&ec->list);
651
652         return ec;
653 }
654
655 static acpi_status
656 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
657                                void *context, void **return_value)
658 {
659         struct acpi_namespace_node *node = handle;
660         struct acpi_ec *ec = context;
661         int value = 0;
662         if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
663                 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
664         }
665         return AE_OK;
666 }
667
668 static acpi_status
669 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
670 {
671         acpi_status status;
672
673         struct acpi_ec *ec = context;
674         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
675                                      ec_parse_io_ports, ec);
676         if (ACPI_FAILURE(status))
677                 return status;
678
679         /* Get GPE bit assignment (EC events). */
680         /* TODO: Add support for _GPE returning a package */
681         status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
682         if (ACPI_FAILURE(status))
683                 return status;
684         /* Find and register all query methods */
685         acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
686                             acpi_ec_register_query_methods, ec, NULL);
687         /* Use the global lock for all EC transactions? */
688         acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
689         ec->handle = handle;
690         return AE_CTRL_TERMINATE;
691 }
692
693 static void ec_remove_handlers(struct acpi_ec *ec)
694 {
695         if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
696                                 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
697                 printk(KERN_ERR PREFIX "failed to remove space handler\n");
698         if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
699                                 &acpi_ec_gpe_handler)))
700                 printk(KERN_ERR PREFIX "failed to remove gpe handler\n");
701         ec->handlers_installed = 0;
702 }
703
704 static int acpi_ec_add(struct acpi_device *device)
705 {
706         struct acpi_ec *ec = NULL;
707
708         if (!device)
709                 return -EINVAL;
710         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
711         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
712
713         /* Check for boot EC */
714         if (boot_ec) {
715                 if (boot_ec->handle == device->handle) {
716                         /* Pre-loaded EC from DSDT, just move pointer */
717                         ec = boot_ec;
718                         boot_ec = NULL;
719                         goto end;
720                 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
721                         /* ECDT-based EC, time to shut it down */
722                         ec_remove_handlers(boot_ec);
723                         kfree(boot_ec);
724                         first_ec = boot_ec = NULL;
725                 }
726         }
727
728         ec = make_acpi_ec();
729         if (!ec)
730                 return -ENOMEM;
731
732         if (ec_parse_device(device->handle, 0, ec, NULL) !=
733             AE_CTRL_TERMINATE) {
734                 kfree(ec);
735                 return -EINVAL;
736         }
737         ec->handle = device->handle;
738       end:
739         if (!first_ec)
740                 first_ec = ec;
741         acpi_driver_data(device) = ec;
742         acpi_ec_add_fs(device);
743         printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
744                           ec->gpe, ec->command_addr, ec->data_addr);
745         return 0;
746 }
747
748 static int acpi_ec_remove(struct acpi_device *device, int type)
749 {
750         struct acpi_ec *ec;
751         struct acpi_ec_query_handler *handler, *tmp;
752
753         if (!device)
754                 return -EINVAL;
755
756         ec = acpi_driver_data(device);
757         mutex_lock(&ec->lock);
758         list_for_each_entry_safe(handler, tmp, &ec->list, node) {
759                 list_del(&handler->node);
760                 kfree(handler);
761         }
762         mutex_unlock(&ec->lock);
763         acpi_ec_remove_fs(device);
764         acpi_driver_data(device) = NULL;
765         if (ec == first_ec)
766                 first_ec = NULL;
767         kfree(ec);
768         return 0;
769 }
770
771 static acpi_status
772 ec_parse_io_ports(struct acpi_resource *resource, void *context)
773 {
774         struct acpi_ec *ec = context;
775
776         if (resource->type != ACPI_RESOURCE_TYPE_IO)
777                 return AE_OK;
778
779         /*
780          * The first address region returned is the data port, and
781          * the second address region returned is the status/command
782          * port.
783          */
784         if (ec->data_addr == 0)
785                 ec->data_addr = resource->data.io.minimum;
786         else if (ec->command_addr == 0)
787                 ec->command_addr = resource->data.io.minimum;
788         else
789                 return AE_CTRL_TERMINATE;
790
791         return AE_OK;
792 }
793
794 static int ec_install_handlers(struct acpi_ec *ec)
795 {
796         acpi_status status;
797         if (ec->handlers_installed)
798                 return 0;
799         status = acpi_install_gpe_handler(NULL, ec->gpe,
800                                           ACPI_GPE_EDGE_TRIGGERED,
801                                           &acpi_ec_gpe_handler, ec);
802         if (ACPI_FAILURE(status))
803                 return -ENODEV;
804
805         acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
806         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
807
808         status = acpi_install_address_space_handler(ec->handle,
809                                                     ACPI_ADR_SPACE_EC,
810                                                     &acpi_ec_space_handler,
811                                                     &acpi_ec_space_setup, ec);
812         if (ACPI_FAILURE(status)) {
813                 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
814                 return -ENODEV;
815         }
816
817         ec->handlers_installed = 1;
818         return 0;
819 }
820
821 static int acpi_ec_start(struct acpi_device *device)
822 {
823         struct acpi_ec *ec;
824         int ret = 0;
825
826         if (!device)
827                 return -EINVAL;
828
829         ec = acpi_driver_data(device);
830
831         if (!ec)
832                 return -EINVAL;
833
834         ret = ec_install_handlers(ec);
835
836         /* EC is fully operational, allow queries */
837         atomic_set(&ec->query_pending, 0);
838         return ret;
839 }
840
841 static int acpi_ec_stop(struct acpi_device *device, int type)
842 {
843         struct acpi_ec *ec;
844         if (!device)
845                 return -EINVAL;
846         ec = acpi_driver_data(device);
847         if (!ec)
848                 return -EINVAL;
849         ec_remove_handlers(ec);
850
851         return 0;
852 }
853
854 int __init acpi_ec_ecdt_probe(void)
855 {
856         int ret;
857         acpi_status status;
858         struct acpi_table_ecdt *ecdt_ptr;
859
860         boot_ec = make_acpi_ec();
861         if (!boot_ec)
862                 return -ENOMEM;
863         /*
864          * Generate a boot ec context
865          */
866         status = acpi_get_table(ACPI_SIG_ECDT, 1,
867                                 (struct acpi_table_header **)&ecdt_ptr);
868         if (ACPI_SUCCESS(status)) {
869                 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
870                 boot_ec->command_addr = ecdt_ptr->control.address;
871                 boot_ec->data_addr = ecdt_ptr->data.address;
872                 boot_ec->gpe = ecdt_ptr->gpe;
873                 boot_ec->handle = ACPI_ROOT_OBJECT;
874         } else {
875                 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
876                 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
877                                                 boot_ec, NULL);
878                 /* Check that acpi_get_devices actually find something */
879                 if (ACPI_FAILURE(status) || !boot_ec->handle)
880                         goto error;
881         }
882
883         ret = ec_install_handlers(boot_ec);
884         if (!ret) {
885                 first_ec = boot_ec;
886                 return 0;
887         }
888       error:
889         kfree(boot_ec);
890         boot_ec = NULL;
891         return -ENODEV;
892 }
893
894 static int __init acpi_ec_init(void)
895 {
896         int result = 0;
897
898         if (acpi_disabled)
899                 return 0;
900
901         acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
902         if (!acpi_ec_dir)
903                 return -ENODEV;
904
905         /* Now register the driver for the EC */
906         result = acpi_bus_register_driver(&acpi_ec_driver);
907         if (result < 0) {
908                 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
909                 return -ENODEV;
910         }
911
912         return result;
913 }
914
915 subsys_initcall(acpi_ec_init);
916
917 /* EC driver currently not unloadable */
918 #if 0
919 static void __exit acpi_ec_exit(void)
920 {
921
922         acpi_bus_unregister_driver(&acpi_ec_driver);
923
924         remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
925
926         return;
927 }
928 #endif                          /* 0 */
929
930 static int __init acpi_ec_set_intr_mode(char *str)
931 {
932         int intr;
933
934         if (!get_option(&str, &intr))
935                 return 0;
936
937         acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
938
939         printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
940
941         return 1;
942 }
943
944 __setup("ec_intr=", acpi_ec_set_intr_mode);