1 /* $OpenBSD: if_kue.c,v 1.85 2016/04/13 11:03:37 mpi 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 #include "bpfilter.h" 74 75 #include <sys/param.h> 76 #include <sys/systm.h> 77 #include <sys/sockio.h> 78 #include <sys/mbuf.h> 79 #include <sys/malloc.h> 80 #include <sys/kernel.h> 81 #include <sys/socket.h> 82 #include <sys/device.h> 83 84 #include <net/if.h> 85 86 #if NBPFILTER > 0 87 #include <net/bpf.h> 88 #endif 89 90 #include <netinet/in.h> 91 #include <netinet/if_ether.h> 92 93 #include <dev/usb/usb.h> 94 #include <dev/usb/usbdi.h> 95 #include <dev/usb/usbdi_util.h> 96 #include <dev/usb/usbdevs.h> 97 98 #include <dev/usb/if_kuereg.h> 99 #include <dev/usb/if_kuevar.h> 100 101 #ifdef KUE_DEBUG 102 #define DPRINTF(x) do { if (kuedebug) printf x; } while (0) 103 #define DPRINTFN(n,x) do { if (kuedebug >= (n)) printf x; } while (0) 104 int kuedebug = 0; 105 #else 106 #define DPRINTF(x) 107 #define DPRINTFN(n,x) 108 #endif 109 110 /* 111 * Various supported device vendors/products. 112 */ 113 const struct usb_devno kue_devs[] = { 114 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 }, 115 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460 }, 116 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450 }, 117 { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT }, 118 { USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BTX }, 119 { USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 }, 120 { USB_VENDOR_ASANTE, USB_PRODUCT_ASANTE_EA }, 121 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T }, 122 { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_DSB650C }, 123 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T }, 124 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C }, 125 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 }, 126 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX1 }, 127 { USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX2 }, 128 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETT }, 129 { USB_VENDOR_JATON, USB_PRODUCT_JATON_EDA }, 130 { USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_XX1 }, 131 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT }, 132 { USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BTN }, 133 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T }, 134 { USB_VENDOR_MOBILITY, USB_PRODUCT_MOBILITY_EA }, 135 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 }, 136 { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101X }, 137 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET }, 138 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 }, 139 { USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3 }, 140 { USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA8 }, 141 { USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA9 }, 142 { USB_VENDOR_PORTSMITH, USB_PRODUCT_PORTSMITH_EEA }, 143 { USB_VENDOR_SHARK, USB_PRODUCT_SHARK_PA }, 144 { USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_U2E }, 145 { USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_GPE }, 146 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB }, 147 }; 148 149 int kue_match(struct device *, void *, void *); 150 void kue_attach(struct device *, struct device *, void *); 151 int kue_detach(struct device *, int); 152 153 struct cfdriver kue_cd = { 154 NULL, "kue", DV_IFNET 155 }; 156 157 const struct cfattach kue_ca = { 158 sizeof(struct kue_softc), kue_match, kue_attach, kue_detach 159 }; 160 161 int kue_tx_list_init(struct kue_softc *); 162 int kue_rx_list_init(struct kue_softc *); 163 int kue_newbuf(struct kue_softc *, struct kue_chain *,struct mbuf *); 164 int kue_send(struct kue_softc *, struct mbuf *, int); 165 int kue_open_pipes(struct kue_softc *); 166 void kue_rxeof(struct usbd_xfer *, void *, usbd_status); 167 void kue_txeof(struct usbd_xfer *, void *, usbd_status); 168 void kue_start(struct ifnet *); 169 int kue_ioctl(struct ifnet *, u_long, caddr_t); 170 void kue_init(void *); 171 void kue_stop(struct kue_softc *); 172 void kue_watchdog(struct ifnet *); 173 174 void kue_setmulti(struct kue_softc *); 175 void kue_reset(struct kue_softc *); 176 177 usbd_status kue_ctl(struct kue_softc *, int, u_int8_t, 178 u_int16_t, void *, u_int32_t); 179 usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t); 180 int kue_load_fw(struct kue_softc *); 181 void kue_attachhook(struct device *); 182 183 usbd_status 184 kue_setword(struct kue_softc *sc, u_int8_t breq, u_int16_t word) 185 { 186 usb_device_request_t req; 187 188 DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__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 usbd_status 200 kue_ctl(struct kue_softc *sc, int rw, u_int8_t breq, u_int16_t val, 201 void *data, u_int32_t len) 202 { 203 usb_device_request_t req; 204 205 DPRINTFN(10,("%s: %s: enter, len=%d\n", sc->kue_dev.dv_xname, 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 int 222 kue_load_fw(struct kue_softc *sc) 223 { 224 usb_device_descriptor_t *dd; 225 usbd_status err; 226 struct kue_firmware *fw; 227 u_char *buf; 228 size_t buflen; 229 230 DPRINTFN(1,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__)); 231 232 /* 233 * First, check if we even need to load the firmware. 234 * If the device was still attached when the system was 235 * rebooted, it may already have firmware loaded in it. 236 * If this is the case, we don't need to do it again. 237 * And in fact, if we try to load it again, we'll hang, 238 * so we have to avoid this condition if we don't want 239 * to look stupid. 240 * 241 * We can test this quickly by checking the bcdRevision 242 * code. The NIC will return a different revision code if 243 * it's probed while the firmware is still loaded and 244 * running. 245 */ 246 if ((dd = usbd_get_device_descriptor(sc->kue_udev)) == NULL) 247 return (EIO); 248 if (UGETW(dd->bcdDevice) >= KUE_WARM_REV) { 249 printf("%s: warm boot, no firmware download\n", 250 sc->kue_dev.dv_xname); 251 return (0); 252 } 253 254 err = loadfirmware("kue", &buf, &buflen); 255 if (err) { 256 printf("%s: failed loadfirmware of file %s: errno %d\n", 257 sc->kue_dev.dv_xname, "kue", err); 258 return (err); 259 } 260 fw = (struct kue_firmware *)buf; 261 262 printf("%s: cold boot, downloading firmware\n", 263 sc->kue_dev.dv_xname); 264 265 /* Load code segment */ 266 DPRINTFN(1,("%s: kue_load_fw: download code_seg\n", 267 sc->kue_dev.dv_xname)); 268 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 269 0, (void *)&fw->data[0], ntohl(fw->codeseglen)); 270 if (err) { 271 printf("%s: failed to load code segment: %s\n", 272 sc->kue_dev.dv_xname, usbd_errstr(err)); 273 free(buf, M_DEVBUF, 0); 274 return (EIO); 275 } 276 277 /* Load fixup segment */ 278 DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n", 279 sc->kue_dev.dv_xname)); 280 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 281 0, (void *)&fw->data[ntohl(fw->codeseglen)], ntohl(fw->fixseglen)); 282 if (err) { 283 printf("%s: failed to load fixup segment: %s\n", 284 sc->kue_dev.dv_xname, usbd_errstr(err)); 285 free(buf, M_DEVBUF, 0); 286 return (EIO); 287 } 288 289 /* Send trigger command. */ 290 DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n", 291 sc->kue_dev.dv_xname)); 292 err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN, 293 0, (void *)&fw->data[ntohl(fw->codeseglen) + ntohl(fw->fixseglen)], 294 ntohl(fw->trigseglen)); 295 if (err) { 296 printf("%s: failed to load trigger segment: %s\n", 297 sc->kue_dev.dv_xname, usbd_errstr(err)); 298 free(buf, M_DEVBUF, 0); 299 return (EIO); 300 } 301 free(buf, M_DEVBUF, 0); 302 303 usbd_delay_ms(sc->kue_udev, 10); 304 305 /* 306 * Reload device descriptor. 307 * Why? The chip without the firmware loaded returns 308 * one revision code. The chip with the firmware 309 * loaded and running returns a *different* revision 310 * code. This confuses the quirk mechanism, which is 311 * dependent on the revision data. 312 */ 313 (void)usbd_reload_device_desc(sc->kue_udev); 314 315 DPRINTFN(1,("%s: %s: done\n", sc->kue_dev.dv_xname, __func__)); 316 317 /* Reset the adapter. */ 318 kue_reset(sc); 319 320 return (0); 321 } 322 323 void 324 kue_setmulti(struct kue_softc *sc) 325 { 326 struct arpcom *ac = &sc->arpcom; 327 struct ifnet *ifp = GET_IFP(sc); 328 struct ether_multi *enm; 329 struct ether_multistep step; 330 int i; 331 332 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__)); 333 334 if (ifp->if_flags & IFF_PROMISC || ac->ac_multirangecnt > 0) { 335 allmulti: 336 ifp->if_flags |= IFF_ALLMULTI; 337 sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI; 338 sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST; 339 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); 340 return; 341 } 342 343 sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI; 344 345 i = 0; 346 ETHER_FIRST_MULTI(step, ac, enm); 347 while (enm != NULL) { 348 if (i == KUE_MCFILTCNT(sc)) 349 goto allmulti; 350 351 memcpy(KUE_MCFILT(sc, i), enm->enm_addrlo, ETHER_ADDR_LEN); 352 ETHER_NEXT_MULTI(step, enm); 353 i++; 354 } 355 356 ifp->if_flags &= ~IFF_ALLMULTI; 357 358 sc->kue_rxfilt |= KUE_RXFILT_MULTICAST; 359 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS, 360 i, sc->kue_mcfilters, i * ETHER_ADDR_LEN); 361 362 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); 363 } 364 365 /* 366 * Issue a SET_CONFIGURATION command to reset the MAC. This should be 367 * done after the firmware is loaded into the adapter in order to 368 * bring it into proper operation. 369 */ 370 void 371 kue_reset(struct kue_softc *sc) 372 { 373 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__)); 374 375 if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 1) || 376 usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX, 377 &sc->kue_iface)) 378 printf("%s: reset failed\n", sc->kue_dev.dv_xname); 379 380 /* Wait a little while for the chip to get its brains in order. */ 381 usbd_delay_ms(sc->kue_udev, 10); 382 } 383 384 /* 385 * Probe for a KLSI chip. 386 */ 387 int 388 kue_match(struct device *parent, void *match, void *aux) 389 { 390 struct usb_attach_arg *uaa = aux; 391 392 DPRINTFN(25,("kue_match: enter\n")); 393 394 if (uaa->iface != NULL) 395 return (UMATCH_NONE); 396 397 return (usb_lookup(kue_devs, uaa->vendor, uaa->product) != NULL ? 398 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 399 } 400 401 void 402 kue_attachhook(struct device *self) 403 { 404 struct kue_softc *sc = (struct kue_softc *)self; 405 int s; 406 struct ifnet *ifp; 407 struct usbd_device *dev = sc->kue_udev; 408 struct usbd_interface *iface; 409 usbd_status err; 410 usb_interface_descriptor_t *id; 411 usb_endpoint_descriptor_t *ed; 412 int i; 413 414 /* Load the firmware into the NIC. */ 415 if (kue_load_fw(sc)) { 416 printf("%s: loading firmware failed\n", 417 sc->kue_dev.dv_xname); 418 return; 419 } 420 421 err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface); 422 if (err) { 423 printf("%s: getting interface handle failed\n", 424 sc->kue_dev.dv_xname); 425 return; 426 } 427 428 sc->kue_iface = iface; 429 id = usbd_get_interface_descriptor(iface); 430 431 /* Find endpoints. */ 432 for (i = 0; i < id->bNumEndpoints; i++) { 433 ed = usbd_interface2endpoint_descriptor(iface, i); 434 if (ed == NULL) { 435 printf("%s: couldn't get ep %d\n", 436 sc->kue_dev.dv_xname, i); 437 return; 438 } 439 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 440 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 441 sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress; 442 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 443 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 444 sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress; 445 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 446 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 447 sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress; 448 } 449 } 450 451 if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) { 452 printf("%s: missing endpoint\n", sc->kue_dev.dv_xname); 453 return; 454 } 455 456 /* Read ethernet descriptor */ 457 err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR, 458 0, &sc->kue_desc, sizeof(sc->kue_desc)); 459 if (err) { 460 printf("%s: could not read Ethernet descriptor\n", 461 sc->kue_dev.dv_xname); 462 return; 463 } 464 465 sc->kue_mcfilters = mallocarray(KUE_MCFILTCNT(sc), ETHER_ADDR_LEN, 466 M_USBDEV, M_NOWAIT); 467 if (sc->kue_mcfilters == NULL) { 468 printf("%s: no memory for multicast filter buffer\n", 469 sc->kue_dev.dv_xname); 470 return; 471 } 472 473 s = splnet(); 474 475 /* 476 * A KLSI chip was detected. Inform the world. 477 */ 478 printf("%s: address %s\n", sc->kue_dev.dv_xname, 479 ether_sprintf(sc->kue_desc.kue_macaddr)); 480 481 bcopy(sc->kue_desc.kue_macaddr, 482 (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 483 484 /* Initialize interface info.*/ 485 ifp = GET_IFP(sc); 486 ifp->if_softc = sc; 487 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 488 ifp->if_ioctl = kue_ioctl; 489 ifp->if_start = kue_start; 490 ifp->if_watchdog = kue_watchdog; 491 strlcpy(ifp->if_xname, sc->kue_dev.dv_xname, IFNAMSIZ); 492 493 /* Attach the interface. */ 494 if_attach(ifp); 495 ether_ifattach(ifp); 496 497 sc->kue_attached = 1; 498 splx(s); 499 500 } 501 502 /* 503 * Attach the interface. Allocate softc structures, do 504 * setup and ethernet/BPF attach. 505 */ 506 void 507 kue_attach(struct device *parent, struct device *self, void *aux) 508 { 509 struct kue_softc *sc = (struct kue_softc *)self; 510 struct usb_attach_arg *uaa = aux; 511 struct usbd_device *dev = uaa->device; 512 usbd_status err; 513 514 DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev)); 515 516 err = usbd_set_config_no(dev, KUE_CONFIG_NO, 1); 517 if (err) { 518 printf("%s: setting config no failed\n", 519 sc->kue_dev.dv_xname); 520 return; 521 } 522 523 sc->kue_udev = dev; 524 sc->kue_product = uaa->product; 525 sc->kue_vendor = uaa->vendor; 526 527 config_mountroot(self, kue_attachhook); 528 } 529 530 int 531 kue_detach(struct device *self, int flags) 532 { 533 struct kue_softc *sc = (struct kue_softc *)self; 534 struct ifnet *ifp = GET_IFP(sc); 535 int s; 536 537 /* Detached before attached finished, so just bail out. */ 538 if (!sc->kue_attached) 539 return (0); 540 541 s = splusb(); /* XXX why? */ 542 543 if (sc->kue_mcfilters != NULL) { 544 free(sc->kue_mcfilters, M_USBDEV, 0); 545 sc->kue_mcfilters = NULL; 546 } 547 548 if (ifp->if_flags & IFF_RUNNING) 549 kue_stop(sc); 550 551 if (ifp->if_softc != NULL) { 552 ether_ifdetach(ifp); 553 if_detach(ifp); 554 } 555 556 #ifdef DIAGNOSTIC 557 if (sc->kue_ep[KUE_ENDPT_TX] != NULL || 558 sc->kue_ep[KUE_ENDPT_RX] != NULL || 559 sc->kue_ep[KUE_ENDPT_INTR] != NULL) 560 printf("%s: detach has active endpoints\n", 561 sc->kue_dev.dv_xname); 562 #endif 563 564 sc->kue_attached = 0; 565 splx(s); 566 567 return (0); 568 } 569 570 /* 571 * Initialize an RX descriptor and attach an MBUF cluster. 572 */ 573 int 574 kue_newbuf(struct kue_softc *sc, struct kue_chain *c, struct mbuf *m) 575 { 576 struct mbuf *m_new = NULL; 577 578 DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__)); 579 580 if (m == NULL) { 581 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 582 if (m_new == NULL) { 583 printf("%s: no memory for rx list " 584 "-- packet dropped!\n", sc->kue_dev.dv_xname); 585 return (ENOBUFS); 586 } 587 588 MCLGET(m_new, M_DONTWAIT); 589 if (!(m_new->m_flags & M_EXT)) { 590 printf("%s: no memory for rx list " 591 "-- packet dropped!\n", sc->kue_dev.dv_xname); 592 m_freem(m_new); 593 return (ENOBUFS); 594 } 595 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 596 } else { 597 m_new = m; 598 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 599 m_new->m_data = m_new->m_ext.ext_buf; 600 } 601 602 c->kue_mbuf = m_new; 603 604 return (0); 605 } 606 607 int 608 kue_rx_list_init(struct kue_softc *sc) 609 { 610 struct kue_cdata *cd; 611 struct kue_chain *c; 612 int i; 613 614 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__)); 615 616 cd = &sc->kue_cdata; 617 for (i = 0; i < KUE_RX_LIST_CNT; i++) { 618 c = &cd->kue_rx_chain[i]; 619 c->kue_sc = sc; 620 c->kue_idx = i; 621 if (kue_newbuf(sc, c, NULL) == ENOBUFS) 622 return (ENOBUFS); 623 if (c->kue_xfer == NULL) { 624 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev); 625 if (c->kue_xfer == NULL) 626 return (ENOBUFS); 627 c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ); 628 if (c->kue_buf == NULL) 629 return (ENOBUFS); /* XXX free xfer */ 630 } 631 } 632 633 return (0); 634 } 635 636 int 637 kue_tx_list_init(struct kue_softc *sc) 638 { 639 struct kue_cdata *cd; 640 struct kue_chain *c; 641 int i; 642 643 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__)); 644 645 cd = &sc->kue_cdata; 646 for (i = 0; i < KUE_TX_LIST_CNT; i++) { 647 c = &cd->kue_tx_chain[i]; 648 c->kue_sc = sc; 649 c->kue_idx = i; 650 c->kue_mbuf = NULL; 651 if (c->kue_xfer == NULL) { 652 c->kue_xfer = usbd_alloc_xfer(sc->kue_udev); 653 if (c->kue_xfer == NULL) 654 return (ENOBUFS); 655 c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ); 656 if (c->kue_buf == NULL) 657 return (ENOBUFS); 658 } 659 } 660 661 return (0); 662 } 663 664 /* 665 * A frame has been uploaded: pass the resulting mbuf chain up to 666 * the higher level protocols. 667 */ 668 void 669 kue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 670 { 671 struct kue_chain *c = priv; 672 struct kue_softc *sc = c->kue_sc; 673 struct ifnet *ifp = GET_IFP(sc); 674 struct mbuf_list ml = MBUF_LIST_INITIALIZER(); 675 struct mbuf *m; 676 int total_len = 0; 677 int s; 678 679 DPRINTFN(10,("%s: %s: enter status=%d\n", sc->kue_dev.dv_xname, 680 __func__, status)); 681 682 if (usbd_is_dying(sc->kue_udev)) 683 return; 684 685 if (!(ifp->if_flags & IFF_RUNNING)) 686 return; 687 688 if (status != USBD_NORMAL_COMPLETION) { 689 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 690 return; 691 sc->kue_rx_errs++; 692 if (usbd_ratecheck(&sc->kue_rx_notice)) { 693 printf("%s: %u usb errors on rx: %s\n", 694 sc->kue_dev.dv_xname, sc->kue_rx_errs, 695 usbd_errstr(status)); 696 sc->kue_rx_errs = 0; 697 } 698 if (status == USBD_STALLED) 699 usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_RX]); 700 goto done; 701 } 702 703 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 704 705 DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", sc->kue_dev.dv_xname, 706 __func__, total_len, 707 UGETW(mtod(c->kue_mbuf, u_int8_t *)))); 708 709 if (total_len <= 1) 710 goto done; 711 712 m = c->kue_mbuf; 713 /* copy data to mbuf */ 714 memcpy(mtod(m, char *), c->kue_buf, total_len); 715 716 /* No errors; receive the packet. */ 717 total_len = UGETW(mtod(m, u_int8_t *)); 718 m_adj(m, sizeof(u_int16_t)); 719 720 if (total_len < sizeof(struct ether_header)) { 721 ifp->if_ierrors++; 722 goto done; 723 } 724 725 m->m_pkthdr.len = m->m_len = total_len; 726 ml_enqueue(&ml, m); 727 728 if (kue_newbuf(sc, c, NULL) == ENOBUFS) { 729 ifp->if_ierrors++; 730 goto done; 731 } 732 733 s = splnet(); 734 if_input(ifp, &ml); 735 splx(s); 736 737 done: 738 739 /* Setup new transfer. */ 740 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX], 741 c, c->kue_buf, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, 742 USBD_NO_TIMEOUT, kue_rxeof); 743 usbd_transfer(c->kue_xfer); 744 745 DPRINTFN(10,("%s: %s: start rx\n", sc->kue_dev.dv_xname, 746 __func__)); 747 } 748 749 /* 750 * A frame was downloaded to the chip. It's safe for us to clean up 751 * the list buffers. 752 */ 753 754 void 755 kue_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 756 { 757 struct kue_chain *c = priv; 758 struct kue_softc *sc = c->kue_sc; 759 struct ifnet *ifp = GET_IFP(sc); 760 int s; 761 762 if (usbd_is_dying(sc->kue_udev)) 763 return; 764 765 s = splnet(); 766 767 DPRINTFN(10,("%s: %s: enter status=%d\n", sc->kue_dev.dv_xname, 768 __func__, status)); 769 770 ifp->if_timer = 0; 771 ifq_clr_oactive(&ifp->if_snd); 772 773 if (status != USBD_NORMAL_COMPLETION) { 774 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 775 splx(s); 776 return; 777 } 778 ifp->if_oerrors++; 779 printf("%s: usb error on tx: %s\n", sc->kue_dev.dv_xname, 780 usbd_errstr(status)); 781 if (status == USBD_STALLED) 782 usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_TX]); 783 splx(s); 784 return; 785 } 786 787 ifp->if_opackets++; 788 789 m_freem(c->kue_mbuf); 790 c->kue_mbuf = NULL; 791 792 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 793 kue_start(ifp); 794 795 splx(s); 796 } 797 798 int 799 kue_send(struct kue_softc *sc, struct mbuf *m, int idx) 800 { 801 int total_len; 802 struct kue_chain *c; 803 usbd_status err; 804 805 DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__)); 806 807 c = &sc->kue_cdata.kue_tx_chain[idx]; 808 809 /* 810 * Copy the mbuf data into a contiguous buffer, leaving two 811 * bytes at the beginning to hold the frame length. 812 */ 813 m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2); 814 c->kue_mbuf = m; 815 816 total_len = m->m_pkthdr.len + 2; 817 /* XXX what's this? */ 818 total_len += 64 - (total_len % 64); 819 820 /* Frame length is specified in the first 2 bytes of the buffer. */ 821 c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len; 822 c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8); 823 824 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX], 825 c, c->kue_buf, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT, 826 kue_txeof); 827 828 /* Transmit */ 829 err = usbd_transfer(c->kue_xfer); 830 if (err != USBD_IN_PROGRESS) { 831 printf("%s: kue_send error=%s\n", sc->kue_dev.dv_xname, 832 usbd_errstr(err)); 833 kue_stop(sc); 834 return (EIO); 835 } 836 837 sc->kue_cdata.kue_tx_cnt++; 838 839 return (0); 840 } 841 842 void 843 kue_start(struct ifnet *ifp) 844 { 845 struct kue_softc *sc = ifp->if_softc; 846 struct mbuf *m_head = NULL; 847 848 DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__)); 849 850 if (usbd_is_dying(sc->kue_udev)) 851 return; 852 853 if (ifq_is_oactive(&ifp->if_snd)) 854 return; 855 856 m_head = ifq_deq_begin(&ifp->if_snd); 857 if (m_head == NULL) 858 return; 859 860 if (kue_send(sc, m_head, 0)) { 861 ifq_deq_rollback(&ifp->if_snd, m_head); 862 ifq_set_oactive(&ifp->if_snd); 863 return; 864 } 865 866 ifq_deq_commit(&ifp->if_snd, m_head); 867 868 #if NBPFILTER > 0 869 /* 870 * If there's a BPF listener, bounce a copy of this frame 871 * to him. 872 */ 873 if (ifp->if_bpf) 874 bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT); 875 #endif 876 877 ifq_set_oactive(&ifp->if_snd); 878 879 /* 880 * Set a timeout in case the chip goes out to lunch. 881 */ 882 ifp->if_timer = 6; 883 } 884 885 void 886 kue_init(void *xsc) 887 { 888 struct kue_softc *sc = xsc; 889 struct ifnet *ifp = GET_IFP(sc); 890 int s; 891 u_char *eaddr; 892 893 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__)); 894 895 if (ifp->if_flags & IFF_RUNNING) 896 return; 897 898 s = splnet(); 899 900 eaddr = sc->arpcom.ac_enaddr; 901 /* Set MAC address */ 902 kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN); 903 904 sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST; 905 906 /* If we want promiscuous mode, set the allframes bit. */ 907 if (ifp->if_flags & IFF_PROMISC) 908 sc->kue_rxfilt |= KUE_RXFILT_PROMISC; 909 910 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt); 911 912 /* I'm not sure how to tune these. */ 913 #if 0 914 /* 915 * Leave this one alone for now; setting it 916 * wrong causes lockups on some machines/controllers. 917 */ 918 kue_setword(sc, KUE_CMD_SET_SOFS, 1); 919 #endif 920 kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64); 921 922 /* Init TX ring. */ 923 if (kue_tx_list_init(sc) == ENOBUFS) { 924 printf("%s: tx list init failed\n", sc->kue_dev.dv_xname); 925 splx(s); 926 return; 927 } 928 929 /* Init RX ring. */ 930 if (kue_rx_list_init(sc) == ENOBUFS) { 931 printf("%s: rx list init failed\n", sc->kue_dev.dv_xname); 932 splx(s); 933 return; 934 } 935 936 /* Load the multicast filter. */ 937 kue_setmulti(sc); 938 939 if (sc->kue_ep[KUE_ENDPT_RX] == NULL) { 940 if (kue_open_pipes(sc)) { 941 splx(s); 942 return; 943 } 944 } 945 946 ifp->if_flags |= IFF_RUNNING; 947 ifq_clr_oactive(&ifp->if_snd); 948 949 splx(s); 950 } 951 952 int 953 kue_open_pipes(struct kue_softc *sc) 954 { 955 usbd_status err; 956 struct kue_chain *c; 957 int i; 958 959 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__)); 960 961 /* Open RX and TX pipes. */ 962 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX], 963 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]); 964 if (err) { 965 printf("%s: open rx pipe failed: %s\n", 966 sc->kue_dev.dv_xname, usbd_errstr(err)); 967 return (EIO); 968 } 969 970 err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX], 971 USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]); 972 if (err) { 973 printf("%s: open tx pipe failed: %s\n", 974 sc->kue_dev.dv_xname, usbd_errstr(err)); 975 return (EIO); 976 } 977 978 /* Start up the receive pipe. */ 979 for (i = 0; i < KUE_RX_LIST_CNT; i++) { 980 c = &sc->kue_cdata.kue_rx_chain[i]; 981 usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX], 982 c, c->kue_buf, KUE_BUFSZ, 983 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, 984 kue_rxeof); 985 DPRINTFN(5,("%s: %s: start read\n", sc->kue_dev.dv_xname, 986 __func__)); 987 usbd_transfer(c->kue_xfer); 988 } 989 990 return (0); 991 } 992 993 int 994 kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 995 { 996 struct kue_softc *sc = ifp->if_softc; 997 int s, error = 0; 998 999 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__)); 1000 1001 if (usbd_is_dying(sc->kue_udev)) 1002 return (EIO); 1003 1004 #ifdef DIAGNOSTIC 1005 if (!curproc) { 1006 printf("%s: no proc!!\n", sc->kue_dev.dv_xname); 1007 return EIO; 1008 } 1009 #endif 1010 1011 s = splnet(); 1012 1013 switch(command) { 1014 case SIOCSIFADDR: 1015 ifp->if_flags |= IFF_UP; 1016 kue_init(sc); 1017 break; 1018 1019 case SIOCSIFFLAGS: 1020 if (ifp->if_flags & IFF_UP) { 1021 if (ifp->if_flags & IFF_RUNNING && 1022 ifp->if_flags & IFF_PROMISC && 1023 !(sc->kue_if_flags & IFF_PROMISC)) { 1024 sc->kue_rxfilt |= KUE_RXFILT_PROMISC; 1025 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, 1026 sc->kue_rxfilt); 1027 } else if (ifp->if_flags & IFF_RUNNING && 1028 !(ifp->if_flags & IFF_PROMISC) && 1029 sc->kue_if_flags & IFF_PROMISC) { 1030 sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC; 1031 kue_setword(sc, KUE_CMD_SET_PKT_FILTER, 1032 sc->kue_rxfilt); 1033 } else if (!(ifp->if_flags & IFF_RUNNING)) 1034 kue_init(sc); 1035 } else { 1036 if (ifp->if_flags & IFF_RUNNING) 1037 kue_stop(sc); 1038 } 1039 sc->kue_if_flags = ifp->if_flags; 1040 error = 0; 1041 break; 1042 1043 default: 1044 error = ether_ioctl(ifp, &sc->arpcom, command, data); 1045 } 1046 1047 if (error == ENETRESET) { 1048 if (ifp->if_flags & IFF_RUNNING) 1049 kue_setmulti(sc); 1050 error = 0; 1051 } 1052 1053 splx(s); 1054 return (error); 1055 } 1056 1057 void 1058 kue_watchdog(struct ifnet *ifp) 1059 { 1060 struct kue_softc *sc = ifp->if_softc; 1061 struct kue_chain *c; 1062 usbd_status stat; 1063 int s; 1064 1065 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__)); 1066 1067 if (usbd_is_dying(sc->kue_udev)) 1068 return; 1069 1070 ifp->if_oerrors++; 1071 printf("%s: watchdog timeout\n", sc->kue_dev.dv_xname); 1072 1073 s = splusb(); 1074 c = &sc->kue_cdata.kue_tx_chain[0]; 1075 usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat); 1076 kue_txeof(c->kue_xfer, c, stat); 1077 1078 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1079 kue_start(ifp); 1080 splx(s); 1081 } 1082 1083 /* 1084 * Stop the adapter and free any mbufs allocated to the 1085 * RX and TX lists. 1086 */ 1087 void 1088 kue_stop(struct kue_softc *sc) 1089 { 1090 usbd_status err; 1091 struct ifnet *ifp; 1092 int i; 1093 1094 DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__)); 1095 1096 ifp = GET_IFP(sc); 1097 ifp->if_timer = 0; 1098 ifp->if_flags &= ~IFF_RUNNING; 1099 ifq_clr_oactive(&ifp->if_snd); 1100 1101 /* Stop transfers. */ 1102 if (sc->kue_ep[KUE_ENDPT_RX] != NULL) { 1103 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]); 1104 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]); 1105 if (err) { 1106 printf("%s: close rx pipe failed: %s\n", 1107 sc->kue_dev.dv_xname, usbd_errstr(err)); 1108 } 1109 sc->kue_ep[KUE_ENDPT_RX] = NULL; 1110 } 1111 1112 if (sc->kue_ep[KUE_ENDPT_TX] != NULL) { 1113 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]); 1114 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]); 1115 if (err) { 1116 printf("%s: close tx pipe failed: %s\n", 1117 sc->kue_dev.dv_xname, usbd_errstr(err)); 1118 } 1119 sc->kue_ep[KUE_ENDPT_TX] = NULL; 1120 } 1121 1122 if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) { 1123 usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]); 1124 err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]); 1125 if (err) { 1126 printf("%s: close intr pipe failed: %s\n", 1127 sc->kue_dev.dv_xname, usbd_errstr(err)); 1128 } 1129 sc->kue_ep[KUE_ENDPT_INTR] = NULL; 1130 } 1131 1132 /* Free RX resources. */ 1133 for (i = 0; i < KUE_RX_LIST_CNT; i++) { 1134 if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) { 1135 m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf); 1136 sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL; 1137 } 1138 if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) { 1139 usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer); 1140 sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL; 1141 } 1142 } 1143 1144 /* Free TX resources. */ 1145 for (i = 0; i < KUE_TX_LIST_CNT; i++) { 1146 if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) { 1147 m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf); 1148 sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL; 1149 } 1150 if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) { 1151 usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer); 1152 sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL; 1153 } 1154 } 1155 } 1156