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