1 1.99 riastrad /* $NetBSD: if_udav.c,v 1.99 2022/08/20 14:09:20 riastradh Exp $ */ 2 1.1 itojun /* $nabe: if_udav.c,v 1.3 2003/08/21 16:57:19 nabe Exp $ */ 3 1.37 mrg 4 1.1 itojun /* 5 1.1 itojun * Copyright (c) 2003 6 1.1 itojun * Shingo WATANABE <nabe (at) nabechan.org>. All rights reserved. 7 1.1 itojun * 8 1.1 itojun * Redistribution and use in source and binary forms, with or without 9 1.1 itojun * modification, are permitted provided that the following conditions 10 1.1 itojun * are met: 11 1.1 itojun * 1. Redistributions of source code must retain the above copyright 12 1.1 itojun * notice, this list of conditions and the following disclaimer. 13 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 itojun * notice, this list of conditions and the following disclaimer in the 15 1.1 itojun * documentation and/or other materials provided with the distribution. 16 1.2 tsutsui * 3. Neither the name of the author nor the names of any co-contributors 17 1.1 itojun * may be used to endorse or promote products derived from this software 18 1.1 itojun * without specific prior written permission. 19 1.1 itojun * 20 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 1.1 itojun * SUCH DAMAGE. 31 1.1 itojun * 32 1.1 itojun */ 33 1.1 itojun 34 1.1 itojun /* 35 1.1 itojun * DM9601(DAVICOM USB to Ethernet MAC Controller with Integrated 10/100 PHY) 36 1.1 itojun * The spec can be found at the following url. 37 1.1 itojun * http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-F01-062202s.pdf 38 1.1 itojun */ 39 1.1 itojun 40 1.1 itojun /* 41 1.1 itojun * TODO: 42 1.1 itojun * Interrupt Endpoint support 43 1.1 itojun * External PHYs 44 1.1 itojun * powerhook() support? 45 1.1 itojun */ 46 1.1 itojun 47 1.1 itojun #include <sys/cdefs.h> 48 1.99 riastrad __KERNEL_RCSID(0, "$NetBSD: if_udav.c,v 1.99 2022/08/20 14:09:20 riastradh Exp $"); 49 1.1 itojun 50 1.40 christos #ifdef _KERNEL_OPT 51 1.49 skrll #include "opt_usb.h" 52 1.40 christos #endif 53 1.1 itojun 54 1.1 itojun #include <sys/param.h> 55 1.1 itojun 56 1.61 skrll #include <dev/usb/usbnet.h> 57 1.1 itojun #include <dev/usb/if_udavreg.h> 58 1.1 itojun 59 1.1 itojun /* Function declarations */ 60 1.73 maxv static int udav_match(device_t, cfdata_t, void *); 61 1.73 maxv static void udav_attach(device_t, device_t, void *); 62 1.57 mrg 63 1.64 mrg CFATTACH_DECL_NEW(udav, sizeof(struct usbnet), udav_match, udav_attach, 64 1.61 skrll usbnet_detach, usbnet_activate); 65 1.61 skrll 66 1.61 skrll static void udav_chip_init(struct usbnet *); 67 1.1 itojun 68 1.77 thorpej static unsigned udav_uno_tx_prepare(struct usbnet *, struct mbuf *, 69 1.77 thorpej struct usbnet_chain *); 70 1.77 thorpej static void udav_uno_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t); 71 1.77 thorpej static void udav_uno_stop(struct ifnet *, int); 72 1.81 riastrad static void udav_uno_mcast(struct ifnet *); 73 1.77 thorpej static int udav_uno_mii_read_reg(struct usbnet *, int, int, uint16_t *); 74 1.77 thorpej static int udav_uno_mii_write_reg(struct usbnet *, int, int, uint16_t); 75 1.77 thorpej static void udav_uno_mii_statchg(struct ifnet *); 76 1.77 thorpej static int udav_uno_init(struct ifnet *); 77 1.61 skrll static void udav_reset(struct usbnet *); 78 1.61 skrll 79 1.64 mrg static int udav_csr_read(struct usbnet *, int, void *, int); 80 1.64 mrg static int udav_csr_write(struct usbnet *, int, void *, int); 81 1.64 mrg static int udav_csr_read1(struct usbnet *, int); 82 1.64 mrg static int udav_csr_write1(struct usbnet *, int, unsigned char); 83 1.1 itojun 84 1.1 itojun #if 0 85 1.64 mrg static int udav_mem_read(struct usbnet *, int, void *, int); 86 1.64 mrg static int udav_mem_write(struct usbnet *, int, void *, int); 87 1.64 mrg static int udav_mem_write1(struct usbnet *, int, unsigned char); 88 1.1 itojun #endif 89 1.1 itojun 90 1.1 itojun /* Macros */ 91 1.1 itojun #ifdef UDAV_DEBUG 92 1.31 dyoung #define DPRINTF(x) if (udavdebug) printf x 93 1.58 msaitoh #define DPRINTFN(n, x) if (udavdebug >= (n)) printf x 94 1.69 mrg int udavdebug = 0; 95 1.1 itojun #else 96 1.1 itojun #define DPRINTF(x) 97 1.58 msaitoh #define DPRINTFN(n, x) 98 1.1 itojun #endif 99 1.1 itojun 100 1.64 mrg #define UDAV_SETBIT(un, reg, x) \ 101 1.64 mrg udav_csr_write1(un, reg, udav_csr_read1(un, reg) | (x)) 102 1.1 itojun 103 1.64 mrg #define UDAV_CLRBIT(un, reg, x) \ 104 1.64 mrg udav_csr_write1(un, reg, udav_csr_read1(un, reg) & ~(x)) 105 1.1 itojun 106 1.1 itojun static const struct udav_type { 107 1.1 itojun struct usb_devno udav_dev; 108 1.46 skrll uint16_t udav_flags; 109 1.1 itojun #define UDAV_EXT_PHY 0x0001 110 1.42 jakllsch #define UDAV_NO_PHY 0x0002 111 1.1 itojun } udav_devs [] = { 112 1.1 itojun /* Corega USB-TXC */ 113 1.1 itojun {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC }, 0}, 114 1.18 xtraeme /* ShanTou ST268 USB NIC */ 115 1.18 xtraeme {{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268_USB_NIC }, 0}, 116 1.22 drochner /* ShanTou ADM8515 */ 117 1.22 drochner {{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ADM8515 }, 0}, 118 1.30 jmcneill /* SUNRISING SR9600 */ 119 1.30 jmcneill {{ USB_VENDOR_SUNRISING, USB_PRODUCT_SUNRISING_SR9600 }, 0 }, 120 1.42 jakllsch /* SUNRISING QF9700 */ 121 1.42 jakllsch {{ USB_VENDOR_SUNRISING, USB_PRODUCT_SUNRISING_QF9700 }, UDAV_NO_PHY }, 122 1.35 mbalmer /* QUAN DM9601 */ 123 1.35 mbalmer {{USB_VENDOR_QUAN, USB_PRODUCT_QUAN_DM9601 }, 0}, 124 1.1 itojun #if 0 125 1.1 itojun /* DAVICOM DM9601 Generic? */ 126 1.1 itojun /* XXX: The following ids was obtained from the data sheet. */ 127 1.1 itojun {{ 0x0a46, 0x9601 }, 0}, 128 1.1 itojun #endif 129 1.1 itojun }; 130 1.7 christos #define udav_lookup(v, p) ((const struct udav_type *)usb_lookup(udav_devs, v, p)) 131 1.1 itojun 132 1.73 maxv static const struct usbnet_ops udav_ops = { 133 1.77 thorpej .uno_stop = udav_uno_stop, 134 1.81 riastrad .uno_mcast = udav_uno_mcast, 135 1.77 thorpej .uno_read_reg = udav_uno_mii_read_reg, 136 1.77 thorpej .uno_write_reg = udav_uno_mii_write_reg, 137 1.77 thorpej .uno_statchg = udav_uno_mii_statchg, 138 1.77 thorpej .uno_tx_prepare = udav_uno_tx_prepare, 139 1.77 thorpej .uno_rx_loop = udav_uno_rx_loop, 140 1.77 thorpej .uno_init = udav_uno_init, 141 1.63 mrg }; 142 1.1 itojun 143 1.1 itojun /* Probe */ 144 1.73 maxv static int 145 1.31 dyoung udav_match(device_t parent, cfdata_t match, void *aux) 146 1.1 itojun { 147 1.31 dyoung struct usb_attach_arg *uaa = aux; 148 1.1 itojun 149 1.46 skrll return udav_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ? 150 1.46 skrll UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 151 1.1 itojun } 152 1.1 itojun 153 1.1 itojun /* Attach */ 154 1.73 maxv static void 155 1.31 dyoung udav_attach(device_t parent, device_t self, void *aux) 156 1.1 itojun { 157 1.71 mrg USBNET_MII_DECL_DEFAULT(unm); 158 1.70 mrg struct usbnet_mii *unmp; 159 1.64 mrg struct usbnet * const un = device_private(self); 160 1.31 dyoung struct usb_attach_arg *uaa = aux; 161 1.46 skrll struct usbd_device *dev = uaa->uaa_device; 162 1.46 skrll struct usbd_interface *iface; 163 1.1 itojun usbd_status err; 164 1.1 itojun usb_interface_descriptor_t *id; 165 1.1 itojun usb_endpoint_descriptor_t *ed; 166 1.6 augustss char *devinfop; 167 1.61 skrll int i; 168 1.1 itojun 169 1.26 plunky aprint_naive("\n"); 170 1.26 plunky aprint_normal("\n"); 171 1.6 augustss devinfop = usbd_devinfo_alloc(dev, 0); 172 1.24 cube aprint_normal_dev(self, "%s\n", devinfop); 173 1.6 augustss usbd_devinfo_free(devinfop); 174 1.1 itojun 175 1.61 skrll un->un_dev = self; 176 1.61 skrll un->un_udev = dev; 177 1.64 mrg un->un_sc = un; 178 1.63 mrg un->un_ops = &udav_ops; 179 1.65 mrg un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; 180 1.65 mrg un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; 181 1.65 mrg un->un_rx_list_cnt = UDAV_RX_LIST_CNT; 182 1.65 mrg un->un_tx_list_cnt = UDAV_TX_LIST_CNT; 183 1.65 mrg un->un_rx_bufsz = UDAV_BUFSZ; 184 1.65 mrg un->un_tx_bufsz = UDAV_BUFSZ; 185 1.61 skrll 186 1.1 itojun /* Move the device into the configured state. */ 187 1.22 drochner err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1); /* idx 0 */ 188 1.1 itojun if (err) { 189 1.39 skrll aprint_error_dev(self, "failed to set configuration" 190 1.39 skrll ", err=%s\n", usbd_errstr(err)); 191 1.61 skrll return; 192 1.1 itojun } 193 1.1 itojun 194 1.1 itojun /* get control interface */ 195 1.1 itojun err = usbd_device2interface_handle(dev, UDAV_IFACE_INDEX, &iface); 196 1.1 itojun if (err) { 197 1.24 cube aprint_error_dev(self, "failed to get interface, err=%s\n", 198 1.1 itojun usbd_errstr(err)); 199 1.61 skrll return; 200 1.1 itojun } 201 1.1 itojun 202 1.61 skrll un->un_iface = iface; 203 1.64 mrg un->un_flags = udav_lookup(uaa->uaa_vendor, 204 1.48 msaitoh uaa->uaa_product)->udav_flags; 205 1.1 itojun 206 1.1 itojun /* get interface descriptor */ 207 1.61 skrll id = usbd_get_interface_descriptor(un->un_iface); 208 1.1 itojun 209 1.1 itojun /* find endpoints */ 210 1.61 skrll un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] = 211 1.61 skrll un->un_ed[USBNET_ENDPT_INTR] = -1; 212 1.1 itojun for (i = 0; i < id->bNumEndpoints; i++) { 213 1.61 skrll ed = usbd_interface2endpoint_descriptor(un->un_iface, i); 214 1.1 itojun if (ed == NULL) { 215 1.24 cube aprint_error_dev(self, "couldn't get endpoint %d\n", i); 216 1.61 skrll return; 217 1.1 itojun } 218 1.1 itojun if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK && 219 1.1 itojun UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) 220 1.61 skrll un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; 221 1.1 itojun else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK && 222 1.1 itojun UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT) 223 1.61 skrll un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; 224 1.1 itojun else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT && 225 1.1 itojun UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) 226 1.61 skrll un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress; 227 1.1 itojun } 228 1.1 itojun 229 1.62 skrll if (un->un_ed[USBNET_ENDPT_RX] == 0 || 230 1.62 skrll un->un_ed[USBNET_ENDPT_TX] == 0 || 231 1.62 skrll un->un_ed[USBNET_ENDPT_INTR] == 0) { 232 1.24 cube aprint_error_dev(self, "missing endpoint\n"); 233 1.61 skrll return; 234 1.1 itojun } 235 1.1 itojun 236 1.61 skrll /* Not supported yet. */ 237 1.61 skrll un->un_ed[USBNET_ENDPT_INTR] = 0; 238 1.61 skrll 239 1.97 riastrad usbnet_attach(un); 240 1.78 skrll 241 1.61 skrll // /* reset the adapter */ 242 1.61 skrll // udav_reset(un); 243 1.1 itojun 244 1.1 itojun /* Get Ethernet Address */ 245 1.64 mrg err = udav_csr_read(un, UDAV_PAR, un->un_eaddr, ETHER_ADDR_LEN); 246 1.1 itojun if (err) { 247 1.24 cube aprint_error_dev(self, "read MAC address failed\n"); 248 1.61 skrll return; 249 1.1 itojun } 250 1.1 itojun 251 1.70 mrg if (ISSET(un->un_flags, UDAV_NO_PHY)) 252 1.70 mrg unmp = NULL; 253 1.70 mrg else 254 1.70 mrg unmp = &unm; 255 1.1 itojun 256 1.9 wiz /* initialize interface information */ 257 1.70 mrg usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, 258 1.70 mrg 0, unmp); 259 1.42 jakllsch 260 1.31 dyoung return; 261 1.1 itojun } 262 1.1 itojun 263 1.1 itojun #if 0 264 1.1 itojun /* read memory */ 265 1.61 skrll static int 266 1.64 mrg udav_mem_read(struct usbnet *un, int offset, void *buf, int len) 267 1.1 itojun { 268 1.1 itojun usb_device_request_t req; 269 1.1 itojun usbd_status err; 270 1.1 itojun 271 1.1 itojun DPRINTFN(0x200, 272 1.61 skrll ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 273 1.1 itojun 274 1.63 mrg if (usbnet_isdying(un)) 275 1.46 skrll return 0; 276 1.1 itojun 277 1.1 itojun offset &= 0xffff; 278 1.1 itojun len &= 0xff; 279 1.1 itojun 280 1.1 itojun req.bmRequestType = UT_READ_VENDOR_DEVICE; 281 1.1 itojun req.bRequest = UDAV_REQ_MEM_READ; 282 1.1 itojun USETW(req.wValue, 0x0000); 283 1.1 itojun USETW(req.wIndex, offset); 284 1.1 itojun USETW(req.wLength, len); 285 1.1 itojun 286 1.61 skrll err = usbd_do_request(un->un_udev, &req, buf); 287 1.1 itojun if (err) { 288 1.1 itojun DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n", 289 1.61 skrll device_xname(un->un_dev), __func__, offset, err)); 290 1.1 itojun } 291 1.1 itojun 292 1.46 skrll return err; 293 1.1 itojun } 294 1.1 itojun 295 1.1 itojun /* write memory */ 296 1.61 skrll static int 297 1.64 mrg udav_mem_write(struct usbnet *un, int offset, void *buf, int len) 298 1.1 itojun { 299 1.1 itojun usb_device_request_t req; 300 1.1 itojun usbd_status err; 301 1.1 itojun 302 1.1 itojun DPRINTFN(0x200, 303 1.61 skrll ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 304 1.1 itojun 305 1.63 mrg if (usbnet_isdying(un)) 306 1.46 skrll return 0; 307 1.1 itojun 308 1.1 itojun offset &= 0xffff; 309 1.1 itojun len &= 0xff; 310 1.1 itojun 311 1.1 itojun req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 312 1.1 itojun req.bRequest = UDAV_REQ_MEM_WRITE; 313 1.1 itojun USETW(req.wValue, 0x0000); 314 1.1 itojun USETW(req.wIndex, offset); 315 1.1 itojun USETW(req.wLength, len); 316 1.1 itojun 317 1.61 skrll err = usbd_do_request(un->un_udev, &req, buf); 318 1.1 itojun if (err) { 319 1.1 itojun DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n", 320 1.61 skrll device_xname(un->un_dev), __func__, offset, err)); 321 1.1 itojun } 322 1.1 itojun 323 1.46 skrll return err; 324 1.1 itojun } 325 1.1 itojun 326 1.1 itojun /* write memory */ 327 1.61 skrll static int 328 1.64 mrg udav_mem_write1(struct usbnet *un, int offset, unsigned char ch) 329 1.1 itojun { 330 1.1 itojun usb_device_request_t req; 331 1.1 itojun usbd_status err; 332 1.1 itojun 333 1.1 itojun DPRINTFN(0x200, 334 1.61 skrll ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 335 1.1 itojun 336 1.63 mrg if (usbnet_isdying(un)) 337 1.46 skrll return 0; 338 1.1 itojun 339 1.1 itojun offset &= 0xffff; 340 1.1 itojun 341 1.1 itojun req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 342 1.1 itojun req.bRequest = UDAV_REQ_MEM_WRITE1; 343 1.1 itojun USETW(req.wValue, ch); 344 1.1 itojun USETW(req.wIndex, offset); 345 1.1 itojun USETW(req.wLength, 0x0000); 346 1.1 itojun 347 1.61 skrll err = usbd_do_request(un->un_udev, &req, NULL); 348 1.1 itojun if (err) { 349 1.1 itojun DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n", 350 1.61 skrll device_xname(un->un_dev), __func__, offset, err)); 351 1.1 itojun } 352 1.1 itojun 353 1.46 skrll return err; 354 1.1 itojun } 355 1.1 itojun #endif 356 1.1 itojun 357 1.1 itojun /* read register(s) */ 358 1.61 skrll static int 359 1.64 mrg udav_csr_read(struct usbnet *un, int offset, void *buf, int len) 360 1.1 itojun { 361 1.1 itojun usb_device_request_t req; 362 1.1 itojun usbd_status err; 363 1.1 itojun 364 1.90 riastrad if (usbnet_isdying(un)) 365 1.90 riastrad return USBD_IOERROR; 366 1.1 itojun 367 1.1 itojun DPRINTFN(0x200, 368 1.61 skrll ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 369 1.1 itojun 370 1.1 itojun offset &= 0xff; 371 1.1 itojun len &= 0xff; 372 1.1 itojun 373 1.1 itojun req.bmRequestType = UT_READ_VENDOR_DEVICE; 374 1.1 itojun req.bRequest = UDAV_REQ_REG_READ; 375 1.1 itojun USETW(req.wValue, 0x0000); 376 1.1 itojun USETW(req.wIndex, offset); 377 1.1 itojun USETW(req.wLength, len); 378 1.1 itojun 379 1.61 skrll err = usbd_do_request(un->un_udev, &req, buf); 380 1.1 itojun if (err) { 381 1.1 itojun DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n", 382 1.61 skrll device_xname(un->un_dev), __func__, offset, err)); 383 1.91 riastrad memset(buf, 0, len); 384 1.1 itojun } 385 1.1 itojun 386 1.46 skrll return err; 387 1.1 itojun } 388 1.1 itojun 389 1.1 itojun /* write register(s) */ 390 1.61 skrll static int 391 1.64 mrg udav_csr_write(struct usbnet *un, int offset, void *buf, int len) 392 1.1 itojun { 393 1.1 itojun usb_device_request_t req; 394 1.1 itojun usbd_status err; 395 1.1 itojun 396 1.90 riastrad if (usbnet_isdying(un)) 397 1.90 riastrad return USBD_IOERROR; 398 1.1 itojun 399 1.1 itojun DPRINTFN(0x200, 400 1.61 skrll ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 401 1.1 itojun 402 1.1 itojun offset &= 0xff; 403 1.1 itojun len &= 0xff; 404 1.1 itojun 405 1.1 itojun req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 406 1.1 itojun req.bRequest = UDAV_REQ_REG_WRITE; 407 1.1 itojun USETW(req.wValue, 0x0000); 408 1.1 itojun USETW(req.wIndex, offset); 409 1.1 itojun USETW(req.wLength, len); 410 1.1 itojun 411 1.61 skrll err = usbd_do_request(un->un_udev, &req, buf); 412 1.1 itojun if (err) { 413 1.1 itojun DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n", 414 1.61 skrll device_xname(un->un_dev), __func__, offset, err)); 415 1.1 itojun } 416 1.1 itojun 417 1.46 skrll return err; 418 1.1 itojun } 419 1.1 itojun 420 1.61 skrll static int 421 1.64 mrg udav_csr_read1(struct usbnet *un, int offset) 422 1.1 itojun { 423 1.46 skrll uint8_t val = 0; 424 1.5 perry 425 1.1 itojun DPRINTFN(0x200, 426 1.61 skrll ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 427 1.1 itojun 428 1.63 mrg if (usbnet_isdying(un)) 429 1.46 skrll return 0; 430 1.1 itojun 431 1.64 mrg return udav_csr_read(un, offset, &val, 1) ? 0 : val; 432 1.1 itojun } 433 1.1 itojun 434 1.1 itojun /* write a register */ 435 1.61 skrll static int 436 1.64 mrg udav_csr_write1(struct usbnet *un, int offset, unsigned char ch) 437 1.1 itojun { 438 1.1 itojun usb_device_request_t req; 439 1.1 itojun usbd_status err; 440 1.1 itojun 441 1.90 riastrad if (usbnet_isdying(un)) 442 1.90 riastrad return USBD_IOERROR; 443 1.1 itojun 444 1.1 itojun DPRINTFN(0x200, 445 1.61 skrll ("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 446 1.1 itojun 447 1.1 itojun offset &= 0xff; 448 1.1 itojun 449 1.1 itojun req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 450 1.1 itojun req.bRequest = UDAV_REQ_REG_WRITE1; 451 1.1 itojun USETW(req.wValue, ch); 452 1.1 itojun USETW(req.wIndex, offset); 453 1.1 itojun USETW(req.wLength, 0x0000); 454 1.1 itojun 455 1.61 skrll err = usbd_do_request(un->un_udev, &req, NULL); 456 1.1 itojun if (err) { 457 1.1 itojun DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n", 458 1.61 skrll device_xname(un->un_dev), __func__, offset, err)); 459 1.1 itojun } 460 1.1 itojun 461 1.46 skrll return err; 462 1.1 itojun } 463 1.1 itojun 464 1.61 skrll static int 465 1.77 thorpej udav_uno_init(struct ifnet *ifp) 466 1.1 itojun { 467 1.61 skrll struct usbnet * const un = ifp->if_softc; 468 1.61 skrll struct mii_data * const mii = usbnet_mii(un); 469 1.20 dyoung uint8_t eaddr[ETHER_ADDR_LEN]; 470 1.67 mrg int rc = 0; 471 1.61 skrll 472 1.61 skrll DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 473 1.1 itojun 474 1.20 dyoung memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr)); 475 1.64 mrg udav_csr_write(un, UDAV_PAR, eaddr, ETHER_ADDR_LEN); 476 1.1 itojun 477 1.1 itojun /* Initialize network control register */ 478 1.1 itojun /* Disable loopback */ 479 1.64 mrg UDAV_CLRBIT(un, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1); 480 1.1 itojun 481 1.1 itojun /* Initialize RX control register */ 482 1.64 mrg UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC); 483 1.1 itojun 484 1.1 itojun /* If we want promiscuous mode, accept all physical frames. */ 485 1.98 riastrad if (usbnet_ispromisc(un)) 486 1.64 mrg UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC); 487 1.1 itojun else 488 1.64 mrg UDAV_CLRBIT(un, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC); 489 1.1 itojun 490 1.1 itojun /* Enable RX */ 491 1.64 mrg UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_RXEN); 492 1.1 itojun 493 1.1 itojun /* clear POWER_DOWN state of internal PHY */ 494 1.64 mrg UDAV_SETBIT(un, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0); 495 1.64 mrg UDAV_CLRBIT(un, UDAV_GPR, UDAV_GPR_GEPIO0); 496 1.1 itojun 497 1.67 mrg if (mii && (rc = mii_mediachg(mii)) == ENXIO) 498 1.67 mrg rc = 0; 499 1.46 skrll 500 1.77 thorpej if (rc != 0) { 501 1.61 skrll return rc; 502 1.77 thorpej } 503 1.46 skrll 504 1.67 mrg if (usbnet_isdying(un)) 505 1.95 riastrad return EIO; 506 1.77 thorpej 507 1.96 riastrad return 0; 508 1.1 itojun } 509 1.1 itojun 510 1.61 skrll static void 511 1.61 skrll udav_reset(struct usbnet *un) 512 1.1 itojun { 513 1.61 skrll 514 1.63 mrg if (usbnet_isdying(un)) 515 1.61 skrll return; 516 1.61 skrll 517 1.61 skrll DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 518 1.61 skrll 519 1.61 skrll udav_chip_init(un); 520 1.61 skrll } 521 1.1 itojun 522 1.61 skrll static void 523 1.61 skrll udav_chip_init(struct usbnet *un) 524 1.61 skrll { 525 1.1 itojun 526 1.1 itojun /* Select PHY */ 527 1.1 itojun #if 1 528 1.1 itojun /* 529 1.1 itojun * XXX: force select internal phy. 530 1.1 itojun * external phy routines are not tested. 531 1.1 itojun */ 532 1.64 mrg UDAV_CLRBIT(un, UDAV_NCR, UDAV_NCR_EXT_PHY); 533 1.1 itojun #else 534 1.64 mrg if (un->un_flags & UDAV_EXT_PHY) { 535 1.64 mrg UDAV_SETBIT(un, UDAV_NCR, UDAV_NCR_EXT_PHY); 536 1.1 itojun } else { 537 1.64 mrg UDAV_CLRBIT(un, UDAV_NCR, UDAV_NCR_EXT_PHY); 538 1.1 itojun } 539 1.1 itojun #endif 540 1.1 itojun 541 1.64 mrg UDAV_SETBIT(un, UDAV_NCR, UDAV_NCR_RST); 542 1.1 itojun 543 1.61 skrll for (int i = 0; i < UDAV_TX_TIMEOUT; i++) { 544 1.80 riastrad if (usbnet_isdying(un)) 545 1.80 riastrad return; 546 1.64 mrg if (!(udav_csr_read1(un, UDAV_NCR) & UDAV_NCR_RST)) 547 1.1 itojun break; 548 1.1 itojun delay(10); /* XXX */ 549 1.1 itojun } 550 1.1 itojun delay(10000); /* XXX */ 551 1.1 itojun } 552 1.1 itojun 553 1.1 itojun #define UDAV_BITS 6 554 1.1 itojun 555 1.1 itojun #define UDAV_CALCHASH(addr) \ 556 1.1 itojun (ether_crc32_le((addr), ETHER_ADDR_LEN) & ((1 << UDAV_BITS) - 1)) 557 1.1 itojun 558 1.61 skrll static void 559 1.86 riastrad udav_uno_mcast(struct ifnet *ifp) 560 1.1 itojun { 561 1.86 riastrad struct usbnet * const un = ifp->if_softc; 562 1.61 skrll struct ethercom *ec = usbnet_ec(un); 563 1.1 itojun struct ether_multi *enm; 564 1.1 itojun struct ether_multistep step; 565 1.46 skrll uint8_t hashes[8]; 566 1.1 itojun int h = 0; 567 1.1 itojun 568 1.61 skrll DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 569 1.61 skrll 570 1.63 mrg if (usbnet_isdying(un)) 571 1.1 itojun return; 572 1.1 itojun 573 1.64 mrg if (ISSET(un->un_flags, UDAV_NO_PHY)) { 574 1.64 mrg UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL); 575 1.64 mrg UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_PRMSC); 576 1.42 jakllsch return; 577 1.42 jakllsch } 578 1.42 jakllsch 579 1.98 riastrad if (usbnet_ispromisc(un)) { 580 1.87 riastrad ETHER_LOCK(ec); 581 1.87 riastrad ec->ec_flags |= ETHER_F_ALLMULTI; 582 1.87 riastrad ETHER_UNLOCK(ec); 583 1.64 mrg UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL | UDAV_RCR_PRMSC); 584 1.1 itojun return; 585 1.1 itojun } 586 1.1 itojun 587 1.1 itojun /* first, zot all the existing hash bits */ 588 1.1 itojun memset(hashes, 0x00, sizeof(hashes)); 589 1.1 itojun hashes[7] |= 0x80; /* broadcast address */ 590 1.64 mrg udav_csr_write(un, UDAV_MAR, hashes, sizeof(hashes)); 591 1.1 itojun 592 1.1 itojun /* now program new ones */ 593 1.59 msaitoh ETHER_LOCK(ec); 594 1.59 msaitoh ETHER_FIRST_MULTI(step, ec, enm); 595 1.1 itojun while (enm != NULL) { 596 1.1 itojun if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 597 1.59 msaitoh ETHER_ADDR_LEN) != 0) { 598 1.99 riastrad ec->ec_flags |= ETHER_F_ALLMULTI; 599 1.99 riastrad ETHER_UNLOCK(ec); 600 1.99 riastrad UDAV_SETBIT(un, UDAV_RCR, UDAV_RCR_ALL); 601 1.99 riastrad UDAV_CLRBIT(un, UDAV_RCR, UDAV_RCR_PRMSC); 602 1.99 riastrad return; 603 1.59 msaitoh } 604 1.1 itojun 605 1.1 itojun h = UDAV_CALCHASH(enm->enm_addrlo); 606 1.1 itojun hashes[h>>3] |= 1 << (h & 0x7); 607 1.1 itojun ETHER_NEXT_MULTI(step, enm); 608 1.1 itojun } 609 1.87 riastrad ec->ec_flags &= ~ETHER_F_ALLMULTI; 610 1.59 msaitoh ETHER_UNLOCK(ec); 611 1.1 itojun 612 1.1 itojun /* disable all multicast */ 613 1.64 mrg UDAV_CLRBIT(un, UDAV_RCR, UDAV_RCR_ALL); 614 1.1 itojun 615 1.1 itojun /* write hash value to the register */ 616 1.64 mrg udav_csr_write(un, UDAV_MAR, hashes, sizeof(hashes)); 617 1.1 itojun } 618 1.1 itojun 619 1.61 skrll static unsigned 620 1.77 thorpej udav_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) 621 1.1 itojun { 622 1.61 skrll int total_len; 623 1.61 skrll uint8_t *buf = c->unc_buf; 624 1.1 itojun 625 1.61 skrll DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 626 1.1 itojun 627 1.66 skrll if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - 2) 628 1.61 skrll return 0; 629 1.1 itojun 630 1.1 itojun /* Copy the mbuf data into a contiguous buffer */ 631 1.61 skrll m_copydata(m, 0, m->m_pkthdr.len, buf + 2); 632 1.1 itojun total_len = m->m_pkthdr.len; 633 1.1 itojun if (total_len < UDAV_MIN_FRAME_LEN) { 634 1.61 skrll memset(buf + 2 + total_len, 0, 635 1.1 itojun UDAV_MIN_FRAME_LEN - total_len); 636 1.1 itojun total_len = UDAV_MIN_FRAME_LEN; 637 1.1 itojun } 638 1.1 itojun 639 1.1 itojun /* Frame length is specified in the first 2bytes of the buffer */ 640 1.61 skrll buf[0] = (uint8_t)total_len; 641 1.61 skrll buf[1] = (uint8_t)(total_len >> 8); 642 1.1 itojun total_len += 2; 643 1.1 itojun 644 1.61 skrll DPRINTF(("%s: %s: send %d bytes\n", device_xname(un->un_dev), 645 1.61 skrll __func__, total_len)); 646 1.1 itojun 647 1.61 skrll return total_len; 648 1.1 itojun } 649 1.1 itojun 650 1.61 skrll static void 651 1.77 thorpej udav_uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) 652 1.1 itojun { 653 1.61 skrll struct ifnet *ifp = usbnet_ifp(un); 654 1.61 skrll uint8_t *buf = c->unc_buf; 655 1.61 skrll uint16_t pkt_len; 656 1.61 skrll uint8_t pktstat; 657 1.1 itojun 658 1.61 skrll DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 659 1.1 itojun 660 1.61 skrll /* first byte in received data */ 661 1.61 skrll pktstat = *buf; 662 1.61 skrll total_len -= sizeof(pktstat); 663 1.61 skrll buf += sizeof(pktstat); 664 1.1 itojun 665 1.76 christos DPRINTF(("%s: RX Status: 0x%02x\n", device_xname(un->un_dev), pktstat)); 666 1.1 itojun 667 1.61 skrll pkt_len = UGETW(buf); 668 1.61 skrll total_len -= sizeof(pkt_len); 669 1.61 skrll buf += sizeof(pkt_len); 670 1.1 itojun 671 1.76 christos DPRINTF(("%s: RX Length: 0x%02x\n", device_xname(un->un_dev), pkt_len)); 672 1.1 itojun 673 1.61 skrll if (pktstat & UDAV_RSR_LCS) { 674 1.74 thorpej if_statinc(ifp, if_collisions); 675 1.61 skrll return; 676 1.1 itojun } 677 1.1 itojun 678 1.61 skrll if (pkt_len < sizeof(struct ether_header) || 679 1.61 skrll pkt_len > total_len || 680 1.61 skrll (pktstat & UDAV_RSR_ERR)) { 681 1.74 thorpej if_statinc(ifp, if_ierrors); 682 1.61 skrll return; 683 1.1 itojun } 684 1.1 itojun 685 1.61 skrll pkt_len -= ETHER_CRC_LEN; 686 1.1 itojun 687 1.76 christos DPRINTF(("%s: Rx deliver: 0x%02x\n", device_xname(un->un_dev), pkt_len)); 688 1.1 itojun 689 1.61 skrll usbnet_enqueue(un, buf, pkt_len, 0, 0, 0); 690 1.1 itojun } 691 1.1 itojun 692 1.1 itojun /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */ 693 1.61 skrll static void 694 1.77 thorpej udav_uno_stop(struct ifnet *ifp, int disable) 695 1.1 itojun { 696 1.61 skrll struct usbnet * const un = ifp->if_softc; 697 1.1 itojun 698 1.61 skrll DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__)); 699 1.1 itojun 700 1.61 skrll udav_reset(un); 701 1.1 itojun } 702 1.1 itojun 703 1.70 mrg static int 704 1.77 thorpej udav_uno_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) 705 1.1 itojun { 706 1.56 msaitoh uint8_t data[2]; 707 1.1 itojun 708 1.76 christos DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n", 709 1.61 skrll device_xname(un->un_dev), __func__, phy, reg)); 710 1.1 itojun 711 1.63 mrg if (usbnet_isdying(un)) { 712 1.1 itojun #ifdef DIAGNOSTIC 713 1.61 skrll printf("%s: %s: dying\n", device_xname(un->un_dev), 714 1.1 itojun __func__); 715 1.1 itojun #endif 716 1.91 riastrad *val = 0; 717 1.70 mrg return EINVAL; 718 1.1 itojun } 719 1.1 itojun 720 1.1 itojun /* XXX: one PHY only for the internal PHY */ 721 1.1 itojun if (phy != 0) { 722 1.1 itojun DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n", 723 1.61 skrll device_xname(un->un_dev), __func__, phy)); 724 1.91 riastrad *val = 0; 725 1.70 mrg return EINVAL; 726 1.1 itojun } 727 1.1 itojun 728 1.1 itojun /* select internal PHY and set PHY register address */ 729 1.64 mrg udav_csr_write1(un, UDAV_EPAR, 730 1.1 itojun UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK)); 731 1.1 itojun 732 1.1 itojun /* select PHY operation and start read command */ 733 1.64 mrg udav_csr_write1(un, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR); 734 1.1 itojun 735 1.1 itojun /* XXX: should be wait? */ 736 1.1 itojun 737 1.1 itojun /* end read command */ 738 1.64 mrg UDAV_CLRBIT(un, UDAV_EPCR, UDAV_EPCR_ERPRR); 739 1.1 itojun 740 1.1 itojun /* retrieve the result from data registers */ 741 1.64 mrg udav_csr_read(un, UDAV_EPDRL, data, 2); 742 1.1 itojun 743 1.56 msaitoh *val = data[0] | (data[1] << 8); 744 1.1 itojun 745 1.76 christos DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04hx\n", 746 1.61 skrll device_xname(un->un_dev), __func__, phy, reg, *val)); 747 1.1 itojun 748 1.70 mrg return 0; 749 1.1 itojun } 750 1.1 itojun 751 1.70 mrg static int 752 1.77 thorpej udav_uno_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) 753 1.1 itojun { 754 1.56 msaitoh uint8_t data[2]; 755 1.1 itojun 756 1.76 christos DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x val=0x%04hx\n", 757 1.61 skrll device_xname(un->un_dev), __func__, phy, reg, val)); 758 1.1 itojun 759 1.63 mrg if (usbnet_isdying(un)) { 760 1.1 itojun #ifdef DIAGNOSTIC 761 1.61 skrll printf("%s: %s: dying\n", device_xname(un->un_dev), 762 1.1 itojun __func__); 763 1.1 itojun #endif 764 1.70 mrg return EIO; 765 1.1 itojun } 766 1.1 itojun 767 1.1 itojun /* XXX: one PHY only for the internal PHY */ 768 1.1 itojun if (phy != 0) { 769 1.1 itojun DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n", 770 1.61 skrll device_xname(un->un_dev), __func__, phy)); 771 1.70 mrg return EIO; 772 1.1 itojun } 773 1.1 itojun 774 1.1 itojun /* select internal PHY and set PHY register address */ 775 1.64 mrg udav_csr_write1(un, UDAV_EPAR, 776 1.1 itojun UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK)); 777 1.1 itojun 778 1.1 itojun /* put the value to the data registers */ 779 1.56 msaitoh data[0] = val & 0xff; 780 1.56 msaitoh data[1] = (val >> 8) & 0xff; 781 1.64 mrg udav_csr_write(un, UDAV_EPDRL, data, 2); 782 1.1 itojun 783 1.1 itojun /* select PHY operation and start write command */ 784 1.64 mrg udav_csr_write1(un, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW); 785 1.1 itojun 786 1.1 itojun /* XXX: should be wait? */ 787 1.1 itojun 788 1.1 itojun /* end write command */ 789 1.64 mrg UDAV_CLRBIT(un, UDAV_EPCR, UDAV_EPCR_ERPRW); 790 1.1 itojun 791 1.70 mrg return 0; 792 1.1 itojun } 793 1.1 itojun 794 1.61 skrll static void 795 1.77 thorpej udav_uno_mii_statchg(struct ifnet *ifp) 796 1.1 itojun { 797 1.61 skrll struct usbnet * const un = ifp->if_softc; 798 1.61 skrll struct mii_data * const mii = usbnet_mii(un); 799 1.61 skrll 800 1.61 skrll DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__)); 801 1.1 itojun 802 1.63 mrg if (usbnet_isdying(un)) 803 1.1 itojun return; 804 1.1 itojun 805 1.72 skrll if ((mii->mii_media_status & IFM_ACTIVE) && 806 1.61 skrll IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 807 1.61 skrll DPRINTF(("%s: %s: got link\n", 808 1.61 skrll device_xname(un->un_dev), __func__)); 809 1.65 mrg usbnet_set_link(un, true); 810 1.61 skrll } 811 1.61 skrll } 812 1.61 skrll 813 1.61 skrll #ifdef _MODULE 814 1.61 skrll #include "ioconf.c" 815 1.61 skrll #endif 816 1.61 skrll 817 1.67 mrg USBNET_MODULE(udav) 818