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_DEBUG_PRINT((ACPI_DB_INFO,"entering burst mode \n"));
174 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE, &ec->command_addr);
175 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
177 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
178 ACPI_DEBUG_PRINT((ACPI_DB_ERROR," status = %d\n", status));
179 return_VALUE(-EINVAL);
181 acpi_hw_low_level_read(8, &tmp, &ec->data_addr);
182 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
183 if(tmp != 0x90 ) {/* Burst ACK byte*/
184 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,"Ack failed \n"));
185 return_VALUE(-EINVAL);
188 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"already be in burst mode \n"));
189 atomic_set(&ec->leaving_burst , 0);
194 acpi_ec_leave_burst_mode (
199 ACPI_FUNCTION_TRACE("acpi_ec_leave_burst_mode");
201 atomic_set(&ec->leaving_burst , 1);
202 status = acpi_ec_read_status(ec);
203 if (status != -EINVAL &&
204 (status & ACPI_EC_FLAG_BURST)){
205 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"leaving burst mode\n"));
206 acpi_hw_low_level_write(8, ACPI_EC_BURST_DISABLE, &ec->command_addr);
207 status = acpi_ec_wait(ec, ACPI_EC_FLAG_IBF);
209 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
210 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,"------->wait fail\n"));
211 return_VALUE(-EINVAL);
213 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
214 status = acpi_ec_read_status(ec);
215 if (status != -EINVAL &&
216 (status & ACPI_EC_FLAG_BURST)) {
217 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,"------->status fail\n"));
218 return_VALUE(-EINVAL);
221 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"already be in Non-burst mode \n"));
222 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"leaving burst mode\n"));
236 ACPI_FUNCTION_TRACE("acpi_ec_read");
239 return_VALUE(-EINVAL);
244 if (ec->global_lock) {
245 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
246 if (ACPI_FAILURE(status))
247 return_VALUE(-ENODEV);
250 WARN_ON(in_interrupt());
253 if(acpi_ec_enter_burst_mode(ec))
256 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->command_addr);
257 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
258 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
263 acpi_hw_low_level_write(8, address, &ec->data_addr);
264 status= acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
266 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
270 acpi_hw_low_level_read(8, data, &ec->data_addr);
271 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
273 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
277 acpi_ec_leave_burst_mode(ec);
281 acpi_release_global_lock(glk);
283 if(atomic_read(&ec->leaving_burst) == 2){
284 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
285 while(!atomic_read(&ec->pending_gpe)){
288 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
292 return_VALUE(status);
306 ACPI_FUNCTION_TRACE("acpi_ec_write");
309 return_VALUE(-EINVAL);
311 if (ec->global_lock) {
312 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
313 if (ACPI_FAILURE(status))
314 return_VALUE(-ENODEV);
317 WARN_ON(in_interrupt());
320 if(acpi_ec_enter_burst_mode(ec))
323 status = acpi_ec_read_status(ec);
324 if (status != -EINVAL &&
325 !(status & ACPI_EC_FLAG_BURST)){
326 acpi_hw_low_level_write(8, ACPI_EC_BURST_ENABLE, &ec->command_addr);
327 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
330 acpi_hw_low_level_read(8, &tmp, &ec->data_addr);
331 if(tmp != 0x90 ) /* Burst ACK byte*/
334 /*Now we are in burst mode*/
336 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->command_addr);
337 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
338 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
343 acpi_hw_low_level_write(8, address, &ec->data_addr);
344 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
346 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
350 acpi_hw_low_level_write(8, data, &ec->data_addr);
351 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
352 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
356 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
360 acpi_ec_leave_burst_mode(ec);
364 acpi_release_global_lock(glk);
366 if(atomic_read(&ec->leaving_burst) == 2){
367 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
368 while(!atomic_read(&ec->pending_gpe)){
371 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
375 return_VALUE(status);
379 * Externally callable EC access functions. For now, assume 1 EC only
382 ec_read(u8 addr, u8 *val)
391 ec = acpi_driver_data(first_ec);
393 err = acpi_ec_read(ec, addr, &temp_data);
402 EXPORT_SYMBOL(ec_read);
405 ec_write(u8 addr, u8 val)
413 ec = acpi_driver_data(first_ec);
415 err = acpi_ec_write(ec, addr, val);
419 EXPORT_SYMBOL(ec_write);
430 ACPI_FUNCTION_TRACE("acpi_ec_query");
433 return_VALUE(-EINVAL);
437 if (ec->global_lock) {
438 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
439 if (ACPI_FAILURE(status))
440 return_VALUE(-ENODEV);
444 if(acpi_ec_enter_burst_mode(ec))
447 * Query the EC to find out which _Qxx method we need to evaluate.
448 * Note that successful completion of the query causes the ACPI_EC_SCI
449 * bit to be cleared (and thus clearing the interrupt source).
451 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->command_addr);
452 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
454 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
458 acpi_hw_low_level_read(8, data, &ec->data_addr);
459 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
464 acpi_ec_leave_burst_mode(ec);
468 acpi_release_global_lock(glk);
470 if(atomic_read(&ec->leaving_burst) == 2){
471 ACPI_DEBUG_PRINT((ACPI_DB_INFO,"aborted, retry ...\n"));
472 while(!atomic_read(&ec->pending_gpe)){
475 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
479 return_VALUE(status);
483 /* --------------------------------------------------------------------------
485 -------------------------------------------------------------------------- */
487 struct acpi_ec_query_data {
496 struct acpi_ec *ec = (struct acpi_ec *) ec_cxt;
498 int result = -ENODATA;
499 static char object_name[5] = {'_','Q','0','0','\0'};
500 const char hex[] = {'0','1','2','3','4','5','6','7',
501 '8','9','A','B','C','D','E','F'};
503 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
505 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI)
506 result = acpi_ec_query(ec, &value);
511 object_name[2] = hex[((value >> 4) & 0x0F)];
512 object_name[3] = hex[(value & 0x0F)];
514 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
516 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
517 atomic_dec(&ec->pending_gpe);
523 acpi_ec_gpe_handler (
526 acpi_status status = AE_OK;
528 struct acpi_ec *ec = (struct acpi_ec *) data;
531 return ACPI_INTERRUPT_NOT_HANDLED;
533 acpi_disable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
535 value = acpi_ec_read_status(ec);
537 if((value & ACPI_EC_FLAG_IBF) &&
538 !(value & ACPI_EC_FLAG_BURST) &&
539 (atomic_read(&ec->leaving_burst) == 0)) {
541 * the embedded controller disables
542 * burst mode for any reason other
543 * than the burst disable command
544 * to process critical event.
546 atomic_set(&ec->leaving_burst , 2); /* block current pending transaction
550 if ((ec->expect_event == ACPI_EC_EVENT_OBF &&
551 (value & ACPI_EC_FLAG_OBF)) ||
552 (ec->expect_event == ACPI_EC_EVENT_IBE &&
553 !(value & ACPI_EC_FLAG_IBF))) {
554 ec->expect_event = 0;
560 if (value & ACPI_EC_FLAG_SCI){
561 atomic_add(1, &ec->pending_gpe) ;
562 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
563 acpi_ec_gpe_query, ec);
566 return status == AE_OK ?
567 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
570 /* --------------------------------------------------------------------------
571 Address Space Management
572 -------------------------------------------------------------------------- */
575 acpi_ec_space_setup (
576 acpi_handle region_handle,
578 void *handler_context,
579 void **return_context)
582 * The EC object is in the handler context and is needed
583 * when calling the acpi_ec_space_handler.
585 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
586 handler_context : NULL;
593 acpi_ec_space_handler (
595 acpi_physical_address address,
598 void *handler_context,
599 void *region_context)
602 struct acpi_ec *ec = NULL;
604 acpi_integer f_v = 0;
607 ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
609 if ((address > 0xFF) || !value || !handler_context)
610 return_VALUE(AE_BAD_PARAMETER);
613 printk(KERN_WARNING PREFIX "acpi_ec_space_handler: bit_width should be 8\n");
615 return_VALUE(AE_BAD_PARAMETER);
618 ec = (struct acpi_ec *) handler_context;
623 result = acpi_ec_read(ec, (u8) address, &temp);
624 *value = (acpi_integer) temp;
627 result = acpi_ec_write(ec, (u8) address, (u8) *value);
638 if(function == ACPI_READ)
639 f_v |= (acpi_integer) (*value) << 8*i;
640 if(function == ACPI_WRITE)
647 if(function == ACPI_READ){
648 f_v |= (acpi_integer) (*value) << 8*i;
656 return_VALUE(AE_BAD_PARAMETER);
659 return_VALUE(AE_NOT_FOUND);
662 return_VALUE(AE_TIME);
672 /* --------------------------------------------------------------------------
674 -------------------------------------------------------------------------- */
676 static struct proc_dir_entry *acpi_ec_dir;
680 acpi_ec_read_info (struct seq_file *seq, void *offset)
682 struct acpi_ec *ec = (struct acpi_ec *) seq->private;
684 ACPI_FUNCTION_TRACE("acpi_ec_read_info");
689 seq_printf(seq, "gpe bit: 0x%02x\n",
691 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
692 (u32) ec->status_addr.address, (u32) ec->data_addr.address);
693 seq_printf(seq, "use global lock: %s\n",
694 ec->global_lock?"yes":"no");
700 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
702 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
705 static struct file_operations acpi_ec_info_ops = {
706 .open = acpi_ec_info_open_fs,
709 .release = single_release,
710 .owner = THIS_MODULE,
715 struct acpi_device *device)
717 struct proc_dir_entry *entry;
719 ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
721 if (!acpi_device_dir(device)) {
722 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
724 if (!acpi_device_dir(device))
725 return_VALUE(-ENODEV);
728 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
729 acpi_device_dir(device));
731 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
732 "Unable to create '%s' fs entry\n",
735 entry->proc_fops = &acpi_ec_info_ops;
736 entry->data = acpi_driver_data(device);
737 entry->owner = THIS_MODULE;
746 struct acpi_device *device)
748 ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
750 if (acpi_device_dir(device)) {
751 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
752 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
753 acpi_device_dir(device) = NULL;
760 /* --------------------------------------------------------------------------
762 -------------------------------------------------------------------------- */
766 struct acpi_device *device)
773 ACPI_FUNCTION_TRACE("acpi_ec_add");
776 return_VALUE(-EINVAL);
778 ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
780 return_VALUE(-ENOMEM);
781 memset(ec, 0, sizeof(struct acpi_ec));
783 ec->handle = device->handle;
785 atomic_set(&ec->pending_gpe, 0);
786 atomic_set(&ec->leaving_burst , 1);
787 init_MUTEX(&ec->sem);
788 init_waitqueue_head(&ec->wait);
789 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
790 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
791 acpi_driver_data(device) = ec;
793 /* Use the global lock for all EC transactions? */
794 acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
796 /* If our UID matches the UID for the ECDT-enumerated EC,
797 we now have the *real* EC info, so kill the makeshift one.*/
798 acpi_evaluate_integer(ec->handle, "_UID", NULL, &uid);
799 if (ec_ecdt && ec_ecdt->uid == uid) {
800 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
801 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
803 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit, &acpi_ec_gpe_handler);
808 /* Get GPE bit assignment (EC events). */
809 /* TODO: Add support for _GPE returning a package */
810 status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe_bit);
811 if (ACPI_FAILURE(status)) {
812 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
813 "Error obtaining GPE bit assignment\n"));
818 result = acpi_ec_add_fs(device);
822 printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
823 acpi_device_name(device), acpi_device_bid(device),
833 return_VALUE(result);
839 struct acpi_device *device,
844 ACPI_FUNCTION_TRACE("acpi_ec_remove");
847 return_VALUE(-EINVAL);
849 ec = acpi_driver_data(device);
851 acpi_ec_remove_fs(device);
861 struct acpi_resource *resource,
864 struct acpi_ec *ec = (struct acpi_ec *) context;
865 struct acpi_generic_address *addr;
867 if (resource->id != ACPI_RSTYPE_IO) {
872 * The first address region returned is the data port, and
873 * the second address region returned is the status/command
876 if (ec->data_addr.register_bit_width == 0) {
877 addr = &ec->data_addr;
878 } else if (ec->command_addr.register_bit_width == 0) {
879 addr = &ec->command_addr;
881 return AE_CTRL_TERMINATE;
884 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
885 addr->register_bit_width = 8;
886 addr->register_bit_offset = 0;
887 addr->address = resource->data.io.min_base_address;
895 struct acpi_device *device)
900 ACPI_FUNCTION_TRACE("acpi_ec_start");
903 return_VALUE(-EINVAL);
905 ec = acpi_driver_data(device);
908 return_VALUE(-EINVAL);
911 * Get I/O port addresses. Convert to GAS format.
913 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
914 acpi_ec_io_ports, ec);
915 if (ACPI_FAILURE(status) || ec->command_addr.register_bit_width == 0) {
916 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error getting I/O port addresses"));
917 return_VALUE(-ENODEV);
920 ec->status_addr = ec->command_addr;
922 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
923 (u32) ec->gpe_bit, (u32) ec->command_addr.address,
924 (u32) ec->data_addr.address));
927 * Install GPE handler
929 status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
930 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler, ec);
931 if (ACPI_FAILURE(status)) {
932 return_VALUE(-ENODEV);
934 acpi_set_gpe_type (NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
935 acpi_enable_gpe (NULL, ec->gpe_bit, ACPI_NOT_ISR);
937 status = acpi_install_address_space_handler (ec->handle,
938 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
939 &acpi_ec_space_setup, ec);
940 if (ACPI_FAILURE(status)) {
941 acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
942 return_VALUE(-ENODEV);
951 struct acpi_device *device,
957 ACPI_FUNCTION_TRACE("acpi_ec_stop");
960 return_VALUE(-EINVAL);
962 ec = acpi_driver_data(device);
964 status = acpi_remove_address_space_handler(ec->handle,
965 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
966 if (ACPI_FAILURE(status))
967 return_VALUE(-ENODEV);
969 status = acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
970 if (ACPI_FAILURE(status))
971 return_VALUE(-ENODEV);
976 static acpi_status __init
977 acpi_fake_ecdt_callback (
985 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
986 acpi_ec_io_ports, ec_ecdt);
987 if (ACPI_FAILURE(status))
989 ec_ecdt->status_addr = ec_ecdt->command_addr;
992 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
994 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->gpe_bit);
995 if (ACPI_FAILURE(status))
997 ec_ecdt->global_lock = TRUE;
998 ec_ecdt->handle = handle;
1000 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
1001 (u32) ec_ecdt->gpe_bit, (u32) ec_ecdt->command_addr.address,
1002 (u32) ec_ecdt->data_addr.address);
1004 return AE_CTRL_TERMINATE;
1008 * Some BIOS (such as some from Gateway laptops) access EC region very early
1009 * such as in BAT0._INI or EC._INI before an EC device is found and
1010 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
1011 * required, but if EC regison is accessed early, it is required.
1012 * The routine tries to workaround the BIOS bug by pre-scan EC device
1013 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
1014 * op region (since _REG isn't invoked yet). The assumption is true for
1015 * all systems found.
1018 acpi_ec_fake_ecdt(void)
1023 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
1025 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1030 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
1032 status = acpi_get_devices (ACPI_EC_HID,
1033 acpi_fake_ecdt_callback,
1036 if (ACPI_FAILURE(status)) {
1044 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
1049 acpi_ec_get_real_ecdt(void)
1052 struct acpi_table_ecdt *ecdt_ptr;
1054 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
1055 (struct acpi_table_header **) &ecdt_ptr);
1056 if (ACPI_FAILURE(status))
1059 printk(KERN_INFO PREFIX "Found ECDT\n");
1062 * Generate a temporary ec context to use until the namespace is scanned
1064 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
1067 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
1069 init_MUTEX(&ec_ecdt->sem);
1070 init_waitqueue_head(&ec_ecdt->wait);
1071 ec_ecdt->command_addr = ecdt_ptr->ec_control;
1072 ec_ecdt->status_addr = ecdt_ptr->ec_control;
1073 ec_ecdt->data_addr = ecdt_ptr->ec_data;
1074 ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
1075 /* use the GL just to be safe */
1076 ec_ecdt->global_lock = TRUE;
1077 ec_ecdt->uid = ecdt_ptr->uid;
1079 status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
1080 if (ACPI_FAILURE(status)) {
1086 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1093 static int __initdata acpi_fake_ecdt_enabled;
1095 acpi_ec_ecdt_probe (void)
1100 ret = acpi_ec_get_real_ecdt();
1101 /* Try to make a fake ECDT */
1102 if (ret && acpi_fake_ecdt_enabled) {
1103 ret = acpi_ec_fake_ecdt();
1110 * Install GPE handler
1112 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
1113 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler,
1115 if (ACPI_FAILURE(status)) {
1118 acpi_set_gpe_type (NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1119 acpi_enable_gpe (NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
1121 status = acpi_install_address_space_handler (ACPI_ROOT_OBJECT,
1122 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
1123 &acpi_ec_space_setup, ec_ecdt);
1124 if (ACPI_FAILURE(status)) {
1125 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
1126 &acpi_ec_gpe_handler);
1133 printk(KERN_ERR PREFIX "Could not use ECDT\n");
1141 static int __init acpi_ec_init (void)
1145 ACPI_FUNCTION_TRACE("acpi_ec_init");
1150 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1152 return_VALUE(-ENODEV);
1154 /* Now register the driver for the EC */
1155 result = acpi_bus_register_driver(&acpi_ec_driver);
1157 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1158 return_VALUE(-ENODEV);
1161 return_VALUE(result);
1164 subsys_initcall(acpi_ec_init);
1166 /* EC driver currently not unloadable */
1171 ACPI_FUNCTION_TRACE("acpi_ec_exit");
1173 acpi_bus_unregister_driver(&acpi_ec_driver);
1175 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1181 static int __init acpi_fake_ecdt_setup(char *str)
1183 acpi_fake_ecdt_enabled = 1;
1186 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);