xref: /openbmc/linux/sound/usb/endpoint.c (revision f4c3b83b)
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 #define EP_FLAG_RUNNING		1
25 #define EP_FLAG_STOPPING	2
26 
27 /* interface refcounting */
28 struct snd_usb_iface_ref {
29 	unsigned char iface;
30 	bool need_setup;
31 	int opened;
32 	struct list_head list;
33 };
34 
35 /*
36  * snd_usb_endpoint is a model that abstracts everything related to an
37  * USB endpoint and its streaming.
38  *
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.
43  *
44  * There are two different types of endpoints in audio applications.
45  *
46  * SND_USB_ENDPOINT_TYPE_DATA handles full audio data payload for both
47  * inbound and outbound traffic.
48  *
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
51  * (3 or 4 bytes).
52  *
53  * Each endpoint has to be configured prior to being used by calling
54  * snd_usb_endpoint_set_params().
55  *
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.
60  */
61 
62 /*
63  * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
64  * this will overflow at approx 524 kHz
65  */
66 static inline unsigned get_usb_full_speed_rate(unsigned int rate)
67 {
68 	return ((rate << 13) + 62) / 125;
69 }
70 
71 /*
72  * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
73  * this will overflow at approx 4 MHz
74  */
75 static inline unsigned get_usb_high_speed_rate(unsigned int rate)
76 {
77 	return ((rate << 10) + 62) / 125;
78 }
79 
80 /*
81  * release a urb data
82  */
83 static void release_urb_ctx(struct snd_urb_ctx *u)
84 {
85 	if (u->buffer_size)
86 		usb_free_coherent(u->ep->chip->dev, u->buffer_size,
87 				  u->urb->transfer_buffer,
88 				  u->urb->transfer_dma);
89 	usb_free_urb(u->urb);
90 	u->urb = NULL;
91 }
92 
93 static const char *usb_error_string(int err)
94 {
95 	switch (err) {
96 	case -ENODEV:
97 		return "no device";
98 	case -ENOENT:
99 		return "endpoint not enabled";
100 	case -EPIPE:
101 		return "endpoint stalled";
102 	case -ENOSPC:
103 		return "not enough bandwidth";
104 	case -ESHUTDOWN:
105 		return "device disabled";
106 	case -EHOSTUNREACH:
107 		return "device suspended";
108 	case -EINVAL:
109 	case -EAGAIN:
110 	case -EFBIG:
111 	case -EMSGSIZE:
112 		return "internal error";
113 	default:
114 		return "unknown error";
115 	}
116 }
117 
118 /**
119  * snd_usb_endpoint_implicit_feedback_sink: Report endpoint usage type
120  *
121  * @ep: The snd_usb_endpoint
122  *
123  * Determine whether an endpoint is driven by an implicit feedback
124  * data endpoint source.
125  */
126 int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep)
127 {
128 	return  ep->implicit_fb_sync && usb_pipeout(ep->pipe);
129 }
130 
131 /*
132  * Return the number of samples to be sent in the next packet
133  * for streaming based on information derived from sync endpoints
134  *
135  * This won't be used for implicit feedback which takes the packet size
136  * returned from the sync source
137  */
138 static int slave_next_packet_size(struct snd_usb_endpoint *ep)
139 {
140 	unsigned long flags;
141 	int ret;
142 
143 	if (ep->fill_max)
144 		return ep->maxframesize;
145 
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);
151 
152 	return ret;
153 }
154 
155 /*
156  * Return the number of samples to be sent in the next packet
157  * for adaptive and synchronous endpoints
158  */
159 static int next_packet_size(struct snd_usb_endpoint *ep)
160 {
161 	int ret;
162 
163 	if (ep->fill_max)
164 		return ep->maxframesize;
165 
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];
170 	} else {
171 		ret = ep->packsize[0];
172 	}
173 
174 	return ret;
175 }
176 
177 /*
178  * snd_usb_endpoint_next_packet_size: Return the number of samples to be sent
179  * in the next packet
180  */
181 int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep,
182 				      struct snd_urb_ctx *ctx, int idx)
183 {
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);
188 	else
189 		return next_packet_size(ep);
190 }
191 
192 static void call_retire_callback(struct snd_usb_endpoint *ep,
193 				 struct urb *urb)
194 {
195 	struct snd_usb_substream *data_subs;
196 
197 	data_subs = READ_ONCE(ep->data_subs);
198 	if (data_subs && ep->retire_data_urb)
199 		ep->retire_data_urb(data_subs, urb);
200 }
201 
202 static void retire_outbound_urb(struct snd_usb_endpoint *ep,
203 				struct snd_urb_ctx *urb_ctx)
204 {
205 	call_retire_callback(ep, urb_ctx->urb);
206 }
207 
208 static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
209 				    struct snd_usb_endpoint *sender,
210 				    const struct urb *urb);
211 
212 static void retire_inbound_urb(struct snd_usb_endpoint *ep,
213 			       struct snd_urb_ctx *urb_ctx)
214 {
215 	struct urb *urb = urb_ctx->urb;
216 	struct snd_usb_endpoint *sync_sink;
217 
218 	if (unlikely(ep->skip_packets > 0)) {
219 		ep->skip_packets--;
220 		return;
221 	}
222 
223 	sync_sink = READ_ONCE(ep->sync_sink);
224 	if (sync_sink)
225 		snd_usb_handle_sync_urb(sync_sink, ep, urb);
226 
227 	call_retire_callback(ep, urb);
228 }
229 
230 static void prepare_silent_urb(struct snd_usb_endpoint *ep,
231 			       struct snd_urb_ctx *ctx)
232 {
233 	struct urb *urb = ctx->urb;
234 	unsigned int offs = 0;
235 	unsigned int extra = 0;
236 	__le32 packet_length;
237 	int i;
238 
239 	/* For tx_length_quirk, put packet length at start of packet */
240 	if (ep->chip->tx_length_quirk)
241 		extra = sizeof(packet_length);
242 
243 	for (i = 0; i < ctx->packets; ++i) {
244 		unsigned int offset;
245 		unsigned int length;
246 		int counts;
247 
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;
253 		if (extra) {
254 			packet_length = cpu_to_le32(length);
255 			memcpy(urb->transfer_buffer + offset,
256 			       &packet_length, sizeof(packet_length));
257 		}
258 		memset(urb->transfer_buffer + offset + extra,
259 		       ep->silence_value, length);
260 		offs += counts;
261 	}
262 
263 	urb->number_of_packets = ctx->packets;
264 	urb->transfer_buffer_length = offs * ep->stride + ctx->packets * extra;
265 }
266 
267 /*
268  * Prepare a PLAYBACK urb for submission to the bus.
269  */
270 static void prepare_outbound_urb(struct snd_usb_endpoint *ep,
271 				 struct snd_urb_ctx *ctx)
272 {
273 	struct urb *urb = ctx->urb;
274 	unsigned char *cp = urb->transfer_buffer;
275 	struct snd_usb_substream *data_subs;
276 
277 	urb->dev = ep->chip->dev; /* we need to set this at each time */
278 
279 	switch (ep->type) {
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);
286 		break;
287 
288 	case SND_USB_ENDPOINT_TYPE_SYNC:
289 		if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) {
290 			/*
291 			 * fill the length and offset of each urb descriptor.
292 			 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
293 			 */
294 			urb->iso_frame_desc[0].length = 4;
295 			urb->iso_frame_desc[0].offset = 0;
296 			cp[0] = ep->freqn;
297 			cp[1] = ep->freqn >> 8;
298 			cp[2] = ep->freqn >> 16;
299 			cp[3] = ep->freqn >> 24;
300 		} else {
301 			/*
302 			 * fill the length and offset of each urb descriptor.
303 			 * the fixed 10.14 frequency is passed through the pipe.
304 			 */
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;
310 		}
311 
312 		break;
313 	}
314 }
315 
316 /*
317  * Prepare a CAPTURE or SYNC urb for submission to the bus.
318  */
319 static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep,
320 				       struct snd_urb_ctx *urb_ctx)
321 {
322 	int i, offs;
323 	struct urb *urb = urb_ctx->urb;
324 
325 	urb->dev = ep->chip->dev; /* we need to set this at each time */
326 
327 	switch (ep->type) {
328 	case SND_USB_ENDPOINT_TYPE_DATA:
329 		offs = 0;
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;
334 		}
335 
336 		urb->transfer_buffer_length = offs;
337 		urb->number_of_packets = urb_ctx->packets;
338 		break;
339 
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;
343 		break;
344 	}
345 }
346 
347 /* notify an error as XRUN to the assigned PCM data substream */
348 static void notify_xrun(struct snd_usb_endpoint *ep)
349 {
350 	struct snd_usb_substream *data_subs;
351 
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);
355 }
356 
357 static struct snd_usb_packet_info *
358 next_packet_fifo_enqueue(struct snd_usb_endpoint *ep)
359 {
360 	struct snd_usb_packet_info *p;
361 
362 	p = ep->next_packet + (ep->next_packet_head + ep->next_packet_queued) %
363 		ARRAY_SIZE(ep->next_packet);
364 	ep->next_packet_queued++;
365 	return p;
366 }
367 
368 static struct snd_usb_packet_info *
369 next_packet_fifo_dequeue(struct snd_usb_endpoint *ep)
370 {
371 	struct snd_usb_packet_info *p;
372 
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--;
377 	return p;
378 }
379 
380 /*
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
384  * nothing.
385  *
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.
389  *
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.
393  */
394 static void queue_pending_output_urbs(struct snd_usb_endpoint *ep)
395 {
396 	while (test_bit(EP_FLAG_RUNNING, &ep->flags)) {
397 
398 		unsigned long flags;
399 		struct snd_usb_packet_info *packet;
400 		struct snd_urb_ctx *ctx = NULL;
401 		int err, i;
402 
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);
410 
411 			packet = next_packet_fifo_dequeue(ep);
412 		}
413 		spin_unlock_irqrestore(&ep->lock, flags);
414 
415 		if (ctx == NULL)
416 			return;
417 
418 		/* copy over the length information */
419 		for (i = 0; i < packet->packets; i++)
420 			ctx->packet_size[i] = packet->packet_size[i];
421 
422 		/* call the data handler to fill in playback data */
423 		prepare_outbound_urb(ep, ctx);
424 
425 		err = usb_submit_urb(ctx->urb, GFP_ATOMIC);
426 		if (err < 0) {
427 			usb_audio_err(ep->chip,
428 				      "Unable to submit urb #%d: %d at %s\n",
429 				      ctx->index, err, __func__);
430 			notify_xrun(ep);
431 			return;
432 		}
433 
434 		set_bit(ctx->index, &ep->active_mask);
435 	}
436 }
437 
438 /*
439  * complete callback for urbs
440  */
441 static void snd_complete_urb(struct urb *urb)
442 {
443 	struct snd_urb_ctx *ctx = urb->context;
444 	struct snd_usb_endpoint *ep = ctx->ep;
445 	unsigned long flags;
446 	int err;
447 
448 	if (unlikely(urb->status == -ENOENT ||		/* unlinked */
449 		     urb->status == -ENODEV ||		/* device removed */
450 		     urb->status == -ECONNRESET ||	/* unlinked */
451 		     urb->status == -ESHUTDOWN))	/* device disabled */
452 		goto exit_clear;
453 	/* device disconnected */
454 	if (unlikely(atomic_read(&ep->chip->shutdown)))
455 		goto exit_clear;
456 
457 	if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
458 		goto exit_clear;
459 
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)))
464 			goto exit_clear;
465 
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);
472 			return;
473 		}
474 
475 		prepare_outbound_urb(ep, ctx);
476 		/* can be stopped during prepare callback */
477 		if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
478 			goto exit_clear;
479 	} else {
480 		retire_inbound_urb(ep, ctx);
481 		/* can be stopped during retire callback */
482 		if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
483 			goto exit_clear;
484 
485 		prepare_inbound_urb(ep, ctx);
486 	}
487 
488 	err = usb_submit_urb(urb, GFP_ATOMIC);
489 	if (err == 0)
490 		return;
491 
492 	usb_audio_err(ep->chip, "cannot submit urb (err = %d)\n", err);
493 	notify_xrun(ep);
494 
495 exit_clear:
496 	clear_bit(ctx->index, &ep->active_mask);
497 }
498 
499 /*
500  * Find or create a refcount object for the given interface
501  *
502  * The objects are released altogether in snd_usb_endpoint_free_all()
503  */
504 static struct snd_usb_iface_ref *
505 iface_ref_find(struct snd_usb_audio *chip, int iface)
506 {
507 	struct snd_usb_iface_ref *ip;
508 
509 	list_for_each_entry(ip, &chip->iface_ref_list, list)
510 		if (ip->iface == iface)
511 			return ip;
512 
513 	ip = kzalloc(sizeof(*ip), GFP_KERNEL);
514 	if (!ip)
515 		return NULL;
516 	ip->iface = iface;
517 	list_add_tail(&ip->list, &chip->iface_ref_list);
518 	return ip;
519 }
520 
521 /*
522  * Get the existing endpoint object corresponding EP
523  * Returns NULL if not present.
524  */
525 struct snd_usb_endpoint *
526 snd_usb_get_endpoint(struct snd_usb_audio *chip, int ep_num)
527 {
528 	struct snd_usb_endpoint *ep;
529 
530 	list_for_each_entry(ep, &chip->ep_list, list) {
531 		if (ep->ep_num == ep_num)
532 			return ep;
533 	}
534 
535 	return NULL;
536 }
537 
538 #define ep_type_name(type) \
539 	(type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync")
540 
541 /**
542  * snd_usb_add_endpoint: Add an endpoint to an USB audio chip
543  *
544  * @chip: The chip
545  * @ep_num: The number of the endpoint to use
546  * @type: SND_USB_ENDPOINT_TYPE_DATA or SND_USB_ENDPOINT_TYPE_SYNC
547  *
548  * If the requested endpoint has not been added to the given chip before,
549  * a new instance is created.
550  *
551  * Returns zero on success or a negative error code.
552  *
553  * New endpoints will be added to chip->ep_list and freed by
554  * calling snd_usb_endpoint_free_all().
555  *
556  * For SND_USB_ENDPOINT_TYPE_SYNC, the caller needs to guarantee that
557  * bNumEndpoints > 1 beforehand.
558  */
559 int snd_usb_add_endpoint(struct snd_usb_audio *chip, int ep_num, int type)
560 {
561 	struct snd_usb_endpoint *ep;
562 	bool is_playback;
563 
564 	ep = snd_usb_get_endpoint(chip, ep_num);
565 	if (ep)
566 		return 0;
567 
568 	usb_audio_dbg(chip, "Creating new %s endpoint #%x\n",
569 		      ep_type_name(type),
570 		      ep_num);
571 	ep = kzalloc(sizeof(*ep), GFP_KERNEL);
572 	if (!ep)
573 		return -ENOMEM;
574 
575 	ep->chip = chip;
576 	spin_lock_init(&ep->lock);
577 	ep->type = type;
578 	ep->ep_num = ep_num;
579 	INIT_LIST_HEAD(&ep->ready_playback_urbs);
580 
581 	is_playback = ((ep_num & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
582 	ep_num &= USB_ENDPOINT_NUMBER_MASK;
583 	if (is_playback)
584 		ep->pipe = usb_sndisocpipe(chip->dev, ep_num);
585 	else
586 		ep->pipe = usb_rcvisocpipe(chip->dev, ep_num);
587 
588 	list_add_tail(&ep->list, &chip->ep_list);
589 	return 0;
590 }
591 
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)
595 {
596 	struct usb_host_interface *alts;
597 	struct usb_endpoint_descriptor *desc;
598 
599 	alts = snd_usb_get_host_interface(chip, ep->iface, ep->altsetting);
600 	if (!alts)
601 		return;
602 
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;
611 	else
612 		ep->syncinterval = 3;
613 
614 	ep->syncmaxsize = le16_to_cpu(desc->wMaxPacketSize);
615 }
616 
617 static bool endpoint_compatible(struct snd_usb_endpoint *ep,
618 				const struct audioformat *fp,
619 				const struct snd_pcm_hw_params *params)
620 {
621 	if (!ep->opened)
622 		return false;
623 	if (ep->cur_audiofmt != fp)
624 		return false;
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))
629 		return false;
630 	return true;
631 }
632 
633 /*
634  * Check whether the given fp and hw params are compatbile with the current
635  * setup of the target EP for implicit feedback sync
636  */
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)
641 {
642 	bool ret;
643 
644 	mutex_lock(&chip->mutex);
645 	ret = endpoint_compatible(ep, fp, params);
646 	mutex_unlock(&chip->mutex);
647 	return ret;
648 }
649 
650 /*
651  * snd_usb_endpoint_open: Open the endpoint
652  *
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.
658  *
659  * Note that this function doesn't configure the endpoint.  The substream
660  * needs to set it up later via snd_usb_endpoint_configure().
661  */
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,
666 		      bool is_sync_ep)
667 {
668 	struct snd_usb_endpoint *ep;
669 	int ep_num = is_sync_ep ? fp->sync_ep : fp->endpoint;
670 
671 	mutex_lock(&chip->mutex);
672 	ep = snd_usb_get_endpoint(chip, ep_num);
673 	if (!ep) {
674 		usb_audio_err(chip, "Cannot find EP 0x%x to open\n", ep_num);
675 		goto unlock;
676 	}
677 
678 	if (!ep->opened) {
679 		if (is_sync_ep) {
680 			ep->iface = fp->sync_iface;
681 			ep->altsetting = fp->sync_altsetting;
682 			ep->ep_idx = fp->sync_ep_idx;
683 		} else {
684 			ep->iface = fp->iface;
685 			ep->altsetting = fp->altsetting;
686 			ep->ep_idx = fp->ep_idx;
687 		}
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);
690 
691 		ep->iface_ref = iface_ref_find(chip, ep->iface);
692 		if (!ep->iface_ref) {
693 			ep = NULL;
694 			goto unlock;
695 		}
696 
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);
706 
707 		if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
708 			endpoint_set_syncinterval(chip, ep);
709 
710 		ep->implicit_fb_sync = fp->implicit_fb;
711 		ep->need_setup = true;
712 
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);
718 
719 	} else {
720 		if (WARN_ON(!ep->iface_ref)) {
721 			ep = NULL;
722 			goto unlock;
723 		}
724 
725 		if (!endpoint_compatible(ep, fp, params)) {
726 			usb_audio_err(chip, "Incompatible EP setup for 0x%x\n",
727 				      ep_num);
728 			ep = NULL;
729 			goto unlock;
730 		}
731 
732 		usb_audio_dbg(chip, "Reopened EP 0x%x (count %d)\n",
733 			      ep_num, ep->opened);
734 	}
735 
736 	if (!ep->iface_ref->opened++)
737 		ep->iface_ref->need_setup = true;
738 
739 	ep->opened++;
740 
741  unlock:
742 	mutex_unlock(&chip->mutex);
743 	return ep;
744 }
745 
746 /*
747  * snd_usb_endpoint_set_sync: Link data and sync endpoints
748  *
749  * Pass NULL to sync_ep to unlink again
750  */
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)
754 {
755 	data_ep->sync_source = sync_ep;
756 }
757 
758 /*
759  * Set data endpoint callbacks and the assigned data stream
760  *
761  * Called at PCM trigger and cleanups.
762  * Pass NULL to deactivate each callback.
763  */
764 void snd_usb_endpoint_set_callback(struct snd_usb_endpoint *ep,
765 				   void (*prepare)(struct snd_usb_substream *subs,
766 						   struct urb *urb),
767 				   void (*retire)(struct snd_usb_substream *subs,
768 						  struct urb *urb),
769 				   struct snd_usb_substream *data_subs)
770 {
771 	ep->prepare_data_urb = prepare;
772 	ep->retire_data_urb = retire;
773 	WRITE_ONCE(ep->data_subs, data_subs);
774 }
775 
776 static int endpoint_set_interface(struct snd_usb_audio *chip,
777 				  struct snd_usb_endpoint *ep,
778 				  bool set)
779 {
780 	int altset = set ? ep->altsetting : 0;
781 	int err;
782 
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);
786 	if (err < 0) {
787 		usb_audio_err(chip, "%d:%d: usb_set_interface failed (%d)\n",
788 			      ep->iface, altset, err);
789 		return err;
790 	}
791 
792 	snd_usb_set_interface_quirk(chip);
793 	return 0;
794 }
795 
796 /*
797  * snd_usb_endpoint_close: Close the endpoint
798  *
799  * Unreference the already opened endpoint via snd_usb_endpoint_open().
800  */
801 void snd_usb_endpoint_close(struct snd_usb_audio *chip,
802 			    struct snd_usb_endpoint *ep)
803 {
804 	mutex_lock(&chip->mutex);
805 	usb_audio_dbg(chip, "Closing EP 0x%x (count %d)\n",
806 		      ep->ep_num, ep->opened);
807 
808 	if (!--ep->iface_ref->opened)
809 		endpoint_set_interface(chip, ep, false);
810 
811 	if (!--ep->opened) {
812 		ep->iface = 0;
813 		ep->altsetting = 0;
814 		ep->cur_audiofmt = NULL;
815 		ep->cur_rate = 0;
816 		ep->iface_ref = NULL;
817 		usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num);
818 	}
819 	mutex_unlock(&chip->mutex);
820 }
821 
822 /* Prepare for suspening EP, called from the main suspend handler */
823 void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep)
824 {
825 	ep->need_setup = true;
826 	if (ep->iface_ref)
827 		ep->iface_ref->need_setup = true;
828 }
829 
830 /*
831  *  wait until all urbs are processed.
832  */
833 static int wait_clear_urbs(struct snd_usb_endpoint *ep)
834 {
835 	unsigned long end_time = jiffies + msecs_to_jiffies(1000);
836 	int alive;
837 
838 	if (!test_bit(EP_FLAG_STOPPING, &ep->flags))
839 		return 0;
840 
841 	do {
842 		alive = bitmap_weight(&ep->active_mask, ep->nurbs);
843 		if (!alive)
844 			break;
845 
846 		schedule_timeout_uninterruptible(1);
847 	} while (time_before(jiffies, end_time));
848 
849 	if (alive)
850 		usb_audio_err(ep->chip,
851 			"timeout: still %d active urbs on EP #%x\n",
852 			alive, ep->ep_num);
853 	clear_bit(EP_FLAG_STOPPING, &ep->flags);
854 
855 	ep->sync_sink = NULL;
856 	snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
857 
858 	return 0;
859 }
860 
861 /* sync the pending stop operation;
862  * this function itself doesn't trigger the stop operation
863  */
864 void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep)
865 {
866 	if (ep)
867 		wait_clear_urbs(ep);
868 }
869 
870 /*
871  * Stop and unlink active urbs.
872  *
873  * This function checks and clears EP_FLAG_RUNNING state.
874  * When @wait_sync is set, it waits until all pending URBs are killed.
875  */
876 static int stop_and_unlink_urbs(struct snd_usb_endpoint *ep, bool force,
877 				bool wait_sync)
878 {
879 	unsigned int i;
880 
881 	if (!force && atomic_read(&ep->chip->shutdown)) /* to be sure... */
882 		return -EBADFD;
883 
884 	if (atomic_read(&ep->running))
885 		return -EBUSY;
886 
887 	if (!test_and_clear_bit(EP_FLAG_RUNNING, &ep->flags))
888 		goto out;
889 
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;
894 
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;
899 				usb_unlink_urb(u);
900 			}
901 		}
902 	}
903 
904  out:
905 	if (wait_sync)
906 		return wait_clear_urbs(ep);
907 	return 0;
908 }
909 
910 /*
911  * release an endpoint's urbs
912  */
913 static void release_urbs(struct snd_usb_endpoint *ep, int force)
914 {
915 	int i;
916 
917 	/* route incoming urbs to nirvana */
918 	snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
919 
920 	/* stop urbs */
921 	stop_and_unlink_urbs(ep, force, true);
922 
923 	for (i = 0; i < ep->nurbs; i++)
924 		release_urb_ctx(&ep->urb[i]);
925 
926 	usb_free_coherent(ep->chip->dev, SYNC_URBS * 4,
927 			  ep->syncbuf, ep->sync_dma);
928 
929 	ep->syncbuf = NULL;
930 	ep->nurbs = 0;
931 }
932 
933 /*
934  * configure a data endpoint
935  */
936 static int data_ep_set_params(struct snd_usb_endpoint *ep)
937 {
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));
946 
947 	usb_audio_dbg(chip, "Setting params for data EP 0x%x, pipe 0x%x\n",
948 		      ep->ep_num, ep->pipe);
949 
950 	if (ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) {
951 		/*
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.
955 		 */
956 		frame_bits += ep->cur_channels << 3;
957 	}
958 
959 	ep->datainterval = fmt->datainterval;
960 	ep->stride = frame_bits >> 3;
961 
962 	switch (ep->cur_format) {
963 	case SNDRV_PCM_FORMAT_U8:
964 		ep->silence_value = 0x80;
965 		break;
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;
972 		break;
973 	default:
974 		ep->silence_value = 0;
975 	}
976 
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.)
993 	 */
994 	maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) *
995 			 (frame_bits >> 3);
996 	if (tx_length_quirk)
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;
1002 
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);
1008 	}
1009 
1010 	if (ep->fill_max)
1011 		ep->curpacksize = ep->maxpacksize;
1012 	else
1013 		ep->curpacksize = maxsize;
1014 
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;
1018 	} else {
1019 		packs_per_ms = 1;
1020 		max_packs_per_urb = MAX_PACKS;
1021 	}
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);
1026 
1027 	/*
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.
1031 	 *
1032 	 * Playback endpoints with implicit sync much use the same parameters
1033 	 * as their corresponding capture endpoint.
1034 	 */
1035 	if (usb_pipein(ep->pipe) || ep->implicit_fb_sync) {
1036 
1037 		urb_packs = packs_per_ms;
1038 		/*
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
1042 		 * gaps.
1043 		 */
1044 		if (snd_usb_get_speed(chip->dev) == USB_SPEED_WIRELESS) {
1045 			int interval = ep->datainterval;
1046 			while (interval < 5) {
1047 				urb_packs <<= 1;
1048 				++interval;
1049 			}
1050 		}
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)
1054 			urb_packs >>= 1;
1055 		ep->nurbs = MAX_URBS;
1056 
1057 	/*
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.
1062 	 */
1063 	} else {
1064 		/* determine how small a packet can be */
1065 		minsize = (ep->freqn >> (16 - ep->datainterval)) *
1066 				(frame_bits >> 3);
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);
1071 
1072 		/* how many packets will contain an entire ALSA period? */
1073 		max_packs_per_period = DIV_ROUND_UP(ep->cur_period_bytes, minsize);
1074 
1075 		/* how many URBs will contain a period? */
1076 		urbs_per_period = DIV_ROUND_UP(max_packs_per_period,
1077 				max_packs_per_urb);
1078 		/* how many packets are needed in each URB? */
1079 		urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period);
1080 
1081 		/* limit the number of frames in a single URB */
1082 		ep->max_urb_frames = DIV_ROUND_UP(ep->cur_period_frames,
1083 						  urbs_per_period);
1084 
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);
1089 	}
1090 
1091 	/* allocate and initialize data urbs */
1092 	for (i = 0; i < ep->nurbs; i++) {
1093 		struct snd_urb_ctx *u = &ep->urb[i];
1094 		u->index = i;
1095 		u->ep = ep;
1096 		u->packets = urb_packs;
1097 		u->buffer_size = maxsize * u->packets;
1098 
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);
1102 		if (!u->urb)
1103 			goto out_of_memory;
1104 
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)
1109 			goto out_of_memory;
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);
1116 	}
1117 
1118 	return 0;
1119 
1120 out_of_memory:
1121 	release_urbs(ep, 0);
1122 	return -ENOMEM;
1123 }
1124 
1125 /*
1126  * configure a sync endpoint
1127  */
1128 static int sync_ep_set_params(struct snd_usb_endpoint *ep)
1129 {
1130 	struct snd_usb_audio *chip = ep->chip;
1131 	int i;
1132 
1133 	usb_audio_dbg(chip, "Setting params for sync EP 0x%x, pipe 0x%x\n",
1134 		      ep->ep_num, ep->pipe);
1135 
1136 	ep->syncbuf = usb_alloc_coherent(chip->dev, SYNC_URBS * 4,
1137 					 GFP_KERNEL, &ep->sync_dma);
1138 	if (!ep->syncbuf)
1139 		return -ENOMEM;
1140 
1141 	for (i = 0; i < SYNC_URBS; i++) {
1142 		struct snd_urb_ctx *u = &ep->urb[i];
1143 		u->index = i;
1144 		u->ep = ep;
1145 		u->packets = 1;
1146 		u->urb = usb_alloc_urb(1, GFP_KERNEL);
1147 		if (!u->urb)
1148 			goto out_of_memory;
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;
1158 	}
1159 
1160 	ep->nurbs = SYNC_URBS;
1161 
1162 	return 0;
1163 
1164 out_of_memory:
1165 	release_urbs(ep, 0);
1166 	return -ENOMEM;
1167 }
1168 
1169 /*
1170  * snd_usb_endpoint_set_params: configure an snd_usb_endpoint
1171  *
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.
1175  */
1176 static int snd_usb_endpoint_set_params(struct snd_usb_audio *chip,
1177 				       struct snd_usb_endpoint *ep)
1178 {
1179 	const struct audioformat *fmt = ep->cur_audiofmt;
1180 	int err;
1181 
1182 	/* release old buffers, if any */
1183 	release_urbs(ep, 0);
1184 
1185 	ep->datainterval = fmt->datainterval;
1186 	ep->maxpacksize = fmt->maxpacksize;
1187 	ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX);
1188 
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;
1192 	} else {
1193 		ep->freqn = get_usb_high_speed_rate(ep->cur_rate);
1194 		ep->pps = 8000 >> ep->datainterval;
1195 	}
1196 
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;
1200 
1201 	/* calculate the frequency in 16.16 format */
1202 	ep->freqm = ep->freqn;
1203 	ep->freqshift = INT_MIN;
1204 
1205 	ep->phase = 0;
1206 
1207 	switch (ep->type) {
1208 	case  SND_USB_ENDPOINT_TYPE_DATA:
1209 		err = data_ep_set_params(ep);
1210 		break;
1211 	case  SND_USB_ENDPOINT_TYPE_SYNC:
1212 		err = sync_ep_set_params(ep);
1213 		break;
1214 	default:
1215 		err = -EINVAL;
1216 	}
1217 
1218 	usb_audio_dbg(chip, "Set up %d URBS, ret=%d\n", ep->nurbs, err);
1219 
1220 	if (err < 0)
1221 		return err;
1222 
1223 	/* some unit conversions in runtime */
1224 	ep->maxframesize = ep->maxpacksize / ep->cur_frame_bytes;
1225 	ep->curframesize = ep->curpacksize / ep->cur_frame_bytes;
1226 
1227 	return 0;
1228 }
1229 
1230 /*
1231  * snd_usb_endpoint_configure: Configure the endpoint
1232  *
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.
1237  *
1238  * This returns zero if unchanged, 1 if the configuration has changed,
1239  * or a negative error code.
1240  */
1241 int snd_usb_endpoint_configure(struct snd_usb_audio *chip,
1242 			       struct snd_usb_endpoint *ep)
1243 {
1244 	bool iface_first;
1245 	int err = 0;
1246 
1247 	mutex_lock(&chip->mutex);
1248 	if (WARN_ON(!ep->iface_ref))
1249 		goto unlock;
1250 	if (!ep->need_setup)
1251 		goto unlock;
1252 
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);
1256 		if (err < 0)
1257 			goto unlock;
1258 		goto done;
1259 	}
1260 
1261 	/* Need to deselect altsetting at first */
1262 	endpoint_set_interface(chip, ep, false);
1263 
1264 	/* Some UAC1 devices (e.g. Yamaha THR10) need the host interface
1265 	 * to be set up before parameter setups
1266 	 */
1267 	iface_first = ep->cur_audiofmt->protocol == UAC_VERSION_1;
1268 	if (iface_first) {
1269 		err = endpoint_set_interface(chip, ep, true);
1270 		if (err < 0)
1271 			goto unlock;
1272 	}
1273 
1274 	err = snd_usb_init_pitch(chip, ep->cur_audiofmt);
1275 	if (err < 0)
1276 		goto unlock;
1277 
1278 	err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, ep->cur_rate);
1279 	if (err < 0)
1280 		goto unlock;
1281 
1282 	err = snd_usb_endpoint_set_params(chip, ep);
1283 	if (err < 0)
1284 		goto unlock;
1285 
1286 	err = snd_usb_select_mode_quirk(chip, ep->cur_audiofmt);
1287 	if (err < 0)
1288 		goto unlock;
1289 
1290 	/* for UAC2/3, enable the interface altset here at last */
1291 	if (!iface_first) {
1292 		err = endpoint_set_interface(chip, ep, true);
1293 		if (err < 0)
1294 			goto unlock;
1295 	}
1296 
1297 	ep->iface_ref->need_setup = false;
1298 
1299  done:
1300 	ep->need_setup = false;
1301 	err = 1;
1302 
1303 unlock:
1304 	mutex_unlock(&chip->mutex);
1305 	return err;
1306 }
1307 
1308 /**
1309  * snd_usb_endpoint_start: start an snd_usb_endpoint
1310  *
1311  * @ep: the endpoint to start
1312  *
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.
1316  *
1317  * Must be balanced to calls of snd_usb_endpoint_stop().
1318  *
1319  * Returns an error if the URB submission failed, 0 in all other cases.
1320  */
1321 int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)
1322 {
1323 	int err;
1324 	unsigned int i;
1325 
1326 	if (atomic_read(&ep->chip->shutdown))
1327 		return -EBADFD;
1328 
1329 	if (ep->sync_source)
1330 		WRITE_ONCE(ep->sync_source->sync_sink, ep);
1331 
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));
1335 
1336 	/* already running? */
1337 	if (atomic_inc_return(&ep->running) != 1)
1338 		return 0;
1339 
1340 	ep->active_mask = 0;
1341 	ep->unlink_mask = 0;
1342 	ep->phase = 0;
1343 	ep->sample_accum = 0;
1344 
1345 	snd_usb_endpoint_start_quirk(ep);
1346 
1347 	/*
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.
1352 	 */
1353 
1354 	set_bit(EP_FLAG_RUNNING, &ep->flags);
1355 
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);
1360 		}
1361 
1362 		usb_audio_dbg(ep->chip, "No URB submission due to implicit fb sync\n");
1363 		return 0;
1364 	}
1365 
1366 	for (i = 0; i < ep->nurbs; i++) {
1367 		struct urb *urb = ep->urb[i].urb;
1368 
1369 		if (snd_BUG_ON(!urb))
1370 			goto __error;
1371 
1372 		if (usb_pipeout(ep->pipe)) {
1373 			prepare_outbound_urb(ep, urb->context);
1374 		} else {
1375 			prepare_inbound_urb(ep, urb->context);
1376 		}
1377 
1378 		err = usb_submit_urb(urb, GFP_ATOMIC);
1379 		if (err < 0) {
1380 			usb_audio_err(ep->chip,
1381 				"cannot submit urb %d, error %d: %s\n",
1382 				i, err, usb_error_string(err));
1383 			goto __error;
1384 		}
1385 		set_bit(i, &ep->active_mask);
1386 	}
1387 
1388 	usb_audio_dbg(ep->chip, "%d URBs submitted for EP 0x%x\n",
1389 		      ep->nurbs, ep->ep_num);
1390 	return 0;
1391 
1392 __error:
1393 	snd_usb_endpoint_stop(ep);
1394 	return -EPIPE;
1395 }
1396 
1397 /**
1398  * snd_usb_endpoint_stop: stop an snd_usb_endpoint
1399  *
1400  * @ep: the endpoint to stop (may be NULL)
1401  *
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.
1405  *
1406  * Must be balanced to calls of snd_usb_endpoint_start().
1407  *
1408  * The caller needs to synchronize the pending stop operation via
1409  * snd_usb_endpoint_sync_pending_stop().
1410  */
1411 void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep)
1412 {
1413 	if (!ep)
1414 		return;
1415 
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));
1419 
1420 	if (snd_BUG_ON(!atomic_read(&ep->running)))
1421 		return;
1422 
1423 	if (ep->sync_source)
1424 		WRITE_ONCE(ep->sync_source->sync_sink, NULL);
1425 
1426 	if (!atomic_dec_return(&ep->running))
1427 		stop_and_unlink_urbs(ep, false, false);
1428 }
1429 
1430 /**
1431  * snd_usb_endpoint_release: Tear down an snd_usb_endpoint
1432  *
1433  * @ep: the endpoint to release
1434  *
1435  * This function does not care for the endpoint's running count but will tear
1436  * down all the streaming URBs immediately.
1437  */
1438 void snd_usb_endpoint_release(struct snd_usb_endpoint *ep)
1439 {
1440 	release_urbs(ep, 1);
1441 }
1442 
1443 /**
1444  * snd_usb_endpoint_free_all: Free the resources of an snd_usb_endpoint
1445  * @card: The chip
1446  *
1447  * This free all endpoints and those resources
1448  */
1449 void snd_usb_endpoint_free_all(struct snd_usb_audio *chip)
1450 {
1451 	struct snd_usb_endpoint *ep, *en;
1452 	struct snd_usb_iface_ref *ip, *in;
1453 
1454 	list_for_each_entry_safe(ep, en, &chip->ep_list, list)
1455 		kfree(ep);
1456 
1457 	list_for_each_entry_safe(ip, in, &chip->iface_ref_list, list)
1458 		kfree(ip);
1459 }
1460 
1461 /*
1462  * snd_usb_handle_sync_urb: parse an USB sync packet
1463  *
1464  * @ep: the endpoint to handle the packet
1465  * @sender: the sending endpoint
1466  * @urb: the received packet
1467  *
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.
1470  */
1471 static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
1472 				    struct snd_usb_endpoint *sender,
1473 				    const struct urb *urb)
1474 {
1475 	int shift;
1476 	unsigned int f;
1477 	unsigned long flags;
1478 
1479 	snd_BUG_ON(ep == sender);
1480 
1481 	/*
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.
1486 	 */
1487 	if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
1488 	    atomic_read(&ep->running)) {
1489 
1490 		/* implicit feedback case */
1491 		int i, bytes = 0;
1492 		struct snd_urb_ctx *in_ctx;
1493 		struct snd_usb_packet_info *out_packet;
1494 
1495 		in_ctx = urb->context;
1496 
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;
1501 
1502 		/*
1503 		 * skip empty packets. At least M-Audio's Fast Track Ultra stops
1504 		 * streaming once it received a 0-byte OUT URB
1505 		 */
1506 		if (bytes == 0)
1507 			return;
1508 
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",
1514 				      ep->ep_num);
1515 			notify_xrun(ep);
1516 			return;
1517 		}
1518 
1519 		out_packet = next_packet_fifo_enqueue(ep);
1520 
1521 		/*
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.
1529 		 */
1530 
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;
1536 			else
1537 				out_packet->packet_size[i] = 0;
1538 		}
1539 
1540 		spin_unlock_irqrestore(&ep->lock, flags);
1541 		queue_pending_output_urbs(ep);
1542 
1543 		return;
1544 	}
1545 
1546 	/*
1547 	 * process after playback sync complete
1548 	 *
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
1551 	 * microframe.
1552 	 *
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.
1556 	 *
1557 	 * Therefore, we cannot predict what format any particular device uses
1558 	 * and must detect it automatically.
1559 	 */
1560 
1561 	if (urb->iso_frame_desc[0].status != 0 ||
1562 	    urb->iso_frame_desc[0].actual_length < 3)
1563 		return;
1564 
1565 	f = le32_to_cpup(urb->transfer_buffer);
1566 	if (urb->iso_frame_desc[0].actual_length == 3)
1567 		f &= 0x00ffffff;
1568 	else
1569 		f &= 0x0fffffff;
1570 
1571 	if (f == 0)
1572 		return;
1573 
1574 	if (unlikely(sender->tenor_fb_quirk)) {
1575 		/*
1576 		 * Devices based on Tenor 8802 chipsets (TEAC UD-H01
1577 		 * and others) sometimes change the feedback value
1578 		 * by +/- 0x1.0000.
1579 		 */
1580 		if (f < ep->freqn - 0x8000)
1581 			f += 0xf000;
1582 		else if (f > ep->freqn + 0x8000)
1583 			f -= 0xf000;
1584 	} else if (unlikely(ep->freqshift == INT_MIN)) {
1585 		/*
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%.
1590 		 */
1591 		shift = 0;
1592 		while (f < ep->freqn - ep->freqn / 4) {
1593 			f <<= 1;
1594 			shift++;
1595 		}
1596 		while (f > ep->freqn + ep->freqn / 2) {
1597 			f >>= 1;
1598 			shift--;
1599 		}
1600 		ep->freqshift = shift;
1601 	} else if (ep->freqshift >= 0)
1602 		f <<= ep->freqshift;
1603 	else
1604 		f >>= -ep->freqshift;
1605 
1606 	if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) {
1607 		/*
1608 		 * If the frequency looks valid, set it.
1609 		 * This value is referred to in prepare_playback_urb().
1610 		 */
1611 		spin_lock_irqsave(&ep->lock, flags);
1612 		ep->freqm = f;
1613 		spin_unlock_irqrestore(&ep->lock, flags);
1614 	} else {
1615 		/*
1616 		 * Out of range; maybe the shift value is wrong.
1617 		 * Reset it so that we autodetect again the next time.
1618 		 */
1619 		ep->freqshift = INT_MIN;
1620 	}
1621 }
1622 
1623