acpi_lid.c revision 129879
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: head/sys/dev/acpica/acpi_lid.c 129879 2004-05-30 20:08:47Z phk $");
32
33#include "opt_acpi.h"
34#include <sys/param.h>
35#include <sys/kernel.h>
36#include <sys/module.h>
37#include <sys/bus.h>
38#include <sys/proc.h>
39
40#include "acpi.h"
41#include <dev/acpica/acpivar.h>
42
43/* Hooks for the ACPI CA debugging infrastructure */
44#define _COMPONENT	ACPI_BUTTON
45ACPI_MODULE_NAME("LID")
46
47struct acpi_lid_softc {
48    device_t	lid_dev;
49    ACPI_HANDLE	lid_handle;
50    int		lid_status;	/* open or closed */
51};
52
53static int	acpi_lid_probe(device_t dev);
54static int	acpi_lid_attach(device_t dev);
55static int	acpi_lid_suspend(device_t dev);
56static int	acpi_lid_resume(device_t dev);
57static void	acpi_lid_notify_status_changed(void *arg);
58static void 	acpi_lid_notify_handler(ACPI_HANDLE h, UINT32 notify,
59					void *context);
60
61static device_method_t acpi_lid_methods[] = {
62    /* Device interface */
63    DEVMETHOD(device_probe,	acpi_lid_probe),
64    DEVMETHOD(device_attach,	acpi_lid_attach),
65    DEVMETHOD(device_suspend,	acpi_lid_suspend),
66    DEVMETHOD(device_resume,	acpi_lid_resume),
67
68    {0, 0}
69};
70
71static driver_t acpi_lid_driver = {
72    "acpi_lid",
73    acpi_lid_methods,
74    sizeof(struct acpi_lid_softc),
75};
76
77static devclass_t acpi_lid_devclass;
78DRIVER_MODULE(acpi_lid, acpi, acpi_lid_driver, acpi_lid_devclass, 0, 0);
79MODULE_DEPEND(acpi_lid, acpi, 1, 1, 1);
80
81static int
82acpi_lid_probe(device_t dev)
83{
84    if (acpi_get_type(dev) == ACPI_TYPE_DEVICE && !acpi_disabled("lid") &&
85	acpi_MatchHid(dev, "PNP0C0D")) {
86
87	device_set_desc(dev, "Control Method Lid Switch");
88	return (0);
89    }
90    return (ENXIO);
91}
92
93static int
94acpi_lid_attach(device_t dev)
95{
96    struct acpi_lid_softc	*sc;
97
98    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
99
100    sc = device_get_softc(dev);
101    sc->lid_dev = dev;
102    sc->lid_handle = acpi_get_handle(dev);
103
104    /*
105     * If a system does not get lid events, it may make sense to change
106     * the type to ACPI_ALL_NOTIFY.  Some systems generate both a wake and
107     * runtime notify in that case though.
108     */
109    AcpiInstallNotifyHandler(sc->lid_handle, ACPI_DEVICE_NOTIFY,
110			     acpi_lid_notify_handler, sc);
111
112    /* Enable the GPE for wake/runtime. */
113    acpi_wake_init(dev, ACPI_GPE_TYPE_WAKE_RUN);
114    acpi_wake_set_enable(dev, 1);
115
116    return_VALUE (0);
117}
118
119static int
120acpi_lid_suspend(device_t dev)
121{
122    struct acpi_softc		*acpi_sc;
123
124    acpi_sc = acpi_device_get_parent_softc(dev);
125    acpi_wake_sleep_prep(dev, acpi_sc->acpi_sstate);
126    return (0);
127}
128
129static int
130acpi_lid_resume(device_t dev)
131{
132    acpi_wake_run_prep(dev);
133    return (0);
134}
135
136static void
137acpi_lid_notify_status_changed(void *arg)
138{
139    struct acpi_lid_softc	*sc;
140    struct acpi_softc		*acpi_sc;
141    ACPI_STATUS			status;
142
143    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
144
145    sc = (struct acpi_lid_softc *)arg;
146
147    /*
148     * Evaluate _LID and check the return value, update lid status.
149     *	Zero:		The lid is closed
150     *	Non-zero:	The lid is open
151     */
152    status = acpi_GetInteger(sc->lid_handle, "_LID", &sc->lid_status);
153    if (ACPI_FAILURE(status))
154	return_VOID;
155
156    acpi_sc = acpi_device_get_parent_softc(sc->lid_dev);
157    if (acpi_sc == NULL)
158        return_VOID;
159
160    ACPI_VPRINT(sc->lid_dev, acpi_sc, "Lid %s\n",
161		sc->lid_status ? "opened" : "closed");
162
163    acpi_UserNotify("Lid", sc->lid_handle, sc->lid_status);
164
165    if (sc->lid_status == 0)
166	EVENTHANDLER_INVOKE(acpi_sleep_event, acpi_sc->acpi_lid_switch_sx);
167    else
168	EVENTHANDLER_INVOKE(acpi_wakeup_event, acpi_sc->acpi_lid_switch_sx);
169
170    return_VOID;
171}
172
173/* XXX maybe not here */
174#define	ACPI_NOTIFY_STATUS_CHANGED	0x80
175
176static void
177acpi_lid_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
178{
179    struct acpi_lid_softc	*sc;
180
181    ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
182
183    sc = (struct acpi_lid_softc *)context;
184    switch (notify) {
185    case ACPI_NOTIFY_STATUS_CHANGED:
186	AcpiOsQueueForExecution(OSD_PRIORITY_LO,
187				acpi_lid_notify_status_changed, sc);
188	break;
189    default:
190	device_printf(sc->lid_dev, "unknown notify %#x\n", notify);
191	break;
192    }
193
194    return_VOID;
195}
196