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