Skip to content

Commit

Permalink
sound: usb-audio: make URB sizes more equal
Browse files Browse the repository at this point in the history
Distribute the packets evenly among the URBs, instead of making all URBs
except the last one to have the maximum size.  This makes the timing of
pointer updates more regular and removes some special cases from the
code.

Signed-off-by: Clemens Ladisch <clemens@ladisch.de>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
  • Loading branch information
Clemens Ladisch authored and Takashi Iwai committed Jan 26, 2009
1 parent 4d788e0 commit 160389c
Showing 1 changed file with 5 additions and 24 deletions.
29 changes: 5 additions & 24 deletions sound/usb/usbaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1035,9 +1035,9 @@ static void release_substream_urbs(struct snd_usb_substream *subs, int force)
static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int period_bytes,
unsigned int rate, unsigned int frame_bits)
{
unsigned int maxsize, n, i;
unsigned int maxsize, i;
int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
unsigned int npacks[MAX_URBS], urb_packs, total_packs, packs_per_ms;
unsigned int urb_packs, total_packs, packs_per_ms;

/* calculate the frequency in 16.16 format */
if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
Expand Down Expand Up @@ -1109,39 +1109,20 @@ static int init_substream_urbs(struct snd_usb_substream *subs, unsigned int peri
/* too much... */
subs->nurbs = MAX_URBS;
total_packs = MAX_URBS * urb_packs;
}
n = total_packs;
for (i = 0; i < subs->nurbs; i++) {
npacks[i] = n > urb_packs ? urb_packs : n;
n -= urb_packs;
}
if (subs->nurbs <= 1) {
} else if (subs->nurbs < 2) {
/* too little - we need at least two packets
* to ensure contiguous playback/capture
*/
subs->nurbs = 2;
npacks[0] = (total_packs + 1) / 2;
npacks[1] = total_packs - npacks[0];
} else if (npacks[subs->nurbs-1] < MIN_PACKS_URB * packs_per_ms) {
/* the last packet is too small.. */
if (subs->nurbs > 2) {
/* merge to the first one */
npacks[0] += npacks[subs->nurbs - 1];
subs->nurbs--;
} else {
/* divide to two */
subs->nurbs = 2;
npacks[0] = (total_packs + 1) / 2;
npacks[1] = total_packs - npacks[0];
}
}

/* allocate and initialize data urbs */
for (i = 0; i < subs->nurbs; i++) {
struct snd_urb_ctx *u = &subs->dataurb[i];
u->index = i;
u->subs = subs;
u->packets = npacks[i];
u->packets = (i + 1) * total_packs / subs->nurbs
- i * total_packs / subs->nurbs;
u->buffer_size = maxsize * u->packets;
if (subs->fmt_type == USB_FORMAT_TYPE_II)
u->packets++; /* for transfer delimiter */
Expand Down

0 comments on commit 160389c

Please sign in to comment.