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