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")
43 #define ACPI_EC_COMPONENT 0x00100000
44 #define ACPI_EC_CLASS "embedded_controller"
45 #define ACPI_EC_HID "PNP0C09"
46 #define ACPI_EC_DRIVER_NAME "ACPI Embedded Controller Driver"
47 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
48 #define ACPI_EC_FILE_INFO "info"
51 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
52 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
53 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
54 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
56 #define ACPI_EC_EVENT_OBF 0x01 /* Output buffer full */
57 #define ACPI_EC_EVENT_IBE 0x02 /* Input buffer empty */
59 #define ACPI_EC_DELAY 50 /* Wait 50ms max. during EC ops */
60 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
62 #define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
63 #define ACPI_EC_UDELAY_COUNT 1000 /* Wait 10ms max. during EC ops */
65 #define ACPI_EC_COMMAND_READ 0x80
66 #define ACPI_EC_COMMAND_WRITE 0x81
67 #define ACPI_EC_BURST_ENABLE 0x82
68 #define ACPI_EC_BURST_DISABLE 0x83
69 #define ACPI_EC_COMMAND_QUERY 0x84
71 #define EC_POLLING 0xFF
75 static int acpi_ec_remove (struct acpi_device *device, int type);
76 static int acpi_ec_start (struct acpi_device *device);
77 static int acpi_ec_stop (struct acpi_device *device, int type);
78 static int acpi_ec_burst_add ( struct acpi_device *device);
79 static int acpi_ec_polling_add ( struct acpi_device *device);
81 static struct acpi_driver acpi_ec_driver = {
82 .name = ACPI_EC_DRIVER_NAME,
83 .class = ACPI_EC_CLASS,
86 .add = acpi_ec_polling_add,
87 .remove = acpi_ec_remove,
88 .start = acpi_ec_start,
97 unsigned long gpe_bit;
98 struct acpi_generic_address status_addr;
99 struct acpi_generic_address command_addr;
100 struct acpi_generic_address data_addr;
101 unsigned long global_lock;
108 unsigned long gpe_bit;
109 struct acpi_generic_address status_addr;
110 struct acpi_generic_address command_addr;
111 struct acpi_generic_address data_addr;
112 unsigned long global_lock;
113 unsigned int expect_event;
114 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort*/
115 atomic_t pending_gpe;
116 struct semaphore sem;
117 wait_queue_head_t wait;
124 unsigned long gpe_bit;
125 struct acpi_generic_address status_addr;
126 struct acpi_generic_address command_addr;
127 struct acpi_generic_address data_addr;
128 unsigned long global_lock;
133 static int acpi_ec_polling_wait ( union acpi_ec *ec, u8 event);
134 static int acpi_ec_burst_wait(union acpi_ec *ec, unsigned int event);
135 static int acpi_ec_polling_read ( union acpi_ec *ec, u8 address, u32 *data);
136 static int acpi_ec_burst_read( union acpi_ec *ec, u8 address, u32 *data);
137 static int acpi_ec_polling_write ( union acpi_ec *ec, u8 address, u8 data);
138 static int acpi_ec_burst_write ( union acpi_ec *ec, u8 address, u8 data);
139 static int acpi_ec_polling_query ( union acpi_ec *ec, u32 *data);
140 static int acpi_ec_burst_query ( union acpi_ec *ec, u32 *data);
141 static void acpi_ec_gpe_polling_query ( void *ec_cxt);
142 static void acpi_ec_gpe_burst_query ( void *ec_cxt);
143 static u32 acpi_ec_gpe_polling_handler ( void *data);
144 static u32 acpi_ec_gpe_burst_handler ( void *data);
145 static acpi_status __init
146 acpi_fake_ecdt_polling_callback (
152 static acpi_status __init
153 acpi_fake_ecdt_burst_callback (
160 acpi_ec_polling_get_real_ecdt(void);
162 acpi_ec_burst_get_real_ecdt(void);
163 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
164 static union acpi_ec *ec_ecdt;
166 /* External interfaces use first EC only, so remember */
167 static struct acpi_device *first_ec;
168 static int acpi_ec_polling_mode = EC_POLLING;
170 /* --------------------------------------------------------------------------
171 Transaction Management
172 -------------------------------------------------------------------------- */
174 static inline u32 acpi_ec_read_status(union acpi_ec *ec)
178 acpi_hw_low_level_read(8, &status, &ec->common.status_addr);
187 if (acpi_ec_polling_mode)
188 return acpi_ec_polling_wait (ec, event);
190 return acpi_ec_burst_wait (ec, event);
194 acpi_ec_polling_wait (
198 u32 acpi_ec_status = 0;
199 u32 i = ACPI_EC_UDELAY_COUNT;
204 /* Poll the EC status register waiting for the event to occur. */
206 case ACPI_EC_EVENT_OBF:
208 acpi_hw_low_level_read(8, &acpi_ec_status, &ec->common.status_addr);
209 if (acpi_ec_status & ACPI_EC_FLAG_OBF)
211 udelay(ACPI_EC_UDELAY);
214 case ACPI_EC_EVENT_IBE:
216 acpi_hw_low_level_read(8, &acpi_ec_status, &ec->common.status_addr);
217 if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
219 udelay(ACPI_EC_UDELAY);
228 static int acpi_ec_burst_wait(union acpi_ec *ec, unsigned int event)
232 ACPI_FUNCTION_TRACE("acpi_ec_wait");
234 ec->burst.expect_event = event;
238 case ACPI_EC_EVENT_OBF:
239 if (acpi_ec_read_status(ec) & event) {
240 ec->burst.expect_event = 0;
245 case ACPI_EC_EVENT_IBE:
246 if (~acpi_ec_read_status(ec) & event) {
247 ec->burst.expect_event = 0;
253 result = wait_event_timeout(ec->burst.wait,
254 !ec->burst.expect_event,
255 msecs_to_jiffies(ACPI_EC_DELAY));
257 ec->burst.expect_event = 0;
261 * Verify that the event in question has actually happened by
262 * querying EC status. Do the check even if operation timed-out
263 * to make sure that we did not miss interrupt.
266 case ACPI_EC_EVENT_OBF:
267 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
271 case ACPI_EC_EVENT_IBE:
272 if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
277 return_VALUE(-ETIME);
283 acpi_ec_enter_burst_mode (
289 ACPI_FUNCTION_TRACE("acpi_ec_enter_burst_mode");
291 status = acpi_ec_read_status(ec);
292 if (status != -EINVAL &&
293 !(status & ACPI_EC_FLAG_BURST)){
294 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
297 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE, &ec->common.command_addr);
298 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
300 return_VALUE(-EINVAL);
301 acpi_hw_low_level_read(8, &tmp, &ec->common.data_addr);
302 if(tmp != 0x90 ) {/* Burst ACK byte*/
303 return_VALUE(-EINVAL);
307 atomic_set(&ec->burst.leaving_burst , 0);
310 printk("Error in acpi_ec_wait\n");
315 acpi_ec_leave_burst_mode (
319 ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
321 atomic_set(&ec->burst.leaving_burst, 1);
331 if (acpi_ec_polling_mode)
332 return acpi_ec_polling_read(ec, address, data);
334 return acpi_ec_burst_read(ec, address, data);
342 if (acpi_ec_polling_mode)
343 return acpi_ec_polling_write(ec, address, data);
345 return acpi_ec_burst_write(ec, address, data);
348 acpi_ec_polling_read (
353 acpi_status status = AE_OK;
355 unsigned long flags = 0;
358 ACPI_FUNCTION_TRACE("acpi_ec_read");
361 return_VALUE(-EINVAL);
365 if (ec->common.global_lock) {
366 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
367 if (ACPI_FAILURE(status))
368 return_VALUE(-ENODEV);
371 spin_lock_irqsave(&ec->polling.lock, flags);
373 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->common.command_addr);
374 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
378 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
379 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
383 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
385 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
389 spin_unlock_irqrestore(&ec->polling.lock, flags);
391 if (ec->common.global_lock)
392 acpi_release_global_lock(glk);
394 return_VALUE(result);
399 acpi_ec_polling_write (
405 acpi_status status = AE_OK;
406 unsigned long flags = 0;
409 ACPI_FUNCTION_TRACE("acpi_ec_write");
412 return_VALUE(-EINVAL);
414 if (ec->common.global_lock) {
415 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
416 if (ACPI_FAILURE(status))
417 return_VALUE(-ENODEV);
420 spin_lock_irqsave(&ec->polling.lock, flags);
422 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->common.command_addr);
423 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
427 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
428 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
432 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
433 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
437 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
441 spin_unlock_irqrestore(&ec->polling.lock, flags);
443 if (ec->common.global_lock)
444 acpi_release_global_lock(glk);
446 return_VALUE(result);
458 ACPI_FUNCTION_TRACE("acpi_ec_read");
461 return_VALUE(-EINVAL);
465 if (ec->common.global_lock) {
466 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
467 if (ACPI_FAILURE(status))
468 return_VALUE(-ENODEV);
471 WARN_ON(in_interrupt());
472 down(&ec->burst.sem);
474 acpi_ec_enter_burst_mode(ec);
475 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
477 printk("read EC, IB not empty\n");
480 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->common.command_addr);
481 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
483 printk("read EC, IB not empty\n");
486 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
487 status= acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
489 printk("read EC, OB not full\n");
492 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
493 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
497 acpi_ec_leave_burst_mode(ec);
500 if (ec->common.global_lock)
501 acpi_release_global_lock(glk);
503 return_VALUE(status);
508 acpi_ec_burst_write (
516 ACPI_FUNCTION_TRACE("acpi_ec_write");
519 return_VALUE(-EINVAL);
521 if (ec->common.global_lock) {
522 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
523 if (ACPI_FAILURE(status))
524 return_VALUE(-ENODEV);
527 WARN_ON(in_interrupt());
528 down(&ec->burst.sem);
530 acpi_ec_enter_burst_mode(ec);
532 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
534 printk("write EC, IB not empty\n");
536 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->common.command_addr);
537 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
539 printk ("write EC, IB not empty\n");
542 acpi_hw_low_level_write(8, address, &ec->common.data_addr);
543 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
545 printk("write EC, IB not empty\n");
548 acpi_hw_low_level_write(8, data, &ec->common.data_addr);
550 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
553 acpi_ec_leave_burst_mode(ec);
556 if (ec->common.global_lock)
557 acpi_release_global_lock(glk);
559 return_VALUE(status);
563 * Externally callable EC access functions. For now, assume 1 EC only
566 ec_read(u8 addr, u8 *val)
575 ec = acpi_driver_data(first_ec);
577 err = acpi_ec_read(ec, addr, &temp_data);
586 EXPORT_SYMBOL(ec_read);
589 ec_write(u8 addr, u8 val)
597 ec = acpi_driver_data(first_ec);
599 err = acpi_ec_write(ec, addr, val);
603 EXPORT_SYMBOL(ec_write);
610 if (acpi_ec_polling_mode)
611 return acpi_ec_polling_query(ec, data);
613 return acpi_ec_burst_query(ec, data);
616 acpi_ec_polling_query (
621 acpi_status status = AE_OK;
622 unsigned long flags = 0;
625 ACPI_FUNCTION_TRACE("acpi_ec_query");
628 return_VALUE(-EINVAL);
632 if (ec->common.global_lock) {
633 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
634 if (ACPI_FAILURE(status))
635 return_VALUE(-ENODEV);
639 * Query the EC to find out which _Qxx method we need to evaluate.
640 * Note that successful completion of the query causes the ACPI_EC_SCI
641 * bit to be cleared (and thus clearing the interrupt source).
643 spin_lock_irqsave(&ec->polling.lock, flags);
645 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->common.command_addr);
646 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
650 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
655 spin_unlock_irqrestore(&ec->polling.lock, flags);
657 if (ec->common.global_lock)
658 acpi_release_global_lock(glk);
660 return_VALUE(result);
663 acpi_ec_burst_query (
670 ACPI_FUNCTION_TRACE("acpi_ec_query");
673 return_VALUE(-EINVAL);
676 if (ec->common.global_lock) {
677 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
678 if (ACPI_FAILURE(status))
679 return_VALUE(-ENODEV);
682 down(&ec->burst.sem);
684 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
686 printk("query EC, IB not empty\n");
690 * Query the EC to find out which _Qxx method we need to evaluate.
691 * Note that successful completion of the query causes the ACPI_EC_SCI
692 * bit to be cleared (and thus clearing the interrupt source).
694 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->common.command_addr);
695 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
697 printk("query EC, OB not full\n");
701 acpi_hw_low_level_read(8, data, &ec->common.data_addr);
708 if (ec->common.global_lock)
709 acpi_release_global_lock(glk);
711 return_VALUE(status);
715 /* --------------------------------------------------------------------------
717 -------------------------------------------------------------------------- */
719 union acpi_ec_query_data {
728 if (acpi_ec_polling_mode)
729 acpi_ec_gpe_polling_query(ec_cxt);
731 acpi_ec_gpe_burst_query(ec_cxt);
735 acpi_ec_gpe_polling_query (
738 union acpi_ec *ec = (union acpi_ec *) ec_cxt;
740 unsigned long flags = 0;
741 static char object_name[5] = {'_','Q','0','0','\0'};
742 const char hex[] = {'0','1','2','3','4','5','6','7',
743 '8','9','A','B','C','D','E','F'};
745 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
750 spin_lock_irqsave(&ec->polling.lock, flags);
751 acpi_hw_low_level_read(8, &value, &ec->common.command_addr);
752 spin_unlock_irqrestore(&ec->polling.lock, flags);
754 /* TBD: Implement asynch events!
755 * NOTE: All we care about are EC-SCI's. Other EC events are
756 * handled via polling (yuck!). This is because some systems
757 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
758 * a purely interrupt-driven approach (grumble, grumble).
760 if (!(value & ACPI_EC_FLAG_SCI))
763 if (acpi_ec_query(ec, &value))
766 object_name[2] = hex[((value >> 4) & 0x0F)];
767 object_name[3] = hex[(value & 0x0F)];
769 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
771 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
774 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
777 acpi_ec_gpe_burst_query (
780 union acpi_ec *ec = (union acpi_ec *) ec_cxt;
782 int result = -ENODATA;
783 static char object_name[5] = {'_','Q','0','0','\0'};
784 const char hex[] = {'0','1','2','3','4','5','6','7',
785 '8','9','A','B','C','D','E','F'};
787 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
789 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
790 result = acpi_ec_query(ec, &value);
795 object_name[2] = hex[((value >> 4) & 0x0F)];
796 object_name[3] = hex[(value & 0x0F)];
798 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
800 acpi_evaluate_object(ec->common.handle, object_name, NULL, NULL);
802 atomic_dec(&ec->burst.pending_gpe);
807 acpi_ec_gpe_handler (
810 if (acpi_ec_polling_mode)
811 return acpi_ec_gpe_polling_handler(data);
813 return acpi_ec_gpe_burst_handler(data);
816 acpi_ec_gpe_polling_handler (
819 acpi_status status = AE_OK;
820 union acpi_ec *ec = (union acpi_ec *) data;
823 return ACPI_INTERRUPT_NOT_HANDLED;
825 acpi_disable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
827 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
828 acpi_ec_gpe_query, ec);
831 return ACPI_INTERRUPT_HANDLED;
833 return ACPI_INTERRUPT_NOT_HANDLED;
836 acpi_ec_gpe_burst_handler (
839 acpi_status status = AE_OK;
841 union acpi_ec *ec = (union acpi_ec *) data;
844 return ACPI_INTERRUPT_NOT_HANDLED;
846 acpi_clear_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
847 value = acpi_ec_read_status(ec);
849 switch ( ec->burst.expect_event) {
850 case ACPI_EC_EVENT_OBF:
851 if (!(value & ACPI_EC_FLAG_OBF))
853 case ACPI_EC_EVENT_IBE:
854 if ((value & ACPI_EC_FLAG_IBF))
856 ec->burst.expect_event = 0;
857 wake_up(&ec->burst.wait);
858 return ACPI_INTERRUPT_HANDLED;
863 if (value & ACPI_EC_FLAG_SCI){
864 atomic_add(1, &ec->burst.pending_gpe) ;
865 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
866 acpi_ec_gpe_query, ec);
867 return status == AE_OK ?
868 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
870 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_ISR);
871 return status == AE_OK ?
872 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
875 /* --------------------------------------------------------------------------
876 Address Space Management
877 -------------------------------------------------------------------------- */
880 acpi_ec_space_setup (
881 acpi_handle region_handle,
883 void *handler_context,
884 void **return_context)
887 * The EC object is in the handler context and is needed
888 * when calling the acpi_ec_space_handler.
890 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
891 handler_context : NULL;
898 acpi_ec_space_handler (
900 acpi_physical_address address,
903 void *handler_context,
904 void *region_context)
907 union acpi_ec *ec = NULL;
909 acpi_integer f_v = 0;
912 ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
914 if ((address > 0xFF) || !value || !handler_context)
915 return_VALUE(AE_BAD_PARAMETER);
917 if (bit_width != 8 && acpi_strict) {
918 printk(KERN_WARNING PREFIX "acpi_ec_space_handler: bit_width should be 8\n");
919 return_VALUE(AE_BAD_PARAMETER);
922 ec = (union acpi_ec *) handler_context;
928 result = acpi_ec_read(ec, (u8) address, (u32 *)&temp);
931 result = acpi_ec_write(ec, (u8) address, (u8) temp);
941 if (function == ACPI_READ)
942 f_v |= temp << 8 * i;
943 if (function == ACPI_WRITE)
950 if (function == ACPI_READ) {
951 f_v |= temp << 8 * i;
959 return_VALUE(AE_BAD_PARAMETER);
962 return_VALUE(AE_NOT_FOUND);
965 return_VALUE(AE_TIME);
973 /* --------------------------------------------------------------------------
975 -------------------------------------------------------------------------- */
977 static struct proc_dir_entry *acpi_ec_dir;
981 acpi_ec_read_info (struct seq_file *seq, void *offset)
983 union acpi_ec *ec = (union acpi_ec *) seq->private;
985 ACPI_FUNCTION_TRACE("acpi_ec_read_info");
990 seq_printf(seq, "gpe bit: 0x%02x\n",
991 (u32) ec->common.gpe_bit);
992 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
993 (u32) ec->common.status_addr.address, (u32) ec->common.data_addr.address);
994 seq_printf(seq, "use global lock: %s\n",
995 ec->common.global_lock?"yes":"no");
996 acpi_enable_gpe(NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
1002 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
1004 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
1007 static struct file_operations acpi_ec_info_ops = {
1008 .open = acpi_ec_info_open_fs,
1010 .llseek = seq_lseek,
1011 .release = single_release,
1012 .owner = THIS_MODULE,
1017 struct acpi_device *device)
1019 struct proc_dir_entry *entry = NULL;
1021 ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
1023 if (!acpi_device_dir(device)) {
1024 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1026 if (!acpi_device_dir(device))
1027 return_VALUE(-ENODEV);
1030 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
1031 acpi_device_dir(device));
1033 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
1034 "Unable to create '%s' fs entry\n",
1035 ACPI_EC_FILE_INFO));
1037 entry->proc_fops = &acpi_ec_info_ops;
1038 entry->data = acpi_driver_data(device);
1039 entry->owner = THIS_MODULE;
1048 struct acpi_device *device)
1050 ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
1052 if (acpi_device_dir(device)) {
1053 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
1054 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
1055 acpi_device_dir(device) = NULL;
1062 /* --------------------------------------------------------------------------
1064 -------------------------------------------------------------------------- */
1068 acpi_ec_polling_add (
1069 struct acpi_device *device)
1072 acpi_status status = AE_OK;
1073 union acpi_ec *ec = NULL;
1076 ACPI_FUNCTION_TRACE("acpi_ec_add");
1079 return_VALUE(-EINVAL);
1081 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1083 return_VALUE(-ENOMEM);
1084 memset(ec, 0, sizeof(union acpi_ec));
1086 ec->common.handle = device->handle;
1087 ec->common.uid = -1;
1088 spin_lock_init(&ec->polling.lock);
1089 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1090 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1091 acpi_driver_data(device) = ec;
1093 /* Use the global lock for all EC transactions? */
1094 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL, &ec->common.global_lock);
1096 /* If our UID matches the UID for the ECDT-enumerated EC,
1097 we now have the *real* EC info, so kill the makeshift one.*/
1098 acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
1099 if (ec_ecdt && ec_ecdt->common.uid == uid) {
1100 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1101 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
1103 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit, &acpi_ec_gpe_handler);
1108 /* Get GPE bit assignment (EC events). */
1109 /* TODO: Add support for _GPE returning a package */
1110 status = acpi_evaluate_integer(ec->common.handle, "_GPE", NULL, &ec->common.gpe_bit);
1111 if (ACPI_FAILURE(status)) {
1112 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1113 "Error obtaining GPE bit assignment\n"));
1118 result = acpi_ec_add_fs(device);
1122 printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
1123 acpi_device_name(device), acpi_device_bid(device),
1124 (u32) ec->common.gpe_bit);
1133 return_VALUE(result);
1137 struct acpi_device *device)
1140 acpi_status status = AE_OK;
1141 union acpi_ec *ec = NULL;
1144 ACPI_FUNCTION_TRACE("acpi_ec_add");
1147 return_VALUE(-EINVAL);
1149 ec = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1151 return_VALUE(-ENOMEM);
1152 memset(ec, 0, sizeof(union acpi_ec));
1154 ec->common.handle = device->handle;
1155 ec->common.uid = -1;
1156 atomic_set(&ec->burst.pending_gpe, 0);
1157 atomic_set(&ec->burst.leaving_burst , 1);
1158 init_MUTEX(&ec->burst.sem);
1159 init_waitqueue_head(&ec->burst.wait);
1160 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
1161 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
1162 acpi_driver_data(device) = ec;
1164 /* Use the global lock for all EC transactions? */
1165 acpi_evaluate_integer(ec->common.handle, "_GLK", NULL, &ec->common.global_lock);
1167 /* If our UID matches the UID for the ECDT-enumerated EC,
1168 we now have the *real* EC info, so kill the makeshift one.*/
1169 acpi_evaluate_integer(ec->common.handle, "_UID", NULL, &uid);
1170 if (ec_ecdt && ec_ecdt->common.uid == uid) {
1171 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
1172 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
1174 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit, &acpi_ec_gpe_handler);
1179 /* Get GPE bit assignment (EC events). */
1180 /* TODO: Add support for _GPE returning a package */
1181 status = acpi_evaluate_integer(ec->common.handle, "_GPE", NULL, &ec->common.gpe_bit);
1182 if (ACPI_FAILURE(status)) {
1183 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1184 "Error obtaining GPE bit assignment\n"));
1189 result = acpi_ec_add_fs(device);
1193 printk("burst-mode-ec-10-Aug\n");
1194 printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
1195 acpi_device_name(device), acpi_device_bid(device),
1196 (u32) ec->common.gpe_bit);
1205 return_VALUE(result);
1211 struct acpi_device *device,
1214 union acpi_ec *ec = NULL;
1216 ACPI_FUNCTION_TRACE("acpi_ec_remove");
1219 return_VALUE(-EINVAL);
1221 ec = acpi_driver_data(device);
1223 acpi_ec_remove_fs(device);
1233 struct acpi_resource *resource,
1236 union acpi_ec *ec = (union acpi_ec *) context;
1237 struct acpi_generic_address *addr;
1239 if (resource->id != ACPI_RSTYPE_IO) {
1244 * The first address region returned is the data port, and
1245 * the second address region returned is the status/command
1248 if (ec->common.data_addr.register_bit_width == 0) {
1249 addr = &ec->common.data_addr;
1250 } else if (ec->common.command_addr.register_bit_width == 0) {
1251 addr = &ec->common.command_addr;
1253 return AE_CTRL_TERMINATE;
1256 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
1257 addr->register_bit_width = 8;
1258 addr->register_bit_offset = 0;
1259 addr->address = resource->data.io.min_base_address;
1267 struct acpi_device *device)
1269 acpi_status status = AE_OK;
1270 union acpi_ec *ec = NULL;
1272 ACPI_FUNCTION_TRACE("acpi_ec_start");
1275 return_VALUE(-EINVAL);
1277 ec = acpi_driver_data(device);
1280 return_VALUE(-EINVAL);
1283 * Get I/O port addresses. Convert to GAS format.
1285 status = acpi_walk_resources(ec->common.handle, METHOD_NAME__CRS,
1286 acpi_ec_io_ports, ec);
1287 if (ACPI_FAILURE(status) || ec->common.command_addr.register_bit_width == 0) {
1288 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error getting I/O port addresses"));
1289 return_VALUE(-ENODEV);
1292 ec->common.status_addr = ec->common.command_addr;
1294 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
1295 (u32) ec->common.gpe_bit, (u32) ec->common.command_addr.address,
1296 (u32) ec->common.data_addr.address));
1300 * Install GPE handler
1302 status = acpi_install_gpe_handler(NULL, ec->common.gpe_bit,
1303 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler, ec);
1304 if (ACPI_FAILURE(status)) {
1305 return_VALUE(-ENODEV);
1307 acpi_set_gpe_type (NULL, ec->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1308 acpi_enable_gpe (NULL, ec->common.gpe_bit, ACPI_NOT_ISR);
1310 status = acpi_install_address_space_handler (ec->common.handle,
1311 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
1312 &acpi_ec_space_setup, ec);
1313 if (ACPI_FAILURE(status)) {
1314 acpi_remove_gpe_handler(NULL, ec->common.gpe_bit, &acpi_ec_gpe_handler);
1315 return_VALUE(-ENODEV);
1318 return_VALUE(AE_OK);
1324 struct acpi_device *device,
1327 acpi_status status = AE_OK;
1328 union acpi_ec *ec = NULL;
1330 ACPI_FUNCTION_TRACE("acpi_ec_stop");
1333 return_VALUE(-EINVAL);
1335 ec = acpi_driver_data(device);
1337 status = acpi_remove_address_space_handler(ec->common.handle,
1338 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
1339 if (ACPI_FAILURE(status))
1340 return_VALUE(-ENODEV);
1342 status = acpi_remove_gpe_handler(NULL, ec->common.gpe_bit, &acpi_ec_gpe_handler);
1343 if (ACPI_FAILURE(status))
1344 return_VALUE(-ENODEV);
1349 static acpi_status __init
1350 acpi_fake_ecdt_callback (
1357 if (acpi_ec_polling_mode)
1358 return acpi_fake_ecdt_polling_callback(handle,
1359 Level, context, retval);
1361 return acpi_fake_ecdt_burst_callback(handle,
1362 Level, context, retval);
1365 static acpi_status __init
1366 acpi_fake_ecdt_polling_callback (
1374 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1375 acpi_ec_io_ports, ec_ecdt);
1376 if (ACPI_FAILURE(status))
1378 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1380 ec_ecdt->common.uid = -1;
1381 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1383 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->common.gpe_bit);
1384 if (ACPI_FAILURE(status))
1386 spin_lock_init(&ec_ecdt->polling.lock);
1387 ec_ecdt->common.global_lock = TRUE;
1388 ec_ecdt->common.handle = handle;
1390 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1391 (u32) ec_ecdt->common.gpe_bit, (u32) ec_ecdt->common.command_addr.address,
1392 (u32) ec_ecdt->common.data_addr.address);
1394 return AE_CTRL_TERMINATE;
1397 static acpi_status __init
1398 acpi_fake_ecdt_burst_callback (
1406 init_MUTEX(&ec_ecdt->burst.sem);
1407 init_waitqueue_head(&ec_ecdt->burst.wait);
1408 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1409 acpi_ec_io_ports, ec_ecdt);
1410 if (ACPI_FAILURE(status))
1412 ec_ecdt->common.status_addr = ec_ecdt->common.command_addr;
1414 ec_ecdt->common.uid = -1;
1415 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->common.uid);
1417 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->common.gpe_bit);
1418 if (ACPI_FAILURE(status))
1420 ec_ecdt->common.global_lock = TRUE;
1421 ec_ecdt->common.handle = handle;
1423 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1424 (u32) ec_ecdt->common.gpe_bit, (u32) ec_ecdt->common.command_addr.address,
1425 (u32) ec_ecdt->common.data_addr.address);
1427 return AE_CTRL_TERMINATE;
1431 * Some BIOS (such as some from Gateway laptops) access EC region very early
1432 * such as in BAT0._INI or EC._INI before an EC device is found and
1433 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1434 * required, but if EC regison is accessed early, it is required.
1435 * The routine tries to workaround the BIOS bug by pre-scan EC device
1436 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1437 * op region (since _REG isn't invoked yet). The assumption is true for
1438 * all systems found.
1441 acpi_ec_fake_ecdt(void)
1446 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1448 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1453 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1455 status = acpi_get_devices (ACPI_EC_HID,
1456 acpi_fake_ecdt_callback,
1459 if (ACPI_FAILURE(status)) {
1467 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1472 acpi_ec_get_real_ecdt(void)
1474 if (acpi_ec_polling_mode)
1475 return acpi_ec_polling_get_real_ecdt();
1477 return acpi_ec_burst_get_real_ecdt();
1481 acpi_ec_polling_get_real_ecdt(void)
1484 struct acpi_table_ecdt *ecdt_ptr;
1486 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1487 (struct acpi_table_header **) &ecdt_ptr);
1488 if (ACPI_FAILURE(status))
1491 printk(KERN_INFO PREFIX "Found ECDT\n");
1494 * Generate a temporary ec context to use until the namespace is scanned
1496 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1499 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1501 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1502 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1503 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1504 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1505 spin_lock_init(&ec_ecdt->polling.lock);
1506 /* use the GL just to be safe */
1507 ec_ecdt->common.global_lock = TRUE;
1508 ec_ecdt->common.uid = ecdt_ptr->uid;
1510 status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1511 if (ACPI_FAILURE(status)) {
1517 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1526 acpi_ec_burst_get_real_ecdt(void)
1529 struct acpi_table_ecdt *ecdt_ptr;
1531 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1532 (struct acpi_table_header **) &ecdt_ptr);
1533 if (ACPI_FAILURE(status))
1536 printk(KERN_INFO PREFIX "Found ECDT\n");
1539 * Generate a temporary ec context to use until the namespace is scanned
1541 ec_ecdt = kmalloc(sizeof(union acpi_ec), GFP_KERNEL);
1544 memset(ec_ecdt, 0, sizeof(union acpi_ec));
1546 init_MUTEX(&ec_ecdt->burst.sem);
1547 init_waitqueue_head(&ec_ecdt->burst.wait);
1548 ec_ecdt->common.command_addr = ecdt_ptr->ec_control;
1549 ec_ecdt->common.status_addr = ecdt_ptr->ec_control;
1550 ec_ecdt->common.data_addr = ecdt_ptr->ec_data;
1551 ec_ecdt->common.gpe_bit = ecdt_ptr->gpe_bit;
1552 /* use the GL just to be safe */
1553 ec_ecdt->common.global_lock = TRUE;
1554 ec_ecdt->common.uid = ecdt_ptr->uid;
1556 status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->common.handle);
1557 if (ACPI_FAILURE(status)) {
1563 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1570 static int __initdata acpi_fake_ecdt_enabled;
1572 acpi_ec_ecdt_probe (void)
1577 ret = acpi_ec_get_real_ecdt();
1578 /* Try to make a fake ECDT */
1579 if (ret && acpi_fake_ecdt_enabled) {
1580 ret = acpi_ec_fake_ecdt();
1587 * Install GPE handler
1589 status = acpi_install_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1590 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler,
1592 if (ACPI_FAILURE(status)) {
1595 acpi_set_gpe_type (NULL, ec_ecdt->common.gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1596 acpi_enable_gpe (NULL, ec_ecdt->common.gpe_bit, ACPI_NOT_ISR);
1598 status = acpi_install_address_space_handler (ACPI_ROOT_OBJECT,
1599 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
1600 &acpi_ec_space_setup, ec_ecdt);
1601 if (ACPI_FAILURE(status)) {
1602 acpi_remove_gpe_handler(NULL, ec_ecdt->common.gpe_bit,
1603 &acpi_ec_gpe_handler);
1610 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1618 static int __init acpi_ec_init (void)
1622 ACPI_FUNCTION_TRACE("acpi_ec_init");
1627 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1629 return_VALUE(-ENODEV);
1631 /* Now register the driver for the EC */
1632 result = acpi_bus_register_driver(&acpi_ec_driver);
1634 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1635 return_VALUE(-ENODEV);
1638 return_VALUE(result);
1641 subsys_initcall(acpi_ec_init);
1643 /* EC driver currently not unloadable */
1648 ACPI_FUNCTION_TRACE("acpi_ec_exit");
1650 acpi_bus_unregister_driver(&acpi_ec_driver);
1652 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1658 static int __init acpi_fake_ecdt_setup(char *str)
1660 acpi_fake_ecdt_enabled = 1;
1664 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1665 static int __init acpi_ec_set_polling_mode(char *str)
1669 if (!get_option(&str, &burst))
1673 acpi_ec_polling_mode = EC_BURST;
1674 acpi_ec_driver.ops.add = acpi_ec_burst_add;
1676 acpi_ec_polling_mode = EC_POLLING;
1677 acpi_ec_driver.ops.add = acpi_ec_polling_add;
1679 printk(KERN_INFO PREFIX "EC %s mode.\n",
1680 burst ? "burst": "polling");
1683 __setup("ec_burst=", acpi_ec_set_polling_mode);