acpi_button.c revision 1.39
1/*	$NetBSD: acpi_button.c,v 1.39 2010/10/25 07:48:03 jruoho 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.39 2010/10/25 07:48:03 jruoho 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
53#define _COMPONENT		 ACPI_BUTTON_COMPONENT
54ACPI_MODULE_NAME		 ("acpi_button")
55
56#define ACPI_NOTIFY_BUTTON	 0x80
57
58struct acpibut_softc {
59	struct acpi_devnode	*sc_node;
60	struct sysmon_pswitch	 sc_smpsw;
61};
62
63static const char * const power_button_hid[] = {
64	"PNP0C0C",
65	NULL
66};
67
68static const char * const sleep_button_hid[] = {
69	"PNP0C0E",
70	NULL
71};
72
73static int	acpibut_match(device_t, cfdata_t, void *);
74static void	acpibut_attach(device_t, device_t, void *);
75static int	acpibut_detach(device_t, int);
76static void	acpibut_pressed_event(void *);
77static void	acpibut_notify_handler(ACPI_HANDLE, uint32_t, void *);
78
79CFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
80    acpibut_match, acpibut_attach, acpibut_detach, NULL);
81
82/*
83 * acpibut_match:
84 *
85 *	Autoconfiguration `match' routine.
86 */
87static int
88acpibut_match(device_t parent, cfdata_t match, void *aux)
89{
90	struct acpi_attach_args *aa = aux;
91
92	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
93		return 0;
94
95	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid))
96		return 1;
97
98	if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid))
99		return 1;
100
101	return 0;
102}
103
104/*
105 * acpibut_attach:
106 *
107 *	Autoconfiguration `attach' routine.
108 */
109static void
110acpibut_attach(device_t parent, device_t self, void *aux)
111{
112	struct acpibut_softc *sc = device_private(self);
113	struct acpi_attach_args *aa = aux;
114	const char *desc;
115
116	sc->sc_smpsw.smpsw_name = device_xname(self);
117
118	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) {
119		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
120		desc = "Power";
121	} else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) {
122		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
123		desc = "Sleep";
124	} else
125		panic("%s: impossible", __func__);
126
127	aprint_naive(": ACPI %s Button\n", desc);
128	aprint_normal(": ACPI %s Button\n", desc);
129
130	sc->sc_node = aa->aa_node;
131
132	(void)pmf_device_register(self, NULL, NULL);
133	(void)sysmon_pswitch_register(&sc->sc_smpsw);
134	(void)acpi_register_notify(sc->sc_node, acpibut_notify_handler);
135}
136
137/*
138 * acpibut_detach:
139 *
140 *	Autoconfiguration `detach' routine.
141 */
142static int
143acpibut_detach(device_t self, int flags)
144{
145	struct acpibut_softc *sc = device_private(self);
146
147	pmf_device_deregister(self);
148	acpi_deregister_notify(sc->sc_node);
149	sysmon_pswitch_unregister(&sc->sc_smpsw);
150
151	return 0;
152}
153
154/*
155 * acpibut_pressed_event:
156 *
157 *	Deal with a button being pressed.
158 */
159static void
160acpibut_pressed_event(void *arg)
161{
162	device_t dv = arg;
163	struct acpibut_softc *sc = device_private(dv);
164
165	aprint_debug_dev(dv, "button pressed\n");
166	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
167}
168
169/*
170 * acpibut_notify_handler:
171 *
172 *	Callback from ACPI interrupt handler to notify us of an event.
173 */
174static void
175acpibut_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
176{
177	static const int handler = OSL_NOTIFY_HANDLER;
178	device_t dv = context;
179
180	switch (notify) {
181
182	case ACPI_NOTIFY_BUTTON:
183		(void)AcpiOsExecute(handler, acpibut_pressed_event, dv);
184		break;
185
186	case ACPI_NOTIFY_DEVICE_WAKE:
187		break;
188
189	default:
190		aprint_debug_dev(dv, "unknown notify 0x%02X\n", notify);
191	}
192}
193
194#ifdef _MODULE
195
196MODULE(MODULE_CLASS_DRIVER, acpibut, NULL);
197CFDRIVER_DECL(acpibut, DV_DULL, NULL);
198
199static int acpibutloc[] = { -1 };
200extern struct cfattach acpibut_ca;
201
202static struct cfparent acpiparent = {
203	"acpinodebus", NULL, DVUNIT_ANY
204};
205
206static struct cfdata acpibut_cfdata[] = {
207	{
208		.cf_name = "acpibut",
209		.cf_atname = "acpibut",
210		.cf_unit = 0,
211		.cf_fstate = FSTATE_STAR,
212		.cf_loc = acpibutloc,
213		.cf_flags = 0,
214		.cf_pspec = &acpiparent,
215	},
216
217	{ NULL, NULL, 0, 0, NULL, 0, NULL }
218};
219
220static int
221acpibut_modcmd(modcmd_t cmd, void *context)
222{
223	int err;
224
225	switch (cmd) {
226
227	case MODULE_CMD_INIT:
228
229		err = config_cfdriver_attach(&acpibut_cd);
230
231		if (err != 0)
232			return err;
233
234		err = config_cfattach_attach("acpibut", &acpibut_ca);
235
236		if (err != 0) {
237			config_cfdriver_detach(&acpibut_cd);
238			return err;
239		}
240
241		err = config_cfdata_attach(acpibut_cfdata, 1);
242
243		if (err != 0) {
244			config_cfattach_detach("acpibut", &acpibut_ca);
245			config_cfdriver_detach(&acpibut_cd);
246			return err;
247		}
248
249		return 0;
250
251	case MODULE_CMD_FINI:
252
253		err = config_cfdata_detach(acpibut_cfdata);
254
255		if (err != 0)
256			return err;
257
258		config_cfattach_detach("acpibut", &acpibut_ca);
259		config_cfdriver_detach(&acpibut_cd);
260
261		return 0;
262
263	default:
264		return ENOTTY;
265	}
266}
267
268#endif	/* _MODULE */
269