SG: audit of drivers that use blk_rq_map_sg()
[firewire-audio.git] / drivers / acpi / ec.c
blob7b4178393e34856e8b6525a0c476a99a37b6962e
1 /*
2 * ec.c - ACPI Embedded Controller Driver (v2.0)
4 * Copyright (C) 2006, 2007 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
5 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
6 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
7 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/types.h>
33 #include <linux/delay.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36 #include <linux/interrupt.h>
37 #include <linux/list.h>
38 #include <asm/io.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.h>
41 #include <acpi/actypes.h>
43 #define ACPI_EC_CLASS "embedded_controller"
44 #define ACPI_EC_DEVICE_NAME "Embedded Controller"
45 #define ACPI_EC_FILE_INFO "info"
47 #undef PREFIX
48 #define PREFIX "ACPI: EC: "
50 /* EC status register */
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 /* EC commands */
57 enum ec_command {
58 ACPI_EC_COMMAND_READ = 0x80,
59 ACPI_EC_COMMAND_WRITE = 0x81,
60 ACPI_EC_BURST_ENABLE = 0x82,
61 ACPI_EC_BURST_DISABLE = 0x83,
62 ACPI_EC_COMMAND_QUERY = 0x84,
65 /* EC events */
66 enum ec_event {
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 */
74 static enum ec_mode {
75 EC_INTR = 1, /* Output buffer full */
76 EC_POLL, /* Input buffer empty */
77 } acpi_ec_mode = EC_INTR;
79 static int acpi_ec_remove(struct acpi_device *device, int type);
80 static int acpi_ec_start(struct acpi_device *device);
81 static int acpi_ec_stop(struct acpi_device *device, int type);
82 static int acpi_ec_add(struct acpi_device *device);
84 static const struct acpi_device_id ec_device_ids[] = {
85 {"PNP0C09", 0},
86 {"", 0},
89 static struct acpi_driver acpi_ec_driver = {
90 .name = "ec",
91 .class = ACPI_EC_CLASS,
92 .ids = ec_device_ids,
93 .ops = {
94 .add = acpi_ec_add,
95 .remove = acpi_ec_remove,
96 .start = acpi_ec_start,
97 .stop = acpi_ec_stop,
101 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
102 /* External interfaces use first EC only, so remember */
103 typedef int (*acpi_ec_query_func) (void *data);
105 struct acpi_ec_query_handler {
106 struct list_head node;
107 acpi_ec_query_func func;
108 acpi_handle handle;
109 void *data;
110 u8 query_bit;
113 static struct acpi_ec {
114 acpi_handle handle;
115 unsigned long gpe;
116 unsigned long command_addr;
117 unsigned long data_addr;
118 unsigned long global_lock;
119 struct mutex lock;
120 atomic_t query_pending;
121 atomic_t event_count;
122 wait_queue_head_t wait;
123 struct list_head list;
124 u8 handlers_installed;
125 } *boot_ec, *first_ec;
127 /* --------------------------------------------------------------------------
128 Transaction Management
129 -------------------------------------------------------------------------- */
131 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
133 return inb(ec->command_addr);
136 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
138 return inb(ec->data_addr);
141 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
143 outb(command, ec->command_addr);
146 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
148 outb(data, ec->data_addr);
151 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
152 unsigned old_count)
154 u8 status = acpi_ec_read_status(ec);
155 if (old_count == atomic_read(&ec->event_count))
156 return 0;
157 if (event == ACPI_EC_EVENT_OBF_1) {
158 if (status & ACPI_EC_FLAG_OBF)
159 return 1;
160 } else if (event == ACPI_EC_EVENT_IBF_0) {
161 if (!(status & ACPI_EC_FLAG_IBF))
162 return 1;
165 return 0;
168 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event,
169 unsigned count, int force_poll)
171 if (unlikely(force_poll) || acpi_ec_mode == EC_POLL) {
172 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
173 while (time_before(jiffies, delay)) {
174 if (acpi_ec_check_status(ec, event, 0))
175 return 0;
177 } else {
178 if (wait_event_timeout(ec->wait,
179 acpi_ec_check_status(ec, event, count),
180 msecs_to_jiffies(ACPI_EC_DELAY)) ||
181 acpi_ec_check_status(ec, event, 0)) {
182 return 0;
183 } else {
184 printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
185 " status = %d, expect_event = %d\n",
186 acpi_ec_read_status(ec), event);
190 return -ETIME;
193 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
194 const u8 * wdata, unsigned wdata_len,
195 u8 * rdata, unsigned rdata_len,
196 int force_poll)
198 int result = 0;
199 unsigned count = atomic_read(&ec->event_count);
200 acpi_ec_write_cmd(ec, command);
202 for (; wdata_len > 0; --wdata_len) {
203 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
204 if (result) {
205 printk(KERN_ERR PREFIX
206 "write_cmd timeout, command = %d\n", command);
207 goto end;
209 count = atomic_read(&ec->event_count);
210 acpi_ec_write_data(ec, *(wdata++));
213 if (!rdata_len) {
214 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count, force_poll);
215 if (result) {
216 printk(KERN_ERR PREFIX
217 "finish-write timeout, command = %d\n", command);
218 goto end;
220 } else if (command == ACPI_EC_COMMAND_QUERY) {
221 atomic_set(&ec->query_pending, 0);
224 for (; rdata_len > 0; --rdata_len) {
225 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count, force_poll);
226 if (result) {
227 printk(KERN_ERR PREFIX "read timeout, command = %d\n",
228 command);
229 goto end;
231 count = atomic_read(&ec->event_count);
232 *(rdata++) = acpi_ec_read_data(ec);
234 end:
235 return result;
238 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
239 const u8 * wdata, unsigned wdata_len,
240 u8 * rdata, unsigned rdata_len,
241 int force_poll)
243 int status;
244 u32 glk;
246 if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
247 return -EINVAL;
249 if (rdata)
250 memset(rdata, 0, rdata_len);
252 mutex_lock(&ec->lock);
253 if (ec->global_lock) {
254 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
255 if (ACPI_FAILURE(status)) {
256 mutex_unlock(&ec->lock);
257 return -ENODEV;
261 /* Make sure GPE is enabled before doing transaction */
262 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
264 status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0, 0);
265 if (status) {
266 printk(KERN_ERR PREFIX
267 "input buffer is not empty, aborting transaction\n");
268 goto end;
271 status = acpi_ec_transaction_unlocked(ec, command,
272 wdata, wdata_len,
273 rdata, rdata_len,
274 force_poll);
276 end:
278 if (ec->global_lock)
279 acpi_release_global_lock(glk);
280 mutex_unlock(&ec->lock);
282 return status;
286 * Note: samsung nv5000 doesn't work with ec burst mode.
287 * http://bugzilla.kernel.org/show_bug.cgi?id=4980
289 int acpi_ec_burst_enable(struct acpi_ec *ec)
291 u8 d;
292 return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1, 0);
295 int acpi_ec_burst_disable(struct acpi_ec *ec)
297 return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0, 0);
300 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
302 int result;
303 u8 d;
305 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
306 &address, 1, &d, 1, 0);
307 *data = d;
308 return result;
311 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
313 u8 wdata[2] = { address, data };
314 return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
315 wdata, 2, NULL, 0, 0);
319 * Externally callable EC access functions. For now, assume 1 EC only
321 int ec_burst_enable(void)
323 if (!first_ec)
324 return -ENODEV;
325 return acpi_ec_burst_enable(first_ec);
328 EXPORT_SYMBOL(ec_burst_enable);
330 int ec_burst_disable(void)
332 if (!first_ec)
333 return -ENODEV;
334 return acpi_ec_burst_disable(first_ec);
337 EXPORT_SYMBOL(ec_burst_disable);
339 int ec_read(u8 addr, u8 * val)
341 int err;
342 u8 temp_data;
344 if (!first_ec)
345 return -ENODEV;
347 err = acpi_ec_read(first_ec, addr, &temp_data);
349 if (!err) {
350 *val = temp_data;
351 return 0;
352 } else
353 return err;
356 EXPORT_SYMBOL(ec_read);
358 int ec_write(u8 addr, u8 val)
360 int err;
362 if (!first_ec)
363 return -ENODEV;
365 err = acpi_ec_write(first_ec, addr, val);
367 return err;
370 EXPORT_SYMBOL(ec_write);
372 int ec_transaction(u8 command,
373 const u8 * wdata, unsigned wdata_len,
374 u8 * rdata, unsigned rdata_len,
375 int force_poll)
377 if (!first_ec)
378 return -ENODEV;
380 return acpi_ec_transaction(first_ec, command, wdata,
381 wdata_len, rdata, rdata_len,
382 force_poll);
385 EXPORT_SYMBOL(ec_transaction);
387 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
389 int result;
390 u8 d;
392 if (!ec || !data)
393 return -EINVAL;
396 * Query the EC to find out which _Qxx method we need to evaluate.
397 * Note that successful completion of the query causes the ACPI_EC_SCI
398 * bit to be cleared (and thus clearing the interrupt source).
401 result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1, 0);
402 if (result)
403 return result;
405 if (!d)
406 return -ENODATA;
408 *data = d;
409 return 0;
412 /* --------------------------------------------------------------------------
413 Event Management
414 -------------------------------------------------------------------------- */
415 int acpi_ec_add_query_handler(struct acpi_ec *ec, u8 query_bit,
416 acpi_handle handle, acpi_ec_query_func func,
417 void *data)
419 struct acpi_ec_query_handler *handler =
420 kzalloc(sizeof(struct acpi_ec_query_handler), GFP_KERNEL);
421 if (!handler)
422 return -ENOMEM;
424 handler->query_bit = query_bit;
425 handler->handle = handle;
426 handler->func = func;
427 handler->data = data;
428 mutex_lock(&ec->lock);
429 list_add(&handler->node, &ec->list);
430 mutex_unlock(&ec->lock);
431 return 0;
434 EXPORT_SYMBOL_GPL(acpi_ec_add_query_handler);
436 void acpi_ec_remove_query_handler(struct acpi_ec *ec, u8 query_bit)
438 struct acpi_ec_query_handler *handler;
439 mutex_lock(&ec->lock);
440 list_for_each_entry(handler, &ec->list, node) {
441 if (query_bit == handler->query_bit) {
442 list_del(&handler->node);
443 kfree(handler);
446 mutex_unlock(&ec->lock);
449 EXPORT_SYMBOL_GPL(acpi_ec_remove_query_handler);
451 static void acpi_ec_gpe_query(void *ec_cxt)
453 struct acpi_ec *ec = ec_cxt;
454 u8 value = 0;
455 struct acpi_ec_query_handler *handler, copy;
457 if (!ec || acpi_ec_query(ec, &value))
458 return;
459 mutex_lock(&ec->lock);
460 list_for_each_entry(handler, &ec->list, node) {
461 if (value == handler->query_bit) {
462 /* have custom handler for this bit */
463 memcpy(&copy, handler, sizeof(copy));
464 mutex_unlock(&ec->lock);
465 if (copy.func) {
466 copy.func(copy.data);
467 } else if (copy.handle) {
468 acpi_evaluate_object(copy.handle, NULL, NULL, NULL);
470 return;
473 mutex_unlock(&ec->lock);
476 static u32 acpi_ec_gpe_handler(void *data)
478 acpi_status status = AE_OK;
479 u8 value;
480 struct acpi_ec *ec = data;
482 atomic_inc(&ec->event_count);
484 if (acpi_ec_mode == EC_INTR) {
485 wake_up(&ec->wait);
488 value = acpi_ec_read_status(ec);
489 if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
490 atomic_set(&ec->query_pending, 1);
491 status =
492 acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query, ec);
495 return status == AE_OK ?
496 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
499 /* --------------------------------------------------------------------------
500 Address Space Management
501 -------------------------------------------------------------------------- */
503 static acpi_status
504 acpi_ec_space_setup(acpi_handle region_handle,
505 u32 function, void *handler_context, void **return_context)
508 * The EC object is in the handler context and is needed
509 * when calling the acpi_ec_space_handler.
511 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
512 handler_context : NULL;
514 return AE_OK;
517 static acpi_status
518 acpi_ec_space_handler(u32 function, acpi_physical_address address,
519 u32 bits, acpi_integer *value,
520 void *handler_context, void *region_context)
522 struct acpi_ec *ec = handler_context;
523 int result = 0, i = 0;
524 u8 temp = 0;
526 if ((address > 0xFF) || !value || !handler_context)
527 return AE_BAD_PARAMETER;
529 if (function != ACPI_READ && function != ACPI_WRITE)
530 return AE_BAD_PARAMETER;
532 if (bits != 8 && acpi_strict)
533 return AE_BAD_PARAMETER;
535 while (bits - i > 0) {
536 if (function == ACPI_READ) {
537 result = acpi_ec_read(ec, address, &temp);
538 (*value) |= ((acpi_integer)temp) << i;
539 } else {
540 temp = 0xff & ((*value) >> i);
541 result = acpi_ec_write(ec, address, temp);
543 i += 8;
544 ++address;
547 switch (result) {
548 case -EINVAL:
549 return AE_BAD_PARAMETER;
550 break;
551 case -ENODEV:
552 return AE_NOT_FOUND;
553 break;
554 case -ETIME:
555 return AE_TIME;
556 break;
557 default:
558 return AE_OK;
562 /* --------------------------------------------------------------------------
563 FS Interface (/proc)
564 -------------------------------------------------------------------------- */
566 static struct proc_dir_entry *acpi_ec_dir;
568 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
570 struct acpi_ec *ec = seq->private;
572 if (!ec)
573 goto end;
575 seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
576 seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
577 (unsigned)ec->command_addr, (unsigned)ec->data_addr);
578 seq_printf(seq, "use global lock:\t%s\n",
579 ec->global_lock ? "yes" : "no");
580 end:
581 return 0;
584 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
586 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
589 static struct file_operations acpi_ec_info_ops = {
590 .open = acpi_ec_info_open_fs,
591 .read = seq_read,
592 .llseek = seq_lseek,
593 .release = single_release,
594 .owner = THIS_MODULE,
597 static int acpi_ec_add_fs(struct acpi_device *device)
599 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 -------------------------------------------------------------------------- */
636 static acpi_status
637 ec_parse_io_ports(struct acpi_resource *resource, void *context);
639 static struct acpi_ec *make_acpi_ec(void)
641 struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
642 if (!ec)
643 return NULL;
645 atomic_set(&ec->query_pending, 1);
646 atomic_set(&ec->event_count, 1);
647 mutex_init(&ec->lock);
648 init_waitqueue_head(&ec->wait);
649 INIT_LIST_HEAD(&ec->list);
651 return ec;
654 static acpi_status
655 acpi_ec_register_query_methods(acpi_handle handle, u32 level,
656 void *context, void **return_value)
658 struct acpi_namespace_node *node = handle;
659 struct acpi_ec *ec = context;
660 int value = 0;
661 if (sscanf(node->name.ascii, "_Q%x", &value) == 1) {
662 acpi_ec_add_query_handler(ec, value, handle, NULL, NULL);
664 return AE_OK;
667 static acpi_status
668 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
670 acpi_status status;
672 struct acpi_ec *ec = context;
673 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
674 ec_parse_io_ports, ec);
675 if (ACPI_FAILURE(status))
676 return status;
678 /* Get GPE bit assignment (EC events). */
679 /* TODO: Add support for _GPE returning a package */
680 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
681 if (ACPI_FAILURE(status))
682 return status;
683 /* Find and register all query methods */
684 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
685 acpi_ec_register_query_methods, ec, NULL);
686 /* Use the global lock for all EC transactions? */
687 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
688 ec->handle = handle;
689 return AE_CTRL_TERMINATE;
692 static void ec_remove_handlers(struct acpi_ec *ec)
694 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
695 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
696 printk(KERN_ERR PREFIX "failed to remove space handler\n");
697 if (ACPI_FAILURE(acpi_remove_gpe_handler(NULL, ec->gpe,
698 &acpi_ec_gpe_handler)))
699 printk(KERN_ERR PREFIX "failed to remove gpe handler\n");
700 ec->handlers_installed = 0;
703 static int acpi_ec_add(struct acpi_device *device)
705 struct acpi_ec *ec = NULL;
707 if (!device)
708 return -EINVAL;
709 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
710 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
712 /* Check for boot EC */
713 if (boot_ec) {
714 if (boot_ec->handle == device->handle) {
715 /* Pre-loaded EC from DSDT, just move pointer */
716 ec = boot_ec;
717 boot_ec = NULL;
718 goto end;
719 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) {
720 /* ECDT-based EC, time to shut it down */
721 ec_remove_handlers(boot_ec);
722 kfree(boot_ec);
723 first_ec = boot_ec = NULL;
727 ec = make_acpi_ec();
728 if (!ec)
729 return -ENOMEM;
731 if (ec_parse_device(device->handle, 0, ec, NULL) !=
732 AE_CTRL_TERMINATE) {
733 kfree(ec);
734 return -EINVAL;
736 ec->handle = device->handle;
737 end:
738 if (!first_ec)
739 first_ec = ec;
740 acpi_driver_data(device) = ec;
741 acpi_ec_add_fs(device);
742 printk(KERN_INFO PREFIX "GPE = 0x%lx, I/O: command/status = 0x%lx, data = 0x%lx\n",
743 ec->gpe, ec->command_addr, ec->data_addr);
744 return 0;
747 static int acpi_ec_remove(struct acpi_device *device, int type)
749 struct acpi_ec *ec;
750 struct acpi_ec_query_handler *handler, *tmp;
752 if (!device)
753 return -EINVAL;
755 ec = acpi_driver_data(device);
756 mutex_lock(&ec->lock);
757 list_for_each_entry_safe(handler, tmp, &ec->list, node) {
758 list_del(&handler->node);
759 kfree(handler);
761 mutex_unlock(&ec->lock);
762 acpi_ec_remove_fs(device);
763 acpi_driver_data(device) = NULL;
764 if (ec == first_ec)
765 first_ec = NULL;
766 kfree(ec);
767 return 0;
770 static acpi_status
771 ec_parse_io_ports(struct acpi_resource *resource, void *context)
773 struct acpi_ec *ec = context;
775 if (resource->type != ACPI_RESOURCE_TYPE_IO)
776 return AE_OK;
779 * The first address region returned is the data port, and
780 * the second address region returned is the status/command
781 * port.
783 if (ec->data_addr == 0)
784 ec->data_addr = resource->data.io.minimum;
785 else if (ec->command_addr == 0)
786 ec->command_addr = resource->data.io.minimum;
787 else
788 return AE_CTRL_TERMINATE;
790 return AE_OK;
793 static int ec_install_handlers(struct acpi_ec *ec)
795 acpi_status status;
796 if (ec->handlers_installed)
797 return 0;
798 status = acpi_install_gpe_handler(NULL, ec->gpe,
799 ACPI_GPE_EDGE_TRIGGERED,
800 &acpi_ec_gpe_handler, ec);
801 if (ACPI_FAILURE(status))
802 return -ENODEV;
804 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
805 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
807 status = acpi_install_address_space_handler(ec->handle,
808 ACPI_ADR_SPACE_EC,
809 &acpi_ec_space_handler,
810 &acpi_ec_space_setup, ec);
811 if (ACPI_FAILURE(status)) {
812 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
813 return -ENODEV;
816 ec->handlers_installed = 1;
817 return 0;
820 static int acpi_ec_start(struct acpi_device *device)
822 struct acpi_ec *ec;
823 int ret = 0;
825 if (!device)
826 return -EINVAL;
828 ec = acpi_driver_data(device);
830 if (!ec)
831 return -EINVAL;
833 ret = ec_install_handlers(ec);
835 /* EC is fully operational, allow queries */
836 atomic_set(&ec->query_pending, 0);
837 return ret;
840 static int acpi_ec_stop(struct acpi_device *device, int type)
842 struct acpi_ec *ec;
843 if (!device)
844 return -EINVAL;
845 ec = acpi_driver_data(device);
846 if (!ec)
847 return -EINVAL;
848 ec_remove_handlers(ec);
850 return 0;
853 int __init acpi_ec_ecdt_probe(void)
855 int ret;
856 acpi_status status;
857 struct acpi_table_ecdt *ecdt_ptr;
859 boot_ec = make_acpi_ec();
860 if (!boot_ec)
861 return -ENOMEM;
863 * Generate a boot ec context
865 status = acpi_get_table(ACPI_SIG_ECDT, 1,
866 (struct acpi_table_header **)&ecdt_ptr);
867 if (ACPI_SUCCESS(status)) {
868 printk(KERN_INFO PREFIX "EC description table is found, configuring boot EC\n");
869 boot_ec->command_addr = ecdt_ptr->control.address;
870 boot_ec->data_addr = ecdt_ptr->data.address;
871 boot_ec->gpe = ecdt_ptr->gpe;
872 boot_ec->handle = ACPI_ROOT_OBJECT;
873 } else {
874 printk(KERN_DEBUG PREFIX "Look up EC in DSDT\n");
875 status = acpi_get_devices(ec_device_ids[0].id, ec_parse_device,
876 boot_ec, NULL);
877 /* Check that acpi_get_devices actually find something */
878 if (ACPI_FAILURE(status) || !boot_ec->handle)
879 goto error;
882 ret = ec_install_handlers(boot_ec);
883 if (!ret) {
884 first_ec = boot_ec;
885 return 0;
887 error:
888 kfree(boot_ec);
889 boot_ec = NULL;
890 return -ENODEV;
893 static int __init acpi_ec_init(void)
895 int result = 0;
897 if (acpi_disabled)
898 return 0;
900 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
901 if (!acpi_ec_dir)
902 return -ENODEV;
904 /* Now register the driver for the EC */
905 result = acpi_bus_register_driver(&acpi_ec_driver);
906 if (result < 0) {
907 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
908 return -ENODEV;
911 return result;
914 subsys_initcall(acpi_ec_init);
916 /* EC driver currently not unloadable */
917 #if 0
918 static void __exit acpi_ec_exit(void)
921 acpi_bus_unregister_driver(&acpi_ec_driver);
923 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
925 return;
927 #endif /* 0 */
929 static int __init acpi_ec_set_intr_mode(char *str)
931 int intr;
933 if (!get_option(&str, &intr))
934 return 0;
936 acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
938 printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
940 return 1;
943 __setup("ec_intr=", acpi_ec_set_intr_mode);