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