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