fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / usb / net / pegasus.c
index 542481e..d48c024 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 1999-2003 Petko Manolov (petkan@users.sourceforge.net)
+ *  Copyright (c) 1999-2005 Petko Manolov (petkan@users.sourceforge.net)
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -28,8 +28,6 @@
  *                     is out of the interrupt routine.
  */
 
-#undef DEBUG
-
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/init.h>
@@ -47,7 +45,7 @@
 /*
  * Version Information
  */
-#define DRIVER_VERSION "v0.5.12 (2005/01/13)"
+#define DRIVER_VERSION "v0.6.14 (2006/09/27)"
 #define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
 #define DRIVER_DESC "Pegasus/Pegasus II USB Ethernet driver"
 
@@ -59,13 +57,14 @@ static const char driver_name[] = "pegasus";
 
 static int loopback = 0;
 static int mii_mode = 0;
-static int multicast_filter_limit = 32;
+static char *devid=NULL;
 
 static struct usb_eth_dev usb_dev_id[] = {
 #define        PEGASUS_DEV(pn, vid, pid, flags)        \
        {.name = pn, .vendor = vid, .device = pid, .private = flags},
 #include "pegasus.h"
 #undef PEGASUS_DEV
+       {NULL, 0, 0, 0},
        {NULL, 0, 0, 0}
 };
 
@@ -74,6 +73,7 @@ static struct usb_device_id pegasus_ids[] = {
        {.match_flags = USB_DEVICE_ID_MATCH_DEVICE, .idVendor = vid, .idProduct = pid},
 #include "pegasus.h"
 #undef PEGASUS_DEV
+       {},
        {}
 };
 
@@ -82,14 +82,21 @@ MODULE_DESCRIPTION(DRIVER_DESC);
 MODULE_LICENSE("GPL");
 module_param(loopback, bool, 0);
 module_param(mii_mode, bool, 0);
+module_param(devid, charp, 0);
 MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
 MODULE_PARM_DESC(mii_mode, "Enable HomePNA mode (bit 0),default=MII mode = 0");
+MODULE_PARM_DESC(devid, "The format is: 'DEV_name:VendorID:DeviceID:Flags'");
+
+/* use ethtool to change the level for any given device */
+static int msg_level = -1;
+module_param (msg_level, int, 0);
+MODULE_PARM_DESC (msg_level, "Override default message level");
 
 MODULE_DEVICE_TABLE(usb, pegasus_ids);
 
 static int update_eth_regs_async(pegasus_t *);
 /* Aargh!!! I _really_ hate such tweaks */
-static void ctrl_callback(struct urb *urb, struct pt_regs *regs)
+static void ctrl_callback(struct urb *urb)
 {
        pegasus_t *pegasus = urb->context;
 
@@ -110,7 +117,9 @@ static void ctrl_callback(struct urb *urb, struct pt_regs *regs)
        case -ENOENT:
                break;
        default:
-               warn("%s: status %d", __FUNCTION__, urb->status);
+               if (netif_msg_drv(pegasus))
+                       dev_dbg(&pegasus->intf->dev, "%s, status %d\n",
+                               __FUNCTION__, urb->status);
        }
        pegasus->flags &= ~ETH_REGS_CHANGED;
        wake_up(&pegasus->ctrl_wait);
@@ -125,7 +134,9 @@ static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
 
        buffer = kmalloc(size, GFP_KERNEL);
        if (!buffer) {
-               warn("%s: looks like we're out of memory", __FUNCTION__);
+               if (netif_msg_drv(pegasus))
+                       dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
+                                       __FUNCTION__);
                return -ENOMEM;
        }
        add_wait_queue(&pegasus->ctrl_wait, &wait);
@@ -152,7 +163,12 @@ static int get_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
 
        /* using ATOMIC, we'd never wake up if we slept */
        if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
-               err("%s: BAD CTRLs %d", __FUNCTION__, ret);
+               set_current_state(TASK_RUNNING);
+               if (ret == -ENODEV)
+                       netif_device_detach(pegasus->net);
+               if (netif_msg_drv(pegasus))
+                       dev_err(&pegasus->intf->dev, "%s, status %d\n",
+                                       __FUNCTION__, ret);
                goto out;
        }
 
@@ -174,7 +190,9 @@ static int set_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
 
        buffer = kmalloc(size, GFP_KERNEL);
        if (!buffer) {
-               warn("%s: looks like we're out of memory", __FUNCTION__);
+               if (netif_msg_drv(pegasus))
+                       dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
+                                       __FUNCTION__);
                return -ENOMEM;
        }
        memcpy(buffer, data, size);
@@ -202,7 +220,11 @@ static int set_registers(pegasus_t * pegasus, __u16 indx, __u16 size,
        set_current_state(TASK_UNINTERRUPTIBLE);
 
        if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
-               err("%s: BAD CTRL %d", __FUNCTION__, ret);
+               if (ret == -ENODEV)
+                       netif_device_detach(pegasus->net);
+               if (netif_msg_drv(pegasus))
+                       dev_err(&pegasus->intf->dev, "%s, status %d\n",
+                                       __FUNCTION__, ret);
                goto out;
        }
 
@@ -222,7 +244,9 @@ static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
 
        tmp = kmalloc(1, GFP_KERNEL);
        if (!tmp) {
-               warn("%s: looks like we're out of memory", __FUNCTION__);
+               if (netif_msg_drv(pegasus))
+                       dev_warn(&pegasus->intf->dev, "out of memory in %s\n",
+                                       __FUNCTION__);
                return -ENOMEM;
        }
        memcpy(tmp, &data, 1);
@@ -243,13 +267,17 @@ static int set_register(pegasus_t * pegasus, __u16 indx, __u8 data)
        usb_fill_control_urb(pegasus->ctrl_urb, pegasus->usb,
                             usb_sndctrlpipe(pegasus->usb, 0),
                             (char *) &pegasus->dr,
-                            &tmp, 1, ctrl_callback, pegasus);
+                            tmp, 1, ctrl_callback, pegasus);
 
        add_wait_queue(&pegasus->ctrl_wait, &wait);
        set_current_state(TASK_UNINTERRUPTIBLE);
 
        if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
-               err("%s: BAD CTRL %d", __FUNCTION__, ret);
+               if (ret == -ENODEV)
+                       netif_device_detach(pegasus->net);
+               if (netif_msg_drv(pegasus))
+                       dev_err(&pegasus->intf->dev, "%s, status %d\n",
+                                       __FUNCTION__, ret);
                goto out;
        }
 
@@ -277,9 +305,13 @@ static int update_eth_regs_async(pegasus_t * pegasus)
                             (char *) &pegasus->dr,
                             pegasus->eth_regs, 3, ctrl_callback, pegasus);
 
-       if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC)))
-               err("%s: BAD CTRL %d, flgs %x", __FUNCTION__, ret,
-                   pegasus->flags);
+       if ((ret = usb_submit_urb(pegasus->ctrl_urb, GFP_ATOMIC))) {
+               if (ret == -ENODEV)
+                       netif_device_detach(pegasus->net);
+               if (netif_msg_drv(pegasus))
+                       dev_err(&pegasus->intf->dev, "%s, status %d\n",
+                                       __FUNCTION__, ret);
+       }
 
        return ret;
 }
@@ -289,53 +321,64 @@ static int read_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 * regd)
        int i;
        __u8 data[4] = { phy, 0, 0, indx };
        __le16 regdi;
+       int ret;
 
        set_register(pegasus, PhyCtrl, 0);
        set_registers(pegasus, PhyAddr, sizeof (data), data);
        set_register(pegasus, PhyCtrl, (indx | PHY_READ));
        for (i = 0; i < REG_TIMEOUT; i++) {
-               get_registers(pegasus, PhyCtrl, 1, data);
+               ret = get_registers(pegasus, PhyCtrl, 1, data);
+               if (ret == -ESHUTDOWN)
+                       goto fail;
                if (data[0] & PHY_DONE)
                        break;
        }
        if (i < REG_TIMEOUT) {
-               get_registers(pegasus, PhyData, 2, &regdi);
+               ret = get_registers(pegasus, PhyData, 2, &regdi);
                *regd = le16_to_cpu(regdi);
-               return 0;
+               return ret;
        }
-       warn("%s: failed", __FUNCTION__);
+fail:
+       if (netif_msg_drv(pegasus))
+               dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
 
-       return 1;
+       return ret;
 }
 
 static int mdio_read(struct net_device *dev, int phy_id, int loc)
 {
        pegasus_t *pegasus = (pegasus_t *) netdev_priv(dev);
-       int res;
+       u16 res;
 
-       read_mii_word(pegasus, phy_id, loc, (u16 *) & res);
-       return res & 0xffff;
+       read_mii_word(pegasus, phy_id, loc, &res);
+       return (int)res;
 }
 
 static int write_mii_word(pegasus_t * pegasus, __u8 phy, __u8 indx, __u16 regd)
 {
        int i;
        __u8 data[4] = { phy, 0, 0, indx };
+       int ret;
 
-       *(data + 1) = cpu_to_le16p(&regd);
+       data[1] = (u8) regd;
+       data[2] = (u8) (regd >> 8);
        set_register(pegasus, PhyCtrl, 0);
        set_registers(pegasus, PhyAddr, sizeof(data), data);
        set_register(pegasus, PhyCtrl, (indx | PHY_WRITE));
        for (i = 0; i < REG_TIMEOUT; i++) {
-               get_registers(pegasus, PhyCtrl, 1, data);
+               ret = get_registers(pegasus, PhyCtrl, 1, data);
+               if (ret == -ESHUTDOWN)
+                       goto fail;
                if (data[0] & PHY_DONE)
                        break;
        }
        if (i < REG_TIMEOUT)
-               return 0;
-       warn("%s: failed", __FUNCTION__);
+               return ret;
 
-       return 1;
+fail:
+       if (netif_msg_drv(pegasus))
+               dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
+       return -ETIMEDOUT;
 }
 
 static void mdio_write(struct net_device *dev, int phy_id, int loc, int val)
@@ -350,30 +393,36 @@ static int read_eprom_word(pegasus_t * pegasus, __u8 index, __u16 * retdata)
        int i;
        __u8 tmp;
        __le16 retdatai;
+       int ret;
 
        set_register(pegasus, EpromCtrl, 0);
        set_register(pegasus, EpromOffset, index);
        set_register(pegasus, EpromCtrl, EPROM_READ);
 
        for (i = 0; i < REG_TIMEOUT; i++) {
-               get_registers(pegasus, EpromCtrl, 1, &tmp);
+               ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
                if (tmp & EPROM_DONE)
                        break;
+               if (ret == -ESHUTDOWN)
+                       goto fail;
        }
        if (i < REG_TIMEOUT) {
-               get_registers(pegasus, EpromData, 2, &retdatai);
+               ret = get_registers(pegasus, EpromData, 2, &retdatai);
                *retdata = le16_to_cpu(retdatai);
-               return 0;
+               return ret;
        }
-       warn("%s: failed", __FUNCTION__);
 
-       return -1;
+fail:
+       if (netif_msg_drv(pegasus))
+               dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
+       return -ETIMEDOUT;
 }
 
 #ifdef PEGASUS_WRITE_EEPROM
 static inline void enable_eprom_write(pegasus_t * pegasus)
 {
        __u8 tmp;
+       int ret;
 
        get_registers(pegasus, EthCtrl2, 1, &tmp);
        set_register(pegasus, EthCtrl2, tmp | EPROM_WR_ENABLE);
@@ -382,6 +431,7 @@ static inline void enable_eprom_write(pegasus_t * pegasus)
 static inline void disable_eprom_write(pegasus_t * pegasus)
 {
        __u8 tmp;
+       int ret;
 
        get_registers(pegasus, EthCtrl2, 1, &tmp);
        set_register(pegasus, EpromCtrl, 0);
@@ -392,6 +442,7 @@ static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
 {
        int i;
        __u8 tmp, d[4] = { 0x3f, 0, 0, EPROM_WRITE };
+       int ret;
 
        set_registers(pegasus, EpromOffset, 4, d);
        enable_eprom_write(pegasus);
@@ -400,15 +451,19 @@ static int write_eprom_word(pegasus_t * pegasus, __u8 index, __u16 data)
        set_register(pegasus, EpromCtrl, EPROM_WRITE);
 
        for (i = 0; i < REG_TIMEOUT; i++) {
-               get_registers(pegasus, EpromCtrl, 1, &tmp);
+               ret = get_registers(pegasus, EpromCtrl, 1, &tmp);
+               if (ret == -ESHUTDOWN)
+                       goto fail;
                if (tmp & EPROM_DONE)
                        break;
        }
        disable_eprom_write(pegasus);
        if (i < REG_TIMEOUT)
-               return 0;
-       warn("%s: failed", __FUNCTION__);
-       return -1;
+               return ret;
+fail:
+       if (netif_msg_drv(pegasus))
+               dev_warn(&pegasus->intf->dev, "%s failed\n", __FUNCTION__);
+       return -ETIMEDOUT;
 }
 #endif                         /* PEGASUS_WRITE_EEPROM */
 
@@ -427,8 +482,12 @@ static void set_ethernet_addr(pegasus_t * pegasus)
 {
        __u8 node_id[6];
 
-       get_node_id(pegasus, node_id);
-       set_registers(pegasus, EthID, sizeof (node_id), node_id);
+       if (pegasus->features & PEGASUS_II) {
+               get_registers(pegasus, 0x10, sizeof(node_id), node_id);
+       } else {
+               get_node_id(pegasus, node_id);
+               set_registers(pegasus, EthID, sizeof (node_id), node_id);
+       }
        memcpy(pegasus->net->dev_addr, node_id, sizeof (node_id));
 }
 
@@ -453,7 +512,7 @@ static inline int reset_mac(pegasus_t * pegasus)
                }
        }
        if (i == REG_TIMEOUT)
-               return 1;
+               return -ETIMEDOUT;
 
        if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
            usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
@@ -474,6 +533,7 @@ static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
        __u16 linkpart;
        __u8 data[4];
        pegasus_t *pegasus = netdev_priv(dev);
+       int ret;
 
        read_mii_word(pegasus, pegasus->phy, MII_LPA, &linkpart);
        data[0] = 0xc9;
@@ -487,16 +547,17 @@ static int enable_net_traffic(struct net_device *dev, struct usb_device *usb)
        data[2] = (loopback & 1) ? 0x09 : 0x01;
 
        memcpy(pegasus->eth_regs, data, sizeof (data));
-       set_registers(pegasus, EthCtrl0, 3, data);
+       ret = set_registers(pegasus, EthCtrl0, 3, data);
 
        if (usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS ||
+           usb_dev_id[pegasus->dev_index].vendor == VENDOR_LINKSYS2 ||
            usb_dev_id[pegasus->dev_index].vendor == VENDOR_DLINK) {
                u16 auxmode;
                read_mii_word(pegasus, 0, 0x1b, &auxmode);
                write_mii_word(pegasus, 0, 0x1b, auxmode | 4);
        }
 
-       return 0;
+       return ret;
 }
 
 static void fill_skb_pool(pegasus_t * pegasus)
@@ -545,53 +606,61 @@ static inline struct sk_buff *pull_skb(pegasus_t * pegasus)
        return NULL;
 }
 
-static void read_bulk_callback(struct urb *urb, struct pt_regs *regs)
+static void read_bulk_callback(struct urb *urb)
 {
        pegasus_t *pegasus = urb->context;
        struct net_device *net;
        int rx_status, count = urb->actual_length;
+       u8 *buf = urb->transfer_buffer;
        __u16 pkt_len;
 
-       if (!pegasus || !(pegasus->flags & PEGASUS_RUNNING))
+       if (!pegasus)
                return;
 
        net = pegasus->net;
-       if (!netif_device_present(net))
+       if (!netif_device_present(net) || !netif_running(net))
                return;
 
        switch (urb->status) {
        case 0:
                break;
-       case -ETIMEDOUT:
-               dbg("%s: reset MAC", net->name);
+       case -ETIME:
+               if (netif_msg_rx_err(pegasus))
+                       pr_debug("%s: reset MAC\n", net->name);
                pegasus->flags &= ~PEGASUS_RX_BUSY;
                break;
        case -EPIPE:            /* stall, or disconnect from TT */
                /* FIXME schedule work to clear the halt */
-               warn("%s: no rx stall recovery", net->name);
+               if (netif_msg_rx_err(pegasus))
+                       printk(KERN_WARNING "%s: no rx stall recovery\n",
+                                       net->name);
                return;
        case -ENOENT:
        case -ECONNRESET:
        case -ESHUTDOWN:
-               dbg("%s: rx unlink, %d", net->name, urb->status);
+               if (netif_msg_ifdown(pegasus))
+                       pr_debug("%s: rx unlink, %d\n", net->name, urb->status);
                return;
        default:
-               dbg("%s: RX status %d", net->name, urb->status);
+               if (netif_msg_rx_err(pegasus))
+                       pr_debug("%s: RX status %d\n", net->name, urb->status);
                goto goon;
        }
 
-       if (!count)
+       if (!count || count < 4)
                goto goon;
 
-       rx_status = le32_to_cpu(*(__le32 *) (urb->transfer_buffer + count - 4));
-       if (rx_status & 0x000e0000) {
-               dbg("%s: RX packet error %x", net->name, rx_status & 0xe0000);
+       rx_status = buf[count - 2];
+       if (rx_status & 0x1e) {
+               if (netif_msg_rx_err(pegasus))
+                       pr_debug("%s: RX packet error %x\n",
+                                       net->name, rx_status);
                pegasus->stats.rx_errors++;
-               if (rx_status & 0x060000)
+               if (rx_status & 0x06)   // long or runt
                        pegasus->stats.rx_length_errors++;
-               if (rx_status & 0x080000)
+               if (rx_status & 0x08)
                        pegasus->stats.rx_crc_errors++;
-               if (rx_status & 0x100000)
+               if (rx_status & 0x10)   // extra bits
                        pegasus->stats.rx_frame_errors++;
                goto goon;
        }
@@ -600,9 +669,19 @@ static void read_bulk_callback(struct urb *urb, struct pt_regs *regs)
                pkt_len &= 0x0fff;
                pegasus->rx_skb->data += 2;
        } else {
-               pkt_len = (rx_status & 0xfff) - 8;
+               pkt_len = buf[count - 3] << 8;
+               pkt_len += buf[count - 4];
+               pkt_len &= 0xfff;
+               pkt_len -= 8;
        }
 
+       /*
+        * If the packet is unreasonably long, quietly drop it rather than
+        * kernel panicing by calling skb_put.
+        */
+       if (pkt_len > PEGASUS_MTU)
+               goto goon;
+
        /*
         * at this point we are sure pegasus->rx_skb != NULL
         * so we go ahead and pass up the packet.
@@ -627,7 +706,10 @@ goon:
                          usb_rcvbulkpipe(pegasus->usb, 1),
                          pegasus->rx_skb->data, PEGASUS_MTU + 8,
                          read_bulk_callback, pegasus);
-       if (usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC)) {
+       rx_status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
+       if (rx_status == -ENODEV)
+               netif_device_detach(pegasus->net);
+       else if (rx_status) {
                pegasus->flags |= PEGASUS_RX_URB_FAIL;
                goto tl_sched;
        } else {
@@ -636,7 +718,7 @@ goon:
 
        return;
 
-      tl_sched:
+tl_sched:
        tasklet_schedule(&pegasus->rx_tl);
 }
 
@@ -644,6 +726,7 @@ static void rx_fixup(unsigned long data)
 {
        pegasus_t *pegasus;
        unsigned long flags;
+       int status;
 
        pegasus = (pegasus_t *) data;
        if (pegasus->flags & PEGASUS_UNPLUG)
@@ -658,7 +741,9 @@ static void rx_fixup(unsigned long data)
                pegasus->rx_skb = pull_skb(pegasus);
        }
        if (pegasus->rx_skb == NULL) {
-               warn("wow, low on memory");
+               if (netif_msg_rx_err(pegasus))
+                       printk(KERN_WARNING "%s: low on memory\n",
+                                       pegasus->net->name);
                tasklet_schedule(&pegasus->rx_tl);
                goto done;
        }
@@ -667,7 +752,10 @@ static void rx_fixup(unsigned long data)
                          pegasus->rx_skb->data, PEGASUS_MTU + 8,
                          read_bulk_callback, pegasus);
 try_again:
-       if (usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC)) {
+       status = usb_submit_urb(pegasus->rx_urb, GFP_ATOMIC);
+       if (status == -ENODEV)
+               netif_device_detach(pegasus->net);
+       else if (status) {
                pegasus->flags |= PEGASUS_RX_URB_FAIL;
                tasklet_schedule(&pegasus->rx_tl);
        } else {
@@ -677,30 +765,34 @@ done:
        spin_unlock_irqrestore(&pegasus->rx_pool_lock, flags);
 }
 
-static void write_bulk_callback(struct urb *urb, struct pt_regs *regs)
+static void write_bulk_callback(struct urb *urb)
 {
        pegasus_t *pegasus = urb->context;
        struct net_device *net = pegasus->net;
 
-       if (!pegasus || !(pegasus->flags & PEGASUS_RUNNING))
+       if (!pegasus)
                return;
 
-       if (!netif_device_present(net))
+       if (!netif_device_present(net) || !netif_running(net))
                return;
 
        switch (urb->status) {
        case -EPIPE:
                /* FIXME schedule_work() to clear the tx halt */
                netif_stop_queue(net);
-               warn("%s: no tx stall recovery", net->name);
+               if (netif_msg_tx_err(pegasus))
+                       printk(KERN_WARNING "%s: no tx stall recovery\n",
+                                       net->name);
                return;
        case -ENOENT:
        case -ECONNRESET:
        case -ESHUTDOWN:
-               dbg("%s: tx unlink, %d", net->name, urb->status);
+               if (netif_msg_ifdown(pegasus))
+                       pr_debug("%s: tx unlink, %d\n", net->name, urb->status);
                return;
        default:
-               info("%s: TX status %d", net->name, urb->status);
+               if (netif_msg_tx_err(pegasus))
+                       pr_info("%s: TX status %d\n", net->name, urb->status);
                /* FALL THROUGH */
        case 0:
                break;
@@ -710,7 +802,7 @@ static void write_bulk_callback(struct urb *urb, struct pt_regs *regs)
        netif_wake_queue(net);
 }
 
-static void intr_callback(struct urb *urb, struct pt_regs *regs)
+static void intr_callback(struct urb *urb)
 {
        pegasus_t *pegasus = urb->context;
        struct net_device *net;
@@ -731,7 +823,9 @@ static void intr_callback(struct urb *urb, struct pt_regs *regs)
                /* some Pegasus-I products report LOTS of data
                 * toggle errors... avoid log spamming
                 */
-               pr_debug("%s: intr status %d\n", net->name, urb->status);
+               if (netif_msg_timer(pegasus))
+                       pr_debug("%s: intr status %d\n", net->name,
+                                       urb->status);
        }
 
        if (urb->actual_length >= 6) {
@@ -762,8 +856,10 @@ static void intr_callback(struct urb *urb, struct pt_regs *regs)
                pegasus->stats.rx_missed_errors += ((d[3] & 0x7f) << 8) | d[4];
        }
 
-       status = usb_submit_urb(urb, SLAB_ATOMIC);
-       if (status)
+       status = usb_submit_urb(urb, GFP_ATOMIC);
+       if (status == -ENODEV)
+               netif_device_detach(pegasus->net);
+       if (status && netif_msg_timer(pegasus))
                printk(KERN_ERR "%s: can't resubmit interrupt urb, %d\n",
                                net->name, status);
 }
@@ -771,8 +867,8 @@ static void intr_callback(struct urb *urb, struct pt_regs *regs)
 static void pegasus_tx_timeout(struct net_device *net)
 {
        pegasus_t *pegasus = netdev_priv(net);
-       printk(KERN_WARNING "%s: tx timeout\n", net->name);
-       pegasus->tx_urb->transfer_flags |= URB_ASYNC_UNLINK;
+       if (netif_msg_timer(pegasus))
+               printk(KERN_WARNING "%s: tx timeout\n", net->name);
        usb_unlink_urb(pegasus->tx_urb);
        pegasus->stats.tx_errors++;
 }
@@ -793,12 +889,15 @@ static int pegasus_start_xmit(struct sk_buff *skb, struct net_device *net)
                          pegasus->tx_buff, count,
                          write_bulk_callback, pegasus);
        if ((res = usb_submit_urb(pegasus->tx_urb, GFP_ATOMIC))) {
-               warn("failed tx_urb %d", res);
+               if (netif_msg_tx_err(pegasus))
+                       printk(KERN_WARNING "%s: fail tx, %d\n",
+                                       net->name, res);
                switch (res) {
                case -EPIPE:            /* stall, or disconnect from TT */
                        /* cleanup should already have been scheduled */
                        break;
                case -ENODEV:           /* disconnect() upcoming */
+                       netif_device_detach(pegasus->net);
                        break;
                default:
                        pegasus->stats.tx_errors++;
@@ -831,13 +930,17 @@ static inline void get_interrupt_interval(pegasus_t * pegasus)
        __u8 data[2];
 
        read_eprom_word(pegasus, 4, (__u16 *) data);
-       if (data[1] < 0x80) {
-               info("intr interval will be changed from %ums to %ums",
-                    data[1], 0x80);
-               data[1] = 0x80;
-#ifdef PEGASUS_WRITE_EEPROM
-               write_eprom_word(pegasus, 4, *(__u16 *) data);
+       if (pegasus->usb->speed != USB_SPEED_HIGH) {
+               if (data[1] < 0x80) {
+                       if (netif_msg_timer(pegasus))
+                               dev_info(&pegasus->intf->dev, "intr interval "
+                                       "changed from %ums to %ums\n",
+                                       data[1], 0x80);
+                       data[1] = 0x80;
+#ifdef PEGASUS_WRITE_EEPROM
+                       write_eprom_word(pegasus, 4, *(__u16 *) data);
 #endif
+               }
        }
        pegasus->intr_interval = data[1];
 }
@@ -845,14 +948,15 @@ static inline void get_interrupt_interval(pegasus_t * pegasus)
 static void set_carrier(struct net_device *net)
 {
        pegasus_t *pegasus = netdev_priv(net);
-       short tmp;
+       u16 tmp;
+
+       if (!read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp))
+               return;
 
-       read_mii_word(pegasus, pegasus->phy, MII_BMSR, &tmp);
        if (tmp & BMSR_LSTATUS)
                netif_carrier_on(net);
        else
                netif_carrier_off(net);
-
 }
 
 static void free_all_urbs(pegasus_t * pegasus)
@@ -912,24 +1016,36 @@ static int pegasus_open(struct net_device *net)
        if (!pegasus->rx_skb)
                return -ENOMEM;
 
-       set_registers(pegasus, EthID, 6, net->dev_addr);
+       res = set_registers(pegasus, EthID, 6, net->dev_addr);
        
        usb_fill_bulk_urb(pegasus->rx_urb, pegasus->usb,
                          usb_rcvbulkpipe(pegasus->usb, 1),
                          pegasus->rx_skb->data, PEGASUS_MTU + 8,
                          read_bulk_callback, pegasus);
-       if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL)))
-               warn("%s: failed rx_urb %d", __FUNCTION__, res);
+       if ((res = usb_submit_urb(pegasus->rx_urb, GFP_KERNEL))) {
+               if (res == -ENODEV)
+                       netif_device_detach(pegasus->net);
+               if (netif_msg_ifup(pegasus))
+                       pr_debug("%s: failed rx_urb, %d", net->name, res);
+               goto exit;
+       }
+
        usb_fill_int_urb(pegasus->intr_urb, pegasus->usb,
                         usb_rcvintpipe(pegasus->usb, 3),
                         pegasus->intr_buff, sizeof (pegasus->intr_buff),
                         intr_callback, pegasus, pegasus->intr_interval);
-       if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL)))
-               warn("%s: failed intr_urb %d", __FUNCTION__, res);
-       netif_start_queue(net);
-       pegasus->flags |= PEGASUS_RUNNING;
+       if ((res = usb_submit_urb(pegasus->intr_urb, GFP_KERNEL))) {
+               if (res == -ENODEV)
+                       netif_device_detach(pegasus->net);
+               if (netif_msg_ifup(pegasus))
+                       pr_debug("%s: failed intr_urb, %d\n", net->name, res);
+               usb_kill_urb(pegasus->rx_urb);
+               goto exit;
+       }
        if ((res = enable_net_traffic(net, pegasus->usb))) {
-               err("can't enable_net_traffic() - %d", res);
+               if (netif_msg_ifup(pegasus))
+                       pr_debug("%s: can't enable_net_traffic() - %d\n",
+                                       net->name, res);
                res = -EIO;
                usb_kill_urb(pegasus->rx_urb);
                usb_kill_urb(pegasus->intr_urb);
@@ -937,6 +1053,9 @@ static int pegasus_open(struct net_device *net)
                goto exit;
        }
        set_carrier(net);
+       netif_start_queue(net);
+       if (netif_msg_ifup(pegasus))
+               pr_debug("%s: open\n", net->name);
        res = 0;
 exit:
        return res;
@@ -946,7 +1065,6 @@ static int pegasus_close(struct net_device *net)
 {
        pegasus_t *pegasus = netdev_priv(net);
 
-       pegasus->flags &= ~PEGASUS_RUNNING;
        netif_stop_queue(net);
        if (!(pegasus->flags & PEGASUS_UNPLUG))
                disable_net_traffic(pegasus);
@@ -956,7 +1074,8 @@ static int pegasus_close(struct net_device *net)
        return 0;
 }
 
-void pegasus_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
+static void pegasus_get_drvinfo(struct net_device *dev,
+                               struct ethtool_drvinfo *info)
 {
        pegasus_t *pegasus = netdev_priv(dev);
        strncpy(info->driver, driver_name, sizeof (info->driver) - 1);
@@ -989,6 +1108,7 @@ pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
                reg78 |= 0x80;
        if (wol->wolopts & WAKE_PHY)
                reg78 |= 0x40;
+       /* FIXME this 0x10 bit still needs to get set in the chip... */
        if (wol->wolopts)
                pegasus->eth_regs[0] |= 0x10;
        else
@@ -997,8 +1117,7 @@ pegasus_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
        return set_register(pegasus, WakeupControl, reg78);
 }
 
-static inline void
-pegasus_reset_wol(struct net_device *dev)
+static inline void pegasus_reset_wol(struct net_device *dev)
 {
        struct ethtool_wolinfo wol;
        
@@ -1009,10 +1128,17 @@ pegasus_reset_wol(struct net_device *dev)
 static int
 pegasus_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
 {
-       pegasus_t *pegasus = netdev_priv(dev);
+       pegasus_t *pegasus;
+
+       if (in_atomic())
+               return 0;
+
+       pegasus = netdev_priv(dev);
        mii_ethtool_gset(&pegasus->mii, ecmd);
+
        return 0;
 }
+
 static int
 pegasus_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
 {
@@ -1035,13 +1161,13 @@ static u32 pegasus_get_link(struct net_device *dev)
 static u32 pegasus_get_msglevel(struct net_device *dev)
 {
        pegasus_t *pegasus = netdev_priv(dev);
-       return pegasus->msg_level;
+       return pegasus->msg_enable;
 }
 
 static void pegasus_set_msglevel(struct net_device *dev, u32 v)
 {
        pegasus_t *pegasus = netdev_priv(dev);
-       pegasus->msg_level = v;
+       pegasus->msg_enable = v;
 }
 
 static struct ethtool_ops ops = {
@@ -1087,19 +1213,21 @@ static void pegasus_set_multicast(struct net_device *net)
 
        if (net->flags & IFF_PROMISC) {
                pegasus->eth_regs[EthCtrl2] |= RX_PROMISCUOUS;
-               pr_info("%s: Promiscuous mode enabled.\n", net->name);
-       } else if ((net->mc_count > multicast_filter_limit) ||
+               if (netif_msg_link(pegasus))
+                       pr_info("%s: Promiscuous mode enabled.\n", net->name);
+       } else if (net->mc_count ||
                   (net->flags & IFF_ALLMULTI)) {
                pegasus->eth_regs[EthCtrl0] |= RX_MULTICAST;
                pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
-               pr_info("%s: set allmulti\n", net->name);
+               if (netif_msg_link(pegasus))
+                       pr_info("%s: set allmulti\n", net->name);
        } else {
                pegasus->eth_regs[EthCtrl0] &= ~RX_MULTICAST;
                pegasus->eth_regs[EthCtrl2] &= ~RX_PROMISCUOUS;
        }
 
        pegasus->flags |= ETH_REGS_CHANGE;
-       ctrl_callback(pegasus->ctrl_urb, NULL);
+       ctrl_callback(pegasus->ctrl_urb);
 }
 
 static __u8 mii_phy_probe(pegasus_t * pegasus)
@@ -1149,6 +1277,20 @@ static inline void setup_pegasus_II(pegasus_t * pegasus)
                set_register(pegasus, Reg81, 2);
 }
 
+
+static struct workqueue_struct *pegasus_workqueue = NULL;
+#define CARRIER_CHECK_DELAY (2 * HZ)
+
+static void check_carrier(struct work_struct *work)
+{
+       pegasus_t *pegasus = container_of(work, pegasus_t, carrier_check.work);
+       set_carrier(pegasus->net);
+       if (!(pegasus->flags & PEGASUS_UNPLUG)) {
+               queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
+                       CARRIER_CHECK_DELAY);
+       }
+}
+
 static int pegasus_probe(struct usb_interface *intf,
                         const struct usb_device_id *id)
 {
@@ -1161,7 +1303,7 @@ static int pegasus_probe(struct usb_interface *intf,
        usb_get_dev(dev);
        net = alloc_etherdev(sizeof(struct pegasus));
        if (!net) {
-               err("out of memory allocating device structure");
+               dev_err(&intf->dev, "can't allocate %s\n", "device");
                goto out;
        }
 
@@ -1170,11 +1312,16 @@ static int pegasus_probe(struct usb_interface *intf,
        pegasus->dev_index = dev_index;
        init_waitqueue_head(&pegasus->ctrl_wait);
 
-       if (!alloc_urbs(pegasus))
+       if (!alloc_urbs(pegasus)) {
+               dev_err(&intf->dev, "can't allocate %s\n", "urbs");
                goto out1;
+       }
 
        tasklet_init(&pegasus->rx_tl, rx_fixup, (unsigned long) pegasus);
 
+       INIT_DELAYED_WORK(&pegasus->carrier_check, check_carrier);
+
+       pegasus->intf = intf;
        pegasus->usb = dev;
        pegasus->net = net;
        SET_MODULE_OWNER(net);
@@ -1193,6 +1340,8 @@ static int pegasus_probe(struct usb_interface *intf,
        pegasus->mii.phy_id_mask = 0x1f;
        pegasus->mii.reg_num_mask = 0x1f;
        spin_lock_init(&pegasus->rx_pool_lock);
+       pegasus->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
+                               | NETIF_MSG_PROBE | NETIF_MSG_LINK);
 
        pegasus->features = usb_dev_id[dev_index].private;
        get_interrupt_interval(pegasus);
@@ -1212,13 +1361,22 @@ static int pegasus_probe(struct usb_interface *intf,
                dev_warn(&intf->dev, "can't locate MII phy, using default\n");
                pegasus->phy = 1;
        }
+       pegasus->mii.phy_id = pegasus->phy;
        usb_set_intfdata(intf, pegasus);
        SET_NETDEV_DEV(net, &intf->dev);
        pegasus_reset_wol(net);
        res = register_netdev(net);
        if (res)
                goto out3;
-       pr_info("%s: %s\n", net->name, usb_dev_id[dev_index].name);
+       queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
+                               CARRIER_CHECK_DELAY);
+
+       dev_info(&intf->dev, "%s, %s, %02x:%02x:%02x:%02x:%02x:%02x\n",
+               net->name,
+               usb_dev_id[dev_index].name,
+               net->dev_addr [0], net->dev_addr [1],
+               net->dev_addr [2], net->dev_addr [3],
+               net->dev_addr [4], net->dev_addr [5]);
        return 0;
 
 out3:
@@ -1239,13 +1397,15 @@ static void pegasus_disconnect(struct usb_interface *intf)
 
        usb_set_intfdata(intf, NULL);
        if (!pegasus) {
-               warn("unregistering non-existant device");
+               dev_dbg(&intf->dev, "unregistering non-bound device?\n");
                return;
        }
 
        pegasus->flags |= PEGASUS_UNPLUG;
+       cancel_delayed_work(&pegasus->carrier_check);
        unregister_netdev(pegasus->net);
        usb_put_dev(interface_to_usbdev(intf));
+       unlink_all_urbs(pegasus);
        free_all_urbs(pegasus);
        free_skb_pool(pegasus);
        if (pegasus->rx_skb)
@@ -1253,11 +1413,16 @@ static void pegasus_disconnect(struct usb_interface *intf)
        free_netdev(pegasus->net);
 }
 
-static int pegasus_suspend (struct usb_interface *intf, u32 state)
+static int pegasus_suspend (struct usb_interface *intf, pm_message_t message)
 {
        struct pegasus *pegasus = usb_get_intfdata(intf);
        
        netif_device_detach (pegasus->net);
+       cancel_delayed_work(&pegasus->carrier_check);
+       if (netif_running(pegasus->net)) {
+               usb_kill_urb(pegasus->rx_urb);
+               usb_kill_urb(pegasus->intr_urb);
+       }
        return 0;
 }
 
@@ -1266,6 +1431,17 @@ static int pegasus_resume (struct usb_interface *intf)
        struct pegasus *pegasus = usb_get_intfdata(intf);
 
        netif_device_attach (pegasus->net);
+       if (netif_running(pegasus->net)) {
+               pegasus->rx_urb->status = 0;
+               pegasus->rx_urb->actual_length = 0;
+               read_bulk_callback(pegasus->rx_urb);
+
+               pegasus->intr_urb->status = 0;
+               pegasus->intr_urb->actual_length = 0;
+               intr_callback(pegasus->intr_urb);
+       }
+       queue_delayed_work(pegasus_workqueue, &pegasus->carrier_check,
+                               CARRIER_CHECK_DELAY);
        return 0;
 }
 
@@ -1278,14 +1454,51 @@ static struct usb_driver pegasus_driver = {
        .resume = pegasus_resume,
 };
 
+static void parse_id(char *id)
+{
+       unsigned int vendor_id=0, device_id=0, flags=0, i=0;
+       char *token, *name=NULL;
+
+       if ((token = strsep(&id, ":")) != NULL)
+               name = token;
+       /* name now points to a null terminated string*/
+       if ((token = strsep(&id, ":")) != NULL)
+               vendor_id = simple_strtoul(token, NULL, 16);
+       if ((token = strsep(&id, ":")) != NULL)
+               device_id = simple_strtoul(token, NULL, 16);
+       flags = simple_strtoul(id, NULL, 16);
+       pr_info("%s: new device %s, vendor ID 0x%04x, device ID 0x%04x, flags: 0x%x\n",
+               driver_name, name, vendor_id, device_id, flags);
+
+       if (vendor_id > 0x10000 || vendor_id == 0)
+               return;
+       if (device_id > 0x10000 || device_id == 0)
+               return;
+
+       for (i=0; usb_dev_id[i].name; i++);
+       usb_dev_id[i].name = name;
+       usb_dev_id[i].vendor = vendor_id;
+       usb_dev_id[i].device = device_id;
+       usb_dev_id[i].private = flags;
+       pegasus_ids[i].match_flags = USB_DEVICE_ID_MATCH_DEVICE;
+       pegasus_ids[i].idVendor = vendor_id;
+       pegasus_ids[i].idProduct = device_id;
+}
+
 static int __init pegasus_init(void)
 {
        pr_info("%s: %s, " DRIVER_DESC "\n", driver_name, DRIVER_VERSION);
+       if (devid)
+               parse_id(devid);
+       pegasus_workqueue = create_singlethread_workqueue("pegasus");
+       if (!pegasus_workqueue)
+               return -ENOMEM;
        return usb_register(&pegasus_driver);
 }
 
 static void __exit pegasus_exit(void)
 {
+       destroy_workqueue(pegasus_workqueue);
        usb_deregister(&pegasus_driver);
 }