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