xref: /openbmc/linux/drivers/net/usb/pegasus.c (revision 23a64c514631fc7c96dbe3ac3fe355e51df2a9a8)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25b2fc499SJeff Garzik /*
3323b3496SPetko Manolov  *  Copyright (c) 1999-2013 Petko Manolov (petkan@nucleusys.com)
45b2fc499SJeff Garzik  *
55b2fc499SJeff Garzik  *	ChangeLog:
65b2fc499SJeff Garzik  *		....	Most of the time spent on reading sources & docs.
75b2fc499SJeff Garzik  *		v0.2.x	First official release for the Linux kernel.
85b2fc499SJeff Garzik  *		v0.3.0	Beutified and structured, some bugs fixed.
95b2fc499SJeff Garzik  *		v0.3.x	URBifying bulk requests and bugfixing. First relatively
105b2fc499SJeff Garzik  *			stable release. Still can touch device's registers only
115b2fc499SJeff Garzik  *			from top-halves.
125b2fc499SJeff Garzik  *		v0.4.0	Control messages remained unurbified are now URBs.
135b2fc499SJeff Garzik  *			Now we can touch the HW at any time.
145b2fc499SJeff Garzik  *		v0.4.9	Control urbs again use process context to wait. Argh...
155b2fc499SJeff Garzik  *			Some long standing bugs (enable_net_traffic) fixed.
165b2fc499SJeff Garzik  *			Also nasty trick about resubmiting control urb from
175b2fc499SJeff Garzik  *			interrupt context used. Please let me know how it
185b2fc499SJeff Garzik  *			behaves. Pegasus II support added since this version.
195b2fc499SJeff Garzik  *			TODO: suppressing HCD warnings spewage on disconnect.
205b2fc499SJeff Garzik  *		v0.4.13	Ethernet address is now set at probe(), not at open()
215b2fc499SJeff Garzik  *			time as this seems to break dhcpd.
225b2fc499SJeff Garzik  *		v0.5.0	branch to 2.5.x kernels
235b2fc499SJeff Garzik  *		v0.5.1	ethtool support added
245b2fc499SJeff Garzik  *		v0.5.5	rx socket buffers are in a pool and the their allocation
255b2fc499SJeff Garzik  *			is out of the interrupt routine.
26323b3496SPetko Manolov  *		...
27323b3496SPetko Manolov  *		v0.9.3	simplified [get|set]_register(s), async update registers
28323b3496SPetko Manolov  *			logic revisited, receive skb_pool removed.
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>
427c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
435b2fc499SJeff Garzik #include "pegasus.h"
445b2fc499SJeff Garzik 
455b2fc499SJeff Garzik /*
465b2fc499SJeff Garzik  * Version Information
475b2fc499SJeff Garzik  */
48323b3496SPetko Manolov #define DRIVER_VERSION "v0.9.3 (2013/04/25)"
49323b3496SPetko Manolov #define DRIVER_AUTHOR "Petko Manolov <petkan@nucleusys.com>"
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)
572f5107c3SPetko Manolov #define CARRIER_CHECK_DELAY (2 * HZ)
585b2fc499SJeff Garzik 
59eb939922SRusty Russell static bool loopback;
60eb939922SRusty Russell static bool mii_mode;
615a9dbfe0SNicolas Kaiser static char *devid;
625b2fc499SJeff Garzik 
635b2fc499SJeff Garzik static struct usb_eth_dev usb_dev_id[] = {
645b2fc499SJeff Garzik #define	PEGASUS_DEV(pn, vid, pid, flags)	\
655b2fc499SJeff Garzik 	{.name = pn, .vendor = vid, .device = pid, .private = flags},
66ab854b24SChris Rankin #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
67ab854b24SChris Rankin 	PEGASUS_DEV(pn, vid, pid, flags)
685b2fc499SJeff Garzik #include "pegasus.h"
695b2fc499SJeff Garzik #undef	PEGASUS_DEV
70ab854b24SChris Rankin #undef	PEGASUS_DEV_CLASS
715b2fc499SJeff Garzik 	{NULL, 0, 0, 0},
725b2fc499SJeff Garzik 	{NULL, 0, 0, 0}
735b2fc499SJeff Garzik };
745b2fc499SJeff Garzik 
755b2fc499SJeff Garzik static struct usb_device_id pegasus_ids[] = {
765b2fc499SJeff Garzik #define	PEGASUS_DEV(pn, vid, pid, flags) \
775b2fc499SJeff Garzik 	{.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
78ab854b24SChris Rankin /*
79ab854b24SChris Rankin  * The Belkin F8T012xx1 bluetooth adaptor has the same vendor and product
80ab854b24SChris Rankin  * IDs as the Belkin F5D5050, so we need to teach the pegasus driver to
81ab854b24SChris Rankin  * ignore adaptors belonging to the "Wireless" class 0xE0. For this one
82ab854b24SChris Rankin  * case anyway, seeing as the pegasus is for "Wired" adaptors.
83ab854b24SChris Rankin  */
84ab854b24SChris Rankin #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
85ab854b24SChris Rankin 	{.match_flags = (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_CLASS), \
86ab854b24SChris Rankin 	.idVendor = vid, .idProduct = pid, .bDeviceClass = dclass},
875b2fc499SJeff Garzik #include "pegasus.h"
885b2fc499SJeff Garzik #undef	PEGASUS_DEV
89ab854b24SChris Rankin #undef	PEGASUS_DEV_CLASS
905b2fc499SJeff Garzik 	{},
915b2fc499SJeff Garzik 	{}
925b2fc499SJeff Garzik };
935b2fc499SJeff Garzik 
945b2fc499SJeff Garzik MODULE_AUTHOR(DRIVER_AUTHOR);
955b2fc499SJeff Garzik MODULE_DESCRIPTION(DRIVER_DESC);
965b2fc499SJeff Garzik MODULE_LICENSE("GPL");
975b2fc499SJeff Garzik module_param(loopback, bool, 0);
985b2fc499SJeff Garzik module_param(mii_mode, bool, 0);
995b2fc499SJeff Garzik module_param(devid, charp, 0);
1005b2fc499SJeff Garzik MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
1015b2fc499SJeff Garzik MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
1025b2fc499SJeff Garzik MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
1035b2fc499SJeff Garzik 
1045b2fc499SJeff Garzik /* use ethtool to change the level for any given device */
1055b2fc499SJeff Garzik static int msg_level = -1;
1065b2fc499SJeff Garzik module_param(msg_level, int, 0);
1075b2fc499SJeff Garzik MODULE_PARM_DESC(msg_level, "Override default message level");
1085b2fc499SJeff Garzik 
1095b2fc499SJeff Garzik MODULE_DEVICE_TABLE(usb, pegasus_ids);
1108cb89571SOliver Neukum static const struct net_device_ops pegasus_netdev_ops;
1115b2fc499SJeff Garzik 
112323b3496SPetko Manolov /*****/
113323b3496SPetko Manolov 
114323b3496SPetko Manolov static void async_ctrl_callback(struct urb *urb)
1155b2fc499SJeff Garzik {
116323b3496SPetko Manolov 	struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
117c94cb314SOliver Neukum 	int status = urb->status;
1185b2fc499SJeff Garzik 
119323b3496SPetko Manolov 	if (status < 0)
120323b3496SPetko Manolov 		dev_dbg(&urb->dev->dev, "%s failed with %d", __func__, status);
121323b3496SPetko Manolov 	kfree(req);
122323b3496SPetko Manolov 	usb_free_urb(urb);
1235b2fc499SJeff Garzik }
1245b2fc499SJeff Garzik 
125323b3496SPetko Manolov static int get_registers(pegasus_t *pegasus, __u16 indx, __u16 size, void *data)
1265b2fc499SJeff Garzik {
127fb58cf4fSPetko Manolov 	return usb_control_msg_recv(pegasus->usb, 0, PEGASUS_REQ_GET_REGS,
128fb58cf4fSPetko Manolov 				   PEGASUS_REQT_READ, 0, indx, data, size,
129fb58cf4fSPetko Manolov 				   1000, GFP_NOIO);
1305b2fc499SJeff Garzik }
1315b2fc499SJeff Garzik 
1325593523fSBen Hutchings static int set_registers(pegasus_t *pegasus, __u16 indx, __u16 size,
1335593523fSBen Hutchings 			 const void *data)
1345b2fc499SJeff Garzik {
135fb58cf4fSPetko Manolov 	return usb_control_msg_send(pegasus->usb, 0, PEGASUS_REQ_SET_REGS,
136fb58cf4fSPetko Manolov 				    PEGASUS_REQT_WRITE, 0, indx, data, size,
137fb58cf4fSPetko Manolov 				    1000, GFP_NOIO);
1385b2fc499SJeff Garzik }
1395b2fc499SJeff Garzik 
140fb58cf4fSPetko Manolov /*
141fb58cf4fSPetko Manolov  * There is only one way to write to a single ADM8511 register and this is via
142fb58cf4fSPetko Manolov  * specific control request.  'data' is ignored by the device, but it is here to
143fb58cf4fSPetko Manolov  * not break the API.
144fb58cf4fSPetko Manolov  */
1455b2fc499SJeff Garzik static int set_register(pegasus_t *pegasus, __u16 indx, __u8 data)
1465b2fc499SJeff Garzik {
147fb58cf4fSPetko Manolov 	void *buf = &data;
1485b2fc499SJeff Garzik 
149fb58cf4fSPetko Manolov 	return usb_control_msg_send(pegasus->usb, 0, PEGASUS_REQ_SET_REG,
150fb58cf4fSPetko Manolov 				    PEGASUS_REQT_WRITE, data, indx, buf, 1,
151fb58cf4fSPetko Manolov 				    1000, GFP_NOIO);
1525b2fc499SJeff Garzik }
1535b2fc499SJeff Garzik 
1545b2fc499SJeff Garzik static int update_eth_regs_async(pegasus_t *pegasus)
1555b2fc499SJeff Garzik {
156323b3496SPetko Manolov 	int ret = -ENOMEM;
157323b3496SPetko Manolov 	struct urb *async_urb;
158323b3496SPetko Manolov 	struct usb_ctrlrequest *req;
1595b2fc499SJeff Garzik 
160323b3496SPetko Manolov 	req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
161323b3496SPetko Manolov 	if (req == NULL)
162323b3496SPetko Manolov 		return ret;
1635b2fc499SJeff Garzik 
164323b3496SPetko Manolov 	async_urb = usb_alloc_urb(0, GFP_ATOMIC);
165323b3496SPetko Manolov 	if (async_urb == NULL) {
166323b3496SPetko Manolov 		kfree(req);
167323b3496SPetko Manolov 		return ret;
168323b3496SPetko Manolov 	}
169323b3496SPetko Manolov 	req->bRequestType = PEGASUS_REQT_WRITE;
170323b3496SPetko Manolov 	req->bRequest = PEGASUS_REQ_SET_REGS;
171323b3496SPetko Manolov 	req->wValue = cpu_to_le16(0);
172323b3496SPetko Manolov 	req->wIndex = cpu_to_le16(EthCtrl0);
173323b3496SPetko Manolov 	req->wLength = cpu_to_le16(3);
1745b2fc499SJeff Garzik 
175323b3496SPetko Manolov 	usb_fill_control_urb(async_urb, pegasus->usb,
176323b3496SPetko Manolov 			     usb_sndctrlpipe(pegasus->usb, 0), (void *)req,
177323b3496SPetko Manolov 			     pegasus->eth_regs, 3, async_ctrl_callback, req);
178323b3496SPetko Manolov 
179323b3496SPetko Manolov 	ret = usb_submit_urb(async_urb, GFP_ATOMIC);
180323b3496SPetko Manolov 	if (ret) {
1815b2fc499SJeff Garzik 		if (ret == -ENODEV)
1825b2fc499SJeff Garzik 			netif_device_detach(pegasus->net);
183a475f603SJoe Perches 		netif_err(pegasus, drv, pegasus->net,
184323b3496SPetko Manolov 			  "%s returned %d\n", __func__, ret);
1855b2fc499SJeff Garzik 	}
1865b2fc499SJeff Garzik 	return ret;
1875b2fc499SJeff Garzik }
1885b2fc499SJeff Garzik 
1892bd64701SPetko Manolov static int __mii_op(pegasus_t *p, __u8 phy, __u8 indx, __u16 *regd, __u8 cmd)
1905b2fc499SJeff Garzik {
1915b2fc499SJeff Garzik 	int i;
1925b2fc499SJeff Garzik 	__u8 data[4] = { phy, 0, 0, indx };
1935b2fc499SJeff Garzik 	__le16 regdi;
1942bd64701SPetko Manolov 	int ret = -ETIMEDOUT;
1955b2fc499SJeff Garzik 
1962bd64701SPetko Manolov 	if (cmd & PHY_WRITE) {
1972bd64701SPetko Manolov 		__le16 *t = (__le16 *) & data[1];
1982bd64701SPetko Manolov 		*t = cpu_to_le16(*regd);
1992bd64701SPetko Manolov 	}
2002bd64701SPetko Manolov 	set_register(p, PhyCtrl, 0);
2012bd64701SPetko Manolov 	set_registers(p, PhyAddr, sizeof(data), data);
2022bd64701SPetko Manolov 	set_register(p, PhyCtrl, (indx | cmd));
2035b2fc499SJeff Garzik 	for (i = 0; i < REG_TIMEOUT; i++) {
2042bd64701SPetko Manolov 		ret = get_registers(p, PhyCtrl, 1, data);
2052bd64701SPetko Manolov 		if (ret < 0)
2065b2fc499SJeff Garzik 			goto fail;
2075b2fc499SJeff Garzik 		if (data[0] & PHY_DONE)
2085b2fc499SJeff Garzik 			break;
2095b2fc499SJeff Garzik 	}
210a475f603SJoe Perches 	if (i >= REG_TIMEOUT)
211a475f603SJoe Perches 		goto fail;
2122bd64701SPetko Manolov 	if (cmd & PHY_READ) {
2132bd64701SPetko Manolov 		ret = get_registers(p, PhyData, 2, &regdi);
2145b2fc499SJeff Garzik 		*regd = le16_to_cpu(regdi);
2155b2fc499SJeff Garzik 		return ret;
2162bd64701SPetko Manolov 	}
2172bd64701SPetko Manolov 	return 0;
2185b2fc499SJeff Garzik fail:
2192bd64701SPetko Manolov 	netif_dbg(p, drv, p->net, "%s failed\n", __func__);
2205b2fc499SJeff Garzik 	return ret;
2215b2fc499SJeff Garzik }
2225b2fc499SJeff Garzik 
2232bd64701SPetko Manolov /* Returns non-negative int on success, error on failure */
2242bd64701SPetko Manolov static int read_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
2252bd64701SPetko Manolov {
2262bd64701SPetko Manolov 	return __mii_op(pegasus, phy, indx, regd, PHY_READ);
2272bd64701SPetko Manolov }
2282bd64701SPetko Manolov 
2292bd64701SPetko Manolov /* Returns zero on success, error on failure */
2302bd64701SPetko Manolov static int write_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
2312bd64701SPetko Manolov {
2322bd64701SPetko Manolov 	return __mii_op(pegasus, phy, indx, regd, PHY_WRITE);
2332bd64701SPetko Manolov }
2342bd64701SPetko Manolov 
2355b2fc499SJeff Garzik static int mdio_read(struct net_device *dev, int phy_id, int loc)
2365b2fc499SJeff Garzik {
2378739cfefSJoe Perches 	pegasus_t *pegasus = netdev_priv(dev);
2385b2fc499SJeff Garzik 	u16 res;
2395b2fc499SJeff Garzik 
2405b2fc499SJeff Garzik 	read_mii_word(pegasus, phy_id, loc, &res);
2415b2fc499SJeff Garzik 	return (int)res;
2425b2fc499SJeff Garzik }
2435b2fc499SJeff Garzik 
2445b2fc499SJeff Garzik static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
2455b2fc499SJeff Garzik {
2468739cfefSJoe Perches 	pegasus_t *pegasus = netdev_priv(dev);
2473d64fc70SDan Carpenter 	u16 data = val;
2485b2fc499SJeff Garzik 
2493d64fc70SDan Carpenter 	write_mii_word(pegasus, phy_id, loc, &data);
2505b2fc499SJeff Garzik }
2515b2fc499SJeff Garzik 
2525b2fc499SJeff Garzik static int read_eprom_word(pegasus_t *pegasus, __u8 index, __u16 *retdata)
2535b2fc499SJeff Garzik {
2545b2fc499SJeff Garzik 	int i;
255224c0497SDenis Kirjanov 	__u8 tmp = 0;
2565b2fc499SJeff Garzik 	__le16 retdatai;
2575b2fc499SJeff Garzik 	int ret;
2585b2fc499SJeff Garzik 
2595b2fc499SJeff Garzik 	set_register(pegasus, EpromCtrl, 0);
2605b2fc499SJeff Garzik 	set_register(pegasus, EpromOffset, index);
2615b2fc499SJeff Garzik 	set_register(pegasus, EpromCtrl, EPROM_READ);
2625b2fc499SJeff Garzik 
2635b2fc499SJeff Garzik 	for (i = 0; i < REG_TIMEOUT; i++) {
2645b2fc499SJeff Garzik 		ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
2655b2fc499SJeff Garzik 		if (tmp & EPROM_DONE)
2665b2fc499SJeff Garzik 			break;
2675b2fc499SJeff Garzik 		if (ret == -ESHUTDOWN)
2685b2fc499SJeff Garzik 			goto fail;
2695b2fc499SJeff Garzik 	}
270a475f603SJoe Perches 	if (i >= REG_TIMEOUT)
271a475f603SJoe Perches 		goto fail;
272a475f603SJoe Perches 
2735b2fc499SJeff Garzik 	ret = get_registers(pegasus, EpromData, 2, &retdatai);
2745b2fc499SJeff Garzik 	*retdata = le16_to_cpu(retdatai);
2755b2fc499SJeff Garzik 	return ret;
2765b2fc499SJeff Garzik 
2775b2fc499SJeff Garzik fail:
278a475f603SJoe Perches 	netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
2795b2fc499SJeff Garzik 	return -ETIMEDOUT;
2805b2fc499SJeff Garzik }
2815b2fc499SJeff Garzik 
2825b2fc499SJeff Garzik #ifdef	PEGASUS_WRITE_EEPROM
2835b2fc499SJeff Garzik static inline void enable_eprom_write(pegasus_t *pegasus)
2845b2fc499SJeff Garzik {
2855b2fc499SJeff Garzik 	__u8 tmp;
2865b2fc499SJeff Garzik 
2875b2fc499SJeff Garzik 	get_registers(pegasus, EthCtrl2, 1, &tmp);
2885b2fc499SJeff Garzik 	set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
2895b2fc499SJeff Garzik }
2905b2fc499SJeff Garzik 
2915b2fc499SJeff Garzik static inline void disable_eprom_write(pegasus_t *pegasus)
2925b2fc499SJeff Garzik {
2935b2fc499SJeff Garzik 	__u8 tmp;
2945b2fc499SJeff Garzik 
2955b2fc499SJeff Garzik 	get_registers(pegasus, EthCtrl2, 1, &tmp);
2965b2fc499SJeff Garzik 	set_register(pegasus, EpromCtrl, 0);
2975b2fc499SJeff Garzik 	set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
2985b2fc499SJeff Garzik }
2995b2fc499SJeff Garzik 
3005b2fc499SJeff Garzik static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data)
3015b2fc499SJeff Garzik {
3025b2fc499SJeff Garzik 	int i;
3035b2fc499SJeff Garzik 	__u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
3045b2fc499SJeff Garzik 	int ret;
305e3453f63SMichael Buesch 	__le16 le_data = cpu_to_le16(data);
3065b2fc499SJeff Garzik 
3075b2fc499SJeff Garzik 	set_registers(pegasus, EpromOffset, 4, d);
3085b2fc499SJeff Garzik 	enable_eprom_write(pegasus);
3095b2fc499SJeff Garzik 	set_register(pegasus, EpromOffset, index);
310e3453f63SMichael Buesch 	set_registers(pegasus, EpromData, 2, &le_data);
3115b2fc499SJeff Garzik 	set_register(pegasus, EpromCtrl, EPROM_WRITE);
3125b2fc499SJeff Garzik 
3135b2fc499SJeff Garzik 	for (i = 0; i < REG_TIMEOUT; i++) {
3145b2fc499SJeff Garzik 		ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
3155b2fc499SJeff Garzik 		if (ret == -ESHUTDOWN)
3165b2fc499SJeff Garzik 			goto fail;
3175b2fc499SJeff Garzik 		if (tmp & EPROM_DONE)
3185b2fc499SJeff Garzik 			break;
3195b2fc499SJeff Garzik 	}
3205b2fc499SJeff Garzik 	disable_eprom_write(pegasus);
321a475f603SJoe Perches 	if (i >= REG_TIMEOUT)
322a475f603SJoe Perches 		goto fail;
323a475f603SJoe Perches 
3245b2fc499SJeff Garzik 	return ret;
325a475f603SJoe Perches 
3265b2fc499SJeff Garzik fail:
327a475f603SJoe Perches 	netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
3285b2fc499SJeff Garzik 	return -ETIMEDOUT;
3295b2fc499SJeff Garzik }
3305b2fc499SJeff Garzik #endif				/* PEGASUS_WRITE_EEPROM */
3315b2fc499SJeff Garzik 
332f30e25a9SPetko Manolov static inline int get_node_id(pegasus_t *pegasus, u8 *id)
3335b2fc499SJeff Garzik {
334f30e25a9SPetko Manolov 	int i, ret;
335f30e25a9SPetko Manolov 	u16 w16;
3365b2fc499SJeff Garzik 
3375b2fc499SJeff Garzik 	for (i = 0; i < 3; i++) {
338f30e25a9SPetko Manolov 		ret = read_eprom_word(pegasus, i, &w16);
339f30e25a9SPetko Manolov 		if (ret < 0)
340f30e25a9SPetko Manolov 			return ret;
341da2bbdccSHarvey Harrison 		((__le16 *) id)[i] = cpu_to_le16(w16);
3425b2fc499SJeff Garzik 	}
343f30e25a9SPetko Manolov 
344f30e25a9SPetko Manolov 	return 0;
3455b2fc499SJeff Garzik }
3465b2fc499SJeff Garzik 
3475b2fc499SJeff Garzik static void set_ethernet_addr(pegasus_t *pegasus)
3485b2fc499SJeff Garzik {
349f30e25a9SPetko Manolov 	int ret;
350f30e25a9SPetko Manolov 	u8 node_id[6];
3515b2fc499SJeff Garzik 
3525b2fc499SJeff Garzik 	if (pegasus->features & PEGASUS_II) {
353f30e25a9SPetko Manolov 		ret = get_registers(pegasus, 0x10, sizeof(node_id), node_id);
354f30e25a9SPetko Manolov 		if (ret < 0)
355f30e25a9SPetko Manolov 			goto err;
3565b2fc499SJeff Garzik 	} else {
357f30e25a9SPetko Manolov 		ret = get_node_id(pegasus, node_id);
358f30e25a9SPetko Manolov 		if (ret < 0)
359f30e25a9SPetko Manolov 			goto err;
360f30e25a9SPetko Manolov 		ret = set_registers(pegasus, EthID, sizeof(node_id), node_id);
361f30e25a9SPetko Manolov 		if (ret < 0)
362f30e25a9SPetko Manolov 			goto err;
3635b2fc499SJeff Garzik 	}
364f30e25a9SPetko Manolov 
3655b2fc499SJeff Garzik 	memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
366f30e25a9SPetko Manolov 
367f30e25a9SPetko Manolov 	return;
368f30e25a9SPetko Manolov err:
369f30e25a9SPetko Manolov 	eth_hw_addr_random(pegasus->net);
370f30e25a9SPetko Manolov 	dev_info(&pegasus->intf->dev, "software assigned MAC address.\n");
371f30e25a9SPetko Manolov 
372f30e25a9SPetko Manolov 	return;
3735b2fc499SJeff Garzik }
3745b2fc499SJeff Garzik 
3755b2fc499SJeff Garzik static inline int reset_mac(pegasus_t *pegasus)
3765b2fc499SJeff Garzik {
3775b2fc499SJeff Garzik 	__u8 data = 0x8;
3785b2fc499SJeff Garzik 	int i;
3795b2fc499SJeff Garzik 
3805b2fc499SJeff Garzik 	set_register(pegasus, EthCtrl1, data);
3815b2fc499SJeff Garzik 	for (i = 0; i < REG_TIMEOUT; i++) {
3825b2fc499SJeff Garzik 		get_registers(pegasus, EthCtrl1, 1, &data);
3835b2fc499SJeff Garzik 		if (~data & 0x08) {
384681f1623SDan Carpenter 			if (loopback)
3855b2fc499SJeff Garzik 				break;
3865b2fc499SJeff Garzik 			if (mii_mode && (pegasus->features & HAS_HOME_PNA))
3875b2fc499SJeff Garzik 				set_register(pegasus, Gpio1, 0x34);
3885b2fc499SJeff Garzik 			else
3895b2fc499SJeff Garzik 				set_register(pegasus, Gpio1, 0x26);
3905b2fc499SJeff Garzik 			set_register(pegasus, Gpio0, pegasus->features);
3915b2fc499SJeff Garzik 			set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
3925b2fc499SJeff Garzik 			break;
3935b2fc499SJeff Garzik 		}
3945b2fc499SJeff Garzik 	}
3955b2fc499SJeff Garzik 	if (i == REG_TIMEOUT)
3965b2fc499SJeff Garzik 		return -ETIMEDOUT;
3975b2fc499SJeff Garzik 
3985b2fc499SJeff Garzik 	if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
3995b2fc499SJeff Garzik 	    usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
4005b2fc499SJeff Garzik 		set_register(pegasus, Gpio0, 0x24);
4015b2fc499SJeff Garzik 		set_register(pegasus, Gpio0, 0x26);
4025b2fc499SJeff Garzik 	}
4035b2fc499SJeff Garzik 	if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
4045b2fc499SJeff Garzik 		__u16 auxmode;
4055b2fc499SJeff Garzik 		read_mii_word(pegasus, 3, 0x1b, &auxmode);
4062bd64701SPetko Manolov 		auxmode |= 4;
4072bd64701SPetko Manolov 		write_mii_word(pegasus, 3, 0x1b, &auxmode);
4085b2fc499SJeff Garzik 	}
4095b2fc499SJeff Garzik 
4105b2fc499SJeff Garzik 	return 0;
4115b2fc499SJeff Garzik }
4125b2fc499SJeff Garzik 
4135b2fc499SJeff Garzik static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
4145b2fc499SJeff Garzik {
4155b2fc499SJeff Garzik 	__u16 linkpart;
4165b2fc499SJeff Garzik 	__u8 data[4];
4175b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
4185b2fc499SJeff Garzik 	int ret;
4195b2fc499SJeff Garzik 
4205b2fc499SJeff Garzik 	read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
4211a8deec0SPetko Manolov 	data[0] = 0xc8; /* TX & RX enable, append status, no CRC */
4225b2fc499SJeff Garzik 	data[1] = 0;
4235b2fc499SJeff Garzik 	if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
4245b2fc499SJeff Garzik 		data[1] |= 0x20;	/* set full duplex */
4255b2fc499SJeff Garzik 	if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
4265b2fc499SJeff Garzik 		data[1] |= 0x10;	/* set 100 Mbps */
4275b2fc499SJeff Garzik 	if (mii_mode)
4285b2fc499SJeff Garzik 		data[1] = 0;
429681f1623SDan Carpenter 	data[2] = loopback ? 0x09 : 0x01;
4305b2fc499SJeff Garzik 
4315b2fc499SJeff Garzik 	memcpy(pegasus->eth_regs, data, sizeof(data));
4325b2fc499SJeff Garzik 	ret = set_registers(pegasus, EthCtrl0, 3, data);
4335b2fc499SJeff Garzik 
4345b2fc499SJeff Garzik 	if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
4355b2fc499SJeff Garzik 	    usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
4365b2fc499SJeff Garzik 	    usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
4375b2fc499SJeff Garzik 		u16 auxmode;
4385b2fc499SJeff Garzik 		read_mii_word(pegasus, 0, 0x1b, &auxmode);
4392bd64701SPetko Manolov 		auxmode |= 4;
4402bd64701SPetko Manolov 		write_mii_word(pegasus, 0, 0x1b, &auxmode);
4415b2fc499SJeff Garzik 	}
4425b2fc499SJeff Garzik 
4435b2fc499SJeff Garzik 	return ret;
4445b2fc499SJeff Garzik }
4455b2fc499SJeff Garzik 
4465b2fc499SJeff Garzik static void read_bulk_callback(struct urb *urb)
4475b2fc499SJeff Garzik {
4485b2fc499SJeff Garzik 	pegasus_t *pegasus = urb->context;
4495b2fc499SJeff Garzik 	struct net_device *net;
4505b2fc499SJeff Garzik 	int rx_status, count = urb->actual_length;
451c94cb314SOliver Neukum 	int status = urb->status;
4525b2fc499SJeff Garzik 	u8 *buf = urb->transfer_buffer;
4535b2fc499SJeff Garzik 	__u16 pkt_len;
4545b2fc499SJeff Garzik 
4555b2fc499SJeff Garzik 	if (!pegasus)
4565b2fc499SJeff Garzik 		return;
4575b2fc499SJeff Garzik 
4585b2fc499SJeff Garzik 	net = pegasus->net;
4595b2fc499SJeff Garzik 	if (!netif_device_present(net) || !netif_running(net))
4605b2fc499SJeff Garzik 		return;
4615b2fc499SJeff Garzik 
462c94cb314SOliver Neukum 	switch (status) {
4635b2fc499SJeff Garzik 	case 0:
4645b2fc499SJeff Garzik 		break;
4655b2fc499SJeff Garzik 	case -ETIME:
466a475f603SJoe Perches 		netif_dbg(pegasus, rx_err, net, "reset MAC\n");
4675b2fc499SJeff Garzik 		pegasus->flags &= ~PEGASUS_RX_BUSY;
4685b2fc499SJeff Garzik 		break;
4695b2fc499SJeff Garzik 	case -EPIPE:		/* stall, or disconnect from TT */
4705b2fc499SJeff Garzik 		/* FIXME schedule work to clear the halt */
471a475f603SJoe Perches 		netif_warn(pegasus, rx_err, net, "no rx stall recovery\n");
4725b2fc499SJeff Garzik 		return;
4735b2fc499SJeff Garzik 	case -ENOENT:
4745b2fc499SJeff Garzik 	case -ECONNRESET:
4755b2fc499SJeff Garzik 	case -ESHUTDOWN:
476a475f603SJoe Perches 		netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status);
4775b2fc499SJeff Garzik 		return;
4785b2fc499SJeff Garzik 	default:
479a475f603SJoe Perches 		netif_dbg(pegasus, rx_err, net, "RX status %d\n", status);
4805b2fc499SJeff Garzik 		goto goon;
4815b2fc499SJeff Garzik 	}
4825b2fc499SJeff Garzik 
483bbf178e0Sxypron.glpk@gmx.de 	if (count < 4)
4845b2fc499SJeff Garzik 		goto goon;
4855b2fc499SJeff Garzik 
4865b2fc499SJeff Garzik 	rx_status = buf[count - 2];
4875b2fc499SJeff Garzik 	if (rx_status & 0x1e) {
488a475f603SJoe Perches 		netif_dbg(pegasus, rx_err, net,
489a475f603SJoe Perches 			  "RX packet error %x\n", rx_status);
49082d82938STobias Klauser 		net->stats.rx_errors++;
4915a9dbfe0SNicolas Kaiser 		if (rx_status & 0x06)	/* long or runt	*/
49282d82938STobias Klauser 			net->stats.rx_length_errors++;
4935b2fc499SJeff Garzik 		if (rx_status & 0x08)
49482d82938STobias Klauser 			net->stats.rx_crc_errors++;
4955a9dbfe0SNicolas Kaiser 		if (rx_status & 0x10)	/* extra bits	*/
49682d82938STobias Klauser 			net->stats.rx_frame_errors++;
4975b2fc499SJeff Garzik 		goto goon;
4985b2fc499SJeff Garzik 	}
4995b2fc499SJeff Garzik 	if (pegasus->chip == 0x8513) {
5005b2fc499SJeff Garzik 		pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
5015b2fc499SJeff Garzik 		pkt_len &= 0x0fff;
5025b2fc499SJeff Garzik 		pegasus->rx_skb->data += 2;
5035b2fc499SJeff Garzik 	} else {
5045b2fc499SJeff Garzik 		pkt_len = buf[count - 3] << 8;
5055b2fc499SJeff Garzik 		pkt_len += buf[count - 4];
5065b2fc499SJeff Garzik 		pkt_len &= 0xfff;
5071a8deec0SPetko Manolov 		pkt_len -= 4;
5085b2fc499SJeff Garzik 	}
5095b2fc499SJeff Garzik 
5105b2fc499SJeff Garzik 	/*
5115b2fc499SJeff Garzik 	 * If the packet is unreasonably long, quietly drop it rather than
5125b2fc499SJeff Garzik 	 * kernel panicing by calling skb_put.
5135b2fc499SJeff Garzik 	 */
5145b2fc499SJeff Garzik 	if (pkt_len > PEGASUS_MTU)
5155b2fc499SJeff Garzik 		goto goon;
5165b2fc499SJeff Garzik 
5175b2fc499SJeff Garzik 	/*
5185b2fc499SJeff Garzik 	 * at this point we are sure pegasus->rx_skb != NULL
5195b2fc499SJeff Garzik 	 * so we go ahead and pass up the packet.
5205b2fc499SJeff Garzik 	 */
5215b2fc499SJeff Garzik 	skb_put(pegasus->rx_skb, pkt_len);
5225b2fc499SJeff Garzik 	pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
5235b2fc499SJeff Garzik 	netif_rx(pegasus->rx_skb);
52482d82938STobias Klauser 	net->stats.rx_packets++;
52582d82938STobias Klauser 	net->stats.rx_bytes += pkt_len;
5265b2fc499SJeff Garzik 
5275b2fc499SJeff Garzik 	if (pegasus->flags & PEGASUS_UNPLUG)
5285b2fc499SJeff Garzik 		return;
5295b2fc499SJeff Garzik 
530313a58e4SPetko Manolov 	pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, PEGASUS_MTU,
531313a58e4SPetko Manolov 						      GFP_ATOMIC);
5325b2fc499SJeff Garzik 
5335b2fc499SJeff Garzik 	if (pegasus->rx_skb == NULL)
5345b2fc499SJeff Garzik 		goto tl_sched;
5355b2fc499SJeff Garzik goon:
5365b2fc499SJeff Garzik 	usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
5375b2fc499SJeff Garzik 			  usb_rcvbulkpipe(pegasus->usb, 1),
538b7302ca0SPetko Manolov 			  pegasus->rx_skb->data, PEGASUS_MTU,
5395b2fc499SJeff Garzik 			  read_bulk_callback, pegasus);
5405b2fc499SJeff Garzik 	rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
5415b2fc499SJeff Garzik 	if (rx_status == -ENODEV)
5425b2fc499SJeff Garzik 		netif_device_detach(pegasus->net);
5435b2fc499SJeff Garzik 	else if (rx_status) {
5445b2fc499SJeff Garzik 		pegasus->flags |= PEGASUS_RX_URB_FAIL;
5455b2fc499SJeff Garzik 		goto tl_sched;
5465b2fc499SJeff Garzik 	} else {
5475b2fc499SJeff Garzik 		pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
5485b2fc499SJeff Garzik 	}
5495b2fc499SJeff Garzik 
5505b2fc499SJeff Garzik 	return;
5515b2fc499SJeff Garzik 
5525b2fc499SJeff Garzik tl_sched:
5535b2fc499SJeff Garzik 	tasklet_schedule(&pegasus->rx_tl);
5545b2fc499SJeff Garzik }
5555b2fc499SJeff Garzik 
556*23a64c51SEmil Renner Berthing static void rx_fixup(struct tasklet_struct *t)
5575b2fc499SJeff Garzik {
558*23a64c51SEmil Renner Berthing 	pegasus_t *pegasus = from_tasklet(pegasus, t, rx_tl);
5595b2fc499SJeff Garzik 	int status;
5605b2fc499SJeff Garzik 
5615b2fc499SJeff Garzik 	if (pegasus->flags & PEGASUS_UNPLUG)
5625b2fc499SJeff Garzik 		return;
5635b2fc499SJeff Garzik 
5645b2fc499SJeff Garzik 	if (pegasus->flags & PEGASUS_RX_URB_FAIL)
5655b2fc499SJeff Garzik 		if (pegasus->rx_skb)
5665b2fc499SJeff Garzik 			goto try_again;
5675a9dbfe0SNicolas Kaiser 	if (pegasus->rx_skb == NULL)
568313a58e4SPetko Manolov 		pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
569313a58e4SPetko Manolov 							      PEGASUS_MTU,
570313a58e4SPetko Manolov 							      GFP_ATOMIC);
5715b2fc499SJeff Garzik 	if (pegasus->rx_skb == NULL) {
572a475f603SJoe Perches 		netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
5735b2fc499SJeff Garzik 		tasklet_schedule(&pegasus->rx_tl);
574313a58e4SPetko Manolov 		return;
5755b2fc499SJeff Garzik 	}
5765b2fc499SJeff Garzik 	usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
5775b2fc499SJeff Garzik 			  usb_rcvbulkpipe(pegasus->usb, 1),
578b7302ca0SPetko Manolov 			  pegasus->rx_skb->data, PEGASUS_MTU,
5795b2fc499SJeff Garzik 			  read_bulk_callback, pegasus);
5805b2fc499SJeff Garzik try_again:
5815b2fc499SJeff Garzik 	status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
5825b2fc499SJeff Garzik 	if (status == -ENODEV)
5835b2fc499SJeff Garzik 		netif_device_detach(pegasus->net);
5845b2fc499SJeff Garzik 	else if (status) {
5855b2fc499SJeff Garzik 		pegasus->flags |= PEGASUS_RX_URB_FAIL;
5865b2fc499SJeff Garzik 		tasklet_schedule(&pegasus->rx_tl);
5875b2fc499SJeff Garzik 	} else {
5885b2fc499SJeff Garzik 		pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
5895b2fc499SJeff Garzik 	}
5905b2fc499SJeff Garzik }
5915b2fc499SJeff Garzik 
5925b2fc499SJeff Garzik static void write_bulk_callback(struct urb *urb)
5935b2fc499SJeff Garzik {
5945b2fc499SJeff Garzik 	pegasus_t *pegasus = urb->context;
5959351982bSMicah Gruber 	struct net_device *net;
596c94cb314SOliver Neukum 	int status = urb->status;
5975b2fc499SJeff Garzik 
5985b2fc499SJeff Garzik 	if (!pegasus)
5995b2fc499SJeff Garzik 		return;
6005b2fc499SJeff Garzik 
6019351982bSMicah Gruber 	net = pegasus->net;
6029351982bSMicah Gruber 
6035b2fc499SJeff Garzik 	if (!netif_device_present(net) || !netif_running(net))
6045b2fc499SJeff Garzik 		return;
6055b2fc499SJeff Garzik 
606c94cb314SOliver Neukum 	switch (status) {
6075b2fc499SJeff Garzik 	case -EPIPE:
6085b2fc499SJeff Garzik 		/* FIXME schedule_work() to clear the tx halt */
6095b2fc499SJeff Garzik 		netif_stop_queue(net);
610a475f603SJoe Perches 		netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
6115b2fc499SJeff Garzik 		return;
6125b2fc499SJeff Garzik 	case -ENOENT:
6135b2fc499SJeff Garzik 	case -ECONNRESET:
6145b2fc499SJeff Garzik 	case -ESHUTDOWN:
615a475f603SJoe Perches 		netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
6165b2fc499SJeff Garzik 		return;
6175b2fc499SJeff Garzik 	default:
618a475f603SJoe Perches 		netif_info(pegasus, tx_err, net, "TX status %d\n", status);
619df561f66SGustavo A. R. Silva 		fallthrough;
6205b2fc499SJeff Garzik 	case 0:
6215b2fc499SJeff Garzik 		break;
6225b2fc499SJeff Garzik 	}
6235b2fc499SJeff Garzik 
624860e9538SFlorian Westphal 	netif_trans_update(net); /* prevent tx timeout */
6255b2fc499SJeff Garzik 	netif_wake_queue(net);
6265b2fc499SJeff Garzik }
6275b2fc499SJeff Garzik 
6285b2fc499SJeff Garzik static void intr_callback(struct urb *urb)
6295b2fc499SJeff Garzik {
6305b2fc499SJeff Garzik 	pegasus_t *pegasus = urb->context;
6315b2fc499SJeff Garzik 	struct net_device *net;
632c94cb314SOliver Neukum 	int res, status = urb->status;
6335b2fc499SJeff Garzik 
6345b2fc499SJeff Garzik 	if (!pegasus)
6355b2fc499SJeff Garzik 		return;
6365b2fc499SJeff Garzik 	net = pegasus->net;
6375b2fc499SJeff Garzik 
638c94cb314SOliver Neukum 	switch (status) {
6395b2fc499SJeff Garzik 	case 0:
6405b2fc499SJeff Garzik 		break;
6415b2fc499SJeff Garzik 	case -ECONNRESET:	/* unlink */
6425b2fc499SJeff Garzik 	case -ENOENT:
6435b2fc499SJeff Garzik 	case -ESHUTDOWN:
6445b2fc499SJeff Garzik 		return;
6455b2fc499SJeff Garzik 	default:
6465b2fc499SJeff Garzik 		/* some Pegasus-I products report LOTS of data
6475b2fc499SJeff Garzik 		 * toggle errors... avoid log spamming
6485b2fc499SJeff Garzik 		 */
649a475f603SJoe Perches 		netif_dbg(pegasus, timer, net, "intr status %d\n", status);
6505b2fc499SJeff Garzik 	}
6515b2fc499SJeff Garzik 
6525b2fc499SJeff Garzik 	if (urb->actual_length >= 6) {
6535b2fc499SJeff Garzik 		u8 *d = urb->transfer_buffer;
6545b2fc499SJeff Garzik 
6555b2fc499SJeff Garzik 		/* byte 0 == tx_status1, reg 2B */
6565b2fc499SJeff Garzik 		if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
6575b2fc499SJeff Garzik 					|LATE_COL|JABBER_TIMEOUT)) {
65882d82938STobias Klauser 			net->stats.tx_errors++;
6595b2fc499SJeff Garzik 			if (d[0] & TX_UNDERRUN)
66082d82938STobias Klauser 				net->stats.tx_fifo_errors++;
6615b2fc499SJeff Garzik 			if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
66282d82938STobias Klauser 				net->stats.tx_aborted_errors++;
6635b2fc499SJeff Garzik 			if (d[0] & LATE_COL)
66482d82938STobias Klauser 				net->stats.tx_window_errors++;
6655b2fc499SJeff Garzik 		}
6665b2fc499SJeff Garzik 
6675b2fc499SJeff Garzik 		/* d[5].LINK_STATUS lies on some adapters.
6685b2fc499SJeff Garzik 		 * d[0].NO_CARRIER kicks in only with failed TX.
6695b2fc499SJeff Garzik 		 * ... so monitoring with MII may be safest.
6705b2fc499SJeff Garzik 		 */
6715b2fc499SJeff Garzik 
6725b2fc499SJeff Garzik 		/* bytes 3-4 == rx_lostpkt, reg 2E/2F */
67382d82938STobias Klauser 		net->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
6745b2fc499SJeff Garzik 	}
6755b2fc499SJeff Garzik 
676c94cb314SOliver Neukum 	res = usb_submit_urb(urb, GFP_ATOMIC);
677c94cb314SOliver Neukum 	if (res == -ENODEV)
6785b2fc499SJeff Garzik 		netif_device_detach(pegasus->net);
679a475f603SJoe Perches 	if (res)
680a475f603SJoe Perches 		netif_err(pegasus, timer, net,
681a475f603SJoe Perches 			  "can't resubmit interrupt urb, %d\n", res);
6825b2fc499SJeff Garzik }
6835b2fc499SJeff Garzik 
6840290bd29SMichael S. Tsirkin static void pegasus_tx_timeout(struct net_device *net, unsigned int txqueue)
6855b2fc499SJeff Garzik {
6865b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
687a475f603SJoe Perches 	netif_warn(pegasus, timer, net, "tx timeout\n");
6885b2fc499SJeff Garzik 	usb_unlink_urb(pegasus->tx_urb);
68982d82938STobias Klauser 	net->stats.tx_errors++;
6905b2fc499SJeff Garzik }
6915b2fc499SJeff Garzik 
69225a79c41SStephen Hemminger static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
69325a79c41SStephen Hemminger 					    struct net_device *net)
6945b2fc499SJeff Garzik {
6955b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
6965b2fc499SJeff Garzik 	int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
6975b2fc499SJeff Garzik 	int res;
6985b2fc499SJeff Garzik 	__u16 l16 = skb->len;
6995b2fc499SJeff Garzik 
7005b2fc499SJeff Garzik 	netif_stop_queue(net);
7015b2fc499SJeff Garzik 
7025b2fc499SJeff Garzik 	((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
7035b2fc499SJeff Garzik 	skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
7045b2fc499SJeff Garzik 	usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
7055b2fc499SJeff Garzik 			  usb_sndbulkpipe(pegasus->usb, 2),
7065b2fc499SJeff Garzik 			  pegasus->tx_buff, count,
7075b2fc499SJeff Garzik 			  write_bulk_callback, pegasus);
7085b2fc499SJeff Garzik 	if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
709a475f603SJoe Perches 		netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
7105b2fc499SJeff Garzik 		switch (res) {
7115b2fc499SJeff Garzik 		case -EPIPE:		/* stall, or disconnect from TT */
7125b2fc499SJeff Garzik 			/* cleanup should already have been scheduled */
7135b2fc499SJeff Garzik 			break;
7145b2fc499SJeff Garzik 		case -ENODEV:		/* disconnect() upcoming */
7159dd014ebSOliver Neukum 		case -EPERM:
7165b2fc499SJeff Garzik 			netif_device_detach(pegasus->net);
7175b2fc499SJeff Garzik 			break;
7185b2fc499SJeff Garzik 		default:
71982d82938STobias Klauser 			net->stats.tx_errors++;
7205b2fc499SJeff Garzik 			netif_start_queue(net);
7215b2fc499SJeff Garzik 		}
7225b2fc499SJeff Garzik 	} else {
72382d82938STobias Klauser 		net->stats.tx_packets++;
72482d82938STobias Klauser 		net->stats.tx_bytes += skb->len;
7255b2fc499SJeff Garzik 	}
7265b2fc499SJeff Garzik 	dev_kfree_skb(skb);
7275b2fc499SJeff Garzik 
7286ed10654SPatrick McHardy 	return NETDEV_TX_OK;
7295b2fc499SJeff Garzik }
7305b2fc499SJeff Garzik 
7315b2fc499SJeff Garzik static inline void disable_net_traffic(pegasus_t *pegasus)
7325b2fc499SJeff Garzik {
733e3453f63SMichael Buesch 	__le16 tmp = cpu_to_le16(0);
7345b2fc499SJeff Garzik 
735e3453f63SMichael Buesch 	set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
7365b2fc499SJeff Garzik }
7375b2fc499SJeff Garzik 
7385b2fc499SJeff Garzik static inline void get_interrupt_interval(pegasus_t *pegasus)
7395b2fc499SJeff Garzik {
740e3453f63SMichael Buesch 	u16 data;
741e3453f63SMichael Buesch 	u8 interval;
7425b2fc499SJeff Garzik 
743e3453f63SMichael Buesch 	read_eprom_word(pegasus, 4, &data);
744e3453f63SMichael Buesch 	interval = data >> 8;
7455b2fc499SJeff Garzik 	if (pegasus->usb->speed != USB_SPEED_HIGH) {
746e3453f63SMichael Buesch 		if (interval < 0x80) {
747a475f603SJoe Perches 			netif_info(pegasus, timer, pegasus->net,
748a475f603SJoe Perches 				   "intr interval changed from %ums to %ums\n",
749e3453f63SMichael Buesch 				   interval, 0x80);
750e3453f63SMichael Buesch 			interval = 0x80;
751e3453f63SMichael Buesch 			data = (data & 0x00FF) | ((u16)interval << 8);
7525b2fc499SJeff Garzik #ifdef PEGASUS_WRITE_EEPROM
753e3453f63SMichael Buesch 			write_eprom_word(pegasus, 4, data);
7545b2fc499SJeff Garzik #endif
7555b2fc499SJeff Garzik 		}
7565b2fc499SJeff Garzik 	}
757e3453f63SMichael Buesch 	pegasus->intr_interval = interval;
7585b2fc499SJeff Garzik }
7595b2fc499SJeff Garzik 
7605b2fc499SJeff Garzik static void set_carrier(struct net_device *net)
7615b2fc499SJeff Garzik {
7625b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
7635b2fc499SJeff Garzik 	u16 tmp;
7645b2fc499SJeff Garzik 
7655b2fc499SJeff Garzik 	if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
7665b2fc499SJeff Garzik 		return;
7675b2fc499SJeff Garzik 
7685b2fc499SJeff Garzik 	if (tmp & BMSR_LSTATUS)
7695b2fc499SJeff Garzik 		netif_carrier_on(net);
7705b2fc499SJeff Garzik 	else
7715b2fc499SJeff Garzik 		netif_carrier_off(net);
7725b2fc499SJeff Garzik }
7735b2fc499SJeff Garzik 
7745b2fc499SJeff Garzik static void free_all_urbs(pegasus_t *pegasus)
7755b2fc499SJeff Garzik {
7765b2fc499SJeff Garzik 	usb_free_urb(pegasus->intr_urb);
7775b2fc499SJeff Garzik 	usb_free_urb(pegasus->tx_urb);
7785b2fc499SJeff Garzik 	usb_free_urb(pegasus->rx_urb);
7795b2fc499SJeff Garzik }
7805b2fc499SJeff Garzik 
7815b2fc499SJeff Garzik static void unlink_all_urbs(pegasus_t *pegasus)
7825b2fc499SJeff Garzik {
7835b2fc499SJeff Garzik 	usb_kill_urb(pegasus->intr_urb);
7845b2fc499SJeff Garzik 	usb_kill_urb(pegasus->tx_urb);
7855b2fc499SJeff Garzik 	usb_kill_urb(pegasus->rx_urb);
7865b2fc499SJeff Garzik }
7875b2fc499SJeff Garzik 
7885b2fc499SJeff Garzik static int alloc_urbs(pegasus_t *pegasus)
7895b2fc499SJeff Garzik {
790323b3496SPetko Manolov 	int res = -ENOMEM;
791323b3496SPetko Manolov 
7925b2fc499SJeff Garzik 	pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
7935b2fc499SJeff Garzik 	if (!pegasus->rx_urb) {
794323b3496SPetko Manolov 		return res;
7955b2fc499SJeff Garzik 	}
7965b2fc499SJeff Garzik 	pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
7975b2fc499SJeff Garzik 	if (!pegasus->tx_urb) {
7985b2fc499SJeff Garzik 		usb_free_urb(pegasus->rx_urb);
799323b3496SPetko Manolov 		return res;
8005b2fc499SJeff Garzik 	}
8015b2fc499SJeff Garzik 	pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
8025b2fc499SJeff Garzik 	if (!pegasus->intr_urb) {
8035b2fc499SJeff Garzik 		usb_free_urb(pegasus->tx_urb);
8045b2fc499SJeff Garzik 		usb_free_urb(pegasus->rx_urb);
805323b3496SPetko Manolov 		return res;
8065b2fc499SJeff Garzik 	}
8075b2fc499SJeff Garzik 
808323b3496SPetko Manolov 	return 0;
8095b2fc499SJeff Garzik }
8105b2fc499SJeff Garzik 
8115b2fc499SJeff Garzik static int pegasus_open(struct net_device *net)
8125b2fc499SJeff Garzik {
8135b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
814323b3496SPetko Manolov 	int res=-ENOMEM;
8155b2fc499SJeff Garzik 
8165b2fc499SJeff Garzik 	if (pegasus->rx_skb == NULL)
817313a58e4SPetko Manolov 		pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
818313a58e4SPetko Manolov 							      PEGASUS_MTU,
819313a58e4SPetko Manolov 							      GFP_KERNEL);
8205b2fc499SJeff Garzik 	if (!pegasus->rx_skb)
821323b3496SPetko Manolov 		goto exit;
8225b2fc499SJeff Garzik 
8235b2fc499SJeff Garzik 	res = set_registers(pegasus, EthID, 6, net->dev_addr);
8245b2fc499SJeff Garzik 
8255b2fc499SJeff Garzik 	usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
8265b2fc499SJeff Garzik 			  usb_rcvbulkpipe(pegasus->usb, 1),
827b7302ca0SPetko Manolov 			  pegasus->rx_skb->data, PEGASUS_MTU,
8285b2fc499SJeff Garzik 			  read_bulk_callback, pegasus);
8295b2fc499SJeff Garzik 	if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
8305b2fc499SJeff Garzik 		if (res == -ENODEV)
8315b2fc499SJeff Garzik 			netif_device_detach(pegasus->net);
832a475f603SJoe Perches 		netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
8335b2fc499SJeff Garzik 		goto exit;
8345b2fc499SJeff Garzik 	}
8355b2fc499SJeff Garzik 
8365b2fc499SJeff Garzik 	usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
8375b2fc499SJeff Garzik 			 usb_rcvintpipe(pegasus->usb, 3),
8385b2fc499SJeff Garzik 			 pegasus->intr_buff, sizeof(pegasus->intr_buff),
8395b2fc499SJeff Garzik 			 intr_callback, pegasus, pegasus->intr_interval);
8405b2fc499SJeff Garzik 	if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
8415b2fc499SJeff Garzik 		if (res == -ENODEV)
8425b2fc499SJeff Garzik 			netif_device_detach(pegasus->net);
843a475f603SJoe Perches 		netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
8445b2fc499SJeff Garzik 		usb_kill_urb(pegasus->rx_urb);
8455b2fc499SJeff Garzik 		goto exit;
8465b2fc499SJeff Garzik 	}
847323b3496SPetko Manolov 	res = enable_net_traffic(net, pegasus->usb);
848323b3496SPetko Manolov 	if (res < 0) {
849a475f603SJoe Perches 		netif_dbg(pegasus, ifup, net,
850a475f603SJoe Perches 			  "can't enable_net_traffic() - %d\n", res);
8515b2fc499SJeff Garzik 		res = -EIO;
8525b2fc499SJeff Garzik 		usb_kill_urb(pegasus->rx_urb);
8535b2fc499SJeff Garzik 		usb_kill_urb(pegasus->intr_urb);
8545b2fc499SJeff Garzik 		goto exit;
8555b2fc499SJeff Garzik 	}
8565b2fc499SJeff Garzik 	set_carrier(net);
8575b2fc499SJeff Garzik 	netif_start_queue(net);
858a475f603SJoe Perches 	netif_dbg(pegasus, ifup, net, "open\n");
8595b2fc499SJeff Garzik 	res = 0;
8605b2fc499SJeff Garzik exit:
8615b2fc499SJeff Garzik 	return res;
8625b2fc499SJeff Garzik }
8635b2fc499SJeff Garzik 
8645b2fc499SJeff Garzik static int pegasus_close(struct net_device *net)
8655b2fc499SJeff Garzik {
8665b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
8675b2fc499SJeff Garzik 
8685b2fc499SJeff Garzik 	netif_stop_queue(net);
8695b2fc499SJeff Garzik 	if (!(pegasus->flags & PEGASUS_UNPLUG))
8705b2fc499SJeff Garzik 		disable_net_traffic(pegasus);
8715b2fc499SJeff Garzik 	tasklet_kill(&pegasus->rx_tl);
8725b2fc499SJeff Garzik 	unlink_all_urbs(pegasus);
8735b2fc499SJeff Garzik 
8745b2fc499SJeff Garzik 	return 0;
8755b2fc499SJeff Garzik }
8765b2fc499SJeff Garzik 
8775b2fc499SJeff Garzik static void pegasus_get_drvinfo(struct net_device *dev,
8785b2fc499SJeff Garzik 				struct ethtool_drvinfo *info)
8795b2fc499SJeff Garzik {
8805b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
8817826d43fSJiri Pirko 
8827826d43fSJiri Pirko 	strlcpy(info->driver, driver_name, sizeof(info->driver));
8837826d43fSJiri Pirko 	strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
8845b2fc499SJeff Garzik 	usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info));
8855b2fc499SJeff Garzik }
8865b2fc499SJeff Garzik 
8875b2fc499SJeff Garzik /* also handles three patterns of some kind in hardware */
8885b2fc499SJeff Garzik #define	WOL_SUPPORTED	(WAKE_MAGIC|WAKE_PHY)
8895b2fc499SJeff Garzik 
8905b2fc499SJeff Garzik static void
8915b2fc499SJeff Garzik pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8925b2fc499SJeff Garzik {
8935b2fc499SJeff Garzik 	pegasus_t	*pegasus = netdev_priv(dev);
8945b2fc499SJeff Garzik 
8955b2fc499SJeff Garzik 	wol->supported = WAKE_MAGIC | WAKE_PHY;
8965b2fc499SJeff Garzik 	wol->wolopts = pegasus->wolopts;
8975b2fc499SJeff Garzik }
8985b2fc499SJeff Garzik 
8995b2fc499SJeff Garzik static int
9005b2fc499SJeff Garzik pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
9015b2fc499SJeff Garzik {
9025b2fc499SJeff Garzik 	pegasus_t	*pegasus = netdev_priv(dev);
9035b2fc499SJeff Garzik 	u8		reg78 = 0x04;
9044fbc5b20SMing Lei 	int		ret;
9055b2fc499SJeff Garzik 
9065b2fc499SJeff Garzik 	if (wol->wolopts & ~WOL_SUPPORTED)
9075b2fc499SJeff Garzik 		return -EINVAL;
9085b2fc499SJeff Garzik 
9095b2fc499SJeff Garzik 	if (wol->wolopts & WAKE_MAGIC)
9105b2fc499SJeff Garzik 		reg78 |= 0x80;
9115b2fc499SJeff Garzik 	if (wol->wolopts & WAKE_PHY)
9125b2fc499SJeff Garzik 		reg78 |= 0x40;
9135b2fc499SJeff Garzik 	/* FIXME this 0x10 bit still needs to get set in the chip... */
9145b2fc499SJeff Garzik 	if (wol->wolopts)
9155b2fc499SJeff Garzik 		pegasus->eth_regs[0] |= 0x10;
9165b2fc499SJeff Garzik 	else
9175b2fc499SJeff Garzik 		pegasus->eth_regs[0] &= ~0x10;
9185b2fc499SJeff Garzik 	pegasus->wolopts = wol->wolopts;
9194fbc5b20SMing Lei 
9204fbc5b20SMing Lei 	ret = set_register(pegasus, WakeupControl, reg78);
9214fbc5b20SMing Lei 	if (!ret)
9224fbc5b20SMing Lei 		ret = device_set_wakeup_enable(&pegasus->usb->dev,
9234fbc5b20SMing Lei 						wol->wolopts);
9244fbc5b20SMing Lei 	return ret;
9255b2fc499SJeff Garzik }
9265b2fc499SJeff Garzik 
9275b2fc499SJeff Garzik static inline void pegasus_reset_wol(struct net_device *dev)
9285b2fc499SJeff Garzik {
9295b2fc499SJeff Garzik 	struct ethtool_wolinfo wol;
9305b2fc499SJeff Garzik 
9315b2fc499SJeff Garzik 	memset(&wol, 0, sizeof wol);
9325b2fc499SJeff Garzik 	(void) pegasus_set_wol(dev, &wol);
9335b2fc499SJeff Garzik }
9345b2fc499SJeff Garzik 
9355b2fc499SJeff Garzik static int
93671dbc341SPhilippe Reynes pegasus_get_link_ksettings(struct net_device *dev,
93771dbc341SPhilippe Reynes 			   struct ethtool_link_ksettings *ecmd)
9385b2fc499SJeff Garzik {
9395b2fc499SJeff Garzik 	pegasus_t *pegasus;
9405b2fc499SJeff Garzik 
9415b2fc499SJeff Garzik 	pegasus = netdev_priv(dev);
94271dbc341SPhilippe Reynes 	mii_ethtool_get_link_ksettings(&pegasus->mii, ecmd);
9435b2fc499SJeff Garzik 	return 0;
9445b2fc499SJeff Garzik }
9455b2fc499SJeff Garzik 
9465b2fc499SJeff Garzik static int
94771dbc341SPhilippe Reynes pegasus_set_link_ksettings(struct net_device *dev,
94871dbc341SPhilippe Reynes 			   const struct ethtool_link_ksettings *ecmd)
9495b2fc499SJeff Garzik {
9505b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
95171dbc341SPhilippe Reynes 	return mii_ethtool_set_link_ksettings(&pegasus->mii, ecmd);
9525b2fc499SJeff Garzik }
9535b2fc499SJeff Garzik 
9545b2fc499SJeff Garzik static int pegasus_nway_reset(struct net_device *dev)
9555b2fc499SJeff Garzik {
9565b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
9575b2fc499SJeff Garzik 	return mii_nway_restart(&pegasus->mii);
9585b2fc499SJeff Garzik }
9595b2fc499SJeff Garzik 
9605b2fc499SJeff Garzik static u32 pegasus_get_link(struct net_device *dev)
9615b2fc499SJeff Garzik {
9625b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
9635b2fc499SJeff Garzik 	return mii_link_ok(&pegasus->mii);
9645b2fc499SJeff Garzik }
9655b2fc499SJeff Garzik 
9665b2fc499SJeff Garzik static u32 pegasus_get_msglevel(struct net_device *dev)
9675b2fc499SJeff Garzik {
9685b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
9695b2fc499SJeff Garzik 	return pegasus->msg_enable;
9705b2fc499SJeff Garzik }
9715b2fc499SJeff Garzik 
9725b2fc499SJeff Garzik static void pegasus_set_msglevel(struct net_device *dev, u32 v)
9735b2fc499SJeff Garzik {
9745b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(dev);
9755b2fc499SJeff Garzik 	pegasus->msg_enable = v;
9765b2fc499SJeff Garzik }
9775b2fc499SJeff Garzik 
9780fc0b732SStephen Hemminger static const struct ethtool_ops ops = {
9795b2fc499SJeff Garzik 	.get_drvinfo = pegasus_get_drvinfo,
9805b2fc499SJeff Garzik 	.nway_reset = pegasus_nway_reset,
9815b2fc499SJeff Garzik 	.get_link = pegasus_get_link,
9825b2fc499SJeff Garzik 	.get_msglevel = pegasus_get_msglevel,
9835b2fc499SJeff Garzik 	.set_msglevel = pegasus_set_msglevel,
9845b2fc499SJeff Garzik 	.get_wol = pegasus_get_wol,
9855b2fc499SJeff Garzik 	.set_wol = pegasus_set_wol,
98671dbc341SPhilippe Reynes 	.get_link_ksettings = pegasus_get_link_ksettings,
98771dbc341SPhilippe Reynes 	.set_link_ksettings = pegasus_set_link_ksettings,
9885b2fc499SJeff Garzik };
9895b2fc499SJeff Garzik 
9905b2fc499SJeff Garzik static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
9915b2fc499SJeff Garzik {
9925b2fc499SJeff Garzik 	__u16 *data = (__u16 *) &rq->ifr_ifru;
9935b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
9945b2fc499SJeff Garzik 	int res;
9955b2fc499SJeff Garzik 
9965b2fc499SJeff Garzik 	switch (cmd) {
9975b2fc499SJeff Garzik 	case SIOCDEVPRIVATE:
9985b2fc499SJeff Garzik 		data[0] = pegasus->phy;
999df561f66SGustavo A. R. Silva 		fallthrough;
10005b2fc499SJeff Garzik 	case SIOCDEVPRIVATE + 1:
10015b2fc499SJeff Garzik 		read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
10025b2fc499SJeff Garzik 		res = 0;
10035b2fc499SJeff Garzik 		break;
10045b2fc499SJeff Garzik 	case SIOCDEVPRIVATE + 2:
10055b2fc499SJeff Garzik 		if (!capable(CAP_NET_ADMIN))
10065b2fc499SJeff Garzik 			return -EPERM;
10072bd64701SPetko Manolov 		write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, &data[2]);
10085b2fc499SJeff Garzik 		res = 0;
10095b2fc499SJeff Garzik 		break;
10105b2fc499SJeff Garzik 	default:
10115b2fc499SJeff Garzik 		res = -EOPNOTSUPP;
10125b2fc499SJeff Garzik 	}
10135b2fc499SJeff Garzik 	return res;
10145b2fc499SJeff Garzik }
10155b2fc499SJeff Garzik 
10165b2fc499SJeff Garzik static void pegasus_set_multicast(struct net_device *net)
10175b2fc499SJeff Garzik {
10185b2fc499SJeff Garzik 	pegasus_t *pegasus = netdev_priv(net);
10195b2fc499SJeff Garzik 
10205b2fc499SJeff Garzik 	if (net->flags & IFF_PROMISC) {
10215b2fc499SJeff Garzik 		pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1022a475f603SJoe Perches 		netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
10234cd24eafSJiri Pirko 	} else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
10245b2fc499SJeff Garzik 		pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
10255b2fc499SJeff Garzik 		pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1026a475f603SJoe Perches 		netif_dbg(pegasus, link, net, "set allmulti\n");
10275b2fc499SJeff Garzik 	} else {
10285b2fc499SJeff Garzik 		pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
10295b2fc499SJeff Garzik 		pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
10305b2fc499SJeff Garzik 	}
1031323b3496SPetko Manolov 	update_eth_regs_async(pegasus);
10325b2fc499SJeff Garzik }
10335b2fc499SJeff Garzik 
10345b2fc499SJeff Garzik static __u8 mii_phy_probe(pegasus_t *pegasus)
10355b2fc499SJeff Garzik {
10365b2fc499SJeff Garzik 	int i;
10375b2fc499SJeff Garzik 	__u16 tmp;
10385b2fc499SJeff Garzik 
10395b2fc499SJeff Garzik 	for (i = 0; i < 32; i++) {
10405b2fc499SJeff Garzik 		read_mii_word(pegasus, i, MII_BMSR, &tmp);
10415b2fc499SJeff Garzik 		if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
10425b2fc499SJeff Garzik 			continue;
10435b2fc499SJeff Garzik 		else
10445b2fc499SJeff Garzik 			return i;
10455b2fc499SJeff Garzik 	}
10465b2fc499SJeff Garzik 
10475b2fc499SJeff Garzik 	return 0xff;
10485b2fc499SJeff Garzik }
10495b2fc499SJeff Garzik 
10505b2fc499SJeff Garzik static inline void setup_pegasus_II(pegasus_t *pegasus)
10515b2fc499SJeff Garzik {
10525b2fc499SJeff Garzik 	__u8 data = 0xa5;
10535b2fc499SJeff Garzik 
10545b2fc499SJeff Garzik 	set_register(pegasus, Reg1d, 0);
10555b2fc499SJeff Garzik 	set_register(pegasus, Reg7b, 1);
10566dff5addSJia-Ju Bai 	msleep(100);
10575b2fc499SJeff Garzik 	if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
10585b2fc499SJeff Garzik 		set_register(pegasus, Reg7b, 0);
10595b2fc499SJeff Garzik 	else
10605b2fc499SJeff Garzik 		set_register(pegasus, Reg7b, 2);
10615b2fc499SJeff Garzik 
10625b2fc499SJeff Garzik 	set_register(pegasus, 0x83, data);
10635b2fc499SJeff Garzik 	get_registers(pegasus, 0x83, 1, &data);
10645b2fc499SJeff Garzik 
10655a9dbfe0SNicolas Kaiser 	if (data == 0xa5)
10665b2fc499SJeff Garzik 		pegasus->chip = 0x8513;
10675a9dbfe0SNicolas Kaiser 	else
10685b2fc499SJeff Garzik 		pegasus->chip = 0;
10695b2fc499SJeff Garzik 
10705b2fc499SJeff Garzik 	set_register(pegasus, 0x80, 0xc0);
10715b2fc499SJeff Garzik 	set_register(pegasus, 0x83, 0xff);
10725b2fc499SJeff Garzik 	set_register(pegasus, 0x84, 0x01);
10735b2fc499SJeff Garzik 
10745b2fc499SJeff Garzik 	if (pegasus->features & HAS_HOME_PNA && mii_mode)
10755b2fc499SJeff Garzik 		set_register(pegasus, Reg81, 6);
10765b2fc499SJeff Garzik 	else
10775b2fc499SJeff Garzik 		set_register(pegasus, Reg81, 2);
10785b2fc499SJeff Garzik }
10795b2fc499SJeff Garzik 
10805b2fc499SJeff Garzik static void check_carrier(struct work_struct *work)
10815b2fc499SJeff Garzik {
10825b2fc499SJeff Garzik 	pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
10835b2fc499SJeff Garzik 	set_carrier(pegasus->net);
10845b2fc499SJeff Garzik 	if (!(pegasus->flags & PEGASUS_UNPLUG)) {
10852f5107c3SPetko Manolov 		queue_delayed_work(system_long_wq, &pegasus->carrier_check,
10865b2fc499SJeff Garzik 			CARRIER_CHECK_DELAY);
10875b2fc499SJeff Garzik 	}
10885b2fc499SJeff Garzik }
10895b2fc499SJeff Garzik 
10904f63135eSBen Collins static int pegasus_blacklisted(struct usb_device *udev)
10914f63135eSBen Collins {
10924f63135eSBen Collins 	struct usb_device_descriptor *udd = &udev->descriptor;
10934f63135eSBen Collins 
10944f63135eSBen Collins 	/* Special quirk to keep the driver from handling the Belkin Bluetooth
10954f63135eSBen Collins 	 * dongle which happens to have the same ID.
10964f63135eSBen Collins 	 */
1097e3453f63SMichael Buesch 	if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
1098e3453f63SMichael Buesch 	    (udd->idProduct == cpu_to_le16(0x0121)) &&
10994f63135eSBen Collins 	    (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
11004f63135eSBen Collins 	    (udd->bDeviceProtocol == 1))
11014f63135eSBen Collins 		return 1;
11024f63135eSBen Collins 
11034f63135eSBen Collins 	return 0;
11044f63135eSBen Collins }
11054f63135eSBen Collins 
11065b2fc499SJeff Garzik static int pegasus_probe(struct usb_interface *intf,
11075b2fc499SJeff Garzik 			 const struct usb_device_id *id)
11085b2fc499SJeff Garzik {
11095b2fc499SJeff Garzik 	struct usb_device *dev = interface_to_usbdev(intf);
11105b2fc499SJeff Garzik 	struct net_device *net;
11115b2fc499SJeff Garzik 	pegasus_t *pegasus;
11125b2fc499SJeff Garzik 	int dev_index = id - pegasus_ids;
11135b2fc499SJeff Garzik 	int res = -ENOMEM;
11145b2fc499SJeff Garzik 
1115cda2836dSDavid Brownell 	if (pegasus_blacklisted(dev))
1116cda2836dSDavid Brownell 		return -ENODEV;
11174f63135eSBen Collins 
11185b2fc499SJeff Garzik 	net = alloc_etherdev(sizeof(struct pegasus));
111941de8d4cSJoe Perches 	if (!net)
11205b2fc499SJeff Garzik 		goto out;
11215b2fc499SJeff Garzik 
11225b2fc499SJeff Garzik 	pegasus = netdev_priv(net);
11235b2fc499SJeff Garzik 	pegasus->dev_index = dev_index;
11245b2fc499SJeff Garzik 
1125323b3496SPetko Manolov 	res = alloc_urbs(pegasus);
1126323b3496SPetko Manolov 	if (res < 0) {
11275b2fc499SJeff Garzik 		dev_err(&intf->dev, "can't allocate %s\n", "urbs");
11285b2fc499SJeff Garzik 		goto out1;
11295b2fc499SJeff Garzik 	}
11305b2fc499SJeff Garzik 
1131*23a64c51SEmil Renner Berthing 	tasklet_setup(&pegasus->rx_tl, rx_fixup);
11325b2fc499SJeff Garzik 
11335b2fc499SJeff Garzik 	INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
11345b2fc499SJeff Garzik 
11355b2fc499SJeff Garzik 	pegasus->intf = intf;
11365b2fc499SJeff Garzik 	pegasus->usb = dev;
11375b2fc499SJeff Garzik 	pegasus->net = net;
11388cb89571SOliver Neukum 
11398cb89571SOliver Neukum 
11405b2fc499SJeff Garzik 	net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
11418cb89571SOliver Neukum 	net->netdev_ops = &pegasus_netdev_ops;
11427ad24ea4SWilfried Klaebe 	net->ethtool_ops = &ops;
11435b2fc499SJeff Garzik 	pegasus->mii.dev = net;
11445b2fc499SJeff Garzik 	pegasus->mii.mdio_read = mdio_read;
11455b2fc499SJeff Garzik 	pegasus->mii.mdio_write = mdio_write;
11465b2fc499SJeff Garzik 	pegasus->mii.phy_id_mask = 0x1f;
11475b2fc499SJeff Garzik 	pegasus->mii.reg_num_mask = 0x1f;
11485b2fc499SJeff Garzik 	pegasus->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
11495b2fc499SJeff Garzik 				| NETIF_MSG_PROBE | NETIF_MSG_LINK);
11505b2fc499SJeff Garzik 
11515b2fc499SJeff Garzik 	pegasus->features = usb_dev_id[dev_index].private;
11525b2fc499SJeff Garzik 	get_interrupt_interval(pegasus);
11535b2fc499SJeff Garzik 	if (reset_mac(pegasus)) {
11545b2fc499SJeff Garzik 		dev_err(&intf->dev, "can't reset MAC\n");
11555b2fc499SJeff Garzik 		res = -EIO;
11565b2fc499SJeff Garzik 		goto out2;
11575b2fc499SJeff Garzik 	}
11585b2fc499SJeff Garzik 	set_ethernet_addr(pegasus);
11595b2fc499SJeff Garzik 	if (pegasus->features & PEGASUS_II) {
11605b2fc499SJeff Garzik 		dev_info(&intf->dev, "setup Pegasus II specific registers\n");
11615b2fc499SJeff Garzik 		setup_pegasus_II(pegasus);
11625b2fc499SJeff Garzik 	}
11635b2fc499SJeff Garzik 	pegasus->phy = mii_phy_probe(pegasus);
11645b2fc499SJeff Garzik 	if (pegasus->phy == 0xff) {
11655b2fc499SJeff Garzik 		dev_warn(&intf->dev, "can't locate MII phy, using default\n");
11665b2fc499SJeff Garzik 		pegasus->phy = 1;
11675b2fc499SJeff Garzik 	}
11685b2fc499SJeff Garzik 	pegasus->mii.phy_id = pegasus->phy;
11695b2fc499SJeff Garzik 	usb_set_intfdata(intf, pegasus);
11705b2fc499SJeff Garzik 	SET_NETDEV_DEV(net, &intf->dev);
11715b2fc499SJeff Garzik 	pegasus_reset_wol(net);
11725b2fc499SJeff Garzik 	res = register_netdev(net);
11735b2fc499SJeff Garzik 	if (res)
11745b2fc499SJeff Garzik 		goto out3;
11752f5107c3SPetko Manolov 	queue_delayed_work(system_long_wq, &pegasus->carrier_check,
11765b2fc499SJeff Garzik 			   CARRIER_CHECK_DELAY);
1177323b3496SPetko Manolov 	dev_info(&intf->dev, "%s, %s, %pM\n", net->name,
1178323b3496SPetko Manolov 		 usb_dev_id[dev_index].name, net->dev_addr);
11795b2fc499SJeff Garzik 	return 0;
11805b2fc499SJeff Garzik 
11815b2fc499SJeff Garzik out3:
11825b2fc499SJeff Garzik 	usb_set_intfdata(intf, NULL);
11835b2fc499SJeff Garzik out2:
11845b2fc499SJeff Garzik 	free_all_urbs(pegasus);
11855b2fc499SJeff Garzik out1:
11865b2fc499SJeff Garzik 	free_netdev(net);
11875b2fc499SJeff Garzik out:
11885b2fc499SJeff Garzik 	return res;
11895b2fc499SJeff Garzik }
11905b2fc499SJeff Garzik 
11915b2fc499SJeff Garzik static void pegasus_disconnect(struct usb_interface *intf)
11925b2fc499SJeff Garzik {
11935b2fc499SJeff Garzik 	struct pegasus *pegasus = usb_get_intfdata(intf);
11945b2fc499SJeff Garzik 
11955b2fc499SJeff Garzik 	usb_set_intfdata(intf, NULL);
11965b2fc499SJeff Garzik 	if (!pegasus) {
11975b2fc499SJeff Garzik 		dev_dbg(&intf->dev, "unregistering non-bound device?\n");
11985b2fc499SJeff Garzik 		return;
11995b2fc499SJeff Garzik 	}
12005b2fc499SJeff Garzik 
12015b2fc499SJeff Garzik 	pegasus->flags |= PEGASUS_UNPLUG;
12022f5107c3SPetko Manolov 	cancel_delayed_work_sync(&pegasus->carrier_check);
12035b2fc499SJeff Garzik 	unregister_netdev(pegasus->net);
12045b2fc499SJeff Garzik 	unlink_all_urbs(pegasus);
12055b2fc499SJeff Garzik 	free_all_urbs(pegasus);
12065b2fc499SJeff Garzik 	if (pegasus->rx_skb != NULL) {
12075b2fc499SJeff Garzik 		dev_kfree_skb(pegasus->rx_skb);
12085b2fc499SJeff Garzik 		pegasus->rx_skb = NULL;
12095b2fc499SJeff Garzik 	}
12105b2fc499SJeff Garzik 	free_netdev(pegasus->net);
12115b2fc499SJeff Garzik }
12125b2fc499SJeff Garzik 
12135b2fc499SJeff Garzik static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
12145b2fc499SJeff Garzik {
12155b2fc499SJeff Garzik 	struct pegasus *pegasus = usb_get_intfdata(intf);
12165b2fc499SJeff Garzik 
12175b2fc499SJeff Garzik 	netif_device_detach(pegasus->net);
12182f5107c3SPetko Manolov 	cancel_delayed_work_sync(&pegasus->carrier_check);
12195b2fc499SJeff Garzik 	if (netif_running(pegasus->net)) {
12205b2fc499SJeff Garzik 		usb_kill_urb(pegasus->rx_urb);
12215b2fc499SJeff Garzik 		usb_kill_urb(pegasus->intr_urb);
12225b2fc499SJeff Garzik 	}
12235b2fc499SJeff Garzik 	return 0;
12245b2fc499SJeff Garzik }
12255b2fc499SJeff Garzik 
12265b2fc499SJeff Garzik static int pegasus_resume(struct usb_interface *intf)
12275b2fc499SJeff Garzik {
12285b2fc499SJeff Garzik 	struct pegasus *pegasus = usb_get_intfdata(intf);
12295b2fc499SJeff Garzik 
12305b2fc499SJeff Garzik 	netif_device_attach(pegasus->net);
12315b2fc499SJeff Garzik 	if (netif_running(pegasus->net)) {
12325b2fc499SJeff Garzik 		pegasus->rx_urb->status = 0;
12335b2fc499SJeff Garzik 		pegasus->rx_urb->actual_length = 0;
12345b2fc499SJeff Garzik 		read_bulk_callback(pegasus->rx_urb);
12355b2fc499SJeff Garzik 
12365b2fc499SJeff Garzik 		pegasus->intr_urb->status = 0;
12375b2fc499SJeff Garzik 		pegasus->intr_urb->actual_length = 0;
12385b2fc499SJeff Garzik 		intr_callback(pegasus->intr_urb);
12395b2fc499SJeff Garzik 	}
12402f5107c3SPetko Manolov 	queue_delayed_work(system_long_wq, &pegasus->carrier_check,
12415b2fc499SJeff Garzik 				CARRIER_CHECK_DELAY);
12425b2fc499SJeff Garzik 	return 0;
12435b2fc499SJeff Garzik }
12445b2fc499SJeff Garzik 
12458cb89571SOliver Neukum static const struct net_device_ops pegasus_netdev_ops = {
12468cb89571SOliver Neukum 	.ndo_open =			pegasus_open,
12478cb89571SOliver Neukum 	.ndo_stop =			pegasus_close,
12488cb89571SOliver Neukum 	.ndo_do_ioctl =			pegasus_ioctl,
12498cb89571SOliver Neukum 	.ndo_start_xmit =		pegasus_start_xmit,
1250afc4b13dSJiri Pirko 	.ndo_set_rx_mode =		pegasus_set_multicast,
12518cb89571SOliver Neukum 	.ndo_tx_timeout =		pegasus_tx_timeout,
1252240c102dSBen Hutchings 	.ndo_set_mac_address =		eth_mac_addr,
1253240c102dSBen Hutchings 	.ndo_validate_addr =		eth_validate_addr,
12548cb89571SOliver Neukum };
12558cb89571SOliver Neukum 
12565b2fc499SJeff Garzik static struct usb_driver pegasus_driver = {
12575b2fc499SJeff Garzik 	.name = driver_name,
12585b2fc499SJeff Garzik 	.probe = pegasus_probe,
12595b2fc499SJeff Garzik 	.disconnect = pegasus_disconnect,
12605b2fc499SJeff Garzik 	.id_table = pegasus_ids,
12615b2fc499SJeff Garzik 	.suspend = pegasus_suspend,
12625b2fc499SJeff Garzik 	.resume = pegasus_resume,
1263e1f12eb6SSarah Sharp 	.disable_hub_initiated_lpm = 1,
12645b2fc499SJeff Garzik };
12655b2fc499SJeff Garzik 
1266cda2836dSDavid Brownell static void __init parse_id(char *id)
12675b2fc499SJeff Garzik {
12685b2fc499SJeff Garzik 	unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0;
12695b2fc499SJeff Garzik 	char *token, *name = NULL;
12705b2fc499SJeff Garzik 
12715b2fc499SJeff Garzik 	if ((token = strsep(&id, ":")) != NULL)
12725b2fc499SJeff Garzik 		name = token;
12735b2fc499SJeff Garzik 	/* name now points to a null terminated string*/
12745b2fc499SJeff Garzik 	if ((token = strsep(&id, ":")) != NULL)
12755b2fc499SJeff Garzik 		vendor_id = simple_strtoul(token, NULL, 16);
12765b2fc499SJeff Garzik 	if ((token = strsep(&id, ":")) != NULL)
12775b2fc499SJeff Garzik 		device_id = simple_strtoul(token, NULL, 16);
12785b2fc499SJeff Garzik 	flags = simple_strtoul(id, NULL, 16);
12795b2fc499SJeff Garzik 	pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
12805b2fc499SJeff Garzik 		driver_name, name, vendor_id, device_id, flags);
12815b2fc499SJeff Garzik 
12825b2fc499SJeff Garzik 	if (vendor_id > 0x10000 || vendor_id == 0)
12835b2fc499SJeff Garzik 		return;
12845b2fc499SJeff Garzik 	if (device_id > 0x10000 || device_id == 0)
12855b2fc499SJeff Garzik 		return;
12865b2fc499SJeff Garzik 
12875b2fc499SJeff Garzik 	for (i = 0; usb_dev_id[i].name; i++);
12885b2fc499SJeff Garzik 	usb_dev_id[i].name = name;
12895b2fc499SJeff Garzik 	usb_dev_id[i].vendor = vendor_id;
12905b2fc499SJeff Garzik 	usb_dev_id[i].device = device_id;
12915b2fc499SJeff Garzik 	usb_dev_id[i].private = flags;
12925b2fc499SJeff Garzik 	pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
12935b2fc499SJeff Garzik 	pegasus_ids[i].idVendor = vendor_id;
12945b2fc499SJeff Garzik 	pegasus_ids[i].idProduct = device_id;
12955b2fc499SJeff Garzik }
12965b2fc499SJeff Garzik 
12975b2fc499SJeff Garzik static int __init pegasus_init(void)
12985b2fc499SJeff Garzik {
12995b2fc499SJeff Garzik 	pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
13005b2fc499SJeff Garzik 	if (devid)
13015b2fc499SJeff Garzik 		parse_id(devid);
13025b2fc499SJeff Garzik 	return usb_register(&pegasus_driver);
13035b2fc499SJeff Garzik }
13045b2fc499SJeff Garzik 
13055b2fc499SJeff Garzik static void __exit pegasus_exit(void)
13065b2fc499SJeff Garzik {
13075b2fc499SJeff Garzik 	usb_deregister(&pegasus_driver);
13085b2fc499SJeff Garzik }
13095b2fc499SJeff Garzik 
13105b2fc499SJeff Garzik module_init(pegasus_init);
13115b2fc499SJeff Garzik module_exit(pegasus_exit);
1312