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