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