sysmon_envsys_events.c revision 1.60 1 /* $NetBSD: sysmon_envsys_events.c,v 1.60 2008/11/04 22:16:15 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2007, 2008 Juan Romero Pardines.
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 ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * sysmon_envsys(9) events framework.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_events.c,v 1.60 2008/11/04 22:16:15 pgoyette Exp $");
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/conf.h>
38 #include <sys/errno.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/mutex.h>
43 #include <sys/kmem.h>
44 #include <sys/callout.h>
45
46 /* #define ENVSYS_DEBUG */
47
48 #include <dev/sysmon/sysmonvar.h>
49 #include <dev/sysmon/sysmon_envsysvar.h>
50
51 struct sme_sensor_event {
52 int state;
53 int event;
54 };
55
56 static const struct sme_sensor_event sme_sensor_event[] = {
57 { ENVSYS_SVALID, PENVSYS_EVENT_NORMAL },
58 { ENVSYS_SCRITOVER, PENVSYS_EVENT_CRITOVER },
59 { ENVSYS_SCRITUNDER, PENVSYS_EVENT_CRITUNDER },
60 { ENVSYS_SWARNOVER, PENVSYS_EVENT_WARNOVER },
61 { ENVSYS_SWARNUNDER, PENVSYS_EVENT_WARNUNDER },
62 { -1, -1 }
63 };
64
65 static bool sysmon_low_power;
66
67 #define SME_EVTIMO (SME_EVENTS_DEFTIMEOUT * hz)
68
69 static bool sme_event_check_low_power(void);
70 static bool sme_battery_check(void);
71 static bool sme_battery_critical(envsys_data_t *);
72 static bool sme_acadapter_check(void);
73
74 /*
75 * sme_event_register:
76 *
77 * + Registers a new sysmon envsys event or updates any event
78 * already in the queue.
79 */
80 int
81 sme_event_register(prop_dictionary_t sdict, envsys_data_t *edata,
82 struct sysmon_envsys *sme, const char *objkey,
83 int32_t critval, int crittype, int powertype)
84 {
85 sme_event_t *see = NULL, *osee = NULL;
86 prop_object_t obj;
87 bool critvalup = false;
88 int error = 0;
89 int real_crittype;
90 int32_t o_critval;
91
92 KASSERT(sdict != NULL || edata != NULL || sme != NULL);
93 /*
94 * Allocate a new sysmon_envsys event.
95 */
96 see = kmem_zalloc(sizeof(*see), KM_SLEEP);
97 if (see == NULL)
98 return ENOMEM;
99
100 /*
101 * Map user-types to kernel types
102 */
103 switch (crittype) {
104 case PENVSYS_EVENT_USER_CRITMAX:
105 case PENVSYS_EVENT_USER_CRITMIN:
106 case PENVSYS_EVENT_USER_WARNMAX:
107 case PENVSYS_EVENT_USER_WARNMIN:
108 real_crittype = PENVSYS_EVENT_USER_LIMITS;
109 break;
110 case PENVSYS_EVENT_BATT_USERCAP:
111 case PENVSYS_EVENT_BATT_USERWARN:
112 real_crittype = PENVSYS_EVENT_BATT_USER_LIMITS;
113 break;
114 default:
115 real_crittype = crittype;
116 }
117
118 /*
119 * check if the event is already on the list and return
120 * EEXIST if value provided hasn't been changed.
121 */
122 mutex_enter(&sme->sme_mtx);
123 LIST_FOREACH(osee, &sme->sme_events_list, see_list) {
124 if (strcmp(edata->desc, osee->see_pes.pes_sensname) == 0)
125 if (real_crittype == osee->see_type) {
126 switch (crittype) {
127 case PENVSYS_EVENT_USER_CRITMAX:
128 case PENVSYS_EVENT_BATT_USERCAP:
129 o_critval = osee->see_critmax;
130 break;
131 case PENVSYS_EVENT_USER_WARNMAX:
132 case PENVSYS_EVENT_BATT_USERWARN:
133 o_critval = osee->see_warnmax;
134 break;
135 case PENVSYS_EVENT_USER_WARNMIN:
136 o_critval = osee->see_warnmin;
137 break;
138 case PENVSYS_EVENT_USER_CRITMIN:
139 default:
140 o_critval = osee->see_critmin;
141 break;
142 }
143 if (o_critval == critval) {
144 DPRINTF(("%s: dev=%s sensor=%s type=%d "
145 "(already exists)\n", __func__,
146 osee->see_pes.pes_dvname,
147 osee->see_pes.pes_sensname,
148 osee->see_type));
149 error = EEXIST;
150 goto out;
151 }
152 critvalup = true;
153 break;
154 }
155 }
156
157 /*
158 * Critical condition operation requested by userland.
159 */
160 if (objkey && critval && critvalup) {
161 obj = prop_dictionary_get(sdict, objkey);
162 if (obj && prop_object_type(obj) == PROP_TYPE_NUMBER) {
163 /*
164 * object is already in dictionary and value
165 * provided is not the same than we have
166 * currently, update the critical value.
167 */
168 switch (crittype) {
169 case PENVSYS_EVENT_USER_CRITMAX:
170 case PENVSYS_EVENT_BATT_USERCAP:
171 osee->see_critmax = critval;
172 break;
173 case PENVSYS_EVENT_USER_WARNMAX:
174 case PENVSYS_EVENT_BATT_USERWARN:
175 osee->see_warnmax = critval;
176 break;
177 case PENVSYS_EVENT_USER_WARNMIN:
178 osee->see_warnmin = critval;
179 break;
180 case PENVSYS_EVENT_USER_CRITMIN:
181 default:
182 osee->see_critmin = critval;
183 break;
184 }
185 DPRINTF(("%s: (%s) event [sensor=%s type=%d] "
186 "(critval updated)\n", __func__, sme->sme_name,
187 edata->desc, osee->see_type));
188 error = sme_sensor_upint32(sdict, objkey, critval);
189 goto out;
190 }
191 }
192
193 /*
194 * New limit defined for existing event
195 */
196 if (osee != NULL) {
197 osee->see_edata = edata;
198 switch (crittype) {
199 case PENVSYS_EVENT_USER_CRITMAX:
200 case PENVSYS_EVENT_BATT_USERCAP:
201 osee->see_critmax = critval;
202 break;
203 case PENVSYS_EVENT_USER_WARNMAX:
204 case PENVSYS_EVENT_BATT_USERWARN:
205 osee->see_warnmax = critval;
206 break;
207 case PENVSYS_EVENT_USER_WARNMIN:
208 osee->see_warnmin = critval;
209 break;
210 case PENVSYS_EVENT_USER_CRITMIN:
211 default:
212 osee->see_critmin = critval;
213 break;
214 }
215 if (objkey && critval) {
216 error = sme_sensor_upint32(sdict, objkey, critval);
217 if (error)
218 goto out;
219 }
220 DPRINTF(("%s: (%s) new limit added to existing event, type %d "
221 "critmin=%" PRIu32 " warnmin=%" PRIu32 " warnmax=%"
222 PRIu32 " critmax=%d\n", __func__, osee->see_sme->sme_name,
223 osee->see_type, osee->see_critmin, osee->see_warnmin,
224 osee->see_warnmax, osee->see_critmax));
225 goto out;
226 }
227 /*
228 * New event requested.
229 */
230 see->see_edata = edata;
231 switch (crittype) {
232 case PENVSYS_EVENT_USER_CRITMAX:
233 case PENVSYS_EVENT_BATT_USERCAP:
234 see->see_critmax = critval;
235 break;
236 case PENVSYS_EVENT_USER_WARNMAX:
237 case PENVSYS_EVENT_BATT_USERWARN:
238 see->see_warnmax = critval;
239 break;
240 case PENVSYS_EVENT_USER_WARNMIN:
241 see->see_warnmin = critval;
242 break;
243 case PENVSYS_EVENT_USER_CRITMIN:
244 default:
245 see->see_critmin = critval;
246 break;
247 }
248 see->see_type = real_crittype;
249 see->see_sme = sme;
250
251 /* Initialize sensor type and previously-sent state */
252
253 see->see_pes.pes_type = powertype;
254 switch (powertype) {
255 case PENVSYS_TYPE_BATTERY:
256 see->see_evsent = ENVSYS_BATTERY_CAPACITY_NORMAL;
257 break;
258 case PENVSYS_TYPE_DRIVE:
259 see->see_evsent = ENVSYS_DRIVE_EMPTY;
260 break;
261 case PENVSYS_TYPE_FAN:
262 case PENVSYS_TYPE_INDICATOR:
263 case PENVSYS_TYPE_TEMP:
264 case PENVSYS_TYPE_POWER:
265 case PENVSYS_TYPE_RESISTANCE:
266 case PENVSYS_TYPE_VOLTAGE:
267 see->see_evsent = ENVSYS_SVALID;
268 break;
269 default:
270 see->see_evsent = 0;
271 }
272
273 (void)strlcpy(see->see_pes.pes_dvname, sme->sme_name,
274 sizeof(see->see_pes.pes_dvname));
275 (void)strlcpy(see->see_pes.pes_sensname, edata->desc,
276 sizeof(see->see_pes.pes_sensname));
277
278 LIST_INSERT_HEAD(&sme->sme_events_list, see, see_list);
279 if (objkey && critval) {
280 error = sme_sensor_upint32(sdict, objkey, critval);
281 if (error)
282 goto out;
283 }
284 DPRINTF(("%s: (%s) event registered (sensor=%s snum=%d type=%d "
285 "critmin=%" PRIu32 " warnmin=%" PRIu32 " warnmax=%" PRIu32
286 " crixmax=%" PRIu32 ")\n", __func__,
287 see->see_sme->sme_name, see->see_pes.pes_sensname,
288 see->see_edata->sensor, see->see_type, see->see_critmin,
289 see->see_warnmin, see->see_warnmax, see->see_critmax));
290 /*
291 * Initialize the events framework if it wasn't initialized before.
292 */
293 if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0)
294 error = sme_events_init(sme);
295 out:
296 mutex_exit(&sme->sme_mtx);
297 if (error || critvalup)
298 kmem_free(see, sizeof(*see));
299 return error;
300 }
301
302 /*
303 * sme_event_unregister_all:
304 *
305 * + Unregisters all events associated with a sysmon envsys device.
306 */
307 void
308 sme_event_unregister_all(struct sysmon_envsys *sme)
309 {
310 sme_event_t *see;
311 int evcounter = 0;
312
313 KASSERT(sme != NULL);
314
315 mutex_enter(&sme->sme_mtx);
316 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
317 while (see->see_flags & SEE_EVENT_WORKING)
318 cv_wait(&sme->sme_condvar, &sme->sme_mtx);
319
320 if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0)
321 evcounter++;
322 }
323
324 DPRINTF(("%s: total events %d (%s)\n", __func__,
325 evcounter, sme->sme_name));
326
327 while ((see = LIST_FIRST(&sme->sme_events_list))) {
328 if (evcounter == 0)
329 break;
330
331 if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0) {
332 LIST_REMOVE(see, see_list);
333 DPRINTF(("%s: event %s %d removed (%s)\n", __func__,
334 see->see_pes.pes_sensname, see->see_type,
335 sme->sme_name));
336 kmem_free(see, sizeof(*see));
337 evcounter--;
338 }
339 }
340
341 if (LIST_EMPTY(&sme->sme_events_list))
342 if (sme->sme_flags & SME_CALLOUT_INITIALIZED)
343 sme_events_destroy(sme);
344 mutex_exit(&sme->sme_mtx);
345 }
346
347 /*
348 * sme_event_unregister:
349 *
350 * + Unregisters an event from the specified sysmon envsys device.
351 */
352 int
353 sme_event_unregister(struct sysmon_envsys *sme, const char *sensor, int type)
354 {
355 sme_event_t *see;
356 bool found = false;
357
358 KASSERT(sensor != NULL);
359
360 mutex_enter(&sme->sme_mtx);
361 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
362 if (strcmp(see->see_pes.pes_sensname, sensor) == 0) {
363 if (see->see_type == type) {
364 found = true;
365 break;
366 }
367 }
368 }
369
370 if (!found) {
371 mutex_exit(&sme->sme_mtx);
372 return EINVAL;
373 }
374
375 /*
376 * Wait for the event to finish its work, remove from the list
377 * and release resouces.
378 */
379 while (see->see_flags & SEE_EVENT_WORKING)
380 cv_wait(&sme->sme_condvar, &sme->sme_mtx);
381
382 DPRINTF(("%s: removed dev=%s sensor=%s type=%d\n",
383 __func__, see->see_pes.pes_dvname, sensor, type));
384 LIST_REMOVE(see, see_list);
385 /*
386 * So the events list is empty, we'll do the following:
387 *
388 * - stop and destroy the callout.
389 * - destroy the workqueue.
390 */
391 if (LIST_EMPTY(&sme->sme_events_list))
392 sme_events_destroy(sme);
393 mutex_exit(&sme->sme_mtx);
394
395 kmem_free(see, sizeof(*see));
396 return 0;
397 }
398
399 /*
400 * sme_event_drvadd:
401 *
402 * + Registers a new event for a device that had enabled any of
403 * the monitoring flags in the driver.
404 */
405 void
406 sme_event_drvadd(void *arg)
407 {
408 sme_event_drv_t *sed_t = arg;
409 int error = 0;
410
411 KASSERT(sed_t != NULL);
412
413 #define SEE_REGEVENT(a, b, c) \
414 do { \
415 if (sed_t->sed_edata->flags & (a)) { \
416 char str[ENVSYS_DESCLEN] = "monitoring-state-"; \
417 \
418 error = sme_event_register(sed_t->sed_sdict, \
419 sed_t->sed_edata, \
420 sed_t->sed_sme, \
421 NULL, \
422 0, \
423 (b), \
424 sed_t->sed_powertype); \
425 if (error && error != EEXIST) \
426 printf("%s: failed to add event! " \
427 "error=%d sensor=%s event=%s\n", \
428 __func__, error, \
429 sed_t->sed_edata->desc, (c)); \
430 else { \
431 (void)strlcat(str, (c), sizeof(str)); \
432 prop_dictionary_set_bool(sed_t->sed_sdict, \
433 str, \
434 true); \
435 } \
436 } \
437 } while (/* CONSTCOND */ 0)
438
439 SEE_REGEVENT(ENVSYS_FMONCRITICAL,
440 PENVSYS_EVENT_CRITICAL,
441 "critical");
442
443 SEE_REGEVENT(ENVSYS_FMONCRITUNDER | ENVSYS_FMONCRITOVER |
444 ENVSYS_FMONWARNUNDER | ENVSYS_FMONWARNOVER,
445 PENVSYS_EVENT_HW_LIMITS,
446 "hw-range-limits");
447
448 SEE_REGEVENT(ENVSYS_FMONSTCHANGED,
449 PENVSYS_EVENT_STATE_CHANGED,
450 "state-changed");
451
452 /*
453 * we are done, free memory now.
454 */
455 kmem_free(sed_t, sizeof(*sed_t));
456 }
457
458 /*
459 * sme_events_init:
460 *
461 * + Initialize the events framework for this device.
462 */
463 int
464 sme_events_init(struct sysmon_envsys *sme)
465 {
466 int error = 0;
467 uint64_t timo;
468
469 KASSERT(sme != NULL);
470 KASSERT(mutex_owned(&sme->sme_mtx));
471
472 if (sme->sme_events_timeout)
473 timo = sme->sme_events_timeout * hz;
474 else
475 timo = SME_EVTIMO;
476
477 error = workqueue_create(&sme->sme_wq, sme->sme_name,
478 sme_events_worker, sme, PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE);
479 if (error)
480 return error;
481
482 mutex_init(&sme->sme_callout_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
483 callout_init(&sme->sme_callout, CALLOUT_MPSAFE);
484 callout_setfunc(&sme->sme_callout, sme_events_check, sme);
485 callout_schedule(&sme->sme_callout, timo);
486 sme->sme_flags |= SME_CALLOUT_INITIALIZED;
487 DPRINTF(("%s: events framework initialized for '%s'\n",
488 __func__, sme->sme_name));
489
490 return error;
491 }
492
493 /*
494 * sme_events_destroy:
495 *
496 * + Destroys the event framework for this device: callout
497 * stopped, workqueue destroyed and callout mutex destroyed.
498 */
499 void
500 sme_events_destroy(struct sysmon_envsys *sme)
501 {
502 KASSERT(mutex_owned(&sme->sme_mtx));
503
504 callout_stop(&sme->sme_callout);
505 workqueue_destroy(sme->sme_wq);
506 mutex_destroy(&sme->sme_callout_mtx);
507 callout_destroy(&sme->sme_callout);
508 sme->sme_flags &= ~SME_CALLOUT_INITIALIZED;
509 DPRINTF(("%s: events framework destroyed for '%s'\n",
510 __func__, sme->sme_name));
511 }
512
513 /*
514 * sme_events_check:
515 *
516 * + Passes the events to the workqueue thread and stops
517 * the callout if the 'low-power' condition is triggered.
518 */
519 void
520 sme_events_check(void *arg)
521 {
522 struct sysmon_envsys *sme = arg;
523 sme_event_t *see;
524 uint64_t timo;
525
526 KASSERT(sme != NULL);
527
528 mutex_enter(&sme->sme_callout_mtx);
529 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
530 workqueue_enqueue(sme->sme_wq, &see->see_wk, NULL);
531 see->see_edata->flags |= ENVSYS_FNEED_REFRESH;
532 }
533 if (sme->sme_events_timeout)
534 timo = sme->sme_events_timeout * hz;
535 else
536 timo = SME_EVTIMO;
537 if (!sysmon_low_power)
538 callout_schedule(&sme->sme_callout, timo);
539 mutex_exit(&sme->sme_callout_mtx);
540 }
541
542 /*
543 * sme_events_worker:
544 *
545 * + workqueue thread that checks if there's a critical condition
546 * and sends an event if it was triggered.
547 */
548 void
549 sme_events_worker(struct work *wk, void *arg)
550 {
551 const struct sme_description_table *sdt = NULL;
552 const struct sme_sensor_event *sse = sme_sensor_event;
553 sme_event_t *see = (void *)wk;
554 struct sysmon_envsys *sme = see->see_sme;
555 envsys_data_t *edata = see->see_edata;
556 int i, state = 0;
557
558 KASSERT(wk == &see->see_wk);
559 KASSERT(sme != NULL || edata != NULL);
560
561 mutex_enter(&sme->sme_mtx);
562 if ((see->see_flags & SEE_EVENT_WORKING) == 0)
563 see->see_flags |= SEE_EVENT_WORKING;
564 /*
565 * sme_events_check marks the first event for the device to
566 * mak us refresh it here. Don't refresh if the driver uses
567 * its own method for refreshing.
568 */
569 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) {
570 if ((see->see_edata->flags & ENVSYS_FNEED_REFRESH) != 0) {
571 /* refresh sensor in device */
572 (*sme->sme_refresh)(sme, edata);
573 see->see_edata->flags &= ~ENVSYS_FNEED_REFRESH;
574 }
575 }
576
577 DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d type=%d state=%d units=%d "
578 "value_cur=%d\n", __func__, sme->sme_name, edata->desc,
579 edata->sensor, see->see_type, edata->state, edata->units,
580 edata->value_cur));
581
582 /* skip the event if current sensor is in invalid state */
583 if (edata->state == ENVSYS_SINVALID)
584 goto out;
585
586 switch (see->see_type) {
587 /*
588 * For user range limits, calculate a new state first
589 * State based on user limits will override any hardware
590 * detected state.
591 */
592 case PENVSYS_EVENT_USER_LIMITS:
593 case PENVSYS_EVENT_BATT_USER_LIMITS:
594 #define __EXCEEDED_LIMIT(lim, rel) ((lim) && edata->value_cur rel (lim))
595 if __EXCEEDED_LIMIT(see->see_critmin, <)
596 edata->state = ENVSYS_SCRITUNDER;
597 else if __EXCEEDED_LIMIT(see->see_warnmin, <)
598 edata->state = ENVSYS_SWARNUNDER;
599 else if __EXCEEDED_LIMIT(see->see_warnmax, >)
600 edata->state = ENVSYS_SWARNOVER;
601 else if __EXCEEDED_LIMIT(see->see_critmax, >)
602 edata->state = ENVSYS_SCRITOVER;
603 /* FALLTHROUGH */
604 #undef __EXCEED_LIMIT
605 /*
606 * For hardware and user range limits, send event if state has changed
607 */
608 case PENVSYS_EVENT_HW_LIMITS:
609 if (edata->state == see->see_evsent)
610 break;
611
612 for (i = 0; sse[i].state != -1; i++)
613 if (sse[i].state == edata->state)
614 break;
615
616 if (sse[i].state == -1)
617 break;
618
619 if (edata->state == ENVSYS_SVALID)
620 sysmon_penvsys_event(&see->see_pes,
621 PENVSYS_EVENT_NORMAL);
622 else
623 sysmon_penvsys_event(&see->see_pes, sse[i].event);
624
625 see->see_evsent = edata->state;
626
627 break;
628
629 /*
630 * Send PENVSYS_EVENT_CRITICAL event if:
631 * State has gone from non-CRITICAL to CRITICAL,
632 * State remains CRITICAL and value has changed, or
633 * State has returned from CRITICAL to non-CRITICAL
634 */
635 case PENVSYS_EVENT_CRITICAL:
636 if (edata->state == ENVSYS_SVALID &&
637 see->see_evsent != 0) {
638 sysmon_penvsys_event(&see->see_pes,
639 PENVSYS_EVENT_NORMAL);
640 see->see_evsent = 0;
641 } else if (edata->state == ENVSYS_SCRITICAL &&
642 see->see_evsent != edata->value_cur) {
643 sysmon_penvsys_event(&see->see_pes,
644 PENVSYS_EVENT_CRITICAL);
645 see->see_evsent = edata->value_cur;
646 }
647 break;
648
649 /*
650 * if value_cur is not normal (battery) or online (drive),
651 * send the event...
652 */
653 case PENVSYS_EVENT_STATE_CHANGED:
654 /*
655 * the state has not been changed, just ignore the event.
656 */
657 if (edata->value_cur == see->see_evsent)
658 break;
659
660 switch (edata->units) {
661 case ENVSYS_DRIVE:
662 sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
663 state = ENVSYS_DRIVE_ONLINE;
664 break;
665 case ENVSYS_BATTERY_CAPACITY:
666 sdt = sme_get_description_table(
667 SME_DESC_BATTERY_CAPACITY);
668 state = ENVSYS_BATTERY_CAPACITY_NORMAL;
669 break;
670 default:
671 panic("%s: invalid units for ENVSYS_FMONSTCHANGED",
672 __func__);
673 }
674
675 for (i = 0; sdt[i].type != -1; i++)
676 if (sdt[i].type == edata->value_cur)
677 break;
678
679 if (sdt[i].type == -1)
680 break;
681
682 /*
683 * copy current state description.
684 */
685 (void)strlcpy(see->see_pes.pes_statedesc, sdt[i].desc,
686 sizeof(see->see_pes.pes_statedesc));
687
688 /*
689 * state is ok again... send a normal event.
690 */
691 if (see->see_evsent && edata->value_cur == state) {
692 sysmon_penvsys_event(&see->see_pes,
693 PENVSYS_EVENT_NORMAL);
694 see->see_evsent = false;
695 }
696
697 /*
698 * state has been changed... send event.
699 */
700 if (see->see_evsent || edata->value_cur != state) {
701 /*
702 * save current drive state.
703 */
704 see->see_evsent = edata->value_cur;
705 sysmon_penvsys_event(&see->see_pes, see->see_type);
706 }
707
708 /*
709 * There's no need to continue if it's a drive sensor.
710 */
711 if (edata->units == ENVSYS_DRIVE)
712 break;
713
714 /*
715 * Check if the system is running in low power and send the
716 * event to powerd (if running) or shutdown the system
717 * otherwise.
718 */
719 if (!sysmon_low_power && sme_event_check_low_power()) {
720 struct penvsys_state pes;
721
722 /*
723 * Stop the callout and send the 'low-power' event.
724 */
725 sysmon_low_power = true;
726 callout_stop(&sme->sme_callout);
727 pes.pes_type = PENVSYS_TYPE_BATTERY;
728 sysmon_penvsys_event(&pes, PENVSYS_EVENT_LOW_POWER);
729 }
730 break;
731 default:
732 panic("%s: invalid event type %d", __func__, see->see_type);
733 }
734
735 out:
736 see->see_flags &= ~SEE_EVENT_WORKING;
737 cv_broadcast(&sme->sme_condvar);
738 mutex_exit(&sme->sme_mtx);
739 }
740
741 /*
742 * Returns true if the system is in low power state: an AC adapter
743 * is OFF and all batteries are in LOW/CRITICAL state.
744 */
745 static bool
746 sme_event_check_low_power(void)
747 {
748 if (!sme_acadapter_check())
749 return false;
750
751 return sme_battery_check();
752 }
753
754 /*
755 * Called with the sysmon_envsys device mtx held through the
756 * workqueue thread.
757 */
758 static bool
759 sme_acadapter_check(void)
760 {
761 struct sysmon_envsys *sme;
762 envsys_data_t *edata;
763 bool dev = false, sensor = false;
764
765 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
766 if (sme->sme_class == SME_CLASS_ACADAPTER) {
767 dev = true;
768 break;
769 }
770 }
771
772 /*
773 * No AC Adapter devices were found.
774 */
775 if (!dev)
776 return false;
777
778 /*
779 * Check if there's an AC adapter device connected.
780 */
781 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
782 if (edata->units == ENVSYS_INDICATOR) {
783 sensor = true;
784 /* refresh current sensor */
785 (*sme->sme_refresh)(sme, edata);
786 if (edata->value_cur)
787 return false;
788 }
789 }
790
791 if (!sensor)
792 return false;
793
794 /*
795 * AC adapter found and not connected.
796 */
797 return true;
798 }
799
800 /*
801 * Called with the sysmon_envsys device mtx held through the
802 * workqueue thread.
803 */
804 static bool
805 sme_battery_check(void)
806 {
807 struct sysmon_envsys *sme;
808 envsys_data_t *edata;
809 int batteriesfound = 0;
810 bool present, batterycap, batterycharge;
811
812 /*
813 * Check for battery devices and its state.
814 */
815 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
816 if (sme->sme_class != SME_CLASS_BATTERY)
817 continue;
818
819 present = true;
820 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
821 if (edata->units == ENVSYS_INDICATOR &&
822 !edata->value_cur) {
823 present = false;
824 break;
825 }
826 }
827 if (!present)
828 continue;
829 /*
830 * We've found a battery device...
831 */
832 batteriesfound++;
833 batterycap = batterycharge = false;
834 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
835 if (edata->units == ENVSYS_BATTERY_CAPACITY) {
836 batterycap = true;
837 if (!sme_battery_critical(edata))
838 return false;
839 } else if (edata->units == ENVSYS_BATTERY_CHARGE) {
840 batterycharge = true;
841 if (edata->value_cur)
842 return false;
843 }
844 }
845 if (!batterycap || !batterycharge)
846 return false;
847 }
848
849 if (!batteriesfound)
850 return false;
851
852 /*
853 * All batteries in low/critical capacity and discharging.
854 */
855 return true;
856 }
857
858 static bool
859 sme_battery_critical(envsys_data_t *edata)
860 {
861 if (edata->value_cur == ENVSYS_BATTERY_CAPACITY_CRITICAL ||
862 edata->value_cur == ENVSYS_BATTERY_CAPACITY_LOW)
863 return true;
864
865 return false;
866 }
867