smsc95xx.c 48.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 /***************************************************************************
 *
 * Copyright (C) 2007-2008 SMSC
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *****************************************************************************/

#include <linux/module.h>
#include <linux/kmod.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/mii.h>
#include <linux/usb.h>
29 30
#include <linux/bitrev.h>
#include <linux/crc16.h>
31 32
#include <linux/crc32.h>
#include <linux/usb/usbnet.h>
33
#include <linux/slab.h>
34 35 36
#include "smsc95xx.h"

#define SMSC_CHIPNAME			"smsc95xx"
37
#define SMSC_DRIVER_VERSION		"1.0.4"
38 39 40 41 42 43 44 45
#define HS_USB_PKT_SIZE			(512)
#define FS_USB_PKT_SIZE			(64)
#define DEFAULT_HS_BURST_CAP_SIZE	(16 * 1024 + 5 * HS_USB_PKT_SIZE)
#define DEFAULT_FS_BURST_CAP_SIZE	(6 * 1024 + 33 * FS_USB_PKT_SIZE)
#define DEFAULT_BULK_IN_DELAY		(0x00002000)
#define MAX_SINGLE_PACKET_SIZE		(2048)
#define LAN95XX_EEPROM_MAGIC		(0x9500)
#define EEPROM_MAC_OFFSET		(0x01)
46
#define DEFAULT_TX_CSUM_ENABLE		(true)
47 48 49
#define DEFAULT_RX_CSUM_ENABLE		(true)
#define SMSC95XX_INTERNAL_PHY_ID	(1)
#define SMSC95XX_TX_OVERHEAD		(8)
50
#define SMSC95XX_TX_OVERHEAD_CSUM	(12)
51
#define SUPPORTED_WAKE			(WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
52
					 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
53

54 55 56 57
#define FEATURE_8_WAKEUP_FILTERS	(0x01)
#define FEATURE_PHY_NLP_CROSSOVER	(0x02)
#define FEATURE_AUTOSUSPEND		(0x04)

58 59 60 61 62 63 64 65 66
#define check_warn(ret, fmt, args...) \
	({ if (ret < 0) netdev_warn(dev->net, fmt, ##args); })

#define check_warn_return(ret, fmt, args...) \
	({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); return ret; } })

#define check_warn_goto_done(ret, fmt, args...) \
	({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); goto done; } })

67 68
struct smsc95xx_priv {
	u32 mac_cr;
69 70
	u32 hash_hi;
	u32 hash_lo;
71
	u32 wolopts;
72
	spinlock_t mac_cr_lock;
73
	u8 features;
74 75
};

76
static bool turbo_mode = true;
77 78 79
module_param(turbo_mode, bool, 0644);
MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");

80 81
static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
					    u32 *data, int in_pm)
82
{
83
	u32 buf;
84
	int ret;
85
	int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
86 87 88

	BUG_ON(!dev);

89 90 91 92 93 94 95 96
	if (!in_pm)
		fn = usbnet_read_cmd;
	else
		fn = usbnet_read_cmd_nopm;

	ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
		 0, index, &buf, 4);
97
	if (unlikely(ret < 0))
J
Joe Perches 已提交
98 99
		netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
			    index, ret);
100

101 102
	le32_to_cpus(&buf);
	*data = buf;
103 104 105 106

	return ret;
}

107 108
static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index,
					     u32 data, int in_pm)
109
{
110
	u32 buf;
111
	int ret;
112
	int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
113 114 115

	BUG_ON(!dev);

116 117 118 119 120
	if (!in_pm)
		fn = usbnet_write_cmd;
	else
		fn = usbnet_write_cmd_nopm;

121 122
	buf = data;
	cpu_to_le32s(&buf);
123

124 125 126
	ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
		 0, index, &buf, 4);
127
	if (unlikely(ret < 0))
J
Joe Perches 已提交
128 129
		netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
			    index, ret);
130 131 132 133

	return ret;
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index,
					       u32 *data)
{
	return __smsc95xx_read_reg(dev, index, data, 1);
}

static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index,
						u32 data)
{
	return __smsc95xx_write_reg(dev, index, data, 1);
}

static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
					  u32 *data)
{
	return __smsc95xx_read_reg(dev, index, data, 0);
}

static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
					   u32 data)
{
	return __smsc95xx_write_reg(dev, index, data, 0);
}
157 158 159 160 161
static int smsc95xx_set_feature(struct usbnet *dev, u32 feature)
{
	if (WARN_ON_ONCE(!dev))
		return -EINVAL;

162 163 164
	return usbnet_write_cmd_nopm(dev, USB_REQ_SET_FEATURE,
				     USB_RECIP_DEVICE, feature, 0,
				     NULL, 0);
165 166 167 168 169 170 171
}

static int smsc95xx_clear_feature(struct usbnet *dev, u32 feature)
{
	if (WARN_ON_ONCE(!dev))
		return -EINVAL;

172 173 174
	return usbnet_write_cmd_nopm(dev, USB_REQ_CLEAR_FEATURE,
				     USB_RECIP_DEVICE, feature,
				     0, NULL, 0);
175 176
}

177 178
/* Loop until the read is completed with timeout
 * called with phy_mutex held */
179 180
static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
						     int in_pm)
181 182 183
{
	unsigned long start_time = jiffies;
	u32 val;
184
	int ret;
185 186

	do {
187
		ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
J
Joe Perches 已提交
188
		check_warn_return(ret, "Error reading MII_ACCESS\n");
189 190 191 192 193 194 195
		if (!(val & MII_BUSY_))
			return 0;
	} while (!time_after(jiffies, start_time + HZ));

	return -EIO;
}

196 197
static int __smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
				int in_pm)
198 199 200
{
	struct usbnet *dev = netdev_priv(netdev);
	u32 val, addr;
201
	int ret;
202 203 204 205

	mutex_lock(&dev->phy_mutex);

	/* confirm MII not busy */
206
	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
J
Joe Perches 已提交
207
	check_warn_goto_done(ret, "MII is busy in smsc95xx_mdio_read\n");
208 209 210 211

	/* set the address, index & direction (read from PHY) */
	phy_id &= dev->mii.phy_id_mask;
	idx &= dev->mii.reg_num_mask;
212
	addr = (phy_id << 11) | (idx << 6) | MII_READ_ | MII_BUSY_;
213
	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
J
Joe Perches 已提交
214
	check_warn_goto_done(ret, "Error writing MII_ADDR\n");
215

216
	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
J
Joe Perches 已提交
217
	check_warn_goto_done(ret, "Timed out reading MII reg %02X\n", idx);
218

219
	ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
J
Joe Perches 已提交
220
	check_warn_goto_done(ret, "Error reading MII_DATA\n");
221

222
	ret = (u16)(val & 0xFFFF);
223

224 225 226
done:
	mutex_unlock(&dev->phy_mutex);
	return ret;
227 228
}

229 230
static void __smsc95xx_mdio_write(struct net_device *netdev, int phy_id,
				  int idx, int regval, int in_pm)
231 232 233
{
	struct usbnet *dev = netdev_priv(netdev);
	u32 val, addr;
234
	int ret;
235 236 237 238

	mutex_lock(&dev->phy_mutex);

	/* confirm MII not busy */
239
	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
J
Joe Perches 已提交
240
	check_warn_goto_done(ret, "MII is busy in smsc95xx_mdio_write\n");
241 242

	val = regval;
243
	ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
J
Joe Perches 已提交
244
	check_warn_goto_done(ret, "Error writing MII_DATA\n");
245 246 247 248

	/* set the address, index & direction (write to PHY) */
	phy_id &= dev->mii.phy_id_mask;
	idx &= dev->mii.reg_num_mask;
249
	addr = (phy_id << 11) | (idx << 6) | MII_WRITE_ | MII_BUSY_;
250
	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
J
Joe Perches 已提交
251
	check_warn_goto_done(ret, "Error writing MII_ADDR\n");
252

253
	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
J
Joe Perches 已提交
254
	check_warn_goto_done(ret, "Timed out writing MII reg %02X\n", idx);
255

256
done:
257 258 259
	mutex_unlock(&dev->phy_mutex);
}

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
static int smsc95xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
				   int idx)
{
	return __smsc95xx_mdio_read(netdev, phy_id, idx, 1);
}

static void smsc95xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
				     int idx, int regval)
{
	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 1);
}

static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
{
	return __smsc95xx_mdio_read(netdev, phy_id, idx, 0);
}

static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
				int regval)
{
	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 0);
}

283
static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
284 285 286
{
	unsigned long start_time = jiffies;
	u32 val;
287
	int ret;
288 289

	do {
290
		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
J
Joe Perches 已提交
291
		check_warn_return(ret, "Error reading E2P_CMD\n");
292 293 294 295 296 297
		if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
			break;
		udelay(40);
	} while (!time_after(jiffies, start_time + HZ));

	if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
298
		netdev_warn(dev->net, "EEPROM read operation timeout\n");
299 300 301 302 303 304
		return -EIO;
	}

	return 0;
}

305
static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
306 307 308
{
	unsigned long start_time = jiffies;
	u32 val;
309
	int ret;
310 311

	do {
312
		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
J
Joe Perches 已提交
313
		check_warn_return(ret, "Error reading E2P_CMD\n");
314 315 316 317 318 319 320

		if (!(val & E2P_CMD_BUSY_))
			return 0;

		udelay(40);
	} while (!time_after(jiffies, start_time + HZ));

321
	netdev_warn(dev->net, "EEPROM is busy\n");
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	return -EIO;
}

static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
				u8 *data)
{
	u32 val;
	int i, ret;

	BUG_ON(!dev);
	BUG_ON(!data);

	ret = smsc95xx_eeprom_confirm_not_busy(dev);
	if (ret)
		return ret;

	for (i = 0; i < length; i++) {
		val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
340
		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
J
Joe Perches 已提交
341
		check_warn_return(ret, "Error writing E2P_CMD\n");
342 343 344 345 346

		ret = smsc95xx_wait_eeprom(dev);
		if (ret < 0)
			return ret;

347
		ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
J
Joe Perches 已提交
348
		check_warn_return(ret, "Error reading E2P_DATA\n");
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

		data[i] = val & 0xFF;
		offset++;
	}

	return 0;
}

static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
				 u8 *data)
{
	u32 val;
	int i, ret;

	BUG_ON(!dev);
	BUG_ON(!data);

	ret = smsc95xx_eeprom_confirm_not_busy(dev);
	if (ret)
		return ret;

	/* Issue write/erase enable command */
	val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
372
	ret = smsc95xx_write_reg(dev, E2P_CMD, val);
J
Joe Perches 已提交
373
	check_warn_return(ret, "Error writing E2P_DATA\n");
374 375 376 377 378 379 380 381 382

	ret = smsc95xx_wait_eeprom(dev);
	if (ret < 0)
		return ret;

	for (i = 0; i < length; i++) {

		/* Fill data register */
		val = data[i];
383
		ret = smsc95xx_write_reg(dev, E2P_DATA, val);
J
Joe Perches 已提交
384
		check_warn_return(ret, "Error writing E2P_DATA\n");
385 386 387

		/* Send "write" command */
		val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
388
		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
J
Joe Perches 已提交
389
		check_warn_return(ret, "Error writing E2P_CMD\n");
390 391 392 393 394 395 396 397 398 399 400

		ret = smsc95xx_wait_eeprom(dev);
		if (ret < 0)
			return ret;

		offset++;
	}

	return 0;
}

401 402
static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
						 u32 *data)
403
{
404
	const u16 size = 4;
405
	int ret;
406

407 408 409 410 411 412 413 414
	ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
				     USB_DIR_OUT | USB_TYPE_VENDOR |
				     USB_RECIP_DEVICE,
				     0, index, data, size);
	if (ret < 0)
		netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
			    ret);
	return ret;
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
}

/* returns hash bit number for given MAC address
 * example:
 * 01 00 5E 00 00 01 -> returns bit number 31 */
static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
{
	return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
}

static void smsc95xx_set_multicast(struct net_device *netdev)
{
	struct usbnet *dev = netdev_priv(netdev);
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	unsigned long flags;
430
	int ret;
431

432 433 434
	pdata->hash_hi = 0;
	pdata->hash_lo = 0;

435 436 437
	spin_lock_irqsave(&pdata->mac_cr_lock, flags);

	if (dev->net->flags & IFF_PROMISC) {
438
		netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
439 440 441
		pdata->mac_cr |= MAC_CR_PRMS_;
		pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
	} else if (dev->net->flags & IFF_ALLMULTI) {
442
		netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
443 444
		pdata->mac_cr |= MAC_CR_MCPAS_;
		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
445
	} else if (!netdev_mc_empty(dev->net)) {
446
		struct netdev_hw_addr *ha;
447 448 449 450

		pdata->mac_cr |= MAC_CR_HPFILT_;
		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);

451 452
		netdev_for_each_mc_addr(ha, netdev) {
			u32 bitnum = smsc95xx_hash(ha->addr);
453 454
			u32 mask = 0x01 << (bitnum & 0x1F);
			if (bitnum & 0x20)
455
				pdata->hash_hi |= mask;
456
			else
457
				pdata->hash_lo |= mask;
458 459
		}

460
		netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
461
				   pdata->hash_hi, pdata->hash_lo);
462
	} else {
463
		netif_dbg(dev, drv, dev->net, "receive own packets only\n");
464 465 466 467 468 469 470
		pdata->mac_cr &=
			~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
	}

	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);

	/* Initiate async writes, as we can't wait for completion here */
471
	ret = smsc95xx_write_reg_async(dev, HASHH, &pdata->hash_hi);
J
Joe Perches 已提交
472
	check_warn(ret, "failed to initiate async write to HASHH\n");
473 474

	ret = smsc95xx_write_reg_async(dev, HASHL, &pdata->hash_lo);
J
Joe Perches 已提交
475
	check_warn(ret, "failed to initiate async write to HASHL\n");
476 477

	ret = smsc95xx_write_reg_async(dev, MAC_CR, &pdata->mac_cr);
J
Joe Perches 已提交
478
	check_warn(ret, "failed to initiate async write to MAC_CR\n");
479 480
}

481 482
static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
					   u16 lcladv, u16 rmtadv)
483 484 485 486
{
	u32 flow, afc_cfg = 0;

	int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
J
Joe Perches 已提交
487
	check_warn_return(ret, "Error reading AFC_CFG\n");
488 489

	if (duplex == DUPLEX_FULL) {
490
		u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
491 492 493 494 495 496 497 498 499 500 501

		if (cap & FLOW_CTRL_RX)
			flow = 0xFFFF0002;
		else
			flow = 0;

		if (cap & FLOW_CTRL_TX)
			afc_cfg |= 0xF;
		else
			afc_cfg &= ~0xF;

502
		netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
503 504
				   cap & FLOW_CTRL_RX ? "enabled" : "disabled",
				   cap & FLOW_CTRL_TX ? "enabled" : "disabled");
505
	} else {
506
		netif_dbg(dev, link, dev->net, "half duplex\n");
507 508 509 510
		flow = 0;
		afc_cfg |= 0xF;
	}

511
	ret = smsc95xx_write_reg(dev, FLOW, flow);
J
Joe Perches 已提交
512
	check_warn_return(ret, "Error writing FLOW\n");
513 514

	ret = smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
J
Joe Perches 已提交
515
	check_warn_return(ret, "Error writing AFC_CFG\n");
516 517

	return 0;
518 519 520 521 522 523
}

static int smsc95xx_link_reset(struct usbnet *dev)
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	struct mii_if_info *mii = &dev->mii;
524
	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
525 526
	unsigned long flags;
	u16 lcladv, rmtadv;
527
	int ret;
528 529

	/* clear interrupt status */
530
	ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
J
Joe Perches 已提交
531
	check_warn_return(ret, "Error reading PHY_INT_SRC\n");
532 533

	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
J
Joe Perches 已提交
534
	check_warn_return(ret, "Error writing INT_STS\n");
535 536 537 538 539 540

	mii_check_media(mii, 1, 1);
	mii_ethtool_gset(&dev->mii, &ecmd);
	lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
	rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);

541 542 543
	netif_dbg(dev, link, dev->net,
		  "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
		  ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
544 545 546 547 548 549 550 551 552 553 554

	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
	if (ecmd.duplex != DUPLEX_FULL) {
		pdata->mac_cr &= ~MAC_CR_FDPX_;
		pdata->mac_cr |= MAC_CR_RCVOWN_;
	} else {
		pdata->mac_cr &= ~MAC_CR_RCVOWN_;
		pdata->mac_cr |= MAC_CR_FDPX_;
	}
	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);

555
	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
J
Joe Perches 已提交
556
	check_warn_return(ret, "Error writing MAC_CR\n");
557

558
	ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
J
Joe Perches 已提交
559
	check_warn_return(ret, "Error updating PHY flow control\n");
560 561 562 563 564 565 566 567 568

	return 0;
}

static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
{
	u32 intdata;

	if (urb->actual_length != 4) {
569 570
		netdev_warn(dev->net, "unexpected urb length %d\n",
			    urb->actual_length);
571 572 573 574
		return;
	}

	memcpy(&intdata, urb->transfer_buffer, 4);
575
	le32_to_cpus(&intdata);
576

577
	netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
578 579 580 581

	if (intdata & INT_ENP_PHY_INT_)
		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
	else
582 583
		netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
			    intdata);
584 585
}

586
/* Enable or disable Tx & Rx checksum offload engines */
587 588
static int smsc95xx_set_features(struct net_device *netdev,
	netdev_features_t features)
589
{
590
	struct usbnet *dev = netdev_priv(netdev);
591
	u32 read_buf;
592 593 594
	int ret;

	ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
595
	check_warn_return(ret, "Failed to read COE_CR: %d\n", ret);
596

597
	if (features & NETIF_F_HW_CSUM)
598 599 600 601
		read_buf |= Tx_COE_EN_;
	else
		read_buf &= ~Tx_COE_EN_;

602
	if (features & NETIF_F_RXCSUM)
603 604 605 606 607
		read_buf |= Rx_COE_EN_;
	else
		read_buf &= ~Rx_COE_EN_;

	ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
608
	check_warn_return(ret, "Failed to write COE_CR: %d\n", ret);
609

610
	netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
	return 0;
}

static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
{
	return MAX_EEPROM_SIZE;
}

static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
				       struct ethtool_eeprom *ee, u8 *data)
{
	struct usbnet *dev = netdev_priv(netdev);

	ee->magic = LAN95XX_EEPROM_MAGIC;

	return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
}

static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
				       struct ethtool_eeprom *ee, u8 *data)
{
	struct usbnet *dev = netdev_priv(netdev);

	if (ee->magic != LAN95XX_EEPROM_MAGIC) {
635 636
		netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
			    ee->magic);
637 638 639 640 641 642
		return -EINVAL;
	}

	return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
}

643 644 645 646 647 648 649 650 651 652 653
static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
{
	/* all smsc95xx registers */
	return COE_CR - ID_REV + 1;
}

static void
smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
			 void *buf)
{
	struct usbnet *dev = netdev_priv(netdev);
654 655
	unsigned int i, j;
	int retval;
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
	u32 *data = buf;

	retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
	if (retval < 0) {
		netdev_warn(netdev, "REGS: cannot read ID_REV\n");
		return;
	}

	for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
		retval = smsc95xx_read_reg(dev, i, &data[j]);
		if (retval < 0) {
			netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
			return;
		}
	}
}

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
static void smsc95xx_ethtool_get_wol(struct net_device *net,
				     struct ethtool_wolinfo *wolinfo)
{
	struct usbnet *dev = netdev_priv(net);
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);

	wolinfo->supported = SUPPORTED_WAKE;
	wolinfo->wolopts = pdata->wolopts;
}

static int smsc95xx_ethtool_set_wol(struct net_device *net,
				    struct ethtool_wolinfo *wolinfo)
{
	struct usbnet *dev = netdev_priv(net);
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);

	pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
	return 0;
}

693
static const struct ethtool_ops smsc95xx_ethtool_ops = {
694 695 696 697 698 699 700 701 702 703
	.get_link	= usbnet_get_link,
	.nway_reset	= usbnet_nway_reset,
	.get_drvinfo	= usbnet_get_drvinfo,
	.get_msglevel	= usbnet_get_msglevel,
	.set_msglevel	= usbnet_set_msglevel,
	.get_settings	= usbnet_get_settings,
	.set_settings	= usbnet_set_settings,
	.get_eeprom_len	= smsc95xx_ethtool_get_eeprom_len,
	.get_eeprom	= smsc95xx_ethtool_get_eeprom,
	.set_eeprom	= smsc95xx_ethtool_set_eeprom,
704 705
	.get_regs_len	= smsc95xx_ethtool_getregslen,
	.get_regs	= smsc95xx_ethtool_getregs,
706 707
	.get_wol	= smsc95xx_ethtool_get_wol,
	.set_wol	= smsc95xx_ethtool_set_wol,
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
};

static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
{
	struct usbnet *dev = netdev_priv(netdev);

	if (!netif_running(netdev))
		return -EINVAL;

	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
}

static void smsc95xx_init_mac_address(struct usbnet *dev)
{
	/* try reading mac address from EEPROM */
	if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
			dev->net->dev_addr) == 0) {
		if (is_valid_ether_addr(dev->net->dev_addr)) {
			/* eeprom values are valid so use them */
727
			netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
728 729 730 731 732
			return;
		}
	}

	/* no eeprom, or eeprom values are invalid. generate random MAC */
733
	eth_hw_addr_random(dev->net);
J
Joe Perches 已提交
734
	netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
735 736 737 738 739 740 741 742 743 744
}

static int smsc95xx_set_mac_address(struct usbnet *dev)
{
	u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
		dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
	u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
	int ret;

	ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
745
	check_warn_return(ret, "Failed to write ADDRL: %d\n", ret);
746 747

	ret = smsc95xx_write_reg(dev, ADDRH, addr_hi);
748
	check_warn_return(ret, "Failed to write ADDRH: %d\n", ret);
749 750 751 752 753

	return 0;
}

/* starts the TX path */
754
static int smsc95xx_start_tx_path(struct usbnet *dev)
755 756 757
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	unsigned long flags;
758
	int ret;
759 760 761 762 763 764

	/* Enable Tx at MAC */
	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
	pdata->mac_cr |= MAC_CR_TXEN_;
	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);

765 766
	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
	check_warn_return(ret, "Failed to write MAC_CR: %d\n", ret);
767 768

	/* Enable Tx at SCSRs */
769 770 771 772
	ret = smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
	check_warn_return(ret, "Failed to write TX_CFG: %d\n", ret);

	return 0;
773 774 775
}

/* Starts the Receive path */
776
static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
777 778 779
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	unsigned long flags;
780
	int ret;
781 782 783 784 785

	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
	pdata->mac_cr |= MAC_CR_RXEN_;
	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);

786
	ret = __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
787 788 789
	check_warn_return(ret, "Failed to write MAC_CR: %d\n", ret);

	return 0;
790 791 792 793
}

static int smsc95xx_phy_initialize(struct usbnet *dev)
{
794
	int bmcr, ret, timeout = 0;
795

796 797 798 799 800 801 802 803
	/* Initialize MII structure */
	dev->mii.dev = dev->net;
	dev->mii.mdio_read = smsc95xx_mdio_read;
	dev->mii.mdio_write = smsc95xx_mdio_write;
	dev->mii.phy_id_mask = 0x1f;
	dev->mii.reg_num_mask = 0x1f;
	dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;

804
	/* reset phy and wait for reset to complete */
805
	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
806 807 808 809 810

	do {
		msleep(10);
		bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
		timeout++;
R
Rabin Vincent 已提交
811
	} while ((bmcr & BMCR_RESET) && (timeout < 100));
812 813 814 815 816 817

	if (timeout >= 100) {
		netdev_warn(dev->net, "timeout on PHY Reset");
		return -EIO;
	}

818 819 820 821 822
	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
		ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
		ADVERTISE_PAUSE_ASYM);

	/* read to clear */
823
	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
J
Joe Perches 已提交
824
	check_warn_return(ret, "Failed to read PHY_INT_SRC during init\n");
825 826 827 828 829

	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
		PHY_INT_MASK_DEFAULT_);
	mii_nway_restart(&dev->mii);

830
	netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
831 832 833 834 835 836 837 838 839
	return 0;
}

static int smsc95xx_reset(struct usbnet *dev)
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	u32 read_buf, write_buf, burst_cap;
	int ret = 0, timeout;

840
	netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
841

842
	ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
843
	check_warn_return(ret, "Failed to write HW_CFG_LRST_ bit in HW_CFG\n");
844 845 846

	timeout = 0;
	do {
847
		msleep(10);
848
		ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
849
		check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
850 851 852 853
		timeout++;
	} while ((read_buf & HW_CFG_LRST_) && (timeout < 100));

	if (timeout >= 100) {
854
		netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
855 856 857
		return ret;
	}

858
	ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
859
	check_warn_return(ret, "Failed to write PM_CTRL: %d\n", ret);
860 861 862

	timeout = 0;
	do {
863
		msleep(10);
864
		ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
865
		check_warn_return(ret, "Failed to read PM_CTRL: %d\n", ret);
866 867 868 869
		timeout++;
	} while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));

	if (timeout >= 100) {
870
		netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
871 872 873 874 875 876 877
		return ret;
	}

	ret = smsc95xx_set_mac_address(dev);
	if (ret < 0)
		return ret;

J
Joe Perches 已提交
878 879
	netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
		  dev->net->dev_addr);
880 881

	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
882
	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
883

J
Joe Perches 已提交
884 885
	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
		  read_buf);
886 887 888 889

	read_buf |= HW_CFG_BIR_;

	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
890
	check_warn_return(ret, "Failed to write HW_CFG_BIR_ bit in HW_CFG\n");
891 892

	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
893
	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
894 895 896
	netif_dbg(dev, ifup, dev->net,
		  "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
		  read_buf);
897 898 899 900 901 902 903 904 905 906 907 908

	if (!turbo_mode) {
		burst_cap = 0;
		dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
	} else if (dev->udev->speed == USB_SPEED_HIGH) {
		burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
		dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
	} else {
		burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
		dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
	}

J
Joe Perches 已提交
909 910
	netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
		  (ulong)dev->rx_urb_size);
911 912

	ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
913
	check_warn_return(ret, "Failed to write BURST_CAP: %d\n", ret);
914 915

	ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
916 917
	check_warn_return(ret, "Failed to read BURST_CAP: %d\n", ret);

918 919 920
	netif_dbg(dev, ifup, dev->net,
		  "Read Value from BURST_CAP after writing: 0x%08x\n",
		  read_buf);
921

922
	ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
923
	check_warn_return(ret, "Failed to write BULK_IN_DLY: %d\n", ret);
924 925

	ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
926 927
	check_warn_return(ret, "Failed to read BULK_IN_DLY: %d\n", ret);

928 929 930
	netif_dbg(dev, ifup, dev->net,
		  "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
		  read_buf);
931 932

	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
933 934
	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);

J
Joe Perches 已提交
935 936
	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
		  read_buf);
937 938 939 940 941 942 943 944 945 946

	if (turbo_mode)
		read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);

	read_buf &= ~HW_CFG_RXDOFF_;

	/* set Rx data offset=2, Make IP header aligns on word boundary. */
	read_buf |= NET_IP_ALIGN << 9;

	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
947
	check_warn_return(ret, "Failed to write HW_CFG: %d\n", ret);
948 949

	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
950 951
	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);

952 953
	netif_dbg(dev, ifup, dev->net,
		  "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
954

955
	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
956
	check_warn_return(ret, "Failed to write INT_STS: %d\n", ret);
957 958

	ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
959
	check_warn_return(ret, "Failed to read ID_REV: %d\n", ret);
960
	netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
961

962 963 964 965
	/* Configure GPIO pins as LED outputs */
	write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
		LED_GPIO_CFG_FDX_LED;
	ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
966
	check_warn_return(ret, "Failed to write LED_GPIO_CFG: %d\n", ret);
967

968
	/* Init Tx */
969
	ret = smsc95xx_write_reg(dev, FLOW, 0);
970
	check_warn_return(ret, "Failed to write FLOW: %d\n", ret);
971

972
	ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
973
	check_warn_return(ret, "Failed to write AFC_CFG: %d\n", ret);
974 975 976

	/* Don't need mac_cr_lock during initialisation */
	ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
977
	check_warn_return(ret, "Failed to read MAC_CR: %d\n", ret);
978 979 980

	/* Init Rx */
	/* Set Vlan */
981
	ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
982
	check_warn_return(ret, "Failed to write VLAN1: %d\n", ret);
983

984
	/* Enable or disable checksum offload engines */
985
	ret = smsc95xx_set_features(dev->net, dev->net->features);
J
Joe Perches 已提交
986
	check_warn_return(ret, "Failed to set checksum offload features\n");
987 988 989

	smsc95xx_set_multicast(dev->net);

990
	ret = smsc95xx_phy_initialize(dev);
J
Joe Perches 已提交
991
	check_warn_return(ret, "Failed to init PHY\n");
992 993

	ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
994
	check_warn_return(ret, "Failed to read INT_EP_CTL: %d\n", ret);
995 996 997 998 999

	/* enable PHY interrupts */
	read_buf |= INT_EP_CTL_PHY_INT_;

	ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
1000
	check_warn_return(ret, "Failed to write INT_EP_CTL: %d\n", ret);
1001

1002
	ret = smsc95xx_start_tx_path(dev);
J
Joe Perches 已提交
1003
	check_warn_return(ret, "Failed to start TX path\n");
1004

1005
	ret = smsc95xx_start_rx_path(dev, 0);
J
Joe Perches 已提交
1006
	check_warn_return(ret, "Failed to start RX path\n");
1007

1008
	netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
1009 1010 1011
	return 0;
}

1012 1013 1014 1015 1016 1017 1018 1019 1020
static const struct net_device_ops smsc95xx_netdev_ops = {
	.ndo_open		= usbnet_open,
	.ndo_stop		= usbnet_stop,
	.ndo_start_xmit		= usbnet_start_xmit,
	.ndo_tx_timeout		= usbnet_tx_timeout,
	.ndo_change_mtu		= usbnet_change_mtu,
	.ndo_set_mac_address 	= eth_mac_addr,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_do_ioctl 		= smsc95xx_ioctl,
1021
	.ndo_set_rx_mode	= smsc95xx_set_multicast,
1022
	.ndo_set_features	= smsc95xx_set_features,
1023 1024
};

1025 1026 1027
static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
{
	struct smsc95xx_priv *pdata = NULL;
1028
	u32 val;
1029 1030 1031 1032 1033
	int ret;

	printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");

	ret = usbnet_get_endpoints(dev, intf);
1034
	check_warn_return(ret, "usbnet_get_endpoints failed: %d\n", ret);
1035 1036 1037 1038 1039 1040

	dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
		GFP_KERNEL);

	pdata = (struct smsc95xx_priv *)(dev->data[0]);
	if (!pdata) {
1041
		netdev_warn(dev->net, "Unable to allocate struct smsc95xx_priv\n");
1042 1043 1044 1045 1046
		return -ENOMEM;
	}

	spin_lock_init(&pdata->mac_cr_lock);

1047 1048 1049 1050 1051 1052
	if (DEFAULT_TX_CSUM_ENABLE)
		dev->net->features |= NETIF_F_HW_CSUM;
	if (DEFAULT_RX_CSUM_ENABLE)
		dev->net->features |= NETIF_F_RXCSUM;

	dev->net->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1053

1054 1055
	smsc95xx_init_mac_address(dev);

1056 1057 1058
	/* Init all registers */
	ret = smsc95xx_reset(dev);

1059 1060 1061 1062
	/* detect device revision as different features may be available */
	ret = smsc95xx_read_reg(dev, ID_REV, &val);
	check_warn_return(ret, "Failed to read ID_REV: %d\n", ret);
	val >>= 16;
1063 1064 1065 1066 1067 1068 1069 1070

	if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
	    (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
		pdata->features = (FEATURE_8_WAKEUP_FILTERS |
			FEATURE_PHY_NLP_CROSSOVER |
			FEATURE_AUTOSUSPEND);
	else if (val == ID_REV_CHIP_ID_9512_)
		pdata->features = FEATURE_8_WAKEUP_FILTERS;
1071

1072
	dev->net->netdev_ops = &smsc95xx_netdev_ops;
1073 1074
	dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
	dev->net->flags |= IFF_MULTICAST;
1075
	dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
S
Stephane Fillod 已提交
1076
	dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1077 1078 1079 1080 1081 1082 1083
	return 0;
}

static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	if (pdata) {
1084
		netif_dbg(dev, ifdown, dev->net, "free pdata\n");
1085 1086 1087 1088 1089 1090
		kfree(pdata);
		pdata = NULL;
		dev->data[0] = 0;
	}
}

1091 1092 1093 1094 1095
static u16 smsc_crc(const u8 *buffer, size_t len, int filter)
{
	return bitrev16(crc16(0xFFFF, buffer, len)) << ((filter % 2) * 16);
}

1096 1097 1098 1099 1100
static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
{
	struct mii_if_info *mii = &dev->mii;
	int ret;

J
Joe Perches 已提交
1101
	netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1102 1103 1104

	/* read to clear */
	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
J
Joe Perches 已提交
1105
	check_warn_return(ret, "Error reading PHY_INT_SRC\n");
1106 1107 1108

	/* enable interrupt source */
	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
J
Joe Perches 已提交
1109
	check_warn_return(ret, "Error reading PHY_INT_MASK\n");
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124

	ret |= mask;

	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);

	return 0;
}

static int smsc95xx_link_ok_nopm(struct usbnet *dev)
{
	struct mii_if_info *mii = &dev->mii;
	int ret;

	/* first, a dummy read, needed to latch some MII phys */
	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
J
Joe Perches 已提交
1125
	check_warn_return(ret, "Error reading MII_BMSR\n");
1126 1127

	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
J
Joe Perches 已提交
1128
	check_warn_return(ret, "Error reading MII_BMSR\n");
1129 1130 1131 1132

	return !!(ret & BMSR_LSTATUS);
}

1133 1134 1135 1136 1137 1138 1139
static int smsc95xx_enter_suspend0(struct usbnet *dev)
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	u32 val;
	int ret;

	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
J
Joe Perches 已提交
1140
	check_warn_return(ret, "Error reading PM_CTRL\n");
1141 1142 1143 1144 1145

	val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
	val |= PM_CTL_SUS_MODE_0;

	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
J
Joe Perches 已提交
1146
	check_warn_return(ret, "Error writing PM_CTRL\n");
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156

	/* clear wol status */
	val &= ~PM_CTL_WUPS_;
	val |= PM_CTL_WUPS_WOL_;

	/* enable energy detection */
	if (pdata->wolopts & WAKE_PHY)
		val |= PM_CTL_WUPS_ED_;

	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
J
Joe Perches 已提交
1157
	check_warn_return(ret, "Error writing PM_CTRL\n");
1158 1159 1160

	/* read back PM_CTRL */
	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
J
Joe Perches 已提交
1161
	check_warn_return(ret, "Error reading PM_CTRL\n");
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183

	smsc95xx_set_feature(dev, USB_DEVICE_REMOTE_WAKEUP);

	return 0;
}

static int smsc95xx_enter_suspend1(struct usbnet *dev)
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	struct mii_if_info *mii = &dev->mii;
	u32 val;
	int ret;

	/* reconfigure link pulse detection timing for
	 * compatibility with non-standard link partners
	 */
	if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
		smsc95xx_mdio_write_nopm(dev->net, mii->phy_id,	PHY_EDPD_CONFIG,
			PHY_EDPD_CONFIG_DEFAULT);

	/* enable energy detect power-down mode */
	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS);
J
Joe Perches 已提交
1184
	check_warn_return(ret, "Error reading PHY_MODE_CTRL_STS\n");
1185 1186 1187 1188 1189 1190 1191

	ret |= MODE_CTRL_STS_EDPWRDOWN_;

	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS, ret);

	/* enter SUSPEND1 mode */
	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
J
Joe Perches 已提交
1192
	check_warn_return(ret, "Error reading PM_CTRL\n");
1193 1194 1195 1196 1197

	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
	val |= PM_CTL_SUS_MODE_1;

	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
J
Joe Perches 已提交
1198
	check_warn_return(ret, "Error writing PM_CTRL\n");
1199 1200 1201 1202 1203 1204

	/* clear wol status, enable energy detection */
	val &= ~PM_CTL_WUPS_;
	val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);

	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
J
Joe Perches 已提交
1205
	check_warn_return(ret, "Error writing PM_CTRL\n");
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

	smsc95xx_set_feature(dev, USB_DEVICE_REMOTE_WAKEUP);

	return 0;
}

static int smsc95xx_enter_suspend2(struct usbnet *dev)
{
	u32 val;
	int ret;

	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
J
Joe Perches 已提交
1218
	check_warn_return(ret, "Error reading PM_CTRL\n");
1219 1220 1221 1222 1223

	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
	val |= PM_CTL_SUS_MODE_2;

	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
J
Joe Perches 已提交
1224
	check_warn_return(ret, "Error writing PM_CTRL\n");
1225 1226 1227 1228

	return 0;
}

1229 1230 1231
static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
{
	struct usbnet *dev = usb_get_intfdata(intf);
1232
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1233
	u32 val, link_up;
1234 1235 1236
	int ret;

	ret = usbnet_suspend(intf, message);
J
Joe Perches 已提交
1237
	check_warn_return(ret, "usbnet_suspend error\n");
1238

1239 1240 1241 1242 1243 1244 1245 1246
	/* determine if link is up using only _nopm functions */
	link_up = smsc95xx_link_ok_nopm(dev);

	/* if no wol options set, or if link is down and we're not waking on
	 * PHY activity, enter lowest power SUSPEND2 mode
	 */
	if (!(pdata->wolopts & SUPPORTED_WAKE) ||
		!(link_up || (pdata->wolopts & WAKE_PHY))) {
J
Joe Perches 已提交
1247
		netdev_info(dev->net, "entering SUSPEND2 mode\n");
1248 1249

		/* disable energy detect (link up) & wake up events */
1250
		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1251
		check_warn_goto_done(ret, "Error reading WUCSR\n");
1252 1253 1254

		val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);

1255
		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1256
		check_warn_goto_done(ret, "Error writing WUCSR\n");
1257

1258
		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1259
		check_warn_goto_done(ret, "Error reading PM_CTRL\n");
1260 1261 1262

		val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);

1263
		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1264
		check_warn_goto_done(ret, "Error writing PM_CTRL\n");
1265

1266 1267
		ret = smsc95xx_enter_suspend2(dev);
		goto done;
1268 1269
	}

1270 1271 1272
	if (pdata->wolopts & WAKE_PHY) {
		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
			(PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
1273
		check_warn_goto_done(ret, "error enabling PHY wakeup ints\n");
1274 1275 1276 1277 1278

		/* if link is down then configure EDPD and enter SUSPEND1,
		 * otherwise enter SUSPEND0 below
		 */
		if (!link_up) {
J
Joe Perches 已提交
1279
			netdev_info(dev->net, "entering SUSPEND1 mode\n");
1280 1281
			ret = smsc95xx_enter_suspend1(dev);
			goto done;
1282 1283 1284
		}
	}

1285
	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1286
		u32 *filter_mask = kzalloc(sizeof(u32) * 32, GFP_KERNEL);
1287 1288 1289
		u32 command[2];
		u32 offset[2];
		u32 crc[4];
1290 1291 1292
		int wuff_filter_count =
			(pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
			LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
1293 1294
		int i, filter = 0;

1295 1296
		if (!filter_mask) {
			netdev_warn(dev->net, "Unable to allocate filter_mask\n");
1297 1298
			ret = -ENOMEM;
			goto done;
1299 1300
		}

1301 1302 1303 1304
		memset(command, 0, sizeof(command));
		memset(offset, 0, sizeof(offset));
		memset(crc, 0, sizeof(crc));

1305 1306
		if (pdata->wolopts & WAKE_BCAST) {
			const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
J
Joe Perches 已提交
1307
			netdev_info(dev->net, "enabling broadcast detection\n");
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
			filter_mask[filter * 4] = 0x003F;
			filter_mask[filter * 4 + 1] = 0x00;
			filter_mask[filter * 4 + 2] = 0x00;
			filter_mask[filter * 4 + 3] = 0x00;
			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
			crc[filter/2] |= smsc_crc(bcast, 6, filter);
			filter++;
		}

		if (pdata->wolopts & WAKE_MCAST) {
			const u8 mcast[] = {0x01, 0x00, 0x5E};
J
Joe Perches 已提交
1320
			netdev_info(dev->net, "enabling multicast detection\n");
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
			filter_mask[filter * 4] = 0x0007;
			filter_mask[filter * 4 + 1] = 0x00;
			filter_mask[filter * 4 + 2] = 0x00;
			filter_mask[filter * 4 + 3] = 0x00;
			command[filter/4] |= 0x09UL << ((filter % 4) * 8);
			offset[filter/4] |= 0x00  << ((filter % 4) * 8);
			crc[filter/2] |= smsc_crc(mcast, 3, filter);
			filter++;
		}

		if (pdata->wolopts & WAKE_ARP) {
			const u8 arp[] = {0x08, 0x06};
J
Joe Perches 已提交
1333
			netdev_info(dev->net, "enabling ARP detection\n");
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
			filter_mask[filter * 4] = 0x0003;
			filter_mask[filter * 4 + 1] = 0x00;
			filter_mask[filter * 4 + 2] = 0x00;
			filter_mask[filter * 4 + 3] = 0x00;
			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
			offset[filter/4] |= 0x0C << ((filter % 4) * 8);
			crc[filter/2] |= smsc_crc(arp, 2, filter);
			filter++;
		}

		if (pdata->wolopts & WAKE_UCAST) {
J
Joe Perches 已提交
1345
			netdev_info(dev->net, "enabling unicast detection\n");
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
			filter_mask[filter * 4] = 0x003F;
			filter_mask[filter * 4 + 1] = 0x00;
			filter_mask[filter * 4 + 2] = 0x00;
			filter_mask[filter * 4 + 3] = 0x00;
			command[filter/4] |= 0x01UL << ((filter % 4) * 8);
			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
			crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
			filter++;
		}

1356
		for (i = 0; i < (wuff_filter_count * 4); i++) {
1357
			ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
1358 1359
			if (ret < 0)
				kfree(filter_mask);
1360
			check_warn_goto_done(ret, "Error writing WUFF\n");
1361
		}
1362
		kfree(filter_mask);
1363

1364
		for (i = 0; i < (wuff_filter_count / 4); i++) {
1365
			ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
1366
			check_warn_goto_done(ret, "Error writing WUFF\n");
1367 1368
		}

1369
		for (i = 0; i < (wuff_filter_count / 4); i++) {
1370
			ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
1371
			check_warn_goto_done(ret, "Error writing WUFF\n");
1372 1373
		}

1374
		for (i = 0; i < (wuff_filter_count / 2); i++) {
1375
			ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
1376
			check_warn_goto_done(ret, "Error writing WUFF\n");
1377 1378 1379
		}

		/* clear any pending pattern match packet status */
1380
		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1381
		check_warn_goto_done(ret, "Error reading WUCSR\n");
1382 1383 1384

		val |= WUCSR_WUFR_;

1385
		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1386
		check_warn_goto_done(ret, "Error writing WUCSR\n");
1387 1388
	}

1389 1390
	if (pdata->wolopts & WAKE_MAGIC) {
		/* clear any pending magic packet status */
1391
		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1392
		check_warn_goto_done(ret, "Error reading WUCSR\n");
1393 1394 1395

		val |= WUCSR_MPR_;

1396
		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1397
		check_warn_goto_done(ret, "Error writing WUCSR\n");
1398 1399
	}

1400
	/* enable/disable wakeup sources */
1401
	ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1402
	check_warn_goto_done(ret, "Error reading WUCSR\n");
1403

1404
	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
J
Joe Perches 已提交
1405
		netdev_info(dev->net, "enabling pattern match wakeup\n");
1406 1407
		val |= WUCSR_WAKE_EN_;
	} else {
J
Joe Perches 已提交
1408
		netdev_info(dev->net, "disabling pattern match wakeup\n");
1409 1410 1411
		val &= ~WUCSR_WAKE_EN_;
	}

1412
	if (pdata->wolopts & WAKE_MAGIC) {
J
Joe Perches 已提交
1413
		netdev_info(dev->net, "enabling magic packet wakeup\n");
1414 1415
		val |= WUCSR_MPEN_;
	} else {
J
Joe Perches 已提交
1416
		netdev_info(dev->net, "disabling magic packet wakeup\n");
1417 1418 1419
		val &= ~WUCSR_MPEN_;
	}

1420
	ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1421
	check_warn_goto_done(ret, "Error writing WUCSR\n");
1422 1423

	/* enable wol wakeup source */
1424
	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1425
	check_warn_goto_done(ret, "Error reading PM_CTRL\n");
1426 1427 1428

	val |= PM_CTL_WOL_EN_;

1429 1430 1431 1432
	/* phy energy detect wakeup source */
	if (pdata->wolopts & WAKE_PHY)
		val |= PM_CTL_ED_EN_;

1433
	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1434
	check_warn_goto_done(ret, "Error writing PM_CTRL\n");
1435

1436
	/* enable receiver to enable frame reception */
1437
	smsc95xx_start_rx_path(dev, 1);
1438 1439

	/* some wol options are enabled, so enter SUSPEND0 */
J
Joe Perches 已提交
1440
	netdev_info(dev->net, "entering SUSPEND0 mode\n");
1441 1442 1443 1444 1445 1446
	ret = smsc95xx_enter_suspend0(dev);

done:
	if (ret)
		usbnet_resume(intf);
	return ret;
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
}

static int smsc95xx_resume(struct usb_interface *intf)
{
	struct usbnet *dev = usb_get_intfdata(intf);
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	int ret;
	u32 val;

	BUG_ON(!dev);

1458
	if (pdata->wolopts) {
1459 1460
		smsc95xx_clear_feature(dev, USB_DEVICE_REMOTE_WAKEUP);

1461
		/* clear wake-up sources */
1462
		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
J
Joe Perches 已提交
1463
		check_warn_return(ret, "Error reading WUCSR\n");
1464

1465
		val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
1466

1467
		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
J
Joe Perches 已提交
1468
		check_warn_return(ret, "Error writing WUCSR\n");
1469 1470

		/* clear wake-up status */
1471
		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
J
Joe Perches 已提交
1472
		check_warn_return(ret, "Error reading PM_CTRL\n");
1473 1474 1475 1476

		val &= ~PM_CTL_WOL_EN_;
		val |= PM_CTL_WUPS_;

1477
		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
J
Joe Perches 已提交
1478
		check_warn_return(ret, "Error writing PM_CTRL\n");
1479 1480
	}

1481
	ret = usbnet_resume(intf);
J
Joe Perches 已提交
1482
	check_warn_return(ret, "usbnet_resume error\n");
1483

1484 1485 1486
	return 0;
}

1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
{
	skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
	skb->ip_summed = CHECKSUM_COMPLETE;
	skb_trim(skb, skb->len - 2);
}

static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
{
	while (skb->len > 0) {
		u32 header, align_count;
		struct sk_buff *ax_skb;
		unsigned char *packet;
		u16 size;

		memcpy(&header, skb->data, sizeof(header));
		le32_to_cpus(&header);
		skb_pull(skb, 4 + NET_IP_ALIGN);
		packet = skb->data;

		/* get the packet length */
		size = (u16)((header & RX_STS_FL_) >> 16);
		align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;

		if (unlikely(header & RX_STS_ES_)) {
1512 1513
			netif_dbg(dev, rx_err, dev->net,
				  "Error header=0x%08x\n", header);
1514 1515
			dev->net->stats.rx_errors++;
			dev->net->stats.rx_dropped++;
1516 1517

			if (header & RX_STS_CRC_) {
1518
				dev->net->stats.rx_crc_errors++;
1519 1520
			} else {
				if (header & (RX_STS_TL_ | RX_STS_RF_))
1521
					dev->net->stats.rx_frame_errors++;
1522 1523 1524

				if ((header & RX_STS_LE_) &&
					(!(header & RX_STS_FT_)))
1525
					dev->net->stats.rx_length_errors++;
1526 1527 1528 1529
			}
		} else {
			/* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
			if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1530 1531
				netif_dbg(dev, rx_err, dev->net,
					  "size err header=0x%08x\n", header);
1532 1533 1534 1535 1536
				return 0;
			}

			/* last frame in this batch */
			if (skb->len == size) {
1537
				if (dev->net->features & NETIF_F_RXCSUM)
1538
					smsc95xx_rx_csum_offload(skb);
1539
				skb_trim(skb, skb->len - 4); /* remove fcs */
1540 1541 1542 1543 1544 1545 1546
				skb->truesize = size + sizeof(struct sk_buff);

				return 1;
			}

			ax_skb = skb_clone(skb, GFP_ATOMIC);
			if (unlikely(!ax_skb)) {
1547
				netdev_warn(dev->net, "Error allocating skb\n");
1548 1549 1550 1551 1552 1553 1554
				return 0;
			}

			ax_skb->len = size;
			ax_skb->data = packet;
			skb_set_tail_pointer(ax_skb, size);

1555
			if (dev->net->features & NETIF_F_RXCSUM)
1556
				smsc95xx_rx_csum_offload(ax_skb);
1557
			skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
			ax_skb->truesize = size + sizeof(struct sk_buff);

			usbnet_skb_return(dev, ax_skb);
		}

		skb_pull(skb, size);

		/* padding bytes before the next frame starts */
		if (skb->len)
			skb_pull(skb, align_count);
	}

	if (unlikely(skb->len < 0)) {
1571
		netdev_warn(dev->net, "invalid rx length<0 %d\n", skb->len);
1572 1573 1574 1575 1576 1577
		return 0;
	}

	return 1;
}

1578 1579
static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
{
1580 1581
	u16 low_16 = (u16)skb_checksum_start_offset(skb);
	u16 high_16 = low_16 + skb->csum_offset;
1582 1583 1584
	return (high_16 << 16) | low_16;
}

1585 1586 1587
static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
					 struct sk_buff *skb, gfp_t flags)
{
1588
	bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
1589
	int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
1590 1591
	u32 tx_cmd_a, tx_cmd_b;

1592 1593 1594 1595
	/* We do not advertise SG, so skbs should be already linearized */
	BUG_ON(skb_shinfo(skb)->nr_frags);

	if (skb_headroom(skb) < overhead) {
1596
		struct sk_buff *skb2 = skb_copy_expand(skb,
1597
			overhead, 0, flags);
1598 1599 1600 1601 1602 1603
		dev_kfree_skb_any(skb);
		skb = skb2;
		if (!skb)
			return NULL;
	}

1604
	if (csum) {
1605 1606 1607
		if (skb->len <= 45) {
			/* workaround - hardware tx checksum does not work
			 * properly with extremely small packets */
1608
			long csstart = skb_checksum_start_offset(skb);
1609 1610 1611 1612 1613 1614 1615 1616 1617
			__wsum calc = csum_partial(skb->data + csstart,
				skb->len - csstart, 0);
			*((__sum16 *)(skb->data + csstart
				+ skb->csum_offset)) = csum_fold(calc);

			csum = false;
		} else {
			u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
			skb_push(skb, 4);
1618
			cpu_to_le32s(&csum_preamble);
1619 1620
			memcpy(skb->data, &csum_preamble, 4);
		}
1621 1622
	}

1623 1624
	skb_push(skb, 4);
	tx_cmd_b = (u32)(skb->len - 4);
1625 1626
	if (csum)
		tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
	cpu_to_le32s(&tx_cmd_b);
	memcpy(skb->data, &tx_cmd_b, 4);

	skb_push(skb, 4);
	tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ |
		TX_CMD_A_LAST_SEG_;
	cpu_to_le32s(&tx_cmd_a);
	memcpy(skb->data, &tx_cmd_a, 4);

	return skb;
}

static const struct driver_info smsc95xx_info = {
	.description	= "smsc95xx USB 2.0 Ethernet",
	.bind		= smsc95xx_bind,
	.unbind		= smsc95xx_unbind,
	.link_reset	= smsc95xx_link_reset,
	.reset		= smsc95xx_reset,
	.rx_fixup	= smsc95xx_rx_fixup,
	.tx_fixup	= smsc95xx_tx_fixup,
	.status		= smsc95xx_status,
1648
	.flags		= FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
1649 1650 1651 1652 1653 1654 1655 1656
};

static const struct usb_device_id products[] = {
	{
		/* SMSC9500 USB Ethernet Device */
		USB_DEVICE(0x0424, 0x9500),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
	{
		/* SMSC9505 USB Ethernet Device */
		USB_DEVICE(0x0424, 0x9505),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9500A USB Ethernet Device */
		USB_DEVICE(0x0424, 0x9E00),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9505A USB Ethernet Device */
		USB_DEVICE(0x0424, 0x9E01),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
1672 1673 1674 1675 1676
	{
		/* SMSC9512/9514 USB Hub & Ethernet Device */
		USB_DEVICE(0x0424, 0xec00),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
	{
		/* SMSC9500 USB Ethernet Device (SAL10) */
		USB_DEVICE(0x0424, 0x9900),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9505 USB Ethernet Device (SAL10) */
		USB_DEVICE(0x0424, 0x9901),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9500A USB Ethernet Device (SAL10) */
		USB_DEVICE(0x0424, 0x9902),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9505A USB Ethernet Device (SAL10) */
		USB_DEVICE(0x0424, 0x9903),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
		USB_DEVICE(0x0424, 0x9904),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9500A USB Ethernet Device (HAL) */
		USB_DEVICE(0x0424, 0x9905),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9505A USB Ethernet Device (HAL) */
		USB_DEVICE(0x0424, 0x9906),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9500 USB Ethernet Device (Alternate ID) */
		USB_DEVICE(0x0424, 0x9907),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9500A USB Ethernet Device (Alternate ID) */
		USB_DEVICE(0x0424, 0x9908),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
		USB_DEVICE(0x0424, 0x9909),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
	{
		/* SMSC LAN9530 USB Ethernet Device */
		USB_DEVICE(0x0424, 0x9530),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC LAN9730 USB Ethernet Device */
		USB_DEVICE(0x0424, 0x9730),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
	{
		/* SMSC LAN89530 USB Ethernet Device */
		USB_DEVICE(0x0424, 0x9E08),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
1742 1743 1744 1745 1746 1747 1748 1749
	{ },		/* END */
};
MODULE_DEVICE_TABLE(usb, products);

static struct usb_driver smsc95xx_driver = {
	.name		= "smsc95xx",
	.id_table	= products,
	.probe		= usbnet_probe,
1750
	.suspend	= smsc95xx_suspend,
1751 1752
	.resume		= smsc95xx_resume,
	.reset_resume	= smsc95xx_resume,
1753
	.disconnect	= usbnet_disconnect,
1754
	.disable_hub_initiated_lpm = 1,
1755 1756
};

1757
module_usb_driver(smsc95xx_driver);
1758 1759

MODULE_AUTHOR("Nancy Lin");
1760
MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
1761 1762
MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
MODULE_LICENSE("GPL");