1 // SPDX-License-Identifier: GPL-2.0-only
6 * .... Most of the time spent on reading sources & docs.
7 * v0.2.x First official release for the Linux kernel.
8 * v0.3.0 Beutified and structured, some bugs fixed.
9 * v0.3.x URBifying bulk requests and bugfixing. First relatively
10 * stable release. Still can touch device's registers only
12 * v0.4.0 Control messages remained unurbified are now URBs.
13 * Now we can touch the HW at any time.
14 * v0.4.9 Control urbs again use process context to wait. Argh...
15 * Some long standing bugs (enable_net_traffic) fixed.
16 * Also nasty trick about resubmiting control urb from
17 * interrupt context used. Please let me know how it
18 * behaves. Pegasus II support added since this version.
19 * TODO: suppressing HCD warnings spewage on disconnect.
20 * v0.4.13 Ethernet address is now set at probe(), not at open()
21 * time as this seems to break dhcpd.
22 * v0.5.0 branch to 2.5.x kernels
23 * v0.5.1 ethtool support added
24 * v0.5.5 rx socket buffers are in a pool and the their allocation
25 * is out of the interrupt routine.
27 * v0.9.3 simplified [get|set]_register(s), async update registers
28 * logic revisited, receive skb_pool removed.
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>
48 #define DRIVER_VERSION "v0.9.3 (2013/04/25)"
50 #define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
52 static const char driver_name[] = "pegasus";
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)
63 static struct usb_eth_dev usb_dev_id[] = {
64 #define PEGASUS_DEV(pn, vid, pid, flags) \
65 {.name = pn, .vendor = vid, .device = pid, .private = flags},
66 #define PEGASUS_DEV_CLASS(pn, vid, pid, dclass, flags) \
67 PEGASUS_DEV(pn, vid, pid, flags)
70 #undef PEGASUS_DEV_CLASS
75 static struct usb_device_id pegasus_ids[] = {
76 #define PEGASUS_DEV(pn, vid, pid, flags) \
77 {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
79 * The Belkin F8T012xx1 bluetooth adaptor has the same vendor and product
80 * IDs as the Belkin F5D5050, so we need to teach the pegasus driver to
81 * ignore adaptors belonging to the "Wireless" class 0xE0. For this one
82 * case anyway, seeing as the pegasus is for "Wired" adaptors.
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},
89 #undef PEGASUS_DEV_CLASS
94 MODULE_AUTHOR(DRIVER_AUTHOR);
95 MODULE_DESCRIPTION(DRIVER_DESC);
96 MODULE_LICENSE("GPL");
97 module_param(loopback, bool, 0);
98 module_param(mii_mode, bool, 0);
99 module_param(devid, charp, 0);
100 MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
101 MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
102 MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
104 /* use ethtool to change the level for any given device */
105 static int msg_level = -1;
106 module_param(msg_level, int, 0);
107 MODULE_PARM_DESC(msg_level, "Override default message level");
109 MODULE_DEVICE_TABLE(usb, pegasus_ids);
110 static const struct net_device_ops pegasus_netdev_ops;
114 static void async_ctrl_callback(struct urb *urb)
116 struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
117 int status = urb->status;
120 dev_dbg(&urb->dev->dev, "%s failed with %d", __func__, status);
125 static int get_registers(pegasus_t *pegasus, __u16 indx, __u16 size, void *data)
127 return usb_control_msg_recv(pegasus->usb, 0, PEGASUS_REQ_GET_REGS,
128 PEGASUS_REQT_READ, 0, indx, data, size,
132 static int set_registers(pegasus_t *pegasus, __u16 indx, __u16 size,
135 return usb_control_msg_send(pegasus->usb, 0, PEGASUS_REQ_SET_REGS,
136 PEGASUS_REQT_WRITE, 0, indx, data, size,
141 * There is only one way to write to a single ADM8511 register and this is via
142 * specific control request. 'data' is ignored by the device, but it is here to
145 static int set_register(pegasus_t *pegasus, __u16 indx, __u8 data)
149 return usb_control_msg_send(pegasus->usb, 0, PEGASUS_REQ_SET_REG,
150 PEGASUS_REQT_WRITE, data, indx, buf, 1,
154 static int update_eth_regs_async(pegasus_t *pegasus)
157 struct urb *async_urb;
158 struct usb_ctrlrequest *req;
160 req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC);
164 async_urb = usb_alloc_urb(0, GFP_ATOMIC);
165 if (async_urb == NULL) {
169 req->bRequestType = PEGASUS_REQT_WRITE;
170 req->bRequest = PEGASUS_REQ_SET_REGS;
171 req->wValue = cpu_to_le16(0);
172 req->wIndex = cpu_to_le16(EthCtrl0);
173 req->wLength = cpu_to_le16(3);
175 usb_fill_control_urb(async_urb, pegasus->usb,
176 usb_sndctrlpipe(pegasus->usb, 0), (void *)req,
177 pegasus->eth_regs, 3, async_ctrl_callback, req);
179 ret = usb_submit_urb(async_urb, GFP_ATOMIC);
182 netif_device_detach(pegasus->net);
183 netif_err(pegasus, drv, pegasus->net,
184 "%s returned %d\n", __func__, ret);
189 static int __mii_op(pegasus_t *p, __u8 phy, __u8 indx, __u16 *regd, __u8 cmd)
192 __u8 data[4] = { phy, 0, 0, indx };
194 int ret = -ETIMEDOUT;
196 if (cmd & PHY_WRITE) {
197 __le16 *t = (__le16 *) & data[1];
198 *t = cpu_to_le16(*regd);
200 set_register(p, PhyCtrl, 0);
201 set_registers(p, PhyAddr, sizeof(data), data);
202 set_register(p, PhyCtrl, (indx | cmd));
203 for (i = 0; i < REG_TIMEOUT; i++) {
204 ret = get_registers(p, PhyCtrl, 1, data);
207 if (data[0] & PHY_DONE)
210 if (i >= REG_TIMEOUT)
212 if (cmd & PHY_READ) {
213 ret = get_registers(p, PhyData, 2, ®di);
214 *regd = le16_to_cpu(regdi);
219 netif_dbg(p, drv, p->net, "%s failed\n", __func__);
223 /* Returns non-negative int on success, error on failure */
224 static int read_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
226 return __mii_op(pegasus, phy, indx, regd, PHY_READ);
229 /* Returns zero on success, error on failure */
230 static int write_mii_word(pegasus_t *pegasus, __u8 phy, __u8 indx, __u16 *regd)
232 return __mii_op(pegasus, phy, indx, regd, PHY_WRITE);
235 static int mdio_read(struct net_device *dev, int phy_id, int loc)
237 pegasus_t *pegasus = netdev_priv(dev);
240 read_mii_word(pegasus, phy_id, loc, &res);
244 static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
246 pegasus_t *pegasus = netdev_priv(dev);
249 write_mii_word(pegasus, phy_id, loc, &data);
252 static int read_eprom_word(pegasus_t *pegasus, __u8 index, __u16 *retdata)
259 set_register(pegasus, EpromCtrl, 0);
260 set_register(pegasus, EpromOffset, index);
261 set_register(pegasus, EpromCtrl, EPROM_READ);
263 for (i = 0; i < REG_TIMEOUT; i++) {
264 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
265 if (tmp & EPROM_DONE)
267 if (ret == -ESHUTDOWN)
270 if (i >= REG_TIMEOUT)
273 ret = get_registers(pegasus, EpromData, 2, &retdatai);
274 *retdata = le16_to_cpu(retdatai);
278 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
282 #ifdef PEGASUS_WRITE_EEPROM
283 static inline void enable_eprom_write(pegasus_t *pegasus)
287 get_registers(pegasus, EthCtrl2, 1, &tmp);
288 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
291 static inline void disable_eprom_write(pegasus_t *pegasus)
295 get_registers(pegasus, EthCtrl2, 1, &tmp);
296 set_register(pegasus, EpromCtrl, 0);
297 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
300 static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data)
303 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
305 __le16 le_data = cpu_to_le16(data);
307 set_registers(pegasus, EpromOffset, 4, d);
308 enable_eprom_write(pegasus);
309 set_register(pegasus, EpromOffset, index);
310 set_registers(pegasus, EpromData, 2, &le_data);
311 set_register(pegasus, EpromCtrl, EPROM_WRITE);
313 for (i = 0; i < REG_TIMEOUT; i++) {
314 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
315 if (ret == -ESHUTDOWN)
317 if (tmp & EPROM_DONE)
320 disable_eprom_write(pegasus);
321 if (i >= REG_TIMEOUT)
327 netif_warn(pegasus, drv, pegasus->net, "%s failed\n", __func__);
330 #endif /* PEGASUS_WRITE_EEPROM */
332 static inline int get_node_id(pegasus_t *pegasus, u8 *id)
337 for (i = 0; i < 3; i++) {
338 ret = read_eprom_word(pegasus, i, &w16);
341 ((__le16 *) id)[i] = cpu_to_le16(w16);
347 static void set_ethernet_addr(pegasus_t *pegasus)
352 if (pegasus->features & PEGASUS_II) {
353 ret = get_registers(pegasus, 0x10, sizeof(node_id), node_id);
357 ret = get_node_id(pegasus, node_id);
360 ret = set_registers(pegasus, EthID, sizeof(node_id), node_id);
365 memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
369 eth_hw_addr_random(pegasus->net);
370 dev_info(&pegasus->intf->dev, "software assigned MAC address.\n");
375 static inline int reset_mac(pegasus_t *pegasus)
380 set_register(pegasus, EthCtrl1, data);
381 for (i = 0; i < REG_TIMEOUT; i++) {
382 get_registers(pegasus, EthCtrl1, 1, &data);
386 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
387 set_register(pegasus, Gpio1, 0x34);
389 set_register(pegasus, Gpio1, 0x26);
390 set_register(pegasus, Gpio0, pegasus->features);
391 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
395 if (i == REG_TIMEOUT)
398 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
399 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
400 set_register(pegasus, Gpio0, 0x24);
401 set_register(pegasus, Gpio0, 0x26);
403 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
405 read_mii_word(pegasus, 3, 0x1b, &auxmode);
407 write_mii_word(pegasus, 3, 0x1b, &auxmode);
413 static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
417 pegasus_t *pegasus = netdev_priv(dev);
420 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
421 data[0] = 0xc8; /* TX & RX enable, append status, no CRC */
423 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
424 data[1] |= 0x20; /* set full duplex */
425 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
426 data[1] |= 0x10; /* set 100 Mbps */
429 data[2] = loopback ? 0x09 : 0x01;
431 memcpy(pegasus->eth_regs, data, sizeof(data));
432 ret = set_registers(pegasus, EthCtrl0, 3, data);
434 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
435 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
436 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
438 read_mii_word(pegasus, 0, 0x1b, &auxmode);
440 write_mii_word(pegasus, 0, 0x1b, &auxmode);
446 static void read_bulk_callback(struct urb *urb)
448 pegasus_t *pegasus = urb->context;
449 struct net_device *net;
450 int rx_status, count = urb->actual_length;
451 int status = urb->status;
452 u8 *buf = urb->transfer_buffer;
459 if (!netif_device_present(net) || !netif_running(net))
466 netif_dbg(pegasus, rx_err, net, "reset MAC\n");
467 pegasus->flags &= ~PEGASUS_RX_BUSY;
469 case -EPIPE: /* stall, or disconnect from TT */
470 /* FIXME schedule work to clear the halt */
471 netif_warn(pegasus, rx_err, net, "no rx stall recovery\n");
476 netif_dbg(pegasus, ifdown, net, "rx unlink, %d\n", status);
479 netif_dbg(pegasus, rx_err, net, "RX status %d\n", status);
486 rx_status = buf[count - 2];
487 if (rx_status & 0x1e) {
488 netif_dbg(pegasus, rx_err, net,
489 "RX packet error %x\n", rx_status);
490 net->stats.rx_errors++;
491 if (rx_status & 0x06) /* long or runt */
492 net->stats.rx_length_errors++;
493 if (rx_status & 0x08)
494 net->stats.rx_crc_errors++;
495 if (rx_status & 0x10) /* extra bits */
496 net->stats.rx_frame_errors++;
499 if (pegasus->chip == 0x8513) {
500 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
502 pegasus->rx_skb->data += 2;
504 pkt_len = buf[count - 3] << 8;
505 pkt_len += buf[count - 4];
511 * If the packet is unreasonably long, quietly drop it rather than
512 * kernel panicing by calling skb_put.
514 if (pkt_len > PEGASUS_MTU)
518 * at this point we are sure pegasus->rx_skb != NULL
519 * so we go ahead and pass up the packet.
521 skb_put(pegasus->rx_skb, pkt_len);
522 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
523 netif_rx(pegasus->rx_skb);
524 net->stats.rx_packets++;
525 net->stats.rx_bytes += pkt_len;
527 if (pegasus->flags & PEGASUS_UNPLUG)
530 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net, PEGASUS_MTU,
533 if (pegasus->rx_skb == NULL)
536 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
537 usb_rcvbulkpipe(pegasus->usb, 1),
538 pegasus->rx_skb->data, PEGASUS_MTU,
539 read_bulk_callback, pegasus);
540 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
541 if (rx_status == -ENODEV)
542 netif_device_detach(pegasus->net);
543 else if (rx_status) {
544 pegasus->flags |= PEGASUS_RX_URB_FAIL;
547 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
553 tasklet_schedule(&pegasus->rx_tl);
556 static void rx_fixup(struct tasklet_struct *t)
558 pegasus_t *pegasus = from_tasklet(pegasus, t, rx_tl);
561 if (pegasus->flags & PEGASUS_UNPLUG)
564 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
567 if (pegasus->rx_skb == NULL)
568 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
571 if (pegasus->rx_skb == NULL) {
572 netif_warn(pegasus, rx_err, pegasus->net, "low on memory\n");
573 tasklet_schedule(&pegasus->rx_tl);
576 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
577 usb_rcvbulkpipe(pegasus->usb, 1),
578 pegasus->rx_skb->data, PEGASUS_MTU,
579 read_bulk_callback, pegasus);
581 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
582 if (status == -ENODEV)
583 netif_device_detach(pegasus->net);
585 pegasus->flags |= PEGASUS_RX_URB_FAIL;
586 tasklet_schedule(&pegasus->rx_tl);
588 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
592 static void write_bulk_callback(struct urb *urb)
594 pegasus_t *pegasus = urb->context;
595 struct net_device *net;
596 int status = urb->status;
603 if (!netif_device_present(net) || !netif_running(net))
608 /* FIXME schedule_work() to clear the tx halt */
609 netif_stop_queue(net);
610 netif_warn(pegasus, tx_err, net, "no tx stall recovery\n");
615 netif_dbg(pegasus, ifdown, net, "tx unlink, %d\n", status);
618 netif_info(pegasus, tx_err, net, "TX status %d\n", status);
624 netif_trans_update(net); /* prevent tx timeout */
625 netif_wake_queue(net);
628 static void intr_callback(struct urb *urb)
630 pegasus_t *pegasus = urb->context;
631 struct net_device *net;
632 int res, status = urb->status;
641 case -ECONNRESET: /* unlink */
646 /* some Pegasus-I products report LOTS of data
647 * toggle errors... avoid log spamming
649 netif_dbg(pegasus, timer, net, "intr status %d\n", status);
652 if (urb->actual_length >= 6) {
653 u8 *d = urb->transfer_buffer;
655 /* byte 0 == tx_status1, reg 2B */
656 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
657 |LATE_COL|JABBER_TIMEOUT)) {
658 net->stats.tx_errors++;
659 if (d[0] & TX_UNDERRUN)
660 net->stats.tx_fifo_errors++;
661 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
662 net->stats.tx_aborted_errors++;
664 net->stats.tx_window_errors++;
667 /* d[5].LINK_STATUS lies on some adapters.
668 * d[0].NO_CARRIER kicks in only with failed TX.
669 * ... so monitoring with MII may be safest.
672 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
673 net->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
676 res = usb_submit_urb(urb, GFP_ATOMIC);
678 netif_device_detach(pegasus->net);
680 netif_err(pegasus, timer, net,
681 "can't resubmit interrupt urb, %d\n", res);
684 static void pegasus_tx_timeout(struct net_device *net, unsigned int txqueue)
686 pegasus_t *pegasus = netdev_priv(net);
687 netif_warn(pegasus, timer, net, "tx timeout\n");
688 usb_unlink_urb(pegasus->tx_urb);
689 net->stats.tx_errors++;
692 static netdev_tx_t pegasus_start_xmit(struct sk_buff *skb,
693 struct net_device *net)
695 pegasus_t *pegasus = netdev_priv(net);
696 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
698 __u16 l16 = skb->len;
700 netif_stop_queue(net);
702 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
703 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
704 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
705 usb_sndbulkpipe(pegasus->usb, 2),
706 pegasus->tx_buff, count,
707 write_bulk_callback, pegasus);
708 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
709 netif_warn(pegasus, tx_err, net, "fail tx, %d\n", res);
711 case -EPIPE: /* stall, or disconnect from TT */
712 /* cleanup should already have been scheduled */
714 case -ENODEV: /* disconnect() upcoming */
716 netif_device_detach(pegasus->net);
719 net->stats.tx_errors++;
720 netif_start_queue(net);
723 net->stats.tx_packets++;
724 net->stats.tx_bytes += skb->len;
731 static inline void disable_net_traffic(pegasus_t *pegasus)
733 __le16 tmp = cpu_to_le16(0);
735 set_registers(pegasus, EthCtrl0, sizeof(tmp), &tmp);
738 static inline void get_interrupt_interval(pegasus_t *pegasus)
743 read_eprom_word(pegasus, 4, &data);
744 interval = data >> 8;
745 if (pegasus->usb->speed != USB_SPEED_HIGH) {
746 if (interval < 0x80) {
747 netif_info(pegasus, timer, pegasus->net,
748 "intr interval changed from %ums to %ums\n",
751 data = (data & 0x00FF) | ((u16)interval << 8);
752 #ifdef PEGASUS_WRITE_EEPROM
753 write_eprom_word(pegasus, 4, data);
757 pegasus->intr_interval = interval;
760 static void set_carrier(struct net_device *net)
762 pegasus_t *pegasus = netdev_priv(net);
765 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
768 if (tmp & BMSR_LSTATUS)
769 netif_carrier_on(net);
771 netif_carrier_off(net);
774 static void free_all_urbs(pegasus_t *pegasus)
776 usb_free_urb(pegasus->intr_urb);
777 usb_free_urb(pegasus->tx_urb);
778 usb_free_urb(pegasus->rx_urb);
781 static void unlink_all_urbs(pegasus_t *pegasus)
783 usb_kill_urb(pegasus->intr_urb);
784 usb_kill_urb(pegasus->tx_urb);
785 usb_kill_urb(pegasus->rx_urb);
788 static int alloc_urbs(pegasus_t *pegasus)
792 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
793 if (!pegasus->rx_urb) {
796 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
797 if (!pegasus->tx_urb) {
798 usb_free_urb(pegasus->rx_urb);
801 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
802 if (!pegasus->intr_urb) {
803 usb_free_urb(pegasus->tx_urb);
804 usb_free_urb(pegasus->rx_urb);
811 static int pegasus_open(struct net_device *net)
813 pegasus_t *pegasus = netdev_priv(net);
816 if (pegasus->rx_skb == NULL)
817 pegasus->rx_skb = __netdev_alloc_skb_ip_align(pegasus->net,
820 if (!pegasus->rx_skb)
823 res = set_registers(pegasus, EthID, 6, net->dev_addr);
825 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
826 usb_rcvbulkpipe(pegasus->usb, 1),
827 pegasus->rx_skb->data, PEGASUS_MTU,
828 read_bulk_callback, pegasus);
829 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
831 netif_device_detach(pegasus->net);
832 netif_dbg(pegasus, ifup, net, "failed rx_urb, %d\n", res);
836 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
837 usb_rcvintpipe(pegasus->usb, 3),
838 pegasus->intr_buff, sizeof(pegasus->intr_buff),
839 intr_callback, pegasus, pegasus->intr_interval);
840 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
842 netif_device_detach(pegasus->net);
843 netif_dbg(pegasus, ifup, net, "failed intr_urb, %d\n", res);
844 usb_kill_urb(pegasus->rx_urb);
847 res = enable_net_traffic(net, pegasus->usb);
849 netif_dbg(pegasus, ifup, net,
850 "can't enable_net_traffic() - %d\n", res);
852 usb_kill_urb(pegasus->rx_urb);
853 usb_kill_urb(pegasus->intr_urb);
857 netif_start_queue(net);
858 netif_dbg(pegasus, ifup, net, "open\n");
864 static int pegasus_close(struct net_device *net)
866 pegasus_t *pegasus = netdev_priv(net);
868 netif_stop_queue(net);
869 if (!(pegasus->flags & PEGASUS_UNPLUG))
870 disable_net_traffic(pegasus);
871 tasklet_kill(&pegasus->rx_tl);
872 unlink_all_urbs(pegasus);
877 static void pegasus_get_drvinfo(struct net_device *dev,
878 struct ethtool_drvinfo *info)
880 pegasus_t *pegasus = netdev_priv(dev);
882 strlcpy(info->driver, driver_name, sizeof(info->driver));
883 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
884 usb_make_path(pegasus->usb, info->bus_info, sizeof(info->bus_info));
887 /* also handles three patterns of some kind in hardware */
888 #define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
891 pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
893 pegasus_t *pegasus = netdev_priv(dev);
895 wol->supported = WAKE_MAGIC | WAKE_PHY;
896 wol->wolopts = pegasus->wolopts;
900 pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
902 pegasus_t *pegasus = netdev_priv(dev);
906 if (wol->wolopts & ~WOL_SUPPORTED)
909 if (wol->wolopts & WAKE_MAGIC)
911 if (wol->wolopts & WAKE_PHY)
913 /* FIXME this 0x10 bit still needs to get set in the chip... */
915 pegasus->eth_regs[0] |= 0x10;
917 pegasus->eth_regs[0] &= ~0x10;
918 pegasus->wolopts = wol->wolopts;
920 ret = set_register(pegasus, WakeupControl, reg78);
922 ret = device_set_wakeup_enable(&pegasus->usb->dev,
927 static inline void pegasus_reset_wol(struct net_device *dev)
929 struct ethtool_wolinfo wol;
931 memset(&wol, 0, sizeof wol);
932 (void) pegasus_set_wol(dev, &wol);
936 pegasus_get_link_ksettings(struct net_device *dev,
937 struct ethtool_link_ksettings *ecmd)
941 pegasus = netdev_priv(dev);
942 mii_ethtool_get_link_ksettings(&pegasus->mii, ecmd);
947 pegasus_set_link_ksettings(struct net_device *dev,
948 const struct ethtool_link_ksettings *ecmd)
950 pegasus_t *pegasus = netdev_priv(dev);
951 return mii_ethtool_set_link_ksettings(&pegasus->mii, ecmd);
954 static int pegasus_nway_reset(struct net_device *dev)
956 pegasus_t *pegasus = netdev_priv(dev);
957 return mii_nway_restart(&pegasus->mii);
960 static u32 pegasus_get_link(struct net_device *dev)
962 pegasus_t *pegasus = netdev_priv(dev);
963 return mii_link_ok(&pegasus->mii);
966 static u32 pegasus_get_msglevel(struct net_device *dev)
968 pegasus_t *pegasus = netdev_priv(dev);
969 return pegasus->msg_enable;
972 static void pegasus_set_msglevel(struct net_device *dev, u32 v)
974 pegasus_t *pegasus = netdev_priv(dev);
975 pegasus->msg_enable = v;
978 static const struct ethtool_ops ops = {
979 .get_drvinfo = pegasus_get_drvinfo,
980 .nway_reset = pegasus_nway_reset,
981 .get_link = pegasus_get_link,
982 .get_msglevel = pegasus_get_msglevel,
983 .set_msglevel = pegasus_set_msglevel,
984 .get_wol = pegasus_get_wol,
985 .set_wol = pegasus_set_wol,
986 .get_link_ksettings = pegasus_get_link_ksettings,
987 .set_link_ksettings = pegasus_set_link_ksettings,
990 static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
992 __u16 *data = (__u16 *) &rq->ifr_ifru;
993 pegasus_t *pegasus = netdev_priv(net);
998 data[0] = pegasus->phy;
1000 case SIOCDEVPRIVATE + 1:
1001 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1004 case SIOCDEVPRIVATE + 2:
1005 if (!capable(CAP_NET_ADMIN))
1007 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, &data[2]);
1016 static void pegasus_set_multicast(struct net_device *net)
1018 pegasus_t *pegasus = netdev_priv(net);
1020 if (net->flags & IFF_PROMISC) {
1021 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1022 netif_info(pegasus, link, net, "Promiscuous mode enabled\n");
1023 } else if (!netdev_mc_empty(net) || (net->flags & IFF_ALLMULTI)) {
1024 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1025 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1026 netif_dbg(pegasus, link, net, "set allmulti\n");
1028 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1029 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1031 update_eth_regs_async(pegasus);
1034 static __u8 mii_phy_probe(pegasus_t *pegasus)
1039 for (i = 0; i < 32; i++) {
1040 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1041 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1050 static inline void setup_pegasus_II(pegasus_t *pegasus)
1054 set_register(pegasus, Reg1d, 0);
1055 set_register(pegasus, Reg7b, 1);
1057 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1058 set_register(pegasus, Reg7b, 0);
1060 set_register(pegasus, Reg7b, 2);
1062 set_register(pegasus, 0x83, data);
1063 get_registers(pegasus, 0x83, 1, &data);
1066 pegasus->chip = 0x8513;
1070 set_register(pegasus, 0x80, 0xc0);
1071 set_register(pegasus, 0x83, 0xff);
1072 set_register(pegasus, 0x84, 0x01);
1074 if (pegasus->features & HAS_HOME_PNA && mii_mode)
1075 set_register(pegasus, Reg81, 6);
1077 set_register(pegasus, Reg81, 2);
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(system_long_wq, &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 static int pegasus_probe(struct usb_interface *intf,
1107 const struct usb_device_id *id)
1109 struct usb_device *dev = interface_to_usbdev(intf);
1110 struct net_device *net;
1112 int dev_index = id - pegasus_ids;
1115 if (pegasus_blacklisted(dev))
1118 net = alloc_etherdev(sizeof(struct pegasus));
1122 pegasus = netdev_priv(net);
1123 pegasus->dev_index = dev_index;
1125 res = alloc_urbs(pegasus);
1127 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1131 tasklet_setup(&pegasus->rx_tl, rx_fixup);
1133 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
1135 pegasus->intf = intf;
1140 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1141 net->netdev_ops = &pegasus_netdev_ops;
1142 net->ethtool_ops = &ops;
1143 pegasus->mii.dev = net;
1144 pegasus->mii.mdio_read = mdio_read;
1145 pegasus->mii.mdio_write = mdio_write;
1146 pegasus->mii.phy_id_mask = 0x1f;
1147 pegasus->mii.reg_num_mask = 0x1f;
1148 pegasus->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
1149 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1151 pegasus->features = usb_dev_id[dev_index].private;
1152 get_interrupt_interval(pegasus);
1153 if (reset_mac(pegasus)) {
1154 dev_err(&intf->dev, "can't reset MAC\n");
1158 set_ethernet_addr(pegasus);
1159 if (pegasus->features & PEGASUS_II) {
1160 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1161 setup_pegasus_II(pegasus);
1163 pegasus->phy = mii_phy_probe(pegasus);
1164 if (pegasus->phy == 0xff) {
1165 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1168 pegasus->mii.phy_id = pegasus->phy;
1169 usb_set_intfdata(intf, pegasus);
1170 SET_NETDEV_DEV(net, &intf->dev);
1171 pegasus_reset_wol(net);
1172 res = register_netdev(net);
1175 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
1176 CARRIER_CHECK_DELAY);
1177 dev_info(&intf->dev, "%s, %s, %pM\n", net->name,
1178 usb_dev_id[dev_index].name, net->dev_addr);
1182 usb_set_intfdata(intf, NULL);
1184 free_all_urbs(pegasus);
1191 static void pegasus_disconnect(struct usb_interface *intf)
1193 struct pegasus *pegasus = usb_get_intfdata(intf);
1195 usb_set_intfdata(intf, NULL);
1197 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1201 pegasus->flags |= PEGASUS_UNPLUG;
1202 cancel_delayed_work_sync(&pegasus->carrier_check);
1203 unregister_netdev(pegasus->net);
1204 unlink_all_urbs(pegasus);
1205 free_all_urbs(pegasus);
1206 if (pegasus->rx_skb != NULL) {
1207 dev_kfree_skb(pegasus->rx_skb);
1208 pegasus->rx_skb = NULL;
1210 free_netdev(pegasus->net);
1213 static int pegasus_suspend(struct usb_interface *intf, pm_message_t message)
1215 struct pegasus *pegasus = usb_get_intfdata(intf);
1217 netif_device_detach(pegasus->net);
1218 cancel_delayed_work_sync(&pegasus->carrier_check);
1219 if (netif_running(pegasus->net)) {
1220 usb_kill_urb(pegasus->rx_urb);
1221 usb_kill_urb(pegasus->intr_urb);
1226 static int pegasus_resume(struct usb_interface *intf)
1228 struct pegasus *pegasus = usb_get_intfdata(intf);
1230 netif_device_attach(pegasus->net);
1231 if (netif_running(pegasus->net)) {
1232 pegasus->rx_urb->status = 0;
1233 pegasus->rx_urb->actual_length = 0;
1234 read_bulk_callback(pegasus->rx_urb);
1236 pegasus->intr_urb->status = 0;
1237 pegasus->intr_urb->actual_length = 0;
1238 intr_callback(pegasus->intr_urb);
1240 queue_delayed_work(system_long_wq, &pegasus->carrier_check,
1241 CARRIER_CHECK_DELAY);
1245 static const struct net_device_ops pegasus_netdev_ops = {
1246 .ndo_open = pegasus_open,
1247 .ndo_stop = pegasus_close,
1248 .ndo_do_ioctl = pegasus_ioctl,
1249 .ndo_start_xmit = pegasus_start_xmit,
1250 .ndo_set_rx_mode = pegasus_set_multicast,
1251 .ndo_tx_timeout = pegasus_tx_timeout,
1252 .ndo_set_mac_address = eth_mac_addr,
1253 .ndo_validate_addr = eth_validate_addr,
1256 static struct usb_driver pegasus_driver = {
1257 .name = driver_name,
1258 .probe = pegasus_probe,
1259 .disconnect = pegasus_disconnect,
1260 .id_table = pegasus_ids,
1261 .suspend = pegasus_suspend,
1262 .resume = pegasus_resume,
1263 .disable_hub_initiated_lpm = 1,
1266 static void __init parse_id(char *id)
1268 unsigned int vendor_id = 0, device_id = 0, flags = 0, i = 0;
1269 char *token, *name = NULL;
1271 if ((token = strsep(&id, ":")) != NULL)
1273 /* name now points to a null terminated string*/
1274 if ((token = strsep(&id, ":")) != NULL)
1275 vendor_id = simple_strtoul(token, NULL, 16);
1276 if ((token = strsep(&id, ":")) != NULL)
1277 device_id = simple_strtoul(token, NULL, 16);
1278 flags = simple_strtoul(id, NULL, 16);
1279 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1280 driver_name, name, vendor_id, device_id, flags);
1282 if (vendor_id > 0x10000 || vendor_id == 0)
1284 if (device_id > 0x10000 || device_id == 0)
1287 for (i = 0; usb_dev_id[i].name; i++);
1288 usb_dev_id[i].name = name;
1289 usb_dev_id[i].vendor = vendor_id;
1290 usb_dev_id[i].device = device_id;
1291 usb_dev_id[i].private = flags;
1292 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1293 pegasus_ids[i].idVendor = vendor_id;
1294 pegasus_ids[i].idProduct = device_id;
1297 static int __init pegasus_init(void)
1299 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
1302 return usb_register(&pegasus_driver);
1305 static void __exit pegasus_exit(void)
1307 usb_deregister(&pegasus_driver);
1310 module_init(pegasus_init);
1311 module_exit(pegasus_exit);