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 __FBSDID("$FreeBSD$");
32
33 #include "opt_acpi.h"
34 #include "opt_evdev.h"
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/proc.h>
40
41 #include <contrib/dev/acpica/include/acpi.h>
42 #include <contrib/dev/acpica/include/accommon.h>
43
44 #include <dev/acpica/acpivar.h>
45
46 #ifdef EVDEV_SUPPORT
47 #include <dev/evdev/input.h>
48 #include <dev/evdev/evdev.h>
49 #endif
50
51 /* Hooks for the ACPI CA debugging infrastructure */
52 #define _COMPONENT ACPI_BUTTON
53 ACPI_MODULE_NAME("LID")
54
55 struct acpi_lid_softc {
56 device_t lid_dev;
57 ACPI_HANDLE lid_handle;
58 int lid_status; /* open or closed */
59 #ifdef EVDEV_SUPPORT
60 struct evdev_dev *lid_evdev;
61 #endif
62 };
63
64 ACPI_HANDLE acpi_lid_handle;
65
66 ACPI_SERIAL_DECL(lid, "ACPI lid");
67
68 static int acpi_lid_probe(device_t dev);
69 static int acpi_lid_attach(device_t dev);
70 static int acpi_lid_suspend(device_t dev);
71 static int acpi_lid_resume(device_t dev);
72 static void acpi_lid_notify_status_changed(void *arg);
73 static void acpi_lid_notify_handler(ACPI_HANDLE h, UINT32 notify,
74 void *context);
75
76 static device_method_t acpi_lid_methods[] = {
77 /* Device interface */
78 DEVMETHOD(device_probe, acpi_lid_probe),
79 DEVMETHOD(device_attach, acpi_lid_attach),
80 DEVMETHOD(device_suspend, acpi_lid_suspend),
81 DEVMETHOD(device_resume, acpi_lid_resume),
82
83 DEVMETHOD_END
84 };
85
86 static driver_t acpi_lid_driver = {
87 "acpi_lid",
88 acpi_lid_methods,
89 sizeof(struct acpi_lid_softc),
90 };
91
92 static devclass_t acpi_lid_devclass;
93 DRIVER_MODULE(acpi_lid, acpi, acpi_lid_driver, acpi_lid_devclass, 0, 0);
94 MODULE_DEPEND(acpi_lid, acpi, 1, 1, 1);
95
96 static void
97 acpi_lid_status_update(struct acpi_lid_softc *sc)
98 {
99 ACPI_STATUS status;
100 int lid_status;
101
102 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
103
104 /*
105 * Evaluate _LID and check the return value, update lid status.
106 * Zero: The lid is closed
107 * Non-zero: The lid is open
108 */
109 status = acpi_GetInteger(sc->lid_handle, "_LID", &lid_status);
110 if (ACPI_FAILURE(status))
111 lid_status = 1; /* assume lid is opened */
112
113 /* range check value */
114 sc->lid_status = lid_status ? 1 : 0;
115
116 /* Send notification via devd */
117 acpi_UserNotify("Lid", sc->lid_handle, sc->lid_status);
118
119 #ifdef EVDEV_SUPPORT
120 /* Notify evdev about lid status */
121 evdev_push_sw(sc->lid_evdev, SW_LID, lid_status ? 0 : 1);
122 evdev_sync(sc->lid_evdev);
123 #endif
124 }
125
126 static int
127 acpi_lid_probe(device_t dev)
128 {
129 static char *lid_ids[] = { "PNP0C0D", NULL };
130
131 if (acpi_disabled("lid") ||
132 ACPI_ID_PROBE(device_get_parent(dev), dev, lid_ids) == NULL)
133 return (ENXIO);
134
135 device_set_desc(dev, "Control Method Lid Switch");
136 return (0);
137 }
138
139 static int
140 acpi_lid_attach(device_t dev)
141 {
142 struct acpi_prw_data prw;
143 struct acpi_lid_softc *sc;
144
145 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
146
147 sc = device_get_softc(dev);
148 sc->lid_dev = dev;
149 acpi_lid_handle = sc->lid_handle = acpi_get_handle(dev);
150
151 #ifdef EVDEV_SUPPORT
152 /* Register evdev device before initial status update */
153 sc->lid_evdev = evdev_alloc();
154 evdev_set_name(sc->lid_evdev, device_get_desc(dev));
155 evdev_set_phys(sc->lid_evdev, device_get_nameunit(dev));
156 evdev_set_id(sc->lid_evdev, BUS_HOST, 0, 0, 1);
157 evdev_support_event(sc->lid_evdev, EV_SYN);
158 evdev_support_event(sc->lid_evdev, EV_SW);
159 evdev_support_sw(sc->lid_evdev, SW_LID);
160
161 if (evdev_register(sc->lid_evdev))
162 return (ENXIO);
163 #endif
164
165 /*
166 * If a system does not get lid events, it may make sense to change
167 * the type to ACPI_ALL_NOTIFY. Some systems generate both a wake and
168 * runtime notify in that case though.
169 */
170 AcpiInstallNotifyHandler(sc->lid_handle, ACPI_DEVICE_NOTIFY,
171 acpi_lid_notify_handler, sc);
172
173 /* Enable the GPE for wake/runtime. */
174 acpi_wake_set_enable(dev, 1);
175 if (acpi_parse_prw(sc->lid_handle, &prw) == 0)
176 AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit);
177
178 /* Get the initial lid status */
179 acpi_lid_status_update(sc);
180
181 /*
182 * Export the lid status
183 */
184 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
185 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
186 "state", CTLFLAG_RD, &sc->lid_status, 0,
187 "Device state (0 = closed, 1 = open)");
188
189 return (0);
190 }
191
192 static int
193 acpi_lid_suspend(device_t dev)
194 {
195 return (0);
196 }
197
198 static int
199 acpi_lid_resume(device_t dev)
200 {
201 struct acpi_lid_softc *sc;
202
203 sc = device_get_softc(dev);
204
205 /* Update lid status, if any */
206 acpi_lid_status_update(sc);
207
208 return (0);
209 }
210
211 static void
212 acpi_lid_notify_status_changed(void *arg)
213 {
214 struct acpi_lid_softc *sc;
215 struct acpi_softc *acpi_sc;
216
217 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
218
219 sc = (struct acpi_lid_softc *)arg;
220 ACPI_SERIAL_BEGIN(lid);
221
222 /* Update lid status, if any */
223 acpi_lid_status_update(sc);
224
225 acpi_sc = acpi_device_get_parent_softc(sc->lid_dev);
226 if (acpi_sc == NULL)
227 goto out;
228
229 ACPI_VPRINT(sc->lid_dev, acpi_sc, "Lid %s\n",
230 sc->lid_status ? "opened" : "closed");
231
232 acpi_UserNotify("Lid", sc->lid_handle, sc->lid_status);
233
234 if (sc->lid_status == 0)
235 EVENTHANDLER_INVOKE(acpi_sleep_event, acpi_sc->acpi_lid_switch_sx);
236 else
237 EVENTHANDLER_INVOKE(acpi_wakeup_event, acpi_sc->acpi_lid_switch_sx);
238
239 out:
240 ACPI_SERIAL_END(lid);
241 return_VOID;
242 }
243
244 /* XXX maybe not here */
245 #define ACPI_NOTIFY_STATUS_CHANGED 0x80
246
247 static void
248 acpi_lid_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
249 {
250 struct acpi_lid_softc *sc;
251
252 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
253
254 sc = (struct acpi_lid_softc *)context;
255 switch (notify) {
256 case ACPI_NOTIFY_STATUS_CHANGED:
257 AcpiOsExecute(OSL_NOTIFY_HANDLER,
258 acpi_lid_notify_status_changed, sc);
259 break;
260 default:
261 device_printf(sc->lid_dev, "unknown notify %#x\n", notify);
262 break;
263 }
264
265 return_VOID;
266 }
Cache object: e831d86a14005902bab42f3e175601e2
|