xref: /netbsd-src/sys/dev/sysmon/sysmon_envsys.c (revision 0920b4f20b78ab1ccd9f2312fbe10deaf000cbf3)
1 /*	$NetBSD: sysmon_envsys.c,v 1.46 2007/08/05 23:16:25 xtraeme Exp $	*/
2 
3 /*-
4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Juan Romero Pardines.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by Juan Romero Pardines
21  *      for the NetBSD Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*-
40  * Copyright (c) 2000 Zembu Labs, Inc.
41  * All rights reserved.
42  *
43  * Author: Jason R. Thorpe <thorpej@zembu.com>
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. All advertising materials mentioning features or use of this software
54  *    must display the following acknowledgement:
55  *	This product includes software developed by Zembu Labs, Inc.
56  * 4. Neither the name of Zembu Labs nor the names of its employees may
57  *    be used to endorse or promote products derived from this software
58  *    without specific prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
61  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
62  * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
63  * CLAIMED.  IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
64  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
65  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
66  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
67  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
69  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70  */
71 
72 /*
73  * Environmental sensor framework for sysmon, exported to userland
74  * with proplib(3).
75  */
76 
77 #include <sys/cdefs.h>
78 __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.46 2007/08/05 23:16:25 xtraeme Exp $");
79 
80 #include <sys/param.h>
81 #include <sys/types.h>
82 #include <sys/conf.h>
83 #include <sys/errno.h>
84 #include <sys/fcntl.h>
85 #include <sys/kernel.h>
86 #include <sys/systm.h>
87 #include <sys/proc.h>
88 #include <sys/mutex.h>
89 #include <sys/kmem.h>
90 
91 #include <dev/sysmon/sysmonvar.h>
92 #include <dev/sysmon/sysmon_envsysvar.h>
93 #include <dev/sysmon/sysmon_taskq.h>
94 
95 #include "opt_compat_netbsd.h"
96 
97 struct sme_sensor_type {
98 	int		type;
99 	int		crittype;
100 	const char	*desc;
101 };
102 
103 static const struct sme_sensor_type sme_sensor_type[] = {
104 	{ ENVSYS_STEMP,		PENVSYS_TYPE_TEMP,	"Temperature" },
105 	{ ENVSYS_SFANRPM,	PENVSYS_TYPE_FAN,	"Fan" },
106 	{ ENVSYS_SVOLTS_AC,	PENVSYS_TYPE_VOLTAGE,	"Voltage AC" },
107 	{ ENVSYS_SVOLTS_DC,	PENVSYS_TYPE_VOLTAGE,	"Voltage DC" },
108 	{ ENVSYS_SOHMS,		PENVSYS_TYPE_RESISTANCE,"Ohms" },
109 	{ ENVSYS_SWATTS,	PENVSYS_TYPE_POWER,	"Watts" },
110 	{ ENVSYS_SAMPS,		PENVSYS_TYPE_POWER,	"Ampere" },
111 	{ ENVSYS_SWATTHOUR,	PENVSYS_TYPE_BATTERY,	"Watt hour" },
112 	{ ENVSYS_SAMPHOUR,	PENVSYS_TYPE_BATTERY,	"Ampere hour" },
113 	{ ENVSYS_INDICATOR,	PENVSYS_TYPE_INDICATOR,	"Indicator" },
114 	{ ENVSYS_INTEGER,	PENVSYS_TYPE_INDICATOR,	"Integer" },
115 	{ ENVSYS_DRIVE,		PENVSYS_TYPE_DRIVE,	"Drive" },
116 	{ -1,			-1,			"unknown" }
117 };
118 
119 struct sme_sensor_state {
120 	int		type;
121 	const char 	*desc;
122 };
123 
124 static const struct sme_sensor_state sme_sensor_state[] = {
125 	{ ENVSYS_SVALID,	"valid" },
126 	{ ENVSYS_SINVALID,	"invalid" },
127 	{ ENVSYS_SCRITICAL,	"critical" },
128 	{ ENVSYS_SCRITUNDER,	"critical-under" },
129 	{ ENVSYS_SCRITOVER,	"critical-over" },
130 	{ ENVSYS_SWARNUNDER,	"warning-under" },
131 	{ ENVSYS_SWARNOVER,	"warning-over" },
132 	{ -1,			"unknown" }
133 };
134 
135 static const struct sme_sensor_state sme_sensor_drive_state[] = {
136 	{ ENVSYS_DRIVE_EMPTY,		"drive state is unknown" },
137 	{ ENVSYS_DRIVE_READY,		"drive is ready" },
138 	{ ENVSYS_DRIVE_POWERUP,		"drive is powering up" },
139 	{ ENVSYS_DRIVE_ONLINE,		"drive is online" },
140 	{ ENVSYS_DRIVE_IDLE,		"drive is idle" },
141 	{ ENVSYS_DRIVE_ACTIVE,		"drive is active" },
142 	{ ENVSYS_DRIVE_REBUILD,		"drive is rebuilding" },
143 	{ ENVSYS_DRIVE_POWERDOWN,	"drive is powering down" },
144 	{ ENVSYS_DRIVE_FAIL,		"drive failed" },
145 	{ ENVSYS_DRIVE_PFAIL,		"drive degraded" },
146 	{ -1,				"unknown" }
147 };
148 
149 struct sme_sensor_names {
150 	SLIST_ENTRY(sme_sensor_names) sme_names;
151 	int	assigned;
152 	char	desc[ENVSYS_DESCLEN];
153 };
154 
155 static SLIST_HEAD(, sme_sensor_names) sme_names_list;
156 static prop_dictionary_t sme_propd;
157 static kcondvar_t sme_list_cv;
158 static int sme_uniqsensors = 0;
159 
160 kmutex_t sme_mtx, sme_list_mtx, sme_event_mtx;
161 kcondvar_t sme_event_cv;
162 
163 #ifdef COMPAT_40
164 static u_int sysmon_envsys_next_sensor_index = 0;
165 static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
166 #endif
167 
168 static void sysmon_envsys_release(struct sysmon_envsys *);
169 static int sme_register_sensorname(envsys_data_t *);
170 
171 /*
172  * sysmon_envsys_init:
173  *
174  * 	+ Initialize global mutexes, dictionary and the linked lists.
175  */
176 void
177 sysmon_envsys_init(void)
178 {
179 	LIST_INIT(&sysmon_envsys_list);
180 	LIST_INIT(&sme_events_list);
181 	mutex_init(&sme_mtx, MUTEX_DRIVER, IPL_NONE);
182 	mutex_init(&sme_list_mtx, MUTEX_DRIVER, IPL_NONE);
183 	mutex_init(&sme_event_mtx, MUTEX_DRIVER, IPL_NONE);
184 	mutex_init(&sme_event_init_mtx, MUTEX_DRIVER, IPL_NONE);
185 	cv_init(&sme_list_cv, "smefind");
186 	cv_init(&sme_event_cv, "smeevent");
187 	sme_propd = prop_dictionary_create();
188 }
189 
190 /*
191  * sysmonopen_envsys:
192  *
193  *	+ Open the system monitor device.
194  */
195 int
196 sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
197 {
198 	return 0;
199 }
200 
201 /*
202  * sysmonclose_envsys:
203  *
204  *	+ Close the system monitor device.
205  */
206 int
207 sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
208 {
209 	/* Nothing to do */
210 	return 0;
211 }
212 
213 /*
214  * sysmonioctl_envsys:
215  *
216  *	+ Perform an envsys control request.
217  */
218 int
219 sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
220 {
221 	struct sysmon_envsys *sme = NULL;
222 	int error = 0;
223 #ifdef COMPAT_40
224 	u_int oidx;
225 #endif
226 
227 	switch (cmd) {
228 	case ENVSYS_GETDICTIONARY:
229 	    {
230 		struct plistref *plist = (struct plistref *)data;
231 
232 		/*
233 		 * Update all sysmon envsys devices dictionaries with
234 		 * new data if it's different than we have currently
235 		 * in the dictionary.
236 		 */
237 		mutex_enter(&sme_list_mtx);
238 		LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
239 			if (sme == NULL)
240 				continue;
241 
242 			mutex_enter(&sme_mtx);
243 			error = sme_update_dictionary(sme);
244 			if (error) {
245 				DPRINTF(("%s: sme_update_dictionary, "
246 				    "error=%d\n", __func__, error));
247 				mutex_exit(&sme_list_mtx);
248 				mutex_exit(&sme_mtx);
249 				return error;
250 			}
251 			mutex_exit(&sme_mtx);
252 		}
253 		mutex_exit(&sme_list_mtx);
254 		/*
255 		 * Copy global dictionary to userland.
256 		 */
257 		error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
258 		break;
259 	    }
260 	case ENVSYS_SETDICTIONARY:
261 	    {
262 		const struct plistref *plist = (const struct plistref *)data;
263 		prop_dictionary_t udict;
264 		prop_object_t obj;
265 		const char *devname = NULL;
266 
267 		if ((flag & FWRITE) == 0)
268 			return EPERM;
269 
270 		/*
271 		 * Get dictionary from userland.
272 		 */
273 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
274 		if (error) {
275 			DPRINTF(("%s: copyin_ioctl error=%d\n",
276 			    __func__, error));
277 			break;
278 		}
279 
280 		/*
281 		 * Parse "driver-name" key to obtain the driver we
282 		 * are searching for.
283 		 */
284 		obj = prop_dictionary_get(udict, "driver-name");
285 		if (obj == NULL || prop_object_type(obj) != PROP_TYPE_STRING) {
286 			DPRINTF(("%s: driver-name failed\n", __func__));
287 			prop_object_release(udict);
288 			error = EINVAL;
289 			break;
290 		}
291 
292 		/* driver name */
293 		devname = prop_string_cstring_nocopy(obj);
294 
295 		/* find the correct sme device */
296 		sme = sysmon_envsys_find(devname);
297 		if (sme == NULL) {
298 			DPRINTF(("%s: NULL sme\n", __func__));
299 			prop_object_release(udict);
300 			error = EINVAL;
301 			break;
302 		}
303 
304 		/*
305 		 * Find the correct array object with the string supplied
306 		 * by the userland dictionary.
307 		 */
308 		obj = prop_dictionary_get(sme_propd, devname);
309 		if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
310 			DPRINTF(("%s: array device failed\n", __func__));
311 			sysmon_envsys_release(sme);
312 			prop_object_release(udict);
313 			error = EINVAL;
314 			break;
315 		}
316 
317 		/* do the real work now */
318 		error = sme_userset_dictionary(sme, udict, obj);
319 		sysmon_envsys_release(sme);
320 		break;
321 	    }
322 	    /*
323 	     * Compatibility functions with the old interface, only
324 	     * implemented ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough
325 	     * to make old applications work.
326 	     */
327 #ifdef COMPAT_40
328 	case ENVSYS_GTREDATA:
329 	    {
330 		struct envsys_tre_data *tred = (void *)data;
331 		envsys_data_t *edata = NULL;
332 
333 		tred->validflags = 0;
334 
335 		sme = sysmon_envsys_find_40(tred->sensor);
336 		if (sme == NULL)
337 			break;
338 
339 		mutex_enter(&sme_mtx);
340 		oidx = tred->sensor;
341 		tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
342 
343 		DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
344 		    __func__, tred->sensor, oidx, sme->sme_name,
345 		    sme->sme_nsensors));
346 
347 		edata = &sme->sme_sensor_data[tred->sensor];
348 
349 		if (tred->sensor < sme->sme_nsensors) {
350 			if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
351 				error = (*sme->sme_gtredata)(sme, edata);
352 				if (error) {
353 					DPRINTF(("%s: sme_gtredata failed\n",
354 				    	    __func__));
355 					mutex_exit(&sme_mtx);
356 					return error;
357 				}
358 			}
359 
360 			/* copy required values to the old interface */
361 			tred->sensor = edata->sensor;
362 			tred->cur.data_us = edata->value_cur;
363 			tred->cur.data_s = edata->value_cur;
364 			tred->max.data_us = edata->value_max;
365 			tred->max.data_s = edata->value_max;
366 			tred->min.data_us = edata->value_min;
367 			tred->min.data_s = edata->value_min;
368 			tred->avg.data_us = edata->value_avg;
369 			tred->avg.data_s = edata->value_avg;
370 			tred->units = edata->units;
371 
372 			tred->validflags |= ENVSYS_FVALID;
373 			tred->validflags |= ENVSYS_FCURVALID;
374 
375 			if (edata->flags & ENVSYS_FPERCENT) {
376 				tred->validflags |= ENVSYS_FMAXVALID;
377 				tred->validflags |= ENVSYS_FFRACVALID;
378 			}
379 
380 			if (edata->state == ENVSYS_SINVALID ||
381 			    edata->flags & ENVSYS_FNOTVALID) {
382 				tred->validflags &= ~ENVSYS_FCURVALID;
383 				tred->cur.data_us = tred->cur.data_s = 0;
384 			}
385 
386 			DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
387 			    __func__, edata->desc, tred->cur.data_s));
388 			DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
389 			    " tred->sensor=%d\n", __func__, tred->validflags,
390 			    tred->units, tred->sensor));
391 		}
392 		tred->sensor = oidx;
393 		mutex_exit(&sme_mtx);
394 
395 		break;
396 	    }
397 	case ENVSYS_GTREINFO:
398 	    {
399 		struct envsys_basic_info *binfo = (void *)data;
400 		envsys_data_t *edata = NULL;
401 
402 		binfo->validflags = 0;
403 
404 		sme = sysmon_envsys_find_40(binfo->sensor);
405 		if (sme == NULL)
406 			break;
407 
408 		mutex_enter(&sme_mtx);
409 		oidx = binfo->sensor;
410 		binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
411 
412 		edata = &sme->sme_sensor_data[binfo->sensor];
413 
414 		binfo->validflags |= ENVSYS_FVALID;
415 
416 		if (binfo->sensor < sme->sme_nsensors) {
417 			binfo->units = edata->units;
418 			(void)strlcpy(binfo->desc, edata->desc,
419 			    sizeof(binfo->desc));
420 		}
421 
422 		DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
423 		    __func__, binfo->units, binfo->validflags));
424 		DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
425 		    __func__, binfo->desc, binfo->sensor));
426 
427 		binfo->sensor = oidx;
428 		mutex_exit(&sme_mtx);
429 
430 		break;
431 	    }
432 #endif /* COMPAT_40 */
433 	default:
434 		error = ENOTTY;
435 		break;
436 	}
437 
438 	return error;
439 }
440 
441 /*
442  * sysmon_envsys_register:
443  *
444  *	+ Register an envsys device.
445  *	+ Create device dictionary.
446  */
447 int
448 sysmon_envsys_register(struct sysmon_envsys *sme)
449 {
450 	struct sysmon_envsys *lsme;
451 	int error = 0;
452 
453 	/*
454 	 * sanity check 1, make sure the driver has initialized
455 	 * the sensors count or the value is not too high.
456 	 */
457 	if (!sme->sme_nsensors || sme->sme_nsensors > ENVSYS_MAXSENSORS)
458 		return EINVAL;
459 
460 	/*
461 	 * sanity check 2, make sure that sme->sme_name and
462 	 * sme->sme_data are not NULL.
463 	 */
464 	if (sme->sme_name == NULL || sme->sme_sensor_data == NULL)
465 		return EINVAL;
466 
467 	/*
468 	 * sanity check 3, if SME_DISABLE_GTREDATA is not set
469 	 * the sme_gtredata function callback must be non NULL.
470 	 */
471 	if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
472 		if (sme->sme_gtredata == NULL)
473 			return EINVAL;
474 	}
475 
476 	/*
477 	 * - check if requested sysmon_envsys device is valid
478 	 *   and does not exist already in the list.
479 	 * - add device into the list.
480 	 * - create the plist structure.
481 	 */
482 	mutex_enter(&sme_list_mtx);
483 	LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
484 	       if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
485 		       error = EEXIST;
486 		       goto out;
487 	       }
488 	}
489 
490 	/* initialize the singly linked list for sensor descriptions */
491 	SLIST_INIT(&sme_names_list);
492 
493 #ifdef COMPAT_40
494 	sme->sme_fsensor = sysmon_envsys_next_sensor_index;
495 	sysmon_envsys_next_sensor_index += sme->sme_nsensors;
496 #endif
497 	error = sysmon_envsys_createplist(sme);
498 	if (!error) {
499 		LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
500 		sme_uniqsensors = 0;
501 	}
502 
503 out:
504 	mutex_exit(&sme_list_mtx);
505 	return error;
506 }
507 
508 /*
509  * sysmon_envsys_unregister:
510  *
511  *	+ Unregister an envsys device.
512  *	+ Unregister all events associated with this device.
513  *	+ Remove device dictionary.
514  */
515 void
516 sysmon_envsys_unregister(struct sysmon_envsys *sme)
517 {
518 	struct sme_sensor_names *snames;
519 
520 	KASSERT(sme != NULL);
521 
522 	mutex_enter(&sme_list_mtx);
523 	while (sme->sme_flags & SME_FLAG_BUSY) {
524 		sme->sme_flags |= SME_FLAG_WANTED;
525 		cv_wait(&sme_list_cv, &sme_list_mtx);
526 	}
527 	prop_dictionary_remove(sme_propd, sme->sme_name);
528 	/*
529 	 * Unregister all events associated with this device.
530 	 */
531 	mutex_exit(&sme_list_mtx);
532 	sme_event_unregister_all(sme);
533 	mutex_enter(&sme_list_mtx);
534 
535 	/*
536 	 * Remove all elements from the sensor names singly linked list.
537 	 */
538 	while (!SLIST_EMPTY(&sme_names_list)) {
539 		snames = SLIST_FIRST(&sme_names_list);
540 		SLIST_REMOVE_HEAD(&sme_names_list, sme_names);
541 		mutex_exit(&sme_list_mtx);
542 		kmem_free(snames, sizeof(*snames));
543 		mutex_enter(&sme_list_mtx);
544 	}
545 	LIST_REMOVE(sme, sme_list);
546 	mutex_exit(&sme_list_mtx);
547 }
548 
549 /*
550  * sysmon_envsys_find:
551  *
552  *	+ Find an envsys device.
553  */
554 struct sysmon_envsys *
555 sysmon_envsys_find(const char *name)
556 {
557 	struct sysmon_envsys *sme;
558 
559 	mutex_enter(&sme_list_mtx);
560 again:
561 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
562 	     sme = LIST_NEXT(sme, sme_list)) {
563 			if (strcmp(sme->sme_name, name) == 0) {
564 				if (sme->sme_flags & SME_FLAG_BUSY) {
565 					sme->sme_flags |= SME_FLAG_WANTED;
566 					cv_wait(&sme_list_cv, &sme_list_mtx);
567 					goto again;
568 				}
569 				sme->sme_flags |= SME_FLAG_BUSY;
570 				break;
571 			}
572 	}
573 	mutex_exit(&sme_list_mtx);
574 	return sme;
575 }
576 
577 /*
578  * sysmon_envsys_release:
579  *
580  * 	+ Release an envsys device.
581  */
582 void
583 sysmon_envsys_release(struct sysmon_envsys *sme)
584 {
585 	mutex_enter(&sme_list_mtx);
586 	if (sme->sme_flags & SME_FLAG_WANTED)
587 		cv_broadcast(&sme_list_cv);
588 	sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
589 	mutex_exit(&sme_list_mtx);
590 }
591 
592 /* compatibility function */
593 #ifdef COMPAT_40
594 struct sysmon_envsys *
595 sysmon_envsys_find_40(u_int idx)
596 {
597 	struct sysmon_envsys *sme;
598 
599 	mutex_enter(&sme_list_mtx);
600 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
601 	     sme = LIST_NEXT(sme, sme_list)) {
602 		if (idx >= sme->sme_fsensor &&
603 	    	    idx < (sme->sme_fsensor + sme->sme_nsensors))
604 			break;
605 	}
606 	mutex_exit(&sme_list_mtx);
607 	return sme;
608 }
609 #endif
610 
611 /*
612  * sysmon_envsys_createplist:
613  *
614  * 	+ Create the property list structure for a device.
615  */
616 int
617 sysmon_envsys_createplist(struct sysmon_envsys *sme)
618 {
619 	envsys_data_t *edata;
620 	prop_array_t array;
621 	int i, nsens, error;
622 
623 	nsens = error = 0;
624 
625 	KASSERT(mutex_owned(&sme_list_mtx));
626 	mutex_exit(&sme_list_mtx);
627 
628 	/* create the sysmon envsys device array. */
629 	array = prop_array_create();
630 	if (array == NULL)
631 		return EINVAL;
632 
633 	/*
634 	 * Iterate over all sensors and create a dictionary with all
635 	 * values specified by the sysmon envsys driver.
636 	 */
637 	for (i = 0; i < sme->sme_nsensors; i++) {
638 		/*
639 		 * XXX:
640 		 *
641 		 * workaround for LKMs. First sensor used in a LKM,
642 		 * gets the index in sensor from all edata structures
643 		 * allocated in kernel. It is unknown to me why this
644 		 * value is not 0 when using a LKM.
645 		 *
646 		 * For now, overwrite its index to 0.
647 		 */
648 		if (i == 0)
649 			sme->sme_sensor_data[0].sensor = 0;
650 
651 		edata = &sme->sme_sensor_data[i];
652 		sme_make_dictionary(sme, array, edata);
653 	}
654 
655 	if (prop_array_count(array) == 0) {
656 		error = EINVAL;
657 		prop_object_release(array);
658 		return error;
659 	}
660 
661 	/*
662 	 * <dict>
663 	 * 	<key>foo0</key>
664 	 * 	<array>
665 	 * 		...
666 	 */
667 	mutex_enter(&sme_mtx);
668 	if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
669 		DPRINTF(("%s: prop_dictionary_set\n", __func__));
670 		error = EINVAL;
671 	}
672 	mutex_exit(&sme_mtx);
673 
674 
675 	prop_object_release(array);
676 	mutex_enter(&sme_list_mtx);
677 	return error;
678 }
679 
680 /*
681  * sme_register_sensorname:
682  *
683  * 	+ Registers a sensor name into the list maintained per device.
684  */
685 static int
686 sme_register_sensorname(envsys_data_t *edata)
687 {
688 	struct sme_sensor_names *snames, *snames2 = NULL;
689 
690 	KASSERT(edata != NULL);
691 
692 	snames = kmem_zalloc(sizeof(*snames), KM_SLEEP);
693 
694 	mutex_enter(&sme_list_mtx);
695 	SLIST_FOREACH(snames2, &sme_names_list, sme_names) {
696 		/*
697 		 * Match sensors with empty and duplicate description.
698 		 */
699 		if (strlen(edata->desc) == 0 ||
700 		    strcmp(snames2->desc, edata->desc) == 0)
701 			if (snames2->assigned) {
702 				edata->flags |= ENVSYS_FNOTVALID;
703 				mutex_exit(&sme_list_mtx);
704 				DPRINTF(("%s: duplicate name "
705 				    "(%s)\n", __func__, edata->desc));
706 				kmem_free(snames, sizeof(*snames));
707 				return EEXIST;
708 			}
709 	}
710 
711 	snames->assigned = true;
712 	(void)strlcpy(snames->desc, edata->desc, sizeof(snames->desc));
713 	DPRINTF(("%s: registering sensor name=%s\n", __func__, edata->desc));
714 	SLIST_INSERT_HEAD(&sme_names_list, snames, sme_names);
715 	sme_uniqsensors++;
716 	mutex_exit(&sme_list_mtx);
717 
718 	return 0;
719 }
720 
721 /*
722  * sme_make_dictionary:
723  *
724  * 	+ Create sensor's dictionary in device's array.
725  */
726 void
727 sme_make_dictionary(struct sysmon_envsys *sme, prop_array_t array,
728 		    envsys_data_t *edata)
729 {
730 	const struct sme_sensor_type *est = sme_sensor_type;
731 	const struct sme_sensor_state *ess = sme_sensor_state;
732 	const struct sme_sensor_state *esds = sme_sensor_drive_state;
733 	sme_event_drv_t *sme_evdrv_t = NULL;
734 	prop_dictionary_t dict;
735 	int i, j;
736 
737 	i = j = 0;
738 
739 
740 	/*
741 	 * <array>
742 	 * 	<dict>
743 	 * 		...
744 	 */
745 	dict = prop_dictionary_create();
746 	if (dict == NULL) {
747 		DPRINTF(("%s: couldn't allocate a dictionary\n", __func__));
748 		goto invalidate_sensor;
749 	}
750 
751 	/* find the correct unit for this sensor. */
752 	for (i = 0; est[i].type != -1; i++)
753 		if (est[i].type == edata->units)
754 			break;
755 
756 	if (strcmp(est[i].desc, "unknown") == 0) {
757 		DPRINTF(("%s: invalid units type for sensor=%d\n",
758 		    __func__, edata->sensor));
759 		goto invalidate_sensor;
760 	}
761 
762 	/*
763 	 * 		...
764 	 * 		<key>type</key>
765 	 * 		<string>foo</string>
766 	 * 		<key>description</key>
767 	 * 		<string>blah blah</string>
768 	 * 		...
769 	 */
770 	if (sme_sensor_upstring(dict, "type", est[i].desc))
771 		goto invalidate_sensor;
772 
773 	if (strlen(edata->desc) == 0) {
774 		DPRINTF(("%s: invalid description for sensor=%d\n",
775 		    __func__, edata->sensor));
776 		goto invalidate_sensor;
777 	}
778 
779 	/*
780 	 * Check if description was already assigned in other sensor.
781 	 */
782 	if (sme_register_sensorname(edata))
783 		goto out;
784 
785 	if (sme_sensor_upstring(dict, "description", edata->desc))
786 		goto invalidate_sensor;
787 
788 	/*
789 	 * Add sensor's state description.
790 	 *
791 	 * 		...
792 	 * 		<key>state</key>
793 	 * 		<string>valid</string>
794 	 * 		...
795 	 */
796 	for (j = 0; ess[j].type != -1; j++)
797 		if (ess[j].type == edata->state)
798 			break;
799 
800 	if (strcmp(ess[j].desc, "unknown") == 0) {
801 		DPRINTF(("%s: invalid state for sensor=%d\n",
802 		    __func__, edata->sensor));
803 		goto invalidate_sensor;
804 	}
805 
806 	DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
807 	    __func__, edata->desc, edata->units, edata->state));
808 
809 	if (sme_sensor_upstring(dict, "state", ess[j].desc))
810 		goto invalidate_sensor;
811 
812 	/*
813 	 * add the percentage boolean object:
814 	 *
815 	 * 		...
816 	 * 		<key>want-percentage</key>
817 	 * 		<true/>
818 	 * 		...
819 	 */
820 	if (edata->flags & ENVSYS_FPERCENT)
821 		if (sme_sensor_upbool(dict, "want-percentage", true))
822 			goto out;
823 
824 	/*
825 	 * Add the monitoring boolean object:
826 	 *
827 	 * 		...
828 	 * 		<key>monitoring-supported</key>
829 	 * 		<true/>
830 	 *		...
831 	 *
832 	 * always false on Drive and Indicator types, they
833 	 * cannot be monitored.
834 	 *
835 	 */
836 	if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
837 	    (edata->units == ENVSYS_INDICATOR) ||
838 	    (edata->units == ENVSYS_DRIVE)) {
839 		if (sme_sensor_upbool(dict, "monitoring-supported", false))
840 			goto out;
841 	} else {
842 		if (sme_sensor_upbool(dict, "monitoring-supported", true))
843 			goto out;
844 	}
845 
846 	/*
847 	 * Add the drive-state object for drive sensors:
848 	 *
849 	 * 		...
850 	 * 		<key>drive-state</key>
851 	 * 		<string>drive is online</string>
852 	 * 		...
853 	 */
854 	if (edata->units == ENVSYS_DRIVE) {
855 		for (j = 0; esds[j].type != -1; j++)
856 			if (esds[j].type == edata->value_cur)
857 				break;
858 
859 		if (sme_sensor_upstring(dict, "drive-state", esds[j].desc))
860 			goto out;
861 	}
862 
863 	/* if sensor is enabled, add the following properties... */
864 	if (edata->state == ENVSYS_SVALID) {
865 		/*
866 		 * 	...
867 		 * 	<key>rpms</key>
868 		 * 	<integer>2500</integer>
869 		 * 	<key>rfact</key>
870 		 * 	<integer>10000</integer>
871 		 * 	<key>cur-value</key>
872 		 * 	<integer>1250</integer>
873 		 * 	<key>min-value</key>
874 		 * 	<integer>800</integer>
875 		 * 	<key>max-value</integer>
876 		 * 	<integer>3000</integer>
877 		 * 	<key>avg-value</integer>
878 		 * 	<integer>1400</integer>
879 		 * </dict>
880 		 */
881 		if (edata->units == ENVSYS_SFANRPM)
882 			if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
883 				goto out;
884 
885 		if (edata->units == ENVSYS_SVOLTS_AC ||
886 		    edata->units == ENVSYS_SVOLTS_DC)
887 			if (sme_sensor_upint32(dict, "rfact", edata->rfact))
888 				goto out;
889 
890 		if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
891 			goto out;
892 
893 		if (edata->flags & ENVSYS_FVALID_MIN) {
894 			if (sme_sensor_upint32(dict,
895 					       "min-value",
896 					       edata->value_min))
897 				goto out;
898 		}
899 
900 		if (edata->flags & ENVSYS_FVALID_MAX) {
901 			if (sme_sensor_upint32(dict,
902 					       "max-value",
903 					       edata->value_max))
904 				goto out;
905 		}
906 
907 		if (edata->flags & ENVSYS_FVALID_AVG) {
908 			if (sme_sensor_upint32(dict,
909 					       "avg-value",
910 					        edata->value_avg))
911 				goto out;
912 		}
913 	}
914 
915 	/*
916 	 * 	...
917 	 * </array>
918 	 */
919 
920 	mutex_enter(&sme_mtx);
921 	if (!prop_array_set(array, sme_uniqsensors - 1, dict)) {
922 		mutex_exit(&sme_mtx);
923 		DPRINTF(("%s: prop_array_add\n", __func__));
924 		goto invalidate_sensor;
925 	}
926 	mutex_exit(&sme_mtx);
927 
928 	/*
929 	 * Add a new event if a monitoring flag was set.
930 	 */
931 	if (edata->monitor) {
932 		sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
933 
934 		sme_evdrv_t->sdict = dict;
935 		sme_evdrv_t->edata = edata;
936 		sme_evdrv_t->sme = sme;
937 		sme_evdrv_t->powertype = est[i].crittype;
938 
939 		sysmon_task_queue_init();
940 		sysmon_task_queue_sched(0, sme_event_drvadd, sme_evdrv_t);
941 	}
942 
943 out:
944 	prop_object_release(dict);
945 	return;
946 
947 invalidate_sensor:
948 	mutex_enter(&sme_mtx);
949 	edata->flags |= ENVSYS_FNOTVALID;
950 	mutex_exit(&sme_mtx);
951 	prop_object_release(dict);
952 }
953 
954 /*
955  * sme_update_dictionary:
956  *
957  * 	+ Update per-sensor dictionaries with new values if there were
958  * 	  changes, otherwise the object in dictionary is untouched.
959  * 	+ Send a critical event if any sensor is in a critical condition.
960  */
961 int
962 sme_update_dictionary(struct sysmon_envsys *sme)
963 {
964 	const struct sme_sensor_type *est = sme_sensor_type;
965 	const struct sme_sensor_state *ess = sme_sensor_state;
966 	const struct sme_sensor_state *esds = sme_sensor_drive_state;
967 	envsys_data_t *edata = NULL;
968 	prop_object_t array, dict;
969 	int i, j, error, invalid;
970 
971 	error = invalid = 0;
972 	array = dict = NULL;
973 
974 	/* retrieve the array of dictionaries in device. */
975 	array = prop_dictionary_get(sme_propd, sme->sme_name);
976 	if (prop_object_type(array) != PROP_TYPE_ARRAY) {
977 		DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
978 		return EINVAL;
979 	}
980 
981 	/*
982 	 * - iterate over all sensors.
983 	 * - fetch new data.
984 	 * - check if data in dictionary is different than new data.
985 	 * - update dictionary if there were changes.
986 	 */
987 	for (i = 0; i < sme->sme_nsensors; i++) {
988 		edata = &sme->sme_sensor_data[i];
989 		/* skip invalid sensors */
990 		if (edata->flags & ENVSYS_FNOTVALID) {
991 			invalid++;
992 			continue;
993 		}
994 
995 		/*
996 		 * refresh sensor data via sme_gtredata only if the
997 		 * flag is not set.
998 		 */
999 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
1000 			error = (*sme->sme_gtredata)(sme, edata);
1001 			if (error) {
1002 				DPRINTF(("%s: gtredata[%d] failed\n",
1003 				    __func__, i));
1004 				return error;
1005 			}
1006 		}
1007 
1008 		/* retrieve sensor's dictionary. */
1009 		dict = prop_array_get(array, i - invalid);
1010 		if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
1011 			DPRINTF(("%s: not a dictionary (%d:%s)\n",
1012 			    __func__, edata->sensor, sme->sme_name));
1013 			return EINVAL;
1014 		}
1015 
1016 		for (j = 0; ess[j].type != -1; j++)
1017 			if (ess[j].type == edata->state)
1018 				break;
1019 
1020 		DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
1021 		    "units=%d sensor=%d\n", __func__, ess[j].desc,
1022 		    ess[j].type, edata->flags, edata->units, edata->sensor));
1023 
1024 		/* update sensor state */
1025 		error = sme_sensor_upstring(dict, "state", ess[j].desc);
1026 		if (error)
1027 			break;
1028 
1029 		for (j = 0; est[j].type != -1; j++)
1030 			if (est[j].type == edata->units)
1031 				break;
1032 
1033 		/* update sensor type */
1034 		error = sme_sensor_upstring(dict, "type", est[j].desc);
1035 		if (error)
1036 			break;
1037 
1038 		/* update sensor current value */
1039 		error = sme_sensor_upint32(dict,
1040 					   "cur-value",
1041 					   edata->value_cur);
1042 		if (error)
1043 			break;
1044 
1045 		/*
1046 		 * Integer and Indicator types do not the following
1047 		 * values, so skip them.
1048 		 */
1049 		if (edata->units == ENVSYS_INTEGER ||
1050 		    edata->units == ENVSYS_INDICATOR)
1051 			continue;
1052 
1053 		/* update sensor flags */
1054 		if (edata->flags & ENVSYS_FPERCENT) {
1055 			error = sme_sensor_upbool(dict,
1056 						  "want-percentage",
1057 						  true);
1058 			if (error)
1059 				break;
1060 		}
1061 
1062 		if (edata->flags & ENVSYS_FVALID_MAX) {
1063 			error = sme_sensor_upint32(dict,
1064 						   "max-value",
1065 						   edata->value_max);
1066 			if (error)
1067 				break;
1068 		}
1069 
1070 		if (edata->flags & ENVSYS_FVALID_MIN) {
1071 			error = sme_sensor_upint32(dict,
1072 						   "min-value",
1073 						   edata->value_min);
1074 			if (error)
1075 				break;
1076 		}
1077 
1078 		if (edata->flags & ENVSYS_FVALID_AVG) {
1079 			error = sme_sensor_upint32(dict,
1080 						   "avg-value",
1081 						   edata->value_avg);
1082 			if (error)
1083 				break;
1084 		}
1085 
1086 		/* update 'rpms' only in ENVSYS_SFANRPM. */
1087 		if (edata->units == ENVSYS_SFANRPM) {
1088 			error = sme_sensor_upuint32(dict,
1089 						    "rpms",
1090 						    edata->rpms);
1091 			if (error)
1092 				break;
1093 		}
1094 
1095 		/* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
1096 		if (edata->units == ENVSYS_SVOLTS_AC ||
1097 		    edata->units == ENVSYS_SVOLTS_DC) {
1098 			error = sme_sensor_upint32(dict,
1099 						   "rfact",
1100 						   edata->rfact);
1101 			if (error)
1102 				break;
1103 		}
1104 
1105 		/* update 'drive-state' only in ENVSYS_DRIVE. */
1106 		if (edata->units == ENVSYS_DRIVE) {
1107 			for (j = 0; esds[j].type != -1; j++)
1108 				if (esds[j].type == edata->value_cur)
1109 					break;
1110 
1111 			error = sme_sensor_upstring(dict,
1112 						    "drive-state",
1113 						    esds[j].desc);
1114 			if (error)
1115 				break;
1116 		}
1117 	}
1118 
1119 	return error;
1120 }
1121 
1122 /*
1123  * sme_userset_dictionary:
1124  *
1125  * 	+ Parses the userland dictionary and run the appropiate
1126  * 	  tasks that were requested.
1127  */
1128 int
1129 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
1130 		       prop_array_t array)
1131 {
1132 	const struct sme_sensor_type *sst = sme_sensor_type;
1133 	envsys_data_t *edata, *nedata;
1134 	prop_dictionary_t dict;
1135 	prop_object_t obj, obj1, obj2;
1136 	int32_t critval;
1137 	int i, invalid, error;
1138 	const char *blah, *sname;
1139 	bool targetfound = false;
1140 
1141 	error = invalid = 0;
1142 	blah = sname = NULL;
1143 
1144 	/* get sensor's name from userland dictionary. */
1145 	obj = prop_dictionary_get(udict, "sensor-name");
1146 	if (prop_object_type(obj) != PROP_TYPE_STRING) {
1147 		DPRINTF(("%s: sensor-name failed\n", __func__));
1148 		return EINVAL;
1149 	}
1150 
1151 	/* iterate over the sensors to find the right one */
1152 	for (i = 0; i < sme->sme_nsensors; i++) {
1153 		edata = &sme->sme_sensor_data[i];
1154 		/* skip sensors with duplicate description */
1155 		if (edata->flags & ENVSYS_FNOTVALID) {
1156 			invalid++;
1157 			continue;
1158 		}
1159 
1160 		dict = prop_array_get(array, i - invalid);
1161 		obj1 = prop_dictionary_get(dict, "description");
1162 
1163 		/* is it our sensor? */
1164 		if (!prop_string_equals(obj1, obj))
1165 			continue;
1166 
1167 		/*
1168 		 * Check if a new description operation was
1169 		 * requested by the user and set new description.
1170 		 */
1171 		if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
1172 			targetfound = true;
1173 			blah = prop_string_cstring_nocopy(obj2);
1174 
1175 			for (i = 0; i < sme->sme_nsensors; i++) {
1176 				if (i == edata->sensor)
1177 					continue;
1178 
1179 				nedata = &sme->sme_sensor_data[i];
1180 				if (strcmp(blah, nedata->desc) == 0) {
1181 					error = EEXIST;
1182 					break;
1183 				}
1184 			}
1185 
1186 			if (error)
1187 				break;
1188 
1189 			error = sme_sensor_upstring(dict,
1190 						    "description",
1191 						    blah);
1192 			if (!error)
1193 				(void)strlcpy(edata->desc,
1194 					      blah,
1195 					      sizeof(edata->desc));
1196 
1197 			break;
1198 		}
1199 
1200 		/* did the user want to remove a critical capacity limit? */
1201 		obj2 = prop_dictionary_get(udict, "remove-critical-cap");
1202 		if (obj2 != NULL) {
1203 			targetfound = true;
1204 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1205 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
1206 				    error = ENOTSUP;
1207 				    break;
1208 			}
1209 
1210 			sname = prop_string_cstring_nocopy(obj);
1211 			error = sme_event_unregister(sname,
1212 			    PENVSYS_EVENT_BATT_USERCAP);
1213 			if (error)
1214 				break;
1215 
1216 			prop_dictionary_remove(dict, "critical-capacity");
1217 			break;
1218 		}
1219 
1220 		/* did the user want to remove a critical min limit? */
1221 		obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
1222 		if (obj2 != NULL) {
1223 			targetfound = true;
1224 			sname = prop_string_cstring_nocopy(obj);
1225 			error = sme_event_unregister(sname,
1226 			    PENVSYS_EVENT_USER_CRITMIN);
1227 			if (error)
1228 				break;
1229 
1230 			prop_dictionary_remove(dict, "critical-min-limit");
1231 			break;
1232 		}
1233 
1234 		/* did the user want to remove a critical max limit? */
1235 		obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
1236 		if (obj2 != NULL) {
1237 			targetfound = true;
1238 			sname = prop_string_cstring_nocopy(obj);
1239 			error = sme_event_unregister(sname,
1240 			    PENVSYS_EVENT_USER_CRITMAX);
1241 			if (error)
1242 				break;
1243 
1244 			prop_dictionary_remove(dict, "critical-max-limit");
1245 			break;
1246 		}
1247 
1248 		/* did the user want to change rfact? */
1249 		obj2 = prop_dictionary_get(udict, "new-rfact");
1250 		if (obj2 != NULL) {
1251 			targetfound = true;
1252 			if (edata->flags & ENVSYS_FCHANGERFACT)
1253 				edata->rfact = prop_number_integer_value(obj2);
1254 			else
1255 				error = ENOTSUP;
1256 
1257 			break;
1258 		}
1259 
1260 		for (i = 0; sst[i].type != -1; i++)
1261 			if (sst[i].type == edata->units)
1262 				break;
1263 
1264 		/* did the user want to set a critical capacity event? */
1265 		obj2 = prop_dictionary_get(udict, "critical-capacity");
1266 		if (obj2 != NULL) {
1267 			targetfound = true;
1268 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1269 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
1270 				error = ENOTSUP;
1271 				break;
1272 			}
1273 
1274 			critval = prop_number_integer_value(obj2);
1275 			error = sme_event_add(dict,
1276 					      edata,
1277 					      sme->sme_name,
1278 					      "critical-capacity",
1279 					      critval,
1280 					      PENVSYS_EVENT_BATT_USERCAP,
1281 					      sst[i].crittype);
1282 			break;
1283 		}
1284 
1285 		/* did the user want to set a critical max event? */
1286 		obj2 = prop_dictionary_get(udict, "critical-max-limit");
1287 		if (obj2 != NULL) {
1288 			targetfound = true;
1289 			if (edata->units == ENVSYS_INDICATOR ||
1290 			    edata->flags & ENVSYS_FMONNOTSUPP) {
1291 				error = ENOTSUP;
1292 				break;
1293 			}
1294 
1295 			critval = prop_number_integer_value(obj2);
1296 			error = sme_event_add(dict,
1297 					      edata,
1298 					      sme->sme_name,
1299 					      "critical-max-limit",
1300 					      critval,
1301 					      PENVSYS_EVENT_USER_CRITMAX,
1302 					      sst[i].crittype);
1303 			break;
1304 		}
1305 
1306 		/* did the user want to set a critical min event? */
1307 		obj2 = prop_dictionary_get(udict, "critical-min-limit");
1308 		if (obj2 != NULL) {
1309 			targetfound = true;
1310 			if (edata->units == ENVSYS_INDICATOR ||
1311 			    edata->flags & ENVSYS_FMONNOTSUPP) {
1312 				error = ENOTSUP;
1313 				break;
1314 			}
1315 
1316 			critval = prop_number_integer_value(obj2);
1317 			error = sme_event_add(dict,
1318 					      edata,
1319 					      sme->sme_name,
1320 					      "critical-min-limit",
1321 					      critval,
1322 					      PENVSYS_EVENT_USER_CRITMIN,
1323 					      sst[i].crittype);
1324 			break;
1325 		}
1326 	}
1327 
1328 	/* invalid target? return the error */
1329 	if (!targetfound)
1330 		error = EINVAL;
1331 
1332 	return error;
1333 }
1334