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