xref: /netbsd-src/sys/dev/acpi/acpi_button.c (revision 3816d47b2c42fcd6e549e3407f842a5b1a1d23ad)
1 /*	$NetBSD: acpi_button.c,v 1.28 2009/08/25 10:34:08 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.28 2009/08/25 10:34:08 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, cfdata_t, 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, cfdata_t 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 #ifdef ACPI_BUT_DEBUG
149 	/* Display the current state when it changes. */
150 	sc->sc_flags = ACPIBUT_F_VERBOSE;
151 #endif
152 
153 	if (!pmf_device_register(self, NULL, NULL))
154 		aprint_error_dev(self, "couldn't establish power handler\n");
155 }
156 
157 /*
158  * acpibut_pressed_event:
159  *
160  *	Deal with a button being pressed.
161  */
162 static void
163 acpibut_pressed_event(void *arg)
164 {
165 	device_t dv = arg;
166 	struct acpibut_softc *sc = device_private(dv);
167 
168 	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
169 		aprint_verbose_dev(dv, "button pressed\n");
170 
171 	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
172 }
173 
174 /*
175  * acpibut_notify_handler:
176  *
177  *	Callback from ACPI interrupt handler to notify us of an event.
178  */
179 static void
180 acpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
181 {
182 	device_t dv = context;
183 	ACPI_STATUS rv;
184 
185 	switch (notify) {
186 	case ACPI_NOTIFY_S0PowerButtonPressed:
187 #if 0
188 	case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
189 #endif
190 #ifdef ACPI_BUT_DEBUG
191 		aprint_debug_dev(dv, "received ButtonPressed message\n");
192 #endif
193 		rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
194 		    acpibut_pressed_event, dv);
195 		if (ACPI_FAILURE(rv))
196 			aprint_error_dev(dv,
197 			    "WARNING: unable to queue button pressed callback: %s\n",
198 			    AcpiFormatException(rv));
199 		break;
200 
201 	/* XXX ACPI_NOTIFY_DeviceWake?? */
202 
203 	default:
204 		aprint_error_dev(dv, "received unknown notify message: 0x%x\n",
205 		    notify);
206 	}
207 }
208