21 #include <linux/module.h>
24 #include <linux/netdevice.h>
26 #include <linux/ethtool.h>
27 #include <linux/mii.h>
31 #include <linux/slab.h>
34 #define SMSC_CHIPNAME "smsc95xx"
35 #define SMSC_DRIVER_VERSION "1.0.4"
36 #define HS_USB_PKT_SIZE (512)
37 #define FS_USB_PKT_SIZE (64)
38 #define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
39 #define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
40 #define DEFAULT_BULK_IN_DELAY (0x00002000)
41 #define MAX_SINGLE_PACKET_SIZE (2048)
42 #define LAN95XX_EEPROM_MAGIC (0x9500)
43 #define EEPROM_MAC_OFFSET (0x01)
44 #define DEFAULT_TX_CSUM_ENABLE (true)
45 #define DEFAULT_RX_CSUM_ENABLE (true)
46 #define SMSC95XX_INTERNAL_PHY_ID (1)
47 #define SMSC95XX_TX_OVERHEAD (8)
48 #define SMSC95XX_TX_OVERHEAD_CSUM (12)
49 #define SUPPORTED_WAKE (WAKE_MAGIC)
51 #define check_warn(ret, fmt, args...) \
52 ({ if (ret < 0) netdev_warn(dev->net, fmt, ##args); })
54 #define check_warn_return(ret, fmt, args...) \
55 ({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); return ret; } })
57 #define check_warn_goto_done(ret, fmt, args...) \
58 ({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); goto done; } })
73 static bool turbo_mode =
true;
91 00, index, buf, 4, USB_CTRL_GET_TIMEOUT);
94 netdev_warn(dev->
net,
"Failed to read register index 0x%08x\n", index);
120 00, index, buf, 4, USB_CTRL_SET_TIMEOUT);
123 netdev_warn(dev->
net,
"Failed to write register index 0x%08x\n", index);
139 USB_CTRL_SET_TIMEOUT);
142 static int smsc95xx_clear_feature(
struct usbnet *dev,
u32 feature)
151 USB_CTRL_SET_TIMEOUT);
163 ret = smsc95xx_read_reg(dev,
MII_ADDR, &val);
174 struct usbnet *dev = netdev_priv(netdev);
181 ret = smsc95xx_phy_wait_not_busy(dev);
185 phy_id &= dev->
mii.phy_id_mask;
186 idx &= dev->
mii.reg_num_mask;
188 ret = smsc95xx_write_reg(dev,
MII_ADDR, addr);
191 ret = smsc95xx_phy_wait_not_busy(dev);
194 ret = smsc95xx_read_reg(dev,
MII_DATA, &val);
197 ret = (
u16)(val & 0xFFFF);
204 static void smsc95xx_mdio_write(
struct net_device *netdev,
int phy_id,
int idx,
207 struct usbnet *dev = netdev_priv(netdev);
214 ret = smsc95xx_phy_wait_not_busy(dev);
218 ret = smsc95xx_write_reg(dev,
MII_DATA, val);
222 phy_id &= dev->
mii.phy_id_mask;
223 idx &= dev->
mii.reg_num_mask;
225 ret = smsc95xx_write_reg(dev,
MII_ADDR, addr);
228 ret = smsc95xx_phy_wait_not_busy(dev);
237 unsigned long start_time =
jiffies;
242 ret = smsc95xx_read_reg(dev,
E2P_CMD, &val);
250 netdev_warn(dev->
net,
"EEPROM read operation timeout\n");
259 unsigned long start_time =
jiffies;
264 ret = smsc95xx_read_reg(dev,
E2P_CMD, &val);
267 if (!(val & E2P_CMD_BUSY_))
273 netdev_warn(dev->
net,
"EEPROM is busy\n");
286 ret = smsc95xx_eeprom_confirm_not_busy(dev);
290 for (i = 0; i <
length; i++) {
292 ret = smsc95xx_write_reg(dev,
E2P_CMD, val);
295 ret = smsc95xx_wait_eeprom(dev);
299 ret = smsc95xx_read_reg(dev,
E2P_DATA, &val);
302 data[
i] = val & 0xFF;
309 static int smsc95xx_write_eeprom(
struct usbnet *dev,
u32 offset,
u32 length,
318 ret = smsc95xx_eeprom_confirm_not_busy(dev);
324 ret = smsc95xx_write_reg(dev,
E2P_CMD, val);
327 ret = smsc95xx_wait_eeprom(dev);
331 for (i = 0; i <
length; i++) {
335 ret = smsc95xx_write_reg(dev,
E2P_DATA, val);
340 ret = smsc95xx_write_reg(dev,
E2P_CMD, val);
343 ret = smsc95xx_wait_eeprom(dev);
353 static void smsc95xx_async_cmd_callback(
struct urb *
urb)
359 check_warn(status,
"async callback failed with %d\n", status);
375 netdev_warn(dev->
net,
"Error allocating URB\n");
380 if (usb_context ==
NULL) {
381 netdev_warn(dev->
net,
"Error allocating control msg\n");
386 usb_context->
req.bRequestType =
389 usb_context->
req.wValue = 00;
393 usb_fill_control_urb(urb, dev->
udev, usb_sndctrlpipe(dev->
udev, 0),
394 (
void *)&usb_context->
req, data, size,
395 smsc95xx_async_cmd_callback,
396 (
void *)usb_context);
400 netdev_warn(dev->
net,
"Error submitting control msg, sts=%d\n",
412 static unsigned int smsc95xx_hash(
char addr[
ETH_ALEN])
414 return (
ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
417 static void smsc95xx_set_multicast(
struct net_device *netdev)
419 struct usbnet *dev = netdev_priv(netdev);
430 netif_dbg(dev, drv, dev->
net,
"promiscuous mode enabled\n");
434 netif_dbg(dev, drv, dev->
net,
"receive all multicast enabled\n");
444 u32 bitnum = smsc95xx_hash(ha->
addr);
445 u32 mask = 0x01 << (bitnum & 0x1F);
452 netif_dbg(dev, drv, dev->
net,
"HASHH=0x%08X, HASHL=0x%08X\n",
455 netif_dbg(dev, drv, dev->
net,
"receive own packets only\n");
460 spin_unlock_irqrestore(&pdata->
mac_cr_lock, flags);
463 ret = smsc95xx_write_reg_async(dev,
HASHH, &pdata->
hash_hi);
464 check_warn(ret,
"failed to initiate async write to HASHH");
466 ret = smsc95xx_write_reg_async(dev,
HASHL, &pdata->
hash_lo);
467 check_warn(ret,
"failed to initiate async write to HASHL");
469 ret = smsc95xx_write_reg_async(dev,
MAC_CR, &pdata->
mac_cr);
470 check_warn(ret,
"failed to initiate async write to MAC_CR");
473 static int smsc95xx_phy_update_flowcontrol(
struct usbnet *dev,
u8 duplex,
476 u32 flow, afc_cfg = 0;
478 int ret = smsc95xx_read_reg(dev,
AFC_CFG, &afc_cfg);
482 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
495 cap & FLOW_CTRL_RX ?
"enabled" :
"disabled",
496 cap & FLOW_CTRL_TX ?
"enabled" :
"disabled");
503 ret = smsc95xx_write_reg(dev,
FLOW, flow);
506 ret = smsc95xx_write_reg(dev,
AFC_CFG, afc_cfg);
512 static int smsc95xx_link_reset(
struct usbnet *dev)
534 "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
535 ethtool_cmd_speed(&ecmd), ecmd.
duplex, lcladv, rmtadv);
545 spin_unlock_irqrestore(&pdata->
mac_cr_lock, flags);
550 ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.
duplex, lcladv, rmtadv);
556 static void smsc95xx_status(
struct usbnet *dev,
struct urb *urb)
560 if (urb->actual_length != 4) {
561 netdev_warn(dev->
net,
"unexpected urb length %d\n",
566 memcpy(&intdata, urb->transfer_buffer, 4);
574 netdev_warn(dev->
net,
"unexpected interrupt, intdata=0x%08X\n",
579 static int smsc95xx_set_features(
struct net_device *netdev,
582 struct usbnet *dev = netdev_priv(netdev);
586 ret = smsc95xx_read_reg(dev,
COE_CR, &read_buf);
599 ret = smsc95xx_write_reg(dev,
COE_CR, read_buf);
606 static int smsc95xx_ethtool_get_eeprom_len(
struct net_device *
net)
611 static int smsc95xx_ethtool_get_eeprom(
struct net_device *netdev,
614 struct usbnet *dev = netdev_priv(netdev);
618 return smsc95xx_read_eeprom(dev, ee->
offset, ee->
len, data);
621 static int smsc95xx_ethtool_set_eeprom(
struct net_device *netdev,
624 struct usbnet *dev = netdev_priv(netdev);
627 netdev_warn(dev->
net,
"EEPROM: magic value mismatch, magic = 0x%x\n",
632 return smsc95xx_write_eeprom(dev, ee->
offset, ee->
len, data);
635 static int smsc95xx_ethtool_getregslen(
struct net_device *netdev)
645 struct usbnet *dev = netdev_priv(netdev);
652 netdev_warn(netdev,
"REGS: cannot read ID_REV\n");
657 retval = smsc95xx_read_reg(dev, i, &data[j]);
659 netdev_warn(netdev,
"REGS: cannot read reg[%x]\n", i);
668 struct usbnet *dev = netdev_priv(net);
675 static int smsc95xx_ethtool_set_wol(
struct net_device *net,
678 struct usbnet *dev = netdev_priv(net);
685 static const struct ethtool_ops smsc95xx_ethtool_ops = {
693 .get_eeprom_len = smsc95xx_ethtool_get_eeprom_len,
694 .get_eeprom = smsc95xx_ethtool_get_eeprom,
695 .set_eeprom = smsc95xx_ethtool_set_eeprom,
696 .get_regs_len = smsc95xx_ethtool_getregslen,
697 .get_regs = smsc95xx_ethtool_getregs,
698 .get_wol = smsc95xx_ethtool_get_wol,
699 .set_wol = smsc95xx_ethtool_set_wol,
704 struct usbnet *dev = netdev_priv(netdev);
706 if (!netif_running(netdev))
712 static void smsc95xx_init_mac_address(
struct usbnet *dev)
716 dev->
net->dev_addr) == 0) {
717 if (is_valid_ether_addr(dev->
net->dev_addr)) {
719 netif_dbg(dev, ifup, dev->
net,
"MAC address read from EEPROM\n");
725 eth_hw_addr_random(dev->
net);
726 netif_dbg(dev, ifup, dev->
net,
"MAC address set to eth_random_addr\n");
729 static int smsc95xx_set_mac_address(
struct usbnet *dev)
731 u32 addr_lo = dev->
net->dev_addr[0] | dev->
net->dev_addr[1] << 8 |
732 dev->
net->dev_addr[2] << 16 | dev->
net->dev_addr[3] << 24;
733 u32 addr_hi = dev->
net->dev_addr[4] | dev->
net->dev_addr[5] << 8;
736 ret = smsc95xx_write_reg(dev,
ADDRL, addr_lo);
739 ret = smsc95xx_write_reg(dev,
ADDRH, addr_hi);
746 static int smsc95xx_start_tx_path(
struct usbnet *dev)
755 spin_unlock_irqrestore(&pdata->
mac_cr_lock, flags);
768 static int smsc95xx_start_rx_path(
struct usbnet *dev)
776 spin_unlock_irqrestore(&pdata->
mac_cr_lock, flags);
784 static int smsc95xx_phy_initialize(
struct usbnet *dev)
790 dev->
mii.mdio_read = smsc95xx_mdio_read;
791 dev->
mii.mdio_write = smsc95xx_mdio_write;
792 dev->
mii.phy_id_mask = 0x1f;
793 dev->
mii.reg_num_mask = 0x1f;
803 }
while ((bmcr &
BMCR_RESET) && (timeout < 100));
805 if (timeout >= 100) {
806 netdev_warn(dev->
net,
"timeout on PHY Reset");
822 netif_dbg(dev, ifup, dev->
net,
"phy initialised successfully\n");
826 static int smsc95xx_reset(
struct usbnet *dev)
830 int ret = 0, timeout;
832 netif_dbg(dev, ifup, dev->
net,
"entering smsc95xx_reset\n");
840 ret = smsc95xx_read_reg(dev,
HW_CFG, &read_buf);
845 if (timeout >= 100) {
846 netdev_warn(dev->
net,
"timeout waiting for completion of Lite Reset\n");
856 ret = smsc95xx_read_reg(dev,
PM_CTRL, &read_buf);
861 if (timeout >= 100) {
862 netdev_warn(dev->
net,
"timeout waiting for PHY Reset\n");
866 ret = smsc95xx_set_mac_address(dev);
871 "MAC Address: %pM\n", dev->
net->dev_addr);
873 ret = smsc95xx_read_reg(dev,
HW_CFG, &read_buf);
877 "Read Value from HW_CFG : 0x%08x\n", read_buf);
881 ret = smsc95xx_write_reg(dev,
HW_CFG, read_buf);
884 ret = smsc95xx_read_reg(dev,
HW_CFG, &read_buf);
887 "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
904 ret = smsc95xx_write_reg(dev,
BURST_CAP, burst_cap);
907 ret = smsc95xx_read_reg(dev,
BURST_CAP, &read_buf);
911 "Read Value from BURST_CAP after writing: 0x%08x\n",
917 ret = smsc95xx_read_reg(dev,
BULK_IN_DLY, &read_buf);
921 "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
924 ret = smsc95xx_read_reg(dev,
HW_CFG, &read_buf);
928 "Read Value from HW_CFG: 0x%08x\n", read_buf);
938 ret = smsc95xx_write_reg(dev,
HW_CFG, read_buf);
941 ret = smsc95xx_read_reg(dev,
HW_CFG, &read_buf);
945 "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
950 ret = smsc95xx_read_reg(dev,
ID_REV, &read_buf);
952 netif_dbg(dev, ifup, dev->
net,
"ID_REV = 0x%08x\n", read_buf);
961 ret = smsc95xx_write_reg(dev,
FLOW, 0);
977 ret = smsc95xx_set_features(dev->
net, dev->
net->features);
980 smsc95xx_set_multicast(dev->
net);
982 ret = smsc95xx_phy_initialize(dev);
985 ret = smsc95xx_read_reg(dev,
INT_EP_CTL, &read_buf);
991 ret = smsc95xx_write_reg(dev,
INT_EP_CTL, read_buf);
994 ret = smsc95xx_start_tx_path(dev);
997 ret = smsc95xx_start_rx_path(dev);
1000 netif_dbg(dev, ifup, dev->
net,
"smsc95xx_reset, return 0\n");
1012 .ndo_do_ioctl = smsc95xx_ioctl,
1013 .ndo_set_rx_mode = smsc95xx_set_multicast,
1014 .ndo_set_features = smsc95xx_set_features,
1032 netdev_warn(dev->
net,
"Unable to allocate struct smsc95xx_priv\n");
1045 smsc95xx_init_mac_address(dev);
1048 ret = smsc95xx_reset(dev);
1050 dev->
net->netdev_ops = &smsc95xx_netdev_ops;
1051 dev->
net->ethtool_ops = &smsc95xx_ethtool_ops;
1071 struct usbnet *dev = usb_get_intfdata(intf);
1081 netdev_info(dev->
net,
"entering SUSPEND2 mode");
1084 ret = smsc95xx_read_reg(dev,
WUCSR, &val);
1089 ret = smsc95xx_write_reg(dev,
WUCSR, val);
1092 ret = smsc95xx_read_reg(dev,
PM_CTRL, &val);
1097 ret = smsc95xx_write_reg(dev,
PM_CTRL, val);
1101 ret = smsc95xx_read_reg(dev,
PM_CTRL, &val);
1107 ret = smsc95xx_write_reg(dev,
PM_CTRL, val);
1115 ret = smsc95xx_read_reg(dev,
WUCSR, &val);
1120 ret = smsc95xx_write_reg(dev,
WUCSR, val);
1125 ret = smsc95xx_read_reg(dev,
WUCSR, &val);
1129 netdev_info(dev->
net,
"enabling magic packet wakeup");
1132 netdev_info(dev->
net,
"disabling magic packet wakeup");
1136 ret = smsc95xx_write_reg(dev,
WUCSR, val);
1140 ret = smsc95xx_read_reg(dev,
PM_CTRL, &val);
1145 ret = smsc95xx_write_reg(dev,
PM_CTRL, val);
1149 smsc95xx_start_rx_path(dev);
1152 netdev_info(dev->
net,
"entering SUSPEND0 mode");
1154 ret = smsc95xx_read_reg(dev,
PM_CTRL, &val);
1160 ret = smsc95xx_write_reg(dev,
PM_CTRL, val);
1166 ret = smsc95xx_write_reg(dev,
PM_CTRL, val);
1170 ret = smsc95xx_read_reg(dev,
PM_CTRL, &val);
1180 struct usbnet *dev = usb_get_intfdata(intf);
1191 ret = smsc95xx_read_reg(dev,
WUCSR, &val);
1196 ret = smsc95xx_write_reg(dev,
WUCSR, val);
1200 ret = smsc95xx_read_reg(dev,
PM_CTRL, &val);
1206 ret = smsc95xx_write_reg(dev,
PM_CTRL, val);
1216 static void smsc95xx_rx_csum_offload(
struct sk_buff *
skb)
1218 skb->
csum = *(
u16 *)(skb_tail_pointer(skb) - 2);
1223 static int smsc95xx_rx_fixup(
struct usbnet *dev,
struct sk_buff *skb)
1225 while (skb->
len > 0) {
1242 "Error header=0x%08x\n", header);
1243 dev->
net->stats.rx_errors++;
1244 dev->
net->stats.rx_dropped++;
1247 dev->
net->stats.rx_crc_errors++;
1250 dev->
net->stats.rx_frame_errors++;
1254 dev->
net->stats.rx_length_errors++;
1260 "size err header=0x%08x\n", header);
1265 if (skb->
len == size) {
1267 smsc95xx_rx_csum_offload(skb);
1276 netdev_warn(dev->
net,
"Error allocating skb\n");
1281 ax_skb->
data = packet;
1282 skb_set_tail_pointer(ax_skb, size);
1285 smsc95xx_rx_csum_offload(ax_skb);
1300 netdev_warn(dev->
net,
"invalid rx length<0 %d\n", skb->
len);
1307 static u32 smsc95xx_calc_csum_preamble(
struct sk_buff *skb)
1309 u16 low_16 = (
u16)skb_checksum_start_offset(skb);
1311 return (high_16 << 16) | low_16;
1314 static struct sk_buff *smsc95xx_tx_fixup(
struct usbnet *dev,
1319 u32 tx_cmd_a, tx_cmd_b;
1322 BUG_ON(skb_shinfo(skb)->nr_frags);
1324 if (skb_headroom(skb) < overhead) {
1326 overhead, 0, flags);
1334 if (skb->
len <= 45) {
1337 long csstart = skb_checksum_start_offset(skb);
1339 skb->
len - csstart, 0);
1345 u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
1353 tx_cmd_b = (
u32)(skb->
len - 4);
1369 .description =
"smsc95xx USB 2.0 Ethernet",
1370 .bind = smsc95xx_bind,
1371 .unbind = smsc95xx_unbind,
1372 .link_reset = smsc95xx_link_reset,
1373 .reset = smsc95xx_reset,
1374 .rx_fixup = smsc95xx_rx_fixup,
1375 .tx_fixup = smsc95xx_tx_fixup,
1376 .status = smsc95xx_status,
1383 USB_DEVICE(0x0424, 0x9500),
1384 .driver_info = (
unsigned long) &smsc95xx_info,
1388 USB_DEVICE(0x0424, 0x9505),
1389 .driver_info = (
unsigned long) &smsc95xx_info,
1393 USB_DEVICE(0x0424, 0x9E00),
1394 .driver_info = (
unsigned long) &smsc95xx_info,
1398 USB_DEVICE(0x0424, 0x9E01),
1399 .driver_info = (
unsigned long) &smsc95xx_info,
1403 USB_DEVICE(0x0424, 0xec00),
1404 .driver_info = (
unsigned long) &smsc95xx_info,
1408 USB_DEVICE(0x0424, 0x9900),
1409 .driver_info = (
unsigned long) &smsc95xx_info,
1413 USB_DEVICE(0x0424, 0x9901),
1414 .driver_info = (
unsigned long) &smsc95xx_info,
1418 USB_DEVICE(0x0424, 0x9902),
1419 .driver_info = (
unsigned long) &smsc95xx_info,
1423 USB_DEVICE(0x0424, 0x9903),
1424 .driver_info = (
unsigned long) &smsc95xx_info,
1428 USB_DEVICE(0x0424, 0x9904),
1429 .driver_info = (
unsigned long) &smsc95xx_info,
1433 USB_DEVICE(0x0424, 0x9905),
1434 .driver_info = (
unsigned long) &smsc95xx_info,
1438 USB_DEVICE(0x0424, 0x9906),
1439 .driver_info = (
unsigned long) &smsc95xx_info,
1443 USB_DEVICE(0x0424, 0x9907),
1444 .driver_info = (
unsigned long) &smsc95xx_info,
1448 USB_DEVICE(0x0424, 0x9908),
1449 .driver_info = (
unsigned long) &smsc95xx_info,
1453 USB_DEVICE(0x0424, 0x9909),
1454 .driver_info = (
unsigned long) &smsc95xx_info,
1458 USB_DEVICE(0x0424, 0x9530),
1459 .driver_info = (
unsigned long) &smsc95xx_info,
1463 USB_DEVICE(0x0424, 0x9730),
1464 .driver_info = (
unsigned long) &smsc95xx_info,
1468 USB_DEVICE(0x0424, 0x9E08),
1469 .driver_info = (
unsigned long) &smsc95xx_info,
1475 static struct usb_driver smsc95xx_driver = {
1477 .id_table = products,
1479 .suspend = smsc95xx_suspend,
1480 .resume = smsc95xx_resume,
1481 .reset_resume = smsc95xx_resume,
1483 .disable_hub_initiated_lpm = 1,