xref: /openbmc/linux/drivers/net/usb/pegasus.c (revision 860e9538a9482bb84589f7d0718a7e6d0a944d58)
15b2fc499SJeff Garzik /*
2323b3496SPetko Manolov  *  Copyright (c) 1999-2013 Petko Manolov (petkan@nucleusys.com)
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.
29323b3496SPetko Manolov  *		...
30323b3496SPetko Manolov  *		v0.9.3	simplified [get|set]_register(s), async update registers
31323b3496SPetko Manolov  *			logic revisited, receive skb_pool removed.
325b2fc499SJeff Garzik  */
335b2fc499SJeff Garzik 
345b2fc499SJeff Garzik #include <linux/sched.h>
355b2fc499SJeff Garzik #include <linux/slab.h>
365b2fc499SJeff Garzik #include <linux/init.h>
375b2fc499SJeff Garzik #include <linux/delay.h>
385b2fc499SJeff Garzik #include <linux/netdevice.h>
395b2fc499SJeff Garzik #include <linux/etherdevice.h>
405b2fc499SJeff Garzik #include <linux/ethtool.h>
415b2fc499SJeff Garzik #include <linux/mii.h>
425b2fc499SJeff Garzik #include <linux/usb.h>
435b2fc499SJeff Garzik #include <linux/module.h>
445b2fc499SJeff Garzik #include <asm/byteorder.h>
455b2fc499SJeff Garzik #include <asm/uaccess.h>
465b2fc499SJeff Garzik #include "pegasus.h"
475b2fc499SJeff Garzik 
485b2fc499SJeff Garzik /*
495b2fc499SJeff Garzik  * Version Information
505b2fc499SJeff Garzik  */
51323b3496SPetko Manolov #define DRIVER_VERSION "v0.9.3 (2013/04/25)"
52323b3496SPetko Manolov #define DRIVER_AUTHOR "Petko Manolov <petkan@nucleusys.com>"
535b2fc499SJeff Garzik #define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
545b2fc499SJeff Garzik 
555b2fc499SJeff Garzik static const char driver_name[] = "pegasus";
565b2fc499SJeff Garzik 
575b2fc499SJeff Garzik #undef	PEGASUS_WRITE_EEPROM
585b2fc499SJeff Garzik #define	BMSR_MEDIA	(BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \
595b2fc499SJeff Garzik 			BMSR_100FULL | BMSR_ANEGCAPABLE)
605b2fc499SJeff Garzik 
61eb939922SRusty Russell static bool loopback;
62eb939922SRusty Russell static bool mii_mode;
635a9dbfe0SNicolas Kaiser static char *devid;
645b2fc499SJeff Garzik 
655b2fc499SJeff Garzik static struct usb_eth_dev usb_dev_id[] = {
665b2fc499SJeff Garzik #define	PEGASUS_DEV(pn, vid, pid, flags)	\
675b2fc499SJeff Garzik 	{.name = pn, .vendor = vid, .device = pid, .private = flags},
68ab854b24SChris Rankin #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
69ab854b24SChris Rankin 	PEGASUS_DEV(pn, vid, pid, flags)
705b2fc499SJeff Garzik #include "pegasus.h"
715b2fc499SJeff Garzik #undef	PEGASUS_DEV
72ab854b24SChris Rankin #undef	PEGASUS_DEV_CLASS
735b2fc499SJeff Garzik 	{NULL, 0, 0, 0},
745b2fc499SJeff Garzik 	{NULL, 0, 0, 0}
755b2fc499SJeff Garzik };
765b2fc499SJeff Garzik 
775b2fc499SJeff Garzik static struct usb_device_id pegasus_ids[] = {
785b2fc499SJeff Garzik #define	PEGASUS_DEV(pn, vid, pid, flags) \
795b2fc499SJeff Garzik 	{.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
80ab854b24SChris Rankin /*
81ab854b24SChris Rankin  * The Belkin F8T012xx1 bluetooth adaptor has the same vendor and product
82ab854b24SChris Rankin  * IDs as the Belkin F5D5050, so we need to teach the pegasus driver to
83ab854b24SChris Rankin  * ignore adaptors belonging to the "Wireless" class 0xE0. For this one
84ab854b24SChris Rankin  * case anyway, seeing as the pegasus is for "Wired" adaptors.
85ab854b24SChris Rankin  */
86ab854b24SChris Rankin #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
87ab854b24SChris Rankin 	{.match_flags = (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_CLASS), \
88ab854b24SChris Rankin 	.idVendor = vid, .idProduct = pid, .bDeviceClass = dclass},
895b2fc499SJeff Garzik #include "pegasus.h"
905b2fc499SJeff Garzik #undef	PEGASUS_DEV
91ab854b24SChris Rankin #undef	PEGASUS_DEV_CLASS
925b2fc499SJeff Garzik 	{},
935b2fc499SJeff Garzik 	{}
945b2fc499SJeff Garzik };
955b2fc499SJeff Garzik 
965b2fc499SJeff Garzik MODULE_AUTHOR(DRIVER_AUTHOR);
975b2fc499SJeff Garzik MODULE_DESCRIPTION(DRIVER_DESC);
985b2fc499SJeff Garzik MODULE_LICENSE("GPL");
995b2fc499SJeff Garzik module_param(loopback, bool, 0);
1005b2fc499SJeff Garzik module_param(mii_mode, bool, 0);
1015b2fc499SJeff Garzik module_param(devid, charp, 0);
1025b2fc499SJeff Garzik MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
1035b2fc499SJeff Garzik MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
1045b2fc499SJeff Garzik MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
1055b2fc499SJeff Garzik 
1065b2fc499SJeff Garzik /* use ethtool to change the level for any given device */
1075b2fc499SJeff Garzik static int msg_level = -1;
1085b2fc499SJeff Garzik module_param(msg_level, int, 0);
1095b2fc499SJeff Garzik MODULE_PARM_DESC(msg_level, "Override default message level");
1105b2fc499SJeff Garzik 
1115b2fc499SJeff Garzik MODULE_DEVICE_TABLE(usb, pegasus_ids);
1128cb89571SOliver Neukum static const struct net_device_ops pegasus_netdev_ops;
1135b2fc499SJeff Garzik 
114323b3496SPetko Manolov /*****/
115323b3496SPetko Manolov 
116323b3496SPetko Manolov static void async_ctrl_callback(struct urb *urb)
1175b2fc499SJeff Garzik {
118323b3496SPetko Manolov 	struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
119c94cb314SOliver Neukum 	int status = urb->status;
1205b2fc499SJeff Garzik 
121323b3496SPetko Manolov 	if (status < 0)
122323b3496SPetko Manolov 		dev_dbg(&urb->dev->dev, "%s failed with %d", __func__, status);
123323b3496SPetko Manolov 	kfree(req);
124323b3496SPetko Manolov 	usb_free_urb(urb);
1255b2fc499SJeff Garzik }
1265b2fc499SJeff Garzik 
127323b3496SPetko Manolov static int get_registers(pegasus_t *pegasus, __u16 indx, __u16 size, void *data)
1285b2fc499SJeff Garzik {
1295b2fc499SJeff Garzik 	int ret;
1305b2fc499SJeff Garzik 
131323b3496SPetko Manolov 	ret = usb_control_msg(pegasus->usb, usb_rcvctrlpipe(pegasus->usb, 0),
132323b3496SPetko Manolov 			      PEGASUS_REQ_GET_REGS, PEGASUS_REQT_READ, 0,
133323b3496SPetko Manolov 			      indx, data, size, 1000);
134323b3496SPetko Manolov 	if (ret < 0)
135323b3496SPetko Manolov 		netif_dbg(pegasus, drv, pegasus->net,
136323b3496SPetko Manolov 			  "%s returned %d\n", __func__, ret);
1375b2fc499SJeff Garzik 	return ret;
1385b2fc499SJeff Garzik }
1395b2fc499SJeff Garzik 
140323b3496SPetko Manolov static int set_registers(pegasus_t *pegasus, __u16 indx, __u16 size, void *data)
1415b2fc499SJeff Garzik {
1425b2fc499SJeff Garzik 	int ret;
1435b2fc499SJeff Garzik 
144323b3496SPetko Manolov 	ret = usb_control_msg(pegasus->usb, usb_sndctrlpipe(pegasus->usb, 0),
145323b3496SPetko Manolov 			      PEGASUS_REQ_SET_REGS, PEGASUS_REQT_WRITE, 0,
146323b3496SPetko Manolov 			      indx, data, size, 100);
147323b3496SPetko Manolov 	if (ret < 0)
148323b3496SPetko Manolov 		netif_dbg(pegasus, drv, pegasus->net,
149323b3496SPetko Manolov 			  "%s returned %d\n", __func__, ret);
1505b2fc499SJeff Garzik 	return ret;
1515b2fc499SJeff Garzik }
1525b2fc499SJeff Garzik 
1535b2fc499SJeff Garzik static int set_register(pegasus_t *pegasus, __u16 indx, __u8 data)
1545b2fc499SJeff Garzik {
1555b2fc499SJeff Garzik 	int ret;
1565b2fc499SJeff Garzik 
157323b3496SPetko Manolov 	ret = usb_control_msg(pegasus->usb, usb_sndctrlpipe(pegasus->usb, 0),
158323b3496SPetko Manolov 			      PEGASUS_REQ_SET_REG, PEGASUS_REQT_WRITE, data,
159323b3496SPetko Manolov 			      indx, &data, 1, 1000);
160323b3496SPetko Manolov 	if (ret < 0)
161323b3496SPetko Manolov 		netif_dbg(pegasus, drv, pegasus->net,
162323b3496SPetko Manolov 			  "%s returned %d\n", __func__, ret);
1635b2fc499SJeff Garzik 	return ret;
1645b2fc499SJeff Garzik }
1655b2fc499SJeff Garzik 
1665b2fc499SJeff Garzik static int update_eth_regs_async(pegasus_t *pegasus)
1675b2fc499SJeff Garzik {
168323b3496SPetko Manolov 	int ret = -ENOMEM;
169323b3496SPetko Manolov 	struct urb *async_urb;
170323b3496SPetko Manolov 	struct usb_ctrlrequest *req;
1715b2fc499SJeff Garzik 
172323b3496SPetko Manolov 	req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
173323b3496SPetko Manolov 	if (req == NULL)
174323b3496SPetko Manolov 		return ret;
1755b2fc499SJeff Garzik 
176323b3496SPetko Manolov 	async_urb = usb_alloc_urb(0, GFP_ATOMIC);
177323b3496SPetko Manolov 	if (async_urb == NULL) {
178323b3496SPetko Manolov 		kfree(req);
179323b3496SPetko Manolov 		return ret;
180323b3496SPetko Manolov 	}
181323b3496SPetko Manolov 	req->bRequestType = PEGASUS_REQT_WRITE;
182323b3496SPetko Manolov 	req->bRequest = PEGASUS_REQ_SET_REGS;
183323b3496SPetko Manolov 	req->wValue = cpu_to_le16(0);
184323b3496SPetko Manolov 	req->wIndex = cpu_to_le16(EthCtrl0);
185323b3496SPetko Manolov 	req->wLength = cpu_to_le16(3);
1865b2fc499SJeff Garzik 
187323b3496SPetko Manolov 	usb_fill_control_urb(async_urb, pegasus->usb,
188323b3496SPetko Manolov 			     usb_sndctrlpipe(pegasus->usb, 0), (void *)req,
189323b3496SPetko Manolov 			     pegasus->eth_regs, 3, async_ctrl_callback, req);
190323b3496SPetko Manolov 
191323b3496SPetko Manolov 	ret = usb_submit_urb(async_urb, GFP_ATOMIC);
192323b3496SPetko Manolov 	if (ret) {
1935b2fc499SJeff Garzik 		if (ret == -ENODEV)
1945b2fc499SJeff Garzik 			netif_device_detach(pegasus->net);
195a475f603SJoe Perches 		netif_err(pegasus, drv, pegasus->net,
196323b3496SPetko Manolov 			  "%s returned %d\n", __func__, ret);
1975b2fc499SJeff Garzik 	}
1985b2fc499SJeff Garzik 	return ret;
1995b2fc499SJeff Garzik }
2005b2fc499SJeff Garzik 
2012bd64701SPetko Manolov static int __mii_op(pegasus_t *p, __u8 phy, __u8 indx, __u16 *regd, __u8 cmd)
2025b2fc499SJeff Garzik {
2035b2fc499SJeff Garzik 	int i;
2045b2fc499SJeff Garzik 	__u8 data[4] = { phy, 0, 0, indx };
2055b2fc499SJeff Garzik 	__le16 regdi;
2062bd64701SPetko Manolov 	int ret = -ETIMEDOUT;
2075b2fc499SJeff Garzik 
2082bd64701SPetko Manolov 	if (cmd & PHY_WRITE) {
2092bd64701SPetko Manolov 		__le16 *t = (__le16 *) & data[1];
2102bd64701SPetko Manolov 		*t = cpu_to_le16(*regd);
2112bd64701SPetko Manolov 	}
2122bd64701SPetko Manolov 	set_register(p, PhyCtrl, 0);
2132bd64701SPetko Manolov 	set_registers(p, PhyAddr, sizeof(data), data);
2142bd64701SPetko Manolov 	set_register(p, PhyCtrl, (indx | cmd));
2155b2fc499SJeff Garzik 	for (i = 0; i < REG_TIMEOUT; i++) {
2162bd64701SPetko Manolov 		ret = get_registers(p, PhyCtrl, 1, data);
2172bd64701SPetko Manolov 		if (ret < 0)
2185b2fc499SJeff Garzik 			goto fail;
2195b2fc499SJeff Garzik 		if (data[0] & PHY_DONE)
2205b2fc499SJeff Garzik 			break;
2215b2fc499SJeff Garzik 	}
222a475f603SJoe Perches 	if (i >= REG_TIMEOUT)
223a475f603SJoe Perches 		goto fail;
2242bd64701SPetko Manolov 	if (cmd & PHY_READ) {
2252bd64701SPetko Manolov 		ret = get_registers(p, PhyData, 2, &regdi);
2265b2fc499SJeff Garzik 		*regd = le16_to_cpu(regdi);
2275b2fc499SJeff Garzik 		return ret;
2282bd64701SPetko Manolov 	}
2292bd64701SPetko Manolov 	return 0;
2305b2fc499SJeff Garzik fail:
2312bd64701SPetko Manolov 	netif_dbg(p, drv, p->net, "%s failed\n", __func__);
2325b2fc499SJeff Garzik 	return ret;
2335b2fc499SJeff Garzik }
2345b2fc499SJeff Garzik 
2352bd64701SPetko Manolov /* Returns non-negative int on success, error on failure */
2362bd64701SPetko Manolov static int read_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
2372bd64701SPetko Manolov {
2382bd64701SPetko Manolov 	return __mii_op(pegasus, phy, indx, regd, PHY_READ);
2392bd64701SPetko Manolov }
2402bd64701SPetko Manolov 
2412bd64701SPetko Manolov /* Returns zero on success, error on failure */
2422bd64701SPetko Manolov static int write_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
2432bd64701SPetko Manolov {
2442bd64701SPetko Manolov 	return __mii_op(pegasus, phy, indx, regd, PHY_WRITE);
2452bd64701SPetko Manolov }
2462bd64701SPetko Manolov 
2475b2fc499SJeff Garzik static int mdio_read(struct net_device *dev, int phy_id, int loc)
2485b2fc499SJeff Garzik {
2498739cfefSJoe Perches 	pegasus_t *pegasus = netdev_priv(dev);
2505b2fc499SJeff Garzik 	u16 res;
2515b2fc499SJeff Garzik 
2525b2fc499SJeff Garzik 	read_mii_word(pegasus, phy_id, loc, &res);
2535b2fc499SJeff Garzik 	return (int)res;
2545b2fc499SJeff Garzik }
2555b2fc499SJeff Garzik 
2565b2fc499SJeff Garzik static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
2575b2fc499SJeff Garzik {
2588739cfefSJoe Perches 	pegasus_t *pegasus = netdev_priv(dev);
2593d64fc70SDan Carpenter 	u16 data = val;
2605b2fc499SJeff Garzik 
2613d64fc70SDan Carpenter 	write_mii_word(pegasus, phy_id, loc, &data);
2625b2fc499SJeff Garzik }
2635b2fc499SJeff Garzik 
2645b2fc499SJeff Garzik static int read_eprom_word(pegasus_t *pegasus, __u8 index, __u16 *retdata)
2655b2fc499SJeff Garzik {
2665b2fc499SJeff Garzik 	int i;
2675b2fc499SJeff Garzik 	__u8 tmp;
2685b2fc499SJeff Garzik 	__le16 retdatai;
2695b2fc499SJeff Garzik 	int ret;
2705b2fc499SJeff Garzik 
2715b2fc499SJeff Garzik 	set_register(pegasus, EpromCtrl, 0);
2725b2fc499SJeff Garzik 	set_register(pegasus, EpromOffset, index);
2735b2fc499SJeff Garzik 	set_register(pegasus, EpromCtrl, EPROM_READ);
2745b2fc499SJeff Garzik 
2755b2fc499SJeff Garzik 	for (i = 0; i < REG_TIMEOUT; i++) {
2765b2fc499SJeff Garzik 		ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
2775b2fc499SJeff Garzik 		if (tmp & EPROM_DONE)
2785b2fc499SJeff Garzik 			break;
2795b2fc499SJeff Garzik 		if (ret == -ESHUTDOWN)
2805b2fc499SJeff Garzik 			goto fail;
2815b2fc499SJeff Garzik 	}
282a475f603SJoe Perches 	if (i >= REG_TIMEOUT)
283a475f603SJoe Perches 		goto fail;
284a475f603SJoe Perches 
2855b2fc499SJeff Garzik 	ret = get_registers(pegasus, EpromData, 2, &retdatai);
2865b2fc499SJeff Garzik 	*retdata = le16_to_cpu(retdatai);
2875b2fc499SJeff Garzik 	return ret;
2885b2fc499SJeff Garzik 
2895b2fc499SJeff Garzik fail:
290a475f603SJoe Perches 	netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
2915b2fc499SJeff Garzik 	return -ETIMEDOUT;
2925b2fc499SJeff Garzik }
2935b2fc499SJeff Garzik 
2945b2fc499SJeff Garzik #ifdef	PEGASUS_WRITE_EEPROM
2955b2fc499SJeff Garzik static inline void enable_eprom_write(pegasus_t *pegasus)
2965b2fc499SJeff Garzik {
2975b2fc499SJeff Garzik 	__u8 tmp;
2985b2fc499SJeff Garzik 
2995b2fc499SJeff Garzik 	get_registers(pegasus, EthCtrl2, 1, &tmp);
3005b2fc499SJeff Garzik 	set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
3015b2fc499SJeff Garzik }
3025b2fc499SJeff Garzik 
3035b2fc499SJeff Garzik static inline void disable_eprom_write(pegasus_t *pegasus)
3045b2fc499SJeff Garzik {
3055b2fc499SJeff Garzik 	__u8 tmp;
3065b2fc499SJeff Garzik 
3075b2fc499SJeff Garzik 	get_registers(pegasus, EthCtrl2, 1, &tmp);
3085b2fc499SJeff Garzik 	set_register(pegasus, EpromCtrl, 0);
3095b2fc499SJeff Garzik 	set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
3105b2fc499SJeff Garzik }
3115b2fc499SJeff Garzik 
3125b2fc499SJeff Garzik static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data)
3135b2fc499SJeff Garzik {
3145b2fc499SJeff Garzik 	int i;
3155b2fc499SJeff Garzik 	__u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
3165b2fc499SJeff Garzik 	int ret;
317e3453f63SMichael Buesch 	__le16 le_data = cpu_to_le16(data);
3185b2fc499SJeff Garzik 
3195b2fc499SJeff Garzik 	set_registers(pegasus, EpromOffset, 4, d);
3205b2fc499SJeff Garzik 	enable_eprom_write(pegasus);
3215b2fc499SJeff Garzik 	set_register(pegasus, EpromOffset, index);
322e3453f63SMichael Buesch 	set_registers(pegasus, EpromData, 2, &le_data);
3235b2fc499SJeff Garzik 	set_register(pegasus, EpromCtrl, EPROM_WRITE);
3245b2fc499SJeff Garzik 
3255b2fc499SJeff Garzik 	for (i = 0; i < REG_TIMEOUT; i++) {
3265b2fc499SJeff Garzik 		ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
3275b2fc499SJeff Garzik 		if (ret == -ESHUTDOWN)
3285b2fc499SJeff Garzik 			goto fail;
3295b2fc499SJeff Garzik 		if (tmp & EPROM_DONE)
3305b2fc499SJeff Garzik 			break;
3315b2fc499SJeff Garzik 	}
3325b2fc499SJeff Garzik 	disable_eprom_write(pegasus);
333a475f603SJoe Perches 	if (i >= REG_TIMEOUT)
334a475f603SJoe Perches 		goto fail;
335a475f603SJoe Perches 
3365b2fc499SJeff Garzik 	return ret;
337a475f603SJoe Perches 
3385b2fc499SJeff Garzik fail:
339a475f603SJoe Perches 	netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
3405b2fc499SJeff Garzik 	return -ETIMEDOUT;
3415b2fc499SJeff Garzik }
3425b2fc499SJeff Garzik #endif				/* PEGASUS_WRITE_EEPROM */
3435b2fc499SJeff Garzik 
3445b2fc499SJeff Garzik static inline void get_node_id(pegasus_t *pegasus, __u8 *id)
3455b2fc499SJeff Garzik {
3465b2fc499SJeff Garzik 	int i;
3475b2fc499SJeff Garzik 	__u16 w16;
3485b2fc499SJeff Garzik 
3495b2fc499SJeff Garzik 	for (i = 0; i < 3; i++) {
3505b2fc499SJeff Garzik 		read_eprom_word(pegasus, i, &w16);
351da2bbdccSHarvey Harrison 		((__le16 *) id)[i] = cpu_to_le16(w16);
3525b2fc499SJeff Garzik 	}
3535b2fc499SJeff Garzik }
3545b2fc499SJeff Garzik 
3555b2fc499SJeff Garzik static void set_ethernet_addr(pegasus_t *pegasus)
3565b2fc499SJeff Garzik {
3575b2fc499SJeff Garzik 	__u8 node_id[6];
3585b2fc499SJeff Garzik 
3595b2fc499SJeff Garzik 	if (pegasus->features & PEGASUS_II) {
3605b2fc499SJeff Garzik 		get_registers(pegasus, 0x10, sizeof(node_id), node_id);
3615b2fc499SJeff Garzik 	} else {
3625b2fc499SJeff Garzik 		get_node_id(pegasus, node_id);
3635b2fc499SJeff Garzik 		set_registers(pegasus, EthID, sizeof(node_id), node_id);
3645b2fc499SJeff Garzik 	}
3655b2fc499SJeff Garzik 	memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
3665b2fc499SJeff Garzik }
3675b2fc499SJeff Garzik 
3685b2fc499SJeff Garzik static inline int reset_mac(pegasus_t *pegasus)
3695b2fc499SJeff Garzik {
3705b2fc499SJeff Garzik 	__u8 data = 0x8;
3715b2fc499SJeff Garzik 	int i;
3725b2fc499SJeff Garzik 
3735b2fc499SJeff Garzik 	set_register(pegasus, EthCtrl1, data);
3745b2fc499SJeff Garzik 	for (i = 0; i < REG_TIMEOUT; i++) {
3755b2fc499SJeff Garzik 		get_registers(pegasus, EthCtrl1, 1, &data);
3765b2fc499SJeff Garzik 		if (~data & 0x08) {
377681f1623SDan Carpenter 			if (loopback)
3785b2fc499SJeff Garzik 				break;
3795b2fc499SJeff Garzik 			if (mii_mode && (pegasus->features & HAS_HOME_PNA))
3805b2fc499SJeff Garzik 				set_register(pegasus, Gpio1, 0x34);
3815b2fc499SJeff Garzik 			else
3825b2fc499SJeff Garzik 				set_register(pegasus, Gpio1, 0x26);
3835b2fc499SJeff Garzik 			set_register(pegasus, Gpio0, pegasus->features);
3845b2fc499SJeff Garzik 			set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
3855b2fc499SJeff Garzik 			break;
3865b2fc499SJeff Garzik 		}
3875b2fc499SJeff Garzik 	}
3885b2fc499SJeff Garzik 	if (i == REG_TIMEOUT)
3895b2fc499SJeff Garzik 		return -ETIMEDOUT;
3905b2fc499SJeff Garzik 
3915b2fc499SJeff Garzik 	if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
3925b2fc499SJeff Garzik 	    usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
3935b2fc499SJeff Garzik 		set_register(pegasus, Gpio0, 0x24);
3945b2fc499SJeff Garzik 		set_register(pegasus, Gpio0, 0x26);
3955b2fc499SJeff Garzik 	}
3965b2fc499SJeff Garzik 	if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
3975b2fc499SJeff Garzik 		__u16 auxmode;
3985b2fc499SJeff Garzik 		read_mii_word(pegasus, 3, 0x1b, &auxmode);
3992bd64701SPetko Manolov 		auxmode |= 4;
4002bd64701SPetko Manolov 		write_mii_word(pegasus, 3, 0x1b, &auxmode);
4015b2fc499SJeff Garzik 	}
4025b2fc499SJeff Garzik 
4035b2fc499SJeff Garzik 	return 0;
4045b2fc499SJeff Garzik }
4055b2fc499SJeff Garzik 
4065b2fc499SJeff Garzik static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
4075b2fc499SJeff Garzik {
4085b2fc499SJeff Garzik 	__u16 linkpart;
4095b2fc499SJeff Garzik 	__u8 data[4];
4105b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
4115b2fc499SJeff Garzik 	int ret;
4125b2fc499SJeff Garzik 
4135b2fc499SJeff Garzik 	read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
4141a8deec0SPetko Manolov 	data[0] = 0xc8; /* TX & RX enable, append status, no CRC */
4155b2fc499SJeff Garzik 	data[1] = 0;
4165b2fc499SJeff Garzik 	if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
4175b2fc499SJeff Garzik 		data[1] |= 0x20;	/* set full duplex */
4185b2fc499SJeff Garzik 	if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
4195b2fc499SJeff Garzik 		data[1] |= 0x10;	/* set 100 Mbps */
4205b2fc499SJeff Garzik 	if (mii_mode)
4215b2fc499SJeff Garzik 		data[1] = 0;
422681f1623SDan Carpenter 	data[2] = loopback ? 0x09 : 0x01;
4235b2fc499SJeff Garzik 
4245b2fc499SJeff Garzik 	memcpy(pegasus->eth_regs, data, sizeof(data));
4255b2fc499SJeff Garzik 	ret = set_registers(pegasus, EthCtrl0, 3, data);
4265b2fc499SJeff Garzik 
4275b2fc499SJeff Garzik 	if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
4285b2fc499SJeff Garzik 	    usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
4295b2fc499SJeff Garzik 	    usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
4305b2fc499SJeff Garzik 		u16 auxmode;
4315b2fc499SJeff Garzik 		read_mii_word(pegasus, 0, 0x1b, &auxmode);
4322bd64701SPetko Manolov 		auxmode |= 4;
4332bd64701SPetko Manolov 		write_mii_word(pegasus, 0, 0x1b, &auxmode);
4345b2fc499SJeff Garzik 	}
4355b2fc499SJeff Garzik 
4365b2fc499SJeff Garzik 	return ret;
4375b2fc499SJeff Garzik }
4385b2fc499SJeff Garzik 
4395b2fc499SJeff Garzik static void read_bulk_callback(struct urb *urb)
4405b2fc499SJeff Garzik {
4415b2fc499SJeff Garzik 	pegasus_t *pegasus = urb->context;
4425b2fc499SJeff Garzik 	struct net_device *net;
4435b2fc499SJeff Garzik 	int rx_status, count = urb->actual_length;
444c94cb314SOliver Neukum 	int status = urb->status;
4455b2fc499SJeff Garzik 	u8 *buf = urb->transfer_buffer;
4465b2fc499SJeff Garzik 	__u16 pkt_len;
4475b2fc499SJeff Garzik 
4485b2fc499SJeff Garzik 	if (!pegasus)
4495b2fc499SJeff Garzik 		return;
4505b2fc499SJeff Garzik 
4515b2fc499SJeff Garzik 	net = pegasus->net;
4525b2fc499SJeff Garzik 	if (!netif_device_present(net) || !netif_running(net))
4535b2fc499SJeff Garzik 		return;
4545b2fc499SJeff Garzik 
455c94cb314SOliver Neukum 	switch (status) {
4565b2fc499SJeff Garzik 	case 0:
4575b2fc499SJeff Garzik 		break;
4585b2fc499SJeff Garzik 	case -ETIME:
459a475f603SJoe Perches 		netif_dbg(pegasus, rx_err, net, "reset MAC\n");
4605b2fc499SJeff Garzik 		pegasus->flags &= ~PEGASUS_RX_BUSY;
4615b2fc499SJeff Garzik 		break;
4625b2fc499SJeff Garzik 	case -EPIPE:		/* stall, or disconnect from TT */
4635b2fc499SJeff Garzik 		/* FIXME schedule work to clear the halt */
464a475f603SJoe Perches 		netif_warn(pegasus, rx_err, net, "no rx stall recovery\n");
4655b2fc499SJeff Garzik 		return;
4665b2fc499SJeff Garzik 	case -ENOENT:
4675b2fc499SJeff Garzik 	case -ECONNRESET:
4685b2fc499SJeff Garzik 	case -ESHUTDOWN:
469a475f603SJoe Perches 		netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status);
4705b2fc499SJeff Garzik 		return;
4715b2fc499SJeff Garzik 	default:
472a475f603SJoe Perches 		netif_dbg(pegasus, rx_err, net, "RX status %d\n", status);
4735b2fc499SJeff Garzik 		goto goon;
4745b2fc499SJeff Garzik 	}
4755b2fc499SJeff Garzik 
4765b2fc499SJeff Garzik 	if (!count || count < 4)
4775b2fc499SJeff Garzik 		goto goon;
4785b2fc499SJeff Garzik 
4795b2fc499SJeff Garzik 	rx_status = buf[count - 2];
4805b2fc499SJeff Garzik 	if (rx_status & 0x1e) {
481a475f603SJoe Perches 		netif_dbg(pegasus, rx_err, net,
482a475f603SJoe Perches 			  "RX packet error %x\n", rx_status);
4835b2fc499SJeff Garzik 		pegasus->stats.rx_errors++;
4845a9dbfe0SNicolas Kaiser 		if (rx_status & 0x06)	/* long or runt	*/
4855b2fc499SJeff Garzik 			pegasus->stats.rx_length_errors++;
4865b2fc499SJeff Garzik 		if (rx_status & 0x08)
4875b2fc499SJeff Garzik 			pegasus->stats.rx_crc_errors++;
4885a9dbfe0SNicolas Kaiser 		if (rx_status & 0x10)	/* extra bits	*/
4895b2fc499SJeff Garzik 			pegasus->stats.rx_frame_errors++;
4905b2fc499SJeff Garzik 		goto goon;
4915b2fc499SJeff Garzik 	}
4925b2fc499SJeff Garzik 	if (pegasus->chip == 0x8513) {
4935b2fc499SJeff Garzik 		pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
4945b2fc499SJeff Garzik 		pkt_len &= 0x0fff;
4955b2fc499SJeff Garzik 		pegasus->rx_skb->data += 2;
4965b2fc499SJeff Garzik 	} else {
4975b2fc499SJeff Garzik 		pkt_len = buf[count - 3] << 8;
4985b2fc499SJeff Garzik 		pkt_len += buf[count - 4];
4995b2fc499SJeff Garzik 		pkt_len &= 0xfff;
5001a8deec0SPetko Manolov 		pkt_len -= 4;
5015b2fc499SJeff Garzik 	}
5025b2fc499SJeff Garzik 
5035b2fc499SJeff Garzik 	/*
5045b2fc499SJeff Garzik 	 * If the packet is unreasonably long, quietly drop it rather than
5055b2fc499SJeff Garzik 	 * kernel panicing by calling skb_put.
5065b2fc499SJeff Garzik 	 */
5075b2fc499SJeff Garzik 	if (pkt_len > PEGASUS_MTU)
5085b2fc499SJeff Garzik 		goto goon;
5095b2fc499SJeff Garzik 
5105b2fc499SJeff Garzik 	/*
5115b2fc499SJeff Garzik 	 * at this point we are sure pegasus->rx_skb != NULL
5125b2fc499SJeff Garzik 	 * so we go ahead and pass up the packet.
5135b2fc499SJeff Garzik 	 */
5145b2fc499SJeff Garzik 	skb_put(pegasus->rx_skb, pkt_len);
5155b2fc499SJeff Garzik 	pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
5165b2fc499SJeff Garzik 	netif_rx(pegasus->rx_skb);
5175b2fc499SJeff Garzik 	pegasus->stats.rx_packets++;
5185b2fc499SJeff Garzik 	pegasus->stats.rx_bytes += pkt_len;
5195b2fc499SJeff Garzik 
5205b2fc499SJeff Garzik 	if (pegasus->flags & PEGASUS_UNPLUG)
5215b2fc499SJeff Garzik 		return;
5225b2fc499SJeff Garzik 
523313a58e4SPetko Manolov 	pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, PEGASUS_MTU,
524313a58e4SPetko Manolov 						      GFP_ATOMIC);
5255b2fc499SJeff Garzik 
5265b2fc499SJeff Garzik 	if (pegasus->rx_skb == NULL)
5275b2fc499SJeff Garzik 		goto tl_sched;
5285b2fc499SJeff Garzik goon:
5295b2fc499SJeff Garzik 	usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
5305b2fc499SJeff Garzik 			  usb_rcvbulkpipe(pegasus->usb, 1),
531b7302ca0SPetko Manolov 			  pegasus->rx_skb->data, PEGASUS_MTU,
5325b2fc499SJeff Garzik 			  read_bulk_callback, pegasus);
5335b2fc499SJeff Garzik 	rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
5345b2fc499SJeff Garzik 	if (rx_status == -ENODEV)
5355b2fc499SJeff Garzik 		netif_device_detach(pegasus->net);
5365b2fc499SJeff Garzik 	else if (rx_status) {
5375b2fc499SJeff Garzik 		pegasus->flags |= PEGASUS_RX_URB_FAIL;
5385b2fc499SJeff Garzik 		goto tl_sched;
5395b2fc499SJeff Garzik 	} else {
5405b2fc499SJeff Garzik 		pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
5415b2fc499SJeff Garzik 	}
5425b2fc499SJeff Garzik 
5435b2fc499SJeff Garzik 	return;
5445b2fc499SJeff Garzik 
5455b2fc499SJeff Garzik tl_sched:
5465b2fc499SJeff Garzik 	tasklet_schedule(&pegasus->rx_tl);
5475b2fc499SJeff Garzik }
5485b2fc499SJeff Garzik 
5495b2fc499SJeff Garzik static void rx_fixup(unsigned long data)
5505b2fc499SJeff Garzik {
5515b2fc499SJeff Garzik 	pegasus_t *pegasus;
5525b2fc499SJeff Garzik 	int status;
5535b2fc499SJeff Garzik 
5545b2fc499SJeff Garzik 	pegasus = (pegasus_t *) data;
5555b2fc499SJeff Garzik 	if (pegasus->flags & PEGASUS_UNPLUG)
5565b2fc499SJeff Garzik 		return;
5575b2fc499SJeff Garzik 
5585b2fc499SJeff Garzik 	if (pegasus->flags & PEGASUS_RX_URB_FAIL)
5595b2fc499SJeff Garzik 		if (pegasus->rx_skb)
5605b2fc499SJeff Garzik 			goto try_again;
5615a9dbfe0SNicolas Kaiser 	if (pegasus->rx_skb == NULL)
562313a58e4SPetko Manolov 		pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
563313a58e4SPetko Manolov 							      PEGASUS_MTU,
564313a58e4SPetko Manolov 							      GFP_ATOMIC);
5655b2fc499SJeff Garzik 	if (pegasus->rx_skb == NULL) {
566a475f603SJoe Perches 		netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
5675b2fc499SJeff Garzik 		tasklet_schedule(&pegasus->rx_tl);
568313a58e4SPetko Manolov 		return;
5695b2fc499SJeff Garzik 	}
5705b2fc499SJeff Garzik 	usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
5715b2fc499SJeff Garzik 			  usb_rcvbulkpipe(pegasus->usb, 1),
572b7302ca0SPetko Manolov 			  pegasus->rx_skb->data, PEGASUS_MTU,
5735b2fc499SJeff Garzik 			  read_bulk_callback, pegasus);
5745b2fc499SJeff Garzik try_again:
5755b2fc499SJeff Garzik 	status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
5765b2fc499SJeff Garzik 	if (status == -ENODEV)
5775b2fc499SJeff Garzik 		netif_device_detach(pegasus->net);
5785b2fc499SJeff Garzik 	else if (status) {
5795b2fc499SJeff Garzik 		pegasus->flags |= PEGASUS_RX_URB_FAIL;
5805b2fc499SJeff Garzik 		tasklet_schedule(&pegasus->rx_tl);
5815b2fc499SJeff Garzik 	} else {
5825b2fc499SJeff Garzik 		pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
5835b2fc499SJeff Garzik 	}
5845b2fc499SJeff Garzik }
5855b2fc499SJeff Garzik 
5865b2fc499SJeff Garzik static void write_bulk_callback(struct urb *urb)
5875b2fc499SJeff Garzik {
5885b2fc499SJeff Garzik 	pegasus_t *pegasus = urb->context;
5899351982bSMicah Gruber 	struct net_device *net;
590c94cb314SOliver Neukum 	int status = urb->status;
5915b2fc499SJeff Garzik 
5925b2fc499SJeff Garzik 	if (!pegasus)
5935b2fc499SJeff Garzik 		return;
5945b2fc499SJeff Garzik 
5959351982bSMicah Gruber 	net = pegasus->net;
5969351982bSMicah Gruber 
5975b2fc499SJeff Garzik 	if (!netif_device_present(net) || !netif_running(net))
5985b2fc499SJeff Garzik 		return;
5995b2fc499SJeff Garzik 
600c94cb314SOliver Neukum 	switch (status) {
6015b2fc499SJeff Garzik 	case -EPIPE:
6025b2fc499SJeff Garzik 		/* FIXME schedule_work() to clear the tx halt */
6035b2fc499SJeff Garzik 		netif_stop_queue(net);
604a475f603SJoe Perches 		netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
6055b2fc499SJeff Garzik 		return;
6065b2fc499SJeff Garzik 	case -ENOENT:
6075b2fc499SJeff Garzik 	case -ECONNRESET:
6085b2fc499SJeff Garzik 	case -ESHUTDOWN:
609a475f603SJoe Perches 		netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
6105b2fc499SJeff Garzik 		return;
6115b2fc499SJeff Garzik 	default:
612a475f603SJoe Perches 		netif_info(pegasus, tx_err, net, "TX status %d\n", status);
6135b2fc499SJeff Garzik 		/* FALL THROUGH */
6145b2fc499SJeff Garzik 	case 0:
6155b2fc499SJeff Garzik 		break;
6165b2fc499SJeff Garzik 	}
6175b2fc499SJeff Garzik 
618*860e9538SFlorian Westphal 	netif_trans_update(net); /* prevent tx timeout */
6195b2fc499SJeff Garzik 	netif_wake_queue(net);
6205b2fc499SJeff Garzik }
6215b2fc499SJeff Garzik 
6225b2fc499SJeff Garzik static void intr_callback(struct urb *urb)
6235b2fc499SJeff Garzik {
6245b2fc499SJeff Garzik 	pegasus_t *pegasus = urb->context;
6255b2fc499SJeff Garzik 	struct net_device *net;
626c94cb314SOliver Neukum 	int res, status = urb->status;
6275b2fc499SJeff Garzik 
6285b2fc499SJeff Garzik 	if (!pegasus)
6295b2fc499SJeff Garzik 		return;
6305b2fc499SJeff Garzik 	net = pegasus->net;
6315b2fc499SJeff Garzik 
632c94cb314SOliver Neukum 	switch (status) {
6335b2fc499SJeff Garzik 	case 0:
6345b2fc499SJeff Garzik 		break;
6355b2fc499SJeff Garzik 	case -ECONNRESET:	/* unlink */
6365b2fc499SJeff Garzik 	case -ENOENT:
6375b2fc499SJeff Garzik 	case -ESHUTDOWN:
6385b2fc499SJeff Garzik 		return;
6395b2fc499SJeff Garzik 	default:
6405b2fc499SJeff Garzik 		/* some Pegasus-I products report LOTS of data
6415b2fc499SJeff Garzik 		 * toggle errors... avoid log spamming
6425b2fc499SJeff Garzik 		 */
643a475f603SJoe Perches 		netif_dbg(pegasus, timer, net, "intr status %d\n", status);
6445b2fc499SJeff Garzik 	}
6455b2fc499SJeff Garzik 
6465b2fc499SJeff Garzik 	if (urb->actual_length >= 6) {
6475b2fc499SJeff Garzik 		u8 *d = urb->transfer_buffer;
6485b2fc499SJeff Garzik 
6495b2fc499SJeff Garzik 		/* byte 0 == tx_status1, reg 2B */
6505b2fc499SJeff Garzik 		if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
6515b2fc499SJeff Garzik 					|LATE_COL|JABBER_TIMEOUT)) {
6525b2fc499SJeff Garzik 			pegasus->stats.tx_errors++;
6535b2fc499SJeff Garzik 			if (d[0] & TX_UNDERRUN)
6545b2fc499SJeff Garzik 				pegasus->stats.tx_fifo_errors++;
6555b2fc499SJeff Garzik 			if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
6565b2fc499SJeff Garzik 				pegasus->stats.tx_aborted_errors++;
6575b2fc499SJeff Garzik 			if (d[0] & LATE_COL)
6585b2fc499SJeff Garzik 				pegasus->stats.tx_window_errors++;
6595b2fc499SJeff Garzik 		}
6605b2fc499SJeff Garzik 
6615b2fc499SJeff Garzik 		/* d[5].LINK_STATUS lies on some adapters.
6625b2fc499SJeff Garzik 		 * d[0].NO_CARRIER kicks in only with failed TX.
6635b2fc499SJeff Garzik 		 * ... so monitoring with MII may be safest.
6645b2fc499SJeff Garzik 		 */
6655b2fc499SJeff Garzik 
6665b2fc499SJeff Garzik 		/* bytes 3-4 == rx_lostpkt, reg 2E/2F */
6675b2fc499SJeff Garzik 		pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
6685b2fc499SJeff Garzik 	}
6695b2fc499SJeff Garzik 
670c94cb314SOliver Neukum 	res = usb_submit_urb(urb, GFP_ATOMIC);
671c94cb314SOliver Neukum 	if (res == -ENODEV)
6725b2fc499SJeff Garzik 		netif_device_detach(pegasus->net);
673a475f603SJoe Perches 	if (res)
674a475f603SJoe Perches 		netif_err(pegasus, timer, net,
675a475f603SJoe Perches 			  "can't resubmit interrupt urb, %d\n", res);
6765b2fc499SJeff Garzik }
6775b2fc499SJeff Garzik 
6785b2fc499SJeff Garzik static void pegasus_tx_timeout(struct net_device *net)
6795b2fc499SJeff Garzik {
6805b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
681a475f603SJoe Perches 	netif_warn(pegasus, timer, net, "tx timeout\n");
6825b2fc499SJeff Garzik 	usb_unlink_urb(pegasus->tx_urb);
6835b2fc499SJeff Garzik 	pegasus->stats.tx_errors++;
6845b2fc499SJeff Garzik }
6855b2fc499SJeff Garzik 
68625a79c41SStephen Hemminger static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
68725a79c41SStephen Hemminger 					    struct net_device *net)
6885b2fc499SJeff Garzik {
6895b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
6905b2fc499SJeff Garzik 	int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
6915b2fc499SJeff Garzik 	int res;
6925b2fc499SJeff Garzik 	__u16 l16 = skb->len;
6935b2fc499SJeff Garzik 
6945b2fc499SJeff Garzik 	netif_stop_queue(net);
6955b2fc499SJeff Garzik 
6965b2fc499SJeff Garzik 	((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
6975b2fc499SJeff Garzik 	skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
6985b2fc499SJeff Garzik 	usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
6995b2fc499SJeff Garzik 			  usb_sndbulkpipe(pegasus->usb, 2),
7005b2fc499SJeff Garzik 			  pegasus->tx_buff, count,
7015b2fc499SJeff Garzik 			  write_bulk_callback, pegasus);
7025b2fc499SJeff Garzik 	if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
703a475f603SJoe Perches 		netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
7045b2fc499SJeff Garzik 		switch (res) {
7055b2fc499SJeff Garzik 		case -EPIPE:		/* stall, or disconnect from TT */
7065b2fc499SJeff Garzik 			/* cleanup should already have been scheduled */
7075b2fc499SJeff Garzik 			break;
7085b2fc499SJeff Garzik 		case -ENODEV:		/* disconnect() upcoming */
7099dd014ebSOliver Neukum 		case -EPERM:
7105b2fc499SJeff Garzik 			netif_device_detach(pegasus->net);
7115b2fc499SJeff Garzik 			break;
7125b2fc499SJeff Garzik 		default:
7135b2fc499SJeff Garzik 			pegasus->stats.tx_errors++;
7145b2fc499SJeff Garzik 			netif_start_queue(net);
7155b2fc499SJeff Garzik 		}
7165b2fc499SJeff Garzik 	} else {
7175b2fc499SJeff Garzik 		pegasus->stats.tx_packets++;
7185b2fc499SJeff Garzik 		pegasus->stats.tx_bytes += skb->len;
7195b2fc499SJeff Garzik 	}
7205b2fc499SJeff Garzik 	dev_kfree_skb(skb);
7215b2fc499SJeff Garzik 
7226ed10654SPatrick McHardy 	return NETDEV_TX_OK;
7235b2fc499SJeff Garzik }
7245b2fc499SJeff Garzik 
7255b2fc499SJeff Garzik static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
7265b2fc499SJeff Garzik {
7275b2fc499SJeff Garzik 	return &((pegasus_t *) netdev_priv(dev))->stats;
7285b2fc499SJeff Garzik }
7295b2fc499SJeff Garzik 
7305b2fc499SJeff Garzik static inline void disable_net_traffic(pegasus_t *pegasus)
7315b2fc499SJeff Garzik {
732e3453f63SMichael Buesch 	__le16 tmp = cpu_to_le16(0);
7335b2fc499SJeff Garzik 
734e3453f63SMichael Buesch 	set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
7355b2fc499SJeff Garzik }
7365b2fc499SJeff Garzik 
7375b2fc499SJeff Garzik static inline void get_interrupt_interval(pegasus_t *pegasus)
7385b2fc499SJeff Garzik {
739e3453f63SMichael Buesch 	u16 data;
740e3453f63SMichael Buesch 	u8 interval;
7415b2fc499SJeff Garzik 
742e3453f63SMichael Buesch 	read_eprom_word(pegasus, 4, &data);
743e3453f63SMichael Buesch 	interval = data >> 8;
7445b2fc499SJeff Garzik 	if (pegasus->usb->speed != USB_SPEED_HIGH) {
745e3453f63SMichael Buesch 		if (interval < 0x80) {
746a475f603SJoe Perches 			netif_info(pegasus, timer, pegasus->net,
747a475f603SJoe Perches 				   "intr interval changed from %ums to %ums\n",
748e3453f63SMichael Buesch 				   interval, 0x80);
749e3453f63SMichael Buesch 			interval = 0x80;
750e3453f63SMichael Buesch 			data = (data & 0x00FF) | ((u16)interval << 8);
7515b2fc499SJeff Garzik #ifdef PEGASUS_WRITE_EEPROM
752e3453f63SMichael Buesch 			write_eprom_word(pegasus, 4, data);
7535b2fc499SJeff Garzik #endif
7545b2fc499SJeff Garzik 		}
7555b2fc499SJeff Garzik 	}
756e3453f63SMichael Buesch 	pegasus->intr_interval = interval;
7575b2fc499SJeff Garzik }
7585b2fc499SJeff Garzik 
7595b2fc499SJeff Garzik static void set_carrier(struct net_device *net)
7605b2fc499SJeff Garzik {
7615b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
7625b2fc499SJeff Garzik 	u16 tmp;
7635b2fc499SJeff Garzik 
7645b2fc499SJeff Garzik 	if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
7655b2fc499SJeff Garzik 		return;
7665b2fc499SJeff Garzik 
7675b2fc499SJeff Garzik 	if (tmp & BMSR_LSTATUS)
7685b2fc499SJeff Garzik 		netif_carrier_on(net);
7695b2fc499SJeff Garzik 	else
7705b2fc499SJeff Garzik 		netif_carrier_off(net);
7715b2fc499SJeff Garzik }
7725b2fc499SJeff Garzik 
7735b2fc499SJeff Garzik static void free_all_urbs(pegasus_t *pegasus)
7745b2fc499SJeff Garzik {
7755b2fc499SJeff Garzik 	usb_free_urb(pegasus->intr_urb);
7765b2fc499SJeff Garzik 	usb_free_urb(pegasus->tx_urb);
7775b2fc499SJeff Garzik 	usb_free_urb(pegasus->rx_urb);
7785b2fc499SJeff Garzik }
7795b2fc499SJeff Garzik 
7805b2fc499SJeff Garzik static void unlink_all_urbs(pegasus_t *pegasus)
7815b2fc499SJeff Garzik {
7825b2fc499SJeff Garzik 	usb_kill_urb(pegasus->intr_urb);
7835b2fc499SJeff Garzik 	usb_kill_urb(pegasus->tx_urb);
7845b2fc499SJeff Garzik 	usb_kill_urb(pegasus->rx_urb);
7855b2fc499SJeff Garzik }
7865b2fc499SJeff Garzik 
7875b2fc499SJeff Garzik static int alloc_urbs(pegasus_t *pegasus)
7885b2fc499SJeff Garzik {
789323b3496SPetko Manolov 	int res = -ENOMEM;
790323b3496SPetko Manolov 
7915b2fc499SJeff Garzik 	pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
7925b2fc499SJeff Garzik 	if (!pegasus->rx_urb) {
793323b3496SPetko Manolov 		return res;
7945b2fc499SJeff Garzik 	}
7955b2fc499SJeff Garzik 	pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
7965b2fc499SJeff Garzik 	if (!pegasus->tx_urb) {
7975b2fc499SJeff Garzik 		usb_free_urb(pegasus->rx_urb);
798323b3496SPetko Manolov 		return res;
7995b2fc499SJeff Garzik 	}
8005b2fc499SJeff Garzik 	pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
8015b2fc499SJeff Garzik 	if (!pegasus->intr_urb) {
8025b2fc499SJeff Garzik 		usb_free_urb(pegasus->tx_urb);
8035b2fc499SJeff Garzik 		usb_free_urb(pegasus->rx_urb);
804323b3496SPetko Manolov 		return res;
8055b2fc499SJeff Garzik 	}
8065b2fc499SJeff Garzik 
807323b3496SPetko Manolov 	return 0;
8085b2fc499SJeff Garzik }
8095b2fc499SJeff Garzik 
8105b2fc499SJeff Garzik static int pegasus_open(struct net_device *net)
8115b2fc499SJeff Garzik {
8125b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
813323b3496SPetko Manolov 	int res=-ENOMEM;
8145b2fc499SJeff Garzik 
8155b2fc499SJeff Garzik 	if (pegasus->rx_skb == NULL)
816313a58e4SPetko Manolov 		pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
817313a58e4SPetko Manolov 							      PEGASUS_MTU,
818313a58e4SPetko Manolov 							      GFP_KERNEL);
8195b2fc499SJeff Garzik 	if (!pegasus->rx_skb)
820323b3496SPetko Manolov 		goto exit;
8215b2fc499SJeff Garzik 
8225b2fc499SJeff Garzik 	res = set_registers(pegasus, EthID, 6, net->dev_addr);
8235b2fc499SJeff Garzik 
8245b2fc499SJeff Garzik 	usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
8255b2fc499SJeff Garzik 			  usb_rcvbulkpipe(pegasus->usb, 1),
826b7302ca0SPetko Manolov 			  pegasus->rx_skb->data, PEGASUS_MTU,
8275b2fc499SJeff Garzik 			  read_bulk_callback, pegasus);
8285b2fc499SJeff Garzik 	if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
8295b2fc499SJeff Garzik 		if (res == -ENODEV)
8305b2fc499SJeff Garzik 			netif_device_detach(pegasus->net);
831a475f603SJoe Perches 		netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
8325b2fc499SJeff Garzik 		goto exit;
8335b2fc499SJeff Garzik 	}
8345b2fc499SJeff Garzik 
8355b2fc499SJeff Garzik 	usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
8365b2fc499SJeff Garzik 			 usb_rcvintpipe(pegasus->usb, 3),
8375b2fc499SJeff Garzik 			 pegasus->intr_buff, sizeof(pegasus->intr_buff),
8385b2fc499SJeff Garzik 			 intr_callback, pegasus, pegasus->intr_interval);
8395b2fc499SJeff Garzik 	if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
8405b2fc499SJeff Garzik 		if (res == -ENODEV)
8415b2fc499SJeff Garzik 			netif_device_detach(pegasus->net);
842a475f603SJoe Perches 		netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
8435b2fc499SJeff Garzik 		usb_kill_urb(pegasus->rx_urb);
8445b2fc499SJeff Garzik 		goto exit;
8455b2fc499SJeff Garzik 	}
846323b3496SPetko Manolov 	res = enable_net_traffic(net, pegasus->usb);
847323b3496SPetko Manolov 	if (res < 0) {
848a475f603SJoe Perches 		netif_dbg(pegasus, ifup, net,
849a475f603SJoe Perches 			  "can't enable_net_traffic() - %d\n", res);
8505b2fc499SJeff Garzik 		res = -EIO;
8515b2fc499SJeff Garzik 		usb_kill_urb(pegasus->rx_urb);
8525b2fc499SJeff Garzik 		usb_kill_urb(pegasus->intr_urb);
8535b2fc499SJeff Garzik 		goto exit;
8545b2fc499SJeff Garzik 	}
8555b2fc499SJeff Garzik 	set_carrier(net);
8565b2fc499SJeff Garzik 	netif_start_queue(net);
857a475f603SJoe Perches 	netif_dbg(pegasus, ifup, net, "open\n");
8585b2fc499SJeff Garzik 	res = 0;
8595b2fc499SJeff Garzik exit:
8605b2fc499SJeff Garzik 	return res;
8615b2fc499SJeff Garzik }
8625b2fc499SJeff Garzik 
8635b2fc499SJeff Garzik static int pegasus_close(struct net_device *net)
8645b2fc499SJeff Garzik {
8655b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
8665b2fc499SJeff Garzik 
8675b2fc499SJeff Garzik 	netif_stop_queue(net);
8685b2fc499SJeff Garzik 	if (!(pegasus->flags & PEGASUS_UNPLUG))
8695b2fc499SJeff Garzik 		disable_net_traffic(pegasus);
8705b2fc499SJeff Garzik 	tasklet_kill(&pegasus->rx_tl);
8715b2fc499SJeff Garzik 	unlink_all_urbs(pegasus);
8725b2fc499SJeff Garzik 
8735b2fc499SJeff Garzik 	return 0;
8745b2fc499SJeff Garzik }
8755b2fc499SJeff Garzik 
8765b2fc499SJeff Garzik static void pegasus_get_drvinfo(struct net_device *dev,
8775b2fc499SJeff Garzik 				struct ethtool_drvinfo *info)
8785b2fc499SJeff Garzik {
8795b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
8807826d43fSJiri Pirko 
8817826d43fSJiri Pirko 	strlcpy(info->driver, driver_name, sizeof(info->driver));
8827826d43fSJiri Pirko 	strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
8835b2fc499SJeff Garzik 	usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info));
8845b2fc499SJeff Garzik }
8855b2fc499SJeff Garzik 
8865b2fc499SJeff Garzik /* also handles three patterns of some kind in hardware */
8875b2fc499SJeff Garzik #define	WOL_SUPPORTED	(WAKE_MAGIC|WAKE_PHY)
8885b2fc499SJeff Garzik 
8895b2fc499SJeff Garzik static void
8905b2fc499SJeff Garzik pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8915b2fc499SJeff Garzik {
8925b2fc499SJeff Garzik 	pegasus_t	*pegasus = netdev_priv(dev);
8935b2fc499SJeff Garzik 
8945b2fc499SJeff Garzik 	wol->supported = WAKE_MAGIC | WAKE_PHY;
8955b2fc499SJeff Garzik 	wol->wolopts = pegasus->wolopts;
8965b2fc499SJeff Garzik }
8975b2fc499SJeff Garzik 
8985b2fc499SJeff Garzik static int
8995b2fc499SJeff Garzik pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9005b2fc499SJeff Garzik {
9015b2fc499SJeff Garzik 	pegasus_t	*pegasus = netdev_priv(dev);
9025b2fc499SJeff Garzik 	u8		reg78 = 0x04;
9034fbc5b20SMing Lei 	int		ret;
9045b2fc499SJeff Garzik 
9055b2fc499SJeff Garzik 	if (wol->wolopts & ~WOL_SUPPORTED)
9065b2fc499SJeff Garzik 		return -EINVAL;
9075b2fc499SJeff Garzik 
9085b2fc499SJeff Garzik 	if (wol->wolopts & WAKE_MAGIC)
9095b2fc499SJeff Garzik 		reg78 |= 0x80;
9105b2fc499SJeff Garzik 	if (wol->wolopts & WAKE_PHY)
9115b2fc499SJeff Garzik 		reg78 |= 0x40;
9125b2fc499SJeff Garzik 	/* FIXME this 0x10 bit still needs to get set in the chip... */
9135b2fc499SJeff Garzik 	if (wol->wolopts)
9145b2fc499SJeff Garzik 		pegasus->eth_regs[0] |= 0x10;
9155b2fc499SJeff Garzik 	else
9165b2fc499SJeff Garzik 		pegasus->eth_regs[0] &= ~0x10;
9175b2fc499SJeff Garzik 	pegasus->wolopts = wol->wolopts;
9184fbc5b20SMing Lei 
9194fbc5b20SMing Lei 	ret = set_register(pegasus, WakeupControl, reg78);
9204fbc5b20SMing Lei 	if (!ret)
9214fbc5b20SMing Lei 		ret = device_set_wakeup_enable(&pegasus->usb->dev,
9224fbc5b20SMing Lei 						wol->wolopts);
9234fbc5b20SMing Lei 	return ret;
9245b2fc499SJeff Garzik }
9255b2fc499SJeff Garzik 
9265b2fc499SJeff Garzik static inline void pegasus_reset_wol(struct net_device *dev)
9275b2fc499SJeff Garzik {
9285b2fc499SJeff Garzik 	struct ethtool_wolinfo wol;
9295b2fc499SJeff Garzik 
9305b2fc499SJeff Garzik 	memset(&wol, 0, sizeof wol);
9315b2fc499SJeff Garzik 	(void) pegasus_set_wol(dev, &wol);
9325b2fc499SJeff Garzik }
9335b2fc499SJeff Garzik 
9345b2fc499SJeff Garzik static int
9355b2fc499SJeff Garzik pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
9365b2fc499SJeff Garzik {
9375b2fc499SJeff Garzik 	pegasus_t *pegasus;
9385b2fc499SJeff Garzik 
9395b2fc499SJeff Garzik 	pegasus = netdev_priv(dev);
9405b2fc499SJeff Garzik 	mii_ethtool_gset(&pegasus->mii, ecmd);
9415b2fc499SJeff Garzik 	return 0;
9425b2fc499SJeff Garzik }
9435b2fc499SJeff Garzik 
9445b2fc499SJeff Garzik static int
9455b2fc499SJeff Garzik pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
9465b2fc499SJeff Garzik {
9475b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
9485b2fc499SJeff Garzik 	return mii_ethtool_sset(&pegasus->mii, ecmd);
9495b2fc499SJeff Garzik }
9505b2fc499SJeff Garzik 
9515b2fc499SJeff Garzik static int pegasus_nway_reset(struct net_device *dev)
9525b2fc499SJeff Garzik {
9535b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
9545b2fc499SJeff Garzik 	return mii_nway_restart(&pegasus->mii);
9555b2fc499SJeff Garzik }
9565b2fc499SJeff Garzik 
9575b2fc499SJeff Garzik static u32 pegasus_get_link(struct net_device *dev)
9585b2fc499SJeff Garzik {
9595b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
9605b2fc499SJeff Garzik 	return mii_link_ok(&pegasus->mii);
9615b2fc499SJeff Garzik }
9625b2fc499SJeff Garzik 
9635b2fc499SJeff Garzik static u32 pegasus_get_msglevel(struct net_device *dev)
9645b2fc499SJeff Garzik {
9655b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
9665b2fc499SJeff Garzik 	return pegasus->msg_enable;
9675b2fc499SJeff Garzik }
9685b2fc499SJeff Garzik 
9695b2fc499SJeff Garzik static void pegasus_set_msglevel(struct net_device *dev, u32 v)
9705b2fc499SJeff Garzik {
9715b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
9725b2fc499SJeff Garzik 	pegasus->msg_enable = v;
9735b2fc499SJeff Garzik }
9745b2fc499SJeff Garzik 
9750fc0b732SStephen Hemminger static const struct ethtool_ops ops = {
9765b2fc499SJeff Garzik 	.get_drvinfo = pegasus_get_drvinfo,
9775b2fc499SJeff Garzik 	.get_settings = pegasus_get_settings,
9785b2fc499SJeff Garzik 	.set_settings = pegasus_set_settings,
9795b2fc499SJeff Garzik 	.nway_reset = pegasus_nway_reset,
9805b2fc499SJeff Garzik 	.get_link = pegasus_get_link,
9815b2fc499SJeff Garzik 	.get_msglevel = pegasus_get_msglevel,
9825b2fc499SJeff Garzik 	.set_msglevel = pegasus_set_msglevel,
9835b2fc499SJeff Garzik 	.get_wol = pegasus_get_wol,
9845b2fc499SJeff Garzik 	.set_wol = pegasus_set_wol,
9855b2fc499SJeff Garzik };
9865b2fc499SJeff Garzik 
9875b2fc499SJeff Garzik static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
9885b2fc499SJeff Garzik {
9895b2fc499SJeff Garzik 	__u16 *data = (__u16 *) &rq->ifr_ifru;
9905b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
9915b2fc499SJeff Garzik 	int res;
9925b2fc499SJeff Garzik 
9935b2fc499SJeff Garzik 	switch (cmd) {
9945b2fc499SJeff Garzik 	case SIOCDEVPRIVATE:
9955b2fc499SJeff Garzik 		data[0] = pegasus->phy;
9965b2fc499SJeff Garzik 	case SIOCDEVPRIVATE + 1:
9975b2fc499SJeff Garzik 		read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
9985b2fc499SJeff Garzik 		res = 0;
9995b2fc499SJeff Garzik 		break;
10005b2fc499SJeff Garzik 	case SIOCDEVPRIVATE + 2:
10015b2fc499SJeff Garzik 		if (!capable(CAP_NET_ADMIN))
10025b2fc499SJeff Garzik 			return -EPERM;
10032bd64701SPetko Manolov 		write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, &data[2]);
10045b2fc499SJeff Garzik 		res = 0;
10055b2fc499SJeff Garzik 		break;
10065b2fc499SJeff Garzik 	default:
10075b2fc499SJeff Garzik 		res = -EOPNOTSUPP;
10085b2fc499SJeff Garzik 	}
10095b2fc499SJeff Garzik 	return res;
10105b2fc499SJeff Garzik }
10115b2fc499SJeff Garzik 
10125b2fc499SJeff Garzik static void pegasus_set_multicast(struct net_device *net)
10135b2fc499SJeff Garzik {
10145b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
10155b2fc499SJeff Garzik 
10165b2fc499SJeff Garzik 	if (net->flags & IFF_PROMISC) {
10175b2fc499SJeff Garzik 		pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1018a475f603SJoe Perches 		netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
10194cd24eafSJiri Pirko 	} else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
10205b2fc499SJeff Garzik 		pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
10215b2fc499SJeff Garzik 		pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1022a475f603SJoe Perches 		netif_dbg(pegasus, link, net, "set allmulti\n");
10235b2fc499SJeff Garzik 	} else {
10245b2fc499SJeff Garzik 		pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
10255b2fc499SJeff Garzik 		pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
10265b2fc499SJeff Garzik 	}
1027323b3496SPetko Manolov 	update_eth_regs_async(pegasus);
10285b2fc499SJeff Garzik }
10295b2fc499SJeff Garzik 
10305b2fc499SJeff Garzik static __u8 mii_phy_probe(pegasus_t *pegasus)
10315b2fc499SJeff Garzik {
10325b2fc499SJeff Garzik 	int i;
10335b2fc499SJeff Garzik 	__u16 tmp;
10345b2fc499SJeff Garzik 
10355b2fc499SJeff Garzik 	for (i = 0; i < 32; i++) {
10365b2fc499SJeff Garzik 		read_mii_word(pegasus, i, MII_BMSR, &tmp);
10375b2fc499SJeff Garzik 		if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
10385b2fc499SJeff Garzik 			continue;
10395b2fc499SJeff Garzik 		else
10405b2fc499SJeff Garzik 			return i;
10415b2fc499SJeff Garzik 	}
10425b2fc499SJeff Garzik 
10435b2fc499SJeff Garzik 	return 0xff;
10445b2fc499SJeff Garzik }
10455b2fc499SJeff Garzik 
10465b2fc499SJeff Garzik static inline void setup_pegasus_II(pegasus_t *pegasus)
10475b2fc499SJeff Garzik {
10485b2fc499SJeff Garzik 	__u8 data = 0xa5;
10495b2fc499SJeff Garzik 
10505b2fc499SJeff Garzik 	set_register(pegasus, Reg1d, 0);
10515b2fc499SJeff Garzik 	set_register(pegasus, Reg7b, 1);
10525b2fc499SJeff Garzik 	mdelay(100);
10535b2fc499SJeff Garzik 	if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
10545b2fc499SJeff Garzik 		set_register(pegasus, Reg7b, 0);
10555b2fc499SJeff Garzik 	else
10565b2fc499SJeff Garzik 		set_register(pegasus, Reg7b, 2);
10575b2fc499SJeff Garzik 
10585b2fc499SJeff Garzik 	set_register(pegasus, 0x83, data);
10595b2fc499SJeff Garzik 	get_registers(pegasus, 0x83, 1, &data);
10605b2fc499SJeff Garzik 
10615a9dbfe0SNicolas Kaiser 	if (data == 0xa5)
10625b2fc499SJeff Garzik 		pegasus->chip = 0x8513;
10635a9dbfe0SNicolas Kaiser 	else
10645b2fc499SJeff Garzik 		pegasus->chip = 0;
10655b2fc499SJeff Garzik 
10665b2fc499SJeff Garzik 	set_register(pegasus, 0x80, 0xc0);
10675b2fc499SJeff Garzik 	set_register(pegasus, 0x83, 0xff);
10685b2fc499SJeff Garzik 	set_register(pegasus, 0x84, 0x01);
10695b2fc499SJeff Garzik 
10705b2fc499SJeff Garzik 	if (pegasus->features & HAS_HOME_PNA && mii_mode)
10715b2fc499SJeff Garzik 		set_register(pegasus, Reg81, 6);
10725b2fc499SJeff Garzik 	else
10735b2fc499SJeff Garzik 		set_register(pegasus, Reg81, 2);
10745b2fc499SJeff Garzik }
10755b2fc499SJeff Garzik 
10765b2fc499SJeff Garzik 
1077cda2836dSDavid Brownell static int pegasus_count;
10785a9dbfe0SNicolas Kaiser static struct workqueue_struct *pegasus_workqueue;
10795b2fc499SJeff Garzik #define CARRIER_CHECK_DELAY (2 * HZ)
10805b2fc499SJeff Garzik 
10815b2fc499SJeff Garzik static void check_carrier(struct work_struct *work)
10825b2fc499SJeff Garzik {
10835b2fc499SJeff Garzik 	pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
10845b2fc499SJeff Garzik 	set_carrier(pegasus->net);
10855b2fc499SJeff Garzik 	if (!(pegasus->flags & PEGASUS_UNPLUG)) {
10865b2fc499SJeff Garzik 		queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
10875b2fc499SJeff Garzik 			CARRIER_CHECK_DELAY);
10885b2fc499SJeff Garzik 	}
10895b2fc499SJeff Garzik }
10905b2fc499SJeff Garzik 
10914f63135eSBen Collins static int pegasus_blacklisted(struct usb_device *udev)
10924f63135eSBen Collins {
10934f63135eSBen Collins 	struct usb_device_descriptor *udd = &udev->descriptor;
10944f63135eSBen Collins 
10954f63135eSBen Collins 	/* Special quirk to keep the driver from handling the Belkin Bluetooth
10964f63135eSBen Collins 	 * dongle which happens to have the same ID.
10974f63135eSBen Collins 	 */
1098e3453f63SMichael Buesch 	if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
1099e3453f63SMichael Buesch 	    (udd->idProduct == cpu_to_le16(0x0121)) &&
11004f63135eSBen Collins 	    (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
11014f63135eSBen Collins 	    (udd->bDeviceProtocol == 1))
11024f63135eSBen Collins 		return 1;
11034f63135eSBen Collins 
11044f63135eSBen Collins 	return 0;
11054f63135eSBen Collins }
11064f63135eSBen Collins 
1107cda2836dSDavid Brownell /* we rely on probe() and remove() being serialized so we
1108cda2836dSDavid Brownell  * don't need extra locking on pegasus_count.
1109cda2836dSDavid Brownell  */
1110cda2836dSDavid Brownell static void pegasus_dec_workqueue(void)
1111cda2836dSDavid Brownell {
1112cda2836dSDavid Brownell 	pegasus_count--;
1113cda2836dSDavid Brownell 	if (pegasus_count == 0) {
1114cda2836dSDavid Brownell 		destroy_workqueue(pegasus_workqueue);
1115cda2836dSDavid Brownell 		pegasus_workqueue = NULL;
1116cda2836dSDavid Brownell 	}
1117cda2836dSDavid Brownell }
1118cda2836dSDavid Brownell 
11195b2fc499SJeff Garzik static int pegasus_probe(struct usb_interface *intf,
11205b2fc499SJeff Garzik 			 const struct usb_device_id *id)
11215b2fc499SJeff Garzik {
11225b2fc499SJeff Garzik 	struct usb_device *dev = interface_to_usbdev(intf);
11235b2fc499SJeff Garzik 	struct net_device *net;
11245b2fc499SJeff Garzik 	pegasus_t *pegasus;
11255b2fc499SJeff Garzik 	int dev_index = id - pegasus_ids;
11265b2fc499SJeff Garzik 	int res = -ENOMEM;
11275b2fc499SJeff Garzik 
1128cda2836dSDavid Brownell 	if (pegasus_blacklisted(dev))
1129cda2836dSDavid Brownell 		return -ENODEV;
11304f63135eSBen Collins 
1131cda2836dSDavid Brownell 	if (pegasus_count == 0) {
1132cda2836dSDavid Brownell 		pegasus_workqueue = create_singlethread_workqueue("pegasus");
1133cda2836dSDavid Brownell 		if (!pegasus_workqueue)
1134cda2836dSDavid Brownell 			return -ENOMEM;
11354f63135eSBen Collins 	}
1136cda2836dSDavid Brownell 	pegasus_count++;
1137cda2836dSDavid Brownell 
11385b2fc499SJeff Garzik 	net = alloc_etherdev(sizeof(struct pegasus));
113941de8d4cSJoe Perches 	if (!net)
11405b2fc499SJeff Garzik 		goto out;
11415b2fc499SJeff Garzik 
11425b2fc499SJeff Garzik 	pegasus = netdev_priv(net);
11435b2fc499SJeff Garzik 	pegasus->dev_index = dev_index;
11445b2fc499SJeff Garzik 
1145323b3496SPetko Manolov 	res = alloc_urbs(pegasus);
1146323b3496SPetko Manolov 	if (res < 0) {
11475b2fc499SJeff Garzik 		dev_err(&intf->dev, "can't allocate %s\n", "urbs");
11485b2fc499SJeff Garzik 		goto out1;
11495b2fc499SJeff Garzik 	}
11505b2fc499SJeff Garzik 
11515b2fc499SJeff Garzik 	tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
11525b2fc499SJeff Garzik 
11535b2fc499SJeff Garzik 	INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
11545b2fc499SJeff Garzik 
11555b2fc499SJeff Garzik 	pegasus->intf = intf;
11565b2fc499SJeff Garzik 	pegasus->usb = dev;
11575b2fc499SJeff Garzik 	pegasus->net = net;
11588cb89571SOliver Neukum 
11598cb89571SOliver Neukum 
11605b2fc499SJeff Garzik 	net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
11618cb89571SOliver Neukum 	net->netdev_ops = &pegasus_netdev_ops;
11627ad24ea4SWilfried Klaebe 	net->ethtool_ops = &ops;
11635b2fc499SJeff Garzik 	pegasus->mii.dev = net;
11645b2fc499SJeff Garzik 	pegasus->mii.mdio_read = mdio_read;
11655b2fc499SJeff Garzik 	pegasus->mii.mdio_write = mdio_write;
11665b2fc499SJeff Garzik 	pegasus->mii.phy_id_mask = 0x1f;
11675b2fc499SJeff Garzik 	pegasus->mii.reg_num_mask = 0x1f;
11685b2fc499SJeff Garzik 	pegasus->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
11695b2fc499SJeff Garzik 				| NETIF_MSG_PROBE | NETIF_MSG_LINK);
11705b2fc499SJeff Garzik 
11715b2fc499SJeff Garzik 	pegasus->features = usb_dev_id[dev_index].private;
11725b2fc499SJeff Garzik 	get_interrupt_interval(pegasus);
11735b2fc499SJeff Garzik 	if (reset_mac(pegasus)) {
11745b2fc499SJeff Garzik 		dev_err(&intf->dev, "can't reset MAC\n");
11755b2fc499SJeff Garzik 		res = -EIO;
11765b2fc499SJeff Garzik 		goto out2;
11775b2fc499SJeff Garzik 	}
11785b2fc499SJeff Garzik 	set_ethernet_addr(pegasus);
11795b2fc499SJeff Garzik 	if (pegasus->features & PEGASUS_II) {
11805b2fc499SJeff Garzik 		dev_info(&intf->dev, "setup Pegasus II specific registers\n");
11815b2fc499SJeff Garzik 		setup_pegasus_II(pegasus);
11825b2fc499SJeff Garzik 	}
11835b2fc499SJeff Garzik 	pegasus->phy = mii_phy_probe(pegasus);
11845b2fc499SJeff Garzik 	if (pegasus->phy == 0xff) {
11855b2fc499SJeff Garzik 		dev_warn(&intf->dev, "can't locate MII phy, using default\n");
11865b2fc499SJeff Garzik 		pegasus->phy = 1;
11875b2fc499SJeff Garzik 	}
11885b2fc499SJeff Garzik 	pegasus->mii.phy_id = pegasus->phy;
11895b2fc499SJeff Garzik 	usb_set_intfdata(intf, pegasus);
11905b2fc499SJeff Garzik 	SET_NETDEV_DEV(net, &intf->dev);
11915b2fc499SJeff Garzik 	pegasus_reset_wol(net);
11925b2fc499SJeff Garzik 	res = register_netdev(net);
11935b2fc499SJeff Garzik 	if (res)
11945b2fc499SJeff Garzik 		goto out3;
11955b2fc499SJeff Garzik 	queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
11965b2fc499SJeff Garzik 			   CARRIER_CHECK_DELAY);
1197323b3496SPetko Manolov 	dev_info(&intf->dev, "%s, %s, %pM\n", net->name,
1198323b3496SPetko Manolov 		 usb_dev_id[dev_index].name, net->dev_addr);
11995b2fc499SJeff Garzik 	return 0;
12005b2fc499SJeff Garzik 
12015b2fc499SJeff Garzik out3:
12025b2fc499SJeff Garzik 	usb_set_intfdata(intf, NULL);
12035b2fc499SJeff Garzik out2:
12045b2fc499SJeff Garzik 	free_all_urbs(pegasus);
12055b2fc499SJeff Garzik out1:
12065b2fc499SJeff Garzik 	free_netdev(net);
12075b2fc499SJeff Garzik out:
1208cda2836dSDavid Brownell 	pegasus_dec_workqueue();
12095b2fc499SJeff Garzik 	return res;
12105b2fc499SJeff Garzik }
12115b2fc499SJeff Garzik 
12125b2fc499SJeff Garzik static void pegasus_disconnect(struct usb_interface *intf)
12135b2fc499SJeff Garzik {
12145b2fc499SJeff Garzik 	struct pegasus *pegasus = usb_get_intfdata(intf);
12155b2fc499SJeff Garzik 
12165b2fc499SJeff Garzik 	usb_set_intfdata(intf, NULL);
12175b2fc499SJeff Garzik 	if (!pegasus) {
12185b2fc499SJeff Garzik 		dev_dbg(&intf->dev, "unregistering non-bound device?\n");
12195b2fc499SJeff Garzik 		return;
12205b2fc499SJeff Garzik 	}
12215b2fc499SJeff Garzik 
12225b2fc499SJeff Garzik 	pegasus->flags |= PEGASUS_UNPLUG;
12235b2fc499SJeff Garzik 	cancel_delayed_work(&pegasus->carrier_check);
12245b2fc499SJeff Garzik 	unregister_netdev(pegasus->net);
12255b2fc499SJeff Garzik 	unlink_all_urbs(pegasus);
12265b2fc499SJeff Garzik 	free_all_urbs(pegasus);
12275b2fc499SJeff Garzik 	if (pegasus->rx_skb != NULL) {
12285b2fc499SJeff Garzik 		dev_kfree_skb(pegasus->rx_skb);
12295b2fc499SJeff Garzik 		pegasus->rx_skb = NULL;
12305b2fc499SJeff Garzik 	}
12315b2fc499SJeff Garzik 	free_netdev(pegasus->net);
1232cda2836dSDavid Brownell 	pegasus_dec_workqueue();
12335b2fc499SJeff Garzik }
12345b2fc499SJeff Garzik 
12355b2fc499SJeff Garzik static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
12365b2fc499SJeff Garzik {
12375b2fc499SJeff Garzik 	struct pegasus *pegasus = usb_get_intfdata(intf);
12385b2fc499SJeff Garzik 
12395b2fc499SJeff Garzik 	netif_device_detach(pegasus->net);
12405b2fc499SJeff Garzik 	cancel_delayed_work(&pegasus->carrier_check);
12415b2fc499SJeff Garzik 	if (netif_running(pegasus->net)) {
12425b2fc499SJeff Garzik 		usb_kill_urb(pegasus->rx_urb);
12435b2fc499SJeff Garzik 		usb_kill_urb(pegasus->intr_urb);
12445b2fc499SJeff Garzik 	}
12455b2fc499SJeff Garzik 	return 0;
12465b2fc499SJeff Garzik }
12475b2fc499SJeff Garzik 
12485b2fc499SJeff Garzik static int pegasus_resume(struct usb_interface *intf)
12495b2fc499SJeff Garzik {
12505b2fc499SJeff Garzik 	struct pegasus *pegasus = usb_get_intfdata(intf);
12515b2fc499SJeff Garzik 
12525b2fc499SJeff Garzik 	netif_device_attach(pegasus->net);
12535b2fc499SJeff Garzik 	if (netif_running(pegasus->net)) {
12545b2fc499SJeff Garzik 		pegasus->rx_urb->status = 0;
12555b2fc499SJeff Garzik 		pegasus->rx_urb->actual_length = 0;
12565b2fc499SJeff Garzik 		read_bulk_callback(pegasus->rx_urb);
12575b2fc499SJeff Garzik 
12585b2fc499SJeff Garzik 		pegasus->intr_urb->status = 0;
12595b2fc499SJeff Garzik 		pegasus->intr_urb->actual_length = 0;
12605b2fc499SJeff Garzik 		intr_callback(pegasus->intr_urb);
12615b2fc499SJeff Garzik 	}
12625b2fc499SJeff Garzik 	queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
12635b2fc499SJeff Garzik 				CARRIER_CHECK_DELAY);
12645b2fc499SJeff Garzik 	return 0;
12655b2fc499SJeff Garzik }
12665b2fc499SJeff Garzik 
12678cb89571SOliver Neukum static const struct net_device_ops pegasus_netdev_ops = {
12688cb89571SOliver Neukum 	.ndo_open =			pegasus_open,
12698cb89571SOliver Neukum 	.ndo_stop =			pegasus_close,
12708cb89571SOliver Neukum 	.ndo_do_ioctl =			pegasus_ioctl,
12718cb89571SOliver Neukum 	.ndo_start_xmit =		pegasus_start_xmit,
1272afc4b13dSJiri Pirko 	.ndo_set_rx_mode =		pegasus_set_multicast,
12738cb89571SOliver Neukum 	.ndo_get_stats =		pegasus_netdev_stats,
12748cb89571SOliver Neukum 	.ndo_tx_timeout =		pegasus_tx_timeout,
1275635ecaa7SBen Hutchings 	.ndo_change_mtu =		eth_change_mtu,
1276240c102dSBen Hutchings 	.ndo_set_mac_address =		eth_mac_addr,
1277240c102dSBen Hutchings 	.ndo_validate_addr =		eth_validate_addr,
12788cb89571SOliver Neukum };
12798cb89571SOliver Neukum 
12805b2fc499SJeff Garzik static struct usb_driver pegasus_driver = {
12815b2fc499SJeff Garzik 	.name = driver_name,
12825b2fc499SJeff Garzik 	.probe = pegasus_probe,
12835b2fc499SJeff Garzik 	.disconnect = pegasus_disconnect,
12845b2fc499SJeff Garzik 	.id_table = pegasus_ids,
12855b2fc499SJeff Garzik 	.suspend = pegasus_suspend,
12865b2fc499SJeff Garzik 	.resume = pegasus_resume,
1287e1f12eb6SSarah Sharp 	.disable_hub_initiated_lpm = 1,
12885b2fc499SJeff Garzik };
12895b2fc499SJeff Garzik 
1290cda2836dSDavid Brownell static void __init parse_id(char *id)
12915b2fc499SJeff Garzik {
12925b2fc499SJeff Garzik 	unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0;
12935b2fc499SJeff Garzik 	char *token, *name = NULL;
12945b2fc499SJeff Garzik 
12955b2fc499SJeff Garzik 	if ((token = strsep(&id, ":")) != NULL)
12965b2fc499SJeff Garzik 		name = token;
12975b2fc499SJeff Garzik 	/* name now points to a null terminated string*/
12985b2fc499SJeff Garzik 	if ((token = strsep(&id, ":")) != NULL)
12995b2fc499SJeff Garzik 		vendor_id = simple_strtoul(token, NULL, 16);
13005b2fc499SJeff Garzik 	if ((token = strsep(&id, ":")) != NULL)
13015b2fc499SJeff Garzik 		device_id = simple_strtoul(token, NULL, 16);
13025b2fc499SJeff Garzik 	flags = simple_strtoul(id, NULL, 16);
13035b2fc499SJeff Garzik 	pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
13045b2fc499SJeff Garzik 		driver_name, name, vendor_id, device_id, flags);
13055b2fc499SJeff Garzik 
13065b2fc499SJeff Garzik 	if (vendor_id > 0x10000 || vendor_id == 0)
13075b2fc499SJeff Garzik 		return;
13085b2fc499SJeff Garzik 	if (device_id > 0x10000 || device_id == 0)
13095b2fc499SJeff Garzik 		return;
13105b2fc499SJeff Garzik 
13115b2fc499SJeff Garzik 	for (i = 0; usb_dev_id[i].name; i++);
13125b2fc499SJeff Garzik 	usb_dev_id[i].name = name;
13135b2fc499SJeff Garzik 	usb_dev_id[i].vendor = vendor_id;
13145b2fc499SJeff Garzik 	usb_dev_id[i].device = device_id;
13155b2fc499SJeff Garzik 	usb_dev_id[i].private = flags;
13165b2fc499SJeff Garzik 	pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
13175b2fc499SJeff Garzik 	pegasus_ids[i].idVendor = vendor_id;
13185b2fc499SJeff Garzik 	pegasus_ids[i].idProduct = device_id;
13195b2fc499SJeff Garzik }
13205b2fc499SJeff Garzik 
13215b2fc499SJeff Garzik static int __init pegasus_init(void)
13225b2fc499SJeff Garzik {
13235b2fc499SJeff Garzik 	pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
13245b2fc499SJeff Garzik 	if (devid)
13255b2fc499SJeff Garzik 		parse_id(devid);
13265b2fc499SJeff Garzik 	return usb_register(&pegasus_driver);
13275b2fc499SJeff Garzik }
13285b2fc499SJeff Garzik 
13295b2fc499SJeff Garzik static void __exit pegasus_exit(void)
13305b2fc499SJeff Garzik {
13315b2fc499SJeff Garzik 	usb_deregister(&pegasus_driver);
13325b2fc499SJeff Garzik }
13335b2fc499SJeff Garzik 
13345b2fc499SJeff Garzik module_init(pegasus_init);
13355b2fc499SJeff Garzik module_exit(pegasus_exit);
1336