acpi_event.c revision 1.1.6.2 1 1.1.6.2 christos /* $NetBSD: acpi_event.c,v 1.1.6.2 2019/06/10 22:07:05 christos Exp $ */
2 1.1.6.2 christos
3 1.1.6.2 christos /*-
4 1.1.6.2 christos * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1.6.2 christos * All rights reserved.
6 1.1.6.2 christos *
7 1.1.6.2 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1.6.2 christos * by Jared McNeill <jmcneill (at) invisible.ca>.
9 1.1.6.2 christos *
10 1.1.6.2 christos * Redistribution and use in source and binary forms, with or without
11 1.1.6.2 christos * modification, are permitted provided that the following conditions
12 1.1.6.2 christos * are met:
13 1.1.6.2 christos * 1. Redistributions of source code must retain the above copyright
14 1.1.6.2 christos * notice, this list of conditions and the following disclaimer.
15 1.1.6.2 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.6.2 christos * notice, this list of conditions and the following disclaimer in the
17 1.1.6.2 christos * documentation and/or other materials provided with the distribution.
18 1.1.6.2 christos *
19 1.1.6.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.6.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.6.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.6.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.6.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.6.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.6.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.6.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.6.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.6.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.6.2 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1.6.2 christos */
31 1.1.6.2 christos
32 1.1.6.2 christos #include <sys/cdefs.h>
33 1.1.6.2 christos __KERNEL_RCSID(0, "$NetBSD: acpi_event.c,v 1.1.6.2 2019/06/10 22:07:05 christos Exp $");
34 1.1.6.2 christos
35 1.1.6.2 christos #include <sys/param.h>
36 1.1.6.2 christos #include <sys/bus.h>
37 1.1.6.2 christos #include <sys/cpu.h>
38 1.1.6.2 christos #include <sys/kmem.h>
39 1.1.6.2 christos
40 1.1.6.2 christos #include <dev/acpi/acpireg.h>
41 1.1.6.2 christos #include <dev/acpi/acpivar.h>
42 1.1.6.2 christos #include <dev/acpi/acpi_event.h>
43 1.1.6.2 christos
44 1.1.6.2 christos struct acpi_event {
45 1.1.6.2 christos device_t ev_dev;
46 1.1.6.2 christos ACPI_HANDLE ev_method;
47 1.1.6.2 christos bool ev_method_evt;
48 1.1.6.2 christos UINT16 ev_data;
49 1.1.6.2 christos };
50 1.1.6.2 christos
51 1.1.6.2 christos struct acpi_event_gpio_context {
52 1.1.6.2 christos device_t ctx_dev;
53 1.1.6.2 christos ACPI_HANDLE ctx_handle;
54 1.1.6.2 christos void (*ctx_func)(void *, struct acpi_event *, ACPI_RESOURCE_GPIO *);
55 1.1.6.2 christos void *ctx_arg;
56 1.1.6.2 christos };
57 1.1.6.2 christos
58 1.1.6.2 christos static ACPI_STATUS
59 1.1.6.2 christos acpi_event_create(device_t dev, ACPI_HANDLE handle, UINT16 data, UINT8 trig, struct acpi_event **pev)
60 1.1.6.2 christos {
61 1.1.6.2 christos struct acpi_event *ev;
62 1.1.6.2 christos char namebuf[5];
63 1.1.6.2 christos
64 1.1.6.2 christos const char trigchar = trig == ACPI_LEVEL_SENSITIVE ? 'L' : 'E';
65 1.1.6.2 christos
66 1.1.6.2 christos ev = kmem_alloc(sizeof(*ev), KM_SLEEP);
67 1.1.6.2 christos ev->ev_dev = dev;
68 1.1.6.2 christos ev->ev_method = NULL;
69 1.1.6.2 christos ev->ev_method_evt = false;
70 1.1.6.2 christos ev->ev_data = data;
71 1.1.6.2 christos
72 1.1.6.2 christos if (data <= 255) {
73 1.1.6.2 christos snprintf(namebuf, sizeof(namebuf), "_%c%02X", trigchar, data);
74 1.1.6.2 christos AcpiGetHandle(handle, namebuf, &ev->ev_method);
75 1.1.6.2 christos }
76 1.1.6.2 christos if (ev->ev_method == NULL && ACPI_SUCCESS(AcpiGetHandle(handle, "_EVT", &ev->ev_method)))
77 1.1.6.2 christos ev->ev_method_evt = true;
78 1.1.6.2 christos
79 1.1.6.2 christos if (ev->ev_method == NULL) {
80 1.1.6.2 christos aprint_error_dev(dev, "%s is missing a control method for event %#x (trig %c)\n",
81 1.1.6.2 christos acpi_name(handle), data, trigchar);
82 1.1.6.2 christos kmem_free(ev, sizeof(*ev));
83 1.1.6.2 christos return AE_NOT_FOUND;
84 1.1.6.2 christos }
85 1.1.6.2 christos
86 1.1.6.2 christos *pev = ev;
87 1.1.6.2 christos
88 1.1.6.2 christos return AE_OK;
89 1.1.6.2 christos }
90 1.1.6.2 christos
91 1.1.6.2 christos static ACPI_STATUS
92 1.1.6.2 christos acpi_event_gpio_resource_cb(ACPI_RESOURCE *res, void *priv)
93 1.1.6.2 christos {
94 1.1.6.2 christos struct acpi_event_gpio_context *ctx = priv;
95 1.1.6.2 christos struct acpi_event *ev;
96 1.1.6.2 christos ACPI_RESOURCE_GPIO *gpio;
97 1.1.6.2 christos ACPI_STATUS rv;
98 1.1.6.2 christos
99 1.1.6.2 christos if (res->Type != ACPI_RESOURCE_TYPE_GPIO)
100 1.1.6.2 christos return AE_OK;
101 1.1.6.2 christos
102 1.1.6.2 christos gpio = &res->Data.Gpio;
103 1.1.6.2 christos if (gpio->ConnectionType != ACPI_RESOURCE_GPIO_TYPE_INT)
104 1.1.6.2 christos return AE_OK;
105 1.1.6.2 christos if (gpio->PinTableLength != 1)
106 1.1.6.2 christos return AE_OK;
107 1.1.6.2 christos
108 1.1.6.2 christos rv = acpi_event_create(ctx->ctx_dev, ctx->ctx_handle, gpio->PinTable[0], gpio->Triggering, &ev);
109 1.1.6.2 christos if (ACPI_SUCCESS(rv))
110 1.1.6.2 christos ctx->ctx_func(ctx->ctx_arg, ev, gpio);
111 1.1.6.2 christos
112 1.1.6.2 christos return AE_OK;
113 1.1.6.2 christos }
114 1.1.6.2 christos
115 1.1.6.2 christos ACPI_STATUS
116 1.1.6.2 christos acpi_event_create_gpio(device_t dev, ACPI_HANDLE handle,
117 1.1.6.2 christos void (*func)(void *, struct acpi_event *, ACPI_RESOURCE_GPIO *), void *arg)
118 1.1.6.2 christos {
119 1.1.6.2 christos struct acpi_event_gpio_context ctx;
120 1.1.6.2 christos
121 1.1.6.2 christos ctx.ctx_dev = dev;
122 1.1.6.2 christos ctx.ctx_handle = handle;
123 1.1.6.2 christos ctx.ctx_func = func;
124 1.1.6.2 christos ctx.ctx_arg = arg;
125 1.1.6.2 christos
126 1.1.6.2 christos return AcpiWalkResources(handle, "_AEI", acpi_event_gpio_resource_cb, &ctx);
127 1.1.6.2 christos }
128 1.1.6.2 christos
129 1.1.6.2 christos ACPI_STATUS
130 1.1.6.2 christos acpi_event_create_int(device_t dev, ACPI_HANDLE handle,
131 1.1.6.2 christos void (*func)(void *, struct acpi_event *, struct acpi_irq *), void *arg)
132 1.1.6.2 christos {
133 1.1.6.2 christos struct acpi_resources res;
134 1.1.6.2 christos struct acpi_event *ev;
135 1.1.6.2 christos struct acpi_irq *irq;
136 1.1.6.2 christos ACPI_STATUS rv;
137 1.1.6.2 christos int n;
138 1.1.6.2 christos
139 1.1.6.2 christos rv = acpi_resource_parse(dev, handle, "_CRS", &res,
140 1.1.6.2 christos &acpi_resource_parse_ops_default);
141 1.1.6.2 christos if (ACPI_FAILURE(rv))
142 1.1.6.2 christos return rv;
143 1.1.6.2 christos
144 1.1.6.2 christos for (n = 0; (irq = acpi_res_irq(&res, n)) != NULL; n++) {
145 1.1.6.2 christos rv = acpi_event_create(dev, handle, irq->ar_irq, irq->ar_type, &ev);
146 1.1.6.2 christos if (ACPI_SUCCESS(rv))
147 1.1.6.2 christos func(arg, ev, irq);
148 1.1.6.2 christos }
149 1.1.6.2 christos
150 1.1.6.2 christos acpi_resource_cleanup(&res);
151 1.1.6.2 christos
152 1.1.6.2 christos return AE_OK;
153 1.1.6.2 christos }
154 1.1.6.2 christos
155 1.1.6.2 christos static void
156 1.1.6.2 christos acpi_event_invoke(void *priv)
157 1.1.6.2 christos {
158 1.1.6.2 christos struct acpi_event * const ev = priv;
159 1.1.6.2 christos ACPI_OBJECT_LIST objs, *arg = NULL;
160 1.1.6.2 christos ACPI_OBJECT obj[1];
161 1.1.6.2 christos ACPI_STATUS rv;
162 1.1.6.2 christos
163 1.1.6.2 christos if (ev->ev_method_evt) {
164 1.1.6.2 christos objs.Count = 1;
165 1.1.6.2 christos objs.Pointer = obj;
166 1.1.6.2 christos obj[0].Type = ACPI_TYPE_INTEGER;
167 1.1.6.2 christos obj[0].Integer.Value = ev->ev_data;
168 1.1.6.2 christos arg = &objs;
169 1.1.6.2 christos }
170 1.1.6.2 christos
171 1.1.6.2 christos rv = AcpiEvaluateObject(ev->ev_method, NULL, arg, NULL);
172 1.1.6.2 christos if (ACPI_FAILURE(rv))
173 1.1.6.2 christos device_printf(ev->ev_dev, "failed to handle %s event: %s\n",
174 1.1.6.2 christos acpi_name(ev->ev_method), AcpiFormatException(rv));
175 1.1.6.2 christos }
176 1.1.6.2 christos
177 1.1.6.2 christos ACPI_STATUS
178 1.1.6.2 christos acpi_event_notify(struct acpi_event *ev)
179 1.1.6.2 christos {
180 1.1.6.2 christos return AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_event_invoke, ev);
181 1.1.6.2 christos }
182