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