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