]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/acpica/acpi_cmbat.c
This commit was generated by cvs2svn to compensate for changes in r82367,
[FreeBSD/FreeBSD.git] / sys / dev / acpica / acpi_cmbat.c
1 /*-
2  * Copyright (c) 2000 Munehiro Matsuda
3  * Copyright (c) 2000 Takanori Watanabe
4  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #include "opt_acpi.h"
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/ioccom.h>
36 #include <sys/conf.h>
37
38 #include <machine/bus.h>
39 #include <machine/resource.h>
40 #include <sys/rman.h>
41 #include <sys/malloc.h>
42
43 #include  "acpi.h"
44
45 #include <dev/acpica/acpivar.h>
46 #include <dev/acpica/acpiio.h>
47
48 /*
49  * Hooks for the ACPI CA debugging infrastructure
50  */
51 #define _COMPONENT      ACPI_BATTERY
52 MODULE_NAME("BATTERY")
53
54 static void      acpi_cmbat_get_bst(void *);
55 static void      acpi_cmbat_get_bif(void *);
56 static void      acpi_cmbat_notify_handler(ACPI_HANDLE, UINT32, void *);
57 static int       acpi_cmbat_probe(device_t);
58 static int       acpi_cmbat_attach(device_t);
59 static int       acpi_cmbat_ioctl(u_long, caddr_t, void *);
60
61 struct acpi_cmbat_softc {
62         device_t        dev;
63         struct acpi_bif bif;
64         struct acpi_bst bst;
65         ACPI_BUFFER     bif_buffer;
66         ACPI_BUFFER     bst_buffer;
67         struct timespec bif_lastupdated;
68         struct timespec bst_lastupdated;
69
70         int             not_present;
71         int             cap;
72         int             min;
73         int             full_charge_time;
74 };
75
76 static struct timespec  acpi_cmbat_info_lastupdated;
77
78 /* XXX: devclass_get_maxunit() don't give us the current allocated units... */
79 static int acpi_cmbat_units = 0;
80
81 #define ACPI_BATTERY_BST_CHANGE 0x80
82 #define ACPI_BATTERY_BIF_CHANGE 0x81
83
84 #define PKG_GETINT(res, tmp, idx, dest, label) do {                     \
85         tmp = &res->Package.Elements[idx];                              \
86         if (tmp == NULL) {                                              \
87                 device_printf(dev, "%s: PKG_GETINT idx = %d\n.",        \
88                     __func__, idx);                                     \
89                 goto label;                                             \
90         }                                                               \
91         if (tmp->Type != ACPI_TYPE_INTEGER)                             \
92                 goto label;                                             \
93         dest = tmp->Integer.Value;                                      \
94 } while(0)
95
96 #define PKG_GETSTR(res, tmp, idx, dest, size, label) do {               \
97         size_t  length;                                                 \
98         length = size;                                                  \
99         tmp = &res->Package.Elements[idx];                              \
100         if (tmp == NULL) {                                              \
101                 device_printf(dev, "%s: PKG_GETSTR idx = %d\n.",        \
102                     __func__, idx);                                     \
103                 goto label;                                             \
104         }                                                               \
105         bzero(dest, sizeof(dest));                                      \
106         switch (tmp->Type) {                                            \
107         case ACPI_TYPE_STRING:                                          \
108                 if (tmp->String.Length < length) {                      \
109                         length = tmp->String.Length;                    \
110                 }                                                       \
111                 strncpy(dest, tmp->String.Pointer, length);             \
112                 break;                                                  \
113         case ACPI_TYPE_BUFFER:                                          \
114                 if (tmp->Buffer.Length < length) {                      \
115                         length = tmp->Buffer.Length;                    \
116                 }                                                       \
117                 strncpy(dest, tmp->Buffer.Pointer, length);             \
118                 break;                                                  \
119         default:                                                        \
120                 goto label;                                             \
121         }                                                               \
122         dest[sizeof(dest)-1] = '\0';                                    \
123 } while(0)
124
125 #define BATTERY_INFO_EXPIRE     5
126 static __inline int
127 acpi_cmbat_info_expired(struct timespec *lastupdated)
128 {
129         struct timespec curtime;
130
131         if (lastupdated == NULL) {
132                 return (1);
133         }
134
135         if (!timespecisset(lastupdated)) {
136                 return (1);
137         }
138
139         getnanotime(&curtime);
140         timespecsub(&curtime, lastupdated);
141         return ((curtime.tv_sec < 0 || curtime.tv_sec > BATTERY_INFO_EXPIRE));
142 }
143
144
145 static __inline void
146 acpi_cmbat_info_updated(struct timespec *lastupdated)
147 {
148
149         if (lastupdated != NULL) {
150                 getnanotime(lastupdated);
151         }
152 }
153
154 static void
155 acpi_cmbat_get_bst(void *context)
156 {
157         device_t        dev = context;
158         struct acpi_cmbat_softc *sc = device_get_softc(dev);
159         ACPI_STATUS     as;
160         ACPI_OBJECT     *res, *tmp;
161         ACPI_HANDLE     h = acpi_get_handle(dev);
162
163         if (!acpi_cmbat_info_expired(&sc->bst_lastupdated)) {
164                 return;
165         }
166
167 retry:
168         if (sc->bst_buffer.Length == 0) {
169                 sc->bst_buffer.Pointer = NULL;
170                 as = AcpiEvaluateObject(h, "_BST", NULL, &sc->bst_buffer);
171                 if (as != AE_BUFFER_OVERFLOW){
172                         device_printf(dev, "CANNOT FOUND _BST (%d)\n", as);
173                         return;
174                 }
175
176                 sc->bst_buffer.Pointer = malloc(sc->bst_buffer.Length, M_DEVBUF, M_NOWAIT);
177                 if (sc->bst_buffer.Pointer == NULL) {
178                         device_printf(dev,"malloc failed");
179                         return;
180                 }
181         }
182
183         bzero(sc->bst_buffer.Pointer, sc->bst_buffer.Length);
184         as = AcpiEvaluateObject(h, "_BST", NULL, &sc->bst_buffer);
185
186         if (as == AE_BUFFER_OVERFLOW){
187                 if (sc->bst_buffer.Pointer){
188                         free(sc->bst_buffer.Pointer, M_DEVBUF);
189                 }
190                 device_printf(dev, "bst size changed to %d\n", sc->bst_buffer.Length);
191                 sc->bst_buffer.Length = 0;
192                 goto retry;
193         } else if (as != AE_OK){
194                 device_printf(dev, "CANNOT FOUND _BST (%d)\n", as);
195                 return;
196         }
197
198         res = (ACPI_OBJECT *)sc->bst_buffer.Pointer;
199
200         if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 4)) {
201                 device_printf(dev, "Battery status corrupted\n");
202                 return;
203         }
204
205         PKG_GETINT(res, tmp, 0, sc->bst.state, end);
206         PKG_GETINT(res, tmp, 1, sc->bst.rate, end);
207         PKG_GETINT(res, tmp, 2, sc->bst.cap, end);
208         PKG_GETINT(res, tmp, 3, sc->bst.volt, end);
209         acpi_cmbat_info_updated(&sc->bst_lastupdated);
210 end:
211 }
212
213 static void
214 acpi_cmbat_get_bif(void *context)
215 {
216         device_t        dev = context;
217         struct acpi_cmbat_softc *sc = device_get_softc(dev);
218         ACPI_STATUS     as;
219         ACPI_HANDLE     h = acpi_get_handle(dev);
220         ACPI_OBJECT     *res, *tmp;
221
222         if (!acpi_cmbat_info_expired(&sc->bif_lastupdated)) {
223                 return;
224         }
225
226 retry:
227         if (sc->bif_buffer.Length == 0) {
228                 sc->bif_buffer.Pointer = NULL;
229                 as = AcpiEvaluateObject(h, "_BIF", NULL, &sc->bif_buffer);
230                 if (as != AE_BUFFER_OVERFLOW){
231                         device_printf(dev, "CANNOT FOUND _BIF (%d)\n", as);
232                         return;
233                 }
234
235                 sc->bif_buffer.Pointer = malloc(sc->bif_buffer.Length, M_DEVBUF, M_NOWAIT);
236                 if (sc->bif_buffer.Pointer == NULL) {
237                         device_printf(dev,"malloc failed");
238                         return;
239                 }
240         }
241
242         bzero(sc->bif_buffer.Pointer, sc->bif_buffer.Length);
243         as = AcpiEvaluateObject(h, "_BIF", NULL, &sc->bif_buffer);
244
245         if (as == AE_BUFFER_OVERFLOW){
246                 if (sc->bif_buffer.Pointer){
247                         free(sc->bif_buffer.Pointer, M_DEVBUF);
248                 }
249                 device_printf(dev, "bif size changed to %d\n", sc->bif_buffer.Length);
250                 sc->bif_buffer.Length = 0;
251                 goto retry;
252         } else if (as != AE_OK){
253                 device_printf(dev, "CANNOT FOUND _BIF (%d)\n", as);
254                 return;
255         }
256
257         res = (ACPI_OBJECT *)sc->bif_buffer.Pointer;
258
259         if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count != 13)) {
260                 device_printf(dev, "Battery info corrupted\n");
261                 return;
262         }
263
264         PKG_GETINT(res, tmp,  0, sc->bif.unit, end);
265         PKG_GETINT(res, tmp,  1, sc->bif.dcap, end);
266         PKG_GETINT(res, tmp,  2, sc->bif.lfcap, end);
267         PKG_GETINT(res, tmp,  3, sc->bif.btech, end);
268         PKG_GETINT(res, tmp,  4, sc->bif.dvol, end);
269         PKG_GETINT(res, tmp,  5, sc->bif.wcap, end);
270         PKG_GETINT(res, tmp,  6, sc->bif.lcap, end);
271         PKG_GETINT(res, tmp,  7, sc->bif.gra1, end);
272         PKG_GETINT(res, tmp,  8, sc->bif.gra2, end);
273         PKG_GETSTR(res, tmp,  9, sc->bif.model, ACPI_CMBAT_MAXSTRLEN, end);
274         PKG_GETSTR(res, tmp, 10, sc->bif.serial, ACPI_CMBAT_MAXSTRLEN, end);
275         PKG_GETSTR(res, tmp, 11, sc->bif.type, ACPI_CMBAT_MAXSTRLEN, end);
276         PKG_GETSTR(res, tmp, 12, sc->bif.oeminfo, ACPI_CMBAT_MAXSTRLEN, end);
277         acpi_cmbat_info_updated(&sc->bif_lastupdated);
278 end:
279 }
280
281 static void
282 acpi_cmbat_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
283 {
284         device_t        dev;
285         struct acpi_cmbat_softc *sc;
286
287         dev = (device_t)context;
288         if ((sc = device_get_softc(dev)) == NULL) {
289                 return;
290         }
291
292         switch (notify) {
293         case ACPI_BATTERY_BST_CHANGE:
294                 timespecclear(&sc->bst_lastupdated);
295                 break;
296         case ACPI_BATTERY_BIF_CHANGE:
297                 timespecclear(&sc->bif_lastupdated);
298                 break;
299         default:
300                 break;
301         }
302 }
303
304 static int
305 acpi_cmbat_probe(device_t dev)
306 {
307
308         if ((acpi_get_type(dev) == ACPI_TYPE_DEVICE) &&
309             acpi_MatchHid(dev, "PNP0C0A")) {
310                 /*
311                  * Set device description 
312                  */
313                 device_set_desc(dev, "Control method Battery");
314                 return(0);
315         }
316         return(ENXIO);
317 }
318   
319 static int
320 acpi_cmbat_attach(device_t dev)
321 {
322         int      error;
323         ACPI_HANDLE handle = acpi_get_handle(dev);
324         struct acpi_cmbat_softc *sc;
325
326         if ((sc = device_get_softc(dev)) == NULL) {
327                 return (ENXIO);
328         }
329         AcpiInstallNotifyHandler(handle, ACPI_DEVICE_NOTIFY,
330                                  acpi_cmbat_notify_handler, dev);
331         bzero(&sc->bif_buffer, sizeof(sc->bif_buffer));
332         bzero(&sc->bst_buffer, sizeof(sc->bst_buffer));
333         sc->dev = dev;
334
335         timespecclear(&sc->bif_lastupdated);
336         timespecclear(&sc->bst_lastupdated);
337
338         if (acpi_cmbat_units == 0) {
339                 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BIF,
340                                 acpi_cmbat_ioctl, NULL)) != 0) {
341                         return (error);
342                 }
343                 if ((error = acpi_register_ioctl(ACPIIO_CMBAT_GET_BST,
344                                 acpi_cmbat_ioctl, NULL)) != 0) {
345                         return (error);
346                 }
347         }
348
349         if ((error = acpi_battery_register(ACPI_BATT_TYPE_CMBAT,
350                         acpi_cmbat_units)) != 0) {
351                 return (error);
352         }
353
354         acpi_cmbat_units++;
355         timespecclear(&acpi_cmbat_info_lastupdated);
356
357
358         return(0);
359 }
360
361 static device_method_t acpi_cmbat_methods[] = {
362         /* Device interface */
363         DEVMETHOD(device_probe,         acpi_cmbat_probe),
364         DEVMETHOD(device_attach,        acpi_cmbat_attach),
365
366         {0, 0}
367 };
368
369 static driver_t acpi_cmbat_driver = {
370         "acpi_cmbat",
371         acpi_cmbat_methods,
372         sizeof(struct acpi_cmbat_softc),
373 };
374
375 static devclass_t acpi_cmbat_devclass;
376 DRIVER_MODULE(acpi_cmbat, acpi, acpi_cmbat_driver, acpi_cmbat_devclass, 0, 0);
377
378 static int
379 acpi_cmbat_ioctl(u_long cmd, caddr_t addr, void *arg)
380 {
381         device_t dev;
382         union acpi_battery_ioctl_arg    *ioctl_arg;
383         struct acpi_cmbat_softc *sc;
384         struct acpi_bif *bifp;
385         struct acpi_bst *bstp;
386
387         ioctl_arg = (union acpi_battery_ioctl_arg *)addr;
388         if ((dev = devclass_get_device(acpi_cmbat_devclass,
389                         ioctl_arg->unit)) == NULL) {
390                 return(ENXIO);
391         }
392
393         if ((sc = device_get_softc(dev)) == NULL) {
394                 return(ENXIO);
395         }
396
397         switch (cmd) {
398         case ACPIIO_CMBAT_GET_BIF:
399                 acpi_cmbat_get_bif(dev);
400                 bifp = &ioctl_arg->bif;
401                 bifp->unit = sc->bif.unit;
402                 bifp->dcap = sc->bif.dcap;
403                 bifp->lfcap = sc->bif.lfcap;
404                 bifp->btech = sc->bif.btech;
405                 bifp->dvol = sc->bif.dvol;
406                 bifp->wcap = sc->bif.wcap;
407                 bifp->lcap = sc->bif.lcap;
408                 bifp->gra1 = sc->bif.gra1;
409                 bifp->gra2 = sc->bif.gra2;
410                 strncpy(bifp->model, sc->bif.model, sizeof(sc->bif.model));
411                 strncpy(bifp->serial, sc->bif.serial, sizeof(sc->bif.serial));
412                 strncpy(bifp->type, sc->bif.type, sizeof(sc->bif.type));
413                 strncpy(bifp->oeminfo, sc->bif.oeminfo, sizeof(sc->bif.oeminfo));
414                 break;
415
416         case ACPIIO_CMBAT_GET_BST:
417                 acpi_cmbat_get_bst(dev);
418                 bstp = &ioctl_arg->bst;
419                 bstp->state = sc->bst.state;
420                 bstp->rate = sc->bst.rate;
421                 bstp->cap = sc->bst.cap;
422                 bstp->volt = sc->bst.volt;
423                 break;
424         }
425
426         return(0);
427 }
428
429 static int
430 acpi_cmbat_get_total_battinfo(struct acpi_battinfo *battinfo)
431 {
432         int      i;
433         int      error;
434         int      batt_stat;
435         int      valid_rate, valid_units;
436         int      cap, min;
437         int      total_cap, total_min, total_full;
438         device_t dev;
439         struct acpi_cmbat_softc *sc;
440         static int       bat_units = 0;
441         static struct acpi_cmbat_softc  **bat = NULL;
442
443         cap = min = -1;
444         batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
445         error = 0;
446
447         /* Allocate array of softc pointers */
448         if (bat_units != acpi_cmbat_units) {
449                 if (bat != NULL) {
450                         free(bat, M_DEVBUF);
451                 }
452                 bat_units = 0;
453         }
454         if (bat == NULL) {
455                 bat_units = acpi_cmbat_units;
456                 bat = malloc(sizeof(struct acpi_cmbat_softc *) * bat_units,
457                              M_DEVBUF, M_NOWAIT);
458                 if (bat == NULL) {
459                         error = ENOMEM;
460                         goto out;
461                 }
462
463                 /* Collect softc pointers */
464                 for (i = 0; i < acpi_cmbat_units; i++) { 
465                         if ((dev = devclass_get_device(acpi_cmbat_devclass, i)) == NULL) {
466                                 error = ENXIO;
467                                 goto out;
468                         }
469
470                         if ((sc = device_get_softc(dev)) == NULL) {
471                                 error = ENXIO;
472                                 goto out;
473                         }
474
475                         bat[i] = sc;
476                 }
477         }
478
479         /* Get battery status, valid rate and valid units */
480         batt_stat = valid_rate = valid_units = 0;
481         for (i = 0; i < acpi_cmbat_units; i++) {
482                 acpi_cmbat_get_bif(bat[i]->dev);
483                 acpi_cmbat_get_bst(bat[i]->dev);
484
485                 /* If battey not installed, we get strange values */
486                 if (bat[i]->bst.state >= ACPI_BATT_STAT_MAX ||
487                     bat[i]->bst.cap == 0xffffffff ||
488                     bat[i]->bst.volt == 0xffffffff ||
489                     bat[i]->bif.lfcap == 0) {
490                         bat[i]->not_present = 1;
491                         continue;
492                 } else {
493                         bat[i]->not_present = 0;
494                 }
495
496                 valid_units++;
497
498                 bat[i]->cap = 100 * bat[i]->bst.cap / bat[i]->bif.lfcap;
499
500                 batt_stat |= bat[i]->bst.state;
501
502                 if (bat[i]->bst.rate > 0) {
503                         /*
504                          * XXX Hack to calculate total battery time.
505                          * Systems with 2 or more battries, they may get used
506                          * one by one, thus bst.rate is set only to the one
507                          * in use. For remaining batteries bst.rate = 0, which
508                          * makes it impossible to calculate remaining time.
509                          * Some other systems may need sum of bst.rate in 
510                          * dis-charging state.
511                          * There for we sum up the bst.rate that is valid
512                          * (in dis-charging state), and use the sum to
513                          * calcutate remaining batteries' time.
514                          */
515                         if (bat[i]->bst.state & ACPI_BATT_STAT_DISCHARG) {
516                                 valid_rate += bat[i]->bst.rate;
517                         }
518                 }
519         }
520
521         /* Calculate total battery capacity and time */
522         total_cap = total_min = total_full = 0;
523         for (i = 0; i < acpi_cmbat_units; i++) {
524                 if (bat[i]->not_present) {
525                         continue;
526                 }
527
528                 if (valid_rate > 0) {
529                         /* Use the sum of bst.rate */
530                         bat[i]->min = 60 * bat[i]->bst.cap / valid_rate;
531                 } else if (bat[i]->full_charge_time > 0) {
532                         bat[i]->min = (bat[i]->full_charge_time * bat[i]->cap) / 100;
533                 } else {
534                         /* Couldn't find valid rate and full battery time */
535                         bat[i]->min = 0;
536                 }
537                 total_min += bat[i]->min;
538                 total_cap += bat[i]->cap;
539                 total_full += bat[i]->full_charge_time;
540         }
541
542         /* Battery life */
543         if (valid_units == 0) {
544                 cap = -1;
545                 batt_stat = ACPI_BATT_STAT_NOT_PRESENT;
546         } else {
547                 cap = total_cap / valid_units;
548         }
549
550         /* Battery time */
551         if (valid_units == 0) {
552                 min = -1;
553         } else if (valid_rate == 0 || (batt_stat & ACPI_BATT_STAT_CHARGING)) {
554                 if (total_full == 0) {
555                         min = -1;
556                 } else {
557                         min = (total_full * cap) / 100;
558                 }
559         } else {
560                 min = total_min;
561         }
562
563         acpi_cmbat_info_updated(&acpi_cmbat_info_lastupdated);
564 out:
565         battinfo->cap = cap;
566         battinfo->min = min;
567         battinfo->state = batt_stat;
568
569         return (error);
570 }
571
572 /*
573  * Public interfaces.
574  */
575
576 int
577 acpi_cmbat_get_battinfo(int unit, struct acpi_battinfo *battinfo)
578 {
579         int      error;
580         device_t dev;
581         struct acpi_cmbat_softc *sc;
582
583         if (unit == -1) {
584                 return (acpi_cmbat_get_total_battinfo(battinfo));
585         }
586
587         if (acpi_cmbat_info_expired(&acpi_cmbat_info_lastupdated)) {
588                 error = acpi_cmbat_get_total_battinfo(battinfo);
589                 if (error) {
590                         goto out;
591                 }
592         }
593
594         error = 0;
595         if (unit >= acpi_cmbat_units) {
596                 error = ENXIO;
597                 goto out;
598         }
599
600         if ((dev = devclass_get_device(acpi_cmbat_devclass, unit)) == NULL) {
601                 error = ENXIO;
602                 goto out;
603         }
604
605         if ((sc = device_get_softc(dev)) == NULL) {
606                 error = ENXIO;
607                 goto out;
608         }
609
610         if (sc->not_present) {
611                 battinfo->cap = -1;
612                 battinfo->min = -1;
613                 battinfo->state = ACPI_BATT_STAT_NOT_PRESENT;
614         } else {
615                 battinfo->cap = sc->cap;
616                 battinfo->min = sc->min;
617                 battinfo->state = sc->bst.state;
618         }
619 out:
620         return (error);
621 }
622