xref: /netbsd-src/sys/dev/acpi/acpi_button.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: acpi_button.c,v 1.2 2001/11/13 13:01:57 lukem Exp $	*/
2 
3 /*
4  * Copyright 2001 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.2 2001/11/13 13:01:57 lukem 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 #define	ACPI_BUT_DEBUG
54 
55 struct acpibut_softc {
56 	struct device sc_dev;		/* base device glue */
57 	struct acpi_devnode *sc_node;	/* our ACPI devnode */
58 	int sc_buttype;			/* button type */
59 	int sc_flags;			/* see below */
60 };
61 
62 #define	ACPIBUT_TYPE_POWER		0
63 #define	ACPIBUT_TYPE_SLEEP		1
64 
65 #define	ACPIBUT_F_VERBOSE		0x01	/* verbose events */
66 
67 int	acpibut_match(struct device *, struct cfdata *, void *);
68 void	acpibut_attach(struct device *, struct device *, void *);
69 
70 struct cfattach acpibut_ca = {
71 	sizeof(struct acpibut_softc), acpibut_match, acpibut_attach,
72 };
73 
74 void	acpibut_pressed_for_sleep(void *);
75 void	acpibut_pressed_for_wakeup(void *);
76 void	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *context);
77 
78 /*
79  * acpibut_match:
80  *
81  *	Autoconfiguration `match' routine.
82  */
83 int
84 acpibut_match(struct device *parent, struct cfdata *match, void *aux)
85 {
86 	struct acpi_attach_args *aa = aux;
87 
88 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
89 		return (0);
90 
91 	if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0C") == 0 ||
92 	    strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0E") == 0)
93 		return (1);
94 
95 	return (0);
96 }
97 
98 /*
99  * acpibut_attach:
100  *
101  *	Autoconfiguration `attach' routine.
102  */
103 void
104 acpibut_attach(struct device *parent, struct device *self, void *aux)
105 {
106 	struct acpibut_softc *sc = (void *) self;
107 	struct acpi_attach_args *aa = aux;
108 	const char *desc;
109 	ACPI_STATUS rv;
110 
111 	if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0C") == 0) {
112 		sc->sc_buttype = ACPIBUT_TYPE_POWER;
113 		desc = "Power";
114 	} else if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0E") == 0) {
115 		sc->sc_buttype = ACPIBUT_TYPE_SLEEP;
116 		desc = "Sleep";
117 	} else {
118 		printf("\n");
119 		panic("acpibut_attach: impossible");
120 	}
121 
122 	printf(": ACPI %s Button\n", desc);
123 
124 	sc->sc_node = aa->aa_node;
125 
126 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
127 	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler, sc);
128 	if (rv != AE_OK) {
129 		printf("%s: unable to register device notify handler: %d\n",
130 		    sc->sc_dev.dv_xname, rv);
131 		return;
132 	}
133 
134 #ifdef ACPI_BUT_DEBUG
135 	/* Display the current state when it changes. */
136 	sc->sc_flags = ACPIBUT_F_VERBOSE;
137 #endif
138 }
139 
140 /*
141  * acpibut_pressed_for_sleep:
142  *
143  *	Deal with a button being pressed for sleep.
144  */
145 void
146 acpibut_pressed_for_sleep(void *arg)
147 {
148 	struct acpibut_softc *sc = arg;
149 
150 	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
151 		printf("%s: button pressed for sleep\n",
152 		    sc->sc_dev.dv_xname);
153 
154 	/*
155 	 * XXX Perform the appropriate action.
156 	 */
157 }
158 
159 /*
160  * acpibut_pressed_for_wakeup:
161  *
162  *	Deal with a button being pressed for wakeup.
163  */
164 void
165 acpibut_pressed_for_wakeup(void *arg)
166 {
167 	struct acpibut_softc *sc = arg;
168 
169 	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
170 		printf("%s: button pressed for wakeup\n",
171 		    sc->sc_dev.dv_xname);
172 
173 	/*
174 	 * XXX Perform the appropriate action.
175 	 */
176 }
177 
178 /*
179  * acpibut_notify_handler:
180  *
181  *	Callback from ACPI interrupt handler to notify us of an event.
182  */
183 void
184 acpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
185 {
186 	struct acpibut_softc *sc = context;
187 	int rv;
188 
189 	switch (notify) {
190 	case ACPI_NOTIFY_S0PowerButtonPressed:
191 #if 0
192 	case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
193 #endif
194 #ifdef ACPI_BUT_DEBUG
195 		printf("%s: received ButtonPressed message\n",
196 		    sc->sc_dev.dv_xname);
197 #endif
198 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
199 		    acpibut_pressed_for_sleep, sc);
200 		if (rv != AE_OK)
201 			printf("%s: WARNING: unable to queue button pressed "
202 			    "event: %d\n", sc->sc_dev.dv_xname, rv);
203 		break;
204 
205 	/* XXX ACPI_NOTIFY_DeviceWake?? */
206 
207 	default:
208 		printf("%s: received unknown notify message: 0x%x\n",
209 		    sc->sc_dev.dv_xname, notify);
210 	}
211 }
212