1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /***************************************************************************
4 * Copyright (C) 2007-2008 SMSC
6 *****************************************************************************/
8 #include <linux/module.h>
9 #include <linux/kmod.h>
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/ethtool.h>
13 #include <linux/mii.h>
14 #include <linux/usb.h>
15 #include <linux/bitrev.h>
16 #include <linux/crc16.h>
17 #include <linux/crc32.h>
18 #include <linux/usb/usbnet.h>
19 #include <linux/slab.h>
20 #include <linux/of_net.h>
21 #include <linux/mdio.h>
22 #include <linux/phy.h>
25 #define SMSC_CHIPNAME "smsc95xx"
26 #define SMSC_DRIVER_VERSION "2.0.0"
27 #define HS_USB_PKT_SIZE (512)
28 #define FS_USB_PKT_SIZE (64)
29 #define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
30 #define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
31 #define DEFAULT_BULK_IN_DELAY (0x00002000)
32 #define MAX_SINGLE_PACKET_SIZE (2048)
33 #define LAN95XX_EEPROM_MAGIC (0x9500)
34 #define EEPROM_MAC_OFFSET (0x01)
35 #define DEFAULT_TX_CSUM_ENABLE (true)
36 #define DEFAULT_RX_CSUM_ENABLE (true)
37 #define SMSC95XX_INTERNAL_PHY_ID (1)
38 #define SMSC95XX_TX_OVERHEAD (8)
39 #define SMSC95XX_TX_OVERHEAD_CSUM (12)
40 #define SUPPORTED_WAKE (WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
41 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
43 #define FEATURE_8_WAKEUP_FILTERS (0x01)
44 #define FEATURE_PHY_NLP_CROSSOVER (0x02)
45 #define FEATURE_REMOTE_WAKEUP (0x04)
47 #define SUSPEND_SUSPEND0 (0x01)
48 #define SUSPEND_SUSPEND1 (0x02)
49 #define SUSPEND_SUSPEND2 (0x04)
50 #define SUSPEND_SUSPEND3 (0x08)
51 #define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
52 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
54 struct smsc95xx_priv {
59 spinlock_t mac_cr_lock;
62 struct mii_bus *mdiobus;
63 struct phy_device *phydev;
66 static bool turbo_mode = true;
67 module_param(turbo_mode, bool, 0644);
68 MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
70 static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
75 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
82 fn = usbnet_read_cmd_nopm;
84 ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
85 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
87 if (unlikely(ret < 0)) {
88 netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
99 static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index,
104 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
109 fn = usbnet_write_cmd;
111 fn = usbnet_write_cmd_nopm;
116 ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
117 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
119 if (unlikely(ret < 0))
120 netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
126 static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index,
129 return __smsc95xx_read_reg(dev, index, data, 1);
132 static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index,
135 return __smsc95xx_write_reg(dev, index, data, 1);
138 static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
141 return __smsc95xx_read_reg(dev, index, data, 0);
144 static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
147 return __smsc95xx_write_reg(dev, index, data, 0);
150 /* Loop until the read is completed with timeout
151 * called with phy_mutex held */
152 static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
155 unsigned long start_time = jiffies;
160 ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
162 netdev_warn(dev->net, "Error reading MII_ACCESS\n");
166 if (!(val & MII_BUSY_))
168 } while (!time_after(jiffies, start_time + HZ));
173 static u32 mii_address_cmd(int phy_id, int idx, u16 op)
175 return (phy_id & 0x1f) << 11 | (idx & 0x1f) << 6 | op;
178 static int __smsc95xx_mdio_read(struct usbnet *dev, int phy_id, int idx,
184 mutex_lock(&dev->phy_mutex);
186 /* confirm MII not busy */
187 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
189 netdev_warn(dev->net, "%s: MII is busy\n", __func__);
193 /* set the address, index & direction (read from PHY) */
194 addr = mii_address_cmd(phy_id, idx, MII_READ_ | MII_BUSY_);
195 ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
197 netdev_warn(dev->net, "Error writing MII_ADDR\n");
201 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
203 netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
207 ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
209 netdev_warn(dev->net, "Error reading MII_DATA\n");
213 ret = (u16)(val & 0xFFFF);
216 mutex_unlock(&dev->phy_mutex);
220 static void __smsc95xx_mdio_write(struct usbnet *dev, int phy_id,
221 int idx, int regval, int in_pm)
226 mutex_lock(&dev->phy_mutex);
228 /* confirm MII not busy */
229 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
231 netdev_warn(dev->net, "%s: MII is busy\n", __func__);
236 ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
238 netdev_warn(dev->net, "Error writing MII_DATA\n");
242 /* set the address, index & direction (write to PHY) */
243 addr = mii_address_cmd(phy_id, idx, MII_WRITE_ | MII_BUSY_);
244 ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
246 netdev_warn(dev->net, "Error writing MII_ADDR\n");
250 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
252 netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
257 mutex_unlock(&dev->phy_mutex);
260 static int smsc95xx_mdio_read_nopm(struct usbnet *dev, int idx)
262 struct smsc95xx_priv *pdata = dev->driver_priv;
264 return __smsc95xx_mdio_read(dev, pdata->phydev->mdio.addr, idx, 1);
267 static void smsc95xx_mdio_write_nopm(struct usbnet *dev, int idx, int regval)
269 struct smsc95xx_priv *pdata = dev->driver_priv;
271 __smsc95xx_mdio_write(dev, pdata->phydev->mdio.addr, idx, regval, 1);
274 static int smsc95xx_mdiobus_read(struct mii_bus *bus, int phy_id, int idx)
276 struct usbnet *dev = bus->priv;
278 return __smsc95xx_mdio_read(dev, phy_id, idx, 0);
281 static int smsc95xx_mdiobus_write(struct mii_bus *bus, int phy_id, int idx,
284 struct usbnet *dev = bus->priv;
286 __smsc95xx_mdio_write(dev, phy_id, idx, regval, 0);
290 static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
292 unsigned long start_time = jiffies;
297 ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
299 netdev_warn(dev->net, "Error reading E2P_CMD\n");
303 if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
306 } while (!time_after(jiffies, start_time + HZ));
308 if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
309 netdev_warn(dev->net, "EEPROM read operation timeout\n");
316 static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
318 unsigned long start_time = jiffies;
323 ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
325 netdev_warn(dev->net, "Error reading E2P_CMD\n");
329 if (!(val & E2P_CMD_BUSY_))
333 } while (!time_after(jiffies, start_time + HZ));
335 netdev_warn(dev->net, "EEPROM is busy\n");
339 static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
348 ret = smsc95xx_eeprom_confirm_not_busy(dev);
352 for (i = 0; i < length; i++) {
353 val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
354 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
356 netdev_warn(dev->net, "Error writing E2P_CMD\n");
360 ret = smsc95xx_wait_eeprom(dev);
364 ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
366 netdev_warn(dev->net, "Error reading E2P_DATA\n");
370 data[i] = val & 0xFF;
377 static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
386 ret = smsc95xx_eeprom_confirm_not_busy(dev);
390 /* Issue write/erase enable command */
391 val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
392 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
394 netdev_warn(dev->net, "Error writing E2P_DATA\n");
398 ret = smsc95xx_wait_eeprom(dev);
402 for (i = 0; i < length; i++) {
404 /* Fill data register */
406 ret = smsc95xx_write_reg(dev, E2P_DATA, val);
408 netdev_warn(dev->net, "Error writing E2P_DATA\n");
412 /* Send "write" command */
413 val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
414 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
416 netdev_warn(dev->net, "Error writing E2P_CMD\n");
420 ret = smsc95xx_wait_eeprom(dev);
430 static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
440 ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
441 USB_DIR_OUT | USB_TYPE_VENDOR |
443 0, index, &buf, size);
445 netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
450 /* returns hash bit number for given MAC address
452 * 01 00 5E 00 00 01 -> returns bit number 31 */
453 static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
455 return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
458 static void smsc95xx_set_multicast(struct net_device *netdev)
460 struct usbnet *dev = netdev_priv(netdev);
461 struct smsc95xx_priv *pdata = dev->driver_priv;
468 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
470 if (dev->net->flags & IFF_PROMISC) {
471 netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
472 pdata->mac_cr |= MAC_CR_PRMS_;
473 pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
474 } else if (dev->net->flags & IFF_ALLMULTI) {
475 netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
476 pdata->mac_cr |= MAC_CR_MCPAS_;
477 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
478 } else if (!netdev_mc_empty(dev->net)) {
479 struct netdev_hw_addr *ha;
481 pdata->mac_cr |= MAC_CR_HPFILT_;
482 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
484 netdev_for_each_mc_addr(ha, netdev) {
485 u32 bitnum = smsc95xx_hash(ha->addr);
486 u32 mask = 0x01 << (bitnum & 0x1F);
488 pdata->hash_hi |= mask;
490 pdata->hash_lo |= mask;
493 netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
494 pdata->hash_hi, pdata->hash_lo);
496 netif_dbg(dev, drv, dev->net, "receive own packets only\n");
498 ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
501 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
503 /* Initiate async writes, as we can't wait for completion here */
504 ret = smsc95xx_write_reg_async(dev, HASHH, pdata->hash_hi);
506 netdev_warn(dev->net, "failed to initiate async write to HASHH\n");
508 ret = smsc95xx_write_reg_async(dev, HASHL, pdata->hash_lo);
510 netdev_warn(dev->net, "failed to initiate async write to HASHL\n");
512 ret = smsc95xx_write_reg_async(dev, MAC_CR, pdata->mac_cr);
514 netdev_warn(dev->net, "failed to initiate async write to MAC_CR\n");
517 static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev)
519 u32 flow = 0, afc_cfg;
520 struct smsc95xx_priv *pdata = dev->driver_priv;
521 bool tx_pause, rx_pause;
523 int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
527 if (pdata->phydev->duplex == DUPLEX_FULL) {
528 phy_get_pause(pdata->phydev, &tx_pause, &rx_pause);
540 netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
541 rx_pause ? "enabled" : "disabled",
542 tx_pause ? "enabled" : "disabled");
544 netif_dbg(dev, link, dev->net, "half duplex\n");
548 ret = smsc95xx_write_reg(dev, FLOW, flow);
552 return smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
555 static int smsc95xx_link_reset(struct usbnet *dev)
557 struct smsc95xx_priv *pdata = dev->driver_priv;
561 ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
565 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
566 if (pdata->phydev->duplex != DUPLEX_FULL) {
567 pdata->mac_cr &= ~MAC_CR_FDPX_;
568 pdata->mac_cr |= MAC_CR_RCVOWN_;
570 pdata->mac_cr &= ~MAC_CR_RCVOWN_;
571 pdata->mac_cr |= MAC_CR_FDPX_;
573 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
575 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
579 ret = smsc95xx_phy_update_flowcontrol(dev);
581 netdev_warn(dev->net, "Error updating PHY flow control\n");
586 static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
590 if (urb->actual_length != 4) {
591 netdev_warn(dev->net, "unexpected urb length %d\n",
596 intdata = get_unaligned_le32(urb->transfer_buffer);
597 netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
599 if (intdata & INT_ENP_PHY_INT_)
600 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
602 netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
606 /* Enable or disable Tx & Rx checksum offload engines */
607 static int smsc95xx_set_features(struct net_device *netdev,
608 netdev_features_t features)
610 struct usbnet *dev = netdev_priv(netdev);
614 ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
618 if (features & NETIF_F_IP_CSUM)
619 read_buf |= Tx_COE_EN_;
621 read_buf &= ~Tx_COE_EN_;
623 if (features & NETIF_F_RXCSUM)
624 read_buf |= Rx_COE_EN_;
626 read_buf &= ~Rx_COE_EN_;
628 ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
632 netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
636 static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
638 return MAX_EEPROM_SIZE;
641 static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
642 struct ethtool_eeprom *ee, u8 *data)
644 struct usbnet *dev = netdev_priv(netdev);
646 ee->magic = LAN95XX_EEPROM_MAGIC;
648 return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
651 static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
652 struct ethtool_eeprom *ee, u8 *data)
654 struct usbnet *dev = netdev_priv(netdev);
656 if (ee->magic != LAN95XX_EEPROM_MAGIC) {
657 netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
662 return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
665 static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
667 /* all smsc95xx registers */
668 return COE_CR - ID_REV + sizeof(u32);
672 smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
675 struct usbnet *dev = netdev_priv(netdev);
680 retval = smsc95xx_read_reg(dev, ID_REV, ®s->version);
682 netdev_warn(netdev, "REGS: cannot read ID_REV\n");
686 for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
687 retval = smsc95xx_read_reg(dev, i, &data[j]);
689 netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
695 static void smsc95xx_ethtool_get_wol(struct net_device *net,
696 struct ethtool_wolinfo *wolinfo)
698 struct usbnet *dev = netdev_priv(net);
699 struct smsc95xx_priv *pdata = dev->driver_priv;
701 wolinfo->supported = SUPPORTED_WAKE;
702 wolinfo->wolopts = pdata->wolopts;
705 static int smsc95xx_ethtool_set_wol(struct net_device *net,
706 struct ethtool_wolinfo *wolinfo)
708 struct usbnet *dev = netdev_priv(net);
709 struct smsc95xx_priv *pdata = dev->driver_priv;
712 if (wolinfo->wolopts & ~SUPPORTED_WAKE)
715 pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
717 ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
719 netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
724 static u32 smsc95xx_get_link(struct net_device *net)
726 phy_read_status(net->phydev);
727 return net->phydev->link;
730 static const struct ethtool_ops smsc95xx_ethtool_ops = {
731 .get_link = smsc95xx_get_link,
732 .nway_reset = phy_ethtool_nway_reset,
733 .get_drvinfo = usbnet_get_drvinfo,
734 .get_msglevel = usbnet_get_msglevel,
735 .set_msglevel = usbnet_set_msglevel,
736 .get_eeprom_len = smsc95xx_ethtool_get_eeprom_len,
737 .get_eeprom = smsc95xx_ethtool_get_eeprom,
738 .set_eeprom = smsc95xx_ethtool_set_eeprom,
739 .get_regs_len = smsc95xx_ethtool_getregslen,
740 .get_regs = smsc95xx_ethtool_getregs,
741 .get_wol = smsc95xx_ethtool_get_wol,
742 .set_wol = smsc95xx_ethtool_set_wol,
743 .get_link_ksettings = phy_ethtool_get_link_ksettings,
744 .set_link_ksettings = phy_ethtool_set_link_ksettings,
745 .get_ts_info = ethtool_op_get_ts_info,
748 static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
750 if (!netif_running(netdev))
753 return phy_mii_ioctl(netdev->phydev, rq, cmd);
756 static void smsc95xx_init_mac_address(struct usbnet *dev)
760 /* maybe the boot loader passed the MAC address in devicetree */
761 mac_addr = of_get_mac_address(dev->udev->dev.of_node);
762 if (!IS_ERR(mac_addr)) {
763 ether_addr_copy(dev->net->dev_addr, mac_addr);
767 /* try reading mac address from EEPROM */
768 if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
769 dev->net->dev_addr) == 0) {
770 if (is_valid_ether_addr(dev->net->dev_addr)) {
771 /* eeprom values are valid so use them */
772 netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
777 /* no useful static MAC address found. generate a random one */
778 eth_hw_addr_random(dev->net);
779 netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
782 static int smsc95xx_set_mac_address(struct usbnet *dev)
784 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
785 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
786 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
789 ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
793 return smsc95xx_write_reg(dev, ADDRH, addr_hi);
796 /* starts the TX path */
797 static int smsc95xx_start_tx_path(struct usbnet *dev)
799 struct smsc95xx_priv *pdata = dev->driver_priv;
803 /* Enable Tx at MAC */
804 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
805 pdata->mac_cr |= MAC_CR_TXEN_;
806 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
808 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
812 /* Enable Tx at SCSRs */
813 return smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
816 /* Starts the Receive path */
817 static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
819 struct smsc95xx_priv *pdata = dev->driver_priv;
822 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
823 pdata->mac_cr |= MAC_CR_RXEN_;
824 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
826 return __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
829 static int smsc95xx_reset(struct usbnet *dev)
831 struct smsc95xx_priv *pdata = dev->driver_priv;
832 u32 read_buf, write_buf, burst_cap;
833 int ret = 0, timeout;
835 netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
837 ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
844 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
848 } while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
850 if (timeout >= 100) {
851 netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
855 ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
862 ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
866 } while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
868 if (timeout >= 100) {
869 netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
873 ret = smsc95xx_set_mac_address(dev);
877 netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
880 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
884 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
887 read_buf |= HW_CFG_BIR_;
889 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
893 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
897 netif_dbg(dev, ifup, dev->net,
898 "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
903 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
904 } else if (dev->udev->speed == USB_SPEED_HIGH) {
905 burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
906 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
908 burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
909 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
912 netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
913 (ulong)dev->rx_urb_size);
915 ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
919 ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
923 netif_dbg(dev, ifup, dev->net,
924 "Read Value from BURST_CAP after writing: 0x%08x\n",
927 ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
931 ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
935 netif_dbg(dev, ifup, dev->net,
936 "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
939 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
943 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
947 read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
949 read_buf &= ~HW_CFG_RXDOFF_;
951 /* set Rx data offset=2, Make IP header aligns on word boundary. */
952 read_buf |= NET_IP_ALIGN << 9;
954 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
958 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
962 netif_dbg(dev, ifup, dev->net,
963 "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
965 ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
969 ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
972 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
974 /* Configure GPIO pins as LED outputs */
975 write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
976 LED_GPIO_CFG_FDX_LED;
977 ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
982 ret = smsc95xx_write_reg(dev, FLOW, 0);
986 ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
990 /* Don't need mac_cr_lock during initialisation */
991 ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
997 ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
1001 /* Enable or disable checksum offload engines */
1002 ret = smsc95xx_set_features(dev->net, dev->net->features);
1004 netdev_warn(dev->net, "Failed to set checksum offload features\n");
1008 smsc95xx_set_multicast(dev->net);
1010 ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
1014 /* enable PHY interrupts */
1015 read_buf |= INT_EP_CTL_PHY_INT_;
1017 ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
1021 ret = smsc95xx_start_tx_path(dev);
1023 netdev_warn(dev->net, "Failed to start TX path\n");
1027 ret = smsc95xx_start_rx_path(dev, 0);
1029 netdev_warn(dev->net, "Failed to start RX path\n");
1033 netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
1037 static const struct net_device_ops smsc95xx_netdev_ops = {
1038 .ndo_open = usbnet_open,
1039 .ndo_stop = usbnet_stop,
1040 .ndo_start_xmit = usbnet_start_xmit,
1041 .ndo_tx_timeout = usbnet_tx_timeout,
1042 .ndo_change_mtu = usbnet_change_mtu,
1043 .ndo_get_stats64 = usbnet_get_stats64,
1044 .ndo_set_mac_address = eth_mac_addr,
1045 .ndo_validate_addr = eth_validate_addr,
1046 .ndo_do_ioctl = smsc95xx_ioctl,
1047 .ndo_set_rx_mode = smsc95xx_set_multicast,
1048 .ndo_set_features = smsc95xx_set_features,
1051 static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
1053 struct smsc95xx_priv *pdata;
1054 bool is_internal_phy;
1058 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1060 ret = usbnet_get_endpoints(dev, intf);
1062 netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1066 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1070 dev->driver_priv = pdata;
1072 spin_lock_init(&pdata->mac_cr_lock);
1074 /* LAN95xx devices do not alter the computed checksum of 0 to 0xffff.
1075 * RFC 2460, ipv6 UDP calculated checksum yields a result of zero must
1076 * be changed to 0xffff. RFC 768, ipv4 UDP computed checksum is zero,
1077 * it is transmitted as all ones. The zero transmitted checksum means
1078 * transmitter generated no checksum. Hence, enable csum offload only
1081 if (DEFAULT_TX_CSUM_ENABLE)
1082 dev->net->features |= NETIF_F_IP_CSUM;
1083 if (DEFAULT_RX_CSUM_ENABLE)
1084 dev->net->features |= NETIF_F_RXCSUM;
1086 dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1087 set_bit(EVENT_NO_IP_ALIGN, &dev->flags);
1089 smsc95xx_init_mac_address(dev);
1091 /* Init all registers */
1092 ret = smsc95xx_reset(dev);
1096 pdata->mdiobus = mdiobus_alloc();
1097 if (!pdata->mdiobus) {
1102 ret = smsc95xx_read_reg(dev, HW_CFG, &val);
1106 is_internal_phy = !(val & HW_CFG_PSEL_);
1107 if (is_internal_phy)
1108 pdata->mdiobus->phy_mask = ~(1u << SMSC95XX_INTERNAL_PHY_ID);
1110 pdata->mdiobus->priv = dev;
1111 pdata->mdiobus->read = smsc95xx_mdiobus_read;
1112 pdata->mdiobus->write = smsc95xx_mdiobus_write;
1113 pdata->mdiobus->name = "smsc95xx-mdiobus";
1114 pdata->mdiobus->parent = &dev->udev->dev;
1116 snprintf(pdata->mdiobus->id, ARRAY_SIZE(pdata->mdiobus->id),
1117 "usb-%03d:%03d", dev->udev->bus->busnum, dev->udev->devnum);
1119 ret = mdiobus_register(pdata->mdiobus);
1121 netdev_err(dev->net, "Could not register MDIO bus\n");
1125 pdata->phydev = phy_find_first(pdata->mdiobus);
1126 if (!pdata->phydev) {
1127 netdev_err(dev->net, "no PHY found\n");
1129 goto unregister_mdio;
1132 pdata->phydev->is_internal = is_internal_phy;
1134 /* detect device revision as different features may be available */
1135 ret = smsc95xx_read_reg(dev, ID_REV, &val);
1137 goto unregister_mdio;
1140 if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
1141 (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
1142 pdata->features = (FEATURE_8_WAKEUP_FILTERS |
1143 FEATURE_PHY_NLP_CROSSOVER |
1144 FEATURE_REMOTE_WAKEUP);
1145 else if (val == ID_REV_CHIP_ID_9512_)
1146 pdata->features = FEATURE_8_WAKEUP_FILTERS;
1148 dev->net->netdev_ops = &smsc95xx_netdev_ops;
1149 dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
1150 dev->net->flags |= IFF_MULTICAST;
1151 dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
1152 dev->net->min_mtu = ETH_MIN_MTU;
1153 dev->net->max_mtu = ETH_DATA_LEN;
1154 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1158 mdiobus_unregister(pdata->mdiobus);
1161 mdiobus_free(pdata->mdiobus);
1168 static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1170 struct smsc95xx_priv *pdata = dev->driver_priv;
1172 mdiobus_unregister(pdata->mdiobus);
1173 mdiobus_free(pdata->mdiobus);
1174 netif_dbg(dev, ifdown, dev->net, "free pdata\n");
1178 static void smsc95xx_handle_link_change(struct net_device *net)
1180 phy_print_status(net->phydev);
1183 static int smsc95xx_start_phy(struct usbnet *dev)
1185 struct smsc95xx_priv *pdata = dev->driver_priv;
1186 struct net_device *net = dev->net;
1189 ret = smsc95xx_reset(dev);
1193 ret = phy_connect_direct(net, pdata->phydev,
1194 &smsc95xx_handle_link_change,
1195 PHY_INTERFACE_MODE_MII);
1197 netdev_err(net, "can't attach PHY to %s\n", pdata->mdiobus->id);
1201 phy_attached_info(net->phydev);
1202 phy_start(net->phydev);
1206 static int smsc95xx_disconnect_phy(struct usbnet *dev)
1208 phy_stop(dev->net->phydev);
1209 phy_disconnect(dev->net->phydev);
1213 static u32 smsc_crc(const u8 *buffer, size_t len, int filter)
1215 u32 crc = bitrev16(crc16(0xFFFF, buffer, len));
1216 return crc << ((filter % 2) * 16);
1219 static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1223 netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1226 ret = smsc95xx_mdio_read_nopm(dev, PHY_INT_SRC);
1230 /* enable interrupt source */
1231 ret = smsc95xx_mdio_read_nopm(dev, PHY_INT_MASK);
1237 smsc95xx_mdio_write_nopm(dev, PHY_INT_MASK, ret);
1242 static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1246 /* first, a dummy read, needed to latch some MII phys */
1247 ret = smsc95xx_mdio_read_nopm(dev, MII_BMSR);
1251 ret = smsc95xx_mdio_read_nopm(dev, MII_BMSR);
1255 return !!(ret & BMSR_LSTATUS);
1258 static int smsc95xx_enter_suspend0(struct usbnet *dev)
1260 struct smsc95xx_priv *pdata = dev->driver_priv;
1264 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1268 val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1269 val |= PM_CTL_SUS_MODE_0;
1271 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1275 /* clear wol status */
1276 val &= ~PM_CTL_WUPS_;
1277 val |= PM_CTL_WUPS_WOL_;
1279 /* enable energy detection */
1280 if (pdata->wolopts & WAKE_PHY)
1281 val |= PM_CTL_WUPS_ED_;
1283 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1287 /* read back PM_CTRL */
1288 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1292 pdata->suspend_flags |= SUSPEND_SUSPEND0;
1297 static int smsc95xx_enter_suspend1(struct usbnet *dev)
1299 struct smsc95xx_priv *pdata = dev->driver_priv;
1303 /* reconfigure link pulse detection timing for
1304 * compatibility with non-standard link partners
1306 if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
1307 smsc95xx_mdio_write_nopm(dev, PHY_EDPD_CONFIG,
1308 PHY_EDPD_CONFIG_DEFAULT);
1310 /* enable energy detect power-down mode */
1311 ret = smsc95xx_mdio_read_nopm(dev, PHY_MODE_CTRL_STS);
1315 ret |= MODE_CTRL_STS_EDPWRDOWN_;
1317 smsc95xx_mdio_write_nopm(dev, PHY_MODE_CTRL_STS, ret);
1319 /* enter SUSPEND1 mode */
1320 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1324 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1325 val |= PM_CTL_SUS_MODE_1;
1327 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1331 /* clear wol status, enable energy detection */
1332 val &= ~PM_CTL_WUPS_;
1333 val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1335 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1339 pdata->suspend_flags |= SUSPEND_SUSPEND1;
1344 static int smsc95xx_enter_suspend2(struct usbnet *dev)
1346 struct smsc95xx_priv *pdata = dev->driver_priv;
1350 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1354 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1355 val |= PM_CTL_SUS_MODE_2;
1357 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1361 pdata->suspend_flags |= SUSPEND_SUSPEND2;
1366 static int smsc95xx_enter_suspend3(struct usbnet *dev)
1368 struct smsc95xx_priv *pdata = dev->driver_priv;
1372 ret = smsc95xx_read_reg_nopm(dev, RX_FIFO_INF, &val);
1376 if (val & RX_FIFO_INF_USED_) {
1377 netdev_info(dev->net, "rx fifo not empty in autosuspend\n");
1381 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1385 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1386 val |= PM_CTL_SUS_MODE_3 | PM_CTL_RES_CLR_WKP_STS;
1388 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1392 /* clear wol status */
1393 val &= ~PM_CTL_WUPS_;
1394 val |= PM_CTL_WUPS_WOL_;
1396 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1400 pdata->suspend_flags |= SUSPEND_SUSPEND3;
1405 static int smsc95xx_autosuspend(struct usbnet *dev, u32 link_up)
1407 struct smsc95xx_priv *pdata = dev->driver_priv;
1410 if (!netif_running(dev->net)) {
1411 /* interface is ifconfig down so fully power down hw */
1412 netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1413 return smsc95xx_enter_suspend2(dev);
1417 /* link is down so enter EDPD mode, but only if device can
1418 * reliably resume from it. This check should be redundant
1419 * as current FEATURE_REMOTE_WAKEUP parts also support
1420 * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */
1421 if (!(pdata->features & FEATURE_PHY_NLP_CROSSOVER)) {
1422 netdev_warn(dev->net, "EDPD not supported\n");
1426 netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1428 /* enable PHY wakeup events for if cable is attached */
1429 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1430 PHY_INT_MASK_ANEG_COMP_);
1432 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1436 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1437 return smsc95xx_enter_suspend1(dev);
1440 /* enable PHY wakeup events so we remote wakeup if cable is pulled */
1441 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1442 PHY_INT_MASK_LINK_DOWN_);
1444 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1448 netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1449 return smsc95xx_enter_suspend3(dev);
1452 static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1454 struct usbnet *dev = usb_get_intfdata(intf);
1455 struct smsc95xx_priv *pdata = dev->driver_priv;
1459 ret = usbnet_suspend(intf, message);
1461 netdev_warn(dev->net, "usbnet_suspend error\n");
1465 if (pdata->suspend_flags) {
1466 netdev_warn(dev->net, "error during last resume\n");
1467 pdata->suspend_flags = 0;
1470 /* determine if link is up using only _nopm functions */
1471 link_up = smsc95xx_link_ok_nopm(dev);
1473 if (message.event == PM_EVENT_AUTO_SUSPEND &&
1474 (pdata->features & FEATURE_REMOTE_WAKEUP)) {
1475 ret = smsc95xx_autosuspend(dev, link_up);
1479 /* if we get this far we're not autosuspending */
1480 /* if no wol options set, or if link is down and we're not waking on
1481 * PHY activity, enter lowest power SUSPEND2 mode
1483 if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1484 !(link_up || (pdata->wolopts & WAKE_PHY))) {
1485 netdev_info(dev->net, "entering SUSPEND2 mode\n");
1487 /* disable energy detect (link up) & wake up events */
1488 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1492 val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1494 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1498 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1502 val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1504 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1508 ret = smsc95xx_enter_suspend2(dev);
1512 if (pdata->wolopts & WAKE_PHY) {
1513 ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1514 (PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
1516 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1520 /* if link is down then configure EDPD and enter SUSPEND1,
1521 * otherwise enter SUSPEND0 below
1524 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1525 ret = smsc95xx_enter_suspend1(dev);
1530 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1531 u32 *filter_mask = kcalloc(32, sizeof(u32), GFP_KERNEL);
1535 int wuff_filter_count =
1536 (pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
1537 LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
1541 netdev_warn(dev->net, "Unable to allocate filter_mask\n");
1546 memset(command, 0, sizeof(command));
1547 memset(offset, 0, sizeof(offset));
1548 memset(crc, 0, sizeof(crc));
1550 if (pdata->wolopts & WAKE_BCAST) {
1551 const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1552 netdev_info(dev->net, "enabling broadcast detection\n");
1553 filter_mask[filter * 4] = 0x003F;
1554 filter_mask[filter * 4 + 1] = 0x00;
1555 filter_mask[filter * 4 + 2] = 0x00;
1556 filter_mask[filter * 4 + 3] = 0x00;
1557 command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1558 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1559 crc[filter/2] |= smsc_crc(bcast, 6, filter);
1563 if (pdata->wolopts & WAKE_MCAST) {
1564 const u8 mcast[] = {0x01, 0x00, 0x5E};
1565 netdev_info(dev->net, "enabling multicast detection\n");
1566 filter_mask[filter * 4] = 0x0007;
1567 filter_mask[filter * 4 + 1] = 0x00;
1568 filter_mask[filter * 4 + 2] = 0x00;
1569 filter_mask[filter * 4 + 3] = 0x00;
1570 command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1571 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1572 crc[filter/2] |= smsc_crc(mcast, 3, filter);
1576 if (pdata->wolopts & WAKE_ARP) {
1577 const u8 arp[] = {0x08, 0x06};
1578 netdev_info(dev->net, "enabling ARP detection\n");
1579 filter_mask[filter * 4] = 0x0003;
1580 filter_mask[filter * 4 + 1] = 0x00;
1581 filter_mask[filter * 4 + 2] = 0x00;
1582 filter_mask[filter * 4 + 3] = 0x00;
1583 command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1584 offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1585 crc[filter/2] |= smsc_crc(arp, 2, filter);
1589 if (pdata->wolopts & WAKE_UCAST) {
1590 netdev_info(dev->net, "enabling unicast detection\n");
1591 filter_mask[filter * 4] = 0x003F;
1592 filter_mask[filter * 4 + 1] = 0x00;
1593 filter_mask[filter * 4 + 2] = 0x00;
1594 filter_mask[filter * 4 + 3] = 0x00;
1595 command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1596 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1597 crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1601 for (i = 0; i < (wuff_filter_count * 4); i++) {
1602 ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
1610 for (i = 0; i < (wuff_filter_count / 4); i++) {
1611 ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
1616 for (i = 0; i < (wuff_filter_count / 4); i++) {
1617 ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
1622 for (i = 0; i < (wuff_filter_count / 2); i++) {
1623 ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
1628 /* clear any pending pattern match packet status */
1629 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1635 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1640 if (pdata->wolopts & WAKE_MAGIC) {
1641 /* clear any pending magic packet status */
1642 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1648 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1653 /* enable/disable wakeup sources */
1654 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1658 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1659 netdev_info(dev->net, "enabling pattern match wakeup\n");
1660 val |= WUCSR_WAKE_EN_;
1662 netdev_info(dev->net, "disabling pattern match wakeup\n");
1663 val &= ~WUCSR_WAKE_EN_;
1666 if (pdata->wolopts & WAKE_MAGIC) {
1667 netdev_info(dev->net, "enabling magic packet wakeup\n");
1670 netdev_info(dev->net, "disabling magic packet wakeup\n");
1671 val &= ~WUCSR_MPEN_;
1674 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1678 /* enable wol wakeup source */
1679 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1683 val |= PM_CTL_WOL_EN_;
1685 /* phy energy detect wakeup source */
1686 if (pdata->wolopts & WAKE_PHY)
1687 val |= PM_CTL_ED_EN_;
1689 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1693 /* enable receiver to enable frame reception */
1694 smsc95xx_start_rx_path(dev, 1);
1696 /* some wol options are enabled, so enter SUSPEND0 */
1697 netdev_info(dev->net, "entering SUSPEND0 mode\n");
1698 ret = smsc95xx_enter_suspend0(dev);
1702 * TODO: resume() might need to handle the suspend failure
1705 if (ret && PMSG_IS_AUTO(message))
1706 usbnet_resume(intf);
1711 static int smsc95xx_resume(struct usb_interface *intf)
1713 struct usbnet *dev = usb_get_intfdata(intf);
1714 struct smsc95xx_priv *pdata;
1720 pdata = dev->driver_priv;
1721 suspend_flags = pdata->suspend_flags;
1723 netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
1725 /* do this first to ensure it's cleared even in error case */
1726 pdata->suspend_flags = 0;
1728 if (suspend_flags & SUSPEND_ALLMODES) {
1729 /* clear wake-up sources */
1730 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1734 val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
1736 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1740 /* clear wake-up status */
1741 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1745 val &= ~PM_CTL_WOL_EN_;
1746 val |= PM_CTL_WUPS_;
1748 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1753 ret = usbnet_resume(intf);
1755 netdev_warn(dev->net, "usbnet_resume error\n");
1757 phy_init_hw(pdata->phydev);
1761 static int smsc95xx_reset_resume(struct usb_interface *intf)
1763 struct usbnet *dev = usb_get_intfdata(intf);
1766 ret = smsc95xx_reset(dev);
1770 return smsc95xx_resume(intf);
1773 static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
1775 skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
1776 skb->ip_summed = CHECKSUM_COMPLETE;
1777 skb_trim(skb, skb->len - 2);
1780 static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1782 /* This check is no longer done by usbnet */
1783 if (skb->len < dev->net->hard_header_len)
1786 while (skb->len > 0) {
1787 u32 header, align_count;
1788 struct sk_buff *ax_skb;
1789 unsigned char *packet;
1792 header = get_unaligned_le32(skb->data);
1793 skb_pull(skb, 4 + NET_IP_ALIGN);
1796 /* get the packet length */
1797 size = (u16)((header & RX_STS_FL_) >> 16);
1798 align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
1800 if (unlikely(header & RX_STS_ES_)) {
1801 netif_dbg(dev, rx_err, dev->net,
1802 "Error header=0x%08x\n", header);
1803 dev->net->stats.rx_errors++;
1804 dev->net->stats.rx_dropped++;
1806 if (header & RX_STS_CRC_) {
1807 dev->net->stats.rx_crc_errors++;
1809 if (header & (RX_STS_TL_ | RX_STS_RF_))
1810 dev->net->stats.rx_frame_errors++;
1812 if ((header & RX_STS_LE_) &&
1813 (!(header & RX_STS_FT_)))
1814 dev->net->stats.rx_length_errors++;
1817 /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1818 if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1819 netif_dbg(dev, rx_err, dev->net,
1820 "size err header=0x%08x\n", header);
1824 /* last frame in this batch */
1825 if (skb->len == size) {
1826 if (dev->net->features & NETIF_F_RXCSUM)
1827 smsc95xx_rx_csum_offload(skb);
1828 skb_trim(skb, skb->len - 4); /* remove fcs */
1829 skb->truesize = size + sizeof(struct sk_buff);
1834 ax_skb = skb_clone(skb, GFP_ATOMIC);
1835 if (unlikely(!ax_skb)) {
1836 netdev_warn(dev->net, "Error allocating skb\n");
1841 ax_skb->data = packet;
1842 skb_set_tail_pointer(ax_skb, size);
1844 if (dev->net->features & NETIF_F_RXCSUM)
1845 smsc95xx_rx_csum_offload(ax_skb);
1846 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
1847 ax_skb->truesize = size + sizeof(struct sk_buff);
1849 usbnet_skb_return(dev, ax_skb);
1852 skb_pull(skb, size);
1854 /* padding bytes before the next frame starts */
1856 skb_pull(skb, align_count);
1862 static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
1864 u16 low_16 = (u16)skb_checksum_start_offset(skb);
1865 u16 high_16 = low_16 + skb->csum_offset;
1866 return (high_16 << 16) | low_16;
1869 /* The TX CSUM won't work if the checksum lies in the last 4 bytes of the
1870 * transmission. This is fairly unlikely, only seems to trigger with some
1871 * short TCP ACK packets sent.
1873 * Note, this calculation should probably check for the alignment of the
1874 * data as well, but a straight check for csum being in the last four bytes
1875 * of the packet should be ok for now.
1877 static bool smsc95xx_can_tx_checksum(struct sk_buff *skb)
1879 unsigned int len = skb->len - skb_checksum_start_offset(skb);
1883 return skb->csum_offset < (len - (4 + 1));
1886 static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
1887 struct sk_buff *skb, gfp_t flags)
1889 bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
1890 int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
1891 u32 tx_cmd_a, tx_cmd_b;
1894 /* We do not advertise SG, so skbs should be already linearized */
1895 BUG_ON(skb_shinfo(skb)->nr_frags);
1897 /* Make writable and expand header space by overhead if required */
1898 if (skb_cow_head(skb, overhead)) {
1899 /* Must deallocate here as returning NULL to indicate error
1900 * means the skb won't be deallocated in the caller.
1902 dev_kfree_skb_any(skb);
1906 tx_cmd_b = (u32)skb->len;
1907 tx_cmd_a = tx_cmd_b | TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
1910 if (!smsc95xx_can_tx_checksum(skb)) {
1911 /* workaround - hardware tx checksum does not work
1912 * properly with extremely small packets */
1913 long csstart = skb_checksum_start_offset(skb);
1914 __wsum calc = csum_partial(skb->data + csstart,
1915 skb->len - csstart, 0);
1916 *((__sum16 *)(skb->data + csstart
1917 + skb->csum_offset)) = csum_fold(calc);
1921 u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
1922 ptr = skb_push(skb, 4);
1923 put_unaligned_le32(csum_preamble, ptr);
1927 tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
1931 ptr = skb_push(skb, 8);
1932 put_unaligned_le32(tx_cmd_a, ptr);
1933 put_unaligned_le32(tx_cmd_b, ptr+4);
1938 static int smsc95xx_manage_power(struct usbnet *dev, int on)
1940 struct smsc95xx_priv *pdata = dev->driver_priv;
1942 dev->intf->needs_remote_wakeup = on;
1944 if (pdata->features & FEATURE_REMOTE_WAKEUP)
1947 /* this chip revision isn't capable of remote wakeup */
1948 netdev_info(dev->net, "hardware isn't capable of remote wakeup\n");
1951 usb_autopm_get_interface_no_resume(dev->intf);
1953 usb_autopm_put_interface(dev->intf);
1958 static const struct driver_info smsc95xx_info = {
1959 .description = "smsc95xx USB 2.0 Ethernet",
1960 .bind = smsc95xx_bind,
1961 .unbind = smsc95xx_unbind,
1962 .link_reset = smsc95xx_link_reset,
1963 .reset = smsc95xx_start_phy,
1964 .stop = smsc95xx_disconnect_phy,
1965 .rx_fixup = smsc95xx_rx_fixup,
1966 .tx_fixup = smsc95xx_tx_fixup,
1967 .status = smsc95xx_status,
1968 .manage_power = smsc95xx_manage_power,
1969 .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
1972 static const struct usb_device_id products[] = {
1974 /* SMSC9500 USB Ethernet Device */
1975 USB_DEVICE(0x0424, 0x9500),
1976 .driver_info = (unsigned long) &smsc95xx_info,
1979 /* SMSC9505 USB Ethernet Device */
1980 USB_DEVICE(0x0424, 0x9505),
1981 .driver_info = (unsigned long) &smsc95xx_info,
1984 /* SMSC9500A USB Ethernet Device */
1985 USB_DEVICE(0x0424, 0x9E00),
1986 .driver_info = (unsigned long) &smsc95xx_info,
1989 /* SMSC9505A USB Ethernet Device */
1990 USB_DEVICE(0x0424, 0x9E01),
1991 .driver_info = (unsigned long) &smsc95xx_info,
1994 /* SMSC9512/9514 USB Hub & Ethernet Device */
1995 USB_DEVICE(0x0424, 0xec00),
1996 .driver_info = (unsigned long) &smsc95xx_info,
1999 /* SMSC9500 USB Ethernet Device (SAL10) */
2000 USB_DEVICE(0x0424, 0x9900),
2001 .driver_info = (unsigned long) &smsc95xx_info,
2004 /* SMSC9505 USB Ethernet Device (SAL10) */
2005 USB_DEVICE(0x0424, 0x9901),
2006 .driver_info = (unsigned long) &smsc95xx_info,
2009 /* SMSC9500A USB Ethernet Device (SAL10) */
2010 USB_DEVICE(0x0424, 0x9902),
2011 .driver_info = (unsigned long) &smsc95xx_info,
2014 /* SMSC9505A USB Ethernet Device (SAL10) */
2015 USB_DEVICE(0x0424, 0x9903),
2016 .driver_info = (unsigned long) &smsc95xx_info,
2019 /* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
2020 USB_DEVICE(0x0424, 0x9904),
2021 .driver_info = (unsigned long) &smsc95xx_info,
2024 /* SMSC9500A USB Ethernet Device (HAL) */
2025 USB_DEVICE(0x0424, 0x9905),
2026 .driver_info = (unsigned long) &smsc95xx_info,
2029 /* SMSC9505A USB Ethernet Device (HAL) */
2030 USB_DEVICE(0x0424, 0x9906),
2031 .driver_info = (unsigned long) &smsc95xx_info,
2034 /* SMSC9500 USB Ethernet Device (Alternate ID) */
2035 USB_DEVICE(0x0424, 0x9907),
2036 .driver_info = (unsigned long) &smsc95xx_info,
2039 /* SMSC9500A USB Ethernet Device (Alternate ID) */
2040 USB_DEVICE(0x0424, 0x9908),
2041 .driver_info = (unsigned long) &smsc95xx_info,
2044 /* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
2045 USB_DEVICE(0x0424, 0x9909),
2046 .driver_info = (unsigned long) &smsc95xx_info,
2049 /* SMSC LAN9530 USB Ethernet Device */
2050 USB_DEVICE(0x0424, 0x9530),
2051 .driver_info = (unsigned long) &smsc95xx_info,
2054 /* SMSC LAN9730 USB Ethernet Device */
2055 USB_DEVICE(0x0424, 0x9730),
2056 .driver_info = (unsigned long) &smsc95xx_info,
2059 /* SMSC LAN89530 USB Ethernet Device */
2060 USB_DEVICE(0x0424, 0x9E08),
2061 .driver_info = (unsigned long) &smsc95xx_info,
2065 MODULE_DEVICE_TABLE(usb, products);
2067 static struct usb_driver smsc95xx_driver = {
2069 .id_table = products,
2070 .probe = usbnet_probe,
2071 .suspend = smsc95xx_suspend,
2072 .resume = smsc95xx_resume,
2073 .reset_resume = smsc95xx_reset_resume,
2074 .disconnect = usbnet_disconnect,
2075 .disable_hub_initiated_lpm = 1,
2076 .supports_autosuspend = 1,
2079 module_usb_driver(smsc95xx_driver);
2081 MODULE_AUTHOR("Nancy Lin");
2082 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
2083 MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
2084 MODULE_LICENSE("GPL");