[PATCH 3.16 264/366] usb: gadget: u_audio: update hw_ptr in iso_complete after data copied

From: Ben Hutchings
Date: Sun Nov 11 2018 - 15:00:38 EST


3.16.61-rc1 review patch. If anyone has any objections, please let me know.

------------------

From: Joshua Frkuska <joshua_frkuska@xxxxxxxxxx>

commit 6b37bd78d30c890e575a1bda22978d1d2a233362 upstream.

In u_audio_iso_complete, the runtime hw_ptr is updated before the
data is actually copied over to/from the buffer/dma area. When
ALSA uses this hw_ptr, the data may not actually be available to
be used. This causes trash/stale audio to play/record. This
patch updates the hw_ptr after the data has been copied to avoid
this.

Fixes: 132fcb460839 ("usb: gadget: Add Audio Class 2.0 Driver")
Signed-off-by: Joshua Frkuska <joshua_frkuska@xxxxxxxxxx>
Signed-off-by: Eugeniu Rosca <erosca@xxxxxxxxxxxxxx>
Signed-off-by: Felipe Balbi <felipe.balbi@xxxxxxxxxxxxxxx>
[bwh: Backported to 3.16:
- Don't use a local hw_ptr variable
- Adjust filename]
Signed-off-by: Ben Hutchings <ben@xxxxxxxxxxxxxxx>
---
--- a/drivers/usb/gadget/f_uac2.c
+++ b/drivers/usb/gadget/f_uac2.c
@@ -229,12 +229,16 @@ agdev_iso_complete(struct usb_ep *ep, st
if (pending >= prm->period_size)
update_alsa = true;

- prm->hw_ptr = (prm->hw_ptr + req->actual) % prm->dma_bytes;
-
spin_unlock_irqrestore(&prm->lock, flags);

/* Pack USB load in ALSA ring buffer */
memcpy(dst, src, req->actual);
+
+ spin_lock_irqsave(&prm->lock, flags);
+ /* update hw_ptr after data is copied to memory */
+ prm->hw_ptr = (prm->hw_ptr + req->actual) % prm->dma_bytes;
+ spin_unlock_irqrestore(&prm->lock, flags);
+
exit:
if (usb_ep_queue(ep, req, GFP_ATOMIC))
dev_err(&uac2->pdev.dev, "%d Error!\n", __LINE__);