Home | History | Annotate | Line # | Download | only in ir
cir.c revision 1.30
      1  1.30  dholland /*	$NetBSD: cir.c,v 1.30 2014/03/16 05:20:28 dholland Exp $	*/
      2   1.1  augustss 
      3   1.1  augustss /*
      4   1.1  augustss  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5   1.1  augustss  * All rights reserved.
      6   1.1  augustss  *
      7   1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  augustss  * by Lennart Augustsson (lennart (at) augustsson.net).
      9   1.1  augustss  *
     10   1.1  augustss  * Redistribution and use in source and binary forms, with or without
     11   1.1  augustss  * modification, are permitted provided that the following conditions
     12   1.1  augustss  * are met:
     13   1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     14   1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     15   1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  augustss  *    documentation and/or other materials provided with the distribution.
     18   1.1  augustss  *
     19   1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  augustss  */
     31   1.9     lukem 
     32   1.9     lukem #include <sys/cdefs.h>
     33  1.30  dholland __KERNEL_RCSID(0, "$NetBSD: cir.c,v 1.30 2014/03/16 05:20:28 dholland Exp $");
     34   1.1  augustss 
     35   1.1  augustss #include <sys/param.h>
     36   1.1  augustss #include <sys/systm.h>
     37   1.1  augustss #include <sys/ioctl.h>
     38   1.1  augustss #include <sys/kernel.h>
     39   1.1  augustss #include <sys/device.h>
     40   1.1  augustss #include <sys/conf.h>
     41   1.1  augustss #include <sys/poll.h>
     42   1.1  augustss #include <sys/select.h>
     43   1.1  augustss #include <sys/vnode.h>
     44  1.27  jmcneill #include <sys/module.h>
     45   1.1  augustss 
     46   1.2  augustss #include <dev/ir/ir.h>
     47   1.2  augustss #include <dev/ir/cirio.h>
     48   1.1  augustss #include <dev/ir/cirvar.h>
     49   1.1  augustss 
     50   1.3   gehenna dev_type_open(ciropen);
     51   1.3   gehenna dev_type_close(circlose);
     52   1.3   gehenna dev_type_read(cirread);
     53   1.3   gehenna dev_type_write(cirwrite);
     54   1.3   gehenna dev_type_ioctl(cirioctl);
     55   1.3   gehenna dev_type_poll(cirpoll);
     56   1.3   gehenna 
     57   1.3   gehenna const struct cdevsw cir_cdevsw = {
     58  1.30  dholland 	.d_open = ciropen,
     59  1.30  dholland 	.d_close = circlose,
     60  1.30  dholland 	.d_read = cirread,
     61  1.30  dholland 	.d_write = cirwrite,
     62  1.30  dholland 	.d_ioctl = cirioctl,
     63  1.30  dholland 	.d_stop = nostop,
     64  1.30  dholland 	.d_tty = notty,
     65  1.30  dholland 	.d_poll = cirpoll,
     66  1.30  dholland 	.d_mmap = nommap,
     67  1.30  dholland 	.d_kqfilter = nokqfilter,
     68  1.30  dholland 	.d_flag = D_OTHER
     69   1.3   gehenna };
     70   1.1  augustss 
     71  1.25    cegger int cir_match(device_t parent, cfdata_t match, void *aux);
     72  1.25    cegger void cir_attach(device_t parent, device_t self, void *aux);
     73  1.25    cegger int cir_detach(device_t self, int flags);
     74   1.1  augustss 
     75  1.29       mrg CFATTACH_DECL_NEW(cir, sizeof(struct cir_softc),
     76  1.26    dyoung     cir_match, cir_attach, cir_detach, NULL);
     77   1.1  augustss 
     78   1.1  augustss extern struct cfdriver cir_cd;
     79   1.1  augustss 
     80   1.1  augustss #define CIRUNIT(dev) (minor(dev))
     81   1.1  augustss 
     82   1.1  augustss int
     83  1.25    cegger cir_match(device_t parent, cfdata_t match, void *aux)
     84   1.1  augustss {
     85   1.1  augustss 	struct ir_attach_args *ia = aux;
     86   1.1  augustss 
     87   1.1  augustss 	return (ia->ia_type == IR_TYPE_CIR);
     88   1.1  augustss }
     89   1.1  augustss 
     90   1.1  augustss void
     91  1.25    cegger cir_attach(device_t parent, device_t self, void *aux)
     92   1.1  augustss {
     93  1.14   thorpej 	struct cir_softc *sc = device_private(self);
     94   1.1  augustss 	struct ir_attach_args *ia = aux;
     95   1.1  augustss 
     96  1.29       mrg 	sc->sc_dev = self;
     97  1.29       mrg 
     98  1.18     rmind 	selinit(&sc->sc_rdsel);
     99   1.1  augustss 	sc->sc_methods = ia->ia_methods;
    100   1.1  augustss 	sc->sc_handle = ia->ia_handle;
    101   1.1  augustss 
    102   1.1  augustss #ifdef DIAGNOSTIC
    103   1.1  augustss 	if (sc->sc_methods->im_read == NULL ||
    104   1.2  augustss 	    sc->sc_methods->im_write == NULL ||
    105   1.2  augustss 	    sc->sc_methods->im_setparams == NULL)
    106  1.29       mrg 		panic("%s: missing methods", device_xname(sc->sc_dev));
    107   1.1  augustss #endif
    108   1.1  augustss 	printf("\n");
    109   1.1  augustss }
    110   1.1  augustss 
    111   1.1  augustss int
    112  1.25    cegger cir_detach(device_t self, int flags)
    113   1.1  augustss {
    114  1.18     rmind 	struct cir_softc *sc = device_private(self);
    115   1.1  augustss 	int maj, mn;
    116   1.1  augustss 
    117   1.1  augustss 	/* locate the major number */
    118   1.3   gehenna 	maj = cdevsw_lookup_major(&cir_cdevsw);
    119   1.1  augustss 
    120   1.1  augustss 	/* Nuke the vnodes for any open instances (calls close). */
    121  1.13   thorpej 	mn = device_unit(self);
    122   1.1  augustss 	vdevgone(maj, mn, mn, VCHR);
    123   1.1  augustss 
    124  1.18     rmind 	seldestroy(&sc->sc_rdsel);
    125  1.18     rmind 
    126   1.1  augustss 	return (0);
    127   1.1  augustss }
    128   1.1  augustss 
    129   1.1  augustss int
    130  1.16      cube ciropen(dev_t dev, int flag, int mode, struct lwp *l)
    131   1.1  augustss {
    132   1.1  augustss 	struct cir_softc *sc;
    133   1.1  augustss 	int error;
    134   1.1  augustss 
    135  1.21    cegger 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    136   1.1  augustss 	if (sc == NULL)
    137   1.1  augustss 		return (ENXIO);
    138  1.29       mrg 	if (!device_is_active(sc->sc_dev))
    139   1.1  augustss 		return (EIO);
    140   1.1  augustss 	if (sc->sc_open)
    141   1.1  augustss 		return (EBUSY);
    142  1.23  jmcneill 
    143  1.23  jmcneill 	sc->sc_rdframes = 0;
    144   1.1  augustss 	if (sc->sc_methods->im_open != NULL) {
    145  1.16      cube 		error = sc->sc_methods->im_open(sc->sc_handle, flag, mode,
    146  1.16      cube 		    l->l_proc);
    147   1.1  augustss 		if (error)
    148   1.1  augustss 			return (error);
    149   1.1  augustss 	}
    150   1.1  augustss 	sc->sc_open = 1;
    151   1.1  augustss 	return (0);
    152   1.1  augustss }
    153   1.1  augustss 
    154   1.1  augustss int
    155  1.16      cube circlose(dev_t dev, int flag, int mode, struct lwp *l)
    156   1.1  augustss {
    157   1.1  augustss 	struct cir_softc *sc;
    158   1.1  augustss 	int error;
    159   1.1  augustss 
    160  1.21    cegger 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    161   1.1  augustss 	if (sc == NULL)
    162   1.1  augustss 		return (ENXIO);
    163   1.1  augustss 	if (sc->sc_methods->im_close != NULL)
    164  1.16      cube 		error = sc->sc_methods->im_close(sc->sc_handle, flag, mode,
    165  1.16      cube 		    l->l_proc);
    166   1.1  augustss 	else
    167   1.1  augustss 		error = 0;
    168   1.1  augustss 	sc->sc_open = 0;
    169   1.1  augustss 	return (error);
    170   1.1  augustss }
    171   1.1  augustss 
    172   1.1  augustss int
    173   1.1  augustss cirread(dev_t dev, struct uio *uio, int flag)
    174   1.1  augustss {
    175   1.1  augustss 	struct cir_softc *sc;
    176   1.1  augustss 
    177  1.21    cegger 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    178   1.1  augustss 	if (sc == NULL)
    179   1.1  augustss 		return (ENXIO);
    180  1.29       mrg 	if (!device_is_active(sc->sc_dev))
    181   1.1  augustss 		return (EIO);
    182   1.1  augustss 	return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
    183   1.1  augustss }
    184   1.1  augustss 
    185   1.1  augustss int
    186   1.1  augustss cirwrite(dev_t dev, struct uio *uio, int flag)
    187   1.1  augustss {
    188   1.1  augustss 	struct cir_softc *sc;
    189   1.1  augustss 
    190  1.21    cegger 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    191   1.1  augustss 	if (sc == NULL)
    192   1.1  augustss 		return (ENXIO);
    193  1.29       mrg 	if (!device_is_active(sc->sc_dev))
    194   1.1  augustss 		return (EIO);
    195   1.1  augustss 	return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
    196   1.1  augustss }
    197   1.1  augustss 
    198   1.1  augustss int
    199  1.17  christos cirioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    200   1.1  augustss {
    201   1.1  augustss 	struct cir_softc *sc;
    202   1.1  augustss 	int error;
    203   1.1  augustss 
    204  1.21    cegger 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    205   1.1  augustss 	if (sc == NULL)
    206   1.1  augustss 		return (ENXIO);
    207  1.29       mrg 	if (!device_is_active(sc->sc_dev))
    208   1.1  augustss 		return (EIO);
    209   1.1  augustss 
    210   1.1  augustss 	switch (cmd) {
    211   1.1  augustss 	case FIONBIO:
    212   1.1  augustss 		/* All handled in the upper FS layer. */
    213   1.1  augustss 		error = 0;
    214   1.2  augustss 		break;
    215   1.2  augustss 	case CIR_GET_PARAMS:
    216   1.2  augustss 		*(struct cir_params *)addr = sc->sc_params;
    217  1.16      cube 		error = 0;
    218   1.2  augustss 		break;
    219   1.2  augustss 	case CIR_SET_PARAMS:
    220   1.2  augustss 		error = sc->sc_methods->im_setparams(sc->sc_handle,
    221   1.2  augustss 			    (struct cir_params *)addr);
    222   1.2  augustss 		if (!error)
    223   1.2  augustss 			sc->sc_params = *(struct cir_params *)addr;
    224   1.1  augustss 		break;
    225   1.1  augustss 	default:
    226   1.1  augustss 		error = EINVAL;
    227   1.1  augustss 		break;
    228   1.1  augustss 	}
    229   1.1  augustss 	return (error);
    230   1.1  augustss }
    231   1.1  augustss 
    232   1.1  augustss int
    233  1.16      cube cirpoll(dev_t dev, int events, struct lwp *l)
    234   1.1  augustss {
    235   1.1  augustss 	struct cir_softc *sc;
    236   1.1  augustss 	int revents;
    237   1.1  augustss 	int s;
    238   1.1  augustss 
    239  1.21    cegger 	sc = device_lookup_private(&cir_cd, CIRUNIT(dev));
    240   1.1  augustss 	if (sc == NULL)
    241  1.11  augustss 		return (POLLERR);
    242  1.29       mrg 	if (!device_is_active(sc->sc_dev))
    243  1.11  augustss 		return (POLLERR);
    244   1.1  augustss 
    245   1.1  augustss 	revents = 0;
    246   1.1  augustss 	s = splir();
    247   1.1  augustss 	if (events & (POLLIN | POLLRDNORM))
    248   1.1  augustss 		if (sc->sc_rdframes > 0)
    249   1.1  augustss 			revents |= events & (POLLIN | POLLRDNORM);
    250   1.1  augustss 
    251   1.1  augustss #if 0
    252   1.1  augustss 	/* How about write? */
    253   1.1  augustss 	if (events & (POLLOUT | POLLWRNORM))
    254  1.15      cube 		if (/* ??? */)
    255   1.1  augustss 			revents |= events & (POLLOUT | POLLWRNORM);
    256   1.1  augustss #endif
    257   1.1  augustss 
    258   1.1  augustss 	if (revents == 0) {
    259   1.1  augustss 		if (events & (POLLIN | POLLRDNORM))
    260  1.16      cube 			selrecord(l, &sc->sc_rdsel);
    261   1.1  augustss 
    262   1.1  augustss #if 0
    263   1.1  augustss 		if (events & (POLLOUT | POLLWRNORM))
    264   1.1  augustss 			selrecord(p, &sc->sc_wrsel);
    265   1.1  augustss #endif
    266   1.1  augustss 	}
    267   1.1  augustss 
    268   1.1  augustss 	splx(s);
    269   1.1  augustss 	return (revents);
    270   1.1  augustss }
    271  1.27  jmcneill 
    272  1.27  jmcneill MODULE(MODULE_CLASS_DRIVER, cir, "ir");
    273  1.27  jmcneill 
    274  1.27  jmcneill #ifdef _MODULE
    275  1.27  jmcneill #include "ioconf.c"
    276  1.27  jmcneill #endif
    277  1.27  jmcneill 
    278  1.27  jmcneill static int
    279  1.27  jmcneill cir_modcmd(modcmd_t cmd, void *opaque)
    280  1.27  jmcneill {
    281  1.28  jmcneill 	int error = 0;
    282  1.28  jmcneill #ifdef _MODULE
    283  1.28  jmcneill 	int bmaj = -1, cmaj = -1;
    284  1.28  jmcneill #endif
    285  1.28  jmcneill 
    286  1.27  jmcneill 	switch (cmd) {
    287  1.27  jmcneill 	case MODULE_CMD_INIT:
    288  1.27  jmcneill #ifdef _MODULE
    289  1.28  jmcneill 		error = config_init_component(cfdriver_ioconf_cir,
    290  1.27  jmcneill 		    cfattach_ioconf_cir, cfdata_ioconf_cir);
    291  1.28  jmcneill 		if (error)
    292  1.28  jmcneill 			return error;
    293  1.28  jmcneill 		error = devsw_attach("cir", NULL, &bmaj, &cir_cdevsw, &cmaj);
    294  1.28  jmcneill 		if (error)
    295  1.28  jmcneill 			config_fini_component(cfdriver_ioconf_cir,
    296  1.28  jmcneill 			    cfattach_ioconf_cir, cfdata_ioconf_cir);
    297  1.27  jmcneill #endif
    298  1.28  jmcneill 		return error;
    299  1.27  jmcneill 	case MODULE_CMD_FINI:
    300  1.27  jmcneill #ifdef _MODULE
    301  1.28  jmcneill 		devsw_detach(NULL, &cir_cdevsw);
    302  1.27  jmcneill 		return config_fini_component(cfdriver_ioconf_cir,
    303  1.27  jmcneill 		    cfattach_ioconf_cir, cfdata_ioconf_cir);
    304  1.27  jmcneill #endif
    305  1.28  jmcneill 		return error;
    306  1.27  jmcneill 	default:
    307  1.27  jmcneill 		return ENOTTY;
    308  1.27  jmcneill 	}
    309  1.27  jmcneill }
    310