FreeBSD/Linux Kernel Cross Reference
sys/dev/cx/if_cx.c
1 /*-
2 * Cronyx-Sigma adapter driver for FreeBSD.
3 * Supports PPP/HDLC and Cisco/HDLC protocol in synchronous mode,
4 * and asynchronous channels with full modem control.
5 * Keepalive protocol implemented in both Cisco and PPP modes.
6 *
7 * Copyright (C) 1994-2002 Cronyx Engineering.
8 * Author: Serge Vakulenko, <vak@cronyx.ru>
9 *
10 * Copyright (C) 1999-2004 Cronyx Engineering.
11 * Rewritten on DDK, ported to NETGRAPH, rewritten for FreeBSD 3.x-5.x by
12 * Kurakin Roman, <rik@cronyx.ru>
13 *
14 * This software is distributed with NO WARRANTIES, not even the implied
15 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * Authors grant any other persons or organisations a permission to use,
18 * modify and redistribute this software in source and binary forms,
19 * as long as this message is kept with the software, all derivative
20 * works or modified versions.
21 *
22 * Cronyx Id: if_cx.c,v 1.1.2.34 2004/06/23 17:09:13 rik Exp $
23 */
24
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
27
28 #include <sys/param.h>
29
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/priv.h>
34 #include <sys/proc.h>
35 #include <sys/mbuf.h>
36 #include <sys/sockio.h>
37 #include <sys/malloc.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40 #include <sys/conf.h>
41 #include <sys/errno.h>
42 #include <sys/serial.h>
43 #include <sys/tty.h>
44 #include <sys/bus.h>
45 #include <machine/bus.h>
46 #include <sys/rman.h>
47 #include <isa/isavar.h>
48 #include <sys/fcntl.h>
49 #include <sys/interrupt.h>
50 #include <vm/vm.h>
51 #include <vm/pmap.h>
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <machine/cpufunc.h>
55 #include <machine/cserial.h>
56 #include <machine/resource.h>
57 #include <dev/cx/machdep.h>
58 #include <dev/cx/cxddk.h>
59 #include <dev/cx/cronyxfw.h>
60 #include "opt_ng_cronyx.h"
61 #ifdef NETGRAPH_CRONYX
62 # include "opt_netgraph.h"
63 # include <netgraph/ng_message.h>
64 # include <netgraph/netgraph.h>
65 # include <dev/cx/ng_cx.h>
66 #else
67 # include <net/if_types.h>
68 # include <net/if_sppp.h>
69 # define PP_CISCO IFF_LINK2
70 # include <net/bpf.h>
71 #endif
72
73 #define NCX 1
74
75 /* If we don't have Cronyx's sppp version, we don't have fr support via sppp */
76 #ifndef PP_FR
77 #define PP_FR 0
78 #endif
79
80 #define CX_DEBUG(d,s) ({if (d->chan->debug) {\
81 printf ("%s: ", d->name); printf s;}})
82 #define CX_DEBUG2(d,s) ({if (d->chan->debug>1) {\
83 printf ("%s: ", d->name); printf s;}})
84
85 #define CX_LOCK_NAME "cxX"
86
87 #define CX_LOCK(_bd) mtx_lock (&(_bd)->cx_mtx)
88 #define CX_UNLOCK(_bd) mtx_unlock (&(_bd)->cx_mtx)
89 #define CX_LOCK_ASSERT(_bd) mtx_assert (&(_bd)->cx_mtx, MA_OWNED)
90
91 typedef struct _async_q {
92 int beg;
93 int end;
94 #define BF_SZ 14400
95 int buf[BF_SZ+1];
96 } async_q;
97
98 #define AQ_GSZ(q) ((BF_SZ + (q)->end - (q)->beg)%BF_SZ)
99 #define AQ_PUSH(q,c) {*((q)->buf + (q)->end) = c;\
100 (q)->end = ((q)->end + 1)%BF_SZ;}
101 #define AQ_POP(q,c) {c = *((q)->buf + (q)->beg);\
102 (q)->beg = ((q)->beg + 1)%BF_SZ;}
103
104 static void cx_identify __P((driver_t *, device_t));
105 static int cx_probe __P((device_t));
106 static int cx_attach __P((device_t));
107 static int cx_detach __P((device_t));
108 static t_open_t cx_topen;
109 static t_modem_t cx_tmodem;
110 static t_close_t cx_tclose;
111
112 static device_method_t cx_isa_methods [] = {
113 DEVMETHOD(device_identify, cx_identify),
114 DEVMETHOD(device_probe, cx_probe),
115 DEVMETHOD(device_attach, cx_attach),
116 DEVMETHOD(device_detach, cx_detach),
117
118 DEVMETHOD_END
119 };
120
121 typedef struct _cx_dma_mem_t {
122 unsigned long phys;
123 void *virt;
124 size_t size;
125 bus_dma_tag_t dmat;
126 bus_dmamap_t mapp;
127 } cx_dma_mem_t;
128
129 typedef struct _drv_t {
130 char name [8];
131 cx_chan_t *chan;
132 cx_board_t *board;
133 cx_dma_mem_t dmamem;
134 struct tty *tty;
135 struct callout dcd_timeout_handle;
136 unsigned callout;
137 unsigned lock;
138 int open_dev;
139 int cd;
140 int running;
141 #ifdef NETGRAPH
142 char nodename [NG_NODESIZ];
143 hook_p hook;
144 hook_p debug_hook;
145 node_p node;
146 struct ifqueue lo_queue;
147 struct ifqueue hi_queue;
148 #else
149 struct ifqueue queue;
150 struct ifnet *ifp;
151 #endif
152 short timeout;
153 struct callout timeout_handle;
154 struct cdev *devt;
155 async_q aqueue;
156 #define CX_READ 1
157 #define CX_WRITE 2
158 int intr_action;
159 short atimeout;
160 } drv_t;
161
162 typedef struct _bdrv_t {
163 cx_board_t *board;
164 struct resource *base_res;
165 struct resource *drq_res;
166 struct resource *irq_res;
167 int base_rid;
168 int drq_rid;
169 int irq_rid;
170 void *intrhand;
171 drv_t channel [NCHAN];
172 struct mtx cx_mtx;
173 } bdrv_t;
174
175 static driver_t cx_isa_driver = {
176 "cx",
177 cx_isa_methods,
178 sizeof (bdrv_t),
179 };
180
181 static devclass_t cx_devclass;
182
183 extern long csigma_fw_len;
184 extern const char *csigma_fw_version;
185 extern const char *csigma_fw_date;
186 extern const char *csigma_fw_copyright;
187 extern const cr_dat_tst_t csigma_fw_tvec[];
188 extern const u_char csigma_fw_data[];
189 static void cx_oproc (struct tty *tp);
190 static int cx_param (struct tty *tp, struct termios *t);
191 static void cx_stop (struct tty *tp, int flag);
192 static void cx_receive (cx_chan_t *c, char *data, int len);
193 static void cx_transmit (cx_chan_t *c, void *attachment, int len);
194 static void cx_error (cx_chan_t *c, int data);
195 static void cx_modem (cx_chan_t *c);
196 static void cx_up (drv_t *d);
197 static void cx_start (drv_t *d);
198 static void cx_softintr (void *);
199 static void *cx_fast_ih;
200 static void cx_down (drv_t *d);
201 static void cx_watchdog (drv_t *d);
202 static void cx_watchdog_timer (void *arg);
203 static void cx_carrier (void *arg);
204
205 #ifdef NETGRAPH
206 extern struct ng_type typestruct;
207 #else
208 static void cx_ifstart (struct ifnet *ifp);
209 static void cx_tlf (struct sppp *sp);
210 static void cx_tls (struct sppp *sp);
211 static int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data);
212 static void cx_initialize (void *softc);
213 #endif
214
215 static cx_board_t *adapter [NCX];
216 static drv_t *channel [NCX*NCHAN];
217 static struct callout led_timo [NCX];
218 static struct callout timeout_handle;
219
220 static int cx_open (struct cdev *dev, int flag, int mode, struct thread *td);
221 static int cx_close (struct cdev *dev, int flag, int mode, struct thread *td);
222 static int cx_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td);
223 static struct cdevsw cx_cdevsw = {
224 .d_version = D_VERSION,
225 .d_open = cx_open,
226 .d_close = cx_close,
227 .d_ioctl = cx_ioctl,
228 .d_name = "cx",
229 .d_flags = D_TTY,
230 };
231
232 static int MY_SOFT_INTR;
233
234 /*
235 * Make an mbuf from data.
236 */
237 static struct mbuf *makembuf (void *buf, u_int len)
238 {
239 struct mbuf *m, *o, *p;
240
241 MGETHDR (m, M_NOWAIT, MT_DATA);
242
243 if (! m)
244 return 0;
245
246 if (len >= MINCLSIZE)
247 MCLGET (m, M_NOWAIT);
248
249 m->m_pkthdr.len = len;
250 m->m_len = 0;
251
252 p = m;
253 while (len) {
254 u_int n = M_TRAILINGSPACE (p);
255 if (n > len)
256 n = len;
257 if (! n) {
258 /* Allocate new mbuf. */
259 o = p;
260 MGET (p, M_NOWAIT, MT_DATA);
261 if (! p) {
262 m_freem (m);
263 return 0;
264 }
265 if (len >= MINCLSIZE)
266 MCLGET (p, M_NOWAIT);
267 p->m_len = 0;
268 o->m_next = p;
269
270 n = M_TRAILINGSPACE (p);
271 if (n > len)
272 n = len;
273 }
274 bcopy (buf, mtod (p, caddr_t) + p->m_len, n);
275
276 p->m_len += n;
277 buf = n + (char*) buf;
278 len -= n;
279 }
280 return m;
281 }
282
283 /*
284 * Recover after lost transmit interrupts.
285 */
286 static void cx_timeout (void *arg)
287 {
288 drv_t *d;
289 int s, i, k;
290
291 for (i = 0; i < NCX; i++) {
292 if (adapter[i] == NULL)
293 continue;
294 for (k = 0; k < NCHAN; ++k) {
295 d = channel[i * NCHAN + k];
296 if (! d)
297 continue;
298 s = splhigh ();
299 CX_LOCK ((bdrv_t *)d->board->sys);
300 if (d->atimeout == 1 && d->tty && d->tty->t_state & TS_BUSY) {
301 d->tty->t_state &= ~TS_BUSY;
302 if (d->tty->t_dev) {
303 d->intr_action |= CX_WRITE;
304 MY_SOFT_INTR = 1;
305 swi_sched (cx_fast_ih, 0);
306 }
307 CX_DEBUG (d, ("cx_timeout\n"));
308 }
309 if (d->atimeout)
310 d->atimeout--;
311 CX_UNLOCK ((bdrv_t *)d->board->sys);
312 splx (s);
313 }
314 }
315 callout_reset (&timeout_handle, hz*5, cx_timeout, 0);
316 }
317
318 static void cx_led_off (void *arg)
319 {
320 cx_board_t *b = arg;
321 bdrv_t *bd = b->sys;
322 int s;
323
324 s = splhigh ();
325 CX_LOCK (bd);
326 cx_led (b, 0);
327 CX_UNLOCK (bd);
328 splx (s);
329 }
330
331 /*
332 * Activate interrupt handler from DDK.
333 */
334 static void cx_intr (void *arg)
335 {
336 bdrv_t *bd = arg;
337 cx_board_t *b = bd->board;
338 #ifndef NETGRAPH
339 int i;
340 #endif
341 int s = splhigh ();
342
343 CX_LOCK (bd);
344 /* Turn LED on. */
345 cx_led (b, 1);
346
347 cx_int_handler (b);
348
349 /* Turn LED off 50 msec later. */
350 callout_reset (&led_timo[b->num], hz/20, cx_led_off, b);
351 CX_UNLOCK (bd);
352 splx (s);
353
354 #ifndef NETGRAPH
355 /* Pass packets in a lock-free state */
356 for (i = 0; i < NCHAN && b->chan[i].type; i++) {
357 drv_t *d = b->chan[i].sys;
358 struct mbuf *m;
359 if (!d || !d->running)
360 continue;
361 while (_IF_QLEN(&d->queue)) {
362 IF_DEQUEUE (&d->queue,m);
363 if (!m)
364 continue;
365 sppp_input (d->ifp, m);
366 }
367 }
368 #endif
369 }
370
371 static int probe_irq (cx_board_t *b, int irq)
372 {
373 int mask, busy, cnt;
374
375 /* Clear pending irq, if any. */
376 cx_probe_irq (b, -irq);
377 DELAY (100);
378 for (cnt=0; cnt<5; ++cnt) {
379 /* Get the mask of pending irqs, assuming they are busy.
380 * Activate the adapter on given irq. */
381 busy = cx_probe_irq (b, irq);
382 DELAY (100);
383
384 /* Get the mask of active irqs.
385 * Deactivate our irq. */
386 mask = cx_probe_irq (b, -irq);
387 DELAY (100);
388 if ((mask & ~busy) == 1 << irq) {
389 cx_probe_irq (b, 0);
390 /* printf ("cx%d: irq %d ok, mask=0x%04x, busy=0x%04x\n",
391 b->num, irq, mask, busy); */
392 return 1;
393 }
394 }
395 /* printf ("cx%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n",
396 b->num, irq, mask, busy); */
397 cx_probe_irq (b, 0);
398 return 0;
399 }
400
401 static short porttab [] = {
402 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
403 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
404 };
405 static char dmatab [] = { 7, 6, 5, 0 };
406 static char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 };
407
408 static int cx_is_free_res (device_t dev, int rid, int type, rman_res_t start,
409 rman_res_t end, rman_res_t count)
410 {
411 struct resource *res;
412
413 if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count, 0)))
414 return 0;
415
416 bus_release_resource (dev, type, rid, res);
417
418 return 1;
419 }
420
421 static void cx_identify (driver_t *driver, device_t dev)
422 {
423 rman_res_t iobase, rescount;
424 int devcount;
425 device_t *devices;
426 device_t child;
427 devclass_t my_devclass;
428 int i, k;
429
430 if ((my_devclass = devclass_find ("cx")) == NULL)
431 return;
432
433 devclass_get_devices (my_devclass, &devices, &devcount);
434
435 if (devcount == 0) {
436 /* We should find all devices by our self. We could alter other
437 * devices, but we don't have a choise
438 */
439 for (i = 0; (iobase = porttab [i]) != 0; i++) {
440 if (!cx_is_free_res (dev, 0, SYS_RES_IOPORT,
441 iobase, iobase + NPORT, NPORT))
442 continue;
443 if (cx_probe_board (iobase, -1, -1) == 0)
444 continue;
445
446 devcount++;
447
448 child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "cx",
449 -1);
450
451 if (child == NULL)
452 return;
453
454 device_set_desc_copy (child, "Cronyx Sigma");
455 device_set_driver (child, driver);
456 bus_set_resource (child, SYS_RES_IOPORT, 0,
457 iobase, NPORT);
458
459 if (devcount >= NCX)
460 break;
461 }
462 } else {
463 static short porttab [] = {
464 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0,
465 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0
466 };
467 /* Lets check user choise.
468 */
469 for (k = 0; k < devcount; k++) {
470 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
471 &iobase, &rescount) != 0)
472 continue;
473
474 for (i = 0; porttab [i] != 0; i++) {
475 if (porttab [i] != iobase)
476 continue;
477 if (!cx_is_free_res (devices[k], 0, SYS_RES_IOPORT,
478 iobase, iobase + NPORT, NPORT))
479 continue;
480 if (cx_probe_board (iobase, -1, -1) == 0)
481 continue;
482 porttab [i] = -1;
483 device_set_desc_copy (devices[k], "Cronyx Sigma");
484 break;
485 }
486
487 if (porttab [i] == 0) {
488 device_delete_child (
489 device_get_parent (devices[k]),
490 devices [k]);
491 devices[k] = 0;
492 continue;
493 }
494 }
495 for (k = 0; k < devcount; k++) {
496 if (devices[k] == 0)
497 continue;
498 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0,
499 &iobase, &rescount) == 0)
500 continue;
501 for (i = 0; (iobase = porttab [i]) != 0; i++) {
502 if (porttab [i] == -1) {
503 continue;
504 }
505 if (!cx_is_free_res (devices[k], 0, SYS_RES_IOPORT,
506 iobase, iobase + NPORT, NPORT))
507 continue;
508 if (cx_probe_board (iobase, -1, -1) == 0)
509 continue;
510
511 bus_set_resource (devices[k], SYS_RES_IOPORT, 0,
512 iobase, NPORT);
513 porttab [i] = -1;
514 device_set_desc_copy (devices[k], "Cronyx Sigma");
515 break;
516 }
517 if (porttab [i] == 0) {
518 device_delete_child (
519 device_get_parent (devices[k]),
520 devices [k]);
521 }
522 }
523 free (devices, M_TEMP);
524 }
525
526 return;
527 }
528
529 static int cx_probe (device_t dev)
530 {
531 int unit = device_get_unit (dev);
532 int i;
533 rman_res_t iobase, rescount;
534
535 if (!device_get_desc (dev) ||
536 strcmp (device_get_desc (dev), "Cronyx Sigma"))
537 return ENXIO;
538
539 if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) {
540 printf ("cx%d: Couldn't get IOPORT\n", unit);
541 return ENXIO;
542 }
543
544 if (!cx_is_free_res (dev, 0, SYS_RES_IOPORT,
545 iobase, iobase + NPORT, NPORT)) {
546 printf ("cx%d: Resource IOPORT isn't free %lx\n", unit, iobase);
547 return ENXIO;
548 }
549
550 for (i = 0; porttab [i] != 0; i++) {
551 if (porttab [i] == iobase) {
552 porttab [i] = -1;
553 break;
554 }
555 }
556
557 if (porttab [i] == 0) {
558 return ENXIO;
559 }
560
561 if (!cx_probe_board (iobase, -1, -1)) {
562 printf ("cx%d: probing for Sigma at %lx faild\n", unit, iobase);
563 return ENXIO;
564 }
565
566 return 0;
567 }
568
569 static void
570 cx_bus_dmamap_addr (void *arg, bus_dma_segment_t *segs, int nseg, int error)
571 {
572 unsigned long *addr;
573
574 if (error)
575 return;
576
577 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
578 addr = arg;
579 *addr = segs->ds_addr;
580 }
581
582 static int
583 cx_bus_dma_mem_alloc (int bnum, int cnum, cx_dma_mem_t *dmem)
584 {
585 int error;
586
587 error = bus_dma_tag_create (NULL, 16, 0, BUS_SPACE_MAXADDR_24BIT,
588 BUS_SPACE_MAXADDR, NULL, NULL, dmem->size, 1,
589 dmem->size, 0, NULL, NULL, &dmem->dmat);
590 if (error) {
591 if (cnum >= 0) printf ("cx%d-%d: ", bnum, cnum);
592 else printf ("cx%d: ", bnum);
593 printf ("couldn't allocate tag for dma memory\n");
594 return 0;
595 }
596 error = bus_dmamem_alloc (dmem->dmat, (void **)&dmem->virt,
597 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &dmem->mapp);
598 if (error) {
599 if (cnum >= 0) printf ("cx%d-%d: ", bnum, cnum);
600 else printf ("cx%d: ", bnum);
601 printf ("couldn't allocate mem for dma memory\n");
602 bus_dma_tag_destroy (dmem->dmat);
603 return 0;
604 }
605 error = bus_dmamap_load (dmem->dmat, dmem->mapp, dmem->virt,
606 dmem->size, cx_bus_dmamap_addr, &dmem->phys, 0);
607 if (error) {
608 if (cnum >= 0) printf ("cx%d-%d: ", bnum, cnum);
609 else printf ("cx%d: ", bnum);
610 printf ("couldn't load mem map for dma memory\n");
611 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
612 bus_dma_tag_destroy (dmem->dmat);
613 return 0;
614 }
615 return 1;
616 }
617
618 static void
619 cx_bus_dma_mem_free (cx_dma_mem_t *dmem)
620 {
621 bus_dmamap_unload (dmem->dmat, dmem->mapp);
622 bus_dmamem_free (dmem->dmat, dmem->virt, dmem->mapp);
623 bus_dma_tag_destroy (dmem->dmat);
624 }
625
626 /*
627 * The adapter is present, initialize the driver structures.
628 */
629 static int cx_attach (device_t dev)
630 {
631 bdrv_t *bd = device_get_softc (dev);
632 rman_res_t iobase, drq, irq, rescount;
633 int unit = device_get_unit (dev);
634 char *cx_ln = CX_LOCK_NAME;
635 cx_board_t *b;
636 cx_chan_t *c;
637 drv_t *d;
638 int i;
639 int s;
640
641 KASSERT ((bd != NULL), ("cx%d: NULL device softc\n", unit));
642
643 bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount);
644 bd->base_rid = 0;
645 bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid,
646 iobase, iobase + NPORT, NPORT, RF_ACTIVE);
647 if (! bd->base_res) {
648 printf ("cx%d: cannot allocate base address\n", unit);
649 return ENXIO;
650 }
651
652 if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) {
653 for (i = 0; (drq = dmatab [i]) != 0; i++) {
654 if (!cx_is_free_res (dev, 0, SYS_RES_DRQ,
655 drq, drq + 1, 1))
656 continue;
657 bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1);
658 break;
659 }
660
661 if (dmatab[i] == 0) {
662 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
663 bd->base_res);
664 printf ("cx%d: Couldn't get DRQ\n", unit);
665 return ENXIO;
666 }
667 }
668
669 bd->drq_rid = 0;
670 bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid,
671 drq, drq + 1, 1, RF_ACTIVE);
672 if (! bd->drq_res) {
673 printf ("cx%d: cannot allocate drq\n", unit);
674 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
675 bd->base_res);
676 return ENXIO;
677 }
678
679 if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) {
680 for (i = 0; (irq = irqtab [i]) != 0; i++) {
681 if (!cx_is_free_res (dev, 0, SYS_RES_IRQ,
682 irq, irq + 1, 1))
683 continue;
684 bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1);
685 break;
686 }
687
688 if (irqtab[i] == 0) {
689 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
690 bd->drq_res);
691 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
692 bd->base_res);
693 printf ("cx%d: Couldn't get IRQ\n", unit);
694 return ENXIO;
695 }
696 }
697
698 bd->irq_rid = 0;
699 bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid,
700 irq, irq + 1, 1, RF_ACTIVE);
701 if (! bd->irq_res) {
702 printf ("cx%d: Couldn't allocate irq\n", unit);
703 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
704 bd->drq_res);
705 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
706 bd->base_res);
707 return ENXIO;
708 }
709
710 b = malloc (sizeof (cx_board_t), M_DEVBUF, M_WAITOK);
711 if (!b) {
712 printf ("cx:%d: Couldn't allocate memory\n", unit);
713 return (ENXIO);
714 }
715 adapter[unit] = b;
716 bzero (b, sizeof(cx_board_t));
717
718 if (! cx_open_board (b, unit, iobase, irq, drq)) {
719 printf ("cx%d: error loading firmware\n", unit);
720 free (b, M_DEVBUF);
721 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
722 bd->irq_res);
723 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
724 bd->drq_res);
725 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
726 bd->base_res);
727 return ENXIO;
728 }
729
730 bd->board = b;
731
732 cx_ln[2] = '' + unit;
733 mtx_init (&bd->cx_mtx, cx_ln, MTX_NETWORK_LOCK, MTX_DEF|MTX_RECURSE);
734 if (! probe_irq (b, irq)) {
735 printf ("cx%d: irq %ld not functional\n", unit, irq);
736 bd->board = 0;
737 adapter [unit] = 0;
738 mtx_destroy (&bd->cx_mtx);
739 free (b, M_DEVBUF);
740 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
741 bd->irq_res);
742 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
743 bd->drq_res);
744 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
745 bd->base_res);
746 return ENXIO;
747 }
748 b->sys = bd;
749 callout_init (&led_timo[b->num], 1);
750 s = splhigh ();
751 if (bus_setup_intr (dev, bd->irq_res,
752 INTR_TYPE_NET|INTR_MPSAFE,
753 NULL, cx_intr, bd, &bd->intrhand)) {
754 printf ("cx%d: Can't setup irq %ld\n", unit, irq);
755 bd->board = 0;
756 b->sys = 0;
757 adapter [unit] = 0;
758 mtx_destroy (&bd->cx_mtx);
759 free (b, M_DEVBUF);
760 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid,
761 bd->irq_res);
762 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid,
763 bd->drq_res);
764 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid,
765 bd->base_res);
766 splx (s);
767 return ENXIO;
768 }
769
770 CX_LOCK (bd);
771 cx_init (b, b->num, b->port, irq, drq);
772 cx_setup_board (b, 0, 0, 0);
773 CX_UNLOCK (bd);
774
775 printf ("cx%d: <Cronyx-Sigma-%s>\n", b->num, b->name);
776
777 for (c=b->chan; c<b->chan+NCHAN; ++c) {
778 if (c->type == T_NONE)
779 continue;
780 d = &bd->channel[c->num];
781 d->dmamem.size = sizeof(cx_buf_t);
782 if (! cx_bus_dma_mem_alloc (unit, c->num, &d->dmamem))
783 continue;
784 d->board = b;
785 d->chan = c;
786 d->open_dev = 0;
787 c->sys = d;
788 channel [b->num*NCHAN + c->num] = d;
789 sprintf (d->name, "cx%d.%d", b->num, c->num);
790
791 switch (c->type) {
792 case T_SYNC_RS232:
793 case T_SYNC_V35:
794 case T_SYNC_RS449:
795 case T_UNIV:
796 case T_UNIV_RS232:
797 case T_UNIV_RS449:
798 case T_UNIV_V35:
799 callout_init (&d->timeout_handle, 1);
800 #ifdef NETGRAPH
801 if (ng_make_node_common (&typestruct, &d->node) != 0) {
802 printf ("%s: cannot make common node\n", d->name);
803 channel [b->num*NCHAN + c->num] = 0;
804 c->sys = 0;
805 cx_bus_dma_mem_free (&d->dmamem);
806 continue;
807 }
808 NG_NODE_SET_PRIVATE (d->node, d);
809 sprintf (d->nodename, "%s%d", NG_CX_NODE_TYPE,
810 c->board->num*NCHAN + c->num);
811 if (ng_name_node (d->node, d->nodename)) {
812 printf ("%s: cannot name node\n", d->nodename);
813 NG_NODE_UNREF (d->node);
814 channel [b->num*NCHAN + c->num] = 0;
815 c->sys = 0;
816 cx_bus_dma_mem_free (&d->dmamem);
817 continue;
818 }
819 d->lo_queue.ifq_maxlen = ifqmaxlen;
820 d->hi_queue.ifq_maxlen = ifqmaxlen;
821 mtx_init (&d->lo_queue.ifq_mtx, "cx_queue_lo", NULL, MTX_DEF);
822 mtx_init (&d->hi_queue.ifq_mtx, "cx_queue_hi", NULL, MTX_DEF);
823 #else /*NETGRAPH*/
824 d->ifp = if_alloc(IFT_PPP);
825 if (d->ifp == NULL) {
826 printf ("%s: cannot if_alloc() common interface\n",
827 d->name);
828 channel [b->num*NCHAN + c->num] = 0;
829 c->sys = 0;
830 cx_bus_dma_mem_free (&d->dmamem);
831 continue;
832 }
833 d->ifp->if_softc = d;
834 if_initname (d->ifp, "cx", b->num * NCHAN + c->num);
835 d->ifp->if_mtu = PP_MTU;
836 d->ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
837 d->ifp->if_ioctl = cx_sioctl;
838 d->ifp->if_start = cx_ifstart;
839 d->ifp->if_init = cx_initialize;
840 d->queue.ifq_maxlen = 2;
841 mtx_init (&d->queue.ifq_mtx, "cx_queue", NULL, MTX_DEF);
842 sppp_attach (d->ifp);
843 if_attach (d->ifp);
844 IFP2SP(d->ifp)->pp_tlf = cx_tlf;
845 IFP2SP(d->ifp)->pp_tls = cx_tls;
846 /* If BPF is in the kernel, call the attach for it.
847 * Size of PPP header is 4 bytes. */
848 bpfattach (d->ifp, DLT_PPP, 4);
849 #endif /*NETGRAPH*/
850 }
851 d->tty = ttyalloc ();
852 d->tty->t_open = cx_topen;
853 d->tty->t_close = cx_tclose;
854 d->tty->t_param = cx_param;
855 d->tty->t_stop = cx_stop;
856 d->tty->t_modem = cx_tmodem;
857 d->tty->t_oproc = cx_oproc;
858 d->tty->t_sc = d;
859 CX_LOCK (bd);
860 cx_start_chan (c, d->dmamem.virt, d->dmamem.phys);
861 cx_register_receive (c, &cx_receive);
862 cx_register_transmit (c, &cx_transmit);
863 cx_register_error (c, &cx_error);
864 cx_register_modem (c, &cx_modem);
865 CX_UNLOCK (bd);
866
867 ttycreate(d->tty, TS_CALLOUT, "x%r%r", b->num, c->num);
868 d->devt = make_dev (&cx_cdevsw, b->num*NCHAN + c->num + 64, UID_ROOT, GID_WHEEL, 0600, "cx%d", b->num*NCHAN + c->num);
869 d->devt->si_drv1 = d;
870 callout_init (&d->dcd_timeout_handle, 1);
871 }
872 splx (s);
873
874 gone_in_dev(dev, 13, "sync serial (T1/E1) ISA card drivers");
875 return 0;
876 }
877
878 static int cx_detach (device_t dev)
879 {
880 bdrv_t *bd = device_get_softc (dev);
881 cx_board_t *b = bd->board;
882 cx_chan_t *c;
883 int s;
884
885 KASSERT (mtx_initialized (&bd->cx_mtx), ("cx mutex not initialized"));
886
887 s = splhigh ();
888 CX_LOCK (bd);
889 /* Check if the device is busy (open). */
890 for (c = b->chan; c < b->chan + NCHAN; ++c) {
891 drv_t *d = (drv_t*) c->sys;
892
893 if (!d || d->chan->type == T_NONE)
894 continue;
895 if (d->lock) {
896 CX_UNLOCK (bd);
897 splx (s);
898 return EBUSY;
899 }
900 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
901 (d->open_dev|0x2)) {
902 CX_UNLOCK (bd);
903 splx (s);
904 return EBUSY;
905 }
906 if (d->running) {
907 CX_UNLOCK (bd);
908 splx (s);
909 return EBUSY;
910 }
911 }
912
913 /* Deactivate the timeout routine. And soft interrupt*/
914 callout_stop (&led_timo[b->num]);
915
916 for (c = b->chan; c < b->chan + NCHAN; ++c) {
917 drv_t *d = c->sys;
918
919 if (!d || d->chan->type == T_NONE)
920 continue;
921
922 callout_stop (&d->dcd_timeout_handle);
923 }
924 CX_UNLOCK (bd);
925 bus_teardown_intr (dev, bd->irq_res, bd->intrhand);
926 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res);
927
928 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res);
929
930 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res);
931
932 CX_LOCK (bd);
933 cx_close_board (b);
934
935 /* Detach the interfaces, free buffer memory. */
936 for (c = b->chan; c < b->chan + NCHAN; ++c) {
937 drv_t *d = (drv_t*) c->sys;
938
939 if (!d || d->chan->type == T_NONE)
940 continue;
941
942 if (d->tty) {
943 ttyfree (d->tty);
944 d->tty = NULL;
945 }
946
947 callout_stop (&d->timeout_handle);
948 #ifdef NETGRAPH
949 if (d->node) {
950 ng_rmnode_self (d->node);
951 NG_NODE_UNREF (d->node);
952 d->node = NULL;
953 }
954 mtx_destroy (&d->lo_queue.ifq_mtx);
955 mtx_destroy (&d->hi_queue.ifq_mtx);
956 #else
957 /* Detach from the packet filter list of interfaces. */
958 bpfdetach (d->ifp);
959 /* Detach from the sync PPP list. */
960 sppp_detach (d->ifp);
961
962 if_detach (d->ifp);
963 if_free(d->ifp);
964 /* XXXRIK: check interconnection with irq handler */
965 IF_DRAIN (&d->queue);
966 mtx_destroy (&d->queue.ifq_mtx);
967 #endif
968 destroy_dev (d->devt);
969 }
970
971 cx_led_off (b);
972 CX_UNLOCK (bd);
973 callout_drain (&led_timo[b->num]);
974 for (c = b->chan; c < b->chan + NCHAN; ++c) {
975 drv_t *d = c->sys;
976
977 if (!d || d->chan->type == T_NONE)
978 continue;
979
980 callout_drain (&d->dcd_timeout_handle);
981 callout_drain (&d->timeout_handle);
982 }
983 splx (s);
984
985 s = splhigh ();
986 for (c = b->chan; c < b->chan + NCHAN; ++c) {
987 drv_t *d = (drv_t*) c->sys;
988
989 if (!d || d->chan->type == T_NONE)
990 continue;
991
992 /* Deallocate buffers. */
993 cx_bus_dma_mem_free (&d->dmamem);
994 }
995 bd->board = NULL;
996 adapter [b->num] = NULL;
997 free (b, M_DEVBUF);
998 splx (s);
999
1000 mtx_destroy (&bd->cx_mtx);
1001
1002 return 0;
1003 }
1004
1005 #ifndef NETGRAPH
1006 static void cx_ifstart (struct ifnet *ifp)
1007 {
1008 drv_t *d = ifp->if_softc;
1009 bdrv_t *bd = d->board->sys;
1010
1011 CX_LOCK (bd);
1012 cx_start (d);
1013 CX_UNLOCK (bd);
1014 }
1015
1016 static void cx_tlf (struct sppp *sp)
1017 {
1018 drv_t *d = SP2IFP(sp)->if_softc;
1019
1020 CX_DEBUG (d, ("cx_tlf\n"));
1021 /* cx_set_dtr (d->chan, 0);*/
1022 /* cx_set_rts (d->chan, 0);*/
1023 if (!(IFP2SP(d->ifp)->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
1024 sp->pp_down (sp);
1025 }
1026
1027 static void cx_tls (struct sppp *sp)
1028 {
1029 drv_t *d = SP2IFP(sp)->if_softc;
1030
1031 CX_DEBUG (d, ("cx_tls\n"));
1032 if (!(IFP2SP(d->ifp)->pp_flags & PP_FR) && !(d->ifp->if_flags & PP_CISCO))
1033 sp->pp_up (sp);
1034 }
1035
1036 /*
1037 * Initialization of interface.
1038 * It seems to be never called by upper level.
1039 */
1040 static void cx_initialize (void *softc)
1041 {
1042 drv_t *d = softc;
1043
1044 CX_DEBUG (d, ("cx_initialize\n"));
1045 }
1046
1047 /*
1048 * Process an ioctl request.
1049 */
1050 static int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data)
1051 {
1052 drv_t *d = ifp->if_softc;
1053 bdrv_t *bd = d->board->sys;
1054 int error, s, was_up, should_be_up;
1055
1056 /* No socket ioctls while the channel is in async mode. */
1057 if (d->chan->type == T_NONE || d->chan->mode == M_ASYNC)
1058 return EBUSY;
1059
1060 /* Socket ioctls on slave subchannels are not allowed. */
1061 was_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1062 error = sppp_ioctl (ifp, cmd, data);
1063 if (error)
1064 return error;
1065
1066 s = splhigh ();
1067 CX_LOCK (bd);
1068 if (! (ifp->if_flags & IFF_DEBUG))
1069 d->chan->debug = 0;
1070 else
1071 d->chan->debug = d->chan->debug_shadow;
1072 CX_UNLOCK (bd);
1073 splx (s);
1074
1075 switch (cmd) {
1076 default: CX_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0;
1077 case SIOCADDMULTI: CX_DEBUG2 (d, ("SIOCADDMULTI\n")); return 0;
1078 case SIOCDELMULTI: CX_DEBUG2 (d, ("SIOCDELMULTI\n")); return 0;
1079 case SIOCSIFFLAGS: CX_DEBUG2 (d, ("SIOCSIFFLAGS\n")); break;
1080 case SIOCSIFADDR: CX_DEBUG2 (d, ("SIOCSIFADDR\n")); break;
1081 }
1082
1083 /* We get here only in case of SIFFLAGS or SIFADDR. */
1084 s = splhigh ();
1085 CX_LOCK (bd);
1086 should_be_up = (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1087 if (!was_up && should_be_up) {
1088 /* Interface goes up -- start it. */
1089 cx_up (d);
1090 cx_start (d);
1091 } else if (was_up && !should_be_up) {
1092 /* Interface is going down -- stop it. */
1093 /* if ((IFP2SP(d->ifp)->pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/
1094 cx_down (d);
1095 }
1096 CX_UNLOCK (bd);
1097 splx (s);
1098 return 0;
1099 }
1100 #endif /*NETGRAPH*/
1101
1102 /*
1103 * Stop the interface. Called on splimp().
1104 */
1105 static void cx_down (drv_t *d)
1106 {
1107 int s = splhigh ();
1108 CX_DEBUG (d, ("cx_down\n"));
1109 cx_set_dtr (d->chan, 0);
1110 cx_set_rts (d->chan, 0);
1111 d->running = 0;
1112 callout_stop (&d->timeout_handle);
1113 splx (s);
1114 }
1115
1116 /*
1117 * Start the interface. Called on splimp().
1118 */
1119 static void cx_up (drv_t *d)
1120 {
1121 int s = splhigh ();
1122 CX_DEBUG (d, ("cx_up\n"));
1123 cx_set_dtr (d->chan, 1);
1124 cx_set_rts (d->chan, 1);
1125 d->running = 1;
1126 splx (s);
1127 }
1128
1129 /*
1130 * Start output on the (slave) interface. Get another datagram to send
1131 * off of the interface queue, and copy it to the interface
1132 * before starting the output.
1133 */
1134 static void cx_send (drv_t *d)
1135 {
1136 struct mbuf *m;
1137 u_short len;
1138
1139 CX_DEBUG2 (d, ("cx_send\n"));
1140
1141 /* No output if the interface is down. */
1142 if (! d->running)
1143 return;
1144
1145 /* No output if the modem is off. */
1146 if (! cx_get_dsr (d->chan) && ! cx_get_loop(d->chan))
1147 return;
1148
1149 if (cx_buf_free (d->chan)) {
1150 /* Get the packet to send. */
1151 #ifdef NETGRAPH
1152 IF_DEQUEUE (&d->hi_queue, m);
1153 if (! m)
1154 IF_DEQUEUE (&d->lo_queue, m);
1155 #else
1156 m = sppp_dequeue (d->ifp);
1157 #endif
1158 if (! m)
1159 return;
1160 #ifndef NETGRAPH
1161 BPF_MTAP (d->ifp, m);
1162 #endif
1163 len = m_length (m, NULL);
1164 if (! m->m_next)
1165 cx_send_packet (d->chan, (u_char*)mtod (m, caddr_t),
1166 len, 0);
1167 else {
1168 u_char buf [DMABUFSZ];
1169 m_copydata (m, 0, len, buf);
1170 cx_send_packet (d->chan, buf, len, 0);
1171 }
1172 m_freem (m);
1173
1174 /* Set up transmit timeout, 10 seconds. */
1175 d->timeout = 10;
1176 }
1177 #ifndef NETGRAPH
1178 d->ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1179 #endif
1180 }
1181
1182 /*
1183 * Start output on the interface.
1184 * Always called on splimp().
1185 */
1186 static void cx_start (drv_t *d)
1187 {
1188 int s = splhigh ();
1189 if (d->running) {
1190 if (! d->chan->dtr)
1191 cx_set_dtr (d->chan, 1);
1192 if (! d->chan->rts)
1193 cx_set_rts (d->chan, 1);
1194 cx_send (d);
1195 callout_reset (&d->timeout_handle, hz, cx_watchdog_timer, d);
1196 }
1197 splx (s);
1198 }
1199
1200 /*
1201 * Handle transmit timeouts.
1202 * Recover after lost transmit interrupts.
1203 * Always called on splimp().
1204 */
1205 static void cx_watchdog (drv_t *d)
1206 {
1207
1208 CX_DEBUG (d, ("device timeout\n"));
1209 if (d->running) {
1210 cx_setup_chan (d->chan);
1211 cx_start_chan (d->chan, 0, 0);
1212 cx_set_dtr (d->chan, 1);
1213 cx_set_rts (d->chan, 1);
1214 cx_start (d);
1215 }
1216 }
1217
1218 static void cx_watchdog_timer (void *arg)
1219 {
1220 drv_t *d = arg;
1221 bdrv_t *bd = d->board->sys;
1222
1223 CX_LOCK (bd);
1224 if (d->timeout == 1)
1225 cx_watchdog (d);
1226 if (d->timeout)
1227 d->timeout--;
1228 callout_reset (&d->timeout_handle, hz, cx_watchdog_timer, d);
1229 CX_UNLOCK (bd);
1230 }
1231
1232 /*
1233 * Transmit callback function.
1234 */
1235 static void cx_transmit (cx_chan_t *c, void *attachment, int len)
1236 {
1237 drv_t *d = c->sys;
1238
1239 if (!d)
1240 return;
1241
1242 if (c->mode == M_ASYNC && d->tty) {
1243 d->tty->t_state &= ~(TS_BUSY | TS_FLUSH);
1244 d->atimeout = 0;
1245 if (d->tty->t_dev) {
1246 d->intr_action |= CX_WRITE;
1247 MY_SOFT_INTR = 1;
1248 swi_sched (cx_fast_ih, 0);
1249 }
1250 return;
1251 }
1252 d->timeout = 0;
1253 #ifndef NETGRAPH
1254 if_inc_counter(d->ifp, IFCOUNTER_OPACKETS, 1);
1255 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1256 #endif
1257 cx_start (d);
1258 }
1259
1260 /*
1261 * Process the received packet.
1262 */
1263 static void cx_receive (cx_chan_t *c, char *data, int len)
1264 {
1265 drv_t *d = c->sys;
1266 struct mbuf *m;
1267 char *cc = data;
1268 #ifdef NETGRAPH
1269 int error;
1270 #endif
1271
1272 if (!d)
1273 return;
1274
1275 if (c->mode == M_ASYNC && d->tty) {
1276 if (d->tty->t_state & TS_ISOPEN) {
1277 async_q *q = &d->aqueue;
1278 int size = BF_SZ - 1 - AQ_GSZ (q);
1279
1280 if (len <= 0 && !size)
1281 return;
1282
1283 if (len > size) {
1284 c->ierrs++;
1285 cx_error (c, CX_OVERRUN);
1286 len = size - 1;
1287 }
1288
1289 while (len--) {
1290 AQ_PUSH (q, *(unsigned char *)cc);
1291 cc++;
1292 }
1293
1294 d->intr_action |= CX_READ;
1295 MY_SOFT_INTR = 1;
1296 swi_sched (cx_fast_ih, 0);
1297 }
1298 return;
1299 }
1300 if (! d->running)
1301 return;
1302
1303 m = makembuf (data, len);
1304 if (! m) {
1305 CX_DEBUG (d, ("no memory for packet\n"));
1306 #ifndef NETGRAPH
1307 if_inc_counter(d->ifp, IFCOUNTER_IQDROPS, 1);
1308 #endif
1309 return;
1310 }
1311 if (c->debug > 1)
1312 m_print (m, 0);
1313 #ifdef NETGRAPH
1314 m->m_pkthdr.rcvif = 0;
1315 NG_SEND_DATA_ONLY (error, d->hook, m);
1316 #else
1317 if_inc_counter(d->ifp, IFCOUNTER_IPACKETS, 1);
1318 m->m_pkthdr.rcvif = d->ifp;
1319 /* Check if there's a BPF listener on this interface.
1320 * If so, hand off the raw packet to bpf. */
1321 BPF_MTAP(d->ifp, m);
1322 IF_ENQUEUE (&d->queue, m);
1323 #endif
1324 }
1325
1326 #define CONDITION(t,tp) (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))\
1327 && (!(tp->t_iflag & BRKINT) || (tp->t_iflag & IGNBRK))\
1328 && (!(tp->t_iflag & PARMRK)\
1329 || (tp->t_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))\
1330 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))\
1331 && linesw[tp->t_line]->l_rint == ttyinput)
1332
1333 /*
1334 * Error callback function.
1335 */
1336 static void cx_error (cx_chan_t *c, int data)
1337 {
1338 drv_t *d = c->sys;
1339 async_q *q;
1340
1341 if (!d)
1342 return;
1343
1344 q = &(d->aqueue);
1345
1346 switch (data) {
1347 case CX_FRAME:
1348 CX_DEBUG (d, ("frame error\n"));
1349 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1350 && (AQ_GSZ (q) < BF_SZ - 1)
1351 && (!CONDITION((&d->tty->t_termios), (d->tty))
1352 || !(d->tty->t_iflag & (IGNPAR | PARMRK)))) {
1353 AQ_PUSH (q, TTY_FE);
1354 d->intr_action |= CX_READ;
1355 MY_SOFT_INTR = 1;
1356 swi_sched (cx_fast_ih, 0);
1357 }
1358 #ifndef NETGRAPH
1359 else
1360 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1361 #endif
1362 break;
1363 case CX_CRC:
1364 CX_DEBUG (d, ("crc error\n"));
1365 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1366 && (AQ_GSZ (q) < BF_SZ - 1)
1367 && (!CONDITION((&d->tty->t_termios), (d->tty))
1368 || !(d->tty->t_iflag & INPCK)
1369 || !(d->tty->t_iflag & (IGNPAR | PARMRK)))) {
1370 AQ_PUSH (q, TTY_PE);
1371 d->intr_action |= CX_READ;
1372 MY_SOFT_INTR = 1;
1373 swi_sched (cx_fast_ih, 0);
1374 }
1375 #ifndef NETGRAPH
1376 else
1377 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1378 #endif
1379 break;
1380 case CX_OVERRUN:
1381 CX_DEBUG (d, ("overrun error\n"));
1382 #ifdef TTY_OE
1383 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1384 && (AQ_GSZ (q) < BF_SZ - 1)
1385 && (!CONDITION((&d->tty->t_termios), (d->tty)))) {
1386 AQ_PUSH (q, TTY_OE);
1387 d->intr_action |= CX_READ;
1388 MY_SOFT_INTR = 1;
1389 swi_sched (cx_fast_ih, 0);
1390 }
1391 #endif
1392 #ifndef NETGRAPH
1393 else {
1394 if_inc_counter(d->ifp, IFCOUNTER_COLLISIONS, 1);
1395 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1396 }
1397 #endif
1398 break;
1399 case CX_OVERFLOW:
1400 CX_DEBUG (d, ("overflow error\n"));
1401 #ifndef NETGRAPH
1402 if (c->mode != M_ASYNC)
1403 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1404 #endif
1405 break;
1406 case CX_UNDERRUN:
1407 CX_DEBUG (d, ("underrun error\n"));
1408 if (c->mode != M_ASYNC) {
1409 d->timeout = 0;
1410 #ifndef NETGRAPH
1411 if_inc_counter(d->ifp, IFCOUNTER_OERRORS, 1);
1412 d->ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1413 #endif
1414 cx_start (d);
1415 }
1416 break;
1417 case CX_BREAK:
1418 CX_DEBUG (d, ("break error\n"));
1419 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN)
1420 && (AQ_GSZ (q) < BF_SZ - 1)
1421 && (!CONDITION((&d->tty->t_termios), (d->tty))
1422 || !(d->tty->t_iflag & (IGNBRK | BRKINT | PARMRK)))) {
1423 AQ_PUSH (q, TTY_BI);
1424 d->intr_action |= CX_READ;
1425 MY_SOFT_INTR = 1;
1426 swi_sched (cx_fast_ih, 0);
1427 }
1428 #ifndef NETGRAPH
1429 else
1430 if_inc_counter(d->ifp, IFCOUNTER_IERRORS, 1);
1431 #endif
1432 break;
1433 default:
1434 CX_DEBUG (d, ("error #%d\n", data));
1435 }
1436 }
1437
1438 static int cx_topen (struct tty *tp, struct cdev *dev)
1439 {
1440 bdrv_t *bd;
1441 drv_t *d;
1442
1443 d = tp->t_sc;
1444 CX_DEBUG2 (d, ("cx_open (serial)\n"));
1445
1446 bd = d->board->sys;
1447
1448 if (d->chan->mode != M_ASYNC)
1449 return (EBUSY);
1450
1451 d->open_dev |= 0x2;
1452 CX_LOCK (bd);
1453 cx_start_chan (d->chan, 0, 0);
1454 cx_set_dtr (d->chan, 1);
1455 cx_set_rts (d->chan, 1);
1456 d->cd = cx_get_cd (d->chan);
1457 CX_UNLOCK (bd);
1458
1459 CX_DEBUG2 (d, ("cx_open done\n"));
1460
1461 return 0;
1462 }
1463
1464 static void cx_tclose (struct tty *tp)
1465 {
1466 drv_t *d;
1467 bdrv_t *bd;
1468
1469 d = tp->t_sc;
1470 CX_DEBUG2 (d, ("cx_close\n"));
1471 bd = d->board->sys;
1472 CX_LOCK (bd);
1473 /* Disable receiver.
1474 * Transmitter continues sending the queued data. */
1475 cx_enable_receive (d->chan, 0);
1476 CX_UNLOCK (bd);
1477 d->open_dev &= ~0x2;
1478 }
1479
1480 static int cx_tmodem (struct tty *tp, int sigon, int sigoff)
1481 {
1482 drv_t *d;
1483 bdrv_t *bd;
1484
1485 d = tp->t_sc;
1486 bd = d->board->sys;
1487
1488 CX_LOCK (bd);
1489 if (!sigon && !sigoff) {
1490 if (cx_get_dsr (d->chan)) sigon |= SER_DSR;
1491 if (cx_get_cd (d->chan)) sigon |= SER_DCD;
1492 if (cx_get_cts (d->chan)) sigon |= SER_CTS;
1493 if (d->chan->dtr) sigon |= SER_DTR;
1494 if (d->chan->rts) sigon |= SER_RTS;
1495 CX_UNLOCK (bd);
1496 return sigon;
1497 }
1498
1499 if (sigon & SER_DTR)
1500 cx_set_dtr (d->chan, 1);
1501 if (sigoff & SER_DTR)
1502 cx_set_dtr (d->chan, 0);
1503 if (sigon & SER_RTS)
1504 cx_set_rts (d->chan, 1);
1505 if (sigoff & SER_RTS)
1506 cx_set_rts (d->chan, 0);
1507 CX_UNLOCK (bd);
1508
1509 return (0);
1510 }
1511
1512 static int cx_open (struct cdev *dev, int flag, int mode, struct thread *td)
1513 {
1514 int unit;
1515 drv_t *d;
1516
1517 d = dev->si_drv1;
1518 unit = d->chan->num;
1519
1520 CX_DEBUG2 (d, ("cx_open unit=%d, flag=0x%x, mode=0x%x\n",
1521 unit, flag, mode));
1522
1523 d->open_dev |= 0x1;
1524
1525 CX_DEBUG2 (d, ("cx_open done\n"));
1526
1527 return 0;
1528 }
1529
1530 static int cx_close (struct cdev *dev, int flag, int mode, struct thread *td)
1531 {
1532 drv_t *d;
1533
1534 d = dev->si_drv1;
1535 CX_DEBUG2 (d, ("cx_close\n"));
1536 d->open_dev &= ~0x1;
1537 return 0;
1538 }
1539
1540 static int cx_modem_status (drv_t *d)
1541 {
1542 bdrv_t *bd = d->board->sys;
1543 int status = 0, s = splhigh ();
1544 CX_LOCK (bd);
1545 /* Already opened by someone or network interface is up? */
1546 if ((d->chan->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
1547 (d->open_dev|0x2)) || (d->chan->mode != M_ASYNC && d->running))
1548 status = TIOCM_LE; /* always enabled while open */
1549
1550 if (cx_get_dsr (d->chan)) status |= TIOCM_DSR;
1551 if (cx_get_cd (d->chan)) status |= TIOCM_CD;
1552 if (cx_get_cts (d->chan)) status |= TIOCM_CTS;
1553 if (d->chan->dtr) status |= TIOCM_DTR;
1554 if (d->chan->rts) status |= TIOCM_RTS;
1555 CX_UNLOCK (bd);
1556 splx (s);
1557 return status;
1558 }
1559
1560 static int cx_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
1561 {
1562 drv_t *d;
1563 bdrv_t *bd;
1564 cx_chan_t *c;
1565 struct serial_statistics *st;
1566 int error, s;
1567 char mask[16];
1568
1569 d = dev->si_drv1;
1570 c = d->chan;
1571
1572 bd = d->board->sys;
1573
1574 switch (cmd) {
1575 case SERIAL_GETREGISTERED:
1576 CX_DEBUG2 (d, ("ioctl: getregistered\n"));
1577 bzero (mask, sizeof(mask));
1578 for (s=0; s<NCX*NCHAN; ++s)
1579 if (channel [s])
1580 mask [s/8] |= 1 << (s & 7);
1581 bcopy (mask, data, sizeof (mask));
1582 return 0;
1583
1584 case SERIAL_GETPORT:
1585 CX_DEBUG2 (d, ("ioctl: getport\n"));
1586 s = splhigh ();
1587 CX_LOCK (bd);
1588 *(int *)data = cx_get_port (c);
1589 CX_UNLOCK (bd);
1590 splx (s);
1591 if (*(int *)data<0)
1592 return (EINVAL);
1593 else
1594 return 0;
1595
1596 case SERIAL_SETPORT:
1597 CX_DEBUG2 (d, ("ioctl: setproto\n"));
1598 /* Only for superuser! */
1599 error = priv_check (td, PRIV_DRIVER);
1600 if (error)
1601 return error;
1602
1603 s = splhigh ();
1604 CX_LOCK (bd);
1605 cx_set_port (c, *(int *)data);
1606 CX_UNLOCK (bd);
1607 splx (s);
1608 return 0;
1609
1610 #ifndef NETGRAPH
1611 case SERIAL_GETPROTO:
1612 CX_DEBUG2 (d, ("ioctl: getproto\n"));
1613 s = splhigh ();
1614 CX_LOCK (bd);
1615 strcpy ((char*)data, (c->mode == M_ASYNC) ? "async" :
1616 (IFP2SP(d->ifp)->pp_flags & PP_FR) ? "fr" :
1617 (d->ifp->if_flags & PP_CISCO) ? "cisco" : "ppp");
1618 CX_UNLOCK (bd);
1619 splx (s);
1620 return 0;
1621
1622 case SERIAL_SETPROTO:
1623 CX_DEBUG2 (d, ("ioctl: setproto\n"));
1624 /* Only for superuser! */
1625 error = priv_check (td, PRIV_DRIVER);
1626 if (error)
1627 return error;
1628 if (c->mode == M_ASYNC)
1629 return EBUSY;
1630 if (d->ifp->if_drv_flags & IFF_DRV_RUNNING)
1631 return EBUSY;
1632 if (! strcmp ("cisco", (char*)data)) {
1633 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR);
1634 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1635 d->ifp->if_flags |= PP_CISCO;
1636 } else if (! strcmp ("fr", (char*)data)) {
1637 d->ifp->if_flags &= ~(PP_CISCO);
1638 IFP2SP(d->ifp)->pp_flags |= PP_FR | PP_KEEPALIVE;
1639 } else if (! strcmp ("ppp", (char*)data)) {
1640 IFP2SP(d->ifp)->pp_flags &= ~(PP_FR | PP_KEEPALIVE);
1641 d->ifp->if_flags &= ~(PP_CISCO);
1642 } else
1643 return EINVAL;
1644 return 0;
1645
1646 case SERIAL_GETKEEPALIVE:
1647 CX_DEBUG2 (d, ("ioctl: getkeepalive\n"));
1648 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1649 (d->ifp->if_flags & PP_CISCO) ||
1650 (c->mode == M_ASYNC))
1651 return EINVAL;
1652 s = splhigh ();
1653 CX_LOCK (bd);
1654 *(int*)data = (IFP2SP(d->ifp)->pp_flags & PP_KEEPALIVE) ? 1 : 0;
1655 CX_UNLOCK (bd);
1656 splx (s);
1657 return 0;
1658
1659 case SERIAL_SETKEEPALIVE:
1660 CX_DEBUG2 (d, ("ioctl: setkeepalive\n"));
1661 /* Only for superuser! */
1662 error = priv_check (td, PRIV_DRIVER);
1663 if (error)
1664 return error;
1665 if ((IFP2SP(d->ifp)->pp_flags & PP_FR) ||
1666 (d->ifp->if_flags & PP_CISCO))
1667 return EINVAL;
1668 s = splhigh ();
1669 CX_LOCK (bd);
1670 if (*(int*)data)
1671 IFP2SP(d->ifp)->pp_flags |= PP_KEEPALIVE;
1672 else
1673 IFP2SP(d->ifp)->pp_flags &= ~PP_KEEPALIVE;
1674 CX_UNLOCK (bd);
1675 splx (s);
1676 return 0;
1677 #endif /*NETGRAPH*/
1678
1679 case SERIAL_GETMODE:
1680 CX_DEBUG2 (d, ("ioctl: getmode\n"));
1681 s = splhigh ();
1682 CX_LOCK (bd);
1683 *(int*)data = (c->mode == M_ASYNC) ?
1684 SERIAL_ASYNC : SERIAL_HDLC;
1685 CX_UNLOCK (bd);
1686 splx (s);
1687 return 0;
1688
1689 case SERIAL_SETMODE:
1690 CX_DEBUG2 (d, ("ioctl: setmode\n"));
1691 /* Only for superuser! */
1692 error = priv_check (td, PRIV_DRIVER);
1693 if (error)
1694 return error;
1695
1696 /* Somebody is waiting for carrier? */
1697 if (d->lock)
1698 return EBUSY;
1699 /* /dev/ttyXX is already opened by someone? */
1700 if (c->mode == M_ASYNC && d->tty && (d->tty->t_state & TS_ISOPEN) &&
1701 (d->open_dev|0x2))
1702 return EBUSY;
1703 /* Network interface is up?
1704 * Cannot change to async mode. */
1705 if (c->mode != M_ASYNC && d->running &&
1706 (*(int*)data == SERIAL_ASYNC))
1707 return EBUSY;
1708
1709 s = splhigh ();
1710 CX_LOCK (bd);
1711 if (c->mode == M_HDLC && *(int*)data == SERIAL_ASYNC) {
1712 cx_set_mode (c, M_ASYNC);
1713 cx_enable_receive (c, 0);
1714 cx_enable_transmit (c, 0);
1715 } else if (c->mode == M_ASYNC && *(int*)data == SERIAL_HDLC) {
1716 if (d->ifp->if_flags & IFF_DEBUG)
1717 c->debug = c->debug_shadow;
1718 cx_set_mode (c, M_HDLC);
1719 cx_enable_receive (c, 1);
1720 cx_enable_transmit (c, 1);
1721 }
1722 CX_UNLOCK (bd);
1723 splx (s);
1724 return 0;
1725
1726 case SERIAL_GETSTAT:
1727 CX_DEBUG2 (d, ("ioctl: getestat\n"));
1728 st = (struct serial_statistics*) data;
1729 s = splhigh ();
1730 CX_LOCK (bd);
1731 st->rintr = c->rintr;
1732 st->tintr = c->tintr;
1733 st->mintr = c->mintr;
1734 st->ibytes = c->ibytes;
1735 st->ipkts = c->ipkts;
1736 st->ierrs = c->ierrs;
1737 st->obytes = c->obytes;
1738 st->opkts = c->opkts;
1739 st->oerrs = c->oerrs;
1740 CX_UNLOCK (bd);
1741 splx (s);
1742 return 0;
1743
1744 case SERIAL_CLRSTAT:
1745 CX_DEBUG2 (d, ("ioctl: clrstat\n"));
1746 /* Only for superuser! */
1747 error = priv_check (td, PRIV_DRIVER);
1748 if (error)
1749 return error;
1750 s = splhigh ();
1751 CX_LOCK (bd);
1752 c->rintr = 0;
1753 c->tintr = 0;
1754 c->mintr = 0;
1755 c->ibytes = 0;
1756 c->ipkts = 0;
1757 c->ierrs = 0;
1758 c->obytes = 0;
1759 c->opkts = 0;
1760 c->oerrs = 0;
1761 CX_UNLOCK (bd);
1762 splx (s);
1763 return 0;
1764
1765 case SERIAL_GETBAUD:
1766 CX_DEBUG2 (d, ("ioctl: getbaud\n"));
1767 if (c->mode == M_ASYNC)
1768 return EINVAL;
1769 s = splhigh ();
1770 CX_LOCK (bd);
1771 *(long*)data = cx_get_baud(c);
1772 CX_UNLOCK (bd);
1773 splx (s);
1774 return 0;
1775
1776 case SERIAL_SETBAUD:
1777 CX_DEBUG2 (d, ("ioctl: setbaud\n"));
1778 /* Only for superuser! */
1779 error = priv_check (td, PRIV_DRIVER);
1780 if (error)
1781 return error;
1782 if (c->mode == M_ASYNC)
1783 return EINVAL;
1784 s = splhigh ();
1785 CX_LOCK (bd);
1786 cx_set_baud (c, *(long*)data);
1787 CX_UNLOCK (bd);
1788 splx (s);
1789 return 0;
1790
1791 case SERIAL_GETLOOP:
1792 CX_DEBUG2 (d, ("ioctl: getloop\n"));
1793 if (c->mode == M_ASYNC)
1794 return EINVAL;
1795 s = splhigh ();
1796 CX_LOCK (bd);
1797 *(int*)data = cx_get_loop (c);
1798 CX_UNLOCK (bd);
1799 splx (s);
1800 return 0;
1801
1802 case SERIAL_SETLOOP:
1803 CX_DEBUG2 (d, ("ioctl: setloop\n"));
1804 /* Only for superuser! */
1805 error = priv_check (td, PRIV_DRIVER);
1806 if (error)
1807 return error;
1808 if (c->mode == M_ASYNC)
1809 return EINVAL;
1810 s = splhigh ();
1811 CX_LOCK (bd);
1812 cx_set_loop (c, *(int*)data);
1813 CX_UNLOCK (bd);
1814 splx (s);
1815 return 0;
1816
1817 case SERIAL_GETDPLL:
1818 CX_DEBUG2 (d, ("ioctl: getdpll\n"));
1819 if (c->mode == M_ASYNC)
1820 return EINVAL;
1821 s = splhigh ();
1822 CX_LOCK (bd);
1823 *(int*)data = cx_get_dpll (c);
1824 CX_UNLOCK (bd);
1825 splx (s);
1826 return 0;
1827
1828 case SERIAL_SETDPLL:
1829 CX_DEBUG2 (d, ("ioctl: setdpll\n"));
1830 /* Only for superuser! */
1831 error = priv_check (td, PRIV_DRIVER);
1832 if (error)
1833 return error;
1834 if (c->mode == M_ASYNC)
1835 return EINVAL;
1836 s = splhigh ();
1837 CX_LOCK (bd);
1838 cx_set_dpll (c, *(int*)data);
1839 CX_UNLOCK (bd);
1840 splx (s);
1841 return 0;
1842
1843 case SERIAL_GETNRZI:
1844 CX_DEBUG2 (d, ("ioctl: getnrzi\n"));
1845 if (c->mode == M_ASYNC)
1846 return EINVAL;
1847 s = splhigh ();
1848 CX_LOCK (bd);
1849 *(int*)data = cx_get_nrzi (c);
1850 CX_UNLOCK (bd);
1851 splx (s);
1852 return 0;
1853
1854 case SERIAL_SETNRZI:
1855 CX_DEBUG2 (d, ("ioctl: setnrzi\n"));
1856 /* Only for superuser! */
1857 error = priv_check (td, PRIV_DRIVER);
1858 if (error)
1859 return error;
1860 if (c->mode == M_ASYNC)
1861 return EINVAL;
1862 s = splhigh ();
1863 CX_LOCK (bd);
1864 cx_set_nrzi (c, *(int*)data);
1865 CX_UNLOCK (bd);
1866 splx (s);
1867 return 0;
1868
1869 case SERIAL_GETDEBUG:
1870 CX_DEBUG2 (d, ("ioctl: getdebug\n"));
1871 s = splhigh ();
1872 CX_LOCK (bd);
1873 *(int*)data = c->debug;
1874 CX_UNLOCK (bd);
1875 splx (s);
1876 return 0;
1877
1878 case SERIAL_SETDEBUG:
1879 CX_DEBUG2 (d, ("ioctl: setdebug\n"));
1880 /* Only for superuser! */
1881 error = priv_check (td, PRIV_DRIVER);
1882 if (error)
1883 return error;
1884 s = splhigh ();
1885 CX_LOCK (bd);
1886 #ifndef NETGRAPH
1887 if (c->mode == M_ASYNC) {
1888 c->debug = *(int*)data;
1889 } else {
1890 /*
1891 * The debug_shadow is always greater than zero for
1892 * logic simplicity. For switching debug off the
1893 * IFF_DEBUG is responsible (for !M_ASYNC mode).
1894 */
1895 c->debug_shadow = (*(int*)data) ? (*(int*)data) : 1;
1896 if (d->ifp->if_flags & IFF_DEBUG)
1897 c->debug = c->debug_shadow;
1898 }
1899 #else
1900 c->debug = *(int*)data;
1901 #endif
1902 CX_UNLOCK (bd);
1903 splx (s);
1904 return 0;
1905 }
1906
1907 switch (cmd) {
1908 case TIOCSDTR: /* Set DTR */
1909 CX_DEBUG2 (d, ("ioctl: tiocsdtr\n"));
1910 s = splhigh ();
1911 CX_LOCK (bd);
1912 cx_set_dtr (c, 1);
1913 CX_UNLOCK (bd);
1914 splx (s);
1915 return 0;
1916
1917 case TIOCCDTR: /* Clear DTR */
1918 CX_DEBUG2 (d, ("ioctl: tioccdtr\n"));
1919 s = splhigh ();
1920 CX_LOCK (bd);
1921 cx_set_dtr (c, 0);
1922 CX_UNLOCK (bd);
1923 splx (s);
1924 return 0;
1925
1926 case TIOCMSET: /* Set DTR/RTS */
1927 CX_DEBUG2 (d, ("ioctl: tiocmset\n"));
1928 s = splhigh ();
1929 CX_LOCK (bd);
1930 cx_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0);
1931 cx_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0);
1932 CX_UNLOCK (bd);
1933 splx (s);
1934 return 0;
1935
1936 case TIOCMBIS: /* Add DTR/RTS */
1937 CX_DEBUG2 (d, ("ioctl: tiocmbis\n"));
1938 s = splhigh ();
1939 CX_LOCK (bd);
1940 if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 1);
1941 if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 1);
1942 CX_UNLOCK (bd);
1943 splx (s);
1944 return 0;
1945
1946 case TIOCMBIC: /* Clear DTR/RTS */
1947 CX_DEBUG2 (d, ("ioctl: tiocmbic\n"));
1948 s = splhigh ();
1949 CX_LOCK (bd);
1950 if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 0);
1951 if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 0);
1952 CX_UNLOCK (bd);
1953 splx (s);
1954 return 0;
1955
1956 case TIOCMGET: /* Get modem status */
1957 CX_DEBUG2 (d, ("ioctl: tiocmget\n"));
1958 *(int*)data = cx_modem_status (d);
1959 return 0;
1960
1961 }
1962
1963 CX_DEBUG2 (d, ("ioctl: 0x%lx\n", cmd));
1964 return ENOTTY;
1965 }
1966
1967 void cx_softintr (void *unused)
1968 {
1969 drv_t *d;
1970 bdrv_t *bd;
1971 async_q *q;
1972 int i, s, ic, k;
1973 while (MY_SOFT_INTR) {
1974 MY_SOFT_INTR = 0;
1975 for (i=0; i<NCX*NCHAN; ++i) {
1976 d = channel [i];
1977 if (!d || !d->chan || d->chan->type == T_NONE
1978 || d->chan->mode != M_ASYNC || !d->tty
1979 || !d->tty->t_dev)
1980 continue;
1981 bd = d->board->sys;
1982 s = splhigh ();
1983 CX_LOCK (bd);
1984 if (d->intr_action & CX_READ) {
1985 q = &(d->aqueue);
1986 if (d->tty->t_state & TS_CAN_BYPASS_L_RINT) {
1987 k = AQ_GSZ(q);
1988 if (d->tty->t_rawq.c_cc + k >
1989 d->tty->t_ihiwat
1990 && (d->tty->t_cflag & CRTS_IFLOW
1991 || d->tty->t_iflag & IXOFF)
1992 && !(d->tty->t_state & TS_TBLOCK))
1993 ttyblock(d->tty);
1994 d->tty->t_rawcc += k;
1995 while (k>0) {
1996 k--;
1997 AQ_POP (q, ic);
1998 CX_UNLOCK (bd);
1999 splx (s);
2000 putc (ic, &d->tty->t_rawq);
2001 s = splhigh ();
2002 CX_LOCK (bd);
2003 }
2004 ttwakeup(d->tty);
2005 if (d->tty->t_state & TS_TTSTOP
2006 && (d->tty->t_iflag & IXANY
2007 || d->tty->t_cc[VSTART] ==
2008 d->tty->t_cc[VSTOP])) {
2009 d->tty->t_state &= ~TS_TTSTOP;
2010 d->tty->t_lflag &= ~FLUSHO;
2011 d->intr_action |= CX_WRITE;
2012 }
2013 } else {
2014 while (q->end != q->beg) {
2015 AQ_POP (q, ic);
2016 CX_UNLOCK (bd);
2017 splx (s);
2018 ttyld_rint (d->tty, ic);
2019 s = splhigh ();
2020 CX_LOCK (bd);
2021 }
2022 }
2023 d->intr_action &= ~CX_READ;
2024 }
2025 splx (s);
2026 CX_UNLOCK (bd);
2027
2028 s = splhigh ();
2029 CX_LOCK (bd);
2030 if (d->intr_action & CX_WRITE) {
2031 if (d->tty->t_line)
2032 ttyld_start (d->tty);
2033 else
2034 cx_oproc (d->tty);
2035 d->intr_action &= ~CX_WRITE;
2036 }
2037 CX_UNLOCK (bd);
2038 splx (s);
2039
2040 }
2041 }
2042 }
2043
2044 /*
2045 * Fill transmitter buffer with data.
2046 */
2047 static void cx_oproc (struct tty *tp)
2048 {
2049 int s, k;
2050 drv_t *d;
2051 bdrv_t *bd;
2052 static u_char buf[DMABUFSZ];
2053 u_char *p;
2054 u_short len = 0, sublen = 0;
2055
2056 d = tp->t_sc;
2057 bd = d->board->sys;
2058
2059 CX_DEBUG2 (d, ("cx_oproc\n"));
2060
2061 s = splhigh ();
2062 CX_LOCK (bd);
2063
2064 if (tp->t_cflag & CRTSCTS && (tp->t_state & TS_TBLOCK) && d->chan->rts)
2065 cx_set_rts (d->chan, 0);
2066 else if (tp->t_cflag & CRTSCTS && ! (tp->t_state & TS_TBLOCK) && ! d->chan->rts)
2067 cx_set_rts (d->chan, 1);
2068
2069 if (! (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))) {
2070 /* Start transmitter. */
2071 cx_enable_transmit (d->chan, 1);
2072
2073 /* Is it busy? */
2074 if (! cx_buf_free (d->chan)) {
2075 tp->t_state |= TS_BUSY;
2076 CX_UNLOCK (bd);
2077 splx (s);
2078 return;
2079 }
2080 if (tp->t_iflag & IXOFF) {
2081 p = (buf + (DMABUFSZ/2));
2082 sublen = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
2083 k = sublen;
2084 while (k--) {
2085 /* Send XON/XOFF out of band. */
2086 if (*p == tp->t_cc[VSTOP]) {
2087 cx_xflow_ctl (d->chan, 0);
2088 p++;
2089 continue;
2090 }
2091 if (*p == tp->t_cc[VSTART]) {
2092 cx_xflow_ctl (d->chan, 1);
2093 p++;
2094 continue;
2095 }
2096 buf[len] = *p;
2097 len++;
2098 p++;
2099 }
2100 } else {
2101 p = buf;
2102 len = q_to_b (&tp->t_outq, p, (DMABUFSZ/2));
2103 }
2104 if (len) {
2105 cx_send_packet (d->chan, buf, len, 0);
2106 tp->t_state |= TS_BUSY;
2107 d->atimeout = 10;
2108 CX_DEBUG2 (d, ("out %d bytes\n", len));
2109 }
2110 }
2111 ttwwakeup (tp);
2112 CX_UNLOCK (bd);
2113 splx (s);
2114 }
2115
2116 static int cx_param (struct tty *tp, struct termios *t)
2117 {
2118 drv_t *d;
2119 bdrv_t *bd;
2120 int s, bits, parity;
2121
2122 d = tp->t_sc;
2123 bd = d->board->sys;
2124
2125 s = splhigh ();
2126 CX_LOCK (bd);
2127 if (t->c_ospeed == 0) {
2128 /* Clear DTR and RTS. */
2129 cx_set_dtr (d->chan, 0);
2130 CX_UNLOCK (bd);
2131 splx (s);
2132 CX_DEBUG2 (d, ("cx_param (hangup)\n"));
2133 return 0;
2134 }
2135 CX_DEBUG2 (d, ("cx_param\n"));
2136
2137 /* Check requested parameters. */
2138 if (t->c_ospeed < 300 || t->c_ospeed > 256*1024) {
2139 CX_UNLOCK (bd);
2140 splx (s);
2141 return EINVAL;
2142 }
2143 if (t->c_ispeed && (t->c_ispeed < 300 || t->c_ispeed > 256*1024)) {
2144 CX_UNLOCK (bd);
2145 splx (s);
2146 return EINVAL;
2147 }
2148
2149 /* And copy them to tty and channel structures. */
2150 tp->t_ispeed = t->c_ispeed = tp->t_ospeed = t->c_ospeed;
2151 tp->t_cflag = t->c_cflag;
2152
2153 /* Set character length and parity mode. */
2154 switch (t->c_cflag & CSIZE) {
2155 default:
2156 case CS8: bits = 8; break;
2157 case CS7: bits = 7; break;
2158 case CS6: bits = 6; break;
2159 case CS5: bits = 5; break;
2160 }
2161
2162 parity = ((t->c_cflag & PARENB) ? 1 : 0) *
2163 (1 + ((t->c_cflag & PARODD) ? 0 : 1));
2164
2165 /* Set current channel number. */
2166 if (! d->chan->dtr)
2167 cx_set_dtr (d->chan, 1);
2168
2169 ttyldoptim (tp);
2170 cx_set_async_param (d->chan, t->c_ospeed, bits, parity, (t->c_cflag & CSTOPB),
2171 !(t->c_cflag & PARENB), (t->c_cflag & CRTSCTS),
2172 (t->c_iflag & IXON), (t->c_iflag & IXANY),
2173 t->c_cc[VSTART], t->c_cc[VSTOP]);
2174 CX_UNLOCK (bd);
2175 splx (s);
2176 return 0;
2177 }
2178
2179 /*
2180 * Stop output on a line
2181 */
2182 static void cx_stop (struct tty *tp, int flag)
2183 {
2184 drv_t *d;
2185 bdrv_t *bd;
2186 int s;
2187
2188 d = tp->t_sc;
2189 bd = d->board->sys;
2190
2191 s = splhigh ();
2192 CX_LOCK (bd);
2193 if (tp->t_state & TS_BUSY) {
2194 /* Stop transmitter */
2195 CX_DEBUG2 (d, ("cx_stop\n"));
2196 cx_transmitter_ctl (d->chan, 0);
2197 }
2198 CX_UNLOCK (bd);
2199 splx (s);
2200 }
2201
2202 /*
2203 * Process the (delayed) carrier signal setup.
2204 */
2205 static void cx_carrier (void *arg)
2206 {
2207 drv_t *d = arg;
2208 bdrv_t *bd = d->board->sys;
2209 cx_chan_t *c = d->chan;
2210 int s, cd;
2211
2212 s = splhigh ();
2213 CX_LOCK (bd);
2214 cd = cx_get_cd (c);
2215 if (d->cd != cd) {
2216 if (cd) {
2217 CX_DEBUG (d, ("carrier on\n"));
2218 d->cd = 1;
2219 CX_UNLOCK (bd);
2220 splx (s);
2221 if (d->tty)
2222 ttyld_modem(d->tty, 1);
2223 } else {
2224 CX_DEBUG (d, ("carrier loss\n"));
2225 d->cd = 0;
2226 CX_UNLOCK (bd);
2227 splx (s);
2228 if (d->tty)
2229 ttyld_modem(d->tty, 0);
2230 }
2231 } else {
2232 CX_UNLOCK (bd);
2233 splx (s);
2234 }
2235 }
2236
2237 /*
2238 * Modem signal callback function.
2239 */
2240 static void cx_modem (cx_chan_t *c)
2241 {
2242 drv_t *d = c->sys;
2243
2244 if (!d || c->mode != M_ASYNC)
2245 return;
2246 /* Handle carrier detect/loss. */
2247 /* Carrier changed - delay processing DCD for a while
2248 * to give both sides some time to initialize. */
2249 callout_reset (&d->dcd_timeout_handle, hz/2, cx_carrier, d);
2250 }
2251
2252 #ifdef NETGRAPH
2253 static int ng_cx_constructor (node_p node)
2254 {
2255 drv_t *d = NG_NODE_PRIVATE (node);
2256 CX_DEBUG (d, ("Constructor\n"));
2257 return EINVAL;
2258 }
2259
2260 static int ng_cx_newhook (node_p node, hook_p hook, const char *name)
2261 {
2262 int s;
2263 drv_t *d = NG_NODE_PRIVATE (node);
2264 bdrv_t *bd = d->board->sys;
2265
2266 if (d->chan->mode == M_ASYNC)
2267 return EINVAL;
2268
2269 /* Attach debug hook */
2270 if (strcmp (name, NG_CX_HOOK_DEBUG) == 0) {
2271 NG_HOOK_SET_PRIVATE (hook, NULL);
2272 d->debug_hook = hook;
2273 return 0;
2274 }
2275
2276 /* Check for raw hook */
2277 if (strcmp (name, NG_CX_HOOK_RAW) != 0)
2278 return EINVAL;
2279
2280 NG_HOOK_SET_PRIVATE (hook, d);
2281 d->hook = hook;
2282 s = splhigh ();
2283 CX_LOCK (bd);
2284 cx_up (d);
2285 CX_UNLOCK (bd);
2286 splx (s);
2287 return 0;
2288 }
2289
2290 static int print_modems (char *s, cx_chan_t *c, int need_header)
2291 {
2292 int status = cx_modem_status (c->sys);
2293 int length = 0;
2294
2295 if (need_header)
2296 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n");
2297 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n",
2298 status & TIOCM_LE ? "On" : "-",
2299 status & TIOCM_DTR ? "On" : "-",
2300 status & TIOCM_DSR ? "On" : "-",
2301 status & TIOCM_RTS ? "On" : "-",
2302 status & TIOCM_CTS ? "On" : "-",
2303 status & TIOCM_CD ? "On" : "-");
2304 return length;
2305 }
2306
2307 static int print_stats (char *s, cx_chan_t *c, int need_header)
2308 {
2309 int length = 0;
2310
2311 if (need_header)
2312 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n");
2313 length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n",
2314 c->rintr, c->tintr, c->mintr, c->ibytes, c->ipkts,
2315 c->ierrs, c->obytes, c->opkts, c->oerrs);
2316 return length;
2317 }
2318
2319 static int print_chan (char *s, cx_chan_t *c)
2320 {
2321 drv_t *d = c->sys;
2322 int length = 0;
2323
2324 length += sprintf (s + length, "cx%d", c->board->num * NCHAN + c->num);
2325 if (d->chan->debug)
2326 length += sprintf (s + length, " debug=%d", d->chan->debug);
2327
2328 if (cx_get_baud (c))
2329 length += sprintf (s + length, " %ld", cx_get_baud (c));
2330 else
2331 length += sprintf (s + length, " extclock");
2332
2333 if (c->mode == M_HDLC) {
2334 length += sprintf (s + length, " dpll=%s", cx_get_dpll (c) ? "on" : "off");
2335 length += sprintf (s + length, " nrzi=%s", cx_get_nrzi (c) ? "on" : "off");
2336 }
2337
2338 length += sprintf (s + length, " loop=%s", cx_get_loop (c) ? "on\n" : "off\n");
2339 return length;
2340 }
2341
2342 static int ng_cx_rcvmsg (node_p node, item_p item, hook_p lasthook)
2343 {
2344 drv_t *d = NG_NODE_PRIVATE (node);
2345 struct ng_mesg *msg;
2346 struct ng_mesg *resp = NULL;
2347 int error = 0;
2348
2349 if (!d)
2350 return EINVAL;
2351
2352 CX_DEBUG (d, ("Rcvmsg\n"));
2353 NGI_GET_MSG (item, msg);
2354 switch (msg->header.typecookie) {
2355 default:
2356 error = EINVAL;
2357 break;
2358
2359 case NGM_CX_COOKIE:
2360 printf ("Don't forget to implement\n");
2361 error = EINVAL;
2362 break;
2363
2364 case NGM_GENERIC_COOKIE:
2365 switch (msg->header.cmd) {
2366 default:
2367 error = EINVAL;
2368 break;
2369
2370 case NGM_TEXT_STATUS: {
2371 char *s;
2372 int l = 0;
2373 int dl = sizeof (struct ng_mesg) + 730;
2374
2375 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT);
2376 if (! resp) {
2377 error = ENOMEM;
2378 break;
2379 }
2380 bzero (resp, dl);
2381 s = (resp)->data;
2382 l += print_chan (s + l, d->chan);
2383 l += print_stats (s + l, d->chan, 1);
2384 l += print_modems (s + l, d->chan, 1);
2385 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRSIZ);
2386 }
2387 break;
2388 }
2389 break;
2390 }
2391 NG_RESPOND_MSG (error, node, item, resp);
2392 NG_FREE_MSG (msg);
2393 return error;
2394 }
2395
2396 static int ng_cx_rcvdata (hook_p hook, item_p item)
2397 {
2398 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook));
2399 struct mbuf *m;
2400 struct ng_tag_prio *ptag;
2401 bdrv_t *bd;
2402 struct ifqueue *q;
2403 int s;
2404
2405 NGI_GET_M (item, m);
2406 NG_FREE_ITEM (item);
2407 if (! NG_HOOK_PRIVATE (hook) || ! d) {
2408 NG_FREE_M (m);
2409 return ENETDOWN;
2410 }
2411
2412 bd = d->board->sys;
2413 /* Check for high priority data */
2414 if ((ptag = (struct ng_tag_prio *)m_tag_locate(m, NGM_GENERIC_COOKIE,
2415 NG_TAG_PRIO, NULL)) != NULL && (ptag->priority > NG_PRIO_CUTOFF) )
2416 q = &d->hi_queue;
2417 else
2418 q = &d->lo_queue;
2419
2420 s = splhigh ();
2421 CX_LOCK (bd);
2422 IF_LOCK (q);
2423 if (_IF_QFULL (q)) {
2424 IF_UNLOCK (q);
2425 CX_UNLOCK (bd);
2426 splx (s);
2427 NG_FREE_M (m);
2428 return ENOBUFS;
2429 }
2430 _IF_ENQUEUE (q, m);
2431 IF_UNLOCK (q);
2432 cx_start (d);
2433 CX_UNLOCK (bd);
2434 splx (s);
2435 return 0;
2436 }
2437
2438 static int ng_cx_rmnode (node_p node)
2439 {
2440 drv_t *d = NG_NODE_PRIVATE (node);
2441 bdrv_t *bd;
2442
2443 CX_DEBUG (d, ("Rmnode\n"));
2444 if (d && d->running) {
2445 int s = splhigh ();
2446 bd = d->board->sys;
2447 CX_LOCK (bd);
2448 cx_down (d);
2449 CX_UNLOCK (bd);
2450 splx (s);
2451 }
2452 #ifdef KLD_MODULE
2453 if (node->nd_flags & NGF_REALLY_DIE) {
2454 NG_NODE_SET_PRIVATE (node, NULL);
2455 NG_NODE_UNREF (node);
2456 }
2457 NG_NODE_REVIVE(node); /* Persistent node */
2458 #endif
2459 return 0;
2460 }
2461
2462 static int ng_cx_connect (hook_p hook)
2463 {
2464 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2465
2466 callout_reset (&d->timeout_handle, hz, cx_watchdog_timer, d);
2467 return 0;
2468 }
2469
2470 static int ng_cx_disconnect (hook_p hook)
2471 {
2472 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook));
2473 bdrv_t *bd = d->board->sys;
2474 int s;
2475
2476 s = splhigh ();
2477 CX_LOCK (bd);
2478 if (NG_HOOK_PRIVATE (hook))
2479 cx_down (d);
2480 CX_UNLOCK (bd);
2481 splx (s);
2482 /* If we were wait it than it reasserted now, just stop it. */
2483 if (!callout_drain (&d->timeout_handle))
2484 callout_stop (&d->timeout_handle);
2485 return 0;
2486 }
2487 #endif /*NETGRAPH*/
2488
2489 static int cx_modevent (module_t mod, int type, void *unused)
2490 {
2491 static int load_count = 0;
2492
2493 switch (type) {
2494 case MOD_LOAD:
2495 #ifdef NETGRAPH
2496 if (ng_newtype (&typestruct))
2497 printf ("Failed to register ng_cx\n");
2498 #endif
2499 ++load_count;
2500
2501 callout_init (&timeout_handle, 1);
2502 callout_reset (&timeout_handle, hz*5, cx_timeout, 0);
2503 /* Software interrupt. */
2504 swi_add(&tty_intr_event, "cx", cx_softintr, NULL, SWI_TTY,
2505 INTR_MPSAFE, &cx_fast_ih);
2506 break;
2507 case MOD_UNLOAD:
2508 if (load_count == 1) {
2509 printf ("Removing device entry for Sigma\n");
2510 #ifdef NETGRAPH
2511 ng_rmtype (&typestruct);
2512 #endif
2513 }
2514 /* If we were wait it than it reasserted now, just stop it. */
2515 if (!callout_drain (&timeout_handle))
2516 callout_stop (&timeout_handle);
2517 swi_remove (cx_fast_ih);
2518 --load_count;
2519 break;
2520 case MOD_SHUTDOWN:
2521 break;
2522 }
2523 return 0;
2524 }
2525
2526 #ifdef NETGRAPH
2527 static struct ng_type typestruct = {
2528 .version = NG_ABI_VERSION,
2529 .name = NG_CX_NODE_TYPE,
2530 .constructor = ng_cx_constructor,
2531 .rcvmsg = ng_cx_rcvmsg,
2532 .shutdown = ng_cx_rmnode,
2533 .newhook = ng_cx_newhook,
2534 .connect = ng_cx_connect,
2535 .rcvdata = ng_cx_rcvdata,
2536 .disconnect = ng_cx_disconnect,
2537 };
2538 #endif /*NETGRAPH*/
2539
2540 #ifdef NETGRAPH
2541 MODULE_DEPEND (ng_cx, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);
2542 #else
2543 MODULE_DEPEND (isa_cx, sppp, 1, 1, 1);
2544 #endif
2545 DRIVER_MODULE (cx, isa, cx_isa_driver, cx_devclass, cx_modevent, NULL);
2546 MODULE_VERSION (cx, 1);
Cache object: 4242dd37d2f3c9f914feaab7f5f254f3
|