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