Skip to content

Commit

Permalink
[PATCH] USB: convert a bunch of USB semaphores to mutexes
Browse files Browse the repository at this point in the history
the patch below converts a bunch of semaphores-used-as-mutex in the USB
code to mutexes

Signed-off-by: Arjan van de Ven <arjan@infradead.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
  • Loading branch information
Arjan van de Ven authored and Greg Kroah-Hartman committed Mar 20, 2006
1 parent 35cce73 commit 4186ecf
Show file tree
Hide file tree
Showing 34 changed files with 389 additions and 372 deletions.
23 changes: 12 additions & 11 deletions drivers/usb/class/cdc-acm.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/smp_lock.h>
#include <linux/mutex.h>
#include <asm/uaccess.h>
#include <linux/usb.h>
#include <linux/usb_cdc.h>
Expand All @@ -80,7 +81,7 @@ static struct usb_driver acm_driver;
static struct tty_driver *acm_tty_driver;
static struct acm *acm_table[ACM_TTY_MINORS];

static DECLARE_MUTEX(open_sem);
static DEFINE_MUTEX(open_mutex);

#define ACM_READY(acm) (acm && acm->dev && acm->used)

Expand Down Expand Up @@ -431,8 +432,8 @@ static int acm_tty_open(struct tty_struct *tty, struct file *filp)
int rv = -EINVAL;
int i;
dbg("Entering acm_tty_open.\n");
down(&open_sem);

mutex_lock(&open_mutex);

acm = acm_table[tty->index];
if (!acm || !acm->dev)
Expand Down Expand Up @@ -474,14 +475,14 @@ static int acm_tty_open(struct tty_struct *tty, struct file *filp)

done:
err_out:
up(&open_sem);
mutex_unlock(&open_mutex);
return rv;

full_bailout:
usb_kill_urb(acm->ctrlurb);
bail_out:
acm->used--;
up(&open_sem);
mutex_unlock(&open_mutex);
return -EIO;
}

Expand All @@ -507,7 +508,7 @@ static void acm_tty_close(struct tty_struct *tty, struct file *filp)
if (!acm || !acm->used)
return;

down(&open_sem);
mutex_lock(&open_mutex);
if (!--acm->used) {
if (acm->dev) {
acm_set_control(acm, acm->ctrlout = 0);
Expand All @@ -518,7 +519,7 @@ static void acm_tty_close(struct tty_struct *tty, struct file *filp)
} else
acm_tty_unregister(acm);
}
up(&open_sem);
mutex_unlock(&open_mutex);
}

static int acm_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
Expand Down Expand Up @@ -1013,9 +1014,9 @@ static void acm_disconnect(struct usb_interface *intf)
return;
}

down(&open_sem);
mutex_lock(&open_mutex);
if (!usb_get_intfdata(intf)) {
up(&open_sem);
mutex_unlock(&open_mutex);
return;
}
acm->dev = NULL;
Expand Down Expand Up @@ -1045,11 +1046,11 @@ static void acm_disconnect(struct usb_interface *intf)

if (!acm->used) {
acm_tty_unregister(acm);
up(&open_sem);
mutex_unlock(&open_mutex);
return;
}

up(&open_sem);
mutex_unlock(&open_mutex);

if (acm->tty)
tty_hangup(acm->tty);
Expand Down
15 changes: 8 additions & 7 deletions drivers/usb/class/usblp.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/lp.h>
#include <linux/mutex.h>
#undef DEBUG
#include <linux/usb.h>

Expand Down Expand Up @@ -223,7 +224,7 @@ static int usblp_cache_device_id_string(struct usblp *usblp);

/* forward reference to make our lives easier */
static struct usb_driver usblp_driver;
static DECLARE_MUTEX(usblp_sem); /* locks the existence of usblp's */
static DEFINE_MUTEX(usblp_mutex); /* locks the existence of usblp's */

/*
* Functions for usblp control messages.
Expand Down Expand Up @@ -351,7 +352,7 @@ static int usblp_open(struct inode *inode, struct file *file)
if (minor < 0)
return -ENODEV;

down (&usblp_sem);
mutex_lock (&usblp_mutex);

retval = -ENODEV;
intf = usb_find_interface(&usblp_driver, minor);
Expand Down Expand Up @@ -399,7 +400,7 @@ static int usblp_open(struct inode *inode, struct file *file)
}
}
out:
up (&usblp_sem);
mutex_unlock (&usblp_mutex);
return retval;
}

Expand All @@ -425,13 +426,13 @@ static int usblp_release(struct inode *inode, struct file *file)
{
struct usblp *usblp = file->private_data;

down (&usblp_sem);
mutex_lock (&usblp_mutex);
usblp->used = 0;
if (usblp->present) {
usblp_unlink_urbs(usblp);
} else /* finish cleanup from disconnect */
usblp_cleanup (usblp);
up (&usblp_sem);
mutex_unlock (&usblp_mutex);
return 0;
}

Expand Down Expand Up @@ -1152,7 +1153,7 @@ static void usblp_disconnect(struct usb_interface *intf)

device_remove_file(&intf->dev, &dev_attr_ieee1284_id);

down (&usblp_sem);
mutex_lock (&usblp_mutex);
down (&usblp->sem);
usblp->present = 0;
usb_set_intfdata (intf, NULL);
Expand All @@ -1166,7 +1167,7 @@ static void usblp_disconnect(struct usb_interface *intf)

if (!usblp->used)
usblp_cleanup (usblp);
up (&usblp_sem);
mutex_unlock (&usblp_mutex);
}

static struct usb_device_id usblp_ids [] = {
Expand Down
7 changes: 4 additions & 3 deletions drivers/usb/core/devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include <linux/usb.h>
#include <linux/smp_lock.h>
#include <linux/usbdevice_fs.h>
#include <linux/mutex.h>
#include <asm/uaccess.h>

#include "usb.h"
Expand Down Expand Up @@ -570,7 +571,7 @@ static ssize_t usb_device_read(struct file *file, char __user *buf, size_t nbyte
if (!access_ok(VERIFY_WRITE, buf, nbytes))
return -EFAULT;

down (&usb_bus_list_lock);
mutex_lock(&usb_bus_list_lock);
/* print devices for all busses */
list_for_each_entry(bus, &usb_bus_list, bus_list) {
/* recurse through all children of the root hub */
Expand All @@ -580,12 +581,12 @@ static ssize_t usb_device_read(struct file *file, char __user *buf, size_t nbyte
ret = usb_device_dump(&buf, &nbytes, &skip_bytes, ppos, bus->root_hub, bus, 0, 0, 0);
usb_unlock_device(bus->root_hub);
if (ret < 0) {
up(&usb_bus_list_lock);
mutex_unlock(&usb_bus_list_lock);
return ret;
}
total_written += ret;
}
up (&usb_bus_list_lock);
mutex_unlock(&usb_bus_list_lock);
return total_written;
}

Expand Down
25 changes: 13 additions & 12 deletions drivers/usb/core/hcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <asm/scatterlist.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/mutex.h>
#include <asm/irq.h>
#include <asm/byteorder.h>

Expand Down Expand Up @@ -93,7 +94,7 @@ struct usb_busmap {
static struct usb_busmap busmap;

/* used when updating list of hcds */
DECLARE_MUTEX (usb_bus_list_lock); /* exported only for usbfs */
DEFINE_MUTEX(usb_bus_list_lock); /* exported only for usbfs */
EXPORT_SYMBOL_GPL (usb_bus_list_lock);

/* used for controlling access to virtual root hubs */
Expand Down Expand Up @@ -761,30 +762,30 @@ static int usb_register_bus(struct usb_bus *bus)
{
int busnum;

down (&usb_bus_list_lock);
mutex_lock(&usb_bus_list_lock);
busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
if (busnum < USB_MAXBUS) {
set_bit (busnum, busmap.busmap);
bus->busnum = busnum;
} else {
printk (KERN_ERR "%s: too many buses\n", usbcore_name);
up(&usb_bus_list_lock);
mutex_unlock(&usb_bus_list_lock);
return -E2BIG;
}

bus->class_dev = class_device_create(usb_host_class, NULL, MKDEV(0,0),
bus->controller, "usb_host%d", busnum);
if (IS_ERR(bus->class_dev)) {
clear_bit(busnum, busmap.busmap);
up(&usb_bus_list_lock);
mutex_unlock(&usb_bus_list_lock);
return PTR_ERR(bus->class_dev);
}

class_set_devdata(bus->class_dev, bus);

/* Add it to the local list of buses */
list_add (&bus->bus_list, &usb_bus_list);
up (&usb_bus_list_lock);
mutex_unlock(&usb_bus_list_lock);

usb_notify_add_bus(bus);

Expand All @@ -809,9 +810,9 @@ static void usb_deregister_bus (struct usb_bus *bus)
* controller code, as well as having it call this when cleaning
* itself up
*/
down (&usb_bus_list_lock);
mutex_lock(&usb_bus_list_lock);
list_del (&bus->bus_list);
up (&usb_bus_list_lock);
mutex_unlock(&usb_bus_list_lock);

usb_notify_remove_bus(bus);

Expand Down Expand Up @@ -844,14 +845,14 @@ static int register_root_hub (struct usb_device *usb_dev,
set_bit (devnum, usb_dev->bus->devmap.devicemap);
usb_set_device_state(usb_dev, USB_STATE_ADDRESS);

down (&usb_bus_list_lock);
mutex_lock(&usb_bus_list_lock);
usb_dev->bus->root_hub = usb_dev;

usb_dev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
if (retval != sizeof usb_dev->descriptor) {
usb_dev->bus->root_hub = NULL;
up (&usb_bus_list_lock);
mutex_unlock(&usb_bus_list_lock);
dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
usb_dev->dev.bus_id, retval);
return (retval < 0) ? retval : -EMSGSIZE;
Expand All @@ -863,7 +864,7 @@ static int register_root_hub (struct usb_device *usb_dev,
dev_err (parent_dev, "can't register root hub for %s, %d\n",
usb_dev->dev.bus_id, retval);
}
up (&usb_bus_list_lock);
mutex_unlock(&usb_bus_list_lock);

if (retval == 0) {
spin_lock_irq (&hcd_root_hub_lock);
Expand Down Expand Up @@ -1891,9 +1892,9 @@ void usb_remove_hcd(struct usb_hcd *hcd)
hcd->rh_registered = 0;
spin_unlock_irq (&hcd_root_hub_lock);

down(&usb_bus_list_lock);
mutex_lock(&usb_bus_list_lock);
usb_disconnect(&hcd->self.root_hub);
up(&usb_bus_list_lock);
mutex_unlock(&usb_bus_list_lock);

hcd->poll_rh = 0;
del_timer_sync(&hcd->rh_timer);
Expand Down
2 changes: 1 addition & 1 deletion drivers/usb/core/hcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ extern void usb_set_device_state(struct usb_device *udev,
/* exported only within usbcore */

extern struct list_head usb_bus_list;
extern struct semaphore usb_bus_list_lock;
extern struct mutex usb_bus_list_lock;
extern wait_queue_head_t usb_kill_urb_queue;

extern struct usb_bus *usb_bus_get (struct usb_bus *bus);
Expand Down
7 changes: 4 additions & 3 deletions drivers/usb/core/hub.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <linux/usb.h>
#include <linux/usbdevice_fs.h>
#include <linux/kthread.h>
#include <linux/mutex.h>

#include <asm/semaphore.h>
#include <asm/uaccess.h>
Expand Down Expand Up @@ -2162,7 +2163,7 @@ static int
hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
int retry_counter)
{
static DECLARE_MUTEX(usb_address0_sem);
static DEFINE_MUTEX(usb_address0_mutex);

struct usb_device *hdev = hub->hdev;
int i, j, retval;
Expand All @@ -2183,7 +2184,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
if (oldspeed == USB_SPEED_LOW)
delay = HUB_LONG_RESET_TIME;

down(&usb_address0_sem);
mutex_lock(&usb_address0_mutex);

/* Reset the device; full speed may morph to high speed */
retval = hub_port_reset(hub, port1, udev, delay);
Expand Down Expand Up @@ -2381,7 +2382,7 @@ hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
fail:
if (retval)
hub_port_disable(hub, port1, 0);
up(&usb_address0_sem);
mutex_unlock(&usb_address0_mutex);
return retval;
}

Expand Down
15 changes: 8 additions & 7 deletions drivers/usb/core/notify.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,31 @@
#include <linux/kernel.h>
#include <linux/notifier.h>
#include <linux/usb.h>
#include <linux/mutex.h>
#include "usb.h"


static struct notifier_block *usb_notifier_list;
static DECLARE_MUTEX(usb_notifier_lock);
static DEFINE_MUTEX(usb_notifier_lock);

static void usb_notifier_chain_register(struct notifier_block **list,
struct notifier_block *n)
{
down(&usb_notifier_lock);
mutex_lock(&usb_notifier_lock);
while (*list) {
if (n->priority > (*list)->priority)
break;
list = &((*list)->next);
}
n->next = *list;
*list = n;
up(&usb_notifier_lock);
mutex_unlock(&usb_notifier_lock);
}

static void usb_notifier_chain_unregister(struct notifier_block **nl,
struct notifier_block *n)
{
down(&usb_notifier_lock);
mutex_lock(&usb_notifier_lock);
while ((*nl)!=NULL) {
if ((*nl)==n) {
*nl = n->next;
Expand All @@ -45,7 +46,7 @@ static void usb_notifier_chain_unregister(struct notifier_block **nl,
nl=&((*nl)->next);
}
exit:
up(&usb_notifier_lock);
mutex_unlock(&usb_notifier_lock);
}

static int usb_notifier_call_chain(struct notifier_block **n,
Expand All @@ -54,7 +55,7 @@ static int usb_notifier_call_chain(struct notifier_block **n,
int ret=NOTIFY_DONE;
struct notifier_block *nb = *n;

down(&usb_notifier_lock);
mutex_lock(&usb_notifier_lock);
while (nb) {
ret = nb->notifier_call(nb,val,v);
if (ret&NOTIFY_STOP_MASK) {
Expand All @@ -63,7 +64,7 @@ static int usb_notifier_call_chain(struct notifier_block **n,
nb = nb->next;
}
exit:
up(&usb_notifier_lock);
mutex_unlock(&usb_notifier_lock);
return ret;
}

Expand Down
Loading

0 comments on commit 4186ecf

Please sign in to comment.