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