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