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