ACPI: ec: Remove calls to clear_gpe() and enable_gpe(), as these are handled at
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / ec.c
blobacfe8830da147c54010323e1945b0f823cb2fb2b
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 #undef PREFIX
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 */
58 /* EC commands */
59 #define ACPI_EC_COMMAND_READ 0x80
60 #define ACPI_EC_COMMAND_WRITE 0x81
61 #define ACPI_EC_BURST_ENABLE 0x82
62 #define ACPI_EC_BURST_DISABLE 0x83
63 #define ACPI_EC_COMMAND_QUERY 0x84
65 /* EC events */
66 enum {
67 ACPI_EC_EVENT_OBF_1 = 1, /* Output buffer full */
68 ACPI_EC_EVENT_IBF_0, /* Input buffer empty */
71 #define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
72 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
73 #define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
74 #define ACPI_EC_UDELAY_COUNT 1000 /* Wait 100ms max. during EC ops */
76 enum {
77 EC_INTR = 1, /* Output buffer full */
78 EC_POLL, /* Input buffer empty */
81 static int acpi_ec_remove(struct acpi_device *device, int type);
82 static int acpi_ec_start(struct acpi_device *device);
83 static int acpi_ec_stop(struct acpi_device *device, int type);
84 static int acpi_ec_add(struct acpi_device *device);
86 static struct acpi_driver acpi_ec_driver = {
87 .name = ACPI_EC_DRIVER_NAME,
88 .class = ACPI_EC_CLASS,
89 .ids = ACPI_EC_HID,
90 .ops = {
91 .add = acpi_ec_add,
92 .remove = acpi_ec_remove,
93 .start = acpi_ec_start,
94 .stop = acpi_ec_stop,
98 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
99 struct acpi_ec {
100 acpi_handle handle;
101 unsigned long uid;
102 unsigned long gpe_bit;
103 unsigned long command_addr;
104 unsigned long data_addr;
105 unsigned long global_lock;
106 struct semaphore sem;
107 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
108 wait_queue_head_t wait;
109 } *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 inline u8 acpi_ec_read_status(struct acpi_ec *ec)
121 return inb(ec->command_addr);
124 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
126 return inb(ec->data_addr);
129 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
131 outb(command, ec->command_addr);
134 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
136 outb(data, ec->data_addr);
139 static int acpi_ec_check_status(struct acpi_ec *ec, u8 event)
141 u8 status = acpi_ec_read_status(ec);
142 switch (event) {
143 case ACPI_EC_EVENT_OBF_1:
144 if (status & ACPI_EC_FLAG_OBF)
145 return 1;
146 break;
147 case ACPI_EC_EVENT_IBF_0:
148 if (!(status & ACPI_EC_FLAG_IBF))
149 return 1;
150 break;
151 default:
152 break;
155 return 0;
158 static int acpi_ec_wait(struct acpi_ec *ec, u8 event)
160 if (acpi_ec_mode == EC_POLL) {
161 int i;
162 for (i = 0; i < ACPI_EC_UDELAY_COUNT; ++i) {
163 if (acpi_ec_check_status(ec, event))
164 return 0;
165 udelay(ACPI_EC_UDELAY);
167 } else {
168 if (wait_event_timeout(ec->wait,
169 acpi_ec_check_status(ec, event),
170 msecs_to_jiffies(ACPI_EC_DELAY)) ||
171 acpi_ec_check_status(ec, event)) {
172 return 0;
173 } else {
174 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
175 " status = %d, expect_event = %d\n",
176 acpi_ec_read_status(ec), event);
180 return -ETIME;
183 #ifdef ACPI_FUTURE_USAGE
185 * Note: samsung nv5000 doesn't work with ec burst mode.
186 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
188 int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
190 u8 tmp = 0;
191 u8 status = 0;
194 status = acpi_ec_read_status(ec);
195 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
196 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
197 if (status)
198 goto end;
199 acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
200 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
201 tmp = acpi_ec_read_data(ec);
202 if (tmp != 0x90) { /* Burst ACK byte */
203 return -EINVAL;
207 atomic_set(&ec->leaving_burst, 0);
208 return 0;
209 end:
210 ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
211 return -1;
214 int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
216 u8 status = 0;
219 status = acpi_ec_read_status(ec);
220 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
221 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
222 if(status)
223 goto end;
224 acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
225 acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
227 atomic_set(&ec->leaving_burst, 1);
228 return 0;
229 end:
230 ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
231 return -1;
233 #endif /* ACPI_FUTURE_USAGE */
235 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
236 const u8 *wdata, unsigned wdata_len,
237 u8 *rdata, unsigned rdata_len)
239 int result = 0;
241 acpi_ec_write_cmd(ec, command);
243 for (; wdata_len > 0; wdata_len --) {
244 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
245 if (result) {
246 printk(KERN_ERR PREFIX "write_cmd timeout, command = %d\n",
247 command);
248 goto end;
250 acpi_ec_write_data(ec, *(wdata++));
253 if (!rdata_len) {
254 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
255 if (result) {
256 printk(KERN_ERR PREFIX "finish-write timeout, command = %d\n",
257 command);
258 goto end;
262 for (; rdata_len > 0; rdata_len --) {
263 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
264 if (result) {
265 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
266 command);
267 goto end;
270 *(rdata++) = acpi_ec_read_data(ec);
272 end:
273 return result;
276 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
277 const u8 *wdata, unsigned wdata_len,
278 u8 *rdata, unsigned rdata_len)
280 int status;
281 u32 glk;
283 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
284 return -EINVAL;
286 if (rdata)
287 memset(rdata, 0, rdata_len);
289 if (ec->global_lock) {
290 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
291 if (ACPI_FAILURE(status))
292 return -ENODEV;
294 down(&ec->sem);
296 /* Make sure GPE is enabled before doing transaction */
297 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
299 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
300 if (status) {
301 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
302 goto end;
305 status = acpi_ec_transaction_unlocked(ec, command,
306 wdata, wdata_len,
307 rdata, rdata_len);
309 end:
310 up(&ec->sem);
312 if (ec->global_lock)
313 acpi_release_global_lock(glk);
315 return status;
318 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
320 int result;
321 u8 d;
323 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
324 &address, 1, &d, 1);
325 *data = d;
326 return result;
329 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
331 u8 wdata[2] = { address, data };
332 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
333 wdata, 2, NULL, 0);
337 * Externally callable EC access functions. For now, assume 1 EC only
339 int ec_read(u8 addr, u8 *val)
341 struct acpi_ec *ec;
342 int err;
343 u8 temp_data;
345 if (!first_ec)
346 return -ENODEV;
348 ec = acpi_driver_data(first_ec);
350 err = acpi_ec_read(ec, addr, &temp_data);
352 if (!err) {
353 *val = temp_data;
354 return 0;
355 } else
356 return err;
359 EXPORT_SYMBOL(ec_read);
361 int ec_write(u8 addr, u8 val)
363 struct acpi_ec *ec;
364 int err;
366 if (!first_ec)
367 return -ENODEV;
369 ec = acpi_driver_data(first_ec);
371 err = acpi_ec_write(ec, addr, val);
373 return err;
376 EXPORT_SYMBOL(ec_write);
378 extern int ec_transaction(u8 command,
379 const u8 *wdata, unsigned wdata_len,
380 u8 *rdata, unsigned rdata_len)
382 struct acpi_ec *ec;
384 if (!first_ec)
385 return -ENODEV;
387 ec = acpi_driver_data(first_ec);
389 return acpi_ec_transaction(ec, command, wdata,
390 wdata_len, rdata, rdata_len);
393 EXPORT_SYMBOL(ec_transaction);
395 static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
397 int result;
398 u8 d;
400 if (!ec || !data)
401 return -EINVAL;
404 * Query the EC to find out which _Qxx method we need to evaluate.
405 * Note that successful completion of the query causes the ACPI_EC_SCI
406 * bit to be cleared (and thus clearing the interrupt source).
409 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
410 if (result)
411 return result;
413 if (!d)
414 return -ENODATA;
416 *data = d;
417 return 0;
420 /* --------------------------------------------------------------------------
421 Event Management
422 -------------------------------------------------------------------------- */
424 static void acpi_ec_gpe_query(void *ec_cxt)
426 struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
427 u8 value = 0;
428 static char object_name[8];
430 if (!ec)
431 return;
433 value = acpi_ec_read_status(ec);
435 if (!(value & ACPI_EC_FLAG_SCI))
436 return;
438 if (acpi_ec_query(ec, &value))
439 return;
441 snprintf(object_name, 8, "_Q%2.2X", value);
443 printk(KERN_INFO PREFIX "evaluating %s\n", object_name);
445 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
448 static u32 acpi_ec_gpe_handler(void *data)
450 acpi_status status = AE_OK;
451 u8 value;
452 struct acpi_ec *ec = (struct acpi_ec *)data;
455 if (acpi_ec_mode == EC_INTR) {
456 wake_up(&ec->wait);
459 value = acpi_ec_read_status(ec);
460 if (value & ACPI_EC_FLAG_SCI) {
461 status = acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
464 return status == AE_OK ?
465 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
468 /* --------------------------------------------------------------------------
469 Address Space Management
470 -------------------------------------------------------------------------- */
472 static acpi_status
473 acpi_ec_space_setup(acpi_handle region_handle,
474 u32 function, void *handler_context, void **return_context)
477 * The EC object is in the handler context and is needed
478 * when calling the acpi_ec_space_handler.
480 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
481 handler_context : NULL;
483 return AE_OK;
486 static acpi_status
487 acpi_ec_space_handler(u32 function,
488 acpi_physical_address address,
489 u32 bit_width,
490 acpi_integer * value,
491 void *handler_context, void *region_context)
493 int result = 0;
494 struct acpi_ec *ec = NULL;
495 u64 temp = *value;
496 acpi_integer f_v = 0;
497 int i = 0;
500 if ((address > 0xFF) || !value || !handler_context)
501 return AE_BAD_PARAMETER;
503 if (bit_width != 8 && acpi_strict) {
504 return AE_BAD_PARAMETER;
507 ec = (struct acpi_ec *)handler_context;
509 next_byte:
510 switch (function) {
511 case ACPI_READ:
512 temp = 0;
513 result = acpi_ec_read(ec, (u8) address, (u8 *) &temp);
514 break;
515 case ACPI_WRITE:
516 result = acpi_ec_write(ec, (u8) address, (u8) temp);
517 break;
518 default:
519 result = -EINVAL;
520 goto out;
521 break;
524 bit_width -= 8;
525 if (bit_width) {
526 if (function == ACPI_READ)
527 f_v |= temp << 8 * i;
528 if (function == ACPI_WRITE)
529 temp >>= 8;
530 i++;
531 address++;
532 goto next_byte;
535 if (function == ACPI_READ) {
536 f_v |= temp << 8 * i;
537 *value = f_v;
540 out:
541 switch (result) {
542 case -EINVAL:
543 return AE_BAD_PARAMETER;
544 break;
545 case -ENODEV:
546 return AE_NOT_FOUND;
547 break;
548 case -ETIME:
549 return AE_TIME;
550 break;
551 default:
552 return AE_OK;
556 /* --------------------------------------------------------------------------
557 FS Interface (/proc)
558 -------------------------------------------------------------------------- */
560 static struct proc_dir_entry *acpi_ec_dir;
562 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
564 struct acpi_ec *ec = (struct acpi_ec *)seq->private;
567 if (!ec)
568 goto end;
570 seq_printf(seq, "gpe bit: 0x%02x\n",
571 (u32) ec->gpe_bit);
572 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
573 (u32) ec->command_addr,
574 (u32) ec->data_addr);
575 seq_printf(seq, "use global lock: %s\n",
576 ec->global_lock ? "yes" : "no");
577 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
579 end:
580 return 0;
583 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
585 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
588 static struct file_operations acpi_ec_info_ops = {
589 .open = acpi_ec_info_open_fs,
590 .read = seq_read,
591 .llseek = seq_lseek,
592 .release = single_release,
593 .owner = THIS_MODULE,
596 static int acpi_ec_add_fs(struct acpi_device *device)
598 struct proc_dir_entry *entry = NULL;
601 if (!acpi_device_dir(device)) {
602 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
603 acpi_ec_dir);
604 if (!acpi_device_dir(device))
605 return -ENODEV;
608 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
609 acpi_device_dir(device));
610 if (!entry)
611 return -ENODEV;
612 else {
613 entry->proc_fops = &acpi_ec_info_ops;
614 entry->data = acpi_driver_data(device);
615 entry->owner = THIS_MODULE;
618 return 0;
621 static int acpi_ec_remove_fs(struct acpi_device *device)
624 if (acpi_device_dir(device)) {
625 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
626 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
627 acpi_device_dir(device) = NULL;
630 return 0;
633 /* --------------------------------------------------------------------------
634 Driver Interface
635 -------------------------------------------------------------------------- */
637 static int acpi_ec_add(struct acpi_device *device)
639 int result = 0;
640 acpi_status status = AE_OK;
641 struct acpi_ec *ec = NULL;
644 if (!device)
645 return -EINVAL;
647 ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
648 if (!ec)
649 return -ENOMEM;
650 memset(ec, 0, sizeof(struct acpi_ec));
652 ec->handle = device->handle;
653 ec->uid = -1;
654 init_MUTEX(&ec->sem);
655 if (acpi_ec_mode == EC_INTR) {
656 atomic_set(&ec->leaving_burst, 1);
657 init_waitqueue_head(&ec->wait);
659 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
660 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
661 acpi_driver_data(device) = ec;
663 /* Use the global lock for all EC transactions? */
664 acpi_evaluate_integer(ec->handle, "_GLK", NULL,
665 &ec->global_lock);
667 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
668 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
669 if (ec_ecdt) {
670 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
671 ACPI_ADR_SPACE_EC,
672 &acpi_ec_space_handler);
674 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
675 &acpi_ec_gpe_handler);
677 kfree(ec_ecdt);
680 /* Get GPE bit assignment (EC events). */
681 /* TODO: Add support for _GPE returning a package */
682 status =
683 acpi_evaluate_integer(ec->handle, "_GPE", NULL,
684 &ec->gpe_bit);
685 if (ACPI_FAILURE(status)) {
686 ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit assignment"));
687 result = -ENODEV;
688 goto end;
691 result = acpi_ec_add_fs(device);
692 if (result)
693 goto end;
695 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
696 acpi_device_name(device), acpi_device_bid(device),
697 (u32) ec->gpe_bit));
699 if (!first_ec)
700 first_ec = device;
702 end:
703 if (result)
704 kfree(ec);
706 return result;
709 static int acpi_ec_remove(struct acpi_device *device, int type)
711 struct acpi_ec *ec = NULL;
714 if (!device)
715 return -EINVAL;
717 ec = acpi_driver_data(device);
719 acpi_ec_remove_fs(device);
721 kfree(ec);
723 return 0;
726 static acpi_status
727 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
729 struct acpi_ec *ec = (struct acpi_ec *)context;
731 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
732 return AE_OK;
736 * The first address region returned is the data port, and
737 * the second address region returned is the status/command
738 * port.
740 if (ec->data_addr == 0) {
741 ec->data_addr = resource->data.io.minimum;
742 } else if (ec->command_addr == 0) {
743 ec->command_addr = resource->data.io.minimum;
744 } else {
745 return AE_CTRL_TERMINATE;
748 return AE_OK;
751 static int acpi_ec_start(struct acpi_device *device)
753 acpi_status status = AE_OK;
754 struct acpi_ec *ec = NULL;
757 if (!device)
758 return -EINVAL;
760 ec = acpi_driver_data(device);
762 if (!ec)
763 return -EINVAL;
766 * Get I/O port addresses. Convert to GAS format.
768 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
769 acpi_ec_io_ports, ec);
770 if (ACPI_FAILURE(status) || ec->command_addr == 0) {
771 ACPI_EXCEPTION((AE_INFO, status,
772 "Error getting I/O port addresses"));
773 return -ENODEV;
776 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
777 ec->gpe_bit, ec->command_addr, ec->data_addr));
780 * Install GPE handler
782 status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
783 ACPI_GPE_EDGE_TRIGGERED,
784 &acpi_ec_gpe_handler, ec);
785 if (ACPI_FAILURE(status)) {
786 return -ENODEV;
788 acpi_set_gpe_type(NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
789 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
791 status = acpi_install_address_space_handler(ec->handle,
792 ACPI_ADR_SPACE_EC,
793 &acpi_ec_space_handler,
794 &acpi_ec_space_setup, ec);
795 if (ACPI_FAILURE(status)) {
796 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
797 &acpi_ec_gpe_handler);
798 return -ENODEV;
801 return AE_OK;
804 static int acpi_ec_stop(struct acpi_device *device, int type)
806 acpi_status status = AE_OK;
807 struct acpi_ec *ec = NULL;
810 if (!device)
811 return -EINVAL;
813 ec = acpi_driver_data(device);
815 status = acpi_remove_address_space_handler(ec->handle,
816 ACPI_ADR_SPACE_EC,
817 &acpi_ec_space_handler);
818 if (ACPI_FAILURE(status))
819 return -ENODEV;
821 status =
822 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
823 &acpi_ec_gpe_handler);
824 if (ACPI_FAILURE(status))
825 return -ENODEV;
827 return 0;
830 static acpi_status __init
831 acpi_fake_ecdt_callback(acpi_handle handle,
832 u32 Level, void *context, void **retval)
834 acpi_status status;
836 init_MUTEX(&ec_ecdt->sem);
837 if (acpi_ec_mode == EC_INTR) {
838 init_waitqueue_head(&ec_ecdt->wait);
840 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
841 acpi_ec_io_ports, ec_ecdt);
842 if (ACPI_FAILURE(status))
843 return status;
845 ec_ecdt->uid = -1;
846 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
848 status =
849 acpi_evaluate_integer(handle, "_GPE", NULL,
850 &ec_ecdt->gpe_bit);
851 if (ACPI_FAILURE(status))
852 return status;
853 ec_ecdt->global_lock = TRUE;
854 ec_ecdt->handle = handle;
856 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
857 ec_ecdt->gpe_bit, ec_ecdt->command_addr, ec_ecdt->data_addr));
859 return AE_CTRL_TERMINATE;
863 * Some BIOS (such as some from Gateway laptops) access EC region very early
864 * such as in BAT0._INI or EC._INI before an EC device is found and
865 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
866 * required, but if EC regison is accessed early, it is required.
867 * The routine tries to workaround the BIOS bug by pre-scan EC device
868 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
869 * op region (since _REG isn't invoked yet). The assumption is true for
870 * all systems found.
872 static int __init acpi_ec_fake_ecdt(void)
874 acpi_status status;
875 int ret = 0;
877 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
879 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
880 if (!ec_ecdt) {
881 ret = -ENOMEM;
882 goto error;
884 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
886 status = acpi_get_devices(ACPI_EC_HID,
887 acpi_fake_ecdt_callback, NULL, NULL);
888 if (ACPI_FAILURE(status)) {
889 kfree(ec_ecdt);
890 ec_ecdt = NULL;
891 ret = -ENODEV;
892 ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
893 goto error;
895 return 0;
896 error:
897 return ret;
900 static int __init acpi_ec_get_real_ecdt(void)
902 acpi_status status;
903 struct acpi_table_ecdt *ecdt_ptr;
905 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
906 (struct acpi_table_header **)
907 &ecdt_ptr);
908 if (ACPI_FAILURE(status))
909 return -ENODEV;
911 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
914 * Generate a temporary ec context to use until the namespace is scanned
916 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
917 if (!ec_ecdt)
918 return -ENOMEM;
919 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
921 init_MUTEX(&ec_ecdt->sem);
922 if (acpi_ec_mode == EC_INTR) {
923 init_waitqueue_head(&ec_ecdt->wait);
925 ec_ecdt->command_addr = ecdt_ptr->ec_control.address;
926 ec_ecdt->data_addr = ecdt_ptr->ec_data.address;
927 ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
928 /* use the GL just to be safe */
929 ec_ecdt->global_lock = TRUE;
930 ec_ecdt->uid = ecdt_ptr->uid;
932 status =
933 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
934 if (ACPI_FAILURE(status)) {
935 goto error;
938 return 0;
939 error:
940 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
941 kfree(ec_ecdt);
942 ec_ecdt = NULL;
944 return -ENODEV;
947 static int __initdata acpi_fake_ecdt_enabled;
948 int __init acpi_ec_ecdt_probe(void)
950 acpi_status status;
951 int ret;
953 ret = acpi_ec_get_real_ecdt();
954 /* Try to make a fake ECDT */
955 if (ret && acpi_fake_ecdt_enabled) {
956 ret = acpi_ec_fake_ecdt();
959 if (ret)
960 return 0;
963 * Install GPE handler
965 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
966 ACPI_GPE_EDGE_TRIGGERED,
967 &acpi_ec_gpe_handler, ec_ecdt);
968 if (ACPI_FAILURE(status)) {
969 goto error;
971 acpi_set_gpe_type(NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
972 acpi_enable_gpe(NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
974 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
975 ACPI_ADR_SPACE_EC,
976 &acpi_ec_space_handler,
977 &acpi_ec_space_setup,
978 ec_ecdt);
979 if (ACPI_FAILURE(status)) {
980 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
981 &acpi_ec_gpe_handler);
982 goto error;
985 return 0;
987 error:
988 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
989 kfree(ec_ecdt);
990 ec_ecdt = NULL;
992 return -ENODEV;
995 static int __init acpi_ec_init(void)
997 int result = 0;
1000 if (acpi_disabled)
1001 return 0;
1003 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1004 if (!acpi_ec_dir)
1005 return -ENODEV;
1007 /* Now register the driver for the EC */
1008 result = acpi_bus_register_driver(&acpi_ec_driver);
1009 if (result < 0) {
1010 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1011 return -ENODEV;
1014 return result;
1017 subsys_initcall(acpi_ec_init);
1019 /* EC driver currently not unloadable */
1020 #if 0
1021 static void __exit acpi_ec_exit(void)
1024 acpi_bus_unregister_driver(&acpi_ec_driver);
1026 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1028 return;
1030 #endif /* 0 */
1032 static int __init acpi_fake_ecdt_setup(char *str)
1034 acpi_fake_ecdt_enabled = 1;
1035 return 1;
1038 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1039 static int __init acpi_ec_set_intr_mode(char *str)
1041 int intr;
1043 if (!get_option(&str, &intr))
1044 return 0;
1046 if (intr) {
1047 acpi_ec_mode = EC_INTR;
1048 } else {
1049 acpi_ec_mode = EC_POLL;
1051 acpi_ec_driver.ops.add = acpi_ec_add;
1052 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n", intr ? "interrupt" : "polling"));
1054 return 1;
1057 __setup("ec_intr=", acpi_ec_set_intr_mode);