pcm.c 43.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
Daniel Mack's avatar
Daniel Mack committed
2
3
4
5
/*
 */

#include <linux/init.h>
6
#include <linux/slab.h>
7
#include <linux/bitrev.h>
8
#include <linux/ratelimit.h>
Daniel Mack's avatar
Daniel Mack committed
9
10
#include <linux/usb.h>
#include <linux/usb/audio.h>
Daniel Mack's avatar
Daniel Mack committed
11
#include <linux/usb/audio-v2.h>
Daniel Mack's avatar
Daniel Mack committed
12
13
14
15
16
17
18
19

#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>

#include "usbaudio.h"
#include "card.h"
#include "quirks.h"
20
#include "endpoint.h"
Daniel Mack's avatar
Daniel Mack committed
21
22
#include "helper.h"
#include "pcm.h"
23
#include "clock.h"
24
#include "power.h"
25
#include "media.h"
26
#include "implicit.h"
Daniel Mack's avatar
Daniel Mack committed
27

28
29
30
#define SUBSTREAM_FLAG_DATA_EP_STARTED	0
#define SUBSTREAM_FLAG_SYNC_EP_STARTED	1

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
36
	unsigned int current_frame_number;
	unsigned int frame_diff;
37
	int est_delay;
38
	int queued;
39

40
41
42
43
44
45
46
	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
		queued = bytes_to_frames(runtime, subs->inflight_bytes);
		if (!queued)
			return 0;
	} else if (!subs->running) {
		return 0;
	}
47

48
49
50
51
52
53
54
55
56
57
	current_frame_number = usb_get_current_frame_number(subs->dev);
	/*
	 * HCD implementations use different widths, use lower 8 bits.
	 * The delay will be managed up to 256ms, which is more than
	 * enough
	 */
	frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;

	/* Approximation based on number of samples per USB frame (ms),
	   some truncation for 44.1 but the estimate is good enough */
58
59
60
61
62
63
64
	est_delay = frame_diff * runtime->rate / 1000;

	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
		est_delay = queued - est_delay;
		if (est_delay < 0)
			est_delay = 0;
	}
65

66
67
68
	return est_delay;
}

Daniel Mack's avatar
Daniel Mack committed
69
70
71
72
73
/*
 * return the current pcm pointer.  just based on the hwptr_done value.
 */
static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
{
74
75
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_usb_substream *subs = runtime->private_data;
Daniel Mack's avatar
Daniel Mack committed
76
77
	unsigned int hwptr_done;

78
	if (atomic_read(&subs->stream->chip->shutdown))
79
		return SNDRV_PCM_POS_XRUN;
Daniel Mack's avatar
Daniel Mack committed
80
81
	spin_lock(&subs->lock);
	hwptr_done = subs->hwptr_done;
82
	runtime->delay = snd_usb_pcm_delay(subs, runtime);
Daniel Mack's avatar
Daniel Mack committed
83
	spin_unlock(&subs->lock);
84
	return bytes_to_frames(runtime, hwptr_done);
Daniel Mack's avatar
Daniel Mack committed
85
86
87
88
89
}

/*
 * find a matching audio format
 */
90
static const struct audioformat *
91
92
93
find_format(struct list_head *fmt_list_head, snd_pcm_format_t format,
	    unsigned int rate, unsigned int channels, bool strict_match,
	    struct snd_usb_substream *subs)
Daniel Mack's avatar
Daniel Mack committed
94
{
95
96
	const struct audioformat *fp;
	const struct audioformat *found = NULL;
Daniel Mack's avatar
Daniel Mack committed
97
98
	int cur_attr = 0, attr;

99
	list_for_each_entry(fp, fmt_list_head, list) {
100
101
102
103
104
105
		if (strict_match) {
			if (!(fp->formats & pcm_format_to_bits(format)))
				continue;
			if (fp->channels != channels)
				continue;
		}
106
		if (rate < fp->rate_min || rate > fp->rate_max)
Daniel Mack's avatar
Daniel Mack committed
107
			continue;
108
		if (!(fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
Daniel Mack's avatar
Daniel Mack committed
109
110
			unsigned int i;
			for (i = 0; i < fp->nr_rates; i++)
111
				if (fp->rate_table[i] == rate)
Daniel Mack's avatar
Daniel Mack committed
112
113
114
115
116
					break;
			if (i >= fp->nr_rates)
				continue;
		}
		attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
117
		if (!found) {
Daniel Mack's avatar
Daniel Mack committed
118
119
120
121
122
123
124
125
126
			found = fp;
			cur_attr = attr;
			continue;
		}
		/* avoid async out and adaptive in if the other method
		 * supports the same format.
		 * this is a workaround for the case like
		 * M-audio audiophile USB.
		 */
127
		if (subs && attr != cur_attr) {
Daniel Mack's avatar
Daniel Mack committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
			if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
			     subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
			    (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
			     subs->direction == SNDRV_PCM_STREAM_CAPTURE))
				continue;
			if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
			     subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
			    (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
			     subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
				found = fp;
				cur_attr = attr;
				continue;
			}
		}
		/* find the format with the largest max. packet size */
		if (fp->maxpacksize > found->maxpacksize) {
			found = fp;
			cur_attr = attr;
		}
	}
	return found;
}

151
static const struct audioformat *
152
153
find_substream_format(struct snd_usb_substream *subs,
		      const struct snd_pcm_hw_params *params)
154
{
155
156
157
	return find_format(&subs->fmt_list, params_format(params),
			   params_rate(params), params_channels(params),
			   true, subs);
158
159
}

160
static int init_pitch_v1(struct snd_usb_audio *chip, int ep)
161
162
163
164
165
166
{
	struct usb_device *dev = chip->dev;
	unsigned char data[1];
	int err;

	data[0] = 1;
167
168
169
170
	err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
			      USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
			      UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
			      data, sizeof(data));
171
	return err;
172
}
Daniel Mack's avatar
Daniel Mack committed
173

174
static int init_pitch_v2(struct snd_usb_audio *chip, int ep)
175
176
177
178
179
180
{
	struct usb_device *dev = chip->dev;
	unsigned char data[1];
	int err;

	data[0] = 1;
181
182
183
184
	err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
			      USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
			      UAC2_EP_CS_PITCH << 8, 0,
			      data, sizeof(data));
185
	return err;
186
187
}

Daniel Mack's avatar
Daniel Mack committed
188
/*
189
 * initialize the pitch control and sample rate
Daniel Mack's avatar
Daniel Mack committed
190
 */
191
int snd_usb_init_pitch(struct snd_usb_audio *chip,
192
		       const struct audioformat *fmt)
Daniel Mack's avatar
Daniel Mack committed
193
{
194
195
	int err;

196
197
198
199
	/* if endpoint doesn't have pitch control, bail out */
	if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
		return 0;

200
201
	usb_audio_dbg(chip, "enable PITCH for EP 0x%x\n", fmt->endpoint);

202
	switch (fmt->protocol) {
203
	case UAC_VERSION_1:
204
205
206
207
208
		err = init_pitch_v1(chip, fmt->endpoint);
		break;
	case UAC_VERSION_2:
		err = init_pitch_v2(chip, fmt->endpoint);
		break;
209
	default:
210
211
		return 0;
	}
212

213
214
215
216
	if (err < 0) {
		usb_audio_err(chip, "failed to enable PITCH for EP 0x%x\n",
			      fmt->endpoint);
		return err;
217
	}
218
219

	return 0;
220
221
}

222
static bool stop_endpoints(struct snd_usb_substream *subs)
223
{
224
225
	bool stopped = 0;

226
227
	if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
		snd_usb_endpoint_stop(subs->sync_endpoint);
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);
232
233
234
		stopped = true;
	}
	return stopped;
235
236
}

237
static int start_endpoints(struct snd_usb_substream *subs)
238
239
240
241
242
243
244
{
	int err;

	if (!subs->data_endpoint)
		return -EINVAL;

	if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
245
		err = snd_usb_endpoint_start(subs->data_endpoint);
246
247
		if (err < 0) {
			clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
248
			goto error;
249
250
251
252
253
		}
	}

	if (subs->sync_endpoint &&
	    !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
254
		err = snd_usb_endpoint_start(subs->sync_endpoint);
255
256
		if (err < 0) {
			clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
257
			goto error;
258
259
260
261
		}
	}

	return 0;
262
263
264
265

 error:
	stop_endpoints(subs);
	return err;
266
267
}

268
269
270
271
272
273
274
275
276
277
278
static void sync_pending_stops(struct snd_usb_substream *subs)
{
	snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
	snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
}

/* PCM sync_stop callback */
static int snd_usb_pcm_sync_stop(struct snd_pcm_substream *substream)
{
	struct snd_usb_substream *subs = substream->runtime->private_data;

279
	sync_pending_stops(subs);
280
	return 0;
281
282
}

283
/* Set up sync endpoint */
284
285
int snd_usb_audioformat_set_sync_ep(struct snd_usb_audio *chip,
				    struct audioformat *fmt)
286
{
287
288
289
290
291
	struct usb_device *dev = chip->dev;
	struct usb_host_interface *alts;
	struct usb_interface_descriptor *altsd;
	unsigned int ep, attr, sync_attr;
	bool is_playback;
292
293
	int err;

294
	alts = snd_usb_get_host_interface(chip, fmt->iface, fmt->altsetting);
295
	if (!alts)
296
		return 0;
297
298
	altsd = get_iface_desc(alts);

299
300
301
302
303
304
305
	err = snd_usb_parse_implicit_fb_quirk(chip, fmt, alts);
	if (err > 0)
		return 0; /* matched */

	/*
	 * Generic sync EP handling
	 */
306

307
308
	if (altsd->bNumEndpoints < 2)
		return 0;
309

310
	is_playback = !(get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
311
	attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
312
313
	if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
			     attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
314
315
	    (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
		return 0;
316

317
318
	sync_attr = get_endpoint(alts, 1)->bmAttributes;

319
320
321
322
323
324
	/*
	 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
	 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
	 * error fall back to SYNC mode and don't create sync endpoint
	 */

325
326
327
328
	/* check sync-pipe endpoint */
	/* ... and check descriptor size before accessing bSynchAddress
	   because there is a version of the SB Audigy 2 NX firmware lacking
	   the audio fields in the endpoint descriptors */
329
	if ((sync_attr & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
330
	    (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
331
	     get_endpoint(alts, 1)->bSynchAddress != 0)) {
332
333
334
		dev_err(&dev->dev,
			"%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
			   fmt->iface, fmt->altsetting,
335
336
337
			   get_endpoint(alts, 1)->bmAttributes,
			   get_endpoint(alts, 1)->bLength,
			   get_endpoint(alts, 1)->bSynchAddress);
338
339
		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
			return 0;
340
341
342
		return -EINVAL;
	}
	ep = get_endpoint(alts, 1)->bEndpointAddress;
343
	if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
344
	    get_endpoint(alts, 0)->bSynchAddress != 0 &&
345
346
	    ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
	     (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
347
348
349
		dev_err(&dev->dev,
			"%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
			   fmt->iface, fmt->altsetting,
350
			   is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
351
352
		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
			return 0;
353
		return -EINVAL;
354
	}
Daniel Mack's avatar
Daniel Mack committed
355

356
357
358
	fmt->sync_ep = ep;
	fmt->sync_iface = altsd->bInterfaceNumber;
	fmt->sync_altsetting = altsd->bAlternateSetting;
359
	fmt->sync_ep_idx = 1;
360
361
362
363
364
365
366
367
368
369
	if ((sync_attr & USB_ENDPOINT_USAGE_MASK) == USB_ENDPOINT_USAGE_IMPLICIT_FB)
		fmt->implicit_fb = 1;

	dev_dbg(&dev->dev, "%d:%d: found sync_ep=0x%x, iface=%d, alt=%d, implicit_fb=%d\n",
		fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface,
		fmt->sync_altsetting, fmt->implicit_fb);

	return 0;
}

370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state)
{
	int ret;

	if (!subs->str_pd)
		return 0;

	ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
	if (ret < 0) {
		dev_err(&subs->dev->dev,
			"Cannot change Power Domain ID: %d to state: %d. Err: %d\n",
			subs->str_pd->pd_id, state, ret);
		return ret;
	}

	return 0;
}

int snd_usb_pcm_suspend(struct snd_usb_stream *as)
{
	int ret;

	ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2);
	if (ret < 0)
		return ret;

	ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2);
	if (ret < 0)
		return ret;

	return 0;
}

int snd_usb_pcm_resume(struct snd_usb_stream *as)
{
	int ret;

407
	ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1);
408
409
410
	if (ret < 0)
		return ret;

411
	ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1);
412
413
414
415
416
417
	if (ret < 0)
		return ret;

	return 0;
}

418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
static void close_endpoints(struct snd_usb_audio *chip,
			    struct snd_usb_substream *subs)
{
	if (subs->data_endpoint) {
		snd_usb_endpoint_set_sync(chip, subs->data_endpoint, NULL);
		snd_usb_endpoint_close(chip, subs->data_endpoint);
		subs->data_endpoint = NULL;
	}

	if (subs->sync_endpoint) {
		snd_usb_endpoint_close(chip, subs->sync_endpoint);
		subs->sync_endpoint = NULL;
	}
}

static int configure_endpoints(struct snd_usb_audio *chip,
			       struct snd_usb_substream *subs)
{
	int err;

	if (subs->data_endpoint->need_setup) {
		/* stop any running stream beforehand */
		if (stop_endpoints(subs))
			sync_pending_stops(subs);
		err = snd_usb_endpoint_configure(chip, subs->data_endpoint);
		if (err < 0)
			return err;
		snd_usb_set_format_quirk(subs, subs->cur_audiofmt);
	}

	if (subs->sync_endpoint) {
		err = snd_usb_endpoint_configure(chip, subs->sync_endpoint);
		if (err < 0)
			return err;
	}

	return 0;
}

Daniel Mack's avatar
Daniel Mack committed
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
 * hw_params callback
 *
 * allocate a buffer and set the given audio format.
 *
 * so far we use a physically linear buffer although packetize transfer
 * doesn't need a continuous area.
 * if sg buffer is supported on the later version of alsa, we'll follow
 * that.
 */
static int snd_usb_hw_params(struct snd_pcm_substream *substream,
			     struct snd_pcm_hw_params *hw_params)
{
	struct snd_usb_substream *subs = substream->runtime->private_data;
471
	struct snd_usb_audio *chip = subs->stream->chip;
472
473
	const struct audioformat *fmt;
	const struct audioformat *sync_fmt;
474
	int ret;
Daniel Mack's avatar
Daniel Mack committed
475

476
477
478
479
	ret = snd_media_start_pipeline(subs);
	if (ret)
		return ret;

480
	fmt = find_substream_format(subs, hw_params);
Daniel Mack's avatar
Daniel Mack committed
481
	if (!fmt) {
482
483
484
485
		usb_audio_dbg(chip,
			      "cannot find format: format=%s, rate=%d, channels=%d\n",
			      snd_pcm_format_name(params_format(hw_params)),
			      params_rate(hw_params), params_channels(hw_params));
486
487
		ret = -EINVAL;
		goto stop_pipeline;
Daniel Mack's avatar
Daniel Mack committed
488
489
	}

490
491
492
493
	if (fmt->implicit_fb) {
		sync_fmt = snd_usb_find_implicit_fb_sync_format(chip, fmt,
								hw_params,
								!substream->stream);
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
		if (!sync_fmt) {
			usb_audio_dbg(chip,
				      "cannot find sync format: ep=0x%x, iface=%d:%d, format=%s, rate=%d, channels=%d\n",
				      fmt->sync_ep, fmt->sync_iface,
				      fmt->sync_altsetting,
				      snd_pcm_format_name(params_format(hw_params)),
				      params_rate(hw_params), params_channels(hw_params));
			ret = -EINVAL;
			goto stop_pipeline;
		}
	} else {
		sync_fmt = fmt;
	}

	ret = snd_usb_lock_shutdown(chip);
509
	if (ret < 0)
510
		goto stop_pipeline;
511
512
513
514
515

	ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
	if (ret < 0)
		goto unlock;

516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
	if (subs->data_endpoint) {
		if (snd_usb_endpoint_compatible(chip, subs->data_endpoint,
						fmt, hw_params))
			goto unlock;
		close_endpoints(chip, subs);
	}

	subs->data_endpoint = snd_usb_endpoint_open(chip, fmt, hw_params, false);
	if (!subs->data_endpoint) {
		ret = -EINVAL;
		goto unlock;
	}

	if (fmt->sync_ep) {
		subs->sync_endpoint = snd_usb_endpoint_open(chip, sync_fmt,
							    hw_params,
							    fmt == sync_fmt);
		if (!subs->sync_endpoint) {
			ret = -EINVAL;
			goto unlock;
		}

		snd_usb_endpoint_set_sync(chip, subs->data_endpoint,
					  subs->sync_endpoint);
	}

542
	mutex_lock(&chip->mutex);
543
	subs->cur_audiofmt = fmt;
544
	mutex_unlock(&chip->mutex);
545
546
547

	ret = configure_endpoints(chip, subs);

548
 unlock:
549
	if (ret < 0)
550
		close_endpoints(chip, subs);
551

552
	snd_usb_unlock_shutdown(chip);
553
 stop_pipeline:
554
555
556
	if (ret < 0)
		snd_media_stop_pipeline(subs);

557
	return ret;
Daniel Mack's avatar
Daniel Mack committed
558
559
560
561
562
563
564
565
566
567
}

/*
 * hw_free callback
 *
 * reset the audio format and release the buffer
 */
static int snd_usb_hw_free(struct snd_pcm_substream *substream)
{
	struct snd_usb_substream *subs = substream->runtime->private_data;
568
	struct snd_usb_audio *chip = subs->stream->chip;
Daniel Mack's avatar
Daniel Mack committed
569

570
	snd_media_stop_pipeline(subs);
571
	mutex_lock(&chip->mutex);
Daniel Mack's avatar
Daniel Mack committed
572
	subs->cur_audiofmt = NULL;
573
	mutex_unlock(&chip->mutex);
574
	if (!snd_usb_lock_shutdown(chip)) {
575
576
577
		if (stop_endpoints(subs))
			sync_pending_stops(subs);
		close_endpoints(chip, subs);
578
		snd_usb_unlock_shutdown(chip);
579
	}
580

581
	return 0;
Daniel Mack's avatar
Daniel Mack committed
582
583
584
585
586
587
588
589
590
591
592
}

/*
 * prepare callback
 *
 * only a few subtle things...
 */
static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_usb_substream *subs = runtime->private_data;
593
	struct snd_usb_audio *chip = subs->stream->chip;
594
	int ret;
Daniel Mack's avatar
Daniel Mack committed
595

596
	ret = snd_usb_lock_shutdown(chip);
597
598
	if (ret < 0)
		return ret;
599
600
601
602
	if (snd_BUG_ON(!subs->data_endpoint)) {
		ret = -EIO;
		goto unlock;
	}
603

604
	ret = configure_endpoints(chip, subs);
605
	if (ret < 0)
606
		goto unlock;
607

Daniel Mack's avatar
Daniel Mack committed
608
	/* reset the pointer */
609
	subs->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
610
	subs->inflight_bytes = 0;
Daniel Mack's avatar
Daniel Mack committed
611
612
	subs->hwptr_done = 0;
	subs->transfer_done = 0;
613
	subs->last_frame_number = 0;
614
	subs->period_elapsed_pending = 0;
Daniel Mack's avatar
Daniel Mack committed
615
616
	runtime->delay = 0;

617
618
619
620
621
622
623
624
	/* check whether early start is needed for playback stream */
	subs->early_playback_start =
		subs->direction == SNDRV_PCM_STREAM_PLAYBACK &&
		subs->data_endpoint->nominal_queue_size >= subs->buffer_bytes;

	if (subs->early_playback_start)
		ret = start_endpoints(subs);

625
 unlock:
626
	snd_usb_unlock_shutdown(chip);
627
	return ret;
Daniel Mack's avatar
Daniel Mack committed
628
629
}

Takashi Iwai's avatar
Takashi Iwai committed
630
631
632
633
634
635
636
637
638
639
/*
 * h/w constraints
 */

#ifdef HW_CONST_DEBUG
#define hwc_debug(fmt, args...) pr_debug(fmt, ##args)
#else
#define hwc_debug(fmt, args...) do { } while(0)
#endif

640
static const struct snd_pcm_hardware snd_usb_hardware =
Daniel Mack's avatar
Daniel Mack committed
641
642
643
644
645
646
647
{
	.info =			SNDRV_PCM_INFO_MMAP |
				SNDRV_PCM_INFO_MMAP_VALID |
				SNDRV_PCM_INFO_BATCH |
				SNDRV_PCM_INFO_INTERLEAVED |
				SNDRV_PCM_INFO_BLOCK_TRANSFER |
				SNDRV_PCM_INFO_PAUSE,
648
649
	.channels_min =		1,
	.channels_max =		256,
Daniel Mack's avatar
Daniel Mack committed
650
651
652
653
654
655
656
657
658
	.buffer_bytes_max =	1024 * 1024,
	.period_bytes_min =	64,
	.period_bytes_max =	512 * 1024,
	.periods_min =		2,
	.periods_max =		1024,
};

static int hw_check_valid_format(struct snd_usb_substream *subs,
				 struct snd_pcm_hw_params *params,
659
				 const struct audioformat *fp)
Daniel Mack's avatar
Daniel Mack committed
660
661
662
663
664
{
	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
	struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
	struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
	struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
665
	struct snd_mask check_fmts;
Daniel Mack's avatar
Daniel Mack committed
666
667
668
	unsigned int ptime;

	/* check the format */
669
670
671
672
673
	snd_mask_none(&check_fmts);
	check_fmts.bits[0] = (u32)fp->formats;
	check_fmts.bits[1] = (u32)(fp->formats >> 32);
	snd_mask_intersect(&check_fmts, fmts);
	if (snd_mask_empty(&check_fmts)) {
674
		hwc_debug("   > check: no supported format 0x%llx\n", fp->formats);
Daniel Mack's avatar
Daniel Mack committed
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
		return 0;
	}
	/* check the channels */
	if (fp->channels < ct->min || fp->channels > ct->max) {
		hwc_debug("   > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
		return 0;
	}
	/* check the rate is within the range */
	if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
		hwc_debug("   > check: rate_min %d > max %d\n", fp->rate_min, it->max);
		return 0;
	}
	if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
		hwc_debug("   > check: rate_max %d < min %d\n", fp->rate_max, it->min);
		return 0;
	}
	/* check whether the period time is >= the data packet interval */
692
	if (subs->speed != USB_SPEED_FULL) {
Daniel Mack's avatar
Daniel Mack committed
693
694
695
696
697
698
699
700
701
		ptime = 125 * (1 << fp->datainterval);
		if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
			hwc_debug("   > check: ptime %u > max %u\n", ptime, pt->max);
			return 0;
		}
	}
	return 1;
}

702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
static int apply_hw_params_minmax(struct snd_interval *it, unsigned int rmin,
				  unsigned int rmax)
{
	int changed;

	if (rmin > rmax) {
		hwc_debug("  --> get empty\n");
		it->empty = 1;
		return -EINVAL;
	}

	changed = 0;
	if (it->min < rmin) {
		it->min = rmin;
		it->openmin = 0;
		changed = 1;
	}
	if (it->max > rmax) {
		it->max = rmax;
		it->openmax = 0;
		changed = 1;
	}
	if (snd_interval_checkempty(it)) {
		it->empty = 1;
		return -EINVAL;
	}
	hwc_debug("  --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
	return changed;
}

Daniel Mack's avatar
Daniel Mack committed
732
733
734
735
static int hw_rule_rate(struct snd_pcm_hw_params *params,
			struct snd_pcm_hw_rule *rule)
{
	struct snd_usb_substream *subs = rule->private;
736
	const struct audioformat *fp;
Daniel Mack's avatar
Daniel Mack committed
737
	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
738
739
	unsigned int rmin, rmax, r;
	int i;
Daniel Mack's avatar
Daniel Mack committed
740
741

	hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
742
743
	rmin = UINT_MAX;
	rmax = 0;
744
	list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Mack's avatar
Daniel Mack committed
745
746
		if (!hw_check_valid_format(subs, params, fp))
			continue;
747
748
749
750
751
752
753
754
		if (fp->rate_table && fp->nr_rates) {
			for (i = 0; i < fp->nr_rates; i++) {
				r = fp->rate_table[i];
				if (!snd_interval_test(it, r))
					continue;
				rmin = min(rmin, r);
				rmax = max(rmax, r);
			}
Daniel Mack's avatar
Daniel Mack committed
755
		} else {
756
757
			rmin = min(rmin, fp->rate_min);
			rmax = max(rmax, fp->rate_max);
Daniel Mack's avatar
Daniel Mack committed
758
759
760
		}
	}

761
	return apply_hw_params_minmax(it, rmin, rmax);
Daniel Mack's avatar
Daniel Mack committed
762
763
764
765
766
767
768
}


static int hw_rule_channels(struct snd_pcm_hw_params *params,
			    struct snd_pcm_hw_rule *rule)
{
	struct snd_usb_substream *subs = rule->private;
769
	const struct audioformat *fp;
Daniel Mack's avatar
Daniel Mack committed
770
771
772
773
	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
	unsigned int rmin, rmax;

	hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
774
775
	rmin = UINT_MAX;
	rmax = 0;
776
	list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Mack's avatar
Daniel Mack committed
777
778
		if (!hw_check_valid_format(subs, params, fp))
			continue;
779
780
		rmin = min(rmin, fp->channels);
		rmax = max(rmax, fp->channels);
Daniel Mack's avatar
Daniel Mack committed
781
782
	}

783
	return apply_hw_params_minmax(it, rmin, rmax);
Daniel Mack's avatar
Daniel Mack committed
784
785
}

786
static int apply_hw_params_format_bits(struct snd_mask *fmt, u64 fbits)
Daniel Mack's avatar
Daniel Mack committed
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
{
	u32 oldbits[2];
	int changed;

	oldbits[0] = fmt->bits[0];
	oldbits[1] = fmt->bits[1];
	fmt->bits[0] &= (u32)fbits;
	fmt->bits[1] &= (u32)(fbits >> 32);
	if (!fmt->bits[0] && !fmt->bits[1]) {
		hwc_debug("  --> get empty\n");
		return -EINVAL;
	}
	changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
	hwc_debug("  --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
	return changed;
}

804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
static int hw_rule_format(struct snd_pcm_hw_params *params,
			  struct snd_pcm_hw_rule *rule)
{
	struct snd_usb_substream *subs = rule->private;
	const struct audioformat *fp;
	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
	u64 fbits;

	hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
	fbits = 0;
	list_for_each_entry(fp, &subs->fmt_list, list) {
		if (!hw_check_valid_format(subs, params, fp))
			continue;
		fbits |= fp->formats;
	}
	return apply_hw_params_format_bits(fmt, fbits);
}

Daniel Mack's avatar
Daniel Mack committed
822
823
824
825
static int hw_rule_period_time(struct snd_pcm_hw_params *params,
			       struct snd_pcm_hw_rule *rule)
{
	struct snd_usb_substream *subs = rule->private;
826
	const struct audioformat *fp;
Daniel Mack's avatar
Daniel Mack committed
827
828
829
830
831
832
833
834
835
836
837
838
839
	struct snd_interval *it;
	unsigned char min_datainterval;
	unsigned int pmin;

	it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
	hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
	min_datainterval = 0xff;
	list_for_each_entry(fp, &subs->fmt_list, list) {
		if (!hw_check_valid_format(subs, params, fp))
			continue;
		min_datainterval = min(min_datainterval, fp->datainterval);
	}
	if (min_datainterval == 0xff) {
840
		hwc_debug("  --> get empty\n");
Daniel Mack's avatar
Daniel Mack committed
841
842
843
844
		it->empty = 1;
		return -EINVAL;
	}
	pmin = 125 * (1 << min_datainterval);
845
846

	return apply_hw_params_minmax(it, pmin, UINT_MAX);
Daniel Mack's avatar
Daniel Mack committed
847
848
}

849
850
851
/* get the EP or the sync EP for implicit fb when it's already set up */
static const struct snd_usb_endpoint *
get_sync_ep_from_substream(struct snd_usb_substream *subs)
852
853
{
	struct snd_usb_audio *chip = subs->stream->chip;
854
	const struct audioformat *fp;
855
	const struct snd_usb_endpoint *ep;
856
857

	list_for_each_entry(fp, &subs->fmt_list, list) {
858
		ep = snd_usb_get_endpoint(chip, fp->endpoint);
859
860
861
862
863
864
865
866
		if (ep && ep->cur_audiofmt) {
			/* if EP is already opened solely for this substream,
			 * we still allow us to change the parameter; otherwise
			 * this substream has to follow the existing parameter
			 */
			if (ep->cur_audiofmt != subs->cur_audiofmt || ep->opened > 1)
				return ep;
		}
867
868
869
		if (!fp->implicit_fb)
			continue;
		/* for the implicit fb, check the sync ep as well */
870
		ep = snd_usb_get_endpoint(chip, fp->sync_ep);
871
		if (ep && ep->cur_audiofmt)
872
			return ep;
873
	}
874
875
876
877
878
879
880
881
882
883
	return NULL;
}

/* additional hw constraints for implicit feedback mode */
static int hw_rule_format_implicit_fb(struct snd_pcm_hw_params *params,
				      struct snd_pcm_hw_rule *rule)
{
	struct snd_usb_substream *subs = rule->private;
	const struct snd_usb_endpoint *ep;
	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
884

885
886
	ep = get_sync_ep_from_substream(subs);
	if (!ep)
887
888
		return 0;

889
890
891
	hwc_debug("applying %s\n", __func__);
	return apply_hw_params_format_bits(fmt, pcm_format_to_bits(ep->cur_format));
}
892

893
894
895
896
897
898
static int hw_rule_rate_implicit_fb(struct snd_pcm_hw_params *params,
				    struct snd_pcm_hw_rule *rule)
{
	struct snd_usb_substream *subs = rule->private;
	const struct snd_usb_endpoint *ep;
	struct snd_interval *it;
899

900
901
902
	ep = get_sync_ep_from_substream(subs);
	if (!ep)
		return 0;
903

904
905
906
907
908
909
910
911
912
913
914
	hwc_debug("applying %s\n", __func__);
	it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
	return apply_hw_params_minmax(it, ep->cur_rate, ep->cur_rate);
}

static int hw_rule_period_size_implicit_fb(struct snd_pcm_hw_params *params,
					   struct snd_pcm_hw_rule *rule)
{
	struct snd_usb_substream *subs = rule->private;
	const struct snd_usb_endpoint *ep;
	struct snd_interval *it;
915

916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
	ep = get_sync_ep_from_substream(subs);
	if (!ep)
		return 0;

	hwc_debug("applying %s\n", __func__);
	it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
	return apply_hw_params_minmax(it, ep->cur_period_frames,
				      ep->cur_period_frames);
}

static int hw_rule_periods_implicit_fb(struct snd_pcm_hw_params *params,
				       struct snd_pcm_hw_rule *rule)
{
	struct snd_usb_substream *subs = rule->private;
	const struct snd_usb_endpoint *ep;
	struct snd_interval *it;

	ep = get_sync_ep_from_substream(subs);
	if (!ep)
		return 0;

	hwc_debug("applying %s\n", __func__);
	it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIODS);
	return apply_hw_params_minmax(it, ep->cur_buffer_periods,
				      ep->cur_buffer_periods);
941
}
Daniel Mack's avatar
Daniel Mack committed
942
943
944
945
946
947
948

/*
 * set up the runtime hardware information.
 */

static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
{
949
	const struct audioformat *fp;
Daniel Mack's avatar
Daniel Mack committed
950
	unsigned int pt, ptmin;
951
	int param_period_time_if_needed = -1;
Daniel Mack's avatar
Daniel Mack committed
952
953
954
955
956
957
958
959
960
961
962
	int err;

	runtime->hw.formats = subs->formats;

	runtime->hw.rate_min = 0x7fffffff;
	runtime->hw.rate_max = 0;
	runtime->hw.channels_min = 256;
	runtime->hw.channels_max = 0;
	runtime->hw.rates = 0;
	ptmin = UINT_MAX;
	/* check min/max rates and channels */
963
	list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Mack's avatar
Daniel Mack committed
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
		runtime->hw.rates |= fp->rates;
		if (runtime->hw.rate_min > fp->rate_min)
			runtime->hw.rate_min = fp->rate_min;
		if (runtime->hw.rate_max < fp->rate_max)
			runtime->hw.rate_max = fp->rate_max;
		if (runtime->hw.channels_min > fp->channels)
			runtime->hw.channels_min = fp->channels;
		if (runtime->hw.channels_max < fp->channels)
			runtime->hw.channels_max = fp->channels;
		if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
			/* FIXME: there might be more than one audio formats... */
			runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
				fp->frame_size;
		}
		pt = 125 * (1 << fp->datainterval);
		ptmin = min(ptmin, pt);
	}

	param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
983
	if (subs->speed == USB_SPEED_FULL)
Daniel Mack's avatar
Daniel Mack committed
984
985
986
987
988
		/* full speed devices have fixed data packet interval */
		ptmin = 1000;
	if (ptmin == 1000)
		/* if period time doesn't go below 1 ms, no rules needed */
		param_period_time_if_needed = -1;
989
990
991
992
993
994
995
996
997

	err = snd_pcm_hw_constraint_minmax(runtime,
					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
					   ptmin, UINT_MAX);
	if (err < 0)
		return err;

	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
				  hw_rule_rate, subs,
998
				  SNDRV_PCM_HW_PARAM_RATE,
999
1000
1001
1002
1003
1004
				  SNDRV_PCM_HW_PARAM_FORMAT,
				  SNDRV_PCM_HW_PARAM_CHANNELS,
				  param_period_time_if_needed,
				  -1);
	if (err < 0)
		return err;
1005

1006
1007
	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
				  hw_rule_channels, subs,
1008
				  SNDRV_PCM_HW_PARAM_CHANNELS,
1009
1010
1011
1012
1013
1014
1015
1016
				  SNDRV_PCM_HW_PARAM_FORMAT,
				  SNDRV_PCM_HW_PARAM_RATE,
				  param_period_time_if_needed,
				  -1);
	if (err < 0)
		return err;
	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
				  hw_rule_format, subs,
1017
				  SNDRV_PCM_HW_PARAM_FORMAT,
1018
1019
1020
1021
1022
1023
				  SNDRV_PCM_HW_PARAM_RATE,
				  SNDRV_PCM_HW_PARAM_CHANNELS,
				  param_period_time_if_needed,
				  -1);
	if (err < 0)
		return err;
Daniel Mack's avatar
Daniel Mack committed
1024
1025
1026
1027
1028
1029
1030
1031
1032
	if (param_period_time_if_needed >= 0) {
		err = snd_pcm_hw_rule_add(runtime, 0,
					  SNDRV_PCM_HW_PARAM_PERIOD_TIME,
					  hw_rule_period_time, subs,
					  SNDRV_PCM_HW_PARAM_FORMAT,
					  SNDRV_PCM_HW_PARAM_CHANNELS,
					  SNDRV_PCM_HW_PARAM_RATE,
					  -1);
		if (err < 0)
1033
			return err;
Daniel Mack's avatar
Daniel Mack committed
1034
	}
1035

1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
	/* additional hw constraints for implicit fb */
	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
				  hw_rule_format_implicit_fb, subs,
				  SNDRV_PCM_HW_PARAM_FORMAT, -1);
	if (err < 0)
		return err;
	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
				  hw_rule_rate_implicit_fb, subs,
				  SNDRV_PCM_HW_PARAM_RATE, -1);
	if (err < 0)
		return err;
	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
				  hw_rule_period_size_implicit_fb, subs,
				  SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
	if (err < 0)
		return err;
	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
				  hw_rule_periods_implicit_fb, subs,
				  SNDRV_PCM_HW_PARAM_PERIODS, -1);
	if (err < 0)
		return err;

1058
	return 0;
Daniel Mack's avatar
Daniel Mack committed
1059
1060
}

1061
static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
Daniel Mack's avatar
Daniel Mack committed
1062
{
1063
	int direction = substream->stream;
Daniel Mack's avatar
Daniel Mack committed
1064
1065
1066
	struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
	struct snd_pcm_runtime *runtime = substream->runtime;
	struct snd_usb_substream *subs = &as->substream[direction];
1067
	int ret;
Daniel Mack's avatar
Daniel Mack committed
1068
1069
1070
1071

	runtime->hw = snd_usb_hardware;
	runtime->private_data = subs;
	subs->pcm_substream = substream;
1072
	/* runtime PM is also done there */
1073
1074
1075
1076
1077
1078

	/* initialize DSD/DOP context */
	subs->dsd_dop.byte_idx = 0;
	subs->dsd_dop.channel = 0;
	subs->dsd_dop.marker = 1;

1079
	ret = setup_hw_info(runtime, subs);
1080
1081
1082
1083
1084
1085
1086
1087
	if (ret < 0)
		return ret;
	ret = snd_usb_autoresume(subs->stream->chip);
	if (ret < 0)
		return ret;
	ret = snd_media_stream_init(subs, as->pcm, direction);
	if (ret < 0)
		snd_usb_autosuspend(subs->stream->chip);
1088
	return ret;
Daniel Mack's avatar
Daniel Mack committed
1089
1090
}

1091
static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
Daniel Mack's avatar
Daniel Mack committed
1092
{
1093
	int direction = substream->stream;
Daniel Mack's avatar
Daniel Mack committed
1094
1095
	struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
	struct snd_usb_substream *subs = &as->substream[direction];
1096
	int ret;
Daniel Mack's avatar
Daniel Mack committed
1097

1098
	snd_media_stop_pipeline(subs);
1099

1100
	if (!snd_usb_lock_shutdown(subs->stream->chip)) {
1101
		ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
1102
		snd_usb_unlock_shutdown(subs->stream->chip);
1103
1104
		if (ret < 0)
			return ret;
1105
1106
	}

Daniel Mack's avatar
Daniel Mack committed
1107
	subs->pcm_substream = NULL;
1108
	snd_usb_autosuspend(subs->stream->chip);
1109

1110
	return 0;
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
}

/* Since a URB can handle only a single linear buffer, we must use double
 * buffering when the data to be transferred overflows the buffer boundary.
 * To avoid inconsistencies when updating hwptr_done, we use double buffering
 * for all URBs.
 */
static void retire_capture_urb(struct snd_usb_substream *subs,
			       struct urb *urb)
{
	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
	unsigned int stride, frames, bytes, oldptr;
	int i, period_elapsed = 0;
	unsigned long flags;
	unsigned char *cp;
1126
1127
1128
1129
	int current_frame_number;

	/* read frame number here, update pointer in critical section */
	current_frame_number = usb_get_current_frame_number(subs->dev);
1130
1131
1132
1133

	stride = runtime->frame_bits >> 3;

	for (i = 0; i < urb->number_of_packets; i++) {
1134
		cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
1135
		if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1136
1137
			dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
				i, urb->iso_frame_desc[i].status);
1138
1139
1140
			// continue;
		}
		bytes = urb->iso_frame_desc[i].actual_length;
1141
1142
1143
1144
1145
1146
		if (subs->stream_offset_adj > 0) {
			unsigned int adj = min(subs->stream_offset_adj, bytes);
			cp += adj;
			bytes -= adj;
			subs->stream_offset_adj -= adj;
		}
1147
1148
1149
1150
1151
1152
		frames = bytes / stride;
		if (!subs->txfr_quirk)
			bytes = frames * stride;
		if (bytes % (runtime->sample_bits >> 3) != 0) {
			int oldbytes = bytes;
			bytes = frames * stride;
1153
			dev_warn_ratelimited(&subs->dev->dev,
1154
				 "Corrected urb data len. %d->%d\n",
1155
1156
1157
1158
1159
1160
							oldbytes, bytes);
		}
		/* update the current pointer */
		spin_lock_irqsave(&subs->lock, flags);
		oldptr = subs->hwptr_done;
		subs->hwptr_done += bytes;
1161
1162
		if (subs->hwptr_done >= subs->buffer_bytes)
			subs->hwptr_done -= subs->buffer_bytes;
1163
1164
1165
1166
1167
1168
		frames = (bytes + (oldptr % stride)) / stride;
		subs->transfer_done += frames;
		if (subs->transfer_done >= runtime->period_size) {
			subs->transfer_done -= runtime->period_size;
			period_elapsed = 1;
		}
1169
1170
1171
1172

		/* realign last_frame_number */
		subs->last_frame_number = current_frame_number;

1173
1174
		spin_unlock_irqrestore(&subs->lock, flags);
		/* copy a data chunk */
1175
1176
1177
		if (oldptr + bytes > subs->buffer_bytes) {
			unsigned int bytes1 = subs->buffer_bytes - oldptr;

1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
			memcpy(runtime->dma_area + oldptr, cp, bytes1);
			memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
		} else {
			memcpy(runtime->dma_area + oldptr, cp, bytes);
		}
	}

	if (period_elapsed)
		snd_pcm_period_elapsed(subs->pcm_substream);
}

1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
static void urb_ctx_queue_advance(struct snd_usb_substream *subs,
				  struct urb *urb, unsigned int bytes)
{
	struct snd_urb_ctx *ctx = urb->context;

	ctx->queued += bytes;
	subs->inflight_bytes += bytes;
	subs->hwptr_done += bytes;
	if (subs->hwptr_done >= subs->buffer_bytes)
		subs->hwptr_done -= subs->buffer_bytes;
}

1201
1202
1203
1204
1205
1206
static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
					     struct urb *urb, unsigned int bytes)
{
	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
	unsigned int dst_idx = 0;
	unsigned int src_idx = subs->hwptr_done;
1207
	unsigned int wrap = subs->buffer_bytes;
1208
1209
1210
	u8 *dst = urb->transfer_buffer;
	u8 *src = runtime->dma_area;
	u8 marker[] = { 0x05, 0xfa };
1211
	unsigned int queued = 0;
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244

	/*
	 * The DSP DOP format defines a way to transport DSD samples over
	 * normal PCM data endpoints. It requires stuffing of marker bytes
	 * (0x05 and 0xfa, alternating per sample frame), and then expects
	 * 2 additional bytes of actual payload. The whole frame is stored
	 * LSB.
	 *
	 * Hence, for a stereo transport, the buffer layout looks like this,
	 * where L refers to left channel samples and R to right.
	 *
	 *   L1 L2 0x05   R1 R2 0x05   L3 L4 0xfa  R3 R4 0xfa
	 *   L5 L6 0x05   R5 R6 0x05   L7 L8 0xfa  R7 R8 0xfa
	 *   .....
	 *
	 */

	while (bytes--) {
		if (++subs->dsd_dop.byte_idx == 3) {
			/* frame boundary? */
			dst[dst_idx++] = marker[subs->dsd_dop.marker];
			src_idx += 2;
			subs->dsd_dop.byte_idx = 0;

			if (++subs->dsd_dop.channel % runtime->channels == 0) {
				/* alternate the marker */
				subs->dsd_dop.marker++;
				subs->dsd_dop.marker %= ARRAY_SIZE(marker);
				subs->dsd_dop.channel = 0;
			}
		} else {
			/* stuff the DSD payload */
			int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
1245
1246
1247