xref: /openbmc/linux/sound/usb/endpoint.c (revision 66a28915)
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 /*
28  * snd_usb_endpoint is a model that abstracts everything related to an
29  * USB endpoint and its streaming.
30  *
31  * There are functions to activate and deactivate the streaming URBs and
32  * optional callbacks to let the pcm logic handle the actual content of the
33  * packets for playback and record. Thus, the bus streaming and the audio
34  * handlers are fully decoupled.
35  *
36  * There are two different types of endpoints in audio applications.
37  *
38  * SND_USB_ENDPOINT_TYPE_DATA handles full audio data payload for both
39  * inbound and outbound traffic.
40  *
41  * SND_USB_ENDPOINT_TYPE_SYNC endpoints are for inbound traffic only and
42  * expect the payload to carry Q10.14 / Q16.16 formatted sync information
43  * (3 or 4 bytes).
44  *
45  * Each endpoint has to be configured prior to being used by calling
46  * snd_usb_endpoint_set_params().
47  *
48  * The model incorporates a reference counting, so that multiple users
49  * can call snd_usb_endpoint_start() and snd_usb_endpoint_stop(), and
50  * only the first user will effectively start the URBs, and only the last
51  * one to stop it will tear the URBs down again.
52  */
53 
54 /*
55  * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
56  * this will overflow at approx 524 kHz
57  */
58 static inline unsigned get_usb_full_speed_rate(unsigned int rate)
59 {
60 	return ((rate << 13) + 62) / 125;
61 }
62 
63 /*
64  * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
65  * this will overflow at approx 4 MHz
66  */
67 static inline unsigned get_usb_high_speed_rate(unsigned int rate)
68 {
69 	return ((rate << 10) + 62) / 125;
70 }
71 
72 /*
73  * release a urb data
74  */
75 static void release_urb_ctx(struct snd_urb_ctx *u)
76 {
77 	if (u->buffer_size)
78 		usb_free_coherent(u->ep->chip->dev, u->buffer_size,
79 				  u->urb->transfer_buffer,
80 				  u->urb->transfer_dma);
81 	usb_free_urb(u->urb);
82 	u->urb = NULL;
83 }
84 
85 static const char *usb_error_string(int err)
86 {
87 	switch (err) {
88 	case -ENODEV:
89 		return "no device";
90 	case -ENOENT:
91 		return "endpoint not enabled";
92 	case -EPIPE:
93 		return "endpoint stalled";
94 	case -ENOSPC:
95 		return "not enough bandwidth";
96 	case -ESHUTDOWN:
97 		return "device disabled";
98 	case -EHOSTUNREACH:
99 		return "device suspended";
100 	case -EINVAL:
101 	case -EAGAIN:
102 	case -EFBIG:
103 	case -EMSGSIZE:
104 		return "internal error";
105 	default:
106 		return "unknown error";
107 	}
108 }
109 
110 /**
111  * snd_usb_endpoint_implicit_feedback_sink: Report endpoint usage type
112  *
113  * @ep: The snd_usb_endpoint
114  *
115  * Determine whether an endpoint is driven by an implicit feedback
116  * data endpoint source.
117  */
118 int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep)
119 {
120 	return  ep->implicit_fb_sync && usb_pipeout(ep->pipe);
121 }
122 
123 /*
124  * Return the number of samples to be sent in the next packet
125  * for streaming based on information derived from sync endpoints
126  *
127  * This won't be used for implicit feedback which takes the packet size
128  * returned from the sync source
129  */
130 static int slave_next_packet_size(struct snd_usb_endpoint *ep)
131 {
132 	unsigned long flags;
133 	int ret;
134 
135 	if (ep->fill_max)
136 		return ep->maxframesize;
137 
138 	spin_lock_irqsave(&ep->lock, flags);
139 	ep->phase = (ep->phase & 0xffff)
140 		+ (ep->freqm << ep->datainterval);
141 	ret = min(ep->phase >> 16, ep->maxframesize);
142 	spin_unlock_irqrestore(&ep->lock, flags);
143 
144 	return ret;
145 }
146 
147 /*
148  * Return the number of samples to be sent in the next packet
149  * for adaptive and synchronous endpoints
150  */
151 static int next_packet_size(struct snd_usb_endpoint *ep)
152 {
153 	int ret;
154 
155 	if (ep->fill_max)
156 		return ep->maxframesize;
157 
158 	ep->sample_accum += ep->sample_rem;
159 	if (ep->sample_accum >= ep->pps) {
160 		ep->sample_accum -= ep->pps;
161 		ret = ep->packsize[1];
162 	} else {
163 		ret = ep->packsize[0];
164 	}
165 
166 	return ret;
167 }
168 
169 /*
170  * snd_usb_endpoint_next_packet_size: Return the number of samples to be sent
171  * in the next packet
172  */
173 int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep,
174 				      struct snd_urb_ctx *ctx, int idx)
175 {
176 	if (ctx->packet_size[idx])
177 		return ctx->packet_size[idx];
178 	else if (ep->sync_source)
179 		return slave_next_packet_size(ep);
180 	else
181 		return next_packet_size(ep);
182 }
183 
184 static void call_retire_callback(struct snd_usb_endpoint *ep,
185 				 struct urb *urb)
186 {
187 	struct snd_usb_substream *data_subs;
188 
189 	data_subs = READ_ONCE(ep->data_subs);
190 	if (data_subs && ep->retire_data_urb)
191 		ep->retire_data_urb(data_subs, urb);
192 }
193 
194 static void retire_outbound_urb(struct snd_usb_endpoint *ep,
195 				struct snd_urb_ctx *urb_ctx)
196 {
197 	call_retire_callback(ep, urb_ctx->urb);
198 }
199 
200 static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
201 				    struct snd_usb_endpoint *sender,
202 				    const struct urb *urb);
203 
204 static void retire_inbound_urb(struct snd_usb_endpoint *ep,
205 			       struct snd_urb_ctx *urb_ctx)
206 {
207 	struct urb *urb = urb_ctx->urb;
208 	struct snd_usb_endpoint *sync_sink;
209 
210 	if (unlikely(ep->skip_packets > 0)) {
211 		ep->skip_packets--;
212 		return;
213 	}
214 
215 	sync_sink = READ_ONCE(ep->sync_sink);
216 	if (sync_sink)
217 		snd_usb_handle_sync_urb(sync_sink, ep, urb);
218 
219 	call_retire_callback(ep, urb);
220 }
221 
222 static void prepare_silent_urb(struct snd_usb_endpoint *ep,
223 			       struct snd_urb_ctx *ctx)
224 {
225 	struct urb *urb = ctx->urb;
226 	unsigned int offs = 0;
227 	unsigned int extra = 0;
228 	__le32 packet_length;
229 	int i;
230 
231 	/* For tx_length_quirk, put packet length at start of packet */
232 	if (ep->chip->tx_length_quirk)
233 		extra = sizeof(packet_length);
234 
235 	for (i = 0; i < ctx->packets; ++i) {
236 		unsigned int offset;
237 		unsigned int length;
238 		int counts;
239 
240 		counts = snd_usb_endpoint_next_packet_size(ep, ctx, i);
241 		length = counts * ep->stride; /* number of silent bytes */
242 		offset = offs * ep->stride + extra * i;
243 		urb->iso_frame_desc[i].offset = offset;
244 		urb->iso_frame_desc[i].length = length + extra;
245 		if (extra) {
246 			packet_length = cpu_to_le32(length);
247 			memcpy(urb->transfer_buffer + offset,
248 			       &packet_length, sizeof(packet_length));
249 		}
250 		memset(urb->transfer_buffer + offset + extra,
251 		       ep->silence_value, length);
252 		offs += counts;
253 	}
254 
255 	urb->number_of_packets = ctx->packets;
256 	urb->transfer_buffer_length = offs * ep->stride + ctx->packets * extra;
257 }
258 
259 /*
260  * Prepare a PLAYBACK urb for submission to the bus.
261  */
262 static void prepare_outbound_urb(struct snd_usb_endpoint *ep,
263 				 struct snd_urb_ctx *ctx)
264 {
265 	struct urb *urb = ctx->urb;
266 	unsigned char *cp = urb->transfer_buffer;
267 	struct snd_usb_substream *data_subs;
268 
269 	urb->dev = ep->chip->dev; /* we need to set this at each time */
270 
271 	switch (ep->type) {
272 	case SND_USB_ENDPOINT_TYPE_DATA:
273 		data_subs = READ_ONCE(ep->data_subs);
274 		if (data_subs && ep->prepare_data_urb)
275 			ep->prepare_data_urb(data_subs, urb);
276 		else /* no data provider, so send silence */
277 			prepare_silent_urb(ep, ctx);
278 		break;
279 
280 	case SND_USB_ENDPOINT_TYPE_SYNC:
281 		if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) {
282 			/*
283 			 * fill the length and offset of each urb descriptor.
284 			 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
285 			 */
286 			urb->iso_frame_desc[0].length = 4;
287 			urb->iso_frame_desc[0].offset = 0;
288 			cp[0] = ep->freqn;
289 			cp[1] = ep->freqn >> 8;
290 			cp[2] = ep->freqn >> 16;
291 			cp[3] = ep->freqn >> 24;
292 		} else {
293 			/*
294 			 * fill the length and offset of each urb descriptor.
295 			 * the fixed 10.14 frequency is passed through the pipe.
296 			 */
297 			urb->iso_frame_desc[0].length = 3;
298 			urb->iso_frame_desc[0].offset = 0;
299 			cp[0] = ep->freqn >> 2;
300 			cp[1] = ep->freqn >> 10;
301 			cp[2] = ep->freqn >> 18;
302 		}
303 
304 		break;
305 	}
306 }
307 
308 /*
309  * Prepare a CAPTURE or SYNC urb for submission to the bus.
310  */
311 static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep,
312 				       struct snd_urb_ctx *urb_ctx)
313 {
314 	int i, offs;
315 	struct urb *urb = urb_ctx->urb;
316 
317 	urb->dev = ep->chip->dev; /* we need to set this at each time */
318 
319 	switch (ep->type) {
320 	case SND_USB_ENDPOINT_TYPE_DATA:
321 		offs = 0;
322 		for (i = 0; i < urb_ctx->packets; i++) {
323 			urb->iso_frame_desc[i].offset = offs;
324 			urb->iso_frame_desc[i].length = ep->curpacksize;
325 			offs += ep->curpacksize;
326 		}
327 
328 		urb->transfer_buffer_length = offs;
329 		urb->number_of_packets = urb_ctx->packets;
330 		break;
331 
332 	case SND_USB_ENDPOINT_TYPE_SYNC:
333 		urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize);
334 		urb->iso_frame_desc[0].offset = 0;
335 		break;
336 	}
337 }
338 
339 /* notify an error as XRUN to the assigned PCM data substream */
340 static void notify_xrun(struct snd_usb_endpoint *ep)
341 {
342 	struct snd_usb_substream *data_subs;
343 
344 	data_subs = READ_ONCE(ep->data_subs);
345 	if (data_subs && data_subs->pcm_substream)
346 		snd_pcm_stop_xrun(data_subs->pcm_substream);
347 }
348 
349 static struct snd_usb_packet_info *
350 next_packet_fifo_enqueue(struct snd_usb_endpoint *ep)
351 {
352 	struct snd_usb_packet_info *p;
353 
354 	p = ep->next_packet + (ep->next_packet_head + ep->next_packet_queued) %
355 		ARRAY_SIZE(ep->next_packet);
356 	ep->next_packet_queued++;
357 	return p;
358 }
359 
360 static struct snd_usb_packet_info *
361 next_packet_fifo_dequeue(struct snd_usb_endpoint *ep)
362 {
363 	struct snd_usb_packet_info *p;
364 
365 	p = ep->next_packet + ep->next_packet_head;
366 	ep->next_packet_head++;
367 	ep->next_packet_head %= ARRAY_SIZE(ep->next_packet);
368 	ep->next_packet_queued--;
369 	return p;
370 }
371 
372 /*
373  * Send output urbs that have been prepared previously. URBs are dequeued
374  * from ep->ready_playback_urbs and in case there aren't any available
375  * or there are no packets that have been prepared, this function does
376  * nothing.
377  *
378  * The reason why the functionality of sending and preparing URBs is separated
379  * is that host controllers don't guarantee the order in which they return
380  * inbound and outbound packets to their submitters.
381  *
382  * This function is only used for implicit feedback endpoints. For endpoints
383  * driven by dedicated sync endpoints, URBs are immediately re-submitted
384  * from their completion handler.
385  */
386 static void queue_pending_output_urbs(struct snd_usb_endpoint *ep)
387 {
388 	while (test_bit(EP_FLAG_RUNNING, &ep->flags)) {
389 
390 		unsigned long flags;
391 		struct snd_usb_packet_info *packet;
392 		struct snd_urb_ctx *ctx = NULL;
393 		int err, i;
394 
395 		spin_lock_irqsave(&ep->lock, flags);
396 		if (ep->next_packet_queued > 0 &&
397 		    !list_empty(&ep->ready_playback_urbs)) {
398 			/* take URB out of FIFO */
399 			ctx = list_first_entry(&ep->ready_playback_urbs,
400 					       struct snd_urb_ctx, ready_list);
401 			list_del_init(&ctx->ready_list);
402 
403 			packet = next_packet_fifo_dequeue(ep);
404 		}
405 		spin_unlock_irqrestore(&ep->lock, flags);
406 
407 		if (ctx == NULL)
408 			return;
409 
410 		/* copy over the length information */
411 		for (i = 0; i < packet->packets; i++)
412 			ctx->packet_size[i] = packet->packet_size[i];
413 
414 		/* call the data handler to fill in playback data */
415 		prepare_outbound_urb(ep, ctx);
416 
417 		err = usb_submit_urb(ctx->urb, GFP_ATOMIC);
418 		if (err < 0) {
419 			usb_audio_err(ep->chip,
420 				      "Unable to submit urb #%d: %d at %s\n",
421 				      ctx->index, err, __func__);
422 			notify_xrun(ep);
423 			return;
424 		}
425 
426 		set_bit(ctx->index, &ep->active_mask);
427 	}
428 }
429 
430 /*
431  * complete callback for urbs
432  */
433 static void snd_complete_urb(struct urb *urb)
434 {
435 	struct snd_urb_ctx *ctx = urb->context;
436 	struct snd_usb_endpoint *ep = ctx->ep;
437 	unsigned long flags;
438 	int err;
439 
440 	if (unlikely(urb->status == -ENOENT ||		/* unlinked */
441 		     urb->status == -ENODEV ||		/* device removed */
442 		     urb->status == -ECONNRESET ||	/* unlinked */
443 		     urb->status == -ESHUTDOWN))	/* device disabled */
444 		goto exit_clear;
445 	/* device disconnected */
446 	if (unlikely(atomic_read(&ep->chip->shutdown)))
447 		goto exit_clear;
448 
449 	if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
450 		goto exit_clear;
451 
452 	if (usb_pipeout(ep->pipe)) {
453 		retire_outbound_urb(ep, ctx);
454 		/* can be stopped during retire callback */
455 		if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
456 			goto exit_clear;
457 
458 		if (snd_usb_endpoint_implicit_feedback_sink(ep)) {
459 			spin_lock_irqsave(&ep->lock, flags);
460 			list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
461 			clear_bit(ctx->index, &ep->active_mask);
462 			spin_unlock_irqrestore(&ep->lock, flags);
463 			queue_pending_output_urbs(ep);
464 			return;
465 		}
466 
467 		prepare_outbound_urb(ep, ctx);
468 		/* can be stopped during prepare callback */
469 		if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
470 			goto exit_clear;
471 	} else {
472 		retire_inbound_urb(ep, ctx);
473 		/* can be stopped during retire callback */
474 		if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags)))
475 			goto exit_clear;
476 
477 		prepare_inbound_urb(ep, ctx);
478 	}
479 
480 	err = usb_submit_urb(urb, GFP_ATOMIC);
481 	if (err == 0)
482 		return;
483 
484 	usb_audio_err(ep->chip, "cannot submit urb (err = %d)\n", err);
485 	notify_xrun(ep);
486 
487 exit_clear:
488 	clear_bit(ctx->index, &ep->active_mask);
489 }
490 
491 /*
492  * Get the existing endpoint object corresponding EP
493  * Returns NULL if not present.
494  */
495 struct snd_usb_endpoint *
496 snd_usb_get_endpoint(struct snd_usb_audio *chip, int ep_num)
497 {
498 	struct snd_usb_endpoint *ep;
499 
500 	list_for_each_entry(ep, &chip->ep_list, list) {
501 		if (ep->ep_num == ep_num)
502 			return ep;
503 	}
504 
505 	return NULL;
506 }
507 
508 #define ep_type_name(type) \
509 	(type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync")
510 
511 /**
512  * snd_usb_add_endpoint: Add an endpoint to an USB audio chip
513  *
514  * @chip: The chip
515  * @ep_num: The number of the endpoint to use
516  * @type: SND_USB_ENDPOINT_TYPE_DATA or SND_USB_ENDPOINT_TYPE_SYNC
517  *
518  * If the requested endpoint has not been added to the given chip before,
519  * a new instance is created.
520  *
521  * Returns zero on success or a negative error code.
522  *
523  * New endpoints will be added to chip->ep_list and must be freed by
524  * calling snd_usb_endpoint_free().
525  *
526  * For SND_USB_ENDPOINT_TYPE_SYNC, the caller needs to guarantee that
527  * bNumEndpoints > 1 beforehand.
528  */
529 int snd_usb_add_endpoint(struct snd_usb_audio *chip, int ep_num, int type)
530 {
531 	struct snd_usb_endpoint *ep;
532 	bool is_playback;
533 
534 	ep = snd_usb_get_endpoint(chip, ep_num);
535 	if (ep)
536 		return 0;
537 
538 	usb_audio_dbg(chip, "Creating new %s endpoint #%x\n",
539 		      ep_type_name(type),
540 		      ep_num);
541 	ep = kzalloc(sizeof(*ep), GFP_KERNEL);
542 	if (!ep)
543 		return -ENOMEM;
544 
545 	ep->chip = chip;
546 	spin_lock_init(&ep->lock);
547 	ep->type = type;
548 	ep->ep_num = ep_num;
549 	INIT_LIST_HEAD(&ep->ready_playback_urbs);
550 
551 	is_playback = ((ep_num & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT);
552 	ep_num &= USB_ENDPOINT_NUMBER_MASK;
553 	if (is_playback)
554 		ep->pipe = usb_sndisocpipe(chip->dev, ep_num);
555 	else
556 		ep->pipe = usb_rcvisocpipe(chip->dev, ep_num);
557 
558 	list_add_tail(&ep->list, &chip->ep_list);
559 	return 0;
560 }
561 
562 /* Set up syncinterval and maxsyncsize for a sync EP */
563 static void endpoint_set_syncinterval(struct snd_usb_audio *chip,
564 				      struct snd_usb_endpoint *ep)
565 {
566 	struct usb_host_interface *alts;
567 	struct usb_endpoint_descriptor *desc;
568 
569 	alts = snd_usb_get_host_interface(chip, ep->iface, ep->altsetting);
570 	if (!alts)
571 		return;
572 
573 	desc = get_endpoint(alts, ep->ep_idx);
574 	if (desc->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
575 	    desc->bRefresh >= 1 && desc->bRefresh <= 9)
576 		ep->syncinterval = desc->bRefresh;
577 	else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL)
578 		ep->syncinterval = 1;
579 	else if (desc->bInterval >= 1 && desc->bInterval <= 16)
580 		ep->syncinterval = desc->bInterval - 1;
581 	else
582 		ep->syncinterval = 3;
583 
584 	ep->syncmaxsize = le16_to_cpu(desc->wMaxPacketSize);
585 }
586 
587 static bool endpoint_compatible(struct snd_usb_endpoint *ep,
588 				const struct audioformat *fp,
589 				const struct snd_pcm_hw_params *params)
590 {
591 	if (!ep->opened)
592 		return false;
593 	if (ep->cur_audiofmt != fp)
594 		return false;
595 	if (ep->cur_rate != params_rate(params) ||
596 	    ep->cur_format != params_format(params) ||
597 	    ep->cur_period_frames != params_period_size(params) ||
598 	    ep->cur_buffer_periods != params_periods(params))
599 		return false;
600 	return true;
601 }
602 
603 /*
604  * Check whether the given fp and hw params are compatbile with the current
605  * setup of the target EP for implicit feedback sync
606  */
607 bool snd_usb_endpoint_compatible(struct snd_usb_audio *chip,
608 				 struct snd_usb_endpoint *ep,
609 				 const struct audioformat *fp,
610 				 const struct snd_pcm_hw_params *params)
611 {
612 	bool ret;
613 
614 	mutex_lock(&chip->mutex);
615 	ret = endpoint_compatible(ep, fp, params);
616 	mutex_unlock(&chip->mutex);
617 	return ret;
618 }
619 
620 /*
621  * snd_usb_endpoint_open: Open the endpoint
622  *
623  * Called from hw_params to assign the endpoint to the substream.
624  * It's reference-counted, and only the first opener is allowed to set up
625  * arbitrary parameters.  The later opener must be compatible with the
626  * former opened parameters.
627  * The endpoint needs to be closed via snd_usb_endpoint_close() later.
628  *
629  * Note that this function doesn't configure the endpoint.  The substream
630  * needs to set it up later via snd_usb_endpoint_configure().
631  */
632 struct snd_usb_endpoint *
633 snd_usb_endpoint_open(struct snd_usb_audio *chip,
634 		      const struct audioformat *fp,
635 		      const struct snd_pcm_hw_params *params,
636 		      bool is_sync_ep)
637 {
638 	struct snd_usb_endpoint *ep;
639 	int ep_num = is_sync_ep ? fp->sync_ep : fp->endpoint;
640 
641 	mutex_lock(&chip->mutex);
642 	ep = snd_usb_get_endpoint(chip, ep_num);
643 	if (!ep) {
644 		usb_audio_err(chip, "Cannot find EP 0x%x to open\n", ep_num);
645 		goto unlock;
646 	}
647 
648 	if (!ep->opened) {
649 		if (is_sync_ep) {
650 			ep->iface = fp->sync_iface;
651 			ep->altsetting = fp->sync_altsetting;
652 			ep->ep_idx = fp->sync_ep_idx;
653 		} else {
654 			ep->iface = fp->iface;
655 			ep->altsetting = fp->altsetting;
656 			ep->ep_idx = 0;
657 		}
658 		usb_audio_dbg(chip, "Open EP 0x%x, iface=%d:%d, idx=%d\n",
659 			      ep_num, ep->iface, ep->altsetting, ep->ep_idx);
660 
661 		ep->cur_audiofmt = fp;
662 		ep->cur_channels = fp->channels;
663 		ep->cur_rate = params_rate(params);
664 		ep->cur_format = params_format(params);
665 		ep->cur_frame_bytes = snd_pcm_format_physical_width(ep->cur_format) *
666 			ep->cur_channels / 8;
667 		ep->cur_period_frames = params_period_size(params);
668 		ep->cur_period_bytes = ep->cur_period_frames * ep->cur_frame_bytes;
669 		ep->cur_buffer_periods = params_periods(params);
670 
671 		if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC)
672 			endpoint_set_syncinterval(chip, ep);
673 
674 		ep->implicit_fb_sync = fp->implicit_fb;
675 		ep->need_setup = true;
676 
677 		usb_audio_dbg(chip, "  channels=%d, rate=%d, format=%s, period_bytes=%d, periods=%d, implicit_fb=%d\n",
678 			      ep->cur_channels, ep->cur_rate,
679 			      snd_pcm_format_name(ep->cur_format),
680 			      ep->cur_period_bytes, ep->cur_buffer_periods,
681 			      ep->implicit_fb_sync);
682 
683 	} else {
684 		if (!endpoint_compatible(ep, fp, params)) {
685 			usb_audio_err(chip, "Incompatible EP setup for 0x%x\n",
686 				      ep_num);
687 			ep = NULL;
688 			goto unlock;
689 		}
690 
691 		usb_audio_dbg(chip, "Reopened EP 0x%x (count %d)\n",
692 			      ep_num, ep->opened);
693 	}
694 
695 	ep->opened++;
696 
697  unlock:
698 	mutex_unlock(&chip->mutex);
699 	return ep;
700 }
701 
702 /*
703  * snd_usb_endpoint_set_sync: Link data and sync endpoints
704  *
705  * Pass NULL to sync_ep to unlink again
706  */
707 void snd_usb_endpoint_set_sync(struct snd_usb_audio *chip,
708 			       struct snd_usb_endpoint *data_ep,
709 			       struct snd_usb_endpoint *sync_ep)
710 {
711 	data_ep->sync_source = sync_ep;
712 }
713 
714 /*
715  * Set data endpoint callbacks and the assigned data stream
716  *
717  * Called at PCM trigger and cleanups.
718  * Pass NULL to deactivate each callback.
719  */
720 void snd_usb_endpoint_set_callback(struct snd_usb_endpoint *ep,
721 				   void (*prepare)(struct snd_usb_substream *subs,
722 						   struct urb *urb),
723 				   void (*retire)(struct snd_usb_substream *subs,
724 						  struct urb *urb),
725 				   struct snd_usb_substream *data_subs)
726 {
727 	ep->prepare_data_urb = prepare;
728 	ep->retire_data_urb = retire;
729 	WRITE_ONCE(ep->data_subs, data_subs);
730 }
731 
732 static int endpoint_set_interface(struct snd_usb_audio *chip,
733 				  struct snd_usb_endpoint *ep,
734 				  bool set)
735 {
736 	int altset = set ? ep->altsetting : 0;
737 	int err;
738 
739 	usb_audio_dbg(chip, "Setting usb interface %d:%d for EP 0x%x\n",
740 		      ep->iface, altset, ep->ep_num);
741 	err = usb_set_interface(chip->dev, ep->iface, altset);
742 	if (err < 0) {
743 		usb_audio_err(chip, "%d:%d: usb_set_interface failed (%d)\n",
744 			      ep->iface, altset, err);
745 		return err;
746 	}
747 
748 	snd_usb_set_interface_quirk(chip);
749 	return 0;
750 }
751 
752 /*
753  * snd_usb_endpoint_close: Close the endpoint
754  *
755  * Unreference the already opened endpoint via snd_usb_endpoint_open().
756  */
757 void snd_usb_endpoint_close(struct snd_usb_audio *chip,
758 			    struct snd_usb_endpoint *ep)
759 {
760 	mutex_lock(&chip->mutex);
761 	usb_audio_dbg(chip, "Closing EP 0x%x (count %d)\n",
762 		      ep->ep_num, ep->opened);
763 	if (!--ep->opened) {
764 		endpoint_set_interface(chip, ep, false);
765 		ep->iface = 0;
766 		ep->altsetting = 0;
767 		ep->cur_audiofmt = NULL;
768 		ep->cur_rate = 0;
769 		usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num);
770 	}
771 	mutex_unlock(&chip->mutex);
772 }
773 
774 /* Prepare for suspening EP, called from the main suspend handler */
775 void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep)
776 {
777 	ep->need_setup = true;
778 }
779 
780 /*
781  *  wait until all urbs are processed.
782  */
783 static int wait_clear_urbs(struct snd_usb_endpoint *ep)
784 {
785 	unsigned long end_time = jiffies + msecs_to_jiffies(1000);
786 	int alive;
787 
788 	if (!test_bit(EP_FLAG_STOPPING, &ep->flags))
789 		return 0;
790 
791 	do {
792 		alive = bitmap_weight(&ep->active_mask, ep->nurbs);
793 		if (!alive)
794 			break;
795 
796 		schedule_timeout_uninterruptible(1);
797 	} while (time_before(jiffies, end_time));
798 
799 	if (alive)
800 		usb_audio_err(ep->chip,
801 			"timeout: still %d active urbs on EP #%x\n",
802 			alive, ep->ep_num);
803 	clear_bit(EP_FLAG_STOPPING, &ep->flags);
804 
805 	ep->sync_sink = NULL;
806 	snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
807 
808 	return 0;
809 }
810 
811 /* sync the pending stop operation;
812  * this function itself doesn't trigger the stop operation
813  */
814 void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep)
815 {
816 	if (ep)
817 		wait_clear_urbs(ep);
818 }
819 
820 /*
821  * Stop and unlink active urbs.
822  *
823  * This function checks and clears EP_FLAG_RUNNING state.
824  * When @wait_sync is set, it waits until all pending URBs are killed.
825  */
826 static int stop_and_unlink_urbs(struct snd_usb_endpoint *ep, bool force,
827 				bool wait_sync)
828 {
829 	unsigned int i;
830 
831 	if (!force && atomic_read(&ep->chip->shutdown)) /* to be sure... */
832 		return -EBADFD;
833 
834 	if (atomic_read(&ep->running))
835 		return -EBUSY;
836 
837 	if (!test_and_clear_bit(EP_FLAG_RUNNING, &ep->flags))
838 		goto out;
839 
840 	set_bit(EP_FLAG_STOPPING, &ep->flags);
841 	INIT_LIST_HEAD(&ep->ready_playback_urbs);
842 	ep->next_packet_head = 0;
843 	ep->next_packet_queued = 0;
844 
845 	for (i = 0; i < ep->nurbs; i++) {
846 		if (test_bit(i, &ep->active_mask)) {
847 			if (!test_and_set_bit(i, &ep->unlink_mask)) {
848 				struct urb *u = ep->urb[i].urb;
849 				usb_unlink_urb(u);
850 			}
851 		}
852 	}
853 
854  out:
855 	if (wait_sync)
856 		return wait_clear_urbs(ep);
857 	return 0;
858 }
859 
860 /*
861  * release an endpoint's urbs
862  */
863 static void release_urbs(struct snd_usb_endpoint *ep, int force)
864 {
865 	int i;
866 
867 	/* route incoming urbs to nirvana */
868 	snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL);
869 
870 	/* stop urbs */
871 	stop_and_unlink_urbs(ep, force, true);
872 
873 	for (i = 0; i < ep->nurbs; i++)
874 		release_urb_ctx(&ep->urb[i]);
875 
876 	usb_free_coherent(ep->chip->dev, SYNC_URBS * 4,
877 			  ep->syncbuf, ep->sync_dma);
878 
879 	ep->syncbuf = NULL;
880 	ep->nurbs = 0;
881 }
882 
883 /*
884  * configure a data endpoint
885  */
886 static int data_ep_set_params(struct snd_usb_endpoint *ep)
887 {
888 	struct snd_usb_audio *chip = ep->chip;
889 	unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb;
890 	unsigned int max_packs_per_period, urbs_per_period, urb_packs;
891 	unsigned int max_urbs, i;
892 	const struct audioformat *fmt = ep->cur_audiofmt;
893 	int frame_bits = ep->cur_frame_bytes * 8;
894 	int tx_length_quirk = (chip->tx_length_quirk &&
895 			       usb_pipeout(ep->pipe));
896 
897 	usb_audio_dbg(chip, "Setting params for data EP 0x%x, pipe 0x%x\n",
898 		      ep->ep_num, ep->pipe);
899 
900 	if (ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) {
901 		/*
902 		 * When operating in DSD DOP mode, the size of a sample frame
903 		 * in hardware differs from the actual physical format width
904 		 * because we need to make room for the DOP markers.
905 		 */
906 		frame_bits += ep->cur_channels << 3;
907 	}
908 
909 	ep->datainterval = fmt->datainterval;
910 	ep->stride = frame_bits >> 3;
911 
912 	switch (ep->cur_format) {
913 	case SNDRV_PCM_FORMAT_U8:
914 		ep->silence_value = 0x80;
915 		break;
916 	case SNDRV_PCM_FORMAT_DSD_U8:
917 	case SNDRV_PCM_FORMAT_DSD_U16_LE:
918 	case SNDRV_PCM_FORMAT_DSD_U32_LE:
919 	case SNDRV_PCM_FORMAT_DSD_U16_BE:
920 	case SNDRV_PCM_FORMAT_DSD_U32_BE:
921 		ep->silence_value = 0x69;
922 		break;
923 	default:
924 		ep->silence_value = 0;
925 	}
926 
927 	/* assume max. frequency is 50% higher than nominal */
928 	ep->freqmax = ep->freqn + (ep->freqn >> 1);
929 	/* Round up freqmax to nearest integer in order to calculate maximum
930 	 * packet size, which must represent a whole number of frames.
931 	 * This is accomplished by adding 0x0.ffff before converting the
932 	 * Q16.16 format into integer.
933 	 * In order to accurately calculate the maximum packet size when
934 	 * the data interval is more than 1 (i.e. ep->datainterval > 0),
935 	 * multiply by the data interval prior to rounding. For instance,
936 	 * a freqmax of 41 kHz will result in a max packet size of 6 (5.125)
937 	 * frames with a data interval of 1, but 11 (10.25) frames with a
938 	 * data interval of 2.
939 	 * (ep->freqmax << ep->datainterval overflows at 8.192 MHz for the
940 	 * maximum datainterval value of 3, at USB full speed, higher for
941 	 * USB high speed, noting that ep->freqmax is in units of
942 	 * frames per packet in Q16.16 format.)
943 	 */
944 	maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) *
945 			 (frame_bits >> 3);
946 	if (tx_length_quirk)
947 		maxsize += sizeof(__le32); /* Space for length descriptor */
948 	/* but wMaxPacketSize might reduce this */
949 	if (ep->maxpacksize && ep->maxpacksize < maxsize) {
950 		/* whatever fits into a max. size packet */
951 		unsigned int data_maxsize = maxsize = ep->maxpacksize;
952 
953 		if (tx_length_quirk)
954 			/* Need to remove the length descriptor to calc freq */
955 			data_maxsize -= sizeof(__le32);
956 		ep->freqmax = (data_maxsize / (frame_bits >> 3))
957 				<< (16 - ep->datainterval);
958 	}
959 
960 	if (ep->fill_max)
961 		ep->curpacksize = ep->maxpacksize;
962 	else
963 		ep->curpacksize = maxsize;
964 
965 	if (snd_usb_get_speed(chip->dev) != USB_SPEED_FULL) {
966 		packs_per_ms = 8 >> ep->datainterval;
967 		max_packs_per_urb = MAX_PACKS_HS;
968 	} else {
969 		packs_per_ms = 1;
970 		max_packs_per_urb = MAX_PACKS;
971 	}
972 	if (ep->sync_source && !ep->implicit_fb_sync)
973 		max_packs_per_urb = min(max_packs_per_urb,
974 					1U << ep->sync_source->syncinterval);
975 	max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval);
976 
977 	/*
978 	 * Capture endpoints need to use small URBs because there's no way
979 	 * to tell in advance where the next period will end, and we don't
980 	 * want the next URB to complete much after the period ends.
981 	 *
982 	 * Playback endpoints with implicit sync much use the same parameters
983 	 * as their corresponding capture endpoint.
984 	 */
985 	if (usb_pipein(ep->pipe) || ep->implicit_fb_sync) {
986 
987 		urb_packs = packs_per_ms;
988 		/*
989 		 * Wireless devices can poll at a max rate of once per 4ms.
990 		 * For dataintervals less than 5, increase the packet count to
991 		 * allow the host controller to use bursting to fill in the
992 		 * gaps.
993 		 */
994 		if (snd_usb_get_speed(chip->dev) == USB_SPEED_WIRELESS) {
995 			int interval = ep->datainterval;
996 			while (interval < 5) {
997 				urb_packs <<= 1;
998 				++interval;
999 			}
1000 		}
1001 		/* make capture URBs <= 1 ms and smaller than a period */
1002 		urb_packs = min(max_packs_per_urb, urb_packs);
1003 		while (urb_packs > 1 && urb_packs * maxsize >= ep->cur_period_bytes)
1004 			urb_packs >>= 1;
1005 		ep->nurbs = MAX_URBS;
1006 
1007 	/*
1008 	 * Playback endpoints without implicit sync are adjusted so that
1009 	 * a period fits as evenly as possible in the smallest number of
1010 	 * URBs.  The total number of URBs is adjusted to the size of the
1011 	 * ALSA buffer, subject to the MAX_URBS and MAX_QUEUE limits.
1012 	 */
1013 	} else {
1014 		/* determine how small a packet can be */
1015 		minsize = (ep->freqn >> (16 - ep->datainterval)) *
1016 				(frame_bits >> 3);
1017 		/* with sync from device, assume it can be 12% lower */
1018 		if (ep->sync_source)
1019 			minsize -= minsize >> 3;
1020 		minsize = max(minsize, 1u);
1021 
1022 		/* how many packets will contain an entire ALSA period? */
1023 		max_packs_per_period = DIV_ROUND_UP(ep->cur_period_bytes, minsize);
1024 
1025 		/* how many URBs will contain a period? */
1026 		urbs_per_period = DIV_ROUND_UP(max_packs_per_period,
1027 				max_packs_per_urb);
1028 		/* how many packets are needed in each URB? */
1029 		urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period);
1030 
1031 		/* limit the number of frames in a single URB */
1032 		ep->max_urb_frames = DIV_ROUND_UP(ep->cur_period_frames,
1033 						  urbs_per_period);
1034 
1035 		/* try to use enough URBs to contain an entire ALSA buffer */
1036 		max_urbs = min((unsigned) MAX_URBS,
1037 				MAX_QUEUE * packs_per_ms / urb_packs);
1038 		ep->nurbs = min(max_urbs, urbs_per_period * ep->cur_buffer_periods);
1039 	}
1040 
1041 	/* allocate and initialize data urbs */
1042 	for (i = 0; i < ep->nurbs; i++) {
1043 		struct snd_urb_ctx *u = &ep->urb[i];
1044 		u->index = i;
1045 		u->ep = ep;
1046 		u->packets = urb_packs;
1047 		u->buffer_size = maxsize * u->packets;
1048 
1049 		if (fmt->fmt_type == UAC_FORMAT_TYPE_II)
1050 			u->packets++; /* for transfer delimiter */
1051 		u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
1052 		if (!u->urb)
1053 			goto out_of_memory;
1054 
1055 		u->urb->transfer_buffer =
1056 			usb_alloc_coherent(chip->dev, u->buffer_size,
1057 					   GFP_KERNEL, &u->urb->transfer_dma);
1058 		if (!u->urb->transfer_buffer)
1059 			goto out_of_memory;
1060 		u->urb->pipe = ep->pipe;
1061 		u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1062 		u->urb->interval = 1 << ep->datainterval;
1063 		u->urb->context = u;
1064 		u->urb->complete = snd_complete_urb;
1065 		INIT_LIST_HEAD(&u->ready_list);
1066 	}
1067 
1068 	return 0;
1069 
1070 out_of_memory:
1071 	release_urbs(ep, 0);
1072 	return -ENOMEM;
1073 }
1074 
1075 /*
1076  * configure a sync endpoint
1077  */
1078 static int sync_ep_set_params(struct snd_usb_endpoint *ep)
1079 {
1080 	struct snd_usb_audio *chip = ep->chip;
1081 	int i;
1082 
1083 	usb_audio_dbg(chip, "Setting params for sync EP 0x%x, pipe 0x%x\n",
1084 		      ep->ep_num, ep->pipe);
1085 
1086 	ep->syncbuf = usb_alloc_coherent(chip->dev, SYNC_URBS * 4,
1087 					 GFP_KERNEL, &ep->sync_dma);
1088 	if (!ep->syncbuf)
1089 		return -ENOMEM;
1090 
1091 	for (i = 0; i < SYNC_URBS; i++) {
1092 		struct snd_urb_ctx *u = &ep->urb[i];
1093 		u->index = i;
1094 		u->ep = ep;
1095 		u->packets = 1;
1096 		u->urb = usb_alloc_urb(1, GFP_KERNEL);
1097 		if (!u->urb)
1098 			goto out_of_memory;
1099 		u->urb->transfer_buffer = ep->syncbuf + i * 4;
1100 		u->urb->transfer_dma = ep->sync_dma + i * 4;
1101 		u->urb->transfer_buffer_length = 4;
1102 		u->urb->pipe = ep->pipe;
1103 		u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
1104 		u->urb->number_of_packets = 1;
1105 		u->urb->interval = 1 << ep->syncinterval;
1106 		u->urb->context = u;
1107 		u->urb->complete = snd_complete_urb;
1108 	}
1109 
1110 	ep->nurbs = SYNC_URBS;
1111 
1112 	return 0;
1113 
1114 out_of_memory:
1115 	release_urbs(ep, 0);
1116 	return -ENOMEM;
1117 }
1118 
1119 /*
1120  * snd_usb_endpoint_set_params: configure an snd_usb_endpoint
1121  *
1122  * Determine the number of URBs to be used on this endpoint.
1123  * An endpoint must be configured before it can be started.
1124  * An endpoint that is already running can not be reconfigured.
1125  */
1126 static int snd_usb_endpoint_set_params(struct snd_usb_audio *chip,
1127 				       struct snd_usb_endpoint *ep)
1128 {
1129 	const struct audioformat *fmt = ep->cur_audiofmt;
1130 	int err;
1131 
1132 	/* release old buffers, if any */
1133 	release_urbs(ep, 0);
1134 
1135 	ep->datainterval = fmt->datainterval;
1136 	ep->maxpacksize = fmt->maxpacksize;
1137 	ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX);
1138 
1139 	if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) {
1140 		ep->freqn = get_usb_full_speed_rate(ep->cur_rate);
1141 		ep->pps = 1000 >> ep->datainterval;
1142 	} else {
1143 		ep->freqn = get_usb_high_speed_rate(ep->cur_rate);
1144 		ep->pps = 8000 >> ep->datainterval;
1145 	}
1146 
1147 	ep->sample_rem = ep->cur_rate % ep->pps;
1148 	ep->packsize[0] = ep->cur_rate / ep->pps;
1149 	ep->packsize[1] = (ep->cur_rate + (ep->pps - 1)) / ep->pps;
1150 
1151 	/* calculate the frequency in 16.16 format */
1152 	ep->freqm = ep->freqn;
1153 	ep->freqshift = INT_MIN;
1154 
1155 	ep->phase = 0;
1156 
1157 	switch (ep->type) {
1158 	case  SND_USB_ENDPOINT_TYPE_DATA:
1159 		err = data_ep_set_params(ep);
1160 		break;
1161 	case  SND_USB_ENDPOINT_TYPE_SYNC:
1162 		err = sync_ep_set_params(ep);
1163 		break;
1164 	default:
1165 		err = -EINVAL;
1166 	}
1167 
1168 	usb_audio_dbg(chip, "Set up %d URBS, ret=%d\n", ep->nurbs, err);
1169 
1170 	if (err < 0)
1171 		return err;
1172 
1173 	/* some unit conversions in runtime */
1174 	ep->maxframesize = ep->maxpacksize / ep->cur_frame_bytes;
1175 	ep->curframesize = ep->curpacksize / ep->cur_frame_bytes;
1176 
1177 	return 0;
1178 }
1179 
1180 /*
1181  * snd_usb_endpoint_configure: Configure the endpoint
1182  *
1183  * This function sets up the EP to be fully usable state.
1184  * It's called either from hw_params or prepare callback.
1185  * The function checks need_setup flag, and perfoms nothing unless needed,
1186  * so it's safe to call this multiple times.
1187  *
1188  * This returns zero if unchanged, 1 if the configuration has changed,
1189  * or a negative error code.
1190  */
1191 int snd_usb_endpoint_configure(struct snd_usb_audio *chip,
1192 			       struct snd_usb_endpoint *ep)
1193 {
1194 	bool iface_first;
1195 	int err = 0;
1196 
1197 	mutex_lock(&chip->mutex);
1198 	if (!ep->need_setup)
1199 		goto unlock;
1200 
1201 	/* No need to (re-)configure the sync EP belonging to the same altset */
1202 	if (ep->ep_idx) {
1203 		err = snd_usb_endpoint_set_params(chip, ep);
1204 		if (err < 0)
1205 			goto unlock;
1206 		goto done;
1207 	}
1208 
1209 	/* Need to deselect altsetting at first */
1210 	endpoint_set_interface(chip, ep, false);
1211 
1212 	/* Some UAC1 devices (e.g. Yamaha THR10) need the host interface
1213 	 * to be set up before parameter setups
1214 	 */
1215 	iface_first = ep->cur_audiofmt->protocol == UAC_VERSION_1;
1216 	if (iface_first) {
1217 		err = endpoint_set_interface(chip, ep, true);
1218 		if (err < 0)
1219 			goto unlock;
1220 	}
1221 
1222 	err = snd_usb_init_pitch(chip, ep->cur_audiofmt);
1223 	if (err < 0)
1224 		goto unlock;
1225 
1226 	err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, ep->cur_rate);
1227 	if (err < 0)
1228 		goto unlock;
1229 
1230 	err = snd_usb_endpoint_set_params(chip, ep);
1231 	if (err < 0)
1232 		goto unlock;
1233 
1234 	err = snd_usb_select_mode_quirk(chip, ep->cur_audiofmt);
1235 	if (err < 0)
1236 		goto unlock;
1237 
1238 	/* for UAC2/3, enable the interface altset here at last */
1239 	if (!iface_first) {
1240 		err = endpoint_set_interface(chip, ep, true);
1241 		if (err < 0)
1242 			goto unlock;
1243 	}
1244 
1245  done:
1246 	ep->need_setup = false;
1247 	err = 1;
1248 
1249 unlock:
1250 	mutex_unlock(&chip->mutex);
1251 	return err;
1252 }
1253 
1254 /**
1255  * snd_usb_endpoint_start: start an snd_usb_endpoint
1256  *
1257  * @ep: the endpoint to start
1258  *
1259  * A call to this function will increment the running count of the endpoint.
1260  * In case it is not already running, the URBs for this endpoint will be
1261  * submitted. Otherwise, this function does nothing.
1262  *
1263  * Must be balanced to calls of snd_usb_endpoint_stop().
1264  *
1265  * Returns an error if the URB submission failed, 0 in all other cases.
1266  */
1267 int snd_usb_endpoint_start(struct snd_usb_endpoint *ep)
1268 {
1269 	int err;
1270 	unsigned int i;
1271 
1272 	if (atomic_read(&ep->chip->shutdown))
1273 		return -EBADFD;
1274 
1275 	if (ep->sync_source)
1276 		WRITE_ONCE(ep->sync_source->sync_sink, ep);
1277 
1278 	usb_audio_dbg(ep->chip, "Starting %s EP 0x%x (running %d)\n",
1279 		      ep_type_name(ep->type), ep->ep_num,
1280 		      atomic_read(&ep->running));
1281 
1282 	/* already running? */
1283 	if (atomic_inc_return(&ep->running) != 1)
1284 		return 0;
1285 
1286 	ep->active_mask = 0;
1287 	ep->unlink_mask = 0;
1288 	ep->phase = 0;
1289 	ep->sample_accum = 0;
1290 
1291 	snd_usb_endpoint_start_quirk(ep);
1292 
1293 	/*
1294 	 * If this endpoint has a data endpoint as implicit feedback source,
1295 	 * don't start the urbs here. Instead, mark them all as available,
1296 	 * wait for the record urbs to return and queue the playback urbs
1297 	 * from that context.
1298 	 */
1299 
1300 	set_bit(EP_FLAG_RUNNING, &ep->flags);
1301 
1302 	if (snd_usb_endpoint_implicit_feedback_sink(ep)) {
1303 		for (i = 0; i < ep->nurbs; i++) {
1304 			struct snd_urb_ctx *ctx = ep->urb + i;
1305 			list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs);
1306 		}
1307 
1308 		usb_audio_dbg(ep->chip, "No URB submission due to implicit fb sync\n");
1309 		return 0;
1310 	}
1311 
1312 	for (i = 0; i < ep->nurbs; i++) {
1313 		struct urb *urb = ep->urb[i].urb;
1314 
1315 		if (snd_BUG_ON(!urb))
1316 			goto __error;
1317 
1318 		if (usb_pipeout(ep->pipe)) {
1319 			prepare_outbound_urb(ep, urb->context);
1320 		} else {
1321 			prepare_inbound_urb(ep, urb->context);
1322 		}
1323 
1324 		err = usb_submit_urb(urb, GFP_ATOMIC);
1325 		if (err < 0) {
1326 			usb_audio_err(ep->chip,
1327 				"cannot submit urb %d, error %d: %s\n",
1328 				i, err, usb_error_string(err));
1329 			goto __error;
1330 		}
1331 		set_bit(i, &ep->active_mask);
1332 	}
1333 
1334 	usb_audio_dbg(ep->chip, "%d URBs submitted for EP 0x%x\n",
1335 		      ep->nurbs, ep->ep_num);
1336 	return 0;
1337 
1338 __error:
1339 	snd_usb_endpoint_stop(ep);
1340 	return -EPIPE;
1341 }
1342 
1343 /**
1344  * snd_usb_endpoint_stop: stop an snd_usb_endpoint
1345  *
1346  * @ep: the endpoint to stop (may be NULL)
1347  *
1348  * A call to this function will decrement the running count of the endpoint.
1349  * In case the last user has requested the endpoint stop, the URBs will
1350  * actually be deactivated.
1351  *
1352  * Must be balanced to calls of snd_usb_endpoint_start().
1353  *
1354  * The caller needs to synchronize the pending stop operation via
1355  * snd_usb_endpoint_sync_pending_stop().
1356  */
1357 void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep)
1358 {
1359 	if (!ep)
1360 		return;
1361 
1362 	usb_audio_dbg(ep->chip, "Stopping %s EP 0x%x (running %d)\n",
1363 		      ep_type_name(ep->type), ep->ep_num,
1364 		      atomic_read(&ep->running));
1365 
1366 	if (snd_BUG_ON(!atomic_read(&ep->running)))
1367 		return;
1368 
1369 	if (ep->sync_source)
1370 		WRITE_ONCE(ep->sync_source->sync_sink, NULL);
1371 
1372 	if (!atomic_dec_return(&ep->running))
1373 		stop_and_unlink_urbs(ep, false, false);
1374 }
1375 
1376 /**
1377  * snd_usb_endpoint_release: Tear down an snd_usb_endpoint
1378  *
1379  * @ep: the endpoint to release
1380  *
1381  * This function does not care for the endpoint's running count but will tear
1382  * down all the streaming URBs immediately.
1383  */
1384 void snd_usb_endpoint_release(struct snd_usb_endpoint *ep)
1385 {
1386 	release_urbs(ep, 1);
1387 }
1388 
1389 /**
1390  * snd_usb_endpoint_free: Free the resources of an snd_usb_endpoint
1391  *
1392  * @ep: the endpoint to free
1393  *
1394  * This free all resources of the given ep.
1395  */
1396 void snd_usb_endpoint_free(struct snd_usb_endpoint *ep)
1397 {
1398 	kfree(ep);
1399 }
1400 
1401 /*
1402  * snd_usb_handle_sync_urb: parse an USB sync packet
1403  *
1404  * @ep: the endpoint to handle the packet
1405  * @sender: the sending endpoint
1406  * @urb: the received packet
1407  *
1408  * This function is called from the context of an endpoint that received
1409  * the packet and is used to let another endpoint object handle the payload.
1410  */
1411 static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
1412 				    struct snd_usb_endpoint *sender,
1413 				    const struct urb *urb)
1414 {
1415 	int shift;
1416 	unsigned int f;
1417 	unsigned long flags;
1418 
1419 	snd_BUG_ON(ep == sender);
1420 
1421 	/*
1422 	 * In case the endpoint is operating in implicit feedback mode, prepare
1423 	 * a new outbound URB that has the same layout as the received packet
1424 	 * and add it to the list of pending urbs. queue_pending_output_urbs()
1425 	 * will take care of them later.
1426 	 */
1427 	if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
1428 	    atomic_read(&ep->running)) {
1429 
1430 		/* implicit feedback case */
1431 		int i, bytes = 0;
1432 		struct snd_urb_ctx *in_ctx;
1433 		struct snd_usb_packet_info *out_packet;
1434 
1435 		in_ctx = urb->context;
1436 
1437 		/* Count overall packet size */
1438 		for (i = 0; i < in_ctx->packets; i++)
1439 			if (urb->iso_frame_desc[i].status == 0)
1440 				bytes += urb->iso_frame_desc[i].actual_length;
1441 
1442 		/*
1443 		 * skip empty packets. At least M-Audio's Fast Track Ultra stops
1444 		 * streaming once it received a 0-byte OUT URB
1445 		 */
1446 		if (bytes == 0)
1447 			return;
1448 
1449 		spin_lock_irqsave(&ep->lock, flags);
1450 		if (ep->next_packet_queued >= ARRAY_SIZE(ep->next_packet)) {
1451 			spin_unlock_irqrestore(&ep->lock, flags);
1452 			usb_audio_err(ep->chip,
1453 				      "next package FIFO overflow EP 0x%x\n",
1454 				      ep->ep_num);
1455 			notify_xrun(ep);
1456 			return;
1457 		}
1458 
1459 		out_packet = next_packet_fifo_enqueue(ep);
1460 
1461 		/*
1462 		 * Iterate through the inbound packet and prepare the lengths
1463 		 * for the output packet. The OUT packet we are about to send
1464 		 * will have the same amount of payload bytes per stride as the
1465 		 * IN packet we just received. Since the actual size is scaled
1466 		 * by the stride, use the sender stride to calculate the length
1467 		 * in case the number of channels differ between the implicitly
1468 		 * fed-back endpoint and the synchronizing endpoint.
1469 		 */
1470 
1471 		out_packet->packets = in_ctx->packets;
1472 		for (i = 0; i < in_ctx->packets; i++) {
1473 			if (urb->iso_frame_desc[i].status == 0)
1474 				out_packet->packet_size[i] =
1475 					urb->iso_frame_desc[i].actual_length / sender->stride;
1476 			else
1477 				out_packet->packet_size[i] = 0;
1478 		}
1479 
1480 		spin_unlock_irqrestore(&ep->lock, flags);
1481 		queue_pending_output_urbs(ep);
1482 
1483 		return;
1484 	}
1485 
1486 	/*
1487 	 * process after playback sync complete
1488 	 *
1489 	 * Full speed devices report feedback values in 10.14 format as samples
1490 	 * per frame, high speed devices in 16.16 format as samples per
1491 	 * microframe.
1492 	 *
1493 	 * Because the Audio Class 1 spec was written before USB 2.0, many high
1494 	 * speed devices use a wrong interpretation, some others use an
1495 	 * entirely different format.
1496 	 *
1497 	 * Therefore, we cannot predict what format any particular device uses
1498 	 * and must detect it automatically.
1499 	 */
1500 
1501 	if (urb->iso_frame_desc[0].status != 0 ||
1502 	    urb->iso_frame_desc[0].actual_length < 3)
1503 		return;
1504 
1505 	f = le32_to_cpup(urb->transfer_buffer);
1506 	if (urb->iso_frame_desc[0].actual_length == 3)
1507 		f &= 0x00ffffff;
1508 	else
1509 		f &= 0x0fffffff;
1510 
1511 	if (f == 0)
1512 		return;
1513 
1514 	if (unlikely(sender->tenor_fb_quirk)) {
1515 		/*
1516 		 * Devices based on Tenor 8802 chipsets (TEAC UD-H01
1517 		 * and others) sometimes change the feedback value
1518 		 * by +/- 0x1.0000.
1519 		 */
1520 		if (f < ep->freqn - 0x8000)
1521 			f += 0xf000;
1522 		else if (f > ep->freqn + 0x8000)
1523 			f -= 0xf000;
1524 	} else if (unlikely(ep->freqshift == INT_MIN)) {
1525 		/*
1526 		 * The first time we see a feedback value, determine its format
1527 		 * by shifting it left or right until it matches the nominal
1528 		 * frequency value.  This assumes that the feedback does not
1529 		 * differ from the nominal value more than +50% or -25%.
1530 		 */
1531 		shift = 0;
1532 		while (f < ep->freqn - ep->freqn / 4) {
1533 			f <<= 1;
1534 			shift++;
1535 		}
1536 		while (f > ep->freqn + ep->freqn / 2) {
1537 			f >>= 1;
1538 			shift--;
1539 		}
1540 		ep->freqshift = shift;
1541 	} else if (ep->freqshift >= 0)
1542 		f <<= ep->freqshift;
1543 	else
1544 		f >>= -ep->freqshift;
1545 
1546 	if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) {
1547 		/*
1548 		 * If the frequency looks valid, set it.
1549 		 * This value is referred to in prepare_playback_urb().
1550 		 */
1551 		spin_lock_irqsave(&ep->lock, flags);
1552 		ep->freqm = f;
1553 		spin_unlock_irqrestore(&ep->lock, flags);
1554 	} else {
1555 		/*
1556 		 * Out of range; maybe the shift value is wrong.
1557 		 * Reset it so that we autodetect again the next time.
1558 		 */
1559 		ep->freqshift = INT_MIN;
1560 	}
1561 }
1562 
1563