13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/stddef.h>
17 #include <linux/netdevice.h>
19 #include <linux/ethtool.h>
20 #include <linux/mii.h>
24 #include <linux/slab.h>
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
44 #define DM_PHY_ADDR 0x10
45 #define DM_MCAST_ADDR 0x16
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
53 #define DM_RX_OVERHEAD 7
54 #define DM_TIMEOUT 1000
62 netdev_dbg(dev->
net,
"dm_read() reg=0x%02x length=%d\n", reg, length);
69 usb_rcvctrlpipe(dev->
udev, 0),
72 0, reg, buf, length, USB_CTRL_SET_TIMEOUT);
85 return dm_read(dev, reg, 1, value);
93 netdev_dbg(dev->
net,
"dm_write() reg=0x%02x, length=%d\n", reg, length);
102 usb_sndctrlpipe(dev->
udev, 0),
105 0, reg, buf, length, USB_CTRL_SET_TIMEOUT);
107 if (err >= 0 && err < length)
115 netdev_dbg(dev->
net,
"dm_write_reg() reg=0x%02x, value=0x%02x\n",
118 usb_sndctrlpipe(dev->
udev, 0),
121 value, reg,
NULL, 0, USB_CTRL_SET_TIMEOUT);
124 static void dm_write_async_callback(
struct urb *
urb)
137 static void dm_write_async_helper(
struct usbnet *dev,
u8 reg,
u8 value,
138 u16 length,
void *data)
146 netdev_err(dev->
net,
"Error allocating URB in dm_write_async_helper!\n");
152 netdev_err(dev->
net,
"Failed to allocate memory for control request\n");
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);
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)
214 if (i == DM_TIMEOUT) {
215 netdev_err(dev->
net,
"%s read timed out!\n", phy ?
"phy" :
"eeprom");
223 netdev_dbg(dev->
net,
"read shared %d 0x%02x returned 0x%04x, %d\n",
224 phy, reg, *value, ret);
231 static int dm_write_shared_word(
struct usbnet *dev,
int phy,
u8 reg,
__le16 value)
257 if (i == DM_TIMEOUT) {
258 netdev_err(dev->
net,
"%s write timed out!\n", phy ?
"phy" :
"eeprom");
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)
285 struct usbnet *dev = netdev_priv(net);
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,
303 struct usbnet *dev = netdev_priv(netdev);
312 dm_read_shared_word(dev, 1, loc, &res);
315 "dm9601_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
321 static void dm9601_mdio_write(
struct net_device *netdev,
int phy_id,
int loc,
324 struct usbnet *dev = netdev_priv(netdev);
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,
348 struct usbnet *dev = netdev_priv(net);
355 struct usbnet *dev = netdev_priv(net);
360 static const struct ethtool_ops dm9601_ethtool_ops = {
361 .get_drvinfo = dm9601_get_drvinfo,
362 .get_link = dm9601_get_link,
365 .get_eeprom_len = dm9601_get_eeprom_len,
366 .get_eeprom = dm9601_get_eeprom,
372 static void dm9601_set_multicast(
struct net_device *net)
374 struct usbnet *dev = netdev_priv(net);
393 hashes[crc >> 3] |= 1 << (crc & 0x7);
401 static void __dm9601_set_mac_address(
struct usbnet *dev)
406 static int dm9601_set_mac_address(
struct net_device *net,
void *
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",
418 __dm9601_set_mac_address(dev);
430 .ndo_do_ioctl = dm9601_ioctl,
431 .ndo_set_rx_mode = dm9601_set_multicast,
432 .ndo_set_mac_address = dm9601_set_mac_address,
444 dev->
net->netdev_ops = &dm9601_netdev_ops;
445 dev->
net->ethtool_ops = &dm9601_ethtool_ops;
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;
470 if (is_valid_ether_addr(mac))
474 "dm9601: No valid MAC address in EEPROM, using %pM\n",
476 __dm9601_set_mac_address(dev);
484 dm9601_set_multicast(dev->
net);
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;
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++;
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) {
600 static int dm9601_link_reset(
struct usbnet *dev)
608 ethtool_cmd_speed(&ecmd), ecmd.
duplex);
614 .description =
"Davicom DM9601 USB Ethernet",
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,
626 USB_DEVICE(0x07aa, 0x9601),
627 .driver_info = (
unsigned long)&dm9601_info,
630 USB_DEVICE(0x0a46, 0x9601),
631 .driver_info = (
unsigned long)&dm9601_info,
634 USB_DEVICE(0x0a46, 0x6688),
635 .driver_info = (
unsigned long)&dm9601_info,
638 USB_DEVICE(0x0a46, 0x0268),
639 .driver_info = (
unsigned long)&dm9601_info,
642 USB_DEVICE(0x0a46, 0x8515),
643 .driver_info = (
unsigned long)&dm9601_info,
646 USB_DEVICE(0x0a47, 0x9601),
647 .driver_info = (
unsigned long)&dm9601_info,
650 USB_DEVICE(0x0fe6, 0x8101),
651 .driver_info = (
unsigned long)&dm9601_info,
654 USB_DEVICE(0x0fe6, 0x9700),
655 .driver_info = (
unsigned long)&dm9601_info,
658 USB_DEVICE(0x0a46, 0x9000),
659 .driver_info = (
unsigned long)&dm9601_info,
666 static struct usb_driver dm9601_driver = {
668 .id_table = products,
673 .disable_hub_initiated_lpm = 1,