acpi_acad.c revision 131282
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: head/sys/dev/acpica/acpi_acad.c 131282 2004-06-29 19:02:27Z njl $
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 <machine/bus.h>
35#include <sys/rman.h>
36#include <sys/ioccom.h>
37#include <sys/malloc.h>
38#include <sys/module.h>
39#include <sys/conf.h>
40#include <sys/power.h>
41
42#include "acpi.h"
43#include <dev/acpica/acpivar.h>
44#include <dev/acpica/acpiio.h>
45#include <isa/isavar.h>
46#include <isa/pnpvar.h>
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
55#define ACPI_DEVICE_CHECK_PNP		0x00
56#define ACPI_DEVICE_CHECK_EXISTENCE	0x01
57#define ACPI_POWERSOURCE_STAT_CHANGE	0x80
58
59struct	acpi_acad_softc {
60    int status;
61    int initializing;
62};
63
64static void	acpi_acad_get_status(void *);
65static void	acpi_acad_notify_handler(ACPI_HANDLE, UINT32, void *);
66static int	acpi_acad_probe(device_t);
67static int	acpi_acad_attach(device_t);
68static int	acpi_acad_ioctl(u_long, caddr_t, void *);
69static int	acpi_acad_sysctl(SYSCTL_HANDLER_ARGS);
70static void	acpi_acad_init_acline(void *arg);
71
72static device_method_t acpi_acad_methods[] = {
73    /* Device interface */
74    DEVMETHOD(device_probe,	acpi_acad_probe),
75    DEVMETHOD(device_attach,	acpi_acad_attach),
76
77    {0, 0}
78};
79
80static driver_t acpi_acad_driver = {
81    "acpi_acad",
82    acpi_acad_methods,
83    sizeof(struct acpi_acad_softc),
84};
85
86static devclass_t acpi_acad_devclass;
87DRIVER_MODULE(acpi_acad, acpi, acpi_acad_driver, acpi_acad_devclass, 0, 0);
88MODULE_DEPEND(acpi_acad, acpi, 1, 1, 1);
89
90static void
91acpi_acad_get_status(void *context)
92{
93    struct acpi_acad_softc *sc;
94    device_t	dev;
95    ACPI_HANDLE	h;
96    int		newstatus;
97
98    dev = context;
99    sc = device_get_softc(dev);
100    h = acpi_get_handle(dev);
101    if (ACPI_FAILURE(acpi_GetInteger(h, "_PSR", &newstatus))) {
102	sc->status = -1;
103	return;
104    }
105
106    if (sc->status != newstatus) {
107	sc->status = newstatus;
108
109	/* Set system power profile based on AC adapter status */
110	power_profile_set_state(sc->status ? POWER_PROFILE_PERFORMANCE :
111				POWER_PROFILE_ECONOMY);
112	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
113		    "%s Line\n", sc->status ? "On" : "Off");
114
115	acpi_UserNotify("ACAD", h, sc->status);
116    }
117}
118
119static void
120acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
121{
122    device_t dev;
123
124    dev = (device_t)context;
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	device_printf(dev, "unknown notify %#x\n", notify);
134	break;
135    }
136}
137
138static int
139acpi_acad_probe(device_t dev)
140{
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);
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);
160    if (sc == NULL)
161	return (ENXIO);
162    handle = acpi_get_handle(dev);
163
164    error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS, acpi_acad_ioctl, dev);
165    if (error != 0)
166	return (error);
167
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;
178    sc->initializing = 0;
179
180    /*
181     * Also install a system notify handler even though this is not
182     * required by the specification.  The Casio FIVA needs this.
183     */
184    AcpiInstallNotifyHandler(handle, ACPI_SYSTEM_NOTIFY,
185			     acpi_acad_notify_handler, dev);
186    AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
187			     acpi_acad_notify_handler, dev);
188    AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_init_acline, dev);
189
190    return (0);
191}
192
193static int
194acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg)
195{
196    struct acpi_acad_softc *sc;
197    device_t dev;
198
199    dev = (device_t)arg;
200    sc = device_get_softc(dev);
201    if (sc == NULL)
202	return (ENXIO);
203
204    /*
205     * No security check required: information retrieval only.  If
206     * new functions are added here, a check might be required.
207     */
208    switch (cmd) {
209    case ACPIIO_ACAD_GET_STATUS:
210	acpi_acad_get_status(dev);
211	*(int *)addr = sc->status;
212	break;
213    default:
214	break;
215    }
216
217    return (0);
218}
219
220static int
221acpi_acad_sysctl(SYSCTL_HANDLER_ARGS)
222{
223    int val, error;
224
225    if (acpi_acad_get_acline(&val) != 0)
226	return (ENXIO);
227
228    val = *(u_int *)oidp->oid_arg1;
229    error = sysctl_handle_int(oidp, &val, 0, req);
230    return (error);
231}
232
233static void
234acpi_acad_init_acline(void *arg)
235{
236    struct acpi_acad_softc *sc;
237    device_t	dev;
238    int		retry, status;
239
240    dev = (device_t)arg;
241    sc = device_get_softc(dev);
242    if (sc->initializing)
243	return;
244
245    sc->initializing = 1;
246    ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
247		"acline initialization start\n");
248
249    status = 0;
250    for (retry = 0; retry < ACPI_ACAD_RETRY_MAX; retry++) {
251	acpi_acad_get_status(dev);
252	if (status != sc->status)
253	    break;
254	AcpiOsSleep(10, 0);
255    }
256
257    sc->initializing = 0;
258    ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
259		"acline initialization done, tried %d times\n", retry + 1);
260}
261
262/*
263 * Public interfaces.
264 */
265int
266acpi_acad_get_acline(int *status)
267{
268    struct acpi_acad_softc *sc;
269    device_t dev;
270
271    dev = devclass_get_device(acpi_acad_devclass, 0);
272    if (dev == NULL)
273	return (ENXIO);
274    sc = device_get_softc(dev);
275    if (sc == NULL)
276	return (ENXIO);
277
278    acpi_acad_get_status(dev);
279    *status = sc->status;
280
281    return (0);
282}
283