1 /* $OpenBSD: if_mos.c,v 1.43 2020/07/31 10:49:32 mglocker Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Johann Christian Rode <jcrode@gmx.net> 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) 2005, 2006, 2007 Jonathan Gray <jsg@openbsd.org> 21 * 22 * Permission to use, copy, modify, and distribute this software for any 23 * purpose with or without fee is hereby granted, provided that the above 24 * copyright notice and this permission notice appear in all copies. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 27 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 28 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 29 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 30 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 31 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 32 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 33 */ 34 35 /* 36 * Copyright (c) 1997, 1998, 1999, 2000-2003 37 * Bill Paul <wpaul@windriver.com>. All rights reserved. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. All advertising materials mentioning features or use of this software 48 * must display the following acknowledgement: 49 * This product includes software developed by Bill Paul. 50 * 4. Neither the name of the author nor the names of any co-contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 58 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 59 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 60 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 61 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 62 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 63 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 64 * THE POSSIBILITY OF SUCH DAMAGE. 65 */ 66 67 /* 68 * Moschip MCS7730/MCS7830/MCS7832 USB to Ethernet controller 69 * The datasheet is available at the following URL: 70 * http://www.moschip.com/data/products/MCS7830/Data%20Sheet_7830.pdf 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/rwlock.h> 79 #include <sys/mbuf.h> 80 #include <sys/kernel.h> 81 #include <sys/socket.h> 82 83 #include <sys/device.h> 84 85 #include <machine/bus.h> 86 87 #include <net/if.h> 88 #include <net/if_media.h> 89 90 #if NBPFILTER > 0 91 #include <net/bpf.h> 92 #endif 93 94 #include <netinet/in.h> 95 #include <netinet/if_ether.h> 96 97 #include <dev/mii/miivar.h> 98 99 #include <dev/usb/usb.h> 100 #include <dev/usb/usbdi.h> 101 #include <dev/usb/usbdi_util.h> 102 #include <dev/usb/usbdivar.h> 103 #include <dev/usb/usbdevs.h> 104 105 #include <dev/usb/if_mosreg.h> 106 107 #ifdef MOS_DEBUG 108 #define DPRINTF(x) do { if (mosdebug) printf x; } while (0) 109 #define DPRINTFN(n,x) do { if (mosdebug >= (n)) printf x; } while (0) 110 int mosdebug = 0; 111 #else 112 #define DPRINTF(x) 113 #define DPRINTFN(n,x) 114 #endif 115 116 /* 117 * Various supported device vendors/products. 118 */ 119 const struct mos_type mos_devs[] = { 120 { { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7730 }, MCS7730 }, 121 { { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7830 }, MCS7830 }, 122 { { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7832 }, MCS7832 }, 123 { { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN030 }, MCS7830 }, 124 }; 125 #define mos_lookup(v, p) ((struct mos_type *)usb_lookup(mos_devs, v, p)) 126 127 int mos_match(struct device *, void *, void *); 128 void mos_attach(struct device *, struct device *, void *); 129 int mos_detach(struct device *, int); 130 131 struct cfdriver mos_cd = { 132 NULL, "mos", DV_IFNET 133 }; 134 135 const struct cfattach mos_ca = { 136 sizeof(struct mos_softc), mos_match, mos_attach, mos_detach 137 }; 138 139 int mos_tx_list_init(struct mos_softc *); 140 int mos_rx_list_init(struct mos_softc *); 141 struct mbuf *mos_newbuf(void); 142 int mos_encap(struct mos_softc *, struct mbuf *, int); 143 void mos_rxeof(struct usbd_xfer *, void *, usbd_status); 144 void mos_txeof(struct usbd_xfer *, void *, usbd_status); 145 void mos_tick(void *); 146 void mos_tick_task(void *); 147 void mos_start(struct ifnet *); 148 int mos_ioctl(struct ifnet *, u_long, caddr_t); 149 void mos_init(void *); 150 void mos_chip_init(struct mos_softc *); 151 void mos_stop(struct mos_softc *); 152 void mos_watchdog(struct ifnet *); 153 int mos_miibus_readreg(struct device *, int, int); 154 void mos_miibus_writereg(struct device *, int, int, int); 155 void mos_miibus_statchg(struct device *); 156 int mos_ifmedia_upd(struct ifnet *); 157 void mos_ifmedia_sts(struct ifnet *, struct ifmediareq *); 158 void mos_reset(struct mos_softc *sc); 159 160 int mos_reg_read_1(struct mos_softc *, int); 161 int mos_reg_read_2(struct mos_softc *, int); 162 int mos_reg_write_1(struct mos_softc *, int, int); 163 int mos_reg_write_2(struct mos_softc *, int, int); 164 int mos_readmac(struct mos_softc *, u_char *); 165 int mos_writemac(struct mos_softc *, u_char *); 166 int mos_write_mcast(struct mos_softc *, u_char *); 167 168 void mos_iff(struct mos_softc *); 169 void mos_lock_mii(struct mos_softc *); 170 void mos_unlock_mii(struct mos_softc *); 171 172 /* 173 * Get exclusive access to the MII registers 174 */ 175 void 176 mos_lock_mii(struct mos_softc *sc) 177 { 178 sc->mos_refcnt++; 179 rw_enter_write(&sc->mos_mii_lock); 180 } 181 182 void 183 mos_unlock_mii(struct mos_softc *sc) 184 { 185 rw_exit_write(&sc->mos_mii_lock); 186 if (--sc->mos_refcnt < 0) 187 usb_detach_wakeup(&sc->mos_dev); 188 } 189 190 int 191 mos_reg_read_1(struct mos_softc *sc, int reg) 192 { 193 usb_device_request_t req; 194 usbd_status err; 195 uByte val = 0; 196 197 if (usbd_is_dying(sc->mos_udev)) 198 return(0); 199 200 req.bmRequestType = UT_READ_VENDOR_DEVICE; 201 req.bRequest = MOS_UR_READREG; 202 USETW(req.wValue, 0); 203 USETW(req.wIndex, reg); 204 USETW(req.wLength, 1); 205 206 err = usbd_do_request(sc->mos_udev, &req, &val); 207 208 if (err) { 209 DPRINTF(("mos_reg_read_1 error, reg: %d\n", reg)); 210 return (-1); 211 } 212 213 return (val); 214 } 215 216 int 217 mos_reg_read_2(struct mos_softc *sc, int reg) 218 { 219 usb_device_request_t req; 220 usbd_status err; 221 uWord val; 222 223 USETW(val,0); 224 225 if (usbd_is_dying(sc->mos_udev)) 226 return(0); 227 228 req.bmRequestType = UT_READ_VENDOR_DEVICE; 229 req.bRequest = MOS_UR_READREG; 230 USETW(req.wValue, 0); 231 USETW(req.wIndex, reg); 232 USETW(req.wLength, 2); 233 234 err = usbd_do_request(sc->mos_udev, &req, &val); 235 236 if (err) { 237 DPRINTF(("mos_reg_read_2 error, reg: %d\n", reg)); 238 return (-1); 239 } 240 241 return(UGETW(val)); 242 } 243 244 int 245 mos_reg_write_1(struct mos_softc *sc, int reg, int aval) 246 { 247 usb_device_request_t req; 248 usbd_status err; 249 uByte val; 250 251 val = aval; 252 253 if (usbd_is_dying(sc->mos_udev)) 254 return(0); 255 256 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 257 req.bRequest = MOS_UR_WRITEREG; 258 USETW(req.wValue, 0); 259 USETW(req.wIndex, reg); 260 USETW(req.wLength, 1); 261 262 err = usbd_do_request(sc->mos_udev, &req, &val); 263 264 if (err) { 265 DPRINTF(("mos_reg_write_1 error, reg: %d\n", reg)); 266 return (-1); 267 } 268 269 return(0); 270 } 271 272 int 273 mos_reg_write_2(struct mos_softc *sc, int reg, int aval) 274 { 275 usb_device_request_t req; 276 usbd_status err; 277 uWord val; 278 279 USETW(val, aval); 280 281 if (usbd_is_dying(sc->mos_udev)) 282 return (0); 283 284 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 285 req.bRequest = MOS_UR_WRITEREG; 286 USETW(req.wValue, 0); 287 USETW(req.wIndex, reg); 288 USETW(req.wLength, 2); 289 290 err = usbd_do_request(sc->mos_udev, &req, &val); 291 292 if (err) { 293 DPRINTF(("mos_reg_write_2 error, reg: %d\n", reg)); 294 return (-1); 295 } 296 297 return (0); 298 } 299 300 int 301 mos_readmac(struct mos_softc *sc, u_char *mac) 302 { 303 usb_device_request_t req; 304 usbd_status err; 305 306 if (usbd_is_dying(sc->mos_udev)) 307 return(0); 308 309 req.bmRequestType = UT_READ_VENDOR_DEVICE; 310 req.bRequest = MOS_UR_READREG; 311 USETW(req.wValue, 0); 312 USETW(req.wIndex, MOS_MAC); 313 USETW(req.wLength, ETHER_ADDR_LEN); 314 315 err = usbd_do_request(sc->mos_udev, &req, mac); 316 317 if (err) { 318 DPRINTF(("mos_readmac error")); 319 return (-1); 320 } 321 322 return (0); 323 } 324 325 int 326 mos_writemac(struct mos_softc *sc, u_char *mac) 327 { 328 usb_device_request_t req; 329 usbd_status err; 330 331 if (usbd_is_dying(sc->mos_udev)) 332 return(0); 333 334 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 335 req.bRequest = MOS_UR_WRITEREG; 336 USETW(req.wValue, 0); 337 USETW(req.wIndex, MOS_MAC); 338 USETW(req.wLength, ETHER_ADDR_LEN); 339 340 err = usbd_do_request(sc->mos_udev, &req, mac); 341 342 if (err) { 343 DPRINTF(("mos_writemac error")); 344 return (-1); 345 } 346 347 return (0); 348 } 349 350 int 351 mos_write_mcast(struct mos_softc *sc, u_char *hashtbl) 352 { 353 usb_device_request_t req; 354 usbd_status err; 355 356 if (usbd_is_dying(sc->mos_udev)) 357 return(0); 358 359 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 360 req.bRequest = MOS_UR_WRITEREG; 361 USETW(req.wValue, 0); 362 USETW(req.wIndex, MOS_MCAST_TABLE); 363 USETW(req.wLength, 8); 364 365 err = usbd_do_request(sc->mos_udev, &req, hashtbl); 366 367 if (err) { 368 DPRINTF(("mos_reg_mcast error\n")); 369 return(-1); 370 } 371 372 return(0); 373 } 374 375 int 376 mos_miibus_readreg(struct device *dev, int phy, int reg) 377 { 378 struct mos_softc *sc = (void *)dev; 379 int i,res; 380 381 if (usbd_is_dying(sc->mos_udev)) { 382 DPRINTF(("mos: dying\n")); 383 return (0); 384 } 385 386 mos_lock_mii(sc); 387 388 mos_reg_write_2(sc, MOS_PHY_DATA, 0); 389 mos_reg_write_1(sc, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) | 390 MOS_PHYCTL_READ); 391 mos_reg_write_1(sc, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) | 392 MOS_PHYSTS_PENDING); 393 394 for (i = 0; i < MOS_TIMEOUT; i++) { 395 if (mos_reg_read_1(sc, MOS_PHY_STS) & MOS_PHYSTS_READY) 396 break; 397 } 398 if (i == MOS_TIMEOUT) { 399 printf("%s: MII read timeout\n", sc->mos_dev.dv_xname); 400 } 401 402 res = mos_reg_read_2(sc, MOS_PHY_DATA); 403 404 mos_unlock_mii(sc); 405 406 return (res); 407 } 408 409 void 410 mos_miibus_writereg(struct device *dev, int phy, int reg, int val) 411 { 412 struct mos_softc *sc = (void *)dev; 413 int i; 414 415 if (usbd_is_dying(sc->mos_udev)) 416 return; 417 418 mos_lock_mii(sc); 419 420 mos_reg_write_2(sc, MOS_PHY_DATA, val); 421 mos_reg_write_1(sc, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) | 422 MOS_PHYCTL_WRITE); 423 mos_reg_write_1(sc, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) | 424 MOS_PHYSTS_PENDING); 425 426 for (i = 0; i < MOS_TIMEOUT; i++) { 427 if (mos_reg_read_1(sc, MOS_PHY_STS) & MOS_PHYSTS_READY) 428 break; 429 } 430 if (i == MOS_TIMEOUT) { 431 printf("%s: MII write timeout\n", sc->mos_dev.dv_xname); 432 } 433 434 mos_unlock_mii(sc); 435 436 return; 437 } 438 439 void 440 mos_miibus_statchg(struct device *dev) 441 { 442 struct mos_softc *sc = (void *)dev; 443 struct mii_data *mii = GET_MII(sc); 444 int val, err; 445 446 mos_lock_mii(sc); 447 448 /* disable RX, TX prior to changing FDX, SPEEDSEL */ 449 val = mos_reg_read_1(sc, MOS_CTL); 450 val &= ~(MOS_CTL_TX_ENB | MOS_CTL_RX_ENB); 451 mos_reg_write_1(sc, MOS_CTL, val); 452 453 /* reset register which counts dropped frames */ 454 mos_reg_write_1(sc, MOS_FRAME_DROP_CNT, 0); 455 456 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) 457 val |= MOS_CTL_FDX_ENB; 458 else 459 val &= ~(MOS_CTL_FDX_ENB); 460 461 switch (IFM_SUBTYPE(mii->mii_media_active)) { 462 case IFM_100_TX: 463 val |= MOS_CTL_SPEEDSEL; 464 break; 465 case IFM_10_T: 466 val &= ~(MOS_CTL_SPEEDSEL); 467 break; 468 } 469 470 /* re-enable TX, RX */ 471 val |= (MOS_CTL_TX_ENB | MOS_CTL_RX_ENB); 472 err = mos_reg_write_1(sc, MOS_CTL, val); 473 mos_unlock_mii(sc); 474 475 if (err) { 476 printf("%s: media change failed\n", sc->mos_dev.dv_xname); 477 return; 478 } 479 } 480 481 /* 482 * Set media options. 483 */ 484 int 485 mos_ifmedia_upd(struct ifnet *ifp) 486 { 487 struct mos_softc *sc = ifp->if_softc; 488 struct mii_data *mii = GET_MII(sc); 489 490 sc->mos_link = 0; 491 if (mii->mii_instance) { 492 struct mii_softc *miisc; 493 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 494 mii_phy_reset(miisc); 495 } 496 mii_mediachg(mii); 497 498 return (0); 499 } 500 501 /* 502 * Report current media status. 503 */ 504 void 505 mos_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 506 { 507 struct mos_softc *sc = ifp->if_softc; 508 struct mii_data *mii = GET_MII(sc); 509 510 mii_pollstat(mii); 511 ifmr->ifm_active = mii->mii_media_active; 512 ifmr->ifm_status = mii->mii_media_status; 513 } 514 515 void 516 mos_iff(struct mos_softc *sc) 517 { 518 struct ifnet *ifp = GET_IFP(sc); 519 struct arpcom *ac = &sc->arpcom; 520 struct ether_multi *enm; 521 struct ether_multistep step; 522 u_int32_t h = 0; 523 u_int8_t rxmode, hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 524 525 if (usbd_is_dying(sc->mos_udev)) 526 return; 527 528 rxmode = mos_reg_read_1(sc, MOS_CTL); 529 rxmode &= ~(MOS_CTL_ALLMULTI | MOS_CTL_RX_PROMISC); 530 ifp->if_flags &= ~IFF_ALLMULTI; 531 532 if (ifp->if_flags & IFF_PROMISC || ac->ac_multirangecnt > 0) { 533 ifp->if_flags |= IFF_ALLMULTI; 534 rxmode |= MOS_CTL_ALLMULTI; 535 if (ifp->if_flags & IFF_PROMISC) 536 rxmode |= MOS_CTL_RX_PROMISC; 537 } else { 538 /* now program new ones */ 539 ETHER_FIRST_MULTI(step, ac, enm); 540 while (enm != NULL) { 541 h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26; 542 543 hashtbl[h / 8] |= 1 << (h % 8); 544 545 ETHER_NEXT_MULTI(step, enm); 546 } 547 } 548 549 /* 550 * The datasheet claims broadcast frames were always accepted 551 * regardless of filter settings. But the hardware seems to 552 * filter broadcast frames, so pass them explicitly. 553 */ 554 h = ether_crc32_be(etherbroadcastaddr, ETHER_ADDR_LEN) >> 26; 555 hashtbl[h / 8] |= 1 << (h % 8); 556 557 mos_write_mcast(sc, (void *)&hashtbl); 558 mos_reg_write_1(sc, MOS_CTL, rxmode); 559 } 560 561 void 562 mos_reset(struct mos_softc *sc) 563 { 564 u_int8_t ctl; 565 if (usbd_is_dying(sc->mos_udev)) 566 return; 567 568 ctl = mos_reg_read_1(sc, MOS_CTL); 569 ctl &= ~(MOS_CTL_RX_PROMISC | MOS_CTL_ALLMULTI | MOS_CTL_TX_ENB | 570 MOS_CTL_RX_ENB); 571 /* Disable RX, TX, promiscuous and allmulticast mode */ 572 mos_reg_write_1(sc, MOS_CTL, ctl); 573 574 /* Reset frame drop counter register to zero */ 575 mos_reg_write_1(sc, MOS_FRAME_DROP_CNT, 0); 576 577 /* Wait a little while for the chip to get its brains in order. */ 578 DELAY(1000); 579 return; 580 } 581 582 void 583 mos_chip_init(struct mos_softc *sc) 584 { 585 int i; 586 587 /* 588 * Rev.C devices have a pause threshold register which needs to be set 589 * at startup. 590 */ 591 if (mos_reg_read_1(sc, MOS_PAUSE_TRHD) != -1) { 592 for (i=0;i<MOS_PAUSE_REWRITES;i++) 593 mos_reg_write_1(sc, MOS_PAUSE_TRHD, 0); 594 } 595 596 sc->mos_phyaddrs[0] = 1; sc->mos_phyaddrs[1] = 0xFF; 597 } 598 599 /* 600 * Probe for a MCS7x30 chip. 601 */ 602 int 603 mos_match(struct device *parent, void *match, void *aux) 604 { 605 struct usb_attach_arg *uaa = aux; 606 607 if (uaa->iface == NULL || uaa->configno != MOS_CONFIG_NO) 608 return(UMATCH_NONE); 609 610 return (mos_lookup(uaa->vendor, uaa->product) != NULL ? 611 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 612 } 613 614 /* 615 * Attach the interface. Allocate softc structures, do ifmedia 616 * setup and ethernet/BPF attach. 617 */ 618 void 619 mos_attach(struct device *parent, struct device *self, void *aux) 620 { 621 struct mos_softc *sc = (struct mos_softc *)self; 622 struct usb_attach_arg *uaa = aux; 623 struct ifnet *ifp; 624 struct usbd_device *dev = uaa->device; 625 usbd_status err; 626 usb_interface_descriptor_t *id; 627 usb_endpoint_descriptor_t *ed; 628 struct mii_data *mii; 629 u_char eaddr[ETHER_ADDR_LEN]; 630 int i,s; 631 632 sc->mos_udev = dev; 633 sc->mos_unit = self->dv_unit; 634 635 usb_init_task(&sc->mos_tick_task, mos_tick_task, sc, 636 USB_TASK_TYPE_GENERIC); 637 rw_init(&sc->mos_mii_lock, "mosmii"); 638 usb_init_task(&sc->mos_stop_task, (void (*)(void *))mos_stop, sc, 639 USB_TASK_TYPE_GENERIC); 640 641 err = usbd_device2interface_handle(dev, MOS_IFACE_IDX, &sc->mos_iface); 642 if (err) { 643 printf("%s: getting interface handle failed\n", 644 sc->mos_dev.dv_xname); 645 return; 646 } 647 648 sc->mos_flags = mos_lookup(uaa->vendor, uaa->product)->mos_flags; 649 650 id = usbd_get_interface_descriptor(sc->mos_iface); 651 652 sc->mos_bufsz = MOS_BUFSZ; 653 654 /* Find endpoints. */ 655 for (i = 0; i < id->bNumEndpoints; i++) { 656 ed = usbd_interface2endpoint_descriptor(sc->mos_iface, i); 657 if (!ed) { 658 printf("%s: couldn't get ep %d\n", 659 sc->mos_dev.dv_xname, i); 660 return; 661 } 662 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 663 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 664 sc->mos_ed[MOS_ENDPT_RX] = ed->bEndpointAddress; 665 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 666 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 667 sc->mos_ed[MOS_ENDPT_TX] = ed->bEndpointAddress; 668 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 669 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 670 sc->mos_ed[MOS_ENDPT_INTR] = ed->bEndpointAddress; 671 } 672 } 673 674 s = splnet(); 675 676 printf("%s:", sc->mos_dev.dv_xname); 677 678 if (sc->mos_flags & MCS7730) 679 printf(" MCS7730"); 680 else if (sc->mos_flags & MCS7830) 681 printf(" MCS7830"); 682 else if (sc->mos_flags & MCS7832) 683 printf(" MCS7832"); 684 685 mos_chip_init(sc); 686 687 /* 688 * Read MAC address, inform the world. 689 */ 690 err = mos_readmac(sc, (void*)&eaddr); 691 if (err) { 692 printf("%s: couldn't get MAC address\n", 693 sc->mos_dev.dv_xname); 694 splx(s); 695 return; 696 } 697 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 698 printf(", address %s\n", ether_sprintf(eaddr)); 699 700 /* Initialize interface info.*/ 701 ifp = GET_IFP(sc); 702 ifp->if_softc = sc; 703 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 704 ifp->if_ioctl = mos_ioctl; 705 ifp->if_start = mos_start; 706 ifp->if_watchdog = mos_watchdog; 707 strlcpy(ifp->if_xname, sc->mos_dev.dv_xname, IFNAMSIZ); 708 709 ifp->if_capabilities = IFCAP_VLAN_MTU; 710 711 /* Initialize MII/media info. */ 712 mii = GET_MII(sc); 713 mii->mii_ifp = ifp; 714 mii->mii_readreg = mos_miibus_readreg; 715 mii->mii_writereg = mos_miibus_writereg; 716 mii->mii_statchg = mos_miibus_statchg; 717 mii->mii_flags = MIIF_AUTOTSLEEP; 718 719 ifmedia_init(&mii->mii_media, 0, mos_ifmedia_upd, mos_ifmedia_sts); 720 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); 721 722 if (LIST_FIRST(&mii->mii_phys) == NULL) { 723 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 724 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 725 } else 726 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 727 728 /* Attach the interface. */ 729 if_attach(ifp); 730 ether_ifattach(ifp); 731 732 timeout_set(&sc->mos_stat_ch, mos_tick, sc); 733 734 splx(s); 735 } 736 737 int 738 mos_detach(struct device *self, int flags) 739 { 740 struct mos_softc *sc = (struct mos_softc *)self; 741 struct ifnet *ifp = GET_IFP(sc); 742 int s; 743 744 DPRINTFN(2,("%s: %s: enter\n", sc->mos_dev.dv_xname, __func__)); 745 746 if (timeout_initialized(&sc->mos_stat_ch)) 747 timeout_del(&sc->mos_stat_ch); 748 749 if (sc->mos_ep[MOS_ENDPT_TX] != NULL) 750 usbd_abort_pipe(sc->mos_ep[MOS_ENDPT_TX]); 751 if (sc->mos_ep[MOS_ENDPT_RX] != NULL) 752 usbd_abort_pipe(sc->mos_ep[MOS_ENDPT_RX]); 753 if (sc->mos_ep[MOS_ENDPT_INTR] != NULL) 754 usbd_abort_pipe(sc->mos_ep[MOS_ENDPT_INTR]); 755 756 /* 757 * Remove any pending tasks. They cannot be executing because they run 758 * in the same thread as detach. 759 */ 760 usb_rem_task(sc->mos_udev, &sc->mos_tick_task); 761 usb_rem_task(sc->mos_udev, &sc->mos_stop_task); 762 s = splusb(); 763 764 if (--sc->mos_refcnt >= 0) { 765 /* Wait for processes to go away */ 766 usb_detach_wait(&sc->mos_dev); 767 } 768 769 if (ifp->if_flags & IFF_RUNNING) 770 mos_stop(sc); 771 772 mii_detach(&sc->mos_mii, MII_PHY_ANY, MII_OFFSET_ANY); 773 ifmedia_delete_instance(&sc->mos_mii.mii_media, IFM_INST_ANY); 774 if (ifp->if_softc != NULL) { 775 ether_ifdetach(ifp); 776 if_detach(ifp); 777 } 778 779 #ifdef DIAGNOSTIC 780 if (sc->mos_ep[MOS_ENDPT_TX] != NULL || 781 sc->mos_ep[MOS_ENDPT_RX] != NULL || 782 sc->mos_ep[MOS_ENDPT_INTR] != NULL) 783 printf("%s: detach has active endpoints\n", 784 sc->mos_dev.dv_xname); 785 #endif 786 787 splx(s); 788 789 return (0); 790 } 791 792 struct mbuf * 793 mos_newbuf(void) 794 { 795 struct mbuf *m; 796 797 MGETHDR(m, M_DONTWAIT, MT_DATA); 798 if (m == NULL) 799 return (NULL); 800 801 MCLGET(m, M_DONTWAIT); 802 if (!(m->m_flags & M_EXT)) { 803 m_freem(m); 804 return (NULL); 805 } 806 807 m->m_len = m->m_pkthdr.len = MCLBYTES; 808 m_adj(m, ETHER_ALIGN); 809 810 return (m); 811 } 812 813 int 814 mos_rx_list_init(struct mos_softc *sc) 815 { 816 struct mos_cdata *cd; 817 struct mos_chain *c; 818 int i; 819 820 DPRINTF(("%s: %s: enter\n", sc->mos_dev.dv_xname, __func__)); 821 822 cd = &sc->mos_cdata; 823 for (i = 0; i < MOS_RX_LIST_CNT; i++) { 824 c = &cd->mos_rx_chain[i]; 825 c->mos_sc = sc; 826 c->mos_idx = i; 827 c->mos_mbuf = NULL; 828 if (c->mos_xfer == NULL) { 829 c->mos_xfer = usbd_alloc_xfer(sc->mos_udev); 830 if (c->mos_xfer == NULL) 831 return (ENOBUFS); 832 c->mos_buf = usbd_alloc_buffer(c->mos_xfer, 833 sc->mos_bufsz); 834 if (c->mos_buf == NULL) { 835 usbd_free_xfer(c->mos_xfer); 836 return (ENOBUFS); 837 } 838 } 839 } 840 841 return (0); 842 } 843 844 int 845 mos_tx_list_init(struct mos_softc *sc) 846 { 847 struct mos_cdata *cd; 848 struct mos_chain *c; 849 int i; 850 851 DPRINTF(("%s: %s: enter\n", sc->mos_dev.dv_xname, __func__)); 852 853 cd = &sc->mos_cdata; 854 for (i = 0; i < MOS_TX_LIST_CNT; i++) { 855 c = &cd->mos_tx_chain[i]; 856 c->mos_sc = sc; 857 c->mos_idx = i; 858 c->mos_mbuf = NULL; 859 if (c->mos_xfer == NULL) { 860 c->mos_xfer = usbd_alloc_xfer(sc->mos_udev); 861 if (c->mos_xfer == NULL) 862 return (ENOBUFS); 863 c->mos_buf = usbd_alloc_buffer(c->mos_xfer, 864 sc->mos_bufsz); 865 if (c->mos_buf == NULL) { 866 usbd_free_xfer(c->mos_xfer); 867 return (ENOBUFS); 868 } 869 } 870 } 871 872 return (0); 873 } 874 875 /* 876 * A frame has been uploaded: pass the resulting mbuf chain up to 877 * the higher level protocols. 878 */ 879 void 880 mos_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 881 { 882 struct mos_chain *c = (struct mos_chain *)priv; 883 struct mos_softc *sc = c->mos_sc; 884 struct ifnet *ifp = GET_IFP(sc); 885 u_char *buf = c->mos_buf; 886 u_int8_t rxstat; 887 u_int32_t total_len; 888 u_int16_t pktlen = 0; 889 struct mbuf_list ml = MBUF_LIST_INITIALIZER(); 890 struct mbuf *m; 891 int s; 892 893 DPRINTFN(10,("%s: %s: enter\n", sc->mos_dev.dv_xname,__func__)); 894 895 if (usbd_is_dying(sc->mos_udev)) 896 return; 897 898 if (!(ifp->if_flags & IFF_RUNNING)) 899 return; 900 901 if (status != USBD_NORMAL_COMPLETION) { 902 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 903 return; 904 if (usbd_ratecheck(&sc->mos_rx_notice)) { 905 printf("%s: usb errors on rx: %s\n", 906 sc->mos_dev.dv_xname, usbd_errstr(status)); 907 } 908 if (status == USBD_STALLED) 909 usbd_clear_endpoint_stall_async(sc->mos_ep[MOS_ENDPT_RX]); 910 goto done; 911 } 912 913 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 914 915 if (total_len <= 1) 916 goto done; 917 918 /* evaluate status byte at the end */ 919 pktlen = total_len - 1; 920 rxstat = buf[pktlen] & MOS_RXSTS_MASK; 921 922 if (rxstat != MOS_RXSTS_VALID) { 923 DPRINTF(("%s: erroneous frame received: ", 924 sc->mos_dev.dv_xname)); 925 if (rxstat & MOS_RXSTS_SHORT_FRAME) 926 DPRINTF(("frame size less than 64 bytes\n")); 927 if (rxstat & MOS_RXSTS_LARGE_FRAME) 928 DPRINTF(("frame size larger than 1532 bytes\n")); 929 if (rxstat & MOS_RXSTS_CRC_ERROR) 930 DPRINTF(("CRC error\n")); 931 if (rxstat & MOS_RXSTS_ALIGN_ERROR) 932 DPRINTF(("alignment error\n")); 933 ifp->if_ierrors++; 934 goto done; 935 } 936 937 if ( pktlen < sizeof(struct ether_header) ) { 938 ifp->if_ierrors++; 939 goto done; 940 } 941 942 m = mos_newbuf(); 943 if (m == NULL) { 944 ifp->if_ierrors++; 945 goto done; 946 } 947 948 m->m_pkthdr.len = m->m_len = pktlen; 949 950 memcpy(mtod(m, char *), buf, pktlen); 951 952 ml_enqueue(&ml, m); 953 954 s = splnet(); 955 if_input(ifp, &ml); 956 splx(s); 957 958 done: 959 memset(c->mos_buf, 0, sc->mos_bufsz); 960 961 /* Setup new transfer. */ 962 usbd_setup_xfer(xfer, sc->mos_ep[MOS_ENDPT_RX], 963 c, c->mos_buf, sc->mos_bufsz, 964 USBD_SHORT_XFER_OK | USBD_NO_COPY, 965 USBD_NO_TIMEOUT, mos_rxeof); 966 usbd_transfer(xfer); 967 968 DPRINTFN(10,("%s: %s: start rx\n", sc->mos_dev.dv_xname, __func__)); 969 970 return; 971 } 972 973 /* 974 * A frame was downloaded to the chip. It's safe for us to clean up 975 * the list buffers. 976 */ 977 978 void 979 mos_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 980 { 981 struct mos_softc *sc; 982 struct mos_chain *c; 983 struct ifnet *ifp; 984 int s; 985 986 c = priv; 987 sc = c->mos_sc; 988 ifp = &sc->arpcom.ac_if; 989 990 if (usbd_is_dying(sc->mos_udev)) 991 return; 992 993 s = splnet(); 994 995 if (status != USBD_NORMAL_COMPLETION) { 996 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 997 splx(s); 998 return; 999 } 1000 ifp->if_oerrors++; 1001 printf("%s: usb error on tx: %s\n", sc->mos_dev.dv_xname, 1002 usbd_errstr(status)); 1003 if (status == USBD_STALLED) 1004 usbd_clear_endpoint_stall_async(sc->mos_ep[MOS_ENDPT_TX]); 1005 splx(s); 1006 return; 1007 } 1008 1009 ifp->if_timer = 0; 1010 ifq_clr_oactive(&ifp->if_snd); 1011 1012 m_freem(c->mos_mbuf); 1013 c->mos_mbuf = NULL; 1014 1015 if (ifq_empty(&ifp->if_snd) == 0) 1016 mos_start(ifp); 1017 1018 splx(s); 1019 return; 1020 } 1021 1022 void 1023 mos_tick(void *xsc) 1024 { 1025 struct mos_softc *sc = xsc; 1026 1027 if (sc == NULL) 1028 return; 1029 1030 DPRINTFN(0xff, ("%s: %s: enter\n", sc->mos_dev.dv_xname, 1031 __func__)); 1032 1033 if (usbd_is_dying(sc->mos_udev)) 1034 return; 1035 1036 /* Perform periodic stuff in process context */ 1037 usb_add_task(sc->mos_udev, &sc->mos_tick_task); 1038 1039 } 1040 1041 void 1042 mos_tick_task(void *xsc) 1043 { 1044 int s; 1045 struct mos_softc *sc; 1046 struct ifnet *ifp; 1047 struct mii_data *mii; 1048 1049 sc = xsc; 1050 1051 if (sc == NULL) 1052 return; 1053 1054 if (usbd_is_dying(sc->mos_udev)) 1055 return; 1056 1057 ifp = GET_IFP(sc); 1058 mii = GET_MII(sc); 1059 if (mii == NULL) 1060 return; 1061 1062 s = splnet(); 1063 1064 mii_tick(mii); 1065 if (!sc->mos_link && mii->mii_media_status & IFM_ACTIVE && 1066 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1067 DPRINTF(("%s: %s: got link\n", 1068 sc->mos_dev.dv_xname, __func__)); 1069 sc->mos_link++; 1070 if (ifq_empty(&ifp->if_snd) == 0) 1071 mos_start(ifp); 1072 } 1073 1074 timeout_add_sec(&sc->mos_stat_ch, 1); 1075 1076 splx(s); 1077 } 1078 1079 int 1080 mos_encap(struct mos_softc *sc, struct mbuf *m, int idx) 1081 { 1082 struct mos_chain *c; 1083 usbd_status err; 1084 int length; 1085 1086 c = &sc->mos_cdata.mos_tx_chain[idx]; 1087 1088 m_copydata(m, 0, m->m_pkthdr.len, c->mos_buf); 1089 length = m->m_pkthdr.len; 1090 1091 c->mos_mbuf = m; 1092 1093 usbd_setup_xfer(c->mos_xfer, sc->mos_ep[MOS_ENDPT_TX], 1094 c, c->mos_buf, length, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, 1095 10000, mos_txeof); 1096 1097 /* Transmit */ 1098 err = usbd_transfer(c->mos_xfer); 1099 if (err != USBD_IN_PROGRESS) { 1100 c->mos_mbuf = NULL; 1101 mos_stop(sc); 1102 return(EIO); 1103 } 1104 1105 sc->mos_cdata.mos_tx_cnt++; 1106 1107 return(0); 1108 } 1109 1110 void 1111 mos_start(struct ifnet *ifp) 1112 { 1113 struct mos_softc *sc; 1114 struct mbuf *m_head = NULL; 1115 1116 sc = ifp->if_softc; 1117 1118 if (!sc->mos_link) 1119 return; 1120 1121 if (ifq_is_oactive(&ifp->if_snd)) 1122 return; 1123 1124 m_head = ifq_dequeue(&ifp->if_snd); 1125 if (m_head == NULL) 1126 return; 1127 1128 if (mos_encap(sc, m_head, 0)) { 1129 m_freem(m_head); 1130 ifq_set_oactive(&ifp->if_snd); 1131 return; 1132 } 1133 1134 /* 1135 * If there's a BPF listener, bounce a copy of this frame 1136 * to him. 1137 */ 1138 #if NBPFILTER > 0 1139 if (ifp->if_bpf) 1140 bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT); 1141 #endif 1142 1143 ifq_set_oactive(&ifp->if_snd); 1144 1145 /* 1146 * Set a timeout in case the chip goes out to lunch. 1147 */ 1148 ifp->if_timer = 5; 1149 1150 return; 1151 } 1152 1153 void 1154 mos_init(void *xsc) 1155 { 1156 struct mos_softc *sc = xsc; 1157 struct ifnet *ifp = &sc->arpcom.ac_if; 1158 struct mos_chain *c; 1159 usbd_status err; 1160 u_int8_t rxmode; 1161 int i, s; 1162 1163 s = splnet(); 1164 1165 /* 1166 * Cancel pending I/O and free all RX/TX buffers. 1167 */ 1168 mos_reset(sc); 1169 1170 /* 1171 * Write MAC address 1172 */ 1173 mos_writemac(sc, sc->arpcom.ac_enaddr); 1174 1175 /* Init RX ring. */ 1176 if (mos_rx_list_init(sc) == ENOBUFS) { 1177 printf("%s: rx list init failed\n", sc->mos_dev.dv_xname); 1178 splx(s); 1179 return; 1180 } 1181 1182 /* Init TX ring. */ 1183 if (mos_tx_list_init(sc) == ENOBUFS) { 1184 printf("%s: tx list init failed\n", sc->mos_dev.dv_xname); 1185 splx(s); 1186 return; 1187 } 1188 1189 /* Read and set transmitter IPG values */ 1190 sc->mos_ipgs[0] = mos_reg_read_1(sc, MOS_IPG0); 1191 sc->mos_ipgs[1] = mos_reg_read_1(sc, MOS_IPG1); 1192 mos_reg_write_1(sc, MOS_IPG0, sc->mos_ipgs[0]); 1193 mos_reg_write_1(sc, MOS_IPG1, sc->mos_ipgs[1]); 1194 1195 /* Program promiscuous mode and multicast filters. */ 1196 mos_iff(sc); 1197 1198 /* Enable receiver and transmitter, bridge controls speed/duplex mode */ 1199 rxmode = mos_reg_read_1(sc, MOS_CTL); 1200 rxmode |= MOS_CTL_RX_ENB | MOS_CTL_TX_ENB | MOS_CTL_BS_ENB; 1201 rxmode &= ~(MOS_CTL_SLEEP); 1202 mos_reg_write_1(sc, MOS_CTL, rxmode); 1203 1204 mii_mediachg(GET_MII(sc)); 1205 1206 /* Open RX and TX pipes. */ 1207 err = usbd_open_pipe(sc->mos_iface, sc->mos_ed[MOS_ENDPT_RX], 1208 USBD_EXCLUSIVE_USE, &sc->mos_ep[MOS_ENDPT_RX]); 1209 if (err) { 1210 printf("%s: open rx pipe failed: %s\n", 1211 sc->mos_dev.dv_xname, usbd_errstr(err)); 1212 splx(s); 1213 return; 1214 } 1215 1216 err = usbd_open_pipe(sc->mos_iface, sc->mos_ed[MOS_ENDPT_TX], 1217 USBD_EXCLUSIVE_USE, &sc->mos_ep[MOS_ENDPT_TX]); 1218 if (err) { 1219 printf("%s: open tx pipe failed: %s\n", 1220 sc->mos_dev.dv_xname, usbd_errstr(err)); 1221 splx(s); 1222 return; 1223 } 1224 1225 /* Start up the receive pipe. */ 1226 for (i = 0; i < MOS_RX_LIST_CNT; i++) { 1227 c = &sc->mos_cdata.mos_rx_chain[i]; 1228 usbd_setup_xfer(c->mos_xfer, sc->mos_ep[MOS_ENDPT_RX], 1229 c, c->mos_buf, sc->mos_bufsz, 1230 USBD_SHORT_XFER_OK | USBD_NO_COPY, 1231 USBD_NO_TIMEOUT, mos_rxeof); 1232 usbd_transfer(c->mos_xfer); 1233 } 1234 1235 ifp->if_flags |= IFF_RUNNING; 1236 ifq_clr_oactive(&ifp->if_snd); 1237 1238 splx(s); 1239 1240 timeout_add_sec(&sc->mos_stat_ch, 1); 1241 return; 1242 } 1243 1244 int 1245 mos_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1246 { 1247 struct mos_softc *sc = ifp->if_softc; 1248 struct ifreq *ifr = (struct ifreq *)data; 1249 int s, error = 0; 1250 1251 s = splnet(); 1252 1253 switch(cmd) { 1254 case SIOCSIFADDR: 1255 ifp->if_flags |= IFF_UP; 1256 if (!(ifp->if_flags & IFF_RUNNING)) 1257 mos_init(sc); 1258 break; 1259 1260 case SIOCSIFFLAGS: 1261 if (ifp->if_flags & IFF_UP) { 1262 if (ifp->if_flags & IFF_RUNNING) 1263 error = ENETRESET; 1264 else 1265 mos_init(sc); 1266 } else { 1267 if (ifp->if_flags & IFF_RUNNING) 1268 mos_stop(sc); 1269 } 1270 break; 1271 1272 case SIOCGIFMEDIA: 1273 case SIOCSIFMEDIA: 1274 error = ifmedia_ioctl(ifp, ifr, &sc->mos_mii.mii_media, cmd); 1275 break; 1276 1277 default: 1278 error = ether_ioctl(ifp, &sc->arpcom, cmd, data); 1279 } 1280 1281 if (error == ENETRESET) { 1282 if (ifp->if_flags & IFF_RUNNING) 1283 mos_iff(sc); 1284 error = 0; 1285 } 1286 1287 splx(s); 1288 return(error); 1289 } 1290 1291 void 1292 mos_watchdog(struct ifnet *ifp) 1293 { 1294 struct mos_softc *sc; 1295 struct mos_chain *c; 1296 usbd_status stat; 1297 int s; 1298 1299 sc = ifp->if_softc; 1300 1301 ifp->if_oerrors++; 1302 printf("%s: watchdog timeout\n", sc->mos_dev.dv_xname); 1303 1304 s = splusb(); 1305 c = &sc->mos_cdata.mos_tx_chain[0]; 1306 usbd_get_xfer_status(c->mos_xfer, NULL, NULL, NULL, &stat); 1307 mos_txeof(c->mos_xfer, c, stat); 1308 1309 if (!ifq_empty(&ifp->if_snd)) 1310 mos_start(ifp); 1311 splx(s); 1312 } 1313 1314 1315 /* 1316 * Stop the adapter and free any mbufs allocated to the 1317 * RX and TX lists. 1318 */ 1319 void 1320 mos_stop(struct mos_softc *sc) 1321 { 1322 usbd_status err; 1323 struct ifnet *ifp; 1324 int i; 1325 1326 mos_reset(sc); 1327 1328 ifp = &sc->arpcom.ac_if; 1329 ifp->if_timer = 0; 1330 ifp->if_flags &= ~IFF_RUNNING; 1331 ifq_clr_oactive(&ifp->if_snd); 1332 1333 timeout_del(&sc->mos_stat_ch); 1334 1335 /* Stop transfers. */ 1336 if (sc->mos_ep[MOS_ENDPT_RX] != NULL) { 1337 err = usbd_close_pipe(sc->mos_ep[MOS_ENDPT_RX]); 1338 if (err) { 1339 printf("%s: close rx pipe failed: %s\n", 1340 sc->mos_dev.dv_xname, usbd_errstr(err)); 1341 } 1342 sc->mos_ep[MOS_ENDPT_RX] = NULL; 1343 } 1344 1345 if (sc->mos_ep[MOS_ENDPT_TX] != NULL) { 1346 err = usbd_close_pipe(sc->mos_ep[MOS_ENDPT_TX]); 1347 if (err) { 1348 printf("%s: close tx pipe failed: %s\n", 1349 sc->mos_dev.dv_xname, usbd_errstr(err)); 1350 } 1351 sc->mos_ep[MOS_ENDPT_TX] = NULL; 1352 } 1353 1354 if (sc->mos_ep[MOS_ENDPT_INTR] != NULL) { 1355 err = usbd_close_pipe(sc->mos_ep[MOS_ENDPT_INTR]); 1356 if (err) { 1357 printf("%s: close intr pipe failed: %s\n", 1358 sc->mos_dev.dv_xname, usbd_errstr(err)); 1359 } 1360 sc->mos_ep[MOS_ENDPT_INTR] = NULL; 1361 } 1362 1363 /* Free RX resources. */ 1364 for (i = 0; i < MOS_RX_LIST_CNT; i++) { 1365 if (sc->mos_cdata.mos_rx_chain[i].mos_mbuf != NULL) { 1366 m_freem(sc->mos_cdata.mos_rx_chain[i].mos_mbuf); 1367 sc->mos_cdata.mos_rx_chain[i].mos_mbuf = NULL; 1368 } 1369 if (sc->mos_cdata.mos_rx_chain[i].mos_xfer != NULL) { 1370 usbd_free_xfer(sc->mos_cdata.mos_rx_chain[i].mos_xfer); 1371 sc->mos_cdata.mos_rx_chain[i].mos_xfer = NULL; 1372 } 1373 } 1374 1375 /* Free TX resources. */ 1376 for (i = 0; i < MOS_TX_LIST_CNT; i++) { 1377 if (sc->mos_cdata.mos_tx_chain[i].mos_mbuf != NULL) { 1378 m_freem(sc->mos_cdata.mos_tx_chain[i].mos_mbuf); 1379 sc->mos_cdata.mos_tx_chain[i].mos_mbuf = NULL; 1380 } 1381 if (sc->mos_cdata.mos_tx_chain[i].mos_xfer != NULL) { 1382 usbd_free_xfer(sc->mos_cdata.mos_tx_chain[i].mos_xfer); 1383 sc->mos_cdata.mos_tx_chain[i].mos_xfer = NULL; 1384 } 1385 } 1386 1387 sc->mos_link = 0; 1388 } 1389 1390