ACPI: EC: Add workaround for "optimized" controllers
[pandora-kernel.git] / drivers / acpi / ec.c
1 /*
2  *  ec.c - ACPI Embedded Controller Driver (v2.0)
3  *
4  *  Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5  *  Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
6  *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
7  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
9  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or (at
15  *  your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful, but
18  *  WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  *  General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License along
23  *  with this program; if not, write to the Free Software Foundation, Inc.,
24  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25  *
26  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27  */
28
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/types.h>
33 #include <linux/delay.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36 #include <linux/interrupt.h>
37 #include <linux/list.h>
38 #include <asm/io.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.h>
41 #include <acpi/actypes.h>
42
43 #define ACPI_EC_CLASS                   "embedded_controller"
44 #define ACPI_EC_DEVICE_NAME             "Embedded Controller"
45 #define ACPI_EC_FILE_INFO               "info"
46
47 #undef PREFIX
48 #define PREFIX                          "ACPI: EC: "
49
50 /* EC status register */
51 #define ACPI_EC_FLAG_OBF        0x01    /* Output buffer full */
52 #define ACPI_EC_FLAG_IBF        0x02    /* Input buffer full */
53 #define ACPI_EC_FLAG_BURST      0x10    /* burst mode */
54 #define ACPI_EC_FLAG_SCI        0x20    /* EC-SCI occurred */
55
56 /* EC commands */
57 enum ec_command {
58         ACPI_EC_COMMAND_READ = 0x80,
59         ACPI_EC_COMMAND_WRITE = 0x81,
60         ACPI_EC_BURST_ENABLE = 0x82,
61         ACPI_EC_BURST_DISABLE = 0x83,
62         ACPI_EC_COMMAND_QUERY = 0x84,
63 };
64
65 /* EC events */
66 enum ec_event {
67         ACPI_EC_EVENT_OBF_1 = 1,        /* Output buffer full */
68         ACPI_EC_EVENT_IBF_0,            /* Input buffer empty */
69 };
70
71 #define ACPI_EC_DELAY           500     /* Wait 500ms max. during EC ops */
72 #define ACPI_EC_UDELAY_GLK      1000    /* Wait 1ms max. to get global lock */
73
74 enum {
75         EC_FLAGS_WAIT_GPE = 0,          /* Don't check status until GPE arrives */
76         EC_FLAGS_QUERY_PENDING,         /* Query is pending */
77         EC_FLAGS_GPE_MODE,              /* Expect GPE to be sent for status change */
78         EC_FLAGS_ONLY_IBF_GPE,          /* Expect GPE only for IBF = 0 event */
79 };
80
81 static int acpi_ec_remove(struct acpi_device *device, int type);
82 static int acpi_ec_start(struct acpi_device *device);
83 static int acpi_ec_stop(struct acpi_device *device, int type);
84 static int acpi_ec_add(struct acpi_device *device);
85
86 static const struct acpi_device_id ec_device_ids[] = {
87         {"PNP0C09", 0},
88         {"", 0},
89 };
90
91 static struct acpi_driver acpi_ec_driver = {
92         .name = "ec",
93         .class = ACPI_EC_CLASS,
94         .ids = ec_device_ids,
95         .ops = {
96                 .add = acpi_ec_add,
97                 .remove = acpi_ec_remove,
98                 .start = acpi_ec_start,
99                 .stop = acpi_ec_stop,
100                 },
101 };
102
103 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
104 /* External interfaces use first EC only, so remember */
105 typedef int (*acpi_ec_query_func) (void *data);
106
107 struct acpi_ec_query_handler {
108         struct list_head node;
109         acpi_ec_query_func func;
110         acpi_handle handle;
111         void *data;
112         u8 query_bit;
113 };
114
115 static struct acpi_ec {
116         acpi_handle handle;
117         unsigned long gpe;
118         unsigned long command_addr;
119         unsigned long data_addr;
120         unsigned long global_lock;
121         unsigned long flags;
122         struct mutex lock;
123         wait_queue_head_t wait;
124         struct list_head list;
125         u8 handlers_installed;
126 } *boot_ec, *first_ec;
127
128 /* --------------------------------------------------------------------------
129                              Transaction Management
130    -------------------------------------------------------------------------- */
131
132 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
133 {
134         return inb(ec->command_addr);
135 }
136
137 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
138 {
139         return inb(ec->data_addr);
140 }
141
142 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
143 {
144         outb(command, ec->command_addr);
145 }
146
147 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
148 {
149         outb(data, ec->data_addr);
150 }
151
152 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
153 {
154         if (test_bit(EC_FLAGS_WAIT_GPE, &ec->flags))
155                 return 0;
156         if (event == ACPI_EC_EVENT_OBF_1) {
157                 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
158                         return 1;
159         } else if (event == ACPI_EC_EVENT_IBF_0) {
160                 if (!(acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF))
161                         return 1;
162         }
163
164         return 0;
165 }
166
167 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
168 {
169         if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
170             likely(!force_poll)) {
171                 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
172                                        msecs_to_jiffies(ACPI_EC_DELAY)))
173                         return 0;
174                 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
175                 if (acpi_ec_check_status(ec, event)) {
176                         if (event == ACPI_EC_EVENT_OBF_1)
177                                 /* miss OBF = 1 GPE, don't expect it anymore */
178                                 set_bit(EC_FLAGS_ONLY_IBF_GPE, &ec->flags);
179                         else
180                                 /* missing GPEs, switch back to poll mode */
181                                 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
182                         return 0;
183                 }
184         } else {
185                 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
186                 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
187                 while (time_before(jiffies, delay)) {
188                         if (acpi_ec_check_status(ec, event))
189                                 return 0;
190                 }
191         }
192         printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
193                                " status = %d, expect_event = %d\n",
194                                acpi_ec_read_status(ec), event);
195         return -ETIME;
196 }
197
198 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
199                                         const u8 * wdata, unsigned wdata_len,
200                                         u8 * rdata, unsigned rdata_len,
201                                         int force_poll)
202 {
203         int result = 0;
204         set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
205         acpi_ec_write_cmd(ec, command);
206
207         for (; wdata_len > 0; --wdata_len) {
208                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
209                 if (result) {
210                         printk(KERN_ERR PREFIX
211                                "write_cmd timeout, command = %d\n", command);
212                         goto end;
213                 }
214                 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
215                 acpi_ec_write_data(ec, *(wdata++));
216         }
217
218         if (!rdata_len) {
219                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
220                 if (result) {
221                         printk(KERN_ERR PREFIX
222                                "finish-write timeout, command = %d\n", command);
223                         goto end;
224                 }
225         } else if (command == ACPI_EC_COMMAND_QUERY)
226                 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
227
228         for (; rdata_len > 0; --rdata_len) {
229                 if (test_bit(EC_FLAGS_ONLY_IBF_GPE, &ec->flags))
230                         force_poll = 1;
231                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, force_poll);
232                 if (result) {
233                         printk(KERN_ERR PREFIX "read timeout, command = %d\n",
234                                command);
235                         goto end;
236                 }
237                 /* Don't expect GPE after last read */
238                 if (rdata_len > 1)
239                         set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
240                 *(rdata++) = acpi_ec_read_data(ec);
241         }
242       end:
243         return result;
244 }
245
246 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
247                                const u8 * wdata, unsigned wdata_len,
248                                u8 * rdata, unsigned rdata_len,
249                                int force_poll)
250 {
251         int status;
252         u32 glk;
253
254         if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
255                 return -EINVAL;
256
257         if (rdata)
258                 memset(rdata, 0, rdata_len);
259
260         mutex_lock(&ec->lock);
261         if (ec->global_lock) {
262                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
263                 if (ACPI_FAILURE(status)) {
264                         mutex_unlock(&ec->lock);
265                         return -ENODEV;
266                 }
267         }
268
269         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
270         if (status) {
271                 printk(KERN_ERR PREFIX
272                        "input buffer is not empty, aborting transaction\n");
273                 goto end;
274         }
275
276         status = acpi_ec_transaction_unlocked(ec, command,
277                                               wdata, wdata_len,
278                                               rdata, rdata_len,
279                                               force_poll);
280
281       end:
282
283         if (ec->global_lock)
284                 acpi_release_global_lock(glk);
285         mutex_unlock(&ec->lock);
286
287         return status;
288 }
289
290 /*
291  * Note: samsung nv5000 doesn't work with ec burst mode.
292  * http://bugzilla.kernel.org/show_bug.cgi?id=4980
293  */
294 int acpi_ec_burst_enable(struct acpi_ec *ec)
295 {
296         u8 d;
297         return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
298 }
299
300 int acpi_ec_burst_disable(struct acpi_ec *ec)
301 {
302         return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
303 }
304
305 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
306 {
307         int result;
308         u8 d;
309
310         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
311                                      &address, 1, &d, 1, 0);
312         *data = d;
313         return result;
314 }
315
316 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
317 {
318         u8 wdata[2] = { address, data };
319         return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
320                                    wdata, 2, NULL, 0, 0);
321 }
322
323 /*
324  * Externally callable EC access functions. For now, assume 1 EC only
325  */
326 int ec_burst_enable(void)
327 {
328         if (!first_ec)
329                 return -ENODEV;
330         return acpi_ec_burst_enable(first_ec);
331 }
332
333 EXPORT_SYMBOL(ec_burst_enable);
334
335 int ec_burst_disable(void)
336 {
337         if (!first_ec)
338                 return -ENODEV;
339         return acpi_ec_burst_disable(first_ec);
340 }
341
342 EXPORT_SYMBOL(ec_burst_disable);
343
344 int ec_read(u8 addr, u8 * val)
345 {
346         int err;
347         u8 temp_data;
348
349         if (!first_ec)
350                 return -ENODEV;
351
352         err = acpi_ec_read(first_ec, addr, &temp_data);
353
354         if (!err) {
355                 *val = temp_data;
356                 return 0;
357         } else
358                 return err;
359 }
360
361 EXPORT_SYMBOL(ec_read);
362
363 int ec_write(u8 addr, u8 val)
364 {
365         int err;
366
367         if (!first_ec)
368                 return -ENODEV;
369
370         err = acpi_ec_write(first_ec, addr, val);
371
372         return err;
373 }
374
375 EXPORT_SYMBOL(ec_write);
376
377 int ec_transaction(u8 command,
378                    const u8 * wdata, unsigned wdata_len,
379                    u8 * rdata, unsigned rdata_len,
380                    int force_poll)
381 {
382         if (!first_ec)
383                 return -ENODEV;
384
385         return acpi_ec_transaction(first_ec, command, wdata,
386                                    wdata_len, rdata, rdata_len,
387                                    force_poll);
388 }
389
390 EXPORT_SYMBOL(ec_transaction);
391
392 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
393 {
394         int result;
395         u8 d;
396
397         if (!ec || !data)
398                 return -EINVAL;
399
400         /*
401          * Query the EC to find out which _Qxx method we need to evaluate.
402          * Note that successful completion of the query causes the ACPI_EC_SCI
403          * bit to be cleared (and thus clearing the interrupt source).
404          */
405
406         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
407         if (result)
408                 return result;
409
410         if (!d)
411                 return -ENODATA;
412
413         *data = d;
414         return 0;
415 }
416
417 /* --------------------------------------------------------------------------
418                                 Event Management
419    -------------------------------------------------------------------------- */
420 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
421                               acpi_handle handle, acpi_ec_query_func func,
422                               void *data)
423 {
424         struct acpi_ec_query_handler *handler =
425             kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
426         if (!handler)
427                 return -ENOMEM;
428
429         handler->query_bit = query_bit;
430         handler->handle = handle;
431         handler->func = func;
432         handler->data = data;
433         mutex_lock(&ec->lock);
434         list_add(&handler->node, &ec->list);
435         mutex_unlock(&ec->lock);
436         return 0;
437 }
438
439 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
440
441 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
442 {
443         struct acpi_ec_query_handler *handler;
444         mutex_lock(&ec->lock);
445         list_for_each_entry(handler, &ec->list, node) {
446                 if (query_bit == handler->query_bit) {
447                         list_del(&handler->node);
448                         kfree(handler);
449                 }
450         }
451         mutex_unlock(&ec->lock);
452 }
453
454 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
455
456 static void acpi_ec_gpe_query(void *ec_cxt)
457 {
458         struct acpi_ec *ec = ec_cxt;
459         u8 value = 0;
460         struct acpi_ec_query_handler *handler, copy;
461
462         if (!ec || acpi_ec_query(ec, &value))
463                 return;
464         mutex_lock(&ec->lock);
465         list_for_each_entry(handler, &ec->list, node) {
466                 if (value == handler->query_bit) {
467                         /* have custom handler for this bit */
468                         memcpy(&copy, handler, sizeof(copy));
469                         mutex_unlock(&ec->lock);
470                         if (copy.func) {
471                                 copy.func(copy.data);
472                         } else if (copy.handle) {
473                                 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
474                         }
475                         return;
476                 }
477         }
478         mutex_unlock(&ec->lock);
479 }
480
481 static u32 acpi_ec_gpe_handler(void *data)
482 {
483         acpi_status status = AE_OK;
484         struct acpi_ec *ec = data;
485
486         clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
487         if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
488                 wake_up(&ec->wait);
489
490         if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) {
491                 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
492                         status = acpi_os_execute(OSL_EC_BURST_HANDLER,
493                                 acpi_ec_gpe_query, ec);
494         } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE, &ec->flags)))
495                 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
496
497         return ACPI_SUCCESS(status) ?
498             ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
499 }
500
501 /* --------------------------------------------------------------------------
502                              Address Space Management
503    -------------------------------------------------------------------------- */
504
505 static acpi_status
506 acpi_ec_space_setup(acpi_handle region_handle,
507                     u32 function, void *handler_context, void **return_context)
508 {
509         /*
510          * The EC object is in the handler context and is needed
511          * when calling the acpi_ec_space_handler.
512          */
513         *return_context = (function != ACPI_REGION_DEACTIVATE) ?
514             handler_context : NULL;
515
516         return AE_OK;
517 }
518
519 static acpi_status
520 acpi_ec_space_handler(u32 function, acpi_physical_address address,
521                       u32 bits, acpi_integer *value,
522                       void *handler_context, void *region_context)
523 {
524         struct acpi_ec *ec = handler_context;
525         int result = 0, i = 0;
526         u8 temp = 0;
527
528         if ((address > 0xFF) || !value || !handler_context)
529                 return AE_BAD_PARAMETER;
530
531         if (function != ACPI_READ && function != ACPI_WRITE)
532                 return AE_BAD_PARAMETER;
533
534         if (bits != 8 && acpi_strict)
535                 return AE_BAD_PARAMETER;
536
537         while (bits - i > 0) {
538                 if (function == ACPI_READ) {
539                         result = acpi_ec_read(ec, address, &temp);
540                         (*value) |= ((acpi_integer)temp) << i;
541                 } else {
542                         temp = 0xff & ((*value) >> i);
543                         result = acpi_ec_write(ec, address, temp);
544                 }
545                 i += 8;
546                 ++address;
547         }
548
549         switch (result) {
550         case -EINVAL:
551                 return AE_BAD_PARAMETER;
552                 break;
553         case -ENODEV:
554                 return AE_NOT_FOUND;
555                 break;
556         case -ETIME:
557                 return AE_TIME;
558                 break;
559         default:
560                 return AE_OK;
561         }
562 }
563
564 /* --------------------------------------------------------------------------
565                               FS Interface (/proc)
566    -------------------------------------------------------------------------- */
567
568 static struct proc_dir_entry *acpi_ec_dir;
569
570 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
571 {
572         struct acpi_ec *ec = seq->private;
573
574         if (!ec)
575                 goto end;
576
577         seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
578         seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
579                    (unsigned)ec->command_addr, (unsigned)ec->data_addr);
580         seq_printf(seq, "use global lock:\t%s\n",
581                    ec->global_lock ? "yes" : "no");
582       end:
583         return 0;
584 }
585
586 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
587 {
588         return single_open(file, acpi_ec_read_info, PDE(inode)->data);
589 }
590
591 static struct file_operations acpi_ec_info_ops = {
592         .open = acpi_ec_info_open_fs,
593         .read = seq_read,
594         .llseek = seq_lseek,
595         .release = single_release,
596         .owner = THIS_MODULE,
597 };
598
599 static int acpi_ec_add_fs(struct acpi_device *device)
600 {
601         struct proc_dir_entry *entry = NULL;
602
603         if (!acpi_device_dir(device)) {
604                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
605                                                      acpi_ec_dir);
606                 if (!acpi_device_dir(device))
607                         return -ENODEV;
608         }
609
610         entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
611                                   acpi_device_dir(device));
612         if (!entry)
613                 return -ENODEV;
614         else {
615                 entry->proc_fops = &acpi_ec_info_ops;
616                 entry->data = acpi_driver_data(device);
617                 entry->owner = THIS_MODULE;
618         }
619
620         return 0;
621 }
622
623 static int acpi_ec_remove_fs(struct acpi_device *device)
624 {
625
626         if (acpi_device_dir(device)) {
627                 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
628                 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
629                 acpi_device_dir(device) = NULL;
630         }
631
632         return 0;
633 }
634
635 /* --------------------------------------------------------------------------
636                                Driver Interface
637    -------------------------------------------------------------------------- */
638 static acpi_status
639 ec_parse_io_ports(struct acpi_resource *resource, void *context);
640
641 static struct acpi_ec *make_acpi_ec(void)
642 {
643         struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
644         if (!ec)
645                 return NULL;
646         ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
647         mutex_init(&ec->lock);
648         init_waitqueue_head(&ec->wait);
649         INIT_LIST_HEAD(&ec->list);
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 */