1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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
52static 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
59static bool loopback;
60static bool mii_mode;
61static char *devid;
62
63static 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
75static 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
80
81
82
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
94MODULE_AUTHOR(DRIVER_AUTHOR);
95MODULE_DESCRIPTION(DRIVER_DESC);
96MODULE_LICENSE("GPL");
97module_param(loopback, bool, 0);
98module_param(mii_mode, bool, 0);
99module_param(devid, charp, 0);
100MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
101MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
102MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
103
104
105static int msg_level = -1;
106module_param(msg_level, int, 0);
107MODULE_PARM_DESC(msg_level, "Override default message level");
108
109MODULE_DEVICE_TABLE(usb, pegasus_ids);
110static const struct net_device_ops pegasus_netdev_ops;
111
112
113
114static 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
125static int get_registers(pegasus_t *pegasus, __u16 indx, __u16 size, void *data)
126{
127 u8 *buf;
128 int ret;
129
130 buf = kmalloc(size, GFP_NOIO);
131 if (!buf)
132 return -ENOMEM;
133
134 ret = usb_control_msg(pegasus->usb, usb_rcvctrlpipe(pegasus->usb, 0),
135 PEGASUS_REQ_GET_REGS, PEGASUS_REQT_READ, 0,
136 indx, buf, size, 1000);
137 if (ret < 0)
138 netif_dbg(pegasus, drv, pegasus->net,
139 "%s returned %d\n", __func__, ret);
140 else if (ret <= size)
141 memcpy(data, buf, ret);
142 kfree(buf);
143 return ret;
144}
145
146static int set_registers(pegasus_t *pegasus, __u16 indx, __u16 size,
147 const void *data)
148{
149 u8 *buf;
150 int ret;
151
152 buf = kmemdup(data, size, GFP_NOIO);
153 if (!buf)
154 return -ENOMEM;
155
156 ret = usb_control_msg(pegasus->usb, usb_sndctrlpipe(pegasus->usb, 0),
157 PEGASUS_REQ_SET_REGS, PEGASUS_REQT_WRITE, 0,
158 indx, buf, size, 100);
159 if (ret < 0)
160 netif_dbg(pegasus, drv, pegasus->net,
161 "%s returned %d\n", __func__, ret);
162 kfree(buf);
163 return ret;
164}
165
166static int set_register(pegasus_t *pegasus, __u16 indx, __u8 data)
167{
168 u8 *buf;
169 int ret;
170
171 buf = kmemdup(&data, 1, GFP_NOIO);
172 if (!buf)
173 return -ENOMEM;
174
175 ret = usb_control_msg(pegasus->usb, usb_sndctrlpipe(pegasus->usb, 0),
176 PEGASUS_REQ_SET_REG, PEGASUS_REQT_WRITE, data,
177 indx, buf, 1, 1000);
178 if (ret < 0)
179 netif_dbg(pegasus, drv, pegasus->net,
180 "%s returned %d\n", __func__, ret);
181 kfree(buf);
182 return ret;
183}
184
185static int update_eth_regs_async(pegasus_t *pegasus)
186{
187 int ret = -ENOMEM;
188 struct urb *async_urb;
189 struct usb_ctrlrequest *req;
190
191 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
192 if (req == NULL)
193 return ret;
194
195 async_urb = usb_alloc_urb(0, GFP_ATOMIC);
196 if (async_urb == NULL) {
197 kfree(req);
198 return ret;
199 }
200 req->bRequestType = PEGASUS_REQT_WRITE;
201 req->bRequest = PEGASUS_REQ_SET_REGS;
202 req->wValue = cpu_to_le16(0);
203 req->wIndex = cpu_to_le16(EthCtrl0);
204 req->wLength = cpu_to_le16(3);
205
206 usb_fill_control_urb(async_urb, pegasus->usb,
207 usb_sndctrlpipe(pegasus->usb, 0), (void *)req,
208 pegasus->eth_regs, 3, async_ctrl_callback, req);
209
210 ret = usb_submit_urb(async_urb, GFP_ATOMIC);
211 if (ret) {
212 if (ret == -ENODEV)
213 netif_device_detach(pegasus->net);
214 netif_err(pegasus, drv, pegasus->net,
215 "%s returned %d\n", __func__, ret);
216 }
217 return ret;
218}
219
220static int __mii_op(pegasus_t *p, __u8 phy, __u8 indx, __u16 *regd, __u8 cmd)
221{
222 int i;
223 __u8 data[4] = { phy, 0, 0, indx };
224 __le16 regdi;
225 int ret = -ETIMEDOUT;
226
227 if (cmd & PHY_WRITE) {
228 __le16 *t = (__le16 *) & data[1];
229 *t = cpu_to_le16(*regd);
230 }
231 set_register(p, PhyCtrl, 0);
232 set_registers(p, PhyAddr, sizeof(data), data);
233 set_register(p, PhyCtrl, (indx | cmd));
234 for (i = 0; i < REG_TIMEOUT; i++) {
235 ret = get_registers(p, PhyCtrl, 1, data);
236 if (ret < 0)
237 goto fail;
238 if (data[0] & PHY_DONE)
239 break;
240 }
241 if (i >= REG_TIMEOUT)
242 goto fail;
243 if (cmd & PHY_READ) {
244 ret = get_registers(p, PhyData, 2, ®di);
245 *regd = le16_to_cpu(regdi);
246 return ret;
247 }
248 return 0;
249fail:
250 netif_dbg(p, drv, p->net, "%s failed\n", __func__);
251 return ret;
252}
253
254
255static int read_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
256{
257 return __mii_op(pegasus, phy, indx, regd, PHY_READ);
258}
259
260
261static int write_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
262{
263 return __mii_op(pegasus, phy, indx, regd, PHY_WRITE);
264}
265
266static int mdio_read(struct net_device *dev, int phy_id, int loc)
267{
268 pegasus_t *pegasus = netdev_priv(dev);
269 u16 res;
270
271 read_mii_word(pegasus, phy_id, loc, &res);
272 return (int)res;
273}
274
275static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
276{
277 pegasus_t *pegasus = netdev_priv(dev);
278 u16 data = val;
279
280 write_mii_word(pegasus, phy_id, loc, &data);
281}
282
283static int read_eprom_word(pegasus_t *pegasus, __u8 index, __u16 *retdata)
284{
285 int i;
286 __u8 tmp = 0;
287 __le16 retdatai;
288 int ret;
289
290 set_register(pegasus, EpromCtrl, 0);
291 set_register(pegasus, EpromOffset, index);
292 set_register(pegasus, EpromCtrl, EPROM_READ);
293
294 for (i = 0; i < REG_TIMEOUT; i++) {
295 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
296 if (tmp & EPROM_DONE)
297 break;
298 if (ret == -ESHUTDOWN)
299 goto fail;
300 }
301 if (i >= REG_TIMEOUT)
302 goto fail;
303
304 ret = get_registers(pegasus, EpromData, 2, &retdatai);
305 *retdata = le16_to_cpu(retdatai);
306 return ret;
307
308fail:
309 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
310 return -ETIMEDOUT;
311}
312
313#ifdef PEGASUS_WRITE_EEPROM
314static inline void enable_eprom_write(pegasus_t *pegasus)
315{
316 __u8 tmp;
317
318 get_registers(pegasus, EthCtrl2, 1, &tmp);
319 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
320}
321
322static inline void disable_eprom_write(pegasus_t *pegasus)
323{
324 __u8 tmp;
325
326 get_registers(pegasus, EthCtrl2, 1, &tmp);
327 set_register(pegasus, EpromCtrl, 0);
328 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
329}
330
331static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data)
332{
333 int i;
334 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
335 int ret;
336 __le16 le_data = cpu_to_le16(data);
337
338 set_registers(pegasus, EpromOffset, 4, d);
339 enable_eprom_write(pegasus);
340 set_register(pegasus, EpromOffset, index);
341 set_registers(pegasus, EpromData, 2, &le_data);
342 set_register(pegasus, EpromCtrl, EPROM_WRITE);
343
344 for (i = 0; i < REG_TIMEOUT; i++) {
345 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
346 if (ret == -ESHUTDOWN)
347 goto fail;
348 if (tmp & EPROM_DONE)
349 break;
350 }
351 disable_eprom_write(pegasus);
352 if (i >= REG_TIMEOUT)
353 goto fail;
354
355 return ret;
356
357fail:
358 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
359 return -ETIMEDOUT;
360}
361#endif
362
363static inline void get_node_id(pegasus_t *pegasus, __u8 *id)
364{
365 int i;
366 __u16 w16;
367
368 for (i = 0; i < 3; i++) {
369 read_eprom_word(pegasus, i, &w16);
370 ((__le16 *) id)[i] = cpu_to_le16(w16);
371 }
372}
373
374static void set_ethernet_addr(pegasus_t *pegasus)
375{
376 __u8 node_id[6];
377
378 if (pegasus->features & PEGASUS_II) {
379 get_registers(pegasus, 0x10, sizeof(node_id), node_id);
380 } else {
381 get_node_id(pegasus, node_id);
382 set_registers(pegasus, EthID, sizeof(node_id), node_id);
383 }
384 memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
385}
386
387static inline int reset_mac(pegasus_t *pegasus)
388{
389 __u8 data = 0x8;
390 int i;
391
392 set_register(pegasus, EthCtrl1, data);
393 for (i = 0; i < REG_TIMEOUT; i++) {
394 get_registers(pegasus, EthCtrl1, 1, &data);
395 if (~data & 0x08) {
396 if (loopback)
397 break;
398 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
399 set_register(pegasus, Gpio1, 0x34);
400 else
401 set_register(pegasus, Gpio1, 0x26);
402 set_register(pegasus, Gpio0, pegasus->features);
403 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
404 break;
405 }
406 }
407 if (i == REG_TIMEOUT)
408 return -ETIMEDOUT;
409
410 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
411 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
412 set_register(pegasus, Gpio0, 0x24);
413 set_register(pegasus, Gpio0, 0x26);
414 }
415 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
416 __u16 auxmode;
417 read_mii_word(pegasus, 3, 0x1b, &auxmode);
418 auxmode |= 4;
419 write_mii_word(pegasus, 3, 0x1b, &auxmode);
420 }
421
422 return 0;
423}
424
425static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
426{
427 __u16 linkpart;
428 __u8 data[4];
429 pegasus_t *pegasus = netdev_priv(dev);
430 int ret;
431
432 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
433 data[0] = 0xc8;
434 data[1] = 0;
435 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
436 data[1] |= 0x20;
437 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
438 data[1] |= 0x10;
439 if (mii_mode)
440 data[1] = 0;
441 data[2] = loopback ? 0x09 : 0x01;
442
443 memcpy(pegasus->eth_regs, data, sizeof(data));
444 ret = set_registers(pegasus, EthCtrl0, 3, data);
445
446 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
447 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
448 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
449 u16 auxmode;
450 read_mii_word(pegasus, 0, 0x1b, &auxmode);
451 auxmode |= 4;
452 write_mii_word(pegasus, 0, 0x1b, &auxmode);
453 }
454
455 return ret;
456}
457
458static void read_bulk_callback(struct urb *urb)
459{
460 pegasus_t *pegasus = urb->context;
461 struct net_device *net;
462 int rx_status, count = urb->actual_length;
463 int status = urb->status;
464 u8 *buf = urb->transfer_buffer;
465 __u16 pkt_len;
466
467 if (!pegasus)
468 return;
469
470 net = pegasus->net;
471 if (!netif_device_present(net) || !netif_running(net))
472 return;
473
474 switch (status) {
475 case 0:
476 break;
477 case -ETIME:
478 netif_dbg(pegasus, rx_err, net, "reset MAC\n");
479 pegasus->flags &= ~PEGASUS_RX_BUSY;
480 break;
481 case -EPIPE:
482
483 netif_warn(pegasus, rx_err, net, "no rx stall recovery\n");
484 return;
485 case -ENOENT:
486 case -ECONNRESET:
487 case -ESHUTDOWN:
488 netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status);
489 return;
490 default:
491 netif_dbg(pegasus, rx_err, net, "RX status %d\n", status);
492 goto goon;
493 }
494
495 if (count < 4)
496 goto goon;
497
498 rx_status = buf[count - 2];
499 if (rx_status & 0x1e) {
500 netif_dbg(pegasus, rx_err, net,
501 "RX packet error %x\n", rx_status);
502 net->stats.rx_errors++;
503 if (rx_status & 0x06)
504 net->stats.rx_length_errors++;
505 if (rx_status & 0x08)
506 net->stats.rx_crc_errors++;
507 if (rx_status & 0x10)
508 net->stats.rx_frame_errors++;
509 goto goon;
510 }
511 if (pegasus->chip == 0x8513) {
512 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
513 pkt_len &= 0x0fff;
514 pegasus->rx_skb->data += 2;
515 } else {
516 pkt_len = buf[count - 3] << 8;
517 pkt_len += buf[count - 4];
518 pkt_len &= 0xfff;
519 pkt_len -= 4;
520 }
521
522
523
524
525
526 if (pkt_len > PEGASUS_MTU)
527 goto goon;
528
529
530
531
532
533 skb_put(pegasus->rx_skb, pkt_len);
534 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
535 netif_rx(pegasus->rx_skb);
536 net->stats.rx_packets++;
537 net->stats.rx_bytes += pkt_len;
538
539 if (pegasus->flags & PEGASUS_UNPLUG)
540 return;
541
542 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, PEGASUS_MTU,
543 GFP_ATOMIC);
544
545 if (pegasus->rx_skb == NULL)
546 goto tl_sched;
547goon:
548 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
549 usb_rcvbulkpipe(pegasus->usb, 1),
550 pegasus->rx_skb->data, PEGASUS_MTU,
551 read_bulk_callback, pegasus);
552 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
553 if (rx_status == -ENODEV)
554 netif_device_detach(pegasus->net);
555 else if (rx_status) {
556 pegasus->flags |= PEGASUS_RX_URB_FAIL;
557 goto tl_sched;
558 } else {
559 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
560 }
561
562 return;
563
564tl_sched:
565 tasklet_schedule(&pegasus->rx_tl);
566}
567
568static void rx_fixup(unsigned long data)
569{
570 pegasus_t *pegasus;
571 int status;
572
573 pegasus = (pegasus_t *) data;
574 if (pegasus->flags & PEGASUS_UNPLUG)
575 return;
576
577 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
578 if (pegasus->rx_skb)
579 goto try_again;
580 if (pegasus->rx_skb == NULL)
581 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
582 PEGASUS_MTU,
583 GFP_ATOMIC);
584 if (pegasus->rx_skb == NULL) {
585 netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
586 tasklet_schedule(&pegasus->rx_tl);
587 return;
588 }
589 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
590 usb_rcvbulkpipe(pegasus->usb, 1),
591 pegasus->rx_skb->data, PEGASUS_MTU,
592 read_bulk_callback, pegasus);
593try_again:
594 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
595 if (status == -ENODEV)
596 netif_device_detach(pegasus->net);
597 else if (status) {
598 pegasus->flags |= PEGASUS_RX_URB_FAIL;
599 tasklet_schedule(&pegasus->rx_tl);
600 } else {
601 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
602 }
603}
604
605static void write_bulk_callback(struct urb *urb)
606{
607 pegasus_t *pegasus = urb->context;
608 struct net_device *net;
609 int status = urb->status;
610
611 if (!pegasus)
612 return;
613
614 net = pegasus->net;
615
616 if (!netif_device_present(net) || !netif_running(net))
617 return;
618
619 switch (status) {
620 case -EPIPE:
621
622 netif_stop_queue(net);
623 netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
624 return;
625 case -ENOENT:
626 case -ECONNRESET:
627 case -ESHUTDOWN:
628 netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
629 return;
630 default:
631 netif_info(pegasus, tx_err, net, "TX status %d\n", status);
632
633 case 0:
634 break;
635 }
636
637 netif_trans_update(net);
638 netif_wake_queue(net);
639}
640
641static void intr_callback(struct urb *urb)
642{
643 pegasus_t *pegasus = urb->context;
644 struct net_device *net;
645 int res, status = urb->status;
646
647 if (!pegasus)
648 return;
649 net = pegasus->net;
650
651 switch (status) {
652 case 0:
653 break;
654 case -ECONNRESET:
655 case -ENOENT:
656 case -ESHUTDOWN:
657 return;
658 default:
659
660
661
662 netif_dbg(pegasus, timer, net, "intr status %d\n", status);
663 }
664
665 if (urb->actual_length >= 6) {
666 u8 *d = urb->transfer_buffer;
667
668
669 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
670 |LATE_COL|JABBER_TIMEOUT)) {
671 net->stats.tx_errors++;
672 if (d[0] & TX_UNDERRUN)
673 net->stats.tx_fifo_errors++;
674 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
675 net->stats.tx_aborted_errors++;
676 if (d[0] & LATE_COL)
677 net->stats.tx_window_errors++;
678 }
679
680
681
682
683
684
685
686 net->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
687 }
688
689 res = usb_submit_urb(urb, GFP_ATOMIC);
690 if (res == -ENODEV)
691 netif_device_detach(pegasus->net);
692 if (res)
693 netif_err(pegasus, timer, net,
694 "can't resubmit interrupt urb, %d\n", res);
695}
696
697static void pegasus_tx_timeout(struct net_device *net, unsigned int txqueue)
698{
699 pegasus_t *pegasus = netdev_priv(net);
700 netif_warn(pegasus, timer, net, "tx timeout\n");
701 usb_unlink_urb(pegasus->tx_urb);
702 net->stats.tx_errors++;
703}
704
705static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
706 struct net_device *net)
707{
708 pegasus_t *pegasus = netdev_priv(net);
709 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
710 int res;
711 __u16 l16 = skb->len;
712
713 netif_stop_queue(net);
714
715 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
716 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
717 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
718 usb_sndbulkpipe(pegasus->usb, 2),
719 pegasus->tx_buff, count,
720 write_bulk_callback, pegasus);
721 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
722 netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
723 switch (res) {
724 case -EPIPE:
725
726 break;
727 case -ENODEV:
728 case -EPERM:
729 netif_device_detach(pegasus->net);
730 break;
731 default:
732 net->stats.tx_errors++;
733 netif_start_queue(net);
734 }
735 } else {
736 net->stats.tx_packets++;
737 net->stats.tx_bytes += skb->len;
738 }
739 dev_kfree_skb(skb);
740
741 return NETDEV_TX_OK;
742}
743
744static inline void disable_net_traffic(pegasus_t *pegasus)
745{
746 __le16 tmp = cpu_to_le16(0);
747
748 set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
749}
750
751static inline void get_interrupt_interval(pegasus_t *pegasus)
752{
753 u16 data;
754 u8 interval;
755
756 read_eprom_word(pegasus, 4, &data);
757 interval = data >> 8;
758 if (pegasus->usb->speed != USB_SPEED_HIGH) {
759 if (interval < 0x80) {
760 netif_info(pegasus, timer, pegasus->net,
761 "intr interval changed from %ums to %ums\n",
762 interval, 0x80);
763 interval = 0x80;
764 data = (data & 0x00FF) | ((u16)interval << 8);
765#ifdef PEGASUS_WRITE_EEPROM
766 write_eprom_word(pegasus, 4, data);
767#endif
768 }
769 }
770 pegasus->intr_interval = interval;
771}
772
773static void set_carrier(struct net_device *net)
774{
775 pegasus_t *pegasus = netdev_priv(net);
776 u16 tmp;
777
778 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
779 return;
780
781 if (tmp & BMSR_LSTATUS)
782 netif_carrier_on(net);
783 else
784 netif_carrier_off(net);
785}
786
787static void free_all_urbs(pegasus_t *pegasus)
788{
789 usb_free_urb(pegasus->intr_urb);
790 usb_free_urb(pegasus->tx_urb);
791 usb_free_urb(pegasus->rx_urb);
792}
793
794static void unlink_all_urbs(pegasus_t *pegasus)
795{
796 usb_kill_urb(pegasus->intr_urb);
797 usb_kill_urb(pegasus->tx_urb);
798 usb_kill_urb(pegasus->rx_urb);
799}
800
801static int alloc_urbs(pegasus_t *pegasus)
802{
803 int res = -ENOMEM;
804
805 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
806 if (!pegasus->rx_urb) {
807 return res;
808 }
809 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
810 if (!pegasus->tx_urb) {
811 usb_free_urb(pegasus->rx_urb);
812 return res;
813 }
814 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
815 if (!pegasus->intr_urb) {
816 usb_free_urb(pegasus->tx_urb);
817 usb_free_urb(pegasus->rx_urb);
818 return res;
819 }
820
821 return 0;
822}
823
824static int pegasus_open(struct net_device *net)
825{
826 pegasus_t *pegasus = netdev_priv(net);
827 int res=-ENOMEM;
828
829 if (pegasus->rx_skb == NULL)
830 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
831 PEGASUS_MTU,
832 GFP_KERNEL);
833 if (!pegasus->rx_skb)
834 goto exit;
835
836 res = set_registers(pegasus, EthID, 6, net->dev_addr);
837
838 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
839 usb_rcvbulkpipe(pegasus->usb, 1),
840 pegasus->rx_skb->data, PEGASUS_MTU,
841 read_bulk_callback, pegasus);
842 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
843 if (res == -ENODEV)
844 netif_device_detach(pegasus->net);
845 netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
846 goto exit;
847 }
848
849 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
850 usb_rcvintpipe(pegasus->usb, 3),
851 pegasus->intr_buff, sizeof(pegasus->intr_buff),
852 intr_callback, pegasus, pegasus->intr_interval);
853 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
854 if (res == -ENODEV)
855 netif_device_detach(pegasus->net);
856 netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
857 usb_kill_urb(pegasus->rx_urb);
858 goto exit;
859 }
860 res = enable_net_traffic(net, pegasus->usb);
861 if (res < 0) {
862 netif_dbg(pegasus, ifup, net,
863 "can't enable_net_traffic() - %d\n", res);
864 res = -EIO;
865 usb_kill_urb(pegasus->rx_urb);
866 usb_kill_urb(pegasus->intr_urb);
867 goto exit;
868 }
869 set_carrier(net);
870 netif_start_queue(net);
871 netif_dbg(pegasus, ifup, net, "open\n");
872 res = 0;
873exit:
874 return res;
875}
876
877static int pegasus_close(struct net_device *net)
878{
879 pegasus_t *pegasus = netdev_priv(net);
880
881 netif_stop_queue(net);
882 if (!(pegasus->flags & PEGASUS_UNPLUG))
883 disable_net_traffic(pegasus);
884 tasklet_kill(&pegasus->rx_tl);
885 unlink_all_urbs(pegasus);
886
887 return 0;
888}
889
890static void pegasus_get_drvinfo(struct net_device *dev,
891 struct ethtool_drvinfo *info)
892{
893 pegasus_t *pegasus = netdev_priv(dev);
894
895 strlcpy(info->driver, driver_name, sizeof(info->driver));
896 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
897 usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info));
898}
899
900
901#define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
902
903static void
904pegasus_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
912static int
913pegasus_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
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
940static 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
948static int
949pegasus_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
959static int
960pegasus_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
967static 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
973static 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
979static u32 pegasus_get_msglevel(struct net_device *dev)
980{
981 pegasus_t *pegasus = netdev_priv(dev);
982 return pegasus->msg_enable;
983}
984
985static 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
991static 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
1003static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1004{
1005 __u16 *data = (__u16 *) &rq->ifr_ifru;
1006 pegasus_t *pegasus = netdev_priv(net);
1007 int res;
1008
1009 switch (cmd) {
1010 case SIOCDEVPRIVATE:
1011 data[0] = pegasus->phy;
1012
1013 case SIOCDEVPRIVATE + 1:
1014 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1015 res = 0;
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
1029static 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
1047static __u8 mii_phy_probe(pegasus_t *pegasus)
1048{
1049 int i;
1050 __u16 tmp;
1051
1052 for (i = 0; i < 32; i++) {
1053 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1054 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1055 continue;
1056 else
1057 return i;
1058 }
1059
1060 return 0xff;
1061}
1062
1063static inline void setup_pegasus_II(pegasus_t *pegasus)
1064{
1065 __u8 data = 0xa5;
1066
1067 set_register(pegasus, Reg1d, 0);
1068 set_register(pegasus, Reg7b, 1);
1069 msleep(100);
1070 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1071 set_register(pegasus, Reg7b, 0);
1072 else
1073 set_register(pegasus, Reg7b, 2);
1074
1075 set_register(pegasus, 0x83, data);
1076 get_registers(pegasus, 0x83, 1, &data);
1077
1078 if (data == 0xa5)
1079 pegasus->chip = 0x8513;
1080 else
1081 pegasus->chip = 0;
1082
1083 set_register(pegasus, 0x80, 0xc0);
1084 set_register(pegasus, 0x83, 0xff);
1085 set_register(pegasus, 0x84, 0x01);
1086
1087 if (pegasus->features & HAS_HOME_PNA && mii_mode)
1088 set_register(pegasus, Reg81, 6);
1089 else
1090 set_register(pegasus, Reg81, 2);
1091}
1092
1093static void check_carrier(struct work_struct *work)
1094{
1095 pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
1096 set_carrier(pegasus->net);
1097 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1098 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
1099 CARRIER_CHECK_DELAY);
1100 }
1101}
1102
1103static int pegasus_blacklisted(struct usb_device *udev)
1104{
1105 struct usb_device_descriptor *udd = &udev->descriptor;
1106
1107
1108
1109
1110 if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
1111 (udd->idProduct == cpu_to_le16(0x0121)) &&
1112 (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
1113 (udd->bDeviceProtocol == 1))
1114 return 1;
1115
1116 return 0;
1117}
1118
1119static int pegasus_probe(struct usb_interface *intf,
1120 const struct usb_device_id *id)
1121{
1122 struct usb_device *dev = interface_to_usbdev(intf);
1123 struct net_device *net;
1124 pegasus_t *pegasus;
1125 int dev_index = id - pegasus_ids;
1126 int res = -ENOMEM;
1127
1128 if (pegasus_blacklisted(dev))
1129 return -ENODEV;
1130
1131 net = alloc_etherdev(sizeof(struct pegasus));
1132 if (!net)
1133 goto out;
1134
1135 pegasus = netdev_priv(net);
1136 pegasus->dev_index = dev_index;
1137
1138 res = alloc_urbs(pegasus);
1139 if (res < 0) {
1140 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1141 goto out1;
1142 }
1143
1144 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1145
1146 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
1147
1148 pegasus->intf = intf;
1149 pegasus->usb = dev;
1150 pegasus->net = net;
1151
1152
1153 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1154 net->netdev_ops = &pegasus_netdev_ops;
1155 net->ethtool_ops = &ops;
1156 pegasus->mii.dev = net;
1157 pegasus->mii.mdio_read = mdio_read;
1158 pegasus->mii.mdio_write = mdio_write;
1159 pegasus->mii.phy_id_mask = 0x1f;
1160 pegasus->mii.reg_num_mask = 0x1f;
1161 pegasus->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
1162 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1163
1164 pegasus->features = usb_dev_id[dev_index].private;
1165 get_interrupt_interval(pegasus);
1166 if (reset_mac(pegasus)) {
1167 dev_err(&intf->dev, "can't reset MAC\n");
1168 res = -EIO;
1169 goto out2;
1170 }
1171 set_ethernet_addr(pegasus);
1172 if (pegasus->features & PEGASUS_II) {
1173 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1174 setup_pegasus_II(pegasus);
1175 }
1176 pegasus->phy = mii_phy_probe(pegasus);
1177 if (pegasus->phy == 0xff) {
1178 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1179 pegasus->phy = 1;
1180 }
1181 pegasus->mii.phy_id = pegasus->phy;
1182 usb_set_intfdata(intf, pegasus);
1183 SET_NETDEV_DEV(net, &intf->dev);
1184 pegasus_reset_wol(net);
1185 res = register_netdev(net);
1186 if (res)
1187 goto out3;
1188 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
1189 CARRIER_CHECK_DELAY);
1190 dev_info(&intf->dev, "%s, %s, %pM\n", net->name,
1191 usb_dev_id[dev_index].name, net->dev_addr);
1192 return 0;
1193
1194out3:
1195 usb_set_intfdata(intf, NULL);
1196out2:
1197 free_all_urbs(pegasus);
1198out1:
1199 free_netdev(net);
1200out:
1201 return res;
1202}
1203
1204static void pegasus_disconnect(struct usb_interface *intf)
1205{
1206 struct pegasus *pegasus = usb_get_intfdata(intf);
1207
1208 usb_set_intfdata(intf, NULL);
1209 if (!pegasus) {
1210 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1211 return;
1212 }
1213
1214 pegasus->flags |= PEGASUS_UNPLUG;
1215 cancel_delayed_work_sync(&pegasus->carrier_check);
1216 unregister_netdev(pegasus->net);
1217 unlink_all_urbs(pegasus);
1218 free_all_urbs(pegasus);
1219 if (pegasus->rx_skb != NULL) {
1220 dev_kfree_skb(pegasus->rx_skb);
1221 pegasus->rx_skb = NULL;
1222 }
1223 free_netdev(pegasus->net);
1224}
1225
1226static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
1227{
1228 struct pegasus *pegasus = usb_get_intfdata(intf);
1229
1230 netif_device_detach(pegasus->net);
1231 cancel_delayed_work_sync(&pegasus->carrier_check);
1232 if (netif_running(pegasus->net)) {
1233 usb_kill_urb(pegasus->rx_urb);
1234 usb_kill_urb(pegasus->intr_urb);
1235 }
1236 return 0;
1237}
1238
1239static int pegasus_resume(struct usb_interface *intf)
1240{
1241 struct pegasus *pegasus = usb_get_intfdata(intf);
1242
1243 netif_device_attach(pegasus->net);
1244 if (netif_running(pegasus->net)) {
1245 pegasus->rx_urb->status = 0;
1246 pegasus->rx_urb->actual_length = 0;
1247 read_bulk_callback(pegasus->rx_urb);
1248
1249 pegasus->intr_urb->status = 0;
1250 pegasus->intr_urb->actual_length = 0;
1251 intr_callback(pegasus->intr_urb);
1252 }
1253 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
1254 CARRIER_CHECK_DELAY);
1255 return 0;
1256}
1257
1258static const struct net_device_ops pegasus_netdev_ops = {
1259 .ndo_open = pegasus_open,
1260 .ndo_stop = pegasus_close,
1261 .ndo_do_ioctl = pegasus_ioctl,
1262 .ndo_start_xmit = pegasus_start_xmit,
1263 .ndo_set_rx_mode = pegasus_set_multicast,
1264 .ndo_tx_timeout = pegasus_tx_timeout,
1265 .ndo_set_mac_address = eth_mac_addr,
1266 .ndo_validate_addr = eth_validate_addr,
1267};
1268
1269static struct usb_driver pegasus_driver = {
1270 .name = driver_name,
1271 .probe = pegasus_probe,
1272 .disconnect = pegasus_disconnect,
1273 .id_table = pegasus_ids,
1274 .suspend = pegasus_suspend,
1275 .resume = pegasus_resume,
1276 .disable_hub_initiated_lpm = 1,
1277};
1278
1279static void __init parse_id(char *id)
1280{
1281 unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0;
1282 char *token, *name = NULL;
1283
1284 if ((token = strsep(&id, ":")) != NULL)
1285 name = token;
1286
1287 if ((token = strsep(&id, ":")) != NULL)
1288 vendor_id = simple_strtoul(token, NULL, 16);
1289 if ((token = strsep(&id, ":")) != NULL)
1290 device_id = simple_strtoul(token, NULL, 16);
1291 flags = simple_strtoul(id, NULL, 16);
1292 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1293 driver_name, name, vendor_id, device_id, flags);
1294
1295 if (vendor_id > 0x10000 || vendor_id == 0)
1296 return;
1297 if (device_id > 0x10000 || device_id == 0)
1298 return;
1299
1300 for (i = 0; usb_dev_id[i].name; i++);
1301 usb_dev_id[i].name = name;
1302 usb_dev_id[i].vendor = vendor_id;
1303 usb_dev_id[i].device = device_id;
1304 usb_dev_id[i].private = flags;
1305 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1306 pegasus_ids[i].idVendor = vendor_id;
1307 pegasus_ids[i].idProduct = device_id;
1308}
1309
1310static int __init pegasus_init(void)
1311{
1312 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
1313 if (devid)
1314 parse_id(devid);
1315 return usb_register(&pegasus_driver);
1316}
1317
1318static void __exit pegasus_exit(void)
1319{
1320 usb_deregister(&pegasus_driver);
1321}
1322
1323module_init(pegasus_init);
1324module_exit(pegasus_exit);
1325