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