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