]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/acpica/acpi_lid.c
MFV r358511,r358532:
[FreeBSD/FreeBSD.git] / sys / dev / acpica / acpi_lid.c
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$");
32
33 #include "opt_acpi.h"
34 #include <sys/param.h>
35 #include <sys/eventhandler.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/proc.h>
40
41 #include <contrib/dev/acpica/include/acpi.h>
42 #include <contrib/dev/acpica/include/accommon.h>
43
44 #include <dev/acpica/acpivar.h>
45
46 /* Hooks for the ACPI CA debugging infrastructure */
47 #define _COMPONENT      ACPI_BUTTON
48 ACPI_MODULE_NAME("LID")
49
50 struct acpi_lid_softc {
51     device_t    lid_dev;
52     ACPI_HANDLE lid_handle;
53     int         lid_status;     /* open or closed */
54 };
55
56 ACPI_HANDLE acpi_lid_handle;
57
58 ACPI_SERIAL_DECL(lid, "ACPI lid");
59
60 static int      acpi_lid_probe(device_t dev);
61 static int      acpi_lid_attach(device_t dev);
62 static int      acpi_lid_suspend(device_t dev);
63 static int      acpi_lid_resume(device_t dev);
64 static void     acpi_lid_notify_status_changed(void *arg);
65 static void     acpi_lid_notify_handler(ACPI_HANDLE h, UINT32 notify,
66                                         void *context);
67
68 static device_method_t acpi_lid_methods[] = {
69     /* Device interface */
70     DEVMETHOD(device_probe,     acpi_lid_probe),
71     DEVMETHOD(device_attach,    acpi_lid_attach),
72     DEVMETHOD(device_suspend,   acpi_lid_suspend),
73     DEVMETHOD(device_resume,    acpi_lid_resume),
74
75     DEVMETHOD_END
76 };
77
78 static driver_t acpi_lid_driver = {
79     "acpi_lid",
80     acpi_lid_methods,
81     sizeof(struct acpi_lid_softc),
82 };
83
84 static devclass_t acpi_lid_devclass;
85 DRIVER_MODULE(acpi_lid, acpi, acpi_lid_driver, acpi_lid_devclass, 0, 0);
86 MODULE_DEPEND(acpi_lid, acpi, 1, 1, 1);
87
88 static void
89 acpi_lid_status_update(struct acpi_lid_softc *sc)
90 {
91         ACPI_STATUS status;
92         int lid_status;
93
94         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
95
96         /*
97          * Evaluate _LID and check the return value, update lid status.
98          *      Zero:           The lid is closed
99          *      Non-zero:       The lid is open
100          */
101         status = acpi_GetInteger(sc->lid_handle, "_LID", &lid_status);
102         if (ACPI_FAILURE(status))
103                 lid_status = 1;         /* assume lid is opened */
104
105         /* range check value */
106         sc->lid_status = lid_status ? 1 : 0;
107 }
108
109 static int
110 acpi_lid_probe(device_t dev)
111 {
112     static char *lid_ids[] = { "PNP0C0D", NULL };
113     int rv;
114
115     if (acpi_disabled("lid"))
116         return (ENXIO);
117     rv = ACPI_ID_PROBE(device_get_parent(dev), dev, lid_ids, NULL);
118     if (rv <= 0)
119         device_set_desc(dev, "Control Method Lid Switch");
120     return (rv);
121 }
122
123 static int
124 acpi_lid_attach(device_t dev)
125 {
126     struct acpi_prw_data        prw;
127     struct acpi_lid_softc       *sc;
128
129     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
130
131     sc = device_get_softc(dev);
132     sc->lid_dev = dev;
133     acpi_lid_handle = sc->lid_handle = acpi_get_handle(dev);
134
135     /*
136      * If a system does not get lid events, it may make sense to change
137      * the type to ACPI_ALL_NOTIFY.  Some systems generate both a wake and
138      * runtime notify in that case though.
139      */
140     AcpiInstallNotifyHandler(sc->lid_handle, ACPI_DEVICE_NOTIFY,
141                              acpi_lid_notify_handler, sc);
142
143     /* Enable the GPE for wake/runtime. */
144     acpi_wake_set_enable(dev, 1);
145     if (acpi_parse_prw(sc->lid_handle, &prw) == 0)
146         AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit);
147
148     /* Get the initial lid status */
149     acpi_lid_status_update(sc);
150
151     /*
152      * Export the lid status
153      */
154     SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
155         SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
156         "state", CTLFLAG_RD, &sc->lid_status, 0,
157         "Device state (0 = closed, 1 = open)");
158
159     return (0);
160 }
161
162 static int
163 acpi_lid_suspend(device_t dev)
164 {
165     return (0);
166 }
167
168 static int
169 acpi_lid_resume(device_t dev)
170 {
171     struct acpi_lid_softc       *sc;
172
173     sc = device_get_softc(dev);
174
175     /* Update lid status, if any */
176     acpi_lid_status_update(sc);
177
178     return (0);
179 }
180
181 static void
182 acpi_lid_notify_status_changed(void *arg)
183 {
184     struct acpi_lid_softc       *sc;
185     struct acpi_softc           *acpi_sc;
186
187     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
188
189     sc = (struct acpi_lid_softc *)arg;
190     ACPI_SERIAL_BEGIN(lid);
191
192     /* Update lid status, if any */
193     acpi_lid_status_update(sc);
194
195     acpi_sc = acpi_device_get_parent_softc(sc->lid_dev);
196     if (acpi_sc == NULL)
197         goto out;
198
199     ACPI_VPRINT(sc->lid_dev, acpi_sc, "Lid %s\n",
200                 sc->lid_status ? "opened" : "closed");
201
202     acpi_UserNotify("Lid", sc->lid_handle, sc->lid_status);
203
204     if (sc->lid_status == 0)
205         EVENTHANDLER_INVOKE(acpi_sleep_event, acpi_sc->acpi_lid_switch_sx);
206     else
207         EVENTHANDLER_INVOKE(acpi_wakeup_event, acpi_sc->acpi_lid_switch_sx);
208
209 out:
210     ACPI_SERIAL_END(lid);
211     return_VOID;
212 }
213
214 /* XXX maybe not here */
215 #define ACPI_NOTIFY_STATUS_CHANGED      0x80
216
217 static void 
218 acpi_lid_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
219 {
220     struct acpi_lid_softc       *sc;
221
222     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
223
224     sc = (struct acpi_lid_softc *)context;
225     switch (notify) {
226     case ACPI_NOTIFY_STATUS_CHANGED:
227         AcpiOsExecute(OSL_NOTIFY_HANDLER,
228                       acpi_lid_notify_status_changed, sc);
229         break;
230     default:
231         device_printf(sc->lid_dev, "unknown notify %#x\n", notify);
232         break;
233     }
234
235     return_VOID;
236 }