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