xref: /linux/drivers/net/usb/smsc95xx.c (revision 0883c2c06fb5bcf5b9e008270827e63c09a88c1e)
1  /***************************************************************************
2  *
3  * Copyright (C) 2007-2008 SMSC
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  *
18  *****************************************************************************/
19 
20 #include <linux/module.h>
21 #include <linux/kmod.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/ethtool.h>
25 #include <linux/mii.h>
26 #include <linux/usb.h>
27 #include <linux/bitrev.h>
28 #include <linux/crc16.h>
29 #include <linux/crc32.h>
30 #include <linux/usb/usbnet.h>
31 #include <linux/slab.h>
32 #include <linux/of_net.h>
33 #include "smsc95xx.h"
34 
35 #define SMSC_CHIPNAME			"smsc95xx"
36 #define SMSC_DRIVER_VERSION		"1.0.4"
37 #define HS_USB_PKT_SIZE			(512)
38 #define FS_USB_PKT_SIZE			(64)
39 #define DEFAULT_HS_BURST_CAP_SIZE	(16 * 1024 + 5 * HS_USB_PKT_SIZE)
40 #define DEFAULT_FS_BURST_CAP_SIZE	(6 * 1024 + 33 * FS_USB_PKT_SIZE)
41 #define DEFAULT_BULK_IN_DELAY		(0x00002000)
42 #define MAX_SINGLE_PACKET_SIZE		(2048)
43 #define LAN95XX_EEPROM_MAGIC		(0x9500)
44 #define EEPROM_MAC_OFFSET		(0x01)
45 #define DEFAULT_TX_CSUM_ENABLE		(true)
46 #define DEFAULT_RX_CSUM_ENABLE		(true)
47 #define SMSC95XX_INTERNAL_PHY_ID	(1)
48 #define SMSC95XX_TX_OVERHEAD		(8)
49 #define SMSC95XX_TX_OVERHEAD_CSUM	(12)
50 #define SUPPORTED_WAKE			(WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
51 					 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
52 
53 #define FEATURE_8_WAKEUP_FILTERS	(0x01)
54 #define FEATURE_PHY_NLP_CROSSOVER	(0x02)
55 #define FEATURE_REMOTE_WAKEUP		(0x04)
56 
57 #define SUSPEND_SUSPEND0		(0x01)
58 #define SUSPEND_SUSPEND1		(0x02)
59 #define SUSPEND_SUSPEND2		(0x04)
60 #define SUSPEND_SUSPEND3		(0x08)
61 #define SUSPEND_ALLMODES		(SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
62 					 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
63 
64 #define CARRIER_CHECK_DELAY (2 * HZ)
65 
66 struct smsc95xx_priv {
67 	u32 mac_cr;
68 	u32 hash_hi;
69 	u32 hash_lo;
70 	u32 wolopts;
71 	spinlock_t mac_cr_lock;
72 	u8 features;
73 	u8 suspend_flags;
74 	bool link_ok;
75 	struct delayed_work carrier_check;
76 	struct usbnet *dev;
77 };
78 
79 static bool turbo_mode = true;
80 module_param(turbo_mode, bool, 0644);
81 MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
82 
83 static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
84 					    u32 *data, int in_pm)
85 {
86 	u32 buf;
87 	int ret;
88 	int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
89 
90 	BUG_ON(!dev);
91 
92 	if (!in_pm)
93 		fn = usbnet_read_cmd;
94 	else
95 		fn = usbnet_read_cmd_nopm;
96 
97 	ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
98 		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
99 		 0, index, &buf, 4);
100 	if (unlikely(ret < 0)) {
101 		netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
102 			    index, ret);
103 		return ret;
104 	}
105 
106 	le32_to_cpus(&buf);
107 	*data = buf;
108 
109 	return ret;
110 }
111 
112 static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index,
113 					     u32 data, int in_pm)
114 {
115 	u32 buf;
116 	int ret;
117 	int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
118 
119 	BUG_ON(!dev);
120 
121 	if (!in_pm)
122 		fn = usbnet_write_cmd;
123 	else
124 		fn = usbnet_write_cmd_nopm;
125 
126 	buf = data;
127 	cpu_to_le32s(&buf);
128 
129 	ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
130 		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
131 		 0, index, &buf, 4);
132 	if (unlikely(ret < 0))
133 		netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
134 			    index, ret);
135 
136 	return ret;
137 }
138 
139 static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index,
140 					       u32 *data)
141 {
142 	return __smsc95xx_read_reg(dev, index, data, 1);
143 }
144 
145 static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index,
146 						u32 data)
147 {
148 	return __smsc95xx_write_reg(dev, index, data, 1);
149 }
150 
151 static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
152 					  u32 *data)
153 {
154 	return __smsc95xx_read_reg(dev, index, data, 0);
155 }
156 
157 static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
158 					   u32 data)
159 {
160 	return __smsc95xx_write_reg(dev, index, data, 0);
161 }
162 
163 /* Loop until the read is completed with timeout
164  * called with phy_mutex held */
165 static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
166 						     int in_pm)
167 {
168 	unsigned long start_time = jiffies;
169 	u32 val;
170 	int ret;
171 
172 	do {
173 		ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
174 		if (ret < 0) {
175 			netdev_warn(dev->net, "Error reading MII_ACCESS\n");
176 			return ret;
177 		}
178 
179 		if (!(val & MII_BUSY_))
180 			return 0;
181 	} while (!time_after(jiffies, start_time + HZ));
182 
183 	return -EIO;
184 }
185 
186 static int __smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
187 				int in_pm)
188 {
189 	struct usbnet *dev = netdev_priv(netdev);
190 	u32 val, addr;
191 	int ret;
192 
193 	mutex_lock(&dev->phy_mutex);
194 
195 	/* confirm MII not busy */
196 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
197 	if (ret < 0) {
198 		netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_read\n");
199 		goto done;
200 	}
201 
202 	/* set the address, index & direction (read from PHY) */
203 	phy_id &= dev->mii.phy_id_mask;
204 	idx &= dev->mii.reg_num_mask;
205 	addr = (phy_id << 11) | (idx << 6) | MII_READ_ | MII_BUSY_;
206 	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
207 	if (ret < 0) {
208 		netdev_warn(dev->net, "Error writing MII_ADDR\n");
209 		goto done;
210 	}
211 
212 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
213 	if (ret < 0) {
214 		netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
215 		goto done;
216 	}
217 
218 	ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
219 	if (ret < 0) {
220 		netdev_warn(dev->net, "Error reading MII_DATA\n");
221 		goto done;
222 	}
223 
224 	ret = (u16)(val & 0xFFFF);
225 
226 done:
227 	mutex_unlock(&dev->phy_mutex);
228 	return ret;
229 }
230 
231 static void __smsc95xx_mdio_write(struct net_device *netdev, int phy_id,
232 				  int idx, int regval, int in_pm)
233 {
234 	struct usbnet *dev = netdev_priv(netdev);
235 	u32 val, addr;
236 	int ret;
237 
238 	mutex_lock(&dev->phy_mutex);
239 
240 	/* confirm MII not busy */
241 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
242 	if (ret < 0) {
243 		netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_write\n");
244 		goto done;
245 	}
246 
247 	val = regval;
248 	ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
249 	if (ret < 0) {
250 		netdev_warn(dev->net, "Error writing MII_DATA\n");
251 		goto done;
252 	}
253 
254 	/* set the address, index & direction (write to PHY) */
255 	phy_id &= dev->mii.phy_id_mask;
256 	idx &= dev->mii.reg_num_mask;
257 	addr = (phy_id << 11) | (idx << 6) | MII_WRITE_ | MII_BUSY_;
258 	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
259 	if (ret < 0) {
260 		netdev_warn(dev->net, "Error writing MII_ADDR\n");
261 		goto done;
262 	}
263 
264 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
265 	if (ret < 0) {
266 		netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
267 		goto done;
268 	}
269 
270 done:
271 	mutex_unlock(&dev->phy_mutex);
272 }
273 
274 static int smsc95xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
275 				   int idx)
276 {
277 	return __smsc95xx_mdio_read(netdev, phy_id, idx, 1);
278 }
279 
280 static void smsc95xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
281 				     int idx, int regval)
282 {
283 	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 1);
284 }
285 
286 static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
287 {
288 	return __smsc95xx_mdio_read(netdev, phy_id, idx, 0);
289 }
290 
291 static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
292 				int regval)
293 {
294 	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 0);
295 }
296 
297 static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
298 {
299 	unsigned long start_time = jiffies;
300 	u32 val;
301 	int ret;
302 
303 	do {
304 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
305 		if (ret < 0) {
306 			netdev_warn(dev->net, "Error reading E2P_CMD\n");
307 			return ret;
308 		}
309 
310 		if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
311 			break;
312 		udelay(40);
313 	} while (!time_after(jiffies, start_time + HZ));
314 
315 	if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
316 		netdev_warn(dev->net, "EEPROM read operation timeout\n");
317 		return -EIO;
318 	}
319 
320 	return 0;
321 }
322 
323 static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
324 {
325 	unsigned long start_time = jiffies;
326 	u32 val;
327 	int ret;
328 
329 	do {
330 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
331 		if (ret < 0) {
332 			netdev_warn(dev->net, "Error reading E2P_CMD\n");
333 			return ret;
334 		}
335 
336 		if (!(val & E2P_CMD_BUSY_))
337 			return 0;
338 
339 		udelay(40);
340 	} while (!time_after(jiffies, start_time + HZ));
341 
342 	netdev_warn(dev->net, "EEPROM is busy\n");
343 	return -EIO;
344 }
345 
346 static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
347 				u8 *data)
348 {
349 	u32 val;
350 	int i, ret;
351 
352 	BUG_ON(!dev);
353 	BUG_ON(!data);
354 
355 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
356 	if (ret)
357 		return ret;
358 
359 	for (i = 0; i < length; i++) {
360 		val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
361 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
362 		if (ret < 0) {
363 			netdev_warn(dev->net, "Error writing E2P_CMD\n");
364 			return ret;
365 		}
366 
367 		ret = smsc95xx_wait_eeprom(dev);
368 		if (ret < 0)
369 			return ret;
370 
371 		ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
372 		if (ret < 0) {
373 			netdev_warn(dev->net, "Error reading E2P_DATA\n");
374 			return ret;
375 		}
376 
377 		data[i] = val & 0xFF;
378 		offset++;
379 	}
380 
381 	return 0;
382 }
383 
384 static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
385 				 u8 *data)
386 {
387 	u32 val;
388 	int i, ret;
389 
390 	BUG_ON(!dev);
391 	BUG_ON(!data);
392 
393 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
394 	if (ret)
395 		return ret;
396 
397 	/* Issue write/erase enable command */
398 	val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
399 	ret = smsc95xx_write_reg(dev, E2P_CMD, val);
400 	if (ret < 0) {
401 		netdev_warn(dev->net, "Error writing E2P_DATA\n");
402 		return ret;
403 	}
404 
405 	ret = smsc95xx_wait_eeprom(dev);
406 	if (ret < 0)
407 		return ret;
408 
409 	for (i = 0; i < length; i++) {
410 
411 		/* Fill data register */
412 		val = data[i];
413 		ret = smsc95xx_write_reg(dev, E2P_DATA, val);
414 		if (ret < 0) {
415 			netdev_warn(dev->net, "Error writing E2P_DATA\n");
416 			return ret;
417 		}
418 
419 		/* Send "write" command */
420 		val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
421 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
422 		if (ret < 0) {
423 			netdev_warn(dev->net, "Error writing E2P_CMD\n");
424 			return ret;
425 		}
426 
427 		ret = smsc95xx_wait_eeprom(dev);
428 		if (ret < 0)
429 			return ret;
430 
431 		offset++;
432 	}
433 
434 	return 0;
435 }
436 
437 static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
438 						 u32 data)
439 {
440 	const u16 size = 4;
441 	u32 buf;
442 	int ret;
443 
444 	buf = data;
445 	cpu_to_le32s(&buf);
446 
447 	ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
448 				     USB_DIR_OUT | USB_TYPE_VENDOR |
449 				     USB_RECIP_DEVICE,
450 				     0, index, &buf, size);
451 	if (ret < 0)
452 		netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
453 			    ret);
454 	return ret;
455 }
456 
457 /* returns hash bit number for given MAC address
458  * example:
459  * 01 00 5E 00 00 01 -> returns bit number 31 */
460 static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
461 {
462 	return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
463 }
464 
465 static void smsc95xx_set_multicast(struct net_device *netdev)
466 {
467 	struct usbnet *dev = netdev_priv(netdev);
468 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
469 	unsigned long flags;
470 	int ret;
471 
472 	pdata->hash_hi = 0;
473 	pdata->hash_lo = 0;
474 
475 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
476 
477 	if (dev->net->flags & IFF_PROMISC) {
478 		netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
479 		pdata->mac_cr |= MAC_CR_PRMS_;
480 		pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
481 	} else if (dev->net->flags & IFF_ALLMULTI) {
482 		netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
483 		pdata->mac_cr |= MAC_CR_MCPAS_;
484 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
485 	} else if (!netdev_mc_empty(dev->net)) {
486 		struct netdev_hw_addr *ha;
487 
488 		pdata->mac_cr |= MAC_CR_HPFILT_;
489 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
490 
491 		netdev_for_each_mc_addr(ha, netdev) {
492 			u32 bitnum = smsc95xx_hash(ha->addr);
493 			u32 mask = 0x01 << (bitnum & 0x1F);
494 			if (bitnum & 0x20)
495 				pdata->hash_hi |= mask;
496 			else
497 				pdata->hash_lo |= mask;
498 		}
499 
500 		netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
501 				   pdata->hash_hi, pdata->hash_lo);
502 	} else {
503 		netif_dbg(dev, drv, dev->net, "receive own packets only\n");
504 		pdata->mac_cr &=
505 			~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
506 	}
507 
508 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
509 
510 	/* Initiate async writes, as we can't wait for completion here */
511 	ret = smsc95xx_write_reg_async(dev, HASHH, pdata->hash_hi);
512 	if (ret < 0)
513 		netdev_warn(dev->net, "failed to initiate async write to HASHH\n");
514 
515 	ret = smsc95xx_write_reg_async(dev, HASHL, pdata->hash_lo);
516 	if (ret < 0)
517 		netdev_warn(dev->net, "failed to initiate async write to HASHL\n");
518 
519 	ret = smsc95xx_write_reg_async(dev, MAC_CR, pdata->mac_cr);
520 	if (ret < 0)
521 		netdev_warn(dev->net, "failed to initiate async write to MAC_CR\n");
522 }
523 
524 static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
525 					   u16 lcladv, u16 rmtadv)
526 {
527 	u32 flow, afc_cfg = 0;
528 
529 	int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
530 	if (ret < 0)
531 		return ret;
532 
533 	if (duplex == DUPLEX_FULL) {
534 		u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
535 
536 		if (cap & FLOW_CTRL_RX)
537 			flow = 0xFFFF0002;
538 		else
539 			flow = 0;
540 
541 		if (cap & FLOW_CTRL_TX)
542 			afc_cfg |= 0xF;
543 		else
544 			afc_cfg &= ~0xF;
545 
546 		netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
547 				   cap & FLOW_CTRL_RX ? "enabled" : "disabled",
548 				   cap & FLOW_CTRL_TX ? "enabled" : "disabled");
549 	} else {
550 		netif_dbg(dev, link, dev->net, "half duplex\n");
551 		flow = 0;
552 		afc_cfg |= 0xF;
553 	}
554 
555 	ret = smsc95xx_write_reg(dev, FLOW, flow);
556 	if (ret < 0)
557 		return ret;
558 
559 	return smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
560 }
561 
562 static int smsc95xx_link_reset(struct usbnet *dev)
563 {
564 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
565 	struct mii_if_info *mii = &dev->mii;
566 	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
567 	unsigned long flags;
568 	u16 lcladv, rmtadv;
569 	int ret;
570 
571 	/* clear interrupt status */
572 	ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
573 	if (ret < 0)
574 		return ret;
575 
576 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
577 	if (ret < 0)
578 		return ret;
579 
580 	mii_check_media(mii, 1, 1);
581 	mii_ethtool_gset(&dev->mii, &ecmd);
582 	lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
583 	rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
584 
585 	netif_dbg(dev, link, dev->net,
586 		  "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
587 		  ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
588 
589 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
590 	if (ecmd.duplex != DUPLEX_FULL) {
591 		pdata->mac_cr &= ~MAC_CR_FDPX_;
592 		pdata->mac_cr |= MAC_CR_RCVOWN_;
593 	} else {
594 		pdata->mac_cr &= ~MAC_CR_RCVOWN_;
595 		pdata->mac_cr |= MAC_CR_FDPX_;
596 	}
597 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
598 
599 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
600 	if (ret < 0)
601 		return ret;
602 
603 	ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
604 	if (ret < 0)
605 		netdev_warn(dev->net, "Error updating PHY flow control\n");
606 
607 	return ret;
608 }
609 
610 static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
611 {
612 	u32 intdata;
613 
614 	if (urb->actual_length != 4) {
615 		netdev_warn(dev->net, "unexpected urb length %d\n",
616 			    urb->actual_length);
617 		return;
618 	}
619 
620 	memcpy(&intdata, urb->transfer_buffer, 4);
621 	le32_to_cpus(&intdata);
622 
623 	netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
624 
625 	if (intdata & INT_ENP_PHY_INT_)
626 		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
627 	else
628 		netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
629 			    intdata);
630 }
631 
632 static void set_carrier(struct usbnet *dev, bool link)
633 {
634 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
635 
636 	if (pdata->link_ok == link)
637 		return;
638 
639 	pdata->link_ok = link;
640 
641 	if (link)
642 		usbnet_link_change(dev, 1, 0);
643 	else
644 		usbnet_link_change(dev, 0, 0);
645 }
646 
647 static void check_carrier(struct work_struct *work)
648 {
649 	struct smsc95xx_priv *pdata = container_of(work, struct smsc95xx_priv,
650 						carrier_check.work);
651 	struct usbnet *dev = pdata->dev;
652 	int ret;
653 
654 	if (pdata->suspend_flags != 0)
655 		return;
656 
657 	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMSR);
658 	if (ret < 0) {
659 		netdev_warn(dev->net, "Failed to read MII_BMSR\n");
660 		return;
661 	}
662 	if (ret & BMSR_LSTATUS)
663 		set_carrier(dev, 1);
664 	else
665 		set_carrier(dev, 0);
666 
667 	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
668 }
669 
670 /* Enable or disable Tx & Rx checksum offload engines */
671 static int smsc95xx_set_features(struct net_device *netdev,
672 	netdev_features_t features)
673 {
674 	struct usbnet *dev = netdev_priv(netdev);
675 	u32 read_buf;
676 	int ret;
677 
678 	ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
679 	if (ret < 0)
680 		return ret;
681 
682 	if (features & NETIF_F_HW_CSUM)
683 		read_buf |= Tx_COE_EN_;
684 	else
685 		read_buf &= ~Tx_COE_EN_;
686 
687 	if (features & NETIF_F_RXCSUM)
688 		read_buf |= Rx_COE_EN_;
689 	else
690 		read_buf &= ~Rx_COE_EN_;
691 
692 	ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
693 	if (ret < 0)
694 		return ret;
695 
696 	netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
697 	return 0;
698 }
699 
700 static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
701 {
702 	return MAX_EEPROM_SIZE;
703 }
704 
705 static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
706 				       struct ethtool_eeprom *ee, u8 *data)
707 {
708 	struct usbnet *dev = netdev_priv(netdev);
709 
710 	ee->magic = LAN95XX_EEPROM_MAGIC;
711 
712 	return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
713 }
714 
715 static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
716 				       struct ethtool_eeprom *ee, u8 *data)
717 {
718 	struct usbnet *dev = netdev_priv(netdev);
719 
720 	if (ee->magic != LAN95XX_EEPROM_MAGIC) {
721 		netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
722 			    ee->magic);
723 		return -EINVAL;
724 	}
725 
726 	return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
727 }
728 
729 static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
730 {
731 	/* all smsc95xx registers */
732 	return COE_CR - ID_REV + sizeof(u32);
733 }
734 
735 static void
736 smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
737 			 void *buf)
738 {
739 	struct usbnet *dev = netdev_priv(netdev);
740 	unsigned int i, j;
741 	int retval;
742 	u32 *data = buf;
743 
744 	retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
745 	if (retval < 0) {
746 		netdev_warn(netdev, "REGS: cannot read ID_REV\n");
747 		return;
748 	}
749 
750 	for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
751 		retval = smsc95xx_read_reg(dev, i, &data[j]);
752 		if (retval < 0) {
753 			netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
754 			return;
755 		}
756 	}
757 }
758 
759 static void smsc95xx_ethtool_get_wol(struct net_device *net,
760 				     struct ethtool_wolinfo *wolinfo)
761 {
762 	struct usbnet *dev = netdev_priv(net);
763 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
764 
765 	wolinfo->supported = SUPPORTED_WAKE;
766 	wolinfo->wolopts = pdata->wolopts;
767 }
768 
769 static int smsc95xx_ethtool_set_wol(struct net_device *net,
770 				    struct ethtool_wolinfo *wolinfo)
771 {
772 	struct usbnet *dev = netdev_priv(net);
773 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
774 	int ret;
775 
776 	pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
777 
778 	ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
779 	if (ret < 0)
780 		netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
781 
782 	return ret;
783 }
784 
785 static const struct ethtool_ops smsc95xx_ethtool_ops = {
786 	.get_link	= usbnet_get_link,
787 	.nway_reset	= usbnet_nway_reset,
788 	.get_drvinfo	= usbnet_get_drvinfo,
789 	.get_msglevel	= usbnet_get_msglevel,
790 	.set_msglevel	= usbnet_set_msglevel,
791 	.get_settings	= usbnet_get_settings,
792 	.set_settings	= usbnet_set_settings,
793 	.get_eeprom_len	= smsc95xx_ethtool_get_eeprom_len,
794 	.get_eeprom	= smsc95xx_ethtool_get_eeprom,
795 	.set_eeprom	= smsc95xx_ethtool_set_eeprom,
796 	.get_regs_len	= smsc95xx_ethtool_getregslen,
797 	.get_regs	= smsc95xx_ethtool_getregs,
798 	.get_wol	= smsc95xx_ethtool_get_wol,
799 	.set_wol	= smsc95xx_ethtool_set_wol,
800 };
801 
802 static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
803 {
804 	struct usbnet *dev = netdev_priv(netdev);
805 
806 	if (!netif_running(netdev))
807 		return -EINVAL;
808 
809 	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
810 }
811 
812 static void smsc95xx_init_mac_address(struct usbnet *dev)
813 {
814 	const u8 *mac_addr;
815 
816 	/* maybe the boot loader passed the MAC address in devicetree */
817 	mac_addr = of_get_mac_address(dev->udev->dev.of_node);
818 	if (mac_addr) {
819 		memcpy(dev->net->dev_addr, mac_addr, ETH_ALEN);
820 		return;
821 	}
822 
823 	/* try reading mac address from EEPROM */
824 	if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
825 			dev->net->dev_addr) == 0) {
826 		if (is_valid_ether_addr(dev->net->dev_addr)) {
827 			/* eeprom values are valid so use them */
828 			netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
829 			return;
830 		}
831 	}
832 
833 	/* no useful static MAC address found. generate a random one */
834 	eth_hw_addr_random(dev->net);
835 	netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
836 }
837 
838 static int smsc95xx_set_mac_address(struct usbnet *dev)
839 {
840 	u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
841 		dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
842 	u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
843 	int ret;
844 
845 	ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
846 	if (ret < 0)
847 		return ret;
848 
849 	return smsc95xx_write_reg(dev, ADDRH, addr_hi);
850 }
851 
852 /* starts the TX path */
853 static int smsc95xx_start_tx_path(struct usbnet *dev)
854 {
855 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
856 	unsigned long flags;
857 	int ret;
858 
859 	/* Enable Tx at MAC */
860 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
861 	pdata->mac_cr |= MAC_CR_TXEN_;
862 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
863 
864 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
865 	if (ret < 0)
866 		return ret;
867 
868 	/* Enable Tx at SCSRs */
869 	return smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
870 }
871 
872 /* Starts the Receive path */
873 static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
874 {
875 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
876 	unsigned long flags;
877 
878 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
879 	pdata->mac_cr |= MAC_CR_RXEN_;
880 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
881 
882 	return __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
883 }
884 
885 static int smsc95xx_phy_initialize(struct usbnet *dev)
886 {
887 	int bmcr, ret, timeout = 0;
888 
889 	/* Initialize MII structure */
890 	dev->mii.dev = dev->net;
891 	dev->mii.mdio_read = smsc95xx_mdio_read;
892 	dev->mii.mdio_write = smsc95xx_mdio_write;
893 	dev->mii.phy_id_mask = 0x1f;
894 	dev->mii.reg_num_mask = 0x1f;
895 	dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
896 
897 	/* reset phy and wait for reset to complete */
898 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
899 
900 	do {
901 		msleep(10);
902 		bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
903 		timeout++;
904 	} while ((bmcr & BMCR_RESET) && (timeout < 100));
905 
906 	if (timeout >= 100) {
907 		netdev_warn(dev->net, "timeout on PHY Reset");
908 		return -EIO;
909 	}
910 
911 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
912 		ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
913 		ADVERTISE_PAUSE_ASYM);
914 
915 	/* read to clear */
916 	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
917 	if (ret < 0) {
918 		netdev_warn(dev->net, "Failed to read PHY_INT_SRC during init\n");
919 		return ret;
920 	}
921 
922 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
923 		PHY_INT_MASK_DEFAULT_);
924 	mii_nway_restart(&dev->mii);
925 
926 	netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
927 	return 0;
928 }
929 
930 static int smsc95xx_reset(struct usbnet *dev)
931 {
932 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
933 	u32 read_buf, write_buf, burst_cap;
934 	int ret = 0, timeout;
935 
936 	netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
937 
938 	ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
939 	if (ret < 0)
940 		return ret;
941 
942 	timeout = 0;
943 	do {
944 		msleep(10);
945 		ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
946 		if (ret < 0)
947 			return ret;
948 		timeout++;
949 	} while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
950 
951 	if (timeout >= 100) {
952 		netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
953 		return ret;
954 	}
955 
956 	ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
957 	if (ret < 0)
958 		return ret;
959 
960 	timeout = 0;
961 	do {
962 		msleep(10);
963 		ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
964 		if (ret < 0)
965 			return ret;
966 		timeout++;
967 	} while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
968 
969 	if (timeout >= 100) {
970 		netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
971 		return ret;
972 	}
973 
974 	ret = smsc95xx_set_mac_address(dev);
975 	if (ret < 0)
976 		return ret;
977 
978 	netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
979 		  dev->net->dev_addr);
980 
981 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
982 	if (ret < 0)
983 		return ret;
984 
985 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
986 		  read_buf);
987 
988 	read_buf |= HW_CFG_BIR_;
989 
990 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
991 	if (ret < 0)
992 		return ret;
993 
994 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
995 	if (ret < 0)
996 		return ret;
997 
998 	netif_dbg(dev, ifup, dev->net,
999 		  "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
1000 		  read_buf);
1001 
1002 	if (!turbo_mode) {
1003 		burst_cap = 0;
1004 		dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
1005 	} else if (dev->udev->speed == USB_SPEED_HIGH) {
1006 		burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
1007 		dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
1008 	} else {
1009 		burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
1010 		dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
1011 	}
1012 
1013 	netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
1014 		  (ulong)dev->rx_urb_size);
1015 
1016 	ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
1017 	if (ret < 0)
1018 		return ret;
1019 
1020 	ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
1021 	if (ret < 0)
1022 		return ret;
1023 
1024 	netif_dbg(dev, ifup, dev->net,
1025 		  "Read Value from BURST_CAP after writing: 0x%08x\n",
1026 		  read_buf);
1027 
1028 	ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
1029 	if (ret < 0)
1030 		return ret;
1031 
1032 	ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
1033 	if (ret < 0)
1034 		return ret;
1035 
1036 	netif_dbg(dev, ifup, dev->net,
1037 		  "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
1038 		  read_buf);
1039 
1040 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1041 	if (ret < 0)
1042 		return ret;
1043 
1044 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
1045 		  read_buf);
1046 
1047 	if (turbo_mode)
1048 		read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
1049 
1050 	read_buf &= ~HW_CFG_RXDOFF_;
1051 
1052 	/* set Rx data offset=2, Make IP header aligns on word boundary. */
1053 	read_buf |= NET_IP_ALIGN << 9;
1054 
1055 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
1056 	if (ret < 0)
1057 		return ret;
1058 
1059 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1060 	if (ret < 0)
1061 		return ret;
1062 
1063 	netif_dbg(dev, ifup, dev->net,
1064 		  "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
1065 
1066 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
1067 	if (ret < 0)
1068 		return ret;
1069 
1070 	ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
1071 	if (ret < 0)
1072 		return ret;
1073 	netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
1074 
1075 	/* Configure GPIO pins as LED outputs */
1076 	write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
1077 		LED_GPIO_CFG_FDX_LED;
1078 	ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
1079 	if (ret < 0)
1080 		return ret;
1081 
1082 	/* Init Tx */
1083 	ret = smsc95xx_write_reg(dev, FLOW, 0);
1084 	if (ret < 0)
1085 		return ret;
1086 
1087 	ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
1088 	if (ret < 0)
1089 		return ret;
1090 
1091 	/* Don't need mac_cr_lock during initialisation */
1092 	ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
1093 	if (ret < 0)
1094 		return ret;
1095 
1096 	/* Init Rx */
1097 	/* Set Vlan */
1098 	ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
1099 	if (ret < 0)
1100 		return ret;
1101 
1102 	/* Enable or disable checksum offload engines */
1103 	ret = smsc95xx_set_features(dev->net, dev->net->features);
1104 	if (ret < 0) {
1105 		netdev_warn(dev->net, "Failed to set checksum offload features\n");
1106 		return ret;
1107 	}
1108 
1109 	smsc95xx_set_multicast(dev->net);
1110 
1111 	ret = smsc95xx_phy_initialize(dev);
1112 	if (ret < 0) {
1113 		netdev_warn(dev->net, "Failed to init PHY\n");
1114 		return ret;
1115 	}
1116 
1117 	ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
1118 	if (ret < 0)
1119 		return ret;
1120 
1121 	/* enable PHY interrupts */
1122 	read_buf |= INT_EP_CTL_PHY_INT_;
1123 
1124 	ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
1125 	if (ret < 0)
1126 		return ret;
1127 
1128 	ret = smsc95xx_start_tx_path(dev);
1129 	if (ret < 0) {
1130 		netdev_warn(dev->net, "Failed to start TX path\n");
1131 		return ret;
1132 	}
1133 
1134 	ret = smsc95xx_start_rx_path(dev, 0);
1135 	if (ret < 0) {
1136 		netdev_warn(dev->net, "Failed to start RX path\n");
1137 		return ret;
1138 	}
1139 
1140 	netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
1141 	return 0;
1142 }
1143 
1144 static const struct net_device_ops smsc95xx_netdev_ops = {
1145 	.ndo_open		= usbnet_open,
1146 	.ndo_stop		= usbnet_stop,
1147 	.ndo_start_xmit		= usbnet_start_xmit,
1148 	.ndo_tx_timeout		= usbnet_tx_timeout,
1149 	.ndo_change_mtu		= usbnet_change_mtu,
1150 	.ndo_set_mac_address 	= eth_mac_addr,
1151 	.ndo_validate_addr	= eth_validate_addr,
1152 	.ndo_do_ioctl 		= smsc95xx_ioctl,
1153 	.ndo_set_rx_mode	= smsc95xx_set_multicast,
1154 	.ndo_set_features	= smsc95xx_set_features,
1155 };
1156 
1157 static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
1158 {
1159 	struct smsc95xx_priv *pdata = NULL;
1160 	u32 val;
1161 	int ret;
1162 
1163 	printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1164 
1165 	ret = usbnet_get_endpoints(dev, intf);
1166 	if (ret < 0) {
1167 		netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1168 		return ret;
1169 	}
1170 
1171 	dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
1172 					      GFP_KERNEL);
1173 
1174 	pdata = (struct smsc95xx_priv *)(dev->data[0]);
1175 	if (!pdata)
1176 		return -ENOMEM;
1177 
1178 	spin_lock_init(&pdata->mac_cr_lock);
1179 
1180 	if (DEFAULT_TX_CSUM_ENABLE)
1181 		dev->net->features |= NETIF_F_HW_CSUM;
1182 	if (DEFAULT_RX_CSUM_ENABLE)
1183 		dev->net->features |= NETIF_F_RXCSUM;
1184 
1185 	dev->net->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1186 
1187 	smsc95xx_init_mac_address(dev);
1188 
1189 	/* Init all registers */
1190 	ret = smsc95xx_reset(dev);
1191 
1192 	/* detect device revision as different features may be available */
1193 	ret = smsc95xx_read_reg(dev, ID_REV, &val);
1194 	if (ret < 0)
1195 		return ret;
1196 	val >>= 16;
1197 
1198 	if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
1199 	    (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
1200 		pdata->features = (FEATURE_8_WAKEUP_FILTERS |
1201 			FEATURE_PHY_NLP_CROSSOVER |
1202 			FEATURE_REMOTE_WAKEUP);
1203 	else if (val == ID_REV_CHIP_ID_9512_)
1204 		pdata->features = FEATURE_8_WAKEUP_FILTERS;
1205 
1206 	dev->net->netdev_ops = &smsc95xx_netdev_ops;
1207 	dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
1208 	dev->net->flags |= IFF_MULTICAST;
1209 	dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
1210 	dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1211 
1212 	pdata->dev = dev;
1213 	INIT_DELAYED_WORK(&pdata->carrier_check, check_carrier);
1214 	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1215 
1216 	return 0;
1217 }
1218 
1219 static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1220 {
1221 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1222 
1223 	if (pdata) {
1224 		cancel_delayed_work(&pdata->carrier_check);
1225 		netif_dbg(dev, ifdown, dev->net, "free pdata\n");
1226 		kfree(pdata);
1227 		pdata = NULL;
1228 		dev->data[0] = 0;
1229 	}
1230 }
1231 
1232 static u32 smsc_crc(const u8 *buffer, size_t len, int filter)
1233 {
1234 	u32 crc = bitrev16(crc16(0xFFFF, buffer, len));
1235 	return crc << ((filter % 2) * 16);
1236 }
1237 
1238 static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1239 {
1240 	struct mii_if_info *mii = &dev->mii;
1241 	int ret;
1242 
1243 	netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1244 
1245 	/* read to clear */
1246 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
1247 	if (ret < 0)
1248 		return ret;
1249 
1250 	/* enable interrupt source */
1251 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
1252 	if (ret < 0)
1253 		return ret;
1254 
1255 	ret |= mask;
1256 
1257 	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1258 
1259 	return 0;
1260 }
1261 
1262 static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1263 {
1264 	struct mii_if_info *mii = &dev->mii;
1265 	int ret;
1266 
1267 	/* first, a dummy read, needed to latch some MII phys */
1268 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1269 	if (ret < 0)
1270 		return ret;
1271 
1272 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1273 	if (ret < 0)
1274 		return ret;
1275 
1276 	return !!(ret & BMSR_LSTATUS);
1277 }
1278 
1279 static int smsc95xx_enter_suspend0(struct usbnet *dev)
1280 {
1281 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1282 	u32 val;
1283 	int ret;
1284 
1285 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1286 	if (ret < 0)
1287 		return ret;
1288 
1289 	val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1290 	val |= PM_CTL_SUS_MODE_0;
1291 
1292 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1293 	if (ret < 0)
1294 		return ret;
1295 
1296 	/* clear wol status */
1297 	val &= ~PM_CTL_WUPS_;
1298 	val |= PM_CTL_WUPS_WOL_;
1299 
1300 	/* enable energy detection */
1301 	if (pdata->wolopts & WAKE_PHY)
1302 		val |= PM_CTL_WUPS_ED_;
1303 
1304 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1305 	if (ret < 0)
1306 		return ret;
1307 
1308 	/* read back PM_CTRL */
1309 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1310 	if (ret < 0)
1311 		return ret;
1312 
1313 	pdata->suspend_flags |= SUSPEND_SUSPEND0;
1314 
1315 	return 0;
1316 }
1317 
1318 static int smsc95xx_enter_suspend1(struct usbnet *dev)
1319 {
1320 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1321 	struct mii_if_info *mii = &dev->mii;
1322 	u32 val;
1323 	int ret;
1324 
1325 	/* reconfigure link pulse detection timing for
1326 	 * compatibility with non-standard link partners
1327 	 */
1328 	if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
1329 		smsc95xx_mdio_write_nopm(dev->net, mii->phy_id,	PHY_EDPD_CONFIG,
1330 			PHY_EDPD_CONFIG_DEFAULT);
1331 
1332 	/* enable energy detect power-down mode */
1333 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS);
1334 	if (ret < 0)
1335 		return ret;
1336 
1337 	ret |= MODE_CTRL_STS_EDPWRDOWN_;
1338 
1339 	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS, ret);
1340 
1341 	/* enter SUSPEND1 mode */
1342 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1343 	if (ret < 0)
1344 		return ret;
1345 
1346 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1347 	val |= PM_CTL_SUS_MODE_1;
1348 
1349 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1350 	if (ret < 0)
1351 		return ret;
1352 
1353 	/* clear wol status, enable energy detection */
1354 	val &= ~PM_CTL_WUPS_;
1355 	val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1356 
1357 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1358 	if (ret < 0)
1359 		return ret;
1360 
1361 	pdata->suspend_flags |= SUSPEND_SUSPEND1;
1362 
1363 	return 0;
1364 }
1365 
1366 static int smsc95xx_enter_suspend2(struct usbnet *dev)
1367 {
1368 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1369 	u32 val;
1370 	int ret;
1371 
1372 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1373 	if (ret < 0)
1374 		return ret;
1375 
1376 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1377 	val |= PM_CTL_SUS_MODE_2;
1378 
1379 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1380 	if (ret < 0)
1381 		return ret;
1382 
1383 	pdata->suspend_flags |= SUSPEND_SUSPEND2;
1384 
1385 	return 0;
1386 }
1387 
1388 static int smsc95xx_enter_suspend3(struct usbnet *dev)
1389 {
1390 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1391 	u32 val;
1392 	int ret;
1393 
1394 	ret = smsc95xx_read_reg_nopm(dev, RX_FIFO_INF, &val);
1395 	if (ret < 0)
1396 		return ret;
1397 
1398 	if (val & 0xFFFF) {
1399 		netdev_info(dev->net, "rx fifo not empty in autosuspend\n");
1400 		return -EBUSY;
1401 	}
1402 
1403 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1404 	if (ret < 0)
1405 		return ret;
1406 
1407 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1408 	val |= PM_CTL_SUS_MODE_3 | PM_CTL_RES_CLR_WKP_STS;
1409 
1410 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1411 	if (ret < 0)
1412 		return ret;
1413 
1414 	/* clear wol status */
1415 	val &= ~PM_CTL_WUPS_;
1416 	val |= PM_CTL_WUPS_WOL_;
1417 
1418 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1419 	if (ret < 0)
1420 		return ret;
1421 
1422 	pdata->suspend_flags |= SUSPEND_SUSPEND3;
1423 
1424 	return 0;
1425 }
1426 
1427 static int smsc95xx_autosuspend(struct usbnet *dev, u32 link_up)
1428 {
1429 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1430 	int ret;
1431 
1432 	if (!netif_running(dev->net)) {
1433 		/* interface is ifconfig down so fully power down hw */
1434 		netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1435 		return smsc95xx_enter_suspend2(dev);
1436 	}
1437 
1438 	if (!link_up) {
1439 		/* link is down so enter EDPD mode, but only if device can
1440 		 * reliably resume from it.  This check should be redundant
1441 		 * as current FEATURE_REMOTE_WAKEUP parts also support
1442 		 * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */
1443 		if (!(pdata->features & FEATURE_PHY_NLP_CROSSOVER)) {
1444 			netdev_warn(dev->net, "EDPD not supported\n");
1445 			return -EBUSY;
1446 		}
1447 
1448 		netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1449 
1450 		/* enable PHY wakeup events for if cable is attached */
1451 		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1452 			PHY_INT_MASK_ANEG_COMP_);
1453 		if (ret < 0) {
1454 			netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1455 			return ret;
1456 		}
1457 
1458 		netdev_info(dev->net, "entering SUSPEND1 mode\n");
1459 		return smsc95xx_enter_suspend1(dev);
1460 	}
1461 
1462 	/* enable PHY wakeup events so we remote wakeup if cable is pulled */
1463 	ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1464 		PHY_INT_MASK_LINK_DOWN_);
1465 	if (ret < 0) {
1466 		netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1467 		return ret;
1468 	}
1469 
1470 	netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1471 	return smsc95xx_enter_suspend3(dev);
1472 }
1473 
1474 static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1475 {
1476 	struct usbnet *dev = usb_get_intfdata(intf);
1477 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1478 	u32 val, link_up;
1479 	int ret;
1480 
1481 	ret = usbnet_suspend(intf, message);
1482 	if (ret < 0) {
1483 		netdev_warn(dev->net, "usbnet_suspend error\n");
1484 		return ret;
1485 	}
1486 
1487 	if (pdata->suspend_flags) {
1488 		netdev_warn(dev->net, "error during last resume\n");
1489 		pdata->suspend_flags = 0;
1490 	}
1491 
1492 	/* determine if link is up using only _nopm functions */
1493 	link_up = smsc95xx_link_ok_nopm(dev);
1494 
1495 	if (message.event == PM_EVENT_AUTO_SUSPEND &&
1496 	    (pdata->features & FEATURE_REMOTE_WAKEUP)) {
1497 		ret = smsc95xx_autosuspend(dev, link_up);
1498 		goto done;
1499 	}
1500 
1501 	/* if we get this far we're not autosuspending */
1502 	/* if no wol options set, or if link is down and we're not waking on
1503 	 * PHY activity, enter lowest power SUSPEND2 mode
1504 	 */
1505 	if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1506 		!(link_up || (pdata->wolopts & WAKE_PHY))) {
1507 		netdev_info(dev->net, "entering SUSPEND2 mode\n");
1508 
1509 		/* disable energy detect (link up) & wake up events */
1510 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1511 		if (ret < 0)
1512 			goto done;
1513 
1514 		val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1515 
1516 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1517 		if (ret < 0)
1518 			goto done;
1519 
1520 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1521 		if (ret < 0)
1522 			goto done;
1523 
1524 		val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1525 
1526 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1527 		if (ret < 0)
1528 			goto done;
1529 
1530 		ret = smsc95xx_enter_suspend2(dev);
1531 		goto done;
1532 	}
1533 
1534 	if (pdata->wolopts & WAKE_PHY) {
1535 		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1536 			(PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
1537 		if (ret < 0) {
1538 			netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1539 			goto done;
1540 		}
1541 
1542 		/* if link is down then configure EDPD and enter SUSPEND1,
1543 		 * otherwise enter SUSPEND0 below
1544 		 */
1545 		if (!link_up) {
1546 			netdev_info(dev->net, "entering SUSPEND1 mode\n");
1547 			ret = smsc95xx_enter_suspend1(dev);
1548 			goto done;
1549 		}
1550 	}
1551 
1552 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1553 		u32 *filter_mask = kzalloc(sizeof(u32) * 32, GFP_KERNEL);
1554 		u32 command[2];
1555 		u32 offset[2];
1556 		u32 crc[4];
1557 		int wuff_filter_count =
1558 			(pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
1559 			LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
1560 		int i, filter = 0;
1561 
1562 		if (!filter_mask) {
1563 			netdev_warn(dev->net, "Unable to allocate filter_mask\n");
1564 			ret = -ENOMEM;
1565 			goto done;
1566 		}
1567 
1568 		memset(command, 0, sizeof(command));
1569 		memset(offset, 0, sizeof(offset));
1570 		memset(crc, 0, sizeof(crc));
1571 
1572 		if (pdata->wolopts & WAKE_BCAST) {
1573 			const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1574 			netdev_info(dev->net, "enabling broadcast detection\n");
1575 			filter_mask[filter * 4] = 0x003F;
1576 			filter_mask[filter * 4 + 1] = 0x00;
1577 			filter_mask[filter * 4 + 2] = 0x00;
1578 			filter_mask[filter * 4 + 3] = 0x00;
1579 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1580 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1581 			crc[filter/2] |= smsc_crc(bcast, 6, filter);
1582 			filter++;
1583 		}
1584 
1585 		if (pdata->wolopts & WAKE_MCAST) {
1586 			const u8 mcast[] = {0x01, 0x00, 0x5E};
1587 			netdev_info(dev->net, "enabling multicast detection\n");
1588 			filter_mask[filter * 4] = 0x0007;
1589 			filter_mask[filter * 4 + 1] = 0x00;
1590 			filter_mask[filter * 4 + 2] = 0x00;
1591 			filter_mask[filter * 4 + 3] = 0x00;
1592 			command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1593 			offset[filter/4] |= 0x00  << ((filter % 4) * 8);
1594 			crc[filter/2] |= smsc_crc(mcast, 3, filter);
1595 			filter++;
1596 		}
1597 
1598 		if (pdata->wolopts & WAKE_ARP) {
1599 			const u8 arp[] = {0x08, 0x06};
1600 			netdev_info(dev->net, "enabling ARP detection\n");
1601 			filter_mask[filter * 4] = 0x0003;
1602 			filter_mask[filter * 4 + 1] = 0x00;
1603 			filter_mask[filter * 4 + 2] = 0x00;
1604 			filter_mask[filter * 4 + 3] = 0x00;
1605 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1606 			offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1607 			crc[filter/2] |= smsc_crc(arp, 2, filter);
1608 			filter++;
1609 		}
1610 
1611 		if (pdata->wolopts & WAKE_UCAST) {
1612 			netdev_info(dev->net, "enabling unicast detection\n");
1613 			filter_mask[filter * 4] = 0x003F;
1614 			filter_mask[filter * 4 + 1] = 0x00;
1615 			filter_mask[filter * 4 + 2] = 0x00;
1616 			filter_mask[filter * 4 + 3] = 0x00;
1617 			command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1618 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1619 			crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1620 			filter++;
1621 		}
1622 
1623 		for (i = 0; i < (wuff_filter_count * 4); i++) {
1624 			ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
1625 			if (ret < 0) {
1626 				kfree(filter_mask);
1627 				goto done;
1628 			}
1629 		}
1630 		kfree(filter_mask);
1631 
1632 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1633 			ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
1634 			if (ret < 0)
1635 				goto done;
1636 		}
1637 
1638 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1639 			ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
1640 			if (ret < 0)
1641 				goto done;
1642 		}
1643 
1644 		for (i = 0; i < (wuff_filter_count / 2); i++) {
1645 			ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
1646 			if (ret < 0)
1647 				goto done;
1648 		}
1649 
1650 		/* clear any pending pattern match packet status */
1651 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1652 		if (ret < 0)
1653 			goto done;
1654 
1655 		val |= WUCSR_WUFR_;
1656 
1657 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1658 		if (ret < 0)
1659 			goto done;
1660 	}
1661 
1662 	if (pdata->wolopts & WAKE_MAGIC) {
1663 		/* clear any pending magic packet status */
1664 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1665 		if (ret < 0)
1666 			goto done;
1667 
1668 		val |= WUCSR_MPR_;
1669 
1670 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1671 		if (ret < 0)
1672 			goto done;
1673 	}
1674 
1675 	/* enable/disable wakeup sources */
1676 	ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1677 	if (ret < 0)
1678 		goto done;
1679 
1680 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1681 		netdev_info(dev->net, "enabling pattern match wakeup\n");
1682 		val |= WUCSR_WAKE_EN_;
1683 	} else {
1684 		netdev_info(dev->net, "disabling pattern match wakeup\n");
1685 		val &= ~WUCSR_WAKE_EN_;
1686 	}
1687 
1688 	if (pdata->wolopts & WAKE_MAGIC) {
1689 		netdev_info(dev->net, "enabling magic packet wakeup\n");
1690 		val |= WUCSR_MPEN_;
1691 	} else {
1692 		netdev_info(dev->net, "disabling magic packet wakeup\n");
1693 		val &= ~WUCSR_MPEN_;
1694 	}
1695 
1696 	ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1697 	if (ret < 0)
1698 		goto done;
1699 
1700 	/* enable wol wakeup source */
1701 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1702 	if (ret < 0)
1703 		goto done;
1704 
1705 	val |= PM_CTL_WOL_EN_;
1706 
1707 	/* phy energy detect wakeup source */
1708 	if (pdata->wolopts & WAKE_PHY)
1709 		val |= PM_CTL_ED_EN_;
1710 
1711 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1712 	if (ret < 0)
1713 		goto done;
1714 
1715 	/* enable receiver to enable frame reception */
1716 	smsc95xx_start_rx_path(dev, 1);
1717 
1718 	/* some wol options are enabled, so enter SUSPEND0 */
1719 	netdev_info(dev->net, "entering SUSPEND0 mode\n");
1720 	ret = smsc95xx_enter_suspend0(dev);
1721 
1722 done:
1723 	/*
1724 	 * TODO: resume() might need to handle the suspend failure
1725 	 * in system sleep
1726 	 */
1727 	if (ret && PMSG_IS_AUTO(message))
1728 		usbnet_resume(intf);
1729 	return ret;
1730 }
1731 
1732 static int smsc95xx_resume(struct usb_interface *intf)
1733 {
1734 	struct usbnet *dev = usb_get_intfdata(intf);
1735 	struct smsc95xx_priv *pdata;
1736 	u8 suspend_flags;
1737 	int ret;
1738 	u32 val;
1739 
1740 	BUG_ON(!dev);
1741 	pdata = (struct smsc95xx_priv *)(dev->data[0]);
1742 	suspend_flags = pdata->suspend_flags;
1743 
1744 	netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
1745 
1746 	/* do this first to ensure it's cleared even in error case */
1747 	pdata->suspend_flags = 0;
1748 	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1749 
1750 	if (suspend_flags & SUSPEND_ALLMODES) {
1751 		/* clear wake-up sources */
1752 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1753 		if (ret < 0)
1754 			return ret;
1755 
1756 		val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
1757 
1758 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1759 		if (ret < 0)
1760 			return ret;
1761 
1762 		/* clear wake-up status */
1763 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1764 		if (ret < 0)
1765 			return ret;
1766 
1767 		val &= ~PM_CTL_WOL_EN_;
1768 		val |= PM_CTL_WUPS_;
1769 
1770 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1771 		if (ret < 0)
1772 			return ret;
1773 	}
1774 
1775 	ret = usbnet_resume(intf);
1776 	if (ret < 0)
1777 		netdev_warn(dev->net, "usbnet_resume error\n");
1778 
1779 	return ret;
1780 }
1781 
1782 static int smsc95xx_reset_resume(struct usb_interface *intf)
1783 {
1784 	struct usbnet *dev = usb_get_intfdata(intf);
1785 	int ret;
1786 
1787 	ret = smsc95xx_reset(dev);
1788 	if (ret < 0)
1789 		return ret;
1790 
1791 	return smsc95xx_resume(intf);
1792 }
1793 
1794 static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
1795 {
1796 	skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
1797 	skb->ip_summed = CHECKSUM_COMPLETE;
1798 	skb_trim(skb, skb->len - 2);
1799 }
1800 
1801 static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1802 {
1803 	/* This check is no longer done by usbnet */
1804 	if (skb->len < dev->net->hard_header_len)
1805 		return 0;
1806 
1807 	while (skb->len > 0) {
1808 		u32 header, align_count;
1809 		struct sk_buff *ax_skb;
1810 		unsigned char *packet;
1811 		u16 size;
1812 
1813 		memcpy(&header, skb->data, sizeof(header));
1814 		le32_to_cpus(&header);
1815 		skb_pull(skb, 4 + NET_IP_ALIGN);
1816 		packet = skb->data;
1817 
1818 		/* get the packet length */
1819 		size = (u16)((header & RX_STS_FL_) >> 16);
1820 		align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
1821 
1822 		if (unlikely(header & RX_STS_ES_)) {
1823 			netif_dbg(dev, rx_err, dev->net,
1824 				  "Error header=0x%08x\n", header);
1825 			dev->net->stats.rx_errors++;
1826 			dev->net->stats.rx_dropped++;
1827 
1828 			if (header & RX_STS_CRC_) {
1829 				dev->net->stats.rx_crc_errors++;
1830 			} else {
1831 				if (header & (RX_STS_TL_ | RX_STS_RF_))
1832 					dev->net->stats.rx_frame_errors++;
1833 
1834 				if ((header & RX_STS_LE_) &&
1835 					(!(header & RX_STS_FT_)))
1836 					dev->net->stats.rx_length_errors++;
1837 			}
1838 		} else {
1839 			/* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1840 			if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1841 				netif_dbg(dev, rx_err, dev->net,
1842 					  "size err header=0x%08x\n", header);
1843 				return 0;
1844 			}
1845 
1846 			/* last frame in this batch */
1847 			if (skb->len == size) {
1848 				if (dev->net->features & NETIF_F_RXCSUM)
1849 					smsc95xx_rx_csum_offload(skb);
1850 				skb_trim(skb, skb->len - 4); /* remove fcs */
1851 				skb->truesize = size + sizeof(struct sk_buff);
1852 
1853 				return 1;
1854 			}
1855 
1856 			ax_skb = skb_clone(skb, GFP_ATOMIC);
1857 			if (unlikely(!ax_skb)) {
1858 				netdev_warn(dev->net, "Error allocating skb\n");
1859 				return 0;
1860 			}
1861 
1862 			ax_skb->len = size;
1863 			ax_skb->data = packet;
1864 			skb_set_tail_pointer(ax_skb, size);
1865 
1866 			if (dev->net->features & NETIF_F_RXCSUM)
1867 				smsc95xx_rx_csum_offload(ax_skb);
1868 			skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
1869 			ax_skb->truesize = size + sizeof(struct sk_buff);
1870 
1871 			usbnet_skb_return(dev, ax_skb);
1872 		}
1873 
1874 		skb_pull(skb, size);
1875 
1876 		/* padding bytes before the next frame starts */
1877 		if (skb->len)
1878 			skb_pull(skb, align_count);
1879 	}
1880 
1881 	return 1;
1882 }
1883 
1884 static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
1885 {
1886 	u16 low_16 = (u16)skb_checksum_start_offset(skb);
1887 	u16 high_16 = low_16 + skb->csum_offset;
1888 	return (high_16 << 16) | low_16;
1889 }
1890 
1891 static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
1892 					 struct sk_buff *skb, gfp_t flags)
1893 {
1894 	bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
1895 	int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
1896 	u32 tx_cmd_a, tx_cmd_b;
1897 
1898 	/* We do not advertise SG, so skbs should be already linearized */
1899 	BUG_ON(skb_shinfo(skb)->nr_frags);
1900 
1901 	if (skb_headroom(skb) < overhead) {
1902 		struct sk_buff *skb2 = skb_copy_expand(skb,
1903 			overhead, 0, flags);
1904 		dev_kfree_skb_any(skb);
1905 		skb = skb2;
1906 		if (!skb)
1907 			return NULL;
1908 	}
1909 
1910 	if (csum) {
1911 		if (skb->len <= 45) {
1912 			/* workaround - hardware tx checksum does not work
1913 			 * properly with extremely small packets */
1914 			long csstart = skb_checksum_start_offset(skb);
1915 			__wsum calc = csum_partial(skb->data + csstart,
1916 				skb->len - csstart, 0);
1917 			*((__sum16 *)(skb->data + csstart
1918 				+ skb->csum_offset)) = csum_fold(calc);
1919 
1920 			csum = false;
1921 		} else {
1922 			u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
1923 			skb_push(skb, 4);
1924 			cpu_to_le32s(&csum_preamble);
1925 			memcpy(skb->data, &csum_preamble, 4);
1926 		}
1927 	}
1928 
1929 	skb_push(skb, 4);
1930 	tx_cmd_b = (u32)(skb->len - 4);
1931 	if (csum)
1932 		tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
1933 	cpu_to_le32s(&tx_cmd_b);
1934 	memcpy(skb->data, &tx_cmd_b, 4);
1935 
1936 	skb_push(skb, 4);
1937 	tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ |
1938 		TX_CMD_A_LAST_SEG_;
1939 	cpu_to_le32s(&tx_cmd_a);
1940 	memcpy(skb->data, &tx_cmd_a, 4);
1941 
1942 	return skb;
1943 }
1944 
1945 static int smsc95xx_manage_power(struct usbnet *dev, int on)
1946 {
1947 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1948 
1949 	dev->intf->needs_remote_wakeup = on;
1950 
1951 	if (pdata->features & FEATURE_REMOTE_WAKEUP)
1952 		return 0;
1953 
1954 	/* this chip revision isn't capable of remote wakeup */
1955 	netdev_info(dev->net, "hardware isn't capable of remote wakeup\n");
1956 
1957 	if (on)
1958 		usb_autopm_get_interface_no_resume(dev->intf);
1959 	else
1960 		usb_autopm_put_interface(dev->intf);
1961 
1962 	return 0;
1963 }
1964 
1965 static const struct driver_info smsc95xx_info = {
1966 	.description	= "smsc95xx USB 2.0 Ethernet",
1967 	.bind		= smsc95xx_bind,
1968 	.unbind		= smsc95xx_unbind,
1969 	.link_reset	= smsc95xx_link_reset,
1970 	.reset		= smsc95xx_reset,
1971 	.rx_fixup	= smsc95xx_rx_fixup,
1972 	.tx_fixup	= smsc95xx_tx_fixup,
1973 	.status		= smsc95xx_status,
1974 	.manage_power	= smsc95xx_manage_power,
1975 	.flags		= FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
1976 };
1977 
1978 static const struct usb_device_id products[] = {
1979 	{
1980 		/* SMSC9500 USB Ethernet Device */
1981 		USB_DEVICE(0x0424, 0x9500),
1982 		.driver_info = (unsigned long) &smsc95xx_info,
1983 	},
1984 	{
1985 		/* SMSC9505 USB Ethernet Device */
1986 		USB_DEVICE(0x0424, 0x9505),
1987 		.driver_info = (unsigned long) &smsc95xx_info,
1988 	},
1989 	{
1990 		/* SMSC9500A USB Ethernet Device */
1991 		USB_DEVICE(0x0424, 0x9E00),
1992 		.driver_info = (unsigned long) &smsc95xx_info,
1993 	},
1994 	{
1995 		/* SMSC9505A USB Ethernet Device */
1996 		USB_DEVICE(0x0424, 0x9E01),
1997 		.driver_info = (unsigned long) &smsc95xx_info,
1998 	},
1999 	{
2000 		/* SMSC9512/9514 USB Hub & Ethernet Device */
2001 		USB_DEVICE(0x0424, 0xec00),
2002 		.driver_info = (unsigned long) &smsc95xx_info,
2003 	},
2004 	{
2005 		/* SMSC9500 USB Ethernet Device (SAL10) */
2006 		USB_DEVICE(0x0424, 0x9900),
2007 		.driver_info = (unsigned long) &smsc95xx_info,
2008 	},
2009 	{
2010 		/* SMSC9505 USB Ethernet Device (SAL10) */
2011 		USB_DEVICE(0x0424, 0x9901),
2012 		.driver_info = (unsigned long) &smsc95xx_info,
2013 	},
2014 	{
2015 		/* SMSC9500A USB Ethernet Device (SAL10) */
2016 		USB_DEVICE(0x0424, 0x9902),
2017 		.driver_info = (unsigned long) &smsc95xx_info,
2018 	},
2019 	{
2020 		/* SMSC9505A USB Ethernet Device (SAL10) */
2021 		USB_DEVICE(0x0424, 0x9903),
2022 		.driver_info = (unsigned long) &smsc95xx_info,
2023 	},
2024 	{
2025 		/* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
2026 		USB_DEVICE(0x0424, 0x9904),
2027 		.driver_info = (unsigned long) &smsc95xx_info,
2028 	},
2029 	{
2030 		/* SMSC9500A USB Ethernet Device (HAL) */
2031 		USB_DEVICE(0x0424, 0x9905),
2032 		.driver_info = (unsigned long) &smsc95xx_info,
2033 	},
2034 	{
2035 		/* SMSC9505A USB Ethernet Device (HAL) */
2036 		USB_DEVICE(0x0424, 0x9906),
2037 		.driver_info = (unsigned long) &smsc95xx_info,
2038 	},
2039 	{
2040 		/* SMSC9500 USB Ethernet Device (Alternate ID) */
2041 		USB_DEVICE(0x0424, 0x9907),
2042 		.driver_info = (unsigned long) &smsc95xx_info,
2043 	},
2044 	{
2045 		/* SMSC9500A USB Ethernet Device (Alternate ID) */
2046 		USB_DEVICE(0x0424, 0x9908),
2047 		.driver_info = (unsigned long) &smsc95xx_info,
2048 	},
2049 	{
2050 		/* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
2051 		USB_DEVICE(0x0424, 0x9909),
2052 		.driver_info = (unsigned long) &smsc95xx_info,
2053 	},
2054 	{
2055 		/* SMSC LAN9530 USB Ethernet Device */
2056 		USB_DEVICE(0x0424, 0x9530),
2057 		.driver_info = (unsigned long) &smsc95xx_info,
2058 	},
2059 	{
2060 		/* SMSC LAN9730 USB Ethernet Device */
2061 		USB_DEVICE(0x0424, 0x9730),
2062 		.driver_info = (unsigned long) &smsc95xx_info,
2063 	},
2064 	{
2065 		/* SMSC LAN89530 USB Ethernet Device */
2066 		USB_DEVICE(0x0424, 0x9E08),
2067 		.driver_info = (unsigned long) &smsc95xx_info,
2068 	},
2069 	{ },		/* END */
2070 };
2071 MODULE_DEVICE_TABLE(usb, products);
2072 
2073 static struct usb_driver smsc95xx_driver = {
2074 	.name		= "smsc95xx",
2075 	.id_table	= products,
2076 	.probe		= usbnet_probe,
2077 	.suspend	= smsc95xx_suspend,
2078 	.resume		= smsc95xx_resume,
2079 	.reset_resume	= smsc95xx_reset_resume,
2080 	.disconnect	= usbnet_disconnect,
2081 	.disable_hub_initiated_lpm = 1,
2082 	.supports_autosuspend = 1,
2083 };
2084 
2085 module_usb_driver(smsc95xx_driver);
2086 
2087 MODULE_AUTHOR("Nancy Lin");
2088 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
2089 MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
2090 MODULE_LICENSE("GPL");
2091