Commit c031637c authored by Petko Manolov's avatar Petko Manolov Committed by Greg Kroah-Hartman

[PATCH] rtl8150 for 2.5

This one is fixing the problems Oliver spoted.  Namely:
	- double free in probe();
	- Rx skb pool resolves the issue with OOM condition in
	   read_bulk_callback() which potentially kills the Rx path;
parent 39224417
...@@ -21,14 +21,11 @@ ...@@ -21,14 +21,11 @@
#include <linux/usb.h> #include <linux/usb.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
/* Version Information */ /* Version Information */
#define DRIVER_VERSION "v0.5.0 (2002/03/28)" #define DRIVER_VERSION "v0.5.3 (2002/04/08)"
#define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>" #define DRIVER_AUTHOR "Petko Manolov <petkan@users.sourceforge.net>"
#define DRIVER_DESC "rtl8150 based usb-ethernet driver" #define DRIVER_DESC "rtl8150 based usb-ethernet driver"
#define IRD 0x0120 #define IRD 0x0120
#define MAR 0x0126 #define MAR 0x0126
#define CR 0x012e #define CR 0x012e
...@@ -53,65 +50,65 @@ ...@@ -53,65 +50,65 @@
#define PHY_WRITE 0x20 #define PHY_WRITE 0x20
#define PHY_GO 0x40 #define PHY_GO 0x40
#define MII_TIMEOUT 10
#define RTL8150_REQT_READ 0xc0 #define RTL8150_REQT_READ 0xc0
#define RTL8150_REQT_WRITE 0x40 #define RTL8150_REQT_WRITE 0x40
#define RTL8150_REQ_GET_REGS 0x05 #define RTL8150_REQ_GET_REGS 0x05
#define RTL8150_REQ_SET_REGS 0x05 #define RTL8150_REQ_SET_REGS 0x05
#define RTL8150_MTU 1500 #define RTL8150_MTU 1540
#define RTL8150_MAX_MTU 1536
#define RTL8150_TX_TIMEOUT (HZ) #define RTL8150_TX_TIMEOUT (HZ)
#define RX_SKB_POOL_SIZE 4
/* rtl8150 flags */ /* rtl8150 flags */
#define RTL8150_FLAG_HWCRC 0 #define RTL8150_HW_CRC 0
#define RX_REG_SET 1 #define RX_REG_SET 1
#define RTL8150_UNPLUG 2 #define RTL8150_UNPLUG 2
/* Define these values to match your device */ /* Define these values to match your device */
#define VENDOR_ID_REALTEK 0x0bda #define VENDOR_ID_REALTEK 0x0bda
#define PRODUCT_ID_RTL8150 0x8150 #define PRODUCT_ID_RTL8150 0x8150
/* table of devices that work with this driver */ /* table of devices that work with this driver */
static struct usb_device_id rtl8150_table [] = { static struct usb_device_id rtl8150_table[] = {
{ USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8150) }, {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8150)},
{ } {}
}; };
MODULE_DEVICE_TABLE (usb, rtl8150_table); MODULE_DEVICE_TABLE(usb, rtl8150_table);
struct rtl8150 { struct rtl8150 {
unsigned int flags; unsigned long flags;
struct usb_device *udev; struct usb_device *udev;
struct usb_interface *interface;
struct semaphore sem; struct semaphore sem;
struct tasklet_struct tl;
struct net_device_stats stats; struct net_device_stats stats;
struct net_device *netdev; struct net_device *netdev;
struct urb *rx_urb, *tx_urb, *intr_urb, *ctrl_urb; struct urb *rx_urb, *tx_urb, *intr_urb, *ctrl_urb;
struct sk_buff *tx_skb, *rx_skb;
struct sk_buff *rx_skb_pool[RX_SKB_POOL_SIZE];
spinlock_t rx_pool_lock;
struct usb_ctrlrequest dr; struct usb_ctrlrequest dr;
int intr_interval; int intr_interval;
u16 rx_creg; u16 rx_creg;
u8 rx_buff[RTL8150_MAX_MTU];
u8 tx_buff[RTL8150_MAX_MTU];
u8 intr_buff[8]; u8 intr_buff[8];
u8 phy; u8 phy;
}; };
typedef struct rtl8150 rtl8150_t; typedef struct rtl8150 rtl8150_t;
/* the global usb devfs handle */ /* the global usb devfs handle */
extern devfs_handle_t usb_devfs_handle; extern devfs_handle_t usb_devfs_handle;
unsigned long multicast_filter_limit = 32; unsigned long multicast_filter_limit = 32;
static void fill_skb_pool(rtl8150_t *);
static void free_skb_pool(rtl8150_t *);
static struct sk_buff *pull_skb(rtl8150_t *);
static void rtl8150_disconnect(struct usb_device *dev, void *ptr); static void rtl8150_disconnect(struct usb_device *dev, void *ptr);
static void * rtl8150_probe(struct usb_device *dev, unsigned int ifnum, static void *rtl8150_probe(struct usb_device *dev, unsigned int ifnum,
const struct usb_device_id *id); const struct usb_device_id *id);
static struct usb_driver rtl8150_driver = { static struct usb_driver rtl8150_driver = {
name: "rtl8150", name: "rtl8150",
probe: rtl8150_probe, probe: rtl8150_probe,
...@@ -119,29 +116,25 @@ static struct usb_driver rtl8150_driver = { ...@@ -119,29 +116,25 @@ static struct usb_driver rtl8150_driver = {
id_table: rtl8150_table, id_table: rtl8150_table,
}; };
/* /*
** **
** device related part of the code ** device related part of the code
** **
*/ */
static int get_registers(rtl8150_t *dev, u16 indx, u16 size, void *data) static int get_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
{ {
return usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev,0), return usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
RTL8150_REQ_GET_REGS, RTL8150_REQT_READ, RTL8150_REQ_GET_REGS, RTL8150_REQT_READ,
indx, 0, data, size, HZ/2); indx, 0, data, size, HZ / 2);
} }
static int set_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
static int set_registers(rtl8150_t *dev, u16 indx, u16 size, void *data)
{ {
return usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev,0), return usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
RTL8150_REQ_SET_REGS, RTL8150_REQT_WRITE, RTL8150_REQ_SET_REGS, RTL8150_REQT_WRITE,
indx, 0, data, size, HZ/2); indx, 0, data, size, HZ / 2);
} }
static void ctrl_callback(struct urb *urb) static void ctrl_callback(struct urb *urb)
{ {
rtl8150_t *dev; rtl8150_t *dev;
...@@ -160,8 +153,7 @@ static void ctrl_callback(struct urb *urb) ...@@ -160,8 +153,7 @@ static void ctrl_callback(struct urb *urb)
clear_bit(RX_REG_SET, &dev->flags); clear_bit(RX_REG_SET, &dev->flags);
} }
static int async_set_registers(rtl8150_t * dev, u16 indx, u16 size, void *data)
static int async_set_registers(rtl8150_t *dev, u16 indx, u16 size, void *data)
{ {
int ret; int ret;
...@@ -174,9 +166,9 @@ static int async_set_registers(rtl8150_t *dev, u16 indx, u16 size, void *data) ...@@ -174,9 +166,9 @@ static int async_set_registers(rtl8150_t *dev, u16 indx, u16 size, void *data)
dev->dr.wIndex = 0; dev->dr.wIndex = 0;
dev->dr.wLength = cpu_to_le16(size); dev->dr.wLength = cpu_to_le16(size);
dev->ctrl_urb->transfer_buffer_length = size; dev->ctrl_urb->transfer_buffer_length = size;
FILL_CONTROL_URB(dev->ctrl_urb, dev->udev, usb_sndctrlpipe(dev->udev,0), FILL_CONTROL_URB(dev->ctrl_urb, dev->udev,
(char*)&dev->dr, &dev->rx_creg, size, usb_sndctrlpipe(dev->udev, 0), (char *) &dev->dr,
ctrl_callback, dev); &dev->rx_creg, size, ctrl_callback, dev);
if ((ret = usb_submit_urb(dev->ctrl_urb, GFP_ATOMIC))) if ((ret = usb_submit_urb(dev->ctrl_urb, GFP_ATOMIC)))
err("control request submission failed: %d", ret); err("control request submission failed: %d", ret);
else else
...@@ -185,8 +177,7 @@ static int async_set_registers(rtl8150_t *dev, u16 indx, u16 size, void *data) ...@@ -185,8 +177,7 @@ static int async_set_registers(rtl8150_t *dev, u16 indx, u16 size, void *data)
return ret; return ret;
} }
static int read_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 * reg)
static int read_mii_word(rtl8150_t *dev, u8 phy, __u8 indx, u16 *reg)
{ {
int i; int i;
u8 data[3], tmp; u8 data[3], tmp;
...@@ -200,9 +191,9 @@ static int read_mii_word(rtl8150_t *dev, u8 phy, __u8 indx, u16 *reg) ...@@ -200,9 +191,9 @@ static int read_mii_word(rtl8150_t *dev, u8 phy, __u8 indx, u16 *reg)
set_registers(dev, PHYCNT, 1, &tmp); set_registers(dev, PHYCNT, 1, &tmp);
do { do {
get_registers(dev, PHYCNT, 1, data); get_registers(dev, PHYCNT, 1, data);
} while ((data[0] & PHY_GO) && (i++ < HZ)); } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
if (i < HZ) { if (i < MII_TIMEOUT) {
get_registers(dev, PHYDAT, 2, data); get_registers(dev, PHYDAT, 2, data);
*reg = le16_to_cpup(data); *reg = le16_to_cpup(data);
return 0; return 0;
...@@ -210,8 +201,7 @@ static int read_mii_word(rtl8150_t *dev, u8 phy, __u8 indx, u16 *reg) ...@@ -210,8 +201,7 @@ static int read_mii_word(rtl8150_t *dev, u8 phy, __u8 indx, u16 *reg)
return 1; return 1;
} }
static int write_mii_word(rtl8150_t * dev, u8 phy, __u8 indx, u16 reg)
static int write_mii_word(rtl8150_t *dev, u8 phy, __u8 indx, u16 reg)
{ {
int i; int i;
u8 data[3], tmp; u8 data[3], tmp;
...@@ -225,16 +215,15 @@ static int write_mii_word(rtl8150_t *dev, u8 phy, __u8 indx, u16 reg) ...@@ -225,16 +215,15 @@ static int write_mii_word(rtl8150_t *dev, u8 phy, __u8 indx, u16 reg)
set_registers(dev, PHYCNT, 1, &tmp); set_registers(dev, PHYCNT, 1, &tmp);
do { do {
get_registers(dev, PHYCNT, 1, data); get_registers(dev, PHYCNT, 1, data);
} while((data[0] & PHY_GO) && (i++ < HZ)); } while ((data[0] & PHY_GO) && (i++ < MII_TIMEOUT));
if (i < HZ) if (i < MII_TIMEOUT)
return 0; return 0;
else else
return 1; return 1;
} }
static inline void set_ethernet_addr(rtl8150_t * dev)
static inline void set_ethernet_addr(rtl8150_t *dev)
{ {
u8 node_id[6]; u8 node_id[6];
...@@ -242,22 +231,20 @@ static inline void set_ethernet_addr(rtl8150_t *dev) ...@@ -242,22 +231,20 @@ static inline void set_ethernet_addr(rtl8150_t *dev)
memcpy(dev->netdev->dev_addr, node_id, sizeof(node_id)); memcpy(dev->netdev->dev_addr, node_id, sizeof(node_id));
} }
static int rtl8150_reset(rtl8150_t * dev)
static int rtl8150_reset(rtl8150_t *dev)
{ {
u8 data=0x10; u8 data = 0x10;
int i=HZ; int i = HZ;
set_registers(dev, CR, 1, &data); set_registers(dev, CR, 1, &data);
do { do {
get_registers(dev, CR, 1, &data); get_registers(dev, CR, 1, &data);
} while ((data & 0x10) && --i); } while ((data & 0x10) && --i);
return (i > 0) ? 0 : -1; return (i > 0) ? 1 : 0;
} }
static int alloc_all_urbs(rtl8150_t * dev)
static int alloc_all_urbs(rtl8150_t *dev)
{ {
dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL); dev->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
if (!dev->rx_urb) if (!dev->rx_urb)
...@@ -284,8 +271,7 @@ static int alloc_all_urbs(rtl8150_t *dev) ...@@ -284,8 +271,7 @@ static int alloc_all_urbs(rtl8150_t *dev)
return 1; return 1;
} }
static void free_all_urbs(rtl8150_t * dev)
static void free_all_urbs(rtl8150_t *dev)
{ {
usb_free_urb(dev->rx_urb); usb_free_urb(dev->rx_urb);
usb_free_urb(dev->tx_urb); usb_free_urb(dev->tx_urb);
...@@ -293,8 +279,7 @@ static void free_all_urbs(rtl8150_t *dev) ...@@ -293,8 +279,7 @@ static void free_all_urbs(rtl8150_t *dev)
usb_free_urb(dev->ctrl_urb); usb_free_urb(dev->ctrl_urb);
} }
static void unlink_all_urbs(rtl8150_t * dev)
static void unlink_all_urbs(rtl8150_t *dev)
{ {
usb_unlink_urb(dev->rx_urb); usb_unlink_urb(dev->rx_urb);
usb_unlink_urb(dev->tx_urb); usb_unlink_urb(dev->tx_urb);
...@@ -302,7 +287,6 @@ static void unlink_all_urbs(rtl8150_t *dev) ...@@ -302,7 +287,6 @@ static void unlink_all_urbs(rtl8150_t *dev)
usb_unlink_urb(dev->ctrl_urb); usb_unlink_urb(dev->ctrl_urb);
} }
static void read_bulk_callback(struct urb *urb) static void read_bulk_callback(struct urb *urb)
{ {
rtl8150_t *dev; rtl8150_t *dev;
...@@ -318,7 +302,7 @@ static void read_bulk_callback(struct urb *urb) ...@@ -318,7 +302,7 @@ static void read_bulk_callback(struct urb *urb)
} }
netdev = dev->netdev; netdev = dev->netdev;
if (!netif_device_present(netdev)) { if (!netif_device_present(netdev)) {
warn("netdev is not present"); warn("the network device is not present");
return; return;
} }
switch (urb->status) { switch (urb->status) {
...@@ -334,27 +318,58 @@ static void read_bulk_callback(struct urb *urb) ...@@ -334,27 +318,58 @@ static void read_bulk_callback(struct urb *urb)
goto goon; goto goon;
} }
if (!dev->rx_skb) {
/* lost packets++ */
return;
}
res = urb->actual_length; res = urb->actual_length;
rx_stat = le16_to_cpu(*(short*)(dev->rx_buff + res - 4)); rx_stat = le16_to_cpu(*(short *)(urb->transfer_buffer + res - 4));
pkt_len = res - 4; pkt_len = res - 4;
if (!(skb = dev_alloc_skb(pkt_len + 2))) skb_put(dev->rx_skb, pkt_len);
goto goon; dev->rx_skb->protocol = eth_type_trans(dev->rx_skb, netdev);
skb->dev = netdev; netif_rx(dev->rx_skb);
skb_reserve(skb, 2);
eth_copy_and_sum(skb, dev->rx_buff, pkt_len, 0);
skb_put(skb, pkt_len);
skb->protocol = eth_type_trans(skb, netdev);
netif_rx(skb);
dev->stats.rx_packets++; dev->stats.rx_packets++;
dev->stats.rx_bytes += pkt_len; dev->stats.rx_bytes += pkt_len;
tasklet_schedule(&dev->tl);
skb = pull_skb(dev);
if (!skb) {
dev->rx_skb = NULL;
return;
}
skb->dev = netdev;
skb_reserve(skb, 2);
dev->rx_skb = skb;
goon: goon:
FILL_BULK_URB(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev,1), FILL_BULK_URB(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
dev->rx_buff, RTL8150_MAX_MTU, read_bulk_callback, dev); dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
if ((res=usb_submit_urb(dev->rx_urb, GFP_ATOMIC))) if ((res = usb_submit_urb(dev->rx_urb, GFP_ATOMIC)))
warn("%s: Rx urb submission failed %d", netdev->name, res); warn("%s: Rx urb submission failed %d", netdev->name, res);
} }
static void rx_fixup(unsigned long data)
{
rtl8150_t *dev;
struct sk_buff *skb;
dev = (rtl8150_t *)data;
fill_skb_pool(dev);
skb = pull_skb(dev);
if (!skb) {
tasklet_schedule(&dev->tl);
return;
}
if (dev->rx_skb)
return;
dev->rx_skb = skb;
FILL_BULK_URB(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
usb_submit_urb(dev->rx_urb, GFP_ATOMIC);
}
static void write_bulk_callback(struct urb *urb) static void write_bulk_callback(struct urb *urb)
{ {
...@@ -363,6 +378,7 @@ static void write_bulk_callback(struct urb *urb) ...@@ -363,6 +378,7 @@ static void write_bulk_callback(struct urb *urb)
dev = urb->context; dev = urb->context;
if (!dev) if (!dev)
return; return;
dev_kfree_skb_irq(dev->tx_skb);
if (!netif_device_present(dev->netdev)) if (!netif_device_present(dev->netdev))
return; return;
if (urb->status) if (urb->status)
...@@ -371,7 +387,6 @@ static void write_bulk_callback(struct urb *urb) ...@@ -371,7 +387,6 @@ static void write_bulk_callback(struct urb *urb)
netif_wake_queue(dev->netdev); netif_wake_queue(dev->netdev);
} }
void intr_callback(struct urb *urb) void intr_callback(struct urb *urb)
{ {
rtl8150_t *dev; rtl8150_t *dev;
...@@ -385,29 +400,81 @@ void intr_callback(struct urb *urb) ...@@ -385,29 +400,81 @@ void intr_callback(struct urb *urb)
case -ENOENT: case -ENOENT:
return; return;
default: default:
info("%s: intr status %d", dev->netdev->name, info("%s: intr status %d", dev->netdev->name, urb->status);
urb->status);
} }
} }
/* /*
** **
** network related part of the code ** network related part of the code
** **
*/ */
static void fill_skb_pool(rtl8150_t *dev)
{
struct sk_buff *skb;
int i;
unsigned long flags;
spin_lock_irqsave(&dev->rx_pool_lock, flags);
for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
if (dev->rx_skb_pool[i])
continue;
skb = dev_alloc_skb(RTL8150_MTU + 2);
if (!skb) {
spin_unlock_irqrestore(&dev->rx_pool_lock, flags);
return;
}
skb->dev = dev->netdev;
skb_reserve(skb, 2);
dev->rx_skb_pool[i] = skb;
}
spin_unlock_irqrestore(&dev->rx_pool_lock, flags);
}
static void free_skb_pool(rtl8150_t *dev)
{
int i;
spin_lock_irq(&dev->rx_pool_lock);
for (i = 0; i < RX_SKB_POOL_SIZE; i++)
if (dev->rx_skb_pool[i])
dev_kfree_skb(dev->rx_skb_pool[i]);
spin_unlock_irq(&dev->rx_pool_lock);
}
static struct sk_buff *pull_skb(rtl8150_t *dev)
{
struct sk_buff *skb;
int i;
unsigned long flags;
spin_lock_irqsave(&dev->rx_pool_lock, flags);
for (i = 0; i < RX_SKB_POOL_SIZE; i++) {
if (dev->rx_skb_pool[i]) {
skb = dev->rx_skb_pool[i];
dev->rx_skb_pool[i] = NULL;
spin_unlock_irqrestore(&dev->rx_pool_lock, flags);
return skb;
}
}
spin_unlock_irqrestore(&dev->rx_pool_lock, flags);
return NULL;
}
static int enable_net_traffic(rtl8150_t *dev) static int enable_net_traffic(rtl8150_t * dev)
{ {
u8 cr, tcr, rcr, msr; u8 cr, tcr, rcr, msr;
if (rtl8150_reset(dev)) { if (!rtl8150_reset(dev)) {
warn("%s - device reset failed", __FUNCTION__); warn("%s - device reset failed", __FUNCTION__);
} }
dev->rx_creg = rcr = 0x9e; /* bit7=1 attach Rx info at the end */ /* RCR bit7=1 attach Rx info at the end; =0 HW CRC (which is broken) */
tcr = 0xd8; /* bit0=1 no CRC at the end of the frame */ dev->rx_creg = rcr = 0x9e;
tcr = 0xd8;
cr = 0x0c; cr = 0x0c;
if (!(rcr & 0x80))
set_bit(RTL8150_HW_CRC, &dev->flags);
set_registers(dev, RCR, 1, &rcr); set_registers(dev, RCR, 1, &rcr);
set_registers(dev, TCR, 1, &tcr); set_registers(dev, TCR, 1, &tcr);
set_registers(dev, CR, 1, &cr); set_registers(dev, CR, 1, &cr);
...@@ -416,8 +483,7 @@ static int enable_net_traffic(rtl8150_t *dev) ...@@ -416,8 +483,7 @@ static int enable_net_traffic(rtl8150_t *dev)
return 0; return 0;
} }
static void disable_net_traffic(rtl8150_t * dev)
static void disable_net_traffic(rtl8150_t *dev)
{ {
u8 cr; u8 cr;
...@@ -426,13 +492,11 @@ static void disable_net_traffic(rtl8150_t *dev) ...@@ -426,13 +492,11 @@ static void disable_net_traffic(rtl8150_t *dev)
set_registers(dev, CR, 1, &cr); set_registers(dev, CR, 1, &cr);
} }
static struct net_device_stats *rtl8150_netdev_stats(struct net_device *dev) static struct net_device_stats *rtl8150_netdev_stats(struct net_device *dev)
{ {
return &((rtl8150_t *)dev->priv)->stats; return &((rtl8150_t *) dev->priv)->stats;
} }
static void rtl8150_tx_timeout(struct net_device *netdev) static void rtl8150_tx_timeout(struct net_device *netdev)
{ {
rtl8150_t *dev; rtl8150_t *dev;
...@@ -446,7 +510,6 @@ static void rtl8150_tx_timeout(struct net_device *netdev) ...@@ -446,7 +510,6 @@ static void rtl8150_tx_timeout(struct net_device *netdev)
dev->stats.tx_errors++; dev->stats.tx_errors++;
} }
static void rtl8150_set_multicast(struct net_device *netdev) static void rtl8150_set_multicast(struct net_device *netdev)
{ {
rtl8150_t *dev; rtl8150_t *dev;
...@@ -469,7 +532,6 @@ static void rtl8150_set_multicast(struct net_device *netdev) ...@@ -469,7 +532,6 @@ static void rtl8150_set_multicast(struct net_device *netdev)
netif_wake_queue(netdev); netif_wake_queue(netdev);
} }
static int rtl8150_start_xmit(struct sk_buff *skb, struct net_device *netdev) static int rtl8150_start_xmit(struct sk_buff *skb, struct net_device *netdev)
{ {
rtl8150_t *dev; rtl8150_t *dev;
...@@ -479,11 +541,9 @@ static int rtl8150_start_xmit(struct sk_buff *skb, struct net_device *netdev) ...@@ -479,11 +541,9 @@ static int rtl8150_start_xmit(struct sk_buff *skb, struct net_device *netdev)
dev = netdev->priv; dev = netdev->priv;
count = (skb->len < 60) ? 60 : skb->len; count = (skb->len < 60) ? 60 : skb->len;
count = (count & 0x3f) ? count : count + 1; count = (count & 0x3f) ? count : count + 1;
memcpy(dev->tx_buff, skb->data, skb->len); dev->tx_skb = skb;
FILL_BULK_URB(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev,2), FILL_BULK_URB(dev->tx_urb, dev->udev, usb_sndbulkpipe(dev->udev, 2),
dev->tx_buff, RTL8150_MAX_MTU, write_bulk_callback, dev); skb->data, count, write_bulk_callback, dev);
dev->tx_urb->transfer_buffer_length = count;
if ((res = usb_submit_urb(dev->tx_urb, GFP_KERNEL))) { if ((res = usb_submit_urb(dev->tx_urb, GFP_KERNEL))) {
warn("failed tx_urb %d\n", res); warn("failed tx_urb %d\n", res);
dev->stats.tx_errors++; dev->stats.tx_errors++;
...@@ -493,12 +553,10 @@ static int rtl8150_start_xmit(struct sk_buff *skb, struct net_device *netdev) ...@@ -493,12 +553,10 @@ static int rtl8150_start_xmit(struct sk_buff *skb, struct net_device *netdev)
dev->stats.tx_bytes += skb->len; dev->stats.tx_bytes += skb->len;
netdev->trans_start = jiffies; netdev->trans_start = jiffies;
} }
dev_kfree_skb(skb);
return 0; return 0;
} }
static int rtl8150_open(struct net_device *netdev) static int rtl8150_open(struct net_device *netdev)
{ {
rtl8150_t *dev; rtl8150_t *dev;
...@@ -509,15 +567,19 @@ static int rtl8150_open(struct net_device *netdev) ...@@ -509,15 +567,19 @@ static int rtl8150_open(struct net_device *netdev)
return -ENODEV; return -ENODEV;
} }
dev->rx_skb = pull_skb(dev);
if (!dev->rx_skb)
return -ENOMEM;
down(&dev->sem); down(&dev->sem);
FILL_BULK_URB(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev,1), FILL_BULK_URB(dev->rx_urb, dev->udev, usb_rcvbulkpipe(dev->udev, 1),
dev->rx_buff, RTL8150_MAX_MTU, read_bulk_callback, dev); dev->rx_skb->data, RTL8150_MTU, read_bulk_callback, dev);
if ((res=usb_submit_urb(dev->rx_urb, GFP_KERNEL))) if ((res = usb_submit_urb(dev->rx_urb, GFP_KERNEL)))
warn("%s: rx_urb submit failed: %d", __FUNCTION__, res); warn("%s: rx_urb submit failed: %d", __FUNCTION__, res);
FILL_INT_URB(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev,3), FILL_INT_URB(dev->intr_urb, dev->udev, usb_rcvintpipe(dev->udev, 3),
dev->intr_buff, sizeof(dev->intr_buff), intr_callback, dev->intr_buff, sizeof(dev->intr_buff), intr_callback,
dev, dev->intr_interval); dev, dev->intr_interval);
if ((res=usb_submit_urb(dev->intr_urb, GFP_KERNEL))) if ((res = usb_submit_urb(dev->intr_urb, GFP_KERNEL)))
warn("%s: intr_urb submit failed: %d", __FUNCTION__, res); warn("%s: intr_urb submit failed: %d", __FUNCTION__, res);
netif_start_queue(netdev); netif_start_queue(netdev);
enable_net_traffic(dev); enable_net_traffic(dev);
...@@ -526,7 +588,6 @@ static int rtl8150_open(struct net_device *netdev) ...@@ -526,7 +588,6 @@ static int rtl8150_open(struct net_device *netdev)
return res; return res;
} }
static int rtl8150_close(struct net_device *netdev) static int rtl8150_close(struct net_device *netdev)
{ {
rtl8150_t *dev; rtl8150_t *dev;
...@@ -543,32 +604,30 @@ static int rtl8150_close(struct net_device *netdev) ...@@ -543,32 +604,30 @@ static int rtl8150_close(struct net_device *netdev)
netif_stop_queue(netdev); netif_stop_queue(netdev);
up(&dev->sem); up(&dev->sem);
return res; return res;
} }
static int rtl8150_ethtool_ioctl(struct net_device *netdev, void *uaddr) static int rtl8150_ethtool_ioctl(struct net_device *netdev, void *uaddr)
{ {
rtl8150_t *dev; rtl8150_t *dev;
int cmd; int cmd;
dev = netdev->priv; dev = netdev->priv;
if (get_user(cmd, (int *)uaddr)) if (get_user(cmd, (int *) uaddr))
return -EFAULT; return -EFAULT;
switch (cmd) { switch (cmd) {
case ETHTOOL_GDRVINFO: { case ETHTOOL_GDRVINFO:{
struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO}; struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
strncpy(info.driver, DRIVER_DESC, ETHTOOL_BUSINFO_LEN); strncpy(info.driver, DRIVER_DESC, ETHTOOL_BUSINFO_LEN);
strncpy(info.version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN); strncpy(info.version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
usb_make_path (dev->udev, info.bus_info, sizeof info.bus_info); usb_make_path(dev->udev, info.bus_info, sizeof info.bus_info);
if (copy_to_user(uaddr, &info, sizeof(info))) if (copy_to_user(uaddr, &info, sizeof(info)))
return -EFAULT; return -EFAULT;
return 0; return 0;
} }
case ETHTOOL_GSET: { case ETHTOOL_GSET:{
struct ethtool_cmd ecmd; struct ethtool_cmd ecmd;
short lpa, bmcr; short lpa, bmcr;
...@@ -579,8 +638,7 @@ static int rtl8150_ethtool_ioctl(struct net_device *netdev, void *uaddr) ...@@ -579,8 +638,7 @@ static int rtl8150_ethtool_ioctl(struct net_device *netdev, void *uaddr)
SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Half |
SUPPORTED_100baseT_Full | SUPPORTED_100baseT_Full |
SUPPORTED_Autoneg | SUPPORTED_Autoneg |
SUPPORTED_TP | SUPPORTED_TP | SUPPORTED_MII);
SUPPORTED_MII);
ecmd.port = PORT_TP; ecmd.port = PORT_TP;
ecmd.transceiver = XCVR_INTERNAL; ecmd.transceiver = XCVR_INTERNAL;
ecmd.phy_address = dev->phy; ecmd.phy_address = dev->phy;
...@@ -609,8 +667,8 @@ static int rtl8150_ethtool_ioctl(struct net_device *netdev, void *uaddr) ...@@ -609,8 +667,8 @@ static int rtl8150_ethtool_ioctl(struct net_device *netdev, void *uaddr)
} }
case ETHTOOL_SSET: case ETHTOOL_SSET:
return -ENOTSUPP; return -ENOTSUPP;
case ETHTOOL_GLINK: { case ETHTOOL_GLINK:{
struct ethtool_value edata = {ETHTOOL_GLINK}; struct ethtool_value edata = { ETHTOOL_GLINK };
edata.data = netif_carrier_ok(netdev); edata.data = netif_carrier_ok(netdev);
if (copy_to_user(uaddr, &edata, sizeof(edata))) if (copy_to_user(uaddr, &edata, sizeof(edata)))
...@@ -622,15 +680,14 @@ static int rtl8150_ethtool_ioctl(struct net_device *netdev, void *uaddr) ...@@ -622,15 +680,14 @@ static int rtl8150_ethtool_ioctl(struct net_device *netdev, void *uaddr)
} }
} }
static int rtl8150_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
static int rtl8150_ioctl (struct net_device *netdev, struct ifreq *rq, int cmd)
{ {
rtl8150_t *dev; rtl8150_t *dev;
u16 *data; u16 *data;
int res; int res;
dev = netdev->priv; dev = netdev->priv;
data = (u16 *)&rq->ifr_data; data = (u16 *) & rq->ifr_data;
res = 0; res = 0;
down(&dev->sem); down(&dev->sem);
...@@ -640,10 +697,10 @@ static int rtl8150_ioctl (struct net_device *netdev, struct ifreq *rq, int cmd) ...@@ -640,10 +697,10 @@ static int rtl8150_ioctl (struct net_device *netdev, struct ifreq *rq, int cmd)
break; break;
case SIOCDEVPRIVATE: case SIOCDEVPRIVATE:
data[0] = dev->phy; data[0] = dev->phy;
case SIOCDEVPRIVATE+1: case SIOCDEVPRIVATE + 1:
read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]); read_mii_word(dev, dev->phy, (data[1] & 0x1f), &data[3]);
break; break;
case SIOCDEVPRIVATE+2: case SIOCDEVPRIVATE + 2:
if (!capable(CAP_NET_ADMIN)) { if (!capable(CAP_NET_ADMIN)) {
up(&dev->sem); up(&dev->sem);
return -EPERM; return -EPERM;
...@@ -657,14 +714,12 @@ static int rtl8150_ioctl (struct net_device *netdev, struct ifreq *rq, int cmd) ...@@ -657,14 +714,12 @@ static int rtl8150_ioctl (struct net_device *netdev, struct ifreq *rq, int cmd)
return res; return res;
} }
static void *rtl8150_probe(struct usb_device *udev, unsigned int ifnum,
static void * rtl8150_probe(struct usb_device *udev, unsigned int ifnum,
const struct usb_device_id *id) const struct usb_device_id *id)
{ {
rtl8150_t *dev; rtl8150_t *dev;
struct net_device *netdev; struct net_device *netdev;
udev->config[0].bConfigurationValue = 1;
if (usb_set_configuration(udev, udev->config[0].bConfigurationValue)) { if (usb_set_configuration(udev, udev->config[0].bConfigurationValue)) {
err("usb_set_configuration() failed"); err("usb_set_configuration() failed");
return NULL; return NULL;
...@@ -676,8 +731,8 @@ static void * rtl8150_probe(struct usb_device *udev, unsigned int ifnum, ...@@ -676,8 +731,8 @@ static void * rtl8150_probe(struct usb_device *udev, unsigned int ifnum,
} }
dev = kmalloc(sizeof(rtl8150_t), GFP_KERNEL); dev = kmalloc(sizeof(rtl8150_t), GFP_KERNEL);
if (!dev) { if (!dev) {
err ("Out of memory"); err("Out of memory");
goto exit; return NULL;
} else } else
memset(dev, 0, sizeof(rtl8150_t)); memset(dev, 0, sizeof(rtl8150_t));
...@@ -685,11 +740,14 @@ static void * rtl8150_probe(struct usb_device *udev, unsigned int ifnum, ...@@ -685,11 +740,14 @@ static void * rtl8150_probe(struct usb_device *udev, unsigned int ifnum,
if (!netdev) { if (!netdev) {
kfree(dev); kfree(dev);
err("Oh boy, out of memory again?!?"); err("Oh boy, out of memory again?!?");
dev = NULL; return NULL;
goto exit;
} }
init_MUTEX(&dev->sem); init_MUTEX(&dev->sem);
tasklet_init(&dev->tl, rx_fixup, (unsigned long)dev);
spin_lock_init(&dev->rx_pool_lock);
down(&dev->sem);
dev->udev = udev; dev->udev = udev;
dev->netdev = netdev; dev->netdev = netdev;
SET_MODULE_OWNER(netdev); SET_MODULE_OWNER(netdev);
...@@ -705,23 +763,29 @@ static void * rtl8150_probe(struct usb_device *udev, unsigned int ifnum, ...@@ -705,23 +763,29 @@ static void * rtl8150_probe(struct usb_device *udev, unsigned int ifnum,
netdev->mtu = RTL8150_MTU; netdev->mtu = RTL8150_MTU;
dev->intr_interval = 100; /* 100ms */ dev->intr_interval = 100; /* 100ms */
if (rtl8150_reset(dev) || !alloc_all_urbs(dev)) { if (!alloc_all_urbs(dev)) {
err("out of memory");
goto err;
}
if (!rtl8150_reset(dev)) {
err("couldn't reset the device"); err("couldn't reset the device");
free_all_urbs(dev); free_all_urbs(dev);
unregister_netdev(dev->netdev); goto err;
kfree(netdev);
kfree(dev);
dev = NULL;
goto exit;
} }
fill_skb_pool(dev);
set_ethernet_addr(dev); set_ethernet_addr(dev);
info("%s: rtl8150 is detected", netdev->name); info("%s: rtl8150 is detected", netdev->name);
exit:
up(&dev->sem);
return dev; return dev;
err:
unregister_netdev(dev->netdev);
up(&dev->sem);
kfree(netdev);
kfree(dev);
return NULL;
} }
static void rtl8150_disconnect(struct usb_device *udev, void *ptr) static void rtl8150_disconnect(struct usb_device *udev, void *ptr)
{ {
rtl8150_t *dev; rtl8150_t *dev;
...@@ -731,27 +795,26 @@ static void rtl8150_disconnect(struct usb_device *udev, void *ptr) ...@@ -731,27 +795,26 @@ static void rtl8150_disconnect(struct usb_device *udev, void *ptr)
unregister_netdev(dev->netdev); unregister_netdev(dev->netdev);
unlink_all_urbs(dev); unlink_all_urbs(dev);
free_all_urbs(dev); free_all_urbs(dev);
free_skb_pool(dev);
if (dev->rx_skb)
dev_kfree_skb(dev->rx_skb);
kfree(dev->netdev); kfree(dev->netdev);
kfree(dev); kfree(dev);
dev->netdev = NULL; dev->netdev = NULL;
dev = NULL; dev = NULL;
} }
static int __init usb_rtl8150_init(void) static int __init usb_rtl8150_init(void)
{ {
info(DRIVER_DESC " " DRIVER_VERSION); info(DRIVER_DESC " " DRIVER_VERSION);
return usb_register(&rtl8150_driver); return usb_register(&rtl8150_driver);
} }
static void __exit usb_rtl8150_exit(void) static void __exit usb_rtl8150_exit(void)
{ {
usb_deregister(&rtl8150_driver); usb_deregister(&rtl8150_driver);
} }
module_init(usb_rtl8150_init); module_init(usb_rtl8150_init);
module_exit(usb_rtl8150_exit); module_exit(usb_rtl8150_exit);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment