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