acpi_lid.c revision 71872
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 *	$FreeBSD: head/sys/dev/acpica/acpi_lid.c 71872 2001-01-31 09:30:57Z msmith $
30 */
31
32#include "opt_acpi.h"
33#include <sys/param.h>
34#include <sys/kernel.h>
35#include <sys/bus.h>
36#include <sys/mutex.h>
37
38#include "acpi.h"
39
40#include <dev/acpica/acpivar.h>
41
42/*
43 * Hooks for the ACPI CA debugging infrastructure
44 */
45#define _COMPONENT	SYSTEM_CONTROL
46MODULE_NAME("LID")
47
48struct acpi_lid_softc {
49    device_t	lid_dev;
50    ACPI_HANDLE	lid_handle;
51    int		lid_status;	/* open or closed */
52};
53
54static int	acpi_lid_probe(device_t dev);
55static int	acpi_lid_attach(device_t dev);
56static void	acpi_lid_notify_status_changed(void *arg);
57static void 	acpi_lid_notify_handler(ACPI_HANDLE h,UINT32 notify, void *context);
58
59static device_method_t acpi_lid_methods[] = {
60    /* Device interface */
61    DEVMETHOD(device_probe,	acpi_lid_probe),
62    DEVMETHOD(device_attach,	acpi_lid_attach),
63
64    {0, 0}
65};
66
67static driver_t acpi_lid_driver = {
68    "acpi_lid",
69    acpi_lid_methods,
70    sizeof(struct acpi_lid_softc),
71};
72
73devclass_t acpi_lid_devclass;
74DRIVER_MODULE(acpi_lid, acpi, acpi_lid_driver, acpi_lid_devclass, 0, 0);
75
76static int
77acpi_lid_probe(device_t dev)
78{
79    if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
80	!acpi_disabled("lid") &&
81	acpi_MatchHid(dev, "PNP0C0D")) {
82	device_set_desc(dev, "Control Method Lid Switch");
83	return(0);
84    }
85    return(ENXIO);
86}
87
88static int
89acpi_lid_attach(device_t dev)
90{
91    struct acpi_lid_softc	*sc;
92
93    FUNCTION_TRACE(__FUNCTION__);
94
95    sc = device_get_softc(dev);
96    sc->lid_dev = dev;
97    sc->lid_handle = acpi_get_handle(dev);
98
99    /*
100     * Install notification handler
101     */
102    AcpiInstallNotifyHandler(sc->lid_handle, ACPI_DEVICE_NOTIFY, acpi_lid_notify_handler, sc);
103    return_VALUE(0);
104}
105
106static void
107acpi_lid_notify_status_changed(void *arg)
108{
109    struct acpi_lid_softc	*sc;
110    struct acpi_softc		*acpi_sc;
111
112    FUNCTION_TRACE(__FUNCTION__);
113
114    sc = (struct acpi_lid_softc *)arg;
115
116    /*
117     * Evaluate _LID and check the return value, update lid status.
118     *	Zero:		The lid is closed
119     *	Non-zero:	The lid is open
120     */
121    if (acpi_EvaluateInteger(sc->lid_handle, "_LID", &sc->lid_status) != AE_OK)
122	return_VOID;
123    device_printf(sc->lid_dev, "Lid %s\n", sc->lid_status ? "opened" : "closed");
124
125    acpi_sc = acpi_device_get_parent_softc(sc->lid_dev);
126    if (acpi_sc == NULL) {
127        return_VOID;
128    }
129
130    if (sc->lid_status == 0) {
131	EVENTHANDLER_INVOKE(acpi_sleep_event, acpi_sc->acpi_lid_switch_sx);
132    } else {
133	EVENTHANDLER_INVOKE(acpi_wakeup_event, acpi_sc->acpi_lid_switch_sx);
134    }
135
136    return_VOID;
137}
138
139/* XXX maybe not here */
140#define	ACPI_NOTIFY_STATUS_CHANGED	0x80
141
142static void
143acpi_lid_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
144{
145    struct acpi_lid_softc	*sc = (struct acpi_lid_softc *)context;
146
147    FUNCTION_TRACE_U32(__FUNCTION__, notify);
148
149    switch (notify) {
150    case ACPI_NOTIFY_STATUS_CHANGED:
151	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_lid_notify_status_changed, sc);
152	break;
153    default:
154	break;		/* unknown notification value */
155    }
156    return_VOID;
157}
158
159