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