xref: /openbmc/linux/sound/usb/pcm.c (revision e80a48ba)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  */
4 
5 #include <linux/init.h>
6 #include <linux/slab.h>
7 #include <linux/bitrev.h>
8 #include <linux/ratelimit.h>
9 #include <linux/usb.h>
10 #include <linux/usb/audio.h>
11 #include <linux/usb/audio-v2.h>
12 
13 #include <sound/core.h>
14 #include <sound/pcm.h>
15 #include <sound/pcm_params.h>
16 
17 #include "usbaudio.h"
18 #include "card.h"
19 #include "quirks.h"
20 #include "endpoint.h"
21 #include "helper.h"
22 #include "pcm.h"
23 #include "clock.h"
24 #include "power.h"
25 #include "media.h"
26 #include "implicit.h"
27 
28 #define SUBSTREAM_FLAG_DATA_EP_STARTED	0
29 #define SUBSTREAM_FLAG_SYNC_EP_STARTED	1
30 
31 /* return the estimated delay based on USB frame counters */
32 static snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
33 					   struct snd_pcm_runtime *runtime)
34 {
35 	unsigned int current_frame_number;
36 	unsigned int frame_diff;
37 	int est_delay;
38 	int queued;
39 
40 	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
41 		queued = bytes_to_frames(runtime, subs->inflight_bytes);
42 		if (!queued)
43 			return 0;
44 	} else if (!subs->running) {
45 		return 0;
46 	}
47 
48 	current_frame_number = usb_get_current_frame_number(subs->dev);
49 	/*
50 	 * HCD implementations use different widths, use lower 8 bits.
51 	 * The delay will be managed up to 256ms, which is more than
52 	 * enough
53 	 */
54 	frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
55 
56 	/* Approximation based on number of samples per USB frame (ms),
57 	   some truncation for 44.1 but the estimate is good enough */
58 	est_delay = frame_diff * runtime->rate / 1000;
59 
60 	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
61 		est_delay = queued - est_delay;
62 		if (est_delay < 0)
63 			est_delay = 0;
64 	}
65 
66 	return est_delay;
67 }
68 
69 /*
70  * return the current pcm pointer.  just based on the hwptr_done value.
71  */
72 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
73 {
74 	struct snd_pcm_runtime *runtime = substream->runtime;
75 	struct snd_usb_substream *subs = runtime->private_data;
76 	unsigned int hwptr_done;
77 
78 	if (atomic_read(&subs->stream->chip->shutdown))
79 		return SNDRV_PCM_POS_XRUN;
80 	spin_lock(&subs->lock);
81 	hwptr_done = subs->hwptr_done;
82 	runtime->delay = snd_usb_pcm_delay(subs, runtime);
83 	spin_unlock(&subs->lock);
84 	return bytes_to_frames(runtime, hwptr_done);
85 }
86 
87 /*
88  * find a matching audio format
89  */
90 static const struct audioformat *
91 find_format(struct list_head *fmt_list_head, snd_pcm_format_t format,
92 	    unsigned int rate, unsigned int channels, bool strict_match,
93 	    struct snd_usb_substream *subs)
94 {
95 	const struct audioformat *fp;
96 	const struct audioformat *found = NULL;
97 	int cur_attr = 0, attr;
98 
99 	list_for_each_entry(fp, fmt_list_head, list) {
100 		if (strict_match) {
101 			if (!(fp->formats & pcm_format_to_bits(format)))
102 				continue;
103 			if (fp->channels != channels)
104 				continue;
105 		}
106 		if (rate < fp->rate_min || rate > fp->rate_max)
107 			continue;
108 		if (!(fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
109 			unsigned int i;
110 			for (i = 0; i < fp->nr_rates; i++)
111 				if (fp->rate_table[i] == rate)
112 					break;
113 			if (i >= fp->nr_rates)
114 				continue;
115 		}
116 		attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
117 		if (!found) {
118 			found = fp;
119 			cur_attr = attr;
120 			continue;
121 		}
122 		/* avoid async out and adaptive in if the other method
123 		 * supports the same format.
124 		 * this is a workaround for the case like
125 		 * M-audio audiophile USB.
126 		 */
127 		if (subs && attr != cur_attr) {
128 			if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
129 			     subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
130 			    (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
131 			     subs->direction == SNDRV_PCM_STREAM_CAPTURE))
132 				continue;
133 			if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
134 			     subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
135 			    (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
136 			     subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
137 				found = fp;
138 				cur_attr = attr;
139 				continue;
140 			}
141 		}
142 		/* find the format with the largest max. packet size */
143 		if (fp->maxpacksize > found->maxpacksize) {
144 			found = fp;
145 			cur_attr = attr;
146 		}
147 	}
148 	return found;
149 }
150 
151 static const struct audioformat *
152 find_substream_format(struct snd_usb_substream *subs,
153 		      const struct snd_pcm_hw_params *params)
154 {
155 	return find_format(&subs->fmt_list, params_format(params),
156 			   params_rate(params), params_channels(params),
157 			   true, subs);
158 }
159 
160 bool snd_usb_pcm_has_fixed_rate(struct snd_usb_substream *subs)
161 {
162 	const struct audioformat *fp;
163 	struct snd_usb_audio *chip = subs->stream->chip;
164 	int rate = -1;
165 
166 	if (!(chip->quirk_flags & QUIRK_FLAG_FIXED_RATE))
167 		return false;
168 	list_for_each_entry(fp, &subs->fmt_list, list) {
169 		if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
170 			return false;
171 		if (fp->nr_rates < 1)
172 			continue;
173 		if (fp->nr_rates > 1)
174 			return false;
175 		if (rate < 0) {
176 			rate = fp->rate_table[0];
177 			continue;
178 		}
179 		if (rate != fp->rate_table[0])
180 			return false;
181 	}
182 	return true;
183 }
184 
185 static int init_pitch_v1(struct snd_usb_audio *chip, int ep)
186 {
187 	struct usb_device *dev = chip->dev;
188 	unsigned char data[1];
189 	int err;
190 
191 	data[0] = 1;
192 	err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
193 			      USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
194 			      UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
195 			      data, sizeof(data));
196 	return err;
197 }
198 
199 static int init_pitch_v2(struct snd_usb_audio *chip, int ep)
200 {
201 	struct usb_device *dev = chip->dev;
202 	unsigned char data[1];
203 	int err;
204 
205 	data[0] = 1;
206 	err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
207 			      USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
208 			      UAC2_EP_CS_PITCH << 8, 0,
209 			      data, sizeof(data));
210 	return err;
211 }
212 
213 /*
214  * initialize the pitch control and sample rate
215  */
216 int snd_usb_init_pitch(struct snd_usb_audio *chip,
217 		       const struct audioformat *fmt)
218 {
219 	int err;
220 
221 	/* if endpoint doesn't have pitch control, bail out */
222 	if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
223 		return 0;
224 
225 	usb_audio_dbg(chip, "enable PITCH for EP 0x%x\n", fmt->endpoint);
226 
227 	switch (fmt->protocol) {
228 	case UAC_VERSION_1:
229 		err = init_pitch_v1(chip, fmt->endpoint);
230 		break;
231 	case UAC_VERSION_2:
232 		err = init_pitch_v2(chip, fmt->endpoint);
233 		break;
234 	default:
235 		return 0;
236 	}
237 
238 	if (err < 0) {
239 		usb_audio_err(chip, "failed to enable PITCH for EP 0x%x\n",
240 			      fmt->endpoint);
241 		return err;
242 	}
243 
244 	return 0;
245 }
246 
247 static bool stop_endpoints(struct snd_usb_substream *subs, bool keep_pending)
248 {
249 	bool stopped = 0;
250 
251 	if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
252 		snd_usb_endpoint_stop(subs->sync_endpoint, keep_pending);
253 		stopped = true;
254 	}
255 	if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
256 		snd_usb_endpoint_stop(subs->data_endpoint, keep_pending);
257 		stopped = true;
258 	}
259 	return stopped;
260 }
261 
262 static int start_endpoints(struct snd_usb_substream *subs)
263 {
264 	int err;
265 
266 	if (!subs->data_endpoint)
267 		return -EINVAL;
268 
269 	if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
270 		err = snd_usb_endpoint_start(subs->data_endpoint);
271 		if (err < 0) {
272 			clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
273 			goto error;
274 		}
275 	}
276 
277 	if (subs->sync_endpoint &&
278 	    !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
279 		err = snd_usb_endpoint_start(subs->sync_endpoint);
280 		if (err < 0) {
281 			clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
282 			goto error;
283 		}
284 	}
285 
286 	return 0;
287 
288  error:
289 	stop_endpoints(subs, false);
290 	return err;
291 }
292 
293 static void sync_pending_stops(struct snd_usb_substream *subs)
294 {
295 	snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
296 	snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
297 }
298 
299 /* PCM sync_stop callback */
300 static int snd_usb_pcm_sync_stop(struct snd_pcm_substream *substream)
301 {
302 	struct snd_usb_substream *subs = substream->runtime->private_data;
303 
304 	sync_pending_stops(subs);
305 	return 0;
306 }
307 
308 /* Set up sync endpoint */
309 int snd_usb_audioformat_set_sync_ep(struct snd_usb_audio *chip,
310 				    struct audioformat *fmt)
311 {
312 	struct usb_device *dev = chip->dev;
313 	struct usb_host_interface *alts;
314 	struct usb_interface_descriptor *altsd;
315 	unsigned int ep, attr, sync_attr;
316 	bool is_playback;
317 	int err;
318 
319 	if (fmt->sync_ep)
320 		return 0; /* already set up */
321 
322 	alts = snd_usb_get_host_interface(chip, fmt->iface, fmt->altsetting);
323 	if (!alts)
324 		return 0;
325 	altsd = get_iface_desc(alts);
326 
327 	err = snd_usb_parse_implicit_fb_quirk(chip, fmt, alts);
328 	if (err > 0)
329 		return 0; /* matched */
330 
331 	/*
332 	 * Generic sync EP handling
333 	 */
334 
335 	if (fmt->ep_idx > 0 || altsd->bNumEndpoints < 2)
336 		return 0;
337 
338 	is_playback = !(get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
339 	attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
340 	if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
341 			     attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
342 	    (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
343 		return 0;
344 
345 	sync_attr = get_endpoint(alts, 1)->bmAttributes;
346 
347 	/*
348 	 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
349 	 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
350 	 * error fall back to SYNC mode and don't create sync endpoint
351 	 */
352 
353 	/* check sync-pipe endpoint */
354 	/* ... and check descriptor size before accessing bSynchAddress
355 	   because there is a version of the SB Audigy 2 NX firmware lacking
356 	   the audio fields in the endpoint descriptors */
357 	if ((sync_attr & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
358 	    (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
359 	     get_endpoint(alts, 1)->bSynchAddress != 0)) {
360 		dev_err(&dev->dev,
361 			"%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
362 			   fmt->iface, fmt->altsetting,
363 			   get_endpoint(alts, 1)->bmAttributes,
364 			   get_endpoint(alts, 1)->bLength,
365 			   get_endpoint(alts, 1)->bSynchAddress);
366 		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
367 			return 0;
368 		return -EINVAL;
369 	}
370 	ep = get_endpoint(alts, 1)->bEndpointAddress;
371 	if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
372 	    get_endpoint(alts, 0)->bSynchAddress != 0 &&
373 	    ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
374 	     (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
375 		dev_err(&dev->dev,
376 			"%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
377 			   fmt->iface, fmt->altsetting,
378 			   is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
379 		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
380 			return 0;
381 		return -EINVAL;
382 	}
383 
384 	fmt->sync_ep = ep;
385 	fmt->sync_iface = altsd->bInterfaceNumber;
386 	fmt->sync_altsetting = altsd->bAlternateSetting;
387 	fmt->sync_ep_idx = 1;
388 	if ((sync_attr & USB_ENDPOINT_USAGE_MASK) == USB_ENDPOINT_USAGE_IMPLICIT_FB)
389 		fmt->implicit_fb = 1;
390 
391 	dev_dbg(&dev->dev, "%d:%d: found sync_ep=0x%x, iface=%d, alt=%d, implicit_fb=%d\n",
392 		fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface,
393 		fmt->sync_altsetting, fmt->implicit_fb);
394 
395 	return 0;
396 }
397 
398 static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state)
399 {
400 	int ret;
401 
402 	if (!subs->str_pd)
403 		return 0;
404 
405 	ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
406 	if (ret < 0) {
407 		dev_err(&subs->dev->dev,
408 			"Cannot change Power Domain ID: %d to state: %d. Err: %d\n",
409 			subs->str_pd->pd_id, state, ret);
410 		return ret;
411 	}
412 
413 	return 0;
414 }
415 
416 int snd_usb_pcm_suspend(struct snd_usb_stream *as)
417 {
418 	int ret;
419 
420 	ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2);
421 	if (ret < 0)
422 		return ret;
423 
424 	ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2);
425 	if (ret < 0)
426 		return ret;
427 
428 	return 0;
429 }
430 
431 int snd_usb_pcm_resume(struct snd_usb_stream *as)
432 {
433 	int ret;
434 
435 	ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1);
436 	if (ret < 0)
437 		return ret;
438 
439 	ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1);
440 	if (ret < 0)
441 		return ret;
442 
443 	return 0;
444 }
445 
446 static void close_endpoints(struct snd_usb_audio *chip,
447 			    struct snd_usb_substream *subs)
448 {
449 	if (subs->data_endpoint) {
450 		snd_usb_endpoint_set_sync(chip, subs->data_endpoint, NULL);
451 		snd_usb_endpoint_close(chip, subs->data_endpoint);
452 		subs->data_endpoint = NULL;
453 	}
454 
455 	if (subs->sync_endpoint) {
456 		snd_usb_endpoint_close(chip, subs->sync_endpoint);
457 		subs->sync_endpoint = NULL;
458 	}
459 }
460 
461 /*
462  * hw_params callback
463  *
464  * allocate a buffer and set the given audio format.
465  *
466  * so far we use a physically linear buffer although packetize transfer
467  * doesn't need a continuous area.
468  * if sg buffer is supported on the later version of alsa, we'll follow
469  * that.
470  */
471 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
472 			     struct snd_pcm_hw_params *hw_params)
473 {
474 	struct snd_usb_substream *subs = substream->runtime->private_data;
475 	struct snd_usb_audio *chip = subs->stream->chip;
476 	const struct audioformat *fmt;
477 	const struct audioformat *sync_fmt;
478 	bool fixed_rate, sync_fixed_rate;
479 	int ret;
480 
481 	ret = snd_media_start_pipeline(subs);
482 	if (ret)
483 		return ret;
484 
485 	fixed_rate = snd_usb_pcm_has_fixed_rate(subs);
486 	fmt = find_substream_format(subs, hw_params);
487 	if (!fmt) {
488 		usb_audio_dbg(chip,
489 			      "cannot find format: format=%s, rate=%d, channels=%d\n",
490 			      snd_pcm_format_name(params_format(hw_params)),
491 			      params_rate(hw_params), params_channels(hw_params));
492 		ret = -EINVAL;
493 		goto stop_pipeline;
494 	}
495 
496 	if (fmt->implicit_fb) {
497 		sync_fmt = snd_usb_find_implicit_fb_sync_format(chip, fmt,
498 								hw_params,
499 								!substream->stream,
500 								&sync_fixed_rate);
501 		if (!sync_fmt) {
502 			usb_audio_dbg(chip,
503 				      "cannot find sync format: ep=0x%x, iface=%d:%d, format=%s, rate=%d, channels=%d\n",
504 				      fmt->sync_ep, fmt->sync_iface,
505 				      fmt->sync_altsetting,
506 				      snd_pcm_format_name(params_format(hw_params)),
507 				      params_rate(hw_params), params_channels(hw_params));
508 			ret = -EINVAL;
509 			goto stop_pipeline;
510 		}
511 	} else {
512 		sync_fmt = fmt;
513 		sync_fixed_rate = fixed_rate;
514 	}
515 
516 	ret = snd_usb_lock_shutdown(chip);
517 	if (ret < 0)
518 		goto stop_pipeline;
519 
520 	ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
521 	if (ret < 0)
522 		goto unlock;
523 
524 	if (subs->data_endpoint) {
525 		if (snd_usb_endpoint_compatible(chip, subs->data_endpoint,
526 						fmt, hw_params))
527 			goto unlock;
528 		close_endpoints(chip, subs);
529 	}
530 
531 	subs->data_endpoint = snd_usb_endpoint_open(chip, fmt, hw_params, false, fixed_rate);
532 	if (!subs->data_endpoint) {
533 		ret = -EINVAL;
534 		goto unlock;
535 	}
536 
537 	if (fmt->sync_ep) {
538 		subs->sync_endpoint = snd_usb_endpoint_open(chip, sync_fmt,
539 							    hw_params,
540 							    fmt == sync_fmt,
541 							    sync_fixed_rate);
542 		if (!subs->sync_endpoint) {
543 			ret = -EINVAL;
544 			goto unlock;
545 		}
546 
547 		snd_usb_endpoint_set_sync(chip, subs->data_endpoint,
548 					  subs->sync_endpoint);
549 	}
550 
551 	mutex_lock(&chip->mutex);
552 	subs->cur_audiofmt = fmt;
553 	mutex_unlock(&chip->mutex);
554 
555 	if (!subs->data_endpoint->need_setup)
556 		goto unlock;
557 
558 	if (subs->sync_endpoint) {
559 		ret = snd_usb_endpoint_set_params(chip, subs->sync_endpoint);
560 		if (ret < 0)
561 			goto unlock;
562 	}
563 
564 	ret = snd_usb_endpoint_set_params(chip, subs->data_endpoint);
565 
566  unlock:
567 	if (ret < 0)
568 		close_endpoints(chip, subs);
569 
570 	snd_usb_unlock_shutdown(chip);
571  stop_pipeline:
572 	if (ret < 0)
573 		snd_media_stop_pipeline(subs);
574 
575 	return ret;
576 }
577 
578 /*
579  * hw_free callback
580  *
581  * reset the audio format and release the buffer
582  */
583 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
584 {
585 	struct snd_usb_substream *subs = substream->runtime->private_data;
586 	struct snd_usb_audio *chip = subs->stream->chip;
587 
588 	snd_media_stop_pipeline(subs);
589 	mutex_lock(&chip->mutex);
590 	subs->cur_audiofmt = NULL;
591 	mutex_unlock(&chip->mutex);
592 	if (!snd_usb_lock_shutdown(chip)) {
593 		if (stop_endpoints(subs, false))
594 			sync_pending_stops(subs);
595 		close_endpoints(chip, subs);
596 		snd_usb_unlock_shutdown(chip);
597 	}
598 
599 	return 0;
600 }
601 
602 /* free-wheeling mode? (e.g. dmix) */
603 static int in_free_wheeling_mode(struct snd_pcm_runtime *runtime)
604 {
605 	return runtime->stop_threshold > runtime->buffer_size;
606 }
607 
608 /* check whether early start is needed for playback stream */
609 static int lowlatency_playback_available(struct snd_pcm_runtime *runtime,
610 					 struct snd_usb_substream *subs)
611 {
612 	struct snd_usb_audio *chip = subs->stream->chip;
613 
614 	if (subs->direction == SNDRV_PCM_STREAM_CAPTURE)
615 		return false;
616 	/* disabled via module option? */
617 	if (!chip->lowlatency)
618 		return false;
619 	if (in_free_wheeling_mode(runtime))
620 		return false;
621 	/* implicit feedback mode has own operation mode */
622 	if (snd_usb_endpoint_implicit_feedback_sink(subs->data_endpoint))
623 		return false;
624 	return true;
625 }
626 
627 /*
628  * prepare callback
629  *
630  * only a few subtle things...
631  */
632 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
633 {
634 	struct snd_pcm_runtime *runtime = substream->runtime;
635 	struct snd_usb_substream *subs = runtime->private_data;
636 	struct snd_usb_audio *chip = subs->stream->chip;
637 	int retry = 0;
638 	int ret;
639 
640 	ret = snd_usb_lock_shutdown(chip);
641 	if (ret < 0)
642 		return ret;
643 	if (snd_BUG_ON(!subs->data_endpoint)) {
644 		ret = -EIO;
645 		goto unlock;
646 	}
647 
648  again:
649 	if (subs->sync_endpoint) {
650 		ret = snd_usb_endpoint_prepare(chip, subs->sync_endpoint);
651 		if (ret < 0)
652 			goto unlock;
653 	}
654 
655 	ret = snd_usb_endpoint_prepare(chip, subs->data_endpoint);
656 	if (ret < 0)
657 		goto unlock;
658 	else if (ret > 0)
659 		snd_usb_set_format_quirk(subs, subs->cur_audiofmt);
660 	ret = 0;
661 
662 	/* reset the pointer */
663 	subs->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
664 	subs->inflight_bytes = 0;
665 	subs->hwptr_done = 0;
666 	subs->transfer_done = 0;
667 	subs->last_frame_number = 0;
668 	subs->period_elapsed_pending = 0;
669 	runtime->delay = 0;
670 
671 	subs->lowlatency_playback = lowlatency_playback_available(runtime, subs);
672 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
673 	    !subs->lowlatency_playback) {
674 		ret = start_endpoints(subs);
675 		/* if XRUN happens at starting streams (possibly with implicit
676 		 * fb case), restart again, but only try once.
677 		 */
678 		if (ret == -EPIPE && !retry++) {
679 			sync_pending_stops(subs);
680 			goto again;
681 		}
682 	}
683  unlock:
684 	snd_usb_unlock_shutdown(chip);
685 	return ret;
686 }
687 
688 /*
689  * h/w constraints
690  */
691 
692 #ifdef HW_CONST_DEBUG
693 #define hwc_debug(fmt, args...) pr_debug(fmt, ##args)
694 #else
695 #define hwc_debug(fmt, args...) do { } while(0)
696 #endif
697 
698 static const struct snd_pcm_hardware snd_usb_hardware =
699 {
700 	.info =			SNDRV_PCM_INFO_MMAP |
701 				SNDRV_PCM_INFO_MMAP_VALID |
702 				SNDRV_PCM_INFO_BATCH |
703 				SNDRV_PCM_INFO_INTERLEAVED |
704 				SNDRV_PCM_INFO_BLOCK_TRANSFER |
705 				SNDRV_PCM_INFO_PAUSE,
706 	.channels_min =		1,
707 	.channels_max =		256,
708 	.buffer_bytes_max =	INT_MAX, /* limited by BUFFER_TIME later */
709 	.period_bytes_min =	64,
710 	.period_bytes_max =	INT_MAX, /* limited by PERIOD_TIME later */
711 	.periods_min =		2,
712 	.periods_max =		1024,
713 };
714 
715 static int hw_check_valid_format(struct snd_usb_substream *subs,
716 				 struct snd_pcm_hw_params *params,
717 				 const struct audioformat *fp)
718 {
719 	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
720 	struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
721 	struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
722 	struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
723 	struct snd_mask check_fmts;
724 	unsigned int ptime;
725 
726 	/* check the format */
727 	snd_mask_none(&check_fmts);
728 	check_fmts.bits[0] = (u32)fp->formats;
729 	check_fmts.bits[1] = (u32)(fp->formats >> 32);
730 	snd_mask_intersect(&check_fmts, fmts);
731 	if (snd_mask_empty(&check_fmts)) {
732 		hwc_debug("   > check: no supported format 0x%llx\n", fp->formats);
733 		return 0;
734 	}
735 	/* check the channels */
736 	if (fp->channels < ct->min || fp->channels > ct->max) {
737 		hwc_debug("   > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
738 		return 0;
739 	}
740 	/* check the rate is within the range */
741 	if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
742 		hwc_debug("   > check: rate_min %d > max %d\n", fp->rate_min, it->max);
743 		return 0;
744 	}
745 	if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
746 		hwc_debug("   > check: rate_max %d < min %d\n", fp->rate_max, it->min);
747 		return 0;
748 	}
749 	/* check whether the period time is >= the data packet interval */
750 	if (subs->speed != USB_SPEED_FULL) {
751 		ptime = 125 * (1 << fp->datainterval);
752 		if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
753 			hwc_debug("   > check: ptime %u > max %u\n", ptime, pt->max);
754 			return 0;
755 		}
756 	}
757 	return 1;
758 }
759 
760 static int apply_hw_params_minmax(struct snd_interval *it, unsigned int rmin,
761 				  unsigned int rmax)
762 {
763 	int changed;
764 
765 	if (rmin > rmax) {
766 		hwc_debug("  --> get empty\n");
767 		it->empty = 1;
768 		return -EINVAL;
769 	}
770 
771 	changed = 0;
772 	if (it->min < rmin) {
773 		it->min = rmin;
774 		it->openmin = 0;
775 		changed = 1;
776 	}
777 	if (it->max > rmax) {
778 		it->max = rmax;
779 		it->openmax = 0;
780 		changed = 1;
781 	}
782 	if (snd_interval_checkempty(it)) {
783 		it->empty = 1;
784 		return -EINVAL;
785 	}
786 	hwc_debug("  --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
787 	return changed;
788 }
789 
790 static int hw_rule_rate(struct snd_pcm_hw_params *params,
791 			struct snd_pcm_hw_rule *rule)
792 {
793 	struct snd_usb_substream *subs = rule->private;
794 	struct snd_usb_audio *chip = subs->stream->chip;
795 	const struct audioformat *fp;
796 	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
797 	unsigned int rmin, rmax, r;
798 	int i;
799 
800 	hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
801 	rmin = UINT_MAX;
802 	rmax = 0;
803 	list_for_each_entry(fp, &subs->fmt_list, list) {
804 		if (!hw_check_valid_format(subs, params, fp))
805 			continue;
806 		r = snd_usb_endpoint_get_clock_rate(chip, fp->clock);
807 		if (r > 0) {
808 			if (!snd_interval_test(it, r))
809 				continue;
810 			rmin = min(rmin, r);
811 			rmax = max(rmax, r);
812 			continue;
813 		}
814 		if (fp->rate_table && fp->nr_rates) {
815 			for (i = 0; i < fp->nr_rates; i++) {
816 				r = fp->rate_table[i];
817 				if (!snd_interval_test(it, r))
818 					continue;
819 				rmin = min(rmin, r);
820 				rmax = max(rmax, r);
821 			}
822 		} else {
823 			rmin = min(rmin, fp->rate_min);
824 			rmax = max(rmax, fp->rate_max);
825 		}
826 	}
827 
828 	return apply_hw_params_minmax(it, rmin, rmax);
829 }
830 
831 
832 static int hw_rule_channels(struct snd_pcm_hw_params *params,
833 			    struct snd_pcm_hw_rule *rule)
834 {
835 	struct snd_usb_substream *subs = rule->private;
836 	const struct audioformat *fp;
837 	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
838 	unsigned int rmin, rmax;
839 
840 	hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
841 	rmin = UINT_MAX;
842 	rmax = 0;
843 	list_for_each_entry(fp, &subs->fmt_list, list) {
844 		if (!hw_check_valid_format(subs, params, fp))
845 			continue;
846 		rmin = min(rmin, fp->channels);
847 		rmax = max(rmax, fp->channels);
848 	}
849 
850 	return apply_hw_params_minmax(it, rmin, rmax);
851 }
852 
853 static int apply_hw_params_format_bits(struct snd_mask *fmt, u64 fbits)
854 {
855 	u32 oldbits[2];
856 	int changed;
857 
858 	oldbits[0] = fmt->bits[0];
859 	oldbits[1] = fmt->bits[1];
860 	fmt->bits[0] &= (u32)fbits;
861 	fmt->bits[1] &= (u32)(fbits >> 32);
862 	if (!fmt->bits[0] && !fmt->bits[1]) {
863 		hwc_debug("  --> get empty\n");
864 		return -EINVAL;
865 	}
866 	changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
867 	hwc_debug("  --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
868 	return changed;
869 }
870 
871 static int hw_rule_format(struct snd_pcm_hw_params *params,
872 			  struct snd_pcm_hw_rule *rule)
873 {
874 	struct snd_usb_substream *subs = rule->private;
875 	const struct audioformat *fp;
876 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
877 	u64 fbits;
878 
879 	hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
880 	fbits = 0;
881 	list_for_each_entry(fp, &subs->fmt_list, list) {
882 		if (!hw_check_valid_format(subs, params, fp))
883 			continue;
884 		fbits |= fp->formats;
885 	}
886 	return apply_hw_params_format_bits(fmt, fbits);
887 }
888 
889 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
890 			       struct snd_pcm_hw_rule *rule)
891 {
892 	struct snd_usb_substream *subs = rule->private;
893 	const struct audioformat *fp;
894 	struct snd_interval *it;
895 	unsigned char min_datainterval;
896 	unsigned int pmin;
897 
898 	it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
899 	hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
900 	min_datainterval = 0xff;
901 	list_for_each_entry(fp, &subs->fmt_list, list) {
902 		if (!hw_check_valid_format(subs, params, fp))
903 			continue;
904 		min_datainterval = min(min_datainterval, fp->datainterval);
905 	}
906 	if (min_datainterval == 0xff) {
907 		hwc_debug("  --> get empty\n");
908 		it->empty = 1;
909 		return -EINVAL;
910 	}
911 	pmin = 125 * (1 << min_datainterval);
912 
913 	return apply_hw_params_minmax(it, pmin, UINT_MAX);
914 }
915 
916 /* get the EP or the sync EP for implicit fb when it's already set up */
917 static const struct snd_usb_endpoint *
918 get_sync_ep_from_substream(struct snd_usb_substream *subs)
919 {
920 	struct snd_usb_audio *chip = subs->stream->chip;
921 	const struct audioformat *fp;
922 	const struct snd_usb_endpoint *ep;
923 
924 	list_for_each_entry(fp, &subs->fmt_list, list) {
925 		ep = snd_usb_get_endpoint(chip, fp->endpoint);
926 		if (ep && ep->cur_audiofmt) {
927 			/* if EP is already opened solely for this substream,
928 			 * we still allow us to change the parameter; otherwise
929 			 * this substream has to follow the existing parameter
930 			 */
931 			if (ep->cur_audiofmt != subs->cur_audiofmt || ep->opened > 1)
932 				return ep;
933 		}
934 		if (!fp->implicit_fb)
935 			continue;
936 		/* for the implicit fb, check the sync ep as well */
937 		ep = snd_usb_get_endpoint(chip, fp->sync_ep);
938 		if (ep && ep->cur_audiofmt)
939 			return ep;
940 	}
941 	return NULL;
942 }
943 
944 /* additional hw constraints for implicit feedback mode */
945 static int hw_rule_format_implicit_fb(struct snd_pcm_hw_params *params,
946 				      struct snd_pcm_hw_rule *rule)
947 {
948 	struct snd_usb_substream *subs = rule->private;
949 	const struct snd_usb_endpoint *ep;
950 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
951 
952 	ep = get_sync_ep_from_substream(subs);
953 	if (!ep)
954 		return 0;
955 
956 	hwc_debug("applying %s\n", __func__);
957 	return apply_hw_params_format_bits(fmt, pcm_format_to_bits(ep->cur_format));
958 }
959 
960 static int hw_rule_rate_implicit_fb(struct snd_pcm_hw_params *params,
961 				    struct snd_pcm_hw_rule *rule)
962 {
963 	struct snd_usb_substream *subs = rule->private;
964 	const struct snd_usb_endpoint *ep;
965 	struct snd_interval *it;
966 
967 	ep = get_sync_ep_from_substream(subs);
968 	if (!ep)
969 		return 0;
970 
971 	hwc_debug("applying %s\n", __func__);
972 	it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
973 	return apply_hw_params_minmax(it, ep->cur_rate, ep->cur_rate);
974 }
975 
976 static int hw_rule_period_size_implicit_fb(struct snd_pcm_hw_params *params,
977 					   struct snd_pcm_hw_rule *rule)
978 {
979 	struct snd_usb_substream *subs = rule->private;
980 	const struct snd_usb_endpoint *ep;
981 	struct snd_interval *it;
982 
983 	ep = get_sync_ep_from_substream(subs);
984 	if (!ep)
985 		return 0;
986 
987 	hwc_debug("applying %s\n", __func__);
988 	it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
989 	return apply_hw_params_minmax(it, ep->cur_period_frames,
990 				      ep->cur_period_frames);
991 }
992 
993 static int hw_rule_periods_implicit_fb(struct snd_pcm_hw_params *params,
994 				       struct snd_pcm_hw_rule *rule)
995 {
996 	struct snd_usb_substream *subs = rule->private;
997 	const struct snd_usb_endpoint *ep;
998 	struct snd_interval *it;
999 
1000 	ep = get_sync_ep_from_substream(subs);
1001 	if (!ep)
1002 		return 0;
1003 
1004 	hwc_debug("applying %s\n", __func__);
1005 	it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIODS);
1006 	return apply_hw_params_minmax(it, ep->cur_buffer_periods,
1007 				      ep->cur_buffer_periods);
1008 }
1009 
1010 /*
1011  * set up the runtime hardware information.
1012  */
1013 
1014 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1015 {
1016 	const struct audioformat *fp;
1017 	unsigned int pt, ptmin;
1018 	int param_period_time_if_needed = -1;
1019 	int err;
1020 
1021 	runtime->hw.formats = subs->formats;
1022 
1023 	runtime->hw.rate_min = 0x7fffffff;
1024 	runtime->hw.rate_max = 0;
1025 	runtime->hw.channels_min = 256;
1026 	runtime->hw.channels_max = 0;
1027 	runtime->hw.rates = 0;
1028 	ptmin = UINT_MAX;
1029 	/* check min/max rates and channels */
1030 	list_for_each_entry(fp, &subs->fmt_list, list) {
1031 		runtime->hw.rates |= fp->rates;
1032 		if (runtime->hw.rate_min > fp->rate_min)
1033 			runtime->hw.rate_min = fp->rate_min;
1034 		if (runtime->hw.rate_max < fp->rate_max)
1035 			runtime->hw.rate_max = fp->rate_max;
1036 		if (runtime->hw.channels_min > fp->channels)
1037 			runtime->hw.channels_min = fp->channels;
1038 		if (runtime->hw.channels_max < fp->channels)
1039 			runtime->hw.channels_max = fp->channels;
1040 		if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1041 			/* FIXME: there might be more than one audio formats... */
1042 			runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1043 				fp->frame_size;
1044 		}
1045 		pt = 125 * (1 << fp->datainterval);
1046 		ptmin = min(ptmin, pt);
1047 	}
1048 
1049 	param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
1050 	if (subs->speed == USB_SPEED_FULL)
1051 		/* full speed devices have fixed data packet interval */
1052 		ptmin = 1000;
1053 	if (ptmin == 1000)
1054 		/* if period time doesn't go below 1 ms, no rules needed */
1055 		param_period_time_if_needed = -1;
1056 
1057 	err = snd_pcm_hw_constraint_minmax(runtime,
1058 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1059 					   ptmin, UINT_MAX);
1060 	if (err < 0)
1061 		return err;
1062 
1063 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1064 				  hw_rule_rate, subs,
1065 				  SNDRV_PCM_HW_PARAM_RATE,
1066 				  SNDRV_PCM_HW_PARAM_FORMAT,
1067 				  SNDRV_PCM_HW_PARAM_CHANNELS,
1068 				  param_period_time_if_needed,
1069 				  -1);
1070 	if (err < 0)
1071 		return err;
1072 
1073 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1074 				  hw_rule_channels, subs,
1075 				  SNDRV_PCM_HW_PARAM_CHANNELS,
1076 				  SNDRV_PCM_HW_PARAM_FORMAT,
1077 				  SNDRV_PCM_HW_PARAM_RATE,
1078 				  param_period_time_if_needed,
1079 				  -1);
1080 	if (err < 0)
1081 		return err;
1082 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1083 				  hw_rule_format, subs,
1084 				  SNDRV_PCM_HW_PARAM_FORMAT,
1085 				  SNDRV_PCM_HW_PARAM_RATE,
1086 				  SNDRV_PCM_HW_PARAM_CHANNELS,
1087 				  param_period_time_if_needed,
1088 				  -1);
1089 	if (err < 0)
1090 		return err;
1091 	if (param_period_time_if_needed >= 0) {
1092 		err = snd_pcm_hw_rule_add(runtime, 0,
1093 					  SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1094 					  hw_rule_period_time, subs,
1095 					  SNDRV_PCM_HW_PARAM_FORMAT,
1096 					  SNDRV_PCM_HW_PARAM_CHANNELS,
1097 					  SNDRV_PCM_HW_PARAM_RATE,
1098 					  -1);
1099 		if (err < 0)
1100 			return err;
1101 	}
1102 
1103 	/* set max period and buffer sizes for 1 and 2 seconds, respectively */
1104 	err = snd_pcm_hw_constraint_minmax(runtime,
1105 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1106 					   0, 1000000);
1107 	if (err < 0)
1108 		return err;
1109 	err = snd_pcm_hw_constraint_minmax(runtime,
1110 					   SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1111 					   0, 2000000);
1112 	if (err < 0)
1113 		return err;
1114 
1115 	/* additional hw constraints for implicit fb */
1116 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1117 				  hw_rule_format_implicit_fb, subs,
1118 				  SNDRV_PCM_HW_PARAM_FORMAT, -1);
1119 	if (err < 0)
1120 		return err;
1121 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1122 				  hw_rule_rate_implicit_fb, subs,
1123 				  SNDRV_PCM_HW_PARAM_RATE, -1);
1124 	if (err < 0)
1125 		return err;
1126 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1127 				  hw_rule_period_size_implicit_fb, subs,
1128 				  SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1129 	if (err < 0)
1130 		return err;
1131 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1132 				  hw_rule_periods_implicit_fb, subs,
1133 				  SNDRV_PCM_HW_PARAM_PERIODS, -1);
1134 	if (err < 0)
1135 		return err;
1136 
1137 	list_for_each_entry(fp, &subs->fmt_list, list) {
1138 		if (fp->implicit_fb) {
1139 			runtime->hw.info |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1140 			break;
1141 		}
1142 	}
1143 
1144 	return 0;
1145 }
1146 
1147 static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
1148 {
1149 	int direction = substream->stream;
1150 	struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1151 	struct snd_pcm_runtime *runtime = substream->runtime;
1152 	struct snd_usb_substream *subs = &as->substream[direction];
1153 	int ret;
1154 
1155 	runtime->hw = snd_usb_hardware;
1156 	/* need an explicit sync to catch applptr update in low-latency mode */
1157 	if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
1158 	    as->chip->lowlatency)
1159 		runtime->hw.info |= SNDRV_PCM_INFO_SYNC_APPLPTR;
1160 	runtime->private_data = subs;
1161 	subs->pcm_substream = substream;
1162 	/* runtime PM is also done there */
1163 
1164 	/* initialize DSD/DOP context */
1165 	subs->dsd_dop.byte_idx = 0;
1166 	subs->dsd_dop.channel = 0;
1167 	subs->dsd_dop.marker = 1;
1168 
1169 	ret = setup_hw_info(runtime, subs);
1170 	if (ret < 0)
1171 		return ret;
1172 	ret = snd_usb_autoresume(subs->stream->chip);
1173 	if (ret < 0)
1174 		return ret;
1175 	ret = snd_media_stream_init(subs, as->pcm, direction);
1176 	if (ret < 0)
1177 		snd_usb_autosuspend(subs->stream->chip);
1178 	return ret;
1179 }
1180 
1181 static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
1182 {
1183 	int direction = substream->stream;
1184 	struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1185 	struct snd_usb_substream *subs = &as->substream[direction];
1186 	int ret;
1187 
1188 	snd_media_stop_pipeline(subs);
1189 
1190 	if (!snd_usb_lock_shutdown(subs->stream->chip)) {
1191 		ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
1192 		snd_usb_unlock_shutdown(subs->stream->chip);
1193 		if (ret < 0)
1194 			return ret;
1195 	}
1196 
1197 	subs->pcm_substream = NULL;
1198 	snd_usb_autosuspend(subs->stream->chip);
1199 
1200 	return 0;
1201 }
1202 
1203 /* Since a URB can handle only a single linear buffer, we must use double
1204  * buffering when the data to be transferred overflows the buffer boundary.
1205  * To avoid inconsistencies when updating hwptr_done, we use double buffering
1206  * for all URBs.
1207  */
1208 static void retire_capture_urb(struct snd_usb_substream *subs,
1209 			       struct urb *urb)
1210 {
1211 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1212 	unsigned int stride, frames, bytes, oldptr;
1213 	int i, period_elapsed = 0;
1214 	unsigned long flags;
1215 	unsigned char *cp;
1216 	int current_frame_number;
1217 
1218 	/* read frame number here, update pointer in critical section */
1219 	current_frame_number = usb_get_current_frame_number(subs->dev);
1220 
1221 	stride = runtime->frame_bits >> 3;
1222 
1223 	for (i = 0; i < urb->number_of_packets; i++) {
1224 		cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
1225 		if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1226 			dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1227 				i, urb->iso_frame_desc[i].status);
1228 			// continue;
1229 		}
1230 		bytes = urb->iso_frame_desc[i].actual_length;
1231 		if (subs->stream_offset_adj > 0) {
1232 			unsigned int adj = min(subs->stream_offset_adj, bytes);
1233 			cp += adj;
1234 			bytes -= adj;
1235 			subs->stream_offset_adj -= adj;
1236 		}
1237 		frames = bytes / stride;
1238 		if (!subs->txfr_quirk)
1239 			bytes = frames * stride;
1240 		if (bytes % (runtime->sample_bits >> 3) != 0) {
1241 			int oldbytes = bytes;
1242 			bytes = frames * stride;
1243 			dev_warn_ratelimited(&subs->dev->dev,
1244 				 "Corrected urb data len. %d->%d\n",
1245 							oldbytes, bytes);
1246 		}
1247 		/* update the current pointer */
1248 		spin_lock_irqsave(&subs->lock, flags);
1249 		oldptr = subs->hwptr_done;
1250 		subs->hwptr_done += bytes;
1251 		if (subs->hwptr_done >= subs->buffer_bytes)
1252 			subs->hwptr_done -= subs->buffer_bytes;
1253 		frames = (bytes + (oldptr % stride)) / stride;
1254 		subs->transfer_done += frames;
1255 		if (subs->transfer_done >= runtime->period_size) {
1256 			subs->transfer_done -= runtime->period_size;
1257 			period_elapsed = 1;
1258 		}
1259 
1260 		/* realign last_frame_number */
1261 		subs->last_frame_number = current_frame_number;
1262 
1263 		spin_unlock_irqrestore(&subs->lock, flags);
1264 		/* copy a data chunk */
1265 		if (oldptr + bytes > subs->buffer_bytes) {
1266 			unsigned int bytes1 = subs->buffer_bytes - oldptr;
1267 
1268 			memcpy(runtime->dma_area + oldptr, cp, bytes1);
1269 			memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1270 		} else {
1271 			memcpy(runtime->dma_area + oldptr, cp, bytes);
1272 		}
1273 	}
1274 
1275 	if (period_elapsed)
1276 		snd_pcm_period_elapsed(subs->pcm_substream);
1277 }
1278 
1279 static void urb_ctx_queue_advance(struct snd_usb_substream *subs,
1280 				  struct urb *urb, unsigned int bytes)
1281 {
1282 	struct snd_urb_ctx *ctx = urb->context;
1283 
1284 	ctx->queued += bytes;
1285 	subs->inflight_bytes += bytes;
1286 	subs->hwptr_done += bytes;
1287 	if (subs->hwptr_done >= subs->buffer_bytes)
1288 		subs->hwptr_done -= subs->buffer_bytes;
1289 }
1290 
1291 static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1292 					     struct urb *urb, unsigned int bytes)
1293 {
1294 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1295 	unsigned int dst_idx = 0;
1296 	unsigned int src_idx = subs->hwptr_done;
1297 	unsigned int wrap = subs->buffer_bytes;
1298 	u8 *dst = urb->transfer_buffer;
1299 	u8 *src = runtime->dma_area;
1300 	static const u8 marker[] = { 0x05, 0xfa };
1301 	unsigned int queued = 0;
1302 
1303 	/*
1304 	 * The DSP DOP format defines a way to transport DSD samples over
1305 	 * normal PCM data endpoints. It requires stuffing of marker bytes
1306 	 * (0x05 and 0xfa, alternating per sample frame), and then expects
1307 	 * 2 additional bytes of actual payload. The whole frame is stored
1308 	 * LSB.
1309 	 *
1310 	 * Hence, for a stereo transport, the buffer layout looks like this,
1311 	 * where L refers to left channel samples and R to right.
1312 	 *
1313 	 *   L1 L2 0x05   R1 R2 0x05   L3 L4 0xfa  R3 R4 0xfa
1314 	 *   L5 L6 0x05   R5 R6 0x05   L7 L8 0xfa  R7 R8 0xfa
1315 	 *   .....
1316 	 *
1317 	 */
1318 
1319 	while (bytes--) {
1320 		if (++subs->dsd_dop.byte_idx == 3) {
1321 			/* frame boundary? */
1322 			dst[dst_idx++] = marker[subs->dsd_dop.marker];
1323 			src_idx += 2;
1324 			subs->dsd_dop.byte_idx = 0;
1325 
1326 			if (++subs->dsd_dop.channel % runtime->channels == 0) {
1327 				/* alternate the marker */
1328 				subs->dsd_dop.marker++;
1329 				subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1330 				subs->dsd_dop.channel = 0;
1331 			}
1332 		} else {
1333 			/* stuff the DSD payload */
1334 			int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
1335 
1336 			if (subs->cur_audiofmt->dsd_bitrev)
1337 				dst[dst_idx++] = bitrev8(src[idx]);
1338 			else
1339 				dst[dst_idx++] = src[idx];
1340 			queued++;
1341 		}
1342 	}
1343 
1344 	urb_ctx_queue_advance(subs, urb, queued);
1345 }
1346 
1347 /* copy bit-reversed bytes onto transfer buffer */
1348 static void fill_playback_urb_dsd_bitrev(struct snd_usb_substream *subs,
1349 					 struct urb *urb, unsigned int bytes)
1350 {
1351 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1352 	const u8 *src = runtime->dma_area;
1353 	u8 *buf = urb->transfer_buffer;
1354 	int i, ofs = subs->hwptr_done;
1355 
1356 	for (i = 0; i < bytes; i++) {
1357 		*buf++ = bitrev8(src[ofs]);
1358 		if (++ofs >= subs->buffer_bytes)
1359 			ofs = 0;
1360 	}
1361 
1362 	urb_ctx_queue_advance(subs, urb, bytes);
1363 }
1364 
1365 static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1366 			int offset, int stride, unsigned int bytes)
1367 {
1368 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1369 
1370 	if (subs->hwptr_done + bytes > subs->buffer_bytes) {
1371 		/* err, the transferred area goes over buffer boundary. */
1372 		unsigned int bytes1 = subs->buffer_bytes - subs->hwptr_done;
1373 
1374 		memcpy(urb->transfer_buffer + offset,
1375 		       runtime->dma_area + subs->hwptr_done, bytes1);
1376 		memcpy(urb->transfer_buffer + offset + bytes1,
1377 		       runtime->dma_area, bytes - bytes1);
1378 	} else {
1379 		memcpy(urb->transfer_buffer + offset,
1380 		       runtime->dma_area + subs->hwptr_done, bytes);
1381 	}
1382 
1383 	urb_ctx_queue_advance(subs, urb, bytes);
1384 }
1385 
1386 static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1387 				      struct urb *urb, int stride,
1388 				      unsigned int bytes)
1389 {
1390 	__le32 packet_length;
1391 	int i;
1392 
1393 	/* Put __le32 length descriptor at start of each packet. */
1394 	for (i = 0; i < urb->number_of_packets; i++) {
1395 		unsigned int length = urb->iso_frame_desc[i].length;
1396 		unsigned int offset = urb->iso_frame_desc[i].offset;
1397 
1398 		packet_length = cpu_to_le32(length);
1399 		offset += i * sizeof(packet_length);
1400 		urb->iso_frame_desc[i].offset = offset;
1401 		urb->iso_frame_desc[i].length += sizeof(packet_length);
1402 		memcpy(urb->transfer_buffer + offset,
1403 		       &packet_length, sizeof(packet_length));
1404 		copy_to_urb(subs, urb, offset + sizeof(packet_length),
1405 			    stride, length);
1406 	}
1407 	/* Adjust transfer size accordingly. */
1408 	bytes += urb->number_of_packets * sizeof(packet_length);
1409 	return bytes;
1410 }
1411 
1412 static int prepare_playback_urb(struct snd_usb_substream *subs,
1413 				struct urb *urb,
1414 				bool in_stream_lock)
1415 {
1416 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1417 	struct snd_usb_endpoint *ep = subs->data_endpoint;
1418 	struct snd_urb_ctx *ctx = urb->context;
1419 	unsigned int frames, bytes;
1420 	int counts;
1421 	unsigned int transfer_done, frame_limit, avail = 0;
1422 	int i, stride, period_elapsed = 0;
1423 	unsigned long flags;
1424 	int err = 0;
1425 
1426 	stride = ep->stride;
1427 
1428 	frames = 0;
1429 	ctx->queued = 0;
1430 	urb->number_of_packets = 0;
1431 
1432 	spin_lock_irqsave(&subs->lock, flags);
1433 	frame_limit = subs->frame_limit + ep->max_urb_frames;
1434 	transfer_done = subs->transfer_done;
1435 
1436 	if (subs->lowlatency_playback &&
1437 	    runtime->state != SNDRV_PCM_STATE_DRAINING) {
1438 		unsigned int hwptr = subs->hwptr_done / stride;
1439 
1440 		/* calculate the byte offset-in-buffer of the appl_ptr */
1441 		avail = (runtime->control->appl_ptr - runtime->hw_ptr_base)
1442 			% runtime->buffer_size;
1443 		if (avail <= hwptr)
1444 			avail += runtime->buffer_size;
1445 		avail -= hwptr;
1446 	}
1447 
1448 	for (i = 0; i < ctx->packets; i++) {
1449 		counts = snd_usb_endpoint_next_packet_size(ep, ctx, i, avail);
1450 		if (counts < 0)
1451 			break;
1452 		/* set up descriptor */
1453 		urb->iso_frame_desc[i].offset = frames * stride;
1454 		urb->iso_frame_desc[i].length = counts * stride;
1455 		frames += counts;
1456 		avail -= counts;
1457 		urb->number_of_packets++;
1458 		transfer_done += counts;
1459 		if (transfer_done >= runtime->period_size) {
1460 			transfer_done -= runtime->period_size;
1461 			frame_limit = 0;
1462 			period_elapsed = 1;
1463 			if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1464 				if (transfer_done > 0) {
1465 					/* FIXME: fill-max mode is not
1466 					 * supported yet */
1467 					frames -= transfer_done;
1468 					counts -= transfer_done;
1469 					urb->iso_frame_desc[i].length =
1470 						counts * stride;
1471 					transfer_done = 0;
1472 				}
1473 				i++;
1474 				if (i < ctx->packets) {
1475 					/* add a transfer delimiter */
1476 					urb->iso_frame_desc[i].offset =
1477 						frames * stride;
1478 					urb->iso_frame_desc[i].length = 0;
1479 					urb->number_of_packets++;
1480 				}
1481 				break;
1482 			}
1483 		}
1484 		/* finish at the period boundary or after enough frames */
1485 		if ((period_elapsed || transfer_done >= frame_limit) &&
1486 		    !snd_usb_endpoint_implicit_feedback_sink(ep))
1487 			break;
1488 	}
1489 
1490 	if (!frames) {
1491 		err = -EAGAIN;
1492 		goto unlock;
1493 	}
1494 
1495 	bytes = frames * stride;
1496 	subs->transfer_done = transfer_done;
1497 	subs->frame_limit = frame_limit;
1498 	if (unlikely(ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1499 		     subs->cur_audiofmt->dsd_dop)) {
1500 		fill_playback_urb_dsd_dop(subs, urb, bytes);
1501 	} else if (unlikely(ep->cur_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1502 			   subs->cur_audiofmt->dsd_bitrev)) {
1503 		fill_playback_urb_dsd_bitrev(subs, urb, bytes);
1504 	} else {
1505 		/* usual PCM */
1506 		if (!subs->tx_length_quirk)
1507 			copy_to_urb(subs, urb, 0, stride, bytes);
1508 		else
1509 			bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1510 			/* bytes is now amount of outgoing data */
1511 	}
1512 
1513 	subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1514 
1515 	if (subs->trigger_tstamp_pending_update) {
1516 		/* this is the first actual URB submitted,
1517 		 * update trigger timestamp to reflect actual start time
1518 		 */
1519 		snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1520 		subs->trigger_tstamp_pending_update = false;
1521 	}
1522 
1523 	if (period_elapsed && !subs->running && subs->lowlatency_playback) {
1524 		subs->period_elapsed_pending = 1;
1525 		period_elapsed = 0;
1526 	}
1527 
1528  unlock:
1529 	spin_unlock_irqrestore(&subs->lock, flags);
1530 	if (err < 0)
1531 		return err;
1532 	urb->transfer_buffer_length = bytes;
1533 	if (period_elapsed) {
1534 		if (in_stream_lock)
1535 			snd_pcm_period_elapsed_under_stream_lock(subs->pcm_substream);
1536 		else
1537 			snd_pcm_period_elapsed(subs->pcm_substream);
1538 	}
1539 	return 0;
1540 }
1541 
1542 /*
1543  * process after playback data complete
1544  * - decrease the delay count again
1545  */
1546 static void retire_playback_urb(struct snd_usb_substream *subs,
1547 			       struct urb *urb)
1548 {
1549 	unsigned long flags;
1550 	struct snd_urb_ctx *ctx = urb->context;
1551 	bool period_elapsed = false;
1552 
1553 	spin_lock_irqsave(&subs->lock, flags);
1554 	if (ctx->queued) {
1555 		if (subs->inflight_bytes >= ctx->queued)
1556 			subs->inflight_bytes -= ctx->queued;
1557 		else
1558 			subs->inflight_bytes = 0;
1559 	}
1560 
1561 	subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1562 	if (subs->running) {
1563 		period_elapsed = subs->period_elapsed_pending;
1564 		subs->period_elapsed_pending = 0;
1565 	}
1566 	spin_unlock_irqrestore(&subs->lock, flags);
1567 	if (period_elapsed)
1568 		snd_pcm_period_elapsed(subs->pcm_substream);
1569 }
1570 
1571 /* PCM ack callback for the playback stream;
1572  * this plays a role only when the stream is running in low-latency mode.
1573  */
1574 static int snd_usb_pcm_playback_ack(struct snd_pcm_substream *substream)
1575 {
1576 	struct snd_usb_substream *subs = substream->runtime->private_data;
1577 	struct snd_usb_endpoint *ep;
1578 
1579 	if (!subs->lowlatency_playback || !subs->running)
1580 		return 0;
1581 	ep = subs->data_endpoint;
1582 	if (!ep)
1583 		return 0;
1584 	/* When no more in-flight URBs available, try to process the pending
1585 	 * outputs here
1586 	 */
1587 	if (!ep->active_mask)
1588 		snd_usb_queue_pending_output_urbs(ep, true);
1589 	return 0;
1590 }
1591 
1592 static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1593 					      int cmd)
1594 {
1595 	struct snd_usb_substream *subs = substream->runtime->private_data;
1596 	int err;
1597 
1598 	switch (cmd) {
1599 	case SNDRV_PCM_TRIGGER_START:
1600 		subs->trigger_tstamp_pending_update = true;
1601 		fallthrough;
1602 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1603 		snd_usb_endpoint_set_callback(subs->data_endpoint,
1604 					      prepare_playback_urb,
1605 					      retire_playback_urb,
1606 					      subs);
1607 		if (subs->lowlatency_playback &&
1608 		    cmd == SNDRV_PCM_TRIGGER_START) {
1609 			if (in_free_wheeling_mode(substream->runtime))
1610 				subs->lowlatency_playback = false;
1611 			err = start_endpoints(subs);
1612 			if (err < 0) {
1613 				snd_usb_endpoint_set_callback(subs->data_endpoint,
1614 							      NULL, NULL, NULL);
1615 				return err;
1616 			}
1617 		}
1618 		subs->running = 1;
1619 		dev_dbg(&subs->dev->dev, "%d:%d Start Playback PCM\n",
1620 			subs->cur_audiofmt->iface,
1621 			subs->cur_audiofmt->altsetting);
1622 		return 0;
1623 	case SNDRV_PCM_TRIGGER_SUSPEND:
1624 	case SNDRV_PCM_TRIGGER_STOP:
1625 		stop_endpoints(subs, substream->runtime->state == SNDRV_PCM_STATE_DRAINING);
1626 		snd_usb_endpoint_set_callback(subs->data_endpoint,
1627 					      NULL, NULL, NULL);
1628 		subs->running = 0;
1629 		dev_dbg(&subs->dev->dev, "%d:%d Stop Playback PCM\n",
1630 			subs->cur_audiofmt->iface,
1631 			subs->cur_audiofmt->altsetting);
1632 		return 0;
1633 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1634 		/* keep retire_data_urb for delay calculation */
1635 		snd_usb_endpoint_set_callback(subs->data_endpoint,
1636 					      NULL,
1637 					      retire_playback_urb,
1638 					      subs);
1639 		subs->running = 0;
1640 		dev_dbg(&subs->dev->dev, "%d:%d Pause Playback PCM\n",
1641 			subs->cur_audiofmt->iface,
1642 			subs->cur_audiofmt->altsetting);
1643 		return 0;
1644 	}
1645 
1646 	return -EINVAL;
1647 }
1648 
1649 static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1650 					     int cmd)
1651 {
1652 	int err;
1653 	struct snd_usb_substream *subs = substream->runtime->private_data;
1654 
1655 	switch (cmd) {
1656 	case SNDRV_PCM_TRIGGER_START:
1657 		err = start_endpoints(subs);
1658 		if (err < 0)
1659 			return err;
1660 		fallthrough;
1661 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1662 		snd_usb_endpoint_set_callback(subs->data_endpoint,
1663 					      NULL, retire_capture_urb,
1664 					      subs);
1665 		subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1666 		subs->running = 1;
1667 		dev_dbg(&subs->dev->dev, "%d:%d Start Capture PCM\n",
1668 			subs->cur_audiofmt->iface,
1669 			subs->cur_audiofmt->altsetting);
1670 		return 0;
1671 	case SNDRV_PCM_TRIGGER_SUSPEND:
1672 	case SNDRV_PCM_TRIGGER_STOP:
1673 		stop_endpoints(subs, false);
1674 		fallthrough;
1675 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1676 		snd_usb_endpoint_set_callback(subs->data_endpoint,
1677 					      NULL, NULL, NULL);
1678 		subs->running = 0;
1679 		dev_dbg(&subs->dev->dev, "%d:%d Stop Capture PCM\n",
1680 			subs->cur_audiofmt->iface,
1681 			subs->cur_audiofmt->altsetting);
1682 		return 0;
1683 	}
1684 
1685 	return -EINVAL;
1686 }
1687 
1688 static const struct snd_pcm_ops snd_usb_playback_ops = {
1689 	.open =		snd_usb_pcm_open,
1690 	.close =	snd_usb_pcm_close,
1691 	.hw_params =	snd_usb_hw_params,
1692 	.hw_free =	snd_usb_hw_free,
1693 	.prepare =	snd_usb_pcm_prepare,
1694 	.trigger =	snd_usb_substream_playback_trigger,
1695 	.sync_stop =	snd_usb_pcm_sync_stop,
1696 	.pointer =	snd_usb_pcm_pointer,
1697 	.ack =		snd_usb_pcm_playback_ack,
1698 };
1699 
1700 static const struct snd_pcm_ops snd_usb_capture_ops = {
1701 	.open =		snd_usb_pcm_open,
1702 	.close =	snd_usb_pcm_close,
1703 	.hw_params =	snd_usb_hw_params,
1704 	.hw_free =	snd_usb_hw_free,
1705 	.prepare =	snd_usb_pcm_prepare,
1706 	.trigger =	snd_usb_substream_capture_trigger,
1707 	.sync_stop =	snd_usb_pcm_sync_stop,
1708 	.pointer =	snd_usb_pcm_pointer,
1709 };
1710 
1711 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1712 {
1713 	const struct snd_pcm_ops *ops;
1714 
1715 	ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
1716 			&snd_usb_playback_ops : &snd_usb_capture_ops;
1717 	snd_pcm_set_ops(pcm, stream, ops);
1718 }
1719 
1720 void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
1721 {
1722 	struct snd_pcm *pcm = subs->stream->pcm;
1723 	struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
1724 	struct device *dev = subs->dev->bus->sysdev;
1725 
1726 	if (snd_usb_use_vmalloc)
1727 		snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_VMALLOC,
1728 					   NULL, 0, 0);
1729 	else
1730 		snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_DEV_SG,
1731 					   dev, 64*1024, 512*1024);
1732 }
1733