acpi_button.c revision 1.36
1/*	$NetBSD: acpi_button.c,v 1.36 2010/04/27 05:57:43 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.36 2010/04/27 05:57:43 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	default:
187		aprint_error_dev(dv, "unknown notify 0x%02X\n", notify);
188	}
189}
190
191#ifdef _MODULE
192
193MODULE(MODULE_CLASS_DRIVER, acpibut, NULL);
194CFDRIVER_DECL(acpibut, DV_DULL, NULL);
195
196static int acpibutloc[] = { -1 };
197extern struct cfattach acpibut_ca;
198
199static struct cfparent acpiparent = {
200	"acpinodebus", NULL, DVUNIT_ANY
201};
202
203static struct cfdata acpibut_cfdata[] = {
204	{
205		.cf_name = "acpibut",
206		.cf_atname = "acpibut",
207		.cf_unit = 0,
208		.cf_fstate = FSTATE_STAR,
209		.cf_loc = acpibutloc,
210		.cf_flags = 0,
211		.cf_pspec = &acpiparent,
212	},
213
214	{ NULL }
215};
216
217static int
218acpibut_modcmd(modcmd_t cmd, void *context)
219{
220	int err;
221
222	switch (cmd) {
223
224	case MODULE_CMD_INIT:
225
226		err = config_cfdriver_attach(&acpibut_cd);
227
228		if (err != 0)
229			return err;
230
231		err = config_cfattach_attach("acpibut", &acpibut_ca);
232
233		if (err != 0) {
234			config_cfdriver_detach(&acpibut_cd);
235			return err;
236		}
237
238		err = config_cfdata_attach(acpibut_cfdata, 1);
239
240		if (err != 0) {
241			config_cfattach_detach("acpibut", &acpibut_ca);
242			config_cfdriver_detach(&acpibut_cd);
243			return err;
244		}
245
246		return 0;
247
248	case MODULE_CMD_FINI:
249
250		err = config_cfdata_detach(acpibut_cfdata);
251
252		if (err != 0)
253			return err;
254
255		config_cfattach_detach("acpibut", &acpibut_ca);
256		config_cfdriver_detach(&acpibut_cd);
257
258		return 0;
259
260	default:
261		return ENOTTY;
262	}
263}
264
265#endif	/* _MODULE */
266