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