xref: /netbsd-src/sys/dev/acpi/acpi_button.c (revision eb961d0e02b7a46a9acfa877b02df48df6637278)
1 /*	$NetBSD: acpi_button.c,v 1.20 2006/02/20 12:17:49 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.20 2006/02/20 12:17:49 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 
55 struct 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 static const char * const power_button_hid[] = {
63 	"PNP0C0C",
64 	NULL
65 };
66 
67 static const char * const sleep_button_hid[] = {
68 	"PNP0C0E",
69 	NULL
70 };
71 
72 #define	ACPIBUT_F_VERBOSE		0x01	/* verbose events */
73 
74 static int	acpibut_match(struct device *, struct cfdata *, void *);
75 static void	acpibut_attach(struct device *, struct device *, void *);
76 
77 CFATTACH_DECL(acpibut, sizeof(struct acpibut_softc),
78     acpibut_match, acpibut_attach, NULL, NULL);
79 
80 static void	acpibut_pressed_event(void *);
81 static void	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *);
82 
83 /*
84  * acpibut_match:
85  *
86  *	Autoconfiguration `match' routine.
87  */
88 static int
89 acpibut_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  */
110 static void
111 acpibut_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 	aprint_naive(": ACPI %s Button\n", desc);
132 	aprint_normal(": ACPI %s Button\n", desc);
133 
134 	sc->sc_node = aa->aa_node;
135 
136 	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
137 		aprint_error("%s: unable to register with sysmon\n",
138 		    sc->sc_dev.dv_xname);
139 		return;
140 	}
141 
142 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
143 	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler, sc);
144 	if (ACPI_FAILURE(rv)) {
145 		aprint_error("%s: unable to register DEVICE NOTIFY handler: %s\n",
146 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
147 		return;
148 	}
149 
150 	acpi_set_wake_gpe(sc->sc_node->ad_handle);
151 
152 #ifdef ACPI_BUT_DEBUG
153 	/* Display the current state when it changes. */
154 	sc->sc_flags = ACPIBUT_F_VERBOSE;
155 #endif
156 }
157 
158 /*
159  * acpibut_pressed_event:
160  *
161  *	Deal with a button being pressed.
162  */
163 static void
164 acpibut_pressed_event(void *arg)
165 {
166 	struct acpibut_softc *sc = arg;
167 
168 	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
169 		printf("%s: button pressed\n", sc->sc_dev.dv_xname);
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 	struct acpibut_softc *sc = context;
183 	int 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 		printf("%s: received ButtonPressed message\n",
192 		    sc->sc_dev.dv_xname);
193 #endif
194 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
195 		    acpibut_pressed_event, sc);
196 		if (ACPI_FAILURE(rv))
197 			printf("%s: WARNING: unable to queue button pressed "
198 			    "callback: %s\n", sc->sc_dev.dv_xname,
199 			    AcpiFormatException(rv));
200 		break;
201 
202 	/* XXX ACPI_NOTIFY_DeviceWake?? */
203 
204 	default:
205 		printf("%s: received unknown notify message: 0x%x\n",
206 		    sc->sc_dev.dv_xname, notify);
207 	}
208 }
209