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