1 /* $OpenBSD: if_kue.c,v 1.23 2003/05/07 04:33:33 deraadt Exp $ */ 2 /* $NetBSD: if_kue.c,v 1.50 2002/07/16 22:00:31 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_kue.c,v 1.14 2000/01/14 01:36:15 wpaul Exp $ 35 */ 36 37 /* 38 * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver. 39 * 40 * Written by Bill Paul <wpaul@ee.columbia.edu> 41 * Electrical Engineering Department 42 * Columbia University, New York City 43 */ 44 45 /* 46 * The KLSI USB to ethernet adapter chip contains an USB serial interface, 47 * ethernet MAC and embedded microcontroller (called the QT Engine). 48 * The chip must have firmware loaded into it before it will operate. 49 * Packets are passed between the chip and host via bulk transfers. 50 * There is an interrupt endpoint mentioned in the software spec, however 51 * it's currently unused. This device is 10Mbps half-duplex only, hence 52 * there is no media selection logic. The MAC supports a 128 entry 53 * multicast filter, though the exact size of the filter can depend 54 * on the firmware. Curiously, while the software spec describes various 55 * ethernet statistics counters, my sample adapter and firmware combination 56 * claims not to support any statistics counters at all. 57 * 58 * Note that once we load the firmware in the device, we have to be 59 * careful not to load it again: if you restart your computer but 60 * leave the adapter attached to the USB controller, it may remain 61 * powered on and retain its firmware. In this case, we don't need 62 * to load the firmware a second time. 63 * 64 * Special thanks to Rob Furr for providing an ADS Technologies 65 * adapter for development and testing. No monkeys were harmed during 66 * the development of this driver. 67 */ 68 69 /* 70 * Ported to NetBSD and somewhat rewritten by Lennart Augustsson. 71 */ 72 73 #if defined(__NetBSD__) 74 #include "opt_inet.h" 75 #include "opt_ns.h" 76 #include "bpfilter.h" 77 #include "rnd.h" 78 #elif defined(__OpenBSD__) 79 #include "bpfilter.h" 80 #endif 81 82 #include <sys/param.h> 83 #include <sys/systm.h> 84 #include <sys/sockio.h> 85 #include <sys/mbuf.h> 86 #include <sys/malloc.h> 87 #include <sys/kernel.h> 88 #include <sys/socket.h> 89 #include <sys/device.h> 90 #include <sys/proc.h> 91 92 #if NRND > 0 93 #include <sys/rnd.h> 94 #endif 95 96 #include <net/if.h> 97 #if defined(__NetBSD__) 98 #include <net/if_arp.h> 99 #endif 100 #include <net/if_dl.h> 101 102 #if NBPFILTER > 0 103 #include <net/bpf.h> 104 #endif 105 106 #if defined(__NetBSD__) 107 #include <net/if_ether.h> 108 #ifdef INET 109 #include <netinet/in.h> 110 #include <netinet/if_inarp.h> 111 #endif 112 #endif /* defined (__NetBSD__) */ 113 114 #if defined(__OpenBSD__) 115 #ifdef INET 116 #include <netinet/in.h> 117 #include <netinet/in_systm.h> 118 #include <netinet/in_var.h> 119 #include <netinet/ip.h> 120 #include <netinet/if_ether.h> 121 #endif 122 #endif /* defined (__OpenBSD__) */ 123 124 #ifdef NS 125 #include <netns/ns.h> 126 #include <netns/ns_if.h> 127 #endif 128 129 #include <dev/usb/usb.h> 130 #include <dev/usb/usbdi.h> 131 #include <dev/usb/usbdi_util.h> 132 #include <dev/usb/usbdevs.h> 133 134 #include <dev/usb/if_kuereg.h> 135 136 #if defined(__OpenBSD__) 137 #include <dev/microcode/kue/kue_fw.h> 138 #else 139 #include <dev/usb/kue_fw.h> 140 #endif 141 142 #ifdef KUE_DEBUG 143 #define DPRINTF(x) if (kuedebug) logprintf x 144 #define DPRINTFN(n,x) if (kuedebug >= (n)) logprintf x 145 int kuedebug = 0; 146 #else 147 #define DPRINTF(x) 148 #define DPRINTFN(n,x) 149 #endif 150 151 /* 152 * Various supported device vendors/products. 153 */ 154 Static const struct usb_devno kue_devs[] = { 155 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 }, 156 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460 }, 157 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450 }, 158 { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT }, 159 { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BTX }, 160 { USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 }, 161 { USB_VENDOR_ASANTE, USB_PRODUCT_ASANTE_EA }, 162 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T }, 163 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_DSB650C }, 164 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T }, 165 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C }, 166 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 }, 167 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX1 }, 168 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX2 }, 169 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETT }, 170 { USB_VENDOR_JATON, USB_PRODUCT_JATON_EDA }, 171 { USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_XX1 }, 172 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT }, 173 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BTN }, 174 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T }, 175 { USB_VENDOR_MOBILITY, USB_PRODUCT_MOBILITY_EA }, 176 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 }, 177 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101X }, 178 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET }, 179 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 }, 180 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3 }, 181 { USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA8 }, 182 { USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA9 }, 183 { USB_VENDOR_PORTSMITH, USB_PRODUCT_PORTSMITH_EEA }, 184 { USB_VENDOR_SHARK, USB_PRODUCT_SHARK_PA }, 185 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB }, 186 }; 187 #define kue_lookup(v, p) (usb_lookup(kue_devs, v, p)) 188 189 USB_DECLARE_DRIVER(kue); 190 191 Static int kue_tx_list_init(struct kue_softc *); 192 Static int kue_rx_list_init(struct kue_softc *); 193 Static int kue_newbuf(struct kue_softc *, struct kue_chain *,struct mbuf *); 194 Static int kue_send(struct kue_softc *, struct mbuf *, int); 195 Static int kue_open_pipes(struct kue_softc *); 196 Static void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 197 Static void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 198 Static void kue_start(struct ifnet *); 199 Static int kue_ioctl(struct ifnet *, u_long, caddr_t); 200 Static void kue_init(void *); 201 Static void kue_stop(struct kue_softc *); 202 Static void kue_watchdog(struct ifnet *); 203 204 Static void kue_setmulti(struct kue_softc *); 205 Static void kue_reset(struct kue_softc *); 206 207 Static usbd_status kue_ctl(struct kue_softc *, int, u_int8_t, 208 u_int16_t, void *, u_int32_t); 209 Static usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t); 210 Static int kue_load_fw(struct kue_softc *); 211 212 Static usbd_status 213 kue_setword(struct kue_softc *sc, u_int8_t breq, u_int16_t word) 214 { 215 usb_device_request_t req; 216 217 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__)); 218 219 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 220 req.bRequest = breq; 221 USETW(req.wValue, word); 222 USETW(req.wIndex, 0); 223 USETW(req.wLength, 0); 224 225 return (usbd_do_request(sc->kue_udev, &req, NULL)); 226 } 227 228 Static usbd_status 229 kue_ctl(struct kue_softc *sc, int rw, u_int8_t breq, u_int16_t val, 230 void *data, u_int32_t len) 231 { 232 usb_device_request_t req; 233 234 DPRINTFN(10,("%s: %s: enter, len=%d\n", USBDEVNAME(sc->kue_dev), 235 __func__, len)); 236 237 if (rw == KUE_CTL_WRITE) 238 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 239 else 240 req.bmRequestType = UT_READ_VENDOR_DEVICE; 241 242 req.bRequest = breq; 243 USETW(req.wValue, val); 244 USETW(req.wIndex, 0); 245 USETW(req.wLength, len); 246 247 return (usbd_do_request(sc->kue_udev, &req, data)); 248 } 249 250 Static int 251 kue_load_fw(struct kue_softc *sc) 252 { 253 usb_device_descriptor_t dd; 254 usbd_status err; 255 256 DPRINTFN(1,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __func__)); 257 258 /* 259 * First, check if we even need to load the firmware. 260 * If the device was still attached when the system was 261 * rebooted, it may already have firmware loaded in it. 262 * If this is the case, we don't need to do it again. 263 * And in fact, if we try to load it again, we'll hang, 264 * so we have to avoid this condition if we don't want 265 * to look stupid. 266 * 267 * We can test this quickly by checking the bcdRevision 268 * code. The NIC will return a different revision code if 269 * it's probed while the firmware is still loaded and 270 * running. 271 */ 272 if (usbd_get_device_desc(sc->kue_udev, &dd)) 273 return (EIO); 274 if (UGETW(dd.bcdDevice) == KUE_WARM_REV) { 275 printf("%s: warm boot, no firmware download\n", 276 USBDEVNAME(sc->kue_dev)); 277 return (0); 278 } 279 280 printf("%s: cold boot, downloading firmware\n", 281 USBDEVNAME(sc->kue_dev)); 282 283 /* Load code segment */ 284 DPRINTFN(1,("%s: kue_load_fw: download code_seg\n", 285 USBDEVNAME(sc->kue_dev))); 286 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 287 0, (void *)kue_code_seg, sizeof(kue_code_seg)); 288 if (err) { 289 printf("%s: failed to load code segment: %s\n", 290 USBDEVNAME(sc->kue_dev), usbd_errstr(err)); 291 return (EIO); 292 } 293 294 /* Load fixup segment */ 295 DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n", 296 USBDEVNAME(sc->kue_dev))); 297 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 298 0, (void *)kue_fix_seg, sizeof(kue_fix_seg)); 299 if (err) { 300 printf("%s: failed to load fixup segment: %s\n", 301 USBDEVNAME(sc->kue_dev), usbd_errstr(err)); 302 return (EIO); 303 } 304 305 /* Send trigger command. */ 306 DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n", 307 USBDEVNAME(sc->kue_dev))); 308 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 309 0, (void *)kue_trig_seg, sizeof(kue_trig_seg)); 310 if (err) { 311 printf("%s: failed to load trigger segment: %s\n", 312 USBDEVNAME(sc->kue_dev), usbd_errstr(err)); 313 return (EIO); 314 } 315 316 usbd_delay_ms(sc->kue_udev, 10); 317 318 /* 319 * Reload device descriptor. 320 * Why? The chip without the firmware loaded returns 321 * one revision code. The chip with the firmware 322 * loaded and running returns a *different* revision 323 * code. This confuses the quirk mechanism, which is 324 * dependent on the revision data. 325 */ 326 (void)usbd_reload_device_desc(sc->kue_udev); 327 328 DPRINTFN(1,("%s: %s: done\n", USBDEVNAME(sc->kue_dev), __func__)); 329 330 /* Reset the adapter. */ 331 kue_reset(sc); 332 333 return (0); 334 } 335 336 Static void 337 kue_setmulti(struct kue_softc *sc) 338 { 339 struct ifnet *ifp = GET_IFP(sc); 340 struct ether_multi *enm; 341 struct ether_multistep step; 342 int i; 343 344 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __func__)); 345 346 if (ifp->if_flags & IFF_PROMISC) { 347 allmulti: 348 ifp->if_flags |= IFF_ALLMULTI; 349 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI; 350 sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST; 351 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); 352 return; 353 } 354 355 sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI; 356 357 i = 0; 358 #if defined (__NetBSD__) 359 ETHER_FIRST_MULTI(step, &sc->kue_ec, enm); 360 #else 361 ETHER_FIRST_MULTI(step, &sc->arpcom, enm); 362 #endif 363 while (enm != NULL) { 364 if (i == KUE_MCFILTCNT(sc) || 365 memcmp(enm->enm_addrlo, enm->enm_addrhi, 366 ETHER_ADDR_LEN) != 0) 367 goto allmulti; 368 369 memcpy(KUE_MCFILT(sc, i), enm->enm_addrlo, ETHER_ADDR_LEN); 370 ETHER_NEXT_MULTI(step, enm); 371 i++; 372 } 373 374 ifp->if_flags &= ~IFF_ALLMULTI; 375 376 sc->kue_rxfilt |= KUE_RXFILT_MULTICAST; 377 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS, 378 i, sc->kue_mcfilters, i * ETHER_ADDR_LEN); 379 380 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); 381 } 382 383 /* 384 * Issue a SET_CONFIGURATION command to reset the MAC. This should be 385 * done after the firmware is loaded into the adapter in order to 386 * bring it into proper operation. 387 */ 388 Static void 389 kue_reset(struct kue_softc *sc) 390 { 391 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __func__)); 392 393 if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 1) || 394 usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX, 395 &sc->kue_iface)) 396 printf("%s: reset failed\n", USBDEVNAME(sc->kue_dev)); 397 398 /* Wait a little while for the chip to get its brains in order. */ 399 usbd_delay_ms(sc->kue_udev, 10); 400 } 401 402 /* 403 * Probe for a KLSI chip. 404 */ 405 USB_MATCH(kue) 406 { 407 USB_MATCH_START(kue, uaa); 408 409 DPRINTFN(25,("kue_match: enter\n")); 410 411 if (uaa->iface != NULL) 412 return (UMATCH_NONE); 413 414 return (kue_lookup(uaa->vendor, uaa->product) != NULL ? 415 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 416 } 417 418 /* 419 * Attach the interface. Allocate softc structures, do 420 * setup and ethernet/BPF attach. 421 */ 422 USB_ATTACH(kue) 423 { 424 USB_ATTACH_START(kue, sc, uaa); 425 char devinfo[1024]; 426 int s; 427 struct ifnet *ifp; 428 usbd_device_handle dev = uaa->device; 429 usbd_interface_handle iface; 430 usbd_status err; 431 usb_interface_descriptor_t *id; 432 usb_endpoint_descriptor_t *ed; 433 int i; 434 435 DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev)); 436 437 usbd_devinfo(dev, 0, devinfo, sizeof devinfo); 438 USB_ATTACH_SETUP; 439 printf("%s: %s\n", USBDEVNAME(sc->kue_dev), devinfo); 440 441 err = usbd_set_config_no(dev, KUE_CONFIG_NO, 1); 442 if (err) { 443 printf("%s: setting config no failed\n", 444 USBDEVNAME(sc->kue_dev)); 445 USB_ATTACH_ERROR_RETURN; 446 } 447 448 sc->kue_udev = dev; 449 sc->kue_product = uaa->product; 450 sc->kue_vendor = uaa->vendor; 451 452 /* Load the firmware into the NIC. */ 453 if (kue_load_fw(sc)) { 454 printf("%s: loading firmware failed\n", 455 USBDEVNAME(sc->kue_dev)); 456 USB_ATTACH_ERROR_RETURN; 457 } 458 459 err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface); 460 if (err) { 461 printf("%s: getting interface handle failed\n", 462 USBDEVNAME(sc->kue_dev)); 463 USB_ATTACH_ERROR_RETURN; 464 } 465 466 sc->kue_iface = iface; 467 id = usbd_get_interface_descriptor(iface); 468 469 /* Find endpoints. */ 470 for (i = 0; i < id->bNumEndpoints; i++) { 471 ed = usbd_interface2endpoint_descriptor(iface, i); 472 if (ed == NULL) { 473 printf("%s: couldn't get ep %d\n", 474 USBDEVNAME(sc->kue_dev), i); 475 USB_ATTACH_ERROR_RETURN; 476 } 477 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 478 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 479 sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress; 480 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 481 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 482 sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress; 483 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 484 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 485 sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress; 486 } 487 } 488 489 if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) { 490 printf("%s: missing endpoint\n", USBDEVNAME(sc->kue_dev)); 491 USB_ATTACH_ERROR_RETURN; 492 } 493 494 /* Read ethernet descriptor */ 495 err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR, 496 0, &sc->kue_desc, sizeof(sc->kue_desc)); 497 if (err) { 498 printf("%s: could not read Ethernet descriptor\n", 499 USBDEVNAME(sc->kue_dev)); 500 USB_ATTACH_ERROR_RETURN; 501 } 502 503 sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN, 504 M_USBDEV, M_NOWAIT); 505 if (sc->kue_mcfilters == NULL) { 506 printf("%s: no memory for multicast filter buffer\n", 507 USBDEVNAME(sc->kue_dev)); 508 USB_ATTACH_ERROR_RETURN; 509 } 510 511 s = splnet(); 512 513 /* 514 * A KLSI chip was detected. Inform the world. 515 */ 516 printf("%s: address %s\n", USBDEVNAME(sc->kue_dev), 517 ether_sprintf(sc->kue_desc.kue_macaddr)); 518 519 #if defined(__OpenBSD__) 520 bcopy(sc->kue_desc.kue_macaddr, 521 (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 522 #endif 523 524 /* Initialize interface info.*/ 525 ifp = GET_IFP(sc); 526 ifp->if_softc = sc; 527 ifp->if_mtu = ETHERMTU; 528 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 529 ifp->if_ioctl = kue_ioctl; 530 ifp->if_start = kue_start; 531 ifp->if_watchdog = kue_watchdog; 532 strncpy(ifp->if_xname, USBDEVNAME(sc->kue_dev), IFNAMSIZ); 533 534 IFQ_SET_READY(&ifp->if_snd); 535 536 /* Attach the interface. */ 537 if_attach(ifp); 538 Ether_ifattach(ifp, sc->kue_desc.kue_macaddr); 539 #if NRND > 0 540 rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->kue_dev), 541 RND_TYPE_NET, 0); 542 #endif 543 544 sc->kue_attached = 1; 545 splx(s); 546 547 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->kue_udev, 548 USBDEV(sc->kue_dev)); 549 550 USB_ATTACH_SUCCESS_RETURN; 551 } 552 553 USB_DETACH(kue) 554 { 555 USB_DETACH_START(kue, sc); 556 struct ifnet *ifp = GET_IFP(sc); 557 int s; 558 559 s = splusb(); /* XXX why? */ 560 561 if (sc->kue_mcfilters != NULL) { 562 free(sc->kue_mcfilters, M_USBDEV); 563 sc->kue_mcfilters = NULL; 564 } 565 566 if (!sc->kue_attached) { 567 /* Detached before attached finished, so just bail out. */ 568 splx(s); 569 return (0); 570 } 571 572 if (ifp->if_flags & IFF_RUNNING) 573 kue_stop(sc); 574 575 #if defined(__NetBSD__) 576 #if NRND > 0 577 rnd_detach_source(&sc->rnd_source); 578 #endif 579 #endif /* __NetBSD__ */ 580 ether_ifdetach(ifp); 581 582 if_detach(ifp); 583 584 #ifdef DIAGNOSTIC 585 if (sc->kue_ep[KUE_ENDPT_TX] != NULL || 586 sc->kue_ep[KUE_ENDPT_RX] != NULL || 587 sc->kue_ep[KUE_ENDPT_INTR] != NULL) 588 printf("%s: detach has active endpoints\n", 589 USBDEVNAME(sc->kue_dev)); 590 #endif 591 592 sc->kue_attached = 0; 593 splx(s); 594 595 return (0); 596 } 597 598 int 599 kue_activate(device_ptr_t self, enum devact act) 600 { 601 struct kue_softc *sc = (struct kue_softc *)self; 602 603 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __func__)); 604 605 switch (act) { 606 case DVACT_ACTIVATE: 607 return (EOPNOTSUPP); 608 break; 609 610 case DVACT_DEACTIVATE: 611 #if defined(__NetBSD__) 612 /* Deactivate the interface. */ 613 if_deactivate(&sc->kue_ec.ec_if); 614 #endif 615 sc->kue_dying = 1; 616 break; 617 } 618 return (0); 619 } 620 621 /* 622 * Initialize an RX descriptor and attach an MBUF cluster. 623 */ 624 Static int 625 kue_newbuf(struct kue_softc *sc, struct kue_chain *c, struct mbuf *m) 626 { 627 struct mbuf *m_new = NULL; 628 629 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__)); 630 631 if (m == NULL) { 632 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 633 if (m_new == NULL) { 634 printf("%s: no memory for rx list " 635 "-- packet dropped!\n", USBDEVNAME(sc->kue_dev)); 636 return (ENOBUFS); 637 } 638 639 MCLGET(m_new, M_DONTWAIT); 640 if (!(m_new->m_flags & M_EXT)) { 641 printf("%s: no memory for rx list " 642 "-- packet dropped!\n", USBDEVNAME(sc->kue_dev)); 643 m_freem(m_new); 644 return (ENOBUFS); 645 } 646 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 647 } else { 648 m_new = m; 649 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 650 m_new->m_data = m_new->m_ext.ext_buf; 651 } 652 653 c->kue_mbuf = m_new; 654 655 return (0); 656 } 657 658 Static int 659 kue_rx_list_init(struct kue_softc *sc) 660 { 661 struct kue_cdata *cd; 662 struct kue_chain *c; 663 int i; 664 665 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __func__)); 666 667 cd = &sc->kue_cdata; 668 for (i = 0; i < KUE_RX_LIST_CNT; i++) { 669 c = &cd->kue_rx_chain[i]; 670 c->kue_sc = sc; 671 c->kue_idx = i; 672 if (kue_newbuf(sc, c, NULL) == ENOBUFS) 673 return (ENOBUFS); 674 if (c->kue_xfer == NULL) { 675 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev); 676 if (c->kue_xfer == NULL) 677 return (ENOBUFS); 678 c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ); 679 if (c->kue_buf == NULL) 680 return (ENOBUFS); /* XXX free xfer */ 681 } 682 } 683 684 return (0); 685 } 686 687 Static int 688 kue_tx_list_init(struct kue_softc *sc) 689 { 690 struct kue_cdata *cd; 691 struct kue_chain *c; 692 int i; 693 694 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __func__)); 695 696 cd = &sc->kue_cdata; 697 for (i = 0; i < KUE_TX_LIST_CNT; i++) { 698 c = &cd->kue_tx_chain[i]; 699 c->kue_sc = sc; 700 c->kue_idx = i; 701 c->kue_mbuf = NULL; 702 if (c->kue_xfer == NULL) { 703 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev); 704 if (c->kue_xfer == NULL) 705 return (ENOBUFS); 706 c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ); 707 if (c->kue_buf == NULL) 708 return (ENOBUFS); 709 } 710 } 711 712 return (0); 713 } 714 715 /* 716 * A frame has been uploaded: pass the resulting mbuf chain up to 717 * the higher level protocols. 718 */ 719 Static void 720 kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 721 { 722 struct kue_chain *c = priv; 723 struct kue_softc *sc = c->kue_sc; 724 struct ifnet *ifp = GET_IFP(sc); 725 struct mbuf *m; 726 int total_len = 0; 727 int s; 728 729 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev), 730 __func__, status)); 731 732 if (sc->kue_dying) 733 return; 734 735 if (!(ifp->if_flags & IFF_RUNNING)) 736 return; 737 738 if (status != USBD_NORMAL_COMPLETION) { 739 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 740 return; 741 sc->kue_rx_errs++; 742 if (usbd_ratecheck(&sc->kue_rx_notice)) { 743 printf("%s: %u usb errors on rx: %s\n", 744 USBDEVNAME(sc->kue_dev), sc->kue_rx_errs, 745 usbd_errstr(status)); 746 sc->kue_rx_errs = 0; 747 } 748 if (status == USBD_STALLED) 749 usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_RX]); 750 goto done; 751 } 752 753 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 754 755 DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", USBDEVNAME(sc->kue_dev), 756 __func__, total_len, 757 UGETW(mtod(c->kue_mbuf, u_int8_t *)))); 758 759 if (total_len <= 1) 760 goto done; 761 762 m = c->kue_mbuf; 763 /* copy data to mbuf */ 764 memcpy(mtod(m, char *), c->kue_buf, total_len); 765 766 /* No errors; receive the packet. */ 767 total_len = UGETW(mtod(m, u_int8_t *)); 768 m_adj(m, sizeof(u_int16_t)); 769 770 if (total_len < sizeof(struct ether_header)) { 771 ifp->if_ierrors++; 772 goto done; 773 } 774 775 ifp->if_ipackets++; 776 m->m_pkthdr.len = m->m_len = total_len; 777 778 m->m_pkthdr.rcvif = ifp; 779 780 s = splnet(); 781 782 /* XXX ugly */ 783 if (kue_newbuf(sc, c, NULL) == ENOBUFS) { 784 ifp->if_ierrors++; 785 goto done1; 786 } 787 788 #if NBPFILTER > 0 789 /* 790 * Handle BPF listeners. Let the BPF user see the packet, but 791 * don't pass it up to the ether_input() layer unless it's 792 * a broadcast packet, multicast packet, matches our ethernet 793 * address or the interface is in promiscuous mode. 794 */ 795 if (ifp->if_bpf) 796 bpf_mtap(ifp->if_bpf, m); 797 #endif 798 799 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->kue_dev), 800 __func__, m->m_len)); 801 IF_INPUT(ifp, m); 802 done1: 803 splx(s); 804 805 done: 806 807 /* Setup new transfer. */ 808 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX], 809 c, c->kue_buf, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, 810 USBD_NO_TIMEOUT, kue_rxeof); 811 usbd_transfer(c->kue_xfer); 812 813 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->kue_dev), 814 __func__)); 815 } 816 817 /* 818 * A frame was downloaded to the chip. It's safe for us to clean up 819 * the list buffers. 820 */ 821 822 Static void 823 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 824 { 825 struct kue_chain *c = priv; 826 struct kue_softc *sc = c->kue_sc; 827 struct ifnet *ifp = GET_IFP(sc); 828 int s; 829 830 if (sc->kue_dying) 831 return; 832 833 s = splnet(); 834 835 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev), 836 __func__, status)); 837 838 ifp->if_timer = 0; 839 ifp->if_flags &= ~IFF_OACTIVE; 840 841 if (status != USBD_NORMAL_COMPLETION) { 842 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 843 splx(s); 844 return; 845 } 846 ifp->if_oerrors++; 847 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->kue_dev), 848 usbd_errstr(status)); 849 if (status == USBD_STALLED) 850 usbd_clear_endpoint_stall(sc->kue_ep[KUE_ENDPT_TX]); 851 splx(s); 852 return; 853 } 854 855 ifp->if_opackets++; 856 857 m_freem(c->kue_mbuf); 858 c->kue_mbuf = NULL; 859 860 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 861 kue_start(ifp); 862 863 splx(s); 864 } 865 866 Static int 867 kue_send(struct kue_softc *sc, struct mbuf *m, int idx) 868 { 869 int total_len; 870 struct kue_chain *c; 871 usbd_status err; 872 873 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__)); 874 875 c = &sc->kue_cdata.kue_tx_chain[idx]; 876 877 /* 878 * Copy the mbuf data into a contiguous buffer, leaving two 879 * bytes at the beginning to hold the frame length. 880 */ 881 m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2); 882 c->kue_mbuf = m; 883 884 total_len = m->m_pkthdr.len + 2; 885 /* XXX what's this? */ 886 total_len += 64 - (total_len % 64); 887 888 /* Frame length is specified in the first 2 bytes of the buffer. */ 889 c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len; 890 c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8); 891 892 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX], 893 c, c->kue_buf, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT, 894 kue_txeof); 895 896 /* Transmit */ 897 err = usbd_transfer(c->kue_xfer); 898 if (err != USBD_IN_PROGRESS) { 899 printf("%s: kue_send error=%s\n", USBDEVNAME(sc->kue_dev), 900 usbd_errstr(err)); 901 kue_stop(sc); 902 return (EIO); 903 } 904 905 sc->kue_cdata.kue_tx_cnt++; 906 907 return (0); 908 } 909 910 Static void 911 kue_start(struct ifnet *ifp) 912 { 913 struct kue_softc *sc = ifp->if_softc; 914 struct mbuf *m_head = NULL; 915 916 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__)); 917 918 if (sc->kue_dying) 919 return; 920 921 if (ifp->if_flags & IFF_OACTIVE) 922 return; 923 924 IFQ_POLL(&ifp->if_snd, m_head); 925 if (m_head == NULL) 926 return; 927 928 if (kue_send(sc, m_head, 0)) { 929 ifp->if_flags |= IFF_OACTIVE; 930 return; 931 } 932 933 IFQ_DEQUEUE(&ifp->if_snd, m_head); 934 935 #if NBPFILTER > 0 936 /* 937 * If there's a BPF listener, bounce a copy of this frame 938 * to him. 939 */ 940 if (ifp->if_bpf) 941 bpf_mtap(ifp->if_bpf, m_head); 942 #endif 943 944 ifp->if_flags |= IFF_OACTIVE; 945 946 /* 947 * Set a timeout in case the chip goes out to lunch. 948 */ 949 ifp->if_timer = 6; 950 } 951 952 Static void 953 kue_init(void *xsc) 954 { 955 struct kue_softc *sc = xsc; 956 struct ifnet *ifp = GET_IFP(sc); 957 int s; 958 u_char *eaddr; 959 960 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__)); 961 962 if (ifp->if_flags & IFF_RUNNING) 963 return; 964 965 s = splnet(); 966 967 #if defined(__NetBSD__) 968 eaddr = LLADDR(ifp->if_sadl); 969 #else 970 eaddr = sc->arpcom.ac_enaddr; 971 #endif /* defined(__NetBSD__) */ 972 /* Set MAC address */ 973 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN); 974 975 sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST; 976 977 /* If we want promiscuous mode, set the allframes bit. */ 978 if (ifp->if_flags & IFF_PROMISC) 979 sc->kue_rxfilt |= KUE_RXFILT_PROMISC; 980 981 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); 982 983 /* I'm not sure how to tune these. */ 984 #if 0 985 /* 986 * Leave this one alone for now; setting it 987 * wrong causes lockups on some machines/controllers. 988 */ 989 kue_setword(sc, KUE_CMD_SET_SOFS, 1); 990 #endif 991 kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64); 992 993 /* Init TX ring. */ 994 if (kue_tx_list_init(sc) == ENOBUFS) { 995 printf("%s: tx list init failed\n", USBDEVNAME(sc->kue_dev)); 996 splx(s); 997 return; 998 } 999 1000 /* Init RX ring. */ 1001 if (kue_rx_list_init(sc) == ENOBUFS) { 1002 printf("%s: rx list init failed\n", USBDEVNAME(sc->kue_dev)); 1003 splx(s); 1004 return; 1005 } 1006 1007 /* Load the multicast filter. */ 1008 kue_setmulti(sc); 1009 1010 if (sc->kue_ep[KUE_ENDPT_RX] == NULL) { 1011 if (kue_open_pipes(sc)) { 1012 splx(s); 1013 return; 1014 } 1015 } 1016 1017 ifp->if_flags |= IFF_RUNNING; 1018 ifp->if_flags &= ~IFF_OACTIVE; 1019 1020 splx(s); 1021 } 1022 1023 Static int 1024 kue_open_pipes(struct kue_softc *sc) 1025 { 1026 usbd_status err; 1027 struct kue_chain *c; 1028 int i; 1029 1030 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__)); 1031 1032 /* Open RX and TX pipes. */ 1033 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX], 1034 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]); 1035 if (err) { 1036 printf("%s: open rx pipe failed: %s\n", 1037 USBDEVNAME(sc->kue_dev), usbd_errstr(err)); 1038 return (EIO); 1039 } 1040 1041 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX], 1042 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]); 1043 if (err) { 1044 printf("%s: open tx pipe failed: %s\n", 1045 USBDEVNAME(sc->kue_dev), usbd_errstr(err)); 1046 return (EIO); 1047 } 1048 1049 /* Start up the receive pipe. */ 1050 for (i = 0; i < KUE_RX_LIST_CNT; i++) { 1051 c = &sc->kue_cdata.kue_rx_chain[i]; 1052 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX], 1053 c, c->kue_buf, KUE_BUFSZ, 1054 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, 1055 kue_rxeof); 1056 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->kue_dev), 1057 __func__)); 1058 usbd_transfer(c->kue_xfer); 1059 } 1060 1061 return (0); 1062 } 1063 1064 Static int 1065 kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 1066 { 1067 struct kue_softc *sc = ifp->if_softc; 1068 struct ifaddr *ifa = (struct ifaddr *)data; 1069 struct ifreq *ifr = (struct ifreq *)data; 1070 int s, error = 0; 1071 1072 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__)); 1073 1074 if (sc->kue_dying) 1075 return (EIO); 1076 1077 #ifdef DIAGNOSTIC 1078 if (!curproc) { 1079 printf("%s: no proc!!\n", USBDEVNAME(sc->kue_dev)); 1080 return EIO; 1081 } 1082 #endif 1083 1084 s = splnet(); 1085 1086 switch(command) { 1087 case SIOCSIFADDR: 1088 ifp->if_flags |= IFF_UP; 1089 kue_init(sc); 1090 1091 switch (ifa->ifa_addr->sa_family) { 1092 #ifdef INET 1093 case AF_INET: 1094 #if defined(__NetBSD__) 1095 arp_ifinit(ifp, ifa); 1096 #else 1097 arp_ifinit(&sc->arpcom, ifa); 1098 #endif 1099 break; 1100 #endif /* INET */ 1101 #ifdef NS 1102 case AF_NS: 1103 { 1104 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr; 1105 1106 if (ns_nullhost(*ina)) 1107 ina->x_host = *(union ns_host *) 1108 LLADDR(ifp->if_sadl); 1109 else 1110 memcpy(LLADDR(ifp->if_sadl), 1111 ina->x_host.c_host, 1112 ifp->if_addrlen); 1113 break; 1114 } 1115 #endif /* NS */ 1116 } 1117 break; 1118 1119 case SIOCSIFMTU: 1120 if (ifr->ifr_mtu > ETHERMTU) 1121 error = EINVAL; 1122 else 1123 ifp->if_mtu = ifr->ifr_mtu; 1124 break; 1125 1126 case SIOCSIFFLAGS: 1127 if (ifp->if_flags & IFF_UP) { 1128 if (ifp->if_flags & IFF_RUNNING && 1129 ifp->if_flags & IFF_PROMISC && 1130 !(sc->kue_if_flags & IFF_PROMISC)) { 1131 sc->kue_rxfilt |= KUE_RXFILT_PROMISC; 1132 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, 1133 sc->kue_rxfilt); 1134 } else if (ifp->if_flags & IFF_RUNNING && 1135 !(ifp->if_flags & IFF_PROMISC) && 1136 sc->kue_if_flags & IFF_PROMISC) { 1137 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC; 1138 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, 1139 sc->kue_rxfilt); 1140 } else if (!(ifp->if_flags & IFF_RUNNING)) 1141 kue_init(sc); 1142 } else { 1143 if (ifp->if_flags & IFF_RUNNING) 1144 kue_stop(sc); 1145 } 1146 sc->kue_if_flags = ifp->if_flags; 1147 error = 0; 1148 break; 1149 case SIOCADDMULTI: 1150 case SIOCDELMULTI: 1151 kue_setmulti(sc); 1152 error = 0; 1153 break; 1154 default: 1155 error = EINVAL; 1156 break; 1157 } 1158 1159 splx(s); 1160 1161 return (error); 1162 } 1163 1164 Static void 1165 kue_watchdog(struct ifnet *ifp) 1166 { 1167 struct kue_softc *sc = ifp->if_softc; 1168 struct kue_chain *c; 1169 usbd_status stat; 1170 int s; 1171 1172 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__)); 1173 1174 if (sc->kue_dying) 1175 return; 1176 1177 ifp->if_oerrors++; 1178 printf("%s: watchdog timeout\n", USBDEVNAME(sc->kue_dev)); 1179 1180 s = splusb(); 1181 c = &sc->kue_cdata.kue_tx_chain[0]; 1182 usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat); 1183 kue_txeof(c->kue_xfer, c, stat); 1184 1185 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1186 kue_start(ifp); 1187 splx(s); 1188 } 1189 1190 /* 1191 * Stop the adapter and free any mbufs allocated to the 1192 * RX and TX lists. 1193 */ 1194 Static void 1195 kue_stop(struct kue_softc *sc) 1196 { 1197 usbd_status err; 1198 struct ifnet *ifp; 1199 int i; 1200 1201 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__)); 1202 1203 ifp = GET_IFP(sc); 1204 ifp->if_timer = 0; 1205 1206 /* Stop transfers. */ 1207 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) { 1208 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]); 1209 if (err) { 1210 printf("%s: abort rx pipe failed: %s\n", 1211 USBDEVNAME(sc->kue_dev), usbd_errstr(err)); 1212 } 1213 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]); 1214 if (err) { 1215 printf("%s: close rx pipe failed: %s\n", 1216 USBDEVNAME(sc->kue_dev), usbd_errstr(err)); 1217 } 1218 sc->kue_ep[KUE_ENDPT_RX] = NULL; 1219 } 1220 1221 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) { 1222 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]); 1223 if (err) { 1224 printf("%s: abort tx pipe failed: %s\n", 1225 USBDEVNAME(sc->kue_dev), usbd_errstr(err)); 1226 } 1227 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]); 1228 if (err) { 1229 printf("%s: close tx pipe failed: %s\n", 1230 USBDEVNAME(sc->kue_dev), usbd_errstr(err)); 1231 } 1232 sc->kue_ep[KUE_ENDPT_TX] = NULL; 1233 } 1234 1235 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) { 1236 err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]); 1237 if (err) { 1238 printf("%s: abort intr pipe failed: %s\n", 1239 USBDEVNAME(sc->kue_dev), usbd_errstr(err)); 1240 } 1241 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]); 1242 if (err) { 1243 printf("%s: close intr pipe failed: %s\n", 1244 USBDEVNAME(sc->kue_dev), usbd_errstr(err)); 1245 } 1246 sc->kue_ep[KUE_ENDPT_INTR] = NULL; 1247 } 1248 1249 /* Free RX resources. */ 1250 for (i = 0; i < KUE_RX_LIST_CNT; i++) { 1251 if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) { 1252 m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf); 1253 sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL; 1254 } 1255 if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) { 1256 usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer); 1257 sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL; 1258 } 1259 } 1260 1261 /* Free TX resources. */ 1262 for (i = 0; i < KUE_TX_LIST_CNT; i++) { 1263 if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) { 1264 m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf); 1265 sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL; 1266 } 1267 if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) { 1268 usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer); 1269 sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL; 1270 } 1271 } 1272 1273 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1274 } 1275