Home | History | Annotate | Line # | Download | only in ir
irframe_tty.c revision 1.43
      1 /*	$NetBSD: irframe_tty.c,v 1.43 2007/03/08 19:35:44 drochner Exp $	*/
      2 
      3 /*
      4  * TODO
      5  *  Test dongle code.
      6  */
      7 
      8 /*
      9  * Copyright (c) 2001 The NetBSD Foundation, Inc.
     10  * All rights reserved.
     11  *
     12  * This code is derived from software contributed to The NetBSD Foundation
     13  * by Lennart Augustsson (lennart (at) augustsson.net) and Tommy Bohlin
     14  * (tommy (at) gatespace.com).
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. All advertising materials mentioning features or use of this software
     25  *    must display the following acknowledgement:
     26  *        This product includes software developed by the NetBSD
     27  *        Foundation, Inc. and its contributors.
     28  * 4. Neither the name of The NetBSD Foundation nor the names of its
     29  *    contributors may be used to endorse or promote products derived
     30  *    from this software without specific prior written permission.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     33  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     34  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     35  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     36  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     37  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     38  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     39  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     40  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     41  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     42  * POSSIBILITY OF SUCH DAMAGE.
     43  */
     44 
     45 /*
     46  * Loosely based on ppp_tty.c.
     47  * Framing and dongle handling written by Tommy Bohlin.
     48  */
     49 
     50 #include <sys/cdefs.h>
     51 __KERNEL_RCSID(0, "$NetBSD: irframe_tty.c,v 1.43 2007/03/08 19:35:44 drochner Exp $");
     52 
     53 #include <sys/param.h>
     54 #include <sys/proc.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/tty.h>
     57 #include <sys/kernel.h>
     58 #include <sys/lock.h>
     59 #include <sys/malloc.h>
     60 #include <sys/conf.h>
     61 #include <sys/systm.h>
     62 #include <sys/device.h>
     63 #include <sys/file.h>
     64 #include <sys/vnode.h>
     65 #include <sys/poll.h>
     66 #include <sys/kauth.h>
     67 
     68 #include <dev/ir/ir.h>
     69 #include <dev/ir/sir.h>
     70 #include <dev/ir/irdaio.h>
     71 #include <dev/ir/irframevar.h>
     72 
     73 #ifdef IRFRAMET_DEBUG
     74 #define DPRINTF(x)	if (irframetdebug) printf x
     75 int irframetdebug = 0;
     76 #else
     77 #define DPRINTF(x)
     78 #endif
     79 
     80 /*****/
     81 
     82 /* Max size with framing. */
     83 #define MAX_IRDA_FRAME (2*IRDA_MAX_FRAME_SIZE + IRDA_MAX_EBOFS + 4)
     84 
     85 struct irt_frame {
     86 	u_char *buf;
     87 	u_int len;
     88 };
     89 #define MAXFRAMES 8
     90 
     91 struct irframet_softc {
     92 	struct irframe_softc sc_irp;
     93 	struct tty *sc_tp;
     94 
     95 	int sc_dongle;
     96 	int sc_dongle_private;
     97 
     98 	int sc_state;
     99 #define	IRT_RSLP		0x01	/* waiting for data (read) */
    100 #if 0
    101 #define	IRT_WSLP		0x02	/* waiting for data (write) */
    102 #define IRT_CLOSING		0x04	/* waiting for output to drain */
    103 #endif
    104 	struct lock sc_wr_lk;
    105 
    106 	struct irda_params sc_params;
    107 
    108 	u_char* sc_inbuf;
    109 	int sc_framestate;
    110 #define FRAME_OUTSIDE    0
    111 #define FRAME_INSIDE     1
    112 #define FRAME_ESCAPE     2
    113 	int sc_inchars;
    114 	int sc_inFCS;
    115 	struct callout sc_timeout;
    116 
    117 	u_int sc_nframes;
    118 	u_int sc_framei;
    119 	u_int sc_frameo;
    120 	struct irt_frame sc_frames[MAXFRAMES];
    121 	u_int8_t sc_buffer[MAX_IRDA_FRAME];
    122 	struct selinfo sc_rsel;
    123 	/* XXXJRT Nothing selnotify's sc_wsel */
    124 	struct selinfo sc_wsel;
    125 };
    126 
    127 /* line discipline methods */
    128 int	irframetopen(dev_t, struct tty *);
    129 int	irframetclose(struct tty *, int);
    130 int	irframetioctl(struct tty *, u_long, void *, int, struct lwp *);
    131 int	irframetinput(int, struct tty *);
    132 int	irframetstart(struct tty *);
    133 
    134 /* pseudo device init */
    135 void	irframettyattach(int);
    136 
    137 /* irframe methods */
    138 static int	irframet_open(void *, int, int, struct lwp *);
    139 static int	irframet_close(void *, int, int, struct lwp *);
    140 static int	irframet_read(void *, struct uio *, int);
    141 static int	irframet_write(void *, struct uio *, int);
    142 static int	irframet_poll(void *, int, struct lwp *);
    143 static int	irframet_kqfilter(void *, struct knote *);
    144 
    145 static int	irframet_set_params(void *, struct irda_params *);
    146 static int	irframet_get_speeds(void *, int *);
    147 static int	irframet_get_turnarounds(void *, int *);
    148 
    149 /* internal */
    150 static int	irt_write_frame(struct tty *, u_int8_t *, size_t);
    151 static int	irt_putc(struct tty *, int);
    152 static void	irt_frame(struct irframet_softc *, u_char *, u_int);
    153 static void	irt_timeout(void *);
    154 static void	irt_ioctl(struct tty *, u_long, void *);
    155 static void	irt_setspeed(struct tty *, u_int);
    156 static void	irt_setline(struct tty *, u_int);
    157 static void	irt_delay(struct tty *, u_int);
    158 
    159 static const struct irframe_methods irframet_methods = {
    160 	irframet_open, irframet_close, irframet_read, irframet_write,
    161 	irframet_poll, irframet_kqfilter, irframet_set_params,
    162 	irframet_get_speeds, irframet_get_turnarounds
    163 };
    164 
    165 static void irts_none(struct tty *, u_int);
    166 static void irts_tekram(struct tty *, u_int);
    167 static void irts_jeteye(struct tty *, u_int);
    168 static void irts_actisys(struct tty *, u_int);
    169 static void irts_litelink(struct tty *, u_int);
    170 static void irts_girbil(struct tty *, u_int);
    171 
    172 #define NORMAL_SPEEDS (IRDA_SPEEDS_SIR & ~IRDA_SPEED_2400)
    173 #define TURNT_POS (IRDA_TURNT_10000 | IRDA_TURNT_5000 | IRDA_TURNT_1000 | \
    174 	IRDA_TURNT_500 | IRDA_TURNT_100 | IRDA_TURNT_50 | IRDA_TURNT_10)
    175 static const struct dongle {
    176 	void (*setspeed)(struct tty *, u_int);
    177 	u_int speedmask;
    178 	u_int turnmask;
    179 } irt_dongles[DONGLE_MAX] = {
    180 	/* Indexed by dongle number from irdaio.h */
    181 	{ irts_none, IRDA_SPEEDS_SIR, IRDA_TURNT_10000 },
    182 	{ irts_tekram, IRDA_SPEEDS_SIR, IRDA_TURNT_10000 },
    183 	{ irts_jeteye, IRDA_SPEED_9600|IRDA_SPEED_19200|IRDA_SPEED_115200,
    184 	  				IRDA_TURNT_10000 },
    185 	{ irts_actisys, NORMAL_SPEEDS & ~IRDA_SPEED_38400, TURNT_POS },
    186 	{ irts_actisys, NORMAL_SPEEDS, TURNT_POS },
    187 	{ irts_litelink, NORMAL_SPEEDS, TURNT_POS },
    188 	{ irts_girbil, IRDA_SPEEDS_SIR, IRDA_TURNT_10000 | IRDA_TURNT_5000 },
    189 };
    190 
    191 static struct linesw irframet_disc = {
    192 	.l_name = "irframe",
    193 	.l_open = irframetopen,
    194 	.l_close = irframetclose,
    195 	.l_read = ttyerrio,
    196 	.l_write = ttyerrio,
    197 	.l_ioctl = irframetioctl,
    198 	.l_rint = irframetinput,
    199 	.l_start = irframetstart,
    200 	.l_modem = ttymodem,
    201 	.l_poll = ttyerrpoll
    202 };
    203 
    204 /* glue to attach irframe device */
    205 static void irframet_attach(struct device *, struct device *, void *);
    206 static int irframet_detach(struct device *, int);
    207 
    208 CFATTACH_DECL(irframet, sizeof(struct irframet_softc),
    209 	NULL, irframet_attach, irframet_detach, NULL);
    210 
    211 void
    212 irframettyattach(int n)
    213 {
    214 	extern struct cfdriver irframe_cd;
    215 
    216 	(void) ttyldisc_attach(&irframet_disc);
    217 
    218 	/* XXX might fail if "real" attachments have pulled this in */
    219 	/* XXX should not be done here */
    220 	config_cfdriver_attach(&irframe_cd);
    221 
    222 	config_cfattach_attach("irframe", &irframet_ca);
    223 }
    224 
    225 static void
    226 irframet_attach(struct device *parent, struct device *self, void *aux)
    227 {
    228 
    229 	/* pseudo-device attachment does not print name */
    230 	printf("%s", self->dv_xname);
    231 #if 0 /* XXX can't do it yet because pseudo-devices don't get aux */
    232 	struct ir_attach_args ia;
    233 
    234 	ia.ia_methods = &irframet_methods;
    235 	ia.ia_handle = aux->xxx;
    236 
    237 	irframe_attach(parent, self, &ia);
    238 #endif
    239 }
    240 
    241 static int
    242 irframet_detach(struct device *dev, int flags)
    243 {
    244 
    245 	return (irframe_detach(dev, flags));
    246 }
    247 
    248 /*
    249  * Line specific open routine for async tty devices.
    250  * Attach the given tty to the first available irframe unit.
    251  * Called from device open routine or ttioctl.
    252  */
    253 /* ARGSUSED */
    254 int
    255 irframetopen(dev_t dev, struct tty *tp)
    256 {
    257 	struct lwp *l = curlwp;		/* XXX */
    258 	struct irframet_softc *sc;
    259 	int error, s;
    260 	struct cfdata *cfdata;
    261 	struct ir_attach_args ia;
    262 
    263 	DPRINTF(("%s\n", __FUNCTION__));
    264 
    265 	if ((error = kauth_authorize_device_tty(l->l_cred,
    266 		KAUTH_DEVICE_TTY_OPEN, tp)))
    267 		return (error);
    268 
    269 	s = spltty();
    270 
    271 	DPRINTF(("%s: linesw=%p disc=%s\n", __FUNCTION__, tp->t_linesw,
    272 		 tp->t_linesw->l_name));
    273 	if (tp->t_linesw == &irframet_disc) {
    274 		sc = (struct irframet_softc *)tp->t_sc;
    275 		DPRINTF(("%s: sc=%p sc_tp=%p\n", __FUNCTION__, sc, sc->sc_tp));
    276 		if (sc != NULL) {
    277 			splx(s);
    278 			return (EBUSY);
    279 		}
    280 	}
    281 
    282 	cfdata = malloc(sizeof(struct cfdata), M_DEVBUF, M_WAITOK);
    283 	cfdata->cf_name = "irframe";
    284 	cfdata->cf_atname = "irframet";
    285 	cfdata->cf_fstate = FSTATE_STAR;
    286 	cfdata->cf_unit = 0;
    287 	sc = (struct irframet_softc *)config_attach_pseudo(cfdata);
    288 
    289 	/* XXX should be done in irframet_attach() */
    290 	ia.ia_methods = &irframet_methods;
    291 	ia.ia_handle = tp;
    292 	irframe_attach(0, (struct device *)sc, &ia);
    293 
    294 	tp->t_sc = sc;
    295 	sc->sc_tp = tp;
    296 	printf("%s attached at tty%02d\n", sc->sc_irp.sc_dev.dv_xname,
    297 	    minor(tp->t_dev));
    298 
    299 	DPRINTF(("%s: set sc=%p\n", __FUNCTION__, sc));
    300 
    301 	ttyflush(tp, FREAD | FWRITE);
    302 
    303 	sc->sc_dongle = DONGLE_NONE;
    304 	sc->sc_dongle_private = 0;
    305 
    306 	splx(s);
    307 
    308 	return (0);
    309 }
    310 
    311 /*
    312  * Line specific close routine, called from device close routine
    313  * and from ttioctl.
    314  * Detach the tty from the irframe unit.
    315  * Mimics part of ttyclose().
    316  */
    317 int
    318 irframetclose(struct tty *tp, int flag)
    319 {
    320 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    321 	int s;
    322 	struct cfdata *cfdata;
    323 
    324 	DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
    325 
    326 	s = spltty();
    327 	ttyflush(tp, FREAD | FWRITE);
    328 	ttyldisc_release(tp->t_linesw);
    329 	tp->t_linesw = ttyldisc_default();
    330 	if (sc != NULL) {
    331 		tp->t_sc = NULL;
    332 		printf("%s detached from tty%02d\n", sc->sc_irp.sc_dev.dv_xname,
    333 		    minor(tp->t_dev));
    334 
    335 		if (sc->sc_tp == tp) {
    336 			cfdata = sc->sc_irp.sc_dev.dv_cfdata;
    337 			config_detach(&sc->sc_irp.sc_dev, 0);
    338 			free(cfdata, M_DEVBUF);
    339 		}
    340 	}
    341 	splx(s);
    342 	return (0);
    343 }
    344 
    345 /*
    346  * Line specific (tty) ioctl routine.
    347  * This discipline requires that tty device drivers call
    348  * the line specific l_ioctl routine from their ioctl routines.
    349  */
    350 /* ARGSUSED */
    351 int
    352 irframetioctl(struct tty *tp, u_long cmd, void *data, int flag,
    353     struct lwp *l)
    354 {
    355 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    356 	int error;
    357 	int d;
    358 
    359 	DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
    360 
    361 	if (sc == NULL || tp != sc->sc_tp)
    362 		return (EPASSTHROUGH);
    363 
    364 	error = 0;
    365 	switch (cmd) {
    366 	case IRFRAMETTY_GET_DEVICE:
    367 		*(int *)data = device_unit(&sc->sc_irp.sc_dev);
    368 		break;
    369 	case IRFRAMETTY_GET_DONGLE:
    370 		*(int *)data = sc->sc_dongle;
    371 		break;
    372 	case IRFRAMETTY_SET_DONGLE:
    373 		d = *(int *)data;
    374 		if (d < 0 || d >= DONGLE_MAX)
    375 			return (EINVAL);
    376 		sc->sc_dongle = d;
    377 		break;
    378 	default:
    379 		error = EPASSTHROUGH;
    380 		break;
    381 	}
    382 
    383 	return (error);
    384 }
    385 
    386 /*
    387  * Start output on async tty interface.
    388  */
    389 int
    390 irframetstart(struct tty *tp)
    391 {
    392 	/*struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;*/
    393 	int s;
    394 
    395 	DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
    396 
    397 	s = spltty();
    398 	if (tp->t_oproc != NULL)
    399 		(*tp->t_oproc)(tp);
    400 	splx(s);
    401 
    402 	return (0);
    403 }
    404 
    405 void
    406 irt_frame(struct irframet_softc *sc, u_char *tbuf, u_int len)
    407 {
    408 	DPRINTF(("%s: nframe=%d framei=%d frameo=%d\n",
    409 		 __FUNCTION__, sc->sc_nframes, sc->sc_framei, sc->sc_frameo));
    410 
    411 	if (sc->sc_inbuf == NULL) /* XXX happens if device is closed? */
    412 		return;
    413 	if (sc->sc_nframes >= MAXFRAMES) {
    414 #ifdef IRFRAMET_DEBUG
    415 		printf("%s: dropped frame\n", __FUNCTION__);
    416 #endif
    417 		return;
    418 	}
    419 	if (sc->sc_frames[sc->sc_framei].buf == NULL)
    420 		return;
    421 	memcpy(sc->sc_frames[sc->sc_framei].buf, tbuf, len);
    422 	sc->sc_frames[sc->sc_framei].len = len;
    423 	sc->sc_framei = (sc->sc_framei+1) % MAXFRAMES;
    424 	sc->sc_nframes++;
    425 	if (sc->sc_state & IRT_RSLP) {
    426 		sc->sc_state &= ~IRT_RSLP;
    427 		DPRINTF(("%s: waking up reader\n", __FUNCTION__));
    428 		wakeup(sc->sc_frames);
    429 	}
    430 	selnotify(&sc->sc_rsel, 0);
    431 }
    432 
    433 void
    434 irt_timeout(void *v)
    435 {
    436 	struct irframet_softc *sc = v;
    437 
    438 #ifdef IRFRAMET_DEBUG
    439 	if (sc->sc_framestate != FRAME_OUTSIDE)
    440 		printf("%s: input frame timeout\n", __FUNCTION__);
    441 #endif
    442 	sc->sc_framestate = FRAME_OUTSIDE;
    443 }
    444 
    445 int
    446 irframetinput(int c, struct tty *tp)
    447 {
    448 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    449 
    450 	c &= 0xff;
    451 
    452 #if IRFRAMET_DEBUG
    453 	if (irframetdebug > 1)
    454 		DPRINTF(("%s: tp=%p c=0x%02x\n", __FUNCTION__, tp, c));
    455 #endif
    456 
    457 	if (sc == NULL || tp != (struct tty *)sc->sc_tp)
    458 		return (0);
    459 
    460 	if (sc->sc_inbuf == NULL)
    461 		return (0);
    462 
    463 	switch (c) {
    464 	case SIR_BOF:
    465 		DPRINTF(("%s: BOF\n", __FUNCTION__));
    466 		sc->sc_framestate = FRAME_INSIDE;
    467 		sc->sc_inchars = 0;
    468 		sc->sc_inFCS = INITFCS;
    469 		break;
    470 	case SIR_EOF:
    471 		DPRINTF(("%s: EOF state=%d inchars=%d fcs=0x%04x\n",
    472 			 __FUNCTION__,
    473 			 sc->sc_framestate, sc->sc_inchars, sc->sc_inFCS));
    474 		if (sc->sc_framestate == FRAME_INSIDE &&
    475 		    sc->sc_inchars >= 4 && sc->sc_inFCS == GOODFCS) {
    476 			irt_frame(sc, sc->sc_inbuf, sc->sc_inchars - 2);
    477 		} else if (sc->sc_framestate != FRAME_OUTSIDE) {
    478 #ifdef IRFRAMET_DEBUG
    479 			printf("%s: malformed input frame\n", __FUNCTION__);
    480 #endif
    481 		}
    482 		sc->sc_framestate = FRAME_OUTSIDE;
    483 		break;
    484 	case SIR_CE:
    485 		DPRINTF(("%s: CE\n", __FUNCTION__));
    486 		if (sc->sc_framestate == FRAME_INSIDE)
    487 			sc->sc_framestate = FRAME_ESCAPE;
    488 		break;
    489 	default:
    490 #if IRFRAMET_DEBUG
    491 	if (irframetdebug > 1)
    492 		DPRINTF(("%s: c=0x%02x, inchar=%d state=%d\n", __FUNCTION__, c,
    493 			 sc->sc_inchars, sc->sc_state));
    494 #endif
    495 		if (sc->sc_framestate != FRAME_OUTSIDE) {
    496 			if (sc->sc_framestate == FRAME_ESCAPE) {
    497 				sc->sc_framestate = FRAME_INSIDE;
    498 				c ^= SIR_ESC_BIT;
    499 			}
    500 			if (sc->sc_inchars < sc->sc_params.maxsize + 2) {
    501 				sc->sc_inbuf[sc->sc_inchars++] = c;
    502 				sc->sc_inFCS = updateFCS(sc->sc_inFCS, c);
    503 			} else {
    504 				sc->sc_framestate = FRAME_OUTSIDE;
    505 #ifdef IRFRAMET_DEBUG
    506 				printf("%s: input frame overrun\n",
    507 				       __FUNCTION__);
    508 #endif
    509 			}
    510 		}
    511 		break;
    512 	}
    513 
    514 #if 1
    515 	if (sc->sc_framestate != FRAME_OUTSIDE) {
    516 		callout_reset(&sc->sc_timeout, hz/20, irt_timeout, sc);
    517 	}
    518 #endif
    519 
    520 	return (0);
    521 }
    522 
    523 
    524 /*** irframe methods ***/
    525 
    526 int
    527 irframet_open(void *h, int flag, int mode,
    528     struct lwp *l)
    529 {
    530 	struct tty *tp = h;
    531 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    532 
    533 	DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
    534 
    535 	sc->sc_params.speed = 0;
    536 	sc->sc_params.ebofs = IRDA_DEFAULT_EBOFS;
    537 	sc->sc_params.maxsize = 0;
    538 	sc->sc_framestate = FRAME_OUTSIDE;
    539 	sc->sc_nframes = 0;
    540 	sc->sc_framei = 0;
    541 	sc->sc_frameo = 0;
    542 	callout_init(&sc->sc_timeout);
    543 	lockinit(&sc->sc_wr_lk, PZERO, "irfrtl", 0, 0);
    544 
    545 	return (0);
    546 }
    547 
    548 int
    549 irframet_close(void *h, int flag, int mode,
    550     struct lwp *l)
    551 {
    552 	struct tty *tp = h;
    553 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    554 	int i, s;
    555 
    556 	DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
    557 
    558 	callout_stop(&sc->sc_timeout);
    559 	s = splir();
    560 	if (sc->sc_inbuf != NULL) {
    561 		free(sc->sc_inbuf, M_DEVBUF);
    562 		sc->sc_inbuf = NULL;
    563 	}
    564 	for (i = 0; i < MAXFRAMES; i++) {
    565 		if (sc->sc_frames[i].buf != NULL) {
    566 			free(sc->sc_frames[i].buf, M_DEVBUF);
    567 			sc->sc_frames[i].buf = NULL;
    568 		}
    569 	}
    570 	splx(s);
    571 
    572 	return (0);
    573 }
    574 
    575 int
    576 irframet_read(void *h, struct uio *uio, int flag)
    577 {
    578 	struct tty *tp = h;
    579 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    580 	int error = 0;
    581 	int s;
    582 
    583 	DPRINTF(("%s: resid=%zd, iovcnt=%d, offset=%ld\n",
    584 		 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
    585 		 (long)uio->uio_offset));
    586 	DPRINTF(("%s: nframe=%d framei=%d frameo=%d\n",
    587 		 __FUNCTION__, sc->sc_nframes, sc->sc_framei, sc->sc_frameo));
    588 
    589 
    590 	s = splir();
    591 	while (sc->sc_nframes == 0) {
    592 		if (flag & IO_NDELAY) {
    593 			splx(s);
    594 			return (EWOULDBLOCK);
    595 		}
    596 		sc->sc_state |= IRT_RSLP;
    597 		DPRINTF(("%s: sleep\n", __FUNCTION__));
    598 		error = tsleep(sc->sc_frames, PZERO | PCATCH, "irtrd", 0);
    599 		DPRINTF(("%s: woke, error=%d\n", __FUNCTION__, error));
    600 		if (error) {
    601 			sc->sc_state &= ~IRT_RSLP;
    602 			break;
    603 		}
    604 	}
    605 
    606 	/* Do just one frame transfer per read */
    607 	if (!error) {
    608 		if (uio->uio_resid < sc->sc_frames[sc->sc_frameo].len) {
    609 			DPRINTF(("%s: uio buffer smaller than frame size "
    610 				 "(%zd < %d)\n", __FUNCTION__, uio->uio_resid,
    611 				 sc->sc_frames[sc->sc_frameo].len));
    612 			error = EINVAL;
    613 		} else {
    614 			DPRINTF(("%s: moving %d bytes\n", __FUNCTION__,
    615 				 sc->sc_frames[sc->sc_frameo].len));
    616 			error = uiomove(sc->sc_frames[sc->sc_frameo].buf,
    617 					sc->sc_frames[sc->sc_frameo].len, uio);
    618 			DPRINTF(("%s: error=%d\n", __FUNCTION__, error));
    619 		}
    620 		sc->sc_frameo = (sc->sc_frameo+1) % MAXFRAMES;
    621 		sc->sc_nframes--;
    622 	}
    623 	splx(s);
    624 
    625 	return (error);
    626 }
    627 
    628 int
    629 irt_putc(struct tty *tp, int c)
    630 {
    631 	int s;
    632 	int error;
    633 
    634 #if IRFRAMET_DEBUG
    635 	if (irframetdebug > 3)
    636 		DPRINTF(("%s: tp=%p c=0x%02x cc=%d\n", __FUNCTION__, tp, c,
    637 			 tp->t_outq.c_cc));
    638 #endif
    639 	if (tp->t_outq.c_cc > tp->t_hiwat) {
    640 		irframetstart(tp);
    641 		s = spltty();
    642 		/*
    643 		 * This can only occur if FLUSHO is set in t_lflag,
    644 		 * or if ttstart/oproc is synchronous (or very fast).
    645 		 */
    646 		if (tp->t_outq.c_cc <= tp->t_hiwat) {
    647 			splx(s);
    648 			goto go;
    649 		}
    650 		SET(tp->t_state, TS_ASLEEP);
    651 		error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
    652 		splx(s);
    653 		if (error)
    654 			return (error);
    655 	}
    656  go:
    657 	if (putc(c, &tp->t_outq) < 0) {
    658 		printf("irframe: putc failed\n");
    659 		return (EIO);
    660 	}
    661 	return (0);
    662 }
    663 
    664 int
    665 irframet_write(void *h, struct uio *uio, int flag)
    666 {
    667 	struct tty *tp = h;
    668 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    669 	int n;
    670 
    671 	DPRINTF(("%s: resid=%zd, iovcnt=%d, offset=%ld\n",
    672 		 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
    673 		 (long)uio->uio_offset));
    674 
    675 	n = irda_sir_frame(sc->sc_buffer, sizeof(sc->sc_buffer), uio,
    676 	    sc->sc_params.ebofs);
    677 	if (n < 0) {
    678 #ifdef IRFRAMET_DEBUG
    679 		printf("%s: irda_sir_frame() error=%d\n", __FUNCTION__, -n);
    680 #endif
    681 		return (-n);
    682 	}
    683 	return (irt_write_frame(tp, sc->sc_buffer, n));
    684 }
    685 
    686 int
    687 irt_write_frame(struct tty *tp, u_int8_t *tbuf, size_t len)
    688 {
    689 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    690 	int error, i;
    691 
    692 	DPRINTF(("%s: tp=%p len=%zd\n", __FUNCTION__, tp, len));
    693 
    694 	lockmgr(&sc->sc_wr_lk, LK_EXCLUSIVE, NULL);
    695 	error = 0;
    696 	for (i = 0; !error && i < len; i++)
    697 		error = irt_putc(tp, tbuf[i]);
    698 	lockmgr(&sc->sc_wr_lk, LK_RELEASE, NULL);
    699 
    700 	irframetstart(tp);
    701 
    702 	DPRINTF(("%s: done, error=%d\n", __FUNCTION__, error));
    703 
    704 	return (error);
    705 }
    706 
    707 int
    708 irframet_poll(void *h, int events, struct lwp *l)
    709 {
    710 	struct tty *tp = h;
    711 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    712 	int revents = 0;
    713 	int s;
    714 
    715 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    716 
    717 	s = splir();
    718 	/* XXX is this a good check? */
    719 	if (events & (POLLOUT | POLLWRNORM))
    720 		if (tp->t_outq.c_cc <= tp->t_lowat)
    721 			revents |= events & (POLLOUT | POLLWRNORM);
    722 
    723 	if (events & (POLLIN | POLLRDNORM)) {
    724 		if (sc->sc_nframes > 0) {
    725 			DPRINTF(("%s: have data\n", __FUNCTION__));
    726 			revents |= events & (POLLIN | POLLRDNORM);
    727 		} else {
    728 			DPRINTF(("%s: recording select\n", __FUNCTION__));
    729 			selrecord(l, &sc->sc_rsel);
    730 		}
    731 	}
    732 	splx(s);
    733 
    734 	return (revents);
    735 }
    736 
    737 static void
    738 filt_irframetrdetach(struct knote *kn)
    739 {
    740 	struct tty *tp = kn->kn_hook;
    741 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    742 	int s;
    743 
    744 	s = splir();
    745 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
    746 	splx(s);
    747 }
    748 
    749 static int
    750 filt_irframetread(struct knote *kn, long hint)
    751 {
    752 	struct tty *tp = kn->kn_hook;
    753 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    754 
    755 	kn->kn_data = sc->sc_nframes;
    756 	return (kn->kn_data > 0);
    757 }
    758 
    759 static void
    760 filt_irframetwdetach(struct knote *kn)
    761 {
    762 	struct tty *tp = kn->kn_hook;
    763 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    764 	int s;
    765 
    766 	s = splir();
    767 	SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext);
    768 	splx(s);
    769 }
    770 
    771 static int
    772 filt_irframetwrite(struct knote *kn, long hint)
    773 {
    774 	struct tty *tp = kn->kn_hook;
    775 
    776 	/* XXX double-check this */
    777 
    778 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    779 		kn->kn_data = tp->t_lowat - tp->t_outq.c_cc;
    780 		return (1);
    781 	}
    782 
    783 	kn->kn_data = 0;
    784 	return (0);
    785 }
    786 
    787 static const struct filterops irframetread_filtops =
    788 	{ 1, NULL, filt_irframetrdetach, filt_irframetread };
    789 static const struct filterops irframetwrite_filtops =
    790 	{ 1, NULL, filt_irframetwdetach, filt_irframetwrite };
    791 
    792 int
    793 irframet_kqfilter(void *h, struct knote *kn)
    794 {
    795 	struct tty *tp = h;
    796 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    797 	struct klist *klist;
    798 	int s;
    799 
    800 	switch (kn->kn_filter) {
    801 	case EVFILT_READ:
    802 		klist = &sc->sc_rsel.sel_klist;
    803 		kn->kn_fop = &irframetread_filtops;
    804 		break;
    805 	case EVFILT_WRITE:
    806 		klist = &sc->sc_wsel.sel_klist;
    807 		kn->kn_fop = &irframetwrite_filtops;
    808 		break;
    809 	default:
    810 		return (1);
    811 	}
    812 
    813 	kn->kn_hook = tp;
    814 
    815 	s = splir();
    816 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    817 	splx(s);
    818 
    819 	return (0);
    820 }
    821 
    822 int
    823 irframet_set_params(void *h, struct irda_params *p)
    824 {
    825 	struct tty *tp = h;
    826 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    827 	int i;
    828 
    829 	DPRINTF(("%s: tp=%p speed=%d ebofs=%d maxsize=%d\n",
    830 		 __FUNCTION__, tp, p->speed, p->ebofs, p->maxsize));
    831 
    832 	if (p->speed != sc->sc_params.speed) {
    833 		/* Checked in irframe.c */
    834 		lockmgr(&sc->sc_wr_lk, LK_EXCLUSIVE, NULL);
    835 		irt_dongles[sc->sc_dongle].setspeed(tp, p->speed);
    836 		lockmgr(&sc->sc_wr_lk, LK_RELEASE, NULL);
    837 		sc->sc_params.speed = p->speed;
    838 	}
    839 
    840 	/* Max size checked in irframe.c */
    841 	sc->sc_params.ebofs = p->ebofs;
    842 	/* Max size checked in irframe.c */
    843 	if (sc->sc_params.maxsize != p->maxsize) {
    844 		sc->sc_params.maxsize = p->maxsize;
    845 		if (sc->sc_inbuf != NULL)
    846 			free(sc->sc_inbuf, M_DEVBUF);
    847 		for (i = 0; i < MAXFRAMES; i++)
    848 			if (sc->sc_frames[i].buf != NULL)
    849 				free(sc->sc_frames[i].buf, M_DEVBUF);
    850 		if (sc->sc_params.maxsize != 0) {
    851 			sc->sc_inbuf = malloc(sc->sc_params.maxsize+2,
    852 					      M_DEVBUF, M_WAITOK);
    853 			for (i = 0; i < MAXFRAMES; i++)
    854 				sc->sc_frames[i].buf =
    855 					malloc(sc->sc_params.maxsize,
    856 					       M_DEVBUF, M_WAITOK);
    857 		} else {
    858 			sc->sc_inbuf = NULL;
    859 			for (i = 0; i < MAXFRAMES; i++)
    860 				sc->sc_frames[i].buf = NULL;
    861 		}
    862 	}
    863 	sc->sc_framestate = FRAME_OUTSIDE;
    864 
    865 	return (0);
    866 }
    867 
    868 int
    869 irframet_get_speeds(void *h, int *speeds)
    870 {
    871 	struct tty *tp = h;
    872 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    873 
    874 	DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
    875 
    876 	if (sc == NULL)		/* during attach */
    877 		*speeds = IRDA_SPEEDS_SIR;
    878 	else
    879 		*speeds = irt_dongles[sc->sc_dongle].speedmask;
    880 	return (0);
    881 }
    882 
    883 int
    884 irframet_get_turnarounds(void *h, int *turnarounds)
    885 {
    886 	struct tty *tp = h;
    887 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    888 
    889 	DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
    890 
    891 	*turnarounds = irt_dongles[sc->sc_dongle].turnmask;
    892 	return (0);
    893 }
    894 
    895 void
    896 irt_ioctl(struct tty *tp, u_long cmd, void *arg)
    897 {
    898 	const struct cdevsw *cdev;
    899 	int error;
    900 	dev_t dev;
    901 
    902 	dev = tp->t_dev;
    903 	cdev = cdevsw_lookup(dev);
    904 	if (cdev != NULL)
    905 		error = (*cdev->d_ioctl)(dev, cmd, arg, 0, curlwp);
    906 	else
    907 		error = ENXIO;
    908 #ifdef DIAGNOSTIC
    909 	if (error)
    910 		printf("irt_ioctl: cmd=0x%08lx error=%d\n", cmd, error);
    911 #endif
    912 }
    913 
    914 void
    915 irt_setspeed(struct tty *tp, u_int speed)
    916 {
    917 	struct termios tt;
    918 
    919 	irt_ioctl(tp, TIOCGETA,  &tt);
    920 	tt.c_ispeed = tt.c_ospeed = speed;
    921 	tt.c_cflag &= ~HUPCL;
    922 	tt.c_cflag |= CLOCAL;
    923 	irt_ioctl(tp, TIOCSETAF, &tt);
    924 }
    925 
    926 void
    927 irt_setline(struct tty *tp, u_int line)
    928 {
    929 	int mline;
    930 
    931 	irt_ioctl(tp, TIOCMGET, &mline);
    932 	mline &= ~(TIOCM_DTR | TIOCM_RTS);
    933 	mline |= line;
    934 	irt_ioctl(tp, TIOCMSET, (void *)&mline);
    935 }
    936 
    937 void
    938 irt_delay(struct tty *tp, u_int ms)
    939 {
    940 	if (cold)
    941 		delay(ms * 1000);
    942 	else
    943 		tsleep(&irt_delay, PZERO, "irtdly", ms * hz / 1000 + 1);
    944 
    945 }
    946 
    947 /**********************************************************************
    948  * No dongle
    949  **********************************************************************/
    950 void
    951 irts_none(struct tty *tp, u_int speed)
    952 {
    953 	irt_setspeed(tp, speed);
    954 }
    955 
    956 /**********************************************************************
    957  * Tekram
    958  **********************************************************************/
    959 #define TEKRAM_PW     0x10
    960 
    961 #define TEKRAM_115200 (TEKRAM_PW|0x00)
    962 #define TEKRAM_57600  (TEKRAM_PW|0x01)
    963 #define TEKRAM_38400  (TEKRAM_PW|0x02)
    964 #define TEKRAM_19200  (TEKRAM_PW|0x03)
    965 #define TEKRAM_9600   (TEKRAM_PW|0x04)
    966 #define TEKRAM_2400   (TEKRAM_PW|0x08)
    967 
    968 #define TEKRAM_TV     (TEKRAM_PW|0x05)
    969 
    970 void
    971 irts_tekram(struct tty *tp, u_int speed)
    972 {
    973 	int s;
    974 
    975 	irt_setspeed(tp, 9600);
    976 	irt_setline(tp, 0);
    977 	irt_delay(tp, 50);
    978 
    979 	irt_setline(tp, TIOCM_RTS);
    980 	irt_delay(tp, 1);
    981 
    982 	irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
    983 	irt_delay(tp, 1);	/* 50 us */
    984 
    985 	irt_setline(tp, TIOCM_DTR);
    986 	irt_delay(tp, 1);	/* 7 us */
    987 
    988 	switch(speed) {
    989 	case 115200: s = TEKRAM_115200; break;
    990 	case 57600:  s = TEKRAM_57600; break;
    991 	case 38400:  s = TEKRAM_38400; break;
    992 	case 19200:  s = TEKRAM_19200; break;
    993 	case 2400:   s = TEKRAM_2400; break;
    994 	default:     s = TEKRAM_9600; break;
    995 	}
    996 	irt_putc(tp, s);
    997 	irframetstart(tp);
    998 
    999 	irt_delay(tp, 100);
   1000 
   1001 	irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
   1002 	if (speed != 9600)
   1003 		irt_setspeed(tp, speed);
   1004 	irt_delay(tp, 1);	/* 50 us */
   1005 }
   1006 
   1007 /**********************************************************************
   1008  * Jeteye
   1009  **********************************************************************/
   1010 void
   1011 irts_jeteye(struct tty *tp, u_int speed)
   1012 {
   1013 	switch (speed) {
   1014 	case 19200:
   1015 		irt_setline(tp, TIOCM_DTR);
   1016 		break;
   1017 	case 115200:
   1018 		irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
   1019 		break;
   1020 	default: /*9600*/
   1021 		irt_setline(tp, TIOCM_RTS);
   1022 		break;
   1023 	}
   1024 	irt_setspeed(tp, speed);
   1025 }
   1026 
   1027 /**********************************************************************
   1028  * Actisys
   1029  **********************************************************************/
   1030 void
   1031 irts_actisys(struct tty *tp, u_int speed)
   1032 {
   1033 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
   1034 	int pulses;
   1035 
   1036 	irt_setspeed(tp, speed);
   1037 
   1038 	switch(speed) {
   1039 	case 19200:  pulses=1; break;
   1040 	case 57600:  pulses=2; break;
   1041 	case 115200: pulses=3; break;
   1042 	case 38400:  pulses=4; break;
   1043 	default: /* 9600 */ pulses=0; break;
   1044 	}
   1045 
   1046 	if (sc->sc_dongle_private == 0) {
   1047 		sc->sc_dongle_private = 1;
   1048 		irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
   1049 		/*
   1050 		 * Must wait at least 50ms after initial
   1051 		 * power on to charge internal capacitor
   1052 		 */
   1053 		irt_delay(tp, 50);
   1054 	}
   1055 	irt_setline(tp, TIOCM_RTS);
   1056 	delay(2);
   1057 	for (;;) {
   1058 		irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
   1059 		delay(2);
   1060 		if (--pulses <= 0)
   1061 			break;
   1062 		irt_setline(tp, TIOCM_DTR);
   1063 		delay(2);
   1064 	}
   1065 }
   1066 
   1067 /**********************************************************************
   1068  * Litelink
   1069  **********************************************************************/
   1070 void
   1071 irts_litelink(struct tty *tp, u_int speed)
   1072 {
   1073 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
   1074 	int pulses;
   1075 
   1076 	irt_setspeed(tp, speed);
   1077 
   1078 	switch(speed) {
   1079 	case 57600:  pulses=1; break;
   1080 	case 38400:  pulses=2; break;
   1081 	case 19200:  pulses=3; break;
   1082 	case 9600:   pulses=4; break;
   1083 	default: /* 115200 */ pulses=0; break;
   1084 	}
   1085 
   1086 	if (sc->sc_dongle_private == 0) {
   1087 		sc->sc_dongle_private = 1;
   1088 		irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
   1089 	}
   1090 	irt_setline(tp, TIOCM_RTS);
   1091 	irt_delay(tp, 1); /* 15 us */;
   1092 	for (;;) {
   1093 		irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
   1094 		irt_delay(tp, 1); /* 15 us */;
   1095 		if (--pulses <= 0)
   1096 			break;
   1097 		irt_setline(tp, TIOCM_DTR);
   1098 		irt_delay(tp, 1); /* 15 us */;
   1099 	}
   1100 }
   1101 
   1102 /**********************************************************************
   1103  * Girbil
   1104  **********************************************************************/
   1105 /* Control register 1 */
   1106 #define GIRBIL_TXEN      0x01 /* Enable transmitter */
   1107 #define GIRBIL_RXEN      0x02 /* Enable receiver */
   1108 #define GIRBIL_ECAN      0x04 /* Cancel self emmited data */
   1109 #define GIRBIL_ECHO      0x08 /* Echo control characters */
   1110 
   1111 /* LED Current Register */
   1112 #define GIRBIL_HIGH      0x20
   1113 #define GIRBIL_MEDIUM    0x21
   1114 #define GIRBIL_LOW       0x22
   1115 
   1116 /* Baud register */
   1117 #define GIRBIL_2400      0x30
   1118 #define GIRBIL_4800      0x31
   1119 #define GIRBIL_9600      0x32
   1120 #define GIRBIL_19200     0x33
   1121 #define GIRBIL_38400     0x34
   1122 #define GIRBIL_57600     0x35
   1123 #define GIRBIL_115200    0x36
   1124 
   1125 /* Mode register */
   1126 #define GIRBIL_IRDA      0x40
   1127 #define GIRBIL_ASK       0x41
   1128 
   1129 /* Control register 2 */
   1130 #define GIRBIL_LOAD      0x51 /* Load the new baud rate value */
   1131 
   1132 void
   1133 irts_girbil(struct tty *tp, u_int speed)
   1134 {
   1135 	int s;
   1136 
   1137 	irt_setspeed(tp, 9600);
   1138 	irt_setline(tp, TIOCM_DTR);
   1139 	irt_delay(tp, 5);
   1140 	irt_setline(tp, TIOCM_RTS);
   1141 	irt_delay(tp, 20);
   1142 	switch(speed) {
   1143 	case 115200: s = GIRBIL_115200; break;
   1144 	case 57600:  s = GIRBIL_57600; break;
   1145 	case 38400:  s = GIRBIL_38400; break;
   1146 	case 19200:  s = GIRBIL_19200; break;
   1147 	case 4800:   s = GIRBIL_4800; break;
   1148 	case 2400:   s = GIRBIL_2400; break;
   1149 	default:     s = GIRBIL_9600; break;
   1150 	}
   1151 	irt_putc(tp, GIRBIL_TXEN|GIRBIL_RXEN);
   1152 	irt_putc(tp, s);
   1153 	irt_putc(tp, GIRBIL_LOAD);
   1154 	irframetstart(tp);
   1155 	irt_delay(tp, 100);
   1156 	irt_setline(tp, TIOCM_DTR | TIOCM_RTS);
   1157 	if (speed != 9600)
   1158 		irt_setspeed(tp, speed);
   1159 }
   1160