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
27 /* interface refcounting */
28 struct snd_usb_iface_ref {
32 struct list_head list;
36 * snd_usb_endpoint is a model that abstracts everything related to an
37 * USB endpoint and its streaming.
39 * There are functions to activate and deactivate the streaming URBs and
40 * optional callbacks to let the pcm logic handle the actual content of the
41 * packets for playback and record. Thus, the bus streaming and the audio
42 * handlers are fully decoupled.
44 * There are two different types of endpoints in audio applications.
46 * SND_USB_ENDPOINT_TYPE_DATA handles full audio data payload for both
47 * inbound and outbound traffic.
49 * SND_USB_ENDPOINT_TYPE_SYNC endpoints are for inbound traffic only and
50 * expect the payload to carry Q10.14 / Q16.16 formatted sync information
53 * Each endpoint has to be configured prior to being used by calling
54 * snd_usb_endpoint_set_params().
56 * The model incorporates a reference counting, so that multiple users
57 * can call snd_usb_endpoint_start() and snd_usb_endpoint_stop(), and
58 * only the first user will effectively start the URBs, and only the last
59 * one to stop it will tear the URBs down again.
63 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
64 * this will overflow at approx 524 kHz
66 static inline unsigned get_usb_full_speed_rate(unsigned int rate)
68 return ((rate << 13) + 62) / 125;
72 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
73 * this will overflow at approx 4 MHz
75 static inline unsigned get_usb_high_speed_rate(unsigned int rate)
77 return ((rate << 10) + 62) / 125;
83 static void release_urb_ctx(struct snd_urb_ctx *u)
86 usb_free_coherent(u->ep->chip->dev, u->buffer_size,
87 u->urb->transfer_buffer,
88 u->urb->transfer_dma);
93 static const char *usb_error_string(int err)
99 return "endpoint not enabled";
101 return "endpoint stalled";
103 return "not enough bandwidth";
105 return "device disabled";
107 return "device suspended";
112 return "internal error";
114 return "unknown error";
119 * snd_usb_endpoint_implicit_feedback_sink: Report endpoint usage type
121 * @ep: The snd_usb_endpoint
123 * Determine whether an endpoint is driven by an implicit feedback
124 * data endpoint source.
126 int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep)
128 return ep->implicit_fb_sync && usb_pipeout(ep->pipe);
132 * Return the number of samples to be sent in the next packet
133 * for streaming based on information derived from sync endpoints
135 * This won't be used for implicit feedback which takes the packet size
136 * returned from the sync source
138 static int slave_next_packet_size(struct snd_usb_endpoint *ep)
144 return ep->maxframesize;
146 spin_lock_irqsave(&ep->lock, flags);
147 ep->phase = (ep->phase & 0xffff)
148 + (ep->freqm << ep->datainterval);
149 ret = min(ep->phase >> 16, ep->maxframesize);
150 spin_unlock_irqrestore(&ep->lock, flags);
156 * Return the number of samples to be sent in the next packet
157 * for adaptive and synchronous endpoints
159 static int next_packet_size(struct snd_usb_endpoint *ep)
164 return ep->maxframesize;
166 ep->sample_accum += ep->sample_rem;
167 if (ep->sample_accum >= ep->pps) {
168 ep->sample_accum -= ep->pps;
169 ret = ep->packsize[1];
171 ret = ep->packsize[0];
178 * snd_usb_endpoint_next_packet_size: Return the number of samples to be sent
181 int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep,
182 struct snd_urb_ctx *ctx, int idx)
184 if (ctx->packet_size[idx])
185 return ctx->packet_size[idx];
186 else if (ep->sync_source)
187 return slave_next_packet_size(ep);
189 return next_packet_size(ep);
192 static void call_retire_callback(struct snd_usb_endpoint *ep,
195 struct snd_usb_substream *data_subs;
197 data_subs = READ_ONCE(ep->data_subs);
198 if (data_subs && ep->retire_data_urb)
199 ep->retire_data_urb(data_subs, urb);
202 static void retire_outbound_urb(struct snd_usb_endpoint *ep,
203 struct snd_urb_ctx *urb_ctx)
205 call_retire_callback(ep, urb_ctx->urb);
208 static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
209 struct snd_usb_endpoint *sender,
210 const struct urb *urb);
212 static void retire_inbound_urb(struct snd_usb_endpoint *ep,
213 struct snd_urb_ctx *urb_ctx)
215 struct urb *urb = urb_ctx->urb;
216 struct snd_usb_endpoint *sync_sink;
218 if (unlikely(ep->skip_packets > 0)) {
223 sync_sink = READ_ONCE(ep->sync_sink);
225 snd_usb_handle_sync_urb(sync_sink, ep, urb);
227 call_retire_callback(ep, urb);
230 static void prepare_silent_urb(struct snd_usb_endpoint *ep,
231 struct snd_urb_ctx *ctx)
233 struct urb *urb = ctx->urb;
234 unsigned int offs = 0;
235 unsigned int extra = 0;
236 __le32 packet_length;
239 /* For tx_length_quirk, put packet length at start of packet */
240 if (ep->chip->tx_length_quirk)
241 extra = sizeof(packet_length);
243 for (i = 0; i < ctx->packets; ++i) {
248 counts = snd_usb_endpoint_next_packet_size(ep, ctx, i);
249 length = counts * ep->stride; /* number of silent bytes */
250 offset = offs * ep->stride + extra * i;
251 urb->iso_frame_desc[i].offset = offset;
252 urb->iso_frame_desc[i].length = length + extra;
254 packet_length = cpu_to_le32(length);
255 memcpy(urb->transfer_buffer + offset,
256 &packet_length, sizeof(packet_length));
258 memset(urb->transfer_buffer + offset + extra,
259 ep->silence_value, length);
263 urb->number_of_packets = ctx->packets;
264 urb->transfer_buffer_length = offs * ep->stride + ctx->packets * extra;
268 * Prepare a PLAYBACK urb for submission to the bus.
270 static void prepare_outbound_urb(struct snd_usb_endpoint *ep,
271 struct snd_urb_ctx *ctx)
273 struct urb *urb = ctx->urb;
274 unsigned char *cp = urb->transfer_buffer;
275 struct snd_usb_substream *data_subs;
277 urb->dev = ep->chip->dev; /* we need to set this at each time */
280 case SND_USB_ENDPOINT_TYPE_DATA:
281 data_subs = READ_ONCE(ep->data_subs);
282 if (data_subs && ep->prepare_data_urb)
283 ep->prepare_data_urb(data_subs, urb);
284 else /* no data provider, so send silence */
285 prepare_silent_urb(ep, ctx);
288 case SND_USB_ENDPOINT_TYPE_SYNC:
289 if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) {
291 * fill the length and offset of each urb descriptor.
292 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
294 urb->iso_frame_desc[0].length = 4;
295 urb->iso_frame_desc[0].offset = 0;
297 cp[1] = ep->freqn >> 8;
298 cp[2] = ep->freqn >> 16;
299 cp[3] = ep->freqn >> 24;
302 * fill the length and offset of each urb descriptor.
303 * the fixed 10.14 frequency is passed through the pipe.
305 urb->iso_frame_desc[0].length = 3;
306 urb->iso_frame_desc[0].offset = 0;
307 cp[0] = ep->freqn >> 2;
308 cp[1] = ep->freqn >> 10;
309 cp[2] = ep->freqn >> 18;
317 * Prepare a CAPTURE or SYNC urb for submission to the bus.
319 static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep,
320 struct snd_urb_ctx *urb_ctx)
323 struct urb *urb = urb_ctx->urb;
325 urb->dev = ep->chip->dev; /* we need to set this at each time */
328 case SND_USB_ENDPOINT_TYPE_DATA:
330 for (i = 0; i < urb_ctx->packets; i++) {
331 urb->iso_frame_desc[i].offset = offs;
332 urb->iso_frame_desc[i].length = ep->curpacksize;
333 offs += ep->curpacksize;
336 urb->transfer_buffer_length = offs;
337 urb->number_of_packets = urb_ctx->packets;
340 case SND_USB_ENDPOINT_TYPE_SYNC:
341 urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize);
342 urb->iso_frame_desc[0].offset = 0;
347 /* notify an error as XRUN to the assigned PCM data substream */
348 static void notify_xrun(struct snd_usb_endpoint *ep)
350 struct snd_usb_substream *data_subs;
352 data_subs = READ_ONCE(ep->data_subs);
353 if (data_subs && data_subs->pcm_substream)
354 snd_pcm_stop_xrun(data_subs->pcm_substream);
357 static struct snd_usb_packet_info *
358 next_packet_fifo_enqueue(struct snd_usb_endpoint *ep)
360 struct snd_usb_packet_info *p;
362 p = ep->next_packet + (ep->next_packet_head + ep->next_packet_queued) %
363 ARRAY_SIZE(ep->next_packet);
364 ep->next_packet_queued++;
368 static struct snd_usb_packet_info *
369 next_packet_fifo_dequeue(struct snd_usb_endpoint *ep)
371 struct snd_usb_packet_info *p;
373 p = ep->next_packet + ep->next_packet_head;
374 ep->next_packet_head++;
375 ep->next_packet_head %= ARRAY_SIZE(ep->next_packet);
376 ep->next_packet_queued--;
381 * Send output urbs that have been prepared previously. URBs are dequeued
382 * from ep->ready_playback_urbs and in case there aren't any available
383 * or there are no packets that have been prepared, this function does
386 * The reason why the functionality of sending and preparing URBs is separated
387 * is that host controllers don't guarantee the order in which they return
388 * inbound and outbound packets to their submitters.
390 * This function is only used for implicit feedback endpoints. For endpoints
391 * driven by dedicated sync endpoints, URBs are immediately re-submitted
392 * from their completion handler.
394 static void queue_pending_output_urbs(struct snd_usb_endpoint *ep)
396 while (test_bit(EP_FLAG_RUNNING, &ep->flags)) {
399 struct snd_usb_packet_info *packet;
400 struct snd_urb_ctx *ctx = NULL;
403 spin_lock_irqsave(&ep->lock, flags);
404 if (ep->next_packet_queued > 0 &&
405 !list_empty(&ep->ready_playback_urbs)) {
406 /* take URB out of FIFO */
407 ctx = list_first_entry(&ep->ready_playback_urbs,
408 struct snd_urb_ctx, ready_list);
409 list_del_init(&ctx->ready_list);
411 packet = next_packet_fifo_dequeue(ep);
413 spin_unlock_irqrestore(&ep->lock, flags);
418 /* copy over the length information */
419 for (i = 0; i < packet->packets; i++)
420 ctx->packet_size[i] = packet->packet_size[i];
422 /* call the data handler to fill in playback data */
423 prepare_outbound_urb(ep, ctx);
425 err = usb_submit_urb(ctx->urb, GFP_ATOMIC);
427 usb_audio_err(ep->chip,
428 "Unable to submit urb #%d: %d at %s\n",
429 ctx->index, err, __func__);
434 set_bit(ctx->index, &ep->active_mask);
439 * complete callback for urbs
441 static void snd_complete_urb(struct urb *urb)
443 struct snd_urb_ctx *ctx = urb->context;
444 struct snd_usb_endpoint *ep = ctx->ep;
448 if (unlikely(urb->status == -ENOENT || /* unlinked */
449 urb->status == -ENODEV || /* device removed */
450 urb->status == -ECONNRESET || /* unlinked */
451 urb->status == -ESHUTDOWN)) /* device disabled */
453 /* device disconnected */
454 if (unlikely(atomic_read(&ep->chip->shutdown)))
457 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
460 if (usb_pipeout(ep->pipe)) {
461 retire_outbound_urb(ep, ctx);
462 /* can be stopped during retire callback */
463 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
466 if (snd_usb_endpoint_implicit_feedback_sink(ep)) {
467 spin_lock_irqsave(&ep->lock, flags);
468 list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
469 clear_bit(ctx->index, &ep->active_mask);
470 spin_unlock_irqrestore(&ep->lock, flags);
471 queue_pending_output_urbs(ep);
475 prepare_outbound_urb(ep, ctx);
476 /* can be stopped during prepare callback */
477 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
480 retire_inbound_urb(ep, ctx);
481 /* can be stopped during retire callback */
482 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
485 prepare_inbound_urb(ep, ctx);
488 err = usb_submit_urb(urb, GFP_ATOMIC);
492 usb_audio_err(ep->chip, "cannot submit urb (err = %d)\n", err);
496 clear_bit(ctx->index, &ep->active_mask);
500 * Find or create a refcount object for the given interface
502 * The objects are released altogether in snd_usb_endpoint_free_all()
504 static struct snd_usb_iface_ref *
505 iface_ref_find(struct snd_usb_audio *chip, int iface)
507 struct snd_usb_iface_ref *ip;
509 list_for_each_entry(ip, &chip->iface_ref_list, list)
510 if (ip->iface == iface)
513 ip = kzalloc(sizeof(*ip), GFP_KERNEL);
517 list_add_tail(&ip->list, &chip->iface_ref_list);
522 * Get the existing endpoint object corresponding EP
523 * Returns NULL if not present.
525 struct snd_usb_endpoint *
526 snd_usb_get_endpoint(struct snd_usb_audio *chip, int ep_num)
528 struct snd_usb_endpoint *ep;
530 list_for_each_entry(ep, &chip->ep_list, list) {
531 if (ep->ep_num == ep_num)
538 #define ep_type_name(type) \
539 (type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync")
542 * snd_usb_add_endpoint: Add an endpoint to an USB audio chip
545 * @ep_num: The number of the endpoint to use
546 * @type: SND_USB_ENDPOINT_TYPE_DATA or SND_USB_ENDPOINT_TYPE_SYNC
548 * If the requested endpoint has not been added to the given chip before,
549 * a new instance is created.
551 * Returns zero on success or a negative error code.
553 * New endpoints will be added to chip->ep_list and freed by
554 * calling snd_usb_endpoint_free_all().
556 * For SND_USB_ENDPOINT_TYPE_SYNC, the caller needs to guarantee that
557 * bNumEndpoints > 1 beforehand.
559 int snd_usb_add_endpoint(struct snd_usb_audio *chip, int ep_num, int type)
561 struct snd_usb_endpoint *ep;
564 ep = snd_usb_get_endpoint(chip, ep_num);
568 usb_audio_dbg(chip, "Creating new %s endpoint #%x\n",
571 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
576 spin_lock_init(&ep->lock);
579 INIT_LIST_HEAD(&ep->ready_playback_urbs);
581 is_playback = ((ep_num & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
582 ep_num &= USB_ENDPOINT_NUMBER_MASK;
584 ep->pipe = usb_sndisocpipe(chip->dev, ep_num);
586 ep->pipe = usb_rcvisocpipe(chip->dev, ep_num);
588 list_add_tail(&ep->list, &chip->ep_list);
592 /* Set up syncinterval and maxsyncsize for a sync EP */
593 static void endpoint_set_syncinterval(struct snd_usb_audio *chip,
594 struct snd_usb_endpoint *ep)
596 struct usb_host_interface *alts;
597 struct usb_endpoint_descriptor *desc;
599 alts = snd_usb_get_host_interface(chip, ep->iface, ep->altsetting);
603 desc = get_endpoint(alts, ep->ep_idx);
604 if (desc->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
605 desc->bRefresh >= 1 && desc->bRefresh <= 9)
606 ep->syncinterval = desc->bRefresh;
607 else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL)
608 ep->syncinterval = 1;
609 else if (desc->bInterval >= 1 && desc->bInterval <= 16)
610 ep->syncinterval = desc->bInterval - 1;
612 ep->syncinterval = 3;
614 ep->syncmaxsize = le16_to_cpu(desc->wMaxPacketSize);
617 static bool endpoint_compatible(struct snd_usb_endpoint *ep,
618 const struct audioformat *fp,
619 const struct snd_pcm_hw_params *params)
623 if (ep->cur_audiofmt != fp)
625 if (ep->cur_rate != params_rate(params) ||
626 ep->cur_format != params_format(params) ||
627 ep->cur_period_frames != params_period_size(params) ||
628 ep->cur_buffer_periods != params_periods(params))
634 * Check whether the given fp and hw params are compatbile with the current
635 * setup of the target EP for implicit feedback sync
637 bool snd_usb_endpoint_compatible(struct snd_usb_audio *chip,
638 struct snd_usb_endpoint *ep,
639 const struct audioformat *fp,
640 const struct snd_pcm_hw_params *params)
644 mutex_lock(&chip->mutex);
645 ret = endpoint_compatible(ep, fp, params);
646 mutex_unlock(&chip->mutex);
651 * snd_usb_endpoint_open: Open the endpoint
653 * Called from hw_params to assign the endpoint to the substream.
654 * It's reference-counted, and only the first opener is allowed to set up
655 * arbitrary parameters. The later opener must be compatible with the
656 * former opened parameters.
657 * The endpoint needs to be closed via snd_usb_endpoint_close() later.
659 * Note that this function doesn't configure the endpoint. The substream
660 * needs to set it up later via snd_usb_endpoint_configure().
662 struct snd_usb_endpoint *
663 snd_usb_endpoint_open(struct snd_usb_audio *chip,
664 const struct audioformat *fp,
665 const struct snd_pcm_hw_params *params,
668 struct snd_usb_endpoint *ep;
669 int ep_num = is_sync_ep ? fp->sync_ep : fp->endpoint;
671 mutex_lock(&chip->mutex);
672 ep = snd_usb_get_endpoint(chip, ep_num);
674 usb_audio_err(chip, "Cannot find EP 0x%x to open\n", ep_num);
680 ep->iface = fp->sync_iface;
681 ep->altsetting = fp->sync_altsetting;
682 ep->ep_idx = fp->sync_ep_idx;
684 ep->iface = fp->iface;
685 ep->altsetting = fp->altsetting;
686 ep->ep_idx = fp->ep_idx;
688 usb_audio_dbg(chip, "Open EP 0x%x, iface=%d:%d, idx=%d\n",
689 ep_num, ep->iface, ep->altsetting, ep->ep_idx);
691 ep->iface_ref = iface_ref_find(chip, ep->iface);
692 if (!ep->iface_ref) {
697 ep->cur_audiofmt = fp;
698 ep->cur_channels = fp->channels;
699 ep->cur_rate = params_rate(params);
700 ep->cur_format = params_format(params);
701 ep->cur_frame_bytes = snd_pcm_format_physical_width(ep->cur_format) *
702 ep->cur_channels / 8;
703 ep->cur_period_frames = params_period_size(params);
704 ep->cur_period_bytes = ep->cur_period_frames * ep->cur_frame_bytes;
705 ep->cur_buffer_periods = params_periods(params);
707 if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
708 endpoint_set_syncinterval(chip, ep);
710 ep->implicit_fb_sync = fp->implicit_fb;
711 ep->need_setup = true;
713 usb_audio_dbg(chip, " channels=%d, rate=%d, format=%s, period_bytes=%d, periods=%d, implicit_fb=%d\n",
714 ep->cur_channels, ep->cur_rate,
715 snd_pcm_format_name(ep->cur_format),
716 ep->cur_period_bytes, ep->cur_buffer_periods,
717 ep->implicit_fb_sync);
720 if (WARN_ON(!ep->iface_ref)) {
725 if (!endpoint_compatible(ep, fp, params)) {
726 usb_audio_err(chip, "Incompatible EP setup for 0x%x\n",
732 usb_audio_dbg(chip, "Reopened EP 0x%x (count %d)\n",
736 if (!ep->iface_ref->opened++)
737 ep->iface_ref->need_setup = true;
742 mutex_unlock(&chip->mutex);
747 * snd_usb_endpoint_set_sync: Link data and sync endpoints
749 * Pass NULL to sync_ep to unlink again
751 void snd_usb_endpoint_set_sync(struct snd_usb_audio *chip,
752 struct snd_usb_endpoint *data_ep,
753 struct snd_usb_endpoint *sync_ep)
755 data_ep->sync_source = sync_ep;
759 * Set data endpoint callbacks and the assigned data stream
761 * Called at PCM trigger and cleanups.
762 * Pass NULL to deactivate each callback.
764 void snd_usb_endpoint_set_callback(struct snd_usb_endpoint *ep,
765 void (*prepare)(struct snd_usb_substream *subs,
767 void (*retire)(struct snd_usb_substream *subs,
769 struct snd_usb_substream *data_subs)
771 ep->prepare_data_urb = prepare;
772 ep->retire_data_urb = retire;
773 WRITE_ONCE(ep->data_subs, data_subs);
776 static int endpoint_set_interface(struct snd_usb_audio *chip,
777 struct snd_usb_endpoint *ep,
780 int altset = set ? ep->altsetting : 0;
783 usb_audio_dbg(chip, "Setting usb interface %d:%d for EP 0x%x\n",
784 ep->iface, altset, ep->ep_num);
785 err = usb_set_interface(chip->dev, ep->iface, altset);
787 usb_audio_err(chip, "%d:%d: usb_set_interface failed (%d)\n",
788 ep->iface, altset, err);
792 snd_usb_set_interface_quirk(chip);
797 * snd_usb_endpoint_close: Close the endpoint
799 * Unreference the already opened endpoint via snd_usb_endpoint_open().
801 void snd_usb_endpoint_close(struct snd_usb_audio *chip,
802 struct snd_usb_endpoint *ep)
804 mutex_lock(&chip->mutex);
805 usb_audio_dbg(chip, "Closing EP 0x%x (count %d)\n",
806 ep->ep_num, ep->opened);
808 if (!--ep->iface_ref->opened)
809 endpoint_set_interface(chip, ep, false);
814 ep->cur_audiofmt = NULL;
816 ep->iface_ref = NULL;
817 usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num);
819 mutex_unlock(&chip->mutex);
822 /* Prepare for suspening EP, called from the main suspend handler */
823 void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep)
825 ep->need_setup = true;
827 ep->iface_ref->need_setup = true;
831 * wait until all urbs are processed.
833 static int wait_clear_urbs(struct snd_usb_endpoint *ep)
835 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
838 if (!test_bit(EP_FLAG_STOPPING, &ep->flags))
842 alive = bitmap_weight(&ep->active_mask, ep->nurbs);
846 schedule_timeout_uninterruptible(1);
847 } while (time_before(jiffies, end_time));
850 usb_audio_err(ep->chip,
851 "timeout: still %d active urbs on EP #%x\n",
853 clear_bit(EP_FLAG_STOPPING, &ep->flags);
855 ep->sync_sink = NULL;
856 snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
861 /* sync the pending stop operation;
862 * this function itself doesn't trigger the stop operation
864 void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep)
871 * Stop and unlink active urbs.
873 * This function checks and clears EP_FLAG_RUNNING state.
874 * When @wait_sync is set, it waits until all pending URBs are killed.
876 static int stop_and_unlink_urbs(struct snd_usb_endpoint *ep, bool force,
881 if (!force && atomic_read(&ep->chip->shutdown)) /* to be sure... */
884 if (atomic_read(&ep->running))
887 if (!test_and_clear_bit(EP_FLAG_RUNNING, &ep->flags))
890 set_bit(EP_FLAG_STOPPING, &ep->flags);
891 INIT_LIST_HEAD(&ep->ready_playback_urbs);
892 ep->next_packet_head = 0;
893 ep->next_packet_queued = 0;
895 for (i = 0; i < ep->nurbs; i++) {
896 if (test_bit(i, &ep->active_mask)) {
897 if (!test_and_set_bit(i, &ep->unlink_mask)) {
898 struct urb *u = ep->urb[i].urb;
906 return wait_clear_urbs(ep);
911 * release an endpoint's urbs
913 static void release_urbs(struct snd_usb_endpoint *ep, int force)
917 /* route incoming urbs to nirvana */
918 snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
921 stop_and_unlink_urbs(ep, force, true);
923 for (i = 0; i < ep->nurbs; i++)
924 release_urb_ctx(&ep->urb[i]);
926 usb_free_coherent(ep->chip->dev, SYNC_URBS * 4,
927 ep->syncbuf, ep->sync_dma);
934 * configure a data endpoint
936 static int data_ep_set_params(struct snd_usb_endpoint *ep)
938 struct snd_usb_audio *chip = ep->chip;
939 unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb;
940 unsigned int max_packs_per_period, urbs_per_period, urb_packs;
941 unsigned int max_urbs, i;
942 const struct audioformat *fmt = ep->cur_audiofmt;
943 int frame_bits = ep->cur_frame_bytes * 8;
944 int tx_length_quirk = (chip->tx_length_quirk &&
945 usb_pipeout(ep->pipe));
947 usb_audio_dbg(chip, "Setting params for data EP 0x%x, pipe 0x%x\n",
948 ep->ep_num, ep->pipe);
950 if (ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) {
952 * When operating in DSD DOP mode, the size of a sample frame
953 * in hardware differs from the actual physical format width
954 * because we need to make room for the DOP markers.
956 frame_bits += ep->cur_channels << 3;
959 ep->datainterval = fmt->datainterval;
960 ep->stride = frame_bits >> 3;
962 switch (ep->cur_format) {
963 case SNDRV_PCM_FORMAT_U8:
964 ep->silence_value = 0x80;
966 case SNDRV_PCM_FORMAT_DSD_U8:
967 case SNDRV_PCM_FORMAT_DSD_U16_LE:
968 case SNDRV_PCM_FORMAT_DSD_U32_LE:
969 case SNDRV_PCM_FORMAT_DSD_U16_BE:
970 case SNDRV_PCM_FORMAT_DSD_U32_BE:
971 ep->silence_value = 0x69;
974 ep->silence_value = 0;
977 /* assume max. frequency is 50% higher than nominal */
978 ep->freqmax = ep->freqn + (ep->freqn >> 1);
979 /* Round up freqmax to nearest integer in order to calculate maximum
980 * packet size, which must represent a whole number of frames.
981 * This is accomplished by adding 0x0.ffff before converting the
982 * Q16.16 format into integer.
983 * In order to accurately calculate the maximum packet size when
984 * the data interval is more than 1 (i.e. ep->datainterval > 0),
985 * multiply by the data interval prior to rounding. For instance,
986 * a freqmax of 41 kHz will result in a max packet size of 6 (5.125)
987 * frames with a data interval of 1, but 11 (10.25) frames with a
988 * data interval of 2.
989 * (ep->freqmax << ep->datainterval overflows at 8.192 MHz for the
990 * maximum datainterval value of 3, at USB full speed, higher for
991 * USB high speed, noting that ep->freqmax is in units of
992 * frames per packet in Q16.16 format.)
994 maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) *
997 maxsize += sizeof(__le32); /* Space for length descriptor */
998 /* but wMaxPacketSize might reduce this */
999 if (ep->maxpacksize && ep->maxpacksize < maxsize) {
1000 /* whatever fits into a max. size packet */
1001 unsigned int data_maxsize = maxsize = ep->maxpacksize;
1003 if (tx_length_quirk)
1004 /* Need to remove the length descriptor to calc freq */
1005 data_maxsize -= sizeof(__le32);
1006 ep->freqmax = (data_maxsize / (frame_bits >> 3))
1007 << (16 - ep->datainterval);
1011 ep->curpacksize = ep->maxpacksize;
1013 ep->curpacksize = maxsize;
1015 if (snd_usb_get_speed(chip->dev) != USB_SPEED_FULL) {
1016 packs_per_ms = 8 >> ep->datainterval;
1017 max_packs_per_urb = MAX_PACKS_HS;
1020 max_packs_per_urb = MAX_PACKS;
1022 if (ep->sync_source && !ep->implicit_fb_sync)
1023 max_packs_per_urb = min(max_packs_per_urb,
1024 1U << ep->sync_source->syncinterval);
1025 max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval);
1028 * Capture endpoints need to use small URBs because there's no way
1029 * to tell in advance where the next period will end, and we don't
1030 * want the next URB to complete much after the period ends.
1032 * Playback endpoints with implicit sync much use the same parameters
1033 * as their corresponding capture endpoint.
1035 if (usb_pipein(ep->pipe) || ep->implicit_fb_sync) {
1037 urb_packs = packs_per_ms;
1039 * Wireless devices can poll at a max rate of once per 4ms.
1040 * For dataintervals less than 5, increase the packet count to
1041 * allow the host controller to use bursting to fill in the
1044 if (snd_usb_get_speed(chip->dev) == USB_SPEED_WIRELESS) {
1045 int interval = ep->datainterval;
1046 while (interval < 5) {
1051 /* make capture URBs <= 1 ms and smaller than a period */
1052 urb_packs = min(max_packs_per_urb, urb_packs);
1053 while (urb_packs > 1 && urb_packs * maxsize >= ep->cur_period_bytes)
1055 ep->nurbs = MAX_URBS;
1058 * Playback endpoints without implicit sync are adjusted so that
1059 * a period fits as evenly as possible in the smallest number of
1060 * URBs. The total number of URBs is adjusted to the size of the
1061 * ALSA buffer, subject to the MAX_URBS and MAX_QUEUE limits.
1064 /* determine how small a packet can be */
1065 minsize = (ep->freqn >> (16 - ep->datainterval)) *
1067 /* with sync from device, assume it can be 12% lower */
1068 if (ep->sync_source)
1069 minsize -= minsize >> 3;
1070 minsize = max(minsize, 1u);
1072 /* how many packets will contain an entire ALSA period? */
1073 max_packs_per_period = DIV_ROUND_UP(ep->cur_period_bytes, minsize);
1075 /* how many URBs will contain a period? */
1076 urbs_per_period = DIV_ROUND_UP(max_packs_per_period,
1078 /* how many packets are needed in each URB? */
1079 urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period);
1081 /* limit the number of frames in a single URB */
1082 ep->max_urb_frames = DIV_ROUND_UP(ep->cur_period_frames,
1085 /* try to use enough URBs to contain an entire ALSA buffer */
1086 max_urbs = min((unsigned) MAX_URBS,
1087 MAX_QUEUE * packs_per_ms / urb_packs);
1088 ep->nurbs = min(max_urbs, urbs_per_period * ep->cur_buffer_periods);
1091 /* allocate and initialize data urbs */
1092 for (i = 0; i < ep->nurbs; i++) {
1093 struct snd_urb_ctx *u = &ep->urb[i];
1096 u->packets = urb_packs;
1097 u->buffer_size = maxsize * u->packets;
1099 if (fmt->fmt_type == UAC_FORMAT_TYPE_II)
1100 u->packets++; /* for transfer delimiter */
1101 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
1105 u->urb->transfer_buffer =
1106 usb_alloc_coherent(chip->dev, u->buffer_size,
1107 GFP_KERNEL, &u->urb->transfer_dma);
1108 if (!u->urb->transfer_buffer)
1110 u->urb->pipe = ep->pipe;
1111 u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1112 u->urb->interval = 1 << ep->datainterval;
1113 u->urb->context = u;
1114 u->urb->complete = snd_complete_urb;
1115 INIT_LIST_HEAD(&u->ready_list);
1121 release_urbs(ep, 0);
1126 * configure a sync endpoint
1128 static int sync_ep_set_params(struct snd_usb_endpoint *ep)
1130 struct snd_usb_audio *chip = ep->chip;
1133 usb_audio_dbg(chip, "Setting params for sync EP 0x%x, pipe 0x%x\n",
1134 ep->ep_num, ep->pipe);
1136 ep->syncbuf = usb_alloc_coherent(chip->dev, SYNC_URBS * 4,
1137 GFP_KERNEL, &ep->sync_dma);
1141 for (i = 0; i < SYNC_URBS; i++) {
1142 struct snd_urb_ctx *u = &ep->urb[i];
1146 u->urb = usb_alloc_urb(1, GFP_KERNEL);
1149 u->urb->transfer_buffer = ep->syncbuf + i * 4;
1150 u->urb->transfer_dma = ep->sync_dma + i * 4;
1151 u->urb->transfer_buffer_length = 4;
1152 u->urb->pipe = ep->pipe;
1153 u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1154 u->urb->number_of_packets = 1;
1155 u->urb->interval = 1 << ep->syncinterval;
1156 u->urb->context = u;
1157 u->urb->complete = snd_complete_urb;
1160 ep->nurbs = SYNC_URBS;
1165 release_urbs(ep, 0);
1170 * snd_usb_endpoint_set_params: configure an snd_usb_endpoint
1172 * Determine the number of URBs to be used on this endpoint.
1173 * An endpoint must be configured before it can be started.
1174 * An endpoint that is already running can not be reconfigured.
1176 static int snd_usb_endpoint_set_params(struct snd_usb_audio *chip,
1177 struct snd_usb_endpoint *ep)
1179 const struct audioformat *fmt = ep->cur_audiofmt;
1182 /* release old buffers, if any */
1183 release_urbs(ep, 0);
1185 ep->datainterval = fmt->datainterval;
1186 ep->maxpacksize = fmt->maxpacksize;
1187 ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX);
1189 if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) {
1190 ep->freqn = get_usb_full_speed_rate(ep->cur_rate);
1191 ep->pps = 1000 >> ep->datainterval;
1193 ep->freqn = get_usb_high_speed_rate(ep->cur_rate);
1194 ep->pps = 8000 >> ep->datainterval;
1197 ep->sample_rem = ep->cur_rate % ep->pps;
1198 ep->packsize[0] = ep->cur_rate / ep->pps;
1199 ep->packsize[1] = (ep->cur_rate + (ep->pps - 1)) / ep->pps;
1201 /* calculate the frequency in 16.16 format */
1202 ep->freqm = ep->freqn;
1203 ep->freqshift = INT_MIN;
1208 case SND_USB_ENDPOINT_TYPE_DATA:
1209 err = data_ep_set_params(ep);
1211 case SND_USB_ENDPOINT_TYPE_SYNC:
1212 err = sync_ep_set_params(ep);
1218 usb_audio_dbg(chip, "Set up %d URBS, ret=%d\n", ep->nurbs, err);
1223 /* some unit conversions in runtime */
1224 ep->maxframesize = ep->maxpacksize / ep->cur_frame_bytes;
1225 ep->curframesize = ep->curpacksize / ep->cur_frame_bytes;
1231 * snd_usb_endpoint_configure: Configure the endpoint
1233 * This function sets up the EP to be fully usable state.
1234 * It's called either from hw_params or prepare callback.
1235 * The function checks need_setup flag, and perfoms nothing unless needed,
1236 * so it's safe to call this multiple times.
1238 * This returns zero if unchanged, 1 if the configuration has changed,
1239 * or a negative error code.
1241 int snd_usb_endpoint_configure(struct snd_usb_audio *chip,
1242 struct snd_usb_endpoint *ep)
1247 mutex_lock(&chip->mutex);
1248 if (WARN_ON(!ep->iface_ref))
1250 if (!ep->need_setup)
1253 /* If the interface has been already set up, just set EP parameters */
1254 if (!ep->iface_ref->need_setup) {
1255 err = snd_usb_endpoint_set_params(chip, ep);
1261 /* Need to deselect altsetting at first */
1262 endpoint_set_interface(chip, ep, false);
1264 /* Some UAC1 devices (e.g. Yamaha THR10) need the host interface
1265 * to be set up before parameter setups
1267 iface_first = ep->cur_audiofmt->protocol == UAC_VERSION_1;
1269 err = endpoint_set_interface(chip, ep, true);
1274 err = snd_usb_init_pitch(chip, ep->cur_audiofmt);
1278 err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, ep->cur_rate);
1282 err = snd_usb_endpoint_set_params(chip, ep);
1286 err = snd_usb_select_mode_quirk(chip, ep->cur_audiofmt);
1290 /* for UAC2/3, enable the interface altset here at last */
1292 err = endpoint_set_interface(chip, ep, true);
1297 ep->iface_ref->need_setup = false;
1300 ep->need_setup = false;
1304 mutex_unlock(&chip->mutex);
1309 * snd_usb_endpoint_start: start an snd_usb_endpoint
1311 * @ep: the endpoint to start
1313 * A call to this function will increment the running count of the endpoint.
1314 * In case it is not already running, the URBs for this endpoint will be
1315 * submitted. Otherwise, this function does nothing.
1317 * Must be balanced to calls of snd_usb_endpoint_stop().
1319 * Returns an error if the URB submission failed, 0 in all other cases.
1321 int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)
1326 if (atomic_read(&ep->chip->shutdown))
1329 if (ep->sync_source)
1330 WRITE_ONCE(ep->sync_source->sync_sink, ep);
1332 usb_audio_dbg(ep->chip, "Starting %s EP 0x%x (running %d)\n",
1333 ep_type_name(ep->type), ep->ep_num,
1334 atomic_read(&ep->running));
1336 /* already running? */
1337 if (atomic_inc_return(&ep->running) != 1)
1340 ep->active_mask = 0;
1341 ep->unlink_mask = 0;
1343 ep->sample_accum = 0;
1345 snd_usb_endpoint_start_quirk(ep);
1348 * If this endpoint has a data endpoint as implicit feedback source,
1349 * don't start the urbs here. Instead, mark them all as available,
1350 * wait for the record urbs to return and queue the playback urbs
1351 * from that context.
1354 set_bit(EP_FLAG_RUNNING, &ep->flags);
1356 if (snd_usb_endpoint_implicit_feedback_sink(ep)) {
1357 for (i = 0; i < ep->nurbs; i++) {
1358 struct snd_urb_ctx *ctx = ep->urb + i;
1359 list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
1362 usb_audio_dbg(ep->chip, "No URB submission due to implicit fb sync\n");
1366 for (i = 0; i < ep->nurbs; i++) {
1367 struct urb *urb = ep->urb[i].urb;
1369 if (snd_BUG_ON(!urb))
1372 if (usb_pipeout(ep->pipe)) {
1373 prepare_outbound_urb(ep, urb->context);
1375 prepare_inbound_urb(ep, urb->context);
1378 err = usb_submit_urb(urb, GFP_ATOMIC);
1380 usb_audio_err(ep->chip,
1381 "cannot submit urb %d, error %d: %s\n",
1382 i, err, usb_error_string(err));
1385 set_bit(i, &ep->active_mask);
1388 usb_audio_dbg(ep->chip, "%d URBs submitted for EP 0x%x\n",
1389 ep->nurbs, ep->ep_num);
1393 snd_usb_endpoint_stop(ep);
1398 * snd_usb_endpoint_stop: stop an snd_usb_endpoint
1400 * @ep: the endpoint to stop (may be NULL)
1402 * A call to this function will decrement the running count of the endpoint.
1403 * In case the last user has requested the endpoint stop, the URBs will
1404 * actually be deactivated.
1406 * Must be balanced to calls of snd_usb_endpoint_start().
1408 * The caller needs to synchronize the pending stop operation via
1409 * snd_usb_endpoint_sync_pending_stop().
1411 void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep)
1416 usb_audio_dbg(ep->chip, "Stopping %s EP 0x%x (running %d)\n",
1417 ep_type_name(ep->type), ep->ep_num,
1418 atomic_read(&ep->running));
1420 if (snd_BUG_ON(!atomic_read(&ep->running)))
1423 if (ep->sync_source)
1424 WRITE_ONCE(ep->sync_source->sync_sink, NULL);
1426 if (!atomic_dec_return(&ep->running))
1427 stop_and_unlink_urbs(ep, false, false);
1431 * snd_usb_endpoint_release: Tear down an snd_usb_endpoint
1433 * @ep: the endpoint to release
1435 * This function does not care for the endpoint's running count but will tear
1436 * down all the streaming URBs immediately.
1438 void snd_usb_endpoint_release(struct snd_usb_endpoint *ep)
1440 release_urbs(ep, 1);
1444 * snd_usb_endpoint_free_all: Free the resources of an snd_usb_endpoint
1447 * This free all endpoints and those resources
1449 void snd_usb_endpoint_free_all(struct snd_usb_audio *chip)
1451 struct snd_usb_endpoint *ep, *en;
1452 struct snd_usb_iface_ref *ip, *in;
1454 list_for_each_entry_safe(ep, en, &chip->ep_list, list)
1457 list_for_each_entry_safe(ip, in, &chip->iface_ref_list, list)
1462 * snd_usb_handle_sync_urb: parse an USB sync packet
1464 * @ep: the endpoint to handle the packet
1465 * @sender: the sending endpoint
1466 * @urb: the received packet
1468 * This function is called from the context of an endpoint that received
1469 * the packet and is used to let another endpoint object handle the payload.
1471 static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
1472 struct snd_usb_endpoint *sender,
1473 const struct urb *urb)
1477 unsigned long flags;
1479 snd_BUG_ON(ep == sender);
1482 * In case the endpoint is operating in implicit feedback mode, prepare
1483 * a new outbound URB that has the same layout as the received packet
1484 * and add it to the list of pending urbs. queue_pending_output_urbs()
1485 * will take care of them later.
1487 if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
1488 atomic_read(&ep->running)) {
1490 /* implicit feedback case */
1492 struct snd_urb_ctx *in_ctx;
1493 struct snd_usb_packet_info *out_packet;
1495 in_ctx = urb->context;
1497 /* Count overall packet size */
1498 for (i = 0; i < in_ctx->packets; i++)
1499 if (urb->iso_frame_desc[i].status == 0)
1500 bytes += urb->iso_frame_desc[i].actual_length;
1503 * skip empty packets. At least M-Audio's Fast Track Ultra stops
1504 * streaming once it received a 0-byte OUT URB
1509 spin_lock_irqsave(&ep->lock, flags);
1510 if (ep->next_packet_queued >= ARRAY_SIZE(ep->next_packet)) {
1511 spin_unlock_irqrestore(&ep->lock, flags);
1512 usb_audio_err(ep->chip,
1513 "next package FIFO overflow EP 0x%x\n",
1519 out_packet = next_packet_fifo_enqueue(ep);
1522 * Iterate through the inbound packet and prepare the lengths
1523 * for the output packet. The OUT packet we are about to send
1524 * will have the same amount of payload bytes per stride as the
1525 * IN packet we just received. Since the actual size is scaled
1526 * by the stride, use the sender stride to calculate the length
1527 * in case the number of channels differ between the implicitly
1528 * fed-back endpoint and the synchronizing endpoint.
1531 out_packet->packets = in_ctx->packets;
1532 for (i = 0; i < in_ctx->packets; i++) {
1533 if (urb->iso_frame_desc[i].status == 0)
1534 out_packet->packet_size[i] =
1535 urb->iso_frame_desc[i].actual_length / sender->stride;
1537 out_packet->packet_size[i] = 0;
1540 spin_unlock_irqrestore(&ep->lock, flags);
1541 queue_pending_output_urbs(ep);
1547 * process after playback sync complete
1549 * Full speed devices report feedback values in 10.14 format as samples
1550 * per frame, high speed devices in 16.16 format as samples per
1553 * Because the Audio Class 1 spec was written before USB 2.0, many high
1554 * speed devices use a wrong interpretation, some others use an
1555 * entirely different format.
1557 * Therefore, we cannot predict what format any particular device uses
1558 * and must detect it automatically.
1561 if (urb->iso_frame_desc[0].status != 0 ||
1562 urb->iso_frame_desc[0].actual_length < 3)
1565 f = le32_to_cpup(urb->transfer_buffer);
1566 if (urb->iso_frame_desc[0].actual_length == 3)
1574 if (unlikely(sender->tenor_fb_quirk)) {
1576 * Devices based on Tenor 8802 chipsets (TEAC UD-H01
1577 * and others) sometimes change the feedback value
1580 if (f < ep->freqn - 0x8000)
1582 else if (f > ep->freqn + 0x8000)
1584 } else if (unlikely(ep->freqshift == INT_MIN)) {
1586 * The first time we see a feedback value, determine its format
1587 * by shifting it left or right until it matches the nominal
1588 * frequency value. This assumes that the feedback does not
1589 * differ from the nominal value more than +50% or -25%.
1592 while (f < ep->freqn - ep->freqn / 4) {
1596 while (f > ep->freqn + ep->freqn / 2) {
1600 ep->freqshift = shift;
1601 } else if (ep->freqshift >= 0)
1602 f <<= ep->freqshift;
1604 f >>= -ep->freqshift;
1606 if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) {
1608 * If the frequency looks valid, set it.
1609 * This value is referred to in prepare_playback_urb().
1611 spin_lock_irqsave(&ep->lock, flags);
1613 spin_unlock_irqrestore(&ep->lock, flags);
1616 * Out of range; maybe the shift value is wrong.
1617 * Reset it so that we autodetect again the next time.
1619 ep->freqshift = INT_MIN;