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