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>
24 #include <linux/slab.h>
27 http://ptm2.cc.utu.fi/ftp/network/cards/DM9601/From_NET/DM9601-DS-P01-930914.pdf
30 /* control requests */
31 #define DM_READ_REGS 0x00
32 #define DM_WRITE_REGS 0x01
33 #define DM_READ_MEMS 0x02
34 #define DM_WRITE_REG 0x03
35 #define DM_WRITE_MEMS 0x05
36 #define DM_WRITE_MEM 0x07
39 #define DM_NET_CTRL 0x00
40 #define DM_RX_CTRL 0x05
41 #define DM_SHARED_CTRL 0x0b
42 #define DM_SHARED_ADDR 0x0c
43 #define DM_SHARED_DATA 0x0d /* low + high */
44 #define DM_PHY_ADDR 0x10 /* 6 bytes */
45 #define DM_MCAST_ADDR 0x16 /* 8 bytes */
46 #define DM_GPR_CTRL 0x1e
47 #define DM_GPR_DATA 0x1f
48 #define DM_CHIP_ID 0x2c
49 #define DM_MODE_CTRL 0x91 /* only on dm9620 */
55 #define DM_MAX_MCAST 64
56 #define DM_MCAST_SIZE 8
57 #define DM_EEPROM_LEN 256
58 #define DM_TX_OVERHEAD 2 /* 2 byte header */
59 #define DM_RX_OVERHEAD 7 /* 3 byte header + 4 byte crc tail */
60 #define DM_TIMEOUT 1000
62 static int dm_read(struct usbnet
*dev
, u8 reg
, u16 length
, void *data
)
65 err
= usbnet_read_cmd(dev
, DM_READ_REGS
,
66 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
67 0, reg
, data
, length
);
68 if(err
!= length
&& err
>= 0)
73 static int dm_read_reg(struct usbnet
*dev
, u8 reg
, u8
*value
)
75 return dm_read(dev
, reg
, 1, value
);
78 static int dm_write(struct usbnet
*dev
, u8 reg
, u16 length
, void *data
)
81 err
= usbnet_write_cmd(dev
, DM_WRITE_REGS
,
82 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
83 0, reg
, data
, length
);
85 if (err
>= 0 && err
< length
)
90 static int dm_write_reg(struct usbnet
*dev
, u8 reg
, u8 value
)
92 return usbnet_write_cmd(dev
, DM_WRITE_REG
,
93 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
97 static void dm_write_async(struct usbnet
*dev
, u8 reg
, u16 length
, void *data
)
99 usbnet_write_cmd_async(dev
, DM_WRITE_REGS
,
100 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
101 0, reg
, data
, length
);
104 static void dm_write_reg_async(struct usbnet
*dev
, u8 reg
, u8 value
)
106 usbnet_write_cmd_async(dev
, DM_WRITE_REG
,
107 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
108 value
, reg
, NULL
, 0);
111 static int dm_read_shared_word(struct usbnet
*dev
, int phy
, u8 reg
, __le16
*value
)
115 mutex_lock(&dev
->phy_mutex
);
117 dm_write_reg(dev
, DM_SHARED_ADDR
, phy
? (reg
| 0x40) : reg
);
118 dm_write_reg(dev
, DM_SHARED_CTRL
, phy
? 0xc : 0x4);
120 for (i
= 0; i
< DM_TIMEOUT
; i
++) {
124 ret
= dm_read_reg(dev
, DM_SHARED_CTRL
, &tmp
);
133 if (i
== DM_TIMEOUT
) {
134 netdev_err(dev
->net
, "%s read timed out!\n", phy
? "phy" : "eeprom");
139 dm_write_reg(dev
, DM_SHARED_CTRL
, 0x0);
140 ret
= dm_read(dev
, DM_SHARED_DATA
, 2, value
);
142 netdev_dbg(dev
->net
, "read shared %d 0x%02x returned 0x%04x, %d\n",
143 phy
, reg
, *value
, ret
);
146 mutex_unlock(&dev
->phy_mutex
);
150 static int dm_write_shared_word(struct usbnet
*dev
, int phy
, u8 reg
, __le16 value
)
154 mutex_lock(&dev
->phy_mutex
);
156 ret
= dm_write(dev
, DM_SHARED_DATA
, 2, &value
);
160 dm_write_reg(dev
, DM_SHARED_ADDR
, phy
? (reg
| 0x40) : reg
);
161 dm_write_reg(dev
, DM_SHARED_CTRL
, phy
? 0x1a : 0x12);
163 for (i
= 0; i
< DM_TIMEOUT
; i
++) {
167 ret
= dm_read_reg(dev
, DM_SHARED_CTRL
, &tmp
);
176 if (i
== DM_TIMEOUT
) {
177 netdev_err(dev
->net
, "%s write timed out!\n", phy
? "phy" : "eeprom");
182 dm_write_reg(dev
, DM_SHARED_CTRL
, 0x0);
185 mutex_unlock(&dev
->phy_mutex
);
189 static int dm_read_eeprom_word(struct usbnet
*dev
, u8 offset
, void *value
)
191 return dm_read_shared_word(dev
, 0, offset
, value
);
196 static int dm9601_get_eeprom_len(struct net_device
*dev
)
198 return DM_EEPROM_LEN
;
201 static int dm9601_get_eeprom(struct net_device
*net
,
202 struct ethtool_eeprom
*eeprom
, u8
* data
)
204 struct usbnet
*dev
= netdev_priv(net
);
205 __le16
*ebuf
= (__le16
*) data
;
208 /* access is 16bit */
209 if ((eeprom
->offset
% 2) || (eeprom
->len
% 2))
212 for (i
= 0; i
< eeprom
->len
/ 2; i
++) {
213 if (dm_read_eeprom_word(dev
, eeprom
->offset
/ 2 + i
,
220 static int dm9601_mdio_read(struct net_device
*netdev
, int phy_id
, int loc
)
222 struct usbnet
*dev
= netdev_priv(netdev
);
227 netdev_dbg(dev
->net
, "Only internal phy supported\n");
231 dm_read_shared_word(dev
, 1, loc
, &res
);
234 "dm9601_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
235 phy_id
, loc
, le16_to_cpu(res
));
237 return le16_to_cpu(res
);
240 static void dm9601_mdio_write(struct net_device
*netdev
, int phy_id
, int loc
,
243 struct usbnet
*dev
= netdev_priv(netdev
);
244 __le16 res
= cpu_to_le16(val
);
247 netdev_dbg(dev
->net
, "Only internal phy supported\n");
251 netdev_dbg(dev
->net
, "dm9601_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
254 dm_write_shared_word(dev
, 1, loc
, res
);
257 static void dm9601_get_drvinfo(struct net_device
*net
,
258 struct ethtool_drvinfo
*info
)
260 /* Inherit standard device info */
261 usbnet_get_drvinfo(net
, info
);
262 info
->eedump_len
= DM_EEPROM_LEN
;
265 static u32
dm9601_get_link(struct net_device
*net
)
267 struct usbnet
*dev
= netdev_priv(net
);
269 return mii_link_ok(&dev
->mii
);
272 static int dm9601_ioctl(struct net_device
*net
, struct ifreq
*rq
, int cmd
)
274 struct usbnet
*dev
= netdev_priv(net
);
276 return generic_mii_ioctl(&dev
->mii
, if_mii(rq
), cmd
, NULL
);
279 static const struct ethtool_ops dm9601_ethtool_ops
= {
280 .get_drvinfo
= dm9601_get_drvinfo
,
281 .get_link
= dm9601_get_link
,
282 .get_msglevel
= usbnet_get_msglevel
,
283 .set_msglevel
= usbnet_set_msglevel
,
284 .get_eeprom_len
= dm9601_get_eeprom_len
,
285 .get_eeprom
= dm9601_get_eeprom
,
286 .get_settings
= usbnet_get_settings
,
287 .set_settings
= usbnet_set_settings
,
288 .nway_reset
= usbnet_nway_reset
,
291 static void dm9601_set_multicast(struct net_device
*net
)
293 struct usbnet
*dev
= netdev_priv(net
);
294 /* We use the 20 byte dev->data for our 8 byte filter buffer
295 * to avoid allocating memory that is tricky to free later */
296 u8
*hashes
= (u8
*) & dev
->data
;
299 memset(hashes
, 0x00, DM_MCAST_SIZE
);
300 hashes
[DM_MCAST_SIZE
- 1] |= 0x80; /* broadcast address */
302 if (net
->flags
& IFF_PROMISC
) {
304 } else if (net
->flags
& IFF_ALLMULTI
||
305 netdev_mc_count(net
) > DM_MAX_MCAST
) {
307 } else if (!netdev_mc_empty(net
)) {
308 struct netdev_hw_addr
*ha
;
310 netdev_for_each_mc_addr(ha
, net
) {
311 u32 crc
= ether_crc(ETH_ALEN
, ha
->addr
) >> 26;
312 hashes
[crc
>> 3] |= 1 << (crc
& 0x7);
316 dm_write_async(dev
, DM_MCAST_ADDR
, DM_MCAST_SIZE
, hashes
);
317 dm_write_reg_async(dev
, DM_RX_CTRL
, rx_ctl
);
320 static void __dm9601_set_mac_address(struct usbnet
*dev
)
322 dm_write_async(dev
, DM_PHY_ADDR
, ETH_ALEN
, dev
->net
->dev_addr
);
325 static int dm9601_set_mac_address(struct net_device
*net
, void *p
)
327 struct sockaddr
*addr
= p
;
328 struct usbnet
*dev
= netdev_priv(net
);
330 if (!is_valid_ether_addr(addr
->sa_data
)) {
331 dev_err(&net
->dev
, "not setting invalid mac address %pM\n",
336 memcpy(net
->dev_addr
, addr
->sa_data
, net
->addr_len
);
337 __dm9601_set_mac_address(dev
);
342 static const struct net_device_ops dm9601_netdev_ops
= {
343 .ndo_open
= usbnet_open
,
344 .ndo_stop
= usbnet_stop
,
345 .ndo_start_xmit
= usbnet_start_xmit
,
346 .ndo_tx_timeout
= usbnet_tx_timeout
,
347 .ndo_change_mtu
= usbnet_change_mtu
,
348 .ndo_validate_addr
= eth_validate_addr
,
349 .ndo_do_ioctl
= dm9601_ioctl
,
350 .ndo_set_rx_mode
= dm9601_set_multicast
,
351 .ndo_set_mac_address
= dm9601_set_mac_address
,
354 static int dm9601_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
357 u8 mac
[ETH_ALEN
], id
;
359 ret
= usbnet_get_endpoints(dev
, intf
);
363 dev
->net
->netdev_ops
= &dm9601_netdev_ops
;
364 dev
->net
->ethtool_ops
= &dm9601_ethtool_ops
;
365 dev
->net
->hard_header_len
+= DM_TX_OVERHEAD
;
366 dev
->hard_mtu
= dev
->net
->mtu
+ dev
->net
->hard_header_len
;
367 dev
->rx_urb_size
= dev
->net
->mtu
+ ETH_HLEN
+ DM_RX_OVERHEAD
;
369 dev
->mii
.dev
= dev
->net
;
370 dev
->mii
.mdio_read
= dm9601_mdio_read
;
371 dev
->mii
.mdio_write
= dm9601_mdio_write
;
372 dev
->mii
.phy_id_mask
= 0x1f;
373 dev
->mii
.reg_num_mask
= 0x1f;
376 dm_write_reg(dev
, DM_NET_CTRL
, 1);
380 if (dm_read(dev
, DM_PHY_ADDR
, ETH_ALEN
, mac
) < 0) {
381 printk(KERN_ERR
"Error reading MAC address\n");
387 * Overwrite the auto-generated address only with good ones.
389 if (is_valid_ether_addr(mac
))
390 memcpy(dev
->net
->dev_addr
, mac
, ETH_ALEN
);
393 "dm9601: No valid MAC address in EEPROM, using %pM\n",
395 __dm9601_set_mac_address(dev
);
398 if (dm_read_reg(dev
, DM_CHIP_ID
, &id
) < 0) {
399 netdev_err(dev
->net
, "Error reading chip ID\n");
404 /* put dm9620 devices in dm9601 mode */
405 if (id
== ID_DM9620
) {
408 if (dm_read_reg(dev
, DM_MODE_CTRL
, &mode
) < 0) {
409 netdev_err(dev
->net
, "Error reading MODE_CTRL\n");
413 dm_write_reg(dev
, DM_MODE_CTRL
, mode
& 0x7f);
417 dm_write_reg(dev
, DM_GPR_CTRL
, 1);
418 dm_write_reg(dev
, DM_GPR_DATA
, 0);
420 /* receive broadcast packets */
421 dm9601_set_multicast(dev
->net
);
423 dm9601_mdio_write(dev
->net
, dev
->mii
.phy_id
, MII_BMCR
, BMCR_RESET
);
424 dm9601_mdio_write(dev
->net
, dev
->mii
.phy_id
, MII_ADVERTISE
,
425 ADVERTISE_ALL
| ADVERTISE_CSMA
| ADVERTISE_PAUSE_CAP
);
426 mii_nway_restart(&dev
->mii
);
432 static int dm9601_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
439 b2: packet length (incl crc) low
440 b3: packet length (incl crc) high
442 bn-3..bn: ethernet crc
445 if (unlikely(skb
->len
< DM_RX_OVERHEAD
)) {
446 dev_err(&dev
->udev
->dev
, "unexpected tiny rx frame\n");
450 status
= skb
->data
[0];
451 len
= (skb
->data
[1] | (skb
->data
[2] << 8)) - 4;
453 if (unlikely(status
& 0xbf)) {
454 if (status
& 0x01) dev
->net
->stats
.rx_fifo_errors
++;
455 if (status
& 0x02) dev
->net
->stats
.rx_crc_errors
++;
456 if (status
& 0x04) dev
->net
->stats
.rx_frame_errors
++;
457 if (status
& 0x20) dev
->net
->stats
.rx_missed_errors
++;
458 if (status
& 0x90) dev
->net
->stats
.rx_length_errors
++;
468 static struct sk_buff
*dm9601_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
,
474 b1: packet length low
475 b2: packet length high
481 if (skb_headroom(skb
) < DM_TX_OVERHEAD
) {
482 struct sk_buff
*skb2
;
484 skb2
= skb_copy_expand(skb
, DM_TX_OVERHEAD
, 0, flags
);
485 dev_kfree_skb_any(skb
);
491 __skb_push(skb
, DM_TX_OVERHEAD
);
493 /* usbnet adds padding if length is a multiple of packet size
494 if so, adjust length value in header */
495 if ((skb
->len
% dev
->maxpacket
) == 0)
499 skb
->data
[1] = len
>> 8;
504 static void dm9601_status(struct usbnet
*dev
, struct urb
*urb
)
520 if (urb
->actual_length
< 8)
523 buf
= urb
->transfer_buffer
;
525 link
= !!(buf
[0] & 0x40);
526 if (netif_carrier_ok(dev
->net
) != link
) {
527 usbnet_link_change(dev
, link
, 1);
528 netdev_dbg(dev
->net
, "Link Status is: %d\n", link
);
532 static int dm9601_link_reset(struct usbnet
*dev
)
534 struct ethtool_cmd ecmd
= { .cmd
= ETHTOOL_GSET
};
536 mii_check_media(&dev
->mii
, 1, 1);
537 mii_ethtool_gset(&dev
->mii
, &ecmd
);
539 netdev_dbg(dev
->net
, "link_reset() speed: %u duplex: %d\n",
540 ethtool_cmd_speed(&ecmd
), ecmd
.duplex
);
545 static const struct driver_info dm9601_info
= {
546 .description
= "Davicom DM9601 USB Ethernet",
547 .flags
= FLAG_ETHER
| FLAG_LINK_INTR
,
549 .rx_fixup
= dm9601_rx_fixup
,
550 .tx_fixup
= dm9601_tx_fixup
,
551 .status
= dm9601_status
,
552 .link_reset
= dm9601_link_reset
,
553 .reset
= dm9601_link_reset
,
556 static const struct usb_device_id products
[] = {
558 USB_DEVICE(0x07aa, 0x9601), /* Corega FEther USB-TXC */
559 .driver_info
= (unsigned long)&dm9601_info
,
562 USB_DEVICE(0x0a46, 0x9601), /* Davicom USB-100 */
563 .driver_info
= (unsigned long)&dm9601_info
,
566 USB_DEVICE(0x0a46, 0x6688), /* ZT6688 USB NIC */
567 .driver_info
= (unsigned long)&dm9601_info
,
570 USB_DEVICE(0x0a46, 0x0268), /* ShanTou ST268 USB NIC */
571 .driver_info
= (unsigned long)&dm9601_info
,
574 USB_DEVICE(0x0a46, 0x8515), /* ADMtek ADM8515 USB NIC */
575 .driver_info
= (unsigned long)&dm9601_info
,
578 USB_DEVICE(0x0a47, 0x9601), /* Hirose USB-100 */
579 .driver_info
= (unsigned long)&dm9601_info
,
582 USB_DEVICE(0x0fe6, 0x8101), /* DM9601 USB to Fast Ethernet Adapter */
583 .driver_info
= (unsigned long)&dm9601_info
,
586 USB_DEVICE(0x0fe6, 0x9700), /* DM9601 USB to Fast Ethernet Adapter */
587 .driver_info
= (unsigned long)&dm9601_info
,
590 USB_DEVICE(0x0a46, 0x9000), /* DM9000E */
591 .driver_info
= (unsigned long)&dm9601_info
,
594 USB_DEVICE(0x0a46, 0x9620), /* DM9620 USB to Fast Ethernet Adapter */
595 .driver_info
= (unsigned long)&dm9601_info
,
600 MODULE_DEVICE_TABLE(usb
, products
);
602 static struct usb_driver dm9601_driver
= {
604 .id_table
= products
,
605 .probe
= usbnet_probe
,
606 .disconnect
= usbnet_disconnect
,
607 .suspend
= usbnet_suspend
,
608 .resume
= usbnet_resume
,
609 .disable_hub_initiated_lpm
= 1,
612 module_usb_driver(dm9601_driver
);
614 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
615 MODULE_DESCRIPTION("Davicom DM9601 USB 1.1 ethernet devices");
616 MODULE_LICENSE("GPL");