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_COMMAND_READ 0x80
63 #define ACPI_EC_COMMAND_WRITE 0x81
64 #define ACPI_EC_BURST_ENABLE 0x82
65 #define ACPI_EC_BURST_DISABLE 0x83
66 #define ACPI_EC_COMMAND_QUERY 0x84
68 static int acpi_ec_add (struct acpi_device *device);
69 static int acpi_ec_remove (struct acpi_device *device, int type);
70 static int acpi_ec_start (struct acpi_device *device);
71 static int acpi_ec_stop (struct acpi_device *device, int type);
73 static struct acpi_driver acpi_ec_driver = {
74 .name = ACPI_EC_DRIVER_NAME,
75 .class = ACPI_EC_CLASS,
79 .remove = acpi_ec_remove,
80 .start = acpi_ec_start,
88 unsigned long gpe_bit;
89 struct acpi_generic_address status_addr;
90 struct acpi_generic_address command_addr;
91 struct acpi_generic_address data_addr;
92 unsigned long global_lock;
93 unsigned int expect_event;
94 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort*/
97 wait_queue_head_t wait;
100 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
101 static struct acpi_ec *ec_ecdt;
103 /* External interfaces use first EC only, so remember */
104 static struct acpi_device *first_ec;
106 /* --------------------------------------------------------------------------
107 Transaction Management
108 -------------------------------------------------------------------------- */
110 static inline u32 acpi_ec_read_status(struct acpi_ec *ec)
114 acpi_hw_low_level_read(8, &status, &ec->status_addr);
118 static int acpi_ec_wait(struct acpi_ec *ec, unsigned int event)
122 ACPI_FUNCTION_TRACE("acpi_ec_wait");
124 ec->expect_event = event;
127 result = wait_event_interruptible_timeout(ec->wait,
129 msecs_to_jiffies(ACPI_EC_DELAY));
131 ec->expect_event = 0;
135 ACPI_DEBUG_PRINT((ACPI_DB_ERROR," result = %d ", result));
136 return_VALUE(result);
140 * Verify that the event in question has actually happened by
141 * querying EC status. Do the check even if operation timed-out
142 * to make sure that we did not miss interrupt.
145 case ACPI_EC_EVENT_OBF:
146 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_OBF)
150 case ACPI_EC_EVENT_IBE:
151 if (~acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF)
156 return_VALUE(-ETIME);
162 acpi_ec_enter_burst_mode (
168 ACPI_FUNCTION_TRACE("acpi_ec_enter_burst_mode");
170 status = acpi_ec_read_status(ec);
171 if (status != -EINVAL &&
172 !(status & ACPI_EC_FLAG_BURST)){
173 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE, &ec->command_addr);
174 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
176 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
177 return_VALUE(-EINVAL);
179 acpi_hw_low_level_read(8, &tmp, &ec->data_addr);
180 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
181 if(tmp != 0x90 ) {/* Burst ACK byte*/
182 return_VALUE(-EINVAL);
186 atomic_set(&ec->leaving_burst , 0);
191 acpi_ec_leave_burst_mode (
196 ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
198 atomic_set(&ec->leaving_burst , 1);
199 status = acpi_ec_read_status(ec);
200 if (status != -EINVAL &&
201 (status & ACPI_EC_FLAG_BURST)){
202 acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->command_addr);
203 status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
205 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
206 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,"------->wait fail\n"));
207 return_VALUE(-EINVAL);
209 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
210 status = acpi_ec_read_status(ec);
225 ACPI_FUNCTION_TRACE("acpi_ec_read");
228 return_VALUE(-EINVAL);
233 if (ec->global_lock) {
234 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
235 if (ACPI_FAILURE(status))
236 return_VALUE(-ENODEV);
239 WARN_ON(in_interrupt());
242 if(acpi_ec_enter_burst_mode(ec))
245 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->command_addr);
246 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
247 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
252 acpi_hw_low_level_write(8, address, &ec->data_addr);
253 status= acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
255 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
259 acpi_hw_low_level_read(8, data, &ec->data_addr);
260 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
262 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
266 acpi_ec_leave_burst_mode(ec);
270 acpi_release_global_lock(glk);
272 if(atomic_read(&ec->leaving_burst) == 2){
273 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
274 while(atomic_read(&ec->pending_gpe)){
277 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
281 return_VALUE(status);
295 ACPI_FUNCTION_TRACE("acpi_ec_write");
298 return_VALUE(-EINVAL);
300 if (ec->global_lock) {
301 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
302 if (ACPI_FAILURE(status))
303 return_VALUE(-ENODEV);
306 WARN_ON(in_interrupt());
309 if(acpi_ec_enter_burst_mode(ec))
312 status = acpi_ec_read_status(ec);
313 if (status != -EINVAL &&
314 !(status & ACPI_EC_FLAG_BURST)){
315 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE, &ec->command_addr);
316 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
319 acpi_hw_low_level_read(8, &tmp, &ec->data_addr);
320 if(tmp != 0x90 ) /* Burst ACK byte*/
323 /*Now we are in burst mode*/
325 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->command_addr);
326 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
327 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
332 acpi_hw_low_level_write(8, address, &ec->data_addr);
333 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
335 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
339 acpi_hw_low_level_write(8, data, &ec->data_addr);
340 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
341 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
345 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
349 acpi_ec_leave_burst_mode(ec);
353 acpi_release_global_lock(glk);
355 if(atomic_read(&ec->leaving_burst) == 2){
356 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
357 while(atomic_read(&ec->pending_gpe)){
360 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
364 return_VALUE(status);
368 * Externally callable EC access functions. For now, assume 1 EC only
371 ec_read(u8 addr, u8 *val)
380 ec = acpi_driver_data(first_ec);
382 err = acpi_ec_read(ec, addr, &temp_data);
391 EXPORT_SYMBOL(ec_read);
394 ec_write(u8 addr, u8 val)
402 ec = acpi_driver_data(first_ec);
404 err = acpi_ec_write(ec, addr, val);
408 EXPORT_SYMBOL(ec_write);
419 ACPI_FUNCTION_TRACE("acpi_ec_query");
422 return_VALUE(-EINVAL);
425 if (ec->global_lock) {
426 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
427 if (ACPI_FAILURE(status))
428 return_VALUE(-ENODEV);
432 if(acpi_ec_enter_burst_mode(ec))
435 * Query the EC to find out which _Qxx method we need to evaluate.
436 * Note that successful completion of the query causes the ACPI_EC_SCI
437 * bit to be cleared (and thus clearing the interrupt source).
439 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->command_addr);
440 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
442 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
446 acpi_hw_low_level_read(8, data, &ec->data_addr);
447 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
452 acpi_ec_leave_burst_mode(ec);
456 acpi_release_global_lock(glk);
458 if(atomic_read(&ec->leaving_burst) == 2){
459 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
460 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
463 return_VALUE(status);
467 /* --------------------------------------------------------------------------
469 -------------------------------------------------------------------------- */
471 struct acpi_ec_query_data {
480 struct acpi_ec *ec = (struct acpi_ec *) ec_cxt;
482 int result = -ENODATA;
483 static char object_name[5] = {'_','Q','0','0','\0'};
484 const char hex[] = {'0','1','2','3','4','5','6','7',
485 '8','9','A','B','C','D','E','F'};
487 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
489 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
490 result = acpi_ec_query(ec, &value);
495 object_name[2] = hex[((value >> 4) & 0x0F)];
496 object_name[3] = hex[(value & 0x0F)];
498 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
500 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
502 atomic_dec(&ec->pending_gpe);
507 acpi_ec_gpe_handler (
510 acpi_status status = AE_OK;
512 struct acpi_ec *ec = (struct acpi_ec *) data;
515 return ACPI_INTERRUPT_NOT_HANDLED;
517 acpi_disable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
519 value = acpi_ec_read_status(ec);
521 if((value & ACPI_EC_FLAG_IBF) &&
522 !(value & ACPI_EC_FLAG_BURST) &&
523 (atomic_read(&ec->leaving_burst) == 0)) {
525 * the embedded controller disables
526 * burst mode for any reason other
527 * than the burst disable command
528 * to process critical event.
530 atomic_set(&ec->leaving_burst , 2); /* block current pending transaction
534 if ((ec->expect_event == ACPI_EC_EVENT_OBF &&
535 (value & ACPI_EC_FLAG_OBF)) ||
536 (ec->expect_event == ACPI_EC_EVENT_IBE &&
537 !(value & ACPI_EC_FLAG_IBF))) {
538 ec->expect_event = 0;
540 return ACPI_INTERRUPT_HANDLED;
544 if (value & ACPI_EC_FLAG_SCI){
545 atomic_add(1, &ec->pending_gpe) ;
546 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
547 acpi_ec_gpe_query, ec);
548 return status == AE_OK ?
549 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
551 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
552 return status == AE_OK ?
553 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
556 /* --------------------------------------------------------------------------
557 Address Space Management
558 -------------------------------------------------------------------------- */
561 acpi_ec_space_setup (
562 acpi_handle region_handle,
564 void *handler_context,
565 void **return_context)
568 * The EC object is in the handler context and is needed
569 * when calling the acpi_ec_space_handler.
571 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
572 handler_context : NULL;
579 acpi_ec_space_handler (
581 acpi_physical_address address,
584 void *handler_context,
585 void *region_context)
588 struct acpi_ec *ec = NULL;
590 acpi_integer f_v = 0;
593 ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
595 if ((address > 0xFF) || !value || !handler_context)
596 return_VALUE(AE_BAD_PARAMETER);
598 if (bit_width != 8 && acpi_strict) {
599 printk(KERN_WARNING PREFIX "acpi_ec_space_handler: bit_width should be 8\n");
600 return_VALUE(AE_BAD_PARAMETER);
603 ec = (struct acpi_ec *) handler_context;
609 result = acpi_ec_read(ec, (u8) address, (u32 *)&temp);
612 result = acpi_ec_write(ec, (u8) address, (u8) temp);
622 if (function == ACPI_READ)
623 f_v |= temp << 8 * i;
624 if (function == ACPI_WRITE)
631 if (function == ACPI_READ) {
632 f_v |= temp << 8 * i;
640 return_VALUE(AE_BAD_PARAMETER);
643 return_VALUE(AE_NOT_FOUND);
646 return_VALUE(AE_TIME);
654 /* --------------------------------------------------------------------------
656 -------------------------------------------------------------------------- */
658 static struct proc_dir_entry *acpi_ec_dir;
662 acpi_ec_read_info (struct seq_file *seq, void *offset)
664 struct acpi_ec *ec = (struct acpi_ec *) seq->private;
666 ACPI_FUNCTION_TRACE("acpi_ec_read_info");
671 seq_printf(seq, "gpe bit: 0x%02x\n",
673 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
674 (u32) ec->status_addr.address, (u32) ec->data_addr.address);
675 seq_printf(seq, "use global lock: %s\n",
676 ec->global_lock?"yes":"no");
677 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
683 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
685 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
688 static struct file_operations acpi_ec_info_ops = {
689 .open = acpi_ec_info_open_fs,
692 .release = single_release,
693 .owner = THIS_MODULE,
698 struct acpi_device *device)
700 struct proc_dir_entry *entry;
702 ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
704 if (!acpi_device_dir(device)) {
705 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
707 if (!acpi_device_dir(device))
708 return_VALUE(-ENODEV);
711 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
712 acpi_device_dir(device));
714 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
715 "Unable to create '%s' fs entry\n",
718 entry->proc_fops = &acpi_ec_info_ops;
719 entry->data = acpi_driver_data(device);
720 entry->owner = THIS_MODULE;
729 struct acpi_device *device)
731 ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
733 if (acpi_device_dir(device)) {
734 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
735 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
736 acpi_device_dir(device) = NULL;
743 /* --------------------------------------------------------------------------
745 -------------------------------------------------------------------------- */
749 struct acpi_device *device)
756 ACPI_FUNCTION_TRACE("acpi_ec_add");
759 return_VALUE(-EINVAL);
761 ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
763 return_VALUE(-ENOMEM);
764 memset(ec, 0, sizeof(struct acpi_ec));
766 ec->handle = device->handle;
768 atomic_set(&ec->pending_gpe, 0);
769 atomic_set(&ec->leaving_burst , 1);
770 init_MUTEX(&ec->sem);
771 init_waitqueue_head(&ec->wait);
772 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
773 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
774 acpi_driver_data(device) = ec;
776 /* Use the global lock for all EC transactions? */
777 acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
779 /* If our UID matches the UID for the ECDT-enumerated EC,
780 we now have the *real* EC info, so kill the makeshift one.*/
781 acpi_evaluate_integer(ec->handle, "_UID", NULL, &uid);
782 if (ec_ecdt && ec_ecdt->uid == uid) {
783 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
784 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
786 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit, &acpi_ec_gpe_handler);
791 /* Get GPE bit assignment (EC events). */
792 /* TODO: Add support for _GPE returning a package */
793 status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe_bit);
794 if (ACPI_FAILURE(status)) {
795 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
796 "Error obtaining GPE bit assignment\n"));
801 result = acpi_ec_add_fs(device);
805 printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
806 acpi_device_name(device), acpi_device_bid(device),
816 return_VALUE(result);
822 struct acpi_device *device,
827 ACPI_FUNCTION_TRACE("acpi_ec_remove");
830 return_VALUE(-EINVAL);
832 ec = acpi_driver_data(device);
834 acpi_ec_remove_fs(device);
844 struct acpi_resource *resource,
847 struct acpi_ec *ec = (struct acpi_ec *) context;
848 struct acpi_generic_address *addr;
850 if (resource->id != ACPI_RSTYPE_IO) {
855 * The first address region returned is the data port, and
856 * the second address region returned is the status/command
859 if (ec->data_addr.register_bit_width == 0) {
860 addr = &ec->data_addr;
861 } else if (ec->command_addr.register_bit_width == 0) {
862 addr = &ec->command_addr;
864 return AE_CTRL_TERMINATE;
867 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
868 addr->register_bit_width = 8;
869 addr->register_bit_offset = 0;
870 addr->address = resource->data.io.min_base_address;
878 struct acpi_device *device)
883 ACPI_FUNCTION_TRACE("acpi_ec_start");
886 return_VALUE(-EINVAL);
888 ec = acpi_driver_data(device);
891 return_VALUE(-EINVAL);
894 * Get I/O port addresses. Convert to GAS format.
896 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
897 acpi_ec_io_ports, ec);
898 if (ACPI_FAILURE(status) || ec->command_addr.register_bit_width == 0) {
899 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error getting I/O port addresses"));
900 return_VALUE(-ENODEV);
903 ec->status_addr = ec->command_addr;
905 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
906 (u32) ec->gpe_bit, (u32) ec->command_addr.address,
907 (u32) ec->data_addr.address));
910 * Install GPE handler
912 status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
913 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler, ec);
914 if (ACPI_FAILURE(status)) {
915 return_VALUE(-ENODEV);
917 acpi_set_gpe_type (NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
918 acpi_enable_gpe (NULL, ec->gpe_bit, ACPI_NOT_ISR);
920 status = acpi_install_address_space_handler (ec->handle,
921 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
922 &acpi_ec_space_setup, ec);
923 if (ACPI_FAILURE(status)) {
924 acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
925 return_VALUE(-ENODEV);
934 struct acpi_device *device,
940 ACPI_FUNCTION_TRACE("acpi_ec_stop");
943 return_VALUE(-EINVAL);
945 ec = acpi_driver_data(device);
947 status = acpi_remove_address_space_handler(ec->handle,
948 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
949 if (ACPI_FAILURE(status))
950 return_VALUE(-ENODEV);
952 status = acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
953 if (ACPI_FAILURE(status))
954 return_VALUE(-ENODEV);
959 static acpi_status __init
960 acpi_fake_ecdt_callback (
968 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
969 acpi_ec_io_ports, ec_ecdt);
970 if (ACPI_FAILURE(status))
972 ec_ecdt->status_addr = ec_ecdt->command_addr;
975 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
977 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->gpe_bit);
978 if (ACPI_FAILURE(status))
980 ec_ecdt->global_lock = TRUE;
981 ec_ecdt->handle = handle;
983 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
984 (u32) ec_ecdt->gpe_bit, (u32) ec_ecdt->command_addr.address,
985 (u32) ec_ecdt->data_addr.address);
987 return AE_CTRL_TERMINATE;
991 * Some BIOS (such as some from Gateway laptops) access EC region very early
992 * such as in BAT0._INI or EC._INI before an EC device is found and
993 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
994 * required, but if EC regison is accessed early, it is required.
995 * The routine tries to workaround the BIOS bug by pre-scan EC device
996 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
997 * op region (since _REG isn't invoked yet). The assumption is true for
1001 acpi_ec_fake_ecdt(void)
1006 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1008 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1013 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
1015 status = acpi_get_devices (ACPI_EC_HID,
1016 acpi_fake_ecdt_callback,
1019 if (ACPI_FAILURE(status)) {
1027 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1032 acpi_ec_get_real_ecdt(void)
1035 struct acpi_table_ecdt *ecdt_ptr;
1037 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1038 (struct acpi_table_header **) &ecdt_ptr);
1039 if (ACPI_FAILURE(status))
1042 printk(KERN_INFO PREFIX "Found ECDT\n");
1045 * Generate a temporary ec context to use until the namespace is scanned
1047 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1050 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
1052 init_MUTEX(&ec_ecdt->sem);
1053 init_waitqueue_head(&ec_ecdt->wait);
1054 ec_ecdt->command_addr = ecdt_ptr->ec_control;
1055 ec_ecdt->status_addr = ecdt_ptr->ec_control;
1056 ec_ecdt->data_addr = ecdt_ptr->ec_data;
1057 ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
1058 /* use the GL just to be safe */
1059 ec_ecdt->global_lock = TRUE;
1060 ec_ecdt->uid = ecdt_ptr->uid;
1062 status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
1063 if (ACPI_FAILURE(status)) {
1069 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1076 static int __initdata acpi_fake_ecdt_enabled;
1078 acpi_ec_ecdt_probe (void)
1083 ret = acpi_ec_get_real_ecdt();
1084 /* Try to make a fake ECDT */
1085 if (ret && acpi_fake_ecdt_enabled) {
1086 ret = acpi_ec_fake_ecdt();
1093 * Install GPE handler
1095 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
1096 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler,
1098 if (ACPI_FAILURE(status)) {
1101 acpi_set_gpe_type (NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1102 acpi_enable_gpe (NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
1104 status = acpi_install_address_space_handler (ACPI_ROOT_OBJECT,
1105 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
1106 &acpi_ec_space_setup, ec_ecdt);
1107 if (ACPI_FAILURE(status)) {
1108 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
1109 &acpi_ec_gpe_handler);
1116 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1124 static int __init acpi_ec_init (void)
1128 ACPI_FUNCTION_TRACE("acpi_ec_init");
1133 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1135 return_VALUE(-ENODEV);
1137 /* Now register the driver for the EC */
1138 result = acpi_bus_register_driver(&acpi_ec_driver);
1140 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1141 return_VALUE(-ENODEV);
1144 return_VALUE(result);
1147 subsys_initcall(acpi_ec_init);
1149 /* EC driver currently not unloadable */
1154 ACPI_FUNCTION_TRACE("acpi_ec_exit");
1156 acpi_bus_unregister_driver(&acpi_ec_driver);
1158 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1164 static int __init acpi_fake_ecdt_setup(char *str)
1166 acpi_fake_ecdt_enabled = 1;
1169 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);