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