Home | History | Annotate | Line # | Download | only in qbus
qd.c revision 1.20
      1 /*	$NetBSD: qd.c,v 1.20 2000/06/28 17:09:43 mrg 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 ((*linesw[tp->t_line].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 		(*linesw[tp->t_line].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 		   (*linesw[tp->t_line].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 
   1557 		if (events & (POLLIN | POLLRDNORM))  {
   1558 		     /* This is ttnread.  It's static and I don't feel
   1559 		      * like altering platform independant parts of NetBSD
   1560 		      */
   1561 		     int nread;
   1562 		     /* if (tp->t_lflag & PENDIN)
   1563 				     ttypend(tp); */
   1564 		     nread = tp->t_canq.c_cc;
   1565 		     if (!(tp->t_lflag & ICANON))  {
   1566 			     nread += tp->t_rawq.c_cc;
   1567 			     if (nread < tp->t_cc[VMIN] && !tp->t_cc[VTIME])
   1568 				     nread = 0;
   1569 		     }
   1570 		     if (nread > 0)
   1571 			     revents |= events & (POLLIN | POLLRDNORM);
   1572 		}
   1573 
   1574 		if (events & (POLLOUT | POLLWRNORM))
   1575 			if (tp->t_outq.c_cc <= tp->t_lowat)
   1576 				revents |= events & (POLLOUT | POLLWRNORM);
   1577 
   1578 		if (revents == 0)  {
   1579 			if (events & (POLLIN | POLLRDNORM))
   1580 				selrecord(p, &tp->t_rsel);
   1581 
   1582 			if (events & (POLLOUT | POLLWRNORM))
   1583 				selrecord(p, &tp->t_wsel);
   1584 		}
   1585 	}
   1586 
   1587 	splx(s);
   1588 	return (revents);
   1589 } /* qdpoll() */
   1590 
   1591 
   1592 void qd_strategy(struct buf *bp);
   1593 
   1594 /*ARGSUSED*/
   1595 int
   1596 qdwrite(dev, uio, flag)
   1597 	dev_t dev;
   1598 	struct uio *uio;
   1599 {
   1600 	struct tty *tp;
   1601 	int minor_dev;
   1602 	int unit;
   1603 
   1604 	minor_dev = minor(dev);
   1605 	unit = (minor_dev >> 2) & 0x07;
   1606 
   1607 	if (((minor_dev&0x03) != 0x02) && (qdflags[unit].inuse&CONS_DEV)) {
   1608 	       /*
   1609 		* this is the console...
   1610 		*/
   1611 		tp = qd_tty[minor_dev];
   1612 		return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
   1613 	} else if (qdflags[unit].inuse & GRAPHIC_DEV) {
   1614 	       /*
   1615 		* this is a DMA xfer from user space
   1616 		*/
   1617 		return (physio(qd_strategy, &qdbuf[unit],
   1618 		dev, B_WRITE, minphys, uio));
   1619 	}
   1620 	return (ENXIO);
   1621 }
   1622 
   1623 /*ARGSUSED*/
   1624 int
   1625 qdread(dev, uio, flag)
   1626 	dev_t dev;
   1627 	struct uio *uio;
   1628 {
   1629 	struct tty *tp;
   1630 	int minor_dev;
   1631 	int unit;
   1632 
   1633 	minor_dev = minor(dev);
   1634 	unit = (minor_dev >> 2) & 0x07;
   1635 
   1636 	if ((minor_dev & 0x03) != 0x02 && qdflags[unit].inuse & CONS_DEV) {
   1637 	       /*
   1638 		* this is the console
   1639 		*/
   1640 		tp = qd_tty[minor_dev];
   1641 		return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
   1642 	} else if (qdflags[unit].inuse & GRAPHIC_DEV) {
   1643 	       /*
   1644 		* this is a bitmap-to-processor xfer
   1645 		*/
   1646 		return (physio(qd_strategy, &qdbuf[unit],
   1647 		dev, B_READ, minphys, uio));
   1648 	}
   1649 	return (ENXIO);
   1650 }
   1651 
   1652 /***************************************************************
   1653 *
   1654 *	qd_strategy()... strategy routine to do DMA
   1655 *
   1656 ***************************************************************/
   1657 
   1658 void
   1659 qd_strategy(bp)
   1660 	struct buf *bp;
   1661 {
   1662 	volatile struct dga *dga;
   1663 	volatile struct adder *adder;
   1664 	int unit;
   1665 	int QBAreg;
   1666 	int s;
   1667 	int cookie;
   1668 	struct uba_softc *uh;
   1669 
   1670 	unit = (minor(bp->b_dev) >> 2) & 0x07;
   1671 
   1672 	uh = (struct uba_softc *)
   1673 	     (((struct device *)(qd_cd.cd_devs[unit]))->dv_parent);
   1674 
   1675 	/*
   1676 	* init pointers
   1677 	*/
   1678 	dga = (struct dga *) qdmap[unit].dga;
   1679 panic("qd_strategy");
   1680 #ifdef notyet
   1681 	if ((QBAreg = ubasetup(uh, bp, 0)) == 0) {
   1682 		printf("qd%d: qd_strategy: QBA setup error\n", unit);
   1683 		goto STRAT_ERR;
   1684 	}
   1685 #endif
   1686 	s = spl5();
   1687 	qdflags[unit].user_dma = -1;
   1688 	dga->csr |= DMA_IE;
   1689 	cookie = QBAreg & 0x3FFFF;
   1690 	dga->adrs_lo = (short) cookie;
   1691 	dga->adrs_hi = (short) (cookie >> 16);
   1692 	dga->bytcnt_lo = (short) bp->b_bcount;
   1693 	dga->bytcnt_hi = (short) (bp->b_bcount >> 16);
   1694 
   1695 	while (qdflags[unit].user_dma) {
   1696 		(void) tsleep(&qdflags[unit].user_dma, QSPRIOR,
   1697 		    "qdstrat", 0);
   1698 	}
   1699 	splx(s);
   1700 #ifdef notyet
   1701 	ubarelse(uh, &QBAreg);
   1702 #endif
   1703 	if (!(dga->csr & DMA_ERR)) {
   1704 		biodone(bp);
   1705 		return;
   1706 	}
   1707 
   1708 /* STRAT_ERR: */
   1709 	adder = (struct adder *) qdmap[unit].adder;
   1710 	adder->command = CANCEL;	/* cancel adder activity */
   1711 	dga->csr &= ~DMA_IE;
   1712 	dga->csr &= ~0x0600;		/* halt DMA (reset fifo) */
   1713 	dga->csr |= DMA_ERR;		/* clear error condition */
   1714 	bp->b_flags |= B_ERROR; 	/* flag an error to physio() */
   1715 
   1716 	/*
   1717 	 * if DMA was running, flush spurious intrpt
   1718 	 */
   1719 	if (dga->bytcnt_lo != 0) {
   1720 		dga->bytcnt_lo = 0;
   1721 		dga->bytcnt_hi = 0;
   1722 		DMA_SETIGNORE(DMAheader[unit]);
   1723 		dga->csr |= DMA_IE;
   1724 	}
   1725 	biodone(bp);
   1726 } /* qd_strategy */
   1727 
   1728 
   1729 /*
   1730  *  Start output to the console screen
   1731  */
   1732 void qdstart(tp)
   1733 	struct tty *tp;
   1734 {
   1735 	int which_unit, unit, c;
   1736 	int s;
   1737 
   1738 	unit = minor(tp->t_dev);
   1739 	which_unit = (unit >> 2) & 0x3;
   1740 	unit &= 0x03;
   1741 
   1742 	s = spl5();
   1743 
   1744 	/*
   1745 	* If it's currently active, or delaying, no need to do anything.
   1746 	*/
   1747 	if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
   1748 		goto out;
   1749 
   1750 	/*
   1751 	* Display chars until the queue is empty.
   1752 	* Drop input from anything but the console
   1753 	* device on the floor.
   1754 	*
   1755 	* XXX - this loop is done at spltty.
   1756 	*
   1757 	*/
   1758 	while (tp->t_outq.c_cc) {
   1759 		c = getc(&tp->t_outq);
   1760 		if (unit == 0)
   1761 			blitc(which_unit, (u_char)c);
   1762 	}
   1763 	/*
   1764 	* If there are sleepers, and output has drained below low
   1765 	* water mark, wake up the sleepers.
   1766 	*/
   1767 	if (tp->t_outq.c_cc <= tp->t_lowat) {
   1768 		if (tp->t_state & TS_ASLEEP){
   1769 			tp->t_state &= ~TS_ASLEEP;
   1770 			wakeup((caddr_t) &tp->t_outq);
   1771 		}
   1772 	}
   1773 
   1774 	tp->t_state &= ~TS_BUSY;
   1775 
   1776 out:
   1777 	splx(s);
   1778 
   1779 } /* qdstart */
   1780 
   1781 /*ARGSUSED*/
   1782 void
   1783 qdstop(tp, flag)
   1784 	struct tty *tp;
   1785 	int flag;
   1786 {
   1787 	int s;
   1788 
   1789 	s = spl5();	/* block intrpts during state modification */
   1790 	if (tp->t_state & TS_BUSY) {
   1791 		if ((tp->t_state & TS_TTSTOP) == 0)
   1792 			tp->t_state |= TS_FLUSH;
   1793 		else
   1794 			tp->t_state &= ~TS_BUSY;
   1795 	}
   1796 	splx(s);
   1797 }
   1798 
   1799 /*
   1800  *  Output a character to the QDSS screen
   1801  */
   1802 void
   1803 blitc(unit, chr)
   1804 	int unit;
   1805 	u_char chr;
   1806 {
   1807 	volatile struct adder *adder;
   1808 	volatile struct dga *dga;
   1809 	int i;
   1810 	int nograph = !(qdflags[unit].inuse&GRAPHIC_DEV);
   1811 	static short inescape[NQD];
   1812 
   1813 	adder = (struct adder *)qdmap[unit].adder;
   1814 	dga = (struct dga *) qdmap[unit].dga;
   1815 	/*
   1816 	 * BSD comment: this (&=0177) defeats the extended character
   1817 	 * set code for the glass tty, but if i had the time i would
   1818 	 * spend it ripping out the code completely.  This driver
   1819 	 * is too big for its own good.
   1820 	 */
   1821 	chr &= 0177;
   1822 	/*
   1823 	 * Cursor addressing (so vi will work).
   1824 	 * Decode for "\E=%.%." cursor motion description.
   1825 	 * Corresponds to type "qdcons" in /etc/termcap:
   1826 	 *
   1827 	 *    qd|qdss|qdcons|qdss glass tty (4.4 BSD):\
   1828 	 *      :am:do=^J:le=^H:bs:cm=\E=%.%.:cl=1^Z:co#128:li#57::nd=^L:up=^K:
   1829 	 *
   1830 	 */
   1831 	if (inescape[unit] && nograph) {
   1832 		switch (inescape[unit]++) {
   1833 		case 1:
   1834 			if (chr != '=') {
   1835 				/* abort escape sequence */
   1836 				inescape[unit] = 0;
   1837 				blitc(unit, chr);
   1838 			}
   1839 			return;
   1840 		case 2:
   1841 			/* position row */
   1842 			cursor[unit].y = CHAR_HEIGHT * chr;
   1843 			if (cursor[unit].y > 863 - CHAR_HEIGHT)
   1844 				cursor[unit].y = 863 - CHAR_HEIGHT;
   1845 			dga->y_cursor = TRANY(cursor[unit].y);
   1846 			return;
   1847 		case 3:
   1848 			/* position column */
   1849 			cursor[unit].x = CHAR_WIDTH * chr;
   1850 			if (cursor[unit].x > 1024 - CHAR_WIDTH)
   1851 				cursor[unit].x = 1023 - CHAR_WIDTH;
   1852 			dga->x_cursor = TRANX(cursor[unit].x);
   1853 			inescape[unit] = 0;
   1854 			return;
   1855 		default:
   1856 			inescape[unit] = 0;
   1857 			blitc(unit, chr);
   1858 		}
   1859 	}
   1860 
   1861 	switch (chr) {
   1862 	case '\r':			/* return char */
   1863 		cursor[unit].x = 0;
   1864 		if (nograph)
   1865 			dga->x_cursor = TRANX(cursor[unit].x);
   1866 		return;
   1867 
   1868 	case '\t':			/* tab char */
   1869 		for (i = 8 - ((cursor[unit].x >> 3) & 0x07); i > 0; --i) {
   1870 			blitc(unit, ' ');
   1871 		}
   1872 		return;
   1873 
   1874 	case '\n':			/* line feed char */
   1875 		if ((cursor[unit].y += CHAR_HEIGHT) > (863 - CHAR_HEIGHT)) {
   1876 			if (nograph) {
   1877 				cursor[unit].y -= CHAR_HEIGHT;
   1878 				scroll_up(adder);
   1879 			} else
   1880 				cursor[unit].y = 0;
   1881 		}
   1882 		if (nograph)
   1883 			dga->y_cursor = TRANY(cursor[unit].y);
   1884 		return;
   1885 
   1886 	case '\b':			/* backspace char */
   1887 		if (cursor[unit].x > 0) {
   1888 			cursor[unit].x -= CHAR_WIDTH;
   1889 			if (nograph)
   1890 				dga->x_cursor = TRANX(cursor[unit].x);
   1891 		}
   1892 		return;
   1893 	case CTRL('k'):		/* cursor up */
   1894 		if (nograph && cursor[unit].y > 0) {
   1895 			cursor[unit].y -= CHAR_HEIGHT;
   1896 			dga->y_cursor = TRANY(cursor[unit].y);
   1897 		}
   1898 		return;
   1899 
   1900 	case CTRL('^'):		/* home cursor */
   1901 		if (nograph) {
   1902 			cursor[unit].x = 0;
   1903 			dga->x_cursor = TRANX(cursor[unit].x);
   1904 			cursor[unit].y = 0;
   1905 			dga->y_cursor = TRANY(cursor[unit].y);
   1906 		}
   1907 		return;
   1908 
   1909 	case CTRL('l'):		/* cursor right */
   1910 		if (nograph && cursor[unit].x < 1023 - CHAR_WIDTH) {
   1911 			cursor[unit].x += CHAR_WIDTH;
   1912 			dga->x_cursor = TRANX(cursor[unit].x);
   1913 		}
   1914 		return;
   1915 
   1916 	case CTRL('z'):		/* clear screen */
   1917 		if (nograph) {
   1918 			setup_dragon(unit);
   1919 			clear_qd_screen(unit);
   1920 			/* home cursor - termcap seems to assume this */
   1921 			cursor[unit].x = 0;
   1922 			dga->x_cursor = TRANX(cursor[unit].x);
   1923 			cursor[unit].y = 0;
   1924 			dga->y_cursor = TRANY(cursor[unit].y);
   1925 		}
   1926 		return;
   1927 
   1928 	case '\033':		/* start escape sequence */
   1929 		if (nograph)
   1930 			inescape[unit] = 1;
   1931 		return;
   1932 
   1933 	default:
   1934 		if ((chr < ' ') || (chr > '~'))
   1935 			return;
   1936 	}
   1937 	/*
   1938 	 * setup VIPER operand control registers
   1939 	 */
   1940 	write_ID(adder, CS_UPDATE_MASK, 0x0001);  /* select plane #0 */
   1941 	write_ID(adder, SRC1_OCR_B,
   1942 	EXT_NONE | INT_SOURCE | ID | BAR_SHIFT_DELAY);
   1943 	write_ID(adder, CS_UPDATE_MASK, 0x00FE);  /* select other planes */
   1944 	write_ID(adder, SRC1_OCR_B,
   1945 	EXT_SOURCE | INT_NONE | NO_ID | BAR_SHIFT_DELAY);
   1946 	write_ID(adder, CS_UPDATE_MASK, 0x00FF);  /* select all planes */
   1947 	write_ID(adder, DST_OCR_B,
   1948 	EXT_NONE | INT_NONE | NO_ID | NO_BAR_SHIFT_DELAY);
   1949 	write_ID(adder, MASK_1, 0xFFFF);
   1950 	write_ID(adder, VIPER_Z_LOAD | FOREGROUND_COLOR_Z, 1);
   1951 	write_ID(adder, VIPER_Z_LOAD | BACKGROUND_COLOR_Z, 0);
   1952 	adder->x_clip_min = 0;
   1953 	adder->x_clip_max = 1024;
   1954 	adder->y_clip_min = 0;
   1955 	adder->y_clip_max = 864;
   1956 	/*
   1957 	 * load DESTINATION origin and vectors
   1958 	 */
   1959 	adder->fast_dest_dy = 0;
   1960 	adder->slow_dest_dx = 0;
   1961 	adder->error_1 = 0;
   1962 	adder->error_2 = 0;
   1963 	adder->rasterop_mode = DST_WRITE_ENABLE | NORMAL;
   1964 	(void)wait_status(adder, RASTEROP_COMPLETE);
   1965 	adder->destination_x = cursor[unit].x;
   1966 	adder->fast_dest_dx = CHAR_WIDTH;
   1967 	adder->destination_y = cursor[unit].y;
   1968 	adder->slow_dest_dy = CHAR_HEIGHT;
   1969 	/*
   1970 	 * load SOURCE origin and vectors
   1971 	 */
   1972 	if ((chr - ' ') > (CHARS - 1))  {
   1973 		printf("Invalid character (x)%x in blitc\n",chr);
   1974 		chr = ' ';
   1975 	}
   1976 	/*
   1977 	 * X position is modulo the number of characters per line
   1978 	 */
   1979 	adder->source_1_x = FONT_X +
   1980 	    (((chr - ' ') % (MAX_SCREEN_X/CHAR_WIDTH)) * CHAR_WIDTH);
   1981 	/*
   1982 	 * Point to either first or second row
   1983 	 */
   1984 	adder->source_1_y = 2048 - 15 *
   1985 	    (((chr - ' ')/(MAX_SCREEN_X/CHAR_WIDTH)) + 1);
   1986 	adder->source_1_dx = CHAR_WIDTH;
   1987 	adder->source_1_dy = CHAR_HEIGHT;
   1988 	write_ID(adder, LU_FUNCTION_R1, FULL_SRC_RESOLUTION | LF_SOURCE);
   1989 	adder->cmd = RASTEROP | OCRB | 0 | S1E | DTE;
   1990 	/*
   1991 	 * update console cursor coordinates
   1992 	 */
   1993 	cursor[unit].x += CHAR_WIDTH;
   1994 	if (nograph)
   1995 		dga->x_cursor = TRANX(cursor[unit].x);
   1996 	if (cursor[unit].x > (1024 - CHAR_WIDTH)) {
   1997 		blitc(unit, '\r');
   1998 		blitc(unit, '\n');
   1999 	}
   2000 
   2001 } /* blitc */
   2002 
   2003 /*
   2004  *  INTERRUPT SERVICE ROUTINES
   2005  */
   2006 
   2007 /*
   2008  *  Service "DMA DONE" interrupt condition
   2009  */
   2010 
   2011 static void
   2012 qddint(arg)
   2013 	void *arg;
   2014 {
   2015 	struct device *dv = arg;
   2016 	struct DMAreq_header *header;
   2017 	struct DMAreq *request;
   2018 	volatile struct dga *dga;
   2019 	volatile struct adder *adder;
   2020 	int cookie;			/* DMA adrs for QDSS */
   2021 
   2022 	(void)spl4(); 			/* allow interval timer in */
   2023 
   2024 	/*
   2025 	* init pointers
   2026 	*/
   2027 	header = DMAheader[dv->dv_unit]; 	    /* register for optimization */
   2028 	dga = (struct dga *) qdmap[dv->dv_unit].dga;
   2029 	adder = (struct adder *) qdmap[dv->dv_unit].adder;
   2030 
   2031 	/*
   2032 	* if this interrupt flagged as bogus for interrupt flushing purposes..
   2033 	*/
   2034 	if (DMA_ISIGNORE(header)) {
   2035 	   DMA_CLRIGNORE(header);
   2036 		return;
   2037 	}
   2038 
   2039 	/*
   2040 	* dump a DMA hardware error message if appropriate
   2041 	*/
   2042 	if (dga->csr & DMA_ERR) {
   2043 
   2044 		if (dga->csr & PARITY_ERR)
   2045 		    printf("qd%d: qddint: DMA hardware parity fault.\n", dv->dv_unit);
   2046 
   2047 		if (dga->csr & BUS_ERR)
   2048 		    printf("qd%d: qddint: DMA hardware bus error.\n", dv->dv_unit);
   2049 	}
   2050 
   2051 	/*
   2052 	* if this was a DMA from user space...
   2053 	*/
   2054 	if (qdflags[dv->dv_unit].user_dma) {
   2055 		qdflags[dv->dv_unit].user_dma = 0;
   2056 		wakeup((caddr_t)&qdflags[dv->dv_unit].user_dma);
   2057 		return;
   2058 	}
   2059 
   2060 	/*
   2061 	* if we're doing DMA request queue services, field the error condition
   2062 	*/
   2063 	if (dga->csr & DMA_ERR) {
   2064 
   2065 		dga->csr &= ~0x0600;		/* halt DMA (reset fifo) */
   2066 		dga->csr |= DMA_ERR;		/* clear error condition */
   2067 		adder->command = CANCEL;	/* cancel adder activity */
   2068 
   2069 		DMA_SETERROR(header);	/* flag error in header status word */
   2070 		DMA_CLRACTIVE(header);
   2071 		header->DMAreq[header->oldest].DMAdone |= HARD_ERROR;
   2072 		header->newest = header->oldest;
   2073 		header->used = 0;
   2074 
   2075 		if (qdrsel[dv->dv_unit].si_pid && qdflags[dv->dv_unit].selmask & SEL_WRITE) {
   2076 			selwakeup(&qdrsel[dv->dv_unit]);
   2077 			qdrsel[dv->dv_unit].si_pid = 0;
   2078 			qdflags[dv->dv_unit].selmask &= ~SEL_WRITE;
   2079 		}
   2080 
   2081 		if (dga->bytcnt_lo != 0) {
   2082 			dga->bytcnt_lo = 0;
   2083 			dga->bytcnt_hi = 0;
   2084 			DMA_SETIGNORE(header);
   2085 		}
   2086 		return;
   2087 	}
   2088 
   2089 	/*
   2090 	* if the DMA request queue is now becoming non-full,
   2091 	* wakeup "select" client.
   2092 	*/
   2093 	if (DMA_ISFULL(header)) {
   2094 		if (qdrsel[dv->dv_unit].si_pid && qdflags[dv->dv_unit].selmask & SEL_WRITE) {
   2095 			selwakeup(&qdrsel[dv->dv_unit]);
   2096 			qdrsel[dv->dv_unit].si_pid = 0;
   2097 			qdflags[dv->dv_unit].selmask &= ~SEL_WRITE;
   2098 		}
   2099 	}
   2100 
   2101 	header->DMAreq[header->oldest].DMAdone |= REQUEST_DONE;
   2102 	QDlast_DMAtype = header->DMAreq[header->oldest].DMAtype;
   2103 
   2104 	/* check for unexpected interrupt */
   2105 	if (DMA_ISEMPTY(header))
   2106 	    return;
   2107 
   2108 	DMA_GETEND(header);	/* update request queue indices */
   2109 
   2110 	/*
   2111 	* if no more DMA pending, wake up "select" client and exit
   2112 	*/
   2113 	if (DMA_ISEMPTY(header)) {
   2114 		if (qdrsel[dv->dv_unit].si_pid && qdflags[dv->dv_unit].selmask & SEL_WRITE) {
   2115 			selwakeup(&qdrsel[dv->dv_unit]);
   2116 			qdrsel[dv->dv_unit].si_pid = 0;
   2117 			qdflags[dv->dv_unit].selmask &= ~SEL_WRITE;
   2118 		}
   2119 		DMA_CLRACTIVE(header);  /* flag DMA done */
   2120 		return;
   2121 	}
   2122 
   2123 	/*
   2124 	* initiate next DMA xfer
   2125 	*/
   2126 	request = DMA_GETBEGIN(header);
   2127 	if (request->DMAtype != QDlast_DMAtype) {
   2128 		dga->csr &= ~0x0600;	  /* halt DMA (reset fifo) */
   2129 		adder->command = CANCEL;  /* cancel adder activity */
   2130 	}
   2131 
   2132 
   2133 	switch (request->DMAtype) {
   2134 
   2135 	case DISPLIST:
   2136 		if (request->DMAtype != QDlast_DMAtype) {
   2137 			dga->csr |= DL_ENB;
   2138 			dga->csr &= ~(BTOP_ENB | BYTE_DMA);
   2139 		}
   2140 		break;
   2141 
   2142 	case PTOB:
   2143 		if (request->DMAtype != QDlast_DMAtype) {
   2144 			if (request->DMAdone & BYTE_PACK)
   2145 			    dga->csr |= (PTOB_ENB | BYTE_DMA);
   2146 			else {
   2147 				dga->csr |= PTOB_ENB;
   2148 				dga->csr &= ~BYTE_DMA;
   2149 			}
   2150 		}
   2151 		break;
   2152 
   2153 	case BTOP:
   2154 		if (request->DMAtype != QDlast_DMAtype) {
   2155 			if (request->DMAdone & BYTE_PACK) {
   2156 				dga->csr &= ~DL_ENB;
   2157 				dga->csr |= (BTOP_ENB | BYTE_DMA);
   2158 			}
   2159 			else {
   2160 				dga->csr |= BTOP_ENB;
   2161 				dga->csr &= ~(BYTE_DMA | DL_ENB);
   2162 			}
   2163 		}
   2164 		break;
   2165 	default:
   2166 		printf("qd%d: qddint: illegal DMAtype parameter.\n", dv->dv_unit);
   2167 		DMA_CLRACTIVE(header);	/* flag DMA done */
   2168 		return;
   2169 	}
   2170 
   2171 	if (request->DMAdone & COUNT_ZERO) {
   2172 		dga->csr &= ~SET_DONE_FIFO;
   2173 	}
   2174 	else if (request->DMAdone & FIFO_EMPTY) {
   2175 		dga->csr |= SET_DONE_FIFO;
   2176 	}
   2177 
   2178 	if (request->DMAdone & WORD_PACK)
   2179 	    dga->csr &= ~BYTE_DMA;
   2180 	else if (request->DMAdone & BYTE_PACK)
   2181 	    dga->csr |= BYTE_DMA;
   2182 
   2183 	dga->csr |= DMA_IE;
   2184 	QDlast_DMAtype = request->DMAtype;
   2185 
   2186 	cookie = ((int)request->bufp - (int)header) + (int)header->QBAreg;
   2187 
   2188 	dga->adrs_lo = (short) cookie;
   2189 	dga->adrs_hi = (short) (cookie >> 16);
   2190 
   2191 	dga->bytcnt_lo = (short) request->length;
   2192 	dga->bytcnt_hi = (short) (request->length >> 16);
   2193 
   2194 	return;
   2195 }
   2196 
   2197 /*
   2198  * ADDER interrupt service routine
   2199  */
   2200 static void
   2201 qdaint(arg)
   2202 	void *arg;
   2203 {
   2204 	struct device *dv = arg;
   2205 	volatile struct adder *adder;
   2206 	struct color_buf *cbuf;
   2207 	int i;
   2208 	struct rgb *rgbp;
   2209 	volatile short *red;
   2210 	volatile short *green;
   2211 	volatile short *blue;
   2212 
   2213 	(void)spl4(); 			/* allow interval timer in */
   2214 
   2215 	adder = (struct adder *) qdmap[dv->dv_unit].adder;
   2216 
   2217 	/*
   2218 	* service the vertical blank interrupt (VSYNC bit) by loading
   2219 	* any pending color map load request
   2220 	*/
   2221 	if (adder->status & VSYNC) {
   2222 		adder->status &= ~VSYNC;	/* clear the interrupt */
   2223 		cbuf = color_buf[dv->dv_unit];
   2224 		if (cbuf->status & LOAD_COLOR_MAP) {
   2225 
   2226 			red = (short *) qdmap[dv->dv_unit].red;
   2227 			green = (short *) qdmap[dv->dv_unit].green;
   2228 			blue = (short *) qdmap[dv->dv_unit].blue;
   2229 
   2230 			for (i = cbuf->count, rgbp = cbuf->rgb;
   2231 			     --i >= 0; rgbp++) {
   2232 				red[rgbp->offset] = (short) rgbp->red;
   2233 				green[rgbp->offset] = (short) rgbp->green;
   2234 				blue[rgbp->offset] = (short) rgbp->blue;
   2235 			}
   2236 
   2237 			cbuf->status &= ~LOAD_COLOR_MAP;
   2238 		}
   2239 	}
   2240 
   2241 	/*
   2242 	* service the scroll interrupt (FRAME_SYNC bit)
   2243 	*/
   2244 	if (adder->status & FRAME_SYNC) {
   2245 		adder->status &= ~FRAME_SYNC;	/* clear the interrupt */
   2246 
   2247 		if (scroll[dv->dv_unit]->status & LOAD_REGS) {
   2248 
   2249 			for (i = 1000, adder->status = 0; i > 0 &&
   2250 			     !(adder->status&ID_SCROLL_READY); --i)
   2251 			      ;
   2252 
   2253 			if (i == 0) {
   2254 			    printf("qd%d: qdaint: timeout on ID_SCROLL_READY\n",
   2255 				qd);
   2256 				return;
   2257 			}
   2258 
   2259 			adder->ID_scroll_data = scroll[dv->dv_unit]->viper_constant;
   2260 			adder->ID_scroll_command = ID_LOAD | SCROLL_CONSTANT;
   2261 
   2262 			adder->y_scroll_constant =
   2263 				scroll[dv->dv_unit]->y_scroll_constant;
   2264 			adder->y_offset_pending = scroll[dv->dv_unit]->y_offset;
   2265 
   2266 			if (scroll[dv->dv_unit]->status & LOAD_INDEX) {
   2267 
   2268 				adder->x_index_pending =
   2269 					scroll[dv->dv_unit]->x_index_pending;
   2270 				adder->y_index_pending =
   2271 					scroll[dv->dv_unit]->y_index_pending;
   2272 			}
   2273 
   2274 			scroll[dv->dv_unit]->status = 0x00;
   2275 		}
   2276 	}
   2277 }
   2278 
   2279 /*
   2280  *  DUART input interrupt service routine
   2281  *
   2282  *  XXX - this routine should be broken out - it is essentially
   2283  *	      straight line code.
   2284  */
   2285 
   2286 static void
   2287 qdiint(arg)
   2288 	void *arg;
   2289 {
   2290 	struct device *dv = arg;
   2291 	struct _vs_event *event;
   2292 	struct qdinput *eqh;
   2293 	volatile struct dga *dga;
   2294 	volatile struct duart *duart;
   2295 	struct mouse_report *new_rep;
   2296 	struct tty *tp;
   2297 	u_short chr;
   2298 	u_short status;
   2299 	u_short data;
   2300 	u_short key;
   2301 	char do_wakeup = 0;		/* flag to do a select wakeup call */
   2302 	char a, b, c;			/* mouse button test variables */
   2303 
   2304 	(void)spl4(); 			/* allow interval timer in */
   2305 
   2306 	eqh = eq_header[dv->dv_unit];		/* optimized as a register */
   2307 	new_rep = &current_rep[dv->dv_unit];
   2308 	duart = (struct duart *) qdmap[dv->dv_unit].duart;
   2309 
   2310 	/*
   2311 	* if the graphic device is turned on..
   2312 	*/
   2313 	if (qdflags[dv->dv_unit].inuse & GRAPHIC_DEV) {
   2314 		/*
   2315 		* empty DUART
   2316 		*/
   2317 		while (duart->statusA&RCV_RDY || duart->statusB&RCV_RDY) {
   2318 			/*
   2319 			 * pick up LK-201 input (if any)
   2320 			 */
   2321 			if (duart->statusA&RCV_RDY) {
   2322 
   2323 				/* if error condition, then reset it */
   2324 
   2325 				if (duart->statusA&0x70) {
   2326 					duart->cmdA = 0x40;
   2327 					continue;
   2328 				}
   2329 
   2330 				/* event queue full now? (overflow condition) */
   2331 
   2332 				if (ISFULL(eqh) == TRUE) {
   2333 					printf(
   2334 					 "qd%d: qdiint: event queue overflow\n",
   2335 					   qd);
   2336 					break;
   2337 				}
   2338 
   2339 				/*
   2340 				* Check for various keyboard errors  */
   2341 
   2342 				key = duart->dataA & 0xFF;
   2343 
   2344 				if (key==LK_POWER_ERROR ||
   2345 				    key==LK_KDOWN_ERROR ||
   2346 				    key == LK_INPUT_ERROR ||
   2347 				    key == LK_OUTPUT_ERROR) {
   2348 					printf(
   2349 				    "qd%d: qdiint: keyboard error, code = %x\n",
   2350 					qd,key);
   2351 					return;
   2352 				}
   2353 
   2354 				if (key < LK_LOWEST)
   2355 				    return;
   2356 
   2357 				++do_wakeup;  /* request a select wakeup call */
   2358 
   2359 				event = PUTBEGIN(eqh);
   2360 				PUTEND(eqh);
   2361 
   2362 				event->vse_key = key;
   2363 				event->vse_key &= 0x00FF;
   2364 				event->vse_x = eqh->curs_pos.x;
   2365 				event->vse_y = eqh->curs_pos.y;
   2366 				event->vse_time = TOY;
   2367 				event->vse_type = VSE_BUTTON;
   2368 				event->vse_direction = VSE_KBTRAW;
   2369 				event->vse_device = VSE_DKB;
   2370 			}
   2371 
   2372 			/*
   2373 			* pick up the mouse input (if any)  */
   2374 
   2375 			if ((status = duart->statusB) & RCV_RDY  &&
   2376 			    qdflags[dv->dv_unit].pntr_id == MOUSE_ID) {
   2377 
   2378 				if (status & 0x70) {
   2379 					duart->cmdB = 0x40;
   2380 					continue;
   2381 				}
   2382 
   2383 				/* event queue full now? (overflow condition) */
   2384 
   2385 				if (ISFULL(eqh) == TRUE) {
   2386 					printf(
   2387 					"qd%d: qdiint: event queue overflow\n",
   2388 					     qd);
   2389 					break;
   2390 				}
   2391 
   2392 				data = duart->dataB;      /* get report byte */
   2393 				++new_rep->bytcnt; /* bump report byte count */
   2394 
   2395 				/*
   2396 				* if 1st byte of report.. */
   2397 
   2398 				if ( data & START_FRAME) {
   2399 					new_rep->state = data;
   2400 					if (new_rep->bytcnt > 1) {
   2401 						/* start of new frame */
   2402 						new_rep->bytcnt = 1;
   2403 						/* ..continue looking */
   2404 						continue;
   2405 					}
   2406 				}
   2407 
   2408 				/*
   2409 				* if 2nd byte of report.. */
   2410 
   2411 				else if (new_rep->bytcnt == 2) {
   2412 					new_rep->dx = data & 0x00FF;
   2413 				}
   2414 
   2415 				/*
   2416 				* if 3rd byte of report, load input event queue */
   2417 
   2418 				else if (new_rep->bytcnt == 3) {
   2419 
   2420 					new_rep->dy = data & 0x00FF;
   2421 					new_rep->bytcnt = 0;
   2422 
   2423 					/*
   2424 					* if mouse position has changed.. */
   2425 
   2426 					if (new_rep->dx != 0  ||  new_rep->dy != 0) {
   2427 
   2428 						/*
   2429 						* calculate acceleration factor, if needed	*/
   2430 
   2431 						if (qdflags[dv->dv_unit].curs_acc > ACC_OFF) {
   2432 
   2433 							if (qdflags[dv->dv_unit].curs_thr <= new_rep->dx)
   2434 							    new_rep->dx +=
   2435 							    (new_rep->dx - qdflags[dv->dv_unit].curs_thr)
   2436 							    * qdflags[dv->dv_unit].curs_acc;
   2437 
   2438 							if (qdflags[dv->dv_unit].curs_thr <= new_rep->dy)
   2439 							    new_rep->dy +=
   2440 							    (new_rep->dy - qdflags[dv->dv_unit].curs_thr)
   2441 							    * qdflags[dv->dv_unit].curs_acc;
   2442 						}
   2443 
   2444 						/*
   2445 						* update cursor position coordinates */
   2446 
   2447 						if (new_rep->state & X_SIGN) {
   2448 							eqh->curs_pos.x += new_rep->dx;
   2449 							if (eqh->curs_pos.x > 1023)
   2450 							    eqh->curs_pos.x = 1023;
   2451 						}
   2452 						else {
   2453 							eqh->curs_pos.x -= new_rep->dx;
   2454 							if (eqh->curs_pos.x < -15)
   2455 							    eqh->curs_pos.x = -15;
   2456 						}
   2457 
   2458 						if (new_rep->state & Y_SIGN) {
   2459 							eqh->curs_pos.y -= new_rep->dy;
   2460 							if (eqh->curs_pos.y < -15)
   2461 							    eqh->curs_pos.y = -15;
   2462 						}
   2463 						else {
   2464 							eqh->curs_pos.y += new_rep->dy;
   2465 							if (eqh->curs_pos.y > 863)
   2466 							    eqh->curs_pos.y = 863;
   2467 						}
   2468 
   2469 						/*
   2470 						* update cursor screen position */
   2471 
   2472 						dga = (struct dga *) qdmap[dv->dv_unit].dga;
   2473 						dga->x_cursor = TRANX(eqh->curs_pos.x);
   2474 						dga->y_cursor = TRANY(eqh->curs_pos.y);
   2475 
   2476 						/*
   2477 						* if cursor is in the box, no event report */
   2478 
   2479 						if (eqh->curs_pos.x <= eqh->curs_box.right	&&
   2480 						    eqh->curs_pos.x >= eqh->curs_box.left  &&
   2481 						    eqh->curs_pos.y >= eqh->curs_box.top  &&
   2482 						    eqh->curs_pos.y <= eqh->curs_box.bottom ) {
   2483 							goto GET_MBUTTON;
   2484 						}
   2485 
   2486 						/*
   2487 						* report the mouse motion event */
   2488 
   2489 						event = PUTBEGIN(eqh);
   2490 						PUTEND(eqh);
   2491 
   2492 						++do_wakeup;   /* request a select wakeup call */
   2493 
   2494 						event->vse_x = eqh->curs_pos.x;
   2495 						event->vse_y = eqh->curs_pos.y;
   2496 
   2497 						event->vse_device = VSE_MOUSE;  /* mouse */
   2498 						event->vse_type = VSE_MMOTION;  /* pos changed */
   2499 						event->vse_key = 0;
   2500 						event->vse_direction = 0;
   2501 						event->vse_time = TOY;	/* time stamp */
   2502 					}
   2503 
   2504 GET_MBUTTON:
   2505 					/*
   2506 					* if button state has changed */
   2507 
   2508 					a = new_rep->state & 0x07;    /*mask nonbutton bits */
   2509 					b = last_rep[dv->dv_unit].state & 0x07;
   2510 
   2511 					if (a ^ b) {
   2512 
   2513 						for ( c = 1;  c < 8; c <<= 1) {
   2514 
   2515 							if (!( c & (a ^ b))) /* this button change? */
   2516 							    continue;
   2517 
   2518 							/* event queue full? (overflow condition) */
   2519 
   2520 							if (ISFULL(eqh) == TRUE) {
   2521 								printf("qd%d: qdiint: event queue overflow\n", qd);
   2522 								break;
   2523 							}
   2524 
   2525 							event = PUTBEGIN(eqh);	/* get new event */
   2526 							PUTEND(eqh);
   2527 
   2528 							++do_wakeup;   /* request select wakeup */
   2529 
   2530 							event->vse_x = eqh->curs_pos.x;
   2531 							event->vse_y = eqh->curs_pos.y;
   2532 
   2533 							event->vse_device = VSE_MOUSE;	/* mouse */
   2534 							event->vse_type = VSE_BUTTON; /* new button */
   2535 							event->vse_time = TOY;	      /* time stamp */
   2536 
   2537 							/* flag changed button and if up or down */
   2538 
   2539 							if (c == RIGHT_BUTTON)
   2540 							    event->vse_key = VSE_RIGHT_BUTTON;
   2541 							else if (c == MIDDLE_BUTTON)
   2542 							    event->vse_key = VSE_MIDDLE_BUTTON;
   2543 							else if (c == LEFT_BUTTON)
   2544 							    event->vse_key = VSE_LEFT_BUTTON;
   2545 
   2546 							/* set bit = button depressed */
   2547 
   2548 							if (c & a)
   2549 							    event->vse_direction = VSE_KBTDOWN;
   2550 							else
   2551 								event->vse_direction = VSE_KBTUP;
   2552 						}
   2553 					}
   2554 
   2555 					/* refresh last report */
   2556 
   2557 					last_rep[dv->dv_unit] = current_rep[dv->dv_unit];
   2558 
   2559 				}  /* get last byte of report */
   2560 			} else if ((status = duart->statusB)&RCV_RDY &&
   2561 				   qdflags[dv->dv_unit].pntr_id == TABLET_ID) {
   2562 				/*
   2563 				* pickup tablet input, if any
   2564 				*/
   2565 				if (status&0x70) {
   2566 					duart->cmdB = 0x40;
   2567 					continue;
   2568 				}
   2569 				/*
   2570 				 * event queue full now? (overflow condition)
   2571 				 */
   2572 				if (ISFULL(eqh) == TRUE) {
   2573 					printf("qd%d: qdiint: event queue overflow\n", qd);
   2574 					break;
   2575 				}
   2576 
   2577 				data = duart->dataB;      /* get report byte */
   2578 				++new_rep->bytcnt;	      /* bump report byte count */
   2579 
   2580 				/*
   2581 				* if 1st byte of report.. */
   2582 
   2583 				if (data & START_FRAME) {
   2584 					new_rep->state = data;
   2585 					if (new_rep->bytcnt > 1) {
   2586 						new_rep->bytcnt = 1;    /* start of new frame */
   2587 						continue;		    /* ..continue looking */
   2588 					}
   2589 				}
   2590 
   2591 				/*
   2592 				* if 2nd byte of report.. */
   2593 
   2594 				else if (new_rep->bytcnt == 2) {
   2595 					new_rep->dx = data & 0x3F;
   2596 				}
   2597 
   2598 				/*
   2599 				* if 3rd byte of report.. */
   2600 
   2601 				else if (new_rep->bytcnt == 3) {
   2602 					new_rep->dx |= (data & 0x3F) << 6;
   2603 				}
   2604 
   2605 				/*
   2606 				* if 4th byte of report.. */
   2607 
   2608 				else if (new_rep->bytcnt == 4) {
   2609 					new_rep->dy = data & 0x3F;
   2610 				}
   2611 
   2612 				/*
   2613 				* if 5th byte of report, load input event queue */
   2614 
   2615 				else if (new_rep->bytcnt == 5) {
   2616 
   2617 					new_rep->dy |= (data & 0x3F) << 6;
   2618 					new_rep->bytcnt = 0;
   2619 
   2620 					/*
   2621 					* update cursor position coordinates */
   2622 
   2623 					new_rep->dx /= qdflags[dv->dv_unit].tab_res;
   2624 					new_rep->dy = (2200 - new_rep->dy)
   2625 					    / qdflags[dv->dv_unit].tab_res;
   2626 
   2627 					if (new_rep->dx > 1023) {
   2628 						new_rep->dx = 1023;
   2629 					}
   2630 					if (new_rep->dy > 863) {
   2631 						new_rep->dy = 863;
   2632 					}
   2633 
   2634 					/*
   2635 					* report an event if the puck/stylus has moved
   2636 					*/
   2637 
   2638 					if (eqh->curs_pos.x != new_rep->dx ||
   2639 					    eqh->curs_pos.y != new_rep->dy) {
   2640 
   2641 						eqh->curs_pos.x = new_rep->dx;
   2642 						eqh->curs_pos.y = new_rep->dy;
   2643 
   2644 						/*
   2645 						* update cursor screen position */
   2646 
   2647 						dga = (struct dga *) qdmap[dv->dv_unit].dga;
   2648 						dga->x_cursor = TRANX(eqh->curs_pos.x);
   2649 						dga->y_cursor = TRANY(eqh->curs_pos.y);
   2650 
   2651 						/*
   2652 						* if cursor is in the box, no event report
   2653 						*/
   2654 
   2655 						if (eqh->curs_pos.x <= eqh->curs_box.right	&&
   2656 						    eqh->curs_pos.x >= eqh->curs_box.left  &&
   2657 						    eqh->curs_pos.y >= eqh->curs_box.top  &&
   2658 						    eqh->curs_pos.y <= eqh->curs_box.bottom ) {
   2659 							goto GET_TBUTTON;
   2660 						}
   2661 
   2662 						/*
   2663 						* report the tablet motion event */
   2664 
   2665 						event = PUTBEGIN(eqh);
   2666 						PUTEND(eqh);
   2667 
   2668 						++do_wakeup;   /* request a select wakeup call */
   2669 
   2670 						event->vse_x = eqh->curs_pos.x;
   2671 						event->vse_y = eqh->curs_pos.y;
   2672 
   2673 						event->vse_device = VSE_TABLET;  /* tablet */
   2674 						/*
   2675 						* right now, X handles tablet motion the same
   2676 						* as mouse motion
   2677 						*/
   2678 						event->vse_type = VSE_MMOTION;   /* pos changed */
   2679 						event->vse_key = 0;
   2680 						event->vse_direction = 0;
   2681 						event->vse_time = TOY;	/* time stamp */
   2682 					}
   2683 GET_TBUTTON:
   2684 					/*
   2685 					* if button state has changed */
   2686 
   2687 					a = new_rep->state & 0x1E;   /* mask nonbutton bits */
   2688 					b = last_rep[dv->dv_unit].state & 0x1E;
   2689 
   2690 					if (a ^ b) {
   2691 
   2692 						/* event queue full now? (overflow condition) */
   2693 
   2694 						if (ISFULL(eqh) == TRUE) {
   2695 							printf("qd%d: qdiint: event queue overflow\n",qd);
   2696 							break;
   2697 						}
   2698 
   2699 						event = PUTBEGIN(eqh);  /* get new event */
   2700 						PUTEND(eqh);
   2701 
   2702 						++do_wakeup;   /* request a select wakeup call */
   2703 
   2704 						event->vse_x = eqh->curs_pos.x;
   2705 						event->vse_y = eqh->curs_pos.y;
   2706 
   2707 						event->vse_device = VSE_TABLET;  /* tablet */
   2708 						event->vse_type = VSE_BUTTON; /* button changed */
   2709 						event->vse_time = TOY;	   /* time stamp */
   2710 
   2711 						/* define the changed button and if up or down */
   2712 
   2713 						for ( c = 1;  c <= 0x10; c <<= 1) {
   2714 							if (c & (a ^ b)) {
   2715 								if (c == T_LEFT_BUTTON)
   2716 								    event->vse_key = VSE_T_LEFT_BUTTON;
   2717 								else if (c == T_FRONT_BUTTON)
   2718 								    event->vse_key = VSE_T_FRONT_BUTTON;
   2719 								else if (c == T_RIGHT_BUTTON)
   2720 								    event->vse_key = VSE_T_RIGHT_BUTTON;
   2721 								else if (c == T_BACK_BUTTON)
   2722 								    event->vse_key = VSE_T_BACK_BUTTON;
   2723 								break;
   2724 							}
   2725 						}
   2726 
   2727 						/* set bit = button depressed */
   2728 
   2729 						if (c & a)
   2730 						    event->vse_direction = VSE_KBTDOWN;
   2731 						else
   2732 							event->vse_direction = VSE_KBTUP;
   2733 					}
   2734 
   2735 					/* refresh last report */
   2736 
   2737 					last_rep[dv->dv_unit] = current_rep[dv->dv_unit];
   2738 
   2739 				} /* get last byte of report */
   2740 			} /* pick up tablet input */
   2741 
   2742 		} /* while input available.. */
   2743 
   2744 		/*
   2745 		* do select wakeup
   2746 		*/
   2747 		if (qdrsel[dv->dv_unit].si_pid && do_wakeup && qdflags[dv->dv_unit].selmask & SEL_READ) {
   2748 			selwakeup(&qdrsel[dv->dv_unit]);
   2749 			qdrsel[dv->dv_unit].si_pid = 0;
   2750 			qdflags[dv->dv_unit].selmask &= ~SEL_READ;
   2751 			do_wakeup = 0;
   2752 		}
   2753 	} else {
   2754 		/*
   2755 		 * if the graphic device is not turned on, this is console input
   2756 		 */
   2757 		if (qdpolling)
   2758 			return;
   2759 
   2760 		if (dv->dv_unit >= qd_cd.cd_ndevs || qd_cd.cd_devs[dv->dv_unit] == NULL)
   2761 			return;		/* no such device or address */
   2762 
   2763 		tp = qd_tty[dv->dv_unit << 2];
   2764 
   2765 		/*
   2766 		 * Get a character from the keyboard.
   2767 		 */
   2768 		while (duart->statusA&RCV_RDY) {
   2769 			key = duart->dataA;
   2770 			key &= 0xFF;
   2771 			/*
   2772 			* Check for various keyboard errors
   2773 			*/
   2774 			if (key == LK_POWER_ERROR || key == LK_KDOWN_ERROR ||
   2775 			    key == LK_INPUT_ERROR || key == LK_OUTPUT_ERROR) {
   2776 				printf("qd%d: qdiint: Keyboard error, code = %x\n",qd,key);
   2777 				return;
   2778 			}
   2779 
   2780 			if (key < LK_LOWEST)
   2781 			    return;
   2782 
   2783 			/*
   2784 			* See if its a state change key */
   2785 
   2786 			switch (key) {
   2787 
   2788 			case LOCK:
   2789 				q_keyboard.lock ^= 0xffff;	/* toggle */
   2790 				if (q_keyboard.lock)
   2791 					led_control(qd, LK_LED_ENABLE,
   2792 							  LK_LED_LOCK);
   2793 				else
   2794 					led_control(qd, LK_LED_DISABLE,
   2795 							  LK_LED_LOCK);
   2796 				return;
   2797 
   2798 			case SHIFT:
   2799 				q_keyboard.shift ^= 0xFFFF;
   2800 				return;
   2801 
   2802 			case CNTRL:
   2803 				q_keyboard.cntrl ^= 0xFFFF;
   2804 				return;
   2805 
   2806 			case ALLUP:
   2807 				q_keyboard.cntrl = 0;
   2808 				q_keyboard.shift = 0;
   2809 				return;
   2810 
   2811 			case REPEAT:
   2812 				chr = q_keyboard.last;
   2813 				break;
   2814 
   2815 				/*
   2816 				* Test for cntrl characters. If set, see if the character
   2817 				* is elligible to become a control character. */
   2818 
   2819 			default:
   2820 
   2821 				if (q_keyboard.cntrl) {
   2822 					chr = q_key[key];
   2823 					if (chr >= ' ' && chr <= '~')
   2824 					    chr &= 0x1F;
   2825 					else if (chr >= 0xA1 && chr <= 0xFE)
   2826 					    chr &= 0x9F;
   2827 				}
   2828 				else if( q_keyboard.lock || q_keyboard.shift )
   2829 				    chr = q_shift_key[key];
   2830 				else
   2831 					chr = q_key[key];
   2832 				break;
   2833 			}
   2834 
   2835 			q_keyboard.last = chr;
   2836 
   2837 			/*
   2838 			* Check for special function keys */
   2839 
   2840 			if (chr & 0x100) {
   2841 				char *string;
   2842 				string = q_special[chr & 0x7F];
   2843 				while(*string)
   2844 				    (*linesw[tp->t_line].l_rint)(*string++, tp);
   2845 			}
   2846 			else {
   2847 #ifdef DDB
   2848 				/* Check for kernel debugger escape here */
   2849 				int j;
   2850 
   2851 				j = kdbrint(chr&0177);
   2852 
   2853 				if (j == 1)  /* Escape received, just return */
   2854 				    return;
   2855 
   2856 				if (j == 2)  /* Second char wasn't 'D' */
   2857 				    (*linesw[tp->t_line].l_rint)(27, tp);
   2858 #endif
   2859 				(*linesw[tp->t_line].l_rint)(chr&0177, tp);
   2860 			}
   2861 		}
   2862 	}
   2863 } /* qdiint */
   2864 
   2865 /*
   2866  *
   2867  * Clear the QDSS screen
   2868  *
   2869  *			     >>> NOTE <<<
   2870  *
   2871  *   This code requires that certain adder initialization be valid.  To
   2872  *   assure that this requirement is satisfied, this routine should be
   2873  *   called only after calling the "setup_dragon()" function.
   2874  *
   2875  *   Clear the bitmap a piece at a time. Since the fast scroll clear
   2876  *   only clears the current displayed portion of the bitmap put a
   2877  *   temporary value in the y limit register so we can access whole
   2878  *   bitmap
   2879  *
   2880  */
   2881 void
   2882 clear_qd_screen(unit)
   2883 	int unit;
   2884 {
   2885 	volatile struct adder *adder;
   2886 	adder = (struct adder *) qdmap[unit].adder;
   2887 
   2888 	adder->x_limit = 1024;
   2889 	adder->y_limit = 2048 - CHAR_HEIGHT;
   2890 	adder->y_offset_pending = 0;
   2891 #define WSV  (void)wait_status(adder, VSYNC); (void)wait_status(adder, VSYNC)
   2892 	WSV;
   2893 	adder->y_scroll_constant = SCROLL_ERASE;
   2894 	WSV;
   2895 	adder->y_offset_pending = 864;
   2896 	WSV;
   2897 	adder->y_scroll_constant = SCROLL_ERASE;
   2898 	WSV;
   2899 	adder->y_offset_pending = 1728;
   2900 	WSV;
   2901 	adder->y_scroll_constant = SCROLL_ERASE;
   2902 	WSV;
   2903 	adder->y_offset_pending = 0;	 /* back to normal */
   2904 	WSV;
   2905 	adder->x_limit = MAX_SCREEN_X;
   2906 	adder->y_limit = MAX_SCREEN_Y + FONT_HEIGHT;
   2907 #undef WSV
   2908 
   2909 } /* clear_qd_screen */
   2910 
   2911 /*
   2912  *  kernel console output to the glass tty
   2913  */
   2914 void
   2915 qdcnputc(dev, chr)
   2916 	dev_t dev;
   2917 	int chr;
   2918 {
   2919 
   2920 	/*
   2921 	 * if system is now physical, forget it (ie: crash DUMP)
   2922 	 */
   2923 	if ((mfpr(PR_MAPEN) & 1) == 0)
   2924 		return;
   2925 
   2926 	blitc(0, (u_char)(chr & 0xff));
   2927 	if ((chr & 0177) == '\n')
   2928 		blitc(0, '\r');
   2929 
   2930 } /* qdputc */
   2931 
   2932 /*
   2933  *  load the mouse cursor's template RAM bitmap
   2934  */
   2935 void
   2936 ldcursor(unit, bitmap)
   2937 	int unit;
   2938 	short *bitmap;
   2939 {
   2940 	volatile struct dga *dga;
   2941 	volatile short *temp;
   2942 	int i;
   2943 	int curs;
   2944 
   2945 	dga = (struct dga *) qdmap[unit].dga;
   2946 	temp = (short *) qdmap[unit].template;
   2947 
   2948 	if (dga->csr & CURS_ENB) {	/* if the cursor is enabled.. */
   2949 		curs = -1;		/* ..note that.. */
   2950 		dga->csr &= ~CURS_ENB;	/* ..and shut it off */
   2951 	} else
   2952 		curs = 0;
   2953 
   2954 	dga->csr &= ~CURS_ENB;		/* shut off the cursor */
   2955 
   2956 	temp += (8 * 1024) - 32;	/* cursor is 32 WORDS from the end */
   2957 	/* ..of the 8k WORD template space */
   2958 	for (i = 0; i < 32; ++i)
   2959 		*temp++ = *bitmap++;
   2960 
   2961 	if (curs) {			/* if cursor was enabled.. */
   2962 		dga->csr |= CURS_ENB;	/* ..turn it back on */
   2963 	}
   2964 
   2965 } /* ldcursor */
   2966 
   2967 /*
   2968  *  Put the console font in the QDSS off-screen memory
   2969  */
   2970 void
   2971 ldfont(unit)
   2972 	int unit;
   2973 {
   2974 	volatile struct adder *adder;
   2975 
   2976 	int i, j, k, max_chars_line;
   2977 	short packed;
   2978 
   2979 	adder = (struct adder *) qdmap[unit].adder;
   2980 
   2981 	/*
   2982 	* setup VIPER operand control registers
   2983 	*/
   2984 	write_ID(adder, MASK_1, 0xFFFF);
   2985 	write_ID(adder, VIPER_Z_LOAD | FOREGROUND_COLOR_Z, 255);
   2986 	write_ID(adder, VIPER_Z_LOAD | BACKGROUND_COLOR_Z, 0);
   2987 
   2988 	write_ID(adder, SRC1_OCR_B,
   2989 	EXT_NONE | INT_NONE | ID | BAR_SHIFT_DELAY);
   2990 	write_ID(adder, SRC2_OCR_B,
   2991 	EXT_NONE | INT_NONE | ID | BAR_SHIFT_DELAY);
   2992 	write_ID(adder, DST_OCR_B,
   2993 	EXT_SOURCE | INT_NONE | NO_ID | NO_BAR_SHIFT_DELAY);
   2994 
   2995 	adder->rasterop_mode = DST_WRITE_ENABLE | DST_INDEX_ENABLE | NORMAL;
   2996 
   2997 	/*
   2998 	* load destination data
   2999 	*/
   3000 	(void)wait_status(adder, RASTEROP_COMPLETE);
   3001 
   3002 	adder->destination_x = FONT_X;
   3003 	adder->destination_y = FONT_Y;
   3004 #if FONT_WIDTH > MAX_SCREEN_X
   3005 	adder->fast_dest_dx = MAX_SCREEN_X;
   3006 #else
   3007 	adder->fast_dest_dx = FONT_WIDTH;
   3008 #endif
   3009 	adder->slow_dest_dy = CHAR_HEIGHT;
   3010 
   3011 	/*
   3012 	* setup for processor to bitmap xfer  */
   3013 
   3014 	write_ID(adder, CS_UPDATE_MASK, 0x0001);
   3015 	adder->cmd = PBT | OCRB | 2 | DTE | 2;
   3016 
   3017 	/*
   3018 	* Figure out how many characters can be stored on one "line" of
   3019 	* offscreen memory.
   3020 	*/
   3021 	max_chars_line = MAX_SCREEN_X/(CHAR_WIDTH*2);
   3022 	if ((CHARS/2 + CHARS%2) < max_chars_line)
   3023 	    max_chars_line = CHARS/2 + CHARS%2;
   3024 
   3025 	/*
   3026 	* iteratively do the processor to bitmap xfer */
   3027 
   3028 	for (i = 0; i < ROWS; ++i) {
   3029 
   3030 		/* PTOB a scan line */
   3031 
   3032 		for (j = 0, k = i; j < max_chars_line; ++j) {
   3033 			/* PTOB one scan of a char cell */
   3034 
   3035 			packed = q_font[k];
   3036 			k += ROWS;
   3037 			packed |= ((short)q_font[k] << 8);
   3038 			k += ROWS;
   3039 
   3040 			(void)wait_status(adder, TX_READY);
   3041 			adder->id_data = packed;
   3042 		}
   3043 	}
   3044 
   3045 	/*
   3046 	 * (XXX XXX XXX - should remove)
   3047 	 *
   3048 	 * Copy the second row of characters.  Subtract the first
   3049 	 * row from the total number.  Divide this quantity by 2
   3050 	 * because 2 chars are stored in a short in the PTOB loop
   3051 	 * below.  Figure out how many characters can be stored on
   3052 	 * one "line" of offscreen memory
   3053 	 */
   3054 
   3055 	max_chars_line = MAX_SCREEN_X/(CHAR_WIDTH*2);
   3056 	if ((CHARS/2 + CHARS%2) < max_chars_line)
   3057 	    return;
   3058 	max_chars_line = (CHARS/2 + CHARS%2) - max_chars_line; /* 95 - 64 */
   3059 	/* Paranoia check to see if 3rd row may be needed */
   3060 	if (max_chars_line > (MAX_SCREEN_X/(CHAR_WIDTH*2)))
   3061 	    max_chars_line = MAX_SCREEN_X/(CHAR_WIDTH*2);
   3062 
   3063 	adder->destination_x = FONT_X;
   3064 	adder->destination_y = FONT_Y - CHAR_HEIGHT;
   3065 	adder->fast_dest_dx = max_chars_line * CHAR_WIDTH * 2;
   3066 	adder->slow_dest_dy = CHAR_HEIGHT;
   3067 
   3068 	/*
   3069 	* setup for processor to bitmap xfer
   3070 	*/
   3071 	write_ID(adder, CS_UPDATE_MASK, 0x0001);
   3072 	adder->cmd = PBT | OCRB | 2 | DTE | 2;
   3073 
   3074 	/*
   3075 	* iteratively do the processor to bitmap xfer
   3076 	*/
   3077 	for (i = 0; i < ROWS; ++i) {
   3078 		/*
   3079 		 * PTOB a scan line
   3080 		 */
   3081 		for (j = 0, k = i; j < max_chars_line; ++j) {
   3082 			/*
   3083 			 * PTOB one scan of a char cell
   3084 			 */
   3085 			packed = q_font[k + FONT_OFFSET];
   3086 			k += ROWS;
   3087 			packed |= ((short)q_font[k + FONT_OFFSET] << 8);
   3088 			k += ROWS;
   3089 			(void)wait_status(adder, TX_READY);
   3090 			adder->id_data = packed;
   3091 		}
   3092 	}
   3093 
   3094 }  /* ldfont */
   3095 
   3096 
   3097 /*
   3098  * Disable or enable polling.  This is used when entering or leaving the
   3099  * kernel debugger.
   3100  */
   3101 void
   3102 qdcnpollc(dev, onoff)
   3103 	dev_t dev;
   3104 	int onoff;
   3105 {
   3106      qdpolling = onoff;
   3107 }
   3108 
   3109 
   3110 /*
   3111  *  Get a character from the LK201 (polled)
   3112  */
   3113 int
   3114 qdcngetc(dev)
   3115 	dev_t dev;
   3116 {
   3117 	short key;
   3118 	char chr;
   3119 	volatile struct duart *duart;
   3120 
   3121 	duart = (struct duart *) qdmap[0].duart;
   3122 
   3123 	/*
   3124 	* Get a character from the keyboard.
   3125 	*/
   3126 LOOP:
   3127 	while (!(duart->statusA&RCV_RDY))
   3128 		;
   3129 
   3130 	key = duart->dataA;
   3131 	key &= 0xFF;
   3132 
   3133 	/*
   3134 	* Check for various keyboard errors  */
   3135 
   3136 	if (key == LK_POWER_ERROR || key == LK_KDOWN_ERROR ||
   3137 	    key == LK_INPUT_ERROR || key == LK_OUTPUT_ERROR) {
   3138 		printf("Keyboard error, code = %x\n", key);
   3139 		return(0);
   3140 	}
   3141 
   3142 	if (key < LK_LOWEST)
   3143 		return(0);
   3144 
   3145 	/*
   3146 	 * See if its a state change key
   3147 	 */
   3148 	switch (key) {
   3149 
   3150 	case LOCK:
   3151 		q_keyboard.lock ^= 0xffff;	/* toggle */
   3152 		if (q_keyboard.lock)
   3153 			led_control(0, LK_LED_ENABLE, LK_LED_LOCK);
   3154 		else
   3155 			led_control(0, LK_LED_DISABLE, LK_LED_LOCK);
   3156 		goto LOOP;
   3157 
   3158 	case SHIFT:
   3159 		q_keyboard.shift ^= 0xFFFF;
   3160 		goto LOOP;
   3161 
   3162 	case CNTRL:
   3163 		q_keyboard.cntrl ^= 0xFFFF;
   3164 		goto LOOP;
   3165 
   3166 	case ALLUP:
   3167 		q_keyboard.cntrl = 0;
   3168 		q_keyboard.shift = 0;
   3169 		goto LOOP;
   3170 
   3171 	case REPEAT:
   3172 		chr = q_keyboard.last;
   3173 		break;
   3174 
   3175 		/*
   3176 		* Test for cntrl characters. If set, see if the character
   3177 		* is elligible to become a control character.
   3178 		*/
   3179 	default:
   3180 
   3181 		if (q_keyboard.cntrl) {
   3182 			chr = q_key[key];
   3183 			if (chr >= ' ' && chr <= '~')
   3184 			    chr &= 0x1F;
   3185 		}
   3186 		else if ( q_keyboard.lock || q_keyboard.shift )
   3187 		    chr = q_shift_key[key];
   3188 		else
   3189 			chr = q_key[key];
   3190 		break;
   3191 	}
   3192 
   3193 	if (chr < ' ' && chr > '~')	/* if input is non-displayable */
   3194 		return(0);		/* ..then pitch it! */
   3195 
   3196 	q_keyboard.last = chr;
   3197 
   3198 	/*
   3199 	* Check for special function keys */
   3200 
   3201 	if (chr & 0x80) 		/* pitch the function keys */
   3202 		return(0);
   3203 	else
   3204 		return(chr);
   3205 
   3206 } /* qdgetc */
   3207 
   3208 /*
   3209  *  led_control()... twiddle LK-201 LED's
   3210  */
   3211 void
   3212 led_control(unit, cmd, led_mask)
   3213 	int unit, cmd, led_mask;
   3214 {
   3215 	int i;
   3216 	volatile struct duart *duart;
   3217 
   3218 	duart = (struct duart *)qdmap[unit].duart;
   3219 
   3220 	for (i = 1000; i > 0; --i) {
   3221 		if (duart->statusA&XMT_RDY) {
   3222 			duart->dataA = cmd;
   3223 			break;
   3224 		}
   3225 	}
   3226 	for (i = 1000; i > 0; --i) {
   3227 		if (duart->statusA&XMT_RDY) {
   3228 			duart->dataA = led_mask;
   3229 			break;
   3230 		}
   3231 	}
   3232 	return;
   3233 
   3234 } /* led_control */
   3235 
   3236 /*
   3237  *  scroll_up()... move the screen up one character height
   3238  */
   3239 void
   3240 scroll_up(adder)
   3241 	volatile struct adder *adder;
   3242 {
   3243 	/*
   3244 	* setup VIPER operand control registers
   3245 	*/
   3246 	(void)wait_status(adder, ADDRESS_COMPLETE);
   3247 	write_ID(adder, CS_UPDATE_MASK, 0x00FF);  /* select all planes */
   3248 	write_ID(adder, MASK_1, 0xFFFF);
   3249 	write_ID(adder, VIPER_Z_LOAD | FOREGROUND_COLOR_Z, 255);
   3250 	write_ID(adder, VIPER_Z_LOAD | BACKGROUND_COLOR_Z, 0);
   3251 	write_ID(adder, SRC1_OCR_B,
   3252 	EXT_NONE | INT_SOURCE | ID | BAR_SHIFT_DELAY);
   3253 	write_ID(adder, DST_OCR_B,
   3254 	EXT_NONE | INT_NONE | NO_ID | NO_BAR_SHIFT_DELAY);
   3255 	/*
   3256 	 * load DESTINATION origin and vectors
   3257 	 */
   3258 	adder->fast_dest_dy = 0;
   3259 	adder->slow_dest_dx = 0;
   3260 	adder->error_1 = 0;
   3261 	adder->error_2 = 0;
   3262 	adder->rasterop_mode = DST_WRITE_ENABLE | NORMAL;
   3263 	adder->destination_x = 0;
   3264 	adder->fast_dest_dx = 1024;
   3265 	adder->destination_y = 0;
   3266 	adder->slow_dest_dy = 864 - CHAR_HEIGHT;
   3267 	/*
   3268 	 * load SOURCE origin and vectors
   3269 	 */
   3270 	adder->source_1_x = 0;
   3271 	adder->source_1_dx = 1024;
   3272 	adder->source_1_y = 0 + CHAR_HEIGHT;
   3273 	adder->source_1_dy = 864 - CHAR_HEIGHT;
   3274 	write_ID(adder, LU_FUNCTION_R1, FULL_SRC_RESOLUTION | LF_SOURCE);
   3275 	adder->cmd = RASTEROP | OCRB | 0 | S1E | DTE;
   3276 	/*
   3277 	 * do a rectangle clear of last screen line
   3278 	 */
   3279 	write_ID(adder, MASK_1, 0xffff);
   3280 	write_ID(adder, SOURCE, 0xffff);
   3281 	write_ID(adder,DST_OCR_B,
   3282 	(EXT_NONE | INT_NONE | NO_ID | NO_BAR_SHIFT_DELAY));
   3283 	write_ID(adder, VIPER_Z_LOAD | FOREGROUND_COLOR_Z, 0);
   3284 	adder->error_1 = 0;
   3285 	adder->error_2 = 0;
   3286 	adder->slow_dest_dx = 0;		/* set up the width of	*/
   3287 	adder->slow_dest_dy = CHAR_HEIGHT;	/* rectangle */
   3288 	adder->rasterop_mode = (NORMAL | DST_WRITE_ENABLE) ;
   3289 	(void)wait_status(adder, RASTEROP_COMPLETE);
   3290 	adder->destination_x = 0;
   3291 	adder->destination_y = 864 - CHAR_HEIGHT;
   3292 	adder->fast_dest_dx = 1024;	/* set up the height	*/
   3293 	adder->fast_dest_dy = 0;	/* of rectangle 	*/
   3294 	write_ID(adder, LU_FUNCTION_R2, (FULL_SRC_RESOLUTION | LF_SOURCE));
   3295 	adder->cmd = (RASTEROP | OCRB | LF_R2 | DTE ) ;
   3296 
   3297 } /* scroll_up */
   3298 
   3299 /*
   3300  *  init shared memory pointers and structures
   3301  */
   3302 void
   3303 init_shared(unit)
   3304 	int unit;
   3305 {
   3306 	volatile struct dga *dga;
   3307 
   3308 	dga = (struct dga *) qdmap[unit].dga;
   3309 
   3310 	/*
   3311 	* initialize the event queue pointers and header */
   3312 
   3313 	eq_header[unit] = (struct qdinput *)
   3314 	    ((((int)event_shared & ~(0x01FF)) + 512)
   3315 		+ (EVENT_BUFSIZE * unit));
   3316 	eq_header[unit]->curs_pos.x = 0;
   3317 	eq_header[unit]->curs_pos.y = 0;
   3318 	dga->x_cursor = TRANX(eq_header[unit]->curs_pos.x);
   3319 	dga->y_cursor = TRANY(eq_header[unit]->curs_pos.y);
   3320 	eq_header[unit]->curs_box.left = 0;
   3321 	eq_header[unit]->curs_box.right = 0;
   3322 	eq_header[unit]->curs_box.top = 0;
   3323 	eq_header[unit]->curs_box.bottom = 0;
   3324 	/*
   3325 	 * assign a pointer to the DMA I/O buffer for this QDSS.
   3326 	 */
   3327 	DMAheader[unit] = (struct DMAreq_header *)
   3328 	    (((int)(&DMA_shared[0] + 512) & ~0x1FF)
   3329 		+ (DMAbuf_size * unit));
   3330 	DMAheader[unit]->DMAreq = (struct DMAreq *) ((int)DMAheader[unit]
   3331 	    + sizeof(struct DMAreq_header));
   3332 	DMAheader[unit]->QBAreg = 0;
   3333 	DMAheader[unit]->status = 0;
   3334 	DMAheader[unit]->shared_size = DMAbuf_size;
   3335 	DMAheader[unit]->used = 0;
   3336 	DMAheader[unit]->size = 10;	/* default = 10 requests */
   3337 	DMAheader[unit]->oldest = 0;
   3338 	DMAheader[unit]->newest = 0;
   3339 	/*
   3340 	* assign a pointer to the scroll structure for this QDSS.
   3341 	*/
   3342 	scroll[unit] = (struct scroll *)
   3343 	    (((int)(&scroll_shared[0] + 512) & ~0x1FF)
   3344 		+ (sizeof(struct scroll) * unit));
   3345 	scroll[unit]->status = 0;
   3346 	scroll[unit]->viper_constant = 0;
   3347 	scroll[unit]->y_scroll_constant = 0;
   3348 	scroll[unit]->y_offset = 0;
   3349 	scroll[unit]->x_index_pending = 0;
   3350 	scroll[unit]->y_index_pending = 0;
   3351 	/*
   3352 	* assign a pointer to the color map write buffer for this QDSS
   3353 	*/
   3354 	color_buf[unit] = (struct color_buf *)
   3355 	    (((int)(&color_shared[0] + 512) & ~0x1FF)
   3356 		+ (COLOR_BUFSIZ * unit));
   3357 	color_buf[unit]->status = 0;
   3358 	color_buf[unit]->count = 0;
   3359 
   3360 } /* init_shared */
   3361 
   3362 /*
   3363  * init the ADDER, VIPER, bitmaps, & color map
   3364  */
   3365 void
   3366 setup_dragon(unit)
   3367 	int unit;
   3368 {
   3369 
   3370 	volatile struct adder *adder;
   3371 	volatile struct dga *dga;
   3372 	volatile short *memcsr;
   3373 	int i;
   3374 	short top;		/* clipping/scrolling boundaries */
   3375 	short bottom;
   3376 	short right;
   3377 	short left;
   3378 	volatile short *red;		/* color map pointers */
   3379 	volatile short *green;
   3380 	volatile short *blue;
   3381 
   3382 	/*
   3383 	* init for setup
   3384 	*/
   3385 	adder = (struct adder *) qdmap[unit].adder;
   3386 	dga = (struct dga *) qdmap[unit].dga;
   3387 	memcsr = (short *) qdmap[unit].memcsr;
   3388 	dga->csr &= ~(DMA_IE | 0x700);	/* halt DMA and kill the intrpts */
   3389 	*memcsr = SYNC_ON;		/* blank screen and turn off LED's */
   3390 	adder->command = CANCEL;
   3391 	/*
   3392 	* set monitor timing
   3393 	*/
   3394 	adder->x_scan_count_0 = 0x2800;
   3395 	adder->x_scan_count_1 = 0x1020;
   3396 	adder->x_scan_count_2 = 0x003A;
   3397 	adder->x_scan_count_3 = 0x38F0;
   3398 	adder->x_scan_count_4 = 0x6128;
   3399 	adder->x_scan_count_5 = 0x093A;
   3400 	adder->x_scan_count_6 = 0x313C;
   3401 	adder->sync_phase_adj = 0x0100;
   3402 	adder->x_scan_conf = 0x00C8;
   3403 	/*
   3404 	 * got a bug in secound pass ADDER! lets take care of it
   3405 	 *
   3406 	 * normally, just use the code in the following bug fix code, but to
   3407 	 * make repeated demos look pretty, load the registers as if there was
   3408 	 * no bug and then test to see if we are getting sync
   3409 	 */
   3410 	adder->y_scan_count_0 = 0x135F;
   3411 	adder->y_scan_count_1 = 0x3363;
   3412 	adder->y_scan_count_2 = 0x2366;
   3413 	adder->y_scan_count_3 = 0x0388;
   3414 	/*
   3415 	 * if no sync, do the bug fix code
   3416 	 */
   3417 	if (wait_status(adder, VSYNC) == BAD) {
   3418 		/* first load all Y scan registers with very short frame and
   3419 		 * wait for scroll service.  This guarantees at least one SYNC
   3420 		 * to fix the pass 2 Adder initialization bug (synchronizes
   3421 		 * XCINCH with DMSEEDH)
   3422 		 */
   3423 		adder->y_scan_count_0 = 0x01;
   3424 		adder->y_scan_count_1 = 0x01;
   3425 		adder->y_scan_count_2 = 0x01;
   3426 		adder->y_scan_count_3 = 0x01;
   3427 		/*
   3428 		 * delay at least 1 full frame time
   3429 		 */
   3430 		(void)wait_status(adder, VSYNC);
   3431 		(void)wait_status(adder, VSYNC);
   3432 		/*
   3433 		 * now load the REAL sync values (in reverse order just to
   3434 		 * be safe.
   3435 		 */
   3436 		adder->y_scan_count_3 = 0x0388;
   3437 		adder->y_scan_count_2 = 0x2366;
   3438 		adder->y_scan_count_1 = 0x3363;
   3439 		adder->y_scan_count_0 = 0x135F;
   3440 	}
   3441 	*memcsr = SYNC_ON | UNBLANK;	/* turn off leds and turn on video */
   3442 	/*
   3443 	 * zero the index registers
   3444 	 */
   3445 	adder->x_index_pending = 0;
   3446 	adder->y_index_pending = 0;
   3447 	adder->x_index_new = 0;
   3448 	adder->y_index_new = 0;
   3449 	adder->x_index_old = 0;
   3450 	adder->y_index_old = 0;
   3451 	adder->pause = 0;
   3452 	/*
   3453 	 * set rasterop mode to normal pen down
   3454 	 */
   3455 	adder->rasterop_mode = DST_WRITE_ENABLE | DST_INDEX_ENABLE | NORMAL;
   3456 	/*
   3457 	 * set the rasterop registers to a default values
   3458 	 */
   3459 	adder->source_1_dx = 1;
   3460 	adder->source_1_dy = 1;
   3461 	adder->source_1_x = 0;
   3462 	adder->source_1_y = 0;
   3463 	adder->destination_x = 0;
   3464 	adder->destination_y = 0;
   3465 	adder->fast_dest_dx = 1;
   3466 	adder->fast_dest_dy = 0;
   3467 	adder->slow_dest_dx = 0;
   3468 	adder->slow_dest_dy = 1;
   3469 	adder->error_1 = 0;
   3470 	adder->error_2 = 0;
   3471 	/*
   3472 	 * scale factor = UNITY
   3473 	 */
   3474 	adder->fast_scale = UNITY;
   3475 	adder->slow_scale = UNITY;
   3476 	/*
   3477 	 * set the source 2 parameters
   3478 	 */
   3479 	adder->source_2_x = 0;
   3480 	adder->source_2_y = 0;
   3481 	adder->source_2_size = 0x0022;
   3482 	/*
   3483 	* initialize plane addresses for eight vipers
   3484 	*/
   3485 	write_ID(adder, CS_UPDATE_MASK, 0x0001);
   3486 	write_ID(adder, PLANE_ADDRESS, 0x0000);
   3487 	write_ID(adder, CS_UPDATE_MASK, 0x0002);
   3488 	write_ID(adder, PLANE_ADDRESS, 0x0001);
   3489 	write_ID(adder, CS_UPDATE_MASK, 0x0004);
   3490 	write_ID(adder, PLANE_ADDRESS, 0x0002);
   3491 	write_ID(adder, CS_UPDATE_MASK, 0x0008);
   3492 	write_ID(adder, PLANE_ADDRESS, 0x0003);
   3493 	write_ID(adder, CS_UPDATE_MASK, 0x0010);
   3494 	write_ID(adder, PLANE_ADDRESS, 0x0004);
   3495 	write_ID(adder, CS_UPDATE_MASK, 0x0020);
   3496 	write_ID(adder, PLANE_ADDRESS, 0x0005);
   3497 	write_ID(adder, CS_UPDATE_MASK, 0x0040);
   3498 	write_ID(adder, PLANE_ADDRESS, 0x0006);
   3499 	write_ID(adder, CS_UPDATE_MASK, 0x0080);
   3500 	write_ID(adder, PLANE_ADDRESS, 0x0007);
   3501 	/*
   3502 	 * initialize the external registers.
   3503 	 */
   3504 	write_ID(adder, CS_UPDATE_MASK, 0x00FF);
   3505 	write_ID(adder, CS_SCROLL_MASK, 0x00FF);
   3506 	/*
   3507 	 * initialize resolution mode
   3508 	 */
   3509 	write_ID(adder, MEMORY_BUS_WIDTH, 0x000C);     /* bus width = 16 */
   3510 	write_ID(adder, RESOLUTION_MODE, 0x0000);      /* one bit/pixel */
   3511 	/*
   3512 	 * initialize viper registers
   3513 	 */
   3514 	write_ID(adder, SCROLL_CONSTANT, SCROLL_ENABLE|VIPER_LEFT|VIPER_UP);
   3515 	write_ID(adder, SCROLL_FILL, 0x0000);
   3516 	/*
   3517 	 * set clipping and scrolling limits to full screen
   3518 	 */
   3519 	for (i = 1000, adder->status = 0;
   3520 	     i > 0 && !(adder->status&ADDRESS_COMPLETE); --i)
   3521 		;
   3522 	if (i == 0)
   3523 	    printf("qd%d: setup_dragon: timeout on ADDRESS_COMPLETE\n",unit);
   3524 	top = 0;
   3525 	bottom = 2048;
   3526 	left = 0;
   3527 	right = 1024;
   3528 	adder->x_clip_min = left;
   3529 	adder->x_clip_max = right;
   3530 	adder->y_clip_min = top;
   3531 	adder->y_clip_max = bottom;
   3532 	adder->scroll_x_min = left;
   3533 	adder->scroll_x_max = right;
   3534 	adder->scroll_y_min = top;
   3535 	adder->scroll_y_max = bottom;
   3536 	(void)wait_status(adder, VSYNC);	/* wait at LEAST 1 full frame */
   3537 	(void)wait_status(adder, VSYNC);
   3538 	adder->x_index_pending = left;
   3539 	adder->y_index_pending = top;
   3540 	adder->x_index_new = left;
   3541 	adder->y_index_new = top;
   3542 	adder->x_index_old = left;
   3543 	adder->y_index_old = top;
   3544 
   3545 	for (i = 1000, adder->status = 0; i > 0 &&
   3546 	     !(adder->status&ADDRESS_COMPLETE) ; --i)
   3547 		;
   3548 	if (i == 0)
   3549 	       printf("qd%d: setup_dragon: timeout on ADDRESS_COMPLETE\n",unit);
   3550 
   3551 	write_ID(adder, LEFT_SCROLL_MASK, 0x0000);
   3552 	write_ID(adder, RIGHT_SCROLL_MASK, 0x0000);
   3553 	/*
   3554 	* set source and the mask register to all ones (ie: white) o
   3555 	*/
   3556 	write_ID(adder, SOURCE, 0xFFFF);
   3557 	write_ID(adder, MASK_1, 0xFFFF);
   3558 	write_ID(adder, VIPER_Z_LOAD | FOREGROUND_COLOR_Z, 255);
   3559 	write_ID(adder, VIPER_Z_LOAD | BACKGROUND_COLOR_Z, 0);
   3560 	/*
   3561 	* initialize Operand Control Register banks for fill command
   3562 	*/
   3563 	write_ID(adder, SRC1_OCR_A, EXT_NONE | INT_M1_M2  | NO_ID | WAIT);
   3564 	write_ID(adder, SRC2_OCR_A, EXT_NONE | INT_SOURCE | NO_ID | NO_WAIT);
   3565 	write_ID(adder, DST_OCR_A, EXT_NONE | INT_NONE	 | NO_ID | NO_WAIT);
   3566 	write_ID(adder, SRC1_OCR_B, EXT_NONE | INT_SOURCE | NO_ID | WAIT);
   3567 	write_ID(adder, SRC2_OCR_B, EXT_NONE | INT_M1_M2  | NO_ID | NO_WAIT);
   3568 	write_ID(adder, DST_OCR_B, EXT_NONE | INT_NONE | NO_ID | NO_WAIT);
   3569 	/*
   3570 	* init Logic Unit Function registers, (these are just common values,
   3571 	* and may be changed as required).
   3572 	*/
   3573 	write_ID(adder, LU_FUNCTION_R1, FULL_SRC_RESOLUTION | LF_SOURCE);
   3574 	write_ID(adder, LU_FUNCTION_R2, FULL_SRC_RESOLUTION | LF_SOURCE |
   3575 		 INV_M1_M2);
   3576 	write_ID(adder, LU_FUNCTION_R3, FULL_SRC_RESOLUTION | LF_D_OR_S);
   3577 	write_ID(adder, LU_FUNCTION_R4, FULL_SRC_RESOLUTION | LF_D_XOR_S);
   3578 	/*
   3579 	* load the color map for black & white
   3580 	*/
   3581 	for (i = 0, adder->status = 0; i < 10000 && !(adder->status&VSYNC); ++i)
   3582 		;
   3583 
   3584 	if (i == 0)
   3585 		printf("qd%d: setup_dragon: timeout on VSYNC\n", unit);
   3586 
   3587 	red = (short *) qdmap[unit].red;
   3588 	green = (short *) qdmap[unit].green;
   3589 	blue = (short *) qdmap[unit].blue;
   3590 
   3591 	*red++ = 0x00;			/* black */
   3592 	*green++ = 0x00;
   3593 	*blue++ = 0x00;
   3594 
   3595 	*red-- = 0xFF;			/* white */
   3596 	*green-- = 0xFF;
   3597 	*blue-- = 0xFF;
   3598 
   3599 	/*
   3600 	* set color map for mouse cursor
   3601 	*/
   3602 
   3603 	red += 254;
   3604 	green += 254;
   3605 	blue += 254;
   3606 
   3607 	*red++ = 0x00;			/* black */
   3608 	*green++ = 0x00;
   3609 	*blue++ = 0x00;
   3610 
   3611 	*red = 0xFF;			/* white */
   3612 	*green = 0xFF;
   3613 	*blue = 0xFF;
   3614 
   3615 } /* setup_dragon */
   3616 
   3617 /*
   3618  * Init the DUART and set defaults in input
   3619  */
   3620 void
   3621 setup_input(unit)
   3622 	int unit;
   3623 {
   3624 	volatile struct duart *duart;	/* DUART register structure pointer */
   3625 	int i, bits;
   3626 	char id_byte;
   3627 
   3628 	duart = (struct duart *) qdmap[unit].duart;
   3629 	duart->imask = 0;
   3630 
   3631 	/*
   3632 	* setup the DUART for kbd & pointing device
   3633 	*/
   3634 	duart->cmdA = RESET_M;	/* reset mode reg ptr for kbd */
   3635 	duart->modeA = 0x13;	/* 8 bits, no parity, rcv IE, */
   3636 				/* no RTS control,char error mode */
   3637 	duart->modeA = 0x07;	/* 1 stop bit,CTS does not IE XMT */
   3638 				/* no RTS control,no echo or loop */
   3639 	duart->cmdB = RESET_M;	/* reset mode reg pntr for host */
   3640 	duart->modeB = 0x07;	/* 8 bits, odd parity, rcv IE.. */
   3641 				/* ..no RTS cntrl, char error mode */
   3642 	duart->modeB = 0x07;	/* 1 stop bit,CTS does not IE XMT */
   3643 				/* no RTS control,no echo or loop */
   3644 	duart->auxctl = 0x00;	/* baud rate set 1 */
   3645 	duart->clkselA = 0x99;	/* 4800 baud for kbd */
   3646 	duart->clkselB = 0x99;	/* 4800 baud for mouse */
   3647 
   3648 	/* reset everything for keyboard */
   3649 
   3650 	for (bits = RESET_M; bits < START_BREAK; bits += 0x10)
   3651 		duart->cmdA = bits;
   3652 
   3653 	/* reset everything for host */
   3654 
   3655 	for (bits = RESET_M; bits < START_BREAK; bits += 0x10)
   3656 		duart->cmdB = bits;
   3657 
   3658 	duart->cmdA = EN_RCV | EN_XMT; /* enbl xmt & rcv for kbd */
   3659 	duart->cmdB = EN_RCV | EN_XMT; /* enbl xmt & rcv for pointer device */
   3660 
   3661 	/*
   3662 	* init keyboard defaults (DUART channel A)
   3663 	*/
   3664 	for (i = 500; i > 0; --i) {
   3665 		if (duart->statusA&XMT_RDY) {
   3666 			duart->dataA = LK_DEFAULTS;
   3667 			break;
   3668 		}
   3669 	}
   3670 
   3671 	for (i = 100000; i > 0; --i) {
   3672 		if (duart->statusA&RCV_RDY) {
   3673 			break;
   3674 		}
   3675 	}
   3676 
   3677 	if (duart->dataA)	/* flush the ACK */
   3678 		;
   3679 
   3680 	/*
   3681 	* identify the pointing device
   3682 	*/
   3683 	for (i = 500; i > 0; --i) {
   3684 		if (duart->statusB&XMT_RDY) {
   3685 			duart->dataB = SELF_TEST;
   3686 			break;
   3687 		}
   3688 	}
   3689 
   3690 	/*
   3691 	* wait for 1st byte of self test report */
   3692 
   3693 	for (i = 100000; i > 0; --i) {
   3694 		if (duart->statusB&RCV_RDY) {
   3695 			break;
   3696 		}
   3697 	}
   3698 
   3699 	if (i == 0) {
   3700 		printf("qd[%d]: setup_input: timeout on 1st byte of self test\n"
   3701 		       ,unit);
   3702 		goto OUT;
   3703 	}
   3704 
   3705 	if (duart->dataB)
   3706 		;
   3707 
   3708 	/*
   3709 	* wait for ID byte of self test report
   3710 	*/
   3711 	for (i = 100000; i > 0; --i) {
   3712 		if (duart->statusB&RCV_RDY) {
   3713 			break;
   3714 		}
   3715 	}
   3716 
   3717 	if (i == 0) {
   3718 		printf("qd[%d]: setup_input: timeout on 2nd byte of self test\n", unit);
   3719 		goto OUT;
   3720 	}
   3721 
   3722 	id_byte = duart->dataB;
   3723 
   3724 	/*
   3725 	* wait for other bytes to come in
   3726 	*/
   3727 	for (i = 100000; i > 0; --i) {
   3728 		if (duart->statusB & RCV_RDY) {
   3729 			if (duart->dataB)
   3730 				;
   3731 			break;
   3732 		}
   3733 	}
   3734 	if (i == 0) {
   3735 		printf("qd[%d]: setup_input: timeout on 3rd byte of self test\n", unit);
   3736 		goto OUT;
   3737 	}
   3738 	for (i = 100000; i > 0; --i) {
   3739 		if (duart->statusB&RCV_RDY) {
   3740 			if (duart->dataB)
   3741 				;
   3742 			break;
   3743 		}
   3744 	}
   3745 	if (i == 0) {
   3746 		printf("qd[%d]: setup_input: timeout on 4th byte of self test\n", unit);
   3747 		goto OUT;
   3748 	}
   3749 	/*
   3750 	* flag pointing device type and set defaults
   3751 	*/
   3752 	for (i=100000; i>0; --i)
   3753 		;		/*XXX*/
   3754 
   3755 	if ((id_byte & 0x0F) != TABLET_ID) {
   3756 		qdflags[unit].pntr_id = MOUSE_ID;
   3757 
   3758 		for (i = 500; i > 0; --i) {
   3759 			if (duart->statusB&XMT_RDY) {
   3760 				duart->dataB = INC_STREAM_MODE;
   3761 				break;
   3762 			}
   3763 		}
   3764 	}
   3765 	else {
   3766 		qdflags[unit].pntr_id = TABLET_ID;
   3767 
   3768 		for (i = 500; i > 0; --i) {
   3769 			if (duart->statusB&XMT_RDY) {
   3770 				duart->dataB = T_STREAM;
   3771 				break;
   3772 			}
   3773 		}
   3774 	}
   3775 OUT:
   3776 	duart->imask = qdflags[unit].duart_imask;
   3777 
   3778 } /* setup_input */
   3779 
   3780 /*
   3781  * delay for at least one display frame time
   3782  *
   3783  *	return: BAD means that we timed out without ever seeing the
   3784  *		      vertical sync status bit
   3785  *		GOOD otherwise
   3786  */
   3787 int
   3788 wait_status(adder, mask)
   3789 	volatile struct adder *adder;
   3790 	int mask;
   3791 {
   3792 	int i;
   3793 
   3794 	for (i = 10000, adder->status = 0 ; i > 0  &&
   3795 	     !(adder->status&mask) ; --i)
   3796 		;
   3797 
   3798 	if (i == 0) {
   3799 		printf("wait_status: timeout polling for 0x%x in adder->status\n", mask);
   3800 		return(BAD);
   3801 	}
   3802 
   3803 	return(GOOD);
   3804 
   3805 } /* wait_status */
   3806 
   3807 /*
   3808  * write out onto the ID bus
   3809  */
   3810 void
   3811 write_ID(adder, adrs, data)
   3812 	volatile struct adder *adder;
   3813 	short adrs;
   3814 	short data;
   3815 {
   3816 	int i;
   3817 
   3818 	for (i = 100000, adder->status = 0 ;
   3819 	      i > 0  &&  !(adder->status&ADDRESS_COMPLETE) ; --i)
   3820 		;
   3821 
   3822 	if (i == 0)
   3823 		goto ERR;
   3824 
   3825 	for (i = 100000, adder->status = 0 ;
   3826 	      i > 0  &&  !(adder->status&TX_READY) ; --i)
   3827 		;
   3828 
   3829 	if (i > 0) {
   3830 		adder->id_data = data;
   3831 		adder->command = ID_LOAD | adrs;
   3832 		return ;
   3833 	}
   3834 
   3835 ERR:
   3836 	printf("write_ID: timeout trying to write to VIPER\n");
   3837 	return ;
   3838 
   3839 } /* write_ID */
   3840