xref: /freebsd/sys/dev/acpica/acpi_lid.c (revision 6971b3c7d12b77bbcb2662221234b370fc6cf46b)
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  *	$FreeBSD$
30  */
31 
32 #include "opt_acpi.h"
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/proc.h>
39 
40 #include "acpi.h"
41 
42 #include <dev/acpica/acpivar.h>
43 
44 /*
45  * Hooks for the ACPI CA debugging infrastructure
46  */
47 #define _COMPONENT	ACPI_BUTTON
48 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 static int	acpi_lid_probe(device_t dev);
57 static int	acpi_lid_attach(device_t dev);
58 static void	acpi_lid_notify_status_changed(void *arg);
59 static void 	acpi_lid_notify_handler(ACPI_HANDLE h,UINT32 notify, void *context);
60 
61 static device_method_t acpi_lid_methods[] = {
62     /* Device interface */
63     DEVMETHOD(device_probe,	acpi_lid_probe),
64     DEVMETHOD(device_attach,	acpi_lid_attach),
65 
66     {0, 0}
67 };
68 
69 static driver_t acpi_lid_driver = {
70     "acpi_lid",
71     acpi_lid_methods,
72     sizeof(struct acpi_lid_softc),
73 };
74 
75 devclass_t acpi_lid_devclass;
76 DRIVER_MODULE(acpi_lid, acpi, acpi_lid_driver, acpi_lid_devclass, 0, 0);
77 
78 static int
79 acpi_lid_probe(device_t dev)
80 {
81     if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
82 	!acpi_disabled("lid") &&
83 	acpi_MatchHid(dev, "PNP0C0D")) {
84 	device_set_desc(dev, "Control Method Lid Switch");
85 	return(0);
86     }
87     return(ENXIO);
88 }
89 
90 static int
91 acpi_lid_attach(device_t dev)
92 {
93     struct acpi_lid_softc	*sc;
94 
95     FUNCTION_TRACE(__func__);
96 
97     sc = device_get_softc(dev);
98     sc->lid_dev = dev;
99     sc->lid_handle = acpi_get_handle(dev);
100 
101     /*
102      * Install notification handler
103      */
104     AcpiInstallNotifyHandler(sc->lid_handle, ACPI_DEVICE_NOTIFY, acpi_lid_notify_handler, sc);
105     return_VALUE(0);
106 }
107 
108 static void
109 acpi_lid_notify_status_changed(void *arg)
110 {
111     struct acpi_lid_softc	*sc;
112     struct acpi_softc		*acpi_sc;
113 
114     FUNCTION_TRACE(__func__);
115 
116     sc = (struct acpi_lid_softc *)arg;
117 
118     /*
119      * Evaluate _LID and check the return value, update lid status.
120      *	Zero:		The lid is closed
121      *	Non-zero:	The lid is open
122      */
123     if (acpi_EvaluateInteger(sc->lid_handle, "_LID", &sc->lid_status) != AE_OK)
124 	return_VOID;
125 
126     acpi_sc = acpi_device_get_parent_softc(sc->lid_dev);
127     if (acpi_sc == NULL) {
128         return_VOID;
129     }
130 
131     ACPI_VPRINT(sc->lid_dev, acpi_sc,
132 	"Lid %s\n", sc->lid_status ? "opened" : "closed");
133 
134     if (sc->lid_status == 0) {
135 	EVENTHANDLER_INVOKE(acpi_sleep_event, acpi_sc->acpi_lid_switch_sx);
136     } else {
137 	EVENTHANDLER_INVOKE(acpi_wakeup_event, acpi_sc->acpi_lid_switch_sx);
138     }
139 
140     return_VOID;
141 }
142 
143 /* XXX maybe not here */
144 #define	ACPI_NOTIFY_STATUS_CHANGED	0x80
145 
146 static void
147 acpi_lid_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
148 {
149     struct acpi_lid_softc	*sc = (struct acpi_lid_softc *)context;
150 
151     FUNCTION_TRACE_U32(__func__, notify);
152 
153     switch (notify) {
154     case ACPI_NOTIFY_STATUS_CHANGED:
155 	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_lid_notify_status_changed, sc);
156 	break;
157     default:
158 	break;		/* unknown notification value */
159     }
160     return_VOID;
161 }
162 
163