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