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