ACPI: EC: Don't expect interrupt after last read
[firefly-linux-kernel-4.4.55.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                 /* Don't expect GPE after last read */
232                 if (rdata_len > 1)
233                         set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
234                 *(rdata++) = acpi_ec_read_data(ec);
235         }
236       end:
237         return result;
238 }
239
240 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
241                                const u8 * wdata, unsigned wdata_len,
242                                u8 * rdata, unsigned rdata_len,
243                                int force_poll)
244 {
245         int status;
246         u32 glk;
247
248         if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
249                 return -EINVAL;
250
251         if (rdata)
252                 memset(rdata, 0, rdata_len);
253
254         mutex_lock(&ec->lock);
255         if (ec->global_lock) {
256                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
257                 if (ACPI_FAILURE(status)) {
258                         mutex_unlock(&ec->lock);
259                         return -ENODEV;
260                 }
261         }
262
263         /* Make sure GPE is enabled before doing transaction */
264         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
265
266         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
267         if (status) {
268                 printk(KERN_ERR PREFIX
269                        "input buffer is not empty, aborting transaction\n");
270                 goto end;
271         }
272
273         status = acpi_ec_transaction_unlocked(ec, command,
274                                               wdata, wdata_len,
275                                               rdata, rdata_len,
276                                               force_poll);
277
278       end:
279
280         if (ec->global_lock)
281                 acpi_release_global_lock(glk);
282         mutex_unlock(&ec->lock);
283
284         return status;
285 }
286
287 /*
288  * Note: samsung nv5000 doesn't work with ec burst mode.
289  * http://bugzilla.kernel.org/show_bug.cgi?id=4980
290  */
291 int acpi_ec_burst_enable(struct acpi_ec *ec)
292 {
293         u8 d;
294         return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
295 }
296
297 int acpi_ec_burst_disable(struct acpi_ec *ec)
298 {
299         return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
300 }
301
302 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
303 {
304         int result;
305         u8 d;
306
307         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
308                                      &address, 1, &d, 1, 0);
309         *data = d;
310         return result;
311 }
312
313 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
314 {
315         u8 wdata[2] = { address, data };
316         return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
317                                    wdata, 2, NULL, 0, 0);
318 }
319
320 /*
321  * Externally callable EC access functions. For now, assume 1 EC only
322  */
323 int ec_burst_enable(void)
324 {
325         if (!first_ec)
326                 return -ENODEV;
327         return acpi_ec_burst_enable(first_ec);
328 }
329
330 EXPORT_SYMBOL(ec_burst_enable);
331
332 int ec_burst_disable(void)
333 {
334         if (!first_ec)
335                 return -ENODEV;
336         return acpi_ec_burst_disable(first_ec);
337 }
338
339 EXPORT_SYMBOL(ec_burst_disable);
340
341 int ec_read(u8 addr, u8 * val)
342 {
343         int err;
344         u8 temp_data;
345
346         if (!first_ec)
347                 return -ENODEV;
348
349         err = acpi_ec_read(first_ec, addr, &temp_data);
350
351         if (!err) {
352                 *val = temp_data;
353                 return 0;
354         } else
355                 return err;
356 }
357
358 EXPORT_SYMBOL(ec_read);
359
360 int ec_write(u8 addr, u8 val)
361 {
362         int err;
363
364         if (!first_ec)
365                 return -ENODEV;
366
367         err = acpi_ec_write(first_ec, addr, val);
368
369         return err;
370 }
371
372 EXPORT_SYMBOL(ec_write);
373
374 int ec_transaction(u8 command,
375                    const u8 * wdata, unsigned wdata_len,
376                    u8 * rdata, unsigned rdata_len,
377                    int force_poll)
378 {
379         if (!first_ec)
380                 return -ENODEV;
381
382         return acpi_ec_transaction(first_ec, command, wdata,
383                                    wdata_len, rdata, rdata_len,
384                                    force_poll);
385 }
386
387 EXPORT_SYMBOL(ec_transaction);
388
389 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
390 {
391         int result;
392         u8 d;
393
394         if (!ec || !data)
395                 return -EINVAL;
396
397         /*
398          * Query the EC to find out which _Qxx method we need to evaluate.
399          * Note that successful completion of the query causes the ACPI_EC_SCI
400          * bit to be cleared (and thus clearing the interrupt source).
401          */
402
403         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
404         if (result)
405                 return result;
406
407         if (!d)
408                 return -ENODATA;
409
410         *data = d;
411         return 0;
412 }
413
414 /* --------------------------------------------------------------------------
415                                 Event Management
416    -------------------------------------------------------------------------- */
417 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
418                               acpi_handle handle, acpi_ec_query_func func,
419                               void *data)
420 {
421         struct acpi_ec_query_handler *handler =
422             kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
423         if (!handler)
424                 return -ENOMEM;
425
426         handler->query_bit = query_bit;
427         handler->handle = handle;
428         handler->func = func;
429         handler->data = data;
430         mutex_lock(&ec->lock);
431         list_add(&handler->node, &ec->list);
432         mutex_unlock(&ec->lock);
433         return 0;
434 }
435
436 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
437
438 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
439 {
440         struct acpi_ec_query_handler *handler;
441         mutex_lock(&ec->lock);
442         list_for_each_entry(handler, &ec->list, node) {
443                 if (query_bit == handler->query_bit) {
444                         list_del(&handler->node);
445                         kfree(handler);
446                 }
447         }
448         mutex_unlock(&ec->lock);
449 }
450
451 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
452
453 static void acpi_ec_gpe_query(void *ec_cxt)
454 {
455         struct acpi_ec *ec = ec_cxt;
456         u8 value = 0;
457         struct acpi_ec_query_handler *handler, copy;
458
459         if (!ec || acpi_ec_query(ec, &value))
460                 return;
461         mutex_lock(&ec->lock);
462         list_for_each_entry(handler, &ec->list, node) {
463                 if (value == handler->query_bit) {
464                         /* have custom handler for this bit */
465                         memcpy(&copy, handler, sizeof(copy));
466                         mutex_unlock(&ec->lock);
467                         if (copy.func) {
468                                 copy.func(copy.data);
469                         } else if (copy.handle) {
470                                 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
471                         }
472                         return;
473                 }
474         }
475         mutex_unlock(&ec->lock);
476 }
477
478 static u32 acpi_ec_gpe_handler(void *data)
479 {
480         acpi_status status = AE_OK;
481         struct acpi_ec *ec = data;
482
483         clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
484
485         if (acpi_ec_mode == EC_INTR) {
486                 wake_up(&ec->wait);
487         }
488
489         if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
490                 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
491                         status = acpi_os_execute(OSL_EC_BURST_HANDLER,
492                                 acpi_ec_gpe_query, ec);
493         }
494
495         return status == AE_OK ?
496             ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
497 }
498
499 /* --------------------------------------------------------------------------
500                              Address Space Management
501    -------------------------------------------------------------------------- */
502
503 static acpi_status
504 acpi_ec_space_setup(acpi_handle region_handle,
505                     u32 function, void *handler_context, void **return_context)
506 {
507         /*
508          * The EC object is in the handler context and is needed
509          * when calling the acpi_ec_space_handler.
510          */
511         *return_context = (function != ACPI_REGION_DEACTIVATE) ?
512             handler_context : NULL;
513
514         return AE_OK;
515 }
516
517 static acpi_status
518 acpi_ec_space_handler(u32 function, acpi_physical_address address,
519                       u32 bits, acpi_integer *value,
520                       void *handler_context, void *region_context)
521 {
522         struct acpi_ec *ec = handler_context;
523         int result = 0, i = 0;
524         u8 temp = 0;
525
526         if ((address > 0xFF) || !value || !handler_context)
527                 return AE_BAD_PARAMETER;
528
529         if (function != ACPI_READ && function != ACPI_WRITE)
530                 return AE_BAD_PARAMETER;
531
532         if (bits != 8 && acpi_strict)
533                 return AE_BAD_PARAMETER;
534
535         while (bits - i > 0) {
536                 if (function == ACPI_READ) {
537                         result = acpi_ec_read(ec, address, &temp);
538                         (*value) |= ((acpi_integer)temp) << i;
539                 } else {
540                         temp = 0xff & ((*value) >> i);
541                         result = acpi_ec_write(ec, address, temp);
542                 }
543                 i += 8;
544                 ++address;
545         }
546
547         switch (result) {
548         case -EINVAL:
549                 return AE_BAD_PARAMETER;
550                 break;
551         case -ENODEV:
552                 return AE_NOT_FOUND;
553                 break;
554         case -ETIME:
555                 return AE_TIME;
556                 break;
557         default:
558                 return AE_OK;
559         }
560 }
561
562 /* --------------------------------------------------------------------------
563                               FS Interface (/proc)
564    -------------------------------------------------------------------------- */
565
566 static struct proc_dir_entry *acpi_ec_dir;
567
568 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
569 {
570         struct acpi_ec *ec = seq->private;
571
572         if (!ec)
573                 goto end;
574
575         seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
576         seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
577                    (unsigned)ec->command_addr, (unsigned)ec->data_addr);
578         seq_printf(seq, "use global lock:\t%s\n",
579                    ec->global_lock ? "yes" : "no");
580       end:
581         return 0;
582 }
583
584 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
585 {
586         return single_open(file, acpi_ec_read_info, PDE(inode)->data);
587 }
588
589 static struct file_operations acpi_ec_info_ops = {
590         .open = acpi_ec_info_open_fs,
591         .read = seq_read,
592         .llseek = seq_lseek,
593         .release = single_release,
594         .owner = THIS_MODULE,
595 };
596
597 static int acpi_ec_add_fs(struct acpi_device *device)
598 {
599         struct proc_dir_entry *entry = NULL;
600
601         if (!acpi_device_dir(device)) {
602                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
603                                                      acpi_ec_dir);
604                 if (!acpi_device_dir(device))
605                         return -ENODEV;
606         }
607
608         entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
609                                   acpi_device_dir(device));
610         if (!entry)
611                 return -ENODEV;
612         else {
613                 entry->proc_fops = &acpi_ec_info_ops;
614                 entry->data = acpi_driver_data(device);
615                 entry->owner = THIS_MODULE;
616         }
617
618         return 0;
619 }
620
621 static int acpi_ec_remove_fs(struct acpi_device *device)
622 {
623
624         if (acpi_device_dir(device)) {
625                 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
626                 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
627                 acpi_device_dir(device) = NULL;
628         }
629
630         return 0;
631 }
632
633 /* --------------------------------------------------------------------------
634                                Driver Interface
635    -------------------------------------------------------------------------- */
636 static acpi_status
637 ec_parse_io_ports(struct acpi_resource *resource, void *context);
638
639 static struct acpi_ec *make_acpi_ec(void)
640 {
641         struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
642         if (!ec)
643                 return NULL;
644
645         ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
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         /* Find and register all query methods */
683         acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
684                             acpi_ec_register_query_methods, ec, NULL);
685         /* Use the global lock for all EC transactions? */
686         acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
687         ec->handle = handle;
688         return AE_CTRL_TERMINATE;
689 }
690
691 static void ec_remove_handlers(struct acpi_ec *ec)
692 {
693         if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
694                                 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
695                 printk(KERN_ERR PREFIX "failed to remove space handler\n");
696         if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
697                                 &acpi_ec_gpe_handler)))
698                 printk(KERN_ERR PREFIX "failed to remove gpe handler\n");
699         ec->handlers_installed = 0;
700 }
701
702 static int acpi_ec_add(struct acpi_device *device)
703 {
704         struct acpi_ec *ec = NULL;
705
706         if (!device)
707                 return -EINVAL;
708         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
709         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
710
711         /* Check for boot EC */
712         if (boot_ec) {
713                 if (boot_ec->handle == device->handle) {
714                         /* Pre-loaded EC from DSDT, just move pointer */
715                         ec = boot_ec;
716                         boot_ec = NULL;
717                         goto end;
718                 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
719                         /* ECDT-based EC, time to shut it down */
720                         ec_remove_handlers(boot_ec);
721                         kfree(boot_ec);
722                         first_ec = boot_ec = NULL;
723                 }
724         }
725
726         ec = make_acpi_ec();
727         if (!ec)
728                 return -ENOMEM;
729
730         if (ec_parse_device(device->handle, 0, ec, NULL) !=
731             AE_CTRL_TERMINATE) {
732                 kfree(ec);
733                 return -EINVAL;
734         }
735         ec->handle = device->handle;
736       end:
737         if (!first_ec)
738                 first_ec = ec;
739         acpi_driver_data(device) = ec;
740         acpi_ec_add_fs(device);
741         printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
742                           ec->gpe, ec->command_addr, ec->data_addr);
743         return 0;
744 }
745
746 static int acpi_ec_remove(struct acpi_device *device, int type)
747 {
748         struct acpi_ec *ec;
749         struct acpi_ec_query_handler *handler, *tmp;
750
751         if (!device)
752                 return -EINVAL;
753
754         ec = acpi_driver_data(device);
755         mutex_lock(&ec->lock);
756         list_for_each_entry_safe(handler, tmp, &ec->list, node) {
757                 list_del(&handler->node);
758                 kfree(handler);
759         }
760         mutex_unlock(&ec->lock);
761         acpi_ec_remove_fs(device);
762         acpi_driver_data(device) = NULL;
763         if (ec == first_ec)
764                 first_ec = NULL;
765         kfree(ec);
766         return 0;
767 }
768
769 static acpi_status
770 ec_parse_io_ports(struct acpi_resource *resource, void *context)
771 {
772         struct acpi_ec *ec = context;
773
774         if (resource->type != ACPI_RESOURCE_TYPE_IO)
775                 return AE_OK;
776
777         /*
778          * The first address region returned is the data port, and
779          * the second address region returned is the status/command
780          * port.
781          */
782         if (ec->data_addr == 0)
783                 ec->data_addr = resource->data.io.minimum;
784         else if (ec->command_addr == 0)
785                 ec->command_addr = resource->data.io.minimum;
786         else
787                 return AE_CTRL_TERMINATE;
788
789         return AE_OK;
790 }
791
792 static int ec_install_handlers(struct acpi_ec *ec)
793 {
794         acpi_status status;
795         if (ec->handlers_installed)
796                 return 0;
797         status = acpi_install_gpe_handler(NULL, ec->gpe,
798                                           ACPI_GPE_EDGE_TRIGGERED,
799                                           &acpi_ec_gpe_handler, ec);
800         if (ACPI_FAILURE(status))
801                 return -ENODEV;
802
803         acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
804         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
805
806         status = acpi_install_address_space_handler(ec->handle,
807                                                     ACPI_ADR_SPACE_EC,
808                                                     &acpi_ec_space_handler,
809                                                     &acpi_ec_space_setup, ec);
810         if (ACPI_FAILURE(status)) {
811                 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
812                 return -ENODEV;
813         }
814
815         ec->handlers_installed = 1;
816         return 0;
817 }
818
819 static int acpi_ec_start(struct acpi_device *device)
820 {
821         struct acpi_ec *ec;
822         int ret = 0;
823
824         if (!device)
825                 return -EINVAL;
826
827         ec = acpi_driver_data(device);
828
829         if (!ec)
830                 return -EINVAL;
831
832         ret = ec_install_handlers(ec);
833
834         /* EC is fully operational, allow queries */
835         clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
836         return ret;
837 }
838
839 static int acpi_ec_stop(struct acpi_device *device, int type)
840 {
841         struct acpi_ec *ec;
842         if (!device)
843                 return -EINVAL;
844         ec = acpi_driver_data(device);
845         if (!ec)
846                 return -EINVAL;
847         ec_remove_handlers(ec);
848
849         return 0;
850 }
851
852 int __init acpi_ec_ecdt_probe(void)
853 {
854         int ret;
855         acpi_status status;
856         struct acpi_table_ecdt *ecdt_ptr;
857
858         boot_ec = make_acpi_ec();
859         if (!boot_ec)
860                 return -ENOMEM;
861         /*
862          * Generate a boot ec context
863          */
864         status = acpi_get_table(ACPI_SIG_ECDT, 1,
865                                 (struct acpi_table_header **)&ecdt_ptr);
866         if (ACPI_SUCCESS(status)) {
867                 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
868                 boot_ec->command_addr = ecdt_ptr->control.address;
869                 boot_ec->data_addr = ecdt_ptr->data.address;
870                 boot_ec->gpe = ecdt_ptr->gpe;
871                 boot_ec->handle = ACPI_ROOT_OBJECT;
872         } else {
873                 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
874                 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
875                                                 boot_ec, NULL);
876                 /* Check that acpi_get_devices actually find something */
877                 if (ACPI_FAILURE(status) || !boot_ec->handle)
878                         goto error;
879         }
880
881         ret = ec_install_handlers(boot_ec);
882         if (!ret) {
883                 first_ec = boot_ec;
884                 return 0;
885         }
886       error:
887         kfree(boot_ec);
888         boot_ec = NULL;
889         return -ENODEV;
890 }
891
892 static int __init acpi_ec_init(void)
893 {
894         int result = 0;
895
896         if (acpi_disabled)
897                 return 0;
898
899         acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
900         if (!acpi_ec_dir)
901                 return -ENODEV;
902
903         /* Now register the driver for the EC */
904         result = acpi_bus_register_driver(&acpi_ec_driver);
905         if (result < 0) {
906                 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
907                 return -ENODEV;
908         }
909
910         return result;
911 }
912
913 subsys_initcall(acpi_ec_init);
914
915 /* EC driver currently not unloadable */
916 #if 0
917 static void __exit acpi_ec_exit(void)
918 {
919
920         acpi_bus_unregister_driver(&acpi_ec_driver);
921
922         remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
923
924         return;
925 }
926 #endif                          /* 0 */
927
928 static int __init acpi_ec_set_intr_mode(char *str)
929 {
930         int intr;
931
932         if (!get_option(&str, &intr))
933                 return 0;
934
935         acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
936
937         printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
938
939         return 1;
940 }
941
942 __setup("ec_intr=", acpi_ec_set_intr_mode);