1 /* $NetBSD: sysmon.c,v 1.11 2005/12/11 12:23:56 christos 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.11 2005/12/11 12:23:56 christos 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/lock.h> 49 #include <sys/callout.h> 50 #include <sys/kernel.h> 51 #include <sys/systm.h> 52 #include <sys/proc.h> 53 54 #include <dev/sysmon/sysmonvar.h> 55 #include <dev/sysmon/sysmonconf.h> 56 57 dev_type_open(sysmonopen); 58 dev_type_close(sysmonclose); 59 dev_type_ioctl(sysmonioctl); 60 dev_type_read(sysmonread); 61 dev_type_poll(sysmonpoll); 62 dev_type_kqfilter(sysmonkqfilter); 63 64 const struct cdevsw sysmon_cdevsw = { 65 sysmonopen, sysmonclose, sysmonread, nowrite, sysmonioctl, 66 nostop, notty, sysmonpoll, nommap, sysmonkqfilter, 67 }; 68 69 /* 70 * sysmonopen: 71 * 72 * Open the system monitor device. 73 */ 74 int 75 sysmonopen(dev_t dev, int flag, int mode, struct lwp *l) 76 { 77 int error; 78 79 switch (minor(dev)) { 80 #if NSYSMON_ENVSYS > 0 81 case SYSMON_MINOR_ENVSYS: 82 error = sysmonopen_envsys(dev, flag, mode, l); 83 break; 84 #endif 85 #if NSYSMON_WDOG > 0 86 case SYSMON_MINOR_WDOG: 87 error = sysmonopen_wdog(dev, flag, mode, l); 88 break; 89 #endif 90 #if NSYSMON_POWER > 0 91 case SYSMON_MINOR_POWER: 92 error = sysmonopen_power(dev, flag, mode, l); 93 break; 94 #endif 95 default: 96 error = ENODEV; 97 } 98 99 return (error); 100 } 101 102 /* 103 * sysmonclose: 104 * 105 * Close the system monitor device. 106 */ 107 int 108 sysmonclose(dev_t dev, int flag, int mode, struct lwp *l) 109 { 110 int error; 111 112 switch (minor(dev)) { 113 #if NSYSMON_ENVSYS > 0 114 case SYSMON_MINOR_ENVSYS: 115 error = sysmonclose_envsys(dev, flag, mode, l); 116 break; 117 #endif 118 #if NSYSMON_WDOG > 0 119 case SYSMON_MINOR_WDOG: 120 error = sysmonclose_wdog(dev, flag, mode, l); 121 break; 122 #endif 123 #if NSYSMON_POWER > 0 124 case SYSMON_MINOR_POWER: 125 error = sysmonclose_power(dev, flag, mode, l); 126 break; 127 #endif 128 default: 129 error = ENODEV; 130 } 131 132 return (error); 133 } 134 135 /* 136 * sysmonioctl: 137 * 138 * Perform a control request. 139 */ 140 int 141 sysmonioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l) 142 { 143 int error; 144 145 switch (minor(dev)) { 146 #if NSYSMON_ENVSYS > 0 147 case SYSMON_MINOR_ENVSYS: 148 error = sysmonioctl_envsys(dev, cmd, data, flag, l); 149 break; 150 #endif 151 #if NSYSMON_WDOG > 0 152 case SYSMON_MINOR_WDOG: 153 error = sysmonioctl_wdog(dev, cmd, data, flag, l); 154 break; 155 #endif 156 #if NSYSMON_POWER > 0 157 case SYSMON_MINOR_POWER: 158 error = sysmonioctl_power(dev, cmd, data, flag, l); 159 break; 160 #endif 161 default: 162 error = ENODEV; 163 } 164 165 return (error); 166 } 167 168 /* 169 * sysmonread: 170 * 171 * Perform a read request. 172 */ 173 int 174 sysmonread(dev_t dev, struct uio *uio, int flags) 175 { 176 int error; 177 178 switch (minor(dev)) { 179 #if NSYSMON_POWER > 0 180 case SYSMON_MINOR_POWER: 181 error = sysmonread_power(dev, uio, flags); 182 break; 183 #endif 184 default: 185 error = ENODEV; 186 } 187 188 return (error); 189 } 190 191 /* 192 * sysmonpoll: 193 * 194 * Poll the system monitor device. 195 */ 196 int 197 sysmonpoll(dev_t dev, int events, struct lwp *l) 198 { 199 int rv; 200 201 switch (minor(dev)) { 202 #if NSYSMON_POWER > 0 203 case SYSMON_MINOR_POWER: 204 rv = sysmonpoll_power(dev, events, l); 205 break; 206 #endif 207 default: 208 rv = events; 209 } 210 211 return (rv); 212 } 213 214 /* 215 * sysmonkqfilter: 216 * 217 * Kqueue filter for the system monitor device. 218 */ 219 int 220 sysmonkqfilter(dev_t dev, struct knote *kn) 221 { 222 int error; 223 224 switch (minor(dev)) { 225 #if NSYSMON_POWER > 0 226 case SYSMON_MINOR_POWER: 227 error = sysmonkqfilter_power(dev, kn); 228 break; 229 #endif 230 default: 231 error = 1; 232 } 233 234 return (error); 235 } 236