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