2 * Davicom DM9601 USB 1.1 10/100Mbps ethernet devices
4 * Peter Korsgaard <jacmet@sunsite.dk>
6 * This file is licensed under the terms of the GNU General Public License
7 * version 2. This program is licensed "as is" without any warranty of any
8 * kind, whether express or implied.
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/stddef.h>
16 #include <linux/init.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/ethtool.h>
20 #include <linux/mii.h>
21 #include <linux/usb.h>
22 #include <linux/crc32.h>
23 #include <linux/usb/usbnet.h>
26 http://ptm2.cc.utu.fi/ftp/network/cards/DM9601/From_NET/DM9601-DS-P01-930914.pdf
29 /* control requests */
30 #define DM_READ_REGS 0x00
31 #define DM_WRITE_REGS 0x01
32 #define DM_READ_MEMS 0x02
33 #define DM_WRITE_REG 0x03
34 #define DM_WRITE_MEMS 0x05
35 #define DM_WRITE_MEM 0x07
38 #define DM_NET_CTRL 0x00
39 #define DM_RX_CTRL 0x05
40 #define DM_SHARED_CTRL 0x0b
41 #define DM_SHARED_ADDR 0x0c
42 #define DM_SHARED_DATA 0x0d /* low + high */
43 #define DM_PHY_ADDR 0x10 /* 6 bytes */
44 #define DM_MCAST_ADDR 0x16 /* 8 bytes */
45 #define DM_GPR_CTRL 0x1e
46 #define DM_GPR_DATA 0x1f
48 #define DM_MAX_MCAST 64
49 #define DM_MCAST_SIZE 8
50 #define DM_EEPROM_LEN 256
51 #define DM_TX_OVERHEAD 2 /* 2 byte header */
52 #define DM_RX_OVERHEAD 7 /* 3 byte header + 4 byte crc tail */
53 #define DM_TIMEOUT 1000
56 static int dm_read(struct usbnet *dev, u8 reg, u16 length, void *data)
61 devdbg(dev, "dm_read() reg=0x%02x length=%d", reg, length);
63 buf = kmalloc(length, GFP_KERNEL);
67 err = usb_control_msg(dev->udev,
68 usb_rcvctrlpipe(dev->udev, 0),
70 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
71 0, reg, buf, length, USB_CTRL_SET_TIMEOUT);
73 memcpy(data, buf, length);
82 static int dm_read_reg(struct usbnet *dev, u8 reg, u8 *value)
84 return dm_read(dev, reg, 1, value);
87 static int dm_write(struct usbnet *dev, u8 reg, u16 length, void *data)
92 devdbg(dev, "dm_write() reg=0x%02x, length=%d", reg, length);
95 buf = kmalloc(length, GFP_KERNEL);
98 memcpy(buf, data, length);
101 err = usb_control_msg(dev->udev,
102 usb_sndctrlpipe(dev->udev, 0),
104 USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
105 0, reg, buf, length, USB_CTRL_SET_TIMEOUT);
107 if (err >= 0 && err < length)
113 static int dm_write_reg(struct usbnet *dev, u8 reg, u8 value)
115 devdbg(dev, "dm_write_reg() reg=0x%02x, value=0x%02x", reg, value);
116 return usb_control_msg(dev->udev,
117 usb_sndctrlpipe(dev->udev, 0),
119 USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
120 value, reg, NULL, 0, USB_CTRL_SET_TIMEOUT);
123 static void dm_write_async_callback(struct urb *urb)
125 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
126 int status = urb->status;
129 printk(KERN_DEBUG "dm_write_async_callback() failed with %d\n",
136 static void dm_write_async_helper(struct usbnet *dev, u8 reg, u8 value,
137 u16 length, void *data)
139 struct usb_ctrlrequest *req;
143 urb = usb_alloc_urb(0, GFP_ATOMIC);
145 deverr(dev, "Error allocating URB in dm_write_async_helper!");
149 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
151 deverr(dev, "Failed to allocate memory for control request");
156 req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
157 req->bRequest = length ? DM_WRITE_REGS : DM_WRITE_REG;
158 req->wValue = cpu_to_le16(value);
159 req->wIndex = cpu_to_le16(reg);
160 req->wLength = cpu_to_le16(length);
162 usb_fill_control_urb(urb, dev->udev,
163 usb_sndctrlpipe(dev->udev, 0),
164 (void *)req, data, length,
165 dm_write_async_callback, req);
167 status = usb_submit_urb(urb, GFP_ATOMIC);
169 deverr(dev, "Error submitting the control message: status=%d",
176 static void dm_write_async(struct usbnet *dev, u8 reg, u16 length, void *data)
178 devdbg(dev, "dm_write_async() reg=0x%02x length=%d", reg, length);
180 dm_write_async_helper(dev, reg, 0, length, data);
183 static void dm_write_reg_async(struct usbnet *dev, u8 reg, u8 value)
185 devdbg(dev, "dm_write_reg_async() reg=0x%02x value=0x%02x",
188 dm_write_async_helper(dev, reg, value, 0, NULL);
191 static int dm_read_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 *value)
195 mutex_lock(&dev->phy_mutex);
197 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
198 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0xc : 0x4);
200 for (i = 0; i < DM_TIMEOUT; i++) {
204 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
213 if (i == DM_TIMEOUT) {
214 deverr(dev, "%s read timed out!", phy ? "phy" : "eeprom");
219 dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
220 ret = dm_read(dev, DM_SHARED_DATA, 2, value);
222 devdbg(dev, "read shared %d 0x%02x returned 0x%04x, %d",
223 phy, reg, *value, ret);
226 mutex_unlock(&dev->phy_mutex);
230 static int dm_write_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 value)
234 mutex_lock(&dev->phy_mutex);
236 ret = dm_write(dev, DM_SHARED_DATA, 2, &value);
240 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
241 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0x1c : 0x14);
243 for (i = 0; i < DM_TIMEOUT; i++) {
247 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
256 if (i == DM_TIMEOUT) {
257 deverr(dev, "%s write timed out!", phy ? "phy" : "eeprom");
262 dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
265 mutex_unlock(&dev->phy_mutex);
269 static int dm_read_eeprom_word(struct usbnet *dev, u8 offset, void *value)
271 return dm_read_shared_word(dev, 0, offset, value);
276 static int dm9601_get_eeprom_len(struct net_device *dev)
278 return DM_EEPROM_LEN;
281 static int dm9601_get_eeprom(struct net_device *net,
282 struct ethtool_eeprom *eeprom, u8 * data)
284 struct usbnet *dev = netdev_priv(net);
285 __le16 *ebuf = (__le16 *) data;
288 /* access is 16bit */
289 if ((eeprom->offset % 2) || (eeprom->len % 2))
292 for (i = 0; i < eeprom->len / 2; i++) {
293 if (dm_read_eeprom_word(dev, eeprom->offset / 2 + i,
300 static int dm9601_mdio_read(struct net_device *netdev, int phy_id, int loc)
302 struct usbnet *dev = netdev_priv(netdev);
307 devdbg(dev, "Only internal phy supported");
311 dm_read_shared_word(dev, 1, loc, &res);
314 "dm9601_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x",
315 phy_id, loc, le16_to_cpu(res));
317 return le16_to_cpu(res);
320 static void dm9601_mdio_write(struct net_device *netdev, int phy_id, int loc,
323 struct usbnet *dev = netdev_priv(netdev);
324 __le16 res = cpu_to_le16(val);
327 devdbg(dev, "Only internal phy supported");
331 devdbg(dev,"dm9601_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x",
334 dm_write_shared_word(dev, 1, loc, res);
337 static void dm9601_get_drvinfo(struct net_device *net,
338 struct ethtool_drvinfo *info)
340 /* Inherit standard device info */
341 usbnet_get_drvinfo(net, info);
342 info->eedump_len = DM_EEPROM_LEN;
345 static u32 dm9601_get_link(struct net_device *net)
347 struct usbnet *dev = netdev_priv(net);
349 return mii_link_ok(&dev->mii);
352 static int dm9601_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
354 struct usbnet *dev = netdev_priv(net);
356 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
359 static struct ethtool_ops dm9601_ethtool_ops = {
360 .get_drvinfo = dm9601_get_drvinfo,
361 .get_link = dm9601_get_link,
362 .get_msglevel = usbnet_get_msglevel,
363 .set_msglevel = usbnet_set_msglevel,
364 .get_eeprom_len = dm9601_get_eeprom_len,
365 .get_eeprom = dm9601_get_eeprom,
366 .get_settings = usbnet_get_settings,
367 .set_settings = usbnet_set_settings,
368 .nway_reset = usbnet_nway_reset,
371 static void dm9601_set_multicast(struct net_device *net)
373 struct usbnet *dev = netdev_priv(net);
374 /* We use the 20 byte dev->data for our 8 byte filter buffer
375 * to avoid allocating memory that is tricky to free later */
376 u8 *hashes = (u8 *) & dev->data;
379 memset(hashes, 0x00, DM_MCAST_SIZE);
380 hashes[DM_MCAST_SIZE - 1] |= 0x80; /* broadcast address */
382 if (net->flags & IFF_PROMISC) {
384 } else if (net->flags & IFF_ALLMULTI || net->mc_count > DM_MAX_MCAST) {
386 } else if (net->mc_count) {
387 struct dev_mc_list *mc_list = net->mc_list;
390 for (i = 0; i < net->mc_count; i++, mc_list = mc_list->next) {
391 u32 crc = ether_crc(ETH_ALEN, mc_list->dmi_addr) >> 26;
392 hashes[crc >> 3] |= 1 << (crc & 0x7);
396 dm_write_async(dev, DM_MCAST_ADDR, DM_MCAST_SIZE, hashes);
397 dm_write_reg_async(dev, DM_RX_CTRL, rx_ctl);
400 static void __dm9601_set_mac_address(struct usbnet *dev)
402 dm_write_async(dev, DM_PHY_ADDR, ETH_ALEN, dev->net->dev_addr);
405 static int dm9601_set_mac_address(struct net_device *net, void *p)
407 struct sockaddr *addr = p;
408 struct usbnet *dev = netdev_priv(net);
410 if (!is_valid_ether_addr(addr->sa_data)) {
411 dev_err(&net->dev, "not setting invalid mac address %pM\n",
416 memcpy(net->dev_addr, addr->sa_data, net->addr_len);
417 __dm9601_set_mac_address(dev);
422 static int dm9601_bind(struct usbnet *dev, struct usb_interface *intf)
427 ret = usbnet_get_endpoints(dev, intf);
431 dev->net->do_ioctl = dm9601_ioctl;
432 dev->net->set_multicast_list = dm9601_set_multicast;
433 dev->net->set_mac_address = dm9601_set_mac_address;
434 dev->net->ethtool_ops = &dm9601_ethtool_ops;
435 dev->net->hard_header_len += DM_TX_OVERHEAD;
436 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
437 dev->rx_urb_size = dev->net->mtu + ETH_HLEN + DM_RX_OVERHEAD;
439 dev->mii.dev = dev->net;
440 dev->mii.mdio_read = dm9601_mdio_read;
441 dev->mii.mdio_write = dm9601_mdio_write;
442 dev->mii.phy_id_mask = 0x1f;
443 dev->mii.reg_num_mask = 0x1f;
446 dm_write_reg(dev, DM_NET_CTRL, 1);
450 if (dm_read(dev, DM_PHY_ADDR, ETH_ALEN, mac) < 0) {
451 printk(KERN_ERR "Error reading MAC address\n");
457 * Overwrite the auto-generated address only with good ones.
459 if (is_valid_ether_addr(mac))
460 memcpy(dev->net->dev_addr, mac, ETH_ALEN);
463 "dm9601: No valid MAC address in EEPROM, using %pM\n",
465 __dm9601_set_mac_address(dev);
469 dm_write_reg(dev, DM_GPR_CTRL, 1);
470 dm_write_reg(dev, DM_GPR_DATA, 0);
472 /* receive broadcast packets */
473 dm9601_set_multicast(dev->net);
475 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
476 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
477 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
478 mii_nway_restart(&dev->mii);
484 static int dm9601_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
491 b1: packet length (incl crc) low
492 b2: packet length (incl crc) high
494 bn-3..bn: ethernet crc
497 if (unlikely(skb->len < DM_RX_OVERHEAD)) {
498 dev_err(&dev->udev->dev, "unexpected tiny rx frame\n");
502 status = skb->data[0];
503 len = (skb->data[1] | (skb->data[2] << 8)) - 4;
505 if (unlikely(status & 0xbf)) {
506 if (status & 0x01) dev->stats.rx_fifo_errors++;
507 if (status & 0x02) dev->stats.rx_crc_errors++;
508 if (status & 0x04) dev->stats.rx_frame_errors++;
509 if (status & 0x20) dev->stats.rx_missed_errors++;
510 if (status & 0x90) dev->stats.rx_length_errors++;
520 static struct sk_buff *dm9601_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
526 b0: packet length low
527 b1: packet length high
533 if (skb_headroom(skb) < DM_TX_OVERHEAD) {
534 struct sk_buff *skb2;
536 skb2 = skb_copy_expand(skb, DM_TX_OVERHEAD, 0, flags);
537 dev_kfree_skb_any(skb);
543 __skb_push(skb, DM_TX_OVERHEAD);
545 /* usbnet adds padding if length is a multiple of packet size
546 if so, adjust length value in header */
547 if ((skb->len % dev->maxpacket) == 0)
551 skb->data[1] = len >> 8;
556 static void dm9601_status(struct usbnet *dev, struct urb *urb)
572 if (urb->actual_length < 8)
575 buf = urb->transfer_buffer;
577 link = !!(buf[0] & 0x40);
578 if (netif_carrier_ok(dev->net) != link) {
580 netif_carrier_on(dev->net);
581 usbnet_defer_kevent (dev, EVENT_LINK_RESET);
584 netif_carrier_off(dev->net);
585 devdbg(dev, "Link Status is: %d", link);
589 static int dm9601_link_reset(struct usbnet *dev)
591 struct ethtool_cmd ecmd;
593 mii_check_media(&dev->mii, 1, 1);
594 mii_ethtool_gset(&dev->mii, &ecmd);
596 devdbg(dev, "link_reset() speed: %d duplex: %d",
597 ecmd.speed, ecmd.duplex);
602 static const struct driver_info dm9601_info = {
603 .description = "Davicom DM9601 USB Ethernet",
606 .rx_fixup = dm9601_rx_fixup,
607 .tx_fixup = dm9601_tx_fixup,
608 .status = dm9601_status,
609 .link_reset = dm9601_link_reset,
610 .reset = dm9601_link_reset,
613 static const struct usb_device_id products[] = {
615 USB_DEVICE(0x07aa, 0x9601), /* Corega FEther USB-TXC */
616 .driver_info = (unsigned long)&dm9601_info,
619 USB_DEVICE(0x0a46, 0x9601), /* Davicom USB-100 */
620 .driver_info = (unsigned long)&dm9601_info,
623 USB_DEVICE(0x0a46, 0x6688), /* ZT6688 USB NIC */
624 .driver_info = (unsigned long)&dm9601_info,
627 USB_DEVICE(0x0a46, 0x0268), /* ShanTou ST268 USB NIC */
628 .driver_info = (unsigned long)&dm9601_info,
631 USB_DEVICE(0x0a46, 0x8515), /* ADMtek ADM8515 USB NIC */
632 .driver_info = (unsigned long)&dm9601_info,
635 USB_DEVICE(0x0a47, 0x9601), /* Hirose USB-100 */
636 .driver_info = (unsigned long)&dm9601_info,
639 USB_DEVICE(0x0fe6, 0x8101), /* DM9601 USB to Fast Ethernet Adapter */
640 .driver_info = (unsigned long)&dm9601_info,
645 MODULE_DEVICE_TABLE(usb, products);
647 static struct usb_driver dm9601_driver = {
649 .id_table = products,
650 .probe = usbnet_probe,
651 .disconnect = usbnet_disconnect,
652 .suspend = usbnet_suspend,
653 .resume = usbnet_resume,
656 static int __init dm9601_init(void)
658 return usb_register(&dm9601_driver);
661 static void __exit dm9601_exit(void)
663 usb_deregister(&dm9601_driver);
666 module_init(dm9601_init);
667 module_exit(dm9601_exit);
669 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
670 MODULE_DESCRIPTION("Davicom DM9601 USB 1.1 ethernet devices");
671 MODULE_LICENSE("GPL");