1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 */ 4 5 #include <linux/gfp.h> 6 #include <linux/init.h> 7 #include <linux/ratelimit.h> 8 #include <linux/usb.h> 9 #include <linux/usb/audio.h> 10 #include <linux/slab.h> 11 12 #include <sound/core.h> 13 #include <sound/pcm.h> 14 #include <sound/pcm_params.h> 15 16 #include "usbaudio.h" 17 #include "helper.h" 18 #include "card.h" 19 #include "endpoint.h" 20 #include "pcm.h" 21 #include "quirks.h" 22 23 #define EP_FLAG_RUNNING 1 24 #define EP_FLAG_STOPPING 2 25 26 /* 27 * snd_usb_endpoint is a model that abstracts everything related to an 28 * USB endpoint and its streaming. 29 * 30 * There are functions to activate and deactivate the streaming URBs and 31 * optional callbacks to let the pcm logic handle the actual content of the 32 * packets for playback and record. Thus, the bus streaming and the audio 33 * handlers are fully decoupled. 34 * 35 * There are two different types of endpoints in audio applications. 36 * 37 * SND_USB_ENDPOINT_TYPE_DATA handles full audio data payload for both 38 * inbound and outbound traffic. 39 * 40 * SND_USB_ENDPOINT_TYPE_SYNC endpoints are for inbound traffic only and 41 * expect the payload to carry Q10.14 / Q16.16 formatted sync information 42 * (3 or 4 bytes). 43 * 44 * Each endpoint has to be configured prior to being used by calling 45 * snd_usb_endpoint_set_params(). 46 * 47 * The model incorporates a reference counting, so that multiple users 48 * can call snd_usb_endpoint_start() and snd_usb_endpoint_stop(), and 49 * only the first user will effectively start the URBs, and only the last 50 * one to stop it will tear the URBs down again. 51 */ 52 53 /* 54 * convert a sampling rate into our full speed format (fs/1000 in Q16.16) 55 * this will overflow at approx 524 kHz 56 */ 57 static inline unsigned get_usb_full_speed_rate(unsigned int rate) 58 { 59 return ((rate << 13) + 62) / 125; 60 } 61 62 /* 63 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16) 64 * this will overflow at approx 4 MHz 65 */ 66 static inline unsigned get_usb_high_speed_rate(unsigned int rate) 67 { 68 return ((rate << 10) + 62) / 125; 69 } 70 71 /* 72 * release a urb data 73 */ 74 static void release_urb_ctx(struct snd_urb_ctx *u) 75 { 76 if (u->buffer_size) 77 usb_free_coherent(u->ep->chip->dev, u->buffer_size, 78 u->urb->transfer_buffer, 79 u->urb->transfer_dma); 80 usb_free_urb(u->urb); 81 u->urb = NULL; 82 } 83 84 static const char *usb_error_string(int err) 85 { 86 switch (err) { 87 case -ENODEV: 88 return "no device"; 89 case -ENOENT: 90 return "endpoint not enabled"; 91 case -EPIPE: 92 return "endpoint stalled"; 93 case -ENOSPC: 94 return "not enough bandwidth"; 95 case -ESHUTDOWN: 96 return "device disabled"; 97 case -EHOSTUNREACH: 98 return "device suspended"; 99 case -EINVAL: 100 case -EAGAIN: 101 case -EFBIG: 102 case -EMSGSIZE: 103 return "internal error"; 104 default: 105 return "unknown error"; 106 } 107 } 108 109 /** 110 * snd_usb_endpoint_implicit_feedback_sink: Report endpoint usage type 111 * 112 * @ep: The snd_usb_endpoint 113 * 114 * Determine whether an endpoint is driven by an implicit feedback 115 * data endpoint source. 116 */ 117 int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep) 118 { 119 return ep->sync_master && 120 ep->sync_master->type == SND_USB_ENDPOINT_TYPE_DATA && 121 ep->type == SND_USB_ENDPOINT_TYPE_DATA && 122 usb_pipeout(ep->pipe); 123 } 124 125 /* 126 * For streaming based on information derived from sync endpoints, 127 * prepare_outbound_urb_sizes() will call slave_next_packet_size() to 128 * determine the number of samples to be sent in the next packet. 129 * 130 * For implicit feedback, slave_next_packet_size() is unused. 131 */ 132 int snd_usb_endpoint_slave_next_packet_size(struct snd_usb_endpoint *ep) 133 { 134 unsigned long flags; 135 int ret; 136 137 if (ep->fill_max) 138 return ep->maxframesize; 139 140 spin_lock_irqsave(&ep->lock, flags); 141 ep->phase = (ep->phase & 0xffff) 142 + (ep->freqm << ep->datainterval); 143 ret = min(ep->phase >> 16, ep->maxframesize); 144 spin_unlock_irqrestore(&ep->lock, flags); 145 146 return ret; 147 } 148 149 /* 150 * For adaptive and synchronous endpoints, prepare_outbound_urb_sizes() 151 * will call next_packet_size() to determine the number of samples to be 152 * sent in the next packet. 153 */ 154 int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep) 155 { 156 int ret; 157 158 if (ep->fill_max) 159 return ep->maxframesize; 160 161 ep->sample_accum += ep->sample_rem; 162 if (ep->sample_accum >= ep->pps) { 163 ep->sample_accum -= ep->pps; 164 ret = ep->packsize[1]; 165 } else { 166 ret = ep->packsize[0]; 167 } 168 169 return ret; 170 } 171 172 static void call_retire_callback(struct snd_usb_endpoint *ep, 173 struct urb *urb) 174 { 175 struct snd_usb_substream *data_subs; 176 177 data_subs = READ_ONCE(ep->data_subs); 178 if (data_subs && ep->retire_data_urb) 179 ep->retire_data_urb(data_subs, urb); 180 } 181 182 static void retire_outbound_urb(struct snd_usb_endpoint *ep, 183 struct snd_urb_ctx *urb_ctx) 184 { 185 call_retire_callback(ep, urb_ctx->urb); 186 } 187 188 static void retire_inbound_urb(struct snd_usb_endpoint *ep, 189 struct snd_urb_ctx *urb_ctx) 190 { 191 struct urb *urb = urb_ctx->urb; 192 193 if (unlikely(ep->skip_packets > 0)) { 194 ep->skip_packets--; 195 return; 196 } 197 198 if (ep->sync_slave) 199 snd_usb_handle_sync_urb(ep->sync_slave, ep, urb); 200 201 call_retire_callback(ep, urb); 202 } 203 204 static void prepare_silent_urb(struct snd_usb_endpoint *ep, 205 struct snd_urb_ctx *ctx) 206 { 207 struct urb *urb = ctx->urb; 208 unsigned int offs = 0; 209 unsigned int extra = 0; 210 __le32 packet_length; 211 int i; 212 213 /* For tx_length_quirk, put packet length at start of packet */ 214 if (ep->chip->tx_length_quirk) 215 extra = sizeof(packet_length); 216 217 for (i = 0; i < ctx->packets; ++i) { 218 unsigned int offset; 219 unsigned int length; 220 int counts; 221 222 if (ctx->packet_size[i]) 223 counts = ctx->packet_size[i]; 224 else if (ep->sync_master) 225 counts = snd_usb_endpoint_slave_next_packet_size(ep); 226 else 227 counts = snd_usb_endpoint_next_packet_size(ep); 228 229 length = counts * ep->stride; /* number of silent bytes */ 230 offset = offs * ep->stride + extra * i; 231 urb->iso_frame_desc[i].offset = offset; 232 urb->iso_frame_desc[i].length = length + extra; 233 if (extra) { 234 packet_length = cpu_to_le32(length); 235 memcpy(urb->transfer_buffer + offset, 236 &packet_length, sizeof(packet_length)); 237 } 238 memset(urb->transfer_buffer + offset + extra, 239 ep->silence_value, length); 240 offs += counts; 241 } 242 243 urb->number_of_packets = ctx->packets; 244 urb->transfer_buffer_length = offs * ep->stride + ctx->packets * extra; 245 } 246 247 /* 248 * Prepare a PLAYBACK urb for submission to the bus. 249 */ 250 static void prepare_outbound_urb(struct snd_usb_endpoint *ep, 251 struct snd_urb_ctx *ctx) 252 { 253 struct urb *urb = ctx->urb; 254 unsigned char *cp = urb->transfer_buffer; 255 struct snd_usb_substream *data_subs; 256 257 urb->dev = ep->chip->dev; /* we need to set this at each time */ 258 259 switch (ep->type) { 260 case SND_USB_ENDPOINT_TYPE_DATA: 261 data_subs = READ_ONCE(ep->data_subs); 262 if (data_subs && ep->prepare_data_urb) 263 ep->prepare_data_urb(data_subs, urb); 264 else /* no data provider, so send silence */ 265 prepare_silent_urb(ep, ctx); 266 break; 267 268 case SND_USB_ENDPOINT_TYPE_SYNC: 269 if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) { 270 /* 271 * fill the length and offset of each urb descriptor. 272 * the fixed 12.13 frequency is passed as 16.16 through the pipe. 273 */ 274 urb->iso_frame_desc[0].length = 4; 275 urb->iso_frame_desc[0].offset = 0; 276 cp[0] = ep->freqn; 277 cp[1] = ep->freqn >> 8; 278 cp[2] = ep->freqn >> 16; 279 cp[3] = ep->freqn >> 24; 280 } else { 281 /* 282 * fill the length and offset of each urb descriptor. 283 * the fixed 10.14 frequency is passed through the pipe. 284 */ 285 urb->iso_frame_desc[0].length = 3; 286 urb->iso_frame_desc[0].offset = 0; 287 cp[0] = ep->freqn >> 2; 288 cp[1] = ep->freqn >> 10; 289 cp[2] = ep->freqn >> 18; 290 } 291 292 break; 293 } 294 } 295 296 /* 297 * Prepare a CAPTURE or SYNC urb for submission to the bus. 298 */ 299 static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep, 300 struct snd_urb_ctx *urb_ctx) 301 { 302 int i, offs; 303 struct urb *urb = urb_ctx->urb; 304 305 urb->dev = ep->chip->dev; /* we need to set this at each time */ 306 307 switch (ep->type) { 308 case SND_USB_ENDPOINT_TYPE_DATA: 309 offs = 0; 310 for (i = 0; i < urb_ctx->packets; i++) { 311 urb->iso_frame_desc[i].offset = offs; 312 urb->iso_frame_desc[i].length = ep->curpacksize; 313 offs += ep->curpacksize; 314 } 315 316 urb->transfer_buffer_length = offs; 317 urb->number_of_packets = urb_ctx->packets; 318 break; 319 320 case SND_USB_ENDPOINT_TYPE_SYNC: 321 urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize); 322 urb->iso_frame_desc[0].offset = 0; 323 break; 324 } 325 } 326 327 /* 328 * Send output urbs that have been prepared previously. URBs are dequeued 329 * from ep->ready_playback_urbs and in case there aren't any available 330 * or there are no packets that have been prepared, this function does 331 * nothing. 332 * 333 * The reason why the functionality of sending and preparing URBs is separated 334 * is that host controllers don't guarantee the order in which they return 335 * inbound and outbound packets to their submitters. 336 * 337 * This function is only used for implicit feedback endpoints. For endpoints 338 * driven by dedicated sync endpoints, URBs are immediately re-submitted 339 * from their completion handler. 340 */ 341 static void queue_pending_output_urbs(struct snd_usb_endpoint *ep) 342 { 343 while (test_bit(EP_FLAG_RUNNING, &ep->flags)) { 344 345 unsigned long flags; 346 struct snd_usb_packet_info *packet; 347 struct snd_urb_ctx *ctx = NULL; 348 int err, i; 349 350 spin_lock_irqsave(&ep->lock, flags); 351 if (ep->next_packet_read_pos != ep->next_packet_write_pos) { 352 packet = ep->next_packet + ep->next_packet_read_pos; 353 ep->next_packet_read_pos++; 354 ep->next_packet_read_pos %= MAX_URBS; 355 356 /* take URB out of FIFO */ 357 if (!list_empty(&ep->ready_playback_urbs)) { 358 ctx = list_first_entry(&ep->ready_playback_urbs, 359 struct snd_urb_ctx, ready_list); 360 list_del_init(&ctx->ready_list); 361 } 362 } 363 spin_unlock_irqrestore(&ep->lock, flags); 364 365 if (ctx == NULL) 366 return; 367 368 /* copy over the length information */ 369 for (i = 0; i < packet->packets; i++) 370 ctx->packet_size[i] = packet->packet_size[i]; 371 372 /* call the data handler to fill in playback data */ 373 prepare_outbound_urb(ep, ctx); 374 375 err = usb_submit_urb(ctx->urb, GFP_ATOMIC); 376 if (err < 0) 377 usb_audio_err(ep->chip, 378 "Unable to submit urb #%d: %d at %s\n", 379 ctx->index, err, __func__); 380 else 381 set_bit(ctx->index, &ep->active_mask); 382 } 383 } 384 385 /* 386 * complete callback for urbs 387 */ 388 static void snd_complete_urb(struct urb *urb) 389 { 390 struct snd_urb_ctx *ctx = urb->context; 391 struct snd_usb_endpoint *ep = ctx->ep; 392 struct snd_usb_substream *data_subs; 393 unsigned long flags; 394 int err; 395 396 if (unlikely(urb->status == -ENOENT || /* unlinked */ 397 urb->status == -ENODEV || /* device removed */ 398 urb->status == -ECONNRESET || /* unlinked */ 399 urb->status == -ESHUTDOWN)) /* device disabled */ 400 goto exit_clear; 401 /* device disconnected */ 402 if (unlikely(atomic_read(&ep->chip->shutdown))) 403 goto exit_clear; 404 405 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags))) 406 goto exit_clear; 407 408 if (usb_pipeout(ep->pipe)) { 409 retire_outbound_urb(ep, ctx); 410 /* can be stopped during retire callback */ 411 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags))) 412 goto exit_clear; 413 414 if (snd_usb_endpoint_implicit_feedback_sink(ep)) { 415 spin_lock_irqsave(&ep->lock, flags); 416 list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs); 417 spin_unlock_irqrestore(&ep->lock, flags); 418 queue_pending_output_urbs(ep); 419 420 goto exit_clear; 421 } 422 423 prepare_outbound_urb(ep, ctx); 424 /* can be stopped during prepare callback */ 425 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags))) 426 goto exit_clear; 427 } else { 428 retire_inbound_urb(ep, ctx); 429 /* can be stopped during retire callback */ 430 if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags))) 431 goto exit_clear; 432 433 prepare_inbound_urb(ep, ctx); 434 } 435 436 err = usb_submit_urb(urb, GFP_ATOMIC); 437 if (err == 0) 438 return; 439 440 usb_audio_err(ep->chip, "cannot submit urb (err = %d)\n", err); 441 data_subs = READ_ONCE(ep->data_subs); 442 if (data_subs && data_subs->pcm_substream) 443 snd_pcm_stop_xrun(data_subs->pcm_substream); 444 445 exit_clear: 446 clear_bit(ctx->index, &ep->active_mask); 447 } 448 449 /* 450 * Get the existing endpoint object corresponding EP 451 * Returns NULL if not present. 452 */ 453 struct snd_usb_endpoint * 454 snd_usb_get_endpoint(struct snd_usb_audio *chip, int ep_num) 455 { 456 struct snd_usb_endpoint *ep; 457 458 list_for_each_entry(ep, &chip->ep_list, list) { 459 if (ep->ep_num == ep_num) 460 return ep; 461 } 462 463 return NULL; 464 } 465 466 #define ep_type_name(type) \ 467 (type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync") 468 469 /** 470 * snd_usb_add_endpoint: Add an endpoint to an USB audio chip 471 * 472 * @chip: The chip 473 * @ep_num: The number of the endpoint to use 474 * @type: SND_USB_ENDPOINT_TYPE_DATA or SND_USB_ENDPOINT_TYPE_SYNC 475 * 476 * If the requested endpoint has not been added to the given chip before, 477 * a new instance is created. 478 * 479 * Returns zero on success or a negative error code. 480 * 481 * New endpoints will be added to chip->ep_list and must be freed by 482 * calling snd_usb_endpoint_free(). 483 * 484 * For SND_USB_ENDPOINT_TYPE_SYNC, the caller needs to guarantee that 485 * bNumEndpoints > 1 beforehand. 486 */ 487 int snd_usb_add_endpoint(struct snd_usb_audio *chip, int ep_num, int type) 488 { 489 struct snd_usb_endpoint *ep; 490 bool is_playback; 491 492 ep = snd_usb_get_endpoint(chip, ep_num); 493 if (ep) 494 return 0; 495 496 usb_audio_dbg(chip, "Creating new %s endpoint #%x\n", 497 ep_type_name(type), 498 ep_num); 499 ep = kzalloc(sizeof(*ep), GFP_KERNEL); 500 if (!ep) 501 return -ENOMEM; 502 503 ep->chip = chip; 504 spin_lock_init(&ep->lock); 505 ep->type = type; 506 ep->ep_num = ep_num; 507 INIT_LIST_HEAD(&ep->ready_playback_urbs); 508 509 is_playback = ((ep_num & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT); 510 ep_num &= USB_ENDPOINT_NUMBER_MASK; 511 if (is_playback) 512 ep->pipe = usb_sndisocpipe(chip->dev, ep_num); 513 else 514 ep->pipe = usb_rcvisocpipe(chip->dev, ep_num); 515 516 list_add_tail(&ep->list, &chip->ep_list); 517 return 0; 518 } 519 520 /* Set up syncinterval and maxsyncsize for a sync EP */ 521 void snd_usb_endpoint_set_syncinterval(struct snd_usb_audio *chip, 522 struct snd_usb_endpoint *ep, 523 struct usb_host_interface *alts) 524 { 525 struct usb_endpoint_descriptor *desc = get_endpoint(alts, 1); 526 527 if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC) { 528 if (desc->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && 529 desc->bRefresh >= 1 && desc->bRefresh <= 9) 530 ep->syncinterval = desc->bRefresh; 531 else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) 532 ep->syncinterval = 1; 533 else if (desc->bInterval >= 1 && desc->bInterval <= 16) 534 ep->syncinterval = desc->bInterval - 1; 535 else 536 ep->syncinterval = 3; 537 538 ep->syncmaxsize = le16_to_cpu(desc->wMaxPacketSize); 539 } 540 } 541 542 /* 543 * Set data endpoint callbacks and the assigned data stream 544 * 545 * Called at PCM trigger and cleanups. 546 * Pass NULL to deactivate each callback. 547 */ 548 void snd_usb_endpoint_set_callback(struct snd_usb_endpoint *ep, 549 void (*prepare)(struct snd_usb_substream *subs, 550 struct urb *urb), 551 void (*retire)(struct snd_usb_substream *subs, 552 struct urb *urb), 553 struct snd_usb_substream *data_subs) 554 { 555 ep->prepare_data_urb = prepare; 556 ep->retire_data_urb = retire; 557 WRITE_ONCE(ep->data_subs, data_subs); 558 } 559 560 /* 561 * wait until all urbs are processed. 562 */ 563 static int wait_clear_urbs(struct snd_usb_endpoint *ep) 564 { 565 unsigned long end_time = jiffies + msecs_to_jiffies(1000); 566 int alive; 567 568 do { 569 alive = bitmap_weight(&ep->active_mask, ep->nurbs); 570 if (!alive) 571 break; 572 573 schedule_timeout_uninterruptible(1); 574 } while (time_before(jiffies, end_time)); 575 576 if (alive) 577 usb_audio_err(ep->chip, 578 "timeout: still %d active urbs on EP #%x\n", 579 alive, ep->ep_num); 580 clear_bit(EP_FLAG_STOPPING, &ep->flags); 581 582 ep->sync_slave = NULL; 583 snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL); 584 585 return 0; 586 } 587 588 /* sync the pending stop operation; 589 * this function itself doesn't trigger the stop operation 590 */ 591 void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep) 592 { 593 if (ep && test_bit(EP_FLAG_STOPPING, &ep->flags)) 594 wait_clear_urbs(ep); 595 } 596 597 /* 598 * unlink active urbs. 599 */ 600 static int deactivate_urbs(struct snd_usb_endpoint *ep, bool force) 601 { 602 unsigned int i; 603 604 if (!force && atomic_read(&ep->chip->shutdown)) /* to be sure... */ 605 return -EBADFD; 606 607 clear_bit(EP_FLAG_RUNNING, &ep->flags); 608 609 INIT_LIST_HEAD(&ep->ready_playback_urbs); 610 ep->next_packet_read_pos = 0; 611 ep->next_packet_write_pos = 0; 612 613 for (i = 0; i < ep->nurbs; i++) { 614 if (test_bit(i, &ep->active_mask)) { 615 if (!test_and_set_bit(i, &ep->unlink_mask)) { 616 struct urb *u = ep->urb[i].urb; 617 usb_unlink_urb(u); 618 } 619 } 620 } 621 622 return 0; 623 } 624 625 /* 626 * release an endpoint's urbs 627 */ 628 static void release_urbs(struct snd_usb_endpoint *ep, int force) 629 { 630 int i; 631 632 /* route incoming urbs to nirvana */ 633 snd_usb_endpoint_set_callback(ep, NULL, NULL, NULL); 634 635 /* stop urbs */ 636 deactivate_urbs(ep, force); 637 wait_clear_urbs(ep); 638 639 for (i = 0; i < ep->nurbs; i++) 640 release_urb_ctx(&ep->urb[i]); 641 642 usb_free_coherent(ep->chip->dev, SYNC_URBS * 4, 643 ep->syncbuf, ep->sync_dma); 644 645 ep->syncbuf = NULL; 646 ep->nurbs = 0; 647 } 648 649 /* 650 * Check data endpoint for format differences 651 */ 652 static bool check_ep_params(struct snd_usb_endpoint *ep, 653 snd_pcm_format_t pcm_format, 654 unsigned int channels, 655 unsigned int period_bytes, 656 unsigned int frames_per_period, 657 unsigned int periods_per_buffer, 658 unsigned int rate, 659 struct audioformat *fmt, 660 struct snd_usb_endpoint *sync_ep) 661 { 662 unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb; 663 unsigned int max_packs_per_period, urbs_per_period, urb_packs; 664 unsigned int max_urbs; 665 int frame_bits = snd_pcm_format_physical_width(pcm_format) * channels; 666 int tx_length_quirk = (ep->chip->tx_length_quirk && 667 usb_pipeout(ep->pipe)); 668 bool ret = 1; 669 670 /* matching with the saved parameters? */ 671 if (ep->cur_rate == rate && 672 ep->cur_format == pcm_format && 673 ep->cur_channels == channels && 674 ep->cur_period_frames == frames_per_period && 675 ep->cur_buffer_periods == periods_per_buffer) 676 return true; 677 678 if (pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) { 679 /* 680 * When operating in DSD DOP mode, the size of a sample frame 681 * in hardware differs from the actual physical format width 682 * because we need to make room for the DOP markers. 683 */ 684 frame_bits += channels << 3; 685 } 686 687 ret = ret && (ep->datainterval == fmt->datainterval); 688 ret = ret && (ep->stride == frame_bits >> 3); 689 690 switch (pcm_format) { 691 case SNDRV_PCM_FORMAT_U8: 692 ret = ret && (ep->silence_value == 0x80); 693 break; 694 case SNDRV_PCM_FORMAT_DSD_U8: 695 case SNDRV_PCM_FORMAT_DSD_U16_LE: 696 case SNDRV_PCM_FORMAT_DSD_U32_LE: 697 case SNDRV_PCM_FORMAT_DSD_U16_BE: 698 case SNDRV_PCM_FORMAT_DSD_U32_BE: 699 ret = ret && (ep->silence_value == 0x69); 700 break; 701 default: 702 ret = ret && (ep->silence_value == 0); 703 } 704 705 /* assume max. frequency is 50% higher than nominal */ 706 ret = ret && (ep->freqmax == ep->freqn + (ep->freqn >> 1)); 707 /* Round up freqmax to nearest integer in order to calculate maximum 708 * packet size, which must represent a whole number of frames. 709 * This is accomplished by adding 0x0.ffff before converting the 710 * Q16.16 format into integer. 711 * In order to accurately calculate the maximum packet size when 712 * the data interval is more than 1 (i.e. ep->datainterval > 0), 713 * multiply by the data interval prior to rounding. For instance, 714 * a freqmax of 41 kHz will result in a max packet size of 6 (5.125) 715 * frames with a data interval of 1, but 11 (10.25) frames with a 716 * data interval of 2. 717 * (ep->freqmax << ep->datainterval overflows at 8.192 MHz for the 718 * maximum datainterval value of 3, at USB full speed, higher for 719 * USB high speed, noting that ep->freqmax is in units of 720 * frames per packet in Q16.16 format.) 721 */ 722 maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) * 723 (frame_bits >> 3); 724 if (tx_length_quirk) 725 maxsize += sizeof(__le32); /* Space for length descriptor */ 726 /* but wMaxPacketSize might reduce this */ 727 if (ep->maxpacksize && ep->maxpacksize < maxsize) { 728 /* whatever fits into a max. size packet */ 729 unsigned int data_maxsize = maxsize = ep->maxpacksize; 730 731 if (tx_length_quirk) 732 /* Need to remove the length descriptor to calc freq */ 733 data_maxsize -= sizeof(__le32); 734 ret = ret && (ep->freqmax == (data_maxsize / (frame_bits >> 3)) 735 << (16 - ep->datainterval)); 736 } 737 738 if (ep->fill_max) 739 ret = ret && (ep->curpacksize == ep->maxpacksize); 740 else 741 ret = ret && (ep->curpacksize == maxsize); 742 743 if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL) { 744 packs_per_ms = 8 >> ep->datainterval; 745 max_packs_per_urb = MAX_PACKS_HS; 746 } else { 747 packs_per_ms = 1; 748 max_packs_per_urb = MAX_PACKS; 749 } 750 if (sync_ep && !snd_usb_endpoint_implicit_feedback_sink(ep)) 751 max_packs_per_urb = min(max_packs_per_urb, 752 1U << sync_ep->syncinterval); 753 max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval); 754 755 /* 756 * Capture endpoints need to use small URBs because there's no way 757 * to tell in advance where the next period will end, and we don't 758 * want the next URB to complete much after the period ends. 759 * 760 * Playback endpoints with implicit sync much use the same parameters 761 * as their corresponding capture endpoint. 762 */ 763 if (usb_pipein(ep->pipe) || 764 snd_usb_endpoint_implicit_feedback_sink(ep)) { 765 766 urb_packs = packs_per_ms; 767 /* 768 * Wireless devices can poll at a max rate of once per 4ms. 769 * For dataintervals less than 5, increase the packet count to 770 * allow the host controller to use bursting to fill in the 771 * gaps. 772 */ 773 if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_WIRELESS) { 774 int interval = ep->datainterval; 775 776 while (interval < 5) { 777 urb_packs <<= 1; 778 ++interval; 779 } 780 } 781 /* make capture URBs <= 1 ms and smaller than a period */ 782 urb_packs = min(max_packs_per_urb, urb_packs); 783 while (urb_packs > 1 && urb_packs * maxsize >= period_bytes) 784 urb_packs >>= 1; 785 ret = ret && (ep->nurbs == MAX_URBS); 786 787 /* 788 * Playback endpoints without implicit sync are adjusted so that 789 * a period fits as evenly as possible in the smallest number of 790 * URBs. The total number of URBs is adjusted to the size of the 791 * ALSA buffer, subject to the MAX_URBS and MAX_QUEUE limits. 792 */ 793 } else { 794 /* determine how small a packet can be */ 795 minsize = (ep->freqn >> (16 - ep->datainterval)) * 796 (frame_bits >> 3); 797 /* with sync from device, assume it can be 12% lower */ 798 if (sync_ep) 799 minsize -= minsize >> 3; 800 minsize = max(minsize, 1u); 801 802 /* how many packets will contain an entire ALSA period? */ 803 max_packs_per_period = DIV_ROUND_UP(period_bytes, minsize); 804 805 /* how many URBs will contain a period? */ 806 urbs_per_period = DIV_ROUND_UP(max_packs_per_period, 807 max_packs_per_urb); 808 /* how many packets are needed in each URB? */ 809 urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period); 810 811 /* limit the number of frames in a single URB */ 812 ret = ret && (ep->max_urb_frames == 813 DIV_ROUND_UP(frames_per_period, urbs_per_period)); 814 815 /* try to use enough URBs to contain an entire ALSA buffer */ 816 max_urbs = min((unsigned) MAX_URBS, 817 MAX_QUEUE * packs_per_ms / urb_packs); 818 ret = ret && (ep->nurbs == min(max_urbs, 819 urbs_per_period * periods_per_buffer)); 820 } 821 822 ret = ret && (ep->datainterval == fmt->datainterval); 823 ret = ret && (ep->maxpacksize == fmt->maxpacksize); 824 ret = ret && 825 (ep->fill_max == !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)); 826 827 return ret; 828 } 829 830 /* 831 * configure a data endpoint 832 */ 833 static int data_ep_set_params(struct snd_usb_endpoint *ep, 834 snd_pcm_format_t pcm_format, 835 unsigned int channels, 836 unsigned int period_bytes, 837 unsigned int frames_per_period, 838 unsigned int periods_per_buffer, 839 struct audioformat *fmt, 840 struct snd_usb_endpoint *sync_ep) 841 { 842 unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb; 843 unsigned int max_packs_per_period, urbs_per_period, urb_packs; 844 unsigned int max_urbs, i; 845 int frame_bits = snd_pcm_format_physical_width(pcm_format) * channels; 846 int tx_length_quirk = (ep->chip->tx_length_quirk && 847 usb_pipeout(ep->pipe)); 848 849 if (pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) { 850 /* 851 * When operating in DSD DOP mode, the size of a sample frame 852 * in hardware differs from the actual physical format width 853 * because we need to make room for the DOP markers. 854 */ 855 frame_bits += channels << 3; 856 } 857 858 ep->datainterval = fmt->datainterval; 859 ep->stride = frame_bits >> 3; 860 861 switch (pcm_format) { 862 case SNDRV_PCM_FORMAT_U8: 863 ep->silence_value = 0x80; 864 break; 865 case SNDRV_PCM_FORMAT_DSD_U8: 866 case SNDRV_PCM_FORMAT_DSD_U16_LE: 867 case SNDRV_PCM_FORMAT_DSD_U32_LE: 868 case SNDRV_PCM_FORMAT_DSD_U16_BE: 869 case SNDRV_PCM_FORMAT_DSD_U32_BE: 870 ep->silence_value = 0x69; 871 break; 872 default: 873 ep->silence_value = 0; 874 } 875 876 /* assume max. frequency is 50% higher than nominal */ 877 ep->freqmax = ep->freqn + (ep->freqn >> 1); 878 /* Round up freqmax to nearest integer in order to calculate maximum 879 * packet size, which must represent a whole number of frames. 880 * This is accomplished by adding 0x0.ffff before converting the 881 * Q16.16 format into integer. 882 * In order to accurately calculate the maximum packet size when 883 * the data interval is more than 1 (i.e. ep->datainterval > 0), 884 * multiply by the data interval prior to rounding. For instance, 885 * a freqmax of 41 kHz will result in a max packet size of 6 (5.125) 886 * frames with a data interval of 1, but 11 (10.25) frames with a 887 * data interval of 2. 888 * (ep->freqmax << ep->datainterval overflows at 8.192 MHz for the 889 * maximum datainterval value of 3, at USB full speed, higher for 890 * USB high speed, noting that ep->freqmax is in units of 891 * frames per packet in Q16.16 format.) 892 */ 893 maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) * 894 (frame_bits >> 3); 895 if (tx_length_quirk) 896 maxsize += sizeof(__le32); /* Space for length descriptor */ 897 /* but wMaxPacketSize might reduce this */ 898 if (ep->maxpacksize && ep->maxpacksize < maxsize) { 899 /* whatever fits into a max. size packet */ 900 unsigned int data_maxsize = maxsize = ep->maxpacksize; 901 902 if (tx_length_quirk) 903 /* Need to remove the length descriptor to calc freq */ 904 data_maxsize -= sizeof(__le32); 905 ep->freqmax = (data_maxsize / (frame_bits >> 3)) 906 << (16 - ep->datainterval); 907 } 908 909 if (ep->fill_max) 910 ep->curpacksize = ep->maxpacksize; 911 else 912 ep->curpacksize = maxsize; 913 914 if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL) { 915 packs_per_ms = 8 >> ep->datainterval; 916 max_packs_per_urb = MAX_PACKS_HS; 917 } else { 918 packs_per_ms = 1; 919 max_packs_per_urb = MAX_PACKS; 920 } 921 if (sync_ep && !snd_usb_endpoint_implicit_feedback_sink(ep)) 922 max_packs_per_urb = min(max_packs_per_urb, 923 1U << sync_ep->syncinterval); 924 max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval); 925 926 /* 927 * Capture endpoints need to use small URBs because there's no way 928 * to tell in advance where the next period will end, and we don't 929 * want the next URB to complete much after the period ends. 930 * 931 * Playback endpoints with implicit sync much use the same parameters 932 * as their corresponding capture endpoint. 933 */ 934 if (usb_pipein(ep->pipe) || 935 ep->is_implicit_feedback || 936 snd_usb_endpoint_implicit_feedback_sink(ep)) { 937 938 urb_packs = packs_per_ms; 939 /* 940 * Wireless devices can poll at a max rate of once per 4ms. 941 * For dataintervals less than 5, increase the packet count to 942 * allow the host controller to use bursting to fill in the 943 * gaps. 944 */ 945 if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_WIRELESS) { 946 int interval = ep->datainterval; 947 while (interval < 5) { 948 urb_packs <<= 1; 949 ++interval; 950 } 951 } 952 /* make capture URBs <= 1 ms and smaller than a period */ 953 urb_packs = min(max_packs_per_urb, urb_packs); 954 while (urb_packs > 1 && urb_packs * maxsize >= period_bytes) 955 urb_packs >>= 1; 956 ep->nurbs = MAX_URBS; 957 958 /* 959 * Playback endpoints without implicit sync are adjusted so that 960 * a period fits as evenly as possible in the smallest number of 961 * URBs. The total number of URBs is adjusted to the size of the 962 * ALSA buffer, subject to the MAX_URBS and MAX_QUEUE limits. 963 */ 964 } else { 965 /* determine how small a packet can be */ 966 minsize = (ep->freqn >> (16 - ep->datainterval)) * 967 (frame_bits >> 3); 968 /* with sync from device, assume it can be 12% lower */ 969 if (sync_ep) 970 minsize -= minsize >> 3; 971 minsize = max(minsize, 1u); 972 973 /* how many packets will contain an entire ALSA period? */ 974 max_packs_per_period = DIV_ROUND_UP(period_bytes, minsize); 975 976 /* how many URBs will contain a period? */ 977 urbs_per_period = DIV_ROUND_UP(max_packs_per_period, 978 max_packs_per_urb); 979 /* how many packets are needed in each URB? */ 980 urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period); 981 982 /* limit the number of frames in a single URB */ 983 ep->max_urb_frames = DIV_ROUND_UP(frames_per_period, 984 urbs_per_period); 985 986 /* try to use enough URBs to contain an entire ALSA buffer */ 987 max_urbs = min((unsigned) MAX_URBS, 988 MAX_QUEUE * packs_per_ms / urb_packs); 989 ep->nurbs = min(max_urbs, urbs_per_period * periods_per_buffer); 990 } 991 992 /* allocate and initialize data urbs */ 993 for (i = 0; i < ep->nurbs; i++) { 994 struct snd_urb_ctx *u = &ep->urb[i]; 995 u->index = i; 996 u->ep = ep; 997 u->packets = urb_packs; 998 u->buffer_size = maxsize * u->packets; 999 1000 if (fmt->fmt_type == UAC_FORMAT_TYPE_II) 1001 u->packets++; /* for transfer delimiter */ 1002 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL); 1003 if (!u->urb) 1004 goto out_of_memory; 1005 1006 u->urb->transfer_buffer = 1007 usb_alloc_coherent(ep->chip->dev, u->buffer_size, 1008 GFP_KERNEL, &u->urb->transfer_dma); 1009 if (!u->urb->transfer_buffer) 1010 goto out_of_memory; 1011 u->urb->pipe = ep->pipe; 1012 u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 1013 u->urb->interval = 1 << ep->datainterval; 1014 u->urb->context = u; 1015 u->urb->complete = snd_complete_urb; 1016 INIT_LIST_HEAD(&u->ready_list); 1017 } 1018 1019 return 0; 1020 1021 out_of_memory: 1022 release_urbs(ep, 0); 1023 return -ENOMEM; 1024 } 1025 1026 /* 1027 * configure a sync endpoint 1028 */ 1029 static int sync_ep_set_params(struct snd_usb_endpoint *ep) 1030 { 1031 int i; 1032 1033 ep->syncbuf = usb_alloc_coherent(ep->chip->dev, SYNC_URBS * 4, 1034 GFP_KERNEL, &ep->sync_dma); 1035 if (!ep->syncbuf) 1036 return -ENOMEM; 1037 1038 for (i = 0; i < SYNC_URBS; i++) { 1039 struct snd_urb_ctx *u = &ep->urb[i]; 1040 u->index = i; 1041 u->ep = ep; 1042 u->packets = 1; 1043 u->urb = usb_alloc_urb(1, GFP_KERNEL); 1044 if (!u->urb) 1045 goto out_of_memory; 1046 u->urb->transfer_buffer = ep->syncbuf + i * 4; 1047 u->urb->transfer_dma = ep->sync_dma + i * 4; 1048 u->urb->transfer_buffer_length = 4; 1049 u->urb->pipe = ep->pipe; 1050 u->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; 1051 u->urb->number_of_packets = 1; 1052 u->urb->interval = 1 << ep->syncinterval; 1053 u->urb->context = u; 1054 u->urb->complete = snd_complete_urb; 1055 } 1056 1057 ep->nurbs = SYNC_URBS; 1058 1059 return 0; 1060 1061 out_of_memory: 1062 release_urbs(ep, 0); 1063 return -ENOMEM; 1064 } 1065 1066 /** 1067 * snd_usb_endpoint_set_params: configure an snd_usb_endpoint 1068 * 1069 * @ep: the snd_usb_endpoint to configure 1070 * @pcm_format: the audio fomat. 1071 * @channels: the number of audio channels. 1072 * @period_bytes: the number of bytes in one alsa period. 1073 * @period_frames: the number of frames in one alsa period. 1074 * @buffer_periods: the number of periods in one alsa buffer. 1075 * @rate: the frame rate. 1076 * @fmt: the USB audio format information 1077 * @sync_ep: the sync endpoint to use, if any 1078 * 1079 * Determine the number of URBs to be used on this endpoint. 1080 * An endpoint must be configured before it can be started. 1081 * An endpoint that is already running can not be reconfigured. 1082 */ 1083 int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep, 1084 snd_pcm_format_t pcm_format, 1085 unsigned int channels, 1086 unsigned int period_bytes, 1087 unsigned int period_frames, 1088 unsigned int buffer_periods, 1089 unsigned int rate, 1090 struct audioformat *fmt, 1091 struct snd_usb_endpoint *sync_ep) 1092 { 1093 int err; 1094 1095 usb_audio_dbg(ep->chip, 1096 "Setting params for ep %x (type %s, count %d), rate=%d, format=%s, channels=%d, period_bytes=%d, periods=%d\n", 1097 ep->ep_num, ep_type_name(ep->type), ep->use_count, 1098 rate, snd_pcm_format_name(pcm_format), channels, 1099 period_bytes, buffer_periods); 1100 1101 if (ep->use_count != 0) { 1102 bool check = ep->is_implicit_feedback && 1103 check_ep_params(ep, pcm_format, channels, period_bytes, 1104 period_frames, buffer_periods, rate, 1105 fmt, sync_ep); 1106 1107 if (!check) { 1108 usb_audio_warn(ep->chip, 1109 "Unable to change format on ep #%x: already in use\n", 1110 ep->ep_num); 1111 return -EBUSY; 1112 } 1113 1114 usb_audio_dbg(ep->chip, 1115 "Ep #%x already in use as implicit feedback but format not changed\n", 1116 ep->ep_num); 1117 return 0; 1118 } 1119 1120 /* release old buffers, if any */ 1121 release_urbs(ep, 0); 1122 1123 ep->datainterval = fmt->datainterval; 1124 ep->maxpacksize = fmt->maxpacksize; 1125 ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX); 1126 1127 if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL) { 1128 ep->freqn = get_usb_full_speed_rate(rate); 1129 ep->pps = 1000 >> ep->datainterval; 1130 } else { 1131 ep->freqn = get_usb_high_speed_rate(rate); 1132 ep->pps = 8000 >> ep->datainterval; 1133 } 1134 1135 ep->sample_rem = rate % ep->pps; 1136 ep->packsize[0] = rate / ep->pps; 1137 ep->packsize[1] = (rate + (ep->pps - 1)) / ep->pps; 1138 1139 /* calculate the frequency in 16.16 format */ 1140 ep->freqm = ep->freqn; 1141 ep->freqshift = INT_MIN; 1142 1143 ep->phase = 0; 1144 1145 switch (ep->type) { 1146 case SND_USB_ENDPOINT_TYPE_DATA: 1147 err = data_ep_set_params(ep, pcm_format, channels, 1148 period_bytes, period_frames, 1149 buffer_periods, fmt, sync_ep); 1150 break; 1151 case SND_USB_ENDPOINT_TYPE_SYNC: 1152 err = sync_ep_set_params(ep); 1153 break; 1154 default: 1155 err = -EINVAL; 1156 } 1157 1158 usb_audio_dbg(ep->chip, "Set up %d URBS, ret=%d\n", ep->nurbs, err); 1159 1160 if (err < 0) 1161 return err; 1162 1163 /* record the current set up in the endpoint (for implicit fb) */ 1164 spin_lock_irq(&ep->lock); 1165 ep->cur_rate = rate; 1166 ep->cur_channels = channels; 1167 ep->cur_format = pcm_format; 1168 ep->cur_period_frames = period_frames; 1169 ep->cur_period_bytes = period_bytes; 1170 ep->cur_buffer_periods = buffer_periods; 1171 spin_unlock_irq(&ep->lock); 1172 1173 return 0; 1174 } 1175 1176 /** 1177 * snd_usb_endpoint_start: start an snd_usb_endpoint 1178 * 1179 * @ep: the endpoint to start 1180 * 1181 * A call to this function will increment the use count of the endpoint. 1182 * In case it is not already running, the URBs for this endpoint will be 1183 * submitted. Otherwise, this function does nothing. 1184 * 1185 * Must be balanced to calls of snd_usb_endpoint_stop(). 1186 * 1187 * Returns an error if the URB submission failed, 0 in all other cases. 1188 */ 1189 int snd_usb_endpoint_start(struct snd_usb_endpoint *ep) 1190 { 1191 int err; 1192 unsigned int i; 1193 1194 if (atomic_read(&ep->chip->shutdown)) 1195 return -EBADFD; 1196 1197 usb_audio_dbg(ep->chip, "Starting %s EP 0x%x (count %d)\n", 1198 ep_type_name(ep->type), ep->ep_num, ep->use_count); 1199 1200 /* already running? */ 1201 if (++ep->use_count != 1) 1202 return 0; 1203 1204 /* just to be sure */ 1205 deactivate_urbs(ep, false); 1206 1207 ep->active_mask = 0; 1208 ep->unlink_mask = 0; 1209 ep->phase = 0; 1210 ep->sample_accum = 0; 1211 1212 snd_usb_endpoint_start_quirk(ep); 1213 1214 /* 1215 * If this endpoint has a data endpoint as implicit feedback source, 1216 * don't start the urbs here. Instead, mark them all as available, 1217 * wait for the record urbs to return and queue the playback urbs 1218 * from that context. 1219 */ 1220 1221 set_bit(EP_FLAG_RUNNING, &ep->flags); 1222 1223 if (snd_usb_endpoint_implicit_feedback_sink(ep)) { 1224 for (i = 0; i < ep->nurbs; i++) { 1225 struct snd_urb_ctx *ctx = ep->urb + i; 1226 list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs); 1227 } 1228 1229 return 0; 1230 } 1231 1232 for (i = 0; i < ep->nurbs; i++) { 1233 struct urb *urb = ep->urb[i].urb; 1234 1235 if (snd_BUG_ON(!urb)) 1236 goto __error; 1237 1238 if (usb_pipeout(ep->pipe)) { 1239 prepare_outbound_urb(ep, urb->context); 1240 } else { 1241 prepare_inbound_urb(ep, urb->context); 1242 } 1243 1244 err = usb_submit_urb(urb, GFP_ATOMIC); 1245 if (err < 0) { 1246 usb_audio_err(ep->chip, 1247 "cannot submit urb %d, error %d: %s\n", 1248 i, err, usb_error_string(err)); 1249 goto __error; 1250 } 1251 set_bit(i, &ep->active_mask); 1252 } 1253 1254 return 0; 1255 1256 __error: 1257 clear_bit(EP_FLAG_RUNNING, &ep->flags); 1258 ep->use_count--; 1259 deactivate_urbs(ep, false); 1260 return -EPIPE; 1261 } 1262 1263 /** 1264 * snd_usb_endpoint_stop: stop an snd_usb_endpoint 1265 * 1266 * @ep: the endpoint to stop (may be NULL) 1267 * 1268 * A call to this function will decrement the use count of the endpoint. 1269 * In case the last user has requested the endpoint stop, the URBs will 1270 * actually be deactivated. 1271 * 1272 * Must be balanced to calls of snd_usb_endpoint_start(). 1273 * 1274 * The caller needs to synchronize the pending stop operation via 1275 * snd_usb_endpoint_sync_pending_stop(). 1276 */ 1277 void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep) 1278 { 1279 if (!ep) 1280 return; 1281 1282 usb_audio_dbg(ep->chip, "Stopping %s EP 0x%x (count %d)\n", 1283 ep_type_name(ep->type), ep->ep_num, ep->use_count); 1284 1285 if (snd_BUG_ON(ep->use_count == 0)) 1286 return; 1287 1288 if (--ep->use_count == 0) { 1289 deactivate_urbs(ep, false); 1290 set_bit(EP_FLAG_STOPPING, &ep->flags); 1291 } 1292 } 1293 1294 /** 1295 * snd_usb_endpoint_deactivate: deactivate an snd_usb_endpoint 1296 * 1297 * @ep: the endpoint to deactivate 1298 * 1299 * If the endpoint is not currently in use, this functions will 1300 * deactivate its associated URBs. 1301 * 1302 * In case of any active users, this functions does nothing. 1303 */ 1304 void snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep) 1305 { 1306 if (!ep) 1307 return; 1308 1309 if (ep->use_count != 0) 1310 return; 1311 1312 deactivate_urbs(ep, true); 1313 wait_clear_urbs(ep); 1314 1315 /* clear the saved hw params */ 1316 spin_lock_irq(&ep->lock); 1317 ep->cur_rate = 0; 1318 spin_unlock_irq(&ep->lock); 1319 } 1320 1321 /** 1322 * snd_usb_endpoint_release: Tear down an snd_usb_endpoint 1323 * 1324 * @ep: the endpoint to release 1325 * 1326 * This function does not care for the endpoint's use count but will tear 1327 * down all the streaming URBs immediately. 1328 */ 1329 void snd_usb_endpoint_release(struct snd_usb_endpoint *ep) 1330 { 1331 release_urbs(ep, 1); 1332 } 1333 1334 /** 1335 * snd_usb_endpoint_free: Free the resources of an snd_usb_endpoint 1336 * 1337 * @ep: the endpoint to free 1338 * 1339 * This free all resources of the given ep. 1340 */ 1341 void snd_usb_endpoint_free(struct snd_usb_endpoint *ep) 1342 { 1343 kfree(ep); 1344 } 1345 1346 /** 1347 * snd_usb_handle_sync_urb: parse an USB sync packet 1348 * 1349 * @ep: the endpoint to handle the packet 1350 * @sender: the sending endpoint 1351 * @urb: the received packet 1352 * 1353 * This function is called from the context of an endpoint that received 1354 * the packet and is used to let another endpoint object handle the payload. 1355 */ 1356 void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep, 1357 struct snd_usb_endpoint *sender, 1358 const struct urb *urb) 1359 { 1360 int shift; 1361 unsigned int f; 1362 unsigned long flags; 1363 1364 snd_BUG_ON(ep == sender); 1365 1366 /* 1367 * In case the endpoint is operating in implicit feedback mode, prepare 1368 * a new outbound URB that has the same layout as the received packet 1369 * and add it to the list of pending urbs. queue_pending_output_urbs() 1370 * will take care of them later. 1371 */ 1372 if (snd_usb_endpoint_implicit_feedback_sink(ep) && 1373 ep->use_count != 0) { 1374 1375 /* implicit feedback case */ 1376 int i, bytes = 0; 1377 struct snd_urb_ctx *in_ctx; 1378 struct snd_usb_packet_info *out_packet; 1379 1380 in_ctx = urb->context; 1381 1382 /* Count overall packet size */ 1383 for (i = 0; i < in_ctx->packets; i++) 1384 if (urb->iso_frame_desc[i].status == 0) 1385 bytes += urb->iso_frame_desc[i].actual_length; 1386 1387 /* 1388 * skip empty packets. At least M-Audio's Fast Track Ultra stops 1389 * streaming once it received a 0-byte OUT URB 1390 */ 1391 if (bytes == 0) 1392 return; 1393 1394 spin_lock_irqsave(&ep->lock, flags); 1395 out_packet = ep->next_packet + ep->next_packet_write_pos; 1396 1397 /* 1398 * Iterate through the inbound packet and prepare the lengths 1399 * for the output packet. The OUT packet we are about to send 1400 * will have the same amount of payload bytes per stride as the 1401 * IN packet we just received. Since the actual size is scaled 1402 * by the stride, use the sender stride to calculate the length 1403 * in case the number of channels differ between the implicitly 1404 * fed-back endpoint and the synchronizing endpoint. 1405 */ 1406 1407 out_packet->packets = in_ctx->packets; 1408 for (i = 0; i < in_ctx->packets; i++) { 1409 if (urb->iso_frame_desc[i].status == 0) 1410 out_packet->packet_size[i] = 1411 urb->iso_frame_desc[i].actual_length / sender->stride; 1412 else 1413 out_packet->packet_size[i] = 0; 1414 } 1415 1416 ep->next_packet_write_pos++; 1417 ep->next_packet_write_pos %= MAX_URBS; 1418 spin_unlock_irqrestore(&ep->lock, flags); 1419 queue_pending_output_urbs(ep); 1420 1421 return; 1422 } 1423 1424 /* 1425 * process after playback sync complete 1426 * 1427 * Full speed devices report feedback values in 10.14 format as samples 1428 * per frame, high speed devices in 16.16 format as samples per 1429 * microframe. 1430 * 1431 * Because the Audio Class 1 spec was written before USB 2.0, many high 1432 * speed devices use a wrong interpretation, some others use an 1433 * entirely different format. 1434 * 1435 * Therefore, we cannot predict what format any particular device uses 1436 * and must detect it automatically. 1437 */ 1438 1439 if (urb->iso_frame_desc[0].status != 0 || 1440 urb->iso_frame_desc[0].actual_length < 3) 1441 return; 1442 1443 f = le32_to_cpup(urb->transfer_buffer); 1444 if (urb->iso_frame_desc[0].actual_length == 3) 1445 f &= 0x00ffffff; 1446 else 1447 f &= 0x0fffffff; 1448 1449 if (f == 0) 1450 return; 1451 1452 if (unlikely(sender->tenor_fb_quirk)) { 1453 /* 1454 * Devices based on Tenor 8802 chipsets (TEAC UD-H01 1455 * and others) sometimes change the feedback value 1456 * by +/- 0x1.0000. 1457 */ 1458 if (f < ep->freqn - 0x8000) 1459 f += 0xf000; 1460 else if (f > ep->freqn + 0x8000) 1461 f -= 0xf000; 1462 } else if (unlikely(ep->freqshift == INT_MIN)) { 1463 /* 1464 * The first time we see a feedback value, determine its format 1465 * by shifting it left or right until it matches the nominal 1466 * frequency value. This assumes that the feedback does not 1467 * differ from the nominal value more than +50% or -25%. 1468 */ 1469 shift = 0; 1470 while (f < ep->freqn - ep->freqn / 4) { 1471 f <<= 1; 1472 shift++; 1473 } 1474 while (f > ep->freqn + ep->freqn / 2) { 1475 f >>= 1; 1476 shift--; 1477 } 1478 ep->freqshift = shift; 1479 } else if (ep->freqshift >= 0) 1480 f <<= ep->freqshift; 1481 else 1482 f >>= -ep->freqshift; 1483 1484 if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) { 1485 /* 1486 * If the frequency looks valid, set it. 1487 * This value is referred to in prepare_playback_urb(). 1488 */ 1489 spin_lock_irqsave(&ep->lock, flags); 1490 ep->freqm = f; 1491 spin_unlock_irqrestore(&ep->lock, flags); 1492 } else { 1493 /* 1494 * Out of range; maybe the shift value is wrong. 1495 * Reset it so that we autodetect again the next time. 1496 */ 1497 ep->freqshift = INT_MIN; 1498 } 1499 } 1500 1501