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