Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsysvar.h revision 1.5
      1 /* $NetBSD: sysmon_envsysvar.h,v 1.5 2007/07/18 20:31:48 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 		goto out;						\
     81 	}								\
     82 } while (/* CONSTCOND */ 0)
     83 
     84 #define SENSOR_SINT32(a, b, c)	SENSOR_SETTYPE(a, b, c, int32)
     85 #define SENSOR_SUINT32(a, b, c)	SENSOR_SETTYPE(a, b, c, uint32)
     86 #define SENSOR_SBOOL(a, b, c)	SENSOR_SETTYPE(a, b, c, bool)
     87 #define SENSOR_SSTRING(a, b, c)						\
     88 do {									\
     89 	if (!prop_dictionary_set_cstring_nocopy((a), (b), (c))) {	\
     90 		DPRINTF(("%s: set_cstring (%s) failed.\n",		\
     91 		    __func__, (c)));					\
     92 		goto out;						\
     93 	}								\
     94 } while (/* CONSTCOND */ 0)
     95 
     96 #define SENSOR_UPTYPE(a, b, c, d, e)					\
     97 do {									\
     98 	obj = prop_dictionary_get((a), (b));				\
     99 	if (!prop_number_equals_ ## e(obj, (c))) {			\
    100 		if (!prop_dictionary_set_ ## d((a), (b), (c))) { 	\
    101 			SENSOR_DICTSETFAILED((b), (c));			\
    102 			return EINVAL;					\
    103 		}							\
    104 		SENSOR_OBJUPDATED((b), (c));				\
    105 	}								\
    106 } while (/* CONSTCOND */ 0)
    107 
    108 #define SENSOR_UPINT32(a, b, c)		\
    109 	SENSOR_UPTYPE(a, b, c, int32, integer)
    110 #define SENSOR_UPUINT32(a, b, c)	\
    111 	SENSOR_UPTYPE(a, b, c, uint32, unsigned_integer)
    112 #define SENSOR_UPSTRING(a, b, c)					\
    113 do {									\
    114 	obj = prop_dictionary_get((a), (b));				\
    115 	if (obj == NULL) {						\
    116 		SENSOR_SSTRING((a), (b), (c));				\
    117 	} else {							\
    118 		if (!prop_string_equals_cstring((obj), (c))) {		\
    119 			SENSOR_SSTRING((a), (b), (c));			\
    120 		}							\
    121 	}								\
    122 } while (/* CONSTCOND */ 0)
    123 
    124 /* struct used by a sysmon envsys event */
    125 typedef struct sme_event {
    126 	/* to add works into our workqueue */
    127 	struct work see_wk;
    128 	LIST_ENTRY(sme_event) see_list;
    129 	struct penvsys_state	pes;		/* our power envsys */
    130 	int32_t			critval;	/* critical value set */
    131 	int			type;		/* type of the event */
    132 	int			snum;		/* sensor number */
    133 	int			evsent;		/* event already sent */
    134 	int 			see_flags;	/* see above */
    135 #define SME_EVENT_WORKING	0x0001	/* This event is busy */
    136 } sme_event_t;
    137 
    138 /* struct by a sysmon envsys event set by a driver */
    139 typedef struct sme_event_drv {
    140 	struct sysmon_envsys	*sme;
    141 	prop_dictionary_t	sdict;
    142 	envsys_data_t		*edata;
    143 	int			powertype;
    144 } sme_event_drv_t;
    145 
    146 /* common */
    147 extern	kmutex_t sme_mtx;	/* mutex for the sysmon envsys devices */
    148 extern	kmutex_t sme_event_mtx;	/* mutex for the sysmon envsys events */
    149 extern	kmutex_t sme_event_init_mtx;	/* mutex to initialize/destroy see */
    150 extern	kcondvar_t sme_event_cv;
    151 
    152 /* linked list for the sysmon envsys devices */
    153 LIST_HEAD(, sysmon_envsys) sysmon_envsys_list;
    154 
    155 /* linked list for the sysmon envsys events */
    156 LIST_HEAD(, sme_event) sme_events_list;
    157 
    158 /* functions to handle sysmon envsys devices */
    159 int	sysmon_envsys_createplist(struct sysmon_envsys *);
    160 int	sme_make_dictionary(struct sysmon_envsys *, prop_array_t,
    161 			    envsys_data_t *);
    162 int	sme_update_dictionary(struct sysmon_envsys *);
    163 int	sme_userset_dictionary(struct sysmon_envsys *,
    164 			       prop_dictionary_t, prop_array_t);
    165 
    166 /* functions to handle sysmon envsys events */
    167 int	sme_event_register(struct sme_event *);
    168 int	sme_event_unregister(const char *, int);
    169 void	sme_event_drvadd(void *);
    170 int	sme_event_add(prop_dictionary_t, envsys_data_t *,
    171 		      const char *, const char *, int32_t, int, int);
    172 int	sme_events_init(void);
    173 void	sme_events_check(void *);
    174 void	sme_events_worker(struct work *, void *);
    175 
    176 #endif /* _DEV_SYSMON_ENVSYSVAR_H_ */
    177