xref: /openbmc/linux/sound/usb/endpoint.c (revision cd921b9f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  */
4 
5 #include <linux/gfp.h>
6 #include <linux/init.h>
7 #include <linux/ratelimit.h>
8 #include <linux/usb.h>
9 #include <linux/usb/audio.h>
10 #include <linux/slab.h>
11 
12 #include <sound/core.h>
13 #include <sound/pcm.h>
14 #include <sound/pcm_params.h>
15 
16 #include "usbaudio.h"
17 #include "helper.h"
18 #include "card.h"
19 #include "endpoint.h"
20 #include "pcm.h"
21 #include "clock.h"
22 #include "quirks.h"
23 
24 enum {
25 	EP_STATE_STOPPED,
26 	EP_STATE_RUNNING,
27 	EP_STATE_STOPPING,
28 };
29 
30 /* interface refcounting */
31 struct snd_usb_iface_ref {
32 	unsigned char iface;
33 	bool need_setup;
34 	int opened;
35 	struct list_head list;
36 };
37 
38 /*
39  * snd_usb_endpoint is a model that abstracts everything related to an
40  * USB endpoint and its streaming.
41  *
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.
46  *
47  * There are two different types of endpoints in audio applications.
48  *
49  * SND_USB_ENDPOINT_TYPE_DATA handles full audio data payload for both
50  * inbound and outbound traffic.
51  *
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
54  * (3 or 4 bytes).
55  *
56  * Each endpoint has to be configured prior to being used by calling
57  * snd_usb_endpoint_set_params().
58  *
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.
63  */
64 
65 /*
66  * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
67  * this will overflow at approx 524 kHz
68  */
69 static inline unsigned get_usb_full_speed_rate(unsigned int rate)
70 {
71 	return ((rate << 13) + 62) / 125;
72 }
73 
74 /*
75  * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
76  * this will overflow at approx 4 MHz
77  */
78 static inline unsigned get_usb_high_speed_rate(unsigned int rate)
79 {
80 	return ((rate << 10) + 62) / 125;
81 }
82 
83 /*
84  * release a urb data
85  */
86 static void release_urb_ctx(struct snd_urb_ctx *u)
87 {
88 	if (u->buffer_size)
89 		usb_free_coherent(u->ep->chip->dev, u->buffer_size,
90 				  u->urb->transfer_buffer,
91 				  u->urb->transfer_dma);
92 	usb_free_urb(u->urb);
93 	u->urb = NULL;
94 }
95 
96 static const char *usb_error_string(int err)
97 {
98 	switch (err) {
99 	case -ENODEV:
100 		return "no device";
101 	case -ENOENT:
102 		return "endpoint not enabled";
103 	case -EPIPE:
104 		return "endpoint stalled";
105 	case -ENOSPC:
106 		return "not enough bandwidth";
107 	case -ESHUTDOWN:
108 		return "device disabled";
109 	case -EHOSTUNREACH:
110 		return "device suspended";
111 	case -EINVAL:
112 	case -EAGAIN:
113 	case -EFBIG:
114 	case -EMSGSIZE:
115 		return "internal error";
116 	default:
117 		return "unknown error";
118 	}
119 }
120 
121 static inline bool ep_state_running(struct snd_usb_endpoint *ep)
122 {
123 	return atomic_read(&ep->state) == EP_STATE_RUNNING;
124 }
125 
126 static inline bool ep_state_update(struct snd_usb_endpoint *ep, int old, int new)
127 {
128 	return atomic_cmpxchg(&ep->state, old, new) == old;
129 }
130 
131 /**
132  * snd_usb_endpoint_implicit_feedback_sink: Report endpoint usage type
133  *
134  * @ep: The snd_usb_endpoint
135  *
136  * Determine whether an endpoint is driven by an implicit feedback
137  * data endpoint source.
138  */
139 int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep)
140 {
141 	return  ep->implicit_fb_sync && usb_pipeout(ep->pipe);
142 }
143 
144 /*
145  * Return the number of samples to be sent in the next packet
146  * for streaming based on information derived from sync endpoints
147  *
148  * This won't be used for implicit feedback which takes the packet size
149  * returned from the sync source
150  */
151 static int slave_next_packet_size(struct snd_usb_endpoint *ep)
152 {
153 	unsigned long flags;
154 	int ret;
155 
156 	if (ep->fill_max)
157 		return ep->maxframesize;
158 
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);
164 
165 	return ret;
166 }
167 
168 /*
169  * Return the number of samples to be sent in the next packet
170  * for adaptive and synchronous endpoints
171  */
172 static int next_packet_size(struct snd_usb_endpoint *ep)
173 {
174 	int ret;
175 
176 	if (ep->fill_max)
177 		return ep->maxframesize;
178 
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];
183 	} else {
184 		ret = ep->packsize[0];
185 	}
186 
187 	return ret;
188 }
189 
190 /*
191  * snd_usb_endpoint_next_packet_size: Return the number of samples to be sent
192  * in the next packet
193  */
194 int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep,
195 				      struct snd_urb_ctx *ctx, int idx)
196 {
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);
201 	else
202 		return next_packet_size(ep);
203 }
204 
205 static void call_retire_callback(struct snd_usb_endpoint *ep,
206 				 struct urb *urb)
207 {
208 	struct snd_usb_substream *data_subs;
209 
210 	data_subs = READ_ONCE(ep->data_subs);
211 	if (data_subs && ep->retire_data_urb)
212 		ep->retire_data_urb(data_subs, urb);
213 }
214 
215 static void retire_outbound_urb(struct snd_usb_endpoint *ep,
216 				struct snd_urb_ctx *urb_ctx)
217 {
218 	call_retire_callback(ep, urb_ctx->urb);
219 }
220 
221 static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
222 				    struct snd_usb_endpoint *sender,
223 				    const struct urb *urb);
224 
225 static void retire_inbound_urb(struct snd_usb_endpoint *ep,
226 			       struct snd_urb_ctx *urb_ctx)
227 {
228 	struct urb *urb = urb_ctx->urb;
229 	struct snd_usb_endpoint *sync_sink;
230 
231 	if (unlikely(ep->skip_packets > 0)) {
232 		ep->skip_packets--;
233 		return;
234 	}
235 
236 	sync_sink = READ_ONCE(ep->sync_sink);
237 	if (sync_sink)
238 		snd_usb_handle_sync_urb(sync_sink, ep, urb);
239 
240 	call_retire_callback(ep, urb);
241 }
242 
243 static inline bool has_tx_length_quirk(struct snd_usb_audio *chip)
244 {
245 	return chip->quirk_flags & QUIRK_FLAG_TX_LENGTH;
246 }
247 
248 static void prepare_silent_urb(struct snd_usb_endpoint *ep,
249 			       struct snd_urb_ctx *ctx)
250 {
251 	struct urb *urb = ctx->urb;
252 	unsigned int offs = 0;
253 	unsigned int extra = 0;
254 	__le32 packet_length;
255 	int i;
256 
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);
260 
261 	for (i = 0; i < ctx->packets; ++i) {
262 		unsigned int offset;
263 		unsigned int length;
264 		int counts;
265 
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;
271 		if (extra) {
272 			packet_length = cpu_to_le32(length);
273 			memcpy(urb->transfer_buffer + offset,
274 			       &packet_length, sizeof(packet_length));
275 		}
276 		memset(urb->transfer_buffer + offset + extra,
277 		       ep->silence_value, length);
278 		offs += counts;
279 	}
280 
281 	urb->number_of_packets = ctx->packets;
282 	urb->transfer_buffer_length = offs * ep->stride + ctx->packets * extra;
283 	ctx->queued = 0;
284 }
285 
286 /*
287  * Prepare a PLAYBACK urb for submission to the bus.
288  */
289 static void prepare_outbound_urb(struct snd_usb_endpoint *ep,
290 				 struct snd_urb_ctx *ctx)
291 {
292 	struct urb *urb = ctx->urb;
293 	unsigned char *cp = urb->transfer_buffer;
294 	struct snd_usb_substream *data_subs;
295 
296 	urb->dev = ep->chip->dev; /* we need to set this at each time */
297 
298 	switch (ep->type) {
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);
305 		break;
306 
307 	case SND_USB_ENDPOINT_TYPE_SYNC:
308 		if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) {
309 			/*
310 			 * fill the length and offset of each urb descriptor.
311 			 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
312 			 */
313 			urb->iso_frame_desc[0].length = 4;
314 			urb->iso_frame_desc[0].offset = 0;
315 			cp[0] = ep->freqn;
316 			cp[1] = ep->freqn >> 8;
317 			cp[2] = ep->freqn >> 16;
318 			cp[3] = ep->freqn >> 24;
319 		} else {
320 			/*
321 			 * fill the length and offset of each urb descriptor.
322 			 * the fixed 10.14 frequency is passed through the pipe.
323 			 */
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;
329 		}
330 
331 		break;
332 	}
333 }
334 
335 /*
336  * Prepare a CAPTURE or SYNC urb for submission to the bus.
337  */
338 static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep,
339 				       struct snd_urb_ctx *urb_ctx)
340 {
341 	int i, offs;
342 	struct urb *urb = urb_ctx->urb;
343 
344 	urb->dev = ep->chip->dev; /* we need to set this at each time */
345 
346 	switch (ep->type) {
347 	case SND_USB_ENDPOINT_TYPE_DATA:
348 		offs = 0;
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;
353 		}
354 
355 		urb->transfer_buffer_length = offs;
356 		urb->number_of_packets = urb_ctx->packets;
357 		break;
358 
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;
362 		break;
363 	}
364 }
365 
366 /* notify an error as XRUN to the assigned PCM data substream */
367 static void notify_xrun(struct snd_usb_endpoint *ep)
368 {
369 	struct snd_usb_substream *data_subs;
370 
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);
374 }
375 
376 static struct snd_usb_packet_info *
377 next_packet_fifo_enqueue(struct snd_usb_endpoint *ep)
378 {
379 	struct snd_usb_packet_info *p;
380 
381 	p = ep->next_packet + (ep->next_packet_head + ep->next_packet_queued) %
382 		ARRAY_SIZE(ep->next_packet);
383 	ep->next_packet_queued++;
384 	return p;
385 }
386 
387 static struct snd_usb_packet_info *
388 next_packet_fifo_dequeue(struct snd_usb_endpoint *ep)
389 {
390 	struct snd_usb_packet_info *p;
391 
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--;
396 	return p;
397 }
398 
399 /*
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
403  * nothing.
404  *
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.
408  *
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.
412  */
413 static void queue_pending_output_urbs(struct snd_usb_endpoint *ep)
414 {
415 	while (ep_state_running(ep)) {
416 
417 		unsigned long flags;
418 		struct snd_usb_packet_info *packet;
419 		struct snd_urb_ctx *ctx = NULL;
420 		int err, i;
421 
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);
429 
430 			packet = next_packet_fifo_dequeue(ep);
431 		}
432 		spin_unlock_irqrestore(&ep->lock, flags);
433 
434 		if (ctx == NULL)
435 			return;
436 
437 		/* copy over the length information */
438 		for (i = 0; i < packet->packets; i++)
439 			ctx->packet_size[i] = packet->packet_size[i];
440 
441 		/* call the data handler to fill in playback data */
442 		prepare_outbound_urb(ep, ctx);
443 
444 		err = usb_submit_urb(ctx->urb, GFP_ATOMIC);
445 		if (err < 0) {
446 			usb_audio_err(ep->chip,
447 				      "Unable to submit urb #%d: %d at %s\n",
448 				      ctx->index, err, __func__);
449 			notify_xrun(ep);
450 			return;
451 		}
452 
453 		set_bit(ctx->index, &ep->active_mask);
454 	}
455 }
456 
457 /*
458  * complete callback for urbs
459  */
460 static void snd_complete_urb(struct urb *urb)
461 {
462 	struct snd_urb_ctx *ctx = urb->context;
463 	struct snd_usb_endpoint *ep = ctx->ep;
464 	unsigned long flags;
465 	int err;
466 
467 	if (unlikely(urb->status == -ENOENT ||		/* unlinked */
468 		     urb->status == -ENODEV ||		/* device removed */
469 		     urb->status == -ECONNRESET ||	/* unlinked */
470 		     urb->status == -ESHUTDOWN))	/* device disabled */
471 		goto exit_clear;
472 	/* device disconnected */
473 	if (unlikely(atomic_read(&ep->chip->shutdown)))
474 		goto exit_clear;
475 
476 	if (unlikely(!ep_state_running(ep)))
477 		goto exit_clear;
478 
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)))
483 			goto exit_clear;
484 
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);
491 			return;
492 		}
493 
494 		prepare_outbound_urb(ep, ctx);
495 		/* can be stopped during prepare callback */
496 		if (unlikely(!ep_state_running(ep)))
497 			goto exit_clear;
498 	} else {
499 		retire_inbound_urb(ep, ctx);
500 		/* can be stopped during retire callback */
501 		if (unlikely(!ep_state_running(ep)))
502 			goto exit_clear;
503 
504 		prepare_inbound_urb(ep, ctx);
505 	}
506 
507 	err = usb_submit_urb(urb, GFP_ATOMIC);
508 	if (err == 0)
509 		return;
510 
511 	usb_audio_err(ep->chip, "cannot submit urb (err = %d)\n", err);
512 	notify_xrun(ep);
513 
514 exit_clear:
515 	clear_bit(ctx->index, &ep->active_mask);
516 }
517 
518 /*
519  * Find or create a refcount object for the given interface
520  *
521  * The objects are released altogether in snd_usb_endpoint_free_all()
522  */
523 static struct snd_usb_iface_ref *
524 iface_ref_find(struct snd_usb_audio *chip, int iface)
525 {
526 	struct snd_usb_iface_ref *ip;
527 
528 	list_for_each_entry(ip, &chip->iface_ref_list, list)
529 		if (ip->iface == iface)
530 			return ip;
531 
532 	ip = kzalloc(sizeof(*ip), GFP_KERNEL);
533 	if (!ip)
534 		return NULL;
535 	ip->iface = iface;
536 	list_add_tail(&ip->list, &chip->iface_ref_list);
537 	return ip;
538 }
539 
540 /*
541  * Get the existing endpoint object corresponding EP
542  * Returns NULL if not present.
543  */
544 struct snd_usb_endpoint *
545 snd_usb_get_endpoint(struct snd_usb_audio *chip, int ep_num)
546 {
547 	struct snd_usb_endpoint *ep;
548 
549 	list_for_each_entry(ep, &chip->ep_list, list) {
550 		if (ep->ep_num == ep_num)
551 			return ep;
552 	}
553 
554 	return NULL;
555 }
556 
557 #define ep_type_name(type) \
558 	(type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync")
559 
560 /**
561  * snd_usb_add_endpoint: Add an endpoint to an USB audio chip
562  *
563  * @chip: The chip
564  * @ep_num: The number of the endpoint to use
565  * @type: SND_USB_ENDPOINT_TYPE_DATA or SND_USB_ENDPOINT_TYPE_SYNC
566  *
567  * If the requested endpoint has not been added to the given chip before,
568  * a new instance is created.
569  *
570  * Returns zero on success or a negative error code.
571  *
572  * New endpoints will be added to chip->ep_list and freed by
573  * calling snd_usb_endpoint_free_all().
574  *
575  * For SND_USB_ENDPOINT_TYPE_SYNC, the caller needs to guarantee that
576  * bNumEndpoints > 1 beforehand.
577  */
578 int snd_usb_add_endpoint(struct snd_usb_audio *chip, int ep_num, int type)
579 {
580 	struct snd_usb_endpoint *ep;
581 	bool is_playback;
582 
583 	ep = snd_usb_get_endpoint(chip, ep_num);
584 	if (ep)
585 		return 0;
586 
587 	usb_audio_dbg(chip, "Creating new %s endpoint #%x\n",
588 		      ep_type_name(type),
589 		      ep_num);
590 	ep = kzalloc(sizeof(*ep), GFP_KERNEL);
591 	if (!ep)
592 		return -ENOMEM;
593 
594 	ep->chip = chip;
595 	spin_lock_init(&ep->lock);
596 	ep->type = type;
597 	ep->ep_num = ep_num;
598 	INIT_LIST_HEAD(&ep->ready_playback_urbs);
599 
600 	is_playback = ((ep_num & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
601 	ep_num &= USB_ENDPOINT_NUMBER_MASK;
602 	if (is_playback)
603 		ep->pipe = usb_sndisocpipe(chip->dev, ep_num);
604 	else
605 		ep->pipe = usb_rcvisocpipe(chip->dev, ep_num);
606 
607 	list_add_tail(&ep->list, &chip->ep_list);
608 	return 0;
609 }
610 
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)
614 {
615 	struct usb_host_interface *alts;
616 	struct usb_endpoint_descriptor *desc;
617 
618 	alts = snd_usb_get_host_interface(chip, ep->iface, ep->altsetting);
619 	if (!alts)
620 		return;
621 
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;
630 	else
631 		ep->syncinterval = 3;
632 
633 	ep->syncmaxsize = le16_to_cpu(desc->wMaxPacketSize);
634 }
635 
636 static bool endpoint_compatible(struct snd_usb_endpoint *ep,
637 				const struct audioformat *fp,
638 				const struct snd_pcm_hw_params *params)
639 {
640 	if (!ep->opened)
641 		return false;
642 	if (ep->cur_audiofmt != fp)
643 		return false;
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))
648 		return false;
649 	return true;
650 }
651 
652 /*
653  * Check whether the given fp and hw params are compatible with the current
654  * setup of the target EP for implicit feedback sync
655  */
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)
660 {
661 	bool ret;
662 
663 	mutex_lock(&chip->mutex);
664 	ret = endpoint_compatible(ep, fp, params);
665 	mutex_unlock(&chip->mutex);
666 	return ret;
667 }
668 
669 /*
670  * snd_usb_endpoint_open: Open the endpoint
671  *
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.
677  *
678  * Note that this function doesn't configure the endpoint.  The substream
679  * needs to set it up later via snd_usb_endpoint_configure().
680  */
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,
685 		      bool is_sync_ep)
686 {
687 	struct snd_usb_endpoint *ep;
688 	int ep_num = is_sync_ep ? fp->sync_ep : fp->endpoint;
689 
690 	mutex_lock(&chip->mutex);
691 	ep = snd_usb_get_endpoint(chip, ep_num);
692 	if (!ep) {
693 		usb_audio_err(chip, "Cannot find EP 0x%x to open\n", ep_num);
694 		goto unlock;
695 	}
696 
697 	if (!ep->opened) {
698 		if (is_sync_ep) {
699 			ep->iface = fp->sync_iface;
700 			ep->altsetting = fp->sync_altsetting;
701 			ep->ep_idx = fp->sync_ep_idx;
702 		} else {
703 			ep->iface = fp->iface;
704 			ep->altsetting = fp->altsetting;
705 			ep->ep_idx = fp->ep_idx;
706 		}
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);
709 
710 		ep->iface_ref = iface_ref_find(chip, ep->iface);
711 		if (!ep->iface_ref) {
712 			ep = NULL;
713 			goto unlock;
714 		}
715 
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);
725 
726 		if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
727 			endpoint_set_syncinterval(chip, ep);
728 
729 		ep->implicit_fb_sync = fp->implicit_fb;
730 		ep->need_setup = true;
731 
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);
737 
738 	} else {
739 		if (WARN_ON(!ep->iface_ref)) {
740 			ep = NULL;
741 			goto unlock;
742 		}
743 
744 		if (!endpoint_compatible(ep, fp, params)) {
745 			usb_audio_err(chip, "Incompatible EP setup for 0x%x\n",
746 				      ep_num);
747 			ep = NULL;
748 			goto unlock;
749 		}
750 
751 		usb_audio_dbg(chip, "Reopened EP 0x%x (count %d)\n",
752 			      ep_num, ep->opened);
753 	}
754 
755 	if (!ep->iface_ref->opened++)
756 		ep->iface_ref->need_setup = true;
757 
758 	ep->opened++;
759 
760  unlock:
761 	mutex_unlock(&chip->mutex);
762 	return ep;
763 }
764 
765 /*
766  * snd_usb_endpoint_set_sync: Link data and sync endpoints
767  *
768  * Pass NULL to sync_ep to unlink again
769  */
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)
773 {
774 	data_ep->sync_source = sync_ep;
775 }
776 
777 /*
778  * Set data endpoint callbacks and the assigned data stream
779  *
780  * Called at PCM trigger and cleanups.
781  * Pass NULL to deactivate each callback.
782  */
783 void snd_usb_endpoint_set_callback(struct snd_usb_endpoint *ep,
784 				   void (*prepare)(struct snd_usb_substream *subs,
785 						   struct urb *urb),
786 				   void (*retire)(struct snd_usb_substream *subs,
787 						  struct urb *urb),
788 				   struct snd_usb_substream *data_subs)
789 {
790 	ep->prepare_data_urb = prepare;
791 	ep->retire_data_urb = retire;
792 	WRITE_ONCE(ep->data_subs, data_subs);
793 }
794 
795 static int endpoint_set_interface(struct snd_usb_audio *chip,
796 				  struct snd_usb_endpoint *ep,
797 				  bool set)
798 {
799 	int altset = set ? ep->altsetting : 0;
800 	int err;
801 
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);
805 	if (err < 0) {
806 		usb_audio_err(chip, "%d:%d: usb_set_interface failed (%d)\n",
807 			      ep->iface, altset, err);
808 		return err;
809 	}
810 
811 	if (chip->quirk_flags & QUIRK_FLAG_IFACE_DELAY)
812 		msleep(50);
813 	return 0;
814 }
815 
816 /*
817  * snd_usb_endpoint_close: Close the endpoint
818  *
819  * Unreference the already opened endpoint via snd_usb_endpoint_open().
820  */
821 void snd_usb_endpoint_close(struct snd_usb_audio *chip,
822 			    struct snd_usb_endpoint *ep)
823 {
824 	mutex_lock(&chip->mutex);
825 	usb_audio_dbg(chip, "Closing EP 0x%x (count %d)\n",
826 		      ep->ep_num, ep->opened);
827 
828 	if (!--ep->iface_ref->opened)
829 		endpoint_set_interface(chip, ep, false);
830 
831 	if (!--ep->opened) {
832 		ep->iface = 0;
833 		ep->altsetting = 0;
834 		ep->cur_audiofmt = NULL;
835 		ep->cur_rate = 0;
836 		ep->iface_ref = NULL;
837 		usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num);
838 	}
839 	mutex_unlock(&chip->mutex);
840 }
841 
842 /* Prepare for suspening EP, called from the main suspend handler */
843 void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep)
844 {
845 	ep->need_setup = true;
846 	if (ep->iface_ref)
847 		ep->iface_ref->need_setup = true;
848 }
849 
850 /*
851  *  wait until all urbs are processed.
852  */
853 static int wait_clear_urbs(struct snd_usb_endpoint *ep)
854 {
855 	unsigned long end_time = jiffies + msecs_to_jiffies(1000);
856 	int alive;
857 
858 	if (atomic_read(&ep->state) != EP_STATE_STOPPING)
859 		return 0;
860 
861 	do {
862 		alive = bitmap_weight(&ep->active_mask, ep->nurbs);
863 		if (!alive)
864 			break;
865 
866 		schedule_timeout_uninterruptible(1);
867 	} while (time_before(jiffies, end_time));
868 
869 	if (alive)
870 		usb_audio_err(ep->chip,
871 			"timeout: still %d active urbs on EP #%x\n",
872 			alive, ep->ep_num);
873 
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);
877 	}
878 
879 	return 0;
880 }
881 
882 /* sync the pending stop operation;
883  * this function itself doesn't trigger the stop operation
884  */
885 void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep)
886 {
887 	if (ep)
888 		wait_clear_urbs(ep);
889 }
890 
891 /*
892  * Stop active urbs
893  *
894  * This function moves the EP to STOPPING state if it's being RUNNING.
895  */
896 static int stop_urbs(struct snd_usb_endpoint *ep, bool force)
897 {
898 	unsigned int i;
899 
900 	if (!force && atomic_read(&ep->running))
901 		return -EBUSY;
902 
903 	if (!ep_state_update(ep, EP_STATE_RUNNING, EP_STATE_STOPPING))
904 		return 0;
905 
906 	INIT_LIST_HEAD(&ep->ready_playback_urbs);
907 	ep->next_packet_head = 0;
908 	ep->next_packet_queued = 0;
909 
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;
914 				usb_unlink_urb(u);
915 			}
916 		}
917 	}
918 
919 	return 0;
920 }
921 
922 /*
923  * release an endpoint's urbs
924  */
925 static int release_urbs(struct snd_usb_endpoint *ep, bool force)
926 {
927 	int i, err;
928 
929 	/* route incoming urbs to nirvana */
930 	snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
931 
932 	/* stop and unlink urbs */
933 	err = stop_urbs(ep, force);
934 	if (err)
935 		return err;
936 
937 	wait_clear_urbs(ep);
938 
939 	for (i = 0; i < ep->nurbs; i++)
940 		release_urb_ctx(&ep->urb[i]);
941 
942 	usb_free_coherent(ep->chip->dev, SYNC_URBS * 4,
943 			  ep->syncbuf, ep->sync_dma);
944 
945 	ep->syncbuf = NULL;
946 	ep->nurbs = 0;
947 	return 0;
948 }
949 
950 /*
951  * configure a data endpoint
952  */
953 static int data_ep_set_params(struct snd_usb_endpoint *ep)
954 {
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));
963 
964 	usb_audio_dbg(chip, "Setting params for data EP 0x%x, pipe 0x%x\n",
965 		      ep->ep_num, ep->pipe);
966 
967 	if (ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) {
968 		/*
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.
972 		 */
973 		frame_bits += ep->cur_channels << 3;
974 	}
975 
976 	ep->datainterval = fmt->datainterval;
977 	ep->stride = frame_bits >> 3;
978 
979 	switch (ep->cur_format) {
980 	case SNDRV_PCM_FORMAT_U8:
981 		ep->silence_value = 0x80;
982 		break;
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;
989 		break;
990 	default:
991 		ep->silence_value = 0;
992 	}
993 
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.)
1010 	 */
1011 	maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) *
1012 			 (frame_bits >> 3);
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;
1019 
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);
1025 	}
1026 
1027 	if (ep->fill_max)
1028 		ep->curpacksize = ep->maxpacksize;
1029 	else
1030 		ep->curpacksize = maxsize;
1031 
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;
1035 	} else {
1036 		packs_per_ms = 1;
1037 		max_packs_per_urb = MAX_PACKS;
1038 	}
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);
1043 
1044 	/*
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.
1048 	 *
1049 	 * Playback endpoints with implicit sync much use the same parameters
1050 	 * as their corresponding capture endpoint.
1051 	 */
1052 	if (usb_pipein(ep->pipe) || ep->implicit_fb_sync) {
1053 
1054 		urb_packs = packs_per_ms;
1055 		/*
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
1059 		 * gaps.
1060 		 */
1061 		if (snd_usb_get_speed(chip->dev) == USB_SPEED_WIRELESS) {
1062 			int interval = ep->datainterval;
1063 			while (interval < 5) {
1064 				urb_packs <<= 1;
1065 				++interval;
1066 			}
1067 		}
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)
1071 			urb_packs >>= 1;
1072 		ep->nurbs = MAX_URBS;
1073 
1074 	/*
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.
1079 	 */
1080 	} else {
1081 		/* determine how small a packet can be */
1082 		minsize = (ep->freqn >> (16 - ep->datainterval)) *
1083 				(frame_bits >> 3);
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);
1088 
1089 		/* how many packets will contain an entire ALSA period? */
1090 		max_packs_per_period = DIV_ROUND_UP(ep->cur_period_bytes, minsize);
1091 
1092 		/* how many URBs will contain a period? */
1093 		urbs_per_period = DIV_ROUND_UP(max_packs_per_period,
1094 				max_packs_per_urb);
1095 		/* how many packets are needed in each URB? */
1096 		urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period);
1097 
1098 		/* limit the number of frames in a single URB */
1099 		ep->max_urb_frames = DIV_ROUND_UP(ep->cur_period_frames,
1100 						  urbs_per_period);
1101 
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);
1106 	}
1107 
1108 	/* allocate and initialize data urbs */
1109 	for (i = 0; i < ep->nurbs; i++) {
1110 		struct snd_urb_ctx *u = &ep->urb[i];
1111 		u->index = i;
1112 		u->ep = ep;
1113 		u->packets = urb_packs;
1114 		u->buffer_size = maxsize * u->packets;
1115 
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);
1119 		if (!u->urb)
1120 			goto out_of_memory;
1121 
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)
1126 			goto out_of_memory;
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);
1133 	}
1134 
1135 	/* total buffer bytes of all URBs plus the next queue;
1136 	 * referred in pcm.c
1137 	 */
1138 	ep->nominal_queue_size = maxsize * urb_packs * (ep->nurbs + 1);
1139 	return 0;
1140 
1141 out_of_memory:
1142 	release_urbs(ep, false);
1143 	return -ENOMEM;
1144 }
1145 
1146 /*
1147  * configure a sync endpoint
1148  */
1149 static int sync_ep_set_params(struct snd_usb_endpoint *ep)
1150 {
1151 	struct snd_usb_audio *chip = ep->chip;
1152 	int i;
1153 
1154 	usb_audio_dbg(chip, "Setting params for sync EP 0x%x, pipe 0x%x\n",
1155 		      ep->ep_num, ep->pipe);
1156 
1157 	ep->syncbuf = usb_alloc_coherent(chip->dev, SYNC_URBS * 4,
1158 					 GFP_KERNEL, &ep->sync_dma);
1159 	if (!ep->syncbuf)
1160 		return -ENOMEM;
1161 
1162 	for (i = 0; i < SYNC_URBS; i++) {
1163 		struct snd_urb_ctx *u = &ep->urb[i];
1164 		u->index = i;
1165 		u->ep = ep;
1166 		u->packets = 1;
1167 		u->urb = usb_alloc_urb(1, GFP_KERNEL);
1168 		if (!u->urb)
1169 			goto out_of_memory;
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;
1179 	}
1180 
1181 	ep->nurbs = SYNC_URBS;
1182 
1183 	return 0;
1184 
1185 out_of_memory:
1186 	release_urbs(ep, false);
1187 	return -ENOMEM;
1188 }
1189 
1190 /*
1191  * snd_usb_endpoint_set_params: configure an snd_usb_endpoint
1192  *
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.
1196  */
1197 static int snd_usb_endpoint_set_params(struct snd_usb_audio *chip,
1198 				       struct snd_usb_endpoint *ep)
1199 {
1200 	const struct audioformat *fmt = ep->cur_audiofmt;
1201 	int err;
1202 
1203 	/* release old buffers, if any */
1204 	err = release_urbs(ep, false);
1205 	if (err < 0)
1206 		return err;
1207 
1208 	ep->datainterval = fmt->datainterval;
1209 	ep->maxpacksize = fmt->maxpacksize;
1210 	ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX);
1211 
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;
1215 	} else {
1216 		ep->freqn = get_usb_high_speed_rate(ep->cur_rate);
1217 		ep->pps = 8000 >> ep->datainterval;
1218 	}
1219 
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;
1223 
1224 	/* calculate the frequency in 16.16 format */
1225 	ep->freqm = ep->freqn;
1226 	ep->freqshift = INT_MIN;
1227 
1228 	ep->phase = 0;
1229 
1230 	switch (ep->type) {
1231 	case  SND_USB_ENDPOINT_TYPE_DATA:
1232 		err = data_ep_set_params(ep);
1233 		break;
1234 	case  SND_USB_ENDPOINT_TYPE_SYNC:
1235 		err = sync_ep_set_params(ep);
1236 		break;
1237 	default:
1238 		err = -EINVAL;
1239 	}
1240 
1241 	usb_audio_dbg(chip, "Set up %d URBS, ret=%d\n", ep->nurbs, err);
1242 
1243 	if (err < 0)
1244 		return err;
1245 
1246 	/* some unit conversions in runtime */
1247 	ep->maxframesize = ep->maxpacksize / ep->cur_frame_bytes;
1248 	ep->curframesize = ep->curpacksize / ep->cur_frame_bytes;
1249 
1250 	return 0;
1251 }
1252 
1253 /*
1254  * snd_usb_endpoint_configure: Configure the endpoint
1255  *
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.
1260  *
1261  * This returns zero if unchanged, 1 if the configuration has changed,
1262  * or a negative error code.
1263  */
1264 int snd_usb_endpoint_configure(struct snd_usb_audio *chip,
1265 			       struct snd_usb_endpoint *ep)
1266 {
1267 	bool iface_first;
1268 	int err = 0;
1269 
1270 	mutex_lock(&chip->mutex);
1271 	if (WARN_ON(!ep->iface_ref))
1272 		goto unlock;
1273 	if (!ep->need_setup)
1274 		goto unlock;
1275 
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
1280 		 */
1281 		if (ep->cur_audiofmt->protocol == UAC_VERSION_1) {
1282 			err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt,
1283 						       ep->cur_rate);
1284 			if (err < 0)
1285 				goto unlock;
1286 		}
1287 		err = snd_usb_endpoint_set_params(chip, ep);
1288 		if (err < 0)
1289 			goto unlock;
1290 		goto done;
1291 	}
1292 
1293 	/* Need to deselect altsetting at first */
1294 	endpoint_set_interface(chip, ep, false);
1295 
1296 	/* Some UAC1 devices (e.g. Yamaha THR10) need the host interface
1297 	 * to be set up before parameter setups
1298 	 */
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)
1302 		iface_first = true;
1303 	if (iface_first) {
1304 		err = endpoint_set_interface(chip, ep, true);
1305 		if (err < 0)
1306 			goto unlock;
1307 	}
1308 
1309 	err = snd_usb_init_pitch(chip, ep->cur_audiofmt);
1310 	if (err < 0)
1311 		goto unlock;
1312 
1313 	err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, ep->cur_rate);
1314 	if (err < 0)
1315 		goto unlock;
1316 
1317 	err = snd_usb_endpoint_set_params(chip, ep);
1318 	if (err < 0)
1319 		goto unlock;
1320 
1321 	err = snd_usb_select_mode_quirk(chip, ep->cur_audiofmt);
1322 	if (err < 0)
1323 		goto unlock;
1324 
1325 	/* for UAC2/3, enable the interface altset here at last */
1326 	if (!iface_first) {
1327 		err = endpoint_set_interface(chip, ep, true);
1328 		if (err < 0)
1329 			goto unlock;
1330 	}
1331 
1332 	ep->iface_ref->need_setup = false;
1333 
1334  done:
1335 	ep->need_setup = false;
1336 	err = 1;
1337 
1338 unlock:
1339 	mutex_unlock(&chip->mutex);
1340 	return err;
1341 }
1342 
1343 /**
1344  * snd_usb_endpoint_start: start an snd_usb_endpoint
1345  *
1346  * @ep: the endpoint to start
1347  *
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.
1351  *
1352  * Must be balanced to calls of snd_usb_endpoint_stop().
1353  *
1354  * Returns an error if the URB submission failed, 0 in all other cases.
1355  */
1356 int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)
1357 {
1358 	int err;
1359 	unsigned int i;
1360 
1361 	if (atomic_read(&ep->chip->shutdown))
1362 		return -EBADFD;
1363 
1364 	if (ep->sync_source)
1365 		WRITE_ONCE(ep->sync_source->sync_sink, ep);
1366 
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));
1370 
1371 	/* already running? */
1372 	if (atomic_inc_return(&ep->running) != 1)
1373 		return 0;
1374 
1375 	ep->active_mask = 0;
1376 	ep->unlink_mask = 0;
1377 	ep->phase = 0;
1378 	ep->sample_accum = 0;
1379 
1380 	snd_usb_endpoint_start_quirk(ep);
1381 
1382 	/*
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.
1387 	 */
1388 
1389 	if (!ep_state_update(ep, EP_STATE_STOPPED, EP_STATE_RUNNING))
1390 		goto __error;
1391 
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);
1397 		}
1398 
1399 		usb_audio_dbg(ep->chip, "No URB submission due to implicit fb sync\n");
1400 		return 0;
1401 	}
1402 
1403 	for (i = 0; i < ep->nurbs; i++) {
1404 		struct urb *urb = ep->urb[i].urb;
1405 
1406 		if (snd_BUG_ON(!urb))
1407 			goto __error;
1408 
1409 		if (usb_pipeout(ep->pipe)) {
1410 			prepare_outbound_urb(ep, urb->context);
1411 		} else {
1412 			prepare_inbound_urb(ep, urb->context);
1413 		}
1414 
1415 		err = usb_submit_urb(urb, GFP_ATOMIC);
1416 		if (err < 0) {
1417 			usb_audio_err(ep->chip,
1418 				"cannot submit urb %d, error %d: %s\n",
1419 				i, err, usb_error_string(err));
1420 			goto __error;
1421 		}
1422 		set_bit(i, &ep->active_mask);
1423 	}
1424 
1425 	usb_audio_dbg(ep->chip, "%d URBs submitted for EP 0x%x\n",
1426 		      ep->nurbs, ep->ep_num);
1427 	return 0;
1428 
1429 __error:
1430 	snd_usb_endpoint_stop(ep);
1431 	return -EPIPE;
1432 }
1433 
1434 /**
1435  * snd_usb_endpoint_stop: stop an snd_usb_endpoint
1436  *
1437  * @ep: the endpoint to stop (may be NULL)
1438  *
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.
1442  *
1443  * Must be balanced to calls of snd_usb_endpoint_start().
1444  *
1445  * The caller needs to synchronize the pending stop operation via
1446  * snd_usb_endpoint_sync_pending_stop().
1447  */
1448 void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep)
1449 {
1450 	if (!ep)
1451 		return;
1452 
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));
1456 
1457 	if (snd_BUG_ON(!atomic_read(&ep->running)))
1458 		return;
1459 
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);
1464 	}
1465 }
1466 
1467 /**
1468  * snd_usb_endpoint_release: Tear down an snd_usb_endpoint
1469  *
1470  * @ep: the endpoint to release
1471  *
1472  * This function does not care for the endpoint's running count but will tear
1473  * down all the streaming URBs immediately.
1474  */
1475 void snd_usb_endpoint_release(struct snd_usb_endpoint *ep)
1476 {
1477 	release_urbs(ep, true);
1478 }
1479 
1480 /**
1481  * snd_usb_endpoint_free_all: Free the resources of an snd_usb_endpoint
1482  * @chip: The chip
1483  *
1484  * This free all endpoints and those resources
1485  */
1486 void snd_usb_endpoint_free_all(struct snd_usb_audio *chip)
1487 {
1488 	struct snd_usb_endpoint *ep, *en;
1489 	struct snd_usb_iface_ref *ip, *in;
1490 
1491 	list_for_each_entry_safe(ep, en, &chip->ep_list, list)
1492 		kfree(ep);
1493 
1494 	list_for_each_entry_safe(ip, in, &chip->iface_ref_list, list)
1495 		kfree(ip);
1496 }
1497 
1498 /*
1499  * snd_usb_handle_sync_urb: parse an USB sync packet
1500  *
1501  * @ep: the endpoint to handle the packet
1502  * @sender: the sending endpoint
1503  * @urb: the received packet
1504  *
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.
1507  */
1508 static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
1509 				    struct snd_usb_endpoint *sender,
1510 				    const struct urb *urb)
1511 {
1512 	int shift;
1513 	unsigned int f;
1514 	unsigned long flags;
1515 
1516 	snd_BUG_ON(ep == sender);
1517 
1518 	/*
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.
1523 	 */
1524 	if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
1525 	    atomic_read(&ep->running)) {
1526 
1527 		/* implicit feedback case */
1528 		int i, bytes = 0;
1529 		struct snd_urb_ctx *in_ctx;
1530 		struct snd_usb_packet_info *out_packet;
1531 
1532 		in_ctx = urb->context;
1533 
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;
1538 
1539 		/*
1540 		 * skip empty packets. At least M-Audio's Fast Track Ultra stops
1541 		 * streaming once it received a 0-byte OUT URB
1542 		 */
1543 		if (bytes == 0)
1544 			return;
1545 
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",
1551 				      ep->ep_num);
1552 			notify_xrun(ep);
1553 			return;
1554 		}
1555 
1556 		out_packet = next_packet_fifo_enqueue(ep);
1557 
1558 		/*
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.
1566 		 */
1567 
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;
1573 			else
1574 				out_packet->packet_size[i] = 0;
1575 		}
1576 
1577 		spin_unlock_irqrestore(&ep->lock, flags);
1578 		queue_pending_output_urbs(ep);
1579 
1580 		return;
1581 	}
1582 
1583 	/*
1584 	 * process after playback sync complete
1585 	 *
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
1588 	 * microframe.
1589 	 *
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.
1593 	 *
1594 	 * Therefore, we cannot predict what format any particular device uses
1595 	 * and must detect it automatically.
1596 	 */
1597 
1598 	if (urb->iso_frame_desc[0].status != 0 ||
1599 	    urb->iso_frame_desc[0].actual_length < 3)
1600 		return;
1601 
1602 	f = le32_to_cpup(urb->transfer_buffer);
1603 	if (urb->iso_frame_desc[0].actual_length == 3)
1604 		f &= 0x00ffffff;
1605 	else
1606 		f &= 0x0fffffff;
1607 
1608 	if (f == 0)
1609 		return;
1610 
1611 	if (unlikely(sender->tenor_fb_quirk)) {
1612 		/*
1613 		 * Devices based on Tenor 8802 chipsets (TEAC UD-H01
1614 		 * and others) sometimes change the feedback value
1615 		 * by +/- 0x1.0000.
1616 		 */
1617 		if (f < ep->freqn - 0x8000)
1618 			f += 0xf000;
1619 		else if (f > ep->freqn + 0x8000)
1620 			f -= 0xf000;
1621 	} else if (unlikely(ep->freqshift == INT_MIN)) {
1622 		/*
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%.
1627 		 */
1628 		shift = 0;
1629 		while (f < ep->freqn - ep->freqn / 4) {
1630 			f <<= 1;
1631 			shift++;
1632 		}
1633 		while (f > ep->freqn + ep->freqn / 2) {
1634 			f >>= 1;
1635 			shift--;
1636 		}
1637 		ep->freqshift = shift;
1638 	} else if (ep->freqshift >= 0)
1639 		f <<= ep->freqshift;
1640 	else
1641 		f >>= -ep->freqshift;
1642 
1643 	if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) {
1644 		/*
1645 		 * If the frequency looks valid, set it.
1646 		 * This value is referred to in prepare_playback_urb().
1647 		 */
1648 		spin_lock_irqsave(&ep->lock, flags);
1649 		ep->freqm = f;
1650 		spin_unlock_irqrestore(&ep->lock, flags);
1651 	} else {
1652 		/*
1653 		 * Out of range; maybe the shift value is wrong.
1654 		 * Reset it so that we autodetect again the next time.
1655 		 */
1656 		ep->freqshift = INT_MIN;
1657 	}
1658 }
1659 
1660