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