Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsysvar.h revision 1.3
      1 /* $NetBSD: sysmon_envsysvar.h,v 1.3 2007/07/05 23:48:22 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 #ifndef _DEV_SYSMON_ENVSYSVAR_H_
     40 #define _DEV_SYSMON_ENVSYSVAR_H_
     41 
     42 #include <sys/param.h>
     43 #include <sys/types.h>
     44 #include <sys/conf.h>
     45 #include <sys/kernel.h>
     46 #include <sys/systm.h>
     47 #include <sys/mutex.h>
     48 #include <sys/workqueue.h>
     49 
     50 #include <dev/sysmon/sysmonvar.h>
     51 #include <prop/proplib.h>
     52 
     53 #ifdef ENVSYS_DEBUG
     54 #define DPRINTF(x)	printf x
     55 #else
     56 #define DPRINTF(x)
     57 #endif
     58 
     59 #ifdef ENVSYS_OBJECTS_DEBUG
     60 #define DPRINTFOBJ(x)	printf x
     61 #else
     62 #define DPRINTFOBJ(x)
     63 #endif
     64 
     65 /* convenience macros to avoid writing same code many times */
     66 #define SENSOR_OBJUPDATED(a, b)						\
     67 do {									\
     68 	DPRINTFOBJ(("%s: obj (%s:%d) updated\n", __func__, (a), (b)));	\
     69 } while (/* CONSTCOND */ 0)
     70 
     71 #define SENSOR_DICTSETFAILED(a, b)					\
     72 do {									\
     73 	DPRINTF(("%s: dict_set (%s:%d) failed\n", __func__, (a), (b)));	\
     74 } while (/* CONSTCOND */ 0)
     75 
     76 #define SENSOR_SETTYPE(a, b, c, d)					\
     77 do {									\
     78 	if (!prop_dictionary_set_ ## d((a), (b), (c))) {		\
     79 		SENSOR_DICTSETFAILED((b), (c));				\
     80 		if ((a)) 						\
     81 			prop_object_release((a));			\
     82 		goto out;						\
     83 	}								\
     84 } while (/* CONSTCOND */ 0)
     85 
     86 #define SENSOR_SINT32(a, b, c)	SENSOR_SETTYPE(a, b, c, int32)
     87 #define SENSOR_SUINT32(a, b, c)	SENSOR_SETTYPE(a, b, c, uint32)
     88 #define SENSOR_SBOOL(a, b, c)	SENSOR_SETTYPE(a, b, c, bool)
     89 #define SENSOR_SSTRING(a, b, c)						\
     90 do {									\
     91 	if (!prop_dictionary_set_cstring_nocopy((a), (b), (c))) {	\
     92 		DPRINTF(("%s: set_cstring (%s) failed.\n",		\
     93 		    __func__, (c)));					\
     94 		if ((a))						\
     95 			prop_object_release((a));			\
     96 		goto out;						\
     97 	}								\
     98 } while (/* CONSTCOND */ 0)
     99 
    100 #define SENSOR_UPTYPE(a, b, c, d, e)					\
    101 do {									\
    102 	obj = prop_dictionary_get((a), (b));				\
    103 	if (!prop_number_equals_ ## e(obj, (c))) {			\
    104 		if (!prop_dictionary_set_ ## d((a), (b), (c))) { 	\
    105 			SENSOR_DICTSETFAILED((b), (c));			\
    106 			return EINVAL;					\
    107 		}							\
    108 		SENSOR_OBJUPDATED((b), (c));				\
    109 	}								\
    110 } while (/* CONSTCOND */ 0)
    111 
    112 #define SENSOR_UPINT32(a, b, c)		\
    113 	SENSOR_UPTYPE(a, b, c, int32, integer)
    114 #define SENSOR_UPUINT32(a, b, c)	\
    115 	SENSOR_UPTYPE(a, b, c, uint32, unsigned_integer)
    116 #define SENSOR_UPSTRING(a, b, c)					\
    117 do {									\
    118 	obj = prop_dictionary_get((a), (b));				\
    119 	if (obj == NULL) {						\
    120 		SENSOR_SSTRING((a), (b), (c));				\
    121 	} else {							\
    122 		if (!prop_string_equals_cstring((obj), (c))) {		\
    123 			SENSOR_SSTRING((a), (b), (c));			\
    124 		}							\
    125 	}								\
    126 } while (/* CONSTCOND */ 0)
    127 
    128 /* struct used by a sysmon envsys event */
    129 typedef struct sme_event {
    130 	/* to add works into our workqueue */
    131 	union {
    132 		struct work u_work;
    133 		TAILQ_ENTRY(sme_event) u_q;
    134 	} see_u;
    135 #define see_wk	see_u.u_work
    136 #define see_q	see_u.u_q
    137 	LIST_ENTRY(sme_event)	see_list;
    138 	struct penvsys_state	pes;		/* our power envsys */
    139 	int32_t			critval;	/* critical value set */
    140 	int			type;		/* type of the event */
    141 	int			snum;		/* sensor number */
    142 	int			evsent;		/* event already sent */
    143 } sme_event_t;
    144 
    145 /* struct by a sysmon envsys event set by a driver */
    146 typedef struct sme_event_drv {
    147 	struct sysmon_envsys	*sme;
    148 	prop_dictionary_t	sdict;
    149 	envsys_data_t		*edata;
    150 	int			powertype;
    151 } sme_event_drv_t;
    152 
    153 /* common */
    154 extern	kmutex_t sme_mtx;	/* mutex for the sysmon envsys devices */
    155 extern	kmutex_t sme_event_mtx;	/* mutex for the sysmon envsys events */
    156 extern	kmutex_t sme_event_init_mtx;	/* mutex to initialize/destroy see */
    157 
    158 /* linked list for the sysmon envsys devices */
    159 LIST_HEAD(, sysmon_envsys) sysmon_envsys_list;
    160 
    161 /* linked list for the sysmon envsys events */
    162 LIST_HEAD(, sme_event) sme_events_list;
    163 
    164 /* functions to handle sysmon envsys devices */
    165 int	sysmon_envsys_createplist(struct sysmon_envsys *);
    166 int	sme_make_dictionary(struct sysmon_envsys *, prop_array_t,
    167 			    envsys_data_t *);
    168 int	sme_update_dictionary(struct sysmon_envsys *);
    169 int	sme_userset_dictionary(struct sysmon_envsys *,
    170 			       prop_dictionary_t, prop_array_t);
    171 
    172 /* functions to handle sysmon envsys events */
    173 int	sme_event_register(struct sme_event *);
    174 int	sme_event_unregister(const char *, int);
    175 void	sme_event_drvadd(void *);
    176 int	sme_event_add(prop_dictionary_t, envsys_data_t *,
    177 		      const char *, const char *, int32_t, int, int);
    178 int	sme_events_init(void);
    179 void	sme_events_check(void *);
    180 void	sme_events_worker(struct work *, void *);
    181 
    182 #endif /* _DEV_SYSMON_ENVSYSVAR_H_ */
    183