Home | History | Annotate | Line # | Download | only in ir
cir.c revision 1.26
      1 /*	$NetBSD: cir.c,v 1.26 2009/12/06 22:40:56 dyoung 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: cir.c,v 1.26 2009/12/06 22:40:56 dyoung Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/ioctl.h>
     38 #include <sys/kernel.h>
     39 #include <sys/device.h>
     40 #include <sys/conf.h>
     41 #include <sys/poll.h>
     42 #include <sys/select.h>
     43 #include <sys/vnode.h>
     44 
     45 #include <dev/ir/ir.h>
     46 #include <dev/ir/cirio.h>
     47 #include <dev/ir/cirvar.h>
     48 
     49 dev_type_open(ciropen);
     50 dev_type_close(circlose);
     51 dev_type_read(cirread);
     52 dev_type_write(cirwrite);
     53 dev_type_ioctl(cirioctl);
     54 dev_type_poll(cirpoll);
     55 
     56 const struct cdevsw cir_cdevsw = {
     57 	ciropen, circlose, cirread, cirwrite, cirioctl,
     58 	nostop, notty, cirpoll, nommap, nokqfilter,
     59 	D_OTHER
     60 };
     61 
     62 int cir_match(device_t parent, cfdata_t match, void *aux);
     63 void cir_attach(device_t parent, device_t self, void *aux);
     64 int cir_detach(device_t self, int flags);
     65 
     66 CFATTACH_DECL(cir, sizeof(struct cir_softc),
     67     cir_match, cir_attach, cir_detach, NULL);
     68 
     69 extern struct cfdriver cir_cd;
     70 
     71 #define CIRUNIT(dev) (minor(dev))
     72 
     73 int
     74 cir_match(device_t parent, cfdata_t match, void *aux)
     75 {
     76 	struct ir_attach_args *ia = aux;
     77 
     78 	return (ia->ia_type == IR_TYPE_CIR);
     79 }
     80 
     81 void
     82 cir_attach(device_t parent, device_t self, void *aux)
     83 {
     84 	struct cir_softc *sc = device_private(self);
     85 	struct ir_attach_args *ia = aux;
     86 
     87 	selinit(&sc->sc_rdsel);
     88 	sc->sc_methods = ia->ia_methods;
     89 	sc->sc_handle = ia->ia_handle;
     90 
     91 #ifdef DIAGNOSTIC
     92 	if (sc->sc_methods->im_read == NULL ||
     93 	    sc->sc_methods->im_write == NULL ||
     94 	    sc->sc_methods->im_setparams == NULL)
     95 		panic("%s: missing methods", device_xname(&sc->sc_dev));
     96 #endif
     97 	printf("\n");
     98 }
     99 
    100 int
    101 cir_detach(device_t self, int flags)
    102 {
    103 	struct cir_softc *sc = device_private(self);
    104 	int maj, mn;
    105 
    106 	/* locate the major number */
    107 	maj = cdevsw_lookup_major(&cir_cdevsw);
    108 
    109 	/* Nuke the vnodes for any open instances (calls close). */
    110 	mn = device_unit(self);
    111 	vdevgone(maj, mn, mn, VCHR);
    112 
    113 	seldestroy(&sc->sc_rdsel);
    114 
    115 	return (0);
    116 }
    117 
    118 int
    119 ciropen(dev_t dev, int flag, int mode, struct lwp *l)
    120 {
    121 	struct cir_softc *sc;
    122 	int error;
    123 
    124 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    125 	if (sc == NULL)
    126 		return (ENXIO);
    127 	if (!device_is_active(&sc->sc_dev))
    128 		return (EIO);
    129 	if (sc->sc_open)
    130 		return (EBUSY);
    131 
    132 	sc->sc_rdframes = 0;
    133 	if (sc->sc_methods->im_open != NULL) {
    134 		error = sc->sc_methods->im_open(sc->sc_handle, flag, mode,
    135 		    l->l_proc);
    136 		if (error)
    137 			return (error);
    138 	}
    139 	sc->sc_open = 1;
    140 	return (0);
    141 }
    142 
    143 int
    144 circlose(dev_t dev, int flag, int mode, struct lwp *l)
    145 {
    146 	struct cir_softc *sc;
    147 	int error;
    148 
    149 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    150 	if (sc == NULL)
    151 		return (ENXIO);
    152 	if (sc->sc_methods->im_close != NULL)
    153 		error = sc->sc_methods->im_close(sc->sc_handle, flag, mode,
    154 		    l->l_proc);
    155 	else
    156 		error = 0;
    157 	sc->sc_open = 0;
    158 	return (error);
    159 }
    160 
    161 int
    162 cirread(dev_t dev, struct uio *uio, int flag)
    163 {
    164 	struct cir_softc *sc;
    165 
    166 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    167 	if (sc == NULL)
    168 		return (ENXIO);
    169 	if (!device_is_active(&sc->sc_dev))
    170 		return (EIO);
    171 	return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
    172 }
    173 
    174 int
    175 cirwrite(dev_t dev, struct uio *uio, int flag)
    176 {
    177 	struct cir_softc *sc;
    178 
    179 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    180 	if (sc == NULL)
    181 		return (ENXIO);
    182 	if (!device_is_active(&sc->sc_dev))
    183 		return (EIO);
    184 	return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
    185 }
    186 
    187 int
    188 cirioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    189 {
    190 	struct cir_softc *sc;
    191 	int error;
    192 
    193 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    194 	if (sc == NULL)
    195 		return (ENXIO);
    196 	if (!device_is_active(&sc->sc_dev))
    197 		return (EIO);
    198 
    199 	switch (cmd) {
    200 	case FIONBIO:
    201 		/* All handled in the upper FS layer. */
    202 		error = 0;
    203 		break;
    204 	case CIR_GET_PARAMS:
    205 		*(struct cir_params *)addr = sc->sc_params;
    206 		error = 0;
    207 		break;
    208 	case CIR_SET_PARAMS:
    209 		error = sc->sc_methods->im_setparams(sc->sc_handle,
    210 			    (struct cir_params *)addr);
    211 		if (!error)
    212 			sc->sc_params = *(struct cir_params *)addr;
    213 		break;
    214 	default:
    215 		error = EINVAL;
    216 		break;
    217 	}
    218 	return (error);
    219 }
    220 
    221 int
    222 cirpoll(dev_t dev, int events, struct lwp *l)
    223 {
    224 	struct cir_softc *sc;
    225 	int revents;
    226 	int s;
    227 
    228 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    229 	if (sc == NULL)
    230 		return (POLLERR);
    231 	if (!device_is_active(&sc->sc_dev))
    232 		return (POLLERR);
    233 
    234 	revents = 0;
    235 	s = splir();
    236 	if (events & (POLLIN | POLLRDNORM))
    237 		if (sc->sc_rdframes > 0)
    238 			revents |= events & (POLLIN | POLLRDNORM);
    239 
    240 #if 0
    241 	/* How about write? */
    242 	if (events & (POLLOUT | POLLWRNORM))
    243 		if (/* ??? */)
    244 			revents |= events & (POLLOUT | POLLWRNORM);
    245 #endif
    246 
    247 	if (revents == 0) {
    248 		if (events & (POLLIN | POLLRDNORM))
    249 			selrecord(l, &sc->sc_rdsel);
    250 
    251 #if 0
    252 		if (events & (POLLOUT | POLLWRNORM))
    253 			selrecord(p, &sc->sc_wrsel);
    254 #endif
    255 	}
    256 
    257 	splx(s);
    258 	return (revents);
    259 }
    260