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