[PATCH] acpi bridge hotadd: Allow ACPI .add and .start operations to be done independ...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / ec.c
blobfdf143b405be7fb638685a7bf0bb104239348dd4
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 <asm/io.h>
35 #include <acpi/acpi_bus.h>
36 #include <acpi/acpi_drivers.h>
37 #include <acpi/actypes.h>
39 #define _COMPONENT ACPI_EC_COMPONENT
40 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"
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_SCI 0x20 /* EC-SCI occurred */
54 #define ACPI_EC_EVENT_OBF 0x01 /* Output buffer full */
55 #define ACPI_EC_EVENT_IBE 0x02 /* Input buffer empty */
57 #define ACPI_EC_UDELAY 100 /* Poll @ 100us increments */
58 #define ACPI_EC_UDELAY_COUNT 1000 /* Wait 10ms max. during EC ops */
59 #define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
61 #define ACPI_EC_COMMAND_READ 0x80
62 #define ACPI_EC_COMMAND_WRITE 0x81
63 #define ACPI_EC_COMMAND_QUERY 0x84
65 static int acpi_ec_add (struct acpi_device *device);
66 static int acpi_ec_remove (struct acpi_device *device, int type);
67 static int acpi_ec_start (struct acpi_device *device);
68 static int acpi_ec_stop (struct acpi_device *device, int type);
70 static struct acpi_driver acpi_ec_driver = {
71 .name = ACPI_EC_DRIVER_NAME,
72 .class = ACPI_EC_CLASS,
73 .ids = ACPI_EC_HID,
74 .ops = {
75 .add = acpi_ec_add,
76 .remove = acpi_ec_remove,
77 .start = acpi_ec_start,
78 .stop = acpi_ec_stop,
82 struct acpi_ec {
83 acpi_handle handle;
84 unsigned long uid;
85 unsigned long gpe_bit;
86 struct acpi_generic_address status_addr;
87 struct acpi_generic_address command_addr;
88 struct acpi_generic_address data_addr;
89 unsigned long global_lock;
90 spinlock_t lock;
93 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
94 static struct acpi_ec *ec_ecdt;
96 /* External interfaces use first EC only, so remember */
97 static struct acpi_device *first_ec;
99 /* --------------------------------------------------------------------------
100 Transaction Management
101 -------------------------------------------------------------------------- */
103 static int
104 acpi_ec_wait (
105 struct acpi_ec *ec,
106 u8 event)
108 u32 acpi_ec_status = 0;
109 u32 i = ACPI_EC_UDELAY_COUNT;
111 if (!ec)
112 return -EINVAL;
114 /* Poll the EC status register waiting for the event to occur. */
115 switch (event) {
116 case ACPI_EC_EVENT_OBF:
117 do {
118 acpi_hw_low_level_read(8, &acpi_ec_status, &ec->status_addr);
119 if (acpi_ec_status & ACPI_EC_FLAG_OBF)
120 return 0;
121 udelay(ACPI_EC_UDELAY);
122 } while (--i>0);
123 break;
124 case ACPI_EC_EVENT_IBE:
125 do {
126 acpi_hw_low_level_read(8, &acpi_ec_status, &ec->status_addr);
127 if (!(acpi_ec_status & ACPI_EC_FLAG_IBF))
128 return 0;
129 udelay(ACPI_EC_UDELAY);
130 } while (--i>0);
131 break;
132 default:
133 return -EINVAL;
136 return -ETIME;
140 static int
141 acpi_ec_read (
142 struct acpi_ec *ec,
143 u8 address,
144 u32 *data)
146 acpi_status status = AE_OK;
147 int result = 0;
148 unsigned long flags = 0;
149 u32 glk = 0;
151 ACPI_FUNCTION_TRACE("acpi_ec_read");
153 if (!ec || !data)
154 return_VALUE(-EINVAL);
156 *data = 0;
158 if (ec->global_lock) {
159 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
160 if (ACPI_FAILURE(status))
161 return_VALUE(-ENODEV);
164 spin_lock_irqsave(&ec->lock, flags);
166 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_READ, &ec->command_addr);
167 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
168 if (result)
169 goto end;
171 acpi_hw_low_level_write(8, address, &ec->data_addr);
172 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
173 if (result)
174 goto end;
177 acpi_hw_low_level_read(8, data, &ec->data_addr);
179 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Read [%02x] from address [%02x]\n",
180 *data, address));
182 end:
183 spin_unlock_irqrestore(&ec->lock, flags);
185 if (ec->global_lock)
186 acpi_release_global_lock(glk);
188 return_VALUE(result);
192 static int
193 acpi_ec_write (
194 struct acpi_ec *ec,
195 u8 address,
196 u8 data)
198 int result = 0;
199 acpi_status status = AE_OK;
200 unsigned long flags = 0;
201 u32 glk = 0;
203 ACPI_FUNCTION_TRACE("acpi_ec_write");
205 if (!ec)
206 return_VALUE(-EINVAL);
208 if (ec->global_lock) {
209 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
210 if (ACPI_FAILURE(status))
211 return_VALUE(-ENODEV);
214 spin_lock_irqsave(&ec->lock, flags);
216 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_WRITE, &ec->command_addr);
217 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
218 if (result)
219 goto end;
221 acpi_hw_low_level_write(8, address, &ec->data_addr);
222 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
223 if (result)
224 goto end;
226 acpi_hw_low_level_write(8, data, &ec->data_addr);
227 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBE);
228 if (result)
229 goto end;
231 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Wrote [%02x] to address [%02x]\n",
232 data, address));
234 end:
235 spin_unlock_irqrestore(&ec->lock, flags);
237 if (ec->global_lock)
238 acpi_release_global_lock(glk);
240 return_VALUE(result);
244 * Externally callable EC access functions. For now, assume 1 EC only
247 ec_read(u8 addr, u8 *val)
249 struct acpi_ec *ec;
250 int err;
251 u32 temp_data;
253 if (!first_ec)
254 return -ENODEV;
256 ec = acpi_driver_data(first_ec);
258 err = acpi_ec_read(ec, addr, &temp_data);
260 if (!err) {
261 *val = temp_data;
262 return 0;
264 else
265 return err;
267 EXPORT_SYMBOL(ec_read);
270 ec_write(u8 addr, u8 val)
272 struct acpi_ec *ec;
273 int err;
275 if (!first_ec)
276 return -ENODEV;
278 ec = acpi_driver_data(first_ec);
280 err = acpi_ec_write(ec, addr, val);
282 return err;
284 EXPORT_SYMBOL(ec_write);
287 static int
288 acpi_ec_query (
289 struct acpi_ec *ec,
290 u32 *data)
292 int result = 0;
293 acpi_status status = AE_OK;
294 unsigned long flags = 0;
295 u32 glk = 0;
297 ACPI_FUNCTION_TRACE("acpi_ec_query");
299 if (!ec || !data)
300 return_VALUE(-EINVAL);
302 *data = 0;
304 if (ec->global_lock) {
305 status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
306 if (ACPI_FAILURE(status))
307 return_VALUE(-ENODEV);
311 * Query the EC to find out which _Qxx method we need to evaluate.
312 * Note that successful completion of the query causes the ACPI_EC_SCI
313 * bit to be cleared (and thus clearing the interrupt source).
315 spin_lock_irqsave(&ec->lock, flags);
317 acpi_hw_low_level_write(8, ACPI_EC_COMMAND_QUERY, &ec->command_addr);
318 result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF);
319 if (result)
320 goto end;
322 acpi_hw_low_level_read(8, data, &ec->data_addr);
323 if (!*data)
324 result = -ENODATA;
326 end:
327 spin_unlock_irqrestore(&ec->lock, flags);
329 if (ec->global_lock)
330 acpi_release_global_lock(glk);
332 return_VALUE(result);
336 /* --------------------------------------------------------------------------
337 Event Management
338 -------------------------------------------------------------------------- */
340 struct acpi_ec_query_data {
341 acpi_handle handle;
342 u8 data;
345 static void
346 acpi_ec_gpe_query (
347 void *ec_cxt)
349 struct acpi_ec *ec = (struct acpi_ec *) ec_cxt;
350 u32 value = 0;
351 unsigned long flags = 0;
352 static char object_name[5] = {'_','Q','0','0','\0'};
353 const char hex[] = {'0','1','2','3','4','5','6','7',
354 '8','9','A','B','C','D','E','F'};
356 ACPI_FUNCTION_TRACE("acpi_ec_gpe_query");
358 if (!ec_cxt)
359 goto end;
361 spin_lock_irqsave(&ec->lock, flags);
362 acpi_hw_low_level_read(8, &value, &ec->command_addr);
363 spin_unlock_irqrestore(&ec->lock, flags);
365 /* TBD: Implement asynch events!
366 * NOTE: All we care about are EC-SCI's. Other EC events are
367 * handled via polling (yuck!). This is because some systems
368 * treat EC-SCIs as level (versus EDGE!) triggered, preventing
369 * a purely interrupt-driven approach (grumble, grumble).
371 if (!(value & ACPI_EC_FLAG_SCI))
372 goto end;
374 if (acpi_ec_query(ec, &value))
375 goto end;
377 object_name[2] = hex[((value >> 4) & 0x0F)];
378 object_name[3] = hex[(value & 0x0F)];
380 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s\n", object_name));
382 acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
384 end:
385 acpi_enable_gpe(NULL, ec->gpe_bit, ACPI_NOT_ISR);
388 static u32
389 acpi_ec_gpe_handler (
390 void *data)
392 acpi_status status = AE_OK;
393 struct acpi_ec *ec = (struct acpi_ec *) data;
395 if (!ec)
396 return ACPI_INTERRUPT_NOT_HANDLED;
398 acpi_disable_gpe(NULL, ec->gpe_bit, ACPI_ISR);
400 status = acpi_os_queue_for_execution(OSD_PRIORITY_GPE,
401 acpi_ec_gpe_query, ec);
403 if (status == AE_OK)
404 return ACPI_INTERRUPT_HANDLED;
405 else
406 return ACPI_INTERRUPT_NOT_HANDLED;
409 /* --------------------------------------------------------------------------
410 Address Space Management
411 -------------------------------------------------------------------------- */
413 static acpi_status
414 acpi_ec_space_setup (
415 acpi_handle region_handle,
416 u32 function,
417 void *handler_context,
418 void **return_context)
421 * The EC object is in the handler context and is needed
422 * when calling the acpi_ec_space_handler.
424 if(function == ACPI_REGION_DEACTIVATE)
425 *return_context = NULL;
426 else
427 *return_context = handler_context;
429 return AE_OK;
433 static acpi_status
434 acpi_ec_space_handler (
435 u32 function,
436 acpi_physical_address address,
437 u32 bit_width,
438 acpi_integer *value,
439 void *handler_context,
440 void *region_context)
442 int result = 0;
443 struct acpi_ec *ec = NULL;
444 u32 temp = 0;
445 acpi_integer f_v = 0;
446 int i = 0;
448 ACPI_FUNCTION_TRACE("acpi_ec_space_handler");
450 if ((address > 0xFF) || !value || !handler_context)
451 return_VALUE(AE_BAD_PARAMETER);
453 if(bit_width != 8) {
454 printk(KERN_WARNING PREFIX "acpi_ec_space_handler: bit_width should be 8\n");
455 if (acpi_strict)
456 return_VALUE(AE_BAD_PARAMETER);
459 ec = (struct acpi_ec *) handler_context;
461 next_byte:
462 switch (function) {
463 case ACPI_READ:
464 result = acpi_ec_read(ec, (u8) address, &temp);
465 *value = (acpi_integer) temp;
466 break;
467 case ACPI_WRITE:
468 result = acpi_ec_write(ec, (u8) address, (u8) *value);
469 break;
470 default:
471 result = -EINVAL;
472 goto out;
473 break;
476 bit_width -= 8;
477 if(bit_width){
479 if(function == ACPI_READ)
480 f_v |= (acpi_integer) (*value) << 8*i;
481 if(function == ACPI_WRITE)
482 (*value) >>=8;
483 i++;
484 goto next_byte;
488 if(function == ACPI_READ){
489 f_v |= (acpi_integer) (*value) << 8*i;
490 *value = f_v;
494 out:
495 switch (result) {
496 case -EINVAL:
497 return_VALUE(AE_BAD_PARAMETER);
498 break;
499 case -ENODEV:
500 return_VALUE(AE_NOT_FOUND);
501 break;
502 case -ETIME:
503 return_VALUE(AE_TIME);
504 break;
505 default:
506 return_VALUE(AE_OK);
513 /* --------------------------------------------------------------------------
514 FS Interface (/proc)
515 -------------------------------------------------------------------------- */
517 static struct proc_dir_entry *acpi_ec_dir;
520 static int
521 acpi_ec_read_info (struct seq_file *seq, void *offset)
523 struct acpi_ec *ec = (struct acpi_ec *) seq->private;
525 ACPI_FUNCTION_TRACE("acpi_ec_read_info");
527 if (!ec)
528 goto end;
530 seq_printf(seq, "gpe bit: 0x%02x\n",
531 (u32) ec->gpe_bit);
532 seq_printf(seq, "ports: 0x%02x, 0x%02x\n",
533 (u32) ec->status_addr.address, (u32) ec->data_addr.address);
534 seq_printf(seq, "use global lock: %s\n",
535 ec->global_lock?"yes":"no");
537 end:
538 return_VALUE(0);
541 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
543 return single_open(file, acpi_ec_read_info, PDE(inode)->data);
546 static struct file_operations acpi_ec_info_ops = {
547 .open = acpi_ec_info_open_fs,
548 .read = seq_read,
549 .llseek = seq_lseek,
550 .release = single_release,
551 .owner = THIS_MODULE,
554 static int
555 acpi_ec_add_fs (
556 struct acpi_device *device)
558 struct proc_dir_entry *entry = NULL;
560 ACPI_FUNCTION_TRACE("acpi_ec_add_fs");
562 if (!acpi_device_dir(device)) {
563 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
564 acpi_ec_dir);
565 if (!acpi_device_dir(device))
566 return_VALUE(-ENODEV);
569 entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
570 acpi_device_dir(device));
571 if (!entry)
572 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
573 "Unable to create '%s' fs entry\n",
574 ACPI_EC_FILE_INFO));
575 else {
576 entry->proc_fops = &acpi_ec_info_ops;
577 entry->data = acpi_driver_data(device);
578 entry->owner = THIS_MODULE;
581 return_VALUE(0);
585 static int
586 acpi_ec_remove_fs (
587 struct acpi_device *device)
589 ACPI_FUNCTION_TRACE("acpi_ec_remove_fs");
591 if (acpi_device_dir(device)) {
592 remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
593 remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
594 acpi_device_dir(device) = NULL;
597 return_VALUE(0);
601 /* --------------------------------------------------------------------------
602 Driver Interface
603 -------------------------------------------------------------------------- */
605 static int
606 acpi_ec_add (
607 struct acpi_device *device)
609 int result = 0;
610 acpi_status status = AE_OK;
611 struct acpi_ec *ec = NULL;
612 unsigned long uid;
614 ACPI_FUNCTION_TRACE("acpi_ec_add");
616 if (!device)
617 return_VALUE(-EINVAL);
619 ec = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
620 if (!ec)
621 return_VALUE(-ENOMEM);
622 memset(ec, 0, sizeof(struct acpi_ec));
624 ec->handle = device->handle;
625 ec->uid = -1;
626 spin_lock_init(&ec->lock);
627 strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
628 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
629 acpi_driver_data(device) = ec;
631 /* Use the global lock for all EC transactions? */
632 acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
634 /* If our UID matches the UID for the ECDT-enumerated EC,
635 we now have the *real* EC info, so kill the makeshift one.*/
636 acpi_evaluate_integer(ec->handle, "_UID", NULL, &uid);
637 if (ec_ecdt && ec_ecdt->uid == uid) {
638 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
639 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
641 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit, &acpi_ec_gpe_handler);
643 kfree(ec_ecdt);
646 /* Get GPE bit assignment (EC events). */
647 /* TODO: Add support for _GPE returning a package */
648 status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe_bit);
649 if (ACPI_FAILURE(status)) {
650 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
651 "Error obtaining GPE bit assignment\n"));
652 result = -ENODEV;
653 goto end;
656 result = acpi_ec_add_fs(device);
657 if (result)
658 goto end;
660 printk(KERN_INFO PREFIX "%s [%s] (gpe %d)\n",
661 acpi_device_name(device), acpi_device_bid(device),
662 (u32) ec->gpe_bit);
664 if (!first_ec)
665 first_ec = device;
667 end:
668 if (result)
669 kfree(ec);
671 return_VALUE(result);
675 static int
676 acpi_ec_remove (
677 struct acpi_device *device,
678 int type)
680 struct acpi_ec *ec = NULL;
682 ACPI_FUNCTION_TRACE("acpi_ec_remove");
684 if (!device)
685 return_VALUE(-EINVAL);
687 ec = acpi_driver_data(device);
689 acpi_ec_remove_fs(device);
691 kfree(ec);
693 return_VALUE(0);
697 static acpi_status
698 acpi_ec_io_ports (
699 struct acpi_resource *resource,
700 void *context)
702 struct acpi_ec *ec = (struct acpi_ec *) context;
703 struct acpi_generic_address *addr;
705 if (resource->id != ACPI_RSTYPE_IO) {
706 return AE_OK;
710 * The first address region returned is the data port, and
711 * the second address region returned is the status/command
712 * port.
714 if (ec->data_addr.register_bit_width == 0) {
715 addr = &ec->data_addr;
716 } else if (ec->command_addr.register_bit_width == 0) {
717 addr = &ec->command_addr;
718 } else {
719 return AE_CTRL_TERMINATE;
722 addr->address_space_id = ACPI_ADR_SPACE_SYSTEM_IO;
723 addr->register_bit_width = 8;
724 addr->register_bit_offset = 0;
725 addr->address = resource->data.io.min_base_address;
727 return AE_OK;
731 static int
732 acpi_ec_start (
733 struct acpi_device *device)
735 acpi_status status = AE_OK;
736 struct acpi_ec *ec = NULL;
738 ACPI_FUNCTION_TRACE("acpi_ec_start");
740 if (!device)
741 return_VALUE(-EINVAL);
743 ec = acpi_driver_data(device);
745 if (!ec)
746 return_VALUE(-EINVAL);
749 * Get I/O port addresses. Convert to GAS format.
751 status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
752 acpi_ec_io_ports, ec);
753 if (ACPI_FAILURE(status) || ec->command_addr.register_bit_width == 0) {
754 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error getting I/O port addresses"));
755 return_VALUE(-ENODEV);
758 ec->status_addr = ec->command_addr;
760 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02x, ports=0x%2x,0x%2x\n",
761 (u32) ec->gpe_bit, (u32) ec->command_addr.address,
762 (u32) ec->data_addr.address));
765 * Install GPE handler
767 status = acpi_install_gpe_handler(NULL, ec->gpe_bit,
768 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler, ec);
769 if (ACPI_FAILURE(status)) {
770 return_VALUE(-ENODEV);
772 acpi_set_gpe_type (NULL, ec->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
773 acpi_enable_gpe (NULL, ec->gpe_bit, ACPI_NOT_ISR);
775 status = acpi_install_address_space_handler (ec->handle,
776 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
777 &acpi_ec_space_setup, ec);
778 if (ACPI_FAILURE(status)) {
779 acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
780 return_VALUE(-ENODEV);
783 return_VALUE(AE_OK);
787 static int
788 acpi_ec_stop (
789 struct acpi_device *device,
790 int type)
792 acpi_status status = AE_OK;
793 struct acpi_ec *ec = NULL;
795 ACPI_FUNCTION_TRACE("acpi_ec_stop");
797 if (!device)
798 return_VALUE(-EINVAL);
800 ec = acpi_driver_data(device);
802 status = acpi_remove_address_space_handler(ec->handle,
803 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler);
804 if (ACPI_FAILURE(status))
805 return_VALUE(-ENODEV);
807 status = acpi_remove_gpe_handler(NULL, ec->gpe_bit, &acpi_ec_gpe_handler);
808 if (ACPI_FAILURE(status))
809 return_VALUE(-ENODEV);
811 return_VALUE(0);
814 static acpi_status __init
815 acpi_fake_ecdt_callback (
816 acpi_handle handle,
817 u32 Level,
818 void *context,
819 void **retval)
821 acpi_status status;
823 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
824 acpi_ec_io_ports, ec_ecdt);
825 if (ACPI_FAILURE(status))
826 return status;
827 ec_ecdt->status_addr = ec_ecdt->command_addr;
829 ec_ecdt->uid = -1;
830 acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
832 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->gpe_bit);
833 if (ACPI_FAILURE(status))
834 return status;
835 spin_lock_init(&ec_ecdt->lock);
836 ec_ecdt->global_lock = TRUE;
837 ec_ecdt->handle = handle;
839 printk(KERN_INFO PREFIX "GPE=0x%02x, ports=0x%2x, 0x%2x\n",
840 (u32) ec_ecdt->gpe_bit, (u32) ec_ecdt->command_addr.address,
841 (u32) ec_ecdt->data_addr.address);
843 return AE_CTRL_TERMINATE;
847 * Some BIOS (such as some from Gateway laptops) access EC region very early
848 * such as in BAT0._INI or EC._INI before an EC device is found and
849 * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
850 * required, but if EC regison is accessed early, it is required.
851 * The routine tries to workaround the BIOS bug by pre-scan EC device
852 * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
853 * op region (since _REG isn't invoked yet). The assumption is true for
854 * all systems found.
856 static int __init
857 acpi_ec_fake_ecdt(void)
859 acpi_status status;
860 int ret = 0;
862 printk(KERN_INFO PREFIX "Try to make an fake ECDT\n");
864 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
865 if (!ec_ecdt) {
866 ret = -ENOMEM;
867 goto error;
869 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
871 status = acpi_get_devices (ACPI_EC_HID,
872 acpi_fake_ecdt_callback,
873 NULL,
874 NULL);
875 if (ACPI_FAILURE(status)) {
876 kfree(ec_ecdt);
877 ec_ecdt = NULL;
878 ret = -ENODEV;
879 goto error;
881 return 0;
882 error:
883 printk(KERN_ERR PREFIX "Can't make an fake ECDT\n");
884 return ret;
887 static int __init
888 acpi_ec_get_real_ecdt(void)
890 acpi_status status;
891 struct acpi_table_ecdt *ecdt_ptr;
893 status = acpi_get_firmware_table("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
894 (struct acpi_table_header **) &ecdt_ptr);
895 if (ACPI_FAILURE(status))
896 return -ENODEV;
898 printk(KERN_INFO PREFIX "Found ECDT\n");
901 * Generate a temporary ec context to use until the namespace is scanned
903 ec_ecdt = kmalloc(sizeof(struct acpi_ec), GFP_KERNEL);
904 if (!ec_ecdt)
905 return -ENOMEM;
906 memset(ec_ecdt, 0, sizeof(struct acpi_ec));
908 ec_ecdt->command_addr = ecdt_ptr->ec_control;
909 ec_ecdt->status_addr = ecdt_ptr->ec_control;
910 ec_ecdt->data_addr = ecdt_ptr->ec_data;
911 ec_ecdt->gpe_bit = ecdt_ptr->gpe_bit;
912 spin_lock_init(&ec_ecdt->lock);
913 /* use the GL just to be safe */
914 ec_ecdt->global_lock = TRUE;
915 ec_ecdt->uid = ecdt_ptr->uid;
917 status = acpi_get_handle(NULL, ecdt_ptr->ec_id, &ec_ecdt->handle);
918 if (ACPI_FAILURE(status)) {
919 goto error;
922 return 0;
923 error:
924 printk(KERN_ERR PREFIX "Could not use ECDT\n");
925 kfree(ec_ecdt);
926 ec_ecdt = NULL;
928 return -ENODEV;
931 static int __initdata acpi_fake_ecdt_enabled;
932 int __init
933 acpi_ec_ecdt_probe (void)
935 acpi_status status;
936 int ret;
938 ret = acpi_ec_get_real_ecdt();
939 /* Try to make a fake ECDT */
940 if (ret && acpi_fake_ecdt_enabled) {
941 ret = acpi_ec_fake_ecdt();
944 if (ret)
945 return 0;
948 * Install GPE handler
950 status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe_bit,
951 ACPI_GPE_EDGE_TRIGGERED, &acpi_ec_gpe_handler,
952 ec_ecdt);
953 if (ACPI_FAILURE(status)) {
954 goto error;
956 acpi_set_gpe_type (NULL, ec_ecdt->gpe_bit, ACPI_GPE_TYPE_RUNTIME);
957 acpi_enable_gpe (NULL, ec_ecdt->gpe_bit, ACPI_NOT_ISR);
959 status = acpi_install_address_space_handler (ACPI_ROOT_OBJECT,
960 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler,
961 &acpi_ec_space_setup, ec_ecdt);
962 if (ACPI_FAILURE(status)) {
963 acpi_remove_gpe_handler(NULL, ec_ecdt->gpe_bit,
964 &acpi_ec_gpe_handler);
965 goto error;
968 return 0;
970 error:
971 printk(KERN_ERR PREFIX "Could not use ECDT\n");
972 kfree(ec_ecdt);
973 ec_ecdt = NULL;
975 return -ENODEV;
979 static int __init acpi_ec_init (void)
981 int result = 0;
983 ACPI_FUNCTION_TRACE("acpi_ec_init");
985 if (acpi_disabled)
986 return_VALUE(0);
988 acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
989 if (!acpi_ec_dir)
990 return_VALUE(-ENODEV);
992 /* Now register the driver for the EC */
993 result = acpi_bus_register_driver(&acpi_ec_driver);
994 if (result < 0) {
995 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
996 return_VALUE(-ENODEV);
999 return_VALUE(result);
1002 subsys_initcall(acpi_ec_init);
1004 /* EC driver currently not unloadable */
1005 #if 0
1006 static void __exit
1007 acpi_ec_exit (void)
1009 ACPI_FUNCTION_TRACE("acpi_ec_exit");
1011 acpi_bus_unregister_driver(&acpi_ec_driver);
1013 remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1015 return_VOID;
1017 #endif /* 0 */
1019 static int __init acpi_fake_ecdt_setup(char *str)
1021 acpi_fake_ecdt_enabled = 1;
1022 return 0;
1024 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);