ACPI: EC: Unify poll and interrupt gpe handlers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / ec.c
blob0f232e719daf013411475ad1ea293a98eb5f8ac9
1 /*
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>
35 #include <asm/io.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")
42 #define ACPI_EC_COMPONENT 0x00100000
43 #define ACPI_EC_CLASS "embedded_controller"
44 #define ACPI_EC_HID "PNP0C09"
45 #define ACPI_EC_DRIVER_NAME "ACPI Embedded Controller Driver"
46 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
47 #define ACPI_EC_FILE_INFO "info"
49 /* EC status register */
50 #define ACPI_EC_FLAG_OBF 0x01 /* Output buffer full */
51 #define ACPI_EC_FLAG_IBF 0x02 /* Input buffer full */
52 #define ACPI_EC_FLAG_BURST 0x10 /* burst mode */
53 #define ACPI_EC_FLAG_SCI 0x20 /* EC-SCI occurred */
55 /* EC commands */
56 #define ACPI_EC_COMMAND_READ 0x80
57 #define ACPI_EC_COMMAND_WRITE 0x81
58 #define ACPI_EC_BURST_ENABLE 0x82
59 #define ACPI_EC_BURST_DISABLE 0x83
60 #define ACPI_EC_COMMAND_QUERY 0x84
62 /* EC events */
63 enum {
64 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
65 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
68 #define ACPI_EC_DELAY 50 /* Wait 50ms max. during EC ops */
69 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
70 #define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
71 #define ACPI_EC_UDELAY_COUNT 1000 /* Wait 10ms max. during EC ops */
73 enum {
74 EC_INTR = 1, /* Output buffer full */
75 EC_POLL, /* Input buffer empty */
78 static int acpi_ec_remove(struct acpi_device *device, int type);
79 static int acpi_ec_start(struct acpi_device *device);
80 static int acpi_ec_stop(struct acpi_device *device, int type);
81 static int acpi_ec_add(struct acpi_device *device);
83 static struct acpi_driver acpi_ec_driver = {
84 .name = ACPI_EC_DRIVER_NAME,
85 .class = ACPI_EC_CLASS,
86 .ids = ACPI_EC_HID,
87 .ops = {
88 .add = acpi_ec_add,
89 .remove = acpi_ec_remove,
90 .start = acpi_ec_start,
91 .stop = acpi_ec_stop,
94 struct acpi_ec {
95 acpi_handle handle;
96 unsigned long uid;
97 unsigned long gpe_bit;
98 struct acpi_generic_address status_addr;
99 struct acpi_generic_address command_addr;
100 struct acpi_generic_address data_addr;
101 unsigned long global_lock;
102 struct semaphore sem;
103 unsigned int expect_event;
104 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
105 wait_queue_head_t wait;
108 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
109 static struct acpi_ec *ec_ecdt;
111 /* External interfaces use first EC only, so remember */
112 static struct acpi_device *first_ec;
113 static int acpi_ec_mode = EC_INTR;
115 /* --------------------------------------------------------------------------
116 Transaction Management
117 -------------------------------------------------------------------------- */
119 static u32 acpi_ec_read_status(struct acpi_ec *ec)
121 u32 status = 0;
123 acpi_hw_low_level_read(8, &status, &ec->status_addr);
124 return status;
127 static u32 acpi_ec_read_data(struct acpi_ec *ec)
129 u32 data = 0;
131 acpi_hw_low_level_read(8, &data, &ec->data_addr);
132 return data;
135 static void acpi_ec_write_cmd(struct acpi_ec *ec, u32 command)
137 acpi_hw_low_level_write(8, command, &ec->command_addr);
140 static void acpi_ec_write_data(struct acpi_ec *ec, u32 data)
142 acpi_hw_low_level_write(8, data, &ec->data_addr);
145 static int acpi_ec_check_status(u32 status, u8 event) {
147 switch (event) {
148 case ACPI_EC_EVENT_OBF_1:
149 if (status & ACPI_EC_FLAG_OBF)
150 return 1;
151 break;
152 case ACPI_EC_EVENT_IBF_0:
153 if (!(status & ACPI_EC_FLAG_IBF))
154 return 1;
155 break;
156 default:
157 break;
160 return 0;
163 static int acpi_ec_wait(struct acpi_ec *ec, u8 event)
165 int i = (acpi_ec_mode == EC_POLL) ? ACPI_EC_UDELAY_COUNT : 0;
166 long time_left;
168 ec->expect_event = event;
169 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
170 ec->expect_event = 0;
171 return 0;
174 do {
175 if (acpi_ec_mode == EC_POLL) {
176 udelay(ACPI_EC_UDELAY);
177 } else {
178 time_left = wait_event_timeout(ec->wait,
179 !ec->expect_event,
180 msecs_to_jiffies(ACPI_EC_DELAY));
181 if (time_left > 0) {
182 ec->expect_event = 0;
183 return 0;
186 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
187 ec->expect_event = 0;
188 return 0;
190 } while (--i > 0);
192 ec->expect_event = 0;
194 return -ETIME;
197 #ifdef ACPI_FUTURE_USAGE
199 * Note: samsung nv5000 doesn't work with ec burst mode.
200 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
202 int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
204 u32 tmp = 0;
205 u32 status = 0;
208 status = acpi_ec_read_status(ec);
209 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
210 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
211 if (status)
212 goto end;
213 acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
214 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
215 tmp = acpi_ec_read_data(ec);
216 if (tmp != 0x90) { /* Burst ACK byte */
217 return -EINVAL;
221 atomic_set(&ec->leaving_burst, 0);
222 return 0;
223 end:
224 ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
225 return -1;
228 int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
230 u32 status = 0;
233 status = acpi_ec_read_status(ec);
234 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
235 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
236 if(status)
237 goto end;
238 acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
239 acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
241 atomic_set(&ec->leaving_burst, 1);
242 return 0;
243 end:
244 ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
245 return -1;
247 #endif /* ACPI_FUTURE_USAGE */
249 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
250 const u8 *wdata, unsigned wdata_len,
251 u8 *rdata, unsigned rdata_len)
253 int result;
255 acpi_ec_write_cmd(ec, command);
257 for (; wdata_len > 0; wdata_len --) {
258 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
259 if (result)
260 return result;
261 acpi_ec_write_data(ec, *(wdata++));
264 if (command == ACPI_EC_COMMAND_WRITE) {
265 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
266 if (result)
267 return result;
270 for (; rdata_len > 0; rdata_len --) {
271 u32 d;
273 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
274 if (result)
275 return result;
277 d = acpi_ec_read_data(ec);
278 *(rdata++) = (u8) d;
281 return 0;
284 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
285 const u8 *wdata, unsigned wdata_len,
286 u8 *rdata, unsigned rdata_len)
288 int status;
289 u32 glk;
291 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
292 return -EINVAL;
294 if (rdata)
295 memset(rdata, 0, rdata_len);
297 if (ec->global_lock) {
298 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
299 if (ACPI_FAILURE(status))
300 return -ENODEV;
302 down(&ec->sem);
304 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
305 if (status) {
306 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
307 goto end;
310 status = acpi_ec_transaction_unlocked(ec, command,
311 wdata, wdata_len,
312 rdata, rdata_len);
314 end:
315 up(&ec->sem);
317 if (ec->global_lock)
318 acpi_release_global_lock(glk);
320 return status;
323 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u32 * data)
325 int result;
326 u8 d;
328 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
329 &address, 1, &d, 1);
330 *data = d;
331 return result;
333 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
335 u8 wdata[2] = { address, data };
336 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
337 wdata, 2, NULL, 0);
341 * Externally callable EC access functions. For now, assume 1 EC only
343 int ec_read(u8 addr, u8 * val)
345 struct acpi_ec *ec;
346 int err;
347 u32 temp_data;
349 if (!first_ec)
350 return -ENODEV;
352 ec = acpi_driver_data(first_ec);
354 err = acpi_ec_read(ec, addr, &temp_data);
356 if (!err) {
357 *val = temp_data;
358 return 0;
359 } else
360 return err;
363 EXPORT_SYMBOL(ec_read);
365 int ec_write(u8 addr, u8 val)
367 struct acpi_ec *ec;
368 int err;
370 if (!first_ec)
371 return -ENODEV;
373 ec = acpi_driver_data(first_ec);
375 err = acpi_ec_write(ec, addr, val);
377 return err;
380 EXPORT_SYMBOL(ec_write);
382 extern int ec_transaction(u8 command,
383 const u8 *wdata, unsigned wdata_len,
384 u8 *rdata, unsigned rdata_len)
386 struct acpi_ec *ec;
388 if (!first_ec)
389 return -ENODEV;
391 ec = acpi_driver_data(first_ec);
393 return acpi_ec_transaction(ec, command, wdata,
394 wdata_len, rdata, rdata_len);
397 static int acpi_ec_query(struct acpi_ec *ec, u32 * data)
399 int result;
400 u8 d;
402 if (!ec || !data)
403 return -EINVAL;
406 * Query the EC to find out which _Qxx method we need to evaluate.
407 * Note that successful completion of the query causes the ACPI_EC_SCI
408 * bit to be cleared (and thus clearing the interrupt source).
411 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
412 if (result)
413 return result;
415 if (!d)
416 return -ENODATA;
418 *data = d;
419 return 0;
422 /* --------------------------------------------------------------------------
423 Event Management
424 -------------------------------------------------------------------------- */
426 struct acpi_ec_query_data {
427 acpi_handle handle;
428 u8 data;
431 static void acpi_ec_gpe_query(void *ec_cxt)
433 struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
434 u32 value = 0;
435 static char object_name[5] = { '_', 'Q', '0', '0', '\0' };
436 const char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7',
437 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
441 if (!ec_cxt)
442 goto end;
444 value = acpi_ec_read_status(ec);
446 if (!(value & ACPI_EC_FLAG_SCI))
447 goto end;
449 if (acpi_ec_query(ec, &value))
450 goto end;
452 object_name[2] = hex[((value >> 4) & 0x0F)];
453 object_name[3] = hex[(value & 0x0F)];
455 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
457 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
459 end:
460 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
463 static u32 acpi_ec_gpe_handler(void *data)
465 acpi_status status = AE_OK;
466 u32 value;
467 u8 exec_mode;
468 struct acpi_ec *ec = (struct acpi_ec *)data;
470 acpi_clear_gpe(NULL, ec->gpe_bit, ACPI_ISR);
471 value = acpi_ec_read_status(ec);
473 if (acpi_ec_mode == EC_INTR) {
474 if (acpi_ec_check_status(value, ec->expect_event)) {
475 ec->expect_event = 0;
476 wake_up(&ec->wait);
478 exec_mode = OSL_EC_BURST_HANDLER;
479 } else {
480 exec_mode = OSL_EC_POLL_HANDLER;
483 if (value & ACPI_EC_FLAG_SCI) {
484 status = acpi_os_execute(exec_mode, acpi_ec_gpe_query, ec);
485 return status == AE_OK ?
486 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
488 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
489 return status == AE_OK ?
490 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
493 /* --------------------------------------------------------------------------
494 Address Space Management
495 -------------------------------------------------------------------------- */
497 static acpi_status
498 acpi_ec_space_setup(acpi_handle region_handle,
499 u32 function, void *handler_context, void **return_context)
502 * The EC object is in the handler context and is needed
503 * when calling the acpi_ec_space_handler.
505 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
506 handler_context : NULL;
508 return AE_OK;
511 static acpi_status
512 acpi_ec_space_handler(u32 function,
513 acpi_physical_address address,
514 u32 bit_width,
515 acpi_integer * value,
516 void *handler_context, void *region_context)
518 int result = 0;
519 struct acpi_ec *ec = NULL;
520 u64 temp = *value;
521 acpi_integer f_v = 0;
522 int i = 0;
525 if ((address > 0xFF) || !value || !handler_context)
526 return AE_BAD_PARAMETER;
528 if (bit_width != 8 && acpi_strict) {
529 return AE_BAD_PARAMETER;
532 ec = (struct acpi_ec *)handler_context;
534 next_byte:
535 switch (function) {
536 case ACPI_READ:
537 temp = 0;
538 result = acpi_ec_read(ec, (u8) address, (u32 *) & temp);
539 break;
540 case ACPI_WRITE:
541 result = acpi_ec_write(ec, (u8) address, (u8) temp);
542 break;
543 default:
544 result = -EINVAL;
545 goto out;
546 break;
549 bit_width -= 8;
550 if (bit_width) {
551 if (function == ACPI_READ)
552 f_v |= temp << 8 * i;
553 if (function == ACPI_WRITE)
554 temp >>= 8;
555 i++;
556 address++;
557 goto next_byte;
560 if (function == ACPI_READ) {
561 f_v |= temp << 8 * i;
562 *value = f_v;
565 out:
566 switch (result) {
567 case -EINVAL:
568 return AE_BAD_PARAMETER;
569 break;
570 case -ENODEV:
571 return AE_NOT_FOUND;
572 break;
573 case -ETIME:
574 return AE_TIME;
575 break;
576 default:
577 return AE_OK;
581 /* --------------------------------------------------------------------------
582 FS Interface (/proc)
583 -------------------------------------------------------------------------- */
585 static struct proc_dir_entry *acpi_ec_dir;
587 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
589 struct acpi_ec *ec = (struct acpi_ec *)seq->private;
592 if (!ec)
593 goto end;
595 seq_printf(seq, "gpe bit: 0x%02x\n",
596 (u32) ec->gpe_bit);
597 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
598 (u32) ec->status_addr.address,
599 (u32) ec->data_addr.address);
600 seq_printf(seq, "use global lock: %s\n",
601 ec->global_lock ? "yes" : "no");
602 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
604 end:
605 return 0;
608 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
610 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
613 static struct file_operations acpi_ec_info_ops = {
614 .open = acpi_ec_info_open_fs,
615 .read = seq_read,
616 .llseek = seq_lseek,
617 .release = single_release,
618 .owner = THIS_MODULE,
621 static int acpi_ec_add_fs(struct acpi_device *device)
623 struct proc_dir_entry *entry = NULL;
626 if (!acpi_device_dir(device)) {
627 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
628 acpi_ec_dir);
629 if (!acpi_device_dir(device))
630 return -ENODEV;
633 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
634 acpi_device_dir(device));
635 if (!entry)
636 return -ENODEV;
637 else {
638 entry->proc_fops = &acpi_ec_info_ops;
639 entry->data = acpi_driver_data(device);
640 entry->owner = THIS_MODULE;
643 return 0;
646 static int acpi_ec_remove_fs(struct acpi_device *device)
649 if (acpi_device_dir(device)) {
650 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
651 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
652 acpi_device_dir(device) = NULL;
655 return 0;
658 /* --------------------------------------------------------------------------
659 Driver Interface
660 -------------------------------------------------------------------------- */
662 static int acpi_ec_add(struct acpi_device *device)
664 int result = 0;
665 acpi_status status = AE_OK;
666 struct acpi_ec *ec = NULL;
669 if (!device)
670 return -EINVAL;
672 ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
673 if (!ec)
674 return -ENOMEM;
675 memset(ec, 0, sizeof(struct acpi_ec));
677 ec->handle = device->handle;
678 ec->uid = -1;
679 init_MUTEX(&ec->sem);
680 if (acpi_ec_mode == EC_INTR) {
681 atomic_set(&ec->leaving_burst, 1);
682 init_waitqueue_head(&ec->wait);
684 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
685 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
686 acpi_driver_data(device) = ec;
688 /* Use the global lock for all EC transactions? */
689 acpi_evaluate_integer(ec->handle, "_GLK", NULL,
690 &ec->global_lock);
692 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
693 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
694 if (ec_ecdt) {
695 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
696 ACPI_ADR_SPACE_EC,
697 &acpi_ec_space_handler);
699 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
700 &acpi_ec_gpe_handler);
702 kfree(ec_ecdt);
705 /* Get GPE bit assignment (EC events). */
706 /* TODO: Add support for _GPE returning a package */
707 status =
708 acpi_evaluate_integer(ec->handle, "_GPE", NULL,
709 &ec->gpe_bit);
710 if (ACPI_FAILURE(status)) {
711 ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit assignment"));
712 result = -ENODEV;
713 goto end;
716 result = acpi_ec_add_fs(device);
717 if (result)
718 goto end;
720 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
721 acpi_device_name(device), acpi_device_bid(device),
722 (u32) ec->gpe_bit));
724 if (!first_ec)
725 first_ec = device;
727 end:
728 if (result)
729 kfree(ec);
731 return result;
734 static int acpi_ec_remove(struct acpi_device *device, int type)
736 struct acpi_ec *ec = NULL;
739 if (!device)
740 return -EINVAL;
742 ec = acpi_driver_data(device);
744 acpi_ec_remove_fs(device);
746 kfree(ec);
748 return 0;
751 static acpi_status
752 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
754 struct acpi_ec *ec = (struct acpi_ec *)context;
755 struct acpi_generic_address *addr;
757 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
758 return AE_OK;
762 * The first address region returned is the data port, and
763 * the second address region returned is the status/command
764 * port.
766 if (ec->data_addr.register_bit_width == 0) {
767 addr = &ec->data_addr;
768 } else if (ec->command_addr.register_bit_width == 0) {
769 addr = &ec->command_addr;
770 } else {
771 return AE_CTRL_TERMINATE;
774 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
775 addr->register_bit_width = 8;
776 addr->register_bit_offset = 0;
777 addr->address = resource->data.io.minimum;
779 return AE_OK;
782 static int acpi_ec_start(struct acpi_device *device)
784 acpi_status status = AE_OK;
785 struct acpi_ec *ec = NULL;
788 if (!device)
789 return -EINVAL;
791 ec = acpi_driver_data(device);
793 if (!ec)
794 return -EINVAL;
797 * Get I/O port addresses. Convert to GAS format.
799 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
800 acpi_ec_io_ports, ec);
801 if (ACPI_FAILURE(status)
802 || ec->command_addr.register_bit_width == 0) {
803 ACPI_EXCEPTION((AE_INFO, status,
804 "Error getting I/O port addresses"));
805 return -ENODEV;
808 ec->status_addr = ec->command_addr;
810 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x",
811 (u32) ec->gpe_bit,
812 (u32) ec->command_addr.address,
813 (u32) ec->data_addr.address));
816 * Install GPE handler
818 status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
819 ACPI_GPE_EDGE_TRIGGERED,
820 &acpi_ec_gpe_handler, ec);
821 if (ACPI_FAILURE(status)) {
822 return -ENODEV;
824 acpi_set_gpe_type(NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
825 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
827 status = acpi_install_address_space_handler(ec->handle,
828 ACPI_ADR_SPACE_EC,
829 &acpi_ec_space_handler,
830 &acpi_ec_space_setup, ec);
831 if (ACPI_FAILURE(status)) {
832 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
833 &acpi_ec_gpe_handler);
834 return -ENODEV;
837 return AE_OK;
840 static int acpi_ec_stop(struct acpi_device *device, int type)
842 acpi_status status = AE_OK;
843 struct acpi_ec *ec = NULL;
846 if (!device)
847 return -EINVAL;
849 ec = acpi_driver_data(device);
851 status = acpi_remove_address_space_handler(ec->handle,
852 ACPI_ADR_SPACE_EC,
853 &acpi_ec_space_handler);
854 if (ACPI_FAILURE(status))
855 return -ENODEV;
857 status =
858 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
859 &acpi_ec_gpe_handler);
860 if (ACPI_FAILURE(status))
861 return -ENODEV;
863 return 0;
866 static acpi_status __init
867 acpi_fake_ecdt_callback(acpi_handle handle,
868 u32 Level, void *context, void **retval)
870 acpi_status status;
872 init_MUTEX(&ec_ecdt->sem);
873 if (acpi_ec_mode == EC_INTR) {
874 init_waitqueue_head(&ec_ecdt->wait);
876 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
877 acpi_ec_io_ports, ec_ecdt);
878 if (ACPI_FAILURE(status))
879 return status;
880 ec_ecdt->status_addr = ec_ecdt->command_addr;
882 ec_ecdt->uid = -1;
883 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
885 status =
886 acpi_evaluate_integer(handle, "_GPE", NULL,
887 &ec_ecdt->gpe_bit);
888 if (ACPI_FAILURE(status))
889 return status;
890 ec_ecdt->global_lock = TRUE;
891 ec_ecdt->handle = handle;
893 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02x, ports=0x%2x, 0x%2x",
894 (u32) ec_ecdt->gpe_bit,
895 (u32) ec_ecdt->command_addr.address,
896 (u32) ec_ecdt->data_addr.address));
898 return AE_CTRL_TERMINATE;
902 * Some BIOS (such as some from Gateway laptops) access EC region very early
903 * such as in BAT0._INI or EC._INI before an EC device is found and
904 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
905 * required, but if EC regison is accessed early, it is required.
906 * The routine tries to workaround the BIOS bug by pre-scan EC device
907 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
908 * op region (since _REG isn't invoked yet). The assumption is true for
909 * all systems found.
911 static int __init acpi_ec_fake_ecdt(void)
913 acpi_status status;
914 int ret = 0;
916 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
918 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
919 if (!ec_ecdt) {
920 ret = -ENOMEM;
921 goto error;
923 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
925 status = acpi_get_devices(ACPI_EC_HID,
926 acpi_fake_ecdt_callback, NULL, NULL);
927 if (ACPI_FAILURE(status)) {
928 kfree(ec_ecdt);
929 ec_ecdt = NULL;
930 ret = -ENODEV;
931 ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
932 goto error;
934 return 0;
935 error:
936 return ret;
939 static int __init acpi_ec_get_real_ecdt(void)
941 acpi_status status;
942 struct acpi_table_ecdt *ecdt_ptr;
944 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
945 (struct acpi_table_header **)
946 &ecdt_ptr);
947 if (ACPI_FAILURE(status))
948 return -ENODEV;
950 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
953 * Generate a temporary ec context to use until the namespace is scanned
955 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
956 if (!ec_ecdt)
957 return -ENOMEM;
958 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
960 init_MUTEX(&ec_ecdt->sem);
961 if (acpi_ec_mode == EC_INTR) {
962 init_waitqueue_head(&ec_ecdt->wait);
964 ec_ecdt->command_addr = ecdt_ptr->ec_control;
965 ec_ecdt->status_addr = ecdt_ptr->ec_control;
966 ec_ecdt->data_addr = ecdt_ptr->ec_data;
967 ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
968 /* use the GL just to be safe */
969 ec_ecdt->global_lock = TRUE;
970 ec_ecdt->uid = ecdt_ptr->uid;
972 status =
973 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
974 if (ACPI_FAILURE(status)) {
975 goto error;
978 return 0;
979 error:
980 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
981 kfree(ec_ecdt);
982 ec_ecdt = NULL;
984 return -ENODEV;
987 static int __initdata acpi_fake_ecdt_enabled;
988 int __init acpi_ec_ecdt_probe(void)
990 acpi_status status;
991 int ret;
993 ret = acpi_ec_get_real_ecdt();
994 /* Try to make a fake ECDT */
995 if (ret && acpi_fake_ecdt_enabled) {
996 ret = acpi_ec_fake_ecdt();
999 if (ret)
1000 return 0;
1003 * Install GPE handler
1005 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
1006 ACPI_GPE_EDGE_TRIGGERED,
1007 &acpi_ec_gpe_handler, ec_ecdt);
1008 if (ACPI_FAILURE(status)) {
1009 goto error;
1011 acpi_set_gpe_type(NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
1012 acpi_enable_gpe(NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
1014 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
1015 ACPI_ADR_SPACE_EC,
1016 &acpi_ec_space_handler,
1017 &acpi_ec_space_setup,
1018 ec_ecdt);
1019 if (ACPI_FAILURE(status)) {
1020 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
1021 &acpi_ec_gpe_handler);
1022 goto error;
1025 return 0;
1027 error:
1028 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
1029 kfree(ec_ecdt);
1030 ec_ecdt = NULL;
1032 return -ENODEV;
1035 static int __init acpi_ec_init(void)
1037 int result = 0;
1040 if (acpi_disabled)
1041 return 0;
1043 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1044 if (!acpi_ec_dir)
1045 return -ENODEV;
1047 /* Now register the driver for the EC */
1048 result = acpi_bus_register_driver(&acpi_ec_driver);
1049 if (result < 0) {
1050 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1051 return -ENODEV;
1054 return result;
1057 subsys_initcall(acpi_ec_init);
1059 /* EC driver currently not unloadable */
1060 #if 0
1061 static void __exit acpi_ec_exit(void)
1064 acpi_bus_unregister_driver(&acpi_ec_driver);
1066 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1068 return;
1070 #endif /* 0 */
1072 static int __init acpi_fake_ecdt_setup(char *str)
1074 acpi_fake_ecdt_enabled = 1;
1075 return 1;
1078 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1079 static int __init acpi_ec_set_intr_mode(char *str)
1081 int intr;
1083 if (!get_option(&str, &intr))
1084 return 0;
1086 if (intr) {
1087 acpi_ec_mode = EC_INTR;
1088 } else {
1089 acpi_ec_mode = EC_POLL;
1091 acpi_ec_driver.ops.add = acpi_ec_add;
1092 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n", intr ? "interrupt" : "polling"));
1094 return 1;
1097 __setup("ec_intr=", acpi_ec_set_intr_mode);