summaryrefslogtreecommitdiff
path: root/chips/serial_console.c
blob: 7b15dd4cf199496483540cf2d9be23bd2a537a80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/* 
 * Mach Operating System
 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
 * All Rights Reserved.
 * 
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 * 
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 * 
 * Carnegie Mellon requests users of this software to return to
 * 
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 * 
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */
/*
 *	File: serial_console.c
 * 	Author: Alessandro Forin, Carnegie Mellon University
 *	Date:	7/91
 *
 *	Console driver for serial-line based consoles.
 */

#include <constty.h>
#if	NCONSTTY > 0
#include <bm.h>
#include <platforms.h>

#include <mach_kdb.h>

#include <machine/machspl.h>		/* spl definitions */
#include <device/io_req.h>
#include <device/tty.h>
#include <sys/syslog.h>

#include <chips/busses.h>
#include <chips/screen_defs.h>
#include <chips/serial_defs.h>

#ifdef	DECSTATION
#include <mips/prom_interface.h>
#define	find_rconsole(p)	dec_check_rcline(p)
#define	CONSOLE_SERIAL_LINE_NO	3
#endif	/*DECSTATION*/

#ifdef	VAXSTATION
#define	find_rconsole(p)
#define cnputc ser_putc
#define cngetc ser_getc
#define cnpollc ser_pollc
#define cnmaygetc ser_maygetc
#define	CONSOLE_SERIAL_LINE_NO	3
#endif	/*VAXSTATION*/

#ifdef	FLAMINGO
#define	CONSOLE_SERIAL_LINE_NO	3
#endif

#ifndef	CONSOLE_SERIAL_LINE_NO
#define	CONSOLE_SERIAL_LINE_NO	0
#endif

/* Size this as max possible number of lines in any serial chip we might use */
static struct tty 	console_tty_data[NCONSTTY];
struct tty		*console_tty[NCONSTTY];	/* exported */

#define	DEFAULT_SPEED	B9600
#define	DEFAULT_FLAGS	(TF_EVENP|TF_ODDP|TF_ECHO)


/*
 * A machine MUST have a console.  In our case
 * things are a little complicated by the graphic
 * display: people expect it to be their "console",
 * but we'd like to be able to live without it.
 * This is not to be confused with the "rconsole" thing:
 * that just duplicates the console I/O to
 * another place (for debugging/logging purposes).
 *
 * There is then another historical kludge: if
 * there is a graphic display it is assumed that
 * the minor "1" is the mouse, with some more
 * magic attached to it.  And again, one might like to
 * use the serial line 1 as a regular one.
 *
 */
#define	user_console	0

int	console = 0;

int	(*console_probe)() = 0,
	(*console_param)() = 0,
	(*console_start)() = 0,
	(*console_putc)() = 0,
	(*console_getc)() = 0,
	(*console_pollc)() = 0,
	(*console_mctl)() = 0,
	(*console_softCAR)() = 0;

/*
 * Lower-level (internal) interfaces, for printf and gets
 */
int cnunit = 0;		/* which unit owns the 'console' */
int cnline = 0;		/* which line of that unit */
int rcline = 3;		/* alternate, "remote console" line */

rcoff()
{
	spl_t s = splhigh();
	cnpollc(FALSE);
	rcline = 0;
	cnpollc(TRUE);
	splx(s);
}

rcputc(c)
{
	if (rcline)
		(*console_putc)( cnunit, rcline, c);
}

cnputc(c)
{
#if	NBM > 0
	if (SCREEN_ISA_CONSOLE()) {
		/* this does its own rcputc */
		screen_blitc(SCREEN_CONS_UNIT(), c);
	} else
#endif	NBM > 0
	       {
		rcputc(c);
		(*console_putc)( cnunit, cnline, c);/* insist on a console still */
	}
	if (c == '\n')
		cnputc('\r');
}

cngetc()
{
	return (*console_getc)( cnunit, cnline, TRUE, FALSE);
}

cnpollc(bool)
{
	(*console_pollc)(cnunit, bool);
}

 
/* Debugger support */
cnmaygetc()
{
	return (*console_getc)( cnunit, cnline, FALSE, FALSE);
}


#if	NBM > 0
boolean_t
screen_captures(line)
	register int	line;
{
	return (SCREEN_ISA_CONSOLE() &&
		((line == SCREEN_LINE_KEYBOARD) ||
		 (line == SCREEN_LINE_POINTER)));
}
#endif

/*
 * Higher level (external) interface, for GP use
 */


/*
 * This is basically a special form of autoconf,
 * to get printf() going before true autoconf.
 */
cons_find(tube)
	boolean_t tube;
{
	static struct bus_device d;
	register int		 i;
	struct tty		*tp;

	for (i = 0; i < NCONSTTY; i++)
		console_tty[i] = &console_tty_data[i];
	/* the hardware device will set tp->t_addr for valid ttys */

	d.unit = 0;

	if ((console_probe == 0) ||
	    ((*console_probe)(0, &d) == 0)) {
		/* we have no console, but maybe that's ok */
#if	defined(DECSTATION) || defined(FLAMINGO)
		/* no, it is not */
		dprintf("%s", "no console!\n");
		halt();
#endif
		return 0;
	}

	/*
	 * Remote console line
	 */
	find_rconsole(&rcline);

	/*
	 * Console always on unit 0. Fix if you need to
	 */
	cnunit = 0;

#if	NBM > 0
	if (tube && screen_probe(0)) {

		/* associate screen to console iff */
		if (console == user_console)
			screen_console = cnunit | SCREEN_CONS_ENBL;
		cnline = SCREEN_LINE_KEYBOARD;

		/* mouse and keybd */
		tp = console_tty[SCREEN_LINE_KEYBOARD];
		tp->t_ispeed = B4800;
		tp->t_ospeed = B4800;
		tp->t_flags = TF_LITOUT|TF_EVENP|TF_ECHO|TF_XTABS|TF_CRMOD;
		tp->t_dev = SCREEN_LINE_KEYBOARD;
		(*console_param)(tp, SCREEN_LINE_KEYBOARD);

		tp = console_tty[SCREEN_LINE_POINTER];
		tp->t_ispeed = B4800;
		tp->t_ospeed = B4800;
		tp->t_flags = TF_LITOUT|TF_ODDP;
		tp->t_dev = SCREEN_LINE_POINTER;
		(*console_param)(tp, SCREEN_LINE_POINTER);
		/* console_scan will turn on carrier */

	} else {
#endif	NBM > 0
		/* use non-graphic console as console */
		cnline = CONSOLE_SERIAL_LINE_NO;

		tp = console_tty[cnline];
		tp->t_ispeed = B9600;
		tp->t_ospeed = B9600;
		tp->t_flags = TF_LITOUT|TF_EVENP|TF_ECHO|TF_XTABS|TF_CRMOD;
		(*console_softCAR)(cnunit, cnline, TRUE);
		console = cnline;
		tp->t_dev = console;
		(*console_param)(tp, SCREEN_LINE_OTHER);
#if	NBM > 0
	}

	/*
	 * Enable rconsole interrupts for KDB
	 */
	if (tube && rcline != cnline) {
		tp = console_tty[rcline];
		tp->t_ispeed = B9600;
		tp->t_ospeed = B9600;
		tp->t_flags = TF_LITOUT|TF_EVENP|TF_ECHO|TF_XTABS|TF_CRMOD;
		tp->t_dev = rcline;
		(*console_softCAR)(cnunit, rcline, TRUE);
		(*console_param)(tp, SCREEN_LINE_OTHER);
	} else
	 	rcline = 0;
#endif	NBM > 0
}

/*
 * Open routine
 */
extern int
	cons_start(struct tty *),
	cons_stop(struct tty *, int),
	cons_mctl(struct tty *, int, int);

cons_open(dev, flag, ior)
	int	dev;
	int	flag;
	io_req_t ior;
{
	register struct tty	*tp;
	register int		ttyno;
 
	if (dev == user_console)
		dev = console;

	ttyno = dev;
	if (ttyno >= NCONSTTY)
		return D_NO_SUCH_DEVICE;
	tp = console_tty[ttyno];

	/* But was it there at probe time */
	if (tp->t_addr == 0)
		return D_NO_SUCH_DEVICE;

	tp->t_start	= cons_start;
	tp->t_stop	= cons_stop;
	tp->t_mctl	= cons_mctl;

#if	NBM > 0
	if (screen_captures(ttyno))
		screen_open(SCREEN_CONS_UNIT(), ttyno==SCREEN_LINE_KEYBOARD);
#endif	NBM > 0

	if ((tp->t_state & TS_ISOPEN) == 0) {
		if (tp->t_ispeed == 0) {
			tp->t_ispeed = DEFAULT_SPEED;
			tp->t_ospeed = DEFAULT_SPEED;
			tp->t_flags = DEFAULT_FLAGS;
		}
		tp->t_dev = dev;
		(*console_param)(tp, ttyno);
	}

	return (char_open(dev, tp, flag, ior));
}


/*
 * Close routine
 */
cons_close(dev, flag)
	int dev;
{
	register struct tty	*tp;
	register int		ttyno;
	spl_t s;
 
	if (dev == user_console)
		dev = console;

	ttyno = dev;

#if	NBM > 0
	if (screen_captures(ttyno))
		screen_close(SCREEN_CONS_UNIT(), ttyno==SCREEN_LINE_KEYBOARD);
#endif	NBM > 0

	tp = console_tty[ttyno];

	s = spltty();
	simple_lock(&tp->t_lock);

	ttyclose(tp);

	simple_unlock(&tp->t_lock);
	splx(s);
}

cons_read(dev, ior)
	int			dev;
	register io_req_t	ior;
{
	register struct tty	*tp;
	register 		ttyno;
 
	if (dev == user_console)
		dev = console;

	ttyno = dev;
#if	NBM > 0
	if (SCREEN_ISA_CONSOLE() && (ttyno == SCREEN_LINE_POINTER))
		return screen_read(SCREEN_CONS_UNIT(), ior);
#endif	NBM > 0

	tp = console_tty[ttyno];
	return char_read(tp, ior);
}
 

cons_write(dev, ior)
	int			dev;
	register io_req_t	ior;
{
	register struct tty	*tp;
	register		ttyno;

	if (dev == user_console)
		dev = console;

	ttyno = dev;
#if	NBM > 0
	if (screen_captures(ttyno))
		return screen_write(SCREEN_CONS_UNIT(), ior);
#endif	NBM > 0

	tp = console_tty[ttyno];
	return char_write(tp, ior);
}

/*
 * Start output on a line
 */
cons_start(tp)
	register struct tty *tp;
{
	spl_t			s;
 
	s = spltty();
	if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
		goto out;

	if (tp->t_outq.c_cc == 0)
		goto out;

	tp->t_state |= TS_BUSY;

	(*console_start)(tp);

out:
	splx(s);
}

/*
 * Stop output on a line.
 */
cons_stop(tp, flag)
	register struct tty *tp;
{
	register spl_t s;

	s = spltty();
	if (tp->t_state & TS_BUSY) {
		if ((tp->t_state&TS_TTSTOP)==0)
			tp->t_state |= TS_FLUSH;
	}
	splx(s);
}
 

/*
 * Modem control
 */
cons_mctl(
	struct tty	*tp,
	int		bits,
	int		how)
{
	return (*console_mctl)(tp->t_dev, bits, how);
}

/*
 * Abnormal close
 */
cons_portdeath(dev, port)
	int	dev;
	mach_port_t port;
{
	if (dev == user_console)
		dev = console;
	return (tty_portdeath(console_tty[dev], port));
}

/*
 * Get/Set status rotuines
 */
io_return_t
cons_get_status(dev, flavor, data, status_count)
	int		dev;
	dev_flavor_t	flavor;
	int *		data;		/* pointer to OUT array */
	unsigned int	*status_count;		/* out */
{
	register struct tty *tp;
	register int	ttyno;

	if (dev == user_console)
		dev = console;

	ttyno = dev;

#if	NBM > 0
	if (screen_captures(ttyno) &&
	    (screen_get_status(SCREEN_CONS_UNIT(),
			flavor, data, status_count) == D_SUCCESS))
		return D_SUCCESS;
#endif	NBM > 0

	tp = console_tty[ttyno];

	switch (flavor) {
	    case TTY_MODEM:
		/* Take all bits */
		*data = (*console_mctl)(dev, -1, DMGET);
		*status_count = 1;
		break;
	    default:
		return (tty_get_status(tp, flavor, data, status_count));
	}
	return (D_SUCCESS);
}

io_return_t
cons_set_status(dev, flavor, data, status_count)
	int		dev;
	dev_flavor_t	flavor;
	int *		data;
	unsigned int	status_count;
{
	register struct tty *tp;
	register int	ttyno;

	if (dev == user_console)
		dev = console;

	ttyno = dev;

#if	NBM > 0
	if (screen_captures(ttyno) &&
	    (screen_set_status(SCREEN_CONS_UNIT(),
			flavor, data, status_count) == D_SUCCESS))
		return D_SUCCESS;
#endif	NBM > 0

	tp  = console_tty[ttyno];

	switch (flavor) {
	    case TTY_MODEM:
		if (status_count < TTY_MODEM_COUNT)
		    return (D_INVALID_OPERATION);
		(void) (*console_mctl)(dev, *data, DMSET);
		break;

	    case TTY_SET_BREAK:
		(void) (*console_mctl)(dev, TM_BRK, DMBIS);
		break;

	    case TTY_CLEAR_BREAK:
		(void) (*console_mctl)(dev, TM_BRK, DMBIC);
		break;

	    case TTY_STATUS:
	    {
		register int error = D_SUCCESS;
		struct tty_status *tsp;

		/*
		 * Defend from noise. The cshell...
		 */
		tsp = (struct tty_status *)data;
		if ((tsp->tt_ispeed != tp->t_ispeed) ||
		    (tsp->tt_ospeed != tp->t_ospeed) ||
		    (tsp->tt_breakc != tp->t_breakc) ||
		    ((tsp->tt_flags & ~TF_HUPCLS) != tp->t_flags)) {

			error = tty_set_status(tp, flavor, data, status_count);
			if (error == 0) {
			    spl_t s = spltty();
			    tp->t_state &= ~(TS_BUSY|TS_FLUSH);
			    (*console_param)(tp, ttyno);
			    splx(s);
			}
		} else
		if (tsp->tt_flags & TF_HUPCLS)
			tp->t_state |= TS_HUPCLS;
		return (error);
	    }
	    default:
		return (tty_set_status(tp, flavor, data, status_count));
	}
	return (D_SUCCESS);
}


/*
 * A simple scheme to dispatch interrupts.
 *
 * This deals with the fairly common case where we get an
 * interrupt on each rx/tx character.  A more elaborate
 * scheme [someday here too..] would handle instead many
 * characters per interrupt, perhaps using a DMA controller
 * or a large SILO.  Note that it is also possible to simulate
 * a DMA chip with 'pseudo-dma' code that runs directly down
 * in the interrupt routine.
 */
 
/*
 * We just received a character, ship it up for further processing.
 * Arguments are the tty number for which it is meant, a flag that
 * indicates a keyboard or mouse is potentially attached to that
 * tty (-1 if not), the character proper stripped down to 8 bits,
 * and an indication of any error conditions associated with the
 * receipt of the character.
 * We deal here with rconsole input handling and dispatching to
 * mouse or keyboard translation routines. cons_input() does
 * the rest.
 */
#if	MACH_KDB
int	l3break = 0x10;		/* dear old ^P, we miss you so bad. */
#endif	MACH_KDB

cons_simple_rint(ttyno, line, c, err)
	int		line;
	int		c;
{
	/*
	 * Rconsole. Drop in the debugger on break or ^P.
	 * Otherwise pretend input came from keyboard.
	 */
	if (rcline && ttyno == rcline) {
#if	MACH_KDB
		if ((err & CONS_ERR_BREAK) ||
		    ((c & 0x7f) == l3break))
			return gimmeabreak();
#endif	/* MACH_KDB */
		ttyno = console;
		goto process_it;
	}

#if	NBM > 0
	if (screen_captures(line)) {
		if (line == SCREEN_LINE_POINTER)
			return mouse_input(SCREEN_CONS_UNIT(), c);
		if (line == SCREEN_LINE_KEYBOARD) {
			c = lk201_rint(SCREEN_CONS_UNIT(), c, FALSE, FALSE);
			if (c == -1)
				return; /* shift or bad char */
		}
	}
#endif	NBM > 0
process_it:
	cons_input(ttyno, c, err);
}

/*
 * Send along a character on a tty.  If we were waiting for
 * this char to complete the open procedure do so; check
 * for errors; if all is well proceed to ttyinput().
 */
cons_input(ttyno, c, err)
{
	register struct tty *tp;

	tp  = console_tty[ttyno];

	if ((tp->t_state & TS_ISOPEN) == 0) {
		tt_open_wakeup(tp);
		return;
	}
	if (err) {
		if (err & CONS_ERR_OVERRUN)
			log(LOG_WARNING, "sl%d: silo overflow\n", ttyno);

		if (err & CONS_ERR_PARITY)	
			if (((tp->t_flags & (TF_EVENP|TF_ODDP)) == TF_EVENP)
			  || ((tp->t_flags & (TF_EVENP|TF_ODDP)) == TF_ODDP))
				return;
		if (err & CONS_ERR_BREAK)	/* XXX autobaud XXX */
			c = tp->t_breakc;
	}
	ttyinput(c, tp);
}
 
/*
 * Transmission of a character is complete.
 * Return the next character or -1 if none.
 */
cons_simple_tint(ttyno, all_sent)
	boolean_t	all_sent;
{
	register struct tty *tp;
 
	tp = console_tty[ttyno];
	if ((tp->t_addr == 0) || /* not probed --> stray */
	    (tp->t_state & TS_TTSTOP))
		return -1;

	if (all_sent) {
		tp->t_state &= ~TS_BUSY;
		if (tp->t_state & TS_FLUSH)
			tp->t_state &= ~TS_FLUSH;

		cons_start(tp);
	}

	if (tp->t_outq.c_cc == 0 || (tp->t_state&TS_BUSY)==0)
		return -1;

	return getc(&tp->t_outq);
}

 



#endif	/*NCONSTTY > 0*/