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