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