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