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