[PATCH] ext4 balloc: fix _with_rsv freeze
[linux-2.6.git] / drivers / acpi / ec.c
blobe6d4b084dca2eeb40213074998ccdfde52278260
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,
95 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
96 struct acpi_ec {
97 acpi_handle handle;
98 unsigned long uid;
99 unsigned long gpe_bit;
100 unsigned long command_addr;
101 unsigned long data_addr;
102 unsigned long global_lock;
103 struct semaphore sem;
104 unsigned int expect_event;
105 atomic_t leaving_burst; /* 0 : No, 1 : Yes, 2: abort */
106 wait_queue_head_t wait;
107 } *ec_ecdt;
109 /* External interfaces use first EC only, so remember */
110 static struct acpi_device *first_ec;
111 static int acpi_ec_mode = EC_INTR;
113 /* --------------------------------------------------------------------------
114 Transaction Management
115 -------------------------------------------------------------------------- */
117 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
119 return inb(ec->command_addr);
122 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
124 return inb(ec->data_addr);
127 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
129 outb(command, ec->command_addr);
132 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
134 outb(data, ec->data_addr);
137 static int acpi_ec_check_status(u8 status, u8 event)
139 switch (event) {
140 case ACPI_EC_EVENT_OBF_1:
141 if (status & ACPI_EC_FLAG_OBF)
142 return 1;
143 break;
144 case ACPI_EC_EVENT_IBF_0:
145 if (!(status & ACPI_EC_FLAG_IBF))
146 return 1;
147 break;
148 default:
149 break;
152 return 0;
155 static int acpi_ec_wait(struct acpi_ec *ec, u8 event)
157 int i = (acpi_ec_mode == EC_POLL) ? ACPI_EC_UDELAY_COUNT : 0;
158 long time_left;
160 ec->expect_event = event;
161 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
162 ec->expect_event = 0;
163 return 0;
166 do {
167 if (acpi_ec_mode == EC_POLL) {
168 udelay(ACPI_EC_UDELAY);
169 } else {
170 time_left = wait_event_timeout(ec->wait,
171 !ec->expect_event,
172 msecs_to_jiffies(ACPI_EC_DELAY));
173 if (time_left > 0) {
174 ec->expect_event = 0;
175 return 0;
178 if (acpi_ec_check_status(acpi_ec_read_status(ec), event)) {
179 ec->expect_event = 0;
180 return 0;
182 } while (--i > 0);
184 ec->expect_event = 0;
186 return -ETIME;
189 #ifdef ACPI_FUTURE_USAGE
191 * Note: samsung nv5000 doesn't work with ec burst mode.
192 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
194 int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
196 u8 tmp = 0;
197 u8 status = 0;
200 status = acpi_ec_read_status(ec);
201 if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
202 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
203 if (status)
204 goto end;
205 acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
206 status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
207 tmp = acpi_ec_read_data(ec);
208 if (tmp != 0x90) { /* Burst ACK byte */
209 return -EINVAL;
213 atomic_set(&ec->leaving_burst, 0);
214 return 0;
215 end:
216 ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
217 return -1;
220 int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
222 u8 status = 0;
225 status = acpi_ec_read_status(ec);
226 if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)){
227 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
228 if(status)
229 goto end;
230 acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
231 acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
233 atomic_set(&ec->leaving_burst, 1);
234 return 0;
235 end:
236 ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
237 return -1;
239 #endif /* ACPI_FUTURE_USAGE */
241 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
242 const u8 *wdata, unsigned wdata_len,
243 u8 *rdata, unsigned rdata_len)
245 int result;
247 acpi_ec_write_cmd(ec, command);
249 for (; wdata_len > 0; wdata_len --) {
250 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
251 if (result)
252 return result;
253 acpi_ec_write_data(ec, *(wdata++));
256 if (command == ACPI_EC_COMMAND_WRITE) {
257 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
258 if (result)
259 return result;
262 for (; rdata_len > 0; rdata_len --) {
263 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
264 if (result)
265 return result;
267 *(rdata++) = acpi_ec_read_data(ec);
270 return 0;
273 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
274 const u8 *wdata, unsigned wdata_len,
275 u8 *rdata, unsigned rdata_len)
277 int status;
278 u32 glk;
280 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
281 return -EINVAL;
283 if (rdata)
284 memset(rdata, 0, rdata_len);
286 if (ec->global_lock) {
287 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
288 if (ACPI_FAILURE(status))
289 return -ENODEV;
291 down(&ec->sem);
293 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
294 if (status) {
295 printk(KERN_DEBUG PREFIX "read EC, IB not empty\n");
296 goto end;
299 status = acpi_ec_transaction_unlocked(ec, command,
300 wdata, wdata_len,
301 rdata, rdata_len);
303 end:
304 up(&ec->sem);
306 if (ec->global_lock)
307 acpi_release_global_lock(glk);
309 return status;
312 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 *data)
314 int result;
315 u8 d;
317 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
318 &address, 1, &d, 1);
319 *data = d;
320 return result;
323 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
325 u8 wdata[2] = { address, data };
326 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
327 wdata, 2, NULL, 0);
331 * Externally callable EC access functions. For now, assume 1 EC only
333 int ec_read(u8 addr, u8 *val)
335 struct acpi_ec *ec;
336 int err;
337 u8 temp_data;
339 if (!first_ec)
340 return -ENODEV;
342 ec = acpi_driver_data(first_ec);
344 err = acpi_ec_read(ec, addr, &temp_data);
346 if (!err) {
347 *val = temp_data;
348 return 0;
349 } else
350 return err;
353 EXPORT_SYMBOL(ec_read);
355 int ec_write(u8 addr, u8 val)
357 struct acpi_ec *ec;
358 int err;
360 if (!first_ec)
361 return -ENODEV;
363 ec = acpi_driver_data(first_ec);
365 err = acpi_ec_write(ec, addr, val);
367 return err;
370 EXPORT_SYMBOL(ec_write);
372 extern int ec_transaction(u8 command,
373 const u8 *wdata, unsigned wdata_len,
374 u8 *rdata, unsigned rdata_len)
376 struct acpi_ec *ec;
378 if (!first_ec)
379 return -ENODEV;
381 ec = acpi_driver_data(first_ec);
383 return acpi_ec_transaction(ec, command, wdata,
384 wdata_len, rdata, rdata_len);
387 EXPORT_SYMBOL(ec_transaction);
389 static int acpi_ec_query(struct acpi_ec *ec, u8 *data)
391 int result;
392 u8 d;
394 if (!ec || !data)
395 return -EINVAL;
398 * Query the EC to find out which _Qxx method we need to evaluate.
399 * Note that successful completion of the query causes the ACPI_EC_SCI
400 * bit to be cleared (and thus clearing the interrupt source).
403 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
404 if (result)
405 return result;
407 if (!d)
408 return -ENODATA;
410 *data = d;
411 return 0;
414 /* --------------------------------------------------------------------------
415 Event Management
416 -------------------------------------------------------------------------- */
418 struct acpi_ec_query_data {
419 acpi_handle handle;
420 u8 data;
423 static void acpi_ec_gpe_query(void *ec_cxt)
425 struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
426 u8 value = 0;
427 static char object_name[8];
429 if (!ec)
430 goto end;
432 value = acpi_ec_read_status(ec);
434 if (!(value & ACPI_EC_FLAG_SCI))
435 goto end;
437 if (acpi_ec_query(ec, &value))
438 goto end;
440 snprintf(object_name, 8, "_Q%2.2X", value);
442 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
444 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
446 end:
447 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
450 static u32 acpi_ec_gpe_handler(void *data)
452 acpi_status status = AE_OK;
453 u8 value;
454 struct acpi_ec *ec = (struct acpi_ec *)data;
456 acpi_clear_gpe(NULL, ec->gpe_bit, ACPI_ISR);
457 value = acpi_ec_read_status(ec);
459 if (acpi_ec_mode == EC_INTR) {
460 if (acpi_ec_check_status(value, ec->expect_event)) {
461 ec->expect_event = 0;
462 wake_up(&ec->wait);
466 if (value & ACPI_EC_FLAG_SCI) {
467 status = acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
468 return status == AE_OK ?
469 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
471 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
472 return status == AE_OK ?
473 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
476 /* --------------------------------------------------------------------------
477 Address Space Management
478 -------------------------------------------------------------------------- */
480 static acpi_status
481 acpi_ec_space_setup(acpi_handle region_handle,
482 u32 function, void *handler_context, void **return_context)
485 * The EC object is in the handler context and is needed
486 * when calling the acpi_ec_space_handler.
488 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
489 handler_context : NULL;
491 return AE_OK;
494 static acpi_status
495 acpi_ec_space_handler(u32 function,
496 acpi_physical_address address,
497 u32 bit_width,
498 acpi_integer * value,
499 void *handler_context, void *region_context)
501 int result = 0;
502 struct acpi_ec *ec = NULL;
503 u64 temp = *value;
504 acpi_integer f_v = 0;
505 int i = 0;
508 if ((address > 0xFF) || !value || !handler_context)
509 return AE_BAD_PARAMETER;
511 if (bit_width != 8 && acpi_strict) {
512 return AE_BAD_PARAMETER;
515 ec = (struct acpi_ec *)handler_context;
517 next_byte:
518 switch (function) {
519 case ACPI_READ:
520 temp = 0;
521 result = acpi_ec_read(ec, (u8) address, (u8 *) &temp);
522 break;
523 case ACPI_WRITE:
524 result = acpi_ec_write(ec, (u8) address, (u8) temp);
525 break;
526 default:
527 result = -EINVAL;
528 goto out;
529 break;
532 bit_width -= 8;
533 if (bit_width) {
534 if (function == ACPI_READ)
535 f_v |= temp << 8 * i;
536 if (function == ACPI_WRITE)
537 temp >>= 8;
538 i++;
539 address++;
540 goto next_byte;
543 if (function == ACPI_READ) {
544 f_v |= temp << 8 * i;
545 *value = f_v;
548 out:
549 switch (result) {
550 case -EINVAL:
551 return AE_BAD_PARAMETER;
552 break;
553 case -ENODEV:
554 return AE_NOT_FOUND;
555 break;
556 case -ETIME:
557 return AE_TIME;
558 break;
559 default:
560 return AE_OK;
564 /* --------------------------------------------------------------------------
565 FS Interface (/proc)
566 -------------------------------------------------------------------------- */
568 static struct proc_dir_entry *acpi_ec_dir;
570 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
572 struct acpi_ec *ec = (struct acpi_ec *)seq->private;
575 if (!ec)
576 goto end;
578 seq_printf(seq, "gpe bit: 0x%02x\n",
579 (u32) ec->gpe_bit);
580 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
581 (u32) ec->command_addr,
582 (u32) ec->data_addr);
583 seq_printf(seq, "use global lock: %s\n",
584 ec->global_lock ? "yes" : "no");
585 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
587 end:
588 return 0;
591 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
593 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
596 static struct file_operations acpi_ec_info_ops = {
597 .open = acpi_ec_info_open_fs,
598 .read = seq_read,
599 .llseek = seq_lseek,
600 .release = single_release,
601 .owner = THIS_MODULE,
604 static int acpi_ec_add_fs(struct acpi_device *device)
606 struct proc_dir_entry *entry = NULL;
609 if (!acpi_device_dir(device)) {
610 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
611 acpi_ec_dir);
612 if (!acpi_device_dir(device))
613 return -ENODEV;
616 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
617 acpi_device_dir(device));
618 if (!entry)
619 return -ENODEV;
620 else {
621 entry->proc_fops = &acpi_ec_info_ops;
622 entry->data = acpi_driver_data(device);
623 entry->owner = THIS_MODULE;
626 return 0;
629 static int acpi_ec_remove_fs(struct acpi_device *device)
632 if (acpi_device_dir(device)) {
633 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
634 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
635 acpi_device_dir(device) = NULL;
638 return 0;
641 /* --------------------------------------------------------------------------
642 Driver Interface
643 -------------------------------------------------------------------------- */
645 static int acpi_ec_add(struct acpi_device *device)
647 int result = 0;
648 acpi_status status = AE_OK;
649 struct acpi_ec *ec = NULL;
652 if (!device)
653 return -EINVAL;
655 ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
656 if (!ec)
657 return -ENOMEM;
658 memset(ec, 0, sizeof(struct acpi_ec));
660 ec->handle = device->handle;
661 ec->uid = -1;
662 init_MUTEX(&ec->sem);
663 if (acpi_ec_mode == EC_INTR) {
664 atomic_set(&ec->leaving_burst, 1);
665 init_waitqueue_head(&ec->wait);
667 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
668 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
669 acpi_driver_data(device) = ec;
671 /* Use the global lock for all EC transactions? */
672 acpi_evaluate_integer(ec->handle, "_GLK", NULL,
673 &ec->global_lock);
675 /* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
676 http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
677 if (ec_ecdt) {
678 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
679 ACPI_ADR_SPACE_EC,
680 &acpi_ec_space_handler);
682 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
683 &acpi_ec_gpe_handler);
685 kfree(ec_ecdt);
688 /* Get GPE bit assignment (EC events). */
689 /* TODO: Add support for _GPE returning a package */
690 status =
691 acpi_evaluate_integer(ec->handle, "_GPE", NULL,
692 &ec->gpe_bit);
693 if (ACPI_FAILURE(status)) {
694 ACPI_EXCEPTION((AE_INFO, status, "Obtaining GPE bit assignment"));
695 result = -ENODEV;
696 goto end;
699 result = acpi_ec_add_fs(device);
700 if (result)
701 goto end;
703 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
704 acpi_device_name(device), acpi_device_bid(device),
705 (u32) ec->gpe_bit));
707 if (!first_ec)
708 first_ec = device;
710 end:
711 if (result)
712 kfree(ec);
714 return result;
717 static int acpi_ec_remove(struct acpi_device *device, int type)
719 struct acpi_ec *ec = NULL;
722 if (!device)
723 return -EINVAL;
725 ec = acpi_driver_data(device);
727 acpi_ec_remove_fs(device);
729 kfree(ec);
731 return 0;
734 static acpi_status
735 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
737 struct acpi_ec *ec = (struct acpi_ec *)context;
739 if (resource->type != ACPI_RESOURCE_TYPE_IO) {
740 return AE_OK;
744 * The first address region returned is the data port, and
745 * the second address region returned is the status/command
746 * port.
748 if (ec->data_addr == 0) {
749 ec->data_addr = resource->data.io.minimum;
750 } else if (ec->command_addr == 0) {
751 ec->command_addr = resource->data.io.minimum;
752 } else {
753 return AE_CTRL_TERMINATE;
756 return AE_OK;
759 static int acpi_ec_start(struct acpi_device *device)
761 acpi_status status = AE_OK;
762 struct acpi_ec *ec = NULL;
765 if (!device)
766 return -EINVAL;
768 ec = acpi_driver_data(device);
770 if (!ec)
771 return -EINVAL;
774 * Get I/O port addresses. Convert to GAS format.
776 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
777 acpi_ec_io_ports, ec);
778 if (ACPI_FAILURE(status) || ec->command_addr == 0) {
779 ACPI_EXCEPTION((AE_INFO, status,
780 "Error getting I/O port addresses"));
781 return -ENODEV;
784 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
785 ec->gpe_bit, ec->command_addr, ec->data_addr));
788 * Install GPE handler
790 status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
791 ACPI_GPE_EDGE_TRIGGERED,
792 &acpi_ec_gpe_handler, ec);
793 if (ACPI_FAILURE(status)) {
794 return -ENODEV;
796 acpi_set_gpe_type(NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
797 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
799 status = acpi_install_address_space_handler(ec->handle,
800 ACPI_ADR_SPACE_EC,
801 &acpi_ec_space_handler,
802 &acpi_ec_space_setup, ec);
803 if (ACPI_FAILURE(status)) {
804 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
805 &acpi_ec_gpe_handler);
806 return -ENODEV;
809 return AE_OK;
812 static int acpi_ec_stop(struct acpi_device *device, int type)
814 acpi_status status = AE_OK;
815 struct acpi_ec *ec = NULL;
818 if (!device)
819 return -EINVAL;
821 ec = acpi_driver_data(device);
823 status = acpi_remove_address_space_handler(ec->handle,
824 ACPI_ADR_SPACE_EC,
825 &acpi_ec_space_handler);
826 if (ACPI_FAILURE(status))
827 return -ENODEV;
829 status =
830 acpi_remove_gpe_handler(NULL, ec->gpe_bit,
831 &acpi_ec_gpe_handler);
832 if (ACPI_FAILURE(status))
833 return -ENODEV;
835 return 0;
838 static acpi_status __init
839 acpi_fake_ecdt_callback(acpi_handle handle,
840 u32 Level, void *context, void **retval)
842 acpi_status status;
844 init_MUTEX(&ec_ecdt->sem);
845 if (acpi_ec_mode == EC_INTR) {
846 init_waitqueue_head(&ec_ecdt->wait);
848 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
849 acpi_ec_io_ports, ec_ecdt);
850 if (ACPI_FAILURE(status))
851 return status;
853 ec_ecdt->uid = -1;
854 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
856 status =
857 acpi_evaluate_integer(handle, "_GPE", NULL,
858 &ec_ecdt->gpe_bit);
859 if (ACPI_FAILURE(status))
860 return status;
861 ec_ecdt->global_lock = TRUE;
862 ec_ecdt->handle = handle;
864 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
865 ec_ecdt->gpe_bit, ec_ecdt->command_addr, ec_ecdt->data_addr));
867 return AE_CTRL_TERMINATE;
871 * Some BIOS (such as some from Gateway laptops) access EC region very early
872 * such as in BAT0._INI or EC._INI before an EC device is found and
873 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
874 * required, but if EC regison is accessed early, it is required.
875 * The routine tries to workaround the BIOS bug by pre-scan EC device
876 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
877 * op region (since _REG isn't invoked yet). The assumption is true for
878 * all systems found.
880 static int __init acpi_ec_fake_ecdt(void)
882 acpi_status status;
883 int ret = 0;
885 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
887 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
888 if (!ec_ecdt) {
889 ret = -ENOMEM;
890 goto error;
892 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
894 status = acpi_get_devices(ACPI_EC_HID,
895 acpi_fake_ecdt_callback, NULL, NULL);
896 if (ACPI_FAILURE(status)) {
897 kfree(ec_ecdt);
898 ec_ecdt = NULL;
899 ret = -ENODEV;
900 ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
901 goto error;
903 return 0;
904 error:
905 return ret;
908 static int __init acpi_ec_get_real_ecdt(void)
910 acpi_status status;
911 struct acpi_table_ecdt *ecdt_ptr;
913 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
914 (struct acpi_table_header **)
915 &ecdt_ptr);
916 if (ACPI_FAILURE(status))
917 return -ENODEV;
919 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
922 * Generate a temporary ec context to use until the namespace is scanned
924 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
925 if (!ec_ecdt)
926 return -ENOMEM;
927 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
929 init_MUTEX(&ec_ecdt->sem);
930 if (acpi_ec_mode == EC_INTR) {
931 init_waitqueue_head(&ec_ecdt->wait);
933 ec_ecdt->command_addr = ecdt_ptr->ec_control.address;
934 ec_ecdt->data_addr = ecdt_ptr->ec_data.address;
935 ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
936 /* use the GL just to be safe */
937 ec_ecdt->global_lock = TRUE;
938 ec_ecdt->uid = ecdt_ptr->uid;
940 status =
941 acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
942 if (ACPI_FAILURE(status)) {
943 goto error;
946 return 0;
947 error:
948 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
949 kfree(ec_ecdt);
950 ec_ecdt = NULL;
952 return -ENODEV;
955 static int __initdata acpi_fake_ecdt_enabled;
956 int __init acpi_ec_ecdt_probe(void)
958 acpi_status status;
959 int ret;
961 ret = acpi_ec_get_real_ecdt();
962 /* Try to make a fake ECDT */
963 if (ret && acpi_fake_ecdt_enabled) {
964 ret = acpi_ec_fake_ecdt();
967 if (ret)
968 return 0;
971 * Install GPE handler
973 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
974 ACPI_GPE_EDGE_TRIGGERED,
975 &acpi_ec_gpe_handler, ec_ecdt);
976 if (ACPI_FAILURE(status)) {
977 goto error;
979 acpi_set_gpe_type(NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
980 acpi_enable_gpe(NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
982 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
983 ACPI_ADR_SPACE_EC,
984 &acpi_ec_space_handler,
985 &acpi_ec_space_setup,
986 ec_ecdt);
987 if (ACPI_FAILURE(status)) {
988 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
989 &acpi_ec_gpe_handler);
990 goto error;
993 return 0;
995 error:
996 ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
997 kfree(ec_ecdt);
998 ec_ecdt = NULL;
1000 return -ENODEV;
1003 static int __init acpi_ec_init(void)
1005 int result = 0;
1008 if (acpi_disabled)
1009 return 0;
1011 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
1012 if (!acpi_ec_dir)
1013 return -ENODEV;
1015 /* Now register the driver for the EC */
1016 result = acpi_bus_register_driver(&acpi_ec_driver);
1017 if (result < 0) {
1018 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1019 return -ENODEV;
1022 return result;
1025 subsys_initcall(acpi_ec_init);
1027 /* EC driver currently not unloadable */
1028 #if 0
1029 static void __exit acpi_ec_exit(void)
1032 acpi_bus_unregister_driver(&acpi_ec_driver);
1034 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1036 return;
1038 #endif /* 0 */
1040 static int __init acpi_fake_ecdt_setup(char *str)
1042 acpi_fake_ecdt_enabled = 1;
1043 return 1;
1046 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1047 static int __init acpi_ec_set_intr_mode(char *str)
1049 int intr;
1051 if (!get_option(&str, &intr))
1052 return 0;
1054 if (intr) {
1055 acpi_ec_mode = EC_INTR;
1056 } else {
1057 acpi_ec_mode = EC_POLL;
1059 acpi_ec_driver.ops.add = acpi_ec_add;
1060 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "EC %s mode.\n", intr ? "interrupt" : "polling"));
1062 return 1;
1065 __setup("ec_intr=", acpi_ec_set_intr_mode);