Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsysvar.h revision 1.7
      1  1.7  xtraeme /* $NetBSD: sysmon_envsysvar.h,v 1.7 2007/07/20 10:40:08 xtraeme Exp $ */
      2  1.1  xtraeme 
      3  1.1  xtraeme /*-
      4  1.1  xtraeme  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  1.1  xtraeme  * All rights reserved.
      6  1.1  xtraeme  *
      7  1.1  xtraeme  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  xtraeme  * by Juan Romero Pardines.
      9  1.1  xtraeme  *
     10  1.1  xtraeme  * Redistribution and use in source and binary forms, with or without
     11  1.1  xtraeme  * modification, are permitted provided that the following conditions
     12  1.1  xtraeme  * are met:
     13  1.1  xtraeme  * 1. Redistributions of source code must retain the above copyright
     14  1.1  xtraeme  *    notice, this list of conditions and the following disclaimer.
     15  1.1  xtraeme  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  xtraeme  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  xtraeme  *    documentation and/or other materials provided with the distribution.
     18  1.1  xtraeme  * 3. All advertising materials mentioning features or use of this software
     19  1.1  xtraeme  *    must display the following acknowledgement:
     20  1.1  xtraeme  *      This product includes software developed by Juan Romero Pardines
     21  1.1  xtraeme  *      for the NetBSD Foundation, Inc. and its contributors.
     22  1.1  xtraeme  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  xtraeme  *    contributors may be used to endorse or promote products derived
     24  1.1  xtraeme  *    from this software without specific prior written permission.
     25  1.1  xtraeme  *
     26  1.1  xtraeme  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  xtraeme  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  xtraeme  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  xtraeme  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  xtraeme  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  xtraeme  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  xtraeme  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  xtraeme  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  xtraeme  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  xtraeme  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  xtraeme  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  xtraeme  */
     38  1.1  xtraeme 
     39  1.1  xtraeme #ifndef _DEV_SYSMON_ENVSYSVAR_H_
     40  1.1  xtraeme #define _DEV_SYSMON_ENVSYSVAR_H_
     41  1.1  xtraeme 
     42  1.1  xtraeme #include <sys/param.h>
     43  1.1  xtraeme #include <sys/types.h>
     44  1.1  xtraeme #include <sys/conf.h>
     45  1.1  xtraeme #include <sys/kernel.h>
     46  1.1  xtraeme #include <sys/systm.h>
     47  1.1  xtraeme #include <sys/mutex.h>
     48  1.1  xtraeme #include <sys/workqueue.h>
     49  1.1  xtraeme 
     50  1.1  xtraeme #include <dev/sysmon/sysmonvar.h>
     51  1.1  xtraeme #include <prop/proplib.h>
     52  1.1  xtraeme 
     53  1.1  xtraeme #ifdef ENVSYS_DEBUG
     54  1.1  xtraeme #define DPRINTF(x)	printf x
     55  1.1  xtraeme #else
     56  1.1  xtraeme #define DPRINTF(x)
     57  1.1  xtraeme #endif
     58  1.1  xtraeme 
     59  1.1  xtraeme #ifdef ENVSYS_OBJECTS_DEBUG
     60  1.1  xtraeme #define DPRINTFOBJ(x)	printf x
     61  1.1  xtraeme #else
     62  1.1  xtraeme #define DPRINTFOBJ(x)
     63  1.1  xtraeme #endif
     64  1.1  xtraeme 
     65  1.1  xtraeme /* convenience macros to avoid writing same code many times */
     66  1.1  xtraeme #define SENSOR_OBJUPDATED(a, b)						\
     67  1.1  xtraeme do {									\
     68  1.1  xtraeme 	DPRINTFOBJ(("%s: obj (%s:%d) updated\n", __func__, (a), (b)));	\
     69  1.1  xtraeme } while (/* CONSTCOND */ 0)
     70  1.1  xtraeme 
     71  1.7  xtraeme /*
     72  1.7  xtraeme  * Functions to create objects in a dictionary if they do not exist, or
     73  1.7  xtraeme  * for updating its value it value provided doesn't match with the value
     74  1.7  xtraeme  * in dictionary.
     75  1.7  xtraeme  */
     76  1.7  xtraeme static inline int
     77  1.7  xtraeme sme_sensor_upbool(prop_object_t obj, prop_dictionary_t dict,
     78  1.7  xtraeme 		  const char *key, bool val)
     79  1.7  xtraeme {
     80  1.7  xtraeme 	KASSERT(dict != NULL);
     81  1.7  xtraeme 
     82  1.7  xtraeme 	obj = prop_dictionary_get(dict, key);
     83  1.7  xtraeme 	if (obj) {
     84  1.7  xtraeme 		if (prop_bool_true(obj) != val) {
     85  1.7  xtraeme 			if (!prop_dictionary_set_bool(dict, key, val)) {
     86  1.7  xtraeme 				DPRINTF(("%s: (up) set_bool %s:%d\n",
     87  1.7  xtraeme 				    __func__, key, val));
     88  1.7  xtraeme 				return EINVAL;
     89  1.7  xtraeme 			}
     90  1.7  xtraeme 			SENSOR_OBJUPDATED(key, val);
     91  1.7  xtraeme 		}
     92  1.7  xtraeme 	} else {
     93  1.7  xtraeme 		if (!prop_dictionary_set_bool(dict, key, val)) {
     94  1.7  xtraeme 			DPRINTF(("%s: (set) set_bool %s:%d\n",
     95  1.7  xtraeme 			    __func__, key, val));
     96  1.7  xtraeme 			return EINVAL;
     97  1.7  xtraeme 		}
     98  1.7  xtraeme 	}
     99  1.7  xtraeme 
    100  1.7  xtraeme 	return 0;
    101  1.7  xtraeme }
    102  1.7  xtraeme 
    103  1.7  xtraeme static inline int
    104  1.7  xtraeme sme_sensor_upint32(prop_object_t obj, prop_dictionary_t dict,
    105  1.7  xtraeme 		   const char *key, int32_t val)
    106  1.7  xtraeme {
    107  1.7  xtraeme 	KASSERT(dict != NULL);
    108  1.7  xtraeme 
    109  1.7  xtraeme 	obj = prop_dictionary_get(dict, key);
    110  1.7  xtraeme 	if (obj) {
    111  1.7  xtraeme 		if (!prop_number_equals_integer(obj, val)) {
    112  1.7  xtraeme 			if (!prop_dictionary_set_int32(dict, key, val)) {
    113  1.7  xtraeme 				DPRINTF(("%s: (up) set_int32 %s:%d\n",
    114  1.7  xtraeme 				    __func__, key, val));
    115  1.7  xtraeme 				return EINVAL;
    116  1.7  xtraeme 			}
    117  1.7  xtraeme 			SENSOR_OBJUPDATED(key, val);
    118  1.7  xtraeme 		}
    119  1.7  xtraeme 	} else {
    120  1.7  xtraeme 		if (!prop_dictionary_set_int32(dict, key, val)) {
    121  1.7  xtraeme 			DPRINTF(("%s: (set) set_int32 %s:%d\n",
    122  1.7  xtraeme 			    __func__, key, val));
    123  1.7  xtraeme 			return EINVAL;
    124  1.7  xtraeme 		}
    125  1.7  xtraeme 	}
    126  1.7  xtraeme 
    127  1.7  xtraeme 	return 0;
    128  1.7  xtraeme }
    129  1.7  xtraeme 
    130  1.7  xtraeme static inline int
    131  1.7  xtraeme sme_sensor_upuint32(prop_object_t obj, prop_dictionary_t dict,
    132  1.7  xtraeme 		    const char *key, uint32_t val)
    133  1.7  xtraeme {
    134  1.7  xtraeme 	KASSERT(dict != NULL);
    135  1.7  xtraeme 
    136  1.7  xtraeme 	obj = prop_dictionary_get(dict, key);
    137  1.7  xtraeme 	if (obj) {
    138  1.7  xtraeme 		if (!prop_number_equals_unsigned_integer(obj, val)) {
    139  1.7  xtraeme 			if (!prop_dictionary_set_uint32(dict, key, val)) {
    140  1.7  xtraeme 				DPRINTF(("%s: (up) set_uint32 %s:%d\n",
    141  1.7  xtraeme 				    __func__, key, val));
    142  1.7  xtraeme 				return EINVAL;
    143  1.7  xtraeme 			}
    144  1.7  xtraeme 			SENSOR_OBJUPDATED(key, val);
    145  1.7  xtraeme 		}
    146  1.7  xtraeme 	} else {
    147  1.7  xtraeme 		if (!prop_dictionary_set_uint32(dict, key, val)) {
    148  1.7  xtraeme 			DPRINTF(("%s: (set) set_uint32 %s:%d\n",
    149  1.7  xtraeme 			    __func__, key, val));
    150  1.7  xtraeme 			return EINVAL;
    151  1.7  xtraeme 		}
    152  1.7  xtraeme 	}
    153  1.7  xtraeme 
    154  1.7  xtraeme 	return 0;
    155  1.7  xtraeme }
    156  1.7  xtraeme 
    157  1.7  xtraeme static inline int
    158  1.7  xtraeme sme_sensor_upstring(prop_object_t obj, prop_dictionary_t dict,
    159  1.7  xtraeme 		    const char *key, const char *str)
    160  1.7  xtraeme {
    161  1.7  xtraeme 	KASSERT(dict != NULL);
    162  1.7  xtraeme 
    163  1.7  xtraeme 	obj = prop_dictionary_get(dict, key);
    164  1.7  xtraeme 	if (obj == NULL) {
    165  1.7  xtraeme 		if (!prop_dictionary_set_cstring_nocopy(dict, key, str)) {
    166  1.7  xtraeme 			DPRINTF(("%s: (up) set_cstring %s:%s\n",
    167  1.7  xtraeme 			    __func__, key, str));
    168  1.7  xtraeme 			return EINVAL;
    169  1.7  xtraeme 		}
    170  1.7  xtraeme 	} else {
    171  1.7  xtraeme 		if (!prop_string_equals_cstring(obj, str)) {
    172  1.7  xtraeme 			if (!prop_dictionary_set_cstring_nocopy(dict,
    173  1.7  xtraeme 								key,
    174  1.7  xtraeme 								str)) {
    175  1.7  xtraeme 				DPRINTF(("%s: (set) set_cstring %s:%s\n",
    176  1.7  xtraeme 				    __func__, key, str));
    177  1.7  xtraeme 				return EINVAL;
    178  1.7  xtraeme 			}
    179  1.7  xtraeme 		}
    180  1.7  xtraeme 	}
    181  1.1  xtraeme 
    182  1.7  xtraeme 	return 0;
    183  1.7  xtraeme }
    184  1.1  xtraeme 
    185  1.1  xtraeme /* struct used by a sysmon envsys event */
    186  1.1  xtraeme typedef struct sme_event {
    187  1.1  xtraeme 	/* to add works into our workqueue */
    188  1.6  xtraeme 	struct work 		see_wk;
    189  1.6  xtraeme 	LIST_ENTRY(sme_event) 	see_list;
    190  1.1  xtraeme 	struct penvsys_state	pes;		/* our power envsys */
    191  1.1  xtraeme 	int32_t			critval;	/* critical value set */
    192  1.1  xtraeme 	int			type;		/* type of the event */
    193  1.1  xtraeme 	int			snum;		/* sensor number */
    194  1.1  xtraeme 	int			evsent;		/* event already sent */
    195  1.5  xtraeme 	int 			see_flags;	/* see above */
    196  1.6  xtraeme #define SME_EVENT_WORKING	0x0001 		/* This event is busy */
    197  1.1  xtraeme } sme_event_t;
    198  1.1  xtraeme 
    199  1.1  xtraeme /* struct by a sysmon envsys event set by a driver */
    200  1.1  xtraeme typedef struct sme_event_drv {
    201  1.1  xtraeme 	struct sysmon_envsys	*sme;
    202  1.1  xtraeme 	prop_dictionary_t	sdict;
    203  1.1  xtraeme 	envsys_data_t		*edata;
    204  1.1  xtraeme 	int			powertype;
    205  1.1  xtraeme } sme_event_drv_t;
    206  1.1  xtraeme 
    207  1.1  xtraeme /* common */
    208  1.1  xtraeme extern	kmutex_t sme_mtx;	/* mutex for the sysmon envsys devices */
    209  1.1  xtraeme extern	kmutex_t sme_event_mtx;	/* mutex for the sysmon envsys events */
    210  1.1  xtraeme extern	kmutex_t sme_event_init_mtx;	/* mutex to initialize/destroy see */
    211  1.5  xtraeme extern	kcondvar_t sme_event_cv;
    212  1.1  xtraeme 
    213  1.1  xtraeme /* linked list for the sysmon envsys devices */
    214  1.1  xtraeme LIST_HEAD(, sysmon_envsys) sysmon_envsys_list;
    215  1.1  xtraeme 
    216  1.1  xtraeme /* linked list for the sysmon envsys events */
    217  1.1  xtraeme LIST_HEAD(, sme_event) sme_events_list;
    218  1.1  xtraeme 
    219  1.1  xtraeme /* functions to handle sysmon envsys devices */
    220  1.1  xtraeme int	sysmon_envsys_createplist(struct sysmon_envsys *);
    221  1.1  xtraeme int	sme_make_dictionary(struct sysmon_envsys *, prop_array_t,
    222  1.1  xtraeme 			    envsys_data_t *);
    223  1.1  xtraeme int	sme_update_dictionary(struct sysmon_envsys *);
    224  1.1  xtraeme int	sme_userset_dictionary(struct sysmon_envsys *,
    225  1.1  xtraeme 			       prop_dictionary_t, prop_array_t);
    226  1.1  xtraeme 
    227  1.1  xtraeme /* functions to handle sysmon envsys events */
    228  1.1  xtraeme int	sme_event_register(struct sme_event *);
    229  1.1  xtraeme int	sme_event_unregister(const char *, int);
    230  1.1  xtraeme void	sme_event_drvadd(void *);
    231  1.1  xtraeme int	sme_event_add(prop_dictionary_t, envsys_data_t *,
    232  1.1  xtraeme 		      const char *, const char *, int32_t, int, int);
    233  1.1  xtraeme int	sme_events_init(void);
    234  1.1  xtraeme void	sme_events_check(void *);
    235  1.1  xtraeme void	sme_events_worker(struct work *, void *);
    236  1.1  xtraeme 
    237  1.1  xtraeme #endif /* _DEV_SYSMON_ENVSYSVAR_H_ */
    238