ACPI: EC acpi-ecdt-uid-hack
[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 #define ACPI_EC_FLAG_OBF        0x01    /* Output buffer full */
49 #define ACPI_EC_FLAG_IBF        0x02    /* Input buffer full */
50 #define ACPI_EC_FLAG_BURST      0x10    /* burst mode */
51 #define ACPI_EC_FLAG_SCI        0x20    /* EC-SCI occurred */
52 #define ACPI_EC_EVENT_OBF       0x01    /* Output buffer full */
53 #define ACPI_EC_EVENT_IBE       0x02    /* Input buffer empty */
54 #define ACPI_EC_DELAY           50      /* Wait 50ms max. during EC ops */
55 #define ACPI_EC_UDELAY_GLK      1000    /* Wait 1ms max. to get global lock */
56 #define ACPI_EC_UDELAY         100      /* Poll @ 100us increments */
57 #define ACPI_EC_UDELAY_COUNT   1000     /* Wait 10ms max. during EC ops */
58 #define ACPI_EC_COMMAND_READ    0x80
59 #define ACPI_EC_COMMAND_WRITE   0x81
60 #define ACPI_EC_BURST_ENABLE    0x82
61 #define ACPI_EC_BURST_DISABLE   0x83
62 #define ACPI_EC_COMMAND_QUERY   0x84
63 #define EC_POLL                 0xFF
64 #define EC_INTR                 0x00
65 static int acpi_ec_remove(struct acpi_device *device, int type);
66 static int acpi_ec_start(struct acpi_device *device);
67 static int acpi_ec_stop(struct acpi_device *device, int type);
68 static int acpi_ec_intr_add(struct acpi_device *device);
69 static int acpi_ec_poll_add(struct acpi_device *device);
70
71 static struct acpi_driver acpi_ec_driver = {
72         .name = ACPI_EC_DRIVER_NAME,
73         .class = ACPI_EC_CLASS,
74         .ids = ACPI_EC_HID,
75         .ops = {
76                 .add = acpi_ec_intr_add,
77                 .remove = acpi_ec_remove,
78                 .start = acpi_ec_start,
79                 .stop = acpi_ec_stop,
80                 },
81 };
82 union acpi_ec {
83         struct {
84                 u32 mode;
85                 acpi_handle handle;
86                 unsigned long uid;
87                 unsigned long gpe_bit;
88                 struct acpi_generic_address status_addr;
89                 struct acpi_generic_address command_addr;
90                 struct acpi_generic_address data_addr;
91                 unsigned long global_lock;
92         } common;
93
94         struct {
95                 u32 mode;
96                 acpi_handle handle;
97                 unsigned long uid;
98                 unsigned long gpe_bit;
99                 struct acpi_generic_address status_addr;
100                 struct acpi_generic_address command_addr;
101                 struct acpi_generic_address data_addr;
102                 unsigned long global_lock;
103                 unsigned int expect_event;
104                 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
105                 atomic_t pending_gpe;
106                 struct semaphore sem;
107                 wait_queue_head_t wait;
108         } intr;
109
110         struct {
111                 u32 mode;
112                 acpi_handle handle;
113                 unsigned long uid;
114                 unsigned long gpe_bit;
115                 struct acpi_generic_address status_addr;
116                 struct acpi_generic_address command_addr;
117                 struct acpi_generic_address data_addr;
118                 unsigned long global_lock;
119                 spinlock_t lock;
120         } poll;
121 };
122
123 static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event);
124 static int acpi_ec_intr_wait(union acpi_ec *ec, unsigned int event);
125 static int acpi_ec_poll_read(union acpi_ec *ec, u8 address, u32 * data);
126 static int acpi_ec_intr_read(union acpi_ec *ec, u8 address, u32 * data);
127 static int acpi_ec_poll_write(union acpi_ec *ec, u8 address, u8 data);
128 static int acpi_ec_intr_write(union acpi_ec *ec, u8 address, u8 data);
129 static int acpi_ec_poll_query(union acpi_ec *ec, u32 * data);
130 static int acpi_ec_intr_query(union acpi_ec *ec, u32 * data);
131 static void acpi_ec_gpe_poll_query(void *ec_cxt);
132 static void acpi_ec_gpe_intr_query(void *ec_cxt);
133 static u32 acpi_ec_gpe_poll_handler(void *data);
134 static u32 acpi_ec_gpe_intr_handler(void *data);
135 static acpi_status __init
136 acpi_fake_ecdt_poll_callback(acpi_handle handle,
137                                 u32 Level, void *context, void **retval);
138
139 static acpi_status __init
140 acpi_fake_ecdt_intr_callback(acpi_handle handle,
141                               u32 Level, void *context, void **retval);
142
143 static int __init acpi_ec_poll_get_real_ecdt(void);
144 static int __init acpi_ec_intr_get_real_ecdt(void);
145 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
146 static union acpi_ec *ec_ecdt;
147
148 /* External interfaces use first EC only, so remember */
149 static struct acpi_device *first_ec;
150 static int acpi_ec_poll_mode = EC_INTR;
151
152 /* --------------------------------------------------------------------------
153                              Transaction Management
154    -------------------------------------------------------------------------- */
155
156 static u32 acpi_ec_read_status(union acpi_ec *ec)
157 {
158         u32 status = 0;
159
160         acpi_hw_low_level_read(8, &status, &ec->common.status_addr);
161         return status;
162 }
163
164 static int acpi_ec_wait(union acpi_ec *ec, u8 event)
165 {
166         if (acpi_ec_poll_mode)
167                 return acpi_ec_poll_wait(ec, event);
168         else
169                 return acpi_ec_intr_wait(ec, event);
170 }
171
172 static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event)
173 {
174         u32 acpi_ec_status = 0;
175         u32 i = ACPI_EC_UDELAY_COUNT;
176
177         if (!ec)
178                 return -EINVAL;
179
180         /* Poll the EC status register waiting for the event to occur. */
181         switch (event) {
182         case ACPI_EC_EVENT_OBF:
183                 do {
184                         acpi_hw_low_level_read(8, &acpi_ec_status,
185                                                &ec->common.status_addr);
186                         if (acpi_ec_status & ACPI_EC_FLAG_OBF)
187                                 return 0;
188                         udelay(ACPI_EC_UDELAY);
189                 } while (--i > 0);
190                 break;
191         case ACPI_EC_EVENT_IBE:
192                 do {
193                         acpi_hw_low_level_read(8, &acpi_ec_status,
194                                                &ec->common.status_addr);
195                         if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
196                                 return 0;
197                         udelay(ACPI_EC_UDELAY);
198                 } while (--i > 0);
199                 break;
200         default:
201                 return -EINVAL;
202         }
203
204         return -ETIME;
205 }
206 static int acpi_ec_intr_wait(union acpi_ec *ec, unsigned int event)
207 {
208         int result = 0;
209
210         ACPI_FUNCTION_TRACE("acpi_ec_wait");
211
212         ec->intr.expect_event = event;
213         smp_mb();
214
215         switch (event) {
216         case ACPI_EC_EVENT_IBE:
217                 if (~acpi_ec_read_status(ec) & event) {
218                         ec->intr.expect_event = 0;
219                         return_VALUE(0);
220                 }
221                 break;
222         default:
223                 break;
224         }
225
226         result = wait_event_timeout(ec->intr.wait,
227                                     !ec->intr.expect_event,
228                                     msecs_to_jiffies(ACPI_EC_DELAY));
229
230         ec->intr.expect_event = 0;
231         smp_mb();
232
233         /*
234          * Verify that the event in question has actually happened by
235          * querying EC status. Do the check even if operation timed-out
236          * to make sure that we did not miss interrupt.
237          */
238         switch (event) {
239         case ACPI_EC_EVENT_OBF:
240                 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
241                         return_VALUE(0);
242                 break;
243
244         case ACPI_EC_EVENT_IBE:
245                 if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
246                         return_VALUE(0);
247                 break;
248         }
249
250         return_VALUE(-ETIME);
251 }
252
253 #ifdef ACPI_FUTURE_USAGE
254 /*
255  * Note: samsung nv5000 doesn't work with ec burst mode.
256  * http://bugzilla.kernel.org/show_bug.cgi?id=4980
257  */
258 int acpi_ec_enter_burst_mode(union acpi_ec *ec)
259 {
260         u32 tmp = 0;
261         int status = 0;
262
263         ACPI_FUNCTION_TRACE("acpi_ec_enter_burst_mode");
264
265         status = acpi_ec_read_status(ec);
266         if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
267                 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
268                 if (status)
269                         goto end;
270                 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE,
271                                         &ec->common.command_addr);
272                 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
273                 acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
274                 if (tmp != 0x90) {      /* Burst ACK byte */
275                         return_VALUE(-EINVAL);
276                 }
277         }
278
279         atomic_set(&ec->intr.leaving_burst, 0);
280         return_VALUE(0);
281       end:
282         printk(KERN_WARNING PREFIX "Error in acpi_ec_wait\n");
283         return_VALUE(-1);
284 }
285
286 int acpi_ec_leave_burst_mode(union acpi_ec *ec)
287 {
288         int status = 0;
289
290         ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
291
292         status = acpi_ec_read_status(ec);
293         if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
294                 status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
295                 if(status)
296                         goto end;
297                 acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->common.command_addr);
298                 acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
299         } 
300         atomic_set(&ec->intr.leaving_burst, 1);
301         return_VALUE(0);
302 end:
303         printk(KERN_WARNING PREFIX "leave burst_mode:error\n");
304         return_VALUE(-1);
305 }
306 #endif /* ACPI_FUTURE_USAGE */
307
308 static int acpi_ec_read(union acpi_ec *ec, u8 address, u32 * data)
309 {
310         if (acpi_ec_poll_mode)
311                 return acpi_ec_poll_read(ec, address, data);
312         else
313                 return acpi_ec_intr_read(ec, address, data);
314 }
315 static int acpi_ec_write(union acpi_ec *ec, u8 address, u8 data)
316 {
317         if (acpi_ec_poll_mode)
318                 return acpi_ec_poll_write(ec, address, data);
319         else
320                 return acpi_ec_intr_write(ec, address, data);
321 }
322 static int acpi_ec_poll_read(union acpi_ec *ec, u8 address, u32 * data)
323 {
324         acpi_status status = AE_OK;
325         int result = 0;
326         unsigned long flags = 0;
327         u32 glk = 0;
328
329         ACPI_FUNCTION_TRACE("acpi_ec_read");
330
331         if (!ec || !data)
332                 return_VALUE(-EINVAL);
333
334         *data = 0;
335
336         if (ec->common.global_lock) {
337                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
338                 if (ACPI_FAILURE(status))
339                         return_VALUE(-ENODEV);
340         }
341
342         spin_lock_irqsave(&ec->poll.lock, flags);
343
344         acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
345                                 &ec->common.command_addr);
346         result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
347         if (result)
348                 goto end;
349
350         acpi_hw_low_level_write(8, address, &ec->common.data_addr);
351         result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
352         if (result)
353                 goto end;
354
355         acpi_hw_low_level_read(8, data, &ec->common.data_addr);
356
357         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
358                           *data, address));
359
360       end:
361         spin_unlock_irqrestore(&ec->poll.lock, flags);
362
363         if (ec->common.global_lock)
364                 acpi_release_global_lock(glk);
365
366         return_VALUE(result);
367 }
368
369 static int acpi_ec_poll_write(union acpi_ec *ec, u8 address, u8 data)
370 {
371         int result = 0;
372         acpi_status status = AE_OK;
373         unsigned long flags = 0;
374         u32 glk = 0;
375
376         ACPI_FUNCTION_TRACE("acpi_ec_write");
377
378         if (!ec)
379                 return_VALUE(-EINVAL);
380
381         if (ec->common.global_lock) {
382                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
383                 if (ACPI_FAILURE(status))
384                         return_VALUE(-ENODEV);
385         }
386
387         spin_lock_irqsave(&ec->poll.lock, flags);
388
389         acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
390                                 &ec->common.command_addr);
391         result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
392         if (result)
393                 goto end;
394
395         acpi_hw_low_level_write(8, address, &ec->common.data_addr);
396         result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
397         if (result)
398                 goto end;
399
400         acpi_hw_low_level_write(8, data, &ec->common.data_addr);
401         result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
402         if (result)
403                 goto end;
404
405         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
406                           data, address));
407
408       end:
409         spin_unlock_irqrestore(&ec->poll.lock, flags);
410
411         if (ec->common.global_lock)
412                 acpi_release_global_lock(glk);
413
414         return_VALUE(result);
415 }
416
417 static int acpi_ec_intr_read(union acpi_ec *ec, u8 address, u32 * data)
418 {
419         int status = 0;
420         u32 glk;
421
422         ACPI_FUNCTION_TRACE("acpi_ec_read");
423
424         if (!ec || !data)
425                 return_VALUE(-EINVAL);
426
427         *data = 0;
428
429         if (ec->common.global_lock) {
430                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
431                 if (ACPI_FAILURE(status))
432                         return_VALUE(-ENODEV);
433         }
434
435         WARN_ON(in_interrupt());
436         down(&ec->intr.sem);
437
438         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
439         if (status) {
440                 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
441                 goto end;
442         }
443         acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
444                                 &ec->common.command_addr);
445         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
446         if (status) {
447                 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
448         }
449
450         acpi_hw_low_level_write(8, address, &ec->common.data_addr);
451         status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
452         if (status) {
453                 printk(KERN_DEBUG PREFIX "read EC, OB not full\n");
454                 goto end;
455         }
456         acpi_hw_low_level_read(8, data, &ec->common.data_addr);
457         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
458                           *data, address));
459
460       end:
461         up(&ec->intr.sem);
462
463         if (ec->common.global_lock)
464                 acpi_release_global_lock(glk);
465
466         return_VALUE(status);
467 }
468
469 static int acpi_ec_intr_write(union acpi_ec *ec, u8 address, u8 data)
470 {
471         int status = 0;
472         u32 glk;
473
474         ACPI_FUNCTION_TRACE("acpi_ec_write");
475
476         if (!ec)
477                 return_VALUE(-EINVAL);
478
479         if (ec->common.global_lock) {
480                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
481                 if (ACPI_FAILURE(status))
482                         return_VALUE(-ENODEV);
483         }
484
485         WARN_ON(in_interrupt());
486         down(&ec->intr.sem);
487
488         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
489         if (status) {
490                 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
491         }
492         acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
493                                 &ec->common.command_addr);
494         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
495         if (status) {
496                 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
497         }
498
499         acpi_hw_low_level_write(8, address, &ec->common.data_addr);
500         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
501         if (status) {
502                 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
503         }
504
505         acpi_hw_low_level_write(8, data, &ec->common.data_addr);
506
507         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
508                           data, address));
509
510         up(&ec->intr.sem);
511
512         if (ec->common.global_lock)
513                 acpi_release_global_lock(glk);
514
515         return_VALUE(status);
516 }
517
518 /*
519  * Externally callable EC access functions. For now, assume 1 EC only
520  */
521 int ec_read(u8 addr, u8 * val)
522 {
523         union acpi_ec *ec;
524         int err;
525         u32 temp_data;
526
527         if (!first_ec)
528                 return -ENODEV;
529
530         ec = acpi_driver_data(first_ec);
531
532         err = acpi_ec_read(ec, addr, &temp_data);
533
534         if (!err) {
535                 *val = temp_data;
536                 return 0;
537         } else
538                 return err;
539 }
540
541 EXPORT_SYMBOL(ec_read);
542
543 int ec_write(u8 addr, u8 val)
544 {
545         union acpi_ec *ec;
546         int err;
547
548         if (!first_ec)
549                 return -ENODEV;
550
551         ec = acpi_driver_data(first_ec);
552
553         err = acpi_ec_write(ec, addr, val);
554
555         return err;
556 }
557
558 EXPORT_SYMBOL(ec_write);
559
560 static int acpi_ec_query(union acpi_ec *ec, u32 * data)
561 {
562         if (acpi_ec_poll_mode)
563                 return acpi_ec_poll_query(ec, data);
564         else
565                 return acpi_ec_intr_query(ec, data);
566 }
567 static int acpi_ec_poll_query(union acpi_ec *ec, u32 * data)
568 {
569         int result = 0;
570         acpi_status status = AE_OK;
571         unsigned long flags = 0;
572         u32 glk = 0;
573
574         ACPI_FUNCTION_TRACE("acpi_ec_query");
575
576         if (!ec || !data)
577                 return_VALUE(-EINVAL);
578
579         *data = 0;
580
581         if (ec->common.global_lock) {
582                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
583                 if (ACPI_FAILURE(status))
584                         return_VALUE(-ENODEV);
585         }
586
587         /*
588          * Query the EC to find out which _Qxx method we need to evaluate.
589          * Note that successful completion of the query causes the ACPI_EC_SCI
590          * bit to be cleared (and thus clearing the interrupt source).
591          */
592         spin_lock_irqsave(&ec->poll.lock, flags);
593
594         acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
595                                 &ec->common.command_addr);
596         result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
597         if (result)
598                 goto end;
599
600         acpi_hw_low_level_read(8, data, &ec->common.data_addr);
601         if (!*data)
602                 result = -ENODATA;
603
604       end:
605         spin_unlock_irqrestore(&ec->poll.lock, flags);
606
607         if (ec->common.global_lock)
608                 acpi_release_global_lock(glk);
609
610         return_VALUE(result);
611 }
612 static int acpi_ec_intr_query(union acpi_ec *ec, u32 * data)
613 {
614         int status = 0;
615         u32 glk;
616
617         ACPI_FUNCTION_TRACE("acpi_ec_query");
618
619         if (!ec || !data)
620                 return_VALUE(-EINVAL);
621         *data = 0;
622
623         if (ec->common.global_lock) {
624                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
625                 if (ACPI_FAILURE(status))
626                         return_VALUE(-ENODEV);
627         }
628
629         down(&ec->intr.sem);
630
631         status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
632         if (status) {
633                 printk(KERN_DEBUG PREFIX "query EC, IB not empty\n");
634                 goto end;
635         }
636         /*
637          * Query the EC to find out which _Qxx method we need to evaluate.
638          * Note that successful completion of the query causes the ACPI_EC_SCI
639          * bit to be cleared (and thus clearing the interrupt source).
640          */
641         acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
642                                 &ec->common.command_addr);
643         status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
644         if (status) {
645                 printk(KERN_DEBUG PREFIX "query EC, OB not full\n");
646                 goto end;
647         }
648
649         acpi_hw_low_level_read(8, data, &ec->common.data_addr);
650         if (!*data)
651                 status = -ENODATA;
652
653       end:
654         up(&ec->intr.sem);
655
656         if (ec->common.global_lock)
657                 acpi_release_global_lock(glk);
658
659         return_VALUE(status);
660 }
661
662 /* --------------------------------------------------------------------------
663                                 Event Management
664    -------------------------------------------------------------------------- */
665
666 union acpi_ec_query_data {
667         acpi_handle handle;
668         u8 data;
669 };
670
671 static void acpi_ec_gpe_query(void *ec_cxt)
672 {
673         if (acpi_ec_poll_mode)
674                 acpi_ec_gpe_poll_query(ec_cxt);
675         else
676                 acpi_ec_gpe_intr_query(ec_cxt);
677 }
678
679 static void acpi_ec_gpe_poll_query(void *ec_cxt)
680 {
681         union acpi_ec *ec = (union acpi_ec *)ec_cxt;
682         u32 value = 0;
683         unsigned long flags = 0;
684         static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
685         const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
686                 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
687         };
688
689         ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
690
691         if (!ec_cxt)
692                 goto end;
693
694         spin_lock_irqsave(&ec->poll.lock, flags);
695         acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
696         spin_unlock_irqrestore(&ec->poll.lock, flags);
697
698         /* TBD: Implement asynch events!
699          * NOTE: All we care about are EC-SCI's.  Other EC events are
700          * handled via polling (yuck!).  This is because some systems
701          * treat EC-SCIs as level (versus EDGE!) triggered, preventing
702          *  a purely interrupt-driven approach (grumble, grumble).
703          */
704         if (!(value & ACPI_EC_FLAG_SCI))
705                 goto end;
706
707         if (acpi_ec_query(ec, &value))
708                 goto end;
709
710         object_name[2] = hex[((value >> 4) & 0x0F)];
711         object_name[3] = hex[(value & 0x0F)];
712
713         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
714
715         acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
716
717       end:
718         acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
719 }
720 static void acpi_ec_gpe_intr_query(void *ec_cxt)
721 {
722         union acpi_ec *ec = (union acpi_ec *)ec_cxt;
723         u32 value;
724         int result = -ENODATA;
725         static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
726         const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
727                 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
728         };
729
730         ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
731
732         if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
733                 result = acpi_ec_query(ec, &value);
734
735         if (result)
736                 goto end;
737
738         object_name[2] = hex[((value >> 4) & 0x0F)];
739         object_name[3] = hex[(value & 0x0F)];
740
741         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
742
743         acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
744       end:
745         atomic_dec(&ec->intr.pending_gpe);
746         return;
747 }
748
749 static u32 acpi_ec_gpe_handler(void *data)
750 {
751         if (acpi_ec_poll_mode)
752                 return acpi_ec_gpe_poll_handler(data);
753         else
754                 return acpi_ec_gpe_intr_handler(data);
755 }
756 static u32 acpi_ec_gpe_poll_handler(void *data)
757 {
758         acpi_status status = AE_OK;
759         union acpi_ec *ec = (union acpi_ec *)data;
760
761         if (!ec)
762                 return ACPI_INTERRUPT_NOT_HANDLED;
763
764         acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
765
766         status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
767                                              acpi_ec_gpe_query, ec);
768
769         if (status == AE_OK)
770                 return ACPI_INTERRUPT_HANDLED;
771         else
772                 return ACPI_INTERRUPT_NOT_HANDLED;
773 }
774 static u32 acpi_ec_gpe_intr_handler(void *data)
775 {
776         acpi_status status = AE_OK;
777         u32 value;
778         union acpi_ec *ec = (union acpi_ec *)data;
779
780         if (!ec)
781                 return ACPI_INTERRUPT_NOT_HANDLED;
782
783         acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
784         value = acpi_ec_read_status(ec);
785
786         switch (ec->intr.expect_event) {
787         case ACPI_EC_EVENT_OBF:
788                 if (!(value & ACPI_EC_FLAG_OBF))
789                         break;
790         case ACPI_EC_EVENT_IBE:
791                 if ((value & ACPI_EC_FLAG_IBF))
792                         break;
793                 ec->intr.expect_event = 0;
794                 wake_up(&ec->intr.wait);
795                 return ACPI_INTERRUPT_HANDLED;
796         default:
797                 break;
798         }
799
800         if (value & ACPI_EC_FLAG_SCI) {
801                 atomic_add(1, &ec->intr.pending_gpe);
802                 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
803                                                      acpi_ec_gpe_query, ec);
804                 return status == AE_OK ?
805                     ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
806         }
807         acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
808         return status == AE_OK ?
809             ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
810 }
811
812 /* --------------------------------------------------------------------------
813                              Address Space Management
814    -------------------------------------------------------------------------- */
815
816 static acpi_status
817 acpi_ec_space_setup(acpi_handle region_handle,
818                     u32 function, void *handler_context, void **return_context)
819 {
820         /*
821          * The EC object is in the handler context and is needed
822          * when calling the acpi_ec_space_handler.
823          */
824         *return_context = (function != ACPI_REGION_DEACTIVATE) ?
825             handler_context : NULL;
826
827         return AE_OK;
828 }
829
830 static acpi_status
831 acpi_ec_space_handler(u32 function,
832                       acpi_physical_address address,
833                       u32 bit_width,
834                       acpi_integer * value,
835                       void *handler_context, void *region_context)
836 {
837         int result = 0;
838         union acpi_ec *ec = NULL;
839         u64 temp = *value;
840         acpi_integer f_v = 0;
841         int i = 0;
842
843         ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
844
845         if ((address > 0xFF) || !value || !handler_context)
846                 return_VALUE(AE_BAD_PARAMETER);
847
848         if (bit_width != 8 && acpi_strict) {
849                 printk(KERN_WARNING PREFIX
850                        "acpi_ec_space_handler: bit_width should be 8\n");
851                 return_VALUE(AE_BAD_PARAMETER);
852         }
853
854         ec = (union acpi_ec *)handler_context;
855
856       next_byte:
857         switch (function) {
858         case ACPI_READ:
859                 temp = 0;
860                 result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
861                 break;
862         case ACPI_WRITE:
863                 result = acpi_ec_write(ec, (u8) address, (u8) temp);
864                 break;
865         default:
866                 result = -EINVAL;
867                 goto out;
868                 break;
869         }
870
871         bit_width -= 8;
872         if (bit_width) {
873                 if (function == ACPI_READ)
874                         f_v |= temp << 8 * i;
875                 if (function == ACPI_WRITE)
876                         temp >>= 8;
877                 i++;
878                 address++;
879                 goto next_byte;
880         }
881
882         if (function == ACPI_READ) {
883                 f_v |= temp << 8 * i;
884                 *value = f_v;
885         }
886
887       out:
888         switch (result) {
889         case -EINVAL:
890                 return_VALUE(AE_BAD_PARAMETER);
891                 break;
892         case -ENODEV:
893                 return_VALUE(AE_NOT_FOUND);
894                 break;
895         case -ETIME:
896                 return_VALUE(AE_TIME);
897                 break;
898         default:
899                 return_VALUE(AE_OK);
900         }
901 }
902
903 /* --------------------------------------------------------------------------
904                               FS Interface (/proc)
905    -------------------------------------------------------------------------- */
906
907 static struct proc_dir_entry *acpi_ec_dir;
908
909 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
910 {
911         union acpi_ec *ec = (union acpi_ec *)seq->private;
912
913         ACPI_FUNCTION_TRACE("acpi_ec_read_info");
914
915         if (!ec)
916                 goto end;
917
918         seq_printf(seq, "gpe bit:                 0x%02x\n",
919                    (u32) ec->common.gpe_bit);
920         seq_printf(seq, "ports:                   0x%02x, 0x%02x\n",
921                    (u32) ec->common.status_addr.address,
922                    (u32) ec->common.data_addr.address);
923         seq_printf(seq, "use global lock:         %s\n",
924                    ec->common.global_lock ? "yes" : "no");
925         acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
926
927       end:
928         return_VALUE(0);
929 }
930
931 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
932 {
933         return single_open(file, acpi_ec_read_info, PDE(inode)->data);
934 }
935
936 static struct file_operations acpi_ec_info_ops = {
937         .open = acpi_ec_info_open_fs,
938         .read = seq_read,
939         .llseek = seq_lseek,
940         .release = single_release,
941         .owner = THIS_MODULE,
942 };
943
944 static int acpi_ec_add_fs(struct acpi_device *device)
945 {
946         struct proc_dir_entry *entry = NULL;
947
948         ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
949
950         if (!acpi_device_dir(device)) {
951                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
952                                                      acpi_ec_dir);
953                 if (!acpi_device_dir(device))
954                         return_VALUE(-ENODEV);
955         }
956
957         entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
958                                   acpi_device_dir(device));
959         if (!entry)
960                 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
961                                   "Unable to create '%s' fs entry\n",
962                                   ACPI_EC_FILE_INFO));
963         else {
964                 entry->proc_fops = &acpi_ec_info_ops;
965                 entry->data = acpi_driver_data(device);
966                 entry->owner = THIS_MODULE;
967         }
968
969         return_VALUE(0);
970 }
971
972 static int acpi_ec_remove_fs(struct acpi_device *device)
973 {
974         ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
975
976         if (acpi_device_dir(device)) {
977                 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
978                 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
979                 acpi_device_dir(device) = NULL;
980         }
981
982         return_VALUE(0);
983 }
984
985 /* --------------------------------------------------------------------------
986                                Driver Interface
987    -------------------------------------------------------------------------- */
988
989 static int acpi_ec_poll_add(struct acpi_device *device)
990 {
991         int result = 0;
992         acpi_status status = AE_OK;
993         union acpi_ec *ec = NULL;
994
995         ACPI_FUNCTION_TRACE("acpi_ec_add");
996
997         if (!device)
998                 return_VALUE(-EINVAL);
999
1000         ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1001         if (!ec)
1002                 return_VALUE(-ENOMEM);
1003         memset(ec, 0, sizeof(union acpi_ec));
1004
1005         ec->common.handle = device->handle;
1006         ec->common.uid = -1;
1007         spin_lock_init(&ec->poll.lock);
1008         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1009         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1010         acpi_driver_data(device) = ec;
1011
1012         /* Use the global lock for all EC transactions? */
1013         acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
1014                               &ec->common.global_lock);
1015
1016         /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
1017            http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
1018         if (ec_ecdt) {
1019                 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1020                                                   ACPI_ADR_SPACE_EC,
1021                                                   &acpi_ec_space_handler);
1022
1023                 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1024                                         &acpi_ec_gpe_handler);
1025
1026                 kfree(ec_ecdt);
1027         }
1028
1029         /* Get GPE bit assignment (EC events). */
1030         /* TODO: Add support for _GPE returning a package */
1031         status =
1032             acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
1033                                   &ec->common.gpe_bit);
1034         if (ACPI_FAILURE(status)) {
1035                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1036                                   "Error obtaining GPE bit assignment\n"));
1037                 result = -ENODEV;
1038                 goto end;
1039         }
1040
1041         result = acpi_ec_add_fs(device);
1042         if (result)
1043                 goto end;
1044
1045         printk(KERN_INFO PREFIX "%s [%s] (gpe %d) polling mode.\n",
1046                acpi_device_name(device), acpi_device_bid(device),
1047                (u32) ec->common.gpe_bit);
1048
1049         if (!first_ec)
1050                 first_ec = device;
1051
1052       end:
1053         if (result)
1054                 kfree(ec);
1055
1056         return_VALUE(result);
1057 }
1058 static int acpi_ec_intr_add(struct acpi_device *device)
1059 {
1060         int result = 0;
1061         acpi_status status = AE_OK;
1062         union acpi_ec *ec = NULL;
1063
1064         ACPI_FUNCTION_TRACE("acpi_ec_add");
1065
1066         if (!device)
1067                 return_VALUE(-EINVAL);
1068
1069         ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1070         if (!ec)
1071                 return_VALUE(-ENOMEM);
1072         memset(ec, 0, sizeof(union acpi_ec));
1073
1074         ec->common.handle = device->handle;
1075         ec->common.uid = -1;
1076         atomic_set(&ec->intr.pending_gpe, 0);
1077         atomic_set(&ec->intr.leaving_burst, 1);
1078         init_MUTEX(&ec->intr.sem);
1079         init_waitqueue_head(&ec->intr.wait);
1080         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1081         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1082         acpi_driver_data(device) = ec;
1083
1084         /* Use the global lock for all EC transactions? */
1085         acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
1086                               &ec->common.global_lock);
1087
1088         /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
1089            http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
1090         if (ec_ecdt) {
1091                 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1092                                                   ACPI_ADR_SPACE_EC,
1093                                                   &acpi_ec_space_handler);
1094
1095                 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1096                                         &acpi_ec_gpe_handler);
1097
1098                 kfree(ec_ecdt);
1099         }
1100
1101         /* Get GPE bit assignment (EC events). */
1102         /* TODO: Add support for _GPE returning a package */
1103         status =
1104             acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
1105                                   &ec->common.gpe_bit);
1106         if (ACPI_FAILURE(status)) {
1107                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1108                                   "Error obtaining GPE bit assignment\n"));
1109                 result = -ENODEV;
1110                 goto end;
1111         }
1112
1113         result = acpi_ec_add_fs(device);
1114         if (result)
1115                 goto end;
1116
1117         printk(KERN_INFO PREFIX "%s [%s] (gpe %d) interrupt mode.\n",
1118                acpi_device_name(device), acpi_device_bid(device),
1119                (u32) ec->common.gpe_bit);
1120
1121         if (!first_ec)
1122                 first_ec = device;
1123
1124       end:
1125         if (result)
1126                 kfree(ec);
1127
1128         return_VALUE(result);
1129 }
1130
1131 static int acpi_ec_remove(struct acpi_device *device, int type)
1132 {
1133         union acpi_ec *ec = NULL;
1134
1135         ACPI_FUNCTION_TRACE("acpi_ec_remove");
1136
1137         if (!device)
1138                 return_VALUE(-EINVAL);
1139
1140         ec = acpi_driver_data(device);
1141
1142         acpi_ec_remove_fs(device);
1143
1144         kfree(ec);
1145
1146         return_VALUE(0);
1147 }
1148
1149 static acpi_status
1150 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
1151 {
1152         union acpi_ec *ec = (union acpi_ec *)context;
1153         struct acpi_generic_address *addr;
1154
1155         if (resource->type != ACPI_RESOURCE_TYPE_IO) {
1156                 return AE_OK;
1157         }
1158
1159         /*
1160          * The first address region returned is the data port, and
1161          * the second address region returned is the status/command
1162          * port.
1163          */
1164         if (ec->common.data_addr.register_bit_width == 0) {
1165                 addr = &ec->common.data_addr;
1166         } else if (ec->common.command_addr.register_bit_width == 0) {
1167                 addr = &ec->common.command_addr;
1168         } else {
1169                 return AE_CTRL_TERMINATE;
1170         }
1171
1172         addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1173         addr->register_bit_width = 8;
1174         addr->register_bit_offset = 0;
1175         addr->address = resource->data.io.minimum;
1176
1177         return AE_OK;
1178 }
1179
1180 static int acpi_ec_start(struct acpi_device *device)
1181 {
1182         acpi_status status = AE_OK;
1183         union acpi_ec *ec = NULL;
1184
1185         ACPI_FUNCTION_TRACE("acpi_ec_start");
1186
1187         if (!device)
1188                 return_VALUE(-EINVAL);
1189
1190         ec = acpi_driver_data(device);
1191
1192         if (!ec)
1193                 return_VALUE(-EINVAL);
1194
1195         /*
1196          * Get I/O port addresses. Convert to GAS format.
1197          */
1198         status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
1199                                      acpi_ec_io_ports, ec);
1200         if (ACPI_FAILURE(status)
1201             || ec->common.command_addr.register_bit_width == 0) {
1202                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1203                                   "Error getting I/O port addresses"));
1204                 return_VALUE(-ENODEV);
1205         }
1206
1207         ec->common.status_addr = ec->common.command_addr;
1208
1209         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
1210                           (u32) ec->common.gpe_bit,
1211                           (u32) ec->common.command_addr.address,
1212                           (u32) ec->common.data_addr.address));
1213
1214         /*
1215          * Install GPE handler
1216          */
1217         status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
1218                                           ACPI_GPE_EDGE_TRIGGERED,
1219                                           &acpi_ec_gpe_handler, ec);
1220         if (ACPI_FAILURE(status)) {
1221                 return_VALUE(-ENODEV);
1222         }
1223         acpi_set_gpe_type(NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1224         acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
1225
1226         status = acpi_install_address_space_handler(ec->common.handle,
1227                                                     ACPI_ADR_SPACE_EC,
1228                                                     &acpi_ec_space_handler,
1229                                                     &acpi_ec_space_setup, ec);
1230         if (ACPI_FAILURE(status)) {
1231                 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1232                                         &acpi_ec_gpe_handler);
1233                 return_VALUE(-ENODEV);
1234         }
1235
1236         return_VALUE(AE_OK);
1237 }
1238
1239 static int acpi_ec_stop(struct acpi_device *device, int type)
1240 {
1241         acpi_status status = AE_OK;
1242         union acpi_ec *ec = NULL;
1243
1244         ACPI_FUNCTION_TRACE("acpi_ec_stop");
1245
1246         if (!device)
1247                 return_VALUE(-EINVAL);
1248
1249         ec = acpi_driver_data(device);
1250
1251         status = acpi_remove_address_space_handler(ec->common.handle,
1252                                                    ACPI_ADR_SPACE_EC,
1253                                                    &acpi_ec_space_handler);
1254         if (ACPI_FAILURE(status))
1255                 return_VALUE(-ENODEV);
1256
1257         status =
1258             acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1259                                     &acpi_ec_gpe_handler);
1260         if (ACPI_FAILURE(status))
1261                 return_VALUE(-ENODEV);
1262
1263         return_VALUE(0);
1264 }
1265
1266 static acpi_status __init
1267 acpi_fake_ecdt_callback(acpi_handle handle,
1268                         u32 Level, void *context, void **retval)
1269 {
1270
1271         if (acpi_ec_poll_mode)
1272                 return acpi_fake_ecdt_poll_callback(handle,
1273                                                        Level, context, retval);
1274         else
1275                 return acpi_fake_ecdt_intr_callback(handle,
1276                                                      Level, context, retval);
1277 }
1278
1279 static acpi_status __init
1280 acpi_fake_ecdt_poll_callback(acpi_handle handle,
1281                                 u32 Level, void *context, void **retval)
1282 {
1283         acpi_status status;
1284
1285         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1286                                      acpi_ec_io_ports, ec_ecdt);
1287         if (ACPI_FAILURE(status))
1288                 return status;
1289         ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1290
1291         ec_ecdt->common.uid = -1;
1292         acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1293
1294         status =
1295             acpi_evaluate_integer(handle, "_GPE", NULL,
1296                                   &ec_ecdt->common.gpe_bit);
1297         if (ACPI_FAILURE(status))
1298                 return status;
1299         spin_lock_init(&ec_ecdt->poll.lock);
1300         ec_ecdt->common.global_lock = TRUE;
1301         ec_ecdt->common.handle = handle;
1302
1303         printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1304                (u32) ec_ecdt->common.gpe_bit,
1305                (u32) ec_ecdt->common.command_addr.address,
1306                (u32) ec_ecdt->common.data_addr.address);
1307
1308         return AE_CTRL_TERMINATE;
1309 }
1310
1311 static acpi_status __init
1312 acpi_fake_ecdt_intr_callback(acpi_handle handle,
1313                               u32 Level, void *context, void **retval)
1314 {
1315         acpi_status status;
1316
1317         init_MUTEX(&ec_ecdt->intr.sem);
1318         init_waitqueue_head(&ec_ecdt->intr.wait);
1319         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1320                                      acpi_ec_io_ports, ec_ecdt);
1321         if (ACPI_FAILURE(status))
1322                 return status;
1323         ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1324
1325         ec_ecdt->common.uid = -1;
1326         acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1327
1328         status =
1329             acpi_evaluate_integer(handle, "_GPE", NULL,
1330                                   &ec_ecdt->common.gpe_bit);
1331         if (ACPI_FAILURE(status))
1332                 return status;
1333         ec_ecdt->common.global_lock = TRUE;
1334         ec_ecdt->common.handle = handle;
1335
1336         printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1337                (u32) ec_ecdt->common.gpe_bit,
1338                (u32) ec_ecdt->common.command_addr.address,
1339                (u32) ec_ecdt->common.data_addr.address);
1340
1341         return AE_CTRL_TERMINATE;
1342 }
1343
1344 /*
1345  * Some BIOS (such as some from Gateway laptops) access EC region very early
1346  * such as in BAT0._INI or EC._INI before an EC device is found and
1347  * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1348  * required, but if EC regison is accessed early, it is required.
1349  * The routine tries to workaround the BIOS bug by pre-scan EC device
1350  * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1351  * op region (since _REG isn't invoked yet). The assumption is true for
1352  * all systems found.
1353  */
1354 static int __init acpi_ec_fake_ecdt(void)
1355 {
1356         acpi_status status;
1357         int ret = 0;
1358
1359         printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1360
1361         ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1362         if (!ec_ecdt) {
1363                 ret = -ENOMEM;
1364                 goto error;
1365         }
1366         memset(ec_ecdt, 0, sizeof(union acpi_ec));
1367
1368         status = acpi_get_devices(ACPI_EC_HID,
1369                                   acpi_fake_ecdt_callback, NULL, NULL);
1370         if (ACPI_FAILURE(status)) {
1371                 kfree(ec_ecdt);
1372                 ec_ecdt = NULL;
1373                 ret = -ENODEV;
1374                 goto error;
1375         }
1376         return 0;
1377       error:
1378         printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1379         return ret;
1380 }
1381
1382 static int __init acpi_ec_get_real_ecdt(void)
1383 {
1384         if (acpi_ec_poll_mode)
1385                 return acpi_ec_poll_get_real_ecdt();
1386         else
1387                 return acpi_ec_intr_get_real_ecdt();
1388 }
1389
1390 static int __init acpi_ec_poll_get_real_ecdt(void)
1391 {
1392         acpi_status status;
1393         struct acpi_table_ecdt *ecdt_ptr;
1394
1395         status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1396                                          (struct acpi_table_header **)
1397                                          &ecdt_ptr);
1398         if (ACPI_FAILURE(status))
1399                 return -ENODEV;
1400
1401         printk(KERN_INFO PREFIX "Found ECDT\n");
1402
1403         /*
1404          * Generate a temporary ec context to use until the namespace is scanned
1405          */
1406         ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1407         if (!ec_ecdt)
1408                 return -ENOMEM;
1409         memset(ec_ecdt, 0, sizeof(union acpi_ec));
1410
1411         ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1412         ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1413         ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1414         ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1415         spin_lock_init(&ec_ecdt->poll.lock);
1416         /* use the GL just to be safe */
1417         ec_ecdt->common.global_lock = TRUE;
1418         ec_ecdt->common.uid = ecdt_ptr->uid;
1419
1420         status =
1421             acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1422         if (ACPI_FAILURE(status)) {
1423                 goto error;
1424         }
1425
1426         return 0;
1427       error:
1428         printk(KERN_ERR PREFIX "Could not use ECDT\n");
1429         kfree(ec_ecdt);
1430         ec_ecdt = NULL;
1431
1432         return -ENODEV;
1433 }
1434
1435 static int __init acpi_ec_intr_get_real_ecdt(void)
1436 {
1437         acpi_status status;
1438         struct acpi_table_ecdt *ecdt_ptr;
1439
1440         status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1441                                          (struct acpi_table_header **)
1442                                          &ecdt_ptr);
1443         if (ACPI_FAILURE(status))
1444                 return -ENODEV;
1445
1446         printk(KERN_INFO PREFIX "Found ECDT\n");
1447
1448         /*
1449          * Generate a temporary ec context to use until the namespace is scanned
1450          */
1451         ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1452         if (!ec_ecdt)
1453                 return -ENOMEM;
1454         memset(ec_ecdt, 0, sizeof(union acpi_ec));
1455
1456         init_MUTEX(&ec_ecdt->intr.sem);
1457         init_waitqueue_head(&ec_ecdt->intr.wait);
1458         ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1459         ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1460         ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1461         ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1462         /* use the GL just to be safe */
1463         ec_ecdt->common.global_lock = TRUE;
1464         ec_ecdt->common.uid = ecdt_ptr->uid;
1465
1466         status =
1467             acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1468         if (ACPI_FAILURE(status)) {
1469                 goto error;
1470         }
1471
1472         return 0;
1473       error:
1474         printk(KERN_ERR PREFIX "Could not use ECDT\n");
1475         kfree(ec_ecdt);
1476         ec_ecdt = NULL;
1477
1478         return -ENODEV;
1479 }
1480
1481 static int __initdata acpi_fake_ecdt_enabled;
1482 int __init acpi_ec_ecdt_probe(void)
1483 {
1484         acpi_status status;
1485         int ret;
1486
1487         ret = acpi_ec_get_real_ecdt();
1488         /* Try to make a fake ECDT */
1489         if (ret && acpi_fake_ecdt_enabled) {
1490                 ret = acpi_ec_fake_ecdt();
1491         }
1492
1493         if (ret)
1494                 return 0;
1495
1496         /*
1497          * Install GPE handler
1498          */
1499         status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1500                                           ACPI_GPE_EDGE_TRIGGERED,
1501                                           &acpi_ec_gpe_handler, ec_ecdt);
1502         if (ACPI_FAILURE(status)) {
1503                 goto error;
1504         }
1505         acpi_set_gpe_type(NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1506         acpi_enable_gpe(NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
1507
1508         status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
1509                                                     ACPI_ADR_SPACE_EC,
1510                                                     &acpi_ec_space_handler,
1511                                                     &acpi_ec_space_setup,
1512                                                     ec_ecdt);
1513         if (ACPI_FAILURE(status)) {
1514                 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1515                                         &acpi_ec_gpe_handler);
1516                 goto error;
1517         }
1518
1519         return 0;
1520
1521       error:
1522         printk(KERN_ERR PREFIX "Could not use ECDT\n");
1523         kfree(ec_ecdt);
1524         ec_ecdt = NULL;
1525
1526         return -ENODEV;
1527 }
1528
1529 static int __init acpi_ec_init(void)
1530 {
1531         int result = 0;
1532
1533         ACPI_FUNCTION_TRACE("acpi_ec_init");
1534
1535         if (acpi_disabled)
1536                 return_VALUE(0);
1537
1538         acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1539         if (!acpi_ec_dir)
1540                 return_VALUE(-ENODEV);
1541
1542         /* Now register the driver for the EC */
1543         result = acpi_bus_register_driver(&acpi_ec_driver);
1544         if (result < 0) {
1545                 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1546                 return_VALUE(-ENODEV);
1547         }
1548
1549         return_VALUE(result);
1550 }
1551
1552 subsys_initcall(acpi_ec_init);
1553
1554 /* EC driver currently not unloadable */
1555 #if 0
1556 static void __exit acpi_ec_exit(void)
1557 {
1558         ACPI_FUNCTION_TRACE("acpi_ec_exit");
1559
1560         acpi_bus_unregister_driver(&acpi_ec_driver);
1561
1562         remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1563
1564         return_VOID;
1565 }
1566 #endif                          /* 0 */
1567
1568 static int __init acpi_fake_ecdt_setup(char *str)
1569 {
1570         acpi_fake_ecdt_enabled = 1;
1571         return 1;
1572 }
1573
1574 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1575 static int __init acpi_ec_set_intr_mode(char *str)
1576 {
1577         int intr;
1578
1579         if (!get_option(&str, &intr))
1580                 return 0;
1581
1582         if (intr) {
1583                 acpi_ec_poll_mode = EC_INTR;
1584                 acpi_ec_driver.ops.add = acpi_ec_intr_add;
1585         } else {
1586                 acpi_ec_poll_mode = EC_POLL;
1587                 acpi_ec_driver.ops.add = acpi_ec_poll_add;
1588         }
1589         printk(KERN_INFO PREFIX "EC %s mode.\n", intr ? "interrupt" : "polling");
1590         return 1;
1591 }
1592
1593 __setup("ec_intr=", acpi_ec_set_intr_mode);