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"
49 /* EC status register */
50 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
51 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
52 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
53 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
56 #define ACPI_EC_COMMAND_READ 0x80
57 #define ACPI_EC_COMMAND_WRITE 0x81
58 #define ACPI_EC_BURST_ENABLE 0x82
59 #define ACPI_EC_BURST_DISABLE 0x83
60 #define ACPI_EC_COMMAND_QUERY 0x84
64 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
65 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
68 #define ACPI_EC_DELAY 50 /* Wait 50ms max. during EC ops */
69 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
70 #define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
71 #define ACPI_EC_UDELAY_COUNT 1000 /* Wait 10ms max. during EC ops */
74 EC_INTR = 1, /* Output buffer full */
75 EC_POLL, /* Input buffer empty */
78 static int acpi_ec_remove(struct acpi_device *device, int type);
79 static int acpi_ec_start(struct acpi_device *device);
80 static int acpi_ec_stop(struct acpi_device *device, int type);
81 static int acpi_ec_add(struct acpi_device *device);
83 static struct acpi_driver acpi_ec_driver = {
84 .name = ACPI_EC_DRIVER_NAME,
85 .class = ACPI_EC_CLASS,
89 .remove = acpi_ec_remove,
90 .start = acpi_ec_start,
97 unsigned long gpe_bit;
98 struct acpi_generic_address status_addr;
99 struct acpi_generic_address command_addr;
100 struct acpi_generic_address data_addr;
101 unsigned long global_lock;
102 struct semaphore sem;
103 unsigned int expect_event;
104 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
105 wait_queue_head_t wait;
108 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
109 static struct acpi_ec *ec_ecdt;
111 /* External interfaces use first EC only, so remember */
112 static struct acpi_device *first_ec;
113 static int acpi_ec_mode = EC_INTR;
115 static void acpi_ec_gpe_poll_query(void *ec_cxt);
116 static void acpi_ec_gpe_intr_query(void *ec_cxt);
117 static u32 acpi_ec_gpe_poll_handler(void *data);
118 static u32 acpi_ec_gpe_intr_handler(void *data);
120 /* --------------------------------------------------------------------------
121 Transaction Management
122 -------------------------------------------------------------------------- */
124 static u32 acpi_ec_read_status(struct acpi_ec *ec)
128 acpi_hw_low_level_read(8, &status, &ec->status_addr);
132 static u32 acpi_ec_read_data(struct acpi_ec *ec)
136 acpi_hw_low_level_read(8, &data, &ec->data_addr);
140 static void acpi_ec_write_cmd(struct acpi_ec *ec, u32 command)
142 acpi_hw_low_level_write(8, command, &ec->command_addr);
145 static void acpi_ec_write_data(struct acpi_ec *ec, u32 data)
147 acpi_hw_low_level_write(8, data, &ec->data_addr);
150 static int acpi_ec_check_status(u32 status, u8 event) {
153 case ACPI_EC_EVENT_OBF_1:
154 if (status & ACPI_EC_FLAG_OBF)
157 case ACPI_EC_EVENT_IBF_0:
158 if (!(status & ACPI_EC_FLAG_IBF))
168 static int acpi_ec_wait(struct acpi_ec *ec, u8 event)
170 int i = (acpi_ec_mode == EC_POLL) ? ACPI_EC_UDELAY_COUNT : 0;
173 ec->expect_event = event;
174 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
175 ec->expect_event = 0;
180 if (acpi_ec_mode == EC_POLL) {
181 udelay(ACPI_EC_UDELAY);
183 time_left = wait_event_timeout(ec->wait,
185 msecs_to_jiffies(ACPI_EC_DELAY));
187 ec->expect_event = 0;
191 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
192 ec->expect_event = 0;
197 ec->expect_event = 0;
202 #ifdef ACPI_FUTURE_USAGE
204 * Note: samsung nv5000 doesn't work with ec burst mode.
205 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
207 int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
213 status = acpi_ec_read_status(ec);
214 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
215 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
218 acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
219 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
220 tmp = acpi_ec_read_data(ec);
221 if (tmp != 0x90) { /* Burst ACK byte */
226 atomic_set(&ec->leaving_burst, 0);
229 ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
233 int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
238 status = acpi_ec_read_status(ec);
239 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
240 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
243 acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
244 acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
246 atomic_set(&ec->leaving_burst, 1);
249 ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
252 #endif /* ACPI_FUTURE_USAGE */
254 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
255 const u8 *wdata, unsigned wdata_len,
256 u8 *rdata, unsigned rdata_len)
260 acpi_ec_write_cmd(ec, command);
262 for (; wdata_len > 0; wdata_len --) {
263 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
266 acpi_ec_write_data(ec, *(wdata++));
269 if (command == ACPI_EC_COMMAND_WRITE) {
270 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
275 for (; rdata_len > 0; rdata_len --) {
278 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
282 d = acpi_ec_read_data(ec);
289 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
290 const u8 *wdata, unsigned wdata_len,
291 u8 *rdata, unsigned rdata_len)
296 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
300 memset(rdata, 0, rdata_len);
302 if (ec->global_lock) {
303 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
304 if (ACPI_FAILURE(status))
309 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
311 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
315 status = acpi_ec_transaction_unlocked(ec, command,
323 acpi_release_global_lock(glk);
328 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u32 * data)
333 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
338 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
340 u8 wdata[2] = { address, data };
341 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
346 * Externally callable EC access functions. For now, assume 1 EC only
348 int ec_read(u8 addr, u8 * val)
357 ec = acpi_driver_data(first_ec);
359 err = acpi_ec_read(ec, addr, &temp_data);
368 EXPORT_SYMBOL(ec_read);
370 int ec_write(u8 addr, u8 val)
378 ec = acpi_driver_data(first_ec);
380 err = acpi_ec_write(ec, addr, val);
385 EXPORT_SYMBOL(ec_write);
387 extern int ec_transaction(u8 command,
388 const u8 *wdata, unsigned wdata_len,
389 u8 *rdata, unsigned rdata_len)
396 ec = acpi_driver_data(first_ec);
398 return acpi_ec_transaction(ec, command, wdata,
399 wdata_len, rdata, rdata_len);
402 static int acpi_ec_query(struct acpi_ec *ec, u32 * data)
411 * Query the EC to find out which _Qxx method we need to evaluate.
412 * Note that successful completion of the query causes the ACPI_EC_SCI
413 * bit to be cleared (and thus clearing the interrupt source).
416 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
427 /* --------------------------------------------------------------------------
429 -------------------------------------------------------------------------- */
431 union acpi_ec_query_data {
436 static void acpi_ec_gpe_query(void *ec_cxt)
438 if (acpi_ec_mode == EC_POLL)
439 acpi_ec_gpe_poll_query(ec_cxt);
441 acpi_ec_gpe_intr_query(ec_cxt);
444 static void acpi_ec_gpe_poll_query(void *ec_cxt)
446 struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
448 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
449 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
450 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
457 if (down_interruptible (&ec->sem)) {
460 value = acpi_ec_read_status(ec);
463 /* TBD: Implement asynch events!
464 * NOTE: All we care about are EC-SCI's. Other EC events are
465 * handled via polling (yuck!). This is because some systems
466 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
467 * a purely interrupt-driven approach (grumble, grumble).
469 if (!(value & ACPI_EC_FLAG_SCI))
472 if (acpi_ec_query(ec, &value))
475 object_name[2] = hex[((value >> 4) & 0x0F)];
476 object_name[3] = hex[(value & 0x0F)];
478 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
480 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
483 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
485 static void acpi_ec_gpe_intr_query(void *ec_cxt)
487 struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
489 int result = -ENODATA;
490 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
491 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
492 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
496 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
497 result = acpi_ec_query(ec, &value);
502 object_name[2] = hex[((value >> 4) & 0x0F)];
503 object_name[3] = hex[(value & 0x0F)];
505 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
507 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
512 static u32 acpi_ec_gpe_handler(void *data)
514 if (acpi_ec_mode == EC_POLL)
515 return acpi_ec_gpe_poll_handler(data);
517 return acpi_ec_gpe_intr_handler(data);
519 static u32 acpi_ec_gpe_poll_handler(void *data)
521 acpi_status status = AE_OK;
522 struct acpi_ec *ec = (struct acpi_ec *)data;
525 return ACPI_INTERRUPT_NOT_HANDLED;
527 acpi_disable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
529 status = acpi_os_execute(OSL_EC_POLL_HANDLER, acpi_ec_gpe_query, ec);
532 return ACPI_INTERRUPT_HANDLED;
534 return ACPI_INTERRUPT_NOT_HANDLED;
536 static u32 acpi_ec_gpe_intr_handler(void *data)
538 acpi_status status = AE_OK;
540 struct acpi_ec *ec = (struct acpi_ec *)data;
543 return ACPI_INTERRUPT_NOT_HANDLED;
545 acpi_clear_gpe(NULL, ec->gpe_bit, ACPI_ISR);
546 value = acpi_ec_read_status(ec);
548 switch (ec->expect_event) {
549 case ACPI_EC_EVENT_OBF_1:
550 if (!(value & ACPI_EC_FLAG_OBF))
552 ec->expect_event = 0;
555 case ACPI_EC_EVENT_IBF_0:
556 if ((value & ACPI_EC_FLAG_IBF))
558 ec->expect_event = 0;
565 if (value & ACPI_EC_FLAG_SCI) {
566 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
567 acpi_ec_gpe_query, ec);
568 return status == AE_OK ?
569 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
571 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
572 return status == AE_OK ?
573 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
576 /* --------------------------------------------------------------------------
577 Address Space Management
578 -------------------------------------------------------------------------- */
581 acpi_ec_space_setup(acpi_handle region_handle,
582 u32 function, void *handler_context, void **return_context)
585 * The EC object is in the handler context and is needed
586 * when calling the acpi_ec_space_handler.
588 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
589 handler_context : NULL;
595 acpi_ec_space_handler(u32 function,
596 acpi_physical_address address,
598 acpi_integer * value,
599 void *handler_context, void *region_context)
602 struct acpi_ec *ec = NULL;
604 acpi_integer f_v = 0;
608 if ((address > 0xFF) || !value || !handler_context)
609 return AE_BAD_PARAMETER;
611 if (bit_width != 8 && acpi_strict) {
612 return AE_BAD_PARAMETER;
615 ec = (struct acpi_ec *)handler_context;
621 result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
624 result = acpi_ec_write(ec, (u8) address, (u8) temp);
634 if (function == ACPI_READ)
635 f_v |= temp << 8 * i;
636 if (function == ACPI_WRITE)
643 if (function == ACPI_READ) {
644 f_v |= temp << 8 * i;
651 return AE_BAD_PARAMETER;
664 /* --------------------------------------------------------------------------
666 -------------------------------------------------------------------------- */
668 static struct proc_dir_entry *acpi_ec_dir;
670 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
672 struct acpi_ec *ec = (struct acpi_ec *)seq->private;
678 seq_printf(seq, "gpe bit: 0x%02x\n",
680 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
681 (u32) ec->status_addr.address,
682 (u32) ec->data_addr.address);
683 seq_printf(seq, "use global lock: %s\n",
684 ec->global_lock ? "yes" : "no");
685 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
691 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
693 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
696 static struct file_operations acpi_ec_info_ops = {
697 .open = acpi_ec_info_open_fs,
700 .release = single_release,
701 .owner = THIS_MODULE,
704 static int acpi_ec_add_fs(struct acpi_device *device)
706 struct proc_dir_entry *entry = NULL;
709 if (!acpi_device_dir(device)) {
710 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
712 if (!acpi_device_dir(device))
716 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
717 acpi_device_dir(device));
721 entry->proc_fops = &acpi_ec_info_ops;
722 entry->data = acpi_driver_data(device);
723 entry->owner = THIS_MODULE;
729 static int acpi_ec_remove_fs(struct acpi_device *device)
732 if (acpi_device_dir(device)) {
733 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
734 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
735 acpi_device_dir(device) = NULL;
741 /* --------------------------------------------------------------------------
743 -------------------------------------------------------------------------- */
745 static int acpi_ec_add(struct acpi_device *device)
748 acpi_status status = AE_OK;
749 struct acpi_ec *ec = NULL;
755 ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
758 memset(ec, 0, sizeof(struct acpi_ec));
760 ec->handle = device->handle;
762 init_MUTEX(&ec->sem);
763 if (acpi_ec_mode == EC_INTR) {
764 atomic_set(&ec->leaving_burst, 1);
765 init_waitqueue_head(&ec->wait);
767 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
768 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
769 acpi_driver_data(device) = ec;
771 /* Use the global lock for all EC transactions? */
772 acpi_evaluate_integer(ec->handle, "_GLK", NULL,
775 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
776 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
778 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
780 &acpi_ec_space_handler);
782 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
783 &acpi_ec_gpe_handler);
788 /* Get GPE bit assignment (EC events). */
789 /* TODO: Add support for _GPE returning a package */
791 acpi_evaluate_integer(ec->handle, "_GPE", NULL,
793 if (ACPI_FAILURE(status)) {
794 ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit assignment"));
799 result = acpi_ec_add_fs(device);
803 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
804 acpi_device_name(device), acpi_device_bid(device),
817 static int acpi_ec_remove(struct acpi_device *device, int type)
819 struct acpi_ec *ec = NULL;
825 ec = acpi_driver_data(device);
827 acpi_ec_remove_fs(device);
835 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
837 struct acpi_ec *ec = (struct acpi_ec *)context;
838 struct acpi_generic_address *addr;
840 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
845 * The first address region returned is the data port, and
846 * the second address region returned is the status/command
849 if (ec->data_addr.register_bit_width == 0) {
850 addr = &ec->data_addr;
851 } else if (ec->command_addr.register_bit_width == 0) {
852 addr = &ec->command_addr;
854 return AE_CTRL_TERMINATE;
857 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
858 addr->register_bit_width = 8;
859 addr->register_bit_offset = 0;
860 addr->address = resource->data.io.minimum;
865 static int acpi_ec_start(struct acpi_device *device)
867 acpi_status status = AE_OK;
868 struct acpi_ec *ec = NULL;
874 ec = acpi_driver_data(device);
880 * Get I/O port addresses. Convert to GAS format.
882 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
883 acpi_ec_io_ports, ec);
884 if (ACPI_FAILURE(status)
885 || ec->command_addr.register_bit_width == 0) {
886 ACPI_EXCEPTION((AE_INFO, status,
887 "Error getting I/O port addresses"));
891 ec->status_addr = ec->command_addr;
893 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x",
895 (u32) ec->command_addr.address,
896 (u32) ec->data_addr.address));
899 * Install GPE handler
901 status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
902 ACPI_GPE_EDGE_TRIGGERED,
903 &acpi_ec_gpe_handler, ec);
904 if (ACPI_FAILURE(status)) {
907 acpi_set_gpe_type(NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
908 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
910 status = acpi_install_address_space_handler(ec->handle,
912 &acpi_ec_space_handler,
913 &acpi_ec_space_setup, ec);
914 if (ACPI_FAILURE(status)) {
915 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
916 &acpi_ec_gpe_handler);
923 static int acpi_ec_stop(struct acpi_device *device, int type)
925 acpi_status status = AE_OK;
926 struct acpi_ec *ec = NULL;
932 ec = acpi_driver_data(device);
934 status = acpi_remove_address_space_handler(ec->handle,
936 &acpi_ec_space_handler);
937 if (ACPI_FAILURE(status))
941 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
942 &acpi_ec_gpe_handler);
943 if (ACPI_FAILURE(status))
949 static acpi_status __init
950 acpi_fake_ecdt_callback(acpi_handle handle,
951 u32 Level, void *context, void **retval)
955 init_MUTEX(&ec_ecdt->sem);
956 if (acpi_ec_mode == EC_INTR) {
957 init_waitqueue_head(&ec_ecdt->wait);
959 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
960 acpi_ec_io_ports, ec_ecdt);
961 if (ACPI_FAILURE(status))
963 ec_ecdt->status_addr = ec_ecdt->command_addr;
966 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
969 acpi_evaluate_integer(handle, "_GPE", NULL,
971 if (ACPI_FAILURE(status))
973 ec_ecdt->global_lock = TRUE;
974 ec_ecdt->handle = handle;
976 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02x, ports=0x%2x, 0x%2x",
977 (u32) ec_ecdt->gpe_bit,
978 (u32) ec_ecdt->command_addr.address,
979 (u32) ec_ecdt->data_addr.address));
981 return AE_CTRL_TERMINATE;
985 * Some BIOS (such as some from Gateway laptops) access EC region very early
986 * such as in BAT0._INI or EC._INI before an EC device is found and
987 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
988 * required, but if EC regison is accessed early, it is required.
989 * The routine tries to workaround the BIOS bug by pre-scan EC device
990 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
991 * op region (since _REG isn't invoked yet). The assumption is true for
994 static int __init acpi_ec_fake_ecdt(void)
999 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
1001 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1006 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
1008 status = acpi_get_devices(ACPI_EC_HID,
1009 acpi_fake_ecdt_callback, NULL, NULL);
1010 if (ACPI_FAILURE(status)) {
1014 ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
1022 static int __init acpi_ec_get_real_ecdt(void)
1025 struct acpi_table_ecdt *ecdt_ptr;
1027 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1028 (struct acpi_table_header **)
1030 if (ACPI_FAILURE(status))
1033 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
1036 * Generate a temporary ec context to use until the namespace is scanned
1038 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1041 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
1043 init_MUTEX(&ec_ecdt->sem);
1044 if (acpi_ec_mode == EC_INTR) {
1045 init_waitqueue_head(&ec_ecdt->wait);
1047 ec_ecdt->command_addr = ecdt_ptr->ec_control;
1048 ec_ecdt->status_addr = ecdt_ptr->ec_control;
1049 ec_ecdt->data_addr = ecdt_ptr->ec_data;
1050 ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
1051 /* use the GL just to be safe */
1052 ec_ecdt->global_lock = TRUE;
1053 ec_ecdt->uid = ecdt_ptr->uid;
1056 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
1057 if (ACPI_FAILURE(status)) {
1063 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
1070 static int __initdata acpi_fake_ecdt_enabled;
1071 int __init acpi_ec_ecdt_probe(void)
1076 ret = acpi_ec_get_real_ecdt();
1077 /* Try to make a fake ECDT */
1078 if (ret && acpi_fake_ecdt_enabled) {
1079 ret = acpi_ec_fake_ecdt();
1086 * Install GPE handler
1088 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
1089 ACPI_GPE_EDGE_TRIGGERED,
1090 &acpi_ec_gpe_handler, ec_ecdt);
1091 if (ACPI_FAILURE(status)) {
1094 acpi_set_gpe_type(NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1095 acpi_enable_gpe(NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
1097 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
1099 &acpi_ec_space_handler,
1100 &acpi_ec_space_setup,
1102 if (ACPI_FAILURE(status)) {
1103 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
1104 &acpi_ec_gpe_handler);
1111 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
1118 static int __init acpi_ec_init(void)
1126 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1130 /* Now register the driver for the EC */
1131 result = acpi_bus_register_driver(&acpi_ec_driver);
1133 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1140 subsys_initcall(acpi_ec_init);
1142 /* EC driver currently not unloadable */
1144 static void __exit acpi_ec_exit(void)
1147 acpi_bus_unregister_driver(&acpi_ec_driver);
1149 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1155 static int __init acpi_fake_ecdt_setup(char *str)
1157 acpi_fake_ecdt_enabled = 1;
1161 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1162 static int __init acpi_ec_set_intr_mode(char *str)
1166 if (!get_option(&str, &intr))
1170 acpi_ec_mode = EC_INTR;
1172 acpi_ec_mode = EC_POLL;
1174 acpi_ec_driver.ops.add = acpi_ec_add;
1175 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n", intr ? "interrupt" : "polling"));
1180 __setup("ec_intr=", acpi_ec_set_intr_mode);