xref: /netbsd/sys/dev/qbus/qd.c (revision bf9ec67e)
1 /*	$NetBSD: qd.c,v 1.24 2002/03/17 19:41:01 atatat Exp $	*/
2 
3 /*-
4  * Copyright (c) 1988 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)qd.c	7.1 (Berkeley) 6/28/91
36  */
37 
38 /************************************************************************
39 *									*
40 *			Copyright (c) 1985-1988 by			*
41 *		Digital Equipment Corporation, Maynard, MA		*
42 *			All rights reserved.				*
43 *									*
44 *   This software is furnished under a license and may be used and	*
45 *   copied  only  in accordance with the terms of such license and	*
46 *   with the  inclusion  of  the  above  copyright  notice.   This	*
47 *   software  or  any  other copies thereof may not be provided or	*
48 *   otherwise made available to any other person.  No title to and	*
49 *   ownership of the software is hereby transferred.			*
50 *									*
51 *   The information in this software is subject to change  without	*
52 *   notice  and should not be construed as a commitment by Digital	*
53 *   Equipment Corporation.						*
54 *									*
55 *   Digital assumes no responsibility for the use  or  reliability	*
56 *   of its software on equipment which is not supplied by Digital.	*
57 *									*
58 *************************************************************************/
59 
60 /*
61  * qd.c - QDSS display driver for VAXSTATION-II GPX workstation
62  */
63 
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: qd.c,v 1.24 2002/03/17 19:41:01 atatat Exp $");
66 
67 #include "opt_ddb.h"
68 
69 #include "qd.h"
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/conf.h>
74 #include <sys/tty.h>
75 #include <sys/kernel.h>
76 #include <sys/device.h>
77 #include <sys/poll.h>
78 #include <sys/buf.h>
79 
80 #include <uvm/uvm_extern.h>
81 
82 #include <dev/cons.h>
83 
84 #include <machine/bus.h>
85 #include <machine/scb.h>
86 
87 #ifdef __vax__
88 #include <machine/sid.h>
89 #include <machine/cpu.h>
90 #include <machine/pte.h>
91 #endif
92 
93 #include <dev/qbus/ubavar.h>
94 
95 #include <dev/qbus/qduser.h>
96 #include <dev/qbus/qdreg.h>
97 #include <dev/qbus/qdioctl.h>
98 
99 #include "ioconf.h"
100 
101 /*
102  * QDSS driver status flags for tracking operational state
103  */
104 struct qdflags {
105 	u_int inuse;		/* which minor dev's are in use now */
106 	u_int config;		/* I/O page register content */
107 	u_int mapped;		/* user mapping status word */
108 	u_int kernel_loop;	/* if kernel console is redirected */
109 	u_int user_dma;		/* DMA from user space in progress */
110 	u_short pntr_id;	/* type code of pointing device */
111 	u_short duart_imask;	/* shadowing for duart intrpt mask reg */
112 	u_short adder_ie;	/* shadowing for adder intrpt enbl reg */
113 	u_short curs_acc;	/* cursor acceleration factor */
114 	u_short curs_thr;	/* cursor acceleration threshold level */
115 	u_short tab_res;	/* tablet resolution factor */
116 	u_short selmask;	/* mask for active qd select entries */
117 };
118 
119 /*
120  * Softc struct to keep track of all states in this driver.
121  */
122 struct	qd_softc {
123 	struct	device sc_dev;
124 	bus_space_tag_t	sc_iot;
125 	bus_space_handle_t sc_ioh;
126 	bus_dma_tag_t	sc_dmat;
127 };
128 
129 /*
130  * bit definitions for 'inuse' entry
131  */
132 #define CONS_DEV	0x01
133 #define GRAPHIC_DEV	0x04
134 
135 /*
136  * bit definitions for 'mapped' member of flag structure
137  */
138 #define MAPDEV		0x01		/* hardware is mapped */
139 #define MAPDMA		0x02		/* DMA buffer mapped */
140 #define MAPEQ		0x04		/* event queue buffer mapped */
141 #define MAPSCR		0x08		/* scroll param area mapped */
142 #define MAPCOLOR	0x10		/* color map writing buffer mapped */
143 
144 /*
145  * bit definitions for 'selmask' member of qdflag structure
146  */
147 #define SEL_READ	0x01		/* read select is active */
148 #define SEL_WRITE	0x02		/* write select is active */
149 
150 /*
151  * constants used in shared memory operations
152  */
153 #define EVENT_BUFSIZE  1024	/* # of bytes per device's event buffer */
154 #define MAXEVENTS  ( (EVENT_BUFSIZE - sizeof(struct qdinput))	 \
155 	/ sizeof(struct _vs_event) )
156 #define DMA_BUFSIZ	(1024 * 10)
157 #define COLOR_BUFSIZ  ((sizeof(struct color_buf) + 512) & ~0x01FF)
158 
159 /*
160  * reference to an array of "uba_device" structures built by the auto
161  * configuration program.  The uba_device structure decribes the device
162  * sufficiently for the driver to talk to it.  The auto configuration code
163  * fills in the uba_device structures (located in ioconf.c) from user
164  * maintained info.
165  */
166 struct uba_device *qdinfo[NQD];  /* array of pntrs to each QDSS's */
167 struct tty *qd_tty[NQD*4];	/* teletype structures for each.. */
168 volatile char *qvmem[NQD];
169 volatile struct pte *QVmap[NQD];
170 #define CHUNK	  (64 * 1024)
171 #define QMEMSIZE  (1024 * 1024 * 4)	/* 4 meg */
172 
173 /*
174  * static storage used by multiple functions in this code
175  */
176 int Qbus_unmap[NQD];		/* Qbus mapper release code */
177 struct qdmap qdmap[NQD];	/* QDSS register map structure */
178 struct qdflags qdflags[NQD];	/* QDSS register map structure */
179 caddr_t qdbase[NQD];		/* base address of each QDSS unit */
180 struct buf qdbuf[NQD];		/* buf structs used by strategy */
181 short qdopened[NQD];		/* graphics device is open exclusive use */
182 
183 /*
184  * the array "event_shared[]" is made up of a number of event queue buffers
185  * equal to the number of QDSS's configured into the running kernel (NQD).
186  * Each event queue buffer begins with an event queue header (struct qdinput)
187  * followed by a group of event queue entries (struct _vs_event).  The array
188  * "*eq_header[]" is an array of pointers to the start of each event queue
189  * buffer in "event_shared[]".
190  */
191 #define EQSIZE ((EVENT_BUFSIZE * NQD) + 512)
192 
193 char event_shared[EQSIZE];	    /* reserve space for event bufs */
194 struct qdinput *eq_header[NQD];     /* event queue header pntrs */
195 
196 /*
197  * This allocation method reserves enough memory pages for NQD shared DMA I/O
198  * buffers.  Each buffer must consume an integral number of memory pages to
199  * guarantee that a following buffer will begin on a page boundary.  Also,
200  * enough space is allocated so that the FIRST I/O buffer can start at the
201  * 1st page boundary after "&DMA_shared".  Page boundaries are used so that
202  * memory protections can be turned on/off for individual buffers.
203  */
204 #define IOBUFSIZE  ((DMA_BUFSIZ * NQD) + 512)
205 
206 char DMA_shared[IOBUFSIZE];	    /* reserve I/O buffer space */
207 struct DMAreq_header *DMAheader[NQD];  /* DMA buffer header pntrs */
208 
209 /*
210  * The driver assists a client in scroll operations by loading dragon
211  * registers from an interrupt service routine.	The loading is done using
212  * parameters found in memory shrade between the driver and it's client.
213  * The scroll parameter structures are ALL loacted in the same memory page
214  * for reasons of memory economy.
215  */
216 char scroll_shared[2 * 512];	/* reserve space for scroll structs */
217 struct scroll *scroll[NQD];	/* pointers to scroll structures */
218 
219 /*
220  * the driver is programmable to provide the user with color map write
221  * services at VSYNC interrupt time.  At interrupt time the driver loads
222  * the color map with any user-requested load data found in shared memory
223  */
224 #define COLOR_SHARED  ((COLOR_BUFSIZ * NQD) + 512)
225 
226 char color_shared[COLOR_SHARED];      /* reserve space: color bufs */
227 struct color_buf *color_buf[NQD];     /* pointers to color bufs */
228 
229 /*
230  * mouse input event structures
231  */
232 struct mouse_report last_rep[NQD];
233 struct mouse_report current_rep[NQD];
234 
235 struct selinfo qdrsel[NQD]; 	/* process waiting for select */
236 struct _vs_cursor cursor[NQD];	/* console cursor */
237 int qdcount = 0;		/* count of successfully probed qd's */
238 int nNQD = NQD;
239 int DMAbuf_size = DMA_BUFSIZ;
240 int QDlast_DMAtype;             /* type of the last DMA operation */
241 
242 /* #define QDSSMAJOR	41 */	/* QDSS major device number.  We don't care! */
243 
244 /*
245  * macro to get system time.  Used to time stamp event queue entries
246  */
247 #define TOY ((time.tv_sec * 100) + (time.tv_usec / 10000))
248 
249 void qd_attach __P((struct device *, struct device *, void *));
250 static int qd_match __P((struct device *, struct cfdata *, void *));
251 
252 static void qddint __P((void *));	/* DMA gate array intrpt service */
253 static void qdaint __P((void *));	/* Dragon ADDER intrpt service */
254 static void qdiint __P((void *));
255 
256 #define QDPRIOR (PZERO-1)		/* must be negative */
257 #define FALSE	0
258 #ifdef TRUE
259 #undef TRUE
260 #endif
261 #define TRUE	~FALSE
262 #define BAD	-1
263 #define GOOD	0
264 
265 /*
266  * macro to create a system virtual page number from system virtual adrs
267  */
268 #define VTOP(x)  (((int)x & ~0xC0000000) >> VAX_PGSHIFT)
269 
270 /*
271  * QDSS register address offsets from start of QDSS address space
272  */
273 #define QDSIZE	 (52 * 1024)	/* size of entire QDSS foot print */
274 #define TMPSIZE  (16 * 1024)	/* template RAM is 8k SHORT WORDS */
275 #define TMPSTART 0x8000 	/* offset of template RAM from base adrs */
276 #define REGSIZE  (5 * 512)	/* regs touch 2.5k (5 pages) of addr space */
277 #define REGSTART 0xC000 	/* offset of reg pages from base adrs */
278 #define ADDER	(REGSTART+0x000)
279 #define DGA	(REGSTART+0x200)
280 #define DUART	(REGSTART+0x400)
281 #define MEMCSR	(REGSTART+0x800)
282 #define CLRSIZE  (3 * 512)		/* color map size */
283 #define CLRSTART (REGSTART+0xA00)	/* color map start offset from base */
284 /*  0x0C00 really */
285 #define RED	(CLRSTART+0x000)
286 #define BLUE	(CLRSTART+0x200)
287 #define GREEN	(CLRSTART+0x400)
288 
289 
290 /*
291  * QDSS minor device numbers.  The *real* minor device numbers are in
292  * the bottom two bits of the major/minor device spec.  Bits 2 and up are
293  * used to specify the QDSS device number (ie: which one?)
294  */
295 
296 #define CONS		0
297 #define GRAPHIC 	2
298 
299 /*
300  * console cursor bitmap (white block cursor)
301  */
302 short cons_cursor[32] = {
303 	/* A */ 0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF,
304 	0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF,
305 	/* B */ 0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF,
306 	0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF, 0x00FF
307 };
308 
309 /*
310  * constants used in font operations
311  */
312 #define CHARS		190 			/* # of chars in the font */
313 #define CHAR_HEIGHT	15			/* char height in pixels */
314 #define CHAR_WIDTH	8			/* char width in pixels*/
315 #define FONT_WIDTH	(CHAR_WIDTH * CHARS)	/* font width in pixels */
316 #define ROWS		CHAR_HEIGHT
317 #define FONT_X		0			/* font's off screen adrs */
318 #define FONT_Y		(2048 - CHAR_HEIGHT)
319 
320 /* Offset to second row characters (XXX - should remove) */
321 #define FONT_OFFSET	((MAX_SCREEN_X/CHAR_WIDTH)*CHAR_HEIGHT)
322 
323 extern char q_font[];		/* reference font object code */
324 extern	u_short q_key[];	/* reference key xlation tables */
325 extern	u_short q_shift_key[];
326 extern	char *q_special[];
327 
328 /*
329  * definitions for cursor acceleration reporting
330  */
331 #define ACC_OFF 	0x01		/* acceleration is inactive */
332 
333 /*
334  * virtual console support.
335  */
336 extern struct cdevsw *consops;
337 cons_decl(qd);
338 cdev_decl(qd);
339 void setup_dragon __P((int));
340 void init_shared __P((int));
341 void clear_qd_screen __P((int));
342 void ldfont __P((int));
343 void ldcursor __P((int, short *));
344 void setup_input __P((int));
345 void blitc __P((int, u_char));
346 void scroll_up __P((volatile struct adder *));
347 void write_ID __P((volatile struct adder *, short, short));
348 int wait_status __P((volatile struct adder *, int));
349 void led_control __P((int, int, int));
350 void qdstart(struct tty *);
351 void qdearly(void);
352 int qdpolling = 0;
353 
354 /*
355  * LK-201 state storage for input console keyboard conversion to ASCII
356  */
357 struct q_keyboard {
358 	int shift;			/* state variables	*/
359 	int cntrl;
360 	int lock;
361 	int lastcode;			/* last keycode typed	*/
362 	unsigned kup[8];		/* bits for each keycode*/
363 	unsigned dkeys[8];		/* down/up mode keys	*/
364 	char last;			/* last character	*/
365 } q_keyboard;
366 
367 /*
368  * tty settings on first open
369  */
370 #define IFLAG (BRKINT|ISTRIP|IXON|IXANY|ICRNL|IMAXBEL)
371 #define OFLAG (OPOST|OXTABS|ONLCR)
372 #define LFLAG (ISIG|ICANON|ECHO|IEXTEN)
373 #define CFLAG (PARENB|CREAD|CS7|CLOCAL)
374 
375 /*
376  * Kernel virtual addresses where we can map in the QBUS io page and the
377  * QDSS memory during qdcninit.  pmap_bootstrap fills this in.
378  */
379 void *qd_ubaio;
380 
381 /* This is the QDSS unit 0 CSR.  It is hard-coded in here so that the
382  * QDSS can be used as the console.  The console routines don't get
383  * any config info.  The ROM also autodetects at this address, so
384  * the console QDSS should be at this address.  Furthermore, nothing
385  * else shuld be at this address instead because that would confuse the
386  * ROM and this driver.
387  */
388 #define QDSSCSR 0x1F00
389 
390 volatile u_short *qdaddr;         /* Virtual address for QDSS CSR */
391 
392 /*
393  * This flag is set to 1 if the console initialization (qdcninit)
394  * has been performed on qd0.  That initialization is required and must
395  * be done before the device probe routine.
396  */
397 int qd0cninited = 0, qd0iscons = 0;
398 
399 /*
400  * Do early check if the qdss is console. If not; don't allocate
401  * any memory for it in bootstrap.
402  */
403 void
404 qdearly()
405 {
406 	extern vaddr_t virtual_avail;
407 	int tmp;
408 
409 	/* Make sure we're running on a system that can have a QDSS */
410 	if (vax_boardtype == VAX_BTYP_630)  {
411 		/* Now check some undocumented flag */
412 		if ((*(int *)(0x200B801E) & 0x60) == 0)
413 			/* The KA630 isn't using a QDSS as the console,
414 			 * so we won't either */
415 			return;
416 	} else if (vax_boardtype != VAX_BTYP_650)
417 		return;
418 
419 	/* How to check for console on KA650? We assume that if there is a
420 	 * QDSS, it is console.
421 	 */
422 #define	QIOPAGE	0x20000000	/* XXX */
423 #define	UBAIOPAGES 16
424 	tmp = QIOPAGE + ubdevreg(QDSSCSR);
425 	if (badaddr((caddr_t)tmp, sizeof(short)))
426 		return;
427 
428 	MAPVIRT(qvmem[0], 64 * 1024 * NQD / VAX_NBPG);
429 	MAPVIRT(qd_ubaio, 16);
430 	pmap_map((int)qd_ubaio, QIOPAGE, QIOPAGE + UBAIOPAGES * VAX_NBPG,
431 	    VM_PROT_READ|VM_PROT_WRITE);
432 	qdaddr = (u_short *)((u_int)qd_ubaio + ubdevreg(QDSSCSR));
433 	qd0iscons = 1;
434 }
435 
436 void
437 qdcnprobe(cndev)
438 	struct  consdev *cndev;
439 {
440 	int i;
441 
442 	cndev->cn_pri = CN_DEAD;
443 
444 	if (mfpr(PR_MAPEN) == 0)
445 		return; /* Cannot use qd if vm system is OFF */
446 
447 	if (!qd0iscons)
448 		return;
449 
450 	/* Find the console device corresponding to the console QDSS */
451 	for (i = 0; i < nchrdev; i++)
452 		if (cdevsw[i].d_open == qdopen)  {
453 			      cndev->cn_dev = makedev(i,0);
454 			      cndev->cn_pri = CN_INTERNAL;
455 			      return;
456 		 }
457 	return;
458 }
459 
460 
461 /*
462  * Init QDSS as console (before probe routine)
463  */
464 void
465 qdcninit(cndev)
466 	struct  consdev *cndev;
467 {
468 	caddr_t phys_adr;		/* physical QDSS base adrs */
469 	u_int mapix;			/* index into QVmap[] array */
470 	int unit;
471 
472 	/* qdaddr must point to CSR for this unit! */
473 
474 	/* The console QDSS is QDSS unit 0 */
475 	unit = 0;
476 
477 	/*
478 	 * Map q-bus memory used by qdss. (separate map)
479 	 */
480 	mapix = QMEMSIZE - (CHUNK * (unit + 1));
481 #define	QMEM 0x30000000
482 	(int)phys_adr = QMEM + mapix;
483 	pmap_map((int)(qvmem[0]), (int)phys_adr, (int)(phys_adr + (CHUNK*NQD)),
484 				    VM_PROT_READ|VM_PROT_WRITE);
485 
486 	/*
487 	 * Set QVmap to point to page table entries for what we just
488 	 * mapped.
489 	 */
490 	QVmap[0] = (struct pte *)kvtopte(qvmem[0]);
491 
492 	/*
493 	 * tell QDSS which Q memory address base to decode
494 	 * (shifted right 16 bits - its in 64K units)
495 	 */
496 	*qdaddr = (u_short)((int)mapix >> 16);
497 	qdflags[unit].config = *(u_short *)qdaddr;
498 
499 	/*
500 	 * load qdmap struct with the virtual addresses of the QDSS elements
501 	 */
502 	qdbase[unit] = (caddr_t) (qvmem[0]);
503 	qdmap[unit].template = qdbase[unit] + TMPSTART;
504 	qdmap[unit].adder = qdbase[unit] + ADDER;
505 	qdmap[unit].dga = qdbase[unit] + DGA;
506 	qdmap[unit].duart = qdbase[unit] + DUART;
507 	qdmap[unit].memcsr = qdbase[unit] + MEMCSR;
508 	qdmap[unit].red = qdbase[unit] + RED;
509 	qdmap[unit].blue = qdbase[unit] + BLUE;
510 	qdmap[unit].green = qdbase[unit] + GREEN;
511 
512 	qdflags[unit].duart_imask = 0;	/* init shadow variables */
513 
514 	/*
515 	 * init the QDSS
516 	 */
517 
518 	*(short *)qdmap[unit].memcsr |= SYNC_ON; /* once only: turn on sync */
519 
520 	cursor[unit].x = 0;
521 	cursor[unit].y = 0;
522 	init_shared(unit);		/* init shared memory */
523 	setup_dragon(unit);		/* init the ADDER/VIPER stuff */
524 	clear_qd_screen(unit);		/* clear the screen */
525 	ldfont(unit);			/* load the console font */
526 	ldcursor(unit, cons_cursor);	/* load default cursor map */
527 	setup_input(unit);		/* init the DUART */
528 
529 	/* Set flag so probe knows */
530 	qd0cninited = 1;
531 } /* qdcninit */
532 
533 /* see <sys/device.h> */
534 struct cfattach qd_ca = {
535 	sizeof(struct qd_softc), qd_match, qd_attach
536 };
537 
538 #define	QD_RCSR(reg) \
539 	bus_space_read_2(sc->sc_iot, sc->sc_ioh, reg)
540 #define	QD_WCSR(reg, val) \
541 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, reg, val)
542 
543 /*
544  *  Configure QDSS into Q memory and make it intrpt.
545  *
546  *  side effects: QDSS gets mapped into Qbus memory space at the first
547  *		 vacant 64kb boundary counting back from the top of
548  *		 Qbus memory space (qvmem+4mb)
549  *
550  *  return: QDSS bus request level and vector address returned in
551  *	   registers by UNIX convention.
552  *
553  */
554 static int
555 qd_match(parent, match, aux)
556 	struct device *parent;
557 	struct cfdata *match;
558 	void *aux;
559 {
560 	struct qd_softc ssc;
561 	struct qd_softc *sc = &ssc;
562 	struct uba_attach_args *ua = aux;
563 	struct uba_softc *uh = (void *)parent;
564 	int unit;
565 	volatile struct dga *dga;       /* pointer to gate array structure */
566 	int vector;
567 #ifdef notdef
568 	int *ptep;			/* page table entry pointer */
569 	caddr_t phys_adr;		/* physical QDSS base adrs */
570 	u_int mapix;
571 #endif
572 
573 	/* Create a "fake" softc with only a few fields used. */
574 	sc->sc_iot = ua->ua_iot;
575 	sc->sc_ioh = ua->ua_ioh;
576 	sc->sc_dmat = ua->ua_dmat;
577 	/*
578 	 * calculate board unit number from I/O page register address
579 	 */
580 	unit = (int) (((int)sc->sc_ioh >> 1) & 0x0007);
581 
582 	/*
583 	 * QDSS regs must be mapped to Qbus memory space at a 64kb
584 	 * physical boundary.  The Qbus memory space is mapped into
585 	 * the system memory space at config time.  After config
586 	 * runs, "qvmem[0]" (ubavar.h) holds the system virtual adrs
587 	 * of the start of Qbus memory.   The Qbus memory page table
588 	 * is found via an array of pte ptrs called "QVmap[]" (ubavar.h)
589 	 * which is also loaded at config time.   These are the
590 	 * variables used below to find a vacant 64kb boundary in
591 	 * Qbus memory, and load it's corresponding physical adrs
592 	 * into the QDSS's I/O page CSR.
593 	 */
594 
595 	/*
596 	 * Only if QD is the graphics device.
597 	 */
598 
599 	/* if this QDSS is NOT the console, then do init here.. */
600 
601 	if (unit != 0) {
602 		printf("qd: can't support two qdss's (yet)\n");
603 #ifdef notdef	/* can't test */
604 		if (v_consputc != qdputc  ||  unit != 0) {
605 
606 			/*
607 			* read QDSS config info
608 			*/
609 			qdflags[unit].config = *(u_short *)reg;
610 
611 			/*
612 			* find an empty 64kb adrs boundary
613 			*/
614 
615 			qdbase[unit] = (caddr_t) (qvmem[0] + QMEMSIZE - CHUNK);
616 
617 			/*
618 			* find the cpusw entry that matches this machine.
619 			*/
620 			cpup = &cpusw[cpu];
621 			while (!(BADADDR(qdbase[unit], sizeof(short))))
622 				qdbase[unit] -= CHUNK;
623 
624 			/*
625 			* tell QDSS which Q memory address base to decode
626 			*/
627 			mapix = (int) (VTOP(qdbase[unit]) - VTOP(qvmem[0]));
628 			ptep = (int *) QVmap[0] + mapix;
629 			phys_adr = (caddr_t)(((int)*ptep&0x001FFFFF)<<VAX_PGSHIFT);
630 			*(u_short *)reg = (u_short) ((int)phys_adr >> 16);
631 
632 			/*
633 			* load QDSS adrs map with system addresses
634 			* of device regs
635 			*/
636 			qdmap[unit].template = qdbase[unit] + TMPSTART;
637 			qdmap[unit].adder = qdbase[unit] + ADDER;
638 			qdmap[unit].dga = qdbase[unit] + DGA;
639 			qdmap[unit].duart = qdbase[unit] + DUART;
640 			qdmap[unit].memcsr = qdbase[unit] + MEMCSR;
641 			qdmap[unit].red = qdbase[unit] + RED;
642 			qdmap[unit].blue = qdbase[unit] + BLUE;
643 			qdmap[unit].green = qdbase[unit] + GREEN;
644 
645 			/* device init */
646 
647 			cursor[unit].x = 0;
648 			cursor[unit].y = 0;
649 			init_shared(unit);		/* init shared memory */
650 			setup_dragon(unit); 	/* init the ADDER/VIPER stuff */
651 			ldcursor(unit, cons_cursor);	/* load default cursor map */
652 			setup_input(unit);		/* init the DUART */
653 			clear_qd_screen(unit);
654 			ldfont(unit);			/* load the console font */
655 
656 			/* once only: turn on sync */
657 
658 			*(short *)qdmap[unit].memcsr |= SYNC_ON;
659 		}
660 #endif /*notdef*/
661 	} else {
662 		/* We are dealing with qd0 */
663 
664 		if (!qd0cninited) {
665 			/*
666 			 * qd0 has not been initiallized as the console.
667 			 * We need to do some initialization now
668 			 *
669 			 * XXX
670 			 * However, if the QDSS is not the console then
671 			 * that stupid undocumented bit (see qdcnprobe)
672 			 * is cleared.  Then the QDSS refuses to work.
673 			 * (What did the ROM do to it!?)
674 			 * XXX
675 			 */
676 			 return 0;
677 
678 #if 0
679 			 qdaddr = (void *)reg;
680 
681 			 /* Lame probe for QDSS.  Should be ok for qd0 */
682 			 if (badaddr((caddr_t)qdaddr, sizeof(short)))
683 				 return 0;
684 
685 			 qdcninit(NULL);
686 #endif
687 		}
688 	}
689 
690 
691 	/*
692 	* The QDSS interrupts at HEX vectors xx0 (DMA) xx4
693 	* (ADDER) and xx8 (DUART).  Therefore, we take three
694 	* vectors from the vector pool, and then continue
695 	* to take them until we get a xx0 HEX vector.  The
696 	* pool provides vectors in contiguous decending
697 	* order.
698 	*/
699 
700 	vector = (uh->uh_lastiv -= 4*3);	/* take three vectors */
701 
702 	while (vector & 0x0F) {		   /* if lo nibble != 0.. */
703 		/* ..take another vector */
704 		vector = (uh->uh_lastiv -= 4);
705 	}
706 
707 	/*
708 	* setup DGA to do a DMA interrupt (transfer count = 0)
709 	*/
710 	dga = (struct dga *) qdmap[unit].dga;
711 	dga->csr = (short) HALT;	/* disable everything */
712 	dga->ivr = (short) vector;	/* load intrpt base vector */
713 	dga->bytcnt_lo = (short) 0;	/* DMA xfer count = 0 */
714 	dga->bytcnt_hi = (short) 0;
715 
716 	/*
717 	* turn on DMA interrupts
718 	*/
719 	dga->csr &= ~SET_DONE_FIFO;
720 	dga->csr |= DMA_IE | DL_ENB;
721 
722 	DELAY(20000);			/* wait for the intrpt */
723 	dga->csr = HALT;		/* stop the wheels */
724 
725 	/*
726 	* score this as an existing qdss
727 	*/
728 	qdcount++;
729 
730 	return 1;
731 } /* qdprobe */
732 
733 
734 void qd_attach(parent, self, aux)
735 	   struct device *parent, *self;
736 	   void *aux;
737      {
738 	struct uba_attach_args *ua = aux;
739 	int unit;	/* QDSS module # for this call */
740 
741 	printf("\n");
742 
743 	unit = self->dv_unit;		/* get QDSS number */
744 
745 	/* Set interrupt vectors for interrupt handlers */
746 
747 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec    , qddint, self);
748 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec + 4, qdaint, self);
749 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec + 8, qdiint, self);
750 
751 	/*
752 	* init "qdflags[]" for this QDSS
753 	*/
754 	qdflags[unit].inuse = 0;	/* init inuse variable EARLY! */
755 	qdflags[unit].mapped = 0;
756 	qdflags[unit].kernel_loop = -1;
757 	qdflags[unit].user_dma = 0;
758 	qdflags[unit].curs_acc = ACC_OFF;
759 	qdflags[unit].curs_thr = 128;
760 	qdflags[unit].tab_res = 2;	/* default tablet resolution factor */
761 	qdflags[unit].duart_imask = 0;	/* init shadow variables */
762 	qdflags[unit].adder_ie = 0;
763 
764 	/*
765 	* init structures used in kbd/mouse interrupt service.	This code must
766 	* come after the "init_shared()" routine has run since that routine
767 	* inits the eq_header[unit] structure used here.
768 	*/
769 
770 	/*
771 	* init the "latest mouse report" structure
772 	*/
773 	last_rep[unit].state = 0;
774 	last_rep[unit].dx = 0;
775 	last_rep[unit].dy = 0;
776 	last_rep[unit].bytcnt = 0;
777 
778 	/*
779 	* init the event queue (except mouse position)
780 	*/
781 	eq_header[unit]->header.events =
782 	    (struct _vs_event *)((int)eq_header[unit] + sizeof(struct qdinput));
783 
784 	eq_header[unit]->header.size = MAXEVENTS;
785 	eq_header[unit]->header.head = 0;
786 	eq_header[unit]->header.tail = 0;
787 
788 	/*
789 	 * open exclusive for graphics device.
790 	 */
791 	qdopened[unit] = 0;
792 
793 } /* qdattach */
794 
795 
796 /*ARGSUSED*/
797 int
798 qdopen(dev, flag, mode, p)
799 	dev_t dev;
800 	int flag, mode;
801 	struct proc *p;
802 {
803 	volatile struct dga *dga;	/* ptr to gate array struct */
804 	struct tty *tp;
805 	volatile struct duart *duart;
806 	int unit;
807 	int minor_dev;
808 
809 	minor_dev = minor(dev); /* get QDSS minor device number */
810 	unit = minor_dev >> 2;
811 
812 	/*
813 	* check for illegal conditions
814 	*/
815 	if (unit >= qd_cd.cd_ndevs || qd_cd.cd_devs[unit] == NULL)
816 		return (ENXIO);		/* no such device or address */
817 
818 	duart = (struct duart *) qdmap[unit].duart;
819 	dga = (struct dga *) qdmap[unit].dga;
820 
821 	if ((minor_dev & 0x03) == 2) {
822 		/*
823 		* this is the graphic device...
824 		*/
825 		if (qdopened[unit] != 0)
826 			return(EBUSY);
827 		else
828 			qdopened[unit] = 1;
829 		qdflags[unit].inuse |= GRAPHIC_DEV;  /* graphics dev is open */
830 		/*
831 		 * enble kbd & mouse intrpts in DUART mask reg
832 		 */
833 		qdflags[unit].duart_imask |= 0x22;
834 		duart->imask = qdflags[unit].duart_imask;
835 	} else {
836 	       /* Only one console */
837 	       if (minor_dev) return ENXIO;
838 
839 	       /* If not done already, allocate tty structure */
840 	       if (qd_tty[minor_dev] == NULL)
841 		       qd_tty[minor_dev] = ttymalloc();
842 
843 	       if (qd_tty[minor_dev] == NULL)
844 		       return ENXIO;
845 
846 	       /*
847 		* this is the console
848 		*/
849 		qdflags[unit].inuse |= CONS_DEV;  /* mark console as open */
850 		dga->csr |= CURS_ENB;
851 		qdflags[unit].duart_imask |= 0x02;
852 		duart->imask = qdflags[unit].duart_imask;
853 		/*
854 		* some setup for tty handling
855 		*/
856 		tp = qd_tty[minor_dev];
857 		/* tp->t_addr = ui->ui_addr; */
858 		tp->t_oproc = qdstart;
859 		tp->t_dev = dev;
860 		if ((tp->t_state & TS_ISOPEN) == 0) {
861 			ttychars(tp);
862 			tp->t_ispeed = B9600;
863 			tp->t_ospeed = B9600;
864 			tp->t_state = TS_ISOPEN | TS_CARR_ON;
865 			tp->t_iflag = TTYDEF_IFLAG;
866 			tp->t_oflag = TTYDEF_OFLAG;
867 			tp->t_lflag = TTYDEF_LFLAG;
868 			tp->t_cflag = TTYDEF_CFLAG;
869 			ttsetwater(tp);
870 		}
871 		/*
872 		* enable intrpts, open line discipline
873 		*/
874 		dga->csr |= GLOBAL_IE;	/* turn on the interrupts */
875 		return ((*tp->t_linesw->l_open)(dev, tp));
876 	}
877 	dga->csr |= GLOBAL_IE;	/* turn on the interrupts */
878 	return(0);
879 
880 } /* qdopen */
881 
882 /*ARGSUSED*/
883 int
884 qdclose(dev, flag, mode, p)
885 	dev_t dev;
886 	int flag, mode;
887 	struct proc *p;
888 {
889 	struct tty *tp;
890 	struct qdmap *qd;
891 	volatile int *ptep;
892 	volatile struct dga *dga;      	/* gate array register map pointer */
893 	volatile struct duart *duart;
894 	volatile struct adder *adder;
895 	int unit;
896 	int minor_dev;
897 	u_int mapix;
898 	int i;				/* SIGNED index */
899 	struct uba_softc *uh;
900 
901 	minor_dev = minor(dev); 	/* get minor device number */
902 	unit = minor_dev >> 2;		/* get QDSS number */
903 	qd = &qdmap[unit];
904 
905 	uh = (struct uba_softc *)
906 	     (((struct device *)(qd_cd.cd_devs[unit]))->dv_parent);
907 
908 
909 	if ((minor_dev & 0x03) == 2) {
910 		/*
911 		* this is the graphic device...
912 		*/
913 		if (qdopened[unit] != 1)
914 			return(EBUSY);
915 		else
916 			qdopened[unit] = 0;	/* allow it to be re-opened */
917 		/*
918 		* re-protect device memory
919 		*/
920 		if (qdflags[unit].mapped & MAPDEV) {
921 			/*
922 			* TEMPLATE RAM
923 			*/
924 			mapix = VTOP((int)qd->template) - VTOP(qvmem[0]);
925 			ptep = (int *)(QVmap[0] + mapix);
926 			for (i = 0; i < vax_btop(TMPSIZE); i++, ptep++)
927 				*ptep = (*ptep & ~PG_PROT) | PG_V | PG_KW;
928 			/*
929 			* ADDER
930 			*/
931 			mapix = VTOP((int)qd->adder) - VTOP(qvmem[0]);
932 			ptep = (int *)(QVmap[0] + mapix);
933 			for (i = 0; i < vax_btop(REGSIZE); i++, ptep++)
934 				*ptep = (*ptep & ~PG_PROT) | PG_V | PG_KW;
935 			/*
936 			* COLOR MAPS
937 			*/
938 			mapix = VTOP((int)qd->red) - VTOP(qvmem[0]);
939 			ptep = (int *)(QVmap[0] + mapix);
940 			for (i = 0; i < vax_btop(CLRSIZE); i++, ptep++)
941 				*ptep = (*ptep & ~PG_PROT) | PG_V | PG_KW;
942 		}
943 
944 		/*
945 		* re-protect DMA buffer and free the map registers
946 		*/
947 		if (qdflags[unit].mapped & MAPDMA) {
948 			panic("Unmapping unmapped buffer");
949 #ifdef notyet
950 /*
951  * Ragge 990620:
952  * Can't happen because the buffer can't be mapped.
953  */
954 			dga = (struct dga *) qdmap[unit].dga;
955 			adder = (struct adder *) qdmap[unit].adder;
956 			dga->csr &= ~DMA_IE;
957 			dga->csr &= ~0x0600;	     /* kill DMA */
958 			adder->command = CANCEL;
959 			/*
960 			 * if DMA was running, flush spurious intrpt
961 			 */
962 			if (dga->bytcnt_lo != 0) {
963 				dga->bytcnt_lo = 0;
964 				dga->bytcnt_hi = 0;
965 				DMA_SETIGNORE(DMAheader[unit]);
966 				dga->csr |= DMA_IE;
967 				dga->csr &= ~DMA_IE;
968 			}
969 			ptep = (int *)
970 			   ((VTOP(DMAheader[unit]*4)) + (mfpr(PR_SBR)|0x80000000));
971 			for (i = 0; i < vax_btop(DMAbuf_size); i++, ptep++)
972 				*ptep = (*ptep & ~PG_PROT) | PG_V | PG_KW;
973 			ubarelse(uh, &Qbus_unmap[unit]);
974 #endif
975 		}
976 
977 		/*
978 		* re-protect 1K (2 pages) event queue
979 		*/
980 		if (qdflags[unit].mapped & MAPEQ) {
981 			ptep = (int *)
982 			   ((VTOP(eq_header[unit])*4) + (mfpr(PR_SBR)|0x80000000));
983 			*ptep = (*ptep & ~PG_PROT) | PG_KW | PG_V; ptep++;
984 			*ptep = (*ptep & ~PG_PROT) | PG_KW | PG_V;
985 		}
986 		/*
987 		* re-protect scroll param area and disable scroll intrpts
988 		*/
989 		if (qdflags[unit].mapped & MAPSCR) {
990 			ptep = (int *) ((VTOP(scroll[unit]) * 4)
991 				+ (mfpr(PR_SBR) | 0x80000000));
992 			/*
993 			 * re-protect 512 scroll param area
994 			 */
995 			*ptep = (*ptep & ~PG_PROT) | PG_KW | PG_V;
996 			adder = (struct adder *) qdmap[unit].adder;
997 			qdflags[unit].adder_ie &= ~FRAME_SYNC;
998 			adder->interrupt_enable = qdflags[unit].adder_ie;
999 		}
1000 		/*
1001 		* re-protect color map write buffer area and kill intrpts
1002 		*/
1003 		if (qdflags[unit].mapped & MAPCOLOR) {
1004 			ptep = (int *) ((VTOP(color_buf[unit]) * 4)
1005 				+ (mfpr(PR_SBR) | 0x80000000));
1006 			*ptep = (*ptep & ~PG_PROT) | PG_KW | PG_V; ptep++;
1007 			*ptep = (*ptep & ~PG_PROT) | PG_KW | PG_V;
1008 			color_buf[unit]->status = 0;
1009 			adder = (struct adder *) qdmap[unit].adder;
1010 			qdflags[unit].adder_ie &= ~VSYNC;
1011 			adder->interrupt_enable = qdflags[unit].adder_ie;
1012 		}
1013 		mtpr(0, PR_TBIA);
1014 		/* flag everything now unmapped */
1015 		qdflags[unit].mapped = 0;
1016 		qdflags[unit].inuse &= ~GRAPHIC_DEV;
1017 		qdflags[unit].curs_acc = ACC_OFF;
1018 		qdflags[unit].curs_thr = 128;
1019 		/*
1020 		* restore the console
1021 		*/
1022 		dga = (struct dga *) qdmap[unit].dga;
1023 		adder = (struct adder *) qdmap[unit].adder;
1024 		dga->csr &= ~DMA_IE;
1025 		dga->csr &= ~0x0600;	/* halt the DMA! (just in case...) */
1026 		dga->csr |= DMA_ERR;	/* clear error condition */
1027 		adder->command = CANCEL;
1028 		/*
1029 		 * if DMA was running, flush spurious intrpt
1030 		 */
1031 		if (dga->bytcnt_lo != 0) {
1032 			dga->bytcnt_lo = 0;
1033 			dga->bytcnt_hi = 0;
1034 			DMA_SETIGNORE(DMAheader[unit]);
1035 			dga->csr |= DMA_IE;
1036 			dga->csr &= ~DMA_IE;
1037 		}
1038 		init_shared(unit);		/* init shared memory */
1039 		setup_dragon(unit);		/* init ADDER/VIPER */
1040 		ldcursor(unit, cons_cursor);	/* load default cursor map */
1041 		setup_input(unit);		/* init the DUART */
1042 		ldfont(unit);
1043 		cursor[unit].x = 0;
1044 		cursor[unit].y = 0;
1045 		/*
1046 		 * shut off the mouse rcv intrpt and turn on kbd intrpts
1047 		 */
1048 		duart = (struct duart *) qdmap[unit].duart;
1049 		qdflags[unit].duart_imask &= ~(0x20);
1050 		qdflags[unit].duart_imask |= 0x02;
1051 		duart->imask = qdflags[unit].duart_imask;
1052 		/*
1053 		* shut off interrupts if all is closed
1054 		*/
1055 		if (!(qdflags[unit].inuse & CONS_DEV)) {
1056 			dga = (struct dga *) qdmap[unit].dga;
1057 			dga->csr &= ~(GLOBAL_IE | DMA_IE);
1058 		}
1059 	} else {
1060 		/*
1061 		* this is the console
1062 		*/
1063 		tp = qd_tty[minor_dev];
1064 		(*tp->t_linesw->l_close)(tp, flag);
1065 		ttyclose(tp);
1066 		tp->t_state = 0;
1067 		qdflags[unit].inuse &= ~CONS_DEV;
1068 		/*
1069 		* if graphics device is closed, kill interrupts
1070 		*/
1071 		if (!(qdflags[unit].inuse & GRAPHIC_DEV)) {
1072 			dga = (struct dga *) qdmap[unit].dga;
1073 			dga->csr &= ~(GLOBAL_IE | DMA_IE);
1074 		}
1075 	}
1076 
1077 	return(0);
1078 
1079 } /* qdclose */
1080 
1081 int
1082 qdioctl(dev, cmd, datap, flags, p)
1083 	dev_t dev;
1084 	u_long cmd;
1085 	caddr_t datap;
1086 	int flags;
1087 	struct proc *p;
1088 {
1089 	volatile int *ptep;	/* page table entry pointer */
1090 	int mapix;		/* QVmap[] page table index */
1091 	struct _vs_event *event;
1092 	struct tty *tp;
1093 	int i;
1094 	struct qdmap *qd;		/* pointer to device map struct */
1095 	volatile struct dga *dga;	/* Gate Array reg structure pntr */
1096 	volatile struct duart *duart;	/* DUART reg structure pointer */
1097 	volatile struct adder *adder;	/* ADDER reg structure pointer */
1098 	struct prgkbd *cmdbuf;
1099 	struct prg_cursor *curs;
1100 	struct _vs_cursor *pos;
1101 	int unit = minor(dev) >> 2;	/* number of caller's QDSS */
1102 	u_int minor_dev = minor(dev);
1103 	int error;
1104 	int s;
1105 	short *temp;			/* a pointer to template RAM */
1106 	struct uba_softc *uh;
1107 
1108 	uh = (struct uba_softc *)
1109 	     (((struct device *)(qd_cd.cd_devs[unit]))->dv_parent);
1110 
1111 	/*
1112 	* service graphic device ioctl commands
1113 	*/
1114 	switch (cmd) {
1115 
1116 	case QD_GETEVENT:
1117 		/*
1118 		* extract the oldest event from the event queue
1119 		*/
1120 		if (ISEMPTY(eq_header[unit])) {
1121 			event = (struct _vs_event *) datap;
1122 			event->vse_device = VSE_NULL;
1123 			break;
1124 		}
1125 		event = (struct _vs_event *) GETBEGIN(eq_header[unit]);
1126 		s = spl5();
1127 		GETEND(eq_header[unit]);
1128 		splx(s);
1129 		bcopy((caddr_t)event, datap, sizeof(struct _vs_event));
1130 		break;
1131 
1132 	case QD_RESET:
1133 		/*
1134 		* init the dragon stuff, DUART, and driver variables
1135 		*/
1136 		init_shared(unit);		/* init shared memory */
1137 		setup_dragon(unit);	      /* init the ADDER/VIPER stuff */
1138 		clear_qd_screen(unit);
1139 		ldcursor(unit, cons_cursor);	/* load default cursor map */
1140 		ldfont(unit);			/* load the console font */
1141 		setup_input(unit);		/* init the DUART */
1142 		break;
1143 
1144 	case QD_SET:
1145 		/*
1146 		* init the DUART and driver variables
1147 		*/
1148 		init_shared(unit);
1149 		setup_input(unit);
1150 		break;
1151 
1152 	case QD_CLRSCRN:
1153 		/*
1154 		* clear the QDSS screen.  (NOTE that this reinits the dragon)
1155 		*/
1156 #ifdef notdef	/* has caused problems and isn't necessary */
1157 		setup_dragon(unit);
1158 		clear_qd_screen(unit);
1159 #endif
1160 		break;
1161 
1162 	case QD_WTCURSOR:
1163 		/*
1164 		* load a cursor into template RAM
1165 		*/
1166 		ldcursor(unit, (short *)datap);
1167 		break;
1168 
1169 	case QD_RDCURSOR:
1170 
1171 		temp = (short *) qdmap[unit].template;
1172 		/*
1173 		 * cursor is 32 WORDS from the end of the 8k WORD...
1174 		 *  ...template space
1175 		 */
1176 		temp += (8 * 1024) - 32;
1177 		for (i = 0; i < 32; ++i, datap += sizeof(short))
1178 			*(short *)datap = *temp++;
1179 		break;
1180 
1181 	case QD_POSCURSOR:
1182 		/*
1183 		* position the mouse cursor
1184 		*/
1185 		dga = (struct dga *) qdmap[unit].dga;
1186 		pos = (struct _vs_cursor *) datap;
1187 		s = spl5();
1188 		dga->x_cursor = TRANX(pos->x);
1189 		dga->y_cursor = TRANY(pos->y);
1190 		eq_header[unit]->curs_pos.x = pos->x;
1191 		eq_header[unit]->curs_pos.y = pos->y;
1192 		splx(s);
1193 		break;
1194 
1195 	case QD_PRGCURSOR:
1196 		/*
1197 		* set the cursor acceleration factor
1198 		*/
1199 		curs = (struct prg_cursor *) datap;
1200 		s = spl5();
1201 		qdflags[unit].curs_acc = curs->acc_factor;
1202 		qdflags[unit].curs_thr = curs->threshold;
1203 		splx(s);
1204 		break;
1205 
1206 	case QD_MAPDEVICE:
1207 	       /*
1208 		* enable 'user write' to device pages
1209 		*/
1210 		qdflags[unit].mapped |= MAPDEV;
1211 		qd = (struct qdmap *) &qdmap[unit];
1212 		/*
1213 		* enable user write to template RAM
1214 		*/
1215 		mapix = VTOP((int)qd->template) - VTOP(qvmem[0]);
1216 		ptep = (int *)(QVmap[0] + mapix);
1217 		for (i = 0; i < vax_btop(TMPSIZE); i++, ptep++)
1218 			*ptep = (*ptep & ~PG_PROT) | PG_RW | PG_V;
1219 
1220 	       /*
1221 		* enable user write to registers
1222 		*/
1223 		mapix = VTOP((int)qd->adder) - VTOP(qvmem[0]);
1224 		ptep = (int *)(QVmap[0] + mapix);
1225 		for (i = 0; i < vax_btop(REGSIZE); i++, ptep++)
1226 			*ptep = (*ptep & ~PG_PROT) | PG_RW | PG_V;
1227 
1228 		/*
1229 		* enable user write to color maps
1230 		*/
1231 		mapix = VTOP((int)qd->red) - VTOP(qvmem[0]);
1232 		ptep = (int *)(QVmap[0] + mapix);
1233 		for (i = 0; i < vax_btop(CLRSIZE); i++, ptep++)
1234 			*ptep = (*ptep & ~PG_PROT) | PG_RW | PG_V;
1235 
1236 	       /*
1237 		* enable user write to DUART
1238 		*/
1239 		mapix = VTOP((int)qd->duart) - VTOP(qvmem[0]);
1240 		ptep = (int *)(QVmap[0] + mapix);
1241 		*ptep = (*ptep & ~PG_PROT) | PG_RW | PG_V; /* duart page */
1242 
1243 		mtpr(0, PR_TBIA);		/* invalidate translation buffer */
1244 
1245 		/*
1246 		 * stuff qdmap structure in return buffer
1247 		 */
1248 		bcopy((caddr_t)qd, datap, sizeof(struct qdmap));
1249 
1250 		break;
1251 
1252 #ifdef notyet
1253 /*
1254  * Ragge 999620:
1255  * Can't map in the graphic buffer into user space for now.
1256  * The best way to fix this is to convert this driver to wscons.
1257  */
1258 	case QD_MAPIOBUF:
1259 		/*
1260 		 * do setup for DMA by user process
1261 		 *
1262 		 * set 'user write enable' bits for DMA buffer
1263 		 */
1264 		qdflags[unit].mapped |= MAPDMA;
1265 		ptep = (int *) ((VTOP(DMAheader[unit]) * 4)
1266 			+ (mfpr(PR_SBR) | 0x80000000));
1267 		for (i = 0; i < vax_btop(DMAbuf_size); i++, ptep++)
1268 			*ptep = (*ptep & ~PG_PROT) | PG_RW | PG_V;
1269 		mtpr(0, PR_TBIA);	/* invalidate translation buffer */
1270 		/*
1271 		* set up QBUS map registers for DMA
1272 		*/
1273 		DMAheader[unit]->QBAreg =
1274 		    uballoc(uh, (caddr_t)DMAheader[unit], DMAbuf_size, 0);
1275 		if (DMAheader[unit]->QBAreg == 0)
1276 		    printf("qd%d: qdioctl: QBA setup error\n", unit);
1277 		Qbus_unmap[unit] = DMAheader[unit]->QBAreg;
1278 		DMAheader[unit]->QBAreg &= 0x3FFFF;
1279 		/*
1280 		* return I/O buf adr
1281 		*/
1282 		*(int *)datap = (int) DMAheader[unit];
1283 		break;
1284 #endif
1285 
1286 	case QD_MAPSCROLL:
1287 		/*
1288 		* map the shared scroll param area and enable scroll interpts
1289 		*/
1290 		qdflags[unit].mapped |= MAPSCR;
1291 		ptep = (int *) ((VTOP(scroll[unit]) * 4)
1292 			+ (mfpr(PR_SBR) | 0x80000000));
1293 		/*
1294 		 * allow user write to scroll area
1295 		 */
1296 		*ptep = (*ptep & ~PG_PROT) | PG_RW | PG_V;
1297 		mtpr(0, PR_TBIA);			/* invalidate translation buf */
1298 		scroll[unit]->status = 0;
1299 		adder = (struct adder *) qdmap[unit].adder;
1300 		qdflags[unit].adder_ie |= FRAME_SYNC;
1301 		adder->interrupt_enable = qdflags[unit].adder_ie;
1302 		*(int *)datap = (int) scroll[unit]; /* return scroll area */
1303 		break;
1304 
1305 	case QD_UNMAPSCROLL:
1306 		/*
1307 		* unmap shared scroll param area and disable scroll intrpts
1308 		*/
1309 		if (qdflags[unit].mapped & MAPSCR) {
1310 			qdflags[unit].mapped &= ~MAPSCR;
1311 			ptep = (int *) ((VTOP(scroll[unit]) * 4)
1312 				+ (mfpr(PR_SBR) | 0x80000000));
1313 			/*
1314 			 * re-protect 512 scroll param area
1315 			 */
1316 			*ptep = (*ptep & ~PG_PROT) | PG_KW | PG_V;
1317 			mtpr(0, PR_TBIA);	/* smash CPU's translation buf */
1318 			adder = (struct adder *) qdmap[unit].adder;
1319 			qdflags[unit].adder_ie &= ~FRAME_SYNC;
1320 			adder->interrupt_enable = qdflags[unit].adder_ie;
1321 		}
1322 		break;
1323 
1324 	case QD_MAPCOLOR:
1325 		/*
1326 		* map shared color map write buf and turn on vsync intrpt
1327 		*/
1328 		qdflags[unit].mapped |= MAPCOLOR;
1329 		ptep = (int *) ((VTOP(color_buf[unit]) * 4)
1330 			+ (mfpr(PR_SBR) | 0x80000000));
1331 		/*
1332 		 * allow user write to color map write buffer
1333 		 */
1334 		*ptep = (*ptep & ~PG_PROT) | PG_RW | PG_V; ptep++;
1335 		*ptep = (*ptep & ~PG_PROT) | PG_RW | PG_V;
1336 		mtpr(0, PR_TBIA);			/* clr CPU translation buf */
1337 		adder = (struct adder *) qdmap[unit].adder;
1338 		qdflags[unit].adder_ie |= VSYNC;
1339 		adder->interrupt_enable = qdflags[unit].adder_ie;
1340 		/*
1341 		 * return color area address
1342 		 */
1343 		*(int *)datap = (int) color_buf[unit];
1344 		break;
1345 
1346 	case QD_UNMAPCOLOR:
1347 		/*
1348 		 * unmap shared color map write buffer and kill VSYNC intrpts
1349 		 */
1350 		if (qdflags[unit].mapped & MAPCOLOR) {
1351 			qdflags[unit].mapped &= ~MAPCOLOR;
1352 			ptep = (int *) ((VTOP(color_buf[unit]) * 4)
1353 				+ (mfpr(PR_SBR) | 0x80000000));
1354 			/*
1355 			 * re-protect color map write buffer
1356 			 */
1357 			*ptep = (*ptep & ~PG_PROT) | PG_KW | PG_V; ptep++;
1358 			*ptep = (*ptep & ~PG_PROT) | PG_KW | PG_V;
1359 			mtpr(0, PR_TBIA);
1360 			adder = (struct adder *) qdmap[unit].adder;
1361 			qdflags[unit].adder_ie &= ~VSYNC;
1362 			adder->interrupt_enable = qdflags[unit].adder_ie;
1363 		}
1364 		break;
1365 
1366 	case QD_MAPEVENT:
1367 		/*
1368 		* give user write access to the event queue
1369 		*/
1370 		qdflags[unit].mapped |= MAPEQ;
1371 		ptep = (int *) ((VTOP(eq_header[unit]) * 4)
1372 			+ (mfpr(PR_SBR) | 0x80000000));
1373 		/*
1374 		 * allow user write to 1K event queue
1375 		 */
1376 		*ptep = (*ptep & ~PG_PROT) | PG_RW | PG_V; ptep++;
1377 		*ptep = (*ptep & ~PG_PROT) | PG_RW | PG_V;
1378 		mtpr(0, PR_TBIA);			/* clr CPU translation buf */
1379 		/*
1380 		 * return event queue address
1381 		 */
1382 		*(int *)datap = (int)eq_header[unit];
1383 		break;
1384 
1385 	case QD_PRGKBD:
1386 		/*
1387 		* pass caller's programming commands to LK201
1388 		*/
1389 		duart = (struct duart *)qdmap[unit].duart;
1390 		cmdbuf = (struct prgkbd *)datap;    /* pnt to kbd cmd buf */
1391 		/*
1392 		* send command
1393 		*/
1394 		for (i = 1000; i > 0; --i) {
1395 			if (duart->statusA&XMT_RDY) {
1396 				duart->dataA = cmdbuf->cmd;
1397 				break;
1398 			}
1399 		}
1400 		if (i == 0) {
1401 			printf("qd%d: qdioctl: timeout on XMT_RDY [1]\n", unit);
1402 			break;
1403 		}
1404 		/*
1405 		* send param1?
1406 		*/
1407 		if (cmdbuf->cmd & LAST_PARAM)
1408 			break;
1409 		for (i = 1000; i > 0; --i) {
1410 			if (duart->statusA&XMT_RDY) {
1411 				duart->dataA = cmdbuf->param1;
1412 				break;
1413 			}
1414 		}
1415 		if (i == 0) {
1416 			printf("qd%d: qdioctl: timeout on XMT_RDY [2]\n", unit);
1417 			break;
1418 		}
1419 		/*
1420 		* send param2?
1421 		*/
1422 		if (cmdbuf->param1 & LAST_PARAM)
1423 		    break;
1424 		for (i = 1000; i > 0; --i) {
1425 			if (duart->statusA&XMT_RDY) {
1426 				duart->dataA = cmdbuf->param2;
1427 				break;
1428 			}
1429 		}
1430 		if (i == 0) {
1431 			printf("qd%d: qdioctl: timeout on XMT_RDY [3]\n", unit);
1432 			break;
1433 		}
1434 		break;
1435 
1436 	case QD_PRGMOUSE:
1437 		/*
1438 		* pass caller's programming commands to the mouse
1439 		*/
1440 		duart = (struct duart *) qdmap[unit].duart;
1441 		for (i = 1000; i > 0; --i) {
1442 			if (duart->statusB&XMT_RDY) {
1443 				duart->dataB = *datap;
1444 				break;
1445 			}
1446 		}
1447 		if (i == 0) {
1448 			printf("qd%d: qdioctl: timeout on XMT_RDY [4]\n", unit);
1449 		}
1450 		break;
1451 
1452 	case QD_RDCONFIG:
1453 		/*
1454 		* get QDSS configuration word and return it
1455 		*/
1456 		*(short *)datap = qdflags[unit].config;
1457 		break;
1458 
1459 	case QD_KERN_LOOP:
1460 	case QD_KERN_UNLOOP:
1461 		/*
1462 		 * vestige from ultrix.  BSD uses TIOCCONS to redirect
1463 		 * kernel console output.
1464 		 */
1465 		break;
1466 
1467 	case QD_PRGTABLET:
1468 		/*
1469 		* program the tablet
1470 		*/
1471 		duart = (struct duart *) qdmap[unit].duart;
1472 		for (i = 1000; i > 0; --i) {
1473 			if (duart->statusB&XMT_RDY) {
1474 				duart->dataB = *datap;
1475 				break;
1476 			}
1477 		}
1478 		if (i == 0) {
1479 			printf("qd%d: qdioctl: timeout on XMT_RDY [5]\n", unit);
1480 		}
1481 		break;
1482 
1483 	case QD_PRGTABRES:
1484 		/*
1485 		* program the tablet report resolution factor
1486 		*/
1487 		qdflags[unit].tab_res = *(short *)datap;
1488 		break;
1489 
1490 	default:
1491 		/*
1492 		* service tty ioctl's
1493 		*/
1494 		if (!(minor_dev & 0x02)) {
1495 			tp = qd_tty[minor_dev];
1496 			error =
1497 
1498 		   (*tp->t_linesw->l_ioctl)(tp, cmd, datap, flags, p);
1499 			if (error != EPASSTHROUGH) {
1500 				return(error);
1501 			}
1502 			return ttioctl(tp, cmd, datap, flags, p);
1503 		}
1504 		break;
1505 	}
1506 
1507 	return(0);
1508 
1509 } /* qdioctl */
1510 
1511 
1512 int
1513 qdpoll(dev, events, p)
1514 	dev_t dev;
1515 	int events;
1516 	struct proc *p;
1517 {
1518 	int s;
1519 	int unit;
1520 	struct tty *tp;
1521 	u_int minor_dev = minor(dev);
1522 	int revents = 0;
1523 
1524 	s = spl5();
1525 	unit = minor_dev >> 2;
1526 
1527 	if ((minor_dev & 0x03) == 2)  {
1528 		/*
1529 		 * This is a graphics device, so check for events.
1530 		 */
1531 
1532 		if (events & (POLLIN | POLLRDNORM))
1533 			if(!(ISEMPTY(eq_header[unit])))
1534 				revents |= events & (POLLIN | POLLRDNORM);
1535 
1536 		if (events & (POLLOUT | POLLWRNORM))
1537 			if (DMA_ISEMPTY(DMAheader[unit]))
1538 				revents |= events & (POLLOUT | POLLWRNORM);
1539 
1540 		if (revents == 0)  {
1541 			if (events & (POLLIN | POLLRDNORM))  {
1542 				selrecord(p, &qdrsel[unit]);
1543 				qdflags[unit].selmask |= SEL_READ;
1544 			}
1545 
1546 			if (events & (POLLOUT | POLLWRNORM))  {
1547 				selrecord(p, &qdrsel[unit]);
1548 				qdflags[unit].selmask |= SEL_WRITE;
1549 			}
1550 		}
1551 	} else  {
1552 		/*
1553 		* this is a tty device
1554 		*/
1555 		tp = qd_tty[minor_dev];
1556 		revents = (*tp->t_linesw->l_poll)(tp, events, p);
1557 	}
1558 
1559 	splx(s);
1560 	return (revents);
1561 } /* qdpoll() */
1562 
1563 
1564 void qd_strategy(struct buf *bp);
1565 
1566 /*ARGSUSED*/
1567 int
1568 qdwrite(dev, uio, flag)
1569 	dev_t dev;
1570 	struct uio *uio;
1571 {
1572 	struct tty *tp;
1573 	int minor_dev;
1574 	int unit;
1575 
1576 	minor_dev = minor(dev);
1577 	unit = (minor_dev >> 2) & 0x07;
1578 
1579 	if (((minor_dev&0x03) != 0x02) && (qdflags[unit].inuse&CONS_DEV)) {
1580 	       /*
1581 		* this is the console...
1582 		*/
1583 		tp = qd_tty[minor_dev];
1584 		return ((*tp->t_linesw->l_write)(tp, uio, flag));
1585 	} else if (qdflags[unit].inuse & GRAPHIC_DEV) {
1586 	       /*
1587 		* this is a DMA xfer from user space
1588 		*/
1589 		return (physio(qd_strategy, &qdbuf[unit],
1590 		dev, B_WRITE, minphys, uio));
1591 	}
1592 	return (ENXIO);
1593 }
1594 
1595 /*ARGSUSED*/
1596 int
1597 qdread(dev, uio, flag)
1598 	dev_t dev;
1599 	struct uio *uio;
1600 {
1601 	struct tty *tp;
1602 	int minor_dev;
1603 	int unit;
1604 
1605 	minor_dev = minor(dev);
1606 	unit = (minor_dev >> 2) & 0x07;
1607 
1608 	if ((minor_dev & 0x03) != 0x02 && qdflags[unit].inuse & CONS_DEV) {
1609 	       /*
1610 		* this is the console
1611 		*/
1612 		tp = qd_tty[minor_dev];
1613 		return ((*tp->t_linesw->l_read)(tp, uio, flag));
1614 	} else if (qdflags[unit].inuse & GRAPHIC_DEV) {
1615 	       /*
1616 		* this is a bitmap-to-processor xfer
1617 		*/
1618 		return (physio(qd_strategy, &qdbuf[unit],
1619 		dev, B_READ, minphys, uio));
1620 	}
1621 	return (ENXIO);
1622 }
1623 
1624 /***************************************************************
1625 *
1626 *	qd_strategy()... strategy routine to do DMA
1627 *
1628 ***************************************************************/
1629 
1630 void
1631 qd_strategy(bp)
1632 	struct buf *bp;
1633 {
1634 	volatile struct dga *dga;
1635 	volatile struct adder *adder;
1636 	int unit;
1637 	int QBAreg;
1638 	int s;
1639 	int cookie;
1640 	struct uba_softc *uh;
1641 
1642 	unit = (minor(bp->b_dev) >> 2) & 0x07;
1643 
1644 	uh = (struct uba_softc *)
1645 	     (((struct device *)(qd_cd.cd_devs[unit]))->dv_parent);
1646 
1647 	/*
1648 	* init pointers
1649 	*/
1650 	dga = (struct dga *) qdmap[unit].dga;
1651 panic("qd_strategy");
1652 #ifdef notyet
1653 	if ((QBAreg = ubasetup(uh, bp, 0)) == 0) {
1654 		printf("qd%d: qd_strategy: QBA setup error\n", unit);
1655 		goto STRAT_ERR;
1656 	}
1657 #endif
1658 	s = spl5();
1659 	qdflags[unit].user_dma = -1;
1660 	dga->csr |= DMA_IE;
1661 	cookie = QBAreg & 0x3FFFF;
1662 	dga->adrs_lo = (short) cookie;
1663 	dga->adrs_hi = (short) (cookie >> 16);
1664 	dga->bytcnt_lo = (short) bp->b_bcount;
1665 	dga->bytcnt_hi = (short) (bp->b_bcount >> 16);
1666 
1667 	while (qdflags[unit].user_dma) {
1668 		(void) tsleep(&qdflags[unit].user_dma, QSPRIOR,
1669 		    "qdstrat", 0);
1670 	}
1671 	splx(s);
1672 #ifdef notyet
1673 	ubarelse(uh, &QBAreg);
1674 #endif
1675 	if (!(dga->csr & DMA_ERR)) {
1676 		biodone(bp);
1677 		return;
1678 	}
1679 
1680 /* STRAT_ERR: */
1681 	adder = (struct adder *) qdmap[unit].adder;
1682 	adder->command = CANCEL;	/* cancel adder activity */
1683 	dga->csr &= ~DMA_IE;
1684 	dga->csr &= ~0x0600;		/* halt DMA (reset fifo) */
1685 	dga->csr |= DMA_ERR;		/* clear error condition */
1686 	bp->b_flags |= B_ERROR; 	/* flag an error to physio() */
1687 
1688 	/*
1689 	 * if DMA was running, flush spurious intrpt
1690 	 */
1691 	if (dga->bytcnt_lo != 0) {
1692 		dga->bytcnt_lo = 0;
1693 		dga->bytcnt_hi = 0;
1694 		DMA_SETIGNORE(DMAheader[unit]);
1695 		dga->csr |= DMA_IE;
1696 	}
1697 	biodone(bp);
1698 } /* qd_strategy */
1699 
1700 
1701 /*
1702  *  Start output to the console screen
1703  */
1704 void qdstart(tp)
1705 	struct tty *tp;
1706 {
1707 	int which_unit, unit, c;
1708 	int s;
1709 
1710 	unit = minor(tp->t_dev);
1711 	which_unit = (unit >> 2) & 0x3;
1712 	unit &= 0x03;
1713 
1714 	s = spl5();
1715 
1716 	/*
1717 	* If it's currently active, or delaying, no need to do anything.
1718 	*/
1719 	if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
1720 		goto out;
1721 
1722 	/*
1723 	* Display chars until the queue is empty.
1724 	* Drop input from anything but the console
1725 	* device on the floor.
1726 	*
1727 	* XXX - this loop is done at spltty.
1728 	*
1729 	*/
1730 	while (tp->t_outq.c_cc) {
1731 		c = getc(&tp->t_outq);
1732 		if (unit == 0)
1733 			blitc(which_unit, (u_char)c);
1734 	}
1735 	/*
1736 	* If there are sleepers, and output has drained below low
1737 	* water mark, wake up the sleepers.
1738 	*/
1739 	if (tp->t_outq.c_cc <= tp->t_lowat) {
1740 		if (tp->t_state & TS_ASLEEP){
1741 			tp->t_state &= ~TS_ASLEEP;
1742 			wakeup((caddr_t) &tp->t_outq);
1743 		}
1744 	}
1745 
1746 	tp->t_state &= ~TS_BUSY;
1747 
1748 out:
1749 	splx(s);
1750 
1751 } /* qdstart */
1752 
1753 /*ARGSUSED*/
1754 void
1755 qdstop(tp, flag)
1756 	struct tty *tp;
1757 	int flag;
1758 {
1759 	int s;
1760 
1761 	s = spl5();	/* block intrpts during state modification */
1762 	if (tp->t_state & TS_BUSY) {
1763 		if ((tp->t_state & TS_TTSTOP) == 0)
1764 			tp->t_state |= TS_FLUSH;
1765 		else
1766 			tp->t_state &= ~TS_BUSY;
1767 	}
1768 	splx(s);
1769 }
1770 
1771 /*
1772  *  Output a character to the QDSS screen
1773  */
1774 void
1775 blitc(unit, chr)
1776 	int unit;
1777 	u_char chr;
1778 {
1779 	volatile struct adder *adder;
1780 	volatile struct dga *dga;
1781 	int i;
1782 	int nograph = !(qdflags[unit].inuse&GRAPHIC_DEV);
1783 	static short inescape[NQD];
1784 
1785 	adder = (struct adder *)qdmap[unit].adder;
1786 	dga = (struct dga *) qdmap[unit].dga;
1787 	/*
1788 	 * BSD comment: this (&=0177) defeats the extended character
1789 	 * set code for the glass tty, but if i had the time i would
1790 	 * spend it ripping out the code completely.  This driver
1791 	 * is too big for its own good.
1792 	 */
1793 	chr &= 0177;
1794 	/*
1795 	 * Cursor addressing (so vi will work).
1796 	 * Decode for "\E=%.%." cursor motion description.
1797 	 * Corresponds to type "qdcons" in /etc/termcap:
1798 	 *
1799 	 *    qd|qdss|qdcons|qdss glass tty (4.4 BSD):\
1800 	 *      :am:do=^J:le=^H:bs:cm=\E=%.%.:cl=1^Z:co#128:li#57::nd=^L:up=^K:
1801 	 *
1802 	 */
1803 	if (inescape[unit] && nograph) {
1804 		switch (inescape[unit]++) {
1805 		case 1:
1806 			if (chr != '=') {
1807 				/* abort escape sequence */
1808 				inescape[unit] = 0;
1809 				blitc(unit, chr);
1810 			}
1811 			return;
1812 		case 2:
1813 			/* position row */
1814 			cursor[unit].y = CHAR_HEIGHT * chr;
1815 			if (cursor[unit].y > 863 - CHAR_HEIGHT)
1816 				cursor[unit].y = 863 - CHAR_HEIGHT;
1817 			dga->y_cursor = TRANY(cursor[unit].y);
1818 			return;
1819 		case 3:
1820 			/* position column */
1821 			cursor[unit].x = CHAR_WIDTH * chr;
1822 			if (cursor[unit].x > 1024 - CHAR_WIDTH)
1823 				cursor[unit].x = 1023 - CHAR_WIDTH;
1824 			dga->x_cursor = TRANX(cursor[unit].x);
1825 			inescape[unit] = 0;
1826 			return;
1827 		default:
1828 			inescape[unit] = 0;
1829 			blitc(unit, chr);
1830 		}
1831 	}
1832 
1833 	switch (chr) {
1834 	case '\r':			/* return char */
1835 		cursor[unit].x = 0;
1836 		if (nograph)
1837 			dga->x_cursor = TRANX(cursor[unit].x);
1838 		return;
1839 
1840 	case '\t':			/* tab char */
1841 		for (i = 8 - ((cursor[unit].x >> 3) & 0x07); i > 0; --i) {
1842 			blitc(unit, ' ');
1843 		}
1844 		return;
1845 
1846 	case '\n':			/* line feed char */
1847 		if ((cursor[unit].y += CHAR_HEIGHT) > (863 - CHAR_HEIGHT)) {
1848 			if (nograph) {
1849 				cursor[unit].y -= CHAR_HEIGHT;
1850 				scroll_up(adder);
1851 			} else
1852 				cursor[unit].y = 0;
1853 		}
1854 		if (nograph)
1855 			dga->y_cursor = TRANY(cursor[unit].y);
1856 		return;
1857 
1858 	case '\b':			/* backspace char */
1859 		if (cursor[unit].x > 0) {
1860 			cursor[unit].x -= CHAR_WIDTH;
1861 			if (nograph)
1862 				dga->x_cursor = TRANX(cursor[unit].x);
1863 		}
1864 		return;
1865 	case CTRL('k'):		/* cursor up */
1866 		if (nograph && cursor[unit].y > 0) {
1867 			cursor[unit].y -= CHAR_HEIGHT;
1868 			dga->y_cursor = TRANY(cursor[unit].y);
1869 		}
1870 		return;
1871 
1872 	case CTRL('^'):		/* home cursor */
1873 		if (nograph) {
1874 			cursor[unit].x = 0;
1875 			dga->x_cursor = TRANX(cursor[unit].x);
1876 			cursor[unit].y = 0;
1877 			dga->y_cursor = TRANY(cursor[unit].y);
1878 		}
1879 		return;
1880 
1881 	case CTRL('l'):		/* cursor right */
1882 		if (nograph && cursor[unit].x < 1023 - CHAR_WIDTH) {
1883 			cursor[unit].x += CHAR_WIDTH;
1884 			dga->x_cursor = TRANX(cursor[unit].x);
1885 		}
1886 		return;
1887 
1888 	case CTRL('z'):		/* clear screen */
1889 		if (nograph) {
1890 			setup_dragon(unit);
1891 			clear_qd_screen(unit);
1892 			/* home cursor - termcap seems to assume this */
1893 			cursor[unit].x = 0;
1894 			dga->x_cursor = TRANX(cursor[unit].x);
1895 			cursor[unit].y = 0;
1896 			dga->y_cursor = TRANY(cursor[unit].y);
1897 		}
1898 		return;
1899 
1900 	case '\033':		/* start escape sequence */
1901 		if (nograph)
1902 			inescape[unit] = 1;
1903 		return;
1904 
1905 	default:
1906 		if ((chr < ' ') || (chr > '~'))
1907 			return;
1908 	}
1909 	/*
1910 	 * setup VIPER operand control registers
1911 	 */
1912 	write_ID(adder, CS_UPDATE_MASK, 0x0001);  /* select plane #0 */
1913 	write_ID(adder, SRC1_OCR_B,
1914 	EXT_NONE | INT_SOURCE | ID | BAR_SHIFT_DELAY);
1915 	write_ID(adder, CS_UPDATE_MASK, 0x00FE);  /* select other planes */
1916 	write_ID(adder, SRC1_OCR_B,
1917 	EXT_SOURCE | INT_NONE | NO_ID | BAR_SHIFT_DELAY);
1918 	write_ID(adder, CS_UPDATE_MASK, 0x00FF);  /* select all planes */
1919 	write_ID(adder, DST_OCR_B,
1920 	EXT_NONE | INT_NONE | NO_ID | NO_BAR_SHIFT_DELAY);
1921 	write_ID(adder, MASK_1, 0xFFFF);
1922 	write_ID(adder, VIPER_Z_LOAD | FOREGROUND_COLOR_Z, 1);
1923 	write_ID(adder, VIPER_Z_LOAD | BACKGROUND_COLOR_Z, 0);
1924 	adder->x_clip_min = 0;
1925 	adder->x_clip_max = 1024;
1926 	adder->y_clip_min = 0;
1927 	adder->y_clip_max = 864;
1928 	/*
1929 	 * load DESTINATION origin and vectors
1930 	 */
1931 	adder->fast_dest_dy = 0;
1932 	adder->slow_dest_dx = 0;
1933 	adder->error_1 = 0;
1934 	adder->error_2 = 0;
1935 	adder->rasterop_mode = DST_WRITE_ENABLE | NORMAL;
1936 	(void)wait_status(adder, RASTEROP_COMPLETE);
1937 	adder->destination_x = cursor[unit].x;
1938 	adder->fast_dest_dx = CHAR_WIDTH;
1939 	adder->destination_y = cursor[unit].y;
1940 	adder->slow_dest_dy = CHAR_HEIGHT;
1941 	/*
1942 	 * load SOURCE origin and vectors
1943 	 */
1944 	if ((chr - ' ') > (CHARS - 1))  {
1945 		printf("Invalid character (x)%x in blitc\n",chr);
1946 		chr = ' ';
1947 	}
1948 	/*
1949 	 * X position is modulo the number of characters per line
1950 	 */
1951 	adder->source_1_x = FONT_X +
1952 	    (((chr - ' ') % (MAX_SCREEN_X/CHAR_WIDTH)) * CHAR_WIDTH);
1953 	/*
1954 	 * Point to either first or second row
1955 	 */
1956 	adder->source_1_y = 2048 - 15 *
1957 	    (((chr - ' ')/(MAX_SCREEN_X/CHAR_WIDTH)) + 1);
1958 	adder->source_1_dx = CHAR_WIDTH;
1959 	adder->source_1_dy = CHAR_HEIGHT;
1960 	write_ID(adder, LU_FUNCTION_R1, FULL_SRC_RESOLUTION | LF_SOURCE);
1961 	adder->cmd = RASTEROP | OCRB | 0 | S1E | DTE;
1962 	/*
1963 	 * update console cursor coordinates
1964 	 */
1965 	cursor[unit].x += CHAR_WIDTH;
1966 	if (nograph)
1967 		dga->x_cursor = TRANX(cursor[unit].x);
1968 	if (cursor[unit].x > (1024 - CHAR_WIDTH)) {
1969 		blitc(unit, '\r');
1970 		blitc(unit, '\n');
1971 	}
1972 
1973 } /* blitc */
1974 
1975 /*
1976  *  INTERRUPT SERVICE ROUTINES
1977  */
1978 
1979 /*
1980  *  Service "DMA DONE" interrupt condition
1981  */
1982 
1983 static void
1984 qddint(arg)
1985 	void *arg;
1986 {
1987 	struct device *dv = arg;
1988 	struct DMAreq_header *header;
1989 	struct DMAreq *request;
1990 	volatile struct dga *dga;
1991 	volatile struct adder *adder;
1992 	int cookie;			/* DMA adrs for QDSS */
1993 
1994 	(void)spl4(); 			/* allow interval timer in */
1995 
1996 	/*
1997 	* init pointers
1998 	*/
1999 	header = DMAheader[dv->dv_unit]; 	    /* register for optimization */
2000 	dga = (struct dga *) qdmap[dv->dv_unit].dga;
2001 	adder = (struct adder *) qdmap[dv->dv_unit].adder;
2002 
2003 	/*
2004 	* if this interrupt flagged as bogus for interrupt flushing purposes..
2005 	*/
2006 	if (DMA_ISIGNORE(header)) {
2007 	   DMA_CLRIGNORE(header);
2008 		return;
2009 	}
2010 
2011 	/*
2012 	* dump a DMA hardware error message if appropriate
2013 	*/
2014 	if (dga->csr & DMA_ERR) {
2015 
2016 		if (dga->csr & PARITY_ERR)
2017 		    printf("qd%d: qddint: DMA hardware parity fault.\n", dv->dv_unit);
2018 
2019 		if (dga->csr & BUS_ERR)
2020 		    printf("qd%d: qddint: DMA hardware bus error.\n", dv->dv_unit);
2021 	}
2022 
2023 	/*
2024 	* if this was a DMA from user space...
2025 	*/
2026 	if (qdflags[dv->dv_unit].user_dma) {
2027 		qdflags[dv->dv_unit].user_dma = 0;
2028 		wakeup((caddr_t)&qdflags[dv->dv_unit].user_dma);
2029 		return;
2030 	}
2031 
2032 	/*
2033 	* if we're doing DMA request queue services, field the error condition
2034 	*/
2035 	if (dga->csr & DMA_ERR) {
2036 
2037 		dga->csr &= ~0x0600;		/* halt DMA (reset fifo) */
2038 		dga->csr |= DMA_ERR;		/* clear error condition */
2039 		adder->command = CANCEL;	/* cancel adder activity */
2040 
2041 		DMA_SETERROR(header);	/* flag error in header status word */
2042 		DMA_CLRACTIVE(header);
2043 		header->DMAreq[header->oldest].DMAdone |= HARD_ERROR;
2044 		header->newest = header->oldest;
2045 		header->used = 0;
2046 
2047 		if (qdrsel[dv->dv_unit].si_pid && qdflags[dv->dv_unit].selmask & SEL_WRITE) {
2048 			selwakeup(&qdrsel[dv->dv_unit]);
2049 			qdrsel[dv->dv_unit].si_pid = 0;
2050 			qdflags[dv->dv_unit].selmask &= ~SEL_WRITE;
2051 		}
2052 
2053 		if (dga->bytcnt_lo != 0) {
2054 			dga->bytcnt_lo = 0;
2055 			dga->bytcnt_hi = 0;
2056 			DMA_SETIGNORE(header);
2057 		}
2058 		return;
2059 	}
2060 
2061 	/*
2062 	* if the DMA request queue is now becoming non-full,
2063 	* wakeup "select" client.
2064 	*/
2065 	if (DMA_ISFULL(header)) {
2066 		if (qdrsel[dv->dv_unit].si_pid && qdflags[dv->dv_unit].selmask & SEL_WRITE) {
2067 			selwakeup(&qdrsel[dv->dv_unit]);
2068 			qdrsel[dv->dv_unit].si_pid = 0;
2069 			qdflags[dv->dv_unit].selmask &= ~SEL_WRITE;
2070 		}
2071 	}
2072 
2073 	header->DMAreq[header->oldest].DMAdone |= REQUEST_DONE;
2074 	QDlast_DMAtype = header->DMAreq[header->oldest].DMAtype;
2075 
2076 	/* check for unexpected interrupt */
2077 	if (DMA_ISEMPTY(header))
2078 	    return;
2079 
2080 	DMA_GETEND(header);	/* update request queue indices */
2081 
2082 	/*
2083 	* if no more DMA pending, wake up "select" client and exit
2084 	*/
2085 	if (DMA_ISEMPTY(header)) {
2086 		if (qdrsel[dv->dv_unit].si_pid && qdflags[dv->dv_unit].selmask & SEL_WRITE) {
2087 			selwakeup(&qdrsel[dv->dv_unit]);
2088 			qdrsel[dv->dv_unit].si_pid = 0;
2089 			qdflags[dv->dv_unit].selmask &= ~SEL_WRITE;
2090 		}
2091 		DMA_CLRACTIVE(header);  /* flag DMA done */
2092 		return;
2093 	}
2094 
2095 	/*
2096 	* initiate next DMA xfer
2097 	*/
2098 	request = DMA_GETBEGIN(header);
2099 	if (request->DMAtype != QDlast_DMAtype) {
2100 		dga->csr &= ~0x0600;	  /* halt DMA (reset fifo) */
2101 		adder->command = CANCEL;  /* cancel adder activity */
2102 	}
2103 
2104 
2105 	switch (request->DMAtype) {
2106 
2107 	case DISPLIST:
2108 		if (request->DMAtype != QDlast_DMAtype) {
2109 			dga->csr |= DL_ENB;
2110 			dga->csr &= ~(BTOP_ENB | BYTE_DMA);
2111 		}
2112 		break;
2113 
2114 	case PTOB:
2115 		if (request->DMAtype != QDlast_DMAtype) {
2116 			if (request->DMAdone & BYTE_PACK)
2117 			    dga->csr |= (PTOB_ENB | BYTE_DMA);
2118 			else {
2119 				dga->csr |= PTOB_ENB;
2120 				dga->csr &= ~BYTE_DMA;
2121 			}
2122 		}
2123 		break;
2124 
2125 	case BTOP:
2126 		if (request->DMAtype != QDlast_DMAtype) {
2127 			if (request->DMAdone & BYTE_PACK) {
2128 				dga->csr &= ~DL_ENB;
2129 				dga->csr |= (BTOP_ENB | BYTE_DMA);
2130 			}
2131 			else {
2132 				dga->csr |= BTOP_ENB;
2133 				dga->csr &= ~(BYTE_DMA | DL_ENB);
2134 			}
2135 		}
2136 		break;
2137 	default:
2138 		printf("qd%d: qddint: illegal DMAtype parameter.\n", dv->dv_unit);
2139 		DMA_CLRACTIVE(header);	/* flag DMA done */
2140 		return;
2141 	}
2142 
2143 	if (request->DMAdone & COUNT_ZERO) {
2144 		dga->csr &= ~SET_DONE_FIFO;
2145 	}
2146 	else if (request->DMAdone & FIFO_EMPTY) {
2147 		dga->csr |= SET_DONE_FIFO;
2148 	}
2149 
2150 	if (request->DMAdone & WORD_PACK)
2151 	    dga->csr &= ~BYTE_DMA;
2152 	else if (request->DMAdone & BYTE_PACK)
2153 	    dga->csr |= BYTE_DMA;
2154 
2155 	dga->csr |= DMA_IE;
2156 	QDlast_DMAtype = request->DMAtype;
2157 
2158 	cookie = ((int)request->bufp - (int)header) + (int)header->QBAreg;
2159 
2160 	dga->adrs_lo = (short) cookie;
2161 	dga->adrs_hi = (short) (cookie >> 16);
2162 
2163 	dga->bytcnt_lo = (short) request->length;
2164 	dga->bytcnt_hi = (short) (request->length >> 16);
2165 
2166 	return;
2167 }
2168 
2169 /*
2170  * ADDER interrupt service routine
2171  */
2172 static void
2173 qdaint(arg)
2174 	void *arg;
2175 {
2176 	struct device *dv = arg;
2177 	volatile struct adder *adder;
2178 	struct color_buf *cbuf;
2179 	int i;
2180 	struct rgb *rgbp;
2181 	volatile short *red;
2182 	volatile short *green;
2183 	volatile short *blue;
2184 
2185 	(void)spl4(); 			/* allow interval timer in */
2186 
2187 	adder = (struct adder *) qdmap[dv->dv_unit].adder;
2188 
2189 	/*
2190 	* service the vertical blank interrupt (VSYNC bit) by loading
2191 	* any pending color map load request
2192 	*/
2193 	if (adder->status & VSYNC) {
2194 		adder->status &= ~VSYNC;	/* clear the interrupt */
2195 		cbuf = color_buf[dv->dv_unit];
2196 		if (cbuf->status & LOAD_COLOR_MAP) {
2197 
2198 			red = (short *) qdmap[dv->dv_unit].red;
2199 			green = (short *) qdmap[dv->dv_unit].green;
2200 			blue = (short *) qdmap[dv->dv_unit].blue;
2201 
2202 			for (i = cbuf->count, rgbp = cbuf->rgb;
2203 			     --i >= 0; rgbp++) {
2204 				red[rgbp->offset] = (short) rgbp->red;
2205 				green[rgbp->offset] = (short) rgbp->green;
2206 				blue[rgbp->offset] = (short) rgbp->blue;
2207 			}
2208 
2209 			cbuf->status &= ~LOAD_COLOR_MAP;
2210 		}
2211 	}
2212 
2213 	/*
2214 	* service the scroll interrupt (FRAME_SYNC bit)
2215 	*/
2216 	if (adder->status & FRAME_SYNC) {
2217 		adder->status &= ~FRAME_SYNC;	/* clear the interrupt */
2218 
2219 		if (scroll[dv->dv_unit]->status & LOAD_REGS) {
2220 
2221 			for (i = 1000, adder->status = 0; i > 0 &&
2222 			     !(adder->status&ID_SCROLL_READY); --i)
2223 			      ;
2224 
2225 			if (i == 0) {
2226 			    printf("qd%d: qdaint: timeout on ID_SCROLL_READY\n",
2227 				qd);
2228 				return;
2229 			}
2230 
2231 			adder->ID_scroll_data = scroll[dv->dv_unit]->viper_constant;
2232 			adder->ID_scroll_command = ID_LOAD | SCROLL_CONSTANT;
2233 
2234 			adder->y_scroll_constant =
2235 				scroll[dv->dv_unit]->y_scroll_constant;
2236 			adder->y_offset_pending = scroll[dv->dv_unit]->y_offset;
2237 
2238 			if (scroll[dv->dv_unit]->status & LOAD_INDEX) {
2239 
2240 				adder->x_index_pending =
2241 					scroll[dv->dv_unit]->x_index_pending;
2242 				adder->y_index_pending =
2243 					scroll[dv->dv_unit]->y_index_pending;
2244 			}
2245 
2246 			scroll[dv->dv_unit]->status = 0x00;
2247 		}
2248 	}
2249 }
2250 
2251 /*
2252  *  DUART input interrupt service routine
2253  *
2254  *  XXX - this routine should be broken out - it is essentially
2255  *	      straight line code.
2256  */
2257 
2258 static void
2259 qdiint(arg)
2260 	void *arg;
2261 {
2262 	struct device *dv = arg;
2263 	struct _vs_event *event;
2264 	struct qdinput *eqh;
2265 	volatile struct dga *dga;
2266 	volatile struct duart *duart;
2267 	struct mouse_report *new_rep;
2268 	struct tty *tp;
2269 	u_short chr;
2270 	u_short status;
2271 	u_short data;
2272 	u_short key;
2273 	char do_wakeup = 0;		/* flag to do a select wakeup call */
2274 	char a, b, c;			/* mouse button test variables */
2275 
2276 	(void)spl4(); 			/* allow interval timer in */
2277 
2278 	eqh = eq_header[dv->dv_unit];		/* optimized as a register */
2279 	new_rep = &current_rep[dv->dv_unit];
2280 	duart = (struct duart *) qdmap[dv->dv_unit].duart;
2281 
2282 	/*
2283 	* if the graphic device is turned on..
2284 	*/
2285 	if (qdflags[dv->dv_unit].inuse & GRAPHIC_DEV) {
2286 		/*
2287 		* empty DUART
2288 		*/
2289 		while (duart->statusA&RCV_RDY || duart->statusB&RCV_RDY) {
2290 			/*
2291 			 * pick up LK-201 input (if any)
2292 			 */
2293 			if (duart->statusA&RCV_RDY) {
2294 
2295 				/* if error condition, then reset it */
2296 
2297 				if (duart->statusA&0x70) {
2298 					duart->cmdA = 0x40;
2299 					continue;
2300 				}
2301 
2302 				/* event queue full now? (overflow condition) */
2303 
2304 				if (ISFULL(eqh) == TRUE) {
2305 					printf(
2306 					 "qd%d: qdiint: event queue overflow\n",
2307 					   qd);
2308 					break;
2309 				}
2310 
2311 				/*
2312 				* Check for various keyboard errors  */
2313 
2314 				key = duart->dataA & 0xFF;
2315 
2316 				if (key==LK_POWER_ERROR ||
2317 				    key==LK_KDOWN_ERROR ||
2318 				    key == LK_INPUT_ERROR ||
2319 				    key == LK_OUTPUT_ERROR) {
2320 					printf(
2321 				    "qd%d: qdiint: keyboard error, code = %x\n",
2322 					qd,key);
2323 					return;
2324 				}
2325 
2326 				if (key < LK_LOWEST)
2327 				    return;
2328 
2329 				++do_wakeup;  /* request a select wakeup call */
2330 
2331 				event = PUTBEGIN(eqh);
2332 				PUTEND(eqh);
2333 
2334 				event->vse_key = key;
2335 				event->vse_key &= 0x00FF;
2336 				event->vse_x = eqh->curs_pos.x;
2337 				event->vse_y = eqh->curs_pos.y;
2338 				event->vse_time = TOY;
2339 				event->vse_type = VSE_BUTTON;
2340 				event->vse_direction = VSE_KBTRAW;
2341 				event->vse_device = VSE_DKB;
2342 			}
2343 
2344 			/*
2345 			* pick up the mouse input (if any)  */
2346 
2347 			if ((status = duart->statusB) & RCV_RDY  &&
2348 			    qdflags[dv->dv_unit].pntr_id == MOUSE_ID) {
2349 
2350 				if (status & 0x70) {
2351 					duart->cmdB = 0x40;
2352 					continue;
2353 				}
2354 
2355 				/* event queue full now? (overflow condition) */
2356 
2357 				if (ISFULL(eqh) == TRUE) {
2358 					printf(
2359 					"qd%d: qdiint: event queue overflow\n",
2360 					     qd);
2361 					break;
2362 				}
2363 
2364 				data = duart->dataB;      /* get report byte */
2365 				++new_rep->bytcnt; /* bump report byte count */
2366 
2367 				/*
2368 				* if 1st byte of report.. */
2369 
2370 				if ( data & START_FRAME) {
2371 					new_rep->state = data;
2372 					if (new_rep->bytcnt > 1) {
2373 						/* start of new frame */
2374 						new_rep->bytcnt = 1;
2375 						/* ..continue looking */
2376 						continue;
2377 					}
2378 				}
2379 
2380 				/*
2381 				* if 2nd byte of report.. */
2382 
2383 				else if (new_rep->bytcnt == 2) {
2384 					new_rep->dx = data & 0x00FF;
2385 				}
2386 
2387 				/*
2388 				* if 3rd byte of report, load input event queue */
2389 
2390 				else if (new_rep->bytcnt == 3) {
2391 
2392 					new_rep->dy = data & 0x00FF;
2393 					new_rep->bytcnt = 0;
2394 
2395 					/*
2396 					* if mouse position has changed.. */
2397 
2398 					if (new_rep->dx != 0  ||  new_rep->dy != 0) {
2399 
2400 						/*
2401 						* calculate acceleration factor, if needed	*/
2402 
2403 						if (qdflags[dv->dv_unit].curs_acc > ACC_OFF) {
2404 
2405 							if (qdflags[dv->dv_unit].curs_thr <= new_rep->dx)
2406 							    new_rep->dx +=
2407 							    (new_rep->dx - qdflags[dv->dv_unit].curs_thr)
2408 							    * qdflags[dv->dv_unit].curs_acc;
2409 
2410 							if (qdflags[dv->dv_unit].curs_thr <= new_rep->dy)
2411 							    new_rep->dy +=
2412 							    (new_rep->dy - qdflags[dv->dv_unit].curs_thr)
2413 							    * qdflags[dv->dv_unit].curs_acc;
2414 						}
2415 
2416 						/*
2417 						* update cursor position coordinates */
2418 
2419 						if (new_rep->state & X_SIGN) {
2420 							eqh->curs_pos.x += new_rep->dx;
2421 							if (eqh->curs_pos.x > 1023)
2422 							    eqh->curs_pos.x = 1023;
2423 						}
2424 						else {
2425 							eqh->curs_pos.x -= new_rep->dx;
2426 							if (eqh->curs_pos.x < -15)
2427 							    eqh->curs_pos.x = -15;
2428 						}
2429 
2430 						if (new_rep->state & Y_SIGN) {
2431 							eqh->curs_pos.y -= new_rep->dy;
2432 							if (eqh->curs_pos.y < -15)
2433 							    eqh->curs_pos.y = -15;
2434 						}
2435 						else {
2436 							eqh->curs_pos.y += new_rep->dy;
2437 							if (eqh->curs_pos.y > 863)
2438 							    eqh->curs_pos.y = 863;
2439 						}
2440 
2441 						/*
2442 						* update cursor screen position */
2443 
2444 						dga = (struct dga *) qdmap[dv->dv_unit].dga;
2445 						dga->x_cursor = TRANX(eqh->curs_pos.x);
2446 						dga->y_cursor = TRANY(eqh->curs_pos.y);
2447 
2448 						/*
2449 						* if cursor is in the box, no event report */
2450 
2451 						if (eqh->curs_pos.x <= eqh->curs_box.right	&&
2452 						    eqh->curs_pos.x >= eqh->curs_box.left  &&
2453 						    eqh->curs_pos.y >= eqh->curs_box.top  &&
2454 						    eqh->curs_pos.y <= eqh->curs_box.bottom ) {
2455 							goto GET_MBUTTON;
2456 						}
2457 
2458 						/*
2459 						* report the mouse motion event */
2460 
2461 						event = PUTBEGIN(eqh);
2462 						PUTEND(eqh);
2463 
2464 						++do_wakeup;   /* request a select wakeup call */
2465 
2466 						event->vse_x = eqh->curs_pos.x;
2467 						event->vse_y = eqh->curs_pos.y;
2468 
2469 						event->vse_device = VSE_MOUSE;  /* mouse */
2470 						event->vse_type = VSE_MMOTION;  /* pos changed */
2471 						event->vse_key = 0;
2472 						event->vse_direction = 0;
2473 						event->vse_time = TOY;	/* time stamp */
2474 					}
2475 
2476 GET_MBUTTON:
2477 					/*
2478 					* if button state has changed */
2479 
2480 					a = new_rep->state & 0x07;    /*mask nonbutton bits */
2481 					b = last_rep[dv->dv_unit].state & 0x07;
2482 
2483 					if (a ^ b) {
2484 
2485 						for ( c = 1;  c < 8; c <<= 1) {
2486 
2487 							if (!( c & (a ^ b))) /* this button change? */
2488 							    continue;
2489 
2490 							/* event queue full? (overflow condition) */
2491 
2492 							if (ISFULL(eqh) == TRUE) {
2493 								printf("qd%d: qdiint: event queue overflow\n", qd);
2494 								break;
2495 							}
2496 
2497 							event = PUTBEGIN(eqh);	/* get new event */
2498 							PUTEND(eqh);
2499 
2500 							++do_wakeup;   /* request select wakeup */
2501 
2502 							event->vse_x = eqh->curs_pos.x;
2503 							event->vse_y = eqh->curs_pos.y;
2504 
2505 							event->vse_device = VSE_MOUSE;	/* mouse */
2506 							event->vse_type = VSE_BUTTON; /* new button */
2507 							event->vse_time = TOY;	      /* time stamp */
2508 
2509 							/* flag changed button and if up or down */
2510 
2511 							if (c == RIGHT_BUTTON)
2512 							    event->vse_key = VSE_RIGHT_BUTTON;
2513 							else if (c == MIDDLE_BUTTON)
2514 							    event->vse_key = VSE_MIDDLE_BUTTON;
2515 							else if (c == LEFT_BUTTON)
2516 							    event->vse_key = VSE_LEFT_BUTTON;
2517 
2518 							/* set bit = button depressed */
2519 
2520 							if (c & a)
2521 							    event->vse_direction = VSE_KBTDOWN;
2522 							else
2523 								event->vse_direction = VSE_KBTUP;
2524 						}
2525 					}
2526 
2527 					/* refresh last report */
2528 
2529 					last_rep[dv->dv_unit] = current_rep[dv->dv_unit];
2530 
2531 				}  /* get last byte of report */
2532 			} else if ((status = duart->statusB)&RCV_RDY &&
2533 				   qdflags[dv->dv_unit].pntr_id == TABLET_ID) {
2534 				/*
2535 				* pickup tablet input, if any
2536 				*/
2537 				if (status&0x70) {
2538 					duart->cmdB = 0x40;
2539 					continue;
2540 				}
2541 				/*
2542 				 * event queue full now? (overflow condition)
2543 				 */
2544 				if (ISFULL(eqh) == TRUE) {
2545 					printf("qd%d: qdiint: event queue overflow\n", qd);
2546 					break;
2547 				}
2548 
2549 				data = duart->dataB;      /* get report byte */
2550 				++new_rep->bytcnt;	      /* bump report byte count */
2551 
2552 				/*
2553 				* if 1st byte of report.. */
2554 
2555 				if (data & START_FRAME) {
2556 					new_rep->state = data;
2557 					if (new_rep->bytcnt > 1) {
2558 						new_rep->bytcnt = 1;    /* start of new frame */
2559 						continue;		    /* ..continue looking */
2560 					}
2561 				}
2562 
2563 				/*
2564 				* if 2nd byte of report.. */
2565 
2566 				else if (new_rep->bytcnt == 2) {
2567 					new_rep->dx = data & 0x3F;
2568 				}
2569 
2570 				/*
2571 				* if 3rd byte of report.. */
2572 
2573 				else if (new_rep->bytcnt == 3) {
2574 					new_rep->dx |= (data & 0x3F) << 6;
2575 				}
2576 
2577 				/*
2578 				* if 4th byte of report.. */
2579 
2580 				else if (new_rep->bytcnt == 4) {
2581 					new_rep->dy = data & 0x3F;
2582 				}
2583 
2584 				/*
2585 				* if 5th byte of report, load input event queue */
2586 
2587 				else if (new_rep->bytcnt == 5) {
2588 
2589 					new_rep->dy |= (data & 0x3F) << 6;
2590 					new_rep->bytcnt = 0;
2591 
2592 					/*
2593 					* update cursor position coordinates */
2594 
2595 					new_rep->dx /= qdflags[dv->dv_unit].tab_res;
2596 					new_rep->dy = (2200 - new_rep->dy)
2597 					    / qdflags[dv->dv_unit].tab_res;
2598 
2599 					if (new_rep->dx > 1023) {
2600 						new_rep->dx = 1023;
2601 					}
2602 					if (new_rep->dy > 863) {
2603 						new_rep->dy = 863;
2604 					}
2605 
2606 					/*
2607 					* report an event if the puck/stylus has moved
2608 					*/
2609 
2610 					if (eqh->curs_pos.x != new_rep->dx ||
2611 					    eqh->curs_pos.y != new_rep->dy) {
2612 
2613 						eqh->curs_pos.x = new_rep->dx;
2614 						eqh->curs_pos.y = new_rep->dy;
2615 
2616 						/*
2617 						* update cursor screen position */
2618 
2619 						dga = (struct dga *) qdmap[dv->dv_unit].dga;
2620 						dga->x_cursor = TRANX(eqh->curs_pos.x);
2621 						dga->y_cursor = TRANY(eqh->curs_pos.y);
2622 
2623 						/*
2624 						* if cursor is in the box, no event report
2625 						*/
2626 
2627 						if (eqh->curs_pos.x <= eqh->curs_box.right	&&
2628 						    eqh->curs_pos.x >= eqh->curs_box.left  &&
2629 						    eqh->curs_pos.y >= eqh->curs_box.top  &&
2630 						    eqh->curs_pos.y <= eqh->curs_box.bottom ) {
2631 							goto GET_TBUTTON;
2632 						}
2633 
2634 						/*
2635 						* report the tablet motion event */
2636 
2637 						event = PUTBEGIN(eqh);
2638 						PUTEND(eqh);
2639 
2640 						++do_wakeup;   /* request a select wakeup call */
2641 
2642 						event->vse_x = eqh->curs_pos.x;
2643 						event->vse_y = eqh->curs_pos.y;
2644 
2645 						event->vse_device = VSE_TABLET;  /* tablet */
2646 						/*
2647 						* right now, X handles tablet motion the same
2648 						* as mouse motion
2649 						*/
2650 						event->vse_type = VSE_MMOTION;   /* pos changed */
2651 						event->vse_key = 0;
2652 						event->vse_direction = 0;
2653 						event->vse_time = TOY;	/* time stamp */
2654 					}
2655 GET_TBUTTON:
2656 					/*
2657 					* if button state has changed */
2658 
2659 					a = new_rep->state & 0x1E;   /* mask nonbutton bits */
2660 					b = last_rep[dv->dv_unit].state & 0x1E;
2661 
2662 					if (a ^ b) {
2663 
2664 						/* event queue full now? (overflow condition) */
2665 
2666 						if (ISFULL(eqh) == TRUE) {
2667 							printf("qd%d: qdiint: event queue overflow\n",qd);
2668 							break;
2669 						}
2670 
2671 						event = PUTBEGIN(eqh);  /* get new event */
2672 						PUTEND(eqh);
2673 
2674 						++do_wakeup;   /* request a select wakeup call */
2675 
2676 						event->vse_x = eqh->curs_pos.x;
2677 						event->vse_y = eqh->curs_pos.y;
2678 
2679 						event->vse_device = VSE_TABLET;  /* tablet */
2680 						event->vse_type = VSE_BUTTON; /* button changed */
2681 						event->vse_time = TOY;	   /* time stamp */
2682 
2683 						/* define the changed button and if up or down */
2684 
2685 						for ( c = 1;  c <= 0x10; c <<= 1) {
2686 							if (c & (a ^ b)) {
2687 								if (c == T_LEFT_BUTTON)
2688 								    event->vse_key = VSE_T_LEFT_BUTTON;
2689 								else if (c == T_FRONT_BUTTON)
2690 								    event->vse_key = VSE_T_FRONT_BUTTON;
2691 								else if (c == T_RIGHT_BUTTON)
2692 								    event->vse_key = VSE_T_RIGHT_BUTTON;
2693 								else if (c == T_BACK_BUTTON)
2694 								    event->vse_key = VSE_T_BACK_BUTTON;
2695 								break;
2696 							}
2697 						}
2698 
2699 						/* set bit = button depressed */
2700 
2701 						if (c & a)
2702 						    event->vse_direction = VSE_KBTDOWN;
2703 						else
2704 							event->vse_direction = VSE_KBTUP;
2705 					}
2706 
2707 					/* refresh last report */
2708 
2709 					last_rep[dv->dv_unit] = current_rep[dv->dv_unit];
2710 
2711 				} /* get last byte of report */
2712 			} /* pick up tablet input */
2713 
2714 		} /* while input available.. */
2715 
2716 		/*
2717 		* do select wakeup
2718 		*/
2719 		if (qdrsel[dv->dv_unit].si_pid && do_wakeup && qdflags[dv->dv_unit].selmask & SEL_READ) {
2720 			selwakeup(&qdrsel[dv->dv_unit]);
2721 			qdrsel[dv->dv_unit].si_pid = 0;
2722 			qdflags[dv->dv_unit].selmask &= ~SEL_READ;
2723 			do_wakeup = 0;
2724 		}
2725 	} else {
2726 		/*
2727 		 * if the graphic device is not turned on, this is console input
2728 		 */
2729 		if (qdpolling)
2730 			return;
2731 
2732 		if (dv->dv_unit >= qd_cd.cd_ndevs || qd_cd.cd_devs[dv->dv_unit] == NULL)
2733 			return;		/* no such device or address */
2734 
2735 		tp = qd_tty[dv->dv_unit << 2];
2736 
2737 		/*
2738 		 * Get a character from the keyboard.
2739 		 */
2740 		while (duart->statusA&RCV_RDY) {
2741 			key = duart->dataA;
2742 			key &= 0xFF;
2743 			/*
2744 			* Check for various keyboard errors
2745 			*/
2746 			if (key == LK_POWER_ERROR || key == LK_KDOWN_ERROR ||
2747 			    key == LK_INPUT_ERROR || key == LK_OUTPUT_ERROR) {
2748 				printf("qd%d: qdiint: Keyboard error, code = %x\n",qd,key);
2749 				return;
2750 			}
2751 
2752 			if (key < LK_LOWEST)
2753 			    return;
2754 
2755 			/*
2756 			* See if its a state change key */
2757 
2758 			switch (key) {
2759 
2760 			case LOCK:
2761 				q_keyboard.lock ^= 0xffff;	/* toggle */
2762 				if (q_keyboard.lock)
2763 					led_control(qd, LK_LED_ENABLE,
2764 							  LK_LED_LOCK);
2765 				else
2766 					led_control(qd, LK_LED_DISABLE,
2767 							  LK_LED_LOCK);
2768 				return;
2769 
2770 			case SHIFT:
2771 				q_keyboard.shift ^= 0xFFFF;
2772 				return;
2773 
2774 			case CNTRL:
2775 				q_keyboard.cntrl ^= 0xFFFF;
2776 				return;
2777 
2778 			case ALLUP:
2779 				q_keyboard.cntrl = 0;
2780 				q_keyboard.shift = 0;
2781 				return;
2782 
2783 			case REPEAT:
2784 				chr = q_keyboard.last;
2785 				break;
2786 
2787 				/*
2788 				* Test for cntrl characters. If set, see if the character
2789 				* is elligible to become a control character. */
2790 
2791 			default:
2792 
2793 				if (q_keyboard.cntrl) {
2794 					chr = q_key[key];
2795 					if (chr >= ' ' && chr <= '~')
2796 					    chr &= 0x1F;
2797 					else if (chr >= 0xA1 && chr <= 0xFE)
2798 					    chr &= 0x9F;
2799 				}
2800 				else if( q_keyboard.lock || q_keyboard.shift )
2801 				    chr = q_shift_key[key];
2802 				else
2803 					chr = q_key[key];
2804 				break;
2805 			}
2806 
2807 			q_keyboard.last = chr;
2808 
2809 			/*
2810 			* Check for special function keys */
2811 
2812 			if (chr & 0x100) {
2813 				char *string;
2814 				string = q_special[chr & 0x7F];
2815 				while(*string)
2816 				    (*tp->t_linesw->l_rint)(*string++, tp);
2817 			}
2818 			else {
2819 #ifdef DDB
2820 				/* Check for kernel debugger escape here */
2821 				int j;
2822 
2823 				j = kdbrint(chr&0177);
2824 
2825 				if (j == 1)  /* Escape received, just return */
2826 				    return;
2827 
2828 				if (j == 2)  /* Second char wasn't 'D' */
2829 				    (*tp->t_linesw->l_rint)(27, tp);
2830 #endif
2831 				(*tp->t_linesw->l_rint)(chr&0177, tp);
2832 			}
2833 		}
2834 	}
2835 } /* qdiint */
2836 
2837 /*
2838  *
2839  * Clear the QDSS screen
2840  *
2841  *			     >>> NOTE <<<
2842  *
2843  *   This code requires that certain adder initialization be valid.  To
2844  *   assure that this requirement is satisfied, this routine should be
2845  *   called only after calling the "setup_dragon()" function.
2846  *
2847  *   Clear the bitmap a piece at a time. Since the fast scroll clear
2848  *   only clears the current displayed portion of the bitmap put a
2849  *   temporary value in the y limit register so we can access whole
2850  *   bitmap
2851  *
2852  */
2853 void
2854 clear_qd_screen(unit)
2855 	int unit;
2856 {
2857 	volatile struct adder *adder;
2858 	adder = (struct adder *) qdmap[unit].adder;
2859 
2860 	adder->x_limit = 1024;
2861 	adder->y_limit = 2048 - CHAR_HEIGHT;
2862 	adder->y_offset_pending = 0;
2863 #define WSV  (void)wait_status(adder, VSYNC); (void)wait_status(adder, VSYNC)
2864 	WSV;
2865 	adder->y_scroll_constant = SCROLL_ERASE;
2866 	WSV;
2867 	adder->y_offset_pending = 864;
2868 	WSV;
2869 	adder->y_scroll_constant = SCROLL_ERASE;
2870 	WSV;
2871 	adder->y_offset_pending = 1728;
2872 	WSV;
2873 	adder->y_scroll_constant = SCROLL_ERASE;
2874 	WSV;
2875 	adder->y_offset_pending = 0;	 /* back to normal */
2876 	WSV;
2877 	adder->x_limit = MAX_SCREEN_X;
2878 	adder->y_limit = MAX_SCREEN_Y + FONT_HEIGHT;
2879 #undef WSV
2880 
2881 } /* clear_qd_screen */
2882 
2883 /*
2884  *  kernel console output to the glass tty
2885  */
2886 void
2887 qdcnputc(dev, chr)
2888 	dev_t dev;
2889 	int chr;
2890 {
2891 
2892 	/*
2893 	 * if system is now physical, forget it (ie: crash DUMP)
2894 	 */
2895 	if ((mfpr(PR_MAPEN) & 1) == 0)
2896 		return;
2897 
2898 	blitc(0, (u_char)(chr & 0xff));
2899 	if ((chr & 0177) == '\n')
2900 		blitc(0, '\r');
2901 
2902 } /* qdputc */
2903 
2904 /*
2905  *  load the mouse cursor's template RAM bitmap
2906  */
2907 void
2908 ldcursor(unit, bitmap)
2909 	int unit;
2910 	short *bitmap;
2911 {
2912 	volatile struct dga *dga;
2913 	volatile short *temp;
2914 	int i;
2915 	int curs;
2916 
2917 	dga = (struct dga *) qdmap[unit].dga;
2918 	temp = (short *) qdmap[unit].template;
2919 
2920 	if (dga->csr & CURS_ENB) {	/* if the cursor is enabled.. */
2921 		curs = -1;		/* ..note that.. */
2922 		dga->csr &= ~CURS_ENB;	/* ..and shut it off */
2923 	} else
2924 		curs = 0;
2925 
2926 	dga->csr &= ~CURS_ENB;		/* shut off the cursor */
2927 
2928 	temp += (8 * 1024) - 32;	/* cursor is 32 WORDS from the end */
2929 	/* ..of the 8k WORD template space */
2930 	for (i = 0; i < 32; ++i)
2931 		*temp++ = *bitmap++;
2932 
2933 	if (curs) {			/* if cursor was enabled.. */
2934 		dga->csr |= CURS_ENB;	/* ..turn it back on */
2935 	}
2936 
2937 } /* ldcursor */
2938 
2939 /*
2940  *  Put the console font in the QDSS off-screen memory
2941  */
2942 void
2943 ldfont(unit)
2944 	int unit;
2945 {
2946 	volatile struct adder *adder;
2947 
2948 	int i, j, k, max_chars_line;
2949 	short packed;
2950 
2951 	adder = (struct adder *) qdmap[unit].adder;
2952 
2953 	/*
2954 	* setup VIPER operand control registers
2955 	*/
2956 	write_ID(adder, MASK_1, 0xFFFF);
2957 	write_ID(adder, VIPER_Z_LOAD | FOREGROUND_COLOR_Z, 255);
2958 	write_ID(adder, VIPER_Z_LOAD | BACKGROUND_COLOR_Z, 0);
2959 
2960 	write_ID(adder, SRC1_OCR_B,
2961 	EXT_NONE | INT_NONE | ID | BAR_SHIFT_DELAY);
2962 	write_ID(adder, SRC2_OCR_B,
2963 	EXT_NONE | INT_NONE | ID | BAR_SHIFT_DELAY);
2964 	write_ID(adder, DST_OCR_B,
2965 	EXT_SOURCE | INT_NONE | NO_ID | NO_BAR_SHIFT_DELAY);
2966 
2967 	adder->rasterop_mode = DST_WRITE_ENABLE | DST_INDEX_ENABLE | NORMAL;
2968 
2969 	/*
2970 	* load destination data
2971 	*/
2972 	(void)wait_status(adder, RASTEROP_COMPLETE);
2973 
2974 	adder->destination_x = FONT_X;
2975 	adder->destination_y = FONT_Y;
2976 #if FONT_WIDTH > MAX_SCREEN_X
2977 	adder->fast_dest_dx = MAX_SCREEN_X;
2978 #else
2979 	adder->fast_dest_dx = FONT_WIDTH;
2980 #endif
2981 	adder->slow_dest_dy = CHAR_HEIGHT;
2982 
2983 	/*
2984 	* setup for processor to bitmap xfer  */
2985 
2986 	write_ID(adder, CS_UPDATE_MASK, 0x0001);
2987 	adder->cmd = PBT | OCRB | 2 | DTE | 2;
2988 
2989 	/*
2990 	* Figure out how many characters can be stored on one "line" of
2991 	* offscreen memory.
2992 	*/
2993 	max_chars_line = MAX_SCREEN_X/(CHAR_WIDTH*2);
2994 	if ((CHARS/2 + CHARS%2) < max_chars_line)
2995 	    max_chars_line = CHARS/2 + CHARS%2;
2996 
2997 	/*
2998 	* iteratively do the processor to bitmap xfer */
2999 
3000 	for (i = 0; i < ROWS; ++i) {
3001 
3002 		/* PTOB a scan line */
3003 
3004 		for (j = 0, k = i; j < max_chars_line; ++j) {
3005 			/* PTOB one scan of a char cell */
3006 
3007 			packed = q_font[k];
3008 			k += ROWS;
3009 			packed |= ((short)q_font[k] << 8);
3010 			k += ROWS;
3011 
3012 			(void)wait_status(adder, TX_READY);
3013 			adder->id_data = packed;
3014 		}
3015 	}
3016 
3017 	/*
3018 	 * (XXX XXX XXX - should remove)
3019 	 *
3020 	 * Copy the second row of characters.  Subtract the first
3021 	 * row from the total number.  Divide this quantity by 2
3022 	 * because 2 chars are stored in a short in the PTOB loop
3023 	 * below.  Figure out how many characters can be stored on
3024 	 * one "line" of offscreen memory
3025 	 */
3026 
3027 	max_chars_line = MAX_SCREEN_X/(CHAR_WIDTH*2);
3028 	if ((CHARS/2 + CHARS%2) < max_chars_line)
3029 	    return;
3030 	max_chars_line = (CHARS/2 + CHARS%2) - max_chars_line; /* 95 - 64 */
3031 	/* Paranoia check to see if 3rd row may be needed */
3032 	if (max_chars_line > (MAX_SCREEN_X/(CHAR_WIDTH*2)))
3033 	    max_chars_line = MAX_SCREEN_X/(CHAR_WIDTH*2);
3034 
3035 	adder->destination_x = FONT_X;
3036 	adder->destination_y = FONT_Y - CHAR_HEIGHT;
3037 	adder->fast_dest_dx = max_chars_line * CHAR_WIDTH * 2;
3038 	adder->slow_dest_dy = CHAR_HEIGHT;
3039 
3040 	/*
3041 	* setup for processor to bitmap xfer
3042 	*/
3043 	write_ID(adder, CS_UPDATE_MASK, 0x0001);
3044 	adder->cmd = PBT | OCRB | 2 | DTE | 2;
3045 
3046 	/*
3047 	* iteratively do the processor to bitmap xfer
3048 	*/
3049 	for (i = 0; i < ROWS; ++i) {
3050 		/*
3051 		 * PTOB a scan line
3052 		 */
3053 		for (j = 0, k = i; j < max_chars_line; ++j) {
3054 			/*
3055 			 * PTOB one scan of a char cell
3056 			 */
3057 			packed = q_font[k + FONT_OFFSET];
3058 			k += ROWS;
3059 			packed |= ((short)q_font[k + FONT_OFFSET] << 8);
3060 			k += ROWS;
3061 			(void)wait_status(adder, TX_READY);
3062 			adder->id_data = packed;
3063 		}
3064 	}
3065 
3066 }  /* ldfont */
3067 
3068 
3069 /*
3070  * Disable or enable polling.  This is used when entering or leaving the
3071  * kernel debugger.
3072  */
3073 void
3074 qdcnpollc(dev, onoff)
3075 	dev_t dev;
3076 	int onoff;
3077 {
3078      qdpolling = onoff;
3079 }
3080 
3081 
3082 /*
3083  *  Get a character from the LK201 (polled)
3084  */
3085 int
3086 qdcngetc(dev)
3087 	dev_t dev;
3088 {
3089 	short key;
3090 	char chr;
3091 	volatile struct duart *duart;
3092 
3093 	duart = (struct duart *) qdmap[0].duart;
3094 
3095 	/*
3096 	* Get a character from the keyboard.
3097 	*/
3098 LOOP:
3099 	while (!(duart->statusA&RCV_RDY))
3100 		;
3101 
3102 	key = duart->dataA;
3103 	key &= 0xFF;
3104 
3105 	/*
3106 	* Check for various keyboard errors  */
3107 
3108 	if (key == LK_POWER_ERROR || key == LK_KDOWN_ERROR ||
3109 	    key == LK_INPUT_ERROR || key == LK_OUTPUT_ERROR) {
3110 		printf("Keyboard error, code = %x\n", key);
3111 		return(0);
3112 	}
3113 
3114 	if (key < LK_LOWEST)
3115 		return(0);
3116 
3117 	/*
3118 	 * See if its a state change key
3119 	 */
3120 	switch (key) {
3121 
3122 	case LOCK:
3123 		q_keyboard.lock ^= 0xffff;	/* toggle */
3124 		if (q_keyboard.lock)
3125 			led_control(0, LK_LED_ENABLE, LK_LED_LOCK);
3126 		else
3127 			led_control(0, LK_LED_DISABLE, LK_LED_LOCK);
3128 		goto LOOP;
3129 
3130 	case SHIFT:
3131 		q_keyboard.shift ^= 0xFFFF;
3132 		goto LOOP;
3133 
3134 	case CNTRL:
3135 		q_keyboard.cntrl ^= 0xFFFF;
3136 		goto LOOP;
3137 
3138 	case ALLUP:
3139 		q_keyboard.cntrl = 0;
3140 		q_keyboard.shift = 0;
3141 		goto LOOP;
3142 
3143 	case REPEAT:
3144 		chr = q_keyboard.last;
3145 		break;
3146 
3147 		/*
3148 		* Test for cntrl characters. If set, see if the character
3149 		* is elligible to become a control character.
3150 		*/
3151 	default:
3152 
3153 		if (q_keyboard.cntrl) {
3154 			chr = q_key[key];
3155 			if (chr >= ' ' && chr <= '~')
3156 			    chr &= 0x1F;
3157 		}
3158 		else if ( q_keyboard.lock || q_keyboard.shift )
3159 		    chr = q_shift_key[key];
3160 		else
3161 			chr = q_key[key];
3162 		break;
3163 	}
3164 
3165 	if (chr < ' ' && chr > '~')	/* if input is non-displayable */
3166 		return(0);		/* ..then pitch it! */
3167 
3168 	q_keyboard.last = chr;
3169 
3170 	/*
3171 	* Check for special function keys */
3172 
3173 	if (chr & 0x80) 		/* pitch the function keys */
3174 		return(0);
3175 	else
3176 		return(chr);
3177 
3178 } /* qdgetc */
3179 
3180 /*
3181  *  led_control()... twiddle LK-201 LED's
3182  */
3183 void
3184 led_control(unit, cmd, led_mask)
3185 	int unit, cmd, led_mask;
3186 {
3187 	int i;
3188 	volatile struct duart *duart;
3189 
3190 	duart = (struct duart *)qdmap[unit].duart;
3191 
3192 	for (i = 1000; i > 0; --i) {
3193 		if (duart->statusA&XMT_RDY) {
3194 			duart->dataA = cmd;
3195 			break;
3196 		}
3197 	}
3198 	for (i = 1000; i > 0; --i) {
3199 		if (duart->statusA&XMT_RDY) {
3200 			duart->dataA = led_mask;
3201 			break;
3202 		}
3203 	}
3204 	return;
3205 
3206 } /* led_control */
3207 
3208 /*
3209  *  scroll_up()... move the screen up one character height
3210  */
3211 void
3212 scroll_up(adder)
3213 	volatile struct adder *adder;
3214 {
3215 	/*
3216 	* setup VIPER operand control registers
3217 	*/
3218 	(void)wait_status(adder, ADDRESS_COMPLETE);
3219 	write_ID(adder, CS_UPDATE_MASK, 0x00FF);  /* select all planes */
3220 	write_ID(adder, MASK_1, 0xFFFF);
3221 	write_ID(adder, VIPER_Z_LOAD | FOREGROUND_COLOR_Z, 255);
3222 	write_ID(adder, VIPER_Z_LOAD | BACKGROUND_COLOR_Z, 0);
3223 	write_ID(adder, SRC1_OCR_B,
3224 	EXT_NONE | INT_SOURCE | ID | BAR_SHIFT_DELAY);
3225 	write_ID(adder, DST_OCR_B,
3226 	EXT_NONE | INT_NONE | NO_ID | NO_BAR_SHIFT_DELAY);
3227 	/*
3228 	 * load DESTINATION origin and vectors
3229 	 */
3230 	adder->fast_dest_dy = 0;
3231 	adder->slow_dest_dx = 0;
3232 	adder->error_1 = 0;
3233 	adder->error_2 = 0;
3234 	adder->rasterop_mode = DST_WRITE_ENABLE | NORMAL;
3235 	adder->destination_x = 0;
3236 	adder->fast_dest_dx = 1024;
3237 	adder->destination_y = 0;
3238 	adder->slow_dest_dy = 864 - CHAR_HEIGHT;
3239 	/*
3240 	 * load SOURCE origin and vectors
3241 	 */
3242 	adder->source_1_x = 0;
3243 	adder->source_1_dx = 1024;
3244 	adder->source_1_y = 0 + CHAR_HEIGHT;
3245 	adder->source_1_dy = 864 - CHAR_HEIGHT;
3246 	write_ID(adder, LU_FUNCTION_R1, FULL_SRC_RESOLUTION | LF_SOURCE);
3247 	adder->cmd = RASTEROP | OCRB | 0 | S1E | DTE;
3248 	/*
3249 	 * do a rectangle clear of last screen line
3250 	 */
3251 	write_ID(adder, MASK_1, 0xffff);
3252 	write_ID(adder, SOURCE, 0xffff);
3253 	write_ID(adder,DST_OCR_B,
3254 	(EXT_NONE | INT_NONE | NO_ID | NO_BAR_SHIFT_DELAY));
3255 	write_ID(adder, VIPER_Z_LOAD | FOREGROUND_COLOR_Z, 0);
3256 	adder->error_1 = 0;
3257 	adder->error_2 = 0;
3258 	adder->slow_dest_dx = 0;		/* set up the width of	*/
3259 	adder->slow_dest_dy = CHAR_HEIGHT;	/* rectangle */
3260 	adder->rasterop_mode = (NORMAL | DST_WRITE_ENABLE) ;
3261 	(void)wait_status(adder, RASTEROP_COMPLETE);
3262 	adder->destination_x = 0;
3263 	adder->destination_y = 864 - CHAR_HEIGHT;
3264 	adder->fast_dest_dx = 1024;	/* set up the height	*/
3265 	adder->fast_dest_dy = 0;	/* of rectangle 	*/
3266 	write_ID(adder, LU_FUNCTION_R2, (FULL_SRC_RESOLUTION | LF_SOURCE));
3267 	adder->cmd = (RASTEROP | OCRB | LF_R2 | DTE ) ;
3268 
3269 } /* scroll_up */
3270 
3271 /*
3272  *  init shared memory pointers and structures
3273  */
3274 void
3275 init_shared(unit)
3276 	int unit;
3277 {
3278 	volatile struct dga *dga;
3279 
3280 	dga = (struct dga *) qdmap[unit].dga;
3281 
3282 	/*
3283 	* initialize the event queue pointers and header */
3284 
3285 	eq_header[unit] = (struct qdinput *)
3286 	    ((((int)event_shared & ~(0x01FF)) + 512)
3287 		+ (EVENT_BUFSIZE * unit));
3288 	eq_header[unit]->curs_pos.x = 0;
3289 	eq_header[unit]->curs_pos.y = 0;
3290 	dga->x_cursor = TRANX(eq_header[unit]->curs_pos.x);
3291 	dga->y_cursor = TRANY(eq_header[unit]->curs_pos.y);
3292 	eq_header[unit]->curs_box.left = 0;
3293 	eq_header[unit]->curs_box.right = 0;
3294 	eq_header[unit]->curs_box.top = 0;
3295 	eq_header[unit]->curs_box.bottom = 0;
3296 	/*
3297 	 * assign a pointer to the DMA I/O buffer for this QDSS.
3298 	 */
3299 	DMAheader[unit] = (struct DMAreq_header *)
3300 	    (((int)(&DMA_shared[0] + 512) & ~0x1FF)
3301 		+ (DMAbuf_size * unit));
3302 	DMAheader[unit]->DMAreq = (struct DMAreq *) ((int)DMAheader[unit]
3303 	    + sizeof(struct DMAreq_header));
3304 	DMAheader[unit]->QBAreg = 0;
3305 	DMAheader[unit]->status = 0;
3306 	DMAheader[unit]->shared_size = DMAbuf_size;
3307 	DMAheader[unit]->used = 0;
3308 	DMAheader[unit]->size = 10;	/* default = 10 requests */
3309 	DMAheader[unit]->oldest = 0;
3310 	DMAheader[unit]->newest = 0;
3311 	/*
3312 	* assign a pointer to the scroll structure for this QDSS.
3313 	*/
3314 	scroll[unit] = (struct scroll *)
3315 	    (((int)(&scroll_shared[0] + 512) & ~0x1FF)
3316 		+ (sizeof(struct scroll) * unit));
3317 	scroll[unit]->status = 0;
3318 	scroll[unit]->viper_constant = 0;
3319 	scroll[unit]->y_scroll_constant = 0;
3320 	scroll[unit]->y_offset = 0;
3321 	scroll[unit]->x_index_pending = 0;
3322 	scroll[unit]->y_index_pending = 0;
3323 	/*
3324 	* assign a pointer to the color map write buffer for this QDSS
3325 	*/
3326 	color_buf[unit] = (struct color_buf *)
3327 	    (((int)(&color_shared[0] + 512) & ~0x1FF)
3328 		+ (COLOR_BUFSIZ * unit));
3329 	color_buf[unit]->status = 0;
3330 	color_buf[unit]->count = 0;
3331 
3332 } /* init_shared */
3333 
3334 /*
3335  * init the ADDER, VIPER, bitmaps, & color map
3336  */
3337 void
3338 setup_dragon(unit)
3339 	int unit;
3340 {
3341 
3342 	volatile struct adder *adder;
3343 	volatile struct dga *dga;
3344 	volatile short *memcsr;
3345 	int i;
3346 	short top;		/* clipping/scrolling boundaries */
3347 	short bottom;
3348 	short right;
3349 	short left;
3350 	volatile short *red;		/* color map pointers */
3351 	volatile short *green;
3352 	volatile short *blue;
3353 
3354 	/*
3355 	* init for setup
3356 	*/
3357 	adder = (struct adder *) qdmap[unit].adder;
3358 	dga = (struct dga *) qdmap[unit].dga;
3359 	memcsr = (short *) qdmap[unit].memcsr;
3360 	dga->csr &= ~(DMA_IE | 0x700);	/* halt DMA and kill the intrpts */
3361 	*memcsr = SYNC_ON;		/* blank screen and turn off LED's */
3362 	adder->command = CANCEL;
3363 	/*
3364 	* set monitor timing
3365 	*/
3366 	adder->x_scan_count_0 = 0x2800;
3367 	adder->x_scan_count_1 = 0x1020;
3368 	adder->x_scan_count_2 = 0x003A;
3369 	adder->x_scan_count_3 = 0x38F0;
3370 	adder->x_scan_count_4 = 0x6128;
3371 	adder->x_scan_count_5 = 0x093A;
3372 	adder->x_scan_count_6 = 0x313C;
3373 	adder->sync_phase_adj = 0x0100;
3374 	adder->x_scan_conf = 0x00C8;
3375 	/*
3376 	 * got a bug in secound pass ADDER! lets take care of it
3377 	 *
3378 	 * normally, just use the code in the following bug fix code, but to
3379 	 * make repeated demos look pretty, load the registers as if there was
3380 	 * no bug and then test to see if we are getting sync
3381 	 */
3382 	adder->y_scan_count_0 = 0x135F;
3383 	adder->y_scan_count_1 = 0x3363;
3384 	adder->y_scan_count_2 = 0x2366;
3385 	adder->y_scan_count_3 = 0x0388;
3386 	/*
3387 	 * if no sync, do the bug fix code
3388 	 */
3389 	if (wait_status(adder, VSYNC) == BAD) {
3390 		/* first load all Y scan registers with very short frame and
3391 		 * wait for scroll service.  This guarantees at least one SYNC
3392 		 * to fix the pass 2 Adder initialization bug (synchronizes
3393 		 * XCINCH with DMSEEDH)
3394 		 */
3395 		adder->y_scan_count_0 = 0x01;
3396 		adder->y_scan_count_1 = 0x01;
3397 		adder->y_scan_count_2 = 0x01;
3398 		adder->y_scan_count_3 = 0x01;
3399 		/*
3400 		 * delay at least 1 full frame time
3401 		 */
3402 		(void)wait_status(adder, VSYNC);
3403 		(void)wait_status(adder, VSYNC);
3404 		/*
3405 		 * now load the REAL sync values (in reverse order just to
3406 		 * be safe.
3407 		 */
3408 		adder->y_scan_count_3 = 0x0388;
3409 		adder->y_scan_count_2 = 0x2366;
3410 		adder->y_scan_count_1 = 0x3363;
3411 		adder->y_scan_count_0 = 0x135F;
3412 	}
3413 	*memcsr = SYNC_ON | UNBLANK;	/* turn off leds and turn on video */
3414 	/*
3415 	 * zero the index registers
3416 	 */
3417 	adder->x_index_pending = 0;
3418 	adder->y_index_pending = 0;
3419 	adder->x_index_new = 0;
3420 	adder->y_index_new = 0;
3421 	adder->x_index_old = 0;
3422 	adder->y_index_old = 0;
3423 	adder->pause = 0;
3424 	/*
3425 	 * set rasterop mode to normal pen down
3426 	 */
3427 	adder->rasterop_mode = DST_WRITE_ENABLE | DST_INDEX_ENABLE | NORMAL;
3428 	/*
3429 	 * set the rasterop registers to a default values
3430 	 */
3431 	adder->source_1_dx = 1;
3432 	adder->source_1_dy = 1;
3433 	adder->source_1_x = 0;
3434 	adder->source_1_y = 0;
3435 	adder->destination_x = 0;
3436 	adder->destination_y = 0;
3437 	adder->fast_dest_dx = 1;
3438 	adder->fast_dest_dy = 0;
3439 	adder->slow_dest_dx = 0;
3440 	adder->slow_dest_dy = 1;
3441 	adder->error_1 = 0;
3442 	adder->error_2 = 0;
3443 	/*
3444 	 * scale factor = UNITY
3445 	 */
3446 	adder->fast_scale = UNITY;
3447 	adder->slow_scale = UNITY;
3448 	/*
3449 	 * set the source 2 parameters
3450 	 */
3451 	adder->source_2_x = 0;
3452 	adder->source_2_y = 0;
3453 	adder->source_2_size = 0x0022;
3454 	/*
3455 	* initialize plane addresses for eight vipers
3456 	*/
3457 	write_ID(adder, CS_UPDATE_MASK, 0x0001);
3458 	write_ID(adder, PLANE_ADDRESS, 0x0000);
3459 	write_ID(adder, CS_UPDATE_MASK, 0x0002);
3460 	write_ID(adder, PLANE_ADDRESS, 0x0001);
3461 	write_ID(adder, CS_UPDATE_MASK, 0x0004);
3462 	write_ID(adder, PLANE_ADDRESS, 0x0002);
3463 	write_ID(adder, CS_UPDATE_MASK, 0x0008);
3464 	write_ID(adder, PLANE_ADDRESS, 0x0003);
3465 	write_ID(adder, CS_UPDATE_MASK, 0x0010);
3466 	write_ID(adder, PLANE_ADDRESS, 0x0004);
3467 	write_ID(adder, CS_UPDATE_MASK, 0x0020);
3468 	write_ID(adder, PLANE_ADDRESS, 0x0005);
3469 	write_ID(adder, CS_UPDATE_MASK, 0x0040);
3470 	write_ID(adder, PLANE_ADDRESS, 0x0006);
3471 	write_ID(adder, CS_UPDATE_MASK, 0x0080);
3472 	write_ID(adder, PLANE_ADDRESS, 0x0007);
3473 	/*
3474 	 * initialize the external registers.
3475 	 */
3476 	write_ID(adder, CS_UPDATE_MASK, 0x00FF);
3477 	write_ID(adder, CS_SCROLL_MASK, 0x00FF);
3478 	/*
3479 	 * initialize resolution mode
3480 	 */
3481 	write_ID(adder, MEMORY_BUS_WIDTH, 0x000C);     /* bus width = 16 */
3482 	write_ID(adder, RESOLUTION_MODE, 0x0000);      /* one bit/pixel */
3483 	/*
3484 	 * initialize viper registers
3485 	 */
3486 	write_ID(adder, SCROLL_CONSTANT, SCROLL_ENABLE|VIPER_LEFT|VIPER_UP);
3487 	write_ID(adder, SCROLL_FILL, 0x0000);
3488 	/*
3489 	 * set clipping and scrolling limits to full screen
3490 	 */
3491 	for (i = 1000, adder->status = 0;
3492 	     i > 0 && !(adder->status&ADDRESS_COMPLETE); --i)
3493 		;
3494 	if (i == 0)
3495 	    printf("qd%d: setup_dragon: timeout on ADDRESS_COMPLETE\n",unit);
3496 	top = 0;
3497 	bottom = 2048;
3498 	left = 0;
3499 	right = 1024;
3500 	adder->x_clip_min = left;
3501 	adder->x_clip_max = right;
3502 	adder->y_clip_min = top;
3503 	adder->y_clip_max = bottom;
3504 	adder->scroll_x_min = left;
3505 	adder->scroll_x_max = right;
3506 	adder->scroll_y_min = top;
3507 	adder->scroll_y_max = bottom;
3508 	(void)wait_status(adder, VSYNC);	/* wait at LEAST 1 full frame */
3509 	(void)wait_status(adder, VSYNC);
3510 	adder->x_index_pending = left;
3511 	adder->y_index_pending = top;
3512 	adder->x_index_new = left;
3513 	adder->y_index_new = top;
3514 	adder->x_index_old = left;
3515 	adder->y_index_old = top;
3516 
3517 	for (i = 1000, adder->status = 0; i > 0 &&
3518 	     !(adder->status&ADDRESS_COMPLETE) ; --i)
3519 		;
3520 	if (i == 0)
3521 	       printf("qd%d: setup_dragon: timeout on ADDRESS_COMPLETE\n",unit);
3522 
3523 	write_ID(adder, LEFT_SCROLL_MASK, 0x0000);
3524 	write_ID(adder, RIGHT_SCROLL_MASK, 0x0000);
3525 	/*
3526 	* set source and the mask register to all ones (ie: white) o
3527 	*/
3528 	write_ID(adder, SOURCE, 0xFFFF);
3529 	write_ID(adder, MASK_1, 0xFFFF);
3530 	write_ID(adder, VIPER_Z_LOAD | FOREGROUND_COLOR_Z, 255);
3531 	write_ID(adder, VIPER_Z_LOAD | BACKGROUND_COLOR_Z, 0);
3532 	/*
3533 	* initialize Operand Control Register banks for fill command
3534 	*/
3535 	write_ID(adder, SRC1_OCR_A, EXT_NONE | INT_M1_M2  | NO_ID | WAIT);
3536 	write_ID(adder, SRC2_OCR_A, EXT_NONE | INT_SOURCE | NO_ID | NO_WAIT);
3537 	write_ID(adder, DST_OCR_A, EXT_NONE | INT_NONE	 | NO_ID | NO_WAIT);
3538 	write_ID(adder, SRC1_OCR_B, EXT_NONE | INT_SOURCE | NO_ID | WAIT);
3539 	write_ID(adder, SRC2_OCR_B, EXT_NONE | INT_M1_M2  | NO_ID | NO_WAIT);
3540 	write_ID(adder, DST_OCR_B, EXT_NONE | INT_NONE | NO_ID | NO_WAIT);
3541 	/*
3542 	* init Logic Unit Function registers, (these are just common values,
3543 	* and may be changed as required).
3544 	*/
3545 	write_ID(adder, LU_FUNCTION_R1, FULL_SRC_RESOLUTION | LF_SOURCE);
3546 	write_ID(adder, LU_FUNCTION_R2, FULL_SRC_RESOLUTION | LF_SOURCE |
3547 		 INV_M1_M2);
3548 	write_ID(adder, LU_FUNCTION_R3, FULL_SRC_RESOLUTION | LF_D_OR_S);
3549 	write_ID(adder, LU_FUNCTION_R4, FULL_SRC_RESOLUTION | LF_D_XOR_S);
3550 	/*
3551 	* load the color map for black & white
3552 	*/
3553 	for (i = 0, adder->status = 0; i < 10000 && !(adder->status&VSYNC); ++i)
3554 		;
3555 
3556 	if (i == 0)
3557 		printf("qd%d: setup_dragon: timeout on VSYNC\n", unit);
3558 
3559 	red = (short *) qdmap[unit].red;
3560 	green = (short *) qdmap[unit].green;
3561 	blue = (short *) qdmap[unit].blue;
3562 
3563 	*red++ = 0x00;			/* black */
3564 	*green++ = 0x00;
3565 	*blue++ = 0x00;
3566 
3567 	*red-- = 0xFF;			/* white */
3568 	*green-- = 0xFF;
3569 	*blue-- = 0xFF;
3570 
3571 	/*
3572 	* set color map for mouse cursor
3573 	*/
3574 
3575 	red += 254;
3576 	green += 254;
3577 	blue += 254;
3578 
3579 	*red++ = 0x00;			/* black */
3580 	*green++ = 0x00;
3581 	*blue++ = 0x00;
3582 
3583 	*red = 0xFF;			/* white */
3584 	*green = 0xFF;
3585 	*blue = 0xFF;
3586 
3587 } /* setup_dragon */
3588 
3589 /*
3590  * Init the DUART and set defaults in input
3591  */
3592 void
3593 setup_input(unit)
3594 	int unit;
3595 {
3596 	volatile struct duart *duart;	/* DUART register structure pointer */
3597 	int i, bits;
3598 	char id_byte;
3599 
3600 	duart = (struct duart *) qdmap[unit].duart;
3601 	duart->imask = 0;
3602 
3603 	/*
3604 	* setup the DUART for kbd & pointing device
3605 	*/
3606 	duart->cmdA = RESET_M;	/* reset mode reg ptr for kbd */
3607 	duart->modeA = 0x13;	/* 8 bits, no parity, rcv IE, */
3608 				/* no RTS control,char error mode */
3609 	duart->modeA = 0x07;	/* 1 stop bit,CTS does not IE XMT */
3610 				/* no RTS control,no echo or loop */
3611 	duart->cmdB = RESET_M;	/* reset mode reg pntr for host */
3612 	duart->modeB = 0x07;	/* 8 bits, odd parity, rcv IE.. */
3613 				/* ..no RTS cntrl, char error mode */
3614 	duart->modeB = 0x07;	/* 1 stop bit,CTS does not IE XMT */
3615 				/* no RTS control,no echo or loop */
3616 	duart->auxctl = 0x00;	/* baud rate set 1 */
3617 	duart->clkselA = 0x99;	/* 4800 baud for kbd */
3618 	duart->clkselB = 0x99;	/* 4800 baud for mouse */
3619 
3620 	/* reset everything for keyboard */
3621 
3622 	for (bits = RESET_M; bits < START_BREAK; bits += 0x10)
3623 		duart->cmdA = bits;
3624 
3625 	/* reset everything for host */
3626 
3627 	for (bits = RESET_M; bits < START_BREAK; bits += 0x10)
3628 		duart->cmdB = bits;
3629 
3630 	duart->cmdA = EN_RCV | EN_XMT; /* enbl xmt & rcv for kbd */
3631 	duart->cmdB = EN_RCV | EN_XMT; /* enbl xmt & rcv for pointer device */
3632 
3633 	/*
3634 	* init keyboard defaults (DUART channel A)
3635 	*/
3636 	for (i = 500; i > 0; --i) {
3637 		if (duart->statusA&XMT_RDY) {
3638 			duart->dataA = LK_DEFAULTS;
3639 			break;
3640 		}
3641 	}
3642 
3643 	for (i = 100000; i > 0; --i) {
3644 		if (duart->statusA&RCV_RDY) {
3645 			break;
3646 		}
3647 	}
3648 
3649 	if (duart->dataA)	/* flush the ACK */
3650 		;
3651 
3652 	/*
3653 	* identify the pointing device
3654 	*/
3655 	for (i = 500; i > 0; --i) {
3656 		if (duart->statusB&XMT_RDY) {
3657 			duart->dataB = SELF_TEST;
3658 			break;
3659 		}
3660 	}
3661 
3662 	/*
3663 	* wait for 1st byte of self test report */
3664 
3665 	for (i = 100000; i > 0; --i) {
3666 		if (duart->statusB&RCV_RDY) {
3667 			break;
3668 		}
3669 	}
3670 
3671 	if (i == 0) {
3672 		printf("qd[%d]: setup_input: timeout on 1st byte of self test\n"
3673 		       ,unit);
3674 		goto OUT;
3675 	}
3676 
3677 	if (duart->dataB)
3678 		;
3679 
3680 	/*
3681 	* wait for ID byte of self test report
3682 	*/
3683 	for (i = 100000; i > 0; --i) {
3684 		if (duart->statusB&RCV_RDY) {
3685 			break;
3686 		}
3687 	}
3688 
3689 	if (i == 0) {
3690 		printf("qd[%d]: setup_input: timeout on 2nd byte of self test\n", unit);
3691 		goto OUT;
3692 	}
3693 
3694 	id_byte = duart->dataB;
3695 
3696 	/*
3697 	* wait for other bytes to come in
3698 	*/
3699 	for (i = 100000; i > 0; --i) {
3700 		if (duart->statusB & RCV_RDY) {
3701 			if (duart->dataB)
3702 				;
3703 			break;
3704 		}
3705 	}
3706 	if (i == 0) {
3707 		printf("qd[%d]: setup_input: timeout on 3rd byte of self test\n", unit);
3708 		goto OUT;
3709 	}
3710 	for (i = 100000; i > 0; --i) {
3711 		if (duart->statusB&RCV_RDY) {
3712 			if (duart->dataB)
3713 				;
3714 			break;
3715 		}
3716 	}
3717 	if (i == 0) {
3718 		printf("qd[%d]: setup_input: timeout on 4th byte of self test\n", unit);
3719 		goto OUT;
3720 	}
3721 	/*
3722 	* flag pointing device type and set defaults
3723 	*/
3724 	for (i=100000; i>0; --i)
3725 		;		/*XXX*/
3726 
3727 	if ((id_byte & 0x0F) != TABLET_ID) {
3728 		qdflags[unit].pntr_id = MOUSE_ID;
3729 
3730 		for (i = 500; i > 0; --i) {
3731 			if (duart->statusB&XMT_RDY) {
3732 				duart->dataB = INC_STREAM_MODE;
3733 				break;
3734 			}
3735 		}
3736 	}
3737 	else {
3738 		qdflags[unit].pntr_id = TABLET_ID;
3739 
3740 		for (i = 500; i > 0; --i) {
3741 			if (duart->statusB&XMT_RDY) {
3742 				duart->dataB = T_STREAM;
3743 				break;
3744 			}
3745 		}
3746 	}
3747 OUT:
3748 	duart->imask = qdflags[unit].duart_imask;
3749 
3750 } /* setup_input */
3751 
3752 /*
3753  * delay for at least one display frame time
3754  *
3755  *	return: BAD means that we timed out without ever seeing the
3756  *		      vertical sync status bit
3757  *		GOOD otherwise
3758  */
3759 int
3760 wait_status(adder, mask)
3761 	volatile struct adder *adder;
3762 	int mask;
3763 {
3764 	int i;
3765 
3766 	for (i = 10000, adder->status = 0 ; i > 0  &&
3767 	     !(adder->status&mask) ; --i)
3768 		;
3769 
3770 	if (i == 0) {
3771 		printf("wait_status: timeout polling for 0x%x in adder->status\n", mask);
3772 		return(BAD);
3773 	}
3774 
3775 	return(GOOD);
3776 
3777 } /* wait_status */
3778 
3779 /*
3780  * write out onto the ID bus
3781  */
3782 void
3783 write_ID(adder, adrs, data)
3784 	volatile struct adder *adder;
3785 	short adrs;
3786 	short data;
3787 {
3788 	int i;
3789 
3790 	for (i = 100000, adder->status = 0 ;
3791 	      i > 0  &&  !(adder->status&ADDRESS_COMPLETE) ; --i)
3792 		;
3793 
3794 	if (i == 0)
3795 		goto ERR;
3796 
3797 	for (i = 100000, adder->status = 0 ;
3798 	      i > 0  &&  !(adder->status&TX_READY) ; --i)
3799 		;
3800 
3801 	if (i > 0) {
3802 		adder->id_data = data;
3803 		adder->command = ID_LOAD | adrs;
3804 		return ;
3805 	}
3806 
3807 ERR:
3808 	printf("write_ID: timeout trying to write to VIPER\n");
3809 	return ;
3810 
3811 } /* write_ID */
3812