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