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