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