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