xref: /linux/drivers/acpi/ec.c (revision eb2bce7f5e7ac1ca6da434461217fadf3c688d2c)
1 /*
2  *  ec.c - ACPI Embedded Controller Driver (v2.0)
3  *
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>
9  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  *
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.
16  *
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.
21  *
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.
25  *
26  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27  */
28 
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 <asm/io.h>
38 #include <acpi/acpi_bus.h>
39 #include <acpi/acpi_drivers.h>
40 #include <acpi/actypes.h>
41 
42 #define _COMPONENT		ACPI_EC_COMPONENT
43 ACPI_MODULE_NAME("ec");
44 #define ACPI_EC_COMPONENT		0x00100000
45 #define ACPI_EC_CLASS			"embedded_controller"
46 #define ACPI_EC_HID			"PNP0C09"
47 #define ACPI_EC_DEVICE_NAME		"Embedded Controller"
48 #define ACPI_EC_FILE_INFO		"info"
49 #undef PREFIX
50 #define PREFIX				"ACPI: EC: "
51 /* EC status register */
52 #define ACPI_EC_FLAG_OBF	0x01	/* Output buffer full */
53 #define ACPI_EC_FLAG_IBF	0x02	/* Input buffer full */
54 #define ACPI_EC_FLAG_BURST	0x10	/* burst mode */
55 #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,
63 };
64 /* EC events */
65 enum ec_event {
66 	ACPI_EC_EVENT_OBF_1 = 1,	/* Output buffer full */
67 	ACPI_EC_EVENT_IBF_0,	/* Input buffer empty */
68 };
69 
70 #define ACPI_EC_DELAY		500	/* Wait 500ms max. during EC ops */
71 #define ACPI_EC_UDELAY_GLK	1000	/* Wait 1ms max. to get global lock */
72 
73 static enum ec_mode {
74 	EC_INTR = 1,		/* Output buffer full */
75 	EC_POLL,		/* Input buffer empty */
76 } acpi_ec_mode = EC_INTR;
77 
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);
82 
83 static struct acpi_driver acpi_ec_driver = {
84 	.name = "ec",
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,
92 		},
93 };
94 
95 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
96 /* External interfaces use first EC only, so remember */
97 static struct acpi_ec {
98 	acpi_handle handle;
99 	unsigned long gpe;
100 	unsigned long command_addr;
101 	unsigned long data_addr;
102 	unsigned long global_lock;
103 	struct mutex lock;
104 	atomic_t query_pending;
105 	atomic_t event_count;
106 	wait_queue_head_t wait;
107 } *boot_ec, *first_ec;
108 
109 /* --------------------------------------------------------------------------
110                              Transaction Management
111    -------------------------------------------------------------------------- */
112 
113 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
114 {
115 	return inb(ec->command_addr);
116 }
117 
118 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
119 {
120 	return inb(ec->data_addr);
121 }
122 
123 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
124 {
125 	outb(command, ec->command_addr);
126 }
127 
128 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
129 {
130 	outb(data, ec->data_addr);
131 }
132 
133 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
134 				       unsigned old_count)
135 {
136 	u8 status = acpi_ec_read_status(ec);
137 	if (old_count == atomic_read(&ec->event_count))
138 		return 0;
139 	if (event == ACPI_EC_EVENT_OBF_1) {
140 		if (status & ACPI_EC_FLAG_OBF)
141 			return 1;
142 	} else if (event == ACPI_EC_EVENT_IBF_0) {
143 		if (!(status & ACPI_EC_FLAG_IBF))
144 			return 1;
145 	}
146 
147 	return 0;
148 }
149 
150 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, unsigned count)
151 {
152 	if (acpi_ec_mode == EC_POLL) {
153 		unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
154 		while (time_before(jiffies, delay)) {
155 			if (acpi_ec_check_status(ec, event, 0))
156 				return 0;
157 		}
158 	} else {
159 		if (wait_event_timeout(ec->wait,
160 				       acpi_ec_check_status(ec, event, count),
161 				       msecs_to_jiffies(ACPI_EC_DELAY)) ||
162 		    acpi_ec_check_status(ec, event, 0)) {
163 			return 0;
164 		} else {
165 			printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
166 			       " status = %d, expect_event = %d\n",
167 			       acpi_ec_read_status(ec), event);
168 		}
169 	}
170 
171 	return -ETIME;
172 }
173 
174 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
175 					const u8 * wdata, unsigned wdata_len,
176 					u8 * rdata, unsigned rdata_len)
177 {
178 	int result = 0;
179 	unsigned count = atomic_read(&ec->event_count);
180 	acpi_ec_write_cmd(ec, command);
181 
182 	for (; wdata_len > 0; --wdata_len) {
183 		result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count);
184 		if (result) {
185 			printk(KERN_ERR PREFIX
186 			       "write_cmd timeout, command = %d\n", command);
187 			goto end;
188 		}
189 		count = atomic_read(&ec->event_count);
190 		acpi_ec_write_data(ec, *(wdata++));
191 	}
192 
193 	if (!rdata_len) {
194 		result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count);
195 		if (result) {
196 			printk(KERN_ERR PREFIX
197 			       "finish-write timeout, command = %d\n", command);
198 			goto end;
199 		}
200 	} else if (command == ACPI_EC_COMMAND_QUERY) {
201 		atomic_set(&ec->query_pending, 0);
202 	}
203 
204 	for (; rdata_len > 0; --rdata_len) {
205 		result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count);
206 		if (result) {
207 			printk(KERN_ERR PREFIX "read timeout, command = %d\n",
208 			       command);
209 			goto end;
210 		}
211 		count = atomic_read(&ec->event_count);
212 		*(rdata++) = acpi_ec_read_data(ec);
213 	}
214       end:
215 	return result;
216 }
217 
218 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
219 			       const u8 * wdata, unsigned wdata_len,
220 			       u8 * rdata, unsigned rdata_len)
221 {
222 	int status;
223 	u32 glk;
224 
225 	if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
226 		return -EINVAL;
227 
228 	if (rdata)
229 		memset(rdata, 0, rdata_len);
230 
231 	mutex_lock(&ec->lock);
232 	if (ec->global_lock) {
233 		status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
234 		if (ACPI_FAILURE(status)) {
235 			mutex_unlock(&ec->lock);
236 			return -ENODEV;
237 		}
238 	}
239 
240 	/* Make sure GPE is enabled before doing transaction */
241 	acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
242 
243 	status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
244 	if (status) {
245 		printk(KERN_DEBUG PREFIX
246 		       "input buffer is not empty, aborting transaction\n");
247 		goto end;
248 	}
249 
250 	status = acpi_ec_transaction_unlocked(ec, command,
251 					      wdata, wdata_len,
252 					      rdata, rdata_len);
253 
254       end:
255 
256 	if (ec->global_lock)
257 		acpi_release_global_lock(glk);
258 	mutex_unlock(&ec->lock);
259 
260 	return status;
261 }
262 
263 /*
264  * Note: samsung nv5000 doesn't work with ec burst mode.
265  * http://bugzilla.kernel.org/show_bug.cgi?id=4980
266  */
267 int acpi_ec_burst_enable(struct acpi_ec *ec)
268 {
269 	u8 d;
270 	return acpi_ec_transaction(ec, ACPI_EC_BURST_ENABLE, NULL, 0, &d, 1);
271 }
272 
273 int acpi_ec_burst_disable(struct acpi_ec *ec)
274 {
275 	return acpi_ec_transaction(ec, ACPI_EC_BURST_DISABLE, NULL, 0, NULL, 0);
276 }
277 
278 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
279 {
280 	int result;
281 	u8 d;
282 
283 	result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
284 				     &address, 1, &d, 1);
285 	*data = d;
286 	return result;
287 }
288 
289 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
290 {
291 	u8 wdata[2] = { address, data };
292 	return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
293 				   wdata, 2, NULL, 0);
294 }
295 
296 /*
297  * Externally callable EC access functions. For now, assume 1 EC only
298  */
299 int ec_burst_enable(void)
300 {
301 	if (!first_ec)
302 		return -ENODEV;
303 	return acpi_ec_burst_enable(first_ec);
304 }
305 
306 EXPORT_SYMBOL(ec_burst_enable);
307 
308 int ec_burst_disable(void)
309 {
310 	if (!first_ec)
311 		return -ENODEV;
312 	return acpi_ec_burst_disable(first_ec);
313 }
314 
315 EXPORT_SYMBOL(ec_burst_disable);
316 
317 int ec_read(u8 addr, u8 * val)
318 {
319 	int err;
320 	u8 temp_data;
321 
322 	if (!first_ec)
323 		return -ENODEV;
324 
325 	err = acpi_ec_read(first_ec, addr, &temp_data);
326 
327 	if (!err) {
328 		*val = temp_data;
329 		return 0;
330 	} else
331 		return err;
332 }
333 
334 EXPORT_SYMBOL(ec_read);
335 
336 int ec_write(u8 addr, u8 val)
337 {
338 	int err;
339 
340 	if (!first_ec)
341 		return -ENODEV;
342 
343 	err = acpi_ec_write(first_ec, addr, val);
344 
345 	return err;
346 }
347 
348 EXPORT_SYMBOL(ec_write);
349 
350 int ec_transaction(u8 command,
351 		   const u8 * wdata, unsigned wdata_len,
352 		   u8 * rdata, unsigned rdata_len)
353 {
354 	if (!first_ec)
355 		return -ENODEV;
356 
357 	return acpi_ec_transaction(first_ec, command, wdata,
358 				   wdata_len, rdata, rdata_len);
359 }
360 
361 EXPORT_SYMBOL(ec_transaction);
362 
363 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
364 {
365 	int result;
366 	u8 d;
367 
368 	if (!ec || !data)
369 		return -EINVAL;
370 
371 	/*
372 	 * Query the EC to find out which _Qxx method we need to evaluate.
373 	 * Note that successful completion of the query causes the ACPI_EC_SCI
374 	 * bit to be cleared (and thus clearing the interrupt source).
375 	 */
376 
377 	result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
378 	if (result)
379 		return result;
380 
381 	if (!d)
382 		return -ENODATA;
383 
384 	*data = d;
385 	return 0;
386 }
387 
388 /* --------------------------------------------------------------------------
389                                 Event Management
390    -------------------------------------------------------------------------- */
391 
392 static void acpi_ec_gpe_query(void *ec_cxt)
393 {
394 	struct acpi_ec *ec = ec_cxt;
395 	u8 value = 0;
396 	char object_name[8];
397 
398 	if (!ec || acpi_ec_query(ec, &value))
399 		return;
400 
401 	snprintf(object_name, 8, "_Q%2.2X", value);
402 
403 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
404 
405 	acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
406 }
407 
408 static u32 acpi_ec_gpe_handler(void *data)
409 {
410 	acpi_status status = AE_OK;
411 	u8 value;
412 	struct acpi_ec *ec = data;
413 	atomic_inc(&ec->event_count);
414 
415 	if (acpi_ec_mode == EC_INTR) {
416 		wake_up(&ec->wait);
417 	}
418 
419 	value = acpi_ec_read_status(ec);
420 	if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
421 		atomic_set(&ec->query_pending, 1);
422 		status =
423 		    acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
424 				    ec);
425 	}
426 
427 	return status == AE_OK ?
428 	    ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
429 }
430 
431 /* --------------------------------------------------------------------------
432                              Address Space Management
433    -------------------------------------------------------------------------- */
434 
435 static acpi_status
436 acpi_ec_space_setup(acpi_handle region_handle,
437 		    u32 function, void *handler_context, void **return_context)
438 {
439 	/*
440 	 * The EC object is in the handler context and is needed
441 	 * when calling the acpi_ec_space_handler.
442 	 */
443 	*return_context = (function != ACPI_REGION_DEACTIVATE) ?
444 	    handler_context : NULL;
445 
446 	return AE_OK;
447 }
448 
449 static acpi_status
450 acpi_ec_space_handler(u32 function,
451 		      acpi_physical_address address,
452 		      u32 bit_width,
453 		      acpi_integer * value,
454 		      void *handler_context, void *region_context)
455 {
456 	int result = 0;
457 	struct acpi_ec *ec = handler_context;
458 	u64 temp = *value;
459 	acpi_integer f_v = 0;
460 	int i = 0;
461 
462 	if ((address > 0xFF) || !value || !handler_context)
463 		return AE_BAD_PARAMETER;
464 
465 	if (bit_width != 8 && acpi_strict) {
466 		return AE_BAD_PARAMETER;
467 	}
468 
469       next_byte:
470 	switch (function) {
471 	case ACPI_READ:
472 		temp = 0;
473 		result = acpi_ec_read(ec, (u8) address, (u8 *) & temp);
474 		break;
475 	case ACPI_WRITE:
476 		result = acpi_ec_write(ec, (u8) address, (u8) temp);
477 		break;
478 	default:
479 		result = -EINVAL;
480 		goto out;
481 		break;
482 	}
483 
484 	bit_width -= 8;
485 	if (bit_width) {
486 		if (function == ACPI_READ)
487 			f_v |= temp << 8 * i;
488 		if (function == ACPI_WRITE)
489 			temp >>= 8;
490 		i++;
491 		address++;
492 		goto next_byte;
493 	}
494 
495 	if (function == ACPI_READ) {
496 		f_v |= temp << 8 * i;
497 		*value = f_v;
498 	}
499 
500       out:
501 	switch (result) {
502 	case -EINVAL:
503 		return AE_BAD_PARAMETER;
504 		break;
505 	case -ENODEV:
506 		return AE_NOT_FOUND;
507 		break;
508 	case -ETIME:
509 		return AE_TIME;
510 		break;
511 	default:
512 		return AE_OK;
513 	}
514 }
515 
516 /* --------------------------------------------------------------------------
517                               FS Interface (/proc)
518    -------------------------------------------------------------------------- */
519 
520 static struct proc_dir_entry *acpi_ec_dir;
521 
522 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
523 {
524 	struct acpi_ec *ec = seq->private;
525 
526 	if (!ec)
527 		goto end;
528 
529 	seq_printf(seq, "gpe:\t\t\t0x%02x\n", (u32) ec->gpe);
530 	seq_printf(seq, "ports:\t\t\t0x%02x, 0x%02x\n",
531 		   (unsigned)ec->command_addr, (unsigned)ec->data_addr);
532 	seq_printf(seq, "use global lock:\t%s\n",
533 		   ec->global_lock ? "yes" : "no");
534       end:
535 	return 0;
536 }
537 
538 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
539 {
540 	return single_open(file, acpi_ec_read_info, PDE(inode)->data);
541 }
542 
543 static struct file_operations acpi_ec_info_ops = {
544 	.open = acpi_ec_info_open_fs,
545 	.read = seq_read,
546 	.llseek = seq_lseek,
547 	.release = single_release,
548 	.owner = THIS_MODULE,
549 };
550 
551 static int acpi_ec_add_fs(struct acpi_device *device)
552 {
553 	struct proc_dir_entry *entry = NULL;
554 
555 	if (!acpi_device_dir(device)) {
556 		acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
557 						     acpi_ec_dir);
558 		if (!acpi_device_dir(device))
559 			return -ENODEV;
560 	}
561 
562 	entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
563 				  acpi_device_dir(device));
564 	if (!entry)
565 		return -ENODEV;
566 	else {
567 		entry->proc_fops = &acpi_ec_info_ops;
568 		entry->data = acpi_driver_data(device);
569 		entry->owner = THIS_MODULE;
570 	}
571 
572 	return 0;
573 }
574 
575 static int acpi_ec_remove_fs(struct acpi_device *device)
576 {
577 
578 	if (acpi_device_dir(device)) {
579 		remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
580 		remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
581 		acpi_device_dir(device) = NULL;
582 	}
583 
584 	return 0;
585 }
586 
587 /* --------------------------------------------------------------------------
588                                Driver Interface
589    -------------------------------------------------------------------------- */
590 static acpi_status
591 ec_parse_io_ports(struct acpi_resource *resource, void *context);
592 
593 static acpi_status
594 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval);
595 
596 static struct acpi_ec *make_acpi_ec(void)
597 {
598 	struct acpi_ec *ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
599 	if (!ec)
600 		return NULL;
601 
602 	atomic_set(&ec->query_pending, 1);
603 	atomic_set(&ec->event_count, 1);
604 	mutex_init(&ec->lock);
605 	init_waitqueue_head(&ec->wait);
606 
607 	return ec;
608 }
609 
610 static int acpi_ec_add(struct acpi_device *device)
611 {
612 	acpi_status status = AE_OK;
613 	struct acpi_ec *ec = NULL;
614 
615 	if (!device)
616 		return -EINVAL;
617 
618 	strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
619 	strcpy(acpi_device_class(device), ACPI_EC_CLASS);
620 
621 	ec = make_acpi_ec();
622 	if (!ec)
623 		return -ENOMEM;
624 
625 	status = ec_parse_device(device->handle, 0, ec, NULL);
626 	if (status != AE_CTRL_TERMINATE) {
627 		kfree(ec);
628 		return -EINVAL;
629 	}
630 
631 	/* Check if we found the boot EC */
632 	if (boot_ec) {
633 		if (boot_ec->gpe == ec->gpe) {
634 			/* We might have incorrect info for GL at boot time */
635 			mutex_lock(&boot_ec->lock);
636 			boot_ec->global_lock = ec->global_lock;
637 			mutex_unlock(&boot_ec->lock);
638 			kfree(ec);
639 			ec = boot_ec;
640 		}
641 	} else
642 		first_ec = ec;
643 	ec->handle = device->handle;
644 	acpi_driver_data(device) = ec;
645 
646 	acpi_ec_add_fs(device);
647 
648 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
649 			  acpi_device_name(device), acpi_device_bid(device),
650 			  (u32) ec->gpe));
651 
652 	return 0;
653 }
654 
655 static int acpi_ec_remove(struct acpi_device *device, int type)
656 {
657 	struct acpi_ec *ec;
658 
659 	if (!device)
660 		return -EINVAL;
661 
662 	ec = acpi_driver_data(device);
663 	acpi_ec_remove_fs(device);
664 	acpi_driver_data(device) = NULL;
665 	if (ec == first_ec)
666 		first_ec = NULL;
667 
668 	/* Don't touch boot EC */
669 	if (boot_ec != ec)
670 		kfree(ec);
671 	return 0;
672 }
673 
674 static acpi_status
675 ec_parse_io_ports(struct acpi_resource *resource, void *context)
676 {
677 	struct acpi_ec *ec = context;
678 
679 	if (resource->type != ACPI_RESOURCE_TYPE_IO)
680 		return AE_OK;
681 
682 	/*
683 	 * The first address region returned is the data port, and
684 	 * the second address region returned is the status/command
685 	 * port.
686 	 */
687 	if (ec->data_addr == 0)
688 		ec->data_addr = resource->data.io.minimum;
689 	else if (ec->command_addr == 0)
690 		ec->command_addr = resource->data.io.minimum;
691 	else
692 		return AE_CTRL_TERMINATE;
693 
694 	return AE_OK;
695 }
696 
697 static int ec_install_handlers(struct acpi_ec *ec)
698 {
699 	acpi_status status;
700 	status = acpi_install_gpe_handler(NULL, ec->gpe,
701 					  ACPI_GPE_EDGE_TRIGGERED,
702 					  &acpi_ec_gpe_handler, ec);
703 	if (ACPI_FAILURE(status))
704 		return -ENODEV;
705 
706 	acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
707 	acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
708 
709 	status = acpi_install_address_space_handler(ec->handle,
710 						    ACPI_ADR_SPACE_EC,
711 						    &acpi_ec_space_handler,
712 						    &acpi_ec_space_setup, ec);
713 	if (ACPI_FAILURE(status)) {
714 		acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
715 		return -ENODEV;
716 	}
717 
718 	/* EC is fully operational, allow queries */
719 	atomic_set(&ec->query_pending, 0);
720 
721 	return 0;
722 }
723 
724 static int acpi_ec_start(struct acpi_device *device)
725 {
726 	struct acpi_ec *ec;
727 
728 	if (!device)
729 		return -EINVAL;
730 
731 	ec = acpi_driver_data(device);
732 
733 	if (!ec)
734 		return -EINVAL;
735 
736 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
737 			  ec->gpe, ec->command_addr, ec->data_addr));
738 
739 	/* Boot EC is already working */
740 	if (ec == boot_ec)
741 		return 0;
742 
743 	return ec_install_handlers(ec);
744 }
745 
746 static int acpi_ec_stop(struct acpi_device *device, int type)
747 {
748 	acpi_status status;
749 	struct acpi_ec *ec;
750 
751 	if (!device)
752 		return -EINVAL;
753 
754 	ec = acpi_driver_data(device);
755 	if (!ec)
756 		return -EINVAL;
757 
758 	/* Don't touch boot EC */
759 	if (ec == boot_ec)
760 		return 0;
761 
762 	status = acpi_remove_address_space_handler(ec->handle,
763 						   ACPI_ADR_SPACE_EC,
764 						   &acpi_ec_space_handler);
765 	if (ACPI_FAILURE(status))
766 		return -ENODEV;
767 
768 	status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
769 	if (ACPI_FAILURE(status))
770 		return -ENODEV;
771 
772 	return 0;
773 }
774 
775 static acpi_status
776 ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
777 {
778 	acpi_status status;
779 
780 	struct acpi_ec *ec = context;
781 	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
782 				     ec_parse_io_ports, ec);
783 	if (ACPI_FAILURE(status))
784 		return status;
785 
786 	/* Get GPE bit assignment (EC events). */
787 	/* TODO: Add support for _GPE returning a package */
788 	status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
789 	if (ACPI_FAILURE(status))
790 		return status;
791 
792 	/* Use the global lock for all EC transactions? */
793 	acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
794 
795 	ec->handle = handle;
796 
797 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
798 			  ec->gpe, ec->command_addr, ec->data_addr));
799 
800 	return AE_CTRL_TERMINATE;
801 }
802 
803 int __init acpi_ec_ecdt_probe(void)
804 {
805 	int ret;
806 	acpi_status status;
807 	struct acpi_table_ecdt *ecdt_ptr;
808 
809 	boot_ec = make_acpi_ec();
810 	if (!boot_ec)
811 		return -ENOMEM;
812 	/*
813 	 * Generate a boot ec context
814 	 */
815 
816 	status = acpi_get_table(ACPI_SIG_ECDT, 1,
817 				(struct acpi_table_header **)&ecdt_ptr);
818 	if (ACPI_FAILURE(status))
819 		goto error;
820 
821 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
822 
823 	boot_ec->command_addr = ecdt_ptr->control.address;
824 	boot_ec->data_addr = ecdt_ptr->data.address;
825 	boot_ec->gpe = ecdt_ptr->gpe;
826 	boot_ec->handle = ACPI_ROOT_OBJECT;
827 
828 	ret = ec_install_handlers(boot_ec);
829 	if (!ret) {
830 		first_ec = boot_ec;
831 		return 0;
832 	}
833       error:
834 	kfree(boot_ec);
835 	boot_ec = NULL;
836 
837 	return -ENODEV;
838 }
839 
840 static int __init acpi_ec_init(void)
841 {
842 	int result = 0;
843 
844 	if (acpi_disabled)
845 		return 0;
846 
847 	acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
848 	if (!acpi_ec_dir)
849 		return -ENODEV;
850 
851 	/* Now register the driver for the EC */
852 	result = acpi_bus_register_driver(&acpi_ec_driver);
853 	if (result < 0) {
854 		remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
855 		return -ENODEV;
856 	}
857 
858 	return result;
859 }
860 
861 subsys_initcall(acpi_ec_init);
862 
863 /* EC driver currently not unloadable */
864 #if 0
865 static void __exit acpi_ec_exit(void)
866 {
867 
868 	acpi_bus_unregister_driver(&acpi_ec_driver);
869 
870 	remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
871 
872 	return;
873 }
874 #endif				/* 0 */
875 
876 static int __init acpi_ec_set_intr_mode(char *str)
877 {
878 	int intr;
879 
880 	if (!get_option(&str, &intr))
881 		return 0;
882 
883 	acpi_ec_mode = (intr) ? EC_INTR : EC_POLL;
884 
885 	printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
886 
887 	return 1;
888 }
889 
890 __setup("ec_intr=", acpi_ec_set_intr_mode);
891