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