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