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