2 * Davicom DM96xx USB 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/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>
22 #include <linux/usb/usbnet.h>
23 #include <linux/slab.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
47 #define DM_CHIP_ID 0x2c
48 #define DM_MODE_CTRL 0x91 /* only on dm9620 */
54 #define DM_MAX_MCAST 64
55 #define DM_MCAST_SIZE 8
56 #define DM_EEPROM_LEN 256
57 #define DM_TX_OVERHEAD 2 /* 2 byte header */
58 #define DM_RX_OVERHEAD 7 /* 3 byte header + 4 byte crc tail */
59 #define DM_TIMEOUT 1000
61 static int dm_read(struct usbnet *dev, u8 reg, u16 length, void *data)
64 err = usbnet_read_cmd(dev, DM_READ_REGS,
65 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
66 0, reg, data, length);
67 if(err != length && err >= 0)
72 static int dm_read_reg(struct usbnet *dev, u8 reg, u8 *value)
74 return dm_read(dev, reg, 1, value);
77 static int dm_write(struct usbnet *dev, u8 reg, u16 length, void *data)
80 err = usbnet_write_cmd(dev, DM_WRITE_REGS,
81 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
82 0, reg, data, length);
84 if (err >= 0 && err < length)
89 static int dm_write_reg(struct usbnet *dev, u8 reg, u8 value)
91 return usbnet_write_cmd(dev, DM_WRITE_REG,
92 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
96 static void dm_write_async(struct usbnet *dev, u8 reg, u16 length, void *data)
98 usbnet_write_cmd_async(dev, DM_WRITE_REGS,
99 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
100 0, reg, data, length);
103 static void dm_write_reg_async(struct usbnet *dev, u8 reg, u8 value)
105 usbnet_write_cmd_async(dev, DM_WRITE_REG,
106 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
107 value, reg, NULL, 0);
110 static int dm_read_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 *value)
114 mutex_lock(&dev->phy_mutex);
116 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
117 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0xc : 0x4);
119 for (i = 0; i < DM_TIMEOUT; i++) {
123 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
132 if (i == DM_TIMEOUT) {
133 netdev_err(dev->net, "%s read timed out!\n", phy ? "phy" : "eeprom");
138 dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
139 ret = dm_read(dev, DM_SHARED_DATA, 2, value);
141 netdev_dbg(dev->net, "read shared %d 0x%02x returned 0x%04x, %d\n",
142 phy, reg, *value, ret);
145 mutex_unlock(&dev->phy_mutex);
149 static int dm_write_shared_word(struct usbnet *dev, int phy, u8 reg, __le16 value)
153 mutex_lock(&dev->phy_mutex);
155 ret = dm_write(dev, DM_SHARED_DATA, 2, &value);
159 dm_write_reg(dev, DM_SHARED_ADDR, phy ? (reg | 0x40) : reg);
160 dm_write_reg(dev, DM_SHARED_CTRL, phy ? 0x1a : 0x12);
162 for (i = 0; i < DM_TIMEOUT; i++) {
166 ret = dm_read_reg(dev, DM_SHARED_CTRL, &tmp);
175 if (i == DM_TIMEOUT) {
176 netdev_err(dev->net, "%s write timed out!\n", phy ? "phy" : "eeprom");
181 dm_write_reg(dev, DM_SHARED_CTRL, 0x0);
184 mutex_unlock(&dev->phy_mutex);
188 static int dm_read_eeprom_word(struct usbnet *dev, u8 offset, void *value)
190 return dm_read_shared_word(dev, 0, offset, value);
195 static int dm9601_get_eeprom_len(struct net_device *dev)
197 return DM_EEPROM_LEN;
200 static int dm9601_get_eeprom(struct net_device *net,
201 struct ethtool_eeprom *eeprom, u8 * data)
203 struct usbnet *dev = netdev_priv(net);
204 __le16 *ebuf = (__le16 *) data;
207 /* access is 16bit */
208 if ((eeprom->offset % 2) || (eeprom->len % 2))
211 for (i = 0; i < eeprom->len / 2; i++) {
212 if (dm_read_eeprom_word(dev, eeprom->offset / 2 + i,
219 static int dm9601_mdio_read(struct net_device *netdev, int phy_id, int loc)
221 struct usbnet *dev = netdev_priv(netdev);
226 netdev_dbg(dev->net, "Only internal phy supported\n");
230 dm_read_shared_word(dev, 1, loc, &res);
233 "dm9601_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
234 phy_id, loc, le16_to_cpu(res));
236 return le16_to_cpu(res);
239 static void dm9601_mdio_write(struct net_device *netdev, int phy_id, int loc,
242 struct usbnet *dev = netdev_priv(netdev);
243 __le16 res = cpu_to_le16(val);
246 netdev_dbg(dev->net, "Only internal phy supported\n");
250 netdev_dbg(dev->net, "dm9601_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
253 dm_write_shared_word(dev, 1, loc, res);
256 static void dm9601_get_drvinfo(struct net_device *net,
257 struct ethtool_drvinfo *info)
259 /* Inherit standard device info */
260 usbnet_get_drvinfo(net, info);
263 static u32 dm9601_get_link(struct net_device *net)
265 struct usbnet *dev = netdev_priv(net);
267 return mii_link_ok(&dev->mii);
270 static int dm9601_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
272 struct usbnet *dev = netdev_priv(net);
274 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
277 static const struct ethtool_ops dm9601_ethtool_ops = {
278 .get_drvinfo = dm9601_get_drvinfo,
279 .get_link = dm9601_get_link,
280 .get_msglevel = usbnet_get_msglevel,
281 .set_msglevel = usbnet_set_msglevel,
282 .get_eeprom_len = dm9601_get_eeprom_len,
283 .get_eeprom = dm9601_get_eeprom,
284 .get_settings = usbnet_get_settings,
285 .set_settings = usbnet_set_settings,
286 .nway_reset = usbnet_nway_reset,
289 static void dm9601_set_multicast(struct net_device *net)
291 struct usbnet *dev = netdev_priv(net);
292 /* We use the 20 byte dev->data for our 8 byte filter buffer
293 * to avoid allocating memory that is tricky to free later */
294 u8 *hashes = (u8 *) & dev->data;
297 memset(hashes, 0x00, DM_MCAST_SIZE);
298 hashes[DM_MCAST_SIZE - 1] |= 0x80; /* broadcast address */
300 if (net->flags & IFF_PROMISC) {
302 } else if (net->flags & IFF_ALLMULTI ||
303 netdev_mc_count(net) > DM_MAX_MCAST) {
305 } else if (!netdev_mc_empty(net)) {
306 struct netdev_hw_addr *ha;
308 netdev_for_each_mc_addr(ha, net) {
309 u32 crc = ether_crc(ETH_ALEN, ha->addr) >> 26;
310 hashes[crc >> 3] |= 1 << (crc & 0x7);
314 dm_write_async(dev, DM_MCAST_ADDR, DM_MCAST_SIZE, hashes);
315 dm_write_reg_async(dev, DM_RX_CTRL, rx_ctl);
318 static void __dm9601_set_mac_address(struct usbnet *dev)
320 dm_write_async(dev, DM_PHY_ADDR, ETH_ALEN, dev->net->dev_addr);
323 static int dm9601_set_mac_address(struct net_device *net, void *p)
325 struct sockaddr *addr = p;
326 struct usbnet *dev = netdev_priv(net);
328 if (!is_valid_ether_addr(addr->sa_data)) {
329 dev_err(&net->dev, "not setting invalid mac address %pM\n",
334 memcpy(net->dev_addr, addr->sa_data, net->addr_len);
335 __dm9601_set_mac_address(dev);
340 static const struct net_device_ops dm9601_netdev_ops = {
341 .ndo_open = usbnet_open,
342 .ndo_stop = usbnet_stop,
343 .ndo_start_xmit = usbnet_start_xmit,
344 .ndo_tx_timeout = usbnet_tx_timeout,
345 .ndo_change_mtu = usbnet_change_mtu,
346 .ndo_validate_addr = eth_validate_addr,
347 .ndo_do_ioctl = dm9601_ioctl,
348 .ndo_set_rx_mode = dm9601_set_multicast,
349 .ndo_set_mac_address = dm9601_set_mac_address,
352 static int dm9601_bind(struct usbnet *dev, struct usb_interface *intf)
355 u8 mac[ETH_ALEN], id;
357 ret = usbnet_get_endpoints(dev, intf);
361 dev->net->netdev_ops = &dm9601_netdev_ops;
362 dev->net->ethtool_ops = &dm9601_ethtool_ops;
363 dev->net->hard_header_len += DM_TX_OVERHEAD;
364 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
366 /* dm9620/21a require room for 4 byte padding, even in dm9601
367 * mode, so we need +1 to be able to receive full size
370 dev->rx_urb_size = dev->net->mtu + ETH_HLEN + DM_RX_OVERHEAD + 1;
372 dev->mii.dev = dev->net;
373 dev->mii.mdio_read = dm9601_mdio_read;
374 dev->mii.mdio_write = dm9601_mdio_write;
375 dev->mii.phy_id_mask = 0x1f;
376 dev->mii.reg_num_mask = 0x1f;
379 dm_write_reg(dev, DM_NET_CTRL, 1);
383 if (dm_read(dev, DM_PHY_ADDR, ETH_ALEN, mac) < 0) {
384 printk(KERN_ERR "Error reading MAC address\n");
390 * Overwrite the auto-generated address only with good ones.
392 if (is_valid_ether_addr(mac))
393 memcpy(dev->net->dev_addr, mac, ETH_ALEN);
396 "dm9601: No valid MAC address in EEPROM, using %pM\n",
398 __dm9601_set_mac_address(dev);
401 if (dm_read_reg(dev, DM_CHIP_ID, &id) < 0) {
402 netdev_err(dev->net, "Error reading chip ID\n");
407 /* put dm9620 devices in dm9601 mode */
408 if (id == ID_DM9620) {
411 if (dm_read_reg(dev, DM_MODE_CTRL, &mode) < 0) {
412 netdev_err(dev->net, "Error reading MODE_CTRL\n");
416 dm_write_reg(dev, DM_MODE_CTRL, mode & 0x7f);
420 dm_write_reg(dev, DM_GPR_CTRL, 1);
421 dm_write_reg(dev, DM_GPR_DATA, 0);
423 /* receive broadcast packets */
424 dm9601_set_multicast(dev->net);
426 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
427 dm9601_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
428 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
429 mii_nway_restart(&dev->mii);
435 static int dm9601_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
442 b2: packet length (incl crc) low
443 b3: packet length (incl crc) high
445 bn-3..bn: ethernet crc
448 if (unlikely(skb->len < DM_RX_OVERHEAD)) {
449 dev_err(&dev->udev->dev, "unexpected tiny rx frame\n");
453 status = skb->data[0];
454 len = (skb->data[1] | (skb->data[2] << 8)) - 4;
456 if (unlikely(status & 0xbf)) {
457 if (status & 0x01) dev->net->stats.rx_fifo_errors++;
458 if (status & 0x02) dev->net->stats.rx_crc_errors++;
459 if (status & 0x04) dev->net->stats.rx_frame_errors++;
460 if (status & 0x20) dev->net->stats.rx_missed_errors++;
461 if (status & 0x90) dev->net->stats.rx_length_errors++;
471 static struct sk_buff *dm9601_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
477 b1: packet length low
478 b2: packet length high
482 len = skb->len + DM_TX_OVERHEAD;
484 /* workaround for dm962x errata with tx fifo getting out of
485 * sync if a USB bulk transfer retry happens right after a
486 * packet with odd / maxpacket length by adding up to 3 bytes
489 while ((len & 1) || !(len % dev->maxpacket))
492 len -= DM_TX_OVERHEAD; /* hw header doesn't count as part of length */
493 pad = len - skb->len;
495 if (skb_headroom(skb) < DM_TX_OVERHEAD || skb_tailroom(skb) < pad) {
496 struct sk_buff *skb2;
498 skb2 = skb_copy_expand(skb, DM_TX_OVERHEAD, pad, flags);
499 dev_kfree_skb_any(skb);
505 __skb_push(skb, DM_TX_OVERHEAD);
508 memset(skb->data + skb->len, 0, pad);
513 skb->data[1] = len >> 8;
518 static void dm9601_status(struct usbnet *dev, struct urb *urb)
534 if (urb->actual_length < 8)
537 buf = urb->transfer_buffer;
539 link = !!(buf[0] & 0x40);
540 if (netif_carrier_ok(dev->net) != link) {
541 usbnet_link_change(dev, link, 1);
542 netdev_dbg(dev->net, "Link Status is: %d\n", link);
546 static int dm9601_link_reset(struct usbnet *dev)
548 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
550 mii_check_media(&dev->mii, 1, 1);
551 mii_ethtool_gset(&dev->mii, &ecmd);
553 netdev_dbg(dev->net, "link_reset() speed: %u duplex: %d\n",
554 ethtool_cmd_speed(&ecmd), ecmd.duplex);
559 static const struct driver_info dm9601_info = {
560 .description = "Davicom DM96xx USB 10/100 Ethernet",
561 .flags = FLAG_ETHER | FLAG_LINK_INTR,
563 .rx_fixup = dm9601_rx_fixup,
564 .tx_fixup = dm9601_tx_fixup,
565 .status = dm9601_status,
566 .link_reset = dm9601_link_reset,
567 .reset = dm9601_link_reset,
570 static const struct usb_device_id products[] = {
572 USB_DEVICE(0x07aa, 0x9601), /* Corega FEther USB-TXC */
573 .driver_info = (unsigned long)&dm9601_info,
576 USB_DEVICE(0x0a46, 0x9601), /* Davicom USB-100 */
577 .driver_info = (unsigned long)&dm9601_info,
580 USB_DEVICE(0x0a46, 0x6688), /* ZT6688 USB NIC */
581 .driver_info = (unsigned long)&dm9601_info,
584 USB_DEVICE(0x0a46, 0x0268), /* ShanTou ST268 USB NIC */
585 .driver_info = (unsigned long)&dm9601_info,
588 USB_DEVICE(0x0a46, 0x8515), /* ADMtek ADM8515 USB NIC */
589 .driver_info = (unsigned long)&dm9601_info,
592 USB_DEVICE(0x0a47, 0x9601), /* Hirose USB-100 */
593 .driver_info = (unsigned long)&dm9601_info,
596 USB_DEVICE(0x0fe6, 0x8101), /* DM9601 USB to Fast Ethernet Adapter */
597 .driver_info = (unsigned long)&dm9601_info,
600 USB_DEVICE(0x0fe6, 0x9700), /* DM9601 USB to Fast Ethernet Adapter */
601 .driver_info = (unsigned long)&dm9601_info,
604 USB_DEVICE(0x0a46, 0x9000), /* DM9000E */
605 .driver_info = (unsigned long)&dm9601_info,
608 USB_DEVICE(0x0a46, 0x9620), /* DM9620 USB to Fast Ethernet Adapter */
609 .driver_info = (unsigned long)&dm9601_info,
612 USB_DEVICE(0x0a46, 0x9621), /* DM9621A USB to Fast Ethernet Adapter */
613 .driver_info = (unsigned long)&dm9601_info,
616 USB_DEVICE(0x0a46, 0x9622), /* DM9622 USB to Fast Ethernet Adapter */
617 .driver_info = (unsigned long)&dm9601_info,
620 USB_DEVICE(0x0a46, 0x0269), /* DM962OA USB to Fast Ethernet Adapter */
621 .driver_info = (unsigned long)&dm9601_info,
624 USB_DEVICE(0x0a46, 0x1269), /* DM9621A USB to Fast Ethernet Adapter */
625 .driver_info = (unsigned long)&dm9601_info,
630 MODULE_DEVICE_TABLE(usb, products);
632 static struct usb_driver dm9601_driver = {
634 .id_table = products,
635 .probe = usbnet_probe,
636 .disconnect = usbnet_disconnect,
637 .suspend = usbnet_suspend,
638 .resume = usbnet_resume,
639 .disable_hub_initiated_lpm = 1,
642 module_usb_driver(dm9601_driver);
644 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
645 MODULE_DESCRIPTION("Davicom DM96xx USB 10/100 ethernet devices");
646 MODULE_LICENSE("GPL");