1 /* $NetBSD: if_upl.c,v 1.31 2008/04/28 20:23:59 martin Exp $ */ 2 /* 3 * Copyright (c) 2000 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Lennart Augustsson (lennart@augustsson.net) at 8 * Carlstedt Research & Technology. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Prolific PL2301/PL2302 driver 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: if_upl.c,v 1.31 2008/04/28 20:23:59 martin Exp $"); 38 39 #include "opt_inet.h" 40 #include "bpfilter.h" 41 #include "rnd.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/callout.h> 46 #include <sys/sockio.h> 47 #include <sys/mbuf.h> 48 #include <sys/malloc.h> 49 #include <sys/kernel.h> 50 #include <sys/socket.h> 51 52 #include <sys/device.h> 53 #if NRND > 0 54 #include <sys/rnd.h> 55 #endif 56 57 #include <net/if.h> 58 #include <net/if_types.h> 59 #include <net/if_dl.h> 60 #include <net/netisr.h> 61 62 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m)) 63 64 #if NBPFILTER > 0 65 #include <net/bpf.h> 66 #endif 67 68 #ifdef INET 69 #include <netinet/in.h> 70 #include <netinet/in_var.h> 71 #include <netinet/if_inarp.h> 72 #endif 73 74 75 #include <dev/usb/usb.h> 76 #include <dev/usb/usbdi.h> 77 #include <dev/usb/usbdi_util.h> 78 #include <dev/usb/usbdevs.h> 79 80 /* 81 * 7 6 5 4 3 2 1 0 82 * tx rx 1 0 83 * 1110 0000 rxdata 84 * 1010 0000 idle 85 * 0010 0000 tx over 86 * 0110 tx over + rxd 87 */ 88 89 #define UPL_RXDATA 0x40 90 #define UPL_TXOK 0x80 91 92 #define UPL_INTR_PKTLEN 1 93 94 #define UPL_CONFIG_NO 1 95 #define UPL_IFACE_IDX 0 96 97 /***/ 98 99 #define UPL_INTR_INTERVAL 20 100 101 #define UPL_BUFSZ 1024 102 103 #define UPL_RX_FRAMES 1 104 #define UPL_TX_FRAMES 1 105 106 #define UPL_RX_LIST_CNT 1 107 #define UPL_TX_LIST_CNT 1 108 109 #define UPL_ENDPT_RX 0x0 110 #define UPL_ENDPT_TX 0x1 111 #define UPL_ENDPT_INTR 0x2 112 #define UPL_ENDPT_MAX 0x3 113 114 struct upl_type { 115 u_int16_t upl_vid; 116 u_int16_t upl_did; 117 }; 118 119 struct upl_softc; 120 121 struct upl_chain { 122 struct upl_softc *upl_sc; 123 usbd_xfer_handle upl_xfer; 124 char *upl_buf; 125 struct mbuf *upl_mbuf; 126 int upl_idx; 127 }; 128 129 struct upl_cdata { 130 struct upl_chain upl_tx_chain[UPL_TX_LIST_CNT]; 131 struct upl_chain upl_rx_chain[UPL_RX_LIST_CNT]; 132 int upl_tx_prod; 133 int upl_tx_cons; 134 int upl_tx_cnt; 135 int upl_rx_prod; 136 }; 137 138 struct upl_softc { 139 USBBASEDEVICE sc_dev; 140 141 struct ifnet sc_if; 142 #if NRND > 0 143 rndsource_element_t sc_rnd_source; 144 #endif 145 146 usb_callout_t sc_stat_ch; 147 148 usbd_device_handle sc_udev; 149 usbd_interface_handle sc_iface; 150 u_int16_t sc_vendor; 151 u_int16_t sc_product; 152 int sc_ed[UPL_ENDPT_MAX]; 153 usbd_pipe_handle sc_ep[UPL_ENDPT_MAX]; 154 struct upl_cdata sc_cdata; 155 156 uByte sc_ibuf; 157 158 char sc_dying; 159 char sc_attached; 160 u_int sc_rx_errs; 161 struct timeval sc_rx_notice; 162 u_int sc_intr_errs; 163 }; 164 165 #ifdef UPL_DEBUG 166 #define DPRINTF(x) if (upldebug) logprintf x 167 #define DPRINTFN(n,x) if (upldebug >= (n)) logprintf x 168 int upldebug = 0; 169 #else 170 #define DPRINTF(x) 171 #define DPRINTFN(n,x) 172 #endif 173 174 /* 175 * Various supported device vendors/products. 176 */ 177 Static struct upl_type sc_devs[] = { 178 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2301 }, 179 { USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2302 }, 180 { 0, 0 } 181 }; 182 183 USB_DECLARE_DRIVER(upl); 184 185 Static int upl_openpipes(struct upl_softc *); 186 Static int upl_tx_list_init(struct upl_softc *); 187 Static int upl_rx_list_init(struct upl_softc *); 188 Static int upl_newbuf(struct upl_softc *, struct upl_chain *, struct mbuf *); 189 Static int upl_send(struct upl_softc *, struct mbuf *, int); 190 Static void upl_intr(usbd_xfer_handle, usbd_private_handle, usbd_status); 191 Static void upl_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 192 Static void upl_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 193 Static void upl_start(struct ifnet *); 194 Static int upl_ioctl(struct ifnet *, u_long, void *); 195 Static void upl_init(void *); 196 Static void upl_stop(struct upl_softc *); 197 Static void upl_watchdog(struct ifnet *); 198 199 Static int upl_output(struct ifnet *, struct mbuf *, const struct sockaddr *, 200 struct rtentry *); 201 Static void upl_input(struct ifnet *, struct mbuf *); 202 203 /* 204 * Probe for a Prolific chip. 205 */ 206 USB_MATCH(upl) 207 { 208 USB_MATCH_START(upl, uaa); 209 struct upl_type *t; 210 211 for (t = sc_devs; t->upl_vid != 0; t++) 212 if (uaa->vendor == t->upl_vid && uaa->product == t->upl_did) 213 return (UMATCH_VENDOR_PRODUCT); 214 215 return (UMATCH_NONE); 216 } 217 218 USB_ATTACH(upl) 219 { 220 USB_ATTACH_START(upl, sc, uaa); 221 char *devinfop; 222 int s; 223 usbd_device_handle dev = uaa->device; 224 usbd_interface_handle iface; 225 usbd_status err; 226 struct ifnet *ifp; 227 usb_interface_descriptor_t *id; 228 usb_endpoint_descriptor_t *ed; 229 int i; 230 231 DPRINTFN(5,(" : upl_attach: sc=%p, dev=%p", sc, dev)); 232 233 devinfop = usbd_devinfo_alloc(dev, 0); 234 USB_ATTACH_SETUP; 235 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop); 236 usbd_devinfo_free(devinfop); 237 238 err = usbd_set_config_no(dev, UPL_CONFIG_NO, 1); 239 if (err) { 240 printf("%s: setting config no failed\n", 241 USBDEVNAME(sc->sc_dev)); 242 USB_ATTACH_ERROR_RETURN; 243 } 244 245 sc->sc_udev = dev; 246 sc->sc_product = uaa->product; 247 sc->sc_vendor = uaa->vendor; 248 249 err = usbd_device2interface_handle(dev, UPL_IFACE_IDX, &iface); 250 if (err) { 251 printf("%s: getting interface handle failed\n", 252 USBDEVNAME(sc->sc_dev)); 253 USB_ATTACH_ERROR_RETURN; 254 } 255 256 sc->sc_iface = iface; 257 id = usbd_get_interface_descriptor(iface); 258 259 /* Find endpoints. */ 260 for (i = 0; i < id->bNumEndpoints; i++) { 261 ed = usbd_interface2endpoint_descriptor(iface, i); 262 if (ed == NULL) { 263 printf("%s: couldn't get ep %d\n", 264 USBDEVNAME(sc->sc_dev), i); 265 USB_ATTACH_ERROR_RETURN; 266 } 267 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 268 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 269 sc->sc_ed[UPL_ENDPT_RX] = ed->bEndpointAddress; 270 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 271 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 272 sc->sc_ed[UPL_ENDPT_TX] = ed->bEndpointAddress; 273 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 274 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 275 sc->sc_ed[UPL_ENDPT_INTR] = ed->bEndpointAddress; 276 } 277 } 278 279 if (sc->sc_ed[UPL_ENDPT_RX] == 0 || sc->sc_ed[UPL_ENDPT_TX] == 0 || 280 sc->sc_ed[UPL_ENDPT_INTR] == 0) { 281 printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev)); 282 USB_ATTACH_ERROR_RETURN; 283 } 284 285 s = splnet(); 286 287 /* Initialize interface info.*/ 288 ifp = &sc->sc_if; 289 ifp->if_softc = sc; 290 ifp->if_mtu = UPL_BUFSZ; 291 ifp->if_flags = IFF_POINTOPOINT | IFF_NOARP | IFF_SIMPLEX; 292 ifp->if_ioctl = upl_ioctl; 293 ifp->if_start = upl_start; 294 ifp->if_watchdog = upl_watchdog; 295 strncpy(ifp->if_xname, USBDEVNAME(sc->sc_dev), IFNAMSIZ); 296 297 ifp->if_type = IFT_OTHER; 298 ifp->if_addrlen = 0; 299 ifp->if_hdrlen = 0; 300 ifp->if_output = upl_output; 301 ifp->if_input = upl_input; 302 ifp->if_baudrate = 12000000; 303 ifp->if_dlt = DLT_RAW; 304 IFQ_SET_READY(&ifp->if_snd); 305 306 /* Attach the interface. */ 307 if_attach(ifp); 308 if_alloc_sadl(ifp); 309 310 #if NBPFILTER > 0 311 bpfattach(ifp, DLT_RAW, 0); 312 #endif 313 #if NRND > 0 314 rnd_attach_source(&sc->sc_rnd_source, USBDEVNAME(sc->sc_dev), 315 RND_TYPE_NET, 0); 316 #endif 317 318 sc->sc_attached = 1; 319 splx(s); 320 321 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 322 USBDEV(sc->sc_dev)); 323 324 USB_ATTACH_SUCCESS_RETURN; 325 } 326 327 USB_DETACH(upl) 328 { 329 USB_DETACH_START(upl, sc); 330 struct ifnet *ifp = &sc->sc_if; 331 int s; 332 333 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__)); 334 335 s = splusb(); 336 337 if (!sc->sc_attached) { 338 /* Detached before attached finished, so just bail out. */ 339 splx(s); 340 return (0); 341 } 342 343 if (ifp->if_flags & IFF_RUNNING) 344 upl_stop(sc); 345 346 #if NRND > 0 347 rnd_detach_source(&sc->sc_rnd_source); 348 #endif 349 #if NBPFILTER > 0 350 bpfdetach(ifp); 351 #endif 352 353 if_detach(ifp); 354 355 #ifdef DIAGNOSTIC 356 if (sc->sc_ep[UPL_ENDPT_TX] != NULL || 357 sc->sc_ep[UPL_ENDPT_RX] != NULL || 358 sc->sc_ep[UPL_ENDPT_INTR] != NULL) 359 printf("%s: detach has active endpoints\n", 360 USBDEVNAME(sc->sc_dev)); 361 #endif 362 363 sc->sc_attached = 0; 364 splx(s); 365 366 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 367 USBDEV(sc->sc_dev)); 368 369 return (0); 370 } 371 372 int 373 upl_activate(device_ptr_t self, enum devact act) 374 { 375 struct upl_softc *sc = (struct upl_softc *)self; 376 377 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__)); 378 379 switch (act) { 380 case DVACT_ACTIVATE: 381 return (EOPNOTSUPP); 382 break; 383 384 case DVACT_DEACTIVATE: 385 /* Deactivate the interface. */ 386 if_deactivate(&sc->sc_if); 387 sc->sc_dying = 1; 388 break; 389 } 390 return (0); 391 } 392 393 /* 394 * Initialize an RX descriptor and attach an MBUF cluster. 395 */ 396 Static int 397 upl_newbuf(struct upl_softc *sc, struct upl_chain *c, struct mbuf *m) 398 { 399 struct mbuf *m_new = NULL; 400 401 DPRINTFN(8,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__)); 402 403 if (m == NULL) { 404 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 405 if (m_new == NULL) { 406 printf("%s: no memory for rx list " 407 "-- packet dropped!\n", USBDEVNAME(sc->sc_dev)); 408 return (ENOBUFS); 409 } 410 411 MCLGET(m_new, M_DONTWAIT); 412 if (!(m_new->m_flags & M_EXT)) { 413 printf("%s: no memory for rx list " 414 "-- packet dropped!\n", USBDEVNAME(sc->sc_dev)); 415 m_freem(m_new); 416 return (ENOBUFS); 417 } 418 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 419 } else { 420 m_new = m; 421 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 422 m_new->m_data = m_new->m_ext.ext_buf; 423 } 424 425 c->upl_mbuf = m_new; 426 427 return (0); 428 } 429 430 Static int 431 upl_rx_list_init(struct upl_softc *sc) 432 { 433 struct upl_cdata *cd; 434 struct upl_chain *c; 435 int i; 436 437 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__)); 438 439 cd = &sc->sc_cdata; 440 for (i = 0; i < UPL_RX_LIST_CNT; i++) { 441 c = &cd->upl_rx_chain[i]; 442 c->upl_sc = sc; 443 c->upl_idx = i; 444 if (upl_newbuf(sc, c, NULL) == ENOBUFS) 445 return (ENOBUFS); 446 if (c->upl_xfer == NULL) { 447 c->upl_xfer = usbd_alloc_xfer(sc->sc_udev); 448 if (c->upl_xfer == NULL) 449 return (ENOBUFS); 450 c->upl_buf = usbd_alloc_buffer(c->upl_xfer, UPL_BUFSZ); 451 if (c->upl_buf == NULL) { 452 usbd_free_xfer(c->upl_xfer); 453 return (ENOBUFS); 454 } 455 } 456 } 457 458 return (0); 459 } 460 461 Static int 462 upl_tx_list_init(struct upl_softc *sc) 463 { 464 struct upl_cdata *cd; 465 struct upl_chain *c; 466 int i; 467 468 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__)); 469 470 cd = &sc->sc_cdata; 471 for (i = 0; i < UPL_TX_LIST_CNT; i++) { 472 c = &cd->upl_tx_chain[i]; 473 c->upl_sc = sc; 474 c->upl_idx = i; 475 c->upl_mbuf = NULL; 476 if (c->upl_xfer == NULL) { 477 c->upl_xfer = usbd_alloc_xfer(sc->sc_udev); 478 if (c->upl_xfer == NULL) 479 return (ENOBUFS); 480 c->upl_buf = usbd_alloc_buffer(c->upl_xfer, UPL_BUFSZ); 481 if (c->upl_buf == NULL) { 482 usbd_free_xfer(c->upl_xfer); 483 return (ENOBUFS); 484 } 485 } 486 } 487 488 return (0); 489 } 490 491 /* 492 * A frame has been uploaded: pass the resulting mbuf chain up to 493 * the higher level protocols. 494 */ 495 Static void 496 upl_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 497 { 498 struct upl_chain *c = priv; 499 struct upl_softc *sc = c->upl_sc; 500 struct ifnet *ifp = &sc->sc_if; 501 struct mbuf *m; 502 int total_len = 0; 503 int s; 504 505 if (sc->sc_dying) 506 return; 507 508 if (!(ifp->if_flags & IFF_RUNNING)) 509 return; 510 511 if (status != USBD_NORMAL_COMPLETION) { 512 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 513 return; 514 sc->sc_rx_errs++; 515 if (usbd_ratecheck(&sc->sc_rx_notice)) { 516 printf("%s: %u usb errors on rx: %s\n", 517 USBDEVNAME(sc->sc_dev), sc->sc_rx_errs, 518 usbd_errstr(status)); 519 sc->sc_rx_errs = 0; 520 } 521 if (status == USBD_STALLED) 522 usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]); 523 goto done; 524 } 525 526 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 527 528 DPRINTFN(9,("%s: %s: enter status=%d length=%d\n", 529 USBDEVNAME(sc->sc_dev), __func__, status, total_len)); 530 531 m = c->upl_mbuf; 532 memcpy(mtod(c->upl_mbuf, char *), c->upl_buf, total_len); 533 534 ifp->if_ipackets++; 535 m->m_pkthdr.len = m->m_len = total_len; 536 537 m->m_pkthdr.rcvif = ifp; 538 539 s = splnet(); 540 541 /* XXX ugly */ 542 if (upl_newbuf(sc, c, NULL) == ENOBUFS) { 543 ifp->if_ierrors++; 544 goto done1; 545 } 546 547 #if NBPFILTER > 0 548 /* 549 * Handle BPF listeners. Let the BPF user see the packet, but 550 * don't pass it up to the ether_input() layer unless it's 551 * a broadcast packet, multicast packet, matches our ethernet 552 * address or the interface is in promiscuous mode. 553 */ 554 if (ifp->if_bpf) { 555 BPF_MTAP(ifp, m); 556 } 557 #endif 558 559 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->sc_dev), 560 __func__, m->m_len)); 561 562 IF_INPUT(ifp, m); 563 564 done1: 565 splx(s); 566 567 done: 568 #if 1 569 /* Setup new transfer. */ 570 usbd_setup_xfer(c->upl_xfer, sc->sc_ep[UPL_ENDPT_RX], 571 c, c->upl_buf, UPL_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, 572 USBD_NO_TIMEOUT, upl_rxeof); 573 usbd_transfer(c->upl_xfer); 574 575 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->sc_dev), 576 __func__)); 577 #endif 578 } 579 580 /* 581 * A frame was downloaded to the chip. It's safe for us to clean up 582 * the list buffers. 583 */ 584 Static void 585 upl_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, 586 usbd_status status) 587 { 588 struct upl_chain *c = priv; 589 struct upl_softc *sc = c->upl_sc; 590 struct ifnet *ifp = &sc->sc_if; 591 int s; 592 593 if (sc->sc_dying) 594 return; 595 596 s = splnet(); 597 598 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->sc_dev), 599 __func__, status)); 600 601 ifp->if_timer = 0; 602 ifp->if_flags &= ~IFF_OACTIVE; 603 604 if (status != USBD_NORMAL_COMPLETION) { 605 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 606 splx(s); 607 return; 608 } 609 ifp->if_oerrors++; 610 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->sc_dev), 611 usbd_errstr(status)); 612 if (status == USBD_STALLED) 613 usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_TX]); 614 splx(s); 615 return; 616 } 617 618 ifp->if_opackets++; 619 620 m_freem(c->upl_mbuf); 621 c->upl_mbuf = NULL; 622 623 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 624 upl_start(ifp); 625 626 splx(s); 627 } 628 629 Static int 630 upl_send(struct upl_softc *sc, struct mbuf *m, int idx) 631 { 632 int total_len; 633 struct upl_chain *c; 634 usbd_status err; 635 636 c = &sc->sc_cdata.upl_tx_chain[idx]; 637 638 /* 639 * Copy the mbuf data into a contiguous buffer, leaving two 640 * bytes at the beginning to hold the frame length. 641 */ 642 m_copydata(m, 0, m->m_pkthdr.len, c->upl_buf); 643 c->upl_mbuf = m; 644 645 total_len = m->m_pkthdr.len; 646 647 DPRINTFN(10,("%s: %s: total_len=%d\n", 648 USBDEVNAME(sc->sc_dev), __func__, total_len)); 649 650 usbd_setup_xfer(c->upl_xfer, sc->sc_ep[UPL_ENDPT_TX], 651 c, c->upl_buf, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT, 652 upl_txeof); 653 654 /* Transmit */ 655 err = usbd_transfer(c->upl_xfer); 656 if (err != USBD_IN_PROGRESS) { 657 printf("%s: upl_send error=%s\n", USBDEVNAME(sc->sc_dev), 658 usbd_errstr(err)); 659 upl_stop(sc); 660 return (EIO); 661 } 662 663 sc->sc_cdata.upl_tx_cnt++; 664 665 return (0); 666 } 667 668 Static void 669 upl_start(struct ifnet *ifp) 670 { 671 struct upl_softc *sc = ifp->if_softc; 672 struct mbuf *m_head = NULL; 673 674 if (sc->sc_dying) 675 return; 676 677 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__)); 678 679 if (ifp->if_flags & IFF_OACTIVE) 680 return; 681 682 IFQ_POLL(&ifp->if_snd, m_head); 683 if (m_head == NULL) 684 return; 685 686 if (upl_send(sc, m_head, 0)) { 687 ifp->if_flags |= IFF_OACTIVE; 688 return; 689 } 690 691 IFQ_DEQUEUE(&ifp->if_snd, m_head); 692 693 #if NBPFILTER > 0 694 /* 695 * If there's a BPF listener, bounce a copy of this frame 696 * to him. 697 */ 698 if (ifp->if_bpf) 699 BPF_MTAP(ifp, m_head); 700 #endif 701 702 ifp->if_flags |= IFF_OACTIVE; 703 704 /* 705 * Set a timeout in case the chip goes out to lunch. 706 */ 707 ifp->if_timer = 5; 708 } 709 710 Static void 711 upl_init(void *xsc) 712 { 713 struct upl_softc *sc = xsc; 714 struct ifnet *ifp = &sc->sc_if; 715 int s; 716 717 if (sc->sc_dying) 718 return; 719 720 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__)); 721 722 if (ifp->if_flags & IFF_RUNNING) 723 return; 724 725 s = splnet(); 726 727 /* Init TX ring. */ 728 if (upl_tx_list_init(sc) == ENOBUFS) { 729 printf("%s: tx list init failed\n", USBDEVNAME(sc->sc_dev)); 730 splx(s); 731 return; 732 } 733 734 /* Init RX ring. */ 735 if (upl_rx_list_init(sc) == ENOBUFS) { 736 printf("%s: rx list init failed\n", USBDEVNAME(sc->sc_dev)); 737 splx(s); 738 return; 739 } 740 741 if (sc->sc_ep[UPL_ENDPT_RX] == NULL) { 742 if (upl_openpipes(sc)) { 743 splx(s); 744 return; 745 } 746 } 747 748 ifp->if_flags |= IFF_RUNNING; 749 ifp->if_flags &= ~IFF_OACTIVE; 750 751 splx(s); 752 } 753 754 Static int 755 upl_openpipes(struct upl_softc *sc) 756 { 757 struct upl_chain *c; 758 usbd_status err; 759 int i; 760 761 /* Open RX and TX pipes. */ 762 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_RX], 763 USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_RX]); 764 if (err) { 765 printf("%s: open rx pipe failed: %s\n", 766 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 767 return (EIO); 768 } 769 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UPL_ENDPT_TX], 770 USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_TX]); 771 if (err) { 772 printf("%s: open tx pipe failed: %s\n", 773 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 774 return (EIO); 775 } 776 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ed[UPL_ENDPT_INTR], 777 USBD_EXCLUSIVE_USE, &sc->sc_ep[UPL_ENDPT_INTR], sc, 778 &sc->sc_ibuf, UPL_INTR_PKTLEN, upl_intr, 779 UPL_INTR_INTERVAL); 780 if (err) { 781 printf("%s: open intr pipe failed: %s\n", 782 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 783 return (EIO); 784 } 785 786 787 #if 1 788 /* Start up the receive pipe. */ 789 for (i = 0; i < UPL_RX_LIST_CNT; i++) { 790 c = &sc->sc_cdata.upl_rx_chain[i]; 791 usbd_setup_xfer(c->upl_xfer, sc->sc_ep[UPL_ENDPT_RX], 792 c, c->upl_buf, UPL_BUFSZ, 793 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, 794 upl_rxeof); 795 usbd_transfer(c->upl_xfer); 796 } 797 #endif 798 799 return (0); 800 } 801 802 Static void 803 upl_intr(usbd_xfer_handle xfer, usbd_private_handle priv, 804 usbd_status status) 805 { 806 struct upl_softc *sc = priv; 807 struct ifnet *ifp = &sc->sc_if; 808 uByte stat; 809 810 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__)); 811 812 if (sc->sc_dying) 813 return; 814 815 if (!(ifp->if_flags & IFF_RUNNING)) 816 return; 817 818 if (status != USBD_NORMAL_COMPLETION) { 819 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 820 return; 821 } 822 sc->sc_intr_errs++; 823 if (usbd_ratecheck(&sc->sc_rx_notice)) { 824 printf("%s: %u usb errors on intr: %s\n", 825 USBDEVNAME(sc->sc_dev), sc->sc_rx_errs, 826 usbd_errstr(status)); 827 sc->sc_intr_errs = 0; 828 } 829 if (status == USBD_STALLED) 830 usbd_clear_endpoint_stall_async(sc->sc_ep[UPL_ENDPT_RX]); 831 return; 832 } 833 834 stat = sc->sc_ibuf; 835 836 if (stat == 0) 837 return; 838 839 DPRINTFN(10,("%s: %s: stat=0x%02x\n", USBDEVNAME(sc->sc_dev), 840 __func__, stat)); 841 842 } 843 844 Static int 845 upl_ioctl(struct ifnet *ifp, u_long command, void *data) 846 { 847 struct upl_softc *sc = ifp->if_softc; 848 struct ifaddr *ifa = (struct ifaddr *)data; 849 struct ifreq *ifr = (struct ifreq *)data; 850 int s, error = 0; 851 852 if (sc->sc_dying) 853 return (EIO); 854 855 DPRINTFN(5,("%s: %s: cmd=0x%08lx\n", 856 USBDEVNAME(sc->sc_dev), __func__, command)); 857 858 s = splnet(); 859 860 switch(command) { 861 case SIOCSIFADDR: 862 ifp->if_flags |= IFF_UP; 863 upl_init(sc); 864 865 switch (ifa->ifa_addr->sa_family) { 866 #ifdef INET 867 case AF_INET: 868 break; 869 #endif /* INET */ 870 } 871 break; 872 873 case SIOCSIFMTU: 874 if (ifr->ifr_mtu > UPL_BUFSZ) 875 error = EINVAL; 876 else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET) 877 error = 0; 878 break; 879 880 case SIOCSIFFLAGS: 881 if (ifp->if_flags & IFF_UP) { 882 if (!(ifp->if_flags & IFF_RUNNING)) 883 upl_init(sc); 884 } else { 885 if (ifp->if_flags & IFF_RUNNING) 886 upl_stop(sc); 887 } 888 error = 0; 889 break; 890 default: 891 error = EINVAL; 892 break; 893 } 894 895 splx(s); 896 897 return (error); 898 } 899 900 Static void 901 upl_watchdog(struct ifnet *ifp) 902 { 903 struct upl_softc *sc = ifp->if_softc; 904 905 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__)); 906 907 if (sc->sc_dying) 908 return; 909 910 ifp->if_oerrors++; 911 printf("%s: watchdog timeout\n", USBDEVNAME(sc->sc_dev)); 912 913 upl_stop(sc); 914 upl_init(sc); 915 916 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 917 upl_start(ifp); 918 } 919 920 /* 921 * Stop the adapter and free any mbufs allocated to the 922 * RX and TX lists. 923 */ 924 Static void 925 upl_stop(struct upl_softc *sc) 926 { 927 usbd_status err; 928 struct ifnet *ifp; 929 int i; 930 931 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__)); 932 933 ifp = &sc->sc_if; 934 ifp->if_timer = 0; 935 936 /* Stop transfers. */ 937 if (sc->sc_ep[UPL_ENDPT_RX] != NULL) { 938 err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_RX]); 939 if (err) { 940 printf("%s: abort rx pipe failed: %s\n", 941 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 942 } 943 err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_RX]); 944 if (err) { 945 printf("%s: close rx pipe failed: %s\n", 946 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 947 } 948 sc->sc_ep[UPL_ENDPT_RX] = NULL; 949 } 950 951 if (sc->sc_ep[UPL_ENDPT_TX] != NULL) { 952 err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_TX]); 953 if (err) { 954 printf("%s: abort tx pipe failed: %s\n", 955 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 956 } 957 err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_TX]); 958 if (err) { 959 printf("%s: close tx pipe failed: %s\n", 960 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 961 } 962 sc->sc_ep[UPL_ENDPT_TX] = NULL; 963 } 964 965 if (sc->sc_ep[UPL_ENDPT_INTR] != NULL) { 966 err = usbd_abort_pipe(sc->sc_ep[UPL_ENDPT_INTR]); 967 if (err) { 968 printf("%s: abort intr pipe failed: %s\n", 969 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 970 } 971 err = usbd_close_pipe(sc->sc_ep[UPL_ENDPT_INTR]); 972 if (err) { 973 printf("%s: close intr pipe failed: %s\n", 974 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 975 } 976 sc->sc_ep[UPL_ENDPT_INTR] = NULL; 977 } 978 979 /* Free RX resources. */ 980 for (i = 0; i < UPL_RX_LIST_CNT; i++) { 981 if (sc->sc_cdata.upl_rx_chain[i].upl_mbuf != NULL) { 982 m_freem(sc->sc_cdata.upl_rx_chain[i].upl_mbuf); 983 sc->sc_cdata.upl_rx_chain[i].upl_mbuf = NULL; 984 } 985 if (sc->sc_cdata.upl_rx_chain[i].upl_xfer != NULL) { 986 usbd_free_xfer(sc->sc_cdata.upl_rx_chain[i].upl_xfer); 987 sc->sc_cdata.upl_rx_chain[i].upl_xfer = NULL; 988 } 989 } 990 991 /* Free TX resources. */ 992 for (i = 0; i < UPL_TX_LIST_CNT; i++) { 993 if (sc->sc_cdata.upl_tx_chain[i].upl_mbuf != NULL) { 994 m_freem(sc->sc_cdata.upl_tx_chain[i].upl_mbuf); 995 sc->sc_cdata.upl_tx_chain[i].upl_mbuf = NULL; 996 } 997 if (sc->sc_cdata.upl_tx_chain[i].upl_xfer != NULL) { 998 usbd_free_xfer(sc->sc_cdata.upl_tx_chain[i].upl_xfer); 999 sc->sc_cdata.upl_tx_chain[i].upl_xfer = NULL; 1000 } 1001 } 1002 1003 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1004 } 1005 1006 Static int 1007 upl_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 1008 struct rtentry *rt0) 1009 { 1010 int s, len, error; 1011 ALTQ_DECL(struct altq_pktattr pktattr;) 1012 1013 DPRINTFN(10,("%s: %s: enter\n", 1014 USBDEVNAME(((struct upl_softc *)ifp->if_softc)->sc_dev), 1015 __func__)); 1016 1017 /* 1018 * if the queueing discipline needs packet classification, 1019 * do it now. 1020 */ 1021 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr); 1022 1023 len = m->m_pkthdr.len; 1024 s = splnet(); 1025 /* 1026 * Queue message on interface, and start output if interface 1027 * not yet active. 1028 */ 1029 IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error); 1030 if (error) { 1031 /* mbuf is already freed */ 1032 splx(s); 1033 return (error); 1034 } 1035 ifp->if_obytes += len; 1036 if ((ifp->if_flags & IFF_OACTIVE) == 0) 1037 (*ifp->if_start)(ifp); 1038 splx(s); 1039 1040 return (0); 1041 } 1042 1043 Static void 1044 upl_input(struct ifnet *ifp, struct mbuf *m) 1045 { 1046 #ifdef INET 1047 struct ifqueue *inq; 1048 int s; 1049 1050 /* XXX Assume all traffic is IP */ 1051 1052 schednetisr(NETISR_IP); 1053 inq = &ipintrq; 1054 1055 s = splnet(); 1056 if (IF_QFULL(inq)) { 1057 IF_DROP(inq); 1058 splx(s); 1059 #if 0 1060 if (sc->sc_flags & SC_DEBUG) 1061 printf("%s: input queue full\n", ifp->if_xname); 1062 #endif 1063 ifp->if_iqdrops++; 1064 return; 1065 } 1066 IF_ENQUEUE(inq, m); 1067 splx(s); 1068 #endif 1069 ifp->if_ipackets++; 1070 ifp->if_ibytes += m->m_len; 1071 } 1072