xref: /linux/drivers/net/usb/smsc95xx.c (revision 3b14692c01e0b68f0ce51138dfcea967a63cd7bb)
12f7ca802SSteve Glendinning  /***************************************************************************
22f7ca802SSteve Glendinning  *
32f7ca802SSteve Glendinning  * Copyright (C) 2007-2008 SMSC
42f7ca802SSteve Glendinning  *
52f7ca802SSteve Glendinning  * This program is free software; you can redistribute it and/or
62f7ca802SSteve Glendinning  * modify it under the terms of the GNU General Public License
72f7ca802SSteve Glendinning  * as published by the Free Software Foundation; either version 2
82f7ca802SSteve Glendinning  * of the License, or (at your option) any later version.
92f7ca802SSteve Glendinning  *
102f7ca802SSteve Glendinning  * This program is distributed in the hope that it will be useful,
112f7ca802SSteve Glendinning  * but WITHOUT ANY WARRANTY; without even the implied warranty of
122f7ca802SSteve Glendinning  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
132f7ca802SSteve Glendinning  * GNU General Public License for more details.
142f7ca802SSteve Glendinning  *
152f7ca802SSteve Glendinning  * You should have received a copy of the GNU General Public License
162f7ca802SSteve Glendinning  * along with this program; if not, write to the Free Software
172f7ca802SSteve Glendinning  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
182f7ca802SSteve Glendinning  *
192f7ca802SSteve Glendinning  *****************************************************************************/
202f7ca802SSteve Glendinning 
212f7ca802SSteve Glendinning #include <linux/module.h>
222f7ca802SSteve Glendinning #include <linux/kmod.h>
232f7ca802SSteve Glendinning #include <linux/init.h>
242f7ca802SSteve Glendinning #include <linux/netdevice.h>
252f7ca802SSteve Glendinning #include <linux/etherdevice.h>
262f7ca802SSteve Glendinning #include <linux/ethtool.h>
272f7ca802SSteve Glendinning #include <linux/mii.h>
282f7ca802SSteve Glendinning #include <linux/usb.h>
29bbd9f9eeSSteve Glendinning #include <linux/bitrev.h>
30bbd9f9eeSSteve Glendinning #include <linux/crc16.h>
312f7ca802SSteve Glendinning #include <linux/crc32.h>
322f7ca802SSteve Glendinning #include <linux/usb/usbnet.h>
335a0e3ad6STejun Heo #include <linux/slab.h>
342f7ca802SSteve Glendinning #include "smsc95xx.h"
352f7ca802SSteve Glendinning 
362f7ca802SSteve Glendinning #define SMSC_CHIPNAME			"smsc95xx"
37f7b29271SSteve Glendinning #define SMSC_DRIVER_VERSION		"1.0.4"
382f7ca802SSteve Glendinning #define HS_USB_PKT_SIZE			(512)
392f7ca802SSteve Glendinning #define FS_USB_PKT_SIZE			(64)
402f7ca802SSteve Glendinning #define DEFAULT_HS_BURST_CAP_SIZE	(16 * 1024 + 5 * HS_USB_PKT_SIZE)
412f7ca802SSteve Glendinning #define DEFAULT_FS_BURST_CAP_SIZE	(6 * 1024 + 33 * FS_USB_PKT_SIZE)
422f7ca802SSteve Glendinning #define DEFAULT_BULK_IN_DELAY		(0x00002000)
432f7ca802SSteve Glendinning #define MAX_SINGLE_PACKET_SIZE		(2048)
442f7ca802SSteve Glendinning #define LAN95XX_EEPROM_MAGIC		(0x9500)
452f7ca802SSteve Glendinning #define EEPROM_MAC_OFFSET		(0x01)
46f7b29271SSteve Glendinning #define DEFAULT_TX_CSUM_ENABLE		(true)
472f7ca802SSteve Glendinning #define DEFAULT_RX_CSUM_ENABLE		(true)
482f7ca802SSteve Glendinning #define SMSC95XX_INTERNAL_PHY_ID	(1)
492f7ca802SSteve Glendinning #define SMSC95XX_TX_OVERHEAD		(8)
50f7b29271SSteve Glendinning #define SMSC95XX_TX_OVERHEAD_CSUM	(12)
51e5e3af83SSteve Glendinning #define SUPPORTED_WAKE			(WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
52bbd9f9eeSSteve Glendinning 					 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
532f7ca802SSteve Glendinning 
549ebca507SSteve Glendinning #define FEATURE_8_WAKEUP_FILTERS	(0x01)
559ebca507SSteve Glendinning #define FEATURE_PHY_NLP_CROSSOVER	(0x02)
569ebca507SSteve Glendinning #define FEATURE_AUTOSUSPEND		(0x04)
579ebca507SSteve Glendinning 
58769ea6d8SSteve Glendinning #define check_warn(ret, fmt, args...) \
59769ea6d8SSteve Glendinning 	({ if (ret < 0) netdev_warn(dev->net, fmt, ##args); })
60769ea6d8SSteve Glendinning 
61769ea6d8SSteve Glendinning #define check_warn_return(ret, fmt, args...) \
62769ea6d8SSteve Glendinning 	({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); return ret; } })
63769ea6d8SSteve Glendinning 
64769ea6d8SSteve Glendinning #define check_warn_goto_done(ret, fmt, args...) \
65769ea6d8SSteve Glendinning 	({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); goto done; } })
66769ea6d8SSteve Glendinning 
672f7ca802SSteve Glendinning struct smsc95xx_priv {
682f7ca802SSteve Glendinning 	u32 mac_cr;
693c0f3c60SMarc Zyngier 	u32 hash_hi;
703c0f3c60SMarc Zyngier 	u32 hash_lo;
71e0e474a8SSteve Glendinning 	u32 wolopts;
722f7ca802SSteve Glendinning 	spinlock_t mac_cr_lock;
739ebca507SSteve Glendinning 	u8 features;
742f7ca802SSteve Glendinning };
752f7ca802SSteve Glendinning 
76eb939922SRusty Russell static bool turbo_mode = true;
772f7ca802SSteve Glendinning module_param(turbo_mode, bool, 0644);
782f7ca802SSteve Glendinning MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
792f7ca802SSteve Glendinning 
80ec32115dSMing Lei static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
81ec32115dSMing Lei 					    u32 *data, int in_pm)
822f7ca802SSteve Glendinning {
8372108fd2SMing Lei 	u32 buf;
842f7ca802SSteve Glendinning 	int ret;
85ec32115dSMing Lei 	int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
862f7ca802SSteve Glendinning 
872f7ca802SSteve Glendinning 	BUG_ON(!dev);
882f7ca802SSteve Glendinning 
89ec32115dSMing Lei 	if (!in_pm)
90ec32115dSMing Lei 		fn = usbnet_read_cmd;
91ec32115dSMing Lei 	else
92ec32115dSMing Lei 		fn = usbnet_read_cmd_nopm;
93ec32115dSMing Lei 
94ec32115dSMing Lei 	ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
95ec32115dSMing Lei 		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
9672108fd2SMing Lei 		 0, index, &buf, 4);
972f7ca802SSteve Glendinning 	if (unlikely(ret < 0))
981e1d7412SJoe Perches 		netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
991e1d7412SJoe Perches 			    index, ret);
1002f7ca802SSteve Glendinning 
10172108fd2SMing Lei 	le32_to_cpus(&buf);
10272108fd2SMing Lei 	*data = buf;
1032f7ca802SSteve Glendinning 
1042f7ca802SSteve Glendinning 	return ret;
1052f7ca802SSteve Glendinning }
1062f7ca802SSteve Glendinning 
107ec32115dSMing Lei static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index,
108ec32115dSMing Lei 					     u32 data, int in_pm)
1092f7ca802SSteve Glendinning {
11072108fd2SMing Lei 	u32 buf;
1112f7ca802SSteve Glendinning 	int ret;
112ec32115dSMing Lei 	int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
1132f7ca802SSteve Glendinning 
1142f7ca802SSteve Glendinning 	BUG_ON(!dev);
1152f7ca802SSteve Glendinning 
116ec32115dSMing Lei 	if (!in_pm)
117ec32115dSMing Lei 		fn = usbnet_write_cmd;
118ec32115dSMing Lei 	else
119ec32115dSMing Lei 		fn = usbnet_write_cmd_nopm;
120ec32115dSMing Lei 
12172108fd2SMing Lei 	buf = data;
12272108fd2SMing Lei 	cpu_to_le32s(&buf);
1232f7ca802SSteve Glendinning 
124ec32115dSMing Lei 	ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
125ec32115dSMing Lei 		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
12672108fd2SMing Lei 		 0, index, &buf, 4);
1272f7ca802SSteve Glendinning 	if (unlikely(ret < 0))
1281e1d7412SJoe Perches 		netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
1291e1d7412SJoe Perches 			    index, ret);
1302f7ca802SSteve Glendinning 
1312f7ca802SSteve Glendinning 	return ret;
1322f7ca802SSteve Glendinning }
1332f7ca802SSteve Glendinning 
134ec32115dSMing Lei static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index,
135ec32115dSMing Lei 					       u32 *data)
136ec32115dSMing Lei {
137ec32115dSMing Lei 	return __smsc95xx_read_reg(dev, index, data, 1);
138ec32115dSMing Lei }
139ec32115dSMing Lei 
140ec32115dSMing Lei static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index,
141ec32115dSMing Lei 						u32 data)
142ec32115dSMing Lei {
143ec32115dSMing Lei 	return __smsc95xx_write_reg(dev, index, data, 1);
144ec32115dSMing Lei }
145ec32115dSMing Lei 
146ec32115dSMing Lei static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
147ec32115dSMing Lei 					  u32 *data)
148ec32115dSMing Lei {
149ec32115dSMing Lei 	return __smsc95xx_read_reg(dev, index, data, 0);
150ec32115dSMing Lei }
151ec32115dSMing Lei 
152ec32115dSMing Lei static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
153ec32115dSMing Lei 					   u32 data)
154ec32115dSMing Lei {
155ec32115dSMing Lei 	return __smsc95xx_write_reg(dev, index, data, 0);
156ec32115dSMing Lei }
157e0e474a8SSteve Glendinning 
1582f7ca802SSteve Glendinning /* Loop until the read is completed with timeout
1592f7ca802SSteve Glendinning  * called with phy_mutex held */
160e5e3af83SSteve Glendinning static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
161e5e3af83SSteve Glendinning 						     int in_pm)
1622f7ca802SSteve Glendinning {
1632f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
1642f7ca802SSteve Glendinning 	u32 val;
165769ea6d8SSteve Glendinning 	int ret;
1662f7ca802SSteve Glendinning 
1672f7ca802SSteve Glendinning 	do {
168e5e3af83SSteve Glendinning 		ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
1691e1d7412SJoe Perches 		check_warn_return(ret, "Error reading MII_ACCESS\n");
1702f7ca802SSteve Glendinning 		if (!(val & MII_BUSY_))
1712f7ca802SSteve Glendinning 			return 0;
1722f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
1732f7ca802SSteve Glendinning 
1742f7ca802SSteve Glendinning 	return -EIO;
1752f7ca802SSteve Glendinning }
1762f7ca802SSteve Glendinning 
177e5e3af83SSteve Glendinning static int __smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
178e5e3af83SSteve Glendinning 				int in_pm)
1792f7ca802SSteve Glendinning {
1802f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
1812f7ca802SSteve Glendinning 	u32 val, addr;
182769ea6d8SSteve Glendinning 	int ret;
1832f7ca802SSteve Glendinning 
1842f7ca802SSteve Glendinning 	mutex_lock(&dev->phy_mutex);
1852f7ca802SSteve Glendinning 
1862f7ca802SSteve Glendinning 	/* confirm MII not busy */
187e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
1881e1d7412SJoe Perches 	check_warn_goto_done(ret, "MII is busy in smsc95xx_mdio_read\n");
1892f7ca802SSteve Glendinning 
1902f7ca802SSteve Glendinning 	/* set the address, index & direction (read from PHY) */
1912f7ca802SSteve Glendinning 	phy_id &= dev->mii.phy_id_mask;
1922f7ca802SSteve Glendinning 	idx &= dev->mii.reg_num_mask;
19380928805SSteve Glendinning 	addr = (phy_id << 11) | (idx << 6) | MII_READ_ | MII_BUSY_;
194e5e3af83SSteve Glendinning 	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
1951e1d7412SJoe Perches 	check_warn_goto_done(ret, "Error writing MII_ADDR\n");
1962f7ca802SSteve Glendinning 
197e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
1981e1d7412SJoe Perches 	check_warn_goto_done(ret, "Timed out reading MII reg %02X\n", idx);
199769ea6d8SSteve Glendinning 
200e5e3af83SSteve Glendinning 	ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
2011e1d7412SJoe Perches 	check_warn_goto_done(ret, "Error reading MII_DATA\n");
202769ea6d8SSteve Glendinning 
203769ea6d8SSteve Glendinning 	ret = (u16)(val & 0xFFFF);
204769ea6d8SSteve Glendinning 
205769ea6d8SSteve Glendinning done:
2062f7ca802SSteve Glendinning 	mutex_unlock(&dev->phy_mutex);
207769ea6d8SSteve Glendinning 	return ret;
2082f7ca802SSteve Glendinning }
2092f7ca802SSteve Glendinning 
210e5e3af83SSteve Glendinning static void __smsc95xx_mdio_write(struct net_device *netdev, int phy_id,
211e5e3af83SSteve Glendinning 				  int idx, int regval, int in_pm)
2122f7ca802SSteve Glendinning {
2132f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
2142f7ca802SSteve Glendinning 	u32 val, addr;
215769ea6d8SSteve Glendinning 	int ret;
2162f7ca802SSteve Glendinning 
2172f7ca802SSteve Glendinning 	mutex_lock(&dev->phy_mutex);
2182f7ca802SSteve Glendinning 
2192f7ca802SSteve Glendinning 	/* confirm MII not busy */
220e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
2211e1d7412SJoe Perches 	check_warn_goto_done(ret, "MII is busy in smsc95xx_mdio_write\n");
2222f7ca802SSteve Glendinning 
2232f7ca802SSteve Glendinning 	val = regval;
224e5e3af83SSteve Glendinning 	ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
2251e1d7412SJoe Perches 	check_warn_goto_done(ret, "Error writing MII_DATA\n");
2262f7ca802SSteve Glendinning 
2272f7ca802SSteve Glendinning 	/* set the address, index & direction (write to PHY) */
2282f7ca802SSteve Glendinning 	phy_id &= dev->mii.phy_id_mask;
2292f7ca802SSteve Glendinning 	idx &= dev->mii.reg_num_mask;
23080928805SSteve Glendinning 	addr = (phy_id << 11) | (idx << 6) | MII_WRITE_ | MII_BUSY_;
231e5e3af83SSteve Glendinning 	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
2321e1d7412SJoe Perches 	check_warn_goto_done(ret, "Error writing MII_ADDR\n");
2332f7ca802SSteve Glendinning 
234e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
2351e1d7412SJoe Perches 	check_warn_goto_done(ret, "Timed out writing MII reg %02X\n", idx);
2362f7ca802SSteve Glendinning 
237769ea6d8SSteve Glendinning done:
2382f7ca802SSteve Glendinning 	mutex_unlock(&dev->phy_mutex);
2392f7ca802SSteve Glendinning }
2402f7ca802SSteve Glendinning 
241e5e3af83SSteve Glendinning static int smsc95xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
242e5e3af83SSteve Glendinning 				   int idx)
243e5e3af83SSteve Glendinning {
244e5e3af83SSteve Glendinning 	return __smsc95xx_mdio_read(netdev, phy_id, idx, 1);
245e5e3af83SSteve Glendinning }
246e5e3af83SSteve Glendinning 
247e5e3af83SSteve Glendinning static void smsc95xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
248e5e3af83SSteve Glendinning 				     int idx, int regval)
249e5e3af83SSteve Glendinning {
250e5e3af83SSteve Glendinning 	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 1);
251e5e3af83SSteve Glendinning }
252e5e3af83SSteve Glendinning 
253e5e3af83SSteve Glendinning static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
254e5e3af83SSteve Glendinning {
255e5e3af83SSteve Glendinning 	return __smsc95xx_mdio_read(netdev, phy_id, idx, 0);
256e5e3af83SSteve Glendinning }
257e5e3af83SSteve Glendinning 
258e5e3af83SSteve Glendinning static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
259e5e3af83SSteve Glendinning 				int regval)
260e5e3af83SSteve Glendinning {
261e5e3af83SSteve Glendinning 	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 0);
262e5e3af83SSteve Glendinning }
263e5e3af83SSteve Glendinning 
264769ea6d8SSteve Glendinning static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
2652f7ca802SSteve Glendinning {
2662f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
2672f7ca802SSteve Glendinning 	u32 val;
268769ea6d8SSteve Glendinning 	int ret;
2692f7ca802SSteve Glendinning 
2702f7ca802SSteve Glendinning 	do {
271769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
2721e1d7412SJoe Perches 		check_warn_return(ret, "Error reading E2P_CMD\n");
2732f7ca802SSteve Glendinning 		if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
2742f7ca802SSteve Glendinning 			break;
2752f7ca802SSteve Glendinning 		udelay(40);
2762f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
2772f7ca802SSteve Glendinning 
2782f7ca802SSteve Glendinning 	if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
27960b86755SJoe Perches 		netdev_warn(dev->net, "EEPROM read operation timeout\n");
2802f7ca802SSteve Glendinning 		return -EIO;
2812f7ca802SSteve Glendinning 	}
2822f7ca802SSteve Glendinning 
2832f7ca802SSteve Glendinning 	return 0;
2842f7ca802SSteve Glendinning }
2852f7ca802SSteve Glendinning 
286769ea6d8SSteve Glendinning static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
2872f7ca802SSteve Glendinning {
2882f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
2892f7ca802SSteve Glendinning 	u32 val;
290769ea6d8SSteve Glendinning 	int ret;
2912f7ca802SSteve Glendinning 
2922f7ca802SSteve Glendinning 	do {
293769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
2941e1d7412SJoe Perches 		check_warn_return(ret, "Error reading E2P_CMD\n");
2952f7ca802SSteve Glendinning 
2962f7ca802SSteve Glendinning 		if (!(val & E2P_CMD_BUSY_))
2972f7ca802SSteve Glendinning 			return 0;
2982f7ca802SSteve Glendinning 
2992f7ca802SSteve Glendinning 		udelay(40);
3002f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
3012f7ca802SSteve Glendinning 
30260b86755SJoe Perches 	netdev_warn(dev->net, "EEPROM is busy\n");
3032f7ca802SSteve Glendinning 	return -EIO;
3042f7ca802SSteve Glendinning }
3052f7ca802SSteve Glendinning 
3062f7ca802SSteve Glendinning static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
3072f7ca802SSteve Glendinning 				u8 *data)
3082f7ca802SSteve Glendinning {
3092f7ca802SSteve Glendinning 	u32 val;
3102f7ca802SSteve Glendinning 	int i, ret;
3112f7ca802SSteve Glendinning 
3122f7ca802SSteve Glendinning 	BUG_ON(!dev);
3132f7ca802SSteve Glendinning 	BUG_ON(!data);
3142f7ca802SSteve Glendinning 
3152f7ca802SSteve Glendinning 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
3162f7ca802SSteve Glendinning 	if (ret)
3172f7ca802SSteve Glendinning 		return ret;
3182f7ca802SSteve Glendinning 
3192f7ca802SSteve Glendinning 	for (i = 0; i < length; i++) {
3202f7ca802SSteve Glendinning 		val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
321769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
3221e1d7412SJoe Perches 		check_warn_return(ret, "Error writing E2P_CMD\n");
3232f7ca802SSteve Glendinning 
3242f7ca802SSteve Glendinning 		ret = smsc95xx_wait_eeprom(dev);
3252f7ca802SSteve Glendinning 		if (ret < 0)
3262f7ca802SSteve Glendinning 			return ret;
3272f7ca802SSteve Glendinning 
328769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
3291e1d7412SJoe Perches 		check_warn_return(ret, "Error reading E2P_DATA\n");
3302f7ca802SSteve Glendinning 
3312f7ca802SSteve Glendinning 		data[i] = val & 0xFF;
3322f7ca802SSteve Glendinning 		offset++;
3332f7ca802SSteve Glendinning 	}
3342f7ca802SSteve Glendinning 
3352f7ca802SSteve Glendinning 	return 0;
3362f7ca802SSteve Glendinning }
3372f7ca802SSteve Glendinning 
3382f7ca802SSteve Glendinning static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
3392f7ca802SSteve Glendinning 				 u8 *data)
3402f7ca802SSteve Glendinning {
3412f7ca802SSteve Glendinning 	u32 val;
3422f7ca802SSteve Glendinning 	int i, ret;
3432f7ca802SSteve Glendinning 
3442f7ca802SSteve Glendinning 	BUG_ON(!dev);
3452f7ca802SSteve Glendinning 	BUG_ON(!data);
3462f7ca802SSteve Glendinning 
3472f7ca802SSteve Glendinning 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
3482f7ca802SSteve Glendinning 	if (ret)
3492f7ca802SSteve Glendinning 		return ret;
3502f7ca802SSteve Glendinning 
3512f7ca802SSteve Glendinning 	/* Issue write/erase enable command */
3522f7ca802SSteve Glendinning 	val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
353769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, E2P_CMD, val);
3541e1d7412SJoe Perches 	check_warn_return(ret, "Error writing E2P_DATA\n");
3552f7ca802SSteve Glendinning 
3562f7ca802SSteve Glendinning 	ret = smsc95xx_wait_eeprom(dev);
3572f7ca802SSteve Glendinning 	if (ret < 0)
3582f7ca802SSteve Glendinning 		return ret;
3592f7ca802SSteve Glendinning 
3602f7ca802SSteve Glendinning 	for (i = 0; i < length; i++) {
3612f7ca802SSteve Glendinning 
3622f7ca802SSteve Glendinning 		/* Fill data register */
3632f7ca802SSteve Glendinning 		val = data[i];
364769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_DATA, val);
3651e1d7412SJoe Perches 		check_warn_return(ret, "Error writing E2P_DATA\n");
3662f7ca802SSteve Glendinning 
3672f7ca802SSteve Glendinning 		/* Send "write" command */
3682f7ca802SSteve Glendinning 		val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
369769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
3701e1d7412SJoe Perches 		check_warn_return(ret, "Error writing E2P_CMD\n");
3712f7ca802SSteve Glendinning 
3722f7ca802SSteve Glendinning 		ret = smsc95xx_wait_eeprom(dev);
3732f7ca802SSteve Glendinning 		if (ret < 0)
3742f7ca802SSteve Glendinning 			return ret;
3752f7ca802SSteve Glendinning 
3762f7ca802SSteve Glendinning 		offset++;
3772f7ca802SSteve Glendinning 	}
3782f7ca802SSteve Glendinning 
3792f7ca802SSteve Glendinning 	return 0;
3802f7ca802SSteve Glendinning }
3812f7ca802SSteve Glendinning 
382769ea6d8SSteve Glendinning static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
383769ea6d8SSteve Glendinning 						 u32 *data)
3842f7ca802SSteve Glendinning {
3851d74a6bdSSteve Glendinning 	const u16 size = 4;
38672108fd2SMing Lei 	int ret;
3872f7ca802SSteve Glendinning 
38872108fd2SMing Lei 	ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
38972108fd2SMing Lei 				     USB_DIR_OUT | USB_TYPE_VENDOR |
39072108fd2SMing Lei 				     USB_RECIP_DEVICE,
39172108fd2SMing Lei 				     0, index, data, size);
39272108fd2SMing Lei 	if (ret < 0)
39372108fd2SMing Lei 		netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
39472108fd2SMing Lei 			    ret);
39572108fd2SMing Lei 	return ret;
3962f7ca802SSteve Glendinning }
3972f7ca802SSteve Glendinning 
3982f7ca802SSteve Glendinning /* returns hash bit number for given MAC address
3992f7ca802SSteve Glendinning  * example:
4002f7ca802SSteve Glendinning  * 01 00 5E 00 00 01 -> returns bit number 31 */
4012f7ca802SSteve Glendinning static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
4022f7ca802SSteve Glendinning {
4032f7ca802SSteve Glendinning 	return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
4042f7ca802SSteve Glendinning }
4052f7ca802SSteve Glendinning 
4062f7ca802SSteve Glendinning static void smsc95xx_set_multicast(struct net_device *netdev)
4072f7ca802SSteve Glendinning {
4082f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
4092f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
4102f7ca802SSteve Glendinning 	unsigned long flags;
411769ea6d8SSteve Glendinning 	int ret;
4122f7ca802SSteve Glendinning 
4133c0f3c60SMarc Zyngier 	pdata->hash_hi = 0;
4143c0f3c60SMarc Zyngier 	pdata->hash_lo = 0;
4153c0f3c60SMarc Zyngier 
4162f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
4172f7ca802SSteve Glendinning 
4182f7ca802SSteve Glendinning 	if (dev->net->flags & IFF_PROMISC) {
419a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
4202f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_PRMS_;
4212f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
4222f7ca802SSteve Glendinning 	} else if (dev->net->flags & IFF_ALLMULTI) {
423a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
4242f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_MCPAS_;
4252f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
4264cd24eafSJiri Pirko 	} else if (!netdev_mc_empty(dev->net)) {
42722bedad3SJiri Pirko 		struct netdev_hw_addr *ha;
4282f7ca802SSteve Glendinning 
4292f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_HPFILT_;
4302f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
4312f7ca802SSteve Glendinning 
43222bedad3SJiri Pirko 		netdev_for_each_mc_addr(ha, netdev) {
43322bedad3SJiri Pirko 			u32 bitnum = smsc95xx_hash(ha->addr);
4342f7ca802SSteve Glendinning 			u32 mask = 0x01 << (bitnum & 0x1F);
4352f7ca802SSteve Glendinning 			if (bitnum & 0x20)
4363c0f3c60SMarc Zyngier 				pdata->hash_hi |= mask;
4372f7ca802SSteve Glendinning 			else
4383c0f3c60SMarc Zyngier 				pdata->hash_lo |= mask;
4392f7ca802SSteve Glendinning 		}
4402f7ca802SSteve Glendinning 
441a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
4423c0f3c60SMarc Zyngier 				   pdata->hash_hi, pdata->hash_lo);
4432f7ca802SSteve Glendinning 	} else {
444a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "receive own packets only\n");
4452f7ca802SSteve Glendinning 		pdata->mac_cr &=
4462f7ca802SSteve Glendinning 			~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
4472f7ca802SSteve Glendinning 	}
4482f7ca802SSteve Glendinning 
4492f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
4502f7ca802SSteve Glendinning 
4512f7ca802SSteve Glendinning 	/* Initiate async writes, as we can't wait for completion here */
452769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, HASHH, &pdata->hash_hi);
4531e1d7412SJoe Perches 	check_warn(ret, "failed to initiate async write to HASHH\n");
454769ea6d8SSteve Glendinning 
455769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, HASHL, &pdata->hash_lo);
4561e1d7412SJoe Perches 	check_warn(ret, "failed to initiate async write to HASHL\n");
457769ea6d8SSteve Glendinning 
458769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, MAC_CR, &pdata->mac_cr);
4591e1d7412SJoe Perches 	check_warn(ret, "failed to initiate async write to MAC_CR\n");
4602f7ca802SSteve Glendinning }
4612f7ca802SSteve Glendinning 
462769ea6d8SSteve Glendinning static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
4632f7ca802SSteve Glendinning 					   u16 lcladv, u16 rmtadv)
4642f7ca802SSteve Glendinning {
4652f7ca802SSteve Glendinning 	u32 flow, afc_cfg = 0;
4662f7ca802SSteve Glendinning 
4672f7ca802SSteve Glendinning 	int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
4681e1d7412SJoe Perches 	check_warn_return(ret, "Error reading AFC_CFG\n");
4692f7ca802SSteve Glendinning 
4702f7ca802SSteve Glendinning 	if (duplex == DUPLEX_FULL) {
471bc02ff95SSteve Glendinning 		u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
4722f7ca802SSteve Glendinning 
4732f7ca802SSteve Glendinning 		if (cap & FLOW_CTRL_RX)
4742f7ca802SSteve Glendinning 			flow = 0xFFFF0002;
4752f7ca802SSteve Glendinning 		else
4762f7ca802SSteve Glendinning 			flow = 0;
4772f7ca802SSteve Glendinning 
4782f7ca802SSteve Glendinning 		if (cap & FLOW_CTRL_TX)
4792f7ca802SSteve Glendinning 			afc_cfg |= 0xF;
4802f7ca802SSteve Glendinning 		else
4812f7ca802SSteve Glendinning 			afc_cfg &= ~0xF;
4822f7ca802SSteve Glendinning 
483a475f603SJoe Perches 		netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
48460b86755SJoe Perches 				   cap & FLOW_CTRL_RX ? "enabled" : "disabled",
48560b86755SJoe Perches 				   cap & FLOW_CTRL_TX ? "enabled" : "disabled");
4862f7ca802SSteve Glendinning 	} else {
487a475f603SJoe Perches 		netif_dbg(dev, link, dev->net, "half duplex\n");
4882f7ca802SSteve Glendinning 		flow = 0;
4892f7ca802SSteve Glendinning 		afc_cfg |= 0xF;
4902f7ca802SSteve Glendinning 	}
4912f7ca802SSteve Glendinning 
492769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, FLOW, flow);
4931e1d7412SJoe Perches 	check_warn_return(ret, "Error writing FLOW\n");
494769ea6d8SSteve Glendinning 
495769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
4961e1d7412SJoe Perches 	check_warn_return(ret, "Error writing AFC_CFG\n");
497769ea6d8SSteve Glendinning 
498769ea6d8SSteve Glendinning 	return 0;
4992f7ca802SSteve Glendinning }
5002f7ca802SSteve Glendinning 
5012f7ca802SSteve Glendinning static int smsc95xx_link_reset(struct usbnet *dev)
5022f7ca802SSteve Glendinning {
5032f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
5042f7ca802SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
5058ae6dacaSDavid Decotigny 	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
5062f7ca802SSteve Glendinning 	unsigned long flags;
5072f7ca802SSteve Glendinning 	u16 lcladv, rmtadv;
508769ea6d8SSteve Glendinning 	int ret;
5092f7ca802SSteve Glendinning 
5102f7ca802SSteve Glendinning 	/* clear interrupt status */
511769ea6d8SSteve Glendinning 	ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
5121e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PHY_INT_SRC\n");
513769ea6d8SSteve Glendinning 
514769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
5151e1d7412SJoe Perches 	check_warn_return(ret, "Error writing INT_STS\n");
5162f7ca802SSteve Glendinning 
5172f7ca802SSteve Glendinning 	mii_check_media(mii, 1, 1);
5182f7ca802SSteve Glendinning 	mii_ethtool_gset(&dev->mii, &ecmd);
5192f7ca802SSteve Glendinning 	lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
5202f7ca802SSteve Glendinning 	rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
5212f7ca802SSteve Glendinning 
5228ae6dacaSDavid Decotigny 	netif_dbg(dev, link, dev->net,
5238ae6dacaSDavid Decotigny 		  "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
5248ae6dacaSDavid Decotigny 		  ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
5252f7ca802SSteve Glendinning 
5262f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
5272f7ca802SSteve Glendinning 	if (ecmd.duplex != DUPLEX_FULL) {
5282f7ca802SSteve Glendinning 		pdata->mac_cr &= ~MAC_CR_FDPX_;
5292f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_RCVOWN_;
5302f7ca802SSteve Glendinning 	} else {
5312f7ca802SSteve Glendinning 		pdata->mac_cr &= ~MAC_CR_RCVOWN_;
5322f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_FDPX_;
5332f7ca802SSteve Glendinning 	}
5342f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
5352f7ca802SSteve Glendinning 
536769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
5371e1d7412SJoe Perches 	check_warn_return(ret, "Error writing MAC_CR\n");
5382f7ca802SSteve Glendinning 
539769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
5401e1d7412SJoe Perches 	check_warn_return(ret, "Error updating PHY flow control\n");
5412f7ca802SSteve Glendinning 
5422f7ca802SSteve Glendinning 	return 0;
5432f7ca802SSteve Glendinning }
5442f7ca802SSteve Glendinning 
5452f7ca802SSteve Glendinning static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
5462f7ca802SSteve Glendinning {
5472f7ca802SSteve Glendinning 	u32 intdata;
5482f7ca802SSteve Glendinning 
5492f7ca802SSteve Glendinning 	if (urb->actual_length != 4) {
55060b86755SJoe Perches 		netdev_warn(dev->net, "unexpected urb length %d\n",
55160b86755SJoe Perches 			    urb->actual_length);
5522f7ca802SSteve Glendinning 		return;
5532f7ca802SSteve Glendinning 	}
5542f7ca802SSteve Glendinning 
5552f7ca802SSteve Glendinning 	memcpy(&intdata, urb->transfer_buffer, 4);
5561d74a6bdSSteve Glendinning 	le32_to_cpus(&intdata);
5572f7ca802SSteve Glendinning 
558a475f603SJoe Perches 	netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
5592f7ca802SSteve Glendinning 
5602f7ca802SSteve Glendinning 	if (intdata & INT_ENP_PHY_INT_)
5612f7ca802SSteve Glendinning 		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
5622f7ca802SSteve Glendinning 	else
56360b86755SJoe Perches 		netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
56460b86755SJoe Perches 			    intdata);
5652f7ca802SSteve Glendinning }
5662f7ca802SSteve Glendinning 
567f7b29271SSteve Glendinning /* Enable or disable Tx & Rx checksum offload engines */
568c8f44affSMichał Mirosław static int smsc95xx_set_features(struct net_device *netdev,
569c8f44affSMichał Mirosław 	netdev_features_t features)
5702f7ca802SSteve Glendinning {
57178e47fe4SMichał Mirosław 	struct usbnet *dev = netdev_priv(netdev);
5722f7ca802SSteve Glendinning 	u32 read_buf;
57378e47fe4SMichał Mirosław 	int ret;
57478e47fe4SMichał Mirosław 
57578e47fe4SMichał Mirosław 	ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
576769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read COE_CR: %d\n", ret);
5772f7ca802SSteve Glendinning 
57878e47fe4SMichał Mirosław 	if (features & NETIF_F_HW_CSUM)
579f7b29271SSteve Glendinning 		read_buf |= Tx_COE_EN_;
580f7b29271SSteve Glendinning 	else
581f7b29271SSteve Glendinning 		read_buf &= ~Tx_COE_EN_;
582f7b29271SSteve Glendinning 
58378e47fe4SMichał Mirosław 	if (features & NETIF_F_RXCSUM)
5842f7ca802SSteve Glendinning 		read_buf |= Rx_COE_EN_;
5852f7ca802SSteve Glendinning 	else
5862f7ca802SSteve Glendinning 		read_buf &= ~Rx_COE_EN_;
5872f7ca802SSteve Glendinning 
5882f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
589769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write COE_CR: %d\n", ret);
5902f7ca802SSteve Glendinning 
591a475f603SJoe Perches 	netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
5922f7ca802SSteve Glendinning 	return 0;
5932f7ca802SSteve Glendinning }
5942f7ca802SSteve Glendinning 
5952f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
5962f7ca802SSteve Glendinning {
5972f7ca802SSteve Glendinning 	return MAX_EEPROM_SIZE;
5982f7ca802SSteve Glendinning }
5992f7ca802SSteve Glendinning 
6002f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
6012f7ca802SSteve Glendinning 				       struct ethtool_eeprom *ee, u8 *data)
6022f7ca802SSteve Glendinning {
6032f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
6042f7ca802SSteve Glendinning 
6052f7ca802SSteve Glendinning 	ee->magic = LAN95XX_EEPROM_MAGIC;
6062f7ca802SSteve Glendinning 
6072f7ca802SSteve Glendinning 	return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
6082f7ca802SSteve Glendinning }
6092f7ca802SSteve Glendinning 
6102f7ca802SSteve Glendinning static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
6112f7ca802SSteve Glendinning 				       struct ethtool_eeprom *ee, u8 *data)
6122f7ca802SSteve Glendinning {
6132f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
6142f7ca802SSteve Glendinning 
6152f7ca802SSteve Glendinning 	if (ee->magic != LAN95XX_EEPROM_MAGIC) {
61660b86755SJoe Perches 		netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
6172f7ca802SSteve Glendinning 			    ee->magic);
6182f7ca802SSteve Glendinning 		return -EINVAL;
6192f7ca802SSteve Glendinning 	}
6202f7ca802SSteve Glendinning 
6212f7ca802SSteve Glendinning 	return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
6222f7ca802SSteve Glendinning }
6232f7ca802SSteve Glendinning 
6249fa32e94SEmeric Vigier static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
6259fa32e94SEmeric Vigier {
6269fa32e94SEmeric Vigier 	/* all smsc95xx registers */
6279fa32e94SEmeric Vigier 	return COE_CR - ID_REV + 1;
6289fa32e94SEmeric Vigier }
6299fa32e94SEmeric Vigier 
6309fa32e94SEmeric Vigier static void
6319fa32e94SEmeric Vigier smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
6329fa32e94SEmeric Vigier 			 void *buf)
6339fa32e94SEmeric Vigier {
6349fa32e94SEmeric Vigier 	struct usbnet *dev = netdev_priv(netdev);
635d348446bSDan Carpenter 	unsigned int i, j;
636d348446bSDan Carpenter 	int retval;
6379fa32e94SEmeric Vigier 	u32 *data = buf;
6389fa32e94SEmeric Vigier 
6399fa32e94SEmeric Vigier 	retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
6409fa32e94SEmeric Vigier 	if (retval < 0) {
6419fa32e94SEmeric Vigier 		netdev_warn(netdev, "REGS: cannot read ID_REV\n");
6429fa32e94SEmeric Vigier 		return;
6439fa32e94SEmeric Vigier 	}
6449fa32e94SEmeric Vigier 
6459fa32e94SEmeric Vigier 	for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
6469fa32e94SEmeric Vigier 		retval = smsc95xx_read_reg(dev, i, &data[j]);
6479fa32e94SEmeric Vigier 		if (retval < 0) {
6489fa32e94SEmeric Vigier 			netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
6499fa32e94SEmeric Vigier 			return;
6509fa32e94SEmeric Vigier 		}
6519fa32e94SEmeric Vigier 	}
6529fa32e94SEmeric Vigier }
6539fa32e94SEmeric Vigier 
654e0e474a8SSteve Glendinning static void smsc95xx_ethtool_get_wol(struct net_device *net,
655e0e474a8SSteve Glendinning 				     struct ethtool_wolinfo *wolinfo)
656e0e474a8SSteve Glendinning {
657e0e474a8SSteve Glendinning 	struct usbnet *dev = netdev_priv(net);
658e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
659e0e474a8SSteve Glendinning 
660e0e474a8SSteve Glendinning 	wolinfo->supported = SUPPORTED_WAKE;
661e0e474a8SSteve Glendinning 	wolinfo->wolopts = pdata->wolopts;
662e0e474a8SSteve Glendinning }
663e0e474a8SSteve Glendinning 
664e0e474a8SSteve Glendinning static int smsc95xx_ethtool_set_wol(struct net_device *net,
665e0e474a8SSteve Glendinning 				    struct ethtool_wolinfo *wolinfo)
666e0e474a8SSteve Glendinning {
667e0e474a8SSteve Glendinning 	struct usbnet *dev = netdev_priv(net);
668e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
669*3b14692cSSteve Glendinning 	int ret;
670e0e474a8SSteve Glendinning 
671e0e474a8SSteve Glendinning 	pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
672*3b14692cSSteve Glendinning 
673*3b14692cSSteve Glendinning 	ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
674*3b14692cSSteve Glendinning 	check_warn_return(ret, "device_set_wakeup_enable error %d\n", ret);
675*3b14692cSSteve Glendinning 
676e0e474a8SSteve Glendinning 	return 0;
677e0e474a8SSteve Glendinning }
678e0e474a8SSteve Glendinning 
6790fc0b732SStephen Hemminger static const struct ethtool_ops smsc95xx_ethtool_ops = {
6802f7ca802SSteve Glendinning 	.get_link	= usbnet_get_link,
6812f7ca802SSteve Glendinning 	.nway_reset	= usbnet_nway_reset,
6822f7ca802SSteve Glendinning 	.get_drvinfo	= usbnet_get_drvinfo,
6832f7ca802SSteve Glendinning 	.get_msglevel	= usbnet_get_msglevel,
6842f7ca802SSteve Glendinning 	.set_msglevel	= usbnet_set_msglevel,
6852f7ca802SSteve Glendinning 	.get_settings	= usbnet_get_settings,
6862f7ca802SSteve Glendinning 	.set_settings	= usbnet_set_settings,
6872f7ca802SSteve Glendinning 	.get_eeprom_len	= smsc95xx_ethtool_get_eeprom_len,
6882f7ca802SSteve Glendinning 	.get_eeprom	= smsc95xx_ethtool_get_eeprom,
6892f7ca802SSteve Glendinning 	.set_eeprom	= smsc95xx_ethtool_set_eeprom,
6909fa32e94SEmeric Vigier 	.get_regs_len	= smsc95xx_ethtool_getregslen,
6919fa32e94SEmeric Vigier 	.get_regs	= smsc95xx_ethtool_getregs,
692e0e474a8SSteve Glendinning 	.get_wol	= smsc95xx_ethtool_get_wol,
693e0e474a8SSteve Glendinning 	.set_wol	= smsc95xx_ethtool_set_wol,
6942f7ca802SSteve Glendinning };
6952f7ca802SSteve Glendinning 
6962f7ca802SSteve Glendinning static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
6972f7ca802SSteve Glendinning {
6982f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
6992f7ca802SSteve Glendinning 
7002f7ca802SSteve Glendinning 	if (!netif_running(netdev))
7012f7ca802SSteve Glendinning 		return -EINVAL;
7022f7ca802SSteve Glendinning 
7032f7ca802SSteve Glendinning 	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
7042f7ca802SSteve Glendinning }
7052f7ca802SSteve Glendinning 
7062f7ca802SSteve Glendinning static void smsc95xx_init_mac_address(struct usbnet *dev)
7072f7ca802SSteve Glendinning {
7082f7ca802SSteve Glendinning 	/* try reading mac address from EEPROM */
7092f7ca802SSteve Glendinning 	if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
7102f7ca802SSteve Glendinning 			dev->net->dev_addr) == 0) {
7112f7ca802SSteve Glendinning 		if (is_valid_ether_addr(dev->net->dev_addr)) {
7122f7ca802SSteve Glendinning 			/* eeprom values are valid so use them */
713a475f603SJoe Perches 			netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
7142f7ca802SSteve Glendinning 			return;
7152f7ca802SSteve Glendinning 		}
7162f7ca802SSteve Glendinning 	}
7172f7ca802SSteve Glendinning 
7182f7ca802SSteve Glendinning 	/* no eeprom, or eeprom values are invalid. generate random MAC */
719f2cedb63SDanny Kukawka 	eth_hw_addr_random(dev->net);
720c7e12eadSJoe Perches 	netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
7212f7ca802SSteve Glendinning }
7222f7ca802SSteve Glendinning 
7232f7ca802SSteve Glendinning static int smsc95xx_set_mac_address(struct usbnet *dev)
7242f7ca802SSteve Glendinning {
7252f7ca802SSteve Glendinning 	u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
7262f7ca802SSteve Glendinning 		dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
7272f7ca802SSteve Glendinning 	u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
7282f7ca802SSteve Glendinning 	int ret;
7292f7ca802SSteve Glendinning 
7302f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
731769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write ADDRL: %d\n", ret);
7322f7ca802SSteve Glendinning 
7332f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, ADDRH, addr_hi);
734769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write ADDRH: %d\n", ret);
7352f7ca802SSteve Glendinning 
7362f7ca802SSteve Glendinning 	return 0;
7372f7ca802SSteve Glendinning }
7382f7ca802SSteve Glendinning 
7392f7ca802SSteve Glendinning /* starts the TX path */
740769ea6d8SSteve Glendinning static int smsc95xx_start_tx_path(struct usbnet *dev)
7412f7ca802SSteve Glendinning {
7422f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
7432f7ca802SSteve Glendinning 	unsigned long flags;
744769ea6d8SSteve Glendinning 	int ret;
7452f7ca802SSteve Glendinning 
7462f7ca802SSteve Glendinning 	/* Enable Tx at MAC */
7472f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
7482f7ca802SSteve Glendinning 	pdata->mac_cr |= MAC_CR_TXEN_;
7492f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
7502f7ca802SSteve Glendinning 
751769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
752769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write MAC_CR: %d\n", ret);
7532f7ca802SSteve Glendinning 
7542f7ca802SSteve Glendinning 	/* Enable Tx at SCSRs */
755769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
756769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write TX_CFG: %d\n", ret);
757769ea6d8SSteve Glendinning 
758769ea6d8SSteve Glendinning 	return 0;
7592f7ca802SSteve Glendinning }
7602f7ca802SSteve Glendinning 
7612f7ca802SSteve Glendinning /* Starts the Receive path */
762ec32115dSMing Lei static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
7632f7ca802SSteve Glendinning {
7642f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
7652f7ca802SSteve Glendinning 	unsigned long flags;
766769ea6d8SSteve Glendinning 	int ret;
7672f7ca802SSteve Glendinning 
7682f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
7692f7ca802SSteve Glendinning 	pdata->mac_cr |= MAC_CR_RXEN_;
7702f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
7712f7ca802SSteve Glendinning 
772ec32115dSMing Lei 	ret = __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
773769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write MAC_CR: %d\n", ret);
774769ea6d8SSteve Glendinning 
775769ea6d8SSteve Glendinning 	return 0;
7762f7ca802SSteve Glendinning }
7772f7ca802SSteve Glendinning 
7782f7ca802SSteve Glendinning static int smsc95xx_phy_initialize(struct usbnet *dev)
7792f7ca802SSteve Glendinning {
780769ea6d8SSteve Glendinning 	int bmcr, ret, timeout = 0;
781db443c44SSteve Glendinning 
7822f7ca802SSteve Glendinning 	/* Initialize MII structure */
7832f7ca802SSteve Glendinning 	dev->mii.dev = dev->net;
7842f7ca802SSteve Glendinning 	dev->mii.mdio_read = smsc95xx_mdio_read;
7852f7ca802SSteve Glendinning 	dev->mii.mdio_write = smsc95xx_mdio_write;
7862f7ca802SSteve Glendinning 	dev->mii.phy_id_mask = 0x1f;
7872f7ca802SSteve Glendinning 	dev->mii.reg_num_mask = 0x1f;
7882f7ca802SSteve Glendinning 	dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
7892f7ca802SSteve Glendinning 
790db443c44SSteve Glendinning 	/* reset phy and wait for reset to complete */
7912f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
792db443c44SSteve Glendinning 
793db443c44SSteve Glendinning 	do {
794db443c44SSteve Glendinning 		msleep(10);
795db443c44SSteve Glendinning 		bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
796db443c44SSteve Glendinning 		timeout++;
797d9460920SRabin Vincent 	} while ((bmcr & BMCR_RESET) && (timeout < 100));
798db443c44SSteve Glendinning 
799db443c44SSteve Glendinning 	if (timeout >= 100) {
800db443c44SSteve Glendinning 		netdev_warn(dev->net, "timeout on PHY Reset");
801db443c44SSteve Glendinning 		return -EIO;
802db443c44SSteve Glendinning 	}
803db443c44SSteve Glendinning 
8042f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
8052f7ca802SSteve Glendinning 		ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
8062f7ca802SSteve Glendinning 		ADVERTISE_PAUSE_ASYM);
8072f7ca802SSteve Glendinning 
8082f7ca802SSteve Glendinning 	/* read to clear */
809769ea6d8SSteve Glendinning 	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
8101e1d7412SJoe Perches 	check_warn_return(ret, "Failed to read PHY_INT_SRC during init\n");
8112f7ca802SSteve Glendinning 
8122f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
8132f7ca802SSteve Glendinning 		PHY_INT_MASK_DEFAULT_);
8142f7ca802SSteve Glendinning 	mii_nway_restart(&dev->mii);
8152f7ca802SSteve Glendinning 
816a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
8172f7ca802SSteve Glendinning 	return 0;
8182f7ca802SSteve Glendinning }
8192f7ca802SSteve Glendinning 
8202f7ca802SSteve Glendinning static int smsc95xx_reset(struct usbnet *dev)
8212f7ca802SSteve Glendinning {
8222f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
8232f7ca802SSteve Glendinning 	u32 read_buf, write_buf, burst_cap;
8242f7ca802SSteve Glendinning 	int ret = 0, timeout;
8252f7ca802SSteve Glendinning 
826a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
8272f7ca802SSteve Glendinning 
8284436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
829769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write HW_CFG_LRST_ bit in HW_CFG\n");
8302f7ca802SSteve Glendinning 
8312f7ca802SSteve Glendinning 	timeout = 0;
8322f7ca802SSteve Glendinning 	do {
833cf2acec2SSteve Glendinning 		msleep(10);
8342f7ca802SSteve Glendinning 		ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
835769ea6d8SSteve Glendinning 		check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
8362f7ca802SSteve Glendinning 		timeout++;
8372f7ca802SSteve Glendinning 	} while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
8382f7ca802SSteve Glendinning 
8392f7ca802SSteve Glendinning 	if (timeout >= 100) {
84060b86755SJoe Perches 		netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
8412f7ca802SSteve Glendinning 		return ret;
8422f7ca802SSteve Glendinning 	}
8432f7ca802SSteve Glendinning 
8444436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
845769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write PM_CTRL: %d\n", ret);
8462f7ca802SSteve Glendinning 
8472f7ca802SSteve Glendinning 	timeout = 0;
8482f7ca802SSteve Glendinning 	do {
849cf2acec2SSteve Glendinning 		msleep(10);
8502f7ca802SSteve Glendinning 		ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
851769ea6d8SSteve Glendinning 		check_warn_return(ret, "Failed to read PM_CTRL: %d\n", ret);
8522f7ca802SSteve Glendinning 		timeout++;
8532f7ca802SSteve Glendinning 	} while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
8542f7ca802SSteve Glendinning 
8552f7ca802SSteve Glendinning 	if (timeout >= 100) {
85660b86755SJoe Perches 		netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
8572f7ca802SSteve Glendinning 		return ret;
8582f7ca802SSteve Glendinning 	}
8592f7ca802SSteve Glendinning 
8602f7ca802SSteve Glendinning 	ret = smsc95xx_set_mac_address(dev);
8612f7ca802SSteve Glendinning 	if (ret < 0)
8622f7ca802SSteve Glendinning 		return ret;
8632f7ca802SSteve Glendinning 
8641e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
8651e1d7412SJoe Perches 		  dev->net->dev_addr);
8662f7ca802SSteve Glendinning 
8672f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
868769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
8692f7ca802SSteve Glendinning 
8701e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
8711e1d7412SJoe Perches 		  read_buf);
8722f7ca802SSteve Glendinning 
8732f7ca802SSteve Glendinning 	read_buf |= HW_CFG_BIR_;
8742f7ca802SSteve Glendinning 
8752f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
876769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write HW_CFG_BIR_ bit in HW_CFG\n");
8772f7ca802SSteve Glendinning 
8782f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
879769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
880a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
881a475f603SJoe Perches 		  "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
88260b86755SJoe Perches 		  read_buf);
8832f7ca802SSteve Glendinning 
8842f7ca802SSteve Glendinning 	if (!turbo_mode) {
8852f7ca802SSteve Glendinning 		burst_cap = 0;
8862f7ca802SSteve Glendinning 		dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
8872f7ca802SSteve Glendinning 	} else if (dev->udev->speed == USB_SPEED_HIGH) {
8882f7ca802SSteve Glendinning 		burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
8892f7ca802SSteve Glendinning 		dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
8902f7ca802SSteve Glendinning 	} else {
8912f7ca802SSteve Glendinning 		burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
8922f7ca802SSteve Glendinning 		dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
8932f7ca802SSteve Glendinning 	}
8942f7ca802SSteve Glendinning 
8951e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
8961e1d7412SJoe Perches 		  (ulong)dev->rx_urb_size);
8972f7ca802SSteve Glendinning 
8982f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
899769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write BURST_CAP: %d\n", ret);
9002f7ca802SSteve Glendinning 
9012f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
902769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read BURST_CAP: %d\n", ret);
903769ea6d8SSteve Glendinning 
904a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
905a475f603SJoe Perches 		  "Read Value from BURST_CAP after writing: 0x%08x\n",
9062f7ca802SSteve Glendinning 		  read_buf);
9072f7ca802SSteve Glendinning 
9084436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
909769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write BULK_IN_DLY: %d\n", ret);
9102f7ca802SSteve Glendinning 
9112f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
912769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read BULK_IN_DLY: %d\n", ret);
913769ea6d8SSteve Glendinning 
914a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
915a475f603SJoe Perches 		  "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
91660b86755SJoe Perches 		  read_buf);
9172f7ca802SSteve Glendinning 
9182f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
919769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
920769ea6d8SSteve Glendinning 
9211e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
9221e1d7412SJoe Perches 		  read_buf);
9232f7ca802SSteve Glendinning 
9242f7ca802SSteve Glendinning 	if (turbo_mode)
9252f7ca802SSteve Glendinning 		read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
9262f7ca802SSteve Glendinning 
9272f7ca802SSteve Glendinning 	read_buf &= ~HW_CFG_RXDOFF_;
9282f7ca802SSteve Glendinning 
9292f7ca802SSteve Glendinning 	/* set Rx data offset=2, Make IP header aligns on word boundary. */
9302f7ca802SSteve Glendinning 	read_buf |= NET_IP_ALIGN << 9;
9312f7ca802SSteve Glendinning 
9322f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
933769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write HW_CFG: %d\n", ret);
9342f7ca802SSteve Glendinning 
9352f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
936769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
937769ea6d8SSteve Glendinning 
938a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
939a475f603SJoe Perches 		  "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
9402f7ca802SSteve Glendinning 
9414436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
942769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write INT_STS: %d\n", ret);
9432f7ca802SSteve Glendinning 
9442f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
945769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read ID_REV: %d\n", ret);
946a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
9472f7ca802SSteve Glendinning 
948f293501cSSteve Glendinning 	/* Configure GPIO pins as LED outputs */
949f293501cSSteve Glendinning 	write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
950f293501cSSteve Glendinning 		LED_GPIO_CFG_FDX_LED;
951f293501cSSteve Glendinning 	ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
952769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write LED_GPIO_CFG: %d\n", ret);
953f293501cSSteve Glendinning 
9542f7ca802SSteve Glendinning 	/* Init Tx */
9554436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, FLOW, 0);
956769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write FLOW: %d\n", ret);
9572f7ca802SSteve Glendinning 
9584436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
959769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write AFC_CFG: %d\n", ret);
9602f7ca802SSteve Glendinning 
9612f7ca802SSteve Glendinning 	/* Don't need mac_cr_lock during initialisation */
9622f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
963769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read MAC_CR: %d\n", ret);
9642f7ca802SSteve Glendinning 
9652f7ca802SSteve Glendinning 	/* Init Rx */
9662f7ca802SSteve Glendinning 	/* Set Vlan */
9674436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
968769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write VLAN1: %d\n", ret);
9692f7ca802SSteve Glendinning 
970f7b29271SSteve Glendinning 	/* Enable or disable checksum offload engines */
971769ea6d8SSteve Glendinning 	ret = smsc95xx_set_features(dev->net, dev->net->features);
9721e1d7412SJoe Perches 	check_warn_return(ret, "Failed to set checksum offload features\n");
9732f7ca802SSteve Glendinning 
9742f7ca802SSteve Glendinning 	smsc95xx_set_multicast(dev->net);
9752f7ca802SSteve Glendinning 
976769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_initialize(dev);
9771e1d7412SJoe Perches 	check_warn_return(ret, "Failed to init PHY\n");
9782f7ca802SSteve Glendinning 
9792f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
980769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read INT_EP_CTL: %d\n", ret);
9812f7ca802SSteve Glendinning 
9822f7ca802SSteve Glendinning 	/* enable PHY interrupts */
9832f7ca802SSteve Glendinning 	read_buf |= INT_EP_CTL_PHY_INT_;
9842f7ca802SSteve Glendinning 
9852f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
986769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write INT_EP_CTL: %d\n", ret);
9872f7ca802SSteve Glendinning 
988769ea6d8SSteve Glendinning 	ret = smsc95xx_start_tx_path(dev);
9891e1d7412SJoe Perches 	check_warn_return(ret, "Failed to start TX path\n");
990769ea6d8SSteve Glendinning 
991ec32115dSMing Lei 	ret = smsc95xx_start_rx_path(dev, 0);
9921e1d7412SJoe Perches 	check_warn_return(ret, "Failed to start RX path\n");
9932f7ca802SSteve Glendinning 
994a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
9952f7ca802SSteve Glendinning 	return 0;
9962f7ca802SSteve Glendinning }
9972f7ca802SSteve Glendinning 
99863e77b39SStephen Hemminger static const struct net_device_ops smsc95xx_netdev_ops = {
99963e77b39SStephen Hemminger 	.ndo_open		= usbnet_open,
100063e77b39SStephen Hemminger 	.ndo_stop		= usbnet_stop,
100163e77b39SStephen Hemminger 	.ndo_start_xmit		= usbnet_start_xmit,
100263e77b39SStephen Hemminger 	.ndo_tx_timeout		= usbnet_tx_timeout,
100363e77b39SStephen Hemminger 	.ndo_change_mtu		= usbnet_change_mtu,
100463e77b39SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
100563e77b39SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
100663e77b39SStephen Hemminger 	.ndo_do_ioctl 		= smsc95xx_ioctl,
1007afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= smsc95xx_set_multicast,
100878e47fe4SMichał Mirosław 	.ndo_set_features	= smsc95xx_set_features,
100963e77b39SStephen Hemminger };
101063e77b39SStephen Hemminger 
10112f7ca802SSteve Glendinning static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
10122f7ca802SSteve Glendinning {
10132f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = NULL;
1014bbd9f9eeSSteve Glendinning 	u32 val;
10152f7ca802SSteve Glendinning 	int ret;
10162f7ca802SSteve Glendinning 
10172f7ca802SSteve Glendinning 	printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
10182f7ca802SSteve Glendinning 
10192f7ca802SSteve Glendinning 	ret = usbnet_get_endpoints(dev, intf);
1020769ea6d8SSteve Glendinning 	check_warn_return(ret, "usbnet_get_endpoints failed: %d\n", ret);
10212f7ca802SSteve Glendinning 
10222f7ca802SSteve Glendinning 	dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
10232f7ca802SSteve Glendinning 		GFP_KERNEL);
10242f7ca802SSteve Glendinning 
10252f7ca802SSteve Glendinning 	pdata = (struct smsc95xx_priv *)(dev->data[0]);
10262f7ca802SSteve Glendinning 	if (!pdata) {
102760b86755SJoe Perches 		netdev_warn(dev->net, "Unable to allocate struct smsc95xx_priv\n");
10282f7ca802SSteve Glendinning 		return -ENOMEM;
10292f7ca802SSteve Glendinning 	}
10302f7ca802SSteve Glendinning 
10312f7ca802SSteve Glendinning 	spin_lock_init(&pdata->mac_cr_lock);
10322f7ca802SSteve Glendinning 
103378e47fe4SMichał Mirosław 	if (DEFAULT_TX_CSUM_ENABLE)
103478e47fe4SMichał Mirosław 		dev->net->features |= NETIF_F_HW_CSUM;
103578e47fe4SMichał Mirosław 	if (DEFAULT_RX_CSUM_ENABLE)
103678e47fe4SMichał Mirosław 		dev->net->features |= NETIF_F_RXCSUM;
103778e47fe4SMichał Mirosław 
103878e47fe4SMichał Mirosław 	dev->net->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
10392f7ca802SSteve Glendinning 
1040f4e8ab7cSBernard Blackham 	smsc95xx_init_mac_address(dev);
1041f4e8ab7cSBernard Blackham 
10422f7ca802SSteve Glendinning 	/* Init all registers */
10432f7ca802SSteve Glendinning 	ret = smsc95xx_reset(dev);
10442f7ca802SSteve Glendinning 
1045bbd9f9eeSSteve Glendinning 	/* detect device revision as different features may be available */
1046bbd9f9eeSSteve Glendinning 	ret = smsc95xx_read_reg(dev, ID_REV, &val);
1047bbd9f9eeSSteve Glendinning 	check_warn_return(ret, "Failed to read ID_REV: %d\n", ret);
1048bbd9f9eeSSteve Glendinning 	val >>= 16;
10499ebca507SSteve Glendinning 
10509ebca507SSteve Glendinning 	if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
10519ebca507SSteve Glendinning 	    (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
10529ebca507SSteve Glendinning 		pdata->features = (FEATURE_8_WAKEUP_FILTERS |
10539ebca507SSteve Glendinning 			FEATURE_PHY_NLP_CROSSOVER |
10549ebca507SSteve Glendinning 			FEATURE_AUTOSUSPEND);
10559ebca507SSteve Glendinning 	else if (val == ID_REV_CHIP_ID_9512_)
10569ebca507SSteve Glendinning 		pdata->features = FEATURE_8_WAKEUP_FILTERS;
1057bbd9f9eeSSteve Glendinning 
105863e77b39SStephen Hemminger 	dev->net->netdev_ops = &smsc95xx_netdev_ops;
10592f7ca802SSteve Glendinning 	dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
10602f7ca802SSteve Glendinning 	dev->net->flags |= IFF_MULTICAST;
106178e47fe4SMichał Mirosław 	dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
10629bbf5660SStephane Fillod 	dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
10632f7ca802SSteve Glendinning 	return 0;
10642f7ca802SSteve Glendinning }
10652f7ca802SSteve Glendinning 
10662f7ca802SSteve Glendinning static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
10672f7ca802SSteve Glendinning {
10682f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
10692f7ca802SSteve Glendinning 	if (pdata) {
1070a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net, "free pdata\n");
10712f7ca802SSteve Glendinning 		kfree(pdata);
10722f7ca802SSteve Glendinning 		pdata = NULL;
10732f7ca802SSteve Glendinning 		dev->data[0] = 0;
10742f7ca802SSteve Glendinning 	}
10752f7ca802SSteve Glendinning }
10762f7ca802SSteve Glendinning 
1077bbd9f9eeSSteve Glendinning static u16 smsc_crc(const u8 *buffer, size_t len, int filter)
1078bbd9f9eeSSteve Glendinning {
1079bbd9f9eeSSteve Glendinning 	return bitrev16(crc16(0xFFFF, buffer, len)) << ((filter % 2) * 16);
1080bbd9f9eeSSteve Glendinning }
1081bbd9f9eeSSteve Glendinning 
1082e5e3af83SSteve Glendinning static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1083e5e3af83SSteve Glendinning {
1084e5e3af83SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
1085e5e3af83SSteve Glendinning 	int ret;
1086e5e3af83SSteve Glendinning 
10871e1d7412SJoe Perches 	netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1088e5e3af83SSteve Glendinning 
1089e5e3af83SSteve Glendinning 	/* read to clear */
1090e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
10911e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PHY_INT_SRC\n");
1092e5e3af83SSteve Glendinning 
1093e5e3af83SSteve Glendinning 	/* enable interrupt source */
1094e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
10951e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PHY_INT_MASK\n");
1096e5e3af83SSteve Glendinning 
1097e5e3af83SSteve Glendinning 	ret |= mask;
1098e5e3af83SSteve Glendinning 
1099e5e3af83SSteve Glendinning 	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1100e5e3af83SSteve Glendinning 
1101e5e3af83SSteve Glendinning 	return 0;
1102e5e3af83SSteve Glendinning }
1103e5e3af83SSteve Glendinning 
1104e5e3af83SSteve Glendinning static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1105e5e3af83SSteve Glendinning {
1106e5e3af83SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
1107e5e3af83SSteve Glendinning 	int ret;
1108e5e3af83SSteve Glendinning 
1109e5e3af83SSteve Glendinning 	/* first, a dummy read, needed to latch some MII phys */
1110e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
11111e1d7412SJoe Perches 	check_warn_return(ret, "Error reading MII_BMSR\n");
1112e5e3af83SSteve Glendinning 
1113e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
11141e1d7412SJoe Perches 	check_warn_return(ret, "Error reading MII_BMSR\n");
1115e5e3af83SSteve Glendinning 
1116e5e3af83SSteve Glendinning 	return !!(ret & BMSR_LSTATUS);
1117e5e3af83SSteve Glendinning }
1118e5e3af83SSteve Glendinning 
1119319b95b5SSteve Glendinning static int smsc95xx_enter_suspend0(struct usbnet *dev)
1120319b95b5SSteve Glendinning {
1121319b95b5SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1122319b95b5SSteve Glendinning 	u32 val;
1123319b95b5SSteve Glendinning 	int ret;
1124319b95b5SSteve Glendinning 
1125319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
11261e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PM_CTRL\n");
1127319b95b5SSteve Glendinning 
1128319b95b5SSteve Glendinning 	val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1129319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_0;
1130319b95b5SSteve Glendinning 
1131319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
11321e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1133319b95b5SSteve Glendinning 
1134319b95b5SSteve Glendinning 	/* clear wol status */
1135319b95b5SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1136319b95b5SSteve Glendinning 	val |= PM_CTL_WUPS_WOL_;
1137319b95b5SSteve Glendinning 
1138319b95b5SSteve Glendinning 	/* enable energy detection */
1139319b95b5SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY)
1140319b95b5SSteve Glendinning 		val |= PM_CTL_WUPS_ED_;
1141319b95b5SSteve Glendinning 
1142319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
11431e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1144319b95b5SSteve Glendinning 
1145319b95b5SSteve Glendinning 	/* read back PM_CTRL */
1146319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
11471e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PM_CTRL\n");
1148319b95b5SSteve Glendinning 
1149319b95b5SSteve Glendinning 	return 0;
1150319b95b5SSteve Glendinning }
1151319b95b5SSteve Glendinning 
1152319b95b5SSteve Glendinning static int smsc95xx_enter_suspend1(struct usbnet *dev)
1153319b95b5SSteve Glendinning {
1154319b95b5SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1155319b95b5SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
1156319b95b5SSteve Glendinning 	u32 val;
1157319b95b5SSteve Glendinning 	int ret;
1158319b95b5SSteve Glendinning 
1159319b95b5SSteve Glendinning 	/* reconfigure link pulse detection timing for
1160319b95b5SSteve Glendinning 	 * compatibility with non-standard link partners
1161319b95b5SSteve Glendinning 	 */
1162319b95b5SSteve Glendinning 	if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
1163319b95b5SSteve Glendinning 		smsc95xx_mdio_write_nopm(dev->net, mii->phy_id,	PHY_EDPD_CONFIG,
1164319b95b5SSteve Glendinning 			PHY_EDPD_CONFIG_DEFAULT);
1165319b95b5SSteve Glendinning 
1166319b95b5SSteve Glendinning 	/* enable energy detect power-down mode */
1167319b95b5SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS);
11681e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PHY_MODE_CTRL_STS\n");
1169319b95b5SSteve Glendinning 
1170319b95b5SSteve Glendinning 	ret |= MODE_CTRL_STS_EDPWRDOWN_;
1171319b95b5SSteve Glendinning 
1172319b95b5SSteve Glendinning 	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS, ret);
1173319b95b5SSteve Glendinning 
1174319b95b5SSteve Glendinning 	/* enter SUSPEND1 mode */
1175319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
11761e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PM_CTRL\n");
1177319b95b5SSteve Glendinning 
1178319b95b5SSteve Glendinning 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1179319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_1;
1180319b95b5SSteve Glendinning 
1181319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
11821e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1183319b95b5SSteve Glendinning 
1184319b95b5SSteve Glendinning 	/* clear wol status, enable energy detection */
1185319b95b5SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1186319b95b5SSteve Glendinning 	val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1187319b95b5SSteve Glendinning 
1188319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
11891e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1190319b95b5SSteve Glendinning 
1191319b95b5SSteve Glendinning 	return 0;
1192319b95b5SSteve Glendinning }
1193319b95b5SSteve Glendinning 
1194319b95b5SSteve Glendinning static int smsc95xx_enter_suspend2(struct usbnet *dev)
1195319b95b5SSteve Glendinning {
1196319b95b5SSteve Glendinning 	u32 val;
1197319b95b5SSteve Glendinning 	int ret;
1198319b95b5SSteve Glendinning 
1199319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
12001e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PM_CTRL\n");
1201319b95b5SSteve Glendinning 
1202319b95b5SSteve Glendinning 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1203319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_2;
1204319b95b5SSteve Glendinning 
1205319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
12061e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1207319b95b5SSteve Glendinning 
1208319b95b5SSteve Glendinning 	return 0;
1209319b95b5SSteve Glendinning }
1210319b95b5SSteve Glendinning 
1211b5a04475SSteve Glendinning static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1212b5a04475SSteve Glendinning {
1213b5a04475SSteve Glendinning 	struct usbnet *dev = usb_get_intfdata(intf);
1214e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1215e5e3af83SSteve Glendinning 	u32 val, link_up;
1216b5a04475SSteve Glendinning 	int ret;
1217b5a04475SSteve Glendinning 
1218b5a04475SSteve Glendinning 	ret = usbnet_suspend(intf, message);
12191e1d7412SJoe Perches 	check_warn_return(ret, "usbnet_suspend error\n");
1220b5a04475SSteve Glendinning 
1221e5e3af83SSteve Glendinning 	/* determine if link is up using only _nopm functions */
1222e5e3af83SSteve Glendinning 	link_up = smsc95xx_link_ok_nopm(dev);
1223e5e3af83SSteve Glendinning 
1224e5e3af83SSteve Glendinning 	/* if no wol options set, or if link is down and we're not waking on
1225e5e3af83SSteve Glendinning 	 * PHY activity, enter lowest power SUSPEND2 mode
1226e5e3af83SSteve Glendinning 	 */
1227e5e3af83SSteve Glendinning 	if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1228e5e3af83SSteve Glendinning 		!(link_up || (pdata->wolopts & WAKE_PHY))) {
12291e1d7412SJoe Perches 		netdev_info(dev->net, "entering SUSPEND2 mode\n");
1230b5a04475SSteve Glendinning 
1231e0e474a8SSteve Glendinning 		/* disable energy detect (link up) & wake up events */
1232ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
12333b9f7d8cSSteve Glendinning 		check_warn_goto_done(ret, "Error reading WUCSR\n");
1234e0e474a8SSteve Glendinning 
1235e0e474a8SSteve Glendinning 		val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1236e0e474a8SSteve Glendinning 
1237ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
12383b9f7d8cSSteve Glendinning 		check_warn_goto_done(ret, "Error writing WUCSR\n");
1239e0e474a8SSteve Glendinning 
1240ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
12413b9f7d8cSSteve Glendinning 		check_warn_goto_done(ret, "Error reading PM_CTRL\n");
1242e0e474a8SSteve Glendinning 
1243e0e474a8SSteve Glendinning 		val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1244e0e474a8SSteve Glendinning 
1245ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
12463b9f7d8cSSteve Glendinning 		check_warn_goto_done(ret, "Error writing PM_CTRL\n");
1247e0e474a8SSteve Glendinning 
12483b9f7d8cSSteve Glendinning 		ret = smsc95xx_enter_suspend2(dev);
12493b9f7d8cSSteve Glendinning 		goto done;
1250b5a04475SSteve Glendinning 	}
1251b5a04475SSteve Glendinning 
1252e5e3af83SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY) {
1253e5e3af83SSteve Glendinning 		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1254e5e3af83SSteve Glendinning 			(PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
12553b9f7d8cSSteve Glendinning 		check_warn_goto_done(ret, "error enabling PHY wakeup ints\n");
1256e5e3af83SSteve Glendinning 
1257e5e3af83SSteve Glendinning 		/* if link is down then configure EDPD and enter SUSPEND1,
1258e5e3af83SSteve Glendinning 		 * otherwise enter SUSPEND0 below
1259e5e3af83SSteve Glendinning 		 */
1260e5e3af83SSteve Glendinning 		if (!link_up) {
12611e1d7412SJoe Perches 			netdev_info(dev->net, "entering SUSPEND1 mode\n");
12623b9f7d8cSSteve Glendinning 			ret = smsc95xx_enter_suspend1(dev);
12633b9f7d8cSSteve Glendinning 			goto done;
1264e5e3af83SSteve Glendinning 		}
1265e5e3af83SSteve Glendinning 	}
1266e5e3af83SSteve Glendinning 
1267bbd9f9eeSSteve Glendinning 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1268eed9a729SSteve Glendinning 		u32 *filter_mask = kzalloc(sizeof(u32) * 32, GFP_KERNEL);
126906a221beSMing Lei 		u32 command[2];
127006a221beSMing Lei 		u32 offset[2];
127106a221beSMing Lei 		u32 crc[4];
12729ebca507SSteve Glendinning 		int wuff_filter_count =
12739ebca507SSteve Glendinning 			(pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
12749ebca507SSteve Glendinning 			LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
1275bbd9f9eeSSteve Glendinning 		int i, filter = 0;
1276bbd9f9eeSSteve Glendinning 
1277eed9a729SSteve Glendinning 		if (!filter_mask) {
1278eed9a729SSteve Glendinning 			netdev_warn(dev->net, "Unable to allocate filter_mask\n");
12793b9f7d8cSSteve Glendinning 			ret = -ENOMEM;
12803b9f7d8cSSteve Glendinning 			goto done;
1281eed9a729SSteve Glendinning 		}
1282eed9a729SSteve Glendinning 
128306a221beSMing Lei 		memset(command, 0, sizeof(command));
128406a221beSMing Lei 		memset(offset, 0, sizeof(offset));
128506a221beSMing Lei 		memset(crc, 0, sizeof(crc));
128606a221beSMing Lei 
1287bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_BCAST) {
1288bbd9f9eeSSteve Glendinning 			const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
12891e1d7412SJoe Perches 			netdev_info(dev->net, "enabling broadcast detection\n");
1290bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x003F;
1291bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1292bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1293bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1294bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1295bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1296bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(bcast, 6, filter);
1297bbd9f9eeSSteve Glendinning 			filter++;
1298bbd9f9eeSSteve Glendinning 		}
1299bbd9f9eeSSteve Glendinning 
1300bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_MCAST) {
1301bbd9f9eeSSteve Glendinning 			const u8 mcast[] = {0x01, 0x00, 0x5E};
13021e1d7412SJoe Perches 			netdev_info(dev->net, "enabling multicast detection\n");
1303bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x0007;
1304bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1305bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1306bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1307bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1308bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00  << ((filter % 4) * 8);
1309bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(mcast, 3, filter);
1310bbd9f9eeSSteve Glendinning 			filter++;
1311bbd9f9eeSSteve Glendinning 		}
1312bbd9f9eeSSteve Glendinning 
1313bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_ARP) {
1314bbd9f9eeSSteve Glendinning 			const u8 arp[] = {0x08, 0x06};
13151e1d7412SJoe Perches 			netdev_info(dev->net, "enabling ARP detection\n");
1316bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x0003;
1317bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1318bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1319bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1320bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1321bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1322bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(arp, 2, filter);
1323bbd9f9eeSSteve Glendinning 			filter++;
1324bbd9f9eeSSteve Glendinning 		}
1325bbd9f9eeSSteve Glendinning 
1326bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_UCAST) {
13271e1d7412SJoe Perches 			netdev_info(dev->net, "enabling unicast detection\n");
1328bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x003F;
1329bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1330bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1331bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1332bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1333bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1334bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1335bbd9f9eeSSteve Glendinning 			filter++;
1336bbd9f9eeSSteve Glendinning 		}
1337bbd9f9eeSSteve Glendinning 
13389ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count * 4); i++) {
1339ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
134006a221beSMing Lei 			if (ret < 0)
134106a221beSMing Lei 				kfree(filter_mask);
13423b9f7d8cSSteve Glendinning 			check_warn_goto_done(ret, "Error writing WUFF\n");
1343bbd9f9eeSSteve Glendinning 		}
134406a221beSMing Lei 		kfree(filter_mask);
1345bbd9f9eeSSteve Glendinning 
13469ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1347ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
13483b9f7d8cSSteve Glendinning 			check_warn_goto_done(ret, "Error writing WUFF\n");
1349bbd9f9eeSSteve Glendinning 		}
1350bbd9f9eeSSteve Glendinning 
13519ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1352ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
13533b9f7d8cSSteve Glendinning 			check_warn_goto_done(ret, "Error writing WUFF\n");
1354bbd9f9eeSSteve Glendinning 		}
1355bbd9f9eeSSteve Glendinning 
13569ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 2); i++) {
1357ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
13583b9f7d8cSSteve Glendinning 			check_warn_goto_done(ret, "Error writing WUFF\n");
1359bbd9f9eeSSteve Glendinning 		}
1360bbd9f9eeSSteve Glendinning 
1361bbd9f9eeSSteve Glendinning 		/* clear any pending pattern match packet status */
1362ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
13633b9f7d8cSSteve Glendinning 		check_warn_goto_done(ret, "Error reading WUCSR\n");
1364bbd9f9eeSSteve Glendinning 
1365bbd9f9eeSSteve Glendinning 		val |= WUCSR_WUFR_;
1366bbd9f9eeSSteve Glendinning 
1367ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
13683b9f7d8cSSteve Glendinning 		check_warn_goto_done(ret, "Error writing WUCSR\n");
1369bbd9f9eeSSteve Glendinning 	}
1370bbd9f9eeSSteve Glendinning 
1371e0e474a8SSteve Glendinning 	if (pdata->wolopts & WAKE_MAGIC) {
1372e0e474a8SSteve Glendinning 		/* clear any pending magic packet status */
1373ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
13743b9f7d8cSSteve Glendinning 		check_warn_goto_done(ret, "Error reading WUCSR\n");
1375e0e474a8SSteve Glendinning 
1376e0e474a8SSteve Glendinning 		val |= WUCSR_MPR_;
1377e0e474a8SSteve Glendinning 
1378ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
13793b9f7d8cSSteve Glendinning 		check_warn_goto_done(ret, "Error writing WUCSR\n");
1380e0e474a8SSteve Glendinning 	}
1381e0e474a8SSteve Glendinning 
1382bbd9f9eeSSteve Glendinning 	/* enable/disable wakeup sources */
1383ec32115dSMing Lei 	ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
13843b9f7d8cSSteve Glendinning 	check_warn_goto_done(ret, "Error reading WUCSR\n");
1385e0e474a8SSteve Glendinning 
1386bbd9f9eeSSteve Glendinning 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
13871e1d7412SJoe Perches 		netdev_info(dev->net, "enabling pattern match wakeup\n");
1388bbd9f9eeSSteve Glendinning 		val |= WUCSR_WAKE_EN_;
1389bbd9f9eeSSteve Glendinning 	} else {
13901e1d7412SJoe Perches 		netdev_info(dev->net, "disabling pattern match wakeup\n");
1391bbd9f9eeSSteve Glendinning 		val &= ~WUCSR_WAKE_EN_;
1392bbd9f9eeSSteve Glendinning 	}
1393bbd9f9eeSSteve Glendinning 
1394e0e474a8SSteve Glendinning 	if (pdata->wolopts & WAKE_MAGIC) {
13951e1d7412SJoe Perches 		netdev_info(dev->net, "enabling magic packet wakeup\n");
1396e0e474a8SSteve Glendinning 		val |= WUCSR_MPEN_;
1397e0e474a8SSteve Glendinning 	} else {
13981e1d7412SJoe Perches 		netdev_info(dev->net, "disabling magic packet wakeup\n");
1399e0e474a8SSteve Glendinning 		val &= ~WUCSR_MPEN_;
1400e0e474a8SSteve Glendinning 	}
1401e0e474a8SSteve Glendinning 
1402ec32115dSMing Lei 	ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
14033b9f7d8cSSteve Glendinning 	check_warn_goto_done(ret, "Error writing WUCSR\n");
1404e0e474a8SSteve Glendinning 
1405e0e474a8SSteve Glendinning 	/* enable wol wakeup source */
1406ec32115dSMing Lei 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
14073b9f7d8cSSteve Glendinning 	check_warn_goto_done(ret, "Error reading PM_CTRL\n");
1408e0e474a8SSteve Glendinning 
1409e0e474a8SSteve Glendinning 	val |= PM_CTL_WOL_EN_;
1410e0e474a8SSteve Glendinning 
1411e5e3af83SSteve Glendinning 	/* phy energy detect wakeup source */
1412e5e3af83SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY)
1413e5e3af83SSteve Glendinning 		val |= PM_CTL_ED_EN_;
1414e5e3af83SSteve Glendinning 
1415ec32115dSMing Lei 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
14163b9f7d8cSSteve Glendinning 	check_warn_goto_done(ret, "Error writing PM_CTRL\n");
1417e0e474a8SSteve Glendinning 
1418bbd9f9eeSSteve Glendinning 	/* enable receiver to enable frame reception */
1419ec32115dSMing Lei 	smsc95xx_start_rx_path(dev, 1);
1420e0e474a8SSteve Glendinning 
1421e0e474a8SSteve Glendinning 	/* some wol options are enabled, so enter SUSPEND0 */
14221e1d7412SJoe Perches 	netdev_info(dev->net, "entering SUSPEND0 mode\n");
14233b9f7d8cSSteve Glendinning 	ret = smsc95xx_enter_suspend0(dev);
14243b9f7d8cSSteve Glendinning 
14253b9f7d8cSSteve Glendinning done:
14263b9f7d8cSSteve Glendinning 	if (ret)
14273b9f7d8cSSteve Glendinning 		usbnet_resume(intf);
14283b9f7d8cSSteve Glendinning 	return ret;
1429e0e474a8SSteve Glendinning }
1430e0e474a8SSteve Glendinning 
1431e0e474a8SSteve Glendinning static int smsc95xx_resume(struct usb_interface *intf)
1432e0e474a8SSteve Glendinning {
1433e0e474a8SSteve Glendinning 	struct usbnet *dev = usb_get_intfdata(intf);
1434e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1435e0e474a8SSteve Glendinning 	int ret;
1436e0e474a8SSteve Glendinning 	u32 val;
1437e0e474a8SSteve Glendinning 
1438e0e474a8SSteve Glendinning 	BUG_ON(!dev);
1439e0e474a8SSteve Glendinning 
1440bbd9f9eeSSteve Glendinning 	if (pdata->wolopts) {
1441bbd9f9eeSSteve Glendinning 		/* clear wake-up sources */
1442ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
14431e1d7412SJoe Perches 		check_warn_return(ret, "Error reading WUCSR\n");
1444e0e474a8SSteve Glendinning 
1445bbd9f9eeSSteve Glendinning 		val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
1446e0e474a8SSteve Glendinning 
1447ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
14481e1d7412SJoe Perches 		check_warn_return(ret, "Error writing WUCSR\n");
1449e0e474a8SSteve Glendinning 
1450e0e474a8SSteve Glendinning 		/* clear wake-up status */
1451ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
14521e1d7412SJoe Perches 		check_warn_return(ret, "Error reading PM_CTRL\n");
1453e0e474a8SSteve Glendinning 
1454e0e474a8SSteve Glendinning 		val &= ~PM_CTL_WOL_EN_;
1455e0e474a8SSteve Glendinning 		val |= PM_CTL_WUPS_;
1456e0e474a8SSteve Glendinning 
1457ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
14581e1d7412SJoe Perches 		check_warn_return(ret, "Error writing PM_CTRL\n");
1459e0e474a8SSteve Glendinning 	}
1460e0e474a8SSteve Glendinning 
1461af3d7c1eSSteve Glendinning 	ret = usbnet_resume(intf);
14621e1d7412SJoe Perches 	check_warn_return(ret, "usbnet_resume error\n");
1463e0e474a8SSteve Glendinning 
1464e0e474a8SSteve Glendinning 	return 0;
1465e0e474a8SSteve Glendinning }
1466e0e474a8SSteve Glendinning 
14672f7ca802SSteve Glendinning static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
14682f7ca802SSteve Glendinning {
14692f7ca802SSteve Glendinning 	skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
14702f7ca802SSteve Glendinning 	skb->ip_summed = CHECKSUM_COMPLETE;
14712f7ca802SSteve Glendinning 	skb_trim(skb, skb->len - 2);
14722f7ca802SSteve Glendinning }
14732f7ca802SSteve Glendinning 
14742f7ca802SSteve Glendinning static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
14752f7ca802SSteve Glendinning {
14762f7ca802SSteve Glendinning 	while (skb->len > 0) {
14772f7ca802SSteve Glendinning 		u32 header, align_count;
14782f7ca802SSteve Glendinning 		struct sk_buff *ax_skb;
14792f7ca802SSteve Glendinning 		unsigned char *packet;
14802f7ca802SSteve Glendinning 		u16 size;
14812f7ca802SSteve Glendinning 
14822f7ca802SSteve Glendinning 		memcpy(&header, skb->data, sizeof(header));
14832f7ca802SSteve Glendinning 		le32_to_cpus(&header);
14842f7ca802SSteve Glendinning 		skb_pull(skb, 4 + NET_IP_ALIGN);
14852f7ca802SSteve Glendinning 		packet = skb->data;
14862f7ca802SSteve Glendinning 
14872f7ca802SSteve Glendinning 		/* get the packet length */
14882f7ca802SSteve Glendinning 		size = (u16)((header & RX_STS_FL_) >> 16);
14892f7ca802SSteve Glendinning 		align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
14902f7ca802SSteve Glendinning 
14912f7ca802SSteve Glendinning 		if (unlikely(header & RX_STS_ES_)) {
1492a475f603SJoe Perches 			netif_dbg(dev, rx_err, dev->net,
1493a475f603SJoe Perches 				  "Error header=0x%08x\n", header);
149480667ac1SHerbert Xu 			dev->net->stats.rx_errors++;
149580667ac1SHerbert Xu 			dev->net->stats.rx_dropped++;
14962f7ca802SSteve Glendinning 
14972f7ca802SSteve Glendinning 			if (header & RX_STS_CRC_) {
149880667ac1SHerbert Xu 				dev->net->stats.rx_crc_errors++;
14992f7ca802SSteve Glendinning 			} else {
15002f7ca802SSteve Glendinning 				if (header & (RX_STS_TL_ | RX_STS_RF_))
150180667ac1SHerbert Xu 					dev->net->stats.rx_frame_errors++;
15022f7ca802SSteve Glendinning 
15032f7ca802SSteve Glendinning 				if ((header & RX_STS_LE_) &&
15042f7ca802SSteve Glendinning 					(!(header & RX_STS_FT_)))
150580667ac1SHerbert Xu 					dev->net->stats.rx_length_errors++;
15062f7ca802SSteve Glendinning 			}
15072f7ca802SSteve Glendinning 		} else {
15082f7ca802SSteve Glendinning 			/* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
15092f7ca802SSteve Glendinning 			if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1510a475f603SJoe Perches 				netif_dbg(dev, rx_err, dev->net,
1511a475f603SJoe Perches 					  "size err header=0x%08x\n", header);
15122f7ca802SSteve Glendinning 				return 0;
15132f7ca802SSteve Glendinning 			}
15142f7ca802SSteve Glendinning 
15152f7ca802SSteve Glendinning 			/* last frame in this batch */
15162f7ca802SSteve Glendinning 			if (skb->len == size) {
151778e47fe4SMichał Mirosław 				if (dev->net->features & NETIF_F_RXCSUM)
15182f7ca802SSteve Glendinning 					smsc95xx_rx_csum_offload(skb);
1519df18accaSPeter Korsgaard 				skb_trim(skb, skb->len - 4); /* remove fcs */
15202f7ca802SSteve Glendinning 				skb->truesize = size + sizeof(struct sk_buff);
15212f7ca802SSteve Glendinning 
15222f7ca802SSteve Glendinning 				return 1;
15232f7ca802SSteve Glendinning 			}
15242f7ca802SSteve Glendinning 
15252f7ca802SSteve Glendinning 			ax_skb = skb_clone(skb, GFP_ATOMIC);
15262f7ca802SSteve Glendinning 			if (unlikely(!ax_skb)) {
152760b86755SJoe Perches 				netdev_warn(dev->net, "Error allocating skb\n");
15282f7ca802SSteve Glendinning 				return 0;
15292f7ca802SSteve Glendinning 			}
15302f7ca802SSteve Glendinning 
15312f7ca802SSteve Glendinning 			ax_skb->len = size;
15322f7ca802SSteve Glendinning 			ax_skb->data = packet;
15332f7ca802SSteve Glendinning 			skb_set_tail_pointer(ax_skb, size);
15342f7ca802SSteve Glendinning 
153578e47fe4SMichał Mirosław 			if (dev->net->features & NETIF_F_RXCSUM)
15362f7ca802SSteve Glendinning 				smsc95xx_rx_csum_offload(ax_skb);
1537df18accaSPeter Korsgaard 			skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
15382f7ca802SSteve Glendinning 			ax_skb->truesize = size + sizeof(struct sk_buff);
15392f7ca802SSteve Glendinning 
15402f7ca802SSteve Glendinning 			usbnet_skb_return(dev, ax_skb);
15412f7ca802SSteve Glendinning 		}
15422f7ca802SSteve Glendinning 
15432f7ca802SSteve Glendinning 		skb_pull(skb, size);
15442f7ca802SSteve Glendinning 
15452f7ca802SSteve Glendinning 		/* padding bytes before the next frame starts */
15462f7ca802SSteve Glendinning 		if (skb->len)
15472f7ca802SSteve Glendinning 			skb_pull(skb, align_count);
15482f7ca802SSteve Glendinning 	}
15492f7ca802SSteve Glendinning 
15502f7ca802SSteve Glendinning 	if (unlikely(skb->len < 0)) {
155160b86755SJoe Perches 		netdev_warn(dev->net, "invalid rx length<0 %d\n", skb->len);
15522f7ca802SSteve Glendinning 		return 0;
15532f7ca802SSteve Glendinning 	}
15542f7ca802SSteve Glendinning 
15552f7ca802SSteve Glendinning 	return 1;
15562f7ca802SSteve Glendinning }
15572f7ca802SSteve Glendinning 
1558f7b29271SSteve Glendinning static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
1559f7b29271SSteve Glendinning {
156055508d60SMichał Mirosław 	u16 low_16 = (u16)skb_checksum_start_offset(skb);
156155508d60SMichał Mirosław 	u16 high_16 = low_16 + skb->csum_offset;
1562f7b29271SSteve Glendinning 	return (high_16 << 16) | low_16;
1563f7b29271SSteve Glendinning }
1564f7b29271SSteve Glendinning 
15652f7ca802SSteve Glendinning static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
15662f7ca802SSteve Glendinning 					 struct sk_buff *skb, gfp_t flags)
15672f7ca802SSteve Glendinning {
156878e47fe4SMichał Mirosław 	bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
1569f7b29271SSteve Glendinning 	int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
15702f7ca802SSteve Glendinning 	u32 tx_cmd_a, tx_cmd_b;
15712f7ca802SSteve Glendinning 
1572f7b29271SSteve Glendinning 	/* We do not advertise SG, so skbs should be already linearized */
1573f7b29271SSteve Glendinning 	BUG_ON(skb_shinfo(skb)->nr_frags);
1574f7b29271SSteve Glendinning 
1575f7b29271SSteve Glendinning 	if (skb_headroom(skb) < overhead) {
15762f7ca802SSteve Glendinning 		struct sk_buff *skb2 = skb_copy_expand(skb,
1577f7b29271SSteve Glendinning 			overhead, 0, flags);
15782f7ca802SSteve Glendinning 		dev_kfree_skb_any(skb);
15792f7ca802SSteve Glendinning 		skb = skb2;
15802f7ca802SSteve Glendinning 		if (!skb)
15812f7ca802SSteve Glendinning 			return NULL;
15822f7ca802SSteve Glendinning 	}
15832f7ca802SSteve Glendinning 
1584f7b29271SSteve Glendinning 	if (csum) {
158511bc3088SSteve Glendinning 		if (skb->len <= 45) {
158611bc3088SSteve Glendinning 			/* workaround - hardware tx checksum does not work
158711bc3088SSteve Glendinning 			 * properly with extremely small packets */
158855508d60SMichał Mirosław 			long csstart = skb_checksum_start_offset(skb);
158911bc3088SSteve Glendinning 			__wsum calc = csum_partial(skb->data + csstart,
159011bc3088SSteve Glendinning 				skb->len - csstart, 0);
159111bc3088SSteve Glendinning 			*((__sum16 *)(skb->data + csstart
159211bc3088SSteve Glendinning 				+ skb->csum_offset)) = csum_fold(calc);
159311bc3088SSteve Glendinning 
159411bc3088SSteve Glendinning 			csum = false;
159511bc3088SSteve Glendinning 		} else {
1596f7b29271SSteve Glendinning 			u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
1597f7b29271SSteve Glendinning 			skb_push(skb, 4);
159800acda68SSteve Glendinning 			cpu_to_le32s(&csum_preamble);
1599f7b29271SSteve Glendinning 			memcpy(skb->data, &csum_preamble, 4);
1600f7b29271SSteve Glendinning 		}
160111bc3088SSteve Glendinning 	}
1602f7b29271SSteve Glendinning 
16032f7ca802SSteve Glendinning 	skb_push(skb, 4);
16042f7ca802SSteve Glendinning 	tx_cmd_b = (u32)(skb->len - 4);
1605f7b29271SSteve Glendinning 	if (csum)
1606f7b29271SSteve Glendinning 		tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
16072f7ca802SSteve Glendinning 	cpu_to_le32s(&tx_cmd_b);
16082f7ca802SSteve Glendinning 	memcpy(skb->data, &tx_cmd_b, 4);
16092f7ca802SSteve Glendinning 
16102f7ca802SSteve Glendinning 	skb_push(skb, 4);
16112f7ca802SSteve Glendinning 	tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ |
16122f7ca802SSteve Glendinning 		TX_CMD_A_LAST_SEG_;
16132f7ca802SSteve Glendinning 	cpu_to_le32s(&tx_cmd_a);
16142f7ca802SSteve Glendinning 	memcpy(skb->data, &tx_cmd_a, 4);
16152f7ca802SSteve Glendinning 
16162f7ca802SSteve Glendinning 	return skb;
16172f7ca802SSteve Glendinning }
16182f7ca802SSteve Glendinning 
16192f7ca802SSteve Glendinning static const struct driver_info smsc95xx_info = {
16202f7ca802SSteve Glendinning 	.description	= "smsc95xx USB 2.0 Ethernet",
16212f7ca802SSteve Glendinning 	.bind		= smsc95xx_bind,
16222f7ca802SSteve Glendinning 	.unbind		= smsc95xx_unbind,
16232f7ca802SSteve Glendinning 	.link_reset	= smsc95xx_link_reset,
16242f7ca802SSteve Glendinning 	.reset		= smsc95xx_reset,
16252f7ca802SSteve Glendinning 	.rx_fixup	= smsc95xx_rx_fixup,
16262f7ca802SSteve Glendinning 	.tx_fixup	= smsc95xx_tx_fixup,
16272f7ca802SSteve Glendinning 	.status		= smsc95xx_status,
162807d69d42SPaolo Pisati 	.flags		= FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
16292f7ca802SSteve Glendinning };
16302f7ca802SSteve Glendinning 
16312f7ca802SSteve Glendinning static const struct usb_device_id products[] = {
16322f7ca802SSteve Glendinning 	{
16332f7ca802SSteve Glendinning 		/* SMSC9500 USB Ethernet Device */
16342f7ca802SSteve Glendinning 		USB_DEVICE(0x0424, 0x9500),
16352f7ca802SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16362f7ca802SSteve Glendinning 	},
1637726474b8SSteve Glendinning 	{
16386f41d12bSSteve Glendinning 		/* SMSC9505 USB Ethernet Device */
16396f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9505),
16406f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16416f41d12bSSteve Glendinning 	},
16426f41d12bSSteve Glendinning 	{
16436f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device */
16446f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9E00),
16456f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16466f41d12bSSteve Glendinning 	},
16476f41d12bSSteve Glendinning 	{
16486f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device */
16496f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9E01),
16506f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16516f41d12bSSteve Glendinning 	},
16526f41d12bSSteve Glendinning 	{
1653726474b8SSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device */
1654726474b8SSteve Glendinning 		USB_DEVICE(0x0424, 0xec00),
1655726474b8SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
1656726474b8SSteve Glendinning 	},
16576f41d12bSSteve Glendinning 	{
16586f41d12bSSteve Glendinning 		/* SMSC9500 USB Ethernet Device (SAL10) */
16596f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9900),
16606f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16616f41d12bSSteve Glendinning 	},
16626f41d12bSSteve Glendinning 	{
16636f41d12bSSteve Glendinning 		/* SMSC9505 USB Ethernet Device (SAL10) */
16646f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9901),
16656f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16666f41d12bSSteve Glendinning 	},
16676f41d12bSSteve Glendinning 	{
16686f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (SAL10) */
16696f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9902),
16706f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16716f41d12bSSteve Glendinning 	},
16726f41d12bSSteve Glendinning 	{
16736f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device (SAL10) */
16746f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9903),
16756f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16766f41d12bSSteve Glendinning 	},
16776f41d12bSSteve Glendinning 	{
16786f41d12bSSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
16796f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9904),
16806f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16816f41d12bSSteve Glendinning 	},
16826f41d12bSSteve Glendinning 	{
16836f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (HAL) */
16846f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9905),
16856f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16866f41d12bSSteve Glendinning 	},
16876f41d12bSSteve Glendinning 	{
16886f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device (HAL) */
16896f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9906),
16906f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16916f41d12bSSteve Glendinning 	},
16926f41d12bSSteve Glendinning 	{
16936f41d12bSSteve Glendinning 		/* SMSC9500 USB Ethernet Device (Alternate ID) */
16946f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9907),
16956f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16966f41d12bSSteve Glendinning 	},
16976f41d12bSSteve Glendinning 	{
16986f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (Alternate ID) */
16996f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9908),
17006f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
17016f41d12bSSteve Glendinning 	},
17026f41d12bSSteve Glendinning 	{
17036f41d12bSSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
17046f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9909),
17056f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
17066f41d12bSSteve Glendinning 	},
170788edaa41SSteve Glendinning 	{
170888edaa41SSteve Glendinning 		/* SMSC LAN9530 USB Ethernet Device */
170988edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9530),
171088edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
171188edaa41SSteve Glendinning 	},
171288edaa41SSteve Glendinning 	{
171388edaa41SSteve Glendinning 		/* SMSC LAN9730 USB Ethernet Device */
171488edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9730),
171588edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
171688edaa41SSteve Glendinning 	},
171788edaa41SSteve Glendinning 	{
171888edaa41SSteve Glendinning 		/* SMSC LAN89530 USB Ethernet Device */
171988edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9E08),
172088edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
172188edaa41SSteve Glendinning 	},
17222f7ca802SSteve Glendinning 	{ },		/* END */
17232f7ca802SSteve Glendinning };
17242f7ca802SSteve Glendinning MODULE_DEVICE_TABLE(usb, products);
17252f7ca802SSteve Glendinning 
17262f7ca802SSteve Glendinning static struct usb_driver smsc95xx_driver = {
17272f7ca802SSteve Glendinning 	.name		= "smsc95xx",
17282f7ca802SSteve Glendinning 	.id_table	= products,
17292f7ca802SSteve Glendinning 	.probe		= usbnet_probe,
1730b5a04475SSteve Glendinning 	.suspend	= smsc95xx_suspend,
1731e0e474a8SSteve Glendinning 	.resume		= smsc95xx_resume,
1732e0e474a8SSteve Glendinning 	.reset_resume	= smsc95xx_resume,
17332f7ca802SSteve Glendinning 	.disconnect	= usbnet_disconnect,
1734e1f12eb6SSarah Sharp 	.disable_hub_initiated_lpm = 1,
17352f7ca802SSteve Glendinning };
17362f7ca802SSteve Glendinning 
1737d632eb1bSGreg Kroah-Hartman module_usb_driver(smsc95xx_driver);
17382f7ca802SSteve Glendinning 
17392f7ca802SSteve Glendinning MODULE_AUTHOR("Nancy Lin");
174090b24cfbSSteve Glendinning MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
17412f7ca802SSteve Glendinning MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
17422f7ca802SSteve Glendinning MODULE_LICENSE("GPL");
1743