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>
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")
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
65 static int acpi_ec_remove(struct acpi_device *device, int type);
66 static int acpi_ec_start(struct acpi_device *device);
67 static int acpi_ec_stop(struct acpi_device *device, int type);
68 static int acpi_ec_intr_add(struct acpi_device *device);
69 static int acpi_ec_poll_add(struct acpi_device *device);
71 static struct acpi_driver acpi_ec_driver = {
72 .name = ACPI_EC_DRIVER_NAME,
73 .class = ACPI_EC_CLASS,
76 .add = acpi_ec_intr_add,
77 .remove = acpi_ec_remove,
78 .start = acpi_ec_start,
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;
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;
114 unsigned long gpe_bit;
115 struct acpi_generic_address status_addr;
116 struct acpi_generic_address command_addr;
117 struct acpi_generic_address data_addr;
118 unsigned long global_lock;
119 struct semaphore sem;
123 static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event);
124 static int acpi_ec_intr_wait(union acpi_ec *ec, unsigned int event);
125 static int acpi_ec_poll_read(union acpi_ec *ec, u8 address, u32 * data);
126 static int acpi_ec_intr_read(union acpi_ec *ec, u8 address, u32 * data);
127 static int acpi_ec_poll_write(union acpi_ec *ec, u8 address, u8 data);
128 static int acpi_ec_intr_write(union acpi_ec *ec, u8 address, u8 data);
129 static int acpi_ec_poll_query(union acpi_ec *ec, u32 * data);
130 static int acpi_ec_intr_query(union acpi_ec *ec, u32 * data);
131 static void acpi_ec_gpe_poll_query(void *ec_cxt);
132 static void acpi_ec_gpe_intr_query(void *ec_cxt);
133 static u32 acpi_ec_gpe_poll_handler(void *data);
134 static u32 acpi_ec_gpe_intr_handler(void *data);
135 static acpi_status __init
136 acpi_fake_ecdt_poll_callback(acpi_handle handle,
137 u32 Level, void *context, void **retval);
139 static acpi_status __init
140 acpi_fake_ecdt_intr_callback(acpi_handle handle,
141 u32 Level, void *context, void **retval);
143 static int __init acpi_ec_poll_get_real_ecdt(void);
144 static int __init acpi_ec_intr_get_real_ecdt(void);
145 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
146 static union acpi_ec *ec_ecdt;
148 /* External interfaces use first EC only, so remember */
149 static struct acpi_device *first_ec;
150 static int acpi_ec_poll_mode = EC_INTR;
152 /* --------------------------------------------------------------------------
153 Transaction Management
154 -------------------------------------------------------------------------- */
156 static u32 acpi_ec_read_status(union acpi_ec *ec)
160 acpi_hw_low_level_read(8, &status, &ec->common.status_addr);
164 static int acpi_ec_wait(union acpi_ec *ec, u8 event)
166 if (acpi_ec_poll_mode)
167 return acpi_ec_poll_wait(ec, event);
169 return acpi_ec_intr_wait(ec, event);
172 static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event)
174 u32 acpi_ec_status = 0;
175 u32 i = ACPI_EC_UDELAY_COUNT;
180 /* Poll the EC status register waiting for the event to occur. */
182 case ACPI_EC_EVENT_OBF:
184 acpi_hw_low_level_read(8, &acpi_ec_status,
185 &ec->common.status_addr);
186 if (acpi_ec_status & ACPI_EC_FLAG_OBF)
188 udelay(ACPI_EC_UDELAY);
191 case ACPI_EC_EVENT_IBE:
193 acpi_hw_low_level_read(8, &acpi_ec_status,
194 &ec->common.status_addr);
195 if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
197 udelay(ACPI_EC_UDELAY);
206 static int acpi_ec_intr_wait(union acpi_ec *ec, unsigned int event)
210 ACPI_FUNCTION_TRACE("acpi_ec_wait");
212 ec->intr.expect_event = event;
216 case ACPI_EC_EVENT_IBE:
217 if (~acpi_ec_read_status(ec) & event) {
218 ec->intr.expect_event = 0;
226 result = wait_event_timeout(ec->intr.wait,
227 !ec->intr.expect_event,
228 msecs_to_jiffies(ACPI_EC_DELAY));
230 ec->intr.expect_event = 0;
234 * Verify that the event in question has actually happened by
235 * querying EC status. Do the check even if operation timed-out
236 * to make sure that we did not miss interrupt.
239 case ACPI_EC_EVENT_OBF:
240 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
244 case ACPI_EC_EVENT_IBE:
245 if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
250 return_VALUE(-ETIME);
253 #ifdef ACPI_FUTURE_USAGE
255 * Note: samsung nv5000 doesn't work with ec burst mode.
256 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
258 int acpi_ec_enter_burst_mode(union acpi_ec *ec)
263 ACPI_FUNCTION_TRACE("acpi_ec_enter_burst_mode");
265 status = acpi_ec_read_status(ec);
266 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
267 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
270 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE,
271 &ec->common.command_addr);
272 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
273 acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
274 if (tmp != 0x90) { /* Burst ACK byte */
275 return_VALUE(-EINVAL);
279 atomic_set(&ec->intr.leaving_burst, 0);
282 printk(KERN_WARNING PREFIX "Error in acpi_ec_wait\n");
286 int acpi_ec_leave_burst_mode(union acpi_ec *ec)
290 ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
292 status = acpi_ec_read_status(ec);
293 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
294 status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
297 acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->common.command_addr);
298 acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
300 atomic_set(&ec->intr.leaving_burst, 1);
303 printk(KERN_WARNING PREFIX "leave burst_mode:error\n");
306 #endif /* ACPI_FUTURE_USAGE */
308 static int acpi_ec_read(union acpi_ec *ec, u8 address, u32 * data)
310 if (acpi_ec_poll_mode)
311 return acpi_ec_poll_read(ec, address, data);
313 return acpi_ec_intr_read(ec, address, data);
315 static int acpi_ec_write(union acpi_ec *ec, u8 address, u8 data)
317 if (acpi_ec_poll_mode)
318 return acpi_ec_poll_write(ec, address, data);
320 return acpi_ec_intr_write(ec, address, data);
322 static int acpi_ec_poll_read(union acpi_ec *ec, u8 address, u32 * data)
324 acpi_status status = AE_OK;
328 ACPI_FUNCTION_TRACE("acpi_ec_read");
331 return_VALUE(-EINVAL);
335 if (ec->common.global_lock) {
336 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
337 if (ACPI_FAILURE(status))
338 return_VALUE(-ENODEV);
341 if (down_interruptible(&ec->poll.sem)) {
342 result = -ERESTARTSYS;
346 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
347 &ec->common.command_addr);
348 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
352 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
353 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
357 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
359 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
365 if (ec->common.global_lock)
366 acpi_release_global_lock(glk);
368 return_VALUE(result);
371 static int acpi_ec_poll_write(union acpi_ec *ec, u8 address, u8 data)
374 acpi_status status = AE_OK;
377 ACPI_FUNCTION_TRACE("acpi_ec_write");
380 return_VALUE(-EINVAL);
382 if (ec->common.global_lock) {
383 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
384 if (ACPI_FAILURE(status))
385 return_VALUE(-ENODEV);
388 if (down_interruptible(&ec->poll.sem)) {
389 result = -ERESTARTSYS;
393 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
394 &ec->common.command_addr);
395 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
399 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
400 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
404 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
405 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
409 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
415 if (ec->common.global_lock)
416 acpi_release_global_lock(glk);
418 return_VALUE(result);
421 static int acpi_ec_intr_read(union acpi_ec *ec, u8 address, u32 * data)
426 ACPI_FUNCTION_TRACE("acpi_ec_read");
429 return_VALUE(-EINVAL);
433 if (ec->common.global_lock) {
434 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
435 if (ACPI_FAILURE(status))
436 return_VALUE(-ENODEV);
439 WARN_ON(in_interrupt());
442 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
444 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
447 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
448 &ec->common.command_addr);
449 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
451 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
454 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
455 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
457 printk(KERN_DEBUG PREFIX "read EC, OB not full\n");
460 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
461 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
467 if (ec->common.global_lock)
468 acpi_release_global_lock(glk);
470 return_VALUE(status);
473 static int acpi_ec_intr_write(union acpi_ec *ec, u8 address, u8 data)
478 ACPI_FUNCTION_TRACE("acpi_ec_write");
481 return_VALUE(-EINVAL);
483 if (ec->common.global_lock) {
484 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
485 if (ACPI_FAILURE(status))
486 return_VALUE(-ENODEV);
489 WARN_ON(in_interrupt());
492 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
494 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
496 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
497 &ec->common.command_addr);
498 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
500 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
503 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
504 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
506 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
509 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
511 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
516 if (ec->common.global_lock)
517 acpi_release_global_lock(glk);
519 return_VALUE(status);
523 * Externally callable EC access functions. For now, assume 1 EC only
525 int ec_read(u8 addr, u8 * val)
534 ec = acpi_driver_data(first_ec);
536 err = acpi_ec_read(ec, addr, &temp_data);
545 EXPORT_SYMBOL(ec_read);
547 int ec_write(u8 addr, u8 val)
555 ec = acpi_driver_data(first_ec);
557 err = acpi_ec_write(ec, addr, val);
562 EXPORT_SYMBOL(ec_write);
564 static int acpi_ec_query(union acpi_ec *ec, u32 * data)
566 if (acpi_ec_poll_mode)
567 return acpi_ec_poll_query(ec, data);
569 return acpi_ec_intr_query(ec, data);
571 static int acpi_ec_poll_query(union acpi_ec *ec, u32 * data)
574 acpi_status status = AE_OK;
577 ACPI_FUNCTION_TRACE("acpi_ec_query");
580 return_VALUE(-EINVAL);
584 if (ec->common.global_lock) {
585 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
586 if (ACPI_FAILURE(status))
587 return_VALUE(-ENODEV);
591 * Query the EC to find out which _Qxx method we need to evaluate.
592 * Note that successful completion of the query causes the ACPI_EC_SCI
593 * bit to be cleared (and thus clearing the interrupt source).
595 if (down_interruptible(&ec->poll.sem)) {
596 result = -ERESTARTSYS;
600 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
601 &ec->common.command_addr);
602 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
606 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
613 if (ec->common.global_lock)
614 acpi_release_global_lock(glk);
616 return_VALUE(result);
618 static int acpi_ec_intr_query(union acpi_ec *ec, u32 * data)
623 ACPI_FUNCTION_TRACE("acpi_ec_query");
626 return_VALUE(-EINVAL);
629 if (ec->common.global_lock) {
630 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
631 if (ACPI_FAILURE(status))
632 return_VALUE(-ENODEV);
637 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
639 printk(KERN_DEBUG PREFIX "query EC, IB not empty\n");
643 * Query the EC to find out which _Qxx method we need to evaluate.
644 * Note that successful completion of the query causes the ACPI_EC_SCI
645 * bit to be cleared (and thus clearing the interrupt source).
647 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
648 &ec->common.command_addr);
649 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
651 printk(KERN_DEBUG PREFIX "query EC, OB not full\n");
655 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
662 if (ec->common.global_lock)
663 acpi_release_global_lock(glk);
665 return_VALUE(status);
668 /* --------------------------------------------------------------------------
670 -------------------------------------------------------------------------- */
672 union acpi_ec_query_data {
677 static void acpi_ec_gpe_query(void *ec_cxt)
679 if (acpi_ec_poll_mode)
680 acpi_ec_gpe_poll_query(ec_cxt);
682 acpi_ec_gpe_intr_query(ec_cxt);
685 static void acpi_ec_gpe_poll_query(void *ec_cxt)
687 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
689 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
690 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
691 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
694 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
699 if (down_interruptible (&ec->poll.sem)) {
702 acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
705 /* TBD: Implement asynch events!
706 * NOTE: All we care about are EC-SCI's. Other EC events are
707 * handled via polling (yuck!). This is because some systems
708 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
709 * a purely interrupt-driven approach (grumble, grumble).
711 if (!(value & ACPI_EC_FLAG_SCI))
714 if (acpi_ec_query(ec, &value))
717 object_name[2] = hex[((value >> 4) & 0x0F)];
718 object_name[3] = hex[(value & 0x0F)];
720 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
722 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
725 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
727 static void acpi_ec_gpe_intr_query(void *ec_cxt)
729 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
731 int result = -ENODATA;
732 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
733 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
734 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
737 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
739 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
740 result = acpi_ec_query(ec, &value);
745 object_name[2] = hex[((value >> 4) & 0x0F)];
746 object_name[3] = hex[(value & 0x0F)];
748 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
750 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
752 atomic_dec(&ec->intr.pending_gpe);
756 static u32 acpi_ec_gpe_handler(void *data)
758 if (acpi_ec_poll_mode)
759 return acpi_ec_gpe_poll_handler(data);
761 return acpi_ec_gpe_intr_handler(data);
763 static u32 acpi_ec_gpe_poll_handler(void *data)
765 acpi_status status = AE_OK;
766 union acpi_ec *ec = (union acpi_ec *)data;
769 return ACPI_INTERRUPT_NOT_HANDLED;
771 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
773 status = acpi_os_execute(OSL_EC_POLL_HANDLER, acpi_ec_gpe_query, ec);
776 return ACPI_INTERRUPT_HANDLED;
778 return ACPI_INTERRUPT_NOT_HANDLED;
780 static u32 acpi_ec_gpe_intr_handler(void *data)
782 acpi_status status = AE_OK;
784 union acpi_ec *ec = (union acpi_ec *)data;
787 return ACPI_INTERRUPT_NOT_HANDLED;
789 acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
790 value = acpi_ec_read_status(ec);
792 switch (ec->intr.expect_event) {
793 case ACPI_EC_EVENT_OBF:
794 if (!(value & ACPI_EC_FLAG_OBF))
796 case ACPI_EC_EVENT_IBE:
797 if ((value & ACPI_EC_FLAG_IBF))
799 ec->intr.expect_event = 0;
800 wake_up(&ec->intr.wait);
801 return ACPI_INTERRUPT_HANDLED;
806 if (value & ACPI_EC_FLAG_SCI) {
807 atomic_add(1, &ec->intr.pending_gpe);
808 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
809 acpi_ec_gpe_query, ec);
810 return status == AE_OK ?
811 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
813 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
814 return status == AE_OK ?
815 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
818 /* --------------------------------------------------------------------------
819 Address Space Management
820 -------------------------------------------------------------------------- */
823 acpi_ec_space_setup(acpi_handle region_handle,
824 u32 function, void *handler_context, void **return_context)
827 * The EC object is in the handler context and is needed
828 * when calling the acpi_ec_space_handler.
830 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
831 handler_context : NULL;
837 acpi_ec_space_handler(u32 function,
838 acpi_physical_address address,
840 acpi_integer * value,
841 void *handler_context, void *region_context)
844 union acpi_ec *ec = NULL;
846 acpi_integer f_v = 0;
849 ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
851 if ((address > 0xFF) || !value || !handler_context)
852 return_VALUE(AE_BAD_PARAMETER);
854 if (bit_width != 8 && acpi_strict) {
855 printk(KERN_WARNING PREFIX
856 "acpi_ec_space_handler: bit_width should be 8\n");
857 return_VALUE(AE_BAD_PARAMETER);
860 ec = (union acpi_ec *)handler_context;
866 result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
869 result = acpi_ec_write(ec, (u8) address, (u8) temp);
879 if (function == ACPI_READ)
880 f_v |= temp << 8 * i;
881 if (function == ACPI_WRITE)
888 if (function == ACPI_READ) {
889 f_v |= temp << 8 * i;
896 return_VALUE(AE_BAD_PARAMETER);
899 return_VALUE(AE_NOT_FOUND);
902 return_VALUE(AE_TIME);
909 /* --------------------------------------------------------------------------
911 -------------------------------------------------------------------------- */
913 static struct proc_dir_entry *acpi_ec_dir;
915 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
917 union acpi_ec *ec = (union acpi_ec *)seq->private;
919 ACPI_FUNCTION_TRACE("acpi_ec_read_info");
924 seq_printf(seq, "gpe bit: 0x%02x\n",
925 (u32) ec->common.gpe_bit);
926 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
927 (u32) ec->common.status_addr.address,
928 (u32) ec->common.data_addr.address);
929 seq_printf(seq, "use global lock: %s\n",
930 ec->common.global_lock ? "yes" : "no");
931 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
937 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
939 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
942 static struct file_operations acpi_ec_info_ops = {
943 .open = acpi_ec_info_open_fs,
946 .release = single_release,
947 .owner = THIS_MODULE,
950 static int acpi_ec_add_fs(struct acpi_device *device)
952 struct proc_dir_entry *entry = NULL;
954 ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
956 if (!acpi_device_dir(device)) {
957 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
959 if (!acpi_device_dir(device))
960 return_VALUE(-ENODEV);
963 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
964 acpi_device_dir(device));
966 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
967 "Unable to create '%s' fs entry\n",
970 entry->proc_fops = &acpi_ec_info_ops;
971 entry->data = acpi_driver_data(device);
972 entry->owner = THIS_MODULE;
978 static int acpi_ec_remove_fs(struct acpi_device *device)
980 ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
982 if (acpi_device_dir(device)) {
983 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
984 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
985 acpi_device_dir(device) = NULL;
991 /* --------------------------------------------------------------------------
993 -------------------------------------------------------------------------- */
995 static int acpi_ec_poll_add(struct acpi_device *device)
998 acpi_status status = AE_OK;
999 union acpi_ec *ec = NULL;
1001 ACPI_FUNCTION_TRACE("acpi_ec_add");
1004 return_VALUE(-EINVAL);
1006 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1008 return_VALUE(-ENOMEM);
1009 memset(ec, 0, sizeof(union acpi_ec));
1011 ec->common.handle = device->handle;
1012 ec->common.uid = -1;
1013 init_MUTEX(&ec->poll.sem);
1014 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1015 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1016 acpi_driver_data(device) = ec;
1018 /* Use the global lock for all EC transactions? */
1019 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
1020 &ec->common.global_lock);
1022 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
1023 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
1025 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1027 &acpi_ec_space_handler);
1029 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1030 &acpi_ec_gpe_handler);
1035 /* Get GPE bit assignment (EC events). */
1036 /* TODO: Add support for _GPE returning a package */
1038 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
1039 &ec->common.gpe_bit);
1040 if (ACPI_FAILURE(status)) {
1041 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1042 "Error obtaining GPE bit assignment\n"));
1047 result = acpi_ec_add_fs(device);
1051 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) polling mode.\n",
1052 acpi_device_name(device), acpi_device_bid(device),
1053 (u32) ec->common.gpe_bit);
1062 return_VALUE(result);
1064 static int acpi_ec_intr_add(struct acpi_device *device)
1067 acpi_status status = AE_OK;
1068 union acpi_ec *ec = NULL;
1070 ACPI_FUNCTION_TRACE("acpi_ec_add");
1073 return_VALUE(-EINVAL);
1075 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1077 return_VALUE(-ENOMEM);
1078 memset(ec, 0, sizeof(union acpi_ec));
1080 ec->common.handle = device->handle;
1081 ec->common.uid = -1;
1082 atomic_set(&ec->intr.pending_gpe, 0);
1083 atomic_set(&ec->intr.leaving_burst, 1);
1084 init_MUTEX(&ec->intr.sem);
1085 init_waitqueue_head(&ec->intr.wait);
1086 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1087 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1088 acpi_driver_data(device) = ec;
1090 /* Use the global lock for all EC transactions? */
1091 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
1092 &ec->common.global_lock);
1094 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
1095 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
1097 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1099 &acpi_ec_space_handler);
1101 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1102 &acpi_ec_gpe_handler);
1107 /* Get GPE bit assignment (EC events). */
1108 /* TODO: Add support for _GPE returning a package */
1110 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
1111 &ec->common.gpe_bit);
1112 if (ACPI_FAILURE(status)) {
1113 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1114 "Error obtaining GPE bit assignment\n"));
1119 result = acpi_ec_add_fs(device);
1123 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) interrupt mode.\n",
1124 acpi_device_name(device), acpi_device_bid(device),
1125 (u32) ec->common.gpe_bit);
1134 return_VALUE(result);
1137 static int acpi_ec_remove(struct acpi_device *device, int type)
1139 union acpi_ec *ec = NULL;
1141 ACPI_FUNCTION_TRACE("acpi_ec_remove");
1144 return_VALUE(-EINVAL);
1146 ec = acpi_driver_data(device);
1148 acpi_ec_remove_fs(device);
1156 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
1158 union acpi_ec *ec = (union acpi_ec *)context;
1159 struct acpi_generic_address *addr;
1161 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
1166 * The first address region returned is the data port, and
1167 * the second address region returned is the status/command
1170 if (ec->common.data_addr.register_bit_width == 0) {
1171 addr = &ec->common.data_addr;
1172 } else if (ec->common.command_addr.register_bit_width == 0) {
1173 addr = &ec->common.command_addr;
1175 return AE_CTRL_TERMINATE;
1178 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1179 addr->register_bit_width = 8;
1180 addr->register_bit_offset = 0;
1181 addr->address = resource->data.io.minimum;
1186 static int acpi_ec_start(struct acpi_device *device)
1188 acpi_status status = AE_OK;
1189 union acpi_ec *ec = NULL;
1191 ACPI_FUNCTION_TRACE("acpi_ec_start");
1194 return_VALUE(-EINVAL);
1196 ec = acpi_driver_data(device);
1199 return_VALUE(-EINVAL);
1202 * Get I/O port addresses. Convert to GAS format.
1204 status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
1205 acpi_ec_io_ports, ec);
1206 if (ACPI_FAILURE(status)
1207 || ec->common.command_addr.register_bit_width == 0) {
1208 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1209 "Error getting I/O port addresses"));
1210 return_VALUE(-ENODEV);
1213 ec->common.status_addr = ec->common.command_addr;
1215 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
1216 (u32) ec->common.gpe_bit,
1217 (u32) ec->common.command_addr.address,
1218 (u32) ec->common.data_addr.address));
1221 * Install GPE handler
1223 status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
1224 ACPI_GPE_EDGE_TRIGGERED,
1225 &acpi_ec_gpe_handler, ec);
1226 if (ACPI_FAILURE(status)) {
1227 return_VALUE(-ENODEV);
1229 acpi_set_gpe_type(NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1230 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
1232 status = acpi_install_address_space_handler(ec->common.handle,
1234 &acpi_ec_space_handler,
1235 &acpi_ec_space_setup, ec);
1236 if (ACPI_FAILURE(status)) {
1237 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1238 &acpi_ec_gpe_handler);
1239 return_VALUE(-ENODEV);
1242 return_VALUE(AE_OK);
1245 static int acpi_ec_stop(struct acpi_device *device, int type)
1247 acpi_status status = AE_OK;
1248 union acpi_ec *ec = NULL;
1250 ACPI_FUNCTION_TRACE("acpi_ec_stop");
1253 return_VALUE(-EINVAL);
1255 ec = acpi_driver_data(device);
1257 status = acpi_remove_address_space_handler(ec->common.handle,
1259 &acpi_ec_space_handler);
1260 if (ACPI_FAILURE(status))
1261 return_VALUE(-ENODEV);
1264 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1265 &acpi_ec_gpe_handler);
1266 if (ACPI_FAILURE(status))
1267 return_VALUE(-ENODEV);
1272 static acpi_status __init
1273 acpi_fake_ecdt_callback(acpi_handle handle,
1274 u32 Level, void *context, void **retval)
1277 if (acpi_ec_poll_mode)
1278 return acpi_fake_ecdt_poll_callback(handle,
1279 Level, context, retval);
1281 return acpi_fake_ecdt_intr_callback(handle,
1282 Level, context, retval);
1285 static acpi_status __init
1286 acpi_fake_ecdt_poll_callback(acpi_handle handle,
1287 u32 Level, void *context, void **retval)
1291 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1292 acpi_ec_io_ports, ec_ecdt);
1293 if (ACPI_FAILURE(status))
1295 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1297 ec_ecdt->common.uid = -1;
1298 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1301 acpi_evaluate_integer(handle, "_GPE", NULL,
1302 &ec_ecdt->common.gpe_bit);
1303 if (ACPI_FAILURE(status))
1305 init_MUTEX(&ec_ecdt->poll.sem);
1306 ec_ecdt->common.global_lock = TRUE;
1307 ec_ecdt->common.handle = handle;
1309 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1310 (u32) ec_ecdt->common.gpe_bit,
1311 (u32) ec_ecdt->common.command_addr.address,
1312 (u32) ec_ecdt->common.data_addr.address);
1314 return AE_CTRL_TERMINATE;
1317 static acpi_status __init
1318 acpi_fake_ecdt_intr_callback(acpi_handle handle,
1319 u32 Level, void *context, void **retval)
1323 init_MUTEX(&ec_ecdt->intr.sem);
1324 init_waitqueue_head(&ec_ecdt->intr.wait);
1325 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1326 acpi_ec_io_ports, ec_ecdt);
1327 if (ACPI_FAILURE(status))
1329 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1331 ec_ecdt->common.uid = -1;
1332 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1335 acpi_evaluate_integer(handle, "_GPE", NULL,
1336 &ec_ecdt->common.gpe_bit);
1337 if (ACPI_FAILURE(status))
1339 ec_ecdt->common.global_lock = TRUE;
1340 ec_ecdt->common.handle = handle;
1342 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1343 (u32) ec_ecdt->common.gpe_bit,
1344 (u32) ec_ecdt->common.command_addr.address,
1345 (u32) ec_ecdt->common.data_addr.address);
1347 return AE_CTRL_TERMINATE;
1351 * Some BIOS (such as some from Gateway laptops) access EC region very early
1352 * such as in BAT0._INI or EC._INI before an EC device is found and
1353 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1354 * required, but if EC regison is accessed early, it is required.
1355 * The routine tries to workaround the BIOS bug by pre-scan EC device
1356 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1357 * op region (since _REG isn't invoked yet). The assumption is true for
1358 * all systems found.
1360 static int __init acpi_ec_fake_ecdt(void)
1365 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1367 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1372 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1374 status = acpi_get_devices(ACPI_EC_HID,
1375 acpi_fake_ecdt_callback, NULL, NULL);
1376 if (ACPI_FAILURE(status)) {
1384 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1388 static int __init acpi_ec_get_real_ecdt(void)
1390 if (acpi_ec_poll_mode)
1391 return acpi_ec_poll_get_real_ecdt();
1393 return acpi_ec_intr_get_real_ecdt();
1396 static int __init acpi_ec_poll_get_real_ecdt(void)
1399 struct acpi_table_ecdt *ecdt_ptr;
1401 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1402 (struct acpi_table_header **)
1404 if (ACPI_FAILURE(status))
1407 printk(KERN_INFO PREFIX "Found ECDT\n");
1410 * Generate a temporary ec context to use until the namespace is scanned
1412 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1415 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1417 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1418 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1419 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1420 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1421 init_MUTEX(&ec_ecdt->poll.sem);
1422 /* use the GL just to be safe */
1423 ec_ecdt->common.global_lock = TRUE;
1424 ec_ecdt->common.uid = ecdt_ptr->uid;
1427 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1428 if (ACPI_FAILURE(status)) {
1434 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1441 static int __init acpi_ec_intr_get_real_ecdt(void)
1444 struct acpi_table_ecdt *ecdt_ptr;
1446 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1447 (struct acpi_table_header **)
1449 if (ACPI_FAILURE(status))
1452 printk(KERN_INFO PREFIX "Found ECDT\n");
1455 * Generate a temporary ec context to use until the namespace is scanned
1457 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1460 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1462 init_MUTEX(&ec_ecdt->intr.sem);
1463 init_waitqueue_head(&ec_ecdt->intr.wait);
1464 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1465 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1466 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1467 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1468 /* use the GL just to be safe */
1469 ec_ecdt->common.global_lock = TRUE;
1470 ec_ecdt->common.uid = ecdt_ptr->uid;
1473 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1474 if (ACPI_FAILURE(status)) {
1480 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1487 static int __initdata acpi_fake_ecdt_enabled;
1488 int __init acpi_ec_ecdt_probe(void)
1493 ret = acpi_ec_get_real_ecdt();
1494 /* Try to make a fake ECDT */
1495 if (ret && acpi_fake_ecdt_enabled) {
1496 ret = acpi_ec_fake_ecdt();
1503 * Install GPE handler
1505 status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1506 ACPI_GPE_EDGE_TRIGGERED,
1507 &acpi_ec_gpe_handler, ec_ecdt);
1508 if (ACPI_FAILURE(status)) {
1511 acpi_set_gpe_type(NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1512 acpi_enable_gpe(NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
1514 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
1516 &acpi_ec_space_handler,
1517 &acpi_ec_space_setup,
1519 if (ACPI_FAILURE(status)) {
1520 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1521 &acpi_ec_gpe_handler);
1528 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1535 static int __init acpi_ec_init(void)
1539 ACPI_FUNCTION_TRACE("acpi_ec_init");
1544 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1546 return_VALUE(-ENODEV);
1548 /* Now register the driver for the EC */
1549 result = acpi_bus_register_driver(&acpi_ec_driver);
1551 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1552 return_VALUE(-ENODEV);
1555 return_VALUE(result);
1558 subsys_initcall(acpi_ec_init);
1560 /* EC driver currently not unloadable */
1562 static void __exit acpi_ec_exit(void)
1564 ACPI_FUNCTION_TRACE("acpi_ec_exit");
1566 acpi_bus_unregister_driver(&acpi_ec_driver);
1568 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1574 static int __init acpi_fake_ecdt_setup(char *str)
1576 acpi_fake_ecdt_enabled = 1;
1580 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1581 static int __init acpi_ec_set_intr_mode(char *str)
1585 if (!get_option(&str, &intr))
1589 acpi_ec_poll_mode = EC_INTR;
1590 acpi_ec_driver.ops.add = acpi_ec_intr_add;
1592 acpi_ec_poll_mode = EC_POLL;
1593 acpi_ec_driver.ops.add = acpi_ec_poll_add;
1595 printk(KERN_INFO PREFIX "EC %s mode.\n", intr ? "interrupt" : "polling");
1599 __setup("ec_intr=", acpi_ec_set_intr_mode);