Device layer rollup commit.
[dragonfly.git] / sys / dev / acpica / acpi_battery.c
1 /*-
2  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
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_battery.c,v 1.8.2.1 2003/08/22 20:49:20 jhb Exp $
27  *      $DragonFly: src/sys/dev/acpica/Attic/acpi_battery.c,v 1.2 2004/05/05 22:19:22 dillon Exp $ 
28  */
29
30 #include "opt_acpi.h"           /* XXX trim includes */
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/proc.h>
34 #include <sys/malloc.h>
35 #include <sys/bus.h>
36 #include <sys/conf.h>
37 #include <sys/ioccom.h>
38 #include <sys/reboot.h>
39 #include <sys/sysctl.h>
40 #include <sys/ctype.h>
41
42 #include <machine/clock.h>
43
44 #include <machine/resource.h>
45
46 #include "acpi.h"
47
48 #include <dev/acpica/acpivar.h>
49 #include <dev/acpica/acpiio.h>
50
51 MALLOC_DEFINE(M_ACPIBATT, "acpibatt", "ACPI generic battery data");
52
53 /*
54  * ACPI Battery Abstruction Layer.
55  */
56
57 struct acpi_batteries {
58         TAILQ_ENTRY(acpi_batteries) link;
59         struct   acpi_battdesc battdesc;
60 };
61
62 static TAILQ_HEAD(,acpi_batteries) acpi_batteries;
63 static int                      acpi_batteries_initted = 0;
64 static int                      acpi_batteries_units = 0;
65 static int                      acpi_battery_info_expire = 5;
66 static struct acpi_battinfo     acpi_battery_battinfo;
67
68 int
69 acpi_battery_get_units(void)
70 {
71
72         return (acpi_batteries_units);
73 }
74
75 int
76 acpi_battery_get_battdesc(int logical_unit, struct acpi_battdesc *battdesc)
77 {
78         int      i;
79         struct acpi_batteries   *bp;
80
81         if (logical_unit < 0 || logical_unit >= acpi_batteries_units) {
82                 return (ENXIO);
83         }
84
85         i = 0;
86         TAILQ_FOREACH(bp, &acpi_batteries, link) {
87                 if (logical_unit == i) {
88                         battdesc->type = bp->battdesc.type;
89                         battdesc->phys_unit = bp->battdesc.phys_unit;
90                         return (0);
91                 }
92                 i++;
93         }
94
95         return (ENXIO);
96 }
97
98 int
99 acpi_battery_get_battinfo(int unit, struct acpi_battinfo *battinfo)
100 {
101         int      error;
102         struct   acpi_battdesc battdesc;
103
104         error = 0;
105         if (unit == -1) {
106                 error = acpi_cmbat_get_battinfo(-1, battinfo);
107                 goto out;
108         } else {
109                 if ((error = acpi_battery_get_battdesc(unit, &battdesc)) != 0) {
110                         goto out;
111                 }
112                 switch (battdesc.type) {
113                 case ACPI_BATT_TYPE_CMBAT:
114                         error = acpi_cmbat_get_battinfo(battdesc.phys_unit,
115                            battinfo);
116                         break;
117                 default:
118                         error = ENXIO;
119                         break;
120                 }
121         }
122 out:
123         return (error);
124 }
125
126 int
127 acpi_battery_get_info_expire(void)
128 {
129
130         return (acpi_battery_info_expire);
131 }
132
133 static int
134 acpi_battery_ioctl(u_long cmd, caddr_t addr, void *arg)
135 {
136         int      error;
137         int      logical_unit;
138         union acpi_battery_ioctl_arg    *ioctl_arg;
139
140         ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
141         error = 0;
142
143         /*
144          * No security check required: information retrieval only.  If
145          * new functions are added here, a check might be required.
146          */
147
148         switch (cmd) {
149         case ACPIIO_BATT_GET_UNITS:
150                 *(int *)addr = acpi_battery_get_units();
151                 break;
152
153         case ACPIIO_BATT_GET_BATTDESC:
154                 logical_unit = ioctl_arg->unit;
155                 error = acpi_battery_get_battdesc(logical_unit, &ioctl_arg->battdesc);
156                 break;
157
158         case ACPIIO_BATT_GET_BATTINFO:
159                 logical_unit = ioctl_arg->unit;
160                 error = acpi_battery_get_battinfo(logical_unit,
161                     &ioctl_arg->battinfo);
162                 break;
163
164         default:
165                 error = EINVAL;
166                 break;
167         }
168
169         return (error);
170 }
171
172 static int
173 acpi_battery_sysctl(SYSCTL_HANDLER_ARGS)
174 {
175         int     val;
176         int     error;
177
178         acpi_battery_get_battinfo(-1, &acpi_battery_battinfo);
179         val = *(u_int *)oidp->oid_arg1;
180         error = sysctl_handle_int(oidp, &val, 0, req);
181         return (error);
182 }
183
184 static int
185 acpi_battery_init(void)
186 {
187         device_t                 dev;
188         struct acpi_softc       *sc;
189         int                      error;
190
191         if ((dev = devclass_get_device(devclass_find("acpi"), 0)) == NULL) {
192                 return (ENXIO);
193         }
194         if ((sc = device_get_softc(dev)) == NULL) {
195                 return (ENXIO);
196         }
197
198         error = 0;
199
200         TAILQ_INIT(&acpi_batteries);
201         acpi_batteries_initted = 1;
202
203         if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_UNITS,
204                         acpi_battery_ioctl, NULL)) != 0) {
205                 return (error);
206         }
207         if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTDESC,
208                         acpi_battery_ioctl, NULL)) != 0) {
209                 return (error);
210         }
211         if ((error = acpi_register_ioctl(ACPIIO_BATT_GET_BATTINFO,
212                         acpi_battery_ioctl, NULL)) != 0) {
213                 return (error);
214         }
215
216         sysctl_ctx_init(&sc->acpi_battery_sysctl_ctx);
217         sc->acpi_battery_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_battery_sysctl_ctx,
218                                 SYSCTL_CHILDREN(sc->acpi_sysctl_tree),
219                                 OID_AUTO, "battery", CTLFLAG_RD, 0, "");
220         SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx,
221                 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
222                 OID_AUTO, "life", CTLTYPE_INT | CTLFLAG_RD,
223                 &acpi_battery_battinfo.cap, 0, acpi_battery_sysctl, "I", "");
224         SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx,
225                 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
226                 OID_AUTO, "time", CTLTYPE_INT | CTLFLAG_RD,
227                 &acpi_battery_battinfo.min, 0, acpi_battery_sysctl, "I", "");
228         SYSCTL_ADD_PROC(&sc->acpi_battery_sysctl_ctx,
229                 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
230                 OID_AUTO, "state", CTLTYPE_INT | CTLFLAG_RD,
231                 &acpi_battery_battinfo.state, 0, acpi_battery_sysctl, "I", "");
232         SYSCTL_ADD_INT(&sc->acpi_battery_sysctl_ctx,
233                 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
234                 OID_AUTO, "units", CTLFLAG_RD, &acpi_batteries_units, 0, "");
235         SYSCTL_ADD_INT(&sc->acpi_battery_sysctl_ctx,
236                 SYSCTL_CHILDREN(sc->acpi_battery_sysctl_tree),
237                 OID_AUTO, "info_expire", CTLFLAG_RD | CTLFLAG_RW,
238                 &acpi_battery_info_expire, 0, "");
239
240         return (error);
241 }
242
243 int
244 acpi_battery_register(int type, int phys_unit)
245 {
246         int     error;
247         struct acpi_batteries   *bp;
248
249         error = 0;
250         bp = malloc(sizeof(*bp), M_ACPIBATT, M_INTWAIT);
251         bp->battdesc.type = type;
252         bp->battdesc.phys_unit = phys_unit;
253         if (acpi_batteries_initted == 0) {
254                 if ((error = acpi_battery_init()) != 0) {
255                         free(bp, M_ACPIBATT);
256                         return(error);
257                 }
258         }
259                 
260         TAILQ_INSERT_TAIL(&acpi_batteries, bp, link);
261         acpi_batteries_units++;
262
263         return(0);
264 }