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