summaryrefslogtreecommitdiff
path: root/sound/usb/endpoint.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/usb/endpoint.c')
-rw-r--r--sound/usb/endpoint.c943
1 files changed, 533 insertions, 410 deletions
diff --git a/sound/usb/endpoint.c b/sound/usb/endpoint.c
index e2f9ce2f5b8b..162da7a50046 100644
--- a/sound/usb/endpoint.c
+++ b/sound/usb/endpoint.c
@@ -18,6 +18,7 @@
#include "card.h"
#include "endpoint.h"
#include "pcm.h"
+#include "clock.h"
#include "quirks.h"
#define EP_FLAG_RUNNING 1
@@ -116,20 +117,17 @@ static const char *usb_error_string(int err)
*/
int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep)
{
- return ep->sync_master &&
- ep->sync_master->type == SND_USB_ENDPOINT_TYPE_DATA &&
- ep->type == SND_USB_ENDPOINT_TYPE_DATA &&
- usb_pipeout(ep->pipe);
+ return ep->implicit_fb_sync && usb_pipeout(ep->pipe);
}
/*
- * For streaming based on information derived from sync endpoints,
- * prepare_outbound_urb_sizes() will call slave_next_packet_size() to
- * determine the number of samples to be sent in the next packet.
+ * Return the number of samples to be sent in the next packet
+ * for streaming based on information derived from sync endpoints
*
- * For implicit feedback, slave_next_packet_size() is unused.
+ * This won't be used for implicit feedback which takes the packet size
+ * returned from the sync source
*/
-int snd_usb_endpoint_slave_next_packet_size(struct snd_usb_endpoint *ep)
+static int slave_next_packet_size(struct snd_usb_endpoint *ep)
{
unsigned long flags;
int ret;
@@ -147,11 +145,10 @@ int snd_usb_endpoint_slave_next_packet_size(struct snd_usb_endpoint *ep)
}
/*
- * For adaptive and synchronous endpoints, prepare_outbound_urb_sizes()
- * will call next_packet_size() to determine the number of samples to be
- * sent in the next packet.
+ * Return the number of samples to be sent in the next packet
+ * for adaptive and synchronous endpoints
*/
-int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep)
+static int next_packet_size(struct snd_usb_endpoint *ep)
{
int ret;
@@ -169,28 +166,57 @@ int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep)
return ret;
}
+/*
+ * snd_usb_endpoint_next_packet_size: Return the number of samples to be sent
+ * in the next packet
+ */
+int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep,
+ struct snd_urb_ctx *ctx, int idx)
+{
+ if (ctx->packet_size[idx])
+ return ctx->packet_size[idx];
+ else if (ep->sync_source)
+ return slave_next_packet_size(ep);
+ else
+ return next_packet_size(ep);
+}
+
+static void call_retire_callback(struct snd_usb_endpoint *ep,
+ struct urb *urb)
+{
+ struct snd_usb_substream *data_subs;
+
+ data_subs = READ_ONCE(ep->data_subs);
+ if (data_subs && ep->retire_data_urb)
+ ep->retire_data_urb(data_subs, urb);
+}
+
static void retire_outbound_urb(struct snd_usb_endpoint *ep,
struct snd_urb_ctx *urb_ctx)
{
- if (ep->retire_data_urb)
- ep->retire_data_urb(ep->data_subs, urb_ctx->urb);
+ call_retire_callback(ep, urb_ctx->urb);
}
+static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
+ struct snd_usb_endpoint *sender,
+ const struct urb *urb);
+
static void retire_inbound_urb(struct snd_usb_endpoint *ep,
struct snd_urb_ctx *urb_ctx)
{
struct urb *urb = urb_ctx->urb;
+ struct snd_usb_endpoint *sync_sink;
if (unlikely(ep->skip_packets > 0)) {
ep->skip_packets--;
return;
}
- if (ep->sync_slave)
- snd_usb_handle_sync_urb(ep->sync_slave, ep, urb);
+ sync_sink = READ_ONCE(ep->sync_sink);
+ if (sync_sink)
+ snd_usb_handle_sync_urb(sync_sink, ep, urb);
- if (ep->retire_data_urb)
- ep->retire_data_urb(ep->data_subs, urb);
+ call_retire_callback(ep, urb);
}
static void prepare_silent_urb(struct snd_usb_endpoint *ep,
@@ -211,13 +237,7 @@ static void prepare_silent_urb(struct snd_usb_endpoint *ep,
unsigned int length;
int counts;
- if (ctx->packet_size[i])
- counts = ctx->packet_size[i];
- else if (ep->sync_master)
- counts = snd_usb_endpoint_slave_next_packet_size(ep);
- else
- counts = snd_usb_endpoint_next_packet_size(ep);
-
+ counts = snd_usb_endpoint_next_packet_size(ep, ctx, i);
length = counts * ep->stride; /* number of silent bytes */
offset = offs * ep->stride + extra * i;
urb->iso_frame_desc[i].offset = offset;
@@ -244,17 +264,17 @@ static void prepare_outbound_urb(struct snd_usb_endpoint *ep,
{
struct urb *urb = ctx->urb;
unsigned char *cp = urb->transfer_buffer;
+ struct snd_usb_substream *data_subs;
urb->dev = ep->chip->dev; /* we need to set this at each time */
switch (ep->type) {
case SND_USB_ENDPOINT_TYPE_DATA:
- if (ep->prepare_data_urb) {
- ep->prepare_data_urb(ep->data_subs, urb);
- } else {
- /* no data provider, so send silence */
+ data_subs = READ_ONCE(ep->data_subs);
+ if (data_subs && ep->prepare_data_urb)
+ ep->prepare_data_urb(data_subs, urb);
+ else /* no data provider, so send silence */
prepare_silent_urb(ep, ctx);
- }
break;
case SND_USB_ENDPOINT_TYPE_SYNC:
@@ -316,6 +336,39 @@ static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep,
}
}
+/* notify an error as XRUN to the assigned PCM data substream */
+static void notify_xrun(struct snd_usb_endpoint *ep)
+{
+ struct snd_usb_substream *data_subs;
+
+ data_subs = READ_ONCE(ep->data_subs);
+ if (data_subs && data_subs->pcm_substream)
+ snd_pcm_stop_xrun(data_subs->pcm_substream);
+}
+
+static struct snd_usb_packet_info *
+next_packet_fifo_enqueue(struct snd_usb_endpoint *ep)
+{
+ struct snd_usb_packet_info *p;
+
+ p = ep->next_packet + (ep->next_packet_head + ep->next_packet_queued) %
+ ARRAY_SIZE(ep->next_packet);
+ ep->next_packet_queued++;
+ return p;
+}
+
+static struct snd_usb_packet_info *
+next_packet_fifo_dequeue(struct snd_usb_endpoint *ep)
+{
+ struct snd_usb_packet_info *p;
+
+ p = ep->next_packet + ep->next_packet_head;
+ ep->next_packet_head++;
+ ep->next_packet_head %= ARRAY_SIZE(ep->next_packet);
+ ep->next_packet_queued--;
+ return p;
+}
+
/*
* Send output urbs that have been prepared previously. URBs are dequeued
* from ep->ready_playback_urbs and in case there aren't any available
@@ -340,17 +393,14 @@ static void queue_pending_output_urbs(struct snd_usb_endpoint *ep)
int err, i;
spin_lock_irqsave(&ep->lock, flags);
- if (ep->next_packet_read_pos != ep->next_packet_write_pos) {
- packet = ep->next_packet + ep->next_packet_read_pos;
- ep->next_packet_read_pos++;
- ep->next_packet_read_pos %= MAX_URBS;
-
+ if (ep->next_packet_queued > 0 &&
+ !list_empty(&ep->ready_playback_urbs)) {
/* take URB out of FIFO */
- if (!list_empty(&ep->ready_playback_urbs)) {
- ctx = list_first_entry(&ep->ready_playback_urbs,
+ ctx = list_first_entry(&ep->ready_playback_urbs,
struct snd_urb_ctx, ready_list);
- list_del_init(&ctx->ready_list);
- }
+ list_del_init(&ctx->ready_list);
+
+ packet = next_packet_fifo_dequeue(ep);
}
spin_unlock_irqrestore(&ep->lock, flags);
@@ -365,12 +415,15 @@ static void queue_pending_output_urbs(struct snd_usb_endpoint *ep)
prepare_outbound_urb(ep, ctx);
err = usb_submit_urb(ctx->urb, GFP_ATOMIC);
- if (err < 0)
+ if (err < 0) {
usb_audio_err(ep->chip,
- "Unable to submit urb #%d: %d (urb %p)\n",
- ctx->index, err, ctx->urb);
- else
- set_bit(ctx->index, &ep->active_mask);
+ "Unable to submit urb #%d: %d at %s\n",
+ ctx->index, err, __func__);
+ notify_xrun(ep);
+ return;
+ }
+
+ set_bit(ctx->index, &ep->active_mask);
}
}
@@ -381,7 +434,6 @@ static void snd_complete_urb(struct urb *urb)
{
struct snd_urb_ctx *ctx = urb->context;
struct snd_usb_endpoint *ep = ctx->ep;
- struct snd_pcm_substream *substream;
unsigned long flags;
int err;
@@ -406,10 +458,10 @@ static void snd_complete_urb(struct urb *urb)
if (snd_usb_endpoint_implicit_feedback_sink(ep)) {
spin_lock_irqsave(&ep->lock, flags);
list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
+ clear_bit(ctx->index, &ep->active_mask);
spin_unlock_irqrestore(&ep->lock, flags);
queue_pending_output_urbs(ep);
-
- goto exit_clear;
+ return;
}
prepare_outbound_urb(ep, ctx);
@@ -430,27 +482,43 @@ static void snd_complete_urb(struct urb *urb)
return;
usb_audio_err(ep->chip, "cannot submit urb (err = %d)\n", err);
- if (ep->data_subs && ep->data_subs->pcm_substream) {
- substream = ep->data_subs->pcm_substream;
- snd_pcm_stop_xrun(substream);
- }
+ notify_xrun(ep);
exit_clear:
clear_bit(ctx->index, &ep->active_mask);
}
+/*
+ * Get the existing endpoint object corresponding EP
+ * Returns NULL if not present.
+ */
+struct snd_usb_endpoint *
+snd_usb_get_endpoint(struct snd_usb_audio *chip, int ep_num)
+{
+ struct snd_usb_endpoint *ep;
+
+ list_for_each_entry(ep, &chip->ep_list, list) {
+ if (ep->ep_num == ep_num)
+ return ep;
+ }
+
+ return NULL;
+}
+
+#define ep_type_name(type) \
+ (type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync")
+
/**
* snd_usb_add_endpoint: Add an endpoint to an USB audio chip
*
* @chip: The chip
- * @alts: The USB host interface
* @ep_num: The number of the endpoint to use
- * @direction: SNDRV_PCM_STREAM_PLAYBACK or SNDRV_PCM_STREAM_CAPTURE
* @type: SND_USB_ENDPOINT_TYPE_DATA or SND_USB_ENDPOINT_TYPE_SYNC
*
* If the requested endpoint has not been added to the given chip before,
- * a new instance is created. Otherwise, a pointer to the previoulsy
- * created instance is returned. In case of any error, NULL is returned.
+ * a new instance is created.
+ *
+ * Returns zero on success or a negative error code.
*
* New endpoints will be added to chip->ep_list and must be freed by
* calling snd_usb_endpoint_free().
@@ -458,79 +526,258 @@ exit_clear:
* For SND_USB_ENDPOINT_TYPE_SYNC, the caller needs to guarantee that
* bNumEndpoints > 1 beforehand.
*/
-struct snd_usb_endpoint *snd_usb_add_endpoint(struct snd_usb_audio *chip,
- struct usb_host_interface *alts,
- int ep_num, int direction, int type)
+int snd_usb_add_endpoint(struct snd_usb_audio *chip, int ep_num, int type)
{
struct snd_usb_endpoint *ep;
- int is_playback = direction == SNDRV_PCM_STREAM_PLAYBACK;
+ bool is_playback;
- if (WARN_ON(!alts))
- return NULL;
-
- mutex_lock(&chip->mutex);
-
- list_for_each_entry(ep, &chip->ep_list, list) {
- if (ep->ep_num == ep_num &&
- ep->iface == alts->desc.bInterfaceNumber &&
- ep->altsetting == alts->desc.bAlternateSetting) {
- usb_audio_dbg(ep->chip,
- "Re-using EP %x in iface %d,%d @%p\n",
- ep_num, ep->iface, ep->altsetting, ep);
- goto __exit_unlock;
- }
- }
-
- usb_audio_dbg(chip, "Creating new %s %s endpoint #%x\n",
- is_playback ? "playback" : "capture",
- type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync",
- ep_num);
+ ep = snd_usb_get_endpoint(chip, ep_num);
+ if (ep)
+ return 0;
+ usb_audio_dbg(chip, "Creating new %s endpoint #%x\n",
+ ep_type_name(type),
+ ep_num);
ep = kzalloc(sizeof(*ep), GFP_KERNEL);
if (!ep)
- goto __exit_unlock;
+ return -ENOMEM;
ep->chip = chip;
spin_lock_init(&ep->lock);
ep->type = type;
ep->ep_num = ep_num;
- ep->iface = alts->desc.bInterfaceNumber;
- ep->altsetting = alts->desc.bAlternateSetting;
INIT_LIST_HEAD(&ep->ready_playback_urbs);
- ep_num &= USB_ENDPOINT_NUMBER_MASK;
+ is_playback = ((ep_num & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
+ ep_num &= USB_ENDPOINT_NUMBER_MASK;
if (is_playback)
ep->pipe = usb_sndisocpipe(chip->dev, ep_num);
else
ep->pipe = usb_rcvisocpipe(chip->dev, ep_num);
- if (type == SND_USB_ENDPOINT_TYPE_SYNC) {
- if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
- get_endpoint(alts, 1)->bRefresh >= 1 &&
- get_endpoint(alts, 1)->bRefresh <= 9)
- ep->syncinterval = get_endpoint(alts, 1)->bRefresh;
- else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL)
- ep->syncinterval = 1;
- else if (get_endpoint(alts, 1)->bInterval >= 1 &&
- get_endpoint(alts, 1)->bInterval <= 16)
- ep->syncinterval = get_endpoint(alts, 1)->bInterval - 1;
- else
- ep->syncinterval = 3;
-
- ep->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize);
- }
-
list_add_tail(&ep->list, &chip->ep_list);
+ return 0;
+}
+
+/* Set up syncinterval and maxsyncsize for a sync EP */
+static void endpoint_set_syncinterval(struct snd_usb_audio *chip,
+ struct snd_usb_endpoint *ep)
+{
+ struct usb_host_interface *alts;
+ struct usb_endpoint_descriptor *desc;
+
+ alts = snd_usb_get_host_interface(chip, ep->iface, ep->altsetting);
+ if (!alts)
+ return;
+
+ desc = get_endpoint(alts, ep->ep_idx);
+ if (desc->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
+ desc->bRefresh >= 1 && desc->bRefresh <= 9)
+ ep->syncinterval = desc->bRefresh;
+ else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL)
+ ep->syncinterval = 1;
+ else if (desc->bInterval >= 1 && desc->bInterval <= 16)
+ ep->syncinterval = desc->bInterval - 1;
+ else
+ ep->syncinterval = 3;
+
+ ep->syncmaxsize = le16_to_cpu(desc->wMaxPacketSize);
+}
- ep->is_implicit_feedback = 0;
+static bool endpoint_compatible(struct snd_usb_endpoint *ep,
+ const struct audioformat *fp,
+ const struct snd_pcm_hw_params *params)
+{
+ if (!ep->opened)
+ return false;
+ if (ep->cur_audiofmt != fp)
+ return false;
+ if (ep->cur_rate != params_rate(params) ||
+ ep->cur_format != params_format(params) ||
+ ep->cur_period_frames != params_period_size(params) ||
+ ep->cur_buffer_periods != params_periods(params))
+ return false;
+ return true;
+}
+
+/*
+ * Check whether the given fp and hw params are compatbile with the current
+ * setup of the target EP for implicit feedback sync
+ */
+bool snd_usb_endpoint_compatible(struct snd_usb_audio *chip,
+ struct snd_usb_endpoint *ep,
+ const struct audioformat *fp,
+ const struct snd_pcm_hw_params *params)
+{
+ bool ret;
-__exit_unlock:
+ mutex_lock(&chip->mutex);
+ ret = endpoint_compatible(ep, fp, params);
mutex_unlock(&chip->mutex);
+ return ret;
+}
+/*
+ * snd_usb_endpoint_open: Open the endpoint
+ *
+ * Called from hw_params to assign the endpoint to the substream.
+ * It's reference-counted, and only the first opener is allowed to set up
+ * arbitrary parameters. The later opener must be compatible with the
+ * former opened parameters.
+ * The endpoint needs to be closed via snd_usb_endpoint_close() later.
+ *
+ * Note that this function doesn't configure the endpoint. The substream
+ * needs to set it up later via snd_usb_endpoint_configure().
+ */
+struct snd_usb_endpoint *
+snd_usb_endpoint_open(struct snd_usb_audio *chip,
+ const struct audioformat *fp,
+ const struct snd_pcm_hw_params *params,
+ bool is_sync_ep)
+{
+ struct snd_usb_endpoint *ep;
+ int ep_num = is_sync_ep ? fp->sync_ep : fp->endpoint;
+
+ mutex_lock(&chip->mutex);
+ ep = snd_usb_get_endpoint(chip, ep_num);
+ if (!ep) {
+ usb_audio_err(chip, "Cannot find EP 0x%x to open\n", ep_num);
+ goto unlock;
+ }
+
+ if (!ep->opened) {
+ if (is_sync_ep) {
+ ep->iface = fp->sync_iface;
+ ep->altsetting = fp->sync_altsetting;
+ ep->ep_idx = fp->sync_ep_idx;
+ } else {
+ ep->iface = fp->iface;
+ ep->altsetting = fp->altsetting;
+ ep->ep_idx = 0;
+ }
+ usb_audio_dbg(chip, "Open EP 0x%x, iface=%d:%d, idx=%d\n",
+ ep_num, ep->iface, ep->altsetting, ep->ep_idx);
+
+ ep->cur_audiofmt = fp;
+ ep->cur_channels = fp->channels;
+ ep->cur_rate = params_rate(params);
+ ep->cur_format = params_format(params);
+ ep->cur_frame_bytes = snd_pcm_format_physical_width(ep->cur_format) *
+ ep->cur_channels / 8;
+ ep->cur_period_frames = params_period_size(params);
+ ep->cur_period_bytes = ep->cur_period_frames * ep->cur_frame_bytes;
+ ep->cur_buffer_periods = params_periods(params);
+
+ if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
+ endpoint_set_syncinterval(chip, ep);
+
+ ep->implicit_fb_sync = fp->implicit_fb;
+ ep->need_setup = true;
+
+ usb_audio_dbg(chip, " channels=%d, rate=%d, format=%s, period_bytes=%d, periods=%d, implicit_fb=%d\n",
+ ep->cur_channels, ep->cur_rate,
+ snd_pcm_format_name(ep->cur_format),
+ ep->cur_period_bytes, ep->cur_buffer_periods,
+ ep->implicit_fb_sync);
+
+ } else {
+ if (!endpoint_compatible(ep, fp, params)) {
+ usb_audio_err(chip, "Incompatible EP setup for 0x%x\n",
+ ep_num);
+ ep = NULL;
+ goto unlock;
+ }
+
+ usb_audio_dbg(chip, "Reopened EP 0x%x (count %d)\n",
+ ep_num, ep->opened);
+ }
+
+ ep->opened++;
+
+ unlock:
+ mutex_unlock(&chip->mutex);
return ep;
}
/*
+ * snd_usb_endpoint_set_sync: Link data and sync endpoints
+ *
+ * Pass NULL to sync_ep to unlink again
+ */
+void snd_usb_endpoint_set_sync(struct snd_usb_audio *chip,
+ struct snd_usb_endpoint *data_ep,
+ struct snd_usb_endpoint *sync_ep)
+{
+ data_ep->sync_source = sync_ep;
+}
+
+/*
+ * Set data endpoint callbacks and the assigned data stream
+ *
+ * Called at PCM trigger and cleanups.
+ * Pass NULL to deactivate each callback.
+ */
+void snd_usb_endpoint_set_callback(struct snd_usb_endpoint *ep,
+ void (*prepare)(struct snd_usb_substream *subs,
+ struct urb *urb),
+ void (*retire)(struct snd_usb_substream *subs,
+ struct urb *urb),
+ struct snd_usb_substream *data_subs)
+{
+ ep->prepare_data_urb = prepare;
+ ep->retire_data_urb = retire;
+ WRITE_ONCE(ep->data_subs, data_subs);
+}
+
+static int endpoint_set_interface(struct snd_usb_audio *chip,
+ struct snd_usb_endpoint *ep,
+ bool set)
+{
+ int altset = set ? ep->altsetting : 0;
+ int err;
+
+ usb_audio_dbg(chip, "Setting usb interface %d:%d for EP 0x%x\n",
+ ep->iface, altset, ep->ep_num);
+ err = usb_set_interface(chip->dev, ep->iface, altset);
+ if (err < 0) {
+ usb_audio_err(chip, "%d:%d: usb_set_interface failed (%d)\n",
+ ep->iface, altset, err);
+ return err;
+ }
+
+ snd_usb_set_interface_quirk(chip);
+ return 0;
+}
+
+/*
+ * snd_usb_endpoint_close: Close the endpoint
+ *
+ * Unreference the already opened endpoint via snd_usb_endpoint_open().
+ */
+void snd_usb_endpoint_close(struct snd_usb_audio *chip,
+ struct snd_usb_endpoint *ep)
+{
+ mutex_lock(&chip->mutex);
+ usb_audio_dbg(chip, "Closing EP 0x%x (count %d)\n",
+ ep->ep_num, ep->opened);
+ if (!--ep->opened) {
+ endpoint_set_interface(chip, ep, false);
+ ep->iface = 0;
+ ep->altsetting = 0;
+ ep->cur_audiofmt = NULL;
+ ep->cur_rate = 0;
+ usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num);
+ }
+ mutex_unlock(&chip->mutex);
+}
+
+/* Prepare for suspening EP, called from the main suspend handler */
+void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep)
+{
+ ep->need_setup = true;
+}
+
+/*
* wait until all urbs are processed.
*/
static int wait_clear_urbs(struct snd_usb_endpoint *ep)
@@ -538,6 +785,9 @@ static int wait_clear_urbs(struct snd_usb_endpoint *ep)
unsigned long end_time = jiffies + msecs_to_jiffies(1000);
int alive;
+ if (!test_bit(EP_FLAG_STOPPING, &ep->flags))
+ return 0;
+
do {
alive = bitmap_weight(&ep->active_mask, ep->nurbs);
if (!alive)
@@ -552,10 +802,8 @@ static int wait_clear_urbs(struct snd_usb_endpoint *ep)
alive, ep->ep_num);
clear_bit(EP_FLAG_STOPPING, &ep->flags);
- ep->data_subs = NULL;
- ep->sync_slave = NULL;
- ep->retire_data_urb = NULL;
- ep->prepare_data_urb = NULL;
+ ep->sync_sink = NULL;
+ snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
return 0;
}
@@ -565,25 +813,34 @@ static int wait_clear_urbs(struct snd_usb_endpoint *ep)
*/
void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep)
{
- if (ep && test_bit(EP_FLAG_STOPPING, &ep->flags))
+ if (ep)
wait_clear_urbs(ep);
}
/*
- * unlink active urbs.
+ * Stop and unlink active urbs.
+ *
+ * This function checks and clears EP_FLAG_RUNNING state.
+ * When @wait_sync is set, it waits until all pending URBs are killed.
*/
-static int deactivate_urbs(struct snd_usb_endpoint *ep, bool force)
+static int stop_and_unlink_urbs(struct snd_usb_endpoint *ep, bool force,
+ bool wait_sync)
{
unsigned int i;
if (!force && atomic_read(&ep->chip->shutdown)) /* to be sure... */
return -EBADFD;
- clear_bit(EP_FLAG_RUNNING, &ep->flags);
+ if (atomic_read(&ep->running))
+ return -EBUSY;
+ if (!test_and_clear_bit(EP_FLAG_RUNNING, &ep->flags))
+ goto out;
+
+ set_bit(EP_FLAG_STOPPING, &ep->flags);
INIT_LIST_HEAD(&ep->ready_playback_urbs);
- ep->next_packet_read_pos = 0;
- ep->next_packet_write_pos = 0;
+ ep->next_packet_head = 0;
+ ep->next_packet_queued = 0;
for (i = 0; i < ep->nurbs; i++) {
if (test_bit(i, &ep->active_mask)) {
@@ -594,6 +851,9 @@ static int deactivate_urbs(struct snd_usb_endpoint *ep, bool force)
}
}
+ out:
+ if (wait_sync)
+ return wait_clear_urbs(ep);
return 0;
}
@@ -605,12 +865,10 @@ static void release_urbs(struct snd_usb_endpoint *ep, int force)
int i;
/* route incoming urbs to nirvana */
- ep->retire_data_urb = NULL;
- ep->prepare_data_urb = NULL;
+ snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
/* stop urbs */
- deactivate_urbs(ep, force);
- wait_clear_urbs(ep);
+ stop_and_unlink_urbs(ep, force, true);
for (i = 0; i < ep->nurbs; i++)
release_urb_ctx(&ep->urb[i]);
@@ -623,209 +881,35 @@ static void release_urbs(struct snd_usb_endpoint *ep, int force)
}
/*
- * Check data endpoint for format differences
- */
-static bool check_ep_params(struct snd_usb_endpoint *ep,
- snd_pcm_format_t pcm_format,
- unsigned int channels,
- unsigned int period_bytes,
- unsigned int frames_per_period,
- unsigned int periods_per_buffer,
- struct audioformat *fmt,
- struct snd_usb_endpoint *sync_ep)
-{
- unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb;
- unsigned int max_packs_per_period, urbs_per_period, urb_packs;
- unsigned int max_urbs;
- int frame_bits = snd_pcm_format_physical_width(pcm_format) * channels;
- int tx_length_quirk = (ep->chip->tx_length_quirk &&
- usb_pipeout(ep->pipe));
- bool ret = 1;
-
- if (pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) {
- /*
- * When operating in DSD DOP mode, the size of a sample frame
- * in hardware differs from the actual physical format width
- * because we need to make room for the DOP markers.
- */
- frame_bits += channels << 3;
- }
-
- ret = ret && (ep->datainterval == fmt->datainterval);
- ret = ret && (ep->stride == frame_bits >> 3);
-
- switch (pcm_format) {
- case SNDRV_PCM_FORMAT_U8:
- ret = ret && (ep->silence_value == 0x80);
- break;
- case SNDRV_PCM_FORMAT_DSD_U8:
- case SNDRV_PCM_FORMAT_DSD_U16_LE:
- case SNDRV_PCM_FORMAT_DSD_U32_LE:
- case SNDRV_PCM_FORMAT_DSD_U16_BE:
- case SNDRV_PCM_FORMAT_DSD_U32_BE:
- ret = ret && (ep->silence_value == 0x69);
- break;
- default:
- ret = ret && (ep->silence_value == 0);
- }
-
- /* assume max. frequency is 50% higher than nominal */
- ret = ret && (ep->freqmax == ep->freqn + (ep->freqn >> 1));
- /* Round up freqmax to nearest integer in order to calculate maximum
- * packet size, which must represent a whole number of frames.
- * This is accomplished by adding 0x0.ffff before converting the
- * Q16.16 format into integer.
- * In order to accurately calculate the maximum packet size when
- * the data interval is more than 1 (i.e. ep->datainterval > 0),
- * multiply by the data interval prior to rounding. For instance,
- * a freqmax of 41 kHz will result in a max packet size of 6 (5.125)
- * frames with a data interval of 1, but 11 (10.25) frames with a
- * data interval of 2.
- * (ep->freqmax << ep->datainterval overflows at 8.192 MHz for the
- * maximum datainterval value of 3, at USB full speed, higher for
- * USB high speed, noting that ep->freqmax is in units of
- * frames per packet in Q16.16 format.)
- */
- maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) *
- (frame_bits >> 3);
- if (tx_length_quirk)
- maxsize += sizeof(__le32); /* Space for length descriptor */
- /* but wMaxPacketSize might reduce this */
- if (ep->maxpacksize && ep->maxpacksize < maxsize) {
- /* whatever fits into a max. size packet */
- unsigned int data_maxsize = maxsize = ep->maxpacksize;
-
- if (tx_length_quirk)
- /* Need to remove the length descriptor to calc freq */
- data_maxsize -= sizeof(__le32);
- ret = ret && (ep->freqmax == (data_maxsize / (frame_bits >> 3))
- << (16 - ep->datainterval));
- }
-
- if (ep->fill_max)
- ret = ret && (ep->curpacksize == ep->maxpacksize);
- else
- ret = ret && (ep->curpacksize == maxsize);
-
- if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL) {
- packs_per_ms = 8 >> ep->datainterval;
- max_packs_per_urb = MAX_PACKS_HS;
- } else {
- packs_per_ms = 1;
- max_packs_per_urb = MAX_PACKS;
- }
- if (sync_ep && !snd_usb_endpoint_implicit_feedback_sink(ep))
- max_packs_per_urb = min(max_packs_per_urb,
- 1U << sync_ep->syncinterval);
- max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval);
-
- /*
- * Capture endpoints need to use small URBs because there's no way
- * to tell in advance where the next period will end, and we don't
- * want the next URB to complete much after the period ends.
- *
- * Playback endpoints with implicit sync much use the same parameters
- * as their corresponding capture endpoint.
- */
- if (usb_pipein(ep->pipe) ||
- snd_usb_endpoint_implicit_feedback_sink(ep)) {
-
- urb_packs = packs_per_ms;
- /*
- * Wireless devices can poll at a max rate of once per 4ms.
- * For dataintervals less than 5, increase the packet count to
- * allow the host controller to use bursting to fill in the
- * gaps.
- */
- if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_WIRELESS) {
- int interval = ep->datainterval;
-
- while (interval < 5) {
- urb_packs <<= 1;
- ++interval;
- }
- }
- /* make capture URBs <= 1 ms and smaller than a period */
- urb_packs = min(max_packs_per_urb, urb_packs);
- while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
- urb_packs >>= 1;
- ret = ret && (ep->nurbs == MAX_URBS);
-
- /*
- * Playback endpoints without implicit sync are adjusted so that
- * a period fits as evenly as possible in the smallest number of
- * URBs. The total number of URBs is adjusted to the size of the
- * ALSA buffer, subject to the MAX_URBS and MAX_QUEUE limits.
- */
- } else {
- /* determine how small a packet can be */
- minsize = (ep->freqn >> (16 - ep->datainterval)) *
- (frame_bits >> 3);
- /* with sync from device, assume it can be 12% lower */
- if (sync_ep)
- minsize -= minsize >> 3;
- minsize = max(minsize, 1u);
-
- /* how many packets will contain an entire ALSA period? */
- max_packs_per_period = DIV_ROUND_UP(period_bytes, minsize);
-
- /* how many URBs will contain a period? */
- urbs_per_period = DIV_ROUND_UP(max_packs_per_period,
- max_packs_per_urb);
- /* how many packets are needed in each URB? */
- urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period);
-
- /* limit the number of frames in a single URB */
- ret = ret && (ep->max_urb_frames ==
- DIV_ROUND_UP(frames_per_period, urbs_per_period));
-
- /* try to use enough URBs to contain an entire ALSA buffer */
- max_urbs = min((unsigned) MAX_URBS,
- MAX_QUEUE * packs_per_ms / urb_packs);
- ret = ret && (ep->nurbs == min(max_urbs,
- urbs_per_period * periods_per_buffer));
- }
-
- ret = ret && (ep->datainterval == fmt->datainterval);
- ret = ret && (ep->maxpacksize == fmt->maxpacksize);
- ret = ret &&
- (ep->fill_max == !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX));
-
- return ret;
-}
-
-/*
* configure a data endpoint
*/
-static int data_ep_set_params(struct snd_usb_endpoint *ep,
- snd_pcm_format_t pcm_format,
- unsigned int channels,
- unsigned int period_bytes,
- unsigned int frames_per_period,
- unsigned int periods_per_buffer,
- struct audioformat *fmt,
- struct snd_usb_endpoint *sync_ep)
+static int data_ep_set_params(struct snd_usb_endpoint *ep)
{
+ struct snd_usb_audio *chip = ep->chip;
unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb;
unsigned int max_packs_per_period, urbs_per_period, urb_packs;
unsigned int max_urbs, i;
- int frame_bits = snd_pcm_format_physical_width(pcm_format) * channels;
- int tx_length_quirk = (ep->chip->tx_length_quirk &&
+ const struct audioformat *fmt = ep->cur_audiofmt;
+ int frame_bits = ep->cur_frame_bytes * 8;
+ int tx_length_quirk = (chip->tx_length_quirk &&
usb_pipeout(ep->pipe));
- if (pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) {
+ usb_audio_dbg(chip, "Setting params for data EP 0x%x, pipe 0x%x\n",
+ ep->ep_num, ep->pipe);
+
+ if (ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) {
/*
* When operating in DSD DOP mode, the size of a sample frame
* in hardware differs from the actual physical format width
* because we need to make room for the DOP markers.
*/
- frame_bits += channels << 3;
+ frame_bits += ep->cur_channels << 3;
}
ep->datainterval = fmt->datainterval;
ep->stride = frame_bits >> 3;
- switch (pcm_format) {
+ switch (ep->cur_format) {
case SNDRV_PCM_FORMAT_U8:
ep->silence_value = 0x80;
break;
@@ -878,16 +962,16 @@ static int data_ep_set_params(struct snd_usb_endpoint *ep,
else
ep->curpacksize = maxsize;
- if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL) {
+ if (snd_usb_get_speed(chip->dev) != USB_SPEED_FULL) {
packs_per_ms = 8 >> ep->datainterval;
max_packs_per_urb = MAX_PACKS_HS;
} else {
packs_per_ms = 1;
max_packs_per_urb = MAX_PACKS;
}
- if (sync_ep && !snd_usb_endpoint_implicit_feedback_sink(ep))
+ if (ep->sync_source && !ep->implicit_fb_sync)
max_packs_per_urb = min(max_packs_per_urb,
- 1U << sync_ep->syncinterval);
+ 1U << ep->sync_source->syncinterval);
max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval);
/*
@@ -898,8 +982,7 @@ static int data_ep_set_params(struct snd_usb_endpoint *ep,
* Playback endpoints with implicit sync much use the same parameters
* as their corresponding capture endpoint.
*/
- if (usb_pipein(ep->pipe) ||
- snd_usb_endpoint_implicit_feedback_sink(ep)) {
+ if (usb_pipein(ep->pipe) || ep->implicit_fb_sync) {
urb_packs = packs_per_ms;
/*
@@ -908,7 +991,7 @@ static int data_ep_set_params(struct snd_usb_endpoint *ep,
* allow the host controller to use bursting to fill in the
* gaps.
*/
- if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_WIRELESS) {
+ if (snd_usb_get_speed(chip->dev) == USB_SPEED_WIRELESS) {
int interval = ep->datainterval;
while (interval < 5) {
urb_packs <<= 1;
@@ -917,7 +1000,7 @@ static int data_ep_set_params(struct snd_usb_endpoint *ep,
}
/* make capture URBs <= 1 ms and smaller than a period */
urb_packs = min(max_packs_per_urb, urb_packs);
- while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
+ while (urb_packs > 1 && urb_packs * maxsize >= ep->cur_period_bytes)
urb_packs >>= 1;
ep->nurbs = MAX_URBS;
@@ -932,12 +1015,12 @@ static int data_ep_set_params(struct snd_usb_endpoint *ep,
minsize = (ep->freqn >> (16 - ep->datainterval)) *
(frame_bits >> 3);
/* with sync from device, assume it can be 12% lower */
- if (sync_ep)
+ if (ep->sync_source)
minsize -= minsize >> 3;
minsize = max(minsize, 1u);
/* how many packets will contain an entire ALSA period? */
- max_packs_per_period = DIV_ROUND_UP(period_bytes, minsize);
+ max_packs_per_period = DIV_ROUND_UP(ep->cur_period_bytes, minsize);
/* how many URBs will contain a period? */
urbs_per_period = DIV_ROUND_UP(max_packs_per_period,
@@ -946,13 +1029,13 @@ static int data_ep_set_params(struct snd_usb_endpoint *ep,
urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period);
/* limit the number of frames in a single URB */
- ep->max_urb_frames = DIV_ROUND_UP(frames_per_period,
- urbs_per_period);
+ ep->max_urb_frames = DIV_ROUND_UP(ep->cur_period_frames,
+ urbs_per_period);
/* try to use enough URBs to contain an entire ALSA buffer */
max_urbs = min((unsigned) MAX_URBS,
MAX_QUEUE * packs_per_ms / urb_packs);
- ep->nurbs = min(max_urbs, urbs_per_period * periods_per_buffer);
+ ep->nurbs = min(max_urbs, urbs_per_period * ep->cur_buffer_periods);
}
/* allocate and initialize data urbs */
@@ -970,7 +1053,7 @@ static int data_ep_set_params(struct snd_usb_endpoint *ep,
goto out_of_memory;
u->urb->transfer_buffer =
- usb_alloc_coherent(ep->chip->dev, u->buffer_size,
+ usb_alloc_coherent(chip->dev, u->buffer_size,
GFP_KERNEL, &u->urb->transfer_dma);
if (!u->urb->transfer_buffer)
goto out_of_memory;
@@ -994,9 +1077,13 @@ out_of_memory:
*/
static int sync_ep_set_params(struct snd_usb_endpoint *ep)
{
+ struct snd_usb_audio *chip = ep->chip;
int i;
- ep->syncbuf = usb_alloc_coherent(ep->chip->dev, SYNC_URBS * 4,
+ usb_audio_dbg(chip, "Setting params for sync EP 0x%x, pipe 0x%x\n",
+ ep->ep_num, ep->pipe);
+
+ ep->syncbuf = usb_alloc_coherent(chip->dev, SYNC_URBS * 4,
GFP_KERNEL, &ep->sync_dma);
if (!ep->syncbuf)
return -ENOMEM;
@@ -1029,55 +1116,19 @@ out_of_memory:
return -ENOMEM;
}
-/**
+/*
* snd_usb_endpoint_set_params: configure an snd_usb_endpoint
*
- * @ep: the snd_usb_endpoint to configure
- * @pcm_format: the audio fomat.
- * @channels: the number of audio channels.
- * @period_bytes: the number of bytes in one alsa period.
- * @period_frames: the number of frames in one alsa period.
- * @buffer_periods: the number of periods in one alsa buffer.
- * @rate: the frame rate.
- * @fmt: the USB audio format information
- * @sync_ep: the sync endpoint to use, if any
- *
* Determine the number of URBs to be used on this endpoint.
* An endpoint must be configured before it can be started.
* An endpoint that is already running can not be reconfigured.
*/
-int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep,
- snd_pcm_format_t pcm_format,
- unsigned int channels,
- unsigned int period_bytes,
- unsigned int period_frames,
- unsigned int buffer_periods,
- unsigned int rate,
- struct audioformat *fmt,
- struct snd_usb_endpoint *sync_ep)
+static int snd_usb_endpoint_set_params(struct snd_usb_audio *chip,
+ struct snd_usb_endpoint *ep)
{
+ const struct audioformat *fmt = ep->cur_audiofmt;
int err;
- if (ep->use_count != 0) {
- bool check = ep->is_implicit_feedback &&
- check_ep_params(ep, pcm_format,
- channels, period_bytes,
- period_frames, buffer_periods,
- fmt, sync_ep);
-
- if (!check) {
- usb_audio_warn(ep->chip,
- "Unable to change format on ep #%x: already in use\n",
- ep->ep_num);
- return -EBUSY;
- }
-
- usb_audio_dbg(ep->chip,
- "Ep #%x already in use as implicit feedback but format not changed\n",
- ep->ep_num);
- return 0;
- }
-
/* release old buffers, if any */
release_urbs(ep, 0);
@@ -1085,17 +1136,17 @@ int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep,
ep->maxpacksize = fmt->maxpacksize;
ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX);
- if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL) {
- ep->freqn = get_usb_full_speed_rate(rate);
+ if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) {
+ ep->freqn = get_usb_full_speed_rate(ep->cur_rate);
ep->pps = 1000 >> ep->datainterval;
} else {
- ep->freqn = get_usb_high_speed_rate(rate);
+ ep->freqn = get_usb_high_speed_rate(ep->cur_rate);
ep->pps = 8000 >> ep->datainterval;
}
- ep->sample_rem = rate % ep->pps;
- ep->packsize[0] = rate / ep->pps;
- ep->packsize[1] = (rate + (ep->pps - 1)) / ep->pps;
+ ep->sample_rem = ep->cur_rate % ep->pps;
+ ep->packsize[0] = ep->cur_rate / ep->pps;
+ ep->packsize[1] = (ep->cur_rate + (ep->pps - 1)) / ep->pps;
/* calculate the frequency in 16.16 format */
ep->freqm = ep->freqn;
@@ -1105,9 +1156,7 @@ int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep,
switch (ep->type) {
case SND_USB_ENDPOINT_TYPE_DATA:
- err = data_ep_set_params(ep, pcm_format, channels,
- period_bytes, period_frames,
- buffer_periods, fmt, sync_ep);
+ err = data_ep_set_params(ep);
break;
case SND_USB_ENDPOINT_TYPE_SYNC:
err = sync_ep_set_params(ep);
@@ -1116,10 +1165,89 @@ int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep,
err = -EINVAL;
}
- usb_audio_dbg(ep->chip,
- "Setting params for ep #%x (type %d, %d urbs), ret=%d\n",
- ep->ep_num, ep->type, ep->nurbs, err);
+ usb_audio_dbg(chip, "Set up %d URBS, ret=%d\n", ep->nurbs, err);
+
+ if (err < 0)
+ return err;
+
+ /* some unit conversions in runtime */
+ ep->maxframesize = ep->maxpacksize / ep->cur_frame_bytes;
+ ep->curframesize = ep->curpacksize / ep->cur_frame_bytes;
+
+ return 0;
+}
+
+/*
+ * snd_usb_endpoint_configure: Configure the endpoint
+ *
+ * This function sets up the EP to be fully usable state.
+ * It's called either from hw_params or prepare callback.
+ * The function checks need_setup flag, and perfoms nothing unless needed,
+ * so it's safe to call this multiple times.
+ *
+ * This returns zero if unchanged, 1 if the configuration has changed,
+ * or a negative error code.
+ */
+int snd_usb_endpoint_configure(struct snd_usb_audio *chip,
+ struct snd_usb_endpoint *ep)
+{
+ bool iface_first;
+ int err = 0;
+
+ mutex_lock(&chip->mutex);
+ if (!ep->need_setup)
+ goto unlock;
+
+ /* No need to (re-)configure the sync EP belonging to the same altset */
+ if (ep->ep_idx) {
+ err = snd_usb_endpoint_set_params(chip, ep);
+ if (err < 0)
+ goto unlock;
+ goto done;
+ }
+
+ /* Need to deselect altsetting at first */
+ endpoint_set_interface(chip, ep, false);
+
+ /* Some UAC1 devices (e.g. Yamaha THR10) need the host interface
+ * to be set up before parameter setups
+ */
+ iface_first = ep->cur_audiofmt->protocol == UAC_VERSION_1;
+ if (iface_first) {
+ err = endpoint_set_interface(chip, ep, true);
+ if (err < 0)
+ goto unlock;
+ }
+
+ err = snd_usb_init_pitch(chip, ep->cur_audiofmt);
+ if (err < 0)
+ goto unlock;
+ err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, ep->cur_rate);
+ if (err < 0)
+ goto unlock;
+
+ err = snd_usb_endpoint_set_params(chip, ep);
+ if (err < 0)
+ goto unlock;
+
+ err = snd_usb_select_mode_quirk(chip, ep->cur_audiofmt);
+ if (err < 0)
+ goto unlock;
+
+ /* for UAC2/3, enable the interface altset here at last */
+ if (!iface_first) {
+ err = endpoint_set_interface(chip, ep, true);
+ if (err < 0)
+ goto unlock;
+ }
+
+ done:
+ ep->need_setup = false;
+ err = 1;
+
+unlock:
+ mutex_unlock(&chip->mutex);
return err;
}
@@ -1128,7 +1256,7 @@ int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep,
*
* @ep: the endpoint to start
*
- * A call to this function will increment the use count of the endpoint.
+ * A call to this function will increment the running count of the endpoint.
* In case it is not already running, the URBs for this endpoint will be
* submitted. Otherwise, this function does nothing.
*
@@ -1144,13 +1272,17 @@ int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)
if (atomic_read(&ep->chip->shutdown))
return -EBADFD;
+ if (ep->sync_source)
+ WRITE_ONCE(ep->sync_source->sync_sink, ep);
+
+ usb_audio_dbg(ep->chip, "Starting %s EP 0x%x (running %d)\n",
+ ep_type_name(ep->type), ep->ep_num,
+ atomic_read(&ep->running));
+
/* already running? */
- if (++ep->use_count != 1)
+ if (atomic_inc_return(&ep->running) != 1)
return 0;
- /* just to be sure */
- deactivate_urbs(ep, false);
-
ep->active_mask = 0;
ep->unlink_mask = 0;
ep->phase = 0;
@@ -1173,6 +1305,7 @@ int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)
list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
}
+ usb_audio_dbg(ep->chip, "No URB submission due to implicit fb sync\n");
return 0;
}
@@ -1198,12 +1331,12 @@ int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)
set_bit(i, &ep->active_mask);
}
+ usb_audio_dbg(ep->chip, "%d URBs submitted for EP 0x%x\n",
+ ep->nurbs, ep->ep_num);
return 0;
__error:
- clear_bit(EP_FLAG_RUNNING, &ep->flags);
- ep->use_count--;
- deactivate_urbs(ep, false);
+ snd_usb_endpoint_stop(ep);
return -EPIPE;
}
@@ -1212,7 +1345,7 @@ __error:
*
* @ep: the endpoint to stop (may be NULL)
*
- * A call to this function will decrement the use count of the endpoint.
+ * A call to this function will decrement the running count of the endpoint.
* In case the last user has requested the endpoint stop, the URBs will
* actually be deactivated.
*
@@ -1226,35 +1359,18 @@ void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep)
if (!ep)
return;
- if (snd_BUG_ON(ep->use_count == 0))
- return;
-
- if (--ep->use_count == 0) {
- deactivate_urbs(ep, false);
- set_bit(EP_FLAG_STOPPING, &ep->flags);
- }
-}
+ usb_audio_dbg(ep->chip, "Stopping %s EP 0x%x (running %d)\n",
+ ep_type_name(ep->type), ep->ep_num,
+ atomic_read(&ep->running));
-/**
- * snd_usb_endpoint_deactivate: deactivate an snd_usb_endpoint
- *
- * @ep: the endpoint to deactivate
- *
- * If the endpoint is not currently in use, this functions will
- * deactivate its associated URBs.
- *
- * In case of any active users, this functions does nothing.
- */
-void snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep)
-{
- if (!ep)
+ if (snd_BUG_ON(!atomic_read(&ep->running)))
return;
- if (ep->use_count != 0)
- return;
+ if (ep->sync_source)
+ WRITE_ONCE(ep->sync_source->sync_sink, NULL);
- deactivate_urbs(ep, true);
- wait_clear_urbs(ep);
+ if (!atomic_dec_return(&ep->running))
+ stop_and_unlink_urbs(ep, false, false);
}
/**
@@ -1262,7 +1378,7 @@ void snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep)
*
* @ep: the endpoint to release
*
- * This function does not care for the endpoint's use count but will tear
+ * This function does not care for the endpoint's running count but will tear
* down all the streaming URBs immediately.
*/
void snd_usb_endpoint_release(struct snd_usb_endpoint *ep)
@@ -1282,7 +1398,7 @@ void snd_usb_endpoint_free(struct snd_usb_endpoint *ep)
kfree(ep);
}
-/**
+/*
* snd_usb_handle_sync_urb: parse an USB sync packet
*
* @ep: the endpoint to handle the packet
@@ -1292,9 +1408,9 @@ void snd_usb_endpoint_free(struct snd_usb_endpoint *ep)
* This function is called from the context of an endpoint that received
* the packet and is used to let another endpoint object handle the payload.
*/
-void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
- struct snd_usb_endpoint *sender,
- const struct urb *urb)
+static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
+ struct snd_usb_endpoint *sender,
+ const struct urb *urb)
{
int shift;
unsigned int f;
@@ -1309,7 +1425,7 @@ void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
* will take care of them later.
*/
if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
- ep->use_count != 0) {
+ atomic_read(&ep->running)) {
/* implicit feedback case */
int i, bytes = 0;
@@ -1331,7 +1447,16 @@ void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
return;
spin_lock_irqsave(&ep->lock, flags);
- out_packet = ep->next_packet + ep->next_packet_write_pos;
+ if (ep->next_packet_queued >= ARRAY_SIZE(ep->next_packet)) {
+ spin_unlock_irqrestore(&ep->lock, flags);
+ usb_audio_err(ep->chip,
+ "next package FIFO overflow EP 0x%x\n",
+ ep->ep_num);
+ notify_xrun(ep);
+ return;
+ }
+
+ out_packet = next_packet_fifo_enqueue(ep);
/*
* Iterate through the inbound packet and prepare the lengths
@@ -1352,8 +1477,6 @@ void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
out_packet->packet_size[i] = 0;
}
- ep->next_packet_write_pos++;
- ep->next_packet_write_pos %= MAX_URBS;
spin_unlock_irqrestore(&ep->lock, flags);
queue_pending_output_urbs(ep);