Commit 41e02f01 authored by matt mooney's avatar matt mooney Committed by Greg Kroah-Hartman

staging: usbip: stub_main.c: code cleanup

Remove match_find() and replace with get_busid_idx(); change
get_busid_priv(), add_match_busid(), and del_match_busid() to use
get_busid_idx(); and cleanup code in the other functions.
Signed-off-by: default avatarmatt mooney <mfm@muteddisk.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent efad25e9
...@@ -50,82 +50,90 @@ static void init_busid_table(void) ...@@ -50,82 +50,90 @@ static void init_busid_table(void)
spin_lock_init(&busid_table_lock); spin_lock_init(&busid_table_lock);
} }
int match_busid(const char *busid) /*
* Find the index of the busid by name.
* Must be called with busid_table_lock held.
*/
static int get_busid_idx(const char *busid)
{ {
int i; int i;
int idx = -1;
spin_lock(&busid_table_lock);
for (i = 0; i < MAX_BUSID; i++) for (i = 0; i < MAX_BUSID; i++)
if (busid_table[i].name[0]) if (busid_table[i].name[0])
if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) { if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
/* already registerd */ idx = i;
spin_unlock(&busid_table_lock); break;
return 0;
} }
spin_unlock(&busid_table_lock); return idx;
return 1;
} }
struct bus_id_priv *get_busid_priv(const char *busid) struct bus_id_priv *get_busid_priv(const char *busid)
{ {
int i; int idx;
struct bus_id_priv *bid = NULL;
spin_lock(&busid_table_lock); spin_lock(&busid_table_lock);
for (i = 0; i < MAX_BUSID; i++) idx = get_busid_idx(busid);
if (busid_table[i].name[0]) if (idx >= 0)
if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) { bid = &(busid_table[idx]);
/* already registerd */
spin_unlock(&busid_table_lock);
return &(busid_table[i]);
}
spin_unlock(&busid_table_lock); spin_unlock(&busid_table_lock);
return NULL; return bid;
} }
static int add_match_busid(char *busid) static int add_match_busid(char *busid)
{ {
int i; int i;
int ret = -1;
if (!match_busid(busid))
return 0;
spin_lock(&busid_table_lock); spin_lock(&busid_table_lock);
/* already registered? */
if (get_busid_idx(busid) >= 0) {
ret = 0;
goto out;
}
for (i = 0; i < MAX_BUSID; i++) for (i = 0; i < MAX_BUSID; i++)
if (!busid_table[i].name[0]) { if (!busid_table[i].name[0]) {
strncpy(busid_table[i].name, busid, BUSID_SIZE); strncpy(busid_table[i].name, busid, BUSID_SIZE);
if ((busid_table[i].status != STUB_BUSID_ALLOC) && if ((busid_table[i].status != STUB_BUSID_ALLOC) &&
(busid_table[i].status != STUB_BUSID_REMOV)) (busid_table[i].status != STUB_BUSID_REMOV))
busid_table[i].status = STUB_BUSID_ADDED; busid_table[i].status = STUB_BUSID_ADDED;
spin_unlock(&busid_table_lock); ret = 0;
return 0; break;
} }
out:
spin_unlock(&busid_table_lock); spin_unlock(&busid_table_lock);
return -1; return ret;
} }
int del_match_busid(char *busid) int del_match_busid(char *busid)
{ {
int i; int idx;
int ret = -1;
spin_lock(&busid_table_lock); spin_lock(&busid_table_lock);
for (i = 0; i < MAX_BUSID; i++) idx = get_busid_idx(busid);
if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) { if (idx < 0)
/* found */ goto out;
if (busid_table[i].status == STUB_BUSID_OTHER)
memset(busid_table[i].name, 0, BUSID_SIZE); /* found */
if ((busid_table[i].status != STUB_BUSID_OTHER) && ret = 0;
(busid_table[i].status != STUB_BUSID_ADDED)) {
busid_table[i].status = STUB_BUSID_REMOV; if (busid_table[idx].status == STUB_BUSID_OTHER)
} memset(busid_table[idx].name, 0, BUSID_SIZE);
spin_unlock(&busid_table_lock);
return 0; if ((busid_table[idx].status != STUB_BUSID_OTHER) &&
} (busid_table[idx].status != STUB_BUSID_ADDED))
busid_table[idx].status = STUB_BUSID_REMOV;
out:
spin_unlock(&busid_table_lock); spin_unlock(&busid_table_lock);
return -1; return ret;
} }
static ssize_t show_match_busid(struct device_driver *drv, char *buf) static ssize_t show_match_busid(struct device_driver *drv, char *buf)
...@@ -138,8 +146,8 @@ static ssize_t show_match_busid(struct device_driver *drv, char *buf) ...@@ -138,8 +146,8 @@ static ssize_t show_match_busid(struct device_driver *drv, char *buf)
if (busid_table[i].name[0]) if (busid_table[i].name[0])
out += sprintf(out, "%s ", busid_table[i].name); out += sprintf(out, "%s ", busid_table[i].name);
spin_unlock(&busid_table_lock); spin_unlock(&busid_table_lock);
out += sprintf(out, "\n"); out += sprintf(out, "\n");
return out - buf; return out - buf;
} }
...@@ -162,23 +170,24 @@ static ssize_t store_match_busid(struct device_driver *dev, const char *buf, ...@@ -162,23 +170,24 @@ static ssize_t store_match_busid(struct device_driver *dev, const char *buf,
strncpy(busid, buf + 4, BUSID_SIZE); strncpy(busid, buf + 4, BUSID_SIZE);
if (!strncmp(buf, "add ", 4)) { if (!strncmp(buf, "add ", 4)) {
if (add_match_busid(busid) < 0) if (add_match_busid(busid) < 0) {
return -ENOMEM; return -ENOMEM;
else { } else {
pr_debug("add busid %s\n", busid); pr_debug("add busid %s\n", busid);
return count; return count;
} }
} else if (!strncmp(buf, "del ", 4)) { } else if (!strncmp(buf, "del ", 4)) {
if (del_match_busid(busid) < 0) if (del_match_busid(busid) < 0) {
return -ENODEV; return -ENODEV;
else { } else {
pr_debug("del busid %s\n", busid); pr_debug("del busid %s\n", busid);
return count; return count;
} }
} else } else {
return -EINVAL; return -EINVAL;
}
} }
static DRIVER_ATTR(match_busid, S_IRUSR|S_IWUSR, show_match_busid, static DRIVER_ATTR(match_busid, S_IRUSR | S_IWUSR, show_match_busid,
store_match_busid); store_match_busid);
static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead) static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
...@@ -201,36 +210,30 @@ static struct stub_priv *stub_priv_pop(struct stub_device *sdev) ...@@ -201,36 +210,30 @@ static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
spin_lock_irqsave(&sdev->priv_lock, flags); spin_lock_irqsave(&sdev->priv_lock, flags);
priv = stub_priv_pop_from_listhead(&sdev->priv_init); priv = stub_priv_pop_from_listhead(&sdev->priv_init);
if (priv) { if (priv)
spin_unlock_irqrestore(&sdev->priv_lock, flags); goto done;
return priv;
}
priv = stub_priv_pop_from_listhead(&sdev->priv_tx); priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
if (priv) { if (priv)
spin_unlock_irqrestore(&sdev->priv_lock, flags); goto done;
return priv;
}
priv = stub_priv_pop_from_listhead(&sdev->priv_free); priv = stub_priv_pop_from_listhead(&sdev->priv_free);
if (priv) {
spin_unlock_irqrestore(&sdev->priv_lock, flags);
return priv;
}
done:
spin_unlock_irqrestore(&sdev->priv_lock, flags); spin_unlock_irqrestore(&sdev->priv_lock, flags);
return NULL;
return priv;
} }
void stub_device_cleanup_urbs(struct stub_device *sdev) void stub_device_cleanup_urbs(struct stub_device *sdev)
{ {
struct stub_priv *priv; struct stub_priv *priv;
struct urb *urb;
dev_dbg(&sdev->udev->dev, "free sdev %p\n", sdev); dev_dbg(&sdev->udev->dev, "free sdev %p\n", sdev);
while ((priv = stub_priv_pop(sdev))) { while ((priv = stub_priv_pop(sdev))) {
struct urb *urb = priv->urb; urb = priv->urb;
dev_dbg(&sdev->udev->dev, "free urb %p\n", urb); dev_dbg(&sdev->udev->dev, "free urb %p\n", urb);
usb_kill_urb(urb); usb_kill_urb(urb);
...@@ -238,7 +241,6 @@ void stub_device_cleanup_urbs(struct stub_device *sdev) ...@@ -238,7 +241,6 @@ void stub_device_cleanup_urbs(struct stub_device *sdev)
kfree(urb->transfer_buffer); kfree(urb->transfer_buffer);
kfree(urb->setup_packet); kfree(urb->setup_packet);
usb_free_urb(urb); usb_free_urb(urb);
} }
} }
...@@ -250,34 +252,31 @@ static int __init usb_stub_init(void) ...@@ -250,34 +252,31 @@ static int __init usb_stub_init(void)
stub_priv_cache = kmem_cache_create("stub_priv", stub_priv_cache = kmem_cache_create("stub_priv",
sizeof(struct stub_priv), 0, sizeof(struct stub_priv), 0,
SLAB_HWCACHE_ALIGN, NULL); SLAB_HWCACHE_ALIGN, NULL);
if (!stub_priv_cache) { if (!stub_priv_cache) {
pr_err("create stub_priv_cache error\n"); pr_err("kmem_cache_create failed\n");
return -ENOMEM; return -ENOMEM;
} }
ret = usb_register(&stub_driver); ret = usb_register(&stub_driver);
if (ret) { if (ret < 0) {
pr_err("usb_register failed %d\n", ret); pr_err("usb_register failed %d\n", ret);
goto error_usb_register; goto err_usb_register;
} }
pr_info(DRIVER_DESC " " USBIP_VERSION "\n");
init_busid_table();
ret = driver_create_file(&stub_driver.drvwrap.driver, ret = driver_create_file(&stub_driver.drvwrap.driver,
&driver_attr_match_busid); &driver_attr_match_busid);
if (ret < 0) {
if (ret) { pr_err("driver_create_file failed\n");
pr_err("create driver sysfs\n"); goto err_create_file;
goto error_create_file;
} }
init_busid_table();
pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
return ret; return ret;
error_create_file:
err_create_file:
usb_deregister(&stub_driver); usb_deregister(&stub_driver);
error_usb_register: err_usb_register:
kmem_cache_destroy(stub_priv_cache); kmem_cache_destroy(stub_priv_cache);
return ret; return ret;
} }
......
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