1 /* $NetBSD: cir.c,v 1.2 2001/12/12 15:33:53 augustss Exp $ */ 2 3 /* 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart@augustsson.net). 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/ioctl.h> 42 #include <sys/kernel.h> 43 #include <sys/device.h> 44 #include <sys/conf.h> 45 #include <sys/poll.h> 46 #include <sys/select.h> 47 #include <sys/vnode.h> 48 49 #include <dev/ir/ir.h> 50 #include <dev/ir/cirio.h> 51 #include <dev/ir/cirvar.h> 52 53 cdev_decl(cir); 54 55 int cir_match(struct device *parent, struct cfdata *match, void *aux); 56 void cir_attach(struct device *parent, struct device *self, void *aux); 57 int cir_activate(struct device *self, enum devact act); 58 int cir_detach(struct device *self, int flags); 59 60 struct cfattach cir_ca = { 61 sizeof(struct cir_softc), cir_match, cir_attach, 62 cir_detach, cir_activate 63 }; 64 65 extern struct cfdriver cir_cd; 66 67 #define CIRUNIT(dev) (minor(dev)) 68 69 int 70 cir_match(struct device *parent, struct cfdata *match, void *aux) 71 { 72 struct ir_attach_args *ia = aux; 73 74 return (ia->ia_type == IR_TYPE_CIR); 75 } 76 77 void 78 cir_attach(struct device *parent, struct device *self, void *aux) 79 { 80 struct cir_softc *sc = (struct cir_softc *)self; 81 struct ir_attach_args *ia = aux; 82 83 sc->sc_methods = ia->ia_methods; 84 sc->sc_handle = ia->ia_handle; 85 86 #ifdef DIAGNOSTIC 87 if (sc->sc_methods->im_read == NULL || 88 sc->sc_methods->im_write == NULL || 89 sc->sc_methods->im_setparams == NULL) 90 panic("%s: missing methods\n", sc->sc_dev.dv_xname); 91 #endif 92 printf("\n"); 93 } 94 95 int 96 cir_activate(struct device *self, enum devact act) 97 { 98 /*struct cir_softc *sc = (struct cir_softc *)self;*/ 99 100 switch (act) { 101 case DVACT_ACTIVATE: 102 return (EOPNOTSUPP); 103 break; 104 105 case DVACT_DEACTIVATE: 106 break; 107 } 108 return (0); 109 } 110 111 int 112 cir_detach(struct device *self, int flags) 113 { 114 /*struct cir_softc *sc = (struct cir_softc *)self;*/ 115 int maj, mn; 116 117 /* locate the major number */ 118 for (maj = 0; maj < nchrdev; maj++) 119 if (cdevsw[maj].d_open == ciropen) 120 break; 121 122 /* Nuke the vnodes for any open instances (calls close). */ 123 mn = self->dv_unit; 124 vdevgone(maj, mn, mn, VCHR); 125 126 return (0); 127 } 128 129 int 130 ciropen(dev_t dev, int flag, int mode, struct proc *p) 131 { 132 struct cir_softc *sc; 133 int error; 134 135 sc = device_lookup(&cir_cd, CIRUNIT(dev)); 136 if (sc == NULL) 137 return (ENXIO); 138 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0) 139 return (EIO); 140 if (sc->sc_open) 141 return (EBUSY); 142 if (sc->sc_methods->im_open != NULL) { 143 error = sc->sc_methods->im_open(sc->sc_handle, flag, mode, p); 144 if (error) 145 return (error); 146 } 147 sc->sc_open = 1; 148 return (0); 149 } 150 151 int 152 circlose(dev_t dev, int flag, int mode, struct proc *p) 153 { 154 struct cir_softc *sc; 155 int error; 156 157 sc = device_lookup(&cir_cd, CIRUNIT(dev)); 158 if (sc == NULL) 159 return (ENXIO); 160 if (sc->sc_methods->im_close != NULL) 161 error = sc->sc_methods->im_close(sc->sc_handle, flag, mode, p); 162 else 163 error = 0; 164 sc->sc_open = 0; 165 return (error); 166 } 167 168 int 169 cirread(dev_t dev, struct uio *uio, int flag) 170 { 171 struct cir_softc *sc; 172 173 sc = device_lookup(&cir_cd, CIRUNIT(dev)); 174 if (sc == NULL) 175 return (ENXIO); 176 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0) 177 return (EIO); 178 return (sc->sc_methods->im_read(sc->sc_handle, uio, flag)); 179 } 180 181 int 182 cirwrite(dev_t dev, struct uio *uio, int flag) 183 { 184 struct cir_softc *sc; 185 186 sc = device_lookup(&cir_cd, CIRUNIT(dev)); 187 if (sc == NULL) 188 return (ENXIO); 189 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0) 190 return (EIO); 191 return (sc->sc_methods->im_write(sc->sc_handle, uio, flag)); 192 } 193 194 int 195 cirioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 196 { 197 struct cir_softc *sc; 198 int error; 199 200 sc = device_lookup(&cir_cd, CIRUNIT(dev)); 201 if (sc == NULL) 202 return (ENXIO); 203 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0) 204 return (EIO); 205 206 switch (cmd) { 207 case FIONBIO: 208 /* All handled in the upper FS layer. */ 209 error = 0; 210 break; 211 case CIR_GET_PARAMS: 212 *(struct cir_params *)addr = sc->sc_params; 213 break; 214 case CIR_SET_PARAMS: 215 error = sc->sc_methods->im_setparams(sc->sc_handle, 216 (struct cir_params *)addr); 217 if (!error) 218 sc->sc_params = *(struct cir_params *)addr; 219 break; 220 default: 221 error = EINVAL; 222 break; 223 } 224 return (error); 225 } 226 227 int 228 cirpoll(dev_t dev, int events, struct proc *p) 229 { 230 struct cir_softc *sc; 231 int revents; 232 int s; 233 234 sc = device_lookup(&cir_cd, CIRUNIT(dev)); 235 if (sc == NULL) 236 return (ENXIO); 237 if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0) 238 return (EIO); 239 240 revents = 0; 241 s = splir(); 242 #if 0 243 if (events & (POLLIN | POLLRDNORM)) 244 if (sc->sc_rdframes > 0) 245 revents |= events & (POLLIN | POLLRDNORM); 246 #endif 247 248 #if 0 249 /* How about write? */ 250 if (events & (POLLOUT | POLLWRNORM)) 251 if (???) 252 revents |= events & (POLLOUT | POLLWRNORM); 253 #endif 254 255 if (revents == 0) { 256 if (events & (POLLIN | POLLRDNORM)) 257 selrecord(p, &sc->sc_rdsel); 258 259 #if 0 260 if (events & (POLLOUT | POLLWRNORM)) 261 selrecord(p, &sc->sc_wrsel); 262 #endif 263 } 264 265 splx(s); 266 return (revents); 267 } 268