acpi_button.c revision 1.17
1/*	$NetBSD: acpi_button.c,v 1.17 2005/05/02 14:54:00 kochi 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.17 2005/05/02 14:54:00 kochi 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 device sc_dev;		/* base device glue */
57	struct acpi_devnode *sc_node;	/* our ACPI devnode */
58	struct sysmon_pswitch sc_smpsw;	/* our sysmon glue */
59	int sc_flags;			/* see below */
60};
61
62static const char * const power_button_hid[] = {
63	"PNP0C0C",
64	NULL
65};
66
67static const char * const sleep_button_hid[] = {
68	"PNP0C0E",
69	NULL
70};
71
72#define	ACPIBUT_F_VERBOSE		0x01	/* verbose events */
73
74static int	acpibut_match(struct device *, struct cfdata *, void *);
75static void	acpibut_attach(struct device *, struct device *, void *);
76
77CFATTACH_DECL(acpibut, sizeof(struct acpibut_softc),
78    acpibut_match, acpibut_attach, NULL, NULL);
79
80static void	acpibut_pressed_event(void *);
81static void	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *context);
82
83/*
84 * acpibut_match:
85 *
86 *	Autoconfiguration `match' routine.
87 */
88static int
89acpibut_match(struct device *parent, struct cfdata *match, void *aux)
90{
91	struct acpi_attach_args *aa = aux;
92
93	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
94		return 0;
95
96	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid))
97		return 1;
98
99	if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid))
100		return 1;
101
102	return 0;
103}
104
105/*
106 * acpibut_attach:
107 *
108 *	Autoconfiguration `attach' routine.
109 */
110static void
111acpibut_attach(struct device *parent, struct device *self, void *aux)
112{
113	struct acpibut_softc *sc = (void *) self;
114	struct acpi_attach_args *aa = aux;
115	const char *desc;
116	ACPI_STATUS rv;
117
118	sc->sc_smpsw.smpsw_name = sc->sc_dev.dv_xname;
119
120	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) {
121		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
122		desc = "Power";
123	} else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) {
124		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
125		desc = "Sleep";
126	} else {
127		printf("\n");
128		panic("acpibut_attach: impossible");
129	}
130
131	printf(": ACPI %s Button\n", desc);
132
133	sc->sc_node = aa->aa_node;
134
135	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
136		printf("%s: unable to register with sysmon\n",
137		    sc->sc_dev.dv_xname);
138		return;
139	}
140
141	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
142	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler, sc);
143	if (ACPI_FAILURE(rv)) {
144		printf("%s: unable to register DEVICE NOTIFY handler: %s\n",
145		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
146		return;
147	}
148
149	acpi_set_wake_gpe(sc->sc_node->ad_handle);
150
151#ifdef ACPI_BUT_DEBUG
152	/* Display the current state when it changes. */
153	sc->sc_flags = ACPIBUT_F_VERBOSE;
154#endif
155}
156
157/*
158 * acpibut_pressed_event:
159 *
160 *	Deal with a button being pressed.
161 */
162static void
163acpibut_pressed_event(void *arg)
164{
165	struct acpibut_softc *sc = arg;
166
167	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
168		printf("%s: button pressed\n", sc->sc_dev.dv_xname);
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	struct acpibut_softc *sc = 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		printf("%s: received ButtonPressed message\n",
191		    sc->sc_dev.dv_xname);
192#endif
193		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
194		    acpibut_pressed_event, sc);
195		if (ACPI_FAILURE(rv))
196			printf("%s: WARNING: unable to queue button pressed "
197			    "callback: %s\n", sc->sc_dev.dv_xname,
198			    AcpiFormatException(rv));
199		break;
200
201	/* XXX ACPI_NOTIFY_DeviceWake?? */
202
203	default:
204		printf("%s: received unknown notify message: 0x%x\n",
205		    sc->sc_dev.dv_xname, notify);
206	}
207}
208