acpi_button.c revision 1.24
1/*	$NetBSD: acpi_button.c,v 1.24 2007/10/24 07:05:35 joerg 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.24 2007/10/24 07:05:35 joerg Exp $");
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/device.h>
48
49#include <dev/acpi/acpica.h>
50#include <dev/acpi/acpireg.h>
51#include <dev/acpi/acpivar.h>
52
53#include <dev/sysmon/sysmonvar.h>
54
55struct acpibut_softc {
56	struct acpi_devnode *sc_node;	/* our ACPI devnode */
57	struct sysmon_pswitch sc_smpsw;	/* our sysmon glue */
58	int sc_flags;			/* see below */
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
71#define	ACPIBUT_F_VERBOSE		0x01	/* verbose events */
72
73static int	acpibut_match(device_t, struct cfdata *, void *);
74static void	acpibut_attach(device_t, device_t, void *);
75
76CFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
77    acpibut_match, acpibut_attach, NULL, NULL);
78
79static void	acpibut_pressed_event(void *);
80static void	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *);
81
82/*
83 * acpibut_match:
84 *
85 *	Autoconfiguration `match' routine.
86 */
87static int
88acpibut_match(device_t parent, struct cfdata *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	ACPI_STATUS rv;
116
117	sc->sc_smpsw.smpsw_name = device_xname(self);
118
119	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) {
120		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
121		desc = "Power";
122	} else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) {
123		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
124		desc = "Sleep";
125	} else {
126		panic("acpibut_attach: impossible");
127	}
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
134	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
135		aprint_error_dev(self, "unable to register with sysmon\n");
136		return;
137	}
138
139	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
140	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler, self);
141	if (ACPI_FAILURE(rv)) {
142		aprint_error_dev(self,
143		    "unable to register DEVICE NOTIFY handler: %s\n",
144		    AcpiFormatException(rv));
145		return;
146	}
147
148	acpi_set_wake_gpe(sc->sc_node->ad_handle);
149
150#ifdef ACPI_BUT_DEBUG
151	/* Display the current state when it changes. */
152	sc->sc_flags = ACPIBUT_F_VERBOSE;
153#endif
154}
155
156/*
157 * acpibut_pressed_event:
158 *
159 *	Deal with a button being pressed.
160 */
161static void
162acpibut_pressed_event(void *arg)
163{
164	device_t dv = arg;
165	struct acpibut_softc *sc = device_private(dv);
166
167	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
168		aprint_verbose_dev(dv, "button pressed\n");
169
170	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
171}
172
173/*
174 * acpibut_notify_handler:
175 *
176 *	Callback from ACPI interrupt handler to notify us of an event.
177 */
178static void
179acpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
180{
181	device_t dv = context;
182	int rv;
183
184	switch (notify) {
185	case ACPI_NOTIFY_S0PowerButtonPressed:
186#if 0
187	case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
188#endif
189#ifdef ACPI_BUT_DEBUG
190		aprint_debug_dev(dv, "received ButtonPressed message\n");
191#endif
192		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
193		    acpibut_pressed_event, dv);
194		if (ACPI_FAILURE(rv))
195			aprint_error_dev(dv,
196			    "WARNING: unable to queue button pressed callback: %s\n",
197			    AcpiFormatException(rv));
198		break;
199
200	/* XXX ACPI_NOTIFY_DeviceWake?? */
201
202	default:
203		aprint_error_dev(dv, "received unknown notify message: 0x%x\n",
204		    notify);
205	}
206}
207