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