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