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