blob: 34665137f2c330aabf01b3b83838fa52d035f574 [file] [log] [blame]
Arnd Bergmann2a36d702006-10-09 00:08:00 +02001/*
Andreas Mohr00e49912010-01-31 02:58:26 +00002 * MOSCHIP MCS7830 based 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 *
14 * TODO:
Andreas Mohr76802852010-01-31 02:58:19 +000015 * - 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 Bergmann2a36d702006-10-09 00:08:00 +020025 * 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 Kivilinna3692e942008-01-26 00:51:45 +020048#include <linux/usb/usbnet.h>
Arnd Bergmann2a36d702006-10-09 00:08:00 +020049
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 Bergmann8382cc12008-08-23 22:02:23 +020063#define MCS7730_PRODUCT_ID 0x7730
64
65#define SITECOM_VENDOR_ID 0x0DF6
66#define LN_030_PRODUCT_ID 0x0021
Arnd Bergmann2a36d702006-10-09 00:08:00 +020067
68#define MCS7830_MII_ADVERTISE (ADVERTISE_PAUSE_CAP | ADVERTISE_100FULL | \
69 ADVERTISE_100HALF | ADVERTISE_10FULL | \
70 ADVERTISE_10HALF | ADVERTISE_CSMA)
71
Andreas Mohr00e49912010-01-31 02:58:26 +000072/* HIF_REG_XX corresponding index value */
Arnd Bergmann2a36d702006-10-09 00:08:00 +020073enum {
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 Mohrc7746512010-01-31 02:58:33 +000086 /* hmm, spec sez: "R/W", "Except bit 3" (likely TXENABLE). */
Arnd Bergmann2a36d702006-10-09 00:08:00 +020087 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 Mohr00e49912010-01-31 02:58:26 +000094 HIF_REG_CONFIG_PROMISCUOUS = 0x01,
Arnd Bergmann2a36d702006-10-09 00:08:00 +020095 HIF_REG_ETHERNET_ADDR = 0x0f,
Andreas Mohrc7746512010-01-31 02:58:33 +000096 HIF_REG_FRAME_DROP_COUNTER = 0x15, /* 0..ff; reset: 0 */
Arnd Bergmann2a36d702006-10-09 00:08:00 +020097 HIF_REG_PAUSE_THRESHOLD = 0x16,
98 HIF_REG_PAUSE_THRESHOLD_DEFAULT = 0,
99};
100
Andreas Mohr76802852010-01-31 02:58:19 +0000101/* Trailing status byte in Ethernet Rx frame */
102enum {
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 Bergmann2a36d702006-10-09 00:08:00 +0200112struct mcs7830_data {
113 u8 multi_filter[8];
114 u8 config;
115};
116
117static const char driver_name[] = "MOSCHIP usb-ethernet driver";
118
119static 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 Eggers6d317482009-01-21 12:56:24 -0800123 void *buffer;
124
125 buffer = kmalloc(size, GFP_NOIO);
126 if (buffer == NULL)
127 return -ENOMEM;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200128
129 ret = usb_control_msg(xdev, usb_rcvctrlpipe(xdev, 0), MCS7830_RD_BREQ,
Christian Eggers6d317482009-01-21 12:56:24 -0800130 MCS7830_RD_BMREQ, 0x0000, index, buffer,
Russ Dill1d39da32008-01-07 23:15:41 -0800131 size, MCS7830_CTRL_TIMEOUT);
Christian Eggers6d317482009-01-21 12:56:24 -0800132 memcpy(data, buffer, size);
133 kfree(buffer);
134
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200135 return ret;
136}
137
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000138static int mcs7830_set_reg(struct usbnet *dev, u16 index, u16 size, const void *data)
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200139{
140 struct usb_device *xdev = dev->udev;
141 int ret;
Christian Eggers6d317482009-01-21 12:56:24 -0800142 void *buffer;
143
144 buffer = kmalloc(size, GFP_NOIO);
145 if (buffer == NULL)
146 return -ENOMEM;
147
148 memcpy(buffer, data, size);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200149
150 ret = usb_control_msg(xdev, usb_sndctrlpipe(xdev, 0), MCS7830_WR_BREQ,
Christian Eggers6d317482009-01-21 12:56:24 -0800151 MCS7830_WR_BMREQ, 0x0000, index, buffer,
Russ Dill1d39da32008-01-07 23:15:41 -0800152 size, MCS7830_CTRL_TIMEOUT);
Christian Eggers6d317482009-01-21 12:56:24 -0800153 kfree(buffer);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200154 return ret;
155}
156
157static void mcs7830_async_cmd_callback(struct urb *urb)
158{
159 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
Oliver Neukumc94cb312008-12-18 23:00:59 -0800160 int status = urb->status;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200161
Oliver Neukumc94cb312008-12-18 23:00:59 -0800162 if (status < 0)
Joe Perches898eb712007-10-18 03:06:30 -0700163 printk(KERN_DEBUG "%s() failed with %d\n",
Oliver Neukumc94cb312008-12-18 23:00:59 -0800164 __func__, status);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200165
166 kfree(req);
167 usb_free_urb(urb);
168}
169
170static 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 Perches898eb712007-10-18 03:06:30 -0700178 dev_dbg(&dev->udev->dev,
179 "Error allocating URB in write_cmd_async!\n");
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200180 return;
181 }
182
183 req = kmalloc(sizeof *req, GFP_ATOMIC);
184 if (!req) {
Joe Perches898eb712007-10-18 03:06:30 -0700185 dev_err(&dev->udev->dev,
186 "Failed to allocate memory for control request\n");
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200187 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 Perches898eb712007-10-18 03:06:30 -0700202 dev_err(&dev->udev->dev,
203 "Error submitting the control message: ret=%d\n", ret);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200204 goto out;
205 }
206 return;
207out:
208 kfree(req);
209 usb_free_urb(urb);
210}
211
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000212static int mcs7830_hif_get_mac_address(struct usbnet *dev, unsigned char *addr)
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200213{
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000214 int ret = mcs7830_get_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN, addr);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200215 if (ret < 0)
216 return ret;
217 return 0;
218}
219
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000220static 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
229static 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 Bergmann2a36d702006-10-09 00:08:00 +0200252static 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 Bergmanna9fc6332006-10-09 00:08:02 +0200263 mutex_lock(&dev->phy_mutex);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200264 /* 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);
287out:
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200288 mutex_unlock(&dev->phy_mutex);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200289 return ret;
290}
291
292static 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 Bergmanna9fc6332006-10-09 00:08:02 +0200303 mutex_lock(&dev->phy_mutex);
304
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200305 /* 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);
330out:
Arnd Bergmanna9fc6332006-10-09 00:08:02 +0200331 mutex_unlock(&dev->phy_mutex);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200332 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 */
339static 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 */
362static int mcs7830_get_rev(struct usbnet *dev)
363{
364 u8 dummy[2];
365 int ret;
Andreas Mohrc7746512010-01-31 02:58:33 +0000366 ret = mcs7830_get_reg(dev, HIF_REG_FRAME_DROP_COUNTER, 2, dummy);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200367 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 */
375static 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 Bergmann2a36d702006-10-09 00:08:00 +0200390static int mcs7830_mdio_read(struct net_device *netdev, int phy_id,
391 int location)
392{
Wang Chen8f15ea42008-11-12 23:38:36 -0800393 struct usbnet *dev = netdev_priv(netdev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200394 return mcs7830_read_phy(dev, location);
395}
396
397static void mcs7830_mdio_write(struct net_device *netdev, int phy_id,
398 int location, int val)
399{
Wang Chen8f15ea42008-11-12 23:38:36 -0800400 struct usbnet *dev = netdev_priv(netdev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200401 mcs7830_write_phy(dev, location, val);
402}
403
404static 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 Mohrace2a4d2010-01-31 02:58:42 +0000410static inline struct mcs7830_data *mcs7830_get_data(struct usbnet *dev)
411{
412 return (struct mcs7830_data *)&dev->data;
413}
414
415static 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
423static 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
431static void mcs7830_data_set_multicast(struct net_device *net)
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200432{
433 struct usbnet *dev = netdev_priv(net);
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000434 struct mcs7830_data *data = mcs7830_get_data(dev);
435
436 memset(data->multi_filter, 0, sizeof data->multi_filter);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200437
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 Mohr00e49912010-01-31 02:58:26 +0000444 data->config |= HIF_REG_CONFIG_PROMISCUOUS;
Joe Perches8e95a202009-12-03 07:58:21 +0000445 } else if (net->flags & IFF_ALLMULTI ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000446 netdev_mc_count(net) > MCS7830_MAX_MCAST) {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200447 data->config |= HIF_REG_CONFIG_ALLMULTICAST;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000448 } else if (netdev_mc_empty(net)) {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200449 /* 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 */
455 struct dev_mc_list *mc_list = net->mc_list;
456 u32 crc_bits;
457 int i;
458
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200459 /* Build the multicast hash filter. */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000460 for (i = 0; i < netdev_mc_count(net); i++) {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200461 crc_bits = ether_crc(ETH_ALEN, mc_list->dmi_addr) >> 26;
462 data->multi_filter[crc_bits >> 3] |= 1 << (crc_bits & 7);
463 mc_list = mc_list->next;
464 }
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000465 }
466}
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200467
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000468static int mcs7830_apply_base_config(struct usbnet *dev)
469{
470 int ret;
471
472 /* re-configure known MAC (suspend case etc.) */
473 ret = mcs7830_hif_set_mac_address(dev, dev->net->dev_addr);
474 if (ret) {
475 dev_info(&dev->udev->dev, "Cannot set MAC address\n");
476 goto out;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200477 }
478
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000479 /* Set up PHY */
480 ret = mcs7830_set_autoneg(dev, 0);
481 if (ret) {
482 dev_info(&dev->udev->dev, "Cannot set autoneg\n");
483 goto out;
484 }
485
486 mcs7830_hif_update_multicast_hash(dev);
487 mcs7830_hif_update_config(dev);
488
489 mcs7830_rev_C_fixup(dev);
490 ret = 0;
491out:
492 return ret;
493}
494
495/* credits go to asix_set_multicast */
496static void mcs7830_set_multicast(struct net_device *net)
497{
498 struct usbnet *dev = netdev_priv(net);
499
500 mcs7830_data_set_multicast(net);
501
502 mcs7830_hif_update_multicast_hash(dev);
503 mcs7830_hif_update_config(dev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200504}
505
506static int mcs7830_get_regs_len(struct net_device *net)
507{
508 struct usbnet *dev = netdev_priv(net);
509
510 switch (mcs7830_get_rev(dev)) {
511 case 1:
512 return 21;
513 case 2:
514 return 32;
515 }
516 return 0;
517}
518
519static void mcs7830_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *drvinfo)
520{
521 usbnet_get_drvinfo(net, drvinfo);
522 drvinfo->regdump_len = mcs7830_get_regs_len(net);
523}
524
525static void mcs7830_get_regs(struct net_device *net, struct ethtool_regs *regs, void *data)
526{
527 struct usbnet *dev = netdev_priv(net);
528
529 regs->version = mcs7830_get_rev(dev);
530 mcs7830_get_reg(dev, 0, regs->len, data);
531}
532
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700533static const struct ethtool_ops mcs7830_ethtool_ops = {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200534 .get_drvinfo = mcs7830_get_drvinfo,
535 .get_regs_len = mcs7830_get_regs_len,
536 .get_regs = mcs7830_get_regs,
537
538 /* common usbnet calls */
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200539 .get_link = usbnet_get_link,
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200540 .get_msglevel = usbnet_get_msglevel,
541 .set_msglevel = usbnet_set_msglevel,
Arnd Bergmannc41286f2006-10-09 00:08:01 +0200542 .get_settings = usbnet_get_settings,
543 .set_settings = usbnet_set_settings,
544 .nway_reset = usbnet_nway_reset,
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200545};
546
Stephen Hemmingere12c4f82009-03-20 19:35:57 +0000547static const struct net_device_ops mcs7830_netdev_ops = {
548 .ndo_open = usbnet_open,
549 .ndo_stop = usbnet_stop,
550 .ndo_start_xmit = usbnet_start_xmit,
551 .ndo_tx_timeout = usbnet_tx_timeout,
552 .ndo_change_mtu = usbnet_change_mtu,
553 .ndo_validate_addr = eth_validate_addr,
554 .ndo_do_ioctl = mcs7830_ioctl,
555 .ndo_set_multicast_list = mcs7830_set_multicast,
Andreas Mohr00e49912010-01-31 02:58:26 +0000556 .ndo_set_mac_address = mcs7830_set_mac_address,
Stephen Hemmingere12c4f82009-03-20 19:35:57 +0000557};
558
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200559static int mcs7830_bind(struct usbnet *dev, struct usb_interface *udev)
560{
561 struct net_device *net = dev->net;
562 int ret;
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000563 int retry;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200564
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000565 /* Initial startup: Gather MAC address setting from EEPROM */
566 ret = -EINVAL;
567 for (retry = 0; retry < 5 && ret; retry++)
568 ret = mcs7830_hif_get_mac_address(dev, net->dev_addr);
569 if (ret) {
570 dev_warn(&dev->udev->dev, "Cannot read MAC address\n");
571 goto out;
572 }
573
574 mcs7830_data_set_multicast(net);
575
576 ret = mcs7830_apply_base_config(dev);
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200577 if (ret)
578 goto out;
579
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200580 net->ethtool_ops = &mcs7830_ethtool_ops;
Stephen Hemmingere12c4f82009-03-20 19:35:57 +0000581 net->netdev_ops = &mcs7830_netdev_ops;
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200582
583 /* reserve space for the status byte on rx */
584 dev->rx_urb_size = ETH_FRAME_LEN + 1;
585
586 dev->mii.mdio_read = mcs7830_mdio_read;
587 dev->mii.mdio_write = mcs7830_mdio_write;
588 dev->mii.dev = net;
589 dev->mii.phy_id_mask = 0x3f;
590 dev->mii.reg_num_mask = 0x1f;
591 dev->mii.phy_id = *((u8 *) net->dev_addr + 1);
592
593 ret = usbnet_get_endpoints(dev, udev);
594out:
595 return ret;
596}
597
Andreas Mohr00e49912010-01-31 02:58:26 +0000598/* The chip always appends a status byte that we need to strip */
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200599static int mcs7830_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
600{
601 u8 status;
602
603 if (skb->len == 0) {
604 dev_err(&dev->udev->dev, "unexpected empty rx frame\n");
605 return 0;
606 }
607
608 skb_trim(skb, skb->len - 1);
609 status = skb->data[skb->len];
610
Andreas Mohr76802852010-01-31 02:58:19 +0000611 if (status != MCS7830_RX_FRAME_CORRECT) {
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200612 dev_dbg(&dev->udev->dev, "rx fixup status %x\n", status);
613
Andreas Mohr76802852010-01-31 02:58:19 +0000614 /* hmm, perhaps usbnet.c already sees a globally visible
615 frame error and increments rx_errors on its own already? */
616 dev->net->stats.rx_errors++;
617
618 if (status & (MCS7830_RX_SHORT_FRAME
619 |MCS7830_RX_LENGTH_ERROR
620 |MCS7830_RX_LARGE_FRAME))
621 dev->net->stats.rx_length_errors++;
622 if (status & MCS7830_RX_ALIGNMENT_ERROR)
623 dev->net->stats.rx_frame_errors++;
624 if (status & MCS7830_RX_CRC_ERROR)
625 dev->net->stats.rx_crc_errors++;
626 }
627
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200628 return skb->len > 0;
629}
630
631static const struct driver_info moschip_info = {
Arnd Bergmann8382cc12008-08-23 22:02:23 +0200632 .description = "MOSCHIP 7830/7730 usb-NET adapter",
633 .bind = mcs7830_bind,
634 .rx_fixup = mcs7830_rx_fixup,
635 .flags = FLAG_ETHER,
636 .in = 1,
637 .out = 2,
638};
639
640static const struct driver_info sitecom_info = {
641 .description = "Sitecom LN-30 usb-NET adapter",
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200642 .bind = mcs7830_bind,
643 .rx_fixup = mcs7830_rx_fixup,
644 .flags = FLAG_ETHER,
645 .in = 1,
646 .out = 2,
647};
648
649static const struct usb_device_id products[] = {
650 {
651 USB_DEVICE(MCS7830_VENDOR_ID, MCS7830_PRODUCT_ID),
652 .driver_info = (unsigned long) &moschip_info,
653 },
Arnd Bergmann8382cc12008-08-23 22:02:23 +0200654 {
655 USB_DEVICE(MCS7830_VENDOR_ID, MCS7730_PRODUCT_ID),
656 .driver_info = (unsigned long) &moschip_info,
657 },
658 {
659 USB_DEVICE(SITECOM_VENDOR_ID, LN_030_PRODUCT_ID),
660 .driver_info = (unsigned long) &sitecom_info,
661 },
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200662 {},
663};
664MODULE_DEVICE_TABLE(usb, products);
665
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000666static int mcs7830_reset_resume (struct usb_interface *intf)
667{
668 /* YES, this function is successful enough that ethtool -d
669 does show same output pre-/post-suspend */
670
671 struct usbnet *dev = usb_get_intfdata(intf);
672
673 mcs7830_apply_base_config(dev);
674
675 usbnet_resume(intf);
676
677 return 0;
678}
679
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200680static struct usb_driver mcs7830_driver = {
681 .name = driver_name,
682 .id_table = products,
683 .probe = usbnet_probe,
684 .disconnect = usbnet_disconnect,
685 .suspend = usbnet_suspend,
686 .resume = usbnet_resume,
Andreas Mohrace2a4d2010-01-31 02:58:42 +0000687 .reset_resume = mcs7830_reset_resume,
Arnd Bergmann2a36d702006-10-09 00:08:00 +0200688};
689
690static int __init mcs7830_init(void)
691{
692 return usb_register(&mcs7830_driver);
693}
694module_init(mcs7830_init);
695
696static void __exit mcs7830_exit(void)
697{
698 usb_deregister(&mcs7830_driver);
699}
700module_exit(mcs7830_exit);
701
702MODULE_DESCRIPTION("USB to network adapter MCS7830)");
703MODULE_LICENSE("GPL");