acpi_button.c revision 1.9
1/*	$NetBSD: acpi_button.c,v 1.9 2003/04/18 01:31:34 thorpej 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.9 2003/04/18 01:31:34 thorpej 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
62#define	ACPIBUT_F_VERBOSE		0x01	/* verbose events */
63
64int	acpibut_match(struct device *, struct cfdata *, void *);
65void	acpibut_attach(struct device *, struct device *, void *);
66
67CFATTACH_DECL(acpibut, sizeof(struct acpibut_softc),
68    acpibut_match, acpibut_attach, NULL, NULL);
69
70void	acpibut_pressed_event(void *);
71void	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *context);
72
73/*
74 * acpibut_match:
75 *
76 *	Autoconfiguration `match' routine.
77 */
78int
79acpibut_match(struct device *parent, struct cfdata *match, void *aux)
80{
81	struct acpi_attach_args *aa = aux;
82
83	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
84		return (0);
85
86	if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0C") == 0 ||
87	    strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0E") == 0)
88		return (1);
89
90	return (0);
91}
92
93/*
94 * acpibut_attach:
95 *
96 *	Autoconfiguration `attach' routine.
97 */
98void
99acpibut_attach(struct device *parent, struct device *self, void *aux)
100{
101	struct acpibut_softc *sc = (void *) self;
102	struct acpi_attach_args *aa = aux;
103	const char *desc;
104	ACPI_STATUS rv;
105
106	sc->sc_smpsw.smpsw_name = sc->sc_dev.dv_xname;
107
108	if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0C") == 0) {
109		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
110		desc = "Power";
111	} else if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0E") == 0) {
112		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
113		desc = "Sleep";
114	} else {
115		printf("\n");
116		panic("acpibut_attach: impossible");
117	}
118
119	printf(": ACPI %s Button\n", desc);
120
121	sc->sc_node = aa->aa_node;
122
123	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
124		printf("%s: unable to register with sysmon\n",
125		    sc->sc_dev.dv_xname);
126		return;
127	}
128
129	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
130	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler, sc);
131	if (rv != AE_OK) {
132		printf("%s: unable to register device notify handler: %d\n",
133		    sc->sc_dev.dv_xname, rv);
134		return;
135	}
136
137#ifdef ACPI_BUT_DEBUG
138	/* Display the current state when it changes. */
139	sc->sc_flags = ACPIBUT_F_VERBOSE;
140#endif
141}
142
143/*
144 * acpibut_pressed_event:
145 *
146 *	Deal with a button being pressed.
147 */
148void
149acpibut_pressed_event(void *arg)
150{
151	struct acpibut_softc *sc = arg;
152
153	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
154		printf("%s: button pressed\n", sc->sc_dev.dv_xname);
155
156	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
157}
158
159/*
160 * acpibut_notify_handler:
161 *
162 *	Callback from ACPI interrupt handler to notify us of an event.
163 */
164void
165acpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
166{
167	struct acpibut_softc *sc = context;
168	int rv;
169
170	switch (notify) {
171	case ACPI_NOTIFY_S0PowerButtonPressed:
172#if 0
173	case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
174#endif
175#ifdef ACPI_BUT_DEBUG
176		printf("%s: received ButtonPressed message\n",
177		    sc->sc_dev.dv_xname);
178#endif
179		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
180		    acpibut_pressed_event, sc);
181		if (rv != AE_OK)
182			printf("%s: WARNING: unable to queue button pressed "
183			    "callback: %d\n", sc->sc_dev.dv_xname, rv);
184		break;
185
186	/* XXX ACPI_NOTIFY_DeviceWake?? */
187
188	default:
189		printf("%s: received unknown notify message: 0x%x\n",
190		    sc->sc_dev.dv_xname, notify);
191	}
192}
193