xref: /freebsd/sys/dev/acpica/acpi_lid.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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 
37 #include "acpi.h"
38 
39 #include <dev/acpica/acpivar.h>
40 
41 struct acpi_lid_softc {
42     device_t	lid_dev;
43     ACPI_HANDLE	lid_handle;
44     int		lid_status;	/* open or closed */
45 };
46 
47 static int	acpi_lid_probe(device_t dev);
48 static int	acpi_lid_attach(device_t dev);
49 static void	acpi_lid_notify_status_changed(void *arg);
50 static void 	acpi_lid_notify_handler(ACPI_HANDLE h,UINT32 notify, void *context);
51 
52 static device_method_t acpi_lid_methods[] = {
53     /* Device interface */
54     DEVMETHOD(device_probe,	acpi_lid_probe),
55     DEVMETHOD(device_attach,	acpi_lid_attach),
56 
57     {0, 0}
58 };
59 
60 static driver_t acpi_lid_driver = {
61     "acpi_lid",
62     acpi_lid_methods,
63     sizeof(struct acpi_lid_softc),
64 };
65 
66 devclass_t acpi_lid_devclass;
67 DRIVER_MODULE(acpi_lid, acpi, acpi_lid_driver, acpi_lid_devclass, 0, 0);
68 
69 static int
70 acpi_lid_probe(device_t dev)
71 {
72     if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
73 	acpi_MatchHid(dev, "PNP0C0D")) {
74 	device_set_desc(dev, "Control Method Lid Switch");
75 	return(0);
76     }
77     return(ENXIO);
78 }
79 
80 static int
81 acpi_lid_attach(device_t dev)
82 {
83     struct acpi_lid_softc	*sc;
84 
85     sc = device_get_softc(dev);
86     sc->lid_dev = dev;
87     sc->lid_handle = acpi_get_handle(dev);
88 
89     /*
90      * Install notification handler
91      */
92     AcpiInstallNotifyHandler(sc->lid_handle, ACPI_DEVICE_NOTIFY, acpi_lid_notify_handler, sc);
93     return(0);
94 }
95 
96 static void
97 acpi_lid_notify_status_changed(void *arg)
98 {
99     struct acpi_lid_softc	*sc;
100     struct acpi_softc		*acpi_sc;
101     ACPI_BUFFER			Buffer;
102     ACPI_OBJECT			Object;
103 
104     sc = (struct acpi_lid_softc *)arg;
105 
106     /*
107      * Evaluate _LID and check the return value
108      *	Zero:		The lid is closed
109      *	Non-zero:	The lid is open
110      */
111     Buffer.Length = sizeof(Object);
112     Buffer.Pointer = &Object;
113     if (AcpiEvaluateObject(sc->lid_handle, "_LID", NULL, &Buffer) != AE_OK)
114 	return;
115     if (Object.Type != ACPI_TYPE_NUMBER)
116 	return;
117 
118     /*
119      * Update lid status
120     */
121     sc->lid_status = Object.Number.Value;
122     device_printf(sc->lid_dev, "Lid %s\n", sc->lid_status ? "opened" : "closed");
123 
124     acpi_sc = acpi_device_get_parent_softc(sc->lid_dev);
125     if (acpi_sc == NULL) {
126         return;
127     }
128 
129     if (sc->lid_status == 0) {
130 	EVENTHANDLER_INVOKE(acpi_sleep_event, acpi_sc->acpi_lid_switch_sx);
131     } else {
132 	EVENTHANDLER_INVOKE(acpi_wakeup_event, acpi_sc->acpi_lid_switch_sx);
133     }
134 }
135 
136 /* XXX maybe not here */
137 #define	ACPI_NOTIFY_STATUS_CHANGED	0x80
138 
139 static void
140 acpi_lid_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
141 {
142     struct acpi_lid_softc	*sc = (struct acpi_lid_softc *)context;
143 
144     switch (notify) {
145     case ACPI_NOTIFY_STATUS_CHANGED:
146 	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_lid_notify_status_changed, sc);
147 	break;
148     default:
149 	return;		/* unknown notification value */
150     }
151 }
152 
153