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