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