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