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