[ACPI] fix 64-bit build warning in processor_idle.c
[linux-2.6/verdex.git] / drivers / acpi / ec.c
blob2dadb7f632693e0edc03c522a914a1dff5deab35
1 /*
2 * acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 38 $)
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>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>
40 #define _COMPONENT ACPI_EC_COMPONENT
41 ACPI_MODULE_NAME ("acpi_ec")
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"
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 */
56 #define ACPI_EC_EVENT_OBF 0x01 /* Output buffer full */
57 #define ACPI_EC_EVENT_IBE 0x02 /* Input buffer empty */
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 */
62 #define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
63 #define ACPI_EC_UDELAY_COUNT 1000 /* Wait 10ms max. during EC ops */
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
71 #define EC_POLLING 0xFF
72 #define EC_BURST 0x00
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);
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,
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;
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;
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;
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);
151 static acpi_status __init
152 acpi_fake_ecdt_burst_callback (
153 acpi_handle handle,
154 u32 Level,
155 void *context,
156 void **retval);
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;
165 /* External interfaces use first EC only, so remember */
166 static struct acpi_device *first_ec;
167 static int acpi_ec_polling_mode;
169 /* --------------------------------------------------------------------------
170 Transaction Management
171 -------------------------------------------------------------------------- */
173 static inline u32 acpi_ec_read_status(union acpi_ec *ec)
175 u32 status = 0;
177 acpi_hw_low_level_read(8, &status, &ec->common.status_addr);
178 return status;
181 static int
182 acpi_ec_wait (
183 union acpi_ec *ec,
184 u8 event)
186 if (acpi_ec_polling_mode)
187 return acpi_ec_polling_wait (ec, event);
188 else
189 return acpi_ec_burst_wait (ec, event);
192 static int
193 acpi_ec_polling_wait (
194 union acpi_ec *ec,
195 u8 event)
197 u32 acpi_ec_status = 0;
198 u32 i = ACPI_EC_UDELAY_COUNT;
200 if (!ec)
201 return -EINVAL;
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;
225 return -ETIME;
227 static int acpi_ec_burst_wait(union acpi_ec *ec, unsigned int event)
229 int result = 0;
231 ACPI_FUNCTION_TRACE("acpi_ec_wait");
233 ec->burst.expect_event = event;
234 smp_mb();
236 result = wait_event_interruptible_timeout(ec->burst.wait,
237 !ec->burst.expect_event,
238 msecs_to_jiffies(ACPI_EC_DELAY));
240 ec->burst.expect_event = 0;
241 smp_mb();
243 if (result < 0){
244 ACPI_DEBUG_PRINT((ACPI_DB_ERROR," result = %d ", result));
245 return_VALUE(result);
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.
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;
259 case ACPI_EC_EVENT_IBE:
260 if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
261 return_VALUE(0);
262 break;
265 return_VALUE(-ETIME);
270 static int
271 acpi_ec_enter_burst_mode (
272 union acpi_ec *ec)
274 u32 tmp = 0;
275 int status = 0;
277 ACPI_FUNCTION_TRACE("acpi_ec_enter_burst_mode");
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);
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);
295 atomic_set(&ec->burst.leaving_burst , 0);
296 return_VALUE(0);
299 static int
300 acpi_ec_leave_burst_mode (
301 union acpi_ec *ec)
303 int status =0;
305 ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
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);
318 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
319 status = acpi_ec_read_status(ec);
322 return_VALUE(0);
325 static int
326 acpi_ec_read (
327 union acpi_ec *ec,
328 u8 address,
329 u32 *data)
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);
336 static int
337 acpi_ec_write (
338 union acpi_ec *ec,
339 u8 address,
340 u8 data)
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);
347 static int
348 acpi_ec_polling_read (
349 union acpi_ec *ec,
350 u8 address,
351 u32 *data)
353 acpi_status status = AE_OK;
354 int result = 0;
355 unsigned long flags = 0;
356 u32 glk = 0;
358 ACPI_FUNCTION_TRACE("acpi_ec_read");
360 if (!ec || !data)
361 return_VALUE(-EINVAL);
363 *data = 0;
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);
371 spin_lock_irqsave(&ec->polling.lock, flags);
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;
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;
383 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
385 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
386 *data, address));
388 end:
389 spin_unlock_irqrestore(&ec->polling.lock, flags);
391 if (ec->common.global_lock)
392 acpi_release_global_lock(glk);
394 return_VALUE(result);
398 static int
399 acpi_ec_polling_write (
400 union acpi_ec *ec,
401 u8 address,
402 u8 data)
404 int result = 0;
405 acpi_status status = AE_OK;
406 unsigned long flags = 0;
407 u32 glk = 0;
409 ACPI_FUNCTION_TRACE("acpi_ec_write");
411 if (!ec)
412 return_VALUE(-EINVAL);
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);
420 spin_lock_irqsave(&ec->polling.lock, flags);
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;
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;
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;
437 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
438 data, address));
440 end:
441 spin_unlock_irqrestore(&ec->polling.lock, flags);
443 if (ec->common.global_lock)
444 acpi_release_global_lock(glk);
446 return_VALUE(result);
449 static int
450 acpi_ec_burst_read (
451 union acpi_ec *ec,
452 u8 address,
453 u32 *data)
455 int status = 0;
456 u32 glk;
458 ACPI_FUNCTION_TRACE("acpi_ec_read");
460 if (!ec || !data)
461 return_VALUE(-EINVAL);
463 retry:
464 *data = 0;
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);
472 WARN_ON(in_interrupt());
473 down(&ec->burst.sem);
475 if(acpi_ec_enter_burst_mode(ec))
476 goto end;
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;
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;
492 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
493 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
495 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
496 *data, address));
498 end:
499 acpi_ec_leave_burst_mode(ec);
500 up(&ec->burst.sem);
502 if (ec->common.global_lock)
503 acpi_release_global_lock(glk);
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);
510 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
511 goto retry;
514 return_VALUE(status);
518 static int
519 acpi_ec_burst_write (
520 union acpi_ec *ec,
521 u8 address,
522 u8 data)
524 int status = 0;
525 u32 glk;
526 u32 tmp;
528 ACPI_FUNCTION_TRACE("acpi_ec_write");
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);
539 WARN_ON(in_interrupt());
540 down(&ec->burst.sem);
542 if(acpi_ec_enter_burst_mode(ec))
543 goto end;
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;
556 /*Now we are in burst mode*/
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;
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;
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;
578 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
579 data, address));
581 end:
582 acpi_ec_leave_burst_mode(ec);
583 up(&ec->burst.sem);
585 if (ec->common.global_lock)
586 acpi_release_global_lock(glk);
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);
593 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
594 goto retry;
597 return_VALUE(status);
601 * Externally callable EC access functions. For now, assume 1 EC only
604 ec_read(u8 addr, u8 *val)
606 union acpi_ec *ec;
607 int err;
608 u32 temp_data;
610 if (!first_ec)
611 return -ENODEV;
613 ec = acpi_driver_data(first_ec);
615 err = acpi_ec_read(ec, addr, &temp_data);
617 if (!err) {
618 *val = temp_data;
619 return 0;
621 else
622 return err;
624 EXPORT_SYMBOL(ec_read);
627 ec_write(u8 addr, u8 val)
629 union acpi_ec *ec;
630 int err;
632 if (!first_ec)
633 return -ENODEV;
635 ec = acpi_driver_data(first_ec);
637 err = acpi_ec_write(ec, addr, val);
639 return err;
641 EXPORT_SYMBOL(ec_write);
643 static int
644 acpi_ec_query (
645 union acpi_ec *ec,
646 u32 *data)
648 if (acpi_ec_polling_mode)
649 return acpi_ec_polling_query(ec, data);
650 else
651 return acpi_ec_burst_query(ec, data);
653 static int
654 acpi_ec_polling_query (
655 union acpi_ec *ec,
656 u32 *data)
658 int result = 0;
659 acpi_status status = AE_OK;
660 unsigned long flags = 0;
661 u32 glk = 0;
663 ACPI_FUNCTION_TRACE("acpi_ec_query");
665 if (!ec || !data)
666 return_VALUE(-EINVAL);
668 *data = 0;
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);
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).
681 spin_lock_irqsave(&ec->polling.lock, flags);
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;
688 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
689 if (!*data)
690 result = -ENODATA;
692 end:
693 spin_unlock_irqrestore(&ec->polling.lock, flags);
695 if (ec->common.global_lock)
696 acpi_release_global_lock(glk);
698 return_VALUE(result);
700 static int
701 acpi_ec_burst_query (
702 union acpi_ec *ec,
703 u32 *data)
705 int status = 0;
706 u32 glk;
708 ACPI_FUNCTION_TRACE("acpi_ec_query");
710 if (!ec || !data)
711 return_VALUE(-EINVAL);
712 *data = 0;
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);
720 down(&ec->burst.sem);
721 if(acpi_ec_enter_burst_mode(ec))
722 goto end;
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).
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;
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;
740 end:
741 acpi_ec_leave_burst_mode(ec);
742 up(&ec->burst.sem);
744 if (ec->common.global_lock)
745 acpi_release_global_lock(glk);
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;
752 return_VALUE(status);
756 /* --------------------------------------------------------------------------
757 Event Management
758 -------------------------------------------------------------------------- */
760 union acpi_ec_query_data {
761 acpi_handle handle;
762 u8 data;
765 static void
766 acpi_ec_gpe_query (
767 void *ec_cxt)
769 if (acpi_ec_polling_mode)
770 acpi_ec_gpe_polling_query(ec_cxt);
771 else
772 acpi_ec_gpe_burst_query(ec_cxt);
775 static void
776 acpi_ec_gpe_polling_query (
777 void *ec_cxt)
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'};
786 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
788 if (!ec_cxt)
789 goto end;
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);
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).
801 if (!(value & ACPI_EC_FLAG_SCI))
802 goto end;
804 if (acpi_ec_query(ec, &value))
805 goto end;
807 object_name[2] = hex[((value >> 4) & 0x0F)];
808 object_name[3] = hex[(value & 0x0F)];
810 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
812 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
814 end:
815 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
817 static void
818 acpi_ec_gpe_burst_query (
819 void *ec_cxt)
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'};
828 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
830 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
831 result = acpi_ec_query(ec, &value);
833 if (result)
834 goto end;
836 object_name[2] = hex[((value >> 4) & 0x0F)];
837 object_name[3] = hex[(value & 0x0F)];
839 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
841 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
842 end:
843 atomic_dec(&ec->burst.pending_gpe);
844 return;
847 static u32
848 acpi_ec_gpe_handler (
849 void *data)
851 if (acpi_ec_polling_mode)
852 return acpi_ec_gpe_polling_handler(data);
853 else
854 return acpi_ec_gpe_burst_handler(data);
856 static u32
857 acpi_ec_gpe_polling_handler (
858 void *data)
860 acpi_status status = AE_OK;
861 union acpi_ec *ec = (union acpi_ec *) data;
863 if (!ec)
864 return ACPI_INTERRUPT_NOT_HANDLED;
866 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
868 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
869 acpi_ec_gpe_query, ec);
871 if (status == AE_OK)
872 return ACPI_INTERRUPT_HANDLED;
873 else
874 return ACPI_INTERRUPT_NOT_HANDLED;
876 static u32
877 acpi_ec_gpe_burst_handler (
878 void *data)
880 acpi_status status = AE_OK;
881 u32 value;
882 union acpi_ec *ec = (union acpi_ec *) data;
884 if (!ec)
885 return ACPI_INTERRUPT_NOT_HANDLED;
887 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
889 value = acpi_ec_read_status(ec);
891 if((value & ACPI_EC_FLAG_IBF) &&
892 !(value & ACPI_EC_FLAG_BURST) &&
893 (atomic_read(&ec->burst.leaving_burst) == 0)) {
895 * the embedded controller disables
896 * burst mode for any reason other
897 * than the burst disable command
898 * to process critical event.
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;
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;
921 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
922 return status == AE_OK ?
923 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
926 /* --------------------------------------------------------------------------
927 Address Space Management
928 -------------------------------------------------------------------------- */
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)
938 * The EC object is in the handler context and is needed
939 * when calling the acpi_ec_space_handler.
941 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
942 handler_context : NULL;
944 return AE_OK;
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)
957 int result = 0;
958 union acpi_ec *ec = NULL;
959 u64 temp = *value;
960 acpi_integer f_v = 0;
961 int i = 0;
963 ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
965 if ((address > 0xFF) || !value || !handler_context)
966 return_VALUE(AE_BAD_PARAMETER);
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);
973 ec = (union acpi_ec *) handler_context;
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;
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;
1001 if (function == ACPI_READ) {
1002 f_v |= temp << 8 * i;
1003 *value = f_v;
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);
1024 /* --------------------------------------------------------------------------
1025 FS Interface (/proc)
1026 -------------------------------------------------------------------------- */
1028 static struct proc_dir_entry *acpi_ec_dir;
1031 static int
1032 acpi_ec_read_info (struct seq_file *seq, void *offset)
1034 union acpi_ec *ec = (union acpi_ec *) seq->private;
1036 ACPI_FUNCTION_TRACE("acpi_ec_read_info");
1038 if (!ec)
1039 goto end;
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);
1049 end:
1050 return_VALUE(0);
1053 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
1055 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
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,
1066 static int
1067 acpi_ec_add_fs (
1068 struct acpi_device *device)
1070 struct proc_dir_entry *entry = NULL;
1072 ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
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);
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;
1093 return_VALUE(0);
1097 static int
1098 acpi_ec_remove_fs (
1099 struct acpi_device *device)
1101 ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
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;
1109 return_VALUE(0);
1113 /* --------------------------------------------------------------------------
1114 Driver Interface
1115 -------------------------------------------------------------------------- */
1118 static int
1119 acpi_ec_polling_add (
1120 struct acpi_device *device)
1122 int result = 0;
1123 acpi_status status = AE_OK;
1124 union acpi_ec *ec = NULL;
1125 unsigned long uid;
1127 ACPI_FUNCTION_TRACE("acpi_ec_add");
1129 if (!device)
1130 return_VALUE(-EINVAL);
1132 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1133 if (!ec)
1134 return_VALUE(-ENOMEM);
1135 memset(ec, 0, sizeof(union acpi_ec));
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;
1144 /* Use the global lock for all EC transactions? */
1145 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL, &ec->common.global_lock);
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);
1154 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit, &acpi_ec_gpe_handler);
1156 kfree(ec_ecdt);
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;
1169 result = acpi_ec_add_fs(device);
1170 if (result)
1171 goto end;
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);
1177 if (!first_ec)
1178 first_ec = device;
1180 end:
1181 if (result)
1182 kfree(ec);
1184 return_VALUE(result);
1186 static int
1187 acpi_ec_burst_add (
1188 struct acpi_device *device)
1190 int result = 0;
1191 acpi_status status = AE_OK;
1192 union acpi_ec *ec = NULL;
1193 unsigned long uid;
1195 ACPI_FUNCTION_TRACE("acpi_ec_add");
1197 if (!device)
1198 return_VALUE(-EINVAL);
1200 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1201 if (!ec)
1202 return_VALUE(-ENOMEM);
1203 memset(ec, 0, sizeof(union acpi_ec));
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;
1215 /* Use the global lock for all EC transactions? */
1216 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL, &ec->common.global_lock);
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);
1225 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit, &acpi_ec_gpe_handler);
1227 kfree(ec_ecdt);
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;
1240 result = acpi_ec_add_fs(device);
1241 if (result)
1242 goto end;
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);
1248 if (!first_ec)
1249 first_ec = device;
1251 end:
1252 if (result)
1253 kfree(ec);
1255 return_VALUE(result);
1259 static int
1260 acpi_ec_remove (
1261 struct acpi_device *device,
1262 int type)
1264 union acpi_ec *ec = NULL;
1266 ACPI_FUNCTION_TRACE("acpi_ec_remove");
1268 if (!device)
1269 return_VALUE(-EINVAL);
1271 ec = acpi_driver_data(device);
1273 acpi_ec_remove_fs(device);
1275 kfree(ec);
1277 return_VALUE(0);
1281 static acpi_status
1282 acpi_ec_io_ports (
1283 struct acpi_resource *resource,
1284 void *context)
1286 union acpi_ec *ec = (union acpi_ec *) context;
1287 struct acpi_generic_address *addr;
1289 if (resource->id != ACPI_RSTYPE_IO) {
1290 return AE_OK;
1294 * The first address region returned is the data port, and
1295 * the second address region returned is the status/command
1296 * port.
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;
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;
1311 return AE_OK;
1315 static int
1316 acpi_ec_start (
1317 struct acpi_device *device)
1319 acpi_status status = AE_OK;
1320 union acpi_ec *ec = NULL;
1322 ACPI_FUNCTION_TRACE("acpi_ec_start");
1324 if (!device)
1325 return_VALUE(-EINVAL);
1327 ec = acpi_driver_data(device);
1329 if (!ec)
1330 return_VALUE(-EINVAL);
1333 * Get I/O port addresses. Convert to GAS format.
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);
1342 ec->common.status_addr = ec->common.command_addr;
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));
1350 * Install GPE handler
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);
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);
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);
1368 return_VALUE(AE_OK);
1372 static int
1373 acpi_ec_stop (
1374 struct acpi_device *device,
1375 int type)
1377 acpi_status status = AE_OK;
1378 union acpi_ec *ec = NULL;
1380 ACPI_FUNCTION_TRACE("acpi_ec_stop");
1382 if (!device)
1383 return_VALUE(-EINVAL);
1385 ec = acpi_driver_data(device);
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);
1392 status = acpi_remove_gpe_handler(NULL, ec->common.gpe_bit, &acpi_ec_gpe_handler);
1393 if (ACPI_FAILURE(status))
1394 return_VALUE(-ENODEV);
1396 return_VALUE(0);
1399 static acpi_status __init
1400 acpi_fake_ecdt_callback (
1401 acpi_handle handle,
1402 u32 Level,
1403 void *context,
1404 void **retval)
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);
1415 static acpi_status __init
1416 acpi_fake_ecdt_polling_callback (
1417 acpi_handle handle,
1418 u32 Level,
1419 void *context,
1420 void **retval)
1422 acpi_status status;
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;
1430 ec_ecdt->common.uid = -1;
1431 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
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;
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);
1444 return AE_CTRL_TERMINATE;
1447 static acpi_status __init
1448 acpi_fake_ecdt_burst_callback (
1449 acpi_handle handle,
1450 u32 Level,
1451 void *context,
1452 void **retval)
1454 acpi_status status;
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;
1464 ec_ecdt->common.uid = -1;
1465 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
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;
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);
1477 return AE_CTRL_TERMINATE;
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.
1490 static int __init
1491 acpi_ec_fake_ecdt(void)
1493 acpi_status status;
1494 int ret = 0;
1496 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1498 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1499 if (!ec_ecdt) {
1500 ret = -ENOMEM;
1501 goto error;
1503 memset(ec_ecdt, 0, sizeof(union acpi_ec));
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;
1515 return 0;
1516 error:
1517 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1518 return ret;
1521 static int __init
1522 acpi_ec_get_real_ecdt(void)
1524 if (acpi_ec_polling_mode)
1525 return acpi_ec_polling_get_real_ecdt();
1526 else
1527 return acpi_ec_burst_get_real_ecdt();
1530 static int __init
1531 acpi_ec_polling_get_real_ecdt(void)
1533 acpi_status status;
1534 struct acpi_table_ecdt *ecdt_ptr;
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;
1541 printk(KERN_INFO PREFIX "Found ECDT\n");
1544 * Generate a temporary ec context to use until the namespace is scanned
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));
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;
1560 status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1561 if (ACPI_FAILURE(status)) {
1562 goto error;
1565 return 0;
1566 error:
1567 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1568 kfree(ec_ecdt);
1569 ec_ecdt = NULL;
1571 return -ENODEV;
1575 static int __init
1576 acpi_ec_burst_get_real_ecdt(void)
1578 acpi_status status;
1579 struct acpi_table_ecdt *ecdt_ptr;
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;
1586 printk(KERN_INFO PREFIX "Found ECDT\n");
1589 * Generate a temporary ec context to use until the namespace is scanned
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));
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;
1606 status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1607 if (ACPI_FAILURE(status)) {
1608 goto error;
1611 return 0;
1612 error:
1613 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1614 kfree(ec_ecdt);
1615 ec_ecdt = NULL;
1617 return -ENODEV;
1620 static int __initdata acpi_fake_ecdt_enabled;
1621 int __init
1622 acpi_ec_ecdt_probe (void)
1624 acpi_status status;
1625 int ret;
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();
1633 if (ret)
1634 return 0;
1637 * Install GPE handler
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;
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);
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;
1657 return 0;
1659 error:
1660 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1661 kfree(ec_ecdt);
1662 ec_ecdt = NULL;
1664 return -ENODEV;
1668 static int __init acpi_ec_init (void)
1670 int result = 0;
1672 ACPI_FUNCTION_TRACE("acpi_ec_init");
1674 if (acpi_disabled)
1675 return_VALUE(0);
1677 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1678 if (!acpi_ec_dir)
1679 return_VALUE(-ENODEV);
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);
1688 return_VALUE(result);
1691 subsys_initcall(acpi_ec_init);
1693 /* EC driver currently not unloadable */
1694 #if 0
1695 static void __exit
1696 acpi_ec_exit (void)
1698 ACPI_FUNCTION_TRACE("acpi_ec_exit");
1700 acpi_bus_unregister_driver(&acpi_ec_driver);
1702 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1704 return_VOID;
1706 #endif /* 0 */
1708 static int __init acpi_fake_ecdt_setup(char *str)
1710 acpi_fake_ecdt_enabled = 1;
1711 return 0;
1713 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1714 static int __init acpi_ec_set_polling_mode(char *str)
1716 acpi_ec_polling_mode = EC_POLLING;
1717 acpi_ec_driver.ops.add = acpi_ec_polling_add;
1718 return 0;
1720 __setup("ec_polling", acpi_ec_set_polling_mode);