Wed Jan 23 18:04:34 2013 UTC ()
Fix spelling, grammar, typos.


(mbalmer)
diff -r1.108 -r1.109 src/sys/dev/sysmon/sysmon_envsys_events.c

cvs diff -r1.108 -r1.109 src/sys/dev/sysmon/sysmon_envsys_events.c (switch to unified diff)

--- src/sys/dev/sysmon/sysmon_envsys_events.c 2012/12/14 15:33:19 1.108
+++ src/sys/dev/sysmon/sysmon_envsys_events.c 2013/01/23 18:04:33 1.109
@@ -1,1124 +1,1124 @@ @@ -1,1124 +1,1124 @@
1/* $NetBSD: sysmon_envsys_events.c,v 1.108 2012/12/14 15:33:19 pgoyette Exp $ */ 1/* $NetBSD: sysmon_envsys_events.c,v 1.109 2013/01/23 18:04:33 mbalmer Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2007, 2008 Juan Romero Pardines. 4 * Copyright (c) 2007, 2008 Juan Romero Pardines.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 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 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 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 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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 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. 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27 27
28/* 28/*
29 * sysmon_envsys(9) events framework. 29 * sysmon_envsys(9) events framework.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_events.c,v 1.108 2012/12/14 15:33:19 pgoyette Exp $"); 33__KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_events.c,v 1.109 2013/01/23 18:04:33 mbalmer Exp $");
34 34
35#include <sys/param.h> 35#include <sys/param.h>
36#include <sys/types.h> 36#include <sys/types.h>
37#include <sys/conf.h> 37#include <sys/conf.h>
38#include <sys/errno.h> 38#include <sys/errno.h>
39#include <sys/kernel.h> 39#include <sys/kernel.h>
40#include <sys/systm.h> 40#include <sys/systm.h>
41#include <sys/proc.h> 41#include <sys/proc.h>
42#include <sys/mutex.h> 42#include <sys/mutex.h>
43#include <sys/kmem.h> 43#include <sys/kmem.h>
44#include <sys/callout.h> 44#include <sys/callout.h>
45 45
46#include <dev/sysmon/sysmonvar.h> 46#include <dev/sysmon/sysmonvar.h>
47#include <dev/sysmon/sysmon_envsysvar.h> 47#include <dev/sysmon/sysmon_envsysvar.h>
48 48
49struct sme_sensor_event { 49struct sme_sensor_event {
50 int state; 50 int state;
51 int event; 51 int event;
52}; 52};
53 53
54static const struct sme_sensor_event sme_sensor_event[] = { 54static const struct sme_sensor_event sme_sensor_event[] = {
55 { ENVSYS_SVALID, PENVSYS_EVENT_NORMAL }, 55 { ENVSYS_SVALID, PENVSYS_EVENT_NORMAL },
56 { ENVSYS_SCRITOVER, PENVSYS_EVENT_CRITOVER }, 56 { ENVSYS_SCRITOVER, PENVSYS_EVENT_CRITOVER },
57 { ENVSYS_SCRITUNDER, PENVSYS_EVENT_CRITUNDER }, 57 { ENVSYS_SCRITUNDER, PENVSYS_EVENT_CRITUNDER },
58 { ENVSYS_SWARNOVER, PENVSYS_EVENT_WARNOVER }, 58 { ENVSYS_SWARNOVER, PENVSYS_EVENT_WARNOVER },
59 { ENVSYS_SWARNUNDER, PENVSYS_EVENT_WARNUNDER }, 59 { ENVSYS_SWARNUNDER, PENVSYS_EVENT_WARNUNDER },
60 { ENVSYS_BATTERY_CAPACITY_NORMAL, PENVSYS_EVENT_NORMAL }, 60 { ENVSYS_BATTERY_CAPACITY_NORMAL, PENVSYS_EVENT_NORMAL },
61 { ENVSYS_BATTERY_CAPACITY_WARNING, PENVSYS_EVENT_BATT_WARN }, 61 { ENVSYS_BATTERY_CAPACITY_WARNING, PENVSYS_EVENT_BATT_WARN },
62 { ENVSYS_BATTERY_CAPACITY_CRITICAL, PENVSYS_EVENT_BATT_CRIT }, 62 { ENVSYS_BATTERY_CAPACITY_CRITICAL, PENVSYS_EVENT_BATT_CRIT },
63 { ENVSYS_BATTERY_CAPACITY_HIGH, PENVSYS_EVENT_BATT_HIGH }, 63 { ENVSYS_BATTERY_CAPACITY_HIGH, PENVSYS_EVENT_BATT_HIGH },
64 { ENVSYS_BATTERY_CAPACITY_MAX, PENVSYS_EVENT_BATT_MAX }, 64 { ENVSYS_BATTERY_CAPACITY_MAX, PENVSYS_EVENT_BATT_MAX },
65 { -1, -1 } 65 { -1, -1 }
66}; 66};
67 67
68static const struct op_t { 68static const struct op_t {
69 const char *name; 69 const char *name;
70 enum envsys_lims idx; 70 enum envsys_lims idx;
71 uint32_t prop; 71 uint32_t prop;
72} limit_ops[] = { 72} limit_ops[] = {
73 /* Value-based limits */ 73 /* Value-based limits */
74 { "critical-max", ENVSYS_LIM_CRITMAX, PROP_CRITMAX }, 74 { "critical-max", ENVSYS_LIM_CRITMAX, PROP_CRITMAX },
75 { "warning-max", ENVSYS_LIM_WARNMAX, PROP_WARNMAX }, 75 { "warning-max", ENVSYS_LIM_WARNMAX, PROP_WARNMAX },
76 { "warning-min", ENVSYS_LIM_WARNMIN, PROP_WARNMIN }, 76 { "warning-min", ENVSYS_LIM_WARNMIN, PROP_WARNMIN },
77 { "critical-min", ENVSYS_LIM_CRITMIN, PROP_CRITMIN }, 77 { "critical-min", ENVSYS_LIM_CRITMIN, PROP_CRITMIN },
78 78
79 /* %Capacity-based limits */ 79 /* %Capacity-based limits */
80 { "maximum-capacity", ENVSYS_LIM_CRITMAX, PROP_BATTMAX }, 80 { "maximum-capacity", ENVSYS_LIM_CRITMAX, PROP_BATTMAX },
81 { "high-capacity", ENVSYS_LIM_WARNMAX, PROP_BATTHIGH }, 81 { "high-capacity", ENVSYS_LIM_WARNMAX, PROP_BATTHIGH },
82 { "warning-capacity", ENVSYS_LIM_WARNMIN, PROP_BATTWARN }, 82 { "warning-capacity", ENVSYS_LIM_WARNMIN, PROP_BATTWARN },
83 { "critical-capacity", ENVSYS_LIM_CRITMIN, PROP_BATTCAP }, 83 { "critical-capacity", ENVSYS_LIM_CRITMIN, PROP_BATTCAP },
84 { NULL, 0, 0 } 84 { NULL, 0, 0 }
85}; 85};
86 86
87static const struct ev_reg_t { 87static const struct ev_reg_t {
88 uint32_t crittype; 88 uint32_t crittype;
89 uint32_t powertype; 89 uint32_t powertype;
90 const char *name; 90 const char *name;
91} reg_events[] = { 91} reg_events[] = {
92 { ENVSYS_FMONCRITICAL, PENVSYS_EVENT_CRITICAL, "critical" }, 92 { ENVSYS_FMONCRITICAL, PENVSYS_EVENT_CRITICAL, "critical" },
93 { ENVSYS_FMONSTCHANGED, PENVSYS_EVENT_STATE_CHANGED, "state-changed" }, 93 { ENVSYS_FMONSTCHANGED, PENVSYS_EVENT_STATE_CHANGED, "state-changed" },
94 { ENVSYS_FMONLIMITS, PENVSYS_EVENT_LIMITS, "hw-range-limits" }, 94 { ENVSYS_FMONLIMITS, PENVSYS_EVENT_LIMITS, "hw-range-limits" },
95 { ENVSYS_FHAS_ENTROPY, PENVSYS_EVENT_NULL, "refresh-event" }, 95 { ENVSYS_FHAS_ENTROPY, PENVSYS_EVENT_NULL, "refresh-event" },
96 { 0, 0, NULL } 96 { 0, 0, NULL }
97}; 97};
98 98
99static bool sysmon_low_power; 99static bool sysmon_low_power;
100 100
101#define SME_EVTIMO (SME_EVENTS_DEFTIMEOUT * hz) 101#define SME_EVTIMO (SME_EVENTS_DEFTIMEOUT * hz)
102 102
103static bool sme_event_check_low_power(void); 103static bool sme_event_check_low_power(void);
104static bool sme_battery_check(void); 104static bool sme_battery_check(void);
105static bool sme_battery_critical(envsys_data_t *); 105static bool sme_battery_critical(envsys_data_t *);
106static bool sme_acadapter_check(void); 106static bool sme_acadapter_check(void);
107 107
108static void sme_remove_event(sme_event_t *, struct sysmon_envsys *); 108static void sme_remove_event(sme_event_t *, struct sysmon_envsys *);
109 109
110/* 110/*
111 * sme_event_register: 111 * sme_event_register:
112 * 112 *
113 * + Registers a new sysmon envsys event or updates any event 113 * + Registers a new sysmon envsys event or updates any event
114 * already in the queue. 114 * already in the queue.
115 */ 115 */
116int 116int
117sme_event_register(prop_dictionary_t sdict, envsys_data_t *edata, 117sme_event_register(prop_dictionary_t sdict, envsys_data_t *edata,
118 struct sysmon_envsys *sme, sysmon_envsys_lim_t *lims, 118 struct sysmon_envsys *sme, sysmon_envsys_lim_t *lims,
119 uint32_t props, int crittype, int powertype) 119 uint32_t props, int crittype, int powertype)
120{ 120{
121 sme_event_t *see = NULL, *osee = NULL; 121 sme_event_t *see = NULL, *osee = NULL;
122 prop_object_t obj; 122 prop_object_t obj;
123 int error = 0; 123 int error = 0;
124 const char *objkey; 124 const char *objkey;
125 const struct op_t *op; 125 const struct op_t *op;
126 126
127 KASSERT(sdict != NULL); 127 KASSERT(sdict != NULL);
128 KASSERT(edata != NULL); 128 KASSERT(edata != NULL);
129 KASSERT(sme != NULL); 129 KASSERT(sme != NULL);
130 KASSERT(lims != NULL); 130 KASSERT(lims != NULL);
131 131
132 /* 132 /*
133 * Some validation first for limit-checking events 133 * Some validation first for limit-checking events
134 * 134 *
135 * 1. Limits are not permitted if the units is ENVSYS_INDICATOR 135 * 1. Limits are not permitted if the units is ENVSYS_INDICATOR
136 * or ENVSYS_BATTERY_CHARGE. 136 * or ENVSYS_BATTERY_CHARGE.
137 * 137 *
138 * 2. Capacity limits are permitted only if the sensor has the 138 * 2. Capacity limits are permitted only if the sensor has the
139 * ENVSYS_FPERCENT flag set and value_max is set. 139 * ENVSYS_FPERCENT flag set and value_max is set.
140 * 140 *
141 * 3. It is not permissible for both capacity and value limits 141 * 3. It is not permissible for both capacity and value limits
142 * to coexist. 142 * to coexist.
143 * 143 *
144 * Note that it permissible for a sensor to have value limits 144 * Note that it permissible for a sensor to have value limits
145 * even if its ENVSYS_FPERCENT flag and value_max are set. 145 * even if its ENVSYS_FPERCENT flag and value_max are set.
146 */ 146 */
147 147
148 DPRINTF(("%s: units %d props 0x%04x upropset 0x%04x max_val %d" 148 DPRINTF(("%s: units %d props 0x%04x upropset 0x%04x max_val %d"
149 " edata-flags 0x%04x\n", __func__, edata->units, props, 149 " edata-flags 0x%04x\n", __func__, edata->units, props,
150 edata->upropset, edata->value_max, edata->flags)); 150 edata->upropset, edata->value_max, edata->flags));
151 151
152 if (props) 152 if (props)
153 if (edata->units == ENVSYS_INDICATOR || 153 if (edata->units == ENVSYS_INDICATOR ||
154 edata->units == ENVSYS_BATTERY_CHARGE) 154 edata->units == ENVSYS_BATTERY_CHARGE)
155 return ENOTSUP; 155 return ENOTSUP;
156 156
157 if ((props & PROP_CAP_LIMITS) && 157 if ((props & PROP_CAP_LIMITS) &&
158 ((edata->value_max == 0) || 158 ((edata->value_max == 0) ||
159 !(edata->flags & ENVSYS_FPERCENT) || 159 !(edata->flags & ENVSYS_FPERCENT) ||
160 (props & PROP_VAL_LIMITS) || 160 (props & PROP_VAL_LIMITS) ||
161 (edata->upropset & PROP_VAL_LIMITS))) 161 (edata->upropset & PROP_VAL_LIMITS)))
162 props = 0; 162 props = 0;
163 163
164 if ((props & PROP_VAL_LIMITS) && (edata->upropset & PROP_CAP_LIMITS)) 164 if ((props & PROP_VAL_LIMITS) && (edata->upropset & PROP_CAP_LIMITS))
165 props = 0; 165 props = 0;
166 166
167 /*  167 /*
168 * check if the event is already on the list and return 168 * check if the event is already on the list and return
169 * EEXIST if value provided hasn't been changed. 169 * EEXIST if value provided hasn't been changed.
170 */ 170 */
171 mutex_enter(&sme->sme_mtx); 171 mutex_enter(&sme->sme_mtx);
172 LIST_FOREACH(osee, &sme->sme_events_list, see_list) { 172 LIST_FOREACH(osee, &sme->sme_events_list, see_list) {
173 if (strcmp(edata->desc, osee->see_pes.pes_sensname) != 0) 173 if (strcmp(edata->desc, osee->see_pes.pes_sensname) != 0)
174 continue; 174 continue;
175 if (crittype != osee->see_type && 175 if (crittype != osee->see_type &&
176 osee->see_type != PENVSYS_EVENT_NULL) 176 osee->see_type != PENVSYS_EVENT_NULL)
177 continue; 177 continue;
178 178
179 /* 179 /*
180 * We found an existing event for this sensor. Make  180 * We found an existing event for this sensor. Make
181 * sure it references the correct edata 181 * sure it references the correct edata
182 */ 182 */
183 KASSERT(edata == osee->see_edata); 183 KASSERT(edata == osee->see_edata);
184 184
185 DPRINTF(("%s: dev %s sensor %s: event type %d exists\n", 185 DPRINTF(("%s: dev %s sensor %s: event type %d exists\n",
186 __func__, sme->sme_name, edata->desc, crittype)); 186 __func__, sme->sme_name, edata->desc, crittype));
187 187
188 see = osee; 188 see = osee;
189 if (props & edata->upropset & (PROP_CRITMAX | PROP_BATTMAX)) { 189 if (props & edata->upropset & (PROP_CRITMAX | PROP_BATTMAX)) {
190 if (lims->sel_critmax == edata->limits.sel_critmax) { 190 if (lims->sel_critmax == edata->limits.sel_critmax) {
191 DPRINTF(("%s: critmax exists\n", __func__)); 191 DPRINTF(("%s: critmax exists\n", __func__));
192 error = EEXIST; 192 error = EEXIST;
193 props &= ~(PROP_CRITMAX | PROP_BATTMAX); 193 props &= ~(PROP_CRITMAX | PROP_BATTMAX);
194 } 194 }
195 } 195 }
196 if (props & edata->upropset & (PROP_WARNMAX | PROP_BATTHIGH)) { 196 if (props & edata->upropset & (PROP_WARNMAX | PROP_BATTHIGH)) {
197 if (lims->sel_warnmax == edata->limits.sel_warnmax) { 197 if (lims->sel_warnmax == edata->limits.sel_warnmax) {
198 DPRINTF(("%s: warnmax exists\n", __func__)); 198 DPRINTF(("%s: warnmax exists\n", __func__));
199 error = EEXIST; 199 error = EEXIST;
200 props &= ~(PROP_WARNMAX | PROP_BATTHIGH); 200 props &= ~(PROP_WARNMAX | PROP_BATTHIGH);
201 } 201 }
202 } 202 }
203 if (props & edata->upropset & (PROP_WARNMIN | PROP_BATTWARN)) { 203 if (props & edata->upropset & (PROP_WARNMIN | PROP_BATTWARN)) {
204 if (lims->sel_warnmin == edata->limits.sel_warnmin) { 204 if (lims->sel_warnmin == edata->limits.sel_warnmin) {
205 DPRINTF(("%s: warnmin exists\n", __func__)); 205 DPRINTF(("%s: warnmin exists\n", __func__));
206 error = EEXIST; 206 error = EEXIST;
207 props &= ~(PROP_WARNMIN | PROP_BATTWARN); 207 props &= ~(PROP_WARNMIN | PROP_BATTWARN);
208 } 208 }
209 } 209 }
210 if (props & edata->upropset & (PROP_CRITMIN | PROP_BATTCAP)) { 210 if (props & edata->upropset & (PROP_CRITMIN | PROP_BATTCAP)) {
211 if (lims->sel_critmin == edata->limits.sel_critmin) { 211 if (lims->sel_critmin == edata->limits.sel_critmin) {
212 DPRINTF(("%s: critmin exists\n", __func__)); 212 DPRINTF(("%s: critmin exists\n", __func__));
213 error = EEXIST; 213 error = EEXIST;
214 props &= ~(PROP_CRITMIN | PROP_BATTCAP); 214 props &= ~(PROP_CRITMIN | PROP_BATTCAP);
215 } 215 }
216 } 216 }
217 if (props && see->see_type == PENVSYS_EVENT_NULL) 217 if (props && see->see_type == PENVSYS_EVENT_NULL)
218 see->see_type = crittype; 218 see->see_type = crittype;
219 219
220 break; 220 break;
221 } 221 }
222 if (crittype == PENVSYS_EVENT_NULL && see != NULL) { 222 if (crittype == PENVSYS_EVENT_NULL && see != NULL) {
223 mutex_exit(&sme->sme_mtx); 223 mutex_exit(&sme->sme_mtx);
224 return EEXIST; 224 return EEXIST;
225 } 225 }
226 226
227 if (see == NULL) { 227 if (see == NULL) {
228 /* 228 /*
229 * New event requested - allocate a sysmon_envsys event. 229 * New event requested - allocate a sysmon_envsys event.
230 */ 230 */
231 see = kmem_zalloc(sizeof(*see), KM_SLEEP); 231 see = kmem_zalloc(sizeof(*see), KM_SLEEP);
232 if (see == NULL) 232 if (see == NULL)
233 return ENOMEM; 233 return ENOMEM;
234 234
235 DPRINTF(("%s: dev %s sensor %s: new event\n", 235 DPRINTF(("%s: dev %s sensor %s: new event\n",
236 __func__, sme->sme_name, edata->desc)); 236 __func__, sme->sme_name, edata->desc));
237 237
238 see->see_type = crittype; 238 see->see_type = crittype;
239 see->see_sme = sme; 239 see->see_sme = sme;
240 see->see_edata = edata; 240 see->see_edata = edata;
241 241
242 /* Initialize sensor type and previously-sent state */ 242 /* Initialize sensor type and previously-sent state */
243 243
244 see->see_pes.pes_type = powertype; 244 see->see_pes.pes_type = powertype;
245 245
246 switch (crittype) { 246 switch (crittype) {
247 case PENVSYS_EVENT_CAPACITY: 247 case PENVSYS_EVENT_CAPACITY:
248 see->see_evstate = ENVSYS_BATTERY_CAPACITY_NORMAL; 248 see->see_evstate = ENVSYS_BATTERY_CAPACITY_NORMAL;
249 break; 249 break;
250 case PENVSYS_EVENT_STATE_CHANGED: 250 case PENVSYS_EVENT_STATE_CHANGED:
251 if (edata->units == ENVSYS_BATTERY_CAPACITY) 251 if (edata->units == ENVSYS_BATTERY_CAPACITY)
252 see->see_evstate =  252 see->see_evstate =
253 ENVSYS_BATTERY_CAPACITY_NORMAL; 253 ENVSYS_BATTERY_CAPACITY_NORMAL;
254 else if (edata->units == ENVSYS_DRIVE) 254 else if (edata->units == ENVSYS_DRIVE)
255 see->see_evstate = ENVSYS_DRIVE_EMPTY; 255 see->see_evstate = ENVSYS_DRIVE_EMPTY;
256 else if (edata->units == ENVSYS_INDICATOR) 256 else if (edata->units == ENVSYS_INDICATOR)
257 see->see_evstate = ENVSYS_SVALID; 257 see->see_evstate = ENVSYS_SVALID;
258 else 258 else
259 panic("%s: bad units for " 259 panic("%s: bad units for "
260 "PENVSYS_EVENT_STATE_CHANGED", __func__); 260 "PENVSYS_EVENT_STATE_CHANGED", __func__);
261 break; 261 break;
262 case PENVSYS_EVENT_CRITICAL: 262 case PENVSYS_EVENT_CRITICAL:
263 case PENVSYS_EVENT_LIMITS: 263 case PENVSYS_EVENT_LIMITS:
264 default: 264 default:
265 see->see_evstate = ENVSYS_SVALID; 265 see->see_evstate = ENVSYS_SVALID;
266 break; 266 break;
267 } 267 }
268 see->see_evvalue = 0; 268 see->see_evvalue = 0;
269 269
270 (void)strlcpy(see->see_pes.pes_dvname, sme->sme_name, 270 (void)strlcpy(see->see_pes.pes_dvname, sme->sme_name,
271 sizeof(see->see_pes.pes_dvname)); 271 sizeof(see->see_pes.pes_dvname));
272 (void)strlcpy(see->see_pes.pes_sensname, edata->desc, 272 (void)strlcpy(see->see_pes.pes_sensname, edata->desc,
273 sizeof(see->see_pes.pes_sensname)); 273 sizeof(see->see_pes.pes_sensname));
274 } 274 }
275 275
276 /* 276 /*
277 * Limit operation requested. 277 * Limit operation requested.
278 */ 278 */
279 for (op = limit_ops; op->name != NULL; op++) { 279 for (op = limit_ops; op->name != NULL; op++) {
280 if (props & op->prop) { 280 if (props & op->prop) {
281 objkey = op->name; 281 objkey = op->name;
282 obj = prop_dictionary_get(sdict, objkey); 282 obj = prop_dictionary_get(sdict, objkey);
283 if (obj != NULL && 283 if (obj != NULL &&
284 prop_object_type(obj) != PROP_TYPE_NUMBER) { 284 prop_object_type(obj) != PROP_TYPE_NUMBER) {
285 DPRINTF(("%s: (%s) %s object not TYPE_NUMBER\n", 285 DPRINTF(("%s: (%s) %s object not TYPE_NUMBER\n",
286 __func__, sme->sme_name, objkey)); 286 __func__, sme->sme_name, objkey));
287 error = ENOTSUP; 287 error = ENOTSUP;
288 } else { 288 } else {
289 edata->limits.sel_limit_list[op->idx] = 289 edata->limits.sel_limit_list[op->idx] =
290 lims->sel_limit_list[op->idx]; 290 lims->sel_limit_list[op->idx];
291 error = sme_sensor_upint32(sdict, objkey, 291 error = sme_sensor_upint32(sdict, objkey,
292 lims->sel_limit_list[op->idx]); 292 lims->sel_limit_list[op->idx]);
293 DPRINTF(("%s: (%s) event [sensor=%s type=%d] " 293 DPRINTF(("%s: (%s) event [sensor=%s type=%d] "
294 "(%s updated)\n", __func__, sme->sme_name, 294 "(%s updated)\n", __func__, sme->sme_name,
295 edata->desc, crittype, objkey)); 295 edata->desc, crittype, objkey));
296 } 296 }
297 if (error && error != EEXIST) 297 if (error && error != EEXIST)
298 goto out; 298 goto out;
299 edata->upropset |= op->prop; 299 edata->upropset |= op->prop;
300 } 300 }
301 } 301 }
302 302
303 if (props & PROP_DRIVER_LIMITS) 303 if (props & PROP_DRIVER_LIMITS)
304 edata->upropset |= PROP_DRIVER_LIMITS; 304 edata->upropset |= PROP_DRIVER_LIMITS;
305 else 305 else
306 edata->upropset &= ~PROP_DRIVER_LIMITS; 306 edata->upropset &= ~PROP_DRIVER_LIMITS;
307 307
308 DPRINTF(("%s: (%s) event registered (sensor=%s snum=%d type=%d " 308 DPRINTF(("%s: (%s) event registered (sensor=%s snum=%d type=%d "
309 "critmin=%" PRIu32 " warnmin=%" PRIu32 " warnmax=%" PRIu32 309 "critmin=%" PRIu32 " warnmin=%" PRIu32 " warnmax=%" PRIu32
310 " critmax=%" PRIu32 " props 0x%04x)\n", __func__, 310 " critmax=%" PRIu32 " props 0x%04x)\n", __func__,
311 see->see_sme->sme_name, see->see_pes.pes_sensname, 311 see->see_sme->sme_name, see->see_pes.pes_sensname,
312 edata->sensor, see->see_type, edata->limits.sel_critmin, 312 edata->sensor, see->see_type, edata->limits.sel_critmin,
313 edata->limits.sel_warnmin, edata->limits.sel_warnmax, 313 edata->limits.sel_warnmin, edata->limits.sel_warnmax,
314 edata->limits.sel_critmax, edata->upropset)); 314 edata->limits.sel_critmax, edata->upropset));
315 /* 315 /*
316 * Initialize the events framework if it wasn't initialized before. 316 * Initialize the events framework if it wasn't initialized before.
317 */ 317 */
318 if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0) 318 if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0)
319 error = sme_events_init(sme); 319 error = sme_events_init(sme);
320 320
321 /* 321 /*
322 * If driver requested notification, advise it of new 322 * If driver requested notification, advise it of new
323 * limit values 323 * limit values
324 */ 324 */
325 if (sme->sme_set_limits) 325 if (sme->sme_set_limits)
326 (*sme->sme_set_limits)(sme, edata, &(edata->limits), 326 (*sme->sme_set_limits)(sme, edata, &(edata->limits),
327 &(edata->upropset)); 327 &(edata->upropset));
328 328
329out: 329out:
330 if ((error == 0 || error == EEXIST) && osee == NULL) 330 if ((error == 0 || error == EEXIST) && osee == NULL)
331 LIST_INSERT_HEAD(&sme->sme_events_list, see, see_list); 331 LIST_INSERT_HEAD(&sme->sme_events_list, see, see_list);
332 332
333 mutex_exit(&sme->sme_mtx); 333 mutex_exit(&sme->sme_mtx);
334 334
335 return error; 335 return error;
336} 336}
337 337
338/* 338/*
339 * sme_event_unregister_all: 339 * sme_event_unregister_all:
340 * 340 *
341 * + Unregisters all events associated with a sysmon envsys device. 341 * + Unregisters all events associated with a sysmon envsys device.
342 */ 342 */
343void 343void
344sme_event_unregister_all(struct sysmon_envsys *sme) 344sme_event_unregister_all(struct sysmon_envsys *sme)
345{ 345{
346 sme_event_t *see; 346 sme_event_t *see;
347 int evcounter = 0; 347 int evcounter = 0;
348 348
349 KASSERT(sme != NULL); 349 KASSERT(sme != NULL);
350 350
351 mutex_enter(&sme->sme_mtx); 351 mutex_enter(&sme->sme_mtx);
352 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 352 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
353 while (see->see_flags & SEE_EVENT_WORKING) 353 while (see->see_flags & SEE_EVENT_WORKING)
354 cv_wait(&sme->sme_condvar, &sme->sme_mtx); 354 cv_wait(&sme->sme_condvar, &sme->sme_mtx);
355 355
356 if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0) 356 if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0)
357 evcounter++; 357 evcounter++;
358 } 358 }
359 359
360 DPRINTF(("%s: total events %d (%s)\n", __func__, 360 DPRINTF(("%s: total events %d (%s)\n", __func__,
361 evcounter, sme->sme_name)); 361 evcounter, sme->sme_name));
362 362
363 while ((see = LIST_FIRST(&sme->sme_events_list))) { 363 while ((see = LIST_FIRST(&sme->sme_events_list))) {
364 if (evcounter == 0) 364 if (evcounter == 0)
365 break; 365 break;
366 366
367 if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0) { 367 if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0) {
368 DPRINTF(("%s: event %s %d removed (%s)\n", __func__, 368 DPRINTF(("%s: event %s %d removed (%s)\n", __func__,
369 see->see_pes.pes_sensname, see->see_type, 369 see->see_pes.pes_sensname, see->see_type,
370 sme->sme_name)); 370 sme->sme_name));
371 sme_remove_event(see, sme); 371 sme_remove_event(see, sme);
372 372
373 evcounter--; 373 evcounter--;
374 } 374 }
375 } 375 }
376 376
377 if (LIST_EMPTY(&sme->sme_events_list)) 377 if (LIST_EMPTY(&sme->sme_events_list))
378 if (sme->sme_flags & SME_CALLOUT_INITIALIZED) 378 if (sme->sme_flags & SME_CALLOUT_INITIALIZED)
379 sme_events_destroy(sme); 379 sme_events_destroy(sme);
380 mutex_exit(&sme->sme_mtx); 380 mutex_exit(&sme->sme_mtx);
381} 381}
382 382
383/* 383/*
384 * sme_event_unregister: 384 * sme_event_unregister:
385 * 385 *
386 * + Unregisters an event from the specified sysmon envsys device. 386 * + Unregisters an event from the specified sysmon envsys device.
387 */ 387 */
388int 388int
389sme_event_unregister(struct sysmon_envsys *sme, const char *sensor, int type) 389sme_event_unregister(struct sysmon_envsys *sme, const char *sensor, int type)
390{ 390{
391 sme_event_t *see; 391 sme_event_t *see;
392 bool found = false; 392 bool found = false;
393 393
394 KASSERT(sensor != NULL); 394 KASSERT(sensor != NULL);
395 395
396 mutex_enter(&sme->sme_mtx); 396 mutex_enter(&sme->sme_mtx);
397 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 397 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
398 if (strcmp(see->see_pes.pes_sensname, sensor) == 0) { 398 if (strcmp(see->see_pes.pes_sensname, sensor) == 0) {
399 if (see->see_type == type) { 399 if (see->see_type == type) {
400 found = true; 400 found = true;
401 break; 401 break;
402 } 402 }
403 } 403 }
404 } 404 }
405 405
406 if (!found) { 406 if (!found) {
407 mutex_exit(&sme->sme_mtx); 407 mutex_exit(&sme->sme_mtx);
408 return EINVAL; 408 return EINVAL;
409 } 409 }
410 410
411 /* 411 /*
412 * Wait for the event to finish its work, remove from the list 412 * Wait for the event to finish its work, remove it from the list
413 * and release resouces. 413 * and release resources.
414 */ 414 */
415 while (see->see_flags & SEE_EVENT_WORKING) 415 while (see->see_flags & SEE_EVENT_WORKING)
416 cv_wait(&sme->sme_condvar, &sme->sme_mtx); 416 cv_wait(&sme->sme_condvar, &sme->sme_mtx);
417 417
418 DPRINTF(("%s: removed dev=%s sensor=%s type=%d\n", 418 DPRINTF(("%s: removed dev=%s sensor=%s type=%d\n",
419 __func__, see->see_pes.pes_dvname, sensor, type)); 419 __func__, see->see_pes.pes_dvname, sensor, type));
420 420
421 sme_remove_event(see, sme); 421 sme_remove_event(see, sme);
422 422
423 mutex_exit(&sme->sme_mtx); 423 mutex_exit(&sme->sme_mtx);
424 return 0; 424 return 0;
425} 425}
426 426
427/* 427/*
428 * sme_event_unregister_sensor: 428 * sme_event_unregister_sensor:
429 * 429 *
430 * + Unregisters any event associated with a specific sensor 430 * + Unregisters any event associated with a specific sensor
431 * The caller must already own the sme_mtx. 431 * The caller must already own the sme_mtx.
432 */ 432 */
433int 433int
434sme_event_unregister_sensor(struct sysmon_envsys *sme, envsys_data_t *edata) 434sme_event_unregister_sensor(struct sysmon_envsys *sme, envsys_data_t *edata)
435{ 435{
436 sme_event_t *see; 436 sme_event_t *see;
437 bool found = false; 437 bool found = false;
438 438
439 KASSERT(mutex_owned(&sme->sme_mtx)); 439 KASSERT(mutex_owned(&sme->sme_mtx));
440 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 440 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
441 if (see->see_edata == edata) { 441 if (see->see_edata == edata) {
442 found = true; 442 found = true;
443 break; 443 break;
444 } 444 }
445 } 445 }
446 if (!found) 446 if (!found)
447 return EINVAL; 447 return EINVAL;
448 448
449 /* 449 /*
450 * Wait for the event to finish its work, remove from the list 450 * Wait for the event to finish its work, remove it from the list
451 * and release resouces. 451 * and release resources.
452 */ 452 */
453 while (see->see_flags & SEE_EVENT_WORKING) 453 while (see->see_flags & SEE_EVENT_WORKING)
454 cv_wait(&sme->sme_condvar, &sme->sme_mtx); 454 cv_wait(&sme->sme_condvar, &sme->sme_mtx);
455 455
456 DPRINTF(("%s: removed dev=%s sensor=%s\n", 456 DPRINTF(("%s: removed dev=%s sensor=%s\n",
457 __func__, see->see_pes.pes_dvname, edata->desc)); 457 __func__, see->see_pes.pes_dvname, edata->desc));
458 458
459 sme_remove_event(see, sme); 459 sme_remove_event(see, sme);
460 460
461 return 0; 461 return 0;
462} 462}
463 463
464static void 464static void
465sme_remove_event(sme_event_t *see, struct sysmon_envsys *sme) 465sme_remove_event(sme_event_t *see, struct sysmon_envsys *sme)
466{ 466{
467 467
468 KASSERT(mutex_owned(&sme->sme_mtx)); 468 KASSERT(mutex_owned(&sme->sme_mtx));
469 469
470 if (see->see_edata->flags & ENVSYS_FHAS_ENTROPY) 470 if (see->see_edata->flags & ENVSYS_FHAS_ENTROPY)
471 rnd_detach_source(&see->see_edata->rnd_src); 471 rnd_detach_source(&see->see_edata->rnd_src);
472 LIST_REMOVE(see, see_list); 472 LIST_REMOVE(see, see_list);
473 /* 473 /*
474 * So the events list is empty, we'll do the following: 474 * So the events list is empty, we'll do the following:
475 * 475 *
476 * - stop and destroy the callout. 476 * - stop and destroy the callout.
477 * - destroy the workqueue. 477 * - destroy the workqueue.
478 */ 478 */
479 if (LIST_EMPTY(&sme->sme_events_list)) 479 if (LIST_EMPTY(&sme->sme_events_list))
480 sme_events_destroy(sme); 480 sme_events_destroy(sme);
481 481
482 kmem_free(see, sizeof(*see)); 482 kmem_free(see, sizeof(*see));
483} 483}
484 484
485/* 485/*
486 * sme_event_drvadd: 486 * sme_event_drvadd:
487 * 487 *
488 * + Registers a new event for a device that had enabled any of 488 * + Registers a new event for a device that had enabled any of
489 * the monitoring flags in the driver. 489 * the monitoring flags in the driver.
490 */ 490 */
491void 491void
492sme_event_drvadd(void *arg) 492sme_event_drvadd(void *arg)
493{ 493{
494 sme_event_drv_t *sed_t = arg; 494 sme_event_drv_t *sed_t = arg;
495 sysmon_envsys_lim_t lims; 495 sysmon_envsys_lim_t lims;
496 uint32_t props; 496 uint32_t props;
497 int error = 0; 497 int error = 0;
498 const struct ev_reg_t *reg; 498 const struct ev_reg_t *reg;
499 499
500 KASSERT(sed_t != NULL); 500 KASSERT(sed_t != NULL);
501 501
502 /* 502 /*
503 * If driver provides a method to retrieve its internal limit 503 * If driver provides a method to retrieve its internal limit
504 * values, call it and use those returned values as initial 504 * values, call it and use those returned values as initial
505 * limits for event monitoring. 505 * limits for event monitoring.
506 */ 506 */
507 props = 0; 507 props = 0;
508 if (sed_t->sed_edata->flags & ENVSYS_FMONLIMITS) 508 if (sed_t->sed_edata->flags & ENVSYS_FMONLIMITS)
509 if (sed_t->sed_sme->sme_get_limits) 509 if (sed_t->sed_sme->sme_get_limits)
510 (*sed_t->sed_sme->sme_get_limits)(sed_t->sed_sme, 510 (*sed_t->sed_sme->sme_get_limits)(sed_t->sed_sme,
511 sed_t->sed_edata, 511 sed_t->sed_edata,
512 &lims, &props); 512 &lims, &props);
513 /* 513 /*
514 * If driver doesn't provide a way to "absorb" user-specified 514 * If driver doesn't provide a way to "absorb" user-specified
515 * limit values, we must monitor all limits ourselves 515 * limit values, we must monitor all limits ourselves
516 */ 516 */
517 if (sed_t->sed_sme->sme_set_limits == NULL) 517 if (sed_t->sed_sme->sme_set_limits == NULL)
518 props &= ~PROP_DRIVER_LIMITS; 518 props &= ~PROP_DRIVER_LIMITS;
519 519
520 /* Register the events that were specified */ 520 /* Register the events that were specified */
521 521
522 for (reg = reg_events; reg->name != NULL; reg++) { 522 for (reg = reg_events; reg->name != NULL; reg++) {
523 if (sed_t->sed_edata->flags & reg->crittype) { 523 if (sed_t->sed_edata->flags & reg->crittype) {
524 524
525 error = sme_event_register(sed_t->sed_sdict, 525 error = sme_event_register(sed_t->sed_sdict,
526 sed_t->sed_edata, 526 sed_t->sed_edata,
527 sed_t->sed_sme, 527 sed_t->sed_sme,
528 &lims, props, 528 &lims, props,
529 reg->powertype, 529 reg->powertype,
530 sed_t->sed_powertype); 530 sed_t->sed_powertype);
531 if (error && error != EEXIST) 531 if (error && error != EEXIST)
532 printf("%s: failed to add event! " 532 printf("%s: failed to add event! "
533 "error=%d sensor=%s event=%s\n", 533 "error=%d sensor=%s event=%s\n",
534 __func__, error, 534 __func__, error,
535 sed_t->sed_edata->desc, reg->name); 535 sed_t->sed_edata->desc, reg->name);
536 else { 536 else {
537 char str[ENVSYS_DESCLEN] = "monitoring-state-"; 537 char str[ENVSYS_DESCLEN] = "monitoring-state-";
538 (void)strlcat(str, reg->name, sizeof(str)); 538 (void)strlcat(str, reg->name, sizeof(str));
539 prop_dictionary_set_bool(sed_t->sed_sdict, 539 prop_dictionary_set_bool(sed_t->sed_sdict,
540 str, true); 540 str, true);
541 } 541 }
542 } 542 }
543 } 543 }
544 544
545 /*  545 /*
546 * we are done, free memory now. 546 * we are done, free memory now.
547 */ 547 */
548 kmem_free(sed_t, sizeof(*sed_t)); 548 kmem_free(sed_t, sizeof(*sed_t));
549} 549}
550 550
551/* 551/*
552 * sme_events_init: 552 * sme_events_init:
553 * 553 *
554 * + Initialize the events framework for this device. 554 * + Initialize the events framework for this device.
555 */ 555 */
556int 556int
557sme_events_init(struct sysmon_envsys *sme) 557sme_events_init(struct sysmon_envsys *sme)
558{ 558{
559 int error = 0; 559 int error = 0;
560 560
561 KASSERT(sme != NULL); 561 KASSERT(sme != NULL);
562 KASSERT(mutex_owned(&sme->sme_mtx)); 562 KASSERT(mutex_owned(&sme->sme_mtx));
563 563
564 error = workqueue_create(&sme->sme_wq, sme->sme_name, 564 error = workqueue_create(&sme->sme_wq, sme->sme_name,
565 sme_events_worker, sme, PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE); 565 sme_events_worker, sme, PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE);
566 if (error) 566 if (error)
567 return error; 567 return error;
568 568
569 mutex_init(&sme->sme_callout_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK); 569 mutex_init(&sme->sme_callout_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
570 callout_init(&sme->sme_callout, CALLOUT_MPSAFE); 570 callout_init(&sme->sme_callout, CALLOUT_MPSAFE);
571 callout_setfunc(&sme->sme_callout, sme_events_check, sme); 571 callout_setfunc(&sme->sme_callout, sme_events_check, sme);
572 sme->sme_flags |= SME_CALLOUT_INITIALIZED; 572 sme->sme_flags |= SME_CALLOUT_INITIALIZED;
573 sme_schedule_callout(sme); 573 sme_schedule_callout(sme);
574 DPRINTF(("%s: events framework initialized for '%s'\n", 574 DPRINTF(("%s: events framework initialized for '%s'\n",
575 __func__, sme->sme_name)); 575 __func__, sme->sme_name));
576 576
577 return error; 577 return error;
578} 578}
579 579
580/* 580/*
581 * sme_schedule_callout 581 * sme_schedule_callout
582 * 582 *
583 * (Re)-schedule the device's callout timer 583 * (Re)-schedule the device's callout timer
584 */ 584 */
585void 585void
586sme_schedule_callout(struct sysmon_envsys *sme) 586sme_schedule_callout(struct sysmon_envsys *sme)
587{ 587{
588 uint64_t timo; 588 uint64_t timo;
589 589
590 KASSERT(sme != NULL); 590 KASSERT(sme != NULL);
591 591
592 if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0) 592 if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0)
593 return; 593 return;
594 594
595 if (sme->sme_events_timeout) 595 if (sme->sme_events_timeout)
596 timo = sme->sme_events_timeout * hz; 596 timo = sme->sme_events_timeout * hz;
597 else 597 else
598 timo = SME_EVTIMO; 598 timo = SME_EVTIMO;
599 599
600 callout_stop(&sme->sme_callout); 600 callout_stop(&sme->sme_callout);
601 callout_schedule(&sme->sme_callout, timo); 601 callout_schedule(&sme->sme_callout, timo);
602} 602}
603 603
604/* 604/*
605 * sme_events_destroy: 605 * sme_events_destroy:
606 * 606 *
607 * + Destroys the event framework for this device: callout 607 * + Destroys the event framework for this device: callout
608 * stopped, workqueue destroyed and callout mutex destroyed. 608 * stopped, workqueue destroyed and callout mutex destroyed.
609 */ 609 */
610void 610void
611sme_events_destroy(struct sysmon_envsys *sme) 611sme_events_destroy(struct sysmon_envsys *sme)
612{ 612{
613 KASSERT(mutex_owned(&sme->sme_mtx)); 613 KASSERT(mutex_owned(&sme->sme_mtx));
614 614
615 callout_stop(&sme->sme_callout); 615 callout_stop(&sme->sme_callout);
616 workqueue_destroy(sme->sme_wq); 616 workqueue_destroy(sme->sme_wq);
617 mutex_destroy(&sme->sme_callout_mtx); 617 mutex_destroy(&sme->sme_callout_mtx);
618 callout_destroy(&sme->sme_callout); 618 callout_destroy(&sme->sme_callout);
619 sme->sme_flags &= ~SME_CALLOUT_INITIALIZED; 619 sme->sme_flags &= ~SME_CALLOUT_INITIALIZED;
620 DPRINTF(("%s: events framework destroyed for '%s'\n", 620 DPRINTF(("%s: events framework destroyed for '%s'\n",
621 __func__, sme->sme_name)); 621 __func__, sme->sme_name));
622} 622}
623 623
624/* 624/*
625 * sysmon_envsys_update_limits 625 * sysmon_envsys_update_limits
626 * 626 *
627 * + If a driver needs to update the limits that it is providing, 627 * + If a driver needs to update the limits that it is providing,
628 * we need to update the dictionary data as well as the limits. 628 * we need to update the dictionary data as well as the limits.
629 * This only makes sense if the driver is capable of providing 629 * This only makes sense if the driver is capable of providing
630 * its limits, and if there is a limits event-monitor. 630 * its limits, and if there is a limits event-monitor.
631 */ 631 */
632int 632int
633sysmon_envsys_update_limits(struct sysmon_envsys *sme, envsys_data_t *edata) 633sysmon_envsys_update_limits(struct sysmon_envsys *sme, envsys_data_t *edata)
634{ 634{
635 int err; 635 int err;
636 636
637 sysmon_envsys_acquire(sme, false); 637 sysmon_envsys_acquire(sme, false);
638 if (sme->sme_get_limits == NULL || 638 if (sme->sme_get_limits == NULL ||
639 (edata->flags & ENVSYS_FMONLIMITS) == 0) 639 (edata->flags & ENVSYS_FMONLIMITS) == 0)
640 err = EINVAL; 640 err = EINVAL;
641 else 641 else
642 err = sme_update_limits(sme, edata); 642 err = sme_update_limits(sme, edata);
643 sysmon_envsys_release(sme, false); 643 sysmon_envsys_release(sme, false);
644 644
645 return err; 645 return err;
646} 646}
647 647
648/* 648/*
649 * sme_update_limits 649 * sme_update_limits
650 * 650 *
651 * + Internal version of sysmon_envsys_update_limits() to be used 651 * + Internal version of sysmon_envsys_update_limits() to be used
652 * when the device has already been sysmon_envsys_acquire()d. 652 * when the device has already been sysmon_envsys_acquire()d.
653 */ 653 */
654 654
655int 655int
656sme_update_limits(struct sysmon_envsys *sme, envsys_data_t *edata) 656sme_update_limits(struct sysmon_envsys *sme, envsys_data_t *edata)
657{ 657{
658 prop_dictionary_t sdict = NULL; 658 prop_dictionary_t sdict = NULL;
659 prop_array_t array = NULL; 659 prop_array_t array = NULL;
660 sysmon_envsys_lim_t lims; 660 sysmon_envsys_lim_t lims;
661 sme_event_t *see; 661 sme_event_t *see;
662 uint32_t props = 0; 662 uint32_t props = 0;
663 663
664 /* Find the dictionary for this sensor */ 664 /* Find the dictionary for this sensor */
665 array = prop_dictionary_get(sme_propd, sme->sme_name); 665 array = prop_dictionary_get(sme_propd, sme->sme_name);
666 if (array == NULL || 666 if (array == NULL ||
667 prop_object_type(array) != PROP_TYPE_ARRAY) { 667 prop_object_type(array) != PROP_TYPE_ARRAY) {
668 DPRINTF(("%s: array device failed\n", __func__)); 668 DPRINTF(("%s: array device failed\n", __func__));
669 return EINVAL; 669 return EINVAL;
670 } 670 }
671  671
672 sdict = prop_array_get(array, edata->sensor); 672 sdict = prop_array_get(array, edata->sensor);
673 if (sdict == NULL) { 673 if (sdict == NULL) {
674 return EINVAL; 674 return EINVAL;
675 } 675 }
676 676
677 /* Find the event definition to get its powertype */ 677 /* Find the event definition to get its powertype */
678 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 678 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
679 if (edata == see->see_edata && 679 if (edata == see->see_edata &&
680 see->see_type == PENVSYS_EVENT_LIMITS) 680 see->see_type == PENVSYS_EVENT_LIMITS)
681 break; 681 break;
682 } 682 }
683 if (see == NULL) 683 if (see == NULL)
684 return EINVAL; 684 return EINVAL;
685 685
686 /* Update limit values from driver if possible */ 686 /* Update limit values from driver if possible */
687 if (sme->sme_get_limits != NULL) 687 if (sme->sme_get_limits != NULL)
688 (*sme->sme_get_limits)(sme, edata, &lims, &props); 688 (*sme->sme_get_limits)(sme, edata, &lims, &props);
689 689
690 /* Update event and dictionary */ 690 /* Update event and dictionary */
691 sme_event_register(sdict, edata, sme, &lims, props, 691 sme_event_register(sdict, edata, sme, &lims, props,
692 PENVSYS_EVENT_LIMITS, see->see_pes.pes_type); 692 PENVSYS_EVENT_LIMITS, see->see_pes.pes_type);
693 693
694 return 0; 694 return 0;
695} 695}
696 696
697/* 697/*
698 * sme_events_check: 698 * sme_events_check:
699 * 699 *
700 * + Passes the events to the workqueue thread and stops 700 * + Passes the events to the workqueue thread and stops
701 * the callout if the 'low-power' condition is triggered. 701 * the callout if the 'low-power' condition is triggered.
702 */ 702 */
703void 703void
704sme_events_check(void *arg) 704sme_events_check(void *arg)
705{ 705{
706 struct sysmon_envsys *sme = arg; 706 struct sysmon_envsys *sme = arg;
707 sme_event_t *see; 707 sme_event_t *see;
708 uint64_t timo; 708 uint64_t timo;
709 709
710 KASSERT(sme != NULL); 710 KASSERT(sme != NULL);
711 711
712 mutex_enter(&sme->sme_callout_mtx); 712 mutex_enter(&sme->sme_callout_mtx);
713 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 713 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
714 workqueue_enqueue(sme->sme_wq, &see->see_wk, NULL); 714 workqueue_enqueue(sme->sme_wq, &see->see_wk, NULL);
715 see->see_edata->flags |= ENVSYS_FNEED_REFRESH; 715 see->see_edata->flags |= ENVSYS_FNEED_REFRESH;
716 } 716 }
717 if (sme->sme_events_timeout) 717 if (sme->sme_events_timeout)
718 timo = sme->sme_events_timeout * hz; 718 timo = sme->sme_events_timeout * hz;
719 else 719 else
720 timo = SME_EVTIMO; 720 timo = SME_EVTIMO;
721 if (!sysmon_low_power) 721 if (!sysmon_low_power)
722 sme_schedule_callout(sme); 722 sme_schedule_callout(sme);
723 mutex_exit(&sme->sme_callout_mtx); 723 mutex_exit(&sme->sme_callout_mtx);
724} 724}
725 725
726/* 726/*
727 * sme_events_worker: 727 * sme_events_worker:
728 * 728 *
729 * + workqueue thread that checks if there's a critical condition 729 * + workqueue thread that checks if there's a critical condition
730 * and sends an event if it was triggered. 730 * and sends an event if it was triggered.
731 */ 731 */
732void 732void
733sme_events_worker(struct work *wk, void *arg) 733sme_events_worker(struct work *wk, void *arg)
734{ 734{
735 sme_event_t *see = (void *)wk; 735 sme_event_t *see = (void *)wk;
736 struct sysmon_envsys *sme = see->see_sme; 736 struct sysmon_envsys *sme = see->see_sme;
737 envsys_data_t *edata = see->see_edata; 737 envsys_data_t *edata = see->see_edata;
738 738
739 KASSERT(wk == &see->see_wk); 739 KASSERT(wk == &see->see_wk);
740 KASSERT(sme != NULL || edata != NULL); 740 KASSERT(sme != NULL || edata != NULL);
741 741
742 mutex_enter(&sme->sme_mtx); 742 mutex_enter(&sme->sme_mtx);
743 see->see_flags |= SEE_EVENT_WORKING; 743 see->see_flags |= SEE_EVENT_WORKING;
744 /*  744 /*
745 * sme_events_check marks the sensors to make us refresh them here. 745 * sme_events_check marks the sensors to make us refresh them here.
746 * sme_envsys_refresh_sensor will not call the driver if the driver 746 * sme_envsys_refresh_sensor will not call the driver if the driver
747 * does its own setting of the sensor value. 747 * does its own setting of the sensor value.
748 */ 748 */
749 if ((edata->flags & ENVSYS_FNEED_REFRESH) != 0) { 749 if ((edata->flags & ENVSYS_FNEED_REFRESH) != 0) {
750 /* refresh sensor in device */ 750 /* refresh sensor in device */
751 sysmon_envsys_refresh_sensor(sme, edata); 751 sysmon_envsys_refresh_sensor(sme, edata);
752 edata->flags &= ~ENVSYS_FNEED_REFRESH; 752 edata->flags &= ~ENVSYS_FNEED_REFRESH;
753 } 753 }
754 754
755 DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d type=%d state=%d units=%d " 755 DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d type=%d state=%d units=%d "
756 "value_cur=%d upropset=%d\n", __func__, sme->sme_name, edata->desc, 756 "value_cur=%d upropset=%d\n", __func__, sme->sme_name, edata->desc,
757 edata->sensor, see->see_type, edata->state, edata->units, 757 edata->sensor, see->see_type, edata->state, edata->units,
758 edata->value_cur, edata->upropset)); 758 edata->value_cur, edata->upropset));
759 759
760 /* skip the event if current sensor is in invalid state */ 760 /* skip the event if current sensor is in invalid state */
761 if (edata->state == ENVSYS_SINVALID) 761 if (edata->state == ENVSYS_SINVALID)
762 goto out; 762 goto out;
763 763
764 /* 764 /*
765 * For range limits, if the driver claims responsibility for 765 * For range limits, if the driver claims responsibility for
766 * limit/range checking, just user driver-supplied status. 766 * limit/range checking, just user driver-supplied status.
767 * Else calculate our own status. Note that driver must 767 * Else calculate our own status. Note that driver must
768 * relinquish responsibility for ALL limits if there is even 768 * relinquish responsibility for ALL limits if there is even
769 * one limit that it cannot handle! 769 * one limit that it cannot handle!
770 * 770 *
771 * If this is a CAPACITY monitor, but the sensor's max_value 771 * If this is a CAPACITY monitor, but the sensor's max_value
772 * is not set, treat it as though the monitor does not exist. 772 * is not set, treat it as though the monitor does not exist.
773 */ 773 */
774 if ((see->see_type == PENVSYS_EVENT_LIMITS || 774 if ((see->see_type == PENVSYS_EVENT_LIMITS ||
775 see->see_type == PENVSYS_EVENT_CAPACITY) && 775 see->see_type == PENVSYS_EVENT_CAPACITY) &&
776 (edata->upropset & PROP_DRIVER_LIMITS) == 0) { 776 (edata->upropset & PROP_DRIVER_LIMITS) == 0) {
777 if ((see->see_type == PENVSYS_EVENT_CAPACITY) && 777 if ((see->see_type == PENVSYS_EVENT_CAPACITY) &&
778 (edata->value_max == 0)) 778 (edata->value_max == 0))
779 edata->state = ENVSYS_SVALID; 779 edata->state = ENVSYS_SVALID;
780 else if ((edata->upropset & (PROP_CRITMIN | PROP_BATTCAP)) && 780 else if ((edata->upropset & (PROP_CRITMIN | PROP_BATTCAP)) &&
781 (edata->value_cur < edata->limits.sel_critmin)) 781 (edata->value_cur < edata->limits.sel_critmin))
782 edata->state = ENVSYS_SCRITUNDER; 782 edata->state = ENVSYS_SCRITUNDER;
783 else if ((edata->upropset & (PROP_WARNMIN | PROP_BATTWARN)) && 783 else if ((edata->upropset & (PROP_WARNMIN | PROP_BATTWARN)) &&
784 (edata->value_cur < edata->limits.sel_warnmin)) 784 (edata->value_cur < edata->limits.sel_warnmin))
785 edata->state = ENVSYS_SWARNUNDER; 785 edata->state = ENVSYS_SWARNUNDER;
786 else if ((edata->upropset & (PROP_CRITMAX | PROP_BATTMAX)) && 786 else if ((edata->upropset & (PROP_CRITMAX | PROP_BATTMAX)) &&
787 (edata->value_cur > edata->limits.sel_critmax)) 787 (edata->value_cur > edata->limits.sel_critmax))
788 edata->state = ENVSYS_SCRITOVER; 788 edata->state = ENVSYS_SCRITOVER;
789 else if ((edata->upropset & (PROP_WARNMAX | PROP_BATTHIGH)) && 789 else if ((edata->upropset & (PROP_WARNMAX | PROP_BATTHIGH)) &&
790 (edata->value_cur > edata->limits.sel_warnmax)) 790 (edata->value_cur > edata->limits.sel_warnmax))
791 edata->state = ENVSYS_SWARNOVER; 791 edata->state = ENVSYS_SWARNOVER;
792 else 792 else
793 edata->state = ENVSYS_SVALID; 793 edata->state = ENVSYS_SVALID;
794 } 794 }
795 sme_deliver_event(see); 795 sme_deliver_event(see);
796 796
797out: 797out:
798 see->see_flags &= ~SEE_EVENT_WORKING; 798 see->see_flags &= ~SEE_EVENT_WORKING;
799 cv_broadcast(&sme->sme_condvar); 799 cv_broadcast(&sme->sme_condvar);
800 mutex_exit(&sme->sme_mtx); 800 mutex_exit(&sme->sme_mtx);
801} 801}
802 802
803/* 803/*
804 * sysmon_envsys_sensor_event 804 * sysmon_envsys_sensor_event
805 * 805 *
806 * + Find the monitor event of a particular type for a given sensor 806 * + Find the monitor event of a particular type for a given sensor
807 * on a device and deliver the event if one is required. If 807 * on a device and deliver the event if one is required. If
808 * no event type is specified, deliver all events for the sensor. 808 * no event type is specified, deliver all events for the sensor.
809 */ 809 */
810void 810void
811sysmon_envsys_sensor_event(struct sysmon_envsys *sme, envsys_data_t *edata,  811sysmon_envsys_sensor_event(struct sysmon_envsys *sme, envsys_data_t *edata,
812 int ev_type) 812 int ev_type)
813{ 813{
814 sme_event_t *see; 814 sme_event_t *see;
815 815
816 mutex_enter(&sme->sme_mtx); 816 mutex_enter(&sme->sme_mtx);
817 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 817 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
818 if (edata != see->see_edata) 818 if (edata != see->see_edata)
819 continue; 819 continue;
820 if (ev_type == 0 || 820 if (ev_type == 0 ||
821 ev_type == see->see_type) { 821 ev_type == see->see_type) {
822 sme_deliver_event(see); 822 sme_deliver_event(see);
823 if (ev_type != 0) 823 if (ev_type != 0)
824 break; 824 break;
825 } 825 }
826 } 826 }
827 mutex_exit(&sme->sme_mtx); 827 mutex_exit(&sme->sme_mtx);
828} 828}
829 829
830/* 830/*
831 * sme_deliver_event: 831 * sme_deliver_event:
832 * 832 *
833 * + If new sensor state requires it, send an event to powerd 833 * + If new sensor state requires it, send an event to powerd
834 * 834 *
835 * Must be called with the device's sysmon mutex held 835 * Must be called with the device's sysmon mutex held
836 * see->see_sme->sme_mtx 836 * see->see_sme->sme_mtx
837 */ 837 */
838void 838void
839sme_deliver_event(sme_event_t *see) 839sme_deliver_event(sme_event_t *see)
840{ 840{
841 envsys_data_t *edata = see->see_edata; 841 envsys_data_t *edata = see->see_edata;
842 const struct sme_descr_entry *sdt = NULL; 842 const struct sme_descr_entry *sdt = NULL;
843 const struct sme_sensor_event *sse = sme_sensor_event; 843 const struct sme_sensor_event *sse = sme_sensor_event;
844 int i, state = 0; 844 int i, state = 0;
845 845
846 switch (see->see_type) { 846 switch (see->see_type) {
847 case PENVSYS_EVENT_LIMITS: 847 case PENVSYS_EVENT_LIMITS:
848 case PENVSYS_EVENT_CAPACITY: 848 case PENVSYS_EVENT_CAPACITY:
849 /* 849 /*
850 * Send event if state has changed 850 * Send event if state has changed
851 */ 851 */
852 if (edata->state == see->see_evstate) 852 if (edata->state == see->see_evstate)
853 break; 853 break;
854 854
855 for (i = 0; sse[i].state != -1; i++) 855 for (i = 0; sse[i].state != -1; i++)
856 if (sse[i].state == edata->state) 856 if (sse[i].state == edata->state)
857 break; 857 break;
858 858
859 if (sse[i].state == -1) 859 if (sse[i].state == -1)
860 break; 860 break;
861 861
862 if (edata->state == ENVSYS_SVALID) 862 if (edata->state == ENVSYS_SVALID)
863 sysmon_penvsys_event(&see->see_pes, 863 sysmon_penvsys_event(&see->see_pes,
864 PENVSYS_EVENT_NORMAL); 864 PENVSYS_EVENT_NORMAL);
865 else 865 else
866 sysmon_penvsys_event(&see->see_pes, sse[i].event); 866 sysmon_penvsys_event(&see->see_pes, sse[i].event);
867 867
868 see->see_evstate = edata->state; 868 see->see_evstate = edata->state;
869 DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d state=%d send_ev=%d\n", 869 DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d state=%d send_ev=%d\n",
870 __func__, see->see_sme->sme_name, edata->desc, 870 __func__, see->see_sme->sme_name, edata->desc,
871 edata->sensor, edata->state, 871 edata->sensor, edata->state,
872 (edata->state == ENVSYS_SVALID) ? PENVSYS_EVENT_NORMAL : 872 (edata->state == ENVSYS_SVALID) ? PENVSYS_EVENT_NORMAL :
873 sse[i].event)); 873 sse[i].event));
874 874
875 break; 875 break;
876 876
877 /* 877 /*
878 * Send PENVSYS_EVENT_CRITICAL event if: 878 * Send PENVSYS_EVENT_CRITICAL event if:
879 * State has gone from non-CRITICAL to CRITICAL, 879 * State has gone from non-CRITICAL to CRITICAL,
880 * State remains CRITICAL and value has changed, or 880 * State remains CRITICAL and value has changed, or
881 * State has returned from CRITICAL to non-CRITICAL 881 * State has returned from CRITICAL to non-CRITICAL
882 */ 882 */
883 case PENVSYS_EVENT_CRITICAL: 883 case PENVSYS_EVENT_CRITICAL:
884 DPRINTF(("%s: CRITICAL: old/new state %d/%d, old/new value " 884 DPRINTF(("%s: CRITICAL: old/new state %d/%d, old/new value "
885 "%d/%d\n", __func__, see->see_evstate, edata->state, 885 "%d/%d\n", __func__, see->see_evstate, edata->state,
886 see->see_evvalue, edata->value_cur)); 886 see->see_evvalue, edata->value_cur));
887 if (edata->state == ENVSYS_SVALID && 887 if (edata->state == ENVSYS_SVALID &&
888 see->see_evstate != ENVSYS_SVALID) { 888 see->see_evstate != ENVSYS_SVALID) {
889 sysmon_penvsys_event(&see->see_pes, 889 sysmon_penvsys_event(&see->see_pes,
890 PENVSYS_EVENT_NORMAL); 890 PENVSYS_EVENT_NORMAL);
891 see->see_evstate = ENVSYS_SVALID; 891 see->see_evstate = ENVSYS_SVALID;
892 break; 892 break;
893 } else if (edata->state != ENVSYS_SCRITICAL) 893 } else if (edata->state != ENVSYS_SCRITICAL)
894 break; 894 break;
895 if (see->see_evstate != ENVSYS_SCRITICAL || 895 if (see->see_evstate != ENVSYS_SCRITICAL ||
896 see->see_evvalue != edata->value_cur) { 896 see->see_evvalue != edata->value_cur) {
897 sysmon_penvsys_event(&see->see_pes, 897 sysmon_penvsys_event(&see->see_pes,
898 PENVSYS_EVENT_CRITICAL); 898 PENVSYS_EVENT_CRITICAL);
899 see->see_evstate = ENVSYS_SCRITICAL; 899 see->see_evstate = ENVSYS_SCRITICAL;
900 } 900 }
901 see->see_evvalue = edata->value_cur; 901 see->see_evvalue = edata->value_cur;
902 break; 902 break;
903 903
904 /* 904 /*
905 * if value_cur is not normal (battery) or online (drive), 905 * if value_cur is not normal (battery) or online (drive),
906 * send the event... 906 * send the event...
907 */ 907 */
908 case PENVSYS_EVENT_STATE_CHANGED: 908 case PENVSYS_EVENT_STATE_CHANGED:
909 /*  909 /*
910 * the state has not been changed, just ignore the event. 910 * the state has not been changed, just ignore the event.
911 */ 911 */
912 if (edata->value_cur == see->see_evvalue) 912 if (edata->value_cur == see->see_evvalue)
913 break; 913 break;
914 914
915 switch (edata->units) { 915 switch (edata->units) {
916 case ENVSYS_DRIVE: 916 case ENVSYS_DRIVE:
917 sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES, 917 sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES,
918 edata->value_cur); 918 edata->value_cur);
919 state = ENVSYS_DRIVE_ONLINE; 919 state = ENVSYS_DRIVE_ONLINE;
920 break; 920 break;
921 case ENVSYS_BATTERY_CAPACITY: 921 case ENVSYS_BATTERY_CAPACITY:
922 sdt = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY, 922 sdt = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY,
923 edata->value_cur); 923 edata->value_cur);
924 state = ENVSYS_BATTERY_CAPACITY_NORMAL; 924 state = ENVSYS_BATTERY_CAPACITY_NORMAL;
925 break; 925 break;
926 case ENVSYS_INDICATOR: 926 case ENVSYS_INDICATOR:
927 sdt = sme_find_table_entry(SME_DESC_INDICATOR, 927 sdt = sme_find_table_entry(SME_DESC_INDICATOR,
928 edata->value_cur); 928 edata->value_cur);
929 state = see->see_evvalue; /* force state change */ 929 state = see->see_evvalue; /* force state change */
930 break; 930 break;
931 default: 931 default:
932 panic("%s: bad units for PENVSYS_EVENT_STATE_CHANGED", 932 panic("%s: bad units for PENVSYS_EVENT_STATE_CHANGED",
933 __func__); 933 __func__);
934 } 934 }
935 935
936 if (sdt->type == -1) 936 if (sdt->type == -1)
937 break; 937 break;
938 938
939 /*  939 /*
940 * copy current state description. 940 * copy current state description.
941 */ 941 */
942 (void)strlcpy(see->see_pes.pes_statedesc, sdt->desc, 942 (void)strlcpy(see->see_pes.pes_statedesc, sdt->desc,
943 sizeof(see->see_pes.pes_statedesc)); 943 sizeof(see->see_pes.pes_statedesc));
944 944
945 if (edata->value_cur == state) 945 if (edata->value_cur == state)
946 /* 946 /*
947 * state returned to normal condition 947 * state returned to normal condition
948 */ 948 */
949 sysmon_penvsys_event(&see->see_pes, 949 sysmon_penvsys_event(&see->see_pes,
950 PENVSYS_EVENT_NORMAL); 950 PENVSYS_EVENT_NORMAL);
951 else 951 else
952 /* 952 /*
953 * state changed to abnormal condition 953 * state changed to abnormal condition
954 */ 954 */
955 sysmon_penvsys_event(&see->see_pes, see->see_type); 955 sysmon_penvsys_event(&see->see_pes, see->see_type);
956 956
957 see->see_evvalue = edata->value_cur; 957 see->see_evvalue = edata->value_cur;
958 958
959 /*  959 /*
960 * There's no need to continue if it's a drive sensor. 960 * There's no need to continue if it's a drive sensor.
961 */ 961 */
962 if (edata->units == ENVSYS_DRIVE) 962 if (edata->units == ENVSYS_DRIVE)
963 break; 963 break;
964 964
965 /* 965 /*
966 * Check if the system is running in low power and send the 966 * Check if the system is running in low power and send the
967 * event to powerd (if running) or shutdown the system 967 * event to powerd (if running) or shutdown the system
968 * otherwise. 968 * otherwise.
969 */ 969 */
970 if (!sysmon_low_power && sme_event_check_low_power()) { 970 if (!sysmon_low_power && sme_event_check_low_power()) {
971 struct penvsys_state pes; 971 struct penvsys_state pes;
972 972
973 /* 973 /*
974 * Stop the callout and send the 'low-power' event. 974 * Stop the callout and send the 'low-power' event.
975 */ 975 */
976 sysmon_low_power = true; 976 sysmon_low_power = true;
977 callout_stop(&see->see_sme->sme_callout); 977 callout_stop(&see->see_sme->sme_callout);
978 pes.pes_type = PENVSYS_TYPE_BATTERY; 978 pes.pes_type = PENVSYS_TYPE_BATTERY;
979 sysmon_penvsys_event(&pes, PENVSYS_EVENT_LOW_POWER); 979 sysmon_penvsys_event(&pes, PENVSYS_EVENT_LOW_POWER);
980 } 980 }
981 break; 981 break;
982 case PENVSYS_EVENT_NULL: 982 case PENVSYS_EVENT_NULL:
983 break; 983 break;
984 default: 984 default:
985 panic("%s: invalid event type %d", __func__, see->see_type); 985 panic("%s: invalid event type %d", __func__, see->see_type);
986 } 986 }
987} 987}
988 988
989/* 989/*
990 * Returns true if the system is in low power state: an AC adapter 990 * Returns true if the system is in low power state: an AC adapter
991 * is OFF and all batteries are in LOW/CRITICAL state. 991 * is OFF and all batteries are in LOW/CRITICAL state.
992 */ 992 */
993static bool 993static bool
994sme_event_check_low_power(void) 994sme_event_check_low_power(void)
995{ 995{
996 if (!sme_acadapter_check()) 996 if (!sme_acadapter_check())
997 return false; 997 return false;
998 998
999 return sme_battery_check(); 999 return sme_battery_check();
1000} 1000}
1001 1001
1002/* 1002/*
1003 * Called with the sysmon_envsys device mtx held through the 1003 * Called with the sysmon_envsys device mtx held through the
1004 * workqueue thread. 1004 * workqueue thread.
1005 */ 1005 */
1006static bool 1006static bool
1007sme_acadapter_check(void) 1007sme_acadapter_check(void)
1008{ 1008{
1009 struct sysmon_envsys *sme; 1009 struct sysmon_envsys *sme;
1010 envsys_data_t *edata; 1010 envsys_data_t *edata;
1011 bool dev = false, sensor = false; 1011 bool dev = false, sensor = false;
1012 1012
1013 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 1013 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
1014 if (sme->sme_class == SME_CLASS_ACADAPTER) { 1014 if (sme->sme_class == SME_CLASS_ACADAPTER) {
1015 dev = true; 1015 dev = true;
1016 break; 1016 break;
1017 } 1017 }
1018 } 1018 }
1019 1019
1020 /* 1020 /*
1021 * No AC Adapter devices were found. 1021 * No AC Adapter devices were found.
1022 */ 1022 */
1023 if (!dev) 1023 if (!dev)
1024 return false; 1024 return false;
1025 1025
1026 /* 1026 /*
1027 * Check if there's an AC adapter device connected. 1027 * Check if there's an AC adapter device connected.
1028 */ 1028 */
1029 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 1029 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1030 if (edata->units == ENVSYS_INDICATOR) { 1030 if (edata->units == ENVSYS_INDICATOR) {
1031 sensor = true; 1031 sensor = true;
1032 /* refresh current sensor */ 1032 /* refresh current sensor */
1033 sysmon_envsys_refresh_sensor(sme, edata); 1033 sysmon_envsys_refresh_sensor(sme, edata);
1034 1034
1035 if (edata->value_cur) 1035 if (edata->value_cur)
1036 return false; 1036 return false;
1037 } 1037 }
1038 } 1038 }
1039 1039
1040 if (!sensor) 1040 if (!sensor)
1041 return false; 1041 return false;
1042 1042
1043 /*  1043 /*
1044 * AC adapter found and not connected. 1044 * AC adapter found and not connected.
1045 */ 1045 */
1046 return true; 1046 return true;
1047} 1047}
1048 1048
1049/* 1049/*
1050 * Called with the sysmon_envsys device mtx held through the 1050 * Called with the sysmon_envsys device mtx held through the
1051 * workqueue thread. 1051 * workqueue thread.
1052 */ 1052 */
1053static bool 1053static bool
1054sme_battery_check(void) 1054sme_battery_check(void)
1055{ 1055{
1056 struct sysmon_envsys *sme; 1056 struct sysmon_envsys *sme;
1057 envsys_data_t *edata; 1057 envsys_data_t *edata;
1058 int batteriesfound = 0; 1058 int batteriesfound = 0;
1059 bool present, batterycap, batterycharge; 1059 bool present, batterycap, batterycharge;
1060 1060
1061 /* 1061 /*
1062 * Check for battery devices and its state. 1062 * Check for battery devices and its state.
1063 */ 1063 */
1064 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 1064 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
1065 if (sme->sme_class != SME_CLASS_BATTERY) 1065 if (sme->sme_class != SME_CLASS_BATTERY)
1066 continue; 1066 continue;
1067 1067
1068 present = true; 1068 present = true;
1069 1069
1070 /* 1070 /*
1071 * XXX 1071 * XXX
1072 * this assumes that the first valid ENVSYS_INDICATOR is the 1072 * this assumes that the first valid ENVSYS_INDICATOR is the
1073 * presence indicator 1073 * presence indicator
1074 */ 1074 */
1075 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 1075 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1076 if ((edata->units == ENVSYS_INDICATOR) && 1076 if ((edata->units == ENVSYS_INDICATOR) &&
1077 (edata->state == ENVSYS_SVALID)) { 1077 (edata->state == ENVSYS_SVALID)) {
1078 present = edata->value_cur; 1078 present = edata->value_cur;
1079 break; 1079 break;
1080 } 1080 }
1081 } 1081 }
1082 if (!present) 1082 if (!present)
1083 continue; 1083 continue;
1084 /* 1084 /*
1085 * We've found a battery device... 1085 * We've found a battery device...
1086 */ 1086 */
1087 batteriesfound++; 1087 batteriesfound++;
1088 batterycap = batterycharge = false; 1088 batterycap = batterycharge = false;
1089 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 1089 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1090 /* no need to even look at sensors that aren't valid */ 1090 /* no need to even look at sensors that aren't valid */
1091 if (edata->state != ENVSYS_SVALID) 1091 if (edata->state != ENVSYS_SVALID)
1092 continue; 1092 continue;
1093 if (edata->units == ENVSYS_BATTERY_CAPACITY) { 1093 if (edata->units == ENVSYS_BATTERY_CAPACITY) {
1094 batterycap = true; 1094 batterycap = true;
1095 if (!sme_battery_critical(edata)) 1095 if (!sme_battery_critical(edata))
1096 return false; 1096 return false;
1097 } else if (edata->units == ENVSYS_BATTERY_CHARGE) { 1097 } else if (edata->units == ENVSYS_BATTERY_CHARGE) {
1098 batterycharge = true; 1098 batterycharge = true;
1099 if (edata->value_cur) 1099 if (edata->value_cur)
1100 return false; 1100 return false;
1101 } 1101 }
1102 } 1102 }
1103 if (!batterycap || !batterycharge) 1103 if (!batterycap || !batterycharge)
1104 return false; 1104 return false;
1105 } 1105 }
1106 1106
1107 if (!batteriesfound) 1107 if (!batteriesfound)
1108 return false; 1108 return false;
1109 1109
1110 /* 1110 /*
1111 * All batteries in low/critical capacity and discharging. 1111 * All batteries in low/critical capacity and discharging.
1112 */ 1112 */
1113 return true; 1113 return true;
1114} 1114}
1115 1115
1116static bool 1116static bool
1117sme_battery_critical(envsys_data_t *edata) 1117sme_battery_critical(envsys_data_t *edata)
1118{ 1118{
1119 if (edata->value_cur == ENVSYS_BATTERY_CAPACITY_CRITICAL || 1119 if (edata->value_cur == ENVSYS_BATTERY_CAPACITY_CRITICAL ||
1120 edata->value_cur == ENVSYS_BATTERY_CAPACITY_LOW) 1120 edata->value_cur == ENVSYS_BATTERY_CAPACITY_LOW)
1121 return true; 1121 return true;
1122 1122
1123 return false; 1123 return false;
1124} 1124}