2 * MosChips MCS7830 based USB 2.0 Ethernet Devices
4 * based on usbnet.c, asix.c and the vendor provided mcs7830 driver
6 * Copyright (C) 2006 Arnd Bergmann <arnd@arndb.de>
7 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
8 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
9 * Copyright (c) 2002-2003 TiVo Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/crc32.h>
27 #include <linux/etherdevice.h>
28 #include <linux/ethtool.h>
29 #include <linux/init.h>
30 #include <linux/mii.h>
31 #include <linux/module.h>
32 #include <linux/netdevice.h>
33 #include <linux/usb.h>
34 #include <linux/usb/usbnet.h>
37 #define MCS7830_RD_BMREQ (USB_DIR_IN | USB_TYPE_VENDOR | \
39 #define MCS7830_WR_BMREQ (USB_DIR_OUT | USB_TYPE_VENDOR | \
41 #define MCS7830_RD_BREQ 0x0E
42 #define MCS7830_WR_BREQ 0x0D
44 #define MCS7830_CTRL_TIMEOUT 1000
45 #define MCS7830_MAX_MCAST 64
47 #define MCS7830_VENDOR_ID 0x9710
48 #define MCS7830_PRODUCT_ID 0x7830
50 #define MCS7830_MII_ADVERTISE (ADVERTISE_PAUSE_CAP | ADVERTISE_100FULL | \
51 ADVERTISE_100HALF | ADVERTISE_10FULL | \
52 ADVERTISE_10HALF | ADVERTISE_CSMA)
54 /* HIF_REG_XX coressponding index value */
56 HIF_REG_MULTICAST_HASH
= 0x00,
57 HIF_REG_PACKET_GAP1
= 0x08,
58 HIF_REG_PACKET_GAP2
= 0x09,
59 HIF_REG_PHY_DATA
= 0x0a,
60 HIF_REG_PHY_CMD1
= 0x0c,
61 HIF_REG_PHY_CMD1_READ
= 0x40,
62 HIF_REG_PHY_CMD1_WRITE
= 0x20,
63 HIF_REG_PHY_CMD1_PHYADDR
= 0x01,
64 HIF_REG_PHY_CMD2
= 0x0d,
65 HIF_REG_PHY_CMD2_PEND_FLAG_BIT
= 0x80,
66 HIF_REG_PHY_CMD2_READY_FLAG_BIT
= 0x40,
67 HIF_REG_CONFIG
= 0x0e,
68 HIF_REG_CONFIG_CFG
= 0x80,
69 HIF_REG_CONFIG_SPEED100
= 0x40,
70 HIF_REG_CONFIG_FULLDUPLEX_ENABLE
= 0x20,
71 HIF_REG_CONFIG_RXENABLE
= 0x10,
72 HIF_REG_CONFIG_TXENABLE
= 0x08,
73 HIF_REG_CONFIG_SLEEPMODE
= 0x04,
74 HIF_REG_CONFIG_ALLMULTICAST
= 0x02,
75 HIF_REG_CONFIG_PROMISCIOUS
= 0x01,
76 HIF_REG_ETHERNET_ADDR
= 0x0f,
78 HIF_REG_PAUSE_THRESHOLD
= 0x16,
79 HIF_REG_PAUSE_THRESHOLD_DEFAULT
= 0,
87 static const char driver_name
[] = "MOSCHIP usb-ethernet driver";
89 static int mcs7830_get_reg(struct usbnet
*dev
, u16 index
, u16 size
, void *data
)
91 struct usb_device
*xdev
= dev
->udev
;
94 ret
= usb_control_msg(xdev
, usb_rcvctrlpipe(xdev
, 0), MCS7830_RD_BREQ
,
95 MCS7830_RD_BMREQ
, 0x0000, index
, data
,
96 size
, MCS7830_CTRL_TIMEOUT
);
100 static int mcs7830_set_reg(struct usbnet
*dev
, u16 index
, u16 size
, void *data
)
102 struct usb_device
*xdev
= dev
->udev
;
105 ret
= usb_control_msg(xdev
, usb_sndctrlpipe(xdev
, 0), MCS7830_WR_BREQ
,
106 MCS7830_WR_BMREQ
, 0x0000, index
, data
,
107 size
, MCS7830_CTRL_TIMEOUT
);
111 static void mcs7830_async_cmd_callback(struct urb
*urb
)
113 struct usb_ctrlrequest
*req
= (struct usb_ctrlrequest
*)urb
->context
;
116 printk(KERN_DEBUG
"%s() failed with %d\n",
117 __FUNCTION__
, urb
->status
);
123 static void mcs7830_set_reg_async(struct usbnet
*dev
, u16 index
, u16 size
, void *data
)
125 struct usb_ctrlrequest
*req
;
129 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
131 dev_dbg(&dev
->udev
->dev
,
132 "Error allocating URB in write_cmd_async!\n");
136 req
= kmalloc(sizeof *req
, GFP_ATOMIC
);
138 dev_err(&dev
->udev
->dev
,
139 "Failed to allocate memory for control request\n");
142 req
->bRequestType
= MCS7830_WR_BMREQ
;
143 req
->bRequest
= MCS7830_WR_BREQ
;
145 req
->wIndex
= cpu_to_le16(index
);
146 req
->wLength
= cpu_to_le16(size
);
148 usb_fill_control_urb(urb
, dev
->udev
,
149 usb_sndctrlpipe(dev
->udev
, 0),
150 (void *)req
, data
, size
,
151 mcs7830_async_cmd_callback
, req
);
153 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
155 dev_err(&dev
->udev
->dev
,
156 "Error submitting the control message: ret=%d\n", ret
);
165 static int mcs7830_get_address(struct usbnet
*dev
)
168 ret
= mcs7830_get_reg(dev
, HIF_REG_ETHERNET_ADDR
, ETH_ALEN
,
175 static int mcs7830_read_phy(struct usbnet
*dev
, u8 index
)
182 HIF_REG_PHY_CMD1_READ
| HIF_REG_PHY_CMD1_PHYADDR
,
183 HIF_REG_PHY_CMD2_PEND_FLAG_BIT
| index
,
186 mutex_lock(&dev
->phy_mutex
);
187 /* write the MII command */
188 ret
= mcs7830_set_reg(dev
, HIF_REG_PHY_CMD1
, 2, cmd
);
192 /* wait for the data to become valid, should be within < 1ms */
193 for (i
= 0; i
< 10; i
++) {
194 ret
= mcs7830_get_reg(dev
, HIF_REG_PHY_CMD1
, 2, cmd
);
195 if ((ret
< 0) || (cmd
[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT
))
203 /* read actual register contents */
204 ret
= mcs7830_get_reg(dev
, HIF_REG_PHY_DATA
, 2, &val
);
207 ret
= le16_to_cpu(val
);
208 dev_dbg(&dev
->udev
->dev
, "read PHY reg %02x: %04x (%d tries)\n",
211 mutex_unlock(&dev
->phy_mutex
);
215 static int mcs7830_write_phy(struct usbnet
*dev
, u8 index
, u16 val
)
222 HIF_REG_PHY_CMD1_WRITE
| HIF_REG_PHY_CMD1_PHYADDR
,
223 HIF_REG_PHY_CMD2_PEND_FLAG_BIT
| (index
& 0x1F),
226 mutex_lock(&dev
->phy_mutex
);
228 /* write the new register contents */
229 le_val
= cpu_to_le16(val
);
230 ret
= mcs7830_set_reg(dev
, HIF_REG_PHY_DATA
, 2, &le_val
);
234 /* write the MII command */
235 ret
= mcs7830_set_reg(dev
, HIF_REG_PHY_CMD1
, 2, cmd
);
239 /* wait for the command to be accepted by the PHY */
240 for (i
= 0; i
< 10; i
++) {
241 ret
= mcs7830_get_reg(dev
, HIF_REG_PHY_CMD1
, 2, cmd
);
242 if ((ret
< 0) || (cmd
[1] & HIF_REG_PHY_CMD2_READY_FLAG_BIT
))
251 dev_dbg(&dev
->udev
->dev
, "write PHY reg %02x: %04x (%d tries)\n",
254 mutex_unlock(&dev
->phy_mutex
);
259 * This algorithm comes from the original mcs7830 version 1.4 driver,
260 * not sure if it is needed.
262 static int mcs7830_set_autoneg(struct usbnet
*dev
, int ptrUserPhyMode
)
265 /* Enable all media types */
266 ret
= mcs7830_write_phy(dev
, MII_ADVERTISE
, MCS7830_MII_ADVERTISE
);
268 /* First reset BMCR */
270 ret
= mcs7830_write_phy(dev
, MII_BMCR
, 0x0000);
271 /* Enable Auto Neg */
273 ret
= mcs7830_write_phy(dev
, MII_BMCR
, BMCR_ANENABLE
);
274 /* Restart Auto Neg (Keep the Enable Auto Neg Bit Set) */
276 ret
= mcs7830_write_phy(dev
, MII_BMCR
,
277 BMCR_ANENABLE
| BMCR_ANRESTART
);
278 return ret
< 0 ? : 0;
283 * if we can read register 22, the chip revision is C or higher
285 static int mcs7830_get_rev(struct usbnet
*dev
)
289 ret
= mcs7830_get_reg(dev
, HIF_REG_22
, 2, dummy
);
291 return 2; /* Rev C or later */
292 return 1; /* earlier revision */
296 * On rev. C we need to set the pause threshold
298 static void mcs7830_rev_C_fixup(struct usbnet
*dev
)
300 u8 pause_threshold
= HIF_REG_PAUSE_THRESHOLD_DEFAULT
;
303 for (retry
= 0; retry
< 2; retry
++) {
304 if (mcs7830_get_rev(dev
) == 2) {
305 dev_info(&dev
->udev
->dev
, "applying rev.C fixup\n");
306 mcs7830_set_reg(dev
, HIF_REG_PAUSE_THRESHOLD
,
307 1, &pause_threshold
);
313 static int mcs7830_init_dev(struct usbnet
*dev
)
318 /* Read MAC address from EEPROM */
320 for (retry
= 0; retry
< 5 && ret
; retry
++)
321 ret
= mcs7830_get_address(dev
);
323 dev_warn(&dev
->udev
->dev
, "Cannot read MAC address\n");
328 ret
= mcs7830_set_autoneg(dev
, 0);
330 dev_info(&dev
->udev
->dev
, "Cannot set autoneg\n");
334 mcs7830_rev_C_fixup(dev
);
340 static int mcs7830_mdio_read(struct net_device
*netdev
, int phy_id
,
343 struct usbnet
*dev
= netdev
->priv
;
344 return mcs7830_read_phy(dev
, location
);
347 static void mcs7830_mdio_write(struct net_device
*netdev
, int phy_id
,
348 int location
, int val
)
350 struct usbnet
*dev
= netdev
->priv
;
351 mcs7830_write_phy(dev
, location
, val
);
354 static int mcs7830_ioctl(struct net_device
*net
, struct ifreq
*rq
, int cmd
)
356 struct usbnet
*dev
= netdev_priv(net
);
357 return generic_mii_ioctl(&dev
->mii
, if_mii(rq
), cmd
, NULL
);
360 /* credits go to asix_set_multicast */
361 static void mcs7830_set_multicast(struct net_device
*net
)
363 struct usbnet
*dev
= netdev_priv(net
);
364 struct mcs7830_data
*data
= (struct mcs7830_data
*)&dev
->data
;
366 data
->config
= HIF_REG_CONFIG_TXENABLE
;
368 /* this should not be needed, but it doesn't work otherwise */
369 data
->config
|= HIF_REG_CONFIG_ALLMULTICAST
;
371 if (net
->flags
& IFF_PROMISC
) {
372 data
->config
|= HIF_REG_CONFIG_PROMISCIOUS
;
373 } else if (net
->flags
& IFF_ALLMULTI
374 || net
->mc_count
> MCS7830_MAX_MCAST
) {
375 data
->config
|= HIF_REG_CONFIG_ALLMULTICAST
;
376 } else if (net
->mc_count
== 0) {
377 /* just broadcast and directed */
379 /* We use the 20 byte dev->data
380 * for our 8 byte filter buffer
381 * to avoid allocating memory that
382 * is tricky to free later */
383 struct dev_mc_list
*mc_list
= net
->mc_list
;
387 memset(data
->multi_filter
, 0, sizeof data
->multi_filter
);
389 /* Build the multicast hash filter. */
390 for (i
= 0; i
< net
->mc_count
; i
++) {
391 crc_bits
= ether_crc(ETH_ALEN
, mc_list
->dmi_addr
) >> 26;
392 data
->multi_filter
[crc_bits
>> 3] |= 1 << (crc_bits
& 7);
393 mc_list
= mc_list
->next
;
396 mcs7830_set_reg_async(dev
, HIF_REG_MULTICAST_HASH
,
397 sizeof data
->multi_filter
,
401 mcs7830_set_reg_async(dev
, HIF_REG_CONFIG
, 1, &data
->config
);
404 static int mcs7830_get_regs_len(struct net_device
*net
)
406 struct usbnet
*dev
= netdev_priv(net
);
408 switch (mcs7830_get_rev(dev
)) {
417 static void mcs7830_get_drvinfo(struct net_device
*net
, struct ethtool_drvinfo
*drvinfo
)
419 usbnet_get_drvinfo(net
, drvinfo
);
420 drvinfo
->regdump_len
= mcs7830_get_regs_len(net
);
423 static void mcs7830_get_regs(struct net_device
*net
, struct ethtool_regs
*regs
, void *data
)
425 struct usbnet
*dev
= netdev_priv(net
);
427 regs
->version
= mcs7830_get_rev(dev
);
428 mcs7830_get_reg(dev
, 0, regs
->len
, data
);
431 static struct ethtool_ops mcs7830_ethtool_ops
= {
432 .get_drvinfo
= mcs7830_get_drvinfo
,
433 .get_regs_len
= mcs7830_get_regs_len
,
434 .get_regs
= mcs7830_get_regs
,
436 /* common usbnet calls */
437 .get_link
= usbnet_get_link
,
438 .get_msglevel
= usbnet_get_msglevel
,
439 .set_msglevel
= usbnet_set_msglevel
,
440 .get_settings
= usbnet_get_settings
,
441 .set_settings
= usbnet_set_settings
,
442 .nway_reset
= usbnet_nway_reset
,
445 static int mcs7830_bind(struct usbnet
*dev
, struct usb_interface
*udev
)
447 struct net_device
*net
= dev
->net
;
450 ret
= mcs7830_init_dev(dev
);
454 net
->do_ioctl
= mcs7830_ioctl
;
455 net
->ethtool_ops
= &mcs7830_ethtool_ops
;
456 net
->set_multicast_list
= mcs7830_set_multicast
;
457 mcs7830_set_multicast(net
);
459 /* reserve space for the status byte on rx */
460 dev
->rx_urb_size
= ETH_FRAME_LEN
+ 1;
462 dev
->mii
.mdio_read
= mcs7830_mdio_read
;
463 dev
->mii
.mdio_write
= mcs7830_mdio_write
;
465 dev
->mii
.phy_id_mask
= 0x3f;
466 dev
->mii
.reg_num_mask
= 0x1f;
467 dev
->mii
.phy_id
= *((u8
*) net
->dev_addr
+ 1);
469 ret
= usbnet_get_endpoints(dev
, udev
);
474 /* The chip always appends a status bytes that we need to strip */
475 static int mcs7830_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
480 dev_err(&dev
->udev
->dev
, "unexpected empty rx frame\n");
484 skb_trim(skb
, skb
->len
- 1);
485 status
= skb
->data
[skb
->len
];
488 dev_dbg(&dev
->udev
->dev
, "rx fixup status %x\n", status
);
493 static const struct driver_info moschip_info
= {
494 .description
= "MOSCHIP 7830 usb-NET adapter",
495 .bind
= mcs7830_bind
,
496 .rx_fixup
= mcs7830_rx_fixup
,
502 static const struct usb_device_id products
[] = {
504 USB_DEVICE(MCS7830_VENDOR_ID
, MCS7830_PRODUCT_ID
),
505 .driver_info
= (unsigned long) &moschip_info
,
509 MODULE_DEVICE_TABLE(usb
, products
);
511 static struct usb_driver mcs7830_driver
= {
513 .id_table
= products
,
514 .probe
= usbnet_probe
,
515 .disconnect
= usbnet_disconnect
,
516 .suspend
= usbnet_suspend
,
517 .resume
= usbnet_resume
,
520 static int __init
mcs7830_init(void)
522 return usb_register(&mcs7830_driver
);
524 module_init(mcs7830_init
);
526 static void __exit
mcs7830_exit(void)
528 usb_deregister(&mcs7830_driver
);
530 module_exit(mcs7830_exit
);
532 MODULE_DESCRIPTION("USB to network adapter MCS7830)");
533 MODULE_LICENSE("GPL");