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