dcm.c revision 1.74 1 /* $NetBSD: dcm.c,v 1.74 2006/09/09 11:09:48 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1982, 1986, 1990, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * the Systems Programming Group of the University of Utah Computer
45 * Science Department.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * from Utah: $Hdr: dcm.c 1.29 92/01/21$
72 *
73 * @(#)dcm.c 8.4 (Berkeley) 1/12/94
74 */
75 /*
76 * Copyright (c) 1988 University of Utah.
77 *
78 * This code is derived from software contributed to Berkeley by
79 * the Systems Programming Group of the University of Utah Computer
80 * Science Department.
81 *
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
84 * are met:
85 * 1. Redistributions of source code must retain the above copyright
86 * notice, this list of conditions and the following disclaimer.
87 * 2. Redistributions in binary form must reproduce the above copyright
88 * notice, this list of conditions and the following disclaimer in the
89 * documentation and/or other materials provided with the distribution.
90 * 3. All advertising materials mentioning features or use of this software
91 * must display the following acknowledgement:
92 * This product includes software developed by the University of
93 * California, Berkeley and its contributors.
94 * 4. Neither the name of the University nor the names of its contributors
95 * may be used to endorse or promote products derived from this software
96 * without specific prior written permission.
97 *
98 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
99 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108 * SUCH DAMAGE.
109 *
110 * from Utah: $Hdr: dcm.c 1.29 92/01/21$
111 *
112 * @(#)dcm.c 8.4 (Berkeley) 1/12/94
113 */
114
115 /*
116 * TODO:
117 * Timeouts
118 * Test console support.
119 */
120
121 /*
122 * 98642/MUX
123 */
124
125 #include <sys/cdefs.h>
126 __KERNEL_RCSID(0, "$NetBSD: dcm.c,v 1.74 2006/09/09 11:09:48 tsutsui Exp $");
127
128 #include "opt_kgdb.h"
129
130 #include <sys/param.h>
131 #include <sys/systm.h>
132 #include <sys/ioctl.h>
133 #include <sys/proc.h>
134 #include <sys/tty.h>
135 #include <sys/conf.h>
136 #include <sys/file.h>
137 #include <sys/uio.h>
138 #include <sys/kernel.h>
139 #include <sys/syslog.h>
140 #include <sys/time.h>
141 #include <sys/device.h>
142 #include <sys/kauth.h>
143
144 #include <machine/bus.h>
145
146 #include <dev/cons.h>
147
148 #include <hp300/dev/diovar.h>
149 #include <hp300/dev/diodevs.h>
150 #include <hp300/dev/dcmreg.h>
151
152 #include "ioconf.h"
153
154 #ifndef DEFAULT_BAUD_RATE
155 #define DEFAULT_BAUD_RATE 9600
156 #endif
157
158 static const struct speedtab dcmspeedtab[] = {
159 { 0, BR_0 },
160 { 50, BR_50 },
161 { 75, BR_75 },
162 { 110, BR_110 },
163 { 134, BR_134 },
164 { 150, BR_150 },
165 { 300, BR_300 },
166 { 600, BR_600 },
167 { 1200, BR_1200 },
168 { 1800, BR_1800 },
169 { 2400, BR_2400 },
170 { 4800, BR_4800 },
171 { 9600, BR_9600 },
172 { 19200, BR_19200 },
173 { 38400, BR_38400 },
174 { -1, -1 },
175 };
176
177 /* u-sec per character based on baudrate (assumes 1 start/8 data/1 stop bit) */
178 #define DCM_USPERCH(s) (10000000 / (s))
179
180 /*
181 * Per board interrupt scheme. 16.7ms is the polling interrupt rate
182 * (16.7ms is about 550 baud, 38.4k is 72 chars in 16.7ms).
183 */
184 #define DIS_TIMER 0
185 #define DIS_PERCHAR 1
186 #define DIS_RESET 2
187
188 static int dcmistype = -1; /* -1 == dynamic, 0 == timer, 1 == perchar */
189 static int dcminterval = 5; /* interval (secs) between checks */
190 struct dcmischeme {
191 int dis_perchar; /* non-zero if interrupting per char */
192 long dis_time; /* last time examined */
193 int dis_intr; /* recv interrupts during last interval */
194 int dis_char; /* characters read during last interval */
195 };
196
197 #ifdef KGDB
198 /*
199 * Kernel GDB support
200 */
201 #include <machine/remote-sl.h>
202
203 extern dev_t kgdb_dev;
204 extern int kgdb_rate;
205 extern int kgdb_debug_init;
206 #endif
207
208 /* #define DCMSTATS */
209
210 #ifdef DEBUG
211 int dcmdebug = 0x0;
212 #define DDB_SIOERR 0x01
213 #define DDB_PARAM 0x02
214 #define DDB_INPUT 0x04
215 #define DDB_OUTPUT 0x08
216 #define DDB_INTR 0x10
217 #define DDB_IOCTL 0x20
218 #define DDB_INTSCHM 0x40
219 #define DDB_MODEM 0x80
220 #define DDB_OPENCLOSE 0x100
221 #endif
222
223 #ifdef DCMSTATS
224 #define DCMRBSIZE 94
225 #define DCMXBSIZE 24
226
227 struct dcmstats {
228 long xints; /* # of xmit ints */
229 long xchars; /* # of xmit chars */
230 long xempty; /* times outq is empty in dcmstart */
231 long xrestarts; /* times completed while xmitting */
232 long rints; /* # of recv ints */
233 long rchars; /* # of recv chars */
234 long xsilo[DCMXBSIZE+2]; /* times this many chars xmit on one int */
235 long rsilo[DCMRBSIZE+2]; /* times this many chars read on one int */
236 };
237 #endif
238
239 #define DCMUNIT(x) (minor(x) & 0x7ffff)
240 #define DCMDIALOUT(x) (minor(x) & 0x80000)
241 #define DCMBOARD(x) (((x) >> 2) & 0x3f)
242 #define DCMPORT(x) ((x) & 3)
243
244 /*
245 * Conversion from "HP DCE" to almost-normal DCE: on the 638 8-port mux,
246 * the distribution panel uses "HP DCE" conventions. If requested via
247 * the device flags, we swap the inputs to something closer to normal DCE,
248 * allowing a straight-through cable to a DTE or a reversed cable
249 * to a DCE (reversing 2-3, 4-5, 8-20 and leaving 6 unconnected;
250 * this gets "DCD" on pin 20 and "CTS" on 4, but doesn't connect
251 * DSR or make RTS work, though). The following gives the full
252 * details of a cable from this mux panel to a modem:
253 *
254 * HP modem
255 * name pin pin name
256 * HP inputs:
257 * "Rx" 2 3 Tx
258 * CTS 4 5 CTS (only needed for CCTS_OFLOW)
259 * DCD 20 8 DCD
260 * "DSR" 9 6 DSR (unneeded)
261 * RI 22 22 RI (unneeded)
262 *
263 * HP outputs:
264 * "Tx" 3 2 Rx
265 * "DTR" 6 not connected
266 * "RTS" 8 20 DTR
267 * "SR" 23 4 RTS (often not needed)
268 */
269 #define hp2dce_in(ibits) (iconv[(ibits) & 0xf])
270 static const char iconv[16] = {
271 0, MI_DM, MI_CTS, MI_CTS|MI_DM,
272 MI_CD, MI_CD|MI_DM, MI_CD|MI_CTS, MI_CD|MI_CTS|MI_DM,
273 MI_RI, MI_RI|MI_DM, MI_RI|MI_CTS, MI_RI|MI_CTS|MI_DM,
274 MI_RI|MI_CD, MI_RI|MI_CD|MI_DM, MI_RI|MI_CD|MI_CTS,
275 MI_RI|MI_CD|MI_CTS|MI_DM
276 };
277
278 /*
279 * Note that 8-port boards appear as 2 4-port boards at consecutive
280 * select codes.
281 */
282 #define NDCMPORT 4
283
284 struct dcm_softc {
285 struct device sc_dev; /* generic device glue */
286
287 bus_space_tag_t sc_bst;
288 bus_space_handle_t sc_bsh;
289
290 struct dcmdevice *sc_dcm; /* pointer to hardware */
291 struct tty *sc_tty[NDCMPORT]; /* our tty instances */
292 struct modemreg *sc_modem[NDCMPORT]; /* modem control */
293 char sc_mcndlast[NDCMPORT]; /* XXX last modem status for port */
294 short sc_softCAR; /* mask of ports with soft-carrier */
295 struct dcmischeme sc_scheme; /* interrupt scheme for board */
296
297 /*
298 * Mask of soft-carrier bits in config flags.
299 */
300 #define DCM_SOFTCAR 0x0000000f
301
302 int sc_flags; /* misc. configuration info */
303
304 /*
305 * Bits for sc_flags
306 */
307 #define DCM_ACTIVE 0x00000001 /* indicates board is alive */
308 #define DCM_ISCONSOLE 0x00000002 /* indicates board is console */
309 #define DCM_STDDCE 0x00000010 /* re-map DCE to standard */
310 #define DCM_FLAGMASK (DCM_STDDCE) /* mask of valid bits in config flags */
311
312 #ifdef DCMSTATS
313 struct dcmstats sc_stats; /* metrics gathering */
314 #endif
315 };
316
317 static int dcmintr(void *);
318 static void dcmpint(struct dcm_softc *, int, int);
319 static void dcmrint(struct dcm_softc *);
320 static void dcmreadbuf(struct dcm_softc *, int);
321 static void dcmxint(struct dcm_softc *, int);
322 static void dcmmint(struct dcm_softc *, int, int);
323
324 static int dcmparam(struct tty *, struct termios *);
325 static void dcmstart(struct tty *);
326 static int dcmmctl(dev_t, int, int);
327 static void dcmsetischeme(int, int);
328 static void dcminit(struct dcmdevice *, int, int);
329
330 static int dcmselftest(struct dcm_softc *);
331
332 static int dcmcngetc(dev_t);
333 static void dcmcnputc(dev_t, int);
334
335 int dcmcnattach(bus_space_tag_t, bus_addr_t, int);
336
337 static int dcmmatch(struct device *, struct cfdata *, void *);
338 static void dcmattach(struct device *, struct device *, void *);
339
340 CFATTACH_DECL(dcm, sizeof(struct dcm_softc),
341 dcmmatch, dcmattach, NULL, NULL);
342
343 /*
344 * Stuff for DCM console support. This could probably be done a little
345 * better.
346 */
347 static struct dcmdevice *dcm_cn = NULL; /* pointer to hardware */
348 static int dcmconsinit; /* has been initialized */
349 /* static int dcm_lastcnpri = CN_DEAD; */ /* XXX last priority */
350
351 static struct consdev dcm_cons = {
352 NULL,
353 NULL,
354 dcmcngetc,
355 dcmcnputc,
356 nullcnpollc,
357 NULL,
358 NULL,
359 NULL,
360 NODEV,
361 CN_REMOTE
362 };
363 int dcmconscode;
364 int dcmdefaultrate = DEFAULT_BAUD_RATE;
365 int dcmconbrdbusy = 0;
366
367 static dev_type_open(dcmopen);
368 static dev_type_close(dcmclose);
369 static dev_type_read(dcmread);
370 static dev_type_write(dcmwrite);
371 static dev_type_ioctl(dcmioctl);
372 static dev_type_stop(dcmstop);
373 static dev_type_tty(dcmtty);
374 static dev_type_poll(dcmpoll);
375
376 const struct cdevsw dcm_cdevsw = {
377 dcmopen, dcmclose, dcmread, dcmwrite, dcmioctl,
378 dcmstop, dcmtty, dcmpoll, nommap, ttykqfilter, D_TTY
379 };
380
381 static int
382 dcmmatch(struct device *parent, struct cfdata *match, void *aux)
383 {
384 struct dio_attach_args *da = aux;
385
386 switch (da->da_id) {
387 case DIO_DEVICE_ID_DCM:
388 case DIO_DEVICE_ID_DCMREM:
389 return 1;
390 }
391
392 return 0;
393 }
394
395 static void
396 dcmattach(struct device *parent, struct device *self, void *aux)
397 {
398 struct dcm_softc *sc = (struct dcm_softc *)self;
399 struct dio_attach_args *da = aux;
400 struct dcmdevice *dcm;
401 int brd = device_unit(self);
402 int scode = da->da_scode;
403 int i, mbits, code;
404
405 sc->sc_flags = 0;
406
407 if (scode == dcmconscode) {
408 dcm = dcm_cn;
409 sc->sc_flags |= DCM_ISCONSOLE;
410
411 /*
412 * We didn't know which unit this would be during
413 * the console probe, so we have to fixup cn_dev here.
414 * Note that we always assume port 1 on the board.
415 */
416 cn_tab->cn_dev = makedev(cdevsw_lookup_major(&dcm_cdevsw),
417 (brd << 2) | DCMCONSPORT);
418 } else {
419 sc->sc_bst = da->da_bst;
420 if (bus_space_map(sc->sc_bst, da->da_addr, da->da_size,
421 BUS_SPACE_MAP_LINEAR, &sc->sc_bsh)) {
422 printf("\n%s: can't map registers\n",
423 sc->sc_dev.dv_xname);
424 return;
425 }
426 dcm = (struct dcmdevice *)bus_space_vaddr(sc->sc_bst,
427 sc->sc_bsh);
428 }
429
430 sc->sc_dcm = dcm;
431
432 /*
433 * XXX someone _should_ fix this; the self test screws
434 * autoconfig messages.
435 */
436 if ((sc->sc_flags & DCM_ISCONSOLE) && dcmselftest(sc)) {
437 printf("\n%s: self-test failed\n", sc->sc_dev.dv_xname);
438 return;
439 }
440
441 /* Extract configuration info from flags. */
442 sc->sc_softCAR = device_cfdata(self)->cf_flags & DCM_SOFTCAR;
443 sc->sc_flags |= device_cfdata(self)->cf_flags & DCM_FLAGMASK;
444
445 /* Mark our unit as configured. */
446 sc->sc_flags |= DCM_ACTIVE;
447
448 /* Establish the interrupt handler. */
449 (void) dio_intr_establish(dcmintr, sc, da->da_ipl, IPL_TTY);
450
451 if (dcmistype == DIS_TIMER)
452 dcmsetischeme(brd, DIS_RESET|DIS_TIMER);
453 else
454 dcmsetischeme(brd, DIS_RESET|DIS_PERCHAR);
455
456 /* load pointers to modem control */
457 sc->sc_modem[0] = &dcm->dcm_modem0;
458 sc->sc_modem[1] = &dcm->dcm_modem1;
459 sc->sc_modem[2] = &dcm->dcm_modem2;
460 sc->sc_modem[3] = &dcm->dcm_modem3;
461
462 /* set DCD (modem) and CTS (flow control) on all ports */
463 if (sc->sc_flags & DCM_STDDCE)
464 mbits = hp2dce_in(MI_CD|MI_CTS);
465 else
466 mbits = MI_CD|MI_CTS;
467
468 for (i = 0; i < NDCMPORT; i++)
469 sc->sc_modem[i]->mdmmsk = mbits;
470
471 /*
472 * Get current state of mdmin register on all ports, so that
473 * deltas will work properly.
474 */
475 for (i = 0; i < NDCMPORT; i++) {
476 code = sc->sc_modem[i]->mdmin;
477 if (sc->sc_flags & DCM_STDDCE)
478 code = hp2dce_in(code);
479 sc->sc_mcndlast[i] = code;
480 }
481
482 dcm->dcm_ic = IC_IE; /* turn all interrupts on */
483
484 /*
485 * Need to reset baud rate, etc. of next print so reset dcmconsinit.
486 * Also make sure console is always "hardwired"
487 */
488 if (sc->sc_flags & DCM_ISCONSOLE) {
489 dcmconsinit = 0;
490 sc->sc_softCAR |= (1 << DCMCONSPORT);
491 printf(": console on port %d\n", DCMCONSPORT);
492 } else
493 printf("\n");
494
495 #ifdef KGDB
496 if (cdevsw_lookup(kgdb_dev) == &dcm_cdevsw &&
497 DCMBOARD(DCMUNIT(kgdb_dev)) == brd) {
498 if (dcmconsole == DCMUNIT(kgdb_dev)) /* XXX fixme */
499 kgdb_dev = NODEV; /* can't debug over console port */
500 #ifndef KGDB_CHEAT
501 /*
502 * The following could potentially be replaced
503 * by the corresponding code in dcmcnprobe.
504 */
505 else {
506 dcminit(dcm, DCMPORT(DCMUNIT(kgdb_dev)),
507 kgdb_rate);
508 if (kgdb_debug_init) {
509 printf("%s port %d: ", sc->sc_dev.dv_xname,
510 DCMPORT(DCMUNIT(kgdb_dev)));
511 kgdb_connect(1);
512 } else
513 printf("%s port %d: kgdb enabled\n",
514 sc->sc_dev.dv_xname,
515 DCMPORT(DCMUNIT(kgdb_dev)));
516 }
517 /* end could be replaced */
518 #endif /* KGDB_CHEAT */
519 }
520 #endif /* KGDB */
521 }
522
523 /* ARGSUSED */
524 static int
525 dcmopen(dev_t dev, int flag, int mode, struct lwp *l)
526 {
527 struct dcm_softc *sc;
528 struct tty *tp;
529 int unit, brd, port;
530 int error = 0, mbits, s;
531
532 unit = DCMUNIT(dev);
533 brd = DCMBOARD(unit);
534 port = DCMPORT(unit);
535
536 if (brd >= dcm_cd.cd_ndevs || port >= NDCMPORT ||
537 (sc = dcm_cd.cd_devs[brd]) == NULL)
538 return ENXIO;
539
540 if ((sc->sc_flags & DCM_ACTIVE) == 0)
541 return ENXIO;
542
543 if (sc->sc_tty[port] == NULL) {
544 tp = sc->sc_tty[port] = ttymalloc();
545 tty_attach(tp);
546 } else
547 tp = sc->sc_tty[port];
548
549 tp->t_oproc = dcmstart;
550 tp->t_param = dcmparam;
551 tp->t_dev = dev;
552
553 if ((tp->t_state & TS_ISOPEN) &&
554 (tp->t_state & TS_XCLUDE) &&
555 kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
556 &l->l_acflag) != 0)
557 return EBUSY;
558
559 s = spltty();
560
561 if ((tp->t_state & TS_ISOPEN) == 0 && tp->t_wopen == 0) {
562 /*
563 * Sanity clause: reset the card on first open.
564 * The card might be left in an inconsistent state
565 * if the card memory is read inadvertently.
566 */
567 dcminit(sc->sc_dcm, port, dcmdefaultrate);
568
569 ttychars(tp);
570 tp->t_iflag = TTYDEF_IFLAG;
571 tp->t_oflag = TTYDEF_OFLAG;
572 tp->t_cflag = TTYDEF_CFLAG;
573 tp->t_lflag = TTYDEF_LFLAG;
574 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
575
576 (void) dcmparam(tp, &tp->t_termios);
577 ttsetwater(tp);
578
579 /* Set modem control state. */
580 mbits = MO_ON;
581 if (sc->sc_flags & DCM_STDDCE)
582 mbits |= MO_SR; /* pin 23, could be used as RTS */
583
584 (void) dcmmctl(dev, mbits, DMSET); /* enable port */
585
586 /* Set soft-carrier if so configured. */
587 if ((sc->sc_softCAR & (1 << port)) ||
588 (dcmmctl(dev, MO_OFF, DMGET) & MI_CD))
589 tp->t_state |= TS_CARR_ON;
590 }
591
592 splx(s);
593
594 #ifdef DEBUG
595 if (dcmdebug & DDB_MODEM)
596 printf("%s: dcmopen port %d softcarr %c\n",
597 sc->sc_dev.dv_xname, port,
598 (tp->t_state & TS_CARR_ON) ? '1' : '0');
599 #endif
600
601 error = ttyopen(tp, DCMDIALOUT(dev), (flag & O_NONBLOCK));
602 if (error)
603 goto bad;
604
605 #ifdef DEBUG
606 if (dcmdebug & DDB_OPENCLOSE)
607 printf("%s port %d: dcmopen: st %x fl %x\n",
608 sc->sc_dev.dv_xname, port, tp->t_state, tp->t_flags);
609 #endif
610 error = (*tp->t_linesw->l_open)(dev, tp);
611
612 bad:
613 return error;
614 }
615
616 /*ARGSUSED*/
617 static int
618 dcmclose(dev_t dev, int flag, int mode, struct lwp *l)
619 {
620 int s, unit, board, port;
621 struct dcm_softc *sc;
622 struct tty *tp;
623
624 unit = DCMUNIT(dev);
625 board = DCMBOARD(unit);
626 port = DCMPORT(unit);
627
628 sc = dcm_cd.cd_devs[board];
629 tp = sc->sc_tty[port];
630
631 (*tp->t_linesw->l_close)(tp, flag);
632
633 s = spltty();
634
635 if (tp->t_cflag & HUPCL || tp->t_wopen != 0 ||
636 (tp->t_state & TS_ISOPEN) == 0)
637 (void) dcmmctl(dev, MO_OFF, DMSET);
638 #ifdef DEBUG
639 if (dcmdebug & DDB_OPENCLOSE)
640 printf("%s port %d: dcmclose: st %x fl %x\n",
641 sc->sc_dev.dv_xname, port, tp->t_state, tp->t_flags);
642 #endif
643 splx(s);
644 ttyclose(tp);
645 #if 0
646 tty_detach(tp);
647 ttyfree(tp);
648 sc->sc_tty[port] == NULL;
649 #endif
650 return 0;
651 }
652
653 static int
654 dcmread(dev_t dev, struct uio *uio, int flag)
655 {
656 int unit, board, port;
657 struct dcm_softc *sc;
658 struct tty *tp;
659
660 unit = DCMUNIT(dev);
661 board = DCMBOARD(unit);
662 port = DCMPORT(unit);
663
664 sc = dcm_cd.cd_devs[board];
665 tp = sc->sc_tty[port];
666
667 return (*tp->t_linesw->l_read)(tp, uio, flag);
668 }
669
670 static int
671 dcmwrite(dev_t dev, struct uio *uio, int flag)
672 {
673 int unit, board, port;
674 struct dcm_softc *sc;
675 struct tty *tp;
676
677 unit = DCMUNIT(dev);
678 board = DCMBOARD(unit);
679 port = DCMPORT(unit);
680
681 sc = dcm_cd.cd_devs[board];
682 tp = sc->sc_tty[port];
683
684 return (*tp->t_linesw->l_write)(tp, uio, flag);
685 }
686
687 static int
688 dcmpoll(dev_t dev, int events, struct lwp *l)
689 {
690 int unit, board, port;
691 struct dcm_softc *sc;
692 struct tty *tp;
693
694 unit = DCMUNIT(dev);
695 board = DCMBOARD(unit);
696 port = DCMPORT(unit);
697
698 sc = dcm_cd.cd_devs[board];
699 tp = sc->sc_tty[port];
700
701 return (*tp->t_linesw->l_poll)(tp, events, l);
702 }
703
704 static struct tty *
705 dcmtty(dev_t dev)
706 {
707 int unit, board, port;
708 struct dcm_softc *sc;
709
710 unit = DCMUNIT(dev);
711 board = DCMBOARD(unit);
712 port = DCMPORT(unit);
713
714 sc = dcm_cd.cd_devs[board];
715
716 return sc->sc_tty[port];
717 }
718
719 static int
720 dcmintr(void *arg)
721 {
722 struct dcm_softc *sc = arg;
723 struct dcmdevice *dcm = sc->sc_dcm;
724 struct dcmischeme *dis = &sc->sc_scheme;
725 int brd = device_unit(&sc->sc_dev);
726 int code, i;
727 int pcnd[4], mcode, mcnd[4];
728
729 /*
730 * Do all guarded accesses right off to minimize
731 * block out of hardware.
732 */
733 SEM_LOCK(dcm);
734 if ((dcm->dcm_ic & IC_IR) == 0) {
735 SEM_UNLOCK(dcm);
736 return 0;
737 }
738 for (i = 0; i < 4; i++) {
739 pcnd[i] = dcm->dcm_icrtab[i].dcm_data;
740 dcm->dcm_icrtab[i].dcm_data = 0;
741 code = sc->sc_modem[i]->mdmin;
742 if (sc->sc_flags & DCM_STDDCE)
743 code = hp2dce_in(code);
744 mcnd[i] = code;
745 }
746 code = dcm->dcm_iir & IIR_MASK;
747 dcm->dcm_iir = 0; /* XXX doc claims read clears interrupt?! */
748 mcode = dcm->dcm_modemintr;
749 dcm->dcm_modemintr = 0;
750 SEM_UNLOCK(dcm);
751
752 #ifdef DEBUG
753 if (dcmdebug & DDB_INTR) {
754 printf("%s: dcmintr: iir %x pc %x/%x/%x/%x ",
755 sc->sc_dev.dv_xname, code, pcnd[0], pcnd[1],
756 pcnd[2], pcnd[3]);
757 printf("miir %x mc %x/%x/%x/%x\n",
758 mcode, mcnd[0], mcnd[1], mcnd[2], mcnd[3]);
759 }
760 #endif
761 if (code & IIR_TIMEO)
762 dcmrint(sc);
763 if (code & IIR_PORT0)
764 dcmpint(sc, 0, pcnd[0]);
765 if (code & IIR_PORT1)
766 dcmpint(sc, 1, pcnd[1]);
767 if (code & IIR_PORT2)
768 dcmpint(sc, 2, pcnd[2]);
769 if (code & IIR_PORT3)
770 dcmpint(sc, 3, pcnd[3]);
771 if (code & IIR_MODM) {
772 if (mcode == 0 || mcode & 0x1) /* mcode==0 -> 98642 board */
773 dcmmint(sc, 0, mcnd[0]);
774 if (mcode & 0x2)
775 dcmmint(sc, 1, mcnd[1]);
776 if (mcode & 0x4)
777 dcmmint(sc, 2, mcnd[2]);
778 if (mcode & 0x8)
779 dcmmint(sc, 3, mcnd[3]);
780 }
781
782 /*
783 * Chalk up a receiver interrupt if the timer running or one of
784 * the ports reports a special character interrupt.
785 */
786 if ((code & IIR_TIMEO) ||
787 ((pcnd[0]|pcnd[1]|pcnd[2]|pcnd[3]) & IT_SPEC))
788 dis->dis_intr++;
789 /*
790 * See if it is time to check/change the interrupt rate.
791 */
792 if (dcmistype < 0 &&
793 (i = time_second - dis->dis_time) >= dcminterval) {
794 /*
795 * If currently per-character and averaged over 70 interrupts
796 * per-second (66 is threshold of 600 baud) in last interval,
797 * switch to timer mode.
798 *
799 * XXX decay counts ala load average to avoid spikes?
800 */
801 if (dis->dis_perchar && dis->dis_intr > 70 * i)
802 dcmsetischeme(brd, DIS_TIMER);
803 /*
804 * If currently using timer and had more interrupts than
805 * received characters in the last interval, switch back
806 * to per-character. Note that after changing to per-char
807 * we must process any characters already in the queue
808 * since they may have arrived before the bitmap was setup.
809 *
810 * XXX decay counts?
811 */
812 else if (!dis->dis_perchar && dis->dis_intr > dis->dis_char) {
813 dcmsetischeme(brd, DIS_PERCHAR);
814 dcmrint(sc);
815 }
816 dis->dis_intr = dis->dis_char = 0;
817 dis->dis_time = time_second;
818 }
819 return 1;
820 }
821
822 /*
823 * Port interrupt. Can be two things:
824 * First, it might be a special character (exception interrupt);
825 * Second, it may be a buffer empty (transmit interrupt);
826 */
827 static void
828 dcmpint(struct dcm_softc *sc, int port, int code)
829 {
830
831 if (code & IT_SPEC)
832 dcmreadbuf(sc, port);
833 if (code & IT_TX)
834 dcmxint(sc, port);
835 }
836
837 static void
838 dcmrint(struct dcm_softc *sc)
839 {
840 int port;
841
842 for (port = 0; port < NDCMPORT; port++)
843 dcmreadbuf(sc, port);
844 }
845
846 static void
847 dcmreadbuf(struct dcm_softc *sc, int port)
848 {
849 struct dcmdevice *dcm = sc->sc_dcm;
850 struct dcmpreg *pp = dcm_preg(dcm, port);
851 struct dcmrfifo *fifo;
852 struct tty *tp;
853 int c, stat;
854 u_int head;
855 int nch = 0;
856 #ifdef DCMSTATS
857 struct dcmstats *dsp = &sc->sc_stats;
858
859 dsp->rints++;
860 #endif
861 tp = sc->sc_tty[port];
862 if (tp == NULL)
863 return;
864
865 if ((tp->t_state & TS_ISOPEN) == 0) {
866 #ifdef KGDB
867 int maj;
868
869 maj = cdevsw_lookup_major(&dcm_cdevsw);
870
871 if ((makedev(maj, minor(tp->t_dev)) == kgdb_dev) &&
872 (head = pp->r_head & RX_MASK) != (pp->r_tail & RX_MASK) &&
873 dcm->dcm_rfifos[3-port][head>>1].data_char == FRAME_START) {
874 pp->r_head = (head + 2) & RX_MASK;
875 kgdb_connect(0); /* trap into kgdb */
876 return;
877 }
878 #endif /* KGDB */
879 pp->r_head = pp->r_tail & RX_MASK;
880 return;
881 }
882
883 head = pp->r_head & RX_MASK;
884 fifo = &dcm->dcm_rfifos[3-port][head>>1];
885 /*
886 * XXX upper bound on how many chars we will take in one swallow?
887 */
888 while (head != (pp->r_tail & RX_MASK)) {
889 /*
890 * Get character/status and update head pointer as fast
891 * as possible to make room for more characters.
892 */
893 c = fifo->data_char;
894 stat = fifo->data_stat;
895 head = (head + 2) & RX_MASK;
896 pp->r_head = head;
897 fifo = head ? fifo+1 : &dcm->dcm_rfifos[3-port][0];
898 nch++;
899
900 #ifdef DEBUG
901 if (dcmdebug & DDB_INPUT)
902 printf("%s port %d: dcmreadbuf: c%x('%c') s%x f%x h%x t%x\n",
903 sc->sc_dev.dv_xname, port,
904 c&0xFF, c, stat&0xFF,
905 tp->t_flags, head, pp->r_tail);
906 #endif
907 /*
908 * Check for and handle errors
909 */
910 if (stat & RD_MASK) {
911 #ifdef DEBUG
912 if (dcmdebug & (DDB_INPUT|DDB_SIOERR))
913 printf("%s port %d: dcmreadbuf: err: c%x('%c') s%x\n",
914 sc->sc_dev.dv_xname, port,
915 stat, c&0xFF, c);
916 #endif
917 if (stat & (RD_BD | RD_FE))
918 c |= TTY_FE;
919 else if (stat & RD_PE)
920 c |= TTY_PE;
921 else if (stat & RD_OVF)
922 log(LOG_WARNING,
923 "%s port %d: silo overflow\n",
924 sc->sc_dev.dv_xname, port);
925 else if (stat & RD_OE)
926 log(LOG_WARNING,
927 "%s port %d: uart overflow\n",
928 sc->sc_dev.dv_xname, port);
929 }
930 (*tp->t_linesw->l_rint)(c, tp);
931 }
932 sc->sc_scheme.dis_char += nch;
933
934 #ifdef DCMSTATS
935 dsp->rchars += nch;
936 if (nch <= DCMRBSIZE)
937 dsp->rsilo[nch]++;
938 else
939 dsp->rsilo[DCMRBSIZE+1]++;
940 #endif
941 }
942
943 static void
944 dcmxint(struct dcm_softc *sc, int port)
945 {
946 struct tty *tp;
947
948 tp = sc->sc_tty[port];
949 if (tp == NULL || (tp->t_state & TS_ISOPEN) == 0)
950 return;
951
952 tp->t_state &= ~TS_BUSY;
953 if (tp->t_state & TS_FLUSH)
954 tp->t_state &= ~TS_FLUSH;
955 (*tp->t_linesw->l_start)(tp);
956 }
957
958 static void
959 dcmmint(struct dcm_softc *sc, int port, int mcnd)
960 {
961 int delta;
962 struct tty *tp;
963 struct dcmdevice *dcm = sc->sc_dcm;
964
965 #ifdef DEBUG
966 if (dcmdebug & DDB_MODEM)
967 printf("%s port %d: dcmmint: mcnd %x mcndlast %x\n",
968 sc->sc_dev.dv_xname, port, mcnd, sc->sc_mcndlast[port]);
969 #endif
970 delta = mcnd ^ sc->sc_mcndlast[port];
971 sc->sc_mcndlast[port] = mcnd;
972 tp = sc->sc_tty[port];
973 if (tp == NULL || (tp->t_state & TS_ISOPEN) == 0)
974 return;
975
976 if ((delta & MI_CTS) && (tp->t_state & TS_ISOPEN) &&
977 (tp->t_cflag & CCTS_OFLOW)) {
978 if (mcnd & MI_CTS) {
979 tp->t_state &= ~TS_TTSTOP;
980 ttstart(tp);
981 } else
982 tp->t_state |= TS_TTSTOP; /* inline dcmstop */
983 }
984 if (delta & MI_CD) {
985 if (mcnd & MI_CD)
986 (void)(*tp->t_linesw->l_modem)(tp, 1);
987 else if ((sc->sc_softCAR & (1 << port)) == 0 &&
988 (*tp->t_linesw->l_modem)(tp, 0) == 0) {
989 sc->sc_modem[port]->mdmout = MO_OFF;
990 SEM_LOCK(dcm);
991 dcm->dcm_modemchng |= (1 << port);
992 dcm->dcm_cr |= CR_MODM;
993 SEM_UNLOCK(dcm);
994 DELAY(10); /* time to change lines */
995 }
996 }
997 }
998
999 static int
1000 dcmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
1001 {
1002 struct dcm_softc *sc;
1003 struct tty *tp;
1004 struct dcmdevice *dcm;
1005 int board, port, unit = DCMUNIT(dev);
1006 int error, s;
1007
1008 port = DCMPORT(unit);
1009 board = DCMBOARD(unit);
1010
1011 sc = dcm_cd.cd_devs[board];
1012 dcm = sc->sc_dcm;
1013 tp = sc->sc_tty[port];
1014
1015 #ifdef DEBUG
1016 if (dcmdebug & DDB_IOCTL)
1017 printf("%s port %d: dcmioctl: cmd %lx data %x flag %x\n",
1018 sc->sc_dev.dv_xname, port, cmd, *data, flag);
1019 #endif
1020
1021 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
1022 if (error != EPASSTHROUGH)
1023 return error;
1024
1025 error = ttioctl(tp, cmd, data, flag, l);
1026 if (error != EPASSTHROUGH)
1027 return error;
1028
1029 switch (cmd) {
1030 case TIOCSBRK:
1031 /*
1032 * Wait for transmitter buffer to empty
1033 */
1034 s = spltty();
1035 while (dcm->dcm_thead[port].ptr != dcm->dcm_ttail[port].ptr)
1036 DELAY(DCM_USPERCH(tp->t_ospeed));
1037 SEM_LOCK(dcm);
1038 dcm->dcm_cmdtab[port].dcm_data |= CT_BRK;
1039 dcm->dcm_cr |= (1 << port); /* start break */
1040 SEM_UNLOCK(dcm);
1041 splx(s);
1042 break;
1043
1044 case TIOCCBRK:
1045 SEM_LOCK(dcm);
1046 dcm->dcm_cmdtab[port].dcm_data |= CT_BRK;
1047 dcm->dcm_cr |= (1 << port); /* end break */
1048 SEM_UNLOCK(dcm);
1049 break;
1050
1051 case TIOCSDTR:
1052 (void) dcmmctl(dev, MO_ON, DMBIS);
1053 break;
1054
1055 case TIOCCDTR:
1056 (void) dcmmctl(dev, MO_ON, DMBIC);
1057 break;
1058
1059 case TIOCMSET:
1060 (void) dcmmctl(dev, *(int *)data, DMSET);
1061 break;
1062
1063 case TIOCMBIS:
1064 (void) dcmmctl(dev, *(int *)data, DMBIS);
1065 break;
1066
1067 case TIOCMBIC:
1068 (void) dcmmctl(dev, *(int *)data, DMBIC);
1069 break;
1070
1071 case TIOCMGET:
1072 *(int *)data = dcmmctl(dev, 0, DMGET);
1073 break;
1074
1075 case TIOCGFLAGS: {
1076 int bits = 0;
1077
1078 if ((sc->sc_softCAR & (1 << port)))
1079 bits |= TIOCFLAG_SOFTCAR;
1080
1081 if (tp->t_cflag & CLOCAL)
1082 bits |= TIOCFLAG_CLOCAL;
1083
1084 *(int *)data = bits;
1085 break;
1086 }
1087
1088 case TIOCSFLAGS: {
1089 int userbits;
1090
1091 error = kauth_authorize_generic(l->l_cred,
1092 KAUTH_GENERIC_ISSUSER, &l->l_acflag);
1093 if (error)
1094 return EPERM;
1095
1096 userbits = *(int *)data;
1097
1098 if ((userbits & TIOCFLAG_SOFTCAR) ||
1099 ((sc->sc_flags & DCM_ISCONSOLE) &&
1100 (port == DCMCONSPORT)))
1101 sc->sc_softCAR |= (1 << port);
1102
1103 if (userbits & TIOCFLAG_CLOCAL)
1104 tp->t_cflag |= CLOCAL;
1105
1106 break;
1107 }
1108
1109 default:
1110 return EPASSTHROUGH;
1111 }
1112 return 0;
1113 }
1114
1115 static int
1116 dcmparam(struct tty *tp, struct termios *t)
1117 {
1118 struct dcm_softc *sc;
1119 struct dcmdevice *dcm;
1120 int unit, board, port, mode, cflag = t->c_cflag;
1121 int ospeed = ttspeedtab(t->c_ospeed, dcmspeedtab);
1122
1123 unit = DCMUNIT(tp->t_dev);
1124 board = DCMBOARD(unit);
1125 port = DCMPORT(unit);
1126
1127 sc = dcm_cd.cd_devs[board];
1128 dcm = sc->sc_dcm;
1129
1130 /* check requested parameters */
1131 if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
1132 return EINVAL;
1133 /* and copy to tty */
1134 tp->t_ispeed = t->c_ispeed;
1135 tp->t_ospeed = t->c_ospeed;
1136 tp->t_cflag = cflag;
1137 if (ospeed == 0) {
1138 (void)dcmmctl(DCMUNIT(tp->t_dev), MO_OFF, DMSET);
1139 return 0;
1140 }
1141
1142 mode = 0;
1143 switch (cflag&CSIZE) {
1144 case CS5:
1145 mode = LC_5BITS; break;
1146 case CS6:
1147 mode = LC_6BITS; break;
1148 case CS7:
1149 mode = LC_7BITS; break;
1150 case CS8:
1151 mode = LC_8BITS; break;
1152 }
1153 if (cflag&PARENB) {
1154 if (cflag&PARODD)
1155 mode |= LC_PODD;
1156 else
1157 mode |= LC_PEVEN;
1158 }
1159 if (cflag&CSTOPB)
1160 mode |= LC_2STOP;
1161 else
1162 mode |= LC_1STOP;
1163 #ifdef DEBUG
1164 if (dcmdebug & DDB_PARAM)
1165 printf("%s port %d: dcmparam: cflag %x mode %x speed %d uperch %d\n",
1166 sc->sc_dev.dv_xname, port, cflag, mode, tp->t_ospeed,
1167 DCM_USPERCH(tp->t_ospeed));
1168 #endif
1169
1170 /*
1171 * Wait for transmitter buffer to empty.
1172 */
1173 while (dcm->dcm_thead[port].ptr != dcm->dcm_ttail[port].ptr)
1174 DELAY(DCM_USPERCH(tp->t_ospeed));
1175 /*
1176 * Make changes known to hardware.
1177 */
1178 dcm->dcm_data[port].dcm_baud = ospeed;
1179 dcm->dcm_data[port].dcm_conf = mode;
1180 SEM_LOCK(dcm);
1181 dcm->dcm_cmdtab[port].dcm_data |= CT_CON;
1182 dcm->dcm_cr |= (1 << port);
1183 SEM_UNLOCK(dcm);
1184 /*
1185 * Delay for config change to take place. Weighted by baud.
1186 * XXX why do we do this?
1187 */
1188 DELAY(16 * DCM_USPERCH(tp->t_ospeed));
1189 return 0;
1190 }
1191
1192 static void
1193 dcmstart(struct tty *tp)
1194 {
1195 struct dcm_softc *sc;
1196 struct dcmdevice *dcm;
1197 struct dcmpreg *pp;
1198 struct dcmtfifo *fifo;
1199 char *bp;
1200 u_int head, tail, next;
1201 int unit, board, port, nch;
1202 char buf[16];
1203 int s;
1204 #ifdef DCMSTATS
1205 struct dcmstats *dsp = &sc->sc_stats;
1206 int tch = 0;
1207 #endif
1208
1209 unit = DCMUNIT(tp->t_dev);
1210 board = DCMBOARD(unit);
1211 port = DCMPORT(unit);
1212
1213 sc = dcm_cd.cd_devs[board];
1214 dcm = sc->sc_dcm;
1215
1216 s = spltty();
1217 #ifdef DCMSTATS
1218 dsp->xints++;
1219 #endif
1220 #ifdef DEBUG
1221 if (dcmdebug & DDB_OUTPUT)
1222 printf("%s port %d: dcmstart: state %x flags %x outcc %d\n",
1223 sc->sc_dev.dv_xname, port, tp->t_state, tp->t_flags,
1224 tp->t_outq.c_cc);
1225 #endif
1226 if (tp->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP))
1227 goto out;
1228 if (tp->t_outq.c_cc <= tp->t_lowat) {
1229 if (tp->t_state&TS_ASLEEP) {
1230 tp->t_state &= ~TS_ASLEEP;
1231 wakeup((caddr_t)&tp->t_outq);
1232 }
1233 selwakeup(&tp->t_wsel);
1234 }
1235 if (tp->t_outq.c_cc == 0) {
1236 #ifdef DCMSTATS
1237 dsp->xempty++;
1238 #endif
1239 goto out;
1240 }
1241
1242 pp = dcm_preg(dcm, port);
1243 tail = pp->t_tail & TX_MASK;
1244 next = (tail + 1) & TX_MASK;
1245 head = pp->t_head & TX_MASK;
1246 if (head == next)
1247 goto out;
1248 fifo = &dcm->dcm_tfifos[3-port][tail];
1249 again:
1250 nch = q_to_b(&tp->t_outq, buf, (head - next) & TX_MASK);
1251 #ifdef DCMSTATS
1252 tch += nch;
1253 #endif
1254 #ifdef DEBUG
1255 if (dcmdebug & DDB_OUTPUT)
1256 printf("\thead %x tail %x nch %d\n", head, tail, nch);
1257 #endif
1258 /*
1259 * Loop transmitting all the characters we can.
1260 */
1261 for (bp = buf; --nch >= 0; bp++) {
1262 fifo->data_char = *bp;
1263 pp->t_tail = next;
1264 /*
1265 * If this is the first character,
1266 * get the hardware moving right now.
1267 */
1268 if (bp == buf) {
1269 tp->t_state |= TS_BUSY;
1270 SEM_LOCK(dcm);
1271 dcm->dcm_cmdtab[port].dcm_data |= CT_TX;
1272 dcm->dcm_cr |= (1 << port);
1273 SEM_UNLOCK(dcm);
1274 }
1275 tail = next;
1276 fifo = tail ? fifo+1 : &dcm->dcm_tfifos[3-port][0];
1277 next = (next + 1) & TX_MASK;
1278 }
1279 /*
1280 * Head changed while we were loading the buffer,
1281 * go back and load some more if we can.
1282 */
1283 if (tp->t_outq.c_cc && head != (pp->t_head & TX_MASK)) {
1284 #ifdef DCMSTATS
1285 dsp->xrestarts++;
1286 #endif
1287 head = pp->t_head & TX_MASK;
1288 goto again;
1289 }
1290
1291 /*
1292 * Kick it one last time in case it finished while we were
1293 * loading the last bunch.
1294 */
1295 if (bp > &buf[1]) {
1296 tp->t_state |= TS_BUSY;
1297 SEM_LOCK(dcm);
1298 dcm->dcm_cmdtab[port].dcm_data |= CT_TX;
1299 dcm->dcm_cr |= (1 << port);
1300 SEM_UNLOCK(dcm);
1301 }
1302 #ifdef DEBUG
1303 if (dcmdebug & DDB_INTR)
1304 printf("%s port %d: dcmstart: head %x tail %x outqcc %d\n",
1305 sc->sc_dev.dv_xname, port, head, tail, tp->t_outq.c_cc);
1306 #endif
1307 out:
1308 #ifdef DCMSTATS
1309 dsp->xchars += tch;
1310 if (tch <= DCMXBSIZE)
1311 dsp->xsilo[tch]++;
1312 else
1313 dsp->xsilo[DCMXBSIZE+1]++;
1314 #endif
1315 splx(s);
1316 }
1317
1318 /*
1319 * Stop output on a line.
1320 */
1321 static void
1322 dcmstop(struct tty *tp, int flag)
1323 {
1324 int s;
1325
1326 s = spltty();
1327 if (tp->t_state & TS_BUSY) {
1328 /* XXX is there some way to safely stop transmission? */
1329 if ((tp->t_state&TS_TTSTOP) == 0)
1330 tp->t_state |= TS_FLUSH;
1331 }
1332 splx(s);
1333 }
1334
1335 /*
1336 * Modem control
1337 */
1338 int
1339 dcmmctl(dev_t dev, int bits, int how)
1340 {
1341 struct dcm_softc *sc;
1342 struct dcmdevice *dcm;
1343 int s, unit, brd, port, hit = 0;
1344
1345 unit = DCMUNIT(dev);
1346 brd = DCMBOARD(unit);
1347 port = DCMPORT(unit);
1348
1349 sc = dcm_cd.cd_devs[brd];
1350 dcm = sc->sc_dcm;
1351
1352 #ifdef DEBUG
1353 if (dcmdebug & DDB_MODEM)
1354 printf("%s port %d: dcmmctl: bits 0x%x how %x\n",
1355 sc->sc_dev.dv_xname, port, bits, how);
1356 #endif
1357
1358 s = spltty();
1359
1360 switch (how) {
1361 case DMSET:
1362 sc->sc_modem[port]->mdmout = bits;
1363 hit++;
1364 break;
1365
1366 case DMBIS:
1367 sc->sc_modem[port]->mdmout |= bits;
1368 hit++;
1369 break;
1370
1371 case DMBIC:
1372 sc->sc_modem[port]->mdmout &= ~bits;
1373 hit++;
1374 break;
1375
1376 case DMGET:
1377 bits = sc->sc_modem[port]->mdmin;
1378 if (sc->sc_flags & DCM_STDDCE)
1379 bits = hp2dce_in(bits);
1380 break;
1381 }
1382 if (hit) {
1383 SEM_LOCK(dcm);
1384 dcm->dcm_modemchng |= 1<<(unit & 3);
1385 dcm->dcm_cr |= CR_MODM;
1386 SEM_UNLOCK(dcm);
1387 DELAY(10); /* delay until done */
1388 splx(s);
1389 }
1390 return bits;
1391 }
1392
1393 /*
1394 * Set board to either interrupt per-character or at a fixed interval.
1395 */
1396 static void
1397 dcmsetischeme(int brd, int flags)
1398 {
1399 struct dcm_softc *sc = dcm_cd.cd_devs[brd];
1400 struct dcmdevice *dcm = sc->sc_dcm;
1401 struct dcmischeme *dis = &sc->sc_scheme;
1402 int i;
1403 u_char mask;
1404 int perchar = flags & DIS_PERCHAR;
1405
1406 #ifdef DEBUG
1407 if (dcmdebug & DDB_INTSCHM)
1408 printf("%s: dcmsetischeme(%d): cur %d, ints %d, chars %d\n",
1409 sc->sc_dev.dv_xname, perchar, dis->dis_perchar,
1410 dis->dis_intr, dis->dis_char);
1411 if ((flags & DIS_RESET) == 0 && perchar == dis->dis_perchar) {
1412 printf("%s: dcmsetischeme: redundent request %d\n",
1413 sc->sc_dev.dv_xname, perchar);
1414 return;
1415 }
1416 #endif
1417 /*
1418 * If perchar is non-zero, we enable interrupts on all characters
1419 * otherwise we disable perchar interrupts and use periodic
1420 * polling interrupts.
1421 */
1422 dis->dis_perchar = perchar;
1423 mask = perchar ? 0xf : 0x0;
1424 for (i = 0; i < 256; i++)
1425 dcm->dcm_bmap[i].data_data = mask;
1426 /*
1427 * Don't slow down tandem mode, interrupt on flow control
1428 * chars for any port on the board.
1429 */
1430 if (!perchar) {
1431 struct tty *tp;
1432 int c;
1433
1434 for (i = 0; i < NDCMPORT; i++) {
1435 tp = sc->sc_tty[i];
1436
1437 if ((c = tp->t_cc[VSTART]) != _POSIX_VDISABLE)
1438 dcm->dcm_bmap[c].data_data |= (1 << i);
1439 if ((c = tp->t_cc[VSTOP]) != _POSIX_VDISABLE)
1440 dcm->dcm_bmap[c].data_data |= (1 << i);
1441 }
1442 }
1443 /*
1444 * Board starts with timer disabled so if first call is to
1445 * set perchar mode then we don't want to toggle the timer.
1446 */
1447 if (flags == (DIS_RESET|DIS_PERCHAR))
1448 return;
1449 /*
1450 * Toggle card 16.7ms interrupts (we first make sure that card
1451 * has cleared the bit so it will see the toggle).
1452 */
1453 while (dcm->dcm_cr & CR_TIMER)
1454 ;
1455 SEM_LOCK(dcm);
1456 dcm->dcm_cr |= CR_TIMER;
1457 SEM_UNLOCK(dcm);
1458 }
1459
1460 static void
1461 dcminit(struct dcmdevice *dcm, int port, int rate)
1462 {
1463 int s, mode;
1464
1465 mode = LC_8BITS | LC_1STOP;
1466
1467 s = splhigh();
1468
1469 /*
1470 * Wait for transmitter buffer to empty.
1471 */
1472 while (dcm->dcm_thead[port].ptr != dcm->dcm_ttail[port].ptr)
1473 DELAY(DCM_USPERCH(rate));
1474
1475 /*
1476 * Make changes known to hardware.
1477 */
1478 dcm->dcm_data[port].dcm_baud = ttspeedtab(rate, dcmspeedtab);
1479 dcm->dcm_data[port].dcm_conf = mode;
1480 SEM_LOCK(dcm);
1481 dcm->dcm_cmdtab[port].dcm_data |= CT_CON;
1482 dcm->dcm_cr |= (1 << port);
1483 SEM_UNLOCK(dcm);
1484
1485 /*
1486 * Delay for config change to take place. Weighted by baud.
1487 * XXX why do we do this?
1488 */
1489 DELAY(16 * DCM_USPERCH(rate));
1490 splx(s);
1491 }
1492
1493 /*
1494 * Empirically derived self-test magic
1495 */
1496 static int
1497 dcmselftest(struct dcm_softc *sc)
1498 {
1499 struct dcmdevice *dcm = sc->sc_dcm;
1500 int timo = 0;
1501 int s, rv;
1502
1503 rv = 1;
1504
1505 s = splhigh();
1506 dcm->dcm_rsid = DCMRS;
1507 DELAY(50000); /* 5000 is not long enough */
1508 dcm->dcm_rsid = 0;
1509 dcm->dcm_ic = IC_IE;
1510 dcm->dcm_cr = CR_SELFT;
1511 while ((dcm->dcm_ic & IC_IR) == 0) {
1512 if (++timo == 20000)
1513 goto out;
1514 DELAY(1);
1515 }
1516 DELAY(50000); /* XXX why is this needed ???? */
1517 while ((dcm->dcm_iir & IIR_SELFT) == 0) {
1518 if (++timo == 400000)
1519 goto out;
1520 DELAY(1);
1521 }
1522 DELAY(50000); /* XXX why is this needed ???? */
1523 if (dcm->dcm_stcon != ST_OK) {
1524 #if 0
1525 if (hd->hp_args->hw_sc != conscode)
1526 printf("dcm%d: self test failed: %x\n",
1527 brd, dcm->dcm_stcon);
1528 #endif
1529 goto out;
1530 }
1531 dcm->dcm_ic = IC_ID;
1532 rv = 0;
1533
1534 out:
1535 splx(s);
1536 return rv;
1537 }
1538
1539 /*
1540 * Following are all routines needed for DCM to act as console
1541 */
1542
1543 int
1544 dcmcnattach(bus_space_tag_t bst, bus_addr_t addr, int scode)
1545 {
1546 bus_space_handle_t bsh;
1547 caddr_t va;
1548 struct dcmdevice *dcm;
1549 int maj;
1550
1551 if (bus_space_map(bst, addr, DIOCSIZE, 0, &bsh))
1552 return 1;
1553
1554 va = bus_space_vaddr(bst, bsh);
1555 dcm = (struct dcmdevice *)va;
1556
1557 switch (dcm->dcm_rsid) {
1558 #ifdef CONSCODE
1559 case DCMID:
1560 #endif
1561 case DCMID|DCMCON:
1562 break;
1563 default:
1564 goto error;
1565 }
1566
1567 dcminit(dcm, DCMCONSPORT, dcmdefaultrate);
1568 dcmconsinit = 1;
1569 dcmconscode = scode;
1570 dcm_cn = dcm;
1571
1572 /* locate the major number */
1573 maj = cdevsw_lookup_major(&dcm_cdevsw);
1574
1575 /* initialize required fields */
1576 cn_tab = &dcm_cons;
1577 cn_tab->cn_dev = makedev(maj, 0);
1578
1579 #ifdef KGDB_CHEAT
1580 /* XXX this needs to be fixed. */
1581 /*
1582 * This doesn't currently work, at least not with ite consoles;
1583 * the console hasn't been initialized yet.
1584 */
1585 if (major(kgdb_dev) == maj &&
1586 DCMBOARD(DCMUNIT(kgdb_dev)) == DCMBOARD(unit)) {
1587 dcminit(dcm_cn, DCMPORT(DCMUNIT(kgdb_dev)), kgdb_rate);
1588 if (kgdb_debug_init) {
1589 /*
1590 * We assume that console is ready for us...
1591 * this assumes that a dca or ite console
1592 * has been selected already and will init
1593 * on the first putc.
1594 */
1595 printf("dcm%d: ", DCMUNIT(kgdb_dev));
1596 kgdb_connect(1);
1597 }
1598 }
1599 #endif
1600
1601
1602 return 0;
1603
1604 error:
1605 bus_space_unmap(bst, bsh, DIOCSIZE);
1606 return 1;
1607 }
1608
1609 /* ARGSUSED */
1610 static int
1611 dcmcngetc(dev_t dev)
1612 {
1613 struct dcmrfifo *fifo;
1614 struct dcmpreg *pp;
1615 u_int head;
1616 int s, c, stat;
1617
1618 pp = dcm_preg(dcm_cn, DCMCONSPORT);
1619
1620 s = splhigh();
1621 head = pp->r_head & RX_MASK;
1622 fifo = &dcm_cn->dcm_rfifos[3-DCMCONSPORT][head>>1];
1623 while (head == (pp->r_tail & RX_MASK))
1624 ;
1625 /*
1626 * If board interrupts are enabled, just let our received char
1627 * interrupt through in case some other port on the board was
1628 * busy. Otherwise we must clear the interrupt.
1629 */
1630 SEM_LOCK(dcm_cn);
1631 if ((dcm_cn->dcm_ic & IC_IE) == 0)
1632 stat = dcm_cn->dcm_iir;
1633 SEM_UNLOCK(dcm_cn);
1634 c = fifo->data_char;
1635 stat = fifo->data_stat;
1636 pp->r_head = (head + 2) & RX_MASK;
1637 splx(s);
1638 return c;
1639 }
1640
1641 /*
1642 * Console kernel output character routine.
1643 */
1644 /* ARGSUSED */
1645 static void
1646 dcmcnputc(dev_t dev, int c)
1647 {
1648 struct dcmpreg *pp;
1649 unsigned tail;
1650 int s, stat;
1651
1652 pp = dcm_preg(dcm_cn, DCMCONSPORT);
1653
1654 s = splhigh();
1655 #ifdef KGDB
1656 if (dev != kgdb_dev)
1657 #endif
1658 if (dcmconsinit == 0) {
1659 dcminit(dcm_cn, DCMCONSPORT, dcmdefaultrate);
1660 dcmconsinit = 1;
1661 }
1662 tail = pp->t_tail & TX_MASK;
1663 while (tail != (pp->t_head & TX_MASK))
1664 ;
1665 dcm_cn->dcm_tfifos[3-DCMCONSPORT][tail].data_char = c;
1666 pp->t_tail = tail = (tail + 1) & TX_MASK;
1667 SEM_LOCK(dcm_cn);
1668 dcm_cn->dcm_cmdtab[DCMCONSPORT].dcm_data |= CT_TX;
1669 dcm_cn->dcm_cr |= (1 << DCMCONSPORT);
1670 SEM_UNLOCK(dcm_cn);
1671 while (tail != (pp->t_head & TX_MASK))
1672 ;
1673 /*
1674 * If board interrupts are enabled, just let our completion
1675 * interrupt through in case some other port on the board
1676 * was busy. Otherwise we must clear the interrupt.
1677 */
1678 if ((dcm_cn->dcm_ic & IC_IE) == 0) {
1679 SEM_LOCK(dcm_cn);
1680 stat = dcm_cn->dcm_iir;
1681 SEM_UNLOCK(dcm_cn);
1682 }
1683 splx(s);
1684 }
1685