2 * Copyright (c) 1999-2005 Petko Manolov (petkan@users.sourceforge.net)
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.
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 <asm/uaccess.h>
48 #define DRIVER_VERSION "v0.6.14 (2006/09/27)"
49 #define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
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)
58 static int loopback = 0;
59 static int mii_mode = 0;
60 static char *devid=NULL;
62 static struct usb_eth_dev usb_dev_id[] = {
63 #define PEGASUS_DEV(pn, vid, pid, flags) \
64 {.name = pn, .vendor = vid, .device = pid, .private = flags},
71 static struct usb_device_id pegasus_ids[] = {
72 #define PEGASUS_DEV(pn, vid, pid, flags) \
73 {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
80 MODULE_AUTHOR(DRIVER_AUTHOR);
81 MODULE_DESCRIPTION(DRIVER_DESC);
82 MODULE_LICENSE("GPL");
83 module_param(loopback, bool, 0);
84 module_param(mii_mode, bool, 0);
85 module_param(devid, charp, 0);
86 MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
87 MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
88 MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
90 /* use ethtool to change the level for any given device */
91 static int msg_level = -1;
92 module_param (msg_level, int, 0);
93 MODULE_PARM_DESC (msg_level, "Override default message level");
95 MODULE_DEVICE_TABLE(usb, pegasus_ids);
97 static int update_eth_regs_async(pegasus_t *);
98 /* Aargh!!! I _really_ hate such tweaks */
99 static void ctrl_callback(struct urb *urb)
101 pegasus_t *pegasus = urb->context;
106 switch (urb->status) {
108 if (pegasus->flags & ETH_REGS_CHANGE) {
109 pegasus->flags &= ~ETH_REGS_CHANGE;
110 pegasus->flags |= ETH_REGS_CHANGED;
111 update_eth_regs_async(pegasus);
120 if (netif_msg_drv(pegasus))
121 dev_dbg(&pegasus->intf->dev, "%s, status %d\n",
122 __FUNCTION__, urb->status);
124 pegasus->flags &= ~ETH_REGS_CHANGED;
125 wake_up(&pegasus->ctrl_wait);
128 static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
133 DECLARE_WAITQUEUE(wait, current);
135 buffer = kmalloc(size, GFP_KERNEL);
137 if (netif_msg_drv(pegasus))
138 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
142 add_wait_queue(&pegasus->ctrl_wait, &wait);
143 set_current_state(TASK_UNINTERRUPTIBLE);
144 while (pegasus->flags & ETH_REGS_CHANGED)
146 remove_wait_queue(&pegasus->ctrl_wait, &wait);
147 set_current_state(TASK_RUNNING);
149 pegasus->dr.bRequestType = PEGASUS_REQT_READ;
150 pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
151 pegasus->dr.wValue = cpu_to_le16(0);
152 pegasus->dr.wIndex = cpu_to_le16p(&indx);
153 pegasus->dr.wLength = cpu_to_le16p(&size);
154 pegasus->ctrl_urb->transfer_buffer_length = size;
156 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
157 usb_rcvctrlpipe(pegasus->usb, 0),
158 (char *) &pegasus->dr,
159 buffer, size, ctrl_callback, pegasus);
161 add_wait_queue(&pegasus->ctrl_wait, &wait);
162 set_current_state(TASK_UNINTERRUPTIBLE);
164 /* using ATOMIC, we'd never wake up if we slept */
165 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
166 set_current_state(TASK_RUNNING);
168 netif_device_detach(pegasus->net);
169 if (netif_msg_drv(pegasus))
170 dev_err(&pegasus->intf->dev, "%s, status %d\n",
177 remove_wait_queue(&pegasus->ctrl_wait, &wait);
178 memcpy(data, buffer, size);
184 static int set_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
189 DECLARE_WAITQUEUE(wait, current);
191 buffer = kmalloc(size, GFP_KERNEL);
193 if (netif_msg_drv(pegasus))
194 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
198 memcpy(buffer, data, size);
200 add_wait_queue(&pegasus->ctrl_wait, &wait);
201 set_current_state(TASK_UNINTERRUPTIBLE);
202 while (pegasus->flags & ETH_REGS_CHANGED)
204 remove_wait_queue(&pegasus->ctrl_wait, &wait);
205 set_current_state(TASK_RUNNING);
207 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
208 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
209 pegasus->dr.wValue = cpu_to_le16(0);
210 pegasus->dr.wIndex = cpu_to_le16p(&indx);
211 pegasus->dr.wLength = cpu_to_le16p(&size);
212 pegasus->ctrl_urb->transfer_buffer_length = size;
214 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
215 usb_sndctrlpipe(pegasus->usb, 0),
216 (char *) &pegasus->dr,
217 buffer, size, ctrl_callback, pegasus);
219 add_wait_queue(&pegasus->ctrl_wait, &wait);
220 set_current_state(TASK_UNINTERRUPTIBLE);
222 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
224 netif_device_detach(pegasus->net);
225 if (netif_msg_drv(pegasus))
226 dev_err(&pegasus->intf->dev, "%s, status %d\n",
233 remove_wait_queue(&pegasus->ctrl_wait, &wait);
239 static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
243 DECLARE_WAITQUEUE(wait, current);
245 tmp = kmalloc(1, GFP_KERNEL);
247 if (netif_msg_drv(pegasus))
248 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
252 memcpy(tmp, &data, 1);
253 add_wait_queue(&pegasus->ctrl_wait, &wait);
254 set_current_state(TASK_UNINTERRUPTIBLE);
255 while (pegasus->flags & ETH_REGS_CHANGED)
257 remove_wait_queue(&pegasus->ctrl_wait, &wait);
258 set_current_state(TASK_RUNNING);
260 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
261 pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
262 pegasus->dr.wValue = cpu_to_le16(data);
263 pegasus->dr.wIndex = cpu_to_le16p(&indx);
264 pegasus->dr.wLength = cpu_to_le16(1);
265 pegasus->ctrl_urb->transfer_buffer_length = 1;
267 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
268 usb_sndctrlpipe(pegasus->usb, 0),
269 (char *) &pegasus->dr,
270 tmp, 1, ctrl_callback, pegasus);
272 add_wait_queue(&pegasus->ctrl_wait, &wait);
273 set_current_state(TASK_UNINTERRUPTIBLE);
275 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
277 netif_device_detach(pegasus->net);
278 if (netif_msg_drv(pegasus))
279 dev_err(&pegasus->intf->dev, "%s, status %d\n",
286 remove_wait_queue(&pegasus->ctrl_wait, &wait);
292 static int update_eth_regs_async(pegasus_t * pegasus)
296 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
297 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
298 pegasus->dr.wValue = 0;
299 pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
300 pegasus->dr.wLength = cpu_to_le16(3);
301 pegasus->ctrl_urb->transfer_buffer_length = 3;
303 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
304 usb_sndctrlpipe(pegasus->usb, 0),
305 (char *) &pegasus->dr,
306 pegasus->eth_regs, 3, ctrl_callback, pegasus);
308 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
310 netif_device_detach(pegasus->net);
311 if (netif_msg_drv(pegasus))
312 dev_err(&pegasus->intf->dev, "%s, status %d\n",
319 /* Returns 0 on success, error on failure */
320 static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
323 __u8 data[4] = { phy, 0, 0, indx };
327 set_register(pegasus, PhyCtrl, 0);
328 set_registers(pegasus, PhyAddr, sizeof (data), data);
329 set_register(pegasus, PhyCtrl, (indx | PHY_READ));
330 for (i = 0; i < REG_TIMEOUT; i++) {
331 ret = get_registers(pegasus, PhyCtrl, 1, data);
332 if (ret == -ESHUTDOWN)
334 if (data[0] & PHY_DONE)
337 if (i < REG_TIMEOUT) {
338 ret = get_registers(pegasus, PhyData, 2, ®di);
339 *regd = le16_to_cpu(regdi);
343 if (netif_msg_drv(pegasus))
344 dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
349 static int mdio_read(struct net_device *dev, int phy_id, int loc)
351 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
354 read_mii_word(pegasus, phy_id, loc, &res);
358 static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
361 __u8 data[4] = { phy, 0, 0, indx };
365 data[2] = (u8) (regd >> 8);
366 set_register(pegasus, PhyCtrl, 0);
367 set_registers(pegasus, PhyAddr, sizeof(data), data);
368 set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
369 for (i = 0; i < REG_TIMEOUT; i++) {
370 ret = get_registers(pegasus, PhyCtrl, 1, data);
371 if (ret == -ESHUTDOWN)
373 if (data[0] & PHY_DONE)
380 if (netif_msg_drv(pegasus))
381 dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
385 static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
387 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
389 write_mii_word(pegasus, phy_id, loc, val);
392 static int read_eprom_word(pegasus_t * pegasus, __u8 index, __u16 * retdata)
399 set_register(pegasus, EpromCtrl, 0);
400 set_register(pegasus, EpromOffset, index);
401 set_register(pegasus, EpromCtrl, EPROM_READ);
403 for (i = 0; i < REG_TIMEOUT; i++) {
404 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
405 if (tmp & EPROM_DONE)
407 if (ret == -ESHUTDOWN)
410 if (i < REG_TIMEOUT) {
411 ret = get_registers(pegasus, EpromData, 2, &retdatai);
412 *retdata = le16_to_cpu(retdatai);
417 if (netif_msg_drv(pegasus))
418 dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
422 #ifdef PEGASUS_WRITE_EEPROM
423 static inline void enable_eprom_write(pegasus_t * pegasus)
428 get_registers(pegasus, EthCtrl2, 1, &tmp);
429 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
432 static inline void disable_eprom_write(pegasus_t * pegasus)
437 get_registers(pegasus, EthCtrl2, 1, &tmp);
438 set_register(pegasus, EpromCtrl, 0);
439 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
442 static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
445 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
448 set_registers(pegasus, EpromOffset, 4, d);
449 enable_eprom_write(pegasus);
450 set_register(pegasus, EpromOffset, index);
451 set_registers(pegasus, EpromData, 2, &data);
452 set_register(pegasus, EpromCtrl, EPROM_WRITE);
454 for (i = 0; i < REG_TIMEOUT; i++) {
455 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
456 if (ret == -ESHUTDOWN)
458 if (tmp & EPROM_DONE)
461 disable_eprom_write(pegasus);
465 if (netif_msg_drv(pegasus))
466 dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
469 #endif /* PEGASUS_WRITE_EEPROM */
471 static inline void get_node_id(pegasus_t * pegasus, __u8 * id)
476 for (i = 0; i < 3; i++) {
477 read_eprom_word(pegasus, i, &w16);
478 ((__le16 *) id)[i] = cpu_to_le16p(&w16);
482 static void set_ethernet_addr(pegasus_t * pegasus)
486 if (pegasus->features & PEGASUS_II) {
487 get_registers(pegasus, 0x10, sizeof(node_id), node_id);
489 get_node_id(pegasus, node_id);
490 set_registers(pegasus, EthID, sizeof (node_id), node_id);
492 memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
495 static inline int reset_mac(pegasus_t * pegasus)
500 set_register(pegasus, EthCtrl1, data);
501 for (i = 0; i < REG_TIMEOUT; i++) {
502 get_registers(pegasus, EthCtrl1, 1, &data);
506 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
507 set_register(pegasus, Gpio1, 0x34);
509 set_register(pegasus, Gpio1, 0x26);
510 set_register(pegasus, Gpio0, pegasus->features);
511 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
515 if (i == REG_TIMEOUT)
518 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
519 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
520 set_register(pegasus, Gpio0, 0x24);
521 set_register(pegasus, Gpio0, 0x26);
523 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
525 read_mii_word(pegasus, 3, 0x1b, &auxmode);
526 write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
532 static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
536 pegasus_t *pegasus = netdev_priv(dev);
539 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
542 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
543 data[1] |= 0x20; /* set full duplex */
544 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
545 data[1] |= 0x10; /* set 100 Mbps */
548 data[2] = (loopback & 1) ? 0x09 : 0x01;
550 memcpy(pegasus->eth_regs, data, sizeof (data));
551 ret = set_registers(pegasus, EthCtrl0, 3, data);
553 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
554 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
555 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
557 read_mii_word(pegasus, 0, 0x1b, &auxmode);
558 write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
564 static void fill_skb_pool(pegasus_t * pegasus)
568 for (i = 0; i < RX_SKBS; i++) {
569 if (pegasus->rx_pool[i])
571 pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
573 ** we give up if the allocation fail. the tasklet will be
574 ** rescheduled again anyway...
576 if (pegasus->rx_pool[i] == NULL)
578 skb_reserve(pegasus->rx_pool[i], 2);
582 static void free_skb_pool(pegasus_t * pegasus)
586 for (i = 0; i < RX_SKBS; i++) {
587 if (pegasus->rx_pool[i]) {
588 dev_kfree_skb(pegasus->rx_pool[i]);
589 pegasus->rx_pool[i] = NULL;
594 static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
599 for (i = 0; i < RX_SKBS; i++) {
600 if (likely(pegasus->rx_pool[i] != NULL)) {
601 skb = pegasus->rx_pool[i];
602 pegasus->rx_pool[i] = NULL;
609 static void read_bulk_callback(struct urb *urb)
611 pegasus_t *pegasus = urb->context;
612 struct net_device *net;
613 int rx_status, count = urb->actual_length;
614 u8 *buf = urb->transfer_buffer;
621 if (!netif_device_present(net) || !netif_running(net))
624 switch (urb->status) {
628 if (netif_msg_rx_err(pegasus))
629 pr_debug("%s: reset MAC\n", net->name);
630 pegasus->flags &= ~PEGASUS_RX_BUSY;
632 case -EPIPE: /* stall, or disconnect from TT */
633 /* FIXME schedule work to clear the halt */
634 if (netif_msg_rx_err(pegasus))
635 printk(KERN_WARNING "%s: no rx stall recovery\n",
641 if (netif_msg_ifdown(pegasus))
642 pr_debug("%s: rx unlink, %d\n", net->name, urb->status);
645 if (netif_msg_rx_err(pegasus))
646 pr_debug("%s: RX status %d\n", net->name, urb->status);
650 if (!count || count < 4)
653 rx_status = buf[count - 2];
654 if (rx_status & 0x1e) {
655 if (netif_msg_rx_err(pegasus))
656 pr_debug("%s: RX packet error %x\n",
657 net->name, rx_status);
658 pegasus->stats.rx_errors++;
659 if (rx_status & 0x06) // long or runt
660 pegasus->stats.rx_length_errors++;
661 if (rx_status & 0x08)
662 pegasus->stats.rx_crc_errors++;
663 if (rx_status & 0x10) // extra bits
664 pegasus->stats.rx_frame_errors++;
667 if (pegasus->chip == 0x8513) {
668 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
670 pegasus->rx_skb->data += 2;
672 pkt_len = buf[count - 3] << 8;
673 pkt_len += buf[count - 4];
679 * If the packet is unreasonably long, quietly drop it rather than
680 * kernel panicing by calling skb_put.
682 if (pkt_len > PEGASUS_MTU)
686 * at this point we are sure pegasus->rx_skb != NULL
687 * so we go ahead and pass up the packet.
689 skb_put(pegasus->rx_skb, pkt_len);
690 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
691 netif_rx(pegasus->rx_skb);
692 pegasus->stats.rx_packets++;
693 pegasus->stats.rx_bytes += pkt_len;
695 if (pegasus->flags & PEGASUS_UNPLUG)
698 spin_lock(&pegasus->rx_pool_lock);
699 pegasus->rx_skb = pull_skb(pegasus);
700 spin_unlock(&pegasus->rx_pool_lock);
702 if (pegasus->rx_skb == NULL)
705 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
706 usb_rcvbulkpipe(pegasus->usb, 1),
707 pegasus->rx_skb->data, PEGASUS_MTU + 8,
708 read_bulk_callback, pegasus);
709 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
710 if (rx_status == -ENODEV)
711 netif_device_detach(pegasus->net);
712 else if (rx_status) {
713 pegasus->flags |= PEGASUS_RX_URB_FAIL;
716 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
722 tasklet_schedule(&pegasus->rx_tl);
725 static void rx_fixup(unsigned long data)
731 pegasus = (pegasus_t *) data;
732 if (pegasus->flags & PEGASUS_UNPLUG)
735 spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
736 fill_skb_pool(pegasus);
737 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
740 if (pegasus->rx_skb == NULL) {
741 pegasus->rx_skb = pull_skb(pegasus);
743 if (pegasus->rx_skb == NULL) {
744 if (netif_msg_rx_err(pegasus))
745 printk(KERN_WARNING "%s: low on memory\n",
747 tasklet_schedule(&pegasus->rx_tl);
750 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
751 usb_rcvbulkpipe(pegasus->usb, 1),
752 pegasus->rx_skb->data, PEGASUS_MTU + 8,
753 read_bulk_callback, pegasus);
755 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
756 if (status == -ENODEV)
757 netif_device_detach(pegasus->net);
759 pegasus->flags |= PEGASUS_RX_URB_FAIL;
760 tasklet_schedule(&pegasus->rx_tl);
762 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
765 spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
768 static void write_bulk_callback(struct urb *urb)
770 pegasus_t *pegasus = urb->context;
771 struct net_device *net = pegasus->net;
776 if (!netif_device_present(net) || !netif_running(net))
779 switch (urb->status) {
781 /* FIXME schedule_work() to clear the tx halt */
782 netif_stop_queue(net);
783 if (netif_msg_tx_err(pegasus))
784 printk(KERN_WARNING "%s: no tx stall recovery\n",
790 if (netif_msg_ifdown(pegasus))
791 pr_debug("%s: tx unlink, %d\n", net->name, urb->status);
794 if (netif_msg_tx_err(pegasus))
795 pr_info("%s: TX status %d\n", net->name, urb->status);
801 net->trans_start = jiffies;
802 netif_wake_queue(net);
805 static void intr_callback(struct urb *urb)
807 pegasus_t *pegasus = urb->context;
808 struct net_device *net;
815 switch (urb->status) {
818 case -ECONNRESET: /* unlink */
823 /* some Pegasus-I products report LOTS of data
824 * toggle errors... avoid log spamming
826 if (netif_msg_timer(pegasus))
827 pr_debug("%s: intr status %d\n", net->name,
831 if (urb->actual_length >= 6) {
832 u8 * d = urb->transfer_buffer;
834 /* byte 0 == tx_status1, reg 2B */
835 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
836 |LATE_COL|JABBER_TIMEOUT)) {
837 pegasus->stats.tx_errors++;
838 if (d[0] & TX_UNDERRUN)
839 pegasus->stats.tx_fifo_errors++;
840 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
841 pegasus->stats.tx_aborted_errors++;
843 pegasus->stats.tx_window_errors++;
846 /* d[5].LINK_STATUS lies on some adapters.
847 * d[0].NO_CARRIER kicks in only with failed TX.
848 * ... so monitoring with MII may be safest.
850 if (pegasus->features & TRUST_LINK_STATUS) {
851 if (d[5] & LINK_STATUS)
852 netif_carrier_on(net);
854 netif_carrier_off(net);
856 /* Never set carrier _on_ based on ! NO_CARRIER */
857 if (d[0] & NO_CARRIER)
858 netif_carrier_off(net);
861 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
862 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
865 status = usb_submit_urb(urb, GFP_ATOMIC);
866 if (status == -ENODEV)
867 netif_device_detach(pegasus->net);
868 if (status && netif_msg_timer(pegasus))
869 printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
873 static void pegasus_tx_timeout(struct net_device *net)
875 pegasus_t *pegasus = netdev_priv(net);
876 if (netif_msg_timer(pegasus))
877 printk(KERN_WARNING "%s: tx timeout\n", net->name);
878 usb_unlink_urb(pegasus->tx_urb);
879 pegasus->stats.tx_errors++;
882 static int pegasus_start_xmit(struct sk_buff *skb, struct net_device *net)
884 pegasus_t *pegasus = netdev_priv(net);
885 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
887 __u16 l16 = skb->len;
889 netif_stop_queue(net);
891 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
892 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
893 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
894 usb_sndbulkpipe(pegasus->usb, 2),
895 pegasus->tx_buff, count,
896 write_bulk_callback, pegasus);
897 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
898 if (netif_msg_tx_err(pegasus))
899 printk(KERN_WARNING "%s: fail tx, %d\n",
902 case -EPIPE: /* stall, or disconnect from TT */
903 /* cleanup should already have been scheduled */
905 case -ENODEV: /* disconnect() upcoming */
906 netif_device_detach(pegasus->net);
909 pegasus->stats.tx_errors++;
910 netif_start_queue(net);
913 pegasus->stats.tx_packets++;
914 pegasus->stats.tx_bytes += skb->len;
915 net->trans_start = jiffies;
922 static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
924 return &((pegasus_t *) netdev_priv(dev))->stats;
927 static inline void disable_net_traffic(pegasus_t * pegasus)
931 set_registers(pegasus, EthCtrl0, 2, &tmp);
934 static inline void get_interrupt_interval(pegasus_t * pegasus)
938 read_eprom_word(pegasus, 4, (__u16 *) data);
939 if (pegasus->usb->speed != USB_SPEED_HIGH) {
940 if (data[1] < 0x80) {
941 if (netif_msg_timer(pegasus))
942 dev_info(&pegasus->intf->dev, "intr interval "
943 "changed from %ums to %ums\n",
946 #ifdef PEGASUS_WRITE_EEPROM
947 write_eprom_word(pegasus, 4, *(__u16 *) data);
951 pegasus->intr_interval = data[1];
954 static void set_carrier(struct net_device *net)
956 pegasus_t *pegasus = netdev_priv(net);
959 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
962 if (tmp & BMSR_LSTATUS)
963 netif_carrier_on(net);
965 netif_carrier_off(net);
968 static void free_all_urbs(pegasus_t * pegasus)
970 usb_free_urb(pegasus->intr_urb);
971 usb_free_urb(pegasus->tx_urb);
972 usb_free_urb(pegasus->rx_urb);
973 usb_free_urb(pegasus->ctrl_urb);
976 static void unlink_all_urbs(pegasus_t * pegasus)
978 usb_kill_urb(pegasus->intr_urb);
979 usb_kill_urb(pegasus->tx_urb);
980 usb_kill_urb(pegasus->rx_urb);
981 usb_kill_urb(pegasus->ctrl_urb);
984 static int alloc_urbs(pegasus_t * pegasus)
986 pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
987 if (!pegasus->ctrl_urb) {
990 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
991 if (!pegasus->rx_urb) {
992 usb_free_urb(pegasus->ctrl_urb);
995 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
996 if (!pegasus->tx_urb) {
997 usb_free_urb(pegasus->rx_urb);
998 usb_free_urb(pegasus->ctrl_urb);
1001 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1002 if (!pegasus->intr_urb) {
1003 usb_free_urb(pegasus->tx_urb);
1004 usb_free_urb(pegasus->rx_urb);
1005 usb_free_urb(pegasus->ctrl_urb);
1012 static int pegasus_open(struct net_device *net)
1014 pegasus_t *pegasus = netdev_priv(net);
1017 if (pegasus->rx_skb == NULL)
1018 pegasus->rx_skb = pull_skb(pegasus);
1020 ** Note: no point to free the pool. it is empty :-)
1022 if (!pegasus->rx_skb)
1025 res = set_registers(pegasus, EthID, 6, net->dev_addr);
1027 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
1028 usb_rcvbulkpipe(pegasus->usb, 1),
1029 pegasus->rx_skb->data, PEGASUS_MTU + 8,
1030 read_bulk_callback, pegasus);
1031 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
1033 netif_device_detach(pegasus->net);
1034 if (netif_msg_ifup(pegasus))
1035 pr_debug("%s: failed rx_urb, %d", net->name, res);
1039 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
1040 usb_rcvintpipe(pegasus->usb, 3),
1041 pegasus->intr_buff, sizeof (pegasus->intr_buff),
1042 intr_callback, pegasus, pegasus->intr_interval);
1043 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
1045 netif_device_detach(pegasus->net);
1046 if (netif_msg_ifup(pegasus))
1047 pr_debug("%s: failed intr_urb, %d\n", net->name, res);
1048 usb_kill_urb(pegasus->rx_urb);
1051 if ((res = enable_net_traffic(net, pegasus->usb))) {
1052 if (netif_msg_ifup(pegasus))
1053 pr_debug("%s: can't enable_net_traffic() - %d\n",
1056 usb_kill_urb(pegasus->rx_urb);
1057 usb_kill_urb(pegasus->intr_urb);
1058 free_skb_pool(pegasus);
1062 netif_start_queue(net);
1063 if (netif_msg_ifup(pegasus))
1064 pr_debug("%s: open\n", net->name);
1070 static int pegasus_close(struct net_device *net)
1072 pegasus_t *pegasus = netdev_priv(net);
1074 netif_stop_queue(net);
1075 if (!(pegasus->flags & PEGASUS_UNPLUG))
1076 disable_net_traffic(pegasus);
1077 tasklet_kill(&pegasus->rx_tl);
1078 unlink_all_urbs(pegasus);
1083 static void pegasus_get_drvinfo(struct net_device *dev,
1084 struct ethtool_drvinfo *info)
1086 pegasus_t *pegasus = netdev_priv(dev);
1087 strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
1088 strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
1089 usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
1092 /* also handles three patterns of some kind in hardware */
1093 #define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
1096 pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1098 pegasus_t *pegasus = netdev_priv(dev);
1100 wol->supported = WAKE_MAGIC | WAKE_PHY;
1101 wol->wolopts = pegasus->wolopts;
1105 pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1107 pegasus_t *pegasus = netdev_priv(dev);
1110 if (wol->wolopts & ~WOL_SUPPORTED)
1113 if (wol->wolopts & WAKE_MAGIC)
1115 if (wol->wolopts & WAKE_PHY)
1117 /* FIXME this 0x10 bit still needs to get set in the chip... */
1119 pegasus->eth_regs[0] |= 0x10;
1121 pegasus->eth_regs[0] &= ~0x10;
1122 pegasus->wolopts = wol->wolopts;
1123 return set_register(pegasus, WakeupControl, reg78);
1126 static inline void pegasus_reset_wol(struct net_device *dev)
1128 struct ethtool_wolinfo wol;
1130 memset(&wol, 0, sizeof wol);
1131 (void) pegasus_set_wol(dev, &wol);
1135 pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1142 pegasus = netdev_priv(dev);
1143 mii_ethtool_gset(&pegasus->mii, ecmd);
1149 pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1151 pegasus_t *pegasus = netdev_priv(dev);
1152 return mii_ethtool_sset(&pegasus->mii, ecmd);
1155 static int pegasus_nway_reset(struct net_device *dev)
1157 pegasus_t *pegasus = netdev_priv(dev);
1158 return mii_nway_restart(&pegasus->mii);
1161 static u32 pegasus_get_link(struct net_device *dev)
1163 pegasus_t *pegasus = netdev_priv(dev);
1164 return mii_link_ok(&pegasus->mii);
1167 static u32 pegasus_get_msglevel(struct net_device *dev)
1169 pegasus_t *pegasus = netdev_priv(dev);
1170 return pegasus->msg_enable;
1173 static void pegasus_set_msglevel(struct net_device *dev, u32 v)
1175 pegasus_t *pegasus = netdev_priv(dev);
1176 pegasus->msg_enable = v;
1179 static struct ethtool_ops ops = {
1180 .get_drvinfo = pegasus_get_drvinfo,
1181 .get_settings = pegasus_get_settings,
1182 .set_settings = pegasus_set_settings,
1183 .nway_reset = pegasus_nway_reset,
1184 .get_link = pegasus_get_link,
1185 .get_msglevel = pegasus_get_msglevel,
1186 .set_msglevel = pegasus_set_msglevel,
1187 .get_wol = pegasus_get_wol,
1188 .set_wol = pegasus_set_wol,
1191 static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1193 __u16 *data = (__u16 *) & rq->ifr_ifru;
1194 pegasus_t *pegasus = netdev_priv(net);
1198 case SIOCDEVPRIVATE:
1199 data[0] = pegasus->phy;
1200 case SIOCDEVPRIVATE + 1:
1201 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1204 case SIOCDEVPRIVATE + 2:
1205 if (!capable(CAP_NET_ADMIN))
1207 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
1216 static void pegasus_set_multicast(struct net_device *net)
1218 pegasus_t *pegasus = netdev_priv(net);
1220 if (net->flags & IFF_PROMISC) {
1221 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1222 if (netif_msg_link(pegasus))
1223 pr_info("%s: Promiscuous mode enabled.\n", net->name);
1224 } else if (net->mc_count ||
1225 (net->flags & IFF_ALLMULTI)) {
1226 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1227 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1228 if (netif_msg_link(pegasus))
1229 pr_info("%s: set allmulti\n", net->name);
1231 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1232 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1235 pegasus->flags |= ETH_REGS_CHANGE;
1236 ctrl_callback(pegasus->ctrl_urb);
1239 static __u8 mii_phy_probe(pegasus_t * pegasus)
1244 for (i = 0; i < 32; i++) {
1245 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1246 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1255 static inline void setup_pegasus_II(pegasus_t * pegasus)
1259 set_register(pegasus, Reg1d, 0);
1260 set_register(pegasus, Reg7b, 1);
1262 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1263 set_register(pegasus, Reg7b, 0);
1265 set_register(pegasus, Reg7b, 2);
1267 set_register(pegasus, 0x83, data);
1268 get_registers(pegasus, 0x83, 1, &data);
1271 pegasus->chip = 0x8513;
1276 set_register(pegasus, 0x80, 0xc0);
1277 set_register(pegasus, 0x83, 0xff);
1278 set_register(pegasus, 0x84, 0x01);
1280 if (pegasus->features & HAS_HOME_PNA && mii_mode)
1281 set_register(pegasus, Reg81, 6);
1283 set_register(pegasus, Reg81, 2);
1287 static struct workqueue_struct *pegasus_workqueue = NULL;
1288 #define CARRIER_CHECK_DELAY (2 * HZ)
1290 static void check_carrier(struct work_struct *work)
1292 pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
1293 set_carrier(pegasus->net);
1294 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1295 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1296 CARRIER_CHECK_DELAY);
1300 static int pegasus_probe(struct usb_interface *intf,
1301 const struct usb_device_id *id)
1303 struct usb_device *dev = interface_to_usbdev(intf);
1304 struct net_device *net;
1306 int dev_index = id - pegasus_ids;
1310 net = alloc_etherdev(sizeof(struct pegasus));
1312 dev_err(&intf->dev, "can't allocate %s\n", "device");
1316 pegasus = netdev_priv(net);
1317 memset(pegasus, 0, sizeof (struct pegasus));
1318 pegasus->dev_index = dev_index;
1319 init_waitqueue_head(&pegasus->ctrl_wait);
1321 if (!alloc_urbs(pegasus)) {
1322 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1326 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1328 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
1330 pegasus->intf = intf;
1333 SET_MODULE_OWNER(net);
1334 net->open = pegasus_open;
1335 net->stop = pegasus_close;
1336 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1337 net->tx_timeout = pegasus_tx_timeout;
1338 net->do_ioctl = pegasus_ioctl;
1339 net->hard_start_xmit = pegasus_start_xmit;
1340 net->set_multicast_list = pegasus_set_multicast;
1341 net->get_stats = pegasus_netdev_stats;
1342 SET_ETHTOOL_OPS(net, &ops);
1343 pegasus->mii.dev = net;
1344 pegasus->mii.mdio_read = mdio_read;
1345 pegasus->mii.mdio_write = mdio_write;
1346 pegasus->mii.phy_id_mask = 0x1f;
1347 pegasus->mii.reg_num_mask = 0x1f;
1348 spin_lock_init(&pegasus->rx_pool_lock);
1349 pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1350 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1352 pegasus->features = usb_dev_id[dev_index].private;
1353 get_interrupt_interval(pegasus);
1354 if (reset_mac(pegasus)) {
1355 dev_err(&intf->dev, "can't reset MAC\n");
1359 set_ethernet_addr(pegasus);
1360 fill_skb_pool(pegasus);
1361 if (pegasus->features & PEGASUS_II) {
1362 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1363 setup_pegasus_II(pegasus);
1365 pegasus->phy = mii_phy_probe(pegasus);
1366 if (pegasus->phy == 0xff) {
1367 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1370 pegasus->mii.phy_id = pegasus->phy;
1371 usb_set_intfdata(intf, pegasus);
1372 SET_NETDEV_DEV(net, &intf->dev);
1373 pegasus_reset_wol(net);
1374 res = register_netdev(net);
1377 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1378 CARRIER_CHECK_DELAY);
1380 dev_info(&intf->dev, "%s, %s, %02x:%02x:%02x:%02x:%02x:%02x\n",
1382 usb_dev_id[dev_index].name,
1383 net->dev_addr [0], net->dev_addr [1],
1384 net->dev_addr [2], net->dev_addr [3],
1385 net->dev_addr [4], net->dev_addr [5]);
1389 usb_set_intfdata(intf, NULL);
1390 free_skb_pool(pegasus);
1392 free_all_urbs(pegasus);
1400 static void pegasus_disconnect(struct usb_interface *intf)
1402 struct pegasus *pegasus = usb_get_intfdata(intf);
1404 usb_set_intfdata(intf, NULL);
1406 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1410 pegasus->flags |= PEGASUS_UNPLUG;
1411 cancel_delayed_work(&pegasus->carrier_check);
1412 unregister_netdev(pegasus->net);
1413 usb_put_dev(interface_to_usbdev(intf));
1414 unlink_all_urbs(pegasus);
1415 free_all_urbs(pegasus);
1416 free_skb_pool(pegasus);
1417 if (pegasus->rx_skb != NULL) {
1418 dev_kfree_skb(pegasus->rx_skb);
1419 pegasus->rx_skb = NULL;
1421 free_netdev(pegasus->net);
1424 static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
1426 struct pegasus *pegasus = usb_get_intfdata(intf);
1428 netif_device_detach (pegasus->net);
1429 cancel_delayed_work(&pegasus->carrier_check);
1430 if (netif_running(pegasus->net)) {
1431 usb_kill_urb(pegasus->rx_urb);
1432 usb_kill_urb(pegasus->intr_urb);
1437 static int pegasus_resume (struct usb_interface *intf)
1439 struct pegasus *pegasus = usb_get_intfdata(intf);
1441 netif_device_attach (pegasus->net);
1442 if (netif_running(pegasus->net)) {
1443 pegasus->rx_urb->status = 0;
1444 pegasus->rx_urb->actual_length = 0;
1445 read_bulk_callback(pegasus->rx_urb);
1447 pegasus->intr_urb->status = 0;
1448 pegasus->intr_urb->actual_length = 0;
1449 intr_callback(pegasus->intr_urb);
1451 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1452 CARRIER_CHECK_DELAY);
1456 static struct usb_driver pegasus_driver = {
1457 .name = driver_name,
1458 .probe = pegasus_probe,
1459 .disconnect = pegasus_disconnect,
1460 .id_table = pegasus_ids,
1461 .suspend = pegasus_suspend,
1462 .resume = pegasus_resume,
1465 static void parse_id(char *id)
1467 unsigned int vendor_id=0, device_id=0, flags=0, i=0;
1468 char *token, *name=NULL;
1470 if ((token = strsep(&id, ":")) != NULL)
1472 /* name now points to a null terminated string*/
1473 if ((token = strsep(&id, ":")) != NULL)
1474 vendor_id = simple_strtoul(token, NULL, 16);
1475 if ((token = strsep(&id, ":")) != NULL)
1476 device_id = simple_strtoul(token, NULL, 16);
1477 flags = simple_strtoul(id, NULL, 16);
1478 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1479 driver_name, name, vendor_id, device_id, flags);
1481 if (vendor_id > 0x10000 || vendor_id == 0)
1483 if (device_id > 0x10000 || device_id == 0)
1486 for (i=0; usb_dev_id[i].name; i++);
1487 usb_dev_id[i].name = name;
1488 usb_dev_id[i].vendor = vendor_id;
1489 usb_dev_id[i].device = device_id;
1490 usb_dev_id[i].private = flags;
1491 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1492 pegasus_ids[i].idVendor = vendor_id;
1493 pegasus_ids[i].idProduct = device_id;
1496 static int __init pegasus_init(void)
1498 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
1501 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1502 if (!pegasus_workqueue)
1504 return usb_register(&pegasus_driver);
1507 static void __exit pegasus_exit(void)
1509 destroy_workqueue(pegasus_workqueue);
1510 usb_deregister(&pegasus_driver);
1513 module_init(pegasus_init);
1514 module_exit(pegasus_exit);