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