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 <trace/hooks/sound.h>
18
19 #include "usbaudio.h"
20 #include "card.h"
21 #include "quirks.h"
22 #include "debug.h"
23 #include "endpoint.h"
24 #include "helper.h"
25 #include "pcm.h"
26 #include "clock.h"
27 #include "power.h"
28 #include "media.h"
29
30 #define SUBSTREAM_FLAG_DATA_EP_STARTED 0
31 #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
32
33 /* return the estimated delay based on USB frame counters */
snd_usb_pcm_delay(struct snd_usb_substream * subs,unsigned int rate)34 snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
35 unsigned int rate)
36 {
37 int current_frame_number;
38 int frame_diff;
39 int est_delay;
40
41 if (!subs->last_delay)
42 return 0; /* short path */
43
44 current_frame_number = usb_get_current_frame_number(subs->dev);
45 /*
46 * HCD implementations use different widths, use lower 8 bits.
47 * The delay will be managed up to 256ms, which is more than
48 * enough
49 */
50 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
51
52 /* Approximation based on number of samples per USB frame (ms),
53 some truncation for 44.1 but the estimate is good enough */
54 est_delay = frame_diff * rate / 1000;
55 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
56 est_delay = subs->last_delay - est_delay;
57 else
58 est_delay = subs->last_delay + est_delay;
59
60 if (est_delay < 0)
61 est_delay = 0;
62 return est_delay;
63 }
64
65 /*
66 * return the current pcm pointer. just based on the hwptr_done value.
67 */
snd_usb_pcm_pointer(struct snd_pcm_substream * substream)68 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
69 {
70 struct snd_usb_substream *subs = substream->runtime->private_data;
71 unsigned int hwptr_done;
72
73 if (atomic_read(&subs->stream->chip->shutdown))
74 return SNDRV_PCM_POS_XRUN;
75 spin_lock(&subs->lock);
76 hwptr_done = subs->hwptr_done;
77 substream->runtime->delay = snd_usb_pcm_delay(subs,
78 substream->runtime->rate);
79 spin_unlock(&subs->lock);
80 return hwptr_done / (substream->runtime->frame_bits >> 3);
81 }
82
83 /*
84 * find a matching audio format
85 */
find_format(struct snd_usb_substream * subs)86 static struct audioformat *find_format(struct snd_usb_substream *subs)
87 {
88 struct audioformat *fp;
89 struct audioformat *found = NULL;
90 int cur_attr = 0, attr;
91
92 list_for_each_entry(fp, &subs->fmt_list, list) {
93 if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
94 continue;
95 if (fp->channels != subs->channels)
96 continue;
97 if (subs->cur_rate < fp->rate_min ||
98 subs->cur_rate > fp->rate_max)
99 continue;
100 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
101 unsigned int i;
102 for (i = 0; i < fp->nr_rates; i++)
103 if (fp->rate_table[i] == subs->cur_rate)
104 break;
105 if (i >= fp->nr_rates)
106 continue;
107 }
108 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
109 if (! found) {
110 found = fp;
111 cur_attr = attr;
112 continue;
113 }
114 /* avoid async out and adaptive in if the other method
115 * supports the same format.
116 * this is a workaround for the case like
117 * M-audio audiophile USB.
118 */
119 if (attr != cur_attr) {
120 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
121 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
122 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
123 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
124 continue;
125 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
126 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
127 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
128 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
129 found = fp;
130 cur_attr = attr;
131 continue;
132 }
133 }
134 /* find the format with the largest max. packet size */
135 if (fp->maxpacksize > found->maxpacksize) {
136 found = fp;
137 cur_attr = attr;
138 }
139
140 snd_vendor_set_pcm_binterval(fp, found, &cur_attr, &attr);
141 }
142 return found;
143 }
144
145 /*
146 * find a matching audio format as well as non-zero service interval
147 */
find_format_and_si(struct snd_usb_substream * subs,unsigned int datainterval)148 static struct audioformat *find_format_and_si(struct snd_usb_substream *subs,
149 unsigned int datainterval)
150 {
151 unsigned int i;
152 struct audioformat *fp;
153 struct audioformat *found = NULL;
154 int cur_attr = 0, attr;
155
156 list_for_each_entry(fp, &subs->fmt_list, list) {
157 if (datainterval != fp->datainterval)
158 continue;
159 if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
160 continue;
161 if (fp->channels != subs->channels)
162 continue;
163 if (subs->cur_rate < fp->rate_min ||
164 subs->cur_rate > fp->rate_max)
165 continue;
166 if (!(fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
167 for (i = 0; i < fp->nr_rates; i++)
168 if (fp->rate_table[i] == subs->cur_rate)
169 break;
170 if (i >= fp->nr_rates)
171 continue;
172 }
173 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
174 if (!found) {
175 found = fp;
176 cur_attr = attr;
177 continue;
178 }
179 /* avoid async out and adaptive in if the other method
180 * supports the same format.
181 * this is a workaround for the case like
182 * M-audio audiophile USB.
183 */
184 if (attr != cur_attr) {
185 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
186 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
187 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
188 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
189 continue;
190 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
191 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
192 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
193 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
194 found = fp;
195 cur_attr = attr;
196 continue;
197 }
198 }
199 /* find the format with the largest max. packet size */
200 if (fp->maxpacksize > found->maxpacksize) {
201 found = fp;
202 cur_attr = attr;
203 }
204 }
205 return found;
206 }
207
init_pitch_v1(struct snd_usb_audio * chip,int iface,struct usb_host_interface * alts,struct audioformat * fmt)208 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
209 struct usb_host_interface *alts,
210 struct audioformat *fmt)
211 {
212 struct usb_device *dev = chip->dev;
213 unsigned int ep;
214 unsigned char data[1];
215 int err;
216
217 if (get_iface_desc(alts)->bNumEndpoints < 1)
218 return -EINVAL;
219 ep = get_endpoint(alts, 0)->bEndpointAddress;
220
221 data[0] = 1;
222 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
223 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
224 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
225 data, sizeof(data));
226 if (err < 0) {
227 usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
228 iface, ep);
229 return err;
230 }
231
232 return 0;
233 }
234
init_pitch_v2(struct snd_usb_audio * chip,int iface,struct usb_host_interface * alts,struct audioformat * fmt)235 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
236 struct usb_host_interface *alts,
237 struct audioformat *fmt)
238 {
239 struct usb_device *dev = chip->dev;
240 unsigned char data[1];
241 int err;
242
243 data[0] = 1;
244 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
245 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
246 UAC2_EP_CS_PITCH << 8, 0,
247 data, sizeof(data));
248 if (err < 0) {
249 usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
250 iface, fmt->altsetting);
251 return err;
252 }
253
254 return 0;
255 }
256
257 /*
258 * initialize the pitch control and sample rate
259 */
snd_usb_init_pitch(struct snd_usb_audio * chip,int iface,struct usb_host_interface * alts,struct audioformat * fmt)260 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
261 struct usb_host_interface *alts,
262 struct audioformat *fmt)
263 {
264 /* if endpoint doesn't have pitch control, bail out */
265 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
266 return 0;
267
268 switch (fmt->protocol) {
269 case UAC_VERSION_1:
270 default:
271 return init_pitch_v1(chip, iface, alts, fmt);
272
273 case UAC_VERSION_2:
274 return init_pitch_v2(chip, iface, alts, fmt);
275 }
276 }
277
start_endpoints(struct snd_usb_substream * subs)278 static int start_endpoints(struct snd_usb_substream *subs)
279 {
280 int err;
281
282 if (!subs->data_endpoint)
283 return -EINVAL;
284
285 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
286 struct snd_usb_endpoint *ep = subs->data_endpoint;
287
288 dev_dbg(&subs->dev->dev, "Starting data EP @%p\n", ep);
289
290 ep->data_subs = subs;
291 err = snd_usb_endpoint_start(ep);
292 if (err < 0) {
293 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
294 return err;
295 }
296 }
297
298 if (subs->sync_endpoint &&
299 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
300 struct snd_usb_endpoint *ep = subs->sync_endpoint;
301
302 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
303 subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
304 err = usb_set_interface(subs->dev,
305 subs->sync_endpoint->iface,
306 subs->sync_endpoint->altsetting);
307 if (err < 0) {
308 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
309 dev_err(&subs->dev->dev,
310 "%d:%d: cannot set interface (%d)\n",
311 subs->sync_endpoint->iface,
312 subs->sync_endpoint->altsetting, err);
313 return -EIO;
314 }
315 }
316
317 dev_dbg(&subs->dev->dev, "Starting sync EP @%p\n", ep);
318
319 ep->sync_slave = subs->data_endpoint;
320 err = snd_usb_endpoint_start(ep);
321 if (err < 0) {
322 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
323 return err;
324 }
325 }
326
327 return 0;
328 }
329
sync_pending_stops(struct snd_usb_substream * subs)330 static void sync_pending_stops(struct snd_usb_substream *subs)
331 {
332 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
333 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
334 }
335
stop_endpoints(struct snd_usb_substream * subs)336 static void stop_endpoints(struct snd_usb_substream *subs)
337 {
338 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
339 snd_usb_endpoint_stop(subs->sync_endpoint);
340
341 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
342 snd_usb_endpoint_stop(subs->data_endpoint);
343 }
344
345 /* PCM sync_stop callback */
snd_usb_pcm_sync_stop(struct snd_pcm_substream * substream)346 static int snd_usb_pcm_sync_stop(struct snd_pcm_substream *substream)
347 {
348 struct snd_usb_substream *subs = substream->runtime->private_data;
349
350 sync_pending_stops(subs);
351 return 0;
352 }
353
search_roland_implicit_fb(struct usb_device * dev,int ifnum,unsigned int altsetting,struct usb_host_interface ** alts,unsigned int * ep)354 static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
355 unsigned int altsetting,
356 struct usb_host_interface **alts,
357 unsigned int *ep)
358 {
359 struct usb_interface *iface;
360 struct usb_interface_descriptor *altsd;
361 struct usb_endpoint_descriptor *epd;
362
363 iface = usb_ifnum_to_if(dev, ifnum);
364 if (!iface || iface->num_altsetting < altsetting + 1)
365 return -ENOENT;
366 *alts = &iface->altsetting[altsetting];
367 altsd = get_iface_desc(*alts);
368 if (altsd->bAlternateSetting != altsetting ||
369 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
370 (altsd->bInterfaceSubClass != 2 &&
371 altsd->bInterfaceProtocol != 2 ) ||
372 altsd->bNumEndpoints < 1)
373 return -ENOENT;
374 epd = get_endpoint(*alts, 0);
375 if (!usb_endpoint_is_isoc_in(epd) ||
376 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
377 USB_ENDPOINT_USAGE_IMPLICIT_FB)
378 return -ENOENT;
379 *ep = epd->bEndpointAddress;
380 return 0;
381 }
382
383 /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
384 * applies. Returns 1 if a quirk was found.
385 */
set_sync_ep_implicit_fb_quirk(struct snd_usb_substream * subs,struct usb_device * dev,struct usb_interface_descriptor * altsd,unsigned int attr)386 static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
387 struct usb_device *dev,
388 struct usb_interface_descriptor *altsd,
389 unsigned int attr)
390 {
391 struct usb_host_interface *alts;
392 struct usb_interface *iface;
393 unsigned int ep;
394 unsigned int ifnum;
395
396 /* Implicit feedback sync EPs consumers are always playback EPs */
397 if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
398 return 0;
399
400 switch (subs->stream->chip->usb_id) {
401 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
402 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
403 case USB_ID(0x22f0, 0x0006): /* Allen&Heath Qu-16 */
404 ep = 0x81;
405 ifnum = 3;
406 goto add_sync_ep_from_ifnum;
407 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
408 case USB_ID(0x0763, 0x2081):
409 ep = 0x81;
410 ifnum = 2;
411 goto add_sync_ep_from_ifnum;
412 case USB_ID(0x2466, 0x8003): /* Fractal Audio Axe-Fx II */
413 case USB_ID(0x0499, 0x172a): /* Yamaha MODX */
414 ep = 0x86;
415 ifnum = 2;
416 goto add_sync_ep_from_ifnum;
417 case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx III */
418 ep = 0x81;
419 ifnum = 2;
420 goto add_sync_ep_from_ifnum;
421 case USB_ID(0x1686, 0xf029): /* Zoom UAC-2 */
422 ep = 0x82;
423 ifnum = 2;
424 goto add_sync_ep_from_ifnum;
425 case USB_ID(0x1397, 0x0001): /* Behringer UFX1604 */
426 case USB_ID(0x1397, 0x0002): /* Behringer UFX1204 */
427 ep = 0x81;
428 ifnum = 1;
429 goto add_sync_ep_from_ifnum;
430 case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook II/IIc */
431 /* MicroBook IIc */
432 if (altsd->bInterfaceClass == USB_CLASS_AUDIO)
433 return 0;
434
435 /* MicroBook II */
436 ep = 0x84;
437 ifnum = 0;
438 goto add_sync_ep_from_ifnum;
439 case USB_ID(0x07fd, 0x0008): /* MOTU M Series */
440 case USB_ID(0x31e9, 0x0001): /* Solid State Logic SSL2 */
441 case USB_ID(0x31e9, 0x0002): /* Solid State Logic SSL2+ */
442 case USB_ID(0x0499, 0x172f): /* Steinberg UR22C */
443 case USB_ID(0x0d9a, 0x00df): /* RTX6001 */
444 ep = 0x81;
445 ifnum = 2;
446 goto add_sync_ep_from_ifnum;
447 case USB_ID(0x2b73, 0x000a): /* Pioneer DJ DJM-900NXS2 */
448 case USB_ID(0x2b73, 0x0017): /* Pioneer DJ DJM-250MK2 */
449 ep = 0x82;
450 ifnum = 0;
451 goto add_sync_ep_from_ifnum;
452 case USB_ID(0x0582, 0x01d8): /* BOSS Katana */
453 /* BOSS Katana amplifiers do not need quirks */
454 return 0;
455 }
456
457 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
458 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
459 altsd->bInterfaceProtocol == 2 &&
460 altsd->bNumEndpoints == 1 &&
461 USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
462 search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
463 altsd->bAlternateSetting,
464 &alts, &ep) >= 0) {
465 goto add_sync_ep;
466 }
467
468 /* No quirk */
469 return 0;
470
471 add_sync_ep_from_ifnum:
472 iface = usb_ifnum_to_if(dev, ifnum);
473
474 if (!iface || iface->num_altsetting < 2)
475 return -EINVAL;
476
477 alts = &iface->altsetting[1];
478
479 add_sync_ep:
480 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
481 alts, ep, !subs->direction,
482 SND_USB_ENDPOINT_TYPE_DATA);
483 if (!subs->sync_endpoint)
484 return -EINVAL;
485
486 subs->sync_endpoint->is_implicit_feedback = 1;
487
488 subs->data_endpoint->sync_master = subs->sync_endpoint;
489
490 return 1;
491 }
492
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)493 static int set_sync_endpoint(struct snd_usb_substream *subs,
494 struct audioformat *fmt,
495 struct usb_device *dev,
496 struct usb_host_interface *alts,
497 struct usb_interface_descriptor *altsd)
498 {
499 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
500 unsigned int ep, attr;
501 bool implicit_fb;
502 int err;
503
504 /* we need a sync pipe in async OUT or adaptive IN mode */
505 /* check the number of EP, since some devices have broken
506 * descriptors which fool us. if it has only one EP,
507 * assume it as adaptive-out or sync-in.
508 */
509 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
510
511 if ((is_playback && (attr != USB_ENDPOINT_SYNC_ASYNC)) ||
512 (!is_playback && (attr != USB_ENDPOINT_SYNC_ADAPTIVE))) {
513
514 /*
515 * In these modes the notion of sync_endpoint is irrelevant.
516 * Reset pointers to avoid using stale data from previously
517 * used settings, e.g. when configuration and endpoints were
518 * changed
519 */
520
521 subs->sync_endpoint = NULL;
522 subs->data_endpoint->sync_master = NULL;
523 }
524
525 err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
526 if (err < 0)
527 return err;
528
529 /* endpoint set by quirk */
530 if (err > 0)
531 return 0;
532
533 if (altsd->bNumEndpoints < 2)
534 return 0;
535
536 if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
537 attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
538 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
539 return 0;
540
541 /*
542 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
543 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
544 * error fall back to SYNC mode and don't create sync endpoint
545 */
546
547 /* check sync-pipe endpoint */
548 /* ... and check descriptor size before accessing bSynchAddress
549 because there is a version of the SB Audigy 2 NX firmware lacking
550 the audio fields in the endpoint descriptors */
551 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
552 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
553 get_endpoint(alts, 1)->bSynchAddress != 0)) {
554 dev_err(&dev->dev,
555 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
556 fmt->iface, fmt->altsetting,
557 get_endpoint(alts, 1)->bmAttributes,
558 get_endpoint(alts, 1)->bLength,
559 get_endpoint(alts, 1)->bSynchAddress);
560 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
561 return 0;
562 return -EINVAL;
563 }
564 ep = get_endpoint(alts, 1)->bEndpointAddress;
565 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
566 get_endpoint(alts, 0)->bSynchAddress != 0 &&
567 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
568 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
569 dev_err(&dev->dev,
570 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
571 fmt->iface, fmt->altsetting,
572 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
573 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
574 return 0;
575 return -EINVAL;
576 }
577
578 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
579 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
580
581 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
582 alts, ep, !subs->direction,
583 implicit_fb ?
584 SND_USB_ENDPOINT_TYPE_DATA :
585 SND_USB_ENDPOINT_TYPE_SYNC);
586
587 if (!subs->sync_endpoint) {
588 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
589 return 0;
590 return -EINVAL;
591 }
592
593 subs->sync_endpoint->is_implicit_feedback = implicit_fb;
594
595 subs->data_endpoint->sync_master = subs->sync_endpoint;
596
597 return 0;
598 }
599
600 /*
601 * find a matching format and set up the interface
602 */
set_format(struct snd_usb_substream * subs,struct audioformat * fmt)603 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
604 {
605 struct usb_device *dev = subs->dev;
606 struct usb_host_interface *alts;
607 struct usb_interface_descriptor *altsd;
608 struct usb_interface *iface;
609 int err;
610
611 iface = usb_ifnum_to_if(dev, fmt->iface);
612 if (WARN_ON(!iface))
613 return -EINVAL;
614 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
615 if (WARN_ON(!alts))
616 return -EINVAL;
617 altsd = get_iface_desc(alts);
618
619 if (fmt == subs->cur_audiofmt && !subs->need_setup_fmt)
620 return 0;
621
622 /* close the old interface */
623 if (subs->interface >= 0 && (subs->interface != fmt->iface || subs->need_setup_fmt)) {
624 if (!subs->stream->chip->keep_iface) {
625 err = usb_set_interface(subs->dev, subs->interface, 0);
626 if (err < 0) {
627 dev_err(&dev->dev,
628 "%d:%d: return to setting 0 failed (%d)\n",
629 fmt->iface, fmt->altsetting, err);
630 return -EIO;
631 }
632 }
633 subs->interface = -1;
634 subs->altset_idx = 0;
635 }
636
637 if (subs->need_setup_fmt)
638 subs->need_setup_fmt = false;
639
640 /* set interface */
641 if (iface->cur_altsetting != alts) {
642 err = snd_usb_select_mode_quirk(subs, fmt);
643 if (err < 0)
644 return -EIO;
645
646 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
647 if (err < 0) {
648 dev_err(&dev->dev,
649 "%d:%d: usb_set_interface failed (%d)\n",
650 fmt->iface, fmt->altsetting, err);
651 return -EIO;
652 }
653 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
654 fmt->iface, fmt->altsetting);
655 err = snd_vendor_set_pcm_intf(iface, fmt->iface,
656 fmt->altsetting, subs->direction);
657 if (err)
658 return err;
659 snd_usb_set_interface_quirk(dev);
660 }
661
662 subs->interface = fmt->iface;
663 subs->altset_idx = fmt->altset_idx;
664 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
665 alts, fmt->endpoint, subs->direction,
666 SND_USB_ENDPOINT_TYPE_DATA);
667
668 if (!subs->data_endpoint)
669 return -EINVAL;
670
671 err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
672 if (err < 0)
673 return err;
674
675 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
676 if (err < 0)
677 return err;
678
679 subs->cur_audiofmt = fmt;
680
681 snd_usb_set_format_quirk(subs, fmt);
682
683 return 0;
684 }
685
686 static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state);
687
snd_usb_enable_audio_stream(struct snd_usb_substream * subs,int datainterval,bool enable)688 int snd_usb_enable_audio_stream(struct snd_usb_substream *subs,
689 int datainterval, bool enable)
690 {
691 struct audioformat *fmt;
692 struct usb_host_interface *alts;
693 struct usb_interface *iface;
694 int ret;
695
696 if (!enable) {
697 if (subs->interface >= 0) {
698 usb_set_interface(subs->dev, subs->interface, 0);
699 subs->altset_idx = 0;
700 subs->interface = -1;
701 subs->cur_audiofmt = NULL;
702 }
703
704 snd_usb_autosuspend(subs->stream->chip);
705 return 0;
706 }
707
708 snd_usb_autoresume(subs->stream->chip);
709
710 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
711 if (ret < 0)
712 return ret;
713
714 if (datainterval != -EINVAL)
715 fmt = find_format_and_si(subs, datainterval);
716 else
717 fmt = find_format(subs);
718 if (!fmt) {
719 dev_err(&subs->dev->dev,
720 "cannot set format: format = %#x, rate = %d, channels = %d\n",
721 subs->pcm_format, subs->cur_rate, subs->channels);
722 return -EINVAL;
723 }
724
725 subs->altset_idx = 0;
726 subs->interface = -1;
727 if (atomic_read(&subs->stream->chip->shutdown)) {
728 ret = -ENODEV;
729 } else {
730 ret = set_format(subs, fmt);
731 if (ret < 0)
732 return ret;
733
734 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
735 if (!iface) {
736 dev_err(&subs->dev->dev, "Could not get iface %d\n",
737 subs->cur_audiofmt->iface);
738 return -ENODEV;
739 }
740
741 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
742 ret = snd_usb_init_sample_rate(subs->stream->chip,
743 subs->cur_audiofmt->iface,
744 alts,
745 subs->cur_audiofmt,
746 subs->cur_rate);
747 if (ret < 0) {
748 dev_err(&subs->dev->dev, "failed to set rate %d\n",
749 subs->cur_rate);
750 return ret;
751 }
752 }
753
754 subs->interface = fmt->iface;
755 subs->altset_idx = fmt->altset_idx;
756
757 return 0;
758 }
759 EXPORT_SYMBOL_GPL(snd_usb_enable_audio_stream);
760
761 /*
762 * Return the score of matching two audioformats.
763 * Veto the audioformat if:
764 * - It has no channels for some reason.
765 * - Requested PCM format is not supported.
766 * - Requested sample rate is not supported.
767 */
match_endpoint_audioformats(struct snd_usb_substream * subs,struct audioformat * fp,struct audioformat * match,int rate,snd_pcm_format_t pcm_format)768 static int match_endpoint_audioformats(struct snd_usb_substream *subs,
769 struct audioformat *fp,
770 struct audioformat *match, int rate,
771 snd_pcm_format_t pcm_format)
772 {
773 int i;
774 int score = 0;
775
776 if (fp->channels < 1) {
777 dev_dbg(&subs->dev->dev,
778 "%s: (fmt @%p) no channels\n", __func__, fp);
779 return 0;
780 }
781
782 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
783 dev_dbg(&subs->dev->dev,
784 "%s: (fmt @%p) no match for format %d\n", __func__,
785 fp, pcm_format);
786 return 0;
787 }
788
789 for (i = 0; i < fp->nr_rates; i++) {
790 if (fp->rate_table[i] == rate) {
791 score++;
792 break;
793 }
794 }
795 if (!score) {
796 dev_dbg(&subs->dev->dev,
797 "%s: (fmt @%p) no match for rate %d\n", __func__,
798 fp, rate);
799 return 0;
800 }
801
802 if (fp->channels == match->channels)
803 score++;
804
805 dev_dbg(&subs->dev->dev,
806 "%s: (fmt @%p) score %d\n", __func__, fp, score);
807
808 return score;
809 }
810
811 /*
812 * Configure the sync ep using the rate and pcm format of the data ep.
813 */
configure_sync_endpoint(struct snd_usb_substream * subs)814 static int configure_sync_endpoint(struct snd_usb_substream *subs)
815 {
816 int ret;
817 struct audioformat *fp;
818 struct audioformat *sync_fp = NULL;
819 int cur_score = 0;
820 int sync_period_bytes = subs->period_bytes;
821 struct snd_usb_substream *sync_subs =
822 &subs->stream->substream[subs->direction ^ 1];
823
824 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
825 !subs->stream)
826 return snd_usb_endpoint_set_params(subs->sync_endpoint,
827 subs->pcm_format,
828 subs->channels,
829 subs->period_bytes,
830 0, 0,
831 subs->cur_rate,
832 subs->cur_audiofmt,
833 NULL);
834
835 /* Try to find the best matching audioformat. */
836 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
837 int score = match_endpoint_audioformats(subs,
838 fp, subs->cur_audiofmt,
839 subs->cur_rate, subs->pcm_format);
840
841 if (score > cur_score) {
842 sync_fp = fp;
843 cur_score = score;
844 }
845 }
846
847 if (unlikely(sync_fp == NULL)) {
848 dev_err(&subs->dev->dev,
849 "%s: no valid audioformat for sync ep %x found\n",
850 __func__, sync_subs->ep_num);
851 return -EINVAL;
852 }
853
854 /*
855 * Recalculate the period bytes if channel number differ between
856 * data and sync ep audioformat.
857 */
858 if (sync_fp->channels != subs->channels) {
859 sync_period_bytes = (subs->period_bytes / subs->channels) *
860 sync_fp->channels;
861 dev_dbg(&subs->dev->dev,
862 "%s: adjusted sync ep period bytes (%d -> %d)\n",
863 __func__, subs->period_bytes, sync_period_bytes);
864 }
865
866 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
867 subs->pcm_format,
868 sync_fp->channels,
869 sync_period_bytes,
870 0, 0,
871 subs->cur_rate,
872 sync_fp,
873 NULL);
874
875 return ret;
876 }
877
878 /*
879 * configure endpoint params
880 *
881 * called during initial setup and upon resume
882 */
configure_endpoint(struct snd_usb_substream * subs)883 static int configure_endpoint(struct snd_usb_substream *subs)
884 {
885 int ret;
886
887 /* format changed */
888 stop_endpoints(subs);
889 sync_pending_stops(subs);
890 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
891 subs->pcm_format,
892 subs->channels,
893 subs->period_bytes,
894 subs->period_frames,
895 subs->buffer_periods,
896 subs->cur_rate,
897 subs->cur_audiofmt,
898 subs->sync_endpoint);
899 if (ret < 0)
900 return ret;
901
902 if (subs->sync_endpoint)
903 ret = configure_sync_endpoint(subs);
904
905 return ret;
906 }
907
snd_usb_pcm_change_state(struct snd_usb_substream * subs,int state)908 static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state)
909 {
910 int ret;
911
912 if (!subs->str_pd)
913 return 0;
914
915 ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
916 if (ret < 0) {
917 dev_err(&subs->dev->dev,
918 "Cannot change Power Domain ID: %d to state: %d. Err: %d\n",
919 subs->str_pd->pd_id, state, ret);
920 return ret;
921 }
922
923 return 0;
924 }
925
snd_usb_pcm_suspend(struct snd_usb_stream * as)926 int snd_usb_pcm_suspend(struct snd_usb_stream *as)
927 {
928 int ret;
929
930 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2);
931 if (ret < 0)
932 return ret;
933
934 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2);
935 if (ret < 0)
936 return ret;
937
938 return 0;
939 }
940
snd_usb_pcm_resume(struct snd_usb_stream * as)941 int snd_usb_pcm_resume(struct snd_usb_stream *as)
942 {
943 int ret;
944
945 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1);
946 if (ret < 0)
947 return ret;
948
949 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1);
950 if (ret < 0)
951 return ret;
952
953 return 0;
954 }
955
956 /*
957 * hw_params callback
958 *
959 * allocate a buffer and set the given audio format.
960 *
961 * so far we use a physically linear buffer although packetize transfer
962 * doesn't need a continuous area.
963 * if sg buffer is supported on the later version of alsa, we'll follow
964 * that.
965 */
snd_usb_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)966 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
967 struct snd_pcm_hw_params *hw_params)
968 {
969 struct snd_usb_substream *subs = substream->runtime->private_data;
970 struct audioformat *fmt;
971 int ret;
972
973 ret = snd_media_start_pipeline(subs);
974 if (ret)
975 return ret;
976
977 subs->pcm_format = params_format(hw_params);
978 subs->period_bytes = params_period_bytes(hw_params);
979 subs->period_frames = params_period_size(hw_params);
980 subs->buffer_periods = params_periods(hw_params);
981 subs->channels = params_channels(hw_params);
982 subs->cur_rate = params_rate(hw_params);
983
984 fmt = find_format(subs);
985 if (!fmt) {
986 dev_dbg(&subs->dev->dev,
987 "cannot set format: format = %#x, rate = %d, channels = %d\n",
988 subs->pcm_format, subs->cur_rate, subs->channels);
989 ret = -EINVAL;
990 goto stop_pipeline;
991 }
992
993 ret = snd_usb_lock_shutdown(subs->stream->chip);
994 if (ret < 0)
995 goto stop_pipeline;
996
997 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
998 if (ret < 0)
999 goto unlock;
1000
1001 ret = set_format(subs, fmt);
1002 if (ret < 0)
1003 goto unlock;
1004
1005 subs->interface = fmt->iface;
1006 subs->altset_idx = fmt->altset_idx;
1007 subs->need_setup_ep = true;
1008
1009 unlock:
1010 snd_usb_unlock_shutdown(subs->stream->chip);
1011 if (ret < 0)
1012 goto stop_pipeline;
1013 return ret;
1014
1015 stop_pipeline:
1016 snd_media_stop_pipeline(subs);
1017 return ret;
1018 }
1019
1020 /*
1021 * hw_free callback
1022 *
1023 * reset the audio format and release the buffer
1024 */
snd_usb_hw_free(struct snd_pcm_substream * substream)1025 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
1026 {
1027 struct snd_usb_substream *subs = substream->runtime->private_data;
1028
1029 snd_media_stop_pipeline(subs);
1030 subs->cur_audiofmt = NULL;
1031 subs->cur_rate = 0;
1032 subs->period_bytes = 0;
1033 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
1034 stop_endpoints(subs);
1035 sync_pending_stops(subs);
1036 snd_usb_endpoint_deactivate(subs->sync_endpoint);
1037 snd_usb_endpoint_deactivate(subs->data_endpoint);
1038 snd_usb_unlock_shutdown(subs->stream->chip);
1039 }
1040
1041 return 0;
1042 }
1043
1044 /*
1045 * prepare callback
1046 *
1047 * only a few subtle things...
1048 */
snd_usb_pcm_prepare(struct snd_pcm_substream * substream)1049 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
1050 {
1051 struct snd_pcm_runtime *runtime = substream->runtime;
1052 struct snd_usb_substream *subs = runtime->private_data;
1053 struct usb_host_interface *alts;
1054 struct usb_interface *iface;
1055 int ret;
1056
1057 ret = snd_vendor_set_pcm_buf(subs->dev, subs->cur_audiofmt->iface);
1058 if (ret)
1059 return ret;
1060
1061 if (! subs->cur_audiofmt) {
1062 dev_err(&subs->dev->dev, "no format is specified!\n");
1063 return -ENXIO;
1064 }
1065
1066 ret = snd_usb_lock_shutdown(subs->stream->chip);
1067 if (ret < 0)
1068 return ret;
1069 if (snd_BUG_ON(!subs->data_endpoint)) {
1070 ret = -EIO;
1071 goto unlock;
1072 }
1073
1074 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
1075 if (ret < 0)
1076 goto unlock;
1077
1078 ret = set_format(subs, subs->cur_audiofmt);
1079 if (ret < 0)
1080 goto unlock;
1081
1082 if (subs->need_setup_ep) {
1083
1084 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
1085 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
1086 ret = snd_usb_init_sample_rate(subs->stream->chip,
1087 subs->cur_audiofmt->iface,
1088 alts,
1089 subs->cur_audiofmt,
1090 subs->cur_rate);
1091 if (ret < 0)
1092 goto unlock;
1093
1094 if (snd_vendor_get_ops()) {
1095 ret = snd_vendor_set_rate(iface,
1096 subs->cur_audiofmt->iface,
1097 subs->cur_rate,
1098 subs->cur_audiofmt->altsetting);
1099 if (!ret) {
1100 subs->need_setup_ep = false;
1101 goto unlock;
1102 }
1103 }
1104
1105 ret = configure_endpoint(subs);
1106 if (ret < 0)
1107 goto unlock;
1108 subs->need_setup_ep = false;
1109 }
1110
1111 /* some unit conversions in runtime */
1112 subs->data_endpoint->maxframesize =
1113 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
1114 subs->data_endpoint->curframesize =
1115 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
1116
1117 /* reset the pointer */
1118 subs->hwptr_done = 0;
1119 subs->transfer_done = 0;
1120 subs->last_delay = 0;
1121 subs->last_frame_number = 0;
1122 runtime->delay = 0;
1123
1124 /* for playback, submit the URBs now; otherwise, the first hwptr_done
1125 * updates for all URBs would happen at the same time when starting */
1126 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
1127 ret = start_endpoints(subs);
1128
1129 unlock:
1130 snd_usb_unlock_shutdown(subs->stream->chip);
1131 return ret;
1132 }
1133
1134 static const struct snd_pcm_hardware snd_usb_hardware =
1135 {
1136 .info = SNDRV_PCM_INFO_MMAP |
1137 SNDRV_PCM_INFO_MMAP_VALID |
1138 SNDRV_PCM_INFO_BATCH |
1139 SNDRV_PCM_INFO_INTERLEAVED |
1140 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1141 SNDRV_PCM_INFO_PAUSE,
1142 .buffer_bytes_max = 1024 * 1024,
1143 .period_bytes_min = 64,
1144 .period_bytes_max = 512 * 1024,
1145 .periods_min = 2,
1146 .periods_max = 1024,
1147 };
1148
hw_check_valid_format(struct snd_usb_substream * subs,struct snd_pcm_hw_params * params,struct audioformat * fp)1149 static int hw_check_valid_format(struct snd_usb_substream *subs,
1150 struct snd_pcm_hw_params *params,
1151 struct audioformat *fp)
1152 {
1153 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1154 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1155 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1156 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1157 struct snd_mask check_fmts;
1158 unsigned int ptime;
1159
1160 /* check the format */
1161 snd_mask_none(&check_fmts);
1162 check_fmts.bits[0] = (u32)fp->formats;
1163 check_fmts.bits[1] = (u32)(fp->formats >> 32);
1164 snd_mask_intersect(&check_fmts, fmts);
1165 if (snd_mask_empty(&check_fmts)) {
1166 hwc_debug(" > check: no supported format %d\n", fp->format);
1167 return 0;
1168 }
1169 /* check the channels */
1170 if (fp->channels < ct->min || fp->channels > ct->max) {
1171 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1172 return 0;
1173 }
1174 /* check the rate is within the range */
1175 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1176 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1177 return 0;
1178 }
1179 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1180 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1181 return 0;
1182 }
1183 /* check whether the period time is >= the data packet interval */
1184 if (subs->speed != USB_SPEED_FULL) {
1185 ptime = 125 * (1 << fp->datainterval);
1186 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
1187 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
1188 return 0;
1189 }
1190 }
1191 return 1;
1192 }
1193
hw_rule_rate(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)1194 static int hw_rule_rate(struct snd_pcm_hw_params *params,
1195 struct snd_pcm_hw_rule *rule)
1196 {
1197 struct snd_usb_substream *subs = rule->private;
1198 struct audioformat *fp;
1199 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1200 unsigned int rmin, rmax;
1201 int changed;
1202
1203 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1204 changed = 0;
1205 rmin = rmax = 0;
1206 list_for_each_entry(fp, &subs->fmt_list, list) {
1207 if (!hw_check_valid_format(subs, params, fp))
1208 continue;
1209 if (changed++) {
1210 if (rmin > fp->rate_min)
1211 rmin = fp->rate_min;
1212 if (rmax < fp->rate_max)
1213 rmax = fp->rate_max;
1214 } else {
1215 rmin = fp->rate_min;
1216 rmax = fp->rate_max;
1217 }
1218 }
1219
1220 if (!changed) {
1221 hwc_debug(" --> get empty\n");
1222 it->empty = 1;
1223 return -EINVAL;
1224 }
1225
1226 changed = 0;
1227 if (it->min < rmin) {
1228 it->min = rmin;
1229 it->openmin = 0;
1230 changed = 1;
1231 }
1232 if (it->max > rmax) {
1233 it->max = rmax;
1234 it->openmax = 0;
1235 changed = 1;
1236 }
1237 if (snd_interval_checkempty(it)) {
1238 it->empty = 1;
1239 return -EINVAL;
1240 }
1241 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1242 return changed;
1243 }
1244
1245
hw_rule_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)1246 static int hw_rule_channels(struct snd_pcm_hw_params *params,
1247 struct snd_pcm_hw_rule *rule)
1248 {
1249 struct snd_usb_substream *subs = rule->private;
1250 struct audioformat *fp;
1251 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1252 unsigned int rmin, rmax;
1253 int changed;
1254
1255 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1256 changed = 0;
1257 rmin = rmax = 0;
1258 list_for_each_entry(fp, &subs->fmt_list, list) {
1259 if (!hw_check_valid_format(subs, params, fp))
1260 continue;
1261 if (changed++) {
1262 if (rmin > fp->channels)
1263 rmin = fp->channels;
1264 if (rmax < fp->channels)
1265 rmax = fp->channels;
1266 } else {
1267 rmin = fp->channels;
1268 rmax = fp->channels;
1269 }
1270 }
1271
1272 if (!changed) {
1273 hwc_debug(" --> get empty\n");
1274 it->empty = 1;
1275 return -EINVAL;
1276 }
1277
1278 changed = 0;
1279 if (it->min < rmin) {
1280 it->min = rmin;
1281 it->openmin = 0;
1282 changed = 1;
1283 }
1284 if (it->max > rmax) {
1285 it->max = rmax;
1286 it->openmax = 0;
1287 changed = 1;
1288 }
1289 if (snd_interval_checkempty(it)) {
1290 it->empty = 1;
1291 return -EINVAL;
1292 }
1293 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1294 return changed;
1295 }
1296
hw_rule_format(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)1297 static int hw_rule_format(struct snd_pcm_hw_params *params,
1298 struct snd_pcm_hw_rule *rule)
1299 {
1300 struct snd_usb_substream *subs = rule->private;
1301 struct audioformat *fp;
1302 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1303 u64 fbits;
1304 u32 oldbits[2];
1305 int changed;
1306
1307 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1308 fbits = 0;
1309 list_for_each_entry(fp, &subs->fmt_list, list) {
1310 if (!hw_check_valid_format(subs, params, fp))
1311 continue;
1312 fbits |= fp->formats;
1313 }
1314
1315 oldbits[0] = fmt->bits[0];
1316 oldbits[1] = fmt->bits[1];
1317 fmt->bits[0] &= (u32)fbits;
1318 fmt->bits[1] &= (u32)(fbits >> 32);
1319 if (!fmt->bits[0] && !fmt->bits[1]) {
1320 hwc_debug(" --> get empty\n");
1321 return -EINVAL;
1322 }
1323 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1324 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1325 return changed;
1326 }
1327
hw_rule_period_time(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)1328 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1329 struct snd_pcm_hw_rule *rule)
1330 {
1331 struct snd_usb_substream *subs = rule->private;
1332 struct audioformat *fp;
1333 struct snd_interval *it;
1334 unsigned char min_datainterval;
1335 unsigned int pmin;
1336 int changed;
1337
1338 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1339 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1340 min_datainterval = 0xff;
1341 list_for_each_entry(fp, &subs->fmt_list, list) {
1342 if (!hw_check_valid_format(subs, params, fp))
1343 continue;
1344 min_datainterval = min(min_datainterval, fp->datainterval);
1345 }
1346 if (min_datainterval == 0xff) {
1347 hwc_debug(" --> get empty\n");
1348 it->empty = 1;
1349 return -EINVAL;
1350 }
1351 pmin = 125 * (1 << min_datainterval);
1352 changed = 0;
1353 if (it->min < pmin) {
1354 it->min = pmin;
1355 it->openmin = 0;
1356 changed = 1;
1357 }
1358 if (snd_interval_checkempty(it)) {
1359 it->empty = 1;
1360 return -EINVAL;
1361 }
1362 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1363 return changed;
1364 }
1365
1366 /*
1367 * If the device supports unusual bit rates, does the request meet these?
1368 */
snd_usb_pcm_check_knot(struct snd_pcm_runtime * runtime,struct snd_usb_substream * subs)1369 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1370 struct snd_usb_substream *subs)
1371 {
1372 struct audioformat *fp;
1373 int *rate_list;
1374 int count = 0, needs_knot = 0;
1375 int err;
1376
1377 kfree(subs->rate_list.list);
1378 subs->rate_list.list = NULL;
1379
1380 list_for_each_entry(fp, &subs->fmt_list, list) {
1381 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1382 return 0;
1383 count += fp->nr_rates;
1384 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1385 needs_knot = 1;
1386 }
1387 if (!needs_knot)
1388 return 0;
1389
1390 subs->rate_list.list = rate_list =
1391 kmalloc_array(count, sizeof(int), GFP_KERNEL);
1392 if (!subs->rate_list.list)
1393 return -ENOMEM;
1394 subs->rate_list.count = count;
1395 subs->rate_list.mask = 0;
1396 count = 0;
1397 list_for_each_entry(fp, &subs->fmt_list, list) {
1398 int i;
1399 for (i = 0; i < fp->nr_rates; i++)
1400 rate_list[count++] = fp->rate_table[i];
1401 }
1402 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1403 &subs->rate_list);
1404 if (err < 0)
1405 return err;
1406
1407 return 0;
1408 }
1409
1410
1411 /*
1412 * set up the runtime hardware information.
1413 */
1414
setup_hw_info(struct snd_pcm_runtime * runtime,struct snd_usb_substream * subs)1415 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1416 {
1417 struct audioformat *fp;
1418 unsigned int pt, ptmin;
1419 int param_period_time_if_needed;
1420 int err;
1421
1422 runtime->hw.formats = subs->formats;
1423
1424 runtime->hw.rate_min = 0x7fffffff;
1425 runtime->hw.rate_max = 0;
1426 runtime->hw.channels_min = 256;
1427 runtime->hw.channels_max = 0;
1428 runtime->hw.rates = 0;
1429 ptmin = UINT_MAX;
1430 /* check min/max rates and channels */
1431 list_for_each_entry(fp, &subs->fmt_list, list) {
1432 runtime->hw.rates |= fp->rates;
1433 if (runtime->hw.rate_min > fp->rate_min)
1434 runtime->hw.rate_min = fp->rate_min;
1435 if (runtime->hw.rate_max < fp->rate_max)
1436 runtime->hw.rate_max = fp->rate_max;
1437 if (runtime->hw.channels_min > fp->channels)
1438 runtime->hw.channels_min = fp->channels;
1439 if (runtime->hw.channels_max < fp->channels)
1440 runtime->hw.channels_max = fp->channels;
1441 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1442 /* FIXME: there might be more than one audio formats... */
1443 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1444 fp->frame_size;
1445 }
1446 pt = 125 * (1 << fp->datainterval);
1447 ptmin = min(ptmin, pt);
1448 }
1449
1450 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
1451 if (subs->speed == USB_SPEED_FULL)
1452 /* full speed devices have fixed data packet interval */
1453 ptmin = 1000;
1454 if (ptmin == 1000)
1455 /* if period time doesn't go below 1 ms, no rules needed */
1456 param_period_time_if_needed = -1;
1457
1458 err = snd_pcm_hw_constraint_minmax(runtime,
1459 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1460 ptmin, UINT_MAX);
1461 if (err < 0)
1462 return err;
1463
1464 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1465 hw_rule_rate, subs,
1466 SNDRV_PCM_HW_PARAM_FORMAT,
1467 SNDRV_PCM_HW_PARAM_CHANNELS,
1468 param_period_time_if_needed,
1469 -1);
1470 if (err < 0)
1471 return err;
1472 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1473 hw_rule_channels, subs,
1474 SNDRV_PCM_HW_PARAM_FORMAT,
1475 SNDRV_PCM_HW_PARAM_RATE,
1476 param_period_time_if_needed,
1477 -1);
1478 if (err < 0)
1479 return err;
1480 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1481 hw_rule_format, subs,
1482 SNDRV_PCM_HW_PARAM_RATE,
1483 SNDRV_PCM_HW_PARAM_CHANNELS,
1484 param_period_time_if_needed,
1485 -1);
1486 if (err < 0)
1487 return err;
1488 if (param_period_time_if_needed >= 0) {
1489 err = snd_pcm_hw_rule_add(runtime, 0,
1490 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1491 hw_rule_period_time, subs,
1492 SNDRV_PCM_HW_PARAM_FORMAT,
1493 SNDRV_PCM_HW_PARAM_CHANNELS,
1494 SNDRV_PCM_HW_PARAM_RATE,
1495 -1);
1496 if (err < 0)
1497 return err;
1498 }
1499 err = snd_usb_pcm_check_knot(runtime, subs);
1500 if (err < 0)
1501 return err;
1502
1503 return snd_usb_autoresume(subs->stream->chip);
1504 }
1505
snd_usb_pcm_open(struct snd_pcm_substream * substream)1506 static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
1507 {
1508 int direction = substream->stream;
1509 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1510 struct snd_pcm_runtime *runtime = substream->runtime;
1511 struct snd_usb_substream *subs = &as->substream[direction];
1512 int ret;
1513 bool is_support = false;
1514
1515 ret = snd_vendor_set_pcm_connection(subs->dev, SOUND_PCM_OPEN,
1516 direction);
1517 if (ret)
1518 return ret;
1519
1520 subs->interface = -1;
1521 subs->altset_idx = 0;
1522 runtime->hw = snd_usb_hardware;
1523 runtime->private_data = subs;
1524 subs->pcm_substream = substream;
1525 /* runtime PM is also done there */
1526
1527 /* initialize DSD/DOP context */
1528 subs->dsd_dop.byte_idx = 0;
1529 subs->dsd_dop.channel = 0;
1530 subs->dsd_dop.marker = 1;
1531
1532 ret = setup_hw_info(runtime, subs);
1533 if (ret == 0) {
1534 ret = snd_media_stream_init(subs, as->pcm, direction);
1535 if (ret)
1536 snd_usb_autosuspend(subs->stream->chip);
1537 }
1538
1539 trace_android_vh_sound_usb_support_cpu_suspend(subs->dev, direction, &is_support);
1540 if (!ret && is_support)
1541 snd_usb_autosuspend(subs->stream->chip);
1542
1543 return ret;
1544 }
1545
snd_usb_pcm_close(struct snd_pcm_substream * substream)1546 static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
1547 {
1548 int direction = substream->stream;
1549 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1550 struct snd_usb_substream *subs = &as->substream[direction];
1551 int ret;
1552 bool is_support = false;
1553
1554 ret = snd_vendor_set_pcm_connection(subs->dev, SOUND_PCM_CLOSE,
1555 direction);
1556 if (ret)
1557 return ret;
1558
1559 trace_android_vh_sound_usb_support_cpu_suspend(subs->dev, direction, &is_support);
1560 if (!ret && is_support)
1561 snd_usb_autoresume(subs->stream->chip);
1562
1563 snd_media_stop_pipeline(subs);
1564
1565 if (!as->chip->keep_iface &&
1566 subs->interface >= 0 &&
1567 !snd_usb_lock_shutdown(subs->stream->chip)) {
1568 usb_set_interface(subs->dev, subs->interface, 0);
1569 ret = snd_vendor_set_pcm_intf(usb_ifnum_to_if(subs->dev,
1570 subs->interface),
1571 subs->interface, 0,
1572 direction);
1573 if (ret)
1574 return ret;
1575 subs->interface = -1;
1576 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
1577 snd_usb_unlock_shutdown(subs->stream->chip);
1578 if (ret < 0)
1579 return ret;
1580 }
1581
1582 subs->pcm_substream = NULL;
1583 snd_usb_autosuspend(subs->stream->chip);
1584
1585 return 0;
1586 }
1587
1588 /* Since a URB can handle only a single linear buffer, we must use double
1589 * buffering when the data to be transferred overflows the buffer boundary.
1590 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1591 * for all URBs.
1592 */
retire_capture_urb(struct snd_usb_substream * subs,struct urb * urb)1593 static void retire_capture_urb(struct snd_usb_substream *subs,
1594 struct urb *urb)
1595 {
1596 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1597 unsigned int stride, frames, bytes, oldptr;
1598 int i, period_elapsed = 0;
1599 unsigned long flags;
1600 unsigned char *cp;
1601 int current_frame_number;
1602
1603 /* read frame number here, update pointer in critical section */
1604 current_frame_number = usb_get_current_frame_number(subs->dev);
1605
1606 stride = runtime->frame_bits >> 3;
1607
1608 for (i = 0; i < urb->number_of_packets; i++) {
1609 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
1610 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1611 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1612 i, urb->iso_frame_desc[i].status);
1613 // continue;
1614 }
1615 bytes = urb->iso_frame_desc[i].actual_length;
1616 if (subs->stream_offset_adj > 0) {
1617 unsigned int adj = min(subs->stream_offset_adj, bytes);
1618 cp += adj;
1619 bytes -= adj;
1620 subs->stream_offset_adj -= adj;
1621 }
1622 frames = bytes / stride;
1623 if (!subs->txfr_quirk)
1624 bytes = frames * stride;
1625 if (bytes % (runtime->sample_bits >> 3) != 0) {
1626 int oldbytes = bytes;
1627 bytes = frames * stride;
1628 dev_warn_ratelimited(&subs->dev->dev,
1629 "Corrected urb data len. %d->%d\n",
1630 oldbytes, bytes);
1631 }
1632 /* update the current pointer */
1633 spin_lock_irqsave(&subs->lock, flags);
1634 oldptr = subs->hwptr_done;
1635 subs->hwptr_done += bytes;
1636 if (subs->hwptr_done >= runtime->buffer_size * stride)
1637 subs->hwptr_done -= runtime->buffer_size * stride;
1638 frames = (bytes + (oldptr % stride)) / stride;
1639 subs->transfer_done += frames;
1640 if (subs->transfer_done >= runtime->period_size) {
1641 subs->transfer_done -= runtime->period_size;
1642 period_elapsed = 1;
1643 }
1644 /* capture delay is by construction limited to one URB,
1645 * reset delays here
1646 */
1647 runtime->delay = subs->last_delay = 0;
1648
1649 /* realign last_frame_number */
1650 subs->last_frame_number = current_frame_number;
1651 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1652
1653 spin_unlock_irqrestore(&subs->lock, flags);
1654 /* copy a data chunk */
1655 if (oldptr + bytes > runtime->buffer_size * stride) {
1656 unsigned int bytes1 =
1657 runtime->buffer_size * stride - oldptr;
1658 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1659 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1660 } else {
1661 memcpy(runtime->dma_area + oldptr, cp, bytes);
1662 }
1663 }
1664
1665 if (period_elapsed)
1666 snd_pcm_period_elapsed(subs->pcm_substream);
1667 }
1668
fill_playback_urb_dsd_dop(struct snd_usb_substream * subs,struct urb * urb,unsigned int bytes)1669 static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1670 struct urb *urb, unsigned int bytes)
1671 {
1672 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1673 unsigned int stride = runtime->frame_bits >> 3;
1674 unsigned int dst_idx = 0;
1675 unsigned int src_idx = subs->hwptr_done;
1676 unsigned int wrap = runtime->buffer_size * stride;
1677 u8 *dst = urb->transfer_buffer;
1678 u8 *src = runtime->dma_area;
1679 u8 marker[] = { 0x05, 0xfa };
1680
1681 /*
1682 * The DSP DOP format defines a way to transport DSD samples over
1683 * normal PCM data endpoints. It requires stuffing of marker bytes
1684 * (0x05 and 0xfa, alternating per sample frame), and then expects
1685 * 2 additional bytes of actual payload. The whole frame is stored
1686 * LSB.
1687 *
1688 * Hence, for a stereo transport, the buffer layout looks like this,
1689 * where L refers to left channel samples and R to right.
1690 *
1691 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1692 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1693 * .....
1694 *
1695 */
1696
1697 while (bytes--) {
1698 if (++subs->dsd_dop.byte_idx == 3) {
1699 /* frame boundary? */
1700 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1701 src_idx += 2;
1702 subs->dsd_dop.byte_idx = 0;
1703
1704 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1705 /* alternate the marker */
1706 subs->dsd_dop.marker++;
1707 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1708 subs->dsd_dop.channel = 0;
1709 }
1710 } else {
1711 /* stuff the DSD payload */
1712 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
1713
1714 if (subs->cur_audiofmt->dsd_bitrev)
1715 dst[dst_idx++] = bitrev8(src[idx]);
1716 else
1717 dst[dst_idx++] = src[idx];
1718
1719 subs->hwptr_done++;
1720 }
1721 }
1722 if (subs->hwptr_done >= runtime->buffer_size * stride)
1723 subs->hwptr_done -= runtime->buffer_size * stride;
1724 }
1725
copy_to_urb(struct snd_usb_substream * subs,struct urb * urb,int offset,int stride,unsigned int bytes)1726 static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1727 int offset, int stride, unsigned int bytes)
1728 {
1729 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1730
1731 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1732 /* err, the transferred area goes over buffer boundary. */
1733 unsigned int bytes1 =
1734 runtime->buffer_size * stride - subs->hwptr_done;
1735 memcpy(urb->transfer_buffer + offset,
1736 runtime->dma_area + subs->hwptr_done, bytes1);
1737 memcpy(urb->transfer_buffer + offset + bytes1,
1738 runtime->dma_area, bytes - bytes1);
1739 } else {
1740 memcpy(urb->transfer_buffer + offset,
1741 runtime->dma_area + subs->hwptr_done, bytes);
1742 }
1743 subs->hwptr_done += bytes;
1744 if (subs->hwptr_done >= runtime->buffer_size * stride)
1745 subs->hwptr_done -= runtime->buffer_size * stride;
1746 }
1747
copy_to_urb_quirk(struct snd_usb_substream * subs,struct urb * urb,int stride,unsigned int bytes)1748 static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1749 struct urb *urb, int stride,
1750 unsigned int bytes)
1751 {
1752 __le32 packet_length;
1753 int i;
1754
1755 /* Put __le32 length descriptor at start of each packet. */
1756 for (i = 0; i < urb->number_of_packets; i++) {
1757 unsigned int length = urb->iso_frame_desc[i].length;
1758 unsigned int offset = urb->iso_frame_desc[i].offset;
1759
1760 packet_length = cpu_to_le32(length);
1761 offset += i * sizeof(packet_length);
1762 urb->iso_frame_desc[i].offset = offset;
1763 urb->iso_frame_desc[i].length += sizeof(packet_length);
1764 memcpy(urb->transfer_buffer + offset,
1765 &packet_length, sizeof(packet_length));
1766 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1767 stride, length);
1768 }
1769 /* Adjust transfer size accordingly. */
1770 bytes += urb->number_of_packets * sizeof(packet_length);
1771 return bytes;
1772 }
1773
prepare_playback_urb(struct snd_usb_substream * subs,struct urb * urb)1774 static void prepare_playback_urb(struct snd_usb_substream *subs,
1775 struct urb *urb)
1776 {
1777 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1778 struct snd_usb_endpoint *ep = subs->data_endpoint;
1779 struct snd_urb_ctx *ctx = urb->context;
1780 unsigned int counts, frames, bytes;
1781 int i, stride, period_elapsed = 0;
1782 unsigned long flags;
1783
1784 stride = runtime->frame_bits >> 3;
1785
1786 frames = 0;
1787 urb->number_of_packets = 0;
1788 spin_lock_irqsave(&subs->lock, flags);
1789 subs->frame_limit += ep->max_urb_frames;
1790 for (i = 0; i < ctx->packets; i++) {
1791 if (ctx->packet_size[i])
1792 counts = ctx->packet_size[i];
1793 else if (ep->sync_master)
1794 counts = snd_usb_endpoint_slave_next_packet_size(ep);
1795 else
1796 counts = snd_usb_endpoint_next_packet_size(ep);
1797
1798 /* set up descriptor */
1799 urb->iso_frame_desc[i].offset = frames * ep->stride;
1800 urb->iso_frame_desc[i].length = counts * ep->stride;
1801 frames += counts;
1802 urb->number_of_packets++;
1803 subs->transfer_done += counts;
1804 if (subs->transfer_done >= runtime->period_size) {
1805 subs->transfer_done -= runtime->period_size;
1806 subs->frame_limit = 0;
1807 period_elapsed = 1;
1808 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1809 if (subs->transfer_done > 0) {
1810 /* FIXME: fill-max mode is not
1811 * supported yet */
1812 frames -= subs->transfer_done;
1813 counts -= subs->transfer_done;
1814 urb->iso_frame_desc[i].length =
1815 counts * ep->stride;
1816 subs->transfer_done = 0;
1817 }
1818 i++;
1819 if (i < ctx->packets) {
1820 /* add a transfer delimiter */
1821 urb->iso_frame_desc[i].offset =
1822 frames * ep->stride;
1823 urb->iso_frame_desc[i].length = 0;
1824 urb->number_of_packets++;
1825 }
1826 break;
1827 }
1828 }
1829 /* finish at the period boundary or after enough frames */
1830 if ((period_elapsed ||
1831 subs->transfer_done >= subs->frame_limit) &&
1832 !snd_usb_endpoint_implicit_feedback_sink(ep))
1833 break;
1834 }
1835 bytes = frames * ep->stride;
1836
1837 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1838 subs->cur_audiofmt->dsd_dop)) {
1839 fill_playback_urb_dsd_dop(subs, urb, bytes);
1840 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1841 subs->cur_audiofmt->dsd_bitrev)) {
1842 /* bit-reverse the bytes */
1843 u8 *buf = urb->transfer_buffer;
1844 for (i = 0; i < bytes; i++) {
1845 int idx = (subs->hwptr_done + i)
1846 % (runtime->buffer_size * stride);
1847 buf[i] = bitrev8(runtime->dma_area[idx]);
1848 }
1849
1850 subs->hwptr_done += bytes;
1851 if (subs->hwptr_done >= runtime->buffer_size * stride)
1852 subs->hwptr_done -= runtime->buffer_size * stride;
1853 } else {
1854 /* usual PCM */
1855 if (!subs->tx_length_quirk)
1856 copy_to_urb(subs, urb, 0, stride, bytes);
1857 else
1858 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1859 /* bytes is now amount of outgoing data */
1860 }
1861
1862 /* update delay with exact number of samples queued */
1863 runtime->delay = subs->last_delay;
1864 runtime->delay += frames;
1865 subs->last_delay = runtime->delay;
1866
1867 /* realign last_frame_number */
1868 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1869 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1870
1871 if (subs->trigger_tstamp_pending_update) {
1872 /* this is the first actual URB submitted,
1873 * update trigger timestamp to reflect actual start time
1874 */
1875 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1876 subs->trigger_tstamp_pending_update = false;
1877 }
1878
1879 spin_unlock_irqrestore(&subs->lock, flags);
1880 urb->transfer_buffer_length = bytes;
1881 if (period_elapsed)
1882 snd_pcm_period_elapsed(subs->pcm_substream);
1883 }
1884
1885 /*
1886 * process after playback data complete
1887 * - decrease the delay count again
1888 */
retire_playback_urb(struct snd_usb_substream * subs,struct urb * urb)1889 static void retire_playback_urb(struct snd_usb_substream *subs,
1890 struct urb *urb)
1891 {
1892 unsigned long flags;
1893 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1894 struct snd_usb_endpoint *ep = subs->data_endpoint;
1895 int processed = urb->transfer_buffer_length / ep->stride;
1896 int est_delay;
1897
1898 /* ignore the delay accounting when processed=0 is given, i.e.
1899 * silent payloads are processed before handling the actual data
1900 */
1901 if (!processed)
1902 return;
1903
1904 spin_lock_irqsave(&subs->lock, flags);
1905 if (!subs->last_delay)
1906 goto out; /* short path */
1907
1908 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1909 /* update delay with exact number of samples played */
1910 if (processed > subs->last_delay)
1911 subs->last_delay = 0;
1912 else
1913 subs->last_delay -= processed;
1914 runtime->delay = subs->last_delay;
1915
1916 /*
1917 * Report when delay estimate is off by more than 2ms.
1918 * The error should be lower than 2ms since the estimate relies
1919 * on two reads of a counter updated every ms.
1920 */
1921 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1922 dev_dbg_ratelimited(&subs->dev->dev,
1923 "delay: estimated %d, actual %d\n",
1924 est_delay, subs->last_delay);
1925
1926 if (!subs->running) {
1927 /* update last_frame_number for delay counting here since
1928 * prepare_playback_urb won't be called during pause
1929 */
1930 subs->last_frame_number =
1931 usb_get_current_frame_number(subs->dev) & 0xff;
1932 }
1933
1934 out:
1935 spin_unlock_irqrestore(&subs->lock, flags);
1936 }
1937
snd_usb_substream_playback_trigger(struct snd_pcm_substream * substream,int cmd)1938 static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1939 int cmd)
1940 {
1941 struct snd_usb_substream *subs = substream->runtime->private_data;
1942
1943 switch (cmd) {
1944 case SNDRV_PCM_TRIGGER_START:
1945 subs->trigger_tstamp_pending_update = true;
1946 fallthrough;
1947 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1948 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1949 subs->data_endpoint->retire_data_urb = retire_playback_urb;
1950 subs->running = 1;
1951 return 0;
1952 case SNDRV_PCM_TRIGGER_STOP:
1953 stop_endpoints(subs);
1954 subs->running = 0;
1955 return 0;
1956 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1957 subs->data_endpoint->prepare_data_urb = NULL;
1958 /* keep retire_data_urb for delay calculation */
1959 subs->data_endpoint->retire_data_urb = retire_playback_urb;
1960 subs->running = 0;
1961 return 0;
1962 case SNDRV_PCM_TRIGGER_SUSPEND:
1963 if (subs->stream->chip->setup_fmt_after_resume_quirk) {
1964 stop_endpoints(subs);
1965 subs->need_setup_fmt = true;
1966 return 0;
1967 }
1968 break;
1969 }
1970
1971 return -EINVAL;
1972 }
1973
snd_usb_substream_capture_trigger(struct snd_pcm_substream * substream,int cmd)1974 static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1975 int cmd)
1976 {
1977 int err;
1978 struct snd_usb_substream *subs = substream->runtime->private_data;
1979
1980 switch (cmd) {
1981 case SNDRV_PCM_TRIGGER_START:
1982 err = start_endpoints(subs);
1983 if (err < 0)
1984 return err;
1985
1986 subs->data_endpoint->retire_data_urb = retire_capture_urb;
1987 subs->running = 1;
1988 return 0;
1989 case SNDRV_PCM_TRIGGER_STOP:
1990 stop_endpoints(subs);
1991 subs->data_endpoint->retire_data_urb = NULL;
1992 subs->running = 0;
1993 return 0;
1994 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1995 subs->data_endpoint->retire_data_urb = NULL;
1996 subs->running = 0;
1997 return 0;
1998 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1999 subs->data_endpoint->retire_data_urb = retire_capture_urb;
2000 subs->running = 1;
2001 return 0;
2002 case SNDRV_PCM_TRIGGER_SUSPEND:
2003 if (subs->stream->chip->setup_fmt_after_resume_quirk) {
2004 stop_endpoints(subs);
2005 subs->need_setup_fmt = true;
2006 return 0;
2007 }
2008 break;
2009 }
2010
2011 return -EINVAL;
2012 }
2013
2014 static const struct snd_pcm_ops snd_usb_playback_ops = {
2015 .open = snd_usb_pcm_open,
2016 .close = snd_usb_pcm_close,
2017 .hw_params = snd_usb_hw_params,
2018 .hw_free = snd_usb_hw_free,
2019 .prepare = snd_usb_pcm_prepare,
2020 .trigger = snd_usb_substream_playback_trigger,
2021 .sync_stop = snd_usb_pcm_sync_stop,
2022 .pointer = snd_usb_pcm_pointer,
2023 };
2024
2025 static const struct snd_pcm_ops snd_usb_capture_ops = {
2026 .open = snd_usb_pcm_open,
2027 .close = snd_usb_pcm_close,
2028 .hw_params = snd_usb_hw_params,
2029 .hw_free = snd_usb_hw_free,
2030 .prepare = snd_usb_pcm_prepare,
2031 .trigger = snd_usb_substream_capture_trigger,
2032 .sync_stop = snd_usb_pcm_sync_stop,
2033 .pointer = snd_usb_pcm_pointer,
2034 };
2035
snd_usb_set_pcm_ops(struct snd_pcm * pcm,int stream)2036 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
2037 {
2038 const struct snd_pcm_ops *ops;
2039
2040 ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
2041 &snd_usb_playback_ops : &snd_usb_capture_ops;
2042 snd_pcm_set_ops(pcm, stream, ops);
2043 }
2044
snd_usb_preallocate_buffer(struct snd_usb_substream * subs)2045 void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
2046 {
2047 struct snd_pcm *pcm = subs->stream->pcm;
2048 struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
2049 struct device *dev = subs->dev->bus->sysdev;
2050
2051 if (snd_usb_use_vmalloc)
2052 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_VMALLOC,
2053 NULL, 0, 0);
2054 else
2055 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_DEV_SG,
2056 dev, 64*1024, 512*1024);
2057 }
2058