3dc493b8e23b533b9539ab8ef8f5f1a054c90c15
[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.20 2003/10/25 05:03:24 njl Exp $
27  * $DragonFly: src/sys/dev/acpica5/acpi_acad.c,v 1.1 2004/02/21 06:48:08 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 <machine/resource.h>
37 #include <sys/rman.h>
38 #include <sys/ioccom.h>
39 #include <sys/malloc.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
88 static void
89 acpi_acad_get_status(void *context)
90 {
91     struct acpi_acad_softc *sc;
92     device_t    dev;
93     ACPI_HANDLE h;
94     int         newstatus;
95
96     dev = context;
97     sc = device_get_softc(dev);
98     h = acpi_get_handle(dev);
99     if (ACPI_FAILURE(acpi_EvaluateInteger(h, "_PSR", &newstatus))) {
100         sc->status = -1;
101         return;
102     }
103
104     if (sc->status != newstatus) {
105         sc->status = newstatus;
106
107         /* Set system power profile based on AC adapter status */
108         power_profile_set_state(sc->status ? POWER_PROFILE_PERFORMANCE :
109                                 POWER_PROFILE_ECONOMY);
110         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
111                     "%s Line\n", sc->status ? "On" : "Off");
112
113         acpi_UserNotify("ACAD", h, sc->status);
114     }
115 }
116
117 static void
118 acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
119 {
120     device_t dev = context;
121
122     ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
123                 "Notify 0x%x\n", notify);
124
125     switch (notify) {
126     case ACPI_DEVICE_CHECK_PNP:
127     case ACPI_DEVICE_CHECK_EXISTENCE:
128     case ACPI_POWERSOURCE_STAT_CHANGE:
129         /* Temporarily.  It is better to notify policy manager */
130         AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_get_status, context);
131         break;
132     default:
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 &&
141         acpi_MatchHid(dev, "ACPI0003")) {
142
143         device_set_desc(dev, "AC Adapter");
144         return (0);
145     }
146     return (ENXIO);
147 }
148
149 static int
150 acpi_acad_attach(device_t dev)
151 {
152     struct acpi_acad_softc *sc;
153     struct acpi_softc      *acpi_sc;
154     ACPI_HANDLE handle;
155     int         error;
156
157     sc = device_get_softc(dev);
158     if (sc == NULL)
159         return (ENXIO);
160     handle = acpi_get_handle(dev);
161
162     error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS, acpi_acad_ioctl, dev);
163     if (error != 0)
164         return (error);
165
166     if (device_get_unit(dev) == 0) {
167         acpi_sc = acpi_device_get_parent_softc(dev);
168         SYSCTL_ADD_PROC(&acpi_sc->acpi_sysctl_ctx,
169                         SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
170                         OID_AUTO, "acline", CTLTYPE_INT | CTLFLAG_RD,
171                         &sc->status, 0, acpi_acad_sysctl, "I", "");
172     }
173
174     /* Get initial status after whole system is up. */
175     sc->status = -1;
176     sc->initializing = 0;
177
178     /*
179      * Also install a system notify handler even though this is not
180      * required by the specification.  The Casio FIVA needs this.
181      */
182     AcpiInstallNotifyHandler(handle, ACPI_SYSTEM_NOTIFY,
183                              acpi_acad_notify_handler, dev);
184     AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
185                              acpi_acad_notify_handler, dev);
186     AcpiOsQueueForExecution(OSD_PRIORITY_LO, 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     if (sc == NULL)
200         return (ENXIO);
201
202     /*
203      * No security check required: information retrieval only.  If
204      * new functions are added here, a check might be required.
205      */
206     switch (cmd) {
207     case ACPIIO_ACAD_GET_STATUS:
208         acpi_acad_get_status(dev);
209         *(int *)addr = sc->status;
210         break;
211     default:
212         break;
213     }
214
215     return (0);
216 }
217
218 static int
219 acpi_acad_sysctl(SYSCTL_HANDLER_ARGS)
220 {
221     int val, error;
222
223     if (acpi_acad_get_acline(&val) != 0)
224         return (ENXIO);
225
226     val = *(u_int *)oidp->oid_arg1;
227     error = sysctl_handle_int(oidp, &val, 0, req);
228     return (error);
229 }
230
231 static void
232 acpi_acad_init_acline(void *arg)
233 {
234     struct acpi_acad_softc *sc;
235     device_t    dev;
236     int         retry, status;
237
238     dev = (device_t)arg;
239     sc = device_get_softc(dev);
240     if (sc->initializing)
241         return;
242
243     sc->initializing = 1;
244     ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
245                 "acline initialization start\n");
246
247     status = 0;
248     for (retry = 0; retry < ACPI_ACAD_RETRY_MAX; retry++) {
249         acpi_acad_get_status(dev);
250         if (status != sc->status)
251             break;
252         AcpiOsSleep(10, 0);
253     }
254
255     sc->initializing = 0;
256     ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
257                 "acline initialization done, tried %d times\n", retry + 1);
258 }
259
260 /*
261  * Public interfaces.
262  */
263 int
264 acpi_acad_get_acline(int *status)
265 {
266     struct acpi_acad_softc *sc;
267     device_t dev;
268
269     dev = devclass_get_device(acpi_acad_devclass, 0);
270     if (dev == NULL)
271         return (ENXIO);
272     sc = device_get_softc(dev);
273     if (sc == NULL)
274         return (ENXIO);
275
276     acpi_acad_get_status(dev);
277     *status = sc->status;
278
279     return (0);
280 }