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