mfc.c revision 1.57.8.1 1 /* $NetBSD: mfc.c,v 1.57.8.1 2016/07/19 06:26:58 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.8.1 2016/07/19 06:26:58 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 device_t self;
491 struct tty *tp;
492 struct mfcs_softc *sc;
493 int unit, error;
494
495 error = 0;
496 unit = dev & 0x1f;
497
498 self = device_lookup_acquire(&mfcs_cd, unit);
499 if (self == NULL)
500 return ENXIO;
501 sc = device_private(self);
502 if (sc == NULL || (mfcs_active & (1 << unit)) == 0) {
503 device_release(self);
504 return (ENXIO);
505 }
506
507 if (sc->sc_tty)
508 tp = sc->sc_tty;
509 else {
510 tp = sc->sc_tty = tty_alloc();
511 tty_attach(tp);
512 }
513
514 tp->t_oproc = (void (*) (struct tty *)) mfcsstart;
515 tp->t_param = mfcsparam;
516 tp->t_dev = dev;
517 tp->t_hwiflow = mfcshwiflow;
518
519 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) {
520 device_release(self);
521 return (EBUSY);
522 }
523
524 mutex_spin_enter(&tty_lock);
525 if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
526 ttychars(tp);
527 if (tp->t_ispeed == 0) {
528 /*
529 * only when cleared do we reset to defaults.
530 */
531 tp->t_iflag = TTYDEF_IFLAG;
532 tp->t_oflag = TTYDEF_OFLAG;
533 tp->t_cflag = TTYDEF_CFLAG;
534 tp->t_lflag = TTYDEF_LFLAG;
535 tp->t_ispeed = tp->t_ospeed = mfcsdefaultrate;
536 }
537 /*
538 * do these all the time
539 */
540 if (sc->swflags & TIOCFLAG_CLOCAL)
541 tp->t_cflag |= CLOCAL;
542 if (sc->swflags & TIOCFLAG_CRTSCTS)
543 tp->t_cflag |= CRTSCTS;
544 if (sc->swflags & TIOCFLAG_MDMBUF)
545 tp->t_cflag |= MDMBUF;
546 mfcsparam(tp, &tp->t_termios);
547 ttsetwater(tp);
548
549 (void)mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMSET);
550 if ((SWFLAGS(dev) & TIOCFLAG_SOFTCAR) ||
551 (mfcsmctl(dev, 0, DMGET) & TIOCM_CD))
552 tp->t_state |= TS_CARR_ON;
553 else
554 tp->t_state &= ~TS_CARR_ON;
555 }
556
557 /*
558 * if NONBLOCK requested, ignore carrier
559 */
560 if (flag & O_NONBLOCK)
561 goto done;
562
563 /*
564 * block waiting for carrier
565 */
566 while ((tp->t_state & TS_CARR_ON) == 0 && (tp->t_cflag & CLOCAL) == 0) {
567 tp->t_wopen++;
568 error = ttysleep(tp, &tp->t_rawcv, true, 0);
569 tp->t_wopen--;
570 if (error) {
571 mutex_spin_exit(&tty_lock);
572 return(error);
573 }
574 }
575 done:
576 /* This is a way to handle lost XON characters */
577 if ((flag & O_TRUNC) && (tp->t_state & TS_TTSTOP)) {
578 tp->t_state &= ~TS_TTSTOP;
579 ttstart (tp);
580 }
581 /*
582 * Reset the tty pointer, as there could have been a dialout
583 * use of the tty with a dialin open waiting.
584 */
585 tp->t_dev = dev;
586 mutex_spin_exit(&tty_lock);
587
588 device_release(self);
589 return tp->t_linesw->l_open(dev, tp);
590 }
591
592 /*ARGSUSED*/
593 int
594 mfcsclose(dev_t dev, int flag, int mode, struct lwp *l)
595 {
596 device_t self;
597 struct tty *tp;
598 int unit;
599 struct mfcs_softc *sc;
600 struct mfc_softc *scc;
601
602 unit = dev & 31;
603
604 self = device_lookup_acquire(&mfcs_cd, unit);
605 if (self == NULL)
606 return ENXIO;
607 sc = device_private(self);
608 if (sc == NULL) {
609 device_release(self);
610 return ENXIO;
611 }
612 scc = sc->sc_mfc;
613
614 tp = sc->sc_tty;
615 tp->t_linesw->l_close(tp, flag);
616 sc->sc_duart->ch_cr = 0x70; /* stop break */
617
618 scc->imask &= ~(0x7 << ((unit & 1) * 4));
619 scc->sc_regs->du_imr = scc->imask;
620 if (sc->flags & CT_USED) {
621 --scc->ct_usecnt;
622 sc->flags &= ~CT_USED;
623 }
624
625 /*
626 * If the device is closed, it's close, no matter whether we deal with
627 * modem control signals nor not.
628 */
629 #if 0
630 if (tp->t_cflag & HUPCL || tp->t_wopen != 0 ||
631 (tp->t_state & TS_ISOPEN) == 0)
632 #endif
633 (void) mfcsmctl(dev, 0, DMSET);
634 ttyclose(tp);
635 #if not_yet
636 if (tp != &mfcs_cons) {
637 remove_vbl_function(&sc->vbl_node);
638 tty_free(tp);
639 sc->sc_tty = (struct tty *) NULL;
640 }
641 #endif
642 device_release(self);
643 return (0);
644 }
645
646 int
647 mfcsread(dev_t dev, struct uio *uio, int flag)
648 {
649 device_t self;
650 struct mfcs_softc *sc;
651 struct tty *tp;
652 int val;
653
654 self = device_lookup_acquire(&mfcs_cd, dev & 31);
655 if (self == NULL)
656 return ENXIO;
657 sc = device_private(self);
658 if (sc == NULL) {
659 device_release(self);
660 return ENXIO;
661 }
662 tp = sc->sc_tty;
663 if (tp == NULL) {
664 device_release(self);
665 return(ENXIO);
666 }
667 val = tp->t_linesw->l_read(tp, uio, flag);
668 device_release(self);
669 return val;
670 }
671
672 int
673 mfcswrite(dev_t dev, struct uio *uio, int flag)
674 {
675 struct mfcs_softc *sc;
676 struct tty *tp;= sc->sc_tty;
677
678 self = device_lookup_acquire(&mfcs_cd, dev & 31);
679 if (self == NULL)
680 return ENXIO;
681 sc = device_private(self);
682 if (sc == NULL) {
683 device_release(self);
684 return ENXIO;
685 }
686 tp = sc->sc_tty;
687 if (tp == NULL) {
688 device_release(self);
689 return(ENXIO);
690 }
691 val = tp->t_linesw->l_write(tp, uio, flag);
692 device_release(self);
693 return val;
694 }
695
696 int
697 mfcspoll(dev_t dev, int events, struct lwp *l)
698 {
699 struct mfcs_softc *sc;
700 struct tty *tp;= sc->sc_tty;
701
702 self = device_lookup_acquire(&mfcs_cd, dev & 31);
703 if (self == NULL)
704 return ENXIO;
705 sc = device_private(self);
706 if (sc == NULL) {
707 device_release(self);
708 return ENXIO;
709 }
710 tp = sc->sc_tty;
711 if (tp == NULL) {
712 device_release(self);
713 return(ENXIO);
714 }
715 val = tp->t_linesw->l_poll(tp, uio, flag);
716 device_release(self);
717 return val;
718 }
719
720 struct tty *
721 mfcstty(dev_t dev)
722 {
723 device_t self;
724 struct tty *tty;
725 struct mfcs_softc *sc;
726
727 self = device_lookup_acquire(&mfcs_cd, dev & 31);
728 if (self == NULL)
729 return NULL;
730 sc = device_private(self);
731 if (sc != NULL)
732 tty = sc->sc_tty;
733
734 device_free(self);
735 return tty;
736 }
737
738 int
739 mfcsioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
740 {
741 device_t self;
742 register struct tty *tp;
743 register int error;
744 struct mfcs_softc *sc;
745
746 self = device_lookup_acquire(&mfcs_cd, dev & 31);
747 if (self == NULL)
748 return ENXIO;
749 sc = = device_private(self);
750 if (sc == NULL) {
751 device_release(self);
752 return ENXIO;
753 }
754 tp = sc->sc_tty;
755 if (!tp) {
756 device_release(self);
757 return ENXIO;
758 }
759
760 error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l);
761 if (error != EPASSTHROUGH) {
762 device_release(self);
763 return(error);
764 }
765
766 error = ttioctl(tp, cmd, data, flag, l);
767 if (error != EPASSTHROUGH) {
768 device_release(self);
769 return(error);
770 }
771
772 switch (cmd) {
773 case TIOCSBRK:
774 sc->sc_duart->ch_cr = 0x60; /* start break */
775 break;
776
777 case TIOCCBRK:
778 sc->sc_duart->ch_cr = 0x70; /* stop break */
779 break;
780
781 case TIOCSDTR:
782 (void) mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIS);
783 break;
784
785 case TIOCCDTR:
786 (void) mfcsmctl(dev, TIOCM_DTR | TIOCM_RTS, DMBIC);
787 break;
788
789 case TIOCMSET:
790 (void) mfcsmctl(dev, *(int *) data, DMSET);
791 break;
792
793 case TIOCMBIS:
794 (void) mfcsmctl(dev, *(int *) data, DMBIS);
795 break;
796
797 case TIOCMBIC:
798 (void) mfcsmctl(dev, *(int *) data, DMBIC);
799 break;
800
801 case TIOCMGET:
802 *(int *)data = mfcsmctl(dev, 0, DMGET);
803 break;
804 case TIOCGFLAGS:
805 *(int *)data = SWFLAGS(dev);
806 break;
807 case TIOCSFLAGS:
808 error = kauth_authorize_device_tty(l->l_cred,
809 KAUTH_DEVICE_TTY_PRIVSET, tp);
810 if (error != 0) {
811 device_release(self);
812 return(EPERM);
813 }
814
815 sc->swflags = *(int *)data;
816 sc->swflags &= /* only allow valid flags */
817 (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | TIOCFLAG_CRTSCTS);
818 /* XXXX need to change duart parameters? */
819 break;
820 default:
821 return(EPASSTHROUGH);
822 }
823
824 device_release(self);
825 return(0);
826 }
827
828 int
829 mfcsparam(struct tty *tp, struct termios *t)
830 {
831 device_t self;
832 int cflag, unit, ospeed;
833 struct mfcs_softc *sc;
834 struct mfc_softc *scc;
835
836 cflag = t->c_cflag;
837 unit = tp->t_dev & 31;
838 self = device_lookup_acquire(&mfscd_cd, unit);
839 if (self == NULL)
840 return ENXIO;
841 sc = device_private(self);
842 if (sc == NULL) {
843 device_release(self);
844 return ENXIO;
845 }
846 scc = = sc->sc_mfc;
847 if (sc->flags & CT_USED) {
848 --scc->ct_usecnt;
849 sc->flags &= ~CT_USED;
850 }
851 ospeed = ttspeedtab(t->c_ospeed, scc->mfc_iii ? mfcs3speedtab2 :
852 mfcs2speedtab2);
853
854 /*
855 * If Baud Rate Generator can't generate requested speed,
856 * try to use the counter/timer.
857 */
858 if (ospeed < 0 && (scc->clk_frq % t->c_ospeed) == 0) {
859 ospeed = scc->clk_frq / t->c_ospeed; /* divisor */
860 if (scc->ct_usecnt > 0 && scc->ct_val != ospeed)
861 ospeed = -1;
862 else {
863 scc->sc_regs->du_ctur = ospeed >> 8;
864 scc->sc_regs->du_ctlr = ospeed;
865 scc->ct_val = ospeed;
866 ++scc->ct_usecnt;
867 sc->flags |= CT_USED;
868 ospeed = 0xdd;
869 }
870 }
871 /* XXXX 68681 duart could handle split speeds */
872 if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed)) {
873 device_release(self);
874 return(EINVAL);
875 }
876
877 /* XXXX handle parity, character size, stop bits, flow control */
878
879 /*
880 * copy to tty
881 */
882 tp->t_ispeed = t->c_ispeed;
883 tp->t_ospeed = t->c_ospeed;
884 tp->t_cflag = cflag;
885
886 /*
887 * enable interrupts
888 */
889 scc->imask |= (0x2 << ((unit & 1) * 4)) | 0x80;
890 scc->sc_regs->du_imr = scc->imask;
891 #if defined(DEBUG) && 0
892 printf("mfcsparam: speed %d => %x ct %d imask %x cflag %x\n",
893 t->c_ospeed, ospeed, scc->ct_val, scc->imask, cflag);
894 #endif
895 if (ospeed == 0)
896 (void)mfcsmctl(tp->t_dev, 0, DMSET); /* hang up line */
897 else {
898 /*
899 * (re)enable DTR
900 * and set baud rate. (8 bit mode)
901 */
902 (void)mfcsmctl(tp->t_dev, TIOCM_DTR | TIOCM_RTS, DMSET);
903 sc->sc_duart->ch_csr = ospeed;
904 }
905 device_release(self);
906 return(0);
907 }
908
909 int
910 mfcshwiflow(struct tty *tp, int flag)
911 {
912 device_t self;
913 struct mfcs_softc *sc;
914 int unit = tp->t_dev & 1;
915
916 self = device_lookup_acquire(&mfcs_cd, tp->t_dev & 31);
917 if (self == NULL)
918 return ENXIO;
919 sc = device_private(self);
920 if (sc == NULL) {
921 device_release(self);
922 return ENXIO;
923 }
924 if (flag)
925 sc->sc_regs->du_btrst = 1 << unit;
926 else
927 sc->sc_regs->du_btst = 1 << unit;
928
929 device_release(self);
930 return 1;
931 }
932
933 void
934 mfcsstart(struct tty *tp)
935 {
936 device_t self;
937 int cc, s, unit;
938 struct mfcs_softc *sc;
939 struct mfc_softc *scc;
940
941 self = device_lookup_acquire(&mfcs_cd, tp->t_dev & 31);
942 if (self == NULL)
943 return;
944 sc = device_private(self);
945 if (sc == NULL) {
946 device_release(self);
947 return;
948 }
949 scc = sc->sc_mfc;
950 if ((tp->t_state & TS_ISOPEN) == 0) {
951 device_release(self);
952 return;
953 }
954
955 unit = tp->t_dev & 1;
956
957 s = splser();
958 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))
959 goto out;
960
961 cc = tp->t_outq.c_cc;
962 if (!ttypull(tp) || (tp->t_state & TS_BUSY))
963 goto out;
964
965 /*
966 * We only do bulk transfers if using CTSRTS flow control, not for
967 * (probably sloooow) ixon/ixoff devices.
968 */
969 if ((tp->t_cflag & CRTSCTS) == 0)
970 cc = 1;
971
972 /*
973 * Limit the amount of output we do in one burst
974 * to prevent hogging the CPU.
975 */
976 if (cc > SEROBUF_SIZE)
977 cc = SEROBUF_SIZE;
978 cc = q_to_b(&tp->t_outq, sc->outbuf, cc);
979 if (cc > 0) {
980 tp->t_state |= TS_BUSY;
981
982 sc->ptr = sc->outbuf;
983 sc->end = sc->outbuf + cc;
984
985 /*
986 * Get first character out, then have TBE-interrupts blow out
987 * further characters, until buffer is empty, and TS_BUSY gets
988 * cleared.
989 */
990 sc->sc_duart->ch_tb = *sc->ptr++;
991 scc->imask |= 1 << (unit * 4);
992 sc->sc_regs->du_imr = scc->imask;
993 }
994 out:
995 splx(s);
996 device_release(self);
997 }
998
999 /*
1000 * Stop output on a line.
1001 */
1002 /*ARGSUSED*/
1003 void
1004 mfcsstop(struct tty *tp, int flag)
1005 {
1006 int s;
1007
1008 s = splser();
1009 if (tp->t_state & TS_BUSY) {
1010 if ((tp->t_state & TS_TTSTOP) == 0)
1011 tp->t_state |= TS_FLUSH;
1012 }
1013 splx(s);
1014 }
1015
1016 int
1017 mfcsmctl(dev_t dev, int bits, int how)
1018 {
1019 device_t self;
1020 int unit, s;
1021 u_char ub = 0;
1022 struct mfcs_softc *sc;
1023
1024 self = device_lookup_acquire(&mfcs_cd, dev & 31);
1025 if (self == NULL)
1026 return 0;
1027 sc = device_private(self);
1028 if (sc == NULL) {
1029 device_release(self);
1030 return 0;
1031 }
1032
1033 unit = dev & 1;
1034
1035 /*
1036 * convert TIOCM* mask into CIA mask
1037 * which is active low
1038 */
1039 if (how != DMGET) {
1040 /*
1041 * need to save current state of DTR & RTS ?
1042 */
1043 if (bits & TIOCM_DTR)
1044 ub |= 0x04 << unit;
1045 if (bits & TIOCM_RTS)
1046 ub |= 0x01 << unit;
1047 }
1048 s = splser();
1049 switch (how) {
1050 case DMSET:
1051 sc->sc_regs->du_btst = ub;
1052 sc->sc_regs->du_btrst = ub ^ (0x05 << unit);
1053 break;
1054
1055 case DMBIC:
1056 sc->sc_regs->du_btrst = ub;
1057 ub = ~sc->sc_regs->du_ip;
1058 break;
1059
1060 case DMBIS:
1061 sc->sc_regs->du_btst = ub;
1062 ub = ~sc->sc_regs->du_ip;
1063 break;
1064
1065 case DMGET:
1066 ub = ~sc->sc_regs->du_ip;
1067 break;
1068 }
1069 (void)splx(s);
1070
1071 /* XXXX should keep DTR & RTS states in softc? */
1072 bits = TIOCM_DTR | TIOCM_RTS;
1073 if (ub & (1 << unit))
1074 bits |= TIOCM_CTS;
1075 if (ub & (4 << unit))
1076 bits |= TIOCM_DSR;
1077 if (ub & (0x10 << unit))
1078 bits |= TIOCM_CD;
1079 /* XXXX RI is not supported on all boards */
1080 if (sc->sc_regs->pad26 & (1 << unit))
1081 bits |= TIOCM_RI;
1082
1083 device_release(self);
1084 return(bits);
1085 }
1086
1087 /*
1088 * Level 6 interrupt processing for the MultiFaceCard 68681 DUART
1089 */
1090
1091 int
1092 mfcintr(void *arg)
1093 {
1094 device_t self;
1095 struct mfc_softc *scc = arg;
1096 struct mfcs_softc *sc;
1097 struct mfc_regs *regs;
1098 struct tty *tp;
1099 int istat, unit;
1100 u_short c;
1101
1102 regs = scc->sc_regs;
1103 istat = regs->du_isr & scc->imask;
1104 if (istat == 0)
1105 return (0);
1106 unit = device_unit(scc->sc_dev) * 2;
1107 if (istat & 0x02) { /* channel A receive interrupt */
1108 self = device_lookup_acquire(&mfcs_cd, unit);
1109 KASSERT(self != NULL);
1110 sc = device_private(self);
1111 while (1) {
1112 c = regs->du_sra << 8;
1113 if ((c & 0x0100) == 0)
1114 break;
1115 c |= regs->du_rba;
1116 if (sc->incnt == SERIBUF_SIZE)
1117 ++sc->ovfl;
1118 else {
1119 *sc->wptr++ = c;
1120 if (sc->wptr == sc->inbuf + SERIBUF_SIZE)
1121 sc->wptr = sc->inbuf;
1122 ++sc->incnt;
1123 if (sc->incnt > SERIBUF_SIZE - 16)
1124 regs->du_btrst = 1;
1125 }
1126 if (c & 0x1000)
1127 regs->du_cra = 0x40;
1128 }
1129 device_release(self);
1130 }
1131 if (istat & 0x20) { /* channel B receive interrupt */
1132 self = device_lookup_acquire(&mfcs_cd, unit);
1133 KASSERT(self != NULL);
1134 sc = device_private(self);
1135 while (1) {
1136 c = regs->du_srb << 8;
1137 if ((c & 0x0100) == 0)
1138 break;
1139 c |= regs->du_rbb;
1140 if (sc->incnt == SERIBUF_SIZE)
1141 ++sc->ovfl;
1142 else {
1143 *sc->wptr++ = c;
1144 if (sc->wptr == sc->inbuf + SERIBUF_SIZE)
1145 sc->wptr = sc->inbuf;
1146 ++sc->incnt;
1147 if (sc->incnt > SERIBUF_SIZE - 16)
1148 regs->du_btrst = 2;
1149 }
1150 if (c & 0x1000)
1151 regs->du_crb = 0x40;
1152 }
1153 device_release(self);
1154 }
1155 if (istat & 0x01) { /* channel A transmit interrupt */
1156 self = device_lookup_acquire(&mfcs_cd, unit);
1157 KASSERT(self != NULL);
1158 sc = device_private(self);
1159 tp = sc->sc_tty;
1160 if (sc->ptr == sc->end) {
1161 tp->t_state &= ~(TS_BUSY | TS_FLUSH);
1162 scc->imask &= ~0x01;
1163 regs->du_imr = scc->imask;
1164 softint_schedule(sc->mfcs_si);
1165 }
1166 else
1167 regs->du_tba = *sc->ptr++;
1168 device_release(self);
1169 }
1170 if (istat & 0x10) { /* channel B transmit interrupt */
1171 self = device_lookup_acquire(&mfcs_cd, unit);
1172 KASSERT(self != NULL);
1173 sc = device_private(self);
1174 tp = sc->sc_tty;
1175 if (sc->ptr == sc->end) {
1176 tp->t_state &= ~(TS_BUSY | TS_FLUSH);
1177 scc->imask &= ~0x10;
1178 regs->du_imr = scc->imask;
1179 softint_schedule(sc->mfcs_si);
1180 }
1181 else
1182 regs->du_tbb = *sc->ptr++;
1183 device_release(self);
1184 }
1185 if (istat & 0x80) { /* input port change interrupt */
1186 c = regs->du_ipcr;
1187 printf ("%s: ipcr %02x", device_xname(scc->sc_dev), c);
1188 }
1189 return(1);
1190 }
1191
1192 void
1193 mfcsxintr(int unit)
1194 {
1195 device_t self;
1196 int s1, s2, ovfl;
1197 struct mfcs_softc *sc;
1198 struct tty *tp = sc->sc_tty;
1199
1200 self = device_lookup_acquire(&mfcs_cd, unit);
1201 if (self == NULL)
1202 return;
1203 sc = device_private(self);
1204
1205 /*
1206 * Make sure we're not interrupted by another
1207 * vbl, but allow level6 ints
1208 */
1209 s1 = spltty();
1210
1211 /*
1212 * pass along any acumulated information
1213 * while input is not blocked
1214 */
1215 while (sc->incnt && (tp->t_state & TS_TBLOCK) == 0) {
1216 /*
1217 * no collision with ser_fastint()
1218 */
1219 mfcseint(unit, *sc->rptr++);
1220
1221 ovfl = 0;
1222 /* lock against mfcs_fastint() */
1223 s2 = splser();
1224 --sc->incnt;
1225 if (sc->rptr == sc->inbuf + SERIBUF_SIZE)
1226 sc->rptr = sc->inbuf;
1227 if (sc->ovfl != 0) {
1228 ovfl = sc->ovfl;
1229 sc->ovfl = 0;
1230 }
1231 splx(s2);
1232 if (ovfl != 0)
1233 log(LOG_WARNING, "%s: %d buffer overflow!\n",
1234 device_xname(sc->sc_dev), ovfl);
1235 }
1236 if (sc->incnt == 0 && (tp->t_state & TS_TBLOCK) == 0) {
1237 sc->sc_regs->du_btst = 1 << unit; /* XXXX */
1238 }
1239 splx(s1);
1240 device_release(self);
1241 }
1242
1243 void
1244 mfcseint(int unit, int stat)
1245 {
1246 device_t self;
1247 struct mfcs_softc *sc;
1248 struct tty *tp;
1249 u_char ch;
1250 int c;
1251
1252 self = device_lookup_acquire(&mfcs_cd, unit);
1253 if (self == NULL)
1254 return;
1255 sc = device_private(self);
1256
1257 tp = sc->sc_tty;
1258 ch = stat & 0xff;
1259 c = ch;
1260
1261 if ((tp->t_state & TS_ISOPEN) == 0) {
1262 #ifdef KGDB
1263 extern const struct cdevsw ser_cdevsw;
1264 int maj;
1265
1266 /* we don't care about parity errors */
1267 maj = cdevsw_lookup_major(&ser_cdevsw);
1268 if (kgdb_dev == makedev(maj, unit) && c == FRAME_END)
1269 kgdb_connect(0); /* trap into kgdb */
1270 #endif
1271 device_release(self);
1272 return;
1273 }
1274
1275 /*
1276 * Check for break and (if enabled) parity error.
1277 */
1278 if (stat & 0xc000)
1279 c |= TTY_FE;
1280 else if (stat & 0x2000)
1281 c |= TTY_PE;
1282
1283 if (stat & 0x1000)
1284 log(LOG_WARNING, "%s: fifo overflow\n",
1285 device_xname(device_lookup_private(&mfcs_cd, unit)));
1286
1287 tp->t_linesw->l_rint(c, tp);
1288
1289 device_release(self);
1290 }
1291
1292 /*
1293 * This interrupt is periodically invoked in the vertical blank
1294 * interrupt. It's used to keep track of the modem control lines
1295 * and (new with the fast_int code) to move accumulated data
1296 * up into the tty layer.
1297 */
1298 void
1299 mfcsmint(int unit)
1300 {
1301 device_t self;
1302 struct tty *tp;
1303 struct mfcs_softc *sc;
1304 u_char stat, last, istat;
1305
1306 self = device_lookup_acquire(&mfcs_cd, unit);
1307 if (self == NULL)
1308 return;
1309 sc = device_private(self);
1310 if (sc == NULL) {
1311 device_release(self);
1312 return;
1313 }
1314 tp = sc->sc_tty;
1315 if (!tp) {
1316 device_release(self);
1317 return;
1318 }
1319
1320 if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
1321 sc->rptr = sc->wptr = sc->inbuf;
1322 sc->incnt = 0;
1323 device_release(self);
1324 return;
1325 }
1326 /*
1327 * empty buffer
1328 */
1329 mfcsxintr(unit);
1330
1331 stat = ~sc->sc_regs->du_ip;
1332 last = sc->sc_mfc->last_ip;
1333 sc->sc_mfc->last_ip = stat;
1334
1335 /*
1336 * check whether any interesting signal changed state
1337 */
1338 istat = stat ^ last;
1339
1340 if ((istat & (0x10 << (unit & 1))) && /* CD changed */
1341 (SWFLAGS(tp->t_dev) & TIOCFLAG_SOFTCAR) == 0) {
1342 if (stat & (0x10 << (unit & 1)))
1343 tp->t_linesw->l_modem(tp, 1);
1344 else if (tp->t_linesw->l_modem(tp, 0) == 0) {
1345 sc->sc_regs->du_btrst = 0x0a << (unit & 1);
1346 }
1347 }
1348 device_release(self);
1349 }
1350
1351 void
1352 mfcs_intr_soft(void *arg)
1353 {
1354 struct mfcs_softc *sc = (struct mfcs_softc *)arg;
1355 struct tty *tp = sc->sc_tty;
1356
1357 if (tp->t_linesw)
1358 tp->t_linesw->l_start(tp);
1359 else
1360 mfcsstart(tp);
1361 }
1362