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