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) 1340 dev_dbg_ratelimited(&subs->dev->dev, 1341 "frame %d active: %d\n", i, 1342 urb->iso_frame_desc[i].status); 1343 bytes = urb->iso_frame_desc[i].actual_length; 1344 if (subs->stream_offset_adj > 0) { 1345 unsigned int adj = min(subs->stream_offset_adj, bytes); 1346 cp += adj; 1347 bytes -= adj; 1348 subs->stream_offset_adj -= adj; 1349 } 1350 frames = bytes / stride; 1351 if (!subs->txfr_quirk) 1352 bytes = frames * stride; 1353 if (bytes % (runtime->sample_bits >> 3) != 0) { 1354 int oldbytes = bytes; 1355 bytes = frames * stride; 1356 dev_warn_ratelimited(&subs->dev->dev, 1357 "Corrected urb data len. %d->%d\n", 1358 oldbytes, bytes); 1359 } 1360 /* update the current pointer */ 1361 spin_lock_irqsave(&subs->lock, flags); 1362 oldptr = subs->hwptr_done; 1363 subs->hwptr_done += bytes; 1364 if (subs->hwptr_done >= subs->buffer_bytes) 1365 subs->hwptr_done -= subs->buffer_bytes; 1366 frames = (bytes + (oldptr % stride)) / stride; 1367 subs->transfer_done += frames; 1368 if (subs->transfer_done >= runtime->period_size) { 1369 subs->transfer_done -= runtime->period_size; 1370 period_elapsed = 1; 1371 } 1372 1373 /* realign last_frame_number */ 1374 subs->last_frame_number = current_frame_number; 1375 1376 spin_unlock_irqrestore(&subs->lock, flags); 1377 /* copy a data chunk */ 1378 if (oldptr + bytes > subs->buffer_bytes) { 1379 unsigned int bytes1 = subs->buffer_bytes - oldptr; 1380 1381 memcpy(runtime->dma_area + oldptr, cp, bytes1); 1382 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1); 1383 } else { 1384 memcpy(runtime->dma_area + oldptr, cp, bytes); 1385 } 1386 } 1387 1388 if (period_elapsed) 1389 snd_pcm_period_elapsed(subs->pcm_substream); 1390 } 1391 1392 static void urb_ctx_queue_advance(struct snd_usb_substream *subs, 1393 struct urb *urb, unsigned int bytes) 1394 { 1395 struct snd_urb_ctx *ctx = urb->context; 1396 1397 ctx->queued += bytes; 1398 subs->inflight_bytes += bytes; 1399 subs->hwptr_done += bytes; 1400 if (subs->hwptr_done >= subs->buffer_bytes) 1401 subs->hwptr_done -= subs->buffer_bytes; 1402 } 1403 1404 static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs, 1405 struct urb *urb, unsigned int bytes) 1406 { 1407 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; 1408 unsigned int dst_idx = 0; 1409 unsigned int src_idx = subs->hwptr_done; 1410 unsigned int wrap = subs->buffer_bytes; 1411 u8 *dst = urb->transfer_buffer; 1412 u8 *src = runtime->dma_area; 1413 static const u8 marker[] = { 0x05, 0xfa }; 1414 unsigned int queued = 0; 1415 1416 /* 1417 * The DSP DOP format defines a way to transport DSD samples over 1418 * normal PCM data endpoints. It requires stuffing of marker bytes 1419 * (0x05 and 0xfa, alternating per sample frame), and then expects 1420 * 2 additional bytes of actual payload. The whole frame is stored 1421 * LSB. 1422 * 1423 * Hence, for a stereo transport, the buffer layout looks like this, 1424 * where L refers to left channel samples and R to right. 1425 * 1426 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa 1427 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa 1428 * ..... 1429 * 1430 */ 1431 1432 while (bytes--) { 1433 if (++subs->dsd_dop.byte_idx == 3) { 1434 /* frame boundary? */ 1435 dst[dst_idx++] = marker[subs->dsd_dop.marker]; 1436 src_idx += 2; 1437 subs->dsd_dop.byte_idx = 0; 1438 1439 if (++subs->dsd_dop.channel % runtime->channels == 0) { 1440 /* alternate the marker */ 1441 subs->dsd_dop.marker++; 1442 subs->dsd_dop.marker %= ARRAY_SIZE(marker); 1443 subs->dsd_dop.channel = 0; 1444 } 1445 } else { 1446 /* stuff the DSD payload */ 1447 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap; 1448 1449 if (subs->cur_audiofmt->dsd_bitrev) 1450 dst[dst_idx++] = bitrev8(src[idx]); 1451 else 1452 dst[dst_idx++] = src[idx]; 1453 queued++; 1454 } 1455 } 1456 1457 urb_ctx_queue_advance(subs, urb, queued); 1458 } 1459 1460 /* copy bit-reversed bytes onto transfer buffer */ 1461 static void fill_playback_urb_dsd_bitrev(struct snd_usb_substream *subs, 1462 struct urb *urb, unsigned int bytes) 1463 { 1464 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; 1465 const u8 *src = runtime->dma_area; 1466 u8 *buf = urb->transfer_buffer; 1467 int i, ofs = subs->hwptr_done; 1468 1469 for (i = 0; i < bytes; i++) { 1470 *buf++ = bitrev8(src[ofs]); 1471 if (++ofs >= subs->buffer_bytes) 1472 ofs = 0; 1473 } 1474 1475 urb_ctx_queue_advance(subs, urb, bytes); 1476 } 1477 1478 static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb, 1479 int offset, int stride, unsigned int bytes) 1480 { 1481 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; 1482 1483 if (subs->hwptr_done + bytes > subs->buffer_bytes) { 1484 /* err, the transferred area goes over buffer boundary. */ 1485 unsigned int bytes1 = subs->buffer_bytes - subs->hwptr_done; 1486 1487 memcpy(urb->transfer_buffer + offset, 1488 runtime->dma_area + subs->hwptr_done, bytes1); 1489 memcpy(urb->transfer_buffer + offset + bytes1, 1490 runtime->dma_area, bytes - bytes1); 1491 } else { 1492 memcpy(urb->transfer_buffer + offset, 1493 runtime->dma_area + subs->hwptr_done, bytes); 1494 } 1495 1496 urb_ctx_queue_advance(subs, urb, bytes); 1497 } 1498 1499 static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs, 1500 struct urb *urb, int stride, 1501 unsigned int bytes) 1502 { 1503 __le32 packet_length; 1504 int i; 1505 1506 /* Put __le32 length descriptor at start of each packet. */ 1507 for (i = 0; i < urb->number_of_packets; i++) { 1508 unsigned int length = urb->iso_frame_desc[i].length; 1509 unsigned int offset = urb->iso_frame_desc[i].offset; 1510 1511 packet_length = cpu_to_le32(length); 1512 offset += i * sizeof(packet_length); 1513 urb->iso_frame_desc[i].offset = offset; 1514 urb->iso_frame_desc[i].length += sizeof(packet_length); 1515 memcpy(urb->transfer_buffer + offset, 1516 &packet_length, sizeof(packet_length)); 1517 copy_to_urb(subs, urb, offset + sizeof(packet_length), 1518 stride, length); 1519 } 1520 /* Adjust transfer size accordingly. */ 1521 bytes += urb->number_of_packets * sizeof(packet_length); 1522 return bytes; 1523 } 1524 1525 static int prepare_playback_urb(struct snd_usb_substream *subs, 1526 struct urb *urb, 1527 bool in_stream_lock) 1528 { 1529 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; 1530 struct snd_usb_endpoint *ep = subs->data_endpoint; 1531 struct snd_urb_ctx *ctx = urb->context; 1532 unsigned int frames, bytes; 1533 int counts; 1534 unsigned int transfer_done, frame_limit, avail = 0; 1535 int i, stride, period_elapsed = 0; 1536 unsigned long flags; 1537 int err = 0; 1538 1539 stride = ep->stride; 1540 1541 frames = 0; 1542 ctx->queued = 0; 1543 urb->number_of_packets = 0; 1544 1545 spin_lock_irqsave(&subs->lock, flags); 1546 frame_limit = subs->frame_limit + ep->max_urb_frames; 1547 transfer_done = subs->transfer_done; 1548 1549 if (subs->lowlatency_playback && 1550 runtime->state != SNDRV_PCM_STATE_DRAINING) { 1551 unsigned int hwptr = subs->hwptr_done / stride; 1552 1553 /* calculate the byte offset-in-buffer of the appl_ptr */ 1554 avail = (runtime->control->appl_ptr - runtime->hw_ptr_base) 1555 % runtime->buffer_size; 1556 if (avail <= hwptr) 1557 avail += runtime->buffer_size; 1558 avail -= hwptr; 1559 } 1560 1561 for (i = 0; i < ctx->packets; i++) { 1562 counts = snd_usb_endpoint_next_packet_size(ep, ctx, i, avail); 1563 if (counts < 0) 1564 break; 1565 /* set up descriptor */ 1566 urb->iso_frame_desc[i].offset = frames * stride; 1567 urb->iso_frame_desc[i].length = counts * stride; 1568 frames += counts; 1569 avail -= counts; 1570 urb->number_of_packets++; 1571 transfer_done += counts; 1572 if (transfer_done >= runtime->period_size) { 1573 transfer_done -= runtime->period_size; 1574 frame_limit = 0; 1575 period_elapsed = 1; 1576 if (subs->fmt_type == UAC_FORMAT_TYPE_II) { 1577 if (transfer_done > 0) { 1578 /* FIXME: fill-max mode is not 1579 * supported yet */ 1580 frames -= transfer_done; 1581 counts -= transfer_done; 1582 urb->iso_frame_desc[i].length = 1583 counts * stride; 1584 transfer_done = 0; 1585 } 1586 i++; 1587 if (i < ctx->packets) { 1588 /* add a transfer delimiter */ 1589 urb->iso_frame_desc[i].offset = 1590 frames * stride; 1591 urb->iso_frame_desc[i].length = 0; 1592 urb->number_of_packets++; 1593 } 1594 break; 1595 } 1596 } 1597 /* finish at the period boundary or after enough frames */ 1598 if ((period_elapsed || transfer_done >= frame_limit) && 1599 !snd_usb_endpoint_implicit_feedback_sink(ep)) 1600 break; 1601 } 1602 1603 if (!frames) { 1604 err = -EAGAIN; 1605 goto unlock; 1606 } 1607 1608 bytes = frames * stride; 1609 subs->transfer_done = transfer_done; 1610 subs->frame_limit = frame_limit; 1611 if (unlikely(ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE && 1612 subs->cur_audiofmt->dsd_dop)) { 1613 fill_playback_urb_dsd_dop(subs, urb, bytes); 1614 } else if (unlikely(ep->cur_format == SNDRV_PCM_FORMAT_DSD_U8 && 1615 subs->cur_audiofmt->dsd_bitrev)) { 1616 fill_playback_urb_dsd_bitrev(subs, urb, bytes); 1617 } else { 1618 /* usual PCM */ 1619 if (!subs->tx_length_quirk) 1620 copy_to_urb(subs, urb, 0, stride, bytes); 1621 else 1622 bytes = copy_to_urb_quirk(subs, urb, stride, bytes); 1623 /* bytes is now amount of outgoing data */ 1624 } 1625 1626 subs->last_frame_number = usb_get_current_frame_number(subs->dev); 1627 1628 if (subs->trigger_tstamp_pending_update) { 1629 /* this is the first actual URB submitted, 1630 * update trigger timestamp to reflect actual start time 1631 */ 1632 snd_pcm_gettime(runtime, &runtime->trigger_tstamp); 1633 subs->trigger_tstamp_pending_update = false; 1634 } 1635 1636 if (period_elapsed && !subs->running && subs->lowlatency_playback) { 1637 subs->period_elapsed_pending = 1; 1638 period_elapsed = 0; 1639 } 1640 1641 unlock: 1642 spin_unlock_irqrestore(&subs->lock, flags); 1643 if (err < 0) 1644 return err; 1645 urb->transfer_buffer_length = bytes; 1646 if (period_elapsed) { 1647 if (in_stream_lock) 1648 snd_pcm_period_elapsed_under_stream_lock(subs->pcm_substream); 1649 else 1650 snd_pcm_period_elapsed(subs->pcm_substream); 1651 } 1652 return 0; 1653 } 1654 1655 /* 1656 * process after playback data complete 1657 * - decrease the delay count again 1658 */ 1659 static void retire_playback_urb(struct snd_usb_substream *subs, 1660 struct urb *urb) 1661 { 1662 unsigned long flags; 1663 struct snd_urb_ctx *ctx = urb->context; 1664 bool period_elapsed = false; 1665 1666 spin_lock_irqsave(&subs->lock, flags); 1667 if (ctx->queued) { 1668 if (subs->inflight_bytes >= ctx->queued) 1669 subs->inflight_bytes -= ctx->queued; 1670 else 1671 subs->inflight_bytes = 0; 1672 } 1673 1674 subs->last_frame_number = usb_get_current_frame_number(subs->dev); 1675 if (subs->running) { 1676 period_elapsed = subs->period_elapsed_pending; 1677 subs->period_elapsed_pending = 0; 1678 } 1679 spin_unlock_irqrestore(&subs->lock, flags); 1680 if (period_elapsed) 1681 snd_pcm_period_elapsed(subs->pcm_substream); 1682 } 1683 1684 /* PCM ack callback for the playback stream; 1685 * this plays a role only when the stream is running in low-latency mode. 1686 */ 1687 static int snd_usb_pcm_playback_ack(struct snd_pcm_substream *substream) 1688 { 1689 struct snd_usb_substream *subs = substream->runtime->private_data; 1690 struct snd_usb_endpoint *ep; 1691 1692 if (!subs->lowlatency_playback || !subs->running) 1693 return 0; 1694 ep = subs->data_endpoint; 1695 if (!ep) 1696 return 0; 1697 /* When no more in-flight URBs available, try to process the pending 1698 * outputs here 1699 */ 1700 if (!ep->active_mask) 1701 return snd_usb_queue_pending_output_urbs(ep, true); 1702 return 0; 1703 } 1704 1705 static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, 1706 int cmd) 1707 { 1708 struct snd_usb_substream *subs = substream->runtime->private_data; 1709 int err; 1710 1711 switch (cmd) { 1712 case SNDRV_PCM_TRIGGER_START: 1713 subs->trigger_tstamp_pending_update = true; 1714 fallthrough; 1715 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 1716 snd_usb_endpoint_set_callback(subs->data_endpoint, 1717 prepare_playback_urb, 1718 retire_playback_urb, 1719 subs); 1720 if (subs->lowlatency_playback && 1721 cmd == SNDRV_PCM_TRIGGER_START) { 1722 if (in_free_wheeling_mode(substream->runtime)) 1723 subs->lowlatency_playback = false; 1724 err = start_endpoints(subs); 1725 if (err < 0) { 1726 snd_usb_endpoint_set_callback(subs->data_endpoint, 1727 NULL, NULL, NULL); 1728 return err; 1729 } 1730 } 1731 subs->running = 1; 1732 dev_dbg(&subs->dev->dev, "%d:%d Start Playback PCM\n", 1733 subs->cur_audiofmt->iface, 1734 subs->cur_audiofmt->altsetting); 1735 return 0; 1736 case SNDRV_PCM_TRIGGER_SUSPEND: 1737 case SNDRV_PCM_TRIGGER_STOP: 1738 stop_endpoints(subs, substream->runtime->state == SNDRV_PCM_STATE_DRAINING); 1739 snd_usb_endpoint_set_callback(subs->data_endpoint, 1740 NULL, NULL, NULL); 1741 subs->running = 0; 1742 dev_dbg(&subs->dev->dev, "%d:%d Stop Playback PCM\n", 1743 subs->cur_audiofmt->iface, 1744 subs->cur_audiofmt->altsetting); 1745 return 0; 1746 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 1747 /* keep retire_data_urb for delay calculation */ 1748 snd_usb_endpoint_set_callback(subs->data_endpoint, 1749 NULL, 1750 retire_playback_urb, 1751 subs); 1752 subs->running = 0; 1753 dev_dbg(&subs->dev->dev, "%d:%d Pause Playback PCM\n", 1754 subs->cur_audiofmt->iface, 1755 subs->cur_audiofmt->altsetting); 1756 return 0; 1757 } 1758 1759 return -EINVAL; 1760 } 1761 1762 static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, 1763 int cmd) 1764 { 1765 int err; 1766 struct snd_usb_substream *subs = substream->runtime->private_data; 1767 1768 switch (cmd) { 1769 case SNDRV_PCM_TRIGGER_START: 1770 err = start_endpoints(subs); 1771 if (err < 0) 1772 return err; 1773 fallthrough; 1774 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 1775 snd_usb_endpoint_set_callback(subs->data_endpoint, 1776 NULL, retire_capture_urb, 1777 subs); 1778 subs->last_frame_number = usb_get_current_frame_number(subs->dev); 1779 subs->running = 1; 1780 dev_dbg(&subs->dev->dev, "%d:%d Start Capture PCM\n", 1781 subs->cur_audiofmt->iface, 1782 subs->cur_audiofmt->altsetting); 1783 return 0; 1784 case SNDRV_PCM_TRIGGER_SUSPEND: 1785 case SNDRV_PCM_TRIGGER_STOP: 1786 stop_endpoints(subs, false); 1787 fallthrough; 1788 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 1789 snd_usb_endpoint_set_callback(subs->data_endpoint, 1790 NULL, NULL, NULL); 1791 subs->running = 0; 1792 dev_dbg(&subs->dev->dev, "%d:%d Stop Capture PCM\n", 1793 subs->cur_audiofmt->iface, 1794 subs->cur_audiofmt->altsetting); 1795 return 0; 1796 } 1797 1798 return -EINVAL; 1799 } 1800 1801 static const struct snd_pcm_ops snd_usb_playback_ops = { 1802 .open = snd_usb_pcm_open, 1803 .close = snd_usb_pcm_close, 1804 .hw_params = snd_usb_pcm_hw_params, 1805 .hw_free = snd_usb_pcm_hw_free, 1806 .prepare = snd_usb_pcm_prepare, 1807 .trigger = snd_usb_substream_playback_trigger, 1808 .sync_stop = snd_usb_pcm_sync_stop, 1809 .pointer = snd_usb_pcm_pointer, 1810 .ack = snd_usb_pcm_playback_ack, 1811 }; 1812 1813 static const struct snd_pcm_ops snd_usb_capture_ops = { 1814 .open = snd_usb_pcm_open, 1815 .close = snd_usb_pcm_close, 1816 .hw_params = snd_usb_pcm_hw_params, 1817 .hw_free = snd_usb_pcm_hw_free, 1818 .prepare = snd_usb_pcm_prepare, 1819 .trigger = snd_usb_substream_capture_trigger, 1820 .sync_stop = snd_usb_pcm_sync_stop, 1821 .pointer = snd_usb_pcm_pointer, 1822 }; 1823 1824 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream) 1825 { 1826 const struct snd_pcm_ops *ops; 1827 1828 ops = stream == SNDRV_PCM_STREAM_PLAYBACK ? 1829 &snd_usb_playback_ops : &snd_usb_capture_ops; 1830 snd_pcm_set_ops(pcm, stream, ops); 1831 } 1832 1833 void snd_usb_preallocate_buffer(struct snd_usb_substream *subs) 1834 { 1835 struct snd_pcm *pcm = subs->stream->pcm; 1836 struct snd_pcm_substream *s = pcm->streams[subs->direction].substream; 1837 struct device *dev = subs->dev->bus->sysdev; 1838 1839 if (snd_usb_use_vmalloc) 1840 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_VMALLOC, 1841 NULL, 0, 0); 1842 else 1843 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_DEV_SG, 1844 dev, 64*1024, 512*1024); 1845 } 1846