1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * ASIX AX8817X based USB 2.0 Ethernet Devices 4 * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com> 5 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net> 6 * Copyright (C) 2006 James Painter <jamie.painter@iname.com> 7 * Copyright (c) 2002-2003 TiVo Inc. 8 */ 9 10 #include "asix.h" 11 12 #define AX_HOST_EN_RETRIES 30 13 14 int __must_check asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index, 15 u16 size, void *data, int in_pm) 16 { 17 int ret; 18 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16); 19 20 BUG_ON(!dev); 21 22 if (!in_pm) 23 fn = usbnet_read_cmd; 24 else 25 fn = usbnet_read_cmd_nopm; 26 27 ret = fn(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 28 value, index, data, size); 29 30 if (unlikely(ret < size)) { 31 ret = ret < 0 ? ret : -ENODATA; 32 33 netdev_warn(dev->net, "Failed to read reg index 0x%04x: %d\n", 34 index, ret); 35 } 36 37 return ret; 38 } 39 40 int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index, 41 u16 size, void *data, int in_pm) 42 { 43 int ret; 44 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16); 45 46 BUG_ON(!dev); 47 48 if (!in_pm) 49 fn = usbnet_write_cmd; 50 else 51 fn = usbnet_write_cmd_nopm; 52 53 ret = fn(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 54 value, index, data, size); 55 56 if (unlikely(ret < 0)) 57 netdev_warn(dev->net, "Failed to write reg index 0x%04x: %d\n", 58 index, ret); 59 60 return ret; 61 } 62 63 void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index, 64 u16 size, void *data) 65 { 66 usbnet_write_cmd_async(dev, cmd, 67 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 68 value, index, data, size); 69 } 70 71 static int asix_check_host_enable(struct usbnet *dev, int in_pm) 72 { 73 int i, ret; 74 u8 smsr; 75 76 for (i = 0; i < AX_HOST_EN_RETRIES; ++i) { 77 ret = asix_set_sw_mii(dev, in_pm); 78 if (ret == -ENODEV || ret == -ETIMEDOUT) 79 break; 80 usleep_range(1000, 1100); 81 ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG, 82 0, 0, 1, &smsr, in_pm); 83 if (ret == -ENODEV) 84 break; 85 else if (ret < 0) 86 continue; 87 else if (smsr & AX_HOST_EN) 88 break; 89 } 90 91 return i >= AX_HOST_EN_RETRIES ? -ETIMEDOUT : ret; 92 } 93 94 static void reset_asix_rx_fixup_info(struct asix_rx_fixup_info *rx) 95 { 96 /* Reset the variables that have a lifetime outside of 97 * asix_rx_fixup_internal() so that future processing starts from a 98 * known set of initial conditions. 99 */ 100 101 if (rx->ax_skb) { 102 /* Discard any incomplete Ethernet frame in the netdev buffer */ 103 kfree_skb(rx->ax_skb); 104 rx->ax_skb = NULL; 105 } 106 107 /* Assume the Data header 32-bit word is at the start of the current 108 * or next URB socket buffer so reset all the state variables. 109 */ 110 rx->remaining = 0; 111 rx->split_head = false; 112 rx->header = 0; 113 } 114 115 int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb, 116 struct asix_rx_fixup_info *rx) 117 { 118 int offset = 0; 119 u16 size; 120 121 /* When an Ethernet frame spans multiple URB socket buffers, 122 * do a sanity test for the Data header synchronisation. 123 * Attempt to detect the situation of the previous socket buffer having 124 * been truncated or a socket buffer was missing. These situations 125 * cause a discontinuity in the data stream and therefore need to avoid 126 * appending bad data to the end of the current netdev socket buffer. 127 * Also avoid unnecessarily discarding a good current netdev socket 128 * buffer. 129 */ 130 if (rx->remaining && (rx->remaining + sizeof(u32) <= skb->len)) { 131 offset = ((rx->remaining + 1) & 0xfffe); 132 rx->header = get_unaligned_le32(skb->data + offset); 133 offset = 0; 134 135 size = (u16)(rx->header & 0x7ff); 136 if (size != ((~rx->header >> 16) & 0x7ff)) { 137 netdev_err(dev->net, "asix_rx_fixup() Data Header synchronisation was lost, remaining %d\n", 138 rx->remaining); 139 reset_asix_rx_fixup_info(rx); 140 } 141 } 142 143 while (offset + sizeof(u16) <= skb->len) { 144 u16 copy_length; 145 146 if (!rx->remaining) { 147 if (skb->len - offset == sizeof(u16)) { 148 rx->header = get_unaligned_le16( 149 skb->data + offset); 150 rx->split_head = true; 151 offset += sizeof(u16); 152 break; 153 } 154 155 if (rx->split_head == true) { 156 rx->header |= (get_unaligned_le16( 157 skb->data + offset) << 16); 158 rx->split_head = false; 159 offset += sizeof(u16); 160 } else { 161 rx->header = get_unaligned_le32(skb->data + 162 offset); 163 offset += sizeof(u32); 164 } 165 166 /* take frame length from Data header 32-bit word */ 167 size = (u16)(rx->header & 0x7ff); 168 if (size != ((~rx->header >> 16) & 0x7ff)) { 169 netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n", 170 rx->header, offset); 171 reset_asix_rx_fixup_info(rx); 172 return 0; 173 } 174 if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) { 175 netdev_dbg(dev->net, "asix_rx_fixup() Bad RX Length %d\n", 176 size); 177 reset_asix_rx_fixup_info(rx); 178 return 0; 179 } 180 181 /* Sometimes may fail to get a netdev socket buffer but 182 * continue to process the URB socket buffer so that 183 * synchronisation of the Ethernet frame Data header 184 * word is maintained. 185 */ 186 rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size); 187 188 rx->remaining = size; 189 } 190 191 if (rx->remaining > skb->len - offset) { 192 copy_length = skb->len - offset; 193 rx->remaining -= copy_length; 194 } else { 195 copy_length = rx->remaining; 196 rx->remaining = 0; 197 } 198 199 if (rx->ax_skb) { 200 skb_put_data(rx->ax_skb, skb->data + offset, 201 copy_length); 202 if (!rx->remaining) { 203 usbnet_skb_return(dev, rx->ax_skb); 204 rx->ax_skb = NULL; 205 } 206 } 207 208 offset += (copy_length + 1) & 0xfffe; 209 } 210 211 if (skb->len != offset) { 212 netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n", 213 skb->len, offset); 214 reset_asix_rx_fixup_info(rx); 215 return 0; 216 } 217 218 return 1; 219 } 220 221 int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb) 222 { 223 struct asix_common_private *dp = dev->driver_priv; 224 struct asix_rx_fixup_info *rx = &dp->rx_fixup_info; 225 226 return asix_rx_fixup_internal(dev, skb, rx); 227 } 228 229 void asix_rx_fixup_common_free(struct asix_common_private *dp) 230 { 231 struct asix_rx_fixup_info *rx; 232 233 if (!dp) 234 return; 235 236 rx = &dp->rx_fixup_info; 237 238 if (rx->ax_skb) { 239 kfree_skb(rx->ax_skb); 240 rx->ax_skb = NULL; 241 } 242 } 243 244 struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb, 245 gfp_t flags) 246 { 247 int padlen; 248 int headroom = skb_headroom(skb); 249 int tailroom = skb_tailroom(skb); 250 u32 packet_len; 251 u32 padbytes = 0xffff0000; 252 void *ptr; 253 254 padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4; 255 256 /* We need to push 4 bytes in front of frame (packet_len) 257 * and maybe add 4 bytes after the end (if padlen is 4) 258 * 259 * Avoid skb_copy_expand() expensive call, using following rules : 260 * - We are allowed to push 4 bytes in headroom if skb_header_cloned() 261 * is false (and if we have 4 bytes of headroom) 262 * - We are allowed to put 4 bytes at tail if skb_cloned() 263 * is false (and if we have 4 bytes of tailroom) 264 * 265 * TCP packets for example are cloned, but __skb_header_release() 266 * was called in tcp stack, allowing us to use headroom for our needs. 267 */ 268 if (!skb_header_cloned(skb) && 269 !(padlen && skb_cloned(skb)) && 270 headroom + tailroom >= 4 + padlen) { 271 /* following should not happen, but better be safe */ 272 if (headroom < 4 || 273 tailroom < padlen) { 274 skb->data = memmove(skb->head + 4, skb->data, skb->len); 275 skb_set_tail_pointer(skb, skb->len); 276 } 277 } else { 278 struct sk_buff *skb2; 279 280 skb2 = skb_copy_expand(skb, 4, padlen, flags); 281 dev_kfree_skb_any(skb); 282 skb = skb2; 283 if (!skb) 284 return NULL; 285 } 286 287 packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len; 288 ptr = skb_push(skb, 4); 289 put_unaligned_le32(packet_len, ptr); 290 291 if (padlen) { 292 put_unaligned_le32(padbytes, skb_tail_pointer(skb)); 293 skb_put(skb, sizeof(padbytes)); 294 } 295 296 usbnet_set_skb_tx_stats(skb, 1, 0); 297 return skb; 298 } 299 300 int asix_set_sw_mii(struct usbnet *dev, int in_pm) 301 { 302 int ret; 303 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL, in_pm); 304 305 if (ret < 0) 306 netdev_err(dev->net, "Failed to enable software MII access\n"); 307 return ret; 308 } 309 310 int asix_set_hw_mii(struct usbnet *dev, int in_pm) 311 { 312 int ret; 313 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL, in_pm); 314 if (ret < 0) 315 netdev_err(dev->net, "Failed to enable hardware MII access\n"); 316 return ret; 317 } 318 319 int asix_read_phy_addr(struct usbnet *dev, bool internal) 320 { 321 int ret, offset; 322 u8 buf[2]; 323 324 ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf, 0); 325 if (ret < 0) 326 goto error; 327 328 if (ret < 2) { 329 ret = -EIO; 330 goto error; 331 } 332 333 offset = (internal ? 1 : 0); 334 ret = buf[offset]; 335 336 netdev_dbg(dev->net, "%s PHY address 0x%x\n", 337 internal ? "internal" : "external", ret); 338 339 return ret; 340 341 error: 342 netdev_err(dev->net, "Error reading PHY_ID register: %02x\n", ret); 343 344 return ret; 345 } 346 347 int asix_sw_reset(struct usbnet *dev, u8 flags, int in_pm) 348 { 349 int ret; 350 351 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL, in_pm); 352 if (ret < 0) 353 netdev_err(dev->net, "Failed to send software reset: %02x\n", ret); 354 355 return ret; 356 } 357 358 u16 asix_read_rx_ctl(struct usbnet *dev, int in_pm) 359 { 360 __le16 v; 361 int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v, in_pm); 362 363 if (ret < 0) { 364 netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret); 365 goto out; 366 } 367 ret = le16_to_cpu(v); 368 out: 369 return ret; 370 } 371 372 int asix_write_rx_ctl(struct usbnet *dev, u16 mode, int in_pm) 373 { 374 int ret; 375 376 netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode); 377 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL, in_pm); 378 if (ret < 0) 379 netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n", 380 mode, ret); 381 382 return ret; 383 } 384 385 u16 asix_read_medium_status(struct usbnet *dev, int in_pm) 386 { 387 __le16 v; 388 int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 389 0, 0, 2, &v, in_pm); 390 391 if (ret < 0) { 392 netdev_err(dev->net, "Error reading Medium Status register: %02x\n", 393 ret); 394 return ret; /* TODO: callers not checking for error ret */ 395 } 396 397 return le16_to_cpu(v); 398 399 } 400 401 int asix_write_medium_mode(struct usbnet *dev, u16 mode, int in_pm) 402 { 403 int ret; 404 405 netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode); 406 ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, 407 mode, 0, 0, NULL, in_pm); 408 if (ret < 0) 409 netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n", 410 mode, ret); 411 412 return ret; 413 } 414 415 /* set MAC link settings according to information from phylib */ 416 void asix_adjust_link(struct net_device *netdev) 417 { 418 struct phy_device *phydev = netdev->phydev; 419 struct usbnet *dev = netdev_priv(netdev); 420 u16 mode = 0; 421 422 if (phydev->link) { 423 mode = AX88772_MEDIUM_DEFAULT; 424 425 if (phydev->duplex == DUPLEX_HALF) 426 mode &= ~AX_MEDIUM_FD; 427 428 if (phydev->speed != SPEED_100) 429 mode &= ~AX_MEDIUM_PS; 430 } 431 432 asix_write_medium_mode(dev, mode, 0); 433 phy_print_status(phydev); 434 } 435 436 int asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm) 437 { 438 int ret; 439 440 netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value); 441 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL, in_pm); 442 if (ret < 0) 443 netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n", 444 value, ret); 445 446 if (sleep) 447 msleep(sleep); 448 449 return ret; 450 } 451 452 /* 453 * AX88772 & AX88178 have a 16-bit RX_CTL value 454 */ 455 void asix_set_multicast(struct net_device *net) 456 { 457 struct usbnet *dev = netdev_priv(net); 458 struct asix_data *data = (struct asix_data *)&dev->data; 459 u16 rx_ctl = AX_DEFAULT_RX_CTL; 460 461 if (net->flags & IFF_PROMISC) { 462 rx_ctl |= AX_RX_CTL_PRO; 463 } else if (net->flags & IFF_ALLMULTI || 464 netdev_mc_count(net) > AX_MAX_MCAST) { 465 rx_ctl |= AX_RX_CTL_AMALL; 466 } else if (netdev_mc_empty(net)) { 467 /* just broadcast and directed */ 468 } else { 469 /* We use the 20 byte dev->data 470 * for our 8 byte filter buffer 471 * to avoid allocating memory that 472 * is tricky to free later */ 473 struct netdev_hw_addr *ha; 474 u32 crc_bits; 475 476 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE); 477 478 /* Build the multicast hash filter. */ 479 netdev_for_each_mc_addr(ha, net) { 480 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26; 481 data->multi_filter[crc_bits >> 3] |= 482 1 << (crc_bits & 7); 483 } 484 485 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0, 486 AX_MCAST_FILTER_SIZE, data->multi_filter); 487 488 rx_ctl |= AX_RX_CTL_AM; 489 } 490 491 asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL); 492 } 493 494 int asix_mdio_read(struct net_device *netdev, int phy_id, int loc) 495 { 496 struct usbnet *dev = netdev_priv(netdev); 497 __le16 res; 498 int ret; 499 500 mutex_lock(&dev->phy_mutex); 501 502 ret = asix_check_host_enable(dev, 0); 503 if (ret == -ENODEV || ret == -ETIMEDOUT) { 504 mutex_unlock(&dev->phy_mutex); 505 return ret; 506 } 507 508 ret = asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, 509 &res, 0); 510 if (ret < 0) 511 goto out; 512 513 ret = asix_set_hw_mii(dev, 0); 514 out: 515 mutex_unlock(&dev->phy_mutex); 516 517 netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n", 518 phy_id, loc, le16_to_cpu(res)); 519 520 return ret < 0 ? ret : le16_to_cpu(res); 521 } 522 523 static int __asix_mdio_write(struct net_device *netdev, int phy_id, int loc, 524 int val) 525 { 526 struct usbnet *dev = netdev_priv(netdev); 527 __le16 res = cpu_to_le16(val); 528 int ret; 529 530 netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n", 531 phy_id, loc, val); 532 533 mutex_lock(&dev->phy_mutex); 534 535 ret = asix_check_host_enable(dev, 0); 536 if (ret == -ENODEV) 537 goto out; 538 539 ret = asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, 540 &res, 0); 541 if (ret < 0) 542 goto out; 543 544 ret = asix_set_hw_mii(dev, 0); 545 out: 546 mutex_unlock(&dev->phy_mutex); 547 548 return ret < 0 ? ret : 0; 549 } 550 551 void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val) 552 { 553 __asix_mdio_write(netdev, phy_id, loc, val); 554 } 555 556 /* MDIO read and write wrappers for phylib */ 557 int asix_mdio_bus_read(struct mii_bus *bus, int phy_id, int regnum) 558 { 559 struct usbnet *priv = bus->priv; 560 561 return asix_mdio_read(priv->net, phy_id, regnum); 562 } 563 564 int asix_mdio_bus_write(struct mii_bus *bus, int phy_id, int regnum, u16 val) 565 { 566 struct usbnet *priv = bus->priv; 567 568 return __asix_mdio_write(priv->net, phy_id, regnum, val); 569 } 570 571 int asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc) 572 { 573 struct usbnet *dev = netdev_priv(netdev); 574 __le16 res; 575 int ret; 576 577 mutex_lock(&dev->phy_mutex); 578 579 ret = asix_check_host_enable(dev, 1); 580 if (ret == -ENODEV || ret == -ETIMEDOUT) { 581 mutex_unlock(&dev->phy_mutex); 582 return ret; 583 } 584 585 ret = asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, 586 (__u16)loc, 2, &res, 1); 587 if (ret < 0) { 588 mutex_unlock(&dev->phy_mutex); 589 return ret; 590 } 591 asix_set_hw_mii(dev, 1); 592 mutex_unlock(&dev->phy_mutex); 593 594 netdev_dbg(dev->net, "asix_mdio_read_nopm() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n", 595 phy_id, loc, le16_to_cpu(res)); 596 597 return le16_to_cpu(res); 598 } 599 600 void 601 asix_mdio_write_nopm(struct net_device *netdev, int phy_id, int loc, int val) 602 { 603 struct usbnet *dev = netdev_priv(netdev); 604 __le16 res = cpu_to_le16(val); 605 int ret; 606 607 netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n", 608 phy_id, loc, val); 609 610 mutex_lock(&dev->phy_mutex); 611 612 ret = asix_check_host_enable(dev, 1); 613 if (ret == -ENODEV) { 614 mutex_unlock(&dev->phy_mutex); 615 return; 616 } 617 618 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, 619 (__u16)loc, 2, &res, 1); 620 asix_set_hw_mii(dev, 1); 621 mutex_unlock(&dev->phy_mutex); 622 } 623 624 void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo) 625 { 626 struct usbnet *dev = netdev_priv(net); 627 u8 opt; 628 629 if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 630 0, 0, 1, &opt, 0) < 0) { 631 wolinfo->supported = 0; 632 wolinfo->wolopts = 0; 633 return; 634 } 635 wolinfo->supported = WAKE_PHY | WAKE_MAGIC; 636 wolinfo->wolopts = 0; 637 if (opt & AX_MONITOR_LINK) 638 wolinfo->wolopts |= WAKE_PHY; 639 if (opt & AX_MONITOR_MAGIC) 640 wolinfo->wolopts |= WAKE_MAGIC; 641 } 642 643 int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo) 644 { 645 struct usbnet *dev = netdev_priv(net); 646 u8 opt = 0; 647 648 if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC)) 649 return -EINVAL; 650 651 if (wolinfo->wolopts & WAKE_PHY) 652 opt |= AX_MONITOR_LINK; 653 if (wolinfo->wolopts & WAKE_MAGIC) 654 opt |= AX_MONITOR_MAGIC; 655 656 if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE, 657 opt, 0, 0, NULL, 0) < 0) 658 return -EINVAL; 659 660 return 0; 661 } 662 663 int asix_get_eeprom_len(struct net_device *net) 664 { 665 return AX_EEPROM_LEN; 666 } 667 668 int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom, 669 u8 *data) 670 { 671 struct usbnet *dev = netdev_priv(net); 672 u16 *eeprom_buff; 673 int first_word, last_word; 674 int i; 675 676 if (eeprom->len == 0) 677 return -EINVAL; 678 679 eeprom->magic = AX_EEPROM_MAGIC; 680 681 first_word = eeprom->offset >> 1; 682 last_word = (eeprom->offset + eeprom->len - 1) >> 1; 683 684 eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16), 685 GFP_KERNEL); 686 if (!eeprom_buff) 687 return -ENOMEM; 688 689 /* ax8817x returns 2 bytes from eeprom on read */ 690 for (i = first_word; i <= last_word; i++) { 691 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2, 692 &eeprom_buff[i - first_word], 0) < 0) { 693 kfree(eeprom_buff); 694 return -EIO; 695 } 696 } 697 698 memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len); 699 kfree(eeprom_buff); 700 return 0; 701 } 702 703 int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom, 704 u8 *data) 705 { 706 struct usbnet *dev = netdev_priv(net); 707 u16 *eeprom_buff; 708 int first_word, last_word; 709 int i; 710 int ret; 711 712 netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n", 713 eeprom->len, eeprom->offset, eeprom->magic); 714 715 if (eeprom->len == 0) 716 return -EINVAL; 717 718 if (eeprom->magic != AX_EEPROM_MAGIC) 719 return -EINVAL; 720 721 first_word = eeprom->offset >> 1; 722 last_word = (eeprom->offset + eeprom->len - 1) >> 1; 723 724 eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16), 725 GFP_KERNEL); 726 if (!eeprom_buff) 727 return -ENOMEM; 728 729 /* align data to 16 bit boundaries, read the missing data from 730 the EEPROM */ 731 if (eeprom->offset & 1) { 732 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2, 733 &eeprom_buff[0], 0); 734 if (ret < 0) { 735 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word); 736 goto free; 737 } 738 } 739 740 if ((eeprom->offset + eeprom->len) & 1) { 741 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2, 742 &eeprom_buff[last_word - first_word], 0); 743 if (ret < 0) { 744 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word); 745 goto free; 746 } 747 } 748 749 memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len); 750 751 /* write data to EEPROM */ 752 ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL, 0); 753 if (ret < 0) { 754 netdev_err(net, "Failed to enable EEPROM write\n"); 755 goto free; 756 } 757 msleep(20); 758 759 for (i = first_word; i <= last_word; i++) { 760 netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n", 761 i, eeprom_buff[i - first_word]); 762 ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i, 763 eeprom_buff[i - first_word], 0, NULL, 0); 764 if (ret < 0) { 765 netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n", 766 i); 767 goto free; 768 } 769 msleep(20); 770 } 771 772 ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL, 0); 773 if (ret < 0) { 774 netdev_err(net, "Failed to disable EEPROM write\n"); 775 goto free; 776 } 777 778 ret = 0; 779 free: 780 kfree(eeprom_buff); 781 return ret; 782 } 783 784 void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info) 785 { 786 /* Inherit standard device info */ 787 usbnet_get_drvinfo(net, info); 788 strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver)); 789 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version)); 790 } 791 792 int asix_set_mac_address(struct net_device *net, void *p) 793 { 794 struct usbnet *dev = netdev_priv(net); 795 struct asix_data *data = (struct asix_data *)&dev->data; 796 struct sockaddr *addr = p; 797 798 if (netif_running(net)) 799 return -EBUSY; 800 if (!is_valid_ether_addr(addr->sa_data)) 801 return -EADDRNOTAVAIL; 802 803 eth_hw_addr_set(net, addr->sa_data); 804 805 /* We use the 20 byte dev->data 806 * for our 6 byte mac buffer 807 * to avoid allocating memory that 808 * is tricky to free later */ 809 memcpy(data->mac_addr, addr->sa_data, ETH_ALEN); 810 asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN, 811 data->mac_addr); 812 813 return 0; 814 } 815