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