Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ALSA: usb-audio: Refactor endpoint management

This is an intensive surgery for the endpoint and stream management
for achieving more robust and clean code.

The goals of this patch are:
- More clear endpoint resource changes
- The interface altsetting control in a single place
Below are brief description of the whole changes.

First off, most of the endpoint operations are moved into endpoint.c,
so that the snd_usb_endpoint object is only referred in other places.
The endpoint object is acquired and released via the new functions
snd_usb_endpoint_open() and snd_usb_endpoint_close() that are called
at PCM hw_params and hw_free callbacks, respectively. Those are
ref-counted and EPs can manage the multiple opens.

The open callback receives the audioformat and hw_params arguments,
and those are used for initializing the EP parameters; especially the
endpoint, interface and altset numbers are read from there, as well as
the PCM parameters like the format, rate and channels. Those are
stored in snd_usb_endpoint object. If it's the secondary open, the
function checks whether the given parameters are compatible with the
already opened EP setup, too.

The coupling with a sync EP (including an implicit feedback sync) is
done by the sole snd_usb_endpoint_set_sync() call.

The configuration of each endpoint is done in a single shot via
snd_usb_endpoint_configure() call. This is the place where most of
PCM configurations are done. A few flags and special handling in the
snd_usb_substream are dropped along with this change.

A significant difference wrt the configuration from the previous code
is the order of USB host interface setups. Now the interface is
always disabled at beginning and (re-)enabled at the last step of
snd_usb_endpoint_configure(), in order to be compliant with the
standard UAC2/3. For UAC1, the interface is set before the parameter
setups since there seem devices that require it (e.g. Yamaha THR10),
just like how it was done in the previous driver code.

The start/stop are almost same as before, also single-shots. The URB
callbacks need to be set via snd_usb_endpoint_set_callback() like the
previous code at the trigger phase, too.

Finally, the flag for the re-setup is set at the device suspend
through the full EP list, instead of PCM trigger. This catches the
overlooked cases where the PCM hasn't been running yet but the device
needs the full setup after resume.

Tested-by: Keith Milner <kamilner@superlative.org>
Tested-by: Dylan Robinson <dylan_robinson@motu.com>
Link: https://lore.kernel.org/r/20201123085347.19667-26-tiwai@suse.de
Signed-off-by: Takashi Iwai <tiwai@suse.de>

+623 -741
+4 -4
sound/usb/card.c
··· 980 980 { 981 981 struct snd_usb_audio *chip = usb_get_intfdata(intf); 982 982 struct snd_usb_stream *as; 983 + struct snd_usb_endpoint *ep; 983 984 struct usb_mixer_interface *mixer; 984 985 struct list_head *p; 985 986 ··· 988 987 return 0; 989 988 990 989 if (!chip->num_suspended_intf++) { 991 - list_for_each_entry(as, &chip->pcm_list, list) { 990 + list_for_each_entry(as, &chip->pcm_list, list) 992 991 snd_usb_pcm_suspend(as); 993 - as->substream[0].need_setup_ep = 994 - as->substream[1].need_setup_ep = true; 995 - } 992 + list_for_each_entry(ep, &chip->ep_list, list) 993 + snd_usb_endpoint_suspend(ep); 996 994 list_for_each(p, &chip->midi_list) 997 995 snd_usbmidi_suspend(p); 998 996 list_for_each_entry(mixer, &chip->mixer_list, list)
+7 -4
sound/usb/card.h
··· 26 26 unsigned char sync_ep; /* sync endpoint number */ 27 27 unsigned char sync_iface; /* sync EP interface */ 28 28 unsigned char sync_altsetting; /* sync EP alternate setting */ 29 + unsigned char sync_ep_idx; /* sync EP array index */ 29 30 unsigned char datainterval; /* log_2 of data packet interval */ 30 31 unsigned char protocol; /* UAC_VERSION_1/2/3 */ 31 32 unsigned int maxpacksize; /* max. packet size */ ··· 59 58 struct snd_usb_endpoint { 60 59 struct snd_usb_audio *chip; 61 60 61 + int opened; /* open refcount; protect with chip->mutex */ 62 62 int use_count; 63 63 int ep_num; /* the referenced endpoint number */ 64 64 int type; /* SND_USB_ENDPOINT_TYPE_* */ ··· 112 110 unsigned char silence_value; 113 111 unsigned int stride; 114 112 int iface, altsetting; 113 + unsigned char ep_idx; /* endpoint array index */ 115 114 int skip_packets; /* quirks for devices to ignore the first n packets 116 115 in a stream */ 117 - bool is_implicit_feedback; /* This endpoint is used as implicit feedback */ 116 + bool implicit_fb_sync; /* syncs with implicit feedback */ 117 + bool need_setup; /* (re-)need for configure? */ 118 118 119 119 /* for hw constraints */ 120 + struct audioformat *cur_audiofmt; 120 121 unsigned int cur_rate; 121 122 snd_pcm_format_t cur_format; 122 123 unsigned int cur_channels; 124 + unsigned int cur_frame_bytes; 123 125 unsigned int cur_period_frames; 124 126 unsigned int cur_period_bytes; 125 127 unsigned int cur_buffer_periods; ··· 158 152 unsigned int stream_offset_adj; /* Bytes to drop from beginning of stream (for non-compliant devices) */ 159 153 160 154 unsigned int running: 1; /* running status */ 161 - unsigned int fixed_hw:1; /* fixed hw constraints due to sync EP */ 162 155 163 156 unsigned int hwptr_done; /* processed byte position in the buffer */ 164 157 unsigned int transfer_done; /* processed frames since last period update */ ··· 168 163 struct snd_usb_endpoint *data_endpoint; 169 164 struct snd_usb_endpoint *sync_endpoint; 170 165 unsigned long flags; 171 - bool need_setup_ep; /* (re)configure EP at prepare? */ 172 - bool need_setup_fmt; /* (re)configure fmt after resume? */ 173 166 unsigned int speed; /* USB_SPEED_XXX */ 174 167 175 168 u64 formats; /* format bitmasks (all or'ed) */
+3 -10
sound/usb/clock.c
··· 613 613 static int set_sample_rate_v2v3(struct snd_usb_audio *chip, 614 614 struct audioformat *fmt, int rate) 615 615 { 616 - struct usb_device *dev = chip->dev; 617 616 int cur_rate, prev_rate; 618 617 int clock; 619 618 ··· 655 656 return -ENXIO; 656 657 } 657 658 658 - /* Some devices doesn't respond to sample rate changes while the 659 - * interface is active. */ 660 - if (rate != prev_rate) { 661 - usb_set_interface(dev, fmt->iface, 0); 662 - snd_usb_set_interface_quirk(chip); 663 - usb_set_interface(dev, fmt->iface, fmt->altsetting); 664 - snd_usb_set_interface_quirk(chip); 665 - } 666 - 667 659 validation: 668 660 /* validate clock after rate change */ 669 661 if (!uac_clock_source_is_valid(chip, fmt, clock)) ··· 665 675 int snd_usb_init_sample_rate(struct snd_usb_audio *chip, 666 676 struct audioformat *fmt, int rate) 667 677 { 678 + usb_audio_dbg(chip, "%d:%d Set sample rate %d, clock %d\n", 679 + fmt->iface, fmt->altsetting, rate, fmt->clock); 680 + 668 681 switch (fmt->protocol) { 669 682 case UAC_VERSION_1: 670 683 default:
+335 -325
sound/usb/endpoint.c
··· 18 18 #include "card.h" 19 19 #include "endpoint.h" 20 20 #include "pcm.h" 21 + #include "clock.h" 21 22 #include "quirks.h" 22 23 23 24 #define EP_FLAG_RUNNING 1 ··· 117 116 */ 118 117 int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep) 119 118 { 120 - return ep->sync_master && 121 - ep->sync_master->type == SND_USB_ENDPOINT_TYPE_DATA && 122 - ep->type == SND_USB_ENDPOINT_TYPE_DATA && 123 - usb_pipeout(ep->pipe); 119 + return ep->implicit_fb_sync && usb_pipeout(ep->pipe); 124 120 } 125 121 126 122 /* ··· 183 185 call_retire_callback(ep, urb_ctx->urb); 184 186 } 185 187 188 + static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep, 189 + struct snd_usb_endpoint *sender, 190 + const struct urb *urb); 191 + 186 192 static void retire_inbound_urb(struct snd_usb_endpoint *ep, 187 193 struct snd_urb_ctx *urb_ctx) 188 194 { 189 195 struct urb *urb = urb_ctx->urb; 196 + struct snd_usb_endpoint *sync_slave; 190 197 191 198 if (unlikely(ep->skip_packets > 0)) { 192 199 ep->skip_packets--; 193 200 return; 194 201 } 195 202 196 - if (ep->sync_slave) 197 - snd_usb_handle_sync_urb(ep->sync_slave, ep, urb); 203 + sync_slave = READ_ONCE(ep->sync_slave); 204 + if (sync_slave) 205 + snd_usb_handle_sync_urb(sync_slave, ep, urb); 198 206 199 207 call_retire_callback(ep, urb); 200 208 } ··· 522 518 } 523 519 524 520 /* Set up syncinterval and maxsyncsize for a sync EP */ 525 - void snd_usb_endpoint_set_syncinterval(struct snd_usb_audio *chip, 526 - struct snd_usb_endpoint *ep, 527 - struct usb_host_interface *alts) 521 + static void endpoint_set_syncinterval(struct snd_usb_audio *chip, 522 + struct snd_usb_endpoint *ep) 528 523 { 529 - struct usb_endpoint_descriptor *desc = get_endpoint(alts, 1); 524 + struct usb_host_interface *alts; 525 + struct usb_endpoint_descriptor *desc; 530 526 531 - if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC) { 532 - if (desc->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && 533 - desc->bRefresh >= 1 && desc->bRefresh <= 9) 534 - ep->syncinterval = desc->bRefresh; 535 - else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) 536 - ep->syncinterval = 1; 537 - else if (desc->bInterval >= 1 && desc->bInterval <= 16) 538 - ep->syncinterval = desc->bInterval - 1; 539 - else 540 - ep->syncinterval = 3; 527 + alts = snd_usb_get_host_interface(chip, ep->iface, ep->altsetting); 528 + if (!alts) 529 + return; 541 530 542 - ep->syncmaxsize = le16_to_cpu(desc->wMaxPacketSize); 531 + desc = get_endpoint(alts, ep->ep_idx); 532 + if (desc->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && 533 + desc->bRefresh >= 1 && desc->bRefresh <= 9) 534 + ep->syncinterval = desc->bRefresh; 535 + else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) 536 + ep->syncinterval = 1; 537 + else if (desc->bInterval >= 1 && desc->bInterval <= 16) 538 + ep->syncinterval = desc->bInterval - 1; 539 + else 540 + ep->syncinterval = 3; 541 + 542 + ep->syncmaxsize = le16_to_cpu(desc->wMaxPacketSize); 543 + } 544 + 545 + static bool endpoint_compatible(struct snd_usb_endpoint *ep, 546 + const struct audioformat *fp, 547 + const struct snd_pcm_hw_params *params) 548 + { 549 + if (!ep->opened) 550 + return false; 551 + if (ep->cur_audiofmt != fp) 552 + return false; 553 + if (ep->cur_rate != params_rate(params) || 554 + ep->cur_format != params_format(params) || 555 + ep->cur_period_frames != params_period_size(params) || 556 + ep->cur_buffer_periods != params_periods(params)) 557 + return false; 558 + return true; 559 + } 560 + 561 + /* 562 + * Check whether the given fp and hw params are compatbile with the current 563 + * setup of the target EP for implicit feedback sync 564 + */ 565 + bool snd_usb_endpoint_compatible(struct snd_usb_audio *chip, 566 + struct snd_usb_endpoint *ep, 567 + const struct audioformat *fp, 568 + const struct snd_pcm_hw_params *params) 569 + { 570 + bool ret; 571 + 572 + mutex_lock(&chip->mutex); 573 + ret = endpoint_compatible(ep, fp, params); 574 + mutex_unlock(&chip->mutex); 575 + return ret; 576 + } 577 + 578 + /* 579 + * snd_usb_endpoint_open: Open the endpoint 580 + * 581 + * Called from hw_params to assign the endpoint to the substream. 582 + * It's reference-counted, and only the first opener is allowed to set up 583 + * arbitrary parameters. The later opener must be compatible with the 584 + * former opened parameters. 585 + * The endpoint needs to be closed via snd_usb_endpoint_close() later. 586 + * 587 + * Note that this function doesn't configure the endpoint. The substream 588 + * needs to set it up later via snd_usb_endpoint_configure(). 589 + */ 590 + struct snd_usb_endpoint * 591 + snd_usb_endpoint_open(struct snd_usb_audio *chip, 592 + struct audioformat *fp, 593 + const struct snd_pcm_hw_params *params, 594 + bool is_sync_ep) 595 + { 596 + struct snd_usb_endpoint *ep; 597 + int ep_num = is_sync_ep ? fp->sync_ep : fp->endpoint; 598 + 599 + mutex_lock(&chip->mutex); 600 + ep = snd_usb_get_endpoint(chip, ep_num); 601 + if (!ep) { 602 + usb_audio_err(chip, "Cannot find EP 0x%x to open\n", ep_num); 603 + goto unlock; 543 604 } 605 + 606 + if (!ep->opened) { 607 + if (is_sync_ep) { 608 + ep->iface = fp->sync_iface; 609 + ep->altsetting = fp->sync_altsetting; 610 + ep->ep_idx = fp->sync_ep_idx; 611 + } else { 612 + ep->iface = fp->iface; 613 + ep->altsetting = fp->altsetting; 614 + ep->ep_idx = 0; 615 + } 616 + usb_audio_dbg(chip, "Open EP 0x%x, iface=%d:%d, idx=%d\n", 617 + ep_num, ep->iface, ep->altsetting, ep->ep_idx); 618 + 619 + ep->cur_audiofmt = fp; 620 + ep->cur_channels = fp->channels; 621 + ep->cur_rate = params_rate(params); 622 + ep->cur_format = params_format(params); 623 + ep->cur_frame_bytes = snd_pcm_format_physical_width(ep->cur_format) * 624 + ep->cur_channels / 8; 625 + ep->cur_period_frames = params_period_size(params); 626 + ep->cur_period_bytes = ep->cur_period_frames * ep->cur_frame_bytes; 627 + ep->cur_buffer_periods = params_periods(params); 628 + 629 + if (ep->type == SND_USB_ENDPOINT_TYPE_SYNC) 630 + endpoint_set_syncinterval(chip, ep); 631 + 632 + ep->implicit_fb_sync = fp->implicit_fb; 633 + ep->need_setup = true; 634 + 635 + usb_audio_dbg(chip, " channels=%d, rate=%d, format=%s, period_bytes=%d, periods=%d, implicit_fb=%d\n", 636 + ep->cur_channels, ep->cur_rate, 637 + snd_pcm_format_name(ep->cur_format), 638 + ep->cur_period_bytes, ep->cur_buffer_periods, 639 + ep->implicit_fb_sync); 640 + 641 + } else { 642 + if (!endpoint_compatible(ep, fp, params)) { 643 + usb_audio_err(chip, "Incompatible EP setup for 0x%x\n", 644 + ep_num); 645 + ep = NULL; 646 + goto unlock; 647 + } 648 + 649 + usb_audio_dbg(chip, "Reopened EP 0x%x (count %d)\n", 650 + ep_num, ep->opened); 651 + } 652 + 653 + ep->opened++; 654 + 655 + unlock: 656 + mutex_unlock(&chip->mutex); 657 + return ep; 658 + } 659 + 660 + /* 661 + * snd_usb_endpoint_set_sync: Link data and sync endpoints 662 + * 663 + * Pass NULL to sync_ep to unlink again 664 + */ 665 + void snd_usb_endpoint_set_sync(struct snd_usb_audio *chip, 666 + struct snd_usb_endpoint *data_ep, 667 + struct snd_usb_endpoint *sync_ep) 668 + { 669 + data_ep->sync_master = sync_ep; 544 670 } 545 671 546 672 /* ··· 689 555 ep->prepare_data_urb = prepare; 690 556 ep->retire_data_urb = retire; 691 557 WRITE_ONCE(ep->data_subs, data_subs); 558 + } 559 + 560 + static int endpoint_set_interface(struct snd_usb_audio *chip, 561 + struct snd_usb_endpoint *ep, 562 + bool set) 563 + { 564 + int altset = set ? ep->altsetting : 0; 565 + int err; 566 + 567 + usb_audio_dbg(chip, "Setting usb interface %d:%d for EP 0x%x\n", 568 + ep->iface, altset, ep->ep_num); 569 + err = usb_set_interface(chip->dev, ep->iface, altset); 570 + if (err < 0) { 571 + usb_audio_err(chip, "%d:%d: usb_set_interface failed (%d)\n", 572 + ep->iface, altset, err); 573 + return err; 574 + } 575 + 576 + snd_usb_set_interface_quirk(chip); 577 + return 0; 578 + } 579 + 580 + /* 581 + * snd_usb_endpoint_close: Close the endpoint 582 + * 583 + * Unreference the already opened endpoint via snd_usb_endpoint_open(). 584 + */ 585 + void snd_usb_endpoint_close(struct snd_usb_audio *chip, 586 + struct snd_usb_endpoint *ep) 587 + { 588 + mutex_lock(&chip->mutex); 589 + usb_audio_dbg(chip, "Closing EP 0x%x (count %d)\n", 590 + ep->ep_num, ep->opened); 591 + if (!--ep->opened) { 592 + endpoint_set_interface(chip, ep, false); 593 + ep->iface = -1; 594 + ep->altsetting = 0; 595 + ep->cur_audiofmt = NULL; 596 + ep->cur_rate = 0; 597 + usb_audio_dbg(chip, "EP 0x%x closed\n", ep->ep_num); 598 + } 599 + mutex_unlock(&chip->mutex); 600 + } 601 + 602 + /* Prepare for suspening EP, called from the main suspend handler */ 603 + void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep) 604 + { 605 + ep->need_setup = true; 692 606 } 693 607 694 608 /* ··· 829 647 } 830 648 831 649 /* 832 - * Check data endpoint for format differences 833 - */ 834 - static bool check_ep_params(struct snd_usb_endpoint *ep, 835 - snd_pcm_format_t pcm_format, 836 - unsigned int channels, 837 - unsigned int period_bytes, 838 - unsigned int frames_per_period, 839 - unsigned int periods_per_buffer, 840 - unsigned int rate, 841 - struct audioformat *fmt, 842 - struct snd_usb_endpoint *sync_ep) 843 - { 844 - unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb; 845 - unsigned int max_packs_per_period, urbs_per_period, urb_packs; 846 - unsigned int max_urbs; 847 - int frame_bits = snd_pcm_format_physical_width(pcm_format) * channels; 848 - int tx_length_quirk = (ep->chip->tx_length_quirk && 849 - usb_pipeout(ep->pipe)); 850 - bool ret = 1; 851 - 852 - /* matching with the saved parameters? */ 853 - if (ep->cur_rate == rate && 854 - ep->cur_format == pcm_format && 855 - ep->cur_channels == channels && 856 - ep->cur_period_frames == frames_per_period && 857 - ep->cur_buffer_periods == periods_per_buffer) 858 - return true; 859 - 860 - if (pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) { 861 - /* 862 - * When operating in DSD DOP mode, the size of a sample frame 863 - * in hardware differs from the actual physical format width 864 - * because we need to make room for the DOP markers. 865 - */ 866 - frame_bits += channels << 3; 867 - } 868 - 869 - ret = ret && (ep->datainterval == fmt->datainterval); 870 - ret = ret && (ep->stride == frame_bits >> 3); 871 - 872 - switch (pcm_format) { 873 - case SNDRV_PCM_FORMAT_U8: 874 - ret = ret && (ep->silence_value == 0x80); 875 - break; 876 - case SNDRV_PCM_FORMAT_DSD_U8: 877 - case SNDRV_PCM_FORMAT_DSD_U16_LE: 878 - case SNDRV_PCM_FORMAT_DSD_U32_LE: 879 - case SNDRV_PCM_FORMAT_DSD_U16_BE: 880 - case SNDRV_PCM_FORMAT_DSD_U32_BE: 881 - ret = ret && (ep->silence_value == 0x69); 882 - break; 883 - default: 884 - ret = ret && (ep->silence_value == 0); 885 - } 886 - 887 - /* assume max. frequency is 50% higher than nominal */ 888 - ret = ret && (ep->freqmax == ep->freqn + (ep->freqn >> 1)); 889 - /* Round up freqmax to nearest integer in order to calculate maximum 890 - * packet size, which must represent a whole number of frames. 891 - * This is accomplished by adding 0x0.ffff before converting the 892 - * Q16.16 format into integer. 893 - * In order to accurately calculate the maximum packet size when 894 - * the data interval is more than 1 (i.e. ep->datainterval > 0), 895 - * multiply by the data interval prior to rounding. For instance, 896 - * a freqmax of 41 kHz will result in a max packet size of 6 (5.125) 897 - * frames with a data interval of 1, but 11 (10.25) frames with a 898 - * data interval of 2. 899 - * (ep->freqmax << ep->datainterval overflows at 8.192 MHz for the 900 - * maximum datainterval value of 3, at USB full speed, higher for 901 - * USB high speed, noting that ep->freqmax is in units of 902 - * frames per packet in Q16.16 format.) 903 - */ 904 - maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) * 905 - (frame_bits >> 3); 906 - if (tx_length_quirk) 907 - maxsize += sizeof(__le32); /* Space for length descriptor */ 908 - /* but wMaxPacketSize might reduce this */ 909 - if (ep->maxpacksize && ep->maxpacksize < maxsize) { 910 - /* whatever fits into a max. size packet */ 911 - unsigned int data_maxsize = maxsize = ep->maxpacksize; 912 - 913 - if (tx_length_quirk) 914 - /* Need to remove the length descriptor to calc freq */ 915 - data_maxsize -= sizeof(__le32); 916 - ret = ret && (ep->freqmax == (data_maxsize / (frame_bits >> 3)) 917 - << (16 - ep->datainterval)); 918 - } 919 - 920 - if (ep->fill_max) 921 - ret = ret && (ep->curpacksize == ep->maxpacksize); 922 - else 923 - ret = ret && (ep->curpacksize == maxsize); 924 - 925 - if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL) { 926 - packs_per_ms = 8 >> ep->datainterval; 927 - max_packs_per_urb = MAX_PACKS_HS; 928 - } else { 929 - packs_per_ms = 1; 930 - max_packs_per_urb = MAX_PACKS; 931 - } 932 - if (sync_ep && !snd_usb_endpoint_implicit_feedback_sink(ep)) 933 - max_packs_per_urb = min(max_packs_per_urb, 934 - 1U << sync_ep->syncinterval); 935 - max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval); 936 - 937 - /* 938 - * Capture endpoints need to use small URBs because there's no way 939 - * to tell in advance where the next period will end, and we don't 940 - * want the next URB to complete much after the period ends. 941 - * 942 - * Playback endpoints with implicit sync much use the same parameters 943 - * as their corresponding capture endpoint. 944 - */ 945 - if (usb_pipein(ep->pipe) || 946 - snd_usb_endpoint_implicit_feedback_sink(ep)) { 947 - 948 - urb_packs = packs_per_ms; 949 - /* 950 - * Wireless devices can poll at a max rate of once per 4ms. 951 - * For dataintervals less than 5, increase the packet count to 952 - * allow the host controller to use bursting to fill in the 953 - * gaps. 954 - */ 955 - if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_WIRELESS) { 956 - int interval = ep->datainterval; 957 - 958 - while (interval < 5) { 959 - urb_packs <<= 1; 960 - ++interval; 961 - } 962 - } 963 - /* make capture URBs <= 1 ms and smaller than a period */ 964 - urb_packs = min(max_packs_per_urb, urb_packs); 965 - while (urb_packs > 1 && urb_packs * maxsize >= period_bytes) 966 - urb_packs >>= 1; 967 - ret = ret && (ep->nurbs == MAX_URBS); 968 - 969 - /* 970 - * Playback endpoints without implicit sync are adjusted so that 971 - * a period fits as evenly as possible in the smallest number of 972 - * URBs. The total number of URBs is adjusted to the size of the 973 - * ALSA buffer, subject to the MAX_URBS and MAX_QUEUE limits. 974 - */ 975 - } else { 976 - /* determine how small a packet can be */ 977 - minsize = (ep->freqn >> (16 - ep->datainterval)) * 978 - (frame_bits >> 3); 979 - /* with sync from device, assume it can be 12% lower */ 980 - if (sync_ep) 981 - minsize -= minsize >> 3; 982 - minsize = max(minsize, 1u); 983 - 984 - /* how many packets will contain an entire ALSA period? */ 985 - max_packs_per_period = DIV_ROUND_UP(period_bytes, minsize); 986 - 987 - /* how many URBs will contain a period? */ 988 - urbs_per_period = DIV_ROUND_UP(max_packs_per_period, 989 - max_packs_per_urb); 990 - /* how many packets are needed in each URB? */ 991 - urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period); 992 - 993 - /* limit the number of frames in a single URB */ 994 - ret = ret && (ep->max_urb_frames == 995 - DIV_ROUND_UP(frames_per_period, urbs_per_period)); 996 - 997 - /* try to use enough URBs to contain an entire ALSA buffer */ 998 - max_urbs = min((unsigned) MAX_URBS, 999 - MAX_QUEUE * packs_per_ms / urb_packs); 1000 - ret = ret && (ep->nurbs == min(max_urbs, 1001 - urbs_per_period * periods_per_buffer)); 1002 - } 1003 - 1004 - ret = ret && (ep->datainterval == fmt->datainterval); 1005 - ret = ret && (ep->maxpacksize == fmt->maxpacksize); 1006 - ret = ret && 1007 - (ep->fill_max == !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)); 1008 - 1009 - return ret; 1010 - } 1011 - 1012 - /* 1013 650 * configure a data endpoint 1014 651 */ 1015 - static int data_ep_set_params(struct snd_usb_endpoint *ep, 1016 - snd_pcm_format_t pcm_format, 1017 - unsigned int channels, 1018 - unsigned int period_bytes, 1019 - unsigned int frames_per_period, 1020 - unsigned int periods_per_buffer, 1021 - struct audioformat *fmt, 1022 - struct snd_usb_endpoint *sync_ep) 652 + static int data_ep_set_params(struct snd_usb_endpoint *ep) 1023 653 { 654 + struct snd_usb_audio *chip = ep->chip; 1024 655 unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb; 1025 656 unsigned int max_packs_per_period, urbs_per_period, urb_packs; 1026 657 unsigned int max_urbs, i; 1027 - int frame_bits = snd_pcm_format_physical_width(pcm_format) * channels; 1028 - int tx_length_quirk = (ep->chip->tx_length_quirk && 658 + const struct audioformat *fmt = ep->cur_audiofmt; 659 + int frame_bits = ep->cur_frame_bytes * 8; 660 + int tx_length_quirk = (chip->tx_length_quirk && 1029 661 usb_pipeout(ep->pipe)); 1030 662 1031 - if (pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) { 663 + usb_audio_dbg(chip, "Setting params for data EP 0x%x, pipe 0x%x\n", 664 + ep->ep_num, ep->pipe); 665 + 666 + if (ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) { 1032 667 /* 1033 668 * When operating in DSD DOP mode, the size of a sample frame 1034 669 * in hardware differs from the actual physical format width 1035 670 * because we need to make room for the DOP markers. 1036 671 */ 1037 - frame_bits += channels << 3; 672 + frame_bits += ep->cur_channels << 3; 1038 673 } 1039 674 1040 675 ep->datainterval = fmt->datainterval; 1041 676 ep->stride = frame_bits >> 3; 1042 677 1043 - switch (pcm_format) { 678 + switch (ep->cur_format) { 1044 679 case SNDRV_PCM_FORMAT_U8: 1045 680 ep->silence_value = 0x80; 1046 681 break; ··· 910 911 else 911 912 ep->curpacksize = maxsize; 912 913 913 - if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL) { 914 + if (snd_usb_get_speed(chip->dev) != USB_SPEED_FULL) { 914 915 packs_per_ms = 8 >> ep->datainterval; 915 916 max_packs_per_urb = MAX_PACKS_HS; 916 917 } else { 917 918 packs_per_ms = 1; 918 919 max_packs_per_urb = MAX_PACKS; 919 920 } 920 - if (sync_ep && !snd_usb_endpoint_implicit_feedback_sink(ep)) 921 + if (ep->sync_master && !ep->implicit_fb_sync) 921 922 max_packs_per_urb = min(max_packs_per_urb, 922 - 1U << sync_ep->syncinterval); 923 + 1U << ep->sync_master->syncinterval); 923 924 max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval); 924 925 925 926 /* ··· 930 931 * Playback endpoints with implicit sync much use the same parameters 931 932 * as their corresponding capture endpoint. 932 933 */ 933 - if (usb_pipein(ep->pipe) || 934 - ep->is_implicit_feedback || 935 - snd_usb_endpoint_implicit_feedback_sink(ep)) { 934 + if (usb_pipein(ep->pipe) || ep->implicit_fb_sync) { 936 935 937 936 urb_packs = packs_per_ms; 938 937 /* ··· 939 942 * allow the host controller to use bursting to fill in the 940 943 * gaps. 941 944 */ 942 - if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_WIRELESS) { 945 + if (snd_usb_get_speed(chip->dev) == USB_SPEED_WIRELESS) { 943 946 int interval = ep->datainterval; 944 947 while (interval < 5) { 945 948 urb_packs <<= 1; ··· 948 951 } 949 952 /* make capture URBs <= 1 ms and smaller than a period */ 950 953 urb_packs = min(max_packs_per_urb, urb_packs); 951 - while (urb_packs > 1 && urb_packs * maxsize >= period_bytes) 954 + while (urb_packs > 1 && urb_packs * maxsize >= ep->cur_period_bytes) 952 955 urb_packs >>= 1; 953 956 ep->nurbs = MAX_URBS; 954 957 ··· 963 966 minsize = (ep->freqn >> (16 - ep->datainterval)) * 964 967 (frame_bits >> 3); 965 968 /* with sync from device, assume it can be 12% lower */ 966 - if (sync_ep) 969 + if (ep->sync_master) 967 970 minsize -= minsize >> 3; 968 971 minsize = max(minsize, 1u); 969 972 970 973 /* how many packets will contain an entire ALSA period? */ 971 - max_packs_per_period = DIV_ROUND_UP(period_bytes, minsize); 974 + max_packs_per_period = DIV_ROUND_UP(ep->cur_period_bytes, minsize); 972 975 973 976 /* how many URBs will contain a period? */ 974 977 urbs_per_period = DIV_ROUND_UP(max_packs_per_period, ··· 977 980 urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period); 978 981 979 982 /* limit the number of frames in a single URB */ 980 - ep->max_urb_frames = DIV_ROUND_UP(frames_per_period, 981 - urbs_per_period); 983 + ep->max_urb_frames = DIV_ROUND_UP(ep->cur_period_frames, 984 + urbs_per_period); 982 985 983 986 /* try to use enough URBs to contain an entire ALSA buffer */ 984 987 max_urbs = min((unsigned) MAX_URBS, 985 988 MAX_QUEUE * packs_per_ms / urb_packs); 986 - ep->nurbs = min(max_urbs, urbs_per_period * periods_per_buffer); 989 + ep->nurbs = min(max_urbs, urbs_per_period * ep->cur_buffer_periods); 987 990 } 988 991 989 992 /* allocate and initialize data urbs */ ··· 1001 1004 goto out_of_memory; 1002 1005 1003 1006 u->urb->transfer_buffer = 1004 - usb_alloc_coherent(ep->chip->dev, u->buffer_size, 1007 + usb_alloc_coherent(chip->dev, u->buffer_size, 1005 1008 GFP_KERNEL, &u->urb->transfer_dma); 1006 1009 if (!u->urb->transfer_buffer) 1007 1010 goto out_of_memory; ··· 1025 1028 */ 1026 1029 static int sync_ep_set_params(struct snd_usb_endpoint *ep) 1027 1030 { 1031 + struct snd_usb_audio *chip = ep->chip; 1028 1032 int i; 1029 1033 1030 - ep->syncbuf = usb_alloc_coherent(ep->chip->dev, SYNC_URBS * 4, 1034 + usb_audio_dbg(chip, "Setting params for sync EP 0x%x, pipe 0x%x\n", 1035 + ep->ep_num, ep->pipe); 1036 + 1037 + ep->syncbuf = usb_alloc_coherent(chip->dev, SYNC_URBS * 4, 1031 1038 GFP_KERNEL, &ep->sync_dma); 1032 1039 if (!ep->syncbuf) 1033 1040 return -ENOMEM; ··· 1064 1063 return -ENOMEM; 1065 1064 } 1066 1065 1067 - /** 1066 + /* 1068 1067 * snd_usb_endpoint_set_params: configure an snd_usb_endpoint 1069 - * 1070 - * @ep: the snd_usb_endpoint to configure 1071 - * @pcm_format: the audio fomat. 1072 - * @channels: the number of audio channels. 1073 - * @period_bytes: the number of bytes in one alsa period. 1074 - * @period_frames: the number of frames in one alsa period. 1075 - * @buffer_periods: the number of periods in one alsa buffer. 1076 - * @rate: the frame rate. 1077 - * @fmt: the USB audio format information 1078 - * @sync_ep: the sync endpoint to use, if any 1079 1068 * 1080 1069 * Determine the number of URBs to be used on this endpoint. 1081 1070 * An endpoint must be configured before it can be started. 1082 1071 * An endpoint that is already running can not be reconfigured. 1083 1072 */ 1084 - int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep, 1085 - snd_pcm_format_t pcm_format, 1086 - unsigned int channels, 1087 - unsigned int period_bytes, 1088 - unsigned int period_frames, 1089 - unsigned int buffer_periods, 1090 - unsigned int rate, 1091 - struct audioformat *fmt, 1092 - struct snd_usb_endpoint *sync_ep) 1073 + static int snd_usb_endpoint_set_params(struct snd_usb_audio *chip, 1074 + struct snd_usb_endpoint *ep) 1093 1075 { 1076 + const struct audioformat *fmt = ep->cur_audiofmt; 1094 1077 int err; 1095 - 1096 - usb_audio_dbg(ep->chip, 1097 - "Setting params for ep %x (type %s, count %d), rate=%d, format=%s, channels=%d, period_bytes=%d, periods=%d\n", 1098 - ep->ep_num, ep_type_name(ep->type), ep->use_count, 1099 - rate, snd_pcm_format_name(pcm_format), channels, 1100 - period_bytes, buffer_periods); 1101 - 1102 - if (ep->use_count != 0) { 1103 - bool check = ep->is_implicit_feedback && 1104 - check_ep_params(ep, pcm_format, channels, period_bytes, 1105 - period_frames, buffer_periods, rate, 1106 - fmt, sync_ep); 1107 - 1108 - if (!check) { 1109 - usb_audio_warn(ep->chip, 1110 - "Unable to change format on ep #%x: already in use\n", 1111 - ep->ep_num); 1112 - return -EBUSY; 1113 - } 1114 - 1115 - usb_audio_dbg(ep->chip, 1116 - "Ep #%x already in use as implicit feedback but format not changed\n", 1117 - ep->ep_num); 1118 - return 0; 1119 - } 1120 1078 1121 1079 /* release old buffers, if any */ 1122 1080 release_urbs(ep, 0); ··· 1084 1124 ep->maxpacksize = fmt->maxpacksize; 1085 1125 ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX); 1086 1126 1087 - if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL) { 1088 - ep->freqn = get_usb_full_speed_rate(rate); 1127 + if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) { 1128 + ep->freqn = get_usb_full_speed_rate(ep->cur_rate); 1089 1129 ep->pps = 1000 >> ep->datainterval; 1090 1130 } else { 1091 - ep->freqn = get_usb_high_speed_rate(rate); 1131 + ep->freqn = get_usb_high_speed_rate(ep->cur_rate); 1092 1132 ep->pps = 8000 >> ep->datainterval; 1093 1133 } 1094 1134 1095 - ep->sample_rem = rate % ep->pps; 1096 - ep->packsize[0] = rate / ep->pps; 1097 - ep->packsize[1] = (rate + (ep->pps - 1)) / ep->pps; 1135 + ep->sample_rem = ep->cur_rate % ep->pps; 1136 + ep->packsize[0] = ep->cur_rate / ep->pps; 1137 + ep->packsize[1] = (ep->cur_rate + (ep->pps - 1)) / ep->pps; 1098 1138 1099 1139 /* calculate the frequency in 16.16 format */ 1100 1140 ep->freqm = ep->freqn; ··· 1104 1144 1105 1145 switch (ep->type) { 1106 1146 case SND_USB_ENDPOINT_TYPE_DATA: 1107 - err = data_ep_set_params(ep, pcm_format, channels, 1108 - period_bytes, period_frames, 1109 - buffer_periods, fmt, sync_ep); 1147 + err = data_ep_set_params(ep); 1110 1148 break; 1111 1149 case SND_USB_ENDPOINT_TYPE_SYNC: 1112 1150 err = sync_ep_set_params(ep); ··· 1113 1155 err = -EINVAL; 1114 1156 } 1115 1157 1116 - usb_audio_dbg(ep->chip, "Set up %d URBS, ret=%d\n", ep->nurbs, err); 1158 + usb_audio_dbg(chip, "Set up %d URBS, ret=%d\n", ep->nurbs, err); 1117 1159 1118 1160 if (err < 0) 1119 1161 return err; 1120 1162 1121 - /* record the current set up in the endpoint (for implicit fb) */ 1122 - spin_lock_irq(&ep->lock); 1123 - ep->cur_rate = rate; 1124 - ep->cur_channels = channels; 1125 - ep->cur_format = pcm_format; 1126 - ep->cur_period_frames = period_frames; 1127 - ep->cur_period_bytes = period_bytes; 1128 - ep->cur_buffer_periods = buffer_periods; 1129 - spin_unlock_irq(&ep->lock); 1163 + /* some unit conversions in runtime */ 1164 + ep->maxframesize = ep->maxpacksize / ep->cur_frame_bytes; 1165 + ep->curframesize = ep->curpacksize / ep->cur_frame_bytes; 1130 1166 1131 1167 return 0; 1168 + } 1169 + 1170 + /* 1171 + * snd_usb_endpoint_configure: Configure the endpoint 1172 + * 1173 + * This function sets up the EP to be fully usable state. 1174 + * It's called either from hw_params or prepare callback. 1175 + * The function checks need_setup flag, and perfoms nothing unless needed, 1176 + * so it's safe to call this multiple times. 1177 + * 1178 + * This returns zero if unchanged, 1 if the configuration has changed, 1179 + * or a negative error code. 1180 + */ 1181 + int snd_usb_endpoint_configure(struct snd_usb_audio *chip, 1182 + struct snd_usb_endpoint *ep) 1183 + { 1184 + bool iface_first; 1185 + int err = 0; 1186 + 1187 + mutex_lock(&chip->mutex); 1188 + if (!ep->need_setup) 1189 + goto unlock; 1190 + 1191 + /* No need to (re-)configure the sync EP belonging to the same altset */ 1192 + if (ep->ep_idx) { 1193 + err = snd_usb_endpoint_set_params(chip, ep); 1194 + if (err < 0) 1195 + goto unlock; 1196 + goto done; 1197 + } 1198 + 1199 + /* Need to deselect altsetting at first */ 1200 + endpoint_set_interface(chip, ep, false); 1201 + 1202 + /* Some UAC1 devices (e.g. Yamaha THR10) need the host interface 1203 + * to be set up before parameter setups 1204 + */ 1205 + iface_first = ep->cur_audiofmt->protocol == UAC_VERSION_1; 1206 + if (iface_first) { 1207 + err = endpoint_set_interface(chip, ep, true); 1208 + if (err < 0) 1209 + goto unlock; 1210 + } 1211 + 1212 + err = snd_usb_init_pitch(chip, ep->cur_audiofmt); 1213 + if (err < 0) 1214 + goto unlock; 1215 + 1216 + err = snd_usb_init_sample_rate(chip, ep->cur_audiofmt, ep->cur_rate); 1217 + if (err < 0) 1218 + goto unlock; 1219 + 1220 + err = snd_usb_endpoint_set_params(chip, ep); 1221 + if (err < 0) 1222 + goto unlock; 1223 + 1224 + err = snd_usb_select_mode_quirk(chip, ep->cur_audiofmt); 1225 + if (err < 0) 1226 + goto unlock; 1227 + 1228 + /* for UAC2/3, enable the interface altset here at last */ 1229 + if (!iface_first) { 1230 + err = endpoint_set_interface(chip, ep, true); 1231 + if (err < 0) 1232 + goto unlock; 1233 + } 1234 + 1235 + done: 1236 + ep->need_setup = false; 1237 + err = 1; 1238 + 1239 + unlock: 1240 + mutex_unlock(&chip->mutex); 1241 + return err; 1132 1242 } 1133 1243 1134 1244 /** ··· 1219 1193 1220 1194 if (atomic_read(&ep->chip->shutdown)) 1221 1195 return -EBADFD; 1196 + 1197 + if (ep->sync_master) 1198 + WRITE_ONCE(ep->sync_master->sync_slave, ep); 1222 1199 1223 1200 usb_audio_dbg(ep->chip, "Starting %s EP 0x%x (count %d)\n", 1224 1201 ep_type_name(ep->type), ep->ep_num, ep->use_count); ··· 1255 1226 list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs); 1256 1227 } 1257 1228 1229 + usb_audio_dbg(ep->chip, "No URB submission due to implicit fb sync\n"); 1258 1230 return 0; 1259 1231 } 1260 1232 ··· 1281 1251 set_bit(i, &ep->active_mask); 1282 1252 } 1283 1253 1254 + usb_audio_dbg(ep->chip, "%d URBs submitted for EP 0x%x\n", 1255 + ep->nurbs, ep->ep_num); 1284 1256 return 0; 1285 1257 1286 1258 __error: 1259 + if (ep->sync_master) 1260 + WRITE_ONCE(ep->sync_master->sync_slave, NULL); 1287 1261 clear_bit(EP_FLAG_RUNNING, &ep->flags); 1288 1262 ep->use_count--; 1289 1263 deactivate_urbs(ep, false); ··· 1319 1285 if (snd_BUG_ON(ep->use_count == 0)) 1320 1286 return; 1321 1287 1288 + if (ep->sync_master) 1289 + WRITE_ONCE(ep->sync_master->sync_slave, NULL); 1290 + 1322 1291 if (--ep->use_count == 0) { 1323 1292 deactivate_urbs(ep, false); 1324 1293 set_bit(EP_FLAG_STOPPING, &ep->flags); 1325 1294 } 1326 - } 1327 - 1328 - /** 1329 - * snd_usb_endpoint_deactivate: deactivate an snd_usb_endpoint 1330 - * 1331 - * @ep: the endpoint to deactivate 1332 - * 1333 - * If the endpoint is not currently in use, this functions will 1334 - * deactivate its associated URBs. 1335 - * 1336 - * In case of any active users, this functions does nothing. 1337 - */ 1338 - void snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep) 1339 - { 1340 - if (!ep) 1341 - return; 1342 - 1343 - if (ep->use_count != 0) 1344 - return; 1345 - 1346 - deactivate_urbs(ep, true); 1347 - wait_clear_urbs(ep); 1348 - 1349 - /* clear the saved hw params */ 1350 - spin_lock_irq(&ep->lock); 1351 - ep->cur_rate = 0; 1352 - spin_unlock_irq(&ep->lock); 1353 1295 } 1354 1296 1355 1297 /** ··· 1353 1343 kfree(ep); 1354 1344 } 1355 1345 1356 - /** 1346 + /* 1357 1347 * snd_usb_handle_sync_urb: parse an USB sync packet 1358 1348 * 1359 1349 * @ep: the endpoint to handle the packet ··· 1363 1353 * This function is called from the context of an endpoint that received 1364 1354 * the packet and is used to let another endpoint object handle the payload. 1365 1355 */ 1366 - void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep, 1367 - struct snd_usb_endpoint *sender, 1368 - const struct urb *urb) 1356 + static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep, 1357 + struct snd_usb_endpoint *sender, 1358 + const struct urb *urb) 1369 1359 { 1370 1360 int shift; 1371 1361 unsigned int f;
+20 -18
sound/usb/endpoint.h
··· 10 10 11 11 int snd_usb_add_endpoint(struct snd_usb_audio *chip, int ep_num, int type); 12 12 13 - int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep, 14 - snd_pcm_format_t pcm_format, 15 - unsigned int channels, 16 - unsigned int period_bytes, 17 - unsigned int period_frames, 18 - unsigned int buffer_periods, 19 - unsigned int rate, 20 - struct audioformat *fmt, 21 - struct snd_usb_endpoint *sync_ep); 13 + struct snd_usb_endpoint * 14 + snd_usb_endpoint_open(struct snd_usb_audio *chip, 15 + struct audioformat *fp, 16 + const struct snd_pcm_hw_params *params, 17 + bool is_sync_ep); 18 + void snd_usb_endpoint_close(struct snd_usb_audio *chip, 19 + struct snd_usb_endpoint *ep); 20 + int snd_usb_endpoint_configure(struct snd_usb_audio *chip, 21 + struct snd_usb_endpoint *ep); 22 + void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep); 22 23 24 + bool snd_usb_endpoint_compatible(struct snd_usb_audio *chip, 25 + struct snd_usb_endpoint *ep, 26 + const struct audioformat *fp, 27 + const struct snd_pcm_hw_params *params); 28 + 29 + void snd_usb_endpoint_set_sync(struct snd_usb_audio *chip, 30 + struct snd_usb_endpoint *data_ep, 31 + struct snd_usb_endpoint *sync_ep); 23 32 void snd_usb_endpoint_set_callback(struct snd_usb_endpoint *ep, 24 33 void (*prepare)(struct snd_usb_substream *subs, 25 34 struct urb *urb), ··· 36 27 struct urb *urb), 37 28 struct snd_usb_substream *data_subs); 38 29 39 - int snd_usb_endpoint_start(struct snd_usb_endpoint *ep); 30 + int snd_usb_endpoint_start(struct snd_usb_endpoint *ep); 40 31 void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep); 41 32 void snd_usb_endpoint_sync_pending_stop(struct snd_usb_endpoint *ep); 33 + void snd_usb_endpoint_suspend(struct snd_usb_endpoint *ep); 42 34 int snd_usb_endpoint_activate(struct snd_usb_endpoint *ep); 43 - void snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep); 44 35 void snd_usb_endpoint_release(struct snd_usb_endpoint *ep); 45 36 void snd_usb_endpoint_free(struct snd_usb_endpoint *ep); 46 37 47 38 int snd_usb_endpoint_implicit_feedback_sink(struct snd_usb_endpoint *ep); 48 39 int snd_usb_endpoint_slave_next_packet_size(struct snd_usb_endpoint *ep); 49 40 int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep); 50 - void snd_usb_endpoint_set_syncinterval(struct snd_usb_audio *chip, 51 - struct snd_usb_endpoint *ep, 52 - struct usb_host_interface *alts); 53 - 54 - void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep, 55 - struct snd_usb_endpoint *sender, 56 - const struct urb *urb); 57 41 58 42 #endif /* __USBAUDIO_ENDPOINT_H */
+254 -380
sound/usb/pcm.c
··· 80 80 /* 81 81 * find a matching audio format 82 82 */ 83 - static struct audioformat *find_format(struct list_head *fmt_list_head, 84 - snd_pcm_format_t format, 85 - unsigned int rate, 86 - unsigned int channels, 87 - struct snd_usb_substream *subs) 83 + static struct audioformat * 84 + find_format(struct list_head *fmt_list_head, snd_pcm_format_t format, 85 + unsigned int rate, unsigned int channels, bool strict_match, 86 + struct snd_usb_substream *subs) 88 87 { 89 88 struct audioformat *fp; 90 89 struct audioformat *found = NULL; 91 90 int cur_attr = 0, attr; 92 91 93 92 list_for_each_entry(fp, fmt_list_head, list) { 94 - if (!(fp->formats & pcm_format_to_bits(format))) 95 - continue; 96 - if (fp->channels != channels) 97 - continue; 93 + if (strict_match) { 94 + if (!(fp->formats & pcm_format_to_bits(format))) 95 + continue; 96 + if (fp->channels != channels) 97 + continue; 98 + } 98 99 if (rate < fp->rate_min || rate > fp->rate_max) 99 100 continue; 100 101 if (!(fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) { ··· 141 140 return found; 142 141 } 143 142 144 - static struct audioformat *find_substream_format(struct snd_usb_substream *subs) 143 + static struct audioformat * 144 + find_substream_format(struct snd_usb_substream *subs, 145 + const struct snd_pcm_hw_params *params) 145 146 { 146 - return find_format(&subs->fmt_list, subs->pcm_format, subs->cur_rate, 147 - subs->channels, subs); 147 + return find_format(&subs->fmt_list, params_format(params), 148 + params_rate(params), params_channels(params), 149 + true, subs); 148 150 } 149 151 150 - static int init_pitch_v1(struct snd_usb_audio *chip, 151 - struct audioformat *fmt) 152 + static int init_pitch_v1(struct snd_usb_audio *chip, int ep) 152 153 { 153 154 struct usb_device *dev = chip->dev; 154 - unsigned int ep; 155 155 unsigned char data[1]; 156 156 int err; 157 - 158 - ep = fmt->endpoint; 159 157 160 158 data[0] = 1; 161 159 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR, 162 160 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, 163 161 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep, 164 162 data, sizeof(data)); 165 - if (err < 0) { 166 - usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n", 167 - fmt->iface, ep); 168 - return err; 169 - } 170 - 171 - return 0; 163 + return err; 172 164 } 173 165 174 - static int init_pitch_v2(struct snd_usb_audio *chip, 175 - struct audioformat *fmt) 166 + static int init_pitch_v2(struct snd_usb_audio *chip, int ep) 176 167 { 177 168 struct usb_device *dev = chip->dev; 178 169 unsigned char data[1]; ··· 175 182 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT, 176 183 UAC2_EP_CS_PITCH << 8, 0, 177 184 data, sizeof(data)); 178 - if (err < 0) { 179 - usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n", 180 - fmt->iface, fmt->altsetting); 181 - return err; 182 - } 183 - 184 - return 0; 185 + return err; 185 186 } 186 187 187 188 /* ··· 184 197 int snd_usb_init_pitch(struct snd_usb_audio *chip, 185 198 struct audioformat *fmt) 186 199 { 200 + int err; 201 + 187 202 /* if endpoint doesn't have pitch control, bail out */ 188 203 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL)) 189 204 return 0; 190 205 206 + usb_audio_dbg(chip, "enable PITCH for EP 0x%x\n", fmt->endpoint); 207 + 191 208 switch (fmt->protocol) { 192 209 case UAC_VERSION_1: 193 - default: 194 - return init_pitch_v1(chip, fmt); 195 - 210 + err = init_pitch_v1(chip, fmt->endpoint); 211 + break; 196 212 case UAC_VERSION_2: 197 - return init_pitch_v2(chip, fmt); 213 + err = init_pitch_v2(chip, fmt->endpoint); 214 + break; 215 + default: 216 + return 0; 198 217 } 218 + 219 + if (err < 0) { 220 + usb_audio_err(chip, "failed to enable PITCH for EP 0x%x\n", 221 + fmt->endpoint); 222 + return err; 223 + } 224 + 225 + return 0; 199 226 } 200 227 201 - static void stop_endpoints(struct snd_usb_substream *subs) 228 + static bool stop_endpoints(struct snd_usb_substream *subs) 202 229 { 230 + bool stopped = 0; 231 + 203 232 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) { 204 233 snd_usb_endpoint_stop(subs->sync_endpoint); 205 - subs->sync_endpoint->sync_slave = NULL; 234 + stopped = true; 206 235 } 207 - 208 - if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) 236 + if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) { 209 237 snd_usb_endpoint_stop(subs->data_endpoint); 238 + stopped = true; 239 + } 240 + return stopped; 210 241 } 211 242 212 243 static int start_endpoints(struct snd_usb_substream *subs) ··· 235 230 return -EINVAL; 236 231 237 232 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) { 238 - struct snd_usb_endpoint *ep = subs->data_endpoint; 239 - 240 - err = snd_usb_endpoint_start(ep); 233 + err = snd_usb_endpoint_start(subs->data_endpoint); 241 234 if (err < 0) { 242 235 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags); 243 236 goto error; ··· 244 241 245 242 if (subs->sync_endpoint && 246 243 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) { 247 - struct snd_usb_endpoint *ep = subs->sync_endpoint; 248 - 249 - ep->sync_slave = subs->data_endpoint; 250 - err = snd_usb_endpoint_start(ep); 244 + err = snd_usb_endpoint_start(subs->sync_endpoint); 251 245 if (err < 0) { 252 246 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags); 253 - ep->sync_slave = NULL; 254 247 goto error; 255 248 } 256 249 } ··· 445 446 fmt->sync_ep = ep; 446 447 fmt->sync_iface = ifnum; 447 448 fmt->sync_altsetting = alts->desc.bAlternateSetting; 449 + fmt->sync_ep_idx = 0; 448 450 fmt->implicit_fb = 1; 449 451 dev_dbg(&dev->dev, "%d:%d: found implicit_fb sync_ep=%x, iface=%d, alt=%d\n", 450 452 fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface, ··· 527 527 fmt->sync_ep = ep; 528 528 fmt->sync_iface = altsd->bInterfaceNumber; 529 529 fmt->sync_altsetting = altsd->bAlternateSetting; 530 + fmt->sync_ep_idx = 1; 530 531 if ((sync_attr & USB_ENDPOINT_USAGE_MASK) == USB_ENDPOINT_USAGE_IMPLICIT_FB) 531 532 fmt->implicit_fb = 1; 532 533 533 534 dev_dbg(&dev->dev, "%d:%d: found sync_ep=0x%x, iface=%d, alt=%d, implicit_fb=%d\n", 534 535 fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface, 535 536 fmt->sync_altsetting, fmt->implicit_fb); 536 - 537 - return 0; 538 - } 539 - 540 - static int set_sync_endpoint(struct snd_usb_substream *subs, 541 - struct audioformat *fmt) 542 - { 543 - struct usb_device *dev = subs->dev; 544 - struct usb_host_interface *alts; 545 - struct snd_usb_audio *chip = subs->stream->chip; 546 - int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; 547 - unsigned int ep; 548 - int err; 549 - 550 - subs->sync_endpoint = NULL; 551 - subs->data_endpoint->sync_master = NULL; 552 - 553 - ep = fmt->sync_ep; 554 - if (!ep) 555 - return 0; 556 - 557 - alts = snd_usb_get_host_interface(subs->stream->chip, fmt->sync_iface, 558 - fmt->altsetting); 559 - if (!alts) 560 - return 0; 561 - 562 - subs->sync_endpoint = snd_usb_get_endpoint(chip, ep); 563 - if (!subs->sync_endpoint) { 564 - if (is_playback && 565 - (fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) == USB_ENDPOINT_SYNC_NONE) 566 - return 0; 567 - return -EINVAL; 568 - } 569 - 570 - subs->sync_endpoint->iface = fmt->sync_iface; 571 - subs->sync_endpoint->altsetting = fmt->sync_altsetting; 572 - subs->sync_endpoint->is_implicit_feedback = fmt->implicit_fb; 573 - 574 - subs->data_endpoint->sync_master = subs->sync_endpoint; 575 - 576 - snd_usb_endpoint_set_syncinterval(subs->stream->chip, subs->sync_endpoint, alts); 577 - 578 - if (!subs->sync_endpoint->use_count && 579 - (subs->data_endpoint->iface != subs->sync_endpoint->iface || 580 - subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting)) { 581 - err = usb_set_interface(subs->dev, 582 - subs->sync_endpoint->iface, 583 - subs->sync_endpoint->altsetting); 584 - if (err < 0) 585 - return err; 586 - dev_dbg(&dev->dev, "setting usb interface %d:%d\n", 587 - subs->sync_endpoint->iface, 588 - subs->sync_endpoint->altsetting); 589 - snd_usb_set_interface_quirk(chip); 590 - } 591 - 592 - return 0; 593 - } 594 - 595 - /* 596 - * find a matching format and set up the interface 597 - */ 598 - static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt) 599 - { 600 - struct usb_device *dev = subs->dev; 601 - struct snd_usb_audio *chip = subs->stream->chip; 602 - struct usb_host_interface *alts; 603 - struct usb_interface *iface; 604 - struct snd_usb_endpoint *ep; 605 - int err; 606 - 607 - iface = usb_ifnum_to_if(dev, fmt->iface); 608 - if (WARN_ON(!iface)) 609 - return -EINVAL; 610 - alts = usb_altnum_to_altsetting(iface, fmt->altsetting); 611 - if (WARN_ON(!alts)) 612 - return -EINVAL; 613 - 614 - if (fmt == subs->cur_audiofmt && !subs->need_setup_fmt) 615 - return 0; 616 - 617 - /* shared EP with implicit fb */ 618 - if (fmt->implicit_fb && !subs->need_setup_fmt) { 619 - ep = snd_usb_get_endpoint(chip, fmt->endpoint); 620 - if (ep && ep->use_count > 0) 621 - goto add_data_ep; 622 - } 623 - 624 - /* close the old interface */ 625 - if (subs->interface >= 0 && (subs->interface != fmt->iface || subs->need_setup_fmt)) { 626 - err = usb_set_interface(subs->dev, subs->interface, 0); 627 - if (err < 0) { 628 - dev_err(&dev->dev, 629 - "%d:%d: return to setting 0 failed (%d)\n", 630 - fmt->iface, fmt->altsetting, err); 631 - return -EIO; 632 - } 633 - subs->interface = -1; 634 - subs->altset_idx = 0; 635 - } 636 - 637 - if (subs->need_setup_fmt) 638 - subs->need_setup_fmt = false; 639 - 640 - /* set interface */ 641 - if (iface->cur_altsetting != alts) { 642 - err = snd_usb_select_mode_quirk(chip, fmt); 643 - if (err < 0) 644 - return -EIO; 645 - 646 - err = usb_set_interface(dev, fmt->iface, fmt->altsetting); 647 - if (err < 0) { 648 - dev_err(&dev->dev, 649 - "%d:%d: usb_set_interface failed (%d)\n", 650 - fmt->iface, fmt->altsetting, err); 651 - return -EIO; 652 - } 653 - dev_dbg(&dev->dev, "setting usb interface %d:%d\n", 654 - fmt->iface, fmt->altsetting); 655 - snd_usb_set_interface_quirk(chip); 656 - } 657 - 658 - subs->need_setup_ep = true; 659 - 660 - add_data_ep: 661 - subs->interface = fmt->iface; 662 - subs->altset_idx = fmt->altset_idx; 663 - subs->data_endpoint = snd_usb_get_endpoint(chip, fmt->endpoint); 664 - if (!subs->data_endpoint) 665 - return -EINVAL; 666 - subs->data_endpoint->iface = fmt->iface; 667 - subs->data_endpoint->altsetting = fmt->altsetting; 668 - 669 - err = set_sync_endpoint(subs, fmt); 670 - if (err < 0) 671 - return err; 672 - 673 - if (subs->need_setup_ep) { 674 - err = snd_usb_init_pitch(chip, fmt); 675 - if (err < 0) 676 - return err; 677 - } 678 - 679 - subs->cur_audiofmt = fmt; 680 - 681 - snd_usb_set_format_quirk(subs, fmt); 682 537 683 538 return 0; 684 539 } ··· 546 691 * - Requested sample rate is not supported. 547 692 */ 548 693 static int match_endpoint_audioformats(struct snd_usb_substream *subs, 549 - struct audioformat *fp, 550 - struct audioformat *match, int rate, 694 + const struct audioformat *fp, 695 + int rate, int channels, 551 696 snd_pcm_format_t pcm_format) 552 697 { 553 698 int i, score; ··· 571 716 } 572 717 573 718 score = 1; 574 - if (fp->channels == match->channels) 719 + if (fp->channels == channels) 575 720 score++; 576 721 577 722 return score; 578 - } 579 - 580 - /* 581 - * Configure the sync ep using the rate and pcm format of the data ep. 582 - */ 583 - static int configure_sync_endpoint(struct snd_usb_substream *subs) 584 - { 585 - struct audioformat *fp; 586 - struct audioformat *sync_fp = NULL; 587 - int cur_score = 0; 588 - int sync_period_bytes = subs->period_bytes; 589 - struct snd_usb_substream *sync_subs = 590 - &subs->stream->substream[subs->direction ^ 1]; 591 - 592 - if (subs->fixed_hw || 593 - !subs->sync_endpoint->is_implicit_feedback) { 594 - sync_fp = subs->cur_audiofmt; 595 - goto configure; 596 - } 597 - 598 - sync_fp = find_format(&sync_subs->fmt_list, subs->pcm_format, 599 - subs->cur_rate, subs->channels, NULL); 600 - if (sync_fp) 601 - goto configure; 602 - 603 - /* Try to find the best matching audioformat. */ 604 - list_for_each_entry(fp, &sync_subs->fmt_list, list) { 605 - int score = match_endpoint_audioformats(subs, 606 - fp, subs->cur_audiofmt, 607 - subs->cur_rate, subs->pcm_format); 608 - 609 - if (score > cur_score) { 610 - sync_fp = fp; 611 - cur_score = score; 612 - } 613 - } 614 - 615 - if (unlikely(sync_fp == NULL)) { 616 - dev_err(&subs->dev->dev, 617 - "%s: no valid audioformat for sync ep %x found\n", 618 - __func__, sync_subs->ep_num); 619 - return -EINVAL; 620 - } 621 - 622 - /* 623 - * Recalculate the period bytes if channel number differ between 624 - * data and sync ep audioformat. 625 - */ 626 - if (sync_fp->channels != subs->channels) { 627 - sync_period_bytes = (subs->period_bytes / subs->channels) * 628 - sync_fp->channels; 629 - dev_dbg(&subs->dev->dev, 630 - "%s: adjusted sync ep period bytes (%d -> %d)\n", 631 - __func__, subs->period_bytes, sync_period_bytes); 632 - } 633 - 634 - configure: 635 - return snd_usb_endpoint_set_params(subs->sync_endpoint, 636 - subs->pcm_format, 637 - sync_fp->channels, 638 - sync_period_bytes, 639 - subs->period_frames, 640 - subs->buffer_periods, 641 - subs->cur_rate, 642 - sync_fp, 643 - NULL); 644 - } 645 - 646 - /* 647 - * configure endpoint params 648 - * 649 - * called during initial setup and upon resume 650 - */ 651 - static int configure_endpoint(struct snd_usb_substream *subs) 652 - { 653 - int ret; 654 - 655 - /* format changed */ 656 - stop_endpoints(subs); 657 - sync_pending_stops(subs); 658 - ret = snd_usb_endpoint_set_params(subs->data_endpoint, 659 - subs->pcm_format, 660 - subs->channels, 661 - subs->period_bytes, 662 - subs->period_frames, 663 - subs->buffer_periods, 664 - subs->cur_rate, 665 - subs->cur_audiofmt, 666 - subs->sync_endpoint); 667 - if (ret < 0) 668 - return ret; 669 - 670 - if (subs->sync_endpoint) 671 - ret = configure_sync_endpoint(subs); 672 - 673 - return ret; 674 723 } 675 724 676 725 static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state) ··· 625 866 return 0; 626 867 } 627 868 869 + static struct snd_usb_substream * 870 + find_matching_substream(struct snd_usb_audio *chip, int stream, int ep_num, 871 + int fmt_type) 872 + { 873 + struct snd_usb_stream *as; 874 + struct snd_usb_substream *subs; 875 + 876 + list_for_each_entry(as, &chip->pcm_list, list) { 877 + subs = &as->substream[stream]; 878 + if (as->fmt_type == fmt_type && subs->ep_num == ep_num) 879 + return subs; 880 + } 881 + 882 + return NULL; 883 + } 884 + 885 + static struct audioformat * 886 + find_implicit_fb_sync_format(struct snd_usb_audio *chip, 887 + const struct audioformat *target, 888 + const struct snd_pcm_hw_params *params, 889 + int stream) 890 + { 891 + struct snd_usb_substream *subs; 892 + struct audioformat *fp, *sync_fmt; 893 + int score, high_score; 894 + 895 + subs = find_matching_substream(chip, stream, target->sync_ep, 896 + target->fmt_type); 897 + if (!subs) 898 + return NULL; 899 + 900 + sync_fmt = NULL; 901 + high_score = 0; 902 + list_for_each_entry(fp, &subs->fmt_list, list) { 903 + score = match_endpoint_audioformats(subs, fp, 904 + params_rate(params), 905 + params_channels(params), 906 + params_format(params)); 907 + if (score > high_score) { 908 + sync_fmt = fp; 909 + high_score = score; 910 + } 911 + } 912 + 913 + return sync_fmt; 914 + } 915 + 916 + static void close_endpoints(struct snd_usb_audio *chip, 917 + struct snd_usb_substream *subs) 918 + { 919 + if (subs->data_endpoint) { 920 + snd_usb_endpoint_set_sync(chip, subs->data_endpoint, NULL); 921 + snd_usb_endpoint_close(chip, subs->data_endpoint); 922 + subs->data_endpoint = NULL; 923 + } 924 + 925 + if (subs->sync_endpoint) { 926 + snd_usb_endpoint_close(chip, subs->sync_endpoint); 927 + subs->sync_endpoint = NULL; 928 + } 929 + } 930 + 931 + static int configure_endpoints(struct snd_usb_audio *chip, 932 + struct snd_usb_substream *subs) 933 + { 934 + int err; 935 + 936 + if (subs->data_endpoint->need_setup) { 937 + /* stop any running stream beforehand */ 938 + if (stop_endpoints(subs)) 939 + sync_pending_stops(subs); 940 + err = snd_usb_endpoint_configure(chip, subs->data_endpoint); 941 + if (err < 0) 942 + return err; 943 + snd_usb_set_format_quirk(subs, subs->cur_audiofmt); 944 + } 945 + 946 + if (subs->sync_endpoint) { 947 + err = snd_usb_endpoint_configure(chip, subs->sync_endpoint); 948 + if (err < 0) 949 + return err; 950 + } 951 + 952 + return 0; 953 + } 954 + 628 955 /* 629 956 * hw_params callback 630 957 * ··· 725 880 struct snd_pcm_hw_params *hw_params) 726 881 { 727 882 struct snd_usb_substream *subs = substream->runtime->private_data; 883 + struct snd_usb_audio *chip = subs->stream->chip; 728 884 struct audioformat *fmt; 885 + struct audioformat *sync_fmt; 729 886 int ret; 730 887 731 888 ret = snd_media_start_pipeline(subs); 732 889 if (ret) 733 890 return ret; 891 + 892 + fmt = find_substream_format(subs, hw_params); 893 + if (!fmt) { 894 + usb_audio_dbg(chip, 895 + "cannot find format: format=%s, rate=%d, channels=%d\n", 896 + snd_pcm_format_name(params_format(hw_params)), 897 + params_rate(hw_params), params_channels(hw_params)); 898 + ret = -EINVAL; 899 + goto stop_pipeline; 900 + } 901 + 902 + if (fmt->implicit_fb && 903 + (fmt->iface != fmt->sync_iface || 904 + fmt->altsetting != fmt->sync_altsetting)) { 905 + sync_fmt = find_implicit_fb_sync_format(chip, fmt, hw_params, 906 + !substream->stream); 907 + if (!sync_fmt) { 908 + usb_audio_dbg(chip, 909 + "cannot find sync format: ep=0x%x, iface=%d:%d, format=%s, rate=%d, channels=%d\n", 910 + fmt->sync_ep, fmt->sync_iface, 911 + fmt->sync_altsetting, 912 + snd_pcm_format_name(params_format(hw_params)), 913 + params_rate(hw_params), params_channels(hw_params)); 914 + ret = -EINVAL; 915 + goto stop_pipeline; 916 + } 917 + } else { 918 + sync_fmt = fmt; 919 + } 920 + 921 + ret = snd_usb_lock_shutdown(chip); 922 + if (ret < 0) 923 + goto stop_pipeline; 924 + 925 + ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0); 926 + if (ret < 0) 927 + goto unlock; 928 + 929 + if (subs->data_endpoint) { 930 + if (snd_usb_endpoint_compatible(chip, subs->data_endpoint, 931 + fmt, hw_params)) 932 + goto unlock; 933 + close_endpoints(chip, subs); 934 + } 935 + 936 + subs->data_endpoint = snd_usb_endpoint_open(chip, fmt, hw_params, false); 937 + if (!subs->data_endpoint) { 938 + ret = -EINVAL; 939 + goto unlock; 940 + } 941 + 942 + if (fmt->sync_ep) { 943 + subs->sync_endpoint = snd_usb_endpoint_open(chip, sync_fmt, 944 + hw_params, 945 + fmt == sync_fmt); 946 + if (!subs->sync_endpoint) { 947 + ret = -EINVAL; 948 + goto unlock; 949 + } 950 + 951 + snd_usb_endpoint_set_sync(chip, subs->data_endpoint, 952 + subs->sync_endpoint); 953 + } 954 + 955 + subs->interface = fmt->iface; 956 + subs->altset_idx = fmt->altset_idx; 957 + subs->cur_audiofmt = fmt; 958 + 959 + ret = configure_endpoints(chip, subs); 960 + if (ret < 0) 961 + goto unlock; 734 962 735 963 subs->pcm_format = params_format(hw_params); 736 964 subs->period_bytes = params_period_bytes(hw_params); ··· 812 894 subs->channels = params_channels(hw_params); 813 895 subs->cur_rate = params_rate(hw_params); 814 896 815 - fmt = find_substream_format(subs); 816 - if (!fmt) { 817 - dev_dbg(&subs->dev->dev, 818 - "cannot set format: format = %#x, rate = %d, channels = %d\n", 819 - subs->pcm_format, subs->cur_rate, subs->channels); 820 - ret = -EINVAL; 821 - goto stop_pipeline; 822 - } 823 - 824 - ret = snd_usb_lock_shutdown(subs->stream->chip); 825 - if (ret < 0) 826 - goto stop_pipeline; 827 - 828 - ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0); 829 - if (ret < 0) 830 - goto unlock; 831 - 832 - ret = set_format(subs, fmt); 833 - if (ret < 0) 834 - goto unlock; 835 - 836 897 unlock: 837 - snd_usb_unlock_shutdown(subs->stream->chip); 838 898 if (ret < 0) 839 - goto stop_pipeline; 840 - return ret; 899 + close_endpoints(chip, subs); 841 900 901 + snd_usb_unlock_shutdown(chip); 842 902 stop_pipeline: 843 - snd_media_stop_pipeline(subs); 903 + if (ret < 0) 904 + snd_media_stop_pipeline(subs); 905 + 844 906 return ret; 845 907 } 846 908 ··· 839 941 subs->cur_rate = 0; 840 942 subs->period_bytes = 0; 841 943 if (!snd_usb_lock_shutdown(chip)) { 842 - stop_endpoints(subs); 843 - sync_pending_stops(subs); 844 - snd_usb_endpoint_deactivate(subs->sync_endpoint); 845 - snd_usb_endpoint_deactivate(subs->data_endpoint); 846 - if (subs->data_endpoint) { 847 - subs->data_endpoint->sync_master = NULL; 848 - subs->data_endpoint = NULL; 849 - } 850 - subs->sync_endpoint = NULL; 944 + if (stop_endpoints(subs)) 945 + sync_pending_stops(subs); 946 + close_endpoints(chip, subs); 851 947 snd_usb_unlock_shutdown(chip); 852 948 } 853 949 ··· 857 965 { 858 966 struct snd_pcm_runtime *runtime = substream->runtime; 859 967 struct snd_usb_substream *subs = runtime->private_data; 860 - struct usb_host_interface *alts; 861 - struct usb_interface *iface; 968 + struct snd_usb_audio *chip = subs->stream->chip; 862 969 int ret; 863 970 864 - if (! subs->cur_audiofmt) { 865 - dev_err(&subs->dev->dev, "no format is specified!\n"); 866 - return -ENXIO; 867 - } 868 - 869 - ret = snd_usb_lock_shutdown(subs->stream->chip); 971 + ret = snd_usb_lock_shutdown(chip); 870 972 if (ret < 0) 871 973 return ret; 872 974 if (snd_BUG_ON(!subs->data_endpoint)) { ··· 868 982 goto unlock; 869 983 } 870 984 871 - ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0); 985 + ret = configure_endpoints(chip, subs); 872 986 if (ret < 0) 873 987 goto unlock; 874 - 875 - ret = set_format(subs, subs->cur_audiofmt); 876 - if (ret < 0) 877 - goto unlock; 878 - 879 - if (subs->need_setup_ep) { 880 - 881 - iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface); 882 - alts = &iface->altsetting[subs->cur_audiofmt->altset_idx]; 883 - ret = snd_usb_init_sample_rate(subs->stream->chip, 884 - subs->cur_audiofmt, 885 - subs->cur_rate); 886 - if (ret < 0) 887 - goto unlock; 888 - 889 - ret = configure_endpoint(subs); 890 - if (ret < 0) 891 - goto unlock; 892 - subs->need_setup_ep = false; 893 - } 894 - 895 - /* some unit conversions in runtime */ 896 - subs->data_endpoint->maxframesize = 897 - bytes_to_frames(runtime, subs->data_endpoint->maxpacksize); 898 - subs->data_endpoint->curframesize = 899 - bytes_to_frames(runtime, subs->data_endpoint->curpacksize); 900 988 901 989 /* reset the pointer */ 902 990 subs->hwptr_done = 0; ··· 885 1025 ret = start_endpoints(subs); 886 1026 887 1027 unlock: 888 - snd_usb_unlock_shutdown(subs->stream->chip); 1028 + snd_usb_unlock_shutdown(chip); 889 1029 return ret; 890 1030 } 891 1031 ··· 907 1047 SNDRV_PCM_INFO_INTERLEAVED | 908 1048 SNDRV_PCM_INFO_BLOCK_TRANSFER | 909 1049 SNDRV_PCM_INFO_PAUSE, 1050 + .channels_min = 1, 1051 + .channels_max = 256, 910 1052 .buffer_bytes_max = 1024 * 1024, 911 1053 .period_bytes_min = 64, 912 1054 .period_bytes_max = 512 * 1024, ··· 1112 1250 struct audioformat *fp; 1113 1251 int err; 1114 1252 1115 - subs->fixed_hw = 0; 1116 1253 list_for_each_entry(fp, &subs->fmt_list, list) { 1117 1254 ep = snd_usb_get_endpoint(chip, fp->endpoint); 1118 1255 if (ep && ep->cur_rate) ··· 1127 1266 1128 1267 found: 1129 1268 if (!find_format(&subs->fmt_list, ep->cur_format, ep->cur_rate, 1130 - ep->cur_channels, NULL)) { 1269 + ep->cur_channels, false, NULL)) { 1131 1270 usb_audio_dbg(chip, "EP 0x%x being used, but not applicable\n", 1132 1271 ep->ep_num); 1133 1272 return 0; ··· 1135 1274 1136 1275 usb_audio_dbg(chip, "EP 0x%x being used, using fixed params:\n", 1137 1276 ep->ep_num); 1138 - usb_audio_dbg(chip, "rate=%d, format=%s, channels=%d, period_size=%d, periods=%d\n", 1139 - ep->cur_rate, snd_pcm_format_name(ep->cur_format), 1140 - ep->cur_channels, ep->cur_period_frames, 1277 + usb_audio_dbg(chip, "rate=%d, period_size=%d, periods=%d\n", 1278 + ep->cur_rate, ep->cur_period_frames, 1141 1279 ep->cur_buffer_periods); 1142 1280 1143 - runtime->hw.formats = pcm_format_to_bits(ep->cur_format); 1281 + runtime->hw.formats = subs->formats; 1144 1282 runtime->hw.rate_min = runtime->hw.rate_max = ep->cur_rate; 1145 - runtime->hw.channels_min = runtime->hw.channels_max = 1146 - ep->cur_channels; 1147 1283 runtime->hw.rates = SNDRV_PCM_RATE_KNOT; 1148 1284 runtime->hw.periods_min = runtime->hw.periods_max = 1149 1285 ep->cur_buffer_periods; 1150 - subs->fixed_hw = 1; 1286 + 1287 + err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 1288 + hw_rule_channels, subs, 1289 + SNDRV_PCM_HW_PARAM_FORMAT, 1290 + SNDRV_PCM_HW_PARAM_RATE, 1291 + -1); 1292 + if (err < 0) 1293 + return err; 1151 1294 1152 1295 err = snd_pcm_hw_constraint_minmax(runtime, 1153 1296 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, ··· 1307 1442 1308 1443 snd_media_stop_pipeline(subs); 1309 1444 1310 - if (subs->interface >= 0 && 1311 - !snd_usb_lock_shutdown(subs->stream->chip)) { 1312 - usb_set_interface(subs->dev, subs->interface, 0); 1445 + if (!snd_usb_lock_shutdown(subs->stream->chip)) { 1313 1446 subs->interface = -1; 1314 1447 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1); 1315 1448 snd_usb_unlock_shutdown(subs->stream->chip); ··· 1686 1823 retire_playback_urb, 1687 1824 subs); 1688 1825 subs->running = 1; 1826 + dev_dbg(&subs->dev->dev, "%d:%d Start Playback PCM\n", 1827 + subs->cur_audiofmt->iface, 1828 + subs->cur_audiofmt->altsetting); 1689 1829 return 0; 1690 1830 case SNDRV_PCM_TRIGGER_SUSPEND: 1691 - subs->need_setup_fmt = true; 1692 - fallthrough; 1693 1831 case SNDRV_PCM_TRIGGER_STOP: 1694 1832 stop_endpoints(subs); 1695 1833 snd_usb_endpoint_set_callback(subs->data_endpoint, 1696 1834 NULL, NULL, NULL); 1697 1835 subs->running = 0; 1836 + dev_dbg(&subs->dev->dev, "%d:%d Stop Playback PCM\n", 1837 + subs->cur_audiofmt->iface, 1838 + subs->cur_audiofmt->altsetting); 1698 1839 return 0; 1699 1840 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 1700 1841 /* keep retire_data_urb for delay calculation */ ··· 1707 1840 retire_playback_urb, 1708 1841 subs); 1709 1842 subs->running = 0; 1843 + dev_dbg(&subs->dev->dev, "%d:%d Pause Playback PCM\n", 1844 + subs->cur_audiofmt->iface, 1845 + subs->cur_audiofmt->altsetting); 1710 1846 return 0; 1711 1847 } 1712 1848 ··· 1733 1863 NULL, retire_capture_urb, 1734 1864 subs); 1735 1865 subs->running = 1; 1866 + dev_dbg(&subs->dev->dev, "%d:%d Start Capture PCM\n", 1867 + subs->cur_audiofmt->iface, 1868 + subs->cur_audiofmt->altsetting); 1736 1869 return 0; 1737 1870 case SNDRV_PCM_TRIGGER_SUSPEND: 1738 - subs->need_setup_fmt = true; 1739 - fallthrough; 1740 1871 case SNDRV_PCM_TRIGGER_STOP: 1741 1872 stop_endpoints(subs); 1742 1873 fallthrough; ··· 1745 1874 snd_usb_endpoint_set_callback(subs->data_endpoint, 1746 1875 NULL, NULL, NULL); 1747 1876 subs->running = 0; 1877 + dev_dbg(&subs->dev->dev, "%d:%d Stop Capture PCM\n", 1878 + subs->cur_audiofmt->iface, 1879 + subs->cur_audiofmt->altsetting); 1748 1880 return 0; 1749 1881 } 1750 1882