xref: /original-bsd/sys/vax/uba/qv.c (revision b7cc7b86)
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	1.15 (Berkeley) 12/16/90
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)
395 	dev_t dev;
396 	int flag;
397 {
398 	register struct tty *tp;
399 	register unit;
400 	register struct qvdevice *qvaddr;
401 	int error;
402 
403 	unit = minor(dev);
404 	tp = &qv_tty[unit];
405 
406 	/*
407 	 * If this is the keyboard unit (0) shutdown the
408 	 * interface.
409 	 */
410 	qvaddr = (struct qvdevice *)tp->t_addr;
411 	if (QVCHAN(unit) == QVKEYBOARD )
412 		qvaddr->qv_csr &= ~QV_INT_ENABLE;
413 
414 	/*
415 	 * If unit is not the mouse channel call the line disc.
416 	 * otherwise clear the state flag, and put the keyboard into down/up.
417 	 */
418 	if (QVCHAN(unit) != QVMOUSECHAN) {
419 		(*linesw[tp->t_line].l_close)(tp);
420 		error = ttyclose(tp);
421 	} else {
422 		mouseon = 0;
423 		qv_init( qvaddr );
424 		error = 0;
425 	}
426 	tp->t_state = 0;
427 	return (error);
428 }
429 
430 qvread(dev, uio)
431 	dev_t dev;
432 	struct uio *uio;
433 {
434 	register struct tty *tp;
435 	int unit = minor( dev );
436 
437 	if (QVCHAN(unit) != QVMOUSECHAN) {
438 		tp = &qv_tty[unit];
439 		return ((*linesw[tp->t_line].l_read)(tp, uio));
440 	}
441 	return (ENXIO);
442 }
443 
444 qvwrite(dev, uio)
445 	dev_t dev;
446 	struct uio *uio;
447 {
448 	register struct tty *tp;
449 	int unit = minor( dev );
450 
451 	/*
452 	 * If this is the mouse we simply fake the i/o, otherwise
453 	 * we let the line disp. handle it.
454 	 */
455 	if (QVCHAN(unit) == QVMOUSECHAN) {
456 		uio->uio_offset = uio->uio_resid;
457 		uio->uio_resid = 0;
458 		return 0;
459 	}
460 	tp = &qv_tty[unit];
461 	return ((*linesw[tp->t_line].l_write)(tp, uio));
462 }
463 
464 
465 /*
466  * Mouse activity select routine
467  */
468 qvselect(dev, rw)
469 dev_t dev;
470 {
471 	register int s = spl5();
472 	register struct qv_info *qp = qv_scn;
473 
474 	if( QVCHAN(minor(dev)) == QVMOUSECHAN )
475 		switch(rw) {
476 		case FREAD:			/* if events okay */
477 			if(qp->ihead != qp->itail) {
478 				splx(s);
479 				return(1);
480 			}
481 			qvrsel = u.u_procp;
482 			splx(s);
483 			return(0);
484 		default:			/* can never write */
485 			splx(s);
486 			return(0);
487 		}
488 	else {
489 		splx(s);
490 		return( ttselect(dev, rw) );
491 	}
492 	/*NOTREACHED*/
493 }
494 
495 /*
496  * QVSS keyboard interrupt.
497  */
498 qvkint(qv)
499 	int qv;
500 {
501 	struct tty *tp;
502 	register c;
503 	struct uba_device *ui;
504 	register int key;
505 	register int i;
506 
507 	ui = qvinfo[qv];
508 	if (ui == 0 || ui->ui_alive == 0)
509 		return;
510 	tp = &qv_tty[qv<<2];
511 	/*
512 	 * Get a character from the keyboard.
513 	 */
514 	key = ((struct qvdevice *)ui->ui_addr)->qv_uartdata & 0xff;
515 	if( mouseon == 0) {
516 		/*
517 		 * Check for various keyboard errors
518 		 */
519 		if( key == LK_POWER_ERROR || key == LK_KDOWN_ERROR ||
520 		    key == LK_INPUT_ERROR || key == LK_OUTPUT_ERROR) {
521 			log(LOG_ERR,
522 			    "qv%d: Keyboard error, code = %x\n",qv,key);
523 			return;
524 		}
525 		if( key < LK_LOWEST ) return;
526 		/*
527 		 * See if its a state change key
528 		 */
529 		switch ( key ) {
530 		case LOCK:
531 			qv_keyboard.lock ^= 0xffff;	/* toggle */
532 			if( qv_keyboard.lock )
533 				qv_key_out( LK_LED_ENABLE );
534 			else
535 				qv_key_out( LK_LED_DISABLE );
536 			qv_key_out( LED_3 );
537 			return;
538 		case SHIFT:
539 			qv_keyboard.shift ^= 0xffff;
540 			return;
541 		case CNTRL:
542 			qv_keyboard.cntrl ^= 0xffff;
543 			return;
544 		case ALLUP:
545 			qv_keyboard.cntrl = qv_keyboard.shift = 0;
546 			return;
547 		case REPEAT:
548 			c = qv_keyboard.last;
549 			break;
550 		default:
551 		/*
552 		 * Test for control characters. If set, see if the character
553 		 * is elligible to become a control character.
554 		 */
555 			if( qv_keyboard.cntrl ) {
556 				c = q_key[ key ];
557 				if( c >= ' ' && c <= '~' )
558 					c &= 0x1f;
559 			} else if( qv_keyboard.lock || qv_keyboard.shift )
560 				c = q_shift_key[ key ];
561 				else
562 				c = q_key[ key ];
563 			break;
564 		}
565 
566 		qv_keyboard.last = c;
567 
568 		/*
569 		 * Check for special function keys
570 		 */
571 		if( c & 0x80 ) {
572 			register char *string;
573 			string = q_special[ c & 0x7f ];
574 			while( *string )
575 			(*linesw[tp->t_line].l_rint)(*string++, tp);
576 		} else
577 			(*linesw[tp->t_line].l_rint)(c, tp);
578 	} else {
579 		/*
580 		 * Mouse channel is open put it into the event queue
581 		 * instead.
582 		 */
583 		register struct qv_info *qp = qv_scn;
584 		register vsEvent *vep;
585 
586 		if ((i = EVROUND(qp->itail+1)) == qp->ihead)
587 			return;
588 		vep = &qp->ibuff[qp->itail];
589 		vep->vse_direction = VSE_KBTRAW;
590 		vep->vse_type = VSE_BUTTON;
591 		vep->vse_device = VSE_DKB;
592 		vep->vse_x = qp->mouse.x;
593 		vep->vse_y = qp->mouse.y;
594 		vep->vse_time = TOY;
595 		vep->vse_key = key;
596 		qp->itail = i;
597 		if(qvrsel) {
598 			selwakeup(qvrsel,0);
599 			qvrsel = 0;
600 		}
601 	}
602 }
603 
604 /*
605  * Ioctl for QVSS.
606  */
607 /*ARGSUSED*/
608 qvioctl(dev, cmd, data, flag)
609 	dev_t dev;
610 	register caddr_t data;
611 {
612 	register struct tty *tp;
613 	register int unit = minor(dev);
614 	register struct qv_info *qp = qv_scn;
615 	register struct qv_kpcmd *qk;
616 	register unsigned char *cp;
617 	int error;
618 
619 	/*
620 	 * Check for and process qvss specific ioctl's
621 	 */
622 	switch( cmd ) {
623 	case QIOCGINFO:					/* return screen info */
624 		bcopy((caddr_t)qp, data, sizeof (struct qv_info));
625 		break;
626 
627 	case QIOCSMSTATE:				/* set mouse state */
628 		qp->mouse = *((vsCursor *)data);
629 		qv_pos_cur( qp->mouse.x, qp->mouse.y );
630 		break;
631 
632 	case QIOCINIT:					/* init screen	*/
633 		qv_init( qp->qvaddr );
634 		break;
635 
636 	case QIOCKPCMD:
637 		qk = (struct qv_kpcmd *)data;
638 		if(qk->nbytes == 0) qk->cmd |= 0200;
639 		if(mouseon == 0) qk->cmd |= 1;	/* no mode changes */
640 		qv_key_out(qk->cmd);
641 		cp = &qk->par[0];
642 		while(qk->nbytes-- > 0) {	/* terminate parameters */
643 			if(qk->nbytes <= 0) *cp |= 0200;
644 			qv_key_out(*cp++);
645 		}
646 		break;
647 	case QIOCADDR:					/* get struct addr */
648 		*(struct qv_info **) data = qp;
649 		break;
650 	default:					/* not ours ??  */
651 		tp = &qv_tty[unit];
652 		error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
653 		if (error >= 0)
654 			return (error);
655 		error = ttioctl(tp, cmd, data, flag);
656 		if (error >= 0) {
657 			return (error);
658 		}
659 		break;
660 	}
661 	return (0);
662 }
663 /*
664  * Initialize the screen and the scanmap
665  */
666 qv_init(qvaddr)
667 struct qvdevice *qvaddr;
668 {
669 	register short *scanline;
670 	register int i;
671 	register short scan;
672 	register char *ptr;
673 	register struct qv_info *qp = qv_scn;
674 
675 	/*
676 	 * Clear the bit map
677 	 */
678 	for( i=0 , ptr = qp->bitmap ; i<240 ; i += 2 , ptr += 2048)
679 		bzero( ptr, 2048 );
680 	/*
681 	 * Reinitialize the scanmap
682 	 */
683         scan = qvaddr->qv_csr & QV_MEM_BANK;
684         scanline = qp->scanmap;
685         for(i = 0 ; i < qp->max_y ; i++ )
686                 *scanline++ = scan++;
687 
688 	/*
689 	 * Home the cursor
690 	 */
691 	qp->row = qp->col = 0;
692 
693 	/*
694 	 * Reset the cursor to the default type.
695 	 */
696 	for( i=0 ; i<16 ; i++ )
697 		qp->cursorbits[i] = q_cursor[i];
698 	qvaddr->qv_csr |= QV_CUR_MODE;
699 	/*
700 	 * Reset keyboard to default state.
701 	 */
702 	qvkbdreset();
703 }
704 
705 qvreset()
706 {
707 }
708 qvkbdreset()
709 {
710 	register int i;
711 	qv_key_out(LK_DEFAULTS);
712 	for( i=1 ; i < 15 ; i++ )
713 		qv_key_out( divdefaults[i] | (i<<3));
714 	for (i = 0; i < KBD_INIT_LENGTH; i++)
715 		qv_key_out(kbdinitstring[i]);
716 }
717 
718 #define abs(x) (((x) > 0) ? (x) : (-(x)))
719 /*
720  * QVSS vertical sync interrupt
721  */
722 qvvint(qv)
723 	int qv;
724 {
725 	extern int selwait;
726 	register struct qvdevice *qvaddr;
727 	struct uba_device *ui;
728 	register struct qv_info *qp = qv_scn;
729 	int unit;
730 	struct tty *tp0;
731 	int i;
732 	register int j;
733 	/*
734 	 * Mouse state info
735 	 */
736 	static ushort omouse = 0, nmouse = 0;
737 	static char omx=0, omy=0, mx=0, my=0, om_switch=0, m_switch=0;
738 	register int dx, dy;
739 
740 	/*
741 	 * Test and set the qv_ipl_lo flag. If the result is not zero then
742 	 * someone else must have already gotten here.
743 	 */
744 	if( --qv_ipl_lo )
745 		return;
746 	(void)spl4();
747 	ui = qvinfo[qv];
748 	unit = qv<<2;
749 	qvaddr = (struct qvdevice *)ui->ui_addr;
750 	tp0 = &qv_tty[QVCHAN(unit) + QVMOUSECHAN];
751 	/*
752 	 * See if the mouse has moved.
753 	 */
754 	if( omouse != (nmouse = qvaddr->qv_mouse) ) {
755 		omouse = nmouse;
756 		mx = nmouse & 0xff;
757 		my = nmouse >> 8;
758 		dy = my - omy; omy = my;
759 		dx = mx - omx; omx = mx;
760 		if( dy < 50 && dy > -50 && dx < 50 && dx > -50 ) {
761 			register vsEvent *vep;
762 			if( qp->mscale < 0 ) {	/* Ray Lanza's original */
763 				if( dy < 0 )
764 					dy = -( dy * dy );
765 				else
766 					dy *= dy;
767 				if( dx < 0 )
768 					dx = -( dx * dx );
769 				else
770 					dx *= dx;
771 			}
772 			else {			/* Vs100 style, see WGA spec */
773 			    int thresh = qp->mthreshold;
774 			    int scale  = qp->mscale;
775 			    if( abs(dx) > thresh ) {
776 				if ( dx < 0 )
777 				    dx = (dx + thresh)*scale - thresh;
778 				else
779 				    dx = (dx - thresh)*scale + thresh;
780 			    }
781 			    if( abs(dy) > thresh ) {
782 				if ( dy < 0 )
783 				    dy = (dy + thresh)*scale - thresh;
784 				else
785 				    dy = (dy - thresh)*scale + thresh;
786 			    }
787 			}
788 			qp->mouse.x += dx;
789 			qp->mouse.y -= dy;
790 			if( qp->mouse.x < 0 )
791 				qp->mouse.x = 0;
792 			if( qp->mouse.y < 0 )
793 				qp->mouse.y = 0;
794 			if( qp->mouse.x > qp->max_cur_x )
795 				qp->mouse.x = qp->max_cur_x;
796 			if( qp->mouse.y > qp->max_cur_y )
797 				qp->mouse.y = qp->max_cur_y;
798 			if( tp0->t_state & TS_ISOPEN )
799 				qv_pos_cur( qp->mouse.x, qp->mouse.y );
800 			if (qp->mouse.y < qp->mbox.bottom &&
801 			    qp->mouse.y >=  qp->mbox.top &&
802 			    qp->mouse.x < qp->mbox.right &&
803 			    qp->mouse.x >=  qp->mbox.left) goto switches;
804 			qp->mbox.bottom = 0;	/* trash box */
805 			if (EVROUND(qp->itail+1) == qp->ihead)
806 				goto switches;
807 			i = EVROUND(qp->itail - 1);
808 			if ((qp->itail != qp->ihead) &&	(i != qp->ihead)) {
809 				vep = & qp->ibuff[i];
810 				if(vep->vse_type == VSE_MMOTION) {
811 					vep->vse_x = qp->mouse.x;
812 					vep->vse_y = qp->mouse.y;
813 					goto switches;
814 				}
815 			}
816 			/* put event into queue and do select */
817 			vep = & qp->ibuff[qp->itail];
818 			vep->vse_type = VSE_MMOTION;
819 			vep->vse_time = TOY;
820 			vep->vse_x = qp->mouse.x;
821 			vep->vse_y = qp->mouse.y;
822 			qp->itail = EVROUND(qp->itail+1);
823 		}
824 	}
825 	/*
826 	 * See if mouse switches have changed.
827 	 */
828 switches:if( om_switch != ( m_switch = (qvaddr->qv_csr & QV_MOUSE_ANY) >> 8 ) ) {
829 		qp->mswitches = ~m_switch & 0x7;
830 		for (j = 0; j < 3; j++) {	/* check each switch */
831 			register vsEvent *vep;
832 			if ( ((om_switch>>j) & 1) == ((m_switch>>j) & 1) )
833 				continue;
834 			/* check for room in the queue */
835 			if ((i = EVROUND(qp->itail+1)) == qp->ihead) return;
836 			/* put event into queue and do select */
837 			vep = &qp->ibuff[qp->itail];
838 			vep->vse_type = VSE_BUTTON;
839 			vep->vse_key = 2 - j;
840 			vep->vse_direction = VSE_KBTDOWN;
841 			if ( (m_switch >> j) & 1)
842 				vep->vse_direction = VSE_KBTUP;
843 			vep->vse_device = VSE_MOUSE;
844 			vep->vse_time = TOY;
845 			vep->vse_x = qp->mouse.x;
846 			vep->vse_y = qp->mouse.y;
847 		}
848 		qp->itail =  i;
849 		om_switch = m_switch;
850 		qp->mswitches = m_switch;
851 	}
852 	/* if we have proc waiting, and event has happened, wake him up */
853 	if(qvrsel && (qp->ihead != qp->itail)) {
854 		selwakeup(qvrsel,0);
855 		qvrsel = 0;
856 	}
857 	/*
858 	 * Okay we can take another hit now
859 	 */
860 	qv_ipl_lo = 1;
861 }
862 
863 /*
864  * Start  transmission
865  */
866 qvstart(tp)
867 	register struct tty *tp;
868 {
869 	register int unit, c;
870 	register struct tty *tp0;
871 	int s;
872 
873 	unit = minor(tp->t_dev);
874 #ifdef CONS_HACK
875 	tp0 = &qv_tty[(unit&0xfc)+QVPCONS];
876 #endif
877 	unit = QVCHAN(unit);
878 
879 	s = spl5();
880 	/*
881 	 * If it's currently active, or delaying, no need to do anything.
882 	 */
883 	if (tp->t_state&(TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
884 		goto out;
885 	/*
886 	 * Display chars until the queue is empty, if the second subchannel
887 	 * is open direct them there. Drop characters from subchannels other
888 	 * than 0 on the floor.
889 	 */
890 
891 	while( tp->t_outq.c_cc ) {
892 		c = getc(&tp->t_outq);
893 		if (unit == QVKEYBOARD)
894 #ifdef CONS_HACK
895 			if( tp0->t_state & TS_ISOPEN ){
896 				(*linesw[tp0->t_line].l_rint)(c, tp0);
897 			} else
898 #endif
899 				qvputchar( c & 0xff );
900 	}
901 	/*
902 	 * Position the cursor to the next character location.
903 	 */
904 	qv_pos_cur( qv_scn->col*8, qv_scn->row*15 );
905 
906 	/*
907 	 * If there are sleepers, and output has drained below low
908 	 * water mark, wake up the sleepers.
909 	 */
910 	if ( tp->t_outq.c_cc<= tp->t_lowat ) {
911 		if (tp->t_state&TS_ASLEEP){
912 			tp->t_state &= ~TS_ASLEEP;
913 			wakeup((caddr_t)&tp->t_outq);
914 		}
915 	}
916 	tp->t_state &= ~TS_BUSY;
917 out:
918 	splx(s);
919 }
920 
921 /*
922  * Stop output on a line, e.g. for ^S/^Q or output flush.
923  */
924 /*ARGSUSED*/
925 qvstop(tp, flag)
926 	register struct tty *tp;
927 {
928 	register int s;
929 
930 	/*
931 	 * Block input/output interrupts while messing with state.
932 	 */
933 	s = spl5();
934 	if (tp->t_state & TS_BUSY) {
935 		if ((tp->t_state&TS_TTSTOP)==0) {
936 			tp->t_state |= TS_FLUSH;
937 		} else
938 			tp->t_state &= ~TS_BUSY;
939 	}
940 	splx(s);
941 }
942 
943 qvputc(c)
944 char c;
945 {
946 	qvputchar(c);
947 	if (c == '\n')
948 		qvputchar('\r');
949 }
950 
951 /*
952  * Routine to display a character on the screen.  The model used is a
953  * glass tty.  It is assummed that the user will only use this emulation
954  * during system boot and that the screen will be eventually controlled
955  * by a window manager.
956  *
957  */
958 qvputchar( c )
959 register char c;
960 {
961 
962 	register char *b_row, *f_row;
963 	register int i;
964 	register short *scanline;
965 	register int ote = 128;
966 	register struct qv_info *qp = qv_scn;
967 
968 	/*
969 	 * This routine may be called in physical mode by the dump code
970 	 * so we check and punt if that's the case.
971 	 */
972 	if( (mfpr(MAPEN) & 1) == 0 )
973 		return;
974 
975 	c &= 0x7f;
976 
977 	switch ( c ) {
978 	case '\t':				/* tab		*/
979 		for( i = 8 - (qp->col & 0x7) ; i > 0 ; i-- )
980 			qvputchar( ' ' );
981 		break;
982 
983 	case '\r':				/* return	*/
984 		qp->col = 0;
985 		break;
986 
987 	case '\010':				/* backspace	*/
988 		if( --qp->col < 0 )
989 			qp->col = 0;
990 		break;
991 
992 	case '\n':				/* linefeed	*/
993 		if( qp->row+1 >= qp->max_row )
994 			qvscroll();
995 		else
996 			qp->row++;
997 		/*
998 		* Position the cursor to the next character location.
999 		*/
1000 		qv_pos_cur( qp->col*8, qp->row*15 );
1001 		break;
1002 
1003 	case '\007':				/* bell		*/
1004                 /*
1005                  * We don't do anything to the keyboard until after
1006                  * autoconfigure.
1007                  */
1008 		if( qp->qvaddr )
1009 			qv_key_out( LK_RING_BELL );
1010 		return;
1011 
1012 	default:
1013 		if( c >= ' ' && c <= '~' ) {
1014                         scanline = qp->scanmap;
1015                         b_row = qp->bitmap+(scanline[qp->row*15]&0x3ff)*128+qp->col;
1016 			i = c - ' ';
1017 			if( i < 0 || i > 95 )
1018 				i = 0;
1019 			else
1020 				i *= 15;
1021 			f_row = (char *)((int)q_font + i);
1022 
1023 /*			for( i=0 ; i<15 ; i++ , b_row += 128, f_row++ )
1024 				*b_row = *f_row;*/
1025 			/* inline expansion for speed */
1026 			*b_row = *f_row++; b_row += ote;
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 
1042 			if( ++qp->col >= qp->max_col ) {
1043 				qp->col = 0 ;
1044 				if( qp->row+1 >= qp->max_row )
1045 					qvscroll();
1046 				else
1047 					qp->row++;
1048 			}
1049 		}
1050 		break;
1051 	}
1052 }
1053 
1054 /*
1055  * Position the cursor to a particular spot.
1056  */
1057 qv_pos_cur( x, y)
1058 register int x,y;
1059 {
1060 	register struct qvdevice *qvaddr;
1061 	register struct qv_info *qp = qv_scn;
1062 	register index;
1063 
1064 	if( qvaddr = qp->qvaddr ) {
1065 		if( y < 0 || y > qp->max_cur_y )
1066 			y = qp->max_cur_y;
1067 		if( x < 0 || x > qp->max_cur_x )
1068 			x = qp->max_cur_x;
1069 		qp->cursor.x = x;		/* keep track of real cursor*/
1070 		qp->cursor.y = y;		/* position, indep. of mouse*/
1071 
1072 		qvaddr->qv_crtaddr = 10;	/* select cursor start reg */
1073 		qvaddr->qv_crtdata = y & 0xf;
1074 		qvaddr->qv_crtaddr = 11;	/* select cursor end reg */
1075 		qvaddr->qv_crtdata = y & 0xf;
1076 		qvaddr->qv_crtaddr = 14;	/* select cursor y pos. */
1077 		qvaddr->qv_crtdata = y >> 4;
1078 		qvaddr->qv_xcur = x;		/* pos x axis	*/
1079 		/*
1080 		 * If the mouse is being used then we change the mode of
1081 		 * cursor display based on the pixels under the cursor
1082 		 */
1083 		if( mouseon ) {
1084 			index = y*128 + x/8;
1085 			if( qp->bitmap[ index ] && qp->bitmap[ index+128 ] )
1086 				qvaddr->qv_csr &= ~QV_CUR_MODE;
1087 			else
1088 				qvaddr->qv_csr |=  QV_CUR_MODE;
1089 		}
1090 	}
1091 }
1092 /*
1093  * Scroll the bitmap by moving the scanline map words. This could
1094  * be done by moving the bitmap but it's much too slow for a full screen.
1095  * The only drawback is that the scanline map must be reset when the user
1096  * wants to do graphics.
1097  */
1098 qvscroll()
1099 {
1100 	short tmpscanlines[15];
1101 	register char *b_row;
1102 	register short *scanline;
1103 	register struct qv_info *qp = qv_scn;
1104 
1105 	/*
1106 	 * If the mouse is on we don't scroll so that the bit map
1107 	 * remains sane.
1108 	 */
1109 	if( mouseon ) {
1110 		qp->row = 0;
1111 		return;
1112 	}
1113 	/*
1114 	 * Save the first 15 scanlines so that we can put them at
1115 	 * the bottom when done.
1116 	 */
1117 	bcopy((caddr_t)qp->scanmap, (caddr_t)tmpscanlines, sizeof tmpscanlines);
1118 
1119 	/*
1120 	 * Clear the wrapping line so that it won't flash on the bottom
1121 	 * of the screen.
1122 	 */
1123         scanline = qp->scanmap;
1124         b_row = qp->bitmap+(*scanline&0x3ff)*128;
1125 	bzero( b_row, 1920 );
1126 
1127 	/*
1128 	 * Now move the scanlines down
1129 	 */
1130 	bcopy((caddr_t)(qp->scanmap+15), (caddr_t)qp->scanmap,
1131 	      (qp->row * 15) * sizeof (short) );
1132 
1133 	/*
1134 	 * Now put the other lines back
1135 	 */
1136 	bcopy((caddr_t)tmpscanlines, (caddr_t)(qp->scanmap+(qp->row * 15)),
1137 	      sizeof (tmpscanlines) );
1138 
1139 }
1140 
1141 /*
1142  * Output to the keyboard. This routine status polls the transmitter on the
1143  * keyboard to output a code. The timer is to avoid hanging on a bad device.
1144  */
1145 qv_key_out(c)
1146 	u_short c;
1147 {
1148 	int timer = 30000;
1149 	register struct qv_info *qp = qv_scn;
1150 
1151 	if (qp->qvaddr) {
1152 		while ((qp->qvaddr->qv_uartstatus & 0x4) == 0  && timer--)
1153 			;
1154 		qp->qvaddr->qv_uartdata = c;
1155 	}
1156 }
1157 /*
1158  * Virtual console initialization. This routine sets up the qvss so that it can
1159  * be used as the system console. It is invoked before autoconfig and has to do
1160  * everything necessary to allow the device to serve as the system console.
1161  * In this case it must map the q-bus and device areas and initialize the qvss
1162  * screen.
1163  */
1164 qvcons_init()
1165 {
1166         struct percpu *pcpu;            /* pointer to percpu structure  */
1167 	register struct qbus *qb;
1168         struct qvdevice *qvaddr;        /* device pointer               */
1169         short *devptr;                  /* virtual device space         */
1170 	extern cnputc();		/* standard serial console putc */
1171 #define QVSSCSR 017200
1172 
1173 	/*
1174 	 * If secondary console already configured,
1175 	 * don't override the previous one.
1176 	 */
1177 	if (v_putc != cnputc)
1178 		return 0;
1179         /*
1180          * find the percpu entry that matches this machine.
1181          */
1182         for( pcpu = percpu ; pcpu && pcpu->pc_cputype != cpu ; pcpu++ )
1183                 ;
1184         if( pcpu == NULL )
1185                 return 0;
1186 	if (pcpu->pc_io->io_type != IO_QBUS)
1187 		return 0;
1188 
1189         /*
1190          * Found an entry for this cpu. Because this device is Microvax specific
1191          * we assume that there is a single q-bus and don't have to worry about
1192          * multiple adapters.
1193          *
1194          * Map the device registers.
1195          */
1196 	qb = (struct qbus *)pcpu->pc_io->io_details;
1197 	ioaccess(qb->qb_iopage, UMEMmap[0] + qb->qb_memsize, UBAIOPAGES * NBPG);
1198 
1199         /*
1200          * See if the qvss is there.
1201          */
1202         devptr = (short *)((char *)umem[0] + (qb->qb_memsize * NBPG));
1203         qvaddr = (struct qvdevice *)((u_int)devptr + ubdevreg(QVSSCSR));
1204         if (badaddr((caddr_t)qvaddr, sizeof(short)))
1205                 return 0;
1206         /*
1207          * Okay the device is there lets set it up
1208          */
1209         if (!qv_setup(qvaddr, 0, 0))
1210 		return 0;
1211 	v_putc = qvputc;
1212         consops = &cdevsw[QVSSMAJOR];
1213 	return 1;
1214 }
1215 /*
1216  * Do the board specific setup
1217  */
1218 qv_setup(qvaddr, unit, probed)
1219 struct qvdevice *qvaddr;
1220 int unit;
1221 int probed;
1222 {
1223         caddr_t qvssmem;		/* pointer to the display mem   */
1224         register i;			/* simple index                 */
1225 	register struct qv_info *qp;
1226         register int *pte;
1227         struct percpu *pcpu;            /* pointer to percpu structure  */
1228 	register struct qbus *qb;
1229 
1230         /*
1231          * find the percpu entry that matches this machine.
1232          */
1233         for( pcpu = percpu ; pcpu && pcpu->pc_cputype != cpu ; pcpu++ )
1234                 ;
1235         if( pcpu == NULL )
1236                 return(0);
1237 
1238         /*
1239          * Found an entry for this cpu. Because this device is Microvax specific
1240          * we assume that there is a single q-bus and don't have to worry about
1241          * multiple adapters.
1242          *
1243          * Map the device memory.
1244          */
1245 	qb = (struct qbus *)pcpu->pc_io->io_details;
1246 
1247         i = (u_int)(qvaddr->qv_csr & QV_MEM_BANK) << 7;
1248 	ioaccess(qb->qb_maddr + i, QVmap[unit], 512 * NBPG);
1249 	qvssmem = qvmem[unit];
1250         pte = (int *)(QVmap[unit]);
1251         for (i=0; i < 512; i++, pte++)
1252                 *pte = (*pte & ~PG_PROT) | PG_UW | PG_V;
1253 
1254         qv_scn = (struct qv_info *)((u_int)qvssmem + 251*1024);
1255 	qp = qv_scn;
1256         if( (qvaddr->qv_csr & QV_19INCH) && qv_def_scrn == 0)
1257                 qv_def_scrn = 1;
1258         *qv_scn = qv_scn_defaults[ qv_def_scrn ];
1259 	if (probed)
1260 		qp->qvaddr = qvaddr;
1261  	qp->bitmap = qvssmem;
1262         qp->scanmap = (short *)((u_int)qvssmem + 254*1024);
1263         qp->cursorbits = (short *)((u_int)qvssmem + 256*1024-32);
1264 	/* set up event queue for later */
1265 	qp->ibuff = (vsEvent *)qp - QVMAXEVQ;
1266 	qp->iqsize = QVMAXEVQ;
1267 	qp->ihead = qp->itail = 0;
1268 
1269         /*
1270          * Setup the crt controller chip.
1271          */
1272         for( i=0 ; i<16 ; i++ ) {
1273                 qvaddr->qv_crtaddr = i;
1274                 qvaddr->qv_crtdata = qv_crt_parms[ qv_def_scrn ][ i ];
1275         }
1276         /*
1277          * Setup the display.
1278          */
1279         qv_init( qvaddr );
1280 
1281         /*
1282          * Turn on the video
1283          */
1284         qvaddr->qv_csr |= QV_VIDEO_ENA ;
1285 	return 1;
1286 }
1287 #endif
1288