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