1 /* $NetBSD: acpi_button.c,v 1.43 2021/01/29 15:24:00 thorpej Exp $ */
2
3 /*
4 * Copyright 2001, 2003 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * ACPI Button driver.
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: acpi_button.c,v 1.43 2021/01/29 15:24:00 thorpej Exp $");
44
45 #include <sys/param.h>
46 #include <sys/device.h>
47 #include <sys/module.h>
48 #include <sys/systm.h>
49
50 #include <dev/acpi/acpireg.h>
51 #include <dev/acpi/acpivar.h>
52 #include <dev/acpi/acpi_wakedev.h>
53
54 #define _COMPONENT ACPI_BUTTON_COMPONENT
55 ACPI_MODULE_NAME ("acpi_button")
56
57 #define ACPI_NOTIFY_BUTTON 0x80
58
59 struct acpibut_softc {
60 struct acpi_devnode *sc_node;
61 struct sysmon_pswitch sc_smpsw;
62 };
63
64 struct button_type {
65 const char *desc;
66 int type;
67 };
68
69 static const struct button_type power_button_type = {
70 .desc = "Power",
71 .type = PSWITCH_TYPE_POWER
72 };
73
74 static const struct button_type sleep_button_type = {
75 .desc = "Sleep",
76 .type = PSWITCH_TYPE_SLEEP
77 };
78
79 static const struct device_compatible_entry compat_data[] = {
80 { .compat = "PNP0C0C", .data = &power_button_type },
81 { .compat = "PNP0C0E", .data = &sleep_button_type },
82 DEVICE_COMPAT_EOL
83 };
84
85 static int acpibut_match(device_t, cfdata_t, void *);
86 static void acpibut_attach(device_t, device_t, void *);
87 static int acpibut_detach(device_t, int);
88 static void acpibut_pressed_event(void *);
89 static void acpibut_notify_handler(ACPI_HANDLE, uint32_t, void *);
90
91 CFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
92 acpibut_match, acpibut_attach, acpibut_detach, NULL);
93
94 /*
95 * acpibut_match:
96 *
97 * Autoconfiguration `match' routine.
98 */
99 static int
acpibut_match(device_t parent,cfdata_t match,void * aux)100 acpibut_match(device_t parent, cfdata_t match, void *aux)
101 {
102 struct acpi_attach_args *aa = aux;
103
104 return acpi_compatible_match(aa, compat_data);
105 }
106
107 /*
108 * acpibut_attach:
109 *
110 * Autoconfiguration `attach' routine.
111 */
112 static void
acpibut_attach(device_t parent,device_t self,void * aux)113 acpibut_attach(device_t parent, device_t self, void *aux)
114 {
115 struct acpibut_softc *sc = device_private(self);
116 struct acpi_attach_args *aa = aux;
117 const struct device_compatible_entry *dce;
118 const struct button_type *type;
119 struct acpi_wakedev *aw;
120
121 sc->sc_smpsw.smpsw_name = device_xname(self);
122
123 dce = acpi_compatible_lookup(aa, compat_data);
124 KASSERT(dce != NULL);
125 type = dce->data;
126
127 sc->sc_smpsw.smpsw_type = type->type;
128
129 aprint_naive(": ACPI %s Button\n", type->desc);
130 aprint_normal(": ACPI %s Button\n", type->desc);
131
132 sc->sc_node = aa->aa_node;
133 aw = sc->sc_node->ad_wakedev;
134
135 /*
136 * This GPE should always be enabled.
137 */
138 if (aw != NULL)
139 (void)AcpiEnableGpe(aw->aw_handle, aw->aw_number);
140
141 (void)pmf_device_register(self, NULL, NULL);
142 (void)sysmon_pswitch_register(&sc->sc_smpsw);
143 (void)acpi_register_notify(sc->sc_node, acpibut_notify_handler);
144 }
145
146 /*
147 * acpibut_detach:
148 *
149 * Autoconfiguration `detach' routine.
150 */
151 static int
acpibut_detach(device_t self,int flags)152 acpibut_detach(device_t self, int flags)
153 {
154 struct acpibut_softc *sc = device_private(self);
155
156 pmf_device_deregister(self);
157 acpi_deregister_notify(sc->sc_node);
158 sysmon_pswitch_unregister(&sc->sc_smpsw);
159
160 return 0;
161 }
162
163 /*
164 * acpibut_pressed_event:
165 *
166 * Deal with a button being pressed.
167 */
168 static void
acpibut_pressed_event(void * arg)169 acpibut_pressed_event(void *arg)
170 {
171 device_t dv = arg;
172 struct acpibut_softc *sc = device_private(dv);
173
174 aprint_debug_dev(dv, "button pressed\n");
175 sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
176 }
177
178 /*
179 * acpibut_notify_handler:
180 *
181 * Callback from ACPI interrupt handler to notify us of an event.
182 */
183 static void
acpibut_notify_handler(ACPI_HANDLE handle,uint32_t notify,void * context)184 acpibut_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
185 {
186 static const int handler = OSL_NOTIFY_HANDLER;
187 device_t dv = context;
188
189 switch (notify) {
190
191 case ACPI_NOTIFY_BUTTON:
192 (void)AcpiOsExecute(handler, acpibut_pressed_event, dv);
193 break;
194
195 case ACPI_NOTIFY_DEVICE_WAKE:
196 break;
197
198 default:
199 aprint_debug_dev(dv, "unknown notify 0x%02X\n", notify);
200 }
201 }
202
203 MODULE(MODULE_CLASS_DRIVER, acpibut, "sysmon_power");
204
205 #ifdef _MODULE
206 #include "ioconf.c"
207 #endif
208
209 static int
acpibut_modcmd(modcmd_t cmd,void * aux)210 acpibut_modcmd(modcmd_t cmd, void *aux)
211 {
212 int rv = 0;
213
214 switch (cmd) {
215
216 case MODULE_CMD_INIT:
217
218 #ifdef _MODULE
219 rv = config_init_component(cfdriver_ioconf_acpibut,
220 cfattach_ioconf_acpibut, cfdata_ioconf_acpibut);
221 #endif
222 break;
223
224 case MODULE_CMD_FINI:
225
226 #ifdef _MODULE
227 rv = config_fini_component(cfdriver_ioconf_acpibut,
228 cfattach_ioconf_acpibut, cfdata_ioconf_acpibut);
229 #endif
230 break;
231
232 default:
233 rv = ENOTTY;
234 }
235
236 return rv;
237 }
238