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