1 /* $NetBSD: sysmon_envsys.c,v 1.127 2014/08/10 16:44:36 tls Exp $ */ 2 3 /*- 4 * Copyright (c) 2007, 2008 Juan Romero Pardines. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 /*- 29 * Copyright (c) 2000 Zembu Labs, Inc. 30 * All rights reserved. 31 * 32 * Author: Jason R. Thorpe <thorpej@zembu.com> 33 * 34 * Redistribution and use in source and binary forms, with or without 35 * modification, are permitted provided that the following conditions 36 * are met: 37 * 1. Redistributions of source code must retain the above copyright 38 * notice, this list of conditions and the following disclaimer. 39 * 2. Redistributions in binary form must reproduce the above copyright 40 * notice, this list of conditions and the following disclaimer in the 41 * documentation and/or other materials provided with the distribution. 42 * 3. All advertising materials mentioning features or use of this software 43 * must display the following acknowledgement: 44 * This product includes software developed by Zembu Labs, Inc. 45 * 4. Neither the name of Zembu Labs nor the names of its employees may 46 * be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS 50 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR- 51 * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS- 52 * CLAIMED. IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT, 53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 61 /* 62 * Environmental sensor framework for sysmon, exported to userland 63 * with proplib(3). 64 */ 65 66 #include <sys/cdefs.h> 67 __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.127 2014/08/10 16:44:36 tls Exp $"); 68 69 #include <sys/param.h> 70 #include <sys/types.h> 71 #include <sys/conf.h> 72 #include <sys/errno.h> 73 #include <sys/fcntl.h> 74 #include <sys/kernel.h> 75 #include <sys/systm.h> 76 #include <sys/proc.h> 77 #include <sys/mutex.h> 78 #include <sys/kmem.h> 79 #include <sys/rnd.h> 80 81 #include <dev/sysmon/sysmonvar.h> 82 #include <dev/sysmon/sysmon_envsysvar.h> 83 #include <dev/sysmon/sysmon_taskq.h> 84 85 kmutex_t sme_global_mtx; 86 87 prop_dictionary_t sme_propd; 88 89 struct sysmon_envsys_lh sysmon_envsys_list; 90 91 static uint32_t sysmon_envsys_next_sensor_index; 92 static struct sysmon_envsys *sysmon_envsys_find_40(u_int); 93 94 static void sysmon_envsys_destroy_plist(prop_array_t); 95 static void sme_remove_userprops(void); 96 static int sme_add_property_dictionary(struct sysmon_envsys *, prop_array_t, 97 prop_dictionary_t); 98 static sme_event_drv_t * sme_add_sensor_dictionary(struct sysmon_envsys *, 99 prop_array_t, prop_dictionary_t, envsys_data_t *); 100 static void sme_initial_refresh(void *); 101 static uint32_t sme_get_max_value(struct sysmon_envsys *, 102 bool (*)(const envsys_data_t*), bool); 103 104 /* 105 * sysmon_envsys_init: 106 * 107 * + Initialize global mutex, dictionary and the linked list. 108 */ 109 void 110 sysmon_envsys_init(void) 111 { 112 LIST_INIT(&sysmon_envsys_list); 113 mutex_init(&sme_global_mtx, MUTEX_DEFAULT, IPL_NONE); 114 sme_propd = prop_dictionary_create(); 115 } 116 117 /* 118 * sysmonopen_envsys: 119 * 120 * + Open the system monitor device. 121 */ 122 int 123 sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l) 124 { 125 return 0; 126 } 127 128 /* 129 * sysmonclose_envsys: 130 * 131 * + Close the system monitor device. 132 */ 133 int 134 sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l) 135 { 136 return 0; 137 } 138 139 /* 140 * sysmonioctl_envsys: 141 * 142 * + Perform a sysmon envsys control request. 143 */ 144 int 145 sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 146 { 147 struct sysmon_envsys *sme = NULL; 148 int error = 0; 149 u_int oidx; 150 151 switch (cmd) { 152 /* 153 * To update the global dictionary with latest data from devices. 154 */ 155 case ENVSYS_GETDICTIONARY: 156 { 157 struct plistref *plist = (struct plistref *)data; 158 159 /* 160 * Update dictionaries on all sysmon envsys devices 161 * registered. 162 */ 163 mutex_enter(&sme_global_mtx); 164 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 165 sysmon_envsys_acquire(sme, false); 166 error = sme_update_dictionary(sme); 167 if (error) { 168 DPRINTF(("%s: sme_update_dictionary, " 169 "error=%d\n", __func__, error)); 170 sysmon_envsys_release(sme, false); 171 mutex_exit(&sme_global_mtx); 172 return error; 173 } 174 sysmon_envsys_release(sme, false); 175 } 176 mutex_exit(&sme_global_mtx); 177 /* 178 * Copy global dictionary to userland. 179 */ 180 error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd); 181 break; 182 } 183 /* 184 * To set properties on multiple devices. 185 */ 186 case ENVSYS_SETDICTIONARY: 187 { 188 const struct plistref *plist = (const struct plistref *)data; 189 prop_dictionary_t udict; 190 prop_object_iterator_t iter, iter2; 191 prop_object_t obj, obj2; 192 prop_array_t array_u, array_k; 193 const char *devname = NULL; 194 195 if ((flag & FWRITE) == 0) 196 return EPERM; 197 198 /* 199 * Get dictionary from userland. 200 */ 201 error = prop_dictionary_copyin_ioctl(plist, cmd, &udict); 202 if (error) { 203 DPRINTF(("%s: copyin_ioctl error=%d\n", 204 __func__, error)); 205 break; 206 } 207 208 iter = prop_dictionary_iterator(udict); 209 if (!iter) { 210 prop_object_release(udict); 211 return ENOMEM; 212 } 213 214 /* 215 * Iterate over the userland dictionary and process 216 * the list of devices. 217 */ 218 while ((obj = prop_object_iterator_next(iter))) { 219 array_u = prop_dictionary_get_keysym(udict, obj); 220 if (prop_object_type(array_u) != PROP_TYPE_ARRAY) { 221 prop_object_iterator_release(iter); 222 prop_object_release(udict); 223 return EINVAL; 224 } 225 226 devname = prop_dictionary_keysym_cstring_nocopy(obj); 227 DPRINTF(("%s: processing the '%s' array requests\n", 228 __func__, devname)); 229 230 /* 231 * find the correct sme device. 232 */ 233 sme = sysmon_envsys_find(devname); 234 if (!sme) { 235 DPRINTF(("%s: NULL sme\n", __func__)); 236 prop_object_iterator_release(iter); 237 prop_object_release(udict); 238 return EINVAL; 239 } 240 241 /* 242 * Find the correct array object with the string 243 * supplied by the userland dictionary. 244 */ 245 array_k = prop_dictionary_get(sme_propd, devname); 246 if (prop_object_type(array_k) != PROP_TYPE_ARRAY) { 247 DPRINTF(("%s: array device failed\n", 248 __func__)); 249 sysmon_envsys_release(sme, false); 250 prop_object_iterator_release(iter); 251 prop_object_release(udict); 252 return EINVAL; 253 } 254 255 iter2 = prop_array_iterator(array_u); 256 if (!iter2) { 257 sysmon_envsys_release(sme, false); 258 prop_object_iterator_release(iter); 259 prop_object_release(udict); 260 return ENOMEM; 261 } 262 263 /* 264 * Iterate over the array of dictionaries to 265 * process the list of sensors and properties. 266 */ 267 while ((obj2 = prop_object_iterator_next(iter2))) { 268 /* 269 * do the real work now. 270 */ 271 error = sme_userset_dictionary(sme, 272 obj2, 273 array_k); 274 if (error) { 275 sysmon_envsys_release(sme, false); 276 prop_object_iterator_release(iter2); 277 prop_object_iterator_release(iter); 278 prop_object_release(udict); 279 return error; 280 } 281 } 282 283 sysmon_envsys_release(sme, false); 284 prop_object_iterator_release(iter2); 285 } 286 287 prop_object_iterator_release(iter); 288 prop_object_release(udict); 289 break; 290 } 291 /* 292 * To remove all properties from all devices registered. 293 */ 294 case ENVSYS_REMOVEPROPS: 295 { 296 const struct plistref *plist = (const struct plistref *)data; 297 prop_dictionary_t udict; 298 prop_object_t obj; 299 300 if ((flag & FWRITE) == 0) 301 return EPERM; 302 303 error = prop_dictionary_copyin_ioctl(plist, cmd, &udict); 304 if (error) { 305 DPRINTF(("%s: copyin_ioctl error=%d\n", 306 __func__, error)); 307 break; 308 } 309 310 obj = prop_dictionary_get(udict, "envsys-remove-props"); 311 if (!obj || !prop_bool_true(obj)) { 312 DPRINTF(("%s: invalid 'envsys-remove-props'\n", 313 __func__)); 314 return EINVAL; 315 } 316 317 prop_object_release(udict); 318 sme_remove_userprops(); 319 320 break; 321 } 322 /* 323 * Compatibility ioctls with the old interface, only implemented 324 * ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough to make old 325 * applications work. 326 */ 327 case ENVSYS_GTREDATA: 328 { 329 struct envsys_tre_data *tred = (void *)data; 330 envsys_data_t *edata = NULL; 331 bool found = false; 332 333 tred->validflags = 0; 334 335 sme = sysmon_envsys_find_40(tred->sensor); 336 if (!sme) 337 break; 338 339 oidx = tred->sensor; 340 tred->sensor = SME_SENSOR_IDX(sme, tred->sensor); 341 342 DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n", 343 __func__, tred->sensor, oidx, sme->sme_name, 344 sme->sme_nsensors)); 345 346 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 347 if (edata->sensor == tred->sensor) { 348 found = true; 349 break; 350 } 351 } 352 353 if (!found) { 354 sysmon_envsys_release(sme, false); 355 error = ENODEV; 356 break; 357 } 358 359 if (tred->sensor < sme->sme_nsensors) { 360 if ((sme->sme_flags & SME_POLL_ONLY) == 0) { 361 mutex_enter(&sme->sme_mtx); 362 sysmon_envsys_refresh_sensor(sme, edata); 363 mutex_exit(&sme->sme_mtx); 364 } 365 366 /* 367 * copy required values to the old interface. 368 */ 369 tred->sensor = edata->sensor; 370 tred->cur.data_us = edata->value_cur; 371 tred->cur.data_s = edata->value_cur; 372 tred->max.data_us = edata->value_max; 373 tred->max.data_s = edata->value_max; 374 tred->min.data_us = edata->value_min; 375 tred->min.data_s = edata->value_min; 376 tred->avg.data_us = 0; 377 tred->avg.data_s = 0; 378 if (edata->units == ENVSYS_BATTERY_CHARGE) 379 tred->units = ENVSYS_INDICATOR; 380 else 381 tred->units = edata->units; 382 383 tred->validflags |= ENVSYS_FVALID; 384 tred->validflags |= ENVSYS_FCURVALID; 385 386 if (edata->flags & ENVSYS_FPERCENT) { 387 tred->validflags |= ENVSYS_FMAXVALID; 388 tred->validflags |= ENVSYS_FFRACVALID; 389 } 390 391 if (edata->state == ENVSYS_SINVALID) { 392 tred->validflags &= ~ENVSYS_FCURVALID; 393 tred->cur.data_us = tred->cur.data_s = 0; 394 } 395 396 DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n", 397 __func__, edata->desc, tred->cur.data_s)); 398 DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d" 399 " tred->sensor=%d\n", __func__, tred->validflags, 400 tred->units, tred->sensor)); 401 } 402 tred->sensor = oidx; 403 sysmon_envsys_release(sme, false); 404 405 break; 406 } 407 case ENVSYS_GTREINFO: 408 { 409 struct envsys_basic_info *binfo = (void *)data; 410 envsys_data_t *edata = NULL; 411 bool found = false; 412 413 binfo->validflags = 0; 414 415 sme = sysmon_envsys_find_40(binfo->sensor); 416 if (!sme) 417 break; 418 419 oidx = binfo->sensor; 420 binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor); 421 422 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 423 if (edata->sensor == binfo->sensor) { 424 found = true; 425 break; 426 } 427 } 428 429 if (!found) { 430 sysmon_envsys_release(sme, false); 431 error = ENODEV; 432 break; 433 } 434 435 binfo->validflags |= ENVSYS_FVALID; 436 437 if (binfo->sensor < sme->sme_nsensors) { 438 if (edata->units == ENVSYS_BATTERY_CHARGE) 439 binfo->units = ENVSYS_INDICATOR; 440 else 441 binfo->units = edata->units; 442 443 /* 444 * previously, the ACPI sensor names included the 445 * device name. Include that in compatibility code. 446 */ 447 if (strncmp(sme->sme_name, "acpi", 4) == 0) 448 (void)snprintf(binfo->desc, sizeof(binfo->desc), 449 "%s %s", sme->sme_name, edata->desc); 450 else 451 (void)strlcpy(binfo->desc, edata->desc, 452 sizeof(binfo->desc)); 453 } 454 455 DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n", 456 __func__, binfo->units, binfo->validflags)); 457 DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n", 458 __func__, binfo->desc, binfo->sensor)); 459 460 binfo->sensor = oidx; 461 sysmon_envsys_release(sme, false); 462 463 break; 464 } 465 default: 466 error = ENOTTY; 467 break; 468 } 469 470 return error; 471 } 472 473 /* 474 * sysmon_envsys_create: 475 * 476 * + Allocates a new sysmon_envsys object and initializes the 477 * stuff for sensors and events. 478 */ 479 struct sysmon_envsys * 480 sysmon_envsys_create(void) 481 { 482 struct sysmon_envsys *sme; 483 484 sme = kmem_zalloc(sizeof(*sme), KM_SLEEP); 485 TAILQ_INIT(&sme->sme_sensors_list); 486 LIST_INIT(&sme->sme_events_list); 487 mutex_init(&sme->sme_mtx, MUTEX_DEFAULT, IPL_NONE); 488 cv_init(&sme->sme_condvar, "sme_wait"); 489 490 return sme; 491 } 492 493 /* 494 * sysmon_envsys_destroy: 495 * 496 * + Removes all sensors from the tail queue, destroys the callout 497 * and frees the sysmon_envsys object. 498 */ 499 void 500 sysmon_envsys_destroy(struct sysmon_envsys *sme) 501 { 502 envsys_data_t *edata; 503 504 KASSERT(sme != NULL); 505 506 while (!TAILQ_EMPTY(&sme->sme_sensors_list)) { 507 edata = TAILQ_FIRST(&sme->sme_sensors_list); 508 TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head); 509 } 510 mutex_destroy(&sme->sme_mtx); 511 cv_destroy(&sme->sme_condvar); 512 kmem_free(sme, sizeof(*sme)); 513 } 514 515 /* 516 * sysmon_envsys_sensor_attach: 517 * 518 * + Attaches a sensor into a sysmon_envsys device checking that units 519 * is set to a valid type and description is unique and not empty. 520 */ 521 int 522 sysmon_envsys_sensor_attach(struct sysmon_envsys *sme, envsys_data_t *edata) 523 { 524 const struct sme_descr_entry *sdt_units; 525 envsys_data_t *oedata; 526 527 KASSERT(sme != NULL || edata != NULL); 528 529 /* 530 * Find the correct units for this sensor. 531 */ 532 sdt_units = sme_find_table_entry(SME_DESC_UNITS, edata->units); 533 if (sdt_units->type == -1) 534 return EINVAL; 535 536 /* 537 * Check that description is not empty or duplicate. 538 */ 539 if (strlen(edata->desc) == 0) 540 return EINVAL; 541 542 mutex_enter(&sme->sme_mtx); 543 sysmon_envsys_acquire(sme, true); 544 TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) { 545 if (strcmp(oedata->desc, edata->desc) == 0) { 546 sysmon_envsys_release(sme, true); 547 mutex_exit(&sme->sme_mtx); 548 return EEXIST; 549 } 550 } 551 /* 552 * Ok, the sensor has been added into the device queue. 553 */ 554 TAILQ_INSERT_TAIL(&sme->sme_sensors_list, edata, sensors_head); 555 556 /* 557 * Give the sensor an index position. 558 */ 559 edata->sensor = sme->sme_nsensors; 560 sme->sme_nsensors++; 561 sysmon_envsys_release(sme, true); 562 mutex_exit(&sme->sme_mtx); 563 564 DPRINTF(("%s: attached #%d (%s), units=%d (%s)\n", 565 __func__, edata->sensor, edata->desc, 566 sdt_units->type, sdt_units->desc)); 567 568 return 0; 569 } 570 571 /* 572 * sysmon_envsys_sensor_detach: 573 * 574 * + Detachs a sensor from a sysmon_envsys device and decrements the 575 * sensors count on success. 576 */ 577 int 578 sysmon_envsys_sensor_detach(struct sysmon_envsys *sme, envsys_data_t *edata) 579 { 580 envsys_data_t *oedata; 581 bool found = false; 582 583 KASSERT(sme != NULL || edata != NULL); 584 585 /* 586 * Check the sensor is already on the list. 587 */ 588 mutex_enter(&sme->sme_mtx); 589 sysmon_envsys_acquire(sme, true); 590 TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) { 591 if (oedata->sensor == edata->sensor) { 592 found = true; 593 break; 594 } 595 } 596 597 if (!found) { 598 sysmon_envsys_release(sme, true); 599 mutex_exit(&sme->sme_mtx); 600 return EINVAL; 601 } 602 603 /* 604 * remove it, unhook from rnd(4), and decrement the sensors count. 605 */ 606 sme_event_unregister_sensor(sme, edata); 607 TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head); 608 sme->sme_nsensors--; 609 sysmon_envsys_release(sme, true); 610 mutex_exit(&sme->sme_mtx); 611 612 return 0; 613 } 614 615 616 /* 617 * sysmon_envsys_register: 618 * 619 * + Register a sysmon envsys device. 620 * + Create array of dictionaries for a device. 621 */ 622 int 623 sysmon_envsys_register(struct sysmon_envsys *sme) 624 { 625 struct sme_evdrv { 626 SLIST_ENTRY(sme_evdrv) evdrv_head; 627 sme_event_drv_t *evdrv; 628 }; 629 SLIST_HEAD(, sme_evdrv) sme_evdrv_list; 630 struct sme_evdrv *evdv = NULL; 631 struct sysmon_envsys *lsme; 632 prop_array_t array = NULL; 633 prop_dictionary_t dict, dict2; 634 envsys_data_t *edata = NULL; 635 sme_event_drv_t *this_evdrv; 636 int nevent; 637 int error = 0; 638 char rnd_name[sizeof(edata->rnd_src.name)]; 639 640 KASSERT(sme != NULL); 641 KASSERT(sme->sme_name != NULL); 642 643 /* 644 * Check if requested sysmon_envsys device is valid 645 * and does not exist already in the list. 646 */ 647 mutex_enter(&sme_global_mtx); 648 LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) { 649 if (strcmp(lsme->sme_name, sme->sme_name) == 0) { 650 mutex_exit(&sme_global_mtx); 651 return EEXIST; 652 } 653 } 654 mutex_exit(&sme_global_mtx); 655 656 /* 657 * sanity check: if SME_DISABLE_REFRESH is not set, 658 * the sme_refresh function callback must be non NULL. 659 */ 660 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) 661 if (!sme->sme_refresh) 662 return EINVAL; 663 664 /* 665 * If the list of sensors is empty, there's no point to continue... 666 */ 667 if (TAILQ_EMPTY(&sme->sme_sensors_list)) { 668 DPRINTF(("%s: sensors list empty for %s\n", __func__, 669 sme->sme_name)); 670 return ENOTSUP; 671 } 672 673 /* 674 * Initialize the singly linked list for driver events. 675 */ 676 SLIST_INIT(&sme_evdrv_list); 677 678 array = prop_array_create(); 679 if (!array) 680 return ENOMEM; 681 682 /* 683 * Iterate over all sensors and create a dictionary per sensor. 684 * We must respect the order in which the sensors were added. 685 */ 686 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 687 dict = prop_dictionary_create(); 688 if (!dict) { 689 error = ENOMEM; 690 goto out2; 691 } 692 693 /* 694 * Create all objects in sensor's dictionary. 695 */ 696 this_evdrv = sme_add_sensor_dictionary(sme, array, 697 dict, edata); 698 if (this_evdrv) { 699 evdv = kmem_zalloc(sizeof(*evdv), KM_SLEEP); 700 evdv->evdrv = this_evdrv; 701 SLIST_INSERT_HEAD(&sme_evdrv_list, evdv, evdrv_head); 702 } 703 } 704 705 /* 706 * If the array does not contain any object (sensor), there's 707 * no need to attach the driver. 708 */ 709 if (prop_array_count(array) == 0) { 710 error = EINVAL; 711 DPRINTF(("%s: empty array for '%s'\n", __func__, 712 sme->sme_name)); 713 goto out; 714 } 715 716 /* 717 * Add the dictionary for the global properties of this device. 718 */ 719 dict2 = prop_dictionary_create(); 720 if (!dict2) { 721 error = ENOMEM; 722 goto out; 723 } 724 725 error = sme_add_property_dictionary(sme, array, dict2); 726 if (error) { 727 prop_object_release(dict2); 728 goto out; 729 } 730 731 /* 732 * Add the array into the global dictionary for the driver. 733 * 734 * <dict> 735 * <key>foo0</key> 736 * <array> 737 * ... 738 */ 739 mutex_enter(&sme_global_mtx); 740 if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) { 741 error = EINVAL; 742 DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__, 743 sme->sme_name)); 744 goto out; 745 } 746 747 /* 748 * Add the device into the list. 749 */ 750 LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list); 751 sme->sme_fsensor = sysmon_envsys_next_sensor_index; 752 sysmon_envsys_next_sensor_index += sme->sme_nsensors; 753 mutex_exit(&sme_global_mtx); 754 755 out: 756 /* 757 * No errors? Make an initial data refresh if was requested, 758 * then register the events that were set in the driver. Do 759 * the refresh first in case it is needed to establish the 760 * limits or max_value needed by some events. 761 */ 762 if (error == 0) { 763 nevent = 0; 764 sysmon_task_queue_init(); 765 766 if (sme->sme_flags & SME_INIT_REFRESH) { 767 sysmon_task_queue_sched(0, sme_initial_refresh, sme); 768 DPRINTF(("%s: scheduled initial refresh for '%s'\n", 769 __func__, sme->sme_name)); 770 } 771 SLIST_FOREACH(evdv, &sme_evdrv_list, evdrv_head) { 772 sysmon_task_queue_sched(0, 773 sme_event_drvadd, evdv->evdrv); 774 nevent++; 775 } 776 /* 777 * Hook the sensor into rnd(4) entropy pool if requested 778 */ 779 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 780 if (edata->flags & ENVSYS_FHAS_ENTROPY) { 781 uint32_t rnd_type, rnd_flag = 0; 782 size_t n; 783 int tail = 1; 784 785 snprintf(rnd_name, sizeof(rnd_name), "%s-%s", 786 sme->sme_name, edata->desc); 787 n = strlen(rnd_name); 788 /* 789 * 1) Remove trailing white space(s). 790 * 2) If space exist, replace it with '-' 791 */ 792 while (--n) { 793 if (rnd_name[n] == ' ') { 794 if (tail != 0) 795 rnd_name[n] = '\0'; 796 else 797 rnd_name[n] = '-'; 798 } else 799 tail = 0; 800 } 801 rnd_flag |= RND_FLAG_COLLECT_TIME; 802 rnd_flag |= RND_FLAG_ESTIMATE_TIME; 803 804 switch (edata->units) { 805 case ENVSYS_STEMP: 806 case ENVSYS_SFANRPM: 807 case ENVSYS_INTEGER: 808 rnd_type = RND_TYPE_ENV; 809 rnd_flag |= RND_FLAG_COLLECT_VALUE; 810 rnd_flag |= RND_FLAG_ESTIMATE_VALUE; 811 break; 812 case ENVSYS_SVOLTS_AC: 813 case ENVSYS_SVOLTS_DC: 814 case ENVSYS_SOHMS: 815 case ENVSYS_SWATTS: 816 case ENVSYS_SAMPS: 817 case ENVSYS_SWATTHOUR: 818 case ENVSYS_SAMPHOUR: 819 rnd_type = RND_TYPE_POWER; 820 rnd_flag |= RND_FLAG_COLLECT_VALUE; 821 rnd_flag |= RND_FLAG_ESTIMATE_VALUE; 822 break; 823 default: 824 rnd_type = RND_TYPE_UNKNOWN; 825 break; 826 } 827 rnd_attach_source(&edata->rnd_src, rnd_name, 828 rnd_type, rnd_flag); 829 } 830 } 831 DPRINTF(("%s: driver '%s' registered (nsens=%d nevent=%d)\n", 832 __func__, sme->sme_name, sme->sme_nsensors, nevent)); 833 } 834 835 out2: 836 while (!SLIST_EMPTY(&sme_evdrv_list)) { 837 evdv = SLIST_FIRST(&sme_evdrv_list); 838 SLIST_REMOVE_HEAD(&sme_evdrv_list, evdrv_head); 839 kmem_free(evdv, sizeof(*evdv)); 840 } 841 if (!error) 842 return 0; 843 844 /* 845 * Ugh... something wasn't right; unregister all events and sensors 846 * previously assigned and destroy the array with all its objects. 847 */ 848 DPRINTF(("%s: failed to register '%s' (%d)\n", __func__, 849 sme->sme_name, error)); 850 851 sme_event_unregister_all(sme); 852 while (!TAILQ_EMPTY(&sme->sme_sensors_list)) { 853 edata = TAILQ_FIRST(&sme->sme_sensors_list); 854 TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head); 855 } 856 sysmon_envsys_destroy_plist(array); 857 return error; 858 } 859 860 /* 861 * sysmon_envsys_destroy_plist: 862 * 863 * + Remove all objects from the array of dictionaries that is 864 * created in a sysmon envsys device. 865 */ 866 static void 867 sysmon_envsys_destroy_plist(prop_array_t array) 868 { 869 prop_object_iterator_t iter, iter2; 870 prop_dictionary_t dict; 871 prop_object_t obj; 872 873 KASSERT(array != NULL); 874 KASSERT(prop_object_type(array) == PROP_TYPE_ARRAY); 875 876 DPRINTFOBJ(("%s: objects in array=%d\n", __func__, 877 prop_array_count(array))); 878 879 iter = prop_array_iterator(array); 880 if (!iter) 881 return; 882 883 while ((dict = prop_object_iterator_next(iter))) { 884 KASSERT(prop_object_type(dict) == PROP_TYPE_DICTIONARY); 885 iter2 = prop_dictionary_iterator(dict); 886 if (!iter2) 887 goto out; 888 DPRINTFOBJ(("%s: iterating over dictionary\n", __func__)); 889 while ((obj = prop_object_iterator_next(iter2)) != NULL) { 890 DPRINTFOBJ(("%s: obj=%s\n", __func__, 891 prop_dictionary_keysym_cstring_nocopy(obj))); 892 prop_dictionary_remove(dict, 893 prop_dictionary_keysym_cstring_nocopy(obj)); 894 prop_object_iterator_reset(iter2); 895 } 896 prop_object_iterator_release(iter2); 897 DPRINTFOBJ(("%s: objects in dictionary:%d\n", 898 __func__, prop_dictionary_count(dict))); 899 prop_object_release(dict); 900 } 901 902 out: 903 prop_object_iterator_release(iter); 904 prop_object_release(array); 905 } 906 907 /* 908 * sysmon_envsys_unregister: 909 * 910 * + Unregister a sysmon envsys device. 911 */ 912 void 913 sysmon_envsys_unregister(struct sysmon_envsys *sme) 914 { 915 prop_array_t array; 916 struct sysmon_envsys *osme; 917 918 KASSERT(sme != NULL); 919 920 /* 921 * Unregister all events associated with device. 922 */ 923 sme_event_unregister_all(sme); 924 /* 925 * Decrement global sensors counter and the first_sensor index 926 * for remaining devices in the list (only used for compatibility 927 * with previous API), and remove the device from the list. 928 */ 929 mutex_enter(&sme_global_mtx); 930 sysmon_envsys_next_sensor_index -= sme->sme_nsensors; 931 LIST_FOREACH(osme, &sysmon_envsys_list, sme_list) { 932 if (osme->sme_fsensor >= sme->sme_fsensor) 933 osme->sme_fsensor -= sme->sme_nsensors; 934 } 935 LIST_REMOVE(sme, sme_list); 936 mutex_exit(&sme_global_mtx); 937 938 /* 939 * Remove the device (and all its objects) from the global dictionary. 940 */ 941 array = prop_dictionary_get(sme_propd, sme->sme_name); 942 if (array && prop_object_type(array) == PROP_TYPE_ARRAY) { 943 mutex_enter(&sme_global_mtx); 944 prop_dictionary_remove(sme_propd, sme->sme_name); 945 mutex_exit(&sme_global_mtx); 946 sysmon_envsys_destroy_plist(array); 947 } 948 /* 949 * And finally destroy the sysmon_envsys object. 950 */ 951 sysmon_envsys_destroy(sme); 952 } 953 954 /* 955 * sysmon_envsys_find: 956 * 957 * + Find a sysmon envsys device and mark it as busy 958 * once it's available. 959 */ 960 struct sysmon_envsys * 961 sysmon_envsys_find(const char *name) 962 { 963 struct sysmon_envsys *sme; 964 965 mutex_enter(&sme_global_mtx); 966 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 967 if (strcmp(sme->sme_name, name) == 0) { 968 sysmon_envsys_acquire(sme, false); 969 break; 970 } 971 } 972 mutex_exit(&sme_global_mtx); 973 974 return sme; 975 } 976 977 /* 978 * Compatibility function with the old API. 979 */ 980 struct sysmon_envsys * 981 sysmon_envsys_find_40(u_int idx) 982 { 983 struct sysmon_envsys *sme; 984 985 mutex_enter(&sme_global_mtx); 986 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 987 if (idx >= sme->sme_fsensor && 988 idx < (sme->sme_fsensor + sme->sme_nsensors)) { 989 sysmon_envsys_acquire(sme, false); 990 break; 991 } 992 } 993 mutex_exit(&sme_global_mtx); 994 995 return sme; 996 } 997 998 /* 999 * sysmon_envsys_acquire: 1000 * 1001 * + Wait until a sysmon envsys device is available and mark 1002 * it as busy. 1003 */ 1004 void 1005 sysmon_envsys_acquire(struct sysmon_envsys *sme, bool locked) 1006 { 1007 KASSERT(sme != NULL); 1008 1009 if (locked) { 1010 while (sme->sme_flags & SME_FLAG_BUSY) 1011 cv_wait(&sme->sme_condvar, &sme->sme_mtx); 1012 sme->sme_flags |= SME_FLAG_BUSY; 1013 } else { 1014 mutex_enter(&sme->sme_mtx); 1015 while (sme->sme_flags & SME_FLAG_BUSY) 1016 cv_wait(&sme->sme_condvar, &sme->sme_mtx); 1017 sme->sme_flags |= SME_FLAG_BUSY; 1018 mutex_exit(&sme->sme_mtx); 1019 } 1020 } 1021 1022 /* 1023 * sysmon_envsys_release: 1024 * 1025 * + Unmark a sysmon envsys device as busy, and notify 1026 * waiters. 1027 */ 1028 void 1029 sysmon_envsys_release(struct sysmon_envsys *sme, bool locked) 1030 { 1031 KASSERT(sme != NULL); 1032 1033 if (locked) { 1034 sme->sme_flags &= ~SME_FLAG_BUSY; 1035 cv_broadcast(&sme->sme_condvar); 1036 } else { 1037 mutex_enter(&sme->sme_mtx); 1038 sme->sme_flags &= ~SME_FLAG_BUSY; 1039 cv_broadcast(&sme->sme_condvar); 1040 mutex_exit(&sme->sme_mtx); 1041 } 1042 } 1043 1044 /* 1045 * sme_initial_refresh: 1046 * 1047 * + Do an initial refresh of the sensors in a device just after 1048 * interrupts are enabled in the autoconf(9) process. 1049 * 1050 */ 1051 static void 1052 sme_initial_refresh(void *arg) 1053 { 1054 struct sysmon_envsys *sme = arg; 1055 envsys_data_t *edata; 1056 1057 mutex_enter(&sme->sme_mtx); 1058 sysmon_envsys_acquire(sme, true); 1059 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) 1060 sysmon_envsys_refresh_sensor(sme, edata); 1061 sysmon_envsys_release(sme, true); 1062 mutex_exit(&sme->sme_mtx); 1063 } 1064 1065 /* 1066 * sme_sensor_dictionary_get: 1067 * 1068 * + Returns a dictionary of a device specified by its index 1069 * position. 1070 */ 1071 prop_dictionary_t 1072 sme_sensor_dictionary_get(prop_array_t array, const char *index) 1073 { 1074 prop_object_iterator_t iter; 1075 prop_dictionary_t dict; 1076 prop_object_t obj; 1077 1078 KASSERT(array != NULL || index != NULL); 1079 1080 iter = prop_array_iterator(array); 1081 if (!iter) 1082 return NULL; 1083 1084 while ((dict = prop_object_iterator_next(iter))) { 1085 obj = prop_dictionary_get(dict, "index"); 1086 if (prop_string_equals_cstring(obj, index)) 1087 break; 1088 } 1089 1090 prop_object_iterator_release(iter); 1091 return dict; 1092 } 1093 1094 /* 1095 * sme_remove_userprops: 1096 * 1097 * + Remove all properties from all devices that were set by 1098 * the ENVSYS_SETDICTIONARY ioctl. 1099 */ 1100 static void 1101 sme_remove_userprops(void) 1102 { 1103 struct sysmon_envsys *sme; 1104 prop_array_t array; 1105 prop_dictionary_t sdict; 1106 envsys_data_t *edata = NULL; 1107 char tmp[ENVSYS_DESCLEN]; 1108 char rnd_name[sizeof(edata->rnd_src.name)]; 1109 sysmon_envsys_lim_t lims; 1110 const struct sme_descr_entry *sdt_units; 1111 uint32_t props; 1112 int ptype; 1113 1114 mutex_enter(&sme_global_mtx); 1115 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 1116 sysmon_envsys_acquire(sme, false); 1117 array = prop_dictionary_get(sme_propd, sme->sme_name); 1118 1119 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 1120 (void)snprintf(tmp, sizeof(tmp), "sensor%d", 1121 edata->sensor); 1122 sdict = sme_sensor_dictionary_get(array, tmp); 1123 KASSERT(sdict != NULL); 1124 1125 ptype = 0; 1126 if (edata->upropset & PROP_BATTCAP) { 1127 prop_dictionary_remove(sdict, 1128 "critical-capacity"); 1129 ptype = PENVSYS_EVENT_CAPACITY; 1130 } 1131 1132 if (edata->upropset & PROP_BATTWARN) { 1133 prop_dictionary_remove(sdict, 1134 "warning-capacity"); 1135 ptype = PENVSYS_EVENT_CAPACITY; 1136 } 1137 1138 if (edata->upropset & PROP_BATTHIGH) { 1139 prop_dictionary_remove(sdict, 1140 "high-capacity"); 1141 ptype = PENVSYS_EVENT_CAPACITY; 1142 } 1143 1144 if (edata->upropset & PROP_BATTMAX) { 1145 prop_dictionary_remove(sdict, 1146 "maximum-capacity"); 1147 ptype = PENVSYS_EVENT_CAPACITY; 1148 } 1149 if (edata->upropset & PROP_WARNMAX) { 1150 prop_dictionary_remove(sdict, "warning-max"); 1151 ptype = PENVSYS_EVENT_LIMITS; 1152 } 1153 1154 if (edata->upropset & PROP_WARNMIN) { 1155 prop_dictionary_remove(sdict, "warning-min"); 1156 ptype = PENVSYS_EVENT_LIMITS; 1157 } 1158 1159 if (edata->upropset & PROP_CRITMAX) { 1160 prop_dictionary_remove(sdict, "critical-max"); 1161 ptype = PENVSYS_EVENT_LIMITS; 1162 } 1163 1164 if (edata->upropset & PROP_CRITMIN) { 1165 prop_dictionary_remove(sdict, "critical-min"); 1166 ptype = PENVSYS_EVENT_LIMITS; 1167 } 1168 if (edata->upropset & PROP_RFACT) { 1169 (void)sme_sensor_upint32(sdict, "rfact", 0); 1170 edata->rfact = 0; 1171 } 1172 1173 if (edata->upropset & PROP_DESC) 1174 (void)sme_sensor_upstring(sdict, 1175 "description", edata->desc); 1176 1177 if (ptype == 0) 1178 continue; 1179 1180 /* 1181 * If there were any limit values removed, we 1182 * need to revert to initial limits. 1183 * 1184 * First, tell the driver that we need it to 1185 * restore any h/w limits which may have been 1186 * changed to stored, boot-time values. 1187 */ 1188 if (sme->sme_set_limits) { 1189 DPRINTF(("%s: reset limits for %s %s\n", 1190 __func__, sme->sme_name, edata->desc)); 1191 (*sme->sme_set_limits)(sme, edata, NULL, NULL); 1192 } 1193 1194 /* 1195 * Next, we need to retrieve those initial limits. 1196 */ 1197 props = 0; 1198 edata->upropset &= ~PROP_LIMITS; 1199 if (sme->sme_get_limits) { 1200 DPRINTF(("%s: retrieve limits for %s %s\n", 1201 __func__, sme->sme_name, edata->desc)); 1202 lims = edata->limits; 1203 (*sme->sme_get_limits)(sme, edata, &lims, 1204 &props); 1205 } 1206 1207 /* 1208 * Finally, remove any old limits event, then 1209 * install a new event (which will update the 1210 * dictionary) 1211 */ 1212 sme_event_unregister(sme, edata->desc, 1213 PENVSYS_EVENT_LIMITS); 1214 1215 /* 1216 * Find the correct units for this sensor. 1217 */ 1218 sdt_units = sme_find_table_entry(SME_DESC_UNITS, 1219 edata->units); 1220 1221 if (props & PROP_LIMITS) { 1222 DPRINTF(("%s: install limits for %s %s\n", 1223 __func__, sme->sme_name, edata->desc)); 1224 1225 sme_event_register(sdict, edata, sme, 1226 &lims, props, PENVSYS_EVENT_LIMITS, 1227 sdt_units->crittype); 1228 } 1229 if (edata->flags & ENVSYS_FHAS_ENTROPY) { 1230 sme_event_register(sdict, edata, sme, 1231 &lims, props, PENVSYS_EVENT_NULL, 1232 sdt_units->crittype); 1233 snprintf(rnd_name, sizeof(rnd_name), "%s-%s", 1234 sme->sme_name, edata->desc); 1235 rnd_attach_source(&edata->rnd_src, rnd_name, 1236 RND_TYPE_ENV, RND_FLAG_COLLECT_VALUE| 1237 RND_FLAG_COLLECT_TIME| 1238 RND_FLAG_ESTIMATE_VALUE| 1239 RND_FLAG_ESTIMATE_TIME); 1240 } 1241 } 1242 1243 /* 1244 * Restore default timeout value. 1245 */ 1246 sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT; 1247 sme_schedule_callout(sme); 1248 sysmon_envsys_release(sme, false); 1249 } 1250 mutex_exit(&sme_global_mtx); 1251 } 1252 1253 /* 1254 * sme_add_property_dictionary: 1255 * 1256 * + Add global properties into a device. 1257 */ 1258 static int 1259 sme_add_property_dictionary(struct sysmon_envsys *sme, prop_array_t array, 1260 prop_dictionary_t dict) 1261 { 1262 prop_dictionary_t pdict; 1263 const char *class; 1264 int error = 0; 1265 1266 pdict = prop_dictionary_create(); 1267 if (!pdict) 1268 return EINVAL; 1269 1270 /* 1271 * Add the 'refresh-timeout' and 'dev-class' objects into the 1272 * 'device-properties' dictionary. 1273 * 1274 * ... 1275 * <dict> 1276 * <key>device-properties</key> 1277 * <dict> 1278 * <key>refresh-timeout</key> 1279 * <integer>120</integer< 1280 * <key>device-class</key> 1281 * <string>class_name</string> 1282 * </dict> 1283 * </dict> 1284 * ... 1285 * 1286 */ 1287 if (sme->sme_events_timeout == 0) { 1288 sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT; 1289 sme_schedule_callout(sme); 1290 } 1291 1292 if (!prop_dictionary_set_uint64(pdict, "refresh-timeout", 1293 sme->sme_events_timeout)) { 1294 error = EINVAL; 1295 goto out; 1296 } 1297 if (sme->sme_class == SME_CLASS_BATTERY) 1298 class = "battery"; 1299 else if (sme->sme_class == SME_CLASS_ACADAPTER) 1300 class = "ac-adapter"; 1301 else 1302 class = "other"; 1303 if (!prop_dictionary_set_cstring_nocopy(pdict, "device-class", class)) { 1304 error = EINVAL; 1305 goto out; 1306 } 1307 1308 if (!prop_dictionary_set(dict, "device-properties", pdict)) { 1309 error = EINVAL; 1310 goto out; 1311 } 1312 1313 /* 1314 * Add the device dictionary into the sysmon envsys array. 1315 */ 1316 if (!prop_array_add(array, dict)) 1317 error = EINVAL; 1318 1319 out: 1320 prop_object_release(pdict); 1321 return error; 1322 } 1323 1324 /* 1325 * sme_add_sensor_dictionary: 1326 * 1327 * + Adds the sensor objects into the dictionary and returns a pointer 1328 * to a sme_event_drv_t object if a monitoring flag was set 1329 * (or NULL otherwise). 1330 */ 1331 static sme_event_drv_t * 1332 sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array, 1333 prop_dictionary_t dict, envsys_data_t *edata) 1334 { 1335 const struct sme_descr_entry *sdt; 1336 int error; 1337 sme_event_drv_t *sme_evdrv_t = NULL; 1338 char indexstr[ENVSYS_DESCLEN]; 1339 bool mon_supported, allow_rfact; 1340 1341 /* 1342 * Add the index sensor string. 1343 * 1344 * ... 1345 * <key>index</eyr 1346 * <string>sensor0</string> 1347 * ... 1348 */ 1349 (void)snprintf(indexstr, sizeof(indexstr), "sensor%d", edata->sensor); 1350 if (sme_sensor_upstring(dict, "index", indexstr)) 1351 goto bad; 1352 1353 /* 1354 * ... 1355 * <key>description</key> 1356 * <string>blah blah</string> 1357 * ... 1358 */ 1359 if (sme_sensor_upstring(dict, "description", edata->desc)) 1360 goto bad; 1361 1362 /* 1363 * Add the monitoring boolean object: 1364 * 1365 * ... 1366 * <key>monitoring-supported</key> 1367 * <true/> 1368 * ... 1369 * 1370 * always false on Battery {capacity,charge}, Drive and Indicator types. 1371 * They cannot be monitored. 1372 * 1373 */ 1374 if ((edata->flags & ENVSYS_FMONNOTSUPP) || 1375 (edata->units == ENVSYS_INDICATOR) || 1376 (edata->units == ENVSYS_DRIVE) || 1377 (edata->units == ENVSYS_BATTERY_CAPACITY) || 1378 (edata->units == ENVSYS_BATTERY_CHARGE)) 1379 mon_supported = false; 1380 else 1381 mon_supported = true; 1382 if (sme_sensor_upbool(dict, "monitoring-supported", mon_supported)) 1383 goto out; 1384 1385 /* 1386 * Add the allow-rfact boolean object, true if 1387 * ENVSYS_FCHANGERFACT is set, false otherwise. 1388 * 1389 * ... 1390 * <key>allow-rfact</key> 1391 * <true/> 1392 * ... 1393 */ 1394 if (edata->units == ENVSYS_SVOLTS_DC || 1395 edata->units == ENVSYS_SVOLTS_AC) { 1396 if (edata->flags & ENVSYS_FCHANGERFACT) 1397 allow_rfact = true; 1398 else 1399 allow_rfact = false; 1400 if (sme_sensor_upbool(dict, "allow-rfact", allow_rfact)) 1401 goto out; 1402 } 1403 1404 error = sme_update_sensor_dictionary(dict, edata, 1405 (edata->state == ENVSYS_SVALID)); 1406 if (error < 0) 1407 goto bad; 1408 else if (error) 1409 goto out; 1410 1411 /* 1412 * ... 1413 * </dict> 1414 * 1415 * Add the dictionary into the array. 1416 * 1417 */ 1418 if (!prop_array_add(array, dict)) { 1419 DPRINTF(("%s: prop_array_add\n", __func__)); 1420 goto bad; 1421 } 1422 1423 /* 1424 * Register new event(s) if any monitoring flag was set or if 1425 * the sensor provides entropy for rnd(4). 1426 */ 1427 if (edata->flags & (ENVSYS_FMONANY | ENVSYS_FHAS_ENTROPY)) { 1428 sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP); 1429 sme_evdrv_t->sed_sdict = dict; 1430 sme_evdrv_t->sed_edata = edata; 1431 sme_evdrv_t->sed_sme = sme; 1432 sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units); 1433 sme_evdrv_t->sed_powertype = sdt->crittype; 1434 } 1435 1436 out: 1437 return sme_evdrv_t; 1438 1439 bad: 1440 prop_object_release(dict); 1441 return NULL; 1442 } 1443 1444 /* 1445 * Find the maximum of all currently reported values. 1446 * The provided callback decides whether a sensor is part of the 1447 * maximum calculation (by returning true) or ignored (callback 1448 * returns false). Example usage: callback selects temperature 1449 * sensors in a given thermal zone, the function calculates the 1450 * maximum currently reported temperature in this zone. 1451 * If the parameter "refresh" is true, new values will be aquired 1452 * from the hardware, if not, the last reported value will be used. 1453 */ 1454 uint32_t 1455 sysmon_envsys_get_max_value(bool (*predicate)(const envsys_data_t*), 1456 bool refresh) 1457 { 1458 struct sysmon_envsys *sme; 1459 uint32_t maxv, v; 1460 1461 maxv = 0; 1462 mutex_enter(&sme_global_mtx); 1463 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 1464 sysmon_envsys_acquire(sme, false); 1465 v = sme_get_max_value(sme, predicate, refresh); 1466 sysmon_envsys_release(sme, false); 1467 if (v > maxv) 1468 maxv = v; 1469 } 1470 mutex_exit(&sme_global_mtx); 1471 return maxv; 1472 } 1473 1474 static uint32_t 1475 sme_get_max_value(struct sysmon_envsys *sme, 1476 bool (*predicate)(const envsys_data_t*), 1477 bool refresh) 1478 { 1479 envsys_data_t *edata; 1480 uint32_t maxv, v; 1481 1482 /* 1483 * Iterate over all sensors that match the predicate 1484 */ 1485 maxv = 0; 1486 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 1487 if (!(*predicate)(edata)) 1488 continue; 1489 1490 /* 1491 * refresh sensor data 1492 */ 1493 mutex_enter(&sme->sme_mtx); 1494 sysmon_envsys_refresh_sensor(sme, edata); 1495 mutex_exit(&sme->sme_mtx); 1496 1497 v = edata->value_cur; 1498 if (v > maxv) 1499 maxv = v; 1500 1501 } 1502 1503 return maxv; 1504 } 1505 1506 /* 1507 * sme_update_dictionary: 1508 * 1509 * + Update per-sensor dictionaries with new values if there were 1510 * changes, otherwise the object in dictionary is untouched. 1511 */ 1512 int 1513 sme_update_dictionary(struct sysmon_envsys *sme) 1514 { 1515 envsys_data_t *edata; 1516 prop_object_t array, dict, obj, obj2; 1517 int error = 0; 1518 1519 /* 1520 * Retrieve the array of dictionaries in device. 1521 */ 1522 array = prop_dictionary_get(sme_propd, sme->sme_name); 1523 if (prop_object_type(array) != PROP_TYPE_ARRAY) { 1524 DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name)); 1525 return EINVAL; 1526 } 1527 1528 /* 1529 * Get the last dictionary on the array, this contains the 1530 * 'device-properties' sub-dictionary. 1531 */ 1532 obj = prop_array_get(array, prop_array_count(array) - 1); 1533 if (!obj || prop_object_type(obj) != PROP_TYPE_DICTIONARY) { 1534 DPRINTF(("%s: not a device-properties dictionary\n", __func__)); 1535 return EINVAL; 1536 } 1537 1538 obj2 = prop_dictionary_get(obj, "device-properties"); 1539 if (!obj2) 1540 return EINVAL; 1541 1542 /* 1543 * Update the 'refresh-timeout' property. 1544 */ 1545 if (!prop_dictionary_set_uint64(obj2, "refresh-timeout", 1546 sme->sme_events_timeout)) 1547 return EINVAL; 1548 1549 /* 1550 * - iterate over all sensors. 1551 * - fetch new data. 1552 * - check if data in dictionary is different than new data. 1553 * - update dictionary if there were changes. 1554 */ 1555 DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__, 1556 sme->sme_name, sme->sme_nsensors)); 1557 1558 /* 1559 * Don't bother with locking when traversing the queue, 1560 * the device is already marked as busy; if a sensor 1561 * is going to be removed or added it will have to wait. 1562 */ 1563 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 1564 /* 1565 * refresh sensor data via sme_envsys_refresh_sensor 1566 */ 1567 mutex_enter(&sme->sme_mtx); 1568 sysmon_envsys_refresh_sensor(sme, edata); 1569 mutex_exit(&sme->sme_mtx); 1570 1571 /* 1572 * retrieve sensor's dictionary. 1573 */ 1574 dict = prop_array_get(array, edata->sensor); 1575 if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) { 1576 DPRINTF(("%s: not a dictionary (%d:%s)\n", 1577 __func__, edata->sensor, sme->sme_name)); 1578 return EINVAL; 1579 } 1580 1581 /* 1582 * update sensor's state. 1583 */ 1584 error = sme_update_sensor_dictionary(dict, edata, true); 1585 1586 if (error) 1587 break; 1588 } 1589 1590 return error; 1591 } 1592 1593 int 1594 sme_update_sensor_dictionary(prop_object_t dict, envsys_data_t *edata, 1595 bool value_update) 1596 { 1597 const struct sme_descr_entry *sdt; 1598 int error = 0; 1599 1600 sdt = sme_find_table_entry(SME_DESC_STATES, edata->state); 1601 if (sdt == NULL) { 1602 printf("sme_update_sensor_dictionary: can not update sensor " 1603 "state %d unknown\n", edata->state); 1604 return EINVAL; 1605 } 1606 1607 DPRINTFOBJ(("%s: sensor #%d type=%d (%s) flags=%d\n", __func__, 1608 edata->sensor, sdt->type, sdt->desc, edata->flags)); 1609 1610 error = sme_sensor_upstring(dict, "state", sdt->desc); 1611 if (error) 1612 return (-error); 1613 1614 /* 1615 * update sensor's type. 1616 */ 1617 sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units); 1618 1619 DPRINTFOBJ(("%s: sensor #%d units=%d (%s)\n", __func__, edata->sensor, 1620 sdt->type, sdt->desc)); 1621 1622 error = sme_sensor_upstring(dict, "type", sdt->desc); 1623 if (error) 1624 return (-error); 1625 1626 if (value_update) { 1627 /* 1628 * update sensor's current value. 1629 */ 1630 error = sme_sensor_upint32(dict, "cur-value", edata->value_cur); 1631 if (error) 1632 return error; 1633 } 1634 1635 /* 1636 * Battery charge and Indicator types do not 1637 * need the remaining objects, so skip them. 1638 */ 1639 if (edata->units == ENVSYS_INDICATOR || 1640 edata->units == ENVSYS_BATTERY_CHARGE) 1641 return error; 1642 1643 /* 1644 * update sensor flags. 1645 */ 1646 if (edata->flags & ENVSYS_FPERCENT) { 1647 error = sme_sensor_upbool(dict, "want-percentage", true); 1648 if (error) 1649 return error; 1650 } 1651 1652 if (value_update) { 1653 /* 1654 * update sensor's {max,min}-value. 1655 */ 1656 if (edata->flags & ENVSYS_FVALID_MAX) { 1657 error = sme_sensor_upint32(dict, "max-value", 1658 edata->value_max); 1659 if (error) 1660 return error; 1661 } 1662 1663 if (edata->flags & ENVSYS_FVALID_MIN) { 1664 error = sme_sensor_upint32(dict, "min-value", 1665 edata->value_min); 1666 if (error) 1667 return error; 1668 } 1669 1670 /* 1671 * update 'rpms' only for ENVSYS_SFANRPM sensors. 1672 */ 1673 if (edata->units == ENVSYS_SFANRPM) { 1674 error = sme_sensor_upuint32(dict, "rpms", edata->rpms); 1675 if (error) 1676 return error; 1677 } 1678 1679 /* 1680 * update 'rfact' only for ENVSYS_SVOLTS_[AD]C sensors. 1681 */ 1682 if (edata->units == ENVSYS_SVOLTS_AC || 1683 edata->units == ENVSYS_SVOLTS_DC) { 1684 error = sme_sensor_upint32(dict, "rfact", edata->rfact); 1685 if (error) 1686 return error; 1687 } 1688 } 1689 1690 /* 1691 * update 'drive-state' only for ENVSYS_DRIVE sensors. 1692 */ 1693 if (edata->units == ENVSYS_DRIVE) { 1694 sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES, 1695 edata->value_cur); 1696 error = sme_sensor_upstring(dict, "drive-state", sdt->desc); 1697 if (error) 1698 return error; 1699 } 1700 1701 /* 1702 * update 'battery-capacity' only for ENVSYS_BATTERY_CAPACITY 1703 * sensors. 1704 */ 1705 if (edata->units == ENVSYS_BATTERY_CAPACITY) { 1706 sdt = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY, 1707 edata->value_cur); 1708 error = sme_sensor_upstring(dict, "battery-capacity", 1709 sdt->desc); 1710 if (error) 1711 return error; 1712 } 1713 1714 return error; 1715 } 1716 1717 /* 1718 * sme_userset_dictionary: 1719 * 1720 * + Parse the userland dictionary and run the appropiate tasks 1721 * that were specified. 1722 */ 1723 int 1724 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict, 1725 prop_array_t array) 1726 { 1727 const struct sme_descr_entry *sdt; 1728 envsys_data_t *edata; 1729 prop_dictionary_t dict, tdict = NULL; 1730 prop_object_t obj, obj1, obj2, tobj = NULL; 1731 uint32_t props; 1732 uint64_t refresh_timo = 0; 1733 sysmon_envsys_lim_t lims; 1734 int i, error = 0; 1735 const char *blah; 1736 bool targetfound = false; 1737 1738 /* 1739 * The user wanted to change the refresh timeout value for this 1740 * device. 1741 * 1742 * Get the 'device-properties' object from the userland dictionary. 1743 */ 1744 obj = prop_dictionary_get(udict, "device-properties"); 1745 if (obj && prop_object_type(obj) == PROP_TYPE_DICTIONARY) { 1746 /* 1747 * Get the 'refresh-timeout' property for this device. 1748 */ 1749 obj1 = prop_dictionary_get(obj, "refresh-timeout"); 1750 if (obj1 && prop_object_type(obj1) == PROP_TYPE_NUMBER) { 1751 targetfound = true; 1752 refresh_timo = 1753 prop_number_unsigned_integer_value(obj1); 1754 if (refresh_timo < 1) 1755 error = EINVAL; 1756 else { 1757 mutex_enter(&sme->sme_mtx); 1758 if (sme->sme_events_timeout != refresh_timo) { 1759 sme->sme_events_timeout = refresh_timo; 1760 sme_schedule_callout(sme); 1761 } 1762 mutex_exit(&sme->sme_mtx); 1763 } 1764 } 1765 return error; 1766 1767 } else if (!obj) { 1768 /* 1769 * Get sensor's index from userland dictionary. 1770 */ 1771 obj = prop_dictionary_get(udict, "index"); 1772 if (!obj) 1773 return EINVAL; 1774 if (prop_object_type(obj) != PROP_TYPE_STRING) { 1775 DPRINTF(("%s: 'index' not a string\n", __func__)); 1776 return EINVAL; 1777 } 1778 } else 1779 return EINVAL; 1780 1781 /* 1782 * Don't bother with locking when traversing the queue, 1783 * the device is already marked as busy; if a sensor 1784 * is going to be removed or added it will have to wait. 1785 */ 1786 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 1787 /* 1788 * Get a dictionary and check if it's our sensor by checking 1789 * at its index position. 1790 */ 1791 dict = prop_array_get(array, edata->sensor); 1792 obj1 = prop_dictionary_get(dict, "index"); 1793 1794 /* 1795 * is it our sensor? 1796 */ 1797 if (!prop_string_equals(obj1, obj)) 1798 continue; 1799 1800 props = 0; 1801 1802 /* 1803 * Check if a new description operation was 1804 * requested by the user and set new description. 1805 */ 1806 obj2 = prop_dictionary_get(udict, "description"); 1807 if (obj2 && prop_object_type(obj2) == PROP_TYPE_STRING) { 1808 targetfound = true; 1809 blah = prop_string_cstring_nocopy(obj2); 1810 1811 /* 1812 * Check for duplicate description. 1813 */ 1814 for (i = 0; i < sme->sme_nsensors; i++) { 1815 if (i == edata->sensor) 1816 continue; 1817 tdict = prop_array_get(array, i); 1818 tobj = 1819 prop_dictionary_get(tdict, "description"); 1820 if (prop_string_equals(obj2, tobj)) { 1821 error = EEXIST; 1822 goto out; 1823 } 1824 } 1825 1826 /* 1827 * Update the object in dictionary. 1828 */ 1829 mutex_enter(&sme->sme_mtx); 1830 error = sme_sensor_upstring(dict, 1831 "description", 1832 blah); 1833 if (error) { 1834 mutex_exit(&sme->sme_mtx); 1835 goto out; 1836 } 1837 1838 DPRINTF(("%s: sensor%d changed desc to: %s\n", 1839 __func__, edata->sensor, blah)); 1840 edata->upropset |= PROP_DESC; 1841 mutex_exit(&sme->sme_mtx); 1842 } 1843 1844 /* 1845 * did the user want to change the rfact? 1846 */ 1847 obj2 = prop_dictionary_get(udict, "rfact"); 1848 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1849 targetfound = true; 1850 if (edata->flags & ENVSYS_FCHANGERFACT) { 1851 mutex_enter(&sme->sme_mtx); 1852 edata->rfact = prop_number_integer_value(obj2); 1853 edata->upropset |= PROP_RFACT; 1854 mutex_exit(&sme->sme_mtx); 1855 DPRINTF(("%s: sensor%d changed rfact to %d\n", 1856 __func__, edata->sensor, edata->rfact)); 1857 } else { 1858 error = ENOTSUP; 1859 goto out; 1860 } 1861 } 1862 1863 sdt = sme_find_table_entry(SME_DESC_UNITS, edata->units); 1864 1865 /* 1866 * did the user want to set a critical capacity event? 1867 */ 1868 obj2 = prop_dictionary_get(udict, "critical-capacity"); 1869 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1870 targetfound = true; 1871 lims.sel_critmin = prop_number_integer_value(obj2); 1872 props |= PROP_BATTCAP; 1873 } 1874 1875 /* 1876 * did the user want to set a warning capacity event? 1877 */ 1878 obj2 = prop_dictionary_get(udict, "warning-capacity"); 1879 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1880 targetfound = true; 1881 lims.sel_warnmin = prop_number_integer_value(obj2); 1882 props |= PROP_BATTWARN; 1883 } 1884 1885 /* 1886 * did the user want to set a high capacity event? 1887 */ 1888 obj2 = prop_dictionary_get(udict, "high-capacity"); 1889 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1890 targetfound = true; 1891 lims.sel_warnmin = prop_number_integer_value(obj2); 1892 props |= PROP_BATTHIGH; 1893 } 1894 1895 /* 1896 * did the user want to set a maximum capacity event? 1897 */ 1898 obj2 = prop_dictionary_get(udict, "maximum-capacity"); 1899 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1900 targetfound = true; 1901 lims.sel_warnmin = prop_number_integer_value(obj2); 1902 props |= PROP_BATTMAX; 1903 } 1904 1905 /* 1906 * did the user want to set a critical max event? 1907 */ 1908 obj2 = prop_dictionary_get(udict, "critical-max"); 1909 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1910 targetfound = true; 1911 lims.sel_critmax = prop_number_integer_value(obj2); 1912 props |= PROP_CRITMAX; 1913 } 1914 1915 /* 1916 * did the user want to set a warning max event? 1917 */ 1918 obj2 = prop_dictionary_get(udict, "warning-max"); 1919 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1920 targetfound = true; 1921 lims.sel_warnmax = prop_number_integer_value(obj2); 1922 props |= PROP_WARNMAX; 1923 } 1924 1925 /* 1926 * did the user want to set a critical min event? 1927 */ 1928 obj2 = prop_dictionary_get(udict, "critical-min"); 1929 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1930 targetfound = true; 1931 lims.sel_critmin = prop_number_integer_value(obj2); 1932 props |= PROP_CRITMIN; 1933 } 1934 1935 /* 1936 * did the user want to set a warning min event? 1937 */ 1938 obj2 = prop_dictionary_get(udict, "warning-min"); 1939 if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) { 1940 targetfound = true; 1941 lims.sel_warnmin = prop_number_integer_value(obj2); 1942 props |= PROP_WARNMIN; 1943 } 1944 1945 if (props && (edata->flags & ENVSYS_FMONNOTSUPP) != 0) { 1946 error = ENOTSUP; 1947 goto out; 1948 } 1949 if (props || (edata->flags & ENVSYS_FHAS_ENTROPY) != 0) { 1950 error = sme_event_register(dict, edata, sme, &lims, 1951 props, 1952 (edata->flags & ENVSYS_FPERCENT)? 1953 PENVSYS_EVENT_CAPACITY: 1954 PENVSYS_EVENT_LIMITS, 1955 sdt->crittype); 1956 if (error == EEXIST) 1957 error = 0; 1958 if (error) 1959 goto out; 1960 } 1961 1962 /* 1963 * All objects in dictionary were processed. 1964 */ 1965 break; 1966 } 1967 1968 out: 1969 /* 1970 * invalid target? return the error. 1971 */ 1972 if (!targetfound) 1973 error = EINVAL; 1974 1975 return error; 1976 } 1977 1978 /* 1979 * + sysmon_envsys_foreach_sensor 1980 * 1981 * Walk through the devices' sensor lists and execute the callback. 1982 * If the callback returns false, the remainder of the current 1983 * device's sensors are skipped. 1984 */ 1985 void 1986 sysmon_envsys_foreach_sensor(sysmon_envsys_callback_t func, void *arg, 1987 bool refresh) 1988 { 1989 struct sysmon_envsys *sme; 1990 envsys_data_t *sensor; 1991 1992 mutex_enter(&sme_global_mtx); 1993 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 1994 1995 sysmon_envsys_acquire(sme, false); 1996 TAILQ_FOREACH(sensor, &sme->sme_sensors_list, sensors_head) { 1997 if (refresh) { 1998 mutex_enter(&sme->sme_mtx); 1999 sysmon_envsys_refresh_sensor(sme, sensor); 2000 mutex_exit(&sme->sme_mtx); 2001 } 2002 if (!(*func)(sme, sensor, arg)) 2003 break; 2004 } 2005 sysmon_envsys_release(sme, false); 2006 } 2007 mutex_exit(&sme_global_mtx); 2008 } 2009 2010 /* 2011 * Call the sensor's refresh function, and collect/stir entropy 2012 */ 2013 void 2014 sysmon_envsys_refresh_sensor(struct sysmon_envsys *sme, envsys_data_t *edata) 2015 { 2016 2017 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) 2018 (*sme->sme_refresh)(sme, edata); 2019 2020 if (edata->flags & ENVSYS_FHAS_ENTROPY && 2021 edata->state != ENVSYS_SINVALID && 2022 edata->value_prev != edata->value_cur) 2023 rnd_add_uint32(&edata->rnd_src, edata->value_cur); 2024 edata->value_prev = edata->value_cur; 2025 } 2026