xref: /netbsd-src/sys/dev/acpi/acpi_event.c (revision 369725234a52d801d02d2e34cbc47050131c30cf)
1 /* $NetBSD: acpi_event.c,v 1.2 2022/01/11 10:53:08 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_event.c,v 1.2 2022/01/11 10:53:08 jmcneill Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/cpu.h>
38 #include <sys/kmem.h>
39 
40 #include <dev/acpi/acpireg.h>
41 #include <dev/acpi/acpivar.h>
42 #include <dev/acpi/acpi_event.h>
43 #include <dev/acpi/acpi_intr.h>
44 
45 struct acpi_event {
46 	device_t	ev_dev;
47 	ACPI_HANDLE	ev_method;
48 	bool		ev_method_evt;
49 	UINT16		ev_data;
50 	void		*ev_intrcookie;
51 };
52 
53 struct acpi_event_gpio_context {
54 	device_t	ctx_dev;
55 	ACPI_HANDLE	ctx_handle;
56 	void		(*ctx_func)(void *, struct acpi_event *, ACPI_RESOURCE_GPIO *);
57 	void		*ctx_arg;
58 };
59 
60 static ACPI_STATUS
acpi_event_create(device_t dev,ACPI_HANDLE handle,UINT16 data,UINT8 trig,struct acpi_event ** pev)61 acpi_event_create(device_t dev, ACPI_HANDLE handle, UINT16 data, UINT8 trig, struct acpi_event **pev)
62 {
63 	struct acpi_event *ev;
64 	char namebuf[5];
65 
66 	const char trigchar = trig == ACPI_LEVEL_SENSITIVE ? 'L' : 'E';
67 
68 	ev = kmem_alloc(sizeof(*ev), KM_SLEEP);
69 	ev->ev_dev = dev;
70 	ev->ev_method = NULL;
71 	ev->ev_method_evt = false;
72 	ev->ev_data = data;
73 	ev->ev_intrcookie = NULL;
74 
75 	if (data <= 255) {
76 		snprintf(namebuf, sizeof(namebuf), "_%c%02X", trigchar, data);
77 		AcpiGetHandle(handle, namebuf, &ev->ev_method);
78 	}
79 	if (ev->ev_method == NULL && ACPI_SUCCESS(AcpiGetHandle(handle, "_EVT", &ev->ev_method)))
80 		ev->ev_method_evt = true;
81 
82 	if (ev->ev_method == NULL) {
83 		aprint_error_dev(dev, "%s is missing a control method for event %#x (trig %c)\n",
84 		    acpi_name(handle), data, trigchar);
85 		kmem_free(ev, sizeof(*ev));
86 		return AE_NOT_FOUND;
87 	}
88 
89 	*pev = ev;
90 
91 	return AE_OK;
92 }
93 
94 static ACPI_STATUS
acpi_event_gpio_resource_cb(ACPI_RESOURCE * res,void * priv)95 acpi_event_gpio_resource_cb(ACPI_RESOURCE *res, void *priv)
96 {
97 	struct acpi_event_gpio_context *ctx = priv;
98 	struct acpi_event *ev;
99 	ACPI_RESOURCE_GPIO *gpio;
100 	ACPI_STATUS rv;
101 
102 	if (res->Type != ACPI_RESOURCE_TYPE_GPIO)
103 		return AE_OK;
104 
105 	gpio = &res->Data.Gpio;
106 	if (gpio->ConnectionType != ACPI_RESOURCE_GPIO_TYPE_INT)
107 		return AE_OK;
108 	if (gpio->PinTableLength != 1)
109 		return AE_OK;
110 
111 	rv = acpi_event_create(ctx->ctx_dev, ctx->ctx_handle, gpio->PinTable[0], gpio->Triggering, &ev);
112 	if (ACPI_SUCCESS(rv))
113 		ctx->ctx_func(ctx->ctx_arg, ev, gpio);
114 
115 	return AE_OK;
116 }
117 
118 ACPI_STATUS
acpi_event_create_gpio(device_t dev,ACPI_HANDLE handle,void (* func)(void *,struct acpi_event *,ACPI_RESOURCE_GPIO *),void * arg)119 acpi_event_create_gpio(device_t dev, ACPI_HANDLE handle,
120     void (*func)(void *, struct acpi_event *, ACPI_RESOURCE_GPIO *), void *arg)
121 {
122 	struct acpi_event_gpio_context ctx;
123 
124 	ctx.ctx_dev = dev;
125 	ctx.ctx_handle = handle;
126 	ctx.ctx_func = func;
127 	ctx.ctx_arg = arg;
128 
129 	return AcpiWalkResources(handle, "_AEI", acpi_event_gpio_resource_cb, &ctx);
130 }
131 
132 ACPI_STATUS
acpi_event_create_int(device_t dev,ACPI_HANDLE handle,void (* func)(void *,struct acpi_event *,struct acpi_irq *),void * arg)133 acpi_event_create_int(device_t dev, ACPI_HANDLE handle,
134     void (*func)(void *, struct acpi_event *, struct acpi_irq *), void *arg)
135 {
136 	struct acpi_resources res;
137 	struct acpi_event *ev;
138 	struct acpi_irq *irq;
139 	ACPI_STATUS rv;
140 	int n;
141 
142 	rv = acpi_resource_parse(dev, handle, "_CRS", &res,
143             &acpi_resource_parse_ops_default);
144 	if (ACPI_FAILURE(rv))
145 		return rv;
146 
147 	for (n = 0; (irq = acpi_res_irq(&res, n)) != NULL; n++) {
148 		rv = acpi_event_create(dev, handle, irq->ar_irq, irq->ar_type, &ev);
149 		if (ACPI_SUCCESS(rv))
150 			func(arg, ev, irq);
151 	}
152 
153 	acpi_resource_cleanup(&res);
154 
155 	return AE_OK;
156 }
157 
158 static void
acpi_event_invoke(void * priv)159 acpi_event_invoke(void *priv)
160 {
161 	struct acpi_event * const ev = priv;
162 	ACPI_OBJECT_LIST objs, *arg = NULL;
163 	ACPI_OBJECT obj[1];
164 	ACPI_STATUS rv;
165 
166 	if (ev->ev_method_evt) {
167 		objs.Count = 1;
168 		objs.Pointer = obj;
169 		obj[0].Type = ACPI_TYPE_INTEGER;
170 		obj[0].Integer.Value = ev->ev_data;
171 		arg = &objs;
172 	}
173 
174 	rv = AcpiEvaluateObject(ev->ev_method, NULL, arg, NULL);
175 	if (ACPI_FAILURE(rv)) {
176 		device_printf(ev->ev_dev, "failed to handle %s event: %s\n",
177 		    acpi_name(ev->ev_method), AcpiFormatException(rv));
178 	}
179 
180 	if (ev->ev_intrcookie != NULL) {
181 		acpi_intr_unmask(ev->ev_intrcookie);
182 	}
183 }
184 
185 ACPI_STATUS
acpi_event_notify(struct acpi_event * ev)186 acpi_event_notify(struct acpi_event *ev)
187 {
188 	if (ev->ev_intrcookie != NULL) {
189 		acpi_intr_mask(ev->ev_intrcookie);
190 	}
191 
192 	return AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_event_invoke, ev);
193 }
194 
195 void
acpi_event_set_intrcookie(struct acpi_event * ev,void * intrcookie)196 acpi_event_set_intrcookie(struct acpi_event *ev, void *intrcookie)
197 {
198 	KASSERT(ev->ev_intrcookie == NULL);
199 
200 	ev->ev_intrcookie = intrcookie;
201 }
202