smsc95xx.c 34.7 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 29 30
 /***************************************************************************
 *
 * 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>
#include <linux/crc32.h>
#include <linux/usb/usbnet.h>
31
#include <linux/slab.h>
32 33 34
#include "smsc95xx.h"

#define SMSC_CHIPNAME			"smsc95xx"
35
#define SMSC_DRIVER_VERSION		"1.0.4"
36 37 38 39 40 41 42 43
#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)
44
#define DEFAULT_TX_CSUM_ENABLE		(true)
45 46 47
#define DEFAULT_RX_CSUM_ENABLE		(true)
#define SMSC95XX_INTERNAL_PHY_ID	(1)
#define SMSC95XX_TX_OVERHEAD		(8)
48
#define SMSC95XX_TX_OVERHEAD_CSUM	(12)
49

50 51 52 53 54 55 56 57 58
#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; } })

59 60
struct smsc95xx_priv {
	u32 mac_cr;
61 62
	u32 hash_hi;
	u32 hash_lo;
63 64 65 66 67 68 69 70
	spinlock_t mac_cr_lock;
};

struct usb_context {
	struct usb_ctrlrequest req;
	struct usbnet *dev;
};

71
static bool turbo_mode = true;
72 73 74
module_param(turbo_mode, bool, 0644);
MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");

75 76
static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
					  u32 *data)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
{
	u32 *buf = kmalloc(4, GFP_KERNEL);
	int ret;

	BUG_ON(!dev);

	if (!buf)
		return -ENOMEM;

	ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
		USB_VENDOR_REQUEST_READ_REGISTER,
		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
		00, index, buf, 4, USB_CTRL_GET_TIMEOUT);

	if (unlikely(ret < 0))
92
		netdev_warn(dev->net, "Failed to read register index 0x%08x\n", index);
93 94 95 96 97 98 99 100

	le32_to_cpus(buf);
	*data = *buf;
	kfree(buf);

	return ret;
}

101 102
static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
					   u32 data)
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
{
	u32 *buf = kmalloc(4, GFP_KERNEL);
	int ret;

	BUG_ON(!dev);

	if (!buf)
		return -ENOMEM;

	*buf = data;
	cpu_to_le32s(buf);

	ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
		USB_VENDOR_REQUEST_WRITE_REGISTER,
		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
		00, index, buf, 4, USB_CTRL_SET_TIMEOUT);

	if (unlikely(ret < 0))
121
		netdev_warn(dev->net, "Failed to write register index 0x%08x\n", index);
122 123 124 125 126 127 128 129

	kfree(buf);

	return ret;
}

/* Loop until the read is completed with timeout
 * called with phy_mutex held */
130
static int __must_check smsc95xx_phy_wait_not_busy(struct usbnet *dev)
131 132 133
{
	unsigned long start_time = jiffies;
	u32 val;
134
	int ret;
135 136

	do {
137 138
		ret = smsc95xx_read_reg(dev, MII_ADDR, &val);
		check_warn_return(ret, "Error reading MII_ACCESS");
139 140 141 142 143 144 145 146 147 148 149
		if (!(val & MII_BUSY_))
			return 0;
	} while (!time_after(jiffies, start_time + HZ));

	return -EIO;
}

static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
{
	struct usbnet *dev = netdev_priv(netdev);
	u32 val, addr;
150
	int ret;
151 152 153 154

	mutex_lock(&dev->phy_mutex);

	/* confirm MII not busy */
155 156
	ret = smsc95xx_phy_wait_not_busy(dev);
	check_warn_goto_done(ret, "MII is busy in smsc95xx_mdio_read");
157 158 159 160 161

	/* set the address, index & direction (read from PHY) */
	phy_id &= dev->mii.phy_id_mask;
	idx &= dev->mii.reg_num_mask;
	addr = (phy_id << 11) | (idx << 6) | MII_READ_;
162 163
	ret = smsc95xx_write_reg(dev, MII_ADDR, addr);
	check_warn_goto_done(ret, "Error writing MII_ADDR");
164

165 166
	ret = smsc95xx_phy_wait_not_busy(dev);
	check_warn_goto_done(ret, "Timed out reading MII reg %02X", idx);
167

168 169
	ret = smsc95xx_read_reg(dev, MII_DATA, &val);
	check_warn_goto_done(ret, "Error reading MII_DATA");
170

171
	ret = (u16)(val & 0xFFFF);
172

173 174 175
done:
	mutex_unlock(&dev->phy_mutex);
	return ret;
176 177 178 179 180 181 182
}

static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
				int regval)
{
	struct usbnet *dev = netdev_priv(netdev);
	u32 val, addr;
183
	int ret;
184 185 186 187

	mutex_lock(&dev->phy_mutex);

	/* confirm MII not busy */
188 189
	ret = smsc95xx_phy_wait_not_busy(dev);
	check_warn_goto_done(ret, "MII is busy in smsc95xx_mdio_write");
190 191

	val = regval;
192 193
	ret = smsc95xx_write_reg(dev, MII_DATA, val);
	check_warn_goto_done(ret, "Error writing MII_DATA");
194 195 196 197 198

	/* set the address, index & direction (write to PHY) */
	phy_id &= dev->mii.phy_id_mask;
	idx &= dev->mii.reg_num_mask;
	addr = (phy_id << 11) | (idx << 6) | MII_WRITE_;
199 200
	ret = smsc95xx_write_reg(dev, MII_ADDR, addr);
	check_warn_goto_done(ret, "Error writing MII_ADDR");
201

202 203
	ret = smsc95xx_phy_wait_not_busy(dev);
	check_warn_goto_done(ret, "Timed out writing MII reg %02X", idx);
204

205
done:
206 207 208
	mutex_unlock(&dev->phy_mutex);
}

209
static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
210 211 212
{
	unsigned long start_time = jiffies;
	u32 val;
213
	int ret;
214 215

	do {
216 217
		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
		check_warn_return(ret, "Error reading E2P_CMD");
218 219 220 221 222 223
		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_)) {
224
		netdev_warn(dev->net, "EEPROM read operation timeout\n");
225 226 227 228 229 230
		return -EIO;
	}

	return 0;
}

231
static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
232 233 234
{
	unsigned long start_time = jiffies;
	u32 val;
235
	int ret;
236 237

	do {
238 239
		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
		check_warn_return(ret, "Error reading E2P_CMD");
240 241 242 243 244 245 246

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

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

247
	netdev_warn(dev->net, "EEPROM is busy\n");
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	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_);
266 267
		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
		check_warn_return(ret, "Error writing E2P_CMD");
268 269 270 271 272

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

273 274
		ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
		check_warn_return(ret, "Error reading E2P_DATA");
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

		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_;
298 299
	ret = smsc95xx_write_reg(dev, E2P_CMD, val);
	check_warn_return(ret, "Error writing E2P_DATA");
300 301 302 303 304 305 306 307 308

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

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

		/* Fill data register */
		val = data[i];
309 310
		ret = smsc95xx_write_reg(dev, E2P_DATA, val);
		check_warn_return(ret, "Error writing E2P_DATA");
311 312 313

		/* Send "write" command */
		val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
314 315
		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
		check_warn_return(ret, "Error writing E2P_CMD");
316 317 318 319 320 321 322 323 324 325 326

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

		offset++;
	}

	return 0;
}

327
static void smsc95xx_async_cmd_callback(struct urb *urb)
328 329 330
{
	struct usb_context *usb_context = urb->context;
	struct usbnet *dev = usb_context->dev;
331
	int status = urb->status;
332

333
	check_warn(status, "async callback failed with %d\n", status);
334 335 336 337 338

	kfree(usb_context);
	usb_free_urb(urb);
}

339 340
static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
						 u32 *data)
341 342 343 344
{
	struct usb_context *usb_context;
	int status;
	struct urb *urb;
345
	const u16 size = 4;
346 347 348

	urb = usb_alloc_urb(0, GFP_ATOMIC);
	if (!urb) {
349
		netdev_warn(dev->net, "Error allocating URB\n");
350 351 352 353 354
		return -ENOMEM;
	}

	usb_context = kmalloc(sizeof(struct usb_context), GFP_ATOMIC);
	if (usb_context == NULL) {
355
		netdev_warn(dev->net, "Error allocating control msg\n");
356 357 358 359 360 361 362 363
		usb_free_urb(urb);
		return -ENOMEM;
	}

	usb_context->req.bRequestType =
		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
	usb_context->req.bRequest = USB_VENDOR_REQUEST_WRITE_REGISTER;
	usb_context->req.wValue = 00;
364 365
	usb_context->req.wIndex = cpu_to_le16(index);
	usb_context->req.wLength = cpu_to_le16(size);
366 367 368

	usb_fill_control_urb(urb, dev->udev, usb_sndctrlpipe(dev->udev, 0),
		(void *)&usb_context->req, data, size,
369
		smsc95xx_async_cmd_callback,
370 371 372 373
		(void *)usb_context);

	status = usb_submit_urb(urb, GFP_ATOMIC);
	if (status < 0) {
374 375
		netdev_warn(dev->net, "Error submitting control msg, sts=%d\n",
			    status);
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
		kfree(usb_context);
		usb_free_urb(urb);
	}

	return status;
}

/* 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;
396
	int ret;
397

398 399 400
	pdata->hash_hi = 0;
	pdata->hash_lo = 0;

401 402 403
	spin_lock_irqsave(&pdata->mac_cr_lock, flags);

	if (dev->net->flags & IFF_PROMISC) {
404
		netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
405 406 407
		pdata->mac_cr |= MAC_CR_PRMS_;
		pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
	} else if (dev->net->flags & IFF_ALLMULTI) {
408
		netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
409 410
		pdata->mac_cr |= MAC_CR_MCPAS_;
		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
411
	} else if (!netdev_mc_empty(dev->net)) {
412
		struct netdev_hw_addr *ha;
413 414 415 416

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

417 418
		netdev_for_each_mc_addr(ha, netdev) {
			u32 bitnum = smsc95xx_hash(ha->addr);
419 420
			u32 mask = 0x01 << (bitnum & 0x1F);
			if (bitnum & 0x20)
421
				pdata->hash_hi |= mask;
422
			else
423
				pdata->hash_lo |= mask;
424 425
		}

426
		netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
427
				   pdata->hash_hi, pdata->hash_lo);
428
	} else {
429
		netif_dbg(dev, drv, dev->net, "receive own packets only\n");
430 431 432 433 434 435 436
		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 */
437 438 439 440 441 442 443 444
	ret = smsc95xx_write_reg_async(dev, HASHH, &pdata->hash_hi);
	check_warn(ret, "failed to initiate async write to HASHH");

	ret = smsc95xx_write_reg_async(dev, HASHL, &pdata->hash_lo);
	check_warn(ret, "failed to initiate async write to HASHL");

	ret = smsc95xx_write_reg_async(dev, MAC_CR, &pdata->mac_cr);
	check_warn(ret, "failed to initiate async write to MAC_CR");
445 446
}

447 448
static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
					   u16 lcladv, u16 rmtadv)
449 450 451 452
{
	u32 flow, afc_cfg = 0;

	int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
453
	check_warn_return(ret, "Error reading AFC_CFG");
454 455

	if (duplex == DUPLEX_FULL) {
456
		u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
457 458 459 460 461 462 463 464 465 466 467

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

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

468
		netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
469 470
				   cap & FLOW_CTRL_RX ? "enabled" : "disabled",
				   cap & FLOW_CTRL_TX ? "enabled" : "disabled");
471
	} else {
472
		netif_dbg(dev, link, dev->net, "half duplex\n");
473 474 475 476
		flow = 0;
		afc_cfg |= 0xF;
	}

477 478 479 480 481 482 483
	ret = smsc95xx_write_reg(dev, FLOW, flow);
	check_warn_return(ret, "Error writing FLOW");

	ret = smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
	check_warn_return(ret, "Error writing AFC_CFG");

	return 0;
484 485 486 487 488 489
}

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;
490
	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
491 492
	unsigned long flags;
	u16 lcladv, rmtadv;
493
	int ret;
494 495

	/* clear interrupt status */
496 497 498 499 500
	ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
	check_warn_return(ret, "Error reading PHY_INT_SRC");

	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
	check_warn_return(ret, "Error writing INT_STS");
501 502 503 504 505 506

	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);

507 508 509
	netif_dbg(dev, link, dev->net,
		  "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
		  ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
510 511 512 513 514 515 516 517 518 519 520

	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);

521 522
	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
	check_warn_return(ret, "Error writing MAC_CR");
523

524 525
	ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
	check_warn_return(ret, "Error updating PHY flow control");
526 527 528 529 530 531 532 533 534

	return 0;
}

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

	if (urb->actual_length != 4) {
535 536
		netdev_warn(dev->net, "unexpected urb length %d\n",
			    urb->actual_length);
537 538 539 540
		return;
	}

	memcpy(&intdata, urb->transfer_buffer, 4);
541
	le32_to_cpus(&intdata);
542

543
	netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
544 545 546 547

	if (intdata & INT_ENP_PHY_INT_)
		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
	else
548 549
		netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
			    intdata);
550 551
}

552
/* Enable or disable Tx & Rx checksum offload engines */
553 554
static int smsc95xx_set_features(struct net_device *netdev,
	netdev_features_t features)
555
{
556
	struct usbnet *dev = netdev_priv(netdev);
557
	u32 read_buf;
558 559 560
	int ret;

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

563
	if (features & NETIF_F_HW_CSUM)
564 565 566 567
		read_buf |= Tx_COE_EN_;
	else
		read_buf &= ~Tx_COE_EN_;

568
	if (features & NETIF_F_RXCSUM)
569 570 571 572 573
		read_buf |= Rx_COE_EN_;
	else
		read_buf &= ~Rx_COE_EN_;

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

576
	netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
	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) {
601 602
		netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
			    ee->magic);
603 604 605 606 607 608
		return -EINVAL;
	}

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

609 610 611 612 613 614 615 616 617 618 619
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);
620 621
	unsigned int i, j;
	int retval;
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
	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;
		}
	}
}

639
static const struct ethtool_ops smsc95xx_ethtool_ops = {
640 641 642 643 644 645 646 647 648 649
	.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,
650 651
	.get_regs_len	= smsc95xx_ethtool_getregslen,
	.get_regs	= smsc95xx_ethtool_getregs,
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
};

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 */
671
			netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
672 673 674 675 676
			return;
		}
	}

	/* no eeprom, or eeprom values are invalid. generate random MAC */
677
	eth_hw_addr_random(dev->net);
J
Joe Perches 已提交
678
	netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
679 680 681 682 683 684 685 686 687 688
}

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);
689
	check_warn_return(ret, "Failed to write ADDRL: %d\n", ret);
690 691

	ret = smsc95xx_write_reg(dev, ADDRH, addr_hi);
692
	check_warn_return(ret, "Failed to write ADDRH: %d\n", ret);
693 694 695 696 697

	return 0;
}

/* starts the TX path */
698
static int smsc95xx_start_tx_path(struct usbnet *dev)
699 700 701
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	unsigned long flags;
702
	int ret;
703 704 705 706 707 708

	/* 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);

709 710
	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
	check_warn_return(ret, "Failed to write MAC_CR: %d\n", ret);
711 712

	/* Enable Tx at SCSRs */
713 714 715 716
	ret = smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
	check_warn_return(ret, "Failed to write TX_CFG: %d\n", ret);

	return 0;
717 718 719
}

/* Starts the Receive path */
720
static int smsc95xx_start_rx_path(struct usbnet *dev)
721 722 723
{
	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
	unsigned long flags;
724
	int ret;
725 726 727 728 729

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

730 731 732 733
	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
	check_warn_return(ret, "Failed to write MAC_CR: %d\n", ret);

	return 0;
734 735 736 737
}

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

740 741 742 743 744 745 746 747
	/* 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;

748
	/* reset phy and wait for reset to complete */
749
	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
750 751 752 753 754

	do {
		msleep(10);
		bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
		timeout++;
R
Rabin Vincent 已提交
755
	} while ((bmcr & BMCR_RESET) && (timeout < 100));
756 757 758 759 760 761

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

762 763 764 765 766
	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
		ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
		ADVERTISE_PAUSE_ASYM);

	/* read to clear */
767 768
	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
	check_warn_return(ret, "Failed to read PHY_INT_SRC during init");
769 770 771 772 773

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

774
	netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
775 776 777 778 779 780 781 782 783
	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;

784
	netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
785

786
	ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
787
	check_warn_return(ret, "Failed to write HW_CFG_LRST_ bit in HW_CFG\n");
788 789 790

	timeout = 0;
	do {
791
		msleep(10);
792
		ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
793
		check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
794 795 796 797
		timeout++;
	} while ((read_buf & HW_CFG_LRST_) && (timeout < 100));

	if (timeout >= 100) {
798
		netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
799 800 801
		return ret;
	}

802
	ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
803
	check_warn_return(ret, "Failed to write PM_CTRL: %d\n", ret);
804 805 806

	timeout = 0;
	do {
807
		msleep(10);
808
		ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
809
		check_warn_return(ret, "Failed to read PM_CTRL: %d\n", ret);
810 811 812 813
		timeout++;
	} while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));

	if (timeout >= 100) {
814
		netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
815 816 817 818 819 820 821
		return ret;
	}

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

822 823
	netif_dbg(dev, ifup, dev->net,
		  "MAC Address: %pM\n", dev->net->dev_addr);
824 825

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

828 829
	netif_dbg(dev, ifup, dev->net,
		  "Read Value from HW_CFG : 0x%08x\n", read_buf);
830 831 832 833

	read_buf |= HW_CFG_BIR_;

	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
834
	check_warn_return(ret, "Failed to write HW_CFG_BIR_ bit in HW_CFG\n");
835 836

	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
837
	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
838 839 840
	netif_dbg(dev, ifup, dev->net,
		  "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
		  read_buf);
841 842 843 844 845 846 847 848 849 850 851 852

	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;
	}

853 854
	netif_dbg(dev, ifup, dev->net,
		  "rx_urb_size=%ld\n", (ulong)dev->rx_urb_size);
855 856

	ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
857
	check_warn_return(ret, "Failed to write BURST_CAP: %d\n", ret);
858 859

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

862 863 864
	netif_dbg(dev, ifup, dev->net,
		  "Read Value from BURST_CAP after writing: 0x%08x\n",
		  read_buf);
865

866
	ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
867
	check_warn_return(ret, "Failed to write BULK_IN_DLY: %d\n", ret);
868 869

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

872 873 874
	netif_dbg(dev, ifup, dev->net,
		  "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
		  read_buf);
875 876

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

879 880
	netif_dbg(dev, ifup, dev->net,
		  "Read Value from HW_CFG: 0x%08x\n", read_buf);
881 882 883 884 885 886 887 888 889 890

	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);
891
	check_warn_return(ret, "Failed to write HW_CFG: %d\n", ret);
892 893

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

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

899
	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
900
	check_warn_return(ret, "Failed to write INT_STS: %d\n", ret);
901 902

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

906 907 908 909
	/* 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);
910
	check_warn_return(ret, "Failed to write LED_GPIO_CFG: %d\n", ret);
911

912
	/* Init Tx */
913
	ret = smsc95xx_write_reg(dev, FLOW, 0);
914
	check_warn_return(ret, "Failed to write FLOW: %d\n", ret);
915

916
	ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
917
	check_warn_return(ret, "Failed to write AFC_CFG: %d\n", ret);
918 919 920

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

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

928
	/* Enable or disable checksum offload engines */
929 930
	ret = smsc95xx_set_features(dev->net, dev->net->features);
	check_warn_return(ret, "Failed to set checksum offload features");
931 932 933

	smsc95xx_set_multicast(dev->net);

934 935
	ret = smsc95xx_phy_initialize(dev);
	check_warn_return(ret, "Failed to init PHY");
936 937

	ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
938
	check_warn_return(ret, "Failed to read INT_EP_CTL: %d\n", ret);
939 940 941 942 943

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

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

946 947 948 949 950
	ret = smsc95xx_start_tx_path(dev);
	check_warn_return(ret, "Failed to start TX path");

	ret = smsc95xx_start_rx_path(dev);
	check_warn_return(ret, "Failed to start RX path");
951

952
	netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
953 954 955
	return 0;
}

956 957 958 959 960 961 962 963 964
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,
965
	.ndo_set_rx_mode	= smsc95xx_set_multicast,
966
	.ndo_set_features	= smsc95xx_set_features,
967 968
};

969 970 971 972 973 974 975 976
static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
{
	struct smsc95xx_priv *pdata = NULL;
	int ret;

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

	ret = usbnet_get_endpoints(dev, intf);
977
	check_warn_return(ret, "usbnet_get_endpoints failed: %d\n", ret);
978 979 980 981 982 983

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

	pdata = (struct smsc95xx_priv *)(dev->data[0]);
	if (!pdata) {
984
		netdev_warn(dev->net, "Unable to allocate struct smsc95xx_priv\n");
985 986 987 988 989
		return -ENOMEM;
	}

	spin_lock_init(&pdata->mac_cr_lock);

990 991 992 993 994 995
	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;
996

997 998
	smsc95xx_init_mac_address(dev);

999 1000 1001
	/* Init all registers */
	ret = smsc95xx_reset(dev);

1002
	dev->net->netdev_ops = &smsc95xx_netdev_ops;
1003 1004
	dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
	dev->net->flags |= IFF_MULTICAST;
1005
	dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
S
Stephane Fillod 已提交
1006
	dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1007 1008 1009 1010 1011 1012 1013
	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) {
1014
		netif_dbg(dev, ifdown, dev->net, "free pdata\n");
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
		kfree(pdata);
		pdata = NULL;
		dev->data[0] = 0;
	}
}

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_)) {
1046 1047
			netif_dbg(dev, rx_err, dev->net,
				  "Error header=0x%08x\n", header);
1048 1049
			dev->net->stats.rx_errors++;
			dev->net->stats.rx_dropped++;
1050 1051

			if (header & RX_STS_CRC_) {
1052
				dev->net->stats.rx_crc_errors++;
1053 1054
			} else {
				if (header & (RX_STS_TL_ | RX_STS_RF_))
1055
					dev->net->stats.rx_frame_errors++;
1056 1057 1058

				if ((header & RX_STS_LE_) &&
					(!(header & RX_STS_FT_)))
1059
					dev->net->stats.rx_length_errors++;
1060 1061 1062 1063
			}
		} else {
			/* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
			if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1064 1065
				netif_dbg(dev, rx_err, dev->net,
					  "size err header=0x%08x\n", header);
1066 1067 1068 1069 1070
				return 0;
			}

			/* last frame in this batch */
			if (skb->len == size) {
1071
				if (dev->net->features & NETIF_F_RXCSUM)
1072
					smsc95xx_rx_csum_offload(skb);
1073
				skb_trim(skb, skb->len - 4); /* remove fcs */
1074 1075 1076 1077 1078 1079 1080
				skb->truesize = size + sizeof(struct sk_buff);

				return 1;
			}

			ax_skb = skb_clone(skb, GFP_ATOMIC);
			if (unlikely(!ax_skb)) {
1081
				netdev_warn(dev->net, "Error allocating skb\n");
1082 1083 1084 1085 1086 1087 1088
				return 0;
			}

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

1089
			if (dev->net->features & NETIF_F_RXCSUM)
1090
				smsc95xx_rx_csum_offload(ax_skb);
1091
			skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
			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)) {
1105
		netdev_warn(dev->net, "invalid rx length<0 %d\n", skb->len);
1106 1107 1108 1109 1110 1111
		return 0;
	}

	return 1;
}

1112 1113
static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
{
1114 1115
	u16 low_16 = (u16)skb_checksum_start_offset(skb);
	u16 high_16 = low_16 + skb->csum_offset;
1116 1117 1118
	return (high_16 << 16) | low_16;
}

1119 1120 1121
static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
					 struct sk_buff *skb, gfp_t flags)
{
1122
	bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
1123
	int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
1124 1125
	u32 tx_cmd_a, tx_cmd_b;

1126 1127 1128 1129
	/* We do not advertise SG, so skbs should be already linearized */
	BUG_ON(skb_shinfo(skb)->nr_frags);

	if (skb_headroom(skb) < overhead) {
1130
		struct sk_buff *skb2 = skb_copy_expand(skb,
1131
			overhead, 0, flags);
1132 1133 1134 1135 1136 1137
		dev_kfree_skb_any(skb);
		skb = skb2;
		if (!skb)
			return NULL;
	}

1138
	if (csum) {
1139 1140 1141
		if (skb->len <= 45) {
			/* workaround - hardware tx checksum does not work
			 * properly with extremely small packets */
1142
			long csstart = skb_checksum_start_offset(skb);
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
			__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);
			memcpy(skb->data, &csum_preamble, 4);
		}
1154 1155
	}

1156 1157
	skb_push(skb, 4);
	tx_cmd_b = (u32)(skb->len - 4);
1158 1159
	if (csum)
		tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
	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,
1181
	.flags		= FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
1182 1183 1184 1185 1186 1187 1188 1189
};

static const struct usb_device_id products[] = {
	{
		/* SMSC9500 USB Ethernet Device */
		USB_DEVICE(0x0424, 0x9500),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
	{
		/* 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,
	},
1205 1206 1207 1208 1209
	{
		/* SMSC9512/9514 USB Hub & Ethernet Device */
		USB_DEVICE(0x0424, 0xec00),
		.driver_info = (unsigned long) &smsc95xx_info,
	},
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
	{
		/* 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,
	},
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
	{
		/* 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,
	},
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
	{ },		/* END */
};
MODULE_DEVICE_TABLE(usb, products);

static struct usb_driver smsc95xx_driver = {
	.name		= "smsc95xx",
	.id_table	= products,
	.probe		= usbnet_probe,
	.suspend	= usbnet_suspend,
	.resume		= usbnet_resume,
1285
	.reset_resume	= usbnet_resume,
1286
	.disconnect	= usbnet_disconnect,
1287
	.disable_hub_initiated_lpm = 1,
1288 1289
};

1290
module_usb_driver(smsc95xx_driver);
1291 1292

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