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