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