11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2b4cdae20SChristian Riesch /* 3b4cdae20SChristian Riesch * ASIX AX8817X based USB 2.0 Ethernet Devices 4b4cdae20SChristian Riesch * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com> 5b4cdae20SChristian Riesch * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net> 6b4cdae20SChristian Riesch * Copyright (C) 2006 James Painter <jamie.painter@iname.com> 7b4cdae20SChristian Riesch * Copyright (c) 2002-2003 TiVo Inc. 8b4cdae20SChristian Riesch */ 9b4cdae20SChristian Riesch 10607740bcSChristian Riesch #include "asix.h" 11b4cdae20SChristian Riesch 12*d1652b70SPavel Skripkin #define AX_HOST_EN_RETRIES 30 13*d1652b70SPavel Skripkin 14607740bcSChristian Riesch int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index, 15d9fe64e5SRobert Foss u16 size, void *data, int in_pm) 16b4cdae20SChristian Riesch { 170bc69efbSMing Lei int ret; 18d9fe64e5SRobert Foss int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16); 19d9fe64e5SRobert Foss 20d9fe64e5SRobert Foss BUG_ON(!dev); 21d9fe64e5SRobert Foss 22d9fe64e5SRobert Foss if (!in_pm) 23d9fe64e5SRobert Foss fn = usbnet_read_cmd; 24d9fe64e5SRobert Foss else 25d9fe64e5SRobert Foss fn = usbnet_read_cmd_nopm; 26d9fe64e5SRobert Foss 27d9fe64e5SRobert Foss ret = fn(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 280bc69efbSMing Lei value, index, data, size); 29b4cdae20SChristian Riesch 30d9fe64e5SRobert Foss if (unlikely(ret < 0)) 31d9fe64e5SRobert Foss netdev_warn(dev->net, "Failed to read reg index 0x%04x: %d\n", 32d9fe64e5SRobert Foss index, ret); 33d9fe64e5SRobert Foss 340bc69efbSMing Lei return ret; 35b4cdae20SChristian Riesch } 36b4cdae20SChristian Riesch 37607740bcSChristian Riesch int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index, 38d9fe64e5SRobert Foss u16 size, void *data, int in_pm) 39b4cdae20SChristian Riesch { 40d9fe64e5SRobert Foss int ret; 41d9fe64e5SRobert Foss int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16); 42d9fe64e5SRobert Foss 43d9fe64e5SRobert Foss BUG_ON(!dev); 44d9fe64e5SRobert Foss 45d9fe64e5SRobert Foss if (!in_pm) 46d9fe64e5SRobert Foss fn = usbnet_write_cmd; 47d9fe64e5SRobert Foss else 48d9fe64e5SRobert Foss fn = usbnet_write_cmd_nopm; 49d9fe64e5SRobert Foss 50d9fe64e5SRobert Foss ret = fn(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 510bc69efbSMing Lei value, index, data, size); 52d9fe64e5SRobert Foss 53d9fe64e5SRobert Foss if (unlikely(ret < 0)) 54d9fe64e5SRobert Foss netdev_warn(dev->net, "Failed to write reg index 0x%04x: %d\n", 55d9fe64e5SRobert Foss index, ret); 56d9fe64e5SRobert Foss 57d9fe64e5SRobert Foss return ret; 58b4cdae20SChristian Riesch } 59b4cdae20SChristian Riesch 60607740bcSChristian Riesch void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index, 61b4cdae20SChristian Riesch u16 size, void *data) 62b4cdae20SChristian Riesch { 630bc69efbSMing Lei usbnet_write_cmd_async(dev, cmd, 640bc69efbSMing Lei USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 650bc69efbSMing Lei value, index, data, size); 66b4cdae20SChristian Riesch } 67b4cdae20SChristian Riesch 68a786e319SPavel Skripkin static int asix_check_host_enable(struct usbnet *dev, int in_pm) 69a786e319SPavel Skripkin { 70a786e319SPavel Skripkin int i, ret; 71a786e319SPavel Skripkin u8 smsr; 72a786e319SPavel Skripkin 73*d1652b70SPavel Skripkin for (i = 0; i < AX_HOST_EN_RETRIES; ++i) { 74a786e319SPavel Skripkin ret = asix_set_sw_mii(dev, in_pm); 75a786e319SPavel Skripkin if (ret == -ENODEV || ret == -ETIMEDOUT) 76a786e319SPavel Skripkin break; 77a786e319SPavel Skripkin usleep_range(1000, 1100); 78a786e319SPavel Skripkin ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG, 79a786e319SPavel Skripkin 0, 0, 1, &smsr, in_pm); 80a786e319SPavel Skripkin if (ret == -ENODEV) 81a786e319SPavel Skripkin break; 828035b1a2SPavel Skripkin else if (ret < sizeof(smsr)) 83a786e319SPavel Skripkin continue; 84a786e319SPavel Skripkin else if (smsr & AX_HOST_EN) 85a786e319SPavel Skripkin break; 86a786e319SPavel Skripkin } 87a786e319SPavel Skripkin 88*d1652b70SPavel Skripkin return i >= AX_HOST_EN_RETRIES ? -ETIMEDOUT : ret; 89a786e319SPavel Skripkin } 90a786e319SPavel Skripkin 91960eb4eeSDean Jenkins static void reset_asix_rx_fixup_info(struct asix_rx_fixup_info *rx) 92960eb4eeSDean Jenkins { 93960eb4eeSDean Jenkins /* Reset the variables that have a lifetime outside of 94960eb4eeSDean Jenkins * asix_rx_fixup_internal() so that future processing starts from a 95960eb4eeSDean Jenkins * known set of initial conditions. 96960eb4eeSDean Jenkins */ 97960eb4eeSDean Jenkins 98960eb4eeSDean Jenkins if (rx->ax_skb) { 99960eb4eeSDean Jenkins /* Discard any incomplete Ethernet frame in the netdev buffer */ 100960eb4eeSDean Jenkins kfree_skb(rx->ax_skb); 101960eb4eeSDean Jenkins rx->ax_skb = NULL; 102960eb4eeSDean Jenkins } 103960eb4eeSDean Jenkins 104960eb4eeSDean Jenkins /* Assume the Data header 32-bit word is at the start of the current 105960eb4eeSDean Jenkins * or next URB socket buffer so reset all the state variables. 106960eb4eeSDean Jenkins */ 107960eb4eeSDean Jenkins rx->remaining = 0; 108960eb4eeSDean Jenkins rx->split_head = false; 109960eb4eeSDean Jenkins rx->header = 0; 110960eb4eeSDean Jenkins } 111960eb4eeSDean Jenkins 1128b5b6f54SLucas Stach int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb, 1138b5b6f54SLucas Stach struct asix_rx_fixup_info *rx) 114b4cdae20SChristian Riesch { 115b4cdae20SChristian Riesch int offset = 0; 1167b0378f5SDean Jenkins u16 size; 117b4cdae20SChristian Riesch 1183f30b158SDean Jenkins /* When an Ethernet frame spans multiple URB socket buffers, 1193f30b158SDean Jenkins * do a sanity test for the Data header synchronisation. 1203f30b158SDean Jenkins * Attempt to detect the situation of the previous socket buffer having 1213f30b158SDean Jenkins * been truncated or a socket buffer was missing. These situations 1223f30b158SDean Jenkins * cause a discontinuity in the data stream and therefore need to avoid 1233f30b158SDean Jenkins * appending bad data to the end of the current netdev socket buffer. 1243f30b158SDean Jenkins * Also avoid unnecessarily discarding a good current netdev socket 1253f30b158SDean Jenkins * buffer. 1263f30b158SDean Jenkins */ 1273f30b158SDean Jenkins if (rx->remaining && (rx->remaining + sizeof(u32) <= skb->len)) { 128cd9e2e5dSJohn Stultz offset = ((rx->remaining + 1) & 0xfffe); 1293f30b158SDean Jenkins rx->header = get_unaligned_le32(skb->data + offset); 1303f30b158SDean Jenkins offset = 0; 1313f30b158SDean Jenkins 1323f30b158SDean Jenkins size = (u16)(rx->header & 0x7ff); 1333f30b158SDean Jenkins if (size != ((~rx->header >> 16) & 0x7ff)) { 1343f30b158SDean Jenkins netdev_err(dev->net, "asix_rx_fixup() Data Header synchronisation was lost, remaining %d\n", 1353f30b158SDean Jenkins rx->remaining); 136960eb4eeSDean Jenkins reset_asix_rx_fixup_info(rx); 1373f30b158SDean Jenkins } 1383f30b158SDean Jenkins } 1393f30b158SDean Jenkins 1408b5b6f54SLucas Stach while (offset + sizeof(u16) <= skb->len) { 1417b0378f5SDean Jenkins u16 copy_length; 142b4cdae20SChristian Riesch 1437b0378f5SDean Jenkins if (!rx->remaining) { 1443bfc69abSDean Jenkins if (skb->len - offset == sizeof(u16)) { 1458b5b6f54SLucas Stach rx->header = get_unaligned_le16( 1468b5b6f54SLucas Stach skb->data + offset); 1478b5b6f54SLucas Stach rx->split_head = true; 1488b5b6f54SLucas Stach offset += sizeof(u16); 1498b5b6f54SLucas Stach break; 1503bfc69abSDean Jenkins } 1513bfc69abSDean Jenkins 1523bfc69abSDean Jenkins if (rx->split_head == true) { 1538b5b6f54SLucas Stach rx->header |= (get_unaligned_le16( 1543bfc69abSDean Jenkins skb->data + offset) << 16); 1558b5b6f54SLucas Stach rx->split_head = false; 1568b5b6f54SLucas Stach offset += sizeof(u16); 1578b5b6f54SLucas Stach } else { 1588b5b6f54SLucas Stach rx->header = get_unaligned_le32(skb->data + 1598b5b6f54SLucas Stach offset); 160b4cdae20SChristian Riesch offset += sizeof(u32); 1618b5b6f54SLucas Stach } 162b4cdae20SChristian Riesch 1637b0378f5SDean Jenkins /* take frame length from Data header 32-bit word */ 1647b0378f5SDean Jenkins size = (u16)(rx->header & 0x7ff); 1657b0378f5SDean Jenkins if (size != ((~rx->header >> 16) & 0x7ff)) { 1668b5b6f54SLucas Stach netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n", 1678b5b6f54SLucas Stach rx->header, offset); 168960eb4eeSDean Jenkins reset_asix_rx_fixup_info(rx); 1698b5b6f54SLucas Stach return 0; 1708b5b6f54SLucas Stach } 1719a5ccd8eSDean Jenkins if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) { 172b70183dbSstephen hemminger netdev_dbg(dev->net, "asix_rx_fixup() Bad RX Length %d\n", 1739a5ccd8eSDean Jenkins size); 174960eb4eeSDean Jenkins reset_asix_rx_fixup_info(rx); 1759a5ccd8eSDean Jenkins return 0; 1769a5ccd8eSDean Jenkins } 1779a5ccd8eSDean Jenkins 1786a570814SDean Jenkins /* Sometimes may fail to get a netdev socket buffer but 1796a570814SDean Jenkins * continue to process the URB socket buffer so that 1806a570814SDean Jenkins * synchronisation of the Ethernet frame Data header 1816a570814SDean Jenkins * word is maintained. 1826a570814SDean Jenkins */ 1837b0378f5SDean Jenkins rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size); 184b4cdae20SChristian Riesch 1859a5ccd8eSDean Jenkins rx->remaining = size; 186b4cdae20SChristian Riesch } 187b4cdae20SChristian Riesch 1887b0378f5SDean Jenkins if (rx->remaining > skb->len - offset) { 1897b0378f5SDean Jenkins copy_length = skb->len - offset; 1907b0378f5SDean Jenkins rx->remaining -= copy_length; 1917b0378f5SDean Jenkins } else { 1927b0378f5SDean Jenkins copy_length = rx->remaining; 1937b0378f5SDean Jenkins rx->remaining = 0; 1948b5b6f54SLucas Stach } 195b4cdae20SChristian Riesch 1966a570814SDean Jenkins if (rx->ax_skb) { 197b952f4dfSyuan linyu skb_put_data(rx->ax_skb, skb->data + offset, 19859ae1d12SJohannes Berg copy_length); 19922889dbbSDean Jenkins if (!rx->remaining) { 2008b5b6f54SLucas Stach usbnet_skb_return(dev, rx->ax_skb); 20122889dbbSDean Jenkins rx->ax_skb = NULL; 20222889dbbSDean Jenkins } 2036a570814SDean Jenkins } 2048b5b6f54SLucas Stach 2057b0378f5SDean Jenkins offset += (copy_length + 1) & 0xfffe; 206b4cdae20SChristian Riesch } 207b4cdae20SChristian Riesch 208b4cdae20SChristian Riesch if (skb->len != offset) { 2098b5b6f54SLucas Stach netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n", 2108b5b6f54SLucas Stach skb->len, offset); 211960eb4eeSDean Jenkins reset_asix_rx_fixup_info(rx); 212b4cdae20SChristian Riesch return 0; 213b4cdae20SChristian Riesch } 2148b5b6f54SLucas Stach 215b4cdae20SChristian Riesch return 1; 216b4cdae20SChristian Riesch } 217b4cdae20SChristian Riesch 2188b5b6f54SLucas Stach int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb) 2198b5b6f54SLucas Stach { 2208b5b6f54SLucas Stach struct asix_common_private *dp = dev->driver_priv; 2218b5b6f54SLucas Stach struct asix_rx_fixup_info *rx = &dp->rx_fixup_info; 2228b5b6f54SLucas Stach 2238b5b6f54SLucas Stach return asix_rx_fixup_internal(dev, skb, rx); 2248b5b6f54SLucas Stach } 2258b5b6f54SLucas Stach 226d0c8f338SDean Jenkins void asix_rx_fixup_common_free(struct asix_common_private *dp) 227d0c8f338SDean Jenkins { 228d0c8f338SDean Jenkins struct asix_rx_fixup_info *rx; 229d0c8f338SDean Jenkins 230d0c8f338SDean Jenkins if (!dp) 231d0c8f338SDean Jenkins return; 232d0c8f338SDean Jenkins 233d0c8f338SDean Jenkins rx = &dp->rx_fixup_info; 234d0c8f338SDean Jenkins 235d0c8f338SDean Jenkins if (rx->ax_skb) { 236d0c8f338SDean Jenkins kfree_skb(rx->ax_skb); 237d0c8f338SDean Jenkins rx->ax_skb = NULL; 238d0c8f338SDean Jenkins } 239d0c8f338SDean Jenkins } 240d0c8f338SDean Jenkins 241607740bcSChristian Riesch struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb, 242b4cdae20SChristian Riesch gfp_t flags) 243b4cdae20SChristian Riesch { 244b4cdae20SChristian Riesch int padlen; 245b4cdae20SChristian Riesch int headroom = skb_headroom(skb); 246b4cdae20SChristian Riesch int tailroom = skb_tailroom(skb); 247b4cdae20SChristian Riesch u32 packet_len; 248b4cdae20SChristian Riesch u32 padbytes = 0xffff0000; 2497e24b4edSChuhong Yuan void *ptr; 250b4cdae20SChristian Riesch 251b4cdae20SChristian Riesch padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4; 252b4cdae20SChristian Riesch 253b4cdae20SChristian Riesch /* We need to push 4 bytes in front of frame (packet_len) 254b4cdae20SChristian Riesch * and maybe add 4 bytes after the end (if padlen is 4) 255b4cdae20SChristian Riesch * 256b4cdae20SChristian Riesch * Avoid skb_copy_expand() expensive call, using following rules : 257b4cdae20SChristian Riesch * - We are allowed to push 4 bytes in headroom if skb_header_cloned() 258b4cdae20SChristian Riesch * is false (and if we have 4 bytes of headroom) 259b4cdae20SChristian Riesch * - We are allowed to put 4 bytes at tail if skb_cloned() 260b4cdae20SChristian Riesch * is false (and if we have 4 bytes of tailroom) 261b4cdae20SChristian Riesch * 262242c1a28SGao Feng * TCP packets for example are cloned, but __skb_header_release() 263b4cdae20SChristian Riesch * was called in tcp stack, allowing us to use headroom for our needs. 264b4cdae20SChristian Riesch */ 265b4cdae20SChristian Riesch if (!skb_header_cloned(skb) && 266b4cdae20SChristian Riesch !(padlen && skb_cloned(skb)) && 267b4cdae20SChristian Riesch headroom + tailroom >= 4 + padlen) { 268b4cdae20SChristian Riesch /* following should not happen, but better be safe */ 269b4cdae20SChristian Riesch if (headroom < 4 || 270b4cdae20SChristian Riesch tailroom < padlen) { 271b4cdae20SChristian Riesch skb->data = memmove(skb->head + 4, skb->data, skb->len); 272b4cdae20SChristian Riesch skb_set_tail_pointer(skb, skb->len); 273b4cdae20SChristian Riesch } 274b4cdae20SChristian Riesch } else { 275b4cdae20SChristian Riesch struct sk_buff *skb2; 276b4cdae20SChristian Riesch 277b4cdae20SChristian Riesch skb2 = skb_copy_expand(skb, 4, padlen, flags); 278b4cdae20SChristian Riesch dev_kfree_skb_any(skb); 279b4cdae20SChristian Riesch skb = skb2; 280b4cdae20SChristian Riesch if (!skb) 281b4cdae20SChristian Riesch return NULL; 282b4cdae20SChristian Riesch } 283b4cdae20SChristian Riesch 284b4cdae20SChristian Riesch packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len; 2857e24b4edSChuhong Yuan ptr = skb_push(skb, 4); 2867e24b4edSChuhong Yuan put_unaligned_le32(packet_len, ptr); 287b4cdae20SChristian Riesch 288b4cdae20SChristian Riesch if (padlen) { 2897e24b4edSChuhong Yuan put_unaligned_le32(padbytes, skb_tail_pointer(skb)); 290b4cdae20SChristian Riesch skb_put(skb, sizeof(padbytes)); 291b4cdae20SChristian Riesch } 2921e9e39f4SBen Hutchings 2937a1e890eSBen Hutchings usbnet_set_skb_tx_stats(skb, 1, 0); 294b4cdae20SChristian Riesch return skb; 295b4cdae20SChristian Riesch } 296b4cdae20SChristian Riesch 297d9fe64e5SRobert Foss int asix_set_sw_mii(struct usbnet *dev, int in_pm) 298b4cdae20SChristian Riesch { 299b4cdae20SChristian Riesch int ret; 300d9fe64e5SRobert Foss ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL, in_pm); 301d9fe64e5SRobert Foss 302b4cdae20SChristian Riesch if (ret < 0) 303b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to enable software MII access\n"); 304b4cdae20SChristian Riesch return ret; 305b4cdae20SChristian Riesch } 306b4cdae20SChristian Riesch 307d9fe64e5SRobert Foss int asix_set_hw_mii(struct usbnet *dev, int in_pm) 308b4cdae20SChristian Riesch { 309b4cdae20SChristian Riesch int ret; 310d9fe64e5SRobert Foss ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL, in_pm); 311b4cdae20SChristian Riesch if (ret < 0) 312b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to enable hardware MII access\n"); 313b4cdae20SChristian Riesch return ret; 314b4cdae20SChristian Riesch } 315b4cdae20SChristian Riesch 3167e88b11aSOleksij Rempel int asix_read_phy_addr(struct usbnet *dev, bool internal) 317b4cdae20SChristian Riesch { 3187e88b11aSOleksij Rempel int ret, offset; 319b4cdae20SChristian Riesch u8 buf[2]; 320b4cdae20SChristian Riesch 3217e88b11aSOleksij Rempel ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf, 0); 3227e88b11aSOleksij Rempel if (ret < 0) 3237e88b11aSOleksij Rempel goto error; 324b4cdae20SChristian Riesch 325a092b723SHimadri Pandya if (ret < 2) { 3267e88b11aSOleksij Rempel ret = -EIO; 3277e88b11aSOleksij Rempel goto error; 328b4cdae20SChristian Riesch } 3297e88b11aSOleksij Rempel 3307e88b11aSOleksij Rempel offset = (internal ? 1 : 0); 33116626b0cSChristian Riesch ret = buf[offset]; 332b4cdae20SChristian Riesch 3337e88b11aSOleksij Rempel netdev_dbg(dev->net, "%s PHY address 0x%x\n", 3347e88b11aSOleksij Rempel internal ? "internal" : "external", ret); 3357e88b11aSOleksij Rempel 3367e88b11aSOleksij Rempel return ret; 3377e88b11aSOleksij Rempel 3387e88b11aSOleksij Rempel error: 3397e88b11aSOleksij Rempel netdev_err(dev->net, "Error reading PHY_ID register: %02x\n", ret); 3407e88b11aSOleksij Rempel 341b4cdae20SChristian Riesch return ret; 342b4cdae20SChristian Riesch } 343b4cdae20SChristian Riesch 344d9fe64e5SRobert Foss int asix_sw_reset(struct usbnet *dev, u8 flags, int in_pm) 345b4cdae20SChristian Riesch { 346b4cdae20SChristian Riesch int ret; 347b4cdae20SChristian Riesch 348d9fe64e5SRobert Foss ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL, in_pm); 349b4cdae20SChristian Riesch if (ret < 0) 350b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to send software reset: %02x\n", ret); 351b4cdae20SChristian Riesch 352b4cdae20SChristian Riesch return ret; 353b4cdae20SChristian Riesch } 354b4cdae20SChristian Riesch 355d9fe64e5SRobert Foss u16 asix_read_rx_ctl(struct usbnet *dev, int in_pm) 356b4cdae20SChristian Riesch { 357b4cdae20SChristian Riesch __le16 v; 358d9fe64e5SRobert Foss int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v, in_pm); 359b4cdae20SChristian Riesch 360b4cdae20SChristian Riesch if (ret < 0) { 361b4cdae20SChristian Riesch netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret); 362b4cdae20SChristian Riesch goto out; 363b4cdae20SChristian Riesch } 364b4cdae20SChristian Riesch ret = le16_to_cpu(v); 365b4cdae20SChristian Riesch out: 366b4cdae20SChristian Riesch return ret; 367b4cdae20SChristian Riesch } 368b4cdae20SChristian Riesch 369d9fe64e5SRobert Foss int asix_write_rx_ctl(struct usbnet *dev, u16 mode, int in_pm) 370b4cdae20SChristian Riesch { 371b4cdae20SChristian Riesch int ret; 372b4cdae20SChristian Riesch 373b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode); 374d9fe64e5SRobert Foss ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL, in_pm); 375b4cdae20SChristian Riesch if (ret < 0) 376b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n", 377b4cdae20SChristian Riesch mode, ret); 378b4cdae20SChristian Riesch 379b4cdae20SChristian Riesch return ret; 380b4cdae20SChristian Riesch } 381b4cdae20SChristian Riesch 382d9fe64e5SRobert Foss u16 asix_read_medium_status(struct usbnet *dev, int in_pm) 383b4cdae20SChristian Riesch { 384b4cdae20SChristian Riesch __le16 v; 385d9fe64e5SRobert Foss int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS, 386d9fe64e5SRobert Foss 0, 0, 2, &v, in_pm); 387b4cdae20SChristian Riesch 388b4cdae20SChristian Riesch if (ret < 0) { 389b4cdae20SChristian Riesch netdev_err(dev->net, "Error reading Medium Status register: %02x\n", 390b4cdae20SChristian Riesch ret); 391b4cdae20SChristian Riesch return ret; /* TODO: callers not checking for error ret */ 392b4cdae20SChristian Riesch } 393b4cdae20SChristian Riesch 394b4cdae20SChristian Riesch return le16_to_cpu(v); 395b4cdae20SChristian Riesch 396b4cdae20SChristian Riesch } 397b4cdae20SChristian Riesch 398d9fe64e5SRobert Foss int asix_write_medium_mode(struct usbnet *dev, u16 mode, int in_pm) 399b4cdae20SChristian Riesch { 400b4cdae20SChristian Riesch int ret; 401b4cdae20SChristian Riesch 402b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode); 403d9fe64e5SRobert Foss ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, 404d9fe64e5SRobert Foss mode, 0, 0, NULL, in_pm); 405b4cdae20SChristian Riesch if (ret < 0) 406b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n", 407b4cdae20SChristian Riesch mode, ret); 408b4cdae20SChristian Riesch 409b4cdae20SChristian Riesch return ret; 410b4cdae20SChristian Riesch } 411b4cdae20SChristian Riesch 412e532a096SOleksij Rempel /* set MAC link settings according to information from phylib */ 413e532a096SOleksij Rempel void asix_adjust_link(struct net_device *netdev) 414e532a096SOleksij Rempel { 415e532a096SOleksij Rempel struct phy_device *phydev = netdev->phydev; 416e532a096SOleksij Rempel struct usbnet *dev = netdev_priv(netdev); 417e532a096SOleksij Rempel u16 mode = 0; 418e532a096SOleksij Rempel 419e532a096SOleksij Rempel if (phydev->link) { 420e532a096SOleksij Rempel mode = AX88772_MEDIUM_DEFAULT; 421e532a096SOleksij Rempel 422e532a096SOleksij Rempel if (phydev->duplex == DUPLEX_HALF) 423e532a096SOleksij Rempel mode &= ~AX_MEDIUM_FD; 424e532a096SOleksij Rempel 425e532a096SOleksij Rempel if (phydev->speed != SPEED_100) 426e532a096SOleksij Rempel mode &= ~AX_MEDIUM_PS; 427e532a096SOleksij Rempel } 428e532a096SOleksij Rempel 429e532a096SOleksij Rempel asix_write_medium_mode(dev, mode, 0); 430e532a096SOleksij Rempel phy_print_status(phydev); 431e532a096SOleksij Rempel } 432e532a096SOleksij Rempel 433d9fe64e5SRobert Foss int asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm) 434b4cdae20SChristian Riesch { 435b4cdae20SChristian Riesch int ret; 436b4cdae20SChristian Riesch 437b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value); 438d9fe64e5SRobert Foss ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL, in_pm); 439b4cdae20SChristian Riesch if (ret < 0) 440b4cdae20SChristian Riesch netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n", 441b4cdae20SChristian Riesch value, ret); 442b4cdae20SChristian Riesch 443b4cdae20SChristian Riesch if (sleep) 444b4cdae20SChristian Riesch msleep(sleep); 445b4cdae20SChristian Riesch 446b4cdae20SChristian Riesch return ret; 447b4cdae20SChristian Riesch } 448b4cdae20SChristian Riesch 449b4cdae20SChristian Riesch /* 450b4cdae20SChristian Riesch * AX88772 & AX88178 have a 16-bit RX_CTL value 451b4cdae20SChristian Riesch */ 452607740bcSChristian Riesch void asix_set_multicast(struct net_device *net) 453b4cdae20SChristian Riesch { 454b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net); 455b4cdae20SChristian Riesch struct asix_data *data = (struct asix_data *)&dev->data; 456b4cdae20SChristian Riesch u16 rx_ctl = AX_DEFAULT_RX_CTL; 457b4cdae20SChristian Riesch 458b4cdae20SChristian Riesch if (net->flags & IFF_PROMISC) { 459b4cdae20SChristian Riesch rx_ctl |= AX_RX_CTL_PRO; 460b4cdae20SChristian Riesch } else if (net->flags & IFF_ALLMULTI || 461b4cdae20SChristian Riesch netdev_mc_count(net) > AX_MAX_MCAST) { 462b4cdae20SChristian Riesch rx_ctl |= AX_RX_CTL_AMALL; 463b4cdae20SChristian Riesch } else if (netdev_mc_empty(net)) { 464b4cdae20SChristian Riesch /* just broadcast and directed */ 465b4cdae20SChristian Riesch } else { 466b4cdae20SChristian Riesch /* We use the 20 byte dev->data 467b4cdae20SChristian Riesch * for our 8 byte filter buffer 468b4cdae20SChristian Riesch * to avoid allocating memory that 469b4cdae20SChristian Riesch * is tricky to free later */ 470b4cdae20SChristian Riesch struct netdev_hw_addr *ha; 471b4cdae20SChristian Riesch u32 crc_bits; 472b4cdae20SChristian Riesch 473b4cdae20SChristian Riesch memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE); 474b4cdae20SChristian Riesch 475b4cdae20SChristian Riesch /* Build the multicast hash filter. */ 476b4cdae20SChristian Riesch netdev_for_each_mc_addr(ha, net) { 477b4cdae20SChristian Riesch crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26; 478b4cdae20SChristian Riesch data->multi_filter[crc_bits >> 3] |= 479b4cdae20SChristian Riesch 1 << (crc_bits & 7); 480b4cdae20SChristian Riesch } 481b4cdae20SChristian Riesch 482b4cdae20SChristian Riesch asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0, 483b4cdae20SChristian Riesch AX_MCAST_FILTER_SIZE, data->multi_filter); 484b4cdae20SChristian Riesch 485b4cdae20SChristian Riesch rx_ctl |= AX_RX_CTL_AM; 486b4cdae20SChristian Riesch } 487b4cdae20SChristian Riesch 488b4cdae20SChristian Riesch asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL); 489b4cdae20SChristian Riesch } 490b4cdae20SChristian Riesch 491607740bcSChristian Riesch int asix_mdio_read(struct net_device *netdev, int phy_id, int loc) 492b4cdae20SChristian Riesch { 493b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(netdev); 494b4cdae20SChristian Riesch __le16 res; 4958a46f665SRobert Foss int ret; 496b4cdae20SChristian Riesch 497b4cdae20SChristian Riesch mutex_lock(&dev->phy_mutex); 498a786e319SPavel Skripkin 499a786e319SPavel Skripkin ret = asix_check_host_enable(dev, 0); 500610df1d2SGuenter Roeck if (ret == -ENODEV || ret == -ETIMEDOUT) { 5018a46f665SRobert Foss mutex_unlock(&dev->phy_mutex); 5028a46f665SRobert Foss return ret; 5038a46f665SRobert Foss } 504d9fe64e5SRobert Foss 505d275afb6SOleksij Rempel ret = asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, 506d275afb6SOleksij Rempel &res, 0); 507d275afb6SOleksij Rempel if (ret < 0) 508d275afb6SOleksij Rempel goto out; 509d275afb6SOleksij Rempel 510d275afb6SOleksij Rempel ret = asix_set_hw_mii(dev, 0); 511d275afb6SOleksij Rempel out: 512b4cdae20SChristian Riesch mutex_unlock(&dev->phy_mutex); 513b4cdae20SChristian Riesch 514b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n", 515b4cdae20SChristian Riesch phy_id, loc, le16_to_cpu(res)); 516b4cdae20SChristian Riesch 517d275afb6SOleksij Rempel return ret < 0 ? ret : le16_to_cpu(res); 518b4cdae20SChristian Riesch } 519b4cdae20SChristian Riesch 520d275afb6SOleksij Rempel static int __asix_mdio_write(struct net_device *netdev, int phy_id, int loc, 521d275afb6SOleksij Rempel int val) 522b4cdae20SChristian Riesch { 523b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(netdev); 524b4cdae20SChristian Riesch __le16 res = cpu_to_le16(val); 5258a46f665SRobert Foss int ret; 526b4cdae20SChristian Riesch 527b4cdae20SChristian Riesch netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n", 528b4cdae20SChristian Riesch phy_id, loc, val); 529d9fe64e5SRobert Foss 530b4cdae20SChristian Riesch mutex_lock(&dev->phy_mutex); 531d275afb6SOleksij Rempel 532a786e319SPavel Skripkin ret = asix_check_host_enable(dev, 0); 533d275afb6SOleksij Rempel if (ret == -ENODEV) 534d275afb6SOleksij Rempel goto out; 535d275afb6SOleksij Rempel 536d275afb6SOleksij Rempel ret = asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, 537d275afb6SOleksij Rempel &res, 0); 538d275afb6SOleksij Rempel if (ret < 0) 539d275afb6SOleksij Rempel goto out; 540d275afb6SOleksij Rempel 541d275afb6SOleksij Rempel ret = asix_set_hw_mii(dev, 0); 542d275afb6SOleksij Rempel out: 5438a46f665SRobert Foss mutex_unlock(&dev->phy_mutex); 544d275afb6SOleksij Rempel 545d275afb6SOleksij Rempel return ret < 0 ? ret : 0; 5468a46f665SRobert Foss } 547d9fe64e5SRobert Foss 548d275afb6SOleksij Rempel void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val) 549d275afb6SOleksij Rempel { 550d275afb6SOleksij Rempel __asix_mdio_write(netdev, phy_id, loc, val); 551d9fe64e5SRobert Foss } 552d9fe64e5SRobert Foss 553e532a096SOleksij Rempel /* MDIO read and write wrappers for phylib */ 554e532a096SOleksij Rempel int asix_mdio_bus_read(struct mii_bus *bus, int phy_id, int regnum) 555e532a096SOleksij Rempel { 556e532a096SOleksij Rempel struct usbnet *priv = bus->priv; 557e532a096SOleksij Rempel 558e532a096SOleksij Rempel return asix_mdio_read(priv->net, phy_id, regnum); 559e532a096SOleksij Rempel } 560e532a096SOleksij Rempel 561e532a096SOleksij Rempel int asix_mdio_bus_write(struct mii_bus *bus, int phy_id, int regnum, u16 val) 562e532a096SOleksij Rempel { 563e532a096SOleksij Rempel struct usbnet *priv = bus->priv; 564e532a096SOleksij Rempel 565d275afb6SOleksij Rempel return __asix_mdio_write(priv->net, phy_id, regnum, val); 566e532a096SOleksij Rempel } 567e532a096SOleksij Rempel 568d9fe64e5SRobert Foss int asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc) 569d9fe64e5SRobert Foss { 570d9fe64e5SRobert Foss struct usbnet *dev = netdev_priv(netdev); 571d9fe64e5SRobert Foss __le16 res; 5728a46f665SRobert Foss int ret; 573d9fe64e5SRobert Foss 574d9fe64e5SRobert Foss mutex_lock(&dev->phy_mutex); 575a786e319SPavel Skripkin 576a786e319SPavel Skripkin ret = asix_check_host_enable(dev, 1); 577610df1d2SGuenter Roeck if (ret == -ENODEV || ret == -ETIMEDOUT) { 5788a46f665SRobert Foss mutex_unlock(&dev->phy_mutex); 5798a46f665SRobert Foss return ret; 5808a46f665SRobert Foss } 581d9fe64e5SRobert Foss 582d9fe64e5SRobert Foss asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, 583d9fe64e5SRobert Foss (__u16)loc, 2, &res, 1); 584d9fe64e5SRobert Foss asix_set_hw_mii(dev, 1); 585d9fe64e5SRobert Foss mutex_unlock(&dev->phy_mutex); 586d9fe64e5SRobert Foss 587d9fe64e5SRobert Foss netdev_dbg(dev->net, "asix_mdio_read_nopm() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n", 588d9fe64e5SRobert Foss phy_id, loc, le16_to_cpu(res)); 589d9fe64e5SRobert Foss 590d9fe64e5SRobert Foss return le16_to_cpu(res); 591d9fe64e5SRobert Foss } 592d9fe64e5SRobert Foss 593d9fe64e5SRobert Foss void 594d9fe64e5SRobert Foss asix_mdio_write_nopm(struct net_device *netdev, int phy_id, int loc, int val) 595d9fe64e5SRobert Foss { 596d9fe64e5SRobert Foss struct usbnet *dev = netdev_priv(netdev); 597d9fe64e5SRobert Foss __le16 res = cpu_to_le16(val); 5988a46f665SRobert Foss int ret; 599d9fe64e5SRobert Foss 600d9fe64e5SRobert Foss netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n", 601d9fe64e5SRobert Foss phy_id, loc, val); 602d9fe64e5SRobert Foss 603d9fe64e5SRobert Foss mutex_lock(&dev->phy_mutex); 604a786e319SPavel Skripkin 605a786e319SPavel Skripkin ret = asix_check_host_enable(dev, 1); 6068a46f665SRobert Foss if (ret == -ENODEV) { 6078a46f665SRobert Foss mutex_unlock(&dev->phy_mutex); 6088a46f665SRobert Foss return; 6098a46f665SRobert Foss } 610d9fe64e5SRobert Foss 611d9fe64e5SRobert Foss asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, 612d9fe64e5SRobert Foss (__u16)loc, 2, &res, 1); 613d9fe64e5SRobert Foss asix_set_hw_mii(dev, 1); 614b4cdae20SChristian Riesch mutex_unlock(&dev->phy_mutex); 615b4cdae20SChristian Riesch } 616b4cdae20SChristian Riesch 617607740bcSChristian Riesch void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo) 618b4cdae20SChristian Riesch { 619b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net); 620b4cdae20SChristian Riesch u8 opt; 621b4cdae20SChristian Riesch 622d9fe64e5SRobert Foss if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 623d9fe64e5SRobert Foss 0, 0, 1, &opt, 0) < 0) { 624b4cdae20SChristian Riesch wolinfo->supported = 0; 625b4cdae20SChristian Riesch wolinfo->wolopts = 0; 626b4cdae20SChristian Riesch return; 627b4cdae20SChristian Riesch } 628b4cdae20SChristian Riesch wolinfo->supported = WAKE_PHY | WAKE_MAGIC; 629b4cdae20SChristian Riesch wolinfo->wolopts = 0; 630b4cdae20SChristian Riesch if (opt & AX_MONITOR_LINK) 631b4cdae20SChristian Riesch wolinfo->wolopts |= WAKE_PHY; 632b4cdae20SChristian Riesch if (opt & AX_MONITOR_MAGIC) 633b4cdae20SChristian Riesch wolinfo->wolopts |= WAKE_MAGIC; 634b4cdae20SChristian Riesch } 635b4cdae20SChristian Riesch 636607740bcSChristian Riesch int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo) 637b4cdae20SChristian Riesch { 638b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net); 639b4cdae20SChristian Riesch u8 opt = 0; 640b4cdae20SChristian Riesch 641c4ce446eSFlorian Fainelli if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC)) 642c4ce446eSFlorian Fainelli return -EINVAL; 643c4ce446eSFlorian Fainelli 644b4cdae20SChristian Riesch if (wolinfo->wolopts & WAKE_PHY) 645b4cdae20SChristian Riesch opt |= AX_MONITOR_LINK; 646b4cdae20SChristian Riesch if (wolinfo->wolopts & WAKE_MAGIC) 647b4cdae20SChristian Riesch opt |= AX_MONITOR_MAGIC; 648b4cdae20SChristian Riesch 649b4cdae20SChristian Riesch if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE, 650d9fe64e5SRobert Foss opt, 0, 0, NULL, 0) < 0) 651b4cdae20SChristian Riesch return -EINVAL; 652b4cdae20SChristian Riesch 653b4cdae20SChristian Riesch return 0; 654b4cdae20SChristian Riesch } 655b4cdae20SChristian Riesch 656607740bcSChristian Riesch int asix_get_eeprom_len(struct net_device *net) 657b4cdae20SChristian Riesch { 658ceb02c91SChristian Riesch return AX_EEPROM_LEN; 659b4cdae20SChristian Riesch } 660b4cdae20SChristian Riesch 661607740bcSChristian Riesch int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom, 662607740bcSChristian Riesch u8 *data) 663b4cdae20SChristian Riesch { 664b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net); 665ceb02c91SChristian Riesch u16 *eeprom_buff; 666ceb02c91SChristian Riesch int first_word, last_word; 667b4cdae20SChristian Riesch int i; 668b4cdae20SChristian Riesch 669ceb02c91SChristian Riesch if (eeprom->len == 0) 670b4cdae20SChristian Riesch return -EINVAL; 671b4cdae20SChristian Riesch 672b4cdae20SChristian Riesch eeprom->magic = AX_EEPROM_MAGIC; 673b4cdae20SChristian Riesch 674ceb02c91SChristian Riesch first_word = eeprom->offset >> 1; 675ceb02c91SChristian Riesch last_word = (eeprom->offset + eeprom->len - 1) >> 1; 676ceb02c91SChristian Riesch 6776da2ec56SKees Cook eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16), 678ceb02c91SChristian Riesch GFP_KERNEL); 679ceb02c91SChristian Riesch if (!eeprom_buff) 680ceb02c91SChristian Riesch return -ENOMEM; 681ceb02c91SChristian Riesch 682b4cdae20SChristian Riesch /* ax8817x returns 2 bytes from eeprom on read */ 683ceb02c91SChristian Riesch for (i = first_word; i <= last_word; i++) { 684ceb02c91SChristian Riesch if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2, 685d9fe64e5SRobert Foss &eeprom_buff[i - first_word], 0) < 0) { 686ceb02c91SChristian Riesch kfree(eeprom_buff); 687ceb02c91SChristian Riesch return -EIO; 688b4cdae20SChristian Riesch } 689ceb02c91SChristian Riesch } 690ceb02c91SChristian Riesch 691ceb02c91SChristian Riesch memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len); 692ceb02c91SChristian Riesch kfree(eeprom_buff); 693b4cdae20SChristian Riesch return 0; 694b4cdae20SChristian Riesch } 695b4cdae20SChristian Riesch 696cb7b24cdSChristian Riesch int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom, 697cb7b24cdSChristian Riesch u8 *data) 698cb7b24cdSChristian Riesch { 699cb7b24cdSChristian Riesch struct usbnet *dev = netdev_priv(net); 700cb7b24cdSChristian Riesch u16 *eeprom_buff; 701cb7b24cdSChristian Riesch int first_word, last_word; 702cb7b24cdSChristian Riesch int i; 703cb7b24cdSChristian Riesch int ret; 704cb7b24cdSChristian Riesch 705cb7b24cdSChristian Riesch netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n", 706cb7b24cdSChristian Riesch eeprom->len, eeprom->offset, eeprom->magic); 707cb7b24cdSChristian Riesch 708cb7b24cdSChristian Riesch if (eeprom->len == 0) 709cb7b24cdSChristian Riesch return -EINVAL; 710cb7b24cdSChristian Riesch 711cb7b24cdSChristian Riesch if (eeprom->magic != AX_EEPROM_MAGIC) 712cb7b24cdSChristian Riesch return -EINVAL; 713cb7b24cdSChristian Riesch 714cb7b24cdSChristian Riesch first_word = eeprom->offset >> 1; 715cb7b24cdSChristian Riesch last_word = (eeprom->offset + eeprom->len - 1) >> 1; 716cb7b24cdSChristian Riesch 7176da2ec56SKees Cook eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16), 718cb7b24cdSChristian Riesch GFP_KERNEL); 719cb7b24cdSChristian Riesch if (!eeprom_buff) 720cb7b24cdSChristian Riesch return -ENOMEM; 721cb7b24cdSChristian Riesch 722cb7b24cdSChristian Riesch /* align data to 16 bit boundaries, read the missing data from 723cb7b24cdSChristian Riesch the EEPROM */ 724cb7b24cdSChristian Riesch if (eeprom->offset & 1) { 725cb7b24cdSChristian Riesch ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2, 726d9fe64e5SRobert Foss &eeprom_buff[0], 0); 727cb7b24cdSChristian Riesch if (ret < 0) { 728cb7b24cdSChristian Riesch netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word); 729cb7b24cdSChristian Riesch goto free; 730cb7b24cdSChristian Riesch } 731cb7b24cdSChristian Riesch } 732cb7b24cdSChristian Riesch 733cb7b24cdSChristian Riesch if ((eeprom->offset + eeprom->len) & 1) { 734cb7b24cdSChristian Riesch ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2, 735d9fe64e5SRobert Foss &eeprom_buff[last_word - first_word], 0); 736cb7b24cdSChristian Riesch if (ret < 0) { 737cb7b24cdSChristian Riesch netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word); 738cb7b24cdSChristian Riesch goto free; 739cb7b24cdSChristian Riesch } 740cb7b24cdSChristian Riesch } 741cb7b24cdSChristian Riesch 742cb7b24cdSChristian Riesch memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len); 743cb7b24cdSChristian Riesch 744cb7b24cdSChristian Riesch /* write data to EEPROM */ 745d9fe64e5SRobert Foss ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL, 0); 746cb7b24cdSChristian Riesch if (ret < 0) { 747cb7b24cdSChristian Riesch netdev_err(net, "Failed to enable EEPROM write\n"); 748cb7b24cdSChristian Riesch goto free; 749cb7b24cdSChristian Riesch } 750cb7b24cdSChristian Riesch msleep(20); 751cb7b24cdSChristian Riesch 752cb7b24cdSChristian Riesch for (i = first_word; i <= last_word; i++) { 753cb7b24cdSChristian Riesch netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n", 754cb7b24cdSChristian Riesch i, eeprom_buff[i - first_word]); 755cb7b24cdSChristian Riesch ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i, 756d9fe64e5SRobert Foss eeprom_buff[i - first_word], 0, NULL, 0); 757cb7b24cdSChristian Riesch if (ret < 0) { 758cb7b24cdSChristian Riesch netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n", 759cb7b24cdSChristian Riesch i); 760cb7b24cdSChristian Riesch goto free; 761cb7b24cdSChristian Riesch } 762cb7b24cdSChristian Riesch msleep(20); 763cb7b24cdSChristian Riesch } 764cb7b24cdSChristian Riesch 765d9fe64e5SRobert Foss ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL, 0); 766cb7b24cdSChristian Riesch if (ret < 0) { 767cb7b24cdSChristian Riesch netdev_err(net, "Failed to disable EEPROM write\n"); 768cb7b24cdSChristian Riesch goto free; 769cb7b24cdSChristian Riesch } 770cb7b24cdSChristian Riesch 771cb7b24cdSChristian Riesch ret = 0; 772cb7b24cdSChristian Riesch free: 773cb7b24cdSChristian Riesch kfree(eeprom_buff); 774cb7b24cdSChristian Riesch return ret; 775cb7b24cdSChristian Riesch } 776cb7b24cdSChristian Riesch 777607740bcSChristian Riesch void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info) 778b4cdae20SChristian Riesch { 779b4cdae20SChristian Riesch /* Inherit standard device info */ 780b4cdae20SChristian Riesch usbnet_get_drvinfo(net, info); 7817826d43fSJiri Pirko strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver)); 7827826d43fSJiri Pirko strlcpy(info->version, DRIVER_VERSION, sizeof(info->version)); 783b4cdae20SChristian Riesch } 784b4cdae20SChristian Riesch 785607740bcSChristian Riesch int asix_set_mac_address(struct net_device *net, void *p) 786b4cdae20SChristian Riesch { 787b4cdae20SChristian Riesch struct usbnet *dev = netdev_priv(net); 788b4cdae20SChristian Riesch struct asix_data *data = (struct asix_data *)&dev->data; 789b4cdae20SChristian Riesch struct sockaddr *addr = p; 790b4cdae20SChristian Riesch 791b4cdae20SChristian Riesch if (netif_running(net)) 792b4cdae20SChristian Riesch return -EBUSY; 793b4cdae20SChristian Riesch if (!is_valid_ether_addr(addr->sa_data)) 794b4cdae20SChristian Riesch return -EADDRNOTAVAIL; 795b4cdae20SChristian Riesch 79616813717SJakub Kicinski eth_hw_addr_set(net, addr->sa_data); 797b4cdae20SChristian Riesch 798b4cdae20SChristian Riesch /* We use the 20 byte dev->data 799b4cdae20SChristian Riesch * for our 6 byte mac buffer 800b4cdae20SChristian Riesch * to avoid allocating memory that 801b4cdae20SChristian Riesch * is tricky to free later */ 802b4cdae20SChristian Riesch memcpy(data->mac_addr, addr->sa_data, ETH_ALEN); 803b4cdae20SChristian Riesch asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN, 804b4cdae20SChristian Riesch data->mac_addr); 805b4cdae20SChristian Riesch 806b4cdae20SChristian Riesch return 0; 807b4cdae20SChristian Riesch } 808