Skip to content

Commit 5702f75

Browse files
author
Felipe Balbi
committed
usb: gadget: udc-core: move sysfs_notify() to a workqueue
usb_gadget_set_state() will call sysfs_notify() which might sleep. Some users might want to call usb_gadget_set_state() from the very IRQ handler which actually changes the gadget state. Instead of having every UDC driver add their own workqueue for such a simple notification, we're adding it generically to our struct usb_gadget, so the details are hidden from all UDC drivers. Acked-by: Alan Stern <[email protected]> Signed-off-by: Felipe Balbi <[email protected]>
1 parent c75f52f commit 5702f75

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

drivers/usb/gadget/udc-core.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/list.h>
2424
#include <linux/err.h>
2525
#include <linux/dma-mapping.h>
26+
#include <linux/workqueue.h>
2627

2728
#include <linux/usb/ch9.h>
2829
#include <linux/usb/gadget.h>
@@ -105,11 +106,18 @@ EXPORT_SYMBOL_GPL(usb_gadget_unmap_request);
105106

106107
/* ------------------------------------------------------------------------- */
107108

109+
static void usb_gadget_state_work(struct work_struct *work)
110+
{
111+
struct usb_gadget *gadget = work_to_gadget(work);
112+
113+
sysfs_notify(&gadget->dev.kobj, NULL, "status");
114+
}
115+
108116
void usb_gadget_set_state(struct usb_gadget *gadget,
109117
enum usb_device_state state)
110118
{
111119
gadget->state = state;
112-
sysfs_notify(&gadget->dev.kobj, NULL, "status");
120+
schedule_work(&gadget->work);
113121
}
114122
EXPORT_SYMBOL_GPL(usb_gadget_set_state);
115123

@@ -196,6 +204,7 @@ int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget,
196204
goto err1;
197205

198206
dev_set_name(&gadget->dev, "gadget");
207+
INIT_WORK(&gadget->work, usb_gadget_state_work);
199208
gadget->dev.parent = parent;
200209

201210
#ifdef CONFIG_HAS_DMA
@@ -315,6 +324,7 @@ void usb_del_gadget_udc(struct usb_gadget *gadget)
315324
usb_gadget_remove_driver(udc);
316325

317326
kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE);
327+
flush_work(&gadget->work);
318328
device_unregister(&udc->dev);
319329
device_unregister(&gadget->dev);
320330
}

include/linux/usb/gadget.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/slab.h>
2323
#include <linux/scatterlist.h>
2424
#include <linux/types.h>
25+
#include <linux/workqueue.h>
2526
#include <linux/usb/ch9.h>
2627

2728
struct usb_ep;
@@ -475,6 +476,7 @@ struct usb_gadget_ops {
475476

476477
/**
477478
* struct usb_gadget - represents a usb slave device
479+
* @work: (internal use) Workqueue to be used for sysfs_notify()
478480
* @ops: Function pointers used to access hardware-specific operations.
479481
* @ep0: Endpoint zero, used when reading or writing responses to
480482
* driver setup() requests
@@ -520,6 +522,7 @@ struct usb_gadget_ops {
520522
* device is acting as a B-Peripheral (so is_a_peripheral is false).
521523
*/
522524
struct usb_gadget {
525+
struct work_struct work;
523526
/* readonly to gadget driver */
524527
const struct usb_gadget_ops *ops;
525528
struct usb_ep *ep0;
@@ -538,6 +541,7 @@ struct usb_gadget {
538541
unsigned out_epnum;
539542
unsigned in_epnum;
540543
};
544+
#define work_to_gadget(w) (container_of((w), struct usb_gadget, work))
541545

542546
static inline void set_gadget_data(struct usb_gadget *gadget, void *data)
543547
{ dev_set_drvdata(&gadget->dev, data); }

0 commit comments

Comments
 (0)