xref: /openbmc/linux/drivers/net/usb/pegasus.c (revision 635ecaa70e862f85f652581305fe0074810893be)
15b2fc499SJeff Garzik /*
25b2fc499SJeff Garzik  *  Copyright (c) 1999-2005 Petko Manolov (petkan@users.sourceforge.net)
35b2fc499SJeff Garzik  *
45b2fc499SJeff Garzik  * This program is free software; you can redistribute it and/or modify
55b2fc499SJeff Garzik  * it under the terms of the GNU General Public License version 2 as
65b2fc499SJeff Garzik  * published by the Free Software Foundation.
75b2fc499SJeff Garzik  *
85b2fc499SJeff Garzik  *	ChangeLog:
95b2fc499SJeff Garzik  *		....	Most of the time spent on reading sources & docs.
105b2fc499SJeff Garzik  *		v0.2.x	First official release for the Linux kernel.
115b2fc499SJeff Garzik  *		v0.3.0	Beutified and structured, some bugs fixed.
125b2fc499SJeff Garzik  *		v0.3.x	URBifying bulk requests and bugfixing. First relatively
135b2fc499SJeff Garzik  *			stable release. Still can touch device's registers only
145b2fc499SJeff Garzik  *			from top-halves.
155b2fc499SJeff Garzik  *		v0.4.0	Control messages remained unurbified are now URBs.
165b2fc499SJeff Garzik  *			Now we can touch the HW at any time.
175b2fc499SJeff Garzik  *		v0.4.9	Control urbs again use process context to wait. Argh...
185b2fc499SJeff Garzik  *			Some long standing bugs (enable_net_traffic) fixed.
195b2fc499SJeff Garzik  *			Also nasty trick about resubmiting control urb from
205b2fc499SJeff Garzik  *			interrupt context used. Please let me know how it
215b2fc499SJeff Garzik  *			behaves. Pegasus II support added since this version.
225b2fc499SJeff Garzik  *			TODO: suppressing HCD warnings spewage on disconnect.
235b2fc499SJeff Garzik  *		v0.4.13	Ethernet address is now set at probe(), not at open()
245b2fc499SJeff Garzik  *			time as this seems to break dhcpd.
255b2fc499SJeff Garzik  *		v0.5.0	branch to 2.5.x kernels
265b2fc499SJeff Garzik  *		v0.5.1	ethtool support added
275b2fc499SJeff Garzik  *		v0.5.5	rx socket buffers are in a pool and the their allocation
285b2fc499SJeff Garzik  * 			is out of the interrupt routine.
295b2fc499SJeff Garzik  */
305b2fc499SJeff Garzik 
315b2fc499SJeff Garzik #include <linux/sched.h>
325b2fc499SJeff Garzik #include <linux/slab.h>
335b2fc499SJeff Garzik #include <linux/init.h>
345b2fc499SJeff Garzik #include <linux/delay.h>
355b2fc499SJeff Garzik #include <linux/netdevice.h>
365b2fc499SJeff Garzik #include <linux/etherdevice.h>
375b2fc499SJeff Garzik #include <linux/ethtool.h>
385b2fc499SJeff Garzik #include <linux/mii.h>
395b2fc499SJeff Garzik #include <linux/usb.h>
405b2fc499SJeff Garzik #include <linux/module.h>
415b2fc499SJeff Garzik #include <asm/byteorder.h>
425b2fc499SJeff Garzik #include <asm/uaccess.h>
435b2fc499SJeff Garzik #include "pegasus.h"
445b2fc499SJeff Garzik 
455b2fc499SJeff Garzik /*
465b2fc499SJeff Garzik  * Version Information
475b2fc499SJeff Garzik  */
485b2fc499SJeff Garzik #define DRIVER_VERSION "v0.6.14 (2006/09/27)"
495b2fc499SJeff Garzik #define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
505b2fc499SJeff Garzik #define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
515b2fc499SJeff Garzik 
525b2fc499SJeff Garzik static const char driver_name[] = "pegasus";
535b2fc499SJeff Garzik 
545b2fc499SJeff Garzik #undef	PEGASUS_WRITE_EEPROM
555b2fc499SJeff Garzik #define	BMSR_MEDIA	(BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \
565b2fc499SJeff Garzik 			BMSR_100FULL | BMSR_ANEGCAPABLE)
575b2fc499SJeff Garzik 
585b2fc499SJeff Garzik static int loopback = 0;
595b2fc499SJeff Garzik static int mii_mode = 0;
605b2fc499SJeff Garzik static char *devid=NULL;
615b2fc499SJeff Garzik 
625b2fc499SJeff Garzik static struct usb_eth_dev usb_dev_id[] = {
635b2fc499SJeff Garzik #define	PEGASUS_DEV(pn, vid, pid, flags)	\
645b2fc499SJeff Garzik 	{.name = pn, .vendor = vid, .device = pid, .private = flags},
655b2fc499SJeff Garzik #include "pegasus.h"
665b2fc499SJeff Garzik #undef	PEGASUS_DEV
675b2fc499SJeff Garzik 	{NULL, 0, 0, 0},
685b2fc499SJeff Garzik 	{NULL, 0, 0, 0}
695b2fc499SJeff Garzik };
705b2fc499SJeff Garzik 
715b2fc499SJeff Garzik static struct usb_device_id pegasus_ids[] = {
725b2fc499SJeff Garzik #define	PEGASUS_DEV(pn, vid, pid, flags) \
735b2fc499SJeff Garzik 	{.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
745b2fc499SJeff Garzik #include "pegasus.h"
755b2fc499SJeff Garzik #undef	PEGASUS_DEV
765b2fc499SJeff Garzik 	{},
775b2fc499SJeff Garzik 	{}
785b2fc499SJeff Garzik };
795b2fc499SJeff Garzik 
805b2fc499SJeff Garzik MODULE_AUTHOR(DRIVER_AUTHOR);
815b2fc499SJeff Garzik MODULE_DESCRIPTION(DRIVER_DESC);
825b2fc499SJeff Garzik MODULE_LICENSE("GPL");
835b2fc499SJeff Garzik module_param(loopback, bool, 0);
845b2fc499SJeff Garzik module_param(mii_mode, bool, 0);
855b2fc499SJeff Garzik module_param(devid, charp, 0);
865b2fc499SJeff Garzik MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
875b2fc499SJeff Garzik MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
885b2fc499SJeff Garzik MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
895b2fc499SJeff Garzik 
905b2fc499SJeff Garzik /* use ethtool to change the level for any given device */
915b2fc499SJeff Garzik static int msg_level = -1;
925b2fc499SJeff Garzik module_param (msg_level, int, 0);
935b2fc499SJeff Garzik MODULE_PARM_DESC (msg_level, "Override default message level");
945b2fc499SJeff Garzik 
955b2fc499SJeff Garzik MODULE_DEVICE_TABLE(usb, pegasus_ids);
968cb89571SOliver Neukum static const struct net_device_ops pegasus_netdev_ops;
975b2fc499SJeff Garzik 
985b2fc499SJeff Garzik static int update_eth_regs_async(pegasus_t *);
995b2fc499SJeff Garzik /* Aargh!!! I _really_ hate such tweaks */
1005b2fc499SJeff Garzik static void ctrl_callback(struct urb *urb)
1015b2fc499SJeff Garzik {
1025b2fc499SJeff Garzik 	pegasus_t *pegasus = urb->context;
103c94cb314SOliver Neukum 	int status = urb->status;
1045b2fc499SJeff Garzik 
1055b2fc499SJeff Garzik 	if (!pegasus)
1065b2fc499SJeff Garzik 		return;
1075b2fc499SJeff Garzik 
108c94cb314SOliver Neukum 	switch (status) {
1095b2fc499SJeff Garzik 	case 0:
1105b2fc499SJeff Garzik 		if (pegasus->flags & ETH_REGS_CHANGE) {
1115b2fc499SJeff Garzik 			pegasus->flags &= ~ETH_REGS_CHANGE;
1125b2fc499SJeff Garzik 			pegasus->flags |= ETH_REGS_CHANGED;
1135b2fc499SJeff Garzik 			update_eth_regs_async(pegasus);
1145b2fc499SJeff Garzik 			return;
1155b2fc499SJeff Garzik 		}
1165b2fc499SJeff Garzik 		break;
1175b2fc499SJeff Garzik 	case -EINPROGRESS:
1185b2fc499SJeff Garzik 		return;
1195b2fc499SJeff Garzik 	case -ENOENT:
1205b2fc499SJeff Garzik 		break;
1215b2fc499SJeff Garzik 	default:
122e000ea13SDavid Brownell 		if (netif_msg_drv(pegasus) && printk_ratelimit())
1235b2fc499SJeff Garzik 			dev_dbg(&pegasus->intf->dev, "%s, status %d\n",
124c94cb314SOliver Neukum 				__func__, status);
1255b2fc499SJeff Garzik 	}
1265b2fc499SJeff Garzik 	pegasus->flags &= ~ETH_REGS_CHANGED;
1275b2fc499SJeff Garzik 	wake_up(&pegasus->ctrl_wait);
1285b2fc499SJeff Garzik }
1295b2fc499SJeff Garzik 
1305b2fc499SJeff Garzik static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
1315b2fc499SJeff Garzik 			 void *data)
1325b2fc499SJeff Garzik {
1335b2fc499SJeff Garzik 	int ret;
1345b2fc499SJeff Garzik 	char *buffer;
1355b2fc499SJeff Garzik 	DECLARE_WAITQUEUE(wait, current);
1365b2fc499SJeff Garzik 
1375b2fc499SJeff Garzik 	buffer = kmalloc(size, GFP_KERNEL);
1385b2fc499SJeff Garzik 	if (!buffer) {
1395b2fc499SJeff Garzik 		if (netif_msg_drv(pegasus))
1405b2fc499SJeff Garzik 			dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
141b39d66a8SHarvey Harrison 					__func__);
1425b2fc499SJeff Garzik 		return -ENOMEM;
1435b2fc499SJeff Garzik 	}
1445b2fc499SJeff Garzik 	add_wait_queue(&pegasus->ctrl_wait, &wait);
1455b2fc499SJeff Garzik 	set_current_state(TASK_UNINTERRUPTIBLE);
1465b2fc499SJeff Garzik 	while (pegasus->flags & ETH_REGS_CHANGED)
1475b2fc499SJeff Garzik 		schedule();
1485b2fc499SJeff Garzik 	remove_wait_queue(&pegasus->ctrl_wait, &wait);
1495b2fc499SJeff Garzik 	set_current_state(TASK_RUNNING);
1505b2fc499SJeff Garzik 
1515b2fc499SJeff Garzik 	pegasus->dr.bRequestType = PEGASUS_REQT_READ;
1525b2fc499SJeff Garzik 	pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
1535b2fc499SJeff Garzik 	pegasus->dr.wValue = cpu_to_le16(0);
154da2bbdccSHarvey Harrison 	pegasus->dr.wIndex = cpu_to_le16(indx);
155da2bbdccSHarvey Harrison 	pegasus->dr.wLength = cpu_to_le16(size);
1565b2fc499SJeff Garzik 	pegasus->ctrl_urb->transfer_buffer_length = size;
1575b2fc499SJeff Garzik 
1585b2fc499SJeff Garzik 	usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
1595b2fc499SJeff Garzik 			     usb_rcvctrlpipe(pegasus->usb, 0),
1605b2fc499SJeff Garzik 			     (char *) &pegasus->dr,
1615b2fc499SJeff Garzik 			     buffer, size, ctrl_callback, pegasus);
1625b2fc499SJeff Garzik 
1635b2fc499SJeff Garzik 	add_wait_queue(&pegasus->ctrl_wait, &wait);
1645b2fc499SJeff Garzik 	set_current_state(TASK_UNINTERRUPTIBLE);
1655b2fc499SJeff Garzik 
1665b2fc499SJeff Garzik 	/* using ATOMIC, we'd never wake up if we slept */
1675b2fc499SJeff Garzik 	if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
1685b2fc499SJeff Garzik 		set_current_state(TASK_RUNNING);
1695b2fc499SJeff Garzik 		if (ret == -ENODEV)
1705b2fc499SJeff Garzik 			netif_device_detach(pegasus->net);
171e000ea13SDavid Brownell 		if (netif_msg_drv(pegasus) && printk_ratelimit())
1725b2fc499SJeff Garzik 			dev_err(&pegasus->intf->dev, "%s, status %d\n",
173653c0316SHarvey Harrison 					__func__, ret);
1745b2fc499SJeff Garzik 		goto out;
1755b2fc499SJeff Garzik 	}
1765b2fc499SJeff Garzik 
1775b2fc499SJeff Garzik 	schedule();
1785b2fc499SJeff Garzik out:
1795b2fc499SJeff Garzik 	remove_wait_queue(&pegasus->ctrl_wait, &wait);
1805b2fc499SJeff Garzik 	memcpy(data, buffer, size);
1815b2fc499SJeff Garzik 	kfree(buffer);
1825b2fc499SJeff Garzik 
1835b2fc499SJeff Garzik 	return ret;
1845b2fc499SJeff Garzik }
1855b2fc499SJeff Garzik 
1865b2fc499SJeff Garzik static int set_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
1875b2fc499SJeff Garzik 			 void *data)
1885b2fc499SJeff Garzik {
1895b2fc499SJeff Garzik 	int ret;
1905b2fc499SJeff Garzik 	char *buffer;
1915b2fc499SJeff Garzik 	DECLARE_WAITQUEUE(wait, current);
1925b2fc499SJeff Garzik 
1935b2fc499SJeff Garzik 	buffer = kmalloc(size, GFP_KERNEL);
1945b2fc499SJeff Garzik 	if (!buffer) {
1955b2fc499SJeff Garzik 		if (netif_msg_drv(pegasus))
1965b2fc499SJeff Garzik 			dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
197653c0316SHarvey Harrison 					__func__);
1985b2fc499SJeff Garzik 		return -ENOMEM;
1995b2fc499SJeff Garzik 	}
2005b2fc499SJeff Garzik 	memcpy(buffer, data, size);
2015b2fc499SJeff Garzik 
2025b2fc499SJeff Garzik 	add_wait_queue(&pegasus->ctrl_wait, &wait);
2035b2fc499SJeff Garzik 	set_current_state(TASK_UNINTERRUPTIBLE);
2045b2fc499SJeff Garzik 	while (pegasus->flags & ETH_REGS_CHANGED)
2055b2fc499SJeff Garzik 		schedule();
2065b2fc499SJeff Garzik 	remove_wait_queue(&pegasus->ctrl_wait, &wait);
2075b2fc499SJeff Garzik 	set_current_state(TASK_RUNNING);
2085b2fc499SJeff Garzik 
2095b2fc499SJeff Garzik 	pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
2105b2fc499SJeff Garzik 	pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
2115b2fc499SJeff Garzik 	pegasus->dr.wValue = cpu_to_le16(0);
212da2bbdccSHarvey Harrison 	pegasus->dr.wIndex = cpu_to_le16(indx);
213da2bbdccSHarvey Harrison 	pegasus->dr.wLength = cpu_to_le16(size);
2145b2fc499SJeff Garzik 	pegasus->ctrl_urb->transfer_buffer_length = size;
2155b2fc499SJeff Garzik 
2165b2fc499SJeff Garzik 	usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
2175b2fc499SJeff Garzik 			     usb_sndctrlpipe(pegasus->usb, 0),
2185b2fc499SJeff Garzik 			     (char *) &pegasus->dr,
2195b2fc499SJeff Garzik 			     buffer, size, ctrl_callback, pegasus);
2205b2fc499SJeff Garzik 
2215b2fc499SJeff Garzik 	add_wait_queue(&pegasus->ctrl_wait, &wait);
2225b2fc499SJeff Garzik 	set_current_state(TASK_UNINTERRUPTIBLE);
2235b2fc499SJeff Garzik 
2245b2fc499SJeff Garzik 	if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
2255b2fc499SJeff Garzik 		if (ret == -ENODEV)
2265b2fc499SJeff Garzik 			netif_device_detach(pegasus->net);
2275b2fc499SJeff Garzik 		if (netif_msg_drv(pegasus))
2285b2fc499SJeff Garzik 			dev_err(&pegasus->intf->dev, "%s, status %d\n",
229b39d66a8SHarvey Harrison 					__func__, ret);
2305b2fc499SJeff Garzik 		goto out;
2315b2fc499SJeff Garzik 	}
2325b2fc499SJeff Garzik 
2335b2fc499SJeff Garzik 	schedule();
2345b2fc499SJeff Garzik out:
2355b2fc499SJeff Garzik 	remove_wait_queue(&pegasus->ctrl_wait, &wait);
2365b2fc499SJeff Garzik 	kfree(buffer);
2375b2fc499SJeff Garzik 
2385b2fc499SJeff Garzik 	return ret;
2395b2fc499SJeff Garzik }
2405b2fc499SJeff Garzik 
2415b2fc499SJeff Garzik static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
2425b2fc499SJeff Garzik {
2435b2fc499SJeff Garzik 	int ret;
2445b2fc499SJeff Garzik 	char *tmp;
2455b2fc499SJeff Garzik 	DECLARE_WAITQUEUE(wait, current);
2465b2fc499SJeff Garzik 
2475b2fc499SJeff Garzik 	tmp = kmalloc(1, GFP_KERNEL);
2485b2fc499SJeff Garzik 	if (!tmp) {
2495b2fc499SJeff Garzik 		if (netif_msg_drv(pegasus))
2505b2fc499SJeff Garzik 			dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
251b39d66a8SHarvey Harrison 					__func__);
2525b2fc499SJeff Garzik 		return -ENOMEM;
2535b2fc499SJeff Garzik 	}
2545b2fc499SJeff Garzik 	memcpy(tmp, &data, 1);
2555b2fc499SJeff Garzik 	add_wait_queue(&pegasus->ctrl_wait, &wait);
2565b2fc499SJeff Garzik 	set_current_state(TASK_UNINTERRUPTIBLE);
2575b2fc499SJeff Garzik 	while (pegasus->flags & ETH_REGS_CHANGED)
2585b2fc499SJeff Garzik 		schedule();
2595b2fc499SJeff Garzik 	remove_wait_queue(&pegasus->ctrl_wait, &wait);
2605b2fc499SJeff Garzik 	set_current_state(TASK_RUNNING);
2615b2fc499SJeff Garzik 
2625b2fc499SJeff Garzik 	pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
2635b2fc499SJeff Garzik 	pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
2645b2fc499SJeff Garzik 	pegasus->dr.wValue = cpu_to_le16(data);
265da2bbdccSHarvey Harrison 	pegasus->dr.wIndex = cpu_to_le16(indx);
2665b2fc499SJeff Garzik 	pegasus->dr.wLength = cpu_to_le16(1);
2675b2fc499SJeff Garzik 	pegasus->ctrl_urb->transfer_buffer_length = 1;
2685b2fc499SJeff Garzik 
2695b2fc499SJeff Garzik 	usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
2705b2fc499SJeff Garzik 			     usb_sndctrlpipe(pegasus->usb, 0),
2715b2fc499SJeff Garzik 			     (char *) &pegasus->dr,
2725b2fc499SJeff Garzik 			     tmp, 1, ctrl_callback, pegasus);
2735b2fc499SJeff Garzik 
2745b2fc499SJeff Garzik 	add_wait_queue(&pegasus->ctrl_wait, &wait);
2755b2fc499SJeff Garzik 	set_current_state(TASK_UNINTERRUPTIBLE);
2765b2fc499SJeff Garzik 
2775b2fc499SJeff Garzik 	if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
2785b2fc499SJeff Garzik 		if (ret == -ENODEV)
2795b2fc499SJeff Garzik 			netif_device_detach(pegasus->net);
280e000ea13SDavid Brownell 		if (netif_msg_drv(pegasus) && printk_ratelimit())
2815b2fc499SJeff Garzik 			dev_err(&pegasus->intf->dev, "%s, status %d\n",
282b39d66a8SHarvey Harrison 					__func__, ret);
2835b2fc499SJeff Garzik 		goto out;
2845b2fc499SJeff Garzik 	}
2855b2fc499SJeff Garzik 
2865b2fc499SJeff Garzik 	schedule();
2875b2fc499SJeff Garzik out:
2885b2fc499SJeff Garzik 	remove_wait_queue(&pegasus->ctrl_wait, &wait);
2895b2fc499SJeff Garzik 	kfree(tmp);
2905b2fc499SJeff Garzik 
2915b2fc499SJeff Garzik 	return ret;
2925b2fc499SJeff Garzik }
2935b2fc499SJeff Garzik 
2945b2fc499SJeff Garzik static int update_eth_regs_async(pegasus_t * pegasus)
2955b2fc499SJeff Garzik {
2965b2fc499SJeff Garzik 	int ret;
2975b2fc499SJeff Garzik 
2985b2fc499SJeff Garzik 	pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
2995b2fc499SJeff Garzik 	pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
300e3453f63SMichael Buesch 	pegasus->dr.wValue = cpu_to_le16(0);
3015b2fc499SJeff Garzik 	pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
3025b2fc499SJeff Garzik 	pegasus->dr.wLength = cpu_to_le16(3);
3035b2fc499SJeff Garzik 	pegasus->ctrl_urb->transfer_buffer_length = 3;
3045b2fc499SJeff Garzik 
3055b2fc499SJeff Garzik 	usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
3065b2fc499SJeff Garzik 			     usb_sndctrlpipe(pegasus->usb, 0),
3075b2fc499SJeff Garzik 			     (char *) &pegasus->dr,
3085b2fc499SJeff Garzik 			     pegasus->eth_regs, 3, ctrl_callback, pegasus);
3095b2fc499SJeff Garzik 
3105b2fc499SJeff Garzik 	if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
3115b2fc499SJeff Garzik 		if (ret == -ENODEV)
3125b2fc499SJeff Garzik 			netif_device_detach(pegasus->net);
3135b2fc499SJeff Garzik 		if (netif_msg_drv(pegasus))
3145b2fc499SJeff Garzik 			dev_err(&pegasus->intf->dev, "%s, status %d\n",
315b39d66a8SHarvey Harrison 					__func__, ret);
3165b2fc499SJeff Garzik 	}
3175b2fc499SJeff Garzik 
3185b2fc499SJeff Garzik 	return ret;
3195b2fc499SJeff Garzik }
3205b2fc499SJeff Garzik 
3215b2fc499SJeff Garzik /* Returns 0 on success, error on failure */
3225b2fc499SJeff Garzik static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
3235b2fc499SJeff Garzik {
3245b2fc499SJeff Garzik 	int i;
3255b2fc499SJeff Garzik 	__u8 data[4] = { phy, 0, 0, indx };
3265b2fc499SJeff Garzik 	__le16 regdi;
3275b2fc499SJeff Garzik 	int ret;
3285b2fc499SJeff Garzik 
3295b2fc499SJeff Garzik 	set_register(pegasus, PhyCtrl, 0);
3305b2fc499SJeff Garzik 	set_registers(pegasus, PhyAddr, sizeof (data), data);
3315b2fc499SJeff Garzik 	set_register(pegasus, PhyCtrl, (indx | PHY_READ));
3325b2fc499SJeff Garzik 	for (i = 0; i < REG_TIMEOUT; i++) {
3335b2fc499SJeff Garzik 		ret = get_registers(pegasus, PhyCtrl, 1, data);
3345b2fc499SJeff Garzik 		if (ret == -ESHUTDOWN)
3355b2fc499SJeff Garzik 			goto fail;
3365b2fc499SJeff Garzik 		if (data[0] & PHY_DONE)
3375b2fc499SJeff Garzik 			break;
3385b2fc499SJeff Garzik 	}
3395b2fc499SJeff Garzik 	if (i < REG_TIMEOUT) {
3405b2fc499SJeff Garzik 		ret = get_registers(pegasus, PhyData, 2, &regdi);
3415b2fc499SJeff Garzik 		*regd = le16_to_cpu(regdi);
3425b2fc499SJeff Garzik 		return ret;
3435b2fc499SJeff Garzik 	}
3445b2fc499SJeff Garzik fail:
3455b2fc499SJeff Garzik 	if (netif_msg_drv(pegasus))
346b39d66a8SHarvey Harrison 		dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
3475b2fc499SJeff Garzik 
3485b2fc499SJeff Garzik 	return ret;
3495b2fc499SJeff Garzik }
3505b2fc499SJeff Garzik 
3515b2fc499SJeff Garzik static int mdio_read(struct net_device *dev, int phy_id, int loc)
3525b2fc499SJeff Garzik {
3535b2fc499SJeff Garzik 	pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
3545b2fc499SJeff Garzik 	u16 res;
3555b2fc499SJeff Garzik 
3565b2fc499SJeff Garzik 	read_mii_word(pegasus, phy_id, loc, &res);
3575b2fc499SJeff Garzik 	return (int)res;
3585b2fc499SJeff Garzik }
3595b2fc499SJeff Garzik 
3605b2fc499SJeff Garzik static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
3615b2fc499SJeff Garzik {
3625b2fc499SJeff Garzik 	int i;
3635b2fc499SJeff Garzik 	__u8 data[4] = { phy, 0, 0, indx };
3645b2fc499SJeff Garzik 	int ret;
3655b2fc499SJeff Garzik 
3665b2fc499SJeff Garzik 	data[1] = (u8) regd;
3675b2fc499SJeff Garzik 	data[2] = (u8) (regd >> 8);
3685b2fc499SJeff Garzik 	set_register(pegasus, PhyCtrl, 0);
3695b2fc499SJeff Garzik 	set_registers(pegasus, PhyAddr, sizeof(data), data);
3705b2fc499SJeff Garzik 	set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
3715b2fc499SJeff Garzik 	for (i = 0; i < REG_TIMEOUT; i++) {
3725b2fc499SJeff Garzik 		ret = get_registers(pegasus, PhyCtrl, 1, data);
3735b2fc499SJeff Garzik 		if (ret == -ESHUTDOWN)
3745b2fc499SJeff Garzik 			goto fail;
3755b2fc499SJeff Garzik 		if (data[0] & PHY_DONE)
3765b2fc499SJeff Garzik 			break;
3775b2fc499SJeff Garzik 	}
3785b2fc499SJeff Garzik 	if (i < REG_TIMEOUT)
3795b2fc499SJeff Garzik 		return ret;
3805b2fc499SJeff Garzik 
3815b2fc499SJeff Garzik fail:
3825b2fc499SJeff Garzik 	if (netif_msg_drv(pegasus))
383b39d66a8SHarvey Harrison 		dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
3845b2fc499SJeff Garzik 	return -ETIMEDOUT;
3855b2fc499SJeff Garzik }
3865b2fc499SJeff Garzik 
3875b2fc499SJeff Garzik static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
3885b2fc499SJeff Garzik {
3895b2fc499SJeff Garzik 	pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
3905b2fc499SJeff Garzik 
3915b2fc499SJeff Garzik 	write_mii_word(pegasus, phy_id, loc, val);
3925b2fc499SJeff Garzik }
3935b2fc499SJeff Garzik 
3945b2fc499SJeff Garzik static int read_eprom_word(pegasus_t * pegasus, __u8 index, __u16 * retdata)
3955b2fc499SJeff Garzik {
3965b2fc499SJeff Garzik 	int i;
3975b2fc499SJeff Garzik 	__u8 tmp;
3985b2fc499SJeff Garzik 	__le16 retdatai;
3995b2fc499SJeff Garzik 	int ret;
4005b2fc499SJeff Garzik 
4015b2fc499SJeff Garzik 	set_register(pegasus, EpromCtrl, 0);
4025b2fc499SJeff Garzik 	set_register(pegasus, EpromOffset, index);
4035b2fc499SJeff Garzik 	set_register(pegasus, EpromCtrl, EPROM_READ);
4045b2fc499SJeff Garzik 
4055b2fc499SJeff Garzik 	for (i = 0; i < REG_TIMEOUT; i++) {
4065b2fc499SJeff Garzik 		ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
4075b2fc499SJeff Garzik 		if (tmp & EPROM_DONE)
4085b2fc499SJeff Garzik 			break;
4095b2fc499SJeff Garzik 		if (ret == -ESHUTDOWN)
4105b2fc499SJeff Garzik 			goto fail;
4115b2fc499SJeff Garzik 	}
4125b2fc499SJeff Garzik 	if (i < REG_TIMEOUT) {
4135b2fc499SJeff Garzik 		ret = get_registers(pegasus, EpromData, 2, &retdatai);
4145b2fc499SJeff Garzik 		*retdata = le16_to_cpu(retdatai);
4155b2fc499SJeff Garzik 		return ret;
4165b2fc499SJeff Garzik 	}
4175b2fc499SJeff Garzik 
4185b2fc499SJeff Garzik fail:
4195b2fc499SJeff Garzik 	if (netif_msg_drv(pegasus))
420b39d66a8SHarvey Harrison 		dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
4215b2fc499SJeff Garzik 	return -ETIMEDOUT;
4225b2fc499SJeff Garzik }
4235b2fc499SJeff Garzik 
4245b2fc499SJeff Garzik #ifdef	PEGASUS_WRITE_EEPROM
4255b2fc499SJeff Garzik static inline void enable_eprom_write(pegasus_t * pegasus)
4265b2fc499SJeff Garzik {
4275b2fc499SJeff Garzik 	__u8 tmp;
4285b2fc499SJeff Garzik 	int ret;
4295b2fc499SJeff Garzik 
4305b2fc499SJeff Garzik 	get_registers(pegasus, EthCtrl2, 1, &tmp);
4315b2fc499SJeff Garzik 	set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
4325b2fc499SJeff Garzik }
4335b2fc499SJeff Garzik 
4345b2fc499SJeff Garzik static inline void disable_eprom_write(pegasus_t * pegasus)
4355b2fc499SJeff Garzik {
4365b2fc499SJeff Garzik 	__u8 tmp;
4375b2fc499SJeff Garzik 	int ret;
4385b2fc499SJeff Garzik 
4395b2fc499SJeff Garzik 	get_registers(pegasus, EthCtrl2, 1, &tmp);
4405b2fc499SJeff Garzik 	set_register(pegasus, EpromCtrl, 0);
4415b2fc499SJeff Garzik 	set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
4425b2fc499SJeff Garzik }
4435b2fc499SJeff Garzik 
4445b2fc499SJeff Garzik static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
4455b2fc499SJeff Garzik {
4465b2fc499SJeff Garzik 	int i;
4475b2fc499SJeff Garzik 	__u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
4485b2fc499SJeff Garzik 	int ret;
449e3453f63SMichael Buesch 	__le16 le_data = cpu_to_le16(data);
4505b2fc499SJeff Garzik 
4515b2fc499SJeff Garzik 	set_registers(pegasus, EpromOffset, 4, d);
4525b2fc499SJeff Garzik 	enable_eprom_write(pegasus);
4535b2fc499SJeff Garzik 	set_register(pegasus, EpromOffset, index);
454e3453f63SMichael Buesch 	set_registers(pegasus, EpromData, 2, &le_data);
4555b2fc499SJeff Garzik 	set_register(pegasus, EpromCtrl, EPROM_WRITE);
4565b2fc499SJeff Garzik 
4575b2fc499SJeff Garzik 	for (i = 0; i < REG_TIMEOUT; i++) {
4585b2fc499SJeff Garzik 		ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
4595b2fc499SJeff Garzik 		if (ret == -ESHUTDOWN)
4605b2fc499SJeff Garzik 			goto fail;
4615b2fc499SJeff Garzik 		if (tmp & EPROM_DONE)
4625b2fc499SJeff Garzik 			break;
4635b2fc499SJeff Garzik 	}
4645b2fc499SJeff Garzik 	disable_eprom_write(pegasus);
4655b2fc499SJeff Garzik 	if (i < REG_TIMEOUT)
4665b2fc499SJeff Garzik 		return ret;
4675b2fc499SJeff Garzik fail:
4685b2fc499SJeff Garzik 	if (netif_msg_drv(pegasus))
469b39d66a8SHarvey Harrison 		dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
4705b2fc499SJeff Garzik 	return -ETIMEDOUT;
4715b2fc499SJeff Garzik }
4725b2fc499SJeff Garzik #endif				/* PEGASUS_WRITE_EEPROM */
4735b2fc499SJeff Garzik 
4745b2fc499SJeff Garzik static inline void get_node_id(pegasus_t * pegasus, __u8 * id)
4755b2fc499SJeff Garzik {
4765b2fc499SJeff Garzik 	int i;
4775b2fc499SJeff Garzik 	__u16 w16;
4785b2fc499SJeff Garzik 
4795b2fc499SJeff Garzik 	for (i = 0; i < 3; i++) {
4805b2fc499SJeff Garzik 		read_eprom_word(pegasus, i, &w16);
481da2bbdccSHarvey Harrison 		((__le16 *) id)[i] = cpu_to_le16(w16);
4825b2fc499SJeff Garzik 	}
4835b2fc499SJeff Garzik }
4845b2fc499SJeff Garzik 
4855b2fc499SJeff Garzik static void set_ethernet_addr(pegasus_t * pegasus)
4865b2fc499SJeff Garzik {
4875b2fc499SJeff Garzik 	__u8 node_id[6];
4885b2fc499SJeff Garzik 
4895b2fc499SJeff Garzik 	if (pegasus->features & PEGASUS_II) {
4905b2fc499SJeff Garzik 		get_registers(pegasus, 0x10, sizeof(node_id), node_id);
4915b2fc499SJeff Garzik 	} else {
4925b2fc499SJeff Garzik 		get_node_id(pegasus, node_id);
4935b2fc499SJeff Garzik 		set_registers(pegasus, EthID, sizeof (node_id), node_id);
4945b2fc499SJeff Garzik 	}
4955b2fc499SJeff Garzik 	memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
4965b2fc499SJeff Garzik }
4975b2fc499SJeff Garzik 
4985b2fc499SJeff Garzik static inline int reset_mac(pegasus_t * pegasus)
4995b2fc499SJeff Garzik {
5005b2fc499SJeff Garzik 	__u8 data = 0x8;
5015b2fc499SJeff Garzik 	int i;
5025b2fc499SJeff Garzik 
5035b2fc499SJeff Garzik 	set_register(pegasus, EthCtrl1, data);
5045b2fc499SJeff Garzik 	for (i = 0; i < REG_TIMEOUT; i++) {
5055b2fc499SJeff Garzik 		get_registers(pegasus, EthCtrl1, 1, &data);
5065b2fc499SJeff Garzik 		if (~data & 0x08) {
5075b2fc499SJeff Garzik 			if (loopback & 1)
5085b2fc499SJeff Garzik 				break;
5095b2fc499SJeff Garzik 			if (mii_mode && (pegasus->features & HAS_HOME_PNA))
5105b2fc499SJeff Garzik 				set_register(pegasus, Gpio1, 0x34);
5115b2fc499SJeff Garzik 			else
5125b2fc499SJeff Garzik 				set_register(pegasus, Gpio1, 0x26);
5135b2fc499SJeff Garzik 			set_register(pegasus, Gpio0, pegasus->features);
5145b2fc499SJeff Garzik 			set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
5155b2fc499SJeff Garzik 			break;
5165b2fc499SJeff Garzik 		}
5175b2fc499SJeff Garzik 	}
5185b2fc499SJeff Garzik 	if (i == REG_TIMEOUT)
5195b2fc499SJeff Garzik 		return -ETIMEDOUT;
5205b2fc499SJeff Garzik 
5215b2fc499SJeff Garzik 	if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
5225b2fc499SJeff Garzik 	    usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
5235b2fc499SJeff Garzik 		set_register(pegasus, Gpio0, 0x24);
5245b2fc499SJeff Garzik 		set_register(pegasus, Gpio0, 0x26);
5255b2fc499SJeff Garzik 	}
5265b2fc499SJeff Garzik 	if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
5275b2fc499SJeff Garzik 		__u16 auxmode;
5285b2fc499SJeff Garzik 		read_mii_word(pegasus, 3, 0x1b, &auxmode);
5295b2fc499SJeff Garzik 		write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
5305b2fc499SJeff Garzik 	}
5315b2fc499SJeff Garzik 
5325b2fc499SJeff Garzik 	return 0;
5335b2fc499SJeff Garzik }
5345b2fc499SJeff Garzik 
5355b2fc499SJeff Garzik static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
5365b2fc499SJeff Garzik {
5375b2fc499SJeff Garzik 	__u16 linkpart;
5385b2fc499SJeff Garzik 	__u8 data[4];
5395b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
5405b2fc499SJeff Garzik 	int ret;
5415b2fc499SJeff Garzik 
5425b2fc499SJeff Garzik 	read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
5435b2fc499SJeff Garzik 	data[0] = 0xc9;
5445b2fc499SJeff Garzik 	data[1] = 0;
5455b2fc499SJeff Garzik 	if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
5465b2fc499SJeff Garzik 		data[1] |= 0x20;	/* set full duplex */
5475b2fc499SJeff Garzik 	if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
5485b2fc499SJeff Garzik 		data[1] |= 0x10;	/* set 100 Mbps */
5495b2fc499SJeff Garzik 	if (mii_mode)
5505b2fc499SJeff Garzik 		data[1] = 0;
5515b2fc499SJeff Garzik 	data[2] = (loopback & 1) ? 0x09 : 0x01;
5525b2fc499SJeff Garzik 
5535b2fc499SJeff Garzik 	memcpy(pegasus->eth_regs, data, sizeof (data));
5545b2fc499SJeff Garzik 	ret = set_registers(pegasus, EthCtrl0, 3, data);
5555b2fc499SJeff Garzik 
5565b2fc499SJeff Garzik 	if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
5575b2fc499SJeff Garzik 	    usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
5585b2fc499SJeff Garzik 	    usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
5595b2fc499SJeff Garzik 		u16 auxmode;
5605b2fc499SJeff Garzik 		read_mii_word(pegasus, 0, 0x1b, &auxmode);
5615b2fc499SJeff Garzik 		write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
5625b2fc499SJeff Garzik 	}
5635b2fc499SJeff Garzik 
5645b2fc499SJeff Garzik 	return ret;
5655b2fc499SJeff Garzik }
5665b2fc499SJeff Garzik 
5675b2fc499SJeff Garzik static void fill_skb_pool(pegasus_t * pegasus)
5685b2fc499SJeff Garzik {
5695b2fc499SJeff Garzik 	int i;
5705b2fc499SJeff Garzik 
5715b2fc499SJeff Garzik 	for (i = 0; i < RX_SKBS; i++) {
5725b2fc499SJeff Garzik 		if (pegasus->rx_pool[i])
5735b2fc499SJeff Garzik 			continue;
5745b2fc499SJeff Garzik 		pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
5755b2fc499SJeff Garzik 		/*
5765b2fc499SJeff Garzik 		 ** we give up if the allocation fail. the tasklet will be
5775b2fc499SJeff Garzik 		 ** rescheduled again anyway...
5785b2fc499SJeff Garzik 		 */
5795b2fc499SJeff Garzik 		if (pegasus->rx_pool[i] == NULL)
5805b2fc499SJeff Garzik 			return;
5815b2fc499SJeff Garzik 		skb_reserve(pegasus->rx_pool[i], 2);
5825b2fc499SJeff Garzik 	}
5835b2fc499SJeff Garzik }
5845b2fc499SJeff Garzik 
5855b2fc499SJeff Garzik static void free_skb_pool(pegasus_t * pegasus)
5865b2fc499SJeff Garzik {
5875b2fc499SJeff Garzik 	int i;
5885b2fc499SJeff Garzik 
5895b2fc499SJeff Garzik 	for (i = 0; i < RX_SKBS; i++) {
5905b2fc499SJeff Garzik 		if (pegasus->rx_pool[i]) {
5915b2fc499SJeff Garzik 			dev_kfree_skb(pegasus->rx_pool[i]);
5925b2fc499SJeff Garzik 			pegasus->rx_pool[i] = NULL;
5935b2fc499SJeff Garzik 		}
5945b2fc499SJeff Garzik 	}
5955b2fc499SJeff Garzik }
5965b2fc499SJeff Garzik 
5975b2fc499SJeff Garzik static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
5985b2fc499SJeff Garzik {
5995b2fc499SJeff Garzik 	int i;
6005b2fc499SJeff Garzik 	struct sk_buff *skb;
6015b2fc499SJeff Garzik 
6025b2fc499SJeff Garzik 	for (i = 0; i < RX_SKBS; i++) {
6035b2fc499SJeff Garzik 		if (likely(pegasus->rx_pool[i] != NULL)) {
6045b2fc499SJeff Garzik 			skb = pegasus->rx_pool[i];
6055b2fc499SJeff Garzik 			pegasus->rx_pool[i] = NULL;
6065b2fc499SJeff Garzik 			return skb;
6075b2fc499SJeff Garzik 		}
6085b2fc499SJeff Garzik 	}
6095b2fc499SJeff Garzik 	return NULL;
6105b2fc499SJeff Garzik }
6115b2fc499SJeff Garzik 
6125b2fc499SJeff Garzik static void read_bulk_callback(struct urb *urb)
6135b2fc499SJeff Garzik {
6145b2fc499SJeff Garzik 	pegasus_t *pegasus = urb->context;
6155b2fc499SJeff Garzik 	struct net_device *net;
6165b2fc499SJeff Garzik 	int rx_status, count = urb->actual_length;
617c94cb314SOliver Neukum 	int status = urb->status;
6185b2fc499SJeff Garzik 	u8 *buf = urb->transfer_buffer;
6195b2fc499SJeff Garzik 	__u16 pkt_len;
6205b2fc499SJeff Garzik 
6215b2fc499SJeff Garzik 	if (!pegasus)
6225b2fc499SJeff Garzik 		return;
6235b2fc499SJeff Garzik 
6245b2fc499SJeff Garzik 	net = pegasus->net;
6255b2fc499SJeff Garzik 	if (!netif_device_present(net) || !netif_running(net))
6265b2fc499SJeff Garzik 		return;
6275b2fc499SJeff Garzik 
628c94cb314SOliver Neukum 	switch (status) {
6295b2fc499SJeff Garzik 	case 0:
6305b2fc499SJeff Garzik 		break;
6315b2fc499SJeff Garzik 	case -ETIME:
6325b2fc499SJeff Garzik 		if (netif_msg_rx_err(pegasus))
6335b2fc499SJeff Garzik 			pr_debug("%s: reset MAC\n", net->name);
6345b2fc499SJeff Garzik 		pegasus->flags &= ~PEGASUS_RX_BUSY;
6355b2fc499SJeff Garzik 		break;
6365b2fc499SJeff Garzik 	case -EPIPE:		/* stall, or disconnect from TT */
6375b2fc499SJeff Garzik 		/* FIXME schedule work to clear the halt */
6385b2fc499SJeff Garzik 		if (netif_msg_rx_err(pegasus))
6395b2fc499SJeff Garzik 			printk(KERN_WARNING "%s: no rx stall recovery\n",
6405b2fc499SJeff Garzik 					net->name);
6415b2fc499SJeff Garzik 		return;
6425b2fc499SJeff Garzik 	case -ENOENT:
6435b2fc499SJeff Garzik 	case -ECONNRESET:
6445b2fc499SJeff Garzik 	case -ESHUTDOWN:
6455b2fc499SJeff Garzik 		if (netif_msg_ifdown(pegasus))
646c94cb314SOliver Neukum 			pr_debug("%s: rx unlink, %d\n", net->name, status);
6475b2fc499SJeff Garzik 		return;
6485b2fc499SJeff Garzik 	default:
6495b2fc499SJeff Garzik 		if (netif_msg_rx_err(pegasus))
650c94cb314SOliver Neukum 			pr_debug("%s: RX status %d\n", net->name, status);
6515b2fc499SJeff Garzik 		goto goon;
6525b2fc499SJeff Garzik 	}
6535b2fc499SJeff Garzik 
6545b2fc499SJeff Garzik 	if (!count || count < 4)
6555b2fc499SJeff Garzik 		goto goon;
6565b2fc499SJeff Garzik 
6575b2fc499SJeff Garzik 	rx_status = buf[count - 2];
6585b2fc499SJeff Garzik 	if (rx_status & 0x1e) {
6595b2fc499SJeff Garzik 		if (netif_msg_rx_err(pegasus))
6605b2fc499SJeff Garzik 			pr_debug("%s: RX packet error %x\n",
6615b2fc499SJeff Garzik 					net->name, rx_status);
6625b2fc499SJeff Garzik 		pegasus->stats.rx_errors++;
6635b2fc499SJeff Garzik 		if (rx_status & 0x06)	// long or runt
6645b2fc499SJeff Garzik 			pegasus->stats.rx_length_errors++;
6655b2fc499SJeff Garzik 		if (rx_status & 0x08)
6665b2fc499SJeff Garzik 			pegasus->stats.rx_crc_errors++;
6675b2fc499SJeff Garzik 		if (rx_status & 0x10)	// extra bits
6685b2fc499SJeff Garzik 			pegasus->stats.rx_frame_errors++;
6695b2fc499SJeff Garzik 		goto goon;
6705b2fc499SJeff Garzik 	}
6715b2fc499SJeff Garzik 	if (pegasus->chip == 0x8513) {
6725b2fc499SJeff Garzik 		pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
6735b2fc499SJeff Garzik 		pkt_len &= 0x0fff;
6745b2fc499SJeff Garzik 		pegasus->rx_skb->data += 2;
6755b2fc499SJeff Garzik 	} else {
6765b2fc499SJeff Garzik 		pkt_len = buf[count - 3] << 8;
6775b2fc499SJeff Garzik 		pkt_len += buf[count - 4];
6785b2fc499SJeff Garzik 		pkt_len &= 0xfff;
6795b2fc499SJeff Garzik 		pkt_len -= 8;
6805b2fc499SJeff Garzik 	}
6815b2fc499SJeff Garzik 
6825b2fc499SJeff Garzik 	/*
6835b2fc499SJeff Garzik 	 * If the packet is unreasonably long, quietly drop it rather than
6845b2fc499SJeff Garzik 	 * kernel panicing by calling skb_put.
6855b2fc499SJeff Garzik 	 */
6865b2fc499SJeff Garzik 	if (pkt_len > PEGASUS_MTU)
6875b2fc499SJeff Garzik 		goto goon;
6885b2fc499SJeff Garzik 
6895b2fc499SJeff Garzik 	/*
6905b2fc499SJeff Garzik 	 * at this point we are sure pegasus->rx_skb != NULL
6915b2fc499SJeff Garzik 	 * so we go ahead and pass up the packet.
6925b2fc499SJeff Garzik 	 */
6935b2fc499SJeff Garzik 	skb_put(pegasus->rx_skb, pkt_len);
6945b2fc499SJeff Garzik 	pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
6955b2fc499SJeff Garzik 	netif_rx(pegasus->rx_skb);
6965b2fc499SJeff Garzik 	pegasus->stats.rx_packets++;
6975b2fc499SJeff Garzik 	pegasus->stats.rx_bytes += pkt_len;
6985b2fc499SJeff Garzik 
6995b2fc499SJeff Garzik 	if (pegasus->flags & PEGASUS_UNPLUG)
7005b2fc499SJeff Garzik 		return;
7015b2fc499SJeff Garzik 
7025b2fc499SJeff Garzik 	spin_lock(&pegasus->rx_pool_lock);
7035b2fc499SJeff Garzik 	pegasus->rx_skb = pull_skb(pegasus);
7045b2fc499SJeff Garzik 	spin_unlock(&pegasus->rx_pool_lock);
7055b2fc499SJeff Garzik 
7065b2fc499SJeff Garzik 	if (pegasus->rx_skb == NULL)
7075b2fc499SJeff Garzik 		goto tl_sched;
7085b2fc499SJeff Garzik goon:
7095b2fc499SJeff Garzik 	usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
7105b2fc499SJeff Garzik 			  usb_rcvbulkpipe(pegasus->usb, 1),
7115b2fc499SJeff Garzik 			  pegasus->rx_skb->data, PEGASUS_MTU + 8,
7125b2fc499SJeff Garzik 			  read_bulk_callback, pegasus);
7135b2fc499SJeff Garzik 	rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
7145b2fc499SJeff Garzik 	if (rx_status == -ENODEV)
7155b2fc499SJeff Garzik 		netif_device_detach(pegasus->net);
7165b2fc499SJeff Garzik 	else if (rx_status) {
7175b2fc499SJeff Garzik 		pegasus->flags |= PEGASUS_RX_URB_FAIL;
7185b2fc499SJeff Garzik 		goto tl_sched;
7195b2fc499SJeff Garzik 	} else {
7205b2fc499SJeff Garzik 		pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
7215b2fc499SJeff Garzik 	}
7225b2fc499SJeff Garzik 
7235b2fc499SJeff Garzik 	return;
7245b2fc499SJeff Garzik 
7255b2fc499SJeff Garzik tl_sched:
7265b2fc499SJeff Garzik 	tasklet_schedule(&pegasus->rx_tl);
7275b2fc499SJeff Garzik }
7285b2fc499SJeff Garzik 
7295b2fc499SJeff Garzik static void rx_fixup(unsigned long data)
7305b2fc499SJeff Garzik {
7315b2fc499SJeff Garzik 	pegasus_t *pegasus;
7325b2fc499SJeff Garzik 	unsigned long flags;
7335b2fc499SJeff Garzik 	int status;
7345b2fc499SJeff Garzik 
7355b2fc499SJeff Garzik 	pegasus = (pegasus_t *) data;
7365b2fc499SJeff Garzik 	if (pegasus->flags & PEGASUS_UNPLUG)
7375b2fc499SJeff Garzik 		return;
7385b2fc499SJeff Garzik 
7395b2fc499SJeff Garzik 	spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
7405b2fc499SJeff Garzik 	fill_skb_pool(pegasus);
7415b2fc499SJeff Garzik 	if (pegasus->flags & PEGASUS_RX_URB_FAIL)
7425b2fc499SJeff Garzik 		if (pegasus->rx_skb)
7435b2fc499SJeff Garzik 			goto try_again;
7445b2fc499SJeff Garzik 	if (pegasus->rx_skb == NULL) {
7455b2fc499SJeff Garzik 		pegasus->rx_skb = pull_skb(pegasus);
7465b2fc499SJeff Garzik 	}
7475b2fc499SJeff Garzik 	if (pegasus->rx_skb == NULL) {
7485b2fc499SJeff Garzik 		if (netif_msg_rx_err(pegasus))
7495b2fc499SJeff Garzik 			printk(KERN_WARNING "%s: low on memory\n",
7505b2fc499SJeff Garzik 					pegasus->net->name);
7515b2fc499SJeff Garzik 		tasklet_schedule(&pegasus->rx_tl);
7525b2fc499SJeff Garzik 		goto done;
7535b2fc499SJeff Garzik 	}
7545b2fc499SJeff Garzik 	usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
7555b2fc499SJeff Garzik 			  usb_rcvbulkpipe(pegasus->usb, 1),
7565b2fc499SJeff Garzik 			  pegasus->rx_skb->data, PEGASUS_MTU + 8,
7575b2fc499SJeff Garzik 			  read_bulk_callback, pegasus);
7585b2fc499SJeff Garzik try_again:
7595b2fc499SJeff Garzik 	status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
7605b2fc499SJeff Garzik 	if (status == -ENODEV)
7615b2fc499SJeff Garzik 		netif_device_detach(pegasus->net);
7625b2fc499SJeff Garzik 	else if (status) {
7635b2fc499SJeff Garzik 		pegasus->flags |= PEGASUS_RX_URB_FAIL;
7645b2fc499SJeff Garzik 		tasklet_schedule(&pegasus->rx_tl);
7655b2fc499SJeff Garzik 	} else {
7665b2fc499SJeff Garzik 		pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
7675b2fc499SJeff Garzik 	}
7685b2fc499SJeff Garzik done:
7695b2fc499SJeff Garzik 	spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
7705b2fc499SJeff Garzik }
7715b2fc499SJeff Garzik 
7725b2fc499SJeff Garzik static void write_bulk_callback(struct urb *urb)
7735b2fc499SJeff Garzik {
7745b2fc499SJeff Garzik 	pegasus_t *pegasus = urb->context;
7759351982bSMicah Gruber 	struct net_device *net;
776c94cb314SOliver Neukum 	int status = urb->status;
7775b2fc499SJeff Garzik 
7785b2fc499SJeff Garzik 	if (!pegasus)
7795b2fc499SJeff Garzik 		return;
7805b2fc499SJeff Garzik 
7819351982bSMicah Gruber 	net = pegasus->net;
7829351982bSMicah Gruber 
7835b2fc499SJeff Garzik 	if (!netif_device_present(net) || !netif_running(net))
7845b2fc499SJeff Garzik 		return;
7855b2fc499SJeff Garzik 
786c94cb314SOliver Neukum 	switch (status) {
7875b2fc499SJeff Garzik 	case -EPIPE:
7885b2fc499SJeff Garzik 		/* FIXME schedule_work() to clear the tx halt */
7895b2fc499SJeff Garzik 		netif_stop_queue(net);
7905b2fc499SJeff Garzik 		if (netif_msg_tx_err(pegasus))
7915b2fc499SJeff Garzik 			printk(KERN_WARNING "%s: no tx stall recovery\n",
7925b2fc499SJeff Garzik 					net->name);
7935b2fc499SJeff Garzik 		return;
7945b2fc499SJeff Garzik 	case -ENOENT:
7955b2fc499SJeff Garzik 	case -ECONNRESET:
7965b2fc499SJeff Garzik 	case -ESHUTDOWN:
7975b2fc499SJeff Garzik 		if (netif_msg_ifdown(pegasus))
798c94cb314SOliver Neukum 			pr_debug("%s: tx unlink, %d\n", net->name, status);
7995b2fc499SJeff Garzik 		return;
8005b2fc499SJeff Garzik 	default:
8015b2fc499SJeff Garzik 		if (netif_msg_tx_err(pegasus))
802c94cb314SOliver Neukum 			pr_info("%s: TX status %d\n", net->name, status);
8035b2fc499SJeff Garzik 		/* FALL THROUGH */
8045b2fc499SJeff Garzik 	case 0:
8055b2fc499SJeff Garzik 		break;
8065b2fc499SJeff Garzik 	}
8075b2fc499SJeff Garzik 
8085b2fc499SJeff Garzik 	net->trans_start = jiffies;
8095b2fc499SJeff Garzik 	netif_wake_queue(net);
8105b2fc499SJeff Garzik }
8115b2fc499SJeff Garzik 
8125b2fc499SJeff Garzik static void intr_callback(struct urb *urb)
8135b2fc499SJeff Garzik {
8145b2fc499SJeff Garzik 	pegasus_t *pegasus = urb->context;
8155b2fc499SJeff Garzik 	struct net_device *net;
816c94cb314SOliver Neukum 	int res, status = urb->status;
8175b2fc499SJeff Garzik 
8185b2fc499SJeff Garzik 	if (!pegasus)
8195b2fc499SJeff Garzik 		return;
8205b2fc499SJeff Garzik 	net = pegasus->net;
8215b2fc499SJeff Garzik 
822c94cb314SOliver Neukum 	switch (status) {
8235b2fc499SJeff Garzik 	case 0:
8245b2fc499SJeff Garzik 		break;
8255b2fc499SJeff Garzik 	case -ECONNRESET:	/* unlink */
8265b2fc499SJeff Garzik 	case -ENOENT:
8275b2fc499SJeff Garzik 	case -ESHUTDOWN:
8285b2fc499SJeff Garzik 		return;
8295b2fc499SJeff Garzik 	default:
8305b2fc499SJeff Garzik 		/* some Pegasus-I products report LOTS of data
8315b2fc499SJeff Garzik 		 * toggle errors... avoid log spamming
8325b2fc499SJeff Garzik 		 */
8335b2fc499SJeff Garzik 		if (netif_msg_timer(pegasus))
8345b2fc499SJeff Garzik 			pr_debug("%s: intr status %d\n", net->name,
835c94cb314SOliver Neukum 					status);
8365b2fc499SJeff Garzik 	}
8375b2fc499SJeff Garzik 
8385b2fc499SJeff Garzik 	if (urb->actual_length >= 6) {
8395b2fc499SJeff Garzik 		u8	* d = urb->transfer_buffer;
8405b2fc499SJeff Garzik 
8415b2fc499SJeff Garzik 		/* byte 0 == tx_status1, reg 2B */
8425b2fc499SJeff Garzik 		if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
8435b2fc499SJeff Garzik 					|LATE_COL|JABBER_TIMEOUT)) {
8445b2fc499SJeff Garzik 			pegasus->stats.tx_errors++;
8455b2fc499SJeff Garzik 			if (d[0] & TX_UNDERRUN)
8465b2fc499SJeff Garzik 				pegasus->stats.tx_fifo_errors++;
8475b2fc499SJeff Garzik 			if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
8485b2fc499SJeff Garzik 				pegasus->stats.tx_aborted_errors++;
8495b2fc499SJeff Garzik 			if (d[0] & LATE_COL)
8505b2fc499SJeff Garzik 				pegasus->stats.tx_window_errors++;
8515b2fc499SJeff Garzik 		}
8525b2fc499SJeff Garzik 
8535b2fc499SJeff Garzik 		/* d[5].LINK_STATUS lies on some adapters.
8545b2fc499SJeff Garzik 		 * d[0].NO_CARRIER kicks in only with failed TX.
8555b2fc499SJeff Garzik 		 * ... so monitoring with MII may be safest.
8565b2fc499SJeff Garzik 		 */
8575b2fc499SJeff Garzik 
8585b2fc499SJeff Garzik 		/* bytes 3-4 == rx_lostpkt, reg 2E/2F */
8595b2fc499SJeff Garzik 		pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
8605b2fc499SJeff Garzik 	}
8615b2fc499SJeff Garzik 
862c94cb314SOliver Neukum 	res = usb_submit_urb(urb, GFP_ATOMIC);
863c94cb314SOliver Neukum 	if (res == -ENODEV)
8645b2fc499SJeff Garzik 		netif_device_detach(pegasus->net);
865c94cb314SOliver Neukum 	if (res && netif_msg_timer(pegasus))
8665b2fc499SJeff Garzik 		printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
867c94cb314SOliver Neukum 				net->name, res);
8685b2fc499SJeff Garzik }
8695b2fc499SJeff Garzik 
8705b2fc499SJeff Garzik static void pegasus_tx_timeout(struct net_device *net)
8715b2fc499SJeff Garzik {
8725b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
8735b2fc499SJeff Garzik 	if (netif_msg_timer(pegasus))
8745b2fc499SJeff Garzik 		printk(KERN_WARNING "%s: tx timeout\n", net->name);
8755b2fc499SJeff Garzik 	usb_unlink_urb(pegasus->tx_urb);
8765b2fc499SJeff Garzik 	pegasus->stats.tx_errors++;
8775b2fc499SJeff Garzik }
8785b2fc499SJeff Garzik 
8795b2fc499SJeff Garzik static int pegasus_start_xmit(struct sk_buff *skb, struct net_device *net)
8805b2fc499SJeff Garzik {
8815b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
8825b2fc499SJeff Garzik 	int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
8835b2fc499SJeff Garzik 	int res;
8845b2fc499SJeff Garzik 	__u16 l16 = skb->len;
8855b2fc499SJeff Garzik 
8865b2fc499SJeff Garzik 	netif_stop_queue(net);
8875b2fc499SJeff Garzik 
8885b2fc499SJeff Garzik 	((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
8895b2fc499SJeff Garzik 	skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
8905b2fc499SJeff Garzik 	usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
8915b2fc499SJeff Garzik 			  usb_sndbulkpipe(pegasus->usb, 2),
8925b2fc499SJeff Garzik 			  pegasus->tx_buff, count,
8935b2fc499SJeff Garzik 			  write_bulk_callback, pegasus);
8945b2fc499SJeff Garzik 	if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
8955b2fc499SJeff Garzik 		if (netif_msg_tx_err(pegasus))
8965b2fc499SJeff Garzik 			printk(KERN_WARNING "%s: fail tx, %d\n",
8975b2fc499SJeff Garzik 					net->name, res);
8985b2fc499SJeff Garzik 		switch (res) {
8995b2fc499SJeff Garzik 		case -EPIPE:		/* stall, or disconnect from TT */
9005b2fc499SJeff Garzik 			/* cleanup should already have been scheduled */
9015b2fc499SJeff Garzik 			break;
9025b2fc499SJeff Garzik 		case -ENODEV:		/* disconnect() upcoming */
9039dd014ebSOliver Neukum 		case -EPERM:
9045b2fc499SJeff Garzik 			netif_device_detach(pegasus->net);
9055b2fc499SJeff Garzik 			break;
9065b2fc499SJeff Garzik 		default:
9075b2fc499SJeff Garzik 			pegasus->stats.tx_errors++;
9085b2fc499SJeff Garzik 			netif_start_queue(net);
9095b2fc499SJeff Garzik 		}
9105b2fc499SJeff Garzik 	} else {
9115b2fc499SJeff Garzik 		pegasus->stats.tx_packets++;
9125b2fc499SJeff Garzik 		pegasus->stats.tx_bytes += skb->len;
9135b2fc499SJeff Garzik 		net->trans_start = jiffies;
9145b2fc499SJeff Garzik 	}
9155b2fc499SJeff Garzik 	dev_kfree_skb(skb);
9165b2fc499SJeff Garzik 
9175b2fc499SJeff Garzik 	return 0;
9185b2fc499SJeff Garzik }
9195b2fc499SJeff Garzik 
9205b2fc499SJeff Garzik static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
9215b2fc499SJeff Garzik {
9225b2fc499SJeff Garzik 	return &((pegasus_t *) netdev_priv(dev))->stats;
9235b2fc499SJeff Garzik }
9245b2fc499SJeff Garzik 
9255b2fc499SJeff Garzik static inline void disable_net_traffic(pegasus_t * pegasus)
9265b2fc499SJeff Garzik {
927e3453f63SMichael Buesch 	__le16 tmp = cpu_to_le16(0);
9285b2fc499SJeff Garzik 
929e3453f63SMichael Buesch 	set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
9305b2fc499SJeff Garzik }
9315b2fc499SJeff Garzik 
9325b2fc499SJeff Garzik static inline void get_interrupt_interval(pegasus_t * pegasus)
9335b2fc499SJeff Garzik {
934e3453f63SMichael Buesch 	u16 data;
935e3453f63SMichael Buesch 	u8 interval;
9365b2fc499SJeff Garzik 
937e3453f63SMichael Buesch 	read_eprom_word(pegasus, 4, &data);
938e3453f63SMichael Buesch 	interval = data >> 8;
9395b2fc499SJeff Garzik 	if (pegasus->usb->speed != USB_SPEED_HIGH) {
940e3453f63SMichael Buesch 		if (interval < 0x80) {
9415b2fc499SJeff Garzik 			if (netif_msg_timer(pegasus))
9425b2fc499SJeff Garzik 				dev_info(&pegasus->intf->dev, "intr interval "
9435b2fc499SJeff Garzik 					"changed from %ums to %ums\n",
944e3453f63SMichael Buesch 					interval, 0x80);
945e3453f63SMichael Buesch 			interval = 0x80;
946e3453f63SMichael Buesch 			data = (data & 0x00FF) | ((u16)interval << 8);
9475b2fc499SJeff Garzik #ifdef PEGASUS_WRITE_EEPROM
948e3453f63SMichael Buesch 			write_eprom_word(pegasus, 4, data);
9495b2fc499SJeff Garzik #endif
9505b2fc499SJeff Garzik 		}
9515b2fc499SJeff Garzik 	}
952e3453f63SMichael Buesch 	pegasus->intr_interval = interval;
9535b2fc499SJeff Garzik }
9545b2fc499SJeff Garzik 
9555b2fc499SJeff Garzik static void set_carrier(struct net_device *net)
9565b2fc499SJeff Garzik {
9575b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
9585b2fc499SJeff Garzik 	u16 tmp;
9595b2fc499SJeff Garzik 
9605b2fc499SJeff Garzik 	if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
9615b2fc499SJeff Garzik 		return;
9625b2fc499SJeff Garzik 
9635b2fc499SJeff Garzik 	if (tmp & BMSR_LSTATUS)
9645b2fc499SJeff Garzik 		netif_carrier_on(net);
9655b2fc499SJeff Garzik 	else
9665b2fc499SJeff Garzik 		netif_carrier_off(net);
9675b2fc499SJeff Garzik }
9685b2fc499SJeff Garzik 
9695b2fc499SJeff Garzik static void free_all_urbs(pegasus_t * pegasus)
9705b2fc499SJeff Garzik {
9715b2fc499SJeff Garzik 	usb_free_urb(pegasus->intr_urb);
9725b2fc499SJeff Garzik 	usb_free_urb(pegasus->tx_urb);
9735b2fc499SJeff Garzik 	usb_free_urb(pegasus->rx_urb);
9745b2fc499SJeff Garzik 	usb_free_urb(pegasus->ctrl_urb);
9755b2fc499SJeff Garzik }
9765b2fc499SJeff Garzik 
9775b2fc499SJeff Garzik static void unlink_all_urbs(pegasus_t * pegasus)
9785b2fc499SJeff Garzik {
9795b2fc499SJeff Garzik 	usb_kill_urb(pegasus->intr_urb);
9805b2fc499SJeff Garzik 	usb_kill_urb(pegasus->tx_urb);
9815b2fc499SJeff Garzik 	usb_kill_urb(pegasus->rx_urb);
9825b2fc499SJeff Garzik 	usb_kill_urb(pegasus->ctrl_urb);
9835b2fc499SJeff Garzik }
9845b2fc499SJeff Garzik 
9855b2fc499SJeff Garzik static int alloc_urbs(pegasus_t * pegasus)
9865b2fc499SJeff Garzik {
9875b2fc499SJeff Garzik 	pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
9885b2fc499SJeff Garzik 	if (!pegasus->ctrl_urb) {
9895b2fc499SJeff Garzik 		return 0;
9905b2fc499SJeff Garzik 	}
9915b2fc499SJeff Garzik 	pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
9925b2fc499SJeff Garzik 	if (!pegasus->rx_urb) {
9935b2fc499SJeff Garzik 		usb_free_urb(pegasus->ctrl_urb);
9945b2fc499SJeff Garzik 		return 0;
9955b2fc499SJeff Garzik 	}
9965b2fc499SJeff Garzik 	pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
9975b2fc499SJeff Garzik 	if (!pegasus->tx_urb) {
9985b2fc499SJeff Garzik 		usb_free_urb(pegasus->rx_urb);
9995b2fc499SJeff Garzik 		usb_free_urb(pegasus->ctrl_urb);
10005b2fc499SJeff Garzik 		return 0;
10015b2fc499SJeff Garzik 	}
10025b2fc499SJeff Garzik 	pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
10035b2fc499SJeff Garzik 	if (!pegasus->intr_urb) {
10045b2fc499SJeff Garzik 		usb_free_urb(pegasus->tx_urb);
10055b2fc499SJeff Garzik 		usb_free_urb(pegasus->rx_urb);
10065b2fc499SJeff Garzik 		usb_free_urb(pegasus->ctrl_urb);
10075b2fc499SJeff Garzik 		return 0;
10085b2fc499SJeff Garzik 	}
10095b2fc499SJeff Garzik 
10105b2fc499SJeff Garzik 	return 1;
10115b2fc499SJeff Garzik }
10125b2fc499SJeff Garzik 
10135b2fc499SJeff Garzik static int pegasus_open(struct net_device *net)
10145b2fc499SJeff Garzik {
10155b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
10165b2fc499SJeff Garzik 	int res;
10175b2fc499SJeff Garzik 
10185b2fc499SJeff Garzik 	if (pegasus->rx_skb == NULL)
10195b2fc499SJeff Garzik 		pegasus->rx_skb = pull_skb(pegasus);
10205b2fc499SJeff Garzik 	/*
10215b2fc499SJeff Garzik 	 ** Note: no point to free the pool.  it is empty :-)
10225b2fc499SJeff Garzik 	 */
10235b2fc499SJeff Garzik 	if (!pegasus->rx_skb)
10245b2fc499SJeff Garzik 		return -ENOMEM;
10255b2fc499SJeff Garzik 
10265b2fc499SJeff Garzik 	res = set_registers(pegasus, EthID, 6, net->dev_addr);
10275b2fc499SJeff Garzik 
10285b2fc499SJeff Garzik 	usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
10295b2fc499SJeff Garzik 			  usb_rcvbulkpipe(pegasus->usb, 1),
10305b2fc499SJeff Garzik 			  pegasus->rx_skb->data, PEGASUS_MTU + 8,
10315b2fc499SJeff Garzik 			  read_bulk_callback, pegasus);
10325b2fc499SJeff Garzik 	if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
10335b2fc499SJeff Garzik 		if (res == -ENODEV)
10345b2fc499SJeff Garzik 			netif_device_detach(pegasus->net);
10355b2fc499SJeff Garzik 		if (netif_msg_ifup(pegasus))
10365b2fc499SJeff Garzik 			pr_debug("%s: failed rx_urb, %d", net->name, res);
10375b2fc499SJeff Garzik 		goto exit;
10385b2fc499SJeff Garzik 	}
10395b2fc499SJeff Garzik 
10405b2fc499SJeff Garzik 	usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
10415b2fc499SJeff Garzik 			 usb_rcvintpipe(pegasus->usb, 3),
10425b2fc499SJeff Garzik 			 pegasus->intr_buff, sizeof (pegasus->intr_buff),
10435b2fc499SJeff Garzik 			 intr_callback, pegasus, pegasus->intr_interval);
10445b2fc499SJeff Garzik 	if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
10455b2fc499SJeff Garzik 		if (res == -ENODEV)
10465b2fc499SJeff Garzik 			netif_device_detach(pegasus->net);
10475b2fc499SJeff Garzik 		if (netif_msg_ifup(pegasus))
10485b2fc499SJeff Garzik 			pr_debug("%s: failed intr_urb, %d\n", net->name, res);
10495b2fc499SJeff Garzik 		usb_kill_urb(pegasus->rx_urb);
10505b2fc499SJeff Garzik 		goto exit;
10515b2fc499SJeff Garzik 	}
10525b2fc499SJeff Garzik 	if ((res = enable_net_traffic(net, pegasus->usb))) {
10535b2fc499SJeff Garzik 		if (netif_msg_ifup(pegasus))
10545b2fc499SJeff Garzik 			pr_debug("%s: can't enable_net_traffic() - %d\n",
10555b2fc499SJeff Garzik 					net->name, res);
10565b2fc499SJeff Garzik 		res = -EIO;
10575b2fc499SJeff Garzik 		usb_kill_urb(pegasus->rx_urb);
10585b2fc499SJeff Garzik 		usb_kill_urb(pegasus->intr_urb);
10595b2fc499SJeff Garzik 		free_skb_pool(pegasus);
10605b2fc499SJeff Garzik 		goto exit;
10615b2fc499SJeff Garzik 	}
10625b2fc499SJeff Garzik 	set_carrier(net);
10635b2fc499SJeff Garzik 	netif_start_queue(net);
10645b2fc499SJeff Garzik 	if (netif_msg_ifup(pegasus))
10655b2fc499SJeff Garzik 		pr_debug("%s: open\n", net->name);
10665b2fc499SJeff Garzik 	res = 0;
10675b2fc499SJeff Garzik exit:
10685b2fc499SJeff Garzik 	return res;
10695b2fc499SJeff Garzik }
10705b2fc499SJeff Garzik 
10715b2fc499SJeff Garzik static int pegasus_close(struct net_device *net)
10725b2fc499SJeff Garzik {
10735b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
10745b2fc499SJeff Garzik 
10755b2fc499SJeff Garzik 	netif_stop_queue(net);
10765b2fc499SJeff Garzik 	if (!(pegasus->flags & PEGASUS_UNPLUG))
10775b2fc499SJeff Garzik 		disable_net_traffic(pegasus);
10785b2fc499SJeff Garzik 	tasklet_kill(&pegasus->rx_tl);
10795b2fc499SJeff Garzik 	unlink_all_urbs(pegasus);
10805b2fc499SJeff Garzik 
10815b2fc499SJeff Garzik 	return 0;
10825b2fc499SJeff Garzik }
10835b2fc499SJeff Garzik 
10845b2fc499SJeff Garzik static void pegasus_get_drvinfo(struct net_device *dev,
10855b2fc499SJeff Garzik 				struct ethtool_drvinfo *info)
10865b2fc499SJeff Garzik {
10875b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
10885b2fc499SJeff Garzik 	strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
10895b2fc499SJeff Garzik 	strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
10905b2fc499SJeff Garzik 	usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
10915b2fc499SJeff Garzik }
10925b2fc499SJeff Garzik 
10935b2fc499SJeff Garzik /* also handles three patterns of some kind in hardware */
10945b2fc499SJeff Garzik #define	WOL_SUPPORTED	(WAKE_MAGIC|WAKE_PHY)
10955b2fc499SJeff Garzik 
10965b2fc499SJeff Garzik static void
10975b2fc499SJeff Garzik pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10985b2fc499SJeff Garzik {
10995b2fc499SJeff Garzik 	pegasus_t	*pegasus = netdev_priv(dev);
11005b2fc499SJeff Garzik 
11015b2fc499SJeff Garzik 	wol->supported = WAKE_MAGIC | WAKE_PHY;
11025b2fc499SJeff Garzik 	wol->wolopts = pegasus->wolopts;
11035b2fc499SJeff Garzik }
11045b2fc499SJeff Garzik 
11055b2fc499SJeff Garzik static int
11065b2fc499SJeff Garzik pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
11075b2fc499SJeff Garzik {
11085b2fc499SJeff Garzik 	pegasus_t	*pegasus = netdev_priv(dev);
11095b2fc499SJeff Garzik 	u8		reg78 = 0x04;
11105b2fc499SJeff Garzik 
11115b2fc499SJeff Garzik 	if (wol->wolopts & ~WOL_SUPPORTED)
11125b2fc499SJeff Garzik 		return -EINVAL;
11135b2fc499SJeff Garzik 
11145b2fc499SJeff Garzik 	if (wol->wolopts & WAKE_MAGIC)
11155b2fc499SJeff Garzik 		reg78 |= 0x80;
11165b2fc499SJeff Garzik 	if (wol->wolopts & WAKE_PHY)
11175b2fc499SJeff Garzik 		reg78 |= 0x40;
11185b2fc499SJeff Garzik 	/* FIXME this 0x10 bit still needs to get set in the chip... */
11195b2fc499SJeff Garzik 	if (wol->wolopts)
11205b2fc499SJeff Garzik 		pegasus->eth_regs[0] |= 0x10;
11215b2fc499SJeff Garzik 	else
11225b2fc499SJeff Garzik 		pegasus->eth_regs[0] &= ~0x10;
11235b2fc499SJeff Garzik 	pegasus->wolopts = wol->wolopts;
11245b2fc499SJeff Garzik 	return set_register(pegasus, WakeupControl, reg78);
11255b2fc499SJeff Garzik }
11265b2fc499SJeff Garzik 
11275b2fc499SJeff Garzik static inline void pegasus_reset_wol(struct net_device *dev)
11285b2fc499SJeff Garzik {
11295b2fc499SJeff Garzik 	struct ethtool_wolinfo wol;
11305b2fc499SJeff Garzik 
11315b2fc499SJeff Garzik 	memset(&wol, 0, sizeof wol);
11325b2fc499SJeff Garzik 	(void) pegasus_set_wol(dev, &wol);
11335b2fc499SJeff Garzik }
11345b2fc499SJeff Garzik 
11355b2fc499SJeff Garzik static int
11365b2fc499SJeff Garzik pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
11375b2fc499SJeff Garzik {
11385b2fc499SJeff Garzik 	pegasus_t *pegasus;
11395b2fc499SJeff Garzik 
11405b2fc499SJeff Garzik 	pegasus = netdev_priv(dev);
11415b2fc499SJeff Garzik 	mii_ethtool_gset(&pegasus->mii, ecmd);
11425b2fc499SJeff Garzik 	return 0;
11435b2fc499SJeff Garzik }
11445b2fc499SJeff Garzik 
11455b2fc499SJeff Garzik static int
11465b2fc499SJeff Garzik pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
11475b2fc499SJeff Garzik {
11485b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
11495b2fc499SJeff Garzik 	return mii_ethtool_sset(&pegasus->mii, ecmd);
11505b2fc499SJeff Garzik }
11515b2fc499SJeff Garzik 
11525b2fc499SJeff Garzik static int pegasus_nway_reset(struct net_device *dev)
11535b2fc499SJeff Garzik {
11545b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
11555b2fc499SJeff Garzik 	return mii_nway_restart(&pegasus->mii);
11565b2fc499SJeff Garzik }
11575b2fc499SJeff Garzik 
11585b2fc499SJeff Garzik static u32 pegasus_get_link(struct net_device *dev)
11595b2fc499SJeff Garzik {
11605b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
11615b2fc499SJeff Garzik 	return mii_link_ok(&pegasus->mii);
11625b2fc499SJeff Garzik }
11635b2fc499SJeff Garzik 
11645b2fc499SJeff Garzik static u32 pegasus_get_msglevel(struct net_device *dev)
11655b2fc499SJeff Garzik {
11665b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
11675b2fc499SJeff Garzik 	return pegasus->msg_enable;
11685b2fc499SJeff Garzik }
11695b2fc499SJeff Garzik 
11705b2fc499SJeff Garzik static void pegasus_set_msglevel(struct net_device *dev, u32 v)
11715b2fc499SJeff Garzik {
11725b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
11735b2fc499SJeff Garzik 	pegasus->msg_enable = v;
11745b2fc499SJeff Garzik }
11755b2fc499SJeff Garzik 
11765b2fc499SJeff Garzik static struct ethtool_ops ops = {
11775b2fc499SJeff Garzik 	.get_drvinfo = pegasus_get_drvinfo,
11785b2fc499SJeff Garzik 	.get_settings = pegasus_get_settings,
11795b2fc499SJeff Garzik 	.set_settings = pegasus_set_settings,
11805b2fc499SJeff Garzik 	.nway_reset = pegasus_nway_reset,
11815b2fc499SJeff Garzik 	.get_link = pegasus_get_link,
11825b2fc499SJeff Garzik 	.get_msglevel = pegasus_get_msglevel,
11835b2fc499SJeff Garzik 	.set_msglevel = pegasus_set_msglevel,
11845b2fc499SJeff Garzik 	.get_wol = pegasus_get_wol,
11855b2fc499SJeff Garzik 	.set_wol = pegasus_set_wol,
11865b2fc499SJeff Garzik };
11875b2fc499SJeff Garzik 
11885b2fc499SJeff Garzik static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
11895b2fc499SJeff Garzik {
11905b2fc499SJeff Garzik 	__u16 *data = (__u16 *) & rq->ifr_ifru;
11915b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
11925b2fc499SJeff Garzik 	int res;
11935b2fc499SJeff Garzik 
11945b2fc499SJeff Garzik 	switch (cmd) {
11955b2fc499SJeff Garzik 	case SIOCDEVPRIVATE:
11965b2fc499SJeff Garzik 		data[0] = pegasus->phy;
11975b2fc499SJeff Garzik 	case SIOCDEVPRIVATE + 1:
11985b2fc499SJeff Garzik 		read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
11995b2fc499SJeff Garzik 		res = 0;
12005b2fc499SJeff Garzik 		break;
12015b2fc499SJeff Garzik 	case SIOCDEVPRIVATE + 2:
12025b2fc499SJeff Garzik 		if (!capable(CAP_NET_ADMIN))
12035b2fc499SJeff Garzik 			return -EPERM;
12045b2fc499SJeff Garzik 		write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
12055b2fc499SJeff Garzik 		res = 0;
12065b2fc499SJeff Garzik 		break;
12075b2fc499SJeff Garzik 	default:
12085b2fc499SJeff Garzik 		res = -EOPNOTSUPP;
12095b2fc499SJeff Garzik 	}
12105b2fc499SJeff Garzik 	return res;
12115b2fc499SJeff Garzik }
12125b2fc499SJeff Garzik 
12135b2fc499SJeff Garzik static void pegasus_set_multicast(struct net_device *net)
12145b2fc499SJeff Garzik {
12155b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
12165b2fc499SJeff Garzik 
12175b2fc499SJeff Garzik 	if (net->flags & IFF_PROMISC) {
12185b2fc499SJeff Garzik 		pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
12195b2fc499SJeff Garzik 		if (netif_msg_link(pegasus))
12205b2fc499SJeff Garzik 			pr_info("%s: Promiscuous mode enabled.\n", net->name);
1221e000ea13SDavid Brownell 	} else if (net->mc_count || (net->flags & IFF_ALLMULTI)) {
12225b2fc499SJeff Garzik 		pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
12235b2fc499SJeff Garzik 		pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
12245b2fc499SJeff Garzik 		if (netif_msg_link(pegasus))
1225cda2836dSDavid Brownell 			pr_debug("%s: set allmulti\n", net->name);
12265b2fc499SJeff Garzik 	} else {
12275b2fc499SJeff Garzik 		pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
12285b2fc499SJeff Garzik 		pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
12295b2fc499SJeff Garzik 	}
12305b2fc499SJeff Garzik 
1231e000ea13SDavid Brownell 	pegasus->ctrl_urb->status = 0;
1232e000ea13SDavid Brownell 
12335b2fc499SJeff Garzik 	pegasus->flags |= ETH_REGS_CHANGE;
12345b2fc499SJeff Garzik 	ctrl_callback(pegasus->ctrl_urb);
12355b2fc499SJeff Garzik }
12365b2fc499SJeff Garzik 
12375b2fc499SJeff Garzik static __u8 mii_phy_probe(pegasus_t * pegasus)
12385b2fc499SJeff Garzik {
12395b2fc499SJeff Garzik 	int i;
12405b2fc499SJeff Garzik 	__u16 tmp;
12415b2fc499SJeff Garzik 
12425b2fc499SJeff Garzik 	for (i = 0; i < 32; i++) {
12435b2fc499SJeff Garzik 		read_mii_word(pegasus, i, MII_BMSR, &tmp);
12445b2fc499SJeff Garzik 		if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
12455b2fc499SJeff Garzik 			continue;
12465b2fc499SJeff Garzik 		else
12475b2fc499SJeff Garzik 			return i;
12485b2fc499SJeff Garzik 	}
12495b2fc499SJeff Garzik 
12505b2fc499SJeff Garzik 	return 0xff;
12515b2fc499SJeff Garzik }
12525b2fc499SJeff Garzik 
12535b2fc499SJeff Garzik static inline void setup_pegasus_II(pegasus_t * pegasus)
12545b2fc499SJeff Garzik {
12555b2fc499SJeff Garzik 	__u8 data = 0xa5;
12565b2fc499SJeff Garzik 
12575b2fc499SJeff Garzik 	set_register(pegasus, Reg1d, 0);
12585b2fc499SJeff Garzik 	set_register(pegasus, Reg7b, 1);
12595b2fc499SJeff Garzik 	mdelay(100);
12605b2fc499SJeff Garzik 	if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
12615b2fc499SJeff Garzik 		set_register(pegasus, Reg7b, 0);
12625b2fc499SJeff Garzik 	else
12635b2fc499SJeff Garzik 		set_register(pegasus, Reg7b, 2);
12645b2fc499SJeff Garzik 
12655b2fc499SJeff Garzik 	set_register(pegasus, 0x83, data);
12665b2fc499SJeff Garzik 	get_registers(pegasus, 0x83, 1, &data);
12675b2fc499SJeff Garzik 
12685b2fc499SJeff Garzik 	if (data == 0xa5) {
12695b2fc499SJeff Garzik 		pegasus->chip = 0x8513;
12705b2fc499SJeff Garzik 	} else {
12715b2fc499SJeff Garzik 		pegasus->chip = 0;
12725b2fc499SJeff Garzik 	}
12735b2fc499SJeff Garzik 
12745b2fc499SJeff Garzik 	set_register(pegasus, 0x80, 0xc0);
12755b2fc499SJeff Garzik 	set_register(pegasus, 0x83, 0xff);
12765b2fc499SJeff Garzik 	set_register(pegasus, 0x84, 0x01);
12775b2fc499SJeff Garzik 
12785b2fc499SJeff Garzik 	if (pegasus->features & HAS_HOME_PNA && mii_mode)
12795b2fc499SJeff Garzik 		set_register(pegasus, Reg81, 6);
12805b2fc499SJeff Garzik 	else
12815b2fc499SJeff Garzik 		set_register(pegasus, Reg81, 2);
12825b2fc499SJeff Garzik }
12835b2fc499SJeff Garzik 
12845b2fc499SJeff Garzik 
1285cda2836dSDavid Brownell static int pegasus_count;
12865b2fc499SJeff Garzik static struct workqueue_struct *pegasus_workqueue = NULL;
12875b2fc499SJeff Garzik #define CARRIER_CHECK_DELAY (2 * HZ)
12885b2fc499SJeff Garzik 
12895b2fc499SJeff Garzik static void check_carrier(struct work_struct *work)
12905b2fc499SJeff Garzik {
12915b2fc499SJeff Garzik 	pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
12925b2fc499SJeff Garzik 	set_carrier(pegasus->net);
12935b2fc499SJeff Garzik 	if (!(pegasus->flags & PEGASUS_UNPLUG)) {
12945b2fc499SJeff Garzik 		queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
12955b2fc499SJeff Garzik 			CARRIER_CHECK_DELAY);
12965b2fc499SJeff Garzik 	}
12975b2fc499SJeff Garzik }
12985b2fc499SJeff Garzik 
12994f63135eSBen Collins static int pegasus_blacklisted(struct usb_device *udev)
13004f63135eSBen Collins {
13014f63135eSBen Collins 	struct usb_device_descriptor *udd = &udev->descriptor;
13024f63135eSBen Collins 
13034f63135eSBen Collins 	/* Special quirk to keep the driver from handling the Belkin Bluetooth
13044f63135eSBen Collins 	 * dongle which happens to have the same ID.
13054f63135eSBen Collins 	 */
1306e3453f63SMichael Buesch 	if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
1307e3453f63SMichael Buesch 	    (udd->idProduct == cpu_to_le16(0x0121)) &&
13084f63135eSBen Collins 	    (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
13094f63135eSBen Collins 	    (udd->bDeviceProtocol == 1))
13104f63135eSBen Collins 		return 1;
13114f63135eSBen Collins 
13124f63135eSBen Collins 	return 0;
13134f63135eSBen Collins }
13144f63135eSBen Collins 
1315cda2836dSDavid Brownell /* we rely on probe() and remove() being serialized so we
1316cda2836dSDavid Brownell  * don't need extra locking on pegasus_count.
1317cda2836dSDavid Brownell  */
1318cda2836dSDavid Brownell static void pegasus_dec_workqueue(void)
1319cda2836dSDavid Brownell {
1320cda2836dSDavid Brownell 	pegasus_count--;
1321cda2836dSDavid Brownell 	if (pegasus_count == 0) {
1322cda2836dSDavid Brownell 		destroy_workqueue(pegasus_workqueue);
1323cda2836dSDavid Brownell 		pegasus_workqueue = NULL;
1324cda2836dSDavid Brownell 	}
1325cda2836dSDavid Brownell }
1326cda2836dSDavid Brownell 
13275b2fc499SJeff Garzik static int pegasus_probe(struct usb_interface *intf,
13285b2fc499SJeff Garzik 			 const struct usb_device_id *id)
13295b2fc499SJeff Garzik {
13305b2fc499SJeff Garzik 	struct usb_device *dev = interface_to_usbdev(intf);
13315b2fc499SJeff Garzik 	struct net_device *net;
13325b2fc499SJeff Garzik 	pegasus_t *pegasus;
13335b2fc499SJeff Garzik 	int dev_index = id - pegasus_ids;
13345b2fc499SJeff Garzik 	int res = -ENOMEM;
13355b2fc499SJeff Garzik 
1336cda2836dSDavid Brownell 	if (pegasus_blacklisted(dev))
1337cda2836dSDavid Brownell 		return -ENODEV;
13384f63135eSBen Collins 
1339cda2836dSDavid Brownell 	if (pegasus_count == 0) {
1340cda2836dSDavid Brownell 		pegasus_workqueue = create_singlethread_workqueue("pegasus");
1341cda2836dSDavid Brownell 		if (!pegasus_workqueue)
1342cda2836dSDavid Brownell 			return -ENOMEM;
13434f63135eSBen Collins 	}
1344cda2836dSDavid Brownell 	pegasus_count++;
1345cda2836dSDavid Brownell 
1346cda2836dSDavid Brownell 	usb_get_dev(dev);
13474f63135eSBen Collins 
13485b2fc499SJeff Garzik 	net = alloc_etherdev(sizeof(struct pegasus));
13495b2fc499SJeff Garzik 	if (!net) {
13505b2fc499SJeff Garzik 		dev_err(&intf->dev, "can't allocate %s\n", "device");
13515b2fc499SJeff Garzik 		goto out;
13525b2fc499SJeff Garzik 	}
13535b2fc499SJeff Garzik 
13545b2fc499SJeff Garzik 	pegasus = netdev_priv(net);
13555b2fc499SJeff Garzik 	pegasus->dev_index = dev_index;
13565b2fc499SJeff Garzik 	init_waitqueue_head(&pegasus->ctrl_wait);
13575b2fc499SJeff Garzik 
13585b2fc499SJeff Garzik 	if (!alloc_urbs(pegasus)) {
13595b2fc499SJeff Garzik 		dev_err(&intf->dev, "can't allocate %s\n", "urbs");
13605b2fc499SJeff Garzik 		goto out1;
13615b2fc499SJeff Garzik 	}
13625b2fc499SJeff Garzik 
13635b2fc499SJeff Garzik 	tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
13645b2fc499SJeff Garzik 
13655b2fc499SJeff Garzik 	INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
13665b2fc499SJeff Garzik 
13675b2fc499SJeff Garzik 	pegasus->intf = intf;
13685b2fc499SJeff Garzik 	pegasus->usb = dev;
13695b2fc499SJeff Garzik 	pegasus->net = net;
13708cb89571SOliver Neukum 
13718cb89571SOliver Neukum 
13725b2fc499SJeff Garzik 	net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
13738cb89571SOliver Neukum 	net->netdev_ops = &pegasus_netdev_ops;
13745b2fc499SJeff Garzik 	SET_ETHTOOL_OPS(net, &ops);
13755b2fc499SJeff Garzik 	pegasus->mii.dev = net;
13765b2fc499SJeff Garzik 	pegasus->mii.mdio_read = mdio_read;
13775b2fc499SJeff Garzik 	pegasus->mii.mdio_write = mdio_write;
13785b2fc499SJeff Garzik 	pegasus->mii.phy_id_mask = 0x1f;
13795b2fc499SJeff Garzik 	pegasus->mii.reg_num_mask = 0x1f;
13805b2fc499SJeff Garzik 	spin_lock_init(&pegasus->rx_pool_lock);
13815b2fc499SJeff Garzik 	pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
13825b2fc499SJeff Garzik 				| NETIF_MSG_PROBE | NETIF_MSG_LINK);
13835b2fc499SJeff Garzik 
13845b2fc499SJeff Garzik 	pegasus->features = usb_dev_id[dev_index].private;
13855b2fc499SJeff Garzik 	get_interrupt_interval(pegasus);
13865b2fc499SJeff Garzik 	if (reset_mac(pegasus)) {
13875b2fc499SJeff Garzik 		dev_err(&intf->dev, "can't reset MAC\n");
13885b2fc499SJeff Garzik 		res = -EIO;
13895b2fc499SJeff Garzik 		goto out2;
13905b2fc499SJeff Garzik 	}
13915b2fc499SJeff Garzik 	set_ethernet_addr(pegasus);
13925b2fc499SJeff Garzik 	fill_skb_pool(pegasus);
13935b2fc499SJeff Garzik 	if (pegasus->features & PEGASUS_II) {
13945b2fc499SJeff Garzik 		dev_info(&intf->dev, "setup Pegasus II specific registers\n");
13955b2fc499SJeff Garzik 		setup_pegasus_II(pegasus);
13965b2fc499SJeff Garzik 	}
13975b2fc499SJeff Garzik 	pegasus->phy = mii_phy_probe(pegasus);
13985b2fc499SJeff Garzik 	if (pegasus->phy == 0xff) {
13995b2fc499SJeff Garzik 		dev_warn(&intf->dev, "can't locate MII phy, using default\n");
14005b2fc499SJeff Garzik 		pegasus->phy = 1;
14015b2fc499SJeff Garzik 	}
14025b2fc499SJeff Garzik 	pegasus->mii.phy_id = pegasus->phy;
14035b2fc499SJeff Garzik 	usb_set_intfdata(intf, pegasus);
14045b2fc499SJeff Garzik 	SET_NETDEV_DEV(net, &intf->dev);
14055b2fc499SJeff Garzik 	pegasus_reset_wol(net);
14065b2fc499SJeff Garzik 	res = register_netdev(net);
14075b2fc499SJeff Garzik 	if (res)
14085b2fc499SJeff Garzik 		goto out3;
14095b2fc499SJeff Garzik 	queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
14105b2fc499SJeff Garzik 				CARRIER_CHECK_DELAY);
14115b2fc499SJeff Garzik 
1412e174961cSJohannes Berg 	dev_info(&intf->dev, "%s, %s, %pM\n",
14135b2fc499SJeff Garzik 		 net->name,
14145b2fc499SJeff Garzik 		 usb_dev_id[dev_index].name,
1415e174961cSJohannes Berg 		 net->dev_addr);
14165b2fc499SJeff Garzik 	return 0;
14175b2fc499SJeff Garzik 
14185b2fc499SJeff Garzik out3:
14195b2fc499SJeff Garzik 	usb_set_intfdata(intf, NULL);
14205b2fc499SJeff Garzik 	free_skb_pool(pegasus);
14215b2fc499SJeff Garzik out2:
14225b2fc499SJeff Garzik 	free_all_urbs(pegasus);
14235b2fc499SJeff Garzik out1:
14245b2fc499SJeff Garzik 	free_netdev(net);
14255b2fc499SJeff Garzik out:
14265b2fc499SJeff Garzik 	usb_put_dev(dev);
1427cda2836dSDavid Brownell 	pegasus_dec_workqueue();
14285b2fc499SJeff Garzik 	return res;
14295b2fc499SJeff Garzik }
14305b2fc499SJeff Garzik 
14315b2fc499SJeff Garzik static void pegasus_disconnect(struct usb_interface *intf)
14325b2fc499SJeff Garzik {
14335b2fc499SJeff Garzik 	struct pegasus *pegasus = usb_get_intfdata(intf);
14345b2fc499SJeff Garzik 
14355b2fc499SJeff Garzik 	usb_set_intfdata(intf, NULL);
14365b2fc499SJeff Garzik 	if (!pegasus) {
14375b2fc499SJeff Garzik 		dev_dbg(&intf->dev, "unregistering non-bound device?\n");
14385b2fc499SJeff Garzik 		return;
14395b2fc499SJeff Garzik 	}
14405b2fc499SJeff Garzik 
14415b2fc499SJeff Garzik 	pegasus->flags |= PEGASUS_UNPLUG;
14425b2fc499SJeff Garzik 	cancel_delayed_work(&pegasus->carrier_check);
14435b2fc499SJeff Garzik 	unregister_netdev(pegasus->net);
14445b2fc499SJeff Garzik 	usb_put_dev(interface_to_usbdev(intf));
14455b2fc499SJeff Garzik 	unlink_all_urbs(pegasus);
14465b2fc499SJeff Garzik 	free_all_urbs(pegasus);
14475b2fc499SJeff Garzik 	free_skb_pool(pegasus);
14485b2fc499SJeff Garzik 	if (pegasus->rx_skb != NULL) {
14495b2fc499SJeff Garzik 		dev_kfree_skb(pegasus->rx_skb);
14505b2fc499SJeff Garzik 		pegasus->rx_skb = NULL;
14515b2fc499SJeff Garzik 	}
14525b2fc499SJeff Garzik 	free_netdev(pegasus->net);
1453cda2836dSDavid Brownell 	pegasus_dec_workqueue();
14545b2fc499SJeff Garzik }
14555b2fc499SJeff Garzik 
14565b2fc499SJeff Garzik static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
14575b2fc499SJeff Garzik {
14585b2fc499SJeff Garzik 	struct pegasus *pegasus = usb_get_intfdata(intf);
14595b2fc499SJeff Garzik 
14605b2fc499SJeff Garzik 	netif_device_detach (pegasus->net);
14615b2fc499SJeff Garzik 	cancel_delayed_work(&pegasus->carrier_check);
14625b2fc499SJeff Garzik 	if (netif_running(pegasus->net)) {
14635b2fc499SJeff Garzik 		usb_kill_urb(pegasus->rx_urb);
14645b2fc499SJeff Garzik 		usb_kill_urb(pegasus->intr_urb);
14655b2fc499SJeff Garzik 	}
14665b2fc499SJeff Garzik 	return 0;
14675b2fc499SJeff Garzik }
14685b2fc499SJeff Garzik 
14695b2fc499SJeff Garzik static int pegasus_resume (struct usb_interface *intf)
14705b2fc499SJeff Garzik {
14715b2fc499SJeff Garzik 	struct pegasus *pegasus = usb_get_intfdata(intf);
14725b2fc499SJeff Garzik 
14735b2fc499SJeff Garzik 	netif_device_attach (pegasus->net);
14745b2fc499SJeff Garzik 	if (netif_running(pegasus->net)) {
14755b2fc499SJeff Garzik 		pegasus->rx_urb->status = 0;
14765b2fc499SJeff Garzik 		pegasus->rx_urb->actual_length = 0;
14775b2fc499SJeff Garzik 		read_bulk_callback(pegasus->rx_urb);
14785b2fc499SJeff Garzik 
14795b2fc499SJeff Garzik 		pegasus->intr_urb->status = 0;
14805b2fc499SJeff Garzik 		pegasus->intr_urb->actual_length = 0;
14815b2fc499SJeff Garzik 		intr_callback(pegasus->intr_urb);
14825b2fc499SJeff Garzik 	}
14835b2fc499SJeff Garzik 	queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
14845b2fc499SJeff Garzik 				CARRIER_CHECK_DELAY);
14855b2fc499SJeff Garzik 	return 0;
14865b2fc499SJeff Garzik }
14875b2fc499SJeff Garzik 
14888cb89571SOliver Neukum static const struct net_device_ops pegasus_netdev_ops = {
14898cb89571SOliver Neukum 	.ndo_open =			pegasus_open,
14908cb89571SOliver Neukum 	.ndo_stop =			pegasus_close,
14918cb89571SOliver Neukum 	.ndo_do_ioctl =			pegasus_ioctl,
14928cb89571SOliver Neukum 	.ndo_start_xmit =		pegasus_start_xmit,
14938cb89571SOliver Neukum 	.ndo_set_multicast_list =	pegasus_set_multicast,
14948cb89571SOliver Neukum 	.ndo_get_stats =		pegasus_netdev_stats,
14958cb89571SOliver Neukum 	.ndo_tx_timeout =		pegasus_tx_timeout,
1496*635ecaa7SBen Hutchings 	.ndo_change_mtu =		eth_change_mtu,
1497240c102dSBen Hutchings 	.ndo_set_mac_address =		eth_mac_addr,
1498240c102dSBen Hutchings 	.ndo_validate_addr =		eth_validate_addr,
14998cb89571SOliver Neukum };
15008cb89571SOliver Neukum 
15015b2fc499SJeff Garzik static struct usb_driver pegasus_driver = {
15025b2fc499SJeff Garzik 	.name = driver_name,
15035b2fc499SJeff Garzik 	.probe = pegasus_probe,
15045b2fc499SJeff Garzik 	.disconnect = pegasus_disconnect,
15055b2fc499SJeff Garzik 	.id_table = pegasus_ids,
15065b2fc499SJeff Garzik 	.suspend = pegasus_suspend,
15075b2fc499SJeff Garzik 	.resume = pegasus_resume,
15085b2fc499SJeff Garzik };
15095b2fc499SJeff Garzik 
1510cda2836dSDavid Brownell static void __init parse_id(char *id)
15115b2fc499SJeff Garzik {
15125b2fc499SJeff Garzik 	unsigned int vendor_id=0, device_id=0, flags=0, i=0;
15135b2fc499SJeff Garzik 	char *token, *name=NULL;
15145b2fc499SJeff Garzik 
15155b2fc499SJeff Garzik 	if ((token = strsep(&id, ":")) != NULL)
15165b2fc499SJeff Garzik 		name = token;
15175b2fc499SJeff Garzik 	/* name now points to a null terminated string*/
15185b2fc499SJeff Garzik 	if ((token = strsep(&id, ":")) != NULL)
15195b2fc499SJeff Garzik 		vendor_id = simple_strtoul(token, NULL, 16);
15205b2fc499SJeff Garzik 	if ((token = strsep(&id, ":")) != NULL)
15215b2fc499SJeff Garzik 		device_id = simple_strtoul(token, NULL, 16);
15225b2fc499SJeff Garzik 	flags = simple_strtoul(id, NULL, 16);
15235b2fc499SJeff Garzik 	pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
15245b2fc499SJeff Garzik 	        driver_name, name, vendor_id, device_id, flags);
15255b2fc499SJeff Garzik 
15265b2fc499SJeff Garzik 	if (vendor_id > 0x10000 || vendor_id == 0)
15275b2fc499SJeff Garzik 		return;
15285b2fc499SJeff Garzik 	if (device_id > 0x10000 || device_id == 0)
15295b2fc499SJeff Garzik 		return;
15305b2fc499SJeff Garzik 
15315b2fc499SJeff Garzik 	for (i=0; usb_dev_id[i].name; i++);
15325b2fc499SJeff Garzik 	usb_dev_id[i].name = name;
15335b2fc499SJeff Garzik 	usb_dev_id[i].vendor = vendor_id;
15345b2fc499SJeff Garzik 	usb_dev_id[i].device = device_id;
15355b2fc499SJeff Garzik 	usb_dev_id[i].private = flags;
15365b2fc499SJeff Garzik 	pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
15375b2fc499SJeff Garzik 	pegasus_ids[i].idVendor = vendor_id;
15385b2fc499SJeff Garzik 	pegasus_ids[i].idProduct = device_id;
15395b2fc499SJeff Garzik }
15405b2fc499SJeff Garzik 
15415b2fc499SJeff Garzik static int __init pegasus_init(void)
15425b2fc499SJeff Garzik {
15435b2fc499SJeff Garzik 	pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
15445b2fc499SJeff Garzik 	if (devid)
15455b2fc499SJeff Garzik 		parse_id(devid);
15465b2fc499SJeff Garzik 	return usb_register(&pegasus_driver);
15475b2fc499SJeff Garzik }
15485b2fc499SJeff Garzik 
15495b2fc499SJeff Garzik static void __exit pegasus_exit(void)
15505b2fc499SJeff Garzik {
15515b2fc499SJeff Garzik 	usb_deregister(&pegasus_driver);
15525b2fc499SJeff Garzik }
15535b2fc499SJeff Garzik 
15545b2fc499SJeff Garzik module_init(pegasus_init);
15555b2fc499SJeff Garzik module_exit(pegasus_exit);
1556