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