| Commit | Line | Data |
|---|---|---|
| 12bd3c8b SW |
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$ */ | |
| 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 | * TODO: | |
| 42 | * Interrupt Endpoint support | |
| 43 | * External PHYs | |
| 44 | */ | |
| 45 | ||
| 12bd3c8b | 46 | #include <sys/stdint.h> |
| 12bd3c8b SW |
47 | #include <sys/param.h> |
| 48 | #include <sys/queue.h> | |
| 49 | #include <sys/types.h> | |
| 50 | #include <sys/systm.h> | |
| 51 | #include <sys/kernel.h> | |
| 52 | #include <sys/bus.h> | |
| 53 | #include <sys/module.h> | |
| 54 | #include <sys/lock.h> | |
| 55 | #include <sys/mutex.h> | |
| 56 | #include <sys/condvar.h> | |
| 57 | #include <sys/sysctl.h> | |
| 12bd3c8b SW |
58 | #include <sys/unistd.h> |
| 59 | #include <sys/callout.h> | |
| 60 | #include <sys/malloc.h> | |
| 61 | #include <sys/priv.h> | |
| 62 | ||
| b946173a SW |
63 | #include <net/ifq_var.h> |
| 64 | ||
| 65 | #include <bus/u4b/usb.h> | |
| 66 | #include <bus/u4b/usbdi.h> | |
| 67 | #include <bus/u4b/usbdi_util.h> | |
| 68 | #include <bus/u4b/usbdevs.h> | |
| 12bd3c8b SW |
69 | |
| 70 | #define USB_DEBUG_VAR udav_debug | |
| b946173a SW |
71 | #include <bus/u4b/usb_debug.h> |
| 72 | #include <bus/u4b/usb_process.h> | |
| 12bd3c8b | 73 | |
| b946173a SW |
74 | #include <bus/u4b/net/usb_ethernet.h> |
| 75 | #include <bus/u4b/net/if_udavreg.h> | |
| 12bd3c8b SW |
76 | |
| 77 | /* prototypes */ | |
| 78 | ||
| 79 | static device_probe_t udav_probe; | |
| 80 | static device_attach_t udav_attach; | |
| 81 | static device_detach_t udav_detach; | |
| 82 | ||
| 83 | static usb_callback_t udav_bulk_write_callback; | |
| 84 | static usb_callback_t udav_bulk_read_callback; | |
| 85 | static usb_callback_t udav_intr_callback; | |
| 86 | ||
| 87 | static uether_fn_t udav_attach_post; | |
| 88 | static uether_fn_t udav_init; | |
| 89 | static uether_fn_t udav_stop; | |
| 90 | static uether_fn_t udav_start; | |
| 91 | static uether_fn_t udav_tick; | |
| 92 | static uether_fn_t udav_setmulti; | |
| 93 | static uether_fn_t udav_setpromisc; | |
| 94 | ||
| 95 | static int udav_csr_read(struct udav_softc *, uint16_t, void *, int); | |
| 96 | static int udav_csr_write(struct udav_softc *, uint16_t, void *, int); | |
| 97 | static uint8_t udav_csr_read1(struct udav_softc *, uint16_t); | |
| 98 | static int udav_csr_write1(struct udav_softc *, uint16_t, uint8_t); | |
| 99 | static void udav_reset(struct udav_softc *); | |
| 100 | static int udav_ifmedia_upd(struct ifnet *); | |
| 101 | static void udav_ifmedia_status(struct ifnet *, struct ifmediareq *); | |
| 102 | ||
| 103 | static miibus_readreg_t udav_miibus_readreg; | |
| 104 | static miibus_writereg_t udav_miibus_writereg; | |
| 105 | static miibus_statchg_t udav_miibus_statchg; | |
| 106 | ||
| 107 | static const struct usb_config udav_config[UDAV_N_TRANSFER] = { | |
| 108 | ||
| 109 | [UDAV_BULK_DT_WR] = { | |
| 110 | .type = UE_BULK, | |
| 111 | .endpoint = UE_ADDR_ANY, | |
| 112 | .direction = UE_DIR_OUT, | |
| 113 | .bufsize = (MCLBYTES + 2), | |
| 114 | .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, | |
| 115 | .callback = udav_bulk_write_callback, | |
| 116 | .timeout = 10000, /* 10 seconds */ | |
| 117 | }, | |
| 118 | ||
| 119 | [UDAV_BULK_DT_RD] = { | |
| 120 | .type = UE_BULK, | |
| 121 | .endpoint = UE_ADDR_ANY, | |
| 122 | .direction = UE_DIR_IN, | |
| 123 | .bufsize = (MCLBYTES + 3), | |
| 124 | .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, | |
| 125 | .callback = udav_bulk_read_callback, | |
| 126 | .timeout = 0, /* no timeout */ | |
| 127 | }, | |
| 128 | ||
| 129 | [UDAV_INTR_DT_RD] = { | |
| 130 | .type = UE_INTERRUPT, | |
| 131 | .endpoint = UE_ADDR_ANY, | |
| 132 | .direction = UE_DIR_IN, | |
| 133 | .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, | |
| 134 | .bufsize = 0, /* use wMaxPacketSize */ | |
| 135 | .callback = udav_intr_callback, | |
| 136 | }, | |
| 137 | }; | |
| 138 | ||
| 139 | static device_method_t udav_methods[] = { | |
| 140 | /* Device interface */ | |
| 141 | DEVMETHOD(device_probe, udav_probe), | |
| 142 | DEVMETHOD(device_attach, udav_attach), | |
| 143 | DEVMETHOD(device_detach, udav_detach), | |
| 144 | ||
| 145 | /* MII interface */ | |
| 146 | DEVMETHOD(miibus_readreg, udav_miibus_readreg), | |
| 147 | DEVMETHOD(miibus_writereg, udav_miibus_writereg), | |
| 148 | DEVMETHOD(miibus_statchg, udav_miibus_statchg), | |
| 149 | ||
| b946173a | 150 | {0, 0} |
| 12bd3c8b SW |
151 | }; |
| 152 | ||
| 153 | static driver_t udav_driver = { | |
| 154 | .name = "udav", | |
| 155 | .methods = udav_methods, | |
| 156 | .size = sizeof(struct udav_softc), | |
| 157 | }; | |
| 158 | ||
| 159 | static devclass_t udav_devclass; | |
| 160 | ||
| 15f415f6 SW |
161 | DRIVER_MODULE(udav, uhub, udav_driver, udav_devclass, NULL, NULL); |
| 162 | DRIVER_MODULE(miibus, udav, miibus_driver, miibus_devclass, NULL, NULL); | |
| 12bd3c8b SW |
163 | MODULE_DEPEND(udav, uether, 1, 1, 1); |
| 164 | MODULE_DEPEND(udav, usb, 1, 1, 1); | |
| 165 | MODULE_DEPEND(udav, ether, 1, 1, 1); | |
| 166 | MODULE_DEPEND(udav, miibus, 1, 1, 1); | |
| 167 | MODULE_VERSION(udav, 1); | |
| 168 | ||
| 169 | static const struct usb_ether_methods udav_ue_methods = { | |
| 170 | .ue_attach_post = udav_attach_post, | |
| 171 | .ue_start = udav_start, | |
| 172 | .ue_init = udav_init, | |
| 173 | .ue_stop = udav_stop, | |
| 174 | .ue_tick = udav_tick, | |
| 175 | .ue_setmulti = udav_setmulti, | |
| 176 | .ue_setpromisc = udav_setpromisc, | |
| 177 | .ue_mii_upd = udav_ifmedia_upd, | |
| 178 | .ue_mii_sts = udav_ifmedia_status, | |
| 179 | }; | |
| 180 | ||
| 181 | #ifdef USB_DEBUG | |
| 182 | static int udav_debug = 0; | |
| 183 | ||
| 184 | static SYSCTL_NODE(_hw_usb, OID_AUTO, udav, CTLFLAG_RW, 0, "USB udav"); | |
| 185 | SYSCTL_INT(_hw_usb_udav, OID_AUTO, debug, CTLFLAG_RW, &udav_debug, 0, | |
| 186 | "Debug level"); | |
| 187 | #endif | |
| 188 | ||
| 189 | #define UDAV_SETBIT(sc, reg, x) \ | |
| 190 | udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x)) | |
| 191 | ||
| 192 | #define UDAV_CLRBIT(sc, reg, x) \ | |
| 193 | udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x)) | |
| 194 | ||
| 195 | static const STRUCT_USB_HOST_ID udav_devs[] = { | |
| 196 | /* ShanTou DM9601 USB NIC */ | |
| 197 | {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_DM9601, 0)}, | |
| 198 | /* ShanTou ST268 USB NIC */ | |
| 199 | {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268, 0)}, | |
| 200 | /* Corega USB-TXC */ | |
| 201 | {USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC, 0)}, | |
| 202 | /* ShanTou AMD8515 USB NIC */ | |
| 203 | {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ADM8515, 0)}, | |
| 204 | /* Kontron AG USB Ethernet */ | |
| 205 | {USB_VPI(USB_VENDOR_KONTRON, USB_PRODUCT_KONTRON_DM9601, 0)}, | |
| 206 | {USB_VPI(USB_VENDOR_KONTRON, USB_PRODUCT_KONTRON_JP1082, 0)}, | |
| 207 | }; | |
| 208 | ||
| 209 | static void | |
| 210 | udav_attach_post(struct usb_ether *ue) | |
| 211 | { | |
| 212 | struct udav_softc *sc = uether_getsc(ue); | |
| 213 | ||
| 214 | /* reset the adapter */ | |
| 215 | udav_reset(sc); | |
| 216 | ||
| 217 | /* Get Ethernet Address */ | |
| 218 | udav_csr_read(sc, UDAV_PAR, ue->ue_eaddr, ETHER_ADDR_LEN); | |
| 219 | } | |
| 220 | ||
| 221 | static int | |
| 222 | udav_probe(device_t dev) | |
| 223 | { | |
| 224 | struct usb_attach_arg *uaa = device_get_ivars(dev); | |
| 225 | ||
| 226 | if (uaa->usb_mode != USB_MODE_HOST) | |
| 227 | return (ENXIO); | |
| 228 | if (uaa->info.bConfigIndex != UDAV_CONFIG_INDEX) | |
| 229 | return (ENXIO); | |
| 230 | if (uaa->info.bIfaceIndex != UDAV_IFACE_INDEX) | |
| 231 | return (ENXIO); | |
| 232 | ||
| 233 | return (usbd_lookup_id_by_uaa(udav_devs, sizeof(udav_devs), uaa)); | |
| 234 | } | |
| 235 | ||
| 236 | static int | |
| 237 | udav_attach(device_t dev) | |
| 238 | { | |
| 239 | struct usb_attach_arg *uaa = device_get_ivars(dev); | |
| 240 | struct udav_softc *sc = device_get_softc(dev); | |
| 241 | struct usb_ether *ue = &sc->sc_ue; | |
| 242 | uint8_t iface_index; | |
| 243 | int error; | |
| 244 | ||
| 245 | sc->sc_flags = USB_GET_DRIVER_INFO(uaa); | |
| 246 | ||
| 247 | device_set_usb_desc(dev); | |
| 248 | ||
| b946173a | 249 | lockinit(&sc->sc_lock, device_get_nameunit(dev), 0, 0); |
| 12bd3c8b SW |
250 | |
| 251 | iface_index = UDAV_IFACE_INDEX; | |
| 252 | error = usbd_transfer_setup(uaa->device, &iface_index, | |
| b946173a | 253 | sc->sc_xfer, udav_config, UDAV_N_TRANSFER, sc, &sc->sc_lock); |
| 12bd3c8b SW |
254 | if (error) { |
| 255 | device_printf(dev, "allocating USB transfers failed\n"); | |
| 256 | goto detach; | |
| 257 | } | |
| 258 | ||
| 259 | ue->ue_sc = sc; | |
| 260 | ue->ue_dev = dev; | |
| 261 | ue->ue_udev = uaa->device; | |
| b946173a | 262 | ue->ue_lock = &sc->sc_lock; |
| 12bd3c8b SW |
263 | ue->ue_methods = &udav_ue_methods; |
| 264 | ||
| 265 | error = uether_ifattach(ue); | |
| 266 | if (error) { | |
| 267 | device_printf(dev, "could not attach interface\n"); | |
| 268 | goto detach; | |
| 269 | } | |
| 270 | ||
| 271 | return (0); /* success */ | |
| 272 | ||
| 273 | detach: | |
| 274 | udav_detach(dev); | |
| 275 | return (ENXIO); /* failure */ | |
| 276 | } | |
| 277 | ||
| 278 | static int | |
| 279 | udav_detach(device_t dev) | |
| 280 | { | |
| 281 | struct udav_softc *sc = device_get_softc(dev); | |
| 282 | struct usb_ether *ue = &sc->sc_ue; | |
| 283 | ||
| 284 | usbd_transfer_unsetup(sc->sc_xfer, UDAV_N_TRANSFER); | |
| 285 | uether_ifdetach(ue); | |
| b946173a | 286 | lockuninit(&sc->sc_lock); |
| 12bd3c8b SW |
287 | |
| 288 | return (0); | |
| 289 | } | |
| 290 | ||
| 291 | #if 0 | |
| 292 | static int | |
| 293 | udav_mem_read(struct udav_softc *sc, uint16_t offset, void *buf, | |
| 294 | int len) | |
| 295 | { | |
| 296 | struct usb_device_request req; | |
| 297 | ||
| 298 | len &= 0xff; | |
| 299 | ||
| 300 | req.bmRequestType = UT_READ_VENDOR_DEVICE; | |
| 301 | req.bRequest = UDAV_REQ_MEM_READ; | |
| 302 | USETW(req.wValue, 0x0000); | |
| 303 | USETW(req.wIndex, offset); | |
| 304 | USETW(req.wLength, len); | |
| 305 | ||
| 306 | return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); | |
| 307 | } | |
| 308 | ||
| 309 | static int | |
| 310 | udav_mem_write(struct udav_softc *sc, uint16_t offset, void *buf, | |
| 311 | int len) | |
| 312 | { | |
| 313 | struct usb_device_request req; | |
| 314 | ||
| 315 | len &= 0xff; | |
| 316 | ||
| 317 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; | |
| 318 | req.bRequest = UDAV_REQ_MEM_WRITE; | |
| 319 | USETW(req.wValue, 0x0000); | |
| 320 | USETW(req.wIndex, offset); | |
| 321 | USETW(req.wLength, len); | |
| 322 | ||
| 323 | return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); | |
| 324 | } | |
| 325 | ||
| 326 | static int | |
| 327 | udav_mem_write1(struct udav_softc *sc, uint16_t offset, | |
| 328 | uint8_t ch) | |
| 329 | { | |
| 330 | struct usb_device_request req; | |
| 331 | ||
| 332 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; | |
| 333 | req.bRequest = UDAV_REQ_MEM_WRITE1; | |
| 334 | USETW(req.wValue, ch); | |
| 335 | USETW(req.wIndex, offset); | |
| 336 | USETW(req.wLength, 0x0000); | |
| 337 | ||
| 338 | return (uether_do_request(&sc->sc_ue, &req, NULL, 1000)); | |
| 339 | } | |
| 340 | #endif | |
| 341 | ||
| 342 | static int | |
| 343 | udav_csr_read(struct udav_softc *sc, uint16_t offset, void *buf, int len) | |
| 344 | { | |
| 345 | struct usb_device_request req; | |
| 346 | ||
| 347 | len &= 0xff; | |
| 348 | ||
| 349 | req.bmRequestType = UT_READ_VENDOR_DEVICE; | |
| 350 | req.bRequest = UDAV_REQ_REG_READ; | |
| 351 | USETW(req.wValue, 0x0000); | |
| 352 | USETW(req.wIndex, offset); | |
| 353 | USETW(req.wLength, len); | |
| 354 | ||
| 355 | return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); | |
| 356 | } | |
| 357 | ||
| 358 | static int | |
| 359 | udav_csr_write(struct udav_softc *sc, uint16_t offset, void *buf, int len) | |
| 360 | { | |
| 361 | struct usb_device_request req; | |
| 362 | ||
| 363 | offset &= 0xff; | |
| 364 | len &= 0xff; | |
| 365 | ||
| 366 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; | |
| 367 | req.bRequest = UDAV_REQ_REG_WRITE; | |
| 368 | USETW(req.wValue, 0x0000); | |
| 369 | USETW(req.wIndex, offset); | |
| 370 | USETW(req.wLength, len); | |
| 371 | ||
| 372 | return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); | |
| 373 | } | |
| 374 | ||
| 375 | static uint8_t | |
| 376 | udav_csr_read1(struct udav_softc *sc, uint16_t offset) | |
| 377 | { | |
| 378 | uint8_t val; | |
| 379 | ||
| 380 | udav_csr_read(sc, offset, &val, 1); | |
| 381 | return (val); | |
| 382 | } | |
| 383 | ||
| 384 | static int | |
| 385 | udav_csr_write1(struct udav_softc *sc, uint16_t offset, | |
| 386 | uint8_t ch) | |
| 387 | { | |
| 388 | struct usb_device_request req; | |
| 389 | ||
| 390 | offset &= 0xff; | |
| 391 | ||
| 392 | req.bmRequestType = UT_WRITE_VENDOR_DEVICE; | |
| 393 | req.bRequest = UDAV_REQ_REG_WRITE1; | |
| 394 | USETW(req.wValue, ch); | |
| 395 | USETW(req.wIndex, offset); | |
| 396 | USETW(req.wLength, 0x0000); | |
| 397 | ||
| 398 | return (uether_do_request(&sc->sc_ue, &req, NULL, 1000)); | |
| 399 | } | |
| 400 | ||
| 401 | static void | |
| 402 | udav_init(struct usb_ether *ue) | |
| 403 | { | |
| 404 | struct udav_softc *sc = ue->ue_sc; | |
| 405 | struct ifnet *ifp = uether_getifp(&sc->sc_ue); | |
| 406 | ||
| b946173a | 407 | UDAV_LOCK_ASSERT(sc); |
| 12bd3c8b SW |
408 | |
| 409 | /* | |
| 410 | * Cancel pending I/O | |
| 411 | */ | |
| 412 | udav_stop(ue); | |
| 413 | ||
| 414 | /* set MAC address */ | |
| 415 | udav_csr_write(sc, UDAV_PAR, IF_LLADDR(ifp), ETHER_ADDR_LEN); | |
| 416 | ||
| 417 | /* initialize network control register */ | |
| 418 | ||
| 419 | /* disable loopback */ | |
| 420 | UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1); | |
| 421 | ||
| 422 | /* Initialize RX control register */ | |
| 423 | UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC); | |
| 424 | ||
| 425 | /* load multicast filter and update promiscious mode bit */ | |
| 426 | udav_setpromisc(ue); | |
| 427 | ||
| 428 | /* enable RX */ | |
| 429 | UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN); | |
| 430 | ||
| 431 | /* clear POWER_DOWN state of internal PHY */ | |
| 432 | UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0); | |
| 433 | UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0); | |
| 434 | ||
| 435 | usbd_xfer_set_stall(sc->sc_xfer[UDAV_BULK_DT_WR]); | |
| 436 | ||
| b946173a SW |
437 | /* XXX ifp->if_drv_flags |= IFF_DRV_RUNNING; |
| 438 | */ | |
| 12bd3c8b SW |
439 | udav_start(ue); |
| 440 | } | |
| 441 | ||
| 442 | static void | |
| 443 | udav_reset(struct udav_softc *sc) | |
| 444 | { | |
| 445 | int i; | |
| 446 | ||
| 447 | /* Select PHY */ | |
| 448 | #if 1 | |
| 449 | /* | |
| 450 | * XXX: force select internal phy. | |
| 451 | * external phy routines are not tested. | |
| 452 | */ | |
| 453 | UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY); | |
| 454 | #else | |
| 455 | if (sc->sc_flags & UDAV_EXT_PHY) | |
| 456 | UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY); | |
| 457 | else | |
| 458 | UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY); | |
| 459 | #endif | |
| 460 | ||
| 461 | UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST); | |
| 462 | ||
| 463 | for (i = 0; i < UDAV_TX_TIMEOUT; i++) { | |
| 464 | if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST)) | |
| 465 | break; | |
| 466 | if (uether_pause(&sc->sc_ue, hz / 100)) | |
| 467 | break; | |
| 468 | } | |
| 469 | ||
| 470 | uether_pause(&sc->sc_ue, hz / 100); | |
| 471 | } | |
| 472 | ||
| 473 | #define UDAV_BITS 6 | |
| 474 | static void | |
| 475 | udav_setmulti(struct usb_ether *ue) | |
| 476 | { | |
| 477 | struct udav_softc *sc = ue->ue_sc; | |
| 478 | struct ifnet *ifp = uether_getifp(&sc->sc_ue); | |
| 479 | struct ifmultiaddr *ifma; | |
| 480 | uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; | |
| 481 | int h = 0; | |
| 482 | ||
| b946173a | 483 | UDAV_LOCK_ASSERT(sc); |
| 12bd3c8b SW |
484 | |
| 485 | if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { | |
| 486 | UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC); | |
| 487 | return; | |
| 488 | } | |
| 489 | ||
| 490 | /* first, zot all the existing hash bits */ | |
| 491 | memset(hashtbl, 0x00, sizeof(hashtbl)); | |
| 492 | hashtbl[7] |= 0x80; /* broadcast address */ | |
| 493 | udav_csr_write(sc, UDAV_MAR, hashtbl, sizeof(hashtbl)); | |
| 494 | ||
| 495 | /* now program new ones */ | |
| 12bd3c8b SW |
496 | TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) |
| 497 | { | |
| 498 | if (ifma->ifma_addr->sa_family != AF_LINK) | |
| 499 | continue; | |
| 500 | h = ether_crc32_be(LLADDR((struct sockaddr_dl *) | |
| 501 | ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; | |
| 502 | hashtbl[h / 8] |= 1 << (h % 8); | |
| 503 | } | |
| 12bd3c8b SW |
504 | |
| 505 | /* disable all multicast */ | |
| 506 | UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL); | |
| 507 | ||
| 508 | /* write hash value to the register */ | |
| 509 | udav_csr_write(sc, UDAV_MAR, hashtbl, sizeof(hashtbl)); | |
| 510 | } | |
| 511 | ||
| 512 | static void | |
| 513 | udav_setpromisc(struct usb_ether *ue) | |
| 514 | { | |
| 515 | struct udav_softc *sc = ue->ue_sc; | |
| 516 | struct ifnet *ifp = uether_getifp(&sc->sc_ue); | |
| 517 | uint8_t rxmode; | |
| 518 | ||
| 519 | rxmode = udav_csr_read1(sc, UDAV_RCR); | |
| 520 | rxmode &= ~(UDAV_RCR_ALL | UDAV_RCR_PRMSC); | |
| 521 | ||
| 522 | if (ifp->if_flags & IFF_PROMISC) | |
| 523 | rxmode |= UDAV_RCR_ALL | UDAV_RCR_PRMSC; | |
| 524 | else if (ifp->if_flags & IFF_ALLMULTI) | |
| 525 | rxmode |= UDAV_RCR_ALL; | |
| 526 | ||
| 527 | /* write new mode bits */ | |
| 528 | udav_csr_write1(sc, UDAV_RCR, rxmode); | |
| 529 | } | |
| 530 | ||
| 531 | static void | |
| 532 | udav_start(struct usb_ether *ue) | |
| 533 | { | |
| 534 | struct udav_softc *sc = ue->ue_sc; | |
| 535 | ||
| 536 | /* | |
| 537 | * start the USB transfers, if not already started: | |
| 538 | */ | |
| 539 | usbd_transfer_start(sc->sc_xfer[UDAV_INTR_DT_RD]); | |
| 540 | usbd_transfer_start(sc->sc_xfer[UDAV_BULK_DT_RD]); | |
| 541 | usbd_transfer_start(sc->sc_xfer[UDAV_BULK_DT_WR]); | |
| 542 | } | |
| 543 | ||
| 544 | static void | |
| 545 | udav_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) | |
| 546 | { | |
| 547 | struct udav_softc *sc = usbd_xfer_softc(xfer); | |
| 548 | struct ifnet *ifp = uether_getifp(&sc->sc_ue); | |
| 549 | struct usb_page_cache *pc; | |
| 550 | struct mbuf *m; | |
| 551 | int extra_len; | |
| 552 | int temp_len; | |
| 553 | uint8_t buf[2]; | |
| 554 | ||
| 555 | switch (USB_GET_STATE(xfer)) { | |
| 556 | case USB_ST_TRANSFERRED: | |
| 557 | DPRINTFN(11, "transfer complete\n"); | |
| 558 | ifp->if_opackets++; | |
| 559 | ||
| 560 | /* FALLTHROUGH */ | |
| 561 | case USB_ST_SETUP: | |
| 562 | tr_setup: | |
| 563 | if ((sc->sc_flags & UDAV_FLAG_LINK) == 0) { | |
| 564 | /* | |
| 565 | * don't send anything if there is no link ! | |
| 566 | */ | |
| 567 | return; | |
| 568 | } | |
| b946173a | 569 | m = ifq_dequeue(&ifp->if_snd, NULL); |
| 12bd3c8b SW |
570 | |
| 571 | if (m == NULL) | |
| 572 | return; | |
| 573 | if (m->m_pkthdr.len > MCLBYTES) | |
| 574 | m->m_pkthdr.len = MCLBYTES; | |
| 575 | if (m->m_pkthdr.len < UDAV_MIN_FRAME_LEN) { | |
| 576 | extra_len = UDAV_MIN_FRAME_LEN - m->m_pkthdr.len; | |
| 577 | } else { | |
| 578 | extra_len = 0; | |
| 579 | } | |
| 580 | ||
| 581 | temp_len = (m->m_pkthdr.len + extra_len); | |
| 582 | ||
| 583 | /* | |
| 584 | * the frame length is specified in the first 2 bytes of the | |
| 585 | * buffer | |
| 586 | */ | |
| 587 | buf[0] = (uint8_t)(temp_len); | |
| 588 | buf[1] = (uint8_t)(temp_len >> 8); | |
| 589 | ||
| 590 | temp_len += 2; | |
| 591 | ||
| 592 | pc = usbd_xfer_get_frame(xfer, 0); | |
| 593 | usbd_copy_in(pc, 0, buf, 2); | |
| 594 | usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len); | |
| 595 | ||
| 596 | if (extra_len) | |
| 597 | usbd_frame_zero(pc, temp_len - extra_len, extra_len); | |
| 598 | /* | |
| 599 | * if there's a BPF listener, bounce a copy | |
| 600 | * of this frame to him: | |
| 601 | */ | |
| 602 | BPF_MTAP(ifp, m); | |
| 603 | ||
| 604 | m_freem(m); | |
| 605 | ||
| 606 | usbd_xfer_set_frame_len(xfer, 0, temp_len); | |
| 607 | usbd_transfer_submit(xfer); | |
| 608 | return; | |
| 609 | ||
| 610 | default: /* Error */ | |
| 611 | DPRINTFN(11, "transfer error, %s\n", | |
| 612 | usbd_errstr(error)); | |
| 613 | ||
| 614 | ifp->if_oerrors++; | |
| 615 | ||
| 616 | if (error != USB_ERR_CANCELLED) { | |
| 617 | /* try to clear stall first */ | |
| 618 | usbd_xfer_set_stall(xfer); | |
| 619 | goto tr_setup; | |
| 620 | } | |
| 621 | return; | |
| 622 | } | |
| 623 | } | |
| 624 | ||
| 625 | static void | |
| 626 | udav_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) | |
| 627 | { | |
| 628 | struct udav_softc *sc = usbd_xfer_softc(xfer); | |
| 629 | struct usb_ether *ue = &sc->sc_ue; | |
| 630 | struct ifnet *ifp = uether_getifp(ue); | |
| 631 | struct usb_page_cache *pc; | |
| 632 | struct udav_rxpkt stat; | |
| 633 | int len; | |
| 634 | int actlen; | |
| 635 | ||
| 636 | usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); | |
| 637 | ||
| 638 | switch (USB_GET_STATE(xfer)) { | |
| 639 | case USB_ST_TRANSFERRED: | |
| 640 | ||
| 641 | if (actlen < sizeof(stat) + ETHER_CRC_LEN) { | |
| 642 | ifp->if_ierrors++; | |
| 643 | goto tr_setup; | |
| 644 | } | |
| 645 | pc = usbd_xfer_get_frame(xfer, 0); | |
| 646 | usbd_copy_out(pc, 0, &stat, sizeof(stat)); | |
| 647 | actlen -= sizeof(stat); | |
| 648 | len = min(actlen, le16toh(stat.pktlen)); | |
| 649 | len -= ETHER_CRC_LEN; | |
| 650 | ||
| 651 | if (stat.rxstat & UDAV_RSR_LCS) { | |
| 652 | ifp->if_collisions++; | |
| 653 | goto tr_setup; | |
| 654 | } | |
| 655 | if (stat.rxstat & UDAV_RSR_ERR) { | |
| 656 | ifp->if_ierrors++; | |
| 657 | goto tr_setup; | |
| 658 | } | |
| 659 | uether_rxbuf(ue, pc, sizeof(stat), len); | |
| 660 | /* FALLTHROUGH */ | |
| 661 | case USB_ST_SETUP: | |
| 662 | tr_setup: | |
| 663 | usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); | |
| 664 | usbd_transfer_submit(xfer); | |
| 665 | uether_rxflush(ue); | |
| 666 | return; | |
| 667 | ||
| 668 | default: /* Error */ | |
| 669 | DPRINTF("bulk read error, %s\n", | |
| 670 | usbd_errstr(error)); | |
| 671 | ||
| 672 | if (error != USB_ERR_CANCELLED) { | |
| 673 | /* try to clear stall first */ | |
| 674 | usbd_xfer_set_stall(xfer); | |
| 675 | goto tr_setup; | |
| 676 | } | |
| 677 | return; | |
| 678 | } | |
| 679 | } | |
| 680 | ||
| 681 | static void | |
| 682 | udav_intr_callback(struct usb_xfer *xfer, usb_error_t error) | |
| 683 | { | |
| 684 | switch (USB_GET_STATE(xfer)) { | |
| 685 | case USB_ST_TRANSFERRED: | |
| 686 | case USB_ST_SETUP: | |
| 687 | tr_setup: | |
| 688 | usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); | |
| 689 | usbd_transfer_submit(xfer); | |
| 690 | return; | |
| 691 | ||
| 692 | default: /* Error */ | |
| 693 | if (error != USB_ERR_CANCELLED) { | |
| 694 | /* try to clear stall first */ | |
| 695 | usbd_xfer_set_stall(xfer); | |
| 696 | goto tr_setup; | |
| 697 | } | |
| 698 | return; | |
| 699 | } | |
| 700 | } | |
| 701 | ||
| 702 | static void | |
| 703 | udav_stop(struct usb_ether *ue) | |
| 704 | { | |
| 705 | struct udav_softc *sc = ue->ue_sc; | |
| cb652d71 | 706 | #if 0 /* XXX */ |
| 12bd3c8b | 707 | struct ifnet *ifp = uether_getifp(&sc->sc_ue); |
| cb652d71 | 708 | #endif |
| 12bd3c8b | 709 | |
| b946173a | 710 | UDAV_LOCK_ASSERT(sc); |
| 12bd3c8b | 711 | |
| cb652d71 | 712 | #if 0 /* XXX */ |
| 12bd3c8b | 713 | ifp->if_drv_flags &= ~IFF_DRV_RUNNING; |
| cb652d71 | 714 | #endif |
| 12bd3c8b SW |
715 | sc->sc_flags &= ~UDAV_FLAG_LINK; |
| 716 | ||
| 717 | /* | |
| 718 | * stop all the transfers, if not already stopped: | |
| 719 | */ | |
| 720 | usbd_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_WR]); | |
| 721 | usbd_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_RD]); | |
| 722 | usbd_transfer_stop(sc->sc_xfer[UDAV_INTR_DT_RD]); | |
| 723 | ||
| 724 | udav_reset(sc); | |
| 725 | } | |
| 726 | ||
| 727 | static int | |
| 728 | udav_ifmedia_upd(struct ifnet *ifp) | |
| 729 | { | |
| 730 | struct udav_softc *sc = ifp->if_softc; | |
| 731 | struct mii_data *mii = GET_MII(sc); | |
| 732 | struct mii_softc *miisc; | |
| 733 | ||
| b946173a SW |
734 | kprintf("ifmedia upd\n"); |
| 735 | ||
| 736 | UDAV_LOCK_ASSERT(sc); | |
| 12bd3c8b SW |
737 | |
| 738 | sc->sc_flags &= ~UDAV_FLAG_LINK; | |
| 739 | LIST_FOREACH(miisc, &mii->mii_phys, mii_list) | |
| b946173a | 740 | mii_phy_reset(miisc); |
| 12bd3c8b SW |
741 | mii_mediachg(mii); |
| 742 | return (0); | |
| 743 | } | |
| 744 | ||
| 745 | static void | |
| 746 | udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr) | |
| 747 | { | |
| 748 | struct udav_softc *sc = ifp->if_softc; | |
| 749 | struct mii_data *mii = GET_MII(sc); | |
| 750 | ||
| b946173a SW |
751 | kprintf("ifmedia status\n"); |
| 752 | ||
| 12bd3c8b SW |
753 | UDAV_LOCK(sc); |
| 754 | mii_pollstat(mii); | |
| 755 | ifmr->ifm_active = mii->mii_media_active; | |
| 756 | ifmr->ifm_status = mii->mii_media_status; | |
| 757 | UDAV_UNLOCK(sc); | |
| 758 | } | |
| 759 | ||
| 760 | static void | |
| 761 | udav_tick(struct usb_ether *ue) | |
| 762 | { | |
| 763 | struct udav_softc *sc = ue->ue_sc; | |
| 764 | struct mii_data *mii = GET_MII(sc); | |
| 765 | ||
| b946173a | 766 | UDAV_LOCK_ASSERT(sc); |
| 12bd3c8b SW |
767 | |
| 768 | mii_tick(mii); | |
| 769 | if ((sc->sc_flags & UDAV_FLAG_LINK) == 0 | |
| 770 | && mii->mii_media_status & IFM_ACTIVE && | |
| 771 | IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { | |
| 772 | sc->sc_flags |= UDAV_FLAG_LINK; | |
| 773 | udav_start(ue); | |
| 774 | } | |
| 775 | } | |
| 776 | ||
| 777 | static int | |
| 778 | udav_miibus_readreg(device_t dev, int phy, int reg) | |
| 779 | { | |
| 780 | struct udav_softc *sc = device_get_softc(dev); | |
| 781 | uint16_t data16; | |
| 782 | uint8_t val[2]; | |
| 783 | int locked; | |
| 784 | ||
| 785 | /* XXX: one PHY only for the internal PHY */ | |
| 786 | if (phy != 0) | |
| 787 | return (0); | |
| 788 | ||
| b946173a | 789 | locked = lockowned(&sc->sc_lock); |
| 12bd3c8b SW |
790 | if (!locked) |
| 791 | UDAV_LOCK(sc); | |
| 792 | ||
| 793 | /* select internal PHY and set PHY register address */ | |
| 794 | udav_csr_write1(sc, UDAV_EPAR, | |
| 795 | UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK)); | |
| 796 | ||
| 797 | /* select PHY operation and start read command */ | |
| 798 | udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR); | |
| 799 | ||
| 800 | /* XXX: should we wait? */ | |
| 801 | ||
| 802 | /* end read command */ | |
| 803 | UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR); | |
| 804 | ||
| 805 | /* retrieve the result from data registers */ | |
| 806 | udav_csr_read(sc, UDAV_EPDRL, val, 2); | |
| 807 | ||
| 808 | data16 = (val[0] | (val[1] << 8)); | |
| 809 | ||
| 810 | DPRINTFN(11, "phy=%d reg=0x%04x => 0x%04x\n", | |
| 811 | phy, reg, data16); | |
| 812 | ||
| 813 | if (!locked) | |
| 814 | UDAV_UNLOCK(sc); | |
| 815 | return (data16); | |
| 816 | } | |
| 817 | ||
| 818 | static int | |
| 819 | udav_miibus_writereg(device_t dev, int phy, int reg, int data) | |
| 820 | { | |
| 821 | struct udav_softc *sc = device_get_softc(dev); | |
| 822 | uint8_t val[2]; | |
| 823 | int locked; | |
| 824 | ||
| 825 | /* XXX: one PHY only for the internal PHY */ | |
| 826 | if (phy != 0) | |
| 827 | return (0); | |
| 828 | ||
| b946173a | 829 | locked = lockowned(&sc->sc_lock); |
| 12bd3c8b SW |
830 | if (!locked) |
| 831 | UDAV_LOCK(sc); | |
| 832 | ||
| 833 | /* select internal PHY and set PHY register address */ | |
| 834 | udav_csr_write1(sc, UDAV_EPAR, | |
| 835 | UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK)); | |
| 836 | ||
| 837 | /* put the value to the data registers */ | |
| 838 | val[0] = (data & 0xff); | |
| 839 | val[1] = (data >> 8) & 0xff; | |
| 840 | udav_csr_write(sc, UDAV_EPDRL, val, 2); | |
| 841 | ||
| 842 | /* select PHY operation and start write command */ | |
| 843 | udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW); | |
| 844 | ||
| 845 | /* XXX: should we wait? */ | |
| 846 | ||
| 847 | /* end write command */ | |
| 848 | UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW); | |
| 849 | ||
| 850 | if (!locked) | |
| 851 | UDAV_UNLOCK(sc); | |
| 852 | return (0); | |
| 853 | } | |
| 854 | ||
| 855 | static void | |
| 856 | udav_miibus_statchg(device_t dev) | |
| 857 | { | |
| 858 | /* nothing to do */ | |
| 859 | } |