1 /* $OpenBSD: if_cue.c,v 1.18 2003/05/07 04:33:33 deraadt Exp $ */ 2 /* $NetBSD: if_cue.c,v 1.40 2002/07/11 21:14:26 augustss Exp $ */ 3 /* 4 * Copyright (c) 1997, 1998, 1999, 2000 5 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Bill Paul. 18 * 4. Neither the name of the author nor the names of any co-contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32 * THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * $FreeBSD: src/sys/dev/usb/if_cue.c,v 1.4 2000/01/16 22:45:06 wpaul Exp $ 35 */ 36 37 /* 38 * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate 39 * adapters and others. 40 * 41 * Written by Bill Paul <wpaul@ee.columbia.edu> 42 * Electrical Engineering Department 43 * Columbia University, New York City 44 */ 45 46 /* 47 * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The 48 * RX filter uses a 512-bit multicast hash table, single perfect entry 49 * for the station address, and promiscuous mode. Unlike the ADMtek 50 * and KLSI chips, the CATC ASIC supports read and write combining 51 * mode where multiple packets can be transferred using a single bulk 52 * transaction, which helps performance a great deal. 53 */ 54 55 /* 56 * Ported to NetBSD and somewhat rewritten by Lennart Augustsson. 57 */ 58 59 #if defined(__NetBSD__) 60 #include "opt_inet.h" 61 #include "opt_ns.h" 62 #include "bpfilter.h" 63 #include "rnd.h" 64 #elif defined(__OpenBSD__) 65 #include "bpfilter.h" 66 #endif /* defined(__OpenBSD__) */ 67 68 #include <sys/param.h> 69 #include <sys/systm.h> 70 #if !defined(__OpenBSD__) 71 #include <sys/callout.h> 72 #endif 73 #include <sys/sockio.h> 74 #include <sys/mbuf.h> 75 #include <sys/malloc.h> 76 #include <sys/kernel.h> 77 #include <sys/socket.h> 78 79 #include <sys/device.h> 80 #if NRND > 0 81 #include <sys/rnd.h> 82 #endif 83 84 #include <net/if.h> 85 #if defined(__NetBSD__) 86 #include <net/if_arp.h> 87 #endif 88 #include <net/if_dl.h> 89 90 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m)) 91 92 #if NBPFILTER > 0 93 #include <net/bpf.h> 94 #endif 95 96 #if defined(__NetBSD__) 97 #include <net/if_ether.h> 98 #ifdef INET 99 #include <netinet/in.h> 100 #include <netinet/if_inarp.h> 101 #endif 102 #endif /* defined(__NetBSD__) */ 103 104 #if defined(__OpenBSD__) 105 #ifdef INET 106 #include <netinet/in.h> 107 #include <netinet/in_systm.h> 108 #include <netinet/in_var.h> 109 #include <netinet/ip.h> 110 #include <netinet/if_ether.h> 111 #endif 112 #endif /* defined(__OpenBSD__) */ 113 114 #ifdef NS 115 #include <netns/ns.h> 116 #include <netns/ns_if.h> 117 #endif 118 119 #include <dev/usb/usb.h> 120 #include <dev/usb/usbdi.h> 121 #include <dev/usb/usbdi_util.h> 122 #include <dev/usb/usbdevs.h> 123 124 #include <dev/usb/if_cuereg.h> 125 126 #ifdef CUE_DEBUG 127 #define DPRINTF(x) if (cuedebug) logprintf x 128 #define DPRINTFN(n,x) if (cuedebug >= (n)) logprintf x 129 int cuedebug = 0; 130 #else 131 #define DPRINTF(x) 132 #define DPRINTFN(n,x) 133 #endif 134 135 /* 136 * Various supported device vendors/products. 137 */ 138 Static struct usb_devno cue_devs[] = { 139 { USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE }, 140 { USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2 }, 141 { USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK }, 142 /* Belkin F5U111 adapter covered by NETMATE entry */ 143 }; 144 #define cue_lookup(v, p) (usb_lookup(cue_devs, v, p)) 145 146 USB_DECLARE_DRIVER(cue); 147 148 Static int cue_open_pipes(struct cue_softc *); 149 Static int cue_tx_list_init(struct cue_softc *); 150 Static int cue_rx_list_init(struct cue_softc *); 151 Static int cue_newbuf(struct cue_softc *, struct cue_chain *, struct mbuf *); 152 Static int cue_send(struct cue_softc *, struct mbuf *, int); 153 Static void cue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 154 Static void cue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 155 Static void cue_tick(void *); 156 Static void cue_tick_task(void *); 157 Static void cue_start(struct ifnet *); 158 Static int cue_ioctl(struct ifnet *, u_long, caddr_t); 159 Static void cue_init(void *); 160 Static void cue_stop(struct cue_softc *); 161 Static void cue_watchdog(struct ifnet *); 162 163 Static void cue_setmulti(struct cue_softc *); 164 Static u_int32_t cue_crc(caddr_t); 165 Static void cue_reset(struct cue_softc *); 166 167 Static int cue_csr_read_1(struct cue_softc *, int); 168 Static int cue_csr_write_1(struct cue_softc *, int, int); 169 Static int cue_csr_read_2(struct cue_softc *, int); 170 #if 0 171 Static int cue_csr_write_2(struct cue_softc *, int, int); 172 #endif 173 Static int cue_mem(struct cue_softc *, int, int, void *, int); 174 Static int cue_getmac(struct cue_softc *, void *); 175 176 #define CUE_SETBIT(sc, reg, x) \ 177 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x)) 178 179 #define CUE_CLRBIT(sc, reg, x) \ 180 cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x)) 181 182 Static int 183 cue_csr_read_1(struct cue_softc *sc, int reg) 184 { 185 usb_device_request_t req; 186 usbd_status err; 187 u_int8_t val = 0; 188 189 if (sc->cue_dying) 190 return (0); 191 192 req.bmRequestType = UT_READ_VENDOR_DEVICE; 193 req.bRequest = CUE_CMD_READREG; 194 USETW(req.wValue, 0); 195 USETW(req.wIndex, reg); 196 USETW(req.wLength, 1); 197 198 err = usbd_do_request(sc->cue_udev, &req, &val); 199 200 if (err) { 201 DPRINTF(("%s: cue_csr_read_1: reg=0x%x err=%s\n", 202 USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err))); 203 return (0); 204 } 205 206 DPRINTFN(10,("%s: cue_csr_read_1 reg=0x%x val=0x%x\n", 207 USBDEVNAME(sc->cue_dev), reg, val)); 208 209 return (val); 210 } 211 212 Static int 213 cue_csr_read_2(struct cue_softc *sc, int reg) 214 { 215 usb_device_request_t req; 216 usbd_status err; 217 uWord val; 218 219 if (sc->cue_dying) 220 return (0); 221 222 req.bmRequestType = UT_READ_VENDOR_DEVICE; 223 req.bRequest = CUE_CMD_READREG; 224 USETW(req.wValue, 0); 225 USETW(req.wIndex, reg); 226 USETW(req.wLength, 2); 227 228 err = usbd_do_request(sc->cue_udev, &req, &val); 229 230 DPRINTFN(10,("%s: cue_csr_read_2 reg=0x%x val=0x%x\n", 231 USBDEVNAME(sc->cue_dev), reg, UGETW(val))); 232 233 if (err) { 234 DPRINTF(("%s: cue_csr_read_2: reg=0x%x err=%s\n", 235 USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err))); 236 return (0); 237 } 238 239 return (UGETW(val)); 240 } 241 242 Static int 243 cue_csr_write_1(struct cue_softc *sc, int reg, int val) 244 { 245 usb_device_request_t req; 246 usbd_status err; 247 248 if (sc->cue_dying) 249 return (0); 250 251 DPRINTFN(10,("%s: cue_csr_write_1 reg=0x%x val=0x%x\n", 252 USBDEVNAME(sc->cue_dev), reg, val)); 253 254 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 255 req.bRequest = CUE_CMD_WRITEREG; 256 USETW(req.wValue, val); 257 USETW(req.wIndex, reg); 258 USETW(req.wLength, 0); 259 260 err = usbd_do_request(sc->cue_udev, &req, NULL); 261 262 if (err) { 263 DPRINTF(("%s: cue_csr_write_1: reg=0x%x err=%s\n", 264 USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err))); 265 return (-1); 266 } 267 268 DPRINTFN(20,("%s: cue_csr_write_1, after reg=0x%x val=0x%x\n", 269 USBDEVNAME(sc->cue_dev), reg, cue_csr_read_1(sc, reg))); 270 271 return (0); 272 } 273 274 #if 0 275 Static int 276 cue_csr_write_2(struct cue_softc *sc, int reg, int aval) 277 { 278 usb_device_request_t req; 279 usbd_status err; 280 uWord val; 281 int s; 282 283 if (sc->cue_dying) 284 return (0); 285 286 DPRINTFN(10,("%s: cue_csr_write_2 reg=0x%x val=0x%x\n", 287 USBDEVNAME(sc->cue_dev), reg, aval)); 288 289 USETW(val, aval); 290 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 291 req.bRequest = CUE_CMD_WRITEREG; 292 USETW(req.wValue, val); 293 USETW(req.wIndex, reg); 294 USETW(req.wLength, 0); 295 296 err = usbd_do_request(sc->cue_udev, &req, NULL); 297 298 if (err) { 299 DPRINTF(("%s: cue_csr_write_2: reg=0x%x err=%s\n", 300 USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err))); 301 return (-1); 302 } 303 304 return (0); 305 } 306 #endif 307 308 Static int 309 cue_mem(struct cue_softc *sc, int cmd, int addr, void *buf, int len) 310 { 311 usb_device_request_t req; 312 usbd_status err; 313 314 DPRINTFN(10,("%s: cue_mem cmd=0x%x addr=0x%x len=%d\n", 315 USBDEVNAME(sc->cue_dev), cmd, addr, len)); 316 317 if (cmd == CUE_CMD_READSRAM) 318 req.bmRequestType = UT_READ_VENDOR_DEVICE; 319 else 320 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 321 req.bRequest = cmd; 322 USETW(req.wValue, 0); 323 USETW(req.wIndex, addr); 324 USETW(req.wLength, len); 325 326 err = usbd_do_request(sc->cue_udev, &req, buf); 327 328 if (err) { 329 DPRINTF(("%s: cue_csr_mem: addr=0x%x err=%s\n", 330 USBDEVNAME(sc->cue_dev), addr, usbd_errstr(err))); 331 return (-1); 332 } 333 334 return (0); 335 } 336 337 Static int 338 cue_getmac(struct cue_softc *sc, void *buf) 339 { 340 usb_device_request_t req; 341 usbd_status err; 342 343 DPRINTFN(10,("%s: cue_getmac\n", USBDEVNAME(sc->cue_dev))); 344 345 req.bmRequestType = UT_READ_VENDOR_DEVICE; 346 req.bRequest = CUE_CMD_GET_MACADDR; 347 USETW(req.wValue, 0); 348 USETW(req.wIndex, 0); 349 USETW(req.wLength, ETHER_ADDR_LEN); 350 351 err = usbd_do_request(sc->cue_udev, &req, buf); 352 353 if (err) { 354 printf("%s: read MAC address failed\n", 355 USBDEVNAME(sc->cue_dev)); 356 return (-1); 357 } 358 359 return (0); 360 } 361 362 #define CUE_POLY 0xEDB88320 363 #define CUE_BITS 9 364 365 Static u_int32_t 366 cue_crc(caddr_t addr) 367 { 368 u_int32_t idx, bit, data, crc; 369 370 /* Compute CRC for the address value. */ 371 crc = 0xFFFFFFFF; /* initial value */ 372 373 for (idx = 0; idx < 6; idx++) { 374 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) 375 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? CUE_POLY : 0); 376 } 377 378 return (crc & ((1 << CUE_BITS) - 1)); 379 } 380 381 Static void 382 cue_setmulti(struct cue_softc *sc) 383 { 384 struct ifnet *ifp; 385 struct ether_multi *enm; 386 struct ether_multistep step; 387 u_int32_t h, i; 388 389 ifp = GET_IFP(sc); 390 391 DPRINTFN(2,("%s: cue_setmulti if_flags=0x%x\n", 392 USBDEVNAME(sc->cue_dev), ifp->if_flags)); 393 394 if (ifp->if_flags & IFF_PROMISC) { 395 allmulti: 396 ifp->if_flags |= IFF_ALLMULTI; 397 for (i = 0; i < CUE_MCAST_TABLE_LEN; i++) 398 sc->cue_mctab[i] = 0xFF; 399 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, 400 &sc->cue_mctab, CUE_MCAST_TABLE_LEN); 401 return; 402 } 403 404 /* first, zot all the existing hash bits */ 405 for (i = 0; i < CUE_MCAST_TABLE_LEN; i++) 406 sc->cue_mctab[i] = 0; 407 408 /* now program new ones */ 409 #if defined(__NetBSD__) 410 ETHER_FIRST_MULTI(step, &sc->cue_ec, enm); 411 #else 412 ETHER_FIRST_MULTI(step, &sc->arpcom, enm); 413 #endif 414 while (enm != NULL) { 415 if (memcmp(enm->enm_addrlo, 416 enm->enm_addrhi, ETHER_ADDR_LEN) != 0) 417 goto allmulti; 418 419 h = cue_crc(enm->enm_addrlo); 420 sc->cue_mctab[h >> 3] |= 1 << (h & 0x7); 421 ETHER_NEXT_MULTI(step, enm); 422 } 423 424 ifp->if_flags &= ~IFF_ALLMULTI; 425 426 /* 427 * Also include the broadcast address in the filter 428 * so we can receive broadcast frames. 429 */ 430 if (ifp->if_flags & IFF_BROADCAST) { 431 h = cue_crc(etherbroadcastaddr); 432 sc->cue_mctab[h >> 3] |= 1 << (h & 0x7); 433 } 434 435 cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, 436 &sc->cue_mctab, CUE_MCAST_TABLE_LEN); 437 } 438 439 Static void 440 cue_reset(struct cue_softc *sc) 441 { 442 usb_device_request_t req; 443 usbd_status err; 444 445 DPRINTFN(2,("%s: cue_reset\n", USBDEVNAME(sc->cue_dev))); 446 447 if (sc->cue_dying) 448 return; 449 450 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 451 req.bRequest = CUE_CMD_RESET; 452 USETW(req.wValue, 0); 453 USETW(req.wIndex, 0); 454 USETW(req.wLength, 0); 455 456 err = usbd_do_request(sc->cue_udev, &req, NULL); 457 458 if (err) 459 printf("%s: reset failed\n", USBDEVNAME(sc->cue_dev)); 460 461 /* Wait a little while for the chip to get its brains in order. */ 462 usbd_delay_ms(sc->cue_udev, 1); 463 } 464 465 /* 466 * Probe for a CATC chip. 467 */ 468 USB_MATCH(cue) 469 { 470 USB_MATCH_START(cue, uaa); 471 472 if (uaa->iface != NULL) 473 return (UMATCH_NONE); 474 475 return (cue_lookup(uaa->vendor, uaa->product) != NULL ? 476 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 477 } 478 479 /* 480 * Attach the interface. Allocate softc structures, do ifmedia 481 * setup and ethernet/BPF attach. 482 */ 483 USB_ATTACH(cue) 484 { 485 USB_ATTACH_START(cue, sc, uaa); 486 char devinfo[1024]; 487 int s; 488 u_char eaddr[ETHER_ADDR_LEN]; 489 usbd_device_handle dev = uaa->device; 490 usbd_interface_handle iface; 491 usbd_status err; 492 struct ifnet *ifp; 493 usb_interface_descriptor_t *id; 494 usb_endpoint_descriptor_t *ed; 495 int i; 496 497 DPRINTFN(5,(" : cue_attach: sc=%p, dev=%p", sc, dev)); 498 499 usbd_devinfo(dev, 0, devinfo, sizeof devinfo); 500 USB_ATTACH_SETUP; 501 printf("%s: %s\n", USBDEVNAME(sc->cue_dev), devinfo); 502 503 err = usbd_set_config_no(dev, CUE_CONFIG_NO, 1); 504 if (err) { 505 printf("%s: setting config no failed\n", 506 USBDEVNAME(sc->cue_dev)); 507 USB_ATTACH_ERROR_RETURN; 508 } 509 510 sc->cue_udev = dev; 511 sc->cue_product = uaa->product; 512 sc->cue_vendor = uaa->vendor; 513 514 usb_init_task(&sc->cue_tick_task, cue_tick_task, sc); 515 usb_init_task(&sc->cue_stop_task, (void (*)(void *))cue_stop, sc); 516 517 err = usbd_device2interface_handle(dev, CUE_IFACE_IDX, &iface); 518 if (err) { 519 printf("%s: getting interface handle failed\n", 520 USBDEVNAME(sc->cue_dev)); 521 USB_ATTACH_ERROR_RETURN; 522 } 523 524 sc->cue_iface = iface; 525 id = usbd_get_interface_descriptor(iface); 526 527 /* Find endpoints. */ 528 for (i = 0; i < id->bNumEndpoints; i++) { 529 ed = usbd_interface2endpoint_descriptor(iface, i); 530 if (ed == NULL) { 531 printf("%s: couldn't get ep %d\n", 532 USBDEVNAME(sc->cue_dev), i); 533 USB_ATTACH_ERROR_RETURN; 534 } 535 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 536 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 537 sc->cue_ed[CUE_ENDPT_RX] = ed->bEndpointAddress; 538 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 539 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 540 sc->cue_ed[CUE_ENDPT_TX] = ed->bEndpointAddress; 541 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 542 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 543 sc->cue_ed[CUE_ENDPT_INTR] = ed->bEndpointAddress; 544 } 545 } 546 547 #if 0 548 /* Reset the adapter. */ 549 cue_reset(sc); 550 #endif 551 /* 552 * Get station address. 553 */ 554 cue_getmac(sc, &eaddr); 555 556 s = splnet(); 557 558 /* 559 * A CATC chip was detected. Inform the world. 560 */ 561 printf("%s: address %s\n", USBDEVNAME(sc->cue_dev), 562 ether_sprintf(eaddr)); 563 564 #if defined(__OpenBSD__) 565 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 566 #endif 567 568 /* Initialize interface info.*/ 569 ifp = GET_IFP(sc); 570 ifp->if_softc = sc; 571 ifp->if_mtu = ETHERMTU; 572 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 573 ifp->if_ioctl = cue_ioctl; 574 ifp->if_start = cue_start; 575 ifp->if_watchdog = cue_watchdog; 576 strncpy(ifp->if_xname, USBDEVNAME(sc->cue_dev), IFNAMSIZ); 577 578 IFQ_SET_READY(&ifp->if_snd); 579 580 /* Attach the interface. */ 581 if_attach(ifp); 582 Ether_ifattach(ifp, eaddr); 583 #if NRND > 0 584 rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->cue_dev), 585 RND_TYPE_NET, 0); 586 #endif 587 588 usb_callout_init(sc->cue_stat_ch); 589 590 sc->cue_attached = 1; 591 splx(s); 592 593 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cue_udev, 594 USBDEV(sc->cue_dev)); 595 596 USB_ATTACH_SUCCESS_RETURN; 597 } 598 599 USB_DETACH(cue) 600 { 601 USB_DETACH_START(cue, sc); 602 struct ifnet *ifp = GET_IFP(sc); 603 int s; 604 605 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __func__)); 606 607 usb_uncallout(sc->cue_stat_ch, cue_tick, sc); 608 /* 609 * Remove any pending task. It cannot be executing because it run 610 * in the same thread as detach. 611 */ 612 usb_rem_task(sc->cue_udev, &sc->cue_tick_task); 613 usb_rem_task(sc->cue_udev, &sc->cue_stop_task); 614 615 if (!sc->cue_attached) { 616 /* Detached before attached finished, so just bail out. */ 617 return (0); 618 } 619 620 s = splusb(); 621 622 if (ifp->if_flags & IFF_RUNNING) 623 cue_stop(sc); 624 625 #if defined(__NetBSD__) 626 #if NRND > 0 627 rnd_detach_source(&sc->rnd_source); 628 #endif 629 #endif /* __NetBSD__ */ 630 ether_ifdetach(ifp); 631 632 if_detach(ifp); 633 634 #ifdef DIAGNOSTIC 635 if (sc->cue_ep[CUE_ENDPT_TX] != NULL || 636 sc->cue_ep[CUE_ENDPT_RX] != NULL || 637 sc->cue_ep[CUE_ENDPT_INTR] != NULL) 638 printf("%s: detach has active endpoints\n", 639 USBDEVNAME(sc->cue_dev)); 640 #endif 641 642 sc->cue_attached = 0; 643 splx(s); 644 645 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cue_udev, 646 USBDEV(sc->cue_dev)); 647 648 return (0); 649 } 650 651 int 652 cue_activate(device_ptr_t self, enum devact act) 653 { 654 struct cue_softc *sc = (struct cue_softc *)self; 655 656 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __func__)); 657 658 switch (act) { 659 case DVACT_ACTIVATE: 660 return (EOPNOTSUPP); 661 break; 662 663 case DVACT_DEACTIVATE: 664 /* Deactivate the interface. */ 665 if_deactivate(&sc->cue_ec.ec_if); 666 sc->cue_dying = 1; 667 break; 668 } 669 return (0); 670 } 671 672 /* 673 * Initialize an RX descriptor and attach an MBUF cluster. 674 */ 675 Static int 676 cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m) 677 { 678 struct mbuf *m_new = NULL; 679 680 if (m == NULL) { 681 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 682 if (m_new == NULL) { 683 printf("%s: no memory for rx list " 684 "-- packet dropped!\n", USBDEVNAME(sc->cue_dev)); 685 return (ENOBUFS); 686 } 687 688 MCLGET(m_new, M_DONTWAIT); 689 if (!(m_new->m_flags & M_EXT)) { 690 printf("%s: no memory for rx list " 691 "-- packet dropped!\n", USBDEVNAME(sc->cue_dev)); 692 m_freem(m_new); 693 return (ENOBUFS); 694 } 695 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 696 } else { 697 m_new = m; 698 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 699 m_new->m_data = m_new->m_ext.ext_buf; 700 } 701 702 m_adj(m_new, ETHER_ALIGN); 703 c->cue_mbuf = m_new; 704 705 return (0); 706 } 707 708 Static int 709 cue_rx_list_init(struct cue_softc *sc) 710 { 711 struct cue_cdata *cd; 712 struct cue_chain *c; 713 int i; 714 715 cd = &sc->cue_cdata; 716 for (i = 0; i < CUE_RX_LIST_CNT; i++) { 717 c = &cd->cue_rx_chain[i]; 718 c->cue_sc = sc; 719 c->cue_idx = i; 720 if (cue_newbuf(sc, c, NULL) == ENOBUFS) 721 return (ENOBUFS); 722 if (c->cue_xfer == NULL) { 723 c->cue_xfer = usbd_alloc_xfer(sc->cue_udev); 724 if (c->cue_xfer == NULL) 725 return (ENOBUFS); 726 c->cue_buf = usbd_alloc_buffer(c->cue_xfer, CUE_BUFSZ); 727 if (c->cue_buf == NULL) { 728 usbd_free_xfer(c->cue_xfer); 729 return (ENOBUFS); 730 } 731 } 732 } 733 734 return (0); 735 } 736 737 Static int 738 cue_tx_list_init(struct cue_softc *sc) 739 { 740 struct cue_cdata *cd; 741 struct cue_chain *c; 742 int i; 743 744 cd = &sc->cue_cdata; 745 for (i = 0; i < CUE_TX_LIST_CNT; i++) { 746 c = &cd->cue_tx_chain[i]; 747 c->cue_sc = sc; 748 c->cue_idx = i; 749 c->cue_mbuf = NULL; 750 if (c->cue_xfer == NULL) { 751 c->cue_xfer = usbd_alloc_xfer(sc->cue_udev); 752 if (c->cue_xfer == NULL) 753 return (ENOBUFS); 754 c->cue_buf = usbd_alloc_buffer(c->cue_xfer, CUE_BUFSZ); 755 if (c->cue_buf == NULL) { 756 usbd_free_xfer(c->cue_xfer); 757 return (ENOBUFS); 758 } 759 } 760 } 761 762 return (0); 763 } 764 765 /* 766 * A frame has been uploaded: pass the resulting mbuf chain up to 767 * the higher level protocols. 768 */ 769 Static void 770 cue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 771 { 772 struct cue_chain *c = priv; 773 struct cue_softc *sc = c->cue_sc; 774 struct ifnet *ifp = GET_IFP(sc); 775 struct mbuf *m; 776 int total_len = 0; 777 u_int16_t len; 778 int s; 779 780 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev), 781 __func__, status)); 782 783 if (sc->cue_dying) 784 return; 785 786 if (!(ifp->if_flags & IFF_RUNNING)) 787 return; 788 789 if (status != USBD_NORMAL_COMPLETION) { 790 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 791 return; 792 sc->cue_rx_errs++; 793 if (usbd_ratecheck(&sc->cue_rx_notice)) { 794 printf("%s: %u usb errors on rx: %s\n", 795 USBDEVNAME(sc->cue_dev), sc->cue_rx_errs, 796 usbd_errstr(status)); 797 sc->cue_rx_errs = 0; 798 } 799 if (status == USBD_STALLED) 800 usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_RX]); 801 goto done; 802 } 803 804 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 805 806 memcpy(mtod(c->cue_mbuf, char *), c->cue_buf, total_len); 807 808 m = c->cue_mbuf; 809 len = UGETW(mtod(m, u_int8_t *)); 810 811 /* No errors; receive the packet. */ 812 total_len = len; 813 814 if (len < sizeof(struct ether_header)) { 815 ifp->if_ierrors++; 816 goto done; 817 } 818 819 ifp->if_ipackets++; 820 m_adj(m, sizeof(u_int16_t)); 821 m->m_pkthdr.len = m->m_len = total_len; 822 823 m->m_pkthdr.rcvif = ifp; 824 825 s = splnet(); 826 827 /* XXX ugly */ 828 if (cue_newbuf(sc, c, NULL) == ENOBUFS) { 829 ifp->if_ierrors++; 830 goto done1; 831 } 832 833 #if NBPFILTER > 0 834 /* 835 * Handle BPF listeners. Let the BPF user see the packet, but 836 * don't pass it up to the ether_input() layer unless it's 837 * a broadcast packet, multicast packet, matches our ethernet 838 * address or the interface is in promiscuous mode. 839 */ 840 if (ifp->if_bpf) 841 BPF_MTAP(ifp, m); 842 #endif 843 844 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->cue_dev), 845 __func__, m->m_len)); 846 IF_INPUT(ifp, m); 847 done1: 848 splx(s); 849 850 done: 851 /* Setup new transfer. */ 852 usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX], 853 c, c->cue_buf, CUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, 854 USBD_NO_TIMEOUT, cue_rxeof); 855 usbd_transfer(c->cue_xfer); 856 857 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->cue_dev), 858 __func__)); 859 } 860 861 /* 862 * A frame was downloaded to the chip. It's safe for us to clean up 863 * the list buffers. 864 */ 865 Static void 866 cue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 867 { 868 struct cue_chain *c = priv; 869 struct cue_softc *sc = c->cue_sc; 870 struct ifnet *ifp = GET_IFP(sc); 871 int s; 872 873 if (sc->cue_dying) 874 return; 875 876 s = splnet(); 877 878 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev), 879 __func__, status)); 880 881 ifp->if_timer = 0; 882 ifp->if_flags &= ~IFF_OACTIVE; 883 884 if (status != USBD_NORMAL_COMPLETION) { 885 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 886 splx(s); 887 return; 888 } 889 ifp->if_oerrors++; 890 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->cue_dev), 891 usbd_errstr(status)); 892 if (status == USBD_STALLED) 893 usbd_clear_endpoint_stall(sc->cue_ep[CUE_ENDPT_TX]); 894 splx(s); 895 return; 896 } 897 898 ifp->if_opackets++; 899 900 m_freem(c->cue_mbuf); 901 c->cue_mbuf = NULL; 902 903 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 904 cue_start(ifp); 905 906 splx(s); 907 } 908 909 Static void 910 cue_tick(void *xsc) 911 { 912 struct cue_softc *sc = xsc; 913 914 if (sc == NULL) 915 return; 916 917 if (sc->cue_dying) 918 return; 919 920 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __func__)); 921 922 /* Perform statistics update in process context. */ 923 usb_add_task(sc->cue_udev, &sc->cue_tick_task); 924 } 925 926 Static void 927 cue_tick_task(void *xsc) 928 { 929 struct cue_softc *sc = xsc; 930 struct ifnet *ifp; 931 932 if (sc->cue_dying) 933 return; 934 935 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __func__)); 936 937 ifp = GET_IFP(sc); 938 939 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL); 940 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL); 941 ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL); 942 943 if (cue_csr_read_2(sc, CUE_RX_FRAMEERR)) 944 ifp->if_ierrors++; 945 } 946 947 Static int 948 cue_send(struct cue_softc *sc, struct mbuf *m, int idx) 949 { 950 int total_len; 951 struct cue_chain *c; 952 usbd_status err; 953 954 c = &sc->cue_cdata.cue_tx_chain[idx]; 955 956 /* 957 * Copy the mbuf data into a contiguous buffer, leaving two 958 * bytes at the beginning to hold the frame length. 959 */ 960 m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2); 961 c->cue_mbuf = m; 962 963 total_len = m->m_pkthdr.len + 2; 964 965 DPRINTFN(10,("%s: %s: total_len=%d\n", 966 USBDEVNAME(sc->cue_dev), __func__, total_len)); 967 968 /* The first two bytes are the frame length */ 969 c->cue_buf[0] = (u_int8_t)m->m_pkthdr.len; 970 c->cue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8); 971 972 /* XXX 10000 */ 973 usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_TX], 974 c, c->cue_buf, total_len, USBD_NO_COPY, 10000, cue_txeof); 975 976 /* Transmit */ 977 err = usbd_transfer(c->cue_xfer); 978 if (err != USBD_IN_PROGRESS) { 979 printf("%s: cue_send error=%s\n", USBDEVNAME(sc->cue_dev), 980 usbd_errstr(err)); 981 /* Stop the interface from process context. */ 982 usb_add_task(sc->cue_udev, &sc->cue_stop_task); 983 return (EIO); 984 } 985 986 sc->cue_cdata.cue_tx_cnt++; 987 988 return (0); 989 } 990 991 Static void 992 cue_start(struct ifnet *ifp) 993 { 994 struct cue_softc *sc = ifp->if_softc; 995 struct mbuf *m_head = NULL; 996 997 if (sc->cue_dying) 998 return; 999 1000 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__func__)); 1001 1002 if (ifp->if_flags & IFF_OACTIVE) 1003 return; 1004 1005 IFQ_POLL(&ifp->if_snd, m_head); 1006 if (m_head == NULL) 1007 return; 1008 1009 if (cue_send(sc, m_head, 0)) { 1010 ifp->if_flags |= IFF_OACTIVE; 1011 return; 1012 } 1013 1014 IFQ_DEQUEUE(&ifp->if_snd, m_head); 1015 1016 #if NBPFILTER > 0 1017 /* 1018 * If there's a BPF listener, bounce a copy of this frame 1019 * to him. 1020 */ 1021 if (ifp->if_bpf) 1022 BPF_MTAP(ifp, m_head); 1023 #endif 1024 1025 ifp->if_flags |= IFF_OACTIVE; 1026 1027 /* 1028 * Set a timeout in case the chip goes out to lunch. 1029 */ 1030 ifp->if_timer = 5; 1031 } 1032 1033 Static void 1034 cue_init(void *xsc) 1035 { 1036 struct cue_softc *sc = xsc; 1037 struct ifnet *ifp = GET_IFP(sc); 1038 int i, s, ctl; 1039 u_char *eaddr; 1040 1041 if (sc->cue_dying) 1042 return; 1043 1044 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__func__)); 1045 1046 if (ifp->if_flags & IFF_RUNNING) 1047 return; 1048 1049 s = splnet(); 1050 1051 /* 1052 * Cancel pending I/O and free all RX/TX buffers. 1053 */ 1054 #if 1 1055 cue_reset(sc); 1056 #endif 1057 1058 /* Set advanced operation modes. */ 1059 cue_csr_write_1(sc, CUE_ADVANCED_OPMODES, 1060 CUE_AOP_EMBED_RXLEN | 0x03); /* 1 wait state */ 1061 1062 #if defined(__OpenBSD__) 1063 eaddr = sc->arpcom.ac_enaddr; 1064 #elif defined(__NetBSD__) 1065 eaddr = LLADDR(ifp->if_sadl); 1066 #endif 1067 /* Set MAC address */ 1068 for (i = 0; i < ETHER_ADDR_LEN; i++) 1069 cue_csr_write_1(sc, CUE_PAR0 - i, eaddr[i]); 1070 1071 /* Enable RX logic. */ 1072 ctl = CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON; 1073 if (ifp->if_flags & IFF_PROMISC) 1074 ctl |= CUE_ETHCTL_PROMISC; 1075 cue_csr_write_1(sc, CUE_ETHCTL, ctl); 1076 1077 /* Init TX ring. */ 1078 if (cue_tx_list_init(sc) == ENOBUFS) { 1079 printf("%s: tx list init failed\n", USBDEVNAME(sc->cue_dev)); 1080 splx(s); 1081 return; 1082 } 1083 1084 /* Init RX ring. */ 1085 if (cue_rx_list_init(sc) == ENOBUFS) { 1086 printf("%s: rx list init failed\n", USBDEVNAME(sc->cue_dev)); 1087 splx(s); 1088 return; 1089 } 1090 1091 /* Load the multicast filter. */ 1092 cue_setmulti(sc); 1093 1094 /* 1095 * Set the number of RX and TX buffers that we want 1096 * to reserve inside the ASIC. 1097 */ 1098 cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES); 1099 cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES); 1100 1101 /* Set advanced operation modes. */ 1102 cue_csr_write_1(sc, CUE_ADVANCED_OPMODES, 1103 CUE_AOP_EMBED_RXLEN | 0x01); /* 1 wait state */ 1104 1105 /* Program the LED operation. */ 1106 cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK); 1107 1108 if (sc->cue_ep[CUE_ENDPT_RX] == NULL) { 1109 if (cue_open_pipes(sc)) { 1110 splx(s); 1111 return; 1112 } 1113 } 1114 1115 ifp->if_flags |= IFF_RUNNING; 1116 ifp->if_flags &= ~IFF_OACTIVE; 1117 1118 splx(s); 1119 1120 usb_callout(sc->cue_stat_ch, hz, cue_tick, sc); 1121 } 1122 1123 Static int 1124 cue_open_pipes(struct cue_softc *sc) 1125 { 1126 struct cue_chain *c; 1127 usbd_status err; 1128 int i; 1129 1130 /* Open RX and TX pipes. */ 1131 err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX], 1132 USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]); 1133 if (err) { 1134 printf("%s: open rx pipe failed: %s\n", 1135 USBDEVNAME(sc->cue_dev), usbd_errstr(err)); 1136 return (EIO); 1137 } 1138 err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX], 1139 USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]); 1140 if (err) { 1141 printf("%s: open tx pipe failed: %s\n", 1142 USBDEVNAME(sc->cue_dev), usbd_errstr(err)); 1143 return (EIO); 1144 } 1145 1146 /* Start up the receive pipe. */ 1147 for (i = 0; i < CUE_RX_LIST_CNT; i++) { 1148 c = &sc->cue_cdata.cue_rx_chain[i]; 1149 usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX], 1150 c, c->cue_buf, CUE_BUFSZ, 1151 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, 1152 cue_rxeof); 1153 usbd_transfer(c->cue_xfer); 1154 } 1155 1156 return (0); 1157 } 1158 1159 Static int 1160 cue_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 1161 { 1162 struct cue_softc *sc = ifp->if_softc; 1163 struct ifaddr *ifa = (struct ifaddr *)data; 1164 struct ifreq *ifr = (struct ifreq *)data; 1165 int s, error = 0; 1166 1167 if (sc->cue_dying) 1168 return (EIO); 1169 1170 s = splnet(); 1171 1172 switch(command) { 1173 case SIOCSIFADDR: 1174 ifp->if_flags |= IFF_UP; 1175 cue_init(sc); 1176 1177 switch (ifa->ifa_addr->sa_family) { 1178 #ifdef INET 1179 case AF_INET: 1180 #if defined(__NetBSD__) 1181 arp_ifinit(ifp, ifa); 1182 #else 1183 arp_ifinit(&sc->arpcom, ifa); 1184 #endif 1185 break; 1186 #endif /* INET */ 1187 #ifdef NS 1188 case AF_NS: 1189 { 1190 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr; 1191 1192 if (ns_nullhost(*ina)) 1193 ina->x_host = *(union ns_host *) 1194 LLADDR(ifp->if_sadl); 1195 else 1196 memcpy(LLADDR(ifp->if_sadl), 1197 ina->x_host.c_host, 1198 ifp->if_addrlen); 1199 break; 1200 } 1201 #endif /* NS */ 1202 } 1203 break; 1204 1205 case SIOCSIFMTU: 1206 if (ifr->ifr_mtu > ETHERMTU) 1207 error = EINVAL; 1208 else 1209 ifp->if_mtu = ifr->ifr_mtu; 1210 break; 1211 1212 case SIOCSIFFLAGS: 1213 if (ifp->if_flags & IFF_UP) { 1214 if (ifp->if_flags & IFF_RUNNING && 1215 ifp->if_flags & IFF_PROMISC && 1216 !(sc->cue_if_flags & IFF_PROMISC)) { 1217 CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC); 1218 cue_setmulti(sc); 1219 } else if (ifp->if_flags & IFF_RUNNING && 1220 !(ifp->if_flags & IFF_PROMISC) && 1221 sc->cue_if_flags & IFF_PROMISC) { 1222 CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC); 1223 cue_setmulti(sc); 1224 } else if (!(ifp->if_flags & IFF_RUNNING)) 1225 cue_init(sc); 1226 } else { 1227 if (ifp->if_flags & IFF_RUNNING) 1228 cue_stop(sc); 1229 } 1230 sc->cue_if_flags = ifp->if_flags; 1231 error = 0; 1232 break; 1233 case SIOCADDMULTI: 1234 case SIOCDELMULTI: 1235 cue_setmulti(sc); 1236 error = 0; 1237 break; 1238 default: 1239 error = EINVAL; 1240 break; 1241 } 1242 1243 splx(s); 1244 1245 return (error); 1246 } 1247 1248 Static void 1249 cue_watchdog(struct ifnet *ifp) 1250 { 1251 struct cue_softc *sc = ifp->if_softc; 1252 struct cue_chain *c; 1253 usbd_status stat; 1254 int s; 1255 1256 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__func__)); 1257 1258 if (sc->cue_dying) 1259 return; 1260 1261 ifp->if_oerrors++; 1262 printf("%s: watchdog timeout\n", USBDEVNAME(sc->cue_dev)); 1263 1264 s = splusb(); 1265 c = &sc->cue_cdata.cue_tx_chain[0]; 1266 usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat); 1267 cue_txeof(c->cue_xfer, c, stat); 1268 1269 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1270 cue_start(ifp); 1271 splx(s); 1272 } 1273 1274 /* 1275 * Stop the adapter and free any mbufs allocated to the 1276 * RX and TX lists. 1277 */ 1278 Static void 1279 cue_stop(struct cue_softc *sc) 1280 { 1281 usbd_status err; 1282 struct ifnet *ifp; 1283 int i; 1284 1285 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__func__)); 1286 1287 ifp = GET_IFP(sc); 1288 ifp->if_timer = 0; 1289 1290 cue_csr_write_1(sc, CUE_ETHCTL, 0); 1291 cue_reset(sc); 1292 usb_uncallout(sc->cue_stat_ch, cue_tick, sc); 1293 1294 /* Stop transfers. */ 1295 if (sc->cue_ep[CUE_ENDPT_RX] != NULL) { 1296 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]); 1297 if (err) { 1298 printf("%s: abort rx pipe failed: %s\n", 1299 USBDEVNAME(sc->cue_dev), usbd_errstr(err)); 1300 } 1301 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]); 1302 if (err) { 1303 printf("%s: close rx pipe failed: %s\n", 1304 USBDEVNAME(sc->cue_dev), usbd_errstr(err)); 1305 } 1306 sc->cue_ep[CUE_ENDPT_RX] = NULL; 1307 } 1308 1309 if (sc->cue_ep[CUE_ENDPT_TX] != NULL) { 1310 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]); 1311 if (err) { 1312 printf("%s: abort tx pipe failed: %s\n", 1313 USBDEVNAME(sc->cue_dev), usbd_errstr(err)); 1314 } 1315 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]); 1316 if (err) { 1317 printf("%s: close tx pipe failed: %s\n", 1318 USBDEVNAME(sc->cue_dev), usbd_errstr(err)); 1319 } 1320 sc->cue_ep[CUE_ENDPT_TX] = NULL; 1321 } 1322 1323 if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) { 1324 err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]); 1325 if (err) { 1326 printf("%s: abort intr pipe failed: %s\n", 1327 USBDEVNAME(sc->cue_dev), usbd_errstr(err)); 1328 } 1329 err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]); 1330 if (err) { 1331 printf("%s: close intr pipe failed: %s\n", 1332 USBDEVNAME(sc->cue_dev), usbd_errstr(err)); 1333 } 1334 sc->cue_ep[CUE_ENDPT_INTR] = NULL; 1335 } 1336 1337 /* Free RX resources. */ 1338 for (i = 0; i < CUE_RX_LIST_CNT; i++) { 1339 if (sc->cue_cdata.cue_rx_chain[i].cue_mbuf != NULL) { 1340 m_freem(sc->cue_cdata.cue_rx_chain[i].cue_mbuf); 1341 sc->cue_cdata.cue_rx_chain[i].cue_mbuf = NULL; 1342 } 1343 if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) { 1344 usbd_free_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer); 1345 sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL; 1346 } 1347 } 1348 1349 /* Free TX resources. */ 1350 for (i = 0; i < CUE_TX_LIST_CNT; i++) { 1351 if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) { 1352 m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf); 1353 sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL; 1354 } 1355 if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) { 1356 usbd_free_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer); 1357 sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL; 1358 } 1359 } 1360 1361 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1362 } 1363