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;
102 int status = urb->status;
109 if (pegasus->flags & ETH_REGS_CHANGE) {
110 pegasus->flags &= ~ETH_REGS_CHANGE;
111 pegasus->flags |= ETH_REGS_CHANGED;
112 update_eth_regs_async(pegasus);
121 if (netif_msg_drv(pegasus) && printk_ratelimit())
122 dev_dbg(&pegasus->intf->dev, "%s, status %d\n",
125 pegasus->flags &= ~ETH_REGS_CHANGED;
126 wake_up(&pegasus->ctrl_wait);
129 static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
134 DECLARE_WAITQUEUE(wait, current);
136 buffer = kmalloc(size, GFP_KERNEL);
138 if (netif_msg_drv(pegasus))
139 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
143 add_wait_queue(&pegasus->ctrl_wait, &wait);
144 set_current_state(TASK_UNINTERRUPTIBLE);
145 while (pegasus->flags & ETH_REGS_CHANGED)
147 remove_wait_queue(&pegasus->ctrl_wait, &wait);
148 set_current_state(TASK_RUNNING);
150 pegasus->dr.bRequestType = PEGASUS_REQT_READ;
151 pegasus->dr.bRequest = PEGASUS_REQ_GET_REGS;
152 pegasus->dr.wValue = cpu_to_le16(0);
153 pegasus->dr.wIndex = cpu_to_le16p(&indx);
154 pegasus->dr.wLength = cpu_to_le16p(&size);
155 pegasus->ctrl_urb->transfer_buffer_length = size;
157 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
158 usb_rcvctrlpipe(pegasus->usb, 0),
159 (char *) &pegasus->dr,
160 buffer, size, ctrl_callback, pegasus);
162 add_wait_queue(&pegasus->ctrl_wait, &wait);
163 set_current_state(TASK_UNINTERRUPTIBLE);
165 /* using ATOMIC, we'd never wake up if we slept */
166 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
167 set_current_state(TASK_RUNNING);
169 netif_device_detach(pegasus->net);
170 if (netif_msg_drv(pegasus) && printk_ratelimit())
171 dev_err(&pegasus->intf->dev, "%s, status %d\n",
178 remove_wait_queue(&pegasus->ctrl_wait, &wait);
179 memcpy(data, buffer, size);
185 static int set_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
190 DECLARE_WAITQUEUE(wait, current);
192 buffer = kmalloc(size, GFP_KERNEL);
194 if (netif_msg_drv(pegasus))
195 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
199 memcpy(buffer, data, size);
201 add_wait_queue(&pegasus->ctrl_wait, &wait);
202 set_current_state(TASK_UNINTERRUPTIBLE);
203 while (pegasus->flags & ETH_REGS_CHANGED)
205 remove_wait_queue(&pegasus->ctrl_wait, &wait);
206 set_current_state(TASK_RUNNING);
208 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
209 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
210 pegasus->dr.wValue = cpu_to_le16(0);
211 pegasus->dr.wIndex = cpu_to_le16p(&indx);
212 pegasus->dr.wLength = cpu_to_le16p(&size);
213 pegasus->ctrl_urb->transfer_buffer_length = size;
215 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
216 usb_sndctrlpipe(pegasus->usb, 0),
217 (char *) &pegasus->dr,
218 buffer, size, ctrl_callback, pegasus);
220 add_wait_queue(&pegasus->ctrl_wait, &wait);
221 set_current_state(TASK_UNINTERRUPTIBLE);
223 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
225 netif_device_detach(pegasus->net);
226 if (netif_msg_drv(pegasus))
227 dev_err(&pegasus->intf->dev, "%s, status %d\n",
234 remove_wait_queue(&pegasus->ctrl_wait, &wait);
240 static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
244 DECLARE_WAITQUEUE(wait, current);
246 tmp = kmalloc(1, GFP_KERNEL);
248 if (netif_msg_drv(pegasus))
249 dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
253 memcpy(tmp, &data, 1);
254 add_wait_queue(&pegasus->ctrl_wait, &wait);
255 set_current_state(TASK_UNINTERRUPTIBLE);
256 while (pegasus->flags & ETH_REGS_CHANGED)
258 remove_wait_queue(&pegasus->ctrl_wait, &wait);
259 set_current_state(TASK_RUNNING);
261 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
262 pegasus->dr.bRequest = PEGASUS_REQ_SET_REG;
263 pegasus->dr.wValue = cpu_to_le16(data);
264 pegasus->dr.wIndex = cpu_to_le16p(&indx);
265 pegasus->dr.wLength = cpu_to_le16(1);
266 pegasus->ctrl_urb->transfer_buffer_length = 1;
268 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
269 usb_sndctrlpipe(pegasus->usb, 0),
270 (char *) &pegasus->dr,
271 tmp, 1, ctrl_callback, pegasus);
273 add_wait_queue(&pegasus->ctrl_wait, &wait);
274 set_current_state(TASK_UNINTERRUPTIBLE);
276 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
278 netif_device_detach(pegasus->net);
279 if (netif_msg_drv(pegasus) && printk_ratelimit())
280 dev_err(&pegasus->intf->dev, "%s, status %d\n",
287 remove_wait_queue(&pegasus->ctrl_wait, &wait);
293 static int update_eth_regs_async(pegasus_t * pegasus)
297 pegasus->dr.bRequestType = PEGASUS_REQT_WRITE;
298 pegasus->dr.bRequest = PEGASUS_REQ_SET_REGS;
299 pegasus->dr.wValue = 0;
300 pegasus->dr.wIndex = cpu_to_le16(EthCtrl0);
301 pegasus->dr.wLength = cpu_to_le16(3);
302 pegasus->ctrl_urb->transfer_buffer_length = 3;
304 usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
305 usb_sndctrlpipe(pegasus->usb, 0),
306 (char *) &pegasus->dr,
307 pegasus->eth_regs, 3, ctrl_callback, pegasus);
309 if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
311 netif_device_detach(pegasus->net);
312 if (netif_msg_drv(pegasus))
313 dev_err(&pegasus->intf->dev, "%s, status %d\n",
320 /* Returns 0 on success, error on failure */
321 static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
324 __u8 data[4] = { phy, 0, 0, indx };
328 set_register(pegasus, PhyCtrl, 0);
329 set_registers(pegasus, PhyAddr, sizeof (data), data);
330 set_register(pegasus, PhyCtrl, (indx | PHY_READ));
331 for (i = 0; i < REG_TIMEOUT; i++) {
332 ret = get_registers(pegasus, PhyCtrl, 1, data);
333 if (ret == -ESHUTDOWN)
335 if (data[0] & PHY_DONE)
338 if (i < REG_TIMEOUT) {
339 ret = get_registers(pegasus, PhyData, 2, ®di);
340 *regd = le16_to_cpu(regdi);
344 if (netif_msg_drv(pegasus))
345 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
350 static int mdio_read(struct net_device *dev, int phy_id, int loc)
352 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
355 read_mii_word(pegasus, phy_id, loc, &res);
359 static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
362 __u8 data[4] = { phy, 0, 0, indx };
366 data[2] = (u8) (regd >> 8);
367 set_register(pegasus, PhyCtrl, 0);
368 set_registers(pegasus, PhyAddr, sizeof(data), data);
369 set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
370 for (i = 0; i < REG_TIMEOUT; i++) {
371 ret = get_registers(pegasus, PhyCtrl, 1, data);
372 if (ret == -ESHUTDOWN)
374 if (data[0] & PHY_DONE)
381 if (netif_msg_drv(pegasus))
382 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
386 static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
388 pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
390 write_mii_word(pegasus, phy_id, loc, val);
393 static int read_eprom_word(pegasus_t * pegasus, __u8 index, __u16 * retdata)
400 set_register(pegasus, EpromCtrl, 0);
401 set_register(pegasus, EpromOffset, index);
402 set_register(pegasus, EpromCtrl, EPROM_READ);
404 for (i = 0; i < REG_TIMEOUT; i++) {
405 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
406 if (tmp & EPROM_DONE)
408 if (ret == -ESHUTDOWN)
411 if (i < REG_TIMEOUT) {
412 ret = get_registers(pegasus, EpromData, 2, &retdatai);
413 *retdata = le16_to_cpu(retdatai);
418 if (netif_msg_drv(pegasus))
419 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
423 #ifdef PEGASUS_WRITE_EEPROM
424 static inline void enable_eprom_write(pegasus_t * pegasus)
429 get_registers(pegasus, EthCtrl2, 1, &tmp);
430 set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
433 static inline void disable_eprom_write(pegasus_t * pegasus)
438 get_registers(pegasus, EthCtrl2, 1, &tmp);
439 set_register(pegasus, EpromCtrl, 0);
440 set_register(pegasus, EthCtrl2, tmp & ~EPROM_WR_ENABLE);
443 static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
446 __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
449 set_registers(pegasus, EpromOffset, 4, d);
450 enable_eprom_write(pegasus);
451 set_register(pegasus, EpromOffset, index);
452 set_registers(pegasus, EpromData, 2, &data);
453 set_register(pegasus, EpromCtrl, EPROM_WRITE);
455 for (i = 0; i < REG_TIMEOUT; i++) {
456 ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
457 if (ret == -ESHUTDOWN)
459 if (tmp & EPROM_DONE)
462 disable_eprom_write(pegasus);
466 if (netif_msg_drv(pegasus))
467 dev_warn(&pegasus->intf->dev, "%s failed\n", __func__);
470 #endif /* PEGASUS_WRITE_EEPROM */
472 static inline void get_node_id(pegasus_t * pegasus, __u8 * id)
477 for (i = 0; i < 3; i++) {
478 read_eprom_word(pegasus, i, &w16);
479 ((__le16 *) id)[i] = cpu_to_le16p(&w16);
483 static void set_ethernet_addr(pegasus_t * pegasus)
487 if (pegasus->features & PEGASUS_II) {
488 get_registers(pegasus, 0x10, sizeof(node_id), node_id);
490 get_node_id(pegasus, node_id);
491 set_registers(pegasus, EthID, sizeof (node_id), node_id);
493 memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
496 static inline int reset_mac(pegasus_t * pegasus)
501 set_register(pegasus, EthCtrl1, data);
502 for (i = 0; i < REG_TIMEOUT; i++) {
503 get_registers(pegasus, EthCtrl1, 1, &data);
507 if (mii_mode && (pegasus->features & HAS_HOME_PNA))
508 set_register(pegasus, Gpio1, 0x34);
510 set_register(pegasus, Gpio1, 0x26);
511 set_register(pegasus, Gpio0, pegasus->features);
512 set_register(pegasus, Gpio0, DEFAULT_GPIO_SET);
516 if (i == REG_TIMEOUT)
519 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
520 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
521 set_register(pegasus, Gpio0, 0x24);
522 set_register(pegasus, Gpio0, 0x26);
524 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_ELCON) {
526 read_mii_word(pegasus, 3, 0x1b, &auxmode);
527 write_mii_word(pegasus, 3, 0x1b, auxmode | 4);
533 static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
537 pegasus_t *pegasus = netdev_priv(dev);
540 read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
543 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_10FULL))
544 data[1] |= 0x20; /* set full duplex */
545 if (linkpart & (ADVERTISE_100FULL | ADVERTISE_100HALF))
546 data[1] |= 0x10; /* set 100 Mbps */
549 data[2] = (loopback & 1) ? 0x09 : 0x01;
551 memcpy(pegasus->eth_regs, data, sizeof (data));
552 ret = set_registers(pegasus, EthCtrl0, 3, data);
554 if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
555 usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
556 usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
558 read_mii_word(pegasus, 0, 0x1b, &auxmode);
559 write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
565 static void fill_skb_pool(pegasus_t * pegasus)
569 for (i = 0; i < RX_SKBS; i++) {
570 if (pegasus->rx_pool[i])
572 pegasus->rx_pool[i] = dev_alloc_skb(PEGASUS_MTU + 2);
574 ** we give up if the allocation fail. the tasklet will be
575 ** rescheduled again anyway...
577 if (pegasus->rx_pool[i] == NULL)
579 skb_reserve(pegasus->rx_pool[i], 2);
583 static void free_skb_pool(pegasus_t * pegasus)
587 for (i = 0; i < RX_SKBS; i++) {
588 if (pegasus->rx_pool[i]) {
589 dev_kfree_skb(pegasus->rx_pool[i]);
590 pegasus->rx_pool[i] = NULL;
595 static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
600 for (i = 0; i < RX_SKBS; i++) {
601 if (likely(pegasus->rx_pool[i] != NULL)) {
602 skb = pegasus->rx_pool[i];
603 pegasus->rx_pool[i] = NULL;
610 static void read_bulk_callback(struct urb *urb)
612 pegasus_t *pegasus = urb->context;
613 struct net_device *net;
614 int rx_status, count = urb->actual_length;
615 int status = urb->status;
616 u8 *buf = urb->transfer_buffer;
623 if (!netif_device_present(net) || !netif_running(net))
630 if (netif_msg_rx_err(pegasus))
631 pr_debug("%s: reset MAC\n", net->name);
632 pegasus->flags &= ~PEGASUS_RX_BUSY;
634 case -EPIPE: /* stall, or disconnect from TT */
635 /* FIXME schedule work to clear the halt */
636 if (netif_msg_rx_err(pegasus))
637 printk(KERN_WARNING "%s: no rx stall recovery\n",
643 if (netif_msg_ifdown(pegasus))
644 pr_debug("%s: rx unlink, %d\n", net->name, status);
647 if (netif_msg_rx_err(pegasus))
648 pr_debug("%s: RX status %d\n", net->name, status);
652 if (!count || count < 4)
655 rx_status = buf[count - 2];
656 if (rx_status & 0x1e) {
657 if (netif_msg_rx_err(pegasus))
658 pr_debug("%s: RX packet error %x\n",
659 net->name, rx_status);
660 pegasus->stats.rx_errors++;
661 if (rx_status & 0x06) // long or runt
662 pegasus->stats.rx_length_errors++;
663 if (rx_status & 0x08)
664 pegasus->stats.rx_crc_errors++;
665 if (rx_status & 0x10) // extra bits
666 pegasus->stats.rx_frame_errors++;
669 if (pegasus->chip == 0x8513) {
670 pkt_len = le32_to_cpu(*(__le32 *)urb->transfer_buffer);
672 pegasus->rx_skb->data += 2;
674 pkt_len = buf[count - 3] << 8;
675 pkt_len += buf[count - 4];
681 * If the packet is unreasonably long, quietly drop it rather than
682 * kernel panicing by calling skb_put.
684 if (pkt_len > PEGASUS_MTU)
688 * at this point we are sure pegasus->rx_skb != NULL
689 * so we go ahead and pass up the packet.
691 skb_put(pegasus->rx_skb, pkt_len);
692 pegasus->rx_skb->protocol = eth_type_trans(pegasus->rx_skb, net);
693 netif_rx(pegasus->rx_skb);
694 pegasus->stats.rx_packets++;
695 pegasus->stats.rx_bytes += pkt_len;
697 if (pegasus->flags & PEGASUS_UNPLUG)
700 spin_lock(&pegasus->rx_pool_lock);
701 pegasus->rx_skb = pull_skb(pegasus);
702 spin_unlock(&pegasus->rx_pool_lock);
704 if (pegasus->rx_skb == NULL)
707 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
708 usb_rcvbulkpipe(pegasus->usb, 1),
709 pegasus->rx_skb->data, PEGASUS_MTU + 8,
710 read_bulk_callback, pegasus);
711 rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
712 if (rx_status == -ENODEV)
713 netif_device_detach(pegasus->net);
714 else if (rx_status) {
715 pegasus->flags |= PEGASUS_RX_URB_FAIL;
718 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
724 tasklet_schedule(&pegasus->rx_tl);
727 static void rx_fixup(unsigned long data)
733 pegasus = (pegasus_t *) data;
734 if (pegasus->flags & PEGASUS_UNPLUG)
737 spin_lock_irqsave(&pegasus->rx_pool_lock, flags);
738 fill_skb_pool(pegasus);
739 if (pegasus->flags & PEGASUS_RX_URB_FAIL)
742 if (pegasus->rx_skb == NULL) {
743 pegasus->rx_skb = pull_skb(pegasus);
745 if (pegasus->rx_skb == NULL) {
746 if (netif_msg_rx_err(pegasus))
747 printk(KERN_WARNING "%s: low on memory\n",
749 tasklet_schedule(&pegasus->rx_tl);
752 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
753 usb_rcvbulkpipe(pegasus->usb, 1),
754 pegasus->rx_skb->data, PEGASUS_MTU + 8,
755 read_bulk_callback, pegasus);
757 status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
758 if (status == -ENODEV)
759 netif_device_detach(pegasus->net);
761 pegasus->flags |= PEGASUS_RX_URB_FAIL;
762 tasklet_schedule(&pegasus->rx_tl);
764 pegasus->flags &= ~PEGASUS_RX_URB_FAIL;
767 spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
770 static void write_bulk_callback(struct urb *urb)
772 pegasus_t *pegasus = urb->context;
773 struct net_device *net;
774 int status = urb->status;
781 if (!netif_device_present(net) || !netif_running(net))
786 /* FIXME schedule_work() to clear the tx halt */
787 netif_stop_queue(net);
788 if (netif_msg_tx_err(pegasus))
789 printk(KERN_WARNING "%s: no tx stall recovery\n",
795 if (netif_msg_ifdown(pegasus))
796 pr_debug("%s: tx unlink, %d\n", net->name, status);
799 if (netif_msg_tx_err(pegasus))
800 pr_info("%s: TX status %d\n", net->name, status);
806 net->trans_start = jiffies;
807 netif_wake_queue(net);
810 static void intr_callback(struct urb *urb)
812 pegasus_t *pegasus = urb->context;
813 struct net_device *net;
814 int res, status = urb->status;
823 case -ECONNRESET: /* unlink */
828 /* some Pegasus-I products report LOTS of data
829 * toggle errors... avoid log spamming
831 if (netif_msg_timer(pegasus))
832 pr_debug("%s: intr status %d\n", net->name,
836 if (urb->actual_length >= 6) {
837 u8 * d = urb->transfer_buffer;
839 /* byte 0 == tx_status1, reg 2B */
840 if (d[0] & (TX_UNDERRUN|EXCESSIVE_COL
841 |LATE_COL|JABBER_TIMEOUT)) {
842 pegasus->stats.tx_errors++;
843 if (d[0] & TX_UNDERRUN)
844 pegasus->stats.tx_fifo_errors++;
845 if (d[0] & (EXCESSIVE_COL | JABBER_TIMEOUT))
846 pegasus->stats.tx_aborted_errors++;
848 pegasus->stats.tx_window_errors++;
851 /* d[5].LINK_STATUS lies on some adapters.
852 * d[0].NO_CARRIER kicks in only with failed TX.
853 * ... so monitoring with MII may be safest.
856 /* bytes 3-4 == rx_lostpkt, reg 2E/2F */
857 pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
860 res = usb_submit_urb(urb, GFP_ATOMIC);
862 netif_device_detach(pegasus->net);
863 if (res && netif_msg_timer(pegasus))
864 printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
868 static void pegasus_tx_timeout(struct net_device *net)
870 pegasus_t *pegasus = netdev_priv(net);
871 if (netif_msg_timer(pegasus))
872 printk(KERN_WARNING "%s: tx timeout\n", net->name);
873 usb_unlink_urb(pegasus->tx_urb);
874 pegasus->stats.tx_errors++;
877 static int pegasus_start_xmit(struct sk_buff *skb, struct net_device *net)
879 pegasus_t *pegasus = netdev_priv(net);
880 int count = ((skb->len + 2) & 0x3f) ? skb->len + 2 : skb->len + 3;
882 __u16 l16 = skb->len;
884 netif_stop_queue(net);
886 ((__le16 *) pegasus->tx_buff)[0] = cpu_to_le16(l16);
887 skb_copy_from_linear_data(skb, pegasus->tx_buff + 2, skb->len);
888 usb_fill_bulk_urb(pegasus->tx_urb, pegasus->usb,
889 usb_sndbulkpipe(pegasus->usb, 2),
890 pegasus->tx_buff, count,
891 write_bulk_callback, pegasus);
892 if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
893 if (netif_msg_tx_err(pegasus))
894 printk(KERN_WARNING "%s: fail tx, %d\n",
897 case -EPIPE: /* stall, or disconnect from TT */
898 /* cleanup should already have been scheduled */
900 case -ENODEV: /* disconnect() upcoming */
901 netif_device_detach(pegasus->net);
904 pegasus->stats.tx_errors++;
905 netif_start_queue(net);
908 pegasus->stats.tx_packets++;
909 pegasus->stats.tx_bytes += skb->len;
910 net->trans_start = jiffies;
917 static struct net_device_stats *pegasus_netdev_stats(struct net_device *dev)
919 return &((pegasus_t *) netdev_priv(dev))->stats;
922 static inline void disable_net_traffic(pegasus_t * pegasus)
926 set_registers(pegasus, EthCtrl0, 2, &tmp);
929 static inline void get_interrupt_interval(pegasus_t * pegasus)
933 read_eprom_word(pegasus, 4, (__u16 *) data);
934 if (pegasus->usb->speed != USB_SPEED_HIGH) {
935 if (data[1] < 0x80) {
936 if (netif_msg_timer(pegasus))
937 dev_info(&pegasus->intf->dev, "intr interval "
938 "changed from %ums to %ums\n",
941 #ifdef PEGASUS_WRITE_EEPROM
942 write_eprom_word(pegasus, 4, *(__u16 *) data);
946 pegasus->intr_interval = data[1];
949 static void set_carrier(struct net_device *net)
951 pegasus_t *pegasus = netdev_priv(net);
954 if (read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
957 if (tmp & BMSR_LSTATUS)
958 netif_carrier_on(net);
960 netif_carrier_off(net);
963 static void free_all_urbs(pegasus_t * pegasus)
965 usb_free_urb(pegasus->intr_urb);
966 usb_free_urb(pegasus->tx_urb);
967 usb_free_urb(pegasus->rx_urb);
968 usb_free_urb(pegasus->ctrl_urb);
971 static void unlink_all_urbs(pegasus_t * pegasus)
973 usb_kill_urb(pegasus->intr_urb);
974 usb_kill_urb(pegasus->tx_urb);
975 usb_kill_urb(pegasus->rx_urb);
976 usb_kill_urb(pegasus->ctrl_urb);
979 static int alloc_urbs(pegasus_t * pegasus)
981 pegasus->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
982 if (!pegasus->ctrl_urb) {
985 pegasus->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
986 if (!pegasus->rx_urb) {
987 usb_free_urb(pegasus->ctrl_urb);
990 pegasus->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
991 if (!pegasus->tx_urb) {
992 usb_free_urb(pegasus->rx_urb);
993 usb_free_urb(pegasus->ctrl_urb);
996 pegasus->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
997 if (!pegasus->intr_urb) {
998 usb_free_urb(pegasus->tx_urb);
999 usb_free_urb(pegasus->rx_urb);
1000 usb_free_urb(pegasus->ctrl_urb);
1007 static int pegasus_open(struct net_device *net)
1009 pegasus_t *pegasus = netdev_priv(net);
1012 if (pegasus->rx_skb == NULL)
1013 pegasus->rx_skb = pull_skb(pegasus);
1015 ** Note: no point to free the pool. it is empty :-)
1017 if (!pegasus->rx_skb)
1020 res = set_registers(pegasus, EthID, 6, net->dev_addr);
1022 usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
1023 usb_rcvbulkpipe(pegasus->usb, 1),
1024 pegasus->rx_skb->data, PEGASUS_MTU + 8,
1025 read_bulk_callback, pegasus);
1026 if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
1028 netif_device_detach(pegasus->net);
1029 if (netif_msg_ifup(pegasus))
1030 pr_debug("%s: failed rx_urb, %d", net->name, res);
1034 usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
1035 usb_rcvintpipe(pegasus->usb, 3),
1036 pegasus->intr_buff, sizeof (pegasus->intr_buff),
1037 intr_callback, pegasus, pegasus->intr_interval);
1038 if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
1040 netif_device_detach(pegasus->net);
1041 if (netif_msg_ifup(pegasus))
1042 pr_debug("%s: failed intr_urb, %d\n", net->name, res);
1043 usb_kill_urb(pegasus->rx_urb);
1046 if ((res = enable_net_traffic(net, pegasus->usb))) {
1047 if (netif_msg_ifup(pegasus))
1048 pr_debug("%s: can't enable_net_traffic() - %d\n",
1051 usb_kill_urb(pegasus->rx_urb);
1052 usb_kill_urb(pegasus->intr_urb);
1053 free_skb_pool(pegasus);
1057 netif_start_queue(net);
1058 if (netif_msg_ifup(pegasus))
1059 pr_debug("%s: open\n", net->name);
1065 static int pegasus_close(struct net_device *net)
1067 pegasus_t *pegasus = netdev_priv(net);
1069 netif_stop_queue(net);
1070 if (!(pegasus->flags & PEGASUS_UNPLUG))
1071 disable_net_traffic(pegasus);
1072 tasklet_kill(&pegasus->rx_tl);
1073 unlink_all_urbs(pegasus);
1078 static void pegasus_get_drvinfo(struct net_device *dev,
1079 struct ethtool_drvinfo *info)
1081 pegasus_t *pegasus = netdev_priv(dev);
1082 strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
1083 strncpy(info->version, DRIVER_VERSION, sizeof (info->version) - 1);
1084 usb_make_path(pegasus->usb, info->bus_info, sizeof (info->bus_info));
1087 /* also handles three patterns of some kind in hardware */
1088 #define WOL_SUPPORTED (WAKE_MAGIC|WAKE_PHY)
1091 pegasus_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1093 pegasus_t *pegasus = netdev_priv(dev);
1095 wol->supported = WAKE_MAGIC | WAKE_PHY;
1096 wol->wolopts = pegasus->wolopts;
1100 pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1102 pegasus_t *pegasus = netdev_priv(dev);
1105 if (wol->wolopts & ~WOL_SUPPORTED)
1108 if (wol->wolopts & WAKE_MAGIC)
1110 if (wol->wolopts & WAKE_PHY)
1112 /* FIXME this 0x10 bit still needs to get set in the chip... */
1114 pegasus->eth_regs[0] |= 0x10;
1116 pegasus->eth_regs[0] &= ~0x10;
1117 pegasus->wolopts = wol->wolopts;
1118 return set_register(pegasus, WakeupControl, reg78);
1121 static inline void pegasus_reset_wol(struct net_device *dev)
1123 struct ethtool_wolinfo wol;
1125 memset(&wol, 0, sizeof wol);
1126 (void) pegasus_set_wol(dev, &wol);
1130 pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1134 pegasus = netdev_priv(dev);
1135 mii_ethtool_gset(&pegasus->mii, ecmd);
1140 pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1142 pegasus_t *pegasus = netdev_priv(dev);
1143 return mii_ethtool_sset(&pegasus->mii, ecmd);
1146 static int pegasus_nway_reset(struct net_device *dev)
1148 pegasus_t *pegasus = netdev_priv(dev);
1149 return mii_nway_restart(&pegasus->mii);
1152 static u32 pegasus_get_link(struct net_device *dev)
1154 pegasus_t *pegasus = netdev_priv(dev);
1155 return mii_link_ok(&pegasus->mii);
1158 static u32 pegasus_get_msglevel(struct net_device *dev)
1160 pegasus_t *pegasus = netdev_priv(dev);
1161 return pegasus->msg_enable;
1164 static void pegasus_set_msglevel(struct net_device *dev, u32 v)
1166 pegasus_t *pegasus = netdev_priv(dev);
1167 pegasus->msg_enable = v;
1170 static struct ethtool_ops ops = {
1171 .get_drvinfo = pegasus_get_drvinfo,
1172 .get_settings = pegasus_get_settings,
1173 .set_settings = pegasus_set_settings,
1174 .nway_reset = pegasus_nway_reset,
1175 .get_link = pegasus_get_link,
1176 .get_msglevel = pegasus_get_msglevel,
1177 .set_msglevel = pegasus_set_msglevel,
1178 .get_wol = pegasus_get_wol,
1179 .set_wol = pegasus_set_wol,
1182 static int pegasus_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
1184 __u16 *data = (__u16 *) & rq->ifr_ifru;
1185 pegasus_t *pegasus = netdev_priv(net);
1189 case SIOCDEVPRIVATE:
1190 data[0] = pegasus->phy;
1191 case SIOCDEVPRIVATE + 1:
1192 read_mii_word(pegasus, data[0], data[1] & 0x1f, &data[3]);
1195 case SIOCDEVPRIVATE + 2:
1196 if (!capable(CAP_NET_ADMIN))
1198 write_mii_word(pegasus, pegasus->phy, data[1] & 0x1f, data[2]);
1207 static void pegasus_set_multicast(struct net_device *net)
1209 pegasus_t *pegasus = netdev_priv(net);
1211 if (net->flags & IFF_PROMISC) {
1212 pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
1213 if (netif_msg_link(pegasus))
1214 pr_info("%s: Promiscuous mode enabled.\n", net->name);
1215 } else if (net->mc_count || (net->flags & IFF_ALLMULTI)) {
1216 pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
1217 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1218 if (netif_msg_link(pegasus))
1219 pr_debug("%s: set allmulti\n", net->name);
1221 pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
1222 pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
1225 pegasus->ctrl_urb->status = 0;
1227 pegasus->flags |= ETH_REGS_CHANGE;
1228 ctrl_callback(pegasus->ctrl_urb);
1231 static __u8 mii_phy_probe(pegasus_t * pegasus)
1236 for (i = 0; i < 32; i++) {
1237 read_mii_word(pegasus, i, MII_BMSR, &tmp);
1238 if (tmp == 0 || tmp == 0xffff || (tmp & BMSR_MEDIA) == 0)
1247 static inline void setup_pegasus_II(pegasus_t * pegasus)
1251 set_register(pegasus, Reg1d, 0);
1252 set_register(pegasus, Reg7b, 1);
1254 if ((pegasus->features & HAS_HOME_PNA) && mii_mode)
1255 set_register(pegasus, Reg7b, 0);
1257 set_register(pegasus, Reg7b, 2);
1259 set_register(pegasus, 0x83, data);
1260 get_registers(pegasus, 0x83, 1, &data);
1263 pegasus->chip = 0x8513;
1268 set_register(pegasus, 0x80, 0xc0);
1269 set_register(pegasus, 0x83, 0xff);
1270 set_register(pegasus, 0x84, 0x01);
1272 if (pegasus->features & HAS_HOME_PNA && mii_mode)
1273 set_register(pegasus, Reg81, 6);
1275 set_register(pegasus, Reg81, 2);
1279 static int pegasus_count;
1280 static struct workqueue_struct *pegasus_workqueue = NULL;
1281 #define CARRIER_CHECK_DELAY (2 * HZ)
1283 static void check_carrier(struct work_struct *work)
1285 pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
1286 set_carrier(pegasus->net);
1287 if (!(pegasus->flags & PEGASUS_UNPLUG)) {
1288 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1289 CARRIER_CHECK_DELAY);
1293 static int pegasus_blacklisted(struct usb_device *udev)
1295 struct usb_device_descriptor *udd = &udev->descriptor;
1297 /* Special quirk to keep the driver from handling the Belkin Bluetooth
1298 * dongle which happens to have the same ID.
1300 if ((udd->idVendor == VENDOR_BELKIN && udd->idProduct == 0x0121) &&
1301 (udd->bDeviceClass == USB_CLASS_WIRELESS_CONTROLLER) &&
1302 (udd->bDeviceProtocol == 1))
1308 /* we rely on probe() and remove() being serialized so we
1309 * don't need extra locking on pegasus_count.
1311 static void pegasus_dec_workqueue(void)
1314 if (pegasus_count == 0) {
1315 destroy_workqueue(pegasus_workqueue);
1316 pegasus_workqueue = NULL;
1320 static int pegasus_probe(struct usb_interface *intf,
1321 const struct usb_device_id *id)
1323 struct usb_device *dev = interface_to_usbdev(intf);
1324 struct net_device *net;
1326 int dev_index = id - pegasus_ids;
1329 if (pegasus_blacklisted(dev))
1332 if (pegasus_count == 0) {
1333 pegasus_workqueue = create_singlethread_workqueue("pegasus");
1334 if (!pegasus_workqueue)
1341 net = alloc_etherdev(sizeof(struct pegasus));
1343 dev_err(&intf->dev, "can't allocate %s\n", "device");
1347 pegasus = netdev_priv(net);
1348 pegasus->dev_index = dev_index;
1349 init_waitqueue_head(&pegasus->ctrl_wait);
1351 if (!alloc_urbs(pegasus)) {
1352 dev_err(&intf->dev, "can't allocate %s\n", "urbs");
1356 tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
1358 INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
1360 pegasus->intf = intf;
1363 net->open = pegasus_open;
1364 net->stop = pegasus_close;
1365 net->watchdog_timeo = PEGASUS_TX_TIMEOUT;
1366 net->tx_timeout = pegasus_tx_timeout;
1367 net->do_ioctl = pegasus_ioctl;
1368 net->hard_start_xmit = pegasus_start_xmit;
1369 net->set_multicast_list = pegasus_set_multicast;
1370 net->get_stats = pegasus_netdev_stats;
1371 SET_ETHTOOL_OPS(net, &ops);
1372 pegasus->mii.dev = net;
1373 pegasus->mii.mdio_read = mdio_read;
1374 pegasus->mii.mdio_write = mdio_write;
1375 pegasus->mii.phy_id_mask = 0x1f;
1376 pegasus->mii.reg_num_mask = 0x1f;
1377 spin_lock_init(&pegasus->rx_pool_lock);
1378 pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
1379 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
1381 pegasus->features = usb_dev_id[dev_index].private;
1382 get_interrupt_interval(pegasus);
1383 if (reset_mac(pegasus)) {
1384 dev_err(&intf->dev, "can't reset MAC\n");
1388 set_ethernet_addr(pegasus);
1389 fill_skb_pool(pegasus);
1390 if (pegasus->features & PEGASUS_II) {
1391 dev_info(&intf->dev, "setup Pegasus II specific registers\n");
1392 setup_pegasus_II(pegasus);
1394 pegasus->phy = mii_phy_probe(pegasus);
1395 if (pegasus->phy == 0xff) {
1396 dev_warn(&intf->dev, "can't locate MII phy, using default\n");
1399 pegasus->mii.phy_id = pegasus->phy;
1400 usb_set_intfdata(intf, pegasus);
1401 SET_NETDEV_DEV(net, &intf->dev);
1402 pegasus_reset_wol(net);
1403 res = register_netdev(net);
1406 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1407 CARRIER_CHECK_DELAY);
1409 dev_info(&intf->dev, "%s, %s, %pM\n",
1411 usb_dev_id[dev_index].name,
1416 usb_set_intfdata(intf, NULL);
1417 free_skb_pool(pegasus);
1419 free_all_urbs(pegasus);
1424 pegasus_dec_workqueue();
1428 static void pegasus_disconnect(struct usb_interface *intf)
1430 struct pegasus *pegasus = usb_get_intfdata(intf);
1432 usb_set_intfdata(intf, NULL);
1434 dev_dbg(&intf->dev, "unregistering non-bound device?\n");
1438 pegasus->flags |= PEGASUS_UNPLUG;
1439 cancel_delayed_work(&pegasus->carrier_check);
1440 unregister_netdev(pegasus->net);
1441 usb_put_dev(interface_to_usbdev(intf));
1442 unlink_all_urbs(pegasus);
1443 free_all_urbs(pegasus);
1444 free_skb_pool(pegasus);
1445 if (pegasus->rx_skb != NULL) {
1446 dev_kfree_skb(pegasus->rx_skb);
1447 pegasus->rx_skb = NULL;
1449 free_netdev(pegasus->net);
1450 pegasus_dec_workqueue();
1453 static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
1455 struct pegasus *pegasus = usb_get_intfdata(intf);
1457 netif_device_detach (pegasus->net);
1458 cancel_delayed_work(&pegasus->carrier_check);
1459 if (netif_running(pegasus->net)) {
1460 usb_kill_urb(pegasus->rx_urb);
1461 usb_kill_urb(pegasus->intr_urb);
1466 static int pegasus_resume (struct usb_interface *intf)
1468 struct pegasus *pegasus = usb_get_intfdata(intf);
1470 netif_device_attach (pegasus->net);
1471 if (netif_running(pegasus->net)) {
1472 pegasus->rx_urb->status = 0;
1473 pegasus->rx_urb->actual_length = 0;
1474 read_bulk_callback(pegasus->rx_urb);
1476 pegasus->intr_urb->status = 0;
1477 pegasus->intr_urb->actual_length = 0;
1478 intr_callback(pegasus->intr_urb);
1480 queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
1481 CARRIER_CHECK_DELAY);
1485 static struct usb_driver pegasus_driver = {
1486 .name = driver_name,
1487 .probe = pegasus_probe,
1488 .disconnect = pegasus_disconnect,
1489 .id_table = pegasus_ids,
1490 .suspend = pegasus_suspend,
1491 .resume = pegasus_resume,
1494 static void __init parse_id(char *id)
1496 unsigned int vendor_id=0, device_id=0, flags=0, i=0;
1497 char *token, *name=NULL;
1499 if ((token = strsep(&id, ":")) != NULL)
1501 /* name now points to a null terminated string*/
1502 if ((token = strsep(&id, ":")) != NULL)
1503 vendor_id = simple_strtoul(token, NULL, 16);
1504 if ((token = strsep(&id, ":")) != NULL)
1505 device_id = simple_strtoul(token, NULL, 16);
1506 flags = simple_strtoul(id, NULL, 16);
1507 pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
1508 driver_name, name, vendor_id, device_id, flags);
1510 if (vendor_id > 0x10000 || vendor_id == 0)
1512 if (device_id > 0x10000 || device_id == 0)
1515 for (i=0; usb_dev_id[i].name; i++);
1516 usb_dev_id[i].name = name;
1517 usb_dev_id[i].vendor = vendor_id;
1518 usb_dev_id[i].device = device_id;
1519 usb_dev_id[i].private = flags;
1520 pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
1521 pegasus_ids[i].idVendor = vendor_id;
1522 pegasus_ids[i].idProduct = device_id;
1525 static int __init pegasus_init(void)
1527 pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
1530 return usb_register(&pegasus_driver);
1533 static void __exit pegasus_exit(void)
1535 usb_deregister(&pegasus_driver);
1538 module_init(pegasus_init);
1539 module_exit(pegasus_exit);