xref: /netbsd-src/sys/dev/acpi/acpi_button.c (revision d20841bb642898112fe68f0ad3f7b26dddf56f07)
1 /*	$NetBSD: acpi_button.c,v 1.13 2003/11/03 18:07:10 mycroft 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.13 2003/11/03 18:07:10 mycroft 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 button_hid[] = {
63 	"PNP0C0C",
64 	"PNP0C0E",
65 	NULL
66 };
67 
68 #define	ACPIBUT_F_VERBOSE		0x01	/* verbose events */
69 
70 int	acpibut_match(struct device *, struct cfdata *, void *);
71 void	acpibut_attach(struct device *, struct device *, void *);
72 
73 CFATTACH_DECL(acpibut, sizeof(struct acpibut_softc),
74     acpibut_match, acpibut_attach, NULL, NULL);
75 
76 void	acpibut_pressed_event(void *);
77 void	acpibut_notify_handler(ACPI_HANDLE, UINT32, void *context);
78 
79 /*
80  * acpibut_match:
81  *
82  *	Autoconfiguration `match' routine.
83  */
84 int
85 acpibut_match(struct device *parent, struct cfdata *match, void *aux)
86 {
87 	struct acpi_attach_args *aa = aux;
88 
89 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
90 		return (0);
91 
92 	return (acpi_match_hid(aa->aa_node->ad_devinfo, button_hid));
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 	sc->sc_smpsw.smpsw_name = sc->sc_dev.dv_xname;
109 
110 	if (strcmp(aa->aa_node->ad_devinfo->HardwareId.Value, "PNP0C0C") == 0) {
111 		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
112 		desc = "Power";
113 	} else if (strcmp(aa->aa_node->ad_devinfo->HardwareId.Value, "PNP0C0E") == 0) {
114 		sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_SLEEP;
115 		desc = "Sleep";
116 	} else {
117 		printf("\n");
118 		panic("acpibut_attach: impossible");
119 	}
120 
121 	printf(": ACPI %s Button\n", desc);
122 
123 	sc->sc_node = aa->aa_node;
124 
125 	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
126 		printf("%s: unable to register with sysmon\n",
127 		    sc->sc_dev.dv_xname);
128 		return;
129 	}
130 
131 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
132 	    ACPI_DEVICE_NOTIFY, acpibut_notify_handler, sc);
133 	if (ACPI_FAILURE(rv)) {
134 		printf("%s: unable to register DEVICE NOTIFY handler: %s\n",
135 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
136 		return;
137 	}
138 
139 #ifdef ACPI_BUT_DEBUG
140 	/* Display the current state when it changes. */
141 	sc->sc_flags = ACPIBUT_F_VERBOSE;
142 #endif
143 }
144 
145 /*
146  * acpibut_pressed_event:
147  *
148  *	Deal with a button being pressed.
149  */
150 void
151 acpibut_pressed_event(void *arg)
152 {
153 	struct acpibut_softc *sc = arg;
154 
155 	if (sc->sc_flags & ACPIBUT_F_VERBOSE)
156 		printf("%s: button pressed\n", sc->sc_dev.dv_xname);
157 
158 	sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
159 }
160 
161 /*
162  * acpibut_notify_handler:
163  *
164  *	Callback from ACPI interrupt handler to notify us of an event.
165  */
166 void
167 acpibut_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
168 {
169 	struct acpibut_softc *sc = context;
170 	int rv;
171 
172 	switch (notify) {
173 	case ACPI_NOTIFY_S0PowerButtonPressed:
174 #if 0
175 	case ACPI_NOTIFY_S0SleepButtonPressed: /* same as above */
176 #endif
177 #ifdef ACPI_BUT_DEBUG
178 		printf("%s: received ButtonPressed message\n",
179 		    sc->sc_dev.dv_xname);
180 #endif
181 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
182 		    acpibut_pressed_event, sc);
183 		if (ACPI_FAILURE(rv))
184 			printf("%s: WARNING: unable to queue button pressed "
185 			    "callback: %s\n", sc->sc_dev.dv_xname,
186 			    AcpiFormatException(rv));
187 		break;
188 
189 	/* XXX ACPI_NOTIFY_DeviceWake?? */
190 
191 	default:
192 		printf("%s: received unknown notify message: 0x%x\n",
193 		    sc->sc_dev.dv_xname, notify);
194 	}
195 }
196