Home | History | Annotate | Line # | Download | only in sysmon
sysmonvar.h revision 1.32
      1 /*	$NetBSD: sysmonvar.h,v 1.32 2010/02/28 20:04:04 pgoyette Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Zembu Labs, Inc.
      5  * All rights reserved.
      6  *
      7  * Author: Jason R. Thorpe <thorpej (at) zembu.com>
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by Zembu Labs, Inc.
     20  * 4. Neither the name of Zembu Labs nor the names of its employees may
     21  *    be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
     25  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
     26  * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
     27  * CLAIMED.  IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 #ifndef _DEV_SYSMON_SYSMONVAR_H_
     37 #define	_DEV_SYSMON_SYSMONVAR_H_
     38 
     39 #include <sys/param.h>
     40 #include <sys/envsys.h>
     41 #include <sys/wdog.h>
     42 #include <sys/power.h>
     43 #include <sys/queue.h>
     44 #include <sys/callout.h>
     45 #include <sys/mutex.h>
     46 #include <sys/condvar.h>
     47 
     48 struct lwp;
     49 struct proc;
     50 struct knote;
     51 struct uio;
     52 struct workqueue;
     53 
     54 #define	SYSMON_MINOR_ENVSYS	0
     55 #define	SYSMON_MINOR_WDOG	1
     56 #define	SYSMON_MINOR_POWER	2
     57 
     58 /*****************************************************************************
     59  * Environmental sensor support
     60  *****************************************************************************/
     61 
     62 /*
     63  * Thresholds/limits that are being monitored
     64  */
     65 struct sysmon_envsys_lim {
     66 	int32_t		sel_critmax;
     67 	int32_t		sel_warnmax;
     68 	int32_t		sel_warnmin;
     69 	int32_t		sel_critmin;
     70 };
     71 
     72 typedef struct sysmon_envsys_lim sysmon_envsys_lim_t;
     73 
     74 /* struct used by a sensor */
     75 struct envsys_data {
     76 	TAILQ_ENTRY(envsys_data)	sensors_head;
     77 	uint32_t	sensor;		/* sensor number */
     78 	uint32_t	units;		/* type of sensor */
     79 	uint32_t	state;		/* sensor state */
     80 	uint32_t	flags;		/* sensor flags */
     81 	uint32_t	rpms;		/* for fans, nominal RPMs */
     82 	int32_t		rfact;		/* for volts, factor x 10^4 */
     83 	int32_t		value_cur;	/* current value */
     84 	int32_t		value_max;	/* max value */
     85 	int32_t		value_min;	/* min value */
     86 	int32_t		value_avg;	/* avg value */
     87 	sysmon_envsys_lim_t limits;	/* thresholds for monitoring */
     88 	int		upropset;	/* userland property set? */
     89 	bool		monitor;	/* monitoring enabled/disabled */
     90 	char		desc[ENVSYS_DESCLEN];	/* sensor description */
     91 };
     92 
     93 typedef struct envsys_data envsys_data_t;
     94 
     95 /* sensor flags */
     96 #define ENVSYS_FPERCENT 	0x00000001	/* sensor wants a percentage */
     97 #define ENVSYS_FVALID_MAX	0x00000002	/* max value is ok */
     98 #define ENVSYS_FVALID_MIN	0x00000004	/* min value is ok */
     99 #define ENVSYS_FVALID_AVG	0x00000008	/* avg value is ok */
    100 #define ENVSYS_FCHANGERFACT	0x00000010	/* sensor can change rfact */
    101 
    102 /* monitoring flags */
    103 #define ENVSYS_FMONCRITICAL	0x00000020	/* monitor a critical state */
    104 #define ENVSYS_FMONLIMITS	0x00000040	/* monitor limits/thresholds */
    105 #define ENVSYS_FMONSTCHANGED	0x00000400	/* monitor a battery/drive state */
    106 #define ENVSYS_FMONNOTSUPP	0x00000800	/* monitoring not supported */
    107 #define ENVSYS_FNEED_REFRESH	0x00001000	/* sensor needs refreshing */
    108 
    109 /*
    110  * Properties that can be set in upropset (and in the event_limit's
    111  * flags field)
    112  */
    113 #define	PROP_CRITMAX		0x0001
    114 #define	PROP_CRITMIN		0x0002
    115 #define	PROP_WARNMAX		0x0004
    116 #define	PROP_WARNMIN		0x0008
    117 #define	PROP_BATTCAP		0x0010
    118 #define	PROP_BATTWARN		0x0020
    119 #define	PROP_BATTHIGH		0x0040
    120 #define	PROP_BATTMAX		0x0080
    121 #define	PROP_DESC		0x0100
    122 #define	PROP_RFACT		0x0200
    123 
    124 #define	PROP_DRIVER_LIMITS	0x8000
    125 #define	PROP_CAP_LIMITS		(PROP_BATTCAP  | PROP_BATTWARN | \
    126 				 PROP_BATTHIGH | PROP_BATTMAX)
    127 #define	PROP_VAL_LIMITS		(PROP_CRITMAX  | PROP_CRITMIN | \
    128 				 PROP_WARNMAX  | PROP_WARNMIN)
    129 #define	PROP_LIMITS		(PROP_CAP_LIMITS | PROP_VAL_LIMITS)
    130 struct sme_event;
    131 
    132 struct sysmon_envsys {
    133 	const char *sme_name;		/* envsys device name */
    134 	u_int sme_nsensors;		/* sensors count, from driver */
    135 	u_int sme_fsensor;		/* sensor index base, from sysmon */
    136 #define SME_SENSOR_IDX(sme, idx) 	((idx) - (sme)->sme_fsensor)
    137 	int sme_class;			/* class of device */
    138 #define SME_CLASS_BATTERY	1		/* device is a battery */
    139 #define SME_CLASS_ACADAPTER	2		/* device is an AC adapter */
    140 	int sme_flags;			/* additional flags */
    141 #define SME_FLAG_BUSY 		0x00000001 	/* device busy */
    142 #define SME_DISABLE_REFRESH	0x00000002	/* disable sme_refresh */
    143 #define SME_CALLOUT_INITIALIZED	0x00000004	/* callout was initialized */
    144 #define SME_INIT_REFRESH        0x00000008      /* call sme_refresh() after
    145 						   interrupts are enabled in
    146 						   the autoconf(9) process. */
    147 #define SME_POLL_ONLY           0x00000010      /* only poll sme_refresh */
    148 
    149 	void *sme_cookie;		/* for ENVSYS back-end */
    150 
    151 	/*
    152 	 * Function callback to receive data from device.
    153 	 */
    154 	void (*sme_refresh)(struct sysmon_envsys *, envsys_data_t *);
    155 
    156 	/*
    157 	 * Function callbacks to exchange limit/threshold values
    158 	 * with device
    159 	 */
    160 	void (*sme_set_limits)(struct sysmon_envsys *, envsys_data_t *,
    161 			       sysmon_envsys_lim_t *, uint32_t *);
    162 	void (*sme_get_limits)(struct sysmon_envsys *, envsys_data_t *,
    163 			       sysmon_envsys_lim_t *, uint32_t *);
    164 
    165 	struct workqueue *sme_wq;	/* the workqueue for the events */
    166 	struct callout sme_callout;	/* for the events */
    167 	uint64_t sme_events_timeout;	/* the timeout used in the callout */
    168 
    169 	/*
    170 	 * linked list for the sysmon envsys devices.
    171 	 */
    172 	LIST_ENTRY(sysmon_envsys) sme_list;
    173 
    174 	/*
    175 	 * linked list for the events that a device maintains.
    176 	 */
    177 	LIST_HEAD(, sme_event) sme_events_list;
    178 
    179 	/*
    180 	 * tailq for the sensors that a device maintains.
    181 	 */
    182 	TAILQ_HEAD(, envsys_data) sme_sensors_list;
    183 
    184 	/*
    185 	 * Locking/synchronization.
    186 	 */
    187 	kmutex_t sme_mtx;
    188 	kmutex_t sme_callout_mtx;
    189 	kcondvar_t sme_condvar;
    190 };
    191 
    192 int	sysmonopen_envsys(dev_t, int, int, struct lwp *);
    193 int	sysmonclose_envsys(dev_t, int, int, struct lwp *);
    194 int	sysmonioctl_envsys(dev_t, u_long, void *, int, struct lwp *);
    195 
    196 struct sysmon_envsys 	*sysmon_envsys_create(void);
    197 void 			sysmon_envsys_destroy(struct sysmon_envsys *);
    198 
    199 int	sysmon_envsys_register(struct sysmon_envsys *);
    200 void	sysmon_envsys_unregister(struct sysmon_envsys *);
    201 
    202 int	sysmon_envsys_sensor_attach(struct sysmon_envsys *, envsys_data_t *);
    203 int	sysmon_envsys_sensor_detach(struct sysmon_envsys *, envsys_data_t *);
    204 
    205 uint32_t	sysmon_envsys_get_max_value(bool (*)(const envsys_data_t*), bool);
    206 
    207 void	sysmon_envsys_init(void);
    208 
    209 /*****************************************************************************
    210  * Watchdog timer support
    211  *****************************************************************************/
    212 
    213 struct sysmon_wdog {
    214 	const char *smw_name;		/* watchdog device name */
    215 
    216 	LIST_ENTRY(sysmon_wdog) smw_list;
    217 
    218 	void *smw_cookie;		/* for watchdog back-end */
    219 	int (*smw_setmode)(struct sysmon_wdog *);
    220 	int (*smw_tickle)(struct sysmon_wdog *);
    221 	u_int smw_period;		/* timer period (in seconds) */
    222 	int smw_mode;			/* timer mode */
    223 	u_int smw_refcnt;		/* references */
    224 	pid_t smw_tickler;		/* last process to tickle */
    225 };
    226 
    227 int	sysmonopen_wdog(dev_t, int, int, struct lwp *);
    228 int	sysmonclose_wdog(dev_t, int, int, struct lwp *);
    229 int	sysmonioctl_wdog(dev_t, u_long, void *, int, struct lwp *);
    230 
    231 int     sysmon_wdog_register(struct sysmon_wdog *);
    232 int     sysmon_wdog_unregister(struct sysmon_wdog *);
    233 
    234 void	sysmon_wdog_init(void);
    235 
    236 /*****************************************************************************
    237  * Power management support
    238  *****************************************************************************/
    239 
    240 struct sysmon_pswitch {
    241 	const char *smpsw_name;		/* power switch name */
    242 	int smpsw_type;			/* power switch type */
    243 
    244 	LIST_ENTRY(sysmon_pswitch) smpsw_list;
    245 };
    246 
    247 int	sysmonopen_power(dev_t, int, int, struct lwp *);
    248 int	sysmonclose_power(dev_t, int, int, struct lwp *);
    249 int	sysmonread_power(dev_t, struct uio *, int);
    250 int	sysmonpoll_power(dev_t, int, struct lwp *);
    251 int	sysmonkqfilter_power(dev_t, struct knote *);
    252 int	sysmonioctl_power(dev_t, u_long, void *, int, struct lwp *);
    253 
    254 void	sysmon_power_settype(const char *);
    255 
    256 int	sysmon_pswitch_register(struct sysmon_pswitch *);
    257 void	sysmon_pswitch_unregister(struct sysmon_pswitch *);
    258 
    259 void	sysmon_pswitch_event(struct sysmon_pswitch *, int);
    260 void	sysmon_penvsys_event(struct penvsys_state *, int);
    261 
    262 void	sysmon_power_init(void);
    263 
    264 #endif /* _DEV_SYSMON_SYSMONVAR_H_ */
    265