ACPI: delete extra #defines in /drivers/acpi/ drivers
[powerpc.git] / 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                         return -ENODEV;
284         }
285
286         /* Make sure GPE is enabled before doing transaction */
287         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
288
289         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
290         if (status) {
291                 printk(KERN_DEBUG PREFIX
292                        "input buffer is not empty, aborting transaction\n");
293                 goto end;
294         }
295
296         status = acpi_ec_transaction_unlocked(ec, command,
297                                               wdata, wdata_len,
298                                               rdata, rdata_len);
299
300       end:
301
302         if (ec->global_lock)
303                 acpi_release_global_lock(glk);
304         mutex_unlock(&ec->lock);
305
306         return status;
307 }
308
309 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
310 {
311         int result;
312         u8 d;
313
314         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
315                                      &address, 1, &d, 1);
316         *data = d;
317         return result;
318 }
319
320 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
321 {
322         u8 wdata[2] = { address, data };
323         return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
324                                    wdata, 2, NULL, 0);
325 }
326
327 /*
328  * Externally callable EC access functions. For now, assume 1 EC only
329  */
330 int ec_read(u8 addr, u8 * val)
331 {
332         struct acpi_ec *ec;
333         int err;
334         u8 temp_data;
335
336         if (!first_ec)
337                 return -ENODEV;
338
339         ec = acpi_driver_data(first_ec);
340
341         err = acpi_ec_read(ec, addr, &temp_data);
342
343         if (!err) {
344                 *val = temp_data;
345                 return 0;
346         } else
347                 return err;
348 }
349
350 EXPORT_SYMBOL(ec_read);
351
352 int ec_write(u8 addr, u8 val)
353 {
354         struct acpi_ec *ec;
355         int err;
356
357         if (!first_ec)
358                 return -ENODEV;
359
360         ec = acpi_driver_data(first_ec);
361
362         err = acpi_ec_write(ec, addr, val);
363
364         return err;
365 }
366
367 EXPORT_SYMBOL(ec_write);
368
369 int ec_transaction(u8 command,
370                           const u8 * wdata, unsigned wdata_len,
371                           u8 * rdata, unsigned rdata_len)
372 {
373         struct acpi_ec *ec;
374
375         if (!first_ec)
376                 return -ENODEV;
377
378         ec = acpi_driver_data(first_ec);
379
380         return acpi_ec_transaction(ec, command, wdata,
381                                    wdata_len, rdata, rdata_len);
382 }
383
384 EXPORT_SYMBOL(ec_transaction);
385
386 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
387 {
388         int result;
389         u8 d;
390
391         if (!ec || !data)
392                 return -EINVAL;
393
394         /*
395          * Query the EC to find out which _Qxx method we need to evaluate.
396          * Note that successful completion of the query causes the ACPI_EC_SCI
397          * bit to be cleared (and thus clearing the interrupt source).
398          */
399
400         result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
401         if (result)
402                 return result;
403
404         if (!d)
405                 return -ENODATA;
406
407         *data = d;
408         return 0;
409 }
410
411 /* --------------------------------------------------------------------------
412                                 Event Management
413    -------------------------------------------------------------------------- */
414
415 static void acpi_ec_gpe_query(void *ec_cxt)
416 {
417         struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
418         u8 value = 0;
419         char object_name[8];
420
421         if (!ec || acpi_ec_query(ec, &value))
422                 return;
423
424         snprintf(object_name, 8, "_Q%2.2X", value);
425
426         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
427
428         acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
429 }
430
431 static u32 acpi_ec_gpe_handler(void *data)
432 {
433         acpi_status status = AE_OK;
434         u8 value;
435         struct acpi_ec *ec = (struct acpi_ec *)data;
436
437         if (acpi_ec_mode == EC_INTR) {
438                 wake_up(&ec->wait);
439         }
440
441         value = acpi_ec_read_status(ec);
442         if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
443                 atomic_set(&ec->query_pending, 1);
444                 status =
445                     acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
446                                     ec);
447         }
448
449         return status == AE_OK ?
450             ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
451 }
452
453 /* --------------------------------------------------------------------------
454                              Address Space Management
455    -------------------------------------------------------------------------- */
456
457 static acpi_status
458 acpi_ec_space_setup(acpi_handle region_handle,
459                     u32 function, void *handler_context, void **return_context)
460 {
461         /*
462          * The EC object is in the handler context and is needed
463          * when calling the acpi_ec_space_handler.
464          */
465         *return_context = (function != ACPI_REGION_DEACTIVATE) ?
466             handler_context : NULL;
467
468         return AE_OK;
469 }
470
471 static acpi_status
472 acpi_ec_space_handler(u32 function,
473                       acpi_physical_address address,
474                       u32 bit_width,
475                       acpi_integer * value,
476                       void *handler_context, void *region_context)
477 {
478         int result = 0;
479         struct acpi_ec *ec = NULL;
480         u64 temp = *value;
481         acpi_integer f_v = 0;
482         int i = 0;
483
484         if ((address > 0xFF) || !value || !handler_context)
485                 return AE_BAD_PARAMETER;
486
487         if (bit_width != 8 && acpi_strict) {
488                 return AE_BAD_PARAMETER;
489         }
490
491         ec = (struct acpi_ec *)handler_context;
492
493       next_byte:
494         switch (function) {
495         case ACPI_READ:
496                 temp = 0;
497                 result = acpi_ec_read(ec, (u8) address, (u8 *) & temp);
498                 break;
499         case ACPI_WRITE:
500                 result = acpi_ec_write(ec, (u8) address, (u8) temp);
501                 break;
502         default:
503                 result = -EINVAL;
504                 goto out;
505                 break;
506         }
507
508         bit_width -= 8;
509         if (bit_width) {
510                 if (function == ACPI_READ)
511                         f_v |= temp << 8 * i;
512                 if (function == ACPI_WRITE)
513                         temp >>= 8;
514                 i++;
515                 address++;
516                 goto next_byte;
517         }
518
519         if (function == ACPI_READ) {
520                 f_v |= temp << 8 * i;
521                 *value = f_v;
522         }
523
524       out:
525         switch (result) {
526         case -EINVAL:
527                 return AE_BAD_PARAMETER;
528                 break;
529         case -ENODEV:
530                 return AE_NOT_FOUND;
531                 break;
532         case -ETIME:
533                 return AE_TIME;
534                 break;
535         default:
536                 return AE_OK;
537         }
538 }
539
540 /* --------------------------------------------------------------------------
541                               FS Interface (/proc)
542    -------------------------------------------------------------------------- */
543
544 static struct proc_dir_entry *acpi_ec_dir;
545
546 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
547 {
548         struct acpi_ec *ec = (struct acpi_ec *)seq->private;
549
550         if (!ec)
551                 goto end;
552
553         seq_printf(seq, "gpe:                 0x%02x\n", (u32) ec->gpe);
554         seq_printf(seq, "ports:                   0x%02x, 0x%02x\n",
555                    (u32) ec->command_addr, (u32) ec->data_addr);
556         seq_printf(seq, "use global lock:         %s\n",
557                    ec->global_lock ? "yes" : "no");
558         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
559
560       end:
561         return 0;
562 }
563
564 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
565 {
566         return single_open(file, acpi_ec_read_info, PDE(inode)->data);
567 }
568
569 static struct file_operations acpi_ec_info_ops = {
570         .open = acpi_ec_info_open_fs,
571         .read = seq_read,
572         .llseek = seq_lseek,
573         .release = single_release,
574         .owner = THIS_MODULE,
575 };
576
577 static int acpi_ec_add_fs(struct acpi_device *device)
578 {
579         struct proc_dir_entry *entry = NULL;
580
581         if (!acpi_device_dir(device)) {
582                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
583                                                      acpi_ec_dir);
584                 if (!acpi_device_dir(device))
585                         return -ENODEV;
586         }
587
588         entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
589                                   acpi_device_dir(device));
590         if (!entry)
591                 return -ENODEV;
592         else {
593                 entry->proc_fops = &acpi_ec_info_ops;
594                 entry->data = acpi_driver_data(device);
595                 entry->owner = THIS_MODULE;
596         }
597
598         return 0;
599 }
600
601 static int acpi_ec_remove_fs(struct acpi_device *device)
602 {
603
604         if (acpi_device_dir(device)) {
605                 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
606                 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
607                 acpi_device_dir(device) = NULL;
608         }
609
610         return 0;
611 }
612
613 /* --------------------------------------------------------------------------
614                                Driver Interface
615    -------------------------------------------------------------------------- */
616
617 static int acpi_ec_add(struct acpi_device *device)
618 {
619         int result = 0;
620         acpi_status status = AE_OK;
621         struct acpi_ec *ec = NULL;
622
623         if (!device)
624                 return -EINVAL;
625
626         ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
627         if (!ec)
628                 return -ENOMEM;
629
630         ec->handle = device->handle;
631         ec->uid = -1;
632         mutex_init(&ec->lock);
633         atomic_set(&ec->query_pending, 0);
634         if (acpi_ec_mode == EC_INTR) {
635                 atomic_set(&ec->leaving_burst, 1);
636                 init_waitqueue_head(&ec->wait);
637         }
638         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
639         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
640         acpi_driver_data(device) = ec;
641
642         /* Use the global lock for all EC transactions? */
643         acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
644
645         /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
646            http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
647         if (ec_ecdt) {
648                 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
649                                                   ACPI_ADR_SPACE_EC,
650                                                   &acpi_ec_space_handler);
651
652                 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
653                                         &acpi_ec_gpe_handler);
654
655                 kfree(ec_ecdt);
656         }
657
658         /* Get GPE bit assignment (EC events). */
659         /* TODO: Add support for _GPE returning a package */
660         status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe);
661         if (ACPI_FAILURE(status)) {
662                 ACPI_EXCEPTION((AE_INFO, status,
663                                 "Obtaining GPE bit assignment"));
664                 result = -ENODEV;
665                 goto end;
666         }
667
668         result = acpi_ec_add_fs(device);
669         if (result)
670                 goto end;
671
672         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
673                           acpi_device_name(device), acpi_device_bid(device),
674                           (u32) ec->gpe));
675
676         if (!first_ec)
677                 first_ec = device;
678
679       end:
680         if (result)
681                 kfree(ec);
682
683         return result;
684 }
685
686 static int acpi_ec_remove(struct acpi_device *device, int type)
687 {
688         struct acpi_ec *ec = NULL;
689
690         if (!device)
691                 return -EINVAL;
692
693         ec = acpi_driver_data(device);
694
695         acpi_ec_remove_fs(device);
696
697         kfree(ec);
698
699         return 0;
700 }
701
702 static acpi_status
703 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
704 {
705         struct acpi_ec *ec = (struct acpi_ec *)context;
706
707         if (resource->type != ACPI_RESOURCE_TYPE_IO) {
708                 return AE_OK;
709         }
710
711         /*
712          * The first address region returned is the data port, and
713          * the second address region returned is the status/command
714          * port.
715          */
716         if (ec->data_addr == 0) {
717                 ec->data_addr = resource->data.io.minimum;
718         } else if (ec->command_addr == 0) {
719                 ec->command_addr = resource->data.io.minimum;
720         } else {
721                 return AE_CTRL_TERMINATE;
722         }
723
724         return AE_OK;
725 }
726
727 static int acpi_ec_start(struct acpi_device *device)
728 {
729         acpi_status status = AE_OK;
730         struct acpi_ec *ec = NULL;
731
732         if (!device)
733                 return -EINVAL;
734
735         ec = acpi_driver_data(device);
736
737         if (!ec)
738                 return -EINVAL;
739
740         /*
741          * Get I/O port addresses. Convert to GAS format.
742          */
743         status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
744                                      acpi_ec_io_ports, ec);
745         if (ACPI_FAILURE(status) || ec->command_addr == 0) {
746                 ACPI_EXCEPTION((AE_INFO, status,
747                                 "Error getting I/O port addresses"));
748                 return -ENODEV;
749         }
750
751         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
752                           ec->gpe, ec->command_addr, ec->data_addr));
753
754         /*
755          * Install GPE handler
756          */
757         status = acpi_install_gpe_handler(NULL, ec->gpe,
758                                           ACPI_GPE_EDGE_TRIGGERED,
759                                           &acpi_ec_gpe_handler, ec);
760         if (ACPI_FAILURE(status)) {
761                 return -ENODEV;
762         }
763         acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
764         acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
765
766         status = acpi_install_address_space_handler(ec->handle,
767                                                     ACPI_ADR_SPACE_EC,
768                                                     &acpi_ec_space_handler,
769                                                     &acpi_ec_space_setup, ec);
770         if (ACPI_FAILURE(status)) {
771                 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
772                 return -ENODEV;
773         }
774
775         return AE_OK;
776 }
777
778 static int acpi_ec_stop(struct acpi_device *device, int type)
779 {
780         acpi_status status = AE_OK;
781         struct acpi_ec *ec = NULL;
782
783         if (!device)
784                 return -EINVAL;
785
786         ec = acpi_driver_data(device);
787
788         status = acpi_remove_address_space_handler(ec->handle,
789                                                    ACPI_ADR_SPACE_EC,
790                                                    &acpi_ec_space_handler);
791         if (ACPI_FAILURE(status))
792                 return -ENODEV;
793
794         status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
795         if (ACPI_FAILURE(status))
796                 return -ENODEV;
797
798         return 0;
799 }
800
801 static acpi_status __init
802 acpi_fake_ecdt_callback(acpi_handle handle,
803                         u32 Level, void *context, void **retval)
804 {
805         acpi_status status;
806
807         mutex_init(&ec_ecdt->lock);
808         if (acpi_ec_mode == EC_INTR) {
809                 init_waitqueue_head(&ec_ecdt->wait);
810         }
811         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
812                                      acpi_ec_io_ports, ec_ecdt);
813         if (ACPI_FAILURE(status))
814                 return status;
815
816         ec_ecdt->uid = -1;
817         acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
818
819         status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->gpe);
820         if (ACPI_FAILURE(status))
821                 return status;
822         ec_ecdt->global_lock = TRUE;
823         ec_ecdt->handle = handle;
824
825         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
826                           ec_ecdt->gpe, ec_ecdt->command_addr,
827                           ec_ecdt->data_addr));
828
829         return AE_CTRL_TERMINATE;
830 }
831
832 /*
833  * Some BIOS (such as some from Gateway laptops) access EC region very early
834  * such as in BAT0._INI or EC._INI before an EC device is found and
835  * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
836  * required, but if EC regison is accessed early, it is required.
837  * The routine tries to workaround the BIOS bug by pre-scan EC device
838  * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
839  * op region (since _REG isn't invoked yet). The assumption is true for
840  * all systems found.
841  */
842 static int __init acpi_ec_fake_ecdt(void)
843 {
844         acpi_status status;
845         int ret = 0;
846
847         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
848
849         ec_ecdt = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
850         if (!ec_ecdt) {
851                 ret = -ENOMEM;
852                 goto error;
853         }
854
855         status = acpi_get_devices(ACPI_EC_HID,
856                                   acpi_fake_ecdt_callback, NULL, NULL);
857         if (ACPI_FAILURE(status)) {
858                 kfree(ec_ecdt);
859                 ec_ecdt = NULL;
860                 ret = -ENODEV;
861                 ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
862                 goto error;
863         }
864         return 0;
865       error:
866         return ret;
867 }
868
869 static int __init acpi_ec_get_real_ecdt(void)
870 {
871         acpi_status status;
872         struct acpi_table_ecdt *ecdt_ptr;
873
874         status = acpi_get_table(ACPI_SIG_ECDT, 1,
875                                 (struct acpi_table_header **)&ecdt_ptr);
876         if (ACPI_FAILURE(status))
877                 return -ENODEV;
878
879         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
880
881         /*
882          * Generate a temporary ec context to use until the namespace is scanned
883          */
884         ec_ecdt = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
885         if (!ec_ecdt)
886                 return -ENOMEM;
887
888         mutex_init(&ec_ecdt->lock);
889         if (acpi_ec_mode == EC_INTR) {
890                 init_waitqueue_head(&ec_ecdt->wait);
891         }
892         ec_ecdt->command_addr = ecdt_ptr->control.address;
893         ec_ecdt->data_addr = ecdt_ptr->data.address;
894         ec_ecdt->gpe = ecdt_ptr->gpe;
895         /* use the GL just to be safe */
896         ec_ecdt->global_lock = TRUE;
897         ec_ecdt->uid = ecdt_ptr->uid;
898
899         status = acpi_get_handle(NULL, ecdt_ptr->id, &ec_ecdt->handle);
900         if (ACPI_FAILURE(status)) {
901                 goto error;
902         }
903
904         return 0;
905       error:
906         ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
907         kfree(ec_ecdt);
908         ec_ecdt = NULL;
909
910         return -ENODEV;
911 }
912
913 static int __initdata acpi_fake_ecdt_enabled;
914 int __init acpi_ec_ecdt_probe(void)
915 {
916         acpi_status status;
917         int ret;
918
919         ret = acpi_ec_get_real_ecdt();
920         /* Try to make a fake ECDT */
921         if (ret && acpi_fake_ecdt_enabled) {
922                 ret = acpi_ec_fake_ecdt();
923         }
924
925         if (ret)
926                 return 0;
927
928         /*
929          * Install GPE handler
930          */
931         status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe,
932                                           ACPI_GPE_EDGE_TRIGGERED,
933                                           &acpi_ec_gpe_handler, ec_ecdt);
934         if (ACPI_FAILURE(status)) {
935                 goto error;
936         }
937         acpi_set_gpe_type(NULL, ec_ecdt->gpe, ACPI_GPE_TYPE_RUNTIME);
938         acpi_enable_gpe(NULL, ec_ecdt->gpe, ACPI_NOT_ISR);
939
940         status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
941                                                     ACPI_ADR_SPACE_EC,
942                                                     &acpi_ec_space_handler,
943                                                     &acpi_ec_space_setup,
944                                                     ec_ecdt);
945         if (ACPI_FAILURE(status)) {
946                 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
947                                         &acpi_ec_gpe_handler);
948                 goto error;
949         }
950
951         return 0;
952
953       error:
954         ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
955         kfree(ec_ecdt);
956         ec_ecdt = NULL;
957
958         return -ENODEV;
959 }
960
961 static int __init acpi_ec_init(void)
962 {
963         int result = 0;
964
965         if (acpi_disabled)
966                 return 0;
967
968         acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
969         if (!acpi_ec_dir)
970                 return -ENODEV;
971
972         /* Now register the driver for the EC */
973         result = acpi_bus_register_driver(&acpi_ec_driver);
974         if (result < 0) {
975                 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
976                 return -ENODEV;
977         }
978
979         return result;
980 }
981
982 subsys_initcall(acpi_ec_init);
983
984 /* EC driver currently not unloadable */
985 #if 0
986 static void __exit acpi_ec_exit(void)
987 {
988
989         acpi_bus_unregister_driver(&acpi_ec_driver);
990
991         remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
992
993         return;
994 }
995 #endif                          /* 0 */
996
997 static int __init acpi_fake_ecdt_setup(char *str)
998 {
999         acpi_fake_ecdt_enabled = 1;
1000         return 1;
1001 }
1002
1003 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1004 static int __init acpi_ec_set_intr_mode(char *str)
1005 {
1006         int intr;
1007
1008         if (!get_option(&str, &intr))
1009                 return 0;
1010
1011         if (intr) {
1012                 acpi_ec_mode = EC_INTR;
1013         } else {
1014                 acpi_ec_mode = EC_POLL;
1015         }
1016         acpi_ec_driver.ops.add = acpi_ec_add;
1017         printk(KERN_NOTICE PREFIX "%s mode.\n",
1018                           intr ? "interrupt" : "polling");
1019
1020         return 1;
1021 }
1022
1023 __setup("ec_intr=", acpi_ec_set_intr_mode);