rc.c revision 129879
150476Speter/*
247857Sbrian * Copyright (C) 1995 by Pavel Antonov, Moscow, Russia.
347857Sbrian * Copyright (C) 1995 by Andrey A. Chernov, Moscow, Russia.
447857Sbrian * Copyright (C) 2002 by John Baldwin <jhb@FreeBSD.org>
547857Sbrian * All rights reserved.
647857Sbrian *
747857Sbrian * Redistribution and use in source and binary forms, with or without
847857Sbrian * modification, are permitted provided that the following conditions
947857Sbrian * are met:
1047857Sbrian * 1. Redistributions of source code must retain the above copyright
1147857Sbrian *    notice, this list of conditions and the following disclaimer.
1247857Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1347857Sbrian *    notice, this list of conditions and the following disclaimer in the
1447857Sbrian *    documentation and/or other materials provided with the distribution.
1547857Sbrian *
1647857Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND
1747857Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1847857Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1947857Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2047857Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2147857Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2247857Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2347857Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2447857Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2547857Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2647857Sbrian * SUCH DAMAGE.
2747857Sbrian *
2847857Sbrian * $FreeBSD: head/sys/dev/rc/rc.c 129879 2004-05-30 20:08:47Z phk $
2947857Sbrian */
3047857Sbrian
3147857Sbrian/*
3247857Sbrian * SDL Communications Riscom/8 (based on Cirrus Logic CL-CD180) driver
3347857Sbrian *
3447857Sbrian */
3547857Sbrian
3647857Sbrian/*#define RCDEBUG*/
3747857Sbrian
3847857Sbrian#include "opt_tty.h"
3947857Sbrian
4047857Sbrian#include <sys/param.h>
4147857Sbrian#include <sys/systm.h>
4247857Sbrian#include <sys/bus.h>
4347857Sbrian#include <sys/conf.h>
4447857Sbrian#include <sys/fcntl.h>
4547857Sbrian#include <sys/interrupt.h>
4647857Sbrian#include <sys/kernel.h>
4747857Sbrian#include <sys/malloc.h>
4847857Sbrian#include <sys/module.h>
4947857Sbrian#include <sys/tty.h>
5047857Sbrian#include <machine/bus.h>
5147857Sbrian#include <machine/resource.h>
5247857Sbrian#include <sys/rman.h>
5347857Sbrian
5447857Sbrian#include <dev/ic/cd180.h>
5547857Sbrian#include <dev/rc/rcreg.h>
5647857Sbrian#include <isa/isavar.h>
5747857Sbrian
5847857Sbrian#define	IOBASE_ADDRS	14
5947857Sbrian
6047857Sbrian#define	DEV_TO_RC(dev)		(struct rc_chans *)((dev)->si_drv1)
6147857Sbrian#define	TTY_TO_RC(tty)		DEV_TO_RC((tty)->t_dev)
6247857Sbrian
6347857Sbrian#define rcin(sc, port)		RC_IN(sc, port)
6447857Sbrian#define rcout(sc, port, v)	RC_OUT(sc, port, v)
6547857Sbrian
6647857Sbrian#define WAITFORCCR(sc, chan)	rc_wait0((sc), (chan), __LINE__)
6747857Sbrian
6847857Sbrian#define CCRCMD(sc, chan, cmd) do {					\
6947857Sbrian	WAITFORCCR((sc), (chan));					\
7047857Sbrian	rcout((sc), CD180_CCR, (cmd));					\
7147857Sbrian} while (0)
7247857Sbrian
7347857Sbrian#define RC_IBUFSIZE     256
7447857Sbrian#define RB_I_HIGH_WATER (TTYHOG - 2 * RC_IBUFSIZE)
7547857Sbrian#define RC_OBUFSIZE     512
7647857Sbrian#define RC_IHIGHWATER   (3 * RC_IBUFSIZE / 4)
7747857Sbrian#define INPUT_FLAGS_SHIFT (2 * RC_IBUFSIZE)
7847857Sbrian#define LOTS_OF_EVENTS  64
7947857Sbrian
8047857Sbrian#define RC_FAKEID       0x10
8147857Sbrian
8247857Sbrian#define CALLOUT(dev)    (((intptr_t)(dev)->si_drv2) != 0)
8347857Sbrian
8447857Sbrian/* Per-channel structure */
8547857Sbrianstruct rc_chans  {
8647857Sbrian	struct rc_softc *rc_rcb;                /* back ptr             */
8747857Sbrian	dev_t		 rc_dev;		/* non-callout device	*/
8847857Sbrian	dev_t		 rc_cdev;		/* callout device	*/
8947857Sbrian	u_short          rc_flags;              /* Misc. flags          */
9047857Sbrian	int              rc_chan;               /* Channel #            */
9147857Sbrian	u_char           rc_ier;                /* intr. enable reg     */
9247857Sbrian	u_char           rc_msvr;               /* modem sig. status    */
9347857Sbrian	u_char           rc_cor2;               /* options reg          */
9447857Sbrian	u_char           rc_pendcmd;            /* special cmd pending  */
9547857Sbrian	u_int            rc_dtrwait;            /* dtr timeout          */
9647857Sbrian	u_int            rc_dcdwaits;           /* how many waits DCD in open */
9747857Sbrian	u_char		 rc_hotchar;		/* end packed optimize */
9847857Sbrian	struct tty       rc_tp;                 /* tty struct           */
9947857Sbrian	u_char          *rc_iptr;               /* Chars input buffer         */
10047857Sbrian	u_char          *rc_hiwat;              /* hi-water mark        */
10147857Sbrian	u_char          *rc_bufend;             /* end of buffer        */
10247857Sbrian	u_char          *rc_optr;               /* ptr in output buf    */
10347857Sbrian	u_char          *rc_obufend;            /* end of output buf    */
10447857Sbrian	u_char           rc_ibuf[4 * RC_IBUFSIZE];  /* input buffer         */
10547857Sbrian	u_char           rc_obuf[RC_OBUFSIZE];  /* output buffer        */
10647857Sbrian	struct callout	 rc_dtrcallout;
10747857Sbrian};
10847857Sbrian
10947857Sbrian/* Per-board structure */
11047857Sbrianstruct rc_softc {
11147857Sbrian	device_t	 sc_dev;
11247857Sbrian	struct resource *sc_irq;
11347857Sbrian	struct resource *sc_port[IOBASE_ADDRS];
11447857Sbrian	int		 sc_irqrid;
11547857Sbrian	void		*sc_hwicookie;
11647857Sbrian	bus_space_tag_t  sc_bt;
11747857Sbrian	bus_space_handle_t sc_bh;
11847857Sbrian	u_int            sc_unit;       /* unit #               */
11947857Sbrian	u_char           sc_dtr;        /* DTR status           */
12047857Sbrian	int		 sc_opencount;
12147857Sbrian	int		 sc_scheduled_event;
12247857Sbrian	void		*sc_swicookie;
12347857Sbrian	struct rc_chans  sc_channels[CD180_NCHAN]; /* channels */
12447857Sbrian};
12547857Sbrian
12647857Sbrian/* Static prototypes */
12747857Sbrianstatic void rc_release_resources(device_t dev);
12847857Sbrianstatic void rc_intr(void *);
12947857Sbrianstatic void rc_hwreset(struct rc_softc *, unsigned int);
13047857Sbrianstatic int  rc_test(struct rc_softc *);
13147857Sbrianstatic void rc_discard_output(struct rc_chans *);
13247857Sbrianstatic void rc_hardclose(struct rc_chans *);
13347857Sbrianstatic int  rc_modctl(struct rc_chans *, int, int);
13447857Sbrianstatic void rc_start(struct tty *);
13547857Sbrianstatic void rc_stop(struct tty *, int rw);
13647857Sbrianstatic int  rc_param(struct tty *, struct termios *);
13747857Sbrianstatic void rc_pollcard(void *);
13847857Sbrianstatic void rc_reinit(struct rc_softc *);
13947857Sbrian#ifdef RCDEBUG
14047857Sbrianstatic void printrcflags();
14147857Sbrian#endif
14247857Sbrianstatic void rc_dtrwakeup(void *);
14347857Sbrianstatic void disc_optim(struct tty *tp, struct termios *t, struct rc_chans *);
14447857Sbrianstatic void rc_wait0(struct rc_softc *sc, int chan, int line);
14547857Sbrian
14651049Sbrianstatic	d_open_t	rcopen;
14747857Sbrianstatic	d_close_t	rcclose;
14847857Sbrianstatic	d_ioctl_t	rcioctl;
14947857Sbrian
15047857Sbrianstatic struct cdevsw rc_cdevsw = {
15147857Sbrian	.d_version =	D_VERSION,
15247857Sbrian	.d_open =	rcopen,
15347857Sbrian	.d_close =	rcclose,
15447857Sbrian	.d_ioctl =	rcioctl,
15547857Sbrian	.d_name =	"rc",
15647857Sbrian	.d_flags =	D_TTY | D_NEEDGIANT,
15747857Sbrian};
15847857Sbrian
15947857Sbrianstatic devclass_t rc_devclass;
16047857Sbrian
16147857Sbrian/* Flags */
16247857Sbrian#define RC_DTR_OFF      0x0001          /* DTR wait, for close/open     */
16347857Sbrian#define RC_ACTOUT       0x0002          /* Dial-out port active         */
16447857Sbrian#define RC_RTSFLOW      0x0004          /* RTS flow ctl enabled         */
16547857Sbrian#define RC_CTSFLOW      0x0008          /* CTS flow ctl enabled         */
16647857Sbrian#define RC_DORXFER      0x0010          /* RXFER event planned          */
16747857Sbrian#define RC_DOXXFER      0x0020          /* XXFER event planned          */
16847857Sbrian#define RC_MODCHG       0x0040          /* Modem status changed         */
16947857Sbrian#define RC_OSUSP        0x0080          /* Output suspended             */
17047857Sbrian#define RC_OSBUSY       0x0100          /* start() routine in progress  */
17147857Sbrian#define RC_WAS_BUFOVFL  0x0200          /* low-level buffer ovferflow   */
17247857Sbrian#define RC_WAS_SILOVFL  0x0400          /* silo buffer overflow         */
17347857Sbrian#define RC_SEND_RDY     0x0800          /* ready to send */
17447857Sbrian
17547857Sbrian/* Table for translation of RCSR status bits to internal form */
17647857Sbrianstatic int rc_rcsrt[16] = {
17747857Sbrian	0,             TTY_OE,               TTY_FE,
17847857Sbrian	TTY_FE|TTY_OE, TTY_PE,               TTY_PE|TTY_OE,
17947857Sbrian	TTY_PE|TTY_FE, TTY_PE|TTY_FE|TTY_OE, TTY_BI,
18047857Sbrian	TTY_BI|TTY_OE, TTY_BI|TTY_FE,        TTY_BI|TTY_FE|TTY_OE,
18147857Sbrian	TTY_BI|TTY_PE, TTY_BI|TTY_PE|TTY_OE, TTY_BI|TTY_PE|TTY_FE,
18247857Sbrian	TTY_BI|TTY_PE|TTY_FE|TTY_OE
18347857Sbrian};
18451049Sbrian
18547857Sbrianstatic int rc_ports[] =
18647857Sbrian    { 0x220, 0x240, 0x250, 0x260, 0x2a0, 0x2b0, 0x300, 0x320 };
18747857Sbrianstatic int iobase_addrs[IOBASE_ADDRS] =
18847857Sbrian    { 0, 0x400, 0x800, 0xc00, 0x1400, 0x1800, 0x1c00, 0x2000,
18947857Sbrian      0x3000, 0x3400, 0x3800, 0x3c00, 0x4000, 0x8000 };
19047857Sbrian
19147857Sbrian/**********************************************/
19247857Sbrian
193static int
194rc_probe(device_t dev)
195{
196	u_int port;
197	int i, found;
198
199	/*
200	 * We don't know of any PnP ID's for these cards.
201	 */
202	if (isa_get_logicalid(dev) != 0)
203		return (ENXIO);
204
205	/*
206	 * We have to have an IO port hint that is valid.
207	 */
208	port = isa_get_port(dev);
209	if (port == -1)
210		return (ENXIO);
211	found = 0;
212	for (i = 0; i < sizeof(rc_ports) / sizeof(int); i++)
213		if (rc_ports[i] == port) {
214			found = 1;
215			break;
216		}
217	if (!found)
218		return (ENXIO);
219
220	/*
221	 * We have to have an IRQ hint.
222	 */
223	if (isa_get_irq(dev) == -1)
224		return (ENXIO);
225
226	device_set_desc(dev, "SDL Riscom/8");
227	return (0);
228}
229
230static int
231rc_attach(device_t dev)
232{
233 	struct rc_chans *rc;
234	struct tty *tp;
235	struct rc_softc *sc;
236	u_int port;
237	int base, chan, error, i, x;
238	dev_t cdev;
239
240	sc = device_get_softc(dev);
241	sc->sc_dev = dev;
242
243	/*
244	 * We need to have IO ports.  Lots of them.  We need
245	 * the following ranges relative to the base port:
246	 * 0x0    -   0x10
247	 * 0x400  -  0x410
248	 * 0x800  -  0x810
249	 * 0xc00  -  0xc10
250	 * 0x1400 - 0x1410
251	 * 0x1800 - 0x1810
252	 * 0x1c00 - 0x1c10
253	 * 0x2000 - 0x2010
254	 * 0x3000 - 0x3010
255	 * 0x3400 - 0x3410
256	 * 0x3800 - 0x3810
257	 * 0x3c00 - 0x3c10
258	 * 0x4000 - 0x4010
259	 * 0x8000 - 0x8010
260	 */
261	port = isa_get_port(dev);
262	for (i = 0; i < IOBASE_ADDRS; i++)
263		if (bus_set_resource(dev, SYS_RES_IOPORT, i,
264		    port + iobase_addrs[i], 0x10) != 0)
265			return (ENXIO);
266	error = ENOMEM;
267	for (i = 0; i < IOBASE_ADDRS; i++) {
268		x = i;
269		sc->sc_port[i] = bus_alloc_resource(dev, SYS_RES_IOPORT, &x,
270		    0ul, ~0ul, 0x10, RF_ACTIVE);
271		if (x != i) {
272			device_printf(dev, "ioport %d was rid %d\n", i, x);
273			goto fail;
274		}
275		if (sc->sc_port[i] == NULL) {
276			device_printf(dev, "failed to alloc ioports %x-%x\n",
277			    port + iobase_addrs[i],
278			    port + iobase_addrs[i] + 0x10);
279			goto fail;
280		}
281	}
282	sc->sc_bt = rman_get_bustag(sc->sc_port[0]);
283	sc->sc_bh = rman_get_bushandle(sc->sc_port[0]);
284
285	sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irqrid,
286	    RF_ACTIVE);
287	if (sc->sc_irq == NULL) {
288		device_printf(dev, "failed to alloc IRQ\n");
289		goto fail;
290	}
291
292	/*
293	 * Now do some actual tests to make sure it works.
294	 */
295	error = ENXIO;
296	rcout(sc, CD180_PPRL, 0x22); /* Random values to Prescale reg. */
297	rcout(sc, CD180_PPRH, 0x11);
298	if (rcin(sc, CD180_PPRL) != 0x22 || rcin(sc, CD180_PPRH) != 0x11)
299		goto fail;
300	if (rc_test(sc))
301		goto fail;
302
303	/*
304	 * Ok, start actually hooking things up.
305	 */
306	sc->sc_unit = device_get_unit(dev);
307	/*sc->sc_chipid = 0x10 + device_get_unit(dev);*/
308	device_printf(dev, "%d chans, firmware rev. %c\n",
309		CD180_NCHAN, (rcin(sc, CD180_GFRCR) & 0xF) + 'A');
310	rc = sc->sc_channels;
311	base = CD180_NCHAN * sc->sc_unit;
312	for (chan = 0; chan < CD180_NCHAN; chan++, rc++) {
313		rc->rc_rcb     = sc;
314		rc->rc_chan    = chan;
315		rc->rc_iptr    = rc->rc_ibuf;
316		rc->rc_bufend  = &rc->rc_ibuf[RC_IBUFSIZE];
317		rc->rc_hiwat   = &rc->rc_ibuf[RC_IHIGHWATER];
318		rc->rc_optr    = rc->rc_obufend  = rc->rc_obuf;
319		rc->rc_dtrwait = 3 * hz;
320		callout_init(&rc->rc_dtrcallout, 0);
321		tp = &rc->rc_tp;
322		ttychars(tp);
323		tp->t_lflag = tp->t_iflag = tp->t_oflag = 0;
324		tp->t_cflag = TTYDEF_CFLAG;
325		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
326		cdev = make_dev(&rc_cdevsw, chan + base,
327		    UID_ROOT, GID_WHEEL, 0600, "ttym%d", chan + base);
328		cdev->si_drv1 = rc;
329		cdev->si_drv2 = 0;
330		cdev->si_tty = tp;
331		rc->rc_dev = cdev;
332		cdev = make_dev(&rc_cdevsw, chan + base + 128,
333		    UID_UUCP, GID_DIALER, 0660, "cuam%d", chan + base);
334		cdev->si_drv1 = rc;
335		cdev->si_drv2 = (void *)1;
336		cdev->si_tty = tp;
337		rc->rc_cdev = cdev;
338	}
339
340	error = bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_TTY, rc_intr, sc,
341	    &sc->sc_hwicookie);
342	if (error) {
343		device_printf(dev, "failed to register interrupt handler\n");
344		goto fail;
345	}
346
347	swi_add(&tty_ithd, "tty:rc", rc_pollcard, sc, SWI_TTY, 0,
348	    &sc->sc_swicookie);
349	return (0);
350
351fail:
352	rc_release_resources(dev);
353	return (error);
354}
355
356static int
357rc_detach(device_t dev)
358{
359	struct rc_softc *sc;
360	struct rc_chans *rc;
361	int error, i, s;
362
363	sc = device_get_softc(dev);
364	if (sc->sc_opencount > 0)
365		return (EBUSY);
366	sc->sc_opencount = -1;
367
368	rc = sc->sc_channels;
369	for (i = 0; i < CD180_NCHAN; i++, rc++) {
370		destroy_dev(rc->rc_dev);
371		destroy_dev(rc->rc_cdev);
372	}
373
374	rc = sc->sc_channels;
375	s = splsoftclock();
376	for (i = 0; i < CD180_NCHAN; i++) {
377		if ((rc->rc_flags & RC_DTR_OFF) &&
378		    !callout_stop(&rc->rc_dtrcallout))
379			tsleep(&rc->rc_dtrwait, TTIPRI, "rcdtrdet", 0);
380	}
381
382	error = bus_teardown_intr(dev, sc->sc_irq, sc->sc_hwicookie);
383	if (error)
384		device_printf(dev, "failed to deregister interrupt handler\n");
385	ithread_remove_handler(sc->sc_swicookie);
386	rc_release_resources(dev);
387
388	return (0);
389}
390
391static void
392rc_release_resources(device_t dev)
393{
394	struct rc_softc *sc;
395	int i;
396
397	sc = device_get_softc(dev);
398	if (sc->sc_irq != NULL) {
399		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid,
400		    sc->sc_irq);
401		sc->sc_irq = NULL;
402	}
403	for (i = 0; i < IOBASE_ADDRS; i++) {
404		if (sc->sc_port[i] == NULL)
405			break;
406		bus_release_resource(dev, SYS_RES_IOPORT, i, sc->sc_port[i]);
407		sc->sc_port[i] = NULL;
408	}
409}
410
411/* RC interrupt handling */
412static void
413rc_intr(void *arg)
414{
415	struct rc_softc        *sc;
416	struct rc_chans        *rc;
417	int                    resid, chan;
418	u_char                 val, iack, bsr, ucnt, *optr;
419	int                    good_data, t_state;
420
421	sc = (struct rc_softc *)arg;
422	bsr = ~(rcin(sc, RC_BSR));
423	if (!(bsr & (RC_BSR_TOUT|RC_BSR_RXINT|RC_BSR_TXINT|RC_BSR_MOINT))) {
424		device_printf(sc->sc_dev, "extra interrupt\n");
425		rcout(sc, CD180_EOIR, 0);
426		return;
427	}
428
429	while (bsr & (RC_BSR_TOUT|RC_BSR_RXINT|RC_BSR_TXINT|RC_BSR_MOINT)) {
430#ifdef RCDEBUG_DETAILED
431		device_printf(sc->sc_dev, "intr (%p) %s%s%s%s\n", arg, bsr,
432			(bsr & RC_BSR_TOUT)?"TOUT ":"",
433			(bsr & RC_BSR_RXINT)?"RXINT ":"",
434			(bsr & RC_BSR_TXINT)?"TXINT ":"",
435			(bsr & RC_BSR_MOINT)?"MOINT":"");
436#endif
437		if (bsr & RC_BSR_TOUT) {
438			device_printf(sc->sc_dev,
439			    "hardware failure, reset board\n");
440			rcout(sc, RC_CTOUT, 0);
441			rc_reinit(sc);
442			return;
443		}
444		if (bsr & RC_BSR_RXINT) {
445			iack = rcin(sc, RC_PILR_RX);
446			good_data = (iack == (GIVR_IT_RGDI | RC_FAKEID));
447			if (!good_data && iack != (GIVR_IT_REI | RC_FAKEID)) {
448				device_printf(sc->sc_dev,
449				    "fake rxint: %02x\n", iack);
450				goto more_intrs;
451			}
452			chan = ((rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH);
453			rc = &sc->sc_channels[chan];
454			t_state = rc->rc_tp.t_state;
455			/* Do RTS flow control stuff */
456			if (  (rc->rc_flags & RC_RTSFLOW)
457			    || !(t_state & TS_ISOPEN)
458			   ) {
459				if (  (   !(t_state & TS_ISOPEN)
460				       || (t_state & TS_TBLOCK)
461				      )
462				    && (rc->rc_msvr & MSVR_RTS)
463				   )
464					rcout(sc, CD180_MSVR,
465						rc->rc_msvr &= ~MSVR_RTS);
466				else if (!(rc->rc_msvr & MSVR_RTS))
467					rcout(sc, CD180_MSVR,
468						rc->rc_msvr |= MSVR_RTS);
469			}
470			ucnt  = rcin(sc, CD180_RDCR) & 0xF;
471			resid = 0;
472
473			if (t_state & TS_ISOPEN) {
474				/* check for input buffer overflow */
475				if ((rc->rc_iptr + ucnt) >= rc->rc_bufend) {
476					resid  = ucnt;
477					ucnt   = rc->rc_bufend - rc->rc_iptr;
478					resid -= ucnt;
479					if (!(rc->rc_flags & RC_WAS_BUFOVFL)) {
480						rc->rc_flags |= RC_WAS_BUFOVFL;
481						sc->sc_scheduled_event++;
482					}
483				}
484				optr = rc->rc_iptr;
485				/* check foor good data */
486				if (good_data) {
487					while (ucnt-- > 0) {
488						val = rcin(sc, CD180_RDR);
489						optr[0] = val;
490						optr[INPUT_FLAGS_SHIFT] = 0;
491						optr++;
492						sc->sc_scheduled_event++;
493						if (val != 0 && val == rc->rc_hotchar)
494							swi_sched(sc->sc_swicookie, 0);
495					}
496				} else {
497					/* Store also status data */
498					while (ucnt-- > 0) {
499						iack = rcin(sc, CD180_RCSR);
500						if (iack & RCSR_Timeout)
501							break;
502						if (   (iack & RCSR_OE)
503						    && !(rc->rc_flags & RC_WAS_SILOVFL)) {
504							rc->rc_flags |= RC_WAS_SILOVFL;
505							sc->sc_scheduled_event++;
506						}
507						val = rcin(sc, CD180_RDR);
508						/*
509						  Don't store PE if IGNPAR and BREAK if IGNBRK,
510						  this hack allows "raw" tty optimization
511						  works even if IGN* is set.
512						*/
513						if (   !(iack & (RCSR_PE|RCSR_FE|RCSR_Break))
514						    || ((!(iack & (RCSR_PE|RCSR_FE))
515						    ||  !(rc->rc_tp.t_iflag & IGNPAR))
516						    && (!(iack & RCSR_Break)
517						    ||  !(rc->rc_tp.t_iflag & IGNBRK)))) {
518							if (   (iack & (RCSR_PE|RCSR_FE))
519							    && (t_state & TS_CAN_BYPASS_L_RINT)
520							    && ((iack & RCSR_FE)
521							    ||  ((iack & RCSR_PE)
522							    &&  (rc->rc_tp.t_iflag & INPCK))))
523								val = 0;
524							else if (val != 0 && val == rc->rc_hotchar)
525								swi_sched(sc->sc_swicookie, 0);
526							optr[0] = val;
527							optr[INPUT_FLAGS_SHIFT] = iack;
528							optr++;
529							sc->sc_scheduled_event++;
530						}
531					}
532				}
533				rc->rc_iptr = optr;
534				rc->rc_flags |= RC_DORXFER;
535			} else
536				resid = ucnt;
537			/* Clear FIFO if necessary */
538			while (resid-- > 0) {
539				if (!good_data)
540					iack = rcin(sc, CD180_RCSR);
541				else
542					iack = 0;
543				if (iack & RCSR_Timeout)
544					break;
545				(void) rcin(sc, CD180_RDR);
546			}
547			goto more_intrs;
548		}
549		if (bsr & RC_BSR_MOINT) {
550			iack = rcin(sc, RC_PILR_MODEM);
551			if (iack != (GIVR_IT_MSCI | RC_FAKEID)) {
552				device_printf(sc->sc_dev, "fake moint: %02x\n",
553				    iack);
554				goto more_intrs;
555			}
556			chan = ((rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH);
557			rc = &sc->sc_channels[chan];
558			iack = rcin(sc, CD180_MCR);
559			rc->rc_msvr = rcin(sc, CD180_MSVR);
560			rcout(sc, CD180_MCR, 0);
561#ifdef RCDEBUG
562			printrcflags(rc, "moint");
563#endif
564			if (rc->rc_flags & RC_CTSFLOW) {
565				if (rc->rc_msvr & MSVR_CTS)
566					rc->rc_flags |= RC_SEND_RDY;
567				else
568					rc->rc_flags &= ~RC_SEND_RDY;
569			} else
570				rc->rc_flags |= RC_SEND_RDY;
571			if ((iack & MCR_CDchg) && !(rc->rc_flags & RC_MODCHG)) {
572				sc->sc_scheduled_event += LOTS_OF_EVENTS;
573				rc->rc_flags |= RC_MODCHG;
574				swi_sched(sc->sc_swicookie, 0);
575			}
576			goto more_intrs;
577		}
578		if (bsr & RC_BSR_TXINT) {
579			iack = rcin(sc, RC_PILR_TX);
580			if (iack != (GIVR_IT_TDI | RC_FAKEID)) {
581				device_printf(sc->sc_dev, "fake txint: %02x\n",
582				    iack);
583				goto more_intrs;
584			}
585			chan = ((rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH);
586			rc = &sc->sc_channels[chan];
587			if (    (rc->rc_flags & RC_OSUSP)
588			    || !(rc->rc_flags & RC_SEND_RDY)
589			   )
590				goto more_intrs;
591			/* Handle breaks and other stuff */
592			if (rc->rc_pendcmd) {
593				rcout(sc, CD180_COR2, rc->rc_cor2 |= COR2_ETC);
594				rcout(sc, CD180_TDR,  CD180_C_ESC);
595				rcout(sc, CD180_TDR,  rc->rc_pendcmd);
596				rcout(sc, CD180_COR2, rc->rc_cor2 &= ~COR2_ETC);
597				rc->rc_pendcmd = 0;
598				goto more_intrs;
599			}
600			optr = rc->rc_optr;
601			resid = rc->rc_obufend - optr;
602			if (resid > CD180_NFIFO)
603				resid = CD180_NFIFO;
604			while (resid-- > 0)
605				rcout(sc, CD180_TDR, *optr++);
606			rc->rc_optr = optr;
607
608			/* output completed? */
609			if (optr >= rc->rc_obufend) {
610				rcout(sc, CD180_IER, rc->rc_ier &= ~IER_TxRdy);
611#ifdef RCDEBUG
612				device_printf(sc->sc_dev,
613				    "channel %d: output completed\n",
614				    rc->rc_chan);
615#endif
616				if (!(rc->rc_flags & RC_DOXXFER)) {
617					sc->sc_scheduled_event += LOTS_OF_EVENTS;
618					rc->rc_flags |= RC_DOXXFER;
619					swi_sched(sc->sc_swicookie, 0);
620				}
621			}
622		}
623	more_intrs:
624		rcout(sc, CD180_EOIR, 0);   /* end of interrupt */
625		rcout(sc, RC_CTOUT, 0);
626		bsr = ~(rcin(sc, RC_BSR));
627	}
628}
629
630/* Feed characters to output buffer */
631static void
632rc_start(struct tty *tp)
633{
634	struct rc_softc *sc;
635	struct rc_chans *rc;
636	int s;
637
638	rc = TTY_TO_RC(tp);
639	if (rc->rc_flags & RC_OSBUSY)
640		return;
641	sc = rc->rc_rcb;
642	s = spltty();
643	rc->rc_flags |= RC_OSBUSY;
644	critical_enter();
645	if (tp->t_state & TS_TTSTOP)
646		rc->rc_flags |= RC_OSUSP;
647	else
648		rc->rc_flags &= ~RC_OSUSP;
649	/* Do RTS flow control stuff */
650	if (   (rc->rc_flags & RC_RTSFLOW)
651	    && (tp->t_state & TS_TBLOCK)
652	    && (rc->rc_msvr & MSVR_RTS)
653	   ) {
654		rcout(sc, CD180_CAR, rc->rc_chan);
655		rcout(sc, CD180_MSVR, rc->rc_msvr &= ~MSVR_RTS);
656	} else if (!(rc->rc_msvr & MSVR_RTS)) {
657		rcout(sc, CD180_CAR, rc->rc_chan);
658		rcout(sc, CD180_MSVR, rc->rc_msvr |= MSVR_RTS);
659	}
660	critical_exit();
661	if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP))
662		goto out;
663#ifdef RCDEBUG
664	printrcflags(rc, "rcstart");
665#endif
666	ttwwakeup(tp);
667#ifdef RCDEBUG
668	printf("rcstart: outq = %d obuf = %d\n",
669		tp->t_outq.c_cc, rc->rc_obufend - rc->rc_optr);
670#endif
671	if (tp->t_state & TS_BUSY)
672		goto out;    /* output still in progress ... */
673
674	if (tp->t_outq.c_cc > 0) {
675		u_int   ocnt;
676
677		tp->t_state |= TS_BUSY;
678		ocnt = q_to_b(&tp->t_outq, rc->rc_obuf, sizeof rc->rc_obuf);
679		critical_enter();
680		rc->rc_optr = rc->rc_obuf;
681		rc->rc_obufend = rc->rc_optr + ocnt;
682		critical_exit();
683		if (!(rc->rc_ier & IER_TxRdy)) {
684#ifdef RCDEBUG
685			device_printf(sc->sc_dev,
686			    "channel %d: rcstart enable txint\n", rc->rc_chan);
687#endif
688			rcout(sc, CD180_CAR, rc->rc_chan);
689			rcout(sc, CD180_IER, rc->rc_ier |= IER_TxRdy);
690		}
691	}
692out:
693	rc->rc_flags &= ~RC_OSBUSY;
694	(void) splx(s);
695}
696
697/* Handle delayed events. */
698void
699rc_pollcard(void *arg)
700{
701	struct rc_softc *sc;
702	struct rc_chans *rc;
703	struct tty *tp;
704	u_char *tptr, *eptr;
705	int chan, icnt;
706
707	sc = (struct rc_softc *)arg;
708	if (sc->sc_scheduled_event == 0)
709		return;
710	do {
711		rc = sc->sc_channels;
712		for (chan = 0; chan < CD180_NCHAN; rc++, chan++) {
713			tp = &rc->rc_tp;
714#ifdef RCDEBUG
715			if (rc->rc_flags & (RC_DORXFER|RC_DOXXFER|RC_MODCHG|
716			    RC_WAS_BUFOVFL|RC_WAS_SILOVFL))
717				printrcflags(rc, "rcevent");
718#endif
719			if (rc->rc_flags & RC_WAS_BUFOVFL) {
720				critical_enter();
721				rc->rc_flags &= ~RC_WAS_BUFOVFL;
722				sc->sc_scheduled_event--;
723				critical_exit();
724				device_printf(sc->sc_dev,
725			    "channel %d: interrupt-level buffer overflow\n",
726				     chan);
727			}
728			if (rc->rc_flags & RC_WAS_SILOVFL) {
729				critical_enter();
730				rc->rc_flags &= ~RC_WAS_SILOVFL;
731				sc->sc_scheduled_event--;
732				critical_exit();
733				device_printf(sc->sc_dev,
734				    "channel %d: silo overflow\n", chan);
735			}
736			if (rc->rc_flags & RC_MODCHG) {
737				critical_enter();
738				rc->rc_flags &= ~RC_MODCHG;
739				sc->sc_scheduled_event -= LOTS_OF_EVENTS;
740				critical_exit();
741				(*linesw[tp->t_line].l_modem)(tp, !!(rc->rc_msvr & MSVR_CD));
742			}
743			if (rc->rc_flags & RC_DORXFER) {
744				critical_enter();
745				rc->rc_flags &= ~RC_DORXFER;
746				eptr = rc->rc_iptr;
747				if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE])
748					tptr = &rc->rc_ibuf[RC_IBUFSIZE];
749				else
750					tptr = rc->rc_ibuf;
751				icnt = eptr - tptr;
752				if (icnt > 0) {
753					if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE]) {
754						rc->rc_iptr   = rc->rc_ibuf;
755						rc->rc_bufend = &rc->rc_ibuf[RC_IBUFSIZE];
756						rc->rc_hiwat  = &rc->rc_ibuf[RC_IHIGHWATER];
757					} else {
758						rc->rc_iptr   = &rc->rc_ibuf[RC_IBUFSIZE];
759						rc->rc_bufend = &rc->rc_ibuf[2 * RC_IBUFSIZE];
760						rc->rc_hiwat  =
761							&rc->rc_ibuf[RC_IBUFSIZE + RC_IHIGHWATER];
762					}
763					if (   (rc->rc_flags & RC_RTSFLOW)
764					    && (tp->t_state & TS_ISOPEN)
765					    && !(tp->t_state & TS_TBLOCK)
766					    && !(rc->rc_msvr & MSVR_RTS)
767					    ) {
768						rcout(sc, CD180_CAR, chan);
769						rcout(sc, CD180_MSVR,
770							rc->rc_msvr |= MSVR_RTS);
771					}
772					sc->sc_scheduled_event -= icnt;
773				}
774				critical_exit();
775
776				if (icnt <= 0 || !(tp->t_state & TS_ISOPEN))
777					goto done1;
778
779				if (   (tp->t_state & TS_CAN_BYPASS_L_RINT)
780				    && !(tp->t_state & TS_LOCAL)) {
781					if ((tp->t_rawq.c_cc + icnt) >= RB_I_HIGH_WATER
782					    && ((rc->rc_flags & RC_RTSFLOW) || (tp->t_iflag & IXOFF))
783					    && !(tp->t_state & TS_TBLOCK))
784						ttyblock(tp);
785					tk_nin += icnt;
786					tk_rawcc += icnt;
787					tp->t_rawcc += icnt;
788					if (b_to_q(tptr, icnt, &tp->t_rawq))
789						device_printf(sc->sc_dev,
790				    "channel %d: tty-level buffer overflow\n",
791						    chan);
792					ttwakeup(tp);
793					if ((tp->t_state & TS_TTSTOP) && ((tp->t_iflag & IXANY)
794					    || (tp->t_cc[VSTART] == tp->t_cc[VSTOP]))) {
795						tp->t_state &= ~TS_TTSTOP;
796						tp->t_lflag &= ~FLUSHO;
797						rc_start(tp);
798					}
799				} else {
800					for (; tptr < eptr; tptr++)
801						(*linesw[tp->t_line].l_rint)
802						    (tptr[0] |
803						    rc_rcsrt[tptr[INPUT_FLAGS_SHIFT] & 0xF], tp);
804				}
805done1: ;
806			}
807			if (rc->rc_flags & RC_DOXXFER) {
808				critical_enter();
809				sc->sc_scheduled_event -= LOTS_OF_EVENTS;
810				rc->rc_flags &= ~RC_DOXXFER;
811				rc->rc_tp.t_state &= ~TS_BUSY;
812				critical_exit();
813				(*linesw[tp->t_line].l_start)(tp);
814			}
815			if (sc->sc_scheduled_event == 0)
816				break;
817		}
818	} while (sc->sc_scheduled_event >= LOTS_OF_EVENTS);
819}
820
821static void
822rc_stop(struct tty *tp, int rw)
823{
824	struct rc_softc *sc;
825	struct rc_chans *rc;
826	u_char *tptr, *eptr;
827
828	rc = TTY_TO_RC(tp);
829	sc = rc->rc_rcb;
830#ifdef RCDEBUG
831	device_printf(sc->sc_dev, "channel %d: rc_stop %s%s\n",
832	    rc->rc_chan, (rw & FWRITE)?"FWRITE ":"", (rw & FREAD)?"FREAD":"");
833#endif
834	if (rw & FWRITE)
835		rc_discard_output(rc);
836	critical_enter();
837	if (rw & FREAD) {
838		rc->rc_flags &= ~RC_DORXFER;
839		eptr = rc->rc_iptr;
840		if (rc->rc_bufend == &rc->rc_ibuf[2 * RC_IBUFSIZE]) {
841			tptr = &rc->rc_ibuf[RC_IBUFSIZE];
842			rc->rc_iptr = &rc->rc_ibuf[RC_IBUFSIZE];
843		} else {
844			tptr = rc->rc_ibuf;
845			rc->rc_iptr = rc->rc_ibuf;
846		}
847		sc->sc_scheduled_event -= eptr - tptr;
848	}
849	if (tp->t_state & TS_TTSTOP)
850		rc->rc_flags |= RC_OSUSP;
851	else
852		rc->rc_flags &= ~RC_OSUSP;
853	critical_exit();
854}
855
856static int
857rcopen(dev_t dev, int flag, int mode, d_thread_t *td)
858{
859	struct rc_softc *sc;
860	struct rc_chans *rc;
861	struct tty *tp;
862	int s, error = 0;
863
864	rc = DEV_TO_RC(dev);
865	sc = rc->rc_rcb;
866	tp = &rc->rc_tp;
867	if (sc->sc_opencount < 0)
868		return (ENXIO);
869	sc->sc_opencount++;
870#ifdef RCDEBUG
871	device_printf(sc->sc_dev, "channel %d: rcopen: dev %p\n",
872	    rc->rc_chan, dev);
873#endif
874	s = spltty();
875
876again:
877	while (rc->rc_flags & RC_DTR_OFF) {
878		error = tsleep(&(rc->rc_dtrwait), TTIPRI | PCATCH, "rcdtr", 0);
879		if (error != 0)
880			goto out;
881	}
882	if (tp->t_state & TS_ISOPEN) {
883		if (CALLOUT(dev)) {
884			if (!(rc->rc_flags & RC_ACTOUT)) {
885				error = EBUSY;
886				goto out;
887			}
888		} else {
889			if (rc->rc_flags & RC_ACTOUT) {
890				if (flag & O_NONBLOCK) {
891					error = EBUSY;
892					goto out;
893				}
894				error = tsleep(&rc->rc_rcb,
895				     TTIPRI|PCATCH, "rcbi", 0);
896				if (error)
897					goto out;
898				goto again;
899			}
900		}
901		if (tp->t_state & TS_XCLUDE &&
902		    suser(td)) {
903			error = EBUSY;
904			goto out;
905		}
906	} else {
907		tp->t_oproc   = rc_start;
908		tp->t_param   = rc_param;
909		tp->t_stop    = rc_stop;
910		tp->t_dev     = dev;
911
912		if (CALLOUT(dev))
913			tp->t_cflag |= CLOCAL;
914		else
915			tp->t_cflag &= ~CLOCAL;
916
917		error = rc_param(tp, &tp->t_termios);
918		if (error)
919			goto out;
920		(void) rc_modctl(rc, TIOCM_RTS|TIOCM_DTR, DMSET);
921
922		if ((rc->rc_msvr & MSVR_CD) || CALLOUT(dev))
923			(*linesw[tp->t_line].l_modem)(tp, 1);
924	}
925	if (!(tp->t_state & TS_CARR_ON) && !CALLOUT(dev)
926	    && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
927		rc->rc_dcdwaits++;
928		error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "rcdcd", 0);
929		rc->rc_dcdwaits--;
930		if (error != 0)
931			goto out;
932		goto again;
933	}
934	error = (*linesw[tp->t_line].l_open)(dev, tp);
935	disc_optim(tp, &tp->t_termios, rc);
936	if ((tp->t_state & TS_ISOPEN) && CALLOUT(dev))
937		rc->rc_flags |= RC_ACTOUT;
938out:
939	(void) splx(s);
940
941	if(rc->rc_dcdwaits == 0 && !(tp->t_state & TS_ISOPEN))
942		rc_hardclose(rc);
943
944	return error;
945}
946
947static int
948rcclose(dev_t dev, int flag, int mode, d_thread_t *td)
949{
950	struct rc_softc *sc;
951	struct rc_chans *rc;
952	struct tty *tp;
953	int  s;
954
955	rc = DEV_TO_RC(dev);
956	sc = rc->rc_rcb;
957	tp = &rc->rc_tp;
958#ifdef RCDEBUG
959	device_printf(sc->sc_dev, "channel %d: rcclose dev %p\n",
960	    rc->rc_chan, dev);
961#endif
962	s = spltty();
963	(*linesw[tp->t_line].l_close)(tp, flag);
964	disc_optim(tp, &tp->t_termios, rc);
965	rc_stop(tp, FREAD | FWRITE);
966	rc_hardclose(rc);
967	ttyclose(tp);
968	splx(s);
969	KASSERT(sc->sc_opencount > 0, ("rcclose: non-positive open count"));
970	sc->sc_opencount--;
971	return 0;
972}
973
974static void
975rc_hardclose(struct rc_chans *rc)
976{
977	struct rc_softc *sc;
978	struct tty *tp;
979	int s;
980
981	tp = &rc->rc_tp;
982	sc = rc->rc_rcb;
983	s = spltty();
984	rcout(sc, CD180_CAR, rc->rc_chan);
985
986	/* Disable rx/tx intrs */
987	rcout(sc, CD180_IER, rc->rc_ier = 0);
988	if (   (tp->t_cflag & HUPCL)
989	    || (!(rc->rc_flags & RC_ACTOUT)
990	       && !(rc->rc_msvr & MSVR_CD)
991	       && !(tp->t_cflag & CLOCAL))
992	    || !(tp->t_state & TS_ISOPEN)
993	   ) {
994		CCRCMD(sc, rc->rc_chan, CCR_ResetChan);
995		WAITFORCCR(sc, rc->rc_chan);
996		(void) rc_modctl(rc, TIOCM_RTS, DMSET);
997		if (rc->rc_dtrwait) {
998			callout_reset(&rc->rc_dtrcallout, rc->rc_dtrwait,
999			    rc_dtrwakeup, rc);
1000			rc->rc_flags |= RC_DTR_OFF;
1001		}
1002	}
1003	rc->rc_flags &= ~RC_ACTOUT;
1004	wakeup( &rc->rc_rcb);  /* wake bi */
1005	wakeup(TSA_CARR_ON(tp));
1006	(void) splx(s);
1007}
1008
1009/* Reset the bastard */
1010static void
1011rc_hwreset(struct rc_softc *sc, u_int chipid)
1012{
1013	CCRCMD(sc, -1, CCR_HWRESET);            /* Hardware reset */
1014	DELAY(20000);
1015	WAITFORCCR(sc, -1);
1016
1017	rcout(sc, RC_CTOUT, 0);             /* Clear timeout  */
1018	rcout(sc, CD180_GIVR,  chipid);
1019	rcout(sc, CD180_GICR,  0);
1020
1021	/* Set Prescaler Registers (1 msec) */
1022	rcout(sc, CD180_PPRL, ((RC_OSCFREQ + 999) / 1000) & 0xFF);
1023	rcout(sc, CD180_PPRH, ((RC_OSCFREQ + 999) / 1000) >> 8);
1024
1025	/* Initialize Priority Interrupt Level Registers */
1026	rcout(sc, CD180_PILR1, RC_PILR_MODEM);
1027	rcout(sc, CD180_PILR2, RC_PILR_TX);
1028	rcout(sc, CD180_PILR3, RC_PILR_RX);
1029
1030	/* Reset DTR */
1031	rcout(sc, RC_DTREG, ~0);
1032}
1033
1034/* Set channel parameters */
1035static int
1036rc_param(struct tty *tp, struct termios *ts)
1037{
1038	struct rc_softc *sc;
1039	struct rc_chans *rc;
1040	int idivs, odivs, s, val, cflag, iflag, lflag, inpflow;
1041
1042	if (   ts->c_ospeed < 0 || ts->c_ospeed > 76800
1043	    || ts->c_ispeed < 0 || ts->c_ispeed > 76800
1044	   )
1045		return (EINVAL);
1046	if (ts->c_ispeed == 0)
1047		ts->c_ispeed = ts->c_ospeed;
1048	odivs = RC_BRD(ts->c_ospeed);
1049	idivs = RC_BRD(ts->c_ispeed);
1050
1051	rc = TTY_TO_RC(tp);
1052	sc = rc->rc_rcb;
1053	s = spltty();
1054
1055	/* Select channel */
1056	rcout(sc, CD180_CAR, rc->rc_chan);
1057
1058	/* If speed == 0, hangup line */
1059	if (ts->c_ospeed == 0) {
1060		CCRCMD(sc, rc->rc_chan, CCR_ResetChan);
1061		WAITFORCCR(sc, rc->rc_chan);
1062		(void) rc_modctl(rc, TIOCM_DTR, DMBIC);
1063	}
1064
1065	tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1066	cflag = ts->c_cflag;
1067	iflag = ts->c_iflag;
1068	lflag = ts->c_lflag;
1069
1070	if (idivs > 0) {
1071		rcout(sc, CD180_RBPRL, idivs & 0xFF);
1072		rcout(sc, CD180_RBPRH, idivs >> 8);
1073	}
1074	if (odivs > 0) {
1075		rcout(sc, CD180_TBPRL, odivs & 0xFF);
1076		rcout(sc, CD180_TBPRH, odivs >> 8);
1077	}
1078
1079	/* set timeout value */
1080	if (ts->c_ispeed > 0) {
1081		int itm = ts->c_ispeed > 2400 ? 5 : 10000 / ts->c_ispeed + 1;
1082
1083		if (   !(lflag & ICANON)
1084		    && ts->c_cc[VMIN] != 0 && ts->c_cc[VTIME] != 0
1085		    && ts->c_cc[VTIME] * 10 > itm)
1086			itm = ts->c_cc[VTIME] * 10;
1087
1088		rcout(sc, CD180_RTPR, itm <= 255 ? itm : 255);
1089	}
1090
1091	switch (cflag & CSIZE) {
1092		case CS5:       val = COR1_5BITS;      break;
1093		case CS6:       val = COR1_6BITS;      break;
1094		case CS7:       val = COR1_7BITS;      break;
1095		default:
1096		case CS8:       val = COR1_8BITS;      break;
1097	}
1098	if (cflag & PARENB) {
1099		val |= COR1_NORMPAR;
1100		if (cflag & PARODD)
1101			val |= COR1_ODDP;
1102		if (!(cflag & INPCK))
1103			val |= COR1_Ignore;
1104	} else
1105		val |= COR1_Ignore;
1106	if (cflag & CSTOPB)
1107		val |= COR1_2SB;
1108	rcout(sc, CD180_COR1, val);
1109
1110	/* Set FIFO threshold */
1111	val = ts->c_ospeed <= 4800 ? 1 : CD180_NFIFO / 2;
1112	inpflow = 0;
1113	if (   (iflag & IXOFF)
1114	    && (   ts->c_cc[VSTOP] != _POSIX_VDISABLE
1115		&& (   ts->c_cc[VSTART] != _POSIX_VDISABLE
1116		    || (iflag & IXANY)
1117		   )
1118	       )
1119	   ) {
1120		inpflow = 1;
1121		val |= COR3_SCDE|COR3_FCT;
1122	}
1123	rcout(sc, CD180_COR3, val);
1124
1125	/* Initialize on-chip automatic flow control */
1126	val = 0;
1127	rc->rc_flags &= ~(RC_CTSFLOW|RC_SEND_RDY);
1128	if (cflag & CCTS_OFLOW) {
1129		rc->rc_flags |= RC_CTSFLOW;
1130		val |= COR2_CtsAE;
1131	} else
1132		rc->rc_flags |= RC_SEND_RDY;
1133	if (tp->t_state & TS_TTSTOP)
1134		rc->rc_flags |= RC_OSUSP;
1135	else
1136		rc->rc_flags &= ~RC_OSUSP;
1137	if (cflag & CRTS_IFLOW)
1138		rc->rc_flags |= RC_RTSFLOW;
1139	else
1140		rc->rc_flags &= ~RC_RTSFLOW;
1141
1142	if (inpflow) {
1143		if (ts->c_cc[VSTART] != _POSIX_VDISABLE)
1144			rcout(sc, CD180_SCHR1, ts->c_cc[VSTART]);
1145		rcout(sc, CD180_SCHR2, ts->c_cc[VSTOP]);
1146		val |= COR2_TxIBE;
1147		if (iflag & IXANY)
1148			val |= COR2_IXM;
1149	}
1150
1151	rcout(sc, CD180_COR2, rc->rc_cor2 = val);
1152
1153	CCRCMD(sc, rc->rc_chan, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
1154
1155	disc_optim(tp, ts, rc);
1156
1157	/* modem ctl */
1158	val = cflag & CLOCAL ? 0 : MCOR1_CDzd;
1159	if (cflag & CCTS_OFLOW)
1160		val |= MCOR1_CTSzd;
1161	rcout(sc, CD180_MCOR1, val);
1162
1163	val = cflag & CLOCAL ? 0 : MCOR2_CDod;
1164	if (cflag & CCTS_OFLOW)
1165		val |= MCOR2_CTSod;
1166	rcout(sc, CD180_MCOR2, val);
1167
1168	/* enable i/o and interrupts */
1169	CCRCMD(sc, rc->rc_chan,
1170		CCR_XMTREN | ((cflag & CREAD) ? CCR_RCVREN : CCR_RCVRDIS));
1171	WAITFORCCR(sc, rc->rc_chan);
1172
1173	rc->rc_ier = cflag & CLOCAL ? 0 : IER_CD;
1174	if (cflag & CCTS_OFLOW)
1175		rc->rc_ier |= IER_CTS;
1176	if (cflag & CREAD)
1177		rc->rc_ier |= IER_RxData;
1178	if (tp->t_state & TS_BUSY)
1179		rc->rc_ier |= IER_TxRdy;
1180	if (ts->c_ospeed != 0)
1181		rc_modctl(rc, TIOCM_DTR, DMBIS);
1182	if ((cflag & CCTS_OFLOW) && (rc->rc_msvr & MSVR_CTS))
1183		rc->rc_flags |= RC_SEND_RDY;
1184	rcout(sc, CD180_IER, rc->rc_ier);
1185	(void) splx(s);
1186	return 0;
1187}
1188
1189/* Re-initialize board after bogus interrupts */
1190static void
1191rc_reinit(struct rc_softc *sc)
1192{
1193	struct rc_chans *rc;
1194	int i;
1195
1196	rc_hwreset(sc, RC_FAKEID);
1197	rc = sc->sc_channels;
1198	for (i = 0; i < CD180_NCHAN; i++, rc++)
1199		(void) rc_param(&rc->rc_tp, &rc->rc_tp.t_termios);
1200}
1201
1202static int
1203rcioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td)
1204{
1205	struct rc_chans *rc;
1206	struct tty *tp;
1207	int s, error;
1208
1209	rc = DEV_TO_RC(dev);
1210	tp = &rc->rc_tp;
1211	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
1212	if (error != ENOIOCTL)
1213		return (error);
1214	error = ttioctl(tp, cmd, data, flag);
1215	disc_optim(tp, &tp->t_termios, rc);
1216	if (error != ENOIOCTL)
1217		return (error);
1218	s = spltty();
1219
1220	switch (cmd) {
1221	    case TIOCSBRK:
1222		rc->rc_pendcmd = CD180_C_SBRK;
1223		break;
1224
1225	    case TIOCCBRK:
1226		rc->rc_pendcmd = CD180_C_EBRK;
1227		break;
1228
1229	    case TIOCSDTR:
1230		(void) rc_modctl(rc, TIOCM_DTR, DMBIS);
1231		break;
1232
1233	    case TIOCCDTR:
1234		(void) rc_modctl(rc, TIOCM_DTR, DMBIC);
1235		break;
1236
1237	    case TIOCMGET:
1238		*(int *) data = rc_modctl(rc, 0, DMGET);
1239		break;
1240
1241	    case TIOCMSET:
1242		(void) rc_modctl(rc, *(int *) data, DMSET);
1243		break;
1244
1245	    case TIOCMBIC:
1246		(void) rc_modctl(rc, *(int *) data, DMBIC);
1247		break;
1248
1249	    case TIOCMBIS:
1250		(void) rc_modctl(rc, *(int *) data, DMBIS);
1251		break;
1252
1253	    case TIOCMSDTRWAIT:
1254		error = suser(td);
1255		if (error != 0) {
1256			splx(s);
1257			return (error);
1258		}
1259		rc->rc_dtrwait = *(int *)data * hz / 100;
1260		break;
1261
1262	    case TIOCMGDTRWAIT:
1263		*(int *)data = rc->rc_dtrwait * 100 / hz;
1264		break;
1265
1266	    default:
1267		(void) splx(s);
1268		return ENOTTY;
1269	}
1270	(void) splx(s);
1271	return 0;
1272}
1273
1274
1275/* Modem control routines */
1276
1277static int
1278rc_modctl(struct rc_chans *rc, int bits, int cmd)
1279{
1280	struct rc_softc *sc;
1281	u_char *dtr;
1282	u_char msvr;
1283
1284	sc = rc->rc_rcb;
1285	dtr = &sc->sc_dtr;
1286	rcout(sc, CD180_CAR, rc->rc_chan);
1287
1288	switch (cmd) {
1289	    case DMSET:
1290		rcout(sc, RC_DTREG, (bits & TIOCM_DTR) ?
1291				~(*dtr |= 1 << rc->rc_chan) :
1292				~(*dtr &= ~(1 << rc->rc_chan)));
1293		msvr = rcin(sc, CD180_MSVR);
1294		if (bits & TIOCM_RTS)
1295			msvr |= MSVR_RTS;
1296		else
1297			msvr &= ~MSVR_RTS;
1298		if (bits & TIOCM_DTR)
1299			msvr |= MSVR_DTR;
1300		else
1301			msvr &= ~MSVR_DTR;
1302		rcout(sc, CD180_MSVR, msvr);
1303		break;
1304
1305	    case DMBIS:
1306		if (bits & TIOCM_DTR)
1307			rcout(sc, RC_DTREG, ~(*dtr |= 1 << rc->rc_chan));
1308		msvr = rcin(sc, CD180_MSVR);
1309		if (bits & TIOCM_RTS)
1310			msvr |= MSVR_RTS;
1311		if (bits & TIOCM_DTR)
1312			msvr |= MSVR_DTR;
1313		rcout(sc, CD180_MSVR, msvr);
1314		break;
1315
1316	    case DMGET:
1317		bits = TIOCM_LE;
1318		msvr = rc->rc_msvr = rcin(sc, CD180_MSVR);
1319
1320		if (msvr & MSVR_RTS)
1321			bits |= TIOCM_RTS;
1322		if (msvr & MSVR_CTS)
1323			bits |= TIOCM_CTS;
1324		if (msvr & MSVR_DSR)
1325			bits |= TIOCM_DSR;
1326		if (msvr & MSVR_DTR)
1327			bits |= TIOCM_DTR;
1328		if (msvr & MSVR_CD)
1329			bits |= TIOCM_CD;
1330		if (~rcin(sc, RC_RIREG) & (1 << rc->rc_chan))
1331			bits |= TIOCM_RI;
1332		return bits;
1333
1334	    case DMBIC:
1335		if (bits & TIOCM_DTR)
1336			rcout(sc, RC_DTREG, ~(*dtr &= ~(1 << rc->rc_chan)));
1337		msvr = rcin(sc, CD180_MSVR);
1338		if (bits & TIOCM_RTS)
1339			msvr &= ~MSVR_RTS;
1340		if (bits & TIOCM_DTR)
1341			msvr &= ~MSVR_DTR;
1342		rcout(sc, CD180_MSVR, msvr);
1343		break;
1344	}
1345	rc->rc_msvr = rcin(sc, CD180_MSVR);
1346	return 0;
1347}
1348
1349#define ERR(s) do {							\
1350	device_printf(sc->sc_dev, "%s", "");				\
1351	printf s ;							\
1352	printf("\n");							\
1353	(void) splx(old_level);						\
1354	return 1;							\
1355} while (0)
1356
1357/* Test the board. */
1358int
1359rc_test(struct rc_softc *sc)
1360{
1361	int     chan = 0;
1362	int     i = 0, rcnt, old_level;
1363	unsigned int    iack, chipid;
1364	unsigned short  divs;
1365	static  u_char  ctest[] = "\377\125\252\045\244\0\377";
1366#define CTLEN   8
1367
1368	struct rtest {
1369		u_char  txbuf[CD180_NFIFO];     /* TX buffer  */
1370		u_char  rxbuf[CD180_NFIFO];     /* RX buffer  */
1371		int     rxptr;                  /* RX pointer */
1372		int     txptr;                  /* TX pointer */
1373	} tchans[CD180_NCHAN];
1374
1375	old_level = spltty();
1376
1377	chipid = RC_FAKEID;
1378
1379	/* First, reset board to inital state */
1380	rc_hwreset(sc, chipid);
1381
1382	divs = RC_BRD(19200);
1383
1384	/* Initialize channels */
1385	for (chan = 0; chan < CD180_NCHAN; chan++) {
1386
1387		/* Select and reset channel */
1388		rcout(sc, CD180_CAR, chan);
1389		CCRCMD(sc, chan, CCR_ResetChan);
1390		WAITFORCCR(sc, chan);
1391
1392		/* Set speed */
1393		rcout(sc, CD180_RBPRL, divs & 0xFF);
1394		rcout(sc, CD180_RBPRH, divs >> 8);
1395		rcout(sc, CD180_TBPRL, divs & 0xFF);
1396		rcout(sc, CD180_TBPRH, divs >> 8);
1397
1398		/* set timeout value */
1399		rcout(sc, CD180_RTPR,  0);
1400
1401		/* Establish local loopback */
1402		rcout(sc, CD180_COR1, COR1_NOPAR | COR1_8BITS | COR1_1SB);
1403		rcout(sc, CD180_COR2, COR2_LLM);
1404		rcout(sc, CD180_COR3, CD180_NFIFO);
1405		CCRCMD(sc, chan, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
1406		CCRCMD(sc, chan, CCR_RCVREN | CCR_XMTREN);
1407		WAITFORCCR(sc, chan);
1408		rcout(sc, CD180_MSVR, MSVR_RTS);
1409
1410		/* Fill TXBUF with test data */
1411		for (i = 0; i < CD180_NFIFO; i++) {
1412			tchans[chan].txbuf[i] = ctest[i];
1413			tchans[chan].rxbuf[i] = 0;
1414		}
1415		tchans[chan].txptr = tchans[chan].rxptr = 0;
1416
1417		/* Now, start transmit */
1418		rcout(sc, CD180_IER, IER_TxMpty|IER_RxData);
1419	}
1420	/* Pseudo-interrupt poll stuff */
1421	for (rcnt = 10000; rcnt-- > 0; rcnt--) {
1422		i = ~(rcin(sc, RC_BSR));
1423		if (i & RC_BSR_TOUT)
1424			ERR(("BSR timeout bit set\n"));
1425		else if (i & RC_BSR_TXINT) {
1426			iack = rcin(sc, RC_PILR_TX);
1427			if (iack != (GIVR_IT_TDI | chipid))
1428				ERR(("Bad TX intr ack (%02x != %02x)\n",
1429					iack, GIVR_IT_TDI | chipid));
1430			chan = (rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH;
1431			/* If no more data to transmit, disable TX intr */
1432			if (tchans[chan].txptr >= CD180_NFIFO) {
1433				iack = rcin(sc, CD180_IER);
1434				rcout(sc, CD180_IER, iack & ~IER_TxMpty);
1435			} else {
1436				for (iack = tchans[chan].txptr;
1437				    iack < CD180_NFIFO; iack++)
1438					rcout(sc, CD180_TDR,
1439					    tchans[chan].txbuf[iack]);
1440				tchans[chan].txptr = iack;
1441			}
1442			rcout(sc, CD180_EOIR, 0);
1443		} else if (i & RC_BSR_RXINT) {
1444			u_char ucnt;
1445
1446			iack = rcin(sc, RC_PILR_RX);
1447			if (iack != (GIVR_IT_RGDI | chipid) &&
1448			    iack != (GIVR_IT_REI  | chipid))
1449				ERR(("Bad RX intr ack (%02x != %02x)\n",
1450					iack, GIVR_IT_RGDI | chipid));
1451			chan = (rcin(sc, CD180_GICR) & GICR_CHAN) >> GICR_LSH;
1452			ucnt = rcin(sc, CD180_RDCR) & 0xF;
1453			while (ucnt-- > 0) {
1454				iack = rcin(sc, CD180_RCSR);
1455				if (iack & RCSR_Timeout)
1456					break;
1457				if (iack & 0xF)
1458					ERR(("Bad char chan %d (RCSR = %02X)\n",
1459					    chan, iack));
1460				if (tchans[chan].rxptr > CD180_NFIFO)
1461					ERR(("Got extra chars chan %d\n",
1462					    chan));
1463				tchans[chan].rxbuf[tchans[chan].rxptr++] =
1464					rcin(sc, CD180_RDR);
1465			}
1466			rcout(sc, CD180_EOIR, 0);
1467		}
1468		rcout(sc, RC_CTOUT, 0);
1469		for (iack = chan = 0; chan < CD180_NCHAN; chan++)
1470			if (tchans[chan].rxptr >= CD180_NFIFO)
1471				iack++;
1472		if (iack == CD180_NCHAN)
1473			break;
1474	}
1475	for (chan = 0; chan < CD180_NCHAN; chan++) {
1476		/* Select and reset channel */
1477		rcout(sc, CD180_CAR, chan);
1478		CCRCMD(sc, chan, CCR_ResetChan);
1479	}
1480
1481	if (!rcnt)
1482		ERR(("looses characters during local loopback\n"));
1483	/* Now, check data */
1484	for (chan = 0; chan < CD180_NCHAN; chan++)
1485		for (i = 0; i < CD180_NFIFO; i++)
1486			if (ctest[i] != tchans[chan].rxbuf[i])
1487				ERR(("data mismatch chan %d ptr %d (%d != %d)\n",
1488				    chan, i, ctest[i], tchans[chan].rxbuf[i]));
1489	(void) splx(old_level);
1490	return 0;
1491}
1492
1493#ifdef RCDEBUG
1494static void
1495printrcflags(struct rc_chans *rc, char *comment)
1496{
1497	struct rc_softc *sc;
1498	u_short f = rc->rc_flags;
1499
1500	sc = rc->rc_rcb;
1501	printf("rc%d/%d: %s flags: %s%s%s%s%s%s%s%s%s%s%s%s\n",
1502		rc->rc_rcb->rcb_unit, rc->rc_chan, comment,
1503		(f & RC_DTR_OFF)?"DTR_OFF " :"",
1504		(f & RC_ACTOUT) ?"ACTOUT " :"",
1505		(f & RC_RTSFLOW)?"RTSFLOW " :"",
1506		(f & RC_CTSFLOW)?"CTSFLOW " :"",
1507		(f & RC_DORXFER)?"DORXFER " :"",
1508		(f & RC_DOXXFER)?"DOXXFER " :"",
1509		(f & RC_MODCHG) ?"MODCHG "  :"",
1510		(f & RC_OSUSP)  ?"OSUSP " :"",
1511		(f & RC_OSBUSY) ?"OSBUSY " :"",
1512		(f & RC_WAS_BUFOVFL) ?"BUFOVFL " :"",
1513		(f & RC_WAS_SILOVFL) ?"SILOVFL " :"",
1514		(f & RC_SEND_RDY) ?"SEND_RDY":"");
1515
1516	rcout(sc, CD180_CAR, rc->rc_chan);
1517
1518	printf("rc%d/%d: msvr %02x ier %02x ccsr %02x\n",
1519		rc->rc_rcb->rcb_unit, rc->rc_chan,
1520		rcin(sc, CD180_MSVR),
1521		rcin(sc, CD180_IER),
1522		rcin(sc, CD180_CCSR));
1523}
1524#endif /* RCDEBUG */
1525
1526static void
1527rc_dtrwakeup(void *arg)
1528{
1529	struct rc_chans  *rc;
1530
1531	rc = (struct rc_chans *)arg;
1532	rc->rc_flags &= ~RC_DTR_OFF;
1533	wakeup(&rc->rc_dtrwait);
1534}
1535
1536static void
1537rc_discard_output(struct rc_chans *rc)
1538{
1539	critical_enter();
1540	if (rc->rc_flags & RC_DOXXFER) {
1541		rc->rc_rcb->sc_scheduled_event -= LOTS_OF_EVENTS;
1542		rc->rc_flags &= ~RC_DOXXFER;
1543	}
1544	rc->rc_optr = rc->rc_obufend;
1545	rc->rc_tp.t_state &= ~TS_BUSY;
1546	critical_exit();
1547	ttwwakeup(&rc->rc_tp);
1548}
1549
1550static void
1551disc_optim(struct tty *tp, struct termios *t, struct rc_chans *rc)
1552{
1553
1554	if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
1555	    && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
1556	    && (!(t->c_iflag & PARMRK)
1557		|| (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
1558	    && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
1559	    && linesw[tp->t_line].l_rint == ttyinput)
1560		tp->t_state |= TS_CAN_BYPASS_L_RINT;
1561	else
1562		tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
1563	rc->rc_hotchar = linesw[tp->t_line].l_hotchar;
1564}
1565
1566static void
1567rc_wait0(struct rc_softc *sc, int chan, int line)
1568{
1569	int rcnt;
1570
1571	for (rcnt = 50; rcnt && rcin(sc, CD180_CCR); rcnt--)
1572		DELAY(30);
1573	if (rcnt == 0)
1574		device_printf(sc->sc_dev,
1575		    "channel %d command timeout, rc.c line: %d\n", chan, line);
1576}
1577
1578static device_method_t rc_methods[] = {
1579	/* Device interface */
1580	DEVMETHOD(device_probe,		rc_probe),
1581	DEVMETHOD(device_attach,	rc_attach),
1582	DEVMETHOD(device_detach,	rc_detach),
1583	{ 0, 0 }
1584};
1585
1586static driver_t rc_driver = {
1587	"rc",
1588	rc_methods, sizeof(struct rc_softc),
1589};
1590
1591DRIVER_MODULE(rc, isa, rc_driver, rc_devclass, 0, 0);
1592