Home | History | Annotate | Line # | Download | only in ir
irframe.c revision 1.1
      1 /*	$NetBSD: irframe.c,v 1.1 2001/12/02 10:44:43 augustss 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/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/kernel.h>
     43 #include <sys/device.h>
     44 #include <sys/conf.h>
     45 #include <sys/malloc.h>
     46 #include <sys/poll.h>
     47 #include <sys/select.h>
     48 #include <sys/vnode.h>
     49 
     50 #include <dev/ir/ir.h>
     51 #include <dev/ir/irdaio.h>
     52 #include <dev/ir/irframevar.h>
     53 
     54 cdev_decl(irframe);
     55 
     56 int irframe_match(struct device *parent, struct cfdata *match, void *aux);
     57 void irframe_attach(struct device *parent, struct device *self, void *aux);
     58 int irframe_activate(struct device *self, enum devact act);
     59 int irframe_detach(struct device *self, int flags);
     60 
     61 struct cfattach irframe_ca = {
     62 	sizeof(struct irframe_softc), irframe_match, irframe_attach,
     63 	irframe_detach, irframe_activate
     64 };
     65 
     66 extern struct cfattach irframe_ca;
     67 extern struct cfdriver irframe_cd;
     68 
     69 #define IRFRAMEUNIT(dev) (minor(dev))
     70 
     71 int
     72 irframe_match(struct device *parent, struct cfdata *match, void *aux)
     73 {
     74 	struct ir_attach_args *ia = aux;
     75 
     76 	return (ia->ia_type == IR_TYPE_IRFRAME);
     77 }
     78 
     79 void
     80 irframe_attach(struct device *parent, struct device *self, void *aux)
     81 {
     82 	struct irframe_softc *sc = (struct irframe_softc *)self;
     83 	struct ir_attach_args *ia = aux;
     84 	const char *delim;
     85 	int speeds = 0;
     86 
     87 	sc->sc_methods = ia->ia_methods;
     88 	sc->sc_handle = ia->ia_handle;
     89 
     90 #ifdef DIAGNOSTIC
     91 	if (sc->sc_methods->im_read == NULL ||
     92 	    sc->sc_methods->im_write == NULL ||
     93 	    sc->sc_methods->im_set_params == NULL ||
     94 	    sc->sc_methods->im_reset_params == NULL ||
     95 	    sc->sc_methods->im_get_speeds == NULL ||
     96 	    sc->sc_methods->im_get_turnarounds == NULL)
     97 		panic("%s: missing methods\n", sc->sc_dev.dv_xname);
     98 #endif
     99 
    100 	(void)sc->sc_methods->im_get_speeds(sc->sc_handle, &speeds);
    101 	delim = ":";
    102 	if (speeds & IRDA_SPEEDS_SIR) {
    103 		printf("%s SIR", delim);
    104 		delim = ",";
    105 	}
    106 	if (speeds & IRDA_SPEEDS_MIR) {
    107 		printf("%s MIR", delim);
    108 		delim = ",";
    109 	}
    110 	if (speeds & IRDA_SPEEDS_FIR) {
    111 		printf("%s FIR", delim);
    112 		delim = ",";
    113 	}
    114 	if (speeds & IRDA_SPEEDS_VFIR) {
    115 		printf("%s VFIR", delim);
    116 		delim = ",";
    117 	}
    118 	printf("\n");
    119 }
    120 
    121 int
    122 irframe_activate(struct device *self, enum devact act)
    123 {
    124 	/*struct irframe_softc *sc = (struct irframe_softc *)self;*/
    125 
    126 	switch (act) {
    127 	case DVACT_ACTIVATE:
    128 		return (EOPNOTSUPP);
    129 		break;
    130 
    131 	case DVACT_DEACTIVATE:
    132 		break;
    133 	}
    134 	return (0);
    135 }
    136 
    137 int
    138 irframe_detach(struct device *self, int flags)
    139 {
    140 	/*struct irframe_softc *sc = (struct irframe_softc *)self;*/
    141 	int maj, mn;
    142 
    143 	/* locate the major number */
    144 	for (maj = 0; maj < nchrdev; maj++)
    145 		if (cdevsw[maj].d_open == irframeopen)
    146 			break;
    147 
    148 	/* Nuke the vnodes for any open instances (calls close). */
    149 	mn = self->dv_unit;
    150 	vdevgone(maj, mn, mn, VCHR);
    151 
    152 	return (0);
    153 }
    154 
    155 int
    156 irframeopen(dev_t dev, int flag, int mode, struct proc *p)
    157 {
    158 	struct irframe_softc *sc;
    159 	int error;
    160 
    161 	sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
    162 	if (sc == NULL)
    163 		return (ENXIO);
    164 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    165 		return (EIO);
    166 	if (sc->sc_open)
    167 		return (EBUSY);
    168 	if (sc->sc_methods->im_open != NULL) {
    169 		error = sc->sc_methods->im_open(sc->sc_handle, flag, mode, p);
    170 		if (error)
    171 			return (error);
    172 	}
    173 	sc->sc_open = 1;
    174 	return (0);
    175 }
    176 
    177 int
    178 irframeclose(dev_t dev, int flag, int mode, struct proc *p)
    179 {
    180 	struct irframe_softc *sc;
    181 	int error;
    182 
    183 	sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
    184 	if (sc == NULL)
    185 		return (ENXIO);
    186 	if (sc->sc_methods->im_close != NULL)
    187 		error = sc->sc_methods->im_close(sc->sc_handle, flag, mode, p);
    188 	else
    189 		error = 0;
    190 	sc->sc_open = 0;
    191 	return (error);
    192 }
    193 
    194 int
    195 irframeread(dev_t dev, struct uio *uio, int flag)
    196 {
    197 	struct irframe_softc *sc;
    198 
    199 	sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
    200 	if (sc == NULL)
    201 		return (ENXIO);
    202 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    203 		return (EIO);
    204 	return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
    205 }
    206 
    207 int
    208 irframewrite(dev_t dev, struct uio *uio, int flag)
    209 {
    210 	struct irframe_softc *sc;
    211 	u_int8_t buf[MAX_IRDA_FRAME];
    212 	size_t n;
    213 	int error;
    214 
    215 	sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
    216 	if (sc == NULL)
    217 		return (ENXIO);
    218 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    219 		return (EIO);
    220 	n = uio->uio_resid;
    221 	if (n > MAX_IRDA_FRAME)
    222 		return (EINVAL);
    223 	error = uiomove(buf, n, uio);
    224 	if (error)
    225 		return (error);
    226 	return (sc->sc_methods->im_write(sc->sc_handle, buf, n));
    227 }
    228 
    229 int
    230 irframeioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
    231 {
    232 	struct irframe_softc *sc;
    233 	int error;
    234 
    235 	sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
    236 	if (sc == NULL)
    237 		return (ENXIO);
    238 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    239 		return (EIO);
    240 
    241 	switch (cmd) {
    242 	case FIONBIO:
    243 		/* All handled in the upper FS layer. */
    244 		error = 0;
    245 		break;
    246 
    247 	case IRDA_SET_PARAMS:
    248 		error = sc->sc_methods->im_set_params(sc->sc_handle,
    249 		    (void *)addr);
    250 		break;
    251 
    252 	case IRDA_RESET_PARAMS:
    253 		error = sc->sc_methods->im_reset_params(sc->sc_handle);
    254 		break;
    255 
    256 	case IRDA_GET_SPEEDMASK:
    257 		error = sc->sc_methods->im_get_speeds(sc->sc_handle,
    258 		    (int *)addr);
    259 		break;
    260 
    261 	case IRDA_GET_TURNAROUNDMASK:
    262 		error = sc->sc_methods->im_get_turnarounds(sc->sc_handle,
    263 		    (int *)addr);
    264 		break;
    265 
    266 	default:
    267 		error = EINVAL;
    268 		break;
    269 	}
    270 	return (error);
    271 }
    272 
    273 int
    274 irframepoll(dev_t dev, int events, struct proc *p)
    275 {
    276 	struct irframe_softc *sc;
    277 	int revents;
    278 	int s;
    279 
    280 	sc = device_lookup(&irframe_cd, IRFRAMEUNIT(dev));
    281 	if (sc == NULL)
    282 		return (ENXIO);
    283 	if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    284 		return (EIO);
    285 
    286 	revents = 0;
    287 	s = splir();
    288 	if (events & (POLLIN | POLLRDNORM))
    289 		if (sc->sc_rdframes > 0)
    290 			revents |= events & (POLLIN | POLLRDNORM);
    291 
    292 #if 0
    293 	/* How about write? */
    294 	if (events & (POLLOUT | POLLWRNORM))
    295 		if (???)
    296 			revents |= events & (POLLOUT | POLLWRNORM);
    297 #endif
    298 
    299 	if (revents == 0) {
    300 		if (events & (POLLIN | POLLRDNORM))
    301 			selrecord(p, &sc->sc_rdsel);
    302 
    303 #if 0
    304 		if (events & (POLLOUT | POLLWRNORM))
    305 			selrecord(p, &sc->sc_wrsel);
    306 #endif
    307 	}
    308 
    309 	splx(s);
    310 	return (revents);
    311 }
    312 
    313 void
    314 irframe_frame_available(struct device *dev)
    315 {
    316 	struct irframe_softc *sc = (struct irframe_softc *)dev;
    317 	int s;
    318 
    319 	s = splir();
    320 	sc->sc_rdframes++;
    321 	selwakeup(&sc->sc_rdsel);
    322 	splx(s);
    323 }
    324 
    325 /*********/
    326 
    327 
    328 struct device *
    329 irframe_alloc(size_t size, struct irframe_methods *m, void *h)
    330 {
    331 	struct cfdriver *cd = &irframe_cd;
    332 	struct device *dev;
    333 	int unit;
    334 
    335 	for (unit = 0; unit < cd->cd_ndevs; unit++)
    336 		if (cd->cd_devs[unit] == NULL)
    337 			break;
    338 	dev = malloc(size, M_DEVBUF, M_WAITOK);
    339 	memset(dev, 0, size);
    340 	snprintf(dev->dv_xname, sizeof dev->dv_xname, "irframe%d", unit);
    341 	dev->dv_unit = unit;
    342 	dev->dv_flags = DVF_ACTIVE;	/* always initially active */
    343 
    344 	config_makeroom(unit, cd);
    345 	cd->cd_devs[unit] = dev;
    346 
    347 	return (dev);
    348 }
    349 
    350 void
    351 irframe_dealloc(struct device *dev)
    352 {
    353 	struct cfdriver *cd = &irframe_cd;
    354 	int unit;
    355 
    356 	for (unit = 0; unit < cd->cd_ndevs; unit++) {
    357 		if (cd->cd_devs[unit] == dev) {
    358 			cd->cd_devs[unit] = NULL;
    359 			free(dev, M_DEVBUF);
    360 			return;
    361 		}
    362 	}
    363 	panic("irframe_dealloc: device not found\n");
    364 }
    365