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
;
132 u8 handlers_installed
;
133 } *boot_ec
, *first_ec
;
135 /* --------------------------------------------------------------------------
136 Transaction Management
137 -------------------------------------------------------------------------- */
139 static inline u8
acpi_ec_read_status(struct acpi_ec
*ec
)
141 u8 x
= inb(ec
->command_addr
);
142 pr_debug(PREFIX
"---> status = 0x%2.2x\n", x
);
146 static inline u8
acpi_ec_read_data(struct acpi_ec
*ec
)
148 u8 x
= inb(ec
->data_addr
);
149 pr_debug(PREFIX
"---> data = 0x%2.2x\n", x
);
150 return inb(ec
->data_addr
);
153 static inline void acpi_ec_write_cmd(struct acpi_ec
*ec
, u8 command
)
155 pr_debug(PREFIX
"<--- command = 0x%2.2x\n", command
);
156 outb(command
, ec
->command_addr
);
159 static inline void acpi_ec_write_data(struct acpi_ec
*ec
, u8 data
)
161 pr_debug(PREFIX
"<--- data = 0x%2.2x\n", data
);
162 outb(data
, ec
->data_addr
);
165 static inline int acpi_ec_check_status(struct acpi_ec
*ec
, enum ec_event event
)
167 if (test_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
))
169 if (event
== ACPI_EC_EVENT_OBF_1
) {
170 if (acpi_ec_read_status(ec
) & ACPI_EC_FLAG_OBF
)
172 } else if (event
== ACPI_EC_EVENT_IBF_0
) {
173 if (!(acpi_ec_read_status(ec
) & ACPI_EC_FLAG_IBF
))
180 static int acpi_ec_wait(struct acpi_ec
*ec
, enum ec_event event
, int force_poll
)
184 if (unlikely(event
== ACPI_EC_EVENT_OBF_1
&&
185 test_bit(EC_FLAGS_NO_OBF1_GPE
, &ec
->flags
)))
187 if (unlikely(test_bit(EC_FLAGS_ADDRESS
, &ec
->flags
) &&
188 test_bit(EC_FLAGS_NO_ADDRESS_GPE
, &ec
->flags
)))
190 if (unlikely(test_bit(EC_FLAGS_WDATA
, &ec
->flags
) &&
191 test_bit(EC_FLAGS_NO_WDATA_GPE
, &ec
->flags
)))
193 if (likely(test_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
)) &&
194 likely(!force_poll
)) {
195 if (wait_event_timeout(ec
->wait
, acpi_ec_check_status(ec
, event
),
196 msecs_to_jiffies(ACPI_EC_DELAY
)))
198 clear_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
199 if (acpi_ec_check_status(ec
, event
)) {
200 if (event
== ACPI_EC_EVENT_OBF_1
) {
201 /* miss OBF_1 GPE, don't expect it */
202 pr_info(PREFIX
"missing OBF confirmation, "
203 "don't expect it any longer.\n");
204 set_bit(EC_FLAGS_NO_OBF1_GPE
, &ec
->flags
);
205 } else if (test_bit(EC_FLAGS_ADDRESS
, &ec
->flags
)) {
206 /* miss address GPE, don't expect it anymore */
207 pr_info(PREFIX
"missing address confirmation, "
208 "don't expect it any longer.\n");
209 set_bit(EC_FLAGS_NO_ADDRESS_GPE
, &ec
->flags
);
210 } else if (test_bit(EC_FLAGS_WDATA
, &ec
->flags
)) {
211 /* miss write data GPE, don't expect it */
212 pr_info(PREFIX
"missing write data confirmation, "
213 "don't expect it any longer.\n");
214 set_bit(EC_FLAGS_NO_WDATA_GPE
, &ec
->flags
);
216 /* missing GPEs, switch back to poll mode */
217 if (printk_ratelimit())
218 pr_info(PREFIX
"missing confirmations, "
219 "switch off interrupt mode.\n");
220 clear_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
);
225 unsigned long delay
= jiffies
+ msecs_to_jiffies(ACPI_EC_DELAY
);
226 clear_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
227 while (time_before(jiffies
, delay
)) {
228 if (acpi_ec_check_status(ec
, event
))
232 pr_err(PREFIX
"acpi_ec_wait timeout,"
233 " status = %d, expect_event = %d\n",
234 acpi_ec_read_status(ec
), event
);
237 clear_bit(EC_FLAGS_ADDRESS
, &ec
->flags
);
241 static int acpi_ec_transaction_unlocked(struct acpi_ec
*ec
, u8 command
,
242 const u8
* wdata
, unsigned wdata_len
,
243 u8
* rdata
, unsigned rdata_len
,
247 set_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
248 acpi_ec_write_cmd(ec
, command
);
249 pr_debug(PREFIX
"transaction start\n");
250 for (; wdata_len
> 0; --wdata_len
) {
251 result
= acpi_ec_wait(ec
, ACPI_EC_EVENT_IBF_0
, force_poll
);
254 "write_cmd timeout, command = %d\n", command
);
257 /* mark the address byte written to EC */
258 if (rdata_len
+ wdata_len
> 1)
259 set_bit(EC_FLAGS_ADDRESS
, &ec
->flags
);
260 set_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
261 acpi_ec_write_data(ec
, *(wdata
++));
265 set_bit(EC_FLAGS_WDATA
, &ec
->flags
);
266 result
= acpi_ec_wait(ec
, ACPI_EC_EVENT_IBF_0
, force_poll
);
269 "finish-write timeout, command = %d\n", command
);
272 } else if (command
== ACPI_EC_COMMAND_QUERY
)
273 clear_bit(EC_FLAGS_QUERY_PENDING
, &ec
->flags
);
275 for (; rdata_len
> 0; --rdata_len
) {
276 result
= acpi_ec_wait(ec
, ACPI_EC_EVENT_OBF_1
, force_poll
);
278 pr_err(PREFIX
"read timeout, command = %d\n", command
);
281 /* Don't expect GPE after last read */
283 set_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
284 *(rdata
++) = acpi_ec_read_data(ec
);
287 pr_debug(PREFIX
"transaction end\n");
291 static int acpi_ec_transaction(struct acpi_ec
*ec
, u8 command
,
292 const u8
* wdata
, unsigned wdata_len
,
293 u8
* rdata
, unsigned rdata_len
,
299 if (!ec
|| (wdata_len
&& !wdata
) || (rdata_len
&& !rdata
))
303 memset(rdata
, 0, rdata_len
);
305 mutex_lock(&ec
->lock
);
306 if (ec
->global_lock
) {
307 status
= acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK
, &glk
);
308 if (ACPI_FAILURE(status
)) {
309 mutex_unlock(&ec
->lock
);
314 status
= acpi_ec_wait(ec
, ACPI_EC_EVENT_IBF_0
, 0);
316 pr_err(PREFIX
"input buffer is not empty, "
317 "aborting transaction\n");
321 status
= acpi_ec_transaction_unlocked(ec
, command
,
329 acpi_release_global_lock(glk
);
330 mutex_unlock(&ec
->lock
);
336 * Note: samsung nv5000 doesn't work with ec burst mode.
337 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
339 int acpi_ec_burst_enable(struct acpi_ec
*ec
)
342 return acpi_ec_transaction(ec
, ACPI_EC_BURST_ENABLE
, NULL
, 0, &d
, 1, 0);
345 int acpi_ec_burst_disable(struct acpi_ec
*ec
)
347 return acpi_ec_transaction(ec
, ACPI_EC_BURST_DISABLE
, NULL
, 0, NULL
, 0, 0);
350 static int acpi_ec_read(struct acpi_ec
*ec
, u8 address
, u8
* data
)
355 result
= acpi_ec_transaction(ec
, ACPI_EC_COMMAND_READ
,
356 &address
, 1, &d
, 1, 0);
361 static int acpi_ec_write(struct acpi_ec
*ec
, u8 address
, u8 data
)
363 u8 wdata
[2] = { address
, data
};
364 return acpi_ec_transaction(ec
, ACPI_EC_COMMAND_WRITE
,
365 wdata
, 2, NULL
, 0, 0);
369 * Externally callable EC access functions. For now, assume 1 EC only
371 int ec_burst_enable(void)
375 return acpi_ec_burst_enable(first_ec
);
378 EXPORT_SYMBOL(ec_burst_enable
);
380 int ec_burst_disable(void)
384 return acpi_ec_burst_disable(first_ec
);
387 EXPORT_SYMBOL(ec_burst_disable
);
389 int ec_read(u8 addr
, u8
* val
)
397 err
= acpi_ec_read(first_ec
, addr
, &temp_data
);
406 EXPORT_SYMBOL(ec_read
);
408 int ec_write(u8 addr
, u8 val
)
415 err
= acpi_ec_write(first_ec
, addr
, val
);
420 EXPORT_SYMBOL(ec_write
);
422 int ec_transaction(u8 command
,
423 const u8
* wdata
, unsigned wdata_len
,
424 u8
* rdata
, unsigned rdata_len
,
430 return acpi_ec_transaction(first_ec
, command
, wdata
,
431 wdata_len
, rdata
, rdata_len
,
435 EXPORT_SYMBOL(ec_transaction
);
437 static int acpi_ec_query(struct acpi_ec
*ec
, u8
* data
)
446 * Query the EC to find out which _Qxx method we need to evaluate.
447 * Note that successful completion of the query causes the ACPI_EC_SCI
448 * bit to be cleared (and thus clearing the interrupt source).
451 result
= acpi_ec_transaction(ec
, ACPI_EC_COMMAND_QUERY
, NULL
, 0, &d
, 1, 0);
462 /* --------------------------------------------------------------------------
464 -------------------------------------------------------------------------- */
465 int acpi_ec_add_query_handler(struct acpi_ec
*ec
, u8 query_bit
,
466 acpi_handle handle
, acpi_ec_query_func func
,
469 struct acpi_ec_query_handler
*handler
=
470 kzalloc(sizeof(struct acpi_ec_query_handler
), GFP_KERNEL
);
474 handler
->query_bit
= query_bit
;
475 handler
->handle
= handle
;
476 handler
->func
= func
;
477 handler
->data
= data
;
478 mutex_lock(&ec
->lock
);
479 list_add(&handler
->node
, &ec
->list
);
480 mutex_unlock(&ec
->lock
);
484 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler
);
486 void acpi_ec_remove_query_handler(struct acpi_ec
*ec
, u8 query_bit
)
488 struct acpi_ec_query_handler
*handler
, *tmp
;
489 mutex_lock(&ec
->lock
);
490 list_for_each_entry_safe(handler
, tmp
, &ec
->list
, node
) {
491 if (query_bit
== handler
->query_bit
) {
492 list_del(&handler
->node
);
496 mutex_unlock(&ec
->lock
);
499 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler
);
501 static void acpi_ec_gpe_query(void *ec_cxt
)
503 struct acpi_ec
*ec
= ec_cxt
;
505 struct acpi_ec_query_handler
*handler
, copy
;
507 if (!ec
|| acpi_ec_query(ec
, &value
))
509 mutex_lock(&ec
->lock
);
510 list_for_each_entry(handler
, &ec
->list
, node
) {
511 if (value
== handler
->query_bit
) {
512 /* have custom handler for this bit */
513 memcpy(©
, handler
, sizeof(copy
));
514 mutex_unlock(&ec
->lock
);
516 copy
.func(copy
.data
);
517 } else if (copy
.handle
) {
518 acpi_evaluate_object(copy
.handle
, NULL
, NULL
, NULL
);
523 mutex_unlock(&ec
->lock
);
526 static u32
acpi_ec_gpe_handler(void *data
)
528 acpi_status status
= AE_OK
;
529 struct acpi_ec
*ec
= data
;
531 pr_debug(PREFIX
"~~~> interrupt\n");
532 clear_bit(EC_FLAGS_WAIT_GPE
, &ec
->flags
);
533 if (test_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
))
536 if (acpi_ec_read_status(ec
) & ACPI_EC_FLAG_SCI
) {
537 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING
, &ec
->flags
))
538 status
= acpi_os_execute(OSL_EC_BURST_HANDLER
,
539 acpi_ec_gpe_query
, ec
);
540 } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
))) {
541 /* this is non-query, must be confirmation */
542 if (printk_ratelimit())
543 pr_info(PREFIX
"non-query interrupt received,"
544 " switching to interrupt mode\n");
545 set_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
);
548 return ACPI_SUCCESS(status
) ?
549 ACPI_INTERRUPT_HANDLED
: ACPI_INTERRUPT_NOT_HANDLED
;
552 /* --------------------------------------------------------------------------
553 Address Space Management
554 -------------------------------------------------------------------------- */
557 acpi_ec_space_setup(acpi_handle region_handle
,
558 u32 function
, void *handler_context
, void **return_context
)
561 * The EC object is in the handler context and is needed
562 * when calling the acpi_ec_space_handler.
564 *return_context
= (function
!= ACPI_REGION_DEACTIVATE
) ?
565 handler_context
: NULL
;
571 acpi_ec_space_handler(u32 function
, acpi_physical_address address
,
572 u32 bits
, acpi_integer
*value
,
573 void *handler_context
, void *region_context
)
575 struct acpi_ec
*ec
= handler_context
;
579 if ((address
> 0xFF) || !value
|| !handler_context
)
580 return AE_BAD_PARAMETER
;
582 if (function
!= ACPI_READ
&& function
!= ACPI_WRITE
)
583 return AE_BAD_PARAMETER
;
585 if (bits
!= 8 && acpi_strict
)
586 return AE_BAD_PARAMETER
;
588 acpi_ec_burst_enable(ec
);
590 if (function
== ACPI_READ
) {
591 result
= acpi_ec_read(ec
, address
, &temp
);
594 temp
= 0xff & (*value
);
595 result
= acpi_ec_write(ec
, address
, temp
);
598 for (i
= 8; unlikely(bits
- i
> 0); i
+= 8) {
600 if (function
== ACPI_READ
) {
601 result
= acpi_ec_read(ec
, address
, &temp
);
602 (*value
) |= ((acpi_integer
)temp
) << i
;
604 temp
= 0xff & ((*value
) >> i
);
605 result
= acpi_ec_write(ec
, address
, temp
);
609 acpi_ec_burst_disable(ec
);
613 return AE_BAD_PARAMETER
;
626 /* --------------------------------------------------------------------------
628 -------------------------------------------------------------------------- */
630 static struct proc_dir_entry
*acpi_ec_dir
;
632 static int acpi_ec_read_info(struct seq_file
*seq
, void *offset
)
634 struct acpi_ec
*ec
= seq
->private;
639 seq_printf(seq
, "gpe:\t\t\t0x%02x\n", (u32
) ec
->gpe
);
640 seq_printf(seq
, "ports:\t\t\t0x%02x, 0x%02x\n",
641 (unsigned)ec
->command_addr
, (unsigned)ec
->data_addr
);
642 seq_printf(seq
, "use global lock:\t%s\n",
643 ec
->global_lock
? "yes" : "no");
648 static int acpi_ec_info_open_fs(struct inode
*inode
, struct file
*file
)
650 return single_open(file
, acpi_ec_read_info
, PDE(inode
)->data
);
653 static struct file_operations acpi_ec_info_ops
= {
654 .open
= acpi_ec_info_open_fs
,
657 .release
= single_release
,
658 .owner
= THIS_MODULE
,
661 static int acpi_ec_add_fs(struct acpi_device
*device
)
663 struct proc_dir_entry
*entry
= NULL
;
665 if (!acpi_device_dir(device
)) {
666 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
),
668 if (!acpi_device_dir(device
))
672 entry
= create_proc_entry(ACPI_EC_FILE_INFO
, S_IRUGO
,
673 acpi_device_dir(device
));
677 entry
->proc_fops
= &acpi_ec_info_ops
;
678 entry
->data
= acpi_driver_data(device
);
679 entry
->owner
= THIS_MODULE
;
685 static int acpi_ec_remove_fs(struct acpi_device
*device
)
688 if (acpi_device_dir(device
)) {
689 remove_proc_entry(ACPI_EC_FILE_INFO
, acpi_device_dir(device
));
690 remove_proc_entry(acpi_device_bid(device
), acpi_ec_dir
);
691 acpi_device_dir(device
) = NULL
;
697 /* --------------------------------------------------------------------------
699 -------------------------------------------------------------------------- */
701 ec_parse_io_ports(struct acpi_resource
*resource
, void *context
);
703 static struct acpi_ec
*make_acpi_ec(void)
705 struct acpi_ec
*ec
= kzalloc(sizeof(struct acpi_ec
), GFP_KERNEL
);
708 ec
->flags
= 1 << EC_FLAGS_QUERY_PENDING
;
709 mutex_init(&ec
->lock
);
710 init_waitqueue_head(&ec
->wait
);
711 INIT_LIST_HEAD(&ec
->list
);
716 acpi_ec_register_query_methods(acpi_handle handle
, u32 level
,
717 void *context
, void **return_value
)
719 struct acpi_namespace_node
*node
= handle
;
720 struct acpi_ec
*ec
= context
;
722 if (sscanf(node
->name
.ascii
, "_Q%x", &value
) == 1) {
723 acpi_ec_add_query_handler(ec
, value
, handle
, NULL
, NULL
);
729 ec_parse_device(acpi_handle handle
, u32 Level
, void *context
, void **retval
)
733 struct acpi_ec
*ec
= context
;
734 status
= acpi_walk_resources(handle
, METHOD_NAME__CRS
,
735 ec_parse_io_ports
, ec
);
736 if (ACPI_FAILURE(status
))
739 /* Get GPE bit assignment (EC events). */
740 /* TODO: Add support for _GPE returning a package */
741 status
= acpi_evaluate_integer(handle
, "_GPE", NULL
, &ec
->gpe
);
742 if (ACPI_FAILURE(status
))
744 /* Find and register all query methods */
745 acpi_walk_namespace(ACPI_TYPE_METHOD
, handle
, 1,
746 acpi_ec_register_query_methods
, ec
, NULL
);
747 /* Use the global lock for all EC transactions? */
748 acpi_evaluate_integer(handle
, "_GLK", NULL
, &ec
->global_lock
);
750 return AE_CTRL_TERMINATE
;
753 static void ec_remove_handlers(struct acpi_ec
*ec
)
755 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec
->handle
,
756 ACPI_ADR_SPACE_EC
, &acpi_ec_space_handler
)))
757 pr_err(PREFIX
"failed to remove space handler\n");
758 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL
, ec
->gpe
,
759 &acpi_ec_gpe_handler
)))
760 pr_err(PREFIX
"failed to remove gpe handler\n");
761 ec
->handlers_installed
= 0;
764 static int acpi_ec_add(struct acpi_device
*device
)
766 struct acpi_ec
*ec
= NULL
;
770 strcpy(acpi_device_name(device
), ACPI_EC_DEVICE_NAME
);
771 strcpy(acpi_device_class(device
), ACPI_EC_CLASS
);
773 /* Check for boot EC */
775 if (boot_ec
->handle
== device
->handle
) {
776 /* Pre-loaded EC from DSDT, just move pointer */
780 } else if (boot_ec
->handle
== ACPI_ROOT_OBJECT
) {
781 /* ECDT-based EC, time to shut it down */
782 ec_remove_handlers(boot_ec
);
784 first_ec
= boot_ec
= NULL
;
792 if (ec_parse_device(device
->handle
, 0, ec
, NULL
) !=
797 ec
->handle
= device
->handle
;
801 acpi_driver_data(device
) = ec
;
802 acpi_ec_add_fs(device
);
803 pr_info(PREFIX
"GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
804 ec
->gpe
, ec
->command_addr
, ec
->data_addr
);
805 pr_info(PREFIX
"driver started in %s mode\n",
806 (test_bit(EC_FLAGS_GPE_MODE
, &ec
->flags
))?"interrupt":"poll");
810 static int acpi_ec_remove(struct acpi_device
*device
, int type
)
813 struct acpi_ec_query_handler
*handler
, *tmp
;
818 ec
= acpi_driver_data(device
);
819 mutex_lock(&ec
->lock
);
820 list_for_each_entry_safe(handler
, tmp
, &ec
->list
, node
) {
821 list_del(&handler
->node
);
824 mutex_unlock(&ec
->lock
);
825 acpi_ec_remove_fs(device
);
826 acpi_driver_data(device
) = NULL
;
834 ec_parse_io_ports(struct acpi_resource
*resource
, void *context
)
836 struct acpi_ec
*ec
= context
;
838 if (resource
->type
!= ACPI_RESOURCE_TYPE_IO
)
842 * The first address region returned is the data port, and
843 * the second address region returned is the status/command
846 if (ec
->data_addr
== 0)
847 ec
->data_addr
= resource
->data
.io
.minimum
;
848 else if (ec
->command_addr
== 0)
849 ec
->command_addr
= resource
->data
.io
.minimum
;
851 return AE_CTRL_TERMINATE
;
856 static int ec_install_handlers(struct acpi_ec
*ec
)
859 if (ec
->handlers_installed
)
861 status
= acpi_install_gpe_handler(NULL
, ec
->gpe
,
862 ACPI_GPE_EDGE_TRIGGERED
,
863 &acpi_ec_gpe_handler
, ec
);
864 if (ACPI_FAILURE(status
))
867 acpi_set_gpe_type(NULL
, ec
->gpe
, ACPI_GPE_TYPE_RUNTIME
);
868 acpi_enable_gpe(NULL
, ec
->gpe
, ACPI_NOT_ISR
);
870 status
= acpi_install_address_space_handler(ec
->handle
,
872 &acpi_ec_space_handler
,
873 &acpi_ec_space_setup
, ec
);
874 if (ACPI_FAILURE(status
)) {
875 acpi_remove_gpe_handler(NULL
, ec
->gpe
, &acpi_ec_gpe_handler
);
879 ec
->handlers_installed
= 1;
883 static int acpi_ec_start(struct acpi_device
*device
)
891 ec
= acpi_driver_data(device
);
896 ret
= ec_install_handlers(ec
);
898 /* EC is fully operational, allow queries */
899 clear_bit(EC_FLAGS_QUERY_PENDING
, &ec
->flags
);
903 static int acpi_ec_stop(struct acpi_device
*device
, int type
)
908 ec
= acpi_driver_data(device
);
911 ec_remove_handlers(ec
);
916 int __init
acpi_boot_ec_enable(void)
918 if (!boot_ec
|| boot_ec
->handlers_installed
)
920 if (!ec_install_handlers(boot_ec
)) {
927 int __init
acpi_ec_ecdt_probe(void)
931 struct acpi_table_ecdt
*ecdt_ptr
;
933 boot_ec
= make_acpi_ec();
937 * Generate a boot ec context
939 status
= acpi_get_table(ACPI_SIG_ECDT
, 1,
940 (struct acpi_table_header
**)&ecdt_ptr
);
941 if (ACPI_SUCCESS(status
)) {
942 pr_info(PREFIX
"EC description table is found, configuring boot EC\n");
943 boot_ec
->command_addr
= ecdt_ptr
->control
.address
;
944 boot_ec
->data_addr
= ecdt_ptr
->data
.address
;
945 boot_ec
->gpe
= ecdt_ptr
->gpe
;
946 boot_ec
->handle
= ACPI_ROOT_OBJECT
;
948 /* This workaround is needed only on some broken machines,
949 * which require early EC, but fail to provide ECDT */
951 printk(KERN_DEBUG PREFIX
"Look up EC in DSDT\n");
952 status
= acpi_get_devices(ec_device_ids
[0].id
, ec_parse_device
,
954 /* Check that acpi_get_devices actually find something */
955 if (ACPI_FAILURE(status
) || !boot_ec
->handle
)
957 /* We really need to limit this workaround, the only ASUS,
958 * which needs it, has fake EC._INI method, so use it as flag.
959 * Keep boot_ec struct as it will be needed soon.
961 if (ACPI_FAILURE(acpi_get_handle(boot_ec
->handle
, "_INI", &x
)))
965 ret
= ec_install_handlers(boot_ec
);
976 static int __init
acpi_ec_init(void)
983 acpi_ec_dir
= proc_mkdir(ACPI_EC_CLASS
, acpi_root_dir
);
987 /* Now register the driver for the EC */
988 result
= acpi_bus_register_driver(&acpi_ec_driver
);
990 remove_proc_entry(ACPI_EC_CLASS
, acpi_root_dir
);
997 subsys_initcall(acpi_ec_init
);
999 /* EC driver currently not unloadable */
1001 static void __exit
acpi_ec_exit(void)
1004 acpi_bus_unregister_driver(&acpi_ec_driver
);
1006 remove_proc_entry(ACPI_EC_CLASS
, acpi_root_dir
);