1 /* $NetBSD: msc.c,v 1.48 2022/10/26 23:55:40 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * - converted from NetBSD Amiga serial driver to A2232 serial driver
32 * by zik 931207
33 * - added ttyflags hooks rfh 940419
34 * - added new style config support rfh 940601
35 * - added code to halt board during memory load so board doesn't flip
36 * out. /dev/reload works now. Also created mschwiflow function so BSD can
37 * attempt to use board RTS flow control now. rfh 950108
38 * - Integrated work from Jukka Marin <jmarin@jmp.fi> and
39 * Timo Rossi <trossi@jyu.fi> The mscmint() code is Jukka's. 950916
40 * Integrated more bug fixes by Jukka Marin <jmarin@jmp.fi> 950918
41 * Also added Jukka's turbo board code. 950918
42 * - Reformatted to NetBSD style format.
43 * - Rewritten the carrier detect system to prevent lock-ups (jm 951029)
44 */
45
46 /*
47 * Copyright (c) 1993 Zik.
48 * Copyright (c) 1995 Jukka Marin <jmarin@jmp.fi>.
49 * Copyright (c) 1995 Timo Rossi <trossi@jyu.fi>.
50 * Copyright (c) 1995 Rob Healey <rhealey@kas.helios.mn.org>.
51 *
52 * Redistribution and use in source and binary forms, with or without
53 * modification, are permitted provided that the following conditions
54 * are met:
55 * 1. Redistributions of source code must retain the above copyright
56 * notice, this list of conditions and the following disclaimer.
57 * 2. Redistributions in binary form must reproduce the above copyright
58 * notice, this list of conditions and the following disclaimer in the
59 * documentation and/or other materials provided with the distribution.
60 * 3. All advertising materials mentioning features or use of this software
61 * must display the following acknowledgement:
62 * This product includes software developed by the University of
63 * California, Berkeley and its contributors.
64 * 4. Neither the name of the University nor the names of its contributors
65 * may be used to endorse or promote products derived from this software
66 * without specific prior written permission.
67 *
68 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78 * SUCH DAMAGE.
79 *
80 * - converted from NetBSD Amiga serial driver to A2232 serial driver
81 * by zik 931207
82 * - added ttyflags hooks rfh 940419
83 * - added new style config support rfh 940601
84 * - added code to halt board during memory load so board doesn't flip
85 * out. /dev/reload works now. Also created mschwiflow function so BSD can
86 * attempt to use board RTS flow control now. rfh 950108
87 * - Integrated work from Jukka Marin <jmarin@jmp.fi> and
88 * Timo Rossi <trossi@jyu.fi> The mscmint() code is Jukka's. 950916
89 * Integrated more bug fixes by Jukka Marin <jmarin@jmp.fi> 950918
90 * Also added Jukka's turbo board code. 950918
91 * - Reformatted to NetBSD style format.
92 * - Rewritten the carrier detect system to prevent lock-ups (jm 951029)
93 */
94
95 #include <sys/cdefs.h>
96 __KERNEL_RCSID(0, "$NetBSD: msc.c,v 1.48 2022/10/26 23:55:40 riastradh Exp $");
97
98 #include "msc.h"
99
100 #if NMSC > 0
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/ioctl.h>
104 #include <sys/tty.h>
105 #include <sys/proc.h>
106 #include <sys/file.h>
107 #include <sys/uio.h>
108 #include <sys/kernel.h>
109 #include <sys/syslog.h>
110 #include <sys/device.h>
111 #include <sys/conf.h>
112 #include <sys/kauth.h>
113
114 #include <amiga/amiga/device.h>
115 #include <amiga/dev/zbusvar.h>
116 #include <amiga/dev/mscreg.h>
117 #include <machine/cpu.h>
118
119 #include <amiga/amiga/custom.h>
120 #include <amiga/amiga/cia.h>
121 #include <amiga/amiga/cc.h>
122
123 /* 6502 code for A2232 card */
124 #include "msc6502.h"
125
126 /*
127 * Note: These are Zik's original comments:
128 * There is a bit of a "gotcha" concerning the numbering
129 * of msc devices and the specification of the number of serial
130 * lines in the kernel.
131 *
132 * Each board has seven lines, but for programming convenience
133 * and compatibility with Amiga UNIX the boards' minor device
134 * numbers are allocated in groups of sixteen:
135 *
136 * minor device numbers
137 * First board 0 2 4 6 8 10 12
138 * Second board 16 18 20 22 24 26 28
139 * Third board 32 34 36 38 40 42 44
140 *
141 * The intermediate minor device numbers are dialin versions
142 * of the same devices. ie. 10 is dialout line 6 and 11 is
143 * the dialin version of line 6.
144 *
145 * On the other hand, I have made the NMSC config option refer
146 * to the total number of a2232 cards, not the maximum
147 * minor device number. So you might have NMSC=3, in which case
148 * you have three boards with minor device numbers from 0 to 45.
149 */
150
151 int mscparam(struct tty *, struct termios *);
152 void mscstart(struct tty *);
153 int mschwiflow(struct tty *, int);
154 int mscinitcard(struct zbus_args *);
155
156 int mscdefaultrate = TTYDEF_SPEED;
157
158 struct mscdevice mscdev[MSCSLOTS]; /* device structs for all lines */
159 struct tty *msc_tty[MSCTTYS]; /* ttys for all lines */
160
161 struct vbl_node msc_vbl_node[NMSC]; /* vbl interrupt node per board */
162
163 const struct speedtab mscspeedtab_normal[] = {
164 { 0, 0 },
165 { 50, MSCPARAM_B50 },
166 { 75, MSCPARAM_B75 },
167 { 110, MSCPARAM_B110 },
168 { 134, MSCPARAM_B134 },
169 { 150, MSCPARAM_B150 },
170 { 300, MSCPARAM_B300 },
171 { 600, MSCPARAM_B600 },
172 { 1200, MSCPARAM_B1200 },
173 { 1800, MSCPARAM_B1800 },
174 { 2400, MSCPARAM_B2400 },
175 { 3600, MSCPARAM_B3600 },
176 { 4800, MSCPARAM_B4800 },
177 { 7200, MSCPARAM_B7200 },
178 { 9600, MSCPARAM_B9600 },
179 { 19200, MSCPARAM_B19200 },
180 { 115200, MSCPARAM_B115200 },
181 { -1, -1 }
182 };
183
184 const struct speedtab mscspeedtab_turbo[] = {
185 { 0, 0 },
186 { 100, MSCPARAM_B50 },
187 { 150, MSCPARAM_B75 },
188 { 220, MSCPARAM_B110 },
189 { 269, MSCPARAM_B134 },
190 { 300, MSCPARAM_B150 },
191 { 600, MSCPARAM_B300 },
192 { 1200, MSCPARAM_B600 },
193 { 2400, MSCPARAM_B1200 },
194 { 3600, MSCPARAM_B1800 },
195 { 4800, MSCPARAM_B2400 },
196 { 7200, MSCPARAM_B3600 },
197 { 9600, MSCPARAM_B4800 },
198 { 14400, MSCPARAM_B7200 },
199 { 19200, MSCPARAM_B9600 },
200 { 38400, MSCPARAM_B19200 },
201 { 230400, MSCPARAM_B115200 },
202 { -1, -1 }
203 };
204
205 const struct speedtab *mscspeedtab;
206
207 int mscmctl(dev_t dev, int bits, int howto);
208 void mscmint(register void *data);
209
210 int mscmatch(device_t, cfdata_t, void *);
211 void mscattach(device_t, device_t, void *);
212
213 #define SWFLAGS(dev) (msc->openflags | (MSCDIALIN(dev) ? 0 : TIOCFLAG_SOFTCAR))
214 #define DEBUG_CD 0
215
216 CFATTACH_DECL_NEW(msc, 0,
217 mscmatch, mscattach, NULL, NULL);
218
219 dev_type_open(mscopen);
220 dev_type_close(mscclose);
221 dev_type_read(mscread);
222 dev_type_write(mscwrite);
223 dev_type_ioctl(mscioctl);
224 dev_type_stop(mscstop);
225 dev_type_tty(msctty);
226 dev_type_poll(mscpoll);
227
228 const struct cdevsw msc_cdevsw = {
229 .d_open = mscopen,
230 .d_close = mscclose,
231 .d_read = mscread,
232 .d_write = mscwrite,
233 .d_ioctl = mscioctl,
234 .d_stop = mscstop,
235 .d_tty = msctty,
236 .d_poll = mscpoll,
237 .d_mmap = nommap,
238 .d_kqfilter = ttykqfilter,
239 .d_discard = nodiscard,
240 .d_flag = D_TTY
241 };
242
243 int
mscmatch(device_t parent,cfdata_t cf,void * aux)244 mscmatch(device_t parent, cfdata_t cf, void *aux)
245 {
246 struct zbus_args *zap;
247
248 zap = aux;
249 if (zap->manid == 514 && (zap->prodid == 70 || zap->prodid == 69))
250 return(1);
251
252 return (0);
253 }
254
255 void
mscattach(device_t parent,device_t self,void * aux)256 mscattach(device_t parent, device_t self, void *aux)
257 {
258 volatile struct mscmemory *mscmem;
259 struct mscdevice *msc;
260 struct zbus_args *zap;
261 int unit;
262 int Count;
263
264 zap = aux;
265 unit = device_unit(self);
266
267 /*
268 * Make config msgs look nicer.
269 */
270 printf("\n");
271
272 if (mscinitcard(zap) != 0) {
273 printf("msc%d: Board initialize failed, bad download code.\n", unit);
274 return;
275 }
276
277 printf("msc%d: Board successfully initialized.\n", unit);
278
279 mscmem = (struct mscmemory *) zap->va;
280
281 if (mscmem->Common.Crystal == MSC_UNKNOWN) {
282 printf("msc%d: Unable to detect crystal frequency.\n", unit);
283 return;
284 }
285
286 if (mscmem->Common.Crystal == MSC_TURBO) {
287 printf("msc%d: Turbo version detected (%02x%02x:%d)\n", unit,
288 mscmem->Common.TimerH, mscmem->Common.TimerL,
289 mscmem->Common.Pad_a);
290 mscspeedtab = mscspeedtab_turbo;
291 } else {
292 printf("msc%d: Normal version detected (%02x%02x:%d)\n", unit,
293 mscmem->Common.TimerH, mscmem->Common.TimerL,
294 mscmem->Common.Pad_a);
295 mscspeedtab = mscspeedtab_normal;
296 }
297
298 mscmem->Common.CDStatus = 0; /* common status for all 7 ports */
299
300 /* XXX 8 is a constant */
301 for (Count = 0; Count < 8 && MSCSLOTUL(unit, Count) < MSCSLOTS; Count++) {
302 msc = &mscdev[MSCSLOTUL(unit, Count)];
303 msc->board = mscmem;
304 msc->port = Count;
305 msc->flags = 0;
306 msc->openflags = 0;
307 msc->active = 1;
308 msc->unit = unit;
309 msc->closing = false;
310 msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, Count))] = NULL;
311 msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, Count)) + 1] = NULL;
312 }
313
314 /* disable the non-existent eighth port */
315 if (MSCSLOTUL(unit, NUMLINES) < MSCSLOTS)
316 mscdev[MSCSLOTUL(unit, NUMLINES)].active = 0;
317
318 msc_vbl_node[unit].function = (void (*) (void *)) mscmint;
319 msc_vbl_node[unit].data = (void *) unit;
320
321 add_vbl_function (&msc_vbl_node[unit], MSC_VBL_PRIORITY, (void *)unit);
322
323 return;
324 }
325
326 /* ARGSUSED */
327 int
mscopen(dev_t dev,int flag,int mode,struct lwp * l)328 mscopen(dev_t dev, int flag, int mode, struct lwp *l)
329 {
330 register struct tty *tp;
331 struct mscdevice *msc;
332 volatile struct mscstatus *ms;
333 int error = 0;
334 int s, slot, ttyn;
335
336 /* get the device structure */
337 slot = MSCSLOT(dev);
338 ttyn = MSCTTY(dev);
339
340 if (slot >= MSCSLOTS)
341 return ENXIO;
342
343 if (MSCLINE(dev) >= NUMLINES)
344 return ENXIO;
345
346 msc = &mscdev[slot];
347 ms = &msc->board->Status[msc->port];
348
349 if (!msc->active)
350 return ENXIO;
351
352 /*
353 * RFH: WHY here? Put down by while like other serial drivers
354 * But if we do that it makes things bomb.
355 */
356 s = spltty();
357
358 if (!msc_tty[ttyn]) {
359
360 tp = tty_alloc();
361 tty_attach(tp);
362 msc_tty[ttyn] = tp;
363 msc_tty[ttyn+1] = (struct tty *)NULL;
364
365 #if 0
366 /* default values are not optimal for this device, increase buffers. */
367 clfree(&tp->t_rawq);
368 clfree(&tp->t_canq);
369 clfree(&tp->t_outq);
370 clalloc(&tp->t_rawq, 8192, 1);
371 clalloc(&tp->t_canq, 8192, 1);
372 clalloc(&tp->t_outq, 8192, 0);
373 #endif
374
375 } else
376 tp = msc_tty[ttyn];
377
378 tp->t_oproc = (void (*) (struct tty *)) mscstart;
379 tp->t_param = mscparam;
380 tp->t_dev = dev;
381 tp->t_hwiflow = mschwiflow;
382
383 /* if port is still closing, just bitbucket remaining characters */
384 if (msc->closing) {
385 ms->OutFlush = true;
386 msc->closing = false;
387 }
388 splx(s);
389
390 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
391 return (EBUSY);
392
393 ttylock(tp);
394 /* initialize tty */
395 if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
396 ttychars(tp);
397 if (tp->t_ispeed == 0) {
398 tp->t_iflag = TTYDEF_IFLAG;
399 tp->t_oflag = TTYDEF_OFLAG;
400 tp->t_cflag = TTYDEF_CFLAG;
401 tp->t_lflag = TTYDEF_LFLAG;
402 tp->t_ispeed = tp->t_ospeed = mscdefaultrate;
403 }
404
405 /* flags changed to be private to every unit by JM */
406 if (msc->openflags & TIOCFLAG_CLOCAL)
407 tp->t_cflag |= CLOCAL;
408 if (msc->openflags & TIOCFLAG_CRTSCTS)
409 tp->t_cflag |= CRTSCTS;
410 if (msc->openflags & TIOCFLAG_MDMBUF)
411 tp->t_cflag |= MDMBUF;
412
413 mscparam(tp, &tp->t_termios);
414 ttsetwater(tp);
415
416 (void) mscmctl(dev, TIOCM_DTR | TIOCM_RTS, DMSET);
417
418 if ((SWFLAGS(dev) & TIOCFLAG_SOFTCAR) ||
419 (mscmctl(dev, 0, DMGET) & TIOCM_CD))
420 tp->t_state |= TS_CARR_ON;
421 else
422 tp->t_state &= ~TS_CARR_ON;
423
424 }
425
426 /*
427 * if NONBLOCK requested, ignore carrier
428 */
429 if (flag & O_NONBLOCK) {
430 #if DEBUG_CD
431 printf("msc%d: %d open nonblock\n", msc->unit, MSCLINE(dev));
432 #endif
433 goto done;
434 }
435
436 /*
437 * s = spltty();
438 *
439 * This causes hangs when put here, like other TTY drivers do, rather than
440 * above, WHY? RFH
441 *
442 */
443
444 while ((tp->t_state & TS_CARR_ON) == 0 && (tp->t_cflag & CLOCAL) == 0) {
445 tp->t_wopen++;
446
447 #if DEBUG_CD
448 printf("msc%d: %d waiting for CD\n", msc->unit, MSCLINE(dev));
449 #endif
450 error = ttysleep(tp, &tp->t_rawcv, true, 0);
451 tp->t_wopen--;
452
453 if (error) {
454 ttyunlock(tp);
455 return(error);
456 }
457 }
458
459 #if DEBUG_CD
460 printf("msc%d: %d got CD\n", msc->unit, MSCLINE(dev));
461 #endif
462
463 done:
464 /* This is a way to handle lost XON characters */
465 if ((flag & O_TRUNC) && (tp->t_state & TS_TTSTOP)) {
466 tp->t_state &= ~TS_TTSTOP;
467 ttstart (tp);
468 }
469
470 /*
471 * Reset the tty pointer, as there could have been a dialout
472 * use of the tty with a dialin open waiting.
473 */
474 tp->t_dev = dev;
475 ttyunlock(tp);
476
477 return tp->t_linesw->l_open(dev, tp);
478 }
479
480
481 int
mscclose(dev_t dev,int flag,int mode,struct lwp * l)482 mscclose(dev_t dev, int flag, int mode, struct lwp *l)
483 {
484 register struct tty *tp;
485 int slot;
486 volatile struct mscstatus *ms;
487 struct mscdevice *msc;
488
489 /* get the device structure */
490 slot = MSCSLOT(dev);
491
492 if (slot >= MSCSLOTS)
493 return ENXIO;
494
495 msc = &mscdev[slot];
496
497 if (!msc->active)
498 return ENXIO;
499
500 ms = &msc->board->Status[msc->port];
501
502 tp = msc_tty[MSCTTY(dev)];
503 tp->t_linesw->l_close(tp, flag);
504
505 (void) mscmctl(dev, 0, DMSET);
506
507 ttyclose(tp);
508
509 if (msc->flags & TIOCM_DTR)
510 msc->closing = true; /* flush remaining characters before dropping DTR */
511 else
512 ms->OutFlush = true; /* just bitbucket remaining characters */
513
514 return (0);
515 }
516
517
518 int
mscread(dev_t dev,struct uio * uio,int flag)519 mscread(dev_t dev, struct uio *uio, int flag)
520 {
521 register struct tty *tp;
522
523 tp = msc_tty[MSCTTY(dev)];
524
525 if (! tp)
526 return ENXIO;
527
528 return tp->t_linesw->l_read(tp, uio, flag);
529 }
530
531
532 int
mscwrite(dev_t dev,struct uio * uio,int flag)533 mscwrite(dev_t dev, struct uio *uio, int flag)
534 {
535 register struct tty *tp;
536
537 tp = msc_tty[MSCTTY(dev)];
538
539 if (! tp)
540 return ENXIO;
541
542 return tp->t_linesw->l_write(tp, uio, flag);
543 }
544
545 int
mscpoll(dev_t dev,int events,struct lwp * l)546 mscpoll(dev_t dev, int events, struct lwp *l)
547 {
548 register struct tty *tp;
549
550 tp = msc_tty[MSCTTY(dev)];
551
552 if (! tp)
553 return ENXIO;
554
555 return ((*tp->t_linesw->l_poll)(tp, events, l));
556 }
557
558 /*
559 * This interrupt is periodically invoked in the vertical blank
560 * interrupt. It's used to keep track of the modem control lines
561 * and (new with the fast_int code) to move accumulated data up in
562 * to the tty layer.
563 *
564 * NOTE: MSCCDHACK is an invention of mine for dubious purposes. If you
565 * want to activate it add
566 * options MSCCDHACK
567 * in the kernel conf file. Basically it forces CD->Low transitions
568 * to ALWAYS send a signal to the process, even if the device is in
569 * clocal mode or an outdial device. RFH
570 */
571 void
mscmint(register void * data)572 mscmint(register void *data)
573 {
574 register struct tty *tp;
575 struct mscdevice *msc;
576 volatile struct mscstatus *ms;
577 volatile u_char *ibuf, *cbuf;
578 unsigned char newhead; /* was int */
579 unsigned char bufpos; /* was int */
580 unsigned char ncd, ocd, ccd;
581 int unit, slot, maxslot;
582 int s, i;
583
584 unit = (int) data;
585
586 /* check each line on this board */
587 maxslot = MSCSLOTUL(unit, NUMLINES);
588 if (maxslot > MSCSLOTS)
589 maxslot = MSCSLOTS;
590
591 msc = &mscdev[MSCSLOTUL(unit, 0)];
592
593 newhead = msc->board->Common.CDHead;
594 bufpos = msc->board->Common.CDTail;
595 if (newhead != bufpos) { /* CD events in queue */
596 /* set interrupt priority level */
597 s = spltty();
598 ocd = msc->board->Common.CDStatus; /* get old status bits */
599 while (newhead != bufpos) { /* read all events */
600 ncd = msc->board->CDBuf[bufpos++]; /* get one event */
601 ccd = ncd ^ ocd; /* mask of changed lines*/
602 ocd = ncd; /* save new status bits */
603 #if DEBUG_CD
604 printf("ocd %02x ncd %02x ccd %02x\n", ocd, ncd, ccd);
605 #endif
606 for(i = 0; i < NUMLINES; i++) { /* do for all lines */
607 if (ccd & 1) { /* this one changed */
608 msc = &mscdev[MSCSLOTUL(unit, i)];
609 if (ncd & 1) { /* CD is now OFF */
610 #if DEBUG_CD
611 printf("msc%d: CD OFF %d\n", unit, msc->port);
612 #endif
613 msc->flags &= ~TIOCM_CD;
614 if ((tp = msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, i))]) &&
615 (tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
616
617 #ifndef MSCCDHACK
618 if (MSCDIALIN(tp->t_dev))
619 #endif
620 {
621 if (tp->t_linesw->l_modem(tp, 0) == 0) {
622 /* clear RTS and DTR, bitbucket output */
623 ms = &msc->board->Status[msc->port];
624 ms->Command = (ms->Command & ~MSCCMD_CMask) |
625 MSCCMD_Close;
626 ms->Setup = true;
627 msc->flags &= ~(TIOCM_DTR | TIOCM_RTS);
628 ms->OutFlush = true;
629 }
630 }
631 }
632 } else { /* CD is now ON */
633 #if DEBUG_CD
634 printf("msc%d: CD ON %d\n", unit, msc->port);
635 #endif
636 msc->flags |= TIOCM_CD;
637 if ((tp = msc_tty[MSCTTYSLOT(MSCSLOTUL(unit, i))]) &&
638 (tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
639 if (MSCDIALIN(tp->t_dev))
640 tp->t_linesw->l_modem(tp, 1);
641 } /* if tp valid and port open */
642 } /* CD on/off */
643 } /* if CD changed for this line */
644 ccd >>= 1; ncd >>= 1; /* bit for next line */
645 } /* for every line */
646 } /* while events in queue */
647 msc->board->Common.CDStatus = ocd; /* save new status */
648 msc->board->Common.CDTail = bufpos; /* remove events */
649 splx(s);
650 } /* if events in CD queue */
651
652 for (slot = MSCSLOTUL(unit, 0); slot < maxslot; slot++) {
653 msc = &mscdev[slot];
654
655 if (!msc->active)
656 continue;
657
658 tp = msc_tty[MSCTTYSLOT(slot)];
659 ms = &msc->board->Status[msc->port];
660
661 newhead = ms->InHead; /* 65c02 write pointer */
662
663 /* yoohoo, is the port open? */
664 if (tp && (tp->t_state & TS_ISOPEN) && tp->t_wopen == 0) {
665 /* port is open, handle all type of events */
666
667 /* set interrupt priority level */
668 s = spltty();
669
670 /* check for input for this port */
671 if (newhead != (bufpos = ms->InTail)) {
672 /* buffer for input chars/events */
673 ibuf = &msc->board->InBuf[msc->port][0];
674
675 /* data types of bytes in ibuf */
676 cbuf = &msc->board->InCtl[msc->port][0];
677
678 /* do for all chars, if room */
679 while (bufpos != newhead) {
680 /* which type of input data? */
681 switch (cbuf[bufpos]) {
682 /* input event (CD, BREAK, etc.) */
683 case MSCINCTL_EVENT:
684 switch (ibuf[bufpos++]) {
685 case MSCEVENT_Break:
686 tp->t_linesw->l_rint(TTY_FE, tp);
687 break;
688
689 default:
690 printf("msc%d: unknown event type %d\n",
691 msc->unit, ibuf[(bufpos-1)&0xff]);
692 } /* event type switch */
693 break;
694
695 case MSCINCTL_CHAR:
696 if (tp->t_state & TS_TBLOCK) {
697 goto NoRoomForYa;
698 }
699 tp->t_linesw->l_rint((int)ibuf[bufpos++], tp);
700 break;
701
702 default:
703 printf("msc%d: unknown data type %d\n",
704 msc->unit, cbuf[bufpos]);
705 bufpos++;
706 } /* switch on input data type */
707 } /* while there's something in the buffer */
708 NoRoomForYa:
709 ms->InTail = bufpos; /* tell 65C02 what we've read */
710 } /* if there was something in the buffer */
711
712 /* we get here only when the port is open */
713 /* send output */
714 if (tp->t_state & (TS_BUSY|TS_FLUSH)) {
715
716 bufpos = ms->OutHead - ms->OutTail;
717
718 /* busy and below low water mark? */
719 if (tp->t_state & TS_BUSY) {
720 if (bufpos < IOBUFLOWWATER) {
721 tp->t_state &= ~TS_BUSY; /* not busy any more */
722 if (tp->t_linesw)
723 tp->t_linesw->l_start(tp);
724 else
725 mscstart(tp);
726 }
727 }
728
729 /* waiting for flush and buffer empty? */
730 if (tp->t_state & TS_FLUSH) {
731 if (bufpos == 0)
732 tp->t_state &= ~TS_FLUSH; /* finished flushing */
733 }
734 } /* BUSY or FLUSH */
735
736 splx(s);
737
738 } else { /* End of port open */
739 /* port is closed, don't pass on the chars from it */
740
741 /* check for input for this port */
742 if (newhead != (bufpos = ms->InTail)) {
743 /* buffer for input chars/events */
744 ibuf = &msc->board->InBuf[msc->port][0];
745
746 /* data types of bytes in ibuf */
747 cbuf = &msc->board->InCtl[msc->port][0];
748
749 /* do for all chars, if room */
750 while (bufpos != newhead) {
751 /* which type of input data? */
752 switch (cbuf[bufpos]) {
753 /* input event (BREAK, etc.) */
754 case MSCINCTL_EVENT:
755 switch (ibuf[bufpos++]) {
756 default:
757 printf("msc: unknown event type %d\n",
758 ibuf[(bufpos-1)&0xff]);
759 } /* event type switch */
760 break;
761
762 default:
763 bufpos++;
764 } /* switch on input data type */
765 } /* while there's something in the buffer */
766
767 ms->InTail = bufpos; /* tell 65C02 what we've read */
768 } /* if there was something in the buffer */
769 } /* End of port open/close */
770
771 /* is this port closing? */
772 if (msc->closing) {
773 /* if DTR is off, just bitbucket remaining characters */
774 if ( (msc->flags & TIOCM_DTR) == 0) {
775 ms->OutFlush = true;
776 msc->closing = false;
777 }
778 /* if output has drained, drop DTR */
779 else if (ms->OutHead == ms->OutTail) {
780 (void) mscmctl(tp->t_dev, 0, DMSET);
781 msc->closing = false;
782 }
783 }
784 } /* For all ports */
785 }
786
787
788 int
mscioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)789 mscioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
790 {
791 register struct tty *tp;
792 register int slot;
793 register int error;
794 struct mscdevice *msc;
795 volatile struct mscstatus *ms;
796 int s;
797
798 /* get the device structure */
799 slot = MSCSLOT(dev);
800
801 if (slot >= MSCSLOTS)
802 return ENXIO;
803
804 msc = &mscdev[slot];
805
806 if (!msc->active)
807 return ENXIO;
808
809 ms = &msc->board->Status[msc->port];
810 if (!(tp = msc_tty[MSCTTY(dev)]))
811 return ENXIO;
812
813 error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l);
814 if (error != EPASSTHROUGH)
815 return (error);
816
817 error = ttioctl(tp, cmd, data, flag, l);
818 if (error != EPASSTHROUGH)
819 return (error);
820
821 switch (cmd) {
822
823 /* send break */
824 case TIOCSBRK:
825 s = spltty();
826 ms->Command = (ms->Command & (~MSCCMD_RTSMask)) | MSCCMD_Break;
827 ms->Setup = true;
828 splx(s);
829 break;
830
831 /* clear break */
832 case TIOCCBRK:
833 s = spltty();
834 ms->Command = (ms->Command & (~MSCCMD_RTSMask)) | MSCCMD_RTSOn;
835 ms->Setup = true;
836 splx(s);
837 break;
838
839 case TIOCSDTR:
840 (void) mscmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIS);
841 break;
842
843 case TIOCCDTR:
844 if (!MSCDIALIN(dev)) /* don't let dialins drop DTR */
845 (void) mscmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIC);
846 break;
847
848 case TIOCMSET:
849 (void) mscmctl(dev, *(int *)data, DMSET);
850 break;
851
852 case TIOCMBIS:
853 (void) mscmctl(dev, *(int *)data, DMBIS);
854 break;
855
856 case TIOCMBIC:
857 if (MSCDIALIN(dev)) /* don't let dialins drop DTR */
858 (void) mscmctl(dev, *(int *)data & TIOCM_DTR, DMBIC);
859 else
860 (void) mscmctl(dev, *(int *)data, DMBIC);
861 break;
862
863 case TIOCMGET:
864 *(int *)data = mscmctl(dev, 0, DMGET);
865 break;
866
867 case TIOCGFLAGS:
868 *(int *)data = SWFLAGS(dev);
869 break;
870
871 case TIOCSFLAGS:
872 error = kauth_authorize_device_tty(l->l_cred,
873 KAUTH_DEVICE_TTY_PRIVSET, tp);
874 if (error != 0)
875 return(EPERM);
876 msc->openflags = *(int *)data;
877 /* only allow valid flags */
878 msc->openflags &=
879 (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | TIOCFLAG_CRTSCTS);
880 break;
881
882 default:
883 return (EPASSTHROUGH);
884 }
885
886 return (0);
887 }
888
889
890 int
mscparam(register struct tty * tp,register struct termios * t)891 mscparam(register struct tty *tp, register struct termios *t)
892 {
893 register int cflag = t->c_cflag;
894 struct mscdevice *msc;
895 volatile struct mscstatus *ms;
896 int s, slot;
897 int ospeed = ttspeedtab(t->c_ospeed, mscspeedtab);
898
899 /* get the device structure */
900 slot = MSCSLOT(tp->t_dev);
901
902 if (slot >= MSCSLOTS)
903 return ENXIO;
904
905 msc = &mscdev[slot];
906
907 if (!msc->active)
908 return ENXIO;
909
910 ms = &msc->board->Status[msc->port];
911
912 /* check requested parameters */
913 if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
914 return (EINVAL);
915
916 /* and copy to tty */
917 tp->t_ispeed = t->c_ispeed;
918 tp->t_ospeed = t->c_ospeed;
919 tp->t_cflag = cflag;
920
921 /* hang up if baud is zero */
922 if (t->c_ospeed == 0) {
923 if (!MSCDIALIN(tp->t_dev)) /* don't let dialins drop DTR */
924 (void) mscmctl(tp->t_dev, 0, DMSET);
925 } else {
926 /* set the baud rate */
927 s = spltty();
928 ms->Param = (ms->Param & ~MSCPARAM_BaudMask) | ospeed | MSCPARAM_RcvBaud;
929
930 /*
931 * Make sure any previous hangup is undone, ie. reenable DTR.
932 * also mscmctl will cause the speed to be set
933 */
934 (void) mscmctl (tp->t_dev, TIOCM_DTR | TIOCM_RTS, DMSET);
935
936 splx(s);
937 }
938
939 return(0);
940 }
941
942
943 /*
944 * Jukka's code initializes alot of stuff that other drivers don't
945 * I'm including it here so that this code is a common set of work
946 * done by both of us. rfh
947 */
948 int
mschwiflow(struct tty * tp,int flag)949 mschwiflow(struct tty *tp, int flag)
950 {
951
952 /* Rob's version */
953 #if 1
954 if (flag)
955 mscmctl( tp->t_dev, TIOCM_RTS, DMBIC); /* Clear/Lower RTS */
956 else
957 mscmctl( tp->t_dev, TIOCM_RTS, DMBIS); /* Set/Raise RTS */
958
959 #else /* Jukka's version */
960
961 int s, slot;
962 struct mscdevice *msc;
963 volatile struct mscstatus *ms;
964
965 /* get the device structure */
966 slot = MSCSLOT(tp->t_dev);
967 if (slot >= MSCSLOTS)
968 return ENXIO;
969 msc = &mscdev[slot];
970 if (!msc->active)
971 return ENXIO;
972 ms = &msc->board->Status[msc->port];
973
974 /* Well, we should really _do_ something here, but the 65c02 code
975 * manages the RTS signal on its own now, so... This will probably
976 * change in the future.
977 */
978 #endif
979 return 1;
980 }
981
982
983 void
mscstart(register struct tty * tp)984 mscstart(register struct tty *tp)
985 {
986 register int cc;
987 register char *cp;
988 register int mhead;
989 int s, slot;
990 struct mscdevice *msc;
991 volatile struct mscstatus *ms;
992 volatile char *mob;
993 int hiwat = 0;
994 int maxout;
995
996 if (! (tp->t_state & TS_ISOPEN))
997 return;
998
999 slot = MSCSLOT(tp->t_dev);
1000
1001 #if 0
1002 printf("starting msc%d\n", slot);
1003 #endif
1004
1005 s = spltty();
1006
1007 /* don't start if explicitly stopped */
1008 if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP))
1009 goto out;
1010
1011 /* wake up if below low water */
1012 cc = tp->t_outq.c_cc;
1013 if (!ttypull(tp) || (tp->t_state & TS_BUSY))
1014 goto out;
1015
1016 /*
1017 * Limit the amount of output we do in one burst
1018 */
1019 msc = &mscdev[slot];
1020 ms = &msc->board->Status[msc->port];
1021 mhead = ms->OutHead;
1022 maxout = mhead - ms->OutTail;
1023
1024 if (maxout < 0)
1025 maxout += IOBUFLEN;
1026
1027 maxout = IOBUFLEN - 1 - maxout;
1028
1029 if (cc >= maxout) {
1030 hiwat++;
1031 cc = maxout;
1032 }
1033
1034 cc = q_to_b (&tp->t_outq, msc->tmpbuf, cc);
1035
1036 if (cc > 0) {
1037 tp->t_state |= TS_BUSY;
1038
1039 mob = &msc->board->OutBuf[msc->port][0];
1040 cp = &msc->tmpbuf[0];
1041
1042 /* enable output */
1043 ms->OutDisable = false;
1044
1045 #if 0
1046 msc->tmpbuf[cc] = 0;
1047 printf("sending '%s'\n", msctmpbuf);
1048 #endif
1049
1050 /* send the first char across to reduce latency */
1051 mob[mhead++] = *cp++;
1052 mhead &= IOBUFLENMASK;
1053 ms->OutHead = mhead;
1054 cc--;
1055
1056 /* copy the rest of the chars across quickly */
1057 while (cc > 0) {
1058 mob[mhead++] = *cp++;
1059 mhead &= IOBUFLENMASK;
1060 cc--;
1061 }
1062 ms->OutHead = mhead;
1063
1064 /* leave the device busy if we've filled the buffer */
1065 if (!hiwat)
1066 tp->t_state &= ~TS_BUSY;
1067 }
1068
1069 out:
1070 splx(s);
1071 }
1072
1073
1074 /* XXX */
1075 /*
1076 * Stop output on a line.
1077 */
1078 /*ARGSUSED*/
1079 void
mscstop(register struct tty * tp,int flag)1080 mscstop(register struct tty *tp, int flag)
1081 /* flag variable defaulted to int anyway */
1082 {
1083 register int s;
1084 #if 0
1085 struct mscdevice *msc;
1086 volatile struct mscstatus *ms;
1087 #endif
1088
1089 s = spltty();
1090 if (tp->t_state & TS_BUSY) {
1091 if ((tp->t_state & TS_TTSTOP) == 0) {
1092 tp->t_state |= TS_FLUSH;
1093 #if 0
1094 msc = &mscdev[MSCSLOT(tp->t_dev)];
1095 ms = &msc->board->Status[msc->port];
1096 printf("stopped output on msc%d\n", MSCSLOT(tp->t_dev));
1097 ms->OutDisable = true;
1098 #endif
1099 }
1100 }
1101 splx(s);
1102 }
1103
1104
1105 /*
1106 * bits can be: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, TIOCM_CD, TIOCM_RI, TIOCM_DSR
1107 */
1108 int
mscmctl(dev_t dev,int bits,int how)1109 mscmctl(dev_t dev, int bits, int how)
1110 {
1111 struct mscdevice *msc;
1112 volatile struct mscstatus *ms;
1113 int slot;
1114 int s;
1115 u_char newcmd;
1116 int OldFlags;
1117
1118 /* get the device structure */
1119 slot = MSCSLOT(dev);
1120
1121 if (slot >= MSCSLOTS)
1122 return ENXIO;
1123
1124 msc = &mscdev[slot];
1125
1126 if (!msc->active)
1127 return ENXIO;
1128
1129 s = spltty();
1130
1131 if (how != DMGET) {
1132 OldFlags = msc->flags;
1133 bits &= TIOCM_DTR | TIOCM_RTS; /* can only modify DTR and RTS */
1134
1135 switch (how) {
1136 case DMSET:
1137 msc->flags = (bits | (msc->flags & ~(TIOCM_DTR | TIOCM_RTS)));
1138 break;
1139
1140 case DMBIC:
1141 msc->flags &= ~bits;
1142 break;
1143
1144 case DMBIS:
1145 msc->flags |= bits;
1146 break;
1147 }
1148
1149 /* modify modem control state */
1150 ms = &msc->board->Status[msc->port];
1151
1152 if (msc->flags & TIOCM_RTS) /* was bits & */
1153 newcmd = MSCCMD_RTSOn;
1154 else /* this doesn't actually work now */
1155 newcmd = MSCCMD_RTSOff;
1156
1157 if (msc->flags & TIOCM_DTR) /* was bits & */
1158 newcmd |= MSCCMD_Enable;
1159
1160 ms->Command = (ms->Command & (~MSCCMD_RTSMask & ~MSCCMD_Enable)) | newcmd;
1161 ms->Setup = true;
1162
1163 /* if we've dropped DTR, bitbucket any pending output */
1164 if ( (OldFlags & TIOCM_DTR) && ((bits & TIOCM_DTR) == 0))
1165 ms->OutFlush = true;
1166 }
1167
1168 bits = msc->flags;
1169
1170 (void) splx(s);
1171
1172 return(bits);
1173 }
1174
1175
1176 struct tty *
msctty(dev_t dev)1177 msctty(dev_t dev)
1178 {
1179 return(msc_tty[MSCTTY(dev)]);
1180 }
1181
1182
1183 /*
1184 * Load JM's freely redistributable A2232 6502c code. Let turbo detector
1185 * run for a while too.
1186 */
1187
1188 int
mscinitcard(struct zbus_args * zap)1189 mscinitcard(struct zbus_args *zap)
1190 {
1191 int bcount;
1192 short start;
1193 u_char *from;
1194 volatile u_char *to;
1195 volatile struct mscmemory *mlm;
1196
1197 mlm = (volatile struct mscmemory *)zap->va;
1198 (void)mlm->Enable6502Reset;
1199
1200 /* copy the code across to the board */
1201 to = (volatile u_char *)mlm;
1202 from = msc6502code; bcount = sizeof(msc6502code) - 2;
1203 start = *(short *)from; from += sizeof(start);
1204 to += start;
1205
1206 while(bcount--) *to++ = *from++;
1207
1208 mlm->Common.Crystal = MSC_UNKNOWN; /* use automatic speed check */
1209
1210 /* start 6502 running */
1211 (void)mlm->ResetBoard;
1212
1213 /* wait until speed detector has finished */
1214 for (bcount = 0; bcount < 2000; bcount++) {
1215 delay(10000);
1216 if (mlm->Common.Crystal)
1217 break;
1218 }
1219
1220 return(0);
1221 }
1222
1223 #endif /* NMSC > 0 */
1224