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