1 /* $OpenBSD: if_ugl.c,v 1.26 2020/07/31 10:49:32 mglocker Exp $ */ 2 /* $NetBSD: if_upl.c,v 1.19 2002/07/11 21:14:26 augustss Exp $ */ 3 /* 4 * Copyright (c) 2013 SASANO Takayoshi <uaa@uaa.org.uk> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* 20 * Copyright (c) 2000 The NetBSD Foundation, Inc. 21 * All rights reserved. 22 * 23 * This code is derived from software contributed to The NetBSD Foundation 24 * by Lennart Augustsson (lennart@augustsson.net) at 25 * Carlstedt Research & Technology. 26 * 27 * Redistribution and use in source and binary forms, with or without 28 * modification, are permitted provided that the following conditions 29 * are met: 30 * 1. Redistributions of source code must retain the above copyright 31 * notice, this list of conditions and the following disclaimer. 32 * 2. Redistributions in binary form must reproduce the above copyright 33 * notice, this list of conditions and the following disclaimer in the 34 * documentation and/or other materials provided with the distribution. 35 * 36 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 37 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 38 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 40 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 41 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 42 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 43 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 44 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 46 * POSSIBILITY OF SUCH DAMAGE. 47 */ 48 49 /* 50 * Genesys Logic GL620USB-A driver 51 * This driver is based on Prolific PL2301/PL2302 driver (if_upl.c). 52 */ 53 54 #include <bpfilter.h> 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/timeout.h> 59 #include <sys/sockio.h> 60 #include <sys/mbuf.h> 61 #include <sys/kernel.h> 62 #include <sys/socket.h> 63 64 #include <sys/device.h> 65 66 #include <net/if.h> 67 68 #if NBPFILTER > 0 69 #include <net/bpf.h> 70 #endif 71 72 #include <netinet/in.h> 73 #include <netinet/if_ether.h> 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 #define UGL_INTR_PKTLEN 8 81 #define UGL_BULK_PKTLEN 64 82 83 /***/ 84 85 #define UGL_INTR_INTERVAL 20 86 87 #define UGL_MAX_MTU 1514 88 #define UGL_BUFSZ roundup(sizeof(struct ugl_packet), UGL_BULK_PKTLEN) 89 90 #define UGL_RX_FRAMES 1 /* must be one */ 91 #define UGL_TX_FRAMES 1 /* must be one */ 92 93 #define UGL_RX_LIST_CNT 1 94 #define UGL_TX_LIST_CNT 1 95 96 #define UGL_ENDPT_RX 0x0 97 #define UGL_ENDPT_TX 0x1 98 #define UGL_ENDPT_INTR 0x2 99 #define UGL_ENDPT_MAX 0x3 100 101 struct ugl_softc; 102 103 struct ugl_packet { 104 uDWord pkt_count; 105 uDWord pkt_length; 106 char pkt_data[UGL_MAX_MTU]; 107 } __packed; 108 109 struct ugl_chain { 110 struct ugl_softc *ugl_sc; 111 struct usbd_xfer *ugl_xfer; 112 struct ugl_packet *ugl_buf; 113 struct mbuf *ugl_mbuf; 114 int ugl_idx; 115 }; 116 117 struct ugl_cdata { 118 struct ugl_chain ugl_tx_chain[UGL_TX_LIST_CNT]; 119 struct ugl_chain ugl_rx_chain[UGL_RX_LIST_CNT]; 120 int ugl_tx_prod; 121 int ugl_tx_cons; 122 int ugl_tx_cnt; 123 int ugl_rx_prod; 124 }; 125 126 struct ugl_softc { 127 struct device sc_dev; 128 129 struct arpcom sc_arpcom; 130 #define GET_IFP(sc) (&(sc)->sc_arpcom.ac_if) 131 struct timeout sc_stat_ch; 132 133 struct usbd_device *sc_udev; 134 struct usbd_interface *sc_iface; 135 int sc_ed[UGL_ENDPT_MAX]; 136 struct usbd_pipe *sc_ep[UGL_ENDPT_MAX]; 137 struct ugl_cdata sc_cdata; 138 139 uByte sc_ibuf[UGL_INTR_PKTLEN]; 140 141 u_int sc_rx_errs; 142 struct timeval sc_rx_notice; 143 u_int sc_intr_errs; 144 }; 145 146 #ifdef UGL_DEBUG 147 #define DPRINTF(x) do { if (ugldebug) printf x; } while (0) 148 #define DPRINTFN(n,x) do { if (ugldebug >= (n)) printf x; } while (0) 149 int ugldebug = 0; 150 #else 151 #define DPRINTF(x) 152 #define DPRINTFN(n,x) 153 #endif 154 155 /* 156 * Various supported device vendors/products. 157 */ 158 struct usb_devno ugl_devs[] = { 159 { USB_VENDOR_GENESYS, USB_PRODUCT_GENESYS_GL620USB_A }, 160 }; 161 162 int ugl_match(struct device *, void *, void *); 163 void ugl_attach(struct device *, struct device *, void *); 164 int ugl_detach(struct device *, int); 165 166 struct cfdriver ugl_cd = { 167 NULL, "ugl", DV_IFNET 168 }; 169 170 const struct cfattach ugl_ca = { 171 sizeof(struct ugl_softc), ugl_match, ugl_attach, ugl_detach 172 }; 173 174 int ugl_openpipes(struct ugl_softc *); 175 int ugl_tx_list_init(struct ugl_softc *); 176 int ugl_rx_list_init(struct ugl_softc *); 177 int ugl_newbuf(struct ugl_softc *, struct ugl_chain *, struct mbuf *); 178 int ugl_send(struct ugl_softc *, struct mbuf *, int); 179 void ugl_intr(struct usbd_xfer *, void *, usbd_status); 180 void ugl_rxeof(struct usbd_xfer *, void *, usbd_status); 181 void ugl_txeof(struct usbd_xfer *, void *, usbd_status); 182 void ugl_start(struct ifnet *); 183 int ugl_ioctl(struct ifnet *, u_long, caddr_t); 184 void ugl_init(void *); 185 void ugl_stop(struct ugl_softc *); 186 void ugl_watchdog(struct ifnet *); 187 188 /* 189 * Probe for a Genesys Logic chip. 190 */ 191 int 192 ugl_match(struct device *parent, void *match, void *aux) 193 { 194 struct usb_attach_arg *uaa = aux; 195 196 if (uaa->iface == NULL || uaa->configno != 1) 197 return (UMATCH_NONE); 198 199 return (usb_lookup(ugl_devs, uaa->vendor, uaa->product) != NULL ? 200 UMATCH_VENDOR_PRODUCT_CONF_IFACE : UMATCH_NONE); 201 } 202 203 void 204 ugl_attach(struct device *parent, struct device *self, void *aux) 205 { 206 struct ugl_softc *sc = (struct ugl_softc *)self; 207 struct usb_attach_arg *uaa = aux; 208 int s; 209 struct usbd_device *dev = uaa->device; 210 struct usbd_interface *iface = uaa->iface; 211 struct ifnet *ifp = GET_IFP(sc); 212 usb_interface_descriptor_t *id; 213 usb_endpoint_descriptor_t *ed; 214 int i; 215 216 DPRINTFN(5,(" : ugl_attach: sc=%p, dev=%p", sc, dev)); 217 218 sc->sc_udev = dev; 219 sc->sc_iface = iface; 220 id = usbd_get_interface_descriptor(iface); 221 222 /* Find endpoints. */ 223 for (i = 0; i < id->bNumEndpoints; i++) { 224 ed = usbd_interface2endpoint_descriptor(iface, i); 225 if (ed == NULL) { 226 printf("%s: couldn't get ep %d\n", 227 sc->sc_dev.dv_xname, i); 228 return; 229 } 230 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 231 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 232 sc->sc_ed[UGL_ENDPT_RX] = ed->bEndpointAddress; 233 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 234 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 235 sc->sc_ed[UGL_ENDPT_TX] = ed->bEndpointAddress; 236 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 237 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 238 sc->sc_ed[UGL_ENDPT_INTR] = ed->bEndpointAddress; 239 } 240 } 241 242 if (sc->sc_ed[UGL_ENDPT_RX] == 0 || sc->sc_ed[UGL_ENDPT_TX] == 0 || 243 sc->sc_ed[UGL_ENDPT_INTR] == 0) { 244 printf("%s: missing endpoint\n", sc->sc_dev.dv_xname); 245 return; 246 } 247 248 s = splnet(); 249 250 ether_fakeaddr(ifp); 251 printf("%s: address %s\n", 252 sc->sc_dev.dv_xname, ether_sprintf(sc->sc_arpcom.ac_enaddr)); 253 254 /* Initialize interface info.*/ 255 ifp->if_softc = sc; 256 ifp->if_hardmtu = UGL_MAX_MTU; 257 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 258 ifp->if_ioctl = ugl_ioctl; 259 ifp->if_start = ugl_start; 260 ifp->if_watchdog = ugl_watchdog; 261 strlcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ); 262 263 /* Attach the interface. */ 264 if_attach(ifp); 265 ether_ifattach(ifp); 266 267 splx(s); 268 } 269 270 int 271 ugl_detach(struct device *self, int flags) 272 { 273 struct ugl_softc *sc = (struct ugl_softc *)self; 274 struct ifnet *ifp = GET_IFP(sc); 275 int s; 276 277 DPRINTFN(2,("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 278 279 s = splusb(); 280 281 if (ifp->if_flags & IFF_RUNNING) 282 ugl_stop(sc); 283 284 if (ifp->if_softc != NULL) 285 if_detach(ifp); 286 287 #ifdef DIAGNOSTIC 288 if (sc->sc_ep[UGL_ENDPT_TX] != NULL || 289 sc->sc_ep[UGL_ENDPT_RX] != NULL || 290 sc->sc_ep[UGL_ENDPT_INTR] != NULL) 291 printf("%s: detach has active endpoints\n", 292 sc->sc_dev.dv_xname); 293 #endif 294 295 splx(s); 296 297 return (0); 298 } 299 300 /* 301 * Initialize an RX descriptor and attach an MBUF cluster. 302 */ 303 int 304 ugl_newbuf(struct ugl_softc *sc, struct ugl_chain *c, struct mbuf *m) 305 { 306 struct mbuf *m_new = NULL; 307 308 DPRINTFN(8,("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 309 310 if (m == NULL) { 311 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 312 if (m_new == NULL) { 313 printf("%s: no memory for rx list " 314 "-- packet dropped!\n", sc->sc_dev.dv_xname); 315 return (ENOBUFS); 316 } 317 318 MCLGET(m_new, M_DONTWAIT); 319 if (!(m_new->m_flags & M_EXT)) { 320 printf("%s: no memory for rx list " 321 "-- packet dropped!\n", sc->sc_dev.dv_xname); 322 m_freem(m_new); 323 return (ENOBUFS); 324 } 325 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 326 } else { 327 m_new = m; 328 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 329 m_new->m_data = m_new->m_ext.ext_buf; 330 } 331 332 c->ugl_mbuf = m_new; 333 334 return (0); 335 } 336 337 int 338 ugl_rx_list_init(struct ugl_softc *sc) 339 { 340 struct ugl_cdata *cd; 341 struct ugl_chain *c; 342 int i; 343 344 DPRINTFN(5,("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 345 346 cd = &sc->sc_cdata; 347 for (i = 0; i < UGL_RX_LIST_CNT; i++) { 348 c = &cd->ugl_rx_chain[i]; 349 c->ugl_sc = sc; 350 c->ugl_idx = i; 351 if (ugl_newbuf(sc, c, NULL) == ENOBUFS) 352 return (ENOBUFS); 353 if (c->ugl_xfer == NULL) { 354 c->ugl_xfer = usbd_alloc_xfer(sc->sc_udev); 355 if (c->ugl_xfer == NULL) 356 return (ENOBUFS); 357 c->ugl_buf = usbd_alloc_buffer(c->ugl_xfer, UGL_BUFSZ); 358 if (c->ugl_buf == NULL) { 359 usbd_free_xfer(c->ugl_xfer); 360 return (ENOBUFS); 361 } 362 } 363 } 364 365 return (0); 366 } 367 368 int 369 ugl_tx_list_init(struct ugl_softc *sc) 370 { 371 struct ugl_cdata *cd; 372 struct ugl_chain *c; 373 int i; 374 375 DPRINTFN(5,("%s: %s: enter\n", sc->sc_dev.dv_xname, __func__)); 376 377 cd = &sc->sc_cdata; 378 for (i = 0; i < UGL_TX_LIST_CNT; i++) { 379 c = &cd->ugl_tx_chain[i]; 380 c->ugl_sc = sc; 381 c->ugl_idx = i; 382 c->ugl_mbuf = NULL; 383 if (c->ugl_xfer == NULL) { 384 c->ugl_xfer = usbd_alloc_xfer(sc->sc_udev); 385 if (c->ugl_xfer == NULL) 386 return (ENOBUFS); 387 c->ugl_buf = usbd_alloc_buffer(c->ugl_xfer, UGL_BUFSZ); 388 if (c->ugl_buf == NULL) { 389 usbd_free_xfer(c->ugl_xfer); 390 return (ENOBUFS); 391 } 392 } 393 } 394 395 return (0); 396 } 397 398 /* 399 * A frame has been uploaded: pass the resulting mbuf chain up to 400 * the higher level protocols. 401 */ 402 void 403 ugl_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 404 { 405 struct ugl_chain *c = priv; 406 struct ugl_softc *sc = c->ugl_sc; 407 struct ifnet *ifp = GET_IFP(sc); 408 struct mbuf_list ml = MBUF_LIST_INITIALIZER(); 409 struct mbuf *m; 410 int total_len = 0; 411 unsigned int packet_len, packet_count; 412 int s; 413 414 if (usbd_is_dying(sc->sc_udev)) 415 return; 416 417 if (!(ifp->if_flags & IFF_RUNNING)) 418 return; 419 420 if (status != USBD_NORMAL_COMPLETION) { 421 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 422 return; 423 sc->sc_rx_errs++; 424 if (usbd_ratecheck(&sc->sc_rx_notice)) { 425 printf("%s: %u usb errors on rx: %s\n", 426 sc->sc_dev.dv_xname, sc->sc_rx_errs, 427 usbd_errstr(status)); 428 sc->sc_rx_errs = 0; 429 } 430 if (status == USBD_STALLED) 431 usbd_clear_endpoint_stall_async(sc->sc_ep[UGL_ENDPT_RX]); 432 goto done; 433 } 434 435 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 436 437 DPRINTFN(9,("%s: %s: enter status=%d length=%d\n", 438 sc->sc_dev.dv_xname, __func__, status, total_len)); 439 440 if (total_len < offsetof(struct ugl_packet, pkt_data)) { 441 printf("%s: bad header (length=%d)\n", 442 sc->sc_dev.dv_xname, total_len); 443 444 goto done; 445 } 446 447 packet_count = UGETDW(c->ugl_buf->pkt_count); 448 if (packet_count != UGL_RX_FRAMES) { 449 printf("%s: bad packet count (%d)\n", 450 sc->sc_dev.dv_xname, packet_count); 451 452 if (packet_count == 0) 453 goto done; 454 } 455 456 packet_len = UGETDW(c->ugl_buf->pkt_length); 457 if (total_len < packet_len) { 458 printf("%s: bad packet size(%d), length=%d\n", 459 sc->sc_dev.dv_xname, packet_len, total_len); 460 461 if (packet_len == 0) 462 goto done; 463 } 464 465 m = c->ugl_mbuf; 466 memcpy(mtod(c->ugl_mbuf, char *), c->ugl_buf->pkt_data, packet_len); 467 468 m->m_pkthdr.len = m->m_len = packet_len; 469 ml_enqueue(&ml, m); 470 471 if (ugl_newbuf(sc, c, NULL) == ENOBUFS) { 472 ifp->if_ierrors++; 473 goto done; 474 } 475 476 s = splnet(); 477 if_input(ifp, &ml); 478 splx(s); 479 480 done: 481 /* Setup new transfer. */ 482 usbd_setup_xfer(c->ugl_xfer, sc->sc_ep[UGL_ENDPT_RX], 483 c, c->ugl_buf, UGL_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, 484 USBD_NO_TIMEOUT, ugl_rxeof); 485 usbd_transfer(c->ugl_xfer); 486 487 DPRINTFN(10,("%s: %s: start rx\n", sc->sc_dev.dv_xname, 488 __func__)); 489 } 490 491 /* 492 * A frame was downloaded to the chip. It's safe for us to clean up 493 * the list buffers. 494 */ 495 void 496 ugl_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 497 { 498 struct ugl_chain *c = priv; 499 struct ugl_softc *sc = c->ugl_sc; 500 struct ifnet *ifp = GET_IFP(sc); 501 int s; 502 503 if (usbd_is_dying(sc->sc_udev)) 504 return; 505 506 s = splnet(); 507 508 DPRINTFN(10,("%s: %s: enter status=%d\n", sc->sc_dev.dv_xname, 509 __func__, status)); 510 511 ifp->if_timer = 0; 512 ifq_clr_oactive(&ifp->if_snd); 513 514 if (status != USBD_NORMAL_COMPLETION) { 515 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 516 splx(s); 517 return; 518 } 519 ifp->if_oerrors++; 520 printf("%s: usb error on tx: %s\n", sc->sc_dev.dv_xname, 521 usbd_errstr(status)); 522 if (status == USBD_STALLED) 523 usbd_clear_endpoint_stall_async(sc->sc_ep[UGL_ENDPT_TX]); 524 splx(s); 525 return; 526 } 527 528 m_freem(c->ugl_mbuf); 529 c->ugl_mbuf = NULL; 530 531 if (ifq_empty(&ifp->if_snd) == 0) 532 ugl_start(ifp); 533 534 splx(s); 535 } 536 537 int 538 ugl_send(struct ugl_softc *sc, struct mbuf *m, int idx) 539 { 540 int total_len; 541 struct ugl_chain *c; 542 usbd_status err; 543 544 c = &sc->sc_cdata.ugl_tx_chain[idx]; 545 546 /* 547 * Copy the mbuf data into a contiguous buffer, leaving two 548 * bytes at the beginning to hold the frame length. 549 */ 550 USETDW(c->ugl_buf->pkt_count, UGL_TX_FRAMES); 551 USETDW(c->ugl_buf->pkt_length, m->m_pkthdr.len); 552 m_copydata(m, 0, m->m_pkthdr.len, c->ugl_buf->pkt_data); 553 c->ugl_mbuf = m; 554 555 total_len = offsetof(struct ugl_packet, pkt_data[m->m_pkthdr.len]); 556 557 DPRINTFN(10,("%s: %s: total_len=%d\n", 558 sc->sc_dev.dv_xname, __func__, total_len)); 559 560 usbd_setup_xfer(c->ugl_xfer, sc->sc_ep[UGL_ENDPT_TX], 561 c, c->ugl_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, 562 USBD_DEFAULT_TIMEOUT, ugl_txeof); 563 564 /* Transmit */ 565 err = usbd_transfer(c->ugl_xfer); 566 if (err != USBD_IN_PROGRESS) { 567 printf("%s: ugl_send error=%s\n", sc->sc_dev.dv_xname, 568 usbd_errstr(err)); 569 c->ugl_mbuf = NULL; 570 ugl_stop(sc); 571 return (EIO); 572 } 573 574 sc->sc_cdata.ugl_tx_cnt++; 575 576 return (0); 577 } 578 579 void 580 ugl_start(struct ifnet *ifp) 581 { 582 struct ugl_softc *sc = ifp->if_softc; 583 struct mbuf *m_head = NULL; 584 585 if (usbd_is_dying(sc->sc_udev)) 586 return; 587 588 DPRINTFN(10,("%s: %s: enter\n", sc->sc_dev.dv_xname,__func__)); 589 590 if (ifq_is_oactive(&ifp->if_snd)) 591 return; 592 593 m_head = ifq_dequeue(&ifp->if_snd); 594 if (m_head == NULL) 595 return; 596 597 if (ugl_send(sc, m_head, 0)) { 598 m_freem(m_head); 599 ifq_set_oactive(&ifp->if_snd); 600 return; 601 } 602 603 #if NBPFILTER > 0 604 /* 605 * If there's a BPF listener, bounce a copy of this frame 606 * to him. 607 */ 608 if (ifp->if_bpf) 609 bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT); 610 #endif 611 612 ifq_set_oactive(&ifp->if_snd); 613 614 /* 615 * Set a timeout in case the chip goes out to lunch. 616 */ 617 ifp->if_timer = 5; 618 } 619 620 void 621 ugl_init(void *xsc) 622 { 623 struct ugl_softc *sc = xsc; 624 struct ifnet *ifp = GET_IFP(sc); 625 int s; 626 627 if (usbd_is_dying(sc->sc_udev)) 628 return; 629 630 DPRINTFN(10,("%s: %s: enter\n", sc->sc_dev.dv_xname,__func__)); 631 632 s = splnet(); 633 634 /* Init TX ring. */ 635 if (ugl_tx_list_init(sc) == ENOBUFS) { 636 printf("%s: tx list init failed\n", sc->sc_dev.dv_xname); 637 splx(s); 638 return; 639 } 640 641 /* Init RX ring. */ 642 if (ugl_rx_list_init(sc) == ENOBUFS) { 643 printf("%s: rx list init failed\n", sc->sc_dev.dv_xname); 644 splx(s); 645 return; 646 } 647 648 if (sc->sc_ep[UGL_ENDPT_RX] == NULL) { 649 if (ugl_openpipes(sc)) { 650 splx(s); 651 return; 652 } 653 } 654 655 ifp->if_flags |= IFF_RUNNING; 656 ifq_clr_oactive(&ifp->if_snd); 657 658 splx(s); 659 } 660 661 int 662 ugl_openpipes(struct ugl_softc *sc) 663 { 664 struct ugl_chain *c; 665 usbd_status err; 666 int i; 667 668 /* Open RX and TX pipes. */ 669 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UGL_ENDPT_RX], 670 USBD_EXCLUSIVE_USE, &sc->sc_ep[UGL_ENDPT_RX]); 671 if (err) { 672 printf("%s: open rx pipe failed: %s\n", 673 sc->sc_dev.dv_xname, usbd_errstr(err)); 674 return (EIO); 675 } 676 err = usbd_open_pipe(sc->sc_iface, sc->sc_ed[UGL_ENDPT_TX], 677 USBD_EXCLUSIVE_USE, &sc->sc_ep[UGL_ENDPT_TX]); 678 if (err) { 679 printf("%s: open tx pipe failed: %s\n", 680 sc->sc_dev.dv_xname, usbd_errstr(err)); 681 return (EIO); 682 } 683 err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_ed[UGL_ENDPT_INTR], 684 USBD_EXCLUSIVE_USE, &sc->sc_ep[UGL_ENDPT_INTR], sc, 685 sc->sc_ibuf, UGL_INTR_PKTLEN, ugl_intr, 686 UGL_INTR_INTERVAL); 687 if (err) { 688 printf("%s: open intr pipe failed: %s\n", 689 sc->sc_dev.dv_xname, usbd_errstr(err)); 690 return (EIO); 691 } 692 693 /* Start up the receive pipe. */ 694 for (i = 0; i < UGL_RX_LIST_CNT; i++) { 695 c = &sc->sc_cdata.ugl_rx_chain[i]; 696 usbd_setup_xfer(c->ugl_xfer, sc->sc_ep[UGL_ENDPT_RX], 697 c, c->ugl_buf, UGL_BUFSZ, 698 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, 699 ugl_rxeof); 700 usbd_transfer(c->ugl_xfer); 701 } 702 703 return (0); 704 } 705 706 void 707 ugl_intr(struct usbd_xfer *xfer, void *priv, usbd_status status) 708 { 709 struct ugl_softc *sc = priv; 710 struct ifnet *ifp = GET_IFP(sc); 711 int i; 712 713 DPRINTFN(15,("%s: %s: enter\n", sc->sc_dev.dv_xname,__func__)); 714 715 if (usbd_is_dying(sc->sc_udev)) 716 return; 717 718 if (!(ifp->if_flags & IFF_RUNNING)) 719 return; 720 721 if (status != USBD_NORMAL_COMPLETION) { 722 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 723 return; 724 } 725 sc->sc_intr_errs++; 726 if (usbd_ratecheck(&sc->sc_rx_notice)) { 727 printf("%s: %u usb errors on intr: %s\n", 728 sc->sc_dev.dv_xname, sc->sc_rx_errs, 729 usbd_errstr(status)); 730 sc->sc_intr_errs = 0; 731 } 732 if (status == USBD_STALLED) 733 usbd_clear_endpoint_stall_async(sc->sc_ep[UGL_ENDPT_RX]); 734 return; 735 } 736 737 DPRINTFN(10,("%s: %s:", sc->sc_dev.dv_xname, __func__)); 738 for (i = 0; i < UGL_INTR_PKTLEN; i++) 739 DPRINTFN(10,(" 0x%02x", sc->sc_ibuf[i])); 740 DPRINTFN(10,("\n")); 741 742 } 743 744 int 745 ugl_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 746 { 747 struct ugl_softc *sc = ifp->if_softc; 748 int s, error = 0; 749 750 if (usbd_is_dying(sc->sc_udev)) 751 return ENXIO; 752 753 DPRINTFN(5,("%s: %s: cmd=0x%08lx\n", 754 sc->sc_dev.dv_xname, __func__, command)); 755 756 s = splnet(); 757 758 switch(command) { 759 case SIOCSIFADDR: 760 ifp->if_flags |= IFF_UP; 761 if (!(ifp->if_flags & IFF_RUNNING)) 762 ugl_init(sc); 763 break; 764 765 case SIOCSIFFLAGS: 766 if (ifp->if_flags & IFF_UP) { 767 if (ifp->if_flags & IFF_RUNNING) 768 error = ENETRESET; 769 else 770 ugl_init(sc); 771 } else { 772 if (ifp->if_flags & IFF_RUNNING) 773 ugl_stop(sc); 774 } 775 break; 776 777 default: 778 error = ether_ioctl(ifp, &sc->sc_arpcom, command, data); 779 break; 780 } 781 782 if (error == ENETRESET) 783 error = 0; 784 785 splx(s); 786 return (error); 787 } 788 789 void 790 ugl_watchdog(struct ifnet *ifp) 791 { 792 struct ugl_softc *sc = ifp->if_softc; 793 794 if (usbd_is_dying(sc->sc_udev)) 795 return; 796 797 ifp->if_oerrors++; 798 printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname); 799 } 800 801 /* 802 * Stop the adapter and free any mbufs allocated to the 803 * RX and TX lists. 804 */ 805 void 806 ugl_stop(struct ugl_softc *sc) 807 { 808 struct ifnet *ifp; 809 int i; 810 811 DPRINTFN(10,("%s: %s: enter\n", sc->sc_dev.dv_xname,__func__)); 812 813 ifp = GET_IFP(sc); 814 ifp->if_timer = 0; 815 ifp->if_flags &= ~IFF_RUNNING; 816 ifq_clr_oactive(&ifp->if_snd); 817 818 /* Stop transfers. */ 819 if (sc->sc_ep[UGL_ENDPT_RX] != NULL) { 820 usbd_close_pipe(sc->sc_ep[UGL_ENDPT_RX]); 821 sc->sc_ep[UGL_ENDPT_RX] = NULL; 822 } 823 824 if (sc->sc_ep[UGL_ENDPT_TX] != NULL) { 825 usbd_close_pipe(sc->sc_ep[UGL_ENDPT_TX]); 826 sc->sc_ep[UGL_ENDPT_TX] = NULL; 827 } 828 829 if (sc->sc_ep[UGL_ENDPT_INTR] != NULL) { 830 usbd_close_pipe(sc->sc_ep[UGL_ENDPT_INTR]); 831 sc->sc_ep[UGL_ENDPT_INTR] = NULL; 832 } 833 834 /* Free RX resources. */ 835 for (i = 0; i < UGL_RX_LIST_CNT; i++) { 836 if (sc->sc_cdata.ugl_rx_chain[i].ugl_mbuf != NULL) { 837 m_freem(sc->sc_cdata.ugl_rx_chain[i].ugl_mbuf); 838 sc->sc_cdata.ugl_rx_chain[i].ugl_mbuf = NULL; 839 } 840 if (sc->sc_cdata.ugl_rx_chain[i].ugl_xfer != NULL) { 841 usbd_free_xfer(sc->sc_cdata.ugl_rx_chain[i].ugl_xfer); 842 sc->sc_cdata.ugl_rx_chain[i].ugl_xfer = NULL; 843 } 844 } 845 846 /* Free TX resources. */ 847 for (i = 0; i < UGL_TX_LIST_CNT; i++) { 848 if (sc->sc_cdata.ugl_tx_chain[i].ugl_mbuf != NULL) { 849 m_freem(sc->sc_cdata.ugl_tx_chain[i].ugl_mbuf); 850 sc->sc_cdata.ugl_tx_chain[i].ugl_mbuf = NULL; 851 } 852 if (sc->sc_cdata.ugl_tx_chain[i].ugl_xfer != NULL) { 853 usbd_free_xfer(sc->sc_cdata.ugl_tx_chain[i].ugl_xfer); 854 sc->sc_cdata.ugl_tx_chain[i].ugl_xfer = NULL; 855 } 856 } 857 } 858