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