1 /***************************************************************************
3 * Copyright (C) 2007-2008 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *****************************************************************************/
21 #include <linux/module.h>
22 #include <linux/kmod.h>
23 #include <linux/init.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/ethtool.h>
27 #include <linux/mii.h>
28 #include <linux/usb.h>
29 #include <linux/bitrev.h>
30 #include <linux/crc16.h>
31 #include <linux/crc32.h>
32 #include <linux/usb/usbnet.h>
33 #include <linux/slab.h>
36 #define SMSC_CHIPNAME "smsc95xx"
37 #define SMSC_DRIVER_VERSION "1.0.4"
38 #define HS_USB_PKT_SIZE (512)
39 #define FS_USB_PKT_SIZE (64)
40 #define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
41 #define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
42 #define DEFAULT_BULK_IN_DELAY (0x00002000)
43 #define MAX_SINGLE_PACKET_SIZE (2048)
44 #define LAN95XX_EEPROM_MAGIC (0x9500)
45 #define EEPROM_MAC_OFFSET (0x01)
46 #define DEFAULT_TX_CSUM_ENABLE (true)
47 #define DEFAULT_RX_CSUM_ENABLE (true)
48 #define SMSC95XX_INTERNAL_PHY_ID (1)
49 #define SMSC95XX_TX_OVERHEAD (8)
50 #define SMSC95XX_TX_OVERHEAD_CSUM (12)
51 #define SUPPORTED_WAKE (WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
52 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
54 #define FEATURE_8_WAKEUP_FILTERS (0x01)
55 #define FEATURE_PHY_NLP_CROSSOVER (0x02)
56 #define FEATURE_REMOTE_WAKEUP (0x04)
58 #define SUSPEND_SUSPEND0 (0x01)
59 #define SUSPEND_SUSPEND1 (0x02)
60 #define SUSPEND_SUSPEND2 (0x04)
61 #define SUSPEND_SUSPEND3 (0x08)
62 #define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
63 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
65 struct smsc95xx_priv
{
70 spinlock_t mac_cr_lock
;
75 static bool turbo_mode
= true;
76 module_param(turbo_mode
, bool, 0644);
77 MODULE_PARM_DESC(turbo_mode
, "Enable multiple frames per Rx transaction");
79 static int __must_check
__smsc95xx_read_reg(struct usbnet
*dev
, u32 index
,
84 int (*fn
)(struct usbnet
*, u8
, u8
, u16
, u16
, void *, u16
);
91 fn
= usbnet_read_cmd_nopm
;
93 ret
= fn(dev
, USB_VENDOR_REQUEST_READ_REGISTER
, USB_DIR_IN
94 | USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
96 if (unlikely(ret
< 0))
97 netdev_warn(dev
->net
, "Failed to read reg index 0x%08x: %d\n",
106 static int __must_check
__smsc95xx_write_reg(struct usbnet
*dev
, u32 index
,
111 int (*fn
)(struct usbnet
*, u8
, u8
, u16
, u16
, const void *, u16
);
116 fn
= usbnet_write_cmd
;
118 fn
= usbnet_write_cmd_nopm
;
123 ret
= fn(dev
, USB_VENDOR_REQUEST_WRITE_REGISTER
, USB_DIR_OUT
124 | USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
126 if (unlikely(ret
< 0))
127 netdev_warn(dev
->net
, "Failed to write reg index 0x%08x: %d\n",
133 static int __must_check
smsc95xx_read_reg_nopm(struct usbnet
*dev
, u32 index
,
136 return __smsc95xx_read_reg(dev
, index
, data
, 1);
139 static int __must_check
smsc95xx_write_reg_nopm(struct usbnet
*dev
, u32 index
,
142 return __smsc95xx_write_reg(dev
, index
, data
, 1);
145 static int __must_check
smsc95xx_read_reg(struct usbnet
*dev
, u32 index
,
148 return __smsc95xx_read_reg(dev
, index
, data
, 0);
151 static int __must_check
smsc95xx_write_reg(struct usbnet
*dev
, u32 index
,
154 return __smsc95xx_write_reg(dev
, index
, data
, 0);
157 /* Loop until the read is completed with timeout
158 * called with phy_mutex held */
159 static int __must_check
__smsc95xx_phy_wait_not_busy(struct usbnet
*dev
,
162 unsigned long start_time
= jiffies
;
167 ret
= __smsc95xx_read_reg(dev
, MII_ADDR
, &val
, in_pm
);
169 netdev_warn(dev
->net
, "Error reading MII_ACCESS\n");
173 if (!(val
& MII_BUSY_
))
175 } while (!time_after(jiffies
, start_time
+ HZ
));
180 static int __smsc95xx_mdio_read(struct net_device
*netdev
, int phy_id
, int idx
,
183 struct usbnet
*dev
= netdev_priv(netdev
);
187 mutex_lock(&dev
->phy_mutex
);
189 /* confirm MII not busy */
190 ret
= __smsc95xx_phy_wait_not_busy(dev
, in_pm
);
192 netdev_warn(dev
->net
, "MII is busy in smsc95xx_mdio_read\n");
196 /* set the address, index & direction (read from PHY) */
197 phy_id
&= dev
->mii
.phy_id_mask
;
198 idx
&= dev
->mii
.reg_num_mask
;
199 addr
= (phy_id
<< 11) | (idx
<< 6) | MII_READ_
| MII_BUSY_
;
200 ret
= __smsc95xx_write_reg(dev
, MII_ADDR
, addr
, in_pm
);
202 netdev_warn(dev
->net
, "Error writing MII_ADDR\n");
206 ret
= __smsc95xx_phy_wait_not_busy(dev
, in_pm
);
208 netdev_warn(dev
->net
, "Timed out reading MII reg %02X\n", idx
);
212 ret
= __smsc95xx_read_reg(dev
, MII_DATA
, &val
, in_pm
);
214 netdev_warn(dev
->net
, "Error reading MII_DATA\n");
218 ret
= (u16
)(val
& 0xFFFF);
221 mutex_unlock(&dev
->phy_mutex
);
225 static void __smsc95xx_mdio_write(struct net_device
*netdev
, int phy_id
,
226 int idx
, int regval
, int in_pm
)
228 struct usbnet
*dev
= netdev_priv(netdev
);
232 mutex_lock(&dev
->phy_mutex
);
234 /* confirm MII not busy */
235 ret
= __smsc95xx_phy_wait_not_busy(dev
, in_pm
);
237 netdev_warn(dev
->net
, "MII is busy in smsc95xx_mdio_write\n");
242 ret
= __smsc95xx_write_reg(dev
, MII_DATA
, val
, in_pm
);
244 netdev_warn(dev
->net
, "Error writing MII_DATA\n");
248 /* set the address, index & direction (write to PHY) */
249 phy_id
&= dev
->mii
.phy_id_mask
;
250 idx
&= dev
->mii
.reg_num_mask
;
251 addr
= (phy_id
<< 11) | (idx
<< 6) | MII_WRITE_
| MII_BUSY_
;
252 ret
= __smsc95xx_write_reg(dev
, MII_ADDR
, addr
, in_pm
);
254 netdev_warn(dev
->net
, "Error writing MII_ADDR\n");
258 ret
= __smsc95xx_phy_wait_not_busy(dev
, in_pm
);
260 netdev_warn(dev
->net
, "Timed out writing MII reg %02X\n", idx
);
265 mutex_unlock(&dev
->phy_mutex
);
268 static int smsc95xx_mdio_read_nopm(struct net_device
*netdev
, int phy_id
,
271 return __smsc95xx_mdio_read(netdev
, phy_id
, idx
, 1);
274 static void smsc95xx_mdio_write_nopm(struct net_device
*netdev
, int phy_id
,
277 __smsc95xx_mdio_write(netdev
, phy_id
, idx
, regval
, 1);
280 static int smsc95xx_mdio_read(struct net_device
*netdev
, int phy_id
, int idx
)
282 return __smsc95xx_mdio_read(netdev
, phy_id
, idx
, 0);
285 static void smsc95xx_mdio_write(struct net_device
*netdev
, int phy_id
, int idx
,
288 __smsc95xx_mdio_write(netdev
, phy_id
, idx
, regval
, 0);
291 static int __must_check
smsc95xx_wait_eeprom(struct usbnet
*dev
)
293 unsigned long start_time
= jiffies
;
298 ret
= smsc95xx_read_reg(dev
, E2P_CMD
, &val
);
300 netdev_warn(dev
->net
, "Error reading E2P_CMD\n");
304 if (!(val
& E2P_CMD_BUSY_
) || (val
& E2P_CMD_TIMEOUT_
))
307 } while (!time_after(jiffies
, start_time
+ HZ
));
309 if (val
& (E2P_CMD_TIMEOUT_
| E2P_CMD_BUSY_
)) {
310 netdev_warn(dev
->net
, "EEPROM read operation timeout\n");
317 static int __must_check
smsc95xx_eeprom_confirm_not_busy(struct usbnet
*dev
)
319 unsigned long start_time
= jiffies
;
324 ret
= smsc95xx_read_reg(dev
, E2P_CMD
, &val
);
326 netdev_warn(dev
->net
, "Error reading E2P_CMD\n");
330 if (!(val
& E2P_CMD_BUSY_
))
334 } while (!time_after(jiffies
, start_time
+ HZ
));
336 netdev_warn(dev
->net
, "EEPROM is busy\n");
340 static int smsc95xx_read_eeprom(struct usbnet
*dev
, u32 offset
, u32 length
,
349 ret
= smsc95xx_eeprom_confirm_not_busy(dev
);
353 for (i
= 0; i
< length
; i
++) {
354 val
= E2P_CMD_BUSY_
| E2P_CMD_READ_
| (offset
& E2P_CMD_ADDR_
);
355 ret
= smsc95xx_write_reg(dev
, E2P_CMD
, val
);
357 netdev_warn(dev
->net
, "Error writing E2P_CMD\n");
361 ret
= smsc95xx_wait_eeprom(dev
);
365 ret
= smsc95xx_read_reg(dev
, E2P_DATA
, &val
);
367 netdev_warn(dev
->net
, "Error reading E2P_DATA\n");
371 data
[i
] = val
& 0xFF;
378 static int smsc95xx_write_eeprom(struct usbnet
*dev
, u32 offset
, u32 length
,
387 ret
= smsc95xx_eeprom_confirm_not_busy(dev
);
391 /* Issue write/erase enable command */
392 val
= E2P_CMD_BUSY_
| E2P_CMD_EWEN_
;
393 ret
= smsc95xx_write_reg(dev
, E2P_CMD
, val
);
395 netdev_warn(dev
->net
, "Error writing E2P_DATA\n");
399 ret
= smsc95xx_wait_eeprom(dev
);
403 for (i
= 0; i
< length
; i
++) {
405 /* Fill data register */
407 ret
= smsc95xx_write_reg(dev
, E2P_DATA
, val
);
409 netdev_warn(dev
->net
, "Error writing E2P_DATA\n");
413 /* Send "write" command */
414 val
= E2P_CMD_BUSY_
| E2P_CMD_WRITE_
| (offset
& E2P_CMD_ADDR_
);
415 ret
= smsc95xx_write_reg(dev
, E2P_CMD
, val
);
417 netdev_warn(dev
->net
, "Error writing E2P_CMD\n");
421 ret
= smsc95xx_wait_eeprom(dev
);
431 static int __must_check
smsc95xx_write_reg_async(struct usbnet
*dev
, u16 index
,
441 ret
= usbnet_write_cmd_async(dev
, USB_VENDOR_REQUEST_WRITE_REGISTER
,
442 USB_DIR_OUT
| USB_TYPE_VENDOR
|
444 0, index
, &buf
, size
);
446 netdev_warn(dev
->net
, "Error write async cmd, sts=%d\n",
451 /* returns hash bit number for given MAC address
453 * 01 00 5E 00 00 01 -> returns bit number 31 */
454 static unsigned int smsc95xx_hash(char addr
[ETH_ALEN
])
456 return (ether_crc(ETH_ALEN
, addr
) >> 26) & 0x3f;
459 static void smsc95xx_set_multicast(struct net_device
*netdev
)
461 struct usbnet
*dev
= netdev_priv(netdev
);
462 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
469 spin_lock_irqsave(&pdata
->mac_cr_lock
, flags
);
471 if (dev
->net
->flags
& IFF_PROMISC
) {
472 netif_dbg(dev
, drv
, dev
->net
, "promiscuous mode enabled\n");
473 pdata
->mac_cr
|= MAC_CR_PRMS_
;
474 pdata
->mac_cr
&= ~(MAC_CR_MCPAS_
| MAC_CR_HPFILT_
);
475 } else if (dev
->net
->flags
& IFF_ALLMULTI
) {
476 netif_dbg(dev
, drv
, dev
->net
, "receive all multicast enabled\n");
477 pdata
->mac_cr
|= MAC_CR_MCPAS_
;
478 pdata
->mac_cr
&= ~(MAC_CR_PRMS_
| MAC_CR_HPFILT_
);
479 } else if (!netdev_mc_empty(dev
->net
)) {
480 struct netdev_hw_addr
*ha
;
482 pdata
->mac_cr
|= MAC_CR_HPFILT_
;
483 pdata
->mac_cr
&= ~(MAC_CR_PRMS_
| MAC_CR_MCPAS_
);
485 netdev_for_each_mc_addr(ha
, netdev
) {
486 u32 bitnum
= smsc95xx_hash(ha
->addr
);
487 u32 mask
= 0x01 << (bitnum
& 0x1F);
489 pdata
->hash_hi
|= mask
;
491 pdata
->hash_lo
|= mask
;
494 netif_dbg(dev
, drv
, dev
->net
, "HASHH=0x%08X, HASHL=0x%08X\n",
495 pdata
->hash_hi
, pdata
->hash_lo
);
497 netif_dbg(dev
, drv
, dev
->net
, "receive own packets only\n");
499 ~(MAC_CR_PRMS_
| MAC_CR_MCPAS_
| MAC_CR_HPFILT_
);
502 spin_unlock_irqrestore(&pdata
->mac_cr_lock
, flags
);
504 /* Initiate async writes, as we can't wait for completion here */
505 ret
= smsc95xx_write_reg_async(dev
, HASHH
, pdata
->hash_hi
);
507 netdev_warn(dev
->net
, "failed to initiate async write to HASHH\n");
509 ret
= smsc95xx_write_reg_async(dev
, HASHL
, pdata
->hash_lo
);
511 netdev_warn(dev
->net
, "failed to initiate async write to HASHL\n");
513 ret
= smsc95xx_write_reg_async(dev
, MAC_CR
, pdata
->mac_cr
);
515 netdev_warn(dev
->net
, "failed to initiate async write to MAC_CR\n");
518 static int smsc95xx_phy_update_flowcontrol(struct usbnet
*dev
, u8 duplex
,
519 u16 lcladv
, u16 rmtadv
)
521 u32 flow
, afc_cfg
= 0;
523 int ret
= smsc95xx_read_reg(dev
, AFC_CFG
, &afc_cfg
);
527 if (duplex
== DUPLEX_FULL
) {
528 u8 cap
= mii_resolve_flowctrl_fdx(lcladv
, rmtadv
);
530 if (cap
& FLOW_CTRL_RX
)
535 if (cap
& FLOW_CTRL_TX
)
540 netif_dbg(dev
, link
, dev
->net
, "rx pause %s, tx pause %s\n",
541 cap
& FLOW_CTRL_RX
? "enabled" : "disabled",
542 cap
& FLOW_CTRL_TX
? "enabled" : "disabled");
544 netif_dbg(dev
, link
, dev
->net
, "half duplex\n");
549 ret
= smsc95xx_write_reg(dev
, FLOW
, flow
);
553 return smsc95xx_write_reg(dev
, AFC_CFG
, afc_cfg
);
556 static int smsc95xx_link_reset(struct usbnet
*dev
)
558 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
559 struct mii_if_info
*mii
= &dev
->mii
;
560 struct ethtool_cmd ecmd
= { .cmd
= ETHTOOL_GSET
};
565 /* clear interrupt status */
566 ret
= smsc95xx_mdio_read(dev
->net
, mii
->phy_id
, PHY_INT_SRC
);
570 ret
= smsc95xx_write_reg(dev
, INT_STS
, INT_STS_CLEAR_ALL_
);
574 mii_check_media(mii
, 1, 1);
575 mii_ethtool_gset(&dev
->mii
, &ecmd
);
576 lcladv
= smsc95xx_mdio_read(dev
->net
, mii
->phy_id
, MII_ADVERTISE
);
577 rmtadv
= smsc95xx_mdio_read(dev
->net
, mii
->phy_id
, MII_LPA
);
579 netif_dbg(dev
, link
, dev
->net
,
580 "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
581 ethtool_cmd_speed(&ecmd
), ecmd
.duplex
, lcladv
, rmtadv
);
583 spin_lock_irqsave(&pdata
->mac_cr_lock
, flags
);
584 if (ecmd
.duplex
!= DUPLEX_FULL
) {
585 pdata
->mac_cr
&= ~MAC_CR_FDPX_
;
586 pdata
->mac_cr
|= MAC_CR_RCVOWN_
;
588 pdata
->mac_cr
&= ~MAC_CR_RCVOWN_
;
589 pdata
->mac_cr
|= MAC_CR_FDPX_
;
591 spin_unlock_irqrestore(&pdata
->mac_cr_lock
, flags
);
593 ret
= smsc95xx_write_reg(dev
, MAC_CR
, pdata
->mac_cr
);
597 ret
= smsc95xx_phy_update_flowcontrol(dev
, ecmd
.duplex
, lcladv
, rmtadv
);
599 netdev_warn(dev
->net
, "Error updating PHY flow control\n");
604 static void smsc95xx_status(struct usbnet
*dev
, struct urb
*urb
)
608 if (urb
->actual_length
!= 4) {
609 netdev_warn(dev
->net
, "unexpected urb length %d\n",
614 memcpy(&intdata
, urb
->transfer_buffer
, 4);
615 le32_to_cpus(&intdata
);
617 netif_dbg(dev
, link
, dev
->net
, "intdata: 0x%08X\n", intdata
);
619 if (intdata
& INT_ENP_PHY_INT_
)
620 usbnet_defer_kevent(dev
, EVENT_LINK_RESET
);
622 netdev_warn(dev
->net
, "unexpected interrupt, intdata=0x%08X\n",
626 /* Enable or disable Tx & Rx checksum offload engines */
627 static int smsc95xx_set_features(struct net_device
*netdev
,
628 netdev_features_t features
)
630 struct usbnet
*dev
= netdev_priv(netdev
);
634 ret
= smsc95xx_read_reg(dev
, COE_CR
, &read_buf
);
638 if (features
& NETIF_F_HW_CSUM
)
639 read_buf
|= Tx_COE_EN_
;
641 read_buf
&= ~Tx_COE_EN_
;
643 if (features
& NETIF_F_RXCSUM
)
644 read_buf
|= Rx_COE_EN_
;
646 read_buf
&= ~Rx_COE_EN_
;
648 ret
= smsc95xx_write_reg(dev
, COE_CR
, read_buf
);
652 netif_dbg(dev
, hw
, dev
->net
, "COE_CR = 0x%08x\n", read_buf
);
656 static int smsc95xx_ethtool_get_eeprom_len(struct net_device
*net
)
658 return MAX_EEPROM_SIZE
;
661 static int smsc95xx_ethtool_get_eeprom(struct net_device
*netdev
,
662 struct ethtool_eeprom
*ee
, u8
*data
)
664 struct usbnet
*dev
= netdev_priv(netdev
);
666 ee
->magic
= LAN95XX_EEPROM_MAGIC
;
668 return smsc95xx_read_eeprom(dev
, ee
->offset
, ee
->len
, data
);
671 static int smsc95xx_ethtool_set_eeprom(struct net_device
*netdev
,
672 struct ethtool_eeprom
*ee
, u8
*data
)
674 struct usbnet
*dev
= netdev_priv(netdev
);
676 if (ee
->magic
!= LAN95XX_EEPROM_MAGIC
) {
677 netdev_warn(dev
->net
, "EEPROM: magic value mismatch, magic = 0x%x\n",
682 return smsc95xx_write_eeprom(dev
, ee
->offset
, ee
->len
, data
);
685 static int smsc95xx_ethtool_getregslen(struct net_device
*netdev
)
687 /* all smsc95xx registers */
688 return COE_CR
- ID_REV
+ sizeof(u32
);
692 smsc95xx_ethtool_getregs(struct net_device
*netdev
, struct ethtool_regs
*regs
,
695 struct usbnet
*dev
= netdev_priv(netdev
);
700 retval
= smsc95xx_read_reg(dev
, ID_REV
, ®s
->version
);
702 netdev_warn(netdev
, "REGS: cannot read ID_REV\n");
706 for (i
= ID_REV
, j
= 0; i
<= COE_CR
; i
+= (sizeof(u32
)), j
++) {
707 retval
= smsc95xx_read_reg(dev
, i
, &data
[j
]);
709 netdev_warn(netdev
, "REGS: cannot read reg[%x]\n", i
);
715 static void smsc95xx_ethtool_get_wol(struct net_device
*net
,
716 struct ethtool_wolinfo
*wolinfo
)
718 struct usbnet
*dev
= netdev_priv(net
);
719 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
721 wolinfo
->supported
= SUPPORTED_WAKE
;
722 wolinfo
->wolopts
= pdata
->wolopts
;
725 static int smsc95xx_ethtool_set_wol(struct net_device
*net
,
726 struct ethtool_wolinfo
*wolinfo
)
728 struct usbnet
*dev
= netdev_priv(net
);
729 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
732 pdata
->wolopts
= wolinfo
->wolopts
& SUPPORTED_WAKE
;
734 ret
= device_set_wakeup_enable(&dev
->udev
->dev
, pdata
->wolopts
);
736 netdev_warn(dev
->net
, "device_set_wakeup_enable error %d\n", ret
);
741 static const struct ethtool_ops smsc95xx_ethtool_ops
= {
742 .get_link
= usbnet_get_link
,
743 .nway_reset
= usbnet_nway_reset
,
744 .get_drvinfo
= usbnet_get_drvinfo
,
745 .get_msglevel
= usbnet_get_msglevel
,
746 .set_msglevel
= usbnet_set_msglevel
,
747 .get_settings
= usbnet_get_settings
,
748 .set_settings
= usbnet_set_settings
,
749 .get_eeprom_len
= smsc95xx_ethtool_get_eeprom_len
,
750 .get_eeprom
= smsc95xx_ethtool_get_eeprom
,
751 .set_eeprom
= smsc95xx_ethtool_set_eeprom
,
752 .get_regs_len
= smsc95xx_ethtool_getregslen
,
753 .get_regs
= smsc95xx_ethtool_getregs
,
754 .get_wol
= smsc95xx_ethtool_get_wol
,
755 .set_wol
= smsc95xx_ethtool_set_wol
,
758 static int smsc95xx_ioctl(struct net_device
*netdev
, struct ifreq
*rq
, int cmd
)
760 struct usbnet
*dev
= netdev_priv(netdev
);
762 if (!netif_running(netdev
))
765 return generic_mii_ioctl(&dev
->mii
, if_mii(rq
), cmd
, NULL
);
768 static void smsc95xx_init_mac_address(struct usbnet
*dev
)
770 /* try reading mac address from EEPROM */
771 if (smsc95xx_read_eeprom(dev
, EEPROM_MAC_OFFSET
, ETH_ALEN
,
772 dev
->net
->dev_addr
) == 0) {
773 if (is_valid_ether_addr(dev
->net
->dev_addr
)) {
774 /* eeprom values are valid so use them */
775 netif_dbg(dev
, ifup
, dev
->net
, "MAC address read from EEPROM\n");
780 /* no eeprom, or eeprom values are invalid. generate random MAC */
781 eth_hw_addr_random(dev
->net
);
782 netif_dbg(dev
, ifup
, dev
->net
, "MAC address set to eth_random_addr\n");
785 static int smsc95xx_set_mac_address(struct usbnet
*dev
)
787 u32 addr_lo
= dev
->net
->dev_addr
[0] | dev
->net
->dev_addr
[1] << 8 |
788 dev
->net
->dev_addr
[2] << 16 | dev
->net
->dev_addr
[3] << 24;
789 u32 addr_hi
= dev
->net
->dev_addr
[4] | dev
->net
->dev_addr
[5] << 8;
792 ret
= smsc95xx_write_reg(dev
, ADDRL
, addr_lo
);
796 return smsc95xx_write_reg(dev
, ADDRH
, addr_hi
);
799 /* starts the TX path */
800 static int smsc95xx_start_tx_path(struct usbnet
*dev
)
802 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
806 /* Enable Tx at MAC */
807 spin_lock_irqsave(&pdata
->mac_cr_lock
, flags
);
808 pdata
->mac_cr
|= MAC_CR_TXEN_
;
809 spin_unlock_irqrestore(&pdata
->mac_cr_lock
, flags
);
811 ret
= smsc95xx_write_reg(dev
, MAC_CR
, pdata
->mac_cr
);
815 /* Enable Tx at SCSRs */
816 return smsc95xx_write_reg(dev
, TX_CFG
, TX_CFG_ON_
);
819 /* Starts the Receive path */
820 static int smsc95xx_start_rx_path(struct usbnet
*dev
, int in_pm
)
822 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
825 spin_lock_irqsave(&pdata
->mac_cr_lock
, flags
);
826 pdata
->mac_cr
|= MAC_CR_RXEN_
;
827 spin_unlock_irqrestore(&pdata
->mac_cr_lock
, flags
);
829 return __smsc95xx_write_reg(dev
, MAC_CR
, pdata
->mac_cr
, in_pm
);
832 static int smsc95xx_phy_initialize(struct usbnet
*dev
)
834 int bmcr
, ret
, timeout
= 0;
836 /* Initialize MII structure */
837 dev
->mii
.dev
= dev
->net
;
838 dev
->mii
.mdio_read
= smsc95xx_mdio_read
;
839 dev
->mii
.mdio_write
= smsc95xx_mdio_write
;
840 dev
->mii
.phy_id_mask
= 0x1f;
841 dev
->mii
.reg_num_mask
= 0x1f;
842 dev
->mii
.phy_id
= SMSC95XX_INTERNAL_PHY_ID
;
844 /* reset phy and wait for reset to complete */
845 smsc95xx_mdio_write(dev
->net
, dev
->mii
.phy_id
, MII_BMCR
, BMCR_RESET
);
849 bmcr
= smsc95xx_mdio_read(dev
->net
, dev
->mii
.phy_id
, MII_BMCR
);
851 } while ((bmcr
& BMCR_RESET
) && (timeout
< 100));
853 if (timeout
>= 100) {
854 netdev_warn(dev
->net
, "timeout on PHY Reset");
858 smsc95xx_mdio_write(dev
->net
, dev
->mii
.phy_id
, MII_ADVERTISE
,
859 ADVERTISE_ALL
| ADVERTISE_CSMA
| ADVERTISE_PAUSE_CAP
|
860 ADVERTISE_PAUSE_ASYM
);
863 ret
= smsc95xx_mdio_read(dev
->net
, dev
->mii
.phy_id
, PHY_INT_SRC
);
865 netdev_warn(dev
->net
, "Failed to read PHY_INT_SRC during init\n");
869 smsc95xx_mdio_write(dev
->net
, dev
->mii
.phy_id
, PHY_INT_MASK
,
870 PHY_INT_MASK_DEFAULT_
);
871 mii_nway_restart(&dev
->mii
);
873 netif_dbg(dev
, ifup
, dev
->net
, "phy initialised successfully\n");
877 static int smsc95xx_reset(struct usbnet
*dev
)
879 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
880 u32 read_buf
, write_buf
, burst_cap
;
881 int ret
= 0, timeout
;
883 netif_dbg(dev
, ifup
, dev
->net
, "entering smsc95xx_reset\n");
885 ret
= smsc95xx_write_reg(dev
, HW_CFG
, HW_CFG_LRST_
);
892 ret
= smsc95xx_read_reg(dev
, HW_CFG
, &read_buf
);
896 } while ((read_buf
& HW_CFG_LRST_
) && (timeout
< 100));
898 if (timeout
>= 100) {
899 netdev_warn(dev
->net
, "timeout waiting for completion of Lite Reset\n");
903 ret
= smsc95xx_write_reg(dev
, PM_CTRL
, PM_CTL_PHY_RST_
);
910 ret
= smsc95xx_read_reg(dev
, PM_CTRL
, &read_buf
);
914 } while ((read_buf
& PM_CTL_PHY_RST_
) && (timeout
< 100));
916 if (timeout
>= 100) {
917 netdev_warn(dev
->net
, "timeout waiting for PHY Reset\n");
921 ret
= smsc95xx_set_mac_address(dev
);
925 netif_dbg(dev
, ifup
, dev
->net
, "MAC Address: %pM\n",
928 ret
= smsc95xx_read_reg(dev
, HW_CFG
, &read_buf
);
932 netif_dbg(dev
, ifup
, dev
->net
, "Read Value from HW_CFG : 0x%08x\n",
935 read_buf
|= HW_CFG_BIR_
;
937 ret
= smsc95xx_write_reg(dev
, HW_CFG
, read_buf
);
941 ret
= smsc95xx_read_reg(dev
, HW_CFG
, &read_buf
);
945 netif_dbg(dev
, ifup
, dev
->net
,
946 "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
951 dev
->rx_urb_size
= MAX_SINGLE_PACKET_SIZE
;
952 } else if (dev
->udev
->speed
== USB_SPEED_HIGH
) {
953 burst_cap
= DEFAULT_HS_BURST_CAP_SIZE
/ HS_USB_PKT_SIZE
;
954 dev
->rx_urb_size
= DEFAULT_HS_BURST_CAP_SIZE
;
956 burst_cap
= DEFAULT_FS_BURST_CAP_SIZE
/ FS_USB_PKT_SIZE
;
957 dev
->rx_urb_size
= DEFAULT_FS_BURST_CAP_SIZE
;
960 netif_dbg(dev
, ifup
, dev
->net
, "rx_urb_size=%ld\n",
961 (ulong
)dev
->rx_urb_size
);
963 ret
= smsc95xx_write_reg(dev
, BURST_CAP
, burst_cap
);
967 ret
= smsc95xx_read_reg(dev
, BURST_CAP
, &read_buf
);
971 netif_dbg(dev
, ifup
, dev
->net
,
972 "Read Value from BURST_CAP after writing: 0x%08x\n",
975 ret
= smsc95xx_write_reg(dev
, BULK_IN_DLY
, DEFAULT_BULK_IN_DELAY
);
979 ret
= smsc95xx_read_reg(dev
, BULK_IN_DLY
, &read_buf
);
983 netif_dbg(dev
, ifup
, dev
->net
,
984 "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
987 ret
= smsc95xx_read_reg(dev
, HW_CFG
, &read_buf
);
991 netif_dbg(dev
, ifup
, dev
->net
, "Read Value from HW_CFG: 0x%08x\n",
995 read_buf
|= (HW_CFG_MEF_
| HW_CFG_BCE_
);
997 read_buf
&= ~HW_CFG_RXDOFF_
;
999 /* set Rx data offset=2, Make IP header aligns on word boundary. */
1000 read_buf
|= NET_IP_ALIGN
<< 9;
1002 ret
= smsc95xx_write_reg(dev
, HW_CFG
, read_buf
);
1006 ret
= smsc95xx_read_reg(dev
, HW_CFG
, &read_buf
);
1010 netif_dbg(dev
, ifup
, dev
->net
,
1011 "Read Value from HW_CFG after writing: 0x%08x\n", read_buf
);
1013 ret
= smsc95xx_write_reg(dev
, INT_STS
, INT_STS_CLEAR_ALL_
);
1017 ret
= smsc95xx_read_reg(dev
, ID_REV
, &read_buf
);
1020 netif_dbg(dev
, ifup
, dev
->net
, "ID_REV = 0x%08x\n", read_buf
);
1022 /* Configure GPIO pins as LED outputs */
1023 write_buf
= LED_GPIO_CFG_SPD_LED
| LED_GPIO_CFG_LNK_LED
|
1024 LED_GPIO_CFG_FDX_LED
;
1025 ret
= smsc95xx_write_reg(dev
, LED_GPIO_CFG
, write_buf
);
1030 ret
= smsc95xx_write_reg(dev
, FLOW
, 0);
1034 ret
= smsc95xx_write_reg(dev
, AFC_CFG
, AFC_CFG_DEFAULT
);
1038 /* Don't need mac_cr_lock during initialisation */
1039 ret
= smsc95xx_read_reg(dev
, MAC_CR
, &pdata
->mac_cr
);
1045 ret
= smsc95xx_write_reg(dev
, VLAN1
, (u32
)ETH_P_8021Q
);
1049 /* Enable or disable checksum offload engines */
1050 ret
= smsc95xx_set_features(dev
->net
, dev
->net
->features
);
1052 netdev_warn(dev
->net
, "Failed to set checksum offload features\n");
1056 smsc95xx_set_multicast(dev
->net
);
1058 ret
= smsc95xx_phy_initialize(dev
);
1060 netdev_warn(dev
->net
, "Failed to init PHY\n");
1064 ret
= smsc95xx_read_reg(dev
, INT_EP_CTL
, &read_buf
);
1068 /* enable PHY interrupts */
1069 read_buf
|= INT_EP_CTL_PHY_INT_
;
1071 ret
= smsc95xx_write_reg(dev
, INT_EP_CTL
, read_buf
);
1075 ret
= smsc95xx_start_tx_path(dev
);
1077 netdev_warn(dev
->net
, "Failed to start TX path\n");
1081 ret
= smsc95xx_start_rx_path(dev
, 0);
1083 netdev_warn(dev
->net
, "Failed to start RX path\n");
1087 netif_dbg(dev
, ifup
, dev
->net
, "smsc95xx_reset, return 0\n");
1091 static const struct net_device_ops smsc95xx_netdev_ops
= {
1092 .ndo_open
= usbnet_open
,
1093 .ndo_stop
= usbnet_stop
,
1094 .ndo_start_xmit
= usbnet_start_xmit
,
1095 .ndo_tx_timeout
= usbnet_tx_timeout
,
1096 .ndo_change_mtu
= usbnet_change_mtu
,
1097 .ndo_set_mac_address
= eth_mac_addr
,
1098 .ndo_validate_addr
= eth_validate_addr
,
1099 .ndo_do_ioctl
= smsc95xx_ioctl
,
1100 .ndo_set_rx_mode
= smsc95xx_set_multicast
,
1101 .ndo_set_features
= smsc95xx_set_features
,
1104 static int smsc95xx_bind(struct usbnet
*dev
, struct usb_interface
*intf
)
1106 struct smsc95xx_priv
*pdata
= NULL
;
1110 printk(KERN_INFO SMSC_CHIPNAME
" v" SMSC_DRIVER_VERSION
"\n");
1112 ret
= usbnet_get_endpoints(dev
, intf
);
1114 netdev_warn(dev
->net
, "usbnet_get_endpoints failed: %d\n", ret
);
1118 dev
->data
[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv
),
1121 pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1125 spin_lock_init(&pdata
->mac_cr_lock
);
1127 if (DEFAULT_TX_CSUM_ENABLE
)
1128 dev
->net
->features
|= NETIF_F_HW_CSUM
;
1129 if (DEFAULT_RX_CSUM_ENABLE
)
1130 dev
->net
->features
|= NETIF_F_RXCSUM
;
1132 dev
->net
->hw_features
= NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
1134 smsc95xx_init_mac_address(dev
);
1136 /* Init all registers */
1137 ret
= smsc95xx_reset(dev
);
1139 /* detect device revision as different features may be available */
1140 ret
= smsc95xx_read_reg(dev
, ID_REV
, &val
);
1145 if ((val
== ID_REV_CHIP_ID_9500A_
) || (val
== ID_REV_CHIP_ID_9530_
) ||
1146 (val
== ID_REV_CHIP_ID_89530_
) || (val
== ID_REV_CHIP_ID_9730_
))
1147 pdata
->features
= (FEATURE_8_WAKEUP_FILTERS
|
1148 FEATURE_PHY_NLP_CROSSOVER
|
1149 FEATURE_REMOTE_WAKEUP
);
1150 else if (val
== ID_REV_CHIP_ID_9512_
)
1151 pdata
->features
= FEATURE_8_WAKEUP_FILTERS
;
1153 dev
->net
->netdev_ops
= &smsc95xx_netdev_ops
;
1154 dev
->net
->ethtool_ops
= &smsc95xx_ethtool_ops
;
1155 dev
->net
->flags
|= IFF_MULTICAST
;
1156 dev
->net
->hard_header_len
+= SMSC95XX_TX_OVERHEAD_CSUM
;
1157 dev
->hard_mtu
= dev
->net
->mtu
+ dev
->net
->hard_header_len
;
1161 static void smsc95xx_unbind(struct usbnet
*dev
, struct usb_interface
*intf
)
1163 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1165 netif_dbg(dev
, ifdown
, dev
->net
, "free pdata\n");
1172 static u32
smsc_crc(const u8
*buffer
, size_t len
, int filter
)
1174 u32 crc
= bitrev16(crc16(0xFFFF, buffer
, len
));
1175 return crc
<< ((filter
% 2) * 16);
1178 static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet
*dev
, u16 mask
)
1180 struct mii_if_info
*mii
= &dev
->mii
;
1183 netdev_dbg(dev
->net
, "enabling PHY wakeup interrupts\n");
1186 ret
= smsc95xx_mdio_read_nopm(dev
->net
, mii
->phy_id
, PHY_INT_SRC
);
1190 /* enable interrupt source */
1191 ret
= smsc95xx_mdio_read_nopm(dev
->net
, mii
->phy_id
, PHY_INT_MASK
);
1197 smsc95xx_mdio_write_nopm(dev
->net
, mii
->phy_id
, PHY_INT_MASK
, ret
);
1202 static int smsc95xx_link_ok_nopm(struct usbnet
*dev
)
1204 struct mii_if_info
*mii
= &dev
->mii
;
1207 /* first, a dummy read, needed to latch some MII phys */
1208 ret
= smsc95xx_mdio_read_nopm(dev
->net
, mii
->phy_id
, MII_BMSR
);
1212 ret
= smsc95xx_mdio_read_nopm(dev
->net
, mii
->phy_id
, MII_BMSR
);
1216 return !!(ret
& BMSR_LSTATUS
);
1219 static int smsc95xx_enter_suspend0(struct usbnet
*dev
)
1221 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1225 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1229 val
&= (~(PM_CTL_SUS_MODE_
| PM_CTL_WUPS_
| PM_CTL_PHY_RST_
));
1230 val
|= PM_CTL_SUS_MODE_0
;
1232 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1236 /* clear wol status */
1237 val
&= ~PM_CTL_WUPS_
;
1238 val
|= PM_CTL_WUPS_WOL_
;
1240 /* enable energy detection */
1241 if (pdata
->wolopts
& WAKE_PHY
)
1242 val
|= PM_CTL_WUPS_ED_
;
1244 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1248 /* read back PM_CTRL */
1249 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1253 pdata
->suspend_flags
|= SUSPEND_SUSPEND0
;
1258 static int smsc95xx_enter_suspend1(struct usbnet
*dev
)
1260 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1261 struct mii_if_info
*mii
= &dev
->mii
;
1265 /* reconfigure link pulse detection timing for
1266 * compatibility with non-standard link partners
1268 if (pdata
->features
& FEATURE_PHY_NLP_CROSSOVER
)
1269 smsc95xx_mdio_write_nopm(dev
->net
, mii
->phy_id
, PHY_EDPD_CONFIG
,
1270 PHY_EDPD_CONFIG_DEFAULT
);
1272 /* enable energy detect power-down mode */
1273 ret
= smsc95xx_mdio_read_nopm(dev
->net
, mii
->phy_id
, PHY_MODE_CTRL_STS
);
1277 ret
|= MODE_CTRL_STS_EDPWRDOWN_
;
1279 smsc95xx_mdio_write_nopm(dev
->net
, mii
->phy_id
, PHY_MODE_CTRL_STS
, ret
);
1281 /* enter SUSPEND1 mode */
1282 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1286 val
&= ~(PM_CTL_SUS_MODE_
| PM_CTL_WUPS_
| PM_CTL_PHY_RST_
);
1287 val
|= PM_CTL_SUS_MODE_1
;
1289 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1293 /* clear wol status, enable energy detection */
1294 val
&= ~PM_CTL_WUPS_
;
1295 val
|= (PM_CTL_WUPS_ED_
| PM_CTL_ED_EN_
);
1297 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1301 pdata
->suspend_flags
|= SUSPEND_SUSPEND1
;
1306 static int smsc95xx_enter_suspend2(struct usbnet
*dev
)
1308 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1312 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1316 val
&= ~(PM_CTL_SUS_MODE_
| PM_CTL_WUPS_
| PM_CTL_PHY_RST_
);
1317 val
|= PM_CTL_SUS_MODE_2
;
1319 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1323 pdata
->suspend_flags
|= SUSPEND_SUSPEND2
;
1328 static int smsc95xx_enter_suspend3(struct usbnet
*dev
)
1330 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1334 ret
= smsc95xx_read_reg_nopm(dev
, RX_FIFO_INF
, &val
);
1339 netdev_info(dev
->net
, "rx fifo not empty in autosuspend\n");
1343 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1347 val
&= ~(PM_CTL_SUS_MODE_
| PM_CTL_WUPS_
| PM_CTL_PHY_RST_
);
1348 val
|= PM_CTL_SUS_MODE_3
| PM_CTL_RES_CLR_WKP_STS
;
1350 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1354 /* clear wol status */
1355 val
&= ~PM_CTL_WUPS_
;
1356 val
|= PM_CTL_WUPS_WOL_
;
1358 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1362 pdata
->suspend_flags
|= SUSPEND_SUSPEND3
;
1367 static int smsc95xx_autosuspend(struct usbnet
*dev
, u32 link_up
)
1369 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1372 if (!netif_running(dev
->net
)) {
1373 /* interface is ifconfig down so fully power down hw */
1374 netdev_dbg(dev
->net
, "autosuspend entering SUSPEND2\n");
1375 return smsc95xx_enter_suspend2(dev
);
1379 /* link is down so enter EDPD mode, but only if device can
1380 * reliably resume from it. This check should be redundant
1381 * as current FEATURE_REMOTE_WAKEUP parts also support
1382 * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */
1383 if (!(pdata
->features
& FEATURE_PHY_NLP_CROSSOVER
)) {
1384 netdev_warn(dev
->net
, "EDPD not supported\n");
1388 netdev_dbg(dev
->net
, "autosuspend entering SUSPEND1\n");
1390 /* enable PHY wakeup events for if cable is attached */
1391 ret
= smsc95xx_enable_phy_wakeup_interrupts(dev
,
1392 PHY_INT_MASK_ANEG_COMP_
);
1394 netdev_warn(dev
->net
, "error enabling PHY wakeup ints\n");
1398 netdev_info(dev
->net
, "entering SUSPEND1 mode\n");
1399 return smsc95xx_enter_suspend1(dev
);
1402 /* enable PHY wakeup events so we remote wakeup if cable is pulled */
1403 ret
= smsc95xx_enable_phy_wakeup_interrupts(dev
,
1404 PHY_INT_MASK_LINK_DOWN_
);
1406 netdev_warn(dev
->net
, "error enabling PHY wakeup ints\n");
1410 netdev_dbg(dev
->net
, "autosuspend entering SUSPEND3\n");
1411 return smsc95xx_enter_suspend3(dev
);
1414 static int smsc95xx_suspend(struct usb_interface
*intf
, pm_message_t message
)
1416 struct usbnet
*dev
= usb_get_intfdata(intf
);
1417 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1421 ret
= usbnet_suspend(intf
, message
);
1423 netdev_warn(dev
->net
, "usbnet_suspend error\n");
1427 if (pdata
->suspend_flags
) {
1428 netdev_warn(dev
->net
, "error during last resume\n");
1429 pdata
->suspend_flags
= 0;
1432 /* determine if link is up using only _nopm functions */
1433 link_up
= smsc95xx_link_ok_nopm(dev
);
1435 if (message
.event
== PM_EVENT_AUTO_SUSPEND
&&
1436 (pdata
->features
& FEATURE_REMOTE_WAKEUP
)) {
1437 ret
= smsc95xx_autosuspend(dev
, link_up
);
1441 /* if we get this far we're not autosuspending */
1442 /* if no wol options set, or if link is down and we're not waking on
1443 * PHY activity, enter lowest power SUSPEND2 mode
1445 if (!(pdata
->wolopts
& SUPPORTED_WAKE
) ||
1446 !(link_up
|| (pdata
->wolopts
& WAKE_PHY
))) {
1447 netdev_info(dev
->net
, "entering SUSPEND2 mode\n");
1449 /* disable energy detect (link up) & wake up events */
1450 ret
= smsc95xx_read_reg_nopm(dev
, WUCSR
, &val
);
1454 val
&= ~(WUCSR_MPEN_
| WUCSR_WAKE_EN_
);
1456 ret
= smsc95xx_write_reg_nopm(dev
, WUCSR
, val
);
1460 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1464 val
&= ~(PM_CTL_ED_EN_
| PM_CTL_WOL_EN_
);
1466 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1470 ret
= smsc95xx_enter_suspend2(dev
);
1474 if (pdata
->wolopts
& WAKE_PHY
) {
1475 ret
= smsc95xx_enable_phy_wakeup_interrupts(dev
,
1476 (PHY_INT_MASK_ANEG_COMP_
| PHY_INT_MASK_LINK_DOWN_
));
1478 netdev_warn(dev
->net
, "error enabling PHY wakeup ints\n");
1482 /* if link is down then configure EDPD and enter SUSPEND1,
1483 * otherwise enter SUSPEND0 below
1486 netdev_info(dev
->net
, "entering SUSPEND1 mode\n");
1487 ret
= smsc95xx_enter_suspend1(dev
);
1492 if (pdata
->wolopts
& (WAKE_BCAST
| WAKE_MCAST
| WAKE_ARP
| WAKE_UCAST
)) {
1493 u32
*filter_mask
= kzalloc(sizeof(u32
) * 32, GFP_KERNEL
);
1497 int wuff_filter_count
=
1498 (pdata
->features
& FEATURE_8_WAKEUP_FILTERS
) ?
1499 LAN9500A_WUFF_NUM
: LAN9500_WUFF_NUM
;
1503 netdev_warn(dev
->net
, "Unable to allocate filter_mask\n");
1508 memset(command
, 0, sizeof(command
));
1509 memset(offset
, 0, sizeof(offset
));
1510 memset(crc
, 0, sizeof(crc
));
1512 if (pdata
->wolopts
& WAKE_BCAST
) {
1513 const u8 bcast
[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1514 netdev_info(dev
->net
, "enabling broadcast detection\n");
1515 filter_mask
[filter
* 4] = 0x003F;
1516 filter_mask
[filter
* 4 + 1] = 0x00;
1517 filter_mask
[filter
* 4 + 2] = 0x00;
1518 filter_mask
[filter
* 4 + 3] = 0x00;
1519 command
[filter
/4] |= 0x05UL
<< ((filter
% 4) * 8);
1520 offset
[filter
/4] |= 0x00 << ((filter
% 4) * 8);
1521 crc
[filter
/2] |= smsc_crc(bcast
, 6, filter
);
1525 if (pdata
->wolopts
& WAKE_MCAST
) {
1526 const u8 mcast
[] = {0x01, 0x00, 0x5E};
1527 netdev_info(dev
->net
, "enabling multicast detection\n");
1528 filter_mask
[filter
* 4] = 0x0007;
1529 filter_mask
[filter
* 4 + 1] = 0x00;
1530 filter_mask
[filter
* 4 + 2] = 0x00;
1531 filter_mask
[filter
* 4 + 3] = 0x00;
1532 command
[filter
/4] |= 0x09UL
<< ((filter
% 4) * 8);
1533 offset
[filter
/4] |= 0x00 << ((filter
% 4) * 8);
1534 crc
[filter
/2] |= smsc_crc(mcast
, 3, filter
);
1538 if (pdata
->wolopts
& WAKE_ARP
) {
1539 const u8 arp
[] = {0x08, 0x06};
1540 netdev_info(dev
->net
, "enabling ARP detection\n");
1541 filter_mask
[filter
* 4] = 0x0003;
1542 filter_mask
[filter
* 4 + 1] = 0x00;
1543 filter_mask
[filter
* 4 + 2] = 0x00;
1544 filter_mask
[filter
* 4 + 3] = 0x00;
1545 command
[filter
/4] |= 0x05UL
<< ((filter
% 4) * 8);
1546 offset
[filter
/4] |= 0x0C << ((filter
% 4) * 8);
1547 crc
[filter
/2] |= smsc_crc(arp
, 2, filter
);
1551 if (pdata
->wolopts
& WAKE_UCAST
) {
1552 netdev_info(dev
->net
, "enabling unicast 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] |= 0x01UL
<< ((filter
% 4) * 8);
1558 offset
[filter
/4] |= 0x00 << ((filter
% 4) * 8);
1559 crc
[filter
/2] |= smsc_crc(dev
->net
->dev_addr
, ETH_ALEN
, filter
);
1563 for (i
= 0; i
< (wuff_filter_count
* 4); i
++) {
1564 ret
= smsc95xx_write_reg_nopm(dev
, WUFF
, filter_mask
[i
]);
1572 for (i
= 0; i
< (wuff_filter_count
/ 4); i
++) {
1573 ret
= smsc95xx_write_reg_nopm(dev
, WUFF
, command
[i
]);
1578 for (i
= 0; i
< (wuff_filter_count
/ 4); i
++) {
1579 ret
= smsc95xx_write_reg_nopm(dev
, WUFF
, offset
[i
]);
1584 for (i
= 0; i
< (wuff_filter_count
/ 2); i
++) {
1585 ret
= smsc95xx_write_reg_nopm(dev
, WUFF
, crc
[i
]);
1590 /* clear any pending pattern match packet status */
1591 ret
= smsc95xx_read_reg_nopm(dev
, WUCSR
, &val
);
1597 ret
= smsc95xx_write_reg_nopm(dev
, WUCSR
, val
);
1602 if (pdata
->wolopts
& WAKE_MAGIC
) {
1603 /* clear any pending magic packet status */
1604 ret
= smsc95xx_read_reg_nopm(dev
, WUCSR
, &val
);
1610 ret
= smsc95xx_write_reg_nopm(dev
, WUCSR
, val
);
1615 /* enable/disable wakeup sources */
1616 ret
= smsc95xx_read_reg_nopm(dev
, WUCSR
, &val
);
1620 if (pdata
->wolopts
& (WAKE_BCAST
| WAKE_MCAST
| WAKE_ARP
| WAKE_UCAST
)) {
1621 netdev_info(dev
->net
, "enabling pattern match wakeup\n");
1622 val
|= WUCSR_WAKE_EN_
;
1624 netdev_info(dev
->net
, "disabling pattern match wakeup\n");
1625 val
&= ~WUCSR_WAKE_EN_
;
1628 if (pdata
->wolopts
& WAKE_MAGIC
) {
1629 netdev_info(dev
->net
, "enabling magic packet wakeup\n");
1632 netdev_info(dev
->net
, "disabling magic packet wakeup\n");
1633 val
&= ~WUCSR_MPEN_
;
1636 ret
= smsc95xx_write_reg_nopm(dev
, WUCSR
, val
);
1640 /* enable wol wakeup source */
1641 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1645 val
|= PM_CTL_WOL_EN_
;
1647 /* phy energy detect wakeup source */
1648 if (pdata
->wolopts
& WAKE_PHY
)
1649 val
|= PM_CTL_ED_EN_
;
1651 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1655 /* enable receiver to enable frame reception */
1656 smsc95xx_start_rx_path(dev
, 1);
1658 /* some wol options are enabled, so enter SUSPEND0 */
1659 netdev_info(dev
->net
, "entering SUSPEND0 mode\n");
1660 ret
= smsc95xx_enter_suspend0(dev
);
1664 * TODO: resume() might need to handle the suspend failure
1667 if (ret
&& PMSG_IS_AUTO(message
))
1668 usbnet_resume(intf
);
1672 static int smsc95xx_resume(struct usb_interface
*intf
)
1674 struct usbnet
*dev
= usb_get_intfdata(intf
);
1675 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1676 u8 suspend_flags
= pdata
->suspend_flags
;
1682 netdev_dbg(dev
->net
, "resume suspend_flags=0x%02x\n", suspend_flags
);
1684 /* do this first to ensure it's cleared even in error case */
1685 pdata
->suspend_flags
= 0;
1687 if (suspend_flags
& SUSPEND_ALLMODES
) {
1688 /* clear wake-up sources */
1689 ret
= smsc95xx_read_reg_nopm(dev
, WUCSR
, &val
);
1693 val
&= ~(WUCSR_WAKE_EN_
| WUCSR_MPEN_
);
1695 ret
= smsc95xx_write_reg_nopm(dev
, WUCSR
, val
);
1699 /* clear wake-up status */
1700 ret
= smsc95xx_read_reg_nopm(dev
, PM_CTRL
, &val
);
1704 val
&= ~PM_CTL_WOL_EN_
;
1705 val
|= PM_CTL_WUPS_
;
1707 ret
= smsc95xx_write_reg_nopm(dev
, PM_CTRL
, val
);
1712 ret
= usbnet_resume(intf
);
1714 netdev_warn(dev
->net
, "usbnet_resume error\n");
1719 static void smsc95xx_rx_csum_offload(struct sk_buff
*skb
)
1721 skb
->csum
= *(u16
*)(skb_tail_pointer(skb
) - 2);
1722 skb
->ip_summed
= CHECKSUM_COMPLETE
;
1723 skb_trim(skb
, skb
->len
- 2);
1726 static int smsc95xx_rx_fixup(struct usbnet
*dev
, struct sk_buff
*skb
)
1728 while (skb
->len
> 0) {
1729 u32 header
, align_count
;
1730 struct sk_buff
*ax_skb
;
1731 unsigned char *packet
;
1734 memcpy(&header
, skb
->data
, sizeof(header
));
1735 le32_to_cpus(&header
);
1736 skb_pull(skb
, 4 + NET_IP_ALIGN
);
1739 /* get the packet length */
1740 size
= (u16
)((header
& RX_STS_FL_
) >> 16);
1741 align_count
= (4 - ((size
+ NET_IP_ALIGN
) % 4)) % 4;
1743 if (unlikely(header
& RX_STS_ES_
)) {
1744 netif_dbg(dev
, rx_err
, dev
->net
,
1745 "Error header=0x%08x\n", header
);
1746 dev
->net
->stats
.rx_errors
++;
1747 dev
->net
->stats
.rx_dropped
++;
1749 if (header
& RX_STS_CRC_
) {
1750 dev
->net
->stats
.rx_crc_errors
++;
1752 if (header
& (RX_STS_TL_
| RX_STS_RF_
))
1753 dev
->net
->stats
.rx_frame_errors
++;
1755 if ((header
& RX_STS_LE_
) &&
1756 (!(header
& RX_STS_FT_
)))
1757 dev
->net
->stats
.rx_length_errors
++;
1760 /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1761 if (unlikely(size
> (ETH_FRAME_LEN
+ 12))) {
1762 netif_dbg(dev
, rx_err
, dev
->net
,
1763 "size err header=0x%08x\n", header
);
1767 /* last frame in this batch */
1768 if (skb
->len
== size
) {
1769 if (dev
->net
->features
& NETIF_F_RXCSUM
)
1770 smsc95xx_rx_csum_offload(skb
);
1771 skb_trim(skb
, skb
->len
- 4); /* remove fcs */
1772 skb
->truesize
= size
+ sizeof(struct sk_buff
);
1777 ax_skb
= skb_clone(skb
, GFP_ATOMIC
);
1778 if (unlikely(!ax_skb
)) {
1779 netdev_warn(dev
->net
, "Error allocating skb\n");
1784 ax_skb
->data
= packet
;
1785 skb_set_tail_pointer(ax_skb
, size
);
1787 if (dev
->net
->features
& NETIF_F_RXCSUM
)
1788 smsc95xx_rx_csum_offload(ax_skb
);
1789 skb_trim(ax_skb
, ax_skb
->len
- 4); /* remove fcs */
1790 ax_skb
->truesize
= size
+ sizeof(struct sk_buff
);
1792 usbnet_skb_return(dev
, ax_skb
);
1795 skb_pull(skb
, size
);
1797 /* padding bytes before the next frame starts */
1799 skb_pull(skb
, align_count
);
1802 if (unlikely(skb
->len
< 0)) {
1803 netdev_warn(dev
->net
, "invalid rx length<0 %d\n", skb
->len
);
1810 static u32
smsc95xx_calc_csum_preamble(struct sk_buff
*skb
)
1812 u16 low_16
= (u16
)skb_checksum_start_offset(skb
);
1813 u16 high_16
= low_16
+ skb
->csum_offset
;
1814 return (high_16
<< 16) | low_16
;
1817 static struct sk_buff
*smsc95xx_tx_fixup(struct usbnet
*dev
,
1818 struct sk_buff
*skb
, gfp_t flags
)
1820 bool csum
= skb
->ip_summed
== CHECKSUM_PARTIAL
;
1821 int overhead
= csum
? SMSC95XX_TX_OVERHEAD_CSUM
: SMSC95XX_TX_OVERHEAD
;
1822 u32 tx_cmd_a
, tx_cmd_b
;
1824 /* We do not advertise SG, so skbs should be already linearized */
1825 BUG_ON(skb_shinfo(skb
)->nr_frags
);
1827 if (skb_headroom(skb
) < overhead
) {
1828 struct sk_buff
*skb2
= skb_copy_expand(skb
,
1829 overhead
, 0, flags
);
1830 dev_kfree_skb_any(skb
);
1837 if (skb
->len
<= 45) {
1838 /* workaround - hardware tx checksum does not work
1839 * properly with extremely small packets */
1840 long csstart
= skb_checksum_start_offset(skb
);
1841 __wsum calc
= csum_partial(skb
->data
+ csstart
,
1842 skb
->len
- csstart
, 0);
1843 *((__sum16
*)(skb
->data
+ csstart
1844 + skb
->csum_offset
)) = csum_fold(calc
);
1848 u32 csum_preamble
= smsc95xx_calc_csum_preamble(skb
);
1850 cpu_to_le32s(&csum_preamble
);
1851 memcpy(skb
->data
, &csum_preamble
, 4);
1856 tx_cmd_b
= (u32
)(skb
->len
- 4);
1858 tx_cmd_b
|= TX_CMD_B_CSUM_ENABLE
;
1859 cpu_to_le32s(&tx_cmd_b
);
1860 memcpy(skb
->data
, &tx_cmd_b
, 4);
1863 tx_cmd_a
= (u32
)(skb
->len
- 8) | TX_CMD_A_FIRST_SEG_
|
1865 cpu_to_le32s(&tx_cmd_a
);
1866 memcpy(skb
->data
, &tx_cmd_a
, 4);
1871 static int smsc95xx_manage_power(struct usbnet
*dev
, int on
)
1873 struct smsc95xx_priv
*pdata
= (struct smsc95xx_priv
*)(dev
->data
[0]);
1875 dev
->intf
->needs_remote_wakeup
= on
;
1877 if (pdata
->features
& FEATURE_REMOTE_WAKEUP
)
1880 /* this chip revision isn't capable of remote wakeup */
1881 netdev_info(dev
->net
, "hardware isn't capable of remote wakeup\n");
1884 usb_autopm_get_interface_no_resume(dev
->intf
);
1886 usb_autopm_put_interface(dev
->intf
);
1891 static const struct driver_info smsc95xx_info
= {
1892 .description
= "smsc95xx USB 2.0 Ethernet",
1893 .bind
= smsc95xx_bind
,
1894 .unbind
= smsc95xx_unbind
,
1895 .link_reset
= smsc95xx_link_reset
,
1896 .reset
= smsc95xx_reset
,
1897 .rx_fixup
= smsc95xx_rx_fixup
,
1898 .tx_fixup
= smsc95xx_tx_fixup
,
1899 .status
= smsc95xx_status
,
1900 .manage_power
= smsc95xx_manage_power
,
1901 .flags
= FLAG_ETHER
| FLAG_SEND_ZLP
| FLAG_LINK_INTR
,
1904 static const struct usb_device_id products
[] = {
1906 /* SMSC9500 USB Ethernet Device */
1907 USB_DEVICE(0x0424, 0x9500),
1908 .driver_info
= (unsigned long) &smsc95xx_info
,
1911 /* SMSC9505 USB Ethernet Device */
1912 USB_DEVICE(0x0424, 0x9505),
1913 .driver_info
= (unsigned long) &smsc95xx_info
,
1916 /* SMSC9500A USB Ethernet Device */
1917 USB_DEVICE(0x0424, 0x9E00),
1918 .driver_info
= (unsigned long) &smsc95xx_info
,
1921 /* SMSC9505A USB Ethernet Device */
1922 USB_DEVICE(0x0424, 0x9E01),
1923 .driver_info
= (unsigned long) &smsc95xx_info
,
1926 /* SMSC9512/9514 USB Hub & Ethernet Device */
1927 USB_DEVICE(0x0424, 0xec00),
1928 .driver_info
= (unsigned long) &smsc95xx_info
,
1931 /* SMSC9500 USB Ethernet Device (SAL10) */
1932 USB_DEVICE(0x0424, 0x9900),
1933 .driver_info
= (unsigned long) &smsc95xx_info
,
1936 /* SMSC9505 USB Ethernet Device (SAL10) */
1937 USB_DEVICE(0x0424, 0x9901),
1938 .driver_info
= (unsigned long) &smsc95xx_info
,
1941 /* SMSC9500A USB Ethernet Device (SAL10) */
1942 USB_DEVICE(0x0424, 0x9902),
1943 .driver_info
= (unsigned long) &smsc95xx_info
,
1946 /* SMSC9505A USB Ethernet Device (SAL10) */
1947 USB_DEVICE(0x0424, 0x9903),
1948 .driver_info
= (unsigned long) &smsc95xx_info
,
1951 /* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
1952 USB_DEVICE(0x0424, 0x9904),
1953 .driver_info
= (unsigned long) &smsc95xx_info
,
1956 /* SMSC9500A USB Ethernet Device (HAL) */
1957 USB_DEVICE(0x0424, 0x9905),
1958 .driver_info
= (unsigned long) &smsc95xx_info
,
1961 /* SMSC9505A USB Ethernet Device (HAL) */
1962 USB_DEVICE(0x0424, 0x9906),
1963 .driver_info
= (unsigned long) &smsc95xx_info
,
1966 /* SMSC9500 USB Ethernet Device (Alternate ID) */
1967 USB_DEVICE(0x0424, 0x9907),
1968 .driver_info
= (unsigned long) &smsc95xx_info
,
1971 /* SMSC9500A USB Ethernet Device (Alternate ID) */
1972 USB_DEVICE(0x0424, 0x9908),
1973 .driver_info
= (unsigned long) &smsc95xx_info
,
1976 /* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
1977 USB_DEVICE(0x0424, 0x9909),
1978 .driver_info
= (unsigned long) &smsc95xx_info
,
1981 /* SMSC LAN9530 USB Ethernet Device */
1982 USB_DEVICE(0x0424, 0x9530),
1983 .driver_info
= (unsigned long) &smsc95xx_info
,
1986 /* SMSC LAN9730 USB Ethernet Device */
1987 USB_DEVICE(0x0424, 0x9730),
1988 .driver_info
= (unsigned long) &smsc95xx_info
,
1991 /* SMSC LAN89530 USB Ethernet Device */
1992 USB_DEVICE(0x0424, 0x9E08),
1993 .driver_info
= (unsigned long) &smsc95xx_info
,
1997 MODULE_DEVICE_TABLE(usb
, products
);
1999 static struct usb_driver smsc95xx_driver
= {
2001 .id_table
= products
,
2002 .probe
= usbnet_probe
,
2003 .suspend
= smsc95xx_suspend
,
2004 .resume
= smsc95xx_resume
,
2005 .reset_resume
= smsc95xx_resume
,
2006 .disconnect
= usbnet_disconnect
,
2007 .disable_hub_initiated_lpm
= 1,
2008 .supports_autosuspend
= 1,
2011 module_usb_driver(smsc95xx_driver
);
2013 MODULE_AUTHOR("Nancy Lin");
2014 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
2015 MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
2016 MODULE_LICENSE("GPL");