xref: /freebsd/sys/dev/acpica/acpi_lid.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org>
3  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4  * Copyright (c) 2000 Michael Smith <msmith@freebd.org>
5  * Copyright (c) 2000 BSDi
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include "opt_acpi.h"
32 #include "opt_evdev.h"
33 #include <sys/param.h>
34 #include <sys/eventhandler.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/proc.h>
39 
40 #include <contrib/dev/acpica/include/acpi.h>
41 #include <contrib/dev/acpica/include/accommon.h>
42 
43 #include <dev/acpica/acpivar.h>
44 
45 #ifdef EVDEV_SUPPORT
46 #include <dev/evdev/input.h>
47 #include <dev/evdev/evdev.h>
48 #endif
49 
50 /* Hooks for the ACPI CA debugging infrastructure */
51 #define _COMPONENT	ACPI_BUTTON
52 ACPI_MODULE_NAME("LID")
53 
54 struct acpi_lid_softc {
55     device_t	lid_dev;
56     ACPI_HANDLE	lid_handle;
57     int		lid_status;	/* open or closed */
58 #ifdef EVDEV_SUPPORT
59     struct evdev_dev *lid_evdev;
60 #endif
61 };
62 
63 ACPI_HANDLE acpi_lid_handle;
64 
65 ACPI_SERIAL_DECL(lid, "ACPI lid");
66 
67 static int	acpi_lid_probe(device_t dev);
68 static int	acpi_lid_attach(device_t dev);
69 static int	acpi_lid_suspend(device_t dev);
70 static int	acpi_lid_resume(device_t dev);
71 static void	acpi_lid_notify_status_changed(void *arg);
72 static void 	acpi_lid_notify_handler(ACPI_HANDLE h, UINT32 notify,
73 					void *context);
74 
75 static device_method_t acpi_lid_methods[] = {
76     /* Device interface */
77     DEVMETHOD(device_probe,	acpi_lid_probe),
78     DEVMETHOD(device_attach,	acpi_lid_attach),
79     DEVMETHOD(device_suspend,	acpi_lid_suspend),
80     DEVMETHOD(device_resume,	acpi_lid_resume),
81 
82     DEVMETHOD_END
83 };
84 
85 static driver_t acpi_lid_driver = {
86     "acpi_lid",
87     acpi_lid_methods,
88     sizeof(struct acpi_lid_softc),
89 };
90 
91 DRIVER_MODULE(acpi_lid, acpi, acpi_lid_driver, 0, 0);
92 MODULE_DEPEND(acpi_lid, acpi, 1, 1, 1);
93 
94 static int
95 acpi_lid_status_update(struct acpi_lid_softc *sc)
96 {
97 	ACPI_STATUS status;
98 	int lid_status;
99 
100 	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
101 
102 	/*
103 	 * Evaluate _LID and check the return value, update lid status.
104 	 *	Zero:		The lid is closed
105 	 *	Non-zero:	The lid is open
106 	 */
107 	status = acpi_GetInteger(sc->lid_handle, "_LID", &lid_status);
108 	if (ACPI_FAILURE(status))
109 		lid_status = 1;		/* assume lid is opened */
110 	else
111 		lid_status = (lid_status != 0); /* range check value */
112 
113 	if (sc->lid_status == lid_status)
114 		return (EALREADY);
115 	sc->lid_status = lid_status;
116 
117 	/* Send notification via devd */
118 	acpi_UserNotify("Lid", sc->lid_handle, sc->lid_status);
119 
120 #ifdef EVDEV_SUPPORT
121 	/* Notify evdev about lid status */
122 	evdev_push_sw(sc->lid_evdev, SW_LID, lid_status ? 0 : 1);
123 	evdev_sync(sc->lid_evdev);
124 #endif
125 	return (0);
126 }
127 
128 static int
129 acpi_lid_probe(device_t dev)
130 {
131     static char *lid_ids[] = { "PNP0C0D", NULL };
132     int rv;
133 
134     if (acpi_disabled("lid"))
135 	return (ENXIO);
136     rv = ACPI_ID_PROBE(device_get_parent(dev), dev, lid_ids, NULL);
137     if (rv <= 0)
138 	device_set_desc(dev, "Control Method Lid Switch");
139     return (rv);
140 }
141 
142 static int
143 acpi_lid_attach(device_t dev)
144 {
145     struct acpi_prw_data	prw;
146     struct acpi_lid_softc	*sc;
147 
148     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
149 
150     sc = device_get_softc(dev);
151     sc->lid_dev = dev;
152     acpi_lid_handle = sc->lid_handle = acpi_get_handle(dev);
153     sc->lid_status = -1;
154 
155 #ifdef EVDEV_SUPPORT
156     /* Register evdev device before initial status update */
157     sc->lid_evdev = evdev_alloc();
158     evdev_set_name(sc->lid_evdev, device_get_desc(dev));
159     evdev_set_phys(sc->lid_evdev, device_get_nameunit(dev));
160     evdev_set_id(sc->lid_evdev, BUS_HOST, 0, 0, 1);
161     evdev_support_event(sc->lid_evdev, EV_SYN);
162     evdev_support_event(sc->lid_evdev, EV_SW);
163     evdev_support_sw(sc->lid_evdev, SW_LID);
164 
165     if (evdev_register(sc->lid_evdev))
166         return (ENXIO);
167 #endif
168 
169     /*
170      * If a system does not get lid events, it may make sense to change
171      * the type to ACPI_ALL_NOTIFY.  Some systems generate both a wake and
172      * runtime notify in that case though.
173      */
174     AcpiInstallNotifyHandler(sc->lid_handle, ACPI_DEVICE_NOTIFY,
175 			     acpi_lid_notify_handler, sc);
176 
177     /* Enable the GPE for wake/runtime. */
178     acpi_wake_set_enable(dev, 1);
179     if (acpi_parse_prw(sc->lid_handle, &prw) == 0)
180 	AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit);
181 
182     /* Get the initial lid status */
183     acpi_lid_status_update(sc);
184 
185     /*
186      * Export the lid status
187      */
188     SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
189 	SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
190 	"state", CTLFLAG_RD, &sc->lid_status, 0,
191 	"Device state (0 = closed, 1 = open)");
192 
193     return (0);
194 }
195 
196 static int
197 acpi_lid_suspend(device_t dev)
198 {
199     return (0);
200 }
201 
202 static int
203 acpi_lid_resume(device_t dev)
204 {
205     struct acpi_lid_softc	*sc;
206 
207     sc = device_get_softc(dev);
208 
209     /* Update lid status, if any */
210     acpi_lid_status_update(sc);
211 
212     return (0);
213 }
214 
215 static void
216 acpi_lid_notify_status_changed(void *arg)
217 {
218     struct acpi_lid_softc	*sc;
219     struct acpi_softc		*acpi_sc;
220 
221     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
222 
223     sc = (struct acpi_lid_softc *)arg;
224     ACPI_SERIAL_BEGIN(lid);
225 
226     /* Update lid status, if any */
227     if (acpi_lid_status_update(sc) != 0)
228 	goto out;
229 
230     acpi_sc = acpi_device_get_parent_softc(sc->lid_dev);
231     if (acpi_sc == NULL)
232 	goto out;
233 
234     ACPI_VPRINT(sc->lid_dev, acpi_sc, "Lid %s\n",
235 		sc->lid_status ? "opened" : "closed");
236 
237     if (sc->lid_status == 0)
238 	EVENTHANDLER_INVOKE(acpi_sleep_event, acpi_sc->acpi_lid_switch_sx);
239     else
240 	EVENTHANDLER_INVOKE(acpi_wakeup_event, acpi_sc->acpi_lid_switch_sx);
241 
242 out:
243     ACPI_SERIAL_END(lid);
244     return_VOID;
245 }
246 
247 /* XXX maybe not here */
248 #define	ACPI_NOTIFY_STATUS_CHANGED	0x80
249 
250 static void
251 acpi_lid_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
252 {
253     struct acpi_lid_softc	*sc;
254 
255     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
256 
257     sc = (struct acpi_lid_softc *)context;
258     switch (notify) {
259     case ACPI_NOTIFY_STATUS_CHANGED:
260 	AcpiOsExecute(OSL_NOTIFY_HANDLER,
261 		      acpi_lid_notify_status_changed, sc);
262 	break;
263     default:
264 	device_printf(sc->lid_dev, "unknown notify %#x\n", notify);
265 	break;
266     }
267 
268     return_VOID;
269 }
270