2 * Copyright (c) 2000 Takanori Watanabe
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/dev/acpica/acpi_acad.c,v 1.17.2.1 2003/08/22 20:49:20 jhb Exp $
27 * $DragonFly: src/sys/dev/acpica/Attic/acpi_acad.c,v 1.1 2003/09/24 03:32:16 drhodus Exp $
31 #include <sys/param.h>
32 #include <sys/kernel.h>
35 #include <machine/bus.h>
36 #include <machine/resource.h>
38 #include <sys/ioccom.h>
39 #include <sys/malloc.h>
41 #include <sys/power.h>
44 #include <dev/acpica/acpivar.h>
45 #include <dev/acpica/acpiio.h>
48 * Hooks for the ACPI CA debugging infrastructure
50 #define _COMPONENT ACPI_AC_ADAPTER
51 ACPI_MODULE_NAME("AC_ADAPTER")
53 #define ACPI_DEVICE_CHECK_PNP 0x00
54 #define ACPI_DEVICE_CHECK_EXISTENCE 0x01
55 #define ACPI_POWERSOURCE_STAT_CHANGE 0x80
57 static void acpi_acad_get_status(void * );
58 static void acpi_acad_notify_handler(ACPI_HANDLE , UINT32 ,void *);
59 static int acpi_acad_probe(device_t);
60 static int acpi_acad_attach(device_t);
61 static int acpi_acad_ioctl(u_long, caddr_t, void *);
62 static int acpi_acad_sysctl(SYSCTL_HANDLER_ARGS);
63 static void acpi_acad_init_acline(void *arg);
65 struct acpi_acad_softc {
72 acpi_acad_get_status(void *context)
75 device_t dev = context;
76 struct acpi_acad_softc *sc = device_get_softc(dev);
77 ACPI_HANDLE h = acpi_get_handle(dev);
79 if (ACPI_FAILURE(acpi_EvaluateInteger(h, "_PSR", &newstatus))) {
84 if (sc->status != newstatus) {
85 sc->status = newstatus;
86 /* set system power profile based on AC adapter status */
87 power_profile_set_state(sc->status ? POWER_PROFILE_PERFORMANCE : POWER_PROFILE_ECONOMY);
88 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
89 "%s Line\n",(sc->status) ? "On" : "Off");
94 acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
96 device_t dev = context;
98 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
99 "Notify %d\n", notify);
102 case ACPI_DEVICE_CHECK_PNP:
103 case ACPI_DEVICE_CHECK_EXISTENCE:
104 case ACPI_POWERSOURCE_STAT_CHANGE:
105 /*Temporally. It is better to notify policy manager*/
106 AcpiOsQueueForExecution(OSD_PRIORITY_LO,
107 acpi_acad_get_status,context);
115 acpi_acad_probe(device_t dev)
118 if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
119 acpi_MatchHid(dev, "ACPI0003")) {
122 * Set device description
124 device_set_desc(dev, "AC adapter");
131 acpi_acad_attach(device_t dev)
134 struct acpi_acad_softc *sc;
135 struct acpi_softc *acpi_sc;
137 ACPI_HANDLE handle = acpi_get_handle(dev);
138 AcpiInstallNotifyHandler(handle,
140 acpi_acad_notify_handler, dev);
141 /*Installing system notify is not so good*/
142 AcpiInstallNotifyHandler(handle,
144 acpi_acad_notify_handler, dev);
146 if ((sc = device_get_softc(dev)) == NULL) {
149 if ((error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS,
150 acpi_acad_ioctl, dev)) != 0) {
154 if (device_get_unit(dev) == 0) {
155 acpi_sc = acpi_device_get_parent_softc(dev);
156 SYSCTL_ADD_PROC(&acpi_sc->acpi_sysctl_ctx,
157 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
158 OID_AUTO, "acline", CTLTYPE_INT | CTLFLAG_RD,
159 &sc->status, 0, acpi_acad_sysctl, "I", "");
162 /* Get initial status after whole system is up. */
164 sc->initializing = 0;
166 AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_init_acline, dev);
171 static device_method_t acpi_acad_methods[] = {
172 /* Device interface */
173 DEVMETHOD(device_probe, acpi_acad_probe),
174 DEVMETHOD(device_attach, acpi_acad_attach),
179 static driver_t acpi_acad_driver = {
182 sizeof(struct acpi_acad_softc),
185 static devclass_t acpi_acad_devclass;
186 DRIVER_MODULE(acpi_acad,acpi,acpi_acad_driver,acpi_acad_devclass,0,0);
189 acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg)
192 struct acpi_acad_softc *sc;
195 if ((sc = device_get_softc(dev)) == NULL) {
200 * No security check required: information retrieval only. If
201 * new functions are added here, a check might be required.
205 case ACPIIO_ACAD_GET_STATUS:
206 acpi_acad_get_status(dev);
207 *(int *)addr = sc->status;
215 acpi_acad_sysctl(SYSCTL_HANDLER_ARGS)
220 if (acpi_acad_get_acline(&val)) {
224 val = *(u_int *)oidp->oid_arg1;
225 error = sysctl_handle_int(oidp, &val, 0, req);
230 acpi_acad_init_acline(void *arg)
234 device_t dev = (device_t)arg;
235 struct acpi_acad_softc *sc = device_get_softc(dev);
236 #define ACPI_ACAD_RETRY_MAX 6
238 if (sc->initializing) {
242 sc->initializing = 1;
244 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
245 "acline initialization start\n");
248 for (retry = 0; retry < ACPI_ACAD_RETRY_MAX; retry++, AcpiOsSleep(10, 0)) {
249 acpi_acad_get_status(dev);
250 if (status != sc->status)
254 ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
255 "acline initialization done, tried %d times\n", retry+1);
257 sc->initializing = 0;
265 acpi_acad_get_acline(int *status)
268 struct acpi_acad_softc *sc;
270 if ((dev = devclass_get_device(acpi_acad_devclass, 0)) == NULL) {
274 if ((sc = device_get_softc(dev)) == NULL) {
278 acpi_acad_get_status(dev);
279 *status = sc->status;