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