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