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