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 int acpi_ec_poll_transaction(struct acpi_ec *ec, u8 command,
116 const u8 *wdata, unsigned wdata_len,
117 u8 *rdata, unsigned rdata_len);
118 static int acpi_ec_intr_transaction(struct acpi_ec *ec, u8 command,
119 const u8 *wdata, unsigned wdata_len,
120 u8 *rdata, unsigned rdata_len);
121 static void acpi_ec_gpe_poll_query(void *ec_cxt);
122 static void acpi_ec_gpe_intr_query(void *ec_cxt);
123 static u32 acpi_ec_gpe_poll_handler(void *data);
124 static u32 acpi_ec_gpe_intr_handler(void *data);
126 /* --------------------------------------------------------------------------
127 Transaction Management
128 -------------------------------------------------------------------------- */
130 static u32 acpi_ec_read_status(struct acpi_ec *ec)
134 acpi_hw_low_level_read(8, &status, &ec->status_addr);
138 static u32 acpi_ec_read_data(struct acpi_ec *ec)
142 acpi_hw_low_level_read(8, &data, &ec->data_addr);
146 static void acpi_ec_write_cmd(struct acpi_ec *ec, u32 command)
148 acpi_hw_low_level_write(8, command, &ec->command_addr);
151 static void acpi_ec_write_data(struct acpi_ec *ec, u32 data)
153 acpi_hw_low_level_write(8, data, &ec->data_addr);
156 static int acpi_ec_check_status(u32 status, u8 event) {
159 case ACPI_EC_EVENT_OBF_1:
160 if (status & ACPI_EC_FLAG_OBF)
163 case ACPI_EC_EVENT_IBF_0:
164 if (!(status & ACPI_EC_FLAG_IBF))
174 static int acpi_ec_wait(struct acpi_ec *ec, u8 event)
176 int i = (acpi_ec_mode == EC_POLL) ? ACPI_EC_UDELAY_COUNT : 0;
179 ec->expect_event = event;
180 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
181 ec->expect_event = 0;
186 if (acpi_ec_mode == EC_POLL) {
187 udelay(ACPI_EC_UDELAY);
189 time_left = wait_event_timeout(ec->wait,
191 msecs_to_jiffies(ACPI_EC_DELAY));
193 ec->expect_event = 0;
197 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
198 ec->expect_event = 0;
203 ec->expect_event = 0;
208 #ifdef ACPI_FUTURE_USAGE
210 * Note: samsung nv5000 doesn't work with ec burst mode.
211 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
213 int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
219 status = acpi_ec_read_status(ec);
220 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
221 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
224 acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
225 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
226 tmp = acpi_ec_read_data(ec);
227 if (tmp != 0x90) { /* Burst ACK byte */
232 atomic_set(&ec->leaving_burst, 0);
235 ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
239 int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
244 status = acpi_ec_read_status(ec);
245 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
246 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
249 acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
250 acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
252 atomic_set(&ec->leaving_burst, 1);
255 ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
258 #endif /* ACPI_FUTURE_USAGE */
260 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
261 const u8 *wdata, unsigned wdata_len,
262 u8 *rdata, unsigned rdata_len)
264 if (acpi_ec_mode == EC_POLL)
265 return acpi_ec_poll_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
267 return acpi_ec_intr_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
269 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u32 * data)
273 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ, &address, 1, &d, 1);
277 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
279 u8 wdata[2] = { address, data };
280 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE, wdata, 2, NULL, 0);
283 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
284 const u8 *wdata, unsigned wdata_len,
285 u8 *rdata, unsigned rdata_len)
289 acpi_ec_write_cmd(ec, command);
291 for (; wdata_len > 0; wdata_len --) {
292 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
296 acpi_ec_write_data(ec, *(wdata++));
299 if (command == ACPI_EC_COMMAND_WRITE) {
300 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
305 for (; rdata_len > 0; rdata_len --) {
308 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
312 d = acpi_ec_read_data(ec);
319 static int acpi_ec_poll_transaction(struct acpi_ec *ec, u8 command,
320 const u8 *wdata, unsigned wdata_len,
321 u8 *rdata, unsigned rdata_len)
323 acpi_status status = AE_OK;
327 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
331 memset(rdata, 0, rdata_len);
333 if (ec->global_lock) {
334 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
335 if (ACPI_FAILURE(status))
339 if (down_interruptible(&ec->sem)) {
340 result = -ERESTARTSYS;
344 result = acpi_ec_transaction_unlocked(ec, command,
351 acpi_release_global_lock(glk);
356 static int acpi_ec_intr_transaction(struct acpi_ec *ec, u8 command,
357 const u8 *wdata, unsigned wdata_len,
358 u8 *rdata, unsigned rdata_len)
363 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
367 memset(rdata, 0, rdata_len);
369 if (ec->global_lock) {
370 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
371 if (ACPI_FAILURE(status))
375 WARN_ON(in_interrupt());
378 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
380 ACPI_EXCEPTION((AE_INFO, status, "read EC, IB not empty"));
384 status = acpi_ec_transaction_unlocked(ec, command,
392 acpi_release_global_lock(glk);
398 * Externally callable EC access functions. For now, assume 1 EC only
400 int ec_read(u8 addr, u8 * val)
409 ec = acpi_driver_data(first_ec);
411 err = acpi_ec_read(ec, addr, &temp_data);
420 EXPORT_SYMBOL(ec_read);
422 int ec_write(u8 addr, u8 val)
430 ec = acpi_driver_data(first_ec);
432 err = acpi_ec_write(ec, addr, val);
437 EXPORT_SYMBOL(ec_write);
439 extern int ec_transaction(u8 command,
440 const u8 *wdata, unsigned wdata_len,
441 u8 *rdata, unsigned rdata_len)
448 ec = acpi_driver_data(first_ec);
450 return acpi_ec_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
453 EXPORT_SYMBOL(ec_transaction);
455 static int acpi_ec_query(struct acpi_ec *ec, u32 * data) {
463 * Query the EC to find out which _Qxx method we need to evaluate.
464 * Note that successful completion of the query causes the ACPI_EC_SCI
465 * bit to be cleared (and thus clearing the interrupt source).
468 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
479 /* --------------------------------------------------------------------------
481 -------------------------------------------------------------------------- */
483 union acpi_ec_query_data {
488 static void acpi_ec_gpe_query(void *ec_cxt)
490 if (acpi_ec_mode == EC_POLL)
491 acpi_ec_gpe_poll_query(ec_cxt);
493 acpi_ec_gpe_intr_query(ec_cxt);
496 static void acpi_ec_gpe_poll_query(void *ec_cxt)
498 struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
500 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
501 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
502 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
509 if (down_interruptible (&ec->sem)) {
512 value = acpi_ec_read_status(ec);
515 /* TBD: Implement asynch events!
516 * NOTE: All we care about are EC-SCI's. Other EC events are
517 * handled via polling (yuck!). This is because some systems
518 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
519 * a purely interrupt-driven approach (grumble, grumble).
521 if (!(value & ACPI_EC_FLAG_SCI))
524 if (acpi_ec_query(ec, &value))
527 object_name[2] = hex[((value >> 4) & 0x0F)];
528 object_name[3] = hex[(value & 0x0F)];
530 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
532 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
535 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
537 static void acpi_ec_gpe_intr_query(void *ec_cxt)
539 struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
541 int result = -ENODATA;
542 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
543 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
544 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
548 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
549 result = acpi_ec_query(ec, &value);
554 object_name[2] = hex[((value >> 4) & 0x0F)];
555 object_name[3] = hex[(value & 0x0F)];
557 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
559 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
564 static u32 acpi_ec_gpe_handler(void *data)
566 if (acpi_ec_mode == EC_POLL)
567 return acpi_ec_gpe_poll_handler(data);
569 return acpi_ec_gpe_intr_handler(data);
571 static u32 acpi_ec_gpe_poll_handler(void *data)
573 acpi_status status = AE_OK;
574 struct acpi_ec *ec = (struct acpi_ec *)data;
577 return ACPI_INTERRUPT_NOT_HANDLED;
579 acpi_disable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
581 status = acpi_os_execute(OSL_EC_POLL_HANDLER, acpi_ec_gpe_query, ec);
584 return ACPI_INTERRUPT_HANDLED;
586 return ACPI_INTERRUPT_NOT_HANDLED;
588 static u32 acpi_ec_gpe_intr_handler(void *data)
590 acpi_status status = AE_OK;
592 struct acpi_ec *ec = (struct acpi_ec *)data;
595 return ACPI_INTERRUPT_NOT_HANDLED;
597 acpi_clear_gpe(NULL, ec->gpe_bit, ACPI_ISR);
598 value = acpi_ec_read_status(ec);
600 switch (ec->expect_event) {
601 case ACPI_EC_EVENT_OBF_1:
602 if (!(value & ACPI_EC_FLAG_OBF))
604 ec->expect_event = 0;
607 case ACPI_EC_EVENT_IBF_0:
608 if ((value & ACPI_EC_FLAG_IBF))
610 ec->expect_event = 0;
617 if (value & ACPI_EC_FLAG_SCI) {
618 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
619 acpi_ec_gpe_query, ec);
620 return status == AE_OK ?
621 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
623 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
624 return status == AE_OK ?
625 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
628 /* --------------------------------------------------------------------------
629 Address Space Management
630 -------------------------------------------------------------------------- */
633 acpi_ec_space_setup(acpi_handle region_handle,
634 u32 function, void *handler_context, void **return_context)
637 * The EC object is in the handler context and is needed
638 * when calling the acpi_ec_space_handler.
640 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
641 handler_context : NULL;
647 acpi_ec_space_handler(u32 function,
648 acpi_physical_address address,
650 acpi_integer * value,
651 void *handler_context, void *region_context)
654 struct acpi_ec *ec = NULL;
656 acpi_integer f_v = 0;
660 if ((address > 0xFF) || !value || !handler_context)
661 return AE_BAD_PARAMETER;
663 if (bit_width != 8 && acpi_strict) {
664 return AE_BAD_PARAMETER;
667 ec = (struct acpi_ec *)handler_context;
673 result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
676 result = acpi_ec_write(ec, (u8) address, (u8) temp);
686 if (function == ACPI_READ)
687 f_v |= temp << 8 * i;
688 if (function == ACPI_WRITE)
695 if (function == ACPI_READ) {
696 f_v |= temp << 8 * i;
703 return AE_BAD_PARAMETER;
716 /* --------------------------------------------------------------------------
718 -------------------------------------------------------------------------- */
720 static struct proc_dir_entry *acpi_ec_dir;
722 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
724 struct acpi_ec *ec = (struct acpi_ec *)seq->private;
730 seq_printf(seq, "gpe bit: 0x%02x\n",
732 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
733 (u32) ec->status_addr.address,
734 (u32) ec->data_addr.address);
735 seq_printf(seq, "use global lock: %s\n",
736 ec->global_lock ? "yes" : "no");
737 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
743 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
745 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
748 static struct file_operations acpi_ec_info_ops = {
749 .open = acpi_ec_info_open_fs,
752 .release = single_release,
753 .owner = THIS_MODULE,
756 static int acpi_ec_add_fs(struct acpi_device *device)
758 struct proc_dir_entry *entry = NULL;
761 if (!acpi_device_dir(device)) {
762 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
764 if (!acpi_device_dir(device))
768 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
769 acpi_device_dir(device));
773 entry->proc_fops = &acpi_ec_info_ops;
774 entry->data = acpi_driver_data(device);
775 entry->owner = THIS_MODULE;
781 static int acpi_ec_remove_fs(struct acpi_device *device)
784 if (acpi_device_dir(device)) {
785 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
786 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
787 acpi_device_dir(device) = NULL;
793 /* --------------------------------------------------------------------------
795 -------------------------------------------------------------------------- */
797 static int acpi_ec_add(struct acpi_device *device)
800 acpi_status status = AE_OK;
801 struct acpi_ec *ec = NULL;
807 ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
810 memset(ec, 0, sizeof(struct acpi_ec));
812 ec->handle = device->handle;
814 init_MUTEX(&ec->sem);
815 if (acpi_ec_mode == EC_INTR) {
816 atomic_set(&ec->leaving_burst, 1);
817 init_waitqueue_head(&ec->wait);
819 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
820 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
821 acpi_driver_data(device) = ec;
823 /* Use the global lock for all EC transactions? */
824 acpi_evaluate_integer(ec->handle, "_GLK", NULL,
827 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
828 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
830 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
832 &acpi_ec_space_handler);
834 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
835 &acpi_ec_gpe_handler);
840 /* Get GPE bit assignment (EC events). */
841 /* TODO: Add support for _GPE returning a package */
843 acpi_evaluate_integer(ec->handle, "_GPE", NULL,
845 if (ACPI_FAILURE(status)) {
846 ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit assignment"));
851 result = acpi_ec_add_fs(device);
855 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
856 acpi_device_name(device), acpi_device_bid(device),
869 static int acpi_ec_remove(struct acpi_device *device, int type)
871 struct acpi_ec *ec = NULL;
877 ec = acpi_driver_data(device);
879 acpi_ec_remove_fs(device);
887 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
889 struct acpi_ec *ec = (struct acpi_ec *)context;
890 struct acpi_generic_address *addr;
892 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
897 * The first address region returned is the data port, and
898 * the second address region returned is the status/command
901 if (ec->data_addr.register_bit_width == 0) {
902 addr = &ec->data_addr;
903 } else if (ec->command_addr.register_bit_width == 0) {
904 addr = &ec->command_addr;
906 return AE_CTRL_TERMINATE;
909 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
910 addr->register_bit_width = 8;
911 addr->register_bit_offset = 0;
912 addr->address = resource->data.io.minimum;
917 static int acpi_ec_start(struct acpi_device *device)
919 acpi_status status = AE_OK;
920 struct acpi_ec *ec = NULL;
926 ec = acpi_driver_data(device);
932 * Get I/O port addresses. Convert to GAS format.
934 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
935 acpi_ec_io_ports, ec);
936 if (ACPI_FAILURE(status)
937 || ec->command_addr.register_bit_width == 0) {
938 ACPI_EXCEPTION((AE_INFO, status,
939 "Error getting I/O port addresses"));
943 ec->status_addr = ec->command_addr;
945 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x",
947 (u32) ec->command_addr.address,
948 (u32) ec->data_addr.address));
951 * Install GPE handler
953 status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
954 ACPI_GPE_EDGE_TRIGGERED,
955 &acpi_ec_gpe_handler, ec);
956 if (ACPI_FAILURE(status)) {
959 acpi_set_gpe_type(NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
960 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
962 status = acpi_install_address_space_handler(ec->handle,
964 &acpi_ec_space_handler,
965 &acpi_ec_space_setup, ec);
966 if (ACPI_FAILURE(status)) {
967 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
968 &acpi_ec_gpe_handler);
975 static int acpi_ec_stop(struct acpi_device *device, int type)
977 acpi_status status = AE_OK;
978 struct acpi_ec *ec = NULL;
984 ec = acpi_driver_data(device);
986 status = acpi_remove_address_space_handler(ec->handle,
988 &acpi_ec_space_handler);
989 if (ACPI_FAILURE(status))
993 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
994 &acpi_ec_gpe_handler);
995 if (ACPI_FAILURE(status))
1001 static acpi_status __init
1002 acpi_fake_ecdt_callback(acpi_handle handle,
1003 u32 Level, void *context, void **retval)
1007 init_MUTEX(&ec_ecdt->sem);
1008 if (acpi_ec_mode == EC_INTR) {
1009 init_waitqueue_head(&ec_ecdt->wait);
1011 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1012 acpi_ec_io_ports, ec_ecdt);
1013 if (ACPI_FAILURE(status))
1015 ec_ecdt->status_addr = ec_ecdt->command_addr;
1018 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
1021 acpi_evaluate_integer(handle, "_GPE", NULL,
1023 if (ACPI_FAILURE(status))
1025 ec_ecdt->global_lock = TRUE;
1026 ec_ecdt->handle = handle;
1028 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02x, ports=0x%2x, 0x%2x",
1029 (u32) ec_ecdt->gpe_bit,
1030 (u32) ec_ecdt->command_addr.address,
1031 (u32) ec_ecdt->data_addr.address));
1033 return AE_CTRL_TERMINATE;
1037 * Some BIOS (such as some from Gateway laptops) access EC region very early
1038 * such as in BAT0._INI or EC._INI before an EC device is found and
1039 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1040 * required, but if EC regison is accessed early, it is required.
1041 * The routine tries to workaround the BIOS bug by pre-scan EC device
1042 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1043 * op region (since _REG isn't invoked yet). The assumption is true for
1044 * all systems found.
1046 static int __init acpi_ec_fake_ecdt(void)
1051 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
1053 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1058 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
1060 status = acpi_get_devices(ACPI_EC_HID,
1061 acpi_fake_ecdt_callback, NULL, NULL);
1062 if (ACPI_FAILURE(status)) {
1066 ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
1074 static int __init acpi_ec_get_real_ecdt(void)
1077 struct acpi_table_ecdt *ecdt_ptr;
1079 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1080 (struct acpi_table_header **)
1082 if (ACPI_FAILURE(status))
1085 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
1088 * Generate a temporary ec context to use until the namespace is scanned
1090 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1093 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
1095 init_MUTEX(&ec_ecdt->sem);
1096 if (acpi_ec_mode == EC_INTR) {
1097 init_waitqueue_head(&ec_ecdt->wait);
1099 ec_ecdt->command_addr = ecdt_ptr->ec_control;
1100 ec_ecdt->status_addr = ecdt_ptr->ec_control;
1101 ec_ecdt->data_addr = ecdt_ptr->ec_data;
1102 ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
1103 /* use the GL just to be safe */
1104 ec_ecdt->global_lock = TRUE;
1105 ec_ecdt->uid = ecdt_ptr->uid;
1108 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
1109 if (ACPI_FAILURE(status)) {
1115 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
1122 static int __initdata acpi_fake_ecdt_enabled;
1123 int __init acpi_ec_ecdt_probe(void)
1128 ret = acpi_ec_get_real_ecdt();
1129 /* Try to make a fake ECDT */
1130 if (ret && acpi_fake_ecdt_enabled) {
1131 ret = acpi_ec_fake_ecdt();
1138 * Install GPE handler
1140 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
1141 ACPI_GPE_EDGE_TRIGGERED,
1142 &acpi_ec_gpe_handler, ec_ecdt);
1143 if (ACPI_FAILURE(status)) {
1146 acpi_set_gpe_type(NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1147 acpi_enable_gpe(NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
1149 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
1151 &acpi_ec_space_handler,
1152 &acpi_ec_space_setup,
1154 if (ACPI_FAILURE(status)) {
1155 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
1156 &acpi_ec_gpe_handler);
1163 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
1170 static int __init acpi_ec_init(void)
1178 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1182 /* Now register the driver for the EC */
1183 result = acpi_bus_register_driver(&acpi_ec_driver);
1185 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1192 subsys_initcall(acpi_ec_init);
1194 /* EC driver currently not unloadable */
1196 static void __exit acpi_ec_exit(void)
1199 acpi_bus_unregister_driver(&acpi_ec_driver);
1201 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1207 static int __init acpi_fake_ecdt_setup(char *str)
1209 acpi_fake_ecdt_enabled = 1;
1213 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1214 static int __init acpi_ec_set_intr_mode(char *str)
1218 if (!get_option(&str, &intr))
1222 acpi_ec_mode = EC_INTR;
1224 acpi_ec_mode = EC_POLL;
1226 acpi_ec_driver.ops.add = acpi_ec_add;
1227 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n", intr ? "interrupt" : "polling"));
1232 __setup("ec_intr=", acpi_ec_set_intr_mode);