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