Skip to content

Commit ff878b4

Browse files
committed
ALSA: usb-audio: Split endpoint setups for hw_params and prepare
One of the former changes for the endpoint management was the more consistent setup of endpoints at hw_params. snd_usb_endpoint_configure() is a single function that does the full setup, and it's called from both PCM hw_params and prepare callbacks. Although the EP setup at the prepare phase is usually skipped (by checking need_setup flag), it may be still effective in some cases like suspend/resume that requires the interface setup again. As it's a full and single setup, the invocation of snd_usb_endpoint_configure() includes not only the USB interface setup but also the buffer release and allocation. OTOH, doing the buffer release and re-allocation at PCM prepare phase is rather superfluous, and better to be done only in the hw_params phase. For those optimizations, this patch splits the endpoint setup to two phases: snd_usb_endpoint_set_params() and snd_usb_endpoint_prepare(), to be called from hw_params and from prepare, respectively. Note that this patch changes the driver operation slightly, effectively moving the USB interface setup again to PCM prepare stage instead of hw_params stage, while the buffer allocation and such initializations are still done at hw_params stage. And, the change of the USB interface setup timing (moving to prepare) gave an interesting "fix", too: it was reported that the recent kernels caused silent output at the beginning on playbacks on some devices on Android, and this change casually fixed the regression. It seems that those devices are picky about the sample rate change (or the interface change?), and don't follow the too immediate rate changes. Meanwhile, Android operates the PCM in the following order: - open, then hw_params with the possibly highest sample rate - close without prepare - re-open, hw_params with the normal sample rate - prepare, and start streaming This procedure ended up the hw_params twice with different rates, and because the recent kernel did set up the sample rate twice one and after, it screwed up the device. OTOH, the earlier kernels didn't set up the USB interface at hw_params, hence this problem didn't appear. Now, with this patch, the USB interface setup is again back to the prepare phase, and it works around the problem automagically. Although we should address the sample rate problem in a more solid way in future, let's keep things working as before for now. Fixes: bf6313a ("ALSA: usb-audio: Refactor endpoint management") Cc: <[email protected]> Reported-by: chihhao chen <[email protected]> Link: https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Takashi Iwai <[email protected]>
1 parent 2027f11 commit ff878b4

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

sound/usb/endpoint.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,8 @@ bool snd_usb_endpoint_compatible(struct snd_usb_audio *chip,
758758
* The endpoint needs to be closed via snd_usb_endpoint_close() later.
759759
*
760760
* Note that this function doesn't configure the endpoint. The substream
761-
* needs to set it up later via snd_usb_endpoint_configure().
761+
* needs to set it up later via snd_usb_endpoint_set_params() and
762+
* snd_usb_endpoint_prepare().
762763
*/
763764
struct snd_usb_endpoint *
764765
snd_usb_endpoint_open(struct snd_usb_audio *chip,
@@ -1290,12 +1291,13 @@ static int sync_ep_set_params(struct snd_usb_endpoint *ep)
12901291
/*
12911292
* snd_usb_endpoint_set_params: configure an snd_usb_endpoint
12921293
*
1294+
* It's called either from hw_params callback.
12931295
* Determine the number of URBs to be used on this endpoint.
12941296
* An endpoint must be configured before it can be started.
12951297
* An endpoint that is already running can not be reconfigured.
12961298
*/
1297-
static int snd_usb_endpoint_set_params(struct snd_usb_audio *chip,
1298-
struct snd_usb_endpoint *ep)
1299+
int snd_usb_endpoint_set_params(struct snd_usb_audio *chip,
1300+
struct snd_usb_endpoint *ep)
12991301
{
13001302
const struct audioformat *fmt = ep->cur_audiofmt;
13011303
int err;
@@ -1378,18 +1380,18 @@ static int init_sample_rate(struct snd_usb_audio *chip,
13781380
}
13791381

13801382
/*
1381-
* snd_usb_endpoint_configure: Configure the endpoint
1383+
* snd_usb_endpoint_prepare: Prepare the endpoint
13821384
*
13831385
* This function sets up the EP to be fully usable state.
1384-
* It's called either from hw_params or prepare callback.
1386+
* It's called either from prepare callback.
13851387
* The function checks need_setup flag, and performs nothing unless needed,
13861388
* so it's safe to call this multiple times.
13871389
*
13881390
* This returns zero if unchanged, 1 if the configuration has changed,
13891391
* or a negative error code.
13901392
*/
1391-
int snd_usb_endpoint_configure(struct snd_usb_audio *chip,
1392-
struct snd_usb_endpoint *ep)
1393+
int snd_usb_endpoint_prepare(struct snd_usb_audio *chip,
1394+
struct snd_usb_endpoint *ep)
13931395
{
13941396
bool iface_first;
13951397
int err = 0;
@@ -1410,9 +1412,6 @@ int snd_usb_endpoint_configure(struct snd_usb_audio *chip,
14101412
if (err < 0)
14111413
goto unlock;
14121414
}
1413-
err = snd_usb_endpoint_set_params(chip, ep);
1414-
if (err < 0)
1415-
goto unlock;
14161415
goto done;
14171416
}
14181417

@@ -1440,10 +1439,6 @@ int snd_usb_endpoint_configure(struct snd_usb_audio *chip,
14401439
if (err < 0)
14411440
goto unlock;
14421441

1443-
err = snd_usb_endpoint_set_params(chip, ep);
1444-
if (err < 0)
1445-
goto unlock;
1446-
14471442
err = snd_usb_select_mode_quirk(chip, ep->cur_audiofmt);
14481443
if (err < 0)
14491444
goto unlock;

sound/usb/endpoint.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ snd_usb_endpoint_open(struct snd_usb_audio *chip,
1717
bool is_sync_ep);
1818
void snd_usb_endpoint_close(struct snd_usb_audio *chip,
1919
struct snd_usb_endpoint *ep);
20-
int snd_usb_endpoint_configure(struct snd_usb_audio *chip,
21-
struct snd_usb_endpoint *ep);
20+
int snd_usb_endpoint_set_params(struct snd_usb_audio *chip,
21+
struct snd_usb_endpoint *ep);
22+
int snd_usb_endpoint_prepare(struct snd_usb_audio *chip,
23+
struct snd_usb_endpoint *ep);
2224
int snd_usb_endpoint_get_clock_rate(struct snd_usb_audio *chip, int clock);
2325

2426
bool snd_usb_endpoint_compatible(struct snd_usb_audio *chip,

sound/usb/pcm.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,17 +443,17 @@ static int configure_endpoints(struct snd_usb_audio *chip,
443443
if (stop_endpoints(subs, false))
444444
sync_pending_stops(subs);
445445
if (subs->sync_endpoint) {
446-
err = snd_usb_endpoint_configure(chip, subs->sync_endpoint);
446+
err = snd_usb_endpoint_prepare(chip, subs->sync_endpoint);
447447
if (err < 0)
448448
return err;
449449
}
450-
err = snd_usb_endpoint_configure(chip, subs->data_endpoint);
450+
err = snd_usb_endpoint_prepare(chip, subs->data_endpoint);
451451
if (err < 0)
452452
return err;
453453
snd_usb_set_format_quirk(subs, subs->cur_audiofmt);
454454
} else {
455455
if (subs->sync_endpoint) {
456-
err = snd_usb_endpoint_configure(chip, subs->sync_endpoint);
456+
err = snd_usb_endpoint_prepare(chip, subs->sync_endpoint);
457457
if (err < 0)
458458
return err;
459459
}
@@ -551,7 +551,13 @@ static int snd_usb_hw_params(struct snd_pcm_substream *substream,
551551
subs->cur_audiofmt = fmt;
552552
mutex_unlock(&chip->mutex);
553553

554-
ret = configure_endpoints(chip, subs);
554+
if (subs->sync_endpoint) {
555+
ret = snd_usb_endpoint_set_params(chip, subs->sync_endpoint);
556+
if (ret < 0)
557+
goto unlock;
558+
}
559+
560+
ret = snd_usb_endpoint_set_params(chip, subs->data_endpoint);
555561

556562
unlock:
557563
if (ret < 0)

0 commit comments

Comments
 (0)