]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/acpi/ec.c
ACPI: EC: Replace atomic variables with bits
[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 enum {
80         EC_FLAGS_WAIT_GPE = 0,          /* Don't check status until GPE arrives */
81         EC_FLAGS_QUERY_PENDING,         /* Query is pending */
82 };
83
84 static int acpi_ec_remove(struct acpi_device *device, int type);
85 static int acpi_ec_start(struct acpi_device *device);
86 static int acpi_ec_stop(struct acpi_device *device, int type);
87 static int acpi_ec_add(struct acpi_device *device);
88
89 static const struct acpi_device_id ec_device_ids[] = {
90         {"PNP0C09", 0},
91         {"", 0},
92 };
93
94 static struct acpi_driver acpi_ec_driver = {
95         .name = "ec",
96         .class = ACPI_EC_CLASS,
97         .ids = ec_device_ids,
98         .ops = {
99                 .add = acpi_ec_add,
100                 .remove = acpi_ec_remove,
101                 .start = acpi_ec_start,
102                 .stop = acpi_ec_stop,
103                 },
104 };
105
106 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
107 /* External interfaces use first EC only, so remember */
108 typedef int (*acpi_ec_query_func) (void *data);
109
110 struct acpi_ec_query_handler {
111         struct list_head node;
112         acpi_ec_query_func func;
113         acpi_handle handle;
114         void *data;
115         u8 query_bit;
116 };
117
118 static struct acpi_ec {
119         acpi_handle handle;
120         unsigned long gpe;
121         unsigned long command_addr;
122         unsigned long data_addr;
123         unsigned long global_lock;
124         unsigned long flags;
125         struct mutex lock;
126         wait_queue_head_t wait;
127         struct list_head list;
128         u8 handlers_installed;
129 } *boot_ec, *first_ec;
130
131 /* --------------------------------------------------------------------------
132                              Transaction Management
133    -------------------------------------------------------------------------- */
134
135 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
136 {
137         return inb(ec->command_addr);
138 }
139
140 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
141 {
142         return inb(ec->data_addr);
143 }
144
145 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
146 {
147         outb(command, ec->command_addr);
148 }
149
150 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
151 {
152         outb(data, ec->data_addr);
153 }
154
155 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
156 {
157         if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
158                 return 0;
159         if (event == ACPI_EC_EVENT_OBF_1) {
160                 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
161                         return 1;
162         } else if (event == ACPI_EC_EVENT_IBF_0) {
163                 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
164                         return 1;
165         }
166
167         return 0;
168 }
169
170 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
171 {
172         if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) {
173                 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
174                 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
175                 while (time_before(jiffies, delay)) {
176                         if (acpi_ec_check_status(ec, event))
177                                 return 0;
178                 }
179         } else {
180                 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
181                                        msecs_to_jiffies(ACPI_EC_DELAY)))
182                         return 0;
183                 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
184                 if (acpi_ec_check_status(ec, event)) {
185                         return 0;
186                 }
187         }
188         printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
189                                " status = %d, expect_event = %d\n",
190                                acpi_ec_read_status(ec), event);
191         return -ETIME;
192 }
193
194 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
195                                         const u8 * wdata, unsigned wdata_len,
196                                         u8 * rdata, unsigned rdata_len,
197                                         int force_poll)
198 {
199         int result = 0;
200         set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
201         acpi_ec_write_cmd(ec, command);
202
203         for (; wdata_len > 0; --wdata_len) {
204                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
205                 if (result) {
206                         printk(KERN_ERR PREFIX
207                                "write_cmd timeout, command = %d\n", command);
208                         goto end;
209                 }
210                 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
211                 acpi_ec_write_data(ec, *(wdata++));
212         }
213
214         if (!rdata_len) {
215                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
216                 if (result) {
217                         printk(KERN_ERR PREFIX
218                                "finish-write timeout, command = %d\n", command);
219                         goto end;
220                 }
221         } else if (command == ACPI_EC_COMMAND_QUERY)
222                 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
223
224         for (; rdata_len > 0; --rdata_len) {
225                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
226                 if (result) {
227                         printk(KERN_ERR PREFIX "read timeout, command = %d\n",
228                                command);
229                         goto end;
230                 }
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
483         if (acpi_ec_mode == EC_INTR) {
484                 wake_up(&ec->wait);
485         }
486
487         if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
488                 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
489                         status = acpi_os_execute(OSL_EC_BURST_HANDLER,
490                                 acpi_ec_gpe_query, ec);
491         }
492
493         return status == AE_OK ?
494             ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
495 }
496
497 /* --------------------------------------------------------------------------
498                              Address Space Management
499    -------------------------------------------------------------------------- */
500
501 static acpi_status
502 acpi_ec_space_setup(acpi_handle region_handle,
503                     u32 function, void *handler_context, void **return_context)
504 {
505         /*
506          * The EC object is in the handler context and is needed
507          * when calling the acpi_ec_space_handler.
508          */
509         *return_context = (function != ACPI_REGION_DEACTIVATE) ?
510             handler_context : NULL;
511
512         return AE_OK;
513 }
514
515 static acpi_status
516 acpi_ec_space_handler(u32 function, acpi_physical_address address,
517                       u32 bits, acpi_integer *value,
518                       void *handler_context, void *region_context)
519 {
520         struct acpi_ec *ec = handler_context;
521         int result = 0, i = 0;
522         u8 temp = 0;
523
524         if ((address > 0xFF) || !value || !handler_context)
525                 return AE_BAD_PARAMETER;
526
527         if (function != ACPI_READ && function != ACPI_WRITE)
528                 return AE_BAD_PARAMETER;
529
530         if (bits != 8 && acpi_strict)
531                 return AE_BAD_PARAMETER;
532
533         while (bits - i > 0) {
534                 if (function == ACPI_READ) {
535                         result = acpi_ec_read(ec, address, &temp);
536                         (*value) |= ((acpi_integer)temp) << i;
537                 } else {
538                         temp = 0xff & ((*value) >> i);
539                         result = acpi_ec_write(ec, address, temp);
540                 }
541                 i += 8;
542                 ++address;
543         }
544
545         switch (result) {
546         case -EINVAL:
547                 return AE_BAD_PARAMETER;
548                 break;
549         case -ENODEV:
550                 return AE_NOT_FOUND;
551                 break;
552         case -ETIME:
553                 return AE_TIME;
554                 break;
555         default:
556                 return AE_OK;
557         }
558 }
559
560 /* --------------------------------------------------------------------------
561                               FS Interface (/proc)
562    -------------------------------------------------------------------------- */
563
564 static struct proc_dir_entry *acpi_ec_dir;
565
566 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
567 {
568         struct acpi_ec *ec = seq->private;
569
570         if (!ec)
571                 goto end;
572
573         seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
574         seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
575                    (unsigned)ec->command_addr, (unsigned)ec->data_addr);
576         seq_printf(seq, "use global lock:\t%s\n",
577                    ec->global_lock ? "yes" : "no");
578       end:
579         return 0;
580 }
581
582 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
583 {
584         return single_open(file, acpi_ec_read_info, PDE(inode)->data);
585 }
586
587 static struct file_operations acpi_ec_info_ops = {
588         .open = acpi_ec_info_open_fs,
589         .read = seq_read,
590         .llseek = seq_lseek,
591         .release = single_release,
592         .owner = THIS_MODULE,
593 };
594
595 static int acpi_ec_add_fs(struct acpi_device *device)
596 {
597         struct proc_dir_entry *entry = NULL;
598
599         if (!acpi_device_dir(device)) {
600                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
601                                                      acpi_ec_dir);
602                 if (!acpi_device_dir(device))
603                         return -ENODEV;
604         }
605
606         entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
607                                   acpi_device_dir(device));
608         if (!entry)
609                 return -ENODEV;
610         else {
611                 entry->proc_fops = &acpi_ec_info_ops;
612                 entry->data = acpi_driver_data(device);
613                 entry->owner = THIS_MODULE;
614         }
615
616         return 0;
617 }
618
619 static int acpi_ec_remove_fs(struct acpi_device *device)
620 {
621
622         if (acpi_device_dir(device)) {
623                 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
624                 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
625                 acpi_device_dir(device) = NULL;
626         }
627
628         return 0;
629 }
630
631 /* --------------------------------------------------------------------------
632                                Driver Interface
633    -------------------------------------------------------------------------- */
634 static acpi_status
635 ec_parse_io_ports(struct acpi_resource *resource, void *context);
636
637 static struct acpi_ec *make_acpi_ec(void)
638 {
639         struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
640         if (!ec)
641                 return NULL;
642
643         ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
644         mutex_init(&ec->lock);
645         init_waitqueue_head(&ec->wait);
646         INIT_LIST_HEAD(&ec->list);
647
648         return ec;
649 }
650
651 static acpi_status
652 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
653                                void *context, void **return_value)
654 {
655         struct acpi_namespace_node *node = handle;
656         struct acpi_ec *ec = context;
657         int value = 0;
658         if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
659                 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
660         }
661         return AE_OK;
662 }
663
664 static acpi_status
665 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
666 {
667         acpi_status status;
668
669         struct acpi_ec *ec = context;
670         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
671                                      ec_parse_io_ports, ec);
672         if (ACPI_FAILURE(status))
673                 return status;
674
675         /* Get GPE bit assignment (EC events). */
676         /* TODO: Add support for _GPE returning a package */
677         status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
678         if (ACPI_FAILURE(status))
679                 return status;
680         /* Find and register all query methods */
681         acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
682                             acpi_ec_register_query_methods, ec, NULL);
683         /* Use the global lock for all EC transactions? */
684         acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
685         ec->handle = handle;
686         return AE_CTRL_TERMINATE;
687 }
688
689 static void ec_remove_handlers(struct acpi_ec *ec)
690 {
691         if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
692                                 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
693                 printk(KERN_ERR PREFIX "failed to remove space handler\n");
694         if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
695                                 &acpi_ec_gpe_handler)))
696                 printk(KERN_ERR PREFIX "failed to remove gpe handler\n");
697         ec->handlers_installed = 0;
698 }
699
700 static int acpi_ec_add(struct acpi_device *device)
701 {
702         struct acpi_ec *ec = NULL;
703
704         if (!device)
705                 return -EINVAL;
706         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
707         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
708
709         /* Check for boot EC */
710         if (boot_ec) {
711                 if (boot_ec->handle == device->handle) {
712                         /* Pre-loaded EC from DSDT, just move pointer */
713                         ec = boot_ec;
714                         boot_ec = NULL;
715                         goto end;
716                 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
717                         /* ECDT-based EC, time to shut it down */
718                         ec_remove_handlers(boot_ec);
719                         kfree(boot_ec);
720                         first_ec = boot_ec = NULL;
721                 }
722         }
723
724         ec = make_acpi_ec();
725         if (!ec)
726                 return -ENOMEM;
727
728         if (ec_parse_device(device->handle, 0, ec, NULL) !=
729             AE_CTRL_TERMINATE) {
730                 kfree(ec);
731                 return -EINVAL;
732         }
733         ec->handle = device->handle;
734       end:
735         if (!first_ec)
736                 first_ec = ec;
737         acpi_driver_data(device) = ec;
738         acpi_ec_add_fs(device);
739         printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
740                           ec->gpe, ec->command_addr, ec->data_addr);
741         return 0;
742 }
743
744 static int acpi_ec_remove(struct acpi_device *device, int type)
745 {
746         struct acpi_ec *ec;
747         struct acpi_ec_query_handler *handler, *tmp;
748
749         if (!device)
750                 return -EINVAL;
751
752         ec = acpi_driver_data(device);
753         mutex_lock(&ec->lock);
754         list_for_each_entry_safe(handler, tmp, &ec->list, node) {
755                 list_del(&handler->node);
756                 kfree(handler);
757         }
758         mutex_unlock(&ec->lock);
759         acpi_ec_remove_fs(device);
760         acpi_driver_data(device) = NULL;
761         if (ec == first_ec)
762                 first_ec = NULL;
763         kfree(ec);
764         return 0;
765 }
766
767 static acpi_status
768 ec_parse_io_ports(struct acpi_resource *resource, void *context)
769 {
770         struct acpi_ec *ec = context;
771
772         if (resource->type != ACPI_RESOURCE_TYPE_IO)
773                 return AE_OK;
774
775         /*
776          * The first address region returned is the data port, and
777          * the second address region returned is the status/command
778          * port.
779          */
780         if (ec->data_addr == 0)
781                 ec->data_addr = resource->data.io.minimum;
782         else if (ec->command_addr == 0)
783                 ec->command_addr = resource->data.io.minimum;
784         else
785                 return AE_CTRL_TERMINATE;
786
787         return AE_OK;
788 }
789
790 static int ec_install_handlers(struct acpi_ec *ec)
791 {
792         acpi_status status;
793         if (ec->handlers_installed)
794                 return 0;
795         status = acpi_install_gpe_handler(NULL, ec->gpe,
796                                           ACPI_GPE_EDGE_TRIGGERED,
797                                           &acpi_ec_gpe_handler, ec);
798         if (ACPI_FAILURE(status))
799                 return -ENODEV;
800
801         acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
802         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
803
804         status = acpi_install_address_space_handler(ec->handle,
805                                                     ACPI_ADR_SPACE_EC,
806                                                     &acpi_ec_space_handler,
807                                                     &acpi_ec_space_setup, ec);
808         if (ACPI_FAILURE(status)) {
809                 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
810                 return -ENODEV;
811         }
812
813         ec->handlers_installed = 1;
814         return 0;
815 }
816
817 static int acpi_ec_start(struct acpi_device *device)
818 {
819         struct acpi_ec *ec;
820         int ret = 0;
821
822         if (!device)
823                 return -EINVAL;
824
825         ec = acpi_driver_data(device);
826
827         if (!ec)
828                 return -EINVAL;
829
830         ret = ec_install_handlers(ec);
831
832         /* EC is fully operational, allow queries */
833         clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
834         return ret;
835 }
836
837 static int acpi_ec_stop(struct acpi_device *device, int type)
838 {
839         struct acpi_ec *ec;
840         if (!device)
841                 return -EINVAL;
842         ec = acpi_driver_data(device);
843         if (!ec)
844                 return -EINVAL;
845         ec_remove_handlers(ec);
846
847         return 0;
848 }
849
850 int __init acpi_ec_ecdt_probe(void)
851 {
852         int ret;
853         acpi_status status;
854         struct acpi_table_ecdt *ecdt_ptr;
855
856         boot_ec = make_acpi_ec();
857         if (!boot_ec)
858                 return -ENOMEM;
859         /*
860          * Generate a boot ec context
861          */
862         status = acpi_get_table(ACPI_SIG_ECDT, 1,
863                                 (struct acpi_table_header **)&ecdt_ptr);
864         if (ACPI_SUCCESS(status)) {
865                 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
866                 boot_ec->command_addr = ecdt_ptr->control.address;
867                 boot_ec->data_addr = ecdt_ptr->data.address;
868                 boot_ec->gpe = ecdt_ptr->gpe;
869                 boot_ec->handle = ACPI_ROOT_OBJECT;
870         } else {
871                 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
872                 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
873                                                 boot_ec, NULL);
874                 /* Check that acpi_get_devices actually find something */
875                 if (ACPI_FAILURE(status) || !boot_ec->handle)
876                         goto error;
877         }
878
879         ret = ec_install_handlers(boot_ec);
880         if (!ret) {
881                 first_ec = boot_ec;
882                 return 0;
883         }
884       error:
885         kfree(boot_ec);
886         boot_ec = NULL;
887         return -ENODEV;
888 }
889
890 static int __init acpi_ec_init(void)
891 {
892         int result = 0;
893
894         if (acpi_disabled)
895                 return 0;
896
897         acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
898         if (!acpi_ec_dir)
899                 return -ENODEV;
900
901         /* Now register the driver for the EC */
902         result = acpi_bus_register_driver(&acpi_ec_driver);
903         if (result < 0) {
904                 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
905                 return -ENODEV;
906         }
907
908         return result;
909 }
910
911 subsys_initcall(acpi_ec_init);
912
913 /* EC driver currently not unloadable */
914 #if 0
915 static void __exit acpi_ec_exit(void)
916 {
917
918         acpi_bus_unregister_driver(&acpi_ec_driver);
919
920         remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
921
922         return;
923 }
924 #endif                          /* 0 */
925
926 static int __init acpi_ec_set_intr_mode(char *str)
927 {
928         int intr;
929
930         if (!get_option(&str, &intr))
931                 return 0;
932
933         acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
934
935         printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
936
937         return 1;
938 }
939
940 __setup("ec_intr=", acpi_ec_set_intr_mode);