Thomas Gleixner | 1ccea77 | 2019-05-19 15:51:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 2 | /* |
Andreas Mohr | bbccc16 | 2010-12-20 10:38:47 -0800 | [diff] [blame] | 3 | * MOSCHIP MCS7830 based (7730/7830/7832) USB 2.0 Ethernet Devices |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 4 | * |
| 5 | * based on usbnet.c, asix.c and the vendor provided mcs7830 driver |
| 6 | * |
Andreas Mohr | 7680285 | 2010-01-31 02:58:19 +0000 | [diff] [blame] | 7 | * Copyright (C) 2010 Andreas Mohr <andi@lisas.de> |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 8 | * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de> |
| 9 | * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com> |
| 10 | * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net> |
| 11 | * Copyright (c) 2002-2003 TiVo Inc. |
| 12 | * |
Andreas Mohr | 7680285 | 2010-01-31 02:58:19 +0000 | [diff] [blame] | 13 | * Definitions gathered from MOSCHIP, Data Sheet_7830DA.pdf (thanks!). |
| 14 | * |
Andreas Mohr | bbccc16 | 2010-12-20 10:38:47 -0800 | [diff] [blame] | 15 | * 2010-12-19: add 7832 USB PID ("functionality same as MCS7830"), |
| 16 | * per active notification by manufacturer |
| 17 | * |
Andreas Mohr | 7680285 | 2010-01-31 02:58:19 +0000 | [diff] [blame] | 18 | * TODO: |
Andreas Mohr | 7680285 | 2010-01-31 02:58:19 +0000 | [diff] [blame] | 19 | * - support HIF_REG_CONFIG_SLEEPMODE/HIF_REG_CONFIG_TXENABLE (via autopm?) |
| 20 | * - implement ethtool_ops get_pauseparam/set_pauseparam |
| 21 | * via HIF_REG_PAUSE_THRESHOLD (>= revision C only!) |
| 22 | * - implement get_eeprom/[set_eeprom] |
| 23 | * - switch PHY on/off on ifup/ifdown (perhaps in usbnet.c, via MII) |
| 24 | * - mcs7830_get_regs() handling is weird: for rev 2 we return 32 regs, |
| 25 | * can access only ~ 24, remaining user buffer is uninitialized garbage |
| 26 | * - anything else? |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include <linux/crc32.h> |
| 30 | #include <linux/etherdevice.h> |
| 31 | #include <linux/ethtool.h> |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 32 | #include <linux/mii.h> |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/netdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 36 | #include <linux/usb.h> |
Jussi Kivilinna | 3692e94 | 2008-01-26 00:51:45 +0200 | [diff] [blame] | 37 | #include <linux/usb/usbnet.h> |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 38 | |
| 39 | /* requests */ |
| 40 | #define MCS7830_RD_BMREQ (USB_DIR_IN | USB_TYPE_VENDOR | \ |
| 41 | USB_RECIP_DEVICE) |
| 42 | #define MCS7830_WR_BMREQ (USB_DIR_OUT | USB_TYPE_VENDOR | \ |
| 43 | USB_RECIP_DEVICE) |
| 44 | #define MCS7830_RD_BREQ 0x0E |
| 45 | #define MCS7830_WR_BREQ 0x0D |
| 46 | |
| 47 | #define MCS7830_CTRL_TIMEOUT 1000 |
| 48 | #define MCS7830_MAX_MCAST 64 |
| 49 | |
| 50 | #define MCS7830_VENDOR_ID 0x9710 |
Andreas Mohr | bbccc16 | 2010-12-20 10:38:47 -0800 | [diff] [blame] | 51 | #define MCS7832_PRODUCT_ID 0x7832 |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 52 | #define MCS7830_PRODUCT_ID 0x7830 |
Arnd Bergmann | 8382cc1 | 2008-08-23 22:02:23 +0200 | [diff] [blame] | 53 | #define MCS7730_PRODUCT_ID 0x7730 |
| 54 | |
| 55 | #define SITECOM_VENDOR_ID 0x0DF6 |
| 56 | #define LN_030_PRODUCT_ID 0x0021 |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 57 | |
| 58 | #define MCS7830_MII_ADVERTISE (ADVERTISE_PAUSE_CAP | ADVERTISE_100FULL | \ |
| 59 | ADVERTISE_100HALF | ADVERTISE_10FULL | \ |
| 60 | ADVERTISE_10HALF | ADVERTISE_CSMA) |
| 61 | |
Andreas Mohr | 00e4991 | 2010-01-31 02:58:26 +0000 | [diff] [blame] | 62 | /* HIF_REG_XX corresponding index value */ |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 63 | enum { |
| 64 | HIF_REG_MULTICAST_HASH = 0x00, |
| 65 | HIF_REG_PACKET_GAP1 = 0x08, |
| 66 | HIF_REG_PACKET_GAP2 = 0x09, |
| 67 | HIF_REG_PHY_DATA = 0x0a, |
| 68 | HIF_REG_PHY_CMD1 = 0x0c, |
| 69 | HIF_REG_PHY_CMD1_READ = 0x40, |
| 70 | HIF_REG_PHY_CMD1_WRITE = 0x20, |
| 71 | HIF_REG_PHY_CMD1_PHYADDR = 0x01, |
| 72 | HIF_REG_PHY_CMD2 = 0x0d, |
| 73 | HIF_REG_PHY_CMD2_PEND_FLAG_BIT = 0x80, |
| 74 | HIF_REG_PHY_CMD2_READY_FLAG_BIT = 0x40, |
| 75 | HIF_REG_CONFIG = 0x0e, |
Andreas Mohr | c774651 | 2010-01-31 02:58:33 +0000 | [diff] [blame] | 76 | /* hmm, spec sez: "R/W", "Except bit 3" (likely TXENABLE). */ |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 77 | HIF_REG_CONFIG_CFG = 0x80, |
| 78 | HIF_REG_CONFIG_SPEED100 = 0x40, |
| 79 | HIF_REG_CONFIG_FULLDUPLEX_ENABLE = 0x20, |
| 80 | HIF_REG_CONFIG_RXENABLE = 0x10, |
| 81 | HIF_REG_CONFIG_TXENABLE = 0x08, |
| 82 | HIF_REG_CONFIG_SLEEPMODE = 0x04, |
| 83 | HIF_REG_CONFIG_ALLMULTICAST = 0x02, |
Andreas Mohr | 00e4991 | 2010-01-31 02:58:26 +0000 | [diff] [blame] | 84 | HIF_REG_CONFIG_PROMISCUOUS = 0x01, |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 85 | HIF_REG_ETHERNET_ADDR = 0x0f, |
Andreas Mohr | c774651 | 2010-01-31 02:58:33 +0000 | [diff] [blame] | 86 | HIF_REG_FRAME_DROP_COUNTER = 0x15, /* 0..ff; reset: 0 */ |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 87 | HIF_REG_PAUSE_THRESHOLD = 0x16, |
| 88 | HIF_REG_PAUSE_THRESHOLD_DEFAULT = 0, |
| 89 | }; |
| 90 | |
Andreas Mohr | 7680285 | 2010-01-31 02:58:19 +0000 | [diff] [blame] | 91 | /* Trailing status byte in Ethernet Rx frame */ |
| 92 | enum { |
| 93 | MCS7830_RX_SHORT_FRAME = 0x01, /* < 64 bytes */ |
| 94 | MCS7830_RX_LENGTH_ERROR = 0x02, /* framelen != Ethernet length field */ |
| 95 | MCS7830_RX_ALIGNMENT_ERROR = 0x04, /* non-even number of nibbles */ |
| 96 | MCS7830_RX_CRC_ERROR = 0x08, |
| 97 | MCS7830_RX_LARGE_FRAME = 0x10, /* > 1518 bytes */ |
| 98 | MCS7830_RX_FRAME_CORRECT = 0x20, /* frame is correct */ |
| 99 | /* [7:6] reserved */ |
| 100 | }; |
| 101 | |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 102 | struct mcs7830_data { |
| 103 | u8 multi_filter[8]; |
| 104 | u8 config; |
| 105 | }; |
| 106 | |
| 107 | static const char driver_name[] = "MOSCHIP usb-ethernet driver"; |
| 108 | |
| 109 | static int mcs7830_get_reg(struct usbnet *dev, u16 index, u16 size, void *data) |
| 110 | { |
Ming Lei | 9c06a2b | 2012-10-24 19:46:59 +0000 | [diff] [blame] | 111 | return usbnet_read_cmd(dev, MCS7830_RD_BREQ, MCS7830_RD_BMREQ, |
| 112 | 0x0000, index, data, size); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 115 | static int mcs7830_set_reg(struct usbnet *dev, u16 index, u16 size, const void *data) |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 116 | { |
Ming Lei | 9c06a2b | 2012-10-24 19:46:59 +0000 | [diff] [blame] | 117 | return usbnet_write_cmd(dev, MCS7830_WR_BREQ, MCS7830_WR_BMREQ, |
| 118 | 0x0000, index, data, size); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void mcs7830_set_reg_async(struct usbnet *dev, u16 index, u16 size, void *data) |
| 122 | { |
Ming Lei | 9c06a2b | 2012-10-24 19:46:59 +0000 | [diff] [blame] | 123 | usbnet_write_cmd_async(dev, MCS7830_WR_BREQ, MCS7830_WR_BMREQ, |
| 124 | 0x0000, index, data, size); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 127 | static int mcs7830_hif_get_mac_address(struct usbnet *dev, unsigned char *addr) |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 128 | { |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 129 | int ret = mcs7830_get_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 130 | if (ret < 0) |
| 131 | return ret; |
| 132 | return 0; |
| 133 | } |
| 134 | |
Jakub Kicinski | 7666075 | 2021-10-14 07:24:31 -0700 | [diff] [blame] | 135 | static int mcs7830_hif_set_mac_address(struct usbnet *dev, |
| 136 | const unsigned char *addr) |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 137 | { |
| 138 | int ret = mcs7830_set_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr); |
| 139 | |
| 140 | if (ret < 0) |
| 141 | return ret; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int mcs7830_set_mac_address(struct net_device *netdev, void *p) |
| 146 | { |
| 147 | int ret; |
| 148 | struct usbnet *dev = netdev_priv(netdev); |
| 149 | struct sockaddr *addr = p; |
| 150 | |
| 151 | if (netif_running(netdev)) |
| 152 | return -EBUSY; |
| 153 | |
| 154 | if (!is_valid_ether_addr(addr->sa_data)) |
Danny Kukawka | 15b8f4c | 2012-02-21 02:07:50 +0000 | [diff] [blame] | 155 | return -EADDRNOTAVAIL; |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 156 | |
| 157 | ret = mcs7830_hif_set_mac_address(dev, addr->sa_data); |
| 158 | |
| 159 | if (ret < 0) |
| 160 | return ret; |
| 161 | |
| 162 | /* it worked --> adopt it on netdev side */ |
Jakub Kicinski | 49ed8dd | 2021-10-04 09:05:22 -0700 | [diff] [blame] | 163 | eth_hw_addr_set(netdev, addr->sa_data); |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 168 | static int mcs7830_read_phy(struct usbnet *dev, u8 index) |
| 169 | { |
| 170 | int ret; |
| 171 | int i; |
| 172 | __le16 val; |
| 173 | |
| 174 | u8 cmd[2] = { |
| 175 | HIF_REG_PHY_CMD1_READ | HIF_REG_PHY_CMD1_PHYADDR, |
| 176 | HIF_REG_PHY_CMD2_PEND_FLAG_BIT | index, |
| 177 | }; |
| 178 | |
Arnd Bergmann | a9fc633 | 2006-10-09 00:08:02 +0200 | [diff] [blame] | 179 | mutex_lock(&dev->phy_mutex); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 180 | /* write the MII command */ |
| 181 | ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd); |
| 182 | if (ret < 0) |
| 183 | goto out; |
| 184 | |
| 185 | /* wait for the data to become valid, should be within < 1ms */ |
| 186 | for (i = 0; i < 10; i++) { |
| 187 | ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd); |
| 188 | if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT)) |
| 189 | break; |
| 190 | ret = -EIO; |
| 191 | msleep(1); |
| 192 | } |
| 193 | if (ret < 0) |
| 194 | goto out; |
| 195 | |
| 196 | /* read actual register contents */ |
| 197 | ret = mcs7830_get_reg(dev, HIF_REG_PHY_DATA, 2, &val); |
| 198 | if (ret < 0) |
| 199 | goto out; |
| 200 | ret = le16_to_cpu(val); |
| 201 | dev_dbg(&dev->udev->dev, "read PHY reg %02x: %04x (%d tries)\n", |
| 202 | index, val, i); |
| 203 | out: |
Arnd Bergmann | a9fc633 | 2006-10-09 00:08:02 +0200 | [diff] [blame] | 204 | mutex_unlock(&dev->phy_mutex); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | static int mcs7830_write_phy(struct usbnet *dev, u8 index, u16 val) |
| 209 | { |
| 210 | int ret; |
| 211 | int i; |
| 212 | __le16 le_val; |
| 213 | |
| 214 | u8 cmd[2] = { |
| 215 | HIF_REG_PHY_CMD1_WRITE | HIF_REG_PHY_CMD1_PHYADDR, |
| 216 | HIF_REG_PHY_CMD2_PEND_FLAG_BIT | (index & 0x1F), |
| 217 | }; |
| 218 | |
Arnd Bergmann | a9fc633 | 2006-10-09 00:08:02 +0200 | [diff] [blame] | 219 | mutex_lock(&dev->phy_mutex); |
| 220 | |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 221 | /* write the new register contents */ |
| 222 | le_val = cpu_to_le16(val); |
| 223 | ret = mcs7830_set_reg(dev, HIF_REG_PHY_DATA, 2, &le_val); |
| 224 | if (ret < 0) |
| 225 | goto out; |
| 226 | |
| 227 | /* write the MII command */ |
| 228 | ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd); |
| 229 | if (ret < 0) |
| 230 | goto out; |
| 231 | |
| 232 | /* wait for the command to be accepted by the PHY */ |
| 233 | for (i = 0; i < 10; i++) { |
| 234 | ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd); |
| 235 | if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT)) |
| 236 | break; |
| 237 | ret = -EIO; |
| 238 | msleep(1); |
| 239 | } |
| 240 | if (ret < 0) |
| 241 | goto out; |
| 242 | |
| 243 | ret = 0; |
| 244 | dev_dbg(&dev->udev->dev, "write PHY reg %02x: %04x (%d tries)\n", |
| 245 | index, val, i); |
| 246 | out: |
Arnd Bergmann | a9fc633 | 2006-10-09 00:08:02 +0200 | [diff] [blame] | 247 | mutex_unlock(&dev->phy_mutex); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * This algorithm comes from the original mcs7830 version 1.4 driver, |
| 253 | * not sure if it is needed. |
| 254 | */ |
| 255 | static int mcs7830_set_autoneg(struct usbnet *dev, int ptrUserPhyMode) |
| 256 | { |
| 257 | int ret; |
| 258 | /* Enable all media types */ |
| 259 | ret = mcs7830_write_phy(dev, MII_ADVERTISE, MCS7830_MII_ADVERTISE); |
| 260 | |
| 261 | /* First reset BMCR */ |
| 262 | if (!ret) |
| 263 | ret = mcs7830_write_phy(dev, MII_BMCR, 0x0000); |
| 264 | /* Enable Auto Neg */ |
| 265 | if (!ret) |
| 266 | ret = mcs7830_write_phy(dev, MII_BMCR, BMCR_ANENABLE); |
| 267 | /* Restart Auto Neg (Keep the Enable Auto Neg Bit Set) */ |
| 268 | if (!ret) |
| 269 | ret = mcs7830_write_phy(dev, MII_BMCR, |
| 270 | BMCR_ANENABLE | BMCR_ANRESTART ); |
Dan Carpenter | 0e214ad | 2010-12-17 03:25:43 +0000 | [diff] [blame] | 271 | return ret; |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
| 275 | /* |
| 276 | * if we can read register 22, the chip revision is C or higher |
| 277 | */ |
| 278 | static int mcs7830_get_rev(struct usbnet *dev) |
| 279 | { |
| 280 | u8 dummy[2]; |
| 281 | int ret; |
Andreas Mohr | c774651 | 2010-01-31 02:58:33 +0000 | [diff] [blame] | 282 | ret = mcs7830_get_reg(dev, HIF_REG_FRAME_DROP_COUNTER, 2, dummy); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 283 | if (ret > 0) |
| 284 | return 2; /* Rev C or later */ |
| 285 | return 1; /* earlier revision */ |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * On rev. C we need to set the pause threshold |
| 290 | */ |
| 291 | static void mcs7830_rev_C_fixup(struct usbnet *dev) |
| 292 | { |
| 293 | u8 pause_threshold = HIF_REG_PAUSE_THRESHOLD_DEFAULT; |
| 294 | int retry; |
| 295 | |
| 296 | for (retry = 0; retry < 2; retry++) { |
| 297 | if (mcs7830_get_rev(dev) == 2) { |
| 298 | dev_info(&dev->udev->dev, "applying rev.C fixup\n"); |
| 299 | mcs7830_set_reg(dev, HIF_REG_PAUSE_THRESHOLD, |
| 300 | 1, &pause_threshold); |
| 301 | } |
| 302 | msleep(1); |
| 303 | } |
| 304 | } |
| 305 | |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 306 | static int mcs7830_mdio_read(struct net_device *netdev, int phy_id, |
| 307 | int location) |
| 308 | { |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 309 | struct usbnet *dev = netdev_priv(netdev); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 310 | return mcs7830_read_phy(dev, location); |
| 311 | } |
| 312 | |
| 313 | static void mcs7830_mdio_write(struct net_device *netdev, int phy_id, |
| 314 | int location, int val) |
| 315 | { |
Wang Chen | 8f15ea4 | 2008-11-12 23:38:36 -0800 | [diff] [blame] | 316 | struct usbnet *dev = netdev_priv(netdev); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 317 | mcs7830_write_phy(dev, location, val); |
| 318 | } |
| 319 | |
| 320 | static int mcs7830_ioctl(struct net_device *net, struct ifreq *rq, int cmd) |
| 321 | { |
| 322 | struct usbnet *dev = netdev_priv(net); |
| 323 | return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL); |
| 324 | } |
| 325 | |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 326 | static inline struct mcs7830_data *mcs7830_get_data(struct usbnet *dev) |
| 327 | { |
| 328 | return (struct mcs7830_data *)&dev->data; |
| 329 | } |
| 330 | |
| 331 | static void mcs7830_hif_update_multicast_hash(struct usbnet *dev) |
| 332 | { |
| 333 | struct mcs7830_data *data = mcs7830_get_data(dev); |
| 334 | mcs7830_set_reg_async(dev, HIF_REG_MULTICAST_HASH, |
| 335 | sizeof data->multi_filter, |
| 336 | data->multi_filter); |
| 337 | } |
| 338 | |
| 339 | static void mcs7830_hif_update_config(struct usbnet *dev) |
| 340 | { |
| 341 | /* implementation specific to data->config |
| 342 | (argument needs to be heap-based anyway - USB DMA!) */ |
| 343 | struct mcs7830_data *data = mcs7830_get_data(dev); |
| 344 | mcs7830_set_reg_async(dev, HIF_REG_CONFIG, 1, &data->config); |
| 345 | } |
| 346 | |
| 347 | static void mcs7830_data_set_multicast(struct net_device *net) |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 348 | { |
| 349 | struct usbnet *dev = netdev_priv(net); |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 350 | struct mcs7830_data *data = mcs7830_get_data(dev); |
| 351 | |
| 352 | memset(data->multi_filter, 0, sizeof data->multi_filter); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 353 | |
| 354 | data->config = HIF_REG_CONFIG_TXENABLE; |
| 355 | |
| 356 | /* this should not be needed, but it doesn't work otherwise */ |
| 357 | data->config |= HIF_REG_CONFIG_ALLMULTICAST; |
| 358 | |
| 359 | if (net->flags & IFF_PROMISC) { |
Andreas Mohr | 00e4991 | 2010-01-31 02:58:26 +0000 | [diff] [blame] | 360 | data->config |= HIF_REG_CONFIG_PROMISCUOUS; |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 361 | } else if (net->flags & IFF_ALLMULTI || |
Jiri Pirko | 4cd24ea | 2010-02-08 04:30:35 +0000 | [diff] [blame] | 362 | netdev_mc_count(net) > MCS7830_MAX_MCAST) { |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 363 | data->config |= HIF_REG_CONFIG_ALLMULTICAST; |
Jiri Pirko | 4cd24ea | 2010-02-08 04:30:35 +0000 | [diff] [blame] | 364 | } else if (netdev_mc_empty(net)) { |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 365 | /* just broadcast and directed */ |
| 366 | } else { |
| 367 | /* We use the 20 byte dev->data |
| 368 | * for our 8 byte filter buffer |
| 369 | * to avoid allocating memory that |
| 370 | * is tricky to free later */ |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 371 | struct netdev_hw_addr *ha; |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 372 | u32 crc_bits; |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 373 | |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 374 | /* Build the multicast hash filter. */ |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 375 | netdev_for_each_mc_addr(ha, net) { |
| 376 | crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26; |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 377 | data->multi_filter[crc_bits >> 3] |= 1 << (crc_bits & 7); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 378 | } |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 381 | |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 382 | static int mcs7830_apply_base_config(struct usbnet *dev) |
| 383 | { |
| 384 | int ret; |
| 385 | |
| 386 | /* re-configure known MAC (suspend case etc.) */ |
| 387 | ret = mcs7830_hif_set_mac_address(dev, dev->net->dev_addr); |
| 388 | if (ret) { |
| 389 | dev_info(&dev->udev->dev, "Cannot set MAC address\n"); |
| 390 | goto out; |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 391 | } |
| 392 | |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 393 | /* Set up PHY */ |
| 394 | ret = mcs7830_set_autoneg(dev, 0); |
| 395 | if (ret) { |
| 396 | dev_info(&dev->udev->dev, "Cannot set autoneg\n"); |
| 397 | goto out; |
| 398 | } |
| 399 | |
| 400 | mcs7830_hif_update_multicast_hash(dev); |
| 401 | mcs7830_hif_update_config(dev); |
| 402 | |
| 403 | mcs7830_rev_C_fixup(dev); |
| 404 | ret = 0; |
| 405 | out: |
| 406 | return ret; |
| 407 | } |
| 408 | |
| 409 | /* credits go to asix_set_multicast */ |
| 410 | static void mcs7830_set_multicast(struct net_device *net) |
| 411 | { |
| 412 | struct usbnet *dev = netdev_priv(net); |
| 413 | |
| 414 | mcs7830_data_set_multicast(net); |
| 415 | |
| 416 | mcs7830_hif_update_multicast_hash(dev); |
| 417 | mcs7830_hif_update_config(dev); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | static int mcs7830_get_regs_len(struct net_device *net) |
| 421 | { |
| 422 | struct usbnet *dev = netdev_priv(net); |
| 423 | |
| 424 | switch (mcs7830_get_rev(dev)) { |
| 425 | case 1: |
| 426 | return 21; |
| 427 | case 2: |
| 428 | return 32; |
| 429 | } |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static void mcs7830_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *drvinfo) |
| 434 | { |
| 435 | usbnet_get_drvinfo(net, drvinfo); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | static void mcs7830_get_regs(struct net_device *net, struct ethtool_regs *regs, void *data) |
| 439 | { |
| 440 | struct usbnet *dev = netdev_priv(net); |
| 441 | |
| 442 | regs->version = mcs7830_get_rev(dev); |
| 443 | mcs7830_get_reg(dev, 0, regs->len, data); |
| 444 | } |
| 445 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 446 | static const struct ethtool_ops mcs7830_ethtool_ops = { |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 447 | .get_drvinfo = mcs7830_get_drvinfo, |
| 448 | .get_regs_len = mcs7830_get_regs_len, |
| 449 | .get_regs = mcs7830_get_regs, |
| 450 | |
| 451 | /* common usbnet calls */ |
Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 452 | .get_link = usbnet_get_link, |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 453 | .get_msglevel = usbnet_get_msglevel, |
| 454 | .set_msglevel = usbnet_set_msglevel, |
Arnd Bergmann | c41286f | 2006-10-09 00:08:01 +0200 | [diff] [blame] | 455 | .nway_reset = usbnet_nway_reset, |
Oliver Neukum | 7765190 | 2021-04-05 16:13:41 -0700 | [diff] [blame] | 456 | .get_link_ksettings = usbnet_get_link_ksettings_mii, |
| 457 | .set_link_ksettings = usbnet_set_link_ksettings_mii, |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 458 | }; |
| 459 | |
Stephen Hemminger | e12c4f8 | 2009-03-20 19:35:57 +0000 | [diff] [blame] | 460 | static const struct net_device_ops mcs7830_netdev_ops = { |
| 461 | .ndo_open = usbnet_open, |
| 462 | .ndo_stop = usbnet_stop, |
| 463 | .ndo_start_xmit = usbnet_start_xmit, |
| 464 | .ndo_tx_timeout = usbnet_tx_timeout, |
| 465 | .ndo_change_mtu = usbnet_change_mtu, |
Heiner Kallweit | 323955a | 2020-11-10 20:51:03 +0100 | [diff] [blame] | 466 | .ndo_get_stats64 = dev_get_tstats64, |
Stephen Hemminger | e12c4f8 | 2009-03-20 19:35:57 +0000 | [diff] [blame] | 467 | .ndo_validate_addr = eth_validate_addr, |
Arnd Bergmann | a760537 | 2021-07-27 15:45:13 +0200 | [diff] [blame] | 468 | .ndo_eth_ioctl = mcs7830_ioctl, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 469 | .ndo_set_rx_mode = mcs7830_set_multicast, |
Andreas Mohr | 00e4991 | 2010-01-31 02:58:26 +0000 | [diff] [blame] | 470 | .ndo_set_mac_address = mcs7830_set_mac_address, |
Stephen Hemminger | e12c4f8 | 2009-03-20 19:35:57 +0000 | [diff] [blame] | 471 | }; |
| 472 | |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 473 | static int mcs7830_bind(struct usbnet *dev, struct usb_interface *udev) |
| 474 | { |
| 475 | struct net_device *net = dev->net; |
Jakub Kicinski | 2674e7e | 2021-10-21 06:12:06 -0700 | [diff] [blame] | 476 | u8 addr[ETH_ALEN]; |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 477 | int ret; |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 478 | int retry; |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 479 | |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 480 | /* Initial startup: Gather MAC address setting from EEPROM */ |
| 481 | ret = -EINVAL; |
| 482 | for (retry = 0; retry < 5 && ret; retry++) |
Jakub Kicinski | 2674e7e | 2021-10-21 06:12:06 -0700 | [diff] [blame] | 483 | ret = mcs7830_hif_get_mac_address(dev, addr); |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 484 | if (ret) { |
| 485 | dev_warn(&dev->udev->dev, "Cannot read MAC address\n"); |
| 486 | goto out; |
| 487 | } |
Jakub Kicinski | 2674e7e | 2021-10-21 06:12:06 -0700 | [diff] [blame] | 488 | eth_hw_addr_set(net, addr); |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 489 | |
| 490 | mcs7830_data_set_multicast(net); |
| 491 | |
| 492 | ret = mcs7830_apply_base_config(dev); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 493 | if (ret) |
| 494 | goto out; |
| 495 | |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 496 | net->ethtool_ops = &mcs7830_ethtool_ops; |
Stephen Hemminger | e12c4f8 | 2009-03-20 19:35:57 +0000 | [diff] [blame] | 497 | net->netdev_ops = &mcs7830_netdev_ops; |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 498 | |
| 499 | /* reserve space for the status byte on rx */ |
| 500 | dev->rx_urb_size = ETH_FRAME_LEN + 1; |
| 501 | |
| 502 | dev->mii.mdio_read = mcs7830_mdio_read; |
| 503 | dev->mii.mdio_write = mcs7830_mdio_write; |
| 504 | dev->mii.dev = net; |
| 505 | dev->mii.phy_id_mask = 0x3f; |
| 506 | dev->mii.reg_num_mask = 0x1f; |
| 507 | dev->mii.phy_id = *((u8 *) net->dev_addr + 1); |
| 508 | |
| 509 | ret = usbnet_get_endpoints(dev, udev); |
| 510 | out: |
| 511 | return ret; |
| 512 | } |
| 513 | |
Andreas Mohr | 00e4991 | 2010-01-31 02:58:26 +0000 | [diff] [blame] | 514 | /* The chip always appends a status byte that we need to strip */ |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 515 | static int mcs7830_rx_fixup(struct usbnet *dev, struct sk_buff *skb) |
| 516 | { |
| 517 | u8 status; |
| 518 | |
Emil Goode | eb85569 | 2014-02-13 17:50:19 +0100 | [diff] [blame] | 519 | /* This check is no longer done by usbnet */ |
| 520 | if (skb->len < dev->net->hard_header_len) { |
| 521 | dev_err(&dev->udev->dev, "unexpected tiny rx frame\n"); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | skb_trim(skb, skb->len - 1); |
| 526 | status = skb->data[skb->len]; |
| 527 | |
Andreas Mohr | 7680285 | 2010-01-31 02:58:19 +0000 | [diff] [blame] | 528 | if (status != MCS7830_RX_FRAME_CORRECT) { |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 529 | dev_dbg(&dev->udev->dev, "rx fixup status %x\n", status); |
| 530 | |
Andreas Mohr | 7680285 | 2010-01-31 02:58:19 +0000 | [diff] [blame] | 531 | /* hmm, perhaps usbnet.c already sees a globally visible |
| 532 | frame error and increments rx_errors on its own already? */ |
| 533 | dev->net->stats.rx_errors++; |
| 534 | |
| 535 | if (status & (MCS7830_RX_SHORT_FRAME |
| 536 | |MCS7830_RX_LENGTH_ERROR |
| 537 | |MCS7830_RX_LARGE_FRAME)) |
| 538 | dev->net->stats.rx_length_errors++; |
| 539 | if (status & MCS7830_RX_ALIGNMENT_ERROR) |
| 540 | dev->net->stats.rx_frame_errors++; |
| 541 | if (status & MCS7830_RX_CRC_ERROR) |
| 542 | dev->net->stats.rx_crc_errors++; |
| 543 | } |
| 544 | |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 545 | return skb->len > 0; |
| 546 | } |
| 547 | |
Ondrej Zary | b1ff4f9 | 2012-06-01 10:29:08 +0000 | [diff] [blame] | 548 | static void mcs7830_status(struct usbnet *dev, struct urb *urb) |
| 549 | { |
| 550 | u8 *buf = urb->transfer_buffer; |
Ondrej Zary | dabdaf0 | 2012-10-11 22:51:41 +0000 | [diff] [blame] | 551 | bool link, link_changed; |
Ondrej Zary | b1ff4f9 | 2012-06-01 10:29:08 +0000 | [diff] [blame] | 552 | |
| 553 | if (urb->actual_length < 16) |
| 554 | return; |
| 555 | |
Octavian Purdila | 8d88bbf | 2013-12-23 19:06:31 +0200 | [diff] [blame] | 556 | link = !(buf[1] == 0x20); |
Ondrej Zary | dabdaf0 | 2012-10-11 22:51:41 +0000 | [diff] [blame] | 557 | link_changed = netif_carrier_ok(dev->net) != link; |
| 558 | if (link_changed) { |
Octavian Purdila | 8d88bbf | 2013-12-23 19:06:31 +0200 | [diff] [blame] | 559 | usbnet_link_change(dev, link, 0); |
| 560 | netdev_dbg(dev->net, "Link Status is: %d\n", link); |
| 561 | } |
Ondrej Zary | b1ff4f9 | 2012-06-01 10:29:08 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 564 | static const struct driver_info moschip_info = { |
Andreas Mohr | bbccc16 | 2010-12-20 10:38:47 -0800 | [diff] [blame] | 565 | .description = "MOSCHIP 7830/7832/7730 usb-NET adapter", |
Arnd Bergmann | 8382cc1 | 2008-08-23 22:02:23 +0200 | [diff] [blame] | 566 | .bind = mcs7830_bind, |
| 567 | .rx_fixup = mcs7830_rx_fixup, |
Ondrej Zary | b1ff4f9 | 2012-06-01 10:29:08 +0000 | [diff] [blame] | 568 | .flags = FLAG_ETHER | FLAG_LINK_INTR, |
| 569 | .status = mcs7830_status, |
Arnd Bergmann | 8382cc1 | 2008-08-23 22:02:23 +0200 | [diff] [blame] | 570 | .in = 1, |
| 571 | .out = 2, |
| 572 | }; |
| 573 | |
| 574 | static const struct driver_info sitecom_info = { |
| 575 | .description = "Sitecom LN-30 usb-NET adapter", |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 576 | .bind = mcs7830_bind, |
| 577 | .rx_fixup = mcs7830_rx_fixup, |
Ondrej Zary | b1ff4f9 | 2012-06-01 10:29:08 +0000 | [diff] [blame] | 578 | .flags = FLAG_ETHER | FLAG_LINK_INTR, |
| 579 | .status = mcs7830_status, |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 580 | .in = 1, |
| 581 | .out = 2, |
| 582 | }; |
| 583 | |
| 584 | static const struct usb_device_id products[] = { |
| 585 | { |
Andreas Mohr | bbccc16 | 2010-12-20 10:38:47 -0800 | [diff] [blame] | 586 | USB_DEVICE(MCS7830_VENDOR_ID, MCS7832_PRODUCT_ID), |
| 587 | .driver_info = (unsigned long) &moschip_info, |
| 588 | }, |
| 589 | { |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 590 | USB_DEVICE(MCS7830_VENDOR_ID, MCS7830_PRODUCT_ID), |
| 591 | .driver_info = (unsigned long) &moschip_info, |
| 592 | }, |
Arnd Bergmann | 8382cc1 | 2008-08-23 22:02:23 +0200 | [diff] [blame] | 593 | { |
| 594 | USB_DEVICE(MCS7830_VENDOR_ID, MCS7730_PRODUCT_ID), |
| 595 | .driver_info = (unsigned long) &moschip_info, |
| 596 | }, |
| 597 | { |
| 598 | USB_DEVICE(SITECOM_VENDOR_ID, LN_030_PRODUCT_ID), |
| 599 | .driver_info = (unsigned long) &sitecom_info, |
| 600 | }, |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 601 | {}, |
| 602 | }; |
| 603 | MODULE_DEVICE_TABLE(usb, products); |
| 604 | |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 605 | static int mcs7830_reset_resume (struct usb_interface *intf) |
| 606 | { |
Hui Tang | 094fefd | 2021-05-20 11:47:47 +0800 | [diff] [blame] | 607 | /* YES, this function is successful enough that ethtool -d |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 608 | does show same output pre-/post-suspend */ |
| 609 | |
| 610 | struct usbnet *dev = usb_get_intfdata(intf); |
| 611 | |
| 612 | mcs7830_apply_base_config(dev); |
| 613 | |
| 614 | usbnet_resume(intf); |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 619 | static struct usb_driver mcs7830_driver = { |
| 620 | .name = driver_name, |
| 621 | .id_table = products, |
| 622 | .probe = usbnet_probe, |
| 623 | .disconnect = usbnet_disconnect, |
| 624 | .suspend = usbnet_suspend, |
| 625 | .resume = usbnet_resume, |
Andreas Mohr | ace2a4d | 2010-01-31 02:58:42 +0000 | [diff] [blame] | 626 | .reset_resume = mcs7830_reset_resume, |
Sarah Sharp | e1f12eb | 2012-04-23 10:08:51 -0700 | [diff] [blame] | 627 | .disable_hub_initiated_lpm = 1, |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 628 | }; |
| 629 | |
Greg Kroah-Hartman | d632eb1 | 2011-11-18 09:44:20 -0800 | [diff] [blame] | 630 | module_usb_driver(mcs7830_driver); |
Arnd Bergmann | 2a36d70 | 2006-10-09 00:08:00 +0200 | [diff] [blame] | 631 | |
| 632 | MODULE_DESCRIPTION("USB to network adapter MCS7830)"); |
| 633 | MODULE_LICENSE("GPL"); |