patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / acpi / ec.c
1 /*
2  *  acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 38 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/delay.h>
31 #include <linux/proc_fs.h>
32 #include <asm/io.h>
33 #include <acpi/acpi_bus.h>
34 #include <acpi/acpi_drivers.h>
35 #include <acpi/actypes.h>
36
37 #define _COMPONENT              ACPI_EC_COMPONENT
38 ACPI_MODULE_NAME                ("acpi_ec")
39
40 #define ACPI_EC_COMPONENT               0x00100000
41 #define ACPI_EC_CLASS                   "embedded_controller"
42 #define ACPI_EC_HID                     "PNP0C09"
43 #define ACPI_EC_DRIVER_NAME             "ACPI Embedded Controller Driver"
44 #define ACPI_EC_DEVICE_NAME             "Embedded Controller"
45 #define ACPI_EC_FILE_INFO               "info"
46
47
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_SCI        0x20    /* EC-SCI occurred */
51
52 #define ACPI_EC_EVENT_OBF       0x01    /* Output buffer full */
53 #define ACPI_EC_EVENT_IBE       0x02    /* Input buffer empty */
54
55 #define ACPI_EC_UDELAY          100     /* Poll @ 100us increments */
56 #define ACPI_EC_UDELAY_COUNT    1000    /* Wait 10ms max. during EC ops */
57 #define ACPI_EC_UDELAY_GLK      1000    /* Wait 1ms max. to get global lock */
58
59 #define ACPI_EC_COMMAND_READ    0x80
60 #define ACPI_EC_COMMAND_WRITE   0x81
61 #define ACPI_EC_COMMAND_QUERY   0x84
62
63 static int acpi_ec_add (struct acpi_device *device);
64 static int acpi_ec_remove (struct acpi_device *device, int type);
65 static int acpi_ec_start (struct acpi_device *device);
66 static int acpi_ec_stop (struct acpi_device *device, int type);
67
68 static struct acpi_driver acpi_ec_driver = {
69         .name =         ACPI_EC_DRIVER_NAME,
70         .class =        ACPI_EC_CLASS,
71         .ids =          ACPI_EC_HID,
72         .ops =          {
73                                 .add =          acpi_ec_add,
74                                 .remove =       acpi_ec_remove,
75                                 .start =        acpi_ec_start,
76                                 .stop =         acpi_ec_stop,
77                         },
78 };
79
80 struct acpi_ec {
81         acpi_handle                     handle;
82         unsigned long                   uid;
83         unsigned long                   gpe_bit;
84         struct acpi_generic_address     status_addr;
85         struct acpi_generic_address     command_addr;
86         struct acpi_generic_address     data_addr;
87         unsigned long                   global_lock;
88         spinlock_t                      lock;
89 };
90
91 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
92 static struct acpi_ec   *ec_ecdt;
93
94 /* External interfaces use first EC only, so remember */
95 static struct acpi_device *first_ec;
96
97 /* --------------------------------------------------------------------------
98                              Transaction Management
99    -------------------------------------------------------------------------- */
100
101 static int
102 acpi_ec_wait (
103         struct acpi_ec          *ec,
104         u8                      event)
105 {
106         u32                     acpi_ec_status = 0;
107         u32                     i = ACPI_EC_UDELAY_COUNT;
108
109         if (!ec)
110                 return -EINVAL;
111
112         /* Poll the EC status register waiting for the event to occur. */
113         switch (event) {
114         case ACPI_EC_EVENT_OBF:
115                 do {
116                         acpi_hw_low_level_read(8, &acpi_ec_status, &ec->status_addr);
117                         if (acpi_ec_status & ACPI_EC_FLAG_OBF)
118                                 return 0;
119                         udelay(ACPI_EC_UDELAY);
120                 } while (--i>0);
121                 break;
122         case ACPI_EC_EVENT_IBE:
123                 do {
124                         acpi_hw_low_level_read(8, &acpi_ec_status, &ec->status_addr);
125                         if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
126                                 return 0;
127                         udelay(ACPI_EC_UDELAY);
128                 } while (--i>0);
129                 break;
130         default:
131                 return -EINVAL;
132         }
133
134         return -ETIME;
135 }
136
137
138 static int
139 acpi_ec_read (
140         struct acpi_ec          *ec,
141         u8                      address,
142         u32                     *data)
143 {
144         acpi_status             status = AE_OK;
145         int                     result = 0;
146         unsigned long           flags = 0;
147         u32                     glk = 0;
148
149         ACPI_FUNCTION_TRACE("acpi_ec_read");
150
151         if (!ec || !data)
152                 return_VALUE(-EINVAL);
153
154         *data = 0;
155
156         if (ec->global_lock) {
157                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
158                 if (ACPI_FAILURE(status))
159                         return_VALUE(-ENODEV);
160         }
161         
162         spin_lock_irqsave(&ec->lock, flags);
163
164         acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->command_addr);
165         result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
166         if (result)
167                 goto end;
168
169         acpi_hw_low_level_write(8, address, &ec->data_addr);
170         result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
171         if (result)
172                 goto end;
173
174
175         acpi_hw_low_level_read(8, data, &ec->data_addr);
176
177         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
178                 *data, address));
179
180 end:
181         spin_unlock_irqrestore(&ec->lock, flags);
182
183         if (ec->global_lock)
184                 acpi_release_global_lock(glk);
185
186         return_VALUE(result);
187 }
188
189
190 static int
191 acpi_ec_write (
192         struct acpi_ec          *ec,
193         u8                      address,
194         u8                      data)
195 {
196         int                     result = 0;
197         acpi_status             status = AE_OK;
198         unsigned long           flags = 0;
199         u32                     glk = 0;
200
201         ACPI_FUNCTION_TRACE("acpi_ec_write");
202
203         if (!ec)
204                 return_VALUE(-EINVAL);
205
206         if (ec->global_lock) {
207                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
208                 if (ACPI_FAILURE(status))
209                         return_VALUE(-ENODEV);
210         }
211
212         spin_lock_irqsave(&ec->lock, flags);
213
214         acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->command_addr);
215         result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
216         if (result)
217                 goto end;
218
219         acpi_hw_low_level_write(8, address, &ec->data_addr);
220         result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
221         if (result)
222                 goto end;
223
224         acpi_hw_low_level_write(8, data, &ec->data_addr);
225         result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
226         if (result)
227                 goto end;
228
229         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
230                 data, address));
231
232 end:
233         spin_unlock_irqrestore(&ec->lock, flags);
234
235         if (ec->global_lock)
236                 acpi_release_global_lock(glk);
237
238         return_VALUE(result);
239 }
240
241 /*
242  * Externally callable EC access functions. For now, assume 1 EC only
243  */
244 int
245 ec_read(u8 addr, u8 *val)
246 {
247         struct acpi_ec *ec;
248         int err;
249         u32 temp_data;
250
251         if (!first_ec)
252                 return -ENODEV;
253
254         ec = acpi_driver_data(first_ec);
255
256         err = acpi_ec_read(ec, addr, &temp_data);
257
258         if (!err) {
259                 *val = temp_data;
260                 return 0;
261         }
262         else
263                 return err;
264 }
265
266 int
267 ec_write(u8 addr, u8 val)
268 {
269         struct acpi_ec *ec;
270         int err;
271
272         if (!first_ec)
273                 return -ENODEV;
274
275         ec = acpi_driver_data(first_ec);
276
277         err = acpi_ec_write(ec, addr, val);
278
279         return err;
280 }
281
282
283 static int
284 acpi_ec_query (
285         struct acpi_ec          *ec,
286         u32                     *data)
287 {
288         int                     result = 0;
289         acpi_status             status = AE_OK;
290         unsigned long           flags = 0;
291         u32                     glk = 0;
292
293         ACPI_FUNCTION_TRACE("acpi_ec_query");
294
295         if (!ec || !data)
296                 return_VALUE(-EINVAL);
297
298         *data = 0;
299
300         if (ec->global_lock) {
301                 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
302                 if (ACPI_FAILURE(status))
303                         return_VALUE(-ENODEV);
304         }
305
306         /*
307          * Query the EC to find out which _Qxx method we need to evaluate.
308          * Note that successful completion of the query causes the ACPI_EC_SCI
309          * bit to be cleared (and thus clearing the interrupt source).
310          */
311         spin_lock_irqsave(&ec->lock, flags);
312
313         acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->command_addr);
314         result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
315         if (result)
316                 goto end;
317         
318         acpi_hw_low_level_read(8, data, &ec->data_addr);
319         if (!*data)
320                 result = -ENODATA;
321
322 end:
323         spin_unlock_irqrestore(&ec->lock, flags);
324
325         if (ec->global_lock)
326                 acpi_release_global_lock(glk);
327
328         return_VALUE(result);
329 }
330
331
332 /* --------------------------------------------------------------------------
333                                 Event Management
334    -------------------------------------------------------------------------- */
335
336 struct acpi_ec_query_data {
337         acpi_handle             handle;
338         u8                      data;
339 };
340
341 static void
342 acpi_ec_gpe_query (
343         void                    *ec_cxt)
344 {
345         struct acpi_ec          *ec = (struct acpi_ec *) ec_cxt;
346         u32                     value = 0;
347         unsigned long           flags = 0;
348         static char             object_name[5] = {'_','Q','0','0','\0'};
349         const char              hex[] = {'0','1','2','3','4','5','6','7',
350                                          '8','9','A','B','C','D','E','F'};
351
352         ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
353
354         if (!ec_cxt)
355                 goto end;       
356
357         spin_lock_irqsave(&ec->lock, flags);
358         acpi_hw_low_level_read(8, &value, &ec->command_addr);
359         spin_unlock_irqrestore(&ec->lock, flags);
360
361         /* TBD: Implement asynch events!
362          * NOTE: All we care about are EC-SCI's.  Other EC events are
363          * handled via polling (yuck!).  This is because some systems
364          * treat EC-SCIs as level (versus EDGE!) triggered, preventing
365          *  a purely interrupt-driven approach (grumble, grumble).
366          */
367         if (!(value & ACPI_EC_FLAG_SCI))
368                 goto end;
369
370         if (acpi_ec_query(ec, &value))
371                 goto end;
372         
373         object_name[2] = hex[((value >> 4) & 0x0F)];
374         object_name[3] = hex[(value & 0x0F)];
375
376         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
377
378         acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
379
380 end:
381         acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
382 }
383
384 static void
385 acpi_ec_gpe_handler (
386         void                    *data)
387 {
388         acpi_status             status = AE_OK;
389         struct acpi_ec          *ec = (struct acpi_ec *) data;
390
391         if (!ec)
392                 return;
393
394         acpi_disable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
395
396         status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
397                 acpi_ec_gpe_query, ec);
398 }
399
400 /* --------------------------------------------------------------------------
401                              Address Space Management
402    -------------------------------------------------------------------------- */
403
404 static acpi_status
405 acpi_ec_space_setup (
406         acpi_handle             region_handle,
407         u32                     function,
408         void                    *handler_context,
409         void                    **return_context)
410 {
411         /*
412          * The EC object is in the handler context and is needed
413          * when calling the acpi_ec_space_handler.
414          */
415         if(function == ACPI_REGION_DEACTIVATE) 
416                 *return_context = NULL;
417         else 
418                 *return_context = handler_context;
419
420         return AE_OK;
421 }
422
423
424 static acpi_status
425 acpi_ec_space_handler (
426         u32                     function,
427         acpi_physical_address   address,
428         u32                     bit_width,
429         acpi_integer            *value,
430         void                    *handler_context,
431         void                    *region_context)
432 {
433         int                     result = 0;
434         struct acpi_ec          *ec = NULL;
435         u32                     temp = 0;
436
437         ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
438
439         if ((address > 0xFF) || (bit_width != 8) || !value || !handler_context)
440                 return_VALUE(AE_BAD_PARAMETER);
441
442         ec = (struct acpi_ec *) handler_context;
443
444         switch (function) {
445         case ACPI_READ:
446                 result = acpi_ec_read(ec, (u8) address, &temp);
447                 *value = (acpi_integer) temp;
448                 break;
449         case ACPI_WRITE:
450                 result = acpi_ec_write(ec, (u8) address, (u8) *value);
451                 break;
452         default:
453                 result = -EINVAL;
454                 break;
455         }
456
457         switch (result) {
458         case -EINVAL:
459                 return_VALUE(AE_BAD_PARAMETER);
460                 break;
461         case -ENODEV:
462                 return_VALUE(AE_NOT_FOUND);
463                 break;
464         case -ETIME:
465                 return_VALUE(AE_TIME);
466                 break;
467         default:
468                 return_VALUE(AE_OK);
469         }
470
471 }
472
473
474 /* --------------------------------------------------------------------------
475                               FS Interface (/proc)
476    -------------------------------------------------------------------------- */
477
478 struct proc_dir_entry           *acpi_ec_dir;
479
480
481 static int
482 acpi_ec_read_info (
483         char                    *page,
484         char                    **start,
485         off_t                   off,
486         int                     count,
487         int                     *eof,
488         void                    *data)
489 {
490         struct acpi_ec          *ec = (struct acpi_ec *) data;
491         char                    *p = page;
492         int                     len = 0;
493
494         ACPI_FUNCTION_TRACE("acpi_ec_read_info");
495
496         if (!ec || (off != 0))
497                 goto end;
498
499         p += sprintf(p, "gpe bit:                 0x%02x\n",
500                 (u32) ec->gpe_bit);
501         p += sprintf(p, "ports:                   0x%02x, 0x%02x\n",
502                 (u32) ec->status_addr.address, (u32) ec->data_addr.address);
503         p += sprintf(p, "use global lock:         %s\n",
504                 ec->global_lock?"yes":"no");
505
506 end:
507         len = (p - page);
508         if (len <= off+count) *eof = 1;
509         *start = page + off;
510         len -= off;
511         if (len>count) len = count;
512         if (len<0) len = 0;
513
514         return_VALUE(len);
515 }
516
517
518 static int
519 acpi_ec_add_fs (
520         struct acpi_device      *device)
521 {
522         struct proc_dir_entry   *entry = NULL;
523
524         ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
525
526         if (!acpi_device_dir(device)) {
527                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
528                         acpi_ec_dir);
529                 if (!acpi_device_dir(device))
530                         return_VALUE(-ENODEV);
531         }
532
533         entry = create_proc_read_entry(ACPI_EC_FILE_INFO, S_IRUGO,
534                 acpi_device_dir(device), acpi_ec_read_info,
535                 acpi_driver_data(device));
536         if (!entry)
537                 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
538                         "Unable to create '%s' fs entry\n",
539                         ACPI_EC_FILE_INFO));
540
541         return_VALUE(0);
542 }
543
544
545 static int
546 acpi_ec_remove_fs (
547         struct acpi_device      *device)
548 {
549         ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
550
551         if (acpi_device_dir(device)) {
552                 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
553                 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
554                 acpi_device_dir(device) = NULL;
555         }
556
557         return_VALUE(0);
558 }
559
560
561 /* --------------------------------------------------------------------------
562                                Driver Interface
563    -------------------------------------------------------------------------- */
564
565 static int
566 acpi_ec_add (
567         struct acpi_device      *device)
568 {
569         int                     result = 0;
570         acpi_status             status = AE_OK;
571         struct acpi_ec          *ec = NULL;
572         unsigned long           uid;
573
574         ACPI_FUNCTION_TRACE("acpi_ec_add");
575
576         if (!device)
577                 return_VALUE(-EINVAL);
578
579         ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
580         if (!ec)
581                 return_VALUE(-ENOMEM);
582         memset(ec, 0, sizeof(struct acpi_ec));
583
584         ec->handle = device->handle;
585         ec->uid = -1;
586         ec->lock = SPIN_LOCK_UNLOCKED;
587         strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
588         strcpy(acpi_device_class(device), ACPI_EC_CLASS);
589         acpi_driver_data(device) = ec;
590
591         /* Use the global lock for all EC transactions? */
592         acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
593
594         /* If our UID matches the UID for the ECDT-enumerated EC,
595            we now have the *real* EC info, so kill the makeshift one.*/
596         acpi_evaluate_integer(ec->handle, "_UID", NULL, &uid);
597         if (ec_ecdt && ec_ecdt->uid == uid) {
598                 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
599                         ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
600         
601                 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit, &acpi_ec_gpe_handler);
602
603                 kfree(ec_ecdt);
604         }
605
606         /* Get GPE bit assignment (EC events). */
607         /* TODO: Add support for _GPE returning a package */
608         status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe_bit);
609         if (ACPI_FAILURE(status)) {
610                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
611                         "Error obtaining GPE bit assignment\n"));
612                 result = -ENODEV;
613                 goto end;
614         }
615
616         result = acpi_ec_add_fs(device);
617         if (result)
618                 goto end;
619
620         printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
621                 acpi_device_name(device), acpi_device_bid(device),
622                 (u32) ec->gpe_bit);
623
624         if (!first_ec)
625                 first_ec = device;
626
627 end:
628         if (result)
629                 kfree(ec);
630
631         return_VALUE(result);
632 }
633
634
635 static int
636 acpi_ec_remove (
637         struct acpi_device      *device,
638         int                     type)
639 {
640         struct acpi_ec          *ec = NULL;
641
642         ACPI_FUNCTION_TRACE("acpi_ec_remove");
643
644         if (!device)
645                 return_VALUE(-EINVAL);
646
647         ec = acpi_driver_data(device);
648
649         acpi_ec_remove_fs(device);
650
651         kfree(ec);
652
653         return_VALUE(0);
654 }
655
656
657 static acpi_status
658 acpi_ec_io_ports (
659         struct acpi_resource    *resource,
660         void                    *context)
661 {
662         struct acpi_ec          *ec = (struct acpi_ec *) context;
663         struct acpi_generic_address *addr;
664
665         if (resource->id != ACPI_RSTYPE_IO) {
666                 return AE_OK;
667         }
668
669         /*
670          * The first address region returned is the data port, and
671          * the second address region returned is the status/command
672          * port.
673          */
674         if (ec->data_addr.register_bit_width == 0) {
675                 addr = &ec->data_addr;
676         } else if (ec->command_addr.register_bit_width == 0) {
677                 addr = &ec->command_addr;
678         } else {
679                 return AE_CTRL_TERMINATE;
680         }
681
682         addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
683         addr->register_bit_width = 8;
684         addr->register_bit_offset = 0;
685         addr->address = resource->data.io.min_base_address;
686
687         return AE_OK;
688 }
689
690
691 static int
692 acpi_ec_start (
693         struct acpi_device      *device)
694 {
695         acpi_status             status = AE_OK;
696         struct acpi_ec          *ec = NULL;
697
698         ACPI_FUNCTION_TRACE("acpi_ec_start");
699
700         if (!device)
701                 return_VALUE(-EINVAL);
702
703         ec = acpi_driver_data(device);
704
705         if (!ec)
706                 return_VALUE(-EINVAL);
707
708         /*
709          * Get I/O port addresses. Convert to GAS format.
710          */
711         status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
712                 acpi_ec_io_ports, ec);
713         if (ACPI_FAILURE(status) || ec->command_addr.register_bit_width == 0) {
714                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error getting I/O port addresses"));
715                 return_VALUE(-ENODEV);
716         }
717
718         ec->status_addr = ec->command_addr;
719
720         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
721                 (u32) ec->gpe_bit, (u32) ec->command_addr.address,
722                 (u32) ec->data_addr.address));
723
724         /*
725          * Install GPE handler
726          */
727         status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
728                 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler, ec);
729         if (ACPI_FAILURE(status)) {
730                 return_VALUE(-ENODEV);
731         }
732
733         status = acpi_install_address_space_handler (ec->handle,
734                         ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
735                         &acpi_ec_space_setup, ec);
736         if (ACPI_FAILURE(status)) {
737                 acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
738                 return_VALUE(-ENODEV);
739         }
740
741         return_VALUE(AE_OK);
742 }
743
744
745 static int
746 acpi_ec_stop (
747         struct acpi_device      *device,
748         int                     type)
749 {
750         acpi_status             status = AE_OK;
751         struct acpi_ec          *ec = NULL;
752
753         ACPI_FUNCTION_TRACE("acpi_ec_stop");
754
755         if (!device)
756                 return_VALUE(-EINVAL);
757
758         ec = acpi_driver_data(device);
759
760         status = acpi_remove_address_space_handler(ec->handle,
761                 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
762         if (ACPI_FAILURE(status))
763                 return_VALUE(-ENODEV);
764
765         status = acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
766         if (ACPI_FAILURE(status))
767                 return_VALUE(-ENODEV);
768
769         return_VALUE(0);
770 }
771
772
773 int __init
774 acpi_ec_ecdt_probe (void)
775 {
776         acpi_status             status;
777         struct acpi_table_ecdt  *ecdt_ptr;
778
779         status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING, 
780                 (struct acpi_table_header **) &ecdt_ptr);
781         if (ACPI_FAILURE(status))
782                 return 0;
783
784         printk(KERN_INFO PREFIX "Found ECDT\n");
785
786          /*
787          * Generate a temporary ec context to use until the namespace is scanned
788          */
789         ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
790         if (!ec_ecdt)
791                 return -ENOMEM;
792         memset(ec_ecdt, 0, sizeof(struct acpi_ec));
793
794         ec_ecdt->command_addr = ecdt_ptr->ec_control;
795         ec_ecdt->status_addr = ecdt_ptr->ec_control;
796         ec_ecdt->data_addr = ecdt_ptr->ec_data;
797         ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
798         ec_ecdt->lock = SPIN_LOCK_UNLOCKED;
799         /* use the GL just to be safe */
800         ec_ecdt->global_lock = TRUE;
801         ec_ecdt->uid = ecdt_ptr->uid;
802
803         status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
804         if (ACPI_FAILURE(status)) {
805                 goto error;
806         }
807
808         /*
809          * Install GPE handler
810          */
811         status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
812                 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler,
813                 ec_ecdt);
814         if (ACPI_FAILURE(status)) {
815                 goto error;
816         }
817
818         status = acpi_install_address_space_handler (ACPI_ROOT_OBJECT,
819                         ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
820                         &acpi_ec_space_setup, ec_ecdt);
821         if (ACPI_FAILURE(status)) {
822                 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
823                         &acpi_ec_gpe_handler);
824                 goto error;
825         }
826
827         return 0;
828
829 error:
830         printk(KERN_ERR PREFIX "Could not use ECDT\n");
831         kfree(ec_ecdt);
832         ec_ecdt = NULL;
833
834         return -ENODEV;
835 }
836
837
838 static int __init acpi_ec_init (void)
839 {
840         int                     result = 0;
841
842         ACPI_FUNCTION_TRACE("acpi_ec_init");
843
844         if (acpi_disabled)
845                 return_VALUE(0);
846
847         acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
848         if (!acpi_ec_dir)
849                 return_VALUE(-ENODEV);
850
851         /* Now register the driver for the EC */
852         result = acpi_bus_register_driver(&acpi_ec_driver);
853         if (result < 0) {
854                 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
855                 return_VALUE(-ENODEV);
856         }
857
858         return_VALUE(result);
859 }
860
861 subsys_initcall(acpi_ec_init);
862
863 /* EC driver currently not unloadable */
864 #if 0
865 static void __exit
866 acpi_ec_exit (void)
867 {
868         ACPI_FUNCTION_TRACE("acpi_ec_exit");
869
870         acpi_bus_unregister_driver(&acpi_ec_driver);
871
872         remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
873
874         return_VOID;
875 }
876 #endif /* 0 */
877