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