acpi_button.c revision 1.31
1/*	$NetBSD: acpi_button.c,v 1.31 2010/02/28 17:22:41 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.31 2010/02/28 17:22:41 jruoho Exp $");
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/device.h>
48#include <sys/module.h>
49
50#include <dev/acpi/acpica.h>
51#include <dev/acpi/acpireg.h>
52#include <dev/acpi/acpivar.h>
53
54#include <dev/sysmon/sysmonvar.h>
55
56#define _COMPONENT		ACPI_BUTTON_COMPONENT
57ACPI_MODULE_NAME		("acpi_button")
58
59struct acpibut_softc {
60	struct acpi_devnode *sc_node;	/* our ACPI devnode */
61	struct sysmon_pswitch sc_smpsw;	/* our sysmon glue */
62	int sc_flags;			/* see below */
63};
64
65static const char * const power_button_hid[] = {
66	"PNP0C0C",
67	NULL
68};
69
70static const char * const sleep_button_hid[] = {
71	"PNP0C0E",
72	NULL
73};
74
75#define	ACPIBUT_F_VERBOSE		0x01	/* verbose events */
76
77static int	acpibut_match(device_t, cfdata_t, void *);
78static void	acpibut_attach(device_t, device_t, void *);
79static int	acpibut_detach(device_t, int);
80static void	acpibut_pressed_event(void *);
81static void	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *);
82
83CFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
84    acpibut_match, acpibut_attach, acpibut_detach, NULL);
85
86/*
87 * acpibut_match:
88 *
89 *	Autoconfiguration `match' routine.
90 */
91static int
92acpibut_match(device_t parent, cfdata_t match, void *aux)
93{
94	struct acpi_attach_args *aa = aux;
95
96	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
97		return 0;
98
99	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid))
100		return 1;
101
102	if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid))
103		return 1;
104
105	return 0;
106}
107
108/*
109 * acpibut_attach:
110 *
111 *	Autoconfiguration `attach' routine.
112 */
113static void
114acpibut_attach(device_t parent, device_t self, void *aux)
115{
116	struct acpibut_softc *sc = device_private(self);
117	struct acpi_attach_args *aa = aux;
118	const char *desc;
119	ACPI_STATUS rv;
120
121	sc->sc_smpsw.smpsw_name = device_xname(self);
122
123	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) {
124		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
125		desc = "Power";
126	} else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) {
127		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
128		desc = "Sleep";
129	} else {
130		panic("acpibut_attach: impossible");
131	}
132
133	aprint_naive(": ACPI %s Button\n", desc);
134	aprint_normal(": ACPI %s Button\n", desc);
135
136	sc->sc_node = aa->aa_node;
137
138	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
139		aprint_error_dev(self, "unable to register with sysmon\n");
140		return;
141	}
142
143	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
144	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler, self);
145	if (ACPI_FAILURE(rv)) {
146		aprint_error_dev(self,
147		    "unable to register DEVICE NOTIFY handler: %s\n",
148		    AcpiFormatException(rv));
149		return;
150	}
151
152#ifdef ACPI_BUT_DEBUG
153	/* Display the current state when it changes. */
154	sc->sc_flags = ACPIBUT_F_VERBOSE;
155#endif
156
157	if (!pmf_device_register(self, NULL, NULL))
158		aprint_error_dev(self, "couldn't establish power handler\n");
159}
160
161/*
162 * acpibut_detach:
163 *
164 *	Autoconfiguration `detach' routine.
165 */
166static int
167acpibut_detach(device_t self, int flags)
168{
169	struct acpibut_softc *sc = device_private(self);
170	ACPI_STATUS rv;
171
172	rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
173	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler);
174
175	if (ACPI_FAILURE(rv))
176		return EBUSY;
177
178	pmf_device_deregister(self);
179	sysmon_pswitch_unregister(&sc->sc_smpsw);
180
181	return 0;
182}
183
184/*
185 * acpibut_pressed_event:
186 *
187 *	Deal with a button being pressed.
188 */
189static void
190acpibut_pressed_event(void *arg)
191{
192	device_t dv = arg;
193	struct acpibut_softc *sc = device_private(dv);
194
195	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
196		aprint_verbose_dev(dv, "button pressed\n");
197
198	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
199}
200
201/*
202 * acpibut_notify_handler:
203 *
204 *	Callback from ACPI interrupt handler to notify us of an event.
205 */
206static void
207acpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
208{
209	device_t dv = context;
210	ACPI_STATUS rv;
211
212	switch (notify) {
213	case ACPI_NOTIFY_S0PowerButtonPressed:
214#if 0
215	case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
216#endif
217#ifdef ACPI_BUT_DEBUG
218		aprint_debug_dev(dv, "received ButtonPressed message\n");
219#endif
220		rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
221		    acpibut_pressed_event, dv);
222		if (ACPI_FAILURE(rv))
223			aprint_error_dev(dv,
224			    "WARNING: unable to queue button pressed callback: %s\n",
225			    AcpiFormatException(rv));
226		break;
227
228	/* XXX ACPI_NOTIFY_DeviceWake?? */
229
230	default:
231		aprint_error_dev(dv, "received unknown notify message: 0x%x\n",
232		    notify);
233	}
234}
235
236#ifdef _MODULE
237
238MODULE(MODULE_CLASS_DRIVER, acpibut, NULL);
239CFDRIVER_DECL(acpibut, DV_DULL, NULL);
240
241static int acpibutloc[] = { -1 };
242extern struct cfattach acpibut_ca;
243
244static struct cfparent acpiparent = {
245	"acpinodebus", NULL, DVUNIT_ANY
246};
247
248static struct cfdata acpibut_cfdata[] = {
249	{
250		.cf_name = "acpibut",
251		.cf_atname = "acpibut",
252		.cf_unit = 0,
253		.cf_fstate = FSTATE_STAR,
254		.cf_loc = acpibutloc,
255		.cf_flags = 0,
256		.cf_pspec = &acpiparent,
257	},
258
259	{ NULL }
260};
261
262static int
263acpibut_modcmd(modcmd_t cmd, void *context)
264{
265	int err;
266
267	switch (cmd) {
268
269	case MODULE_CMD_INIT:
270
271		err = config_cfdriver_attach(&acpibut_cd);
272
273		if (err != 0)
274			return err;
275
276		err = config_cfattach_attach("acpibut", &acpibut_ca);
277
278		if (err != 0) {
279			config_cfdriver_detach(&acpibut_cd);
280			return err;
281		}
282
283		err = config_cfdata_attach(acpibut_cfdata, 1);
284
285		if (err != 0) {
286			config_cfattach_detach("acpibut", &acpibut_ca);
287			config_cfdriver_detach(&acpibut_cd);
288			return err;
289		}
290
291		return 0;
292
293	case MODULE_CMD_FINI:
294
295		err = config_cfdata_detach(acpibut_cfdata);
296
297		if (err != 0)
298			return err;
299
300		config_cfattach_detach("acpibut", &acpibut_ca);
301		config_cfdriver_detach(&acpibut_cd);
302
303		return 0;
304
305	default:
306		return ENOTTY;
307	}
308}
309
310#endif	/* _MODULE */
311