xref: /original-bsd/sys/vax/uba/dmf.c (revision 7d595439)
1 /*
2  * Copyright (c) 1982, 1986 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)dmf.c	7.2 (Berkeley) 07/08/86
7  */
8 
9 #include "dmf.h"
10 #if NDMF > 0
11 /*
12  * DMF32 driver
13  *
14  *
15  * TODO:
16  *	test with modem
17  *	load as much as possible into silo
18  *	use auto XON/XOFF
19  *	test reset code
20  ****************************
21  * DMF32 line printer driver
22  *
23  * the line printer on dmfx is indicated by a minor device code of 128+x
24  *
25  * the flags field of the config file is interpreted like so:
26  * bits		meaning
27  * ----		-------
28  * 0-7		soft carrier bits for ttys part of dmf32
29  * 8-15		number of cols/line on the line printer
30  *			if 0, 132 will be used.
31  * 16-23	number of lines/page on the line printer
32  *			if 0, 66 will be used.
33  * 24		if 1 DO NOT use the auto format mode of the
34  *			line printer parallel port
35  */
36 #include "../machine/pte.h"
37 
38 #include "bk.h"
39 #include "uba.h"
40 #include "param.h"
41 #include "conf.h"
42 #include "dir.h"
43 #include "user.h"
44 #include "proc.h"
45 #include "ioctl.h"
46 #include "tty.h"
47 #include "map.h"
48 #include "buf.h"
49 #include "vm.h"
50 #include "bkmac.h"
51 #include "clist.h"
52 #include "file.h"
53 #include "uio.h"
54 #include "kernel.h"
55 #include "syslog.h"
56 
57 #include "ubareg.h"
58 #include "ubavar.h"
59 #include "dmfreg.h"
60 
61 /*
62  * Definition of the driver for the auto-configuration program.
63  */
64 int	dmfprobe(), dmfattach(), dmfrint(), dmfxint();
65 int	dmflint();
66 struct	uba_device *dmfinfo[NDMF];
67 u_short	dmfstd[] = { 0 };
68 struct	uba_driver dmfdriver =
69 	{ dmfprobe, 0, dmfattach, 0, dmfstd, "dmf", dmfinfo };
70 
71 int	dmf_timeout = 10;		/* silo timeout, in ms */
72 int	dmf_mindma = 4;			/* don't dma below this point */
73 
74 /*
75  * Local variables for the driver
76  */
77 char	dmf_speeds[] =
78 	{ 0, 0, 1, 2, 3, 4, 0, 5, 6, 7, 010, 012, 014, 016, 017, 0 };
79 
80 #ifndef	PORTSELECTOR
81 #define	ISPEED	B9600
82 #define	IFLAGS	(EVENP|ODDP|ECHO)
83 #else
84 #define	ISPEED	B4800
85 #define	IFLAGS	(EVENP|ODDP)
86 #endif
87 
88 struct	tty dmf_tty[NDMF*8];
89 char	dmfsoftCAR[NDMF];
90 
91 struct dmfl_softc {
92 	u_int	dmfl_state; 		/* soft state bits */
93 	int	dmfl_info;		/* uba info */
94 	u_short	dmfl_lines;		/* lines per page (66 def.) */
95 	u_short	dmfl_cols; 		/* cols per line (132 def.) */
96 	u_short	dmfl_format;		/* fflag for auto form feed */
97 	char	dmfl_buf[DMFL_BUFSIZ];
98 } dmfl_softc[NDMF];
99 
100 /*
101  * convert device number into DMF line printer unit number
102  */
103 #define	DMFL_UNIT(d)	(minor(d)&0xF)	/* up to 16 DMFs */
104 
105 #define ASLP 1		/* waiting for interrupt from dmf */
106 #define OPEN 2		/* line printer is open */
107 #define ERROR 4		/* error while printing, driver
108 			 refuses to do anything till closed */
109 #define MOREIO 8	/* more data for printer */
110 
111 #ifndef lint
112 int	ndmf = NDMF*8;			/* used by iostat */
113 #endif
114 int	dmfact;				/* mask of active dmf's */
115 int	dmfstart(), ttrstrt();
116 
117 /*
118  * The clist space is mapped by the driver onto each UNIBUS.
119  * The UBACVT macro converts a clist space address for unibus uban
120  * into an i/o space address for the DMA routine.
121  */
122 int	dmf_ubinfo[NUBA];		/* info about allocated unibus map */
123 int	cbase[NUBA];			/* base address in unibus map */
124 #define	UBACVT(x, uban)		(cbase[uban] + ((x)-(char *)cfree))
125 char	dmf_dma[NDMF*8];
126 
127 /*
128  * Routine for configuration to set dmf interrupt.
129  */
130 /*ARGSUSED*/
131 dmfprobe(reg, ctlr)
132 	caddr_t reg;
133 	struct uba_device *ctlr;
134 {
135 	register int br, cvec;		/* these are ``value-result'' */
136 	register struct dmfdevice *dmfaddr = (struct dmfdevice *)reg;
137 	register int i;
138 	register unsigned int a;
139 	static char *dmfdevs[]=
140 		{"parallel","printer","synch","asynch"};
141 	unsigned int dmfoptions;
142 	static int (*intrv[3])() = { (int (*)())0, (int (*)())0, (int (*)())0 };
143 
144 #ifdef lint
145 	br = 0; cvec = br; br = cvec;
146 	dmfxint(0); dmfrint(0);
147 	dmfsrint(); dmfsxint(); dmfdaint(); dmfdbint(); dmflint(0);
148 #endif
149 	/*
150 	 * Pick the usual size DMF vector here (don't decrement it here).
151 	 * grab configuration; note that the DMF32
152 	 * doesn't seem to put the right bits in this
153 	 * register until AFTER the interrupt vector is set.
154 	 */
155 	br = 0x15;
156 	cvec = (uba_hd[numuba].uh_lastiv - 4*8);
157 	dmfaddr->dmfccsr0 = (cvec >> 2);
158 	dmfoptions = dmfaddr->dmfccsr0 & DMFC_CONFMASK;
159 
160 	/* catch a couple of special cases:  Able vmz/32n and vmz/lp	*/
161 	if (dmfoptions == DMFC_ASYNC) {
162 		/* Async portion only */
163 
164 		cvec = (uba_hd[numuba].uh_lastiv -= 8);
165 		dmfaddr->dmfccsr0 = (cvec - 2*8) >> 2;
166 		intrv[0] = ctlr->ui_intr[4];
167 		intrv[1] = ctlr->ui_intr[5];
168 		ctlr->ui_intr = intrv;
169 	} else if (dmfoptions == DMFC_LP) {
170 		/* LP portion only */
171 
172 		cvec = (uba_hd[numuba].uh_lastiv -= 8);
173 		ctlr->ui_intr = &ctlr->ui_intr[6];
174 	} else if (dmfoptions == (DMFC_LP|DMFC_ASYNC)) {
175 		/* LP ans Async portions only */
176 
177 		cvec = (uba_hd[numuba].uh_lastiv -= 2*8);
178 		ctlr->ui_intr = &ctlr->ui_intr[4];
179 	} else {
180 		/* All other configurations get everything */
181 
182 		cvec = (uba_hd[numuba].uh_lastiv -= 4*8);
183 	}
184 	a = (dmfoptions >> 12) & 0xf;
185 	printf("dmf%d:", ctlr->ui_unit);
186 	for (i = 0; a != 0; ++i, a >>= 1) {
187 		if (a & 1)
188 			printf(" %s",dmfdevs[i]);
189 	}
190 	printf(".\n");
191 
192 	if (dmfoptions & DMFC_LP)
193 		dmfaddr->dmfl_ctrl = DMFL_RESET;
194 	return (sizeof (struct dmfdevice));
195 }
196 
197 /*
198  * Routine called to attach a dmf.
199  */
200 dmfattach(ui)
201 	struct uba_device *ui;
202 {
203 	register int cols = (ui->ui_flags>>8) & 0xff;
204 	register int lines = (ui->ui_flags>>16) & 0xff;
205 
206 	dmfsoftCAR[ui->ui_unit] = ui->ui_flags & 0xff;
207 	dmfl_softc[ui->ui_unit].dmfl_cols = cols == 0 ? DMFL_DEFCOLS : cols;
208 	dmfl_softc[ui->ui_unit].dmfl_lines = lines == 0 ? DMFL_DEFLINES : lines;
209  	if ((ui->ui_flags >> 24) & 0x1)
210  		dmfl_softc[ui->ui_unit].dmfl_format = (2 << 8);
211  	else
212  		dmfl_softc[ui->ui_unit].dmfl_format = (2 << 8) | DMFL_FORMAT;
213 	cbase[ui->ui_ubanum] = -1;
214 }
215 
216 
217 /*
218  * Open a DMF32 line, mapping the clist onto the uba if this
219  * is the first dmf on this uba.  Turn on this dmf if this is
220  * the first use of it.
221  */
222 /*ARGSUSED*/
223 dmfopen(dev, flag)
224 	dev_t dev;
225 {
226 	register struct tty *tp;
227 	register int unit, dmf;
228 	register struct dmfdevice *addr;
229 	register struct uba_device *ui;
230 	int s;
231 
232 	unit = minor(dev);
233 	if (unit & 0200)
234 		return (dmflopen(dev,flag));
235 	dmf = unit >> 3;
236 	if (unit >= NDMF*8 || (ui = dmfinfo[dmf])== 0 || ui->ui_alive == 0)
237 		return (ENXIO);
238 	tp = &dmf_tty[unit];
239 	if (tp->t_state&TS_XCLUDE && u.u_uid!=0)
240 		return (EBUSY);
241 	addr = (struct dmfdevice *)ui->ui_addr;
242 	tp->t_addr = (caddr_t)addr;
243 	tp->t_oproc = dmfstart;
244 	tp->t_state |= TS_WOPEN;
245 	/*
246 	 * While setting up state for this uba and this dmf,
247 	 * block uba resets which can clear the state.
248 	 */
249 	s = spltty();
250 	if (cbase[ui->ui_ubanum] == -1) {
251 		dmf_ubinfo[ui->ui_ubanum] =
252 		    uballoc(ui->ui_ubanum, (caddr_t)cfree,
253 			nclist*sizeof(struct cblock), 0);
254 		cbase[ui->ui_ubanum] = UBAI_ADDR(dmf_ubinfo[ui->ui_ubanum]);
255 	}
256 	if ((dmfact&(1<<dmf)) == 0) {
257 		addr->dmfcsr |= DMF_IE;
258 		dmfact |= (1<<dmf);
259 		addr->dmfrsp = dmf_timeout;
260 	}
261 	splx(s);
262 	/*
263 	 * If this is first open, initialize tty state to default.
264 	 */
265 	if ((tp->t_state&TS_ISOPEN) == 0) {
266 		ttychars(tp);
267 #ifndef PORTSELECTOR
268 		if (tp->t_ispeed == 0) {
269 #else
270 			tp->t_state |= TS_HUPCLS;
271 #endif PORTSELECTOR
272 			tp->t_ispeed = ISPEED;
273 			tp->t_ospeed = ISPEED;
274 			tp->t_flags = IFLAGS;
275 #ifndef PORTSELECTOR
276 		}
277 #endif PORTSELECTOR
278 		dmfparam(unit);
279 	}
280 	/*
281 	 * Wait for carrier, then process line discipline specific open.
282 	 */
283 	s = spltty();
284 	for (;;) {
285 		if ((dmfmctl(dev, DMF_ON, DMSET) & (DMF_CAR<<8)) ||
286 		    (dmfsoftCAR[dmf] & (1<<(unit&07))))
287 			tp->t_state |= TS_CARR_ON;
288 		if (tp->t_state & TS_CARR_ON)
289 			break;
290 		tp->t_state |= TS_WOPEN;
291 		sleep((caddr_t)&tp->t_rawq, TTIPRI);
292 	}
293 	splx(s);
294 	return ((*linesw[tp->t_line].l_open)(dev, tp));
295 }
296 
297 /*
298  * Close a DMF32 line.
299  */
300 /*ARGSUSED*/
301 dmfclose(dev, flag)
302 	dev_t dev;
303 	int flag;
304 {
305 	register struct tty *tp;
306 	register unit;
307 
308 	unit = minor(dev);
309 	if (unit & 0200) {
310 		dmflclose(dev,flag);
311 		return;
312 	}
313 
314 	tp = &dmf_tty[unit];
315 	(*linesw[tp->t_line].l_close)(tp);
316 	(void) dmfmctl(unit, DMF_BRK, DMBIC);
317 	if (tp->t_state&TS_HUPCLS || (tp->t_state&TS_ISOPEN)==0)
318 		(void) dmfmctl(unit, DMF_OFF, DMSET);
319 	ttyclose(tp);
320 }
321 
322 dmfread(dev, uio)
323 	dev_t dev;
324 	struct uio *uio;
325 {
326 	register struct tty *tp;
327 
328 	if (minor(dev) & 0200)
329 		return(ENXIO);
330 	tp = &dmf_tty[minor(dev)];
331 	return ((*linesw[tp->t_line].l_read)(tp, uio));
332 }
333 
334 dmfwrite(dev, uio)
335 	dev_t dev;
336 	struct uio *uio;
337 {
338 	register struct tty *tp;
339 
340 	if (minor(dev) & 0200)
341 		return (dmflwrite(dev,uio));
342 	tp = &dmf_tty[minor(dev)];
343 	return ((*linesw[tp->t_line].l_write)(tp, uio));
344 }
345 
346 /*
347  * DMF32 receiver interrupt.
348  */
349 dmfrint(dmf)
350 	int dmf;
351 {
352 	register c;
353 	register struct tty *tp;
354 	register struct dmfdevice *addr;
355 	register struct tty *tp0;
356 	int unit;
357 	int overrun = 0;
358 	register struct uba_device *ui;
359 
360 	ui = dmfinfo[dmf];
361 	if (ui == 0 || ui->ui_alive == 0)
362 		return;
363 	addr = (struct dmfdevice *)ui->ui_addr;
364 	tp0 = &dmf_tty[dmf * 8];
365 	/*
366 	 * Loop fetching characters from the silo for this
367 	 * dmf until there are no more in the silo.
368 	 */
369 	while ((c = addr->dmfrbuf) < 0) {
370 
371 		unit = (c >> 8) & 07;
372 		tp = tp0 + unit;
373 		if (c & DMF_DSC) {
374 			addr->dmfcsr = DMF_IE | DMFIR_TBUF | unit;
375 			if (addr->dmfrms & DMF_CAR)
376 				(void)(*linesw[tp->t_line].l_modem)(tp, 1);
377 			else if ((dmfsoftCAR[dmf] & (1 << unit)) == 0 &&
378 			    (*linesw[tp->t_line].l_modem)(tp, 0) == 0) {
379 				addr->dmfcsr = DMF_IE | DMFIR_LCR | unit;
380 				addr->dmflctms = DMFLCR_ENA;
381 			}
382 			continue;
383 		}
384 		if ((tp->t_state&TS_ISOPEN) == 0) {
385 			wakeup((caddr_t)&tp->t_rawq);
386 #ifdef PORTSELECTOR
387 			if ((tp->t_state & TS_WOPEN) == 0)
388 #endif
389 				continue;
390 		}
391 		if (c & (DMF_PE|DMF_DO|DMF_FE)) {
392 			if (c & DMF_PE)
393 				if ((tp->t_flags & (EVENP|ODDP)) == EVENP
394 			 	|| (tp->t_flags & (EVENP|ODDP)) == ODDP)
395 					continue;
396 			if ((c & DMF_DO) && overrun == 0) {
397 				log(LOG_WARNING, "dmf%d: silo overflow\n", dmf);
398 				overrun = 1;
399 			}
400 			if (c & DMF_FE)
401 				/*
402 			 	* At framing error (break) generate
403 			 	* a null (in raw mode, for getty), or a
404 			 	* interrupt (in cooked/cbreak mode).
405 			 	*/
406 				if (tp->t_flags & RAW)
407 					c = 0;
408 				else
409 					c = tp->t_intrc;
410 		}
411 #if NBK > 0
412 		if (tp->t_line == NETLDISC) {
413 			c &= 0177;
414 			BKINPUT(c, tp);
415 		} else
416 #endif
417 			(*linesw[tp->t_line].l_rint)(c, tp);
418 	}
419 }
420 
421 /*
422  * Ioctl for DMF32.
423  */
424 /*ARGSUSED*/
425 dmfioctl(dev, cmd, data, flag)
426 	dev_t dev;
427 	caddr_t data;
428 {
429 	register struct tty *tp;
430 	register int unit = minor(dev);
431 	int error;
432 
433 	if (unit & 0200)
434 		return (ENOTTY);
435 	tp = &dmf_tty[unit];
436 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
437 	if (error >= 0)
438 		return (error);
439 	error = ttioctl(tp, cmd, data, flag);
440 	if (error >= 0) {
441 		if (cmd == TIOCSETP || cmd == TIOCSETN || cmd == TIOCLBIS ||
442 		    cmd == TIOCLBIC || cmd == TIOCLSET)
443 			dmfparam(unit);
444 		return (error);
445 	}
446 	switch (cmd) {
447 
448 	case TIOCSBRK:
449 		(void) dmfmctl(dev, DMF_BRK, DMBIS);
450 		break;
451 
452 	case TIOCCBRK:
453 		(void) dmfmctl(dev, DMF_BRK, DMBIC);
454 		break;
455 
456 	case TIOCSDTR:
457 		(void) dmfmctl(dev, DMF_DTR|DMF_RTS, DMBIS);
458 		break;
459 
460 	case TIOCCDTR:
461 		(void) dmfmctl(dev, DMF_DTR|DMF_RTS, DMBIC);
462 		break;
463 
464 	case TIOCMSET:
465 		(void) dmfmctl(dev, dmtodmf(*(int *)data), DMSET);
466 		break;
467 
468 	case TIOCMBIS:
469 		(void) dmfmctl(dev, dmtodmf(*(int *)data), DMBIS);
470 		break;
471 
472 	case TIOCMBIC:
473 		(void) dmfmctl(dev, dmtodmf(*(int *)data), DMBIC);
474 		break;
475 
476 	case TIOCMGET:
477 		*(int *)data = dmftodm(dmfmctl(dev, 0, DMGET));
478 		break;
479 
480 	default:
481 		return (ENOTTY);
482 	}
483 	return (0);
484 }
485 
486 dmtodmf(bits)
487 	register int bits;
488 {
489 	register int b;
490 
491 	b = bits & 012;
492 	if (bits & DML_ST) b |= DMF_RATE;
493 	if (bits & DML_RTS) b |= DMF_RTS;
494 	if (bits & DML_USR) b |= DMF_USRW;
495 	return(b);
496 }
497 
498 dmftodm(bits)
499 	register int bits;
500 {
501 	register int b;
502 
503 	b = (bits & 012) | ((bits >> 7) & 0760) | DML_LE;
504 	if (bits & DMF_USRR) b |= DML_USR;
505 	if (bits & DMF_RTS) b |= DML_RTS;
506 	return(b);
507 }
508 
509 
510 /*
511  * Set parameters from open or stty into the DMF hardware
512  * registers.
513  */
514 dmfparam(unit)
515 	register int unit;
516 {
517 	register struct tty *tp;
518 	register struct dmfdevice *addr;
519 	register int lpar, lcr;
520 	int s;
521 
522 	tp = &dmf_tty[unit];
523 	addr = (struct dmfdevice *)tp->t_addr;
524 	/*
525 	 * Block interrupts so parameters will be set
526 	 * before the line interrupts.
527 	 */
528 	s = spltty();
529 	addr->dmfcsr = (unit&07) | DMFIR_LCR | DMF_IE;
530 	if ((tp->t_ispeed)==0) {
531 		tp->t_state |= TS_HUPCLS;
532 		(void) dmfmctl(unit, DMF_OFF, DMSET);
533 		splx(s);
534 		return;
535 	}
536 	lpar = (dmf_speeds[tp->t_ospeed]<<12) | (dmf_speeds[tp->t_ispeed]<<8);
537 	lcr = DMFLCR_ENA;
538 	if ((tp->t_ispeed) == B134)
539 		lpar |= BITS6|PENABLE;
540 	else if (tp->t_flags & (RAW|LITOUT|PASS8))
541 		lpar |= BITS8;
542 	else {
543 		lpar |= BITS7|PENABLE;
544 		/* CHECK FOR XON/XOFF AND SET lcr |= DMF_AUTOX; */
545 	}
546 	if (tp->t_flags&EVENP)
547 		lpar |= EPAR;
548 	if ((tp->t_ospeed) == B110)
549 		lpar |= TWOSB;
550 	lpar |= (unit&07);
551 	addr->dmflpr = lpar;
552 	addr->dmflctms = (addr->dmflctms &~ 0xff) | lcr;
553 	splx(s);
554 }
555 
556 /*
557  * DMF32 transmitter interrupt.
558  * Restart the idle line.
559  */
560 dmfxint(dmf)
561 	int dmf;
562 {
563 	int unit0 = dmf * 8;
564 	struct tty *tp0 = &dmf_tty[unit0];
565 	register struct tty *tp;
566 	register struct dmfdevice *addr;
567 	register struct uba_device *ui;
568 	register int t;
569 	short cntr;
570 
571 	ui = dmfinfo[dmf];
572 	addr = (struct dmfdevice *)ui->ui_addr;
573 	while ((t = addr->dmfcsr) & DMF_TI) {
574 		if (t & DMF_NXM)
575 			/* SHOULD RESTART OR SOMETHING... */
576 			printf("dmf%d: NXM line %d\n", dmf, t >> 8 & 7);
577 		t = t >> 8 & 7;
578 		tp = tp0 + t;
579 		tp->t_state &= ~TS_BUSY;
580 		if (tp->t_state&TS_FLUSH)
581 			tp->t_state &= ~TS_FLUSH;
582 		else if (dmf_dma[unit0 + t])
583 			ndflush(&tp->t_outq, (int)dmf_dma[unit0 + t]);
584 		dmf_dma[unit0 + t] = 0;
585 		if (tp->t_line)
586 			(*linesw[tp->t_line].l_start)(tp);
587 		else
588 			dmfstart(tp);
589 	}
590 }
591 
592 /*
593  * Start (restart) transmission on the given DMF32 line.
594  */
595 dmfstart(tp)
596 	register struct tty *tp;
597 {
598 	register struct dmfdevice *addr;
599 	register int unit, nch;
600 	int s;
601 	register int dmf;
602 
603 	unit = minor(tp->t_dev);
604 	dmf = unit >> 3;
605 	unit &= 07;
606 	addr = (struct dmfdevice *)tp->t_addr;
607 
608 	/*
609 	 * Must hold interrupts in following code to prevent
610 	 * state of the tp from changing.
611 	 */
612 	s = spltty();
613 	/*
614 	 * If it's currently active, or delaying, no need to do anything.
615 	 */
616 	if (tp->t_state&(TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
617 		goto out;
618 	/*
619 	 * If there are still characters in the silo,
620 	 * just reenable the transmitter.
621 	 */
622 	addr->dmfcsr = DMF_IE | DMFIR_TBUF | unit;
623 	if (addr->dmftsc) {
624 		addr->dmfcsr = DMF_IE | DMFIR_LCR | unit;
625 		addr->dmflctms = addr->dmflctms | DMF_TE;
626 		tp->t_state |= TS_BUSY;
627 		goto out;
628 	}
629 	/*
630 	 * If there are sleepers, and output has drained below low
631 	 * water mark, wake up the sleepers.
632 	 */
633 	if (tp->t_outq.c_cc<=TTLOWAT(tp)) {
634 		if (tp->t_state&TS_ASLEEP) {
635 			tp->t_state &= ~TS_ASLEEP;
636 			wakeup((caddr_t)&tp->t_outq);
637 		}
638 		if (tp->t_wsel) {
639 			selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
640 			tp->t_wsel = 0;
641 			tp->t_state &= ~TS_WCOLL;
642 		}
643 	}
644 	/*
645 	 * Now restart transmission unless the output queue is
646 	 * empty.
647 	 */
648 	if (tp->t_outq.c_cc == 0)
649 		goto out;
650 	if (tp->t_flags & (RAW|LITOUT))
651 		nch = ndqb(&tp->t_outq, 0);
652 	else {
653 		if ((nch = ndqb(&tp->t_outq, 0200)) == 0) {
654 			/*
655 		 	* If first thing on queue is a delay process it.
656 		 	*/
657 			nch = getc(&tp->t_outq);
658 			timeout(ttrstrt, (caddr_t)tp, (nch&0x7f)+6);
659 			tp->t_state |= TS_TIMEOUT;
660 			goto out;
661 		}
662 	}
663 	/*
664 	 * If characters to transmit, restart transmission.
665 	 */
666 	if (nch >= dmf_mindma) {
667 		register car;
668 
669 		dmf_dma[minor(tp->t_dev)] = nch;
670 		addr->dmfcsr = DMF_IE | DMFIR_LCR | unit;
671 		addr->dmflctms = addr->dmflctms | DMF_TE;
672 		car = UBACVT(tp->t_outq.c_cf, dmfinfo[dmf]->ui_ubanum);
673 		addr->dmfcsr = DMF_IE | DMFIR_TBA | unit;
674 		addr->dmftba = car;
675 		addr->dmftcc = ((car >> 2) & 0xc000) | nch;
676 		tp->t_state |= TS_BUSY;
677 	} else if (nch) {
678 		register char *cp = tp->t_outq.c_cf;
679 		register int i;
680 
681 		dmf_dma[minor(tp->t_dev)] = 0;
682 		nch = MIN(nch, DMF_SILOCNT);
683 		addr->dmfcsr = DMF_IE | DMFIR_LCR | unit;
684 		addr->dmflctms = addr->dmflctms | DMF_TE;
685 		addr->dmfcsr = DMF_IE | DMFIR_TBUF | unit;
686 		for (i = 0; i < nch; i++)
687 			addr->dmftbuf = *cp++;
688 		ndflush(&tp->t_outq, nch);
689 		tp->t_state |= TS_BUSY;
690 	}
691 out:
692 	splx(s);
693 }
694 
695 /*
696  * Stop output on a line, e.g. for ^S/^Q or output flush.
697  */
698 /*ARGSUSED*/
699 dmfstop(tp, flag)
700 	register struct tty *tp;
701 {
702 	register struct dmfdevice *addr;
703 	register unit = minor(tp->t_dev) & 7;
704 	int s;
705 
706 	addr = (struct dmfdevice *)tp->t_addr;
707 	/*
708 	 * Block input/output interrupts while messing with state.
709 	 */
710 	s = spltty();
711 	if (flag) {
712 		addr->dmfcsr = DMF_IE | DMFIR_TBUF | unit;
713 		if (addr->dmftsc) {
714 			/*
715 			 * Flush regardless of whether we're transmitting
716 			 * (TS_BUSY), if the silo contains untransmitted
717 			 * characters.
718 			 */
719 			addr->dmfcsr = DMFIR_LCR | unit | DMF_IE;
720 			addr->dmflctms = addr->dmflctms | DMF_TE | DMF_FLUSH;
721 			/* this will interrupt so let dmfxint handle the rest */
722 			tp->t_state |= TS_FLUSH|TS_BUSY;
723 		}
724 	} else {
725 		if (tp->t_state & TS_BUSY) {
726 			/*
727 			 * Stop transmission by disabling
728 			 * the transmitter.  We'll pick up where we
729 			 * left off by reenabling in dmfstart.
730 			 */
731 			addr->dmfcsr = DMFIR_LCR | unit | DMF_IE;
732 			addr->dmflctms = addr->dmflctms &~ DMF_TE;
733 			/* no interrupt here */
734 			tp->t_state &= ~TS_BUSY;
735 		}
736 	}
737 	splx(s);
738 }
739 
740 /*
741  * DMF32 modem control
742  */
743 dmfmctl(dev, bits, how)
744 	dev_t dev;
745 	int bits, how;
746 {
747 	register struct dmfdevice *dmfaddr;
748 	register int unit, mbits, lcr;
749 	int s;
750 
751 	unit = minor(dev);
752 	dmfaddr = (struct dmfdevice *)(dmf_tty[unit].t_addr);
753 	unit &= 07;
754 	s = spltty();
755 	dmfaddr->dmfcsr = DMF_IE | DMFIR_TBUF | unit;
756 	mbits = dmfaddr->dmfrms << 8;
757 	dmfaddr->dmfcsr = DMF_IE | DMFIR_LCR | unit;
758 	lcr = dmfaddr->dmflctms;
759 	mbits |= (lcr & 0xff00) >> 8;
760 	switch (how) {
761 	case DMSET:
762 		mbits = (mbits &0xff00) | bits;
763 		break;
764 
765 	case DMBIS:
766 		mbits |= bits;
767 		break;
768 
769 	case DMBIC:
770 		mbits &= ~bits;
771 		break;
772 
773 	case DMGET:
774 		(void) splx(s);
775 		return(mbits);
776 	}
777 	if (mbits & DMF_BRK)
778 		lcr |= DMF_RBRK;
779 	else
780 		lcr &= ~DMF_RBRK;
781 	dmfaddr->dmflctms = ((mbits & 037) << 8) | (lcr & 0xff);
782 	(void) splx(s);
783 	return(mbits);
784 }
785 
786 /*
787  * Reset state of driver if UBA reset was necessary.
788  * Reset the csr, lpr, and lcr registers on open lines, and
789  * restart transmitters.
790  */
791 dmfreset(uban)
792 	int uban;
793 {
794 	register int dmf, unit;
795 	register struct tty *tp;
796 	register struct uba_device *ui;
797 	register struct dmfdevice *addr;
798 	int i;
799 
800 	for (dmf = 0; dmf < NDMF; dmf++) {
801 		ui = dmfinfo[dmf];
802 		if (ui == 0 || ui->ui_alive == 0 || ui->ui_ubanum != uban)
803 			continue;
804 		printf(" dmf%d", dmf);
805 		if (dmf_ubinfo[uban]) {
806 			dmf_ubinfo[uban] = uballoc(uban, (caddr_t)cfree,
807 			    nclist*sizeof (struct cblock), 0);
808 			cbase[uban] = UBAI_ADDR(dmf_ubinfo[uban]);
809 		}
810 		addr = (struct dmfdevice *)ui->ui_addr;
811 		addr->dmfcsr = DMF_IE;
812 		addr->dmfrsp = dmf_timeout;
813 		unit = dmf * 8;
814 		for (i = 0; i < 8; i++) {
815 			tp = &dmf_tty[unit];
816 			if (tp->t_state & (TS_ISOPEN|TS_WOPEN)) {
817 				dmfparam(unit);
818 				(void) dmfmctl(unit, DMF_ON, DMSET);
819 				tp->t_state &= ~TS_BUSY;
820 				dmfstart(tp);
821 			}
822 			unit++;
823 		}
824 	}
825 }
826 
827 /*
828  * dmflopen -- open the line printer port on a dmf32
829  */
830 /* ARGSUSED */
831 dmflopen(dev, flag)
832 	dev_t dev;
833 	int flag;
834 {
835 	register int dmf;
836 	register struct dmfl_softc *sc;
837 	register struct uba_device *ui;
838 	register struct dmfdevice *addr;
839 
840 	dmf = DMFL_UNIT(dev);
841 	if (dmf >= NDMF || (ui = dmfinfo[dmf]) == 0 || ui->ui_alive == 0)
842 		return (ENXIO);
843 	sc = &dmfl_softc[dmf];
844 	if (sc->dmfl_state & OPEN)
845 		return (EBUSY);
846 	addr = (struct dmfdevice *)ui->ui_addr;
847 	if (addr->dmfl_ctrl & DMFL_OFFLINE) {
848 #ifdef notdef
849 		log(LOG_WARNING, "dmf%d: line printer offline/jammed\n",
850 			dmf);
851 #endif
852 		return (EIO);
853 	}
854 	if ((addr->dmfl_ctrl & DMFL_CONV)) {
855 		log(LOG_WARNING, "dmf%d: line printer disconnected\n", dmf);
856 		return (EIO);
857 	}
858 
859 	addr->dmfl_ctrl = 0;
860 	sc->dmfl_state |= OPEN;
861 	return (0);
862 }
863 
864 /* ARGSUSED */
865 dmflclose(dev, flag)
866 	dev_t dev;
867 	int flag;
868 {
869 	register int dmf = DMFL_UNIT(dev);
870 	register struct dmfl_softc *sc = &dmfl_softc[dmf];
871 	register struct uba_device *ui = dmfinfo[dmf];
872 
873 	sc->dmfl_state = 0;
874 	if (sc->dmfl_info != 0)
875 		ubarelse((int)ui->ui_ubanum, &sc->dmfl_info);
876 
877 	((struct dmfdevice *)ui->ui_addr)->dmfl_ctrl = 0;
878 }
879 
880 dmflwrite(dev, uio)
881 	dev_t dev;
882 	struct uio *uio;
883 {
884 	register int n;
885 	register int error;
886 	register struct dmfl_softc *sc;
887 
888 	sc = &dmfl_softc[DMFL_UNIT(dev)];
889 	if (sc->dmfl_state & ERROR)
890 		return (EIO);
891 	while (n = (unsigned)uio->uio_resid) {
892 		if (n > DMFL_BUFSIZ) {
893 			n = DMFL_BUFSIZ;
894 			sc->dmfl_state |= MOREIO;
895 		} else
896 			sc->dmfl_state &= ~MOREIO;
897 		if (error = uiomove(sc->dmfl_buf, (int)n, UIO_WRITE, uio))
898 			return (error);
899 		if (error = dmflout(dev, sc->dmfl_buf, n))
900 			return (error);
901 	}
902 	return (0);
903 }
904 
905 
906 /*
907  * dmflout -- start io operation to dmf line printer
908  *		cp is addr of buf of n chars to be sent.
909  *
910  *	-- dmf will be put in formatted output mode, this will
911  *		be selectable from an ioctl if the
912  *		need ever arises.
913  */
914 dmflout(dev, cp, n)
915 	dev_t dev;
916 	char *cp;
917 	int n;
918 {
919 	register struct dmfl_softc *sc;
920 	register int dmf;
921 	register struct uba_device *ui;
922 	register struct dmfdevice *d;
923 	int s;
924 
925 	dmf = DMFL_UNIT(dev);
926 	sc = &dmfl_softc[dmf];
927 	if (sc->dmfl_state & ERROR)
928 		return (EIO);
929 	ui = dmfinfo[dmf];
930 	/*
931 	 * allocate unibus resources, will be released when io
932 	 * operation is done.
933 	 */
934 	if (sc->dmfl_info == 0)
935 		sc->dmfl_info = uballoc(ui->ui_ubanum, cp, n, 0);
936 	d = (struct dmfdevice *)ui->ui_addr;
937 	d->dmfl_ctrl = sc->dmfl_format;		/* indir reg 2 */
938 	/* indir reg auto increments on r/w */
939 	/* SO DON'T CHANGE THE ORDER OF THIS CODE */
940 	d->dmfl_indrct = 0;			/* prefix chars & num */
941 	d->dmfl_indrct = 0;			/* suffix chars & num */
942 	d->dmfl_indrct = sc->dmfl_info; 	/* dma lo 16 bits addr */
943 	d->dmfl_indrct = -n;			/* number of chars */
944 
945 	d->dmfl_indrct = ((sc->dmfl_info>>16)&3) | DMFL_OPTIONS;
946 						/* dma hi 2 bits addr */
947 	d->dmfl_indrct = sc->dmfl_lines 	/* lines per page */
948 		| (sc->dmfl_cols<<8);		/* carriage width */
949 	sc->dmfl_state |= ASLP;
950 	s = spltty();
951 	d->dmfl_ctrl |= DMFL_PEN | DMFL_IE;
952 	while (sc->dmfl_state & ASLP) {
953 		sleep(sc->dmfl_buf, PZERO + 8);
954 		while (sc->dmfl_state & ERROR) {
955 			timeout(dmflint, (caddr_t)dmf, 10 * hz);
956 			sleep((caddr_t)&sc->dmfl_state, PZERO + 8);
957 		}
958 	}
959 	splx(s);
960 	return (0);
961 }
962 
963 /*
964  * dmflint -- handle an interrupt from the line printer part of the dmf32
965  */
966 dmflint(dmf)
967 	int dmf;
968 {
969 	register struct uba_device *ui;
970 	register struct dmfl_softc *sc;
971 	register struct dmfdevice *d;
972 	short dmfl_stats;
973 
974 	ui = dmfinfo[dmf];
975 	sc = &dmfl_softc[dmf];
976 	d = (struct dmfdevice *)ui->ui_addr;
977 
978 	d->dmfl_ctrl &= ~DMFL_IE;
979 	dmfl_stats = d->dmfl_ctrl;
980 	if (sc->dmfl_state & ERROR) {
981 		if ((dmfl_stats & DMFL_OFFLINE) == 0)
982 			sc->dmfl_state &= ~ERROR;
983 		wakeup((caddr_t)&sc->dmfl_state);
984 		return;
985 	}
986 	if (dmfl_stats & DMFL_DMAERR)
987 		log(LOG_WARNING, "dmf%d: NXM\n", dmf);
988 	if (dmfl_stats & DMFL_OFFLINE) {
989 		log(LOG_WARNING, "dmf%d: printer error\n", dmf);
990 		sc->dmfl_state |= ERROR;
991 	}
992 #ifdef notdef
993 	if (dmfl_stats & DMFL_PDONE) {
994 		printf("bytes= %d\n", d->dmfl_indrct);
995 		printf("lines= %d\n", d->dmfl_indrct);
996 	}
997 #endif
998 	sc->dmfl_state &= ~ASLP;
999 	wakeup((caddr_t)sc->dmfl_buf);
1000 	if (sc->dmfl_info && (sc->dmfl_state & MOREIO) == 0)
1001 		ubarelse(ui->ui_ubanum, &sc->dmfl_info);
1002 }
1003 
1004 /* stubs for interrupt routines for devices not yet supported */
1005 
1006 dmfsrint()
1007 {
1008 	printf("dmfsrint\n");
1009 }
1010 
1011 dmfsxint()
1012 {
1013 	printf("dmfsxint\n");
1014 }
1015 
1016 dmfdaint()
1017 {
1018 	printf("dmfdaint\n");
1019 }
1020 
1021 dmfdbint()
1022 {
1023 	printf("dmfdbint\n");
1024 }
1025 #endif NDMF
1026