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>
24 #define EP_FLAG_RUNNING 1
25 #define EP_FLAG_STOPPING 2
28 * snd_usb_endpoint is a model that abstracts everything related to an
29 * USB endpoint and its streaming.
31 * There are functions to activate and deactivate the streaming URBs and
32 * optional callbacks to let the pcm logic handle the actual content of the
33 * packets for playback and record. Thus, the bus streaming and the audio
34 * handlers are fully decoupled.
36 * There are two different types of endpoints in audio applications.
38 * SND_USB_ENDPOINT_TYPE_DATA handles full audio data payload for both
39 * inbound and outbound traffic.
41 * SND_USB_ENDPOINT_TYPE_SYNC endpoints are for inbound traffic only and
42 * expect the payload to carry Q10.14 / Q16.16 formatted sync information
45 * Each endpoint has to be configured prior to being used by calling
46 * snd_usb_endpoint_set_params().
48 * The model incorporates a reference counting, so that multiple users
49 * can call snd_usb_endpoint_start() and snd_usb_endpoint_stop(), and
50 * only the first user will effectively start the URBs, and only the last
51 * one to stop it will tear the URBs down again.
55 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
56 * this will overflow at approx 524 kHz
58 static inline unsigned get_usb_full_speed_rate(unsigned int rate)
60 return ((rate << 13) + 62) / 125;
64 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
65 * this will overflow at approx 4 MHz
67 static inline unsigned get_usb_high_speed_rate(unsigned int rate)
69 return ((rate << 10) + 62) / 125;
75 static void release_urb_ctx(struct snd_urb_ctx *u)
78 usb_free_coherent(u->ep->chip->dev, u->buffer_size,
79 u->urb->transfer_buffer,
80 u->urb->transfer_dma);
85 static const char *usb_error_string(int err)
91 return "endpoint not enabled";
93 return "endpoint stalled";
95 return "not enough bandwidth";
97 return "device disabled";
99 return "device suspended";
104 return "internal error";
106 return "unknown error";
111 * snd_usb_endpoint_implicit_feedback_sink: Report endpoint usage type
113 * @ep: The snd_usb_endpoint
115 * Determine whether an endpoint is driven by an implicit feedback
116 * data endpoint source.
118 int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep)
120 return ep->implicit_fb_sync && usb_pipeout(ep->pipe);
124 * Return the number of samples to be sent in the next packet
125 * for streaming based on information derived from sync endpoints
127 * This won't be used for implicit feedback which takes the packet size
128 * returned from the sync source
130 static int slave_next_packet_size(struct snd_usb_endpoint *ep)
136 return ep->maxframesize;
138 spin_lock_irqsave(&ep->lock, flags);
139 ep->phase = (ep->phase & 0xffff)
140 + (ep->freqm << ep->datainterval);
141 ret = min(ep->phase >> 16, ep->maxframesize);
142 spin_unlock_irqrestore(&ep->lock, flags);
148 * Return the number of samples to be sent in the next packet
149 * for adaptive and synchronous endpoints
151 static int next_packet_size(struct snd_usb_endpoint *ep)
156 return ep->maxframesize;
158 ep->sample_accum += ep->sample_rem;
159 if (ep->sample_accum >= ep->pps) {
160 ep->sample_accum -= ep->pps;
161 ret = ep->packsize[1];
163 ret = ep->packsize[0];
170 * snd_usb_endpoint_next_packet_size: Return the number of samples to be sent
173 int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep,
174 struct snd_urb_ctx *ctx, int idx)
176 if (ctx->packet_size[idx])
177 return ctx->packet_size[idx];
178 else if (ep->sync_source)
179 return slave_next_packet_size(ep);
181 return next_packet_size(ep);
184 static void call_retire_callback(struct snd_usb_endpoint *ep,
187 struct snd_usb_substream *data_subs;
189 data_subs = READ_ONCE(ep->data_subs);
190 if (data_subs && ep->retire_data_urb)
191 ep->retire_data_urb(data_subs, urb);
194 static void retire_outbound_urb(struct snd_usb_endpoint *ep,
195 struct snd_urb_ctx *urb_ctx)
197 call_retire_callback(ep, urb_ctx->urb);
200 static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
201 struct snd_usb_endpoint *sender,
202 const struct urb *urb);
204 static void retire_inbound_urb(struct snd_usb_endpoint *ep,
205 struct snd_urb_ctx *urb_ctx)
207 struct urb *urb = urb_ctx->urb;
208 struct snd_usb_endpoint *sync_sink;
210 if (unlikely(ep->skip_packets > 0)) {
215 sync_sink = READ_ONCE(ep->sync_sink);
217 snd_usb_handle_sync_urb(sync_sink, ep, urb);
219 call_retire_callback(ep, urb);
222 static void prepare_silent_urb(struct snd_usb_endpoint *ep,
223 struct snd_urb_ctx *ctx)
225 struct urb *urb = ctx->urb;
226 unsigned int offs = 0;
227 unsigned int extra = 0;
228 __le32 packet_length;
231 /* For tx_length_quirk, put packet length at start of packet */
232 if (ep->chip->tx_length_quirk)
233 extra = sizeof(packet_length);
235 for (i = 0; i < ctx->packets; ++i) {
240 counts = snd_usb_endpoint_next_packet_size(ep, ctx, i);
241 length = counts * ep->stride; /* number of silent bytes */
242 offset = offs * ep->stride + extra * i;
243 urb->iso_frame_desc[i].offset = offset;
244 urb->iso_frame_desc[i].length = length + extra;
246 packet_length = cpu_to_le32(length);
247 memcpy(urb->transfer_buffer + offset,
248 &packet_length, sizeof(packet_length));
250 memset(urb->transfer_buffer + offset + extra,
251 ep->silence_value, length);
255 urb->number_of_packets = ctx->packets;
256 urb->transfer_buffer_length = offs * ep->stride + ctx->packets * extra;
260 * Prepare a PLAYBACK urb for submission to the bus.
262 static void prepare_outbound_urb(struct snd_usb_endpoint *ep,
263 struct snd_urb_ctx *ctx)
265 struct urb *urb = ctx->urb;
266 unsigned char *cp = urb->transfer_buffer;
267 struct snd_usb_substream *data_subs;
269 urb->dev = ep->chip->dev; /* we need to set this at each time */
272 case SND_USB_ENDPOINT_TYPE_DATA:
273 data_subs = READ_ONCE(ep->data_subs);
274 if (data_subs && ep->prepare_data_urb)
275 ep->prepare_data_urb(data_subs, urb);
276 else /* no data provider, so send silence */
277 prepare_silent_urb(ep, ctx);
280 case SND_USB_ENDPOINT_TYPE_SYNC:
281 if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) {
283 * fill the length and offset of each urb descriptor.
284 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
286 urb->iso_frame_desc[0].length = 4;
287 urb->iso_frame_desc[0].offset = 0;
289 cp[1] = ep->freqn >> 8;
290 cp[2] = ep->freqn >> 16;
291 cp[3] = ep->freqn >> 24;
294 * fill the length and offset of each urb descriptor.
295 * the fixed 10.14 frequency is passed through the pipe.
297 urb->iso_frame_desc[0].length = 3;
298 urb->iso_frame_desc[0].offset = 0;
299 cp[0] = ep->freqn >> 2;
300 cp[1] = ep->freqn >> 10;
301 cp[2] = ep->freqn >> 18;
309 * Prepare a CAPTURE or SYNC urb for submission to the bus.
311 static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep,
312 struct snd_urb_ctx *urb_ctx)
315 struct urb *urb = urb_ctx->urb;
317 urb->dev = ep->chip->dev; /* we need to set this at each time */
320 case SND_USB_ENDPOINT_TYPE_DATA:
322 for (i = 0; i < urb_ctx->packets; i++) {
323 urb->iso_frame_desc[i].offset = offs;
324 urb->iso_frame_desc[i].length = ep->curpacksize;
325 offs += ep->curpacksize;
328 urb->transfer_buffer_length = offs;
329 urb->number_of_packets = urb_ctx->packets;
332 case SND_USB_ENDPOINT_TYPE_SYNC:
333 urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize);
334 urb->iso_frame_desc[0].offset = 0;
339 /* notify an error as XRUN to the assigned PCM data substream */
340 static void notify_xrun(struct snd_usb_endpoint *ep)
342 struct snd_usb_substream *data_subs;
344 data_subs = READ_ONCE(ep->data_subs);
345 if (data_subs && data_subs->pcm_substream)
346 snd_pcm_stop_xrun(data_subs->pcm_substream);
349 static struct snd_usb_packet_info *
350 next_packet_fifo_enqueue(struct snd_usb_endpoint *ep)
352 struct snd_usb_packet_info *p;
354 p = ep->next_packet + (ep->next_packet_head + ep->next_packet_queued) %
355 ARRAY_SIZE(ep->next_packet);
356 ep->next_packet_queued++;
360 static struct snd_usb_packet_info *
361 next_packet_fifo_dequeue(struct snd_usb_endpoint *ep)
363 struct snd_usb_packet_info *p;
365 p = ep->next_packet + ep->next_packet_head;
366 ep->next_packet_head++;
367 ep->next_packet_head %= ARRAY_SIZE(ep->next_packet);
368 ep->next_packet_queued--;
373 * Send output urbs that have been prepared previously. URBs are dequeued
374 * from ep->ready_playback_urbs and in case there aren't any available
375 * or there are no packets that have been prepared, this function does
378 * The reason why the functionality of sending and preparing URBs is separated
379 * is that host controllers don't guarantee the order in which they return
380 * inbound and outbound packets to their submitters.
382 * This function is only used for implicit feedback endpoints. For endpoints
383 * driven by dedicated sync endpoints, URBs are immediately re-submitted
384 * from their completion handler.
386 static void queue_pending_output_urbs(struct snd_usb_endpoint *ep)
388 while (test_bit(EP_FLAG_RUNNING, &ep->flags)) {
391 struct snd_usb_packet_info *packet;
392 struct snd_urb_ctx *ctx = NULL;
395 spin_lock_irqsave(&ep->lock, flags);
396 if (ep->next_packet_queued > 0 &&
397 !list_empty(&ep->ready_playback_urbs)) {
398 /* take URB out of FIFO */
399 ctx = list_first_entry(&ep->ready_playback_urbs,
400 struct snd_urb_ctx, ready_list);
401 list_del_init(&ctx->ready_list);
403 packet = next_packet_fifo_dequeue(ep);
405 spin_unlock_irqrestore(&ep->lock, flags);
410 /* copy over the length information */
411 for (i = 0; i < packet->packets; i++)
412 ctx->packet_size[i] = packet->packet_size[i];
414 /* call the data handler to fill in playback data */
415 prepare_outbound_urb(ep, ctx);
417 err = usb_submit_urb(ctx->urb, GFP_ATOMIC);
419 usb_audio_err(ep->chip,
420 "Unable to submit urb #%d: %d at %s\n",
421 ctx->index, err, __func__);
426 set_bit(ctx->index, &ep->active_mask);
431 * complete callback for urbs
433 static void snd_complete_urb(struct urb *urb)
435 struct snd_urb_ctx *ctx = urb->context;
436 struct snd_usb_endpoint *ep = ctx->ep;
440 if (unlikely(urb->status == -ENOENT || /* unlinked */
441 urb->status == -ENODEV || /* device removed */
442 urb->status == -ECONNRESET || /* unlinked */
443 urb->status == -ESHUTDOWN)) /* device disabled */
445 /* device disconnected */
446 if (unlikely(atomic_read(&ep->chip->shutdown)))
449 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
452 if (usb_pipeout(ep->pipe)) {
453 retire_outbound_urb(ep, ctx);
454 /* can be stopped during retire callback */
455 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
458 if (snd_usb_endpoint_implicit_feedback_sink(ep)) {
459 spin_lock_irqsave(&ep->lock, flags);
460 list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
461 clear_bit(ctx->index, &ep->active_mask);
462 spin_unlock_irqrestore(&ep->lock, flags);
463 queue_pending_output_urbs(ep);
467 prepare_outbound_urb(ep, ctx);
468 /* can be stopped during prepare callback */
469 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
472 retire_inbound_urb(ep, ctx);
473 /* can be stopped during retire callback */
474 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
477 prepare_inbound_urb(ep, ctx);
480 err = usb_submit_urb(urb, GFP_ATOMIC);
484 usb_audio_err(ep->chip, "cannot submit urb (err = %d)\n", err);
488 clear_bit(ctx->index, &ep->active_mask);
492 * Get the existing endpoint object corresponding EP
493 * Returns NULL if not present.
495 struct snd_usb_endpoint *
496 snd_usb_get_endpoint(struct snd_usb_audio *chip, int ep_num)
498 struct snd_usb_endpoint *ep;
500 list_for_each_entry(ep, &chip->ep_list, list) {
501 if (ep->ep_num == ep_num)
508 #define ep_type_name(type) \
509 (type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync")
512 * snd_usb_add_endpoint: Add an endpoint to an USB audio chip
515 * @ep_num: The number of the endpoint to use
516 * @type: SND_USB_ENDPOINT_TYPE_DATA or SND_USB_ENDPOINT_TYPE_SYNC
518 * If the requested endpoint has not been added to the given chip before,
519 * a new instance is created.
521 * Returns zero on success or a negative error code.
523 * New endpoints will be added to chip->ep_list and must be freed by
524 * calling snd_usb_endpoint_free().
526 * For SND_USB_ENDPOINT_TYPE_SYNC, the caller needs to guarantee that
527 * bNumEndpoints > 1 beforehand.
529 int snd_usb_add_endpoint(struct snd_usb_audio *chip, int ep_num, int type)
531 struct snd_usb_endpoint *ep;
534 ep = snd_usb_get_endpoint(chip, ep_num);
538 usb_audio_dbg(chip, "Creating new %s endpoint #%x\n",
541 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
546 spin_lock_init(&ep->lock);
549 INIT_LIST_HEAD(&ep->ready_playback_urbs);
551 is_playback = ((ep_num & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
552 ep_num &= USB_ENDPOINT_NUMBER_MASK;
554 ep->pipe = usb_sndisocpipe(chip->dev, ep_num);
556 ep->pipe = usb_rcvisocpipe(chip->dev, ep_num);
558 list_add_tail(&ep->list, &chip->ep_list);
562 /* Set up syncinterval and maxsyncsize for a sync EP */
563 static void endpoint_set_syncinterval(struct snd_usb_audio *chip,
564 struct snd_usb_endpoint *ep)
566 struct usb_host_interface *alts;
567 struct usb_endpoint_descriptor *desc;
569 alts = snd_usb_get_host_interface(chip, ep->iface, ep->altsetting);
573 desc = get_endpoint(alts, ep->ep_idx);
574 if (desc->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
575 desc->bRefresh >= 1 && desc->bRefresh <= 9)
576 ep->syncinterval = desc->bRefresh;
577 else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL)
578 ep->syncinterval = 1;
579 else if (desc->bInterval >= 1 && desc->bInterval <= 16)
580 ep->syncinterval = desc->bInterval - 1;
582 ep->syncinterval = 3;
584 ep->syncmaxsize = le16_to_cpu(desc->wMaxPacketSize);
587 static bool endpoint_compatible(struct snd_usb_endpoint *ep,
588 const struct audioformat *fp,
589 const struct snd_pcm_hw_params *params)
593 if (ep->cur_audiofmt != fp)
595 if (ep->cur_rate != params_rate(params) ||
596 ep->cur_format != params_format(params) ||
597 ep->cur_period_frames != params_period_size(params) ||
598 ep->cur_buffer_periods != params_periods(params))
604 * Check whether the given fp and hw params are compatbile with the current
605 * setup of the target EP for implicit feedback sync
607 bool snd_usb_endpoint_compatible(struct snd_usb_audio *chip,
608 struct snd_usb_endpoint *ep,
609 const struct audioformat *fp,
610 const struct snd_pcm_hw_params *params)
614 mutex_lock(&chip->mutex);
615 ret = endpoint_compatible(ep, fp, params);
616 mutex_unlock(&chip->mutex);
621 * snd_usb_endpoint_open: Open the endpoint
623 * Called from hw_params to assign the endpoint to the substream.
624 * It's reference-counted, and only the first opener is allowed to set up
625 * arbitrary parameters. The later opener must be compatible with the
626 * former opened parameters.
627 * The endpoint needs to be closed via snd_usb_endpoint_close() later.
629 * Note that this function doesn't configure the endpoint. The substream
630 * needs to set it up later via snd_usb_endpoint_configure().
632 struct snd_usb_endpoint *
633 snd_usb_endpoint_open(struct snd_usb_audio *chip,
634 const struct audioformat *fp,
635 const struct snd_pcm_hw_params *params,
638 struct snd_usb_endpoint *ep;
639 int ep_num = is_sync_ep ? fp->sync_ep : fp->endpoint;
641 mutex_lock(&chip->mutex);
642 ep = snd_usb_get_endpoint(chip, ep_num);
644 usb_audio_err(chip, "Cannot find EP 0x%x to open\n", ep_num);
650 ep->iface = fp->sync_iface;
651 ep->altsetting = fp->sync_altsetting;
652 ep->ep_idx = fp->sync_ep_idx;
654 ep->iface = fp->iface;
655 ep->altsetting = fp->altsetting;
658 usb_audio_dbg(chip, "Open EP 0x%x, iface=%d:%d, idx=%d\n",
659 ep_num, ep->iface, ep->altsetting, ep->ep_idx);
661 ep->cur_audiofmt = fp;
662 ep->cur_channels = fp->channels;
663 ep->cur_rate = params_rate(params);
664 ep->cur_format = params_format(params);
665 ep->cur_frame_bytes = snd_pcm_format_physical_width(ep->cur_format) *
666 ep->cur_channels / 8;
667 ep->cur_period_frames = params_period_size(params);
668 ep->cur_period_bytes = ep->cur_period_frames * ep->cur_frame_bytes;
669 ep->cur_buffer_periods = params_periods(params);
671 if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
672 endpoint_set_syncinterval(chip, ep);
674 ep->implicit_fb_sync = fp->implicit_fb;
675 ep->need_setup = true;
677 usb_audio_dbg(chip, " channels=%d, rate=%d, format=%s, period_bytes=%d, periods=%d, implicit_fb=%d\n",
678 ep->cur_channels, ep->cur_rate,
679 snd_pcm_format_name(ep->cur_format),
680 ep->cur_period_bytes, ep->cur_buffer_periods,
681 ep->implicit_fb_sync);
684 if (!endpoint_compatible(ep, fp, params)) {
685 usb_audio_err(chip, "Incompatible EP setup for 0x%x\n",
691 usb_audio_dbg(chip, "Reopened EP 0x%x (count %d)\n",
698 mutex_unlock(&chip->mutex);
703 * snd_usb_endpoint_set_sync: Link data and sync endpoints
705 * Pass NULL to sync_ep to unlink again
707 void snd_usb_endpoint_set_sync(struct snd_usb_audio *chip,
708 struct snd_usb_endpoint *data_ep,
709 struct snd_usb_endpoint *sync_ep)
711 data_ep->sync_source = sync_ep;
715 * Set data endpoint callbacks and the assigned data stream
717 * Called at PCM trigger and cleanups.
718 * Pass NULL to deactivate each callback.
720 void snd_usb_endpoint_set_callback(struct snd_usb_endpoint *ep,
721 void (*prepare)(struct snd_usb_substream *subs,
723 void (*retire)(struct snd_usb_substream *subs,
725 struct snd_usb_substream *data_subs)
727 ep->prepare_data_urb = prepare;
728 ep->retire_data_urb = retire;
729 WRITE_ONCE(ep->data_subs, data_subs);
732 static int endpoint_set_interface(struct snd_usb_audio *chip,
733 struct snd_usb_endpoint *ep,
736 int altset = set ? ep->altsetting : 0;
739 usb_audio_dbg(chip, "Setting usb interface %d:%d for EP 0x%x\n",
740 ep->iface, altset, ep->ep_num);
741 err = usb_set_interface(chip->dev, ep->iface, altset);
743 usb_audio_err(chip, "%d:%d: usb_set_interface failed (%d)\n",
744 ep->iface, altset, err);
748 snd_usb_set_interface_quirk(chip);
753 * snd_usb_endpoint_close: Close the endpoint
755 * Unreference the already opened endpoint via snd_usb_endpoint_open().
757 void snd_usb_endpoint_close(struct snd_usb_audio *chip,
758 struct snd_usb_endpoint *ep)
760 mutex_lock(&chip->mutex);
761 usb_audio_dbg(chip, "Closing EP 0x%x (count %d)\n",
762 ep->ep_num, ep->opened);
764 endpoint_set_interface(chip, ep, false);
767 ep->cur_audiofmt = NULL;
769 usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num);
771 mutex_unlock(&chip->mutex);
774 /* Prepare for suspening EP, called from the main suspend handler */
775 void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep)
777 ep->need_setup = true;
781 * wait until all urbs are processed.
783 static int wait_clear_urbs(struct snd_usb_endpoint *ep)
785 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
788 if (!test_bit(EP_FLAG_STOPPING, &ep->flags))
792 alive = bitmap_weight(&ep->active_mask, ep->nurbs);
796 schedule_timeout_uninterruptible(1);
797 } while (time_before(jiffies, end_time));
800 usb_audio_err(ep->chip,
801 "timeout: still %d active urbs on EP #%x\n",
803 clear_bit(EP_FLAG_STOPPING, &ep->flags);
805 ep->sync_sink = NULL;
806 snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
811 /* sync the pending stop operation;
812 * this function itself doesn't trigger the stop operation
814 void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep)
821 * Stop and unlink active urbs.
823 * This function checks and clears EP_FLAG_RUNNING state.
824 * When @wait_sync is set, it waits until all pending URBs are killed.
826 static int stop_and_unlink_urbs(struct snd_usb_endpoint *ep, bool force,
831 if (!force && atomic_read(&ep->chip->shutdown)) /* to be sure... */
834 if (atomic_read(&ep->running))
837 if (!test_and_clear_bit(EP_FLAG_RUNNING, &ep->flags))
840 set_bit(EP_FLAG_STOPPING, &ep->flags);
841 INIT_LIST_HEAD(&ep->ready_playback_urbs);
842 ep->next_packet_head = 0;
843 ep->next_packet_queued = 0;
845 for (i = 0; i < ep->nurbs; i++) {
846 if (test_bit(i, &ep->active_mask)) {
847 if (!test_and_set_bit(i, &ep->unlink_mask)) {
848 struct urb *u = ep->urb[i].urb;
856 return wait_clear_urbs(ep);
861 * release an endpoint's urbs
863 static void release_urbs(struct snd_usb_endpoint *ep, int force)
867 /* route incoming urbs to nirvana */
868 snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
871 stop_and_unlink_urbs(ep, force, true);
873 for (i = 0; i < ep->nurbs; i++)
874 release_urb_ctx(&ep->urb[i]);
876 usb_free_coherent(ep->chip->dev, SYNC_URBS * 4,
877 ep->syncbuf, ep->sync_dma);
884 * configure a data endpoint
886 static int data_ep_set_params(struct snd_usb_endpoint *ep)
888 struct snd_usb_audio *chip = ep->chip;
889 unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb;
890 unsigned int max_packs_per_period, urbs_per_period, urb_packs;
891 unsigned int max_urbs, i;
892 const struct audioformat *fmt = ep->cur_audiofmt;
893 int frame_bits = ep->cur_frame_bytes * 8;
894 int tx_length_quirk = (chip->tx_length_quirk &&
895 usb_pipeout(ep->pipe));
897 usb_audio_dbg(chip, "Setting params for data EP 0x%x, pipe 0x%x\n",
898 ep->ep_num, ep->pipe);
900 if (ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) {
902 * When operating in DSD DOP mode, the size of a sample frame
903 * in hardware differs from the actual physical format width
904 * because we need to make room for the DOP markers.
906 frame_bits += ep->cur_channels << 3;
909 ep->datainterval = fmt->datainterval;
910 ep->stride = frame_bits >> 3;
912 switch (ep->cur_format) {
913 case SNDRV_PCM_FORMAT_U8:
914 ep->silence_value = 0x80;
916 case SNDRV_PCM_FORMAT_DSD_U8:
917 case SNDRV_PCM_FORMAT_DSD_U16_LE:
918 case SNDRV_PCM_FORMAT_DSD_U32_LE:
919 case SNDRV_PCM_FORMAT_DSD_U16_BE:
920 case SNDRV_PCM_FORMAT_DSD_U32_BE:
921 ep->silence_value = 0x69;
924 ep->silence_value = 0;
927 /* assume max. frequency is 50% higher than nominal */
928 ep->freqmax = ep->freqn + (ep->freqn >> 1);
929 /* Round up freqmax to nearest integer in order to calculate maximum
930 * packet size, which must represent a whole number of frames.
931 * This is accomplished by adding 0x0.ffff before converting the
932 * Q16.16 format into integer.
933 * In order to accurately calculate the maximum packet size when
934 * the data interval is more than 1 (i.e. ep->datainterval > 0),
935 * multiply by the data interval prior to rounding. For instance,
936 * a freqmax of 41 kHz will result in a max packet size of 6 (5.125)
937 * frames with a data interval of 1, but 11 (10.25) frames with a
938 * data interval of 2.
939 * (ep->freqmax << ep->datainterval overflows at 8.192 MHz for the
940 * maximum datainterval value of 3, at USB full speed, higher for
941 * USB high speed, noting that ep->freqmax is in units of
942 * frames per packet in Q16.16 format.)
944 maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) *
947 maxsize += sizeof(__le32); /* Space for length descriptor */
948 /* but wMaxPacketSize might reduce this */
949 if (ep->maxpacksize && ep->maxpacksize < maxsize) {
950 /* whatever fits into a max. size packet */
951 unsigned int data_maxsize = maxsize = ep->maxpacksize;
954 /* Need to remove the length descriptor to calc freq */
955 data_maxsize -= sizeof(__le32);
956 ep->freqmax = (data_maxsize / (frame_bits >> 3))
957 << (16 - ep->datainterval);
961 ep->curpacksize = ep->maxpacksize;
963 ep->curpacksize = maxsize;
965 if (snd_usb_get_speed(chip->dev) != USB_SPEED_FULL) {
966 packs_per_ms = 8 >> ep->datainterval;
967 max_packs_per_urb = MAX_PACKS_HS;
970 max_packs_per_urb = MAX_PACKS;
972 if (ep->sync_source && !ep->implicit_fb_sync)
973 max_packs_per_urb = min(max_packs_per_urb,
974 1U << ep->sync_source->syncinterval);
975 max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval);
978 * Capture endpoints need to use small URBs because there's no way
979 * to tell in advance where the next period will end, and we don't
980 * want the next URB to complete much after the period ends.
982 * Playback endpoints with implicit sync much use the same parameters
983 * as their corresponding capture endpoint.
985 if (usb_pipein(ep->pipe) || ep->implicit_fb_sync) {
987 urb_packs = packs_per_ms;
989 * Wireless devices can poll at a max rate of once per 4ms.
990 * For dataintervals less than 5, increase the packet count to
991 * allow the host controller to use bursting to fill in the
994 if (snd_usb_get_speed(chip->dev) == USB_SPEED_WIRELESS) {
995 int interval = ep->datainterval;
996 while (interval < 5) {
1001 /* make capture URBs <= 1 ms and smaller than a period */
1002 urb_packs = min(max_packs_per_urb, urb_packs);
1003 while (urb_packs > 1 && urb_packs * maxsize >= ep->cur_period_bytes)
1005 ep->nurbs = MAX_URBS;
1008 * Playback endpoints without implicit sync are adjusted so that
1009 * a period fits as evenly as possible in the smallest number of
1010 * URBs. The total number of URBs is adjusted to the size of the
1011 * ALSA buffer, subject to the MAX_URBS and MAX_QUEUE limits.
1014 /* determine how small a packet can be */
1015 minsize = (ep->freqn >> (16 - ep->datainterval)) *
1017 /* with sync from device, assume it can be 12% lower */
1018 if (ep->sync_source)
1019 minsize -= minsize >> 3;
1020 minsize = max(minsize, 1u);
1022 /* how many packets will contain an entire ALSA period? */
1023 max_packs_per_period = DIV_ROUND_UP(ep->cur_period_bytes, minsize);
1025 /* how many URBs will contain a period? */
1026 urbs_per_period = DIV_ROUND_UP(max_packs_per_period,
1028 /* how many packets are needed in each URB? */
1029 urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period);
1031 /* limit the number of frames in a single URB */
1032 ep->max_urb_frames = DIV_ROUND_UP(ep->cur_period_frames,
1035 /* try to use enough URBs to contain an entire ALSA buffer */
1036 max_urbs = min((unsigned) MAX_URBS,
1037 MAX_QUEUE * packs_per_ms / urb_packs);
1038 ep->nurbs = min(max_urbs, urbs_per_period * ep->cur_buffer_periods);
1041 /* allocate and initialize data urbs */
1042 for (i = 0; i < ep->nurbs; i++) {
1043 struct snd_urb_ctx *u = &ep->urb[i];
1046 u->packets = urb_packs;
1047 u->buffer_size = maxsize * u->packets;
1049 if (fmt->fmt_type == UAC_FORMAT_TYPE_II)
1050 u->packets++; /* for transfer delimiter */
1051 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
1055 u->urb->transfer_buffer =
1056 usb_alloc_coherent(chip->dev, u->buffer_size,
1057 GFP_KERNEL, &u->urb->transfer_dma);
1058 if (!u->urb->transfer_buffer)
1060 u->urb->pipe = ep->pipe;
1061 u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1062 u->urb->interval = 1 << ep->datainterval;
1063 u->urb->context = u;
1064 u->urb->complete = snd_complete_urb;
1065 INIT_LIST_HEAD(&u->ready_list);
1071 release_urbs(ep, 0);
1076 * configure a sync endpoint
1078 static int sync_ep_set_params(struct snd_usb_endpoint *ep)
1080 struct snd_usb_audio *chip = ep->chip;
1083 usb_audio_dbg(chip, "Setting params for sync EP 0x%x, pipe 0x%x\n",
1084 ep->ep_num, ep->pipe);
1086 ep->syncbuf = usb_alloc_coherent(chip->dev, SYNC_URBS * 4,
1087 GFP_KERNEL, &ep->sync_dma);
1091 for (i = 0; i < SYNC_URBS; i++) {
1092 struct snd_urb_ctx *u = &ep->urb[i];
1096 u->urb = usb_alloc_urb(1, GFP_KERNEL);
1099 u->urb->transfer_buffer = ep->syncbuf + i * 4;
1100 u->urb->transfer_dma = ep->sync_dma + i * 4;
1101 u->urb->transfer_buffer_length = 4;
1102 u->urb->pipe = ep->pipe;
1103 u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1104 u->urb->number_of_packets = 1;
1105 u->urb->interval = 1 << ep->syncinterval;
1106 u->urb->context = u;
1107 u->urb->complete = snd_complete_urb;
1110 ep->nurbs = SYNC_URBS;
1115 release_urbs(ep, 0);
1120 * snd_usb_endpoint_set_params: configure an snd_usb_endpoint
1122 * Determine the number of URBs to be used on this endpoint.
1123 * An endpoint must be configured before it can be started.
1124 * An endpoint that is already running can not be reconfigured.
1126 static int snd_usb_endpoint_set_params(struct snd_usb_audio *chip,
1127 struct snd_usb_endpoint *ep)
1129 const struct audioformat *fmt = ep->cur_audiofmt;
1132 /* release old buffers, if any */
1133 release_urbs(ep, 0);
1135 ep->datainterval = fmt->datainterval;
1136 ep->maxpacksize = fmt->maxpacksize;
1137 ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX);
1139 if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) {
1140 ep->freqn = get_usb_full_speed_rate(ep->cur_rate);
1141 ep->pps = 1000 >> ep->datainterval;
1143 ep->freqn = get_usb_high_speed_rate(ep->cur_rate);
1144 ep->pps = 8000 >> ep->datainterval;
1147 ep->sample_rem = ep->cur_rate % ep->pps;
1148 ep->packsize[0] = ep->cur_rate / ep->pps;
1149 ep->packsize[1] = (ep->cur_rate + (ep->pps - 1)) / ep->pps;
1151 /* calculate the frequency in 16.16 format */
1152 ep->freqm = ep->freqn;
1153 ep->freqshift = INT_MIN;
1158 case SND_USB_ENDPOINT_TYPE_DATA:
1159 err = data_ep_set_params(ep);
1161 case SND_USB_ENDPOINT_TYPE_SYNC:
1162 err = sync_ep_set_params(ep);
1168 usb_audio_dbg(chip, "Set up %d URBS, ret=%d\n", ep->nurbs, err);
1173 /* some unit conversions in runtime */
1174 ep->maxframesize = ep->maxpacksize / ep->cur_frame_bytes;
1175 ep->curframesize = ep->curpacksize / ep->cur_frame_bytes;
1181 * snd_usb_endpoint_configure: Configure the endpoint
1183 * This function sets up the EP to be fully usable state.
1184 * It's called either from hw_params or prepare callback.
1185 * The function checks need_setup flag, and perfoms nothing unless needed,
1186 * so it's safe to call this multiple times.
1188 * This returns zero if unchanged, 1 if the configuration has changed,
1189 * or a negative error code.
1191 int snd_usb_endpoint_configure(struct snd_usb_audio *chip,
1192 struct snd_usb_endpoint *ep)
1197 mutex_lock(&chip->mutex);
1198 if (!ep->need_setup)
1201 /* No need to (re-)configure the sync EP belonging to the same altset */
1203 err = snd_usb_endpoint_set_params(chip, ep);
1209 /* Need to deselect altsetting at first */
1210 endpoint_set_interface(chip, ep, false);
1212 /* Some UAC1 devices (e.g. Yamaha THR10) need the host interface
1213 * to be set up before parameter setups
1215 iface_first = ep->cur_audiofmt->protocol == UAC_VERSION_1;
1217 err = endpoint_set_interface(chip, ep, true);
1222 err = snd_usb_init_pitch(chip, ep->cur_audiofmt);
1226 err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, ep->cur_rate);
1230 err = snd_usb_endpoint_set_params(chip, ep);
1234 err = snd_usb_select_mode_quirk(chip, ep->cur_audiofmt);
1238 /* for UAC2/3, enable the interface altset here at last */
1240 err = endpoint_set_interface(chip, ep, true);
1246 ep->need_setup = false;
1250 mutex_unlock(&chip->mutex);
1255 * snd_usb_endpoint_start: start an snd_usb_endpoint
1257 * @ep: the endpoint to start
1259 * A call to this function will increment the running count of the endpoint.
1260 * In case it is not already running, the URBs for this endpoint will be
1261 * submitted. Otherwise, this function does nothing.
1263 * Must be balanced to calls of snd_usb_endpoint_stop().
1265 * Returns an error if the URB submission failed, 0 in all other cases.
1267 int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)
1272 if (atomic_read(&ep->chip->shutdown))
1275 if (ep->sync_source)
1276 WRITE_ONCE(ep->sync_source->sync_sink, ep);
1278 usb_audio_dbg(ep->chip, "Starting %s EP 0x%x (running %d)\n",
1279 ep_type_name(ep->type), ep->ep_num,
1280 atomic_read(&ep->running));
1282 /* already running? */
1283 if (atomic_inc_return(&ep->running) != 1)
1286 ep->active_mask = 0;
1287 ep->unlink_mask = 0;
1289 ep->sample_accum = 0;
1291 snd_usb_endpoint_start_quirk(ep);
1294 * If this endpoint has a data endpoint as implicit feedback source,
1295 * don't start the urbs here. Instead, mark them all as available,
1296 * wait for the record urbs to return and queue the playback urbs
1297 * from that context.
1300 set_bit(EP_FLAG_RUNNING, &ep->flags);
1302 if (snd_usb_endpoint_implicit_feedback_sink(ep)) {
1303 for (i = 0; i < ep->nurbs; i++) {
1304 struct snd_urb_ctx *ctx = ep->urb + i;
1305 list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
1308 usb_audio_dbg(ep->chip, "No URB submission due to implicit fb sync\n");
1312 for (i = 0; i < ep->nurbs; i++) {
1313 struct urb *urb = ep->urb[i].urb;
1315 if (snd_BUG_ON(!urb))
1318 if (usb_pipeout(ep->pipe)) {
1319 prepare_outbound_urb(ep, urb->context);
1321 prepare_inbound_urb(ep, urb->context);
1324 err = usb_submit_urb(urb, GFP_ATOMIC);
1326 usb_audio_err(ep->chip,
1327 "cannot submit urb %d, error %d: %s\n",
1328 i, err, usb_error_string(err));
1331 set_bit(i, &ep->active_mask);
1334 usb_audio_dbg(ep->chip, "%d URBs submitted for EP 0x%x\n",
1335 ep->nurbs, ep->ep_num);
1339 snd_usb_endpoint_stop(ep);
1344 * snd_usb_endpoint_stop: stop an snd_usb_endpoint
1346 * @ep: the endpoint to stop (may be NULL)
1348 * A call to this function will decrement the running count of the endpoint.
1349 * In case the last user has requested the endpoint stop, the URBs will
1350 * actually be deactivated.
1352 * Must be balanced to calls of snd_usb_endpoint_start().
1354 * The caller needs to synchronize the pending stop operation via
1355 * snd_usb_endpoint_sync_pending_stop().
1357 void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep)
1362 usb_audio_dbg(ep->chip, "Stopping %s EP 0x%x (running %d)\n",
1363 ep_type_name(ep->type), ep->ep_num,
1364 atomic_read(&ep->running));
1366 if (snd_BUG_ON(!atomic_read(&ep->running)))
1369 if (ep->sync_source)
1370 WRITE_ONCE(ep->sync_source->sync_sink, NULL);
1372 if (!atomic_dec_return(&ep->running))
1373 stop_and_unlink_urbs(ep, false, false);
1377 * snd_usb_endpoint_release: Tear down an snd_usb_endpoint
1379 * @ep: the endpoint to release
1381 * This function does not care for the endpoint's running count but will tear
1382 * down all the streaming URBs immediately.
1384 void snd_usb_endpoint_release(struct snd_usb_endpoint *ep)
1386 release_urbs(ep, 1);
1390 * snd_usb_endpoint_free: Free the resources of an snd_usb_endpoint
1392 * @ep: the endpoint to free
1394 * This free all resources of the given ep.
1396 void snd_usb_endpoint_free(struct snd_usb_endpoint *ep)
1402 * snd_usb_handle_sync_urb: parse an USB sync packet
1404 * @ep: the endpoint to handle the packet
1405 * @sender: the sending endpoint
1406 * @urb: the received packet
1408 * This function is called from the context of an endpoint that received
1409 * the packet and is used to let another endpoint object handle the payload.
1411 static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
1412 struct snd_usb_endpoint *sender,
1413 const struct urb *urb)
1417 unsigned long flags;
1419 snd_BUG_ON(ep == sender);
1422 * In case the endpoint is operating in implicit feedback mode, prepare
1423 * a new outbound URB that has the same layout as the received packet
1424 * and add it to the list of pending urbs. queue_pending_output_urbs()
1425 * will take care of them later.
1427 if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
1428 atomic_read(&ep->running)) {
1430 /* implicit feedback case */
1432 struct snd_urb_ctx *in_ctx;
1433 struct snd_usb_packet_info *out_packet;
1435 in_ctx = urb->context;
1437 /* Count overall packet size */
1438 for (i = 0; i < in_ctx->packets; i++)
1439 if (urb->iso_frame_desc[i].status == 0)
1440 bytes += urb->iso_frame_desc[i].actual_length;
1443 * skip empty packets. At least M-Audio's Fast Track Ultra stops
1444 * streaming once it received a 0-byte OUT URB
1449 spin_lock_irqsave(&ep->lock, flags);
1450 if (ep->next_packet_queued >= ARRAY_SIZE(ep->next_packet)) {
1451 spin_unlock_irqrestore(&ep->lock, flags);
1452 usb_audio_err(ep->chip,
1453 "next package FIFO overflow EP 0x%x\n",
1459 out_packet = next_packet_fifo_enqueue(ep);
1462 * Iterate through the inbound packet and prepare the lengths
1463 * for the output packet. The OUT packet we are about to send
1464 * will have the same amount of payload bytes per stride as the
1465 * IN packet we just received. Since the actual size is scaled
1466 * by the stride, use the sender stride to calculate the length
1467 * in case the number of channels differ between the implicitly
1468 * fed-back endpoint and the synchronizing endpoint.
1471 out_packet->packets = in_ctx->packets;
1472 for (i = 0; i < in_ctx->packets; i++) {
1473 if (urb->iso_frame_desc[i].status == 0)
1474 out_packet->packet_size[i] =
1475 urb->iso_frame_desc[i].actual_length / sender->stride;
1477 out_packet->packet_size[i] = 0;
1480 spin_unlock_irqrestore(&ep->lock, flags);
1481 queue_pending_output_urbs(ep);
1487 * process after playback sync complete
1489 * Full speed devices report feedback values in 10.14 format as samples
1490 * per frame, high speed devices in 16.16 format as samples per
1493 * Because the Audio Class 1 spec was written before USB 2.0, many high
1494 * speed devices use a wrong interpretation, some others use an
1495 * entirely different format.
1497 * Therefore, we cannot predict what format any particular device uses
1498 * and must detect it automatically.
1501 if (urb->iso_frame_desc[0].status != 0 ||
1502 urb->iso_frame_desc[0].actual_length < 3)
1505 f = le32_to_cpup(urb->transfer_buffer);
1506 if (urb->iso_frame_desc[0].actual_length == 3)
1514 if (unlikely(sender->tenor_fb_quirk)) {
1516 * Devices based on Tenor 8802 chipsets (TEAC UD-H01
1517 * and others) sometimes change the feedback value
1520 if (f < ep->freqn - 0x8000)
1522 else if (f > ep->freqn + 0x8000)
1524 } else if (unlikely(ep->freqshift == INT_MIN)) {
1526 * The first time we see a feedback value, determine its format
1527 * by shifting it left or right until it matches the nominal
1528 * frequency value. This assumes that the feedback does not
1529 * differ from the nominal value more than +50% or -25%.
1532 while (f < ep->freqn - ep->freqn / 4) {
1536 while (f > ep->freqn + ep->freqn / 2) {
1540 ep->freqshift = shift;
1541 } else if (ep->freqshift >= 0)
1542 f <<= ep->freqshift;
1544 f >>= -ep->freqshift;
1546 if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) {
1548 * If the frequency looks valid, set it.
1549 * This value is referred to in prepare_playback_urb().
1551 spin_lock_irqsave(&ep->lock, flags);
1553 spin_unlock_irqrestore(&ep->lock, flags);
1556 * Out of range; maybe the shift value is wrong.
1557 * Reset it so that we autodetect again the next time.
1559 ep->freqshift = INT_MIN;