1/*
2 *  Copyright (c) 1999-2005 Petko Manolov (petkan@users.sourceforge.net)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 *	ChangeLog:
9 *		....	Most of the time spent on reading sources & docs.
10 *		v0.2.x	First official release for the Linux kernel.
11 *		v0.3.0	Beutified and structured, some bugs fixed.
12 *		v0.3.x	URBifying bulk requests and bugfixing. First relatively
13 *			stable release. Still can touch device's registers only
14 *			from top-halves.
15 *		v0.4.0	Control messages remained unurbified are now URBs.
16 *			Now we can touch the HW at any time.
17 *		v0.4.9	Control urbs again use process context to wait. Argh...
18 *			Some long standing bugs (enable_net_traffic) fixed.
19 *			Also nasty trick about resubmiting control urb from
20 *			interrupt context used. Please let me know how it
21 *			behaves. Pegasus II support added since this version.
22 *			TODO: suppressing HCD warnings spewage on disconnect.
23 *		v0.4.13	Ethernet address is now set at probe(), not at open()
24 *			time as this seems to break dhcpd.
25 *		v0.5.0	branch to 2.5.x kernels
26 *		v0.5.1	ethtool support added
27 *		v0.5.5	rx socket buffers are in a pool and the their allocation
28 * 			is out of the interrupt routine.
29 */
30
31#include <linux/sched.h>
32#include <linux/slab.h>
33#include <linux/init.h>
34#include <linux/delay.h>
35#include <linux/netdevice.h>
36#include <linux/etherdevice.h>
37#include <linux/ethtool.h>
38#include <linux/mii.h>
39#include <linux/usb.h>
40#include <linux/module.h>
41#include <asm/byteorder.h>
42#include <asm/uaccess.h>
43#include "pegasus.h"
44
45/*
46 * Version Information
47 */
48#define DRIVER_VERSION "v0.6.14 (2006/09/27)"
49#define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
50#define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
51
52static const char driver_name[] = "pegasus";
53
54#undef	PEGASUS_WRITE_EEPROM
55#define	BMSR_MEDIA	(BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \
56			BMSR_100FULL | BMSR_ANEGCAPABLE)
57
58static int loopback = 0;
59static int mii_mode = 0;
60static char *devid=NULL;
61
62static struct usb_eth_dev usb_dev_id[] = {
63#define	PEGASUS_DEV(pn, vid, pid, flags)	\
64	{.name = pn, .vendor = vid, .device = pid, .private = flags},
65#include "pegasus.h"
66#undef	PEGASUS_DEV
67	{NULL, 0, 0, 0},
68	{NULL, 0, 0, 0}
69};
70
71static struct usb_device_id pegasus_ids[] = {
72#define	PEGASUS_DEV(pn, vid, pid, flags) \
73	{.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
74#include "pegasus.h"
75#undef	PEGASUS_DEV
76	{},
77	{}
78};
79
80MODULE_AUTHOR(DRIVER_AUTHOR);
81MODULE_DESCRIPTION(DRIVER_DESC);
82MODULE_LICENSE("GPL");
83module_param(loopback, bool, 0);
84module_param(mii_mode, bool, 0);
85module_param(devid, charp, 0);
86MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
87MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
88MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
89
90/* use ethtool to change the level for any given device */
91static int msg_level = -1;
92module_param (msg_level, int, 0);
93MODULE_PARM_DESC (msg_level, "Override default message level");
94
95MODULE_DEVICE_TABLE(usb, pegasus_ids);
96
97static int update_eth_regs_async(pegasus_t *);
98/* Aargh!!! I _really_ hate such tweaks */
99static void ctrl_callback(struct urb *urb)
100{
101	pegasus_t *pegasus = urb->context;
102
103	if (!pegasus)
104		return;
105
106	switch (urb->status) {
107	case 0:
108		if (pegasus->flags & ETH_REGS_CHANGE) {
109			pegasus->flags &= ~ETH_REGS_CHANGE;
110			pegasus->flags |= ETH_REGS_CHANGED;
111			update_eth_regs_async(pegasus);
112			return;
113		}
114		break;
115	case -EINPROGRESS:
116		return;
117	case -ENOENT:
118		break;
119	default:
120		if (netif_msg_drv(pegasus))
121			dev_dbg(&pegasus->intf->dev, "%s, status %d\n",
122				__FUNCTION__, urb->status);
123	}
124	pegasus->flags &= ~ETH_REGS_CHANGED;
125	wake_up(&pegasus->ctrl_wait);
126}
127
128static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
129			 void *data)
130{
131	int ret;
132	char *buffer;
133	DECLARE_WAITQUEUE(wait, current);
134
135	buffer = kmalloc(size, GFP_KERNEL);
136	if (!buffer) {
137		if (netif_msg_drv(pegasus))
138			dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
139					__FUNCTION__);
140		return -ENOMEM;
141	}
142	add_wait_queue(&pegasus->ctrl_wait, &wait);
143	set_current_state(TASK_UNINTERRUPTIBLE);
144	while (pegasus->flags & ETH_REGS_CHANGED)
145		schedule();
146	remove_wait_queue(&pegasus->ctrl_wait, &wait);
147	set_current_state(TASK_RUNNING);
148
149	pegasus->dr.bRequestType = PEGASUS_REQT_READ;
150	pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
151	pegasus->dr.wValue = cpu_to_le16(0);
152	pegasus->dr.wIndex = cpu_to_le16p(&indx);
153	pegasus->dr.wLength = cpu_to_le16p(&size);
154	pegasus->ctrl_urb->transfer_buffer_length = size;
155
156	usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
157			     usb_rcvctrlpipe(pegasus->usb, 0),
158			     (char *) &pegasus->dr,
159			     buffer, size, ctrl_callback, pegasus);
160
161	add_wait_queue(&pegasus->ctrl_wait, &wait);
162	set_current_state(TASK_UNINTERRUPTIBLE);
163
164	/* using ATOMIC, we'd never wake up if we slept */
165	if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
166		set_current_state(TASK_RUNNING);
167		if (ret == -ENODEV)
168			netif_device_detach(pegasus->net);
169		if (netif_msg_drv(pegasus))
170			dev_err(&pegasus->intf->dev, "%s, status %d\n",
171					__FUNCTION__, ret);
172		goto out;
173	}
174
175	schedule();
176out:
177	remove_wait_queue(&pegasus->ctrl_wait, &wait);
178	memcpy(data, buffer, size);
179	kfree(buffer);
180
181	return ret;
182}
183
184static int set_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
185			 void *data)
186{
187	int ret;
188	char *buffer;
189	DECLARE_WAITQUEUE(wait, current);
190
191	buffer = kmalloc(size, GFP_KERNEL);
192	if (!buffer) {
193		if (netif_msg_drv(pegasus))
194			dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
195					__FUNCTION__);
196		return -ENOMEM;
197	}
198	memcpy(buffer, data, size);
199
200	add_wait_queue(&pegasus->ctrl_wait, &wait);
201	set_current_state(TASK_UNINTERRUPTIBLE);
202	while (pegasus->flags & ETH_REGS_CHANGED)
203		schedule();
204	remove_wait_queue(&pegasus->ctrl_wait, &wait);
205	set_current_state(TASK_RUNNING);
206
207	pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
208	pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
209	pegasus->dr.wValue = cpu_to_le16(0);
210	pegasus->dr.wIndex = cpu_to_le16p(&indx);
211	pegasus->dr.wLength = cpu_to_le16p(&size);
212	pegasus->ctrl_urb->transfer_buffer_length = size;
213
214	usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
215			     usb_sndctrlpipe(pegasus->usb, 0),
216			     (char *) &pegasus->dr,
217			     buffer, size, ctrl_callback, pegasus);
218
219	add_wait_queue(&pegasus->ctrl_wait, &wait);
220	set_current_state(TASK_UNINTERRUPTIBLE);
221
222	if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
223		if (ret == -ENODEV)
224			netif_device_detach(pegasus->net);
225		if (netif_msg_drv(pegasus))
226			dev_err(&pegasus->intf->dev, "%s, status %d\n",
227					__FUNCTION__, ret);
228		goto out;
229	}
230
231	schedule();
232out:
233	remove_wait_queue(&pegasus->ctrl_wait, &wait);
234	kfree(buffer);
235
236	return ret;
237}
238
239static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
240{
241	int ret;
242	char *tmp;
243	DECLARE_WAITQUEUE(wait, current);
244
245	tmp = kmalloc(1, GFP_KERNEL);
246	if (!tmp) {
247		if (netif_msg_drv(pegasus))
248			dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
249					__FUNCTION__);
250		return -ENOMEM;
251	}
252	memcpy(tmp, &data, 1);
253	add_wait_queue(&pegasus->ctrl_wait, &wait);
254	set_current_state(TASK_UNINTERRUPTIBLE);
255	while (pegasus->flags & ETH_REGS_CHANGED)
256		schedule();
257	remove_wait_queue(&pegasus->ctrl_wait, &wait);
258	set_current_state(TASK_RUNNING);
259
260	pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
261	pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
262	pegasus->dr.wValue = cpu_to_le16(data);
263	pegasus->dr.wIndex = cpu_to_le16p(&indx);
264	pegasus->dr.wLength = cpu_to_le16(1);
265	pegasus->ctrl_urb->transfer_buffer_length = 1;
266
267	usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
268			     usb_sndctrlpipe(pegasus->usb, 0),
269			     (char *) &pegasus->dr,
270			     tmp, 1, ctrl_callback, pegasus);
271
272	add_wait_queue(&pegasus->ctrl_wait, &wait);
273	set_current_state(TASK_UNINTERRUPTIBLE);
274
275	if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
276		if (ret == -ENODEV)
277			netif_device_detach(pegasus->net);
278		if (netif_msg_drv(pegasus))
279			dev_err(&pegasus->intf->dev, "%s, status %d\n",
280					__FUNCTION__, ret);
281		goto out;
282	}
283
284	schedule();
285out:
286	remove_wait_queue(&pegasus->ctrl_wait, &wait);
287	kfree(tmp);
288
289	return ret;
290}
291
292static int update_eth_regs_async(pegasus_t * pegasus)
293{
294	int ret;
295
296	pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
297	pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
298	pegasus->dr.wValue = 0;
299	pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
300	pegasus->dr.wLength = cpu_to_le16(3);
301	pegasus->ctrl_urb->transfer_buffer_length = 3;
302
303	usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
304			     usb_sndctrlpipe(pegasus->usb, 0),
305			     (char *) &pegasus->dr,
306			     pegasus->eth_regs, 3, ctrl_callback, pegasus);
307
308	if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
309		if (ret == -ENODEV)
310			netif_device_detach(pegasus->net);
311		if (netif_msg_drv(pegasus))
312			dev_err(&pegasus->intf->dev, "%s, status %d\n",
313					__FUNCTION__, ret);
314	}
315
316	return ret;
317}
318
319/* Returns 0 on success, error on failure */
320static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
321{
322	int i;
323	__u8 data[4] = { phy, 0, 0, indx };
324	__le16 regdi;
325	int ret;
326
327	set_register(pegasus, PhyCtrl, 0);
328	set_registers(pegasus, PhyAddr, sizeof (data), data);
329	set_register(pegasus, PhyCtrl, (indx | PHY_READ));
330	for (i = 0; i < REG_TIMEOUT; i++) {
331		ret = get_registers(pegasus, PhyCtrl, 1, data);
332		if (ret == -ESHUTDOWN)
333			goto fail;
334		if (data[0] & PHY_DONE)
335			break;
336	}
337	if (i < REG_TIMEOUT) {
338		ret = get_registers(pegasus, PhyData, 2, &regdi);
339		*regd = le16_to_cpu(regdi);
340		return ret;
341	}
342fail:
343	if (netif_msg_drv(pegasus))
344		dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
345
346	return ret;
347}
348
349static int mdio_read(struct net_device *dev, int phy_id, int loc)
350{
351	pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
352	u16 res;
353
354	read_mii_word(pegasus, phy_id, loc, &res);
355	return (int)res;
356}
357
358static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
359{
360	int i;
361	__u8 data[4] = { phy, 0, 0, indx };
362	int ret;
363
364	data[1] = (u8) regd;
365	data[2] = (u8) (regd >> 8);
366	set_register(pegasus, PhyCtrl, 0);
367	set_registers(pegasus, PhyAddr, sizeof(data), data);
368	set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
369	for (i = 0; i < REG_TIMEOUT; i++) {
370		ret = get_registers(pegasus, PhyCtrl, 1, data);
371		if (ret == -ESHUTDOWN)
372			goto fail;
373		if (data[0] & PHY_DONE)
374			break;
375	}
376	if (i < REG_TIMEOUT)
377		return ret;
378
379fail:
380	if (netif_msg_drv(pegasus))
381		dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
382	return -ETIMEDOUT;
383}
384
385static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
386{
387	pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
388
389	write_mii_word(pegasus, phy_id, loc, val);
390}
391
392static int read_eprom_word(pegasus_t * pegasus, __u8 index, __u16 * retdata)
393{
394	int i;
395	__u8 tmp;
396	__le16 retdatai;
397	int ret;
398
399	set_register(pegasus, EpromCtrl, 0);
400	set_register(pegasus, EpromOffset, index);
401	set_register(pegasus, EpromCtrl, EPROM_READ);
402
403	for (i = 0; i < REG_TIMEOUT; i++) {
404		ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
405		if (tmp & EPROM_DONE)
406			break;
407		if (ret == -ESHUTDOWN)
408			goto fail;
409	}
410	if (i < REG_TIMEOUT) {
411		ret = get_registers(pegasus, EpromData, 2, &retdatai);
412		*retdata = le16_to_cpu(retdatai);
413		return ret;
414	}
415
416fail:
417	if (netif_msg_drv(pegasus))
418		dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
419	return -ETIMEDOUT;
420}
421
422#ifdef	PEGASUS_WRITE_EEPROM
423static inline void enable_eprom_write(pegasus_t * pegasus)
424{
425	__u8 tmp;
426	int ret;
427
428	get_registers(pegasus, EthCtrl2, 1, &tmp);
429	set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
430}
431
432static inline void disable_eprom_write(pegasus_t * pegasus)
433{
434	__u8 tmp;
435	int ret;
436
437	get_registers(pegasus, EthCtrl2, 1, &tmp);
438	set_register(pegasus, EpromCtrl, 0);
439	set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
440}
441
442static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
443{
444	int i;
445	__u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
446	int ret;
447
448	set_registers(pegasus, EpromOffset, 4, d);
449	enable_eprom_write(pegasus);
450	set_register(pegasus, EpromOffset, index);
451	set_registers(pegasus, EpromData, 2, &data);
452	set_register(pegasus, EpromCtrl, EPROM_WRITE);
453
454	for (i = 0; i < REG_TIMEOUT; i++) {
455		ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
456		if (ret == -ESHUTDOWN)
457			goto fail;
458		if (tmp & EPROM_DONE)
459			break;
460	}
461	disable_eprom_write(pegasus);
462	if (i < REG_TIMEOUT)
463		return ret;
464fail:
465	if (netif_msg_drv(pegasus))
466		dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
467	return -ETIMEDOUT;
468}
469#endif				/* PEGASUS_WRITE_EEPROM */
470
471static inline void get_node_id(pegasus_t * pegasus, __u8 * id)
472{
473	int i;
474	__u16 w16;
475
476	for (i = 0; i < 3; i++) {
477		read_eprom_word(pegasus, i, &w16);
478		((__le16 *) id)[i] = cpu_to_le16p(&w16);
479	}
480}
481
482static void set_ethernet_addr(pegasus_t * pegasus)
483{
484	__u8 node_id[6];
485
486	if (pegasus->features & PEGASUS_II) {
487		get_registers(pegasus, 0x10, sizeof(node_id), node_id);
488	} else {
489		get_node_id(pegasus, node_id);
490		set_registers(pegasus, EthID, sizeof (node_id), node_id);
491	}
492	memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
493}
494
495static inline int reset_mac(pegasus_t * pegasus)
496{
497	__u8 data = 0x8;
498	int i;
499
500	set_register(pegasus, EthCtrl1, data);
501	for (i = 0; i < REG_TIMEOUT; i++) {
502		get_registers(pegasus, EthCtrl1, 1, &data);
503		if (~data & 0x08) {
504			if (loopback & 1)
505				break;
506			if (mii_mode && (pegasus->features & HAS_HOME_PNA))
507				set_register(pegasus, Gpio1, 0x34);
508			else
509				set_register(pegasus, Gpio1, 0x26);
510			set_register(pegasus, Gpio0, pegasus->features);
511			set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
512			break;
513		}
514	}
515	if (i == REG_TIMEOUT)
516		return -ETIMEDOUT;
517
518	if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
519	    usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
520		set_register(pegasus, Gpio0, 0x24);
521		set_register(pegasus, Gpio0, 0x26);
522	}
523	if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
524		__u16 auxmode;
525		read_mii_word(pegasus, 3, 0x1b, &auxmode);
526		write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
527	}
528
529	return 0;
530}
531
532static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
533{
534	__u16 linkpart;
535	__u8 data[4];
536	pegasus_t *pegasus = netdev_priv(dev);
537	int ret;
538
539	read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
540	data[0] = 0xc9;
541	data[1] = 0;
542	if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
543		data[1] |= 0x20;	/* set full duplex */
544	if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
545		data[1] |= 0x10;	/* set 100 Mbps */
546	if (mii_mode)
547		data[1] = 0;
548	data[2] = (loopback & 1) ? 0x09 : 0x01;
549
550	memcpy(pegasus->eth_regs, data, sizeof (data));
551	ret = set_registers(pegasus, EthCtrl0, 3, data);
552
553	if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
554	    usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
555	    usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
556		u16 auxmode;
557		read_mii_word(pegasus, 0, 0x1b, &auxmode);
558		write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
559	}
560
561	return ret;
562}
563
564static void fill_skb_pool(pegasus_t * pegasus)
565{
566	int i;
567
568	for (i = 0; i < RX_SKBS; i++) {
569		if (pegasus->rx_pool[i])
570			continue;
571		pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
572		/*
573		 ** we give up if the allocation fail. the tasklet will be
574		 ** rescheduled again anyway...
575		 */
576		if (pegasus->rx_pool[i] == NULL)
577			return;
578		skb_reserve(pegasus->rx_pool[i], 2);
579	}
580}
581
582static void free_skb_pool(pegasus_t * pegasus)
583{
584	int i;
585
586	for (i = 0; i < RX_SKBS; i++) {
587		if (pegasus->rx_pool[i]) {
588			dev_kfree_skb(pegasus->rx_pool[i]);
589			pegasus->rx_pool[i] = NULL;
590		}
591	}
592}
593
594static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
595{
596	int i;
597	struct sk_buff *skb;
598
599	for (i = 0; i < RX_SKBS; i++) {
600		if (likely(pegasus->rx_pool[i] != NULL)) {
601			skb = pegasus->rx_pool[i];
602			pegasus->rx_pool[i] = NULL;
603			return skb;
604		}
605	}
606	return NULL;
607}
608
609static void read_bulk_callback(struct urb *urb)
610{
611	pegasus_t *pegasus = urb->context;
612	struct net_device *net;
613	int rx_status, count = urb->actual_length;
614	u8 *buf = urb->transfer_buffer;
615	__u16 pkt_len;
616
617	if (!pegasus)
618		return;
619
620	net = pegasus->net;
621	if (!netif_device_present(net) || !netif_running(net))
622		return;
623
624	switch (urb->status) {
625	case 0:
626		break;
627	case -ETIME:
628		if (netif_msg_rx_err(pegasus))
629			pr_debug("%s: reset MAC\n", net->name);
630		pegasus->flags &= ~PEGASUS_RX_BUSY;
631		break;
632	case -EPIPE:		/* stall, or disconnect from TT */
633		if (netif_msg_rx_err(pegasus))
634			printk(KERN_WARNING "%s: no rx stall recovery\n",
635					net->name);
636		return;
637	case -ENOENT:
638	case -ECONNRESET:
639	case -ESHUTDOWN:
640		if (netif_msg_ifdown(pegasus))
641			pr_debug("%s: rx unlink, %d\n", net->name, urb->status);
642		return;
643	default:
644		if (netif_msg_rx_err(pegasus))
645			pr_debug("%s: RX status %d\n", net->name, urb->status);
646		goto goon;
647	}
648
649	if (!count || count < 4)
650		goto goon;
651
652	rx_status = buf[count - 2];
653	if (rx_status & 0x1e) {
654		if (netif_msg_rx_err(pegasus))
655			pr_debug("%s: RX packet error %x\n",
656					net->name, rx_status);
657		pegasus->stats.rx_errors++;
658		if (rx_status & 0x06)	// long or runt
659			pegasus->stats.rx_length_errors++;
660		if (rx_status & 0x08)
661			pegasus->stats.rx_crc_errors++;
662		if (rx_status & 0x10)	// extra bits
663			pegasus->stats.rx_frame_errors++;
664		goto goon;
665	}
666	if (pegasus->chip == 0x8513) {
667		pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
668		pkt_len &= 0x0fff;
669		pegasus->rx_skb->data += 2;
670	} else {
671		pkt_len = buf[count - 3] << 8;
672		pkt_len += buf[count - 4];
673		pkt_len &= 0xfff;
674		pkt_len -= 8;
675	}
676
677	/*
678	 * If the packet is unreasonably long, quietly drop it rather than
679	 * kernel panicing by calling skb_put.
680	 */
681	if (pkt_len > PEGASUS_MTU)
682		goto goon;
683
684	/*
685	 * at this point we are sure pegasus->rx_skb != NULL
686	 * so we go ahead and pass up the packet.
687	 */
688	skb_put(pegasus->rx_skb, pkt_len);
689	pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
690	netif_rx(pegasus->rx_skb);
691	pegasus->stats.rx_packets++;
692	pegasus->stats.rx_bytes += pkt_len;
693
694	if (pegasus->flags & PEGASUS_UNPLUG)
695		return;
696
697	spin_lock(&pegasus->rx_pool_lock);
698	pegasus->rx_skb = pull_skb(pegasus);
699	spin_unlock(&pegasus->rx_pool_lock);
700
701	if (pegasus->rx_skb == NULL)
702		goto tl_sched;
703goon:
704	usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
705			  usb_rcvbulkpipe(pegasus->usb, 1),
706			  pegasus->rx_skb->data, PEGASUS_MTU + 8,
707			  read_bulk_callback, pegasus);
708	rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
709	if (rx_status == -ENODEV)
710		netif_device_detach(pegasus->net);
711	else if (rx_status) {
712		pegasus->flags |= PEGASUS_RX_URB_FAIL;
713		goto tl_sched;
714	} else {
715		pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
716	}
717
718	return;
719
720tl_sched:
721	tasklet_schedule(&pegasus->rx_tl);
722}
723
724static void rx_fixup(unsigned long data)
725{
726	pegasus_t *pegasus;
727	unsigned long flags;
728	int status;
729
730	pegasus = (pegasus_t *) data;
731	if (pegasus->flags & PEGASUS_UNPLUG)
732		return;
733
734	spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
735	fill_skb_pool(pegasus);
736	if (pegasus->flags & PEGASUS_RX_URB_FAIL)
737		if (pegasus->rx_skb)
738			goto try_again;
739	if (pegasus->rx_skb == NULL) {
740		pegasus->rx_skb = pull_skb(pegasus);
741	}
742	if (pegasus->rx_skb == NULL) {
743		if (netif_msg_rx_err(pegasus))
744			printk(KERN_WARNING "%s: low on memory\n",
745					pegasus->net->name);
746		tasklet_schedule(&pegasus->rx_tl);
747		goto done;
748	}
749	usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
750			  usb_rcvbulkpipe(pegasus->usb, 1),
751			  pegasus->rx_skb->data, PEGASUS_MTU + 8,
752			  read_bulk_callback, pegasus);
753try_again:
754	status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
755	if (status == -ENODEV)
756		netif_device_detach(pegasus->net);
757	else if (status) {
758		pegasus->flags |= PEGASUS_RX_URB_FAIL;
759		tasklet_schedule(&pegasus->rx_tl);
760	} else {
761		pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
762	}
763done:
764	spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
765}
766
767static void write_bulk_callback(struct urb *urb)
768{
769	pegasus_t *pegasus = urb->context;
770	struct net_device *net = pegasus->net;
771
772	if (!pegasus)
773		return;
774
775	if (!netif_device_present(net) || !netif_running(net))
776		return;
777
778	switch (urb->status) {
779	case -EPIPE:
780		netif_stop_queue(net);
781		if (netif_msg_tx_err(pegasus))
782			printk(KERN_WARNING "%s: no tx stall recovery\n",
783					net->name);
784		return;
785	case -ENOENT:
786	case -ECONNRESET:
787	case -ESHUTDOWN:
788		if (netif_msg_ifdown(pegasus))
789			pr_debug("%s: tx unlink, %d\n", net->name, urb->status);
790		return;
791	default:
792		if (netif_msg_tx_err(pegasus))
793			pr_info("%s: TX status %d\n", net->name, urb->status);
794		/* FALL THROUGH */
795	case 0:
796		break;
797	}
798
799	net->trans_start = jiffies;
800	netif_wake_queue(net);
801}
802
803static void intr_callback(struct urb *urb)
804{
805	pegasus_t *pegasus = urb->context;
806	struct net_device *net;
807	int status;
808
809	if (!pegasus)
810		return;
811	net = pegasus->net;
812
813	switch (urb->status) {
814	case 0:
815		break;
816	case -ECONNRESET:	/* unlink */
817	case -ENOENT:
818	case -ESHUTDOWN:
819		return;
820	default:
821		/* some Pegasus-I products report LOTS of data
822		 * toggle errors... avoid log spamming
823		 */
824		if (netif_msg_timer(pegasus))
825			pr_debug("%s: intr status %d\n", net->name,
826					urb->status);
827	}
828
829	if (urb->actual_length >= 6) {
830		u8	* d = urb->transfer_buffer;
831
832		/* byte 0 == tx_status1, reg 2B */
833		if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
834					|LATE_COL|JABBER_TIMEOUT)) {
835			pegasus->stats.tx_errors++;
836			if (d[0] & TX_UNDERRUN)
837				pegasus->stats.tx_fifo_errors++;
838			if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
839				pegasus->stats.tx_aborted_errors++;
840			if (d[0] & LATE_COL)
841				pegasus->stats.tx_window_errors++;
842		}
843
844		/* d[5].LINK_STATUS lies on some adapters.
845		 * d[0].NO_CARRIER kicks in only with failed TX.
846		 * ... so monitoring with MII may be safest.
847		 */
848
849		/* bytes 3-4 == rx_lostpkt, reg 2E/2F */
850		pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
851	}
852
853	status = usb_submit_urb(urb, GFP_ATOMIC);
854	if (status == -ENODEV)
855		netif_device_detach(pegasus->net);
856	if (status && netif_msg_timer(pegasus))
857		printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
858				net->name, status);
859}
860
861static void pegasus_tx_timeout(struct net_device *net)
862{
863	pegasus_t *pegasus = netdev_priv(net);
864	if (netif_msg_timer(pegasus))
865		printk(KERN_WARNING "%s: tx timeout\n", net->name);
866	usb_unlink_urb(pegasus->tx_urb);
867	pegasus->stats.tx_errors++;
868}
869
870static int pegasus_start_xmit(struct sk_buff *skb, struct net_device *net)
871{
872	pegasus_t *pegasus = netdev_priv(net);
873	int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
874	int res;
875	__u16 l16 = skb->len;
876
877	netif_stop_queue(net);
878
879	((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
880	skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
881	usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
882			  usb_sndbulkpipe(pegasus->usb, 2),
883			  pegasus->tx_buff, count,
884			  write_bulk_callback, pegasus);
885	if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
886		if (netif_msg_tx_err(pegasus))
887			printk(KERN_WARNING "%s: fail tx, %d\n",
888					net->name, res);
889		switch (res) {
890		case -EPIPE:		/* stall, or disconnect from TT */
891			/* cleanup should already have been scheduled */
892			break;
893		case -ENODEV:		/* disconnect() upcoming */
894			netif_device_detach(pegasus->net);
895			break;
896		default:
897			pegasus->stats.tx_errors++;
898			netif_start_queue(net);
899		}
900	} else {
901		pegasus->stats.tx_packets++;
902		pegasus->stats.tx_bytes += skb->len;
903		net->trans_start = jiffies;
904	}
905	dev_kfree_skb(skb);
906
907	return 0;
908}
909
910static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
911{
912	return &((pegasus_t *) netdev_priv(dev))->stats;
913}
914
915static inline void disable_net_traffic(pegasus_t * pegasus)
916{
917	int tmp = 0;
918
919	set_registers(pegasus, EthCtrl0, 2, &tmp);
920}
921
922static inline void get_interrupt_interval(pegasus_t * pegasus)
923{
924	__u8 data[2];
925
926	read_eprom_word(pegasus, 4, (__u16 *) data);
927	if (pegasus->usb->speed != USB_SPEED_HIGH) {
928		if (data[1] < 0x80) {
929			if (netif_msg_timer(pegasus))
930				dev_info(&pegasus->intf->dev, "intr interval "
931					"changed from %ums to %ums\n",
932					data[1], 0x80);
933			data[1] = 0x80;
934#ifdef PEGASUS_WRITE_EEPROM
935			write_eprom_word(pegasus, 4, *(__u16 *) data);
936#endif
937		}
938	}
939	pegasus->intr_interval = data[1];
940}
941
942static void set_carrier(struct net_device *net)
943{
944	pegasus_t *pegasus = netdev_priv(net);
945	u16 tmp;
946
947	if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
948		return;
949
950	if (tmp & BMSR_LSTATUS)
951		netif_carrier_on(net);
952	else
953		netif_carrier_off(net);
954}
955
956static void free_all_urbs(pegasus_t * pegasus)
957{
958	usb_free_urb(pegasus->intr_urb);
959	usb_free_urb(pegasus->tx_urb);
960	usb_free_urb(pegasus->rx_urb);
961	usb_free_urb(pegasus->ctrl_urb);
962}
963
964static void unlink_all_urbs(pegasus_t * pegasus)
965{
966	usb_kill_urb(pegasus->intr_urb);
967	usb_kill_urb(pegasus->tx_urb);
968	usb_kill_urb(pegasus->rx_urb);
969	usb_kill_urb(pegasus->ctrl_urb);
970}
971
972static int alloc_urbs(pegasus_t * pegasus)
973{
974	pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
975	if (!pegasus->ctrl_urb) {
976		return 0;
977	}
978	pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
979	if (!pegasus->rx_urb) {
980		usb_free_urb(pegasus->ctrl_urb);
981		return 0;
982	}
983	pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
984	if (!pegasus->tx_urb) {
985		usb_free_urb(pegasus->rx_urb);
986		usb_free_urb(pegasus->ctrl_urb);
987		return 0;
988	}
989	pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
990	if (!pegasus->intr_urb) {
991		usb_free_urb(pegasus->tx_urb);
992		usb_free_urb(pegasus->rx_urb);
993		usb_free_urb(pegasus->ctrl_urb);
994		return 0;
995	}
996
997	return 1;
998}
999
1000static int pegasus_open(struct net_device *net)
1001{
1002	pegasus_t *pegasus = netdev_priv(net);
1003	int res;
1004
1005	if (pegasus->rx_skb == NULL)
1006		pegasus->rx_skb = pull_skb(pegasus);
1007	/*
1008	 ** Note: no point to free the pool.  it is empty :-)
1009	 */
1010	if (!pegasus->rx_skb)
1011		return -ENOMEM;
1012
1013	res = set_registers(pegasus, EthID, 6, net->dev_addr);
1014
1015	usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
1016			  usb_rcvbulkpipe(pegasus->usb, 1),
1017			  pegasus->rx_skb->data, PEGASUS_MTU + 8,
1018			  read_bulk_callback, pegasus);
1019	if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
1020		if (res == -ENODEV)
1021			netif_device_detach(pegasus->net);
1022		if (netif_msg_ifup(pegasus))
1023			pr_debug("%s: failed rx_urb, %d", net->name, res);
1024		goto exit;
1025	}
1026
1027	usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
1028			 usb_rcvintpipe(pegasus->usb, 3),
1029			 pegasus->intr_buff, sizeof (pegasus->intr_buff),
1030			 intr_callback, pegasus, pegasus->intr_interval);
1031	if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
1032		if (res == -ENODEV)
1033			netif_device_detach(pegasus->net);
1034		if (netif_msg_ifup(pegasus))
1035			pr_debug("%s: failed intr_urb, %d\n", net->name, res);
1036		usb_kill_urb(pegasus->rx_urb);
1037		goto exit;
1038	}
1039	if ((res = enable_net_traffic(net, pegasus->usb))) {
1040		if (netif_msg_ifup(pegasus))
1041			pr_debug("%s: can't enable_net_traffic() - %d\n",
1042					net->name, res);
1043		res = -EIO;
1044		usb_kill_urb(pegasus->rx_urb);
1045		usb_kill_urb(pegasus->intr_urb);
1046		free_skb_pool(pegasus);
1047		goto exit;
1048	}
1049	set_carrier(net);
1050	netif_start_queue(net);
1051	if (netif_msg_ifup(pegasus))
1052		pr_debug("%s: open\n", net->name);
1053	res = 0;
1054exit:
1055	return res;
1056}
1057
1058static int pegasus_close(struct net_device *net)
1059{
1060	pegasus_t *pegasus = netdev_priv(net);
1061
1062	netif_stop_queue(net);
1063	if (!(pegasus->flags & PEGASUS_UNPLUG))
1064		disable_net_traffic(pegasus);
1065	tasklet_kill(&pegasus->rx_tl);
1066	unlink_all_urbs(pegasus);
1067
1068	return 0;
1069}
1070
1071static void pegasus_get_drvinfo(struct net_device *dev,
1072				struct ethtool_drvinfo *info)
1073{
1074	pegasus_t *pegasus = netdev_priv(dev);
1075	strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
1076	strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
1077	usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
1078}
1079
1080/* also handles three patterns of some kind in hardware */
1081#define	WOL_SUPPORTED	(WAKE_MAGIC|WAKE_PHY)
1082
1083static void
1084pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1085{
1086	pegasus_t	*pegasus = netdev_priv(dev);
1087
1088	wol->supported = WAKE_MAGIC | WAKE_PHY;
1089	wol->wolopts = pegasus->wolopts;
1090}
1091
1092static int
1093pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1094{
1095	pegasus_t	*pegasus = netdev_priv(dev);
1096	u8		reg78 = 0x04;
1097
1098	if (wol->wolopts & ~WOL_SUPPORTED)
1099		return -EINVAL;
1100
1101	if (wol->wolopts & WAKE_MAGIC)
1102		reg78 |= 0x80;
1103	if (wol->wolopts & WAKE_PHY)
1104		reg78 |= 0x40;
1105	if (wol->wolopts)
1106		pegasus->eth_regs[0] |= 0x10;
1107	else
1108		pegasus->eth_regs[0] &= ~0x10;
1109	pegasus->wolopts = wol->wolopts;
1110	return set_register(pegasus, WakeupControl, reg78);
1111}
1112
1113static inline void pegasus_reset_wol(struct net_device *dev)
1114{
1115	struct ethtool_wolinfo wol;
1116
1117	memset(&wol, 0, sizeof wol);
1118	(void) pegasus_set_wol(dev, &wol);
1119}
1120
1121static int
1122pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1123{
1124	pegasus_t *pegasus;
1125
1126	if (in_atomic())
1127		return 0;
1128
1129	pegasus = netdev_priv(dev);
1130	mii_ethtool_gset(&pegasus->mii, ecmd);
1131
1132	return 0;
1133}
1134
1135static int
1136pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1137{
1138	pegasus_t *pegasus = netdev_priv(dev);
1139	return mii_ethtool_sset(&pegasus->mii, ecmd);
1140}
1141
1142static int pegasus_nway_reset(struct net_device *dev)
1143{
1144	pegasus_t *pegasus = netdev_priv(dev);
1145	return mii_nway_restart(&pegasus->mii);
1146}
1147
1148static u32 pegasus_get_link(struct net_device *dev)
1149{
1150	pegasus_t *pegasus = netdev_priv(dev);
1151	return mii_link_ok(&pegasus->mii);
1152}
1153
1154static u32 pegasus_get_msglevel(struct net_device *dev)
1155{
1156	pegasus_t *pegasus = netdev_priv(dev);
1157	return pegasus->msg_enable;
1158}
1159
1160static void pegasus_set_msglevel(struct net_device *dev, u32 v)
1161{
1162	pegasus_t *pegasus = netdev_priv(dev);
1163	pegasus->msg_enable = v;
1164}
1165
1166static struct ethtool_ops ops = {
1167	.get_drvinfo = pegasus_get_drvinfo,
1168	.get_settings = pegasus_get_settings,
1169	.set_settings = pegasus_set_settings,
1170	.nway_reset = pegasus_nway_reset,
1171	.get_link = pegasus_get_link,
1172	.get_msglevel = pegasus_get_msglevel,
1173	.set_msglevel = pegasus_set_msglevel,
1174	.get_wol = pegasus_get_wol,
1175	.set_wol = pegasus_set_wol,
1176};
1177
1178static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1179{
1180	__u16 *data = (__u16 *) & rq->ifr_ifru;
1181	pegasus_t *pegasus = netdev_priv(net);
1182	int res;
1183
1184	switch (cmd) {
1185	case SIOCDEVPRIVATE:
1186		data[0] = pegasus->phy;
1187	case SIOCDEVPRIVATE + 1:
1188		read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1189		res = 0;
1190		break;
1191	case SIOCDEVPRIVATE + 2:
1192		if (!capable(CAP_NET_ADMIN))
1193			return -EPERM;
1194		write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
1195		res = 0;
1196		break;
1197	default:
1198		res = -EOPNOTSUPP;
1199	}
1200	return res;
1201}
1202
1203static void pegasus_set_multicast(struct net_device *net)
1204{
1205	pegasus_t *pegasus = netdev_priv(net);
1206
1207	if (net->flags & IFF_PROMISC) {
1208		pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1209		if (netif_msg_link(pegasus))
1210			pr_info("%s: Promiscuous mode enabled.\n", net->name);
1211	} else if (net->mc_count ||
1212		   (net->flags & IFF_ALLMULTI)) {
1213		pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1214		pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1215		if (netif_msg_link(pegasus))
1216			pr_info("%s: set allmulti\n", net->name);
1217	} else {
1218		pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1219		pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1220	}
1221
1222	pegasus->flags |= ETH_REGS_CHANGE;
1223	ctrl_callback(pegasus->ctrl_urb);
1224}
1225
1226static __u8 mii_phy_probe(pegasus_t * pegasus)
1227{
1228	int i;
1229	__u16 tmp;
1230
1231	for (i = 0; i < 32; i++) {
1232		read_mii_word(pegasus, i, MII_BMSR, &tmp);
1233		if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1234			continue;
1235		else
1236			return i;
1237	}
1238
1239	return 0xff;
1240}
1241
1242static inline void setup_pegasus_II(pegasus_t * pegasus)
1243{
1244	__u8 data = 0xa5;
1245
1246	set_register(pegasus, Reg1d, 0);
1247	set_register(pegasus, Reg7b, 1);
1248	mdelay(100);
1249	if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1250		set_register(pegasus, Reg7b, 0);
1251	else
1252		set_register(pegasus, Reg7b, 2);
1253
1254	set_register(pegasus, 0x83, data);
1255	get_registers(pegasus, 0x83, 1, &data);
1256
1257	if (data == 0xa5) {
1258		pegasus->chip = 0x8513;
1259	} else {
1260		pegasus->chip = 0;
1261	}
1262
1263	set_register(pegasus, 0x80, 0xc0);
1264	set_register(pegasus, 0x83, 0xff);
1265	set_register(pegasus, 0x84, 0x01);
1266
1267	if (pegasus->features & HAS_HOME_PNA && mii_mode)
1268		set_register(pegasus, Reg81, 6);
1269	else
1270		set_register(pegasus, Reg81, 2);
1271}
1272
1273
1274static struct workqueue_struct *pegasus_workqueue = NULL;
1275#define CARRIER_CHECK_DELAY (2 * HZ)
1276
1277static void check_carrier(struct work_struct *work)
1278{
1279	pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
1280	set_carrier(pegasus->net);
1281	if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1282		queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1283			CARRIER_CHECK_DELAY);
1284	}
1285}
1286
1287static int pegasus_probe(struct usb_interface *intf,
1288			 const struct usb_device_id *id)
1289{
1290	struct usb_device *dev = interface_to_usbdev(intf);
1291	struct net_device *net;
1292	pegasus_t *pegasus;
1293	int dev_index = id - pegasus_ids;
1294	int res = -ENOMEM;
1295
1296	usb_get_dev(dev);
1297	net = alloc_etherdev(sizeof(struct pegasus));
1298	if (!net) {
1299		dev_err(&intf->dev, "can't allocate %s\n", "device");
1300		goto out;
1301	}
1302
1303	pegasus = netdev_priv(net);
1304	memset(pegasus, 0, sizeof (struct pegasus));
1305	pegasus->dev_index = dev_index;
1306	init_waitqueue_head(&pegasus->ctrl_wait);
1307
1308	if (!alloc_urbs(pegasus)) {
1309		dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1310		goto out1;
1311	}
1312
1313	tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1314
1315	INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
1316
1317	pegasus->intf = intf;
1318	pegasus->usb = dev;
1319	pegasus->net = net;
1320	SET_MODULE_OWNER(net);
1321	net->open = pegasus_open;
1322	net->stop = pegasus_close;
1323	net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1324	net->tx_timeout = pegasus_tx_timeout;
1325	net->do_ioctl = pegasus_ioctl;
1326	net->hard_start_xmit = pegasus_start_xmit;
1327	net->set_multicast_list = pegasus_set_multicast;
1328	net->get_stats = pegasus_netdev_stats;
1329	SET_ETHTOOL_OPS(net, &ops);
1330	pegasus->mii.dev = net;
1331	pegasus->mii.mdio_read = mdio_read;
1332	pegasus->mii.mdio_write = mdio_write;
1333	pegasus->mii.phy_id_mask = 0x1f;
1334	pegasus->mii.reg_num_mask = 0x1f;
1335	spin_lock_init(&pegasus->rx_pool_lock);
1336	pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1337				| NETIF_MSG_PROBE | NETIF_MSG_LINK);
1338
1339	pegasus->features = usb_dev_id[dev_index].private;
1340	get_interrupt_interval(pegasus);
1341	if (reset_mac(pegasus)) {
1342		dev_err(&intf->dev, "can't reset MAC\n");
1343		res = -EIO;
1344		goto out2;
1345	}
1346	set_ethernet_addr(pegasus);
1347	fill_skb_pool(pegasus);
1348	if (pegasus->features & PEGASUS_II) {
1349		dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1350		setup_pegasus_II(pegasus);
1351	}
1352	pegasus->phy = mii_phy_probe(pegasus);
1353	if (pegasus->phy == 0xff) {
1354		dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1355		pegasus->phy = 1;
1356	}
1357	pegasus->mii.phy_id = pegasus->phy;
1358	usb_set_intfdata(intf, pegasus);
1359	SET_NETDEV_DEV(net, &intf->dev);
1360	pegasus_reset_wol(net);
1361	res = register_netdev(net);
1362	if (res)
1363		goto out3;
1364	queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1365				CARRIER_CHECK_DELAY);
1366
1367	dev_info(&intf->dev, "%s, %s, %02x:%02x:%02x:%02x:%02x:%02x\n",
1368		net->name,
1369		usb_dev_id[dev_index].name,
1370		net->dev_addr [0], net->dev_addr [1],
1371		net->dev_addr [2], net->dev_addr [3],
1372		net->dev_addr [4], net->dev_addr [5]);
1373	return 0;
1374
1375out3:
1376	usb_set_intfdata(intf, NULL);
1377	free_skb_pool(pegasus);
1378out2:
1379	free_all_urbs(pegasus);
1380out1:
1381	free_netdev(net);
1382out:
1383	usb_put_dev(dev);
1384	return res;
1385}
1386
1387static void pegasus_disconnect(struct usb_interface *intf)
1388{
1389	struct pegasus *pegasus = usb_get_intfdata(intf);
1390
1391	usb_set_intfdata(intf, NULL);
1392	if (!pegasus) {
1393		dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1394		return;
1395	}
1396
1397	pegasus->flags |= PEGASUS_UNPLUG;
1398	cancel_delayed_work(&pegasus->carrier_check);
1399	unregister_netdev(pegasus->net);
1400	usb_put_dev(interface_to_usbdev(intf));
1401	unlink_all_urbs(pegasus);
1402	free_all_urbs(pegasus);
1403	free_skb_pool(pegasus);
1404	if (pegasus->rx_skb != NULL) {
1405		dev_kfree_skb(pegasus->rx_skb);
1406		pegasus->rx_skb = NULL;
1407	}
1408	free_netdev(pegasus->net);
1409}
1410
1411static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
1412{
1413	struct pegasus *pegasus = usb_get_intfdata(intf);
1414
1415	netif_device_detach (pegasus->net);
1416	cancel_delayed_work(&pegasus->carrier_check);
1417	if (netif_running(pegasus->net)) {
1418		usb_kill_urb(pegasus->rx_urb);
1419		usb_kill_urb(pegasus->intr_urb);
1420	}
1421	return 0;
1422}
1423
1424static int pegasus_resume (struct usb_interface *intf)
1425{
1426	struct pegasus *pegasus = usb_get_intfdata(intf);
1427
1428	netif_device_attach (pegasus->net);
1429	if (netif_running(pegasus->net)) {
1430		pegasus->rx_urb->status = 0;
1431		pegasus->rx_urb->actual_length = 0;
1432		read_bulk_callback(pegasus->rx_urb);
1433
1434		pegasus->intr_urb->status = 0;
1435		pegasus->intr_urb->actual_length = 0;
1436		intr_callback(pegasus->intr_urb);
1437	}
1438	queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1439				CARRIER_CHECK_DELAY);
1440	return 0;
1441}
1442
1443static struct usb_driver pegasus_driver = {
1444	.name = driver_name,
1445	.probe = pegasus_probe,
1446	.disconnect = pegasus_disconnect,
1447	.id_table = pegasus_ids,
1448	.suspend = pegasus_suspend,
1449	.resume = pegasus_resume,
1450};
1451
1452static void parse_id(char *id)
1453{
1454	unsigned int vendor_id=0, device_id=0, flags=0, i=0;
1455	char *token, *name=NULL;
1456
1457	if ((token = strsep(&id, ":")) != NULL)
1458		name = token;
1459	/* name now points to a null terminated string*/
1460	if ((token = strsep(&id, ":")) != NULL)
1461		vendor_id = simple_strtoul(token, NULL, 16);
1462	if ((token = strsep(&id, ":")) != NULL)
1463		device_id = simple_strtoul(token, NULL, 16);
1464	flags = simple_strtoul(id, NULL, 16);
1465	pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1466	        driver_name, name, vendor_id, device_id, flags);
1467
1468	if (vendor_id > 0x10000 || vendor_id == 0)
1469		return;
1470	if (device_id > 0x10000 || device_id == 0)
1471		return;
1472
1473	for (i=0; usb_dev_id[i].name; i++);
1474	usb_dev_id[i].name = name;
1475	usb_dev_id[i].vendor = vendor_id;
1476	usb_dev_id[i].device = device_id;
1477	usb_dev_id[i].private = flags;
1478	pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1479	pegasus_ids[i].idVendor = vendor_id;
1480	pegasus_ids[i].idProduct = device_id;
1481}
1482
1483static int __init pegasus_init(void)
1484{
1485	pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
1486	if (devid)
1487		parse_id(devid);
1488	pegasus_workqueue = create_singlethread_workqueue("pegasus");
1489	if (!pegasus_workqueue)
1490		return -ENOMEM;
1491	return usb_register(&pegasus_driver);
1492}
1493
1494static void __exit pegasus_exit(void)
1495{
1496	destroy_workqueue(pegasus_workqueue);
1497	usb_deregister(&pegasus_driver);
1498}
1499
1500module_init(pegasus_init);
1501module_exit(pegasus_exit);
1502