This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / usb / net / kaweth.c
index 1d16b0c..40e921a 100644 (file)
@@ -246,7 +246,7 @@ struct kaweth_device
        struct kaweth_ethernet_configuration configuration;
 
        struct net_device_stats stats;
-};
+} __attribute__ ((packed));
 
 
 /****************************************************************
@@ -651,7 +651,7 @@ static void kaweth_usb_receive(struct urb *urb, struct pt_regs *regs)
  ****************************************************************/
 static int kaweth_open(struct net_device *net)
 {
-       struct kaweth_device *kaweth = netdev_priv(net);
+       struct kaweth_device *kaweth = (struct kaweth_device *)net->priv;
        int res;
 
        kaweth_dbg("Opening network device.");
@@ -668,13 +668,13 @@ static int kaweth_open(struct net_device *net)
                INTBUFFERSIZE,
                int_callback,
                kaweth,
-               250); /* overriding the descriptor */
+               8);
        kaweth->irq_urb->transfer_dma = kaweth->intbufferhandle;
        kaweth->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
 
        res = usb_submit_urb(kaweth->irq_urb, GFP_KERNEL);
        if (res) {
-               usb_kill_urb(kaweth->rx_urb);
+               usb_unlink_urb(kaweth->rx_urb);
                return -EIO;
        }
 
@@ -689,36 +689,58 @@ static int kaweth_open(struct net_device *net)
  ****************************************************************/
 static int kaweth_close(struct net_device *net)
 {
-       struct kaweth_device *kaweth = netdev_priv(net);
+       struct kaweth_device *kaweth = net->priv;
 
        netif_stop_queue(net);
 
        kaweth->status |= KAWETH_STATUS_CLOSING;
 
-       usb_kill_urb(kaweth->irq_urb);
-       usb_kill_urb(kaweth->rx_urb);
+       usb_unlink_urb(kaweth->irq_urb);
+       usb_unlink_urb(kaweth->rx_urb);
 
        flush_scheduled_work();
 
        /* a scheduled work may have resubmitted,
           we hit them again */
-       usb_kill_urb(kaweth->irq_urb);
-       usb_kill_urb(kaweth->rx_urb);
+       usb_unlink_urb(kaweth->irq_urb);
+       usb_unlink_urb(kaweth->rx_urb);
 
        kaweth->status &= ~KAWETH_STATUS_CLOSING;
 
        return 0;
 }
 
-static void kaweth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
+static int netdev_ethtool_ioctl(struct net_device *dev, void __user *useraddr)
 {
+       u32 ethcmd;
+
+       if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
+               return -EFAULT;
+
+       switch (ethcmd) {
+       case ETHTOOL_GDRVINFO: {
+               struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
+               strlcpy(info.driver, driver_name, sizeof(info.driver));
+               if (copy_to_user(useraddr, &info, sizeof(info)))
+                       return -EFAULT;
+               return 0;
+       }
+       }
 
-       strlcpy(info->driver, driver_name, sizeof(info->driver));
+       return -EOPNOTSUPP;
 }
 
-static struct ethtool_ops ops = {
-       .get_drvinfo = kaweth_get_drvinfo
-};
+/****************************************************************
+ *     kaweth_ioctl
+ ****************************************************************/
+static int kaweth_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
+{
+       switch (cmd) {
+       case SIOCETHTOOL:
+               return netdev_ethtool_ioctl(net, rq->ifr_data);
+       }
+       return -EOPNOTSUPP;
+}
 
 /****************************************************************
  *     kaweth_usb_transmit_complete
@@ -729,8 +751,7 @@ static void kaweth_usb_transmit_complete(struct urb *urb, struct pt_regs *regs)
        struct sk_buff *skb = kaweth->tx_skb;
 
        if (unlikely(urb->status != 0))
-               if (urb->status != -ENOENT)
-                       kaweth_dbg("%s: TX status %d.", kaweth->net->name, urb->status);
+               kaweth_dbg("%s: TX status %d.", kaweth->net->name, urb->status);
 
        netif_wake_queue(kaweth->net);
        dev_kfree_skb_irq(skb);
@@ -741,7 +762,7 @@ static void kaweth_usb_transmit_complete(struct urb *urb, struct pt_regs *regs)
  ****************************************************************/
 static int kaweth_start_xmit(struct sk_buff *skb, struct net_device *net)
 {
-       struct kaweth_device *kaweth = netdev_priv(net);
+       struct kaweth_device *kaweth = net->priv;
        __le16 *private_header;
 
        int res;
@@ -812,7 +833,7 @@ static int kaweth_start_xmit(struct sk_buff *skb, struct net_device *net)
  ****************************************************************/
 static void kaweth_set_rx_mode(struct net_device *net)
 {
-       struct kaweth_device *kaweth = netdev_priv(net);
+       struct kaweth_device *kaweth = net->priv;
 
        __u16 packet_filter_bitmap = KAWETH_PACKET_FILTER_DIRECTED |
                                      KAWETH_PACKET_FILTER_BROADCAST |
@@ -869,8 +890,7 @@ static void kaweth_async_set_rx_mode(struct kaweth_device *kaweth)
  ****************************************************************/
 static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
 {
-       struct kaweth_device *kaweth = netdev_priv(dev);
-       return &kaweth->stats;
+       return &((struct kaweth_device *)dev->priv)->stats;
 }
 
 /****************************************************************
@@ -878,7 +898,7 @@ static struct net_device_stats *kaweth_netdev_stats(struct net_device *dev)
  ****************************************************************/
 static void kaweth_tx_timeout(struct net_device *net)
 {
-       struct kaweth_device *kaweth = netdev_priv(net);
+       struct kaweth_device *kaweth = net->priv;
 
        kaweth_warn("%s: Tx timed out. Resetting.", net->name);
        kaweth->stats.tx_errors++;
@@ -913,14 +933,12 @@ static int kaweth_probe(
                 (int)dev->descriptor.bLength,
                 (int)dev->descriptor.bDescriptorType);
 
-       netdev = alloc_etherdev(sizeof(*kaweth));
-       if (!netdev)
+       if(!(kaweth = kmalloc(sizeof(struct kaweth_device), GFP_KERNEL)))
                return -ENOMEM;
 
-       kaweth = netdev_priv(netdev);
-       kaweth->dev = dev;
-       kaweth->net = netdev;
+       memset(kaweth, 0, sizeof(struct kaweth_device));
 
+       kaweth->dev = dev;
        spin_lock_init(&kaweth->device_lock);
        init_waitqueue_head(&kaweth->term_wait);
 
@@ -945,7 +963,9 @@ static int kaweth_probe(
                                                      100,
                                                      2)) < 0) {
                        kaweth_err("Error downloading firmware (%d)", result);
-                       goto err_fw;
+                       free_page((unsigned long)kaweth->firmware_buf);
+                       kfree(kaweth);
+                       return -EIO;
                }
 
                if ((result = kaweth_download_firmware(kaweth,
@@ -954,7 +974,9 @@ static int kaweth_probe(
                                                      100,
                                                      3)) < 0) {
                        kaweth_err("Error downloading firmware fix (%d)", result);
-                       goto err_fw;
+                       free_page((unsigned long)kaweth->firmware_buf);
+                       kfree(kaweth);
+                       return -EIO;
                }
 
                if ((result = kaweth_download_firmware(kaweth,
@@ -963,8 +985,9 @@ static int kaweth_probe(
                                                      126,
                                                      2)) < 0) {
                        kaweth_err("Error downloading trigger code (%d)", result);
-                       goto err_fw;
-
+                       free_page((unsigned long)kaweth->firmware_buf);
+                       kfree(kaweth);
+                       return -EIO;
                }
 
                if ((result = kaweth_download_firmware(kaweth,
@@ -973,20 +996,23 @@ static int kaweth_probe(
                                                      126,
                                                      3)) < 0) {
                        kaweth_err("Error downloading trigger code fix (%d)", result);
-                       goto err_fw;
+                       free_page((unsigned long)kaweth->firmware_buf);
+                       kfree(kaweth);
+                       return -EIO;
                }
 
 
                if ((result = kaweth_trigger_firmware(kaweth, 126)) < 0) {
                        kaweth_err("Error triggering firmware (%d)", result);
-                       goto err_fw;
+                       free_page((unsigned long)kaweth->firmware_buf);
+                       kfree(kaweth);
+                       return -EIO;
                }
 
                /* Device will now disappear for a moment...  */
                kaweth_info("Firmware loaded.  I'll be back...");
-err_fw:
                free_page((unsigned long)kaweth->firmware_buf);
-               free_netdev(netdev);
+               kfree(kaweth);
                return -EIO;
        }
 
@@ -994,7 +1020,8 @@ err_fw:
 
        if(result < 0) {
                kaweth_err("Error reading configuration (%d), no net device created", result);
-               goto err_free_netdev;
+               kfree(kaweth);
+               return -EIO;
        }
 
        kaweth_info("Statistics collection: %x", kaweth->configuration.statistics_mask);
@@ -1012,17 +1039,18 @@ err_fw:
                    &bcast_addr,
                   sizeof(bcast_addr))) {
                kaweth_err("Firmware not functioning properly, no net device created");
-               goto err_free_netdev;
+               kfree(kaweth);
+               return -EIO;
        }
 
        if(kaweth_set_urb_size(kaweth, KAWETH_BUF_SIZE) < 0) {
                kaweth_dbg("Error setting URB size");
-               goto err_free_netdev;
+               goto err_no_netdev;
        }
 
        if(kaweth_set_sofs_wait(kaweth, KAWETH_SOFS_TO_WAIT) < 0) {
                kaweth_err("Error setting SOFS wait");
-               goto err_free_netdev;
+               goto err_no_netdev;
        }
 
        result = kaweth_set_receive_filter(kaweth,
@@ -1032,14 +1060,20 @@ err_fw:
 
        if(result < 0) {
                kaweth_err("Error setting receive filter");
-               goto err_free_netdev;
+               kfree(kaweth);
+               return -EIO;
        }
 
        kaweth_dbg("Initializing net device.");
 
+       if (!(netdev = alloc_etherdev(0))) {
+               kfree(kaweth);
+               return -ENOMEM;
+       }
+
        kaweth->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
        if (!kaweth->tx_urb)
-               goto err_free_netdev;
+               goto err_no_urb;
        kaweth->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
        if (!kaweth->rx_urb)
                goto err_only_tx;
@@ -1060,23 +1094,26 @@ err_fw:
        if (!kaweth->rx_buf)
                goto err_all_but_rxbuf;
 
-       memcpy(netdev->broadcast, &bcast_addr, sizeof(bcast_addr));
-       memcpy(netdev->dev_addr, &kaweth->configuration.hw_addr,
+       kaweth->net = netdev;
+       memcpy(kaweth->net->broadcast, &bcast_addr, sizeof(bcast_addr));
+       memcpy(kaweth->net->dev_addr,
+               &kaweth->configuration.hw_addr,
                sizeof(kaweth->configuration.hw_addr));
 
-       netdev->open = kaweth_open;
-       netdev->stop = kaweth_close;
+       kaweth->net->priv = kaweth;
+       kaweth->net->open = kaweth_open;
+       kaweth->net->stop = kaweth_close;
 
-       netdev->watchdog_timeo = KAWETH_TX_TIMEOUT;
-       netdev->tx_timeout = kaweth_tx_timeout;
+       kaweth->net->watchdog_timeo = KAWETH_TX_TIMEOUT;
+       kaweth->net->tx_timeout = kaweth_tx_timeout;
 
-       netdev->hard_start_xmit = kaweth_start_xmit;
-       netdev->set_multicast_list = kaweth_set_rx_mode;
-       netdev->get_stats = kaweth_netdev_stats;
-       netdev->mtu = le16_to_cpu(kaweth->configuration.segment_size);
-       SET_ETHTOOL_OPS(netdev, &ops);
+       kaweth->net->do_ioctl = kaweth_ioctl;
+       kaweth->net->hard_start_xmit = kaweth_start_xmit;
+       kaweth->net->set_multicast_list = kaweth_set_rx_mode;
+       kaweth->net->get_stats = kaweth_netdev_stats;
+       kaweth->net->mtu = le16_to_cpu(kaweth->configuration.segment_size);
 
-       /* kaweth is zeroed as part of alloc_netdev */
+       memset(&kaweth->stats, 0, sizeof(kaweth->stats));
 
        INIT_WORK(&kaweth->lowmem_work, kaweth_resubmit_tl, (void *)kaweth);
 
@@ -1113,9 +1150,10 @@ err_tx_and_rx:
        usb_free_urb(kaweth->rx_urb);
 err_only_tx:
        usb_free_urb(kaweth->tx_urb);
-err_free_netdev:
+err_no_urb:
        free_netdev(netdev);
-
+err_no_netdev:
+       kfree(kaweth);
        return -EIO;
 }
 
@@ -1125,7 +1163,6 @@ err_free_netdev:
 static void kaweth_disconnect(struct usb_interface *intf)
 {
        struct kaweth_device *kaweth = usb_get_intfdata(intf);
-       struct net_device *netdev;
 
        kaweth_info("Unregistering");
 
@@ -1134,23 +1171,40 @@ static void kaweth_disconnect(struct usb_interface *intf)
                kaweth_warn("unregistering non-existant device");
                return;
        }
-       netdev = kaweth->net;
+
        kaweth->removed = 1;
-       usb_kill_urb(kaweth->irq_urb);
-       usb_kill_urb(kaweth->rx_urb);
-       usb_kill_urb(kaweth->tx_urb);
+       usb_unlink_urb(kaweth->irq_urb);
+       usb_unlink_urb(kaweth->rx_urb);
+
+       /* we need to wait for the urb to be cancelled, if it is active */
+       spin_lock(&kaweth->device_lock);
+       if (usb_unlink_urb(kaweth->tx_urb) == -EINPROGRESS) {
+               spin_unlock(&kaweth->device_lock);
+               wait_event(kaweth->term_wait, kaweth->end);
+       } else {
+               spin_unlock(&kaweth->device_lock);
+       }
+
+       if(kaweth->net) {
+               if(kaweth->net->flags & IFF_UP) {
+                       kaweth_dbg("Closing net device");
+                       dev_close(kaweth->net);
+               }
 
-       kaweth_dbg("Unregistering net device");
-       unregister_netdev(netdev);
+               kaweth_dbg("Unregistering net device");
+               unregister_netdev(kaweth->net);
+               free_netdev(kaweth->net);
+       }
 
        usb_free_urb(kaweth->rx_urb);
        usb_free_urb(kaweth->tx_urb);
        usb_free_urb(kaweth->irq_urb);
 
+
        usb_buffer_free(kaweth->dev, KAWETH_BUF_SIZE, (void *)kaweth->rx_buf, kaweth->rxbufferhandle);
        usb_buffer_free(kaweth->dev, INTBUFFERSIZE, (void *)kaweth->intbuffer, kaweth->intbufferhandle);
 
-       free_netdev(netdev);
+       kfree(kaweth);
 }
 
 
@@ -1196,17 +1250,19 @@ static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
                 return status;
         }
 
+       set_current_state(TASK_UNINTERRUPTIBLE);
        while (timeout && !awd.done) {
-               set_current_state(TASK_UNINTERRUPTIBLE);
                timeout = schedule_timeout(timeout);
+               set_current_state(TASK_UNINTERRUPTIBLE);
        }
 
+        set_current_state(TASK_RUNNING);
         remove_wait_queue(&awd.wqh, &wait);
 
         if (!timeout) {
                 // timeout
                 kaweth_warn("usb_control/bulk_msg: timeout");
-                usb_kill_urb(urb);  // remove urb safely
+                usb_unlink_urb(urb);  // remove urb safely
                 status = -ETIMEDOUT;
         }
        else {