xref: /linux/drivers/acpi/ec.c (revision c537b994505099b7197e7d3125b942ecbcc51eb6)
1 /*
2  *  acpi_ec.c - ACPI Embedded Controller Driver ($Revision: 38 $)
3  *
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>
7  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
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.
14  *
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.
19  *
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.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  */
26 
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>
39 
40 #define _COMPONENT		ACPI_EC_COMPONENT
41 ACPI_MODULE_NAME("ec");
42 #define ACPI_EC_COMPONENT		0x00100000
43 #define ACPI_EC_CLASS			"embedded_controller"
44 #define ACPI_EC_HID			"PNP0C09"
45 #define ACPI_EC_DEVICE_NAME		"Embedded Controller"
46 #define ACPI_EC_FILE_INFO		"info"
47 #undef PREFIX
48 #define PREFIX				"ACPI: EC: "
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 */
54 /* EC commands */
55 enum ec_command {
56 	ACPI_EC_COMMAND_READ = 0x80,
57 	ACPI_EC_COMMAND_WRITE = 0x81,
58 	ACPI_EC_BURST_ENABLE = 0x82,
59 	ACPI_EC_BURST_DISABLE = 0x83,
60 	ACPI_EC_COMMAND_QUERY = 0x84,
61 };
62 /* EC events */
63 enum ec_event {
64 	ACPI_EC_EVENT_OBF_1 = 1,	/* Output buffer full */
65 	ACPI_EC_EVENT_IBF_0,	/* Input buffer empty */
66 };
67 
68 #define ACPI_EC_DELAY		500	/* Wait 500ms max. during EC ops */
69 #define ACPI_EC_UDELAY_GLK	1000	/* Wait 1ms max. to get global lock */
70 
71 static enum ec_mode {
72 	EC_INTR = 1,		/* Output buffer full */
73 	EC_POLL,		/* Input buffer empty */
74 } acpi_ec_mode = EC_INTR;
75 
76 static int acpi_ec_remove(struct acpi_device *device, int type);
77 static int acpi_ec_start(struct acpi_device *device);
78 static int acpi_ec_stop(struct acpi_device *device, int type);
79 static int acpi_ec_add(struct acpi_device *device);
80 
81 static struct acpi_driver acpi_ec_driver = {
82 	.name = "ec",
83 	.class = ACPI_EC_CLASS,
84 	.ids = ACPI_EC_HID,
85 	.ops = {
86 		.add = acpi_ec_add,
87 		.remove = acpi_ec_remove,
88 		.start = acpi_ec_start,
89 		.stop = acpi_ec_stop,
90 		},
91 };
92 
93 /* If we find an EC via the ECDT, we need to keep a ptr to its context */
94 static struct acpi_ec {
95 	acpi_handle handle;
96 	unsigned long uid;
97 	unsigned long gpe;
98 	unsigned long command_addr;
99 	unsigned long data_addr;
100 	unsigned long global_lock;
101 	struct mutex lock;
102 	atomic_t query_pending;
103 	atomic_t event_count;
104 	atomic_t leaving_burst;	/* 0 : No, 1 : Yes, 2: abort */
105 	wait_queue_head_t wait;
106 } *ec_ecdt;
107 
108 /* External interfaces use first EC only, so remember */
109 static struct acpi_device *first_ec;
110 
111 /* --------------------------------------------------------------------------
112                              Transaction Management
113    -------------------------------------------------------------------------- */
114 
115 static inline u8 acpi_ec_read_status(struct acpi_ec *ec)
116 {
117 	return inb(ec->command_addr);
118 }
119 
120 static inline u8 acpi_ec_read_data(struct acpi_ec *ec)
121 {
122 	return inb(ec->data_addr);
123 }
124 
125 static inline void acpi_ec_write_cmd(struct acpi_ec *ec, u8 command)
126 {
127 	outb(command, ec->command_addr);
128 }
129 
130 static inline void acpi_ec_write_data(struct acpi_ec *ec, u8 data)
131 {
132 	outb(data, ec->data_addr);
133 }
134 
135 static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event,
136 				       unsigned old_count)
137 {
138 	u8 status = acpi_ec_read_status(ec);
139 	if (old_count == atomic_read(&ec->event_count))
140 		return 0;
141 	if (event == ACPI_EC_EVENT_OBF_1) {
142 		if (status & ACPI_EC_FLAG_OBF)
143 			return 1;
144 	} else if (event == ACPI_EC_EVENT_IBF_0) {
145 		if (!(status & ACPI_EC_FLAG_IBF))
146 			return 1;
147 	}
148 
149 	return 0;
150 }
151 
152 static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, unsigned count)
153 {
154 	if (acpi_ec_mode == EC_POLL) {
155 		unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
156 		while (time_before(jiffies, delay)) {
157 			if (acpi_ec_check_status(ec, event, 0))
158 				return 0;
159 		}
160 	} else {
161 		if (wait_event_timeout(ec->wait,
162 				       acpi_ec_check_status(ec, event, count),
163 				       msecs_to_jiffies(ACPI_EC_DELAY)) ||
164 		    acpi_ec_check_status(ec, event, 0)) {
165 			return 0;
166 		} else {
167 			printk(KERN_ERR PREFIX "acpi_ec_wait timeout,"
168 			       " status = %d, expect_event = %d\n",
169 			       acpi_ec_read_status(ec), event);
170 		}
171 	}
172 
173 	return -ETIME;
174 }
175 
176 #ifdef ACPI_FUTURE_USAGE
177 /*
178  * Note: samsung nv5000 doesn't work with ec burst mode.
179  * http://bugzilla.kernel.org/show_bug.cgi?id=4980
180  */
181 int acpi_ec_enter_burst_mode(struct acpi_ec *ec)
182 {
183 	u8 tmp = 0;
184 	u8 status = 0;
185 
186 	status = acpi_ec_read_status(ec);
187 	if (status != -EINVAL && !(status & ACPI_EC_FLAG_BURST)) {
188 		status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
189 		if (status)
190 			goto end;
191 		acpi_ec_write_cmd(ec, ACPI_EC_BURST_ENABLE);
192 		status = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1);
193 		tmp = acpi_ec_read_data(ec);
194 		if (tmp != 0x90) {	/* Burst ACK byte */
195 			return -EINVAL;
196 		}
197 	}
198 
199 	atomic_set(&ec->leaving_burst, 0);
200 	return 0;
201       end:
202 	ACPI_EXCEPTION((AE_INFO, status, "EC wait, burst mode"));
203 	return -1;
204 }
205 
206 int acpi_ec_leave_burst_mode(struct acpi_ec *ec)
207 {
208 	u8 status = 0;
209 
210 	status = acpi_ec_read_status(ec);
211 	if (status != -EINVAL && (status & ACPI_EC_FLAG_BURST)) {
212 		status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
213 		if (status)
214 			goto end;
215 		acpi_ec_write_cmd(ec, ACPI_EC_BURST_DISABLE);
216 		acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0);
217 	}
218 	atomic_set(&ec->leaving_burst, 1);
219 	return 0;
220       end:
221 	ACPI_EXCEPTION((AE_INFO, status, "EC leave burst mode"));
222 	return -1;
223 }
224 #endif				/* ACPI_FUTURE_USAGE */
225 
226 static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
227 					const u8 * wdata, unsigned wdata_len,
228 					u8 * rdata, unsigned rdata_len)
229 {
230 	int result = 0;
231 	unsigned count = atomic_read(&ec->event_count);
232 	acpi_ec_write_cmd(ec, command);
233 
234 	for (; wdata_len > 0; --wdata_len) {
235 		result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count);
236 		if (result) {
237 			printk(KERN_ERR PREFIX
238 			       "write_cmd timeout, command = %d\n", command);
239 			goto end;
240 		}
241 		count = atomic_read(&ec->event_count);
242 		acpi_ec_write_data(ec, *(wdata++));
243 	}
244 
245 	if (!rdata_len) {
246 		result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, count);
247 		if (result) {
248 			printk(KERN_ERR PREFIX
249 			       "finish-write timeout, command = %d\n", command);
250 			goto end;
251 		}
252 	} else if (command == ACPI_EC_COMMAND_QUERY) {
253 		atomic_set(&ec->query_pending, 0);
254 	}
255 
256 	for (; rdata_len > 0; --rdata_len) {
257 		result = acpi_ec_wait(ec, ACPI_EC_EVENT_OBF_1, count);
258 		if (result) {
259 			printk(KERN_ERR PREFIX "read timeout, command = %d\n",
260 			       command);
261 			goto end;
262 		}
263 		count = atomic_read(&ec->event_count);
264 		*(rdata++) = acpi_ec_read_data(ec);
265 	}
266       end:
267 	return result;
268 }
269 
270 static int acpi_ec_transaction(struct acpi_ec *ec, u8 command,
271 			       const u8 * wdata, unsigned wdata_len,
272 			       u8 * rdata, unsigned rdata_len)
273 {
274 	int status;
275 	u32 glk;
276 
277 	if (!ec || (wdata_len && !wdata) || (rdata_len && !rdata))
278 		return -EINVAL;
279 
280 	if (rdata)
281 		memset(rdata, 0, rdata_len);
282 
283 	mutex_lock(&ec->lock);
284 	if (ec->global_lock) {
285 		status = acpi_acquire_global_lock(ACPI_EC_UDELAY_GLK, &glk);
286 		if (ACPI_FAILURE(status)) {
287 			mutex_unlock(&ec->lock);
288 			return -ENODEV;
289 		}
290 	}
291 
292 	/* Make sure GPE is enabled before doing transaction */
293 	acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
294 
295 	status = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, 0);
296 	if (status) {
297 		printk(KERN_DEBUG PREFIX
298 		       "input buffer is not empty, aborting transaction\n");
299 		goto end;
300 	}
301 
302 	status = acpi_ec_transaction_unlocked(ec, command,
303 					      wdata, wdata_len,
304 					      rdata, rdata_len);
305 
306       end:
307 
308 	if (ec->global_lock)
309 		acpi_release_global_lock(glk);
310 	mutex_unlock(&ec->lock);
311 
312 	return status;
313 }
314 
315 static int acpi_ec_read(struct acpi_ec *ec, u8 address, u8 * data)
316 {
317 	int result;
318 	u8 d;
319 
320 	result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_READ,
321 				     &address, 1, &d, 1);
322 	*data = d;
323 	return result;
324 }
325 
326 static int acpi_ec_write(struct acpi_ec *ec, u8 address, u8 data)
327 {
328 	u8 wdata[2] = { address, data };
329 	return acpi_ec_transaction(ec, ACPI_EC_COMMAND_WRITE,
330 				   wdata, 2, NULL, 0);
331 }
332 
333 /*
334  * Externally callable EC access functions. For now, assume 1 EC only
335  */
336 int ec_read(u8 addr, u8 * val)
337 {
338 	struct acpi_ec *ec;
339 	int err;
340 	u8 temp_data;
341 
342 	if (!first_ec)
343 		return -ENODEV;
344 
345 	ec = acpi_driver_data(first_ec);
346 
347 	err = acpi_ec_read(ec, addr, &temp_data);
348 
349 	if (!err) {
350 		*val = temp_data;
351 		return 0;
352 	} else
353 		return err;
354 }
355 
356 EXPORT_SYMBOL(ec_read);
357 
358 int ec_write(u8 addr, u8 val)
359 {
360 	struct acpi_ec *ec;
361 	int err;
362 
363 	if (!first_ec)
364 		return -ENODEV;
365 
366 	ec = acpi_driver_data(first_ec);
367 
368 	err = acpi_ec_write(ec, addr, val);
369 
370 	return err;
371 }
372 
373 EXPORT_SYMBOL(ec_write);
374 
375 int ec_transaction(u8 command,
376 		   const u8 * wdata, unsigned wdata_len,
377 		   u8 * rdata, unsigned rdata_len)
378 {
379 	struct acpi_ec *ec;
380 
381 	if (!first_ec)
382 		return -ENODEV;
383 
384 	ec = acpi_driver_data(first_ec);
385 
386 	return acpi_ec_transaction(ec, command, wdata,
387 				   wdata_len, rdata, rdata_len);
388 }
389 
390 EXPORT_SYMBOL(ec_transaction);
391 
392 static int acpi_ec_query(struct acpi_ec *ec, u8 * data)
393 {
394 	int result;
395 	u8 d;
396 
397 	if (!ec || !data)
398 		return -EINVAL;
399 
400 	/*
401 	 * Query the EC to find out which _Qxx method we need to evaluate.
402 	 * Note that successful completion of the query causes the ACPI_EC_SCI
403 	 * bit to be cleared (and thus clearing the interrupt source).
404 	 */
405 
406 	result = acpi_ec_transaction(ec, ACPI_EC_COMMAND_QUERY, NULL, 0, &d, 1);
407 	if (result)
408 		return result;
409 
410 	if (!d)
411 		return -ENODATA;
412 
413 	*data = d;
414 	return 0;
415 }
416 
417 /* --------------------------------------------------------------------------
418                                 Event Management
419    -------------------------------------------------------------------------- */
420 
421 static void acpi_ec_gpe_query(void *ec_cxt)
422 {
423 	struct acpi_ec *ec = (struct acpi_ec *)ec_cxt;
424 	u8 value = 0;
425 	char object_name[8];
426 
427 	if (!ec || acpi_ec_query(ec, &value))
428 		return;
429 
430 	snprintf(object_name, 8, "_Q%2.2X", value);
431 
432 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluating %s", object_name));
433 
434 	acpi_evaluate_object(ec->handle, object_name, NULL, NULL);
435 }
436 
437 static u32 acpi_ec_gpe_handler(void *data)
438 {
439 	acpi_status status = AE_OK;
440 	u8 value;
441 	struct acpi_ec *ec = (struct acpi_ec *)data;
442 	atomic_inc(&ec->event_count);
443 	if (acpi_ec_mode == EC_INTR) {
444 		wake_up(&ec->wait);
445 	}
446 
447 	value = acpi_ec_read_status(ec);
448 	if ((value & ACPI_EC_FLAG_SCI) && !atomic_read(&ec->query_pending)) {
449 		atomic_set(&ec->query_pending, 1);
450 		status =
451 		    acpi_os_execute(OSL_EC_BURST_HANDLER, acpi_ec_gpe_query,
452 				    ec);
453 	}
454 
455 	return status == AE_OK ?
456 	    ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
457 }
458 
459 /* --------------------------------------------------------------------------
460                              Address Space Management
461    -------------------------------------------------------------------------- */
462 
463 static acpi_status
464 acpi_ec_space_setup(acpi_handle region_handle,
465 		    u32 function, void *handler_context, void **return_context)
466 {
467 	/*
468 	 * The EC object is in the handler context and is needed
469 	 * when calling the acpi_ec_space_handler.
470 	 */
471 	*return_context = (function != ACPI_REGION_DEACTIVATE) ?
472 	    handler_context : NULL;
473 
474 	return AE_OK;
475 }
476 
477 static acpi_status
478 acpi_ec_space_handler(u32 function,
479 		      acpi_physical_address address,
480 		      u32 bit_width,
481 		      acpi_integer * value,
482 		      void *handler_context, void *region_context)
483 {
484 	int result = 0;
485 	struct acpi_ec *ec = NULL;
486 	u64 temp = *value;
487 	acpi_integer f_v = 0;
488 	int i = 0;
489 
490 	if ((address > 0xFF) || !value || !handler_context)
491 		return AE_BAD_PARAMETER;
492 
493 	if (bit_width != 8 && acpi_strict) {
494 		return AE_BAD_PARAMETER;
495 	}
496 
497 	ec = (struct acpi_ec *)handler_context;
498 
499       next_byte:
500 	switch (function) {
501 	case ACPI_READ:
502 		temp = 0;
503 		result = acpi_ec_read(ec, (u8) address, (u8 *) & temp);
504 		break;
505 	case ACPI_WRITE:
506 		result = acpi_ec_write(ec, (u8) address, (u8) temp);
507 		break;
508 	default:
509 		result = -EINVAL;
510 		goto out;
511 		break;
512 	}
513 
514 	bit_width -= 8;
515 	if (bit_width) {
516 		if (function == ACPI_READ)
517 			f_v |= temp << 8 * i;
518 		if (function == ACPI_WRITE)
519 			temp >>= 8;
520 		i++;
521 		address++;
522 		goto next_byte;
523 	}
524 
525 	if (function == ACPI_READ) {
526 		f_v |= temp << 8 * i;
527 		*value = f_v;
528 	}
529 
530       out:
531 	switch (result) {
532 	case -EINVAL:
533 		return AE_BAD_PARAMETER;
534 		break;
535 	case -ENODEV:
536 		return AE_NOT_FOUND;
537 		break;
538 	case -ETIME:
539 		return AE_TIME;
540 		break;
541 	default:
542 		return AE_OK;
543 	}
544 }
545 
546 /* --------------------------------------------------------------------------
547                               FS Interface (/proc)
548    -------------------------------------------------------------------------- */
549 
550 static struct proc_dir_entry *acpi_ec_dir;
551 
552 static int acpi_ec_read_info(struct seq_file *seq, void *offset)
553 {
554 	struct acpi_ec *ec = (struct acpi_ec *)seq->private;
555 
556 	if (!ec)
557 		goto end;
558 
559 	seq_printf(seq, "gpe:                 0x%02x\n", (u32) ec->gpe);
560 	seq_printf(seq, "ports:                   0x%02x, 0x%02x\n",
561 		   (u32) ec->command_addr, (u32) ec->data_addr);
562 	seq_printf(seq, "use global lock:         %s\n",
563 		   ec->global_lock ? "yes" : "no");
564 	acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
565 
566       end:
567 	return 0;
568 }
569 
570 static int acpi_ec_info_open_fs(struct inode *inode, struct file *file)
571 {
572 	return single_open(file, acpi_ec_read_info, PDE(inode)->data);
573 }
574 
575 static struct file_operations acpi_ec_info_ops = {
576 	.open = acpi_ec_info_open_fs,
577 	.read = seq_read,
578 	.llseek = seq_lseek,
579 	.release = single_release,
580 	.owner = THIS_MODULE,
581 };
582 
583 static int acpi_ec_add_fs(struct acpi_device *device)
584 {
585 	struct proc_dir_entry *entry = NULL;
586 
587 	if (!acpi_device_dir(device)) {
588 		acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
589 						     acpi_ec_dir);
590 		if (!acpi_device_dir(device))
591 			return -ENODEV;
592 	}
593 
594 	entry = create_proc_entry(ACPI_EC_FILE_INFO, S_IRUGO,
595 				  acpi_device_dir(device));
596 	if (!entry)
597 		return -ENODEV;
598 	else {
599 		entry->proc_fops = &acpi_ec_info_ops;
600 		entry->data = acpi_driver_data(device);
601 		entry->owner = THIS_MODULE;
602 	}
603 
604 	return 0;
605 }
606 
607 static int acpi_ec_remove_fs(struct acpi_device *device)
608 {
609 
610 	if (acpi_device_dir(device)) {
611 		remove_proc_entry(ACPI_EC_FILE_INFO, acpi_device_dir(device));
612 		remove_proc_entry(acpi_device_bid(device), acpi_ec_dir);
613 		acpi_device_dir(device) = NULL;
614 	}
615 
616 	return 0;
617 }
618 
619 /* --------------------------------------------------------------------------
620                                Driver Interface
621    -------------------------------------------------------------------------- */
622 
623 static int acpi_ec_add(struct acpi_device *device)
624 {
625 	int result = 0;
626 	acpi_status status = AE_OK;
627 	struct acpi_ec *ec = NULL;
628 
629 	if (!device)
630 		return -EINVAL;
631 
632 	ec = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
633 	if (!ec)
634 		return -ENOMEM;
635 
636 	ec->handle = device->handle;
637 	ec->uid = -1;
638 	mutex_init(&ec->lock);
639 	atomic_set(&ec->query_pending, 0);
640 	atomic_set(&ec->event_count, 1);
641 	if (acpi_ec_mode == EC_INTR) {
642 		atomic_set(&ec->leaving_burst, 1);
643 		init_waitqueue_head(&ec->wait);
644 	}
645 	strcpy(acpi_device_name(device), ACPI_EC_DEVICE_NAME);
646 	strcpy(acpi_device_class(device), ACPI_EC_CLASS);
647 	acpi_driver_data(device) = ec;
648 
649 	/* Use the global lock for all EC transactions? */
650 	acpi_evaluate_integer(ec->handle, "_GLK", NULL, &ec->global_lock);
651 
652 	/* XXX we don't test uids, because on some boxes ecdt uid = 0, see:
653 	   http://bugzilla.kernel.org/show_bug.cgi?id=6111 */
654 	if (ec_ecdt) {
655 		acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
656 						  ACPI_ADR_SPACE_EC,
657 						  &acpi_ec_space_handler);
658 
659 		acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
660 					&acpi_ec_gpe_handler);
661 
662 		kfree(ec_ecdt);
663 	}
664 
665 	/* Get GPE bit assignment (EC events). */
666 	/* TODO: Add support for _GPE returning a package */
667 	status = acpi_evaluate_integer(ec->handle, "_GPE", NULL, &ec->gpe);
668 	if (ACPI_FAILURE(status)) {
669 		ACPI_EXCEPTION((AE_INFO, status,
670 				"Obtaining GPE bit assignment"));
671 		result = -ENODEV;
672 		goto end;
673 	}
674 
675 	result = acpi_ec_add_fs(device);
676 	if (result)
677 		goto end;
678 
679 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s [%s] (gpe %d) interrupt mode.",
680 			  acpi_device_name(device), acpi_device_bid(device),
681 			  (u32) ec->gpe));
682 
683 	if (!first_ec)
684 		first_ec = device;
685 
686       end:
687 	if (result)
688 		kfree(ec);
689 
690 	return result;
691 }
692 
693 static int acpi_ec_remove(struct acpi_device *device, int type)
694 {
695 	struct acpi_ec *ec = NULL;
696 
697 	if (!device)
698 		return -EINVAL;
699 
700 	ec = acpi_driver_data(device);
701 
702 	acpi_ec_remove_fs(device);
703 
704 	kfree(ec);
705 
706 	return 0;
707 }
708 
709 static acpi_status
710 acpi_ec_io_ports(struct acpi_resource *resource, void *context)
711 {
712 	struct acpi_ec *ec = (struct acpi_ec *)context;
713 
714 	if (resource->type != ACPI_RESOURCE_TYPE_IO) {
715 		return AE_OK;
716 	}
717 
718 	/*
719 	 * The first address region returned is the data port, and
720 	 * the second address region returned is the status/command
721 	 * port.
722 	 */
723 	if (ec->data_addr == 0) {
724 		ec->data_addr = resource->data.io.minimum;
725 	} else if (ec->command_addr == 0) {
726 		ec->command_addr = resource->data.io.minimum;
727 	} else {
728 		return AE_CTRL_TERMINATE;
729 	}
730 
731 	return AE_OK;
732 }
733 
734 static int acpi_ec_start(struct acpi_device *device)
735 {
736 	acpi_status status = AE_OK;
737 	struct acpi_ec *ec = NULL;
738 
739 	if (!device)
740 		return -EINVAL;
741 
742 	ec = acpi_driver_data(device);
743 
744 	if (!ec)
745 		return -EINVAL;
746 
747 	/*
748 	 * Get I/O port addresses. Convert to GAS format.
749 	 */
750 	status = acpi_walk_resources(ec->handle, METHOD_NAME__CRS,
751 				     acpi_ec_io_ports, ec);
752 	if (ACPI_FAILURE(status) || ec->command_addr == 0) {
753 		ACPI_EXCEPTION((AE_INFO, status,
754 				"Error getting I/O port addresses"));
755 		return -ENODEV;
756 	}
757 
758 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "gpe=0x%02lx, ports=0x%2lx,0x%2lx",
759 			  ec->gpe, ec->command_addr, ec->data_addr));
760 
761 	/*
762 	 * Install GPE handler
763 	 */
764 	status = acpi_install_gpe_handler(NULL, ec->gpe,
765 					  ACPI_GPE_EDGE_TRIGGERED,
766 					  &acpi_ec_gpe_handler, ec);
767 	if (ACPI_FAILURE(status)) {
768 		return -ENODEV;
769 	}
770 	acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
771 	acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
772 
773 	status = acpi_install_address_space_handler(ec->handle,
774 						    ACPI_ADR_SPACE_EC,
775 						    &acpi_ec_space_handler,
776 						    &acpi_ec_space_setup, ec);
777 	if (ACPI_FAILURE(status)) {
778 		acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
779 		return -ENODEV;
780 	}
781 
782 	return AE_OK;
783 }
784 
785 static int acpi_ec_stop(struct acpi_device *device, int type)
786 {
787 	acpi_status status = AE_OK;
788 	struct acpi_ec *ec = NULL;
789 
790 	if (!device)
791 		return -EINVAL;
792 
793 	ec = acpi_driver_data(device);
794 
795 	status = acpi_remove_address_space_handler(ec->handle,
796 						   ACPI_ADR_SPACE_EC,
797 						   &acpi_ec_space_handler);
798 	if (ACPI_FAILURE(status))
799 		return -ENODEV;
800 
801 	status = acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
802 	if (ACPI_FAILURE(status))
803 		return -ENODEV;
804 
805 	return 0;
806 }
807 
808 static acpi_status __init
809 acpi_fake_ecdt_callback(acpi_handle handle,
810 			u32 Level, void *context, void **retval)
811 {
812 	acpi_status status;
813 
814 	mutex_init(&ec_ecdt->lock);
815 	atomic_set(&ec_ecdt->event_count, 1);
816 	if (acpi_ec_mode == EC_INTR) {
817 		init_waitqueue_head(&ec_ecdt->wait);
818 	}
819 	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
820 				     acpi_ec_io_ports, ec_ecdt);
821 	if (ACPI_FAILURE(status))
822 		return status;
823 
824 	ec_ecdt->uid = -1;
825 	acpi_evaluate_integer(handle, "_UID", NULL, &ec_ecdt->uid);
826 
827 	status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec_ecdt->gpe);
828 	if (ACPI_FAILURE(status))
829 		return status;
830 	ec_ecdt->global_lock = TRUE;
831 	ec_ecdt->handle = handle;
832 
833 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "GPE=0x%02lx, ports=0x%2lx, 0x%2lx",
834 			  ec_ecdt->gpe, ec_ecdt->command_addr,
835 			  ec_ecdt->data_addr));
836 
837 	return AE_CTRL_TERMINATE;
838 }
839 
840 /*
841  * Some BIOS (such as some from Gateway laptops) access EC region very early
842  * such as in BAT0._INI or EC._INI before an EC device is found and
843  * do not provide an ECDT. According to ACPI spec, ECDT isn't mandatorily
844  * required, but if EC regison is accessed early, it is required.
845  * The routine tries to workaround the BIOS bug by pre-scan EC device
846  * It assumes that _CRS, _HID, _GPE, _UID methods of EC don't touch any
847  * op region (since _REG isn't invoked yet). The assumption is true for
848  * all systems found.
849  */
850 static int __init acpi_ec_fake_ecdt(void)
851 {
852 	acpi_status status;
853 	int ret = 0;
854 
855 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Try to make an fake ECDT"));
856 
857 	ec_ecdt = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
858 	if (!ec_ecdt) {
859 		ret = -ENOMEM;
860 		goto error;
861 	}
862 
863 	status = acpi_get_devices(ACPI_EC_HID,
864 				  acpi_fake_ecdt_callback, NULL, NULL);
865 	if (ACPI_FAILURE(status)) {
866 		kfree(ec_ecdt);
867 		ec_ecdt = NULL;
868 		ret = -ENODEV;
869 		ACPI_EXCEPTION((AE_INFO, status, "Can't make an fake ECDT"));
870 		goto error;
871 	}
872 	return 0;
873       error:
874 	return ret;
875 }
876 
877 static int __init acpi_ec_get_real_ecdt(void)
878 {
879 	acpi_status status;
880 	struct acpi_table_ecdt *ecdt_ptr;
881 
882 	status = acpi_get_table(ACPI_SIG_ECDT, 1,
883 				(struct acpi_table_header **)&ecdt_ptr);
884 	if (ACPI_FAILURE(status))
885 		return -ENODEV;
886 
887 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found ECDT"));
888 
889 	/*
890 	 * Generate a temporary ec context to use until the namespace is scanned
891 	 */
892 	ec_ecdt = kzalloc(sizeof(struct acpi_ec), GFP_KERNEL);
893 	if (!ec_ecdt)
894 		return -ENOMEM;
895 
896 	mutex_init(&ec_ecdt->lock);
897 	atomic_set(&ec_ecdt->event_count, 1);
898 	if (acpi_ec_mode == EC_INTR) {
899 		init_waitqueue_head(&ec_ecdt->wait);
900 	}
901 	ec_ecdt->command_addr = ecdt_ptr->control.address;
902 	ec_ecdt->data_addr = ecdt_ptr->data.address;
903 	ec_ecdt->gpe = ecdt_ptr->gpe;
904 	/* use the GL just to be safe */
905 	ec_ecdt->global_lock = TRUE;
906 	ec_ecdt->uid = ecdt_ptr->uid;
907 
908 	status = acpi_get_handle(NULL, ecdt_ptr->id, &ec_ecdt->handle);
909 	if (ACPI_FAILURE(status)) {
910 		goto error;
911 	}
912 
913 	return 0;
914       error:
915 	ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
916 	kfree(ec_ecdt);
917 	ec_ecdt = NULL;
918 
919 	return -ENODEV;
920 }
921 
922 static int __initdata acpi_fake_ecdt_enabled;
923 int __init acpi_ec_ecdt_probe(void)
924 {
925 	acpi_status status;
926 	int ret;
927 
928 	ret = acpi_ec_get_real_ecdt();
929 	/* Try to make a fake ECDT */
930 	if (ret && acpi_fake_ecdt_enabled) {
931 		ret = acpi_ec_fake_ecdt();
932 	}
933 
934 	if (ret)
935 		return 0;
936 
937 	/*
938 	 * Install GPE handler
939 	 */
940 	status = acpi_install_gpe_handler(NULL, ec_ecdt->gpe,
941 					  ACPI_GPE_EDGE_TRIGGERED,
942 					  &acpi_ec_gpe_handler, ec_ecdt);
943 	if (ACPI_FAILURE(status)) {
944 		goto error;
945 	}
946 	acpi_set_gpe_type(NULL, ec_ecdt->gpe, ACPI_GPE_TYPE_RUNTIME);
947 	acpi_enable_gpe(NULL, ec_ecdt->gpe, ACPI_NOT_ISR);
948 
949 	status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
950 						    ACPI_ADR_SPACE_EC,
951 						    &acpi_ec_space_handler,
952 						    &acpi_ec_space_setup,
953 						    ec_ecdt);
954 	if (ACPI_FAILURE(status)) {
955 		acpi_remove_gpe_handler(NULL, ec_ecdt->gpe,
956 					&acpi_ec_gpe_handler);
957 		goto error;
958 	}
959 
960 	return 0;
961 
962       error:
963 	ACPI_EXCEPTION((AE_INFO, status, "Could not use ECDT"));
964 	kfree(ec_ecdt);
965 	ec_ecdt = NULL;
966 
967 	return -ENODEV;
968 }
969 
970 static int __init acpi_ec_init(void)
971 {
972 	int result = 0;
973 
974 	if (acpi_disabled)
975 		return 0;
976 
977 	acpi_ec_dir = proc_mkdir(ACPI_EC_CLASS, acpi_root_dir);
978 	if (!acpi_ec_dir)
979 		return -ENODEV;
980 
981 	/* Now register the driver for the EC */
982 	result = acpi_bus_register_driver(&acpi_ec_driver);
983 	if (result < 0) {
984 		remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
985 		return -ENODEV;
986 	}
987 
988 	return result;
989 }
990 
991 subsys_initcall(acpi_ec_init);
992 
993 /* EC driver currently not unloadable */
994 #if 0
995 static void __exit acpi_ec_exit(void)
996 {
997 
998 	acpi_bus_unregister_driver(&acpi_ec_driver);
999 
1000 	remove_proc_entry(ACPI_EC_CLASS, acpi_root_dir);
1001 
1002 	return;
1003 }
1004 #endif				/* 0 */
1005 
1006 static int __init acpi_fake_ecdt_setup(char *str)
1007 {
1008 	acpi_fake_ecdt_enabled = 1;
1009 	return 1;
1010 }
1011 
1012 __setup("acpi_fake_ecdt", acpi_fake_ecdt_setup);
1013 static int __init acpi_ec_set_intr_mode(char *str)
1014 {
1015 	int intr;
1016 
1017 	if (!get_option(&str, &intr))
1018 		return 0;
1019 
1020 	if (intr) {
1021 		acpi_ec_mode = EC_INTR;
1022 	} else {
1023 		acpi_ec_mode = EC_POLL;
1024 	}
1025 	acpi_ec_driver.ops.add = acpi_ec_add;
1026 	printk(KERN_NOTICE PREFIX "%s mode.\n", intr ? "interrupt" : "polling");
1027 
1028 	return 1;
1029 }
1030 
1031 __setup("ec_intr=", acpi_ec_set_intr_mode);
1032