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