2 * MOSCHIP MCS7830 based USB 2.0 Ethernet Devices
4 * based on usbnet.c, asix.c and the vendor provided mcs7830 driver
6 * Copyright (C) 2010 Andreas Mohr <andi@lisas.de>
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.
12 * Definitions gathered from MOSCHIP, Data Sheet_7830DA.pdf (thanks!).
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
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.
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.
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
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>
50 #include <linux/usb/usbnet.h>
53 #define MCS7830_RD_BMREQ (USB_DIR_IN | USB_TYPE_VENDOR | \
55 #define MCS7830_WR_BMREQ (USB_DIR_OUT | USB_TYPE_VENDOR | \
57 #define MCS7830_RD_BREQ 0x0E
58 #define MCS7830_WR_BREQ 0x0D
60 #define MCS7830_CTRL_TIMEOUT 1000
61 #define MCS7830_MAX_MCAST 64
63 #define MCS7830_VENDOR_ID 0x9710
64 #define MCS7830_PRODUCT_ID 0x7830
65 #define MCS7730_PRODUCT_ID 0x7730
67 #define SITECOM_VENDOR_ID 0x0DF6
68 #define LN_030_PRODUCT_ID 0x0021
70 #define MCS7830_MII_ADVERTISE (ADVERTISE_PAUSE_CAP | ADVERTISE_100FULL | \
71 ADVERTISE_100HALF | ADVERTISE_10FULL | \
72 ADVERTISE_10HALF | ADVERTISE_CSMA)
74 /* HIF_REG_XX corresponding index value */
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,
88 /* hmm, spec sez: "R/W", "Except bit 3" (likely TXENABLE). */
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,
96 HIF_REG_CONFIG_PROMISCUOUS = 0x01,
97 HIF_REG_ETHERNET_ADDR = 0x0f,
98 HIF_REG_FRAME_DROP_COUNTER = 0x15, /* 0..ff; reset: 0 */
99 HIF_REG_PAUSE_THRESHOLD = 0x16,
100 HIF_REG_PAUSE_THRESHOLD_DEFAULT = 0,
103 /* Trailing status byte in Ethernet Rx frame */
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 */
114 struct mcs7830_data {
119 static const char driver_name[] = "MOSCHIP usb-ethernet driver";
121 static int mcs7830_get_reg(struct usbnet *dev, u16 index, u16 size, void *data)
123 struct usb_device *xdev = dev->udev;
127 buffer = kmalloc(size, GFP_NOIO);
131 ret = usb_control_msg(xdev, usb_rcvctrlpipe(xdev, 0), MCS7830_RD_BREQ,
132 MCS7830_RD_BMREQ, 0x0000, index, buffer,
133 size, MCS7830_CTRL_TIMEOUT);
134 memcpy(data, buffer, size);
140 static int mcs7830_set_reg(struct usbnet *dev, u16 index, u16 size, void *data)
142 struct usb_device *xdev = dev->udev;
146 buffer = kmalloc(size, GFP_NOIO);
150 memcpy(buffer, data, size);
152 ret = usb_control_msg(xdev, usb_sndctrlpipe(xdev, 0), MCS7830_WR_BREQ,
153 MCS7830_WR_BMREQ, 0x0000, index, buffer,
154 size, MCS7830_CTRL_TIMEOUT);
159 static void mcs7830_async_cmd_callback(struct urb *urb)
161 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
162 int status = urb->status;
165 printk(KERN_DEBUG "%s() failed with %d\n",
172 static void mcs7830_set_reg_async(struct usbnet *dev, u16 index, u16 size, void *data)
174 struct usb_ctrlrequest *req;
178 urb = usb_alloc_urb(0, GFP_ATOMIC);
180 dev_dbg(&dev->udev->dev,
181 "Error allocating URB in write_cmd_async!\n");
185 req = kmalloc(sizeof *req, GFP_ATOMIC);
187 dev_err(&dev->udev->dev,
188 "Failed to allocate memory for control request\n");
191 req->bRequestType = MCS7830_WR_BMREQ;
192 req->bRequest = MCS7830_WR_BREQ;
194 req->wIndex = cpu_to_le16(index);
195 req->wLength = cpu_to_le16(size);
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);
202 ret = usb_submit_urb(urb, GFP_ATOMIC);
204 dev_err(&dev->udev->dev,
205 "Error submitting the control message: ret=%d\n", ret);
214 static int mcs7830_get_address(struct usbnet *dev)
217 ret = mcs7830_get_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN,
224 static int mcs7830_read_phy(struct usbnet *dev, u8 index)
231 HIF_REG_PHY_CMD1_READ | HIF_REG_PHY_CMD1_PHYADDR,
232 HIF_REG_PHY_CMD2_PEND_FLAG_BIT | index,
235 mutex_lock(&dev->phy_mutex);
236 /* write the MII command */
237 ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
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))
252 /* read actual register contents */
253 ret = mcs7830_get_reg(dev, HIF_REG_PHY_DATA, 2, &val);
256 ret = le16_to_cpu(val);
257 dev_dbg(&dev->udev->dev, "read PHY reg %02x: %04x (%d tries)\n",
260 mutex_unlock(&dev->phy_mutex);
264 static int mcs7830_write_phy(struct usbnet *dev, u8 index, u16 val)
271 HIF_REG_PHY_CMD1_WRITE | HIF_REG_PHY_CMD1_PHYADDR,
272 HIF_REG_PHY_CMD2_PEND_FLAG_BIT | (index & 0x1F),
275 mutex_lock(&dev->phy_mutex);
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);
283 /* write the MII command */
284 ret = mcs7830_set_reg(dev, HIF_REG_PHY_CMD1, 2, cmd);
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))
300 dev_dbg(&dev->udev->dev, "write PHY reg %02x: %04x (%d tries)\n",
303 mutex_unlock(&dev->phy_mutex);
308 * This algorithm comes from the original mcs7830 version 1.4 driver,
309 * not sure if it is needed.
311 static int mcs7830_set_autoneg(struct usbnet *dev, int ptrUserPhyMode)
314 /* Enable all media types */
315 ret = mcs7830_write_phy(dev, MII_ADVERTISE, MCS7830_MII_ADVERTISE);
317 /* First reset BMCR */
319 ret = mcs7830_write_phy(dev, MII_BMCR, 0x0000);
320 /* Enable Auto Neg */
322 ret = mcs7830_write_phy(dev, MII_BMCR, BMCR_ANENABLE);
323 /* Restart Auto Neg (Keep the Enable Auto Neg Bit Set) */
325 ret = mcs7830_write_phy(dev, MII_BMCR,
326 BMCR_ANENABLE | BMCR_ANRESTART );
327 return ret < 0 ? : 0;
332 * if we can read register 22, the chip revision is C or higher
334 static int mcs7830_get_rev(struct usbnet *dev)
338 ret = mcs7830_get_reg(dev, HIF_REG_FRAME_DROP_COUNTER, 2, dummy);
340 return 2; /* Rev C or later */
341 return 1; /* earlier revision */
345 * On rev. C we need to set the pause threshold
347 static void mcs7830_rev_C_fixup(struct usbnet *dev)
349 u8 pause_threshold = HIF_REG_PAUSE_THRESHOLD_DEFAULT;
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);
362 static int mcs7830_init_dev(struct usbnet *dev)
367 /* Read MAC address from EEPROM */
369 for (retry = 0; retry < 5 && ret; retry++)
370 ret = mcs7830_get_address(dev);
372 dev_warn(&dev->udev->dev, "Cannot read MAC address\n");
377 ret = mcs7830_set_autoneg(dev, 0);
379 dev_info(&dev->udev->dev, "Cannot set autoneg\n");
383 mcs7830_rev_C_fixup(dev);
389 static int mcs7830_mdio_read(struct net_device *netdev, int phy_id,
392 struct usbnet *dev = netdev_priv(netdev);
393 return mcs7830_read_phy(dev, location);
396 static void mcs7830_mdio_write(struct net_device *netdev, int phy_id,
397 int location, int val)
399 struct usbnet *dev = netdev_priv(netdev);
400 mcs7830_write_phy(dev, location, val);
403 static int mcs7830_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
405 struct usbnet *dev = netdev_priv(net);
406 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
409 /* credits go to asix_set_multicast */
410 static void mcs7830_set_multicast(struct net_device *net)
412 struct usbnet *dev = netdev_priv(net);
413 struct mcs7830_data *data = (struct mcs7830_data *)&dev->data;
415 data->config = HIF_REG_CONFIG_TXENABLE;
417 /* this should not be needed, but it doesn't work otherwise */
418 data->config |= HIF_REG_CONFIG_ALLMULTICAST;
420 if (net->flags & IFF_PROMISC) {
421 data->config |= HIF_REG_CONFIG_PROMISCUOUS;
422 } else if (net->flags & IFF_ALLMULTI ||
423 net->mc_count > MCS7830_MAX_MCAST) {
424 data->config |= HIF_REG_CONFIG_ALLMULTICAST;
425 } else if (net->mc_count == 0) {
426 /* just broadcast and directed */
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;
436 memset(data->multi_filter, 0, sizeof data->multi_filter);
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;
445 mcs7830_set_reg_async(dev, HIF_REG_MULTICAST_HASH,
446 sizeof data->multi_filter,
450 mcs7830_set_reg_async(dev, HIF_REG_CONFIG, 1, &data->config);
453 static int mcs7830_get_regs_len(struct net_device *net)
455 struct usbnet *dev = netdev_priv(net);
457 switch (mcs7830_get_rev(dev)) {
466 static void mcs7830_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *drvinfo)
468 usbnet_get_drvinfo(net, drvinfo);
469 drvinfo->regdump_len = mcs7830_get_regs_len(net);
472 static void mcs7830_get_regs(struct net_device *net, struct ethtool_regs *regs, void *data)
474 struct usbnet *dev = netdev_priv(net);
476 regs->version = mcs7830_get_rev(dev);
477 mcs7830_get_reg(dev, 0, regs->len, data);
480 static const struct ethtool_ops mcs7830_ethtool_ops = {
481 .get_drvinfo = mcs7830_get_drvinfo,
482 .get_regs_len = mcs7830_get_regs_len,
483 .get_regs = mcs7830_get_regs,
485 /* common usbnet calls */
486 .get_link = usbnet_get_link,
487 .get_msglevel = usbnet_get_msglevel,
488 .set_msglevel = usbnet_set_msglevel,
489 .get_settings = usbnet_get_settings,
490 .set_settings = usbnet_set_settings,
491 .nway_reset = usbnet_nway_reset,
494 static int mcs7830_set_mac_address(struct net_device *netdev, void *p)
497 struct usbnet *dev = netdev_priv(netdev);
498 struct sockaddr *addr = p;
500 if (netif_running(netdev))
503 if (!is_valid_ether_addr(addr->sa_data))
506 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
508 ret = mcs7830_set_reg(dev, HIF_REG_ETHERNET_ADDR, ETH_ALEN,
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,
526 .ndo_set_mac_address = mcs7830_set_mac_address,
529 static int mcs7830_bind(struct usbnet *dev, struct usb_interface *udev)
531 struct net_device *net = dev->net;
534 ret = mcs7830_init_dev(dev);
538 net->ethtool_ops = &mcs7830_ethtool_ops;
539 net->netdev_ops = &mcs7830_netdev_ops;
540 mcs7830_set_multicast(net);
542 /* reserve space for the status byte on rx */
543 dev->rx_urb_size = ETH_FRAME_LEN + 1;
545 dev->mii.mdio_read = mcs7830_mdio_read;
546 dev->mii.mdio_write = mcs7830_mdio_write;
548 dev->mii.phy_id_mask = 0x3f;
549 dev->mii.reg_num_mask = 0x1f;
550 dev->mii.phy_id = *((u8 *) net->dev_addr + 1);
552 ret = usbnet_get_endpoints(dev, udev);
557 /* The chip always appends a status byte that we need to strip */
558 static int mcs7830_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
563 dev_err(&dev->udev->dev, "unexpected empty rx frame\n");
567 skb_trim(skb, skb->len - 1);
568 status = skb->data[skb->len];
570 if (status != MCS7830_RX_FRAME_CORRECT) {
571 dev_dbg(&dev->udev->dev, "rx fixup status %x\n", status);
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++;
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++;
590 static const struct driver_info moschip_info = {
591 .description = "MOSCHIP 7830/7730 usb-NET adapter",
592 .bind = mcs7830_bind,
593 .rx_fixup = mcs7830_rx_fixup,
599 static const struct driver_info sitecom_info = {
600 .description = "Sitecom LN-30 usb-NET adapter",
601 .bind = mcs7830_bind,
602 .rx_fixup = mcs7830_rx_fixup,
608 static const struct usb_device_id products[] = {
610 USB_DEVICE(MCS7830_VENDOR_ID, MCS7830_PRODUCT_ID),
611 .driver_info = (unsigned long) &moschip_info,
614 USB_DEVICE(MCS7830_VENDOR_ID, MCS7730_PRODUCT_ID),
615 .driver_info = (unsigned long) &moschip_info,
618 USB_DEVICE(SITECOM_VENDOR_ID, LN_030_PRODUCT_ID),
619 .driver_info = (unsigned long) &sitecom_info,
623 MODULE_DEVICE_TABLE(usb, products);
625 static struct usb_driver mcs7830_driver = {
627 .id_table = products,
628 .probe = usbnet_probe,
629 .disconnect = usbnet_disconnect,
630 .suspend = usbnet_suspend,
631 .resume = usbnet_resume,
634 static int __init mcs7830_init(void)
636 return usb_register(&mcs7830_driver);
638 module_init(mcs7830_init);
640 static void __exit mcs7830_exit(void)
642 usb_deregister(&mcs7830_driver);
644 module_exit(mcs7830_exit);
646 MODULE_DESCRIPTION("USB to network adapter MCS7830)");
647 MODULE_LICENSE("GPL");