1 /* $NetBSD: gencons.c,v 1.56 2017/05/22 16:46:15 ragge Exp $ */
2
3 /*
4 * Copyright (c) 1994 Gordon W. Ross
5 * Copyright (c) 1994 Ludd, University of Lule}, Sweden.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * kd.c,v 1.2 1994/05/05 04:46:51 gwr Exp $
31 */
32
33 /* All bugs are subject to removal without further notice */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: gencons.c,v 1.56 2017/05/22 16:46:15 ragge Exp $");
37
38 #include "opt_ddb.h"
39 #include "opt_cputype.h"
40 #include "opt_multiprocessor.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/conf.h>
45 #include <sys/cpu.h>
46 #include <sys/device.h>
47 #include <sys/file.h>
48 #include <sys/ioctl.h>
49 #include <sys/kernel.h>
50 #include <sys/kauth.h>
51 #include <sys/proc.h>
52 #include <sys/tty.h>
53
54 #include <dev/cons.h>
55
56 #include <machine/sid.h>
57 #include <machine/scb.h>
58 #include <vax/vax/gencons.h>
59
60 static struct gc_softc {
61 short alive;
62 short unit;
63 struct tty *gencn_tty;
64 } gc_softc[4];
65
66 static int maxttys = 1;
67
68 static int pr_txcs[4] = {PR_TXCS, PR_TXCS1, PR_TXCS2, PR_TXCS3};
69 static int pr_rxcs[4] = {PR_RXCS, PR_RXCS1, PR_RXCS2, PR_RXCS3};
70 static int pr_txdb[4] = {PR_TXDB, PR_TXDB1, PR_TXDB2, PR_TXDB3};
71 static int pr_rxdb[4] = {PR_RXDB, PR_RXDB1, PR_RXDB2, PR_RXDB3};
72
73 cons_decl(gen);
74
75 static int gencnparam(struct tty *, struct termios *);
76 static void gencnstart(struct tty *);
77
78 dev_type_open(gencnopen);
79 dev_type_close(gencnclose);
80 dev_type_read(gencnread);
81 dev_type_write(gencnwrite);
82 dev_type_ioctl(gencnioctl);
83 dev_type_tty(gencntty);
84 dev_type_poll(gencnpoll);
85
86 const struct cdevsw gen_cdevsw = {
87 .d_open = gencnopen,
88 .d_close = gencnclose,
89 .d_read = gencnread,
90 .d_write = gencnwrite,
91 .d_ioctl = gencnioctl,
92 .d_stop = nostop,
93 .d_tty = gencntty,
94 .d_poll = gencnpoll,
95 .d_mmap = nommap,
96 .d_kqfilter = ttykqfilter,
97 .d_discard = nodiscard,
98 .d_flag = D_TTY
99 };
100
101 int
gencnopen(dev_t dev,int flag,int mode,struct lwp * l)102 gencnopen(dev_t dev, int flag, int mode, struct lwp *l)
103 {
104 int unit;
105 struct tty *tp;
106
107 unit = minor(dev);
108 if (unit >= maxttys)
109 return ENXIO;
110
111 if (gc_softc[unit].gencn_tty == NULL)
112 gc_softc[unit].gencn_tty = tty_alloc();
113
114 gc_softc[unit].alive = 1;
115 gc_softc[unit].unit = unit;
116 tp = gc_softc[unit].gencn_tty;
117
118 tp->t_oproc = gencnstart;
119 tp->t_param = gencnparam;
120 tp->t_dev = dev;
121
122 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
123 return (EBUSY);
124
125 if ((tp->t_state & TS_ISOPEN) == 0) {
126 ttychars(tp);
127 tp->t_iflag = TTYDEF_IFLAG;
128 tp->t_oflag = TTYDEF_OFLAG;
129 tp->t_cflag = TTYDEF_CFLAG;
130 tp->t_lflag = TTYDEF_LFLAG;
131 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
132 gencnparam(tp, &tp->t_termios);
133 ttsetwater(tp);
134 }
135 tp->t_state |= TS_CARR_ON;
136
137 return ((*tp->t_linesw->l_open)(dev, tp));
138 }
139
140 int
gencnclose(dev_t dev,int flag,int mode,struct lwp * l)141 gencnclose(dev_t dev, int flag, int mode, struct lwp *l)
142 {
143 struct tty *tp = gc_softc[minor(dev)].gencn_tty;
144
145 (*tp->t_linesw->l_close)(tp, flag);
146 ttyclose(tp);
147 gc_softc[minor(dev)].alive = 0;
148 return (0);
149 }
150
151 struct tty *
gencntty(dev_t dev)152 gencntty(dev_t dev)
153 {
154 return gc_softc[minor(dev)].gencn_tty;
155 }
156
157 int
gencnread(dev_t dev,struct uio * uio,int flag)158 gencnread(dev_t dev, struct uio *uio, int flag)
159 {
160 struct tty *tp = gc_softc[minor(dev)].gencn_tty;
161
162 return ((*tp->t_linesw->l_read)(tp, uio, flag));
163 }
164
165 int
gencnwrite(dev_t dev,struct uio * uio,int flag)166 gencnwrite(dev_t dev, struct uio *uio, int flag)
167 {
168 struct tty *tp = gc_softc[minor(dev)].gencn_tty;
169
170 return ((*tp->t_linesw->l_write)(tp, uio, flag));
171 }
172
173 int
gencnpoll(dev_t dev,int events,struct lwp * l)174 gencnpoll(dev_t dev, int events, struct lwp *l)
175 {
176 struct tty *tp = gc_softc[minor(dev)].gencn_tty;
177
178 return ((*tp->t_linesw->l_poll)(tp, events, l));
179 }
180
181 int
gencnioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)182 gencnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
183 {
184 struct tty *tp = gc_softc[minor(dev)].gencn_tty;
185 int error;
186
187 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
188 if (error != EPASSTHROUGH)
189 return error;
190 return ttioctl(tp, cmd, data, flag, l);
191 }
192
193 void
gencnstart(struct tty * tp)194 gencnstart(struct tty *tp)
195 {
196 struct clist *cl;
197 int s, ch;
198
199 #if defined(MULTIPROCESSOR)
200 if ((curcpu()->ci_flags & CI_MASTERCPU) == 0)
201 return cpu_send_ipi(IPI_DEST_MASTER, IPI_START_CNTX);
202 #endif
203
204 s = spltty();
205 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
206 goto out;
207 cl = &tp->t_outq;
208
209 if (ttypull(tp)) {
210 tp->t_state |= TS_BUSY;
211 ch = getc(cl);
212 mtpr(ch, pr_txdb[minor(tp->t_dev)]);
213 }
214 out:
215 splx(s);
216 }
217
218 static void
gencnrint(void * arg)219 gencnrint(void *arg)
220 {
221 struct gc_softc *sc = arg;
222 struct tty *tp = sc->gencn_tty;
223 int i;
224
225 if (sc->alive == 0)
226 return;
227 i = mfpr(pr_rxdb[sc->unit]) & 0377; /* Mask status flags etc... */
228 KERNEL_LOCK(1, NULL);
229
230 #ifdef DDB
231 if (tp->t_dev == cn_tab->cn_dev) {
232 int j = kdbrint(i);
233
234 if (j == 1) { /* Escape received, just return */
235 KERNEL_UNLOCK_ONE(NULL);
236 return;
237 }
238
239 if (j == 2) /* Second char wasn't 'D' */
240 (*tp->t_linesw->l_rint)(27, tp);
241 }
242 #endif
243
244 (*tp->t_linesw->l_rint)(i, tp);
245 KERNEL_UNLOCK_ONE(NULL);
246 }
247
248 static void
gencntint(void * arg)249 gencntint(void *arg)
250 {
251 struct gc_softc *sc = arg;
252 struct tty *tp = sc->gencn_tty;
253
254 if (sc->alive == 0)
255 return;
256 KERNEL_LOCK(1, NULL);
257 tp->t_state &= ~TS_BUSY;
258
259 gencnstart(tp);
260 KERNEL_UNLOCK_ONE(NULL);
261 }
262
263 int
gencnparam(struct tty * tp,struct termios * t)264 gencnparam(struct tty *tp, struct termios *t)
265 {
266 /* XXX - These are ignored... */
267 tp->t_ispeed = t->c_ispeed;
268 tp->t_ospeed = t->c_ospeed;
269 tp->t_cflag = t->c_cflag;
270 return 0;
271 }
272
273 void
gencnprobe(struct consdev * cndev)274 gencnprobe(struct consdev *cndev)
275 {
276 if ((vax_cputype < VAX_TYP_UV2) || /* All older has MTPR console */
277 (vax_boardtype == VAX_BTYP_9RR) ||
278 (vax_boardtype == VAX_BTYP_630) ||
279 (vax_boardtype == VAX_BTYP_660) ||
280 (vax_boardtype == VAX_BTYP_670) ||
281 (vax_boardtype == VAX_BTYP_680) ||
282 (vax_boardtype == VAX_BTYP_681) ||
283 (vax_boardtype == VAX_BTYP_650)) {
284 cndev->cn_dev = makedev(cdevsw_lookup_major(&gen_cdevsw), 0);
285 cndev->cn_pri = CN_NORMAL;
286 } else
287 cndev->cn_pri = CN_DEAD;
288 }
289
290 void
gencninit(struct consdev * cndev)291 gencninit(struct consdev *cndev)
292 {
293
294 /* Allocate interrupt vectors */
295 scb_vecalloc(SCB_G0R, gencnrint, &gc_softc[0], SCB_ISTACK, NULL);
296 scb_vecalloc(SCB_G0T, gencntint, &gc_softc[0], SCB_ISTACK, NULL);
297 mtpr(GC_RIE, pr_rxcs[0]); /* Turn on interrupts */
298 mtpr(GC_TIE, pr_txcs[0]);
299
300 if (vax_cputype == VAX_TYP_8SS) {
301 maxttys = 4;
302 scb_vecalloc(SCB_G1R, gencnrint, &gc_softc[1], SCB_ISTACK, NULL);
303 scb_vecalloc(SCB_G1T, gencntint, &gc_softc[1], SCB_ISTACK, NULL);
304
305 scb_vecalloc(SCB_G2R, gencnrint, &gc_softc[2], SCB_ISTACK, NULL);
306 scb_vecalloc(SCB_G2T, gencntint, &gc_softc[2], SCB_ISTACK, NULL);
307
308 scb_vecalloc(SCB_G3R, gencnrint, &gc_softc[3], SCB_ISTACK, NULL);
309 scb_vecalloc(SCB_G3T, gencntint, &gc_softc[3], SCB_ISTACK, NULL);
310 }
311 #if 0
312 mtpr(0, PR_RXCS);
313 mtpr(0, PR_TXCS);
314 mtpr(0, PR_TBIA); /* ??? */
315 #endif
316 }
317
318 void
gencnputc(dev_t dev,int ch)319 gencnputc(dev_t dev, int ch)
320 {
321 #if VAX8800 || VAXANY
322 /*
323 * On KA88 we may get C-S/C-Q from the console.
324 * XXX - this will cause a loop at spltty() in kernel and will
325 * interfere with other console communication. Fortunately
326 * kernel printf's are uncommon.
327 */
328 if (vax_cputype == VAX_TYP_8NN) {
329 int s = spltty();
330
331 while (mfpr(PR_RXCS) & GC_DON) {
332 if ((mfpr(PR_RXDB) & 0x7f) == 19) {
333 while (1) {
334 while ((mfpr(PR_RXCS) & GC_DON) == 0)
335 ;
336 if ((mfpr(PR_RXDB) & 0x7f) == 17)
337 break;
338 }
339 }
340 }
341 splx(s);
342 }
343 #endif
344
345 while ((mfpr(PR_TXCS) & GC_RDY) == 0) /* Wait until xmit ready */
346 ;
347 mtpr(ch, PR_TXDB); /* xmit character */
348 if(ch == 10)
349 gencnputc(dev, 13); /* CR/LF */
350
351 }
352
353 int
gencngetc(dev_t dev)354 gencngetc(dev_t dev)
355 {
356 int i;
357
358 while ((mfpr(PR_RXCS) & GC_DON) == 0) /* Receive chr */
359 ;
360 i = mfpr(PR_RXDB) & 0x7f;
361 if (i == 13)
362 i = 10;
363 return i;
364 }
365
366 void
gencnpollc(dev_t dev,int pollflag)367 gencnpollc(dev_t dev, int pollflag)
368 {
369 if (pollflag) {
370 mtpr(0, PR_RXCS);
371 mtpr(0, PR_TXCS);
372 } else {
373 mtpr(GC_RIE, PR_RXCS);
374 mtpr(GC_TIE, PR_TXCS);
375 }
376 }
377
378 #if defined(MULTIPROCESSOR)
379 void
gencnstarttx(void)380 gencnstarttx(void)
381 {
382 gencnstart(gc_softc[0].gencn_tty);
383 }
384 #endif
385