Rip out the badly designed softint-based taskqueue used by ACPI for callbacks.
[dragonfly.git] / sys / dev / acpica5 / acpi_acad.c
1 /*-
2  * Copyright (c) 2000 Takanori Watanabe
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/acpica/acpi_acad.c,v 1.26 2004/05/30 20:08:23 phk Exp $
27  * $DragonFly: src/sys/dev/acpica5/acpi_acad.c,v 1.4 2004/08/02 19:51:07 dillon Exp $
28  */
29
30 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34
35 #include <machine/bus.h>
36 #include <sys/rman.h>
37 #include <sys/ioccom.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/conf.h>
41 #include <sys/power.h>
42
43 #include "acpi.h"
44 #include <dev/acpica5/acpivar.h>
45 #include <dev/acpica5/acpiio.h>
46  
47 /* Hooks for the ACPI CA debugging infrastructure */
48 #define _COMPONENT      ACPI_AC_ADAPTER
49 ACPI_MODULE_NAME("AC_ADAPTER")
50
51 /* Number of times to retry initialization before giving up. */
52 #define ACPI_ACAD_RETRY_MAX             6
53
54 #define ACPI_DEVICE_CHECK_PNP           0x00
55 #define ACPI_DEVICE_CHECK_EXISTENCE     0x01
56 #define ACPI_POWERSOURCE_STAT_CHANGE    0x80
57
58 struct  acpi_acad_softc {
59     int status;
60     int initializing;
61 };
62
63 static void     acpi_acad_get_status(void *);
64 static void     acpi_acad_notify_handler(ACPI_HANDLE, UINT32, void *);
65 static int      acpi_acad_probe(device_t);
66 static int      acpi_acad_attach(device_t);
67 static int      acpi_acad_ioctl(u_long, caddr_t, void *);
68 static int      acpi_acad_sysctl(SYSCTL_HANDLER_ARGS);
69 static void     acpi_acad_init_acline(void *arg);
70
71 static device_method_t acpi_acad_methods[] = {
72     /* Device interface */
73     DEVMETHOD(device_probe,     acpi_acad_probe),
74     DEVMETHOD(device_attach,    acpi_acad_attach),
75
76     {0, 0}
77 };
78
79 static driver_t acpi_acad_driver = {
80     "acpi_acad",
81     acpi_acad_methods,
82     sizeof(struct acpi_acad_softc),
83 };
84
85 static devclass_t acpi_acad_devclass;
86 DRIVER_MODULE(acpi_acad, acpi, acpi_acad_driver, acpi_acad_devclass, 0, 0);
87 MODULE_DEPEND(acpi_acad, acpi, 1, 1, 1);
88
89 static void
90 acpi_acad_get_status(void *context)
91 {
92     struct acpi_acad_softc *sc;
93     device_t    dev;
94     ACPI_HANDLE h;
95     int         newstatus;
96
97     dev = context;
98     sc = device_get_softc(dev);
99     h = acpi_get_handle(dev);
100     if (ACPI_FAILURE(acpi_GetInteger(h, "_PSR", &newstatus))) {
101         sc->status = -1;
102         return;
103     }
104
105     if (sc->status != newstatus) {
106         sc->status = newstatus;
107
108         /* Set system power profile based on AC adapter status */
109         power_profile_set_state(sc->status ? POWER_PROFILE_PERFORMANCE :
110                                 POWER_PROFILE_ECONOMY);
111         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
112                     "%s Line\n", sc->status ? "On" : "Off");
113
114         acpi_UserNotify("ACAD", h, sc->status);
115     }
116 }
117
118 static void
119 acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
120 {
121     device_t dev;
122
123     dev = (device_t)context;
124     switch (notify) {
125     case ACPI_DEVICE_CHECK_PNP:
126     case ACPI_DEVICE_CHECK_EXISTENCE:
127     case ACPI_POWERSOURCE_STAT_CHANGE:
128         /* Temporarily.  It is better to notify policy manager */
129         AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_get_status, context);
130         break;
131     default:
132         device_printf(dev, "unknown notify %#x\n", notify);
133         break;
134     }
135 }
136
137 static int
138 acpi_acad_probe(device_t dev)
139 {
140     if (acpi_get_type(dev) == ACPI_TYPE_DEVICE && !acpi_disabled("acad") &&
141         acpi_MatchHid(dev, "ACPI0003")) {
142         device_set_desc(dev, "AC Adapter");
143         return (0);
144     }
145     return (ENXIO);
146 }
147
148 static int
149 acpi_acad_attach(device_t dev)
150 {
151     struct acpi_acad_softc *sc;
152     struct acpi_softc      *acpi_sc;
153     ACPI_HANDLE handle;
154     int         error;
155
156     sc = device_get_softc(dev);
157     if (sc == NULL)
158         return (ENXIO);
159     handle = acpi_get_handle(dev);
160
161     error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS, acpi_acad_ioctl, dev);
162     if (error != 0)
163         return (error);
164
165     if (device_get_unit(dev) == 0) {
166         acpi_sc = acpi_device_get_parent_softc(dev);
167         SYSCTL_ADD_PROC(&acpi_sc->acpi_sysctl_ctx,
168                         SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
169                         OID_AUTO, "acline", CTLTYPE_INT | CTLFLAG_RD,
170                         &sc->status, 0, acpi_acad_sysctl, "I", "");
171     }
172
173     /* Get initial status after whole system is up. */
174     sc->status = -1;
175     sc->initializing = 0;
176
177     /*
178      * Also install a system notify handler even though this is not
179      * required by the specification.  The Casio FIVA needs this.
180      */
181     AcpiInstallNotifyHandler(handle, ACPI_SYSTEM_NOTIFY,
182                              acpi_acad_notify_handler, dev);
183     AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
184                              acpi_acad_notify_handler, dev);
185     AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_init_acline, dev);
186
187     return (0);
188 }
189
190 static int
191 acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg)
192 {
193     struct acpi_acad_softc *sc;
194     device_t dev;
195
196     dev = (device_t)arg;
197     sc = device_get_softc(dev);
198     if (sc == NULL)
199         return (ENXIO);
200
201     /*
202      * No security check required: information retrieval only.  If
203      * new functions are added here, a check might be required.
204      */
205     switch (cmd) {
206     case ACPIIO_ACAD_GET_STATUS:
207         acpi_acad_get_status(dev);
208         *(int *)addr = sc->status;
209         break;
210     default:
211         break;
212     }
213
214     return (0);
215 }
216
217 static int
218 acpi_acad_sysctl(SYSCTL_HANDLER_ARGS)
219 {
220     int val, error;
221
222     if (acpi_acad_get_acline(&val) != 0)
223         return (ENXIO);
224
225     val = *(u_int *)oidp->oid_arg1;
226     error = sysctl_handle_int(oidp, &val, 0, req);
227     return (error);
228 }
229
230 static void
231 acpi_acad_init_acline(void *arg)
232 {
233     struct acpi_acad_softc *sc;
234     device_t    dev;
235     int         retry, status;
236
237     dev = (device_t)arg;
238     sc = device_get_softc(dev);
239     if (sc->initializing)
240         return;
241
242     sc->initializing = 1;
243     ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
244                 "acline initialization start\n");
245
246     status = 0;
247     for (retry = 0; retry < ACPI_ACAD_RETRY_MAX; retry++) {
248         acpi_acad_get_status(dev);
249         if (status != sc->status)
250             break;
251         AcpiOsSleep(0, 10);
252     }
253
254     sc->initializing = 0;
255     ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
256                 "acline initialization done, tried %d times\n", retry + 1);
257 }
258
259 /*
260  * Public interfaces.
261  */
262 int
263 acpi_acad_get_acline(int *status)
264 {
265     struct acpi_acad_softc *sc;
266     device_t dev;
267
268     dev = devclass_get_device(acpi_acad_devclass, 0);
269     if (dev == NULL)
270         return (ENXIO);
271     sc = device_get_softc(dev);
272     if (sc == NULL)
273         return (ENXIO);
274
275     acpi_acad_get_status(dev);
276     *status = sc->status;
277
278     return (0);
279 }