From 950f40fdd4732e5161e244812cbd9557c4e1d86c Mon Sep 17 00:00:00 2001 From: Wei Yongjun Date: Thu, 18 Oct 2012 22:46:23 +0800 Subject: ALSA: snd-usb: remove unused variable in init_pitch_v2() The variable ep is initialized but never used otherwise, so remove the unused variable. dpatch engine is used to auto generate this patch. (https://github.com/weiyj/dpatch) Signed-off-by: Wei Yongjun Signed-off-by: Takashi Iwai --- sound/usb/pcm.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'sound/usb/pcm.c') diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index 55e19e1b80ec..f77b87ad0256 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -171,11 +171,8 @@ static int init_pitch_v2(struct snd_usb_audio *chip, int iface, { struct usb_device *dev = chip->dev; unsigned char data[1]; - unsigned int ep; int err; - ep = get_endpoint(alts, 0)->bEndpointAddress; - data[0] = 1; if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR, USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT, -- cgit v1.2.3 From a9bb36261ef5c7e25564d5ce8a5129920a29bff9 Mon Sep 17 00:00:00 2001 From: Takashi Iwai Date: Tue, 20 Nov 2012 18:32:06 +0100 Subject: ALSA: usb-audio: simplify snd_usb_endpoint_start/stop arguments Reduce the redundant arguments for snd_usb_endpoint_start() and snd_usb_endpoint_stop(). Also replaced from int to bool. No functional changes by this commit. Signed-off-by: Takashi Iwai --- sound/usb/endpoint.c | 17 ++++++++--------- sound/usb/endpoint.h | 5 ++--- sound/usb/pcm.c | 25 +++++++++++-------------- 3 files changed, 21 insertions(+), 26 deletions(-) (limited to 'sound/usb/pcm.c') diff --git a/sound/usb/endpoint.c b/sound/usb/endpoint.c index d7382a5e29bb..4d50bbe2c115 100644 --- a/sound/usb/endpoint.c +++ b/sound/usb/endpoint.c @@ -515,7 +515,7 @@ void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep) /* * unlink active urbs. */ -static int deactivate_urbs(struct snd_usb_endpoint *ep, int force, int can_sleep) +static int deactivate_urbs(struct snd_usb_endpoint *ep, bool force, bool can_sleep) { unsigned int i; int async; @@ -561,7 +561,7 @@ static void release_urbs(struct snd_usb_endpoint *ep, int force) ep->prepare_data_urb = NULL; /* stop urbs */ - deactivate_urbs(ep, force, 1); + deactivate_urbs(ep, force, true); wait_clear_urbs(ep); for (i = 0; i < ep->nurbs; i++) @@ -824,7 +824,7 @@ int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep, * * Returns an error if the URB submission failed, 0 in all other cases. */ -int snd_usb_endpoint_start(struct snd_usb_endpoint *ep, int can_sleep) +int snd_usb_endpoint_start(struct snd_usb_endpoint *ep, bool can_sleep) { int err; unsigned int i; @@ -837,7 +837,7 @@ int snd_usb_endpoint_start(struct snd_usb_endpoint *ep, int can_sleep) return 0; /* just to be sure */ - deactivate_urbs(ep, 0, can_sleep); + deactivate_urbs(ep, false, can_sleep); if (can_sleep) wait_clear_urbs(ep); @@ -891,7 +891,7 @@ int snd_usb_endpoint_start(struct snd_usb_endpoint *ep, int can_sleep) __error: clear_bit(EP_FLAG_RUNNING, &ep->flags); ep->use_count--; - deactivate_urbs(ep, 0, 0); + deactivate_urbs(ep, false, false); return -EPIPE; } @@ -906,8 +906,7 @@ __error: * * Must be balanced to calls of snd_usb_endpoint_start(). */ -void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, - int force, int can_sleep, int wait) +void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, bool wait) { if (!ep) return; @@ -916,7 +915,7 @@ void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, return; if (--ep->use_count == 0) { - deactivate_urbs(ep, force, can_sleep); + deactivate_urbs(ep, false, wait); ep->data_subs = NULL; ep->sync_slave = NULL; ep->retire_data_urb = NULL; @@ -947,7 +946,7 @@ int snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep) if (!ep) return -EINVAL; - deactivate_urbs(ep, 1, 1); + deactivate_urbs(ep, true, true); wait_clear_urbs(ep); if (ep->use_count != 0) diff --git a/sound/usb/endpoint.h b/sound/usb/endpoint.h index 3d4c9705041f..f1e451da9a67 100644 --- a/sound/usb/endpoint.h +++ b/sound/usb/endpoint.h @@ -16,9 +16,8 @@ int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep, struct audioformat *fmt, struct snd_usb_endpoint *sync_ep); -int snd_usb_endpoint_start(struct snd_usb_endpoint *ep, int can_sleep); -void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, - int force, int can_sleep, int wait); +int snd_usb_endpoint_start(struct snd_usb_endpoint *ep, bool can_sleep); +void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, bool wait); void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep); int snd_usb_endpoint_activate(struct snd_usb_endpoint *ep); int snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep); diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index e7329d0449f2..d90604aa5137 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -211,7 +211,7 @@ int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface, } } -static int start_endpoints(struct snd_usb_substream *subs, int can_sleep) +static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep) { int err; @@ -263,16 +263,13 @@ static int start_endpoints(struct snd_usb_substream *subs, int can_sleep) return 0; } -static void stop_endpoints(struct snd_usb_substream *subs, - int force, int can_sleep, int wait) +static void stop_endpoints(struct snd_usb_substream *subs, bool wait) { if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) - snd_usb_endpoint_stop(subs->sync_endpoint, - force, can_sleep, wait); + snd_usb_endpoint_stop(subs->sync_endpoint, wait); if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) - snd_usb_endpoint_stop(subs->data_endpoint, - force, can_sleep, wait); + snd_usb_endpoint_stop(subs->data_endpoint, wait); } static int deactivate_endpoints(struct snd_usb_substream *subs) @@ -444,7 +441,7 @@ static int configure_endpoint(struct snd_usb_substream *subs) int ret; /* format changed */ - stop_endpoints(subs, 0, 0, 0); + stop_endpoints(subs, false); ret = snd_usb_endpoint_set_params(subs->data_endpoint, subs->pcm_format, subs->channels, @@ -530,7 +527,7 @@ static int snd_usb_hw_free(struct snd_pcm_substream *substream) subs->period_bytes = 0; down_read(&subs->stream->chip->shutdown_rwsem); if (!subs->stream->chip->shutdown) { - stop_endpoints(subs, 0, 1, 1); + stop_endpoints(subs, true); deactivate_endpoints(subs); } up_read(&subs->stream->chip->shutdown_rwsem); @@ -605,7 +602,7 @@ static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream) /* for playback, submit the URBs now; otherwise, the first hwptr_done * updates for all URBs would happen at the same time when starting */ if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) - ret = start_endpoints(subs, 1); + ret = start_endpoints(subs, true); unlock: up_read(&subs->stream->chip->shutdown_rwsem); @@ -1010,7 +1007,7 @@ static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction) struct snd_usb_stream *as = snd_pcm_substream_chip(substream); struct snd_usb_substream *subs = &as->substream[direction]; - stop_endpoints(subs, 0, 0, 0); + stop_endpoints(subs, false); if (!as->chip->shutdown && subs->interface >= 0) { usb_set_interface(subs->dev, subs->interface, 0); @@ -1245,7 +1242,7 @@ static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substrea subs->running = 1; return 0; case SNDRV_PCM_TRIGGER_STOP: - stop_endpoints(subs, 0, 0, 0); + stop_endpoints(subs, false); subs->running = 0; return 0; case SNDRV_PCM_TRIGGER_PAUSE_PUSH: @@ -1266,7 +1263,7 @@ static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream switch (cmd) { case SNDRV_PCM_TRIGGER_START: - err = start_endpoints(subs, 0); + err = start_endpoints(subs, false); if (err < 0) return err; @@ -1274,7 +1271,7 @@ static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream subs->running = 1; return 0; case SNDRV_PCM_TRIGGER_STOP: - stop_endpoints(subs, 0, 0, 0); + stop_endpoints(subs, false); subs->running = 0; return 0; case SNDRV_PCM_TRIGGER_PAUSE_PUSH: -- cgit v1.2.3 From b2eb950de2f09435d5156f4dc6d5dbf284cd97f3 Mon Sep 17 00:00:00 2001 From: Takashi Iwai Date: Wed, 21 Nov 2012 08:30:48 +0100 Subject: ALSA: usb-audio: stop both data and sync endpoints asynchronously As we are stopping the endpoints asynchronously now, it's better to trigger the stop of both data and sync endpoints and wait for pending stopping operations, instead of the sequential trigger-and-wait procedure. So the wait argument in snd_usb_endpoint_stop() is dropped, and it's expected that the caller synchronizes explicitly by calling snd_usb_endpoint_sync_pending_stop(). (Actually there is only one place calling this, so it was safe to change.) Signed-off-by: Takashi Iwai --- sound/usb/endpoint.c | 11 +++++------ sound/usb/endpoint.h | 2 +- sound/usb/pcm.c | 9 +++++++-- 3 files changed, 13 insertions(+), 9 deletions(-) (limited to 'sound/usb/pcm.c') diff --git a/sound/usb/endpoint.c b/sound/usb/endpoint.c index 6db2143350d3..f487d26f8d40 100644 --- a/sound/usb/endpoint.c +++ b/sound/usb/endpoint.c @@ -896,8 +896,11 @@ __error: * actually be deactivated. * * Must be balanced to calls of snd_usb_endpoint_start(). + * + * The caller needs to synchronize the pending stop operation via + * snd_usb_endpoint_sync_pending_stop(). */ -void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, bool wait) +void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep) { if (!ep) return; @@ -911,11 +914,7 @@ void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, bool wait) ep->sync_slave = NULL; ep->retire_data_urb = NULL; ep->prepare_data_urb = NULL; - - if (wait) - wait_clear_urbs(ep); - else - set_bit(EP_FLAG_STOPPING, &ep->flags); + set_bit(EP_FLAG_STOPPING, &ep->flags); } } diff --git a/sound/usb/endpoint.h b/sound/usb/endpoint.h index f1e451da9a67..447902dd8a4a 100644 --- a/sound/usb/endpoint.h +++ b/sound/usb/endpoint.h @@ -17,7 +17,7 @@ int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep, struct snd_usb_endpoint *sync_ep); int snd_usb_endpoint_start(struct snd_usb_endpoint *ep, bool can_sleep); -void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, bool wait); +void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep); void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep); int snd_usb_endpoint_activate(struct snd_usb_endpoint *ep); int snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep); diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index d90604aa5137..4750d3d5c0cc 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -266,10 +266,15 @@ static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep) static void stop_endpoints(struct snd_usb_substream *subs, bool wait) { if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) - snd_usb_endpoint_stop(subs->sync_endpoint, wait); + snd_usb_endpoint_stop(subs->sync_endpoint); if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) - snd_usb_endpoint_stop(subs->data_endpoint, wait); + snd_usb_endpoint_stop(subs->data_endpoint); + + if (wait) { + snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint); + snd_usb_endpoint_sync_pending_stop(subs->data_endpoint); + } } static int deactivate_endpoints(struct snd_usb_substream *subs) -- cgit v1.2.3 From b0db6063dba4ee02dfda7411ec3aaf8f0fbda0f7 Mon Sep 17 00:00:00 2001 From: Takashi Iwai Date: Wed, 21 Nov 2012 08:35:42 +0100 Subject: ALSA: usb-audio: process pending stop at PCM hw_free and close PCM hw_free and close should wait until all the pending stop operations have been finished. Basically only PCM trigger callback should use non-wait calls. Signed-off-by: Takashi Iwai --- sound/usb/pcm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sound/usb/pcm.c') diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index 4750d3d5c0cc..bc3c9acc68b7 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -446,7 +446,7 @@ static int configure_endpoint(struct snd_usb_substream *subs) int ret; /* format changed */ - stop_endpoints(subs, false); + stop_endpoints(subs, true); ret = snd_usb_endpoint_set_params(subs->data_endpoint, subs->pcm_format, subs->channels, @@ -1012,7 +1012,7 @@ static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction) struct snd_usb_stream *as = snd_pcm_substream_chip(substream); struct snd_usb_substream *subs = &as->substream[direction]; - stop_endpoints(subs, false); + stop_endpoints(subs, true); if (!as->chip->shutdown && subs->interface >= 0) { usb_set_interface(subs->dev, subs->interface, 0); -- cgit v1.2.3 From 3f94fad09538ec988919ec3f371841182df71d04 Mon Sep 17 00:00:00 2001 From: Takashi Iwai Date: Fri, 23 Nov 2012 14:28:42 +0100 Subject: ALSA: usb-audio: ignore delay calculation for capture stream It doesn't make sense to calculate the delay for capture streams in the current implementation. It's always zero, so we should skip the computation in snd_usb_pcm_pointer() in the case of capture. Signed-off-by: Takashi Iwai --- sound/usb/pcm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'sound/usb/pcm.c') diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index e919c2e40fa0..8e1d5e00c182 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -75,7 +75,8 @@ static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream return SNDRV_PCM_POS_XRUN; spin_lock(&subs->lock); hwptr_done = subs->hwptr_done; - substream->runtime->delay = snd_usb_pcm_delay(subs, + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) + substream->runtime->delay = snd_usb_pcm_delay(subs, substream->runtime->rate); spin_unlock(&subs->lock); return hwptr_done / (substream->runtime->frame_bits >> 3); -- cgit v1.2.3 From 48779a0b8ffc45f7f2b519ef462a72b1c5208d09 Mon Sep 17 00:00:00 2001 From: Takashi Iwai Date: Fri, 23 Nov 2012 16:00:37 +0100 Subject: ALSA: usb-audio: fix delay account during pause When a playback stream is paused, the stream isn't actually stopped, thus we still need to take care of the in-flight data amount for the delay calculation. Otherwise the value of subs->last_delay is no longer reliable and can give a bogus value after resuming from pause. This will result in "delay: estimated XX, actual YY" error messages. Also, during pause after all in flight data are processed (i.e. last_delay = 0), we don't have to calculate the actual delay from the current frame. Give a short path in such a case. Signed-off-by: Takashi Iwai --- sound/usb/pcm.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'sound/usb/pcm.c') diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index 8e1d5e00c182..7c64b9560b18 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -46,6 +46,9 @@ snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs, int frame_diff; int est_delay; + if (!subs->last_delay) + return 0; /* short path */ + current_frame_number = usb_get_current_frame_number(subs->dev); /* * HCD implementations use different widths, use lower 8 bits. @@ -1195,6 +1198,9 @@ static void retire_playback_urb(struct snd_usb_substream *subs, return; spin_lock_irqsave(&subs->lock, flags); + if (!subs->last_delay) + goto out; /* short path */ + est_delay = snd_usb_pcm_delay(subs, runtime->rate); /* update delay with exact number of samples played */ if (processed > subs->last_delay) @@ -1212,6 +1218,15 @@ static void retire_playback_urb(struct snd_usb_substream *subs, snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n", est_delay, subs->last_delay); + if (!subs->running) { + /* update last_frame_number for delay counting here since + * prepare_playback_urb won't be called during pause + */ + subs->last_frame_number = + usb_get_current_frame_number(subs->dev) & 0xff; + } + + out: spin_unlock_irqrestore(&subs->lock, flags); } @@ -1253,7 +1268,8 @@ static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substrea return 0; case SNDRV_PCM_TRIGGER_PAUSE_PUSH: subs->data_endpoint->prepare_data_urb = NULL; - subs->data_endpoint->retire_data_urb = NULL; + /* keep retire_data_urb for delay calculation */ + subs->data_endpoint->retire_data_urb = retire_playback_urb; subs->running = 0; return 0; } -- cgit v1.2.3 From fde854bdaf603a99a80b9545c0aaca9ccd02dd31 Mon Sep 17 00:00:00 2001 From: Eldad Zack Date: Wed, 28 Nov 2012 23:55:32 +0100 Subject: ALSA: usb-audio: replace hardcoded value with const In this context, 0x01 is USB_ENDPOINT_XFER_ISOC. Signed-off-by: Eldad Zack Signed-off-by: Takashi Iwai --- sound/usb/pcm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sound/usb/pcm.c') diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index 7c64b9560b18..f488a493a98e 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -384,7 +384,7 @@ static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt) /* ... and check descriptor size before accessing bSynchAddress because there is a version of the SB Audigy 2 NX firmware lacking the audio fields in the endpoint descriptors */ - if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 || + if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC || (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && get_endpoint(alts, 1)->bSynchAddress != 0 && !implicit_fb)) { -- cgit v1.2.3 From ca10a7ebdff1c862ca1ef1d7bd2c6810e3c87e17 Mon Sep 17 00:00:00 2001 From: Eldad Zack Date: Wed, 28 Nov 2012 23:55:41 +0100 Subject: ALSA: usb-audio: FT C400 sync playback EP to capture EP The playback endpoint uses implicit feedback mode, similar to the M-Audio FTU. Like with the FTU, we need to associate the sync pipe ourselves. Signed-off-by: Eldad Zack Signed-off-by: Takashi Iwai --- sound/usb/pcm.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'sound/usb/pcm.c') diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index f488a493a98e..769821c30031 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -362,6 +362,19 @@ static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt) attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE; switch (subs->stream->chip->usb_id) { + case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */ + if (is_playback) { + implicit_fb = 1; + ep = 0x81; + iface = usb_ifnum_to_if(dev, 3); + + if (!iface || iface->num_altsetting == 0) + return -EINVAL; + + alts = &iface->altsetting[1]; + goto add_sync_ep; + } + break; case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */ case USB_ID(0x0763, 0x2081): if (is_playback) { -- cgit v1.2.3 From 0d9741c0e058e2857fe3fed37975515dc8dcd21d Mon Sep 17 00:00:00 2001 From: Eldad Zack Date: Mon, 3 Dec 2012 20:30:09 +0100 Subject: ALSA: usb-audio: sync ep init fix for audioformat mismatch Commit 947d299686aa9cc8aecf749d54e8475c6e498956 , "ALSA: snd-usb: properly initialize the sync endpoint", while correcting the initialization of the sync endpoint when opening just the data endpoint, prevents devices that has a sync endpoint, with a channel number different than that of the data endpoint, from functioning. Due to a different channel and period bytes count, attempting to initialize the sync endpoint will fail at the usb host driver. For example, when using xhci: cannot submit urb 0, error -90: internal error With this patch, if a sync endpoint has multiple audioformats, a matching audioformat is preferred. An audioformat must be found with at least one channel and support the requested sample rate and PCM format, otherwise the stream will not be opened. If the number of channels differ between the selected audioformat and the requested format, adjust the period bytes count accordingly. It is safe to perform the calculation on the basis of the channel count, since the requested PCM audio format and the rate must be supported by the selected audioformat. Cc: Jeffrey Barish Cc: Daniel Mack Signed-off-by: Eldad Zack Signed-off-by: Takashi Iwai --- sound/usb/pcm.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 99 insertions(+), 7 deletions(-) (limited to 'sound/usb/pcm.c') diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index 769821c30031..c6593101c049 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -453,6 +453,103 @@ add_sync_ep: return 0; } +/* + * Return the score of matching two audioformats. + * Veto the audioformat if: + * - It has no channels for some reason. + * - Requested PCM format is not supported. + * - Requested sample rate is not supported. + */ +static int match_endpoint_audioformats(struct audioformat *fp, + struct audioformat *match, int rate, + snd_pcm_format_t pcm_format) +{ + int i; + int score = 0; + + if (fp->channels < 1) { + snd_printdd("%s: (fmt @%p) no channels\n", __func__, fp); + return 0; + } + + if (!(fp->formats & (1ULL << pcm_format))) { + snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__, + fp, pcm_format); + return 0; + } + + for (i = 0; i < fp->nr_rates; i++) { + if (fp->rate_table[i] == rate) { + score++; + break; + } + } + if (!score) { + snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__, + fp, rate); + return 0; + } + + if (fp->channels == match->channels) + score++; + + snd_printdd("%s: (fmt @%p) score %d\n", __func__, fp, score); + + return score; +} + +/* + * Configure the sync ep using the rate and pcm format of the data ep. + */ +static int configure_sync_endpoint(struct snd_usb_substream *subs) +{ + int ret; + struct audioformat *fp; + struct audioformat *sync_fp = NULL; + int cur_score = 0; + int sync_period_bytes = subs->period_bytes; + struct snd_usb_substream *sync_subs = + &subs->stream->substream[subs->direction ^ 1]; + + /* Try to find the best matching audioformat. */ + list_for_each_entry(fp, &sync_subs->fmt_list, list) { + int score = match_endpoint_audioformats(fp, subs->cur_audiofmt, + subs->cur_rate, subs->pcm_format); + + if (score > cur_score) { + sync_fp = fp; + cur_score = score; + } + } + + if (unlikely(sync_fp == NULL)) { + snd_printk(KERN_ERR "%s: no valid audioformat for sync ep %x found\n", + __func__, sync_subs->ep_num); + return -EINVAL; + } + + /* + * Recalculate the period bytes if channel number differ between + * data and sync ep audioformat. + */ + if (sync_fp->channels != subs->channels) { + sync_period_bytes = (subs->period_bytes / subs->channels) * + sync_fp->channels; + snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n", + __func__, subs->period_bytes, sync_period_bytes); + } + + ret = snd_usb_endpoint_set_params(subs->sync_endpoint, + subs->pcm_format, + sync_fp->channels, + sync_period_bytes, + subs->cur_rate, + sync_fp, + NULL); + + return ret; +} + /* * configure endpoint params * @@ -475,13 +572,8 @@ static int configure_endpoint(struct snd_usb_substream *subs) return ret; if (subs->sync_endpoint) - ret = snd_usb_endpoint_set_params(subs->sync_endpoint, - subs->pcm_format, - subs->channels, - subs->period_bytes, - subs->cur_rate, - subs->cur_audiofmt, - NULL); + ret = configure_sync_endpoint(subs); + return ret; } -- cgit v1.2.3