acpi_button.c revision 1.15
1/*	$NetBSD: acpi_button.c,v 1.15 2004/04/11 11:25:34 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.15 2004/04/11 11:25:34 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
74int	acpibut_match(struct device *, struct cfdata *, void *);
75void	acpibut_attach(struct device *, struct device *, void *);
76
77CFATTACH_DECL(acpibut, sizeof(struct acpibut_softc),
78    acpibut_match, acpibut_attach, NULL, NULL);
79
80void	acpibut_pressed_event(void *);
81void	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *context);
82
83/*
84 * acpibut_match:
85 *
86 *	Autoconfiguration `match' routine.
87 */
88int
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 */
110void
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#ifdef ACPI_BUT_DEBUG
150	/* Display the current state when it changes. */
151	sc->sc_flags = ACPIBUT_F_VERBOSE;
152#endif
153}
154
155/*
156 * acpibut_pressed_event:
157 *
158 *	Deal with a button being pressed.
159 */
160void
161acpibut_pressed_event(void *arg)
162{
163	struct acpibut_softc *sc = arg;
164
165	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
166		printf("%s: button pressed\n", sc->sc_dev.dv_xname);
167
168	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
169}
170
171/*
172 * acpibut_notify_handler:
173 *
174 *	Callback from ACPI interrupt handler to notify us of an event.
175 */
176void
177acpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
178{
179	struct acpibut_softc *sc = context;
180	int rv;
181
182	switch (notify) {
183	case ACPI_NOTIFY_S0PowerButtonPressed:
184#if 0
185	case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
186#endif
187#ifdef ACPI_BUT_DEBUG
188		printf("%s: received ButtonPressed message\n",
189		    sc->sc_dev.dv_xname);
190#endif
191		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
192		    acpibut_pressed_event, sc);
193		if (ACPI_FAILURE(rv))
194			printf("%s: WARNING: unable to queue button pressed "
195			    "callback: %s\n", sc->sc_dev.dv_xname,
196			    AcpiFormatException(rv));
197		break;
198
199	/* XXX ACPI_NOTIFY_DeviceWake?? */
200
201	default:
202		printf("%s: received unknown notify message: 0x%x\n",
203		    sc->sc_dev.dv_xname, notify);
204	}
205}
206