xref: /netbsd-src/sys/dev/acpi/acpi_button.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: acpi_button.c,v 1.25 2007/12/09 20:27:52 jmcneill 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.25 2007/12/09 20:27:52 jmcneill 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 struct 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 
61 static const char * const power_button_hid[] = {
62 	"PNP0C0C",
63 	NULL
64 };
65 
66 static const char * const sleep_button_hid[] = {
67 	"PNP0C0E",
68 	NULL
69 };
70 
71 #define	ACPIBUT_F_VERBOSE		0x01	/* verbose events */
72 
73 static int	acpibut_match(device_t, struct cfdata *, void *);
74 static void	acpibut_attach(device_t, device_t, void *);
75 
76 CFATTACH_DECL_NEW(acpibut, sizeof(struct acpibut_softc),
77     acpibut_match, acpibut_attach, NULL, NULL);
78 
79 static void	acpibut_pressed_event(void *);
80 static void	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *);
81 
82 /*
83  * acpibut_match:
84  *
85  *	Autoconfiguration `match' routine.
86  */
87 static int
88 acpibut_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  */
109 static void
110 acpibut_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 	if (!pmf_device_register(self, NULL, NULL))
156 		aprint_error_dev(self, "couldn't establish power handler\n");
157 }
158 
159 /*
160  * acpibut_pressed_event:
161  *
162  *	Deal with a button being pressed.
163  */
164 static void
165 acpibut_pressed_event(void *arg)
166 {
167 	device_t dv = arg;
168 	struct acpibut_softc *sc = device_private(dv);
169 
170 	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
171 		aprint_verbose_dev(dv, "button pressed\n");
172 
173 	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
174 }
175 
176 /*
177  * acpibut_notify_handler:
178  *
179  *	Callback from ACPI interrupt handler to notify us of an event.
180  */
181 static void
182 acpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
183 {
184 	device_t dv = context;
185 	int rv;
186 
187 	switch (notify) {
188 	case ACPI_NOTIFY_S0PowerButtonPressed:
189 #if 0
190 	case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
191 #endif
192 #ifdef ACPI_BUT_DEBUG
193 		aprint_debug_dev(dv, "received ButtonPressed message\n");
194 #endif
195 		rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
196 		    acpibut_pressed_event, dv);
197 		if (ACPI_FAILURE(rv))
198 			aprint_error_dev(dv,
199 			    "WARNING: unable to queue button pressed callback: %s\n",
200 			    AcpiFormatException(rv));
201 		break;
202 
203 	/* XXX ACPI_NOTIFY_DeviceWake?? */
204 
205 	default:
206 		aprint_error_dev(dv, "received unknown notify message: 0x%x\n",
207 		    notify);
208 	}
209 }
210