Home | History | Annotate | Line # | Download | only in ir
cir.c revision 1.31
      1 /*	$NetBSD: cir.c,v 1.31 2014/07/25 08:10:37 dholland 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.31 2014/07/25 08:10:37 dholland 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 #include <sys/module.h>
     45 
     46 #include <dev/ir/ir.h>
     47 #include <dev/ir/cirio.h>
     48 #include <dev/ir/cirvar.h>
     49 
     50 dev_type_open(ciropen);
     51 dev_type_close(circlose);
     52 dev_type_read(cirread);
     53 dev_type_write(cirwrite);
     54 dev_type_ioctl(cirioctl);
     55 dev_type_poll(cirpoll);
     56 
     57 const struct cdevsw cir_cdevsw = {
     58 	.d_open = ciropen,
     59 	.d_close = circlose,
     60 	.d_read = cirread,
     61 	.d_write = cirwrite,
     62 	.d_ioctl = cirioctl,
     63 	.d_stop = nostop,
     64 	.d_tty = notty,
     65 	.d_poll = cirpoll,
     66 	.d_mmap = nommap,
     67 	.d_kqfilter = nokqfilter,
     68 	.d_discard = nodiscard,
     69 	.d_flag = D_OTHER
     70 };
     71 
     72 int cir_match(device_t parent, cfdata_t match, void *aux);
     73 void cir_attach(device_t parent, device_t self, void *aux);
     74 int cir_detach(device_t self, int flags);
     75 
     76 CFATTACH_DECL_NEW(cir, sizeof(struct cir_softc),
     77     cir_match, cir_attach, cir_detach, NULL);
     78 
     79 extern struct cfdriver cir_cd;
     80 
     81 #define CIRUNIT(dev) (minor(dev))
     82 
     83 int
     84 cir_match(device_t parent, cfdata_t match, void *aux)
     85 {
     86 	struct ir_attach_args *ia = aux;
     87 
     88 	return (ia->ia_type == IR_TYPE_CIR);
     89 }
     90 
     91 void
     92 cir_attach(device_t parent, device_t self, void *aux)
     93 {
     94 	struct cir_softc *sc = device_private(self);
     95 	struct ir_attach_args *ia = aux;
     96 
     97 	sc->sc_dev = self;
     98 
     99 	selinit(&sc->sc_rdsel);
    100 	sc->sc_methods = ia->ia_methods;
    101 	sc->sc_handle = ia->ia_handle;
    102 
    103 #ifdef DIAGNOSTIC
    104 	if (sc->sc_methods->im_read == NULL ||
    105 	    sc->sc_methods->im_write == NULL ||
    106 	    sc->sc_methods->im_setparams == NULL)
    107 		panic("%s: missing methods", device_xname(sc->sc_dev));
    108 #endif
    109 	printf("\n");
    110 }
    111 
    112 int
    113 cir_detach(device_t self, int flags)
    114 {
    115 	struct cir_softc *sc = device_private(self);
    116 	int maj, mn;
    117 
    118 	/* locate the major number */
    119 	maj = cdevsw_lookup_major(&cir_cdevsw);
    120 
    121 	/* Nuke the vnodes for any open instances (calls close). */
    122 	mn = device_unit(self);
    123 	vdevgone(maj, mn, mn, VCHR);
    124 
    125 	seldestroy(&sc->sc_rdsel);
    126 
    127 	return (0);
    128 }
    129 
    130 int
    131 ciropen(dev_t dev, int flag, int mode, struct lwp *l)
    132 {
    133 	struct cir_softc *sc;
    134 	int error;
    135 
    136 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    137 	if (sc == NULL)
    138 		return (ENXIO);
    139 	if (!device_is_active(sc->sc_dev))
    140 		return (EIO);
    141 	if (sc->sc_open)
    142 		return (EBUSY);
    143 
    144 	sc->sc_rdframes = 0;
    145 	if (sc->sc_methods->im_open != NULL) {
    146 		error = sc->sc_methods->im_open(sc->sc_handle, flag, mode,
    147 		    l->l_proc);
    148 		if (error)
    149 			return (error);
    150 	}
    151 	sc->sc_open = 1;
    152 	return (0);
    153 }
    154 
    155 int
    156 circlose(dev_t dev, int flag, int mode, struct lwp *l)
    157 {
    158 	struct cir_softc *sc;
    159 	int error;
    160 
    161 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    162 	if (sc == NULL)
    163 		return (ENXIO);
    164 	if (sc->sc_methods->im_close != NULL)
    165 		error = sc->sc_methods->im_close(sc->sc_handle, flag, mode,
    166 		    l->l_proc);
    167 	else
    168 		error = 0;
    169 	sc->sc_open = 0;
    170 	return (error);
    171 }
    172 
    173 int
    174 cirread(dev_t dev, struct uio *uio, int flag)
    175 {
    176 	struct cir_softc *sc;
    177 
    178 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    179 	if (sc == NULL)
    180 		return (ENXIO);
    181 	if (!device_is_active(sc->sc_dev))
    182 		return (EIO);
    183 	return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
    184 }
    185 
    186 int
    187 cirwrite(dev_t dev, struct uio *uio, int flag)
    188 {
    189 	struct cir_softc *sc;
    190 
    191 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    192 	if (sc == NULL)
    193 		return (ENXIO);
    194 	if (!device_is_active(sc->sc_dev))
    195 		return (EIO);
    196 	return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
    197 }
    198 
    199 int
    200 cirioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    201 {
    202 	struct cir_softc *sc;
    203 	int error;
    204 
    205 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    206 	if (sc == NULL)
    207 		return (ENXIO);
    208 	if (!device_is_active(sc->sc_dev))
    209 		return (EIO);
    210 
    211 	switch (cmd) {
    212 	case FIONBIO:
    213 		/* All handled in the upper FS layer. */
    214 		error = 0;
    215 		break;
    216 	case CIR_GET_PARAMS:
    217 		*(struct cir_params *)addr = sc->sc_params;
    218 		error = 0;
    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 lwp *l)
    235 {
    236 	struct cir_softc *sc;
    237 	int revents;
    238 	int s;
    239 
    240 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    241 	if (sc == NULL)
    242 		return (POLLERR);
    243 	if (!device_is_active(sc->sc_dev))
    244 		return (POLLERR);
    245 
    246 	revents = 0;
    247 	s = splir();
    248 	if (events & (POLLIN | POLLRDNORM))
    249 		if (sc->sc_rdframes > 0)
    250 			revents |= events & (POLLIN | POLLRDNORM);
    251 
    252 #if 0
    253 	/* How about write? */
    254 	if (events & (POLLOUT | POLLWRNORM))
    255 		if (/* ??? */)
    256 			revents |= events & (POLLOUT | POLLWRNORM);
    257 #endif
    258 
    259 	if (revents == 0) {
    260 		if (events & (POLLIN | POLLRDNORM))
    261 			selrecord(l, &sc->sc_rdsel);
    262 
    263 #if 0
    264 		if (events & (POLLOUT | POLLWRNORM))
    265 			selrecord(p, &sc->sc_wrsel);
    266 #endif
    267 	}
    268 
    269 	splx(s);
    270 	return (revents);
    271 }
    272 
    273 MODULE(MODULE_CLASS_DRIVER, cir, "ir");
    274 
    275 #ifdef _MODULE
    276 #include "ioconf.c"
    277 #endif
    278 
    279 static int
    280 cir_modcmd(modcmd_t cmd, void *opaque)
    281 {
    282 	int error = 0;
    283 #ifdef _MODULE
    284 	int bmaj = -1, cmaj = -1;
    285 #endif
    286 
    287 	switch (cmd) {
    288 	case MODULE_CMD_INIT:
    289 #ifdef _MODULE
    290 		error = config_init_component(cfdriver_ioconf_cir,
    291 		    cfattach_ioconf_cir, cfdata_ioconf_cir);
    292 		if (error)
    293 			return error;
    294 		error = devsw_attach("cir", NULL, &bmaj, &cir_cdevsw, &cmaj);
    295 		if (error)
    296 			config_fini_component(cfdriver_ioconf_cir,
    297 			    cfattach_ioconf_cir, cfdata_ioconf_cir);
    298 #endif
    299 		return error;
    300 	case MODULE_CMD_FINI:
    301 #ifdef _MODULE
    302 		devsw_detach(NULL, &cir_cdevsw);
    303 		return config_fini_component(cfdriver_ioconf_cir,
    304 		    cfattach_ioconf_cir, cfdata_ioconf_cir);
    305 #endif
    306 		return error;
    307 	default:
    308 		return ENOTTY;
    309 	}
    310 }
    311