Use lockmgr locks for ACPI_SERIAL_*
[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  * $FreeBSD: src/sys/dev/acpica/acpi_acad.c,v 1.39 2009/06/05 18:44:36 jkim Exp
26  */
27
28 #include <sys/cdefs.h>
29
30 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34
35 #include <sys/rman.h>
36 #include <sys/ioccom.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/conf.h>
40 #include <sys/power.h>
41
42 #include "acpi.h"
43
44 #include <dev/acpica5/acpivar.h>
45 #include <dev/acpica5/acpiio.h>
46 #include <bus/isa/isavar.h>
47 #include <bus/isa/pnpvar.h>
48  
49 /* Hooks for the ACPI CA debugging infrastructure */
50 #define _COMPONENT      ACPI_AC_ADAPTER
51 ACPI_MODULE_NAME("AC_ADAPTER")
52
53 /* Number of times to retry initialization before giving up. */
54 #define ACPI_ACAD_RETRY_MAX             6
55
56 #define ACPI_POWERSOURCE_STAT_CHANGE    0x80
57
58 struct  acpi_acad_softc {
59     int status;
60 };
61
62 static void     acpi_acad_get_status(void *);
63 static void     acpi_acad_notify_handler(ACPI_HANDLE, UINT32, void *);
64 static int      acpi_acad_probe(device_t);
65 static int      acpi_acad_attach(device_t);
66 static int      acpi_acad_ioctl(u_long, caddr_t, void *);
67 static int      acpi_acad_sysctl(SYSCTL_HANDLER_ARGS);
68 static void     acpi_acad_init_acline(void *arg);
69 static void     acpi_acad_ac_only(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 ACPI_SERIAL_DECL(acad, "ACPI AC adapter");
90
91 SYSINIT(acad, SI_SUB_KTHREAD_IDLE, SI_ORDER_FIRST, acpi_acad_ac_only, NULL);
92
93 static void
94 acpi_acad_get_status(void *context)
95 {
96     struct acpi_acad_softc *sc;
97     device_t    dev;
98     ACPI_HANDLE h;
99     int         newstatus;
100
101     dev = context;
102     sc = device_get_softc(dev);
103     h = acpi_get_handle(dev);
104     newstatus = -1;
105     acpi_GetInteger(h, "_PSR", &newstatus);
106
107     /* If status is valid and has changed, notify the system. */
108     ACPI_SERIAL_BEGIN(acad);
109     if (newstatus != -1 && sc->status != newstatus) {
110         sc->status = newstatus;
111         power_profile_set_state(newstatus ? POWER_PROFILE_PERFORMANCE :
112             POWER_PROFILE_ECONOMY);
113         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
114             "%s Line\n", newstatus ? "On" : "Off");
115         acpi_UserNotify("ACAD", h, newstatus);
116     }
117     ACPI_SERIAL_END(acad);
118 }
119
120 static void
121 acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
122 {
123     device_t dev;
124
125     dev = (device_t)context;
126     switch (notify) {
127     case ACPI_NOTIFY_BUS_CHECK:
128     case ACPI_NOTIFY_DEVICE_CHECK:
129     case ACPI_POWERSOURCE_STAT_CHANGE:
130         /* Temporarily.  It is better to notify policy manager */
131         AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_acad_get_status, context);
132         break;
133     default:
134         device_printf(dev, "unknown notify %#x\n", notify);
135         break;
136     }
137 }
138
139 static int
140 acpi_acad_probe(device_t dev)
141 {
142     static char *acad_ids[] = { "ACPI0003", NULL };
143
144     if (acpi_disabled("acad") ||
145         ACPI_ID_PROBE(device_get_parent(dev), dev, acad_ids) == NULL)
146         return (ENXIO);
147
148     device_set_desc(dev, "AC Adapter");
149     return (0);
150 }
151
152 static int
153 acpi_acad_attach(device_t dev)
154 {
155     struct acpi_acad_softc *sc;
156     struct acpi_softc      *acpi_sc;
157     ACPI_HANDLE handle;
158     int         error;
159
160     sc = device_get_softc(dev);
161     handle = acpi_get_handle(dev);
162
163     error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS, acpi_acad_ioctl, dev);
164     if (error != 0)
165         return (error);
166
167     ACPI_SERIAL_INIT(acad);
168
169     if (device_get_unit(dev) == 0) {
170         acpi_sc = acpi_device_get_parent_softc(dev);
171         SYSCTL_ADD_PROC(&acpi_sc->acpi_sysctl_ctx,
172                         SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
173                         OID_AUTO, "acline", CTLTYPE_INT | CTLFLAG_RD,
174                         &sc->status, 0, acpi_acad_sysctl, "I", "");
175     }
176
177     /* Get initial status after whole system is up. */
178     sc->status = -1;
179
180     /*
181      * Install both system and device notify handlers since the Casio
182      * FIVA needs them.
183      */
184     AcpiInstallNotifyHandler(handle, ACPI_ALL_NOTIFY,
185                              acpi_acad_notify_handler, dev);
186     AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_acad_init_acline, dev);
187
188     return (0);
189 }
190
191 static int
192 acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg)
193 {
194     struct acpi_acad_softc *sc;
195     device_t dev;
196
197     dev = (device_t)arg;
198     sc = device_get_softc(dev);
199
200     /*
201      * No security check required: information retrieval only.  If
202      * new functions are added here, a check might be required.
203      */
204     switch (cmd) {
205     case ACPIIO_ACAD_GET_STATUS:
206         acpi_acad_get_status(dev);
207         *(int *)addr = sc->status;
208         break;
209     default:
210         break;
211     }
212
213     return (0);
214 }
215
216 static int
217 acpi_acad_sysctl(SYSCTL_HANDLER_ARGS)
218 {
219     int val, error;
220
221     if (acpi_acad_get_acline(&val) != 0)
222         return (ENXIO);
223
224     val = *(u_int *)oidp->oid_arg1;
225     error = sysctl_handle_int(oidp, &val, 0, req);
226     return (error);
227 }
228
229 static void
230 acpi_acad_init_acline(void *arg)
231 {
232     struct acpi_acad_softc *sc;
233     device_t    dev;
234     int         retry;
235
236     dev = (device_t)arg;
237     sc = device_get_softc(dev);
238     ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
239                 "acline initialization start\n");
240
241     for (retry = 0; retry < ACPI_ACAD_RETRY_MAX; retry++) {
242         acpi_acad_get_status(dev);
243         if (sc->status != -1)
244             break;
245         AcpiOsSleep(10000);
246     }
247
248     ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
249                 "acline initialization done, tried %d times\n", retry + 1);
250 }
251
252 /*
253  * If no AC line devices detected after boot, create an "online" event
254  * so that userland code can adjust power settings accordingly.  The default
255  * power profile is "performance" so we don't need to repeat that here.
256  */
257 static void
258 acpi_acad_ac_only(void __unused *arg)
259 {
260
261     if (devclass_get_count(acpi_acad_devclass) == 0)
262         acpi_UserNotify("ACAD", ACPI_ROOT_OBJECT, 1);
263 }
264
265 /*
266  * Public interfaces.
267  */
268 int
269 acpi_acad_get_acline(int *status)
270 {
271     struct acpi_acad_softc *sc;
272     device_t dev;
273
274     dev = devclass_get_device(acpi_acad_devclass, 0);
275     if (dev == NULL)
276         return (ENXIO);
277     sc = device_get_softc(dev);
278
279     acpi_acad_get_status(dev);
280     *status = sc->status;
281
282     return (0);
283 }