xref: /netbsd-src/sys/dev/sysmon/sysmon.c (revision aad9773e38ed2370a628a6416e098f9008fc10a7)
1 /*	$NetBSD: sysmon.c,v 1.19 2014/07/25 08:10:39 dholland 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 /*
37  * Clearing house for system monitoring hardware.  We currently
38  * handle environmental sensors, watchdog timers, and power management.
39  */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: sysmon.c,v 1.19 2014/07/25 08:10:39 dholland Exp $");
43 
44 #include <sys/param.h>
45 #include <sys/conf.h>
46 #include <sys/errno.h>
47 #include <sys/fcntl.h>
48 #include <sys/callout.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/proc.h>
52 
53 #include <dev/sysmon/sysmonvar.h>
54 #include <dev/sysmon/sysmonconf.h>
55 
56 dev_type_open(sysmonopen);
57 dev_type_close(sysmonclose);
58 dev_type_ioctl(sysmonioctl);
59 dev_type_read(sysmonread);
60 dev_type_poll(sysmonpoll);
61 dev_type_kqfilter(sysmonkqfilter);
62 
63 const struct cdevsw sysmon_cdevsw = {
64 	.d_open = sysmonopen,
65 	.d_close = sysmonclose,
66 	.d_read = sysmonread,
67 	.d_write = nowrite,
68 	.d_ioctl = sysmonioctl,
69 	.d_stop = nostop,
70 	.d_tty = notty,
71 	.d_poll = sysmonpoll,
72 	.d_mmap = nommap,
73 	.d_kqfilter = sysmonkqfilter,
74 	.d_discard = nodiscard,
75 	.d_flag = D_OTHER | D_MPSAFE
76 };
77 
78 /*
79  * sysmonopen:
80  *
81  *	Open the system monitor device.
82  */
83 int
84 sysmonopen(dev_t dev, int flag, int mode, struct lwp *l)
85 {
86 	int error;
87 
88 	switch (minor(dev)) {
89 #if NSYSMON_ENVSYS > 0
90 	case SYSMON_MINOR_ENVSYS:
91 		error = sysmonopen_envsys(dev, flag, mode, l);
92 		break;
93 #endif
94 #if NSYSMON_WDOG > 0
95 	case SYSMON_MINOR_WDOG:
96 		error = sysmonopen_wdog(dev, flag, mode, l);
97 		break;
98 #endif
99 #if NSYSMON_POWER > 0
100 	case SYSMON_MINOR_POWER:
101 		error = sysmonopen_power(dev, flag, mode, l);
102 		break;
103 #endif
104 	default:
105 		error = ENODEV;
106 	}
107 
108 	return (error);
109 }
110 
111 /*
112  * sysmonclose:
113  *
114  *	Close the system monitor device.
115  */
116 int
117 sysmonclose(dev_t dev, int flag, int mode, struct lwp *l)
118 {
119 	int error;
120 
121 	switch (minor(dev)) {
122 #if NSYSMON_ENVSYS > 0
123 	case SYSMON_MINOR_ENVSYS:
124 		error = sysmonclose_envsys(dev, flag, mode, l);
125 		break;
126 #endif
127 #if NSYSMON_WDOG > 0
128 	case SYSMON_MINOR_WDOG:
129 		error = sysmonclose_wdog(dev, flag, mode, l);
130 		break;
131 #endif
132 #if NSYSMON_POWER > 0
133 	case SYSMON_MINOR_POWER:
134 		error = sysmonclose_power(dev, flag, mode, l);
135 		break;
136 #endif
137 	default:
138 		error = ENODEV;
139 	}
140 
141 	return (error);
142 }
143 
144 /*
145  * sysmonioctl:
146  *
147  *	Perform a control request.
148  */
149 int
150 sysmonioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
151 {
152 	int error;
153 
154 	switch (minor(dev)) {
155 #if NSYSMON_ENVSYS > 0
156 	case SYSMON_MINOR_ENVSYS:
157 		error = sysmonioctl_envsys(dev, cmd, data, flag, l);
158 		break;
159 #endif
160 #if NSYSMON_WDOG > 0
161 	case SYSMON_MINOR_WDOG:
162 		error = sysmonioctl_wdog(dev, cmd, data, flag, l);
163 		break;
164 #endif
165 #if NSYSMON_POWER > 0
166 	case SYSMON_MINOR_POWER:
167 		error = sysmonioctl_power(dev, cmd, data, flag, l);
168 		break;
169 #endif
170 	default:
171 		error = ENODEV;
172 	}
173 
174 	return (error);
175 }
176 
177 /*
178  * sysmonread:
179  *
180  *	Perform a read request.
181  */
182 int
183 sysmonread(dev_t dev, struct uio *uio, int flags)
184 {
185 	int error;
186 
187 	switch (minor(dev)) {
188 #if NSYSMON_POWER > 0
189 	case SYSMON_MINOR_POWER:
190 		error = sysmonread_power(dev, uio, flags);
191 		break;
192 #endif
193 	default:
194 		error = ENODEV;
195 	}
196 
197 	return (error);
198 }
199 
200 /*
201  * sysmonpoll:
202  *
203  *	Poll the system monitor device.
204  */
205 int
206 sysmonpoll(dev_t dev, int events, struct lwp *l)
207 {
208 	int rv;
209 
210 	switch (minor(dev)) {
211 #if NSYSMON_POWER > 0
212 	case SYSMON_MINOR_POWER:
213 		rv = sysmonpoll_power(dev, events, l);
214 		break;
215 #endif
216 	default:
217 		rv = events;
218 	}
219 
220 	return (rv);
221 }
222 
223 /*
224  * sysmonkqfilter:
225  *
226  *	Kqueue filter for the system monitor device.
227  */
228 int
229 sysmonkqfilter(dev_t dev, struct knote *kn)
230 {
231 	int error;
232 
233 	switch (minor(dev)) {
234 #if NSYSMON_POWER > 0
235 	case SYSMON_MINOR_POWER:
236 		error = sysmonkqfilter_power(dev, kn);
237 		break;
238 #endif
239 	default:
240 		error = 1;
241 	}
242 
243 	return (error);
244 }
245