acpi_button.c revision 129783
167761Smsmith/*-
267761Smsmith * Copyright (c) 2000 Mitsaru IWASAKI <iwasaki@jp.freebsd.org>
367761Smsmith * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
467761Smsmith * Copyright (c) 2000 BSDi
567761Smsmith * All rights reserved.
667761Smsmith *
767761Smsmith * Redistribution and use in source and binary forms, with or without
867761Smsmith * modification, are permitted provided that the following conditions
967761Smsmith * are met:
1067761Smsmith * 1. Redistributions of source code must retain the above copyright
1167761Smsmith *    notice, this list of conditions and the following disclaimer.
1267761Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1367761Smsmith *    notice, this list of conditions and the following disclaimer in the
1467761Smsmith *    documentation and/or other materials provided with the distribution.
1567761Smsmith *
1667761Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1767761Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1867761Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1967761Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2067761Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2167761Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2267761Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2367761Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2467761Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2567761Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2667761Smsmith * SUCH DAMAGE.
2767761Smsmith *
2867761Smsmith *	$FreeBSD: head/sys/dev/acpica/acpi_button.c 129783 2004-05-27 18:38:45Z njl $
2967761Smsmith */
3067761Smsmith
3167761Smsmith#include "opt_acpi.h"
3267761Smsmith#include <sys/param.h>
3367761Smsmith#include <sys/kernel.h>
3467761Smsmith#include <sys/bus.h>
3567761Smsmith
3667761Smsmith#include "acpi.h"
3767761Smsmith#include <dev/acpica/acpivar.h>
3867761Smsmith
39118783Snjl/* Hooks for the ACPI CA debugging infrastructure */
4077432Smsmith#define _COMPONENT	ACPI_BUTTON
4191123SmsmithACPI_MODULE_NAME("BUTTON")
4269744Smsmith
4367761Smsmithstruct acpi_button_softc {
4467761Smsmith    device_t	button_dev;
4567761Smsmith    ACPI_HANDLE	button_handle;
46120308Snjl    boolean_t	button_type;
47118783Snjl#define		ACPI_POWER_BUTTON	0
48118783Snjl#define		ACPI_SLEEP_BUTTON	1
49120308Snjl    boolean_t	fixed;
5067761Smsmith};
5167761Smsmith
52118783Snjl#define		ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP	0x80
53118783Snjl#define		ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP	0x02
54118783Snjl
5567761Smsmithstatic int	acpi_button_probe(device_t dev);
5667761Smsmithstatic int	acpi_button_attach(device_t dev);
57100497Siwasakistatic int	acpi_button_suspend(device_t dev);
58100497Siwasakistatic int	acpi_button_resume(device_t dev);
59118783Snjlstatic void 	acpi_button_notify_handler(ACPI_HANDLE h, UINT32 notify,
60118783Snjl					   void *context);
61120308Snjlstatic ACPI_STATUS
62120308Snjl		acpi_button_fixed_handler(void *context);
63125679Snjlstatic void	acpi_button_notify_sleep(void *arg);
64125679Snjlstatic void	acpi_button_notify_wakeup(void *arg);
6567761Smsmith
6667761Smsmithstatic device_method_t acpi_button_methods[] = {
6767761Smsmith    /* Device interface */
6867761Smsmith    DEVMETHOD(device_probe,	acpi_button_probe),
6967761Smsmith    DEVMETHOD(device_attach,	acpi_button_attach),
70100497Siwasaki    DEVMETHOD(device_suspend,	acpi_button_suspend),
71118049Stakawata    DEVMETHOD(device_shutdown,	acpi_button_suspend),
72100497Siwasaki    DEVMETHOD(device_resume,	acpi_button_resume),
7367761Smsmith
7467761Smsmith    {0, 0}
7567761Smsmith};
7667761Smsmith
7767761Smsmithstatic driver_t acpi_button_driver = {
7867761Smsmith    "acpi_button",
7967761Smsmith    acpi_button_methods,
8067761Smsmith    sizeof(struct acpi_button_softc),
8167761Smsmith};
8267761Smsmith
8389054Smsmithstatic devclass_t acpi_button_devclass;
84118783SnjlDRIVER_MODULE(acpi_button, acpi, acpi_button_driver, acpi_button_devclass,
85118783Snjl	      0, 0);
86128071SnjlMODULE_DEPEND(acpi_button, acpi, 1, 1, 1);
8767761Smsmith
8867761Smsmithstatic int
8967761Smsmithacpi_button_probe(device_t dev)
9067761Smsmith{
9167761Smsmith    struct acpi_button_softc	*sc;
92120308Snjl    int ret = ENXIO;
9367761Smsmith
9467761Smsmith    sc = device_get_softc(dev);
95120308Snjl    if (acpi_get_type(dev) == ACPI_TYPE_DEVICE && !acpi_disabled("button")) {
96120308Snjl	if (acpi_MatchHid(dev, "PNP0C0C")) {
97120308Snjl	    device_set_desc(dev, "Power Button");
98120308Snjl	    sc->button_type = ACPI_POWER_BUTTON;
99120308Snjl	    ret = 0;
100120308Snjl	} else if (acpi_MatchHid(dev, "ACPI_FPB")) {
101120308Snjl	    device_set_desc(dev, "Power Button (fixed)");
102120308Snjl	    sc->button_type = ACPI_POWER_BUTTON;
103120308Snjl	    sc->fixed = 1;
104120308Snjl	    ret = 0;
105120308Snjl	} else if (acpi_MatchHid(dev, "PNP0C0E")) {
106120308Snjl	    device_set_desc(dev, "Sleep Button");
107120308Snjl	    sc->button_type = ACPI_SLEEP_BUTTON;
108120308Snjl	    ret = 0;
109120308Snjl	} else if (acpi_MatchHid(dev, "ACPI_FSB")) {
110120308Snjl	    device_set_desc(dev, "Sleep Button (fixed)");
111120308Snjl	    sc->button_type = ACPI_SLEEP_BUTTON;
112120308Snjl	    sc->fixed = 1;
113120308Snjl	    ret = 0;
11467761Smsmith	}
11567761Smsmith    }
116120308Snjl    return (ret);
11767761Smsmith}
11867761Smsmith
11967761Smsmithstatic int
12067761Smsmithacpi_button_attach(device_t dev)
12167761Smsmith{
12267761Smsmith    struct acpi_button_softc	*sc;
12367761Smsmith    ACPI_STATUS			status;
124120308Snjl    int event;
12567761Smsmith
12696926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
12769744Smsmith
12867761Smsmith    sc = device_get_softc(dev);
12967761Smsmith    sc->button_dev = dev;
13067761Smsmith    sc->button_handle = acpi_get_handle(dev);
131125679Snjl    event = (sc->button_type == ACPI_SLEEP_BUTTON) ?
132125679Snjl	    ACPI_EVENT_SLEEP_BUTTON : ACPI_EVENT_POWER_BUTTON;
13367761Smsmith
134126014Snjl    /*
135126014Snjl     * Install the new handler.  We could remove any fixed handlers added
136126014Snjl     * from the FADT once we have a duplicate from the AML but some systems
137126014Snjl     * only return events on one or the other so we have to keep both.
138126014Snjl     */
139120308Snjl    if (sc->fixed) {
140125679Snjl	AcpiClearEvent(event);
141120308Snjl	status = AcpiInstallFixedEventHandler(event,
142120308Snjl			acpi_button_fixed_handler, sc);
143120308Snjl    } else {
144129692Snjl	/*
145129692Snjl	 * If a system does not get lid events, it may make sense to change
146129692Snjl	 * the type to ACPI_ALL_NOTIFY.  Some systems generate both a wake
147129692Snjl	 * and runtime notify in that case though.
148129692Snjl	 */
149120308Snjl	status = AcpiInstallNotifyHandler(sc->button_handle,
150120308Snjl			ACPI_DEVICE_NOTIFY, acpi_button_notify_handler, sc);
151120308Snjl    }
152118783Snjl    if (ACPI_FAILURE(status)) {
153120308Snjl	device_printf(sc->button_dev, "couldn't install notify handler - %s\n",
154118783Snjl		      AcpiFormatException(status));
155118783Snjl	return_VALUE (ENXIO);
15667761Smsmith    }
157125679Snjl
158129783Snjl    /* Enable the GPE for wake/runtime. */
159129783Snjl    acpi_wake_init(dev, ACPI_GPE_TYPE_WAKE_RUN);
160129783Snjl    acpi_wake_set_enable(dev, 1);
161129783Snjl
162118783Snjl    return_VALUE (0);
16367761Smsmith}
16467761Smsmith
165100497Siwasakistatic int
166100497Siwasakiacpi_button_suspend(device_t dev)
167100497Siwasaki{
168129783Snjl    struct acpi_softc           *acpi_sc;
169100497Siwasaki
170129783Snjl    acpi_sc = acpi_device_get_parent_softc(dev);
171129783Snjl    acpi_wake_sleep_prep(dev, acpi_sc->acpi_sstate);
172100497Siwasaki    return (0);
173100497Siwasaki}
174100497Siwasaki
175100497Siwasakistatic int
176100497Siwasakiacpi_button_resume(device_t dev)
177100497Siwasaki{
178100497Siwasaki    return (0);
179100497Siwasaki}
180100497Siwasaki
18167761Smsmithstatic void
182125679Snjlacpi_button_notify_sleep(void *arg)
18367761Smsmith{
18467761Smsmith    struct acpi_button_softc	*sc;
18567761Smsmith    struct acpi_softc		*acpi_sc;
18667761Smsmith
18796926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
18869744Smsmith
18967761Smsmith    sc = (struct acpi_button_softc *)arg;
19067761Smsmith    acpi_sc = acpi_device_get_parent_softc(sc->button_dev);
191118783Snjl    if (acpi_sc == NULL)
19269744Smsmith	return_VOID;
19367761Smsmith
194121493Snjl    acpi_UserNotify("Button", sc->button_handle, sc->button_type);
195121493Snjl
19667761Smsmith    switch (sc->button_type) {
19767761Smsmith    case ACPI_POWER_BUTTON:
19897274Sbde	ACPI_VPRINT(sc->button_dev, acpi_sc, "power button pressed\n");
199125679Snjl	acpi_event_power_button_sleep(acpi_sc);
20067761Smsmith	break;
20167761Smsmith    case ACPI_SLEEP_BUTTON:
20297274Sbde	ACPI_VPRINT(sc->button_dev, acpi_sc, "sleep button pressed\n");
203125679Snjl	acpi_event_sleep_button_sleep(acpi_sc);
20467761Smsmith	break;
20567761Smsmith    default:
20669744Smsmith	break;		/* unknown button type */
20767761Smsmith    }
20867761Smsmith}
20967761Smsmith
21067761Smsmithstatic void
211125679Snjlacpi_button_notify_wakeup(void *arg)
21267761Smsmith{
21367761Smsmith    struct acpi_button_softc	*sc;
21467761Smsmith    struct acpi_softc		*acpi_sc;
21567761Smsmith
21696926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
21769744Smsmith
21867761Smsmith    sc = (struct acpi_button_softc *)arg;
21967761Smsmith    acpi_sc = acpi_device_get_parent_softc(sc->button_dev);
220118783Snjl    if (acpi_sc == NULL)
22169744Smsmith	return_VOID;
22267761Smsmith
223121493Snjl    acpi_UserNotify("Button", sc->button_handle, sc->button_type);
224121493Snjl
22567761Smsmith    switch (sc->button_type) {
22667761Smsmith    case ACPI_POWER_BUTTON:
22797274Sbde	ACPI_VPRINT(sc->button_dev, acpi_sc, "wakeup by power button\n");
228125679Snjl	acpi_event_power_button_wake(acpi_sc);
22967761Smsmith	break;
23067761Smsmith    case ACPI_SLEEP_BUTTON:
23197274Sbde	ACPI_VPRINT(sc->button_dev, acpi_sc, "wakeup by sleep button\n");
232125679Snjl	acpi_event_sleep_button_wake(acpi_sc);
23367761Smsmith	break;
23467761Smsmith    default:
23569744Smsmith	break;		/* unknown button type */
23667761Smsmith    }
23767761Smsmith}
23867761Smsmith
23967761Smsmithstatic void
24067761Smsmithacpi_button_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
24167761Smsmith{
242129692Snjl    struct acpi_button_softc	*sc;
24367761Smsmith
24496926Speter    ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
24569744Smsmith
246129692Snjl    sc = (struct acpi_button_softc *)context;
24767761Smsmith    switch (notify) {
24867761Smsmith    case ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP:
249118783Snjl	AcpiOsQueueForExecution(OSD_PRIORITY_LO,
250125679Snjl				acpi_button_notify_sleep, sc);
25167761Smsmith	break;
25267761Smsmith    case ACPI_NOTIFY_BUTTON_PRESSED_FOR_WAKEUP:
253118783Snjl	AcpiOsQueueForExecution(OSD_PRIORITY_LO,
254125679Snjl				acpi_button_notify_wakeup, sc);
25567761Smsmith	break;
25667761Smsmith    default:
257129692Snjl	device_printf(sc->button_dev, "unknown notify %#x\n", notify);
258129692Snjl	break;
25967761Smsmith    }
26067761Smsmith}
261120308Snjl
262120308Snjlstatic ACPI_STATUS
263120308Snjlacpi_button_fixed_handler(void *context)
264120308Snjl{
265120308Snjl    struct acpi_button_softc	*sc = (struct acpi_button_softc *)context;
266120308Snjl
267120327Snjl    ACPI_FUNCTION_TRACE_PTR((char *)(uintptr_t)__func__, context);
268120317Sphk
269120308Snjl    if (context == NULL)
270120308Snjl	return_ACPI_STATUS (AE_BAD_PARAMETER);
271120308Snjl
272120308Snjl    acpi_button_notify_handler(sc->button_handle,
273120308Snjl			       ACPI_NOTIFY_BUTTON_PRESSED_FOR_SLEEP, sc);
274120327Snjl    return_ACPI_STATUS (AE_OK);
275120308Snjl}
276