Pull dock-bay into release branch
[linux-2.6] / drivers / acpi / ec.c
1 /*
2  *  ec.c - ACPI Embedded Controller Driver (v2.0)
3  *
4  *  Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5  *  Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
6  *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
7  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
9  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or (at
15  *  your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful, but
18  *  WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  *  General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License along
23  *  with this program; if not, write to the Free Software Foundation, Inc.,
24  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25  *
26  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27  */
28
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/types.h>
33 #include <linux/delay.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36 #include <linux/interrupt.h>
37 #include <linux/list.h>
38 #include <asm/io.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.h>
41 #include <acpi/actypes.h>
42
43 #define ACPI_EC_CLASS                   "embedded_controller"
44 #define ACPI_EC_HID                     "PNP0C09"
45 #define ACPI_EC_DEVICE_NAME             "Embedded Controller"
46 #define ACPI_EC_FILE_INFO               "info"
47
48 #undef PREFIX
49 #define PREFIX                          "ACPI: EC: "
50
51 /* EC status register */
52 #define ACPI_EC_FLAG_OBF        0x01    /* Output buffer full */
53 #define ACPI_EC_FLAG_IBF        0x02    /* Input buffer full */
54 #define ACPI_EC_FLAG_BURST      0x10    /* burst mode */
55 #define ACPI_EC_FLAG_SCI        0x20    /* EC-SCI occurred */
56
57 /* EC commands */
58 enum ec_command {
59         ACPI_EC_COMMAND_READ = 0x80,
60         ACPI_EC_COMMAND_WRITE = 0x81,
61         ACPI_EC_BURST_ENABLE = 0x82,
62         ACPI_EC_BURST_DISABLE = 0x83,
63         ACPI_EC_COMMAND_QUERY = 0x84,
64 };
65
66 /* EC events */
67 enum ec_event {
68         ACPI_EC_EVENT_OBF_1 = 1,        /* Output buffer full */
69         ACPI_EC_EVENT_IBF_0,    /* Input buffer empty */
70 };
71
72 #define ACPI_EC_DELAY           500     /* Wait 500ms max. during EC ops */
73 #define ACPI_EC_UDELAY_GLK      1000    /* Wait 1ms max. to get global lock */
74
75 static enum ec_mode {
76         EC_INTR = 1,            /* Output buffer full */
77         EC_POLL,                /* Input buffer empty */
78 } acpi_ec_mode = EC_INTR;
79
80 static int acpi_ec_remove(struct acpi_device *device, int type);
81 static int acpi_ec_start(struct acpi_device *device);
82 static int acpi_ec_stop(struct acpi_device *device, int type);
83 static int acpi_ec_add(struct acpi_device *device);
84
85 static struct acpi_driver acpi_ec_driver = {
86         .name = "ec",
87         .class = ACPI_EC_CLASS,
88         .ids = ACPI_EC_HID,
89         .ops = {
90                 .add = acpi_ec_add,
91                 .remove = acpi_ec_remove,
92                 .start = acpi_ec_start,
93                 .stop = acpi_ec_stop,
94                 },
95 };
96
97 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
98 /* External interfaces use first EC only, so remember */
99 typedef int (*acpi_ec_query_func) (void *data);
100
101 struct acpi_ec_query_handler {
102         struct list_head node;
103         acpi_ec_query_func func;
104         acpi_handle handle;
105         void *data;
106         u8 query_bit;
107 };
108
109 static struct acpi_ec {
110         acpi_handle handle;
111         unsigned long gpe;
112         unsigned long command_addr;
113         unsigned long data_addr;
114         unsigned long global_lock;
115         struct mutex lock;
116         atomic_t query_pending;
117         atomic_t event_count;
118         wait_queue_head_t wait;
119         struct list_head list;
120 } *boot_ec, *first_ec;
121
122 /* --------------------------------------------------------------------------
123                              Transaction Management
124    -------------------------------------------------------------------------- */
125
126 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
127 {
128         return inb(ec->command_addr);
129 }
130
131 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
132 {
133         return inb(ec->data_addr);
134 }
135
136 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
137 {
138         outb(command, ec->command_addr);
139 }
140
141 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
142 {
143         outb(data, ec->data_addr);
144 }
145
146 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
147                                        unsigned old_count)
148 {
149         u8 status = acpi_ec_read_status(ec);
150         if (old_count == atomic_read(&ec->event_count))
151                 return 0;
152         if (event == ACPI_EC_EVENT_OBF_1) {
153                 if (status & ACPI_EC_FLAG_OBF)
154                         return 1;
155         } else if (event == ACPI_EC_EVENT_IBF_0) {
156                 if (!(status & ACPI_EC_FLAG_IBF))
157                         return 1;
158         }
159
160         return 0;
161 }
162
163 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event,
164                         unsigned count, int force_poll)
165 {
166         if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) {
167                 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
168                 while (time_before(jiffies, delay)) {
169                         if (acpi_ec_check_status(ec, event, 0))
170                                 return 0;
171                 }
172         } else {
173                 if (wait_event_timeout(ec->wait,
174                                        acpi_ec_check_status(ec, event, count),
175                                        msecs_to_jiffies(ACPI_EC_DELAY)) ||
176                     acpi_ec_check_status(ec, event, 0)) {
177                         return 0;
178                 } else {
179                         printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
180                                " status = %d, expect_event = %d\n",
181                                acpi_ec_read_status(ec), event);
182                 }
183         }
184
185         return -ETIME;
186 }
187
188 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
189                                         const u8 * wdata, unsigned wdata_len,
190                                         u8 * rdata, unsigned rdata_len,
191                                         int force_poll)
192 {
193         int result = 0;
194         unsigned count = atomic_read(&ec->event_count);
195         acpi_ec_write_cmd(ec, command);
196
197         for (; wdata_len > 0; --wdata_len) {
198                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
199                 if (result) {
200                         printk(KERN_ERR PREFIX
201                                "write_cmd timeout, command = %d\n", command);
202                         goto end;
203                 }
204                 count = atomic_read(&ec->event_count);
205                 acpi_ec_write_data(ec, *(wdata++));
206         }
207
208         if (!rdata_len) {
209                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
210                 if (result) {
211                         printk(KERN_ERR PREFIX
212                                "finish-write timeout, command = %d\n", command);
213                         goto end;
214                 }
215         } else if (command == ACPI_EC_COMMAND_QUERY) {
216                 atomic_set(&ec->query_pending, 0);
217         }
218
219         for (; rdata_len > 0; --rdata_len) {
220                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count, force_poll);
221                 if (result) {
222                         printk(KERN_ERR PREFIX "read timeout, command = %d\n",
223                                command);
224                         goto end;
225                 }
226                 count = atomic_read(&ec->event_count);
227                 *(rdata++) = acpi_ec_read_data(ec);
228         }
229       end:
230         return result;
231 }
232
233 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
234                                const u8 * wdata, unsigned wdata_len,
235                                u8 * rdata, unsigned rdata_len,
236                                int force_poll)
237 {
238         int status;
239         u32 glk;
240
241         if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
242                 return -EINVAL;
243
244         if (rdata)
245                 memset(rdata, 0, rdata_len);
246
247         mutex_lock(&ec->lock);
248         if (ec->global_lock) {
249                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
250                 if (ACPI_FAILURE(status)) {
251                         mutex_unlock(&ec->lock);
252                         return -ENODEV;
253                 }
254         }
255
256         /* Make sure GPE is enabled before doing transaction */
257         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
258
259         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0, 0);
260         if (status) {
261                 printk(KERN_ERR PREFIX
262                        "input buffer is not empty, aborting transaction\n");
263                 goto end;
264         }
265
266         status = acpi_ec_transaction_unlocked(ec, command,
267                                               wdata, wdata_len,
268                                               rdata, rdata_len,
269                                               force_poll);
270
271       end:
272
273         if (ec->global_lock)
274                 acpi_release_global_lock(glk);
275         mutex_unlock(&ec->lock);
276
277         return status;
278 }
279
280 /*
281  * Note: samsung nv5000 doesn't work with ec burst mode.
282  * http://bugzilla.kernel.org/show_bug.cgi?id=4980
283  */
284 int acpi_ec_burst_enable(struct acpi_ec *ec)
285 {
286         u8 d;
287         return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
288 }
289
290 int acpi_ec_burst_disable(struct acpi_ec *ec)
291 {
292         return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
293 }
294
295 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
296 {
297         int result;
298         u8 d;
299
300         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
301                                      &address, 1, &d, 1, 0);
302         *data = d;
303         return result;
304 }
305
306 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
307 {
308         u8 wdata[2] = { address, data };
309         return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
310                                    wdata, 2, NULL, 0, 0);
311 }
312
313 /*
314  * Externally callable EC access functions. For now, assume 1 EC only
315  */
316 int ec_burst_enable(void)
317 {
318         if (!first_ec)
319                 return -ENODEV;
320         return acpi_ec_burst_enable(first_ec);
321 }
322
323 EXPORT_SYMBOL(ec_burst_enable);
324
325 int ec_burst_disable(void)
326 {
327         if (!first_ec)
328                 return -ENODEV;
329         return acpi_ec_burst_disable(first_ec);
330 }
331
332 EXPORT_SYMBOL(ec_burst_disable);
333
334 int ec_read(u8 addr, u8 * val)
335 {
336         int err;
337         u8 temp_data;
338
339         if (!first_ec)
340                 return -ENODEV;
341
342         err = acpi_ec_read(first_ec, addr, &temp_data);
343
344         if (!err) {
345                 *val = temp_data;
346                 return 0;
347         } else
348                 return err;
349 }
350
351 EXPORT_SYMBOL(ec_read);
352
353 int ec_write(u8 addr, u8 val)
354 {
355         int err;
356
357         if (!first_ec)
358                 return -ENODEV;
359
360         err = acpi_ec_write(first_ec, addr, val);
361
362         return err;
363 }
364
365 EXPORT_SYMBOL(ec_write);
366
367 int ec_transaction(u8 command,
368                    const u8 * wdata, unsigned wdata_len,
369                    u8 * rdata, unsigned rdata_len,
370                    int force_poll)
371 {
372         if (!first_ec)
373                 return -ENODEV;
374
375         return acpi_ec_transaction(first_ec, command, wdata,
376                                    wdata_len, rdata, rdata_len,
377                                    force_poll);
378 }
379
380 EXPORT_SYMBOL(ec_transaction);
381
382 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
383 {
384         int result;
385         u8 d;
386
387         if (!ec || !data)
388                 return -EINVAL;
389
390         /*
391          * Query the EC to find out which _Qxx method we need to evaluate.
392          * Note that successful completion of the query causes the ACPI_EC_SCI
393          * bit to be cleared (and thus clearing the interrupt source).
394          */
395
396         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
397         if (result)
398                 return result;
399
400         if (!d)
401                 return -ENODATA;
402
403         *data = d;
404         return 0;
405 }
406
407 /* --------------------------------------------------------------------------
408                                 Event Management
409    -------------------------------------------------------------------------- */
410 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
411                               acpi_handle handle, acpi_ec_query_func func,
412                               void *data)
413 {
414         struct acpi_ec_query_handler *handler =
415             kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
416         if (!handler)
417                 return -ENOMEM;
418
419         handler->query_bit = query_bit;
420         handler->handle = handle;
421         handler->func = func;
422         handler->data = data;
423         mutex_lock(&ec->lock);
424         list_add_tail(&handler->node, &ec->list);
425         mutex_unlock(&ec->lock);
426         return 0;
427 }
428
429 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
430
431 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
432 {
433         struct acpi_ec_query_handler *handler;
434         mutex_lock(&ec->lock);
435         list_for_each_entry(handler, &ec->list, node) {
436                 if (query_bit == handler->query_bit) {
437                         list_del(&handler->node);
438                         kfree(handler);
439                         break;
440                 }
441         }
442         mutex_unlock(&ec->lock);
443 }
444
445 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
446
447 static void acpi_ec_gpe_query(void *ec_cxt)
448 {
449         struct acpi_ec *ec = ec_cxt;
450         u8 value = 0;
451         struct acpi_ec_query_handler *handler, copy;
452
453         if (!ec || acpi_ec_query(ec, &value))
454                 return;
455         mutex_lock(&ec->lock);
456         list_for_each_entry(handler, &ec->list, node) {
457                 if (value == handler->query_bit) {
458                         /* have custom handler for this bit */
459                         memcpy(&copy, handler, sizeof(copy));
460                         mutex_unlock(&ec->lock);
461                         if (copy.func) {
462                                 copy.func(copy.data);
463                         } else if (copy.handle) {
464                                 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
465                         }
466                         return;
467                 }
468         }
469         mutex_unlock(&ec->lock);
470         printk(KERN_ERR PREFIX "Handler for query 0x%x is not found!\n", value);
471 }
472
473 static u32 acpi_ec_gpe_handler(void *data)
474 {
475         acpi_status status = AE_OK;
476         u8 value;
477         struct acpi_ec *ec = data;
478
479         atomic_inc(&ec->event_count);
480
481         if (acpi_ec_mode == EC_INTR) {
482                 wake_up(&ec->wait);
483         }
484
485         value = acpi_ec_read_status(ec);
486         if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
487                 atomic_set(&ec->query_pending, 1);
488                 status =
489                     acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
490         }
491
492         return status == AE_OK ?
493             ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
494 }
495
496 /* --------------------------------------------------------------------------
497                              Address Space Management
498    -------------------------------------------------------------------------- */
499
500 static acpi_status
501 acpi_ec_space_setup(acpi_handle region_handle,
502                     u32 function, void *handler_context, void **return_context)
503 {
504         /*
505          * The EC object is in the handler context and is needed
506          * when calling the acpi_ec_space_handler.
507          */
508         *return_context = (function != ACPI_REGION_DEACTIVATE) ?
509             handler_context : NULL;
510
511         return AE_OK;
512 }
513
514 static acpi_status
515 acpi_ec_space_handler(u32 function, acpi_physical_address address,
516                       u32 bits, acpi_integer *value,
517                       void *handler_context, void *region_context)
518 {
519         struct acpi_ec *ec = handler_context;
520         int result = 0, i = 0;
521         u8 temp = 0;
522
523         if ((address > 0xFF) || !value || !handler_context)
524                 return AE_BAD_PARAMETER;
525
526         if (function != ACPI_READ && function != ACPI_WRITE)
527                 return AE_BAD_PARAMETER;
528
529         if (bits != 8 && acpi_strict)
530                 return AE_BAD_PARAMETER;
531
532         while (bits - i > 0) {
533                 if (function == ACPI_READ) {
534                         result = acpi_ec_read(ec, address, &temp);
535                         (*value) |= ((acpi_integer)temp) << i;
536                 } else {
537                         temp = 0xff & ((*value) >> i);
538                         result = acpi_ec_write(ec, address, temp);
539                 }
540                 i += 8;
541                 ++address;
542         }
543
544         switch (result) {
545         case -EINVAL:
546                 return AE_BAD_PARAMETER;
547                 break;
548         case -ENODEV:
549                 return AE_NOT_FOUND;
550                 break;
551         case -ETIME:
552                 return AE_TIME;
553                 break;
554         default:
555                 return AE_OK;
556         }
557 }
558
559 /* --------------------------------------------------------------------------
560                               FS Interface (/proc)
561    -------------------------------------------------------------------------- */
562
563 static struct proc_dir_entry *acpi_ec_dir;
564
565 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
566 {
567         struct acpi_ec *ec = seq->private;
568
569         if (!ec)
570                 goto end;
571
572         seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
573         seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
574                    (unsigned)ec->command_addr, (unsigned)ec->data_addr);
575         seq_printf(seq, "use global lock:\t%s\n",
576                    ec->global_lock ? "yes" : "no");
577       end:
578         return 0;
579 }
580
581 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
582 {
583         return single_open(file, acpi_ec_read_info, PDE(inode)->data);
584 }
585
586 static struct file_operations acpi_ec_info_ops = {
587         .open = acpi_ec_info_open_fs,
588         .read = seq_read,
589         .llseek = seq_lseek,
590         .release = single_release,
591         .owner = THIS_MODULE,
592 };
593
594 static int acpi_ec_add_fs(struct acpi_device *device)
595 {
596         struct proc_dir_entry *entry = NULL;
597
598         if (!acpi_device_dir(device)) {
599                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
600                                                      acpi_ec_dir);
601                 if (!acpi_device_dir(device))
602                         return -ENODEV;
603         }
604
605         entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
606                                   acpi_device_dir(device));
607         if (!entry)
608                 return -ENODEV;
609         else {
610                 entry->proc_fops = &acpi_ec_info_ops;
611                 entry->data = acpi_driver_data(device);
612                 entry->owner = THIS_MODULE;
613         }
614
615         return 0;
616 }
617
618 static int acpi_ec_remove_fs(struct acpi_device *device)
619 {
620
621         if (acpi_device_dir(device)) {
622                 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
623                 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
624                 acpi_device_dir(device) = NULL;
625         }
626
627         return 0;
628 }
629
630 /* --------------------------------------------------------------------------
631                                Driver Interface
632    -------------------------------------------------------------------------- */
633 static acpi_status
634 ec_parse_io_ports(struct acpi_resource *resource, void *context);
635
636 static struct acpi_ec *make_acpi_ec(void)
637 {
638         struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
639         if (!ec)
640                 return NULL;
641
642         atomic_set(&ec->query_pending, 1);
643         atomic_set(&ec->event_count, 1);
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 int ec_parse_device(struct acpi_ec *ec, acpi_handle handle)
665 {
666         if (ACPI_FAILURE(acpi_walk_resources(handle, METHOD_NAME__CRS,
667                                      ec_parse_io_ports, ec)))
668                 return -EINVAL;
669
670         /* Get GPE bit assignment (EC events). */
671         /* TODO: Add support for _GPE returning a package */
672         if (ACPI_FAILURE(acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe)))
673                 return -EINVAL;
674
675         /* Use the global lock for all EC transactions? */
676         acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
677
678         /* Find and register all query methods */
679         acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
680                             acpi_ec_register_query_methods, ec, NULL);
681
682         ec->handle = handle;
683
684         printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx",
685                           ec->gpe, ec->command_addr, ec->data_addr);
686
687         return 0;
688 }
689
690 static int acpi_ec_add(struct acpi_device *device)
691 {
692         struct acpi_ec *ec = NULL;
693
694         if (!device)
695                 return -EINVAL;
696
697         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
698         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
699
700         ec = make_acpi_ec();
701         if (!ec)
702                 return -ENOMEM;
703
704         if (ec_parse_device(ec, device->handle)) {
705                 kfree(ec);
706                 return -EINVAL;
707         }
708
709         /* Check if we found the boot EC */
710         if (boot_ec) {
711                 if (boot_ec->gpe == ec->gpe) {
712                         /* We might have incorrect info for GL at boot time */
713                         mutex_lock(&boot_ec->lock);
714                         boot_ec->global_lock = ec->global_lock;
715                         /* Copy handlers from new ec into boot ec */
716                         list_splice(&ec->list, &boot_ec->list);
717                         mutex_unlock(&boot_ec->lock);
718                         kfree(ec);
719                         ec = boot_ec;
720                 }
721         } else
722                 first_ec = ec;
723         ec->handle = device->handle;
724         acpi_driver_data(device) = ec;
725
726         acpi_ec_add_fs(device);
727         return 0;
728 }
729
730 static int acpi_ec_remove(struct acpi_device *device, int type)
731 {
732         struct acpi_ec *ec;
733         struct acpi_ec_query_handler *handler;
734
735         if (!device)
736                 return -EINVAL;
737
738         ec = acpi_driver_data(device);
739         mutex_lock(&ec->lock);
740         list_for_each_entry(handler, &ec->list, node) {
741                 list_del(&handler->node);
742                 kfree(handler);
743         }
744         mutex_unlock(&ec->lock);
745         acpi_ec_remove_fs(device);
746         acpi_driver_data(device) = NULL;
747         if (ec == first_ec)
748                 first_ec = NULL;
749
750         /* Don't touch boot EC */
751         if (boot_ec != ec)
752                 kfree(ec);
753         return 0;
754 }
755
756 static acpi_status
757 ec_parse_io_ports(struct acpi_resource *resource, void *context)
758 {
759         struct acpi_ec *ec = context;
760
761         if (resource->type != ACPI_RESOURCE_TYPE_IO)
762                 return AE_OK;
763
764         /*
765          * The first address region returned is the data port, and
766          * the second address region returned is the status/command
767          * port.
768          */
769         if (ec->data_addr == 0)
770                 ec->data_addr = resource->data.io.minimum;
771         else if (ec->command_addr == 0)
772                 ec->command_addr = resource->data.io.minimum;
773         else
774                 return AE_CTRL_TERMINATE;
775
776         return AE_OK;
777 }
778
779 static int ec_install_handlers(struct acpi_ec *ec)
780 {
781         acpi_status status;
782         status = acpi_install_gpe_handler(NULL, ec->gpe,
783                                           ACPI_GPE_EDGE_TRIGGERED,
784                                           &acpi_ec_gpe_handler, ec);
785         if (ACPI_FAILURE(status))
786                 return -ENODEV;
787
788         acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
789         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
790
791         status = acpi_install_address_space_handler(ec->handle,
792                                                     ACPI_ADR_SPACE_EC,
793                                                     &acpi_ec_space_handler,
794                                                     &acpi_ec_space_setup, ec);
795         if (ACPI_FAILURE(status)) {
796                 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
797                 return -ENODEV;
798         }
799
800         return 0;
801 }
802
803 static int acpi_ec_start(struct acpi_device *device)
804 {
805         struct acpi_ec *ec;
806         int ret = 0;
807
808         if (!device)
809                 return -EINVAL;
810
811         ec = acpi_driver_data(device);
812
813         if (!ec)
814                 return -EINVAL;
815
816         /* Boot EC is already working */
817         if (ec != boot_ec)
818                 ret = ec_install_handlers(ec);
819
820         /* EC is fully operational, allow queries */
821         atomic_set(&ec->query_pending, 0);
822
823         return ret;
824 }
825
826 static int acpi_ec_stop(struct acpi_device *device, int type)
827 {
828         acpi_status status;
829         struct acpi_ec *ec;
830
831         if (!device)
832                 return -EINVAL;
833
834         ec = acpi_driver_data(device);
835         if (!ec)
836                 return -EINVAL;
837
838         /* Don't touch boot EC */
839         if (ec == boot_ec)
840                 return 0;
841
842         status = acpi_remove_address_space_handler(ec->handle,
843                                                    ACPI_ADR_SPACE_EC,
844                                                    &acpi_ec_space_handler);
845         if (ACPI_FAILURE(status))
846                 return -ENODEV;
847
848         status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
849         if (ACPI_FAILURE(status))
850                 return -ENODEV;
851
852         return 0;
853 }
854
855 int __init acpi_ec_ecdt_probe(void)
856 {
857         int ret;
858         acpi_status status;
859         struct acpi_table_ecdt *ecdt_ptr;
860
861         boot_ec = make_acpi_ec();
862         if (!boot_ec)
863                 return -ENOMEM;
864         /*
865          * Generate a boot ec context
866          */
867
868         status = acpi_get_table(ACPI_SIG_ECDT, 1,
869                                 (struct acpi_table_header **)&ecdt_ptr);
870         if (ACPI_FAILURE(status))
871                 goto error;
872
873         printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
874
875         boot_ec->command_addr = ecdt_ptr->control.address;
876         boot_ec->data_addr = ecdt_ptr->data.address;
877         boot_ec->gpe = ecdt_ptr->gpe;
878         boot_ec->handle = ACPI_ROOT_OBJECT;
879
880         ret = ec_install_handlers(boot_ec);
881         if (!ret) {
882                 first_ec = boot_ec;
883                 return 0;
884         }
885       error:
886         kfree(boot_ec);
887         boot_ec = NULL;
888
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);