xref: /linux/drivers/net/usb/smsc95xx.c (revision 1e1d74124257413cc26e53898560c1eac31cc798)
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))
98*1e1d7412SJoe Perches 		netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
99*1e1d7412SJoe 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))
128*1e1d7412SJoe Perches 		netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
129*1e1d7412SJoe 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 static int smsc95xx_set_feature(struct usbnet *dev, u32 feature)
158e0e474a8SSteve Glendinning {
159e0e474a8SSteve Glendinning 	if (WARN_ON_ONCE(!dev))
160e0e474a8SSteve Glendinning 		return -EINVAL;
161e0e474a8SSteve Glendinning 
162ec32115dSMing Lei 	return usbnet_write_cmd_nopm(dev, USB_REQ_SET_FEATURE,
163ec32115dSMing Lei 				     USB_RECIP_DEVICE, feature, 0,
164ec32115dSMing Lei 				     NULL, 0);
165e0e474a8SSteve Glendinning }
166e0e474a8SSteve Glendinning 
167e0e474a8SSteve Glendinning static int smsc95xx_clear_feature(struct usbnet *dev, u32 feature)
168e0e474a8SSteve Glendinning {
169e0e474a8SSteve Glendinning 	if (WARN_ON_ONCE(!dev))
170e0e474a8SSteve Glendinning 		return -EINVAL;
171e0e474a8SSteve Glendinning 
172ec32115dSMing Lei 	return usbnet_write_cmd_nopm(dev, USB_REQ_CLEAR_FEATURE,
173ec32115dSMing Lei 				     USB_RECIP_DEVICE, feature,
174ec32115dSMing Lei 				     0, NULL, 0);
175e0e474a8SSteve Glendinning }
176e0e474a8SSteve Glendinning 
1772f7ca802SSteve Glendinning /* Loop until the read is completed with timeout
1782f7ca802SSteve Glendinning  * called with phy_mutex held */
179e5e3af83SSteve Glendinning static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
180e5e3af83SSteve Glendinning 						     int in_pm)
1812f7ca802SSteve Glendinning {
1822f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
1832f7ca802SSteve Glendinning 	u32 val;
184769ea6d8SSteve Glendinning 	int ret;
1852f7ca802SSteve Glendinning 
1862f7ca802SSteve Glendinning 	do {
187e5e3af83SSteve Glendinning 		ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
188*1e1d7412SJoe Perches 		check_warn_return(ret, "Error reading MII_ACCESS\n");
1892f7ca802SSteve Glendinning 		if (!(val & MII_BUSY_))
1902f7ca802SSteve Glendinning 			return 0;
1912f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
1922f7ca802SSteve Glendinning 
1932f7ca802SSteve Glendinning 	return -EIO;
1942f7ca802SSteve Glendinning }
1952f7ca802SSteve Glendinning 
196e5e3af83SSteve Glendinning static int __smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
197e5e3af83SSteve Glendinning 				int in_pm)
1982f7ca802SSteve Glendinning {
1992f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
2002f7ca802SSteve Glendinning 	u32 val, addr;
201769ea6d8SSteve Glendinning 	int ret;
2022f7ca802SSteve Glendinning 
2032f7ca802SSteve Glendinning 	mutex_lock(&dev->phy_mutex);
2042f7ca802SSteve Glendinning 
2052f7ca802SSteve Glendinning 	/* confirm MII not busy */
206e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
207*1e1d7412SJoe Perches 	check_warn_goto_done(ret, "MII is busy in smsc95xx_mdio_read\n");
2082f7ca802SSteve Glendinning 
2092f7ca802SSteve Glendinning 	/* set the address, index & direction (read from PHY) */
2102f7ca802SSteve Glendinning 	phy_id &= dev->mii.phy_id_mask;
2112f7ca802SSteve Glendinning 	idx &= dev->mii.reg_num_mask;
21280928805SSteve Glendinning 	addr = (phy_id << 11) | (idx << 6) | MII_READ_ | MII_BUSY_;
213e5e3af83SSteve Glendinning 	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
214*1e1d7412SJoe Perches 	check_warn_goto_done(ret, "Error writing MII_ADDR\n");
2152f7ca802SSteve Glendinning 
216e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
217*1e1d7412SJoe Perches 	check_warn_goto_done(ret, "Timed out reading MII reg %02X\n", idx);
218769ea6d8SSteve Glendinning 
219e5e3af83SSteve Glendinning 	ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
220*1e1d7412SJoe Perches 	check_warn_goto_done(ret, "Error reading MII_DATA\n");
221769ea6d8SSteve Glendinning 
222769ea6d8SSteve Glendinning 	ret = (u16)(val & 0xFFFF);
223769ea6d8SSteve Glendinning 
224769ea6d8SSteve Glendinning done:
2252f7ca802SSteve Glendinning 	mutex_unlock(&dev->phy_mutex);
226769ea6d8SSteve Glendinning 	return ret;
2272f7ca802SSteve Glendinning }
2282f7ca802SSteve Glendinning 
229e5e3af83SSteve Glendinning static void __smsc95xx_mdio_write(struct net_device *netdev, int phy_id,
230e5e3af83SSteve Glendinning 				  int idx, int regval, int in_pm)
2312f7ca802SSteve Glendinning {
2322f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
2332f7ca802SSteve Glendinning 	u32 val, addr;
234769ea6d8SSteve Glendinning 	int ret;
2352f7ca802SSteve Glendinning 
2362f7ca802SSteve Glendinning 	mutex_lock(&dev->phy_mutex);
2372f7ca802SSteve Glendinning 
2382f7ca802SSteve Glendinning 	/* confirm MII not busy */
239e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
240*1e1d7412SJoe Perches 	check_warn_goto_done(ret, "MII is busy in smsc95xx_mdio_write\n");
2412f7ca802SSteve Glendinning 
2422f7ca802SSteve Glendinning 	val = regval;
243e5e3af83SSteve Glendinning 	ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
244*1e1d7412SJoe Perches 	check_warn_goto_done(ret, "Error writing MII_DATA\n");
2452f7ca802SSteve Glendinning 
2462f7ca802SSteve Glendinning 	/* set the address, index & direction (write to PHY) */
2472f7ca802SSteve Glendinning 	phy_id &= dev->mii.phy_id_mask;
2482f7ca802SSteve Glendinning 	idx &= dev->mii.reg_num_mask;
24980928805SSteve Glendinning 	addr = (phy_id << 11) | (idx << 6) | MII_WRITE_ | MII_BUSY_;
250e5e3af83SSteve Glendinning 	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
251*1e1d7412SJoe Perches 	check_warn_goto_done(ret, "Error writing MII_ADDR\n");
2522f7ca802SSteve Glendinning 
253e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
254*1e1d7412SJoe Perches 	check_warn_goto_done(ret, "Timed out writing MII reg %02X\n", idx);
2552f7ca802SSteve Glendinning 
256769ea6d8SSteve Glendinning done:
2572f7ca802SSteve Glendinning 	mutex_unlock(&dev->phy_mutex);
2582f7ca802SSteve Glendinning }
2592f7ca802SSteve Glendinning 
260e5e3af83SSteve Glendinning static int smsc95xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
261e5e3af83SSteve Glendinning 				   int idx)
262e5e3af83SSteve Glendinning {
263e5e3af83SSteve Glendinning 	return __smsc95xx_mdio_read(netdev, phy_id, idx, 1);
264e5e3af83SSteve Glendinning }
265e5e3af83SSteve Glendinning 
266e5e3af83SSteve Glendinning static void smsc95xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
267e5e3af83SSteve Glendinning 				     int idx, int regval)
268e5e3af83SSteve Glendinning {
269e5e3af83SSteve Glendinning 	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 1);
270e5e3af83SSteve Glendinning }
271e5e3af83SSteve Glendinning 
272e5e3af83SSteve Glendinning static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
273e5e3af83SSteve Glendinning {
274e5e3af83SSteve Glendinning 	return __smsc95xx_mdio_read(netdev, phy_id, idx, 0);
275e5e3af83SSteve Glendinning }
276e5e3af83SSteve Glendinning 
277e5e3af83SSteve Glendinning static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
278e5e3af83SSteve Glendinning 				int regval)
279e5e3af83SSteve Glendinning {
280e5e3af83SSteve Glendinning 	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 0);
281e5e3af83SSteve Glendinning }
282e5e3af83SSteve Glendinning 
283769ea6d8SSteve Glendinning static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
2842f7ca802SSteve Glendinning {
2852f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
2862f7ca802SSteve Glendinning 	u32 val;
287769ea6d8SSteve Glendinning 	int ret;
2882f7ca802SSteve Glendinning 
2892f7ca802SSteve Glendinning 	do {
290769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
291*1e1d7412SJoe Perches 		check_warn_return(ret, "Error reading E2P_CMD\n");
2922f7ca802SSteve Glendinning 		if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
2932f7ca802SSteve Glendinning 			break;
2942f7ca802SSteve Glendinning 		udelay(40);
2952f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
2962f7ca802SSteve Glendinning 
2972f7ca802SSteve Glendinning 	if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
29860b86755SJoe Perches 		netdev_warn(dev->net, "EEPROM read operation timeout\n");
2992f7ca802SSteve Glendinning 		return -EIO;
3002f7ca802SSteve Glendinning 	}
3012f7ca802SSteve Glendinning 
3022f7ca802SSteve Glendinning 	return 0;
3032f7ca802SSteve Glendinning }
3042f7ca802SSteve Glendinning 
305769ea6d8SSteve Glendinning static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
3062f7ca802SSteve Glendinning {
3072f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
3082f7ca802SSteve Glendinning 	u32 val;
309769ea6d8SSteve Glendinning 	int ret;
3102f7ca802SSteve Glendinning 
3112f7ca802SSteve Glendinning 	do {
312769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
313*1e1d7412SJoe Perches 		check_warn_return(ret, "Error reading E2P_CMD\n");
3142f7ca802SSteve Glendinning 
3152f7ca802SSteve Glendinning 		if (!(val & E2P_CMD_BUSY_))
3162f7ca802SSteve Glendinning 			return 0;
3172f7ca802SSteve Glendinning 
3182f7ca802SSteve Glendinning 		udelay(40);
3192f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
3202f7ca802SSteve Glendinning 
32160b86755SJoe Perches 	netdev_warn(dev->net, "EEPROM is busy\n");
3222f7ca802SSteve Glendinning 	return -EIO;
3232f7ca802SSteve Glendinning }
3242f7ca802SSteve Glendinning 
3252f7ca802SSteve Glendinning static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
3262f7ca802SSteve Glendinning 				u8 *data)
3272f7ca802SSteve Glendinning {
3282f7ca802SSteve Glendinning 	u32 val;
3292f7ca802SSteve Glendinning 	int i, ret;
3302f7ca802SSteve Glendinning 
3312f7ca802SSteve Glendinning 	BUG_ON(!dev);
3322f7ca802SSteve Glendinning 	BUG_ON(!data);
3332f7ca802SSteve Glendinning 
3342f7ca802SSteve Glendinning 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
3352f7ca802SSteve Glendinning 	if (ret)
3362f7ca802SSteve Glendinning 		return ret;
3372f7ca802SSteve Glendinning 
3382f7ca802SSteve Glendinning 	for (i = 0; i < length; i++) {
3392f7ca802SSteve Glendinning 		val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
340769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
341*1e1d7412SJoe Perches 		check_warn_return(ret, "Error writing E2P_CMD\n");
3422f7ca802SSteve Glendinning 
3432f7ca802SSteve Glendinning 		ret = smsc95xx_wait_eeprom(dev);
3442f7ca802SSteve Glendinning 		if (ret < 0)
3452f7ca802SSteve Glendinning 			return ret;
3462f7ca802SSteve Glendinning 
347769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
348*1e1d7412SJoe Perches 		check_warn_return(ret, "Error reading E2P_DATA\n");
3492f7ca802SSteve Glendinning 
3502f7ca802SSteve Glendinning 		data[i] = val & 0xFF;
3512f7ca802SSteve Glendinning 		offset++;
3522f7ca802SSteve Glendinning 	}
3532f7ca802SSteve Glendinning 
3542f7ca802SSteve Glendinning 	return 0;
3552f7ca802SSteve Glendinning }
3562f7ca802SSteve Glendinning 
3572f7ca802SSteve Glendinning static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
3582f7ca802SSteve Glendinning 				 u8 *data)
3592f7ca802SSteve Glendinning {
3602f7ca802SSteve Glendinning 	u32 val;
3612f7ca802SSteve Glendinning 	int i, ret;
3622f7ca802SSteve Glendinning 
3632f7ca802SSteve Glendinning 	BUG_ON(!dev);
3642f7ca802SSteve Glendinning 	BUG_ON(!data);
3652f7ca802SSteve Glendinning 
3662f7ca802SSteve Glendinning 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
3672f7ca802SSteve Glendinning 	if (ret)
3682f7ca802SSteve Glendinning 		return ret;
3692f7ca802SSteve Glendinning 
3702f7ca802SSteve Glendinning 	/* Issue write/erase enable command */
3712f7ca802SSteve Glendinning 	val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
372769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, E2P_CMD, val);
373*1e1d7412SJoe Perches 	check_warn_return(ret, "Error writing E2P_DATA\n");
3742f7ca802SSteve Glendinning 
3752f7ca802SSteve Glendinning 	ret = smsc95xx_wait_eeprom(dev);
3762f7ca802SSteve Glendinning 	if (ret < 0)
3772f7ca802SSteve Glendinning 		return ret;
3782f7ca802SSteve Glendinning 
3792f7ca802SSteve Glendinning 	for (i = 0; i < length; i++) {
3802f7ca802SSteve Glendinning 
3812f7ca802SSteve Glendinning 		/* Fill data register */
3822f7ca802SSteve Glendinning 		val = data[i];
383769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_DATA, val);
384*1e1d7412SJoe Perches 		check_warn_return(ret, "Error writing E2P_DATA\n");
3852f7ca802SSteve Glendinning 
3862f7ca802SSteve Glendinning 		/* Send "write" command */
3872f7ca802SSteve Glendinning 		val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
388769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
389*1e1d7412SJoe Perches 		check_warn_return(ret, "Error writing E2P_CMD\n");
3902f7ca802SSteve Glendinning 
3912f7ca802SSteve Glendinning 		ret = smsc95xx_wait_eeprom(dev);
3922f7ca802SSteve Glendinning 		if (ret < 0)
3932f7ca802SSteve Glendinning 			return ret;
3942f7ca802SSteve Glendinning 
3952f7ca802SSteve Glendinning 		offset++;
3962f7ca802SSteve Glendinning 	}
3972f7ca802SSteve Glendinning 
3982f7ca802SSteve Glendinning 	return 0;
3992f7ca802SSteve Glendinning }
4002f7ca802SSteve Glendinning 
401769ea6d8SSteve Glendinning static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
402769ea6d8SSteve Glendinning 						 u32 *data)
4032f7ca802SSteve Glendinning {
4041d74a6bdSSteve Glendinning 	const u16 size = 4;
40572108fd2SMing Lei 	int ret;
4062f7ca802SSteve Glendinning 
40772108fd2SMing Lei 	ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
40872108fd2SMing Lei 				     USB_DIR_OUT | USB_TYPE_VENDOR |
40972108fd2SMing Lei 				     USB_RECIP_DEVICE,
41072108fd2SMing Lei 				     0, index, data, size);
41172108fd2SMing Lei 	if (ret < 0)
41272108fd2SMing Lei 		netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
41372108fd2SMing Lei 			    ret);
41472108fd2SMing Lei 	return ret;
4152f7ca802SSteve Glendinning }
4162f7ca802SSteve Glendinning 
4172f7ca802SSteve Glendinning /* returns hash bit number for given MAC address
4182f7ca802SSteve Glendinning  * example:
4192f7ca802SSteve Glendinning  * 01 00 5E 00 00 01 -> returns bit number 31 */
4202f7ca802SSteve Glendinning static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
4212f7ca802SSteve Glendinning {
4222f7ca802SSteve Glendinning 	return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
4232f7ca802SSteve Glendinning }
4242f7ca802SSteve Glendinning 
4252f7ca802SSteve Glendinning static void smsc95xx_set_multicast(struct net_device *netdev)
4262f7ca802SSteve Glendinning {
4272f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
4282f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
4292f7ca802SSteve Glendinning 	unsigned long flags;
430769ea6d8SSteve Glendinning 	int ret;
4312f7ca802SSteve Glendinning 
4323c0f3c60SMarc Zyngier 	pdata->hash_hi = 0;
4333c0f3c60SMarc Zyngier 	pdata->hash_lo = 0;
4343c0f3c60SMarc Zyngier 
4352f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
4362f7ca802SSteve Glendinning 
4372f7ca802SSteve Glendinning 	if (dev->net->flags & IFF_PROMISC) {
438a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
4392f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_PRMS_;
4402f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
4412f7ca802SSteve Glendinning 	} else if (dev->net->flags & IFF_ALLMULTI) {
442a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
4432f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_MCPAS_;
4442f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
4454cd24eafSJiri Pirko 	} else if (!netdev_mc_empty(dev->net)) {
44622bedad3SJiri Pirko 		struct netdev_hw_addr *ha;
4472f7ca802SSteve Glendinning 
4482f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_HPFILT_;
4492f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
4502f7ca802SSteve Glendinning 
45122bedad3SJiri Pirko 		netdev_for_each_mc_addr(ha, netdev) {
45222bedad3SJiri Pirko 			u32 bitnum = smsc95xx_hash(ha->addr);
4532f7ca802SSteve Glendinning 			u32 mask = 0x01 << (bitnum & 0x1F);
4542f7ca802SSteve Glendinning 			if (bitnum & 0x20)
4553c0f3c60SMarc Zyngier 				pdata->hash_hi |= mask;
4562f7ca802SSteve Glendinning 			else
4573c0f3c60SMarc Zyngier 				pdata->hash_lo |= mask;
4582f7ca802SSteve Glendinning 		}
4592f7ca802SSteve Glendinning 
460a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
4613c0f3c60SMarc Zyngier 				   pdata->hash_hi, pdata->hash_lo);
4622f7ca802SSteve Glendinning 	} else {
463a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "receive own packets only\n");
4642f7ca802SSteve Glendinning 		pdata->mac_cr &=
4652f7ca802SSteve Glendinning 			~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
4662f7ca802SSteve Glendinning 	}
4672f7ca802SSteve Glendinning 
4682f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
4692f7ca802SSteve Glendinning 
4702f7ca802SSteve Glendinning 	/* Initiate async writes, as we can't wait for completion here */
471769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, HASHH, &pdata->hash_hi);
472*1e1d7412SJoe Perches 	check_warn(ret, "failed to initiate async write to HASHH\n");
473769ea6d8SSteve Glendinning 
474769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, HASHL, &pdata->hash_lo);
475*1e1d7412SJoe Perches 	check_warn(ret, "failed to initiate async write to HASHL\n");
476769ea6d8SSteve Glendinning 
477769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, MAC_CR, &pdata->mac_cr);
478*1e1d7412SJoe Perches 	check_warn(ret, "failed to initiate async write to MAC_CR\n");
4792f7ca802SSteve Glendinning }
4802f7ca802SSteve Glendinning 
481769ea6d8SSteve Glendinning static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
4822f7ca802SSteve Glendinning 					   u16 lcladv, u16 rmtadv)
4832f7ca802SSteve Glendinning {
4842f7ca802SSteve Glendinning 	u32 flow, afc_cfg = 0;
4852f7ca802SSteve Glendinning 
4862f7ca802SSteve Glendinning 	int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
487*1e1d7412SJoe Perches 	check_warn_return(ret, "Error reading AFC_CFG\n");
4882f7ca802SSteve Glendinning 
4892f7ca802SSteve Glendinning 	if (duplex == DUPLEX_FULL) {
490bc02ff95SSteve Glendinning 		u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
4912f7ca802SSteve Glendinning 
4922f7ca802SSteve Glendinning 		if (cap & FLOW_CTRL_RX)
4932f7ca802SSteve Glendinning 			flow = 0xFFFF0002;
4942f7ca802SSteve Glendinning 		else
4952f7ca802SSteve Glendinning 			flow = 0;
4962f7ca802SSteve Glendinning 
4972f7ca802SSteve Glendinning 		if (cap & FLOW_CTRL_TX)
4982f7ca802SSteve Glendinning 			afc_cfg |= 0xF;
4992f7ca802SSteve Glendinning 		else
5002f7ca802SSteve Glendinning 			afc_cfg &= ~0xF;
5012f7ca802SSteve Glendinning 
502a475f603SJoe Perches 		netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
50360b86755SJoe Perches 				   cap & FLOW_CTRL_RX ? "enabled" : "disabled",
50460b86755SJoe Perches 				   cap & FLOW_CTRL_TX ? "enabled" : "disabled");
5052f7ca802SSteve Glendinning 	} else {
506a475f603SJoe Perches 		netif_dbg(dev, link, dev->net, "half duplex\n");
5072f7ca802SSteve Glendinning 		flow = 0;
5082f7ca802SSteve Glendinning 		afc_cfg |= 0xF;
5092f7ca802SSteve Glendinning 	}
5102f7ca802SSteve Glendinning 
511769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, FLOW, flow);
512*1e1d7412SJoe Perches 	check_warn_return(ret, "Error writing FLOW\n");
513769ea6d8SSteve Glendinning 
514769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
515*1e1d7412SJoe Perches 	check_warn_return(ret, "Error writing AFC_CFG\n");
516769ea6d8SSteve Glendinning 
517769ea6d8SSteve Glendinning 	return 0;
5182f7ca802SSteve Glendinning }
5192f7ca802SSteve Glendinning 
5202f7ca802SSteve Glendinning static int smsc95xx_link_reset(struct usbnet *dev)
5212f7ca802SSteve Glendinning {
5222f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
5232f7ca802SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
5248ae6dacaSDavid Decotigny 	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
5252f7ca802SSteve Glendinning 	unsigned long flags;
5262f7ca802SSteve Glendinning 	u16 lcladv, rmtadv;
527769ea6d8SSteve Glendinning 	int ret;
5282f7ca802SSteve Glendinning 
5292f7ca802SSteve Glendinning 	/* clear interrupt status */
530769ea6d8SSteve Glendinning 	ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
531*1e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PHY_INT_SRC\n");
532769ea6d8SSteve Glendinning 
533769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
534*1e1d7412SJoe Perches 	check_warn_return(ret, "Error writing INT_STS\n");
5352f7ca802SSteve Glendinning 
5362f7ca802SSteve Glendinning 	mii_check_media(mii, 1, 1);
5372f7ca802SSteve Glendinning 	mii_ethtool_gset(&dev->mii, &ecmd);
5382f7ca802SSteve Glendinning 	lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
5392f7ca802SSteve Glendinning 	rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
5402f7ca802SSteve Glendinning 
5418ae6dacaSDavid Decotigny 	netif_dbg(dev, link, dev->net,
5428ae6dacaSDavid Decotigny 		  "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
5438ae6dacaSDavid Decotigny 		  ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
5442f7ca802SSteve Glendinning 
5452f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
5462f7ca802SSteve Glendinning 	if (ecmd.duplex != DUPLEX_FULL) {
5472f7ca802SSteve Glendinning 		pdata->mac_cr &= ~MAC_CR_FDPX_;
5482f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_RCVOWN_;
5492f7ca802SSteve Glendinning 	} else {
5502f7ca802SSteve Glendinning 		pdata->mac_cr &= ~MAC_CR_RCVOWN_;
5512f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_FDPX_;
5522f7ca802SSteve Glendinning 	}
5532f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
5542f7ca802SSteve Glendinning 
555769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
556*1e1d7412SJoe Perches 	check_warn_return(ret, "Error writing MAC_CR\n");
5572f7ca802SSteve Glendinning 
558769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
559*1e1d7412SJoe Perches 	check_warn_return(ret, "Error updating PHY flow control\n");
5602f7ca802SSteve Glendinning 
5612f7ca802SSteve Glendinning 	return 0;
5622f7ca802SSteve Glendinning }
5632f7ca802SSteve Glendinning 
5642f7ca802SSteve Glendinning static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
5652f7ca802SSteve Glendinning {
5662f7ca802SSteve Glendinning 	u32 intdata;
5672f7ca802SSteve Glendinning 
5682f7ca802SSteve Glendinning 	if (urb->actual_length != 4) {
56960b86755SJoe Perches 		netdev_warn(dev->net, "unexpected urb length %d\n",
57060b86755SJoe Perches 			    urb->actual_length);
5712f7ca802SSteve Glendinning 		return;
5722f7ca802SSteve Glendinning 	}
5732f7ca802SSteve Glendinning 
5742f7ca802SSteve Glendinning 	memcpy(&intdata, urb->transfer_buffer, 4);
5751d74a6bdSSteve Glendinning 	le32_to_cpus(&intdata);
5762f7ca802SSteve Glendinning 
577a475f603SJoe Perches 	netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
5782f7ca802SSteve Glendinning 
5792f7ca802SSteve Glendinning 	if (intdata & INT_ENP_PHY_INT_)
5802f7ca802SSteve Glendinning 		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
5812f7ca802SSteve Glendinning 	else
58260b86755SJoe Perches 		netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
58360b86755SJoe Perches 			    intdata);
5842f7ca802SSteve Glendinning }
5852f7ca802SSteve Glendinning 
586f7b29271SSteve Glendinning /* Enable or disable Tx & Rx checksum offload engines */
587c8f44affSMichał Mirosław static int smsc95xx_set_features(struct net_device *netdev,
588c8f44affSMichał Mirosław 	netdev_features_t features)
5892f7ca802SSteve Glendinning {
59078e47fe4SMichał Mirosław 	struct usbnet *dev = netdev_priv(netdev);
5912f7ca802SSteve Glendinning 	u32 read_buf;
59278e47fe4SMichał Mirosław 	int ret;
59378e47fe4SMichał Mirosław 
59478e47fe4SMichał Mirosław 	ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
595769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read COE_CR: %d\n", ret);
5962f7ca802SSteve Glendinning 
59778e47fe4SMichał Mirosław 	if (features & NETIF_F_HW_CSUM)
598f7b29271SSteve Glendinning 		read_buf |= Tx_COE_EN_;
599f7b29271SSteve Glendinning 	else
600f7b29271SSteve Glendinning 		read_buf &= ~Tx_COE_EN_;
601f7b29271SSteve Glendinning 
60278e47fe4SMichał Mirosław 	if (features & NETIF_F_RXCSUM)
6032f7ca802SSteve Glendinning 		read_buf |= Rx_COE_EN_;
6042f7ca802SSteve Glendinning 	else
6052f7ca802SSteve Glendinning 		read_buf &= ~Rx_COE_EN_;
6062f7ca802SSteve Glendinning 
6072f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
608769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write COE_CR: %d\n", ret);
6092f7ca802SSteve Glendinning 
610a475f603SJoe Perches 	netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
6112f7ca802SSteve Glendinning 	return 0;
6122f7ca802SSteve Glendinning }
6132f7ca802SSteve Glendinning 
6142f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
6152f7ca802SSteve Glendinning {
6162f7ca802SSteve Glendinning 	return MAX_EEPROM_SIZE;
6172f7ca802SSteve Glendinning }
6182f7ca802SSteve Glendinning 
6192f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
6202f7ca802SSteve Glendinning 				       struct ethtool_eeprom *ee, u8 *data)
6212f7ca802SSteve Glendinning {
6222f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
6232f7ca802SSteve Glendinning 
6242f7ca802SSteve Glendinning 	ee->magic = LAN95XX_EEPROM_MAGIC;
6252f7ca802SSteve Glendinning 
6262f7ca802SSteve Glendinning 	return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
6272f7ca802SSteve Glendinning }
6282f7ca802SSteve Glendinning 
6292f7ca802SSteve Glendinning static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
6302f7ca802SSteve Glendinning 				       struct ethtool_eeprom *ee, u8 *data)
6312f7ca802SSteve Glendinning {
6322f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
6332f7ca802SSteve Glendinning 
6342f7ca802SSteve Glendinning 	if (ee->magic != LAN95XX_EEPROM_MAGIC) {
63560b86755SJoe Perches 		netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
6362f7ca802SSteve Glendinning 			    ee->magic);
6372f7ca802SSteve Glendinning 		return -EINVAL;
6382f7ca802SSteve Glendinning 	}
6392f7ca802SSteve Glendinning 
6402f7ca802SSteve Glendinning 	return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
6412f7ca802SSteve Glendinning }
6422f7ca802SSteve Glendinning 
6439fa32e94SEmeric Vigier static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
6449fa32e94SEmeric Vigier {
6459fa32e94SEmeric Vigier 	/* all smsc95xx registers */
6469fa32e94SEmeric Vigier 	return COE_CR - ID_REV + 1;
6479fa32e94SEmeric Vigier }
6489fa32e94SEmeric Vigier 
6499fa32e94SEmeric Vigier static void
6509fa32e94SEmeric Vigier smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
6519fa32e94SEmeric Vigier 			 void *buf)
6529fa32e94SEmeric Vigier {
6539fa32e94SEmeric Vigier 	struct usbnet *dev = netdev_priv(netdev);
654d348446bSDan Carpenter 	unsigned int i, j;
655d348446bSDan Carpenter 	int retval;
6569fa32e94SEmeric Vigier 	u32 *data = buf;
6579fa32e94SEmeric Vigier 
6589fa32e94SEmeric Vigier 	retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
6599fa32e94SEmeric Vigier 	if (retval < 0) {
6609fa32e94SEmeric Vigier 		netdev_warn(netdev, "REGS: cannot read ID_REV\n");
6619fa32e94SEmeric Vigier 		return;
6629fa32e94SEmeric Vigier 	}
6639fa32e94SEmeric Vigier 
6649fa32e94SEmeric Vigier 	for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
6659fa32e94SEmeric Vigier 		retval = smsc95xx_read_reg(dev, i, &data[j]);
6669fa32e94SEmeric Vigier 		if (retval < 0) {
6679fa32e94SEmeric Vigier 			netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
6689fa32e94SEmeric Vigier 			return;
6699fa32e94SEmeric Vigier 		}
6709fa32e94SEmeric Vigier 	}
6719fa32e94SEmeric Vigier }
6729fa32e94SEmeric Vigier 
673e0e474a8SSteve Glendinning static void smsc95xx_ethtool_get_wol(struct net_device *net,
674e0e474a8SSteve Glendinning 				     struct ethtool_wolinfo *wolinfo)
675e0e474a8SSteve Glendinning {
676e0e474a8SSteve Glendinning 	struct usbnet *dev = netdev_priv(net);
677e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
678e0e474a8SSteve Glendinning 
679e0e474a8SSteve Glendinning 	wolinfo->supported = SUPPORTED_WAKE;
680e0e474a8SSteve Glendinning 	wolinfo->wolopts = pdata->wolopts;
681e0e474a8SSteve Glendinning }
682e0e474a8SSteve Glendinning 
683e0e474a8SSteve Glendinning static int smsc95xx_ethtool_set_wol(struct net_device *net,
684e0e474a8SSteve Glendinning 				    struct ethtool_wolinfo *wolinfo)
685e0e474a8SSteve Glendinning {
686e0e474a8SSteve Glendinning 	struct usbnet *dev = netdev_priv(net);
687e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
688e0e474a8SSteve Glendinning 
689e0e474a8SSteve Glendinning 	pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
690e0e474a8SSteve Glendinning 	return 0;
691e0e474a8SSteve Glendinning }
692e0e474a8SSteve Glendinning 
6930fc0b732SStephen Hemminger static const struct ethtool_ops smsc95xx_ethtool_ops = {
6942f7ca802SSteve Glendinning 	.get_link	= usbnet_get_link,
6952f7ca802SSteve Glendinning 	.nway_reset	= usbnet_nway_reset,
6962f7ca802SSteve Glendinning 	.get_drvinfo	= usbnet_get_drvinfo,
6972f7ca802SSteve Glendinning 	.get_msglevel	= usbnet_get_msglevel,
6982f7ca802SSteve Glendinning 	.set_msglevel	= usbnet_set_msglevel,
6992f7ca802SSteve Glendinning 	.get_settings	= usbnet_get_settings,
7002f7ca802SSteve Glendinning 	.set_settings	= usbnet_set_settings,
7012f7ca802SSteve Glendinning 	.get_eeprom_len	= smsc95xx_ethtool_get_eeprom_len,
7022f7ca802SSteve Glendinning 	.get_eeprom	= smsc95xx_ethtool_get_eeprom,
7032f7ca802SSteve Glendinning 	.set_eeprom	= smsc95xx_ethtool_set_eeprom,
7049fa32e94SEmeric Vigier 	.get_regs_len	= smsc95xx_ethtool_getregslen,
7059fa32e94SEmeric Vigier 	.get_regs	= smsc95xx_ethtool_getregs,
706e0e474a8SSteve Glendinning 	.get_wol	= smsc95xx_ethtool_get_wol,
707e0e474a8SSteve Glendinning 	.set_wol	= smsc95xx_ethtool_set_wol,
7082f7ca802SSteve Glendinning };
7092f7ca802SSteve Glendinning 
7102f7ca802SSteve Glendinning static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
7112f7ca802SSteve Glendinning {
7122f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
7132f7ca802SSteve Glendinning 
7142f7ca802SSteve Glendinning 	if (!netif_running(netdev))
7152f7ca802SSteve Glendinning 		return -EINVAL;
7162f7ca802SSteve Glendinning 
7172f7ca802SSteve Glendinning 	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
7182f7ca802SSteve Glendinning }
7192f7ca802SSteve Glendinning 
7202f7ca802SSteve Glendinning static void smsc95xx_init_mac_address(struct usbnet *dev)
7212f7ca802SSteve Glendinning {
7222f7ca802SSteve Glendinning 	/* try reading mac address from EEPROM */
7232f7ca802SSteve Glendinning 	if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
7242f7ca802SSteve Glendinning 			dev->net->dev_addr) == 0) {
7252f7ca802SSteve Glendinning 		if (is_valid_ether_addr(dev->net->dev_addr)) {
7262f7ca802SSteve Glendinning 			/* eeprom values are valid so use them */
727a475f603SJoe Perches 			netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
7282f7ca802SSteve Glendinning 			return;
7292f7ca802SSteve Glendinning 		}
7302f7ca802SSteve Glendinning 	}
7312f7ca802SSteve Glendinning 
7322f7ca802SSteve Glendinning 	/* no eeprom, or eeprom values are invalid. generate random MAC */
733f2cedb63SDanny Kukawka 	eth_hw_addr_random(dev->net);
734c7e12eadSJoe Perches 	netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
7352f7ca802SSteve Glendinning }
7362f7ca802SSteve Glendinning 
7372f7ca802SSteve Glendinning static int smsc95xx_set_mac_address(struct usbnet *dev)
7382f7ca802SSteve Glendinning {
7392f7ca802SSteve Glendinning 	u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
7402f7ca802SSteve Glendinning 		dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
7412f7ca802SSteve Glendinning 	u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
7422f7ca802SSteve Glendinning 	int ret;
7432f7ca802SSteve Glendinning 
7442f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
745769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write ADDRL: %d\n", ret);
7462f7ca802SSteve Glendinning 
7472f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, ADDRH, addr_hi);
748769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write ADDRH: %d\n", ret);
7492f7ca802SSteve Glendinning 
7502f7ca802SSteve Glendinning 	return 0;
7512f7ca802SSteve Glendinning }
7522f7ca802SSteve Glendinning 
7532f7ca802SSteve Glendinning /* starts the TX path */
754769ea6d8SSteve Glendinning static int smsc95xx_start_tx_path(struct usbnet *dev)
7552f7ca802SSteve Glendinning {
7562f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
7572f7ca802SSteve Glendinning 	unsigned long flags;
758769ea6d8SSteve Glendinning 	int ret;
7592f7ca802SSteve Glendinning 
7602f7ca802SSteve Glendinning 	/* Enable Tx at MAC */
7612f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
7622f7ca802SSteve Glendinning 	pdata->mac_cr |= MAC_CR_TXEN_;
7632f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
7642f7ca802SSteve Glendinning 
765769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
766769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write MAC_CR: %d\n", ret);
7672f7ca802SSteve Glendinning 
7682f7ca802SSteve Glendinning 	/* Enable Tx at SCSRs */
769769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
770769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write TX_CFG: %d\n", ret);
771769ea6d8SSteve Glendinning 
772769ea6d8SSteve Glendinning 	return 0;
7732f7ca802SSteve Glendinning }
7742f7ca802SSteve Glendinning 
7752f7ca802SSteve Glendinning /* Starts the Receive path */
776ec32115dSMing Lei static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
7772f7ca802SSteve Glendinning {
7782f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
7792f7ca802SSteve Glendinning 	unsigned long flags;
780769ea6d8SSteve Glendinning 	int ret;
7812f7ca802SSteve Glendinning 
7822f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
7832f7ca802SSteve Glendinning 	pdata->mac_cr |= MAC_CR_RXEN_;
7842f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
7852f7ca802SSteve Glendinning 
786ec32115dSMing Lei 	ret = __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
787769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write MAC_CR: %d\n", ret);
788769ea6d8SSteve Glendinning 
789769ea6d8SSteve Glendinning 	return 0;
7902f7ca802SSteve Glendinning }
7912f7ca802SSteve Glendinning 
7922f7ca802SSteve Glendinning static int smsc95xx_phy_initialize(struct usbnet *dev)
7932f7ca802SSteve Glendinning {
794769ea6d8SSteve Glendinning 	int bmcr, ret, timeout = 0;
795db443c44SSteve Glendinning 
7962f7ca802SSteve Glendinning 	/* Initialize MII structure */
7972f7ca802SSteve Glendinning 	dev->mii.dev = dev->net;
7982f7ca802SSteve Glendinning 	dev->mii.mdio_read = smsc95xx_mdio_read;
7992f7ca802SSteve Glendinning 	dev->mii.mdio_write = smsc95xx_mdio_write;
8002f7ca802SSteve Glendinning 	dev->mii.phy_id_mask = 0x1f;
8012f7ca802SSteve Glendinning 	dev->mii.reg_num_mask = 0x1f;
8022f7ca802SSteve Glendinning 	dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
8032f7ca802SSteve Glendinning 
804db443c44SSteve Glendinning 	/* reset phy and wait for reset to complete */
8052f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
806db443c44SSteve Glendinning 
807db443c44SSteve Glendinning 	do {
808db443c44SSteve Glendinning 		msleep(10);
809db443c44SSteve Glendinning 		bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
810db443c44SSteve Glendinning 		timeout++;
811d9460920SRabin Vincent 	} while ((bmcr & BMCR_RESET) && (timeout < 100));
812db443c44SSteve Glendinning 
813db443c44SSteve Glendinning 	if (timeout >= 100) {
814db443c44SSteve Glendinning 		netdev_warn(dev->net, "timeout on PHY Reset");
815db443c44SSteve Glendinning 		return -EIO;
816db443c44SSteve Glendinning 	}
817db443c44SSteve Glendinning 
8182f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
8192f7ca802SSteve Glendinning 		ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
8202f7ca802SSteve Glendinning 		ADVERTISE_PAUSE_ASYM);
8212f7ca802SSteve Glendinning 
8222f7ca802SSteve Glendinning 	/* read to clear */
823769ea6d8SSteve Glendinning 	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
824*1e1d7412SJoe Perches 	check_warn_return(ret, "Failed to read PHY_INT_SRC during init\n");
8252f7ca802SSteve Glendinning 
8262f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
8272f7ca802SSteve Glendinning 		PHY_INT_MASK_DEFAULT_);
8282f7ca802SSteve Glendinning 	mii_nway_restart(&dev->mii);
8292f7ca802SSteve Glendinning 
830a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
8312f7ca802SSteve Glendinning 	return 0;
8322f7ca802SSteve Glendinning }
8332f7ca802SSteve Glendinning 
8342f7ca802SSteve Glendinning static int smsc95xx_reset(struct usbnet *dev)
8352f7ca802SSteve Glendinning {
8362f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
8372f7ca802SSteve Glendinning 	u32 read_buf, write_buf, burst_cap;
8382f7ca802SSteve Glendinning 	int ret = 0, timeout;
8392f7ca802SSteve Glendinning 
840a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
8412f7ca802SSteve Glendinning 
8424436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
843769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write HW_CFG_LRST_ bit in HW_CFG\n");
8442f7ca802SSteve Glendinning 
8452f7ca802SSteve Glendinning 	timeout = 0;
8462f7ca802SSteve Glendinning 	do {
847cf2acec2SSteve Glendinning 		msleep(10);
8482f7ca802SSteve Glendinning 		ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
849769ea6d8SSteve Glendinning 		check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
8502f7ca802SSteve Glendinning 		timeout++;
8512f7ca802SSteve Glendinning 	} while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
8522f7ca802SSteve Glendinning 
8532f7ca802SSteve Glendinning 	if (timeout >= 100) {
85460b86755SJoe Perches 		netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
8552f7ca802SSteve Glendinning 		return ret;
8562f7ca802SSteve Glendinning 	}
8572f7ca802SSteve Glendinning 
8584436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
859769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write PM_CTRL: %d\n", ret);
8602f7ca802SSteve Glendinning 
8612f7ca802SSteve Glendinning 	timeout = 0;
8622f7ca802SSteve Glendinning 	do {
863cf2acec2SSteve Glendinning 		msleep(10);
8642f7ca802SSteve Glendinning 		ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
865769ea6d8SSteve Glendinning 		check_warn_return(ret, "Failed to read PM_CTRL: %d\n", ret);
8662f7ca802SSteve Glendinning 		timeout++;
8672f7ca802SSteve Glendinning 	} while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
8682f7ca802SSteve Glendinning 
8692f7ca802SSteve Glendinning 	if (timeout >= 100) {
87060b86755SJoe Perches 		netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
8712f7ca802SSteve Glendinning 		return ret;
8722f7ca802SSteve Glendinning 	}
8732f7ca802SSteve Glendinning 
8742f7ca802SSteve Glendinning 	ret = smsc95xx_set_mac_address(dev);
8752f7ca802SSteve Glendinning 	if (ret < 0)
8762f7ca802SSteve Glendinning 		return ret;
8772f7ca802SSteve Glendinning 
878*1e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
879*1e1d7412SJoe Perches 		  dev->net->dev_addr);
8802f7ca802SSteve Glendinning 
8812f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
882769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
8832f7ca802SSteve Glendinning 
884*1e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
885*1e1d7412SJoe Perches 		  read_buf);
8862f7ca802SSteve Glendinning 
8872f7ca802SSteve Glendinning 	read_buf |= HW_CFG_BIR_;
8882f7ca802SSteve Glendinning 
8892f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
890769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write HW_CFG_BIR_ bit in HW_CFG\n");
8912f7ca802SSteve Glendinning 
8922f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
893769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
894a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
895a475f603SJoe Perches 		  "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
89660b86755SJoe Perches 		  read_buf);
8972f7ca802SSteve Glendinning 
8982f7ca802SSteve Glendinning 	if (!turbo_mode) {
8992f7ca802SSteve Glendinning 		burst_cap = 0;
9002f7ca802SSteve Glendinning 		dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
9012f7ca802SSteve Glendinning 	} else if (dev->udev->speed == USB_SPEED_HIGH) {
9022f7ca802SSteve Glendinning 		burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
9032f7ca802SSteve Glendinning 		dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
9042f7ca802SSteve Glendinning 	} else {
9052f7ca802SSteve Glendinning 		burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
9062f7ca802SSteve Glendinning 		dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
9072f7ca802SSteve Glendinning 	}
9082f7ca802SSteve Glendinning 
909*1e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
910*1e1d7412SJoe Perches 		  (ulong)dev->rx_urb_size);
9112f7ca802SSteve Glendinning 
9122f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
913769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write BURST_CAP: %d\n", ret);
9142f7ca802SSteve Glendinning 
9152f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
916769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read BURST_CAP: %d\n", ret);
917769ea6d8SSteve Glendinning 
918a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
919a475f603SJoe Perches 		  "Read Value from BURST_CAP after writing: 0x%08x\n",
9202f7ca802SSteve Glendinning 		  read_buf);
9212f7ca802SSteve Glendinning 
9224436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
923769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write BULK_IN_DLY: %d\n", ret);
9242f7ca802SSteve Glendinning 
9252f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
926769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read BULK_IN_DLY: %d\n", ret);
927769ea6d8SSteve Glendinning 
928a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
929a475f603SJoe Perches 		  "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
93060b86755SJoe Perches 		  read_buf);
9312f7ca802SSteve Glendinning 
9322f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
933769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
934769ea6d8SSteve Glendinning 
935*1e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
936*1e1d7412SJoe Perches 		  read_buf);
9372f7ca802SSteve Glendinning 
9382f7ca802SSteve Glendinning 	if (turbo_mode)
9392f7ca802SSteve Glendinning 		read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
9402f7ca802SSteve Glendinning 
9412f7ca802SSteve Glendinning 	read_buf &= ~HW_CFG_RXDOFF_;
9422f7ca802SSteve Glendinning 
9432f7ca802SSteve Glendinning 	/* set Rx data offset=2, Make IP header aligns on word boundary. */
9442f7ca802SSteve Glendinning 	read_buf |= NET_IP_ALIGN << 9;
9452f7ca802SSteve Glendinning 
9462f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
947769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write HW_CFG: %d\n", ret);
9482f7ca802SSteve Glendinning 
9492f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
950769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
951769ea6d8SSteve Glendinning 
952a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
953a475f603SJoe Perches 		  "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
9542f7ca802SSteve Glendinning 
9554436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
956769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write INT_STS: %d\n", ret);
9572f7ca802SSteve Glendinning 
9582f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
959769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read ID_REV: %d\n", ret);
960a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
9612f7ca802SSteve Glendinning 
962f293501cSSteve Glendinning 	/* Configure GPIO pins as LED outputs */
963f293501cSSteve Glendinning 	write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
964f293501cSSteve Glendinning 		LED_GPIO_CFG_FDX_LED;
965f293501cSSteve Glendinning 	ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
966769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write LED_GPIO_CFG: %d\n", ret);
967f293501cSSteve Glendinning 
9682f7ca802SSteve Glendinning 	/* Init Tx */
9694436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, FLOW, 0);
970769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write FLOW: %d\n", ret);
9712f7ca802SSteve Glendinning 
9724436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
973769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write AFC_CFG: %d\n", ret);
9742f7ca802SSteve Glendinning 
9752f7ca802SSteve Glendinning 	/* Don't need mac_cr_lock during initialisation */
9762f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
977769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read MAC_CR: %d\n", ret);
9782f7ca802SSteve Glendinning 
9792f7ca802SSteve Glendinning 	/* Init Rx */
9802f7ca802SSteve Glendinning 	/* Set Vlan */
9814436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
982769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write VLAN1: %d\n", ret);
9832f7ca802SSteve Glendinning 
984f7b29271SSteve Glendinning 	/* Enable or disable checksum offload engines */
985769ea6d8SSteve Glendinning 	ret = smsc95xx_set_features(dev->net, dev->net->features);
986*1e1d7412SJoe Perches 	check_warn_return(ret, "Failed to set checksum offload features\n");
9872f7ca802SSteve Glendinning 
9882f7ca802SSteve Glendinning 	smsc95xx_set_multicast(dev->net);
9892f7ca802SSteve Glendinning 
990769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_initialize(dev);
991*1e1d7412SJoe Perches 	check_warn_return(ret, "Failed to init PHY\n");
9922f7ca802SSteve Glendinning 
9932f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
994769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read INT_EP_CTL: %d\n", ret);
9952f7ca802SSteve Glendinning 
9962f7ca802SSteve Glendinning 	/* enable PHY interrupts */
9972f7ca802SSteve Glendinning 	read_buf |= INT_EP_CTL_PHY_INT_;
9982f7ca802SSteve Glendinning 
9992f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
1000769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write INT_EP_CTL: %d\n", ret);
10012f7ca802SSteve Glendinning 
1002769ea6d8SSteve Glendinning 	ret = smsc95xx_start_tx_path(dev);
1003*1e1d7412SJoe Perches 	check_warn_return(ret, "Failed to start TX path\n");
1004769ea6d8SSteve Glendinning 
1005ec32115dSMing Lei 	ret = smsc95xx_start_rx_path(dev, 0);
1006*1e1d7412SJoe Perches 	check_warn_return(ret, "Failed to start RX path\n");
10072f7ca802SSteve Glendinning 
1008a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
10092f7ca802SSteve Glendinning 	return 0;
10102f7ca802SSteve Glendinning }
10112f7ca802SSteve Glendinning 
101263e77b39SStephen Hemminger static const struct net_device_ops smsc95xx_netdev_ops = {
101363e77b39SStephen Hemminger 	.ndo_open		= usbnet_open,
101463e77b39SStephen Hemminger 	.ndo_stop		= usbnet_stop,
101563e77b39SStephen Hemminger 	.ndo_start_xmit		= usbnet_start_xmit,
101663e77b39SStephen Hemminger 	.ndo_tx_timeout		= usbnet_tx_timeout,
101763e77b39SStephen Hemminger 	.ndo_change_mtu		= usbnet_change_mtu,
101863e77b39SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
101963e77b39SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
102063e77b39SStephen Hemminger 	.ndo_do_ioctl 		= smsc95xx_ioctl,
1021afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= smsc95xx_set_multicast,
102278e47fe4SMichał Mirosław 	.ndo_set_features	= smsc95xx_set_features,
102363e77b39SStephen Hemminger };
102463e77b39SStephen Hemminger 
10252f7ca802SSteve Glendinning static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
10262f7ca802SSteve Glendinning {
10272f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = NULL;
1028bbd9f9eeSSteve Glendinning 	u32 val;
10292f7ca802SSteve Glendinning 	int ret;
10302f7ca802SSteve Glendinning 
10312f7ca802SSteve Glendinning 	printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
10322f7ca802SSteve Glendinning 
10332f7ca802SSteve Glendinning 	ret = usbnet_get_endpoints(dev, intf);
1034769ea6d8SSteve Glendinning 	check_warn_return(ret, "usbnet_get_endpoints failed: %d\n", ret);
10352f7ca802SSteve Glendinning 
10362f7ca802SSteve Glendinning 	dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
10372f7ca802SSteve Glendinning 		GFP_KERNEL);
10382f7ca802SSteve Glendinning 
10392f7ca802SSteve Glendinning 	pdata = (struct smsc95xx_priv *)(dev->data[0]);
10402f7ca802SSteve Glendinning 	if (!pdata) {
104160b86755SJoe Perches 		netdev_warn(dev->net, "Unable to allocate struct smsc95xx_priv\n");
10422f7ca802SSteve Glendinning 		return -ENOMEM;
10432f7ca802SSteve Glendinning 	}
10442f7ca802SSteve Glendinning 
10452f7ca802SSteve Glendinning 	spin_lock_init(&pdata->mac_cr_lock);
10462f7ca802SSteve Glendinning 
104778e47fe4SMichał Mirosław 	if (DEFAULT_TX_CSUM_ENABLE)
104878e47fe4SMichał Mirosław 		dev->net->features |= NETIF_F_HW_CSUM;
104978e47fe4SMichał Mirosław 	if (DEFAULT_RX_CSUM_ENABLE)
105078e47fe4SMichał Mirosław 		dev->net->features |= NETIF_F_RXCSUM;
105178e47fe4SMichał Mirosław 
105278e47fe4SMichał Mirosław 	dev->net->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
10532f7ca802SSteve Glendinning 
1054f4e8ab7cSBernard Blackham 	smsc95xx_init_mac_address(dev);
1055f4e8ab7cSBernard Blackham 
10562f7ca802SSteve Glendinning 	/* Init all registers */
10572f7ca802SSteve Glendinning 	ret = smsc95xx_reset(dev);
10582f7ca802SSteve Glendinning 
1059bbd9f9eeSSteve Glendinning 	/* detect device revision as different features may be available */
1060bbd9f9eeSSteve Glendinning 	ret = smsc95xx_read_reg(dev, ID_REV, &val);
1061bbd9f9eeSSteve Glendinning 	check_warn_return(ret, "Failed to read ID_REV: %d\n", ret);
1062bbd9f9eeSSteve Glendinning 	val >>= 16;
10639ebca507SSteve Glendinning 
10649ebca507SSteve Glendinning 	if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
10659ebca507SSteve Glendinning 	    (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
10669ebca507SSteve Glendinning 		pdata->features = (FEATURE_8_WAKEUP_FILTERS |
10679ebca507SSteve Glendinning 			FEATURE_PHY_NLP_CROSSOVER |
10689ebca507SSteve Glendinning 			FEATURE_AUTOSUSPEND);
10699ebca507SSteve Glendinning 	else if (val == ID_REV_CHIP_ID_9512_)
10709ebca507SSteve Glendinning 		pdata->features = FEATURE_8_WAKEUP_FILTERS;
1071bbd9f9eeSSteve Glendinning 
107263e77b39SStephen Hemminger 	dev->net->netdev_ops = &smsc95xx_netdev_ops;
10732f7ca802SSteve Glendinning 	dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
10742f7ca802SSteve Glendinning 	dev->net->flags |= IFF_MULTICAST;
107578e47fe4SMichał Mirosław 	dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
10769bbf5660SStephane Fillod 	dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
10772f7ca802SSteve Glendinning 	return 0;
10782f7ca802SSteve Glendinning }
10792f7ca802SSteve Glendinning 
10802f7ca802SSteve Glendinning static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
10812f7ca802SSteve Glendinning {
10822f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
10832f7ca802SSteve Glendinning 	if (pdata) {
1084a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net, "free pdata\n");
10852f7ca802SSteve Glendinning 		kfree(pdata);
10862f7ca802SSteve Glendinning 		pdata = NULL;
10872f7ca802SSteve Glendinning 		dev->data[0] = 0;
10882f7ca802SSteve Glendinning 	}
10892f7ca802SSteve Glendinning }
10902f7ca802SSteve Glendinning 
1091bbd9f9eeSSteve Glendinning static u16 smsc_crc(const u8 *buffer, size_t len, int filter)
1092bbd9f9eeSSteve Glendinning {
1093bbd9f9eeSSteve Glendinning 	return bitrev16(crc16(0xFFFF, buffer, len)) << ((filter % 2) * 16);
1094bbd9f9eeSSteve Glendinning }
1095bbd9f9eeSSteve Glendinning 
1096e5e3af83SSteve Glendinning static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1097e5e3af83SSteve Glendinning {
1098e5e3af83SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
1099e5e3af83SSteve Glendinning 	int ret;
1100e5e3af83SSteve Glendinning 
1101*1e1d7412SJoe Perches 	netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1102e5e3af83SSteve Glendinning 
1103e5e3af83SSteve Glendinning 	/* read to clear */
1104e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
1105*1e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PHY_INT_SRC\n");
1106e5e3af83SSteve Glendinning 
1107e5e3af83SSteve Glendinning 	/* enable interrupt source */
1108e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
1109*1e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PHY_INT_MASK\n");
1110e5e3af83SSteve Glendinning 
1111e5e3af83SSteve Glendinning 	ret |= mask;
1112e5e3af83SSteve Glendinning 
1113e5e3af83SSteve Glendinning 	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1114e5e3af83SSteve Glendinning 
1115e5e3af83SSteve Glendinning 	return 0;
1116e5e3af83SSteve Glendinning }
1117e5e3af83SSteve Glendinning 
1118e5e3af83SSteve Glendinning static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1119e5e3af83SSteve Glendinning {
1120e5e3af83SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
1121e5e3af83SSteve Glendinning 	int ret;
1122e5e3af83SSteve Glendinning 
1123e5e3af83SSteve Glendinning 	/* first, a dummy read, needed to latch some MII phys */
1124e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1125*1e1d7412SJoe Perches 	check_warn_return(ret, "Error reading MII_BMSR\n");
1126e5e3af83SSteve Glendinning 
1127e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1128*1e1d7412SJoe Perches 	check_warn_return(ret, "Error reading MII_BMSR\n");
1129e5e3af83SSteve Glendinning 
1130e5e3af83SSteve Glendinning 	return !!(ret & BMSR_LSTATUS);
1131e5e3af83SSteve Glendinning }
1132e5e3af83SSteve Glendinning 
1133319b95b5SSteve Glendinning static int smsc95xx_enter_suspend0(struct usbnet *dev)
1134319b95b5SSteve Glendinning {
1135319b95b5SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1136319b95b5SSteve Glendinning 	u32 val;
1137319b95b5SSteve Glendinning 	int ret;
1138319b95b5SSteve Glendinning 
1139319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1140*1e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PM_CTRL\n");
1141319b95b5SSteve Glendinning 
1142319b95b5SSteve Glendinning 	val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1143319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_0;
1144319b95b5SSteve Glendinning 
1145319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1146*1e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1147319b95b5SSteve Glendinning 
1148319b95b5SSteve Glendinning 	/* clear wol status */
1149319b95b5SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1150319b95b5SSteve Glendinning 	val |= PM_CTL_WUPS_WOL_;
1151319b95b5SSteve Glendinning 
1152319b95b5SSteve Glendinning 	/* enable energy detection */
1153319b95b5SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY)
1154319b95b5SSteve Glendinning 		val |= PM_CTL_WUPS_ED_;
1155319b95b5SSteve Glendinning 
1156319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1157*1e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1158319b95b5SSteve Glendinning 
1159319b95b5SSteve Glendinning 	/* read back PM_CTRL */
1160319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1161*1e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PM_CTRL\n");
1162319b95b5SSteve Glendinning 
1163319b95b5SSteve Glendinning 	smsc95xx_set_feature(dev, USB_DEVICE_REMOTE_WAKEUP);
1164319b95b5SSteve Glendinning 
1165319b95b5SSteve Glendinning 	return 0;
1166319b95b5SSteve Glendinning }
1167319b95b5SSteve Glendinning 
1168319b95b5SSteve Glendinning static int smsc95xx_enter_suspend1(struct usbnet *dev)
1169319b95b5SSteve Glendinning {
1170319b95b5SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1171319b95b5SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
1172319b95b5SSteve Glendinning 	u32 val;
1173319b95b5SSteve Glendinning 	int ret;
1174319b95b5SSteve Glendinning 
1175319b95b5SSteve Glendinning 	/* reconfigure link pulse detection timing for
1176319b95b5SSteve Glendinning 	 * compatibility with non-standard link partners
1177319b95b5SSteve Glendinning 	 */
1178319b95b5SSteve Glendinning 	if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
1179319b95b5SSteve Glendinning 		smsc95xx_mdio_write_nopm(dev->net, mii->phy_id,	PHY_EDPD_CONFIG,
1180319b95b5SSteve Glendinning 			PHY_EDPD_CONFIG_DEFAULT);
1181319b95b5SSteve Glendinning 
1182319b95b5SSteve Glendinning 	/* enable energy detect power-down mode */
1183319b95b5SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS);
1184*1e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PHY_MODE_CTRL_STS\n");
1185319b95b5SSteve Glendinning 
1186319b95b5SSteve Glendinning 	ret |= MODE_CTRL_STS_EDPWRDOWN_;
1187319b95b5SSteve Glendinning 
1188319b95b5SSteve Glendinning 	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS, ret);
1189319b95b5SSteve Glendinning 
1190319b95b5SSteve Glendinning 	/* enter SUSPEND1 mode */
1191319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1192*1e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PM_CTRL\n");
1193319b95b5SSteve Glendinning 
1194319b95b5SSteve Glendinning 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1195319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_1;
1196319b95b5SSteve Glendinning 
1197319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1198*1e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1199319b95b5SSteve Glendinning 
1200319b95b5SSteve Glendinning 	/* clear wol status, enable energy detection */
1201319b95b5SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1202319b95b5SSteve Glendinning 	val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1203319b95b5SSteve Glendinning 
1204319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1205*1e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1206319b95b5SSteve Glendinning 
1207319b95b5SSteve Glendinning 	smsc95xx_set_feature(dev, USB_DEVICE_REMOTE_WAKEUP);
1208319b95b5SSteve Glendinning 
1209319b95b5SSteve Glendinning 	return 0;
1210319b95b5SSteve Glendinning }
1211319b95b5SSteve Glendinning 
1212319b95b5SSteve Glendinning static int smsc95xx_enter_suspend2(struct usbnet *dev)
1213319b95b5SSteve Glendinning {
1214319b95b5SSteve Glendinning 	u32 val;
1215319b95b5SSteve Glendinning 	int ret;
1216319b95b5SSteve Glendinning 
1217319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1218*1e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PM_CTRL\n");
1219319b95b5SSteve Glendinning 
1220319b95b5SSteve Glendinning 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1221319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_2;
1222319b95b5SSteve Glendinning 
1223319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1224*1e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1225319b95b5SSteve Glendinning 
1226319b95b5SSteve Glendinning 	return 0;
1227319b95b5SSteve Glendinning }
1228319b95b5SSteve Glendinning 
1229b5a04475SSteve Glendinning static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1230b5a04475SSteve Glendinning {
1231b5a04475SSteve Glendinning 	struct usbnet *dev = usb_get_intfdata(intf);
1232e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1233e5e3af83SSteve Glendinning 	u32 val, link_up;
1234b5a04475SSteve Glendinning 	int ret;
1235b5a04475SSteve Glendinning 
1236b5a04475SSteve Glendinning 	ret = usbnet_suspend(intf, message);
1237*1e1d7412SJoe Perches 	check_warn_return(ret, "usbnet_suspend error\n");
1238b5a04475SSteve Glendinning 
1239e5e3af83SSteve Glendinning 	/* determine if link is up using only _nopm functions */
1240e5e3af83SSteve Glendinning 	link_up = smsc95xx_link_ok_nopm(dev);
1241e5e3af83SSteve Glendinning 
1242e5e3af83SSteve Glendinning 	/* if no wol options set, or if link is down and we're not waking on
1243e5e3af83SSteve Glendinning 	 * PHY activity, enter lowest power SUSPEND2 mode
1244e5e3af83SSteve Glendinning 	 */
1245e5e3af83SSteve Glendinning 	if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1246e5e3af83SSteve Glendinning 		!(link_up || (pdata->wolopts & WAKE_PHY))) {
1247*1e1d7412SJoe Perches 		netdev_info(dev->net, "entering SUSPEND2 mode\n");
1248b5a04475SSteve Glendinning 
1249e0e474a8SSteve Glendinning 		/* disable energy detect (link up) & wake up events */
1250ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1251*1e1d7412SJoe Perches 		check_warn_return(ret, "Error reading WUCSR\n");
1252e0e474a8SSteve Glendinning 
1253e0e474a8SSteve Glendinning 		val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1254e0e474a8SSteve Glendinning 
1255ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1256*1e1d7412SJoe Perches 		check_warn_return(ret, "Error writing WUCSR\n");
1257e0e474a8SSteve Glendinning 
1258ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1259*1e1d7412SJoe Perches 		check_warn_return(ret, "Error reading PM_CTRL\n");
1260e0e474a8SSteve Glendinning 
1261e0e474a8SSteve Glendinning 		val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1262e0e474a8SSteve Glendinning 
1263ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1264*1e1d7412SJoe Perches 		check_warn_return(ret, "Error writing PM_CTRL\n");
1265e0e474a8SSteve Glendinning 
1266319b95b5SSteve Glendinning 		return smsc95xx_enter_suspend2(dev);
1267b5a04475SSteve Glendinning 	}
1268b5a04475SSteve Glendinning 
1269e5e3af83SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY) {
1270e5e3af83SSteve Glendinning 		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1271e5e3af83SSteve Glendinning 			(PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
1272*1e1d7412SJoe Perches 		check_warn_return(ret, "error enabling PHY wakeup ints\n");
1273e5e3af83SSteve Glendinning 
1274e5e3af83SSteve Glendinning 		/* if link is down then configure EDPD and enter SUSPEND1,
1275e5e3af83SSteve Glendinning 		 * otherwise enter SUSPEND0 below
1276e5e3af83SSteve Glendinning 		 */
1277e5e3af83SSteve Glendinning 		if (!link_up) {
1278*1e1d7412SJoe Perches 			netdev_info(dev->net, "entering SUSPEND1 mode\n");
1279319b95b5SSteve Glendinning 			return smsc95xx_enter_suspend1(dev);
1280e5e3af83SSteve Glendinning 		}
1281e5e3af83SSteve Glendinning 	}
1282e5e3af83SSteve Glendinning 
1283bbd9f9eeSSteve Glendinning 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1284bbd9f9eeSSteve Glendinning 		u32 *filter_mask = kzalloc(32, GFP_KERNEL);
128506a221beSMing Lei 		u32 command[2];
128606a221beSMing Lei 		u32 offset[2];
128706a221beSMing Lei 		u32 crc[4];
12889ebca507SSteve Glendinning 		int wuff_filter_count =
12899ebca507SSteve Glendinning 			(pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
12909ebca507SSteve Glendinning 			LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
1291bbd9f9eeSSteve Glendinning 		int i, filter = 0;
1292bbd9f9eeSSteve Glendinning 
129306a221beSMing Lei 		memset(command, 0, sizeof(command));
129406a221beSMing Lei 		memset(offset, 0, sizeof(offset));
129506a221beSMing Lei 		memset(crc, 0, sizeof(crc));
129606a221beSMing Lei 
1297bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_BCAST) {
1298bbd9f9eeSSteve Glendinning 			const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1299*1e1d7412SJoe Perches 			netdev_info(dev->net, "enabling broadcast detection\n");
1300bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x003F;
1301bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1302bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1303bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1304bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1305bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1306bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(bcast, 6, filter);
1307bbd9f9eeSSteve Glendinning 			filter++;
1308bbd9f9eeSSteve Glendinning 		}
1309bbd9f9eeSSteve Glendinning 
1310bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_MCAST) {
1311bbd9f9eeSSteve Glendinning 			const u8 mcast[] = {0x01, 0x00, 0x5E};
1312*1e1d7412SJoe Perches 			netdev_info(dev->net, "enabling multicast detection\n");
1313bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x0007;
1314bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1315bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1316bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1317bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1318bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00  << ((filter % 4) * 8);
1319bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(mcast, 3, filter);
1320bbd9f9eeSSteve Glendinning 			filter++;
1321bbd9f9eeSSteve Glendinning 		}
1322bbd9f9eeSSteve Glendinning 
1323bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_ARP) {
1324bbd9f9eeSSteve Glendinning 			const u8 arp[] = {0x08, 0x06};
1325*1e1d7412SJoe Perches 			netdev_info(dev->net, "enabling ARP detection\n");
1326bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x0003;
1327bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1328bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1329bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1330bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1331bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1332bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(arp, 2, filter);
1333bbd9f9eeSSteve Glendinning 			filter++;
1334bbd9f9eeSSteve Glendinning 		}
1335bbd9f9eeSSteve Glendinning 
1336bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_UCAST) {
1337*1e1d7412SJoe Perches 			netdev_info(dev->net, "enabling unicast detection\n");
1338bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x003F;
1339bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1340bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1341bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1342bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1343bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1344bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1345bbd9f9eeSSteve Glendinning 			filter++;
1346bbd9f9eeSSteve Glendinning 		}
1347bbd9f9eeSSteve Glendinning 
13489ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count * 4); i++) {
1349ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
135006a221beSMing Lei 			if (ret < 0)
135106a221beSMing Lei 				kfree(filter_mask);
1352*1e1d7412SJoe Perches 			check_warn_return(ret, "Error writing WUFF\n");
1353bbd9f9eeSSteve Glendinning 		}
135406a221beSMing Lei 		kfree(filter_mask);
1355bbd9f9eeSSteve Glendinning 
13569ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1357ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
1358*1e1d7412SJoe Perches 			check_warn_return(ret, "Error writing WUFF\n");
1359bbd9f9eeSSteve Glendinning 		}
1360bbd9f9eeSSteve Glendinning 
13619ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1362ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
1363*1e1d7412SJoe Perches 			check_warn_return(ret, "Error writing WUFF\n");
1364bbd9f9eeSSteve Glendinning 		}
1365bbd9f9eeSSteve Glendinning 
13669ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 2); i++) {
1367ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
1368*1e1d7412SJoe Perches 			check_warn_return(ret, "Error writing WUFF\n");
1369bbd9f9eeSSteve Glendinning 		}
1370bbd9f9eeSSteve Glendinning 
1371bbd9f9eeSSteve Glendinning 		/* clear any pending pattern match packet status */
1372ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1373*1e1d7412SJoe Perches 		check_warn_return(ret, "Error reading WUCSR\n");
1374bbd9f9eeSSteve Glendinning 
1375bbd9f9eeSSteve Glendinning 		val |= WUCSR_WUFR_;
1376bbd9f9eeSSteve Glendinning 
1377ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1378*1e1d7412SJoe Perches 		check_warn_return(ret, "Error writing WUCSR\n");
1379bbd9f9eeSSteve Glendinning 	}
1380bbd9f9eeSSteve Glendinning 
1381e0e474a8SSteve Glendinning 	if (pdata->wolopts & WAKE_MAGIC) {
1382e0e474a8SSteve Glendinning 		/* clear any pending magic packet status */
1383ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1384*1e1d7412SJoe Perches 		check_warn_return(ret, "Error reading WUCSR\n");
1385e0e474a8SSteve Glendinning 
1386e0e474a8SSteve Glendinning 		val |= WUCSR_MPR_;
1387e0e474a8SSteve Glendinning 
1388ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1389*1e1d7412SJoe Perches 		check_warn_return(ret, "Error writing WUCSR\n");
1390e0e474a8SSteve Glendinning 	}
1391e0e474a8SSteve Glendinning 
1392bbd9f9eeSSteve Glendinning 	/* enable/disable wakeup sources */
1393ec32115dSMing Lei 	ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1394*1e1d7412SJoe Perches 	check_warn_return(ret, "Error reading WUCSR\n");
1395e0e474a8SSteve Glendinning 
1396bbd9f9eeSSteve Glendinning 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1397*1e1d7412SJoe Perches 		netdev_info(dev->net, "enabling pattern match wakeup\n");
1398bbd9f9eeSSteve Glendinning 		val |= WUCSR_WAKE_EN_;
1399bbd9f9eeSSteve Glendinning 	} else {
1400*1e1d7412SJoe Perches 		netdev_info(dev->net, "disabling pattern match wakeup\n");
1401bbd9f9eeSSteve Glendinning 		val &= ~WUCSR_WAKE_EN_;
1402bbd9f9eeSSteve Glendinning 	}
1403bbd9f9eeSSteve Glendinning 
1404e0e474a8SSteve Glendinning 	if (pdata->wolopts & WAKE_MAGIC) {
1405*1e1d7412SJoe Perches 		netdev_info(dev->net, "enabling magic packet wakeup\n");
1406e0e474a8SSteve Glendinning 		val |= WUCSR_MPEN_;
1407e0e474a8SSteve Glendinning 	} else {
1408*1e1d7412SJoe Perches 		netdev_info(dev->net, "disabling magic packet wakeup\n");
1409e0e474a8SSteve Glendinning 		val &= ~WUCSR_MPEN_;
1410e0e474a8SSteve Glendinning 	}
1411e0e474a8SSteve Glendinning 
1412ec32115dSMing Lei 	ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1413*1e1d7412SJoe Perches 	check_warn_return(ret, "Error writing WUCSR\n");
1414e0e474a8SSteve Glendinning 
1415e0e474a8SSteve Glendinning 	/* enable wol wakeup source */
1416ec32115dSMing Lei 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1417*1e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PM_CTRL\n");
1418e0e474a8SSteve Glendinning 
1419e0e474a8SSteve Glendinning 	val |= PM_CTL_WOL_EN_;
1420e0e474a8SSteve Glendinning 
1421e5e3af83SSteve Glendinning 	/* phy energy detect wakeup source */
1422e5e3af83SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY)
1423e5e3af83SSteve Glendinning 		val |= PM_CTL_ED_EN_;
1424e5e3af83SSteve Glendinning 
1425ec32115dSMing Lei 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1426*1e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1427e0e474a8SSteve Glendinning 
1428bbd9f9eeSSteve Glendinning 	/* enable receiver to enable frame reception */
1429ec32115dSMing Lei 	smsc95xx_start_rx_path(dev, 1);
1430e0e474a8SSteve Glendinning 
1431e0e474a8SSteve Glendinning 	/* some wol options are enabled, so enter SUSPEND0 */
1432*1e1d7412SJoe Perches 	netdev_info(dev->net, "entering SUSPEND0 mode\n");
1433319b95b5SSteve Glendinning 	return smsc95xx_enter_suspend0(dev);
1434e0e474a8SSteve Glendinning }
1435e0e474a8SSteve Glendinning 
1436e0e474a8SSteve Glendinning static int smsc95xx_resume(struct usb_interface *intf)
1437e0e474a8SSteve Glendinning {
1438e0e474a8SSteve Glendinning 	struct usbnet *dev = usb_get_intfdata(intf);
1439e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1440e0e474a8SSteve Glendinning 	int ret;
1441e0e474a8SSteve Glendinning 	u32 val;
1442e0e474a8SSteve Glendinning 
1443e0e474a8SSteve Glendinning 	BUG_ON(!dev);
1444e0e474a8SSteve Glendinning 
1445bbd9f9eeSSteve Glendinning 	if (pdata->wolopts) {
1446e0e474a8SSteve Glendinning 		smsc95xx_clear_feature(dev, USB_DEVICE_REMOTE_WAKEUP);
1447e0e474a8SSteve Glendinning 
1448bbd9f9eeSSteve Glendinning 		/* clear wake-up sources */
1449ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1450*1e1d7412SJoe Perches 		check_warn_return(ret, "Error reading WUCSR\n");
1451e0e474a8SSteve Glendinning 
1452bbd9f9eeSSteve Glendinning 		val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
1453e0e474a8SSteve Glendinning 
1454ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1455*1e1d7412SJoe Perches 		check_warn_return(ret, "Error writing WUCSR\n");
1456e0e474a8SSteve Glendinning 
1457e0e474a8SSteve Glendinning 		/* clear wake-up status */
1458ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1459*1e1d7412SJoe Perches 		check_warn_return(ret, "Error reading PM_CTRL\n");
1460e0e474a8SSteve Glendinning 
1461e0e474a8SSteve Glendinning 		val &= ~PM_CTL_WOL_EN_;
1462e0e474a8SSteve Glendinning 		val |= PM_CTL_WUPS_;
1463e0e474a8SSteve Glendinning 
1464ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1465*1e1d7412SJoe Perches 		check_warn_return(ret, "Error writing PM_CTRL\n");
1466e0e474a8SSteve Glendinning 	}
1467e0e474a8SSteve Glendinning 
1468af3d7c1eSSteve Glendinning 	ret = usbnet_resume(intf);
1469*1e1d7412SJoe Perches 	check_warn_return(ret, "usbnet_resume error\n");
1470e0e474a8SSteve Glendinning 
1471e0e474a8SSteve Glendinning 	return 0;
1472e0e474a8SSteve Glendinning }
1473e0e474a8SSteve Glendinning 
14742f7ca802SSteve Glendinning static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
14752f7ca802SSteve Glendinning {
14762f7ca802SSteve Glendinning 	skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
14772f7ca802SSteve Glendinning 	skb->ip_summed = CHECKSUM_COMPLETE;
14782f7ca802SSteve Glendinning 	skb_trim(skb, skb->len - 2);
14792f7ca802SSteve Glendinning }
14802f7ca802SSteve Glendinning 
14812f7ca802SSteve Glendinning static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
14822f7ca802SSteve Glendinning {
14832f7ca802SSteve Glendinning 	while (skb->len > 0) {
14842f7ca802SSteve Glendinning 		u32 header, align_count;
14852f7ca802SSteve Glendinning 		struct sk_buff *ax_skb;
14862f7ca802SSteve Glendinning 		unsigned char *packet;
14872f7ca802SSteve Glendinning 		u16 size;
14882f7ca802SSteve Glendinning 
14892f7ca802SSteve Glendinning 		memcpy(&header, skb->data, sizeof(header));
14902f7ca802SSteve Glendinning 		le32_to_cpus(&header);
14912f7ca802SSteve Glendinning 		skb_pull(skb, 4 + NET_IP_ALIGN);
14922f7ca802SSteve Glendinning 		packet = skb->data;
14932f7ca802SSteve Glendinning 
14942f7ca802SSteve Glendinning 		/* get the packet length */
14952f7ca802SSteve Glendinning 		size = (u16)((header & RX_STS_FL_) >> 16);
14962f7ca802SSteve Glendinning 		align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
14972f7ca802SSteve Glendinning 
14982f7ca802SSteve Glendinning 		if (unlikely(header & RX_STS_ES_)) {
1499a475f603SJoe Perches 			netif_dbg(dev, rx_err, dev->net,
1500a475f603SJoe Perches 				  "Error header=0x%08x\n", header);
150180667ac1SHerbert Xu 			dev->net->stats.rx_errors++;
150280667ac1SHerbert Xu 			dev->net->stats.rx_dropped++;
15032f7ca802SSteve Glendinning 
15042f7ca802SSteve Glendinning 			if (header & RX_STS_CRC_) {
150580667ac1SHerbert Xu 				dev->net->stats.rx_crc_errors++;
15062f7ca802SSteve Glendinning 			} else {
15072f7ca802SSteve Glendinning 				if (header & (RX_STS_TL_ | RX_STS_RF_))
150880667ac1SHerbert Xu 					dev->net->stats.rx_frame_errors++;
15092f7ca802SSteve Glendinning 
15102f7ca802SSteve Glendinning 				if ((header & RX_STS_LE_) &&
15112f7ca802SSteve Glendinning 					(!(header & RX_STS_FT_)))
151280667ac1SHerbert Xu 					dev->net->stats.rx_length_errors++;
15132f7ca802SSteve Glendinning 			}
15142f7ca802SSteve Glendinning 		} else {
15152f7ca802SSteve Glendinning 			/* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
15162f7ca802SSteve Glendinning 			if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1517a475f603SJoe Perches 				netif_dbg(dev, rx_err, dev->net,
1518a475f603SJoe Perches 					  "size err header=0x%08x\n", header);
15192f7ca802SSteve Glendinning 				return 0;
15202f7ca802SSteve Glendinning 			}
15212f7ca802SSteve Glendinning 
15222f7ca802SSteve Glendinning 			/* last frame in this batch */
15232f7ca802SSteve Glendinning 			if (skb->len == size) {
152478e47fe4SMichał Mirosław 				if (dev->net->features & NETIF_F_RXCSUM)
15252f7ca802SSteve Glendinning 					smsc95xx_rx_csum_offload(skb);
1526df18accaSPeter Korsgaard 				skb_trim(skb, skb->len - 4); /* remove fcs */
15272f7ca802SSteve Glendinning 				skb->truesize = size + sizeof(struct sk_buff);
15282f7ca802SSteve Glendinning 
15292f7ca802SSteve Glendinning 				return 1;
15302f7ca802SSteve Glendinning 			}
15312f7ca802SSteve Glendinning 
15322f7ca802SSteve Glendinning 			ax_skb = skb_clone(skb, GFP_ATOMIC);
15332f7ca802SSteve Glendinning 			if (unlikely(!ax_skb)) {
153460b86755SJoe Perches 				netdev_warn(dev->net, "Error allocating skb\n");
15352f7ca802SSteve Glendinning 				return 0;
15362f7ca802SSteve Glendinning 			}
15372f7ca802SSteve Glendinning 
15382f7ca802SSteve Glendinning 			ax_skb->len = size;
15392f7ca802SSteve Glendinning 			ax_skb->data = packet;
15402f7ca802SSteve Glendinning 			skb_set_tail_pointer(ax_skb, size);
15412f7ca802SSteve Glendinning 
154278e47fe4SMichał Mirosław 			if (dev->net->features & NETIF_F_RXCSUM)
15432f7ca802SSteve Glendinning 				smsc95xx_rx_csum_offload(ax_skb);
1544df18accaSPeter Korsgaard 			skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
15452f7ca802SSteve Glendinning 			ax_skb->truesize = size + sizeof(struct sk_buff);
15462f7ca802SSteve Glendinning 
15472f7ca802SSteve Glendinning 			usbnet_skb_return(dev, ax_skb);
15482f7ca802SSteve Glendinning 		}
15492f7ca802SSteve Glendinning 
15502f7ca802SSteve Glendinning 		skb_pull(skb, size);
15512f7ca802SSteve Glendinning 
15522f7ca802SSteve Glendinning 		/* padding bytes before the next frame starts */
15532f7ca802SSteve Glendinning 		if (skb->len)
15542f7ca802SSteve Glendinning 			skb_pull(skb, align_count);
15552f7ca802SSteve Glendinning 	}
15562f7ca802SSteve Glendinning 
15572f7ca802SSteve Glendinning 	if (unlikely(skb->len < 0)) {
155860b86755SJoe Perches 		netdev_warn(dev->net, "invalid rx length<0 %d\n", skb->len);
15592f7ca802SSteve Glendinning 		return 0;
15602f7ca802SSteve Glendinning 	}
15612f7ca802SSteve Glendinning 
15622f7ca802SSteve Glendinning 	return 1;
15632f7ca802SSteve Glendinning }
15642f7ca802SSteve Glendinning 
1565f7b29271SSteve Glendinning static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
1566f7b29271SSteve Glendinning {
156755508d60SMichał Mirosław 	u16 low_16 = (u16)skb_checksum_start_offset(skb);
156855508d60SMichał Mirosław 	u16 high_16 = low_16 + skb->csum_offset;
1569f7b29271SSteve Glendinning 	return (high_16 << 16) | low_16;
1570f7b29271SSteve Glendinning }
1571f7b29271SSteve Glendinning 
15722f7ca802SSteve Glendinning static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
15732f7ca802SSteve Glendinning 					 struct sk_buff *skb, gfp_t flags)
15742f7ca802SSteve Glendinning {
157578e47fe4SMichał Mirosław 	bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
1576f7b29271SSteve Glendinning 	int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
15772f7ca802SSteve Glendinning 	u32 tx_cmd_a, tx_cmd_b;
15782f7ca802SSteve Glendinning 
1579f7b29271SSteve Glendinning 	/* We do not advertise SG, so skbs should be already linearized */
1580f7b29271SSteve Glendinning 	BUG_ON(skb_shinfo(skb)->nr_frags);
1581f7b29271SSteve Glendinning 
1582f7b29271SSteve Glendinning 	if (skb_headroom(skb) < overhead) {
15832f7ca802SSteve Glendinning 		struct sk_buff *skb2 = skb_copy_expand(skb,
1584f7b29271SSteve Glendinning 			overhead, 0, flags);
15852f7ca802SSteve Glendinning 		dev_kfree_skb_any(skb);
15862f7ca802SSteve Glendinning 		skb = skb2;
15872f7ca802SSteve Glendinning 		if (!skb)
15882f7ca802SSteve Glendinning 			return NULL;
15892f7ca802SSteve Glendinning 	}
15902f7ca802SSteve Glendinning 
1591f7b29271SSteve Glendinning 	if (csum) {
159211bc3088SSteve Glendinning 		if (skb->len <= 45) {
159311bc3088SSteve Glendinning 			/* workaround - hardware tx checksum does not work
159411bc3088SSteve Glendinning 			 * properly with extremely small packets */
159555508d60SMichał Mirosław 			long csstart = skb_checksum_start_offset(skb);
159611bc3088SSteve Glendinning 			__wsum calc = csum_partial(skb->data + csstart,
159711bc3088SSteve Glendinning 				skb->len - csstart, 0);
159811bc3088SSteve Glendinning 			*((__sum16 *)(skb->data + csstart
159911bc3088SSteve Glendinning 				+ skb->csum_offset)) = csum_fold(calc);
160011bc3088SSteve Glendinning 
160111bc3088SSteve Glendinning 			csum = false;
160211bc3088SSteve Glendinning 		} else {
1603f7b29271SSteve Glendinning 			u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
1604f7b29271SSteve Glendinning 			skb_push(skb, 4);
160500acda68SSteve Glendinning 			cpu_to_le32s(&csum_preamble);
1606f7b29271SSteve Glendinning 			memcpy(skb->data, &csum_preamble, 4);
1607f7b29271SSteve Glendinning 		}
160811bc3088SSteve Glendinning 	}
1609f7b29271SSteve Glendinning 
16102f7ca802SSteve Glendinning 	skb_push(skb, 4);
16112f7ca802SSteve Glendinning 	tx_cmd_b = (u32)(skb->len - 4);
1612f7b29271SSteve Glendinning 	if (csum)
1613f7b29271SSteve Glendinning 		tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
16142f7ca802SSteve Glendinning 	cpu_to_le32s(&tx_cmd_b);
16152f7ca802SSteve Glendinning 	memcpy(skb->data, &tx_cmd_b, 4);
16162f7ca802SSteve Glendinning 
16172f7ca802SSteve Glendinning 	skb_push(skb, 4);
16182f7ca802SSteve Glendinning 	tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ |
16192f7ca802SSteve Glendinning 		TX_CMD_A_LAST_SEG_;
16202f7ca802SSteve Glendinning 	cpu_to_le32s(&tx_cmd_a);
16212f7ca802SSteve Glendinning 	memcpy(skb->data, &tx_cmd_a, 4);
16222f7ca802SSteve Glendinning 
16232f7ca802SSteve Glendinning 	return skb;
16242f7ca802SSteve Glendinning }
16252f7ca802SSteve Glendinning 
16262f7ca802SSteve Glendinning static const struct driver_info smsc95xx_info = {
16272f7ca802SSteve Glendinning 	.description	= "smsc95xx USB 2.0 Ethernet",
16282f7ca802SSteve Glendinning 	.bind		= smsc95xx_bind,
16292f7ca802SSteve Glendinning 	.unbind		= smsc95xx_unbind,
16302f7ca802SSteve Glendinning 	.link_reset	= smsc95xx_link_reset,
16312f7ca802SSteve Glendinning 	.reset		= smsc95xx_reset,
16322f7ca802SSteve Glendinning 	.rx_fixup	= smsc95xx_rx_fixup,
16332f7ca802SSteve Glendinning 	.tx_fixup	= smsc95xx_tx_fixup,
16342f7ca802SSteve Glendinning 	.status		= smsc95xx_status,
163507d69d42SPaolo Pisati 	.flags		= FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
16362f7ca802SSteve Glendinning };
16372f7ca802SSteve Glendinning 
16382f7ca802SSteve Glendinning static const struct usb_device_id products[] = {
16392f7ca802SSteve Glendinning 	{
16402f7ca802SSteve Glendinning 		/* SMSC9500 USB Ethernet Device */
16412f7ca802SSteve Glendinning 		USB_DEVICE(0x0424, 0x9500),
16422f7ca802SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16432f7ca802SSteve Glendinning 	},
1644726474b8SSteve Glendinning 	{
16456f41d12bSSteve Glendinning 		/* SMSC9505 USB Ethernet Device */
16466f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9505),
16476f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16486f41d12bSSteve Glendinning 	},
16496f41d12bSSteve Glendinning 	{
16506f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device */
16516f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9E00),
16526f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16536f41d12bSSteve Glendinning 	},
16546f41d12bSSteve Glendinning 	{
16556f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device */
16566f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9E01),
16576f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16586f41d12bSSteve Glendinning 	},
16596f41d12bSSteve Glendinning 	{
1660726474b8SSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device */
1661726474b8SSteve Glendinning 		USB_DEVICE(0x0424, 0xec00),
1662726474b8SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
1663726474b8SSteve Glendinning 	},
16646f41d12bSSteve Glendinning 	{
16656f41d12bSSteve Glendinning 		/* SMSC9500 USB Ethernet Device (SAL10) */
16666f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9900),
16676f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16686f41d12bSSteve Glendinning 	},
16696f41d12bSSteve Glendinning 	{
16706f41d12bSSteve Glendinning 		/* SMSC9505 USB Ethernet Device (SAL10) */
16716f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9901),
16726f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16736f41d12bSSteve Glendinning 	},
16746f41d12bSSteve Glendinning 	{
16756f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (SAL10) */
16766f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9902),
16776f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16786f41d12bSSteve Glendinning 	},
16796f41d12bSSteve Glendinning 	{
16806f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device (SAL10) */
16816f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9903),
16826f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16836f41d12bSSteve Glendinning 	},
16846f41d12bSSteve Glendinning 	{
16856f41d12bSSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
16866f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9904),
16876f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16886f41d12bSSteve Glendinning 	},
16896f41d12bSSteve Glendinning 	{
16906f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (HAL) */
16916f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9905),
16926f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16936f41d12bSSteve Glendinning 	},
16946f41d12bSSteve Glendinning 	{
16956f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device (HAL) */
16966f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9906),
16976f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16986f41d12bSSteve Glendinning 	},
16996f41d12bSSteve Glendinning 	{
17006f41d12bSSteve Glendinning 		/* SMSC9500 USB Ethernet Device (Alternate ID) */
17016f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9907),
17026f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
17036f41d12bSSteve Glendinning 	},
17046f41d12bSSteve Glendinning 	{
17056f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (Alternate ID) */
17066f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9908),
17076f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
17086f41d12bSSteve Glendinning 	},
17096f41d12bSSteve Glendinning 	{
17106f41d12bSSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
17116f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9909),
17126f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
17136f41d12bSSteve Glendinning 	},
171488edaa41SSteve Glendinning 	{
171588edaa41SSteve Glendinning 		/* SMSC LAN9530 USB Ethernet Device */
171688edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9530),
171788edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
171888edaa41SSteve Glendinning 	},
171988edaa41SSteve Glendinning 	{
172088edaa41SSteve Glendinning 		/* SMSC LAN9730 USB Ethernet Device */
172188edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9730),
172288edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
172388edaa41SSteve Glendinning 	},
172488edaa41SSteve Glendinning 	{
172588edaa41SSteve Glendinning 		/* SMSC LAN89530 USB Ethernet Device */
172688edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9E08),
172788edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
172888edaa41SSteve Glendinning 	},
17292f7ca802SSteve Glendinning 	{ },		/* END */
17302f7ca802SSteve Glendinning };
17312f7ca802SSteve Glendinning MODULE_DEVICE_TABLE(usb, products);
17322f7ca802SSteve Glendinning 
17332f7ca802SSteve Glendinning static struct usb_driver smsc95xx_driver = {
17342f7ca802SSteve Glendinning 	.name		= "smsc95xx",
17352f7ca802SSteve Glendinning 	.id_table	= products,
17362f7ca802SSteve Glendinning 	.probe		= usbnet_probe,
1737b5a04475SSteve Glendinning 	.suspend	= smsc95xx_suspend,
1738e0e474a8SSteve Glendinning 	.resume		= smsc95xx_resume,
1739e0e474a8SSteve Glendinning 	.reset_resume	= smsc95xx_resume,
17402f7ca802SSteve Glendinning 	.disconnect	= usbnet_disconnect,
1741e1f12eb6SSarah Sharp 	.disable_hub_initiated_lpm = 1,
17422f7ca802SSteve Glendinning };
17432f7ca802SSteve Glendinning 
1744d632eb1bSGreg Kroah-Hartman module_usb_driver(smsc95xx_driver);
17452f7ca802SSteve Glendinning 
17462f7ca802SSteve Glendinning MODULE_AUTHOR("Nancy Lin");
174790b24cfbSSteve Glendinning MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
17482f7ca802SSteve Glendinning MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
17492f7ca802SSteve Glendinning MODULE_LICENSE("GPL");
1750