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/init.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/ethtool.h>
19 #include <linux/mii.h>
20 #include <linux/usb.h>
21 #include <linux/crc32.h>
26 http://www.davicom.com.tw/big5/download/Data%20Sheet/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)
58 devdbg(dev, "dm_read() reg=0x%02x length=%d", reg, length);
59 return usb_control_msg(dev->udev,
60 usb_rcvctrlpipe(dev->udev, 0),
62 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
63 0, reg, data, length, USB_CTRL_SET_TIMEOUT);
66 static int dm_read_reg(struct usbnet *dev, u8 reg, u8 *value)
68 return dm_read(dev, reg, 1, value);
71 static int dm_write(struct usbnet *dev, u8 reg, u16 length, void *data)
73 devdbg(dev, "dm_write() reg=0x%02x, length=%d", reg, length);
74 return usb_control_msg(dev->udev,
75 usb_sndctrlpipe(dev->udev, 0),
77 USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
78 0, reg, data, length, USB_CTRL_SET_TIMEOUT);
81 static int dm_write_reg(struct usbnet *dev, u8 reg, u8 value)
83 devdbg(dev, "dm_write_reg() reg=0x%02x, value=0x%02x", reg, value);
84 return usb_control_msg(dev->udev,
85 usb_sndctrlpipe(dev->udev, 0),
87 USB_DIR_OUT | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
88 value, reg, 0, 0, USB_CTRL_SET_TIMEOUT);
91 static void dm_write_async_callback(struct urb *urb)
93 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
96 printk(KERN_DEBUG "dm_write_async_callback() failed with %d",
103 static void dm_write_async(struct usbnet *dev, u8 reg, u16 length, void *data)
105 struct usb_ctrlrequest *req;
109 devdbg(dev, "dm_write_async() reg=0x%02x length=%d", reg, length);
111 urb = usb_alloc_urb(0, GFP_ATOMIC);
113 deverr(dev, "Error allocating URB in dm_write_async!");
117 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
119 deverr(dev, "Failed to allocate memory for control request");
124 req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
125 req->bRequest = DM_WRITE_REGS;
127 req->wIndex = cpu_to_le16(reg);
128 req->wLength = cpu_to_le16(length);
130 usb_fill_control_urb(urb, dev->udev,
131 usb_sndctrlpipe(dev->udev, 0),
132 (void *)req, data, length,
133 dm_write_async_callback, req);
135 status = usb_submit_urb(urb, GFP_ATOMIC);
137 deverr(dev, "Error submitting the control message: status=%d",
144 static void dm_write_reg_async(struct usbnet *dev, u8 reg, u8 value)
146 struct usb_ctrlrequest *req;
150 devdbg(dev, "dm_write_reg_async() reg=0x%02x value=0x%02x",
153 urb = usb_alloc_urb(0, GFP_ATOMIC);
155 deverr(dev, "Error allocating URB in dm_write_async!");
159 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
161 deverr(dev, "Failed to allocate memory for control request");
166 req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
167 req->bRequest = DM_WRITE_REG;
168 req->wValue = cpu_to_le16(value);
169 req->wIndex = cpu_to_le16(reg);
172 usb_fill_control_urb(urb, dev->udev,
173 usb_sndctrlpipe(dev->udev, 0),
174 (void *)req, 0, 0, dm_write_async_callback, req);
176 status = usb_submit_urb(urb, GFP_ATOMIC);
178 deverr(dev, "Error submitting the control message: status=%d",
185 static int dm_read_shared_word(struct usbnet *dev, int phy, u8 reg, u16 *value)
189 mutex_lock(&dev->phy_mutex);
191 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
192 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0xc : 0x4);
194 for (i = 0; i < DM_TIMEOUT; i++) {
198 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
207 if (i == DM_TIMEOUT) {
208 deverr(dev, "%s read timed out!", phy ? "phy" : "eeprom");
213 dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
214 ret = dm_read(dev, DM_SHARED_DATA, 2, value);
216 devdbg(dev, "read shared %d 0x%02x returned 0x%04x, %d",
217 phy, reg, *value, ret);
220 mutex_unlock(&dev->phy_mutex);
224 static int dm_write_shared_word(struct usbnet *dev, int phy, u8 reg, u16 value)
228 mutex_lock(&dev->phy_mutex);
230 ret = dm_write(dev, DM_SHARED_DATA, 2, &value);
234 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
235 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0x1c : 0x14);
237 for (i = 0; i < DM_TIMEOUT; i++) {
241 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
250 if (i == DM_TIMEOUT) {
251 deverr(dev, "%s write timed out!", phy ? "phy" : "eeprom");
256 dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
259 mutex_unlock(&dev->phy_mutex);
263 static int dm_read_eeprom_word(struct usbnet *dev, u8 offset, void *value)
265 return dm_read_shared_word(dev, 0, offset, value);
270 static int dm9601_get_eeprom_len(struct net_device *dev)
272 return DM_EEPROM_LEN;
275 static int dm9601_get_eeprom(struct net_device *net,
276 struct ethtool_eeprom *eeprom, u8 * data)
278 struct usbnet *dev = netdev_priv(net);
279 u16 *ebuf = (u16 *) data;
282 /* access is 16bit */
283 if ((eeprom->offset % 2) || (eeprom->len % 2))
286 for (i = 0; i < eeprom->len / 2; i++) {
287 if (dm_read_eeprom_word(dev, eeprom->offset / 2 + i,
294 static int dm9601_mdio_read(struct net_device *netdev, int phy_id, int loc)
296 struct usbnet *dev = netdev_priv(netdev);
301 devdbg(dev, "Only internal phy supported");
305 dm_read_shared_word(dev, 1, loc, &res);
308 "dm9601_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x",
309 phy_id, loc, le16_to_cpu(res));
311 return le16_to_cpu(res);
314 static void dm9601_mdio_write(struct net_device *netdev, int phy_id, int loc,
317 struct usbnet *dev = netdev_priv(netdev);
318 u16 res = cpu_to_le16(val);
321 devdbg(dev, "Only internal phy supported");
325 devdbg(dev,"dm9601_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x",
328 dm_write_shared_word(dev, 1, loc, res);
331 static void dm9601_get_drvinfo(struct net_device *net,
332 struct ethtool_drvinfo *info)
334 /* Inherit standard device info */
335 usbnet_get_drvinfo(net, info);
336 info->eedump_len = DM_EEPROM_LEN;
339 static u32 dm9601_get_link(struct net_device *net)
341 struct usbnet *dev = netdev_priv(net);
343 return mii_link_ok(&dev->mii);
346 static int dm9601_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
348 struct usbnet *dev = netdev_priv(net);
350 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
353 static struct ethtool_ops dm9601_ethtool_ops = {
354 .get_drvinfo = dm9601_get_drvinfo,
355 .get_link = dm9601_get_link,
356 .get_msglevel = usbnet_get_msglevel,
357 .set_msglevel = usbnet_set_msglevel,
358 .get_eeprom_len = dm9601_get_eeprom_len,
359 .get_eeprom = dm9601_get_eeprom,
360 .get_settings = usbnet_get_settings,
361 .set_settings = usbnet_set_settings,
362 .nway_reset = usbnet_nway_reset,
365 static void dm9601_set_multicast(struct net_device *net)
367 struct usbnet *dev = netdev_priv(net);
368 /* We use the 20 byte dev->data for our 8 byte filter buffer
369 * to avoid allocating memory that is tricky to free later */
370 u8 *hashes = (u8 *) & dev->data;
373 memset(hashes, 0x00, DM_MCAST_SIZE);
374 hashes[DM_MCAST_SIZE - 1] |= 0x80; /* broadcast address */
376 if (net->flags & IFF_PROMISC) {
378 } else if (net->flags & IFF_ALLMULTI || net->mc_count > DM_MAX_MCAST) {
380 } else if (net->mc_count) {
381 struct dev_mc_list *mc_list = net->mc_list;
384 for (i = 0; i < net->mc_count; i++) {
385 u32 crc = ether_crc(ETH_ALEN, mc_list->dmi_addr) >> 26;
386 hashes[crc >> 3] |= 1 << (crc & 0x7);
390 dm_write_async(dev, DM_MCAST_ADDR, DM_MCAST_SIZE, hashes);
391 dm_write_reg_async(dev, DM_RX_CTRL, rx_ctl);
394 static int dm9601_bind(struct usbnet *dev, struct usb_interface *intf)
398 ret = usbnet_get_endpoints(dev, intf);
402 dev->net->do_ioctl = dm9601_ioctl;
403 dev->net->set_multicast_list = dm9601_set_multicast;
404 dev->net->ethtool_ops = &dm9601_ethtool_ops;
405 dev->net->hard_header_len += DM_TX_OVERHEAD;
406 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
407 dev->rx_urb_size = dev->net->mtu + DM_RX_OVERHEAD;
409 dev->mii.dev = dev->net;
410 dev->mii.mdio_read = dm9601_mdio_read;
411 dev->mii.mdio_write = dm9601_mdio_write;
412 dev->mii.phy_id_mask = 0x1f;
413 dev->mii.reg_num_mask = 0x1f;
416 ret = dm_write_reg(dev, DM_NET_CTRL, 1);
420 ret = dm_read(dev, DM_PHY_ADDR, ETH_ALEN, dev->net->dev_addr);
422 printk(KERN_ERR "Error reading MAC address\n");
429 dm_write_reg(dev, DM_GPR_CTRL, 1);
430 dm_write_reg(dev, DM_GPR_DATA, 0);
432 /* receive broadcast packets */
433 dm9601_set_multicast(dev->net);
435 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
436 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
437 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
438 mii_nway_restart(&dev->mii);
444 static int dm9601_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
451 b1: packet length (incl crc) low
452 b2: packet length (incl crc) high
454 bn-3..bn: ethernet crc
457 if (unlikely(skb->len < DM_RX_OVERHEAD)) {
458 dev_err(&dev->udev->dev, "unexpected tiny rx frame\n");
462 status = skb->data[0];
463 len = (skb->data[1] | (skb->data[2] << 8)) - 4;
465 if (unlikely(status & 0xbf)) {
466 if (status & 0x01) dev->stats.rx_fifo_errors++;
467 if (status & 0x02) dev->stats.rx_crc_errors++;
468 if (status & 0x04) dev->stats.rx_frame_errors++;
469 if (status & 0x20) dev->stats.rx_missed_errors++;
470 if (status & 0x90) dev->stats.rx_length_errors++;
480 static struct sk_buff *dm9601_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
486 b0: packet length low
487 b1: packet length high
491 if (skb_headroom(skb) < DM_TX_OVERHEAD) {
492 struct sk_buff *skb2;
494 skb2 = skb_copy_expand(skb, DM_TX_OVERHEAD, 0, flags);
495 dev_kfree_skb_any(skb);
501 __skb_push(skb, DM_TX_OVERHEAD);
504 /* usbnet adds padding if length is a multiple of packet size
505 if so, adjust length value in header */
506 if ((len % dev->maxpacket) == 0)
510 skb->data[1] = len >> 8;
515 static void dm9601_status(struct usbnet *dev, struct urb *urb)
531 if (urb->actual_length < 8)
534 buf = urb->transfer_buffer;
536 link = !!(buf[0] & 0x40);
537 if (netif_carrier_ok(dev->net) != link) {
539 netif_carrier_on(dev->net);
540 usbnet_defer_kevent (dev, EVENT_LINK_RESET);
543 netif_carrier_off(dev->net);
544 devdbg(dev, "Link Status is: %d", link);
548 static int dm9601_link_reset(struct usbnet *dev)
550 struct ethtool_cmd ecmd;
552 mii_check_media(&dev->mii, 1, 1);
553 mii_ethtool_gset(&dev->mii, &ecmd);
555 devdbg(dev, "link_reset() speed: %d duplex: %d",
556 ecmd.speed, ecmd.duplex);
561 static const struct driver_info dm9601_info = {
562 .description = "Davicom DM9601 USB Ethernet",
565 .rx_fixup = dm9601_rx_fixup,
566 .tx_fixup = dm9601_tx_fixup,
567 .status = dm9601_status,
568 .link_reset = dm9601_link_reset,
569 .reset = dm9601_link_reset,
572 static const struct usb_device_id products[] = {
574 USB_DEVICE(0x07aa, 0x9601), /* Corega FEther USB-TXC */
575 .driver_info = (unsigned long)&dm9601_info,
578 USB_DEVICE(0x0a46, 0x9601), /* Davicom USB-100 */
579 .driver_info = (unsigned long)&dm9601_info,
582 USB_DEVICE(0x0a46, 0x6688), /* ZT6688 USB NIC */
583 .driver_info = (unsigned long)&dm9601_info,
586 USB_DEVICE(0x0a46, 0x0268), /* ShanTou ST268 USB NIC */
587 .driver_info = (unsigned long)&dm9601_info,
592 MODULE_DEVICE_TABLE(usb, products);
594 static struct usb_driver dm9601_driver = {
596 .id_table = products,
597 .probe = usbnet_probe,
598 .disconnect = usbnet_disconnect,
599 .suspend = usbnet_suspend,
600 .resume = usbnet_resume,
603 static int __init dm9601_init(void)
605 return usb_register(&dm9601_driver);
608 static void __exit dm9601_exit(void)
610 usb_deregister(&dm9601_driver);
613 module_init(dm9601_init);
614 module_exit(dm9601_exit);
616 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
617 MODULE_DESCRIPTION("Davicom DM9601 USB 1.1 ethernet devices");
618 MODULE_LICENSE("GPL");