diff options
Diffstat (limited to 'drivers/usb/usb-skeleton.c')
-rw-r--r-- | drivers/usb/usb-skeleton.c | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/drivers/usb/usb-skeleton.c b/drivers/usb/usb-skeleton.c index 5c6c1bdbd455..8efeae24764f 100644 --- a/drivers/usb/usb-skeleton.c +++ b/drivers/usb/usb-skeleton.c | |||
@@ -27,6 +27,8 @@ | |||
27 | #define USB_SKEL_VENDOR_ID 0xfff0 | 27 | #define USB_SKEL_VENDOR_ID 0xfff0 |
28 | #define USB_SKEL_PRODUCT_ID 0xfff0 | 28 | #define USB_SKEL_PRODUCT_ID 0xfff0 |
29 | 29 | ||
30 | static DEFINE_MUTEX(skel_mutex); | ||
31 | |||
30 | /* table of devices that work with this driver */ | 32 | /* table of devices that work with this driver */ |
31 | static const struct usb_device_id skel_table[] = { | 33 | static const struct usb_device_id skel_table[] = { |
32 | { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) }, | 34 | { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) }, |
@@ -60,7 +62,6 @@ struct usb_skel { | |||
60 | __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */ | 62 | __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */ |
61 | __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */ | 63 | __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */ |
62 | int errors; /* the last request tanked */ | 64 | int errors; /* the last request tanked */ |
63 | int open_count; /* count the number of openers */ | ||
64 | bool ongoing_read; /* a read is going on */ | 65 | bool ongoing_read; /* a read is going on */ |
65 | bool processed_urb; /* indicates we haven't processed the urb */ | 66 | bool processed_urb; /* indicates we haven't processed the urb */ |
66 | spinlock_t err_lock; /* lock for errors */ | 67 | spinlock_t err_lock; /* lock for errors */ |
@@ -100,39 +101,37 @@ static int skel_open(struct inode *inode, struct file *file) | |||
100 | goto exit; | 101 | goto exit; |
101 | } | 102 | } |
102 | 103 | ||
104 | mutex_lock(&skel_mutex); | ||
103 | dev = usb_get_intfdata(interface); | 105 | dev = usb_get_intfdata(interface); |
104 | if (!dev) { | 106 | if (!dev) { |
107 | mutex_unlock(&skel_mutex); | ||
105 | retval = -ENODEV; | 108 | retval = -ENODEV; |
106 | goto exit; | 109 | goto exit; |
107 | } | 110 | } |
108 | 111 | ||
109 | /* increment our usage count for the device */ | 112 | /* increment our usage count for the device */ |
110 | kref_get(&dev->kref); | 113 | kref_get(&dev->kref); |
114 | mutex_unlock(&skel_mutex); | ||
111 | 115 | ||
112 | /* lock the device to allow correctly handling errors | 116 | /* lock the device to allow correctly handling errors |
113 | * in resumption */ | 117 | * in resumption */ |
114 | mutex_lock(&dev->io_mutex); | 118 | mutex_lock(&dev->io_mutex); |
119 | if (!dev->interface) { | ||
120 | retval = -ENODEV; | ||
121 | goto out_err; | ||
122 | } | ||
115 | 123 | ||
116 | if (!dev->open_count++) { | 124 | retval = usb_autopm_get_interface(interface); |
117 | retval = usb_autopm_get_interface(interface); | 125 | if (retval) |
118 | if (retval) { | 126 | goto out_err; |
119 | dev->open_count--; | ||
120 | mutex_unlock(&dev->io_mutex); | ||
121 | kref_put(&dev->kref, skel_delete); | ||
122 | goto exit; | ||
123 | } | ||
124 | } /* else { //uncomment this block if you want exclusive open | ||
125 | retval = -EBUSY; | ||
126 | dev->open_count--; | ||
127 | mutex_unlock(&dev->io_mutex); | ||
128 | kref_put(&dev->kref, skel_delete); | ||
129 | goto exit; | ||
130 | } */ | ||
131 | /* prevent the device from being autosuspended */ | ||
132 | 127 | ||
133 | /* save our object in the file's private structure */ | 128 | /* save our object in the file's private structure */ |
134 | file->private_data = dev; | 129 | file->private_data = dev; |
130 | |||
131 | out_err: | ||
135 | mutex_unlock(&dev->io_mutex); | 132 | mutex_unlock(&dev->io_mutex); |
133 | if (retval) | ||
134 | kref_put(&dev->kref, skel_delete); | ||
136 | 135 | ||
137 | exit: | 136 | exit: |
138 | return retval; | 137 | return retval; |
@@ -148,7 +147,7 @@ static int skel_release(struct inode *inode, struct file *file) | |||
148 | 147 | ||
149 | /* allow the device to be autosuspended */ | 148 | /* allow the device to be autosuspended */ |
150 | mutex_lock(&dev->io_mutex); | 149 | mutex_lock(&dev->io_mutex); |
151 | if (!--dev->open_count && dev->interface) | 150 | if (dev->interface) |
152 | usb_autopm_put_interface(dev->interface); | 151 | usb_autopm_put_interface(dev->interface); |
153 | mutex_unlock(&dev->io_mutex); | 152 | mutex_unlock(&dev->io_mutex); |
154 | 153 | ||
@@ -612,7 +611,6 @@ static void skel_disconnect(struct usb_interface *interface) | |||
612 | int minor = interface->minor; | 611 | int minor = interface->minor; |
613 | 612 | ||
614 | dev = usb_get_intfdata(interface); | 613 | dev = usb_get_intfdata(interface); |
615 | usb_set_intfdata(interface, NULL); | ||
616 | 614 | ||
617 | /* give back our minor */ | 615 | /* give back our minor */ |
618 | usb_deregister_dev(interface, &skel_class); | 616 | usb_deregister_dev(interface, &skel_class); |
@@ -624,8 +622,12 @@ static void skel_disconnect(struct usb_interface *interface) | |||
624 | 622 | ||
625 | usb_kill_anchored_urbs(&dev->submitted); | 623 | usb_kill_anchored_urbs(&dev->submitted); |
626 | 624 | ||
625 | mutex_lock(&skel_mutex); | ||
626 | usb_set_intfdata(interface, NULL); | ||
627 | |||
627 | /* decrement our usage count */ | 628 | /* decrement our usage count */ |
628 | kref_put(&dev->kref, skel_delete); | 629 | kref_put(&dev->kref, skel_delete); |
630 | mutex_unlock(&skel_mutex); | ||
629 | 631 | ||
630 | dev_info(&interface->dev, "USB Skeleton #%d now disconnected", minor); | 632 | dev_info(&interface->dev, "USB Skeleton #%d now disconnected", minor); |
631 | } | 633 | } |