xref: /netbsd-src/sys/arch/amiga/dev/mfc.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: mfc.c,v 1.40 2006/05/14 21:55:09 elad Exp $ */
2 
3 /*
4  * Copyright (c) 1982, 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "opt_kgdb.h"
33 
34 /*
35  * Copyright (c) 1994 Michael L. Hitch
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
50  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
55  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  */
57 
58 #include "opt_kgdb.h"
59 
60 #include <sys/cdefs.h>
61 __KERNEL_RCSID(0, "$NetBSD: mfc.c,v 1.40 2006/05/14 21:55:09 elad Exp $");
62 
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/device.h>
67 #include <sys/tty.h>
68 #include <sys/proc.h>
69 #include <sys/file.h>
70 #include <sys/malloc.h>
71 #include <sys/uio.h>
72 #include <sys/kernel.h>
73 #include <sys/syslog.h>
74 #include <sys/queue.h>
75 #include <sys/conf.h>
76 #include <sys/kauth.h>
77 #include <machine/cpu.h>
78 #include <amiga/amiga/device.h>
79 #include <amiga/amiga/isr.h>
80 #include <amiga/amiga/custom.h>
81 #include <amiga/amiga/cia.h>
82 #include <amiga/amiga/cc.h>
83 #include <amiga/dev/zbusvar.h>
84 
85 #include <dev/cons.h>
86 
87 #include "mfcs.h"
88 
89 #ifndef SEROBUF_SIZE
90 #define SEROBUF_SIZE	128
91 #endif
92 #ifndef SERIBUF_SIZE
93 #define SERIBUF_SIZE	1024
94 #endif
95 
96 #define splser()	spl6()
97 
98 /*
99  * 68581 DUART registers
100  */
101 struct mfc_regs {
102 	volatile u_char du_mr1a;
103 #define	du_mr2a		du_mr1a
104 	u_char pad0;
105 	volatile u_char du_csra;
106 #define	du_sra		du_csra
107 	u_char pad2;
108 	volatile u_char du_cra;
109 	u_char pad4;
110 	volatile u_char du_tba;
111 #define	du_rba		du_tba
112 	u_char pad6;
113 	volatile u_char du_acr;
114 #define	du_ipcr		du_acr
115 	u_char pad8;
116 	volatile u_char du_imr;
117 #define	du_isr		du_imr
118 	u_char pad10;
119 	volatile u_char du_ctur;
120 #define	du_cmsb		du_ctur
121 	u_char pad12;
122 	volatile u_char du_ctlr;
123 #define	du_clsb		du_ctlr
124 	u_char pad14;
125 	volatile u_char du_mr1b;
126 #define	du_mr2b		du_mr1b
127 	u_char pad16;
128 	volatile u_char du_csrb;
129 #define	du_srb		du_csrb
130 	u_char pad18;
131 	volatile u_char du_crb;
132 	u_char pad20;
133 	volatile u_char du_tbb;
134 #define	du_rbb		du_tbb
135 	u_char pad22;
136 	volatile u_char du_ivr;
137 	u_char pad24;
138 	volatile u_char du_opcr;
139 #define	du_ip		du_opcr
140 	u_char pad26;
141 	volatile u_char du_btst;
142 #define	du_strc		du_btst
143 	u_char pad28;
144 	volatile u_char du_btrst;
145 #define	du_stpc		du_btrst
146 	u_char pad30;
147 };
148 
149 /*
150  * 68681 DUART serial port registers
151  */
152 struct duart_regs {
153 	volatile u_char ch_mr1;
154 #define	ch_mr2		ch_mr1
155 	u_char pad0;
156 	volatile u_char	ch_csr;
157 #define	ch_sr		ch_csr
158 	u_char pad1;
159 	volatile u_char	ch_cr;
160 	u_char pad2;
161 	volatile u_char	ch_tb;
162 #define	ch_rb		ch_tb
163 	u_char pad3;
164 };
165 
166 struct mfc_softc {
167 	struct	device sc_dev;
168 	struct	isr sc_isr;
169 	struct	mfc_regs *sc_regs;
170 	u_long	clk_frq;
171 	u_short	ct_val;
172 	u_char	ct_usecnt;
173 	u_char	imask;
174 	u_char	mfc_iii;
175 	u_char	last_ip;
176 };
177 
178 #if NMFCS > 0
179 struct mfcs_softc {
180 	struct	device sc_dev;
181 	struct	tty *sc_tty;
182 	struct	duart_regs *sc_duart;
183 	struct	mfc_regs *sc_regs;
184 	struct	mfc_softc *sc_mfc;
185 	int	swflags;
186 	long	flags;			/* XXX */
187 #define CT_USED	1			/* CT in use */
188 	u_short	*rptr, *wptr, incnt, ovfl;
189 	u_short	inbuf[SERIBUF_SIZE];
190 	char	*ptr, *end;
191 	char	outbuf[SEROBUF_SIZE];
192 	struct vbl_node vbl_node;
193 };
194 #endif
195 
196 #if NMFCP > 0
197 struct mfcp_softc {
198 };
199 #endif
200 
201 struct mfc_args {
202 	struct zbus_args zargs;
203 	const char	*subdev;
204 	char	unit;
205 };
206 
207 int	mfcprint(void *auxp, const char *);
208 void	mfcattach(struct device *, struct device *, void *);
209 int	mfcmatch(struct device *, struct cfdata *, void *);
210 
211 #if NMFCS > 0
212 int	mfcsmatch(struct device *, struct cfdata *, void *);
213 void	mfcsattach(struct device *, struct device *, void *);
214 int	mfcsparam( struct tty *, struct termios *);
215 int	mfcshwiflow(struct tty *, int);
216 void	mfcsstart(struct tty *);
217 int	mfcsmctl(dev_t, int, int);
218 void	mfcsxintr(int);
219 void	mfcseint(int, int);
220 void	mfcsmint(register int);
221 #endif
222 
223 #if NMFCP > 0
224 void mfcpattach(struct device *, struct device *, void *);
225 int mfcpmatch(struct device *, struct cfdata *, void *);
226 #endif
227 int mfcintr(void *);
228 
229 CFATTACH_DECL(mfc, sizeof(struct mfc_softc),
230     mfcmatch, mfcattach, NULL, NULL);
231 
232 #if NMFCS > 0
233 CFATTACH_DECL(mfcs, sizeof(struct mfcs_softc),
234     mfcsmatch, mfcsattach, NULL, NULL);
235 
236 extern struct cfdriver mfcs_cd;
237 #endif
238 
239 #if NMFCP > 0
240 CFATTACH_DECL(mfcp, sizeof(struct mfcp_softc),
241     mfcpmatch, mfcpattach, NULL, NULL);
242 #endif
243 
244 dev_type_open(mfcsopen);
245 dev_type_close(mfcsclose);
246 dev_type_read(mfcsread);
247 dev_type_write(mfcswrite);
248 dev_type_ioctl(mfcsioctl);
249 dev_type_stop(mfcsstop);
250 dev_type_tty(mfcstty);
251 dev_type_poll(mfcspoll);
252 
253 const struct cdevsw mfcs_cdevsw = {
254 	mfcsopen, mfcsclose, mfcsread, mfcswrite, mfcsioctl,
255 	mfcsstop, mfcstty, mfcspoll, nommap, ttykqfilter, D_TTY
256 };
257 
258 int	mfcs_active;
259 int	mfcsdefaultrate = 38400 /*TTYDEF_SPEED*/;
260 #define SWFLAGS(dev) (sc->swflags | (((dev) & 0x80) == 0 ? TIOCFLAG_SOFTCAR : 0))
261 
262 #ifdef notyet
263 /*
264  * MultiFaceCard III, II+ (not supported yet), and
265  * SerialMaster 500+ (not supported yet)
266  * baud rate tables for BRG set 1 [not used yet]
267  */
268 
269 const struct speedtab mfcs3speedtab1[] = {
270 	{ 0,		0	},
271 	{ 100,		0x00	},
272 	{ 220,		0x11	},
273 	{ 600,		0x44	},
274 	{ 1200,		0x55	},
275 	{ 2400,		0x66	},
276 	{ 4800,		0x88	},
277 	{ 9600,		0x99	},
278 	{ 19200,	0xbb	},
279 	{ 115200,	0xcc	},
280 	{ -1,		-1	}
281 };
282 
283 /*
284  * MultiFaceCard II, I, and SerialMaster 500
285  * baud rate tables for BRG set 1 [not used yet]
286  */
287 
288 const struct speedtab mfcs2speedtab1[] = {
289 	{ 0,		0	},
290 	{ 50,		0x00	},
291 	{ 110,		0x11	},
292 	{ 300,		0x44	},
293 	{ 600,		0x55	},
294 	{ 1200,		0x66	},
295 	{ 2400,		0x88	},
296  	{ 4800,		0x99	},
297 	{ 9600,		0xbb	},
298 	{ 38400,	0xcc	},
299 	{ -1,		-1	}
300 };
301 #endif
302 
303 /*
304  * MultiFaceCard III, II+ (not supported yet), and
305  * SerialMaster 500+ (not supported yet)
306  * baud rate tables for BRG set 2
307  */
308 
309 const struct speedtab mfcs3speedtab2[] = {
310 	{ 0,		0	},
311 	{ 150,		0x00	},
312 	{ 200,		0x11	},
313 	{ 300,		0x33	},
314 	{ 600,		0x44	},
315 	{ 1200,		0x55	},
316 	{ 2400,		0x66	},
317 	{ 4800,		0x88	},
318 	{ 9600,		0x99	},
319 	{ 19200,	0xbb	},
320 	{ 38400,	0xcc	},
321 	{ -1,		-1	}
322 };
323 
324 /*
325  * MultiFaceCard II, I, and SerialMaster 500
326  * baud rate tables for BRG set 2
327  */
328 
329 const struct speedtab mfcs2speedtab2[] = {
330 	{ 0,		0	},
331 	{ 75,		0x00	},
332 	{ 100,		0x11	},
333 	{ 150,		0x33	},
334 	{ 300,		0x44	},
335 	{ 600,		0x55	},
336 	{ 1200,		0x66	},
337 	{ 2400,		0x88	},
338  	{ 4800,		0x99	},
339 	{ 9600,		0xbb	},
340 	{ 19200,	0xcc	},
341 	{ -1,		-1	}
342 };
343 
344 /*
345  * if we are an bsc/Alf Data MultFaceCard (I, II, and III)
346  */
347 int
348 mfcmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
349 {
350 	struct zbus_args *zap;
351 
352 	zap = auxp;
353 	if (zap->manid == 2092 &&
354 	    (zap->prodid == 16 || zap->prodid == 17 || zap->prodid == 18))
355 
356 		return(1);
357 	return(0);
358 }
359 
360 void
361 mfcattach(struct device *pdp, struct device *dp, void *auxp)
362 {
363 	struct mfc_softc *scc;
364 	struct zbus_args *zap;
365 	struct mfc_args ma;
366 	int unit;
367 	struct mfc_regs *rp;
368 
369 	zap = auxp;
370 
371 	printf ("\n");
372 
373 	scc = (struct mfc_softc *)dp;
374 	unit = device_unit(&scc->sc_dev);
375 	scc->sc_regs = rp = zap->va;
376 	if (zap->prodid == 18)
377 		scc->mfc_iii = 3;
378 	scc->clk_frq = scc->mfc_iii ? 230400 : 115200;
379 
380 	rp->du_opcr = 0x00;		/* configure output port? */
381 	rp->du_btrst = 0x0f;		/* clear modem lines */
382 	rp->du_ivr = 0;			/* IVR */
383 	rp->du_imr = 0;			/* IMR */
384 	rp->du_acr = 0xe0;		/* baud rate generate set 2 */
385 	rp->du_ctur = 0;
386 	rp->du_ctlr = 4;
387 	rp->du_csra = 0xcc;		/* clock select = 38400 */
388 	rp->du_cra = 0x10;		/* reset mode register ptr */
389 	rp->du_cra = 0x20;
390 	rp->du_cra = 0x30;
391 	rp->du_cra = 0x40;
392 	rp->du_mr1a = 0x93;		/* MRA1 */
393 	rp->du_mr2a = 0x17;		/* MRA2 */
394 	rp->du_csrb = 0xcc;		/* clock select = 38400 */
395 	rp->du_crb = 0x10;		/* reset mode register ptr */
396 	rp->du_crb = 0x20;
397 	rp->du_crb = 0x30;
398 	rp->du_crb = 0x40;
399 	rp->du_mr1b = 0x93;		/* MRB1 */
400 	rp->du_mr2b = 0x17;		/* MRB2 */
401 	rp->du_cra = 0x05;		/* enable A Rx & Tx */
402 	rp->du_crb = 0x05;		/* enable B Rx & Tx */
403 
404 	scc->sc_isr.isr_intr = mfcintr;
405 	scc->sc_isr.isr_arg = scc;
406 	scc->sc_isr.isr_ipl = 6;
407 	add_isr(&scc->sc_isr);
408 
409 	/* configure ports */
410 	bcopy(zap, &ma.zargs, sizeof(struct zbus_args));
411 	ma.subdev = "mfcs";
412 	ma.unit = unit * 2;
413 	config_found(dp, &ma, mfcprint);
414 	ma.unit = unit * 2 + 1;
415 	config_found(dp, &ma, mfcprint);
416 	ma.subdev = "mfcp";
417 	ma.unit = unit;
418 	config_found(dp, &ma, mfcprint);
419 }
420 
421 /*
422  *
423  */
424 int
425 mfcsmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
426 {
427 	struct mfc_args *ma;
428 
429 	ma = auxp;
430 	if (strcmp(ma->subdev, "mfcs") == 0)
431 		return (1);
432 	return (0);
433 }
434 
435 void
436 mfcsattach(struct device *pdp, struct device *dp, void *auxp)
437 {
438 	int unit;
439 	struct mfcs_softc *sc;
440 	struct mfc_softc *scc;
441 	struct mfc_args *ma;
442 	struct mfc_regs *rp;
443 
444 	sc = (struct mfcs_softc *) dp;
445 	scc = (struct mfc_softc *) pdp;
446 	ma = auxp;
447 
448 	if (dp) {
449 		printf (": input fifo %d output fifo %d\n", SERIBUF_SIZE,
450 		    SEROBUF_SIZE);
451 		alloc_sicallback();
452 	}
453 
454 	unit = ma->unit;
455 	mfcs_active |= 1 << unit;
456 	sc->rptr = sc->wptr = sc->inbuf;
457 	sc->sc_mfc = scc;
458 	sc->sc_regs = rp = scc->sc_regs;
459 	sc->sc_duart = (struct duart_regs *) ((unit & 1) ?
460 	    __UNVOLATILE(&rp->du_mr1b) : __UNVOLATILE(&rp->du_mr1a));
461 	/*
462 	 * should have only one vbl routine to handle all ports?
463 	 */
464 	sc->vbl_node.function = (void (*) (void *)) mfcsmint;
465 	sc->vbl_node.data = (void *) unit;
466 	add_vbl_function(&sc->vbl_node, 1, (void *) unit);
467 }
468 
469 /*
470  * print diag if pnp is NULL else just extra
471  */
472 int
473 mfcprint(void *auxp, const char *pnp)
474 {
475 	if (pnp == NULL)
476 		return(UNCONF);
477 	return(QUIET);
478 }
479 
480 int
481 mfcsopen(dev_t dev, int flag, int mode, struct lwp *l)
482 {
483 	struct tty *tp;
484 	struct mfcs_softc *sc;
485 	int unit, error, s;
486 
487 	error = 0;
488 	unit = dev & 0x1f;
489 
490 	if (unit >= mfcs_cd.cd_ndevs || (mfcs_active & (1 << unit)) == 0)
491 		return (ENXIO);
492 	sc = mfcs_cd.cd_devs[unit];
493 
494 	s = spltty();
495 
496 	if (sc->sc_tty)
497 		tp = sc->sc_tty;
498 	else {
499 		tp = sc->sc_tty = ttymalloc();
500 		tty_attach(tp);
501 	}
502 
503 	tp->t_oproc = (void (*) (struct tty *)) mfcsstart;
504 	tp->t_param = mfcsparam;
505 	tp->t_dev = dev;
506 	tp->t_hwiflow = mfcshwiflow;
507 
508 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
509 		ttychars(tp);
510 		if (tp->t_ispeed == 0) {
511 			/*
512 			 * only when cleared do we reset to defaults.
513 			 */
514 			tp->t_iflag = TTYDEF_IFLAG;
515 			tp->t_oflag = TTYDEF_OFLAG;
516 			tp->t_cflag = TTYDEF_CFLAG;
517 			tp->t_lflag = TTYDEF_LFLAG;
518 			tp->t_ispeed = tp->t_ospeed = mfcsdefaultrate;
519 		}
520 		/*
521 		 * do these all the time
522 		 */
523 		if (sc->swflags & TIOCFLAG_CLOCAL)
524 			tp->t_cflag |= CLOCAL;
525 		if (sc->swflags & TIOCFLAG_CRTSCTS)
526 			tp->t_cflag |= CRTSCTS;
527 		if (sc->swflags & TIOCFLAG_MDMBUF)
528 			tp->t_cflag |= MDMBUF;
529 		mfcsparam(tp, &tp->t_termios);
530 		ttsetwater(tp);
531 
532 		(void)mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMSET);
533 		if ((SWFLAGS(dev) & TIOCFLAG_SOFTCAR) ||
534 		    (mfcsmctl(dev, 0, DMGET) & TIOCM_CD))
535 			tp->t_state |= TS_CARR_ON;
536 		else
537 			tp->t_state &= ~TS_CARR_ON;
538 	} else if (tp->t_state & TS_XCLUDE &&
539 		   kauth_authorize_generic(l->l_proc->p_cred,
540 				     KAUTH_GENERIC_ISSUSER,
541 				     &l->l_proc->p_acflag) != 0) {
542 		splx(s);
543 		return(EBUSY);
544 	}
545 
546 	/*
547 	 * if NONBLOCK requested, ignore carrier
548 	 */
549 	if (flag & O_NONBLOCK)
550 		goto done;
551 
552 	/*
553 	 * block waiting for carrier
554 	 */
555 	while ((tp->t_state & TS_CARR_ON) == 0 && (tp->t_cflag & CLOCAL) == 0) {
556 		tp->t_wopen++;
557 		error = ttysleep(tp, (caddr_t)&tp->t_rawq,
558 		    TTIPRI | PCATCH, ttopen, 0);
559 		tp->t_wopen--;
560 		if (error) {
561 			splx(s);
562 			return(error);
563 		}
564 	}
565 done:
566 	/* This is a way to handle lost XON characters */
567 	if ((flag & O_TRUNC) && (tp->t_state & TS_TTSTOP)) {
568 		tp->t_state &= ~TS_TTSTOP;
569 	        ttstart (tp);
570 	}
571 
572 	splx(s);
573 	/*
574 	 * Reset the tty pointer, as there could have been a dialout
575 	 * use of the tty with a dialin open waiting.
576 	 */
577 	tp->t_dev = dev;
578 	return tp->t_linesw->l_open(dev, tp);
579 }
580 
581 /*ARGSUSED*/
582 int
583 mfcsclose(dev_t dev, int flag, int mode, struct lwp *l)
584 {
585 	struct tty *tp;
586 	int unit;
587 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
588 	struct mfc_softc *scc= sc->sc_mfc;
589 
590 	unit = dev & 31;
591 
592 	tp = sc->sc_tty;
593 	tp->t_linesw->l_close(tp, flag);
594 	sc->sc_duart->ch_cr = 0x70;			/* stop break */
595 
596 	scc->imask &= ~(0x7 << ((unit & 1) * 4));
597 	scc->sc_regs->du_imr = scc->imask;
598 	if (sc->flags & CT_USED) {
599 		--scc->ct_usecnt;
600 		sc->flags &= ~CT_USED;
601 	}
602 
603 	/*
604 	 * If the device is closed, it's close, no matter whether we deal with
605 	 * modem control signals nor not.
606 	 */
607 #if 0
608 	if (tp->t_cflag & HUPCL || tp->t_wopen != 0 ||
609 	    (tp->t_state & TS_ISOPEN) == 0)
610 #endif
611 		(void) mfcsmctl(dev, 0, DMSET);
612 	ttyclose(tp);
613 #if not_yet
614 	if (tp != &mfcs_cons) {
615 		remove_vbl_function(&sc->vbl_node);
616 		ttyfree(tp);
617 		sc->sc_tty = (struct tty *) NULL;
618 	}
619 #endif
620 	return (0);
621 }
622 
623 int
624 mfcsread(dev_t dev, struct uio *uio, int flag)
625 {
626 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
627 	struct tty *tp = sc->sc_tty;
628 	if (tp == NULL)
629 		return(ENXIO);
630 	return tp->t_linesw->l_read(tp, uio, flag);
631 }
632 
633 int
634 mfcswrite(dev_t dev, struct uio *uio, int flag)
635 {
636 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
637 	struct tty *tp = sc->sc_tty;
638 
639 	if (tp == NULL)
640 		return(ENXIO);
641 	return tp->t_linesw->l_write(tp, uio, flag);
642 }
643 
644 int
645 mfcspoll(dev_t dev, int events, struct lwp *l)
646 {
647 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
648 	struct tty *tp = sc->sc_tty;
649 
650 	if (tp == NULL)
651 		return(ENXIO);
652 	return ((*tp->t_linesw->l_poll)(tp, events, l));
653 }
654 
655 struct tty *
656 mfcstty(dev_t dev)
657 {
658 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
659 
660 	return (sc->sc_tty);
661 }
662 
663 int
664 mfcsioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
665 {
666 	register struct tty *tp;
667 	register int error;
668 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
669 
670 	tp = sc->sc_tty;
671 	if (!tp)
672 		return ENXIO;
673 
674 	error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l);
675 	if (error != EPASSTHROUGH)
676 		return(error);
677 
678 	error = ttioctl(tp, cmd, data, flag, l);
679 	if (error != EPASSTHROUGH)
680 		return(error);
681 
682 	switch (cmd) {
683 	case TIOCSBRK:
684 		sc->sc_duart->ch_cr = 0x60;		/* start break */
685 		break;
686 
687 	case TIOCCBRK:
688 		sc->sc_duart->ch_cr = 0x70;		/* stop break */
689 		break;
690 
691 	case TIOCSDTR:
692 		(void) mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIS);
693 		break;
694 
695 	case TIOCCDTR:
696 		(void) mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIC);
697 		break;
698 
699 	case TIOCMSET:
700 		(void) mfcsmctl(dev, *(int *) data, DMSET);
701 		break;
702 
703 	case TIOCMBIS:
704 		(void) mfcsmctl(dev, *(int *) data, DMBIS);
705 		break;
706 
707 	case TIOCMBIC:
708 		(void) mfcsmctl(dev, *(int *) data, DMBIC);
709 		break;
710 
711 	case TIOCMGET:
712 		*(int *)data = mfcsmctl(dev, 0, DMGET);
713 		break;
714 	case TIOCGFLAGS:
715 		*(int *)data = SWFLAGS(dev);
716 		break;
717 	case TIOCSFLAGS:
718 		error = kauth_authorize_generic(l->l_proc->p_cred,
719 					  KAUTH_GENERIC_ISSUSER,
720 					  &l->l_proc->p_acflag);
721 		if (error != 0)
722 			return(EPERM);
723 
724 		sc->swflags = *(int *)data;
725                 sc->swflags &= /* only allow valid flags */
726                   (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | TIOCFLAG_CRTSCTS);
727 		/* XXXX need to change duart parameters? */
728 		break;
729 	default:
730 		return(EPASSTHROUGH);
731 	}
732 
733 	return(0);
734 }
735 
736 int
737 mfcsparam(struct tty *tp, struct termios *t)
738 {
739 	int cflag, unit, ospeed;
740 	struct mfcs_softc *sc = mfcs_cd.cd_devs[tp->t_dev & 31];
741 	struct mfc_softc *scc= sc->sc_mfc;
742 
743 	cflag = t->c_cflag;
744 	unit = tp->t_dev & 31;
745 	if (sc->flags & CT_USED) {
746 		--scc->ct_usecnt;
747 		sc->flags &= ~CT_USED;
748 	}
749 	ospeed = ttspeedtab(t->c_ospeed, scc->mfc_iii ? mfcs3speedtab2 :
750 	    mfcs2speedtab2);
751 
752 	/*
753 	 * If Baud Rate Generator can't generate requested speed,
754 	 * try to use the counter/timer.
755 	 */
756 	if (ospeed < 0 && (scc->clk_frq % t->c_ospeed) == 0) {
757 		ospeed = scc->clk_frq / t->c_ospeed;	/* divisor */
758 		if (scc->ct_usecnt > 0 && scc->ct_val != ospeed)
759 			ospeed = -1;
760 		else {
761 			scc->sc_regs->du_ctur = ospeed >> 8;
762 			scc->sc_regs->du_ctlr = ospeed;
763 			scc->ct_val = ospeed;
764 			++scc->ct_usecnt;
765 			sc->flags |= CT_USED;
766 			ospeed = 0xdd;
767 		}
768 	}
769 	/* XXXX 68681 duart could handle split speeds */
770 	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
771 		return(EINVAL);
772 
773 	/* XXXX handle parity, character size, stop bits, flow control */
774 
775 	/*
776 	 * copy to tty
777 	 */
778 	tp->t_ispeed = t->c_ispeed;
779 	tp->t_ospeed = t->c_ospeed;
780 	tp->t_cflag = cflag;
781 
782 	/*
783 	 * enable interrupts
784 	 */
785 	scc->imask |= (0x2 << ((unit & 1) * 4)) | 0x80;
786 	scc->sc_regs->du_imr = scc->imask;
787 #if defined(DEBUG) && 0
788 	printf("mfcsparam: speed %d => %x ct %d imask %x cflag %x\n",
789 	    t->c_ospeed, ospeed, scc->ct_val, scc->imask, cflag);
790 #endif
791 	if (ospeed == 0)
792 		(void)mfcsmctl(tp->t_dev, 0, DMSET);	/* hang up line */
793 	else {
794 		/*
795 		 * (re)enable DTR
796 		 * and set baud rate. (8 bit mode)
797 		 */
798 		(void)mfcsmctl(tp->t_dev, TIOCM_DTR | TIOCM_RTS, DMSET);
799 		sc->sc_duart->ch_csr = ospeed;
800 	}
801 	return(0);
802 }
803 
804 int
805 mfcshwiflow(struct tty *tp, int flag)
806 {
807 	struct mfcs_softc *sc = mfcs_cd.cd_devs[tp->t_dev & 31];
808 	int unit = tp->t_dev & 1;
809 
810         if (flag)
811 		sc->sc_regs->du_btrst = 1 << unit;
812 	else
813 		sc->sc_regs->du_btst = 1 << unit;
814         return 1;
815 }
816 
817 void
818 mfcsstart(struct tty *tp)
819 {
820 	int cc, s, unit;
821 	struct mfcs_softc *sc = mfcs_cd.cd_devs[tp->t_dev & 31];
822 	struct mfc_softc *scc= sc->sc_mfc;
823 
824 	if ((tp->t_state & TS_ISOPEN) == 0)
825 		return;
826 
827 	unit = tp->t_dev & 1;
828 
829 	s = splser();
830 	if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))
831 		goto out;
832 
833 	cc = tp->t_outq.c_cc;
834 	if (cc <= tp->t_lowat) {
835 		if (tp->t_state & TS_ASLEEP) {
836 			tp->t_state &= ~TS_ASLEEP;
837 			wakeup((caddr_t) & tp->t_outq);
838 		}
839 		selwakeup(&tp->t_wsel);
840 	}
841 	if (cc == 0 || (tp->t_state & TS_BUSY))
842 		goto out;
843 
844 	/*
845 	 * We only do bulk transfers if using CTSRTS flow control, not for
846 	 * (probably sloooow) ixon/ixoff devices.
847 	 */
848 	if ((tp->t_cflag & CRTSCTS) == 0)
849 		cc = 1;
850 
851 	/*
852 	 * Limit the amount of output we do in one burst
853 	 * to prevent hogging the CPU.
854 	 */
855 	if (cc > SEROBUF_SIZE)
856 		cc = SEROBUF_SIZE;
857 	cc = q_to_b(&tp->t_outq, sc->outbuf, cc);
858 	if (cc > 0) {
859 		tp->t_state |= TS_BUSY;
860 
861 		sc->ptr = sc->outbuf;
862 		sc->end = sc->outbuf + cc;
863 
864 		/*
865 		 * Get first character out, then have TBE-interrupts blow out
866 		 * further characters, until buffer is empty, and TS_BUSY gets
867 		 * cleared.
868 		 */
869 		sc->sc_duart->ch_tb = *sc->ptr++;
870 		scc->imask |= 1 << (unit * 4);
871 		sc->sc_regs->du_imr = scc->imask;
872 	}
873 out:
874 	splx(s);
875 }
876 
877 /*
878  * Stop output on a line.
879  */
880 /*ARGSUSED*/
881 void
882 mfcsstop(struct tty *tp, int flag)
883 {
884 	int s;
885 
886 	s = splser();
887 	if (tp->t_state & TS_BUSY) {
888 		if ((tp->t_state & TS_TTSTOP) == 0)
889 			tp->t_state |= TS_FLUSH;
890 	}
891 	splx(s);
892 }
893 
894 int
895 mfcsmctl(dev_t dev, int bits, int how)
896 {
897 	int unit, s;
898 	u_char ub = 0;
899 	struct mfcs_softc *sc = mfcs_cd.cd_devs[dev & 31];
900 
901 	unit = dev & 1;
902 
903 	/*
904 	 * convert TIOCM* mask into CIA mask
905 	 * which is active low
906 	 */
907 	if (how != DMGET) {
908 		/*
909 		 * need to save current state of DTR & RTS ?
910 		 */
911 		if (bits & TIOCM_DTR)
912 			ub |= 0x04 << unit;
913 		if (bits & TIOCM_RTS)
914 			ub |= 0x01 << unit;
915 	}
916 	s = splser();
917 	switch (how) {
918 	case DMSET:
919 		sc->sc_regs->du_btst = ub;
920 		sc->sc_regs->du_btrst = ub ^ (0x05 << unit);
921 		break;
922 
923 	case DMBIC:
924 		sc->sc_regs->du_btrst = ub;
925 		ub = ~sc->sc_regs->du_ip;
926 		break;
927 
928 	case DMBIS:
929 		sc->sc_regs->du_btst = ub;
930 		ub = ~sc->sc_regs->du_ip;
931 		break;
932 
933 	case DMGET:
934 		ub = ~sc->sc_regs->du_ip;
935 		break;
936 	}
937 	(void)splx(s);
938 
939 	/* XXXX should keep DTR & RTS states in softc? */
940 	bits = TIOCM_DTR | TIOCM_RTS;
941 	if (ub & (1 << unit))
942 		bits |= TIOCM_CTS;
943 	if (ub & (4 << unit))
944 		bits |= TIOCM_DSR;
945 	if (ub & (0x10 << unit))
946 		bits |= TIOCM_CD;
947 	/* XXXX RI is not supported on all boards */
948 	if (sc->sc_regs->pad26 & (1 << unit))
949 		bits |= TIOCM_RI;
950 
951 	return(bits);
952 }
953 
954 /*
955  * Level 6 interrupt processing for the MultiFaceCard 68681 DUART
956  */
957 
958 int
959 mfcintr(void *arg)
960 {
961 	struct mfc_softc *scc = arg;
962 	struct mfcs_softc *sc;
963 	struct mfc_regs *regs;
964 	struct tty *tp;
965 	int istat, unit;
966 	u_short c;
967 
968 	regs = scc->sc_regs;
969 	istat = regs->du_isr & scc->imask;
970 	if (istat == 0)
971 		return (0);
972 	unit = device_unit(&scc->sc_dev) * 2;
973 	if (istat & 0x02) {		/* channel A receive interrupt */
974 		sc = mfcs_cd.cd_devs[unit];
975 		while (1) {
976 			c = regs->du_sra << 8;
977 			if ((c & 0x0100) == 0)
978 				break;
979 			c |= regs->du_rba;
980 			if (sc->incnt == SERIBUF_SIZE)
981 				++sc->ovfl;
982 			else {
983 				*sc->wptr++ = c;
984 				if (sc->wptr == sc->inbuf + SERIBUF_SIZE)
985 					sc->wptr = sc->inbuf;
986 				++sc->incnt;
987 				if (sc->incnt > SERIBUF_SIZE - 16)
988 					regs->du_btrst = 1;
989 			}
990 			if (c & 0x1000)
991 				regs->du_cra = 0x40;
992 		}
993 	}
994 	if (istat & 0x20) {		/* channel B receive interrupt */
995 		sc = mfcs_cd.cd_devs[unit + 1];
996 		while (1) {
997 			c = regs->du_srb << 8;
998 			if ((c & 0x0100) == 0)
999 				break;
1000 			c |= regs->du_rbb;
1001 			if (sc->incnt == SERIBUF_SIZE)
1002 				++sc->ovfl;
1003 			else {
1004 				*sc->wptr++ = c;
1005 				if (sc->wptr == sc->inbuf + SERIBUF_SIZE)
1006 					sc->wptr = sc->inbuf;
1007 				++sc->incnt;
1008 				if (sc->incnt > SERIBUF_SIZE - 16)
1009 					regs->du_btrst = 2;
1010 			}
1011 			if (c & 0x1000)
1012 				regs->du_crb = 0x40;
1013 		}
1014 	}
1015 	if (istat & 0x01) {		/* channel A transmit interrupt */
1016 		sc = mfcs_cd.cd_devs[unit];
1017 		tp = sc->sc_tty;
1018 		if (sc->ptr == sc->end) {
1019 			tp->t_state &= ~(TS_BUSY | TS_FLUSH);
1020 			scc->imask &= ~0x01;
1021 			regs->du_imr = scc->imask;
1022 			add_sicallback (tp->t_linesw ?
1023 			    (sifunc_t)tp->t_linesw->l_start
1024 			    : (sifunc_t)mfcsstart, tp, NULL);
1025 
1026 		}
1027 		else
1028 			regs->du_tba = *sc->ptr++;
1029 	}
1030 	if (istat & 0x10) {		/* channel B transmit interrupt */
1031 		sc = mfcs_cd.cd_devs[unit + 1];
1032 		tp = sc->sc_tty;
1033 		if (sc->ptr == sc->end) {
1034 			tp->t_state &= ~(TS_BUSY | TS_FLUSH);
1035 			scc->imask &= ~0x10;
1036 			regs->du_imr = scc->imask;
1037 			add_sicallback (tp->t_linesw ?
1038 			    (sifunc_t)tp->t_linesw->l_start
1039 			    : (sifunc_t)mfcsstart, tp, NULL);
1040 		}
1041 		else
1042 			regs->du_tbb = *sc->ptr++;
1043 	}
1044 	if (istat & 0x80) {		/* input port change interrupt */
1045 		c = regs->du_ipcr;
1046 		printf ("%s: ipcr %02x", scc->sc_dev.dv_xname, c);
1047 	}
1048 	return(1);
1049 }
1050 
1051 void
1052 mfcsxintr(int unit)
1053 {
1054 	int s1, s2, ovfl;
1055 	struct mfcs_softc *sc = mfcs_cd.cd_devs[unit];
1056 	struct tty *tp = sc->sc_tty;
1057 
1058 	/*
1059 	 * Make sure we're not interrupted by another
1060 	 * vbl, but allow level6 ints
1061 	 */
1062 	s1 = spltty();
1063 
1064 	/*
1065 	 * pass along any acumulated information
1066 	 * while input is not blocked
1067 	 */
1068 	while (sc->incnt && (tp->t_state & TS_TBLOCK) == 0) {
1069 		/*
1070 		 * no collision with ser_fastint()
1071 		 */
1072 		mfcseint(unit, *sc->rptr++);
1073 
1074 		ovfl = 0;
1075 		/* lock against mfcs_fastint() */
1076 		s2 = splser();
1077 		--sc->incnt;
1078 		if (sc->rptr == sc->inbuf + SERIBUF_SIZE)
1079 			sc->rptr = sc->inbuf;
1080 		if (sc->ovfl != 0) {
1081 			ovfl = sc->ovfl;
1082 			sc->ovfl = 0;
1083 		}
1084 		splx(s2);
1085 		if (ovfl != 0)
1086 			log(LOG_WARNING, "%s: %d buffer overflow!\n",
1087 			    sc->sc_dev.dv_xname, ovfl);
1088 	}
1089 	if (sc->incnt == 0 && (tp->t_state & TS_TBLOCK) == 0) {
1090 		sc->sc_regs->du_btst = 1 << unit;	/* XXXX */
1091 	}
1092 	splx(s1);
1093 }
1094 
1095 void
1096 mfcseint(int unit, int stat)
1097 {
1098 	struct mfcs_softc *sc = mfcs_cd.cd_devs[unit];
1099 	struct tty *tp;
1100 	u_char ch;
1101 	int c;
1102 
1103 	tp = sc->sc_tty;
1104 	ch = stat & 0xff;
1105 	c = ch;
1106 
1107 	if ((tp->t_state & TS_ISOPEN) == 0) {
1108 #ifdef KGDB
1109 		extern const struct cdevsw ser_cdevsw;
1110 		int maj;
1111 
1112 		/* we don't care about parity errors */
1113 		maj = cdevsw_lookup_major(&ser_cdevsw);
1114 		if (kgdb_dev == makedev(maj, unit) && c == FRAME_END)
1115 			kgdb_connect(0);	/* trap into kgdb */
1116 #endif
1117 		return;
1118 	}
1119 
1120 	/*
1121 	 * Check for break and (if enabled) parity error.
1122 	 */
1123 	if (stat & 0xc000)
1124 		c |= TTY_FE;
1125 	else if (stat & 0x2000)
1126 			c |= TTY_PE;
1127 
1128 	if (stat & 0x1000)
1129 		log(LOG_WARNING, "%s: fifo overflow\n",
1130 		    ((struct mfcs_softc *)mfcs_cd.cd_devs[unit])->sc_dev.dv_xname);
1131 
1132 	tp->t_linesw->l_rint(c, tp);
1133 }
1134 
1135 /*
1136  * This interrupt is periodically invoked in the vertical blank
1137  * interrupt.  It's used to keep track of the modem control lines
1138  * and (new with the fast_int code) to move accumulated data
1139  * up into the tty layer.
1140  */
1141 void
1142 mfcsmint(int unit)
1143 {
1144 	struct tty *tp;
1145 	struct mfcs_softc *sc = mfcs_cd.cd_devs[unit];
1146 	u_char stat, last, istat;
1147 
1148 	tp = sc->sc_tty;
1149 	if (!tp)
1150 		return;
1151 
1152 	if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
1153 		sc->rptr = sc->wptr = sc->inbuf;
1154 		sc->incnt = 0;
1155 		return;
1156 	}
1157 	/*
1158 	 * empty buffer
1159 	 */
1160 	mfcsxintr(unit);
1161 
1162 	stat = ~sc->sc_regs->du_ip;
1163 	last = sc->sc_mfc->last_ip;
1164 	sc->sc_mfc->last_ip = stat;
1165 
1166 	/*
1167 	 * check whether any interesting signal changed state
1168 	 */
1169 	istat = stat ^ last;
1170 
1171 	if ((istat & (0x10 << (unit & 1))) && 		/* CD changed */
1172 	    (SWFLAGS(tp->t_dev) & TIOCFLAG_SOFTCAR) == 0) {
1173 		if (stat & (0x10 << (unit & 1)))
1174 			tp->t_linesw->l_modem(tp, 1);
1175 		else if (tp->t_linesw->l_modem(tp, 0) == 0) {
1176 			sc->sc_regs->du_btrst = 0x0a << (unit & 1);
1177 		}
1178 	}
1179 }
1180