1 /* $NetBSD: sysmon.c,v 1.18 2014/03/16 05:20:29 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.18 2014/03/16 05:20:29 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_flag = D_OTHER | D_MPSAFE 75 }; 76 77 /* 78 * sysmonopen: 79 * 80 * Open the system monitor device. 81 */ 82 int 83 sysmonopen(dev_t dev, int flag, int mode, struct lwp *l) 84 { 85 int error; 86 87 switch (minor(dev)) { 88 #if NSYSMON_ENVSYS > 0 89 case SYSMON_MINOR_ENVSYS: 90 error = sysmonopen_envsys(dev, flag, mode, l); 91 break; 92 #endif 93 #if NSYSMON_WDOG > 0 94 case SYSMON_MINOR_WDOG: 95 error = sysmonopen_wdog(dev, flag, mode, l); 96 break; 97 #endif 98 #if NSYSMON_POWER > 0 99 case SYSMON_MINOR_POWER: 100 error = sysmonopen_power(dev, flag, mode, l); 101 break; 102 #endif 103 default: 104 error = ENODEV; 105 } 106 107 return (error); 108 } 109 110 /* 111 * sysmonclose: 112 * 113 * Close the system monitor device. 114 */ 115 int 116 sysmonclose(dev_t dev, int flag, int mode, struct lwp *l) 117 { 118 int error; 119 120 switch (minor(dev)) { 121 #if NSYSMON_ENVSYS > 0 122 case SYSMON_MINOR_ENVSYS: 123 error = sysmonclose_envsys(dev, flag, mode, l); 124 break; 125 #endif 126 #if NSYSMON_WDOG > 0 127 case SYSMON_MINOR_WDOG: 128 error = sysmonclose_wdog(dev, flag, mode, l); 129 break; 130 #endif 131 #if NSYSMON_POWER > 0 132 case SYSMON_MINOR_POWER: 133 error = sysmonclose_power(dev, flag, mode, l); 134 break; 135 #endif 136 default: 137 error = ENODEV; 138 } 139 140 return (error); 141 } 142 143 /* 144 * sysmonioctl: 145 * 146 * Perform a control request. 147 */ 148 int 149 sysmonioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 150 { 151 int error; 152 153 switch (minor(dev)) { 154 #if NSYSMON_ENVSYS > 0 155 case SYSMON_MINOR_ENVSYS: 156 error = sysmonioctl_envsys(dev, cmd, data, flag, l); 157 break; 158 #endif 159 #if NSYSMON_WDOG > 0 160 case SYSMON_MINOR_WDOG: 161 error = sysmonioctl_wdog(dev, cmd, data, flag, l); 162 break; 163 #endif 164 #if NSYSMON_POWER > 0 165 case SYSMON_MINOR_POWER: 166 error = sysmonioctl_power(dev, cmd, data, flag, l); 167 break; 168 #endif 169 default: 170 error = ENODEV; 171 } 172 173 return (error); 174 } 175 176 /* 177 * sysmonread: 178 * 179 * Perform a read request. 180 */ 181 int 182 sysmonread(dev_t dev, struct uio *uio, int flags) 183 { 184 int error; 185 186 switch (minor(dev)) { 187 #if NSYSMON_POWER > 0 188 case SYSMON_MINOR_POWER: 189 error = sysmonread_power(dev, uio, flags); 190 break; 191 #endif 192 default: 193 error = ENODEV; 194 } 195 196 return (error); 197 } 198 199 /* 200 * sysmonpoll: 201 * 202 * Poll the system monitor device. 203 */ 204 int 205 sysmonpoll(dev_t dev, int events, struct lwp *l) 206 { 207 int rv; 208 209 switch (minor(dev)) { 210 #if NSYSMON_POWER > 0 211 case SYSMON_MINOR_POWER: 212 rv = sysmonpoll_power(dev, events, l); 213 break; 214 #endif 215 default: 216 rv = events; 217 } 218 219 return (rv); 220 } 221 222 /* 223 * sysmonkqfilter: 224 * 225 * Kqueue filter for the system monitor device. 226 */ 227 int 228 sysmonkqfilter(dev_t dev, struct knote *kn) 229 { 230 int error; 231 232 switch (minor(dev)) { 233 #if NSYSMON_POWER > 0 234 case SYSMON_MINOR_POWER: 235 error = sysmonkqfilter_power(dev, kn); 236 break; 237 #endif 238 default: 239 error = 1; 240 } 241 242 return (error); 243 } 244