sysmon_envsys.c revision 1.67 1 /* $NetBSD: sysmon_envsys.c,v 1.67 2007/09/09 12:28:08 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.67 2007/09/09 12:28:08 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 return sme_evdrv_t;
965 }
966
967 /*
968 * sme_update_dictionary:
969 *
970 * + Update per-sensor dictionaries with new values if there were
971 * changes, otherwise the object in dictionary is untouched.
972 */
973 int
974 sme_update_dictionary(struct sysmon_envsys *sme)
975 {
976 const struct sme_description_table *sdt;
977 envsys_data_t *edata;
978 prop_object_t array, dict;
979 int i, j, error, invalid;
980
981 KASSERT(mutex_owned(&sme_mtx));
982
983 error = invalid = 0;
984 array = dict = NULL;
985
986 /* retrieve the array of dictionaries in device. */
987 array = prop_dictionary_get(sme_propd, sme->sme_name);
988 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
989 DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
990 return EINVAL;
991 }
992
993 /*
994 * - iterate over all sensors.
995 * - fetch new data.
996 * - check if data in dictionary is different than new data.
997 * - update dictionary if there were changes.
998 */
999 DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__,
1000 sme->sme_name, sme->sme_nsensors));
1001
1002 for (i = 0; i < sme->sme_nsensors; i++) {
1003 edata = &sme->sme_sensor_data[i];
1004 /* skip invalid sensors */
1005 if (edata->flags & ENVSYS_FNOTVALID) {
1006 DPRINTF(("%s: invalid sensor=%s idx=%d\n",
1007 __func__, edata->desc, edata->sensor));
1008 invalid++;
1009 continue;
1010 }
1011
1012 /*
1013 * refresh sensor data via sme_gtredata only if the
1014 * flag is not set.
1015 */
1016 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
1017 error = (*sme->sme_gtredata)(sme, edata);
1018 if (error) {
1019 DPRINTF(("%s: gtredata[%d] failed\n",
1020 __func__, i));
1021 return error;
1022 }
1023 }
1024
1025 /* retrieve sensor's dictionary. */
1026 dict = prop_array_get(array, i - invalid);
1027 if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
1028 DPRINTF(("%s: not a dictionary (%d:%s)\n",
1029 __func__, edata->sensor, sme->sme_name));
1030 return EINVAL;
1031 }
1032
1033 /* update sensor's state */
1034 sdt = sme_get_description_table(SME_DESC_STATES);
1035 for (j = 0; sdt[j].type != -1; j++)
1036 if (sdt[j].type == edata->state)
1037 break;
1038
1039 DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
1040 "units=%d sensor=%d\n", __func__, sdt[j].desc,
1041 sdt[j].type, edata->flags, edata->units, edata->sensor));
1042
1043 /* update sensor state */
1044 error = sme_sensor_upstring(dict, "state", sdt[j].desc);
1045 if (error)
1046 break;
1047
1048 /* update sensor type */
1049 sdt = sme_get_description_table(SME_DESC_UNITS);
1050 for (j = 0; sdt[j].type != -1; j++)
1051 if (sdt[j].type == edata->units)
1052 break;
1053
1054 error = sme_sensor_upstring(dict, "type", sdt[j].desc);
1055 if (error)
1056 break;
1057
1058 /* update sensor current value */
1059 error = sme_sensor_upint32(dict,
1060 "cur-value",
1061 edata->value_cur);
1062 if (error)
1063 break;
1064
1065 /*
1066 * Integer and Indicator types do not the following
1067 * values, so skip them.
1068 */
1069 if (edata->units == ENVSYS_INTEGER ||
1070 edata->units == ENVSYS_INDICATOR)
1071 continue;
1072
1073 /* update sensor flags */
1074 if (edata->flags & ENVSYS_FPERCENT) {
1075 error = sme_sensor_upbool(dict,
1076 "want-percentage",
1077 true);
1078 if (error)
1079 break;
1080 }
1081
1082 if (edata->flags & ENVSYS_FVALID_MAX) {
1083 error = sme_sensor_upint32(dict,
1084 "max-value",
1085 edata->value_max);
1086 if (error)
1087 break;
1088 }
1089
1090 if (edata->flags & ENVSYS_FVALID_MIN) {
1091 error = sme_sensor_upint32(dict,
1092 "min-value",
1093 edata->value_min);
1094 if (error)
1095 break;
1096 }
1097
1098 if (edata->flags & ENVSYS_FVALID_AVG) {
1099 error = sme_sensor_upint32(dict,
1100 "avg-value",
1101 edata->value_avg);
1102 if (error)
1103 break;
1104 }
1105
1106 /* update 'rpms' only in ENVSYS_SFANRPM. */
1107 if (edata->units == ENVSYS_SFANRPM) {
1108 error = sme_sensor_upuint32(dict,
1109 "rpms",
1110 edata->rpms);
1111 if (error)
1112 break;
1113 }
1114
1115 /* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
1116 if (edata->units == ENVSYS_SVOLTS_AC ||
1117 edata->units == ENVSYS_SVOLTS_DC) {
1118 error = sme_sensor_upint32(dict,
1119 "rfact",
1120 edata->rfact);
1121 if (error)
1122 break;
1123 }
1124
1125 /* update 'drive-state' only in ENVSYS_DRIVE. */
1126 if (edata->units == ENVSYS_DRIVE) {
1127 sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
1128 for (j = 0; sdt[j].type != -1; j++)
1129 if (sdt[j].type == edata->value_cur)
1130 break;
1131
1132 error = sme_sensor_upstring(dict,
1133 "drive-state",
1134 sdt[j].desc);
1135 if (error)
1136 break;
1137 }
1138
1139 /* update 'battery-state' only in ENVSYS_BATTERY_STATE. */
1140 if (edata->units == ENVSYS_BATTERY_STATE) {
1141 sdt =
1142 sme_get_description_table(SME_DESC_BATTERY_STATES);
1143 for (j = 0; sdt[j].type != -1; j++)
1144 if (sdt[j].type == edata->value_cur)
1145 break;
1146
1147 error = sme_sensor_upstring(dict,
1148 "battery-state",
1149 sdt[j].desc);
1150 if (error)
1151 break;
1152 }
1153 }
1154
1155 return error;
1156 }
1157
1158 /*
1159 * sme_userset_dictionary:
1160 *
1161 * + Parse the userland dictionary and run the appropiate
1162 * task that was requested.
1163 */
1164 int
1165 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
1166 prop_array_t array)
1167 {
1168 const struct sme_description_table *sdt;
1169 envsys_data_t *edata, *nedata;
1170 prop_dictionary_t dict;
1171 prop_object_t obj, obj1, obj2;
1172 int32_t critval;
1173 int i, invalid, error;
1174 const char *blah, *sname;
1175 bool targetfound = false;
1176
1177 error = invalid = 0;
1178 blah = sname = NULL;
1179
1180 /* get sensor's name from userland dictionary. */
1181 obj = prop_dictionary_get(udict, "sensor-name");
1182 if (prop_object_type(obj) != PROP_TYPE_STRING) {
1183 DPRINTF(("%s: sensor-name failed\n", __func__));
1184 return EINVAL;
1185 }
1186
1187 /* iterate over the sensors to find the right one */
1188 for (i = 0; i < sme->sme_nsensors; i++) {
1189 edata = &sme->sme_sensor_data[i];
1190 /* skip sensors with duplicate description */
1191 if (edata->flags & ENVSYS_FNOTVALID) {
1192 invalid++;
1193 continue;
1194 }
1195
1196 dict = prop_array_get(array, i - invalid);
1197 obj1 = prop_dictionary_get(dict, "description");
1198
1199 /* is it our sensor? */
1200 if (!prop_string_equals(obj1, obj))
1201 continue;
1202
1203 /*
1204 * Check if a new description operation was
1205 * requested by the user and set new description.
1206 */
1207 if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
1208 targetfound = true;
1209 blah = prop_string_cstring_nocopy(obj2);
1210
1211 for (i = 0; i < sme->sme_nsensors; i++) {
1212 if (i == edata->sensor)
1213 continue;
1214
1215 nedata = &sme->sme_sensor_data[i];
1216 if (strcmp(blah, nedata->desc) == 0) {
1217 error = EEXIST;
1218 break;
1219 }
1220 }
1221
1222 if (error)
1223 break;
1224
1225 error = sme_sensor_upstring(dict,
1226 "description",
1227 blah);
1228 if (!error)
1229 (void)strlcpy(edata->desc,
1230 blah,
1231 sizeof(edata->desc));
1232
1233 break;
1234 }
1235
1236 /* did the user want to remove a critical capacity limit? */
1237 obj2 = prop_dictionary_get(udict, "remove-critical-cap");
1238 if (obj2 != NULL) {
1239 targetfound = true;
1240 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1241 (edata->flags & ENVSYS_FPERCENT) == 0) {
1242 error = ENOTSUP;
1243 break;
1244 }
1245
1246 sname = prop_string_cstring_nocopy(obj);
1247 error = sme_event_unregister(sname,
1248 PENVSYS_EVENT_BATT_USERCAP);
1249 if (error)
1250 break;
1251
1252 prop_dictionary_remove(dict, "critical-capacity");
1253 break;
1254 }
1255
1256 /* did the user want to remove a critical min limit? */
1257 obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
1258 if (obj2 != NULL) {
1259 targetfound = true;
1260 sname = prop_string_cstring_nocopy(obj);
1261 error = sme_event_unregister(sname,
1262 PENVSYS_EVENT_USER_CRITMIN);
1263 if (error)
1264 break;
1265
1266 prop_dictionary_remove(dict, "critical-min-limit");
1267 break;
1268 }
1269
1270 /* did the user want to remove a critical max limit? */
1271 obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
1272 if (obj2 != NULL) {
1273 targetfound = true;
1274 sname = prop_string_cstring_nocopy(obj);
1275 error = sme_event_unregister(sname,
1276 PENVSYS_EVENT_USER_CRITMAX);
1277 if (error)
1278 break;
1279
1280 prop_dictionary_remove(dict, "critical-max-limit");
1281 break;
1282 }
1283
1284 /* did the user want to change rfact? */
1285 obj2 = prop_dictionary_get(udict, "new-rfact");
1286 if (obj2 != NULL) {
1287 targetfound = true;
1288 if (edata->flags & ENVSYS_FCHANGERFACT)
1289 edata->rfact = prop_number_integer_value(obj2);
1290 else
1291 error = ENOTSUP;
1292
1293 break;
1294 }
1295
1296 sdt = sme_get_description_table(SME_DESC_UNITS);
1297 for (i = 0; sdt[i].type != -1; i++)
1298 if (sdt[i].type == edata->units)
1299 break;
1300
1301 /* did the user want to set a critical capacity event? */
1302 obj2 = prop_dictionary_get(udict, "critical-capacity");
1303 if (obj2 != NULL) {
1304 targetfound = true;
1305 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1306 (edata->flags & ENVSYS_FPERCENT) == 0) {
1307 error = ENOTSUP;
1308 break;
1309 }
1310
1311 critval = prop_number_integer_value(obj2);
1312 error = sme_event_register(dict,
1313 edata,
1314 sme->sme_name,
1315 "critical-capacity",
1316 critval,
1317 PENVSYS_EVENT_BATT_USERCAP,
1318 sdt[i].crittype);
1319 break;
1320 }
1321
1322 /* did the user want to set a critical max event? */
1323 obj2 = prop_dictionary_get(udict, "critical-max-limit");
1324 if (obj2 != NULL) {
1325 targetfound = true;
1326 if (edata->units == ENVSYS_INDICATOR ||
1327 edata->flags & ENVSYS_FMONNOTSUPP) {
1328 error = ENOTSUP;
1329 break;
1330 }
1331
1332 critval = prop_number_integer_value(obj2);
1333 error = sme_event_register(dict,
1334 edata,
1335 sme->sme_name,
1336 "critical-max-limit",
1337 critval,
1338 PENVSYS_EVENT_USER_CRITMAX,
1339 sdt[i].crittype);
1340 break;
1341 }
1342
1343 /* did the user want to set a critical min event? */
1344 obj2 = prop_dictionary_get(udict, "critical-min-limit");
1345 if (obj2 != NULL) {
1346 targetfound = true;
1347 if (edata->units == ENVSYS_INDICATOR ||
1348 edata->flags & ENVSYS_FMONNOTSUPP) {
1349 error = ENOTSUP;
1350 break;
1351 }
1352
1353 critval = prop_number_integer_value(obj2);
1354 error = sme_event_register(dict,
1355 edata,
1356 sme->sme_name,
1357 "critical-min-limit",
1358 critval,
1359 PENVSYS_EVENT_USER_CRITMIN,
1360 sdt[i].crittype);
1361 break;
1362 }
1363 }
1364
1365 /* invalid target? return the error */
1366 if (!targetfound)
1367 error = EINVAL;
1368
1369 return error;
1370 }
1371