if_udav.c revision 187259
1/* $NetBSD: if_udav.c,v 1.2 2003/09/04 15:17:38 tsutsui Exp $ */ 2/* $nabe: if_udav.c,v 1.3 2003/08/21 16:57:19 nabe Exp $ */ 3/* $FreeBSD: head/sys/dev/usb2/ethernet/if_udav2.c 187259 2009-01-15 02:35:40Z thompsa $ */ 4/*- 5 * Copyright (c) 2003 6 * Shingo WATANABE <nabe@nabechan.org>. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 */ 33 34/* 35 * DM9601(DAVICOM USB to Ethernet MAC Controller with Integrated 10/100 PHY) 36 * The spec can be found at the following url. 37 * http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-P01-930914.pdf 38 */ 39 40/* 41 * NOTE: all function names beginning like "udav_cfg_" can only 42 * be called from within the config thread function ! 43 */ 44 45/* 46 * TODO: 47 * Interrupt Endpoint support 48 * External PHYs 49 */ 50 51#include <sys/cdefs.h> 52__FBSDID("$FreeBSD: head/sys/dev/usb2/ethernet/if_udav2.c 187259 2009-01-15 02:35:40Z thompsa $"); 53 54#include <dev/usb2/include/usb2_devid.h> 55#include <dev/usb2/include/usb2_standard.h> 56#include <dev/usb2/include/usb2_mfunc.h> 57#include <dev/usb2/include/usb2_error.h> 58 59#define usb2_config_td_cc usb2_ether_cc 60#define usb2_config_td_softc udav_softc 61 62#define USB_DEBUG_VAR udav_debug 63 64#include <dev/usb2/core/usb2_core.h> 65#include <dev/usb2/core/usb2_lookup.h> 66#include <dev/usb2/core/usb2_process.h> 67#include <dev/usb2/core/usb2_config_td.h> 68#include <dev/usb2/core/usb2_debug.h> 69#include <dev/usb2/core/usb2_request.h> 70#include <dev/usb2/core/usb2_busdma.h> 71#include <dev/usb2/core/usb2_util.h> 72 73#include <dev/usb2/ethernet/usb2_ethernet.h> 74#include <dev/usb2/ethernet/if_udavreg.h> 75 76/* prototypes */ 77 78static device_probe_t udav_probe; 79static device_attach_t udav_attach; 80static device_detach_t udav_detach; 81static device_shutdown_t udav_shutdown; 82 83static usb2_callback_t udav_bulk_write_clear_stall_callback; 84static usb2_callback_t udav_bulk_write_callback; 85static usb2_callback_t udav_bulk_read_clear_stall_callback; 86static usb2_callback_t udav_bulk_read_callback; 87static usb2_callback_t udav_intr_clear_stall_callback; 88static usb2_callback_t udav_intr_callback; 89 90static usb2_config_td_command_t udav_cfg_first_time_setup; 91static usb2_config_td_command_t udav_cfg_pre_init; 92static usb2_config_td_command_t udav_cfg_init; 93static usb2_config_td_command_t udav_config_copy; 94static usb2_config_td_command_t udav_cfg_promisc_upd; 95static usb2_config_td_command_t udav_cfg_pre_stop; 96static usb2_config_td_command_t udav_cfg_stop; 97static usb2_config_td_command_t udav_cfg_ifmedia_change; 98static usb2_config_td_command_t udav_cfg_tick; 99 100static void udav_cfg_do_request(struct udav_softc *, 101 struct usb2_device_request *, void *); 102static void udav_cfg_csr_read(struct udav_softc *, uint16_t, void *, 103 uint16_t); 104static void udav_cfg_csr_write(struct udav_softc *, uint16_t, void *, 105 uint16_t); 106static uint8_t udav_cfg_csr_read1(struct udav_softc *, uint16_t); 107static void udav_cfg_csr_write1(struct udav_softc *, uint16_t, uint8_t); 108static void udav_init_cb(void *); 109static void udav_cfg_reset(struct udav_softc *); 110static void udav_start_cb(struct ifnet *); 111static void udav_start_transfers(struct udav_softc *); 112static int udav_ioctl_cb(struct ifnet *, u_long, caddr_t); 113static void udav_watchdog(void *); 114static int udav_ifmedia_change_cb(struct ifnet *); 115static void udav_ifmedia_status_cb(struct ifnet *, struct ifmediareq *); 116 117static miibus_readreg_t udav_cfg_miibus_readreg; 118static miibus_writereg_t udav_cfg_miibus_writereg; 119static miibus_statchg_t udav_cfg_miibus_statchg; 120 121static const struct usb2_config udav_config[UDAV_N_TRANSFER] = { 122 123 [UDAV_BULK_DT_WR] = { 124 .type = UE_BULK, 125 .endpoint = UE_ADDR_ANY, 126 .direction = UE_DIR_OUT, 127 .mh.bufsize = (MCLBYTES + 2), 128 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 129 .mh.callback = &udav_bulk_write_callback, 130 .mh.timeout = 10000, /* 10 seconds */ 131 }, 132 133 [UDAV_BULK_DT_RD] = { 134 .type = UE_BULK, 135 .endpoint = UE_ADDR_ANY, 136 .direction = UE_DIR_IN, 137 .mh.bufsize = (MCLBYTES + 3), 138 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 139 .mh.callback = &udav_bulk_read_callback, 140 .mh.timeout = 0, /* no timeout */ 141 }, 142 143 [UDAV_BULK_CS_WR] = { 144 .type = UE_CONTROL, 145 .endpoint = 0x00, /* Control pipe */ 146 .direction = UE_DIR_ANY, 147 .mh.bufsize = sizeof(struct usb2_device_request), 148 .mh.flags = {}, 149 .mh.callback = &udav_bulk_write_clear_stall_callback, 150 .mh.timeout = 1000, /* 1 second */ 151 .mh.interval = 50, /* 50ms */ 152 }, 153 154 [UDAV_BULK_CS_RD] = { 155 .type = UE_CONTROL, 156 .endpoint = 0x00, /* Control pipe */ 157 .direction = UE_DIR_ANY, 158 .mh.bufsize = sizeof(struct usb2_device_request), 159 .mh.flags = {}, 160 .mh.callback = &udav_bulk_read_clear_stall_callback, 161 .mh.timeout = 1000, /* 1 second */ 162 .mh.interval = 50, /* 50ms */ 163 }, 164 165 [UDAV_INTR_DT_RD] = { 166 .type = UE_INTERRUPT, 167 .endpoint = UE_ADDR_ANY, 168 .direction = UE_DIR_IN, 169 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 170 .mh.bufsize = 0, /* use wMaxPacketSize */ 171 .mh.callback = &udav_intr_callback, 172 }, 173 174 [UDAV_INTR_CS_RD] = { 175 .type = UE_CONTROL, 176 .endpoint = 0x00, /* Control pipe */ 177 .direction = UE_DIR_ANY, 178 .mh.bufsize = sizeof(struct usb2_device_request), 179 .mh.flags = {}, 180 .mh.callback = &udav_intr_clear_stall_callback, 181 .mh.timeout = 1000, /* 1 second */ 182 .mh.interval = 50, /* 50ms */ 183 }, 184}; 185 186static device_method_t udav_methods[] = { 187 /* Device interface */ 188 DEVMETHOD(device_probe, udav_probe), 189 DEVMETHOD(device_attach, udav_attach), 190 DEVMETHOD(device_detach, udav_detach), 191 DEVMETHOD(device_shutdown, udav_shutdown), 192 193 /* bus interface */ 194 DEVMETHOD(bus_print_child, bus_generic_print_child), 195 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 196 197 /* MII interface */ 198 DEVMETHOD(miibus_readreg, udav_cfg_miibus_readreg), 199 DEVMETHOD(miibus_writereg, udav_cfg_miibus_writereg), 200 DEVMETHOD(miibus_statchg, udav_cfg_miibus_statchg), 201 202 {0, 0} 203}; 204 205static driver_t udav_driver = { 206 .name = "udav", 207 .methods = udav_methods, 208 .size = sizeof(struct udav_softc), 209}; 210 211static devclass_t udav_devclass; 212 213DRIVER_MODULE(udav, ushub, udav_driver, udav_devclass, NULL, 0); 214DRIVER_MODULE(miibus, udav, miibus_driver, miibus_devclass, 0, 0); 215MODULE_DEPEND(udav, usb2_ethernet, 1, 1, 1); 216MODULE_DEPEND(udav, usb2_core, 1, 1, 1); 217MODULE_DEPEND(udav, ether, 1, 1, 1); 218MODULE_DEPEND(udav, miibus, 1, 1, 1); 219 220#if USB_DEBUG 221static int udav_debug = 0; 222 223SYSCTL_NODE(_hw_usb2, OID_AUTO, udav, CTLFLAG_RW, 0, "USB udav"); 224SYSCTL_INT(_hw_usb2_udav, OID_AUTO, debug, CTLFLAG_RW, &udav_debug, 0, 225 "Debug level"); 226#endif 227 228#define UDAV_CFG_SETBIT(sc, reg, x) \ 229 udav_cfg_csr_write1(sc, reg, udav_cfg_csr_read1(sc, reg) | (x)) 230 231#define UDAV_CFG_CLRBIT(sc, reg, x) \ 232 udav_cfg_csr_write1(sc, reg, udav_cfg_csr_read1(sc, reg) & ~(x)) 233 234static const struct usb2_device_id udav_devs[] = { 235 /* ShanTou DM9601 USB NIC */ 236 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_DM9601, 0)}, 237 238 /* ShanTou ST268 USB NIC */ 239 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268, 0)}, 240 241 /* Corega USB-TXC */ 242 {USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC, 0)}, 243}; 244 245static int 246udav_probe(device_t dev) 247{ 248 struct usb2_attach_arg *uaa = device_get_ivars(dev); 249 250 if (uaa->usb2_mode != USB_MODE_HOST) { 251 return (ENXIO); 252 } 253 if (uaa->info.bConfigIndex != UDAV_CONFIG_INDEX) { 254 return (ENXIO); 255 } 256 if (uaa->info.bIfaceIndex != UDAV_IFACE_INDEX) { 257 return (ENXIO); 258 } 259 return (usb2_lookup_id_by_uaa(udav_devs, sizeof(udav_devs), uaa)); 260} 261 262static int 263udav_attach(device_t dev) 264{ 265 struct usb2_attach_arg *uaa = device_get_ivars(dev); 266 struct udav_softc *sc = device_get_softc(dev); 267 int32_t error; 268 uint8_t iface_index; 269 270 if (sc == NULL) { 271 return (ENOMEM); 272 } 273 sc->sc_udev = uaa->device; 274 sc->sc_dev = dev; 275 sc->sc_unit = device_get_unit(dev); 276 sc->sc_flags = USB_GET_DRIVER_INFO(uaa); 277 278 device_set_usb2_desc(dev); 279 280 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s", 281 device_get_nameunit(dev)); 282 283 mtx_init(&sc->sc_mtx, "udav lock", NULL, MTX_DEF | MTX_RECURSE); 284 285 usb2_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0); 286 287 iface_index = UDAV_IFACE_INDEX; 288 error = usb2_transfer_setup(uaa->device, &iface_index, 289 sc->sc_xfer, udav_config, UDAV_N_TRANSFER, sc, &sc->sc_mtx); 290 if (error) { 291 device_printf(dev, "allocating USB " 292 "transfers failed!\n"); 293 goto detach; 294 } 295 error = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_mtx, 296 NULL, sizeof(struct usb2_config_td_cc), 16); 297 if (error) { 298 device_printf(dev, "could not setup config " 299 "thread!\n"); 300 goto detach; 301 } 302 mtx_lock(&sc->sc_mtx); 303 304 sc->sc_flags |= UDAV_FLAG_WAIT_LINK; 305 306 /* start setup */ 307 308 usb2_config_td_queue_command 309 (&sc->sc_config_td, NULL, &udav_cfg_first_time_setup, 0, 0); 310 311 udav_watchdog(sc); 312 mtx_unlock(&sc->sc_mtx); 313 return (0); /* success */ 314 315detach: 316 udav_detach(dev); 317 return (ENXIO); /* failure */ 318} 319 320static void 321udav_cfg_first_time_setup(struct udav_softc *sc, 322 struct usb2_config_td_cc *cc, uint16_t refcount) 323{ 324 struct ifnet *ifp; 325 int error; 326 uint8_t eaddr[min(ETHER_ADDR_LEN, 6)]; 327 328 /* reset the adapter */ 329 330 udav_cfg_reset(sc); 331 332 /* get Ethernet Address */ 333 334 udav_cfg_csr_read(sc, UDAV_PAR, eaddr, ETHER_ADDR_LEN); 335 336 mtx_unlock(&sc->sc_mtx); 337 338 ifp = if_alloc(IFT_ETHER); 339 340 mtx_lock(&sc->sc_mtx); 341 342 if (ifp == NULL) { 343 printf("%s: could not if_alloc()\n", 344 sc->sc_name); 345 goto done; 346 } 347 sc->sc_evilhack = ifp; 348 349 ifp->if_softc = sc; 350 if_initname(ifp, "udav", sc->sc_unit); 351 ifp->if_mtu = ETHERMTU; 352 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 353 ifp->if_start = udav_start_cb; 354 ifp->if_ioctl = udav_ioctl_cb; 355 ifp->if_watchdog = NULL; 356 ifp->if_init = udav_init_cb; 357 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 358 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 359 IFQ_SET_READY(&ifp->if_snd); 360 361 /* 362 * XXX need Giant when accessing the device structures ! 363 */ 364 365 mtx_unlock(&sc->sc_mtx); 366 367 mtx_lock(&Giant); 368 369 error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, 370 &udav_ifmedia_change_cb, 371 &udav_ifmedia_status_cb); 372 mtx_unlock(&Giant); 373 374 mtx_lock(&sc->sc_mtx); 375 376 if (error) { 377 printf("%s: MII without any PHY!\n", 378 sc->sc_name); 379 if_free(ifp); 380 goto done; 381 } 382 sc->sc_ifp = ifp; 383 384 mtx_unlock(&sc->sc_mtx); 385 386 /* 387 * Call MI attach routine. 388 */ 389 390 ether_ifattach(ifp, eaddr); 391 392 mtx_lock(&sc->sc_mtx); 393 394done: 395 return; 396} 397 398static int 399udav_detach(device_t dev) 400{ 401 struct udav_softc *sc = device_get_softc(dev); 402 struct ifnet *ifp; 403 404 usb2_config_td_drain(&sc->sc_config_td); 405 406 mtx_lock(&sc->sc_mtx); 407 408 usb2_callout_stop(&sc->sc_watchdog); 409 410 udav_cfg_pre_stop(sc, NULL, 0); 411 412 ifp = sc->sc_ifp; 413 414 mtx_unlock(&sc->sc_mtx); 415 416 /* stop all USB transfers first */ 417 usb2_transfer_unsetup(sc->sc_xfer, UDAV_N_TRANSFER); 418 419 /* get rid of any late children */ 420 bus_generic_detach(dev); 421 422 if (ifp) { 423 ether_ifdetach(ifp); 424 if_free(ifp); 425 } 426 usb2_config_td_unsetup(&sc->sc_config_td); 427 428 usb2_callout_drain(&sc->sc_watchdog); 429 430 mtx_destroy(&sc->sc_mtx); 431 432 return (0); 433} 434 435static void 436udav_cfg_do_request(struct udav_softc *sc, struct usb2_device_request *req, 437 void *data) 438{ 439 uint16_t length; 440 usb2_error_t err; 441 442 if (usb2_config_td_is_gone(&sc->sc_config_td)) { 443 goto error; 444 } 445 err = usb2_do_request_flags 446 (sc->sc_udev, &sc->sc_mtx, req, data, 0, NULL, 1000); 447 448 if (err) { 449 450 DPRINTF("device request failed, err=%s " 451 "(ignored)\n", usb2_errstr(err)); 452 453error: 454 length = UGETW(req->wLength); 455 456 if ((req->bmRequestType & UT_READ) && length) { 457 bzero(data, length); 458 } 459 } 460} 461 462#if 0 463static void 464udav_cfg_mem_read(struct udav_softc *sc, uint16_t offset, void *buf, 465 uint16_t len) 466{ 467 struct usb2_device_request req; 468 469 len &= 0xff; 470 471 req.bmRequestType = UT_READ_VENDOR_DEVICE; 472 req.bRequest = UDAV_REQ_MEM_READ; 473 USETW(req.wValue, 0x0000); 474 USETW(req.wIndex, offset); 475 USETW(req.wLength, len); 476 477 udav_cfg_do_request(sc, &req, buf); 478} 479 480static void 481udav_cfg_mem_write(struct udav_softc *sc, uint16_t offset, void *buf, 482 uint16_t len) 483{ 484 struct usb2_device_request req; 485 486 len &= 0xff; 487 488 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 489 req.bRequest = UDAV_REQ_MEM_WRITE; 490 USETW(req.wValue, 0x0000); 491 USETW(req.wIndex, offset); 492 USETW(req.wLength, len); 493 494 udav_cfg_do_request(sc, &req, buf); 495} 496 497static void 498udav_cfg_mem_write1(struct udav_softc *sc, uint16_t offset, 499 uint8_t ch) 500{ 501 struct usb2_device_request req; 502 503 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 504 req.bRequest = UDAV_REQ_MEM_WRITE1; 505 USETW(req.wValue, ch); 506 USETW(req.wIndex, offset); 507 USETW(req.wLength, 0x0000); 508 509 udav_cfg_do_request(sc, &req, NULL); 510} 511 512#endif 513 514static void 515udav_cfg_csr_read(struct udav_softc *sc, uint16_t offset, void *buf, 516 uint16_t len) 517{ 518 struct usb2_device_request req; 519 520 len &= 0xff; 521 522 req.bmRequestType = UT_READ_VENDOR_DEVICE; 523 req.bRequest = UDAV_REQ_REG_READ; 524 USETW(req.wValue, 0x0000); 525 USETW(req.wIndex, offset); 526 USETW(req.wLength, len); 527 528 udav_cfg_do_request(sc, &req, buf); 529} 530 531static void 532udav_cfg_csr_write(struct udav_softc *sc, uint16_t offset, void *buf, 533 uint16_t len) 534{ 535 struct usb2_device_request req; 536 537 offset &= 0xff; 538 len &= 0xff; 539 540 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 541 req.bRequest = UDAV_REQ_REG_WRITE; 542 USETW(req.wValue, 0x0000); 543 USETW(req.wIndex, offset); 544 USETW(req.wLength, len); 545 546 udav_cfg_do_request(sc, &req, buf); 547} 548 549static uint8_t 550udav_cfg_csr_read1(struct udav_softc *sc, uint16_t offset) 551{ 552 uint8_t val; 553 554 udav_cfg_csr_read(sc, offset, &val, 1); 555 return (val); 556} 557 558static void 559udav_cfg_csr_write1(struct udav_softc *sc, uint16_t offset, 560 uint8_t ch) 561{ 562 struct usb2_device_request req; 563 564 offset &= 0xff; 565 566 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 567 req.bRequest = UDAV_REQ_REG_WRITE1; 568 USETW(req.wValue, ch); 569 USETW(req.wIndex, offset); 570 USETW(req.wLength, 0x0000); 571 572 udav_cfg_do_request(sc, &req, NULL); 573} 574 575static void 576udav_init_cb(void *arg) 577{ 578 struct udav_softc *sc = arg; 579 580 mtx_lock(&sc->sc_mtx); 581 usb2_config_td_queue_command 582 (&sc->sc_config_td, &udav_cfg_pre_init, 583 &udav_cfg_init, 0, 0); 584 mtx_unlock(&sc->sc_mtx); 585} 586 587static void 588udav_cfg_pre_init(struct udav_softc *sc, 589 struct usb2_config_td_cc *cc, uint16_t refcount) 590{ 591 struct ifnet *ifp = sc->sc_ifp; 592 593 /* immediate configuration */ 594 595 udav_cfg_pre_stop(sc, cc, 0); 596 597 ifp->if_drv_flags |= IFF_DRV_RUNNING; 598 599 sc->sc_flags |= UDAV_FLAG_HL_READY; 600} 601 602static void 603udav_cfg_init(struct udav_softc *sc, 604 struct usb2_config_td_cc *cc, uint16_t refcount) 605{ 606 struct mii_data *mii = GET_MII(sc); 607 608 /* 609 * Cancel pending I/O 610 */ 611 612 udav_cfg_stop(sc, cc, 0); 613 614 /* set MAC address */ 615 616 udav_cfg_csr_write(sc, UDAV_PAR, cc->if_lladdr, ETHER_ADDR_LEN); 617 618 /* initialize network control register */ 619 620 /* disable loopback */ 621 622 UDAV_CFG_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1); 623 624 /* Initialize RX control register */ 625 UDAV_CFG_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC); 626 627 /* load multicast filter and update promiscious mode bit */ 628 udav_cfg_promisc_upd(sc, cc, 0); 629 630 /* enable RX */ 631 UDAV_CFG_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN); 632 633 /* clear POWER_DOWN state of internal PHY */ 634 UDAV_CFG_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0); 635 UDAV_CFG_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0); 636 637 mii_mediachg(mii); 638 639 sc->sc_flags |= (UDAV_FLAG_READ_STALL | 640 UDAV_FLAG_WRITE_STALL | 641 UDAV_FLAG_LL_READY); 642 643 udav_start_transfers(sc); 644} 645 646static void 647udav_cfg_reset(struct udav_softc *sc) 648{ 649 usb2_error_t err; 650 uint16_t to; 651 652 /* Select PHY */ 653#if 1 654 /* 655 * XXX: force select internal phy. 656 * external phy routines are not tested. 657 */ 658 UDAV_CFG_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY); 659#else 660 if (sc->sc_flags & UDAV_EXT_PHY) { 661 UDAV_CFG_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY); 662 } else { 663 UDAV_CFG_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY); 664 } 665#endif 666 667 UDAV_CFG_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST); 668 669 for (to = 0;; to++) { 670 671 if (to < 100) { 672 673 err = usb2_config_td_sleep(&sc->sc_config_td, hz / 100); 674 675 if (err) { 676 break; 677 } 678 if (!(udav_cfg_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST)) { 679 break; 680 } 681 } else { 682 printf("%s: reset timeout!\n", 683 sc->sc_name); 684 break; 685 } 686 } 687 688 err = usb2_config_td_sleep(&sc->sc_config_td, hz / 100); 689} 690 691#define UDAV_BITS 6 692 693static void 694udav_mchash(struct usb2_config_td_cc *cc, const uint8_t *ptr) 695{ 696 uint8_t h; 697 698 h = ether_crc32_le(ptr, ETHER_ADDR_LEN) & 699 ((1 << UDAV_BITS) - 1); 700 cc->if_hash[h >> 3] |= 1 << (h & 0x7); 701} 702 703static void 704udav_config_copy(struct udav_softc *sc, 705 struct usb2_config_td_cc *cc, uint16_t refcount) 706{ 707 bzero(cc, sizeof(*cc)); 708 usb2_ether_cc(sc->sc_ifp, &udav_mchash, cc); 709} 710 711static void 712udav_cfg_promisc_upd(struct udav_softc *sc, 713 struct usb2_config_td_cc *cc, uint16_t refcount) 714{ 715 uint8_t rxmode; 716 717 rxmode = udav_cfg_csr_read1(sc, UDAV_RCR); 718 719 rxmode &= ~(UDAV_RCR_ALL | UDAV_RCR_PRMSC); 720 721 if (cc->if_flags & IFF_PROMISC) { 722 723 rxmode |= UDAV_RCR_ALL | UDAV_RCR_PRMSC; 724 725 } else if (cc->if_flags & IFF_ALLMULTI) { 726 727 rxmode |= UDAV_RCR_ALL; 728 } 729 /* write hash value to the register */ 730 udav_cfg_csr_write(sc, UDAV_MAR, cc->if_hash, 8); 731 732 /* write new mode bits */ 733 udav_cfg_csr_write1(sc, UDAV_RCR, rxmode); 734} 735 736static void 737udav_start_cb(struct ifnet *ifp) 738{ 739 struct udav_softc *sc = ifp->if_softc; 740 741 mtx_lock(&sc->sc_mtx); 742 743 udav_start_transfers(sc); 744 745 mtx_unlock(&sc->sc_mtx); 746} 747 748static void 749udav_start_transfers(struct udav_softc *sc) 750{ 751 if ((sc->sc_flags & UDAV_FLAG_LL_READY) && 752 (sc->sc_flags & UDAV_FLAG_HL_READY)) { 753 754 /* 755 * start the USB transfers, if not already started: 756 */ 757 usb2_transfer_start(sc->sc_xfer[UDAV_INTR_DT_RD]); 758 usb2_transfer_start(sc->sc_xfer[UDAV_BULK_DT_RD]); 759 usb2_transfer_start(sc->sc_xfer[UDAV_BULK_DT_WR]); 760 } 761} 762 763static void 764udav_bulk_write_clear_stall_callback(struct usb2_xfer *xfer) 765{ 766 struct udav_softc *sc = xfer->priv_sc; 767 struct usb2_xfer *xfer_other = sc->sc_xfer[UDAV_BULK_DT_WR]; 768 769 if (usb2_clear_stall_callback(xfer, xfer_other)) { 770 DPRINTF("stall cleared\n"); 771 sc->sc_flags &= ~UDAV_FLAG_WRITE_STALL; 772 usb2_transfer_start(xfer_other); 773 } 774} 775 776static void 777udav_bulk_write_callback(struct usb2_xfer *xfer) 778{ 779 struct udav_softc *sc = xfer->priv_sc; 780 struct ifnet *ifp = sc->sc_ifp; 781 struct mbuf *m; 782 uint32_t extra_len; 783 uint32_t temp_len; 784 uint8_t buf[2]; 785 786 switch (USB_GET_STATE(xfer)) { 787 case USB_ST_TRANSFERRED: 788 DPRINTFN(11, "transfer complete\n"); 789 790 ifp->if_opackets++; 791 792 case USB_ST_SETUP: 793 794 if (sc->sc_flags & UDAV_FLAG_WRITE_STALL) { 795 usb2_transfer_start(sc->sc_xfer[UDAV_BULK_CS_WR]); 796 goto done; 797 } 798 if (sc->sc_flags & UDAV_FLAG_WAIT_LINK) { 799 /* 800 * don't send anything if there is no link ! 801 */ 802 goto done; 803 } 804 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 805 806 if (m == NULL) { 807 goto done; 808 } 809 if (m->m_pkthdr.len > MCLBYTES) { 810 m->m_pkthdr.len = MCLBYTES; 811 } 812 if (m->m_pkthdr.len < UDAV_MIN_FRAME_LEN) { 813 extra_len = UDAV_MIN_FRAME_LEN - m->m_pkthdr.len; 814 } else { 815 extra_len = 0; 816 } 817 818 temp_len = (m->m_pkthdr.len + extra_len); 819 820 /* 821 * the frame length is specified in the first 2 bytes of the 822 * buffer 823 */ 824 buf[0] = (uint8_t)(temp_len); 825 buf[1] = (uint8_t)(temp_len >> 8); 826 827 temp_len += 2; 828 829 usb2_copy_in(xfer->frbuffers, 0, buf, 2); 830 831 usb2_m_copy_in(xfer->frbuffers, 2, 832 m, 0, m->m_pkthdr.len); 833 834 if (extra_len) { 835 usb2_bzero(xfer->frbuffers, temp_len - extra_len, 836 extra_len); 837 } 838 /* 839 * if there's a BPF listener, bounce a copy 840 * of this frame to him: 841 */ 842 BPF_MTAP(ifp, m); 843 844 m_freem(m); 845 846 xfer->frlengths[0] = temp_len; 847 usb2_start_hardware(xfer); 848 849done: 850 return; 851 852 default: /* Error */ 853 DPRINTFN(11, "transfer error, %s\n", 854 usb2_errstr(xfer->error)); 855 856 if (xfer->error != USB_ERR_CANCELLED) { 857 /* try to clear stall first */ 858 sc->sc_flags |= UDAV_FLAG_WRITE_STALL; 859 usb2_transfer_start(sc->sc_xfer[UDAV_BULK_CS_WR]); 860 } 861 ifp->if_oerrors++; 862 return; 863 864 } 865} 866 867static void 868udav_bulk_read_clear_stall_callback(struct usb2_xfer *xfer) 869{ 870 struct udav_softc *sc = xfer->priv_sc; 871 struct usb2_xfer *xfer_other = sc->sc_xfer[UDAV_BULK_DT_RD]; 872 873 if (usb2_clear_stall_callback(xfer, xfer_other)) { 874 DPRINTF("stall cleared\n"); 875 sc->sc_flags &= ~UDAV_FLAG_READ_STALL; 876 usb2_transfer_start(xfer_other); 877 } 878} 879 880static void 881udav_bulk_read_callback(struct usb2_xfer *xfer) 882{ 883 struct udav_softc *sc = xfer->priv_sc; 884 struct ifnet *ifp = sc->sc_ifp; 885 uint8_t status; 886 uint16_t total_len; 887 struct mbuf *m = NULL; 888 889 switch (USB_GET_STATE(xfer)) { 890 case USB_ST_TRANSFERRED: 891 892 if (xfer->actlen < 1) { 893 ifp->if_ierrors++; 894 goto tr_setup; 895 } 896 xfer->actlen -= 1; 897 898 usb2_copy_out(xfer->frbuffers, 0, &status, 1); 899 900 if (status & UDAV_RSR_LCS) { 901 ifp->if_collisions++; 902 goto tr_setup; 903 } 904 if ((status & UDAV_RSR_ERR) || (xfer->actlen < 2)) { 905 ifp->if_ierrors++; 906 goto tr_setup; 907 } 908 usb2_copy_out(xfer->frbuffers, 1, &total_len, 2); 909 910 total_len = le16toh(total_len); 911 912 xfer->actlen -= 2; 913 914 xfer->actlen = min(xfer->actlen, total_len); 915 916 if (xfer->actlen < (sizeof(struct ether_header) + ETHER_CRC_LEN)) { 917 ifp->if_ierrors++; 918 goto tr_setup; 919 } 920 xfer->actlen -= ETHER_CRC_LEN; 921 922 m = usb2_ether_get_mbuf(); 923 924 if (m == NULL) { 925 ifp->if_ierrors++; 926 goto tr_setup; 927 } 928 xfer->actlen = min(xfer->actlen, m->m_len); 929 930 usb2_copy_out(xfer->frbuffers, 3, m->m_data, xfer->actlen); 931 932 ifp->if_ipackets++; 933 m->m_pkthdr.rcvif = ifp; 934 m->m_pkthdr.len = m->m_len = xfer->actlen; 935 936 case USB_ST_SETUP: 937tr_setup: 938 939 if (sc->sc_flags & UDAV_FLAG_READ_STALL) { 940 usb2_transfer_start(sc->sc_xfer[UDAV_BULK_CS_RD]); 941 } else { 942 xfer->frlengths[0] = xfer->max_data_length; 943 usb2_start_hardware(xfer); 944 } 945 946 /* 947 * At the end of a USB callback it is always safe to unlock 948 * the private mutex of a device! That is why we do the 949 * "if_input" here, and not some lines up! 950 */ 951 if (m) { 952 mtx_unlock(&sc->sc_mtx); 953 (ifp->if_input) (ifp, m); 954 mtx_lock(&sc->sc_mtx); 955 } 956 return; 957 958 default: /* Error */ 959 if (xfer->error != USB_ERR_CANCELLED) { 960 /* try to clear stall first */ 961 sc->sc_flags |= UDAV_FLAG_READ_STALL; 962 usb2_transfer_start(sc->sc_xfer[UDAV_BULK_CS_RD]); 963 } 964 DPRINTF("bulk read error, %s\n", 965 usb2_errstr(xfer->error)); 966 return; 967 968 } 969} 970 971static void 972udav_intr_clear_stall_callback(struct usb2_xfer *xfer) 973{ 974 struct udav_softc *sc = xfer->priv_sc; 975 struct usb2_xfer *xfer_other = sc->sc_xfer[UDAV_INTR_DT_RD]; 976 977 if (usb2_clear_stall_callback(xfer, xfer_other)) { 978 DPRINTF("stall cleared\n"); 979 sc->sc_flags &= ~UDAV_FLAG_INTR_STALL; 980 usb2_transfer_start(xfer_other); 981 } 982} 983 984static void 985udav_intr_callback(struct usb2_xfer *xfer) 986{ 987 struct udav_softc *sc = xfer->priv_sc; 988 989 switch (USB_GET_STATE(xfer)) { 990 case USB_ST_TRANSFERRED: 991 992 case USB_ST_SETUP: 993 if (sc->sc_flags & UDAV_FLAG_INTR_STALL) { 994 usb2_transfer_start(sc->sc_xfer[UDAV_INTR_CS_RD]); 995 } else { 996 xfer->frlengths[0] = xfer->max_data_length; 997 usb2_start_hardware(xfer); 998 } 999 return; 1000 1001 default: /* Error */ 1002 if (xfer->error != USB_ERR_CANCELLED) { 1003 /* start clear stall */ 1004 sc->sc_flags |= UDAV_FLAG_INTR_STALL; 1005 usb2_transfer_start(sc->sc_xfer[UDAV_INTR_CS_RD]); 1006 } 1007 return; 1008 } 1009} 1010 1011static int 1012udav_ioctl_cb(struct ifnet *ifp, u_long cmd, caddr_t data) 1013{ 1014 struct udav_softc *sc = ifp->if_softc; 1015 struct mii_data *mii; 1016 int error = 0; 1017 1018 switch (cmd) { 1019 case SIOCSIFFLAGS: 1020 mtx_lock(&sc->sc_mtx); 1021 if (ifp->if_flags & IFF_UP) { 1022 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1023 usb2_config_td_queue_command 1024 (&sc->sc_config_td, &udav_config_copy, 1025 &udav_cfg_promisc_upd, 0, 0); 1026 } else { 1027 usb2_config_td_queue_command 1028 (&sc->sc_config_td, &udav_cfg_pre_init, 1029 &udav_cfg_init, 0, 0); 1030 } 1031 } else { 1032 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1033 usb2_config_td_queue_command 1034 (&sc->sc_config_td, &udav_cfg_pre_stop, 1035 &udav_cfg_stop, 0, 0); 1036 } 1037 } 1038 mtx_unlock(&sc->sc_mtx); 1039 break; 1040 1041 case SIOCADDMULTI: 1042 case SIOCDELMULTI: 1043 mtx_lock(&sc->sc_mtx); 1044 usb2_config_td_queue_command 1045 (&sc->sc_config_td, &udav_config_copy, 1046 &udav_cfg_promisc_upd, 0, 0); 1047 mtx_unlock(&sc->sc_mtx); 1048 break; 1049 1050 case SIOCGIFMEDIA: 1051 case SIOCSIFMEDIA: 1052 mii = GET_MII(sc); 1053 if (mii == NULL) { 1054 error = EINVAL; 1055 } else { 1056 error = ifmedia_ioctl 1057 (ifp, (void *)data, &mii->mii_media, cmd); 1058 } 1059 break; 1060 1061 default: 1062 error = ether_ioctl(ifp, cmd, data); 1063 break; 1064 } 1065 return (error); 1066} 1067 1068static void 1069udav_watchdog(void *arg) 1070{ 1071 struct udav_softc *sc = arg; 1072 1073 mtx_assert(&sc->sc_mtx, MA_OWNED); 1074 1075 usb2_config_td_queue_command 1076 (&sc->sc_config_td, NULL, &udav_cfg_tick, 0, 0); 1077 1078 usb2_callout_reset(&sc->sc_watchdog, 1079 hz, &udav_watchdog, sc); 1080} 1081 1082/* 1083 * NOTE: can be called when "ifp" is NULL 1084 */ 1085static void 1086udav_cfg_pre_stop(struct udav_softc *sc, 1087 struct usb2_config_td_cc *cc, uint16_t refcount) 1088{ 1089 struct ifnet *ifp = sc->sc_ifp; 1090 1091 if (cc) { 1092 /* copy the needed configuration */ 1093 udav_config_copy(sc, cc, refcount); 1094 } 1095 /* immediate configuration */ 1096 1097 if (ifp) { 1098 /* clear flags */ 1099 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1100 } 1101 sc->sc_flags &= ~(UDAV_FLAG_HL_READY | 1102 UDAV_FLAG_LL_READY); 1103 1104 sc->sc_flags |= UDAV_FLAG_WAIT_LINK; 1105 1106 /* 1107 * stop all the transfers, if not already stopped: 1108 */ 1109 usb2_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_WR]); 1110 usb2_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_RD]); 1111 usb2_transfer_stop(sc->sc_xfer[UDAV_BULK_CS_WR]); 1112 usb2_transfer_stop(sc->sc_xfer[UDAV_BULK_CS_RD]); 1113 usb2_transfer_stop(sc->sc_xfer[UDAV_INTR_DT_RD]); 1114 usb2_transfer_stop(sc->sc_xfer[UDAV_INTR_CS_RD]); 1115} 1116 1117/* 1118 * NOTE: can be called when "ifp" is NULL 1119 */ 1120static void 1121udav_cfg_stop(struct udav_softc *sc, 1122 struct usb2_config_td_cc *cc, uint16_t refcount) 1123{ 1124 udav_cfg_reset(sc); 1125} 1126 1127static int 1128udav_ifmedia_change_cb(struct ifnet *ifp) 1129{ 1130 struct udav_softc *sc = ifp->if_softc; 1131 1132 mtx_lock(&sc->sc_mtx); 1133 usb2_config_td_queue_command 1134 (&sc->sc_config_td, NULL, 1135 &udav_cfg_ifmedia_change, 0, 0); 1136 mtx_unlock(&sc->sc_mtx); 1137 1138 return (0); 1139} 1140 1141static void 1142udav_cfg_ifmedia_change(struct udav_softc *sc, 1143 struct usb2_config_td_cc *cc, uint16_t refcount) 1144{ 1145 struct ifnet *ifp = sc->sc_ifp; 1146 struct mii_data *mii = GET_MII(sc); 1147 1148 if ((ifp == NULL) || 1149 (mii == NULL)) { 1150 /* not ready */ 1151 return; 1152 } 1153 sc->sc_flags |= UDAV_FLAG_WAIT_LINK; 1154 1155 if (mii->mii_instance) { 1156 struct mii_softc *miisc; 1157 1158 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) { 1159 mii_phy_reset(miisc); 1160 } 1161 } 1162 mii_mediachg(mii); 1163} 1164 1165static void 1166udav_ifmedia_status_cb(struct ifnet *ifp, struct ifmediareq *ifmr) 1167{ 1168 struct udav_softc *sc = ifp->if_softc; 1169 1170 mtx_lock(&sc->sc_mtx); 1171 1172 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 1173 ifmr->ifm_active = sc->sc_media_active; 1174 ifmr->ifm_status = sc->sc_media_status; 1175 } else { 1176 ifmr->ifm_active = IFM_ETHER | IFM_NONE; 1177 ifmr->ifm_status = 0; 1178 } 1179 1180 mtx_unlock(&sc->sc_mtx); 1181} 1182 1183static void 1184udav_cfg_tick(struct udav_softc *sc, 1185 struct usb2_config_td_cc *cc, uint16_t refcount) 1186{ 1187 struct ifnet *ifp = sc->sc_ifp; 1188 struct mii_data *mii = GET_MII(sc); 1189 1190 if ((ifp == NULL) || 1191 (mii == NULL)) { 1192 /* not ready */ 1193 return; 1194 } 1195 mii_tick(mii); 1196 1197 mii_pollstat(mii); 1198 1199 if ((sc->sc_flags & UDAV_FLAG_WAIT_LINK) && 1200 (mii->mii_media_status & IFM_ACTIVE) && 1201 (IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)) { 1202 sc->sc_flags &= ~UDAV_FLAG_WAIT_LINK; 1203 } 1204 sc->sc_media_active = mii->mii_media_active; 1205 sc->sc_media_status = mii->mii_media_status; 1206 1207 /* start stopped transfers, if any */ 1208 1209 udav_start_transfers(sc); 1210} 1211 1212static int 1213udav_cfg_miibus_readreg(device_t dev, int phy, int reg) 1214{ 1215 struct udav_softc *sc = device_get_softc(dev); 1216 uint16_t data16; 1217 uint8_t val[2]; 1218 uint8_t do_unlock; 1219 1220 /* XXX: one PHY only for the internal PHY */ 1221 if (phy != 0) { 1222 return (0); 1223 } 1224 /* avoid recursive locking */ 1225 if (mtx_owned(&sc->sc_mtx)) { 1226 do_unlock = 0; 1227 } else { 1228 mtx_lock(&sc->sc_mtx); 1229 do_unlock = 1; 1230 } 1231 1232 /* select internal PHY and set PHY register address */ 1233 udav_cfg_csr_write1(sc, UDAV_EPAR, 1234 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK)); 1235 1236 /* select PHY operation and start read command */ 1237 udav_cfg_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR); 1238 1239 /* XXX: should we wait? */ 1240 1241 /* end read command */ 1242 UDAV_CFG_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR); 1243 1244 /* retrieve the result from data registers */ 1245 udav_cfg_csr_read(sc, UDAV_EPDRL, val, 2); 1246 1247 if (do_unlock) { 1248 mtx_unlock(&sc->sc_mtx); 1249 } 1250 data16 = (val[0] | (val[1] << 8)); 1251 1252 DPRINTFN(11, "phy=%d reg=0x%04x => 0x%04x\n", 1253 phy, reg, data16); 1254 1255 return (data16); 1256} 1257 1258static int 1259udav_cfg_miibus_writereg(device_t dev, int phy, int reg, int data) 1260{ 1261 struct udav_softc *sc = device_get_softc(dev); 1262 uint8_t val[2]; 1263 uint8_t do_unlock; 1264 1265 /* XXX: one PHY only for the internal PHY */ 1266 if (phy != 0) { 1267 return (0); 1268 } 1269 /* avoid recursive locking */ 1270 if (mtx_owned(&sc->sc_mtx)) { 1271 do_unlock = 0; 1272 } else { 1273 mtx_lock(&sc->sc_mtx); 1274 do_unlock = 1; 1275 } 1276 1277 /* select internal PHY and set PHY register address */ 1278 udav_cfg_csr_write1(sc, UDAV_EPAR, 1279 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK)); 1280 1281 /* put the value to the data registers */ 1282 val[0] = (data & 0xff); 1283 val[1] = (data >> 8) & 0xff; 1284 udav_cfg_csr_write(sc, UDAV_EPDRL, val, 2); 1285 1286 /* select PHY operation and start write command */ 1287 udav_cfg_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW); 1288 1289 /* XXX: should we wait? */ 1290 1291 /* end write command */ 1292 UDAV_CFG_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW); 1293 1294 if (do_unlock) { 1295 mtx_unlock(&sc->sc_mtx); 1296 } 1297 return (0); 1298} 1299 1300static void 1301udav_cfg_miibus_statchg(device_t dev) 1302{ 1303 /* nothing to do */ 1304} 1305 1306/* 1307 * Stop all chip I/O so that the kernel's probe routines don't 1308 * get confused by errant DMAs when rebooting. 1309 */ 1310static int 1311udav_shutdown(device_t dev) 1312{ 1313 struct udav_softc *sc = device_get_softc(dev); 1314 1315 mtx_lock(&sc->sc_mtx); 1316 1317 usb2_config_td_queue_command 1318 (&sc->sc_config_td, &udav_cfg_pre_stop, 1319 &udav_cfg_stop, 0, 0); 1320 1321 mtx_unlock(&sc->sc_mtx); 1322 1323 return (0); 1324} 1325