2 * Copyright (c) 2000 Mitsaru IWASAKI <iwasaki@jp.freebsd.org>
3 * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 2000 BSDi
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sys/dev/acpica/acpi_button.c,v 1.13.2.1 2003/08/22 20:49:20 jhb Exp $
29 * $DragonFly: src/sys/dev/acpica/Attic/acpi_button.c,v 1.1 2003/09/24 03:32:16 drhodus Exp $
33 #include <sys/param.h>
34 #include <sys/kernel.h>
39 #include <dev/acpica/acpivar.h>
42 * Hooks for the ACPI CA debugging infrastructure
44 #define _COMPONENT ACPI_BUTTON
45 ACPI_MODULE_NAME("BUTTON")
47 struct acpi_button_softc {
49 ACPI_HANDLE button_handle;
50 #define ACPI_POWER_BUTTON 0
51 #define ACPI_SLEEP_BUTTON 1
52 boolean_t button_type; /* Power or Sleep Button */
55 static int acpi_button_probe(device_t dev);
56 static int acpi_button_attach(device_t dev);
57 static int acpi_button_suspend(device_t dev);
58 static int acpi_button_resume(device_t dev);
59 static void acpi_button_notify_handler(ACPI_HANDLE h,UINT32 notify, void *context);
60 static void acpi_button_notify_pressed_for_sleep(void *arg);
61 static void acpi_button_notify_pressed_for_wakeup(void *arg);
63 static device_method_t acpi_button_methods[] = {
64 /* Device interface */
65 DEVMETHOD(device_probe, acpi_button_probe),
66 DEVMETHOD(device_attach, acpi_button_attach),
67 DEVMETHOD(device_suspend, acpi_button_suspend),
68 DEVMETHOD(device_resume, acpi_button_resume),
73 static driver_t acpi_button_driver = {
76 sizeof(struct acpi_button_softc),
79 static devclass_t acpi_button_devclass;
80 DRIVER_MODULE(acpi_button, acpi, acpi_button_driver, acpi_button_devclass, 0, 0);
83 acpi_button_probe(device_t dev)
85 struct acpi_button_softc *sc;
87 sc = device_get_softc(dev);
88 if (acpi_get_type(dev) == ACPI_TYPE_DEVICE) {
89 if (!acpi_disabled("button")) {
90 if (acpi_MatchHid(dev, "PNP0C0C")) {
91 device_set_desc(dev, "Power Button");
92 sc->button_type = ACPI_POWER_BUTTON;
95 if (acpi_MatchHid(dev, "PNP0C0E")) {
96 device_set_desc(dev, "Sleep Button");
97 sc->button_type = ACPI_SLEEP_BUTTON;
106 acpi_button_attach(device_t dev)
108 struct acpi_button_softc *sc;
111 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
113 sc = device_get_softc(dev);
114 sc->button_dev = dev;
115 sc->button_handle = acpi_get_handle(dev);
117 if (ACPI_FAILURE(status = AcpiInstallNotifyHandler(sc->button_handle, ACPI_DEVICE_NOTIFY,
118 acpi_button_notify_handler, sc))) {
119 device_printf(sc->button_dev, "couldn't install Notify handler - %s\n", AcpiFormatException(status));
122 acpi_device_enable_wake_capability(sc->button_handle, 1);
127 acpi_button_suspend(device_t dev)
129 struct acpi_button_softc *sc;
131 sc = device_get_softc(dev);
132 acpi_device_enable_wake_event(sc->button_handle);
137 acpi_button_resume(device_t dev)
144 acpi_button_notify_pressed_for_sleep(void *arg)
146 struct acpi_button_softc *sc;
147 struct acpi_softc *acpi_sc;
149 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
151 sc = (struct acpi_button_softc *)arg;
152 acpi_sc = acpi_device_get_parent_softc(sc->button_dev);
153 if (acpi_sc == NULL) {
157 switch (sc->button_type) {
158 case ACPI_POWER_BUTTON:
159 ACPI_VPRINT(sc->button_dev, acpi_sc, "power button pressed\n");
160 acpi_eventhandler_power_button_for_sleep((void *)acpi_sc);
162 case ACPI_SLEEP_BUTTON:
163 ACPI_VPRINT(sc->button_dev, acpi_sc, "sleep button pressed\n");
164 acpi_eventhandler_sleep_button_for_sleep((void *)acpi_sc);
167 break; /* unknown button type */
173 acpi_button_notify_pressed_for_wakeup(void *arg)
175 struct acpi_button_softc *sc;
176 struct acpi_softc *acpi_sc;
178 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
180 sc = (struct acpi_button_softc *)arg;
181 acpi_sc = acpi_device_get_parent_softc(sc->button_dev);
182 if (acpi_sc == NULL) {
186 switch (sc->button_type) {
187 case ACPI_POWER_BUTTON:
188 ACPI_VPRINT(sc->button_dev, acpi_sc, "wakeup by power button\n");
189 acpi_eventhandler_power_button_for_wakeup((void *)acpi_sc);
191 case ACPI_SLEEP_BUTTON:
192 ACPI_VPRINT(sc->button_dev, acpi_sc, "wakeup by sleep button\n");
193 acpi_eventhandler_sleep_button_for_wakeup((void *)acpi_sc);
196 break; /* unknown button type */
201 /* XXX maybe not here */
202 #define ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP 0x80
203 #define ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP 0x02
206 acpi_button_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
208 struct acpi_button_softc *sc = (struct acpi_button_softc *)context;
210 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
213 case ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP:
214 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_button_notify_pressed_for_sleep, sc);
216 case ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP:
217 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_button_notify_pressed_for_wakeup, sc);
220 break; /* unknown notification value */