Skip to content

Commit 9af6f26

Browse files
committed
Merge tag 'usb-4.9-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
Pull USB fixes from Greg KH: "Here are a number of small USB driver fixes for 4.9-rc3. There is the usual number of gadget and xhci patches in here to resolved reported issues, as well as some usb-serial driver fixes and new device ids. All have been in linux-next with no reported issues" * tag 'usb-4.9-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb: (26 commits) usb: chipidea: host: fix NULL ptr dereference during shutdown usb: renesas_usbhs: add wait after initialization for R-Car Gen3 usb: increase ohci watchdog delay to 275 msec usb: musb: Call pm_runtime from musb_gadget_queue usb: musb: Fix hardirq-safe hardirq-unsafe lock order error usb: ehci-platform: increase EHCI_MAX_RSTS to 4 usb: ohci-at91: Set RemoteWakeupConnected bit explicitly. USB: serial: fix potential NULL-dereference at probe xhci: use default USB_RESUME_TIMEOUT when resuming ports. xhci: workaround for hosts missing CAS bit xhci: add restart quirk for Intel Wildcatpoint PCH USB: serial: cp210x: fix tiocmget error handling wusb: fix error return code in wusb_prf() Revert "Documentation: devicetree: dwc2: Deprecate g-tx-fifo-size" Revert "usb: dwc2: gadget: fix TX FIFO size and address initialization" Revert "usb: dwc2: gadget: change variable name to more meaningful" USB: serial: ftdi_sio: add support for Infineon TriBoard TC2X7 wusb: Stop using the stack for sg crypto scratch space usb: dwc3: Fix size used in dma_free_coherent() usb: gadget: f_fs: stop sleeping in ffs_func_eps_disable ...
2 parents c067aff + c1aa677 commit 9af6f26

23 files changed

Lines changed: 296 additions & 84 deletions

File tree

Documentation/devicetree/bindings/usb/dwc2.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ Refer to phy/phy-bindings.txt for generic phy consumer properties
2828
- g-use-dma: enable dma usage in gadget driver.
2929
- g-rx-fifo-size: size of rx fifo size in gadget mode.
3030
- g-np-tx-fifo-size: size of non-periodic tx fifo size in gadget mode.
31-
32-
Deprecated properties:
33-
- g-tx-fifo-size: size of periodic tx fifo per endpoint (except ep0)
34-
in gadget mode.
31+
- g-tx-fifo-size: size of periodic tx fifo per endpoint (except ep0) in gadget mode.
3532

3633
Example:
3734

drivers/usb/chipidea/host.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ static void host_stop(struct ci_hdrc *ci)
188188

189189
if (hcd) {
190190
usb_remove_hcd(hcd);
191+
ci->role = CI_ROLE_END;
192+
synchronize_irq(ci->irq);
191193
usb_put_hcd(hcd);
192194
if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
193195
(ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))

drivers/usb/dwc2/core.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,9 +463,18 @@ static void dwc2_clear_force_mode(struct dwc2_hsotg *hsotg)
463463
*/
464464
void dwc2_force_dr_mode(struct dwc2_hsotg *hsotg)
465465
{
466+
bool ret;
467+
466468
switch (hsotg->dr_mode) {
467469
case USB_DR_MODE_HOST:
468-
dwc2_force_mode(hsotg, true);
470+
ret = dwc2_force_mode(hsotg, true);
471+
/*
472+
* NOTE: This is required for some rockchip soc based
473+
* platforms on their host-only dwc2.
474+
*/
475+
if (!ret)
476+
msleep(50);
477+
469478
break;
470479
case USB_DR_MODE_PERIPHERAL:
471480
dwc2_force_mode(hsotg, false);

drivers/usb/dwc2/core.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ enum dwc2_lx_state {
259259
DWC2_L3, /* Off state */
260260
};
261261

262+
/*
263+
* Gadget periodic tx fifo sizes as used by legacy driver
264+
* EP0 is not included
265+
*/
266+
#define DWC2_G_P_LEGACY_TX_FIFO_SIZE {256, 256, 256, 256, 768, 768, 768, \
267+
768, 0, 0, 0, 0, 0, 0, 0}
268+
262269
/* Gadget ep0 states */
263270
enum dwc2_ep0_state {
264271
DWC2_EP0_SETUP,

drivers/usb/dwc2/gadget.c

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,9 @@ static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
186186
*/
187187
static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
188188
{
189-
unsigned int fifo;
189+
unsigned int ep;
190190
unsigned int addr;
191191
int timeout;
192-
u32 dptxfsizn;
193192
u32 val;
194193

195194
/* Reset fifo map if not correctly cleared during previous session */
@@ -217,16 +216,16 @@ static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
217216
* them to endpoints dynamically according to maxpacket size value of
218217
* given endpoint.
219218
*/
220-
for (fifo = 1; fifo < MAX_EPS_CHANNELS; fifo++) {
221-
dptxfsizn = dwc2_readl(hsotg->regs + DPTXFSIZN(fifo));
222-
223-
val = (dptxfsizn & FIFOSIZE_DEPTH_MASK) | addr;
224-
addr += dptxfsizn >> FIFOSIZE_DEPTH_SHIFT;
225-
226-
if (addr > hsotg->fifo_mem)
227-
break;
219+
for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
220+
if (!hsotg->g_tx_fifo_sz[ep])
221+
continue;
222+
val = addr;
223+
val |= hsotg->g_tx_fifo_sz[ep] << FIFOSIZE_DEPTH_SHIFT;
224+
WARN_ONCE(addr + hsotg->g_tx_fifo_sz[ep] > hsotg->fifo_mem,
225+
"insufficient fifo memory");
226+
addr += hsotg->g_tx_fifo_sz[ep];
228227

229-
dwc2_writel(val, hsotg->regs + DPTXFSIZN(fifo));
228+
dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep));
230229
}
231230

232231
/*
@@ -3807,10 +3806,36 @@ static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
38073806
static void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg)
38083807
{
38093808
struct device_node *np = hsotg->dev->of_node;
3809+
u32 len = 0;
3810+
u32 i = 0;
38103811

38113812
/* Enable dma if requested in device tree */
38123813
hsotg->g_using_dma = of_property_read_bool(np, "g-use-dma");
38133814

3815+
/*
3816+
* Register TX periodic fifo size per endpoint.
3817+
* EP0 is excluded since it has no fifo configuration.
3818+
*/
3819+
if (!of_find_property(np, "g-tx-fifo-size", &len))
3820+
goto rx_fifo;
3821+
3822+
len /= sizeof(u32);
3823+
3824+
/* Read tx fifo sizes other than ep0 */
3825+
if (of_property_read_u32_array(np, "g-tx-fifo-size",
3826+
&hsotg->g_tx_fifo_sz[1], len))
3827+
goto rx_fifo;
3828+
3829+
/* Add ep0 */
3830+
len++;
3831+
3832+
/* Make remaining TX fifos unavailable */
3833+
if (len < MAX_EPS_CHANNELS) {
3834+
for (i = len; i < MAX_EPS_CHANNELS; i++)
3835+
hsotg->g_tx_fifo_sz[i] = 0;
3836+
}
3837+
3838+
rx_fifo:
38143839
/* Register RX fifo size */
38153840
of_property_read_u32(np, "g-rx-fifo-size", &hsotg->g_rx_fifo_sz);
38163841

@@ -3832,10 +3857,13 @@ int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
38323857
struct device *dev = hsotg->dev;
38333858
int epnum;
38343859
int ret;
3860+
int i;
3861+
u32 p_tx_fifo[] = DWC2_G_P_LEGACY_TX_FIFO_SIZE;
38353862

38363863
/* Initialize to legacy fifo configuration values */
38373864
hsotg->g_rx_fifo_sz = 2048;
38383865
hsotg->g_np_g_tx_fifo_sz = 1024;
3866+
memcpy(&hsotg->g_tx_fifo_sz[1], p_tx_fifo, sizeof(p_tx_fifo));
38393867
/* Device tree specific probe */
38403868
dwc2_hsotg_of_probe(hsotg);
38413869

@@ -3853,6 +3881,9 @@ int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
38533881
dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
38543882
hsotg->g_np_g_tx_fifo_sz);
38553883
dev_dbg(dev, "RXFIFO size: %d\n", hsotg->g_rx_fifo_sz);
3884+
for (i = 0; i < MAX_EPS_CHANNELS; i++)
3885+
dev_dbg(dev, "Periodic TXFIFO%2d size: %d\n", i,
3886+
hsotg->g_tx_fifo_sz[i]);
38563887

38573888
hsotg->gadget.max_speed = USB_SPEED_HIGH;
38583889
hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;

drivers/usb/dwc3/gadget.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
783783
req->trb = trb;
784784
req->trb_dma = dwc3_trb_dma_offset(dep, trb);
785785
req->first_trb_index = dep->trb_enqueue;
786+
dep->queued_requests++;
786787
}
787788

788789
dwc3_ep_inc_enq(dep);
@@ -833,8 +834,6 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
833834

834835
trb->ctrl |= DWC3_TRB_CTRL_HWO;
835836

836-
dep->queued_requests++;
837-
838837
trace_dwc3_prepare_trb(dep, trb);
839838
}
840839

@@ -1074,9 +1073,17 @@ static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
10741073

10751074
list_add_tail(&req->list, &dep->pending_list);
10761075

1077-
if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1078-
dep->flags & DWC3_EP_PENDING_REQUEST) {
1079-
if (list_empty(&dep->started_list)) {
1076+
/*
1077+
* NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1078+
* wait for a XferNotReady event so we will know what's the current
1079+
* (micro-)frame number.
1080+
*
1081+
* Without this trick, we are very, very likely gonna get Bus Expiry
1082+
* errors which will force us issue EndTransfer command.
1083+
*/
1084+
if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1085+
if ((dep->flags & DWC3_EP_PENDING_REQUEST) &&
1086+
list_empty(&dep->started_list)) {
10801087
dwc3_stop_active_transfer(dwc, dep->number, true);
10811088
dep->flags = DWC3_EP_ENABLED;
10821089
}
@@ -1861,8 +1868,11 @@ static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
18611868
unsigned int s_pkt = 0;
18621869
unsigned int trb_status;
18631870

1864-
dep->queued_requests--;
18651871
dwc3_ep_inc_deq(dep);
1872+
1873+
if (req->trb == trb)
1874+
dep->queued_requests--;
1875+
18661876
trace_dwc3_complete_trb(dep, trb);
18671877

18681878
/*
@@ -2980,7 +2990,7 @@ int dwc3_gadget_init(struct dwc3 *dwc)
29802990
kfree(dwc->setup_buf);
29812991

29822992
err2:
2983-
dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2993+
dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
29842994
dwc->ep0_trb, dwc->ep0_trb_addr);
29852995

29862996
err1:
@@ -3005,7 +3015,7 @@ void dwc3_gadget_exit(struct dwc3 *dwc)
30053015
kfree(dwc->setup_buf);
30063016
kfree(dwc->zlp_buf);
30073017

3008-
dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
3018+
dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
30093019
dwc->ep0_trb, dwc->ep0_trb_addr);
30103020

30113021
dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),

drivers/usb/gadget/function/f_fs.c

Lines changed: 92 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,60 @@ struct ffs_epfile {
136136
/*
137137
* Buffer for holding data from partial reads which may happen since
138138
* we’re rounding user read requests to a multiple of a max packet size.
139+
*
140+
* The pointer is initialised with NULL value and may be set by
141+
* __ffs_epfile_read_data function to point to a temporary buffer.
142+
*
143+
* In normal operation, calls to __ffs_epfile_read_buffered will consume
144+
* data from said buffer and eventually free it. Importantly, while the
145+
* function is using the buffer, it sets the pointer to NULL. This is
146+
* all right since __ffs_epfile_read_data and __ffs_epfile_read_buffered
147+
* can never run concurrently (they are synchronised by epfile->mutex)
148+
* so the latter will not assign a new value to the pointer.
149+
*
150+
* Meanwhile ffs_func_eps_disable frees the buffer (if the pointer is
151+
* valid) and sets the pointer to READ_BUFFER_DROP value. This special
152+
* value is crux of the synchronisation between ffs_func_eps_disable and
153+
* __ffs_epfile_read_data.
154+
*
155+
* Once __ffs_epfile_read_data is about to finish it will try to set the
156+
* pointer back to its old value (as described above), but seeing as the
157+
* pointer is not-NULL (namely READ_BUFFER_DROP) it will instead free
158+
* the buffer.
159+
*
160+
* == State transitions ==
161+
*
162+
* • ptr == NULL: (initial state)
163+
* ◦ __ffs_epfile_read_buffer_free: go to ptr == DROP
164+
* ◦ __ffs_epfile_read_buffered: nop
165+
* ◦ __ffs_epfile_read_data allocates temp buffer: go to ptr == buf
166+
* ◦ reading finishes: n/a, not in ‘and reading’ state
167+
* • ptr == DROP:
168+
* ◦ __ffs_epfile_read_buffer_free: nop
169+
* ◦ __ffs_epfile_read_buffered: go to ptr == NULL
170+
* ◦ __ffs_epfile_read_data allocates temp buffer: free buf, nop
171+
* ◦ reading finishes: n/a, not in ‘and reading’ state
172+
* • ptr == buf:
173+
* ◦ __ffs_epfile_read_buffer_free: free buf, go to ptr == DROP
174+
* ◦ __ffs_epfile_read_buffered: go to ptr == NULL and reading
175+
* ◦ __ffs_epfile_read_data: n/a, __ffs_epfile_read_buffered
176+
* is always called first
177+
* ◦ reading finishes: n/a, not in ‘and reading’ state
178+
* • ptr == NULL and reading:
179+
* ◦ __ffs_epfile_read_buffer_free: go to ptr == DROP and reading
180+
* ◦ __ffs_epfile_read_buffered: n/a, mutex is held
181+
* ◦ __ffs_epfile_read_data: n/a, mutex is held
182+
* ◦ reading finishes and …
183+
* … all data read: free buf, go to ptr == NULL
184+
* … otherwise: go to ptr == buf and reading
185+
* • ptr == DROP and reading:
186+
* ◦ __ffs_epfile_read_buffer_free: nop
187+
* ◦ __ffs_epfile_read_buffered: n/a, mutex is held
188+
* ◦ __ffs_epfile_read_data: n/a, mutex is held
189+
* ◦ reading finishes: free buf, go to ptr == DROP
139190
*/
140-
struct ffs_buffer *read_buffer; /* P: epfile->mutex */
191+
struct ffs_buffer *read_buffer;
192+
#define READ_BUFFER_DROP ((struct ffs_buffer *)ERR_PTR(-ESHUTDOWN))
141193

142194
char name[5];
143195

@@ -736,25 +788,47 @@ static void ffs_epfile_async_io_complete(struct usb_ep *_ep,
736788
schedule_work(&io_data->work);
737789
}
738790

791+
static void __ffs_epfile_read_buffer_free(struct ffs_epfile *epfile)
792+
{
793+
/*
794+
* See comment in struct ffs_epfile for full read_buffer pointer
795+
* synchronisation story.
796+
*/
797+
struct ffs_buffer *buf = xchg(&epfile->read_buffer, READ_BUFFER_DROP);
798+
if (buf && buf != READ_BUFFER_DROP)
799+
kfree(buf);
800+
}
801+
739802
/* Assumes epfile->mutex is held. */
740803
static ssize_t __ffs_epfile_read_buffered(struct ffs_epfile *epfile,
741804
struct iov_iter *iter)
742805
{
743-
struct ffs_buffer *buf = epfile->read_buffer;
806+
/*
807+
* Null out epfile->read_buffer so ffs_func_eps_disable does not free
808+
* the buffer while we are using it. See comment in struct ffs_epfile
809+
* for full read_buffer pointer synchronisation story.
810+
*/
811+
struct ffs_buffer *buf = xchg(&epfile->read_buffer, NULL);
744812
ssize_t ret;
745-
if (!buf)
813+
if (!buf || buf == READ_BUFFER_DROP)
746814
return 0;
747815

748816
ret = copy_to_iter(buf->data, buf->length, iter);
749817
if (buf->length == ret) {
750818
kfree(buf);
751-
epfile->read_buffer = NULL;
752-
} else if (unlikely(iov_iter_count(iter))) {
819+
return ret;
820+
}
821+
822+
if (unlikely(iov_iter_count(iter))) {
753823
ret = -EFAULT;
754824
} else {
755825
buf->length -= ret;
756826
buf->data += ret;
757827
}
828+
829+
if (cmpxchg(&epfile->read_buffer, NULL, buf))
830+
kfree(buf);
831+
758832
return ret;
759833
}
760834

@@ -783,7 +857,15 @@ static ssize_t __ffs_epfile_read_data(struct ffs_epfile *epfile,
783857
buf->length = data_len;
784858
buf->data = buf->storage;
785859
memcpy(buf->storage, data + ret, data_len);
786-
epfile->read_buffer = buf;
860+
861+
/*
862+
* At this point read_buffer is NULL or READ_BUFFER_DROP (if
863+
* ffs_func_eps_disable has been called in the meanwhile). See comment
864+
* in struct ffs_epfile for full read_buffer pointer synchronisation
865+
* story.
866+
*/
867+
if (unlikely(cmpxchg(&epfile->read_buffer, NULL, buf)))
868+
kfree(buf);
787869

788870
return ret;
789871
}
@@ -1097,8 +1179,7 @@ ffs_epfile_release(struct inode *inode, struct file *file)
10971179

10981180
ENTER();
10991181

1100-
kfree(epfile->read_buffer);
1101-
epfile->read_buffer = NULL;
1182+
__ffs_epfile_read_buffer_free(epfile);
11021183
ffs_data_closed(epfile->ffs);
11031184

11041185
return 0;
@@ -1724,24 +1805,20 @@ static void ffs_func_eps_disable(struct ffs_function *func)
17241805
unsigned count = func->ffs->eps_count;
17251806
unsigned long flags;
17261807

1808+
spin_lock_irqsave(&func->ffs->eps_lock, flags);
17271809
do {
1728-
if (epfile)
1729-
mutex_lock(&epfile->mutex);
1730-
spin_lock_irqsave(&func->ffs->eps_lock, flags);
17311810
/* pending requests get nuked */
17321811
if (likely(ep->ep))
17331812
usb_ep_disable(ep->ep);
17341813
++ep;
1735-
spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
17361814

17371815
if (epfile) {
17381816
epfile->ep = NULL;
1739-
kfree(epfile->read_buffer);
1740-
epfile->read_buffer = NULL;
1741-
mutex_unlock(&epfile->mutex);
1817+
__ffs_epfile_read_buffer_free(epfile);
17421818
++epfile;
17431819
}
17441820
} while (--count);
1821+
spin_unlock_irqrestore(&func->ffs->eps_lock, flags);
17451822
}
17461823

17471824
static int ffs_func_eps_enable(struct ffs_function *func)

0 commit comments

Comments
 (0)