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