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