Home | History | Annotate | Line # | Download | only in ir
irframe.c revision 1.45
      1 /*	$NetBSD: irframe.c,v 1.45 2014/03/16 05:20:28 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: irframe.c,v 1.45 2014/03/16 05:20:28 dholland Exp $");
     34 
     35 #include "irframe.h"
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/ioctl.h>
     40 #include <sys/kernel.h>
     41 #include <sys/device.h>
     42 #include <sys/conf.h>
     43 #include <sys/malloc.h>
     44 #include <sys/poll.h>
     45 #include <sys/select.h>
     46 #include <sys/vnode.h>
     47 
     48 #include <dev/ir/ir.h>
     49 #include <dev/ir/irdaio.h>
     50 #include <dev/ir/irframevar.h>
     51 
     52 #ifdef IRFRAME_DEBUG
     53 #define DPRINTF(x)	if (irframedebug) printf x
     54 #define Static
     55 int irframedebug = 0;
     56 #else
     57 #define DPRINTF(x)
     58 #define Static static
     59 #endif
     60 
     61 dev_type_open(irframeopen);
     62 dev_type_close(irframeclose);
     63 dev_type_read(irframeread);
     64 dev_type_write(irframewrite);
     65 dev_type_ioctl(irframeioctl);
     66 dev_type_poll(irframepoll);
     67 dev_type_kqfilter(irframekqfilter);
     68 
     69 const struct cdevsw irframe_cdevsw = {
     70 	.d_open = irframeopen,
     71 	.d_close = irframeclose,
     72 	.d_read = irframeread,
     73 	.d_write = irframewrite,
     74 	.d_ioctl = irframeioctl,
     75 	.d_stop = nostop,
     76 	.d_tty = notty,
     77 	.d_poll = irframepoll,
     78 	.d_mmap = nommap,
     79 	.d_kqfilter = irframekqfilter,
     80 	.d_flag = D_OTHER
     81 };
     82 
     83 int irframe_match(device_t parent, cfdata_t match, void *aux);
     84 
     85 Static int irf_set_params(struct irframe_softc *sc, struct irda_params *p);
     86 Static int irf_reset_params(struct irframe_softc *sc);
     87 
     88 #if NIRFRAME == 0
     89 /* In case we just have tty attachment. */
     90 CFDRIVER_DECL(irframe, DV_DULL, NULL);
     91 #endif
     92 
     93 CFATTACH_DECL_NEW(irframe, sizeof(struct irframe_softc),
     94     irframe_match, irframe_attach, irframe_detach, NULL);
     95 
     96 extern struct cfdriver irframe_cd;
     97 
     98 #define IRFRAMEUNIT(dev) (minor(dev))
     99 
    100 int
    101 irframe_match(device_t parent, cfdata_t match, void *aux)
    102 {
    103 	struct ir_attach_args *ia = aux;
    104 
    105 	return (ia->ia_type == IR_TYPE_IRFRAME);
    106 }
    107 
    108 void
    109 irframe_attach(device_t parent, device_t self, void *aux)
    110 {
    111 	struct irframe_softc *sc = device_private(self);
    112 	struct ir_attach_args *ia = aux;
    113 	const char *delim;
    114 	int speeds = 0;
    115 
    116 	sc->sc_dev = self;
    117 	sc->sc_methods = ia->ia_methods;
    118 	sc->sc_handle = ia->ia_handle;
    119 
    120 #ifdef DIAGNOSTIC
    121 	if (sc->sc_methods->im_read == NULL ||
    122 	    sc->sc_methods->im_write == NULL ||
    123 	    sc->sc_methods->im_poll == NULL ||
    124 	    sc->sc_methods->im_kqfilter == NULL ||
    125 	    sc->sc_methods->im_set_params == NULL ||
    126 	    sc->sc_methods->im_get_speeds == NULL ||
    127 	    sc->sc_methods->im_get_turnarounds == NULL)
    128 		panic("%s: missing methods", device_xname(self));
    129 #endif
    130 
    131 	(void)sc->sc_methods->im_get_speeds(sc->sc_handle, &speeds);
    132 	sc->sc_speedmask = speeds;
    133 	delim = ":";
    134 	if (speeds & IRDA_SPEEDS_SIR) {
    135 		printf("%s SIR", delim);
    136 		delim = ",";
    137 	}
    138 	if (speeds & IRDA_SPEEDS_MIR) {
    139 		printf("%s MIR", delim);
    140 		delim = ",";
    141 	}
    142 	if (speeds & IRDA_SPEEDS_FIR) {
    143 		printf("%s FIR", delim);
    144 		delim = ",";
    145 	}
    146 	if (speeds & IRDA_SPEEDS_VFIR) {
    147 		printf("%s VFIR", delim);
    148 		delim = ",";
    149 	}
    150 	printf("\n");
    151 
    152 	if (!pmf_device_register(self, NULL, NULL))
    153 		aprint_error_dev(self, "couldn't establish power handler\n");
    154 }
    155 
    156 int
    157 irframe_detach(device_t self, int flags)
    158 {
    159 	/*struct irframe_softc *sc = device_private(self);*/
    160 	int maj, mn;
    161 
    162 	pmf_device_deregister(self);
    163 
    164 	/* XXX needs reference count */
    165 
    166 	/* locate the major number */
    167 	maj = cdevsw_lookup_major(&irframe_cdevsw);
    168 
    169 	/* Nuke the vnodes for any open instances (calls close). */
    170 	mn = device_unit(self);
    171 	vdevgone(maj, mn, mn, VCHR);
    172 
    173 	return (0);
    174 }
    175 
    176 int
    177 irframeopen(dev_t dev, int flag, int mode, struct lwp *l)
    178 {
    179 	struct irframe_softc *sc;
    180 	int error;
    181 
    182 	sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
    183 	if (sc == NULL)
    184 		return (ENXIO);
    185 	if (!device_is_active(sc->sc_dev))
    186 		return (EIO);
    187 	if (sc->sc_open)
    188 		return (EBUSY);
    189 	if (sc->sc_methods->im_open != NULL) {
    190 		error = sc->sc_methods->im_open(sc->sc_handle, flag, mode, l);
    191 		if (error)
    192 			return (error);
    193 	}
    194 	sc->sc_open = 1;
    195 #ifdef DIAGNOSTIC
    196 	sc->sc_speed = IRDA_DEFAULT_SPEED;
    197 #endif
    198 	(void)irf_reset_params(sc);
    199 	return (0);
    200 }
    201 
    202 int
    203 irframeclose(dev_t dev, int flag, int mode, struct lwp *l)
    204 {
    205 	struct irframe_softc *sc;
    206 	int error;
    207 
    208 	sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
    209 	if (sc == NULL)
    210 		return (ENXIO);
    211 	sc->sc_open = 0;
    212 	if (sc->sc_methods->im_close != NULL)
    213 		error = sc->sc_methods->im_close(sc->sc_handle, flag, mode, l);
    214 	else
    215 		error = 0;
    216 	return (error);
    217 }
    218 
    219 int
    220 irframeread(dev_t dev, struct uio *uio, int flag)
    221 {
    222 	struct irframe_softc *sc;
    223 
    224 	sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
    225 	if (sc == NULL)
    226 		return (ENXIO);
    227 	if (!device_is_active(sc->sc_dev) || !sc->sc_open)
    228 		return (EIO);
    229 	if (uio->uio_resid < sc->sc_params.maxsize) {
    230 #ifdef DIAGNOSTIC
    231 		printf("irframeread: short read %ld < %d\n",
    232 		       (long)uio->uio_resid, sc->sc_params.maxsize);
    233 #endif
    234 		return (EINVAL);
    235 	}
    236 	return (sc->sc_methods->im_read(sc->sc_handle, uio, flag));
    237 }
    238 
    239 int
    240 irframewrite(dev_t dev, struct uio *uio, int flag)
    241 {
    242 	struct irframe_softc *sc;
    243 
    244 	sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
    245 	if (sc == NULL)
    246 		return (ENXIO);
    247 	if (!device_is_active(sc->sc_dev) || !sc->sc_open)
    248 		return (EIO);
    249 	if (uio->uio_resid > sc->sc_params.maxsize) {
    250 #ifdef DIAGNOSTIC
    251 		printf("irframeread: long write %ld > %d\n",
    252 		       (long)uio->uio_resid, sc->sc_params.maxsize);
    253 #endif
    254 		return (EINVAL);
    255 	}
    256 	return (sc->sc_methods->im_write(sc->sc_handle, uio, flag));
    257 }
    258 
    259 int
    260 irf_set_params(struct irframe_softc *sc, struct irda_params *p)
    261 {
    262 	int error;
    263 
    264 	DPRINTF(("irf_set_params: set params speed=%u ebofs=%u maxsize=%u "
    265 		 "speedmask=0x%x\n", p->speed, p->ebofs, p->maxsize,
    266 		 sc->sc_speedmask));
    267 
    268 	if (p->maxsize > IRDA_MAX_FRAME_SIZE) {
    269 #ifdef IRFRAME_DEBUG
    270 		printf("irf_set_params: bad maxsize=%u\n", p->maxsize);
    271 #endif
    272 		return (EINVAL);
    273 	}
    274 
    275 	if (p->ebofs > IRDA_MAX_EBOFS) {
    276 #ifdef IRFRAME_DEBUG
    277 		printf("irf_set_params: bad maxsize=%u\n", p->maxsize);
    278 #endif
    279 		return (EINVAL);
    280 	}
    281 
    282 #define CONC(x,y) x##y
    283 #define CASE(s) case s: if (!(sc->sc_speedmask & CONC(IRDA_SPEED_,s))) return (EINVAL); break
    284 	switch (p->speed) {
    285 	CASE(2400);
    286 	CASE(9600);
    287 	CASE(19200);
    288 	CASE(38400);
    289 	CASE(57600);
    290 	CASE(115200);
    291 	CASE(576000);
    292 	CASE(1152000);
    293 	CASE(4000000);
    294 	CASE(16000000);
    295 	default: return (EINVAL);
    296 	}
    297 #undef CONC
    298 #undef CASE
    299 
    300 	error = sc->sc_methods->im_set_params(sc->sc_handle, p);
    301 	if (!error) {
    302 		sc->sc_params = *p;
    303 		DPRINTF(("irf_set_params: ok\n"));
    304 #ifdef DIAGNOSTIC
    305 		if (p->speed != sc->sc_speed) {
    306 			sc->sc_speed = p->speed;
    307 			aprint_verbose_dev(sc->sc_dev, "set speed %u\n",
    308 			       sc->sc_speed);
    309 		}
    310 #endif
    311 	} else {
    312 #ifdef IRFRAME_DEBUG
    313 		printf("irf_set_params: error=%d\n", error);
    314 #endif
    315 	}
    316 	return (error);
    317 }
    318 
    319 int
    320 irf_reset_params(struct irframe_softc *sc)
    321 {
    322 	struct irda_params params;
    323 
    324 	params.speed = IRDA_DEFAULT_SPEED;
    325 	params.ebofs = IRDA_DEFAULT_EBOFS;
    326 	params.maxsize = IRDA_DEFAULT_SIZE;
    327 	return (irf_set_params(sc, &params));
    328 }
    329 
    330 int
    331 irframeioctl(dev_t dev, u_long cmd, void *addr, int flag,
    332     struct lwp *l)
    333 {
    334 	struct irframe_softc *sc;
    335 	void *vaddr = addr;
    336 	int error;
    337 
    338 	sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
    339 	if (sc == NULL)
    340 		return (ENXIO);
    341 	if (!device_is_active(sc->sc_dev) || !sc->sc_open)
    342 		return (EIO);
    343 
    344 	switch (cmd) {
    345 	case FIONBIO:
    346 		/* All handled in the upper FS layer. */
    347 		error = 0;
    348 		break;
    349 
    350 	case IRDA_SET_PARAMS:
    351 		error = irf_set_params(sc, vaddr);
    352 		break;
    353 
    354 	case IRDA_RESET_PARAMS:
    355 		error = irf_reset_params(sc);
    356 		break;
    357 
    358 	case IRDA_GET_SPEEDMASK:
    359 		error = sc->sc_methods->im_get_speeds(sc->sc_handle, vaddr);
    360 		break;
    361 
    362 	case IRDA_GET_TURNAROUNDMASK:
    363 		error = sc->sc_methods->im_get_turnarounds(sc->sc_handle,vaddr);
    364 		break;
    365 
    366 	default:
    367 		error = EINVAL;
    368 		break;
    369 	}
    370 	return (error);
    371 }
    372 
    373 int
    374 irframepoll(dev_t dev, int events, struct lwp *l)
    375 {
    376 	struct irframe_softc *sc;
    377 
    378 	sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
    379 	if (sc == NULL)
    380 		return (POLLHUP);
    381 	if (!device_is_active(sc->sc_dev) || !sc->sc_open)
    382 		return (POLLHUP);
    383 
    384 	return (sc->sc_methods->im_poll(sc->sc_handle, events, l));
    385 }
    386 
    387 int
    388 irframekqfilter(dev_t dev, struct knote *kn)
    389 {
    390 	struct irframe_softc *sc;
    391 
    392 	sc = device_lookup_private(&irframe_cd, IRFRAMEUNIT(dev));
    393 	if (!device_is_active(sc->sc_dev) || !sc->sc_open)
    394 		return (1);
    395 
    396 	return (sc->sc_methods->im_kqfilter(sc->sc_handle, kn));
    397 }
    398