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