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