Rename Makefile.inc to Makefile.sub
[dragonfly.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  *      $FreeBSD: src/sys/dev/acpica/acpi_lid.c,v 1.12.6.1 2003/08/22 20:49:20 jhb Exp $
30  *      $DragonFly: src/sys/dev/acpica/Attic/acpi_lid.c,v 1.1 2003/09/24 03:32:16 drhodus Exp $ 
31  */
32
33 #include "opt_acpi.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/bus.h>
37 #include <sys/proc.h>
38
39 #include "acpi.h"
40
41 #include <dev/acpica/acpivar.h>
42
43 /*
44  * Hooks for the ACPI CA debugging infrastructure
45  */
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 static int      acpi_lid_probe(device_t dev);
56 static int      acpi_lid_attach(device_t dev);
57 static int      acpi_lid_suspend(device_t dev);
58 static int      acpi_lid_resume(device_t dev);
59 static void     acpi_lid_notify_status_changed(void *arg);
60 static void     acpi_lid_notify_handler(ACPI_HANDLE h,UINT32 notify, void *context);
61
62 static device_method_t acpi_lid_methods[] = {
63     /* Device interface */
64     DEVMETHOD(device_probe,     acpi_lid_probe),
65     DEVMETHOD(device_attach,    acpi_lid_attach),
66     DEVMETHOD(device_suspend,   acpi_lid_suspend),
67     DEVMETHOD(device_resume,    acpi_lid_resume),
68
69     {0, 0}
70 };
71
72 static driver_t acpi_lid_driver = {
73     "acpi_lid",
74     acpi_lid_methods,
75     sizeof(struct acpi_lid_softc),
76 };
77
78 static devclass_t acpi_lid_devclass;
79 DRIVER_MODULE(acpi_lid, acpi, acpi_lid_driver, acpi_lid_devclass, 0, 0);
80
81 static int
82 acpi_lid_probe(device_t dev)
83 {
84     if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) && 
85         !acpi_disabled("lid") &&
86         acpi_MatchHid(dev, "PNP0C0D")) {
87         device_set_desc(dev, "Control Method Lid Switch");
88         return(0);
89     }
90     return(ENXIO);
91 }
92
93 static int
94 acpi_lid_attach(device_t dev)
95 {
96     struct acpi_lid_softc       *sc;
97
98     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
99
100     sc = device_get_softc(dev);
101     sc->lid_dev = dev;
102     sc->lid_handle = acpi_get_handle(dev);
103
104     /*
105      * Install notification handler
106      */
107     AcpiInstallNotifyHandler(sc->lid_handle, ACPI_DEVICE_NOTIFY, acpi_lid_notify_handler, sc);
108     acpi_device_enable_wake_capability(sc->lid_handle, 1);
109     return_VALUE(0);
110 }
111
112 static int
113 acpi_lid_suspend(device_t dev)
114 {
115     struct acpi_lid_softc       *sc;
116
117     sc = device_get_softc(dev);
118     acpi_device_enable_wake_event(sc->lid_handle);
119     return (0);
120 }
121
122 static int
123 acpi_lid_resume(device_t dev)
124 {
125     return (0);
126 }
127
128 static void
129 acpi_lid_notify_status_changed(void *arg)
130 {
131     struct acpi_lid_softc       *sc;
132     struct acpi_softc           *acpi_sc;
133
134     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
135
136     sc = (struct acpi_lid_softc *)arg;
137
138     /*
139      * Evaluate _LID and check the return value, update lid status.
140      *  Zero:           The lid is closed
141      *  Non-zero:       The lid is open
142      */
143     if (ACPI_FAILURE(acpi_EvaluateInteger(sc->lid_handle, "_LID", &sc->lid_status)))
144         return_VOID;
145
146     acpi_sc = acpi_device_get_parent_softc(sc->lid_dev);
147     if (acpi_sc == NULL) {
148         return_VOID;
149     }
150
151     ACPI_VPRINT(sc->lid_dev, acpi_sc,
152         "Lid %s\n", sc->lid_status ? "opened" : "closed");
153
154     if (sc->lid_status == 0) {
155         EVENTHANDLER_INVOKE(acpi_sleep_event, acpi_sc->acpi_lid_switch_sx);
156     } else {
157         EVENTHANDLER_INVOKE(acpi_wakeup_event, acpi_sc->acpi_lid_switch_sx);
158     }
159
160     return_VOID;
161 }
162
163 /* XXX maybe not here */
164 #define ACPI_NOTIFY_STATUS_CHANGED      0x80
165
166 static void 
167 acpi_lid_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
168 {
169     struct acpi_lid_softc       *sc = (struct acpi_lid_softc *)context;
170
171     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
172
173     switch (notify) {
174     case ACPI_NOTIFY_STATUS_CHANGED:
175         AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_lid_notify_status_changed, sc);
176         break;
177     default:
178         break;          /* unknown notification value */
179     }
180     return_VOID;
181 }
182