xref: /netbsd-src/sys/dev/acpi/acpi_button.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: acpi_button.c,v 1.42 2015/04/23 23:23:00 pgoyette 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.42 2015/04/23 23:23:00 pgoyette 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 static const char * const power_button_hid[] = {
65 	"PNP0C0C",
66 	NULL
67 };
68 
69 static const char * const sleep_button_hid[] = {
70 	"PNP0C0E",
71 	NULL
72 };
73 
74 static int	acpibut_match(device_t, cfdata_t, void *);
75 static void	acpibut_attach(device_t, device_t, void *);
76 static int	acpibut_detach(device_t, int);
77 static void	acpibut_pressed_event(void *);
78 static void	acpibut_notify_handler(ACPI_HANDLE, uint32_t, void *);
79 
80 CFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
81     acpibut_match, acpibut_attach, acpibut_detach, NULL);
82 
83 /*
84  * acpibut_match:
85  *
86  *	Autoconfiguration `match' routine.
87  */
88 static int
89 acpibut_match(device_t parent, cfdata_t match, void *aux)
90 {
91 	struct acpi_attach_args *aa = aux;
92 
93 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
94 		return 0;
95 
96 	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid))
97 		return 1;
98 
99 	if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid))
100 		return 1;
101 
102 	return 0;
103 }
104 
105 /*
106  * acpibut_attach:
107  *
108  *	Autoconfiguration `attach' routine.
109  */
110 static void
111 acpibut_attach(device_t parent, device_t self, void *aux)
112 {
113 	struct acpibut_softc *sc = device_private(self);
114 	struct acpi_attach_args *aa = aux;
115 	struct acpi_wakedev *aw;
116 	const char *desc;
117 
118 	sc->sc_smpsw.smpsw_name = device_xname(self);
119 
120 	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) {
121 		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
122 		desc = "Power";
123 	} else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) {
124 		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
125 		desc = "Sleep";
126 	} else
127 		panic("%s: impossible", __func__);
128 
129 	aprint_naive(": ACPI %s Button\n", desc);
130 	aprint_normal(": ACPI %s Button\n", 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
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
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
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
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