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