acpi_button.c revision 1.29
1/*	$NetBSD: acpi_button.c,v 1.29 2010/01/30 18:35:48 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.29 2010/01/30 18:35:48 jruoho 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
55#define _COMPONENT		ACPI_BUTTON_COMPONENT
56ACPI_MODULE_NAME		("acpi_button")
57
58struct acpibut_softc {
59	struct acpi_devnode *sc_node;	/* our ACPI devnode */
60	struct sysmon_pswitch sc_smpsw;	/* our sysmon glue */
61	int sc_flags;			/* see below */
62};
63
64static const char * const power_button_hid[] = {
65	"PNP0C0C",
66	NULL
67};
68
69static const char * const sleep_button_hid[] = {
70	"PNP0C0E",
71	NULL
72};
73
74#define	ACPIBUT_F_VERBOSE		0x01	/* verbose events */
75
76static int	acpibut_match(device_t, cfdata_t, void *);
77static void	acpibut_attach(device_t, device_t, void *);
78
79CFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
80    acpibut_match, acpibut_attach, NULL, NULL);
81
82static void	acpibut_pressed_event(void *);
83static void	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *);
84
85/*
86 * acpibut_match:
87 *
88 *	Autoconfiguration `match' routine.
89 */
90static int
91acpibut_match(device_t parent, cfdata_t match, void *aux)
92{
93	struct acpi_attach_args *aa = aux;
94
95	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
96		return 0;
97
98	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid))
99		return 1;
100
101	if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid))
102		return 1;
103
104	return 0;
105}
106
107/*
108 * acpibut_attach:
109 *
110 *	Autoconfiguration `attach' routine.
111 */
112static void
113acpibut_attach(device_t parent, device_t self, void *aux)
114{
115	struct acpibut_softc *sc = device_private(self);
116	struct acpi_attach_args *aa = aux;
117	const char *desc;
118	ACPI_STATUS rv;
119
120	sc->sc_smpsw.smpsw_name = device_xname(self);
121
122	if (acpi_match_hid(aa->aa_node->ad_devinfo, power_button_hid)) {
123		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
124		desc = "Power";
125	} else if (acpi_match_hid(aa->aa_node->ad_devinfo, sleep_button_hid)) {
126		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
127		desc = "Sleep";
128	} else {
129		panic("acpibut_attach: impossible");
130	}
131
132	aprint_naive(": ACPI %s Button\n", desc);
133	aprint_normal(": ACPI %s Button\n", desc);
134
135	sc->sc_node = aa->aa_node;
136
137	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
138		aprint_error_dev(self, "unable to register with sysmon\n");
139		return;
140	}
141
142	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
143	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler, self);
144	if (ACPI_FAILURE(rv)) {
145		aprint_error_dev(self,
146		    "unable to register DEVICE NOTIFY handler: %s\n",
147		    AcpiFormatException(rv));
148		return;
149	}
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	if (!pmf_device_register(self, NULL, NULL))
157		aprint_error_dev(self, "couldn't establish power handler\n");
158}
159
160/*
161 * acpibut_pressed_event:
162 *
163 *	Deal with a button being pressed.
164 */
165static void
166acpibut_pressed_event(void *arg)
167{
168	device_t dv = arg;
169	struct acpibut_softc *sc = device_private(dv);
170
171	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
172		aprint_verbose_dev(dv, "button pressed\n");
173
174	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
175}
176
177/*
178 * acpibut_notify_handler:
179 *
180 *	Callback from ACPI interrupt handler to notify us of an event.
181 */
182static void
183acpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
184{
185	device_t dv = context;
186	ACPI_STATUS rv;
187
188	switch (notify) {
189	case ACPI_NOTIFY_S0PowerButtonPressed:
190#if 0
191	case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
192#endif
193#ifdef ACPI_BUT_DEBUG
194		aprint_debug_dev(dv, "received ButtonPressed message\n");
195#endif
196		rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
197		    acpibut_pressed_event, dv);
198		if (ACPI_FAILURE(rv))
199			aprint_error_dev(dv,
200			    "WARNING: unable to queue button pressed callback: %s\n",
201			    AcpiFormatException(rv));
202		break;
203
204	/* XXX ACPI_NOTIFY_DeviceWake?? */
205
206	default:
207		aprint_error_dev(dv, "received unknown notify message: 0x%x\n",
208		    notify);
209	}
210}
211