pegasus.c 38.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 *  Copyright (c) 1999-2005 Petko Manolov (petkan@users.sourceforge.net)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *	ChangeLog:
 *		....	Most of the time spent on reading sources & docs.
 *		v0.2.x	First official release for the Linux kernel.
 *		v0.3.0	Beutified and structured, some bugs fixed.
 *		v0.3.x	URBifying bulk requests and bugfixing. First relatively
 *			stable release. Still can touch device's registers only
 *			from top-halves.
 *		v0.4.0	Control messages remained unurbified are now URBs.
 *			Now we can touch the HW at any time.
 *		v0.4.9	Control urbs again use process context to wait. Argh...
 *			Some long standing bugs (enable_net_traffic) fixed.
 *			Also nasty trick about resubmiting control urb from
 *			interrupt context used. Please let me know how it
 *			behaves. Pegasus II support added since this version.
 *			TODO: suppressing HCD warnings spewage on disconnect.
 *		v0.4.13	Ethernet address is now set at probe(), not at open()
24
 *			time as this seems to break dhcpd.
L
Linus Torvalds 已提交
25 26 27
 *		v0.5.0	branch to 2.5.x kernels
 *		v0.5.1	ethtool support added
 *		v0.5.5	rx socket buffers are in a pool and the their allocation
28
 *			is out of the interrupt routine.
L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 */

#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/mii.h>
#include <linux/usb.h>
#include <linux/module.h>
#include <asm/byteorder.h>
#include <asm/uaccess.h>
#include "pegasus.h"

/*
 * Version Information
 */
48
#define DRIVER_VERSION "v0.6.14 (2006/09/27)"
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56 57
#define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
#define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"

static const char driver_name[] = "pegasus";

#undef	PEGASUS_WRITE_EEPROM
#define	BMSR_MEDIA	(BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \
			BMSR_100FULL | BMSR_ANEGCAPABLE)

58 59 60
static int loopback;
static int mii_mode;
static char *devid;
L
Linus Torvalds 已提交
61 62 63 64

static struct usb_eth_dev usb_dev_id[] = {
#define	PEGASUS_DEV(pn, vid, pid, flags)	\
	{.name = pn, .vendor = vid, .device = pid, .private = flags},
65 66
#define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
	PEGASUS_DEV(pn, vid, pid, flags)
L
Linus Torvalds 已提交
67 68
#include "pegasus.h"
#undef	PEGASUS_DEV
69
#undef	PEGASUS_DEV_CLASS
70
	{NULL, 0, 0, 0},
L
Linus Torvalds 已提交
71 72 73 74 75 76
	{NULL, 0, 0, 0}
};

static struct usb_device_id pegasus_ids[] = {
#define	PEGASUS_DEV(pn, vid, pid, flags) \
	{.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
77 78 79 80 81 82 83 84 85
/*
 * The Belkin F8T012xx1 bluetooth adaptor has the same vendor and product
 * IDs as the Belkin F5D5050, so we need to teach the pegasus driver to
 * ignore adaptors belonging to the "Wireless" class 0xE0. For this one
 * case anyway, seeing as the pegasus is for "Wired" adaptors.
 */
#define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
	{.match_flags = (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_CLASS), \
	.idVendor = vid, .idProduct = pid, .bDeviceClass = dclass},
L
Linus Torvalds 已提交
86 87
#include "pegasus.h"
#undef	PEGASUS_DEV
88
#undef	PEGASUS_DEV_CLASS
89
	{},
L
Linus Torvalds 已提交
90 91 92 93 94 95 96 97
	{}
};

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
module_param(loopback, bool, 0);
module_param(mii_mode, bool, 0);
98
module_param(devid, charp, 0);
L
Linus Torvalds 已提交
99 100
MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
101
MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
L
Linus Torvalds 已提交
102 103 104

/* use ethtool to change the level for any given device */
static int msg_level = -1;
105 106
module_param(msg_level, int, 0);
MODULE_PARM_DESC(msg_level, "Override default message level");
L
Linus Torvalds 已提交
107 108

MODULE_DEVICE_TABLE(usb, pegasus_ids);
109
static const struct net_device_ops pegasus_netdev_ops;
L
Linus Torvalds 已提交
110 111 112

static int update_eth_regs_async(pegasus_t *);
/* Aargh!!! I _really_ hate such tweaks */
113
static void ctrl_callback(struct urb *urb)
L
Linus Torvalds 已提交
114 115
{
	pegasus_t *pegasus = urb->context;
116
	int status = urb->status;
L
Linus Torvalds 已提交
117 118 119 120

	if (!pegasus)
		return;

121
	switch (status) {
L
Linus Torvalds 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134
	case 0:
		if (pegasus->flags & ETH_REGS_CHANGE) {
			pegasus->flags &= ~ETH_REGS_CHANGE;
			pegasus->flags |= ETH_REGS_CHANGED;
			update_eth_regs_async(pegasus);
			return;
		}
		break;
	case -EINPROGRESS:
		return;
	case -ENOENT:
		break;
	default:
135 136 137 138
		if (net_ratelimit())
			netif_dbg(pegasus, drv, pegasus->net,
				  "%s, status %d\n", __func__, status);
		break;
L
Linus Torvalds 已提交
139 140 141 142 143
	}
	pegasus->flags &= ~ETH_REGS_CHANGED;
	wake_up(&pegasus->ctrl_wait);
}

144
static int get_registers(pegasus_t *pegasus, __u16 indx, __u16 size,
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152
			 void *data)
{
	int ret;
	char *buffer;
	DECLARE_WAITQUEUE(wait, current);

	buffer = kmalloc(size, GFP_KERNEL);
	if (!buffer) {
153 154
		netif_warn(pegasus, drv, pegasus->net,
			   "out of memory in %s\n", __func__);
L
Linus Torvalds 已提交
155 156 157 158 159 160 161 162 163 164 165 166
		return -ENOMEM;
	}
	add_wait_queue(&pegasus->ctrl_wait, &wait);
	set_current_state(TASK_UNINTERRUPTIBLE);
	while (pegasus->flags & ETH_REGS_CHANGED)
		schedule();
	remove_wait_queue(&pegasus->ctrl_wait, &wait);
	set_current_state(TASK_RUNNING);

	pegasus->dr.bRequestType = PEGASUS_REQT_READ;
	pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
	pegasus->dr.wValue = cpu_to_le16(0);
167 168
	pegasus->dr.wIndex = cpu_to_le16(indx);
	pegasus->dr.wLength = cpu_to_le16(size);
L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176 177 178 179 180
	pegasus->ctrl_urb->transfer_buffer_length = size;

	usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
			     usb_rcvctrlpipe(pegasus->usb, 0),
			     (char *) &pegasus->dr,
			     buffer, size, ctrl_callback, pegasus);

	add_wait_queue(&pegasus->ctrl_wait, &wait);
	set_current_state(TASK_UNINTERRUPTIBLE);

	/* using ATOMIC, we'd never wake up if we slept */
	if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
181
		set_current_state(TASK_RUNNING);
182 183
		if (ret == -ENODEV)
			netif_device_detach(pegasus->net);
184 185 186
		if (net_ratelimit())
			netif_err(pegasus, drv, pegasus->net,
				  "%s, status %d\n", __func__, ret);
L
Linus Torvalds 已提交
187 188 189 190 191 192 193 194 195 196 197 198
		goto out;
	}

	schedule();
out:
	remove_wait_queue(&pegasus->ctrl_wait, &wait);
	memcpy(data, buffer, size);
	kfree(buffer);

	return ret;
}

199
static int set_registers(pegasus_t *pegasus, __u16 indx, __u16 size,
L
Linus Torvalds 已提交
200 201 202 203 204 205
			 void *data)
{
	int ret;
	char *buffer;
	DECLARE_WAITQUEUE(wait, current);

J
Julia Lawall 已提交
206
	buffer = kmemdup(data, size, GFP_KERNEL);
L
Linus Torvalds 已提交
207
	if (!buffer) {
208 209
		netif_warn(pegasus, drv, pegasus->net,
			   "out of memory in %s\n", __func__);
L
Linus Torvalds 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222
		return -ENOMEM;
	}

	add_wait_queue(&pegasus->ctrl_wait, &wait);
	set_current_state(TASK_UNINTERRUPTIBLE);
	while (pegasus->flags & ETH_REGS_CHANGED)
		schedule();
	remove_wait_queue(&pegasus->ctrl_wait, &wait);
	set_current_state(TASK_RUNNING);

	pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
	pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
	pegasus->dr.wValue = cpu_to_le16(0);
223 224
	pegasus->dr.wIndex = cpu_to_le16(indx);
	pegasus->dr.wLength = cpu_to_le16(size);
L
Linus Torvalds 已提交
225 226 227 228 229 230 231 232 233 234 235
	pegasus->ctrl_urb->transfer_buffer_length = size;

	usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
			     usb_sndctrlpipe(pegasus->usb, 0),
			     (char *) &pegasus->dr,
			     buffer, size, ctrl_callback, pegasus);

	add_wait_queue(&pegasus->ctrl_wait, &wait);
	set_current_state(TASK_UNINTERRUPTIBLE);

	if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
236 237
		if (ret == -ENODEV)
			netif_device_detach(pegasus->net);
238 239
		netif_err(pegasus, drv, pegasus->net,
			  "%s, status %d\n", __func__, ret);
L
Linus Torvalds 已提交
240 241 242 243 244 245 246 247 248 249 250
		goto out;
	}

	schedule();
out:
	remove_wait_queue(&pegasus->ctrl_wait, &wait);
	kfree(buffer);

	return ret;
}

251
static int set_register(pegasus_t *pegasus, __u16 indx, __u8 data)
L
Linus Torvalds 已提交
252 253 254 255 256
{
	int ret;
	char *tmp;
	DECLARE_WAITQUEUE(wait, current);

J
Julia Lawall 已提交
257
	tmp = kmemdup(&data, 1, GFP_KERNEL);
L
Linus Torvalds 已提交
258
	if (!tmp) {
259 260
		netif_warn(pegasus, drv, pegasus->net,
			   "out of memory in %s\n", __func__);
L
Linus Torvalds 已提交
261 262 263 264 265 266 267 268 269 270 271 272
		return -ENOMEM;
	}
	add_wait_queue(&pegasus->ctrl_wait, &wait);
	set_current_state(TASK_UNINTERRUPTIBLE);
	while (pegasus->flags & ETH_REGS_CHANGED)
		schedule();
	remove_wait_queue(&pegasus->ctrl_wait, &wait);
	set_current_state(TASK_RUNNING);

	pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
	pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
	pegasus->dr.wValue = cpu_to_le16(data);
273
	pegasus->dr.wIndex = cpu_to_le16(indx);
L
Linus Torvalds 已提交
274 275 276 277 278 279
	pegasus->dr.wLength = cpu_to_le16(1);
	pegasus->ctrl_urb->transfer_buffer_length = 1;

	usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
			     usb_sndctrlpipe(pegasus->usb, 0),
			     (char *) &pegasus->dr,
280
			     tmp, 1, ctrl_callback, pegasus);
L
Linus Torvalds 已提交
281 282 283 284 285

	add_wait_queue(&pegasus->ctrl_wait, &wait);
	set_current_state(TASK_UNINTERRUPTIBLE);

	if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
286 287
		if (ret == -ENODEV)
			netif_device_detach(pegasus->net);
288 289 290
		if (net_ratelimit())
			netif_err(pegasus, drv, pegasus->net,
				  "%s, status %d\n", __func__, ret);
L
Linus Torvalds 已提交
291 292 293 294 295 296 297 298 299 300 301
		goto out;
	}

	schedule();
out:
	remove_wait_queue(&pegasus->ctrl_wait, &wait);
	kfree(tmp);

	return ret;
}

302
static int update_eth_regs_async(pegasus_t *pegasus)
L
Linus Torvalds 已提交
303 304 305 306 307
{
	int ret;

	pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
	pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
308
	pegasus->dr.wValue = cpu_to_le16(0);
L
Linus Torvalds 已提交
309 310 311 312 313 314 315 316 317
	pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
	pegasus->dr.wLength = cpu_to_le16(3);
	pegasus->ctrl_urb->transfer_buffer_length = 3;

	usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
			     usb_sndctrlpipe(pegasus->usb, 0),
			     (char *) &pegasus->dr,
			     pegasus->eth_regs, 3, ctrl_callback, pegasus);

318 319 320
	if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
		if (ret == -ENODEV)
			netif_device_detach(pegasus->net);
321 322
		netif_err(pegasus, drv, pegasus->net,
			  "%s, status %d\n", __func__, ret);
323
	}
L
Linus Torvalds 已提交
324 325 326 327

	return ret;
}

328
/* Returns 0 on success, error on failure */
329
static int read_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
L
Linus Torvalds 已提交
330 331 332 333 334 335
{
	int i;
	__u8 data[4] = { phy, 0, 0, indx };
	__le16 regdi;
	int ret;

336
	set_register(pegasus, PhyCtrl, 0);
337
	set_registers(pegasus, PhyAddr, sizeof(data), data);
338
	set_register(pegasus, PhyCtrl, (indx | PHY_READ));
L
Linus Torvalds 已提交
339 340
	for (i = 0; i < REG_TIMEOUT; i++) {
		ret = get_registers(pegasus, PhyCtrl, 1, data);
341 342
		if (ret == -ESHUTDOWN)
			goto fail;
L
Linus Torvalds 已提交
343 344 345
		if (data[0] & PHY_DONE)
			break;
	}
346 347 348 349 350 351 352 353

	if (i >= REG_TIMEOUT)
		goto fail;

	ret = get_registers(pegasus, PhyData, 2, &regdi);
	*regd = le16_to_cpu(regdi);
	return ret;

354
fail:
355
	netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
L
Linus Torvalds 已提交
356

357
	return ret;
L
Linus Torvalds 已提交
358 359 360 361 362 363 364 365 366 367 368
}

static int mdio_read(struct net_device *dev, int phy_id, int loc)
{
	pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
	u16 res;

	read_mii_word(pegasus, phy_id, loc, &res);
	return (int)res;
}

369
static int write_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 regd)
L
Linus Torvalds 已提交
370 371 372 373 374 375 376
{
	int i;
	__u8 data[4] = { phy, 0, 0, indx };
	int ret;

	data[1] = (u8) regd;
	data[2] = (u8) (regd >> 8);
377 378 379
	set_register(pegasus, PhyCtrl, 0);
	set_registers(pegasus, PhyAddr, sizeof(data), data);
	set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
L
Linus Torvalds 已提交
380 381
	for (i = 0; i < REG_TIMEOUT; i++) {
		ret = get_registers(pegasus, PhyCtrl, 1, data);
382 383
		if (ret == -ESHUTDOWN)
			goto fail;
L
Linus Torvalds 已提交
384 385 386
		if (data[0] & PHY_DONE)
			break;
	}
387 388 389 390 391

	if (i >= REG_TIMEOUT)
		goto fail;

	return ret;
L
Linus Torvalds 已提交
392

393
fail:
394
	netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
395
	return -ETIMEDOUT;
L
Linus Torvalds 已提交
396 397 398 399 400 401 402 403 404
}

static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
{
	pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);

	write_mii_word(pegasus, phy_id, loc, val);
}

405
static int read_eprom_word(pegasus_t *pegasus, __u8 index, __u16 *retdata)
L
Linus Torvalds 已提交
406 407 408 409 410 411
{
	int i;
	__u8 tmp;
	__le16 retdatai;
	int ret;

412 413 414
	set_register(pegasus, EpromCtrl, 0);
	set_register(pegasus, EpromOffset, index);
	set_register(pegasus, EpromCtrl, EPROM_READ);
L
Linus Torvalds 已提交
415 416 417 418 419

	for (i = 0; i < REG_TIMEOUT; i++) {
		ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
		if (tmp & EPROM_DONE)
			break;
420 421
		if (ret == -ESHUTDOWN)
			goto fail;
L
Linus Torvalds 已提交
422
	}
423 424 425 426 427 428
	if (i >= REG_TIMEOUT)
		goto fail;

	ret = get_registers(pegasus, EpromData, 2, &retdatai);
	*retdata = le16_to_cpu(retdatai);
	return ret;
L
Linus Torvalds 已提交
429

430
fail:
431
	netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
432
	return -ETIMEDOUT;
L
Linus Torvalds 已提交
433 434 435
}

#ifdef	PEGASUS_WRITE_EEPROM
436
static inline void enable_eprom_write(pegasus_t *pegasus)
L
Linus Torvalds 已提交
437 438 439 440
{
	__u8 tmp;
	int ret;

441 442
	get_registers(pegasus, EthCtrl2, 1, &tmp);
	set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
L
Linus Torvalds 已提交
443 444
}

445
static inline void disable_eprom_write(pegasus_t *pegasus)
L
Linus Torvalds 已提交
446 447 448 449
{
	__u8 tmp;
	int ret;

450 451 452
	get_registers(pegasus, EthCtrl2, 1, &tmp);
	set_register(pegasus, EpromCtrl, 0);
	set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
L
Linus Torvalds 已提交
453 454
}

455
static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data)
L
Linus Torvalds 已提交
456 457 458 459
{
	int i;
	__u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
	int ret;
460
	__le16 le_data = cpu_to_le16(data);
L
Linus Torvalds 已提交
461

462
	set_registers(pegasus, EpromOffset, 4, d);
L
Linus Torvalds 已提交
463
	enable_eprom_write(pegasus);
464
	set_register(pegasus, EpromOffset, index);
465
	set_registers(pegasus, EpromData, 2, &le_data);
466
	set_register(pegasus, EpromCtrl, EPROM_WRITE);
L
Linus Torvalds 已提交
467 468 469

	for (i = 0; i < REG_TIMEOUT; i++) {
		ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
470 471
		if (ret == -ESHUTDOWN)
			goto fail;
L
Linus Torvalds 已提交
472 473 474 475
		if (tmp & EPROM_DONE)
			break;
	}
	disable_eprom_write(pegasus);
476 477 478 479 480
	if (i >= REG_TIMEOUT)
		goto fail;

	return ret;

481
fail:
482
	netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
483
	return -ETIMEDOUT;
L
Linus Torvalds 已提交
484 485 486
}
#endif				/* PEGASUS_WRITE_EEPROM */

487
static inline void get_node_id(pegasus_t *pegasus, __u8 *id)
L
Linus Torvalds 已提交
488 489 490 491 492 493
{
	int i;
	__u16 w16;

	for (i = 0; i < 3; i++) {
		read_eprom_word(pegasus, i, &w16);
494
		((__le16 *) id)[i] = cpu_to_le16(w16);
L
Linus Torvalds 已提交
495 496 497
	}
}

498
static void set_ethernet_addr(pegasus_t *pegasus)
L
Linus Torvalds 已提交
499 500 501
{
	__u8 node_id[6];

502 503 504 505
	if (pegasus->features & PEGASUS_II) {
		get_registers(pegasus, 0x10, sizeof(node_id), node_id);
	} else {
		get_node_id(pegasus, node_id);
506
		set_registers(pegasus, EthID, sizeof(node_id), node_id);
507
	}
508
	memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
L
Linus Torvalds 已提交
509 510
}

511
static inline int reset_mac(pegasus_t *pegasus)
L
Linus Torvalds 已提交
512 513 514 515
{
	__u8 data = 0x8;
	int i;

516
	set_register(pegasus, EthCtrl1, data);
L
Linus Torvalds 已提交
517
	for (i = 0; i < REG_TIMEOUT; i++) {
518
		get_registers(pegasus, EthCtrl1, 1, &data);
L
Linus Torvalds 已提交
519 520 521 522
		if (~data & 0x08) {
			if (loopback & 1)
				break;
			if (mii_mode && (pegasus->features & HAS_HOME_PNA))
523
				set_register(pegasus, Gpio1, 0x34);
L
Linus Torvalds 已提交
524
			else
525 526 527
				set_register(pegasus, Gpio1, 0x26);
			set_register(pegasus, Gpio0, pegasus->features);
			set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
L
Linus Torvalds 已提交
528 529 530 531
			break;
		}
	}
	if (i == REG_TIMEOUT)
532
		return -ETIMEDOUT;
L
Linus Torvalds 已提交
533 534 535

	if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
	    usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
536 537
		set_register(pegasus, Gpio0, 0x24);
		set_register(pegasus, Gpio0, 0x26);
L
Linus Torvalds 已提交
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
	}
	if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
		__u16 auxmode;
		read_mii_word(pegasus, 3, 0x1b, &auxmode);
		write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
	}

	return 0;
}

static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
{
	__u16 linkpart;
	__u8 data[4];
	pegasus_t *pegasus = netdev_priv(dev);
	int ret;

	read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
	data[0] = 0xc9;
	data[1] = 0;
	if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
		data[1] |= 0x20;	/* set full duplex */
	if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
		data[1] |= 0x10;	/* set 100 Mbps */
	if (mii_mode)
		data[1] = 0;
	data[2] = (loopback & 1) ? 0x09 : 0x01;

566
	memcpy(pegasus->eth_regs, data, sizeof(data));
L
Linus Torvalds 已提交
567 568 569
	ret = set_registers(pegasus, EthCtrl0, 3, data);

	if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
570
	    usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
L
Linus Torvalds 已提交
571 572 573 574 575 576
	    usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
		u16 auxmode;
		read_mii_word(pegasus, 0, 0x1b, &auxmode);
		write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
	}

577
	return ret;
L
Linus Torvalds 已提交
578 579
}

580
static void fill_skb_pool(pegasus_t *pegasus)
L
Linus Torvalds 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
{
	int i;

	for (i = 0; i < RX_SKBS; i++) {
		if (pegasus->rx_pool[i])
			continue;
		pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
		/*
		 ** we give up if the allocation fail. the tasklet will be
		 ** rescheduled again anyway...
		 */
		if (pegasus->rx_pool[i] == NULL)
			return;
		skb_reserve(pegasus->rx_pool[i], 2);
	}
}

598
static void free_skb_pool(pegasus_t *pegasus)
L
Linus Torvalds 已提交
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
{
	int i;

	for (i = 0; i < RX_SKBS; i++) {
		if (pegasus->rx_pool[i]) {
			dev_kfree_skb(pegasus->rx_pool[i]);
			pegasus->rx_pool[i] = NULL;
		}
	}
}

static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
{
	int i;
	struct sk_buff *skb;

	for (i = 0; i < RX_SKBS; i++) {
		if (likely(pegasus->rx_pool[i] != NULL)) {
			skb = pegasus->rx_pool[i];
			pegasus->rx_pool[i] = NULL;
			return skb;
		}
	}
	return NULL;
}

625
static void read_bulk_callback(struct urb *urb)
L
Linus Torvalds 已提交
626 627 628 629
{
	pegasus_t *pegasus = urb->context;
	struct net_device *net;
	int rx_status, count = urb->actual_length;
630
	int status = urb->status;
L
Linus Torvalds 已提交
631 632 633 634 635 636 637 638 639 640
	u8 *buf = urb->transfer_buffer;
	__u16 pkt_len;

	if (!pegasus)
		return;

	net = pegasus->net;
	if (!netif_device_present(net) || !netif_running(net))
		return;

641
	switch (status) {
L
Linus Torvalds 已提交
642 643
	case 0:
		break;
644
	case -ETIME:
645
		netif_dbg(pegasus, rx_err, net, "reset MAC\n");
L
Linus Torvalds 已提交
646 647 648 649
		pegasus->flags &= ~PEGASUS_RX_BUSY;
		break;
	case -EPIPE:		/* stall, or disconnect from TT */
		/* FIXME schedule work to clear the halt */
650
		netif_warn(pegasus, rx_err, net, "no rx stall recovery\n");
L
Linus Torvalds 已提交
651 652 653 654
		return;
	case -ENOENT:
	case -ECONNRESET:
	case -ESHUTDOWN:
655
		netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status);
L
Linus Torvalds 已提交
656 657
		return;
	default:
658
		netif_dbg(pegasus, rx_err, net, "RX status %d\n", status);
L
Linus Torvalds 已提交
659 660 661 662 663 664 665 666
		goto goon;
	}

	if (!count || count < 4)
		goto goon;

	rx_status = buf[count - 2];
	if (rx_status & 0x1e) {
667 668
		netif_dbg(pegasus, rx_err, net,
			  "RX packet error %x\n", rx_status);
L
Linus Torvalds 已提交
669
		pegasus->stats.rx_errors++;
670
		if (rx_status & 0x06)	/* long or runt	*/
L
Linus Torvalds 已提交
671 672 673
			pegasus->stats.rx_length_errors++;
		if (rx_status & 0x08)
			pegasus->stats.rx_crc_errors++;
674
		if (rx_status & 0x10)	/* extra bits	*/
L
Linus Torvalds 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688
			pegasus->stats.rx_frame_errors++;
		goto goon;
	}
	if (pegasus->chip == 0x8513) {
		pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
		pkt_len &= 0x0fff;
		pegasus->rx_skb->data += 2;
	} else {
		pkt_len = buf[count - 3] << 8;
		pkt_len += buf[count - 4];
		pkt_len &= 0xfff;
		pkt_len -= 8;
	}

K
Kevin Vigor 已提交
689 690 691 692 693 694 695
	/*
	 * If the packet is unreasonably long, quietly drop it rather than
	 * kernel panicing by calling skb_put.
	 */
	if (pkt_len > PEGASUS_MTU)
		goto goon;

L
Linus Torvalds 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
	/*
	 * at this point we are sure pegasus->rx_skb != NULL
	 * so we go ahead and pass up the packet.
	 */
	skb_put(pegasus->rx_skb, pkt_len);
	pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
	netif_rx(pegasus->rx_skb);
	pegasus->stats.rx_packets++;
	pegasus->stats.rx_bytes += pkt_len;

	if (pegasus->flags & PEGASUS_UNPLUG)
		return;

	spin_lock(&pegasus->rx_pool_lock);
	pegasus->rx_skb = pull_skb(pegasus);
	spin_unlock(&pegasus->rx_pool_lock);

	if (pegasus->rx_skb == NULL)
		goto tl_sched;
goon:
	usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
			  usb_rcvbulkpipe(pegasus->usb, 1),
			  pegasus->rx_skb->data, PEGASUS_MTU + 8,
			  read_bulk_callback, pegasus);
720 721 722 723
	rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
	if (rx_status == -ENODEV)
		netif_device_detach(pegasus->net);
	else if (rx_status) {
L
Linus Torvalds 已提交
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
		pegasus->flags |= PEGASUS_RX_URB_FAIL;
		goto tl_sched;
	} else {
		pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
	}

	return;

tl_sched:
	tasklet_schedule(&pegasus->rx_tl);
}

static void rx_fixup(unsigned long data)
{
	pegasus_t *pegasus;
	unsigned long flags;
740
	int status;
L
Linus Torvalds 已提交
741 742 743 744 745 746 747 748 749 750

	pegasus = (pegasus_t *) data;
	if (pegasus->flags & PEGASUS_UNPLUG)
		return;

	spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
	fill_skb_pool(pegasus);
	if (pegasus->flags & PEGASUS_RX_URB_FAIL)
		if (pegasus->rx_skb)
			goto try_again;
751
	if (pegasus->rx_skb == NULL)
L
Linus Torvalds 已提交
752 753
		pegasus->rx_skb = pull_skb(pegasus);
	if (pegasus->rx_skb == NULL) {
754
		netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
L
Linus Torvalds 已提交
755 756 757 758 759 760 761 762
		tasklet_schedule(&pegasus->rx_tl);
		goto done;
	}
	usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
			  usb_rcvbulkpipe(pegasus->usb, 1),
			  pegasus->rx_skb->data, PEGASUS_MTU + 8,
			  read_bulk_callback, pegasus);
try_again:
763 764 765 766
	status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
	if (status == -ENODEV)
		netif_device_detach(pegasus->net);
	else if (status) {
L
Linus Torvalds 已提交
767 768 769 770 771 772 773 774 775
		pegasus->flags |= PEGASUS_RX_URB_FAIL;
		tasklet_schedule(&pegasus->rx_tl);
	} else {
		pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
	}
done:
	spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
}

776
static void write_bulk_callback(struct urb *urb)
L
Linus Torvalds 已提交
777 778
{
	pegasus_t *pegasus = urb->context;
779
	struct net_device *net;
780
	int status = urb->status;
L
Linus Torvalds 已提交
781 782 783 784

	if (!pegasus)
		return;

785 786
	net = pegasus->net;

L
Linus Torvalds 已提交
787 788 789
	if (!netif_device_present(net) || !netif_running(net))
		return;

790
	switch (status) {
L
Linus Torvalds 已提交
791 792 793
	case -EPIPE:
		/* FIXME schedule_work() to clear the tx halt */
		netif_stop_queue(net);
794
		netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
L
Linus Torvalds 已提交
795 796 797 798
		return;
	case -ENOENT:
	case -ECONNRESET:
	case -ESHUTDOWN:
799
		netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
L
Linus Torvalds 已提交
800 801
		return;
	default:
802
		netif_info(pegasus, tx_err, net, "TX status %d\n", status);
L
Linus Torvalds 已提交
803 804 805 806 807
		/* FALL THROUGH */
	case 0:
		break;
	}

E
Eric Dumazet 已提交
808
	net->trans_start = jiffies; /* prevent tx timeout */
L
Linus Torvalds 已提交
809 810 811
	netif_wake_queue(net);
}

812
static void intr_callback(struct urb *urb)
L
Linus Torvalds 已提交
813 814 815
{
	pegasus_t *pegasus = urb->context;
	struct net_device *net;
816
	int res, status = urb->status;
L
Linus Torvalds 已提交
817 818 819 820 821

	if (!pegasus)
		return;
	net = pegasus->net;

822
	switch (status) {
L
Linus Torvalds 已提交
823 824 825 826 827 828 829 830 831 832
	case 0:
		break;
	case -ECONNRESET:	/* unlink */
	case -ENOENT:
	case -ESHUTDOWN:
		return;
	default:
		/* some Pegasus-I products report LOTS of data
		 * toggle errors... avoid log spamming
		 */
833
		netif_dbg(pegasus, timer, net, "intr status %d\n", status);
L
Linus Torvalds 已提交
834 835 836
	}

	if (urb->actual_length >= 6) {
837
		u8 *d = urb->transfer_buffer;
L
Linus Torvalds 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859

		/* byte 0 == tx_status1, reg 2B */
		if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
					|LATE_COL|JABBER_TIMEOUT)) {
			pegasus->stats.tx_errors++;
			if (d[0] & TX_UNDERRUN)
				pegasus->stats.tx_fifo_errors++;
			if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
				pegasus->stats.tx_aborted_errors++;
			if (d[0] & LATE_COL)
				pegasus->stats.tx_window_errors++;
		}

		/* d[5].LINK_STATUS lies on some adapters.
		 * d[0].NO_CARRIER kicks in only with failed TX.
		 * ... so monitoring with MII may be safest.
		 */

		/* bytes 3-4 == rx_lostpkt, reg 2E/2F */
		pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
	}

860 861
	res = usb_submit_urb(urb, GFP_ATOMIC);
	if (res == -ENODEV)
862
		netif_device_detach(pegasus->net);
863 864 865
	if (res)
		netif_err(pegasus, timer, net,
			  "can't resubmit interrupt urb, %d\n", res);
L
Linus Torvalds 已提交
866 867 868 869 870
}

static void pegasus_tx_timeout(struct net_device *net)
{
	pegasus_t *pegasus = netdev_priv(net);
871
	netif_warn(pegasus, timer, net, "tx timeout\n");
L
Linus Torvalds 已提交
872 873 874 875
	usb_unlink_urb(pegasus->tx_urb);
	pegasus->stats.tx_errors++;
}

876 877
static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
					    struct net_device *net)
L
Linus Torvalds 已提交
878 879 880 881 882 883 884 885 886
{
	pegasus_t *pegasus = netdev_priv(net);
	int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
	int res;
	__u16 l16 = skb->len;

	netif_stop_queue(net);

	((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
887
	skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
L
Linus Torvalds 已提交
888 889 890 891 892
	usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
			  usb_sndbulkpipe(pegasus->usb, 2),
			  pegasus->tx_buff, count,
			  write_bulk_callback, pegasus);
	if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
893
		netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
L
Linus Torvalds 已提交
894 895 896 897 898
		switch (res) {
		case -EPIPE:		/* stall, or disconnect from TT */
			/* cleanup should already have been scheduled */
			break;
		case -ENODEV:		/* disconnect() upcoming */
899
		case -EPERM:
900
			netif_device_detach(pegasus->net);
L
Linus Torvalds 已提交
901 902 903 904 905 906 907 908 909 910 911
			break;
		default:
			pegasus->stats.tx_errors++;
			netif_start_queue(net);
		}
	} else {
		pegasus->stats.tx_packets++;
		pegasus->stats.tx_bytes += skb->len;
	}
	dev_kfree_skb(skb);

912
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
913 914 915 916 917 918 919
}

static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
{
	return &((pegasus_t *) netdev_priv(dev))->stats;
}

920
static inline void disable_net_traffic(pegasus_t *pegasus)
L
Linus Torvalds 已提交
921
{
922
	__le16 tmp = cpu_to_le16(0);
L
Linus Torvalds 已提交
923

924
	set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
L
Linus Torvalds 已提交
925 926
}

927
static inline void get_interrupt_interval(pegasus_t *pegasus)
L
Linus Torvalds 已提交
928
{
929 930
	u16 data;
	u8 interval;
L
Linus Torvalds 已提交
931

932 933
	read_eprom_word(pegasus, 4, &data);
	interval = data >> 8;
K
Kevin Vigor 已提交
934
	if (pegasus->usb->speed != USB_SPEED_HIGH) {
935
		if (interval < 0x80) {
936 937 938
			netif_info(pegasus, timer, pegasus->net,
				   "intr interval changed from %ums to %ums\n",
				   interval, 0x80);
939 940
			interval = 0x80;
			data = (data & 0x00FF) | ((u16)interval << 8);
K
Kevin Vigor 已提交
941
#ifdef PEGASUS_WRITE_EEPROM
942
			write_eprom_word(pegasus, 4, data);
L
Linus Torvalds 已提交
943
#endif
K
Kevin Vigor 已提交
944
		}
L
Linus Torvalds 已提交
945
	}
946
	pegasus->intr_interval = interval;
L
Linus Torvalds 已提交
947 948 949 950 951 952 953
}

static void set_carrier(struct net_device *net)
{
	pegasus_t *pegasus = netdev_priv(net);
	u16 tmp;

954
	if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
L
Linus Torvalds 已提交
955
		return;
K
Kevin Vigor 已提交
956

L
Linus Torvalds 已提交
957 958 959 960 961 962
	if (tmp & BMSR_LSTATUS)
		netif_carrier_on(net);
	else
		netif_carrier_off(net);
}

963
static void free_all_urbs(pegasus_t *pegasus)
L
Linus Torvalds 已提交
964 965 966 967 968 969 970
{
	usb_free_urb(pegasus->intr_urb);
	usb_free_urb(pegasus->tx_urb);
	usb_free_urb(pegasus->rx_urb);
	usb_free_urb(pegasus->ctrl_urb);
}

971
static void unlink_all_urbs(pegasus_t *pegasus)
L
Linus Torvalds 已提交
972 973 974 975 976 977 978
{
	usb_kill_urb(pegasus->intr_urb);
	usb_kill_urb(pegasus->tx_urb);
	usb_kill_urb(pegasus->rx_urb);
	usb_kill_urb(pegasus->ctrl_urb);
}

979
static int alloc_urbs(pegasus_t *pegasus)
L
Linus Torvalds 已提交
980 981
{
	pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
982
	if (!pegasus->ctrl_urb)
L
Linus Torvalds 已提交
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
		return 0;
	pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!pegasus->rx_urb) {
		usb_free_urb(pegasus->ctrl_urb);
		return 0;
	}
	pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!pegasus->tx_urb) {
		usb_free_urb(pegasus->rx_urb);
		usb_free_urb(pegasus->ctrl_urb);
		return 0;
	}
	pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!pegasus->intr_urb) {
		usb_free_urb(pegasus->tx_urb);
		usb_free_urb(pegasus->rx_urb);
		usb_free_urb(pegasus->ctrl_urb);
		return 0;
	}

	return 1;
}

static int pegasus_open(struct net_device *net)
{
	pegasus_t *pegasus = netdev_priv(net);
	int res;

	if (pegasus->rx_skb == NULL)
		pegasus->rx_skb = pull_skb(pegasus);
	/*
	 ** Note: no point to free the pool.  it is empty :-)
	 */
	if (!pegasus->rx_skb)
		return -ENOMEM;

	res = set_registers(pegasus, EthID, 6, net->dev_addr);
1020

L
Linus Torvalds 已提交
1021 1022 1023 1024 1025
	usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
			  usb_rcvbulkpipe(pegasus->usb, 1),
			  pegasus->rx_skb->data, PEGASUS_MTU + 8,
			  read_bulk_callback, pegasus);
	if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
1026 1027
		if (res == -ENODEV)
			netif_device_detach(pegasus->net);
1028
		netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
L
Linus Torvalds 已提交
1029 1030 1031 1032 1033
		goto exit;
	}

	usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
			 usb_rcvintpipe(pegasus->usb, 3),
1034
			 pegasus->intr_buff, sizeof(pegasus->intr_buff),
L
Linus Torvalds 已提交
1035 1036
			 intr_callback, pegasus, pegasus->intr_interval);
	if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
1037 1038
		if (res == -ENODEV)
			netif_device_detach(pegasus->net);
1039
		netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
L
Linus Torvalds 已提交
1040 1041 1042 1043
		usb_kill_urb(pegasus->rx_urb);
		goto exit;
	}
	if ((res = enable_net_traffic(net, pegasus->usb))) {
1044 1045
		netif_dbg(pegasus, ifup, net,
			  "can't enable_net_traffic() - %d\n", res);
L
Linus Torvalds 已提交
1046 1047 1048 1049 1050 1051 1052 1053
		res = -EIO;
		usb_kill_urb(pegasus->rx_urb);
		usb_kill_urb(pegasus->intr_urb);
		free_skb_pool(pegasus);
		goto exit;
	}
	set_carrier(net);
	netif_start_queue(net);
1054
	netif_dbg(pegasus, ifup, net, "open\n");
L
Linus Torvalds 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	res = 0;
exit:
	return res;
}

static int pegasus_close(struct net_device *net)
{
	pegasus_t *pegasus = netdev_priv(net);

	netif_stop_queue(net);
	if (!(pegasus->flags & PEGASUS_UNPLUG))
		disable_net_traffic(pegasus);
	tasklet_kill(&pegasus->rx_tl);
	unlink_all_urbs(pegasus);

	return 0;
}

static void pegasus_get_drvinfo(struct net_device *dev,
				struct ethtool_drvinfo *info)
{
	pegasus_t *pegasus = netdev_priv(dev);
1077 1078 1079
	strncpy(info->driver, driver_name, sizeof(info->driver) - 1);
	strncpy(info->version, DRIVER_VERSION, sizeof(info->version) - 1);
	usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info));
L
Linus Torvalds 已提交
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
}

/* also handles three patterns of some kind in hardware */
#define	WOL_SUPPORTED	(WAKE_MAGIC|WAKE_PHY)

static void
pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	pegasus_t	*pegasus = netdev_priv(dev);

	wol->supported = WAKE_MAGIC | WAKE_PHY;
	wol->wolopts = pegasus->wolopts;
}

static int
pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	pegasus_t	*pegasus = netdev_priv(dev);
	u8		reg78 = 0x04;
1099

L
Linus Torvalds 已提交
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
	if (wol->wolopts & ~WOL_SUPPORTED)
		return -EINVAL;

	if (wol->wolopts & WAKE_MAGIC)
		reg78 |= 0x80;
	if (wol->wolopts & WAKE_PHY)
		reg78 |= 0x40;
	/* FIXME this 0x10 bit still needs to get set in the chip... */
	if (wol->wolopts)
		pegasus->eth_regs[0] |= 0x10;
	else
		pegasus->eth_regs[0] &= ~0x10;
	pegasus->wolopts = wol->wolopts;
	return set_register(pegasus, WakeupControl, reg78);
}

static inline void pegasus_reset_wol(struct net_device *dev)
{
	struct ethtool_wolinfo wol;
1119

L
Linus Torvalds 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
	memset(&wol, 0, sizeof wol);
	(void) pegasus_set_wol(dev, &wol);
}

static int
pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
{
	pegasus_t *pegasus;

	pegasus = netdev_priv(dev);
	mii_ethtool_gset(&pegasus->mii, ecmd);
	return 0;
}

static int
pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
{
	pegasus_t *pegasus = netdev_priv(dev);
	return mii_ethtool_sset(&pegasus->mii, ecmd);
}

static int pegasus_nway_reset(struct net_device *dev)
{
	pegasus_t *pegasus = netdev_priv(dev);
	return mii_nway_restart(&pegasus->mii);
}

static u32 pegasus_get_link(struct net_device *dev)
{
	pegasus_t *pegasus = netdev_priv(dev);
	return mii_link_ok(&pegasus->mii);
}

static u32 pegasus_get_msglevel(struct net_device *dev)
{
	pegasus_t *pegasus = netdev_priv(dev);
	return pegasus->msg_enable;
}

static void pegasus_set_msglevel(struct net_device *dev, u32 v)
{
	pegasus_t *pegasus = netdev_priv(dev);
	pegasus->msg_enable = v;
}

1165
static const struct ethtool_ops ops = {
L
Linus Torvalds 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
	.get_drvinfo = pegasus_get_drvinfo,
	.get_settings = pegasus_get_settings,
	.set_settings = pegasus_set_settings,
	.nway_reset = pegasus_nway_reset,
	.get_link = pegasus_get_link,
	.get_msglevel = pegasus_get_msglevel,
	.set_msglevel = pegasus_set_msglevel,
	.get_wol = pegasus_get_wol,
	.set_wol = pegasus_set_wol,
};

static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
{
1179
	__u16 *data = (__u16 *) &rq->ifr_ifru;
L
Linus Torvalds 已提交
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
	pegasus_t *pegasus = netdev_priv(net);
	int res;

	switch (cmd) {
	case SIOCDEVPRIVATE:
		data[0] = pegasus->phy;
	case SIOCDEVPRIVATE + 1:
		read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
		res = 0;
		break;
	case SIOCDEVPRIVATE + 2:
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
		write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
		res = 0;
		break;
	default:
		res = -EOPNOTSUPP;
	}
	return res;
}

static void pegasus_set_multicast(struct net_device *net)
{
	pegasus_t *pegasus = netdev_priv(net);

	if (net->flags & IFF_PROMISC) {
		pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1208
		netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
1209
	} else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
L
Linus Torvalds 已提交
1210 1211
		pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
		pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1212
		netif_dbg(pegasus, link, net, "set allmulti\n");
L
Linus Torvalds 已提交
1213 1214 1215 1216 1217
	} else {
		pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
		pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
	}

1218 1219
	pegasus->ctrl_urb->status = 0;

L
Linus Torvalds 已提交
1220
	pegasus->flags |= ETH_REGS_CHANGE;
1221
	ctrl_callback(pegasus->ctrl_urb);
L
Linus Torvalds 已提交
1222 1223
}

1224
static __u8 mii_phy_probe(pegasus_t *pegasus)
L
Linus Torvalds 已提交
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
{
	int i;
	__u16 tmp;

	for (i = 0; i < 32; i++) {
		read_mii_word(pegasus, i, MII_BMSR, &tmp);
		if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
			continue;
		else
			return i;
	}

	return 0xff;
}

1240
static inline void setup_pegasus_II(pegasus_t *pegasus)
L
Linus Torvalds 已提交
1241 1242
{
	__u8 data = 0xa5;
1243

1244 1245
	set_register(pegasus, Reg1d, 0);
	set_register(pegasus, Reg7b, 1);
L
Linus Torvalds 已提交
1246 1247
	mdelay(100);
	if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1248
		set_register(pegasus, Reg7b, 0);
L
Linus Torvalds 已提交
1249
	else
1250
		set_register(pegasus, Reg7b, 2);
L
Linus Torvalds 已提交
1251

1252 1253
	set_register(pegasus, 0x83, data);
	get_registers(pegasus, 0x83, 1, &data);
L
Linus Torvalds 已提交
1254

1255
	if (data == 0xa5)
L
Linus Torvalds 已提交
1256
		pegasus->chip = 0x8513;
1257
	else
L
Linus Torvalds 已提交
1258 1259
		pegasus->chip = 0;

1260 1261 1262
	set_register(pegasus, 0x80, 0xc0);
	set_register(pegasus, 0x83, 0xff);
	set_register(pegasus, 0x84, 0x01);
1263

L
Linus Torvalds 已提交
1264
	if (pegasus->features & HAS_HOME_PNA && mii_mode)
1265
		set_register(pegasus, Reg81, 6);
L
Linus Torvalds 已提交
1266
	else
1267
		set_register(pegasus, Reg81, 2);
L
Linus Torvalds 已提交
1268 1269 1270
}


D
David Brownell 已提交
1271
static int pegasus_count;
1272
static struct workqueue_struct *pegasus_workqueue;
L
Linus Torvalds 已提交
1273 1274
#define CARRIER_CHECK_DELAY (2 * HZ)

D
David Howells 已提交
1275
static void check_carrier(struct work_struct *work)
L
Linus Torvalds 已提交
1276
{
D
David Howells 已提交
1277
	pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
L
Linus Torvalds 已提交
1278 1279 1280 1281 1282 1283 1284
	set_carrier(pegasus->net);
	if (!(pegasus->flags & PEGASUS_UNPLUG)) {
		queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
			CARRIER_CHECK_DELAY);
	}
}

1285 1286 1287 1288 1289 1290 1291
static int pegasus_blacklisted(struct usb_device *udev)
{
	struct usb_device_descriptor *udd = &udev->descriptor;

	/* Special quirk to keep the driver from handling the Belkin Bluetooth
	 * dongle which happens to have the same ID.
	 */
1292 1293
	if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
	    (udd->idProduct == cpu_to_le16(0x0121)) &&
1294 1295 1296 1297 1298 1299 1300
	    (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
	    (udd->bDeviceProtocol == 1))
		return 1;

	return 0;
}

D
David Brownell 已提交
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
/* we rely on probe() and remove() being serialized so we
 * don't need extra locking on pegasus_count.
 */
static void pegasus_dec_workqueue(void)
{
	pegasus_count--;
	if (pegasus_count == 0) {
		destroy_workqueue(pegasus_workqueue);
		pegasus_workqueue = NULL;
	}
}

L
Linus Torvalds 已提交
1313 1314 1315 1316 1317 1318 1319 1320 1321
static int pegasus_probe(struct usb_interface *intf,
			 const struct usb_device_id *id)
{
	struct usb_device *dev = interface_to_usbdev(intf);
	struct net_device *net;
	pegasus_t *pegasus;
	int dev_index = id - pegasus_ids;
	int res = -ENOMEM;

D
David Brownell 已提交
1322 1323
	if (pegasus_blacklisted(dev))
		return -ENODEV;
1324

D
David Brownell 已提交
1325 1326 1327 1328
	if (pegasus_count == 0) {
		pegasus_workqueue = create_singlethread_workqueue("pegasus");
		if (!pegasus_workqueue)
			return -ENOMEM;
1329
	}
D
David Brownell 已提交
1330 1331 1332
	pegasus_count++;

	usb_get_dev(dev);
1333

L
Linus Torvalds 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
	net = alloc_etherdev(sizeof(struct pegasus));
	if (!net) {
		dev_err(&intf->dev, "can't allocate %s\n", "device");
		goto out;
	}

	pegasus = netdev_priv(net);
	pegasus->dev_index = dev_index;
	init_waitqueue_head(&pegasus->ctrl_wait);

	if (!alloc_urbs(pegasus)) {
		dev_err(&intf->dev, "can't allocate %s\n", "urbs");
		goto out1;
	}

	tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);

D
David Howells 已提交
1351
	INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
L
Linus Torvalds 已提交
1352 1353 1354 1355

	pegasus->intf = intf;
	pegasus->usb = dev;
	pegasus->net = net;
1356 1357


L
Linus Torvalds 已提交
1358
	net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1359
	net->netdev_ops = &pegasus_netdev_ops;
L
Linus Torvalds 已提交
1360 1361 1362 1363 1364 1365 1366
	SET_ETHTOOL_OPS(net, &ops);
	pegasus->mii.dev = net;
	pegasus->mii.mdio_read = mdio_read;
	pegasus->mii.mdio_write = mdio_write;
	pegasus->mii.phy_id_mask = 0x1f;
	pegasus->mii.reg_num_mask = 0x1f;
	spin_lock_init(&pegasus->rx_pool_lock);
1367
	pegasus->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
L
Linus Torvalds 已提交
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
				| NETIF_MSG_PROBE | NETIF_MSG_LINK);

	pegasus->features = usb_dev_id[dev_index].private;
	get_interrupt_interval(pegasus);
	if (reset_mac(pegasus)) {
		dev_err(&intf->dev, "can't reset MAC\n");
		res = -EIO;
		goto out2;
	}
	set_ethernet_addr(pegasus);
	fill_skb_pool(pegasus);
	if (pegasus->features & PEGASUS_II) {
		dev_info(&intf->dev, "setup Pegasus II specific registers\n");
		setup_pegasus_II(pegasus);
	}
	pegasus->phy = mii_phy_probe(pegasus);
	if (pegasus->phy == 0xff) {
		dev_warn(&intf->dev, "can't locate MII phy, using default\n");
		pegasus->phy = 1;
	}
	pegasus->mii.phy_id = pegasus->phy;
	usb_set_intfdata(intf, pegasus);
	SET_NETDEV_DEV(net, &intf->dev);
	pegasus_reset_wol(net);
	res = register_netdev(net);
	if (res)
		goto out3;
	queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
				CARRIER_CHECK_DELAY);

J
Johannes Berg 已提交
1398
	dev_info(&intf->dev, "%s, %s, %pM\n",
1399 1400
		 net->name,
		 usb_dev_id[dev_index].name,
J
Johannes Berg 已提交
1401
		 net->dev_addr);
L
Linus Torvalds 已提交
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
	return 0;

out3:
	usb_set_intfdata(intf, NULL);
	free_skb_pool(pegasus);
out2:
	free_all_urbs(pegasus);
out1:
	free_netdev(net);
out:
	usb_put_dev(dev);
D
David Brownell 已提交
1413
	pegasus_dec_workqueue();
L
Linus Torvalds 已提交
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
	return res;
}

static void pegasus_disconnect(struct usb_interface *intf)
{
	struct pegasus *pegasus = usb_get_intfdata(intf);

	usb_set_intfdata(intf, NULL);
	if (!pegasus) {
		dev_dbg(&intf->dev, "unregistering non-bound device?\n");
		return;
	}

	pegasus->flags |= PEGASUS_UNPLUG;
	cancel_delayed_work(&pegasus->carrier_check);
	unregister_netdev(pegasus->net);
	usb_put_dev(interface_to_usbdev(intf));
K
Kevin Vigor 已提交
1431
	unlink_all_urbs(pegasus);
L
Linus Torvalds 已提交
1432 1433
	free_all_urbs(pegasus);
	free_skb_pool(pegasus);
1434
	if (pegasus->rx_skb != NULL) {
L
Linus Torvalds 已提交
1435
		dev_kfree_skb(pegasus->rx_skb);
1436 1437
		pegasus->rx_skb = NULL;
	}
L
Linus Torvalds 已提交
1438
	free_netdev(pegasus->net);
D
David Brownell 已提交
1439
	pegasus_dec_workqueue();
L
Linus Torvalds 已提交
1440 1441
}

1442
static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
L
Linus Torvalds 已提交
1443 1444
{
	struct pegasus *pegasus = usb_get_intfdata(intf);
1445 1446

	netif_device_detach(pegasus->net);
1447
	cancel_delayed_work(&pegasus->carrier_check);
1448 1449 1450 1451
	if (netif_running(pegasus->net)) {
		usb_kill_urb(pegasus->rx_urb);
		usb_kill_urb(pegasus->intr_urb);
	}
L
Linus Torvalds 已提交
1452 1453 1454
	return 0;
}

1455
static int pegasus_resume(struct usb_interface *intf)
L
Linus Torvalds 已提交
1456 1457 1458
{
	struct pegasus *pegasus = usb_get_intfdata(intf);

1459
	netif_device_attach(pegasus->net);
1460 1461 1462
	if (netif_running(pegasus->net)) {
		pegasus->rx_urb->status = 0;
		pegasus->rx_urb->actual_length = 0;
1463
		read_bulk_callback(pegasus->rx_urb);
1464 1465 1466

		pegasus->intr_urb->status = 0;
		pegasus->intr_urb->actual_length = 0;
1467
		intr_callback(pegasus->intr_urb);
1468
	}
1469 1470
	queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
				CARRIER_CHECK_DELAY);
L
Linus Torvalds 已提交
1471 1472 1473
	return 0;
}

1474 1475 1476 1477 1478 1479 1480 1481
static const struct net_device_ops pegasus_netdev_ops = {
	.ndo_open =			pegasus_open,
	.ndo_stop =			pegasus_close,
	.ndo_do_ioctl =			pegasus_ioctl,
	.ndo_start_xmit =		pegasus_start_xmit,
	.ndo_set_multicast_list =	pegasus_set_multicast,
	.ndo_get_stats =		pegasus_netdev_stats,
	.ndo_tx_timeout =		pegasus_tx_timeout,
1482
	.ndo_change_mtu =		eth_change_mtu,
1483 1484
	.ndo_set_mac_address =		eth_mac_addr,
	.ndo_validate_addr =		eth_validate_addr,
1485 1486
};

L
Linus Torvalds 已提交
1487 1488 1489 1490 1491 1492 1493 1494 1495
static struct usb_driver pegasus_driver = {
	.name = driver_name,
	.probe = pegasus_probe,
	.disconnect = pegasus_disconnect,
	.id_table = pegasus_ids,
	.suspend = pegasus_suspend,
	.resume = pegasus_resume,
};

D
David Brownell 已提交
1496
static void __init parse_id(char *id)
1497
{
1498 1499
	unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0;
	char *token, *name = NULL;
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509

	if ((token = strsep(&id, ":")) != NULL)
		name = token;
	/* name now points to a null terminated string*/
	if ((token = strsep(&id, ":")) != NULL)
		vendor_id = simple_strtoul(token, NULL, 16);
	if ((token = strsep(&id, ":")) != NULL)
		device_id = simple_strtoul(token, NULL, 16);
	flags = simple_strtoul(id, NULL, 16);
	pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1510
		driver_name, name, vendor_id, device_id, flags);
1511 1512 1513 1514 1515 1516

	if (vendor_id > 0x10000 || vendor_id == 0)
		return;
	if (device_id > 0x10000 || device_id == 0)
		return;

1517
	for (i = 0; usb_dev_id[i].name; i++);
1518 1519 1520 1521 1522 1523 1524 1525 1526
	usb_dev_id[i].name = name;
	usb_dev_id[i].vendor = vendor_id;
	usb_dev_id[i].device = device_id;
	usb_dev_id[i].private = flags;
	pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
	pegasus_ids[i].idVendor = vendor_id;
	pegasus_ids[i].idProduct = device_id;
}

L
Linus Torvalds 已提交
1527 1528 1529
static int __init pegasus_init(void)
{
	pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
1530 1531
	if (devid)
		parse_id(devid);
L
Linus Torvalds 已提交
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
	return usb_register(&pegasus_driver);
}

static void __exit pegasus_exit(void)
{
	usb_deregister(&pegasus_driver);
}

module_init(pegasus_init);
module_exit(pegasus_exit);