usb: dwc3: allocate gadget structure dynamically
Subsystems (2)
PATCH 1/2 usb: dwc3: allocate gadget structure dynamically
The current code uses commit fac323471df6 ("usb: udc: allow adding and removing the same gadget device") as the workaround to let the gadget device is re-used, but it is not allowed from driver core point. In this commit, we allocate gadget structure dynamically, and free it at its release function. Since the gadget device's driver_data has already occupied by usb_composite_dev structure, we have to use gadget device's platform data to store dwc3 structure.
--- drivers/usb/dwc3/core.h | 2 +- drivers/usb/dwc3/ep0.c | 26 +++++----- drivers/usb/dwc3/gadget.c | 102 ++++++++++++++++++++++---------------- drivers/usb/dwc3/gadget.h | 2 +- 4 files changed, 74 insertions(+), 58 deletions(-)
drivers/usb/dwc3/core.h +1 -1
@@ -1102,7 +1102,7 @@ struct dwc3 {struct dwc3_event_buffer *ev_buf;struct dwc3_ep *eps[DWC3_ENDPOINTS_NUM ]; -struct usb_gadget gadget; +struct usb_gadget *gadget;struct usb_gadget_driver *gadget_driver;struct clk_bulk_data *clks;
drivers/usb/dwc3/ep0.c +13 -13
@@ -131,7 +131,7 @@ static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep, direction = !dwc->ep0_expect_in; dwc->delayed_status = false; -usb_gadget_set_state (&dwc->gadget,USB_STATE_CONFIGURED ); +usb_gadget_set_state (dwc->gadget,USB_STATE_CONFIGURED );if (dwc->ep0state ==EP0_STATUS_PHASE )__dwc3_ep0_do_control_status (dwc, dwc->eps[direction]);
@@ -325,7 +325,7 @@ static int dwc3_ep0_handle_status(struct dwc3 *dwc,/* * LTM will be set once we know how to set this in HW. */ - usb_status |= dwc->gadget.is_selfpowered; + usb_status |= dwc->gadget->is_selfpowered;if ((dwc->speed ==DWC3_DSTS_SUPERSPEED ) || (dwc->speed ==DWC3_DSTS_SUPERSPEED_PLUS )) {
@@ -450,7 +450,7 @@ static int dwc3_ep0_handle_device(struct dwc3 *dwc, wValue =le16_to_cpu (ctrl->wValue); wIndex =le16_to_cpu (ctrl->wIndex); - state = dwc->gadget.state; + state = dwc->gadget->state;switch (wValue) {case USB_DEVICE_REMOTE_WAKEUP :
@@ -559,7 +559,7 @@ static int dwc3_ep0_handle_feature(struct dwc3 *dwc,static int dwc3_ep0_set_address (struct dwc3 *dwc,struct usb_ctrlrequest *ctrl) { -enum usb_device_state state = dwc->gadget.state; +enum usb_device_state state = dwc->gadget->state;u32 addr;u32 reg;
@@ -580,9 +580,9 @@ static int dwc3_ep0_set_address(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)dwc3_writel (dwc->regs,DWC3_DCFG , reg);if (addr) -usb_gadget_set_state (&dwc->gadget,USB_STATE_ADDRESS ); +usb_gadget_set_state (dwc->gadget,USB_STATE_ADDRESS );else -usb_gadget_set_state (&dwc->gadget,USB_STATE_DEFAULT ); +usb_gadget_set_state (dwc->gadget,USB_STATE_DEFAULT );return 0 ; }
@@ -592,14 +592,14 @@ static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)int ret;spin_unlock (&dwc->lock); - ret = dwc->gadget_driver->setup (&dwc->gadget, ctrl); + ret = dwc->gadget_driver->setup (dwc->gadget, ctrl);spin_lock (&dwc->lock);return ret; }static int dwc3_ep0_set_config (struct dwc3 *dwc,struct usb_ctrlrequest *ctrl) { -enum usb_device_state state = dwc->gadget.state; +enum usb_device_state state = dwc->gadget->state;u32 cfg;int ret;u32 reg;
@@ -622,7 +622,7 @@ static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)* to change the state on the next usb_ep_queue() */ if (ret ==0 ) -usb_gadget_set_state (&dwc->gadget, +usb_gadget_set_state (dwc->gadget,USB_STATE_CONFIGURED );/*
@@ -641,7 +641,7 @@ static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)case USB_STATE_CONFIGURED : ret =dwc3_ep0_delegate_req (dwc, ctrl);if (!cfg && !ret) -usb_gadget_set_state (&dwc->gadget, +usb_gadget_set_state (dwc->gadget,USB_STATE_ADDRESS );break ;default :
@@ -697,7 +697,7 @@ static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)static int dwc3_ep0_set_sel (struct dwc3 *dwc,struct usb_ctrlrequest *ctrl) {struct dwc3_ep *dep; -enum usb_device_state state = dwc->gadget.state; +enum usb_device_state state = dwc->gadget->state;u16 wLength;if (state ==USB_STATE_DEFAULT )
@@ -741,7 +741,7 @@ static int dwc3_ep0_set_isoch_delay(struct dwc3 *dwc, struct usb_ctrlrequest *ctif (wIndex || wLength)return -EINVAL ; - dwc->gadget.isoch_delay = wValue; + dwc->gadget->isoch_delay = wValue;return 0 ; }
@@ -1102,7 +1102,7 @@ static void dwc3_ep0_xfernotready(struct dwc3 *dwc,*/ if (!list_empty (&dep->pending_list)) { dwc->delayed_status = false; -usb_gadget_set_state (&dwc->gadget, +usb_gadget_set_state (dwc->gadget,USB_STATE_CONFIGURED );dwc3_ep0_do_control_status (dwc, event); }
drivers/usb/dwc3/gadget.c +59 -43
276 lines not on this page · raw message
drivers/usb/dwc3/gadget.h +1 -1
@@ -17,7 +17,7 @@struct dwc3 ;#define to_dwc3_ep (ep) (container_of(ep, struct dwc3_ep, endpoint)) -#define gadget_to_dwc (g) (container_of(g, struct dwc3, gadget)) +#define gadget_to_dwc (g) (dev_get_platdata(&g->dev))/* DEPCFG parameter 1 */ #define DWC3_DEPCFG_INT_NUM (n) (((n) & 0x1f) << 0)
Signature · 1 line
2.17.1
PATCH 2/2 Revert "usb: udc: allow adding and removing the same gadget device"
We have already allocated gadget structure dynamically at UDC (dwc3) driver, so commit fac323471df6 ("usb: udc: allow adding and removing the same gadget device")could be reverted.
--- drivers/usb/gadget/udc/core.c | 1 - 1 file changed, 1 deletion(-)
drivers/usb/gadget/udc/core.c +0 -1
@@ -1338,7 +1338,6 @@ void usb_del_gadget_udc(struct usb_gadget *gadget)flush_work (&gadget->work);device_unregister (&udc->dev);device_unregister (&gadget->dev); -memset (&gadget->dev,0x00 ,sizeof (gadget->dev)); }EXPORT_SYMBOL_GPL (usb_del_gadget_udc);
Signature · 1 line
2.17.1
Quoted 11 lines · Peter Chen
The current code uses commit
fac323471df6("usb: udc: allow adding and removing the same gadget device") as the workaround to let the gadget device is re-used, but it is not allowed from driver core point. In this commit, we allocate gadget structure dynamically, and free it at its release function. Since the gadget device'sdriver_datahas already occupied byusb_composite_devstructure, we have to use gadget device's platform data to store dwc3 structure.Cc Greg Kroah-Hartman <[email protected]>Cc Alan Stern <[email protected]>Signed-off-by Peter Chen <[email protected]>
There's one problem with this patch...
Quoted 16 lines
drivers/usb/dwc3/gadget.c +3
-4@@ -3670,21 +3685,22 @@ int dwc3_gadget_init(struct dwc3 *dwc) ret =dwc3_gadget_init_endpoints (dwc, dwc->num_eps);if (ret) - goto err3; + goto err4; - ret =usb_add_gadget_udc (dwc->dev, &dwc->gadget); + ret =usb_add_gadget_udc_release (dwc->dev, dwc->gadget, dwc_gadget_release);if (ret) {dev_err (dwc->dev,"failed to register udc\n" ); - goto err4; + goto err5; } -dwc3_gadget_set_speed (&dwc->gadget, dwc->maximum_speed);
drivers/usb/dwc3/gadget.c int dwc3_gadget_init(struct dwc3 *dwc) · line 3703
Quoted 10 lines
drivers/usb/dwc3/gadget.c +4
-2+dwc3_gadget_set_speed (dwc->gadget, dwc->maximum_speed);return 0 ; -err4: +err5:dwc3_gadget_free_endpoints (dwc); - +err4: +kfree (dwc->gadget);
Once you call usb_add_gadget_udc_release() -- even if the call fails -- you must not call kfree() directly. Instead you have to do put_device(&dwc->gadget.dev). That's a little awkward given the code arrangement here.
One of the advantages of the patch set I posted recently was that it straightens out this whole situation. Immediately after allocating the gadget structure you can call usb_initialize_gadget(), and after that you call usb_put_gadget() to decrement the refcount -- just like everything else in the device model.
Perhaps you should rewrite this part of your patch to rely on my work.
Alan Stern
Quoted 35 lines · Alan Stern
Quoted 11 lines · Peter Chen
The current code uses commit
fac323471df6("usb: udc: allow adding and removing the same gadget device") as the workaround to let the gadget device is re-used, but it is not allowed from driver core point. In this commit, we allocate gadget structure dynamically, and free it at its release function. Since the gadget device'sdriver_datahas already occupied byusb_composite_devstructure, we have to use gadget device's platform data to store dwc3 structure.Cc Greg Kroah-Hartman <[email protected]>Cc Alan Stern <[email protected]>Signed-off-by Peter Chen <[email protected]>There's one problem with this patch...
Quoted 20 lines
drivers/usb/dwc3/gadget.c +4
-4@@ -3670,21 +3685,22 @@ int dwc3_gadget_init(struct dwc3 *dwc) ret =dwc3_gadget_init_endpoints (dwc, dwc->num_eps);if (ret) - goto err3; + goto err4; - ret =usb_add_gadget_udc (dwc->dev, &dwc->gadget); + ret =usb_add_gadget_udc_release (dwc->dev, dwc->gadget, dwc_gadget_release);if (ret) {dev_err (dwc->dev,"failed to register udc\n" ); - goto err4; + goto err5; } -dwc3_gadget_set_speed (&dwc->gadget, dwc->maximum_speed); +dwc3_gadget_set_speed (dwc->gadget, dwc->maximum_speed);return 0 ;
Quoted 11 lines
drivers/usb/dwc3/gadget.c int dwc3_gadget_init(struct dwc3 *dwc) · line 3703
Quoted 6 lines
drivers/usb/dwc3/gadget.c +3
-2-err4: +err5:dwc3_gadget_free_endpoints (dwc); - +err4: +kfree (dwc->gadget);Once you call
usb_add_gadget_udc_release()-- even if the call fails -- you must not callkfree()directly. Instead you have to doput_device(&dwc->gadget.dev). That's a little awkward given the code arrangement here.
Sorry, I got that wrong. If usb_add_gadget_udc_release() succeeds then you must call put_device() in order to destroy the gadget. But if it fails then it calls put_device() for you, so that dwc->gadget is a stale pointer. In this case you must not call either kfree() or put_device().
Alan Stern