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;
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, u8 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_check_status(u32 status, u8 event) {
167 case ACPI_EC_EVENT_OBF:
168 if (status & ACPI_EC_FLAG_OBF)
170 case ACPI_EC_EVENT_IBE:
171 if (!(status & ACPI_EC_FLAG_IBF))
180 static int acpi_ec_wait(union acpi_ec *ec, u8 event)
182 if (acpi_ec_poll_mode)
183 return acpi_ec_poll_wait(ec, event);
185 return acpi_ec_intr_wait(ec, event);
188 static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event)
190 u32 acpi_ec_status = 0;
191 u32 i = ACPI_EC_UDELAY_COUNT;
196 /* Poll the EC status register waiting for the event to occur. */
198 case ACPI_EC_EVENT_OBF:
200 acpi_hw_low_level_read(8, &acpi_ec_status,
201 &ec->common.status_addr);
202 if (acpi_ec_status & ACPI_EC_FLAG_OBF)
204 udelay(ACPI_EC_UDELAY);
207 case ACPI_EC_EVENT_IBE:
209 acpi_hw_low_level_read(8, &acpi_ec_status,
210 &ec->common.status_addr);
211 if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
213 udelay(ACPI_EC_UDELAY);
223 static int acpi_ec_intr_wait(union acpi_ec *ec, u8 event)
227 ec->intr.expect_event = event;
229 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
230 ec->intr.expect_event = 0;
234 time_left = wait_event_timeout(ec->intr.wait, !ec->intr.expect_event,
235 msecs_to_jiffies(ACPI_EC_DELAY));
237 ec->intr.expect_event = 0;
238 if (time_left <= 0) {
239 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
249 #ifdef ACPI_FUTURE_USAGE
251 * Note: samsung nv5000 doesn't work with ec burst mode.
252 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
254 int acpi_ec_enter_burst_mode(union acpi_ec *ec)
260 status = acpi_ec_read_status(ec);
261 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
262 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
265 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE,
266 &ec->common.command_addr);
267 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
268 acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
269 if (tmp != 0x90) { /* Burst ACK byte */
274 atomic_set(&ec->intr.leaving_burst, 0);
277 ACPI_EXCEPTION ((AE_INFO, status, "EC wait, burst mode");
281 int acpi_ec_leave_burst_mode(union acpi_ec *ec)
286 status = acpi_ec_read_status(ec);
287 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
288 status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
291 acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->common.command_addr);
292 acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
294 atomic_set(&ec->intr.leaving_burst, 1);
297 ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode");
300 #endif /* ACPI_FUTURE_USAGE */
302 static int acpi_ec_transaction(union acpi_ec *ec, u8 command,
303 const u8 *wdata, unsigned wdata_len,
304 u8 *rdata, unsigned rdata_len)
306 if (acpi_ec_poll_mode)
307 return acpi_ec_poll_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
309 return acpi_ec_intr_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
311 static int acpi_ec_read(union acpi_ec *ec, u8 address, u32 * data)
315 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ, &address, 1, &d, 1);
319 static int acpi_ec_write(union acpi_ec *ec, u8 address, u8 data)
321 u8 wdata[2] = { address, data };
322 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE, wdata, 2, NULL, 0);
325 static int acpi_ec_transaction_unlocked(union acpi_ec *ec, u8 command,
326 const u8 *wdata, unsigned wdata_len,
327 u8 *rdata, unsigned rdata_len)
331 acpi_hw_low_level_write(8, command, &ec->common.command_addr);
333 for (; wdata_len > 0; wdata_len --) {
334 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
338 acpi_hw_low_level_write(8, *(wdata++), &ec->common.data_addr);
341 if (command == ACPI_EC_COMMAND_WRITE) {
342 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
347 for (; rdata_len > 0; rdata_len --) {
350 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
354 acpi_hw_low_level_read(8, &d, &ec->common.data_addr);
361 static int acpi_ec_poll_transaction(union acpi_ec *ec, u8 command,
362 const u8 *wdata, unsigned wdata_len,
363 u8 *rdata, unsigned rdata_len)
365 acpi_status status = AE_OK;
369 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
373 memset(rdata, 0, rdata_len);
375 if (ec->common.global_lock) {
376 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
377 if (ACPI_FAILURE(status))
381 if (down_interruptible(&ec->poll.sem)) {
382 result = -ERESTARTSYS;
386 result = acpi_ec_transaction_unlocked(ec, command,
392 if (ec->common.global_lock)
393 acpi_release_global_lock(glk);
398 static int acpi_ec_intr_transaction(union acpi_ec *ec, u8 command,
399 const u8 *wdata, unsigned wdata_len,
400 u8 *rdata, unsigned rdata_len)
405 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
409 memset(rdata, 0, rdata_len);
411 if (ec->common.global_lock) {
412 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
413 if (ACPI_FAILURE(status))
417 WARN_ON(in_interrupt());
420 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
422 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
426 status = acpi_ec_transaction_unlocked(ec, command,
433 if (ec->common.global_lock)
434 acpi_release_global_lock(glk);
440 * Externally callable EC access functions. For now, assume 1 EC only
442 int ec_read(u8 addr, u8 * val)
451 ec = acpi_driver_data(first_ec);
453 err = acpi_ec_read(ec, addr, &temp_data);
462 EXPORT_SYMBOL(ec_read);
464 int ec_write(u8 addr, u8 val)
472 ec = acpi_driver_data(first_ec);
474 err = acpi_ec_write(ec, addr, val);
479 EXPORT_SYMBOL(ec_write);
481 extern int ec_transaction(u8 command,
482 const u8 *wdata, unsigned wdata_len,
483 u8 *rdata, unsigned rdata_len)
490 ec = acpi_driver_data(first_ec);
492 return acpi_ec_transaction(ec, command, wdata, wdata_len, rdata, rdata_len);
495 EXPORT_SYMBOL(ec_transaction);
497 static int acpi_ec_query(union acpi_ec *ec, u32 * data) {
505 * Query the EC to find out which _Qxx method we need to evaluate.
506 * Note that successful completion of the query causes the ACPI_EC_SCI
507 * bit to be cleared (and thus clearing the interrupt source).
510 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
521 /* --------------------------------------------------------------------------
523 -------------------------------------------------------------------------- */
525 union acpi_ec_query_data {
530 static void acpi_ec_gpe_query(void *ec_cxt)
532 if (acpi_ec_poll_mode)
533 acpi_ec_gpe_poll_query(ec_cxt);
535 acpi_ec_gpe_intr_query(ec_cxt);
538 static void acpi_ec_gpe_poll_query(void *ec_cxt)
540 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
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'
551 if (down_interruptible (&ec->poll.sem)) {
554 acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
557 /* TBD: Implement asynch events!
558 * NOTE: All we care about are EC-SCI's. Other EC events are
559 * handled via polling (yuck!). This is because some systems
560 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
561 * a purely interrupt-driven approach (grumble, grumble).
563 if (!(value & ACPI_EC_FLAG_SCI))
566 if (acpi_ec_query(ec, &value))
569 object_name[2] = hex[((value >> 4) & 0x0F)];
570 object_name[3] = hex[(value & 0x0F)];
572 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
574 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
577 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
579 static void acpi_ec_gpe_intr_query(void *ec_cxt)
581 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
583 int result = -ENODATA;
584 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
585 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
586 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
590 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
591 result = acpi_ec_query(ec, &value);
596 object_name[2] = hex[((value >> 4) & 0x0F)];
597 object_name[3] = hex[(value & 0x0F)];
599 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
601 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
603 atomic_dec(&ec->intr.pending_gpe);
607 static u32 acpi_ec_gpe_handler(void *data)
609 if (acpi_ec_poll_mode)
610 return acpi_ec_gpe_poll_handler(data);
612 return acpi_ec_gpe_intr_handler(data);
614 static u32 acpi_ec_gpe_poll_handler(void *data)
616 acpi_status status = AE_OK;
617 union acpi_ec *ec = (union acpi_ec *)data;
620 return ACPI_INTERRUPT_NOT_HANDLED;
622 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
624 status = acpi_os_execute(OSL_EC_POLL_HANDLER, acpi_ec_gpe_query, ec);
627 return ACPI_INTERRUPT_HANDLED;
629 return ACPI_INTERRUPT_NOT_HANDLED;
631 static u32 acpi_ec_gpe_intr_handler(void *data)
633 acpi_status status = AE_OK;
635 union acpi_ec *ec = (union acpi_ec *)data;
638 return ACPI_INTERRUPT_NOT_HANDLED;
640 acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
641 value = acpi_ec_read_status(ec);
643 switch (ec->intr.expect_event) {
644 case ACPI_EC_EVENT_OBF:
645 if (!(value & ACPI_EC_FLAG_OBF))
647 ec->intr.expect_event = 0;
648 wake_up(&ec->intr.wait);
650 case ACPI_EC_EVENT_IBE:
651 if ((value & ACPI_EC_FLAG_IBF))
653 ec->intr.expect_event = 0;
654 wake_up(&ec->intr.wait);
660 if (value & ACPI_EC_FLAG_SCI) {
661 atomic_add(1, &ec->intr.pending_gpe);
662 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
663 acpi_ec_gpe_query, ec);
664 return status == AE_OK ?
665 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
667 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
668 return status == AE_OK ?
669 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
672 /* --------------------------------------------------------------------------
673 Address Space Management
674 -------------------------------------------------------------------------- */
677 acpi_ec_space_setup(acpi_handle region_handle,
678 u32 function, void *handler_context, void **return_context)
681 * The EC object is in the handler context and is needed
682 * when calling the acpi_ec_space_handler.
684 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
685 handler_context : NULL;
691 acpi_ec_space_handler(u32 function,
692 acpi_physical_address address,
694 acpi_integer * value,
695 void *handler_context, void *region_context)
698 union acpi_ec *ec = NULL;
700 acpi_integer f_v = 0;
704 if ((address > 0xFF) || !value || !handler_context)
705 return AE_BAD_PARAMETER;
707 if (bit_width != 8 && acpi_strict) {
708 printk(KERN_WARNING PREFIX
709 "acpi_ec_space_handler: bit_width should be 8\n");
710 return AE_BAD_PARAMETER;
713 ec = (union acpi_ec *)handler_context;
719 result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
722 result = acpi_ec_write(ec, (u8) address, (u8) temp);
732 if (function == ACPI_READ)
733 f_v |= temp << 8 * i;
734 if (function == ACPI_WRITE)
741 if (function == ACPI_READ) {
742 f_v |= temp << 8 * i;
749 return AE_BAD_PARAMETER;
762 /* --------------------------------------------------------------------------
764 -------------------------------------------------------------------------- */
766 static struct proc_dir_entry *acpi_ec_dir;
768 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
770 union acpi_ec *ec = (union acpi_ec *)seq->private;
776 seq_printf(seq, "gpe bit: 0x%02x\n",
777 (u32) ec->common.gpe_bit);
778 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
779 (u32) ec->common.status_addr.address,
780 (u32) ec->common.data_addr.address);
781 seq_printf(seq, "use global lock: %s\n",
782 ec->common.global_lock ? "yes" : "no");
783 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
789 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
791 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
794 static const struct file_operations acpi_ec_info_ops = {
795 .open = acpi_ec_info_open_fs,
798 .release = single_release,
799 .owner = THIS_MODULE,
802 static int acpi_ec_add_fs(struct acpi_device *device)
804 struct proc_dir_entry *entry = NULL;
807 if (!acpi_device_dir(device)) {
808 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
810 if (!acpi_device_dir(device))
814 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
815 acpi_device_dir(device));
819 entry->proc_fops = &acpi_ec_info_ops;
820 entry->data = acpi_driver_data(device);
821 entry->owner = THIS_MODULE;
827 static int acpi_ec_remove_fs(struct acpi_device *device)
830 if (acpi_device_dir(device)) {
831 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
832 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
833 acpi_device_dir(device) = NULL;
839 /* --------------------------------------------------------------------------
841 -------------------------------------------------------------------------- */
843 static int acpi_ec_poll_add(struct acpi_device *device)
846 acpi_status status = AE_OK;
847 union acpi_ec *ec = NULL;
853 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
856 memset(ec, 0, sizeof(union acpi_ec));
858 ec->common.handle = device->handle;
860 init_MUTEX(&ec->poll.sem);
861 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
862 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
863 acpi_driver_data(device) = ec;
865 /* Use the global lock for all EC transactions? */
866 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
867 &ec->common.global_lock);
869 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
870 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
872 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
874 &acpi_ec_space_handler);
876 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
877 &acpi_ec_gpe_handler);
882 /* Get GPE bit assignment (EC events). */
883 /* TODO: Add support for _GPE returning a package */
885 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
886 &ec->common.gpe_bit);
887 if (ACPI_FAILURE(status)) {
888 ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit"));
893 result = acpi_ec_add_fs(device);
897 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) polling mode.\n",
898 acpi_device_name(device), acpi_device_bid(device),
899 (u32) ec->common.gpe_bit);
910 static int acpi_ec_intr_add(struct acpi_device *device)
913 acpi_status status = AE_OK;
914 union acpi_ec *ec = NULL;
920 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
923 memset(ec, 0, sizeof(union acpi_ec));
925 ec->common.handle = device->handle;
927 atomic_set(&ec->intr.pending_gpe, 0);
928 atomic_set(&ec->intr.leaving_burst, 1);
929 init_MUTEX(&ec->intr.sem);
930 init_waitqueue_head(&ec->intr.wait);
931 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
932 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
933 acpi_driver_data(device) = ec;
935 /* Use the global lock for all EC transactions? */
936 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
937 &ec->common.global_lock);
939 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
940 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
942 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
944 &acpi_ec_space_handler);
946 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
947 &acpi_ec_gpe_handler);
952 /* Get GPE bit assignment (EC events). */
953 /* TODO: Add support for _GPE returning a package */
955 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
956 &ec->common.gpe_bit);
957 if (ACPI_FAILURE(status)) {
958 printk(KERN_ERR PREFIX "Obtaining GPE bit assignment\n");
963 result = acpi_ec_add_fs(device);
967 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) interrupt mode.\n",
968 acpi_device_name(device), acpi_device_bid(device),
969 (u32) ec->common.gpe_bit);
981 static int acpi_ec_remove(struct acpi_device *device, int type)
983 union acpi_ec *ec = NULL;
989 ec = acpi_driver_data(device);
991 acpi_ec_remove_fs(device);
999 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
1001 union acpi_ec *ec = (union acpi_ec *)context;
1002 struct acpi_generic_address *addr;
1004 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
1009 * The first address region returned is the data port, and
1010 * the second address region returned is the status/command
1013 if (ec->common.data_addr.register_bit_width == 0) {
1014 addr = &ec->common.data_addr;
1015 } else if (ec->common.command_addr.register_bit_width == 0) {
1016 addr = &ec->common.command_addr;
1018 return AE_CTRL_TERMINATE;
1021 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1022 addr->register_bit_width = 8;
1023 addr->register_bit_offset = 0;
1024 addr->address = resource->data.io.minimum;
1029 static int acpi_ec_start(struct acpi_device *device)
1031 acpi_status status = AE_OK;
1032 union acpi_ec *ec = NULL;
1038 ec = acpi_driver_data(device);
1044 * Get I/O port addresses. Convert to GAS format.
1046 status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
1047 acpi_ec_io_ports, ec);
1048 if (ACPI_FAILURE(status)
1049 || ec->common.command_addr.register_bit_width == 0) {
1050 printk(KERN_ERR PREFIX "Error getting I/O port addresses\n");
1054 ec->common.status_addr = ec->common.command_addr;
1056 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
1057 (u32) ec->common.gpe_bit,
1058 (u32) ec->common.command_addr.address,
1059 (u32) ec->common.data_addr.address));
1062 * Install GPE handler
1064 status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
1065 ACPI_GPE_EDGE_TRIGGERED,
1066 &acpi_ec_gpe_handler, ec);
1067 if (ACPI_FAILURE(status)) {
1070 acpi_set_gpe_type(NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1071 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
1073 status = acpi_install_address_space_handler(ec->common.handle,
1075 &acpi_ec_space_handler,
1076 &acpi_ec_space_setup, ec);
1077 if (ACPI_FAILURE(status)) {
1078 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1079 &acpi_ec_gpe_handler);
1086 static int acpi_ec_stop(struct acpi_device *device, int type)
1088 acpi_status status = AE_OK;
1089 union acpi_ec *ec = NULL;
1095 ec = acpi_driver_data(device);
1097 status = acpi_remove_address_space_handler(ec->common.handle,
1099 &acpi_ec_space_handler);
1100 if (ACPI_FAILURE(status))
1104 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1105 &acpi_ec_gpe_handler);
1106 if (ACPI_FAILURE(status))
1112 static acpi_status __init
1113 acpi_fake_ecdt_callback(acpi_handle handle,
1114 u32 Level, void *context, void **retval)
1117 if (acpi_ec_poll_mode)
1118 return acpi_fake_ecdt_poll_callback(handle,
1119 Level, context, retval);
1121 return acpi_fake_ecdt_intr_callback(handle,
1122 Level, context, retval);
1125 static acpi_status __init
1126 acpi_fake_ecdt_poll_callback(acpi_handle handle,
1127 u32 Level, void *context, void **retval)
1131 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1132 acpi_ec_io_ports, ec_ecdt);
1133 if (ACPI_FAILURE(status))
1135 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1137 ec_ecdt->common.uid = -1;
1138 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1141 acpi_evaluate_integer(handle, "_GPE", NULL,
1142 &ec_ecdt->common.gpe_bit);
1143 if (ACPI_FAILURE(status))
1145 init_MUTEX(&ec_ecdt->poll.sem);
1146 ec_ecdt->common.global_lock = TRUE;
1147 ec_ecdt->common.handle = handle;
1149 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1150 (u32) ec_ecdt->common.gpe_bit,
1151 (u32) ec_ecdt->common.command_addr.address,
1152 (u32) ec_ecdt->common.data_addr.address);
1154 return AE_CTRL_TERMINATE;
1157 static acpi_status __init
1158 acpi_fake_ecdt_intr_callback(acpi_handle handle,
1159 u32 Level, void *context, void **retval)
1163 init_MUTEX(&ec_ecdt->intr.sem);
1164 init_waitqueue_head(&ec_ecdt->intr.wait);
1165 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1166 acpi_ec_io_ports, ec_ecdt);
1167 if (ACPI_FAILURE(status))
1169 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1171 ec_ecdt->common.uid = -1;
1172 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1175 acpi_evaluate_integer(handle, "_GPE", NULL,
1176 &ec_ecdt->common.gpe_bit);
1177 if (ACPI_FAILURE(status))
1179 ec_ecdt->common.global_lock = TRUE;
1180 ec_ecdt->common.handle = handle;
1182 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1183 (u32) ec_ecdt->common.gpe_bit,
1184 (u32) ec_ecdt->common.command_addr.address,
1185 (u32) ec_ecdt->common.data_addr.address);
1187 return AE_CTRL_TERMINATE;
1191 * Some BIOS (such as some from Gateway laptops) access EC region very early
1192 * such as in BAT0._INI or EC._INI before an EC device is found and
1193 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1194 * required, but if EC regison is accessed early, it is required.
1195 * The routine tries to workaround the BIOS bug by pre-scan EC device
1196 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1197 * op region (since _REG isn't invoked yet). The assumption is true for
1198 * all systems found.
1200 static int __init acpi_ec_fake_ecdt(void)
1205 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1207 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1212 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1214 status = acpi_get_devices(ACPI_EC_HID,
1215 acpi_fake_ecdt_callback, NULL, NULL);
1216 if (ACPI_FAILURE(status)) {
1224 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1228 static int __init acpi_ec_get_real_ecdt(void)
1230 if (acpi_ec_poll_mode)
1231 return acpi_ec_poll_get_real_ecdt();
1233 return acpi_ec_intr_get_real_ecdt();
1236 static int __init acpi_ec_poll_get_real_ecdt(void)
1239 struct acpi_table_ecdt *ecdt_ptr;
1241 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1242 (struct acpi_table_header **)
1244 if (ACPI_FAILURE(status))
1247 printk(KERN_INFO PREFIX "Found ECDT\n");
1250 * Generate a temporary ec context to use until the namespace is scanned
1252 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1255 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1257 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1258 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1259 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1260 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1261 init_MUTEX(&ec_ecdt->poll.sem);
1262 /* use the GL just to be safe */
1263 ec_ecdt->common.global_lock = TRUE;
1264 ec_ecdt->common.uid = ecdt_ptr->uid;
1267 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1268 if (ACPI_FAILURE(status)) {
1274 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1281 static int __init acpi_ec_intr_get_real_ecdt(void)
1284 struct acpi_table_ecdt *ecdt_ptr;
1286 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1287 (struct acpi_table_header **)
1289 if (ACPI_FAILURE(status))
1292 printk(KERN_INFO PREFIX "Found ECDT\n");
1295 * Generate a temporary ec context to use until the namespace is scanned
1297 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1300 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1302 init_MUTEX(&ec_ecdt->intr.sem);
1303 init_waitqueue_head(&ec_ecdt->intr.wait);
1304 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1305 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1306 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1307 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1308 /* use the GL just to be safe */
1309 ec_ecdt->common.global_lock = TRUE;
1310 ec_ecdt->common.uid = ecdt_ptr->uid;
1313 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1314 if (ACPI_FAILURE(status)) {
1320 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1327 static int __initdata acpi_fake_ecdt_enabled;
1328 int __init acpi_ec_ecdt_probe(void)
1333 ret = acpi_ec_get_real_ecdt();
1334 /* Try to make a fake ECDT */
1335 if (ret && acpi_fake_ecdt_enabled) {
1336 ret = acpi_ec_fake_ecdt();
1343 * Install GPE handler
1345 status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1346 ACPI_GPE_EDGE_TRIGGERED,
1347 &acpi_ec_gpe_handler, ec_ecdt);
1348 if (ACPI_FAILURE(status)) {
1351 acpi_set_gpe_type(NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1352 acpi_enable_gpe(NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
1354 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
1356 &acpi_ec_space_handler,
1357 &acpi_ec_space_setup,
1359 if (ACPI_FAILURE(status)) {
1360 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1361 &acpi_ec_gpe_handler);
1368 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1375 static int __init acpi_ec_init(void)
1383 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1387 /* Now register the driver for the EC */
1388 result = acpi_bus_register_driver(&acpi_ec_driver);
1390 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1397 subsys_initcall(acpi_ec_init);
1399 /* EC driver currently not unloadable */
1401 static void __exit acpi_ec_exit(void)
1404 acpi_bus_unregister_driver(&acpi_ec_driver);
1406 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1412 static int __init acpi_fake_ecdt_setup(char *str)
1414 acpi_fake_ecdt_enabled = 1;
1418 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1419 static int __init acpi_ec_set_intr_mode(char *str)
1423 if (!get_option(&str, &intr))
1427 acpi_ec_poll_mode = EC_INTR;
1428 acpi_ec_driver.ops.add = acpi_ec_intr_add;
1430 acpi_ec_poll_mode = EC_POLL;
1431 acpi_ec_driver.ops.add = acpi_ec_poll_add;
1433 printk(KERN_INFO PREFIX "EC %s mode.\n", intr ? "interrupt" : "polling");
1437 __setup("ec_intr=", acpi_ec_set_intr_mode);