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
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
40 #include <contrib/dev/acpica/acpi.h>
41 #include <dev/acpica/acpivar.h>
43 /* Hooks for the ACPI CA debugging infrastructure */
44 #define _COMPONENT ACPI_BUTTON
45 ACPI_MODULE_NAME("LID")
47 struct acpi_lid_softc {
49 ACPI_HANDLE lid_handle;
50 int lid_status; /* open or closed */
53 ACPI_SERIAL_DECL(lid, "ACPI lid");
55 static int acpi_lid_probe(device_t dev);
56 static int acpi_lid_attach(device_t dev);
57 static int acpi_lid_suspend(device_t dev);
58 static int acpi_lid_resume(device_t dev);
59 static void acpi_lid_notify_status_changed(void *arg);
60 static void acpi_lid_notify_handler(ACPI_HANDLE h, UINT32 notify,
63 static device_method_t acpi_lid_methods[] = {
64 /* Device interface */
65 DEVMETHOD(device_probe, acpi_lid_probe),
66 DEVMETHOD(device_attach, acpi_lid_attach),
67 DEVMETHOD(device_suspend, acpi_lid_suspend),
68 DEVMETHOD(device_resume, acpi_lid_resume),
73 static driver_t acpi_lid_driver = {
76 sizeof(struct acpi_lid_softc),
79 static devclass_t acpi_lid_devclass;
80 DRIVER_MODULE(acpi_lid, acpi, acpi_lid_driver, acpi_lid_devclass, 0, 0);
81 MODULE_DEPEND(acpi_lid, acpi, 1, 1, 1);
84 acpi_lid_probe(device_t dev)
86 static char *lid_ids[] = { "PNP0C0D", NULL };
88 if (acpi_disabled("lid") ||
89 ACPI_ID_PROBE(device_get_parent(dev), dev, lid_ids) == NULL)
92 device_set_desc(dev, "Control Method Lid Switch");
97 acpi_lid_attach(device_t dev)
99 struct acpi_lid_softc *sc;
101 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
103 sc = device_get_softc(dev);
105 sc->lid_handle = acpi_get_handle(dev);
108 * If a system does not get lid events, it may make sense to change
109 * the type to ACPI_ALL_NOTIFY. Some systems generate both a wake and
110 * runtime notify in that case though.
112 AcpiInstallNotifyHandler(sc->lid_handle, ACPI_DEVICE_NOTIFY,
113 acpi_lid_notify_handler, sc);
115 /* Enable the GPE for wake/runtime. */
116 acpi_wake_init(dev, ACPI_GPE_TYPE_WAKE_RUN);
117 acpi_wake_set_enable(dev, 1);
123 acpi_lid_suspend(device_t dev)
129 acpi_lid_resume(device_t dev)
135 acpi_lid_notify_status_changed(void *arg)
137 struct acpi_lid_softc *sc;
138 struct acpi_softc *acpi_sc;
141 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
143 sc = (struct acpi_lid_softc *)arg;
144 ACPI_SERIAL_BEGIN(lid);
147 * Evaluate _LID and check the return value, update lid status.
148 * Zero: The lid is closed
149 * Non-zero: The lid is open
151 status = acpi_GetInteger(sc->lid_handle, "_LID", &sc->lid_status);
152 if (ACPI_FAILURE(status))
155 acpi_sc = acpi_device_get_parent_softc(sc->lid_dev);
159 ACPI_VPRINT(sc->lid_dev, acpi_sc, "Lid %s\n",
160 sc->lid_status ? "opened" : "closed");
162 acpi_UserNotify("Lid", sc->lid_handle, sc->lid_status);
164 if (sc->lid_status == 0)
165 EVENTHANDLER_INVOKE(acpi_sleep_event, acpi_sc->acpi_lid_switch_sx);
167 EVENTHANDLER_INVOKE(acpi_wakeup_event, acpi_sc->acpi_lid_switch_sx);
170 ACPI_SERIAL_END(lid);
174 /* XXX maybe not here */
175 #define ACPI_NOTIFY_STATUS_CHANGED 0x80
178 acpi_lid_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
180 struct acpi_lid_softc *sc;
182 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
184 sc = (struct acpi_lid_softc *)context;
186 case ACPI_NOTIFY_STATUS_CHANGED:
187 AcpiOsExecute(OSL_NOTIFY_HANDLER,
188 acpi_lid_notify_status_changed, sc);
191 device_printf(sc->lid_dev, "unknown notify %#x\n", notify);