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