acpi_acad.c revision 148318
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
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/acpica/acpi_acad.c 148318 2005-07-22 23:10:02Z njl $");
29
30#include "opt_acpi.h"
31#include <sys/param.h>
32#include <sys/kernel.h>
33#include <sys/bus.h>
34
35#include <machine/bus.h>
36#include <sys/rman.h>
37#include <sys/ioccom.h>
38#include <sys/malloc.h>
39#include <sys/module.h>
40#include <sys/conf.h>
41#include <sys/power.h>
42
43#include "acpi.h"
44#include <dev/acpica/acpivar.h>
45#include <dev/acpica/acpiio.h>
46#include <isa/isavar.h>
47#include <isa/pnpvar.h>
48
49/* Hooks for the ACPI CA debugging infrastructure */
50#define _COMPONENT	ACPI_AC_ADAPTER
51ACPI_MODULE_NAME("AC_ADAPTER")
52
53/* Number of times to retry initialization before giving up. */
54#define ACPI_ACAD_RETRY_MAX		6
55
56#define ACPI_POWERSOURCE_STAT_CHANGE	0x80
57
58struct	acpi_acad_softc {
59    int status;
60};
61
62static void	acpi_acad_get_status(void *);
63static void	acpi_acad_notify_handler(ACPI_HANDLE, UINT32, void *);
64static int	acpi_acad_probe(device_t);
65static int	acpi_acad_attach(device_t);
66static int	acpi_acad_ioctl(u_long, caddr_t, void *);
67static int	acpi_acad_sysctl(SYSCTL_HANDLER_ARGS);
68static void	acpi_acad_init_acline(void *arg);
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);
86MODULE_DEPEND(acpi_acad, acpi, 1, 1, 1);
87
88ACPI_SERIAL_DECL(acad, "ACPI AC adapter");
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    newstatus = -1;
102    acpi_GetInteger(h, "_PSR", &newstatus);
103
104    /* If status is valid and has changed, notify the system. */
105    ACPI_SERIAL_BEGIN(acad);
106    if (newstatus != -1 && sc->status != newstatus) {
107	sc->status = newstatus;
108	power_profile_set_state(newstatus ? POWER_PROFILE_PERFORMANCE :
109	    POWER_PROFILE_ECONOMY);
110	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
111	    "%s Line\n", newstatus ? "On" : "Off");
112	acpi_UserNotify("ACAD", h, newstatus);
113    }
114    ACPI_SERIAL_END(acad);
115}
116
117static void
118acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
119{
120    device_t dev;
121
122    dev = (device_t)context;
123    switch (notify) {
124    case ACPI_NOTIFY_BUS_CHECK:
125    case ACPI_NOTIFY_DEVICE_CHECK:
126    case ACPI_POWERSOURCE_STAT_CHANGE:
127	/* Temporarily.  It is better to notify policy manager */
128	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_get_status, context);
129	break;
130    default:
131	device_printf(dev, "unknown notify %#x\n", notify);
132	break;
133    }
134}
135
136static int
137acpi_acad_probe(device_t dev)
138{
139    static char *acad_ids[] = { "ACPI0003", NULL };
140
141    if (acpi_disabled("acad") ||
142	ACPI_ID_PROBE(device_get_parent(dev), dev, acad_ids) == NULL)
143	return (ENXIO);
144
145    device_set_desc(dev, "AC Adapter");
146    return (0);
147}
148
149static int
150acpi_acad_attach(device_t dev)
151{
152    struct acpi_acad_softc *sc;
153    struct acpi_softc	   *acpi_sc;
154    ACPI_HANDLE	handle;
155    int		error;
156
157    sc = device_get_softc(dev);
158    handle = acpi_get_handle(dev);
159
160    error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS, acpi_acad_ioctl, dev);
161    if (error != 0)
162	return (error);
163
164    if (device_get_unit(dev) == 0) {
165	acpi_sc = acpi_device_get_parent_softc(dev);
166	SYSCTL_ADD_PROC(&acpi_sc->acpi_sysctl_ctx,
167			SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
168			OID_AUTO, "acline", CTLTYPE_INT | CTLFLAG_RD,
169			&sc->status, 0, acpi_acad_sysctl, "I", "");
170    }
171
172    /* Get initial status after whole system is up. */
173    sc->status = -1;
174
175    /*
176     * Install both system and device notify handlers since the Casio
177     * FIVA needs them.
178     */
179    AcpiInstallNotifyHandler(handle, ACPI_ALL_NOTIFY,
180			     acpi_acad_notify_handler, dev);
181    AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpi_acad_init_acline, dev);
182
183    return (0);
184}
185
186static int
187acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg)
188{
189    struct acpi_acad_softc *sc;
190    device_t dev;
191
192    dev = (device_t)arg;
193    sc = device_get_softc(dev);
194
195    /*
196     * No security check required: information retrieval only.  If
197     * new functions are added here, a check might be required.
198     */
199    switch (cmd) {
200    case ACPIIO_ACAD_GET_STATUS:
201	acpi_acad_get_status(dev);
202	*(int *)addr = sc->status;
203	break;
204    default:
205	break;
206    }
207
208    return (0);
209}
210
211static int
212acpi_acad_sysctl(SYSCTL_HANDLER_ARGS)
213{
214    int val, error;
215
216    if (acpi_acad_get_acline(&val) != 0)
217	return (ENXIO);
218
219    val = *(u_int *)oidp->oid_arg1;
220    error = sysctl_handle_int(oidp, &val, 0, req);
221    return (error);
222}
223
224static void
225acpi_acad_init_acline(void *arg)
226{
227    struct acpi_acad_softc *sc;
228    device_t	dev;
229    int		retry;
230
231    dev = (device_t)arg;
232    sc = device_get_softc(dev);
233    ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
234		"acline initialization start\n");
235
236    for (retry = 0; retry < ACPI_ACAD_RETRY_MAX; retry++) {
237	acpi_acad_get_status(dev);
238	if (sc->status != -1)
239	    break;
240	AcpiOsSleep(10000);
241    }
242
243    ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
244		"acline initialization done, tried %d times\n", retry + 1);
245}
246
247/*
248 * Public interfaces.
249 */
250int
251acpi_acad_get_acline(int *status)
252{
253    struct acpi_acad_softc *sc;
254    device_t dev;
255
256    dev = devclass_get_device(acpi_acad_devclass, 0);
257    if (dev == NULL)
258	return (ENXIO);
259    sc = device_get_softc(dev);
260
261    acpi_acad_get_status(dev);
262    *status = sc->status;
263
264    return (0);
265}
266