2 * ec.c - ACPI Embedded Controller Driver (v2.0)
4 * Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
6 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
7 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 /* Uncomment next line to get verbose print outs*/
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/types.h>
36 #include <linux/delay.h>
37 #include <linux/proc_fs.h>
38 #include <linux/seq_file.h>
39 #include <linux/interrupt.h>
40 #include <linux/list.h>
42 #include <acpi/acpi_bus.h>
43 #include <acpi/acpi_drivers.h>
44 #include <acpi/actypes.h>
46 #define ACPI_EC_CLASS "embedded_controller"
47 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
48 #define ACPI_EC_FILE_INFO "info"
51 #define PREFIX "ACPI: EC: "
53 /* EC status register */
54 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
55 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
56 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
57 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
61 ACPI_EC_COMMAND_READ
= 0x80,
62 ACPI_EC_COMMAND_WRITE
= 0x81,
63 ACPI_EC_BURST_ENABLE
= 0x82,
64 ACPI_EC_BURST_DISABLE
= 0x83,
65 ACPI_EC_COMMAND_QUERY
= 0x84,
70 ACPI_EC_EVENT_OBF_1
= 1, /* Output buffer full */
71 ACPI_EC_EVENT_IBF_0
, /* Input buffer empty */
74 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
75 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
78 EC_FLAGS_WAIT_GPE
= 0, /* Don't check status until GPE arrives */
79 EC_FLAGS_QUERY_PENDING
, /* Query is pending */
80 EC_FLAGS_GPE_MODE
, /* Expect GPE to be sent for status change */
81 EC_FLAGS_NO_ADDRESS_GPE
, /* Expect GPE only for non-address event */
82 EC_FLAGS_ADDRESS
, /* Address is being written */
83 EC_FLAGS_NO_WDATA_GPE
, /* Don't expect WDATA GPE event */
84 EC_FLAGS_WDATA
, /* Data is being written */
85 EC_FLAGS_NO_OBF1_GPE
, /* Don't expect GPE before read */
88 static int acpi_ec_remove(struct acpi_device
*device
, int type
);
89 static int acpi_ec_start(struct acpi_device
*device
);
90 static int acpi_ec_stop(struct acpi_device
*device
, int type
);
91 static int acpi_ec_add(struct acpi_device
*device
);
93 static const struct acpi_device_id ec_device_ids
[] = {
98 static struct acpi_driver acpi_ec_driver
= {
100 .class = ACPI_EC_CLASS
,
101 .ids
= ec_device_ids
,
104 .remove
= acpi_ec_remove
,
105 .start
= acpi_ec_start
,
106 .stop
= acpi_ec_stop
,
110 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
111 /* External interfaces use first EC only, so remember */
112 typedef int (*acpi_ec_query_func
) (void *data
);
114 struct acpi_ec_query_handler
{
115 struct list_head node
;
116 acpi_ec_query_func func
;
122 static struct acpi_ec
{
125 unsigned long command_addr
;
126 unsigned long data_addr
;
127 unsigned long global_lock
;
130 wait_queue_head_t wait
;
131 struct list_head list
;
133 u8 handlers_installed
;
134 } *boot_ec
, *first_ec
;
136 /* --------------------------------------------------------------------------
137 Transaction Management
138 -------------------------------------------------------------------------- */
140 static inline u8
acpi_ec_read_status(struct acpi_ec
*ec
)
142 u8 x
= inb(ec
->command_addr
);
143 pr_debug(PREFIX
"---> status = 0x%2.2x\n", x
);
147 static inline u8
acpi_ec_read_data(struct acpi_ec
*ec
)
149 u8 x
= inb(ec
->data_addr
);
150 pr_debug(PREFIX
"---> data = 0x%2.2x\n", x
);
151 return inb(ec
->data_addr
);
154 static inline void acpi_ec_write_cmd(struct acpi_ec
*ec
, u8 command
)
156 pr_debug(PREFIX
"<--- command = 0x%2.2x\n", command
);
157 outb(command
, ec
->command_addr
);
160 static inline void acpi_ec_write_data(struct acpi_ec
*ec
, u8 data
)
162 pr_debug(PREFIX
"<--- data = 0x%2.2x\n", data
);
163 outb(data
, ec
->data_addr
);
166 static inline int acpi_ec_check_status(struct acpi_ec
*ec
, enum ec_event event
)
168 if (test_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
))
170 if (event
== ACPI_EC_EVENT_OBF_1
) {
171 if (acpi_ec_read_status(ec
) & ACPI_EC_FLAG_OBF
)
173 } else if (event
== ACPI_EC_EVENT_IBF_0
) {
174 if (!(acpi_ec_read_status(ec
) & ACPI_EC_FLAG_IBF
))
181 static int acpi_ec_wait(struct acpi_ec
*ec
, enum ec_event event
, int force_poll
)
185 atomic_set(&ec
->irq_count
, 0);
187 if (unlikely(event
== ACPI_EC_EVENT_OBF_1
&&
188 test_bit(EC_FLAGS_NO_OBF1_GPE
, &ec
->flags
)))
190 if (unlikely(test_bit(EC_FLAGS_ADDRESS
, &ec
->flags
) &&
191 test_bit(EC_FLAGS_NO_ADDRESS_GPE
, &ec
->flags
)))
193 if (unlikely(test_bit(EC_FLAGS_WDATA
, &ec
->flags
) &&
194 test_bit(EC_FLAGS_NO_WDATA_GPE
, &ec
->flags
)))
196 if (likely(test_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
)) &&
197 likely(!force_poll
)) {
198 if (wait_event_timeout(ec
->wait
, acpi_ec_check_status(ec
, event
),
199 msecs_to_jiffies(ACPI_EC_DELAY
)))
201 clear_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
202 if (acpi_ec_check_status(ec
, event
)) {
203 if (event
== ACPI_EC_EVENT_OBF_1
) {
204 /* miss OBF_1 GPE, don't expect it */
205 pr_info(PREFIX
"missing OBF confirmation, "
206 "don't expect it any longer.\n");
207 set_bit(EC_FLAGS_NO_OBF1_GPE
, &ec
->flags
);
208 } else if (test_bit(EC_FLAGS_ADDRESS
, &ec
->flags
)) {
209 /* miss address GPE, don't expect it anymore */
210 pr_info(PREFIX
"missing address confirmation, "
211 "don't expect it any longer.\n");
212 set_bit(EC_FLAGS_NO_ADDRESS_GPE
, &ec
->flags
);
213 } else if (test_bit(EC_FLAGS_WDATA
, &ec
->flags
)) {
214 /* miss write data GPE, don't expect it */
215 pr_info(PREFIX
"missing write data confirmation, "
216 "don't expect it any longer.\n");
217 set_bit(EC_FLAGS_NO_WDATA_GPE
, &ec
->flags
);
219 /* missing GPEs, switch back to poll mode */
220 if (printk_ratelimit())
221 pr_info(PREFIX
"missing confirmations, "
222 "switch off interrupt mode.\n");
223 clear_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
);
228 unsigned long delay
= jiffies
+ msecs_to_jiffies(ACPI_EC_DELAY
);
229 clear_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
230 while (time_before(jiffies
, delay
)) {
231 if (acpi_ec_check_status(ec
, event
))
236 pr_err(PREFIX
"acpi_ec_wait timeout,"
237 " status = %d, expect_event = %d\n",
238 acpi_ec_read_status(ec
), event
);
241 clear_bit(EC_FLAGS_ADDRESS
, &ec
->flags
);
245 static int acpi_ec_transaction_unlocked(struct acpi_ec
*ec
, u8 command
,
246 const u8
* wdata
, unsigned wdata_len
,
247 u8
* rdata
, unsigned rdata_len
,
251 set_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
252 acpi_ec_write_cmd(ec
, command
);
253 pr_debug(PREFIX
"transaction start\n");
254 for (; wdata_len
> 0; --wdata_len
) {
255 result
= acpi_ec_wait(ec
, ACPI_EC_EVENT_IBF_0
, force_poll
);
258 "write_cmd timeout, command = %d\n", command
);
261 /* mark the address byte written to EC */
262 if (rdata_len
+ wdata_len
> 1)
263 set_bit(EC_FLAGS_ADDRESS
, &ec
->flags
);
264 set_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
265 acpi_ec_write_data(ec
, *(wdata
++));
269 set_bit(EC_FLAGS_WDATA
, &ec
->flags
);
270 result
= acpi_ec_wait(ec
, ACPI_EC_EVENT_IBF_0
, force_poll
);
273 "finish-write timeout, command = %d\n", command
);
276 } else if (command
== ACPI_EC_COMMAND_QUERY
)
277 clear_bit(EC_FLAGS_QUERY_PENDING
, &ec
->flags
);
279 for (; rdata_len
> 0; --rdata_len
) {
280 result
= acpi_ec_wait(ec
, ACPI_EC_EVENT_OBF_1
, force_poll
);
282 pr_err(PREFIX
"read timeout, command = %d\n", command
);
285 /* Don't expect GPE after last read */
287 set_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
288 *(rdata
++) = acpi_ec_read_data(ec
);
291 pr_debug(PREFIX
"transaction end\n");
295 static int acpi_ec_transaction(struct acpi_ec
*ec
, u8 command
,
296 const u8
* wdata
, unsigned wdata_len
,
297 u8
* rdata
, unsigned rdata_len
,
303 if (!ec
|| (wdata_len
&& !wdata
) || (rdata_len
&& !rdata
))
307 memset(rdata
, 0, rdata_len
);
309 mutex_lock(&ec
->lock
);
310 if (ec
->global_lock
) {
311 status
= acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK
, &glk
);
312 if (ACPI_FAILURE(status
)) {
313 mutex_unlock(&ec
->lock
);
318 status
= acpi_ec_wait(ec
, ACPI_EC_EVENT_IBF_0
, 0);
320 pr_err(PREFIX
"input buffer is not empty, "
321 "aborting transaction\n");
325 status
= acpi_ec_transaction_unlocked(ec
, command
,
333 acpi_release_global_lock(glk
);
334 mutex_unlock(&ec
->lock
);
340 * Note: samsung nv5000 doesn't work with ec burst mode.
341 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
343 int acpi_ec_burst_enable(struct acpi_ec
*ec
)
346 return acpi_ec_transaction(ec
, ACPI_EC_BURST_ENABLE
, NULL
, 0, &d
, 1, 0);
349 int acpi_ec_burst_disable(struct acpi_ec
*ec
)
351 return acpi_ec_transaction(ec
, ACPI_EC_BURST_DISABLE
, NULL
, 0, NULL
, 0, 0);
354 static int acpi_ec_read(struct acpi_ec
*ec
, u8 address
, u8
* data
)
359 result
= acpi_ec_transaction(ec
, ACPI_EC_COMMAND_READ
,
360 &address
, 1, &d
, 1, 0);
365 static int acpi_ec_write(struct acpi_ec
*ec
, u8 address
, u8 data
)
367 u8 wdata
[2] = { address
, data
};
368 return acpi_ec_transaction(ec
, ACPI_EC_COMMAND_WRITE
,
369 wdata
, 2, NULL
, 0, 0);
373 * Externally callable EC access functions. For now, assume 1 EC only
375 int ec_burst_enable(void)
379 return acpi_ec_burst_enable(first_ec
);
382 EXPORT_SYMBOL(ec_burst_enable
);
384 int ec_burst_disable(void)
388 return acpi_ec_burst_disable(first_ec
);
391 EXPORT_SYMBOL(ec_burst_disable
);
393 int ec_read(u8 addr
, u8
* val
)
401 err
= acpi_ec_read(first_ec
, addr
, &temp_data
);
410 EXPORT_SYMBOL(ec_read
);
412 int ec_write(u8 addr
, u8 val
)
419 err
= acpi_ec_write(first_ec
, addr
, val
);
424 EXPORT_SYMBOL(ec_write
);
426 int ec_transaction(u8 command
,
427 const u8
* wdata
, unsigned wdata_len
,
428 u8
* rdata
, unsigned rdata_len
,
434 return acpi_ec_transaction(first_ec
, command
, wdata
,
435 wdata_len
, rdata
, rdata_len
,
439 EXPORT_SYMBOL(ec_transaction
);
441 static int acpi_ec_query(struct acpi_ec
*ec
, u8
* data
)
450 * Query the EC to find out which _Qxx method we need to evaluate.
451 * Note that successful completion of the query causes the ACPI_EC_SCI
452 * bit to be cleared (and thus clearing the interrupt source).
455 result
= acpi_ec_transaction(ec
, ACPI_EC_COMMAND_QUERY
, NULL
, 0, &d
, 1, 0);
466 /* --------------------------------------------------------------------------
468 -------------------------------------------------------------------------- */
469 int acpi_ec_add_query_handler(struct acpi_ec
*ec
, u8 query_bit
,
470 acpi_handle handle
, acpi_ec_query_func func
,
473 struct acpi_ec_query_handler
*handler
=
474 kzalloc(sizeof(struct acpi_ec_query_handler
), GFP_KERNEL
);
478 handler
->query_bit
= query_bit
;
479 handler
->handle
= handle
;
480 handler
->func
= func
;
481 handler
->data
= data
;
482 mutex_lock(&ec
->lock
);
483 list_add(&handler
->node
, &ec
->list
);
484 mutex_unlock(&ec
->lock
);
488 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler
);
490 void acpi_ec_remove_query_handler(struct acpi_ec
*ec
, u8 query_bit
)
492 struct acpi_ec_query_handler
*handler
, *tmp
;
493 mutex_lock(&ec
->lock
);
494 list_for_each_entry_safe(handler
, tmp
, &ec
->list
, node
) {
495 if (query_bit
== handler
->query_bit
) {
496 list_del(&handler
->node
);
500 mutex_unlock(&ec
->lock
);
503 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler
);
505 static void acpi_ec_gpe_query(void *ec_cxt
)
507 struct acpi_ec
*ec
= ec_cxt
;
509 struct acpi_ec_query_handler
*handler
, copy
;
511 if (!ec
|| acpi_ec_query(ec
, &value
))
513 mutex_lock(&ec
->lock
);
514 list_for_each_entry(handler
, &ec
->list
, node
) {
515 if (value
== handler
->query_bit
) {
516 /* have custom handler for this bit */
517 memcpy(©
, handler
, sizeof(copy
));
518 mutex_unlock(&ec
->lock
);
520 copy
.func(copy
.data
);
521 } else if (copy
.handle
) {
522 acpi_evaluate_object(copy
.handle
, NULL
, NULL
, NULL
);
527 mutex_unlock(&ec
->lock
);
530 static u32
acpi_ec_gpe_handler(void *data
)
532 acpi_status status
= AE_OK
;
533 struct acpi_ec
*ec
= data
;
535 pr_debug(PREFIX
"~~~> interrupt\n");
536 atomic_inc(&ec
->irq_count
);
537 if (atomic_read(&ec
->irq_count
) > 5) {
538 pr_err(PREFIX
"GPE storm detected, disabling EC GPE\n");
539 acpi_disable_gpe(NULL
, ec
->gpe
, ACPI_ISR
);
540 clear_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
);
541 return ACPI_INTERRUPT_HANDLED
;
543 clear_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
544 if (test_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
))
547 if (acpi_ec_read_status(ec
) & ACPI_EC_FLAG_SCI
) {
548 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING
, &ec
->flags
))
549 status
= acpi_os_execute(OSL_EC_BURST_HANDLER
,
550 acpi_ec_gpe_query
, ec
);
551 } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
))) {
552 /* this is non-query, must be confirmation */
553 if (printk_ratelimit())
554 pr_info(PREFIX
"non-query interrupt received,"
555 " switching to interrupt mode\n");
556 set_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
);
559 return ACPI_SUCCESS(status
) ?
560 ACPI_INTERRUPT_HANDLED
: ACPI_INTERRUPT_NOT_HANDLED
;
563 /* --------------------------------------------------------------------------
564 Address Space Management
565 -------------------------------------------------------------------------- */
568 acpi_ec_space_setup(acpi_handle region_handle
,
569 u32 function
, void *handler_context
, void **return_context
)
572 * The EC object is in the handler context and is needed
573 * when calling the acpi_ec_space_handler.
575 *return_context
= (function
!= ACPI_REGION_DEACTIVATE
) ?
576 handler_context
: NULL
;
582 acpi_ec_space_handler(u32 function
, acpi_physical_address address
,
583 u32 bits
, acpi_integer
*value
,
584 void *handler_context
, void *region_context
)
586 struct acpi_ec
*ec
= handler_context
;
590 if ((address
> 0xFF) || !value
|| !handler_context
)
591 return AE_BAD_PARAMETER
;
593 if (function
!= ACPI_READ
&& function
!= ACPI_WRITE
)
594 return AE_BAD_PARAMETER
;
596 if (bits
!= 8 && acpi_strict
)
597 return AE_BAD_PARAMETER
;
599 acpi_ec_burst_enable(ec
);
601 if (function
== ACPI_READ
) {
602 result
= acpi_ec_read(ec
, address
, &temp
);
605 temp
= 0xff & (*value
);
606 result
= acpi_ec_write(ec
, address
, temp
);
609 for (i
= 8; unlikely(bits
- i
> 0); i
+= 8) {
611 if (function
== ACPI_READ
) {
612 result
= acpi_ec_read(ec
, address
, &temp
);
613 (*value
) |= ((acpi_integer
)temp
) << i
;
615 temp
= 0xff & ((*value
) >> i
);
616 result
= acpi_ec_write(ec
, address
, temp
);
620 acpi_ec_burst_disable(ec
);
624 return AE_BAD_PARAMETER
;
637 /* --------------------------------------------------------------------------
639 -------------------------------------------------------------------------- */
641 static struct proc_dir_entry
*acpi_ec_dir
;
643 static int acpi_ec_read_info(struct seq_file
*seq
, void *offset
)
645 struct acpi_ec
*ec
= seq
->private;
650 seq_printf(seq
, "gpe:\t\t\t0x%02x\n", (u32
) ec
->gpe
);
651 seq_printf(seq
, "ports:\t\t\t0x%02x, 0x%02x\n",
652 (unsigned)ec
->command_addr
, (unsigned)ec
->data_addr
);
653 seq_printf(seq
, "use global lock:\t%s\n",
654 ec
->global_lock
? "yes" : "no");
659 static int acpi_ec_info_open_fs(struct inode
*inode
, struct file
*file
)
661 return single_open(file
, acpi_ec_read_info
, PDE(inode
)->data
);
664 static struct file_operations acpi_ec_info_ops
= {
665 .open
= acpi_ec_info_open_fs
,
668 .release
= single_release
,
669 .owner
= THIS_MODULE
,
672 static int acpi_ec_add_fs(struct acpi_device
*device
)
674 struct proc_dir_entry
*entry
= NULL
;
676 if (!acpi_device_dir(device
)) {
677 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
),
679 if (!acpi_device_dir(device
))
683 entry
= create_proc_entry(ACPI_EC_FILE_INFO
, S_IRUGO
,
684 acpi_device_dir(device
));
688 entry
->proc_fops
= &acpi_ec_info_ops
;
689 entry
->data
= acpi_driver_data(device
);
690 entry
->owner
= THIS_MODULE
;
696 static int acpi_ec_remove_fs(struct acpi_device
*device
)
699 if (acpi_device_dir(device
)) {
700 remove_proc_entry(ACPI_EC_FILE_INFO
, acpi_device_dir(device
));
701 remove_proc_entry(acpi_device_bid(device
), acpi_ec_dir
);
702 acpi_device_dir(device
) = NULL
;
708 /* --------------------------------------------------------------------------
710 -------------------------------------------------------------------------- */
712 ec_parse_io_ports(struct acpi_resource
*resource
, void *context
);
714 static struct acpi_ec
*make_acpi_ec(void)
716 struct acpi_ec
*ec
= kzalloc(sizeof(struct acpi_ec
), GFP_KERNEL
);
719 ec
->flags
= 1 << EC_FLAGS_QUERY_PENDING
;
720 mutex_init(&ec
->lock
);
721 init_waitqueue_head(&ec
->wait
);
722 INIT_LIST_HEAD(&ec
->list
);
727 acpi_ec_register_query_methods(acpi_handle handle
, u32 level
,
728 void *context
, void **return_value
)
730 struct acpi_namespace_node
*node
= handle
;
731 struct acpi_ec
*ec
= context
;
733 if (sscanf(node
->name
.ascii
, "_Q%x", &value
) == 1) {
734 acpi_ec_add_query_handler(ec
, value
, handle
, NULL
, NULL
);
740 ec_parse_device(acpi_handle handle
, u32 Level
, void *context
, void **retval
)
744 struct acpi_ec
*ec
= context
;
745 status
= acpi_walk_resources(handle
, METHOD_NAME__CRS
,
746 ec_parse_io_ports
, ec
);
747 if (ACPI_FAILURE(status
))
750 /* Get GPE bit assignment (EC events). */
751 /* TODO: Add support for _GPE returning a package */
752 status
= acpi_evaluate_integer(handle
, "_GPE", NULL
, &ec
->gpe
);
753 if (ACPI_FAILURE(status
))
755 /* Find and register all query methods */
756 acpi_walk_namespace(ACPI_TYPE_METHOD
, handle
, 1,
757 acpi_ec_register_query_methods
, ec
, NULL
);
758 /* Use the global lock for all EC transactions? */
759 acpi_evaluate_integer(handle
, "_GLK", NULL
, &ec
->global_lock
);
761 return AE_CTRL_TERMINATE
;
764 static void ec_remove_handlers(struct acpi_ec
*ec
)
766 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec
->handle
,
767 ACPI_ADR_SPACE_EC
, &acpi_ec_space_handler
)))
768 pr_err(PREFIX
"failed to remove space handler\n");
769 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL
, ec
->gpe
,
770 &acpi_ec_gpe_handler
)))
771 pr_err(PREFIX
"failed to remove gpe handler\n");
772 ec
->handlers_installed
= 0;
775 static int acpi_ec_add(struct acpi_device
*device
)
777 struct acpi_ec
*ec
= NULL
;
781 strcpy(acpi_device_name(device
), ACPI_EC_DEVICE_NAME
);
782 strcpy(acpi_device_class(device
), ACPI_EC_CLASS
);
784 /* Check for boot EC */
786 if (boot_ec
->handle
== device
->handle
) {
787 /* Pre-loaded EC from DSDT, just move pointer */
791 } else if (boot_ec
->handle
== ACPI_ROOT_OBJECT
) {
792 /* ECDT-based EC, time to shut it down */
793 ec_remove_handlers(boot_ec
);
795 first_ec
= boot_ec
= NULL
;
803 if (ec_parse_device(device
->handle
, 0, ec
, NULL
) !=
808 ec
->handle
= device
->handle
;
812 acpi_driver_data(device
) = ec
;
813 acpi_ec_add_fs(device
);
814 pr_info(PREFIX
"GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
815 ec
->gpe
, ec
->command_addr
, ec
->data_addr
);
816 pr_info(PREFIX
"driver started in %s mode\n",
817 (test_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
))?"interrupt":"poll");
821 static int acpi_ec_remove(struct acpi_device
*device
, int type
)
824 struct acpi_ec_query_handler
*handler
, *tmp
;
829 ec
= acpi_driver_data(device
);
830 mutex_lock(&ec
->lock
);
831 list_for_each_entry_safe(handler
, tmp
, &ec
->list
, node
) {
832 list_del(&handler
->node
);
835 mutex_unlock(&ec
->lock
);
836 acpi_ec_remove_fs(device
);
837 acpi_driver_data(device
) = NULL
;
845 ec_parse_io_ports(struct acpi_resource
*resource
, void *context
)
847 struct acpi_ec
*ec
= context
;
849 if (resource
->type
!= ACPI_RESOURCE_TYPE_IO
)
853 * The first address region returned is the data port, and
854 * the second address region returned is the status/command
857 if (ec
->data_addr
== 0)
858 ec
->data_addr
= resource
->data
.io
.minimum
;
859 else if (ec
->command_addr
== 0)
860 ec
->command_addr
= resource
->data
.io
.minimum
;
862 return AE_CTRL_TERMINATE
;
867 static int ec_install_handlers(struct acpi_ec
*ec
)
870 if (ec
->handlers_installed
)
872 status
= acpi_install_gpe_handler(NULL
, ec
->gpe
,
873 ACPI_GPE_EDGE_TRIGGERED
,
874 &acpi_ec_gpe_handler
, ec
);
875 if (ACPI_FAILURE(status
))
878 acpi_set_gpe_type(NULL
, ec
->gpe
, ACPI_GPE_TYPE_RUNTIME
);
879 acpi_enable_gpe(NULL
, ec
->gpe
, ACPI_NOT_ISR
);
881 status
= acpi_install_address_space_handler(ec
->handle
,
883 &acpi_ec_space_handler
,
884 &acpi_ec_space_setup
, ec
);
885 if (ACPI_FAILURE(status
)) {
886 acpi_remove_gpe_handler(NULL
, ec
->gpe
, &acpi_ec_gpe_handler
);
890 ec
->handlers_installed
= 1;
894 static int acpi_ec_start(struct acpi_device
*device
)
902 ec
= acpi_driver_data(device
);
907 ret
= ec_install_handlers(ec
);
909 /* EC is fully operational, allow queries */
910 clear_bit(EC_FLAGS_QUERY_PENDING
, &ec
->flags
);
914 static int acpi_ec_stop(struct acpi_device
*device
, int type
)
919 ec
= acpi_driver_data(device
);
922 ec_remove_handlers(ec
);
927 int __init
acpi_boot_ec_enable(void)
929 if (!boot_ec
|| boot_ec
->handlers_installed
)
931 if (!ec_install_handlers(boot_ec
)) {
938 int __init
acpi_ec_ecdt_probe(void)
942 struct acpi_table_ecdt
*ecdt_ptr
;
944 boot_ec
= make_acpi_ec();
948 * Generate a boot ec context
950 status
= acpi_get_table(ACPI_SIG_ECDT
, 1,
951 (struct acpi_table_header
**)&ecdt_ptr
);
952 if (ACPI_SUCCESS(status
)) {
953 pr_info(PREFIX
"EC description table is found, configuring boot EC\n");
954 boot_ec
->command_addr
= ecdt_ptr
->control
.address
;
955 boot_ec
->data_addr
= ecdt_ptr
->data
.address
;
956 boot_ec
->gpe
= ecdt_ptr
->gpe
;
957 boot_ec
->handle
= ACPI_ROOT_OBJECT
;
959 /* This workaround is needed only on some broken machines,
960 * which require early EC, but fail to provide ECDT */
962 printk(KERN_DEBUG PREFIX
"Look up EC in DSDT\n");
963 status
= acpi_get_devices(ec_device_ids
[0].id
, ec_parse_device
,
965 /* Check that acpi_get_devices actually find something */
966 if (ACPI_FAILURE(status
) || !boot_ec
->handle
)
968 /* We really need to limit this workaround, the only ASUS,
969 * which needs it, has fake EC._INI method, so use it as flag.
970 * Keep boot_ec struct as it will be needed soon.
972 if (ACPI_FAILURE(acpi_get_handle(boot_ec
->handle
, "_INI", &x
)))
976 ret
= ec_install_handlers(boot_ec
);
987 static int __init
acpi_ec_init(void)
994 acpi_ec_dir
= proc_mkdir(ACPI_EC_CLASS
, acpi_root_dir
);
998 /* Now register the driver for the EC */
999 result
= acpi_bus_register_driver(&acpi_ec_driver
);
1001 remove_proc_entry(ACPI_EC_CLASS
, acpi_root_dir
);
1008 subsys_initcall(acpi_ec_init
);
1010 /* EC driver currently not unloadable */
1012 static void __exit
acpi_ec_exit(void)
1015 acpi_bus_unregister_driver(&acpi_ec_driver
);
1017 remove_proc_entry(ACPI_EC_CLASS
, acpi_root_dir
);