blob: cc7e72010ac383f1d0981302df50dc7f388ab40c [file] [log] [blame]
Arnd Bergmann2a36d702006-10-09 00:08:00 +02001/*
Andreas Mohrbbccc162010-12-20 10:38:47 -08002 * MOSCHIP MCS7830 based (7730/7830/7832) USB 2.0 Ethernet Devices
Arnd Bergmann2a36d702006-10-09 00:08:00 +02003 *
4 * based on usbnet.c, asix.c and the vendor provided mcs7830 driver
5 *
Andreas Mohr76802852010-01-31 02:58:19 +00006 * Copyright (C) 2010 Andreas Mohr <andi@lisas.de>
Arnd Bergmann2a36d702006-10-09 00:08:00 +02007 * 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 Mohr76802852010-01-31 02:58:19 +000012 * Definitions gathered from MOSCHIP, Data Sheet_7830DA.pdf (thanks!).
13 *
Andreas Mohrbbccc162010-12-20 10:38:47 -080014 * 2010-12-19: add 7832 USB PID ("functionality same as MCS7830"),
15 * per active notification by manufacturer
16 *
Andreas Mohr76802852010-01-31 02:58:19 +000017 * TODO:
Andreas Mohr76802852010-01-31 02:58:19 +000018 * - 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 Bergmann2a36d702006-10-09 00:08:00 +020028 * 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 Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Arnd Bergmann2a36d702006-10-09 00:08:00 +020051#include <linux/usb.h>
Jussi Kivilinna3692e942008-01-26 00:51:45 +020052#include <linux/usb/usbnet.h>
Arnd Bergmann2a36d702006-10-09 00:08:00 +020053
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 Mohrbbccc162010-12-20 10:38:47 -080066#define MCS7832_PRODUCT_ID 0x7832
Arnd Bergmann2a36d702006-10-09 00:08:00 +020067#define MCS7830_PRODUCT_ID 0x7830
Arnd Bergmann8382cc12008-08-23 22:02:23 +020068#define MCS7730_PRODUCT_ID 0x7730
69
70#define SITECOM_VENDOR_ID 0x0DF6
71#define LN_030_PRODUCT_ID 0x0021
Arnd Bergmann2a36d702006-10-09 00:08:00 +020072
73#define MCS7830_MII_ADVERTISE (ADVERTISE_PAUSE_CAP | ADVERTISE_100FULL | \
74 ADVERTISE_100HALF | ADVERTISE_10FULL | \
75 ADVERTISE_10HALF | ADVERTISE_CSMA)
76
Andreas Mohr00e49912010-01-31 02:58:26 +000077/* HIF_REG_XX corresponding index value */
Arnd Bergmann2a36d702006-10-09 00:08:00 +020078enum {
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 Mohrc7746512010-01-31 02:58:33 +000091 /* hmm, spec sez: "R/W", "Except bit 3" (likely TXENABLE). */
Arnd Bergmann2a36d702006-10-09 00:08:00 +020092 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 Mohr00e49912010-01-31 02:58:26 +000099 HIF_REG_CONFIG_PROMISCUOUS = 0x01,
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200100 HIF_REG_ETHERNET_ADDR = 0x0f,
Andreas Mohrc7746512010-01-31 02:58:33 +0000101 HIF_REG_FRAME_DROP_COUNTER = 0x15, /* 0..ff; reset: 0 */
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200102 HIF_REG_PAUSE_THRESHOLD = 0x16,
103 HIF_REG_PAUSE_THRESHOLD_DEFAULT = 0,
104};
105
Andreas Mohr76802852010-01-31 02:58:19 +0000106/* Trailing status byte in Ethernet Rx frame */
107enum {
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 Bergmann2a36d702006-10-09 00:08:00 +0200117struct mcs7830_data {
118 u8 multi_filter[8];
119 u8 config;
Ondrej Zarydabdaf02012-10-11 22:51:41 +0000120 u8 link_counter;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200121};
122
123static const char driver_name[] = "MOSCHIP usb-ethernet driver";
124
125static int mcs7830_get_reg(struct usbnet *dev, u16 index, u16 size, void *data)
126{
127 struct usb_device *xdev = dev->udev;
128 int ret;
Christian Eggers6d317482009-01-21 12:56:24 -0800129 void *buffer;
130
131 buffer = kmalloc(size, GFP_NOIO);
132 if (buffer == NULL)
133 return -ENOMEM;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200134
135 ret = usb_control_msg(xdev, usb_rcvctrlpipe(xdev, 0), MCS7830_RD_BREQ,
Christian Eggers6d317482009-01-21 12:56:24 -0800136 MCS7830_RD_BMREQ, 0x0000, index, buffer,
Russ Dill1d39da32008-01-07 23:15:41 -0800137 size, MCS7830_CTRL_TIMEOUT);
Christian Eggers6d317482009-01-21 12:56:24 -0800138 memcpy(data, buffer, size);
139 kfree(buffer);
140
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200141 return ret;
142}
143
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000144static int mcs7830_set_reg(struct usbnet *dev, u16 index, u16 size, const void *data)
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200145{
146 struct usb_device *xdev = dev->udev;
147 int ret;
Christian Eggers6d317482009-01-21 12:56:24 -0800148 void *buffer;
149
Julia Lawall99bf2362010-05-15 11:20:45 +0000150 buffer = kmemdup(data, size, GFP_NOIO);
Christian Eggers6d317482009-01-21 12:56:24 -0800151 if (buffer == NULL)
152 return -ENOMEM;
153
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200154 ret = usb_control_msg(xdev, usb_sndctrlpipe(xdev, 0), MCS7830_WR_BREQ,
Christian Eggers6d317482009-01-21 12:56:24 -0800155 MCS7830_WR_BMREQ, 0x0000, index, buffer,
Russ Dill1d39da32008-01-07 23:15:41 -0800156 size, MCS7830_CTRL_TIMEOUT);
Christian Eggers6d317482009-01-21 12:56:24 -0800157 kfree(buffer);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200158 return ret;
159}
160
161static void mcs7830_async_cmd_callback(struct urb *urb)
162{
163 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800164 int status = urb->status;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200165
Oliver Neukumc94cb312008-12-18 23:00:59 -0800166 if (status < 0)
Joe Perches898eb712007-10-18 03:06:30 -0700167 printk(KERN_DEBUG "%s() failed with %d\n",
Oliver Neukumc94cb312008-12-18 23:00:59 -0800168 __func__, status);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200169
170 kfree(req);
171 usb_free_urb(urb);
172}
173
174static void mcs7830_set_reg_async(struct usbnet *dev, u16 index, u16 size, void *data)
175{
176 struct usb_ctrlrequest *req;
177 int ret;
178 struct urb *urb;
179
180 urb = usb_alloc_urb(0, GFP_ATOMIC);
181 if (!urb) {
Joe Perches898eb712007-10-18 03:06:30 -0700182 dev_dbg(&dev->udev->dev,
183 "Error allocating URB in write_cmd_async!\n");
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200184 return;
185 }
186
187 req = kmalloc(sizeof *req, GFP_ATOMIC);
188 if (!req) {
Joe Perches898eb712007-10-18 03:06:30 -0700189 dev_err(&dev->udev->dev,
190 "Failed to allocate memory for control request\n");
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200191 goto out;
192 }
193 req->bRequestType = MCS7830_WR_BMREQ;
194 req->bRequest = MCS7830_WR_BREQ;
195 req->wValue = 0;
196 req->wIndex = cpu_to_le16(index);
197 req->wLength = cpu_to_le16(size);
198
199 usb_fill_control_urb(urb, dev->udev,
200 usb_sndctrlpipe(dev->udev, 0),
201 (void *)req, data, size,
202 mcs7830_async_cmd_callback, req);
203
204 ret = usb_submit_urb(urb, GFP_ATOMIC);
205 if (ret < 0) {
Joe Perches898eb712007-10-18 03:06:30 -0700206 dev_err(&dev->udev->dev,
207 "Error submitting the control message: ret=%d\n", ret);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200208 goto out;
209 }
210 return;
211out:
212 kfree(req);
213 usb_free_urb(urb);
214}
215
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000216static int mcs7830_hif_get_mac_address(struct usbnet *dev, unsigned char *addr)
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200217{
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000218 int ret = mcs7830_get_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200219 if (ret < 0)
220 return ret;
221 return 0;
222}
223
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000224static int mcs7830_hif_set_mac_address(struct usbnet *dev, unsigned char *addr)
225{
226 int ret = mcs7830_set_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr);
227
228 if (ret < 0)
229 return ret;
230 return 0;
231}
232
233static int mcs7830_set_mac_address(struct net_device *netdev, void *p)
234{
235 int ret;
236 struct usbnet *dev = netdev_priv(netdev);
237 struct sockaddr *addr = p;
238
239 if (netif_running(netdev))
240 return -EBUSY;
241
242 if (!is_valid_ether_addr(addr->sa_data))
Danny Kukawka15b8f4c2012-02-21 02:07:50 +0000243 return -EADDRNOTAVAIL;
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000244
245 ret = mcs7830_hif_set_mac_address(dev, addr->sa_data);
246
247 if (ret < 0)
248 return ret;
249
250 /* it worked --> adopt it on netdev side */
251 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
252
253 return 0;
254}
255
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200256static int mcs7830_read_phy(struct usbnet *dev, u8 index)
257{
258 int ret;
259 int i;
260 __le16 val;
261
262 u8 cmd[2] = {
263 HIF_REG_PHY_CMD1_READ | HIF_REG_PHY_CMD1_PHYADDR,
264 HIF_REG_PHY_CMD2_PEND_FLAG_BIT | index,
265 };
266
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200267 mutex_lock(&dev->phy_mutex);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200268 /* write the MII command */
269 ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
270 if (ret < 0)
271 goto out;
272
273 /* wait for the data to become valid, should be within < 1ms */
274 for (i = 0; i < 10; i++) {
275 ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
276 if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
277 break;
278 ret = -EIO;
279 msleep(1);
280 }
281 if (ret < 0)
282 goto out;
283
284 /* read actual register contents */
285 ret = mcs7830_get_reg(dev, HIF_REG_PHY_DATA, 2, &val);
286 if (ret < 0)
287 goto out;
288 ret = le16_to_cpu(val);
289 dev_dbg(&dev->udev->dev, "read PHY reg %02x: %04x (%d tries)\n",
290 index, val, i);
291out:
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200292 mutex_unlock(&dev->phy_mutex);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200293 return ret;
294}
295
296static int mcs7830_write_phy(struct usbnet *dev, u8 index, u16 val)
297{
298 int ret;
299 int i;
300 __le16 le_val;
301
302 u8 cmd[2] = {
303 HIF_REG_PHY_CMD1_WRITE | HIF_REG_PHY_CMD1_PHYADDR,
304 HIF_REG_PHY_CMD2_PEND_FLAG_BIT | (index & 0x1F),
305 };
306
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200307 mutex_lock(&dev->phy_mutex);
308
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200309 /* write the new register contents */
310 le_val = cpu_to_le16(val);
311 ret = mcs7830_set_reg(dev, HIF_REG_PHY_DATA, 2, &le_val);
312 if (ret < 0)
313 goto out;
314
315 /* write the MII command */
316 ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
317 if (ret < 0)
318 goto out;
319
320 /* wait for the command to be accepted by the PHY */
321 for (i = 0; i < 10; i++) {
322 ret = mcs7830_get_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
323 if ((ret < 0) || (cmd[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT))
324 break;
325 ret = -EIO;
326 msleep(1);
327 }
328 if (ret < 0)
329 goto out;
330
331 ret = 0;
332 dev_dbg(&dev->udev->dev, "write PHY reg %02x: %04x (%d tries)\n",
333 index, val, i);
334out:
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200335 mutex_unlock(&dev->phy_mutex);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200336 return ret;
337}
338
339/*
340 * This algorithm comes from the original mcs7830 version 1.4 driver,
341 * not sure if it is needed.
342 */
343static int mcs7830_set_autoneg(struct usbnet *dev, int ptrUserPhyMode)
344{
345 int ret;
346 /* Enable all media types */
347 ret = mcs7830_write_phy(dev, MII_ADVERTISE, MCS7830_MII_ADVERTISE);
348
349 /* First reset BMCR */
350 if (!ret)
351 ret = mcs7830_write_phy(dev, MII_BMCR, 0x0000);
352 /* Enable Auto Neg */
353 if (!ret)
354 ret = mcs7830_write_phy(dev, MII_BMCR, BMCR_ANENABLE);
355 /* Restart Auto Neg (Keep the Enable Auto Neg Bit Set) */
356 if (!ret)
357 ret = mcs7830_write_phy(dev, MII_BMCR,
358 BMCR_ANENABLE | BMCR_ANRESTART );
Dan Carpenter0e214ad2010-12-17 03:25:43 +0000359 return ret;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200360}
361
362
363/*
364 * if we can read register 22, the chip revision is C or higher
365 */
366static int mcs7830_get_rev(struct usbnet *dev)
367{
368 u8 dummy[2];
369 int ret;
Andreas Mohrc7746512010-01-31 02:58:33 +0000370 ret = mcs7830_get_reg(dev, HIF_REG_FRAME_DROP_COUNTER, 2, dummy);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200371 if (ret > 0)
372 return 2; /* Rev C or later */
373 return 1; /* earlier revision */
374}
375
376/*
377 * On rev. C we need to set the pause threshold
378 */
379static void mcs7830_rev_C_fixup(struct usbnet *dev)
380{
381 u8 pause_threshold = HIF_REG_PAUSE_THRESHOLD_DEFAULT;
382 int retry;
383
384 for (retry = 0; retry < 2; retry++) {
385 if (mcs7830_get_rev(dev) == 2) {
386 dev_info(&dev->udev->dev, "applying rev.C fixup\n");
387 mcs7830_set_reg(dev, HIF_REG_PAUSE_THRESHOLD,
388 1, &pause_threshold);
389 }
390 msleep(1);
391 }
392}
393
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200394static int mcs7830_mdio_read(struct net_device *netdev, int phy_id,
395 int location)
396{
Wang Chen8f15ea42008-11-12 23:38:36 -0800397 struct usbnet *dev = netdev_priv(netdev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200398 return mcs7830_read_phy(dev, location);
399}
400
401static void mcs7830_mdio_write(struct net_device *netdev, int phy_id,
402 int location, int val)
403{
Wang Chen8f15ea42008-11-12 23:38:36 -0800404 struct usbnet *dev = netdev_priv(netdev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200405 mcs7830_write_phy(dev, location, val);
406}
407
408static int mcs7830_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
409{
410 struct usbnet *dev = netdev_priv(net);
411 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
412}
413
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000414static inline struct mcs7830_data *mcs7830_get_data(struct usbnet *dev)
415{
416 return (struct mcs7830_data *)&dev->data;
417}
418
419static void mcs7830_hif_update_multicast_hash(struct usbnet *dev)
420{
421 struct mcs7830_data *data = mcs7830_get_data(dev);
422 mcs7830_set_reg_async(dev, HIF_REG_MULTICAST_HASH,
423 sizeof data->multi_filter,
424 data->multi_filter);
425}
426
427static void mcs7830_hif_update_config(struct usbnet *dev)
428{
429 /* implementation specific to data->config
430 (argument needs to be heap-based anyway - USB DMA!) */
431 struct mcs7830_data *data = mcs7830_get_data(dev);
432 mcs7830_set_reg_async(dev, HIF_REG_CONFIG, 1, &data->config);
433}
434
435static void mcs7830_data_set_multicast(struct net_device *net)
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200436{
437 struct usbnet *dev = netdev_priv(net);
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000438 struct mcs7830_data *data = mcs7830_get_data(dev);
439
440 memset(data->multi_filter, 0, sizeof data->multi_filter);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200441
442 data->config = HIF_REG_CONFIG_TXENABLE;
443
444 /* this should not be needed, but it doesn't work otherwise */
445 data->config |= HIF_REG_CONFIG_ALLMULTICAST;
446
447 if (net->flags & IFF_PROMISC) {
Andreas Mohr00e49912010-01-31 02:58:26 +0000448 data->config |= HIF_REG_CONFIG_PROMISCUOUS;
Joe Perches8e95a202009-12-03 07:58:21 +0000449 } else if (net->flags & IFF_ALLMULTI ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000450 netdev_mc_count(net) > MCS7830_MAX_MCAST) {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200451 data->config |= HIF_REG_CONFIG_ALLMULTICAST;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000452 } else if (netdev_mc_empty(net)) {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200453 /* just broadcast and directed */
454 } else {
455 /* We use the 20 byte dev->data
456 * for our 8 byte filter buffer
457 * to avoid allocating memory that
458 * is tricky to free later */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000459 struct netdev_hw_addr *ha;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200460 u32 crc_bits;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200461
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200462 /* Build the multicast hash filter. */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000463 netdev_for_each_mc_addr(ha, net) {
464 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200465 data->multi_filter[crc_bits >> 3] |= 1 << (crc_bits & 7);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200466 }
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000467 }
468}
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200469
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000470static int mcs7830_apply_base_config(struct usbnet *dev)
471{
472 int ret;
473
474 /* re-configure known MAC (suspend case etc.) */
475 ret = mcs7830_hif_set_mac_address(dev, dev->net->dev_addr);
476 if (ret) {
477 dev_info(&dev->udev->dev, "Cannot set MAC address\n");
478 goto out;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200479 }
480
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000481 /* Set up PHY */
482 ret = mcs7830_set_autoneg(dev, 0);
483 if (ret) {
484 dev_info(&dev->udev->dev, "Cannot set autoneg\n");
485 goto out;
486 }
487
488 mcs7830_hif_update_multicast_hash(dev);
489 mcs7830_hif_update_config(dev);
490
491 mcs7830_rev_C_fixup(dev);
492 ret = 0;
493out:
494 return ret;
495}
496
497/* credits go to asix_set_multicast */
498static void mcs7830_set_multicast(struct net_device *net)
499{
500 struct usbnet *dev = netdev_priv(net);
501
502 mcs7830_data_set_multicast(net);
503
504 mcs7830_hif_update_multicast_hash(dev);
505 mcs7830_hif_update_config(dev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200506}
507
508static int mcs7830_get_regs_len(struct net_device *net)
509{
510 struct usbnet *dev = netdev_priv(net);
511
512 switch (mcs7830_get_rev(dev)) {
513 case 1:
514 return 21;
515 case 2:
516 return 32;
517 }
518 return 0;
519}
520
521static void mcs7830_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *drvinfo)
522{
523 usbnet_get_drvinfo(net, drvinfo);
524 drvinfo->regdump_len = mcs7830_get_regs_len(net);
525}
526
527static void mcs7830_get_regs(struct net_device *net, struct ethtool_regs *regs, void *data)
528{
529 struct usbnet *dev = netdev_priv(net);
530
531 regs->version = mcs7830_get_rev(dev);
532 mcs7830_get_reg(dev, 0, regs->len, data);
533}
534
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700535static const struct ethtool_ops mcs7830_ethtool_ops = {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200536 .get_drvinfo = mcs7830_get_drvinfo,
537 .get_regs_len = mcs7830_get_regs_len,
538 .get_regs = mcs7830_get_regs,
539
540 /* common usbnet calls */
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200541 .get_link = usbnet_get_link,
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200542 .get_msglevel = usbnet_get_msglevel,
543 .set_msglevel = usbnet_set_msglevel,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200544 .get_settings = usbnet_get_settings,
545 .set_settings = usbnet_set_settings,
546 .nway_reset = usbnet_nway_reset,
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200547};
548
Stephen Hemmingere12c4f82009-03-20 19:35:57 +0000549static const struct net_device_ops mcs7830_netdev_ops = {
550 .ndo_open = usbnet_open,
551 .ndo_stop = usbnet_stop,
552 .ndo_start_xmit = usbnet_start_xmit,
553 .ndo_tx_timeout = usbnet_tx_timeout,
554 .ndo_change_mtu = usbnet_change_mtu,
555 .ndo_validate_addr = eth_validate_addr,
556 .ndo_do_ioctl = mcs7830_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000557 .ndo_set_rx_mode = mcs7830_set_multicast,
Andreas Mohr00e49912010-01-31 02:58:26 +0000558 .ndo_set_mac_address = mcs7830_set_mac_address,
Stephen Hemmingere12c4f82009-03-20 19:35:57 +0000559};
560
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200561static int mcs7830_bind(struct usbnet *dev, struct usb_interface *udev)
562{
563 struct net_device *net = dev->net;
564 int ret;
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000565 int retry;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200566
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000567 /* Initial startup: Gather MAC address setting from EEPROM */
568 ret = -EINVAL;
569 for (retry = 0; retry < 5 && ret; retry++)
570 ret = mcs7830_hif_get_mac_address(dev, net->dev_addr);
571 if (ret) {
572 dev_warn(&dev->udev->dev, "Cannot read MAC address\n");
573 goto out;
574 }
575
576 mcs7830_data_set_multicast(net);
577
578 ret = mcs7830_apply_base_config(dev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200579 if (ret)
580 goto out;
581
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200582 net->ethtool_ops = &mcs7830_ethtool_ops;
Stephen Hemmingere12c4f82009-03-20 19:35:57 +0000583 net->netdev_ops = &mcs7830_netdev_ops;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200584
585 /* reserve space for the status byte on rx */
586 dev->rx_urb_size = ETH_FRAME_LEN + 1;
587
588 dev->mii.mdio_read = mcs7830_mdio_read;
589 dev->mii.mdio_write = mcs7830_mdio_write;
590 dev->mii.dev = net;
591 dev->mii.phy_id_mask = 0x3f;
592 dev->mii.reg_num_mask = 0x1f;
593 dev->mii.phy_id = *((u8 *) net->dev_addr + 1);
594
595 ret = usbnet_get_endpoints(dev, udev);
596out:
597 return ret;
598}
599
Andreas Mohr00e49912010-01-31 02:58:26 +0000600/* The chip always appends a status byte that we need to strip */
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200601static int mcs7830_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
602{
603 u8 status;
604
605 if (skb->len == 0) {
606 dev_err(&dev->udev->dev, "unexpected empty rx frame\n");
607 return 0;
608 }
609
610 skb_trim(skb, skb->len - 1);
611 status = skb->data[skb->len];
612
Andreas Mohr76802852010-01-31 02:58:19 +0000613 if (status != MCS7830_RX_FRAME_CORRECT) {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200614 dev_dbg(&dev->udev->dev, "rx fixup status %x\n", status);
615
Andreas Mohr76802852010-01-31 02:58:19 +0000616 /* hmm, perhaps usbnet.c already sees a globally visible
617 frame error and increments rx_errors on its own already? */
618 dev->net->stats.rx_errors++;
619
620 if (status & (MCS7830_RX_SHORT_FRAME
621 |MCS7830_RX_LENGTH_ERROR
622 |MCS7830_RX_LARGE_FRAME))
623 dev->net->stats.rx_length_errors++;
624 if (status & MCS7830_RX_ALIGNMENT_ERROR)
625 dev->net->stats.rx_frame_errors++;
626 if (status & MCS7830_RX_CRC_ERROR)
627 dev->net->stats.rx_crc_errors++;
628 }
629
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200630 return skb->len > 0;
631}
632
Ondrej Zaryb1ff4f92012-06-01 10:29:08 +0000633static void mcs7830_status(struct usbnet *dev, struct urb *urb)
634{
635 u8 *buf = urb->transfer_buffer;
Ondrej Zarydabdaf02012-10-11 22:51:41 +0000636 bool link, link_changed;
637 struct mcs7830_data *data = mcs7830_get_data(dev);
Ondrej Zaryb1ff4f92012-06-01 10:29:08 +0000638
639 if (urb->actual_length < 16)
640 return;
641
642 link = !(buf[1] & 0x20);
Ondrej Zarydabdaf02012-10-11 22:51:41 +0000643 link_changed = netif_carrier_ok(dev->net) != link;
644 if (link_changed) {
645 data->link_counter++;
646 /*
647 track link state 20 times to guard against erroneous
648 link state changes reported sometimes by the chip
649 */
650 if (data->link_counter > 20) {
651 data->link_counter = 0;
652 if (link) {
653 netif_carrier_on(dev->net);
654 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
655 } else
656 netif_carrier_off(dev->net);
657 netdev_dbg(dev->net, "Link Status is: %d\n", link);
658 }
659 } else
660 data->link_counter = 0;
Ondrej Zaryb1ff4f92012-06-01 10:29:08 +0000661}
662
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200663static const struct driver_info moschip_info = {
Andreas Mohrbbccc162010-12-20 10:38:47 -0800664 .description = "MOSCHIP 7830/7832/7730 usb-NET adapter",
Arnd Bergmann8382cc12008-08-23 22:02:23 +0200665 .bind = mcs7830_bind,
666 .rx_fixup = mcs7830_rx_fixup,
Ondrej Zaryb1ff4f92012-06-01 10:29:08 +0000667 .flags = FLAG_ETHER | FLAG_LINK_INTR,
668 .status = mcs7830_status,
Arnd Bergmann8382cc12008-08-23 22:02:23 +0200669 .in = 1,
670 .out = 2,
671};
672
673static const struct driver_info sitecom_info = {
674 .description = "Sitecom LN-30 usb-NET adapter",
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200675 .bind = mcs7830_bind,
676 .rx_fixup = mcs7830_rx_fixup,
Ondrej Zaryb1ff4f92012-06-01 10:29:08 +0000677 .flags = FLAG_ETHER | FLAG_LINK_INTR,
678 .status = mcs7830_status,
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200679 .in = 1,
680 .out = 2,
681};
682
683static const struct usb_device_id products[] = {
684 {
Andreas Mohrbbccc162010-12-20 10:38:47 -0800685 USB_DEVICE(MCS7830_VENDOR_ID, MCS7832_PRODUCT_ID),
686 .driver_info = (unsigned long) &moschip_info,
687 },
688 {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200689 USB_DEVICE(MCS7830_VENDOR_ID, MCS7830_PRODUCT_ID),
690 .driver_info = (unsigned long) &moschip_info,
691 },
Arnd Bergmann8382cc12008-08-23 22:02:23 +0200692 {
693 USB_DEVICE(MCS7830_VENDOR_ID, MCS7730_PRODUCT_ID),
694 .driver_info = (unsigned long) &moschip_info,
695 },
696 {
697 USB_DEVICE(SITECOM_VENDOR_ID, LN_030_PRODUCT_ID),
698 .driver_info = (unsigned long) &sitecom_info,
699 },
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200700 {},
701};
702MODULE_DEVICE_TABLE(usb, products);
703
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000704static int mcs7830_reset_resume (struct usb_interface *intf)
705{
706 /* YES, this function is successful enough that ethtool -d
707 does show same output pre-/post-suspend */
708
709 struct usbnet *dev = usb_get_intfdata(intf);
710
711 mcs7830_apply_base_config(dev);
712
713 usbnet_resume(intf);
714
715 return 0;
716}
717
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200718static struct usb_driver mcs7830_driver = {
719 .name = driver_name,
720 .id_table = products,
721 .probe = usbnet_probe,
722 .disconnect = usbnet_disconnect,
723 .suspend = usbnet_suspend,
724 .resume = usbnet_resume,
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000725 .reset_resume = mcs7830_reset_resume,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -0700726 .disable_hub_initiated_lpm = 1,
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200727};
728
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -0800729module_usb_driver(mcs7830_driver);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200730
731MODULE_DESCRIPTION("USB to network adapter MCS7830)");
732MODULE_LICENSE("GPL");