Merge commit 'smtms/amd64'
[dragonfly.git] / sys / dev / acpica5 / acpi_acad.c
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: src/sys/dev/acpica/acpi_acad.c,v 1.27 2004/06/13 22:52:30 njl Exp $
27  * $DragonFly: src/sys/dev/acpica5/acpi_acad.c,v 1.8 2007/10/23 03:04:48 y0netan1 Exp $
28  */
29
30 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34 #include <sys/rman.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/conf.h>
38 #include <sys/power.h>
39
40 #include "acpi.h"
41 #include <dev/acpica5/acpivar.h>
42 #include <dev/acpica5/acpiio.h>
43  
44 /* Hooks for the ACPI CA debugging infrastructure */
45 #define _COMPONENT      ACPI_AC_ADAPTER
46 ACPI_MODULE_NAME("AC_ADAPTER")
47
48 /* Number of times to retry initialization before giving up. */
49 #define ACPI_ACAD_RETRY_MAX             6
50
51 #define ACPI_DEVICE_CHECK_PNP           0x00
52 #define ACPI_DEVICE_CHECK_EXISTENCE     0x01
53 #define ACPI_POWERSOURCE_STAT_CHANGE    0x80
54
55 struct  acpi_acad_softc {
56     int status;
57     int initializing;
58 };
59
60 static void     acpi_acad_get_status(void *);
61 static void     acpi_acad_notify_handler(ACPI_HANDLE, UINT32, void *);
62 static int      acpi_acad_probe(device_t);
63 static int      acpi_acad_attach(device_t);
64 static int      acpi_acad_ioctl(u_long, caddr_t, void *);
65 static int      acpi_acad_sysctl(SYSCTL_HANDLER_ARGS);
66 static void     acpi_acad_init_acline(void *arg);
67
68 static device_method_t acpi_acad_methods[] = {
69     /* Device interface */
70     DEVMETHOD(device_probe,     acpi_acad_probe),
71     DEVMETHOD(device_attach,    acpi_acad_attach),
72
73     {0, 0}
74 };
75
76 static driver_t acpi_acad_driver = {
77     "acpi_acad",
78     acpi_acad_methods,
79     sizeof(struct acpi_acad_softc),
80 };
81
82 static devclass_t acpi_acad_devclass;
83 DRIVER_MODULE(acpi_acad, acpi, acpi_acad_driver, acpi_acad_devclass, 0, 0);
84 MODULE_DEPEND(acpi_acad, acpi, 1, 1, 1);
85
86 static void
87 acpi_acad_get_status(void *context)
88 {
89     struct acpi_acad_softc *sc;
90     device_t    dev;
91     ACPI_HANDLE h;
92     int         newstatus;
93
94     dev = context;
95     sc = device_get_softc(dev);
96     h = acpi_get_handle(dev);
97     if (ACPI_FAILURE(acpi_GetInteger(h, "_PSR", &newstatus))) {
98         sc->status = -1;
99         return;
100     }
101
102     if (sc->status != newstatus) {
103         sc->status = newstatus;
104
105         /* Set system power profile based on AC adapter status */
106         power_profile_set_state(sc->status ? POWER_PROFILE_PERFORMANCE :
107                                 POWER_PROFILE_ECONOMY);
108         ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
109                     "%s Line\n", sc->status ? "On" : "Off");
110
111         acpi_UserNotify("ACAD", h, sc->status);
112     }
113 }
114
115 static void
116 acpi_acad_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
117 {
118     device_t dev;
119
120     dev = (device_t)context;
121     switch (notify) {
122     case ACPI_DEVICE_CHECK_PNP:
123     case ACPI_DEVICE_CHECK_EXISTENCE:
124     case ACPI_POWERSOURCE_STAT_CHANGE:
125         /* Temporarily.  It is better to notify policy manager */
126         AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_acad_get_status, context);
127         break;
128     default:
129         device_printf(dev, "unknown notify %#x\n", notify);
130         break;
131     }
132 }
133
134 static int
135 acpi_acad_probe(device_t dev)
136 {
137     if (acpi_get_type(dev) == ACPI_TYPE_DEVICE && !acpi_disabled("acad") &&
138         acpi_MatchHid(acpi_get_handle(dev), "ACPI0003")) {
139         device_set_desc(dev, "AC Adapter");
140         return (0);
141     }
142     return (ENXIO);
143 }
144
145 static int
146 acpi_acad_attach(device_t dev)
147 {
148     struct acpi_acad_softc *sc;
149     struct acpi_softc      *acpi_sc;
150     ACPI_HANDLE handle;
151     int         error;
152
153     sc = device_get_softc(dev);
154     if (sc == NULL)
155         return (ENXIO);
156     handle = acpi_get_handle(dev);
157
158     error = acpi_register_ioctl(ACPIIO_ACAD_GET_STATUS, acpi_acad_ioctl, dev);
159     if (error != 0)
160         return (error);
161
162     if (device_get_unit(dev) == 0) {
163         acpi_sc = acpi_device_get_parent_softc(dev);
164         SYSCTL_ADD_PROC(&acpi_sc->acpi_sysctl_ctx,
165                         SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
166                         OID_AUTO, "acline", CTLTYPE_INT | CTLFLAG_RD,
167                         &sc->status, 0, acpi_acad_sysctl, "I", "");
168     }
169
170     /* Get initial status after whole system is up. */
171     sc->status = -1;
172     sc->initializing = 0;
173
174     /*
175      * Also install a system notify handler even though this is not
176      * required by the specification.  The Casio FIVA needs this.
177      */
178     AcpiInstallNotifyHandler(handle, ACPI_SYSTEM_NOTIFY,
179                              acpi_acad_notify_handler, dev);
180     AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
181                              acpi_acad_notify_handler, dev);
182     AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_acad_init_acline, dev);
183
184     return (0);
185 }
186
187 static int
188 acpi_acad_ioctl(u_long cmd, caddr_t addr, void *arg)
189 {
190     struct acpi_acad_softc *sc;
191     device_t dev;
192
193     dev = (device_t)arg;
194     sc = device_get_softc(dev);
195     if (sc == NULL)
196         return (ENXIO);
197
198     /*
199      * No security check required: information retrieval only.  If
200      * new functions are added here, a check might be required.
201      */
202     switch (cmd) {
203     case ACPIIO_ACAD_GET_STATUS:
204         acpi_acad_get_status(dev);
205         *(int *)addr = sc->status;
206         break;
207     default:
208         break;
209     }
210
211     return (0);
212 }
213
214 static int
215 acpi_acad_sysctl(SYSCTL_HANDLER_ARGS)
216 {
217     int val, error;
218
219     if (acpi_acad_get_acline(&val) != 0)
220         return (ENXIO);
221
222     val = *(u_int *)oidp->oid_arg1;
223     error = sysctl_handle_int(oidp, &val, 0, req);
224     return (error);
225 }
226
227 static void
228 acpi_acad_init_acline(void *arg)
229 {
230     struct acpi_acad_softc *sc;
231     device_t    dev;
232     int         retry, status;
233
234     dev = (device_t)arg;
235     sc = device_get_softc(dev);
236     if (sc->initializing)
237         return;
238
239     sc->initializing = 1;
240     ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
241                 "acline initialization start\n");
242
243     status = 0;
244     for (retry = 0; retry < ACPI_ACAD_RETRY_MAX; retry++) {
245         acpi_acad_get_status(dev);
246         if (status != sc->status)
247             break;
248         AcpiOsSleep(10);
249     }
250
251     sc->initializing = 0;
252     ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
253                 "acline initialization done, tried %d times\n", retry + 1);
254 }
255
256 /*
257  * Public interfaces.
258  */
259 int
260 acpi_acad_get_acline(int *status)
261 {
262     struct acpi_acad_softc *sc;
263     device_t dev;
264
265     dev = devclass_get_device(acpi_acad_devclass, 0);
266     if (dev == NULL)
267         return (ENXIO);
268     sc = device_get_softc(dev);
269     if (sc == NULL)
270         return (ENXIO);
271
272     acpi_acad_get_status(dev);
273     *status = sc->status;
274
275     return (0);
276 }