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_transaction(union acpi_ec *ec, u8 command,
126 const u8 *wdata, unsigned wdata_len,
127 u8 *rdata, unsigned rdata_len);
128 static int acpi_ec_intr_transaction(union acpi_ec *ec, u8 command,
129 const u8 *wdata, unsigned wdata_len,
130 u8 *rdata, unsigned rdata_len);
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)
211 ec->intr.expect_event = event;
215 case ACPI_EC_EVENT_IBE:
216 if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) {
217 ec->intr.expect_event = 0;
225 result = wait_event_timeout(ec->intr.wait,
226 !ec->intr.expect_event,
227 msecs_to_jiffies(ACPI_EC_DELAY));
229 ec->intr.expect_event = 0;
233 * Verify that the event in question has actually happened by
234 * querying EC status. Do the check even if operation timed-out
235 * to make sure that we did not miss interrupt.
238 case ACPI_EC_EVENT_OBF:
239 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
243 case ACPI_EC_EVENT_IBE:
244 if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
252 #ifdef ACPI_FUTURE_USAGE
254 * Note: samsung nv5000 doesn't work with ec burst mode.
255 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
257 int acpi_ec_enter_burst_mode(union acpi_ec *ec)
263 status = acpi_ec_read_status(ec);
264 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
265 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
268 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE,
269 &ec->common.command_addr);
270 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
271 acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
272 if (tmp != 0x90) { /* Burst ACK byte */
277 atomic_set(&ec->intr.leaving_burst, 0);
280 ACPI_EXCEPTION ((AE_INFO, status, "EC wait, burst mode");
284 int acpi_ec_leave_burst_mode(union acpi_ec *ec)
289 status = acpi_ec_read_status(ec);
290 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
291 status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
294 acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->common.command_addr);
295 acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
297 atomic_set(&ec->intr.leaving_burst, 1);
300 ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode");
303 #endif /* ACPI_FUTURE_USAGE */
305 static int acpi_ec_transaction(union acpi_ec *ec, u8 command,
306 const u8 *wdata, unsigned wdata_len,
307 u8 *rdata, unsigned rdata_len)
309 if (acpi_ec_poll_mode)
310 return acpi_ec_poll_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
312 return acpi_ec_intr_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
314 static int acpi_ec_read(union acpi_ec *ec, u8 address, u32 * data)
318 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ, &address, 1, &d, 1);
322 static int acpi_ec_write(union acpi_ec *ec, u8 address, u8 data)
324 u8 wdata[2] = { address, data };
325 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE, wdata, 2, NULL, 0);
328 static int acpi_ec_transaction_unlocked(union acpi_ec *ec, u8 command,
329 const u8 *wdata, unsigned wdata_len,
330 u8 *rdata, unsigned rdata_len)
334 acpi_hw_low_level_write(8, command, &ec->common.command_addr);
336 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
340 for (; wdata_len > 0; wdata_len --) {
342 acpi_hw_low_level_write(8, *(wdata++), &ec->common.data_addr);
344 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
350 for (; rdata_len > 0; rdata_len --) {
353 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
357 acpi_hw_low_level_read(8, &d, &ec->common.data_addr);
364 static int acpi_ec_poll_transaction(union acpi_ec *ec, u8 command,
365 const u8 *wdata, unsigned wdata_len,
366 u8 *rdata, unsigned rdata_len)
368 acpi_status status = AE_OK;
372 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
376 memset(rdata, 0, rdata_len);
378 if (ec->common.global_lock) {
379 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
380 if (ACPI_FAILURE(status))
384 if (down_interruptible(&ec->poll.sem)) {
385 result = -ERESTARTSYS;
389 result = acpi_ec_transaction_unlocked(ec, command,
395 if (ec->common.global_lock)
396 acpi_release_global_lock(glk);
401 static int acpi_ec_intr_transaction(union acpi_ec *ec, u8 command,
402 const u8 *wdata, unsigned wdata_len,
403 u8 *rdata, unsigned rdata_len)
408 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
412 memset(rdata, 0, rdata_len);
414 if (ec->common.global_lock) {
415 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
416 if (ACPI_FAILURE(status))
420 WARN_ON(in_interrupt());
423 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
425 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
429 status = acpi_ec_transaction_unlocked(ec, command,
436 if (ec->common.global_lock)
437 acpi_release_global_lock(glk);
443 * Externally callable EC access functions. For now, assume 1 EC only
445 int ec_read(u8 addr, u8 * val)
454 ec = acpi_driver_data(first_ec);
456 err = acpi_ec_read(ec, addr, &temp_data);
465 EXPORT_SYMBOL(ec_read);
467 int ec_write(u8 addr, u8 val)
475 ec = acpi_driver_data(first_ec);
477 err = acpi_ec_write(ec, addr, val);
482 EXPORT_SYMBOL(ec_write);
484 extern int ec_transaction(u8 command,
485 const u8 *wdata, unsigned wdata_len,
486 u8 *rdata, unsigned rdata_len)
493 ec = acpi_driver_data(first_ec);
495 return acpi_ec_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
498 EXPORT_SYMBOL(ec_transaction);
500 static int acpi_ec_query(union acpi_ec *ec, u32 * data) {
508 * Query the EC to find out which _Qxx method we need to evaluate.
509 * Note that successful completion of the query causes the ACPI_EC_SCI
510 * bit to be cleared (and thus clearing the interrupt source).
513 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
524 /* --------------------------------------------------------------------------
526 -------------------------------------------------------------------------- */
528 union acpi_ec_query_data {
533 static void acpi_ec_gpe_query(void *ec_cxt)
535 if (acpi_ec_poll_mode)
536 acpi_ec_gpe_poll_query(ec_cxt);
538 acpi_ec_gpe_intr_query(ec_cxt);
541 static void acpi_ec_gpe_poll_query(void *ec_cxt)
543 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
545 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
546 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
547 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
554 if (down_interruptible (&ec->poll.sem)) {
557 acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
560 /* TBD: Implement asynch events!
561 * NOTE: All we care about are EC-SCI's. Other EC events are
562 * handled via polling (yuck!). This is because some systems
563 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
564 * a purely interrupt-driven approach (grumble, grumble).
566 if (!(value & ACPI_EC_FLAG_SCI))
569 if (acpi_ec_query(ec, &value))
572 object_name[2] = hex[((value >> 4) & 0x0F)];
573 object_name[3] = hex[(value & 0x0F)];
575 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
577 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
580 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
582 static void acpi_ec_gpe_intr_query(void *ec_cxt)
584 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
586 int result = -ENODATA;
587 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
588 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
589 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
593 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
594 result = acpi_ec_query(ec, &value);
599 object_name[2] = hex[((value >> 4) & 0x0F)];
600 object_name[3] = hex[(value & 0x0F)];
602 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
604 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
606 atomic_dec(&ec->intr.pending_gpe);
610 static u32 acpi_ec_gpe_handler(void *data)
612 if (acpi_ec_poll_mode)
613 return acpi_ec_gpe_poll_handler(data);
615 return acpi_ec_gpe_intr_handler(data);
617 static u32 acpi_ec_gpe_poll_handler(void *data)
619 acpi_status status = AE_OK;
620 union acpi_ec *ec = (union acpi_ec *)data;
623 return ACPI_INTERRUPT_NOT_HANDLED;
625 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
627 status = acpi_os_execute(OSL_EC_POLL_HANDLER, acpi_ec_gpe_query, ec);
630 return ACPI_INTERRUPT_HANDLED;
632 return ACPI_INTERRUPT_NOT_HANDLED;
634 static u32 acpi_ec_gpe_intr_handler(void *data)
636 acpi_status status = AE_OK;
638 union acpi_ec *ec = (union acpi_ec *)data;
641 return ACPI_INTERRUPT_NOT_HANDLED;
643 acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
644 value = acpi_ec_read_status(ec);
646 switch (ec->intr.expect_event) {
647 case ACPI_EC_EVENT_OBF:
648 if (!(value & ACPI_EC_FLAG_OBF))
650 ec->intr.expect_event = 0;
651 wake_up(&ec->intr.wait);
653 case ACPI_EC_EVENT_IBE:
654 if ((value & ACPI_EC_FLAG_IBF))
656 ec->intr.expect_event = 0;
657 wake_up(&ec->intr.wait);
663 if (value & ACPI_EC_FLAG_SCI) {
664 atomic_add(1, &ec->intr.pending_gpe);
665 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
666 acpi_ec_gpe_query, ec);
667 return status == AE_OK ?
668 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
670 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
671 return status == AE_OK ?
672 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
675 /* --------------------------------------------------------------------------
676 Address Space Management
677 -------------------------------------------------------------------------- */
680 acpi_ec_space_setup(acpi_handle region_handle,
681 u32 function, void *handler_context, void **return_context)
684 * The EC object is in the handler context and is needed
685 * when calling the acpi_ec_space_handler.
687 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
688 handler_context : NULL;
694 acpi_ec_space_handler(u32 function,
695 acpi_physical_address address,
697 acpi_integer * value,
698 void *handler_context, void *region_context)
701 union acpi_ec *ec = NULL;
703 acpi_integer f_v = 0;
707 if ((address > 0xFF) || !value || !handler_context)
708 return AE_BAD_PARAMETER;
710 if (bit_width != 8 && acpi_strict) {
711 printk(KERN_WARNING PREFIX
712 "acpi_ec_space_handler: bit_width should be 8\n");
713 return AE_BAD_PARAMETER;
716 ec = (union acpi_ec *)handler_context;
722 result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
725 result = acpi_ec_write(ec, (u8) address, (u8) temp);
735 if (function == ACPI_READ)
736 f_v |= temp << 8 * i;
737 if (function == ACPI_WRITE)
744 if (function == ACPI_READ) {
745 f_v |= temp << 8 * i;
752 return AE_BAD_PARAMETER;
765 /* --------------------------------------------------------------------------
767 -------------------------------------------------------------------------- */
769 static struct proc_dir_entry *acpi_ec_dir;
771 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
773 union acpi_ec *ec = (union acpi_ec *)seq->private;
779 seq_printf(seq, "gpe bit: 0x%02x\n",
780 (u32) ec->common.gpe_bit);
781 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
782 (u32) ec->common.status_addr.address,
783 (u32) ec->common.data_addr.address);
784 seq_printf(seq, "use global lock: %s\n",
785 ec->common.global_lock ? "yes" : "no");
786 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
792 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
794 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
797 static const struct file_operations acpi_ec_info_ops = {
798 .open = acpi_ec_info_open_fs,
801 .release = single_release,
802 .owner = THIS_MODULE,
805 static int acpi_ec_add_fs(struct acpi_device *device)
807 struct proc_dir_entry *entry = NULL;
810 if (!acpi_device_dir(device)) {
811 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
813 if (!acpi_device_dir(device))
817 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
818 acpi_device_dir(device));
822 entry->proc_fops = &acpi_ec_info_ops;
823 entry->data = acpi_driver_data(device);
824 entry->owner = THIS_MODULE;
830 static int acpi_ec_remove_fs(struct acpi_device *device)
833 if (acpi_device_dir(device)) {
834 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
835 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
836 acpi_device_dir(device) = NULL;
842 /* --------------------------------------------------------------------------
844 -------------------------------------------------------------------------- */
846 static int acpi_ec_poll_add(struct acpi_device *device)
849 acpi_status status = AE_OK;
850 union acpi_ec *ec = NULL;
856 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
859 memset(ec, 0, sizeof(union acpi_ec));
861 ec->common.handle = device->handle;
863 init_MUTEX(&ec->poll.sem);
864 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
865 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
866 acpi_driver_data(device) = ec;
868 /* Use the global lock for all EC transactions? */
869 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
870 &ec->common.global_lock);
872 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
873 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
875 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
877 &acpi_ec_space_handler);
879 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
880 &acpi_ec_gpe_handler);
885 /* Get GPE bit assignment (EC events). */
886 /* TODO: Add support for _GPE returning a package */
888 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
889 &ec->common.gpe_bit);
890 if (ACPI_FAILURE(status)) {
891 ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit"));
896 result = acpi_ec_add_fs(device);
900 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) polling mode.\n",
901 acpi_device_name(device), acpi_device_bid(device),
902 (u32) ec->common.gpe_bit);
913 static int acpi_ec_intr_add(struct acpi_device *device)
916 acpi_status status = AE_OK;
917 union acpi_ec *ec = NULL;
923 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
926 memset(ec, 0, sizeof(union acpi_ec));
928 ec->common.handle = device->handle;
930 atomic_set(&ec->intr.pending_gpe, 0);
931 atomic_set(&ec->intr.leaving_burst, 1);
932 init_MUTEX(&ec->intr.sem);
933 init_waitqueue_head(&ec->intr.wait);
934 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
935 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
936 acpi_driver_data(device) = ec;
938 /* Use the global lock for all EC transactions? */
939 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
940 &ec->common.global_lock);
942 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
943 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
945 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
947 &acpi_ec_space_handler);
949 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
950 &acpi_ec_gpe_handler);
955 /* Get GPE bit assignment (EC events). */
956 /* TODO: Add support for _GPE returning a package */
958 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
959 &ec->common.gpe_bit);
960 if (ACPI_FAILURE(status)) {
961 printk(KERN_ERR PREFIX "Obtaining GPE bit assignment\n");
966 result = acpi_ec_add_fs(device);
970 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) interrupt mode.\n",
971 acpi_device_name(device), acpi_device_bid(device),
972 (u32) ec->common.gpe_bit);
984 static int acpi_ec_remove(struct acpi_device *device, int type)
986 union acpi_ec *ec = NULL;
992 ec = acpi_driver_data(device);
994 acpi_ec_remove_fs(device);
1002 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
1004 union acpi_ec *ec = (union acpi_ec *)context;
1005 struct acpi_generic_address *addr;
1007 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
1012 * The first address region returned is the data port, and
1013 * the second address region returned is the status/command
1016 if (ec->common.data_addr.register_bit_width == 0) {
1017 addr = &ec->common.data_addr;
1018 } else if (ec->common.command_addr.register_bit_width == 0) {
1019 addr = &ec->common.command_addr;
1021 return AE_CTRL_TERMINATE;
1024 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1025 addr->register_bit_width = 8;
1026 addr->register_bit_offset = 0;
1027 addr->address = resource->data.io.minimum;
1032 static int acpi_ec_start(struct acpi_device *device)
1034 acpi_status status = AE_OK;
1035 union acpi_ec *ec = NULL;
1041 ec = acpi_driver_data(device);
1047 * Get I/O port addresses. Convert to GAS format.
1049 status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
1050 acpi_ec_io_ports, ec);
1051 if (ACPI_FAILURE(status)
1052 || ec->common.command_addr.register_bit_width == 0) {
1053 printk(KERN_ERR PREFIX "Error getting I/O port addresses\n");
1057 ec->common.status_addr = ec->common.command_addr;
1059 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
1060 (u32) ec->common.gpe_bit,
1061 (u32) ec->common.command_addr.address,
1062 (u32) ec->common.data_addr.address));
1065 * Install GPE handler
1067 status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
1068 ACPI_GPE_EDGE_TRIGGERED,
1069 &acpi_ec_gpe_handler, ec);
1070 if (ACPI_FAILURE(status)) {
1073 acpi_set_gpe_type(NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1074 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
1076 status = acpi_install_address_space_handler(ec->common.handle,
1078 &acpi_ec_space_handler,
1079 &acpi_ec_space_setup, ec);
1080 if (ACPI_FAILURE(status)) {
1081 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1082 &acpi_ec_gpe_handler);
1089 static int acpi_ec_stop(struct acpi_device *device, int type)
1091 acpi_status status = AE_OK;
1092 union acpi_ec *ec = NULL;
1098 ec = acpi_driver_data(device);
1100 status = acpi_remove_address_space_handler(ec->common.handle,
1102 &acpi_ec_space_handler);
1103 if (ACPI_FAILURE(status))
1107 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1108 &acpi_ec_gpe_handler);
1109 if (ACPI_FAILURE(status))
1115 static acpi_status __init
1116 acpi_fake_ecdt_callback(acpi_handle handle,
1117 u32 Level, void *context, void **retval)
1120 if (acpi_ec_poll_mode)
1121 return acpi_fake_ecdt_poll_callback(handle,
1122 Level, context, retval);
1124 return acpi_fake_ecdt_intr_callback(handle,
1125 Level, context, retval);
1128 static acpi_status __init
1129 acpi_fake_ecdt_poll_callback(acpi_handle handle,
1130 u32 Level, void *context, void **retval)
1134 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1135 acpi_ec_io_ports, ec_ecdt);
1136 if (ACPI_FAILURE(status))
1138 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1140 ec_ecdt->common.uid = -1;
1141 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1144 acpi_evaluate_integer(handle, "_GPE", NULL,
1145 &ec_ecdt->common.gpe_bit);
1146 if (ACPI_FAILURE(status))
1148 init_MUTEX(&ec_ecdt->poll.sem);
1149 ec_ecdt->common.global_lock = TRUE;
1150 ec_ecdt->common.handle = handle;
1152 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1153 (u32) ec_ecdt->common.gpe_bit,
1154 (u32) ec_ecdt->common.command_addr.address,
1155 (u32) ec_ecdt->common.data_addr.address);
1157 return AE_CTRL_TERMINATE;
1160 static acpi_status __init
1161 acpi_fake_ecdt_intr_callback(acpi_handle handle,
1162 u32 Level, void *context, void **retval)
1166 init_MUTEX(&ec_ecdt->intr.sem);
1167 init_waitqueue_head(&ec_ecdt->intr.wait);
1168 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1169 acpi_ec_io_ports, ec_ecdt);
1170 if (ACPI_FAILURE(status))
1172 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1174 ec_ecdt->common.uid = -1;
1175 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1178 acpi_evaluate_integer(handle, "_GPE", NULL,
1179 &ec_ecdt->common.gpe_bit);
1180 if (ACPI_FAILURE(status))
1182 ec_ecdt->common.global_lock = TRUE;
1183 ec_ecdt->common.handle = handle;
1185 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1186 (u32) ec_ecdt->common.gpe_bit,
1187 (u32) ec_ecdt->common.command_addr.address,
1188 (u32) ec_ecdt->common.data_addr.address);
1190 return AE_CTRL_TERMINATE;
1194 * Some BIOS (such as some from Gateway laptops) access EC region very early
1195 * such as in BAT0._INI or EC._INI before an EC device is found and
1196 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1197 * required, but if EC regison is accessed early, it is required.
1198 * The routine tries to workaround the BIOS bug by pre-scan EC device
1199 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1200 * op region (since _REG isn't invoked yet). The assumption is true for
1201 * all systems found.
1203 static int __init acpi_ec_fake_ecdt(void)
1208 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1210 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1215 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1217 status = acpi_get_devices(ACPI_EC_HID,
1218 acpi_fake_ecdt_callback, NULL, NULL);
1219 if (ACPI_FAILURE(status)) {
1227 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1231 static int __init acpi_ec_get_real_ecdt(void)
1233 if (acpi_ec_poll_mode)
1234 return acpi_ec_poll_get_real_ecdt();
1236 return acpi_ec_intr_get_real_ecdt();
1239 static int __init acpi_ec_poll_get_real_ecdt(void)
1242 struct acpi_table_ecdt *ecdt_ptr;
1244 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1245 (struct acpi_table_header **)
1247 if (ACPI_FAILURE(status))
1250 printk(KERN_INFO PREFIX "Found ECDT\n");
1253 * Generate a temporary ec context to use until the namespace is scanned
1255 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1258 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1260 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1261 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1262 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1263 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1264 init_MUTEX(&ec_ecdt->poll.sem);
1265 /* use the GL just to be safe */
1266 ec_ecdt->common.global_lock = TRUE;
1267 ec_ecdt->common.uid = ecdt_ptr->uid;
1270 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1271 if (ACPI_FAILURE(status)) {
1277 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1284 static int __init acpi_ec_intr_get_real_ecdt(void)
1287 struct acpi_table_ecdt *ecdt_ptr;
1289 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1290 (struct acpi_table_header **)
1292 if (ACPI_FAILURE(status))
1295 printk(KERN_INFO PREFIX "Found ECDT\n");
1298 * Generate a temporary ec context to use until the namespace is scanned
1300 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1303 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1305 init_MUTEX(&ec_ecdt->intr.sem);
1306 init_waitqueue_head(&ec_ecdt->intr.wait);
1307 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1308 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1309 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1310 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1311 /* use the GL just to be safe */
1312 ec_ecdt->common.global_lock = TRUE;
1313 ec_ecdt->common.uid = ecdt_ptr->uid;
1316 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1317 if (ACPI_FAILURE(status)) {
1323 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1330 static int __initdata acpi_fake_ecdt_enabled;
1331 int __init acpi_ec_ecdt_probe(void)
1336 ret = acpi_ec_get_real_ecdt();
1337 /* Try to make a fake ECDT */
1338 if (ret && acpi_fake_ecdt_enabled) {
1339 ret = acpi_ec_fake_ecdt();
1346 * Install GPE handler
1348 status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1349 ACPI_GPE_EDGE_TRIGGERED,
1350 &acpi_ec_gpe_handler, ec_ecdt);
1351 if (ACPI_FAILURE(status)) {
1354 acpi_set_gpe_type(NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1355 acpi_enable_gpe(NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
1357 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
1359 &acpi_ec_space_handler,
1360 &acpi_ec_space_setup,
1362 if (ACPI_FAILURE(status)) {
1363 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1364 &acpi_ec_gpe_handler);
1371 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1378 static int __init acpi_ec_init(void)
1386 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1390 /* Now register the driver for the EC */
1391 result = acpi_bus_register_driver(&acpi_ec_driver);
1393 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1400 subsys_initcall(acpi_ec_init);
1402 /* EC driver currently not unloadable */
1404 static void __exit acpi_ec_exit(void)
1407 acpi_bus_unregister_driver(&acpi_ec_driver);
1409 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1415 static int __init acpi_fake_ecdt_setup(char *str)
1417 acpi_fake_ecdt_enabled = 1;
1421 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1422 static int __init acpi_ec_set_intr_mode(char *str)
1426 if (!get_option(&str, &intr))
1430 acpi_ec_poll_mode = EC_INTR;
1431 acpi_ec_driver.ops.add = acpi_ec_intr_add;
1433 acpi_ec_poll_mode = EC_POLL;
1434 acpi_ec_driver.ops.add = acpi_ec_poll_add;
1436 printk(KERN_INFO PREFIX "EC %s mode.\n", intr ? "interrupt" : "polling");
1440 __setup("ec_intr=", acpi_ec_set_intr_mode);