Skip to content

Commit f2f69bf

Browse files
johnkeepinggregkh
authored andcommitted
usb: gadget: u_audio: fix calculations for small bInterval
If bInterval is 1, then p_interval is 8000 and p_interval_mil is 8E9, which is too big for a 32-bit value. While the storage is indeed 64-bit, this value is used as the divisor in do_div() which will truncate it into a uint32_t leading to incorrect calculated values. Switch back to keeping the base value in struct snd_uac_chip which fits easily into an int, meaning that the division can be done in two steps with the divisor fitting safely into a uint32_t on both steps. Fixes: 6fec018 ("usb: gadget: u_audio.c: Adding Playback Pitch ctl for sync playback") Tested-by: Pavel Hofman <[email protected]> Signed-off-by: John Keeping <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 92ef98a commit f2f69bf

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

drivers/usb/gadget/function/u_audio.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ struct snd_uac_chip {
7676
struct snd_pcm *pcm;
7777

7878
/* pre-calculated values for playback iso completion */
79-
unsigned long long p_interval_mil;
8079
unsigned long long p_residue_mil;
80+
unsigned int p_interval;
8181
unsigned int p_framesize;
8282
};
8383

@@ -194,21 +194,24 @@ static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req)
194194
* If there is a residue from this division, add it to the
195195
* residue accumulator.
196196
*/
197+
unsigned long long p_interval_mil = uac->p_interval * 1000000ULL;
198+
197199
pitched_rate_mil = (unsigned long long)
198200
params->p_srate * prm->pitch;
199201
div_result = pitched_rate_mil;
200-
do_div(div_result, uac->p_interval_mil);
202+
do_div(div_result, uac->p_interval);
203+
do_div(div_result, 1000000);
201204
frames = (unsigned int) div_result;
202205

203206
pr_debug("p_srate %d, pitch %d, interval_mil %llu, frames %d\n",
204-
params->p_srate, prm->pitch, uac->p_interval_mil, frames);
207+
params->p_srate, prm->pitch, p_interval_mil, frames);
205208

206209
p_pktsize = min_t(unsigned int,
207210
uac->p_framesize * frames,
208211
ep->maxpacket);
209212

210213
if (p_pktsize < ep->maxpacket) {
211-
residue_frames_mil = pitched_rate_mil - frames * uac->p_interval_mil;
214+
residue_frames_mil = pitched_rate_mil - frames * p_interval_mil;
212215
p_pktsize_residue_mil = uac->p_framesize * residue_frames_mil;
213216
} else
214217
p_pktsize_residue_mil = 0;
@@ -222,11 +225,11 @@ static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req)
222225
* size and decrease the accumulator.
223226
*/
224227
div_result = uac->p_residue_mil;
225-
do_div(div_result, uac->p_interval_mil);
228+
do_div(div_result, uac->p_interval);
229+
do_div(div_result, 1000000);
226230
if ((unsigned int) div_result >= uac->p_framesize) {
227231
req->length += uac->p_framesize;
228-
uac->p_residue_mil -= uac->p_framesize *
229-
uac->p_interval_mil;
232+
uac->p_residue_mil -= uac->p_framesize * p_interval_mil;
230233
pr_debug("increased req length to %d\n", req->length);
231234
}
232235
pr_debug("remains uac->p_residue_mil %llu\n", uac->p_residue_mil);
@@ -591,7 +594,7 @@ int u_audio_start_playback(struct g_audio *audio_dev)
591594
unsigned int factor;
592595
const struct usb_endpoint_descriptor *ep_desc;
593596
int req_len, i;
594-
unsigned int p_interval, p_pktsize;
597+
unsigned int p_pktsize;
595598

596599
ep = audio_dev->in_ep;
597600
prm = &uac->p_prm;
@@ -612,11 +615,10 @@ int u_audio_start_playback(struct g_audio *audio_dev)
612615
/* pre-compute some values for iso_complete() */
613616
uac->p_framesize = params->p_ssize *
614617
num_channels(params->p_chmask);
615-
p_interval = factor / (1 << (ep_desc->bInterval - 1));
616-
uac->p_interval_mil = (unsigned long long) p_interval * 1000000;
618+
uac->p_interval = factor / (1 << (ep_desc->bInterval - 1));
617619
p_pktsize = min_t(unsigned int,
618620
uac->p_framesize *
619-
(params->p_srate / p_interval),
621+
(params->p_srate / uac->p_interval),
620622
ep->maxpacket);
621623

622624
req_len = p_pktsize;

0 commit comments

Comments
 (0)