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