2 * acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 38 $)
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/delay.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/interrupt.h>
36 #include <acpi/acpi_bus.h>
37 #include <acpi/acpi_drivers.h>
38 #include <acpi/actypes.h>
40 #define _COMPONENT ACPI_EC_COMPONENT
41 ACPI_MODULE_NAME("acpi_ec")
42 #define ACPI_EC_COMPONENT 0x00100000
43 #define ACPI_EC_CLASS "embedded_controller"
44 #define ACPI_EC_HID "PNP0C09"
45 #define ACPI_EC_DRIVER_NAME "ACPI Embedded Controller Driver"
46 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
47 #define ACPI_EC_FILE_INFO "info"
48 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
49 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
50 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
51 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
52 #define ACPI_EC_EVENT_OBF 0x01 /* Output buffer full */
53 #define ACPI_EC_EVENT_IBE 0x02 /* Input buffer empty */
54 #define ACPI_EC_DELAY 50 /* Wait 50ms max. during EC ops */
55 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
56 #define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
57 #define ACPI_EC_UDELAY_COUNT 1000 /* Wait 10ms max. during EC ops */
58 #define ACPI_EC_COMMAND_READ 0x80
59 #define ACPI_EC_COMMAND_WRITE 0x81
60 #define ACPI_EC_BURST_ENABLE 0x82
61 #define ACPI_EC_BURST_DISABLE 0x83
62 #define ACPI_EC_COMMAND_QUERY 0x84
65 static int acpi_ec_remove(struct acpi_device *device, int type);
66 static int acpi_ec_start(struct acpi_device *device);
67 static int acpi_ec_stop(struct acpi_device *device, int type);
68 static int acpi_ec_intr_add(struct acpi_device *device);
69 static int acpi_ec_poll_add(struct acpi_device *device);
71 static struct acpi_driver acpi_ec_driver = {
72 .name = ACPI_EC_DRIVER_NAME,
73 .class = ACPI_EC_CLASS,
76 .add = acpi_ec_intr_add,
77 .remove = acpi_ec_remove,
78 .start = acpi_ec_start,
87 unsigned long gpe_bit;
88 struct acpi_generic_address status_addr;
89 struct acpi_generic_address command_addr;
90 struct acpi_generic_address data_addr;
91 unsigned long global_lock;
98 unsigned long gpe_bit;
99 struct acpi_generic_address status_addr;
100 struct acpi_generic_address command_addr;
101 struct acpi_generic_address data_addr;
102 unsigned long global_lock;
103 unsigned int expect_event;
104 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
105 atomic_t pending_gpe;
106 struct semaphore sem;
107 wait_queue_head_t wait;
114 unsigned long gpe_bit;
115 struct acpi_generic_address status_addr;
116 struct acpi_generic_address command_addr;
117 struct acpi_generic_address data_addr;
118 unsigned long global_lock;
119 struct semaphore sem;
123 static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event);
124 static int acpi_ec_intr_wait(union acpi_ec *ec, unsigned int event);
125 static int acpi_ec_poll_read(union acpi_ec *ec, u8 address, u32 * data);
126 static int acpi_ec_intr_read(union acpi_ec *ec, u8 address, u32 * data);
127 static int acpi_ec_poll_write(union acpi_ec *ec, u8 address, u8 data);
128 static int acpi_ec_intr_write(union acpi_ec *ec, u8 address, u8 data);
129 static int acpi_ec_poll_query(union acpi_ec *ec, u32 * data);
130 static int acpi_ec_intr_query(union acpi_ec *ec, u32 * data);
131 static void acpi_ec_gpe_poll_query(void *ec_cxt);
132 static void acpi_ec_gpe_intr_query(void *ec_cxt);
133 static u32 acpi_ec_gpe_poll_handler(void *data);
134 static u32 acpi_ec_gpe_intr_handler(void *data);
135 static acpi_status __init
136 acpi_fake_ecdt_poll_callback(acpi_handle handle,
137 u32 Level, void *context, void **retval);
139 static acpi_status __init
140 acpi_fake_ecdt_intr_callback(acpi_handle handle,
141 u32 Level, void *context, void **retval);
143 static int __init acpi_ec_poll_get_real_ecdt(void);
144 static int __init acpi_ec_intr_get_real_ecdt(void);
145 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
146 static union acpi_ec *ec_ecdt;
148 /* External interfaces use first EC only, so remember */
149 static struct acpi_device *first_ec;
150 static int acpi_ec_poll_mode = EC_INTR;
152 /* --------------------------------------------------------------------------
153 Transaction Management
154 -------------------------------------------------------------------------- */
156 static u32 acpi_ec_read_status(union acpi_ec *ec)
160 acpi_hw_low_level_read(8, &status, &ec->common.status_addr);
164 static int acpi_ec_wait(union acpi_ec *ec, u8 event)
166 if (acpi_ec_poll_mode)
167 return acpi_ec_poll_wait(ec, event);
169 return acpi_ec_intr_wait(ec, event);
172 static int acpi_ec_poll_wait(union acpi_ec *ec, u8 event)
174 u32 acpi_ec_status = 0;
175 u32 i = ACPI_EC_UDELAY_COUNT;
180 /* Poll the EC status register waiting for the event to occur. */
182 case ACPI_EC_EVENT_OBF:
184 acpi_hw_low_level_read(8, &acpi_ec_status,
185 &ec->common.status_addr);
186 if (acpi_ec_status & ACPI_EC_FLAG_OBF)
188 udelay(ACPI_EC_UDELAY);
191 case ACPI_EC_EVENT_IBE:
193 acpi_hw_low_level_read(8, &acpi_ec_status,
194 &ec->common.status_addr);
195 if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
197 udelay(ACPI_EC_UDELAY);
206 static int acpi_ec_intr_wait(union acpi_ec *ec, unsigned int event)
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_read(union acpi_ec *ec, u8 address, u32 * data)
307 if (acpi_ec_poll_mode)
308 return acpi_ec_poll_read(ec, address, data);
310 return acpi_ec_intr_read(ec, address, data);
312 static int acpi_ec_write(union acpi_ec *ec, u8 address, u8 data)
314 if (acpi_ec_poll_mode)
315 return acpi_ec_poll_write(ec, address, data);
317 return acpi_ec_intr_write(ec, address, data);
319 static int acpi_ec_poll_read(union acpi_ec *ec, u8 address, u32 * data)
321 acpi_status status = AE_OK;
331 if (ec->common.global_lock) {
332 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
333 if (ACPI_FAILURE(status))
337 if (down_interruptible(&ec->poll.sem)) {
338 result = -ERESTARTSYS;
342 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
343 &ec->common.command_addr);
344 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
348 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
349 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
353 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
355 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
361 if (ec->common.global_lock)
362 acpi_release_global_lock(glk);
367 static int acpi_ec_poll_write(union acpi_ec *ec, u8 address, u8 data)
370 acpi_status status = AE_OK;
377 if (ec->common.global_lock) {
378 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
379 if (ACPI_FAILURE(status))
383 if (down_interruptible(&ec->poll.sem)) {
384 result = -ERESTARTSYS;
388 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
389 &ec->common.command_addr);
390 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
394 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
395 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
399 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
400 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
404 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
410 if (ec->common.global_lock)
411 acpi_release_global_lock(glk);
416 static int acpi_ec_intr_read(union acpi_ec *ec, u8 address, u32 * data)
427 if (ec->common.global_lock) {
428 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
429 if (ACPI_FAILURE(status))
433 WARN_ON(in_interrupt());
436 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
438 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
441 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ,
442 &ec->common.command_addr);
443 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
445 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
448 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
449 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
451 printk(KERN_DEBUG PREFIX "read EC, OB not full\n");
454 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
455 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
461 if (ec->common.global_lock)
462 acpi_release_global_lock(glk);
467 static int acpi_ec_intr_write(union acpi_ec *ec, u8 address, u8 data)
476 if (ec->common.global_lock) {
477 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
478 if (ACPI_FAILURE(status))
482 WARN_ON(in_interrupt());
485 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
487 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
489 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE,
490 &ec->common.command_addr);
491 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
493 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
496 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
497 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
499 printk(KERN_DEBUG PREFIX "write EC, IB not empty\n");
502 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
504 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
509 if (ec->common.global_lock)
510 acpi_release_global_lock(glk);
516 * Externally callable EC access functions. For now, assume 1 EC only
518 int ec_read(u8 addr, u8 * val)
527 ec = acpi_driver_data(first_ec);
529 err = acpi_ec_read(ec, addr, &temp_data);
538 EXPORT_SYMBOL(ec_read);
540 int ec_write(u8 addr, u8 val)
548 ec = acpi_driver_data(first_ec);
550 err = acpi_ec_write(ec, addr, val);
555 EXPORT_SYMBOL(ec_write);
557 static int acpi_ec_query(union acpi_ec *ec, u32 * data)
559 if (acpi_ec_poll_mode)
560 return acpi_ec_poll_query(ec, data);
562 return acpi_ec_intr_query(ec, data);
564 static int acpi_ec_poll_query(union acpi_ec *ec, u32 * data)
567 acpi_status status = AE_OK;
576 if (ec->common.global_lock) {
577 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
578 if (ACPI_FAILURE(status))
583 * Query the EC to find out which _Qxx method we need to evaluate.
584 * Note that successful completion of the query causes the ACPI_EC_SCI
585 * bit to be cleared (and thus clearing the interrupt source).
587 if (down_interruptible(&ec->poll.sem)) {
588 result = -ERESTARTSYS;
592 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
593 &ec->common.command_addr);
594 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
598 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
605 if (ec->common.global_lock)
606 acpi_release_global_lock(glk);
610 static int acpi_ec_intr_query(union acpi_ec *ec, u32 * data)
620 if (ec->common.global_lock) {
621 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
622 if (ACPI_FAILURE(status))
628 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
630 printk(KERN_DEBUG PREFIX "query EC, IB not empty\n");
634 * Query the EC to find out which _Qxx method we need to evaluate.
635 * Note that successful completion of the query causes the ACPI_EC_SCI
636 * bit to be cleared (and thus clearing the interrupt source).
638 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY,
639 &ec->common.command_addr);
640 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
642 printk(KERN_DEBUG PREFIX "query EC, OB not full\n");
646 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
653 if (ec->common.global_lock)
654 acpi_release_global_lock(glk);
659 /* --------------------------------------------------------------------------
661 -------------------------------------------------------------------------- */
663 union acpi_ec_query_data {
668 static void acpi_ec_gpe_query(void *ec_cxt)
670 if (acpi_ec_poll_mode)
671 acpi_ec_gpe_poll_query(ec_cxt);
673 acpi_ec_gpe_intr_query(ec_cxt);
676 static void acpi_ec_gpe_poll_query(void *ec_cxt)
678 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
680 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
681 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
682 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
689 if (down_interruptible (&ec->poll.sem)) {
692 acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
695 /* TBD: Implement asynch events!
696 * NOTE: All we care about are EC-SCI's. Other EC events are
697 * handled via polling (yuck!). This is because some systems
698 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
699 * a purely interrupt-driven approach (grumble, grumble).
701 if (!(value & ACPI_EC_FLAG_SCI))
704 if (acpi_ec_query(ec, &value))
707 object_name[2] = hex[((value >> 4) & 0x0F)];
708 object_name[3] = hex[(value & 0x0F)];
710 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
712 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
715 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
717 static void acpi_ec_gpe_intr_query(void *ec_cxt)
719 union acpi_ec *ec = (union acpi_ec *)ec_cxt;
721 int result = -ENODATA;
722 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
723 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
724 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
728 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
729 result = acpi_ec_query(ec, &value);
734 object_name[2] = hex[((value >> 4) & 0x0F)];
735 object_name[3] = hex[(value & 0x0F)];
737 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
739 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
741 atomic_dec(&ec->intr.pending_gpe);
745 static u32 acpi_ec_gpe_handler(void *data)
747 if (acpi_ec_poll_mode)
748 return acpi_ec_gpe_poll_handler(data);
750 return acpi_ec_gpe_intr_handler(data);
752 static u32 acpi_ec_gpe_poll_handler(void *data)
754 acpi_status status = AE_OK;
755 union acpi_ec *ec = (union acpi_ec *)data;
758 return ACPI_INTERRUPT_NOT_HANDLED;
760 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
762 status = acpi_os_execute(OSL_EC_POLL_HANDLER, acpi_ec_gpe_query, ec);
765 return ACPI_INTERRUPT_HANDLED;
767 return ACPI_INTERRUPT_NOT_HANDLED;
769 static u32 acpi_ec_gpe_intr_handler(void *data)
771 acpi_status status = AE_OK;
773 union acpi_ec *ec = (union acpi_ec *)data;
776 return ACPI_INTERRUPT_NOT_HANDLED;
778 acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
779 value = acpi_ec_read_status(ec);
781 switch (ec->intr.expect_event) {
782 case ACPI_EC_EVENT_OBF:
783 if (!(value & ACPI_EC_FLAG_OBF))
785 ec->intr.expect_event = 0;
786 wake_up(&ec->intr.wait);
788 case ACPI_EC_EVENT_IBE:
789 if ((value & ACPI_EC_FLAG_IBF))
791 ec->intr.expect_event = 0;
792 wake_up(&ec->intr.wait);
798 if (value & ACPI_EC_FLAG_SCI) {
799 atomic_add(1, &ec->intr.pending_gpe);
800 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
801 acpi_ec_gpe_query, ec);
802 return status == AE_OK ?
803 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
805 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
806 return status == AE_OK ?
807 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
810 /* --------------------------------------------------------------------------
811 Address Space Management
812 -------------------------------------------------------------------------- */
815 acpi_ec_space_setup(acpi_handle region_handle,
816 u32 function, void *handler_context, void **return_context)
819 * The EC object is in the handler context and is needed
820 * when calling the acpi_ec_space_handler.
822 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
823 handler_context : NULL;
829 acpi_ec_space_handler(u32 function,
830 acpi_physical_address address,
832 acpi_integer * value,
833 void *handler_context, void *region_context)
836 union acpi_ec *ec = NULL;
838 acpi_integer f_v = 0;
842 if ((address > 0xFF) || !value || !handler_context)
843 return AE_BAD_PARAMETER;
845 if (bit_width != 8 && acpi_strict) {
846 printk(KERN_WARNING PREFIX
847 "acpi_ec_space_handler: bit_width should be 8\n");
848 return AE_BAD_PARAMETER;
851 ec = (union acpi_ec *)handler_context;
857 result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
860 result = acpi_ec_write(ec, (u8) address, (u8) temp);
870 if (function == ACPI_READ)
871 f_v |= temp << 8 * i;
872 if (function == ACPI_WRITE)
879 if (function == ACPI_READ) {
880 f_v |= temp << 8 * i;
887 return AE_BAD_PARAMETER;
900 /* --------------------------------------------------------------------------
902 -------------------------------------------------------------------------- */
904 static struct proc_dir_entry *acpi_ec_dir;
906 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
908 union acpi_ec *ec = (union acpi_ec *)seq->private;
914 seq_printf(seq, "gpe bit: 0x%02x\n",
915 (u32) ec->common.gpe_bit);
916 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
917 (u32) ec->common.status_addr.address,
918 (u32) ec->common.data_addr.address);
919 seq_printf(seq, "use global lock: %s\n",
920 ec->common.global_lock ? "yes" : "no");
921 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
927 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
929 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
932 static struct file_operations acpi_ec_info_ops = {
933 .open = acpi_ec_info_open_fs,
936 .release = single_release,
937 .owner = THIS_MODULE,
940 static int acpi_ec_add_fs(struct acpi_device *device)
942 struct proc_dir_entry *entry = NULL;
945 if (!acpi_device_dir(device)) {
946 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
948 if (!acpi_device_dir(device))
952 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
953 acpi_device_dir(device));
957 entry->proc_fops = &acpi_ec_info_ops;
958 entry->data = acpi_driver_data(device);
959 entry->owner = THIS_MODULE;
965 static int acpi_ec_remove_fs(struct acpi_device *device)
968 if (acpi_device_dir(device)) {
969 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
970 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
971 acpi_device_dir(device) = NULL;
977 /* --------------------------------------------------------------------------
979 -------------------------------------------------------------------------- */
981 static int acpi_ec_poll_add(struct acpi_device *device)
984 acpi_status status = AE_OK;
985 union acpi_ec *ec = NULL;
991 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
994 memset(ec, 0, sizeof(union acpi_ec));
996 ec->common.handle = device->handle;
998 init_MUTEX(&ec->poll.sem);
999 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1000 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1001 acpi_driver_data(device) = ec;
1003 /* Use the global lock for all EC transactions? */
1004 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
1005 &ec->common.global_lock);
1007 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
1008 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
1010 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1012 &acpi_ec_space_handler);
1014 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1015 &acpi_ec_gpe_handler);
1020 /* Get GPE bit assignment (EC events). */
1021 /* TODO: Add support for _GPE returning a package */
1023 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
1024 &ec->common.gpe_bit);
1025 if (ACPI_FAILURE(status)) {
1026 ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit"));
1031 result = acpi_ec_add_fs(device);
1035 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) polling mode.\n",
1036 acpi_device_name(device), acpi_device_bid(device),
1037 (u32) ec->common.gpe_bit);
1048 static int acpi_ec_intr_add(struct acpi_device *device)
1051 acpi_status status = AE_OK;
1052 union acpi_ec *ec = NULL;
1058 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1061 memset(ec, 0, sizeof(union acpi_ec));
1063 ec->common.handle = device->handle;
1064 ec->common.uid = -1;
1065 atomic_set(&ec->intr.pending_gpe, 0);
1066 atomic_set(&ec->intr.leaving_burst, 1);
1067 init_MUTEX(&ec->intr.sem);
1068 init_waitqueue_head(&ec->intr.wait);
1069 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1070 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1071 acpi_driver_data(device) = ec;
1073 /* Use the global lock for all EC transactions? */
1074 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL,
1075 &ec->common.global_lock);
1077 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
1078 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
1080 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1082 &acpi_ec_space_handler);
1084 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1085 &acpi_ec_gpe_handler);
1090 /* Get GPE bit assignment (EC events). */
1091 /* TODO: Add support for _GPE returning a package */
1093 acpi_evaluate_integer(ec->common.handle, "_GPE", NULL,
1094 &ec->common.gpe_bit);
1095 if (ACPI_FAILURE(status)) {
1096 printk(KERN_ERR PREFIX "Obtaining GPE bit assignment\n");
1101 result = acpi_ec_add_fs(device);
1105 printk(KERN_INFO PREFIX "%s [%s] (gpe %d) interrupt mode.\n",
1106 acpi_device_name(device), acpi_device_bid(device),
1107 (u32) ec->common.gpe_bit);
1119 static int acpi_ec_remove(struct acpi_device *device, int type)
1121 union acpi_ec *ec = NULL;
1127 ec = acpi_driver_data(device);
1129 acpi_ec_remove_fs(device);
1137 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
1139 union acpi_ec *ec = (union acpi_ec *)context;
1140 struct acpi_generic_address *addr;
1142 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
1147 * The first address region returned is the data port, and
1148 * the second address region returned is the status/command
1151 if (ec->common.data_addr.register_bit_width == 0) {
1152 addr = &ec->common.data_addr;
1153 } else if (ec->common.command_addr.register_bit_width == 0) {
1154 addr = &ec->common.command_addr;
1156 return AE_CTRL_TERMINATE;
1159 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1160 addr->register_bit_width = 8;
1161 addr->register_bit_offset = 0;
1162 addr->address = resource->data.io.minimum;
1167 static int acpi_ec_start(struct acpi_device *device)
1169 acpi_status status = AE_OK;
1170 union acpi_ec *ec = NULL;
1176 ec = acpi_driver_data(device);
1182 * Get I/O port addresses. Convert to GAS format.
1184 status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
1185 acpi_ec_io_ports, ec);
1186 if (ACPI_FAILURE(status)
1187 || ec->common.command_addr.register_bit_width == 0) {
1188 printk(KERN_ERR PREFIX "Error getting I/O port addresses\n");
1192 ec->common.status_addr = ec->common.command_addr;
1194 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
1195 (u32) ec->common.gpe_bit,
1196 (u32) ec->common.command_addr.address,
1197 (u32) ec->common.data_addr.address));
1200 * Install GPE handler
1202 status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
1203 ACPI_GPE_EDGE_TRIGGERED,
1204 &acpi_ec_gpe_handler, ec);
1205 if (ACPI_FAILURE(status)) {
1208 acpi_set_gpe_type(NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1209 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
1211 status = acpi_install_address_space_handler(ec->common.handle,
1213 &acpi_ec_space_handler,
1214 &acpi_ec_space_setup, ec);
1215 if (ACPI_FAILURE(status)) {
1216 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1217 &acpi_ec_gpe_handler);
1224 static int acpi_ec_stop(struct acpi_device *device, int type)
1226 acpi_status status = AE_OK;
1227 union acpi_ec *ec = NULL;
1233 ec = acpi_driver_data(device);
1235 status = acpi_remove_address_space_handler(ec->common.handle,
1237 &acpi_ec_space_handler);
1238 if (ACPI_FAILURE(status))
1242 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit,
1243 &acpi_ec_gpe_handler);
1244 if (ACPI_FAILURE(status))
1250 static acpi_status __init
1251 acpi_fake_ecdt_callback(acpi_handle handle,
1252 u32 Level, void *context, void **retval)
1255 if (acpi_ec_poll_mode)
1256 return acpi_fake_ecdt_poll_callback(handle,
1257 Level, context, retval);
1259 return acpi_fake_ecdt_intr_callback(handle,
1260 Level, context, retval);
1263 static acpi_status __init
1264 acpi_fake_ecdt_poll_callback(acpi_handle handle,
1265 u32 Level, void *context, void **retval)
1269 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1270 acpi_ec_io_ports, ec_ecdt);
1271 if (ACPI_FAILURE(status))
1273 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1275 ec_ecdt->common.uid = -1;
1276 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1279 acpi_evaluate_integer(handle, "_GPE", NULL,
1280 &ec_ecdt->common.gpe_bit);
1281 if (ACPI_FAILURE(status))
1283 init_MUTEX(&ec_ecdt->poll.sem);
1284 ec_ecdt->common.global_lock = TRUE;
1285 ec_ecdt->common.handle = handle;
1287 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1288 (u32) ec_ecdt->common.gpe_bit,
1289 (u32) ec_ecdt->common.command_addr.address,
1290 (u32) ec_ecdt->common.data_addr.address);
1292 return AE_CTRL_TERMINATE;
1295 static acpi_status __init
1296 acpi_fake_ecdt_intr_callback(acpi_handle handle,
1297 u32 Level, void *context, void **retval)
1301 init_MUTEX(&ec_ecdt->intr.sem);
1302 init_waitqueue_head(&ec_ecdt->intr.wait);
1303 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1304 acpi_ec_io_ports, ec_ecdt);
1305 if (ACPI_FAILURE(status))
1307 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1309 ec_ecdt->common.uid = -1;
1310 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1313 acpi_evaluate_integer(handle, "_GPE", NULL,
1314 &ec_ecdt->common.gpe_bit);
1315 if (ACPI_FAILURE(status))
1317 ec_ecdt->common.global_lock = TRUE;
1318 ec_ecdt->common.handle = handle;
1320 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1321 (u32) ec_ecdt->common.gpe_bit,
1322 (u32) ec_ecdt->common.command_addr.address,
1323 (u32) ec_ecdt->common.data_addr.address);
1325 return AE_CTRL_TERMINATE;
1329 * Some BIOS (such as some from Gateway laptops) access EC region very early
1330 * such as in BAT0._INI or EC._INI before an EC device is found and
1331 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1332 * required, but if EC regison is accessed early, it is required.
1333 * The routine tries to workaround the BIOS bug by pre-scan EC device
1334 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1335 * op region (since _REG isn't invoked yet). The assumption is true for
1336 * all systems found.
1338 static int __init acpi_ec_fake_ecdt(void)
1343 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1345 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1350 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1352 status = acpi_get_devices(ACPI_EC_HID,
1353 acpi_fake_ecdt_callback, NULL, NULL);
1354 if (ACPI_FAILURE(status)) {
1362 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1366 static int __init acpi_ec_get_real_ecdt(void)
1368 if (acpi_ec_poll_mode)
1369 return acpi_ec_poll_get_real_ecdt();
1371 return acpi_ec_intr_get_real_ecdt();
1374 static int __init acpi_ec_poll_get_real_ecdt(void)
1377 struct acpi_table_ecdt *ecdt_ptr;
1379 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1380 (struct acpi_table_header **)
1382 if (ACPI_FAILURE(status))
1385 printk(KERN_INFO PREFIX "Found ECDT\n");
1388 * Generate a temporary ec context to use until the namespace is scanned
1390 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1393 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1395 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1396 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1397 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1398 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1399 init_MUTEX(&ec_ecdt->poll.sem);
1400 /* use the GL just to be safe */
1401 ec_ecdt->common.global_lock = TRUE;
1402 ec_ecdt->common.uid = ecdt_ptr->uid;
1405 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1406 if (ACPI_FAILURE(status)) {
1412 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1419 static int __init acpi_ec_intr_get_real_ecdt(void)
1422 struct acpi_table_ecdt *ecdt_ptr;
1424 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1425 (struct acpi_table_header **)
1427 if (ACPI_FAILURE(status))
1430 printk(KERN_INFO PREFIX "Found ECDT\n");
1433 * Generate a temporary ec context to use until the namespace is scanned
1435 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1438 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1440 init_MUTEX(&ec_ecdt->intr.sem);
1441 init_waitqueue_head(&ec_ecdt->intr.wait);
1442 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1443 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1444 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1445 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1446 /* use the GL just to be safe */
1447 ec_ecdt->common.global_lock = TRUE;
1448 ec_ecdt->common.uid = ecdt_ptr->uid;
1451 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1452 if (ACPI_FAILURE(status)) {
1458 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1465 static int __initdata acpi_fake_ecdt_enabled;
1466 int __init acpi_ec_ecdt_probe(void)
1471 ret = acpi_ec_get_real_ecdt();
1472 /* Try to make a fake ECDT */
1473 if (ret && acpi_fake_ecdt_enabled) {
1474 ret = acpi_ec_fake_ecdt();
1481 * Install GPE handler
1483 status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1484 ACPI_GPE_EDGE_TRIGGERED,
1485 &acpi_ec_gpe_handler, ec_ecdt);
1486 if (ACPI_FAILURE(status)) {
1489 acpi_set_gpe_type(NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1490 acpi_enable_gpe(NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
1492 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
1494 &acpi_ec_space_handler,
1495 &acpi_ec_space_setup,
1497 if (ACPI_FAILURE(status)) {
1498 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1499 &acpi_ec_gpe_handler);
1506 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1513 static int __init acpi_ec_init(void)
1521 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1525 /* Now register the driver for the EC */
1526 result = acpi_bus_register_driver(&acpi_ec_driver);
1528 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1535 subsys_initcall(acpi_ec_init);
1537 /* EC driver currently not unloadable */
1539 static void __exit acpi_ec_exit(void)
1542 acpi_bus_unregister_driver(&acpi_ec_driver);
1544 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1550 static int __init acpi_fake_ecdt_setup(char *str)
1552 acpi_fake_ecdt_enabled = 1;
1556 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1557 static int __init acpi_ec_set_intr_mode(char *str)
1561 if (!get_option(&str, &intr))
1565 acpi_ec_poll_mode = EC_INTR;
1566 acpi_ec_driver.ops.add = acpi_ec_intr_add;
1568 acpi_ec_poll_mode = EC_POLL;
1569 acpi_ec_driver.ops.add = acpi_ec_poll_add;
1571 printk(KERN_INFO PREFIX "EC %s mode.\n", intr ? "interrupt" : "polling");
1575 __setup("ec_intr=", acpi_ec_set_intr_mode);