Home | History | Annotate | Line # | Download | only in uba
qv.c revision 1.29.2.1
      1 /*	$NetBSD: qv.c,v 1.29.2.1 2014/08/10 06:54:10 tls Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1988
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)qv.c	7.2 (Berkeley) 1/21/94
     37  */
     38 
     39 /*
     40  *	derived from: @(#)qv.c	1.8 (ULTRIX) 8/21/85
     41  */
     42 
     43 /************************************************************************
     44  *									*
     45  *			Copyright (c) 1985 by				*
     46  *		Digital Equipment Corporation, Maynard, MA		*
     47  *			All rights reserved.				*
     48  *									*
     49  *   This software is furnished under a license and may be used and	*
     50  *   copied  only  in accordance with the terms of such license and	*
     51  *   with the  inclusion  of  the  above  copyright  notice.   This	*
     52  *   software  or  any  other copies thereof may not be provided or	*
     53  *   otherwise made available to any other person.  No title to and	*
     54  *   ownership of the software is hereby transferred.			*
     55  *									*
     56  *   This software is  derived  from  software  received  from  the	*
     57  *   University    of   California,   Berkeley,   and   from   Bell	*
     58  *   Laboratories.  Use, duplication, or disclosure is  subject  to	*
     59  *   restrictions  under  license  agreements  with  University  of	*
     60  *   California and with AT&T.						*
     61  *									*
     62  *   The information in this software is subject to change  without	*
     63  *   notice  and should not be construed as a commitment by Digital	*
     64  *   Equipment Corporation.						*
     65  *									*
     66  *   Digital assumes no responsibility for the use  or  reliability	*
     67  *   of its software on equipment which is not supplied by Digital.	*
     68  *									*
     69  ************************************************************************
     70  *
     71  * This driver provides glass tty functionality to the qvss. It is a strange
     72  * device in that it supports three subchannels. The first being the asr,
     73  * the second being a channel that intercepts the chars headed for the screen
     74  * ( like a pseudo tty ) and the third being a source of mouse state changes.
     75  * NOTE: the second is conditional on #ifdef CONS_HACK in this version
     76  * of the driver, as it's a total crock.
     77  *
     78  * There may be one and only one qvss in the system.  This restriction is based
     79  * on the inability to map more than one at a time.  This restriction will
     80  * exist until the kernel has shared memory services. This driver therefore
     81  * support a single unit. No attempt was made to have it service more.
     82  *
     83  * (this belongs in sccs - not here)
     84  *
     85  * 02 Aug 85 -- rjl
     86  *	Changed the names of the special setup routines so that the system
     87  *	can have a qvss or a qdss system console.
     88  *
     89  * 03 Jul 85 -- rjl
     90  *	Added a check for virtual mode in qvputc so that the driver
     91  *	doesn't crash while in a dump which is done in physical mode.
     92  *
     93  * 10 Apr 85 -- jg
     94  *	Well, our theory about keyboard handling was wrong; most of the
     95  *	keyboard is in autorepeat, down mode.  These changes are to make
     96  *	the qvss work the same as the Vs100, which is not necessarily
     97  *	completely correct, as some chord usage may fail.  But since we
     98  *	can't easily change the Vs100, we might as well propagate the
     99  *	problem to another device.  There are also changes for screen and
    100  *	mouse accellaration.
    101  *
    102  * 27 Mar 85 -- rjl
    103  *	MicroVAX-II systems have interval timers that interrupt at ipl4.
    104  *	Everything else is higher and thus causes us to miss clock ticks. The
    105  *	problem isn't severe except in the case of a device like this one that
    106  *	generates lots of interrupts. We aren't willing to make this change to
    107  *	all device drivers but it seems acceptable in this case.
    108  *
    109  *  3 Dec 84 -- jg
    110  *	To continue the tradition of building a better mouse trap,  this
    111  * 	driver has been extended to form Vs100 style event queues.  If the
    112  *	mouse device is open, the keyboard events are intercepted and put
    113  *	into the shared memory queue.  Unfortunately, we are ending up with
    114  *	one of the longest Unix device drivers.  Sigh....
    115  *
    116  * 20 Nov 84 -- rjl
    117  *      As a further complication this driver is required to function as the
    118  *      virtual system console. This code runs before and during auto-
    119  *      configuration and therefore is require to have a second path for setup.
    120  *      It is futher constrained to have a character output routine that
    121  *      is not dependent on the interrupt system.
    122  *
    123  */
    124 
    125 #include <sys/cdefs.h>
    126 __KERNEL_RCSID(0, "$NetBSD: qv.c,v 1.29.2.1 2014/08/10 06:54:10 tls Exp $");
    127 
    128 #include "qv.h"
    129 #if NQV > 0
    130 
    131 #include "../include/pte.h"
    132 
    133 #include "sys/param.h"
    134 #include "sys/conf.h"
    135 #include "qvioctl.h"
    136 #include "sys/tty.h"
    137 #include "sys/buf.h"
    138 #include "sys/vm.h"
    139 #include "sys/file.h"
    140 #include "sys/uio.h"
    141 #include "sys/kernel.h"
    142 #include "sys/syslog.h"
    143 #include "../include/cpu.h"
    144 #include "../include/mtpr.h"
    145 #include "ubareg.h"
    146 #include "ubavar.h"
    147 
    148 #define CONS_HACK
    149 
    150 struct	uba_device *qvinfo[NQV];
    151 
    152 struct	tty qv_tty[NQV*4];
    153 
    154 #define	nNQV  NQV
    155 int	nqv = NQV*4;
    156 
    157 /*
    158  * Definition of the driver for the auto-configuration program.
    159  */
    160 int	qvprobe(), qvattach(), qvkint(), qvvint();
    161 u_short	qvstd[] = { 0 };
    162 struct	uba_driver qvdriver =
    163 	{ qvprobe, 0, qvattach, 0, qvstd, "qv", qvinfo };
    164 
    165 extern	char qvmem[][512*VAX_NBPG];
    166 extern	struct pte QVmap[][512];
    167 
    168 /*
    169  * Local variables for the driver. Initialized for 15' screen
    170  * so that it can be used during the boot process.
    171  */
    172 
    173 #define QVWAITPRI 	(PZERO+1)
    174 
    175 #define QVKEYBOARD 	0	/* minor 0, keyboard/glass tty */
    176 #define QVPCONS 	1	/* minor 1, console interceptor XXX */
    177 #define QVMOUSECHAN 	2	/* minor 2, mouse */
    178 #define	QVSPARE		3	/* unused */
    179 #define QVCHAN(unit)	((unit) & 03)
    180 /*
    181  * v_putc is the switch that is used to redirect the console cnputc to the
    182  * virtual console vputc.  consops is used to redirect the console
    183  * device to the qvss console.
    184  */
    185 extern int (*v_putc)();
    186 extern const struct cdevsw *consops;
    187 /*
    188  * qv_def_scrn is used to select the appropriate tables. 0=15 inch 1=19 inch,
    189  * 2 = uVAXII.
    190  */
    191 int qv_def_scrn = 2;
    192 
    193 #define QVMAXEVQ	64	/* must be power of 2 */
    194 #define EVROUND(x)	((x) & (QVMAXEVQ - 1))
    195 
    196 /*
    197  * Screen parameters 15 & 19 inch monitors. These determine the max size in
    198  * pixel and character units for the display and cursor positions.
    199  * Notice that the mouse defaults to original square algorithm, but X
    200  * will change to its defaults once implemented.
    201  */
    202 struct qv_info *qv_scn;
    203 struct qv_info qv_scn_defaults[] = {
    204 	{0, {0, 0}, 0, {0, 0}, 0, 0, 30, 80, 768, 480, 768-16, 480-16,
    205 	 0, 0, 0, 0, 0, QVMAXEVQ, 0, 0, {0, 0}, {0, 0, 0, 0}, 2, 4},
    206 	{0, {0, 0}, 0, {0, 0}, 0, 0, 55, 120, 960, 864, 960-16, 864-16,
    207 	 0, 0, 0, 0, 0, QVMAXEVQ, 0, 0, {0, 0}, {0, 0, 0, 0}, 2, 4},
    208 	{0, {0, 0}, 0, {0, 0}, 0, 0, 56, 120,1024, 864,1024-16, 864-16,
    209 	 0, 0, 0, 0, 0, QVMAXEVQ, 0, 0, {0, 0}, {0, 0, 0, 0}, 2, 4}
    210 };
    211 
    212 /*
    213  * Screen controller initialization parameters. The definations and use
    214  * of these parameters can be found in the Motorola 68045 crtc specs. In
    215  * essence they set the display parameters for the chip. The first set is
    216  * for the 15" screen and the second is for the 19" separate sync. There
    217  * is also a third set for a 19" composite sync monitor which we have not
    218  * tested and which is not supported.
    219  */
    220 static short qv_crt_parms[][16] = {
    221            { 31, 25, 27, 0142, 31, 13, 30, 31, 4, 15, 040, 0, 0, 0, 0, 0 },
    222 /* VR100*/ { 39, 30, 32, 0262, 55, 5, 54, 54, 4, 15, 040, 0, 0, 0, 0, 0 },
    223 /* VR260*/ { 39, 32, 33, 0264, 56, 5, 54, 54, 4, 15, 040, 0, 0, 0, 0, 0},
    224 };
    225 
    226 /*
    227  * Screen parameters
    228  */
    229 struct qv_info  *qv_scn;
    230 int maxqvmem = 254*1024 - sizeof(struct qv_info) - QVMAXEVQ*sizeof(vsEvent);
    231 
    232 /*
    233  * Keyboard state
    234  */
    235 struct qv_keyboard {
    236 	int shift;			/* state variables	*/
    237 	int cntrl;
    238 	int lock;
    239 	char last;			/* last character	*/
    240 } qv_keyboard;
    241 
    242 short divdefaults[15] = { LK_DOWN,	/* 0 doesn't exist */
    243 	LK_AUTODOWN, LK_AUTODOWN, LK_AUTODOWN, LK_DOWN,
    244 	LK_UPDOWN,   LK_UPDOWN,   LK_AUTODOWN, LK_AUTODOWN,
    245 	LK_AUTODOWN, LK_AUTODOWN, LK_AUTODOWN, LK_AUTODOWN,
    246 	LK_DOWN, LK_AUTODOWN };
    247 
    248 short kbdinitstring[] = {		/* reset any random keyboard stuff */
    249 	LK_AR_ENABLE,			/* we want autorepeat by default */
    250 	LK_CL_ENABLE,			/* keyclick */
    251 	0x84,				/* keyclick volume */
    252 	LK_KBD_ENABLE,			/* the keyboard itself */
    253 	LK_BELL_ENABLE,			/* keyboard bell */
    254 	0x84,				/* bell volume */
    255 	LK_LED_DISABLE,			/* keyboard leds */
    256 	LED_ALL };
    257 #define KBD_INIT_LENGTH	sizeof(kbdinitstring)/sizeof(short)
    258 
    259 #define TOY ((time.tv_sec * 100) + (time.tv_usec / 10000))
    260 
    261 int	qv_ipl_lo = 1;			/* IPL low flag			*/
    262 int	mouseon = 0;			/* mouse channel is enabled when 1*/
    263 struct proc *qvrsel;			/* process waiting for select */
    264 
    265 int	qvstart(), qvputc(),  ttrstrt();
    266 
    267 /*
    268  * Keyboard translation and font tables
    269  */
    270 extern u_short q_key[], q_shift_key[], q_cursor[];
    271 extern char *q_special[], q_font[];
    272 
    273 dev_type_open(qvopen);
    274 dev_type_close(qvclose);
    275 dev_type_read(qvread);
    276 dev_type_write(qvwrite);
    277 dev_type_ioctl(qvioctl);
    278 dev_type_stop(qvstop);
    279 dev_type_poll(qvpoll);
    280 dev_type_kqfilter(qvkqfilter);
    281 
    282 const struct cdevsw qv_cdevsw = {
    283 	.d_open = qvopen,
    284 	.d_close = qvclose,
    285 	.d_read = qvread,
    286 	.d_write = qvwrite,
    287 	.d_ioctl = qvioctl,
    288 	.d_stop = qvstop,
    289 	.d_tty = notty,
    290 	.d_poll = qvpoll,
    291 	.d_mmap = nommap,
    292 	.d_kqfilter = qvkqfilter,
    293 	.d_discard = nodiscard,
    294 	.d_flag = 0
    295 };
    296 
    297 /*
    298  * See if the qvss will interrupt.
    299  */
    300 
    301 /*ARGSUSED*/
    302 qvprobe(void *reg, int ctlr)
    303 {
    304 	register int br, cvec;		/* these are ``value-result'' */
    305 	register struct qvdevice *qvaddr = (struct qvdevice *)reg;
    306 	static int tvec, ovec;
    307 
    308 #ifdef lint
    309 	br = 0; cvec = br; br = cvec;
    310 	qvkint(0); qvvint(0);
    311 #endif
    312 	/*
    313 	 * Allocate the next two vectors
    314 	 */
    315 	tvec = 0360;
    316 	ovec = cvec;
    317 	/*
    318 	 * Turn on the keyboard and vertical interrupt vectors.
    319 	 */
    320 	qvaddr->qv_intcsr = 0;		/* init the interrupt controller */
    321 	qvaddr->qv_intcsr = 0x40;	/* reset irr			*/
    322 	qvaddr->qv_intcsr = 0x80;	/* specify individual vectors	*/
    323 	qvaddr->qv_intcsr = 0xc0;	/* preset autoclear data	*/
    324 	qvaddr->qv_intdata = 0xff;	/* all setup as autoclear	*/
    325 
    326 	qvaddr->qv_intcsr = 0xe0;	/* preset vector address 1	*/
    327 	qvaddr->qv_intdata = tvec;	/* give it the keyboard vector	*/
    328 	qvaddr->qv_intcsr = 0x28;	/* enable tx/rx interrupt	*/
    329 
    330 	qvaddr->qv_intcsr = 0xe1;	/* preset vector address 2	*/
    331 	qvaddr->qv_intdata = tvec+4;	/* give it the vertical sysnc	*/
    332 	qvaddr->qv_intcsr = 0x29;	/* enable 			*/
    333 
    334 	qvaddr->qv_intcsr = 0xa1;	/* arm the interrupt ctrl	*/
    335 
    336 	qvaddr->qv_uartcmd = 0x15;	/* set mode pntr/enable rx/tx	*/
    337 	qvaddr->qv_uartmode = 0x17;	/* noparity, 8-bit		*/
    338 	qvaddr->qv_uartmode = 0x07;	/* 1 stop bit			*/
    339 	qvaddr->qv_uartstatus = 0x99;	/* 4800 baud xmit/recv 		*/
    340 	qvaddr->qv_uartintstatus = 2;	/* enable recv interrupts	*/
    341 
    342 	qvaddr->qv_csr |= QV_INT_ENABLE | QV_CUR_MODE;
    343 
    344 	DELAY(10000);
    345 
    346 	qvaddr->qv_csr &= ~QV_INT_ENABLE;
    347 
    348 	/*
    349 	 * If the qvss did interrupt it was the second vector not
    350 	 * the first so we have to return the first so that they
    351 	 * will be setup properly
    352 	 */
    353 	if( ovec == cvec ) {
    354 		return 0;
    355 	} else
    356 		cvec -= 4;
    357 	return (sizeof (struct qvdevice));
    358 }
    359 
    360 /*
    361  * Routine called to attach a qv.
    362  */
    363 qvattach(struct uba_device *ui)
    364 {
    365 
    366         /*
    367          * If not the console then we have to setup the screen
    368          */
    369         if (v_putc != qvputc || ui->ui_unit != 0)
    370                 (void)qv_setup((struct qvdevice *)ui->ui_addr, ui->ui_unit, 1);
    371 	else
    372 		qv_scn->qvaddr = (struct qvdevice *)ui->ui_addr;
    373 }
    374 
    375 
    376 /*ARGSUSED*/
    377 int
    378 qvopen(dev_t dev, int flag, int mode, struct proc *p)
    379 {
    380 	register struct tty *tp;
    381 	register int unit, qv;
    382 	register struct qvdevice *qvaddr;
    383 	register struct uba_device *ui;
    384 	register struct qv_info *qp = qv_scn;
    385 
    386 	unit = minor(dev);
    387 	qv = unit >> 2;
    388 	if (unit >= nqv || (ui = qvinfo[qv])== 0 || ui->ui_alive == 0)
    389 		return (ENXIO);
    390 	if (QVCHAN(unit) == QVSPARE
    391 #ifndef CONS_HACK
    392 	   || QVCHAN(unit) == QVPCONS
    393 #endif
    394 	   )
    395 		return (ENODEV);
    396 	tp = &qv_tty[unit];
    397 	if (tp->t_state&TS_XCLUDE && u.u_uid!=0)
    398 		return (EBUSY);
    399 	qvaddr = (struct qvdevice *)ui->ui_addr;
    400         qv_scn->qvaddr = qvaddr;
    401 	tp->t_addr = (void *)qvaddr;
    402 	tp->t_oproc = qvstart;
    403 
    404 	if ((tp->t_state&TS_ISOPEN) == 0) {
    405 		ttychars(tp);
    406 		tp->t_state = TS_ISOPEN|TS_CARR_ON;
    407 		tp->t_ispeed = B9600;
    408 		tp->t_ospeed = B9600;
    409 		if( QVCHAN(unit) == QVKEYBOARD ) {
    410 			/* make sure keyboard is always back to default */
    411 			qvkbdreset();
    412 			qvaddr->qv_csr |= QV_INT_ENABLE;
    413 			tp->t_iflag = TTYDEF_IFLAG;
    414 			tp->t_oflag = TTYDEF_OFLAG;
    415 			tp->t_lflag = TTYDEF_LFLAG;
    416 			tp->t_cflag = TTYDEF_CFLAG;
    417 		}
    418 		/* XXX ?why?  else
    419 			tp->t_flags = RAW;
    420 		*/
    421 	}
    422 	/*
    423 	 * Process line discipline specific open if its not the
    424 	 * mouse channel. For the mouse we init the ring ptr's.
    425 	 */
    426 	if( QVCHAN(unit) != QVMOUSECHAN )
    427 		return ((*tp->t_linesw->l_open)(dev, tp));
    428 	else {
    429 		mouseon = 1;
    430 		/* set up event queue for later */
    431 		qp->ibuff = (vsEvent *)qp - QVMAXEVQ;
    432 		qp->iqsize = QVMAXEVQ;
    433 		qp->ihead = qp->itail = 0;
    434 		return 0;
    435 	}
    436 
    437 	return (0);
    438 }
    439 
    440 /*
    441  * Close a QVSS line.
    442  */
    443 /*ARGSUSED*/
    444 int
    445 qvclose(dev_t dev, int flag, int mode, struct proc *p)
    446 {
    447 	register struct tty *tp;
    448 	register unit;
    449 	register struct qvdevice *qvaddr;
    450 	int error;
    451 
    452 	unit = minor(dev);
    453 	tp = &qv_tty[unit];
    454 
    455 	/*
    456 	 * If this is the keyboard unit (0) shutdown the
    457 	 * interface.
    458 	 */
    459 	qvaddr = (struct qvdevice *)tp->t_addr;
    460 	if (QVCHAN(unit) == QVKEYBOARD )
    461 		qvaddr->qv_csr &= ~QV_INT_ENABLE;
    462 
    463 	/*
    464 	 * If unit is not the mouse channel call the line disc.
    465 	 * otherwise clear the state flag, and put the keyboard into down/up.
    466 	 */
    467 	if (QVCHAN(unit) != QVMOUSECHAN) {
    468 		(*tp->t_linesw->l_close)(tp, flag);
    469 		error = ttyclose(tp);
    470 	} else {
    471 		mouseon = 0;
    472 		qv_init( qvaddr );
    473 		error = 0;
    474 	}
    475 	tp->t_state = 0;
    476 	return (error);
    477 }
    478 
    479 int
    480 qvread(dev_t dev, struct uio *uio, int flag)
    481 {
    482 	register struct tty *tp;
    483 	int unit = minor( dev );
    484 
    485 	if (QVCHAN(unit) != QVMOUSECHAN) {
    486 		tp = &qv_tty[unit];
    487 		return ((*tp->t_linesw->l_read)(tp, uio));
    488 	}
    489 	return (ENXIO);
    490 }
    491 
    492 int
    493 qvwrite(dev_t dev, struct uio *uio, int flag)
    494 {
    495 	register struct tty *tp;
    496 	int unit = minor( dev );
    497 
    498 	/*
    499 	 * If this is the mouse we simply fake the i/o, otherwise
    500 	 * we let the line disp. handle it.
    501 	 */
    502 	if (QVCHAN(unit) == QVMOUSECHAN) {
    503 		uio->uio_offset = uio->uio_resid;
    504 		uio->uio_resid = 0;
    505 		return 0;
    506 	}
    507 	tp = &qv_tty[unit];
    508 	return ((*tp->t_linesw->l_write)(tp, uio));
    509 }
    510 
    511 int
    512 qvpoll(dev_t dev, int events, struct proc *p)
    513 {
    514 	register struct tty *tp;
    515 	int unit = minor( dev );
    516 
    517 	/*
    518 	 * XXX Should perform similar checks to deprecated `qvselect()'
    519 	 */
    520 	tp = &qv_tty[unit];
    521 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    522 }
    523 
    524 /*
    525  * XXX Is qvselect() even useful now?
    526  * This driver looks to have suffered some serious bit-rot...
    527  */
    528 
    529 /*
    530  * Mouse activity select routine
    531  */
    532 qvselect(dev_t dev, rw)
    533 {
    534 	register int s = spl5();
    535 	register struct qv_info *qp = qv_scn;
    536 
    537 	if( QVCHAN(minor(dev)) == QVMOUSECHAN )
    538 		switch(rw) {
    539 		case FREAD:			/* if events okay */
    540 			if(qp->ihead != qp->itail) {
    541 				splx(s);
    542 				return(1);
    543 			}
    544 			qvrsel = u.u_procp;
    545 			splx(s);
    546 			return(0);
    547 		default:			/* can never write */
    548 			splx(s);
    549 			return(0);
    550 		}
    551 	else {
    552 		splx(s);
    553 		return( ttselect(dev, rw) );
    554 	}
    555 	/*NOTREACHED*/
    556 }
    557 
    558 /*
    559  * QVSS keyboard interrupt.
    560  */
    561 qvkint(int qv)
    562 {
    563 	struct tty *tp;
    564 	register c;
    565 	struct uba_device *ui;
    566 	register int key;
    567 	register int i;
    568 
    569 	ui = qvinfo[qv];
    570 	if (ui == 0 || ui->ui_alive == 0)
    571 		return;
    572 	tp = &qv_tty[qv<<2];
    573 	/*
    574 	 * Get a character from the keyboard.
    575 	 */
    576 	key = ((struct qvdevice *)ui->ui_addr)->qv_uartdata & 0xff;
    577 	if( mouseon == 0) {
    578 		/*
    579 		 * Check for various keyboard errors
    580 		 */
    581 		if( key == LK_POWER_ERROR || key == LK_KDOWN_ERROR ||
    582 		    key == LK_INPUT_ERROR || key == LK_OUTPUT_ERROR) {
    583 			log(LOG_ERR,
    584 			    "qv%d: Keyboard error, code = %x\n",qv,key);
    585 			return;
    586 		}
    587 		if( key < LK_LOWEST ) return;
    588 		/*
    589 		 * See if its a state change key
    590 		 */
    591 		switch ( key ) {
    592 		case LOCK:
    593 			qv_keyboard.lock ^= 0xffff;	/* toggle */
    594 			if( qv_keyboard.lock )
    595 				qv_key_out( LK_LED_ENABLE );
    596 			else
    597 				qv_key_out( LK_LED_DISABLE );
    598 			qv_key_out( LED_3 );
    599 			return;
    600 		case SHIFT:
    601 			qv_keyboard.shift ^= 0xffff;
    602 			return;
    603 		case CNTRL:
    604 			qv_keyboard.cntrl ^= 0xffff;
    605 			return;
    606 		case ALLUP:
    607 			qv_keyboard.cntrl = qv_keyboard.shift = 0;
    608 			return;
    609 		case REPEAT:
    610 			c = qv_keyboard.last;
    611 			break;
    612 		default:
    613 		/*
    614 		 * Test for control characters. If set, see if the character
    615 		 * is elligible to become a control character.
    616 		 */
    617 			if( qv_keyboard.cntrl ) {
    618 				c = q_key[ key ];
    619 				if( c >= ' ' && c <= '~' )
    620 					c &= 0x1f;
    621 			} else if( qv_keyboard.lock || qv_keyboard.shift )
    622 				c = q_shift_key[ key ];
    623 				else
    624 				c = q_key[ key ];
    625 			break;
    626 		}
    627 
    628 		qv_keyboard.last = c;
    629 
    630 		/*
    631 		 * Check for special function keys
    632 		 */
    633 		if( c & 0x80 ) {
    634 			register char *string;
    635 			string = q_special[ c & 0x7f ];
    636 			while( *string )
    637 			(*tp->t_linesw->l_rint)(*string++, tp);
    638 		} else
    639 			(*tp->t_linesw->l_rint)(c, tp);
    640 	} else {
    641 		/*
    642 		 * Mouse channel is open put it into the event queue
    643 		 * instead.
    644 		 */
    645 		register struct qv_info *qp = qv_scn;
    646 		register vsEvent *vep;
    647 
    648 		if ((i = EVROUND(qp->itail+1)) == qp->ihead)
    649 			return;
    650 		vep = &qp->ibuff[qp->itail];
    651 		vep->vse_direction = VSE_KBTRAW;
    652 		vep->vse_type = VSE_BUTTON;
    653 		vep->vse_device = VSE_DKB;
    654 		vep->vse_x = qp->mouse.x;
    655 		vep->vse_y = qp->mouse.y;
    656 		vep->vse_time = TOY;
    657 		vep->vse_key = key;
    658 		qp->itail = i;
    659 		if(qvrsel) {
    660 			selnotify(qvrsel, 0, 0);
    661 			qvrsel = 0;
    662 		}
    663 	}
    664 }
    665 
    666 /*
    667  * Ioctl for QVSS.
    668  */
    669 /*ARGSUSED*/
    670 int
    671 qvioctl(dev_t dev, u_long cmd, register void *data, int flag, struct proc *p)
    672 {
    673 	register struct tty *tp;
    674 	register int unit = minor(dev);
    675 	register struct qv_info *qp = qv_scn;
    676 	register struct qv_kpcmd *qk;
    677 	register unsigned char *cp;
    678 	int error;
    679 
    680 	/*
    681 	 * Check for and process qvss specific ioctl's
    682 	 */
    683 	switch( cmd ) {
    684 	case QIOCGINFO:					/* return screen info */
    685 		memcpy(data, (void *)qp, sizeof (struct qv_info));
    686 		break;
    687 
    688 	case QIOCSMSTATE:				/* set mouse state */
    689 		qp->mouse = *((vsCursor *)data);
    690 		qv_pos_cur( qp->mouse.x, qp->mouse.y );
    691 		break;
    692 
    693 	case QIOCINIT:					/* init screen	*/
    694 		qv_init( qp->qvaddr );
    695 		break;
    696 
    697 	case QIOCKPCMD:
    698 		qk = (struct qv_kpcmd *)data;
    699 		if(qk->nbytes == 0) qk->cmd |= 0200;
    700 		if(mouseon == 0) qk->cmd |= 1;	/* no mode changes */
    701 		qv_key_out(qk->cmd);
    702 		cp = &qk->par[0];
    703 		while(qk->nbytes-- > 0) {	/* terminate parameters */
    704 			if(qk->nbytes <= 0) *cp |= 0200;
    705 			qv_key_out(*cp++);
    706 		}
    707 		break;
    708 	case QIOCADDR:					/* get struct addr */
    709 		*(struct qv_info **) data = qp;
    710 		break;
    711 	default:					/* not ours ??  */
    712 		tp = &qv_tty[unit];
    713 		error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag);
    714 		if (error != EPASSTHROUGH)
    715 			return (error);
    716 		return ttioctl(tp, cmd, data, flag);
    717 		break;
    718 	}
    719 	return (0);
    720 }
    721 /*
    722  * Initialize the screen and the scanmap
    723  */
    724 qv_init(struct qvdevice *qvaddr)
    725 {
    726 	register short *scanline;
    727 	register int i;
    728 	register short scan;
    729 	register char *ptr;
    730 	register struct qv_info *qp = qv_scn;
    731 
    732 	/*
    733 	 * Clear the bit map
    734 	 */
    735 	for( i=0 , ptr = qp->bitmap ; i<240 ; i += 2 , ptr += 2048)
    736 		memset( ptr, 0, 2048 );
    737 	/*
    738 	 * Reinitialize the scanmap
    739 	 */
    740         scan = qvaddr->qv_csr & QV_MEM_BANK;
    741         scanline = qp->scanmap;
    742         for(i = 0 ; i < qp->max_y ; i++ )
    743                 *scanline++ = scan++;
    744 
    745 	/*
    746 	 * Home the cursor
    747 	 */
    748 	qp->row = qp->col = 0;
    749 
    750 	/*
    751 	 * Reset the cursor to the default type.
    752 	 */
    753 	for( i=0 ; i<16 ; i++ )
    754 		qp->cursorbits[i] = q_cursor[i];
    755 	qvaddr->qv_csr |= QV_CUR_MODE;
    756 	/*
    757 	 * Reset keyboard to default state.
    758 	 */
    759 	qvkbdreset();
    760 }
    761 
    762 qvreset(void)
    763 {
    764 }
    765 qvkbdreset(void)
    766 {
    767 	register int i;
    768 	qv_key_out(LK_DEFAULTS);
    769 	for( i=1 ; i < 15 ; i++ )
    770 		qv_key_out( divdefaults[i] | (i<<3));
    771 	for (i = 0; i < KBD_INIT_LENGTH; i++)
    772 		qv_key_out(kbdinitstring[i]);
    773 }
    774 
    775 #define abs(x) (((x) > 0) ? (x) : (-(x)))
    776 /*
    777  * QVSS vertical sync interrupt
    778  */
    779 qvvint(int qv)
    780 {
    781 	extern int selwait;
    782 	register struct qvdevice *qvaddr;
    783 	struct uba_device *ui;
    784 	register struct qv_info *qp = qv_scn;
    785 	int unit;
    786 	struct tty *tp0;
    787 	int i;
    788 	register int j;
    789 	/*
    790 	 * Mouse state info
    791 	 */
    792 	static ushort omouse = 0, nmouse = 0;
    793 	static char omx=0, omy=0, mx=0, my=0, om_switch=0, m_switch=0;
    794 	register int dx, dy;
    795 
    796 	/*
    797 	 * Test and set the qv_ipl_lo flag. If the result is not zero then
    798 	 * someone else must have already gotten here.
    799 	 */
    800 	if( --qv_ipl_lo )
    801 		return;
    802 	(void)spl4();
    803 	ui = qvinfo[qv];
    804 	unit = qv<<2;
    805 	qvaddr = (struct qvdevice *)ui->ui_addr;
    806 	tp0 = &qv_tty[QVCHAN(unit) + QVMOUSECHAN];
    807 	/*
    808 	 * See if the mouse has moved.
    809 	 */
    810 	if( omouse != (nmouse = qvaddr->qv_mouse) ) {
    811 		omouse = nmouse;
    812 		mx = nmouse & 0xff;
    813 		my = nmouse >> 8;
    814 		dy = my - omy; omy = my;
    815 		dx = mx - omx; omx = mx;
    816 		if( dy < 50 && dy > -50 && dx < 50 && dx > -50 ) {
    817 			register vsEvent *vep;
    818 			if( qp->mscale < 0 ) {	/* Ray Lanza's original */
    819 				if( dy < 0 )
    820 					dy = -( dy * dy );
    821 				else
    822 					dy *= dy;
    823 				if( dx < 0 )
    824 					dx = -( dx * dx );
    825 				else
    826 					dx *= dx;
    827 			}
    828 			else {			/* Vs100 style, see WGA spec */
    829 			    int thresh = qp->mthreshold;
    830 			    int scale  = qp->mscale;
    831 			    if( abs(dx) > thresh ) {
    832 				if ( dx < 0 )
    833 				    dx = (dx + thresh)*scale - thresh;
    834 				else
    835 				    dx = (dx - thresh)*scale + thresh;
    836 			    }
    837 			    if( abs(dy) > thresh ) {
    838 				if ( dy < 0 )
    839 				    dy = (dy + thresh)*scale - thresh;
    840 				else
    841 				    dy = (dy - thresh)*scale + thresh;
    842 			    }
    843 			}
    844 			qp->mouse.x += dx;
    845 			qp->mouse.y -= dy;
    846 			if( qp->mouse.x < 0 )
    847 				qp->mouse.x = 0;
    848 			if( qp->mouse.y < 0 )
    849 				qp->mouse.y = 0;
    850 			if( qp->mouse.x > qp->max_cur_x )
    851 				qp->mouse.x = qp->max_cur_x;
    852 			if( qp->mouse.y > qp->max_cur_y )
    853 				qp->mouse.y = qp->max_cur_y;
    854 			if( tp0->t_state & TS_ISOPEN )
    855 				qv_pos_cur( qp->mouse.x, qp->mouse.y );
    856 			if (qp->mouse.y < qp->mbox.bottom &&
    857 			    qp->mouse.y >=  qp->mbox.top &&
    858 			    qp->mouse.x < qp->mbox.right &&
    859 			    qp->mouse.x >=  qp->mbox.left) goto switches;
    860 			qp->mbox.bottom = 0;	/* trash box */
    861 			if (EVROUND(qp->itail+1) == qp->ihead)
    862 				goto switches;
    863 			i = EVROUND(qp->itail - 1);
    864 			if ((qp->itail != qp->ihead) &&	(i != qp->ihead)) {
    865 				vep = & qp->ibuff[i];
    866 				if(vep->vse_type == VSE_MMOTION) {
    867 					vep->vse_x = qp->mouse.x;
    868 					vep->vse_y = qp->mouse.y;
    869 					goto switches;
    870 				}
    871 			}
    872 			/* put event into queue and do select */
    873 			vep = & qp->ibuff[qp->itail];
    874 			vep->vse_type = VSE_MMOTION;
    875 			vep->vse_time = TOY;
    876 			vep->vse_x = qp->mouse.x;
    877 			vep->vse_y = qp->mouse.y;
    878 			qp->itail = EVROUND(qp->itail+1);
    879 		}
    880 	}
    881 	/*
    882 	 * See if mouse switches have changed.
    883 	 */
    884 switches:if( om_switch != ( m_switch = (qvaddr->qv_csr & QV_MOUSE_ANY) >> 8 ) ) {
    885 		qp->mswitches = ~m_switch & 0x7;
    886 		for (j = 0; j < 3; j++) {	/* check each switch */
    887 			register vsEvent *vep;
    888 			if ( ((om_switch>>j) & 1) == ((m_switch>>j) & 1) )
    889 				continue;
    890 			/* check for room in the queue */
    891 			if ((i = EVROUND(qp->itail+1)) == qp->ihead) return;
    892 			/* put event into queue and do select */
    893 			vep = &qp->ibuff[qp->itail];
    894 			vep->vse_type = VSE_BUTTON;
    895 			vep->vse_key = 2 - j;
    896 			vep->vse_direction = VSE_KBTDOWN;
    897 			if ( (m_switch >> j) & 1)
    898 				vep->vse_direction = VSE_KBTUP;
    899 			vep->vse_device = VSE_MOUSE;
    900 			vep->vse_time = TOY;
    901 			vep->vse_x = qp->mouse.x;
    902 			vep->vse_y = qp->mouse.y;
    903 		}
    904 		qp->itail =  i;
    905 		om_switch = m_switch;
    906 		qp->mswitches = m_switch;
    907 	}
    908 	/* if we have proc waiting, and event has happened, wake him up */
    909 	if(qvrsel && (qp->ihead != qp->itail)) {
    910 		selnotify(qvrsel, 0, 0);
    911 		qvrsel = 0;
    912 	}
    913 	/*
    914 	 * Okay we can take another hit now
    915 	 */
    916 	qv_ipl_lo = 1;
    917 }
    918 
    919 /*
    920  * Start  transmission
    921  */
    922 qvstart(register struct tty *tp)
    923 {
    924 	register int unit, c;
    925 	register struct tty *tp0;
    926 	int s;
    927 
    928 	unit = minor(tp->t_dev);
    929 #ifdef CONS_HACK
    930 	tp0 = &qv_tty[(unit&0xfc)+QVPCONS];
    931 #endif
    932 	unit = QVCHAN(unit);
    933 
    934 	s = spl5();
    935 	/*
    936 	 * If it's currently active, or delaying, no need to do anything.
    937 	 */
    938 	if (tp->t_state&(TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
    939 		goto out;
    940 	/*
    941 	 * Display chars until the queue is empty, if the second subchannel
    942 	 * is open direct them there. Drop characters from subchannels other
    943 	 * than 0 on the floor.
    944 	 */
    945 
    946 	while( tp->t_outq.c_cc ) {
    947 		c = getc(&tp->t_outq);
    948 		if (unit == QVKEYBOARD)
    949 #ifdef CONS_HACK
    950 			if( tp0->t_state & TS_ISOPEN ){
    951 				(*tp0->t_linesw->l_rint)(c, tp0);
    952 			} else
    953 #endif
    954 				qvputchar( c & 0xff );
    955 	}
    956 	/*
    957 	 * Position the cursor to the next character location.
    958 	 */
    959 	qv_pos_cur( qv_scn->col*8, qv_scn->row*15 );
    960 
    961 	/*
    962 	 * If there are sleepers, and output has drained below low
    963 	 * water mark, wake up the sleepers.
    964 	 */
    965 	ttypull(tp);
    966 	tp->t_state &= ~TS_BUSY;
    967 out:
    968 	splx(s);
    969 }
    970 
    971 /*
    972  * Stop output on a line, e.g. for ^S/^Q or output flush.
    973  */
    974 /*ARGSUSED*/
    975 void
    976 qvstop(register struct tty *tp, int flag)
    977 {
    978 	register int s;
    979 
    980 	/*
    981 	 * Block input/output interrupts while messing with state.
    982 	 */
    983 	s = spl5();
    984 	if (tp->t_state & TS_BUSY) {
    985 		if ((tp->t_state&TS_TTSTOP)==0) {
    986 			tp->t_state |= TS_FLUSH;
    987 		} else
    988 			tp->t_state &= ~TS_BUSY;
    989 	}
    990 	splx(s);
    991 }
    992 
    993 qvputc(char c)
    994 {
    995 	qvputchar(c);
    996 	if (c == '\n')
    997 		qvputchar('\r');
    998 }
    999 
   1000 /*
   1001  * Routine to display a character on the screen.  The model used is a
   1002  * glass tty.  It is assummed that the user will only use this emulation
   1003  * during system boot and that the screen will be eventually controlled
   1004  * by a window manager.
   1005  *
   1006  */
   1007 qvputchar( c )
   1008 register char c;
   1009 {
   1010 
   1011 	register char *b_row, *f_row;
   1012 	register int i;
   1013 	register short *scanline;
   1014 	register int ote = 128;
   1015 	register struct qv_info *qp = qv_scn;
   1016 
   1017 	/*
   1018 	 * This routine may be called in physical mode by the dump code
   1019 	 * so we check and punt if that's the case.
   1020 	 */
   1021 	if( (mfpr(MAPEN) & 1) == 0 )
   1022 		return;
   1023 
   1024 	c &= 0x7f;
   1025 
   1026 	switch ( c ) {
   1027 	case '\t':				/* tab		*/
   1028 		for( i = 8 - (qp->col & 0x7) ; i > 0 ; i-- )
   1029 			qvputchar( ' ' );
   1030 		break;
   1031 
   1032 	case '\r':				/* return	*/
   1033 		qp->col = 0;
   1034 		break;
   1035 
   1036 	case '\010':				/* backspace	*/
   1037 		if( --qp->col < 0 )
   1038 			qp->col = 0;
   1039 		break;
   1040 
   1041 	case '\n':				/* linefeed	*/
   1042 		if( qp->row+1 >= qp->max_row )
   1043 			qvscroll();
   1044 		else
   1045 			qp->row++;
   1046 		/*
   1047 		* Position the cursor to the next character location.
   1048 		*/
   1049 		qv_pos_cur( qp->col*8, qp->row*15 );
   1050 		break;
   1051 
   1052 	case '\007':				/* bell		*/
   1053                 /*
   1054                  * We don't do anything to the keyboard until after
   1055                  * autoconfigure.
   1056                  */
   1057 		if( qp->qvaddr )
   1058 			qv_key_out( LK_RING_BELL );
   1059 		return;
   1060 
   1061 	default:
   1062 		if( c >= ' ' && c <= '~' ) {
   1063                         scanline = qp->scanmap;
   1064                         b_row = qp->bitmap+(scanline[qp->row*15]&0x3ff)*128+qp->col;
   1065 			i = c - ' ';
   1066 			if( i < 0 || i > 95 )
   1067 				i = 0;
   1068 			else
   1069 				i *= 15;
   1070 			f_row = (char *)((int)q_font + i);
   1071 
   1072 /*			for( i=0 ; i<15 ; i++ , b_row += 128, f_row++ )
   1073 				*b_row = *f_row;*/
   1074 			/* inline expansion for speed */
   1075 			*b_row = *f_row++; b_row += ote;
   1076 			*b_row = *f_row++; b_row += ote;
   1077 			*b_row = *f_row++; b_row += ote;
   1078 			*b_row = *f_row++; b_row += ote;
   1079 			*b_row = *f_row++; b_row += ote;
   1080 			*b_row = *f_row++; b_row += ote;
   1081 			*b_row = *f_row++; b_row += ote;
   1082 			*b_row = *f_row++; b_row += ote;
   1083 			*b_row = *f_row++; b_row += ote;
   1084 			*b_row = *f_row++; b_row += ote;
   1085 			*b_row = *f_row++; b_row += ote;
   1086 			*b_row = *f_row++; b_row += ote;
   1087 			*b_row = *f_row++; b_row += ote;
   1088 			*b_row = *f_row++; b_row += ote;
   1089 			*b_row = *f_row++; b_row += ote;
   1090 
   1091 			if( ++qp->col >= qp->max_col ) {
   1092 				qp->col = 0 ;
   1093 				if( qp->row+1 >= qp->max_row )
   1094 					qvscroll();
   1095 				else
   1096 					qp->row++;
   1097 			}
   1098 		}
   1099 		break;
   1100 	}
   1101 }
   1102 
   1103 /*
   1104  * Position the cursor to a particular spot.
   1105  */
   1106 qv_pos_cur( x, y)
   1107 register int x,y;
   1108 {
   1109 	register struct qvdevice *qvaddr;
   1110 	register struct qv_info *qp = qv_scn;
   1111 	register index;
   1112 
   1113 	if( qvaddr = qp->qvaddr ) {
   1114 		if( y < 0 || y > qp->max_cur_y )
   1115 			y = qp->max_cur_y;
   1116 		if( x < 0 || x > qp->max_cur_x )
   1117 			x = qp->max_cur_x;
   1118 		qp->cursor.x = x;		/* keep track of real cursor*/
   1119 		qp->cursor.y = y;		/* position, indep. of mouse*/
   1120 
   1121 		qvaddr->qv_crtaddr = 10;	/* select cursor start reg */
   1122 		qvaddr->qv_crtdata = y & 0xf;
   1123 		qvaddr->qv_crtaddr = 11;	/* select cursor end reg */
   1124 		qvaddr->qv_crtdata = y & 0xf;
   1125 		qvaddr->qv_crtaddr = 14;	/* select cursor y pos. */
   1126 		qvaddr->qv_crtdata = y >> 4;
   1127 		qvaddr->qv_xcur = x;		/* pos x axis	*/
   1128 		/*
   1129 		 * If the mouse is being used then we change the mode of
   1130 		 * cursor display based on the pixels under the cursor
   1131 		 */
   1132 		if( mouseon ) {
   1133 			index = y*128 + x/8;
   1134 			if( qp->bitmap[ index ] && qp->bitmap[ index+128 ] )
   1135 				qvaddr->qv_csr &= ~QV_CUR_MODE;
   1136 			else
   1137 				qvaddr->qv_csr |=  QV_CUR_MODE;
   1138 		}
   1139 	}
   1140 }
   1141 /*
   1142  * Scroll the bitmap by moving the scanline map words. This could
   1143  * be done by moving the bitmap but it's much too slow for a full screen.
   1144  * The only drawback is that the scanline map must be reset when the user
   1145  * wants to do graphics.
   1146  */
   1147 qvscroll(void)
   1148 {
   1149 	short tmpscanlines[15];
   1150 	register char *b_row;
   1151 	register short *scanline;
   1152 	register struct qv_info *qp = qv_scn;
   1153 
   1154 	/*
   1155 	 * If the mouse is on we don't scroll so that the bit map
   1156 	 * remains sane.
   1157 	 */
   1158 	if( mouseon ) {
   1159 		qp->row = 0;
   1160 		return;
   1161 	}
   1162 	/*
   1163 	 * Save the first 15 scanlines so that we can put them at
   1164 	 * the bottom when done.
   1165 	 */
   1166 	memcpy((void *)tmpscanlines, (void *)qp->scanmap, sizeof tmpscanlines);
   1167 
   1168 	/*
   1169 	 * Clear the wrapping line so that it won't flash on the bottom
   1170 	 * of the screen.
   1171 	 */
   1172         scanline = qp->scanmap;
   1173         b_row = qp->bitmap+(*scanline&0x3ff)*128;
   1174 	memset( b_row, 0, 1920 );
   1175 
   1176 	/*
   1177 	 * Now move the scanlines down
   1178 	 */
   1179 	memcpy((void *)qp->scanmap, (void *)(qp->scanmap+15),
   1180 	      (qp->row * 15) * sizeof (short) );
   1181 
   1182 	/*
   1183 	 * Now put the other lines back
   1184 	 */
   1185 	memcpy((void *)(qp->scanmap+(qp->row * 15)), (void *)tmpscanlines,
   1186 	      sizeof (tmpscanlines) );
   1187 
   1188 }
   1189 
   1190 /*
   1191  * Output to the keyboard. This routine status polls the transmitter on the
   1192  * keyboard to output a code. The timer is to avoid hanging on a bad device.
   1193  */
   1194 qv_key_out(u_short c)
   1195 {
   1196 	int timer = 30000;
   1197 	register struct qv_info *qp = qv_scn;
   1198 
   1199 	if (qp->qvaddr) {
   1200 		while ((qp->qvaddr->qv_uartstatus & 0x4) == 0  && timer--)
   1201 			;
   1202 		qp->qvaddr->qv_uartdata = c;
   1203 	}
   1204 }
   1205 /*
   1206  * Virtual console initialization. This routine sets up the qvss so that it can
   1207  * be used as the system console. It is invoked before autoconfig and has to do
   1208  * everything necessary to allow the device to serve as the system console.
   1209  * In this case it must map the q-bus and device areas and initialize the qvss
   1210  * screen.
   1211  */
   1212 qvcons_init(void)
   1213 {
   1214         struct percpu *pcpu;            /* pointer to percpu structure  */
   1215 	register struct qbus *qb;
   1216         struct qvdevice *qvaddr;        /* device pointer               */
   1217         short *devptr;                  /* virtual device space         */
   1218 	extern cnputc();		/* standard serial console putc */
   1219 #define QVSSCSR 017200
   1220 
   1221 	/*
   1222 	 * If secondary console already configured,
   1223 	 * don't override the previous one.
   1224 	 */
   1225 	if (v_putc != cnputc)
   1226 		return 0;
   1227         /*
   1228          * find the percpu entry that matches this machine.
   1229          */
   1230         for( pcpu = percpu ; pcpu && pcpu->pc_cputype != cpu ; pcpu++ )
   1231                 ;
   1232         if( pcpu == NULL )
   1233                 return 0;
   1234 	if (pcpu->pc_io->io_type != IO_QBUS)
   1235 		return 0;
   1236 
   1237         /*
   1238          * Found an entry for this CPU. Because this device is Microvax specific
   1239          * we assume that there is a single q-bus and don't have to worry about
   1240          * multiple adapters.
   1241          *
   1242          * Map the device registers.
   1243          */
   1244 	qb = (struct qbus *)pcpu->pc_io->io_details;
   1245 	ioaccess(qb->qb_iopage, UMEMmap[0] + qb->qb_memsize, UBAIOPAGES * VAX_NBPG);
   1246 
   1247         /*
   1248          * See if the qvss is there.
   1249          */
   1250         devptr = (short *)((char *)umem[0] + (qb->qb_memsize * VAX_NBPG));
   1251         qvaddr = (struct qvdevice *)((u_int)devptr + ubdevreg(QVSSCSR));
   1252         if (badaddr((void *)qvaddr, sizeof(short)))
   1253                 return 0;
   1254         /*
   1255          * Okay the device is there lets set it up
   1256          */
   1257         if (!qv_setup(qvaddr, 0, 0))
   1258 		return 0;
   1259 	v_putc = qvputc;
   1260         consops = &qv_cdevsw;
   1261 	return 1;
   1262 }
   1263 /*
   1264  * Do the board specific setup
   1265  */
   1266 qv_setup(struct qvdevice *qvaddr, int unit, int probed)
   1267 {
   1268         void *qvssmem;		/* pointer to the display mem   */
   1269         register i;			/* simple index                 */
   1270 	register struct qv_info *qp;
   1271         register int *pte;
   1272         struct percpu *pcpu;            /* pointer to percpu structure  */
   1273 	register struct qbus *qb;
   1274 
   1275         /*
   1276          * find the percpu entry that matches this machine.
   1277          */
   1278         for( pcpu = percpu ; pcpu && pcpu->pc_cputype != cpu ; pcpu++ )
   1279                 ;
   1280         if( pcpu == NULL )
   1281                 return(0);
   1282 
   1283         /*
   1284          * Found an entry for this CPU. Because this device is Microvax specific
   1285          * we assume that there is a single q-bus and don't have to worry about
   1286          * multiple adapters.
   1287          *
   1288          * Map the device memory.
   1289          */
   1290 	qb = (struct qbus *)pcpu->pc_io->io_details;
   1291 
   1292         i = (u_int)(qvaddr->qv_csr & QV_MEM_BANK) << 7;
   1293 	ioaccess(qb->qb_maddr + i, QVmap[unit], 512 * VAX_NBPG);
   1294 	qvssmem = qvmem[unit];
   1295         pte = (int *)(QVmap[unit]);
   1296         for (i=0; i < 512; i++, pte++)
   1297                 *pte = (*pte & ~PG_PROT) | PG_UW | PG_V;
   1298 
   1299         qv_scn = (struct qv_info *)((u_int)qvssmem + 251*1024);
   1300 	qp = qv_scn;
   1301         if( (qvaddr->qv_csr & QV_19INCH) && qv_def_scrn == 0)
   1302                 qv_def_scrn = 1;
   1303         *qv_scn = qv_scn_defaults[ qv_def_scrn ];
   1304 	if (probed)
   1305 		qp->qvaddr = qvaddr;
   1306  	qp->bitmap = qvssmem;
   1307         qp->scanmap = (short *)((u_int)qvssmem + 254*1024);
   1308         qp->cursorbits = (short *)((u_int)qvssmem + 256*1024-32);
   1309 	/* set up event queue for later */
   1310 	qp->ibuff = (vsEvent *)qp - QVMAXEVQ;
   1311 	qp->iqsize = QVMAXEVQ;
   1312 	qp->ihead = qp->itail = 0;
   1313 
   1314         /*
   1315          * Setup the crt controller chip.
   1316          */
   1317         for( i=0 ; i<16 ; i++ ) {
   1318                 qvaddr->qv_crtaddr = i;
   1319                 qvaddr->qv_crtdata = qv_crt_parms[ qv_def_scrn ][ i ];
   1320         }
   1321         /*
   1322          * Setup the display.
   1323          */
   1324         qv_init( qvaddr );
   1325 
   1326         /*
   1327          * Turn on the video
   1328          */
   1329         qvaddr->qv_csr |= QV_VIDEO_ENA ;
   1330 	return 1;
   1331 }
   1332 #endif
   1333