if_cx.c revision 129027
1/* 2 * Cronyx-Sigma adapter driver for FreeBSD. 3 * Supports PPP/HDLC and Cisco/HDLC protocol in synchronous mode, 4 * and asyncronous 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.23 2004/02/26 17:56:40 rik Exp $ 23 */ 24#include <sys/cdefs.h> 25__FBSDID("$FreeBSD: head/sys/dev/cx/if_cx.c 129027 2004-05-07 11:11:13Z rik $"); 26 27#include <sys/param.h> 28 29#if __FreeBSD_version >= 500000 30# define NCX 1 31#else 32# include "cx.h" 33#endif 34 35#if NCX > 0 36#include <sys/systm.h> 37#include <sys/kernel.h> 38#include <sys/proc.h> 39#include <sys/mbuf.h> 40#include <sys/sockio.h> 41#include <sys/malloc.h> 42#include <sys/socket.h> 43#include <sys/conf.h> 44#include <sys/errno.h> 45#include <sys/tty.h> 46#if __FreeBSD_version >= 400000 47# include <sys/bus.h> 48# include <machine/bus.h> 49# include <sys/rman.h> 50# include <isa/isavar.h> 51#endif 52#include <sys/fcntl.h> 53#include <sys/interrupt.h> 54#include <vm/vm.h> 55#include <vm/pmap.h> 56#include <net/if.h> 57#include <machine/cpufunc.h> 58#include <machine/cserial.h> 59#include <machine/clock.h> 60#if __FreeBSD_version < 500000 61#include <machine/ipl.h> 62#include <i386/isa/isa_device.h> 63#endif 64#if __FreeBSD_version >= 400000 65# include <machine/resource.h> 66# if __FreeBSD_version <= 501000 67# include <i386/isa/intr_machdep.h> 68# endif 69#endif 70#if __FreeBSD_version >= 400000 71# include <dev/cx/machdep.h> 72# include <dev/cx/cxddk.h> 73# include <dev/cx/cronyxfw.h> 74#else 75# include <i386/isa/cronyx/machdep.h> 76# include <i386/isa/cronyx/cxddk.h> 77# include <i386/isa/cronyx/cronyxfw.h> 78#endif 79#include "opt_ng_cronyx.h" 80#ifdef NETGRAPH_CRONYX 81# include "opt_netgraph.h" 82# include <netgraph/ng_message.h> 83# include <netgraph/netgraph.h> 84# if __FreeBSD_version >= 400000 85# include <dev/cx/ng_cx.h> 86# else 87# include <netgraph/ng_cx.h> 88# endif 89#else 90# include <net/if_types.h> 91# if __FreeBSD_version < 500000 92# include "sppp.h" 93# if NSPPP <= 0 94# error The device cx requires sppp or netgraph. 95# endif 96# endif 97# include <net/if_sppp.h> 98# define PP_CISCO IFF_LINK2 99#if __FreeBSD_version < 400000 100# include <bpfilter.h> 101# if NBPFILTER > 0 102# include <net/bpf.h> 103# endif 104#else 105# if __FreeBSD_version < 500000 106# include <bpf.h> 107# endif 108# include <net/bpf.h> 109# define NBPFILTER NBPF 110#endif 111#endif 112 113#define CX_DEBUG(d,s) ({if (d->chan->debug) {\ 114 printf ("%s: ", d->name); printf s;}}) 115#define CX_DEBUG2(d,s) ({if (d->chan->debug>1) {\ 116 printf ("%s: ", d->name); printf s;}}) 117 118#define UNIT(d) (minor(d) & 0x3f) 119#define IF_CUNIT(d) (minor(d) & 0x40) 120#define UNIT_CTL 0x3f 121#define CALLOUT(d) (minor(d) & 0x80) 122#define CDEV_MAJOR 42 123 124typedef struct _async_q { 125 int beg; 126 int end; 127 #define BF_SZ 14400 128 int buf[BF_SZ+1]; 129} async_q; 130 131#define AQ_GSZ(q) ((BF_SZ + (q)->end - (q)->beg)%BF_SZ) 132#define AQ_PUSH(q,c) {*((q)->buf + (q)->end) = c;\ 133 (q)->end = ((q)->end + 1)%BF_SZ;} 134#define AQ_POP(q,c) {c = *((q)->buf + (q)->beg);\ 135 (q)->beg = ((q)->beg + 1)%BF_SZ;} 136 137#if __FreeBSD_version >= 400000 138static void cx_identify __P((driver_t *, device_t)); 139static int cx_probe __P((device_t)); 140static int cx_attach __P((device_t)); 141static int cx_detach __P((device_t)); 142 143static device_method_t cx_isa_methods [] = { 144 DEVMETHOD(device_identify, cx_identify), 145 DEVMETHOD(device_probe, cx_probe), 146 DEVMETHOD(device_attach, cx_attach), 147 DEVMETHOD(device_detach, cx_detach), 148 {0, 0} 149}; 150 151typedef struct _bdrv_t { 152 cx_board_t *board; 153 struct resource *base_res; 154 struct resource *drq_res; 155 struct resource *irq_res; 156 int base_rid; 157 int drq_rid; 158 int irq_rid; 159 void *intrhand; 160} bdrv_t; 161 162static driver_t cx_isa_driver = { 163 "cx", 164 cx_isa_methods, 165 sizeof (bdrv_t), 166}; 167 168static devclass_t cx_devclass; 169#endif 170 171typedef struct _drv_t { 172 char name [8]; 173 cx_chan_t *chan; 174 cx_board_t *board; 175 cx_buf_t buf; 176 struct tty tty; 177 struct callout_handle dcd_timeout_handle; 178 unsigned dtrwait; 179 unsigned dtroff; 180 unsigned callout; 181 unsigned lock; 182 int open_dev; 183 int cd; 184 int running; 185 struct callout_handle dtr_timeout_handle; 186#ifdef NETGRAPH 187 char nodename [NG_NODELEN+1]; 188 hook_p hook; 189 hook_p debug_hook; 190 node_p node; 191 struct ifqueue lo_queue; 192 struct ifqueue hi_queue; 193 short timeout; 194 struct callout_handle timeout_handle; 195#else 196 struct sppp pp; 197#endif 198#if __FreeBSD_version >= 400000 199 dev_t devt[3]; 200#endif 201 async_q aqueue; 202 #define CX_READ 1 203 #define CX_WRITE 2 204 int intr_action; 205 short atimeout; 206} drv_t; 207 208extern long csigma_fw_len; 209extern const char *csigma_fw_version; 210extern const char *csigma_fw_date; 211extern const char *csigma_fw_copyright; 212extern const cr_dat_tst_t csigma_fw_tvec[]; 213extern const u_char csigma_fw_data[]; 214static void cx_oproc (struct tty *tp); 215static int cx_param (struct tty *tp, struct termios *t); 216static void cx_stop (struct tty *tp, int flag); 217static void cx_dtrwakeup (void *a); 218static void cx_receive (cx_chan_t *c, char *data, int len); 219static void cx_transmit (cx_chan_t *c, void *attachment, int len); 220static void cx_error (cx_chan_t *c, int data); 221static void cx_modem (cx_chan_t *c); 222static void cx_up (drv_t *d); 223static void cx_start (drv_t *d); 224static void disc_optim(struct tty *tp, struct termios *t); 225#if __FreeBSD_version < 500000 226static swihand_t cx_softintr; 227#else 228static void cx_softintr (void *); 229static void *cx_fast_ih; 230#endif 231static void cx_down (drv_t *d); 232static void cx_watchdog (drv_t *d); 233static void cx_carrier (void *arg); 234 235#ifdef NETGRAPH 236extern struct ng_type typestruct; 237#else 238static void cx_ifstart (struct ifnet *ifp); 239static void cx_tlf (struct sppp *sp); 240static void cx_tls (struct sppp *sp); 241static void cx_ifwatchdog (struct ifnet *ifp); 242static int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data); 243static void cx_initialize (void *softc); 244#endif 245 246static cx_board_t *adapter [NCX]; 247static drv_t *channel [NCX*NCHAN]; 248static struct callout_handle led_timo [NCX]; 249static struct callout_handle timeout_handle; 250#if __FreeBSD_version >= 400000 251 extern struct cdevsw cx_cdevsw; 252#endif 253 254static int MY_SOFT_INTR; 255 256/* 257 * Print the mbuf chain, for debug purposes only. 258 */ 259static void printmbuf (struct mbuf *m) 260{ 261 printf ("mbuf:"); 262 for (; m; m=m->m_next) { 263 if (m->m_flags & M_PKTHDR) 264 printf (" HDR %d:", m->m_pkthdr.len); 265 if (m->m_flags & M_EXT) 266 printf (" EXT:"); 267 printf (" %d", m->m_len); 268 } 269 printf ("\n"); 270} 271 272/* 273 * Make an mbuf from data. 274 */ 275static struct mbuf *makembuf (void *buf, u_int len) 276{ 277 struct mbuf *m, *o, *p; 278 279 MGETHDR (m, M_DONTWAIT, MT_DATA); 280 281 if (! m) 282 return 0; 283 284 if (len >= MINCLSIZE) 285 MCLGET (m, M_DONTWAIT); 286 287 m->m_pkthdr.len = len; 288 m->m_len = 0; 289 290 p = m; 291 while (len) { 292 u_int n = M_TRAILINGSPACE (p); 293 if (n > len) 294 n = len; 295 if (! n) { 296 /* Allocate new mbuf. */ 297 o = p; 298 MGET (p, M_DONTWAIT, MT_DATA); 299 if (! p) { 300 m_freem (m); 301 return 0; 302 } 303 if (len >= MINCLSIZE) 304 MCLGET (p, M_DONTWAIT); 305 p->m_len = 0; 306 o->m_next = p; 307 308 n = M_TRAILINGSPACE (p); 309 if (n > len) 310 n = len; 311 } 312 bcopy (buf, mtod (p, caddr_t) + p->m_len, n); 313 314 p->m_len += n; 315 buf = n + (char*) buf; 316 len -= n; 317 } 318 return m; 319} 320 321/* 322 * Recover after lost transmit interrupts. 323 */ 324static void cx_timeout (void *arg) 325{ 326 drv_t *d; 327 int s, i; 328 329 for (i=0; i<NCX*NCHAN; ++i) { 330 d = channel[i]; 331 if (! d) 332 continue; 333 s = splhigh (); 334 if (d->atimeout == 1 && d->tty.t_state & TS_BUSY) { 335 d->tty.t_state &= ~TS_BUSY; 336 if (d->tty.t_dev) { 337 d->intr_action |= CX_WRITE; 338 MY_SOFT_INTR = 1; 339#if __FreeBSD_version >= 500000 340 swi_sched (cx_fast_ih, 0); 341#else 342 setsofttty (); 343#endif 344 } 345 CX_DEBUG (d, ("cx_timeout\n")); 346 } 347 if (d->atimeout) 348 d->atimeout--; 349 splx (s); 350 } 351 timeout_handle = timeout (cx_timeout, 0, hz*5); 352} 353 354static void cx_led_off (void *arg) 355{ 356 cx_board_t *b = arg; 357 int s = splhigh (); 358 359 cx_led (b, 0); 360 led_timo[b->num].callout = 0; 361 splx (s); 362} 363 364/* 365 * Activate interupt handler from DDK. 366 */ 367#if __FreeBSD_version >= 400000 368static void cx_intr (void *arg) 369{ 370 bdrv_t *bd = arg; 371 cx_board_t *b = bd->board; 372#else 373static void cx_intr (int bnum) 374{ 375 cx_board_t *b = adapter [bnum]; 376#endif 377 int s = splhigh (); 378 379 /* Turn LED on. */ 380 cx_led (b, 1); 381 382 cx_int_handler (b); 383 384 /* Turn LED off 50 msec later. */ 385 if (! led_timo[b->num].callout) 386 led_timo[b->num] = timeout (cx_led_off, b, hz/20); 387 splx (s); 388} 389 390static int probe_irq (cx_board_t *b, int irq) 391{ 392 int mask, busy, cnt; 393 394 /* Clear pending irq, if any. */ 395 cx_probe_irq (b, -irq); 396 DELAY (100); 397 for (cnt=0; cnt<5; ++cnt) { 398 /* Get the mask of pending irqs, assuming they are busy. 399 * Activate the adapter on given irq. */ 400 busy = cx_probe_irq (b, irq); 401 DELAY (100); 402 403 /* Get the mask of active irqs. 404 * Deactivate our irq. */ 405 mask = cx_probe_irq (b, -irq); 406 DELAY (100); 407 if ((mask & ~busy) == 1 << irq) { 408 cx_probe_irq (b, 0); 409 /* printf ("cx%d: irq %d ok, mask=0x%04x, busy=0x%04x\n", 410 b->num, irq, mask, busy); */ 411 return 1; 412 } 413 } 414 /* printf ("cx%d: irq %d not functional, mask=0x%04x, busy=0x%04x\n", 415 b->num, irq, mask, busy); */ 416 cx_probe_irq (b, 0); 417 return 0; 418} 419 420static short porttab [] = { 421 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0, 422 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0 423}; 424static char dmatab [] = { 7, 6, 5, 0 }; 425static char irqtab [] = { 5, 10, 11, 7, 3, 15, 12, 0 }; 426 427#if __FreeBSD_version >= 400000 428static int cx_is_free_res (device_t dev, int rid, int type, u_long start, 429 u_long end, u_long count) 430{ 431 struct resource *res; 432 433 if (!(res = bus_alloc_resource (dev, type, &rid, start, end, count, 434 RF_ALLOCATED))) 435 return 0; 436 437 bus_release_resource (dev, type, rid, res); 438 439 return 1; 440} 441 442static void cx_identify (driver_t *driver, device_t dev) 443{ 444 u_long iobase, rescount; 445 int devcount; 446 device_t *devices; 447 device_t child; 448 devclass_t my_devclass; 449 int i, k; 450 451 if ((my_devclass = devclass_find ("cx")) == NULL) 452 return; 453 454 devclass_get_devices (my_devclass, &devices, &devcount); 455 456 if (devcount == 0) { 457 /* We should find all devices by our self. We could alter other 458 * devices, but we don't have a choise 459 */ 460 for (i = 0; (iobase = porttab [i]) != 0; i++) { 461 if (!cx_is_free_res (dev, 1, SYS_RES_IOPORT, 462 iobase, iobase + NPORT, NPORT)) 463 continue; 464 if (cx_probe_board (iobase, -1, -1) == 0) 465 continue; 466 467 devcount++; 468 469 child = BUS_ADD_CHILD (dev, ISA_ORDER_SPECULATIVE, "cx", 470 -1); 471 472 if (child == NULL) 473 return; 474 475 device_set_desc_copy (child, "Cronyx Sigma"); 476 device_set_driver (child, driver); 477 bus_set_resource (child, SYS_RES_IOPORT, 0, 478 iobase, NPORT); 479 480 if (devcount >= NCX) 481 break; 482 } 483 } else { 484 static short porttab [] = { 485 0x200, 0x220, 0x240, 0x260, 0x280, 0x2a0, 0x2c0, 0x2e0, 486 0x300, 0x320, 0x340, 0x360, 0x380, 0x3a0, 0x3c0, 0x3e0, 0 487 }; 488 /* Lets check user choise. 489 */ 490 for (k = 0; k < devcount; k++) { 491 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0, 492 &iobase, &rescount) != 0) 493 continue; 494 495 for (i = 0; porttab [i] != 0; i++) { 496 if (porttab [i] != iobase) 497 continue; 498 if (!cx_is_free_res (devices[k], 1, SYS_RES_IOPORT, 499 iobase, iobase + NPORT, NPORT)) 500 continue; 501 if (cx_probe_board (iobase, -1, -1) == 0) 502 continue; 503 porttab [i] = -1; 504 device_set_desc_copy (devices[k], "Cronyx Sigma"); 505 break; 506 } 507 508 if (porttab [i] == 0) { 509 device_delete_child ( 510 device_get_parent (devices[k]), 511 devices [k]); 512 devices[k] = 0; 513 continue; 514 } 515 } 516 for (k = 0; k < devcount; k++) { 517 if (devices[k] == 0) 518 continue; 519 if (bus_get_resource (devices[k], SYS_RES_IOPORT, 0, 520 &iobase, &rescount) == 0) 521 continue; 522 for (i = 0; (iobase = porttab [i]) != 0; i++) { 523 if (porttab [i] == -1) { 524 continue; 525 } 526 if (!cx_is_free_res (devices[k], 1, SYS_RES_IOPORT, 527 iobase, iobase + NPORT, NPORT)) 528 continue; 529 if (cx_probe_board (iobase, -1, -1) == 0) 530 continue; 531 532 bus_set_resource (devices[k], SYS_RES_IOPORT, 0, 533 iobase, NPORT); 534 porttab [i] = -1; 535 device_set_desc_copy (devices[k], "Cronyx Sigma"); 536 break; 537 } 538 if (porttab [i] == 0) { 539 device_delete_child ( 540 device_get_parent (devices[k]), 541 devices [k]); 542 } 543 } 544 free (devices, M_TEMP); 545 } 546 547 return; 548} 549 550static int cx_probe (device_t dev) 551{ 552 int unit = device_get_unit (dev); 553 int i; 554 u_long iobase, rescount; 555 556 if (!device_get_desc (dev) || 557 strcmp (device_get_desc (dev), "Cronyx Sigma")) 558 return ENXIO; 559 560 if (bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount) != 0) { 561 printf ("cx%d: Couldn't get IOPORT\n", unit); 562 return ENXIO; 563 } 564 565 if (!cx_is_free_res (dev, 1, SYS_RES_IOPORT, 566 iobase, iobase + NPORT, NPORT)) { 567 printf ("cx%d: Resource IOPORT isn't free %lx\n", unit, iobase); 568 return ENXIO; 569 } 570 571 for (i = 0; porttab [i] != 0; i++) { 572 if (porttab [i] == iobase) { 573 porttab [i] = -1; 574 break; 575 } 576 } 577 578 if (porttab [i] == 0) { 579 return ENXIO; 580 } 581 582 if (!cx_probe_board (iobase, -1, -1)) { 583 printf ("cx%d: probing for Sigma at %lx faild\n", unit, iobase); 584 return ENXIO; 585 } 586 587 return 0; 588} 589#else /* __FreeBSD_version < 400000 */ 590static int cx_probe (struct isa_device *id) 591{ 592 cx_board_t *b; 593 int i; 594 595#ifndef NETGRAPH 596 if (! sppp_attach) { 597 printf ("cx%d: no synchronous PPP driver configured\n", 598 id->id_unit); 599 return 0; 600 } 601#endif 602 if (id->id_iobase < 0) { 603 /* Autodetect the adapter. */ 604 for (i=0; ; i++) { 605 if (! porttab[i]) { 606 id->id_iobase = -1; 607 return 0; 608 } 609 id->id_iobase = porttab[i]; 610 if (id->id_unit > 0 && adapter[0] && adapter[0]->port == id->id_iobase) 611 continue; 612 if (id->id_unit > 1 && adapter[1] && adapter[1]->port == id->id_iobase) 613 continue; 614 if (! haveseen_isadev (id, CC_IOADDR | CC_QUIET) && 615 cx_probe_board (id->id_iobase, -1, -1)) 616 break; 617 } 618 } else if (! cx_probe_board (id->id_iobase, -1, -1)) 619 return 0; 620 621 if (id->id_drq < 0) { 622 /* Find available 16-bit DRQ. */ 623 624 for (i=0; ; ++i) { 625 if (! dmatab[i]) { 626 printf ("cx%d: no available drq found\n", 627 id->id_unit); 628 id->id_drq = -1; 629 return 0; 630 } 631 id->id_drq = dmatab[i]; 632 if (! haveseen_isadev (id, CC_DRQ | CC_QUIET) 633 && !isa_dma_acquire (id->id_drq)) 634 break; 635 } 636 } 637 638 b = malloc (sizeof (cx_board_t), M_DEVBUF, M_WAITOK); 639 if (!b) { 640 printf ("cx:%d: Couldn't allocate memory\n", id->id_unit); 641 return (ENXIO); 642 } 643 adapter[id->id_unit] = b; 644 bzero (b, sizeof(cx_board_t)); 645 646 if (! cx_open_board (b, id->id_unit, id->id_iobase, 647 id->id_irq ? ffs (id->id_irq) - 1 : -1, id->id_drq)) { 648 printf ("cx%d: cannot initialize adapter\n", id->id_unit); 649 isa_dma_release (id->id_drq); 650 adapter[id->id_unit] = 0; 651 free (b, M_DEVBUF); 652 return 0; 653 } 654 655 if (id->id_irq) { 656 if (! probe_irq (b, ffs (id->id_irq) - 1)) 657 printf ("cx%d: irq %d not functional\n", 658 id->id_unit, ffs (id->id_irq) - 1); 659 } else { 660 /* Find available IRQ. */ 661 662 for (i=0; ; ++i) { 663 if (! irqtab[i]) { 664 printf ("cx%d: no available irq found\n", 665 id->id_unit); 666 id->id_irq = -1; 667 isa_dma_release (id->id_drq); 668 adapter[id->id_unit] = 0; 669 free (b, M_DEVBUF); 670 return 0; 671 } 672 id->id_irq = 1 << irqtab[i]; 673 if (haveseen_isadev (id, CC_IRQ | CC_QUIET)) 674 continue; 675#ifdef KLD_MODULE 676 if (register_intr (irqtab[i], 0, 0, (inthand2_t*) 677 cx_intr, &net_imask, id->id_unit) != 0) 678 continue; 679 unregister_intr (irqtab[i], (inthand2_t*) cx_intr); 680#endif 681 if (probe_irq (b, irqtab[i])) 682 break; 683 } 684 } 685 cx_init (b, b->num, b->port, ffs (id->id_irq) - 1, b->dma); 686 cx_setup_board (b, 0, 0, 0); 687 688 return 1; 689} 690#endif /* __FreeBSD_version < 400000 */ 691 692/* 693 * The adapter is present, initialize the driver structures. 694 */ 695#if __FreeBSD_version < 400000 696static int cx_attach (struct isa_device *id) 697{ 698#else 699static int cx_attach (device_t dev) 700{ 701 bdrv_t *bd = device_get_softc (dev); 702 u_long iobase, drq, irq, rescount; 703 int unit = device_get_unit (dev); 704 int i; 705 int s; 706#endif 707 cx_board_t *b; 708 cx_chan_t *c; 709 drv_t *d; 710 711#if __FreeBSD_version >= 400000 712 KASSERT ((bd != NULL), ("cx%d: NULL device softc\n", unit)); 713 714 bus_get_resource (dev, SYS_RES_IOPORT, 0, &iobase, &rescount); 715 bd->base_rid = 0; 716 bd->base_res = bus_alloc_resource (dev, SYS_RES_IOPORT, &bd->base_rid, 717 iobase, iobase + NPORT, NPORT, RF_ACTIVE); 718 if (! bd->base_res) { 719 printf ("cx%d: cannot allocate base address\n", unit); 720 return ENXIO; 721 } 722 723 if (bus_get_resource (dev, SYS_RES_DRQ, 0, &drq, &rescount) != 0) { 724 for (i = 0; (drq = dmatab [i]) != 0; i++) { 725 if (!cx_is_free_res (dev, 1, SYS_RES_DRQ, 726 drq, drq + 1, 1)) 727 continue; 728 bus_set_resource (dev, SYS_RES_DRQ, 0, drq, 1); 729 break; 730 } 731 732 if (dmatab[i] == 0) { 733 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, 734 bd->base_res); 735 printf ("cx%d: Couldn't get DRQ\n", unit); 736 return ENXIO; 737 } 738 } 739 740 bd->drq_rid = 0; 741 bd->drq_res = bus_alloc_resource (dev, SYS_RES_DRQ, &bd->drq_rid, 742 drq, drq + 1, 1, RF_ACTIVE); 743 if (! bd->drq_res) { 744 printf ("cx%d: cannot allocate drq\n", unit); 745 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, 746 bd->base_res); 747 return ENXIO; 748 } 749 750 if (bus_get_resource (dev, SYS_RES_IRQ, 0, &irq, &rescount) != 0) { 751 for (i = 0; (irq = irqtab [i]) != 0; i++) { 752 if (!cx_is_free_res (dev, 1, SYS_RES_IRQ, 753 irq, irq + 1, 1)) 754 continue; 755 bus_set_resource (dev, SYS_RES_IRQ, 0, irq, 1); 756 break; 757 } 758 759 if (irqtab[i] == 0) { 760 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, 761 bd->drq_res); 762 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, 763 bd->base_res); 764 printf ("cx%d: Couldn't get IRQ\n", unit); 765 return ENXIO; 766 } 767 } 768 769 bd->irq_rid = 0; 770 bd->irq_res = bus_alloc_resource (dev, SYS_RES_IRQ, &bd->irq_rid, 771 irq, irq + 1, 1, RF_ACTIVE); 772 if (! bd->irq_res) { 773 printf ("cx%d: Couldn't allocate irq\n", unit); 774 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, 775 bd->drq_res); 776 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, 777 bd->base_res); 778 return ENXIO; 779 } 780 781 b = malloc (sizeof (cx_board_t), M_DEVBUF, M_WAITOK); 782 if (!b) { 783 printf ("cx:%d: Couldn't allocate memory\n", unit); 784 return (ENXIO); 785 } 786 adapter[unit] = b; 787 bzero (b, sizeof(cx_board_t)); 788 789 if (! cx_open_board (b, unit, iobase, irq, drq)) { 790 printf ("cx%d: error loading firmware\n", unit); 791 free (b, M_DEVBUF); 792 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, 793 bd->irq_res); 794 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, 795 bd->drq_res); 796 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, 797 bd->base_res); 798 return ENXIO; 799 } 800 801 bd->board = b; 802 803 if (! probe_irq (b, irq)) { 804 printf ("cx%d: irq %ld not functional\n", unit, irq); 805 bd->board = 0; 806 adapter [unit] = 0; 807 free (b, M_DEVBUF); 808 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, 809 bd->irq_res); 810 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, 811 bd->drq_res); 812 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, 813 bd->base_res); 814 return ENXIO; 815 } 816 817 s = splhigh (); 818 if (bus_setup_intr (dev, bd->irq_res, INTR_TYPE_NET, cx_intr, bd, 819 &bd->intrhand)) { 820 printf ("cx%d: Can't setup irq %ld\n", unit, irq); 821 bd->board = 0; 822 adapter [unit] = 0; 823 free (b, M_DEVBUF); 824 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, 825 bd->irq_res); 826 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, 827 bd->drq_res); 828 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, 829 bd->base_res); 830 splx (s); 831 return ENXIO; 832 } 833 834 cx_init (b, b->num, b->port, irq, drq); 835 cx_setup_board (b, 0, 0, 0); 836#else /* __FreeBSD_version >= 400000 */ 837 b = adapter[id->id_unit]; 838#endif /* __FreeBSD_version >= 400000 */ 839 840 printf ("cx%d: <Cronyx-Sigma-%s>\n", b->num, b->name); 841#if __FreeBSD_version < 400000 842 id->id_ointr = cx_intr; 843#endif 844 845 for (c=b->chan; c<b->chan+NCHAN; ++c) { 846#if __FreeBSD_version >= 400000 847 char *dnmt="tty %x"; 848 char *dnmc="cua %x"; 849#endif 850 if (c->type == T_NONE) 851 continue; 852 d = contigmalloc (sizeof(drv_t), M_DEVBUF, M_WAITOK, 853 0x100000, 0x1000000, 16, 0); 854 channel [b->num*NCHAN + c->num] = d; 855 bzero (d, sizeof(drv_t)); 856 sprintf (d->name, "cx%d.%d", b->num, c->num); 857 d->board = b; 858 d->chan = c; 859 d->tty.t_oproc = cx_oproc; 860 d->tty.t_param = cx_param; 861#if __FreeBSD_version >= 400000 862 d->tty.t_stop = cx_stop; 863#endif 864 d->dtrwait = 3 * hz; /* Default DTR off timeout is 3 seconds. */ 865 d->open_dev = 0; 866 c->sys = d; 867 868 switch (c->type) { 869 case T_SYNC_RS232: 870 case T_SYNC_V35: 871 case T_SYNC_RS449: 872 case T_UNIV: 873 case T_UNIV_RS232: 874 case T_UNIV_RS449: 875 case T_UNIV_V35: 876#ifdef NETGRAPH 877 if (ng_make_node_common (&typestruct, &d->node) != 0) { 878 printf ("%s: cannot make common node\n", d->name); 879 channel [b->num*NCHAN + c->num] = 0; 880 c->sys = 0; 881#if __FreeBSD_version < 400000 882 free (d, M_DEVBUF); 883#else 884 contigfree (d, sizeof (*d), M_DEVBUF); 885#endif 886 continue; 887 } 888#if __FreeBSD_version >= 500000 889 NG_NODE_SET_PRIVATE (d->node, d); 890#else 891 d->node->private = d; 892#endif 893 sprintf (d->nodename, "%s%d", NG_CX_NODE_TYPE, 894 c->board->num*NCHAN + c->num); 895 if (ng_name_node (d->node, d->nodename)) { 896 printf ("%s: cannot name node\n", d->nodename); 897#if __FreeBSD_version >= 500000 898 NG_NODE_UNREF (d->node); 899#else 900 ng_rmnode (d->node); 901 ng_unref (d->node); 902#endif 903 channel [b->num*NCHAN + c->num] = 0; 904 c->sys = 0; 905#if __FreeBSD_version < 400000 906 free (d, M_DEVBUF); 907#else 908 contigfree (d, sizeof (*d), M_DEVBUF); 909#endif 910 continue; 911 } 912 d->lo_queue.ifq_maxlen = IFQ_MAXLEN; 913 d->hi_queue.ifq_maxlen = IFQ_MAXLEN; 914#if __FreeBSD_version >= 500000 915 mtx_init (&d->lo_queue.ifq_mtx, "cx_queue_lo", NULL, MTX_DEF); 916 mtx_init (&d->hi_queue.ifq_mtx, "cx_queue_hi", NULL, MTX_DEF); 917#endif 918#else /*NETGRAPH*/ 919 d->pp.pp_if.if_softc = d; 920#if __FreeBSD_version > 501000 921 if_initname (&d->pp.pp_if, "cx", b->num * NCHAN + c->num); 922#else 923 d->pp.pp_if.if_unit = b->num * NCHAN + c->num; 924 d->pp.pp_if.if_name = "cx"; 925#endif 926 d->pp.pp_if.if_mtu = PP_MTU; 927 d->pp.pp_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 928 d->pp.pp_if.if_ioctl = cx_sioctl; 929 d->pp.pp_if.if_start = cx_ifstart; 930 d->pp.pp_if.if_watchdog = cx_ifwatchdog; 931 d->pp.pp_if.if_init = cx_initialize; 932 sppp_attach (&d->pp.pp_if); 933 if_attach (&d->pp.pp_if); 934 d->pp.pp_tlf = cx_tlf; 935 d->pp.pp_tls = cx_tls; 936#if __FreeBSD_version >= 400000 || NBPFILTER > 0 937 /* If BPF is in the kernel, call the attach for it. 938 * Size of PPP header is 4 bytes. */ 939 bpfattach (&d->pp.pp_if, DLT_PPP, 4); 940#endif 941#endif /*NETGRAPH*/ 942 } 943 cx_start_chan (c, &d->buf, vtophys (&d->buf)); 944 cx_register_receive (c, &cx_receive); 945 cx_register_transmit (c, &cx_transmit); 946 cx_register_error (c, &cx_error); 947 cx_register_modem (c, &cx_modem); 948#if __FreeBSD_version >= 400000 949 dnmt[3] = 'x'+b->num; 950 dnmc[3] = 'x'+b->num; 951 d->devt[0] = make_dev (&cx_cdevsw, b->num*NCHAN + c->num, UID_ROOT, GID_WHEEL, 0644, dnmt, b->num*NCHAN + c->num); 952 d->devt[1] = make_dev (&cx_cdevsw, b->num*NCHAN + c->num + 64, UID_ROOT, GID_WHEEL, 0600, "cx%d", b->num*NCHAN + c->num); 953 d->devt[2] = make_dev (&cx_cdevsw, b->num*NCHAN + c->num + 128, UID_ROOT, GID_WHEEL, 0660, dnmc, b->num*NCHAN + c->num); 954 } 955 splx (s); 956 957 return 0; 958#else /* __FreeBSD_version < 400000 */ 959 } 960 961 return 1; 962#endif 963} 964 965#if __FreeBSD_version >= 400000 966static int cx_detach (device_t dev) 967{ 968 bdrv_t *bd = device_get_softc (dev); 969 cx_board_t *b = bd->board; 970 cx_chan_t *c; 971 int s = splhigh (); 972 973 /* Check if the device is busy (open). */ 974 for (c = b->chan; c < b->chan + NCHAN; ++c) { 975 drv_t *d = (drv_t*) c->sys; 976 977 if (!d || d->chan->type == T_NONE) 978 continue; 979 if (d->lock) { 980 splx (s); 981 return EBUSY; 982 } 983 if (c->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN) && 984 (d->open_dev|0x2)) { 985 splx (s); 986 return EBUSY; 987 } 988 if (d->running) { 989 splx (s); 990 return EBUSY; 991 } 992 } 993 994 /* Deactivate the timeout routine. And soft interrupt*/ 995 if (led_timo[b->num].callout) 996 untimeout (cx_led_off, b, led_timo[b->num]); 997 998 for (c = b->chan; c < b->chan + NCHAN; ++c) { 999 drv_t *d = c->sys; 1000 1001 if (!d || d->chan->type == T_NONE) 1002 continue; 1003 1004 if (d->dtr_timeout_handle.callout) 1005 untimeout (cx_dtrwakeup, d, d->dtr_timeout_handle); 1006 if (d->dcd_timeout_handle.callout) 1007 untimeout (cx_carrier, c, d->dcd_timeout_handle); 1008 } 1009 bus_teardown_intr (dev, bd->irq_res, bd->intrhand); 1010 bus_deactivate_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res); 1011 bus_release_resource (dev, SYS_RES_IRQ, bd->irq_rid, bd->irq_res); 1012 1013 bus_deactivate_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res); 1014 bus_release_resource (dev, SYS_RES_DRQ, bd->drq_rid, bd->drq_res); 1015 1016 bus_deactivate_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->irq_res); 1017 bus_release_resource (dev, SYS_RES_IOPORT, bd->base_rid, bd->base_res); 1018 1019 cx_close_board (b); 1020 1021 /* Detach the interfaces, free buffer memory. */ 1022 for (c = b->chan; c < b->chan + NCHAN; ++c) { 1023 drv_t *d = (drv_t*) c->sys; 1024 1025 if (!d || d->chan->type == T_NONE) 1026 continue; 1027#ifdef NETGRAPH 1028#if __FreeBSD_version >= 500000 1029 if (d->node) { 1030 ng_rmnode_self (d->node); 1031 NG_NODE_UNREF (d->node); 1032 d->node = NULL; 1033 } 1034 mtx_destroy (&d->lo_queue.ifq_mtx); 1035 mtx_destroy (&d->hi_queue.ifq_mtx); 1036#else 1037 ng_rmnode (d->node); 1038 d->node = NULL; 1039#endif 1040#else 1041#if __FreeBSD_version >= 410000 && NBPFILTER > 0 1042 /* Detach from the packet filter list of interfaces. */ 1043 bpfdetach (&d->pp.pp_if); 1044#endif 1045 /* Detach from the sync PPP list. */ 1046 sppp_detach (&d->pp.pp_if); 1047 1048 if_detach (&d->pp.pp_if); 1049#endif 1050 destroy_dev (d->devt[0]); 1051 destroy_dev (d->devt[1]); 1052 destroy_dev (d->devt[2]); 1053 } 1054 1055 cx_led_off (b); 1056 if (led_timo[b->num].callout) 1057 untimeout (cx_led_off, b, led_timo[b->num]); 1058 splx (s); 1059 1060 s = splhigh (); 1061 for (c = b->chan; c < b->chan + NCHAN; ++c) { 1062 drv_t *d = (drv_t*) c->sys; 1063 1064 if (!d || d->chan->type == T_NONE) 1065 continue; 1066 1067 /* Deallocate buffers. */ 1068 contigfree (d, sizeof (*d), M_DEVBUF); 1069 } 1070 bd->board = 0; 1071 adapter [b->num] = 0; 1072 free (b, M_DEVBUF); 1073 splx (s); 1074 1075 return 0; 1076} 1077#endif 1078 1079#ifndef NETGRAPH 1080static void cx_ifstart (struct ifnet *ifp) 1081{ 1082 drv_t *d = ifp->if_softc; 1083 1084 cx_start (d); 1085} 1086 1087static void cx_ifwatchdog (struct ifnet *ifp) 1088{ 1089 drv_t *d = ifp->if_softc; 1090 1091 cx_watchdog (d); 1092} 1093 1094static void cx_tlf (struct sppp *sp) 1095{ 1096 drv_t *d = sp->pp_if.if_softc; 1097 1098 CX_DEBUG (d, ("cx_tlf\n")); 1099/* cx_set_dtr (d->chan, 0);*/ 1100/* cx_set_rts (d->chan, 0);*/ 1101 sp->pp_down (sp); 1102} 1103 1104static void cx_tls (struct sppp *sp) 1105{ 1106 drv_t *d = sp->pp_if.if_softc; 1107 1108 CX_DEBUG (d, ("cx_tls\n")); 1109 sp->pp_up (sp); 1110} 1111 1112/* 1113 * Initialization of interface. 1114 * It seems to be never called by upper level. 1115 */ 1116static void cx_initialize (void *softc) 1117{ 1118 drv_t *d = softc; 1119 1120 CX_DEBUG (d, ("cx_initialize\n")); 1121} 1122 1123/* 1124 * Process an ioctl request. 1125 */ 1126static int cx_sioctl (struct ifnet *ifp, u_long cmd, caddr_t data) 1127{ 1128 drv_t *d = ifp->if_softc; 1129 int error, s, was_up, should_be_up; 1130 1131 /* No socket ioctls while the channel is in async mode. */ 1132 if (d->chan->type == T_NONE || d->chan->mode == M_ASYNC) 1133 return EBUSY; 1134 1135 /* Socket ioctls on slave subchannels are not allowed. */ 1136 was_up = (ifp->if_flags & IFF_RUNNING) != 0; 1137 error = sppp_ioctl (ifp, cmd, data); 1138 if (error) 1139 return error; 1140 1141 if (! (ifp->if_flags & IFF_DEBUG)) 1142 d->chan->debug = 0; 1143 else if (! d->chan->debug) 1144 d->chan->debug = 1; 1145 1146 switch (cmd) { 1147 default: CX_DEBUG2 (d, ("ioctl 0x%lx\n", cmd)); return 0; 1148 case SIOCADDMULTI: CX_DEBUG2 (d, ("SIOCADDMULTI\n")); return 0; 1149 case SIOCDELMULTI: CX_DEBUG2 (d, ("SIOCDELMULTI\n")); return 0; 1150 case SIOCSIFFLAGS: CX_DEBUG2 (d, ("SIOCSIFFLAGS\n")); break; 1151 case SIOCSIFADDR: CX_DEBUG2 (d, ("SIOCSIFADDR\n")); break; 1152 } 1153 1154 /* We get here only in case of SIFFLAGS or SIFADDR. */ 1155 s = splhigh (); 1156 should_be_up = (ifp->if_flags & IFF_RUNNING) != 0; 1157 if (!was_up && should_be_up) { 1158 /* Interface goes up -- start it. */ 1159 cx_up (d); 1160 cx_start (d); 1161 } else if (was_up && !should_be_up) { 1162 /* Interface is going down -- stop it. */ 1163 /* if ((d->pp.pp_flags & PP_FR) || (ifp->if_flags & PP_CISCO))*/ 1164 cx_down (d); 1165 } 1166 splx (s); 1167 return 0; 1168} 1169#endif /*NETGRAPH*/ 1170 1171/* 1172 * Stop the interface. Called on splimp(). 1173 */ 1174static void cx_down (drv_t *d) 1175{ 1176 int s = splhigh (); 1177 CX_DEBUG (d, ("cx_down\n")); 1178 cx_set_dtr (d->chan, 0); 1179 cx_set_rts (d->chan, 0); 1180 d->running = 0; 1181 splx (s); 1182} 1183 1184/* 1185 * Start the interface. Called on splimp(). 1186 */ 1187static void cx_up (drv_t *d) 1188{ 1189 int s = splhigh (); 1190 CX_DEBUG (d, ("cx_up\n")); 1191 cx_set_dtr (d->chan, 1); 1192 cx_set_rts (d->chan, 1); 1193 d->running = 1; 1194 splx (s); 1195} 1196 1197/* 1198 * Start output on the (slave) interface. Get another datagram to send 1199 * off of the interface queue, and copy it to the interface 1200 * before starting the output. 1201 */ 1202static void cx_send (drv_t *d) 1203{ 1204 struct mbuf *m; 1205 u_short len; 1206 1207 CX_DEBUG2 (d, ("cx_send\n")); 1208 1209 /* No output if the interface is down. */ 1210 if (! d->running) 1211 return; 1212 1213 /* No output if the modem is off. */ 1214 if (! cx_get_dsr (d->chan) && ! cx_get_loop(d->chan)) 1215 return; 1216 1217 if (cx_buf_free (d->chan)) { 1218 /* Get the packet to send. */ 1219#ifdef NETGRAPH 1220 IF_DEQUEUE (&d->hi_queue, m); 1221 if (! m) 1222 IF_DEQUEUE (&d->lo_queue, m); 1223#else 1224 m = sppp_dequeue (&d->pp.pp_if); 1225#endif 1226 if (! m) 1227 return; 1228#if (__FreeBSD_version >= 400000 || NBPFILTER > 0) && !defined (NETGRAPH) 1229 if (d->pp.pp_if.if_bpf) 1230#if __FreeBSD_version >= 500000 1231 BPF_MTAP (&d->pp.pp_if, m); 1232#else 1233 bpf_mtap (&d->pp.pp_if, m); 1234#endif 1235#endif 1236 len = m->m_pkthdr.len; 1237 if (! m->m_next) 1238 cx_send_packet (d->chan, (u_char*)mtod (m, caddr_t), 1239 len, 0); 1240 else { 1241 u_char buf [DMABUFSZ]; 1242 m_copydata (m, 0, len, buf); 1243 cx_send_packet (d->chan, buf, len, 0); 1244 } 1245 m_freem (m); 1246 1247 /* Set up transmit timeout, 10 seconds. */ 1248#ifdef NETGRAPH 1249 d->timeout = 10; 1250#else 1251 d->pp.pp_if.if_timer = 10; 1252#endif 1253 } 1254#ifndef NETGRAPH 1255 d->pp.pp_if.if_flags |= IFF_OACTIVE; 1256#endif 1257} 1258 1259/* 1260 * Start output on the interface. 1261 * Always called on splimp(). 1262 */ 1263static void cx_start (drv_t *d) 1264{ 1265 int s = splhigh (); 1266 if (d->running) { 1267 if (! d->chan->dtr) 1268 cx_set_dtr (d->chan, 1); 1269 if (! d->chan->rts) 1270 cx_set_rts (d->chan, 1); 1271 cx_send (d); 1272 } 1273 splx (s); 1274} 1275 1276/* 1277 * Handle transmit timeouts. 1278 * Recover after lost transmit interrupts. 1279 * Always called on splimp(). 1280 */ 1281static void cx_watchdog (drv_t *d) 1282{ 1283 int s = splhigh (); 1284 CX_DEBUG (d, ("device timeout\n")); 1285 if (d->running) { 1286 cx_setup_chan (d->chan); 1287 cx_start_chan (d->chan, 0, 0); 1288 cx_set_dtr (d->chan, 1); 1289 cx_set_rts (d->chan, 1); 1290 cx_start (d); 1291 } 1292 splx (s); 1293} 1294 1295/* 1296 * Transmit callback function. 1297 */ 1298static void cx_transmit (cx_chan_t *c, void *attachment, int len) 1299{ 1300 drv_t *d = c->sys; 1301 1302 if (!d) 1303 return; 1304 1305 if (c->mode == M_ASYNC) { 1306 d->tty.t_state &= ~(TS_BUSY | TS_FLUSH); 1307 d->atimeout = 0; 1308 if (d->tty.t_dev) { 1309 d->intr_action |= CX_WRITE; 1310 MY_SOFT_INTR = 1; 1311#if __FreeBSD_version >= 500000 1312 swi_sched (cx_fast_ih, 0); 1313#else 1314 setsofttty (); 1315#endif 1316 } 1317 return; 1318 } 1319#ifdef NETGRAPH 1320 d->timeout = 0; 1321#else 1322 ++d->pp.pp_if.if_opackets; 1323 d->pp.pp_if.if_flags &= ~IFF_OACTIVE; 1324 d->pp.pp_if.if_timer = 0; 1325#endif 1326 cx_start (d); 1327} 1328 1329/* 1330 * Process the received packet. 1331 */ 1332static void cx_receive (cx_chan_t *c, char *data, int len) 1333{ 1334 drv_t *d = c->sys; 1335 struct mbuf *m; 1336 char *cc = data; 1337#if __FreeBSD_version >= 500000 && defined NETGRAPH 1338 int error; 1339#endif 1340 1341 if (!d) 1342 return; 1343 1344 if (c->mode == M_ASYNC) { 1345 if (d->tty.t_state & TS_ISOPEN) { 1346 async_q *q = &d->aqueue; 1347 int size = BF_SZ - 1 - AQ_GSZ (q); 1348 1349 if (len <= 0 && !size) 1350 return; 1351 1352 if (len > size) { 1353 c->ierrs++; 1354 cx_error (c, CX_OVERRUN); 1355 len = size - 1; 1356 } 1357 1358 while (len--) { 1359 AQ_PUSH (q, *(unsigned char *)cc); 1360 cc++; 1361 } 1362 1363 d->intr_action |= CX_READ; 1364 MY_SOFT_INTR = 1; 1365#if __FreeBSD_version >= 500000 1366 swi_sched (cx_fast_ih, 0); 1367#else 1368 setsofttty (); 1369#endif 1370 } 1371 return; 1372 } 1373 if (! d->running) 1374 return; 1375 1376 m = makembuf (data, len); 1377 if (! m) { 1378 CX_DEBUG (d, ("no memory for packet\n")); 1379#ifndef NETGRAPH 1380 ++d->pp.pp_if.if_iqdrops; 1381#endif 1382 return; 1383 } 1384 if (c->debug > 1) 1385 printmbuf (m); 1386#ifdef NETGRAPH 1387 m->m_pkthdr.rcvif = 0; 1388#if __FreeBSD_version >= 500000 1389 NG_SEND_DATA_ONLY (error, d->hook, m); 1390#else 1391 ng_queue_data (d->hook, m, 0); 1392#endif 1393#else 1394 ++d->pp.pp_if.if_ipackets; 1395 m->m_pkthdr.rcvif = &d->pp.pp_if; 1396#if __FreeBSD_version >= 400000 || NBPFILTER > 0 1397 /* Check if there's a BPF listener on this interface. 1398 * If so, hand off the raw packet to bpf. */ 1399 if (d->pp.pp_if.if_bpf) 1400#if __FreeBSD_version >= 500000 1401 BPF_TAP (&d->pp.pp_if, data, len); 1402#else 1403 bpf_tap (&d->pp.pp_if, data, len); 1404#endif 1405#endif 1406 sppp_input (&d->pp.pp_if, m); 1407#endif 1408} 1409 1410#define CONDITION(t,tp) (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))\ 1411 && (!(tp->t_iflag & BRKINT) || (tp->t_iflag & IGNBRK))\ 1412 && (!(tp->t_iflag & PARMRK)\ 1413 || (tp->t_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))\ 1414 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))\ 1415 && linesw[tp->t_line].l_rint == ttyinput) 1416 1417/* 1418 * Error callback function. 1419 */ 1420static void cx_error (cx_chan_t *c, int data) 1421{ 1422 drv_t *d = c->sys; 1423 async_q *q; 1424 1425 if (!d) 1426 return; 1427 1428 q = &(d->aqueue); 1429 1430 switch (data) { 1431 case CX_FRAME: 1432 CX_DEBUG (d, ("frame error\n")); 1433 if (c->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN) 1434 && (AQ_GSZ (q) < BF_SZ - 1) 1435 && (!CONDITION((&d->tty.t_termios), (&d->tty)) 1436 || !(d->tty.t_iflag & (IGNPAR | PARMRK)))) { 1437 AQ_PUSH (q, TTY_FE); 1438 d->intr_action |= CX_READ; 1439 MY_SOFT_INTR = 1; 1440#if __FreeBSD_version >= 500000 1441 swi_sched (cx_fast_ih, 0); 1442#else 1443 setsofttty (); 1444#endif 1445 } 1446#ifndef NETGRAPH 1447 else 1448 ++d->pp.pp_if.if_ierrors; 1449#endif 1450 break; 1451 case CX_CRC: 1452 CX_DEBUG (d, ("crc error\n")); 1453 if (c->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN) 1454 && (AQ_GSZ (q) < BF_SZ - 1) 1455 && (!CONDITION((&d->tty.t_termios), (&d->tty)) 1456 || !(d->tty.t_iflag & INPCK) 1457 || !(d->tty.t_iflag & (IGNPAR | PARMRK)))) { 1458 AQ_PUSH (q, TTY_PE); 1459 d->intr_action |= CX_READ; 1460 MY_SOFT_INTR = 1; 1461#if __FreeBSD_version >= 500000 1462 swi_sched (cx_fast_ih, 0); 1463#else 1464 setsofttty (); 1465#endif 1466 } 1467#ifndef NETGRAPH 1468 else 1469 ++d->pp.pp_if.if_ierrors; 1470#endif 1471 break; 1472 case CX_OVERRUN: 1473 CX_DEBUG (d, ("overrun error\n")); 1474#ifdef TTY_OE 1475 if (c->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN) 1476 && (AQ_GSZ (q) < BF_SZ - 1) 1477 && (!CONDITION((&d->tty.t_termios), (&d->tty)))) { 1478 AQ_PUSH (q, TTY_OE); 1479 d->intr_action |= CX_READ; 1480 MY_SOFT_INTR = 1; 1481#if __FreeBSD_version >= 500000 1482 swi_sched (cx_fast_ih, 0); 1483#else 1484 setsofttty (); 1485#endif 1486 } 1487#endif 1488#ifndef NETGRAPH 1489 else { 1490 ++d->pp.pp_if.if_collisions; 1491 ++d->pp.pp_if.if_ierrors; 1492 } 1493#endif 1494 break; 1495 case CX_OVERFLOW: 1496 CX_DEBUG (d, ("overflow error\n")); 1497#ifndef NETGRAPH 1498 if (c->mode != M_ASYNC) 1499 ++d->pp.pp_if.if_ierrors; 1500#endif 1501 break; 1502 case CX_UNDERRUN: 1503 CX_DEBUG (d, ("underrun error\n")); 1504 if (c->mode != M_ASYNC) { 1505#ifdef NETGRAPH 1506 d->timeout = 0; 1507#else 1508 ++d->pp.pp_if.if_oerrors; 1509 d->pp.pp_if.if_flags &= ~IFF_OACTIVE; 1510 d->pp.pp_if.if_timer = 0; 1511 cx_start (d); 1512#endif 1513 } 1514 break; 1515 case CX_BREAK: 1516 CX_DEBUG (d, ("break error\n")); 1517 if (c->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN) 1518 && (AQ_GSZ (q) < BF_SZ - 1) 1519 && (!CONDITION((&d->tty.t_termios), (&d->tty)) 1520 || !(d->tty.t_iflag & (IGNBRK | BRKINT | PARMRK)))) { 1521 AQ_PUSH (q, TTY_BI); 1522 d->intr_action |= CX_READ; 1523 MY_SOFT_INTR = 1; 1524#if __FreeBSD_version >= 500000 1525 swi_sched (cx_fast_ih, 0); 1526#else 1527 setsofttty (); 1528#endif 1529 } 1530#ifndef NETGRAPH 1531 else 1532 ++d->pp.pp_if.if_ierrors; 1533#endif 1534 break; 1535 default: 1536 CX_DEBUG (d, ("error #%d\n", data)); 1537 } 1538} 1539 1540#if __FreeBSD_version < 500000 1541static int cx_open (dev_t dev, int flag, int mode, struct proc *p) 1542#else 1543static int cx_open (dev_t dev, int flag, int mode, struct thread *td) 1544#endif 1545{ 1546 int unit = UNIT (dev); 1547 drv_t *d; 1548 int error; 1549 1550 if (unit >= NCX*NCHAN || ! (d = channel[unit])) 1551 return ENXIO; 1552 CX_DEBUG2 (d, ("cx_open unit=%d, flag=0x%x, mode=0x%x\n", 1553 unit, flag, mode)); 1554 1555 if (d->chan->mode != M_ASYNC || IF_CUNIT(dev)) { 1556 d->open_dev |= 0x1; 1557 return 0; 1558 } 1559#if __FreeBSD_version >= 400000 1560 dev->si_tty = &d->tty; 1561#endif 1562 d->tty.t_dev = dev; 1563again: 1564 if (d->dtroff) { 1565 error = tsleep (&d->dtrwait, TTIPRI | PCATCH, "cxdtr", 0); 1566 if (error) 1567 return error; 1568 goto again; 1569 } 1570 1571 if ((d->tty.t_state & TS_ISOPEN) && (d->tty.t_state & TS_XCLUDE) && 1572#if __FreeBSD_version >= 500000 1573 suser (td)) 1574#else 1575 p->p_ucred->cr_uid != 0) 1576#endif 1577 return EBUSY; 1578 1579 if (d->tty.t_state & TS_ISOPEN) { 1580 /* 1581 * Cannot open /dev/cua if /dev/tty already opened. 1582 */ 1583 if (CALLOUT (dev) && ! d->callout) 1584 return EBUSY; 1585 1586 /* 1587 * Opening /dev/tty when /dev/cua is already opened. 1588 * Wait for close, then try again. 1589 */ 1590 if (! CALLOUT (dev) && d->callout) { 1591 if (flag & O_NONBLOCK) 1592 return EBUSY; 1593 error = tsleep (d, TTIPRI | PCATCH, "cxbi", 0); 1594 if (error) 1595 return error; 1596 goto again; 1597 } 1598 } else if (d->lock && ! CALLOUT (dev) && (flag & O_NONBLOCK)) 1599 /* 1600 * We try to open /dev/tty in non-blocking mode 1601 * while somebody is already waiting for carrier on it. 1602 */ 1603 return EBUSY; 1604 else { 1605 ttychars (&d->tty); 1606 if (d->tty.t_ispeed == 0) { 1607 d->tty.t_iflag = 0; 1608 d->tty.t_oflag = 0; 1609 d->tty.t_lflag = 0; 1610 d->tty.t_cflag = CREAD | CS8 | HUPCL; 1611 d->tty.t_ispeed = d->chan->rxbaud; 1612 d->tty.t_ospeed = d->chan->txbaud; 1613 } 1614 if (CALLOUT (dev)) 1615 d->tty.t_cflag |= CLOCAL; 1616 else 1617 d->tty.t_cflag &= ~CLOCAL; 1618 cx_param (&d->tty, &d->tty.t_termios); 1619 ttsetwater (&d->tty); 1620 } 1621 1622 splhigh (); 1623 if (! (d->tty.t_state & TS_ISOPEN)) { 1624 cx_start_chan (d->chan, 0, 0); 1625 cx_set_dtr (d->chan, 1); 1626 cx_set_rts (d->chan, 1); 1627 d->cd = cx_get_cd (d->chan); 1628 if (CALLOUT (dev) || cx_get_cd (d->chan)) 1629 (*linesw[d->tty.t_line].l_modem) (&d->tty, 1); 1630 } 1631 1632 if (! (flag & O_NONBLOCK) && ! (d->tty.t_cflag & CLOCAL) && 1633 ! (d->tty.t_state & TS_CARR_ON)) { 1634 /* Lock the channel against cxconfig while we are 1635 * waiting for carrier. */ 1636 d->lock++; 1637 error = tsleep (&d->tty.t_rawq, TTIPRI | PCATCH, "cxdcd", 0); 1638 /* Unlock the channel. */ 1639 d->lock--; 1640 spl0 (); 1641 if (error) 1642 goto failed; 1643 goto again; 1644 } 1645 1646 error = (*linesw[d->tty.t_line].l_open) (dev, &d->tty); 1647 disc_optim (&d->tty, &d->tty.t_termios); 1648 spl0 (); 1649 if (error) { 1650failed: if (! (d->tty.t_state & TS_ISOPEN)) { 1651 splhigh (); 1652 cx_set_dtr (d->chan, 0); 1653 cx_set_rts (d->chan, 0); 1654 if (d->dtrwait) { 1655 d->dtr_timeout_handle = 1656 timeout (cx_dtrwakeup, d, d->dtrwait); 1657 d->dtroff = 1; 1658 } 1659 spl0 (); 1660 } 1661 return error; 1662 } 1663 1664 if (d->tty.t_state & TS_ISOPEN) 1665 d->callout = CALLOUT (dev) ? 1 : 0; 1666 1667 d->open_dev |= 0x2; 1668 CX_DEBUG2 (d, ("cx_open done\n")); 1669 return 0; 1670} 1671 1672#if __FreeBSD_version < 500000 1673static int cx_close (dev_t dev, int flag, int mode, struct proc *p) 1674#else 1675static int cx_close (dev_t dev, int flag, int mode, struct thread *td) 1676#endif 1677{ 1678 drv_t *d = channel [UNIT (dev)]; 1679 int s; 1680 1681 CX_DEBUG2 (d, ("cx_close\n")); 1682 if ((!(d->open_dev&0x2)) || IF_CUNIT(dev)){ 1683 d->open_dev &= ~0x1; 1684 return 0; 1685 } 1686 s = splhigh (); 1687 (*linesw[d->tty.t_line].l_close) (&d->tty, flag); 1688 disc_optim (&d->tty, &d->tty.t_termios); 1689 1690 /* Disable receiver. 1691 * Transmitter continues sending the queued data. */ 1692 cx_enable_receive (d->chan, 0); 1693 1694 /* Clear DTR and RTS. */ 1695 if ((d->tty.t_cflag & HUPCL) || ! (d->tty.t_state & TS_ISOPEN)) { 1696 cx_set_dtr (d->chan, 0); 1697 cx_set_rts (d->chan, 0); 1698 if (d->dtrwait) { 1699 d->dtr_timeout_handle = 1700 timeout (cx_dtrwakeup, d, d->dtrwait); 1701 d->dtroff = 1; 1702 } 1703 } 1704 ttyclose (&d->tty); 1705 splx (s); 1706 d->callout = 0; 1707 1708 /* Wake up bidirectional opens. */ 1709 wakeup (d); 1710 d->open_dev &= ~0x2; 1711 1712 return 0; 1713} 1714 1715static int cx_read (dev_t dev, struct uio *uio, int flag) 1716{ 1717 drv_t *d = channel [UNIT (dev)]; 1718 1719 if (d) CX_DEBUG2 (d, ("cx_read\n")); 1720 if (!d || d->chan->mode != M_ASYNC || IF_CUNIT(dev)) 1721 return EBADF; 1722 1723 return (*linesw[d->tty.t_line].l_read) (&d->tty, uio, flag); 1724} 1725 1726static int cx_write (dev_t dev, struct uio *uio, int flag) 1727{ 1728 drv_t *d = channel [UNIT (dev)]; 1729 1730 if (d) CX_DEBUG2 (d, ("cx_write\n")); 1731 if (!d || d->chan->mode != M_ASYNC || IF_CUNIT(dev)) 1732 return EBADF; 1733 1734 return (*linesw[d->tty.t_line].l_write) (&d->tty, uio, flag); 1735} 1736 1737static int cx_modem_status (drv_t *d) 1738{ 1739 int status = 0, s = splhigh (); 1740 /* Already opened by someone or network interface is up? */ 1741 if ((d->chan->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN) && 1742 (d->open_dev|0x2)) || (d->chan->mode != M_ASYNC && d->running)) 1743 status = TIOCM_LE; /* always enabled while open */ 1744 1745 if (cx_get_dsr (d->chan)) status |= TIOCM_DSR; 1746 if (cx_get_cd (d->chan)) status |= TIOCM_CD; 1747 if (cx_get_cts (d->chan)) status |= TIOCM_CTS; 1748 if (d->chan->dtr) status |= TIOCM_DTR; 1749 if (d->chan->rts) status |= TIOCM_RTS; 1750 splx (s); 1751 return status; 1752} 1753 1754#if __FreeBSD_version < 500000 1755static int cx_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 1756#else 1757static int cx_ioctl (dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td) 1758#endif 1759{ 1760 drv_t *d = channel [UNIT (dev)]; 1761 cx_chan_t *c; 1762 struct serial_statistics *st; 1763 int error, s; 1764 char mask[16]; 1765 1766 if (!d || !(c = d->chan)) 1767 return EINVAL; 1768 1769 switch (cmd) { 1770 case SERIAL_GETREGISTERED: 1771 CX_DEBUG2 (d, ("ioctl: getregistered\n")); 1772 bzero (mask, sizeof(mask)); 1773 for (s=0; s<NCX*NCHAN; ++s) 1774 if (channel [s]) 1775 mask [s/8] |= 1 << (s & 7); 1776 bcopy (mask, data, sizeof (mask)); 1777 return 0; 1778 1779 case SERIAL_GETPORT: 1780 CX_DEBUG2 (d, ("ioctl: getport\n")); 1781 s = splhigh (); 1782 *(int *)data = cx_get_port (c); 1783 splx (s); 1784 if (*(int *)data<0) 1785 return (EINVAL); 1786 else 1787 return 0; 1788 1789 case SERIAL_SETPORT: 1790 CX_DEBUG2 (d, ("ioctl: setproto\n")); 1791 /* Only for superuser! */ 1792#if __FreeBSD_version < 400000 1793 error = suser (p->p_ucred, &p->p_acflag); 1794#elif __FreeBSD_version < 500000 1795 error = suser (p); 1796#else /* __FreeBSD_version >= 500000 */ 1797 error = suser (td); 1798#endif /* __FreeBSD_version >= 500000 */ 1799 if (error) 1800 return error; 1801 1802 s = splhigh (); 1803 cx_set_port (c, *(int *)data); 1804 splx (s); 1805 return 0; 1806 1807#ifndef NETGRAPH 1808 case SERIAL_GETPROTO: 1809 CX_DEBUG2 (d, ("ioctl: getproto\n")); 1810 s = splhigh (); 1811 strcpy ((char*)data, (c->mode == M_ASYNC) ? "async" : 1812 /*(d->pp.pp_flags & PP_FR) ? "fr" :*/ 1813 (d->pp.pp_if.if_flags & PP_CISCO) ? "cisco" : "ppp"); 1814 splx (s); 1815 return 0; 1816 1817 case SERIAL_SETPROTO: 1818 CX_DEBUG2 (d, ("ioctl: setproto\n")); 1819 /* Only for superuser! */ 1820#if __FreeBSD_version < 400000 1821 error = suser (p->p_ucred, &p->p_acflag); 1822#elif __FreeBSD_version < 500000 1823 error = suser (p); 1824#else /* __FreeBSD_version >= 500000 */ 1825 error = suser (td); 1826#endif /* __FreeBSD_version >= 500000 */ 1827 if (error) 1828 return error; 1829 if (c->mode == M_ASYNC) 1830 return EBUSY; 1831 if (d->pp.pp_if.if_flags & IFF_RUNNING) 1832 return EBUSY; 1833 if (! strcmp ("cisco", (char*)data)) { 1834/* d->pp.pp_flags &= ~(PP_FR);*/ 1835 d->pp.pp_flags |= PP_KEEPALIVE; 1836 d->pp.pp_if.if_flags |= PP_CISCO; 1837/* } else if (! strcmp ("fr", (char*)data)) {*/ 1838/* d->pp.pp_if.if_flags &= ~(PP_CISCO);*/ 1839/* d->pp.pp_flags |= PP_FR | PP_KEEPALIVE;*/ 1840 } else if (! strcmp ("ppp", (char*)data)) { 1841 d->pp.pp_flags &= ~(/*PP_FR |*/ PP_KEEPALIVE); 1842 d->pp.pp_if.if_flags &= ~(PP_CISCO); 1843 } else 1844 return EINVAL; 1845 return 0; 1846 1847 case SERIAL_GETKEEPALIVE: 1848 CX_DEBUG2 (d, ("ioctl: getkeepalive\n")); 1849 if (/*(d->pp.pp_flags & PP_FR) ||*/ 1850 (d->pp.pp_if.if_flags & PP_CISCO) || 1851 (c->mode == M_ASYNC)) 1852 return EINVAL; 1853 s = splhigh (); 1854 *(int*)data = (d->pp.pp_flags & PP_KEEPALIVE) ? 1 : 0; 1855 splx (s); 1856 return 0; 1857 1858 case SERIAL_SETKEEPALIVE: 1859 CX_DEBUG2 (d, ("ioctl: setkeepalive\n")); 1860 /* Only for superuser! */ 1861#if __FreeBSD_version < 400000 1862 error = suser (p->p_ucred, &p->p_acflag); 1863#elif __FreeBSD_version < 500000 1864 error = suser (p); 1865#else /* __FreeBSD_version >= 500000 */ 1866 error = suser (td); 1867#endif /* __FreeBSD_version >= 500000 */ 1868 if (error) 1869 return error; 1870 if (/*(d->pp.pp_flags & PP_FR) ||*/ 1871 (d->pp.pp_if.if_flags & PP_CISCO)) 1872 return EINVAL; 1873 s = splhigh (); 1874 if (*(int*)data) 1875 d->pp.pp_flags |= PP_KEEPALIVE; 1876 else 1877 d->pp.pp_flags &= ~PP_KEEPALIVE; 1878 splx (s); 1879 return 0; 1880#endif /*NETGRAPH*/ 1881 1882 case SERIAL_GETMODE: 1883 CX_DEBUG2 (d, ("ioctl: getmode\n")); 1884 s = splhigh (); 1885 *(int*)data = (c->mode == M_ASYNC) ? 1886 SERIAL_ASYNC : SERIAL_HDLC; 1887 splx (s); 1888 return 0; 1889 1890 case SERIAL_SETMODE: 1891 CX_DEBUG2 (d, ("ioctl: setmode\n")); 1892 /* Only for superuser! */ 1893#if __FreeBSD_version < 400000 1894 error = suser (p->p_ucred, &p->p_acflag); 1895#elif __FreeBSD_version < 500000 1896 error = suser (p); 1897#else /* __FreeBSD_version >= 500000 */ 1898 error = suser (td); 1899#endif /* __FreeBSD_version >= 500000 */ 1900 if (error) 1901 return error; 1902 1903 /* Somebody is waiting for carrier? */ 1904 if (d->lock) 1905 return EBUSY; 1906 /* /dev/ttyXX is already opened by someone? */ 1907 if (c->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN) && 1908 (d->open_dev|0x2)) 1909 return EBUSY; 1910 /* Network interface is up? 1911 * Cannot change to async mode. */ 1912 if (c->mode != M_ASYNC && d->running && 1913 (*(int*)data == SERIAL_ASYNC)) 1914 return EBUSY; 1915 1916 s = splhigh (); 1917 if (c->mode == M_HDLC && *(int*)data == SERIAL_ASYNC) { 1918 cx_set_mode (c, M_ASYNC); 1919 cx_enable_receive (c, 0); 1920 cx_enable_transmit (c, 0); 1921 } else if (c->mode == M_ASYNC && *(int*)data == SERIAL_HDLC) { 1922 cx_set_mode (c, M_HDLC); 1923 cx_enable_receive (c, 1); 1924 cx_enable_transmit (c, 1); 1925 } 1926 splx (s); 1927 return 0; 1928 1929 case SERIAL_GETSTAT: 1930 CX_DEBUG2 (d, ("ioctl: getestat\n")); 1931 st = (struct serial_statistics*) data; 1932 s = splhigh (); 1933 st->rintr = c->rintr; 1934 st->tintr = c->tintr; 1935 st->mintr = c->mintr; 1936 st->ibytes = c->ibytes; 1937 st->ipkts = c->ipkts; 1938 st->ierrs = c->ierrs; 1939 st->obytes = c->obytes; 1940 st->opkts = c->opkts; 1941 st->oerrs = c->oerrs; 1942 splx (s); 1943 return 0; 1944 1945 case SERIAL_CLRSTAT: 1946 CX_DEBUG2 (d, ("ioctl: clrstat\n")); 1947 /* Only for superuser! */ 1948#if __FreeBSD_version < 400000 1949 error = suser (p->p_ucred, &p->p_acflag); 1950#elif __FreeBSD_version < 500000 1951 error = suser (p); 1952#else /* __FreeBSD_version >= 500000 */ 1953 error = suser (td); 1954#endif /* __FreeBSD_version >= 500000 */ 1955 if (error) 1956 return error; 1957 s = splhigh (); 1958 c->rintr = 0; 1959 c->tintr = 0; 1960 c->mintr = 0; 1961 c->ibytes = 0; 1962 c->ipkts = 0; 1963 c->ierrs = 0; 1964 c->obytes = 0; 1965 c->opkts = 0; 1966 c->oerrs = 0; 1967 splx (s); 1968 return 0; 1969 1970 case SERIAL_GETBAUD: 1971 CX_DEBUG2 (d, ("ioctl: getbaud\n")); 1972 if (c->mode == M_ASYNC) 1973 return EINVAL; 1974 s = splhigh (); 1975 *(long*)data = cx_get_baud(c); 1976 splx (s); 1977 return 0; 1978 1979 case SERIAL_SETBAUD: 1980 CX_DEBUG2 (d, ("ioctl: setbaud\n")); 1981 /* Only for superuser! */ 1982#if __FreeBSD_version < 400000 1983 error = suser (p->p_ucred, &p->p_acflag); 1984#elif __FreeBSD_version < 500000 1985 error = suser (p); 1986#else /* __FreeBSD_version >= 500000 */ 1987 error = suser (td); 1988#endif /* __FreeBSD_version >= 500000 */ 1989 if (error) 1990 return error; 1991 if (c->mode == M_ASYNC) 1992 return EINVAL; 1993 s = splhigh (); 1994 cx_set_baud (c, *(long*)data); 1995 splx (s); 1996 return 0; 1997 1998 case SERIAL_GETLOOP: 1999 CX_DEBUG2 (d, ("ioctl: getloop\n")); 2000 if (c->mode == M_ASYNC) 2001 return EINVAL; 2002 s = splhigh (); 2003 *(int*)data = cx_get_loop (c); 2004 splx (s); 2005 return 0; 2006 2007 case SERIAL_SETLOOP: 2008 CX_DEBUG2 (d, ("ioctl: setloop\n")); 2009 /* Only for superuser! */ 2010#if __FreeBSD_version < 400000 2011 error = suser (p->p_ucred, &p->p_acflag); 2012#elif __FreeBSD_version < 500000 2013 error = suser (p); 2014#else /* __FreeBSD_version >= 500000 */ 2015 error = suser (td); 2016#endif /* __FreeBSD_version >= 500000 */ 2017 if (error) 2018 return error; 2019 if (c->mode == M_ASYNC) 2020 return EINVAL; 2021 s = splhigh (); 2022 cx_set_loop (c, *(int*)data); 2023 splx (s); 2024 return 0; 2025 2026 case SERIAL_GETDPLL: 2027 CX_DEBUG2 (d, ("ioctl: getdpll\n")); 2028 if (c->mode == M_ASYNC) 2029 return EINVAL; 2030 s = splhigh (); 2031 *(int*)data = cx_get_dpll (c); 2032 splx (s); 2033 return 0; 2034 2035 case SERIAL_SETDPLL: 2036 CX_DEBUG2 (d, ("ioctl: setdpll\n")); 2037 /* Only for superuser! */ 2038#if __FreeBSD_version < 400000 2039 error = suser (p->p_ucred, &p->p_acflag); 2040#elif __FreeBSD_version < 500000 2041 error = suser (p); 2042#else /* __FreeBSD_version >= 500000 */ 2043 error = suser (td); 2044#endif /* __FreeBSD_version >= 500000 */ 2045 if (error) 2046 return error; 2047 if (c->mode == M_ASYNC) 2048 return EINVAL; 2049 s = splhigh (); 2050 cx_set_dpll (c, *(int*)data); 2051 splx (s); 2052 return 0; 2053 2054 case SERIAL_GETNRZI: 2055 CX_DEBUG2 (d, ("ioctl: getnrzi\n")); 2056 if (c->mode == M_ASYNC) 2057 return EINVAL; 2058 s = splhigh (); 2059 *(int*)data = cx_get_nrzi (c); 2060 splx (s); 2061 return 0; 2062 2063 case SERIAL_SETNRZI: 2064 CX_DEBUG2 (d, ("ioctl: setnrzi\n")); 2065 /* Only for superuser! */ 2066#if __FreeBSD_version < 400000 2067 error = suser (p->p_ucred, &p->p_acflag); 2068#elif __FreeBSD_version < 500000 2069 error = suser (p); 2070#else /* __FreeBSD_version >= 500000 */ 2071 error = suser (td); 2072#endif /* __FreeBSD_version >= 500000 */ 2073 if (error) 2074 return error; 2075 if (c->mode == M_ASYNC) 2076 return EINVAL; 2077 s = splhigh (); 2078 cx_set_nrzi (c, *(int*)data); 2079 splx (s); 2080 return 0; 2081 2082 case SERIAL_GETDEBUG: 2083 CX_DEBUG2 (d, ("ioctl: getdebug\n")); 2084 s = splhigh (); 2085 *(int*)data = c->debug; 2086 splx (s); 2087 return 0; 2088 2089 case SERIAL_SETDEBUG: 2090 CX_DEBUG2 (d, ("ioctl: setdebug\n")); 2091 /* Only for superuser! */ 2092#if __FreeBSD_version < 400000 2093 error = suser (p->p_ucred, &p->p_acflag); 2094#elif __FreeBSD_version < 500000 2095 error = suser (p); 2096#else /* __FreeBSD_version >= 500000 */ 2097 error = suser (td); 2098#endif /* __FreeBSD_version >= 500000 */ 2099 if (error) 2100 return error; 2101 s = splhigh (); 2102 c->debug = *(int*)data; 2103 splx (s); 2104#ifndef NETGRAPH 2105 if (d->chan->debug) 2106 d->pp.pp_if.if_flags |= IFF_DEBUG; 2107 else 2108 d->pp.pp_if.if_flags &= (~IFF_DEBUG); 2109#endif 2110 return 0; 2111 } 2112 2113 if (c->mode == M_ASYNC) { 2114#if __FreeBSD_version >= 500000 2115 error = (*linesw[d->tty.t_line].l_ioctl) (&d->tty, cmd, data, flag, td); 2116#else 2117 error = (*linesw[d->tty.t_line].l_ioctl) (&d->tty, cmd, data, flag, p); 2118#endif 2119 disc_optim (&d->tty, &d->tty.t_termios); 2120 if (error != ENOIOCTL) { 2121 if (error) 2122 CX_DEBUG2 (d, ("l_ioctl: 0x%lx, error %d\n", cmd, error)); 2123 return error; 2124 } 2125 error = ttioctl (&d->tty, cmd, data, flag); 2126 disc_optim (&d->tty, &d->tty.t_termios); 2127 if (error != ENOIOCTL) { 2128 if (error) 2129 CX_DEBUG2 (d, ("ttioctl: 0x%lx, error %d\n", cmd, error)); 2130 return error; 2131 } 2132 } 2133 2134 switch (cmd) { 2135 case TIOCSBRK: /* Start sending line break */ 2136 CX_DEBUG2 (d, ("ioctl: tiocsbrk\n")); 2137 s = splhigh (); 2138 cx_send_break (c, 500); 2139 splx (s); 2140 return 0; 2141 2142 case TIOCCBRK: /* Stop sending line break */ 2143 CX_DEBUG2 (d, ("ioctl: tioccbrk\n")); 2144 return 0; 2145 2146 case TIOCSDTR: /* Set DTR */ 2147 CX_DEBUG2 (d, ("ioctl: tiocsdtr\n")); 2148 s = splhigh (); 2149 cx_set_dtr (c, 1); 2150 splx (s); 2151 return 0; 2152 2153 case TIOCCDTR: /* Clear DTR */ 2154 CX_DEBUG2 (d, ("ioctl: tioccdtr\n")); 2155 s = splhigh (); 2156 cx_set_dtr (c, 0); 2157 splx (s); 2158 return 0; 2159 2160 case TIOCMSET: /* Set DTR/RTS */ 2161 CX_DEBUG2 (d, ("ioctl: tiocmset\n")); 2162 s = splhigh (); 2163 cx_set_dtr (c, (*(int*)data & TIOCM_DTR) ? 1 : 0); 2164 cx_set_rts (c, (*(int*)data & TIOCM_RTS) ? 1 : 0); 2165 splx (s); 2166 return 0; 2167 2168 case TIOCMBIS: /* Add DTR/RTS */ 2169 CX_DEBUG2 (d, ("ioctl: tiocmbis\n")); 2170 s = splhigh (); 2171 if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 1); 2172 if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 1); 2173 splx (s); 2174 return 0; 2175 2176 case TIOCMBIC: /* Clear DTR/RTS */ 2177 CX_DEBUG2 (d, ("ioctl: tiocmbic\n")); 2178 s = splhigh (); 2179 if (*(int*)data & TIOCM_DTR) cx_set_dtr (c, 0); 2180 if (*(int*)data & TIOCM_RTS) cx_set_rts (c, 0); 2181 splx (s); 2182 return 0; 2183 2184 case TIOCMGET: /* Get modem status */ 2185 CX_DEBUG2 (d, ("ioctl: tiocmget\n")); 2186 *(int*)data = cx_modem_status (d); 2187 return 0; 2188 2189#ifdef TIOCMSDTRWAIT 2190 case TIOCMSDTRWAIT: 2191 CX_DEBUG2 (d, ("ioctl: tiocmsdtrwait\n")); 2192 /* Only for superuser! */ 2193#if __FreeBSD_version < 400000 2194 error = suser (p->p_ucred, &p->p_acflag); 2195#elif __FreeBSD_version < 500000 2196 error = suser (p); 2197#else /* __FreeBSD_version >= 500000 */ 2198 error = suser (td); 2199#endif /* __FreeBSD_version >= 500000 */ 2200 if (error) 2201 return error; 2202 s = splhigh (); 2203 d->dtrwait = *(int*)data * hz / 100; 2204 splx (s); 2205 return 0; 2206#endif 2207 2208#ifdef TIOCMGDTRWAIT 2209 case TIOCMGDTRWAIT: 2210 CX_DEBUG2 (d, ("ioctl: tiocmgdtrwait\n")); 2211 s = splhigh (); 2212 *(int*)data = d->dtrwait * 100 / hz; 2213 splx (s); 2214 return 0; 2215#endif 2216 } 2217 CX_DEBUG2 (d, ("ioctl: 0x%lx\n", cmd)); 2218 return ENOTTY; 2219} 2220 2221/* 2222 * Wake up opens() waiting for DTR ready. 2223 */ 2224static void cx_dtrwakeup (void *arg) 2225{ 2226 drv_t *d = arg; 2227 2228 d->dtroff = 0; 2229 wakeup (&d->dtrwait); 2230} 2231 2232 2233static void 2234disc_optim(tp, t) 2235 struct tty *tp; 2236 struct termios *t; 2237{ 2238 if (CONDITION(t,tp)) 2239 tp->t_state |= TS_CAN_BYPASS_L_RINT; 2240 else 2241 tp->t_state &= ~TS_CAN_BYPASS_L_RINT; 2242} 2243 2244#if __FreeBSD_version >= 500000 2245void cx_softintr (void *unused) 2246#else 2247void cx_softintr () 2248#endif 2249{ 2250 drv_t *d; 2251 async_q *q; 2252 int i, s, ic, k; 2253 while (MY_SOFT_INTR) { 2254 MY_SOFT_INTR = 0; 2255 for (i=0; i<NCX*NCHAN; ++i) { 2256 d = channel [i]; 2257 if (!d || !d->chan || d->chan->type == T_NONE 2258 || d->chan->mode != M_ASYNC || !d->tty.t_dev) 2259 continue; 2260 s = splhigh (); 2261 if (d->intr_action & CX_READ) { 2262 q = &(d->aqueue); 2263 if (d->tty.t_state & TS_CAN_BYPASS_L_RINT) { 2264 k = AQ_GSZ(q); 2265 if (d->tty.t_rawq.c_cc + k > 2266 d->tty.t_ihiwat 2267 && (d->tty.t_cflag & CRTS_IFLOW 2268 || d->tty.t_iflag & IXOFF) 2269 && !(d->tty.t_state & TS_TBLOCK)) 2270 ttyblock(&d->tty); 2271 d->tty.t_rawcc += k; 2272 while (k>0) { 2273 k--; 2274 AQ_POP (q, ic); 2275 splx (s); 2276 putc (ic, &d->tty.t_rawq); 2277 s = splhigh (); 2278 } 2279 ttwakeup(&d->tty); 2280 if (d->tty.t_state & TS_TTSTOP 2281 && (d->tty.t_iflag & IXANY 2282 || d->tty.t_cc[VSTART] == 2283 d->tty.t_cc[VSTOP])) { 2284 d->tty.t_state &= ~TS_TTSTOP; 2285 d->tty.t_lflag &= ~FLUSHO; 2286 d->intr_action |= CX_WRITE; 2287 } 2288 } else { 2289 while (q->end != q->beg) { 2290 AQ_POP (q, ic); 2291 splx (s); 2292 (*linesw[d->tty.t_line].l_rint) 2293 (ic, &d->tty); 2294 s = splhigh (); 2295 } 2296 } 2297 d->intr_action &= ~CX_READ; 2298 } 2299 splx (s); 2300 2301 s = splhigh (); 2302 if (d->intr_action & CX_WRITE) { 2303 if (d->tty.t_line) 2304 (*linesw[d->tty.t_line].l_start) (&d->tty); 2305 else 2306 cx_oproc (&d->tty); 2307 d->intr_action &= ~CX_WRITE; 2308 } 2309 splx (s); 2310 2311 } 2312 } 2313} 2314 2315/* 2316 * Fill transmitter buffer with data. 2317 */ 2318static void cx_oproc (struct tty *tp) 2319{ 2320 int s = splhigh (), k; 2321 drv_t *d = channel [UNIT (tp->t_dev)]; 2322 static u_char buf[DMABUFSZ]; 2323 u_char *p; 2324 u_short len = 0, sublen = 0; 2325 2326 if (!d) { 2327 splx (s); 2328 return; 2329 } 2330 2331 CX_DEBUG2 (d, ("cx_oproc\n")); 2332 if (tp->t_cflag & CRTSCTS && (tp->t_state & TS_TBLOCK) && d->chan->rts) 2333 cx_set_rts (d->chan, 0); 2334 else if (tp->t_cflag & CRTSCTS && ! (tp->t_state & TS_TBLOCK) && ! d->chan->rts) 2335 cx_set_rts (d->chan, 1); 2336 2337 if (! (tp->t_state & (TS_TIMEOUT | TS_TTSTOP))) { 2338 /* Start transmitter. */ 2339 cx_enable_transmit (d->chan, 1); 2340 2341 /* Is it busy? */ 2342 if (! cx_buf_free (d->chan)) { 2343 tp->t_state |= TS_BUSY; 2344 splx (s); 2345 return; 2346 } 2347 if (tp->t_iflag & IXOFF) { 2348 p = (buf + (DMABUFSZ/2)); 2349 sublen = q_to_b (&tp->t_outq, p, (DMABUFSZ/2)); 2350 k = sublen; 2351 while (k--) { 2352 /* Send XON/XOFF out of band. */ 2353 if (*p == tp->t_cc[VSTOP]) { 2354 cx_xflow_ctl (d->chan, 0); 2355 p++; 2356 continue; 2357 } 2358 if (*p == tp->t_cc[VSTART]) { 2359 cx_xflow_ctl (d->chan, 1); 2360 p++; 2361 continue; 2362 } 2363 buf[len] = *p; 2364 len++; 2365 p++; 2366 } 2367 } else { 2368 p = buf; 2369 len = q_to_b (&tp->t_outq, p, (DMABUFSZ/2)); 2370 } 2371 if (len) { 2372 cx_send_packet (d->chan, buf, len, 0); 2373 tp->t_state |= TS_BUSY; 2374 d->atimeout = 10; 2375 CX_DEBUG2 (d, ("out %d bytes\n", len)); 2376 } 2377 } 2378 ttwwakeup (tp); 2379 splx (s); 2380} 2381 2382static int cx_param (struct tty *tp, struct termios *t) 2383{ 2384 drv_t *d = channel [UNIT (tp->t_dev)]; 2385 int s, bits, parity; 2386 2387 if (!d) 2388 return EINVAL; 2389 2390 s = splhigh (); 2391 if (t->c_ospeed == 0) { 2392 /* Clear DTR and RTS. */ 2393 cx_set_dtr (d->chan, 0); 2394 splx (s); 2395 CX_DEBUG2 (d, ("cx_param (hangup)\n")); 2396 return 0; 2397 } 2398 CX_DEBUG2 (d, ("cx_param\n")); 2399 2400 /* Check requested parameters. */ 2401 if (t->c_ospeed < 300 || t->c_ospeed > 256*1024) { 2402 splx (s); 2403 return EINVAL; 2404 } 2405 if (t->c_ispeed && (t->c_ispeed < 300 || t->c_ispeed > 256*1024)) { 2406 splx (s); 2407 return EINVAL; 2408 } 2409 2410 /* And copy them to tty and channel structures. */ 2411 tp->t_ispeed = t->c_ispeed = tp->t_ospeed = t->c_ospeed; 2412 tp->t_cflag = t->c_cflag; 2413 2414 /* Set character length and parity mode. */ 2415 switch (t->c_cflag & CSIZE) { 2416 default: 2417 case CS8: bits = 8; break; 2418 case CS7: bits = 7; break; 2419 case CS6: bits = 6; break; 2420 case CS5: bits = 5; break; 2421 } 2422 2423 parity = ((t->c_cflag & PARENB) ? 1 : 0) * 2424 (1 + ((t->c_cflag & PARODD) ? 0 : 1)); 2425 2426 /* Set current channel number. */ 2427 if (! d->chan->dtr) 2428 cx_set_dtr (d->chan, 1); 2429 2430 disc_optim (&d->tty, &d->tty.t_termios); 2431 cx_set_async_param (d->chan, t->c_ospeed, bits, parity, (t->c_cflag & CSTOPB), 2432 !(t->c_cflag & PARENB), (t->c_cflag & CRTSCTS), 2433 (t->c_iflag & IXON), (t->c_iflag & IXANY), 2434 t->c_cc[VSTART], t->c_cc[VSTOP]); 2435 splx (s); 2436 return 0; 2437} 2438 2439#if __FreeBSD_version < 400000 2440static struct tty *cx_devtotty (dev_t dev) 2441{ 2442 int unit = UNIT (dev); 2443 2444 if (unit == UNIT_CTL || unit >= NCX*NCHAN || ! channel[unit]) 2445 return 0; 2446 return &channel[unit]->tty; 2447} 2448#endif 2449 2450/* 2451 * Stop output on a line 2452 */ 2453static void cx_stop (struct tty *tp, int flag) 2454{ 2455 drv_t *d = channel [UNIT (tp->t_dev)]; 2456 int s; 2457 2458 if (!d) 2459 return; 2460 2461 s = splhigh (); 2462 2463 if (tp->t_state & TS_BUSY) { 2464 /* Stop transmitter */ 2465 CX_DEBUG2 (d, ("cx_stop\n")); 2466 cx_transmitter_ctl (d->chan, 0); 2467 } 2468 splx (s); 2469} 2470 2471/* 2472 * Process the (delayed) carrier signal setup. 2473 */ 2474static void cx_carrier (void *arg) 2475{ 2476 drv_t *d = arg; 2477 cx_chan_t *c = d->chan; 2478 int s, cd; 2479 2480 s = splhigh (); 2481 cd = cx_get_cd (c); 2482 if (d->cd != cd) { 2483 if (cd) { 2484 CX_DEBUG (d, ("carrier on\n")); 2485 d->cd = 1; 2486 splx (s); 2487 (*linesw[d->tty.t_line].l_modem) (&d->tty, 1); 2488 } else { 2489 CX_DEBUG (d, ("carrier loss\n")); 2490 d->cd = 0; 2491 splx (s); 2492 (*linesw[d->tty.t_line].l_modem) (&d->tty, 0); 2493 } 2494 } 2495} 2496 2497/* 2498 * Modem signal callback function. 2499 */ 2500static void cx_modem (cx_chan_t *c) 2501{ 2502 drv_t *d = c->sys; 2503 2504 if (!d || c->mode != M_ASYNC) 2505 return; 2506 /* Handle carrier detect/loss. */ 2507 untimeout (cx_carrier, c, d->dcd_timeout_handle); 2508 /* Carrier changed - delay processing DCD for a while 2509 * to give both sides some time to initialize. */ 2510 d->dcd_timeout_handle = timeout (cx_carrier, d, hz/2); 2511} 2512 2513#if __FreeBSD_version < 400000 2514struct isa_driver cxdriver = { cx_probe, cx_attach, "cx" }; 2515static struct cdevsw cx_cdevsw = { 2516 cx_open, cx_close, cx_read, cx_write, 2517 cx_ioctl, cx_stop, noreset, cx_devtotty, 2518 ttpoll, nommap, NULL, "cx", 2519 NULL, -1, 2520}; 2521#elif __FreeBSD_version < 500000 2522static struct cdevsw cx_cdevsw = { 2523 cx_open, cx_close, cx_read, cx_write, 2524 cx_ioctl, ttypoll, nommap, nostrategy, 2525 "cx", CDEV_MAJOR, nodump, nopsize, 2526 D_TTY, -1 2527}; 2528#elif __FreeBSD_version == 500000 2529static struct cdevsw cx_cdevsw = { 2530 cx_open, cx_close, cx_read, cx_write, 2531 cx_ioctl, ttypoll, nommap, nostrategy, 2532 "cx", CDEV_MAJOR, nodump, nopsize, 2533 D_TTY, 2534 }; 2535#elif __FreeBSD_version <= 501000 2536static struct cdevsw cx_cdevsw = { 2537 .d_open = cx_open, 2538 .d_close = cx_close, 2539 .d_read = cx_read, 2540 .d_write = cx_write, 2541 .d_ioctl = cx_ioctl, 2542 .d_poll = ttypoll, 2543 .d_mmap = nommap, 2544 .d_strategy = nostrategy, 2545 .d_name = "cx", 2546 .d_maj = CDEV_MAJOR, 2547 .d_dump = nodump, 2548 .d_flags = D_TTY, 2549}; 2550#elif __FreeBSD_version < 502103 2551static struct cdevsw cx_cdevsw = { 2552 .d_open = cx_open, 2553 .d_close = cx_close, 2554 .d_read = cx_read, 2555 .d_write = cx_write, 2556 .d_ioctl = cx_ioctl, 2557 .d_poll = ttypoll, 2558 .d_name = "cx", 2559 .d_maj = CDEV_MAJOR, 2560 .d_flags = D_TTY, 2561}; 2562#else /* __FreeBSD_version >= 502103 */ 2563static struct cdevsw cx_cdevsw = { 2564 .d_version = D_VERSION, 2565 .d_open = cx_open, 2566 .d_close = cx_close, 2567 .d_read = cx_read, 2568 .d_write = cx_write, 2569 .d_ioctl = cx_ioctl, 2570 .d_name = "cx", 2571 .d_maj = CDEV_MAJOR, 2572 .d_flags = D_TTY | D_NEEDGIANT, 2573}; 2574#endif 2575 2576#ifdef NETGRAPH 2577#if __FreeBSD_version >= 500000 2578static int ng_cx_constructor (node_p node) 2579{ 2580 drv_t *d = NG_NODE_PRIVATE (node); 2581#else 2582static int ng_cx_constructor (node_p *node) 2583{ 2584 drv_t *d = (*node)->private; 2585#endif 2586 CX_DEBUG (d, ("Constructor\n")); 2587 return EINVAL; 2588} 2589 2590static int ng_cx_newhook (node_p node, hook_p hook, const char *name) 2591{ 2592 int s; 2593#if __FreeBSD_version >= 500000 2594 drv_t *d = NG_NODE_PRIVATE (node); 2595#else 2596 drv_t *d = node->private; 2597#endif 2598 2599 if (d->chan->mode == M_ASYNC) 2600 return EINVAL; 2601 2602 /* Attach debug hook */ 2603 if (strcmp (name, NG_CX_HOOK_DEBUG) == 0) { 2604#if __FreeBSD_version >= 500000 2605 NG_HOOK_SET_PRIVATE (hook, NULL); 2606#else 2607 hook->private = 0; 2608#endif 2609 d->debug_hook = hook; 2610 return 0; 2611 } 2612 2613 /* Check for raw hook */ 2614 if (strcmp (name, NG_CX_HOOK_RAW) != 0) 2615 return EINVAL; 2616 2617#if __FreeBSD_version >= 500000 2618 NG_HOOK_SET_PRIVATE (hook, d); 2619#else 2620 hook->private = d; 2621#endif 2622 d->hook = hook; 2623 s = splhigh (); 2624 cx_up (d); 2625 splx (s); 2626 return 0; 2627} 2628 2629static int print_modems (char *s, cx_chan_t *c, int need_header) 2630{ 2631 int status = cx_modem_status (c->sys); 2632 int length = 0; 2633 2634 if (need_header) 2635 length += sprintf (s + length, " LE DTR DSR RTS CTS CD\n"); 2636 length += sprintf (s + length, "%4s %4s %4s %4s %4s %4s\n", 2637 status & TIOCM_LE ? "On" : "-", 2638 status & TIOCM_DTR ? "On" : "-", 2639 status & TIOCM_DSR ? "On" : "-", 2640 status & TIOCM_RTS ? "On" : "-", 2641 status & TIOCM_CTS ? "On" : "-", 2642 status & TIOCM_CD ? "On" : "-"); 2643 return length; 2644} 2645 2646static int print_stats (char *s, cx_chan_t *c, int need_header) 2647{ 2648 int length = 0; 2649 2650 if (need_header) 2651 length += sprintf (s + length, " Rintr Tintr Mintr Ibytes Ipkts Ierrs Obytes Opkts Oerrs\n"); 2652 length += sprintf (s + length, "%7ld %7ld %7ld %8ld %7ld %7ld %8ld %7ld %7ld\n", 2653 c->rintr, c->tintr, c->mintr, c->ibytes, c->ipkts, 2654 c->ierrs, c->obytes, c->opkts, c->oerrs); 2655 return length; 2656} 2657 2658static int print_chan (char *s, cx_chan_t *c) 2659{ 2660 drv_t *d = c->sys; 2661 int length = 0; 2662 2663 length += sprintf (s + length, "cx%d", c->board->num * NCHAN + c->num); 2664 if (d->chan->debug) 2665 length += sprintf (s + length, " debug=%d", d->chan->debug); 2666 2667 if (cx_get_baud (c)) 2668 length += sprintf (s + length, " %ld", cx_get_baud (c)); 2669 else 2670 length += sprintf (s + length, " extclock"); 2671 2672 if (c->mode == M_HDLC) { 2673 length += sprintf (s + length, " dpll=%s", cx_get_dpll (c) ? "on" : "off"); 2674 length += sprintf (s + length, " nrzi=%s", cx_get_nrzi (c) ? "on" : "off"); 2675 } 2676 2677 length += sprintf (s + length, " loop=%s", cx_get_loop (c) ? "on\n" : "off\n"); 2678 return length; 2679} 2680 2681#if __FreeBSD_version >= 500000 2682static int ng_cx_rcvmsg (node_p node, item_p item, hook_p lasthook) 2683{ 2684 drv_t *d = NG_NODE_PRIVATE (node); 2685 struct ng_mesg *msg; 2686#else 2687static int ng_cx_rcvmsg (node_p node, struct ng_mesg *msg, 2688 const char *retaddr, struct ng_mesg **rptr) 2689{ 2690 drv_t *d = node->private; 2691#endif 2692 struct ng_mesg *resp = NULL; 2693 int error = 0; 2694 2695 if (!d) 2696 return EINVAL; 2697 2698 CX_DEBUG (d, ("Rcvmsg\n")); 2699#if __FreeBSD_version >= 500000 2700 NGI_GET_MSG (item, msg); 2701#endif 2702 switch (msg->header.typecookie) { 2703 default: 2704 error = EINVAL; 2705 break; 2706 2707 case NGM_CX_COOKIE: 2708 printf ("Don't forget to implement\n"); 2709 error = EINVAL; 2710 break; 2711 2712 case NGM_GENERIC_COOKIE: 2713 switch (msg->header.cmd) { 2714 default: 2715 error = EINVAL; 2716 break; 2717 2718 case NGM_TEXT_STATUS: { 2719 char *s; 2720 int l = 0; 2721 int dl = sizeof (struct ng_mesg) + 730; 2722 2723#if __FreeBSD_version >= 500000 2724 NG_MKRESPONSE (resp, msg, dl, M_NOWAIT); 2725 if (! resp) { 2726 error = ENOMEM; 2727 break; 2728 } 2729#else 2730 MALLOC (resp, struct ng_mesg *, dl, 2731 M_NETGRAPH, M_NOWAIT); 2732 if (! resp) { 2733 error = ENOMEM; 2734 break; 2735 } 2736#endif 2737 bzero (resp, dl); 2738 s = (resp)->data; 2739 l += print_chan (s + l, d->chan); 2740 l += print_stats (s + l, d->chan, 1); 2741 l += print_modems (s + l, d->chan, 1); 2742#if __FreeBSD_version < 500000 2743 (resp)->header.version = NG_VERSION; 2744 (resp)->header.arglen = strlen (s) + 1; 2745 (resp)->header.token = msg->header.token; 2746 (resp)->header.typecookie = NGM_CX_COOKIE; 2747 (resp)->header.cmd = msg->header.cmd; 2748#endif 2749 strncpy ((resp)->header.cmdstr, "status", NG_CMDSTRLEN); 2750 } 2751 break; 2752 } 2753 break; 2754 } 2755#if __FreeBSD_version >= 500000 2756 NG_RESPOND_MSG (error, node, item, resp); 2757 NG_FREE_MSG (msg); 2758#else 2759 *rptr = resp; 2760 FREE (msg, M_NETGRAPH); 2761#endif 2762 return error; 2763} 2764 2765#if __FreeBSD_version >= 500000 2766static int ng_cx_rcvdata (hook_p hook, item_p item) 2767{ 2768 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE(hook)); 2769 struct mbuf *m; 2770 meta_p meta; 2771#else 2772static int ng_cx_rcvdata (hook_p hook, struct mbuf *m, meta_p meta) 2773{ 2774 drv_t *d = hook->node->private; 2775#endif 2776 struct ifqueue *q; 2777 int s; 2778 2779#if __FreeBSD_version >= 500000 2780 NGI_GET_M (item, m); 2781 NGI_GET_META (item, meta); 2782 NG_FREE_ITEM (item); 2783 if (! NG_HOOK_PRIVATE (hook) || ! d) { 2784 NG_FREE_M (m); 2785 NG_FREE_META (meta); 2786#else 2787 if (! hook->private || ! d) { 2788 NG_FREE_DATA (m,meta); 2789#endif 2790 return ENETDOWN; 2791 } 2792 q = (meta && meta->priority > 0) ? &d->hi_queue : &d->lo_queue; 2793 s = splhigh (); 2794#if __FreeBSD_version >= 500000 2795 IF_LOCK (q); 2796 if (_IF_QFULL (q)) { 2797 _IF_DROP (q); 2798 IF_UNLOCK (q); 2799 splx (s); 2800 NG_FREE_M (m); 2801 NG_FREE_META (meta); 2802 return ENOBUFS; 2803 } 2804 _IF_ENQUEUE (q, m); 2805 IF_UNLOCK (q); 2806#else 2807 if (IF_QFULL (q)) { 2808 IF_DROP (q); 2809 splx (s); 2810 NG_FREE_DATA (m, meta); 2811 return ENOBUFS; 2812 } 2813 IF_ENQUEUE (q, m); 2814#endif 2815 cx_start (d); 2816 splx (s); 2817 return 0; 2818} 2819 2820static int ng_cx_rmnode (node_p node) 2821{ 2822#if __FreeBSD_version >= 500000 2823 drv_t *d = NG_NODE_PRIVATE (node); 2824 2825 CX_DEBUG (d, ("Rmnode\n")); 2826 if (d && d->running) { 2827 int s = splhigh (); 2828 cx_down (d); 2829 splx (s); 2830 } 2831#ifdef KLD_MODULE 2832 if (node->nd_flags & NG_REALLY_DIE) { 2833 NG_NODE_SET_PRIVATE (node, NULL); 2834 NG_NODE_UNREF (node); 2835 } 2836 node->nd_flags &= ~NG_INVALID; 2837#endif 2838#else /* __FreeBSD_version < 500000 */ 2839 drv_t *d = node->private; 2840 int s; 2841 2842 s = splhigh (); 2843 cx_down (d); 2844 splx (s); 2845 node->flags |= NG_INVALID; 2846 ng_cutlinks (node); 2847#ifdef KLD_MODULE 2848 ng_unname (node); 2849 ng_unref (node); 2850#else 2851 node->flags &= ~NG_INVALID; 2852#endif 2853#endif 2854 return 0; 2855} 2856 2857static void ng_cx_watchdog (void *arg) 2858{ 2859 drv_t *d = arg; 2860 2861 if (d->timeout == 1) 2862 cx_watchdog (d); 2863 if (d->timeout) 2864 d->timeout--; 2865 d->timeout_handle = timeout (ng_cx_watchdog, d, hz); 2866} 2867 2868static int ng_cx_connect (hook_p hook) 2869{ 2870#if __FreeBSD_version >= 500000 2871 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook)); 2872#else 2873 drv_t *d = hook->node->private; 2874#endif 2875 2876 d->timeout_handle = timeout (ng_cx_watchdog, d, hz); 2877 return 0; 2878} 2879 2880static int ng_cx_disconnect (hook_p hook) 2881{ 2882#if __FreeBSD_version >= 500000 2883 drv_t *d = NG_NODE_PRIVATE (NG_HOOK_NODE (hook)); 2884#else 2885 drv_t *d = hook->node->private; 2886#endif 2887 int s; 2888 2889 s = splhigh (); 2890#if __FreeBSD_version >= 500000 2891 if (NG_HOOK_PRIVATE (hook)) 2892#else 2893 if (hook->private) 2894#endif 2895 cx_down (d); 2896 splx (s); 2897 untimeout (ng_cx_watchdog, d, d->timeout_handle); 2898 return 0; 2899} 2900#endif /*NETGRAPH*/ 2901 2902#ifdef KLD_MODULE 2903#if __FreeBSD_version < 400000 2904/* 2905 * Function called when loading the driver. 2906 */ 2907static int cx_load (void) 2908{ 2909 int i; 2910 2911 for (i=0;i<NCX; ++i) { 2912 struct isa_device id = {-1, &cxdriver, -1, 0, -1, 0, 0, (inthand2_t *)cx_intr, i, 0, 0, 0, 0 ,0 ,1 ,0 ,0}; 2913 2914 disable_intr(); 2915 if (!cx_probe (&id)) { 2916 enable_intr(); 2917 break; 2918 } 2919 cx_attach (&id); 2920 register_intr ((adapter [i])->irq, 0, 0, (inthand2_t*) cx_intr, 2921 &net_imask, id.id_unit); 2922 enable_intr(); 2923 } 2924 if (!i) { 2925 /* Deactivate the timeout routine. And soft interrupt*/ 2926 untimeout (cx_timeout, 0, timeout_handle); 2927 unregister_swi (SWI_TTY, cx_softintr); 2928 return ENXIO; 2929 } 2930 return 0; 2931} 2932 2933/* 2934 * Function called when unloading the driver. 2935 */ 2936static int cx_unload (void) 2937{ 2938 int i, s; 2939 2940 /* Check if the device is busy (open). */ 2941 for (i=0; i<NCX*NCHAN; ++i) { 2942 drv_t *d = channel[i]; 2943 cx_chan_t *c; 2944 2945 if (!d || (c=d->chan)->type == T_NONE) 2946 continue; 2947 if (d->lock) 2948 return EBUSY; 2949 if (c->mode == M_ASYNC && (d->tty.t_state & TS_ISOPEN) && 2950 (d->open_dev|0x2)) 2951 return EBUSY; 2952 if (d->running) 2953 return EBUSY; 2954 2955 } 2956 2957 s = splhigh (); 2958 2959 /* Deactivate the timeout routine. And soft interrupt*/ 2960 for (i=0; i<NCX; ++i) { 2961 cx_board_t *b = adapter [i]; 2962 2963 if (!b || ! b->port) 2964 continue; 2965 untimeout (cx_timeout, 0, timeout_handle); 2966 unregister_swi (SWI_TTY, cx_softintr); 2967 break; 2968 } 2969 2970 for (i=0; i<NCX*NCHAN; ++i) { 2971 drv_t *d = channel[i]; 2972 cx_chan_t *c; 2973 2974 if (!d || (c=d->chan)->type == T_NONE) 2975 continue; 2976 2977 if (d->dtr_timeout_handle.callout) 2978 untimeout (cx_dtrwakeup, d, d->dtr_timeout_handle); 2979 if (d->dcd_timeout_handle.callout) 2980 untimeout (cx_carrier, c, d->dcd_timeout_handle); 2981 } 2982 2983 /* Close all active boards. */ 2984 for (i=0; i<NCX; ++i) { 2985 cx_board_t *b = adapter [i]; 2986 2987 if (!b || ! b->port) 2988 continue; 2989 2990 cx_close_board (b); 2991 } 2992 2993 for (i=0; i<NCX; ++i) { 2994 cx_board_t *b = adapter [i]; 2995 2996 if (!b || ! b->port) 2997 continue; 2998 2999 if (led_timo[i].callout) 3000 untimeout (cx_led_off, b, led_timo[i]); 3001 } 3002 3003 /* OK to unload the driver, unregister the interrupt first. */ 3004 for (i=0; i<NCX; ++i) { 3005 cx_board_t *b = adapter [i]; 3006 3007 if (!b || ! b->port) 3008 continue; 3009 /* Disable the interrupt request. */ 3010 disable_intr(); 3011 unregister_intr (b->irq, (inthand2_t *)cx_intr); 3012 isa_dma_release (b->dma); 3013 enable_intr(); 3014 } 3015 splx (s); 3016 3017 s = splhigh (); 3018 /* Detach the interfaces, free buffer memory. */ 3019 for (i=0; i<NCX*NCHAN; ++i) { 3020 drv_t *d = channel[i]; 3021 cx_chan_t *c; 3022 3023 if (!d || (c=d->chan)->type == T_NONE) 3024 continue; 3025 3026#ifndef NETGRAPH 3027#if NBPFILTER > 0 3028 /* Detach from the packet filter list of interfaces. */ 3029 { 3030 struct bpf_if *q, **b = &bpf_iflist; 3031 3032 while ((q = *b)) { 3033 if (q->bif_ifp == d->pp.pp_if) { 3034 *b = q->bif_next; 3035 free (q, M_DEVBUF); 3036 } 3037 b = &(q->bif_next); 3038 } 3039 } 3040#endif /* NBPFILTER */ 3041 /* Detach from the sync PPP list. */ 3042 sppp_detach (&d->pp.pp_if); 3043 3044 /* Detach from the system list of interfaces. */ 3045 { 3046 struct ifaddr *ifa; 3047 TAILQ_FOREACH (ifa, &d->pp.pp_if.if_addrhead, ifa_link) { 3048 TAILQ_REMOVE (&d->pp.pp_if.if_addrhead, ifa, ifa_link); 3049 free (ifa, M_IFADDR); 3050 } 3051 TAILQ_REMOVE (&ifnet, &d->pp.pp_if, if_link); 3052 } 3053#endif /* !NETGRAPH */ 3054 /* Deallocate buffers. */ 3055/* free (d, M_DEVBUF);*/ 3056 } 3057 3058 for (i=0; i<NCX; ++i) { 3059 cx_board_t *b = adapter [i]; 3060 if (!b) 3061 continue; 3062 adapter [b->num] = 0; 3063 free (b, M_DEVBUF); 3064 } 3065 3066 splx (s); 3067 3068 return 0; 3069} 3070 3071#define devsw(a) cdevsw[major((a))] 3072#endif /* __FreeBSD_version < 400000 */ 3073#endif /* KLD_MODULE */ 3074 3075#if __FreeBSD_version < 400000 3076#ifdef KLD_MODULE 3077static int cx_modevent (module_t mod, int type, void *unused) 3078{ 3079 dev_t dev; 3080 int result; 3081 static int load_count = 0; 3082 3083 dev = makedev (CDEV_MAJOR, 0); 3084 switch (type) { 3085 case MOD_LOAD: 3086 if (devsw(dev)) 3087 return (ENXIO); 3088 load_count ++; 3089 cdevsw_add (&dev, &cx_cdevsw, NULL); 3090 timeout_handle = timeout (cx_timeout, 0, hz*5); 3091 3092 /* Software interrupt. */ 3093 register_swi (SWI_TTY, cx_softintr); 3094 3095 result = cx_load (); 3096 return result; 3097 case MOD_UNLOAD: 3098 result = cx_unload (); 3099 if (result) 3100 return result; 3101 if (devsw(dev)&&!(load_count-1)) { 3102 cdevsw_add (&dev, NULL, NULL); 3103 } 3104 load_count --; 3105 return result; 3106 case MOD_SHUTDOWN: 3107 break; 3108 } 3109 return 0; 3110} 3111#endif /* KLD_MODULE */ 3112#else /* __FreeBSD_version >= 400000 */ 3113static int cx_modevent (module_t mod, int type, void *unused) 3114{ 3115 dev_t dev; 3116 static int load_count = 0; 3117 struct cdevsw *cdsw; 3118 3119#if __FreeBSD_version >= 502103 3120 dev = udev2dev (makeudev(CDEV_MAJOR, 0)); 3121#else 3122 dev = makedev (CDEV_MAJOR, 0); 3123#endif 3124 switch (type) { 3125 case MOD_LOAD: 3126 if (dev != NODEV && 3127 (cdsw = devsw (dev)) && 3128 cdsw->d_maj == CDEV_MAJOR) { 3129 printf ("Sigma driver is already in system\n"); 3130 return (EEXIST); 3131 } 3132#if __FreeBSD_version >= 500000 && defined NETGRAPH 3133 if (ng_newtype (&typestruct)) 3134 printf ("Failed to register ng_cx\n"); 3135#endif 3136 ++load_count; 3137#if __FreeBSD_version <= 500000 3138 cdevsw_add (&cx_cdevsw); 3139#endif 3140 timeout_handle = timeout (cx_timeout, 0, hz*5); 3141 /* Software interrupt. */ 3142#if __FreeBSD_version < 500000 3143 register_swi (SWI_TTY, cx_softintr); 3144#else 3145 swi_add(&tty_ithd, "tty:cx", cx_softintr, NULL, SWI_TTY, 0, 3146 &cx_fast_ih); 3147#endif 3148 break; 3149 case MOD_UNLOAD: 3150 if (load_count == 1) { 3151 printf ("Removing device entry for Sigma\n"); 3152#if __FreeBSD_version <= 500000 3153 cdevsw_remove (&cx_cdevsw); 3154#endif 3155#if __FreeBSD_version >= 500000 && defined NETGRAPH 3156 ng_rmtype (&typestruct); 3157#endif 3158 } 3159 if (timeout_handle.callout) 3160 untimeout (cx_timeout, 0, timeout_handle); 3161#if __FreeBSD_version >= 500000 3162 ithread_remove_handler (cx_fast_ih); 3163#else 3164 unregister_swi (SWI_TTY, cx_softintr); 3165#endif 3166 --load_count; 3167 break; 3168 case MOD_SHUTDOWN: 3169 break; 3170 } 3171 return 0; 3172} 3173#endif /* __FreeBSD_version >= 400000 */ 3174 3175#ifdef NETGRAPH 3176static struct ng_type typestruct = { 3177#if __FreeBSD_version >= 500000 3178 NG_ABI_VERSION, 3179#else 3180 NG_VERSION, 3181#endif 3182 NG_CX_NODE_TYPE, 3183#if __FreeBSD_version < 500000 && defined KLD_MODULE 3184 cx_modevent, 3185#else 3186 NULL, 3187#endif 3188 ng_cx_constructor, 3189 ng_cx_rcvmsg, 3190 ng_cx_rmnode, 3191 ng_cx_newhook, 3192 NULL, 3193 ng_cx_connect, 3194 ng_cx_rcvdata, 3195#if __FreeBSD_version < 500000 3196 NULL, 3197#endif 3198 ng_cx_disconnect 3199}; 3200 3201#if __FreeBSD_version < 400000 3202NETGRAPH_INIT_ORDERED (cx, &typestruct, SI_SUB_DRIVERS,\ 3203 SI_ORDER_MIDDLE + CDEV_MAJOR); 3204#endif 3205#endif /*NETGRAPH*/ 3206 3207#if __FreeBSD_version >= 500000 3208#ifdef NETGRAPH 3209MODULE_DEPEND (ng_cx, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION); 3210#else 3211MODULE_DEPEND (isa_cx, sppp, 1, 1, 1); 3212#endif 3213#ifdef KLD_MODULE 3214DRIVER_MODULE (cxmod, isa, cx_isa_driver, cx_devclass, cx_modevent, NULL); 3215#else 3216DRIVER_MODULE (cx, isa, cx_isa_driver, cx_devclass, cx_modevent, NULL); 3217#endif 3218#elif __FreeBSD_version >= 400000 3219#ifdef NETGRAPH 3220DRIVER_MODULE(cx, isa, cx_isa_driver, cx_devclass, ng_mod_event, &typestruct); 3221#else 3222DRIVER_MODULE(cx, isa, cx_isa_driver, cx_devclass, cx_modevent, 0); 3223#endif 3224#else /* __FreeBSD_version < 400000 */ 3225#ifdef KLD_MODULE 3226#ifndef NETGRAPH 3227static moduledata_t cxmod = { "cx", cx_modevent, NULL}; 3228DECLARE_MODULE (cx, cxmod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR); 3229#endif 3230#else /* KLD_MODULE */ 3231 3232/* 3233 * Now for some driver initialisation. 3234 * Occurs ONCE during boot (very early). 3235 * This is if we are NOT a loadable module. 3236 */ 3237static void cx_drvinit (void *unused) 3238{ 3239#if __FreeBSD_version < 400000 3240 dev_t dev; 3241 3242 dev = makedev (CDEV_MAJOR, 0); 3243 cdevsw_add (&dev, &cx_cdevsw, NULL); 3244#else 3245 cdevsw_add (&cx_cdevsw); 3246#endif 3247 3248 /* Activate the timeout routine. */ 3249 timeout_handle = timeout (cx_timeout, 0, hz*5); 3250 3251 /* Software interrupt. */ 3252 register_swi (SWI_TTY, cx_softintr); 3253#ifdef NETGRAPH 3254#if 0 3255 /* Register our node type in netgraph */ 3256 if (ng_newtype (&typestruct)) 3257 printf ("Failed to register ng_cx\n"); 3258#endif 3259#endif 3260} 3261 3262SYSINIT (cxdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR, cx_drvinit, 0) 3263 3264#endif /* KLD_MODULE */ 3265#endif /* __FreeBSD_version < 400000 */ 3266#endif /* NCX */ 3267