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