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.26 2004/05/30 20:08:23 phk Exp $
27 * $DragonFly: src/sys/dev/acpica5/acpi_acad.c,v 1.7 2007/01/17 18:31:19 y0netan1 Exp $
31 #include <sys/param.h>
32 #include <sys/kernel.h>
35 #include <sys/ioccom.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
39 #include <sys/power.h>
42 #include <dev/acpica5/acpivar.h>
43 #include <dev/acpica5/acpiio.h>
45 /* Hooks for the ACPI CA debugging infrastructure */
46 #define _COMPONENT ACPI_AC_ADAPTER
47 ACPI_MODULE_NAME("AC_ADAPTER")
49 /* Number of times to retry initialization before giving up. */
50 #define ACPI_ACAD_RETRY_MAX 6
52 #define ACPI_DEVICE_CHECK_PNP 0x00
53 #define ACPI_DEVICE_CHECK_EXISTENCE 0x01
54 #define ACPI_POWERSOURCE_STAT_CHANGE 0x80
56 struct acpi_acad_softc
{
61 static void acpi_acad_get_status(void *);
62 static void acpi_acad_notify_handler(ACPI_HANDLE
, UINT32
, void *);
63 static int acpi_acad_probe(device_t
);
64 static int acpi_acad_attach(device_t
);
65 static int acpi_acad_ioctl(u_long
, caddr_t
, void *);
66 static int acpi_acad_sysctl(SYSCTL_HANDLER_ARGS
);
67 static void acpi_acad_init_acline(void *arg
);
69 static device_method_t acpi_acad_methods
[] = {
70 /* Device interface */
71 DEVMETHOD(device_probe
, acpi_acad_probe
),
72 DEVMETHOD(device_attach
, acpi_acad_attach
),
77 static driver_t acpi_acad_driver
= {
80 sizeof(struct acpi_acad_softc
),
83 static devclass_t acpi_acad_devclass
;
84 DRIVER_MODULE(acpi_acad
, acpi
, acpi_acad_driver
, acpi_acad_devclass
, 0, 0);
85 MODULE_DEPEND(acpi_acad
, acpi
, 1, 1, 1);
88 acpi_acad_get_status(void *context
)
90 struct acpi_acad_softc
*sc
;
96 sc
= device_get_softc(dev
);
97 h
= acpi_get_handle(dev
);
98 if (ACPI_FAILURE(acpi_GetInteger(h
, "_PSR", &newstatus
))) {
103 if (sc
->status
!= newstatus
) {
104 sc
->status
= newstatus
;
106 /* Set system power profile based on AC adapter status */
107 power_profile_set_state(sc
->status
? POWER_PROFILE_PERFORMANCE
:
108 POWER_PROFILE_ECONOMY
);
109 ACPI_VPRINT(dev
, acpi_device_get_parent_softc(dev
),
110 "%s Line\n", sc
->status
? "On" : "Off");
112 acpi_UserNotify("ACAD", h
, sc
->status
);
117 acpi_acad_notify_handler(ACPI_HANDLE h
, UINT32 notify
, void *context
)
121 dev
= (device_t
)context
;
123 case ACPI_DEVICE_CHECK_PNP
:
124 case ACPI_DEVICE_CHECK_EXISTENCE
:
125 case ACPI_POWERSOURCE_STAT_CHANGE
:
126 /* Temporarily. It is better to notify policy manager */
127 AcpiOsExecute(OSL_NOTIFY_HANDLER
, acpi_acad_get_status
, context
);
130 device_printf(dev
, "unknown notify %#x\n", notify
);
136 acpi_acad_probe(device_t dev
)
138 if (acpi_get_type(dev
) == ACPI_TYPE_DEVICE
&& !acpi_disabled("acad") &&
139 acpi_MatchHid(dev
, "ACPI0003")) {
140 device_set_desc(dev
, "AC Adapter");
147 acpi_acad_attach(device_t dev
)
149 struct acpi_acad_softc
*sc
;
150 struct acpi_softc
*acpi_sc
;
154 sc
= device_get_softc(dev
);
157 handle
= acpi_get_handle(dev
);
159 error
= acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS
, acpi_acad_ioctl
, dev
);
163 if (device_get_unit(dev
) == 0) {
164 acpi_sc
= acpi_device_get_parent_softc(dev
);
165 SYSCTL_ADD_PROC(&acpi_sc
->acpi_sysctl_ctx
,
166 SYSCTL_CHILDREN(acpi_sc
->acpi_sysctl_tree
),
167 OID_AUTO
, "acline", CTLTYPE_INT
| CTLFLAG_RD
,
168 &sc
->status
, 0, acpi_acad_sysctl
, "I", "");
171 /* Get initial status after whole system is up. */
173 sc
->initializing
= 0;
176 * Also install a system notify handler even though this is not
177 * required by the specification. The Casio FIVA needs this.
179 AcpiInstallNotifyHandler(handle
, ACPI_SYSTEM_NOTIFY
,
180 acpi_acad_notify_handler
, dev
);
181 AcpiInstallNotifyHandler(handle
, ACPI_DEVICE_NOTIFY
,
182 acpi_acad_notify_handler
, dev
);
183 AcpiOsExecute(OSL_NOTIFY_HANDLER
, acpi_acad_init_acline
, dev
);
189 acpi_acad_ioctl(u_long cmd
, caddr_t addr
, void *arg
)
191 struct acpi_acad_softc
*sc
;
195 sc
= device_get_softc(dev
);
200 * No security check required: information retrieval only. If
201 * new functions are added here, a check might be required.
204 case ACPIIO_ACAD_GET_STATUS
:
205 acpi_acad_get_status(dev
);
206 *(int *)addr
= sc
->status
;
216 acpi_acad_sysctl(SYSCTL_HANDLER_ARGS
)
220 if (acpi_acad_get_acline(&val
) != 0)
223 val
= *(u_int
*)oidp
->oid_arg1
;
224 error
= sysctl_handle_int(oidp
, &val
, 0, req
);
229 acpi_acad_init_acline(void *arg
)
231 struct acpi_acad_softc
*sc
;
236 sc
= device_get_softc(dev
);
237 if (sc
->initializing
)
240 sc
->initializing
= 1;
241 ACPI_VPRINT(dev
, acpi_device_get_parent_softc(dev
),
242 "acline initialization start\n");
245 for (retry
= 0; retry
< ACPI_ACAD_RETRY_MAX
; retry
++) {
246 acpi_acad_get_status(dev
);
247 if (status
!= sc
->status
)
252 sc
->initializing
= 0;
253 ACPI_VPRINT(dev
, acpi_device_get_parent_softc(dev
),
254 "acline initialization done, tried %d times\n", retry
+ 1);
261 acpi_acad_get_acline(int *status
)
263 struct acpi_acad_softc
*sc
;
266 dev
= devclass_get_device(acpi_acad_devclass
, 0);
269 sc
= device_get_softc(dev
);
273 acpi_acad_get_status(dev
);
274 *status
= sc
->status
;