mfc.c revision 1.57.18.1 1 /* $NetBSD: mfc.c,v 1.57.18.1 2017/04/27 05:36:31 pgoyette Exp $ */
2
3 /*
4 * Copyright (c) 1982, 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 /*
32 * Copyright (c) 1994 Michael L. Hitch
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
44 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
45 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
46 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
47 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
52 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 */
54
55 #include "opt_kgdb.h"
56
57 #include <sys/cdefs.h>
58 __KERNEL_RCSID(0, "$NetBSD: mfc.c,v 1.57.18.1 2017/04/27 05:36:31 pgoyette Exp $");
59
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
63 #include <sys/device.h>
64 #include <sys/tty.h>
65 #include <sys/proc.h>
66 #include <sys/file.h>
67 #include <sys/uio.h>
68 #include <sys/syslog.h>
69 #include <sys/queue.h>
70 #include <sys/conf.h>
71 #include <sys/kauth.h>
72 #include <machine/cpu.h>
73 #include <amiga/amiga/device.h>
74 #include <amiga/amiga/isr.h>
75 #include <amiga/amiga/custom.h>
76 #include <amiga/amiga/cia.h>
77 #include <amiga/amiga/cc.h>
78 #include <amiga/dev/zbusvar.h>
79
80 #include <dev/cons.h>
81
82 #include "mfcs.h"
83
84 #ifndef SEROBUF_SIZE
85 #define SEROBUF_SIZE 128
86 #endif
87 #ifndef SERIBUF_SIZE
88 #define SERIBUF_SIZE 1024
89 #endif
90
91 #define splser() spl6()
92
93 /*
94 * 68581 DUART registers
95 */
96 struct mfc_regs {
97 volatile u_char du_mr1a;
98 #define du_mr2a du_mr1a
99 u_char pad0;
100 volatile u_char du_csra;
101 #define du_sra du_csra
102 u_char pad2;
103 volatile u_char du_cra;
104 u_char pad4;
105 volatile u_char du_tba;
106 #define du_rba du_tba
107 u_char pad6;
108 volatile u_char du_acr;
109 #define du_ipcr du_acr
110 u_char pad8;
111 volatile u_char du_imr;
112 #define du_isr du_imr
113 u_char pad10;
114 volatile u_char du_ctur;
115 #define du_cmsb du_ctur
116 u_char pad12;
117 volatile u_char du_ctlr;
118 #define du_clsb du_ctlr
119 u_char pad14;
120 volatile u_char du_mr1b;
121 #define du_mr2b du_mr1b
122 u_char pad16;
123 volatile u_char du_csrb;
124 #define du_srb du_csrb
125 u_char pad18;
126 volatile u_char du_crb;
127 u_char pad20;
128 volatile u_char du_tbb;
129 #define du_rbb du_tbb
130 u_char pad22;
131 volatile u_char du_ivr;
132 u_char pad24;
133 volatile u_char du_opcr;
134 #define du_ip du_opcr
135 u_char pad26;
136 volatile u_char du_btst;
137 #define du_strc du_btst
138 u_char pad28;
139 volatile u_char du_btrst;
140 #define du_stpc du_btrst
141 u_char pad30;
142 };
143
144 /*
145 * 68681 DUART serial port registers
146 */
147 struct duart_regs {
148 volatile u_char ch_mr1;
149 #define ch_mr2 ch_mr1
150 u_char pad0;
151 volatile u_char ch_csr;
152 #define ch_sr ch_csr
153 u_char pad1;
154 volatile u_char ch_cr;
155 u_char pad2;
156 volatile u_char ch_tb;
157 #define ch_rb ch_tb
158 u_char pad3;
159 };
160
161 struct mfc_softc {
162 device_t sc_dev;
163 struct isr sc_isr;
164 struct mfc_regs *sc_regs;
165 u_long clk_frq;
166 u_short ct_val;
167 u_char ct_usecnt;
168 u_char imask;
169 u_char mfc_iii;
170 u_char last_ip;
171 };
172
173 #if NMFCS > 0
174 struct mfcs_softc {
175 device_t sc_dev;
176 struct tty *sc_tty;
177 struct duart_regs *sc_duart;
178 struct mfc_regs *sc_regs;
179 struct mfc_softc *sc_mfc;
180 int swflags;
181 long flags; /* XXX */
182 #define CT_USED 1 /* CT in use */
183 u_short *rptr, *wptr, incnt, ovfl;
184 u_short inbuf[SERIBUF_SIZE];
185 char *ptr, *end;
186 char outbuf[SEROBUF_SIZE];
187 struct vbl_node vbl_node;
188 void *mfcs_si;
189 };
190 #endif
191
192 #if NMFCP > 0
193 struct mfcp_softc {
194 };
195 #endif
196
197 struct mfc_args {
198 struct zbus_args zargs;
199 const char *subdev;
200 char unit;
201 };
202
203 int mfcprint(void *, const char *);
204 void mfcattach(device_t, device_t, void *);
205 int mfcmatch(device_t, cfdata_t, void *);
206
207 #if NMFCS > 0
208 int mfcsmatch(device_t, cfdata_t, void *);
209 void mfcsattach(device_t, device_t, void *);
210 int mfcsparam( struct tty *, struct termios *);
211 int mfcshwiflow(struct tty *, int);
212 void mfcsstart(struct tty *);
213 int mfcsmctl(dev_t, int, int);
214 void mfcsxintr(int);
215 void mfcseint(int, int);
216 void mfcsmint(register int);
217 void mfcs_intr_soft(void *);
218 #endif
219
220 #if NMFCP > 0
221 void mfcpattach(device_t, device_t, void *);
222 int mfcpmatch(device_t, cfdata_t, void *);
223 #endif
224 int mfcintr(void *);
225
226 CFATTACH_DECL_NEW(mfc, sizeof(struct mfc_softc),
227 mfcmatch, mfcattach, NULL, NULL);
228
229 #if NMFCS > 0
230 CFATTACH_DECL_NEW(mfcs, sizeof(struct mfcs_softc),
231 mfcsmatch, mfcsattach, NULL, NULL);
232
233 extern struct cfdriver mfcs_cd;
234 #endif
235
236 #if NMFCP > 0
237 CFATTACH_DECL_NEW(mfcp, sizeof(struct mfcp_softc),
238 mfcpmatch, mfcpattach, NULL, NULL);
239 #endif
240
241 dev_type_open(mfcsopen);
242 dev_type_close(mfcsclose);
243 dev_type_read(mfcsread);
244 dev_type_write(mfcswrite);
245 dev_type_ioctl(mfcsioctl);
246 dev_type_stop(mfcsstop);
247 dev_type_tty(mfcstty);
248 dev_type_poll(mfcspoll);
249
250 const struct cdevsw mfcs_cdevsw = {
251 .d_open = mfcsopen,
252 .d_close = mfcsclose,
253 .d_read = mfcsread,
254 .d_write = mfcswrite,
255 .d_ioctl = mfcsioctl,
256 .d_stop = mfcsstop,
257 .d_tty = mfcstty,
258 .d_poll = mfcspoll,
259 .d_mmap = nommap,
260 .d_kqfilter = ttykqfilter,
261 .d_discard = nodiscard,
262 .d_flag = D_TTY
263 };
264
265 int mfcs_active;
266 int mfcsdefaultrate = 38400 /*TTYDEF_SPEED*/;
267 #define SWFLAGS(dev) (sc->swflags | (((dev) & 0x80) == 0 ? TIOCFLAG_SOFTCAR : 0))
268
269 #ifdef notyet
270 /*
271 * MultiFaceCard III, II+ (not supported yet), and
272 * SerialMaster 500+ (not supported yet)
273 * baud rate tables for BRG set 1 [not used yet]
274 */
275
276 const struct speedtab mfcs3speedtab1[] = {
277 { 0, 0 },
278 { 100, 0x00 },
279 { 220, 0x11 },
280 { 600, 0x44 },
281 { 1200, 0x55 },
282 { 2400, 0x66 },
283 { 4800, 0x88 },
284 { 9600, 0x99 },
285 { 19200, 0xbb },
286 { 115200, 0xcc },
287 { -1, -1 }
288 };
289
290 /*
291 * MultiFaceCard II, I, and SerialMaster 500
292 * baud rate tables for BRG set 1 [not used yet]
293 */
294
295 const struct speedtab mfcs2speedtab1[] = {
296 { 0, 0 },
297 { 50, 0x00 },
298 { 110, 0x11 },
299 { 300, 0x44 },
300 { 600, 0x55 },
301 { 1200, 0x66 },
302 { 2400, 0x88 },
303 { 4800, 0x99 },
304 { 9600, 0xbb },
305 { 38400, 0xcc },
306 { -1, -1 }
307 };
308 #endif
309
310 /*
311 * MultiFaceCard III, II+ (not supported yet), and
312 * SerialMaster 500+ (not supported yet)
313 * baud rate tables for BRG set 2
314 */
315
316 const struct speedtab mfcs3speedtab2[] = {
317 { 0, 0 },
318 { 150, 0x00 },
319 { 200, 0x11 },
320 { 300, 0x33 },
321 { 600, 0x44 },
322 { 1200, 0x55 },
323 { 2400, 0x66 },
324 { 4800, 0x88 },
325 { 9600, 0x99 },
326 { 19200, 0xbb },
327 { 38400, 0xcc },
328 { -1, -1 }
329 };
330
331 /*
332 * MultiFaceCard II, I, and SerialMaster 500
333 * baud rate tables for BRG set 2
334 */
335
336 const struct speedtab mfcs2speedtab2[] = {
337 { 0, 0 },
338 { 75, 0x00 },
339 { 100, 0x11 },
340 { 150, 0x33 },
341 { 300, 0x44 },
342 { 600, 0x55 },
343 { 1200, 0x66 },
344 { 2400, 0x88 },
345 { 4800, 0x99 },
346 { 9600, 0xbb },
347 { 19200, 0xcc },
348 { -1, -1 }
349 };
350
351 /*
352 * if we are an bsc/Alf Data MultFaceCard (I, II, and III)
353 */
354 int
355 mfcmatch(device_t parent, cfdata_t cf, void *aux)
356 {
357 struct zbus_args *zap;
358
359 zap = aux;
360 if (zap->manid == 2092 &&
361 (zap->prodid == 16 || zap->prodid == 17 || zap->prodid == 18))
362
363 return(1);
364 return(0);
365 }
366
367 void
368 mfcattach(device_t parent, device_t self, void *aux)
369 {
370 struct mfc_softc *scc;
371 struct zbus_args *zap;
372 struct mfc_args ma;
373 int unit;
374 struct mfc_regs *rp;
375
376 zap = aux;
377
378 printf ("\n");
379
380 scc = device_private(self);
381 scc->sc_dev = self;
382 unit = device_unit(self);
383 scc->sc_regs = rp = zap->va;
384 if (zap->prodid == 18)
385 scc->mfc_iii = 3;
386 scc->clk_frq = scc->mfc_iii ? 230400 : 115200;
387
388 rp->du_opcr = 0x00; /* configure output port? */
389 rp->du_btrst = 0x0f; /* clear modem lines */
390 rp->du_ivr = 0; /* IVR */
391 rp->du_imr = 0; /* IMR */
392 rp->du_acr = 0xe0; /* baud rate generate set 2 */
393 rp->du_ctur = 0;
394 rp->du_ctlr = 4;
395 rp->du_csra = 0xcc; /* clock select = 38400 */
396 rp->du_cra = 0x10; /* reset mode register ptr */
397 rp->du_cra = 0x20;
398 rp->du_cra = 0x30;
399 rp->du_cra = 0x40;
400 rp->du_mr1a = 0x93; /* MRA1 */
401 rp->du_mr2a = 0x17; /* MRA2 */
402 rp->du_csrb = 0xcc; /* clock select = 38400 */
403 rp->du_crb = 0x10; /* reset mode register ptr */
404 rp->du_crb = 0x20;
405 rp->du_crb = 0x30;
406 rp->du_crb = 0x40;
407 rp->du_mr1b = 0x93; /* MRB1 */
408 rp->du_mr2b = 0x17; /* MRB2 */
409 rp->du_cra = 0x05; /* enable A Rx & Tx */
410 rp->du_crb = 0x05; /* enable B Rx & Tx */
411
412 scc->sc_isr.isr_intr = mfcintr;
413 scc->sc_isr.isr_arg = scc;
414 scc->sc_isr.isr_ipl = 6;
415 add_isr(&scc->sc_isr);
416
417 /* configure ports */
418 memcpy(&ma.zargs, zap, sizeof(struct zbus_args));
419 ma.subdev = "mfcs";
420 ma.unit = unit * 2;
421 config_found(self, &ma, mfcprint);
422 ma.unit = unit * 2 + 1;
423 config_found(self, &ma, mfcprint);
424 ma.subdev = "mfcp";
425 ma.unit = unit;
426 config_found(self, &ma, mfcprint);
427 }
428
429 /*
430 *
431 */
432 int
433 mfcsmatch(device_t parent, cfdata_t cf, void *aux)
434 {
435 struct mfc_args *ma;
436
437 ma = aux;
438 if (strcmp(ma->subdev, "mfcs") == 0)
439 return (1);
440 return (0);
441 }
442
443 void
444 mfcsattach(device_t parent, device_t self, void *aux)
445 {
446 int unit;
447 struct mfcs_softc *sc;
448 struct mfc_softc *scc;
449 struct mfc_args *ma;
450 struct mfc_regs *rp;
451
452 sc = device_private(self);
453 sc->sc_dev = self;
454 scc = device_private(parent);
455 ma = aux;
456
457 printf (": input fifo %d output fifo %d\n", SERIBUF_SIZE,
458 SEROBUF_SIZE);
459
460 unit = ma->unit;
461 mfcs_active |= 1 << unit;
462 sc->rptr = sc->wptr = sc->inbuf;
463 sc->sc_mfc = scc;
464 sc->sc_regs = rp = scc->sc_regs;
465 sc->sc_duart = (struct duart_regs *) ((unit & 1) ?
466 __UNVOLATILE(&rp->du_mr1b) : __UNVOLATILE(&rp->du_mr1a));
467 sc->mfcs_si = softint_establish(SOFTINT_SERIAL, mfcs_intr_soft, sc);
468 /*
469 * should have only one vbl routine to handle all ports?
470 */
471 sc->vbl_node.function = (void (*) (void *)) mfcsmint;
472 sc->vbl_node.data = (void *) unit;
473 add_vbl_function(&sc->vbl_node, 1, (void *) unit);
474 }
475
476 /*
477 * print diag if pnp is NULL else just extra
478 */
479 int
480 mfcprint(void *aux, const char *pnp)
481 {
482 if (pnp == NULL)
483 return(UNCONF);
484 return(QUIET);
485 }
486
487 int
488 mfcsopen(dev_t dev, int flag, int mode, struct lwp *l)
489 {
490 struct tty *tp;
491 struct mfcs_softc *sc;
492 int unit, error;
493
494 error = 0;
495 unit = dev & 0x1f;
496
497 sc = device_lookup_private_acquire(&mfcs_cd, unit);
498 if ((mfcs_active & (1 << unit)) == 0) {
499 device_release(sc->sc_dev);
500 return (ENXIO);
501 }
502 if (sc->sc_tty)
503 tp = sc->sc_tty;
504 else {
505 tp = sc->sc_tty = tty_alloc();
506 tty_attach(tp);
507 }
508
509 tp->t_oproc = (void (*) (struct tty *)) mfcsstart;
510 tp->t_param = mfcsparam;
511 tp->t_dev = dev;
512 tp->t_hwiflow = mfcshwiflow;
513
514 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) {
515 device_release(sc->sc_dev);
516 return (EBUSY);
517 }
518 mutex_spin_enter(&tty_lock);
519 if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
520 ttychars(tp);
521 if (tp->t_ispeed == 0) {
522 /*
523 * only when cleared do we reset to defaults.
524 */
525 tp->t_iflag = TTYDEF_IFLAG;
526 tp->t_oflag = TTYDEF_OFLAG;
527 tp->t_cflag = TTYDEF_CFLAG;
528 tp->t_lflag = TTYDEF_LFLAG;
529 tp->t_ispeed = tp->t_ospeed = mfcsdefaultrate;
530 }
531 /*
532 * do these all the time
533 */
534 if (sc->swflags & TIOCFLAG_CLOCAL)
535 tp->t_cflag |= CLOCAL;
536 if (sc->swflags & TIOCFLAG_CRTSCTS)
537 tp->t_cflag |= CRTSCTS;
538 if (sc->swflags & TIOCFLAG_MDMBUF)
539 tp->t_cflag |= MDMBUF;
540 mfcsparam(tp, &tp->t_termios);
541 ttsetwater(tp);
542
543 (void)mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMSET);
544 if ((SWFLAGS(dev) & TIOCFLAG_SOFTCAR) ||
545 (mfcsmctl(dev, 0, DMGET) & TIOCM_CD))
546 tp->t_state |= TS_CARR_ON;
547 else
548 tp->t_state &= ~TS_CARR_ON;
549 }
550
551 /*
552 * if NONBLOCK requested, ignore carrier
553 */
554 if (flag & O_NONBLOCK)
555 goto done;
556
557 /*
558 * block waiting for carrier
559 */
560 while ((tp->t_state & TS_CARR_ON) == 0 && (tp->t_cflag & CLOCAL) == 0) {
561 tp->t_wopen++;
562 error = ttysleep(tp, &tp->t_rawcv, true, 0);
563 tp->t_wopen--;
564 if (error) {
565 mutex_spin_exit(&tty_lock);
566 device_release(sc->sc_dev);
567 return(error);
568 }
569 }
570 done:
571 /* This is a way to handle lost XON characters */
572 if ((flag & O_TRUNC) && (tp->t_state & TS_TTSTOP)) {
573 tp->t_state &= ~TS_TTSTOP;
574 ttstart (tp);
575 }
576 /*
577 * Reset the tty pointer, as there could have been a dialout
578 * use of the tty with a dialin open waiting.
579 */
580 tp->t_dev = dev;
581 mutex_spin_exit(&tty_lock);
582 device_release(sc->sc_dev);
583 return tp->t_linesw->l_open(dev, tp);
584 }
585
586 /*ARGSUSED*/
587 int
588 mfcsclose(dev_t dev, int flag, int mode, struct lwp *l)
589 {
590 struct tty *tp;
591 int unit;
592 struct mfcs_softc *sc =
593 device_lookup_private_acquire(&mfcs_cd, dev & 31);
594 struct mfc_softc *scc= sc->sc_mfc;
595
596 unit = dev & 31;
597
598 tp = sc->sc_tty;
599 tp->t_linesw->l_close(tp, flag);
600 sc->sc_duart->ch_cr = 0x70; /* stop break */
601
602 scc->imask &= ~(0x7 << ((unit & 1) * 4));
603 scc->sc_regs->du_imr = scc->imask;
604 if (sc->flags & CT_USED) {
605 --scc->ct_usecnt;
606 sc->flags &= ~CT_USED;
607 }
608
609 /*
610 * If the device is closed, it's close, no matter whether we deal with
611 * modem control signals nor not.
612 */
613 #if 0
614 if (tp->t_cflag & HUPCL || tp->t_wopen != 0 ||
615 (tp->t_state & TS_ISOPEN) == 0)
616 #endif
617 (void) mfcsmctl(dev, 0, DMSET);
618 ttyclose(tp);
619 #if not_yet
620 if (tp != &mfcs_cons) {
621 remove_vbl_function(&sc->vbl_node);
622 tty_free(tp);
623 sc->sc_tty = (struct tty *) NULL;
624 }
625 #endif
626 device_release(sc->sc_dev);
627 return (0);
628 }
629
630 int
631 mfcsread(dev_t dev, struct uio *uio, int flag)
632 {
633 int error;
634 struct mfcs_softc *sc =
635 device_lookup_private_acquire(&mfcs_cd, dev & 31);
636 struct tty *tp = sc->sc_tty;
637
638 if (tp == NULL) {
639 device_release(sc->sc_dev);
640 return(ENXIO);
641 }
642 error = tp->t_linesw->l_read(tp, uio, flag);
643
644 device_release(sc->sc_dev);
645 return error;
646 }
647
648 int
649 mfcswrite(dev_t dev, struct uio *uio, int flag)
650 {
651 int error;
652 struct mfcs_softc *sc =
653 device_lookup_private_acquire(&mfcs_cd, dev & 31);
654 struct tty *tp = sc->sc_tty;
655
656 if (tp == NULL) {
657 device_release(sc->sc_dev);
658 return(ENXIO);
659 }
660 error = tp->t_linesw->l_write(tp, uio, flag);
661
662 device_release(sc->sc_dev);
663 return error;
664 }
665
666 int
667 mfcspoll(dev_t dev, int events, struct lwp *l)
668 {
669 int error;
670 struct mfcs_softc *sc =
671 device_lookup_private_acquire(&mfcs_cd, dev & 31);
672 struct tty *tp = sc->sc_tty;
673
674 if (tp == NULL) {
675 device_release(sc->sc_dev);
676 return(ENXIO);
677 }
678 error = ((*tp->t_linesw->l_poll)(tp, events, l));
679
680 device_release(sc->sc_dev);
681 return error;
682 }
683
684 struct tty *
685 mfcstty(dev_t dev)
686 {
687 struct mfcs_softc *sc =
688 device_lookup_private_acquire(&mfcs_cd, dev & 31);
689 struct tty *tty;
690
691 tty = sc->sc_tty;
692 device_release(sc->sc_dev);
693 return tty;
694 }
695
696 int
697 mfcsioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
698 {
699 register struct tty *tp;
700 register int error;
701 struct mfcs_softc *sc =
702 device_lookup_private_acquire(&mfcs_cd, dev & 31);
703
704 tp = sc->sc_tty;
705 if (!tp) {
706 device_release(sc->sc_dev);
707 return ENXIO;
708 }
709
710 error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l);
711 if (error != EPASSTHROUGH) {
712 device_release(sc->sc_dev);
713 return(error);
714 }
715 error = ttioctl(tp, cmd, data, flag, l);
716 if (error != EPASSTHROUGH) {
717 device_release(sc->sc_dev);
718 return(error);
719 }
720 error = 0;
721 switch (cmd) {
722 case TIOCSBRK:
723 sc->sc_duart->ch_cr = 0x60; /* start break */
724 break;
725
726 case TIOCCBRK:
727 sc->sc_duart->ch_cr = 0x70; /* stop break */
728 break;
729
730 case TIOCSDTR:
731 (void) mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIS);
732 break;
733
734 case TIOCCDTR:
735 (void) mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIC);
736 break;
737
738 case TIOCMSET:
739 (void) mfcsmctl(dev, *(int *) data, DMSET);
740 break;
741
742 case TIOCMBIS:
743 (void) mfcsmctl(dev, *(int *) data, DMBIS);
744 break;
745
746 case TIOCMBIC:
747 (void) mfcsmctl(dev, *(int *) data, DMBIC);
748 break;
749
750 case TIOCMGET:
751 *(int *)data = mfcsmctl(dev, 0, DMGET);
752 break;
753 case TIOCGFLAGS:
754 *(int *)data = SWFLAGS(dev);
755 break;
756 case TIOCSFLAGS:
757 error = kauth_authorize_device_tty(l->l_cred,
758 KAUTH_DEVICE_TTY_PRIVSET, tp);
759 if (error != 0)
760 error = EPERM;
761 else {
762 sc->swflags = *(int *)data;
763 sc->swflags &= /* only allow valid flags */
764 (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | TIOCFLAG_CRTSCTS);
765 /* XXXX need to change duart parameters? */
766 }
767 break;
768 default:
769 error = EPASSTHROUGH;
770 }
771
772 device_release(sc->sc_dev);
773 return error;
774 }
775
776 int
777 mfcsparam(struct tty *tp, struct termios *t)
778 {
779 int cflag, unit, ospeed;
780 struct mfcs_softc *sc =
781 device_lookup_private_acquire(&mfcs_cd, tp->t_dev & 31);
782 struct mfc_softc *scc= sc->sc_mfc;
783
784 cflag = t->c_cflag;
785 unit = tp->t_dev & 31;
786 if (sc->flags & CT_USED) {
787 --scc->ct_usecnt;
788 sc->flags &= ~CT_USED;
789 }
790 ospeed = ttspeedtab(t->c_ospeed, scc->mfc_iii ? mfcs3speedtab2 :
791 mfcs2speedtab2);
792
793 /*
794 * If Baud Rate Generator can't generate requested speed,
795 * try to use the counter/timer.
796 */
797 if (ospeed < 0 && (scc->clk_frq % t->c_ospeed) == 0) {
798 ospeed = scc->clk_frq / t->c_ospeed; /* divisor */
799 if (scc->ct_usecnt > 0 && scc->ct_val != ospeed)
800 ospeed = -1;
801 else {
802 scc->sc_regs->du_ctur = ospeed >> 8;
803 scc->sc_regs->du_ctlr = ospeed;
804 scc->ct_val = ospeed;
805 ++scc->ct_usecnt;
806 sc->flags |= CT_USED;
807 ospeed = 0xdd;
808 }
809 }
810 /* XXXX 68681 duart could handle split speeds */
811 if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed)) {
812 device_release(sc->sc_dev);
813 return(EINVAL);
814 }
815
816 /* XXXX handle parity, character size, stop bits, flow control */
817
818 /*
819 * copy to tty
820 */
821 tp->t_ispeed = t->c_ispeed;
822 tp->t_ospeed = t->c_ospeed;
823 tp->t_cflag = cflag;
824
825 /*
826 * enable interrupts
827 */
828 scc->imask |= (0x2 << ((unit & 1) * 4)) | 0x80;
829 scc->sc_regs->du_imr = scc->imask;
830 #if defined(DEBUG) && 0
831 printf("mfcsparam: speed %d => %x ct %d imask %x cflag %x\n",
832 t->c_ospeed, ospeed, scc->ct_val, scc->imask, cflag);
833 #endif
834 if (ospeed == 0)
835 (void)mfcsmctl(tp->t_dev, 0, DMSET); /* hang up line */
836 else {
837 /*
838 * (re)enable DTR
839 * and set baud rate. (8 bit mode)
840 */
841 (void)mfcsmctl(tp->t_dev, TIOCM_DTR | TIOCM_RTS, DMSET);
842 sc->sc_duart->ch_csr = ospeed;
843 }
844 device_release(sc->sc_dev);
845 return(0);
846 }
847
848 int
849 mfcshwiflow(struct tty *tp, int flag)
850 {
851 struct mfcs_softc *sc =
852 device_lookup_private_acquire(&mfcs_cd, tp->t_dev & 31);
853 int unit = tp->t_dev & 1;
854
855 if (flag)
856 sc->sc_regs->du_btrst = 1 << unit;
857 else
858 sc->sc_regs->du_btst = 1 << unit;
859 device_release(sc->sc_dev);
860 return 1;
861 }
862
863 void
864 mfcsstart(struct tty *tp)
865 {
866 int cc, s, unit;
867 struct mfcs_softc *sc =
868 device_lookup_private_acquire(&mfcs_cd, tp->t_dev & 31);
869 struct mfc_softc *scc= sc->sc_mfc;
870
871 if ((tp->t_state & TS_ISOPEN) == 0) {
872 device_release(sc->sc_dev);
873 return;
874 }
875
876 unit = tp->t_dev & 1;
877
878 s = splser();
879 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))
880 goto out;
881
882 cc = tp->t_outq.c_cc;
883 if (!ttypull(tp) || (tp->t_state & TS_BUSY))
884 goto out;
885
886 /*
887 * We only do bulk transfers if using CTSRTS flow control, not for
888 * (probably sloooow) ixon/ixoff devices.
889 */
890 if ((tp->t_cflag & CRTSCTS) == 0)
891 cc = 1;
892
893 /*
894 * Limit the amount of output we do in one burst
895 * to prevent hogging the CPU.
896 */
897 if (cc > SEROBUF_SIZE)
898 cc = SEROBUF_SIZE;
899 cc = q_to_b(&tp->t_outq, sc->outbuf, cc);
900 if (cc > 0) {
901 tp->t_state |= TS_BUSY;
902
903 sc->ptr = sc->outbuf;
904 sc->end = sc->outbuf + cc;
905
906 /*
907 * Get first character out, then have TBE-interrupts blow out
908 * further characters, until buffer is empty, and TS_BUSY gets
909 * cleared.
910 */
911 sc->sc_duart->ch_tb = *sc->ptr++;
912 scc->imask |= 1 << (unit * 4);
913 sc->sc_regs->du_imr = scc->imask;
914 }
915 out:
916 splx(s);
917 device_release(sc->sc_dev);
918 }
919
920 /*
921 * Stop output on a line.
922 */
923 /*ARGSUSED*/
924 void
925 mfcsstop(struct tty *tp, int flag)
926 {
927 int s;
928
929 s = splser();
930 if (tp->t_state & TS_BUSY) {
931 if ((tp->t_state & TS_TTSTOP) == 0)
932 tp->t_state |= TS_FLUSH;
933 }
934 splx(s);
935 }
936
937 int
938 mfcsmctl(dev_t dev, int bits, int how)
939 {
940 int unit, s;
941 u_char ub = 0;
942 struct mfcs_softc *sc =
943 device_lookup_private_acquire(&mfcs_cd, dev & 31);
944
945 unit = dev & 1;
946
947 /*
948 * convert TIOCM* mask into CIA mask
949 * which is active low
950 */
951 if (how != DMGET) {
952 /*
953 * need to save current state of DTR & RTS ?
954 */
955 if (bits & TIOCM_DTR)
956 ub |= 0x04 << unit;
957 if (bits & TIOCM_RTS)
958 ub |= 0x01 << unit;
959 }
960 s = splser();
961 switch (how) {
962 case DMSET:
963 sc->sc_regs->du_btst = ub;
964 sc->sc_regs->du_btrst = ub ^ (0x05 << unit);
965 break;
966
967 case DMBIC:
968 sc->sc_regs->du_btrst = ub;
969 ub = ~sc->sc_regs->du_ip;
970 break;
971
972 case DMBIS:
973 sc->sc_regs->du_btst = ub;
974 ub = ~sc->sc_regs->du_ip;
975 break;
976
977 case DMGET:
978 ub = ~sc->sc_regs->du_ip;
979 break;
980 }
981 (void)splx(s);
982
983 /* XXXX should keep DTR & RTS states in softc? */
984 bits = TIOCM_DTR | TIOCM_RTS;
985 if (ub & (1 << unit))
986 bits |= TIOCM_CTS;
987 if (ub & (4 << unit))
988 bits |= TIOCM_DSR;
989 if (ub & (0x10 << unit))
990 bits |= TIOCM_CD;
991 /* XXXX RI is not supported on all boards */
992 if (sc->sc_regs->pad26 & (1 << unit))
993 bits |= TIOCM_RI;
994
995 device_release(sc->sc_dev);
996 return(bits);
997 }
998
999 /*
1000 * Level 6 interrupt processing for the MultiFaceCard 68681 DUART
1001 */
1002
1003 int
1004 mfcintr(void *arg)
1005 {
1006 struct mfc_softc *scc = arg;
1007 struct mfcs_softc *sc;
1008 struct mfc_regs *regs;
1009 struct tty *tp;
1010 int istat, unit;
1011 u_short c;
1012
1013 regs = scc->sc_regs;
1014 istat = regs->du_isr & scc->imask;
1015 if (istat == 0)
1016 return (0);
1017 unit = device_unit(scc->sc_dev) * 2;
1018 if (istat & 0x02) { /* channel A receive interrupt */
1019 sc = device_lookup_private_acquire(&mfcs_cd, unit);
1020 while (1) {
1021 c = regs->du_sra << 8;
1022 if ((c & 0x0100) == 0)
1023 break;
1024 c |= regs->du_rba;
1025 if (sc->incnt == SERIBUF_SIZE)
1026 ++sc->ovfl;
1027 else {
1028 *sc->wptr++ = c;
1029 if (sc->wptr == sc->inbuf + SERIBUF_SIZE)
1030 sc->wptr = sc->inbuf;
1031 ++sc->incnt;
1032 if (sc->incnt > SERIBUF_SIZE - 16)
1033 regs->du_btrst = 1;
1034 }
1035 if (c & 0x1000)
1036 regs->du_cra = 0x40;
1037 }
1038 device_release(sc->sc_dev);
1039 }
1040 if (istat & 0x20) { /* channel B receive interrupt */
1041 sc = device_lookup_private_acquire(&mfcs_cd, unit + 1);
1042 while (1) {
1043 c = regs->du_srb << 8;
1044 if ((c & 0x0100) == 0)
1045 break;
1046 c |= regs->du_rbb;
1047 if (sc->incnt == SERIBUF_SIZE)
1048 ++sc->ovfl;
1049 else {
1050 *sc->wptr++ = c;
1051 if (sc->wptr == sc->inbuf + SERIBUF_SIZE)
1052 sc->wptr = sc->inbuf;
1053 ++sc->incnt;
1054 if (sc->incnt > SERIBUF_SIZE - 16)
1055 regs->du_btrst = 2;
1056 }
1057 if (c & 0x1000)
1058 regs->du_crb = 0x40;
1059 }
1060 device_release(sc->sc_dev);
1061 }
1062 if (istat & 0x01) { /* channel A transmit interrupt */
1063 sc = device_lookup_private_acquire(&mfcs_cd, unit);
1064 tp = sc->sc_tty;
1065 if (sc->ptr == sc->end) {
1066 tp->t_state &= ~(TS_BUSY | TS_FLUSH);
1067 scc->imask &= ~0x01;
1068 regs->du_imr = scc->imask;
1069 softint_schedule(sc->mfcs_si);
1070 }
1071 else
1072 regs->du_tba = *sc->ptr++;
1073 device_release(sc->sc_dev);
1074 }
1075 if (istat & 0x10) { /* channel B transmit interrupt */
1076 sc = device_lookup_private_acquire(&mfcs_cd, unit + 1);
1077 tp = sc->sc_tty;
1078 if (sc->ptr == sc->end) {
1079 tp->t_state &= ~(TS_BUSY | TS_FLUSH);
1080 scc->imask &= ~0x10;
1081 regs->du_imr = scc->imask;
1082 softint_schedule(sc->mfcs_si);
1083 }
1084 else
1085 regs->du_tbb = *sc->ptr++;
1086 device_release(sc->sc_dev);
1087 }
1088 if (istat & 0x80) { /* input port change interrupt */
1089 c = regs->du_ipcr;
1090 printf ("%s: ipcr %02x", device_xname(scc->sc_dev), c);
1091 }
1092 return(1);
1093 }
1094
1095 void
1096 mfcsxintr(int unit)
1097 {
1098 int s1, s2, ovfl;
1099 struct mfcs_softc *sc =
1100 device_lookup_private_acquire(&mfcs_cd, unit);
1101 struct tty *tp = sc->sc_tty;
1102
1103 /*
1104 * Make sure we're not interrupted by another
1105 * vbl, but allow level6 ints
1106 */
1107 s1 = spltty();
1108
1109 /*
1110 * pass along any acumulated information
1111 * while input is not blocked
1112 */
1113 while (sc->incnt && (tp->t_state & TS_TBLOCK) == 0) {
1114 /*
1115 * no collision with ser_fastint()
1116 */
1117 mfcseint(unit, *sc->rptr++);
1118
1119 ovfl = 0;
1120 /* lock against mfcs_fastint() */
1121 s2 = splser();
1122 --sc->incnt;
1123 if (sc->rptr == sc->inbuf + SERIBUF_SIZE)
1124 sc->rptr = sc->inbuf;
1125 if (sc->ovfl != 0) {
1126 ovfl = sc->ovfl;
1127 sc->ovfl = 0;
1128 }
1129 splx(s2);
1130 if (ovfl != 0)
1131 log(LOG_WARNING, "%s: %d buffer overflow!\n",
1132 device_xname(sc->sc_dev), ovfl);
1133 }
1134 if (sc->incnt == 0 && (tp->t_state & TS_TBLOCK) == 0) {
1135 sc->sc_regs->du_btst = 1 << unit; /* XXXX */
1136 }
1137 splx(s1);
1138 device_release(sc->sc_dev);
1139 }
1140
1141 void
1142 mfcseint(int unit, int stat)
1143 {
1144 struct mfcs_softc *sc = device_lookup_private_acquire(&mfcs_cd, unit);
1145 struct tty *tp;
1146 u_char ch;
1147 int c;
1148
1149 tp = sc->sc_tty;
1150 ch = stat & 0xff;
1151 c = ch;
1152
1153 if ((tp->t_state & TS_ISOPEN) == 0) {
1154 #ifdef KGDB
1155 extern const struct cdevsw ser_cdevsw;
1156 int maj;
1157
1158 /* we don't care about parity errors */
1159 maj = cdevsw_lookup_major(&ser_cdevsw);
1160 if (kgdb_dev == makedev(maj, unit) && c == FRAME_END)
1161 kgdb_connect(0); /* trap into kgdb */
1162 #endif
1163 device_release(sc->sc_dev);
1164 return;
1165 }
1166
1167 /*
1168 * Check for break and (if enabled) parity error.
1169 */
1170 if (stat & 0xc000)
1171 c |= TTY_FE;
1172 else if (stat & 0x2000)
1173 c |= TTY_PE;
1174
1175 if (stat & 0x1000)
1176 log(LOG_WARNING, "%s: fifo overflow\n",
1177 device_xname(device_lookup_private(&mfcs_cd, unit)));
1178
1179 tp->t_linesw->l_rint(c, tp);
1180 device_release(sc->sc_dev);
1181 }
1182
1183 /*
1184 * This interrupt is periodically invoked in the vertical blank
1185 * interrupt. It's used to keep track of the modem control lines
1186 * and (new with the fast_int code) to move accumulated data
1187 * up into the tty layer.
1188 */
1189 void
1190 mfcsmint(int unit)
1191 {
1192 struct tty *tp;
1193 struct mfcs_softc *sc = device_lookup_private_acquire(&mfcs_cd, unit);
1194 u_char stat, last, istat;
1195
1196 tp = sc->sc_tty;
1197 if (!tp) {
1198 device_release(sc->sc-dev);
1199 return;
1200 }
1201 if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
1202 sc->rptr = sc->wptr = sc->inbuf;
1203 sc->incnt = 0;
1204 device_release(sc->sc_dev);
1205 return;
1206 }
1207 /*
1208 * empty buffer
1209 */
1210 mfcsxintr(unit);
1211
1212 stat = ~sc->sc_regs->du_ip;
1213 last = sc->sc_mfc->last_ip;
1214 sc->sc_mfc->last_ip = stat;
1215
1216 /*
1217 * check whether any interesting signal changed state
1218 */
1219 istat = stat ^ last;
1220
1221 if ((istat & (0x10 << (unit & 1))) && /* CD changed */
1222 (SWFLAGS(tp->t_dev) & TIOCFLAG_SOFTCAR) == 0) {
1223 if (stat & (0x10 << (unit & 1)))
1224 tp->t_linesw->l_modem(tp, 1);
1225 else if (tp->t_linesw->l_modem(tp, 0) == 0) {
1226 sc->sc_regs->du_btrst = 0x0a << (unit & 1);
1227 }
1228 }
1229 device_release(sc->sc_dev);
1230 }
1231
1232 void
1233 mfcs_intr_soft(void *arg)
1234 {
1235 struct mfcs_softc *sc = (struct mfcs_softc *)arg;
1236 struct tty *tp = sc->sc_tty;
1237
1238 if (tp->t_linesw)
1239 tp->t_linesw->l_start(tp);
1240 else
1241 mfcsstart(tp);
1242 }
1243