xref: /netbsd-src/sys/dev/acpi/acpi_ged.c (revision 154bfe8e089c1a0a4e9ed8414f08d3da90949162)
1 /* $NetBSD: acpi_ged.c,v 1.1 2018/10/22 22:36:19 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jared McNeill <jmcneill@invisible.ca>.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: acpi_ged.c,v 1.1 2018/10/22 22:36:19 jmcneill Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/cpu.h>
38 #include <sys/device.h>
39 #include <sys/intr.h>
40 #include <sys/kmem.h>
41 
42 #include <dev/acpi/acpireg.h>
43 #include <dev/acpi/acpivar.h>
44 #include <dev/acpi/acpi_event.h>
45 
46 static int	acpi_ged_match(device_t, cfdata_t, void *);
47 static void	acpi_ged_attach(device_t, device_t, void *);
48 
49 static void	acpi_ged_register_event(void *, struct acpi_event *, struct acpi_irq *);
50 static int	acpi_ged_intr(void *);
51 
52 CFATTACH_DECL_NEW(acpiged, 0, acpi_ged_match, acpi_ged_attach, NULL, NULL);
53 
54 static const char * const compatible[] = {
55 	"ACPI0013",
56 	NULL
57 };
58 
59 static int
60 acpi_ged_match(device_t parent, cfdata_t cf, void *aux)
61 {
62 	struct acpi_attach_args *aa = aux;
63 
64 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
65 		return 0;
66 
67 	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
68 }
69 
70 static void
71 acpi_ged_attach(device_t parent, device_t self, void *aux)
72 {
73 	struct acpi_attach_args * const aa = aux;
74 	ACPI_HANDLE handle = aa->aa_node->ad_handle;
75 
76         if (ACPI_FAILURE(acpi_event_create_int(self, handle, acpi_ged_register_event, self)))
77                 aprint_error_dev(self, "failed to create events\n");
78 }
79 
80 static void
81 acpi_ged_register_event(void *priv, struct acpi_event *ev, struct acpi_irq *irq)
82 {
83 	device_t dev = priv;
84 	void *ih;
85 
86 	const int type = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
87 	ih = intr_establish(irq->ar_irq, IPL_VM, type, acpi_ged_intr, ev);
88 	if (ih == NULL) {
89 		aprint_error_dev(dev, "couldn't establish interrupt (irq %d)\n", irq->ar_irq);
90 		return;
91 	}
92 }
93 
94 static int
95 acpi_ged_intr(void *priv)
96 {
97 	struct acpi_event * const ev = priv;
98 
99 	acpi_event_notify(ev);
100 
101 	return 1;
102 }
103