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