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