1 // SPDX-License-Identifier: GPL-2.0-or-later
6 #include <linux/init.h>
7 #include <linux/ratelimit.h>
9 #include <linux/usb/audio.h>
10 #include <linux/slab.h>
12 #include <sound/core.h>
13 #include <sound/pcm.h>
14 #include <sound/pcm_params.h>
30 /* interface refcounting */
31 struct snd_usb_iface_ref {
35 struct list_head list;
39 * snd_usb_endpoint is a model that abstracts everything related to an
40 * USB endpoint and its streaming.
42 * There are functions to activate and deactivate the streaming URBs and
43 * optional callbacks to let the pcm logic handle the actual content of the
44 * packets for playback and record. Thus, the bus streaming and the audio
45 * handlers are fully decoupled.
47 * There are two different types of endpoints in audio applications.
49 * SND_USB_ENDPOINT_TYPE_DATA handles full audio data payload for both
50 * inbound and outbound traffic.
52 * SND_USB_ENDPOINT_TYPE_SYNC endpoints are for inbound traffic only and
53 * expect the payload to carry Q10.14 / Q16.16 formatted sync information
56 * Each endpoint has to be configured prior to being used by calling
57 * snd_usb_endpoint_set_params().
59 * The model incorporates a reference counting, so that multiple users
60 * can call snd_usb_endpoint_start() and snd_usb_endpoint_stop(), and
61 * only the first user will effectively start the URBs, and only the last
62 * one to stop it will tear the URBs down again.
66 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
67 * this will overflow at approx 524 kHz
69 static inline unsigned get_usb_full_speed_rate(unsigned int rate)
71 return ((rate << 13) + 62) / 125;
75 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
76 * this will overflow at approx 4 MHz
78 static inline unsigned get_usb_high_speed_rate(unsigned int rate)
80 return ((rate << 10) + 62) / 125;
86 static void release_urb_ctx(struct snd_urb_ctx *u)
89 usb_free_coherent(u->ep->chip->dev, u->buffer_size,
90 u->urb->transfer_buffer,
91 u->urb->transfer_dma);
96 static const char *usb_error_string(int err)
102 return "endpoint not enabled";
104 return "endpoint stalled";
106 return "not enough bandwidth";
108 return "device disabled";
110 return "device suspended";
115 return "internal error";
117 return "unknown error";
121 static inline bool ep_state_running(struct snd_usb_endpoint *ep)
123 return atomic_read(&ep->state) == EP_STATE_RUNNING;
126 static inline bool ep_state_update(struct snd_usb_endpoint *ep, int old, int new)
128 return atomic_cmpxchg(&ep->state, old, new) == old;
132 * snd_usb_endpoint_implicit_feedback_sink: Report endpoint usage type
134 * @ep: The snd_usb_endpoint
136 * Determine whether an endpoint is driven by an implicit feedback
137 * data endpoint source.
139 int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep)
141 return ep->implicit_fb_sync && usb_pipeout(ep->pipe);
145 * Return the number of samples to be sent in the next packet
146 * for streaming based on information derived from sync endpoints
148 * This won't be used for implicit feedback which takes the packet size
149 * returned from the sync source
151 static int slave_next_packet_size(struct snd_usb_endpoint *ep)
157 return ep->maxframesize;
159 spin_lock_irqsave(&ep->lock, flags);
160 ep->phase = (ep->phase & 0xffff)
161 + (ep->freqm << ep->datainterval);
162 ret = min(ep->phase >> 16, ep->maxframesize);
163 spin_unlock_irqrestore(&ep->lock, flags);
169 * Return the number of samples to be sent in the next packet
170 * for adaptive and synchronous endpoints
172 static int next_packet_size(struct snd_usb_endpoint *ep)
177 return ep->maxframesize;
179 ep->sample_accum += ep->sample_rem;
180 if (ep->sample_accum >= ep->pps) {
181 ep->sample_accum -= ep->pps;
182 ret = ep->packsize[1];
184 ret = ep->packsize[0];
191 * snd_usb_endpoint_next_packet_size: Return the number of samples to be sent
194 int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep,
195 struct snd_urb_ctx *ctx, int idx)
197 if (ctx->packet_size[idx])
198 return ctx->packet_size[idx];
199 else if (ep->sync_source)
200 return slave_next_packet_size(ep);
202 return next_packet_size(ep);
205 static void call_retire_callback(struct snd_usb_endpoint *ep,
208 struct snd_usb_substream *data_subs;
210 data_subs = READ_ONCE(ep->data_subs);
211 if (data_subs && ep->retire_data_urb)
212 ep->retire_data_urb(data_subs, urb);
215 static void retire_outbound_urb(struct snd_usb_endpoint *ep,
216 struct snd_urb_ctx *urb_ctx)
218 call_retire_callback(ep, urb_ctx->urb);
221 static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
222 struct snd_usb_endpoint *sender,
223 const struct urb *urb);
225 static void retire_inbound_urb(struct snd_usb_endpoint *ep,
226 struct snd_urb_ctx *urb_ctx)
228 struct urb *urb = urb_ctx->urb;
229 struct snd_usb_endpoint *sync_sink;
231 if (unlikely(ep->skip_packets > 0)) {
236 sync_sink = READ_ONCE(ep->sync_sink);
238 snd_usb_handle_sync_urb(sync_sink, ep, urb);
240 call_retire_callback(ep, urb);
243 static inline bool has_tx_length_quirk(struct snd_usb_audio *chip)
245 return chip->quirk_flags & QUIRK_FLAG_TX_LENGTH;
248 static void prepare_silent_urb(struct snd_usb_endpoint *ep,
249 struct snd_urb_ctx *ctx)
251 struct urb *urb = ctx->urb;
252 unsigned int offs = 0;
253 unsigned int extra = 0;
254 __le32 packet_length;
257 /* For tx_length_quirk, put packet length at start of packet */
258 if (has_tx_length_quirk(ep->chip))
259 extra = sizeof(packet_length);
261 for (i = 0; i < ctx->packets; ++i) {
266 counts = snd_usb_endpoint_next_packet_size(ep, ctx, i);
267 length = counts * ep->stride; /* number of silent bytes */
268 offset = offs * ep->stride + extra * i;
269 urb->iso_frame_desc[i].offset = offset;
270 urb->iso_frame_desc[i].length = length + extra;
272 packet_length = cpu_to_le32(length);
273 memcpy(urb->transfer_buffer + offset,
274 &packet_length, sizeof(packet_length));
276 memset(urb->transfer_buffer + offset + extra,
277 ep->silence_value, length);
281 urb->number_of_packets = ctx->packets;
282 urb->transfer_buffer_length = offs * ep->stride + ctx->packets * extra;
287 * Prepare a PLAYBACK urb for submission to the bus.
289 static void prepare_outbound_urb(struct snd_usb_endpoint *ep,
290 struct snd_urb_ctx *ctx)
292 struct urb *urb = ctx->urb;
293 unsigned char *cp = urb->transfer_buffer;
294 struct snd_usb_substream *data_subs;
296 urb->dev = ep->chip->dev; /* we need to set this at each time */
299 case SND_USB_ENDPOINT_TYPE_DATA:
300 data_subs = READ_ONCE(ep->data_subs);
301 if (data_subs && ep->prepare_data_urb)
302 ep->prepare_data_urb(data_subs, urb);
303 else /* no data provider, so send silence */
304 prepare_silent_urb(ep, ctx);
307 case SND_USB_ENDPOINT_TYPE_SYNC:
308 if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) {
310 * fill the length and offset of each urb descriptor.
311 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
313 urb->iso_frame_desc[0].length = 4;
314 urb->iso_frame_desc[0].offset = 0;
316 cp[1] = ep->freqn >> 8;
317 cp[2] = ep->freqn >> 16;
318 cp[3] = ep->freqn >> 24;
321 * fill the length and offset of each urb descriptor.
322 * the fixed 10.14 frequency is passed through the pipe.
324 urb->iso_frame_desc[0].length = 3;
325 urb->iso_frame_desc[0].offset = 0;
326 cp[0] = ep->freqn >> 2;
327 cp[1] = ep->freqn >> 10;
328 cp[2] = ep->freqn >> 18;
336 * Prepare a CAPTURE or SYNC urb for submission to the bus.
338 static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep,
339 struct snd_urb_ctx *urb_ctx)
342 struct urb *urb = urb_ctx->urb;
344 urb->dev = ep->chip->dev; /* we need to set this at each time */
347 case SND_USB_ENDPOINT_TYPE_DATA:
349 for (i = 0; i < urb_ctx->packets; i++) {
350 urb->iso_frame_desc[i].offset = offs;
351 urb->iso_frame_desc[i].length = ep->curpacksize;
352 offs += ep->curpacksize;
355 urb->transfer_buffer_length = offs;
356 urb->number_of_packets = urb_ctx->packets;
359 case SND_USB_ENDPOINT_TYPE_SYNC:
360 urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize);
361 urb->iso_frame_desc[0].offset = 0;
366 /* notify an error as XRUN to the assigned PCM data substream */
367 static void notify_xrun(struct snd_usb_endpoint *ep)
369 struct snd_usb_substream *data_subs;
371 data_subs = READ_ONCE(ep->data_subs);
372 if (data_subs && data_subs->pcm_substream)
373 snd_pcm_stop_xrun(data_subs->pcm_substream);
376 static struct snd_usb_packet_info *
377 next_packet_fifo_enqueue(struct snd_usb_endpoint *ep)
379 struct snd_usb_packet_info *p;
381 p = ep->next_packet + (ep->next_packet_head + ep->next_packet_queued) %
382 ARRAY_SIZE(ep->next_packet);
383 ep->next_packet_queued++;
387 static struct snd_usb_packet_info *
388 next_packet_fifo_dequeue(struct snd_usb_endpoint *ep)
390 struct snd_usb_packet_info *p;
392 p = ep->next_packet + ep->next_packet_head;
393 ep->next_packet_head++;
394 ep->next_packet_head %= ARRAY_SIZE(ep->next_packet);
395 ep->next_packet_queued--;
400 * Send output urbs that have been prepared previously. URBs are dequeued
401 * from ep->ready_playback_urbs and in case there aren't any available
402 * or there are no packets that have been prepared, this function does
405 * The reason why the functionality of sending and preparing URBs is separated
406 * is that host controllers don't guarantee the order in which they return
407 * inbound and outbound packets to their submitters.
409 * This function is only used for implicit feedback endpoints. For endpoints
410 * driven by dedicated sync endpoints, URBs are immediately re-submitted
411 * from their completion handler.
413 static void queue_pending_output_urbs(struct snd_usb_endpoint *ep)
415 while (ep_state_running(ep)) {
418 struct snd_usb_packet_info *packet;
419 struct snd_urb_ctx *ctx = NULL;
422 spin_lock_irqsave(&ep->lock, flags);
423 if (ep->next_packet_queued > 0 &&
424 !list_empty(&ep->ready_playback_urbs)) {
425 /* take URB out of FIFO */
426 ctx = list_first_entry(&ep->ready_playback_urbs,
427 struct snd_urb_ctx, ready_list);
428 list_del_init(&ctx->ready_list);
430 packet = next_packet_fifo_dequeue(ep);
432 spin_unlock_irqrestore(&ep->lock, flags);
437 /* copy over the length information */
438 for (i = 0; i < packet->packets; i++)
439 ctx->packet_size[i] = packet->packet_size[i];
441 /* call the data handler to fill in playback data */
442 prepare_outbound_urb(ep, ctx);
444 err = usb_submit_urb(ctx->urb, GFP_ATOMIC);
446 usb_audio_err(ep->chip,
447 "Unable to submit urb #%d: %d at %s\n",
448 ctx->index, err, __func__);
453 set_bit(ctx->index, &ep->active_mask);
458 * complete callback for urbs
460 static void snd_complete_urb(struct urb *urb)
462 struct snd_urb_ctx *ctx = urb->context;
463 struct snd_usb_endpoint *ep = ctx->ep;
467 if (unlikely(urb->status == -ENOENT || /* unlinked */
468 urb->status == -ENODEV || /* device removed */
469 urb->status == -ECONNRESET || /* unlinked */
470 urb->status == -ESHUTDOWN)) /* device disabled */
472 /* device disconnected */
473 if (unlikely(atomic_read(&ep->chip->shutdown)))
476 if (unlikely(!ep_state_running(ep)))
479 if (usb_pipeout(ep->pipe)) {
480 retire_outbound_urb(ep, ctx);
481 /* can be stopped during retire callback */
482 if (unlikely(!ep_state_running(ep)))
485 if (snd_usb_endpoint_implicit_feedback_sink(ep)) {
486 spin_lock_irqsave(&ep->lock, flags);
487 list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
488 clear_bit(ctx->index, &ep->active_mask);
489 spin_unlock_irqrestore(&ep->lock, flags);
490 queue_pending_output_urbs(ep);
494 prepare_outbound_urb(ep, ctx);
495 /* can be stopped during prepare callback */
496 if (unlikely(!ep_state_running(ep)))
499 retire_inbound_urb(ep, ctx);
500 /* can be stopped during retire callback */
501 if (unlikely(!ep_state_running(ep)))
504 prepare_inbound_urb(ep, ctx);
507 err = usb_submit_urb(urb, GFP_ATOMIC);
511 usb_audio_err(ep->chip, "cannot submit urb (err = %d)\n", err);
515 clear_bit(ctx->index, &ep->active_mask);
519 * Find or create a refcount object for the given interface
521 * The objects are released altogether in snd_usb_endpoint_free_all()
523 static struct snd_usb_iface_ref *
524 iface_ref_find(struct snd_usb_audio *chip, int iface)
526 struct snd_usb_iface_ref *ip;
528 list_for_each_entry(ip, &chip->iface_ref_list, list)
529 if (ip->iface == iface)
532 ip = kzalloc(sizeof(*ip), GFP_KERNEL);
536 list_add_tail(&ip->list, &chip->iface_ref_list);
541 * Get the existing endpoint object corresponding EP
542 * Returns NULL if not present.
544 struct snd_usb_endpoint *
545 snd_usb_get_endpoint(struct snd_usb_audio *chip, int ep_num)
547 struct snd_usb_endpoint *ep;
549 list_for_each_entry(ep, &chip->ep_list, list) {
550 if (ep->ep_num == ep_num)
557 #define ep_type_name(type) \
558 (type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync")
561 * snd_usb_add_endpoint: Add an endpoint to an USB audio chip
564 * @ep_num: The number of the endpoint to use
565 * @type: SND_USB_ENDPOINT_TYPE_DATA or SND_USB_ENDPOINT_TYPE_SYNC
567 * If the requested endpoint has not been added to the given chip before,
568 * a new instance is created.
570 * Returns zero on success or a negative error code.
572 * New endpoints will be added to chip->ep_list and freed by
573 * calling snd_usb_endpoint_free_all().
575 * For SND_USB_ENDPOINT_TYPE_SYNC, the caller needs to guarantee that
576 * bNumEndpoints > 1 beforehand.
578 int snd_usb_add_endpoint(struct snd_usb_audio *chip, int ep_num, int type)
580 struct snd_usb_endpoint *ep;
583 ep = snd_usb_get_endpoint(chip, ep_num);
587 usb_audio_dbg(chip, "Creating new %s endpoint #%x\n",
590 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
595 spin_lock_init(&ep->lock);
598 INIT_LIST_HEAD(&ep->ready_playback_urbs);
600 is_playback = ((ep_num & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
601 ep_num &= USB_ENDPOINT_NUMBER_MASK;
603 ep->pipe = usb_sndisocpipe(chip->dev, ep_num);
605 ep->pipe = usb_rcvisocpipe(chip->dev, ep_num);
607 list_add_tail(&ep->list, &chip->ep_list);
611 /* Set up syncinterval and maxsyncsize for a sync EP */
612 static void endpoint_set_syncinterval(struct snd_usb_audio *chip,
613 struct snd_usb_endpoint *ep)
615 struct usb_host_interface *alts;
616 struct usb_endpoint_descriptor *desc;
618 alts = snd_usb_get_host_interface(chip, ep->iface, ep->altsetting);
622 desc = get_endpoint(alts, ep->ep_idx);
623 if (desc->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
624 desc->bRefresh >= 1 && desc->bRefresh <= 9)
625 ep->syncinterval = desc->bRefresh;
626 else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL)
627 ep->syncinterval = 1;
628 else if (desc->bInterval >= 1 && desc->bInterval <= 16)
629 ep->syncinterval = desc->bInterval - 1;
631 ep->syncinterval = 3;
633 ep->syncmaxsize = le16_to_cpu(desc->wMaxPacketSize);
636 static bool endpoint_compatible(struct snd_usb_endpoint *ep,
637 const struct audioformat *fp,
638 const struct snd_pcm_hw_params *params)
642 if (ep->cur_audiofmt != fp)
644 if (ep->cur_rate != params_rate(params) ||
645 ep->cur_format != params_format(params) ||
646 ep->cur_period_frames != params_period_size(params) ||
647 ep->cur_buffer_periods != params_periods(params))
653 * Check whether the given fp and hw params are compatible with the current
654 * setup of the target EP for implicit feedback sync
656 bool snd_usb_endpoint_compatible(struct snd_usb_audio *chip,
657 struct snd_usb_endpoint *ep,
658 const struct audioformat *fp,
659 const struct snd_pcm_hw_params *params)
663 mutex_lock(&chip->mutex);
664 ret = endpoint_compatible(ep, fp, params);
665 mutex_unlock(&chip->mutex);
670 * snd_usb_endpoint_open: Open the endpoint
672 * Called from hw_params to assign the endpoint to the substream.
673 * It's reference-counted, and only the first opener is allowed to set up
674 * arbitrary parameters. The later opener must be compatible with the
675 * former opened parameters.
676 * The endpoint needs to be closed via snd_usb_endpoint_close() later.
678 * Note that this function doesn't configure the endpoint. The substream
679 * needs to set it up later via snd_usb_endpoint_configure().
681 struct snd_usb_endpoint *
682 snd_usb_endpoint_open(struct snd_usb_audio *chip,
683 const struct audioformat *fp,
684 const struct snd_pcm_hw_params *params,
687 struct snd_usb_endpoint *ep;
688 int ep_num = is_sync_ep ? fp->sync_ep : fp->endpoint;
690 mutex_lock(&chip->mutex);
691 ep = snd_usb_get_endpoint(chip, ep_num);
693 usb_audio_err(chip, "Cannot find EP 0x%x to open\n", ep_num);
699 ep->iface = fp->sync_iface;
700 ep->altsetting = fp->sync_altsetting;
701 ep->ep_idx = fp->sync_ep_idx;
703 ep->iface = fp->iface;
704 ep->altsetting = fp->altsetting;
705 ep->ep_idx = fp->ep_idx;
707 usb_audio_dbg(chip, "Open EP 0x%x, iface=%d:%d, idx=%d\n",
708 ep_num, ep->iface, ep->altsetting, ep->ep_idx);
710 ep->iface_ref = iface_ref_find(chip, ep->iface);
711 if (!ep->iface_ref) {
716 ep->cur_audiofmt = fp;
717 ep->cur_channels = fp->channels;
718 ep->cur_rate = params_rate(params);
719 ep->cur_format = params_format(params);
720 ep->cur_frame_bytes = snd_pcm_format_physical_width(ep->cur_format) *
721 ep->cur_channels / 8;
722 ep->cur_period_frames = params_period_size(params);
723 ep->cur_period_bytes = ep->cur_period_frames * ep->cur_frame_bytes;
724 ep->cur_buffer_periods = params_periods(params);
726 if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
727 endpoint_set_syncinterval(chip, ep);
729 ep->implicit_fb_sync = fp->implicit_fb;
730 ep->need_setup = true;
732 usb_audio_dbg(chip, " channels=%d, rate=%d, format=%s, period_bytes=%d, periods=%d, implicit_fb=%d\n",
733 ep->cur_channels, ep->cur_rate,
734 snd_pcm_format_name(ep->cur_format),
735 ep->cur_period_bytes, ep->cur_buffer_periods,
736 ep->implicit_fb_sync);
739 if (WARN_ON(!ep->iface_ref)) {
744 if (!endpoint_compatible(ep, fp, params)) {
745 usb_audio_err(chip, "Incompatible EP setup for 0x%x\n",
751 usb_audio_dbg(chip, "Reopened EP 0x%x (count %d)\n",
755 if (!ep->iface_ref->opened++)
756 ep->iface_ref->need_setup = true;
761 mutex_unlock(&chip->mutex);
766 * snd_usb_endpoint_set_sync: Link data and sync endpoints
768 * Pass NULL to sync_ep to unlink again
770 void snd_usb_endpoint_set_sync(struct snd_usb_audio *chip,
771 struct snd_usb_endpoint *data_ep,
772 struct snd_usb_endpoint *sync_ep)
774 data_ep->sync_source = sync_ep;
778 * Set data endpoint callbacks and the assigned data stream
780 * Called at PCM trigger and cleanups.
781 * Pass NULL to deactivate each callback.
783 void snd_usb_endpoint_set_callback(struct snd_usb_endpoint *ep,
784 void (*prepare)(struct snd_usb_substream *subs,
786 void (*retire)(struct snd_usb_substream *subs,
788 struct snd_usb_substream *data_subs)
790 ep->prepare_data_urb = prepare;
791 ep->retire_data_urb = retire;
792 WRITE_ONCE(ep->data_subs, data_subs);
795 static int endpoint_set_interface(struct snd_usb_audio *chip,
796 struct snd_usb_endpoint *ep,
799 int altset = set ? ep->altsetting : 0;
802 usb_audio_dbg(chip, "Setting usb interface %d:%d for EP 0x%x\n",
803 ep->iface, altset, ep->ep_num);
804 err = usb_set_interface(chip->dev, ep->iface, altset);
806 usb_audio_err(chip, "%d:%d: usb_set_interface failed (%d)\n",
807 ep->iface, altset, err);
811 if (chip->quirk_flags & QUIRK_FLAG_IFACE_DELAY)
817 * snd_usb_endpoint_close: Close the endpoint
819 * Unreference the already opened endpoint via snd_usb_endpoint_open().
821 void snd_usb_endpoint_close(struct snd_usb_audio *chip,
822 struct snd_usb_endpoint *ep)
824 mutex_lock(&chip->mutex);
825 usb_audio_dbg(chip, "Closing EP 0x%x (count %d)\n",
826 ep->ep_num, ep->opened);
828 if (!--ep->iface_ref->opened)
829 endpoint_set_interface(chip, ep, false);
834 ep->cur_audiofmt = NULL;
836 ep->iface_ref = NULL;
837 usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num);
839 mutex_unlock(&chip->mutex);
842 /* Prepare for suspening EP, called from the main suspend handler */
843 void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep)
845 ep->need_setup = true;
847 ep->iface_ref->need_setup = true;
851 * wait until all urbs are processed.
853 static int wait_clear_urbs(struct snd_usb_endpoint *ep)
855 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
858 if (atomic_read(&ep->state) != EP_STATE_STOPPING)
862 alive = bitmap_weight(&ep->active_mask, ep->nurbs);
866 schedule_timeout_uninterruptible(1);
867 } while (time_before(jiffies, end_time));
870 usb_audio_err(ep->chip,
871 "timeout: still %d active urbs on EP #%x\n",
874 if (ep_state_update(ep, EP_STATE_STOPPING, EP_STATE_STOPPED)) {
875 ep->sync_sink = NULL;
876 snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
882 /* sync the pending stop operation;
883 * this function itself doesn't trigger the stop operation
885 void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep)
894 * This function moves the EP to STOPPING state if it's being RUNNING.
896 static int stop_urbs(struct snd_usb_endpoint *ep, bool force)
900 if (!force && atomic_read(&ep->running))
903 if (!ep_state_update(ep, EP_STATE_RUNNING, EP_STATE_STOPPING))
906 INIT_LIST_HEAD(&ep->ready_playback_urbs);
907 ep->next_packet_head = 0;
908 ep->next_packet_queued = 0;
910 for (i = 0; i < ep->nurbs; i++) {
911 if (test_bit(i, &ep->active_mask)) {
912 if (!test_and_set_bit(i, &ep->unlink_mask)) {
913 struct urb *u = ep->urb[i].urb;
923 * release an endpoint's urbs
925 static int release_urbs(struct snd_usb_endpoint *ep, bool force)
929 /* route incoming urbs to nirvana */
930 snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
932 /* stop and unlink urbs */
933 err = stop_urbs(ep, force);
939 for (i = 0; i < ep->nurbs; i++)
940 release_urb_ctx(&ep->urb[i]);
942 usb_free_coherent(ep->chip->dev, SYNC_URBS * 4,
943 ep->syncbuf, ep->sync_dma);
951 * configure a data endpoint
953 static int data_ep_set_params(struct snd_usb_endpoint *ep)
955 struct snd_usb_audio *chip = ep->chip;
956 unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb;
957 unsigned int max_packs_per_period, urbs_per_period, urb_packs;
958 unsigned int max_urbs, i;
959 const struct audioformat *fmt = ep->cur_audiofmt;
960 int frame_bits = ep->cur_frame_bytes * 8;
961 int tx_length_quirk = (has_tx_length_quirk(chip) &&
962 usb_pipeout(ep->pipe));
964 usb_audio_dbg(chip, "Setting params for data EP 0x%x, pipe 0x%x\n",
965 ep->ep_num, ep->pipe);
967 if (ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) {
969 * When operating in DSD DOP mode, the size of a sample frame
970 * in hardware differs from the actual physical format width
971 * because we need to make room for the DOP markers.
973 frame_bits += ep->cur_channels << 3;
976 ep->datainterval = fmt->datainterval;
977 ep->stride = frame_bits >> 3;
979 switch (ep->cur_format) {
980 case SNDRV_PCM_FORMAT_U8:
981 ep->silence_value = 0x80;
983 case SNDRV_PCM_FORMAT_DSD_U8:
984 case SNDRV_PCM_FORMAT_DSD_U16_LE:
985 case SNDRV_PCM_FORMAT_DSD_U32_LE:
986 case SNDRV_PCM_FORMAT_DSD_U16_BE:
987 case SNDRV_PCM_FORMAT_DSD_U32_BE:
988 ep->silence_value = 0x69;
991 ep->silence_value = 0;
994 /* assume max. frequency is 50% higher than nominal */
995 ep->freqmax = ep->freqn + (ep->freqn >> 1);
996 /* Round up freqmax to nearest integer in order to calculate maximum
997 * packet size, which must represent a whole number of frames.
998 * This is accomplished by adding 0x0.ffff before converting the
999 * Q16.16 format into integer.
1000 * In order to accurately calculate the maximum packet size when
1001 * the data interval is more than 1 (i.e. ep->datainterval > 0),
1002 * multiply by the data interval prior to rounding. For instance,
1003 * a freqmax of 41 kHz will result in a max packet size of 6 (5.125)
1004 * frames with a data interval of 1, but 11 (10.25) frames with a
1005 * data interval of 2.
1006 * (ep->freqmax << ep->datainterval overflows at 8.192 MHz for the
1007 * maximum datainterval value of 3, at USB full speed, higher for
1008 * USB high speed, noting that ep->freqmax is in units of
1009 * frames per packet in Q16.16 format.)
1011 maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) *
1013 if (tx_length_quirk)
1014 maxsize += sizeof(__le32); /* Space for length descriptor */
1015 /* but wMaxPacketSize might reduce this */
1016 if (ep->maxpacksize && ep->maxpacksize < maxsize) {
1017 /* whatever fits into a max. size packet */
1018 unsigned int data_maxsize = maxsize = ep->maxpacksize;
1020 if (tx_length_quirk)
1021 /* Need to remove the length descriptor to calc freq */
1022 data_maxsize -= sizeof(__le32);
1023 ep->freqmax = (data_maxsize / (frame_bits >> 3))
1024 << (16 - ep->datainterval);
1028 ep->curpacksize = ep->maxpacksize;
1030 ep->curpacksize = maxsize;
1032 if (snd_usb_get_speed(chip->dev) != USB_SPEED_FULL) {
1033 packs_per_ms = 8 >> ep->datainterval;
1034 max_packs_per_urb = MAX_PACKS_HS;
1037 max_packs_per_urb = MAX_PACKS;
1039 if (ep->sync_source && !ep->implicit_fb_sync)
1040 max_packs_per_urb = min(max_packs_per_urb,
1041 1U << ep->sync_source->syncinterval);
1042 max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval);
1045 * Capture endpoints need to use small URBs because there's no way
1046 * to tell in advance where the next period will end, and we don't
1047 * want the next URB to complete much after the period ends.
1049 * Playback endpoints with implicit sync much use the same parameters
1050 * as their corresponding capture endpoint.
1052 if (usb_pipein(ep->pipe) || ep->implicit_fb_sync) {
1054 urb_packs = packs_per_ms;
1056 * Wireless devices can poll at a max rate of once per 4ms.
1057 * For dataintervals less than 5, increase the packet count to
1058 * allow the host controller to use bursting to fill in the
1061 if (snd_usb_get_speed(chip->dev) == USB_SPEED_WIRELESS) {
1062 int interval = ep->datainterval;
1063 while (interval < 5) {
1068 /* make capture URBs <= 1 ms and smaller than a period */
1069 urb_packs = min(max_packs_per_urb, urb_packs);
1070 while (urb_packs > 1 && urb_packs * maxsize >= ep->cur_period_bytes)
1072 ep->nurbs = MAX_URBS;
1075 * Playback endpoints without implicit sync are adjusted so that
1076 * a period fits as evenly as possible in the smallest number of
1077 * URBs. The total number of URBs is adjusted to the size of the
1078 * ALSA buffer, subject to the MAX_URBS and MAX_QUEUE limits.
1081 /* determine how small a packet can be */
1082 minsize = (ep->freqn >> (16 - ep->datainterval)) *
1084 /* with sync from device, assume it can be 12% lower */
1085 if (ep->sync_source)
1086 minsize -= minsize >> 3;
1087 minsize = max(minsize, 1u);
1089 /* how many packets will contain an entire ALSA period? */
1090 max_packs_per_period = DIV_ROUND_UP(ep->cur_period_bytes, minsize);
1092 /* how many URBs will contain a period? */
1093 urbs_per_period = DIV_ROUND_UP(max_packs_per_period,
1095 /* how many packets are needed in each URB? */
1096 urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period);
1098 /* limit the number of frames in a single URB */
1099 ep->max_urb_frames = DIV_ROUND_UP(ep->cur_period_frames,
1102 /* try to use enough URBs to contain an entire ALSA buffer */
1103 max_urbs = min((unsigned) MAX_URBS,
1104 MAX_QUEUE * packs_per_ms / urb_packs);
1105 ep->nurbs = min(max_urbs, urbs_per_period * ep->cur_buffer_periods);
1108 /* allocate and initialize data urbs */
1109 for (i = 0; i < ep->nurbs; i++) {
1110 struct snd_urb_ctx *u = &ep->urb[i];
1113 u->packets = urb_packs;
1114 u->buffer_size = maxsize * u->packets;
1116 if (fmt->fmt_type == UAC_FORMAT_TYPE_II)
1117 u->packets++; /* for transfer delimiter */
1118 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
1122 u->urb->transfer_buffer =
1123 usb_alloc_coherent(chip->dev, u->buffer_size,
1124 GFP_KERNEL, &u->urb->transfer_dma);
1125 if (!u->urb->transfer_buffer)
1127 u->urb->pipe = ep->pipe;
1128 u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1129 u->urb->interval = 1 << ep->datainterval;
1130 u->urb->context = u;
1131 u->urb->complete = snd_complete_urb;
1132 INIT_LIST_HEAD(&u->ready_list);
1135 /* total buffer bytes of all URBs plus the next queue;
1138 ep->nominal_queue_size = maxsize * urb_packs * (ep->nurbs + 1);
1142 release_urbs(ep, false);
1147 * configure a sync endpoint
1149 static int sync_ep_set_params(struct snd_usb_endpoint *ep)
1151 struct snd_usb_audio *chip = ep->chip;
1154 usb_audio_dbg(chip, "Setting params for sync EP 0x%x, pipe 0x%x\n",
1155 ep->ep_num, ep->pipe);
1157 ep->syncbuf = usb_alloc_coherent(chip->dev, SYNC_URBS * 4,
1158 GFP_KERNEL, &ep->sync_dma);
1162 for (i = 0; i < SYNC_URBS; i++) {
1163 struct snd_urb_ctx *u = &ep->urb[i];
1167 u->urb = usb_alloc_urb(1, GFP_KERNEL);
1170 u->urb->transfer_buffer = ep->syncbuf + i * 4;
1171 u->urb->transfer_dma = ep->sync_dma + i * 4;
1172 u->urb->transfer_buffer_length = 4;
1173 u->urb->pipe = ep->pipe;
1174 u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1175 u->urb->number_of_packets = 1;
1176 u->urb->interval = 1 << ep->syncinterval;
1177 u->urb->context = u;
1178 u->urb->complete = snd_complete_urb;
1181 ep->nurbs = SYNC_URBS;
1186 release_urbs(ep, false);
1191 * snd_usb_endpoint_set_params: configure an snd_usb_endpoint
1193 * Determine the number of URBs to be used on this endpoint.
1194 * An endpoint must be configured before it can be started.
1195 * An endpoint that is already running can not be reconfigured.
1197 static int snd_usb_endpoint_set_params(struct snd_usb_audio *chip,
1198 struct snd_usb_endpoint *ep)
1200 const struct audioformat *fmt = ep->cur_audiofmt;
1203 /* release old buffers, if any */
1204 err = release_urbs(ep, false);
1208 ep->datainterval = fmt->datainterval;
1209 ep->maxpacksize = fmt->maxpacksize;
1210 ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX);
1212 if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) {
1213 ep->freqn = get_usb_full_speed_rate(ep->cur_rate);
1214 ep->pps = 1000 >> ep->datainterval;
1216 ep->freqn = get_usb_high_speed_rate(ep->cur_rate);
1217 ep->pps = 8000 >> ep->datainterval;
1220 ep->sample_rem = ep->cur_rate % ep->pps;
1221 ep->packsize[0] = ep->cur_rate / ep->pps;
1222 ep->packsize[1] = (ep->cur_rate + (ep->pps - 1)) / ep->pps;
1224 /* calculate the frequency in 16.16 format */
1225 ep->freqm = ep->freqn;
1226 ep->freqshift = INT_MIN;
1231 case SND_USB_ENDPOINT_TYPE_DATA:
1232 err = data_ep_set_params(ep);
1234 case SND_USB_ENDPOINT_TYPE_SYNC:
1235 err = sync_ep_set_params(ep);
1241 usb_audio_dbg(chip, "Set up %d URBS, ret=%d\n", ep->nurbs, err);
1246 /* some unit conversions in runtime */
1247 ep->maxframesize = ep->maxpacksize / ep->cur_frame_bytes;
1248 ep->curframesize = ep->curpacksize / ep->cur_frame_bytes;
1254 * snd_usb_endpoint_configure: Configure the endpoint
1256 * This function sets up the EP to be fully usable state.
1257 * It's called either from hw_params or prepare callback.
1258 * The function checks need_setup flag, and performs nothing unless needed,
1259 * so it's safe to call this multiple times.
1261 * This returns zero if unchanged, 1 if the configuration has changed,
1262 * or a negative error code.
1264 int snd_usb_endpoint_configure(struct snd_usb_audio *chip,
1265 struct snd_usb_endpoint *ep)
1270 mutex_lock(&chip->mutex);
1271 if (WARN_ON(!ep->iface_ref))
1273 if (!ep->need_setup)
1276 /* If the interface has been already set up, just set EP parameters */
1277 if (!ep->iface_ref->need_setup) {
1278 /* sample rate setup of UAC1 is per endpoint, and we need
1279 * to update at each EP configuration
1281 if (ep->cur_audiofmt->protocol == UAC_VERSION_1) {
1282 err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt,
1287 err = snd_usb_endpoint_set_params(chip, ep);
1293 /* Need to deselect altsetting at first */
1294 endpoint_set_interface(chip, ep, false);
1296 /* Some UAC1 devices (e.g. Yamaha THR10) need the host interface
1297 * to be set up before parameter setups
1299 iface_first = ep->cur_audiofmt->protocol == UAC_VERSION_1;
1300 /* Workaround for devices that require the interface setup at first like UAC1 */
1301 if (chip->quirk_flags & QUIRK_FLAG_SET_IFACE_FIRST)
1304 err = endpoint_set_interface(chip, ep, true);
1309 err = snd_usb_init_pitch(chip, ep->cur_audiofmt);
1313 err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, ep->cur_rate);
1317 err = snd_usb_endpoint_set_params(chip, ep);
1321 err = snd_usb_select_mode_quirk(chip, ep->cur_audiofmt);
1325 /* for UAC2/3, enable the interface altset here at last */
1327 err = endpoint_set_interface(chip, ep, true);
1332 ep->iface_ref->need_setup = false;
1335 ep->need_setup = false;
1339 mutex_unlock(&chip->mutex);
1344 * snd_usb_endpoint_start: start an snd_usb_endpoint
1346 * @ep: the endpoint to start
1348 * A call to this function will increment the running count of the endpoint.
1349 * In case it is not already running, the URBs for this endpoint will be
1350 * submitted. Otherwise, this function does nothing.
1352 * Must be balanced to calls of snd_usb_endpoint_stop().
1354 * Returns an error if the URB submission failed, 0 in all other cases.
1356 int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)
1361 if (atomic_read(&ep->chip->shutdown))
1364 if (ep->sync_source)
1365 WRITE_ONCE(ep->sync_source->sync_sink, ep);
1367 usb_audio_dbg(ep->chip, "Starting %s EP 0x%x (running %d)\n",
1368 ep_type_name(ep->type), ep->ep_num,
1369 atomic_read(&ep->running));
1371 /* already running? */
1372 if (atomic_inc_return(&ep->running) != 1)
1375 ep->active_mask = 0;
1376 ep->unlink_mask = 0;
1378 ep->sample_accum = 0;
1380 snd_usb_endpoint_start_quirk(ep);
1383 * If this endpoint has a data endpoint as implicit feedback source,
1384 * don't start the urbs here. Instead, mark them all as available,
1385 * wait for the record urbs to return and queue the playback urbs
1386 * from that context.
1389 if (!ep_state_update(ep, EP_STATE_STOPPED, EP_STATE_RUNNING))
1392 if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
1393 !(ep->chip->quirk_flags & QUIRK_FLAG_PLAYBACK_FIRST)) {
1394 for (i = 0; i < ep->nurbs; i++) {
1395 struct snd_urb_ctx *ctx = ep->urb + i;
1396 list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
1399 usb_audio_dbg(ep->chip, "No URB submission due to implicit fb sync\n");
1403 for (i = 0; i < ep->nurbs; i++) {
1404 struct urb *urb = ep->urb[i].urb;
1406 if (snd_BUG_ON(!urb))
1409 if (usb_pipeout(ep->pipe)) {
1410 prepare_outbound_urb(ep, urb->context);
1412 prepare_inbound_urb(ep, urb->context);
1415 err = usb_submit_urb(urb, GFP_ATOMIC);
1417 usb_audio_err(ep->chip,
1418 "cannot submit urb %d, error %d: %s\n",
1419 i, err, usb_error_string(err));
1422 set_bit(i, &ep->active_mask);
1425 usb_audio_dbg(ep->chip, "%d URBs submitted for EP 0x%x\n",
1426 ep->nurbs, ep->ep_num);
1430 snd_usb_endpoint_stop(ep);
1435 * snd_usb_endpoint_stop: stop an snd_usb_endpoint
1437 * @ep: the endpoint to stop (may be NULL)
1439 * A call to this function will decrement the running count of the endpoint.
1440 * In case the last user has requested the endpoint stop, the URBs will
1441 * actually be deactivated.
1443 * Must be balanced to calls of snd_usb_endpoint_start().
1445 * The caller needs to synchronize the pending stop operation via
1446 * snd_usb_endpoint_sync_pending_stop().
1448 void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep)
1453 usb_audio_dbg(ep->chip, "Stopping %s EP 0x%x (running %d)\n",
1454 ep_type_name(ep->type), ep->ep_num,
1455 atomic_read(&ep->running));
1457 if (snd_BUG_ON(!atomic_read(&ep->running)))
1460 if (!atomic_dec_return(&ep->running)) {
1461 if (ep->sync_source)
1462 WRITE_ONCE(ep->sync_source->sync_sink, NULL);
1463 stop_urbs(ep, false);
1468 * snd_usb_endpoint_release: Tear down an snd_usb_endpoint
1470 * @ep: the endpoint to release
1472 * This function does not care for the endpoint's running count but will tear
1473 * down all the streaming URBs immediately.
1475 void snd_usb_endpoint_release(struct snd_usb_endpoint *ep)
1477 release_urbs(ep, true);
1481 * snd_usb_endpoint_free_all: Free the resources of an snd_usb_endpoint
1484 * This free all endpoints and those resources
1486 void snd_usb_endpoint_free_all(struct snd_usb_audio *chip)
1488 struct snd_usb_endpoint *ep, *en;
1489 struct snd_usb_iface_ref *ip, *in;
1491 list_for_each_entry_safe(ep, en, &chip->ep_list, list)
1494 list_for_each_entry_safe(ip, in, &chip->iface_ref_list, list)
1499 * snd_usb_handle_sync_urb: parse an USB sync packet
1501 * @ep: the endpoint to handle the packet
1502 * @sender: the sending endpoint
1503 * @urb: the received packet
1505 * This function is called from the context of an endpoint that received
1506 * the packet and is used to let another endpoint object handle the payload.
1508 static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
1509 struct snd_usb_endpoint *sender,
1510 const struct urb *urb)
1514 unsigned long flags;
1516 snd_BUG_ON(ep == sender);
1519 * In case the endpoint is operating in implicit feedback mode, prepare
1520 * a new outbound URB that has the same layout as the received packet
1521 * and add it to the list of pending urbs. queue_pending_output_urbs()
1522 * will take care of them later.
1524 if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
1525 atomic_read(&ep->running)) {
1527 /* implicit feedback case */
1529 struct snd_urb_ctx *in_ctx;
1530 struct snd_usb_packet_info *out_packet;
1532 in_ctx = urb->context;
1534 /* Count overall packet size */
1535 for (i = 0; i < in_ctx->packets; i++)
1536 if (urb->iso_frame_desc[i].status == 0)
1537 bytes += urb->iso_frame_desc[i].actual_length;
1540 * skip empty packets. At least M-Audio's Fast Track Ultra stops
1541 * streaming once it received a 0-byte OUT URB
1546 spin_lock_irqsave(&ep->lock, flags);
1547 if (ep->next_packet_queued >= ARRAY_SIZE(ep->next_packet)) {
1548 spin_unlock_irqrestore(&ep->lock, flags);
1549 usb_audio_err(ep->chip,
1550 "next package FIFO overflow EP 0x%x\n",
1556 out_packet = next_packet_fifo_enqueue(ep);
1559 * Iterate through the inbound packet and prepare the lengths
1560 * for the output packet. The OUT packet we are about to send
1561 * will have the same amount of payload bytes per stride as the
1562 * IN packet we just received. Since the actual size is scaled
1563 * by the stride, use the sender stride to calculate the length
1564 * in case the number of channels differ between the implicitly
1565 * fed-back endpoint and the synchronizing endpoint.
1568 out_packet->packets = in_ctx->packets;
1569 for (i = 0; i < in_ctx->packets; i++) {
1570 if (urb->iso_frame_desc[i].status == 0)
1571 out_packet->packet_size[i] =
1572 urb->iso_frame_desc[i].actual_length / sender->stride;
1574 out_packet->packet_size[i] = 0;
1577 spin_unlock_irqrestore(&ep->lock, flags);
1578 queue_pending_output_urbs(ep);
1584 * process after playback sync complete
1586 * Full speed devices report feedback values in 10.14 format as samples
1587 * per frame, high speed devices in 16.16 format as samples per
1590 * Because the Audio Class 1 spec was written before USB 2.0, many high
1591 * speed devices use a wrong interpretation, some others use an
1592 * entirely different format.
1594 * Therefore, we cannot predict what format any particular device uses
1595 * and must detect it automatically.
1598 if (urb->iso_frame_desc[0].status != 0 ||
1599 urb->iso_frame_desc[0].actual_length < 3)
1602 f = le32_to_cpup(urb->transfer_buffer);
1603 if (urb->iso_frame_desc[0].actual_length == 3)
1611 if (unlikely(sender->tenor_fb_quirk)) {
1613 * Devices based on Tenor 8802 chipsets (TEAC UD-H01
1614 * and others) sometimes change the feedback value
1617 if (f < ep->freqn - 0x8000)
1619 else if (f > ep->freqn + 0x8000)
1621 } else if (unlikely(ep->freqshift == INT_MIN)) {
1623 * The first time we see a feedback value, determine its format
1624 * by shifting it left or right until it matches the nominal
1625 * frequency value. This assumes that the feedback does not
1626 * differ from the nominal value more than +50% or -25%.
1629 while (f < ep->freqn - ep->freqn / 4) {
1633 while (f > ep->freqn + ep->freqn / 2) {
1637 ep->freqshift = shift;
1638 } else if (ep->freqshift >= 0)
1639 f <<= ep->freqshift;
1641 f >>= -ep->freqshift;
1643 if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) {
1645 * If the frequency looks valid, set it.
1646 * This value is referred to in prepare_playback_urb().
1648 spin_lock_irqsave(&ep->lock, flags);
1650 spin_unlock_irqrestore(&ep->lock, flags);
1653 * Out of range; maybe the shift value is wrong.
1654 * Reset it so that we autodetect again the next time.
1656 ep->freqshift = INT_MIN;