xref: /netbsd/sys/dev/acpi/acpi_button.c (revision c4a72b64)
1 /*	$NetBSD: acpi_button.c,v 1.6 2002/10/02 16:33:36 thorpej 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.6 2002/10/02 16:33:36 thorpej 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 CFATTACH_DECL(acpibut, sizeof(struct acpibut_softc),
69     acpibut_match, acpibut_attach, NULL, NULL);
70 
71 void	acpibut_pressed_for_sleep(void *);
72 void	acpibut_pressed_for_wakeup(void *);
73 void	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *context);
74 
75 /*
76  * acpibut_match:
77  *
78  *	Autoconfiguration `match' routine.
79  */
80 int
81 acpibut_match(struct device *parent, struct cfdata *match, void *aux)
82 {
83 	struct acpi_attach_args *aa = aux;
84 
85 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
86 		return (0);
87 
88 	if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0C") == 0 ||
89 	    strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0E") == 0)
90 		return (1);
91 
92 	return (0);
93 }
94 
95 /*
96  * acpibut_attach:
97  *
98  *	Autoconfiguration `attach' routine.
99  */
100 void
101 acpibut_attach(struct device *parent, struct device *self, void *aux)
102 {
103 	struct acpibut_softc *sc = (void *) self;
104 	struct acpi_attach_args *aa = aux;
105 	const char *desc;
106 	ACPI_STATUS rv;
107 
108 	if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0C") == 0) {
109 		sc->sc_buttype = ACPIBUT_TYPE_POWER;
110 		desc = "Power";
111 	} else if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C0E") == 0) {
112 		sc->sc_buttype = ACPIBUT_TYPE_SLEEP;
113 		desc = "Sleep";
114 	} else {
115 		printf("\n");
116 		panic("acpibut_attach: impossible");
117 	}
118 
119 	printf(": ACPI %s Button\n", desc);
120 
121 	sc->sc_node = aa->aa_node;
122 
123 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
124 	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler, sc);
125 	if (rv != AE_OK) {
126 		printf("%s: unable to register device notify handler: %d\n",
127 		    sc->sc_dev.dv_xname, rv);
128 		return;
129 	}
130 
131 #ifdef ACPI_BUT_DEBUG
132 	/* Display the current state when it changes. */
133 	sc->sc_flags = ACPIBUT_F_VERBOSE;
134 #endif
135 }
136 
137 /*
138  * acpibut_pressed_for_sleep:
139  *
140  *	Deal with a button being pressed for sleep.
141  */
142 void
143 acpibut_pressed_for_sleep(void *arg)
144 {
145 	struct acpibut_softc *sc = arg;
146 
147 	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
148 		printf("%s: button pressed for sleep\n",
149 		    sc->sc_dev.dv_xname);
150 
151 	/*
152 	 * XXX Perform the appropriate action.
153 	 */
154 }
155 
156 /*
157  * acpibut_pressed_for_wakeup:
158  *
159  *	Deal with a button being pressed for wakeup.
160  */
161 void
162 acpibut_pressed_for_wakeup(void *arg)
163 {
164 	struct acpibut_softc *sc = arg;
165 
166 	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
167 		printf("%s: button pressed for wakeup\n",
168 		    sc->sc_dev.dv_xname);
169 
170 	/*
171 	 * XXX Perform the appropriate action.
172 	 */
173 }
174 
175 /*
176  * acpibut_notify_handler:
177  *
178  *	Callback from ACPI interrupt handler to notify us of an event.
179  */
180 void
181 acpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
182 {
183 	struct acpibut_softc *sc = context;
184 	int rv;
185 
186 	switch (notify) {
187 	case ACPI_NOTIFY_S0PowerButtonPressed:
188 #if 0
189 	case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
190 #endif
191 #ifdef ACPI_BUT_DEBUG
192 		printf("%s: received ButtonPressed message\n",
193 		    sc->sc_dev.dv_xname);
194 #endif
195 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
196 		    acpibut_pressed_for_sleep, sc);
197 		if (rv != AE_OK)
198 			printf("%s: WARNING: unable to queue button pressed "
199 			    "event: %d\n", sc->sc_dev.dv_xname, 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