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