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