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