pcm.c 49.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
 */

#include <linux/init.h>
#include <linux/slab.h>
#include <linux/bitrev.h>
#include <linux/ratelimit.h>
#include <linux/usb.h>
#include <linux/usb/audio.h>
#include <linux/usb/audio-v2.h>

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

#include "usbaudio.h"
#include "card.h"
#include "quirks.h"
#include "debug.h"
#include "endpoint.h"
#include "helper.h"
#include "pcm.h"
#include "clock.h"
#include "power.h"
26
#include "media.h"
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#define SUBSTREAM_FLAG_DATA_EP_STARTED	0
#define SUBSTREAM_FLAG_SYNC_EP_STARTED	1

/* return the estimated delay based on USB frame counters */
snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
				    unsigned int rate)
{
	int current_frame_number;
	int frame_diff;
	int est_delay;

	if (!subs->last_delay)
		return 0; /* short path */

	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 */
	est_delay =  frame_diff * rate / 1000;
	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
		est_delay = subs->last_delay - est_delay;
	else
		est_delay = subs->last_delay + est_delay;

	if (est_delay < 0)
		est_delay = 0;
	return est_delay;
}

/*
 * 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)
{
68
	struct snd_usb_substream *subs = substream->runtime->private_data;
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	unsigned int hwptr_done;

	if (atomic_read(&subs->stream->chip->shutdown))
		return SNDRV_PCM_POS_XRUN;
	spin_lock(&subs->lock);
	hwptr_done = subs->hwptr_done;
	substream->runtime->delay = snd_usb_pcm_delay(subs,
						substream->runtime->rate);
	spin_unlock(&subs->lock);
	return hwptr_done / (substream->runtime->frame_bits >> 3);
}

/*
 * find a matching audio format
 */
static struct audioformat *find_format(struct snd_usb_substream *subs)
{
	struct audioformat *fp;
	struct audioformat *found = NULL;
	int cur_attr = 0, attr;

	list_for_each_entry(fp, &subs->fmt_list, list) {
		if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
			continue;
		if (fp->channels != subs->channels)
			continue;
		if (subs->cur_rate < fp->rate_min ||
		    subs->cur_rate > fp->rate_max)
			continue;
		if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
			unsigned int i;
			for (i = 0; i < fp->nr_rates; i++)
				if (fp->rate_table[i] == subs->cur_rate)
					break;
			if (i >= fp->nr_rates)
				continue;
		}
		attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
		if (! found) {
			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.
		 */
		if (attr != cur_attr) {
			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;
}

static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
			 struct usb_host_interface *alts,
			 struct audioformat *fmt)
{
	struct usb_device *dev = chip->dev;
	unsigned int ep;
	unsigned char data[1];
	int err;

	if (get_iface_desc(alts)->bNumEndpoints < 1)
		return -EINVAL;
	ep = get_endpoint(alts, 0)->bEndpointAddress;

	data[0] = 1;
155
156
157
158
159
	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));
	if (err < 0) {
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
		usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
			      iface, ep);
		return err;
	}

	return 0;
}

static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
			 struct usb_host_interface *alts,
			 struct audioformat *fmt)
{
	struct usb_device *dev = chip->dev;
	unsigned char data[1];
	int err;

	data[0] = 1;
177
178
179
180
181
	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));
	if (err < 0) {
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
		usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
			      iface, fmt->altsetting);
		return err;
	}

	return 0;
}

/*
 * initialize the pitch control and sample rate
 */
int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
		       struct usb_host_interface *alts,
		       struct audioformat *fmt)
{
	/* if endpoint doesn't have pitch control, bail out */
	if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
		return 0;

	switch (fmt->protocol) {
	case UAC_VERSION_1:
	default:
		return init_pitch_v1(chip, iface, alts, fmt);

	case UAC_VERSION_2:
		return init_pitch_v2(chip, iface, alts, fmt);
	}
}

211
static int start_endpoints(struct snd_usb_substream *subs)
212
213
214
215
216
217
218
219
220
221
222
223
{
	int err;

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

	if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
		struct snd_usb_endpoint *ep = subs->data_endpoint;

		dev_dbg(&subs->dev->dev, "Starting data EP @%p\n", ep);

		ep->data_subs = subs;
224
		err = snd_usb_endpoint_start(ep);
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
		if (err < 0) {
			clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
			return err;
		}
	}

	if (subs->sync_endpoint &&
	    !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
		struct snd_usb_endpoint *ep = subs->sync_endpoint;

		if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
		    subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
			err = usb_set_interface(subs->dev,
						subs->sync_endpoint->iface,
						subs->sync_endpoint->altsetting);
			if (err < 0) {
				clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
				dev_err(&subs->dev->dev,
					   "%d:%d: cannot set interface (%d)\n",
					   subs->sync_endpoint->iface,
					   subs->sync_endpoint->altsetting, err);
				return -EIO;
			}
		}

		dev_dbg(&subs->dev->dev, "Starting sync EP @%p\n", ep);

		ep->sync_slave = subs->data_endpoint;
253
		err = snd_usb_endpoint_start(ep);
254
255
256
257
258
259
260
261
262
		if (err < 0) {
			clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
			return err;
		}
	}

	return 0;
}

263
264
265
266
267
268
269
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);
}

static void stop_endpoints(struct snd_usb_substream *subs)
270
271
272
273
274
275
{
	if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
		snd_usb_endpoint_stop(subs->sync_endpoint);

	if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
		snd_usb_endpoint_stop(subs->data_endpoint);
276
}
277

278
279
280
281
282
283
284
285
/* 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;

	if (!snd_usb_lock_shutdown(subs->stream->chip)) {
		sync_pending_stops(subs);
		snd_usb_unlock_shutdown(subs->stream->chip);
286
	}
287
	return 0;
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
}

static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
				     unsigned int altsetting,
				     struct usb_host_interface **alts,
				     unsigned int *ep)
{
	struct usb_interface *iface;
	struct usb_interface_descriptor *altsd;
	struct usb_endpoint_descriptor *epd;

	iface = usb_ifnum_to_if(dev, ifnum);
	if (!iface || iface->num_altsetting < altsetting + 1)
		return -ENOENT;
	*alts = &iface->altsetting[altsetting];
	altsd = get_iface_desc(*alts);
	if (altsd->bAlternateSetting != altsetting ||
	    altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
	    (altsd->bInterfaceSubClass != 2 &&
	     altsd->bInterfaceProtocol != 2   ) ||
	    altsd->bNumEndpoints < 1)
		return -ENOENT;
	epd = get_endpoint(*alts, 0);
	if (!usb_endpoint_is_isoc_in(epd) ||
	    (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
					USB_ENDPOINT_USAGE_IMPLICIT_FB)
		return -ENOENT;
	*ep = epd->bEndpointAddress;
	return 0;
}

319
320
321
/* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
 * applies. Returns 1 if a quirk was found.
 */
322
323
324
325
326
327
328
329
static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
					 struct usb_device *dev,
					 struct usb_interface_descriptor *altsd,
					 unsigned int attr)
{
	struct usb_host_interface *alts;
	struct usb_interface *iface;
	unsigned int ep;
330
	unsigned int ifnum;
331
332
333
334
335
336
337
338
339

	/* Implicit feedback sync EPs consumers are always playback EPs */
	if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
		return 0;

	switch (subs->stream->chip->usb_id) {
	case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
	case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
		ep = 0x81;
340
341
		ifnum = 3;
		goto add_sync_ep_from_ifnum;
342
343
344
	case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
	case USB_ID(0x0763, 0x2081):
		ep = 0x81;
345
346
347
		ifnum = 2;
		goto add_sync_ep_from_ifnum;
	case USB_ID(0x2466, 0x8003): /* Fractal Audio Axe-Fx II */
348
		ep = 0x86;
349
350
351
		ifnum = 2;
		goto add_sync_ep_from_ifnum;
	case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx III */
352
		ep = 0x81;
353
354
		ifnum = 2;
		goto add_sync_ep_from_ifnum;
355
	case USB_ID(0x1397, 0x0001): /* Behringer UFX1604 */
356
357
358
359
	case USB_ID(0x1397, 0x0002): /* Behringer UFX1204 */
		ep = 0x81;
		ifnum = 1;
		goto add_sync_ep_from_ifnum;
360
361
362
363
364
365
	case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook II/IIc */
		/* MicroBook IIc */
		if (altsd->bInterfaceClass == USB_CLASS_AUDIO)
			return 0;

		/* MicroBook II */
366
367
368
		ep = 0x84;
		ifnum = 0;
		goto add_sync_ep_from_ifnum;
369
	case USB_ID(0x07fd, 0x0008): /* MOTU M Series */
370
371
	case USB_ID(0x31e9, 0x0002): /* Solid State Logic SSL2+ */
	case USB_ID(0x0d9a, 0x00df): /* RTX6001 */
372
373
374
		ep = 0x81;
		ifnum = 2;
		goto add_sync_ep_from_ifnum;
375
376
377
	case USB_ID(0x0582, 0x01d8): /* BOSS Katana */
		/* BOSS Katana amplifiers do not need quirks */
		return 0;
378
	}
379

380
381
382
383
384
385
386
387
388
389
390
391
392
393
	if (attr == USB_ENDPOINT_SYNC_ASYNC &&
	    altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
	    altsd->bInterfaceProtocol == 2 &&
	    altsd->bNumEndpoints == 1 &&
	    USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
	    search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
				      altsd->bAlternateSetting,
				      &alts, &ep) >= 0) {
		goto add_sync_ep;
	}

	/* No quirk */
	return 0;

394
395
396
add_sync_ep_from_ifnum:
	iface = usb_ifnum_to_if(dev, ifnum);

397
	if (!iface || iface->num_altsetting < 2)
398
399
400
401
		return -EINVAL;

	alts = &iface->altsetting[1];

402
403
404
405
406
407
408
add_sync_ep:
	subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
						   alts, ep, !subs->direction,
						   SND_USB_ENDPOINT_TYPE_DATA);
	if (!subs->sync_endpoint)
		return -EINVAL;

409
410
	subs->sync_endpoint->is_implicit_feedback = 1;

411
412
	subs->data_endpoint->sync_master = subs->sync_endpoint;

413
	return 1;
414
415
416
417
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
}

static int set_sync_endpoint(struct snd_usb_substream *subs,
			     struct audioformat *fmt,
			     struct usb_device *dev,
			     struct usb_host_interface *alts,
			     struct usb_interface_descriptor *altsd)
{
	int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
	unsigned int ep, attr;
	bool implicit_fb;
	int err;

	/* we need a sync pipe in async OUT or adaptive IN mode */
	/* check the number of EP, since some devices have broken
	 * descriptors which fool us.  if it has only one EP,
	 * assume it as adaptive-out or sync-in.
	 */
	attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;

	if ((is_playback && (attr != USB_ENDPOINT_SYNC_ASYNC)) ||
		(!is_playback && (attr != USB_ENDPOINT_SYNC_ADAPTIVE))) {

		/*
		 * In these modes the notion of sync_endpoint is irrelevant.
		 * Reset pointers to avoid using stale data from previously
		 * used settings, e.g. when configuration and endpoints were
		 * changed
		 */

		subs->sync_endpoint = NULL;
		subs->data_endpoint->sync_master = NULL;
	}

	err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
	if (err < 0)
		return err;

452
453
454
455
	/* endpoint set by quirk */
	if (err > 0)
		return 0;

456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
	if (altsd->bNumEndpoints < 2)
		return 0;

	if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
			     attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
	    (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
		return 0;

	/*
	 * 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
	 */

	/* 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 */
	if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
	    (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
	     get_endpoint(alts, 1)->bSynchAddress != 0)) {
		dev_err(&dev->dev,
			"%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
			   fmt->iface, fmt->altsetting,
			   get_endpoint(alts, 1)->bmAttributes,
			   get_endpoint(alts, 1)->bLength,
			   get_endpoint(alts, 1)->bSynchAddress);
		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
			return 0;
		return -EINVAL;
	}
	ep = get_endpoint(alts, 1)->bEndpointAddress;
	if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
489
	    get_endpoint(alts, 0)->bSynchAddress != 0 &&
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
	    ((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)))) {
		dev_err(&dev->dev,
			"%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
			   fmt->iface, fmt->altsetting,
			   is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
			return 0;
		return -EINVAL;
	}

	implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
			== USB_ENDPOINT_USAGE_IMPLICIT_FB;

	subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
						   alts, ep, !subs->direction,
						   implicit_fb ?
							SND_USB_ENDPOINT_TYPE_DATA :
							SND_USB_ENDPOINT_TYPE_SYNC);
509

510
511
512
513
514
515
	if (!subs->sync_endpoint) {
		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
			return 0;
		return -EINVAL;
	}

516
517
	subs->sync_endpoint->is_implicit_feedback = implicit_fb;

518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
	subs->data_endpoint->sync_master = subs->sync_endpoint;

	return 0;
}

/*
 * find a matching format and set up the interface
 */
static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
{
	struct usb_device *dev = subs->dev;
	struct usb_host_interface *alts;
	struct usb_interface_descriptor *altsd;
	struct usb_interface *iface;
	int err;

	iface = usb_ifnum_to_if(dev, fmt->iface);
	if (WARN_ON(!iface))
		return -EINVAL;
537
	alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
538
	if (WARN_ON(!alts))
539
		return -EINVAL;
540
	altsd = get_iface_desc(alts);
541

542
	if (fmt == subs->cur_audiofmt && !subs->need_setup_fmt)
543
544
545
		return 0;

	/* close the old interface */
546
	if (subs->interface >= 0 && (subs->interface != fmt->iface || subs->need_setup_fmt)) {
547
548
549
550
551
552
553
554
		if (!subs->stream->chip->keep_iface) {
			err = usb_set_interface(subs->dev, subs->interface, 0);
			if (err < 0) {
				dev_err(&dev->dev,
					"%d:%d: return to setting 0 failed (%d)\n",
					fmt->iface, fmt->altsetting, err);
				return -EIO;
			}
555
556
557
558
559
		}
		subs->interface = -1;
		subs->altset_idx = 0;
	}

560
561
562
	if (subs->need_setup_fmt)
		subs->need_setup_fmt = false;

563
	/* set interface */
564
	if (iface->cur_altsetting != alts) {
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
		err = snd_usb_select_mode_quirk(subs, fmt);
		if (err < 0)
			return -EIO;

		err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
		if (err < 0) {
			dev_err(&dev->dev,
				"%d:%d: usb_set_interface failed (%d)\n",
				fmt->iface, fmt->altsetting, err);
			return -EIO;
		}
		dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
			fmt->iface, fmt->altsetting);
		snd_usb_set_interface_quirk(dev);
	}

581
582
	subs->interface = fmt->iface;
	subs->altset_idx = fmt->altset_idx;
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
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
	subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
						   alts, fmt->endpoint, subs->direction,
						   SND_USB_ENDPOINT_TYPE_DATA);

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

	err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
	if (err < 0)
		return err;

	err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
	if (err < 0)
		return err;

	subs->cur_audiofmt = fmt;

	snd_usb_set_format_quirk(subs, fmt);

	return 0;
}

/*
 * Return the score of matching two audioformats.
 * Veto the audioformat if:
 * - It has no channels for some reason.
 * - Requested PCM format is not supported.
 * - Requested sample rate is not supported.
 */
static int match_endpoint_audioformats(struct snd_usb_substream *subs,
				       struct audioformat *fp,
				       struct audioformat *match, int rate,
				       snd_pcm_format_t pcm_format)
{
	int i;
	int score = 0;

	if (fp->channels < 1) {
		dev_dbg(&subs->dev->dev,
			"%s: (fmt @%p) no channels\n", __func__, fp);
		return 0;
	}

	if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
		dev_dbg(&subs->dev->dev,
			"%s: (fmt @%p) no match for format %d\n", __func__,
			fp, pcm_format);
		return 0;
	}

	for (i = 0; i < fp->nr_rates; i++) {
		if (fp->rate_table[i] == rate) {
			score++;
			break;
		}
	}
	if (!score) {
		dev_dbg(&subs->dev->dev,
			"%s: (fmt @%p) no match for rate %d\n", __func__,
			fp, rate);
		return 0;
	}

	if (fp->channels == match->channels)
		score++;

	dev_dbg(&subs->dev->dev,
		"%s: (fmt @%p) score %d\n", __func__, fp, score);

	return score;
}

/*
 * Configure the sync ep using the rate and pcm format of the data ep.
 */
static int configure_sync_endpoint(struct snd_usb_substream *subs)
{
	int ret;
	struct audioformat *fp;
	struct audioformat *sync_fp = NULL;
	int cur_score = 0;
	int sync_period_bytes = subs->period_bytes;
	struct snd_usb_substream *sync_subs =
		&subs->stream->substream[subs->direction ^ 1];

	if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
	    !subs->stream)
		return snd_usb_endpoint_set_params(subs->sync_endpoint,
						   subs->pcm_format,
						   subs->channels,
						   subs->period_bytes,
						   0, 0,
						   subs->cur_rate,
						   subs->cur_audiofmt,
						   NULL);

	/* Try to find the best matching audioformat. */
	list_for_each_entry(fp, &sync_subs->fmt_list, list) {
		int score = match_endpoint_audioformats(subs,
							fp, subs->cur_audiofmt,
			subs->cur_rate, subs->pcm_format);

		if (score > cur_score) {
			sync_fp = fp;
			cur_score = score;
		}
	}

	if (unlikely(sync_fp == NULL)) {
		dev_err(&subs->dev->dev,
			"%s: no valid audioformat for sync ep %x found\n",
			__func__, sync_subs->ep_num);
		return -EINVAL;
	}

	/*
	 * Recalculate the period bytes if channel number differ between
	 * data and sync ep audioformat.
	 */
	if (sync_fp->channels != subs->channels) {
		sync_period_bytes = (subs->period_bytes / subs->channels) *
			sync_fp->channels;
		dev_dbg(&subs->dev->dev,
			"%s: adjusted sync ep period bytes (%d -> %d)\n",
			__func__, subs->period_bytes, sync_period_bytes);
	}

	ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
					  subs->pcm_format,
					  sync_fp->channels,
					  sync_period_bytes,
					  0, 0,
					  subs->cur_rate,
					  sync_fp,
					  NULL);

	return ret;
}

/*
 * configure endpoint params
 *
 * called  during initial setup and upon resume
 */
static int configure_endpoint(struct snd_usb_substream *subs)
{
	int ret;

	/* format changed */
732
733
	stop_endpoints(subs);
	sync_pending_stops(subs);
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
	ret = snd_usb_endpoint_set_params(subs->data_endpoint,
					  subs->pcm_format,
					  subs->channels,
					  subs->period_bytes,
					  subs->period_frames,
					  subs->buffer_periods,
					  subs->cur_rate,
					  subs->cur_audiofmt,
					  subs->sync_endpoint);
	if (ret < 0)
		return ret;

	if (subs->sync_endpoint)
		ret = configure_sync_endpoint(subs);

	return ret;
}

752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
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;

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

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

	return 0;
}

800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/*
 * 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;
	struct audioformat *fmt;
	int ret;

817
818
819
820
	ret = snd_media_start_pipeline(subs);
	if (ret)
		return ret;

821
822
823
824
825
826
827
828
829
830
831
832
	subs->pcm_format = params_format(hw_params);
	subs->period_bytes = params_period_bytes(hw_params);
	subs->period_frames = params_period_size(hw_params);
	subs->buffer_periods = params_periods(hw_params);
	subs->channels = params_channels(hw_params);
	subs->cur_rate = params_rate(hw_params);

	fmt = find_format(subs);
	if (!fmt) {
		dev_dbg(&subs->dev->dev,
			"cannot set format: format = %#x, rate = %d, channels = %d\n",
			   subs->pcm_format, subs->cur_rate, subs->channels);
833
834
		ret = -EINVAL;
		goto stop_pipeline;
835
836
837
838
	}

	ret = snd_usb_lock_shutdown(subs->stream->chip);
	if (ret < 0)
839
		goto stop_pipeline;
840
841
842
843
844

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

845
846
	ret = set_format(subs, fmt);
	if (ret < 0)
847
		goto unlock;
848
849
850
851
852

	subs->interface = fmt->iface;
	subs->altset_idx = fmt->altset_idx;
	subs->need_setup_ep = true;

853
854
 unlock:
	snd_usb_unlock_shutdown(subs->stream->chip);
855
856
857
858
859
860
	if (ret < 0)
		goto stop_pipeline;
	return ret;

 stop_pipeline:
	snd_media_stop_pipeline(subs);
861
	return ret;
862
863
864
865
866
867
868
869
870
871
872
}

/*
 * 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;

873
	snd_media_stop_pipeline(subs);
874
875
876
877
	subs->cur_audiofmt = NULL;
	subs->cur_rate = 0;
	subs->period_bytes = 0;
	if (!snd_usb_lock_shutdown(subs->stream->chip)) {
878
879
		stop_endpoints(subs);
		sync_pending_stops(subs);
880
881
882
883
		snd_usb_endpoint_deactivate(subs->sync_endpoint);
		snd_usb_endpoint_deactivate(subs->data_endpoint);
		snd_usb_unlock_shutdown(subs->stream->chip);
	}
884

885
	return 0;
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
}

/*
 * 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;
	struct usb_host_interface *alts;
	struct usb_interface *iface;
	int ret;

	if (! subs->cur_audiofmt) {
		dev_err(&subs->dev->dev, "no format is specified!\n");
		return -ENXIO;
	}

	ret = snd_usb_lock_shutdown(subs->stream->chip);
	if (ret < 0)
		return ret;
	if (snd_BUG_ON(!subs->data_endpoint)) {
		ret = -EIO;
		goto unlock;
	}

914
915
916
917
	ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
	if (ret < 0)
		goto unlock;

918
919
920
921
922
	ret = set_format(subs, subs->cur_audiofmt);
	if (ret < 0)
		goto unlock;

	if (subs->need_setup_ep) {
923
924
925
926
927
928
929
930
931
932
933

		iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
		alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
		ret = snd_usb_init_sample_rate(subs->stream->chip,
					       subs->cur_audiofmt->iface,
					       alts,
					       subs->cur_audiofmt,
					       subs->cur_rate);
		if (ret < 0)
			goto unlock;

934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
		ret = configure_endpoint(subs);
		if (ret < 0)
			goto unlock;
		subs->need_setup_ep = false;
	}

	/* some unit conversions in runtime */
	subs->data_endpoint->maxframesize =
		bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
	subs->data_endpoint->curframesize =
		bytes_to_frames(runtime, subs->data_endpoint->curpacksize);

	/* reset the pointer */
	subs->hwptr_done = 0;
	subs->transfer_done = 0;
	subs->last_delay = 0;
	subs->last_frame_number = 0;
	runtime->delay = 0;

	/* for playback, submit the URBs now; otherwise, the first hwptr_done
	 * updates for all URBs would happen at the same time when starting */
	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
956
		ret = start_endpoints(subs);
957
958
959
960
961
962

 unlock:
	snd_usb_unlock_shutdown(subs->stream->chip);
	return ret;
}

963
static const struct snd_pcm_hardware snd_usb_hardware =
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
	.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,
	.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,
				 struct audioformat *fp)
{
	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);
	struct snd_mask check_fmts;
	unsigned int ptime;

	/* check the format */
	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)) {
		hwc_debug("   > check: no supported format %d\n", fp->format);
		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);
For faster browsing, not all history is shown. View entire blame