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