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