2 * ec.c - ACPI Embedded Controller Driver (v2.1)
4 * Copyright (C) 2006-2008 Alexey Starikovskiy <astarikovskiy@suse.de>
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 printout */
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>
41 #include <linux/spinlock.h>
43 #include <acpi/acpi_bus.h>
44 #include <acpi/acpi_drivers.h>
46 #define ACPI_EC_CLASS "embedded_controller"
47 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
48 #define ACPI_EC_FILE_INFO "info"
50 #define PREFIX "ACPI: EC: "
52 /* EC status register */
53 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
54 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
55 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
56 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
60 ACPI_EC_COMMAND_READ
= 0x80,
61 ACPI_EC_COMMAND_WRITE
= 0x81,
62 ACPI_EC_BURST_ENABLE
= 0x82,
63 ACPI_EC_BURST_DISABLE
= 0x83,
64 ACPI_EC_COMMAND_QUERY
= 0x84,
67 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
68 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
69 #define ACPI_EC_CDELAY 10 /* Wait 10us before polling EC */
70 #define ACPI_EC_MSI_UDELAY 550 /* Wait 550us for MSI EC */
72 #define ACPI_EC_STORM_THRESHOLD 8 /* number of false interrupts
73 per one transaction */
76 EC_FLAGS_QUERY_PENDING
, /* Query is pending */
77 EC_FLAGS_GPE_STORM
, /* GPE storm detected */
78 EC_FLAGS_HANDLERS_INSTALLED
/* Handlers for GPE and
79 * OpReg are installed */
82 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
83 /* External interfaces use first EC only, so remember */
84 typedef int (*acpi_ec_query_func
) (void *data
);
86 struct acpi_ec_query_handler
{
87 struct list_head node
;
88 acpi_ec_query_func func
;
97 unsigned short irq_count
;
106 static struct acpi_ec
{
109 unsigned long command_addr
;
110 unsigned long data_addr
;
111 unsigned long global_lock
;
114 wait_queue_head_t wait
;
115 struct list_head list
;
116 struct transaction
*curr
;
117 spinlock_t curr_lock
;
118 } *boot_ec
, *first_ec
;
120 static int EC_FLAGS_MSI
; /* Out-of-spec MSI controller */
122 /* --------------------------------------------------------------------------
123 Transaction Management
124 -------------------------------------------------------------------------- */
126 static inline u8
acpi_ec_read_status(struct acpi_ec
*ec
)
128 u8 x
= inb(ec
->command_addr
);
129 pr_debug(PREFIX
"---> status = 0x%2.2x\n", x
);
133 static inline u8
acpi_ec_read_data(struct acpi_ec
*ec
)
135 u8 x
= inb(ec
->data_addr
);
136 pr_debug(PREFIX
"---> data = 0x%2.2x\n", x
);
140 static inline void acpi_ec_write_cmd(struct acpi_ec
*ec
, u8 command
)
142 pr_debug(PREFIX
"<--- command = 0x%2.2x\n", command
);
143 outb(command
, ec
->command_addr
);
146 static inline void acpi_ec_write_data(struct acpi_ec
*ec
, u8 data
)
148 pr_debug(PREFIX
"<--- data = 0x%2.2x\n", data
);
149 outb(data
, ec
->data_addr
);
152 static int ec_transaction_done(struct acpi_ec
*ec
)
156 spin_lock_irqsave(&ec
->curr_lock
, flags
);
157 if (!ec
->curr
|| ec
->curr
->done
)
159 spin_unlock_irqrestore(&ec
->curr_lock
, flags
);
163 static void start_transaction(struct acpi_ec
*ec
)
165 ec
->curr
->irq_count
= ec
->curr
->wi
= ec
->curr
->ri
= 0;
166 ec
->curr
->done
= false;
167 acpi_ec_write_cmd(ec
, ec
->curr
->command
);
170 static void advance_transaction(struct acpi_ec
*ec
, u8 status
)
173 spin_lock_irqsave(&ec
->curr_lock
, flags
);
176 if (ec
->curr
->wlen
> ec
->curr
->wi
) {
177 if ((status
& ACPI_EC_FLAG_IBF
) == 0)
178 acpi_ec_write_data(ec
,
179 ec
->curr
->wdata
[ec
->curr
->wi
++]);
182 } else if (ec
->curr
->rlen
> ec
->curr
->ri
) {
183 if ((status
& ACPI_EC_FLAG_OBF
) == 1) {
184 ec
->curr
->rdata
[ec
->curr
->ri
++] = acpi_ec_read_data(ec
);
185 if (ec
->curr
->rlen
== ec
->curr
->ri
)
186 ec
->curr
->done
= true;
189 } else if (ec
->curr
->wlen
== ec
->curr
->wi
&&
190 (status
& ACPI_EC_FLAG_IBF
) == 0)
191 ec
->curr
->done
= true;
194 /* false interrupt, state didn't change */
196 ++ec
->curr
->irq_count
;
198 spin_unlock_irqrestore(&ec
->curr_lock
, flags
);
201 static void acpi_ec_gpe_query(void *ec_cxt
);
203 static int ec_check_sci(struct acpi_ec
*ec
, u8 state
)
205 if (state
& ACPI_EC_FLAG_SCI
) {
206 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING
, &ec
->flags
))
207 return acpi_os_execute(OSL_EC_BURST_HANDLER
,
208 acpi_ec_gpe_query
, ec
);
213 static int ec_poll(struct acpi_ec
*ec
)
216 int repeat
= 2; /* number of command restarts */
218 unsigned long delay
= jiffies
+
219 msecs_to_jiffies(ACPI_EC_DELAY
);
221 /* don't sleep with disabled interrupts */
222 if (EC_FLAGS_MSI
|| irqs_disabled()) {
223 udelay(ACPI_EC_MSI_UDELAY
);
224 if (ec_transaction_done(ec
))
227 if (wait_event_timeout(ec
->wait
,
228 ec_transaction_done(ec
),
229 msecs_to_jiffies(1)))
232 advance_transaction(ec
, acpi_ec_read_status(ec
));
233 } while (time_before(jiffies
, delay
));
234 if (!ec
->curr
->irq_count
||
235 (acpi_ec_read_status(ec
) & ACPI_EC_FLAG_IBF
))
237 /* try restart command if we get any false interrupts */
238 pr_debug(PREFIX
"controller reset, restart transaction\n");
239 spin_lock_irqsave(&ec
->curr_lock
, flags
);
240 start_transaction(ec
);
241 spin_unlock_irqrestore(&ec
->curr_lock
, flags
);
246 static int acpi_ec_transaction_unlocked(struct acpi_ec
*ec
,
247 struct transaction
*t
)
251 pr_debug(PREFIX
"transaction start\n");
252 /* disable GPE during transaction if storm is detected */
253 if (test_bit(EC_FLAGS_GPE_STORM
, &ec
->flags
)) {
254 acpi_disable_gpe(NULL
, ec
->gpe
);
257 udelay(ACPI_EC_MSI_UDELAY
);
258 /* start transaction */
259 spin_lock_irqsave(&ec
->curr_lock
, tmp
);
260 /* following two actions should be kept atomic */
262 start_transaction(ec
);
263 if (ec
->curr
->command
== ACPI_EC_COMMAND_QUERY
)
264 clear_bit(EC_FLAGS_QUERY_PENDING
, &ec
->flags
);
265 spin_unlock_irqrestore(&ec
->curr_lock
, tmp
);
267 pr_debug(PREFIX
"transaction end\n");
268 spin_lock_irqsave(&ec
->curr_lock
, tmp
);
270 spin_unlock_irqrestore(&ec
->curr_lock
, tmp
);
271 if (test_bit(EC_FLAGS_GPE_STORM
, &ec
->flags
)) {
272 /* check if we received SCI during transaction */
273 ec_check_sci(ec
, acpi_ec_read_status(ec
));
274 /* it is safe to enable GPE outside of transaction */
275 acpi_enable_gpe(NULL
, ec
->gpe
);
276 } else if (t
->irq_count
> ACPI_EC_STORM_THRESHOLD
) {
277 pr_info(PREFIX
"GPE storm detected, "
278 "transactions will use polling mode\n");
279 set_bit(EC_FLAGS_GPE_STORM
, &ec
->flags
);
284 static int ec_check_ibf0(struct acpi_ec
*ec
)
286 u8 status
= acpi_ec_read_status(ec
);
287 return (status
& ACPI_EC_FLAG_IBF
) == 0;
290 static int ec_wait_ibf0(struct acpi_ec
*ec
)
292 unsigned long delay
= jiffies
+ msecs_to_jiffies(ACPI_EC_DELAY
);
293 /* interrupt wait manually if GPE mode is not active */
294 while (time_before(jiffies
, delay
))
295 if (wait_event_timeout(ec
->wait
, ec_check_ibf0(ec
),
296 msecs_to_jiffies(1)))
301 static int acpi_ec_transaction(struct acpi_ec
*ec
, struct transaction
*t
)
305 if (!ec
|| (!t
) || (t
->wlen
&& !t
->wdata
) || (t
->rlen
&& !t
->rdata
))
308 memset(t
->rdata
, 0, t
->rlen
);
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
)) {
317 if (ec_wait_ibf0(ec
)) {
318 pr_err(PREFIX
"input buffer is not empty, "
319 "aborting transaction\n");
323 status
= acpi_ec_transaction_unlocked(ec
, t
);
326 acpi_release_global_lock(glk
);
328 mutex_unlock(&ec
->lock
);
332 static int acpi_ec_burst_enable(struct acpi_ec
*ec
)
335 struct transaction t
= {.command
= ACPI_EC_BURST_ENABLE
,
336 .wdata
= NULL
, .rdata
= &d
,
337 .wlen
= 0, .rlen
= 1};
339 return acpi_ec_transaction(ec
, &t
);
342 static int acpi_ec_burst_disable(struct acpi_ec
*ec
)
344 struct transaction t
= {.command
= ACPI_EC_BURST_DISABLE
,
345 .wdata
= NULL
, .rdata
= NULL
,
346 .wlen
= 0, .rlen
= 0};
348 return (acpi_ec_read_status(ec
) & ACPI_EC_FLAG_BURST
) ?
349 acpi_ec_transaction(ec
, &t
) : 0;
352 static int acpi_ec_read(struct acpi_ec
*ec
, u8 address
, u8
* data
)
356 struct transaction t
= {.command
= ACPI_EC_COMMAND_READ
,
357 .wdata
= &address
, .rdata
= &d
,
358 .wlen
= 1, .rlen
= 1};
360 result
= acpi_ec_transaction(ec
, &t
);
365 static int acpi_ec_write(struct acpi_ec
*ec
, u8 address
, u8 data
)
367 u8 wdata
[2] = { address
, data
};
368 struct transaction t
= {.command
= ACPI_EC_COMMAND_WRITE
,
369 .wdata
= wdata
, .rdata
= NULL
,
370 .wlen
= 2, .rlen
= 0};
372 return acpi_ec_transaction(ec
, &t
);
376 * Externally callable EC access functions. For now, assume 1 EC only
378 int ec_burst_enable(void)
382 return acpi_ec_burst_enable(first_ec
);
385 EXPORT_SYMBOL(ec_burst_enable
);
387 int ec_burst_disable(void)
391 return acpi_ec_burst_disable(first_ec
);
394 EXPORT_SYMBOL(ec_burst_disable
);
396 int ec_read(u8 addr
, u8
* val
)
404 err
= acpi_ec_read(first_ec
, addr
, &temp_data
);
413 EXPORT_SYMBOL(ec_read
);
415 int ec_write(u8 addr
, u8 val
)
422 err
= acpi_ec_write(first_ec
, addr
, val
);
427 EXPORT_SYMBOL(ec_write
);
429 int ec_transaction(u8 command
,
430 const u8
* wdata
, unsigned wdata_len
,
431 u8
* rdata
, unsigned rdata_len
,
434 struct transaction t
= {.command
= command
,
435 .wdata
= wdata
, .rdata
= rdata
,
436 .wlen
= wdata_len
, .rlen
= rdata_len
};
440 return acpi_ec_transaction(first_ec
, &t
);
443 EXPORT_SYMBOL(ec_transaction
);
445 static int acpi_ec_query(struct acpi_ec
*ec
, u8
* data
)
449 struct transaction t
= {.command
= ACPI_EC_COMMAND_QUERY
,
450 .wdata
= NULL
, .rdata
= &d
,
451 .wlen
= 0, .rlen
= 1};
456 * Query the EC to find out which _Qxx method we need to evaluate.
457 * Note that successful completion of the query causes the ACPI_EC_SCI
458 * bit to be cleared (and thus clearing the interrupt source).
461 result
= acpi_ec_transaction(ec
, &t
);
472 /* --------------------------------------------------------------------------
474 -------------------------------------------------------------------------- */
475 int acpi_ec_add_query_handler(struct acpi_ec
*ec
, u8 query_bit
,
476 acpi_handle handle
, acpi_ec_query_func func
,
479 struct acpi_ec_query_handler
*handler
=
480 kzalloc(sizeof(struct acpi_ec_query_handler
), GFP_KERNEL
);
484 handler
->query_bit
= query_bit
;
485 handler
->handle
= handle
;
486 handler
->func
= func
;
487 handler
->data
= data
;
488 mutex_lock(&ec
->lock
);
489 list_add(&handler
->node
, &ec
->list
);
490 mutex_unlock(&ec
->lock
);
494 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler
);
496 void acpi_ec_remove_query_handler(struct acpi_ec
*ec
, u8 query_bit
)
498 struct acpi_ec_query_handler
*handler
, *tmp
;
499 mutex_lock(&ec
->lock
);
500 list_for_each_entry_safe(handler
, tmp
, &ec
->list
, node
) {
501 if (query_bit
== handler
->query_bit
) {
502 list_del(&handler
->node
);
506 mutex_unlock(&ec
->lock
);
509 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler
);
511 static void acpi_ec_gpe_query(void *ec_cxt
)
513 struct acpi_ec
*ec
= ec_cxt
;
515 struct acpi_ec_query_handler
*handler
, copy
;
517 if (!ec
|| acpi_ec_query(ec
, &value
))
519 mutex_lock(&ec
->lock
);
520 list_for_each_entry(handler
, &ec
->list
, node
) {
521 if (value
== handler
->query_bit
) {
522 /* have custom handler for this bit */
523 memcpy(©
, handler
, sizeof(copy
));
524 mutex_unlock(&ec
->lock
);
526 copy
.func(copy
.data
);
527 } else if (copy
.handle
) {
528 acpi_evaluate_object(copy
.handle
, NULL
, NULL
, NULL
);
533 mutex_unlock(&ec
->lock
);
536 static u32
acpi_ec_gpe_handler(void *data
)
538 struct acpi_ec
*ec
= data
;
541 pr_debug(PREFIX
"~~~> interrupt\n");
542 status
= acpi_ec_read_status(ec
);
544 advance_transaction(ec
, status
);
545 if (ec_transaction_done(ec
) && (status
& ACPI_EC_FLAG_IBF
) == 0)
547 ec_check_sci(ec
, status
);
548 return ACPI_INTERRUPT_HANDLED
;
551 /* --------------------------------------------------------------------------
552 Address Space Management
553 -------------------------------------------------------------------------- */
556 acpi_ec_space_handler(u32 function
, acpi_physical_address address
,
557 u32 bits
, acpi_integer
*value
,
558 void *handler_context
, void *region_context
)
560 struct acpi_ec
*ec
= handler_context
;
564 if ((address
> 0xFF) || !value
|| !handler_context
)
565 return AE_BAD_PARAMETER
;
567 if (function
!= ACPI_READ
&& function
!= ACPI_WRITE
)
568 return AE_BAD_PARAMETER
;
570 if (bits
!= 8 && acpi_strict
)
571 return AE_BAD_PARAMETER
;
574 acpi_ec_burst_enable(ec
);
576 if (function
== ACPI_READ
) {
577 result
= acpi_ec_read(ec
, address
, &temp
);
580 temp
= 0xff & (*value
);
581 result
= acpi_ec_write(ec
, address
, temp
);
584 for (i
= 8; unlikely(bits
- i
> 0); i
+= 8) {
586 if (function
== ACPI_READ
) {
587 result
= acpi_ec_read(ec
, address
, &temp
);
588 (*value
) |= ((acpi_integer
)temp
) << i
;
590 temp
= 0xff & ((*value
) >> i
);
591 result
= acpi_ec_write(ec
, address
, temp
);
596 acpi_ec_burst_disable(ec
);
600 return AE_BAD_PARAMETER
;
613 /* --------------------------------------------------------------------------
615 -------------------------------------------------------------------------- */
617 static struct proc_dir_entry
*acpi_ec_dir
;
619 static int acpi_ec_read_info(struct seq_file
*seq
, void *offset
)
621 struct acpi_ec
*ec
= seq
->private;
626 seq_printf(seq
, "gpe:\t\t\t0x%02x\n", (u32
) ec
->gpe
);
627 seq_printf(seq
, "ports:\t\t\t0x%02x, 0x%02x\n",
628 (unsigned)ec
->command_addr
, (unsigned)ec
->data_addr
);
629 seq_printf(seq
, "use global lock:\t%s\n",
630 ec
->global_lock
? "yes" : "no");
635 static int acpi_ec_info_open_fs(struct inode
*inode
, struct file
*file
)
637 return single_open(file
, acpi_ec_read_info
, PDE(inode
)->data
);
640 static const struct file_operations acpi_ec_info_ops
= {
641 .open
= acpi_ec_info_open_fs
,
644 .release
= single_release
,
645 .owner
= THIS_MODULE
,
648 static int acpi_ec_add_fs(struct acpi_device
*device
)
650 struct proc_dir_entry
*entry
= NULL
;
652 if (!acpi_device_dir(device
)) {
653 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
),
655 if (!acpi_device_dir(device
))
659 entry
= proc_create_data(ACPI_EC_FILE_INFO
, S_IRUGO
,
660 acpi_device_dir(device
),
661 &acpi_ec_info_ops
, acpi_driver_data(device
));
667 static int acpi_ec_remove_fs(struct acpi_device
*device
)
670 if (acpi_device_dir(device
)) {
671 remove_proc_entry(ACPI_EC_FILE_INFO
, acpi_device_dir(device
));
672 remove_proc_entry(acpi_device_bid(device
), acpi_ec_dir
);
673 acpi_device_dir(device
) = NULL
;
679 /* --------------------------------------------------------------------------
681 -------------------------------------------------------------------------- */
683 ec_parse_io_ports(struct acpi_resource
*resource
, void *context
);
685 static struct acpi_ec
*make_acpi_ec(void)
687 struct acpi_ec
*ec
= kzalloc(sizeof(struct acpi_ec
), GFP_KERNEL
);
690 ec
->flags
= 1 << EC_FLAGS_QUERY_PENDING
;
691 mutex_init(&ec
->lock
);
692 init_waitqueue_head(&ec
->wait
);
693 INIT_LIST_HEAD(&ec
->list
);
694 spin_lock_init(&ec
->curr_lock
);
699 acpi_ec_register_query_methods(acpi_handle handle
, u32 level
,
700 void *context
, void **return_value
)
703 struct acpi_buffer buffer
= { sizeof(node_name
), node_name
};
704 struct acpi_ec
*ec
= context
;
708 status
= acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
);
710 if (ACPI_SUCCESS(status
) && sscanf(node_name
, "_Q%x", &value
) == 1) {
711 acpi_ec_add_query_handler(ec
, value
, handle
, NULL
, NULL
);
717 ec_parse_device(acpi_handle handle
, u32 Level
, void *context
, void **retval
)
720 unsigned long long tmp
= 0;
722 struct acpi_ec
*ec
= context
;
724 /* clear addr values, ec_parse_io_ports depend on it */
725 ec
->command_addr
= ec
->data_addr
= 0;
727 status
= acpi_walk_resources(handle
, METHOD_NAME__CRS
,
728 ec_parse_io_ports
, ec
);
729 if (ACPI_FAILURE(status
))
732 /* Get GPE bit assignment (EC events). */
733 /* TODO: Add support for _GPE returning a package */
734 status
= acpi_evaluate_integer(handle
, "_GPE", NULL
, &tmp
);
735 if (ACPI_FAILURE(status
))
738 /* Use the global lock for all EC transactions? */
740 acpi_evaluate_integer(handle
, "_GLK", NULL
, &tmp
);
741 ec
->global_lock
= tmp
;
743 return AE_CTRL_TERMINATE
;
746 static int ec_install_handlers(struct acpi_ec
*ec
)
749 if (test_bit(EC_FLAGS_HANDLERS_INSTALLED
, &ec
->flags
))
751 status
= acpi_install_gpe_handler(NULL
, ec
->gpe
,
752 ACPI_GPE_EDGE_TRIGGERED
,
753 &acpi_ec_gpe_handler
, ec
);
754 if (ACPI_FAILURE(status
))
756 acpi_set_gpe_type(NULL
, ec
->gpe
, ACPI_GPE_TYPE_RUNTIME
);
757 acpi_enable_gpe(NULL
, ec
->gpe
);
758 status
= acpi_install_address_space_handler(ec
->handle
,
760 &acpi_ec_space_handler
,
762 if (ACPI_FAILURE(status
)) {
763 if (status
== AE_NOT_FOUND
) {
765 * Maybe OS fails in evaluating the _REG object.
766 * The AE_NOT_FOUND error will be ignored and OS
767 * continue to initialize EC.
769 printk(KERN_ERR
"Fail in evaluating the _REG object"
770 " of EC device. Broken bios is suspected.\n");
772 acpi_remove_gpe_handler(NULL
, ec
->gpe
,
773 &acpi_ec_gpe_handler
);
778 set_bit(EC_FLAGS_HANDLERS_INSTALLED
, &ec
->flags
);
782 static void ec_remove_handlers(struct acpi_ec
*ec
)
784 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec
->handle
,
785 ACPI_ADR_SPACE_EC
, &acpi_ec_space_handler
)))
786 pr_err(PREFIX
"failed to remove space handler\n");
787 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL
, ec
->gpe
,
788 &acpi_ec_gpe_handler
)))
789 pr_err(PREFIX
"failed to remove gpe handler\n");
790 clear_bit(EC_FLAGS_HANDLERS_INSTALLED
, &ec
->flags
);
793 static int acpi_ec_add(struct acpi_device
*device
)
795 struct acpi_ec
*ec
= NULL
;
798 strcpy(acpi_device_name(device
), ACPI_EC_DEVICE_NAME
);
799 strcpy(acpi_device_class(device
), ACPI_EC_CLASS
);
801 /* Check for boot EC */
803 (boot_ec
->handle
== device
->handle
||
804 boot_ec
->handle
== ACPI_ROOT_OBJECT
)) {
812 if (ec_parse_device(device
->handle
, 0, ec
, NULL
) !=
818 ec
->handle
= device
->handle
;
820 /* Find and register all query methods */
821 acpi_walk_namespace(ACPI_TYPE_METHOD
, ec
->handle
, 1,
822 acpi_ec_register_query_methods
, ec
, NULL
);
826 device
->driver_data
= ec
;
827 acpi_ec_add_fs(device
);
828 pr_info(PREFIX
"GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
829 ec
->gpe
, ec
->command_addr
, ec
->data_addr
);
831 ret
= ec_install_handlers(ec
);
833 /* EC is fully operational, allow queries */
834 clear_bit(EC_FLAGS_QUERY_PENDING
, &ec
->flags
);
838 static int acpi_ec_remove(struct acpi_device
*device
, int type
)
841 struct acpi_ec_query_handler
*handler
, *tmp
;
846 ec
= acpi_driver_data(device
);
847 ec_remove_handlers(ec
);
848 mutex_lock(&ec
->lock
);
849 list_for_each_entry_safe(handler
, tmp
, &ec
->list
, node
) {
850 list_del(&handler
->node
);
853 mutex_unlock(&ec
->lock
);
854 acpi_ec_remove_fs(device
);
855 device
->driver_data
= NULL
;
863 ec_parse_io_ports(struct acpi_resource
*resource
, void *context
)
865 struct acpi_ec
*ec
= context
;
867 if (resource
->type
!= ACPI_RESOURCE_TYPE_IO
)
871 * The first address region returned is the data port, and
872 * the second address region returned is the status/command
875 if (ec
->data_addr
== 0)
876 ec
->data_addr
= resource
->data
.io
.minimum
;
877 else if (ec
->command_addr
== 0)
878 ec
->command_addr
= resource
->data
.io
.minimum
;
880 return AE_CTRL_TERMINATE
;
885 int __init
acpi_boot_ec_enable(void)
887 if (!boot_ec
|| test_bit(EC_FLAGS_HANDLERS_INSTALLED
, &boot_ec
->flags
))
889 if (!ec_install_handlers(boot_ec
)) {
896 static const struct acpi_device_id ec_device_ids
[] = {
901 int __init
acpi_ec_ecdt_probe(void)
904 struct acpi_ec
*saved_ec
= NULL
;
905 struct acpi_table_ecdt
*ecdt_ptr
;
907 boot_ec
= make_acpi_ec();
911 * Generate a boot ec context
913 if (dmi_name_in_vendors("Micro-Star") ||
914 dmi_name_in_vendors("Notebook")) {
915 pr_info(PREFIX
"Enabling special treatment for EC from MSI.\n");
918 status
= acpi_get_table(ACPI_SIG_ECDT
, 1,
919 (struct acpi_table_header
**)&ecdt_ptr
);
920 if (ACPI_SUCCESS(status
)) {
921 pr_info(PREFIX
"EC description table is found, configuring boot EC\n");
922 boot_ec
->command_addr
= ecdt_ptr
->control
.address
;
923 boot_ec
->data_addr
= ecdt_ptr
->data
.address
;
924 boot_ec
->gpe
= ecdt_ptr
->gpe
;
925 boot_ec
->handle
= ACPI_ROOT_OBJECT
;
926 acpi_get_handle(ACPI_ROOT_OBJECT
, ecdt_ptr
->id
, &boot_ec
->handle
);
927 /* Don't trust ECDT, which comes from ASUSTek */
928 if (!dmi_name_in_vendors("ASUS") && EC_FLAGS_MSI
== 0)
930 saved_ec
= kmalloc(sizeof(struct acpi_ec
), GFP_KERNEL
);
933 memcpy(saved_ec
, boot_ec
, sizeof(struct acpi_ec
));
936 /* This workaround is needed only on some broken machines,
937 * which require early EC, but fail to provide ECDT */
938 printk(KERN_DEBUG PREFIX
"Look up EC in DSDT\n");
939 status
= acpi_get_devices(ec_device_ids
[0].id
, ec_parse_device
,
941 /* Check that acpi_get_devices actually find something */
942 if (ACPI_FAILURE(status
) || !boot_ec
->handle
)
945 /* try to find good ECDT from ASUSTek */
946 if (saved_ec
->command_addr
!= boot_ec
->command_addr
||
947 saved_ec
->data_addr
!= boot_ec
->data_addr
||
948 saved_ec
->gpe
!= boot_ec
->gpe
||
949 saved_ec
->handle
!= boot_ec
->handle
)
950 pr_info(PREFIX
"ASUSTek keeps feeding us with broken "
951 "ECDT tables, which are very hard to workaround. "
952 "Trying to use DSDT EC info instead. Please send "
953 "output of acpidump to linux-acpi@vger.kernel.org\n");
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.
962 if (!dmi_name_in_vendors("ASUS") ||
963 ACPI_FAILURE(acpi_get_handle(boot_ec
->handle
, "_INI",
968 if (!ec_install_handlers(boot_ec
)) {
978 static int acpi_ec_suspend(struct acpi_device
*device
, pm_message_t state
)
980 struct acpi_ec
*ec
= acpi_driver_data(device
);
982 acpi_disable_gpe(NULL
, ec
->gpe
);
986 static int acpi_ec_resume(struct acpi_device
*device
)
988 struct acpi_ec
*ec
= acpi_driver_data(device
);
989 /* Enable use of GPE back */
990 acpi_enable_gpe(NULL
, ec
->gpe
);
994 static struct acpi_driver acpi_ec_driver
= {
996 .class = ACPI_EC_CLASS
,
997 .ids
= ec_device_ids
,
1000 .remove
= acpi_ec_remove
,
1001 .suspend
= acpi_ec_suspend
,
1002 .resume
= acpi_ec_resume
,
1006 int __init
acpi_ec_init(void)
1010 acpi_ec_dir
= proc_mkdir(ACPI_EC_CLASS
, acpi_root_dir
);
1014 /* Now register the driver for the EC */
1015 result
= acpi_bus_register_driver(&acpi_ec_driver
);
1017 remove_proc_entry(ACPI_EC_CLASS
, acpi_root_dir
);
1024 /* EC driver currently not unloadable */
1026 static void __exit
acpi_ec_exit(void)
1029 acpi_bus_unregister_driver(&acpi_ec_driver
);
1031 remove_proc_entry(ACPI_EC_CLASS
, acpi_root_dir
);