xref: /netbsd/sys/dev/sysmon/sysmonvar.h (revision 6550d01e)
1 /*	$NetBSD: sysmonvar.h,v 1.40 2011/01/04 01:51:06 matt Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 Zembu Labs, Inc.
5  * All rights reserved.
6  *
7  * Author: Jason R. Thorpe <thorpej@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 	char		desc[ENVSYS_DESCLEN];	/* sensor description */
90 };
91 
92 typedef struct envsys_data envsys_data_t;
93 
94 /* sensor flags */
95 #define ENVSYS_FPERCENT 	0x00000001	/* sensor wants a percentage */
96 #define ENVSYS_FVALID_MAX	0x00000002	/* max value is ok */
97 #define ENVSYS_FVALID_MIN	0x00000004	/* min value is ok */
98 #define ENVSYS_FVALID_AVG	0x00000008	/* avg value is ok */
99 #define ENVSYS_FCHANGERFACT	0x00000010	/* sensor can change rfact */
100 
101 /* monitoring flags */
102 #define ENVSYS_FMONCRITICAL	0x00000020	/* monitor a critical state */
103 #define ENVSYS_FMONLIMITS	0x00000040	/* monitor limits/thresholds */
104 #define ENVSYS_FMONSTCHANGED	0x00000400	/* monitor a battery/drive state */
105 #define ENVSYS_FMONANY	\
106 	(ENVSYS_FMONCRITICAL | ENVSYS_FMONLIMITS | ENVSYS_FMONSTCHANGED)
107 #define ENVSYS_FMONNOTSUPP	0x00000800	/* monitoring not supported */
108 #define ENVSYS_FNEED_REFRESH	0x00001000	/* sensor needs refreshing */
109 
110 /*
111  * Properties that can be set in upropset (and in the event_limit's
112  * flags field)
113  */
114 #define	PROP_CRITMAX		0x0001
115 #define	PROP_CRITMIN		0x0002
116 #define	PROP_WARNMAX		0x0004
117 #define	PROP_WARNMIN		0x0008
118 #define	PROP_BATTCAP		0x0010
119 #define	PROP_BATTWARN		0x0020
120 #define	PROP_BATTHIGH		0x0040
121 #define	PROP_BATTMAX		0x0080
122 #define	PROP_DESC		0x0100
123 #define	PROP_RFACT		0x0200
124 
125 #define	PROP_DRIVER_LIMITS	0x8000
126 #define	PROP_CAP_LIMITS		(PROP_BATTCAP  | PROP_BATTWARN | \
127 				 PROP_BATTHIGH | PROP_BATTMAX)
128 #define	PROP_VAL_LIMITS		(PROP_CRITMAX  | PROP_CRITMIN | \
129 				 PROP_WARNMAX  | PROP_WARNMIN)
130 #define	PROP_LIMITS		(PROP_CAP_LIMITS | PROP_VAL_LIMITS)
131 struct sme_event;
132 
133 struct sysmon_envsys {
134 	const char *sme_name;		/* envsys device name */
135 	u_int sme_nsensors;		/* sensors count, from driver */
136 	u_int sme_fsensor;		/* sensor index base, from sysmon */
137 #define SME_SENSOR_IDX(sme, idx) 	((idx) - (sme)->sme_fsensor)
138 	int sme_class;			/* class of device */
139 #define SME_CLASS_BATTERY	1		/* device is a battery */
140 #define SME_CLASS_ACADAPTER	2		/* device is an AC adapter */
141 	int sme_flags;			/* additional flags */
142 #define SME_FLAG_BUSY 		0x00000001 	/* device busy */
143 #define SME_DISABLE_REFRESH	0x00000002	/* disable sme_refresh */
144 #define SME_CALLOUT_INITIALIZED	0x00000004	/* callout was initialized */
145 #define SME_INIT_REFRESH        0x00000008      /* call sme_refresh() after
146 						   interrupts are enabled in
147 						   the autoconf(9) process. */
148 #define SME_POLL_ONLY           0x00000010      /* only poll sme_refresh */
149 
150 	void *sme_cookie;		/* for ENVSYS back-end */
151 
152 	/*
153 	 * Function callback to receive data from device.
154 	 */
155 	void (*sme_refresh)(struct sysmon_envsys *, envsys_data_t *);
156 
157 	/*
158 	 * Function callbacks to exchange limit/threshold values
159 	 * with device
160 	 */
161 	void (*sme_set_limits)(struct sysmon_envsys *, envsys_data_t *,
162 			       sysmon_envsys_lim_t *, uint32_t *);
163 	void (*sme_get_limits)(struct sysmon_envsys *, envsys_data_t *,
164 			       sysmon_envsys_lim_t *, uint32_t *);
165 
166 	struct workqueue *sme_wq;	/* the workqueue for the events */
167 	struct callout sme_callout;	/* for the events */
168 	uint64_t sme_events_timeout;	/* the timeout used in the callout */
169 
170 	/*
171 	 * linked list for the sysmon envsys devices.
172 	 */
173 	LIST_ENTRY(sysmon_envsys) sme_list;
174 
175 	/*
176 	 * linked list for the events that a device maintains.
177 	 */
178 	LIST_HEAD(, sme_event) sme_events_list;
179 
180 	/*
181 	 * tailq for the sensors that a device maintains.
182 	 */
183 	TAILQ_HEAD(, envsys_data) sme_sensors_list;
184 
185 	/*
186 	 * Locking/synchronization.
187 	 */
188 	kmutex_t sme_mtx;
189 	kmutex_t sme_callout_mtx;
190 	kcondvar_t sme_condvar;
191 };
192 
193 int	sysmonopen_envsys(dev_t, int, int, struct lwp *);
194 int	sysmonclose_envsys(dev_t, int, int, struct lwp *);
195 int	sysmonioctl_envsys(dev_t, u_long, void *, int, struct lwp *);
196 
197 struct sysmon_envsys 	*sysmon_envsys_create(void);
198 void 			sysmon_envsys_destroy(struct sysmon_envsys *);
199 
200 int	sysmon_envsys_register(struct sysmon_envsys *);
201 void	sysmon_envsys_unregister(struct sysmon_envsys *);
202 
203 int	sysmon_envsys_sensor_attach(struct sysmon_envsys *, envsys_data_t *);
204 int	sysmon_envsys_sensor_detach(struct sysmon_envsys *, envsys_data_t *);
205 
206 uint32_t	sysmon_envsys_get_max_value(bool (*)(const envsys_data_t*), bool);
207 
208 void	sysmon_envsys_sensor_event(struct sysmon_envsys *, envsys_data_t *,
209 				   int);
210 
211 typedef	bool (*sysmon_envsys_callback_t)(const struct sysmon_envsys *,
212 					 const envsys_data_t *, void*);
213 
214 void	sysmon_envsys_foreach_sensor(sysmon_envsys_callback_t, void *, bool);
215 
216 int	sysmon_envsys_update_limits(struct sysmon_envsys *, envsys_data_t *);
217 
218 void	sysmon_envsys_init(void);
219 
220 /*****************************************************************************
221  * Watchdog timer support
222  *****************************************************************************/
223 
224 struct sysmon_wdog {
225 	const char *smw_name;		/* watchdog device name */
226 
227 	LIST_ENTRY(sysmon_wdog) smw_list;
228 
229 	void *smw_cookie;		/* for watchdog back-end */
230 	int (*smw_setmode)(struct sysmon_wdog *);
231 	int (*smw_tickle)(struct sysmon_wdog *);
232 	u_int smw_period;		/* timer period (in seconds) */
233 	int smw_mode;			/* timer mode */
234 	u_int smw_refcnt;		/* references */
235 	pid_t smw_tickler;		/* last process to tickle */
236 };
237 
238 int	sysmonopen_wdog(dev_t, int, int, struct lwp *);
239 int	sysmonclose_wdog(dev_t, int, int, struct lwp *);
240 int	sysmonioctl_wdog(dev_t, u_long, void *, int, struct lwp *);
241 
242 int     sysmon_wdog_setmode(struct sysmon_wdog *, int, u_int);
243 int     sysmon_wdog_register(struct sysmon_wdog *);
244 int     sysmon_wdog_unregister(struct sysmon_wdog *);
245 
246 void	sysmon_wdog_init(void);
247 
248 /*****************************************************************************
249  * Power management support
250  *****************************************************************************/
251 
252 struct sysmon_pswitch {
253 	const char *smpsw_name;		/* power switch name */
254 	int smpsw_type;			/* power switch type */
255 
256 	LIST_ENTRY(sysmon_pswitch) smpsw_list;
257 };
258 
259 int	sysmonopen_power(dev_t, int, int, struct lwp *);
260 int	sysmonclose_power(dev_t, int, int, struct lwp *);
261 int	sysmonread_power(dev_t, struct uio *, int);
262 int	sysmonpoll_power(dev_t, int, struct lwp *);
263 int	sysmonkqfilter_power(dev_t, struct knote *);
264 int	sysmonioctl_power(dev_t, u_long, void *, int, struct lwp *);
265 
266 void	sysmon_power_settype(const char *);
267 
268 int	sysmon_pswitch_register(struct sysmon_pswitch *);
269 void	sysmon_pswitch_unregister(struct sysmon_pswitch *);
270 
271 void	sysmon_pswitch_event(struct sysmon_pswitch *, int);
272 void	sysmon_penvsys_event(struct penvsys_state *, int);
273 
274 void	sysmon_power_init(void);
275 
276 #endif /* _DEV_SYSMON_SYSMONVAR_H_ */
277