2 * Copyright (c) 1999-2013 Petko Manolov (petkan@nucleusys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 * .... Most of the time spent on reading sources & docs.
10 * v0.2.x First official release for the Linux kernel.
11 * v0.3.0 Beutified and structured, some bugs fixed.
12 * v0.3.x URBifying bulk requests and bugfixing. First relatively
13 * stable release. Still can touch device's registers only
15 * v0.4.0 Control messages remained unurbified are now URBs.
16 * Now we can touch the HW at any time.
17 * v0.4.9 Control urbs again use process context to wait. Argh...
18 * Some long standing bugs (enable_net_traffic) fixed.
19 * Also nasty trick about resubmiting control urb from
20 * interrupt context used. Please let me know how it
21 * behaves. Pegasus II support added since this version.
22 * TODO: suppressing HCD warnings spewage on disconnect.
23 * v0.4.13 Ethernet address is now set at probe(), not at open()
24 * time as this seems to break dhcpd.
25 * v0.5.0 branch to 2.5.x kernels
26 * v0.5.1 ethtool support added
27 * v0.5.5 rx socket buffers are in a pool and the their allocation
28 * is out of the interrupt routine.
30 * v0.9.3 simplified [get|set]_register(s), async update registers
31 * logic revisited, receive skb_pool removed.
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/init.h>
37 #include <linux/delay.h>
38 #include <linux/netdevice.h>
39 #include <linux/etherdevice.h>
40 #include <linux/ethtool.h>
41 #include <linux/mii.h>
42 #include <linux/usb.h>
43 #include <linux/module.h>
44 #include <asm/byteorder.h>
45 #include <asm/uaccess.h>
51 #define DRIVER_VERSION "v0.9.3 (2013/04/25)"
52 #define DRIVER_AUTHOR "Petko Manolov <petkan@nucleusys.com>"
53 #define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
55 static const char driver_name[] = "pegasus";
57 #undef PEGASUS_WRITE_EEPROM
58 #define BMSR_MEDIA (BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | \
59 BMSR_100FULL | BMSR_ANEGCAPABLE)
65 static struct usb_eth_dev usb_dev_id[] = {
66 #define PEGASUS_DEV(pn, vid, pid, flags) \
67 {.name = pn, .vendor = vid, .device = pid, .private = flags},
68 #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
69 PEGASUS_DEV(pn, vid, pid, flags)
72 #undef PEGASUS_DEV_CLASS
77 static struct usb_device_id pegasus_ids[] = {
78 #define PEGASUS_DEV(pn, vid, pid, flags) \
79 {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
81 * The Belkin F8T012xx1 bluetooth adaptor has the same vendor and product
82 * IDs as the Belkin F5D5050, so we need to teach the pegasus driver to
83 * ignore adaptors belonging to the "Wireless" class 0xE0. For this one
84 * case anyway, seeing as the pegasus is for "Wired" adaptors.
86 #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
87 {.match_flags = (USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_DEV_CLASS), \
88 .idVendor = vid, .idProduct = pid, .bDeviceClass = dclass},
91 #undef PEGASUS_DEV_CLASS
96 MODULE_AUTHOR(DRIVER_AUTHOR);
97 MODULE_DESCRIPTION(DRIVER_DESC);
98 MODULE_LICENSE("GPL");
99 module_param(loopback, bool, 0);
100 module_param(mii_mode, bool, 0);
101 module_param(devid, charp, 0);
102 MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
103 MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
104 MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
106 /* use ethtool to change the level for any given device */
107 static int msg_level = -1;
108 module_param(msg_level, int, 0);
109 MODULE_PARM_DESC(msg_level, "Override default message level");
111 MODULE_DEVICE_TABLE(usb, pegasus_ids);
112 static const struct net_device_ops pegasus_netdev_ops;
116 static void async_ctrl_callback(struct urb *urb)
118 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
119 int status = urb->status;
122 dev_dbg(&urb->dev->dev, "%s failed with %d", __func__, status);
127 static int get_registers(pegasus_t *pegasus, __u16 indx, __u16 size, void *data)
131 ret = usb_control_msg(pegasus->usb, usb_rcvctrlpipe(pegasus->usb, 0),
132 PEGASUS_REQ_GET_REGS, PEGASUS_REQT_READ, 0,
133 indx, data, size, 1000);
135 netif_dbg(pegasus, drv, pegasus->net,
136 "%s returned %d\n", __func__, ret);
140 static int set_registers(pegasus_t *pegasus, __u16 indx, __u16 size, void *data)
144 ret = usb_control_msg(pegasus->usb, usb_sndctrlpipe(pegasus->usb, 0),
145 PEGASUS_REQ_SET_REGS, PEGASUS_REQT_WRITE, 0,
146 indx, data, size, 100);
148 netif_dbg(pegasus, drv, pegasus->net,
149 "%s returned %d\n", __func__, ret);
153 static int set_register(pegasus_t *pegasus, __u16 indx, __u8 data)
157 ret = usb_control_msg(pegasus->usb, usb_sndctrlpipe(pegasus->usb, 0),
158 PEGASUS_REQ_SET_REG, PEGASUS_REQT_WRITE, data,
159 indx, &data, 1, 1000);
161 netif_dbg(pegasus, drv, pegasus->net,
162 "%s returned %d\n", __func__, ret);
166 static int update_eth_regs_async(pegasus_t *pegasus)
169 struct urb *async_urb;
170 struct usb_ctrlrequest *req;
172 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
176 async_urb = usb_alloc_urb(0, GFP_ATOMIC);
177 if (async_urb == NULL) {
181 req->bRequestType = PEGASUS_REQT_WRITE;
182 req->bRequest = PEGASUS_REQ_SET_REGS;
183 req->wValue = cpu_to_le16(0);
184 req->wIndex = cpu_to_le16(EthCtrl0);
185 req->wLength = cpu_to_le16(3);
187 usb_fill_control_urb(async_urb, pegasus->usb,
188 usb_sndctrlpipe(pegasus->usb, 0), (void *)req,
189 pegasus->eth_regs, 3, async_ctrl_callback, req);
191 ret = usb_submit_urb(async_urb, GFP_ATOMIC);
194 netif_device_detach(pegasus->net);
195 netif_err(pegasus, drv, pegasus->net,
196 "%s returned %d\n", __func__, ret);
201 static int __mii_op(pegasus_t *p, __u8 phy, __u8 indx, __u16 *regd, __u8 cmd)
204 __u8 data[4] = { phy, 0, 0, indx };
206 int ret = -ETIMEDOUT;
208 if (cmd & PHY_WRITE) {
209 __le16 *t = (__le16 *) & data[1];
210 *t = cpu_to_le16(*regd);
212 set_register(p, PhyCtrl, 0);
213 set_registers(p, PhyAddr, sizeof(data), data);
214 set_register(p, PhyCtrl, (indx | cmd));
215 for (i = 0; i < REG_TIMEOUT; i++) {
216 ret = get_registers(p, PhyCtrl, 1, data);
219 if (data[0] & PHY_DONE)
222 if (i >= REG_TIMEOUT)
224 if (cmd & PHY_READ) {
225 ret = get_registers(p, PhyData, 2, ®di);
226 *regd = le16_to_cpu(regdi);
231 netif_dbg(p, drv, p->net, "%s failed\n", __func__);
235 /* Returns non-negative int on success, error on failure */
236 static int read_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
238 return __mii_op(pegasus, phy, indx, regd, PHY_READ);
241 /* Returns zero on success, error on failure */
242 static int write_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
244 return __mii_op(pegasus, phy, indx, regd, PHY_WRITE);
247 static int mdio_read(struct net_device *dev, int phy_id, int loc)
249 pegasus_t *pegasus = netdev_priv(dev);
252 read_mii_word(pegasus, phy_id, loc, &res);
256 static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
258 pegasus_t *pegasus = netdev_priv(dev);
260 write_mii_word(pegasus, phy_id, loc, (__u16 *)&val);
263 static int read_eprom_word(pegasus_t *pegasus, __u8 index, __u16 *retdata)
270 set_register(pegasus, EpromCtrl, 0);
271 set_register(pegasus, EpromOffset, index);
272 set_register(pegasus, EpromCtrl, EPROM_READ);
274 for (i = 0; i < REG_TIMEOUT; i++) {
275 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
276 if (tmp & EPROM_DONE)
278 if (ret == -ESHUTDOWN)
281 if (i >= REG_TIMEOUT)
284 ret = get_registers(pegasus, EpromData, 2, &retdatai);
285 *retdata = le16_to_cpu(retdatai);
289 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
293 #ifdef PEGASUS_WRITE_EEPROM
294 static inline void enable_eprom_write(pegasus_t *pegasus)
298 get_registers(pegasus, EthCtrl2, 1, &tmp);
299 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
302 static inline void disable_eprom_write(pegasus_t *pegasus)
306 get_registers(pegasus, EthCtrl2, 1, &tmp);
307 set_register(pegasus, EpromCtrl, 0);
308 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
311 static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data)
314 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
316 __le16 le_data = cpu_to_le16(data);
318 set_registers(pegasus, EpromOffset, 4, d);
319 enable_eprom_write(pegasus);
320 set_register(pegasus, EpromOffset, index);
321 set_registers(pegasus, EpromData, 2, &le_data);
322 set_register(pegasus, EpromCtrl, EPROM_WRITE);
324 for (i = 0; i < REG_TIMEOUT; i++) {
325 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
326 if (ret == -ESHUTDOWN)
328 if (tmp & EPROM_DONE)
331 disable_eprom_write(pegasus);
332 if (i >= REG_TIMEOUT)
338 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
341 #endif /* PEGASUS_WRITE_EEPROM */
343 static inline void get_node_id(pegasus_t *pegasus, __u8 *id)
348 for (i = 0; i < 3; i++) {
349 read_eprom_word(pegasus, i, &w16);
350 ((__le16 *) id)[i] = cpu_to_le16(w16);
354 static void set_ethernet_addr(pegasus_t *pegasus)
358 if (pegasus->features & PEGASUS_II) {
359 get_registers(pegasus, 0x10, sizeof(node_id), node_id);
361 get_node_id(pegasus, node_id);
362 set_registers(pegasus, EthID, sizeof(node_id), node_id);
364 memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
367 static inline int reset_mac(pegasus_t *pegasus)
372 set_register(pegasus, EthCtrl1, data);
373 for (i = 0; i < REG_TIMEOUT; i++) {
374 get_registers(pegasus, EthCtrl1, 1, &data);
378 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
379 set_register(pegasus, Gpio1, 0x34);
381 set_register(pegasus, Gpio1, 0x26);
382 set_register(pegasus, Gpio0, pegasus->features);
383 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
387 if (i == REG_TIMEOUT)
390 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
391 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
392 set_register(pegasus, Gpio0, 0x24);
393 set_register(pegasus, Gpio0, 0x26);
395 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
397 read_mii_word(pegasus, 3, 0x1b, &auxmode);
399 write_mii_word(pegasus, 3, 0x1b, &auxmode);
405 static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
409 pegasus_t *pegasus = netdev_priv(dev);
412 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
415 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
416 data[1] |= 0x20; /* set full duplex */
417 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
418 data[1] |= 0x10; /* set 100 Mbps */
421 data[2] = loopback ? 0x09 : 0x01;
423 memcpy(pegasus->eth_regs, data, sizeof(data));
424 ret = set_registers(pegasus, EthCtrl0, 3, data);
426 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
427 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
428 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
430 read_mii_word(pegasus, 0, 0x1b, &auxmode);
432 write_mii_word(pegasus, 0, 0x1b, &auxmode);
438 static void read_bulk_callback(struct urb *urb)
440 pegasus_t *pegasus = urb->context;
441 struct net_device *net;
442 int rx_status, count = urb->actual_length;
443 int status = urb->status;
444 u8 *buf = urb->transfer_buffer;
451 if (!netif_device_present(net) || !netif_running(net))
458 netif_dbg(pegasus, rx_err, net, "reset MAC\n");
459 pegasus->flags &= ~PEGASUS_RX_BUSY;
461 case -EPIPE: /* stall, or disconnect from TT */
462 /* FIXME schedule work to clear the halt */
463 netif_warn(pegasus, rx_err, net, "no rx stall recovery\n");
468 netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status);
471 netif_dbg(pegasus, rx_err, net, "RX status %d\n", status);
475 if (!count || count < 4)
478 rx_status = buf[count - 2];
479 if (rx_status & 0x1e) {
480 netif_dbg(pegasus, rx_err, net,
481 "RX packet error %x\n", rx_status);
482 pegasus->stats.rx_errors++;
483 if (rx_status & 0x06) /* long or runt */
484 pegasus->stats.rx_length_errors++;
485 if (rx_status & 0x08)
486 pegasus->stats.rx_crc_errors++;
487 if (rx_status & 0x10) /* extra bits */
488 pegasus->stats.rx_frame_errors++;
491 if (pegasus->chip == 0x8513) {
492 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
494 pegasus->rx_skb->data += 2;
496 pkt_len = buf[count - 3] << 8;
497 pkt_len += buf[count - 4];
503 * If the packet is unreasonably long, quietly drop it rather than
504 * kernel panicing by calling skb_put.
506 if (pkt_len > PEGASUS_MTU)
510 * at this point we are sure pegasus->rx_skb != NULL
511 * so we go ahead and pass up the packet.
513 skb_put(pegasus->rx_skb, pkt_len);
514 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
515 netif_rx(pegasus->rx_skb);
516 pegasus->stats.rx_packets++;
517 pegasus->stats.rx_bytes += pkt_len;
519 if (pegasus->flags & PEGASUS_UNPLUG)
522 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, PEGASUS_MTU,
525 if (pegasus->rx_skb == NULL)
528 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
529 usb_rcvbulkpipe(pegasus->usb, 1),
530 pegasus->rx_skb->data, PEGASUS_MTU + 8,
531 read_bulk_callback, pegasus);
532 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
533 if (rx_status == -ENODEV)
534 netif_device_detach(pegasus->net);
535 else if (rx_status) {
536 pegasus->flags |= PEGASUS_RX_URB_FAIL;
539 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
545 tasklet_schedule(&pegasus->rx_tl);
548 static void rx_fixup(unsigned long data)
553 pegasus = (pegasus_t *) data;
554 if (pegasus->flags & PEGASUS_UNPLUG)
557 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
560 if (pegasus->rx_skb == NULL)
561 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
564 if (pegasus->rx_skb == NULL) {
565 netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
566 tasklet_schedule(&pegasus->rx_tl);
569 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
570 usb_rcvbulkpipe(pegasus->usb, 1),
571 pegasus->rx_skb->data, PEGASUS_MTU + 8,
572 read_bulk_callback, pegasus);
574 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
575 if (status == -ENODEV)
576 netif_device_detach(pegasus->net);
578 pegasus->flags |= PEGASUS_RX_URB_FAIL;
579 tasklet_schedule(&pegasus->rx_tl);
581 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
585 static void write_bulk_callback(struct urb *urb)
587 pegasus_t *pegasus = urb->context;
588 struct net_device *net;
589 int status = urb->status;
596 if (!netif_device_present(net) || !netif_running(net))
601 /* FIXME schedule_work() to clear the tx halt */
602 netif_stop_queue(net);
603 netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
608 netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
611 netif_info(pegasus, tx_err, net, "TX status %d\n", status);
617 net->trans_start = jiffies; /* prevent tx timeout */
618 netif_wake_queue(net);
621 static void intr_callback(struct urb *urb)
623 pegasus_t *pegasus = urb->context;
624 struct net_device *net;
625 int res, status = urb->status;
634 case -ECONNRESET: /* unlink */
639 /* some Pegasus-I products report LOTS of data
640 * toggle errors... avoid log spamming
642 netif_dbg(pegasus, timer, net, "intr status %d\n", status);
645 if (urb->actual_length >= 6) {
646 u8 *d = urb->transfer_buffer;
648 /* byte 0 == tx_status1, reg 2B */
649 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
650 |LATE_COL|JABBER_TIMEOUT)) {
651 pegasus->stats.tx_errors++;
652 if (d[0] & TX_UNDERRUN)
653 pegasus->stats.tx_fifo_errors++;
654 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
655 pegasus->stats.tx_aborted_errors++;
657 pegasus->stats.tx_window_errors++;
660 /* d[5].LINK_STATUS lies on some adapters.
661 * d[0].NO_CARRIER kicks in only with failed TX.
662 * ... so monitoring with MII may be safest.
665 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
666 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
669 res = usb_submit_urb(urb, GFP_ATOMIC);
671 netif_device_detach(pegasus->net);
673 netif_err(pegasus, timer, net,
674 "can't resubmit interrupt urb, %d\n", res);
677 static void pegasus_tx_timeout(struct net_device *net)
679 pegasus_t *pegasus = netdev_priv(net);
680 netif_warn(pegasus, timer, net, "tx timeout\n");
681 usb_unlink_urb(pegasus->tx_urb);
682 pegasus->stats.tx_errors++;
685 static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
686 struct net_device *net)
688 pegasus_t *pegasus = netdev_priv(net);
689 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
691 __u16 l16 = skb->len;
693 netif_stop_queue(net);
695 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
696 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
697 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
698 usb_sndbulkpipe(pegasus->usb, 2),
699 pegasus->tx_buff, count,
700 write_bulk_callback, pegasus);
701 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
702 netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
704 case -EPIPE: /* stall, or disconnect from TT */
705 /* cleanup should already have been scheduled */
707 case -ENODEV: /* disconnect() upcoming */
709 netif_device_detach(pegasus->net);
712 pegasus->stats.tx_errors++;
713 netif_start_queue(net);
716 pegasus->stats.tx_packets++;
717 pegasus->stats.tx_bytes += skb->len;
724 static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
726 return &((pegasus_t *) netdev_priv(dev))->stats;
729 static inline void disable_net_traffic(pegasus_t *pegasus)
731 __le16 tmp = cpu_to_le16(0);
733 set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
736 static inline void get_interrupt_interval(pegasus_t *pegasus)
741 read_eprom_word(pegasus, 4, &data);
742 interval = data >> 8;
743 if (pegasus->usb->speed != USB_SPEED_HIGH) {
744 if (interval < 0x80) {
745 netif_info(pegasus, timer, pegasus->net,
746 "intr interval changed from %ums to %ums\n",
749 data = (data & 0x00FF) | ((u16)interval << 8);
750 #ifdef PEGASUS_WRITE_EEPROM
751 write_eprom_word(pegasus, 4, data);
755 pegasus->intr_interval = interval;
758 static void set_carrier(struct net_device *net)
760 pegasus_t *pegasus = netdev_priv(net);
763 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
766 if (tmp & BMSR_LSTATUS)
767 netif_carrier_on(net);
769 netif_carrier_off(net);
772 static void free_all_urbs(pegasus_t *pegasus)
774 usb_free_urb(pegasus->intr_urb);
775 usb_free_urb(pegasus->tx_urb);
776 usb_free_urb(pegasus->rx_urb);
779 static void unlink_all_urbs(pegasus_t *pegasus)
781 usb_kill_urb(pegasus->intr_urb);
782 usb_kill_urb(pegasus->tx_urb);
783 usb_kill_urb(pegasus->rx_urb);
786 static int alloc_urbs(pegasus_t *pegasus)
790 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
791 if (!pegasus->rx_urb) {
794 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
795 if (!pegasus->tx_urb) {
796 usb_free_urb(pegasus->rx_urb);
799 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
800 if (!pegasus->intr_urb) {
801 usb_free_urb(pegasus->tx_urb);
802 usb_free_urb(pegasus->rx_urb);
809 static int pegasus_open(struct net_device *net)
811 pegasus_t *pegasus = netdev_priv(net);
814 if (pegasus->rx_skb == NULL)
815 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
818 if (!pegasus->rx_skb)
821 res = set_registers(pegasus, EthID, 6, net->dev_addr);
823 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
824 usb_rcvbulkpipe(pegasus->usb, 1),
825 pegasus->rx_skb->data, PEGASUS_MTU + 8,
826 read_bulk_callback, pegasus);
827 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
829 netif_device_detach(pegasus->net);
830 netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
834 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
835 usb_rcvintpipe(pegasus->usb, 3),
836 pegasus->intr_buff, sizeof(pegasus->intr_buff),
837 intr_callback, pegasus, pegasus->intr_interval);
838 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
840 netif_device_detach(pegasus->net);
841 netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
842 usb_kill_urb(pegasus->rx_urb);
845 res = enable_net_traffic(net, pegasus->usb);
847 netif_dbg(pegasus, ifup, net,
848 "can't enable_net_traffic() - %d\n", res);
850 usb_kill_urb(pegasus->rx_urb);
851 usb_kill_urb(pegasus->intr_urb);
855 netif_start_queue(net);
856 netif_dbg(pegasus, ifup, net, "open\n");
862 static int pegasus_close(struct net_device *net)
864 pegasus_t *pegasus = netdev_priv(net);
866 netif_stop_queue(net);
867 if (!(pegasus->flags & PEGASUS_UNPLUG))
868 disable_net_traffic(pegasus);
869 tasklet_kill(&pegasus->rx_tl);
870 unlink_all_urbs(pegasus);
875 static void pegasus_get_drvinfo(struct net_device *dev,
876 struct ethtool_drvinfo *info)
878 pegasus_t *pegasus = netdev_priv(dev);
880 strlcpy(info->driver, driver_name, sizeof(info->driver));
881 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
882 usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info));
885 /* also handles three patterns of some kind in hardware */
886 #define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
889 pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
891 pegasus_t *pegasus = netdev_priv(dev);
893 wol->supported = WAKE_MAGIC | WAKE_PHY;
894 wol->wolopts = pegasus->wolopts;
898 pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
900 pegasus_t *pegasus = netdev_priv(dev);
904 if (wol->wolopts & ~WOL_SUPPORTED)
907 if (wol->wolopts & WAKE_MAGIC)
909 if (wol->wolopts & WAKE_PHY)
911 /* FIXME this 0x10 bit still needs to get set in the chip... */
913 pegasus->eth_regs[0] |= 0x10;
915 pegasus->eth_regs[0] &= ~0x10;
916 pegasus->wolopts = wol->wolopts;
918 ret = set_register(pegasus, WakeupControl, reg78);
920 ret = device_set_wakeup_enable(&pegasus->usb->dev,
925 static inline void pegasus_reset_wol(struct net_device *dev)
927 struct ethtool_wolinfo wol;
929 memset(&wol, 0, sizeof wol);
930 (void) pegasus_set_wol(dev, &wol);
934 pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
938 pegasus = netdev_priv(dev);
939 mii_ethtool_gset(&pegasus->mii, ecmd);
944 pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
946 pegasus_t *pegasus = netdev_priv(dev);
947 return mii_ethtool_sset(&pegasus->mii, ecmd);
950 static int pegasus_nway_reset(struct net_device *dev)
952 pegasus_t *pegasus = netdev_priv(dev);
953 return mii_nway_restart(&pegasus->mii);
956 static u32 pegasus_get_link(struct net_device *dev)
958 pegasus_t *pegasus = netdev_priv(dev);
959 return mii_link_ok(&pegasus->mii);
962 static u32 pegasus_get_msglevel(struct net_device *dev)
964 pegasus_t *pegasus = netdev_priv(dev);
965 return pegasus->msg_enable;
968 static void pegasus_set_msglevel(struct net_device *dev, u32 v)
970 pegasus_t *pegasus = netdev_priv(dev);
971 pegasus->msg_enable = v;
974 static const struct ethtool_ops ops = {
975 .get_drvinfo = pegasus_get_drvinfo,
976 .get_settings = pegasus_get_settings,
977 .set_settings = pegasus_set_settings,
978 .nway_reset = pegasus_nway_reset,
979 .get_link = pegasus_get_link,
980 .get_msglevel = pegasus_get_msglevel,
981 .set_msglevel = pegasus_set_msglevel,
982 .get_wol = pegasus_get_wol,
983 .set_wol = pegasus_set_wol,
986 static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
988 __u16 *data = (__u16 *) &rq->ifr_ifru;
989 pegasus_t *pegasus = netdev_priv(net);
994 data[0] = pegasus->phy;
995 case SIOCDEVPRIVATE + 1:
996 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
999 case SIOCDEVPRIVATE + 2:
1000 if (!capable(CAP_NET_ADMIN))
1002 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, &data[2]);
1011 static void pegasus_set_multicast(struct net_device *net)
1013 pegasus_t *pegasus = netdev_priv(net);
1015 if (net->flags & IFF_PROMISC) {
1016 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1017 netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
1018 } else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
1019 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1020 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1021 netif_dbg(pegasus, link, net, "set allmulti\n");
1023 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1024 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1026 update_eth_regs_async(pegasus);
1029 static __u8 mii_phy_probe(pegasus_t *pegasus)
1034 for (i = 0; i < 32; i++) {
1035 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1036 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1045 static inline void setup_pegasus_II(pegasus_t *pegasus)
1049 set_register(pegasus, Reg1d, 0);
1050 set_register(pegasus, Reg7b, 1);
1052 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1053 set_register(pegasus, Reg7b, 0);
1055 set_register(pegasus, Reg7b, 2);
1057 set_register(pegasus, 0x83, data);
1058 get_registers(pegasus, 0x83, 1, &data);
1061 pegasus->chip = 0x8513;
1065 set_register(pegasus, 0x80, 0xc0);
1066 set_register(pegasus, 0x83, 0xff);
1067 set_register(pegasus, 0x84, 0x01);
1069 if (pegasus->features & HAS_HOME_PNA && mii_mode)
1070 set_register(pegasus, Reg81, 6);
1072 set_register(pegasus, Reg81, 2);
1076 static int pegasus_count;
1077 static struct workqueue_struct *pegasus_workqueue;
1078 #define CARRIER_CHECK_DELAY (2 * HZ)
1080 static void check_carrier(struct work_struct *work)
1082 pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
1083 set_carrier(pegasus->net);
1084 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1085 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1086 CARRIER_CHECK_DELAY);
1090 static int pegasus_blacklisted(struct usb_device *udev)
1092 struct usb_device_descriptor *udd = &udev->descriptor;
1094 /* Special quirk to keep the driver from handling the Belkin Bluetooth
1095 * dongle which happens to have the same ID.
1097 if ((udd->idVendor == cpu_to_le16(VENDOR_BELKIN)) &&
1098 (udd->idProduct == cpu_to_le16(0x0121)) &&
1099 (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
1100 (udd->bDeviceProtocol == 1))
1106 /* we rely on probe() and remove() being serialized so we
1107 * don't need extra locking on pegasus_count.
1109 static void pegasus_dec_workqueue(void)
1112 if (pegasus_count == 0) {
1113 destroy_workqueue(pegasus_workqueue);
1114 pegasus_workqueue = NULL;
1118 static int pegasus_probe(struct usb_interface *intf,
1119 const struct usb_device_id *id)
1121 struct usb_device *dev = interface_to_usbdev(intf);
1122 struct net_device *net;
1124 int dev_index = id - pegasus_ids;
1127 if (pegasus_blacklisted(dev))
1130 if (pegasus_count == 0) {
1131 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1132 if (!pegasus_workqueue)
1137 net = alloc_etherdev(sizeof(struct pegasus));
1141 pegasus = netdev_priv(net);
1142 pegasus->dev_index = dev_index;
1144 res = alloc_urbs(pegasus);
1146 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1150 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1152 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
1154 pegasus->intf = intf;
1159 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1160 net->netdev_ops = &pegasus_netdev_ops;
1161 SET_ETHTOOL_OPS(net, &ops);
1162 pegasus->mii.dev = net;
1163 pegasus->mii.mdio_read = mdio_read;
1164 pegasus->mii.mdio_write = mdio_write;
1165 pegasus->mii.phy_id_mask = 0x1f;
1166 pegasus->mii.reg_num_mask = 0x1f;
1167 pegasus->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
1168 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1170 pegasus->features = usb_dev_id[dev_index].private;
1171 get_interrupt_interval(pegasus);
1172 if (reset_mac(pegasus)) {
1173 dev_err(&intf->dev, "can't reset MAC\n");
1177 set_ethernet_addr(pegasus);
1178 if (pegasus->features & PEGASUS_II) {
1179 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1180 setup_pegasus_II(pegasus);
1182 pegasus->phy = mii_phy_probe(pegasus);
1183 if (pegasus->phy == 0xff) {
1184 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1187 pegasus->mii.phy_id = pegasus->phy;
1188 usb_set_intfdata(intf, pegasus);
1189 SET_NETDEV_DEV(net, &intf->dev);
1190 pegasus_reset_wol(net);
1191 res = register_netdev(net);
1194 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1195 CARRIER_CHECK_DELAY);
1196 dev_info(&intf->dev, "%s, %s, %pM\n", net->name,
1197 usb_dev_id[dev_index].name, net->dev_addr);
1201 usb_set_intfdata(intf, NULL);
1203 free_all_urbs(pegasus);
1207 pegasus_dec_workqueue();
1211 static void pegasus_disconnect(struct usb_interface *intf)
1213 struct pegasus *pegasus = usb_get_intfdata(intf);
1215 usb_set_intfdata(intf, NULL);
1217 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1221 pegasus->flags |= PEGASUS_UNPLUG;
1222 cancel_delayed_work(&pegasus->carrier_check);
1223 unregister_netdev(pegasus->net);
1224 unlink_all_urbs(pegasus);
1225 free_all_urbs(pegasus);
1226 if (pegasus->rx_skb != NULL) {
1227 dev_kfree_skb(pegasus->rx_skb);
1228 pegasus->rx_skb = NULL;
1230 free_netdev(pegasus->net);
1231 pegasus_dec_workqueue();
1234 static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
1236 struct pegasus *pegasus = usb_get_intfdata(intf);
1238 netif_device_detach(pegasus->net);
1239 cancel_delayed_work(&pegasus->carrier_check);
1240 if (netif_running(pegasus->net)) {
1241 usb_kill_urb(pegasus->rx_urb);
1242 usb_kill_urb(pegasus->intr_urb);
1247 static int pegasus_resume(struct usb_interface *intf)
1249 struct pegasus *pegasus = usb_get_intfdata(intf);
1251 netif_device_attach(pegasus->net);
1252 if (netif_running(pegasus->net)) {
1253 pegasus->rx_urb->status = 0;
1254 pegasus->rx_urb->actual_length = 0;
1255 read_bulk_callback(pegasus->rx_urb);
1257 pegasus->intr_urb->status = 0;
1258 pegasus->intr_urb->actual_length = 0;
1259 intr_callback(pegasus->intr_urb);
1261 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1262 CARRIER_CHECK_DELAY);
1266 static const struct net_device_ops pegasus_netdev_ops = {
1267 .ndo_open = pegasus_open,
1268 .ndo_stop = pegasus_close,
1269 .ndo_do_ioctl = pegasus_ioctl,
1270 .ndo_start_xmit = pegasus_start_xmit,
1271 .ndo_set_rx_mode = pegasus_set_multicast,
1272 .ndo_get_stats = pegasus_netdev_stats,
1273 .ndo_tx_timeout = pegasus_tx_timeout,
1274 .ndo_change_mtu = eth_change_mtu,
1275 .ndo_set_mac_address = eth_mac_addr,
1276 .ndo_validate_addr = eth_validate_addr,
1279 static struct usb_driver pegasus_driver = {
1280 .name = driver_name,
1281 .probe = pegasus_probe,
1282 .disconnect = pegasus_disconnect,
1283 .id_table = pegasus_ids,
1284 .suspend = pegasus_suspend,
1285 .resume = pegasus_resume,
1286 .disable_hub_initiated_lpm = 1,
1289 static void __init parse_id(char *id)
1291 unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0;
1292 char *token, *name = NULL;
1294 if ((token = strsep(&id, ":")) != NULL)
1296 /* name now points to a null terminated string*/
1297 if ((token = strsep(&id, ":")) != NULL)
1298 vendor_id = simple_strtoul(token, NULL, 16);
1299 if ((token = strsep(&id, ":")) != NULL)
1300 device_id = simple_strtoul(token, NULL, 16);
1301 flags = simple_strtoul(id, NULL, 16);
1302 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1303 driver_name, name, vendor_id, device_id, flags);
1305 if (vendor_id > 0x10000 || vendor_id == 0)
1307 if (device_id > 0x10000 || device_id == 0)
1310 for (i = 0; usb_dev_id[i].name; i++);
1311 usb_dev_id[i].name = name;
1312 usb_dev_id[i].vendor = vendor_id;
1313 usb_dev_id[i].device = device_id;
1314 usb_dev_id[i].private = flags;
1315 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1316 pegasus_ids[i].idVendor = vendor_id;
1317 pegasus_ids[i].idProduct = device_id;
1320 static int __init pegasus_init(void)
1322 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
1325 return usb_register(&pegasus_driver);
1328 static void __exit pegasus_exit(void)
1330 usb_deregister(&pegasus_driver);
1333 module_init(pegasus_init);
1334 module_exit(pegasus_exit);