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
49 #define DM_MAX_MCAST 64
50 #define DM_MCAST_SIZE 8
51 #define DM_EEPROM_LEN 256
52 #define DM_TX_OVERHEAD 2 /* 2 byte header */
53 #define DM_RX_OVERHEAD 7 /* 3 byte header + 4 byte crc tail */
54 #define DM_TIMEOUT 1000
57 static int dm_read(struct usbnet
*dev
, u8 reg
, u16 length
, void *data
)
62 netdev_dbg(dev
->net
, "dm_read() reg=0x%02x length=%d\n", reg
, length
);
64 buf
= kmalloc(length
, GFP_KERNEL
);
68 err
= usb_control_msg(dev
->udev
,
69 usb_rcvctrlpipe(dev
->udev
, 0),
71 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
72 0, reg
, buf
, length
, USB_CTRL_SET_TIMEOUT
);
74 memcpy(data
, buf
, length
);
83 static int dm_read_reg(struct usbnet
*dev
, u8 reg
, u8
*value
)
85 return dm_read(dev
, reg
, 1, value
);
88 static int dm_write(struct usbnet
*dev
, u8 reg
, u16 length
, void *data
)
93 netdev_dbg(dev
->net
, "dm_write() reg=0x%02x, length=%d\n", reg
, length
);
96 buf
= kmemdup(data
, length
, GFP_KERNEL
);
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 netdev_dbg(dev
->net
, "dm_write_reg() reg=0x%02x, value=0x%02x\n",
117 return usb_control_msg(dev
->udev
,
118 usb_sndctrlpipe(dev
->udev
, 0),
120 USB_DIR_OUT
| USB_TYPE_VENDOR
|USB_RECIP_DEVICE
,
121 value
, reg
, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
124 static void dm_write_async_callback(struct urb
*urb
)
126 struct usb_ctrlrequest
*req
= (struct usb_ctrlrequest
*)urb
->context
;
127 int status
= urb
->status
;
130 printk(KERN_DEBUG
"dm_write_async_callback() failed with %d\n",
137 static void dm_write_async_helper(struct usbnet
*dev
, u8 reg
, u8 value
,
138 u16 length
, void *data
)
140 struct usb_ctrlrequest
*req
;
144 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
146 netdev_err(dev
->net
, "Error allocating URB in dm_write_async_helper!\n");
150 req
= kmalloc(sizeof(struct usb_ctrlrequest
), GFP_ATOMIC
);
152 netdev_err(dev
->net
, "Failed to allocate memory for control request\n");
157 req
->bRequestType
= USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
;
158 req
->bRequest
= length
? DM_WRITE_REGS
: DM_WRITE_REG
;
159 req
->wValue
= cpu_to_le16(value
);
160 req
->wIndex
= cpu_to_le16(reg
);
161 req
->wLength
= cpu_to_le16(length
);
163 usb_fill_control_urb(urb
, dev
->udev
,
164 usb_sndctrlpipe(dev
->udev
, 0),
165 (void *)req
, data
, length
,
166 dm_write_async_callback
, req
);
168 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
170 netdev_err(dev
->net
, "Error submitting the control message: status=%d\n",
177 static void dm_write_async(struct usbnet
*dev
, u8 reg
, u16 length
, void *data
)
179 netdev_dbg(dev
->net
, "dm_write_async() reg=0x%02x length=%d\n", reg
, length
);
181 dm_write_async_helper(dev
, reg
, 0, length
, data
);
184 static void dm_write_reg_async(struct usbnet
*dev
, u8 reg
, u8 value
)
186 netdev_dbg(dev
->net
, "dm_write_reg_async() reg=0x%02x value=0x%02x\n",
189 dm_write_async_helper(dev
, reg
, value
, 0, NULL
);
192 static int dm_read_shared_word(struct usbnet
*dev
, int phy
, u8 reg
, __le16
*value
)
196 mutex_lock(&dev
->phy_mutex
);
198 dm_write_reg(dev
, DM_SHARED_ADDR
, phy
? (reg
| 0x40) : reg
);
199 dm_write_reg(dev
, DM_SHARED_CTRL
, phy
? 0xc : 0x4);
201 for (i
= 0; i
< DM_TIMEOUT
; i
++) {
205 ret
= dm_read_reg(dev
, DM_SHARED_CTRL
, &tmp
);
214 if (i
== DM_TIMEOUT
) {
215 netdev_err(dev
->net
, "%s read timed out!\n", phy
? "phy" : "eeprom");
220 dm_write_reg(dev
, DM_SHARED_CTRL
, 0x0);
221 ret
= dm_read(dev
, DM_SHARED_DATA
, 2, value
);
223 netdev_dbg(dev
->net
, "read shared %d 0x%02x returned 0x%04x, %d\n",
224 phy
, reg
, *value
, ret
);
227 mutex_unlock(&dev
->phy_mutex
);
231 static int dm_write_shared_word(struct usbnet
*dev
, int phy
, u8 reg
, __le16 value
)
235 mutex_lock(&dev
->phy_mutex
);
237 ret
= dm_write(dev
, DM_SHARED_DATA
, 2, &value
);
241 dm_write_reg(dev
, DM_SHARED_ADDR
, phy
? (reg
| 0x40) : reg
);
242 dm_write_reg(dev
, DM_SHARED_CTRL
, phy
? 0x1a : 0x12);
244 for (i
= 0; i
< DM_TIMEOUT
; i
++) {
248 ret
= dm_read_reg(dev
, DM_SHARED_CTRL
, &tmp
);
257 if (i
== DM_TIMEOUT
) {
258 netdev_err(dev
->net
, "%s write timed out!\n", phy
? "phy" : "eeprom");
263 dm_write_reg(dev
, DM_SHARED_CTRL
, 0x0);
266 mutex_unlock(&dev
->phy_mutex
);
270 static int dm_read_eeprom_word(struct usbnet
*dev
, u8 offset
, void *value
)
272 return dm_read_shared_word(dev
, 0, offset
, value
);
277 static int dm9601_get_eeprom_len(struct net_device
*dev
)
279 return DM_EEPROM_LEN
;
282 static int dm9601_get_eeprom(struct net_device
*net
,
283 struct ethtool_eeprom
*eeprom
, u8
* data
)
285 struct usbnet
*dev
= netdev_priv(net
);
286 __le16
*ebuf
= (__le16
*) data
;
289 /* access is 16bit */
290 if ((eeprom
->offset
% 2) || (eeprom
->len
% 2))
293 for (i
= 0; i
< eeprom
->len
/ 2; i
++) {
294 if (dm_read_eeprom_word(dev
, eeprom
->offset
/ 2 + i
,
301 static int dm9601_mdio_read(struct net_device
*netdev
, int phy_id
, int loc
)
303 struct usbnet
*dev
= netdev_priv(netdev
);
308 netdev_dbg(dev
->net
, "Only internal phy supported\n");
312 dm_read_shared_word(dev
, 1, loc
, &res
);
315 "dm9601_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
316 phy_id
, loc
, le16_to_cpu(res
));
318 return le16_to_cpu(res
);
321 static void dm9601_mdio_write(struct net_device
*netdev
, int phy_id
, int loc
,
324 struct usbnet
*dev
= netdev_priv(netdev
);
325 __le16 res
= cpu_to_le16(val
);
328 netdev_dbg(dev
->net
, "Only internal phy supported\n");
332 netdev_dbg(dev
->net
, "dm9601_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
335 dm_write_shared_word(dev
, 1, loc
, res
);
338 static void dm9601_get_drvinfo(struct net_device
*net
,
339 struct ethtool_drvinfo
*info
)
341 /* Inherit standard device info */
342 usbnet_get_drvinfo(net
, info
);
343 info
->eedump_len
= DM_EEPROM_LEN
;
346 static u32
dm9601_get_link(struct net_device
*net
)
348 struct usbnet
*dev
= netdev_priv(net
);
350 return mii_link_ok(&dev
->mii
);
353 static int dm9601_ioctl(struct net_device
*net
, struct ifreq
*rq
, int cmd
)
355 struct usbnet
*dev
= netdev_priv(net
);
357 return generic_mii_ioctl(&dev
->mii
, if_mii(rq
), cmd
, NULL
);
360 static const struct ethtool_ops dm9601_ethtool_ops
= {
361 .get_drvinfo
= dm9601_get_drvinfo
,
362 .get_link
= dm9601_get_link
,
363 .get_msglevel
= usbnet_get_msglevel
,
364 .set_msglevel
= usbnet_set_msglevel
,
365 .get_eeprom_len
= dm9601_get_eeprom_len
,
366 .get_eeprom
= dm9601_get_eeprom
,
367 .get_settings
= usbnet_get_settings
,
368 .set_settings
= usbnet_set_settings
,
369 .nway_reset
= usbnet_nway_reset
,
372 static void dm9601_set_multicast(struct net_device
*net
)
374 struct usbnet
*dev
= netdev_priv(net
);
375 /* We use the 20 byte dev->data for our 8 byte filter buffer
376 * to avoid allocating memory that is tricky to free later */
377 u8
*hashes
= (u8
*) & dev
->data
;
380 memset(hashes
, 0x00, DM_MCAST_SIZE
);
381 hashes
[DM_MCAST_SIZE
- 1] |= 0x80; /* broadcast address */
383 if (net
->flags
& IFF_PROMISC
) {
385 } else if (net
->flags
& IFF_ALLMULTI
||
386 netdev_mc_count(net
) > DM_MAX_MCAST
) {
388 } else if (!netdev_mc_empty(net
)) {
389 struct netdev_hw_addr
*ha
;
391 netdev_for_each_mc_addr(ha
, net
) {
392 u32 crc
= ether_crc(ETH_ALEN
, ha
->addr
) >> 26;
393 hashes
[crc
>> 3] |= 1 << (crc
& 0x7);
397 dm_write_async(dev
, DM_MCAST_ADDR
, DM_MCAST_SIZE
, hashes
);
398 dm_write_reg_async(dev
, DM_RX_CTRL
, rx_ctl
);
401 static void __dm9601_set_mac_address(struct usbnet
*dev
)
403 dm_write_async(dev
, DM_PHY_ADDR
, ETH_ALEN
, dev
->net
->dev_addr
);
406 static int dm9601_set_mac_address(struct net_device
*net
, void *p
)
408 struct sockaddr
*addr
= p
;
409 struct usbnet
*dev
= netdev_priv(net
);
411 if (!is_valid_ether_addr(addr
->sa_data
)) {
412 dev_err(&net
->dev
, "not setting invalid mac address %pM\n",
417 memcpy(net
->dev_addr
, addr
->sa_data
, net
->addr_len
);
418 __dm9601_set_mac_address(dev
);
423 static const struct net_device_ops dm9601_netdev_ops
= {
424 .ndo_open
= usbnet_open
,
425 .ndo_stop
= usbnet_stop
,
426 .ndo_start_xmit
= usbnet_start_xmit
,
427 .ndo_tx_timeout
= usbnet_tx_timeout
,
428 .ndo_change_mtu
= usbnet_change_mtu
,
429 .ndo_validate_addr
= eth_validate_addr
,
430 .ndo_do_ioctl
= dm9601_ioctl
,
431 .ndo_set_multicast_list
= dm9601_set_multicast
,
432 .ndo_set_mac_address
= dm9601_set_mac_address
,
435 static int dm9601_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
440 ret
= usbnet_get_endpoints(dev
, intf
);
444 dev
->net
->netdev_ops
= &dm9601_netdev_ops
;
445 dev
->net
->ethtool_ops
= &dm9601_ethtool_ops
;
446 dev
->net
->hard_header_len
+= DM_TX_OVERHEAD
;
447 dev
->hard_mtu
= dev
->net
->mtu
+ dev
->net
->hard_header_len
;
448 dev
->rx_urb_size
= dev
->net
->mtu
+ ETH_HLEN
+ DM_RX_OVERHEAD
;
450 dev
->mii
.dev
= dev
->net
;
451 dev
->mii
.mdio_read
= dm9601_mdio_read
;
452 dev
->mii
.mdio_write
= dm9601_mdio_write
;
453 dev
->mii
.phy_id_mask
= 0x1f;
454 dev
->mii
.reg_num_mask
= 0x1f;
457 dm_write_reg(dev
, DM_NET_CTRL
, 1);
461 if (dm_read(dev
, DM_PHY_ADDR
, ETH_ALEN
, mac
) < 0) {
462 printk(KERN_ERR
"Error reading MAC address\n");
468 * Overwrite the auto-generated address only with good ones.
470 if (is_valid_ether_addr(mac
))
471 memcpy(dev
->net
->dev_addr
, mac
, ETH_ALEN
);
474 "dm9601: No valid MAC address in EEPROM, using %pM\n",
476 __dm9601_set_mac_address(dev
);
480 dm_write_reg(dev
, DM_GPR_CTRL
, 1);
481 dm_write_reg(dev
, DM_GPR_DATA
, 0);
483 /* receive broadcast packets */
484 dm9601_set_multicast(dev
->net
);
486 dm9601_mdio_write(dev
->net
, dev
->mii
.phy_id
, MII_BMCR
, BMCR_RESET
);
487 dm9601_mdio_write(dev
->net
, dev
->mii
.phy_id
, MII_ADVERTISE
,
488 ADVERTISE_ALL
| ADVERTISE_CSMA
| ADVERTISE_PAUSE_CAP
);
489 mii_nway_restart(&dev
->mii
);
495 static int dm9601_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
502 b2: packet length (incl crc) low
503 b3: packet length (incl crc) high
505 bn-3..bn: ethernet crc
508 if (unlikely(skb
->len
< DM_RX_OVERHEAD
)) {
509 dev_err(&dev
->udev
->dev
, "unexpected tiny rx frame\n");
513 status
= skb
->data
[0];
514 len
= (skb
->data
[1] | (skb
->data
[2] << 8)) - 4;
516 if (unlikely(status
& 0xbf)) {
517 if (status
& 0x01) dev
->net
->stats
.rx_fifo_errors
++;
518 if (status
& 0x02) dev
->net
->stats
.rx_crc_errors
++;
519 if (status
& 0x04) dev
->net
->stats
.rx_frame_errors
++;
520 if (status
& 0x20) dev
->net
->stats
.rx_missed_errors
++;
521 if (status
& 0x90) dev
->net
->stats
.rx_length_errors
++;
531 static struct sk_buff
*dm9601_tx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
,
537 b1: packet length low
538 b2: packet length high
544 if (skb_headroom(skb
) < DM_TX_OVERHEAD
) {
545 struct sk_buff
*skb2
;
547 skb2
= skb_copy_expand(skb
, DM_TX_OVERHEAD
, 0, flags
);
548 dev_kfree_skb_any(skb
);
554 __skb_push(skb
, DM_TX_OVERHEAD
);
556 /* usbnet adds padding if length is a multiple of packet size
557 if so, adjust length value in header */
558 if ((skb
->len
% dev
->maxpacket
) == 0)
562 skb
->data
[1] = len
>> 8;
567 static void dm9601_status(struct usbnet
*dev
, struct urb
*urb
)
583 if (urb
->actual_length
< 8)
586 buf
= urb
->transfer_buffer
;
588 link
= !!(buf
[0] & 0x40);
589 if (netif_carrier_ok(dev
->net
) != link
) {
591 netif_carrier_on(dev
->net
);
592 usbnet_defer_kevent (dev
, EVENT_LINK_RESET
);
595 netif_carrier_off(dev
->net
);
596 netdev_dbg(dev
->net
, "Link Status is: %d\n", link
);
600 static int dm9601_link_reset(struct usbnet
*dev
)
602 struct ethtool_cmd ecmd
;
604 mii_check_media(&dev
->mii
, 1, 1);
605 mii_ethtool_gset(&dev
->mii
, &ecmd
);
607 netdev_dbg(dev
->net
, "link_reset() speed: %d duplex: %d\n",
608 ecmd
.speed
, ecmd
.duplex
);
613 static const struct driver_info dm9601_info
= {
614 .description
= "Davicom DM9601 USB Ethernet",
615 .flags
= FLAG_ETHER
| FLAG_LINK_INTR
,
617 .rx_fixup
= dm9601_rx_fixup
,
618 .tx_fixup
= dm9601_tx_fixup
,
619 .status
= dm9601_status
,
620 .link_reset
= dm9601_link_reset
,
621 .reset
= dm9601_link_reset
,
624 static const struct usb_device_id products
[] = {
626 USB_DEVICE(0x07aa, 0x9601), /* Corega FEther USB-TXC */
627 .driver_info
= (unsigned long)&dm9601_info
,
630 USB_DEVICE(0x0a46, 0x9601), /* Davicom USB-100 */
631 .driver_info
= (unsigned long)&dm9601_info
,
634 USB_DEVICE(0x0a46, 0x6688), /* ZT6688 USB NIC */
635 .driver_info
= (unsigned long)&dm9601_info
,
638 USB_DEVICE(0x0a46, 0x0268), /* ShanTou ST268 USB NIC */
639 .driver_info
= (unsigned long)&dm9601_info
,
642 USB_DEVICE(0x0a46, 0x8515), /* ADMtek ADM8515 USB NIC */
643 .driver_info
= (unsigned long)&dm9601_info
,
646 USB_DEVICE(0x0a47, 0x9601), /* Hirose USB-100 */
647 .driver_info
= (unsigned long)&dm9601_info
,
650 USB_DEVICE(0x0fe6, 0x8101), /* DM9601 USB to Fast Ethernet Adapter */
651 .driver_info
= (unsigned long)&dm9601_info
,
654 USB_DEVICE(0x0a46, 0x9000), /* DM9000E */
655 .driver_info
= (unsigned long)&dm9601_info
,
660 MODULE_DEVICE_TABLE(usb
, products
);
662 static struct usb_driver dm9601_driver
= {
664 .id_table
= products
,
665 .probe
= usbnet_probe
,
666 .disconnect
= usbnet_disconnect
,
667 .suspend
= usbnet_suspend
,
668 .resume
= usbnet_resume
,
671 static int __init
dm9601_init(void)
673 return usb_register(&dm9601_driver
);
676 static void __exit
dm9601_exit(void)
678 usb_deregister(&dm9601_driver
);
681 module_init(dm9601_init
);
682 module_exit(dm9601_exit
);
684 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
685 MODULE_DESCRIPTION("Davicom DM9601 USB 1.1 ethernet devices");
686 MODULE_LICENSE("GPL");