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