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