Home | History | Annotate | Line # | Download | only in ir
cir.c revision 1.4
      1 /*	$NetBSD: cir.c,v 1.4 2002/09/27 15:37:22 provos 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 (at) 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 dev_type_open(ciropen);
     54 dev_type_close(circlose);
     55 dev_type_read(cirread);
     56 dev_type_write(cirwrite);
     57 dev_type_ioctl(cirioctl);
     58 dev_type_poll(cirpoll);
     59 
     60 const struct cdevsw cir_cdevsw = {
     61 	ciropen, circlose, cirread, cirwrite, cirioctl,
     62 	nostop, notty, cirpoll, nommap,
     63 };
     64 
     65 int cir_match(struct device *parent, struct cfdata *match, void *aux);
     66 void cir_attach(struct device *parent, struct device *self, void *aux);
     67 int cir_activate(struct device *self, enum devact act);
     68 int cir_detach(struct device *self, int flags);
     69 
     70 struct cfattach cir_ca = {
     71 	sizeof(struct cir_softc), cir_match, cir_attach,
     72 	cir_detach, cir_activate
     73 };
     74 
     75 extern struct cfdriver cir_cd;
     76 
     77 #define CIRUNIT(dev) (minor(dev))
     78 
     79 int
     80 cir_match(struct device *parent, struct cfdata *match, void *aux)
     81 {
     82 	struct ir_attach_args *ia = aux;
     83 
     84 	return (ia->ia_type == IR_TYPE_CIR);
     85 }
     86 
     87 void
     88 cir_attach(struct device *parent, struct device *self, void *aux)
     89 {
     90 	struct cir_softc *sc = (struct cir_softc *)self;
     91 	struct ir_attach_args *ia = aux;
     92 
     93 	sc->sc_methods = ia->ia_methods;
     94 	sc->sc_handle = ia->ia_handle;
     95 
     96 #ifdef DIAGNOSTIC
     97 	if (sc->sc_methods->im_read == NULL ||
     98 	    sc->sc_methods->im_write == NULL ||
     99 	    sc->sc_methods->im_setparams == NULL)
    100 		panic("%s: missing methods", sc->sc_dev.dv_xname);
    101 #endif
    102 	printf("\n");
    103 }
    104 
    105 int
    106 cir_activate(struct device *self, enum devact act)
    107 {
    108 	/*struct cir_softc *sc = (struct cir_softc *)self;*/
    109 
    110 	switch (act) {
    111 	case DVACT_ACTIVATE:
    112 		return (EOPNOTSUPP);
    113 		break;
    114 
    115 	case DVACT_DEACTIVATE:
    116 		break;
    117 	}
    118 	return (0);
    119 }
    120 
    121 int
    122 cir_detach(struct device *self, int flags)
    123 {
    124 	/*struct cir_softc *sc = (struct cir_softc *)self;*/
    125 	int maj, mn;
    126 
    127 	/* locate the major number */
    128 	maj = cdevsw_lookup_major(&cir_cdevsw);
    129 
    130 	/* Nuke the vnodes for any open instances (calls close). */
    131 	mn = self->dv_unit;
    132 	vdevgone(maj, mn, mn, VCHR);
    133 
    134 	return (0);
    135 }
    136 
    137 int
    138 ciropen(dev_t dev, int flag, int mode, struct proc *p)
    139 {
    140 	struct cir_softc *sc;
    141 	int error;
    142 
    143 	sc = device_lookup(&cir_cd, CIRUNIT(dev));
    144 	if (sc == NULL)
    145 		return (ENXIO);
    146 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    147 		return (EIO);
    148 	if (sc->sc_open)
    149 		return (EBUSY);
    150 	if (sc->sc_methods->im_open != NULL) {
    151 		error = sc->sc_methods->im_open(sc->sc_handle, flag, mode, p);
    152 		if (error)
    153 			return (error);
    154 	}
    155 	sc->sc_open = 1;
    156 	return (0);
    157 }
    158 
    159 int
    160 circlose(dev_t dev, int flag, int mode, struct proc *p)
    161 {
    162 	struct cir_softc *sc;
    163 	int error;
    164 
    165 	sc = device_lookup(&cir_cd, CIRUNIT(dev));
    166 	if (sc == NULL)
    167 		return (ENXIO);
    168 	if (sc->sc_methods->im_close != NULL)
    169 		error = sc->sc_methods->im_close(sc->sc_handle, flag, mode, p);
    170 	else
    171 		error = 0;
    172 	sc->sc_open = 0;
    173 	return (error);
    174 }
    175 
    176 int
    177 cirread(dev_t dev, struct uio *uio, int flag)
    178 {
    179 	struct cir_softc *sc;
    180 
    181 	sc = device_lookup(&cir_cd, CIRUNIT(dev));
    182 	if (sc == NULL)
    183 		return (ENXIO);
    184 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    185 		return (EIO);
    186 	return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
    187 }
    188 
    189 int
    190 cirwrite(dev_t dev, struct uio *uio, int flag)
    191 {
    192 	struct cir_softc *sc;
    193 
    194 	sc = device_lookup(&cir_cd, CIRUNIT(dev));
    195 	if (sc == NULL)
    196 		return (ENXIO);
    197 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    198 		return (EIO);
    199 	return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
    200 }
    201 
    202 int
    203 cirioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
    204 {
    205 	struct cir_softc *sc;
    206 	int error;
    207 
    208 	sc = device_lookup(&cir_cd, CIRUNIT(dev));
    209 	if (sc == NULL)
    210 		return (ENXIO);
    211 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    212 		return (EIO);
    213 
    214 	switch (cmd) {
    215 	case FIONBIO:
    216 		/* All handled in the upper FS layer. */
    217 		error = 0;
    218 		break;
    219 	case CIR_GET_PARAMS:
    220 		*(struct cir_params *)addr = sc->sc_params;
    221 		break;
    222 	case CIR_SET_PARAMS:
    223 		error = sc->sc_methods->im_setparams(sc->sc_handle,
    224 			    (struct cir_params *)addr);
    225 		if (!error)
    226 			sc->sc_params = *(struct cir_params *)addr;
    227 		break;
    228 	default:
    229 		error = EINVAL;
    230 		break;
    231 	}
    232 	return (error);
    233 }
    234 
    235 int
    236 cirpoll(dev_t dev, int events, struct proc *p)
    237 {
    238 	struct cir_softc *sc;
    239 	int revents;
    240 	int s;
    241 
    242 	sc = device_lookup(&cir_cd, CIRUNIT(dev));
    243 	if (sc == NULL)
    244 		return (ENXIO);
    245 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    246 		return (EIO);
    247 
    248 	revents = 0;
    249 	s = splir();
    250 #if 0
    251 	if (events & (POLLIN | POLLRDNORM))
    252 		if (sc->sc_rdframes > 0)
    253 			revents |= events & (POLLIN | POLLRDNORM);
    254 #endif
    255 
    256 #if 0
    257 	/* How about write? */
    258 	if (events & (POLLOUT | POLLWRNORM))
    259 		if (???)
    260 			revents |= events & (POLLOUT | POLLWRNORM);
    261 #endif
    262 
    263 	if (revents == 0) {
    264 		if (events & (POLLIN | POLLRDNORM))
    265 			selrecord(p, &sc->sc_rdsel);
    266 
    267 #if 0
    268 		if (events & (POLLOUT | POLLWRNORM))
    269 			selrecord(p, &sc->sc_wrsel);
    270 #endif
    271 	}
    272 
    273 	splx(s);
    274 	return (revents);
    275 }
    276