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