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