sysmon_envsys.c revision 1.68 1 /* $NetBSD: sysmon_envsys.c,v 1.68 2007/09/24 19:46:38 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 (at) 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.68 2007/09/24 19:46:38 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 /* #define ENVSYS_DEBUG */
92 #include <dev/sysmon/sysmonvar.h>
93 #include <dev/sysmon/sysmon_envsysvar.h>
94 #include <dev/sysmon/sysmon_taskq.h>
95
96 /*
97 * Notes about locking:
98 *
99 * There's a global lock 'sme_mtx' to protect access to 'sysmon_envsys_list'
100 * (devices linked list), 'struct sysmon_envsys' (device), 'sme_events_list'
101 * (events linked list), 'sme_event_t' (event) and the global counter
102 * 'sysmon_envsys_next_sensor_index'.
103 *
104 * Another lock 'sme_init_mtx' is used to protect initialization and
105 * finalization of the events framework (the callout(9) and workqueue(9)
106 * that is used to check for conditions and sending events to powerd(8)).
107 *
108 * The global 'sme_cv' condition variable is used to wait for state changes
109 * on the 'sysmon_envsys_list' and 'sme_events_list' linked lists.
110 *
111 */
112
113 kmutex_t sme_mtx, sme_event_init_mtx;
114 kcondvar_t sme_cv;
115
116 static prop_dictionary_t sme_propd;
117 static uint32_t sysmon_envsys_next_sensor_index = 0;
118 static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
119
120 static void sysmon_envsys_release(struct sysmon_envsys *);
121 static void sysmon_envsys_destroy_plist(prop_array_t);
122 static int sme_register_sensorname(struct sysmon_envsys *, envsys_data_t *);
123
124 /*
125 * sysmon_envsys_init:
126 *
127 * + Initialize global mutexes, dictionary and the linked lists.
128 */
129 void
130 sysmon_envsys_init(void)
131 {
132 LIST_INIT(&sysmon_envsys_list);
133 LIST_INIT(&sme_events_list);
134 mutex_init(&sme_mtx, MUTEX_DRIVER, IPL_NONE);
135 mutex_init(&sme_event_init_mtx, MUTEX_DRIVER, IPL_NONE);
136 cv_init(&sme_cv, "smework");
137 sme_propd = prop_dictionary_create();
138 }
139
140 /*
141 * sysmonopen_envsys:
142 *
143 * + Open the system monitor device.
144 */
145 int
146 sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
147 {
148 return 0;
149 }
150
151 /*
152 * sysmonclose_envsys:
153 *
154 * + Close the system monitor device.
155 */
156 int
157 sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
158 {
159 /* Nothing to do */
160 return 0;
161 }
162
163 /*
164 * sysmonioctl_envsys:
165 *
166 * + Perform a sysmon envsys control request.
167 */
168 int
169 sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
170 {
171 struct sysmon_envsys *sme = NULL;
172 int error = 0;
173 u_int oidx;
174
175 switch (cmd) {
176 case ENVSYS_GETDICTIONARY:
177 {
178 struct plistref *plist = (struct plistref *)data;
179
180 /*
181 * Update all sysmon envsys devices dictionaries with
182 * new data if it's different than we have currently
183 * in the dictionary.
184 */
185 mutex_enter(&sme_mtx);
186 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
187 sme->sme_flags |= SME_FLAG_BUSY;
188 error = sme_update_dictionary(sme);
189 if (error) {
190 DPRINTF(("%s: sme_update_dictionary, "
191 "error=%d\n", __func__, error));
192 sme->sme_flags &= ~SME_FLAG_BUSY;
193 mutex_exit(&sme_mtx);
194 return error;
195 }
196 sme->sme_flags &= ~SME_FLAG_BUSY;
197 }
198 mutex_exit(&sme_mtx);
199 /*
200 * Copy global dictionary to userland.
201 */
202 error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
203 break;
204 }
205 case ENVSYS_SETDICTIONARY:
206 {
207 const struct plistref *plist = (const struct plistref *)data;
208 prop_dictionary_t udict;
209 prop_object_t obj;
210 const char *devname = NULL;
211
212 if ((flag & FWRITE) == 0)
213 return EPERM;
214
215 /*
216 * Get dictionary from userland.
217 */
218 error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
219 if (error) {
220 DPRINTF(("%s: copyin_ioctl error=%d\n",
221 __func__, error));
222 break;
223 }
224
225 /*
226 * Parse "driver-name" key to obtain the driver we
227 * are searching for.
228 */
229 obj = prop_dictionary_get(udict, "driver-name");
230 if (obj == NULL || prop_object_type(obj) != PROP_TYPE_STRING) {
231 DPRINTF(("%s: driver-name failed\n", __func__));
232 prop_object_release(udict);
233 error = EINVAL;
234 break;
235 }
236
237 /* driver name */
238 devname = prop_string_cstring_nocopy(obj);
239
240 /* find the correct sme device */
241 sme = sysmon_envsys_find(devname);
242 if (sme == NULL) {
243 DPRINTF(("%s: NULL sme\n", __func__));
244 prop_object_release(udict);
245 error = EINVAL;
246 break;
247 }
248
249 /*
250 * Find the correct array object with the string supplied
251 * by the userland dictionary.
252 */
253 obj = prop_dictionary_get(sme_propd, devname);
254 if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
255 DPRINTF(("%s: array device failed\n", __func__));
256 sysmon_envsys_release(sme);
257 prop_object_release(udict);
258 error = EINVAL;
259 break;
260 }
261
262 /* do the real work now */
263 error = sme_userset_dictionary(sme, udict, obj);
264 sysmon_envsys_release(sme);
265 prop_object_release(udict);
266 break;
267 }
268 /*
269 * Compatibility functions with the old interface, only
270 * implemented ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough
271 * to make old applications work.
272 */
273 case ENVSYS_GTREDATA:
274 {
275 struct envsys_tre_data *tred = (void *)data;
276 envsys_data_t *edata = NULL;
277
278 tred->validflags = 0;
279
280 sme = sysmon_envsys_find_40(tred->sensor);
281 if (sme == NULL)
282 break;
283
284 mutex_enter(&sme_mtx);
285 oidx = tred->sensor;
286 tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
287
288 DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
289 __func__, tred->sensor, oidx, sme->sme_name,
290 sme->sme_nsensors));
291
292 edata = &sme->sme_sensor_data[tred->sensor];
293
294 if (tred->sensor < sme->sme_nsensors) {
295 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
296 error = (*sme->sme_gtredata)(sme, edata);
297 if (error) {
298 DPRINTF(("%s: sme_gtredata failed\n",
299 __func__));
300 mutex_exit(&sme_mtx);
301 return error;
302 }
303 }
304
305 /* copy required values to the old interface */
306 tred->sensor = edata->sensor;
307 tred->cur.data_us = edata->value_cur;
308 tred->cur.data_s = edata->value_cur;
309 tred->max.data_us = edata->value_max;
310 tred->max.data_s = edata->value_max;
311 tred->min.data_us = edata->value_min;
312 tred->min.data_s = edata->value_min;
313 tred->avg.data_us = edata->value_avg;
314 tred->avg.data_s = edata->value_avg;
315 tred->units = edata->units;
316
317 tred->validflags |= ENVSYS_FVALID;
318 tred->validflags |= ENVSYS_FCURVALID;
319
320 if (edata->flags & ENVSYS_FPERCENT) {
321 tred->validflags |= ENVSYS_FMAXVALID;
322 tred->validflags |= ENVSYS_FFRACVALID;
323 }
324
325 if (edata->state == ENVSYS_SINVALID ||
326 edata->flags & ENVSYS_FNOTVALID) {
327 tred->validflags &= ~ENVSYS_FCURVALID;
328 tred->cur.data_us = tred->cur.data_s = 0;
329 }
330
331 DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
332 __func__, edata->desc, tred->cur.data_s));
333 DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
334 " tred->sensor=%d\n", __func__, tred->validflags,
335 tred->units, tred->sensor));
336 }
337 tred->sensor = oidx;
338 mutex_exit(&sme_mtx);
339
340 break;
341 }
342 case ENVSYS_GTREINFO:
343 {
344 struct envsys_basic_info *binfo = (void *)data;
345 envsys_data_t *edata = NULL;
346
347 binfo->validflags = 0;
348
349 sme = sysmon_envsys_find_40(binfo->sensor);
350 if (sme == NULL)
351 break;
352
353 mutex_enter(&sme_mtx);
354 oidx = binfo->sensor;
355 binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
356
357 edata = &sme->sme_sensor_data[binfo->sensor];
358
359 binfo->validflags |= ENVSYS_FVALID;
360
361 if (binfo->sensor < sme->sme_nsensors) {
362 binfo->units = edata->units;
363 (void)strlcpy(binfo->desc, edata->desc,
364 sizeof(binfo->desc));
365 }
366
367 DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
368 __func__, binfo->units, binfo->validflags));
369 DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
370 __func__, binfo->desc, binfo->sensor));
371
372 binfo->sensor = oidx;
373 mutex_exit(&sme_mtx);
374
375 break;
376 }
377 default:
378 error = ENOTTY;
379 break;
380 }
381
382 return error;
383 }
384
385 /*
386 * sysmon_envsys_register:
387 *
388 * + Register a sysmon envsys device.
389 * + Create array of dictionaries for a device.
390 */
391 int
392 sysmon_envsys_register(struct sysmon_envsys *sme)
393 {
394 struct sme_evdrv {
395 SLIST_ENTRY(sme_evdrv) evdrv_head;
396 sme_event_drv_t *evdrv;
397 };
398 SLIST_HEAD(, sme_evdrv) sme_evdrv_list;
399 struct sme_evdrv *sme_evdrv = NULL;
400 struct sysmon_envsys *lsme;
401 prop_dictionary_t dict;
402 prop_array_t array;
403 envsys_data_t *edata = NULL;
404 int i, error = 0;
405
406 KASSERT(sme != NULL);
407 KASSERT(sme->sme_name != NULL);
408 KASSERT(sme->sme_sensor_data != NULL);
409
410 /*
411 * sme_nsensors is mandatory...
412 */
413 if (!sme->sme_nsensors)
414 return EINVAL;
415
416 /*
417 * sanity check: if SME_DISABLE_GTREDATA is not set,
418 * the sme_gtredata function callback must be non NULL.
419 */
420 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
421 if (sme->sme_gtredata == NULL)
422 return EINVAL;
423 }
424
425
426 /* create the sysmon envsys device array. */
427 array = prop_array_create();
428 if (array == NULL)
429 return ENOMEM;
430
431 /*
432 * Initialize the singly linked list for sensor descriptions.
433 */
434 SLIST_INIT(&sme->sme_names_list);
435
436 /*
437 * Initialize the singly linked list for driver events.
438 */
439 SLIST_INIT(&sme_evdrv_list);
440 /*
441 * Iterate over all sensors and create a dictionary per sensor,
442 * checking firstly if sensor description is unique.
443 */
444 for (i = 0; i < sme->sme_nsensors; i++) {
445 edata = &sme->sme_sensor_data[i];
446 /*
447 * Check if sensor description is unique.
448 */
449 if (sme_register_sensorname(sme, edata))
450 continue;
451
452 dict = prop_dictionary_create();
453 if (dict == NULL) {
454 error = ENOMEM;
455 goto out2;
456 }
457
458 /*
459 * Create all objects in sensor's dictionary.
460 */
461 sme_evdrv = kmem_zalloc(sizeof(*sme_evdrv), KM_SLEEP);
462 sme_evdrv->evdrv = sme_add_sensor_dictionary(sme,
463 array, dict, edata);
464 if (sme_evdrv->evdrv)
465 SLIST_INSERT_HEAD(&sme_evdrv_list,
466 sme_evdrv, evdrv_head);
467 }
468
469 /*
470 * Check if requested sysmon_envsys device is valid
471 * and does not exist already in the list.
472 */
473 mutex_enter(&sme_mtx);
474 sme->sme_flags |= SME_FLAG_BUSY;
475 LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
476 if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
477 error = EEXIST;
478 goto out;
479 }
480 }
481
482 /*
483 * If the array does not contain any object (sensor), there's
484 * no need to attach the driver.
485 */
486 if (prop_array_count(array) == 0) {
487 error = EINVAL;
488 DPRINTF(("%s: empty array for '%s'\n", __func__,
489 sme->sme_name));
490 goto out;
491 }
492 /*
493 * Add the array into the global dictionary for the driver.
494 *
495 * <dict>
496 * <key>foo0</key>
497 * <array>
498 * ...
499 */
500 if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
501 error = EINVAL;
502 DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__,
503 sme->sme_name));
504 goto out;
505 }
506 /*
507 * Add the device into the list.
508 */
509 LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
510 sme->sme_fsensor = sysmon_envsys_next_sensor_index;
511 sysmon_envsys_next_sensor_index += sme->sme_nsensors;
512 out:
513 sme->sme_uniqsensors = 0;
514 sme->sme_flags &= ~SME_FLAG_BUSY;
515 mutex_exit(&sme_mtx);
516
517 if (error == 0) {
518 i = 0;
519 SLIST_FOREACH(sme_evdrv, &sme_evdrv_list, evdrv_head) {
520 if (i == 0)
521 sysmon_task_queue_init();
522 sysmon_task_queue_sched(0,
523 sme_event_drvadd, sme_evdrv->evdrv);
524 }
525 DPRINTF(("%s: driver '%s' registered (nsens=%d)\n",
526 __func__, sme->sme_name, sme->sme_nsensors));
527 }
528
529 out2:
530 while (!SLIST_EMPTY(&sme_evdrv_list)) {
531 sme_evdrv = SLIST_FIRST(&sme_evdrv_list);
532 SLIST_REMOVE_HEAD(&sme_evdrv_list, evdrv_head);
533 kmem_free(sme_evdrv, sizeof(*sme_evdrv));
534 }
535 if (error == 0)
536 return 0;
537
538 DPRINTF(("%s: failed to register '%s' (%d)\n", __func__,
539 sme->sme_name, error));
540 if (error != EEXIST) {
541 mutex_enter(&sme_mtx);
542 sme_event_unregister_all(sme->sme_name);
543 mutex_exit(&sme_mtx);
544 }
545 sysmon_envsys_destroy_plist(array);
546 return error;
547 }
548
549 /*
550 * sysmon_envsys_destroy_plist:
551 *
552 * + Remove all objects from the array of dictionaries that is
553 * created in a sysmon envsys device.
554 */
555 static void
556 sysmon_envsys_destroy_plist(prop_array_t array)
557 {
558 prop_object_iterator_t iter, iter2;
559 prop_dictionary_t dict;
560 prop_object_t obj;
561
562 KASSERT(array != NULL);
563
564 DPRINTFOBJ(("%s: objects in array=%d\n", __func__,
565 prop_array_count(array)));
566
567 iter = prop_array_iterator(array);
568 if (iter == NULL)
569 return;
570
571 while ((dict = prop_object_iterator_next(iter)) != NULL) {
572 KASSERT(prop_object_type(dict) == PROP_TYPE_DICTIONARY);
573 iter2 = prop_dictionary_iterator(dict);
574 if (iter2 == NULL)
575 goto out;
576 DPRINTFOBJ(("%s: iterating over dictionary\n", __func__));
577 while ((obj = prop_object_iterator_next(iter2)) != NULL) {
578 DPRINTFOBJ(("%s: obj=%s\n", __func__,
579 prop_dictionary_keysym_cstring_nocopy(obj)));
580 prop_dictionary_remove(dict,
581 prop_dictionary_keysym_cstring_nocopy(obj));
582 prop_object_iterator_reset(iter2);
583 }
584 prop_object_iterator_release(iter2);
585 DPRINTFOBJ(("%s: objects in dictionary:%d\n",
586 __func__, prop_dictionary_count(dict)));
587 prop_object_release(dict);
588 }
589
590 out:
591 prop_object_iterator_release(iter);
592 prop_object_release(array);
593 }
594
595 /*
596 * sysmon_envsys_unregister:
597 *
598 * + Unregister a sysmon envsys device.
599 */
600 void
601 sysmon_envsys_unregister(struct sysmon_envsys *sme)
602 {
603 struct sme_sensor_names *snames;
604 prop_array_t array;
605
606 KASSERT(sme != NULL);
607
608 mutex_enter(&sme_mtx);
609 while (sme->sme_flags & SME_FLAG_BUSY) {
610 sme->sme_flags |= SME_FLAG_WANTED;
611 cv_wait(&sme_cv, &sme_mtx);
612 }
613 sysmon_envsys_next_sensor_index -= sme->sme_nsensors;
614 /*
615 * Remove all sensor descriptions from the singly linked list.
616 */
617 while (!SLIST_EMPTY(&sme->sme_names_list)) {
618 snames = SLIST_FIRST(&sme->sme_names_list);
619 SLIST_REMOVE_HEAD(&sme->sme_names_list, sme_names);
620 kmem_free(snames, sizeof(*snames));
621 }
622 /*
623 * Unregister all events associated with this device.
624 */
625 sme_event_unregister_all(sme->sme_name);
626 LIST_REMOVE(sme, sme_list);
627 mutex_exit(&sme_mtx);
628 /*
629 * Remove the device (and all its objects) from the global dictionary.
630 */
631 array = prop_dictionary_get(sme_propd, sme->sme_name);
632 if (array && prop_object_type(array) == PROP_TYPE_ARRAY) {
633 prop_dictionary_remove(sme_propd, sme->sme_name);
634 sysmon_envsys_destroy_plist(array);
635 }
636 }
637
638 /*
639 * sysmon_envsys_find:
640 *
641 * + Find a sysmon envsys device.
642 */
643 struct sysmon_envsys *
644 sysmon_envsys_find(const char *name)
645 {
646 struct sysmon_envsys *sme;
647
648 mutex_enter(&sme_mtx);
649 again:
650 for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
651 sme = LIST_NEXT(sme, sme_list)) {
652 if (strcmp(sme->sme_name, name) == 0) {
653 if (sme->sme_flags & SME_FLAG_BUSY) {
654 sme->sme_flags |= SME_FLAG_WANTED;
655 cv_wait(&sme_cv, &sme_mtx);
656 goto again;
657 }
658 sme->sme_flags |= SME_FLAG_BUSY;
659 break;
660 }
661 }
662 mutex_exit(&sme_mtx);
663 return sme;
664 }
665
666 /*
667 * sysmon_envsys_release:
668 *
669 * + Release a sysmon envsys device.
670 */
671 void
672 sysmon_envsys_release(struct sysmon_envsys *sme)
673 {
674 mutex_enter(&sme_mtx);
675 if (sme->sme_flags & SME_FLAG_WANTED)
676 cv_broadcast(&sme_cv);
677 sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
678 mutex_exit(&sme_mtx);
679 }
680
681 /* compatibility function */
682 struct sysmon_envsys *
683 sysmon_envsys_find_40(u_int idx)
684 {
685 struct sysmon_envsys *sme;
686
687 mutex_enter(&sme_mtx);
688 for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
689 sme = LIST_NEXT(sme, sme_list)) {
690 if (idx >= sme->sme_fsensor &&
691 idx < (sme->sme_fsensor + sme->sme_nsensors))
692 break;
693 }
694 mutex_exit(&sme_mtx);
695 return sme;
696 }
697
698 /*
699 * sme_register_sensorname:
700 *
701 * + Register a sensor description into the list maintained per device.
702 */
703 static int
704 sme_register_sensorname(struct sysmon_envsys *sme, envsys_data_t *edata)
705 {
706 struct sme_sensor_names *snames, *snames2 = NULL;
707
708 KASSERT(edata != NULL);
709
710 SLIST_FOREACH(snames2, &sme->sme_names_list, sme_names) {
711 /*
712 * Match sensors with empty and duplicate description.
713 */
714 if (strlen(edata->desc) == 0 ||
715 strcmp(snames2->desc, edata->desc) == 0)
716 if (snames2->assigned) {
717 edata->flags |= ENVSYS_FNOTVALID;
718 DPRINTF(("%s: wrong sensor name='%s'\n",
719 sme->sme_name, edata->desc));
720 return EEXIST;
721 }
722 }
723
724 snames = kmem_zalloc(sizeof(*snames), KM_NOSLEEP);
725 if (snames == NULL)
726 return ENOMEM;
727
728 snames->assigned = true;
729 (void)strlcpy(snames->desc, edata->desc, sizeof(snames->desc));
730 DPRINTF(("%s: registering sensor name='%s'\n",
731 sme->sme_name, edata->desc));
732 SLIST_INSERT_HEAD(&sme->sme_names_list, snames, sme_names);
733 sme->sme_uniqsensors++;
734
735 return 0;
736 }
737
738 /*
739 * sme_add_sensor_dictionary:
740 *
741 * + Add the objects into the dictionary.
742 */
743 sme_event_drv_t *
744 sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
745 prop_dictionary_t dict, envsys_data_t *edata)
746 {
747 const struct sme_description_table *sdt, *sdt_units;
748 sme_event_drv_t *sme_evdrv_t = NULL;
749 int i, j;
750
751 i = j = 0;
752
753 /* find the correct unit for this sensor. */
754 sdt_units = sme_get_description_table(SME_DESC_UNITS);
755 for (i = 0; sdt_units[i].type != -1; i++)
756 if (sdt_units[i].type == edata->units)
757 break;
758
759 if (strcmp(sdt_units[i].desc, "unknown") == 0) {
760 DPRINTF(("%s: invalid units type for sensor=%d\n",
761 __func__, edata->sensor));
762 goto invalidate_sensor;
763 }
764
765 /*
766 * ...
767 * <key>type</key>
768 * <string>foo</string>
769 * <key>description</key>
770 * <string>blah blah</string>
771 * ...
772 */
773 if (sme_sensor_upstring(dict, "type", sdt_units[i].desc))
774 goto invalidate_sensor;
775
776 if (strlen(edata->desc) == 0) {
777 DPRINTF(("%s: invalid description for sensor=%d\n",
778 __func__, edata->sensor));
779 goto invalidate_sensor;
780 }
781
782 if (sme_sensor_upstring(dict, "description", edata->desc))
783 goto invalidate_sensor;
784
785 /*
786 * Add sensor's state description.
787 *
788 * ...
789 * <key>state</key>
790 * <string>valid</string>
791 * ...
792 */
793 sdt = sme_get_description_table(SME_DESC_STATES);
794 for (j = 0; sdt[j].type != -1; j++)
795 if (sdt[j].type == edata->state)
796 break;
797
798 if (strcmp(sdt[j].desc, "unknown") == 0) {
799 DPRINTF(("%s: invalid state for sensor=%d\n",
800 __func__, edata->sensor));
801 goto invalidate_sensor;
802 }
803
804 DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
805 __func__, edata->desc, edata->units, edata->state));
806
807 if (sme_sensor_upstring(dict, "state", sdt[j].desc))
808 goto invalidate_sensor;
809
810 /*
811 * Add the monitoring boolean object:
812 *
813 * ...
814 * <key>monitoring-supported</key>
815 * <true/>
816 * ...
817 *
818 * always false on Battery state, Drive and Indicator types.
819 * They cannot be monitored.
820 *
821 */
822 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
823 (edata->units == ENVSYS_INDICATOR) ||
824 (edata->units == ENVSYS_DRIVE) ||
825 (edata->units == ENVSYS_BATTERY_STATE)) {
826 if (sme_sensor_upbool(dict, "monitoring-supported", false))
827 goto out;
828 } else {
829 if (sme_sensor_upbool(dict, "monitoring-supported", true))
830 goto out;
831 }
832
833 /*
834 * add the percentage boolean object:
835 *
836 * ...
837 * <key>want-percentage</key>
838 * <true/>
839 * ...
840 */
841 if (edata->flags & ENVSYS_FPERCENT)
842 if (sme_sensor_upbool(dict, "want-percentage", true))
843 goto out;
844
845 /*
846 * Add the battery-state object for battery state sensors:
847 *
848 * ...
849 * <key>battery-state</key>
850 * <string>NORMAL</string>
851 * ...
852 */
853 if (edata->units == ENVSYS_BATTERY_STATE) {
854 sdt = sme_get_description_table(SME_DESC_BATTERY_STATES);
855 for (j = 0; sdt[j].type != -1; j++)
856 if (sdt[j].type == edata->value_cur)
857 break;
858
859 if (sme_sensor_upstring(dict, "battery-state", sdt[j].desc))
860 goto out;
861 }
862
863 /*
864 * Add the drive-state object for drive sensors:
865 *
866 * ...
867 * <key>drive-state</key>
868 * <string>drive is online</string>
869 * ...
870 */
871 if (edata->units == ENVSYS_DRIVE) {
872 sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
873 for (j = 0; sdt[j].type != -1; j++)
874 if (sdt[j].type == edata->value_cur)
875 break;
876
877 if (sme_sensor_upstring(dict, "drive-state", sdt[j].desc))
878 goto out;
879 }
880
881 /*
882 * if sensor is enabled, add the following properties...
883 */
884 if (edata->state == ENVSYS_SVALID) {
885 /*
886 * ...
887 * <key>rpms</key>
888 * <integer>2500</integer>
889 * <key>rfact</key>
890 * <integer>10000</integer>
891 * <key>cur-value</key>
892 * <integer>1250</integer>
893 * <key>min-value</key>
894 * <integer>800</integer>
895 * <key>max-value</integer>
896 * <integer>3000</integer>
897 * <key>avg-value</integer>
898 * <integer>1400</integer>
899 * </dict>
900 */
901 if (edata->units == ENVSYS_SFANRPM)
902 if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
903 goto out;
904
905 if (edata->units == ENVSYS_SVOLTS_AC ||
906 edata->units == ENVSYS_SVOLTS_DC)
907 if (sme_sensor_upint32(dict, "rfact", edata->rfact))
908 goto out;
909
910 if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
911 goto out;
912
913 if (edata->flags & ENVSYS_FVALID_MIN) {
914 if (sme_sensor_upint32(dict,
915 "min-value",
916 edata->value_min))
917 goto out;
918 }
919
920 if (edata->flags & ENVSYS_FVALID_MAX) {
921 if (sme_sensor_upint32(dict,
922 "max-value",
923 edata->value_max))
924 goto out;
925 }
926
927 if (edata->flags & ENVSYS_FVALID_AVG) {
928 if (sme_sensor_upint32(dict,
929 "avg-value",
930 edata->value_avg))
931 goto out;
932 }
933 }
934
935 /*
936 * ...
937 * </array>
938 *
939 * Add the dictionary into the array.
940 *
941 */
942
943 if (!prop_array_set(array, sme->sme_uniqsensors - 1, dict)) {
944 DPRINTF(("%s: prop_array_add\n", __func__));
945 goto invalidate_sensor;
946 }
947
948 /*
949 * Add a new event if a monitoring flag was set.
950 */
951 if (edata->monitor) {
952 sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
953 sme_evdrv_t->sdict = dict;
954 sme_evdrv_t->edata = edata;
955 sme_evdrv_t->sme = sme;
956 sme_evdrv_t->powertype = sdt_units[i].crittype;
957 }
958
959 out:
960 return sme_evdrv_t;
961
962 invalidate_sensor:
963 edata->flags |= ENVSYS_FNOTVALID;
964 prop_object_release(dict);
965 return sme_evdrv_t;
966 }
967
968 /*
969 * sme_update_dictionary:
970 *
971 * + Update per-sensor dictionaries with new values if there were
972 * changes, otherwise the object in dictionary is untouched.
973 */
974 int
975 sme_update_dictionary(struct sysmon_envsys *sme)
976 {
977 const struct sme_description_table *sdt;
978 envsys_data_t *edata;
979 prop_object_t array, dict;
980 int i, j, error, invalid;
981
982 KASSERT(mutex_owned(&sme_mtx));
983
984 error = invalid = 0;
985 array = dict = NULL;
986
987 /* retrieve the array of dictionaries in device. */
988 array = prop_dictionary_get(sme_propd, sme->sme_name);
989 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
990 DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
991 return EINVAL;
992 }
993
994 /*
995 * - iterate over all sensors.
996 * - fetch new data.
997 * - check if data in dictionary is different than new data.
998 * - update dictionary if there were changes.
999 */
1000 DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__,
1001 sme->sme_name, sme->sme_nsensors));
1002
1003 for (i = 0; i < sme->sme_nsensors; i++) {
1004 edata = &sme->sme_sensor_data[i];
1005 /* skip invalid sensors */
1006 if (edata->flags & ENVSYS_FNOTVALID) {
1007 DPRINTF(("%s: invalid sensor=%s idx=%d\n",
1008 __func__, edata->desc, edata->sensor));
1009 invalid++;
1010 continue;
1011 }
1012
1013 /*
1014 * refresh sensor data via sme_gtredata only if the
1015 * flag is not set.
1016 */
1017 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
1018 error = (*sme->sme_gtredata)(sme, edata);
1019 if (error) {
1020 DPRINTF(("%s: gtredata[%d] failed\n",
1021 __func__, i));
1022 return error;
1023 }
1024 }
1025
1026 /* retrieve sensor's dictionary. */
1027 dict = prop_array_get(array, i - invalid);
1028 if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
1029 DPRINTF(("%s: not a dictionary (%d:%s)\n",
1030 __func__, edata->sensor, sme->sme_name));
1031 return EINVAL;
1032 }
1033
1034 /* update sensor's state */
1035 sdt = sme_get_description_table(SME_DESC_STATES);
1036 for (j = 0; sdt[j].type != -1; j++)
1037 if (sdt[j].type == edata->state)
1038 break;
1039
1040 DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
1041 "units=%d sensor=%d\n", __func__, sdt[j].desc,
1042 sdt[j].type, edata->flags, edata->units, edata->sensor));
1043
1044 /* update sensor state */
1045 error = sme_sensor_upstring(dict, "state", sdt[j].desc);
1046 if (error)
1047 break;
1048
1049 /* update sensor type */
1050 sdt = sme_get_description_table(SME_DESC_UNITS);
1051 for (j = 0; sdt[j].type != -1; j++)
1052 if (sdt[j].type == edata->units)
1053 break;
1054
1055 error = sme_sensor_upstring(dict, "type", sdt[j].desc);
1056 if (error)
1057 break;
1058
1059 /* update sensor current value */
1060 error = sme_sensor_upint32(dict,
1061 "cur-value",
1062 edata->value_cur);
1063 if (error)
1064 break;
1065
1066 /*
1067 * Integer and Indicator types do not the following
1068 * values, so skip them.
1069 */
1070 if (edata->units == ENVSYS_INTEGER ||
1071 edata->units == ENVSYS_INDICATOR)
1072 continue;
1073
1074 /* update sensor flags */
1075 if (edata->flags & ENVSYS_FPERCENT) {
1076 error = sme_sensor_upbool(dict,
1077 "want-percentage",
1078 true);
1079 if (error)
1080 break;
1081 }
1082
1083 if (edata->flags & ENVSYS_FVALID_MAX) {
1084 error = sme_sensor_upint32(dict,
1085 "max-value",
1086 edata->value_max);
1087 if (error)
1088 break;
1089 }
1090
1091 if (edata->flags & ENVSYS_FVALID_MIN) {
1092 error = sme_sensor_upint32(dict,
1093 "min-value",
1094 edata->value_min);
1095 if (error)
1096 break;
1097 }
1098
1099 if (edata->flags & ENVSYS_FVALID_AVG) {
1100 error = sme_sensor_upint32(dict,
1101 "avg-value",
1102 edata->value_avg);
1103 if (error)
1104 break;
1105 }
1106
1107 /* update 'rpms' only in ENVSYS_SFANRPM. */
1108 if (edata->units == ENVSYS_SFANRPM) {
1109 error = sme_sensor_upuint32(dict,
1110 "rpms",
1111 edata->rpms);
1112 if (error)
1113 break;
1114 }
1115
1116 /* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
1117 if (edata->units == ENVSYS_SVOLTS_AC ||
1118 edata->units == ENVSYS_SVOLTS_DC) {
1119 error = sme_sensor_upint32(dict,
1120 "rfact",
1121 edata->rfact);
1122 if (error)
1123 break;
1124 }
1125
1126 /* update 'drive-state' only in ENVSYS_DRIVE. */
1127 if (edata->units == ENVSYS_DRIVE) {
1128 sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
1129 for (j = 0; sdt[j].type != -1; j++)
1130 if (sdt[j].type == edata->value_cur)
1131 break;
1132
1133 error = sme_sensor_upstring(dict,
1134 "drive-state",
1135 sdt[j].desc);
1136 if (error)
1137 break;
1138 }
1139
1140 /* update 'battery-state' only in ENVSYS_BATTERY_STATE. */
1141 if (edata->units == ENVSYS_BATTERY_STATE) {
1142 sdt =
1143 sme_get_description_table(SME_DESC_BATTERY_STATES);
1144 for (j = 0; sdt[j].type != -1; j++)
1145 if (sdt[j].type == edata->value_cur)
1146 break;
1147
1148 error = sme_sensor_upstring(dict,
1149 "battery-state",
1150 sdt[j].desc);
1151 if (error)
1152 break;
1153 }
1154 }
1155
1156 return error;
1157 }
1158
1159 /*
1160 * sme_userset_dictionary:
1161 *
1162 * + Parse the userland dictionary and run the appropiate
1163 * task that was requested.
1164 */
1165 int
1166 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
1167 prop_array_t array)
1168 {
1169 const struct sme_description_table *sdt;
1170 envsys_data_t *edata, *nedata;
1171 prop_dictionary_t dict;
1172 prop_object_t obj, obj1, obj2;
1173 int32_t critval;
1174 int i, invalid, error;
1175 const char *blah, *sname;
1176 bool targetfound = false;
1177
1178 error = invalid = 0;
1179 blah = sname = NULL;
1180
1181 /* get sensor's name from userland dictionary. */
1182 obj = prop_dictionary_get(udict, "sensor-name");
1183 if (prop_object_type(obj) != PROP_TYPE_STRING) {
1184 DPRINTF(("%s: sensor-name failed\n", __func__));
1185 return EINVAL;
1186 }
1187
1188 /* iterate over the sensors to find the right one */
1189 for (i = 0; i < sme->sme_nsensors; i++) {
1190 edata = &sme->sme_sensor_data[i];
1191 /* skip sensors with duplicate description */
1192 if (edata->flags & ENVSYS_FNOTVALID) {
1193 invalid++;
1194 continue;
1195 }
1196
1197 dict = prop_array_get(array, i - invalid);
1198 obj1 = prop_dictionary_get(dict, "description");
1199
1200 /* is it our sensor? */
1201 if (!prop_string_equals(obj1, obj))
1202 continue;
1203
1204 /*
1205 * Check if a new description operation was
1206 * requested by the user and set new description.
1207 */
1208 if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
1209 targetfound = true;
1210 blah = prop_string_cstring_nocopy(obj2);
1211
1212 for (i = 0; i < sme->sme_nsensors; i++) {
1213 if (i == edata->sensor)
1214 continue;
1215
1216 nedata = &sme->sme_sensor_data[i];
1217 if (strcmp(blah, nedata->desc) == 0) {
1218 error = EEXIST;
1219 break;
1220 }
1221 }
1222
1223 if (error)
1224 break;
1225
1226 error = sme_sensor_upstring(dict,
1227 "description",
1228 blah);
1229 if (!error)
1230 (void)strlcpy(edata->desc,
1231 blah,
1232 sizeof(edata->desc));
1233
1234 break;
1235 }
1236
1237 /* did the user want to remove a critical capacity limit? */
1238 obj2 = prop_dictionary_get(udict, "remove-critical-cap");
1239 if (obj2 != NULL) {
1240 targetfound = true;
1241 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1242 (edata->flags & ENVSYS_FPERCENT) == 0) {
1243 error = ENOTSUP;
1244 break;
1245 }
1246
1247 sname = prop_string_cstring_nocopy(obj);
1248 error = sme_event_unregister(sname,
1249 PENVSYS_EVENT_BATT_USERCAP);
1250 if (error)
1251 break;
1252
1253 prop_dictionary_remove(dict, "critical-capacity");
1254 break;
1255 }
1256
1257 /* did the user want to remove a critical min limit? */
1258 obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
1259 if (obj2 != NULL) {
1260 targetfound = true;
1261 sname = prop_string_cstring_nocopy(obj);
1262 error = sme_event_unregister(sname,
1263 PENVSYS_EVENT_USER_CRITMIN);
1264 if (error)
1265 break;
1266
1267 prop_dictionary_remove(dict, "critical-min-limit");
1268 break;
1269 }
1270
1271 /* did the user want to remove a critical max limit? */
1272 obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
1273 if (obj2 != NULL) {
1274 targetfound = true;
1275 sname = prop_string_cstring_nocopy(obj);
1276 error = sme_event_unregister(sname,
1277 PENVSYS_EVENT_USER_CRITMAX);
1278 if (error)
1279 break;
1280
1281 prop_dictionary_remove(dict, "critical-max-limit");
1282 break;
1283 }
1284
1285 /* did the user want to change rfact? */
1286 obj2 = prop_dictionary_get(udict, "new-rfact");
1287 if (obj2 != NULL) {
1288 targetfound = true;
1289 if (edata->flags & ENVSYS_FCHANGERFACT)
1290 edata->rfact = prop_number_integer_value(obj2);
1291 else
1292 error = ENOTSUP;
1293
1294 break;
1295 }
1296
1297 sdt = sme_get_description_table(SME_DESC_UNITS);
1298 for (i = 0; sdt[i].type != -1; i++)
1299 if (sdt[i].type == edata->units)
1300 break;
1301
1302 /* did the user want to set a critical capacity event? */
1303 obj2 = prop_dictionary_get(udict, "critical-capacity");
1304 if (obj2 != NULL) {
1305 targetfound = true;
1306 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1307 (edata->flags & ENVSYS_FPERCENT) == 0) {
1308 error = ENOTSUP;
1309 break;
1310 }
1311
1312 critval = prop_number_integer_value(obj2);
1313 error = sme_event_register(dict,
1314 edata,
1315 sme->sme_name,
1316 "critical-capacity",
1317 critval,
1318 PENVSYS_EVENT_BATT_USERCAP,
1319 sdt[i].crittype);
1320 break;
1321 }
1322
1323 /* did the user want to set a critical max event? */
1324 obj2 = prop_dictionary_get(udict, "critical-max-limit");
1325 if (obj2 != NULL) {
1326 targetfound = true;
1327 if (edata->units == ENVSYS_INDICATOR ||
1328 edata->flags & ENVSYS_FMONNOTSUPP) {
1329 error = ENOTSUP;
1330 break;
1331 }
1332
1333 critval = prop_number_integer_value(obj2);
1334 error = sme_event_register(dict,
1335 edata,
1336 sme->sme_name,
1337 "critical-max-limit",
1338 critval,
1339 PENVSYS_EVENT_USER_CRITMAX,
1340 sdt[i].crittype);
1341 break;
1342 }
1343
1344 /* did the user want to set a critical min event? */
1345 obj2 = prop_dictionary_get(udict, "critical-min-limit");
1346 if (obj2 != NULL) {
1347 targetfound = true;
1348 if (edata->units == ENVSYS_INDICATOR ||
1349 edata->flags & ENVSYS_FMONNOTSUPP) {
1350 error = ENOTSUP;
1351 break;
1352 }
1353
1354 critval = prop_number_integer_value(obj2);
1355 error = sme_event_register(dict,
1356 edata,
1357 sme->sme_name,
1358 "critical-min-limit",
1359 critval,
1360 PENVSYS_EVENT_USER_CRITMIN,
1361 sdt[i].crittype);
1362 break;
1363 }
1364 }
1365
1366 /* invalid target? return the error */
1367 if (!targetfound)
1368 error = EINVAL;
1369
1370 return error;
1371 }
1372