xref: /csrg-svn/sys/pmax/dev/dc.c (revision 54146)
1 /*
2  * Copyright (c) 1992 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Ralph Campbell.
7  *	@(#)dz.c	7.9 (Berkeley) 6/28/90
8  */
9 
10 /*
11  *  devDC7085.c --
12  *
13  * %sccs.include.redist.c%
14  *
15  *	@(#)dc.c	7.6 (Berkeley) 06/20/92
16  *
17  * devDC7085.c --
18  *
19  *     	This file contains machine-dependent routines that handle the
20  *	output queue for the serial lines.
21  *
22  *	Copyright (C) 1989 Digital Equipment Corporation.
23  *	Permission to use, copy, modify, and distribute this software and
24  *	its documentation for any purpose and without fee is hereby granted,
25  *	provided that the above copyright notice appears in all copies.
26  *	Digital Equipment Corporation makes no representations about the
27  *	suitability of this software for any purpose.  It is provided "as is"
28  *	without express or implied warranty.
29  *
30  * from: $Header: /sprite/src/kernel/dev/ds3100.md/RCS/devDC7085.c,
31  *	v 1.4 89/08/29 11:55:30 nelson Exp $ SPRITE (DECWRL)";
32  */
33 
34 #include "dc.h"
35 #if NDC > 0
36 /*
37  * DC7085 (DZ-11 look alike) Driver
38  */
39 #include "param.h"
40 #include "systm.h"
41 #include "ioctl.h"
42 #include "tty.h"
43 #include "proc.h"
44 #include "map.h"
45 #include "buf.h"
46 #include "conf.h"
47 #include "file.h"
48 #include "uio.h"
49 #include "kernel.h"
50 #include "syslog.h"
51 
52 #include "machine/dc7085cons.h"
53 
54 #include "device.h"
55 #include "pdma.h"
56 
57 /*
58  * Driver information for auto-configuration stuff.
59  */
60 int	dcprobe();
61 void	dcintr();
62 struct	driver dcdriver = {
63 	"dc", dcprobe, 0, 0, dcintr,
64 };
65 
66 #define	NDCLINE 	(NDC*4)
67 
68 extern void dcstart __P((struct tty *));
69 extern void dcxint __P((struct tty *));
70 extern void ttrstrt __P((struct tty *));
71 
72 struct	tty dc_tty[NDCLINE];
73 int	dc_cnt = NDCLINE;
74 void	(*dcDivertXInput)();	/* X windows keyboard input routine */
75 void	(*dcMouseEvent)();	/* X windows mouse motion event routine */
76 void	(*dcMouseButtons)();	/* X windows mouse buttons event routine */
77 #ifdef DEBUG
78 int	debugChar;
79 #endif
80 
81 static void dcscan __P((void));
82 static int dcMapChar __P((int));
83 static void dcKBDReset __P((void));
84 static void MouseInit __P((void));
85 
86 /*
87  * Software copy of brk register since it isn't readable
88  */
89 int	dc_brk[NDC];
90 char	dcsoftCAR[NDC];		/* mask of dc's with carrier on (DSR) */
91 
92 /*
93  * The DC7085 doesn't interrupt on carrier transitions, so
94  * we have to use a timer to watch it.
95  */
96 int	dc_timer;		/* true if timer started */
97 
98 /*
99  * Pdma structures for fast output code
100  */
101 struct	pdma dcpdma[NDCLINE];
102 
103 struct speedtab dcspeedtab[] = {
104 	0,	0,
105 	50,	LPR_B50,
106 	75,	LPR_B75,
107 	110,	LPR_B110,
108 	134,	LPR_B134,
109 	150,	LPR_B150,
110 	300,	LPR_B300,
111 	600,	LPR_B600,
112 	1200,	LPR_B1200,
113 	1800,	LPR_B1800,
114 	2400,	LPR_B2400,
115 	4800,	LPR_B4800,
116 	9600,	LPR_B9600,
117 #ifdef DS5000
118 	19200,	LPR_B19200,
119 #endif
120 	-1,	-1
121 };
122 
123 #ifndef	PORTSELECTOR
124 #define	ISPEED	TTYDEF_SPEED
125 #define	LFLAG	TTYDEF_LFLAG
126 #else
127 #define	ISPEED	B4800
128 #define	LFLAG	(TTYDEF_LFLAG & ~ECHO)
129 #endif
130 
131 /*
132  * Ascii values of command keys.
133  */
134 #define KBD_TAB		'\t'
135 #define KBD_DEL		127
136 #define KBD_RET		'\r'
137 
138 /*
139  *  Define "hardware-independent" codes for the control, shift, meta and
140  *  function keys.  Codes start after the last 7-bit ASCII code (127)
141  *  and are assigned in an arbitrary order.
142  */
143 #define KBD_NOKEY	128
144 
145 #define KBD_F1		201
146 #define KBD_F2		202
147 #define KBD_F3		203
148 #define KBD_F4		204
149 #define KBD_F5		205
150 #define KBD_F6		206
151 #define KBD_F7		207
152 #define KBD_F8		208
153 #define KBD_F9		209
154 #define KBD_F10		210
155 #define KBD_F11		211
156 #define KBD_F12		212
157 #define KBD_F13		213
158 #define KBD_F14		214
159 #define KBD_HELP	215
160 #define KBD_DO		216
161 #define KBD_F17		217
162 #define KBD_F18		218
163 #define KBD_F19		219
164 #define KBD_F20		220
165 
166 #define KBD_FIND	221
167 #define KBD_INSERT	222
168 #define KBD_REMOVE	223
169 #define KBD_SELECT	224
170 #define KBD_PREVIOUS	225
171 #define KBD_NEXT	226
172 
173 #define KBD_KP_ENTER	227
174 #define KBD_KP_F1	228
175 #define KBD_KP_F2	229
176 #define KBD_KP_F3	230
177 #define KBD_KP_F4	231
178 #define KBD_LEFT	232
179 #define KBD_RIGHT	233
180 #define KBD_DOWN	234
181 #define KBD_UP		235
182 
183 #define KBD_CONTROL	236
184 #define KBD_SHIFT	237
185 #define KBD_CAPSLOCK	238
186 #define KBD_ALTERNATE	239
187 
188 /*
189  * Keyboard to Ascii, unshifted.
190  */
191 static unsigned char unshiftedAscii[] = {
192 /*  0 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
193 /*  4 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
194 /*  8 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
195 /*  c */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
196 /* 10 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
197 /* 14 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
198 /* 18 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
199 /* 1c */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
200 /* 20 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
201 /* 24 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
202 /* 28 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
203 /* 2c */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
204 /* 30 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
205 /* 34 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
206 /* 38 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
207 /* 3c */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
208 /* 40 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
209 /* 44 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
210 /* 48 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
211 /* 4c */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
212 /* 50 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
213 /* 54 */ KBD_NOKEY,	KBD_NOKEY,	KBD_F1,		KBD_F2,
214 /* 58 */ KBD_F3,	KBD_F4,		KBD_F5,		KBD_NOKEY,
215 /* 5c */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
216 /* 60 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
217 /* 64 */ KBD_F6,	KBD_F7,		KBD_F8,		KBD_F9,
218 /* 68 */ KBD_F10,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
219 /* 6c */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
220 /* 70 */ KBD_NOKEY,	'\033',		KBD_F12,	KBD_F13,
221 /* 74 */ KBD_F14,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
222 /* 78 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
223 /* 7c */ KBD_HELP,	KBD_DO,		KBD_NOKEY,	KBD_NOKEY,
224 /* 80 */ KBD_F17,	KBD_F18,	KBD_F19,	KBD_F20,
225 /* 84 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
226 /* 88 */ KBD_NOKEY,	KBD_NOKEY,	KBD_FIND,	KBD_INSERT,
227 /* 8c */ KBD_REMOVE,	KBD_SELECT,	KBD_PREVIOUS,	KBD_NEXT,
228 /* 90 */ KBD_NOKEY,	KBD_NOKEY,	'0',		KBD_NOKEY,
229 /* 94 */ '.',		KBD_KP_ENTER,	'1',		'2',
230 /* 98 */ '3',		'4',		'5',		'6',
231 /* 9c */ ',',		'7',		'8',		'9',
232 /* a0 */ '-',		KBD_KP_F1,	KBD_KP_F2,	KBD_KP_F3,
233 /* a4 */ KBD_KP_F4,	KBD_NOKEY,	KBD_NOKEY,	KBD_LEFT,
234 /* a8 */ KBD_RIGHT,	KBD_DOWN, 	KBD_UP,		KBD_NOKEY,
235 /* ac */ KBD_NOKEY,	KBD_NOKEY,	KBD_SHIFT,	KBD_CONTROL,
236 /* b0 */ KBD_CAPSLOCK,	KBD_ALTERNATE,	KBD_NOKEY,	KBD_NOKEY,
237 /* b4 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
238 /* b8 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
239 /* bc */ KBD_DEL,	KBD_RET,	KBD_TAB,	'`',
240 /* c0 */ '1',		'q',		'a',		'z',
241 /* c4 */ KBD_NOKEY,	'2',		'w',		's',
242 /* c8 */ 'x',		'<',		KBD_NOKEY,	'3',
243 /* cc */ 'e',		'd',		'c',		KBD_NOKEY,
244 /* d0 */ '4',		'r',		'f',		'v',
245 /* d4 */ ' ',		KBD_NOKEY,	'5',		't',
246 /* d8 */ 'g',		'b',		KBD_NOKEY,	'6',
247 /* dc */ 'y',		'h',		'n',		KBD_NOKEY,
248 /* e0 */ '7',		'u',		'j',		'm',
249 /* e4 */ KBD_NOKEY,	'8',		'i',		'k',
250 /* e8 */ ',',		KBD_NOKEY,	'9',		'o',
251 /* ec */ 'l',		'.',		KBD_NOKEY,	'0',
252 /* f0 */ 'p',		KBD_NOKEY,	';',		'/',
253 /* f4 */ KBD_NOKEY,	'=',		']',		'\\',
254 /* f8 */ KBD_NOKEY,	'-',		'[',		'\'',
255 /* fc */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
256 };
257 
258 /*
259  * Keyboard to Ascii, shifted.
260  */
261 static unsigned char shiftedAscii[] = {
262 /*  0 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
263 /*  4 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
264 /*  8 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
265 /*  c */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
266 /* 10 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
267 /* 14 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
268 /* 18 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
269 /* 1c */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
270 /* 20 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
271 /* 24 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
272 /* 28 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
273 /* 2c */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
274 /* 30 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
275 /* 34 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
276 /* 38 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
277 /* 3c */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
278 /* 40 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
279 /* 44 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
280 /* 48 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
281 /* 4c */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
282 /* 50 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
283 /* 54 */ KBD_NOKEY,	KBD_NOKEY,	KBD_F1,		KBD_F2,
284 /* 58 */ KBD_F3,	KBD_F4,		KBD_F5,		KBD_NOKEY,
285 /* 5c */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
286 /* 60 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
287 /* 64 */ KBD_F6,	KBD_F7,		KBD_F8,		KBD_F9,
288 /* 68 */ KBD_F10,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
289 /* 6c */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
290 /* 70 */ KBD_NOKEY,	KBD_F11,	KBD_F12,	KBD_F13,
291 /* 74 */ KBD_F14,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
292 /* 78 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
293 /* 7c */ KBD_HELP,	KBD_DO,		KBD_NOKEY,	KBD_NOKEY,
294 /* 80 */ KBD_F17,	KBD_F18,	KBD_F19,	KBD_F20,
295 /* 84 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
296 /* 88 */ KBD_NOKEY,	KBD_NOKEY,	KBD_FIND,	KBD_INSERT,
297 /* 8c */ KBD_REMOVE,	KBD_SELECT,	KBD_PREVIOUS,	KBD_NEXT,
298 /* 90 */ KBD_NOKEY,	KBD_NOKEY,	'0',		KBD_NOKEY,
299 /* 94 */ '.',		KBD_KP_ENTER,	'1',		'2',
300 /* 98 */ '3',		'4',		'5',		'6',
301 /* 9c */ ',',		'7',		'8',		'9',
302 /* a0 */ '-',		KBD_KP_F1,	KBD_KP_F2,	KBD_KP_F3,
303 /* a4 */ KBD_KP_F4,	KBD_NOKEY,	KBD_NOKEY,	KBD_LEFT,
304 /* a8 */ KBD_RIGHT,	KBD_DOWN, 	KBD_UP,		KBD_NOKEY,
305 /* ac */ KBD_NOKEY,	KBD_NOKEY,	KBD_SHIFT,	KBD_CONTROL,
306 /* b0 */ KBD_CAPSLOCK,	KBD_ALTERNATE,	KBD_NOKEY,	KBD_NOKEY,
307 /* b4 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
308 /* b8 */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
309 /* bc */ KBD_DEL,	KBD_RET,	KBD_TAB,	'~',
310 /* c0 */ '!',		'q',		'a',		'z',
311 /* c4 */ KBD_NOKEY,	'@',		'w',		's',
312 /* c8 */ 'x',		'>',		KBD_NOKEY,	'#',
313 /* cc */ 'e',		'd',		'c',		KBD_NOKEY,
314 /* d0 */ '$',		'r',		'f',		'v',
315 /* d4 */ ' ',		KBD_NOKEY,	'%',		't',
316 /* d8 */ 'g',		'b',		KBD_NOKEY,	'^',
317 /* dc */ 'y',		'h',		'n',		KBD_NOKEY,
318 /* e0 */ '&',		'u',		'j',		'm',
319 /* e4 */ KBD_NOKEY,	'*',		'i',		'k',
320 /* e8 */ '<',		KBD_NOKEY,	'(',		'o',
321 /* ec */ 'l',		'>',		KBD_NOKEY,	')',
322 /* f0 */ 'p',		KBD_NOKEY,	':',		'?',
323 /* f4 */ KBD_NOKEY,	'+',		'}',		'|',
324 /* f8 */ KBD_NOKEY,	'_',		'{',		'"',
325 /* fc */ KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,	KBD_NOKEY,
326 };
327 
328 /*
329  * Keyboard initialization string.
330  */
331 static u_char kbdInitString[] = {
332 	LK_LED_ENABLE, LED_ALL,		/* show we are resetting keyboard */
333 	LK_DEFAULTS,
334 	LK_CMD_MODE(LK_AUTODOWN, 1),
335 	LK_CMD_MODE(LK_AUTODOWN, 2),
336 	LK_CMD_MODE(LK_AUTODOWN, 3),
337 	LK_CMD_MODE(LK_DOWN, 4),	/* could also be LK_AUTODOWN */
338 	LK_CMD_MODE(LK_UPDOWN, 5),
339 	LK_CMD_MODE(LK_UPDOWN, 6),
340 	LK_CMD_MODE(LK_AUTODOWN, 7),
341 	LK_CMD_MODE(LK_AUTODOWN, 8),
342 	LK_CMD_MODE(LK_AUTODOWN, 9),
343 	LK_CMD_MODE(LK_AUTODOWN, 10),
344 	LK_CMD_MODE(LK_AUTODOWN, 11),
345 	LK_CMD_MODE(LK_AUTODOWN, 12),
346 	LK_CMD_MODE(LK_DOWN, 13),
347 	LK_CMD_MODE(LK_AUTODOWN, 14),
348 	LK_AR_ENABLE,			/* we want autorepeat by default */
349 	LK_CL_ENABLE, 0x83,		/* keyclick, volume */
350 	LK_KBD_ENABLE,			/* the keyboard itself */
351 	LK_BELL_ENABLE, 0x83,		/* keyboard bell, volume */
352 	LK_LED_DISABLE, LED_ALL,	/* clear keyboard leds */
353 };
354 
355 /*
356  * Test to see if device is present.
357  * Return true if found and initialized ok.
358  */
359 dcprobe(cp)
360 	register struct pmax_ctlr *cp;
361 {
362 	register dcregs *dcaddr;
363 	register struct pdma *pdp;
364 	register struct tty *tp;
365 	register int cntr;
366 
367 	if (cp->pmax_unit >= NDC)
368 		return (0);
369 	if (badaddr(cp->pmax_addr, 2))
370 		return (0);
371 
372 	/* reset chip */
373 	dcaddr = (dcregs *)cp->pmax_addr;
374 	dcaddr->dc_csr = CSR_CLR;
375 	MachEmptyWriteBuffer();
376 	while (dcaddr->dc_csr & CSR_CLR)
377 		;
378 	dcaddr->dc_csr = CSR_MSE | CSR_TIE | CSR_RIE;
379 
380 	/* init pseudo DMA structures */
381 	pdp = &dcpdma[cp->pmax_unit * 4];
382 	tp = &dc_tty[cp->pmax_unit * 4];
383 	for (cntr = 0; cntr < 4; cntr++) {
384 		pdp->p_addr = dcaddr;
385 		pdp->p_arg = (int)tp;
386 		pdp->p_fcn = dcxint;
387 		tp->t_addr = (caddr_t)pdp;
388 		pdp++, tp++;
389 	}
390 	dcsoftCAR[cp->pmax_unit] = cp->pmax_flags | 0xB;
391 
392 	if (dc_timer == 0) {
393 		dc_timer = 1;
394 		timeout(dcscan, (caddr_t)0, hz);
395 	}
396 	printf("dc%d at nexus0 csr 0x%x priority %d\n",
397 		cp->pmax_unit, cp->pmax_addr, cp->pmax_pri);
398 	if (cp->pmax_unit == 0) {
399 		int s;
400 
401 		s = spltty();
402 		dcaddr->dc_lpr = LPR_RXENAB | LPR_B4800 | LPR_8_BIT_CHAR |
403 			KBD_PORT;
404 		dcaddr->dc_lpr = LPR_RXENAB | LPR_B4800 | LPR_OPAR |
405 			LPR_PARENB | LPR_8_BIT_CHAR | MOUSE_PORT;
406 		MachEmptyWriteBuffer();
407 		dcKBDReset();
408 		MouseInit();
409 		splx(s);
410 	}
411 	return (1);
412 }
413 
414 dcopen(dev, flag, mode, p)
415 	dev_t dev;
416 	int flag, mode;
417 	struct proc *p;
418 {
419 	register struct tty *tp;
420 	register int unit;
421 	int s, error = 0;
422 	extern int dcparam();
423 
424 	unit = minor(dev);
425 	if (unit >= dc_cnt || dcpdma[unit].p_addr == 0)
426 		return (ENXIO);
427 	tp = &dc_tty[unit];
428 	tp->t_addr = (caddr_t)&dcpdma[unit];
429 	tp->t_oproc = dcstart;
430 	tp->t_param = dcparam;
431 	tp->t_dev = dev;
432 	if ((tp->t_state & TS_ISOPEN) == 0) {
433 		tp->t_state |= TS_WOPEN;
434 		ttychars(tp);
435 #ifndef PORTSELECTOR
436 		if (tp->t_ispeed == 0) {
437 #endif
438 			tp->t_iflag = TTYDEF_IFLAG;
439 			tp->t_oflag = TTYDEF_OFLAG;
440 			tp->t_cflag = TTYDEF_CFLAG;
441 			tp->t_lflag = LFLAG;
442 			tp->t_ispeed = tp->t_ospeed = ISPEED;
443 #ifdef PORTSELECTOR
444 			tp->t_cflag |= HUPCL;
445 #else
446 		}
447 #endif
448 		(void) dcparam(tp, &tp->t_termios);
449 		ttsetwater(tp);
450 	} else if ((tp->t_state & TS_XCLUDE) && curproc->p_ucred->cr_uid != 0)
451 		return (EBUSY);
452 	(void) dcmctl(dev, DML_DTR, DMSET);
453 	s = spltty();
454 	while (!(flag & O_NONBLOCK) && !(tp->t_cflag & CLOCAL) &&
455 	       !(tp->t_state & TS_CARR_ON)) {
456 		tp->t_state |= TS_WOPEN;
457 		if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
458 		    ttopen, 0))
459 			break;
460 	}
461 	splx(s);
462 	if (error)
463 		return (error);
464 	return ((*linesw[tp->t_line].l_open)(dev, tp));
465 }
466 
467 /*ARGSUSED*/
468 dcclose(dev, flag, mode, p)
469 	dev_t dev;
470 	int flag, mode;
471 	struct proc *p;
472 {
473 	register struct tty *tp;
474 	register int unit, bit;
475 
476 	unit = minor(dev);
477 	tp = &dc_tty[unit];
478 	bit = 1 << ((unit & 03) + 8);
479 	if (dc_brk[unit >> 2] & bit) {
480 		dc_brk[unit >> 2] &= ~bit;
481 		ttyoutput(0, tp);
482 	}
483 	(*linesw[tp->t_line].l_close)(tp, flag);
484 	if ((tp->t_cflag & HUPCL) || (tp->t_state & TS_WOPEN) ||
485 	    !(tp->t_state & TS_ISOPEN))
486 		(void) dcmctl(dev, 0, DMSET);
487 	return (ttyclose(tp));
488 }
489 
490 dcread(dev, uio, flag)
491 	dev_t dev;
492 	struct uio *uio;
493 {
494 	register struct tty *tp;
495 
496 	tp = &dc_tty[minor(dev)];
497 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
498 }
499 
500 dcwrite(dev, uio, flag)
501 	dev_t dev;
502 	struct uio *uio;
503 {
504 	register struct tty *tp;
505 
506 	tp = &dc_tty[minor(dev)];
507 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
508 }
509 
510 /*ARGSUSED*/
511 dcioctl(dev, cmd, data, flag, p)
512 	dev_t dev;
513 	caddr_t data;
514 	int flag;
515 	struct proc *p;
516 {
517 	register struct tty *tp;
518 	register int unit = minor(dev);
519 	register int dc = unit >> 2;
520 	int error;
521 
522 	tp = &dc_tty[unit];
523 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
524 	if (error >= 0)
525 		return (error);
526 	error = ttioctl(tp, cmd, data, flag);
527 	if (error >= 0)
528 		return (error);
529 
530 	switch (cmd) {
531 
532 	case TIOCSBRK:
533 		dc_brk[dc] |= 1 << ((unit & 03) + 8);
534 		ttyoutput(0, tp);
535 		break;
536 
537 	case TIOCCBRK:
538 		dc_brk[dc] &= ~(1 << ((unit & 03) + 8));
539 		ttyoutput(0, tp);
540 		break;
541 
542 	case TIOCSDTR:
543 		(void) dcmctl(dev, DML_DTR|DML_RTS, DMBIS);
544 		break;
545 
546 	case TIOCCDTR:
547 		(void) dcmctl(dev, DML_DTR|DML_RTS, DMBIC);
548 		break;
549 
550 	case TIOCMSET:
551 		(void) dcmctl(dev, *(int *)data, DMSET);
552 		break;
553 
554 	case TIOCMBIS:
555 		(void) dcmctl(dev, *(int *)data, DMBIS);
556 		break;
557 
558 	case TIOCMBIC:
559 		(void) dcmctl(dev, *(int *)data, DMBIC);
560 		break;
561 
562 	case TIOCMGET:
563 		*(int *)data = dcmctl(dev, 0, DMGET);
564 		break;
565 
566 	default:
567 		return (ENOTTY);
568 	}
569 	return (0);
570 }
571 
572 dcparam(tp, t)
573 	register struct tty *tp;
574 	register struct termios *t;
575 {
576 	register dcregs *dcaddr;
577 	register int lpr;
578 	register int cflag = t->c_cflag;
579 	int unit = minor(tp->t_dev);
580 	int ospeed = ttspeedtab(t->c_ospeed, dcspeedtab);
581 
582 	/* check requested parameters */
583         if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed) ||
584             (cflag & CSIZE) == CS5 || (cflag & CSIZE) == CS6)
585                 return (EINVAL);
586         /* and copy to tty */
587         tp->t_ispeed = t->c_ispeed;
588         tp->t_ospeed = t->c_ospeed;
589         tp->t_cflag = cflag;
590 
591 	dcaddr = dcpdma[unit].p_addr;
592 	if (tp == dc_tty + KBD_PORT) {
593 		/* handle the keyboard specially */
594 		dcaddr->dc_lpr = LPR_RXENAB | LPR_B4800 | LPR_8_BIT_CHAR |
595 			KBD_PORT;
596 		MachEmptyWriteBuffer();
597 		return (0);
598 	}
599 	if (tp == dc_tty + MOUSE_PORT) {
600 		/* handle the mouse specially */
601 		dcaddr->dc_lpr = LPR_RXENAB | LPR_B4800 | LPR_OPAR |
602 			LPR_PARENB | LPR_8_BIT_CHAR | MOUSE_PORT;
603 		MachEmptyWriteBuffer();
604 		return (0);
605 	}
606 	if (ospeed == 0) {
607 		(void) dcmctl(unit, 0, DMSET);	/* hang up line */
608 		return (0);
609 	}
610 	lpr = LPR_RXENAB | ospeed | (unit & 03);
611 	if ((cflag & CSIZE) == CS7)
612 		lpr |= LPR_7_BIT_CHAR;
613 	else
614 		lpr |= LPR_8_BIT_CHAR;
615 	if (cflag & PARENB)
616 		lpr |= LPR_PARENB;
617 	if (cflag & PARODD)
618 		lpr |= LPR_OPAR;
619 	if (cflag & CSTOPB)
620 		lpr |= LPR_2_STOP;
621 	dcaddr->dc_lpr = lpr;
622 	MachEmptyWriteBuffer();
623 	return (0);
624 }
625 
626 /*
627  * Check for interrupts from all devices.
628  */
629 void
630 dcintr(unit)
631 	register int unit;
632 {
633 	register dcregs *dcaddr;
634 	register unsigned csr;
635 
636 	unit <<= 2;
637 	dcaddr = dcpdma[unit].p_addr;
638 	while ((csr = dcaddr->dc_csr) & (CSR_RDONE | CSR_TRDY)) {
639 		if (csr & CSR_RDONE)
640 			dcrint(unit);
641 		if (csr & CSR_TRDY)
642 			dcxint(&dc_tty[unit + ((csr >> 8) & 03)]);
643 	}
644 }
645 
646 dcrint(unit)
647 	register int unit;
648 {
649 	register dcregs *dcaddr;
650 	register struct tty *tp;
651 	register int c, cc;
652 	register struct tty *tp0;
653 	int overrun = 0;
654 
655 	dcaddr = dcpdma[unit].p_addr;
656 	tp0 = &dc_tty[unit];
657 	while ((c = dcaddr->dc_rbuf) < 0) {	/* char present */
658 		cc = c & 0xff;
659 		tp = tp0 + ((c >> 8) & 03);
660 		if ((c & RBUF_OERR) && overrun == 0) {
661 			log(LOG_WARNING, "dc%d,%d: silo overflow\n", unit >> 2,
662 				(c >> 8) & 03);
663 			overrun = 1;
664 		}
665 		/* the keyboard requires special translation */
666 		if (tp == &dc_tty[KBD_PORT]) {
667 #ifdef KADB
668 			if (cc == LK_DO) {
669 				spl0();
670 				kdbpanic();
671 				return;
672 			}
673 #endif
674 #ifdef DEBUG
675 			debugChar = cc;
676 #endif
677 			if (dcDivertXInput) {
678 				(*dcDivertXInput)(cc);
679 				return;
680 			}
681 			if ((cc = dcMapChar(cc)) < 0)
682 				return;
683 		} else if (tp == &dc_tty[MOUSE_PORT] && dcMouseButtons) {
684 			register MouseReport *mrp;
685 			static MouseReport currentRep;
686 
687 			mrp = &currentRep;
688 			mrp->byteCount++;
689 			if (cc & MOUSE_START_FRAME) {
690 				/*
691 				 * The first mouse report byte (button state).
692 				 */
693 				mrp->state = cc;
694 				if (mrp->byteCount > 1)
695 					mrp->byteCount = 1;
696 			} else if (mrp->byteCount == 2) {
697 				/*
698 				 * The second mouse report byte (delta x).
699 				 */
700 				mrp->dx = cc;
701 			} else if (mrp->byteCount == 3) {
702 				/*
703 				 * The final mouse report byte (delta y).
704 				 */
705 				mrp->dy = cc;
706 				mrp->byteCount = 0;
707 				if (mrp->dx != 0 || mrp->dy != 0) {
708 					/*
709 					 * If the mouse moved,
710 					 * post a motion event.
711 					 */
712 					(*dcMouseEvent)(mrp);
713 				}
714 				(*dcMouseButtons)(mrp);
715 			}
716 			return;
717 		}
718 		if (!(tp->t_state & TS_ISOPEN)) {
719 			wakeup((caddr_t)&tp->t_rawq);
720 #ifdef PORTSELECTOR
721 			if (!(tp->t_state & TS_WOPEN))
722 #endif
723 				return;
724 		}
725 		if (c & RBUF_FERR)
726 			cc |= TTY_FE;
727 		if (c & RBUF_PERR)
728 			cc |= TTY_PE;
729 		(*linesw[tp->t_line].l_rint)(cc, tp);
730 	}
731 	DELAY(10);
732 }
733 
734 void
735 dcxint(tp)
736 	register struct tty *tp;
737 {
738 	register struct pdma *dp;
739 	register dcregs *dcaddr;
740 
741 	dp = (struct pdma *)tp->t_addr;
742 	if (dp->p_mem < dp->p_end) {
743 		dcaddr = dp->p_addr;
744 		dcaddr->dc_tdr = dc_brk[(tp - dc_tty) >> 2] | *dp->p_mem++;
745 		MachEmptyWriteBuffer();
746 		DELAY(10);
747 		return;
748 	}
749 	tp->t_state &= ~TS_BUSY;
750 	if (tp->t_state & TS_FLUSH)
751 		tp->t_state &= ~TS_FLUSH;
752 	else {
753 		ndflush(&tp->t_outq, dp->p_mem-tp->t_outq.c_cf);
754 		dp->p_end = dp->p_mem = tp->t_outq.c_cf;
755 	}
756 	if (tp->t_line)
757 		(*linesw[tp->t_line].l_start)(tp);
758 	else
759 		dcstart(tp);
760 	if (tp->t_outq.c_cc == 0 || !(tp->t_state & TS_BUSY)) {
761 		dp->p_addr->dc_tcr &= ~(1 << (minor(tp->t_dev) & 03));
762 		MachEmptyWriteBuffer();
763 		DELAY(10);
764 	}
765 }
766 
767 void
768 dcstart(tp)
769 	register struct tty *tp;
770 {
771 	register struct pdma *dp;
772 	register dcregs *dcaddr;
773 	register int cc;
774 	int s;
775 
776 	dp = (struct pdma *)tp->t_addr;
777 	dcaddr = dp->p_addr;
778 	s = spltty();
779 	if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
780 		goto out;
781 	if (tp->t_outq.c_cc <= tp->t_lowat) {
782 		if (tp->t_state & TS_ASLEEP) {
783 			tp->t_state &= ~TS_ASLEEP;
784 			wakeup((caddr_t)&tp->t_outq);
785 		}
786 		selwakeup(&tp->t_wsel);
787 	}
788 	if (tp->t_outq.c_cc == 0)
789 		goto out;
790 	/* handle console specially */
791 	if (tp == dc_tty) {
792 		while (tp->t_outq.c_cc > 0) {
793 			cc = getc(&tp->t_outq) & 0x7f;
794 			cnputc(cc);
795 		}
796 		/*
797 		 * After we flush the output queue we may need to wake
798 		 * up the process that made the output.
799 		 */
800 		if (tp->t_outq.c_cc <= tp->t_lowat) {
801 			if (tp->t_state & TS_ASLEEP) {
802 				tp->t_state &= ~TS_ASLEEP;
803 				wakeup((caddr_t)&tp->t_outq);
804 			}
805 			selwakeup(&tp->t_wsel);
806 		}
807 		goto out;
808 	}
809 	if (tp->t_flags & (RAW|LITOUT))
810 		cc = ndqb(&tp->t_outq, 0);
811 	else {
812 		cc = ndqb(&tp->t_outq, 0200);
813 		if (cc == 0) {
814 			cc = getc(&tp->t_outq);
815 			timeout(ttrstrt, (caddr_t)tp, (cc & 0x7f) + 6);
816 			tp->t_state |= TS_TIMEOUT;
817 			goto out;
818 		}
819 	}
820 	tp->t_state |= TS_BUSY;
821 	dp->p_end = dp->p_mem = tp->t_outq.c_cf;
822 	dp->p_end += cc;
823 	dcaddr->dc_tcr |= 1 << (minor(tp->t_dev) & 03);
824 	MachEmptyWriteBuffer();
825 out:
826 	splx(s);
827 }
828 
829 /*
830  * Stop output on a line.
831  */
832 /*ARGSUSED*/
833 dcstop(tp, flag)
834 	register struct tty *tp;
835 {
836 	register struct pdma *dp;
837 	register int s;
838 
839 	dp = (struct pdma *)tp->t_addr;
840 	s = spltty();
841 	if (tp->t_state & TS_BUSY) {
842 		dp->p_end = dp->p_mem;
843 		if (!(tp->t_state & TS_TTSTOP))
844 			tp->t_state |= TS_FLUSH;
845 	}
846 	splx(s);
847 }
848 
849 dcmctl(dev, bits, how)
850 	dev_t dev;
851 	int bits, how;
852 {
853 	register dcregs *dcaddr;
854 	register int unit, mbits;
855 	int b, s;
856 #ifdef DS5000
857 	register int msr;
858 #endif
859 
860 	unit = minor(dev);
861 	b = 1 << (unit & 03);
862 	dcaddr = dcpdma[unit].p_addr;
863 	s = spltty();
864 	/* only channel 2 has modem control (what about line 3?) */
865 	switch (unit & 03) {
866 	case 2:
867 		mbits = 0;
868 		if (dcaddr->dc_tcr & TCR_DTR2)
869 			mbits |= DML_DTR;
870 #ifdef DS3100
871 		if (dcaddr->dc_msr & MSR_DSR2)
872 			mbits |= DML_DSR | DML_CAR;
873 #endif
874 #ifdef DS5000
875 		msr = dcaddr->dc_msr;
876 		if (msr & MSR_CD2)
877 			mbits |= DML_CAR;
878 		if (msr & MSR_DSR2)
879 			mbits |= DML_DSR;
880 #endif
881 		break;
882 
883 #ifdef DS5000
884 	case 3:
885 		mbits = 0;
886 		if (dcaddr->dc_tcr & TCR_DTR3)
887 			mbits |= DML_DTR;
888 		msr = dcaddr->dc_msr;
889 		if (msr & MSR_CD3)
890 			mbits |= DML_CAR;
891 		if (msr & MSR_DSR3)
892 			mbits |= DML_DSR;
893 		break;
894 #endif
895 
896 	default:
897 		mbits = DML_DTR | DML_DSR | DML_CAR;
898 	}
899 	switch (how) {
900 	case DMSET:
901 		mbits = bits;
902 		break;
903 
904 	case DMBIS:
905 		mbits |= bits;
906 		break;
907 
908 	case DMBIC:
909 		mbits &= ~bits;
910 		break;
911 
912 	case DMGET:
913 		(void) splx(s);
914 		return (mbits);
915 	}
916 	switch (unit & 03) {
917 	case 2:
918 		if (mbits & DML_DTR)
919 			dcaddr->dc_tcr |= TCR_DTR2;
920 		else
921 			dcaddr->dc_tcr &= ~TCR_DTR2;
922 		break;
923 
924 #ifdef DS5000
925 	case 3:
926 		if (mbits & DML_DTR)
927 			dcaddr->dc_tcr |= TCR_DTR3;
928 		else
929 			dcaddr->dc_tcr &= ~TCR_DTR3;
930 #endif
931 	}
932 	if ((mbits & DML_DTR) && (dcsoftCAR[unit >> 2] & b))
933 		dc_tty[unit].t_state |= TS_CARR_ON;
934 	(void) splx(s);
935 	return (mbits);
936 }
937 
938 /*
939  * This is called by timeout() periodically.
940  * Check to see if modem status bits have changed.
941  */
942 static void
943 dcscan()
944 {
945 	register dcregs *dcaddr;
946 	register struct tty *tp;
947 	register int i, bit, car;
948 	int s;
949 
950 	s = spltty();
951 	/* only channel 2 has modem control (what about line 3?) */
952 	dcaddr = dcpdma[i = 2].p_addr;
953 	tp = &dc_tty[i];
954 	bit = TCR_DTR2;
955 	if (dcsoftCAR[i >> 2] & bit)
956 		car = 1;
957 	else
958 		car = dcaddr->dc_msr & MSR_DSR2;
959 	if (car) {
960 		/* carrier present */
961 		if (!(tp->t_state & TS_CARR_ON))
962 			(void)(*linesw[tp->t_line].l_modem)(tp, 1);
963 	} else if ((tp->t_state & TS_CARR_ON) &&
964 	    (*linesw[tp->t_line].l_modem)(tp, 0) == 0)
965 		dcaddr->dc_tcr &= ~bit;
966 	splx(s);
967 	timeout(dcscan, (caddr_t)0, hz);
968 }
969 
970 /*
971  * ----------------------------------------------------------------------------
972  *
973  * dcKBDPutc --
974  *
975  *	Put a character out to the keyboard.
976  *
977  * Results:
978  *	None.
979  *
980  * Side effects:
981  *	A character is written to the keyboard.
982  *
983  * ----------------------------------------------------------------------------
984  */
985 void
986 dcKBDPutc(c)
987 	register int c;
988 {
989 	register dcregs *dcaddr;
990 	register u_short tcr;
991 	register int timeout;
992 	int s, line;
993 
994 	s = spltty();
995 
996 	dcaddr = dcpdma[KBD_PORT].p_addr;
997 	tcr = dcaddr->dc_tcr;
998 	dcaddr->dc_tcr = tcr | (1 << KBD_PORT);
999 	MachEmptyWriteBuffer();
1000 	DELAY(10);
1001 	while (1) {
1002 		/*
1003 		 * Wait for transmitter to be not busy.
1004 		 */
1005 		timeout = 1000000;
1006 		while (!(dcaddr->dc_csr & CSR_TRDY) && timeout > 0)
1007 			timeout--;
1008 		if (timeout == 0) {
1009 			printf("dcKBDPutc: timeout waiting for CSR_TRDY\n");
1010 			break;
1011 		}
1012 		line = (dcaddr->dc_csr >> 8) & 3;
1013 		/*
1014 		 * Check to be sure its the right port.
1015 		 */
1016 		if (line != KBD_PORT) {
1017 			tcr |= 1 << line;
1018 			dcaddr->dc_tcr &= ~(1 << line);
1019 			MachEmptyWriteBuffer();
1020 			DELAY(10);
1021 			continue;
1022 		}
1023 		/*
1024 		 * Start sending the character.
1025 		 */
1026 		dcaddr->dc_tdr = dc_brk[0] | (c & 0xff);
1027 		MachEmptyWriteBuffer();
1028 		DELAY(10);
1029 		/*
1030 		 * Wait for character to be sent.
1031 		 */
1032 		while (1) {
1033 			/*
1034 			 * cc -O bug: this code produces and infinite loop!
1035 			 * while (!(dcaddr->dc_csr & CSR_TRDY))
1036 			 *	;
1037 			 */
1038 			timeout = 1000000;
1039 			while (!(dcaddr->dc_csr & CSR_TRDY) && timeout > 0)
1040 				timeout--;
1041 			line = (dcaddr->dc_csr >> 8) & 3;
1042 			if (line != KBD_PORT) {
1043 				tcr |= 1 << line;
1044 				dcaddr->dc_tcr &= ~(1 << line);
1045 				MachEmptyWriteBuffer();
1046 				DELAY(10);
1047 				continue;
1048 			}
1049 			dcaddr->dc_tcr &= ~(1 << KBD_PORT);
1050 			MachEmptyWriteBuffer();
1051 			DELAY(10);
1052 			break;
1053 		}
1054 		break;
1055 	}
1056 	/*
1057 	 * Enable interrupts for other lines which became ready.
1058 	 */
1059 	if (tcr & 0xF) {
1060 		dcaddr->dc_tcr = tcr;
1061 		MachEmptyWriteBuffer();
1062 		DELAY(10);
1063 	}
1064 
1065 	splx(s);
1066 }
1067 
1068 #ifdef DEBUG
1069 /*
1070  * ----------------------------------------------------------------------------
1071  *
1072  * dcDebugGetc --
1073  *
1074  *	Read a character from the keyboard if one is ready (i.e., don't wait).
1075  *
1076  * Results:
1077  *	A character read from the mouse, -1 if none were ready.
1078  *
1079  * Side effects:
1080  *	None.
1081  *
1082  * ----------------------------------------------------------------------------
1083  */
1084 int
1085 dcDebugGetc()
1086 {
1087 	register dcregs *dcaddr;
1088 	register int c;
1089 	int s;
1090 
1091 	dcaddr = dcpdma[KBD_PORT].p_addr;
1092 	if (!dcaddr)
1093 		return (0);
1094 
1095 	s = spltty();
1096 	if (c = debugChar)
1097 		debugChar = 0;
1098 	else {
1099 		while (dcaddr->dc_csr & CSR_RDONE) {
1100 			c = dcaddr->dc_rbuf;
1101 			DELAY(10);
1102 			if (((c >> 8) & 03) == KBD_PORT)
1103 				break;
1104 			c = 0;
1105 		}
1106 	}
1107 	splx(s);
1108 
1109 	return (c & 0xff);
1110 }
1111 #endif
1112 
1113 /*
1114  * ----------------------------------------------------------------------------
1115  *
1116  * dcKBDGetc --
1117  *
1118  *	Read a character from the keyboard.
1119  *
1120  * Results:
1121  *	A character read from the keyboard.
1122  *
1123  * Side effects:
1124  *	None.
1125  *
1126  * ----------------------------------------------------------------------------
1127  */
1128 int
1129 dcKBDGetc()
1130 {
1131 	register dcregs *dcaddr;
1132 	register int c;
1133 	int s;
1134 
1135 	dcaddr = dcpdma[KBD_PORT].p_addr;
1136 	if (!dcaddr)
1137 		return (-1);
1138 	s = spltty();
1139 	for (;;) {
1140 		if (!(dcaddr->dc_csr & CSR_RDONE))
1141 			continue;
1142 		c = dcaddr->dc_rbuf;
1143 		DELAY(10);
1144 		if (((c >> 8) & 03) != KBD_PORT)
1145 			continue;
1146 		if ((c = dcMapChar(c & 0xff)) >= 0)
1147 			break;
1148 	}
1149 	splx(s);
1150 	return (c);
1151 }
1152 
1153 /*
1154  * ----------------------------------------------------------------------------
1155  *
1156  * dcMapChar --
1157  *
1158  *	Map characters from the keyboard to ASCII. Return -1 if there is
1159  *	no valid mapping.
1160  *
1161  * Results:
1162  *	None.
1163  *
1164  * Side effects:
1165  *	Remember state of shift and control keys.
1166  *
1167  * ----------------------------------------------------------------------------
1168  */
1169 static int
1170 dcMapChar(cc)
1171 	int cc;
1172 {
1173 	static u_char shiftDown;
1174 	static u_char ctrlDown;
1175 	static u_char lastChar;
1176 
1177 	switch (cc) {
1178 	case KEY_REPEAT:
1179 		cc = lastChar;
1180 		goto done;
1181 
1182 	case KEY_UP:
1183 		shiftDown = 0;
1184 		ctrlDown = 0;
1185 		return (-1);
1186 
1187 	case KEY_SHIFT:
1188 		if (ctrlDown)
1189 			shiftDown = 0;
1190 		else
1191 			shiftDown = 1;
1192 		return (-1);
1193 
1194 	case KEY_CONTROL:
1195 		if (shiftDown)
1196 			ctrlDown = 0;
1197 		else
1198 			ctrlDown = 1;
1199 		return (-1);
1200 
1201 	case LK_POWER_ERROR:
1202 	case LK_KDOWN_ERROR:
1203 	case LK_INPUT_ERROR:
1204 	case LK_OUTPUT_ERROR:
1205 		log(LOG_WARNING,
1206 			"dc0,0: keyboard error, code=%x\n", cc);
1207 		return (-1);
1208 	}
1209 	if (shiftDown)
1210 		cc = shiftedAscii[cc];
1211 	else
1212 		cc = unshiftedAscii[cc];
1213 	if (cc >= KBD_NOKEY) {
1214 		/*
1215 		 * A function key was typed - ignore it.
1216 		 */
1217 		return (-1);
1218 	}
1219 	if (cc >= 'a' && cc <= 'z') {
1220 		if (ctrlDown)
1221 			cc = cc - 'a' + '\1'; /* ^A */
1222 		else if (shiftDown)
1223 			cc = cc - 'a' + 'A';
1224 	} else if (ctrlDown) {
1225 		if (cc >= '[' && cc <= '_')
1226 			cc = cc - '@';
1227 		else if (cc == ' ' || cc == '@')
1228 			cc = '\0';
1229 	}
1230 	lastChar = cc;
1231 done:
1232 	return (cc);
1233 }
1234 
1235 /*
1236  * ----------------------------------------------------------------------------
1237  *
1238  * dcKBDReset --
1239  *
1240  *	Reset the keyboard to default characteristics.
1241  *
1242  * Results:
1243  *	None.
1244  *
1245  * Side effects:
1246  *	None.
1247  *
1248  * ----------------------------------------------------------------------------
1249  */
1250 void
1251 dcKBDReset()
1252 {
1253 	register int i;
1254 	static int inKBDReset;
1255 
1256 	if (inKBDReset)
1257 		return;
1258 	inKBDReset = 1;
1259 	for (i = 0; i < sizeof(kbdInitString); i++)
1260 		dcKBDPutc((int)kbdInitString[i]);
1261 	inKBDReset = 0;
1262 }
1263 
1264 /*
1265  * ----------------------------------------------------------------------------
1266  *
1267  * MousePutc --
1268  *
1269  *	Write a character to the mouse.
1270  *	This is only called at initialization time.
1271  *
1272  * Results:
1273  *	None.
1274  *
1275  * Side effects:
1276  *	A character is written to the mouse.
1277  *
1278  * ----------------------------------------------------------------------------
1279  */
1280 static void
1281 MousePutc(c)
1282 	int c;
1283 {
1284 	register dcregs *dcaddr;
1285 	register u_short tcr;
1286 	register int timeout;
1287 	int line;
1288 
1289 	dcaddr = dcpdma[MOUSE_PORT].p_addr;
1290 	tcr = dcaddr->dc_tcr;
1291 	dcaddr->dc_tcr = tcr | (1 << MOUSE_PORT);
1292 	MachEmptyWriteBuffer();
1293 	DELAY(10);
1294 	while (1) {
1295 		/*
1296 		 * Wait for transmitter to be not busy.
1297 		 */
1298 		timeout = 1000000;
1299 		while (!(dcaddr->dc_csr & CSR_TRDY) && timeout > 0)
1300 			timeout--;
1301 		if (timeout == 0) {
1302 			printf("MousePutc: timeout waiting for CSR_TRDY\n");
1303 			break;
1304 		}
1305 		line = (dcaddr->dc_csr >> 8) & 3;
1306 		/*
1307 		 * Check to be sure its the right port.
1308 		 */
1309 		if (line != MOUSE_PORT) {
1310 			tcr |= 1 << line;
1311 			dcaddr->dc_tcr &= ~(1 << line);
1312 			MachEmptyWriteBuffer();
1313 			DELAY(10);
1314 			continue;
1315 		}
1316 		/*
1317 		 * Start sending the character.
1318 		 */
1319 		dcaddr->dc_tdr = dc_brk[0] | (c & 0xff);
1320 		MachEmptyWriteBuffer();
1321 		DELAY(10);
1322 		/*
1323 		 * Wait for character to be sent.
1324 		 */
1325 		while (1) {
1326 			/*
1327 			 * cc -O bug: this code produces and infinite loop!
1328 			 * while (!(dcaddr->dc_csr & CSR_TRDY))
1329 			 *	;
1330 			 */
1331 			timeout = 1000000;
1332 			while (!(dcaddr->dc_csr & CSR_TRDY) && timeout > 0)
1333 				timeout--;
1334 			line = (dcaddr->dc_csr >> 8) & 3;
1335 			if (line != MOUSE_PORT) {
1336 				tcr |= 1 << line;
1337 				dcaddr->dc_tcr &= ~(1 << line);
1338 				MachEmptyWriteBuffer();
1339 				DELAY(10);
1340 				continue;
1341 			}
1342 			dcaddr->dc_tcr &= ~(1 << MOUSE_PORT);
1343 			MachEmptyWriteBuffer();
1344 			DELAY(10);
1345 			break;
1346 		}
1347 		break;
1348 	}
1349 	/*
1350 	 * Enable interrupts for other lines which became ready.
1351 	 */
1352 	if (tcr & 0xF) {
1353 		dcaddr->dc_tcr = tcr;
1354 		MachEmptyWriteBuffer();
1355 		DELAY(10);
1356 	}
1357 }
1358 
1359 /*
1360  * ----------------------------------------------------------------------------
1361  *
1362  * MouseGetc --
1363  *
1364  *	Read a character from the mouse.
1365  *	This is only called at initialization time.
1366  *
1367  * Results:
1368  *	A character read from the mouse, -1 if we timed out waiting.
1369  *
1370  * Side effects:
1371  *	None.
1372  *
1373  * ----------------------------------------------------------------------------
1374  */
1375 static int
1376 MouseGetc()
1377 {
1378 	register dcregs *dcaddr;
1379 	register int timeout;
1380 	register int c;
1381 
1382 	dcaddr = dcpdma[MOUSE_PORT].p_addr;
1383 	for (timeout = 1000000; timeout > 0; timeout--) {
1384 		if (!(dcaddr->dc_csr & CSR_RDONE))
1385 			continue;
1386 		c = dcaddr->dc_rbuf;
1387 		DELAY(10);
1388 		if (((c >> 8) & 03) != MOUSE_PORT)
1389 			continue;
1390 		return (c & 0xff);
1391 	}
1392 
1393 	return (-1);
1394 }
1395 
1396 /*
1397  * ----------------------------------------------------------------------------
1398  *
1399  * MouseInit --
1400  *
1401  *	Initialize the mouse.
1402  *
1403  * Results:
1404  *	None.
1405  *
1406  * Side effects:
1407  *	None.
1408  *
1409  * ----------------------------------------------------------------------------
1410  */
1411 static void
1412 MouseInit()
1413 {
1414 	int id_byte1, id_byte2, id_byte3, id_byte4;
1415 
1416 	/*
1417 	 * Initialize the mouse.
1418 	 */
1419 	MousePutc(MOUSE_SELF_TEST);
1420 	id_byte1 = MouseGetc();
1421 	if (id_byte1 < 0) {
1422 		printf("MouseInit: Timeout on 1st byte of self-test report\n");
1423 		return;
1424 	}
1425 	id_byte2 = MouseGetc();
1426 	if (id_byte2 < 0) {
1427 		printf("MouseInit: Timeout on 2nd byte of self-test report\n");
1428 		return;
1429 	}
1430 	id_byte3 = MouseGetc();
1431 	if (id_byte3 < 0) {
1432 		printf("MouseInit: Timeout on 3rd byte of self-test report\n");
1433 		return;
1434 	}
1435 	id_byte4 = MouseGetc();
1436 	if (id_byte4 < 0) {
1437 		printf("MouseInit: Timeout on 4th byte of self-test report\n");
1438 		return;
1439 	}
1440 	if ((id_byte2 & 0x0f) != 0x2)
1441 		printf("MouseInit: We don't have a mouse!!!\n");
1442 	/*
1443 	 * For some reason, the mouse doesn't see this command if it comes
1444 	 * too soon after a self test.
1445 	 */
1446 	DELAY(100);
1447 	MousePutc(MOUSE_INCREMENTAL);
1448 }
1449 #endif /* NDC */
1450