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