1 /***************************************************************************
3 * Copyright (C) 2007-2010 SMSC
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *****************************************************************************/
20 #include <linux/module.h>
21 #include <linux/kmod.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/ethtool.h>
25 #include <linux/mii.h>
26 #include <linux/usb.h>
27 #include <linux/bitrev.h>
28 #include <linux/crc16.h>
29 #include <linux/crc32.h>
30 #include <linux/usb/usbnet.h>
31 #include <linux/slab.h>
34 #define SMSC_CHIPNAME "smsc75xx"
35 #define SMSC_DRIVER_VERSION "1.0.0"
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 (9000)
42 #define LAN75XX_EEPROM_MAGIC (0x7500)
43 #define EEPROM_MAC_OFFSET (0x01)
44 #define DEFAULT_TX_CSUM_ENABLE (true)
45 #define DEFAULT_RX_CSUM_ENABLE (true)
46 #define SMSC75XX_INTERNAL_PHY_ID (1)
47 #define SMSC75XX_TX_OVERHEAD (8)
48 #define MAX_RX_FIFO_SIZE (20 * 1024)
49 #define MAX_TX_FIFO_SIZE (12 * 1024)
50 #define USB_VENDOR_ID_SMSC (0x0424)
51 #define USB_PRODUCT_ID_LAN7500 (0x7500)
52 #define USB_PRODUCT_ID_LAN7505 (0x7505)
54 #define SUPPORTED_WAKE (WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
55 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
57 #define SUSPEND_SUSPEND0 (0x01)
58 #define SUSPEND_SUSPEND1 (0x02)
59 #define SUSPEND_SUSPEND2 (0x04)
60 #define SUSPEND_SUSPEND3 (0x08)
61 #define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
62 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
64 struct smsc75xx_priv {
68 u32 multicast_hash_table[DP_SEL_VHF_HASH_LEN];
69 struct mutex dataport_mutex;
70 spinlock_t rfe_ctl_lock;
71 struct work_struct set_multicast;
76 struct usb_ctrlrequest req;
80 static bool turbo_mode = true;
81 module_param(turbo_mode, bool, 0644);
82 MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
84 static int __must_check __smsc75xx_read_reg(struct usbnet *dev, u32 index,
89 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
96 fn = usbnet_read_cmd_nopm;
98 ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
99 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
101 if (unlikely(ret < 0))
102 netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
111 static int __must_check __smsc75xx_write_reg(struct usbnet *dev, u32 index,
116 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
121 fn = usbnet_write_cmd;
123 fn = usbnet_write_cmd_nopm;
128 ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
129 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
131 if (unlikely(ret < 0))
132 netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
138 static int __must_check smsc75xx_read_reg_nopm(struct usbnet *dev, u32 index,
141 return __smsc75xx_read_reg(dev, index, data, 1);
144 static int __must_check smsc75xx_write_reg_nopm(struct usbnet *dev, u32 index,
147 return __smsc75xx_write_reg(dev, index, data, 1);
150 static int __must_check smsc75xx_read_reg(struct usbnet *dev, u32 index,
153 return __smsc75xx_read_reg(dev, index, data, 0);
156 static int __must_check smsc75xx_write_reg(struct usbnet *dev, u32 index,
159 return __smsc75xx_write_reg(dev, index, data, 0);
162 /* Loop until the read is completed with timeout
163 * called with phy_mutex held */
164 static __must_check int __smsc75xx_phy_wait_not_busy(struct usbnet *dev,
167 unsigned long start_time = jiffies;
172 ret = __smsc75xx_read_reg(dev, MII_ACCESS, &val, in_pm);
174 netdev_warn(dev->net, "Error reading MII_ACCESS\n");
178 if (!(val & MII_ACCESS_BUSY))
180 } while (!time_after(jiffies, start_time + HZ));
185 static int __smsc75xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
188 struct usbnet *dev = netdev_priv(netdev);
192 mutex_lock(&dev->phy_mutex);
194 /* confirm MII not busy */
195 ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
197 netdev_warn(dev->net, "MII is busy in smsc75xx_mdio_read\n");
201 /* set the address, index & direction (read from PHY) */
202 phy_id &= dev->mii.phy_id_mask;
203 idx &= dev->mii.reg_num_mask;
204 addr = ((phy_id << MII_ACCESS_PHY_ADDR_SHIFT) & MII_ACCESS_PHY_ADDR)
205 | ((idx << MII_ACCESS_REG_ADDR_SHIFT) & MII_ACCESS_REG_ADDR)
206 | MII_ACCESS_READ | MII_ACCESS_BUSY;
207 ret = __smsc75xx_write_reg(dev, MII_ACCESS, addr, in_pm);
209 netdev_warn(dev->net, "Error writing MII_ACCESS\n");
213 ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
215 netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
219 ret = __smsc75xx_read_reg(dev, MII_DATA, &val, in_pm);
221 netdev_warn(dev->net, "Error reading MII_DATA\n");
225 ret = (u16)(val & 0xFFFF);
228 mutex_unlock(&dev->phy_mutex);
232 static void __smsc75xx_mdio_write(struct net_device *netdev, int phy_id,
233 int idx, int regval, int in_pm)
235 struct usbnet *dev = netdev_priv(netdev);
239 mutex_lock(&dev->phy_mutex);
241 /* confirm MII not busy */
242 ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
244 netdev_warn(dev->net, "MII is busy in smsc75xx_mdio_write\n");
249 ret = __smsc75xx_write_reg(dev, MII_DATA, val, in_pm);
251 netdev_warn(dev->net, "Error writing MII_DATA\n");
255 /* set the address, index & direction (write to PHY) */
256 phy_id &= dev->mii.phy_id_mask;
257 idx &= dev->mii.reg_num_mask;
258 addr = ((phy_id << MII_ACCESS_PHY_ADDR_SHIFT) & MII_ACCESS_PHY_ADDR)
259 | ((idx << MII_ACCESS_REG_ADDR_SHIFT) & MII_ACCESS_REG_ADDR)
260 | MII_ACCESS_WRITE | MII_ACCESS_BUSY;
261 ret = __smsc75xx_write_reg(dev, MII_ACCESS, addr, in_pm);
263 netdev_warn(dev->net, "Error writing MII_ACCESS\n");
267 ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
269 netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
274 mutex_unlock(&dev->phy_mutex);
277 static int smsc75xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
280 return __smsc75xx_mdio_read(netdev, phy_id, idx, 1);
283 static void smsc75xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
286 __smsc75xx_mdio_write(netdev, phy_id, idx, regval, 1);
289 static int smsc75xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
291 return __smsc75xx_mdio_read(netdev, phy_id, idx, 0);
294 static void smsc75xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
297 __smsc75xx_mdio_write(netdev, phy_id, idx, regval, 0);
300 static int smsc75xx_wait_eeprom(struct usbnet *dev)
302 unsigned long start_time = jiffies;
307 ret = smsc75xx_read_reg(dev, E2P_CMD, &val);
309 netdev_warn(dev->net, "Error reading E2P_CMD\n");
313 if (!(val & E2P_CMD_BUSY) || (val & E2P_CMD_TIMEOUT))
316 } while (!time_after(jiffies, start_time + HZ));
318 if (val & (E2P_CMD_TIMEOUT | E2P_CMD_BUSY)) {
319 netdev_warn(dev->net, "EEPROM read operation timeout\n");
326 static int smsc75xx_eeprom_confirm_not_busy(struct usbnet *dev)
328 unsigned long start_time = jiffies;
333 ret = smsc75xx_read_reg(dev, E2P_CMD, &val);
335 netdev_warn(dev->net, "Error reading E2P_CMD\n");
339 if (!(val & E2P_CMD_BUSY))
343 } while (!time_after(jiffies, start_time + HZ));
345 netdev_warn(dev->net, "EEPROM is busy\n");
349 static int smsc75xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
358 ret = smsc75xx_eeprom_confirm_not_busy(dev);
362 for (i = 0; i < length; i++) {
363 val = E2P_CMD_BUSY | E2P_CMD_READ | (offset & E2P_CMD_ADDR);
364 ret = smsc75xx_write_reg(dev, E2P_CMD, val);
366 netdev_warn(dev->net, "Error writing E2P_CMD\n");
370 ret = smsc75xx_wait_eeprom(dev);
374 ret = smsc75xx_read_reg(dev, E2P_DATA, &val);
376 netdev_warn(dev->net, "Error reading E2P_DATA\n");
380 data[i] = val & 0xFF;
387 static int smsc75xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
396 ret = smsc75xx_eeprom_confirm_not_busy(dev);
400 /* Issue write/erase enable command */
401 val = E2P_CMD_BUSY | E2P_CMD_EWEN;
402 ret = smsc75xx_write_reg(dev, E2P_CMD, val);
404 netdev_warn(dev->net, "Error writing E2P_CMD\n");
408 ret = smsc75xx_wait_eeprom(dev);
412 for (i = 0; i < length; i++) {
414 /* Fill data register */
416 ret = smsc75xx_write_reg(dev, E2P_DATA, val);
418 netdev_warn(dev->net, "Error writing E2P_DATA\n");
422 /* Send "write" command */
423 val = E2P_CMD_BUSY | E2P_CMD_WRITE | (offset & E2P_CMD_ADDR);
424 ret = smsc75xx_write_reg(dev, E2P_CMD, val);
426 netdev_warn(dev->net, "Error writing E2P_CMD\n");
430 ret = smsc75xx_wait_eeprom(dev);
440 static int smsc75xx_dataport_wait_not_busy(struct usbnet *dev)
444 for (i = 0; i < 100; i++) {
446 ret = smsc75xx_read_reg(dev, DP_SEL, &dp_sel);
448 netdev_warn(dev->net, "Error reading DP_SEL\n");
452 if (dp_sel & DP_SEL_DPRDY)
458 netdev_warn(dev->net, "smsc75xx_dataport_wait_not_busy timed out\n");
463 static int smsc75xx_dataport_write(struct usbnet *dev, u32 ram_select, u32 addr,
464 u32 length, u32 *buf)
466 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
470 mutex_lock(&pdata->dataport_mutex);
472 ret = smsc75xx_dataport_wait_not_busy(dev);
474 netdev_warn(dev->net, "smsc75xx_dataport_write busy on entry\n");
478 ret = smsc75xx_read_reg(dev, DP_SEL, &dp_sel);
480 netdev_warn(dev->net, "Error reading DP_SEL\n");
484 dp_sel &= ~DP_SEL_RSEL;
485 dp_sel |= ram_select;
486 ret = smsc75xx_write_reg(dev, DP_SEL, dp_sel);
488 netdev_warn(dev->net, "Error writing DP_SEL\n");
492 for (i = 0; i < length; i++) {
493 ret = smsc75xx_write_reg(dev, DP_ADDR, addr + i);
495 netdev_warn(dev->net, "Error writing DP_ADDR\n");
499 ret = smsc75xx_write_reg(dev, DP_DATA, buf[i]);
501 netdev_warn(dev->net, "Error writing DP_DATA\n");
505 ret = smsc75xx_write_reg(dev, DP_CMD, DP_CMD_WRITE);
507 netdev_warn(dev->net, "Error writing DP_CMD\n");
511 ret = smsc75xx_dataport_wait_not_busy(dev);
513 netdev_warn(dev->net, "smsc75xx_dataport_write timeout\n");
519 mutex_unlock(&pdata->dataport_mutex);
523 /* returns hash bit number for given MAC address */
524 static u32 smsc75xx_hash(char addr[ETH_ALEN])
526 return (ether_crc(ETH_ALEN, addr) >> 23) & 0x1ff;
529 static void smsc75xx_deferred_multicast_write(struct work_struct *param)
531 struct smsc75xx_priv *pdata =
532 container_of(param, struct smsc75xx_priv, set_multicast);
533 struct usbnet *dev = pdata->dev;
536 netif_dbg(dev, drv, dev->net, "deferred multicast write 0x%08x\n",
539 smsc75xx_dataport_write(dev, DP_SEL_VHF, DP_SEL_VHF_VLAN_LEN,
540 DP_SEL_VHF_HASH_LEN, pdata->multicast_hash_table);
542 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
544 netdev_warn(dev->net, "Error writing RFE_CRL\n");
547 static void smsc75xx_set_multicast(struct net_device *netdev)
549 struct usbnet *dev = netdev_priv(netdev);
550 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
554 spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
557 ~(RFE_CTL_AU | RFE_CTL_AM | RFE_CTL_DPF | RFE_CTL_MHF);
558 pdata->rfe_ctl |= RFE_CTL_AB;
560 for (i = 0; i < DP_SEL_VHF_HASH_LEN; i++)
561 pdata->multicast_hash_table[i] = 0;
563 if (dev->net->flags & IFF_PROMISC) {
564 netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
565 pdata->rfe_ctl |= RFE_CTL_AM | RFE_CTL_AU;
566 } else if (dev->net->flags & IFF_ALLMULTI) {
567 netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
568 pdata->rfe_ctl |= RFE_CTL_AM | RFE_CTL_DPF;
569 } else if (!netdev_mc_empty(dev->net)) {
570 struct netdev_hw_addr *ha;
572 netif_dbg(dev, drv, dev->net, "receive multicast hash filter\n");
574 pdata->rfe_ctl |= RFE_CTL_MHF | RFE_CTL_DPF;
576 netdev_for_each_mc_addr(ha, netdev) {
577 u32 bitnum = smsc75xx_hash(ha->addr);
578 pdata->multicast_hash_table[bitnum / 32] |=
579 (1 << (bitnum % 32));
582 netif_dbg(dev, drv, dev->net, "receive own packets only\n");
583 pdata->rfe_ctl |= RFE_CTL_DPF;
586 spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
588 /* defer register writes to a sleepable context */
589 schedule_work(&pdata->set_multicast);
592 static int smsc75xx_update_flowcontrol(struct usbnet *dev, u8 duplex,
593 u16 lcladv, u16 rmtadv)
595 u32 flow = 0, fct_flow = 0;
598 if (duplex == DUPLEX_FULL) {
599 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
601 if (cap & FLOW_CTRL_TX) {
602 flow = (FLOW_TX_FCEN | 0xFFFF);
603 /* set fct_flow thresholds to 20% and 80% */
604 fct_flow = (8 << 8) | 32;
607 if (cap & FLOW_CTRL_RX)
608 flow |= FLOW_RX_FCEN;
610 netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
611 (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
612 (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
614 netif_dbg(dev, link, dev->net, "half duplex\n");
617 ret = smsc75xx_write_reg(dev, FLOW, flow);
619 netdev_warn(dev->net, "Error writing FLOW\n");
623 ret = smsc75xx_write_reg(dev, FCT_FLOW, fct_flow);
625 netdev_warn(dev->net, "Error writing FCT_FLOW\n");
632 static int smsc75xx_link_reset(struct usbnet *dev)
634 struct mii_if_info *mii = &dev->mii;
635 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
639 /* write to clear phy interrupt status */
640 smsc75xx_mdio_write(dev->net, mii->phy_id, PHY_INT_SRC,
641 PHY_INT_SRC_CLEAR_ALL);
643 ret = smsc75xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL);
645 netdev_warn(dev->net, "Error writing INT_STS\n");
649 mii_check_media(mii, 1, 1);
650 mii_ethtool_gset(&dev->mii, &ecmd);
651 lcladv = smsc75xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
652 rmtadv = smsc75xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
654 netif_dbg(dev, link, dev->net, "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
655 ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
657 return smsc75xx_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
660 static void smsc75xx_status(struct usbnet *dev, struct urb *urb)
664 if (urb->actual_length != 4) {
665 netdev_warn(dev->net, "unexpected urb length %d\n",
670 memcpy(&intdata, urb->transfer_buffer, 4);
671 le32_to_cpus(&intdata);
673 netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
675 if (intdata & INT_ENP_PHY_INT)
676 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
678 netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
682 static int smsc75xx_ethtool_get_eeprom_len(struct net_device *net)
684 return MAX_EEPROM_SIZE;
687 static int smsc75xx_ethtool_get_eeprom(struct net_device *netdev,
688 struct ethtool_eeprom *ee, u8 *data)
690 struct usbnet *dev = netdev_priv(netdev);
692 ee->magic = LAN75XX_EEPROM_MAGIC;
694 return smsc75xx_read_eeprom(dev, ee->offset, ee->len, data);
697 static int smsc75xx_ethtool_set_eeprom(struct net_device *netdev,
698 struct ethtool_eeprom *ee, u8 *data)
700 struct usbnet *dev = netdev_priv(netdev);
702 if (ee->magic != LAN75XX_EEPROM_MAGIC) {
703 netdev_warn(dev->net, "EEPROM: magic value mismatch: 0x%x\n",
708 return smsc75xx_write_eeprom(dev, ee->offset, ee->len, data);
711 static void smsc75xx_ethtool_get_wol(struct net_device *net,
712 struct ethtool_wolinfo *wolinfo)
714 struct usbnet *dev = netdev_priv(net);
715 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
717 wolinfo->supported = SUPPORTED_WAKE;
718 wolinfo->wolopts = pdata->wolopts;
721 static int smsc75xx_ethtool_set_wol(struct net_device *net,
722 struct ethtool_wolinfo *wolinfo)
724 struct usbnet *dev = netdev_priv(net);
725 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
728 pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
730 ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
732 netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
737 static const struct ethtool_ops smsc75xx_ethtool_ops = {
738 .get_link = usbnet_get_link,
739 .nway_reset = usbnet_nway_reset,
740 .get_drvinfo = usbnet_get_drvinfo,
741 .get_msglevel = usbnet_get_msglevel,
742 .set_msglevel = usbnet_set_msglevel,
743 .get_settings = usbnet_get_settings,
744 .set_settings = usbnet_set_settings,
745 .get_eeprom_len = smsc75xx_ethtool_get_eeprom_len,
746 .get_eeprom = smsc75xx_ethtool_get_eeprom,
747 .set_eeprom = smsc75xx_ethtool_set_eeprom,
748 .get_wol = smsc75xx_ethtool_get_wol,
749 .set_wol = smsc75xx_ethtool_set_wol,
752 static int smsc75xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
754 struct usbnet *dev = netdev_priv(netdev);
756 if (!netif_running(netdev))
759 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
762 static void smsc75xx_init_mac_address(struct usbnet *dev)
764 /* try reading mac address from EEPROM */
765 if (smsc75xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
766 dev->net->dev_addr) == 0) {
767 if (is_valid_ether_addr(dev->net->dev_addr)) {
768 /* eeprom values are valid so use them */
769 netif_dbg(dev, ifup, dev->net,
770 "MAC address read from EEPROM\n");
775 /* no eeprom, or eeprom values are invalid. generate random MAC */
776 eth_hw_addr_random(dev->net);
777 netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
780 static int smsc75xx_set_mac_address(struct usbnet *dev)
782 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
783 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
784 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
786 int ret = smsc75xx_write_reg(dev, RX_ADDRH, addr_hi);
788 netdev_warn(dev->net, "Failed to write RX_ADDRH: %d\n", ret);
792 ret = smsc75xx_write_reg(dev, RX_ADDRL, addr_lo);
794 netdev_warn(dev->net, "Failed to write RX_ADDRL: %d\n", ret);
798 addr_hi |= ADDR_FILTX_FB_VALID;
799 ret = smsc75xx_write_reg(dev, ADDR_FILTX, addr_hi);
801 netdev_warn(dev->net, "Failed to write ADDR_FILTX: %d\n", ret);
805 ret = smsc75xx_write_reg(dev, ADDR_FILTX + 4, addr_lo);
807 netdev_warn(dev->net, "Failed to write ADDR_FILTX+4: %d\n", ret);
812 static int smsc75xx_phy_initialize(struct usbnet *dev)
814 int bmcr, ret, timeout = 0;
816 /* Initialize MII structure */
817 dev->mii.dev = dev->net;
818 dev->mii.mdio_read = smsc75xx_mdio_read;
819 dev->mii.mdio_write = smsc75xx_mdio_write;
820 dev->mii.phy_id_mask = 0x1f;
821 dev->mii.reg_num_mask = 0x1f;
822 dev->mii.supports_gmii = 1;
823 dev->mii.phy_id = SMSC75XX_INTERNAL_PHY_ID;
825 /* reset phy and wait for reset to complete */
826 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
830 bmcr = smsc75xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
832 netdev_warn(dev->net, "Error reading MII_BMCR\n");
836 } while ((bmcr & BMCR_RESET) && (timeout < 100));
838 if (timeout >= 100) {
839 netdev_warn(dev->net, "timeout on PHY Reset\n");
843 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
844 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
845 ADVERTISE_PAUSE_ASYM);
846 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
849 /* read and write to clear phy interrupt status */
850 ret = smsc75xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
852 netdev_warn(dev->net, "Error reading PHY_INT_SRC\n");
856 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_SRC, 0xffff);
858 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
859 PHY_INT_MASK_DEFAULT);
860 mii_nway_restart(&dev->mii);
862 netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
866 static int smsc75xx_set_rx_max_frame_length(struct usbnet *dev, int size)
872 ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
874 netdev_warn(dev->net, "Failed to read MAC_RX: %d\n", ret);
878 rxenabled = ((buf & MAC_RX_RXEN) != 0);
882 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
884 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
889 /* add 4 to size for FCS */
890 buf &= ~MAC_RX_MAX_SIZE;
891 buf |= (((size + 4) << MAC_RX_MAX_SIZE_SHIFT) & MAC_RX_MAX_SIZE);
893 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
895 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
901 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
903 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
911 static int smsc75xx_change_mtu(struct net_device *netdev, int new_mtu)
913 struct usbnet *dev = netdev_priv(netdev);
916 if (new_mtu > MAX_SINGLE_PACKET_SIZE)
919 ret = smsc75xx_set_rx_max_frame_length(dev, new_mtu + ETH_HLEN);
921 netdev_warn(dev->net, "Failed to set mac rx frame length\n");
925 return usbnet_change_mtu(netdev, new_mtu);
928 /* Enable or disable Rx checksum offload engine */
929 static int smsc75xx_set_features(struct net_device *netdev,
930 netdev_features_t features)
932 struct usbnet *dev = netdev_priv(netdev);
933 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
937 spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
939 if (features & NETIF_F_RXCSUM)
940 pdata->rfe_ctl |= RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM;
942 pdata->rfe_ctl &= ~(RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM);
944 spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
945 /* it's racing here! */
947 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
949 netdev_warn(dev->net, "Error writing RFE_CTL\n");
954 static int smsc75xx_wait_ready(struct usbnet *dev, int in_pm)
962 ret = __smsc75xx_read_reg(dev, PMT_CTL, &buf, in_pm);
965 netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
969 if (buf & PMT_CTL_DEV_RDY)
974 } while (timeout < 100);
976 netdev_warn(dev->net, "timeout waiting for device ready\n");
980 static int smsc75xx_reset(struct usbnet *dev)
982 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
984 int ret = 0, timeout;
986 netif_dbg(dev, ifup, dev->net, "entering smsc75xx_reset\n");
988 ret = smsc75xx_wait_ready(dev, 0);
990 netdev_warn(dev->net, "device not ready in smsc75xx_reset\n");
994 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
996 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1002 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
1004 netdev_warn(dev->net, "Failed to write HW_CFG: %d\n", ret);
1011 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1013 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1017 } while ((buf & HW_CFG_LRST) && (timeout < 100));
1019 if (timeout >= 100) {
1020 netdev_warn(dev->net, "timeout on completion of Lite Reset\n");
1024 netif_dbg(dev, ifup, dev->net, "Lite reset complete, resetting PHY\n");
1026 ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
1028 netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
1032 buf |= PMT_CTL_PHY_RST;
1034 ret = smsc75xx_write_reg(dev, PMT_CTL, buf);
1036 netdev_warn(dev->net, "Failed to write PMT_CTL: %d\n", ret);
1043 ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
1045 netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
1049 } while ((buf & PMT_CTL_PHY_RST) && (timeout < 100));
1051 if (timeout >= 100) {
1052 netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
1056 netif_dbg(dev, ifup, dev->net, "PHY reset complete\n");
1058 ret = smsc75xx_set_mac_address(dev);
1060 netdev_warn(dev->net, "Failed to set mac address\n");
1064 netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
1065 dev->net->dev_addr);
1067 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1069 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1073 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
1078 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
1080 netdev_warn(dev->net, "Failed to write HW_CFG: %d\n", ret);
1084 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1086 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1090 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG after writing HW_CFG_BIR: 0x%08x\n",
1095 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
1096 } else if (dev->udev->speed == USB_SPEED_HIGH) {
1097 buf = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
1098 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
1100 buf = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
1101 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
1104 netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
1105 (ulong)dev->rx_urb_size);
1107 ret = smsc75xx_write_reg(dev, BURST_CAP, buf);
1109 netdev_warn(dev->net, "Failed to write BURST_CAP: %d\n", ret);
1113 ret = smsc75xx_read_reg(dev, BURST_CAP, &buf);
1115 netdev_warn(dev->net, "Failed to read BURST_CAP: %d\n", ret);
1119 netif_dbg(dev, ifup, dev->net,
1120 "Read Value from BURST_CAP after writing: 0x%08x\n", buf);
1122 ret = smsc75xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
1124 netdev_warn(dev->net, "Failed to write BULK_IN_DLY: %d\n", ret);
1128 ret = smsc75xx_read_reg(dev, BULK_IN_DLY, &buf);
1130 netdev_warn(dev->net, "Failed to read BULK_IN_DLY: %d\n", ret);
1134 netif_dbg(dev, ifup, dev->net,
1135 "Read Value from BULK_IN_DLY after writing: 0x%08x\n", buf);
1138 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1140 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1144 netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x\n", buf);
1146 buf |= (HW_CFG_MEF | HW_CFG_BCE);
1148 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
1150 netdev_warn(dev->net, "Failed to write HW_CFG: %d\n", ret);
1154 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1156 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1160 netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x\n", buf);
1163 /* set FIFO sizes */
1164 buf = (MAX_RX_FIFO_SIZE - 512) / 512;
1165 ret = smsc75xx_write_reg(dev, FCT_RX_FIFO_END, buf);
1167 netdev_warn(dev->net, "Failed to write FCT_RX_FIFO_END: %d\n", ret);
1171 netif_dbg(dev, ifup, dev->net, "FCT_RX_FIFO_END set to 0x%08x\n", buf);
1173 buf = (MAX_TX_FIFO_SIZE - 512) / 512;
1174 ret = smsc75xx_write_reg(dev, FCT_TX_FIFO_END, buf);
1176 netdev_warn(dev->net, "Failed to write FCT_TX_FIFO_END: %d\n", ret);
1180 netif_dbg(dev, ifup, dev->net, "FCT_TX_FIFO_END set to 0x%08x\n", buf);
1182 ret = smsc75xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL);
1184 netdev_warn(dev->net, "Failed to write INT_STS: %d\n", ret);
1188 ret = smsc75xx_read_reg(dev, ID_REV, &buf);
1190 netdev_warn(dev->net, "Failed to read ID_REV: %d\n", ret);
1194 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", buf);
1196 ret = smsc75xx_read_reg(dev, E2P_CMD, &buf);
1198 netdev_warn(dev->net, "Failed to read E2P_CMD: %d\n", ret);
1202 /* only set default GPIO/LED settings if no EEPROM is detected */
1203 if (!(buf & E2P_CMD_LOADED)) {
1204 ret = smsc75xx_read_reg(dev, LED_GPIO_CFG, &buf);
1206 netdev_warn(dev->net, "Failed to read LED_GPIO_CFG: %d\n", ret);
1210 buf &= ~(LED_GPIO_CFG_LED2_FUN_SEL | LED_GPIO_CFG_LED10_FUN_SEL);
1211 buf |= LED_GPIO_CFG_LEDGPIO_EN | LED_GPIO_CFG_LED2_FUN_SEL;
1213 ret = smsc75xx_write_reg(dev, LED_GPIO_CFG, buf);
1215 netdev_warn(dev->net, "Failed to write LED_GPIO_CFG: %d\n", ret);
1220 ret = smsc75xx_write_reg(dev, FLOW, 0);
1222 netdev_warn(dev->net, "Failed to write FLOW: %d\n", ret);
1226 ret = smsc75xx_write_reg(dev, FCT_FLOW, 0);
1228 netdev_warn(dev->net, "Failed to write FCT_FLOW: %d\n", ret);
1232 /* Don't need rfe_ctl_lock during initialisation */
1233 ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
1235 netdev_warn(dev->net, "Failed to read RFE_CTL: %d\n", ret);
1239 pdata->rfe_ctl |= RFE_CTL_AB | RFE_CTL_DPF;
1241 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
1243 netdev_warn(dev->net, "Failed to write RFE_CTL: %d\n", ret);
1247 ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
1249 netdev_warn(dev->net, "Failed to read RFE_CTL: %d\n", ret);
1253 netif_dbg(dev, ifup, dev->net, "RFE_CTL set to 0x%08x\n",
1256 /* Enable or disable checksum offload engines */
1257 smsc75xx_set_features(dev->net, dev->net->features);
1259 smsc75xx_set_multicast(dev->net);
1261 ret = smsc75xx_phy_initialize(dev);
1263 netdev_warn(dev->net, "Failed to initialize PHY: %d\n", ret);
1267 ret = smsc75xx_read_reg(dev, INT_EP_CTL, &buf);
1269 netdev_warn(dev->net, "Failed to read INT_EP_CTL: %d\n", ret);
1273 /* enable PHY interrupts */
1274 buf |= INT_ENP_PHY_INT;
1276 ret = smsc75xx_write_reg(dev, INT_EP_CTL, buf);
1278 netdev_warn(dev->net, "Failed to write INT_EP_CTL: %d\n", ret);
1282 /* allow mac to detect speed and duplex from phy */
1283 ret = smsc75xx_read_reg(dev, MAC_CR, &buf);
1285 netdev_warn(dev->net, "Failed to read MAC_CR: %d\n", ret);
1289 buf |= (MAC_CR_ADD | MAC_CR_ASD);
1290 ret = smsc75xx_write_reg(dev, MAC_CR, buf);
1292 netdev_warn(dev->net, "Failed to write MAC_CR: %d\n", ret);
1296 ret = smsc75xx_read_reg(dev, MAC_TX, &buf);
1298 netdev_warn(dev->net, "Failed to read MAC_TX: %d\n", ret);
1304 ret = smsc75xx_write_reg(dev, MAC_TX, buf);
1306 netdev_warn(dev->net, "Failed to write MAC_TX: %d\n", ret);
1310 netif_dbg(dev, ifup, dev->net, "MAC_TX set to 0x%08x\n", buf);
1312 ret = smsc75xx_read_reg(dev, FCT_TX_CTL, &buf);
1314 netdev_warn(dev->net, "Failed to read FCT_TX_CTL: %d\n", ret);
1318 buf |= FCT_TX_CTL_EN;
1320 ret = smsc75xx_write_reg(dev, FCT_TX_CTL, buf);
1322 netdev_warn(dev->net, "Failed to write FCT_TX_CTL: %d\n", ret);
1326 netif_dbg(dev, ifup, dev->net, "FCT_TX_CTL set to 0x%08x\n", buf);
1328 ret = smsc75xx_set_rx_max_frame_length(dev, dev->net->mtu + ETH_HLEN);
1330 netdev_warn(dev->net, "Failed to set max rx frame length\n");
1334 ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
1336 netdev_warn(dev->net, "Failed to read MAC_RX: %d\n", ret);
1342 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
1344 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
1348 netif_dbg(dev, ifup, dev->net, "MAC_RX set to 0x%08x\n", buf);
1350 ret = smsc75xx_read_reg(dev, FCT_RX_CTL, &buf);
1352 netdev_warn(dev->net, "Failed to read FCT_RX_CTL: %d\n", ret);
1356 buf |= FCT_RX_CTL_EN;
1358 ret = smsc75xx_write_reg(dev, FCT_RX_CTL, buf);
1360 netdev_warn(dev->net, "Failed to write FCT_RX_CTL: %d\n", ret);
1364 netif_dbg(dev, ifup, dev->net, "FCT_RX_CTL set to 0x%08x\n", buf);
1366 netif_dbg(dev, ifup, dev->net, "smsc75xx_reset, return 0\n");
1370 static const struct net_device_ops smsc75xx_netdev_ops = {
1371 .ndo_open = usbnet_open,
1372 .ndo_stop = usbnet_stop,
1373 .ndo_start_xmit = usbnet_start_xmit,
1374 .ndo_tx_timeout = usbnet_tx_timeout,
1375 .ndo_change_mtu = smsc75xx_change_mtu,
1376 .ndo_set_mac_address = eth_mac_addr,
1377 .ndo_validate_addr = eth_validate_addr,
1378 .ndo_do_ioctl = smsc75xx_ioctl,
1379 .ndo_set_rx_mode = smsc75xx_set_multicast,
1380 .ndo_set_features = smsc75xx_set_features,
1383 static int smsc75xx_bind(struct usbnet *dev, struct usb_interface *intf)
1385 struct smsc75xx_priv *pdata = NULL;
1388 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1390 ret = usbnet_get_endpoints(dev, intf);
1392 netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1396 dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc75xx_priv),
1399 pdata = (struct smsc75xx_priv *)(dev->data[0]);
1405 spin_lock_init(&pdata->rfe_ctl_lock);
1406 mutex_init(&pdata->dataport_mutex);
1408 INIT_WORK(&pdata->set_multicast, smsc75xx_deferred_multicast_write);
1410 if (DEFAULT_TX_CSUM_ENABLE)
1411 dev->net->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1413 if (DEFAULT_RX_CSUM_ENABLE)
1414 dev->net->features |= NETIF_F_RXCSUM;
1416 dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1419 ret = smsc75xx_wait_ready(dev, 0);
1421 netdev_warn(dev->net, "device not ready in smsc75xx_bind\n");
1425 smsc75xx_init_mac_address(dev);
1427 /* Init all registers */
1428 ret = smsc75xx_reset(dev);
1430 netdev_warn(dev->net, "smsc75xx_reset error %d\n", ret);
1434 dev->net->netdev_ops = &smsc75xx_netdev_ops;
1435 dev->net->ethtool_ops = &smsc75xx_ethtool_ops;
1436 dev->net->flags |= IFF_MULTICAST;
1437 dev->net->hard_header_len += SMSC75XX_TX_OVERHEAD;
1438 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1442 static void smsc75xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1444 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1446 netif_dbg(dev, ifdown, dev->net, "free pdata\n");
1453 static u16 smsc_crc(const u8 *buffer, size_t len)
1455 return bitrev16(crc16(0xFFFF, buffer, len));
1458 static int smsc75xx_write_wuff(struct usbnet *dev, int filter, u32 wuf_cfg,
1461 int cfg_base = WUF_CFGX + filter * 4;
1462 int mask_base = WUF_MASKX + filter * 16;
1465 ret = smsc75xx_write_reg(dev, cfg_base, wuf_cfg);
1467 netdev_warn(dev->net, "Error writing WUF_CFGX\n");
1471 ret = smsc75xx_write_reg(dev, mask_base, wuf_mask1);
1473 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1477 ret = smsc75xx_write_reg(dev, mask_base + 4, 0);
1479 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1483 ret = smsc75xx_write_reg(dev, mask_base + 8, 0);
1485 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1489 ret = smsc75xx_write_reg(dev, mask_base + 12, 0);
1491 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1498 static int smsc75xx_enter_suspend0(struct usbnet *dev)
1500 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1504 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1506 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1510 val &= (~(PMT_CTL_SUS_MODE | PMT_CTL_PHY_RST));
1511 val |= PMT_CTL_SUS_MODE_0 | PMT_CTL_WOL_EN | PMT_CTL_WUPS;
1513 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1515 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1519 pdata->suspend_flags |= SUSPEND_SUSPEND0;
1524 static int smsc75xx_enter_suspend1(struct usbnet *dev)
1526 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1530 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1532 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1536 val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1537 val |= PMT_CTL_SUS_MODE_1;
1539 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1541 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1545 /* clear wol status, enable energy detection */
1546 val &= ~PMT_CTL_WUPS;
1547 val |= (PMT_CTL_WUPS_ED | PMT_CTL_ED_EN);
1549 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1551 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1555 pdata->suspend_flags |= SUSPEND_SUSPEND1;
1560 static int smsc75xx_enter_suspend2(struct usbnet *dev)
1562 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1566 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1568 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1572 val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1573 val |= PMT_CTL_SUS_MODE_2;
1575 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1577 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1581 pdata->suspend_flags |= SUSPEND_SUSPEND2;
1586 static int smsc75xx_enter_suspend3(struct usbnet *dev)
1588 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1592 ret = smsc75xx_read_reg_nopm(dev, FCT_RX_CTL, &val);
1594 netdev_warn(dev->net, "Error reading FCT_RX_CTL\n");
1598 if (val & FCT_RX_CTL_RXUSED) {
1599 netdev_dbg(dev->net, "rx fifo not empty in autosuspend\n");
1603 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1605 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1609 val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1610 val |= PMT_CTL_SUS_MODE_3 | PMT_CTL_RES_CLR_WKP_EN;
1612 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1614 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1618 /* clear wol status */
1619 val &= ~PMT_CTL_WUPS;
1620 val |= PMT_CTL_WUPS_WOL;
1622 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1624 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1628 pdata->suspend_flags |= SUSPEND_SUSPEND3;
1633 static int smsc75xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1635 struct mii_if_info *mii = &dev->mii;
1638 netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1641 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
1643 netdev_warn(dev->net, "Error reading PHY_INT_SRC\n");
1647 /* enable interrupt source */
1648 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
1650 netdev_warn(dev->net, "Error reading PHY_INT_MASK\n");
1656 smsc75xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1661 static int smsc75xx_link_ok_nopm(struct usbnet *dev)
1663 struct mii_if_info *mii = &dev->mii;
1666 /* first, a dummy read, needed to latch some MII phys */
1667 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1669 netdev_warn(dev->net, "Error reading MII_BMSR\n");
1673 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1675 netdev_warn(dev->net, "Error reading MII_BMSR\n");
1679 return !!(ret & BMSR_LSTATUS);
1682 static int smsc75xx_autosuspend(struct usbnet *dev, u32 link_up)
1686 if (!netif_running(dev->net)) {
1687 /* interface is ifconfig down so fully power down hw */
1688 netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1689 return smsc75xx_enter_suspend2(dev);
1693 /* link is down so enter EDPD mode */
1694 netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1696 /* enable PHY wakeup events for if cable is attached */
1697 ret = smsc75xx_enable_phy_wakeup_interrupts(dev,
1698 PHY_INT_MASK_ANEG_COMP);
1700 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1704 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1705 return smsc75xx_enter_suspend1(dev);
1708 /* enable PHY wakeup events so we remote wakeup if cable is pulled */
1709 ret = smsc75xx_enable_phy_wakeup_interrupts(dev,
1710 PHY_INT_MASK_LINK_DOWN);
1712 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1716 netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1717 return smsc75xx_enter_suspend3(dev);
1720 static int smsc75xx_suspend(struct usb_interface *intf, pm_message_t message)
1722 struct usbnet *dev = usb_get_intfdata(intf);
1723 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1727 ret = usbnet_suspend(intf, message);
1729 netdev_warn(dev->net, "usbnet_suspend error\n");
1733 if (pdata->suspend_flags) {
1734 netdev_warn(dev->net, "error during last resume\n");
1735 pdata->suspend_flags = 0;
1738 /* determine if link is up using only _nopm functions */
1739 link_up = smsc75xx_link_ok_nopm(dev);
1741 if (message.event == PM_EVENT_AUTO_SUSPEND) {
1742 ret = smsc75xx_autosuspend(dev, link_up);
1746 /* if we get this far we're not autosuspending */
1747 /* if no wol options set, or if link is down and we're not waking on
1748 * PHY activity, enter lowest power SUSPEND2 mode
1750 if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1751 !(link_up || (pdata->wolopts & WAKE_PHY))) {
1752 netdev_info(dev->net, "entering SUSPEND2 mode\n");
1754 /* disable energy detect (link up) & wake up events */
1755 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1757 netdev_warn(dev->net, "Error reading WUCSR\n");
1761 val &= ~(WUCSR_MPEN | WUCSR_WUEN);
1763 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1765 netdev_warn(dev->net, "Error writing WUCSR\n");
1769 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1771 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1775 val &= ~(PMT_CTL_ED_EN | PMT_CTL_WOL_EN);
1777 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1779 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1783 ret = smsc75xx_enter_suspend2(dev);
1787 if (pdata->wolopts & WAKE_PHY) {
1788 ret = smsc75xx_enable_phy_wakeup_interrupts(dev,
1789 (PHY_INT_MASK_ANEG_COMP | PHY_INT_MASK_LINK_DOWN));
1791 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1795 /* if link is down then configure EDPD and enter SUSPEND1,
1796 * otherwise enter SUSPEND0 below
1799 struct mii_if_info *mii = &dev->mii;
1800 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1802 /* enable energy detect power-down mode */
1803 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id,
1806 netdev_warn(dev->net, "Error reading PHY_MODE_CTRL_STS\n");
1810 ret |= MODE_CTRL_STS_EDPWRDOWN;
1812 smsc75xx_mdio_write_nopm(dev->net, mii->phy_id,
1813 PHY_MODE_CTRL_STS, ret);
1815 /* enter SUSPEND1 mode */
1816 ret = smsc75xx_enter_suspend1(dev);
1821 if (pdata->wolopts & (WAKE_MCAST | WAKE_ARP)) {
1824 /* disable all filters */
1825 for (i = 0; i < WUF_NUM; i++) {
1826 ret = smsc75xx_write_reg_nopm(dev, WUF_CFGX + i * 4, 0);
1828 netdev_warn(dev->net, "Error writing WUF_CFGX\n");
1833 if (pdata->wolopts & WAKE_MCAST) {
1834 const u8 mcast[] = {0x01, 0x00, 0x5E};
1835 netdev_info(dev->net, "enabling multicast detection\n");
1837 val = WUF_CFGX_EN | WUF_CFGX_ATYPE_MULTICAST
1838 | smsc_crc(mcast, 3);
1839 ret = smsc75xx_write_wuff(dev, filter++, val, 0x0007);
1841 netdev_warn(dev->net, "Error writing wakeup filter\n");
1846 if (pdata->wolopts & WAKE_ARP) {
1847 const u8 arp[] = {0x08, 0x06};
1848 netdev_info(dev->net, "enabling ARP detection\n");
1850 val = WUF_CFGX_EN | WUF_CFGX_ATYPE_ALL | (0x0C << 16)
1852 ret = smsc75xx_write_wuff(dev, filter++, val, 0x0003);
1854 netdev_warn(dev->net, "Error writing wakeup filter\n");
1859 /* clear any pending pattern match packet status */
1860 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1862 netdev_warn(dev->net, "Error reading WUCSR\n");
1868 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1870 netdev_warn(dev->net, "Error writing WUCSR\n");
1874 netdev_info(dev->net, "enabling packet match detection\n");
1875 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1877 netdev_warn(dev->net, "Error reading WUCSR\n");
1883 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1885 netdev_warn(dev->net, "Error writing WUCSR\n");
1889 netdev_info(dev->net, "disabling packet match detection\n");
1890 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1892 netdev_warn(dev->net, "Error reading WUCSR\n");
1898 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1900 netdev_warn(dev->net, "Error writing WUCSR\n");
1905 /* disable magic, bcast & unicast wakeup sources */
1906 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1908 netdev_warn(dev->net, "Error reading WUCSR\n");
1912 val &= ~(WUCSR_MPEN | WUCSR_BCST_EN | WUCSR_PFDA_EN);
1914 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1916 netdev_warn(dev->net, "Error writing WUCSR\n");
1920 if (pdata->wolopts & WAKE_PHY) {
1921 netdev_info(dev->net, "enabling PHY wakeup\n");
1923 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1925 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1929 /* clear wol status, enable energy detection */
1930 val &= ~PMT_CTL_WUPS;
1931 val |= (PMT_CTL_WUPS_ED | PMT_CTL_ED_EN);
1933 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1935 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1940 if (pdata->wolopts & WAKE_MAGIC) {
1941 netdev_info(dev->net, "enabling magic packet wakeup\n");
1942 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1944 netdev_warn(dev->net, "Error reading WUCSR\n");
1948 /* clear any pending magic packet status */
1949 val |= WUCSR_MPR | WUCSR_MPEN;
1951 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1953 netdev_warn(dev->net, "Error writing WUCSR\n");
1958 if (pdata->wolopts & WAKE_BCAST) {
1959 netdev_info(dev->net, "enabling broadcast detection\n");
1960 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1962 netdev_warn(dev->net, "Error reading WUCSR\n");
1966 val |= WUCSR_BCAST_FR | WUCSR_BCST_EN;
1968 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1970 netdev_warn(dev->net, "Error writing WUCSR\n");
1975 if (pdata->wolopts & WAKE_UCAST) {
1976 netdev_info(dev->net, "enabling unicast detection\n");
1977 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1979 netdev_warn(dev->net, "Error reading WUCSR\n");
1983 val |= WUCSR_WUFR | WUCSR_PFDA_EN;
1985 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1987 netdev_warn(dev->net, "Error writing WUCSR\n");
1992 /* enable receiver to enable frame reception */
1993 ret = smsc75xx_read_reg_nopm(dev, MAC_RX, &val);
1995 netdev_warn(dev->net, "Failed to read MAC_RX: %d\n", ret);
2001 ret = smsc75xx_write_reg_nopm(dev, MAC_RX, val);
2003 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
2007 /* some wol options are enabled, so enter SUSPEND0 */
2008 netdev_info(dev->net, "entering SUSPEND0 mode\n");
2009 ret = smsc75xx_enter_suspend0(dev);
2013 * TODO: resume() might need to handle the suspend failure
2016 if (ret && PMSG_IS_AUTO(message))
2017 usbnet_resume(intf);
2021 static int smsc75xx_resume(struct usb_interface *intf)
2023 struct usbnet *dev = usb_get_intfdata(intf);
2024 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
2025 u8 suspend_flags = pdata->suspend_flags;
2029 netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
2031 /* do this first to ensure it's cleared even in error case */
2032 pdata->suspend_flags = 0;
2034 if (suspend_flags & SUSPEND_ALLMODES) {
2035 /* Disable wakeup sources */
2036 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
2038 netdev_warn(dev->net, "Error reading WUCSR\n");
2042 val &= ~(WUCSR_WUEN | WUCSR_MPEN | WUCSR_PFDA_EN
2045 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
2047 netdev_warn(dev->net, "Error writing WUCSR\n");
2051 /* clear wake-up status */
2052 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
2054 netdev_warn(dev->net, "Error reading PMT_CTL\n");
2058 val &= ~PMT_CTL_WOL_EN;
2059 val |= PMT_CTL_WUPS;
2061 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
2063 netdev_warn(dev->net, "Error writing PMT_CTL\n");
2068 if (suspend_flags & SUSPEND_SUSPEND2) {
2069 netdev_info(dev->net, "resuming from SUSPEND2\n");
2071 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
2073 netdev_warn(dev->net, "Error reading PMT_CTL\n");
2077 val |= PMT_CTL_PHY_PWRUP;
2079 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
2081 netdev_warn(dev->net, "Error writing PMT_CTL\n");
2086 ret = smsc75xx_wait_ready(dev, 1);
2088 netdev_warn(dev->net, "device not ready in smsc75xx_resume\n");
2092 return usbnet_resume(intf);
2095 static void smsc75xx_rx_csum_offload(struct usbnet *dev, struct sk_buff *skb,
2096 u32 rx_cmd_a, u32 rx_cmd_b)
2098 if (!(dev->net->features & NETIF_F_RXCSUM) ||
2099 unlikely(rx_cmd_a & RX_CMD_A_LCSM)) {
2100 skb->ip_summed = CHECKSUM_NONE;
2102 skb->csum = ntohs((u16)(rx_cmd_b >> RX_CMD_B_CSUM_SHIFT));
2103 skb->ip_summed = CHECKSUM_COMPLETE;
2107 static int smsc75xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
2109 /* This check is no longer done by usbnet */
2110 if (skb->len < dev->net->hard_header_len)
2113 while (skb->len > 0) {
2114 u32 rx_cmd_a, rx_cmd_b, align_count, size;
2115 struct sk_buff *ax_skb;
2116 unsigned char *packet;
2118 memcpy(&rx_cmd_a, skb->data, sizeof(rx_cmd_a));
2119 le32_to_cpus(&rx_cmd_a);
2122 memcpy(&rx_cmd_b, skb->data, sizeof(rx_cmd_b));
2123 le32_to_cpus(&rx_cmd_b);
2124 skb_pull(skb, 4 + RXW_PADDING);
2128 /* get the packet length */
2129 size = (rx_cmd_a & RX_CMD_A_LEN) - RXW_PADDING;
2130 align_count = (4 - ((size + RXW_PADDING) % 4)) % 4;
2132 if (unlikely(rx_cmd_a & RX_CMD_A_RED)) {
2133 netif_dbg(dev, rx_err, dev->net,
2134 "Error rx_cmd_a=0x%08x\n", rx_cmd_a);
2135 dev->net->stats.rx_errors++;
2136 dev->net->stats.rx_dropped++;
2138 if (rx_cmd_a & RX_CMD_A_FCS)
2139 dev->net->stats.rx_crc_errors++;
2140 else if (rx_cmd_a & (RX_CMD_A_LONG | RX_CMD_A_RUNT))
2141 dev->net->stats.rx_frame_errors++;
2143 /* MAX_SINGLE_PACKET_SIZE + 4(CRC) + 2(COE) + 4(Vlan) */
2144 if (unlikely(size > (MAX_SINGLE_PACKET_SIZE + ETH_HLEN + 12))) {
2145 netif_dbg(dev, rx_err, dev->net,
2146 "size err rx_cmd_a=0x%08x\n",
2151 /* last frame in this batch */
2152 if (skb->len == size) {
2153 smsc75xx_rx_csum_offload(dev, skb, rx_cmd_a,
2156 skb_trim(skb, skb->len - 4); /* remove fcs */
2157 skb->truesize = size + sizeof(struct sk_buff);
2162 ax_skb = skb_clone(skb, GFP_ATOMIC);
2163 if (unlikely(!ax_skb)) {
2164 netdev_warn(dev->net, "Error allocating skb\n");
2169 ax_skb->data = packet;
2170 skb_set_tail_pointer(ax_skb, size);
2172 smsc75xx_rx_csum_offload(dev, ax_skb, rx_cmd_a,
2175 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
2176 ax_skb->truesize = size + sizeof(struct sk_buff);
2178 usbnet_skb_return(dev, ax_skb);
2181 skb_pull(skb, size);
2183 /* padding bytes before the next frame starts */
2185 skb_pull(skb, align_count);
2191 static struct sk_buff *smsc75xx_tx_fixup(struct usbnet *dev,
2192 struct sk_buff *skb, gfp_t flags)
2194 u32 tx_cmd_a, tx_cmd_b;
2196 if (skb_headroom(skb) < SMSC75XX_TX_OVERHEAD) {
2197 struct sk_buff *skb2 =
2198 skb_copy_expand(skb, SMSC75XX_TX_OVERHEAD, 0, flags);
2199 dev_kfree_skb_any(skb);
2205 tx_cmd_a = (u32)(skb->len & TX_CMD_A_LEN) | TX_CMD_A_FCS;
2207 if (skb->ip_summed == CHECKSUM_PARTIAL)
2208 tx_cmd_a |= TX_CMD_A_IPE | TX_CMD_A_TPE;
2210 if (skb_is_gso(skb)) {
2211 u16 mss = max(skb_shinfo(skb)->gso_size, TX_MSS_MIN);
2212 tx_cmd_b = (mss << TX_CMD_B_MSS_SHIFT) & TX_CMD_B_MSS;
2214 tx_cmd_a |= TX_CMD_A_LSO;
2220 cpu_to_le32s(&tx_cmd_b);
2221 memcpy(skb->data, &tx_cmd_b, 4);
2224 cpu_to_le32s(&tx_cmd_a);
2225 memcpy(skb->data, &tx_cmd_a, 4);
2230 static int smsc75xx_manage_power(struct usbnet *dev, int on)
2232 dev->intf->needs_remote_wakeup = on;
2236 static const struct driver_info smsc75xx_info = {
2237 .description = "smsc75xx USB 2.0 Gigabit Ethernet",
2238 .bind = smsc75xx_bind,
2239 .unbind = smsc75xx_unbind,
2240 .link_reset = smsc75xx_link_reset,
2241 .reset = smsc75xx_reset,
2242 .rx_fixup = smsc75xx_rx_fixup,
2243 .tx_fixup = smsc75xx_tx_fixup,
2244 .status = smsc75xx_status,
2245 .manage_power = smsc75xx_manage_power,
2246 .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
2249 static const struct usb_device_id products[] = {
2251 /* SMSC7500 USB Gigabit Ethernet Device */
2252 USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7500),
2253 .driver_info = (unsigned long) &smsc75xx_info,
2256 /* SMSC7500 USB Gigabit Ethernet Device */
2257 USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7505),
2258 .driver_info = (unsigned long) &smsc75xx_info,
2262 MODULE_DEVICE_TABLE(usb, products);
2264 static struct usb_driver smsc75xx_driver = {
2265 .name = SMSC_CHIPNAME,
2266 .id_table = products,
2267 .probe = usbnet_probe,
2268 .suspend = smsc75xx_suspend,
2269 .resume = smsc75xx_resume,
2270 .reset_resume = smsc75xx_resume,
2271 .disconnect = usbnet_disconnect,
2272 .disable_hub_initiated_lpm = 1,
2273 .supports_autosuspend = 1,
2276 module_usb_driver(smsc75xx_driver);
2278 MODULE_AUTHOR("Nancy Lin");
2279 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
2280 MODULE_DESCRIPTION("SMSC75XX USB 2.0 Gigabit Ethernet Devices");
2281 MODULE_LICENSE("GPL");