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