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