]> err.no Git - linux-2.6/blob - drivers/acpi/ec.c
ACPI: ec: add unlock in error path
[linux-2.6] / drivers / acpi / ec.c
1 /*
2  *  acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 38 $)
3  *
4  *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or (at
13  *  your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with this program; if not, write to the Free Software Foundation, Inc.,
22  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  */
26
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/delay.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/interrupt.h>
35 #include <asm/io.h>
36 #include <acpi/acpi_bus.h>
37 #include <acpi/acpi_drivers.h>
38 #include <acpi/actypes.h>
39
40 #define _COMPONENT              ACPI_EC_COMPONENT
41 ACPI_MODULE_NAME("acpi_ec")
42 #define ACPI_EC_COMPONENT               0x00100000
43 #define ACPI_EC_CLASS                   "embedded_controller"
44 #define ACPI_EC_HID                     "PNP0C09"
45 #define ACPI_EC_DRIVER_NAME             "ACPI Embedded Controller Driver"
46 #define ACPI_EC_DEVICE_NAME             "Embedded Controller"
47 #define ACPI_EC_FILE_INFO               "info"
48 #undef PREFIX
49 #define PREFIX                          "ACPI: EC: "
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 /* EC commands */
56 enum ec_command {
57         ACPI_EC_COMMAND_READ = 0x80,
58         ACPI_EC_COMMAND_WRITE = 0x81,
59         ACPI_EC_BURST_ENABLE = 0x82,
60         ACPI_EC_BURST_DISABLE = 0x83,
61         ACPI_EC_COMMAND_QUERY = 0x84,
62 };
63 /* EC events */
64 enum ec_event {
65         ACPI_EC_EVENT_OBF_1 = 1,        /* Output buffer full */
66         ACPI_EC_EVENT_IBF_0,    /* Input buffer empty */
67 };
68
69 #define ACPI_EC_DELAY           500     /* Wait 500ms max. during EC ops */
70 #define ACPI_EC_UDELAY_GLK      1000    /* Wait 1ms max. to get global lock */
71
72 static enum ec_mode {
73         EC_INTR = 1,            /* Output buffer full */
74         EC_POLL,                /* Input buffer empty */
75 } acpi_ec_mode = EC_INTR;
76
77 static int acpi_ec_remove(struct acpi_device *device, int type);
78 static int acpi_ec_start(struct acpi_device *device);
79 static int acpi_ec_stop(struct acpi_device *device, int type);
80 static int acpi_ec_add(struct acpi_device *device);
81
82 static struct acpi_driver acpi_ec_driver = {
83         .name = ACPI_EC_DRIVER_NAME,
84         .class = ACPI_EC_CLASS,
85         .ids = ACPI_EC_HID,
86         .ops = {
87                 .add = acpi_ec_add,
88                 .remove = acpi_ec_remove,
89                 .start = acpi_ec_start,
90                 .stop = acpi_ec_stop,
91                 },
92 };
93
94 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
95 static struct acpi_ec {
96         acpi_handle handle;
97         unsigned long uid;
98         unsigned long gpe;
99         unsigned long command_addr;
100         unsigned long data_addr;
101         unsigned long global_lock;
102         struct mutex lock;
103         atomic_t query_pending;
104         atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
105         wait_queue_head_t wait;
106 } *ec_ecdt;
107
108 /* External interfaces use first EC only, so remember */
109 static struct acpi_device *first_ec;
110
111 /* --------------------------------------------------------------------------
112                              Transaction Management
113    -------------------------------------------------------------------------- */
114
115 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
116 {
117         return inb(ec->command_addr);
118 }
119
120 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
121 {
122         return inb(ec->data_addr);
123 }
124
125 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
126 {
127         outb(command, ec->command_addr);
128 }
129
130 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
131 {
132         outb(data, ec->data_addr);
133 }
134
135 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
136 {
137         u8 status = acpi_ec_read_status(ec);
138
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 {
152         if (acpi_ec_mode == EC_POLL) {
153                 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
154                 while (time_before(jiffies, delay)) {
155                         if (acpi_ec_check_status(ec, event))
156                                 return 0;
157                 }
158         } else {
159                 if (wait_event_timeout(ec->wait,
160                                        acpi_ec_check_status(ec, event),
161                                        msecs_to_jiffies(ACPI_EC_DELAY)) ||
162                     acpi_ec_check_status(ec, event)) {
163                         return 0;
164                 } else {
165                         printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
166                                " status = %d, expect_event = %d\n",
167                                acpi_ec_read_status(ec), event);
168                 }
169         }
170
171         return -ETIME;
172 }
173
174 #ifdef ACPI_FUTURE_USAGE
175 /*
176  * Note: samsung nv5000 doesn't work with ec burst mode.
177  * http://bugzilla.kernel.org/show_bug.cgi?id=4980
178  */
179 int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
180 {
181         u8 tmp = 0;
182         u8 status = 0;
183
184         status = acpi_ec_read_status(ec);
185         if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
186                 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
187                 if (status)
188                         goto end;
189                 acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
190                 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
191                 tmp = acpi_ec_read_data(ec);
192                 if (tmp != 0x90) {      /* Burst ACK byte */
193                         return -EINVAL;
194                 }
195         }
196
197         atomic_set(&ec->leaving_burst, 0);
198         return 0;
199       end:
200         ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
201         return -1;
202 }
203
204 int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
205 {
206         u8 status = 0;
207
208         status = acpi_ec_read_status(ec);
209         if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)) {
210                 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
211                 if (status)
212                         goto end;
213                 acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
214                 acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
215         }
216         atomic_set(&ec->leaving_burst, 1);
217         return 0;
218       end:
219         ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
220         return -1;
221 }
222 #endif                          /* ACPI_FUTURE_USAGE */
223
224 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
225                                         const u8 * wdata, unsigned wdata_len,
226                                         u8 * rdata, unsigned rdata_len)
227 {
228         int result = 0;
229
230         acpi_ec_write_cmd(ec, command);
231
232         for (; wdata_len > 0; --wdata_len) {
233                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
234                 if (result) {
235                         printk(KERN_ERR PREFIX
236                                "write_cmd timeout, command = %d\n", command);
237                         goto end;
238                 }
239                 acpi_ec_write_data(ec, *(wdata++));
240         }
241
242         if (!rdata_len) {
243                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
244                 if (result) {
245                         printk(KERN_ERR PREFIX
246                                "finish-write timeout, command = %d\n", command);
247                         goto end;
248                 }
249         } else if (command == ACPI_EC_COMMAND_QUERY) {
250                 atomic_set(&ec->query_pending, 0);
251         }
252
253         for (; rdata_len > 0; --rdata_len) {
254                 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
255                 if (result) {
256                         printk(KERN_ERR PREFIX "read timeout, command = %d\n",
257                                command);
258                         goto end;
259                 }
260
261                 *(rdata++) = acpi_ec_read_data(ec);
262         }
263       end:
264         return result;
265 }
266
267 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
268                                const u8 * wdata, unsigned wdata_len,
269                                u8 * rdata, unsigned rdata_len)
270 {
271         int status;
272         u32 glk;
273
274         if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
275                 return -EINVAL;
276
277         if (rdata)
278                 memset(rdata, 0, rdata_len);
279
280         mutex_lock(&ec->lock);
281         if (ec->global_lock) {
282                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
283                 if (ACPI_FAILURE(status)) {
284                         mutex_unlock(&ec->lock);
285                         return -ENODEV;
286                 }
287         }
288
289         /* Make sure GPE is enabled before doing transaction */
290         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
291
292         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
293         if (status) {
294                 printk(KERN_DEBUG PREFIX
295                        "input buffer is not empty, aborting transaction\n");
296                 goto end;
297         }
298
299         status = acpi_ec_transaction_unlocked(ec, command,
300                                               wdata, wdata_len,
301                                               rdata, rdata_len);
302
303       end:
304
305         if (ec->global_lock)
306                 acpi_release_global_lock(glk);
307         mutex_unlock(&ec->lock);
308
309         return status;
310 }
311
312 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
313 {
314         int result;
315         u8 d;
316
317         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
318                                      &address, 1, &d, 1);
319         *data = d;
320         return result;
321 }
322
323 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
324 {
325         u8 wdata[2] = { address, data };
326         return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
327                                    wdata, 2, NULL, 0);
328 }
329
330 /*
331  * Externally callable EC access functions. For now, assume 1 EC only
332  */
333 int ec_read(u8 addr, u8 * val)
334 {
335         struct acpi_ec *ec;
336         int err;
337         u8 temp_data;
338
339         if (!first_ec)
340                 return -ENODEV;
341
342         ec = acpi_driver_data(first_ec);
343
344         err = acpi_ec_read(ec, addr, &temp_data);
345
346         if (!err) {
347                 *val = temp_data;
348                 return 0;
349         } else
350                 return err;
351 }
352
353 EXPORT_SYMBOL(ec_read);
354
355 int ec_write(u8 addr, u8 val)
356 {
357         struct acpi_ec *ec;
358         int err;
359
360         if (!first_ec)
361                 return -ENODEV;
362
363         ec = acpi_driver_data(first_ec);
364
365         err = acpi_ec_write(ec, addr, val);
366
367         return err;
368 }
369
370 EXPORT_SYMBOL(ec_write);
371
372 int ec_transaction(u8 command,
373                           const u8 * wdata, unsigned wdata_len,
374                           u8 * rdata, unsigned rdata_len)
375 {
376         struct acpi_ec *ec;
377
378         if (!first_ec)
379                 return -ENODEV;
380
381         ec = acpi_driver_data(first_ec);
382
383         return acpi_ec_transaction(ec, command, wdata,
384                                    wdata_len, rdata, rdata_len);
385 }
386
387 EXPORT_SYMBOL(ec_transaction);
388
389 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
390 {
391         int result;
392         u8 d;
393
394         if (!ec || !data)
395                 return -EINVAL;
396
397         /*
398          * Query the EC to find out which _Qxx method we need to evaluate.
399          * Note that successful completion of the query causes the ACPI_EC_SCI
400          * bit to be cleared (and thus clearing the interrupt source).
401          */
402
403         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
404         if (result)
405                 return result;
406
407         if (!d)
408                 return -ENODATA;
409
410         *data = d;
411         return 0;
412 }
413
414 /* --------------------------------------------------------------------------
415                                 Event Management
416    -------------------------------------------------------------------------- */
417
418 static void acpi_ec_gpe_query(void *ec_cxt)
419 {
420         struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
421         u8 value = 0;
422         char object_name[8];
423
424         if (!ec || acpi_ec_query(ec, &value))
425                 return;
426
427         snprintf(object_name, 8, "_Q%2.2X", value);
428
429         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
430
431         acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
432 }
433
434 static u32 acpi_ec_gpe_handler(void *data)
435 {
436         acpi_status status = AE_OK;
437         u8 value;
438         struct acpi_ec *ec = (struct acpi_ec *)data;
439
440         if (acpi_ec_mode == EC_INTR) {
441                 wake_up(&ec->wait);
442         }
443
444         value = acpi_ec_read_status(ec);
445         if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
446                 atomic_set(&ec->query_pending, 1);
447                 status =
448                     acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
449                                     ec);
450         }
451
452         return status == AE_OK ?
453             ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
454 }
455
456 /* --------------------------------------------------------------------------
457                              Address Space Management
458    -------------------------------------------------------------------------- */
459
460 static acpi_status
461 acpi_ec_space_setup(acpi_handle region_handle,
462                     u32 function, void *handler_context, void **return_context)
463 {
464         /*
465          * The EC object is in the handler context and is needed
466          * when calling the acpi_ec_space_handler.
467          */
468         *return_context = (function != ACPI_REGION_DEACTIVATE) ?
469             handler_context : NULL;
470
471         return AE_OK;
472 }
473
474 static acpi_status
475 acpi_ec_space_handler(u32 function,
476                       acpi_physical_address address,
477                       u32 bit_width,
478                       acpi_integer * value,
479                       void *handler_context, void *region_context)
480 {
481         int result = 0;
482         struct acpi_ec *ec = NULL;
483         u64 temp = *value;
484         acpi_integer f_v = 0;
485         int i = 0;
486
487         if ((address > 0xFF) || !value || !handler_context)
488                 return AE_BAD_PARAMETER;
489
490         if (bit_width != 8 && acpi_strict) {
491                 return AE_BAD_PARAMETER;
492         }
493
494         ec = (struct acpi_ec *)handler_context;
495
496       next_byte:
497         switch (function) {
498         case ACPI_READ:
499                 temp = 0;
500                 result = acpi_ec_read(ec, (u8) address, (u8 *) & temp);
501                 break;
502         case ACPI_WRITE:
503                 result = acpi_ec_write(ec, (u8) address, (u8) temp);
504                 break;
505         default:
506                 result = -EINVAL;
507                 goto out;
508                 break;
509         }
510
511         bit_width -= 8;
512         if (bit_width) {
513                 if (function == ACPI_READ)
514                         f_v |= temp << 8 * i;
515                 if (function == ACPI_WRITE)
516                         temp >>= 8;
517                 i++;
518                 address++;
519                 goto next_byte;
520         }
521
522         if (function == ACPI_READ) {
523                 f_v |= temp << 8 * i;
524                 *value = f_v;
525         }
526
527       out:
528         switch (result) {
529         case -EINVAL:
530                 return AE_BAD_PARAMETER;
531                 break;
532         case -ENODEV:
533                 return AE_NOT_FOUND;
534                 break;
535         case -ETIME:
536                 return AE_TIME;
537                 break;
538         default:
539                 return AE_OK;
540         }
541 }
542
543 /* --------------------------------------------------------------------------
544                               FS Interface (/proc)
545    -------------------------------------------------------------------------- */
546
547 static struct proc_dir_entry *acpi_ec_dir;
548
549 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
550 {
551         struct acpi_ec *ec = (struct acpi_ec *)seq->private;
552
553         if (!ec)
554                 goto end;
555
556         seq_printf(seq, "gpe:                 0x%02x\n", (u32) ec->gpe);
557         seq_printf(seq, "ports:                   0x%02x, 0x%02x\n",
558                    (u32) ec->command_addr, (u32) ec->data_addr);
559         seq_printf(seq, "use global lock:         %s\n",
560                    ec->global_lock ? "yes" : "no");
561         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
562
563       end:
564         return 0;
565 }
566
567 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
568 {
569         return single_open(file, acpi_ec_read_info, PDE(inode)->data);
570 }
571
572 static struct file_operations acpi_ec_info_ops = {
573         .open = acpi_ec_info_open_fs,
574         .read = seq_read,
575         .llseek = seq_lseek,
576         .release = single_release,
577         .owner = THIS_MODULE,
578 };
579
580 static int acpi_ec_add_fs(struct acpi_device *device)
581 {
582         struct proc_dir_entry *entry = NULL;
583
584         if (!acpi_device_dir(device)) {
585                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
586                                                      acpi_ec_dir);
587                 if (!acpi_device_dir(device))
588                         return -ENODEV;
589         }
590
591         entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
592                                   acpi_device_dir(device));
593         if (!entry)
594                 return -ENODEV;
595         else {
596                 entry->proc_fops = &acpi_ec_info_ops;
597                 entry->data = acpi_driver_data(device);
598                 entry->owner = THIS_MODULE;
599         }
600
601         return 0;
602 }
603
604 static int acpi_ec_remove_fs(struct acpi_device *device)
605 {
606
607         if (acpi_device_dir(device)) {
608                 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
609                 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
610                 acpi_device_dir(device) = NULL;
611         }
612
613         return 0;
614 }
615
616 /* --------------------------------------------------------------------------
617                                Driver Interface
618    -------------------------------------------------------------------------- */
619
620 static int acpi_ec_add(struct acpi_device *device)
621 {
622         int result = 0;
623         acpi_status status = AE_OK;
624         struct acpi_ec *ec = NULL;
625
626         if (!device)
627                 return -EINVAL;
628
629         ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
630         if (!ec)
631                 return -ENOMEM;
632
633         ec->handle = device->handle;
634         ec->uid = -1;
635         mutex_init(&ec->lock);
636         atomic_set(&ec->query_pending, 0);
637         if (acpi_ec_mode == EC_INTR) {
638                 atomic_set(&ec->leaving_burst, 1);
639                 init_waitqueue_head(&ec->wait);
640         }
641         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
642         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
643         acpi_driver_data(device) = ec;
644
645         /* Use the global lock for all EC transactions? */
646         acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
647
648         /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
649            http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
650         if (ec_ecdt) {
651                 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
652                                                   ACPI_ADR_SPACE_EC,
653                                                   &acpi_ec_space_handler);
654
655                 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
656                                         &acpi_ec_gpe_handler);
657
658                 kfree(ec_ecdt);
659         }
660
661         /* Get GPE bit assignment (EC events). */
662         /* TODO: Add support for _GPE returning a package */
663         status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe);
664         if (ACPI_FAILURE(status)) {
665                 ACPI_EXCEPTION((AE_INFO, status,
666                                 "Obtaining GPE bit assignment"));
667                 result = -ENODEV;
668                 goto end;
669         }
670
671         result = acpi_ec_add_fs(device);
672         if (result)
673                 goto end;
674
675         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
676                           acpi_device_name(device), acpi_device_bid(device),
677                           (u32) ec->gpe));
678
679         if (!first_ec)
680                 first_ec = device;
681
682       end:
683         if (result)
684                 kfree(ec);
685
686         return result;
687 }
688
689 static int acpi_ec_remove(struct acpi_device *device, int type)
690 {
691         struct acpi_ec *ec = NULL;
692
693         if (!device)
694                 return -EINVAL;
695
696         ec = acpi_driver_data(device);
697
698         acpi_ec_remove_fs(device);
699
700         kfree(ec);
701
702         return 0;
703 }
704
705 static acpi_status
706 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
707 {
708         struct acpi_ec *ec = (struct acpi_ec *)context;
709
710         if (resource->type != ACPI_RESOURCE_TYPE_IO) {
711                 return AE_OK;
712         }
713
714         /*
715          * The first address region returned is the data port, and
716          * the second address region returned is the status/command
717          * port.
718          */
719         if (ec->data_addr == 0) {
720                 ec->data_addr = resource->data.io.minimum;
721         } else if (ec->command_addr == 0) {
722                 ec->command_addr = resource->data.io.minimum;
723         } else {
724                 return AE_CTRL_TERMINATE;
725         }
726
727         return AE_OK;
728 }
729
730 static int acpi_ec_start(struct acpi_device *device)
731 {
732         acpi_status status = AE_OK;
733         struct acpi_ec *ec = NULL;
734
735         if (!device)
736                 return -EINVAL;
737
738         ec = acpi_driver_data(device);
739
740         if (!ec)
741                 return -EINVAL;
742
743         /*
744          * Get I/O port addresses. Convert to GAS format.
745          */
746         status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
747                                      acpi_ec_io_ports, ec);
748         if (ACPI_FAILURE(status) || ec->command_addr == 0) {
749                 ACPI_EXCEPTION((AE_INFO, status,
750                                 "Error getting I/O port addresses"));
751                 return -ENODEV;
752         }
753
754         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
755                           ec->gpe, ec->command_addr, ec->data_addr));
756
757         /*
758          * Install GPE handler
759          */
760         status = acpi_install_gpe_handler(NULL, ec->gpe,
761                                           ACPI_GPE_EDGE_TRIGGERED,
762                                           &acpi_ec_gpe_handler, ec);
763         if (ACPI_FAILURE(status)) {
764                 return -ENODEV;
765         }
766         acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
767         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
768
769         status = acpi_install_address_space_handler(ec->handle,
770                                                     ACPI_ADR_SPACE_EC,
771                                                     &acpi_ec_space_handler,
772                                                     &acpi_ec_space_setup, ec);
773         if (ACPI_FAILURE(status)) {
774                 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
775                 return -ENODEV;
776         }
777
778         return AE_OK;
779 }
780
781 static int acpi_ec_stop(struct acpi_device *device, int type)
782 {
783         acpi_status status = AE_OK;
784         struct acpi_ec *ec = NULL;
785
786         if (!device)
787                 return -EINVAL;
788
789         ec = acpi_driver_data(device);
790
791         status = acpi_remove_address_space_handler(ec->handle,
792                                                    ACPI_ADR_SPACE_EC,
793                                                    &acpi_ec_space_handler);
794         if (ACPI_FAILURE(status))
795                 return -ENODEV;
796
797         status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
798         if (ACPI_FAILURE(status))
799                 return -ENODEV;
800
801         return 0;
802 }
803
804 static acpi_status __init
805 acpi_fake_ecdt_callback(acpi_handle handle,
806                         u32 Level, void *context, void **retval)
807 {
808         acpi_status status;
809
810         mutex_init(&ec_ecdt->lock);
811         if (acpi_ec_mode == EC_INTR) {
812                 init_waitqueue_head(&ec_ecdt->wait);
813         }
814         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
815                                      acpi_ec_io_ports, ec_ecdt);
816         if (ACPI_FAILURE(status))
817                 return status;
818
819         ec_ecdt->uid = -1;
820         acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
821
822         status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->gpe);
823         if (ACPI_FAILURE(status))
824                 return status;
825         ec_ecdt->global_lock = TRUE;
826         ec_ecdt->handle = handle;
827
828         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
829                           ec_ecdt->gpe, ec_ecdt->command_addr,
830                           ec_ecdt->data_addr));
831
832         return AE_CTRL_TERMINATE;
833 }
834
835 /*
836  * Some BIOS (such as some from Gateway laptops) access EC region very early
837  * such as in BAT0._INI or EC._INI before an EC device is found and
838  * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
839  * required, but if EC regison is accessed early, it is required.
840  * The routine tries to workaround the BIOS bug by pre-scan EC device
841  * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
842  * op region (since _REG isn't invoked yet). The assumption is true for
843  * all systems found.
844  */
845 static int __init acpi_ec_fake_ecdt(void)
846 {
847         acpi_status status;
848         int ret = 0;
849
850         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
851
852         ec_ecdt = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
853         if (!ec_ecdt) {
854                 ret = -ENOMEM;
855                 goto error;
856         }
857
858         status = acpi_get_devices(ACPI_EC_HID,
859                                   acpi_fake_ecdt_callback, NULL, NULL);
860         if (ACPI_FAILURE(status)) {
861                 kfree(ec_ecdt);
862                 ec_ecdt = NULL;
863                 ret = -ENODEV;
864                 ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
865                 goto error;
866         }
867         return 0;
868       error:
869         return ret;
870 }
871
872 static int __init acpi_ec_get_real_ecdt(void)
873 {
874         acpi_status status;
875         struct acpi_table_ecdt *ecdt_ptr;
876
877         status = acpi_get_table(ACPI_SIG_ECDT, 1,
878                                 (struct acpi_table_header **)&ecdt_ptr);
879         if (ACPI_FAILURE(status))
880                 return -ENODEV;
881
882         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
883
884         /*
885          * Generate a temporary ec context to use until the namespace is scanned
886          */
887         ec_ecdt = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
888         if (!ec_ecdt)
889                 return -ENOMEM;
890
891         mutex_init(&ec_ecdt->lock);
892         if (acpi_ec_mode == EC_INTR) {
893                 init_waitqueue_head(&ec_ecdt->wait);
894         }
895         ec_ecdt->command_addr = ecdt_ptr->control.address;
896         ec_ecdt->data_addr = ecdt_ptr->data.address;
897         ec_ecdt->gpe = ecdt_ptr->gpe;
898         /* use the GL just to be safe */
899         ec_ecdt->global_lock = TRUE;
900         ec_ecdt->uid = ecdt_ptr->uid;
901
902         status = acpi_get_handle(NULL, ecdt_ptr->id, &ec_ecdt->handle);
903         if (ACPI_FAILURE(status)) {
904                 goto error;
905         }
906
907         return 0;
908       error:
909         ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
910         kfree(ec_ecdt);
911         ec_ecdt = NULL;
912
913         return -ENODEV;
914 }
915
916 static int __initdata acpi_fake_ecdt_enabled;
917 int __init acpi_ec_ecdt_probe(void)
918 {
919         acpi_status status;
920         int ret;
921
922         ret = acpi_ec_get_real_ecdt();
923         /* Try to make a fake ECDT */
924         if (ret && acpi_fake_ecdt_enabled) {
925                 ret = acpi_ec_fake_ecdt();
926         }
927
928         if (ret)
929                 return 0;
930
931         /*
932          * Install GPE handler
933          */
934         status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe,
935                                           ACPI_GPE_EDGE_TRIGGERED,
936                                           &acpi_ec_gpe_handler, ec_ecdt);
937         if (ACPI_FAILURE(status)) {
938                 goto error;
939         }
940         acpi_set_gpe_type(NULL, ec_ecdt->gpe, ACPI_GPE_TYPE_RUNTIME);
941         acpi_enable_gpe(NULL, ec_ecdt->gpe, ACPI_NOT_ISR);
942
943         status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
944                                                     ACPI_ADR_SPACE_EC,
945                                                     &acpi_ec_space_handler,
946                                                     &acpi_ec_space_setup,
947                                                     ec_ecdt);
948         if (ACPI_FAILURE(status)) {
949                 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
950                                         &acpi_ec_gpe_handler);
951                 goto error;
952         }
953
954         return 0;
955
956       error:
957         ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
958         kfree(ec_ecdt);
959         ec_ecdt = NULL;
960
961         return -ENODEV;
962 }
963
964 static int __init acpi_ec_init(void)
965 {
966         int result = 0;
967
968         if (acpi_disabled)
969                 return 0;
970
971         acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
972         if (!acpi_ec_dir)
973                 return -ENODEV;
974
975         /* Now register the driver for the EC */
976         result = acpi_bus_register_driver(&acpi_ec_driver);
977         if (result < 0) {
978                 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
979                 return -ENODEV;
980         }
981
982         return result;
983 }
984
985 subsys_initcall(acpi_ec_init);
986
987 /* EC driver currently not unloadable */
988 #if 0
989 static void __exit acpi_ec_exit(void)
990 {
991
992         acpi_bus_unregister_driver(&acpi_ec_driver);
993
994         remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
995
996         return;
997 }
998 #endif                          /* 0 */
999
1000 static int __init acpi_fake_ecdt_setup(char *str)
1001 {
1002         acpi_fake_ecdt_enabled = 1;
1003         return 1;
1004 }
1005
1006 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1007 static int __init acpi_ec_set_intr_mode(char *str)
1008 {
1009         int intr;
1010
1011         if (!get_option(&str, &intr))
1012                 return 0;
1013
1014         if (intr) {
1015                 acpi_ec_mode = EC_INTR;
1016         } else {
1017                 acpi_ec_mode = EC_POLL;
1018         }
1019         acpi_ec_driver.ops.add = acpi_ec_add;
1020         printk(KERN_NOTICE PREFIX "%s mode.\n",
1021                           intr ? "interrupt" : "polling");
1022
1023         return 1;
1024 }
1025
1026 __setup("ec_intr=", acpi_ec_set_intr_mode);