Home | History | Annotate | Line # | Download | only in ir
irframe_tty.c revision 1.9
      1 /*	$NetBSD: irframe_tty.c,v 1.9 2001/12/05 14:50:14 augustss Exp $	*/
      2 
      3 /*
      4  * TODO
      5  * Implement dongle support.
      6  * Test!!!
      7  * Get rid of MAX_IRDA_FRAME
      8  */
      9 
     10 /*
     11  * Copyright (c) 2001 The NetBSD Foundation, Inc.
     12  * All rights reserved.
     13  *
     14  * This code is derived from software contributed to The NetBSD Foundation
     15  * by Lennart Augustsson (lennart (at) augustsson.net) and Tommy Bohlin
     16  * (tommy (at) gatespace.com).
     17  *
     18  * Redistribution and use in source and binary forms, with or without
     19  * modification, are permitted provided that the following conditions
     20  * are met:
     21  * 1. Redistributions of source code must retain the above copyright
     22  *    notice, this list of conditions and the following disclaimer.
     23  * 2. Redistributions in binary form must reproduce the above copyright
     24  *    notice, this list of conditions and the following disclaimer in the
     25  *    documentation and/or other materials provided with the distribution.
     26  * 3. All advertising materials mentioning features or use of this software
     27  *    must display the following acknowledgement:
     28  *        This product includes software developed by the NetBSD
     29  *        Foundation, Inc. and its contributors.
     30  * 4. Neither the name of The NetBSD Foundation nor the names of its
     31  *    contributors may be used to endorse or promote products derived
     32  *    from this software without specific prior written permission.
     33  *
     34  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     35  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     36  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     37  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     39  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     40  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     41  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     42  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     43  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     44  * POSSIBILITY OF SUCH DAMAGE.
     45  */
     46 
     47 /*
     48  * Loosely based on ppp_tty.c.
     49  * Framing and dongle handling written by Tommy Bohlin.
     50  */
     51 
     52 #include <sys/param.h>
     53 #include <sys/proc.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/tty.h>
     56 #include <sys/kernel.h>
     57 #include <sys/malloc.h>
     58 #include <sys/conf.h>
     59 #include <sys/systm.h>
     60 #include <sys/device.h>
     61 #include <sys/file.h>
     62 #include <sys/vnode.h>
     63 #include <sys/poll.h>
     64 
     65 #include <dev/ir/ir.h>
     66 #include <dev/ir/sir.h>
     67 #include <dev/ir/irdaio.h>
     68 #include <dev/ir/irframevar.h>
     69 
     70 /* Macros to clear/set/test flags. */
     71 #define	SET(t, f)	(t) |= (f)
     72 #define	CLR(t, f)	(t) &= ~(f)
     73 #define	ISSET(t, f)	((t) & (f))
     74 
     75 #ifdef IRFRAMET_DEBUG
     76 #define DPRINTF(x)	if (irframetdebug) printf x
     77 #define Static
     78 int irframetdebug = 0;
     79 #else
     80 #define DPRINTF(x)
     81 #define Static static
     82 #endif
     83 
     84 /*****/
     85 
     86 #define MAX_IRDA_FRAME 5000	/* XXX what is it? */
     87 
     88 struct frame {
     89 	u_char *buf;
     90 	u_int len;
     91 };
     92 #define MAXFRAMES 4
     93 
     94 struct irframet_softc {
     95 	struct irframe_softc sc_irp;
     96 	struct tty *sc_tp;
     97 
     98 	int sc_dongle;
     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(void *h, u_int8_t *buf, size_t len);
    150 Static int	irt_putc(int c, struct tty *tp);
    151 Static void	irt_frame(struct irframet_softc *sc, u_char *buf, u_int len);
    152 Static void	irt_timeout(void *v);
    153 
    154 Static struct irframe_methods irframet_methods = {
    155 	irframet_open, irframet_close, irframet_read, irframet_write,
    156 	irframet_poll, irframet_set_params,
    157 	irframet_get_speeds, irframet_get_turnarounds
    158 };
    159 
    160 void
    161 irframettyattach(int n)
    162 {
    163 }
    164 
    165 /*
    166  * Line specific open routine for async tty devices.
    167  * Attach the given tty to the first available irframe unit.
    168  * Called from device open routine or ttioctl.
    169  */
    170 /* ARGSUSED */
    171 int
    172 irframetopen(dev_t dev, struct tty *tp)
    173 {
    174 	struct proc *p = curproc;		/* XXX */
    175 	struct irframet_softc *sc;
    176 	int error, s;
    177 
    178 	DPRINTF(("%s\n", __FUNCTION__));
    179 
    180 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    181 		return (error);
    182 
    183 	s = spltty();
    184 
    185 	DPRINTF(("%s: linesw=%p disc=%s\n", __FUNCTION__, tp->t_linesw,
    186 		 tp->t_linesw->l_name));
    187 	if (strcmp(tp->t_linesw->l_name, "irframe") == 0) { /* XXX */
    188 		sc = (struct irframet_softc *)tp->t_sc;
    189 		DPRINTF(("%s: sc=%p sc_tp=%p\n", __FUNCTION__, sc, sc->sc_tp));
    190 		if (sc != NULL) {
    191 			splx(s);
    192 			return (EBUSY);
    193 		}
    194 	}
    195 
    196 	tp->t_sc = irframe_alloc(sizeof (struct irframet_softc),
    197 			&irframet_methods, tp);
    198 	sc = (struct irframet_softc *)tp->t_sc;
    199 	sc->sc_tp = tp;
    200 	printf("%s attached at tty%02d\n", sc->sc_irp.sc_dev.dv_xname,
    201 	    minor(tp->t_dev));
    202 
    203 	DPRINTF(("%s: set sc=%p\n", __FUNCTION__, sc));
    204 
    205 	ttyflush(tp, FREAD | FWRITE);
    206 
    207 	sc->sc_dongle = DONGLE_NONE;
    208 
    209 	splx(s);
    210 
    211 	return (0);
    212 }
    213 
    214 /*
    215  * Line specific close routine, called from device close routine
    216  * and from ttioctl.
    217  * Detach the tty from the irframe unit.
    218  * Mimics part of ttyclose().
    219  */
    220 int
    221 irframetclose(struct tty *tp, int flag)
    222 {
    223 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    224 	int s;
    225 
    226 	DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
    227 
    228 	s = spltty();
    229 	ttyflush(tp, FREAD | FWRITE);
    230 	tp->t_linesw = linesw[0]; /* default line discipline */
    231 	if (sc != NULL) {
    232 		tp->t_sc = NULL;
    233 		printf("%s detached from tty%02d\n", sc->sc_irp.sc_dev.dv_xname,
    234 		    minor(tp->t_dev));
    235 
    236 		if (sc->sc_tp == tp)
    237 			irframe_dealloc(&sc->sc_irp.sc_dev);
    238 	}
    239 	splx(s);
    240 	return (0);
    241 }
    242 
    243 /*
    244  * Line specific (tty) ioctl routine.
    245  * This discipline requires that tty device drivers call
    246  * the line specific l_ioctl routine from their ioctl routines.
    247  */
    248 /* ARGSUSED */
    249 int
    250 irframetioctl(struct tty *tp, u_long cmd, caddr_t data, int flag,
    251 	     struct proc *p)
    252 {
    253 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    254 	int error;
    255 
    256 	DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
    257 
    258 	if (sc == NULL || tp != sc->sc_tp)
    259 		return (-1);
    260 
    261 	error = 0;
    262 	switch (cmd) {
    263 	case IRFRAMETTY_GET_DEVICE:
    264 		*(int *)data = sc->sc_irp.sc_dev.dv_unit;
    265 		break;
    266 	case IRFRAMETTY_GET_DONGLE:
    267 		*(int *)data = sc->sc_dongle;
    268 		break;
    269 	case IRFRAMETTY_SET_DONGLE:
    270 		sc->sc_dongle = *(int *)data;
    271 		break;
    272 	default:
    273 		error = EINVAL;
    274 		break;
    275 	}
    276 
    277 	return (error);
    278 }
    279 
    280 /*
    281  * Start output on async tty interface.
    282  * Called at spltty or higher.
    283  */
    284 int
    285 irframetstart(struct tty *tp)
    286 {
    287 	/*struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;*/
    288 
    289 	DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
    290 
    291 	if (tp->t_oproc != NULL)
    292 		(*tp->t_oproc)(tp);
    293 
    294 	return (0);
    295 }
    296 
    297 void
    298 irt_frame(struct irframet_softc *sc, u_char *buf, u_int len)
    299 {
    300 	DPRINTF(("%s: nframe=%d framei=%d frameo=%d\n",
    301 		 __FUNCTION__, sc->sc_nframes, sc->sc_framei, sc->sc_frameo));
    302 
    303 	if (sc->sc_nframes >= MAXFRAMES) {
    304 #ifdef IRFRAMET_DEBUG
    305 		printf("%s: dropped frame\n", __FUNCTION__);
    306 #endif
    307 		return;
    308 	}
    309 	if (sc->sc_frames[sc->sc_framei].buf == NULL)
    310 		return;
    311 	memcpy(sc->sc_frames[sc->sc_framei].buf, buf, len);
    312 	sc->sc_frames[sc->sc_framei].len = len;
    313 	sc->sc_framei = (sc->sc_framei+1) % MAXFRAMES;
    314 	sc->sc_nframes++;
    315 	if (sc->sc_state & IRT_RSLP) {
    316 		sc->sc_state &= ~IRT_RSLP;
    317 		DPRINTF(("%s: waking up reader\n", __FUNCTION__));
    318 		wakeup(sc->sc_frames);
    319 	}
    320 	selwakeup(&sc->sc_rsel);
    321 }
    322 
    323 void
    324 irt_timeout(void *v)
    325 {
    326 	struct irframet_softc *sc = v;
    327 
    328 #ifdef IRFRAMET_DEBUG
    329 	if (sc->sc_framestate != FRAME_OUTSIDE)
    330 		printf("%s: input frame timeout\n", __FUNCTION__);
    331 #endif
    332 	sc->sc_framestate = FRAME_OUTSIDE;
    333 }
    334 
    335 int
    336 irframetinput(int c, struct tty *tp)
    337 {
    338 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    339 
    340 	c &= 0xff;
    341 
    342 #if IRFRAMET_DEBUG
    343 	if (irframetdebug > 1)
    344 		DPRINTF(("%s: tp=%p c=0x%02x\n", __FUNCTION__, tp, c));
    345 #endif
    346 
    347 	if (sc == NULL || tp != (struct tty *)sc->sc_tp)
    348 		return (0);
    349 
    350 	if (sc->sc_inbuf == NULL)
    351 		return (0);
    352 
    353 	switch (c) {
    354 	case SIR_BOF:
    355 		DPRINTF(("%s: BOF\n", __FUNCTION__));
    356 		sc->sc_framestate = FRAME_INSIDE;
    357 		sc->sc_inchars = 0;
    358 		sc->sc_inFCS = INITFCS;
    359 		break;
    360 	case SIR_EOF:
    361 		DPRINTF(("%s: EOF state=%d inchars=%d fcs=0x%04x\n",
    362 			 __FUNCTION__,
    363 			 sc->sc_framestate, sc->sc_inchars, sc->sc_inFCS));
    364 		if (sc->sc_framestate == FRAME_INSIDE &&
    365 		    sc->sc_inchars >= 4 && sc->sc_inFCS == GOODFCS) {
    366 			irt_frame(sc, sc->sc_inbuf, sc->sc_inchars - 2);
    367 		} else if (sc->sc_framestate != FRAME_OUTSIDE) {
    368 #ifdef IRFRAMET_DEBUG
    369 			printf("%s: malformed input frame\n", __FUNCTION__);
    370 #endif
    371 		}
    372 		sc->sc_framestate = FRAME_OUTSIDE;
    373 		break;
    374 	case SIR_CE:
    375 		DPRINTF(("%s: CE\n", __FUNCTION__));
    376 		if (sc->sc_framestate == FRAME_INSIDE)
    377 			sc->sc_framestate = FRAME_ESCAPE;
    378 		break;
    379 	default:
    380 #if IRFRAMET_DEBUG
    381 	if (irframetdebug > 1)
    382 		DPRINTF(("%s: c=0x%02x, inchar=%d state=%d\n", __FUNCTION__, c,
    383 			 sc->sc_inchars, sc->sc_state));
    384 #endif
    385 		if (sc->sc_framestate != FRAME_OUTSIDE) {
    386 			if (sc->sc_framestate == FRAME_ESCAPE) {
    387 				sc->sc_framestate = FRAME_INSIDE;
    388 				c ^= SIR_ESC_BIT;
    389 			}
    390 			if (sc->sc_inchars < sc->sc_maxsize + 2) {
    391 				sc->sc_inbuf[sc->sc_inchars++] = c;
    392 				sc->sc_inFCS = updateFCS(sc->sc_inFCS, c);
    393 			} else {
    394 				sc->sc_framestate = FRAME_OUTSIDE;
    395 #ifdef IRFRAMET_DEBUG
    396 				printf("%s: input frame overrun\n",
    397 				       __FUNCTION__);
    398 #endif
    399 			}
    400 		}
    401 		break;
    402 	}
    403 
    404 #if 1
    405 	if (sc->sc_framestate != FRAME_OUTSIDE) {
    406 		callout_reset(&sc->sc_timeout, hz/20, irt_timeout, sc);
    407 	}
    408 #endif
    409 
    410 	return (0);
    411 }
    412 
    413 
    414 /*** irframe methods ***/
    415 
    416 int
    417 irframet_open(void *h, int flag, int mode, struct proc *p)
    418 {
    419 	struct tty *tp = h;
    420 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    421 
    422 	DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
    423 
    424 	sc->sc_speed = 0;
    425 	sc->sc_ebofs = IRDA_DEFAULT_EBOFS;
    426 	sc->sc_maxsize = 0;
    427 	sc->sc_framestate = FRAME_OUTSIDE;
    428 	sc->sc_nframes = 0;
    429 	sc->sc_framei = 0;
    430 	sc->sc_frameo = 0;
    431 	callout_init(&sc->sc_timeout);
    432 
    433 	return (0);
    434 }
    435 
    436 int
    437 irframet_close(void *h, int flag, int mode, struct proc *p)
    438 {
    439 	struct tty *tp = h;
    440 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    441 	int i, s;
    442 
    443 	DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
    444 
    445 	callout_stop(&sc->sc_timeout);
    446 	s = splir();
    447 	if (sc->sc_inbuf != NULL) {
    448 		free(sc->sc_inbuf, M_DEVBUF);
    449 		sc->sc_inbuf = NULL;
    450 	}
    451 	for (i = 0; i < MAXFRAMES; i++) {
    452 		if (sc->sc_frames[i].buf != NULL) {
    453 			free(sc->sc_frames[i].buf, M_DEVBUF);
    454 			sc->sc_frames[i].buf = NULL;
    455 		}
    456 	}
    457 	splx(s);
    458 
    459 	return (0);
    460 }
    461 
    462 int
    463 irframet_read(void *h, struct uio *uio, int flag)
    464 {
    465 	struct tty *tp = h;
    466 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    467 	int error = 0;
    468 	int s;
    469 
    470 	DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
    471 		 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
    472 		 (long)uio->uio_offset));
    473 	DPRINTF(("%s: nframe=%d framei=%d frameo=%d\n",
    474 		 __FUNCTION__, sc->sc_nframes, sc->sc_framei, sc->sc_frameo));
    475 
    476 
    477 	s = splir();
    478 	while (sc->sc_nframes == 0) {
    479 		if (flag & IO_NDELAY) {
    480 			splx(s);
    481 			return (EWOULDBLOCK);
    482 		}
    483 		sc->sc_state |= IRT_RSLP;
    484 		DPRINTF(("%s: sleep\n", __FUNCTION__));
    485 		error = tsleep(sc->sc_frames, PZERO | PCATCH, "irtrd", 0);
    486 		DPRINTF(("%s: woke, error=%d\n", __FUNCTION__, error));
    487 		if (error) {
    488 			sc->sc_state &= ~IRT_RSLP;
    489 			break;
    490 		}
    491 	}
    492 
    493 	/* Do just one frame transfer per read */
    494 	if (!error) {
    495 		if (uio->uio_resid < sc->sc_frames[sc->sc_frameo].len) {
    496 			DPRINTF(("%s: uio buffer smaller than frame size (%d < %d)\n", __FUNCTION__, uio->uio_resid, sc->sc_frames[sc->sc_frameo].len));
    497 			error = EINVAL;
    498 		} else {
    499 			DPRINTF(("%s: moving %d bytes\n", __FUNCTION__,
    500 				 sc->sc_frames[sc->sc_frameo].len));
    501 			error = uiomove(sc->sc_frames[sc->sc_frameo].buf,
    502 					sc->sc_frames[sc->sc_frameo].len, uio);
    503 			DPRINTF(("%s: error=%d\n", __FUNCTION__, error));
    504 		}
    505 		sc->sc_frameo = (sc->sc_frameo+1) % MAXFRAMES;
    506 		sc->sc_nframes--;
    507 	}
    508 	splx(s);
    509 
    510 	return (error);
    511 }
    512 
    513 int
    514 irt_putc(int c, struct tty *tp)
    515 {
    516 	int s;
    517 	int error;
    518 
    519 #if IRFRAMET_DEBUG
    520 	if (irframetdebug > 3)
    521 		DPRINTF(("%s: tp=%p c=0x%02x cc=%d\n", __FUNCTION__, tp, c,
    522 			 tp->t_outq.c_cc));
    523 #endif
    524 	if (tp->t_outq.c_cc > tp->t_hiwat) {
    525 		irframetstart(tp);
    526 		s = spltty();
    527 		/*
    528 		 * This can only occur if FLUSHO is set in t_lflag,
    529 		 * or if ttstart/oproc is synchronous (or very fast).
    530 		 */
    531 		if (tp->t_outq.c_cc <= tp->t_hiwat) {
    532 			splx(s);
    533 			goto go;
    534 		}
    535 		SET(tp->t_state, TS_ASLEEP);
    536 		error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
    537 		splx(s);
    538 		if (error)
    539 			return (error);
    540 	}
    541  go:
    542 	if (putc(c, &tp->t_outq) < 0) {
    543 		printf("irframe: putc failed\n");
    544 		return (EIO);
    545 	}
    546 	return (0);
    547 }
    548 
    549 int
    550 irframet_write(void *h, struct uio *uio, int flag)
    551 {
    552 	struct tty *tp = h;
    553 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    554 	u_int8_t buf[MAX_IRDA_FRAME];
    555 	int n;
    556 
    557 	DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
    558 		 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
    559 		 (long)uio->uio_offset));
    560 
    561 	n = irda_sir_frame(buf, MAX_IRDA_FRAME, uio, sc->sc_ebofs);
    562 	if (n < 0)
    563 		return (-n);
    564 	return (irt_write_frame(h, buf, n));
    565 }
    566 
    567 int
    568 irt_write_frame(void *h, u_int8_t *buf, size_t len)
    569 {
    570 	struct tty *tp = h;
    571 	int error, i;
    572 
    573 	DPRINTF(("%s: tp=%p len=%d\n", __FUNCTION__, tp, len));
    574 
    575 	error = 0;
    576 	for (i = 0; !error && i < len; i++)
    577 		error = irt_putc(buf[i], tp);
    578 
    579 	irframetstart(tp);
    580 
    581 	DPRINTF(("%s: done, error=%d\n", __FUNCTION__, error));
    582 
    583 	return (error);
    584 }
    585 
    586 int
    587 irframet_poll(void *h, int events, struct proc *p)
    588 {
    589 	struct tty *tp = h;
    590 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    591 	int revents = 0;
    592 	int s;
    593 
    594 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    595 
    596 	s = splir();
    597 	/* XXX is this a good check? */
    598 	if (events & (POLLOUT | POLLWRNORM))
    599 		if (tp->t_outq.c_cc <= tp->t_lowat)
    600 			revents |= events & (POLLOUT | POLLWRNORM);
    601 
    602 	if (events & (POLLIN | POLLRDNORM)) {
    603 		if (sc->sc_nframes > 0) {
    604 			DPRINTF(("%s: have data\n", __FUNCTION__));
    605 			revents |= events & (POLLIN | POLLRDNORM);
    606 		} else {
    607 			DPRINTF(("%s: recording select\n", __FUNCTION__));
    608 			selrecord(p, &sc->sc_rsel);
    609 		}
    610 	}
    611 	splx(s);
    612 
    613 	return (revents);
    614 }
    615 
    616 int
    617 irframet_set_params(void *h, struct irda_params *p)
    618 {
    619 	struct tty *tp = h;
    620 	struct irframet_softc *sc = (struct irframet_softc *)tp->t_sc;
    621 	struct termios tt;
    622 	int i;
    623 
    624 	DPRINTF(("%s: tp=%p speed=%d ebofs=%d maxsize=%d\n",
    625 		 __FUNCTION__, tp, p->speed, p->ebofs, p->maxsize));
    626 
    627 	if (p->speed != sc->sc_speed) {
    628 		switch (p->speed) {
    629 		case   2400:
    630 		case   9600:
    631 		case  19200:
    632 		case  38400:
    633 		case  57600:
    634 		case 115200:
    635 			break;
    636 		default: return (EINVAL);
    637 		}
    638 		ttioctl(tp, TIOCGETA,  (caddr_t)&tt, 0, curproc);
    639 		sc->sc_speed = tt.c_ispeed = tt.c_ospeed = p->speed;
    640 		ttioctl(tp, TIOCSETAF, (caddr_t)&tt, 0, curproc);
    641 	}
    642 
    643 	sc->sc_ebofs = p->ebofs;
    644 	if (sc->sc_maxsize != p->maxsize) {
    645 		sc->sc_maxsize = p->maxsize;
    646 		if (sc->sc_inbuf != NULL)
    647 			free(sc->sc_inbuf, M_DEVBUF);
    648 		for (i = 0; i < MAXFRAMES; i++)
    649 			if (sc->sc_frames[i].buf != NULL)
    650 				free(sc->sc_frames[i].buf, M_DEVBUF);
    651 		if (sc->sc_maxsize != 0) {
    652 			sc->sc_inbuf = malloc(sc->sc_maxsize+2, M_DEVBUF,
    653 					      M_WAITOK);
    654 			for (i = 0; i < MAXFRAMES; i++)
    655 				sc->sc_frames[i].buf = malloc(sc->sc_maxsize,
    656 							   M_DEVBUF, M_WAITOK);
    657 		} else {
    658 			sc->sc_inbuf = NULL;
    659 			for (i = 0; i < MAXFRAMES; i++)
    660 				sc->sc_frames[i].buf = NULL;
    661 		}
    662 	}
    663 	sc->sc_framestate = FRAME_OUTSIDE;
    664 
    665 	return (0);
    666 }
    667 
    668 int
    669 irframet_get_speeds(void *h, int *speeds)
    670 {
    671 	struct tty *tp = h;
    672 
    673 	tp = tp;
    674 	DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
    675 	*speeds = IRDA_SPEEDS_SIR;
    676 	return (0);
    677 }
    678 
    679 int
    680 irframet_get_turnarounds(void *h, int *turnarounds)
    681 {
    682 	struct tty *tp = h;
    683 
    684 	tp = tp;
    685 	DPRINTF(("%s: tp=%p\n", __FUNCTION__, tp));
    686 	*turnarounds = IRDA_TURNT_10000;
    687 	return (0);
    688 }
    689