xref: /netbsd-src/sys/dev/acpi/acpi_lid.c (revision 7f21db1c0118155e0dd40b75182e30c589d9f63e)
1 /*	$NetBSD: acpi_lid.c,v 1.32 2010/01/30 18:35:48 jruoho 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 Lid Switch driver.
40  */
41 
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: acpi_lid.c,v 1.32 2010/01/30 18:35:48 jruoho 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 #define _COMPONENT		ACPI_LID_COMPONENT
56 ACPI_MODULE_NAME		("acpi_lid")
57 
58 struct acpilid_softc {
59 	struct acpi_devnode *sc_node;	/* our ACPI devnode */
60 	struct sysmon_pswitch sc_smpsw;	/* our sysmon glue */
61 };
62 
63 static const char * const lid_hid[] = {
64 	"PNP0C0D",
65 	NULL
66 };
67 
68 static int	acpilid_match(device_t, cfdata_t, void *);
69 static void	acpilid_attach(device_t, device_t, void *);
70 static int	acpilid_detach(device_t, int);
71 
72 CFATTACH_DECL_NEW(acpilid, sizeof(struct acpilid_softc),
73     acpilid_match, acpilid_attach, acpilid_detach, NULL);
74 
75 static void	acpilid_status_changed(void *);
76 static void	acpilid_notify_handler(ACPI_HANDLE, UINT32, void *);
77 
78 static void	acpilid_wake_event(device_t, bool);
79 static bool	acpilid_suspend(device_t, pmf_qual_t);
80 
81 /*
82  * acpilid_match:
83  *
84  *	Autoconfiguration `match' routine.
85  */
86 static int
87 acpilid_match(device_t parent, cfdata_t match, void *aux)
88 {
89 	struct acpi_attach_args *aa = aux;
90 
91 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
92 		return 0;
93 
94 	return acpi_match_hid(aa->aa_node->ad_devinfo, lid_hid);
95 }
96 
97 /*
98  * acpilid_attach:
99  *
100  *	Autoconfiguration `attach' routine.
101  */
102 static void
103 acpilid_attach(device_t parent, device_t self, void *aux)
104 {
105 	struct acpilid_softc *sc = device_private(self);
106 	struct acpi_attach_args *aa = aux;
107 	ACPI_STATUS rv;
108 
109 	aprint_naive(": ACPI Lid Switch\n");
110 	aprint_normal(": ACPI Lid Switch\n");
111 
112 	sc->sc_node = aa->aa_node;
113 
114 	sc->sc_smpsw.smpsw_name = device_xname(self);
115 	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_LID;
116 	if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
117 		aprint_error_dev(self, "unable to register with sysmon\n");
118 		return;
119 	}
120 
121 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
122 	    ACPI_DEVICE_NOTIFY, acpilid_notify_handler, self);
123 	if (ACPI_FAILURE(rv)) {
124 		aprint_error_dev(self,
125 		    "unable to register DEVICE NOTIFY handler: %s\n",
126 		    AcpiFormatException(rv));
127 		return;
128 	}
129 
130 	if (!pmf_device_register(self, acpilid_suspend, NULL))
131 		aprint_error_dev(self, "couldn't establish power handler\n");
132 }
133 
134 static int
135 acpilid_detach(device_t self, int flags)
136 {
137 	struct acpilid_softc *sc = device_private(self);
138 	ACPI_STATUS rv;
139 
140 	rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
141 	    ACPI_DEVICE_NOTIFY, acpilid_notify_handler);
142 	if (ACPI_FAILURE(rv)) {
143 		aprint_error_dev(self,
144 		    "unable to deregister DEVICE NOTIFY handler: %s\n",
145 		    AcpiFormatException(rv));
146 		return EBUSY;
147 	}
148 
149 	pmf_device_deregister(self);
150 	sysmon_pswitch_unregister(&sc->sc_smpsw);
151 
152 	return 0;
153 }
154 
155 static void
156 acpilid_wake_event(device_t dv, bool enable)
157 {
158 	struct acpilid_softc *sc = device_private(dv);
159 	ACPI_OBJECT_LIST arg;
160 	ACPI_OBJECT obj[3];
161 	ACPI_STATUS rv;
162 
163 	/*
164 	 * First try to call the Device Sleep Wake control method, _DSW.
165 	 * Only if this is not available, resort to to the Power State
166 	 * Wake control method, _PSW, which was deprecated in ACPI 3.0.
167 	 */
168 	obj[0].Integer.Value = enable ? 1 : 0;
169 	obj[1].Integer.Value = obj[2].Integer.Value = 0;
170 	obj[0].Type = obj[1].Type = obj[2].Type = ACPI_TYPE_INTEGER;
171 
172 	arg.Count = 3;
173 	arg.Pointer = obj;
174 
175 	rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "_DSW", &arg, NULL);
176 
177 	if (ACPI_SUCCESS(rv))
178 		return;
179 
180 	if (rv != AE_NOT_FOUND)
181 		goto fail;
182 
183 	rv = acpi_eval_set_integer(sc->sc_node->ad_handle, "_PSW",
184 	    enable ? 1 : 0);
185 
186 	if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
187 		goto fail;
188 
189 	return;
190 
191 fail:
192 	aprint_error_dev(dv, "unable to evaluate wake control method: %s\n",
193 	    AcpiFormatException(rv));
194 }
195 
196 /*
197  * acpilid_status_changed:
198  *
199  *	Get, and possibly display, the lid status, and take action.
200  */
201 static void
202 acpilid_status_changed(void *arg)
203 {
204 	struct acpilid_softc *sc = arg;
205 	ACPI_INTEGER status;
206 	ACPI_STATUS rv;
207 
208 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_LID", &status);
209 	if (ACPI_FAILURE(rv))
210 		return;
211 
212 	sysmon_pswitch_event(&sc->sc_smpsw, status == 0 ?
213 	    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
214 }
215 
216 /*
217  * acpilid_notify_handler:
218  *
219  *	Callback from ACPI interrupt handler to notify us of an event.
220  */
221 static void
222 acpilid_notify_handler(ACPI_HANDLE handle, UINT32 notify, void *context)
223 {
224 	device_t dv = context;
225 	struct acpilid_softc *sc = device_private(dv);
226 	ACPI_STATUS rv;
227 
228 	switch (notify) {
229 	case ACPI_NOTIFY_LidStatusChanged:
230 #ifdef ACPI_LID_DEBUG
231 		printf("%s: received LidStatusChanged message\n",
232 		    device_xname(dv));
233 #endif
234 		rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
235 		    acpilid_status_changed, sc);
236 		if (ACPI_FAILURE(rv))
237 			aprint_error_dev(dv,
238 			    "WARNING: unable to queue lid change "
239 			    "callback: %s\n", AcpiFormatException(rv));
240 		break;
241 
242 	default:
243 		aprint_debug_dev(dv,
244 		    "received unknown notify message: 0x%x\n", notify);
245 	}
246 }
247 
248 static bool
249 acpilid_suspend(device_t dv, pmf_qual_t qual)
250 {
251 	struct acpilid_softc *sc = device_private(dv);
252 	ACPI_INTEGER status;
253 	ACPI_STATUS rv;
254 
255 	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_LID", &status);
256 	if (ACPI_FAILURE(rv))
257 		return true;
258 
259 	acpilid_wake_event(dv, status == 0);
260 	return true;
261 }
262