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