.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | | - * This program is free software; you can redistribute it and/or modify |
---|
3 | | - * it under the terms of the GNU General Public License as published by |
---|
4 | | - * the Free Software Foundation; either version 2 of the License, or |
---|
5 | | - * (at your option) any later version. |
---|
6 | | - * |
---|
7 | | - * This program is distributed in the hope that it will be useful, |
---|
8 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | | - * GNU General Public License for more details. |
---|
11 | | - * |
---|
12 | | - * You should have received a copy of the GNU General Public License |
---|
13 | | - * along with this program; if not, write to the Free Software |
---|
14 | | - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
15 | | - * |
---|
16 | 3 | */ |
---|
17 | 4 | |
---|
18 | 5 | #include <linux/gfp.h> |
---|
.. | .. |
---|
86 | 73 | */ |
---|
87 | 74 | static void release_urb_ctx(struct snd_urb_ctx *u) |
---|
88 | 75 | { |
---|
89 | | - if (u->buffer_size) |
---|
| 76 | + if (u->urb && u->buffer_size) |
---|
90 | 77 | usb_free_coherent(u->ep->chip->dev, u->buffer_size, |
---|
91 | 78 | u->urb->transfer_buffer, |
---|
92 | 79 | u->urb->transfer_dma); |
---|
93 | 80 | usb_free_urb(u->urb); |
---|
94 | 81 | u->urb = NULL; |
---|
| 82 | + u->buffer_size = 0; |
---|
95 | 83 | } |
---|
96 | 84 | |
---|
97 | 85 | static const char *usb_error_string(int err) |
---|
.. | .. |
---|
137 | 125 | |
---|
138 | 126 | /* |
---|
139 | 127 | * For streaming based on information derived from sync endpoints, |
---|
140 | | - * prepare_outbound_urb_sizes() will call next_packet_size() to |
---|
| 128 | + * prepare_outbound_urb_sizes() will call slave_next_packet_size() to |
---|
141 | 129 | * determine the number of samples to be sent in the next packet. |
---|
142 | 130 | * |
---|
143 | | - * For implicit feedback, next_packet_size() is unused. |
---|
| 131 | + * For implicit feedback, slave_next_packet_size() is unused. |
---|
144 | 132 | */ |
---|
145 | | -int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep) |
---|
| 133 | +int snd_usb_endpoint_slave_next_packet_size(struct snd_usb_endpoint *ep) |
---|
146 | 134 | { |
---|
147 | 135 | unsigned long flags; |
---|
148 | 136 | int ret; |
---|
.. | .. |
---|
155 | 143 | + (ep->freqm << ep->datainterval); |
---|
156 | 144 | ret = min(ep->phase >> 16, ep->maxframesize); |
---|
157 | 145 | spin_unlock_irqrestore(&ep->lock, flags); |
---|
| 146 | + |
---|
| 147 | + return ret; |
---|
| 148 | +} |
---|
| 149 | + |
---|
| 150 | +/* |
---|
| 151 | + * For adaptive and synchronous endpoints, prepare_outbound_urb_sizes() |
---|
| 152 | + * will call next_packet_size() to determine the number of samples to be |
---|
| 153 | + * sent in the next packet. |
---|
| 154 | + */ |
---|
| 155 | +int snd_usb_endpoint_next_packet_size(struct snd_usb_endpoint *ep) |
---|
| 156 | +{ |
---|
| 157 | + int ret; |
---|
| 158 | + |
---|
| 159 | + if (ep->fill_max) |
---|
| 160 | + return ep->maxframesize; |
---|
| 161 | + |
---|
| 162 | + ep->sample_accum += ep->sample_rem; |
---|
| 163 | + if (ep->sample_accum >= ep->pps) { |
---|
| 164 | + ep->sample_accum -= ep->pps; |
---|
| 165 | + ret = ep->packsize[1]; |
---|
| 166 | + } else { |
---|
| 167 | + ret = ep->packsize[0]; |
---|
| 168 | + } |
---|
158 | 169 | |
---|
159 | 170 | return ret; |
---|
160 | 171 | } |
---|
.. | .. |
---|
203 | 214 | |
---|
204 | 215 | if (ctx->packet_size[i]) |
---|
205 | 216 | counts = ctx->packet_size[i]; |
---|
| 217 | + else if (ep->sync_master) |
---|
| 218 | + counts = snd_usb_endpoint_slave_next_packet_size(ep); |
---|
206 | 219 | else |
---|
207 | 220 | counts = snd_usb_endpoint_next_packet_size(ep); |
---|
208 | 221 | |
---|
.. | .. |
---|
306 | 319 | |
---|
307 | 320 | /* |
---|
308 | 321 | * Send output urbs that have been prepared previously. URBs are dequeued |
---|
309 | | - * from ep->ready_playback_urbs and in case there there aren't any available |
---|
| 322 | + * from ep->ready_playback_urbs and in case there aren't any available |
---|
310 | 323 | * or there are no packets that have been prepared, this function does |
---|
311 | 324 | * nothing. |
---|
312 | 325 | * |
---|
.. | .. |
---|
323 | 336 | while (test_bit(EP_FLAG_RUNNING, &ep->flags)) { |
---|
324 | 337 | |
---|
325 | 338 | unsigned long flags; |
---|
326 | | - struct snd_usb_packet_info *uninitialized_var(packet); |
---|
| 339 | + struct snd_usb_packet_info *packet; |
---|
327 | 340 | struct snd_urb_ctx *ctx = NULL; |
---|
328 | 341 | int err, i; |
---|
329 | 342 | |
---|
.. | .. |
---|
510 | 523 | |
---|
511 | 524 | list_add_tail(&ep->list, &chip->ep_list); |
---|
512 | 525 | |
---|
| 526 | + ep->is_implicit_feedback = 0; |
---|
| 527 | + |
---|
513 | 528 | __exit_unlock: |
---|
514 | 529 | mutex_unlock(&chip->mutex); |
---|
515 | 530 | |
---|
.. | .. |
---|
562 | 577 | { |
---|
563 | 578 | unsigned int i; |
---|
564 | 579 | |
---|
565 | | - if (!force && atomic_read(&ep->chip->shutdown)) /* to be sure... */ |
---|
566 | | - return -EBADFD; |
---|
567 | | - |
---|
568 | 580 | clear_bit(EP_FLAG_RUNNING, &ep->flags); |
---|
569 | 581 | |
---|
570 | 582 | INIT_LIST_HEAD(&ep->ready_playback_urbs); |
---|
.. | .. |
---|
601 | 613 | for (i = 0; i < ep->nurbs; i++) |
---|
602 | 614 | release_urb_ctx(&ep->urb[i]); |
---|
603 | 615 | |
---|
604 | | - if (ep->syncbuf) |
---|
605 | | - usb_free_coherent(ep->chip->dev, SYNC_URBS * 4, |
---|
606 | | - ep->syncbuf, ep->sync_dma); |
---|
| 616 | + usb_free_coherent(ep->chip->dev, SYNC_URBS * 4, |
---|
| 617 | + ep->syncbuf, ep->sync_dma); |
---|
607 | 618 | |
---|
608 | 619 | ep->syncbuf = NULL; |
---|
609 | 620 | ep->nurbs = 0; |
---|
| 621 | +} |
---|
| 622 | + |
---|
| 623 | +/* |
---|
| 624 | + * Check data endpoint for format differences |
---|
| 625 | + */ |
---|
| 626 | +static bool check_ep_params(struct snd_usb_endpoint *ep, |
---|
| 627 | + snd_pcm_format_t pcm_format, |
---|
| 628 | + unsigned int channels, |
---|
| 629 | + unsigned int period_bytes, |
---|
| 630 | + unsigned int frames_per_period, |
---|
| 631 | + unsigned int periods_per_buffer, |
---|
| 632 | + struct audioformat *fmt, |
---|
| 633 | + struct snd_usb_endpoint *sync_ep) |
---|
| 634 | +{ |
---|
| 635 | + unsigned int maxsize, minsize, packs_per_ms, max_packs_per_urb; |
---|
| 636 | + unsigned int max_packs_per_period, urbs_per_period, urb_packs; |
---|
| 637 | + unsigned int max_urbs; |
---|
| 638 | + int frame_bits = snd_pcm_format_physical_width(pcm_format) * channels; |
---|
| 639 | + int tx_length_quirk = (ep->chip->tx_length_quirk && |
---|
| 640 | + usb_pipeout(ep->pipe)); |
---|
| 641 | + bool ret = 1; |
---|
| 642 | + |
---|
| 643 | + if (pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE && fmt->dsd_dop) { |
---|
| 644 | + /* |
---|
| 645 | + * When operating in DSD DOP mode, the size of a sample frame |
---|
| 646 | + * in hardware differs from the actual physical format width |
---|
| 647 | + * because we need to make room for the DOP markers. |
---|
| 648 | + */ |
---|
| 649 | + frame_bits += channels << 3; |
---|
| 650 | + } |
---|
| 651 | + |
---|
| 652 | + ret = ret && (ep->datainterval == fmt->datainterval); |
---|
| 653 | + ret = ret && (ep->stride == frame_bits >> 3); |
---|
| 654 | + |
---|
| 655 | + switch (pcm_format) { |
---|
| 656 | + case SNDRV_PCM_FORMAT_U8: |
---|
| 657 | + ret = ret && (ep->silence_value == 0x80); |
---|
| 658 | + break; |
---|
| 659 | + case SNDRV_PCM_FORMAT_DSD_U8: |
---|
| 660 | + case SNDRV_PCM_FORMAT_DSD_U16_LE: |
---|
| 661 | + case SNDRV_PCM_FORMAT_DSD_U32_LE: |
---|
| 662 | + case SNDRV_PCM_FORMAT_DSD_U16_BE: |
---|
| 663 | + case SNDRV_PCM_FORMAT_DSD_U32_BE: |
---|
| 664 | + ret = ret && (ep->silence_value == 0x69); |
---|
| 665 | + break; |
---|
| 666 | + default: |
---|
| 667 | + ret = ret && (ep->silence_value == 0); |
---|
| 668 | + } |
---|
| 669 | + |
---|
| 670 | + /* assume max. frequency is 50% higher than nominal */ |
---|
| 671 | + ret = ret && (ep->freqmax == ep->freqn + (ep->freqn >> 1)); |
---|
| 672 | + /* Round up freqmax to nearest integer in order to calculate maximum |
---|
| 673 | + * packet size, which must represent a whole number of frames. |
---|
| 674 | + * This is accomplished by adding 0x0.ffff before converting the |
---|
| 675 | + * Q16.16 format into integer. |
---|
| 676 | + * In order to accurately calculate the maximum packet size when |
---|
| 677 | + * the data interval is more than 1 (i.e. ep->datainterval > 0), |
---|
| 678 | + * multiply by the data interval prior to rounding. For instance, |
---|
| 679 | + * a freqmax of 41 kHz will result in a max packet size of 6 (5.125) |
---|
| 680 | + * frames with a data interval of 1, but 11 (10.25) frames with a |
---|
| 681 | + * data interval of 2. |
---|
| 682 | + * (ep->freqmax << ep->datainterval overflows at 8.192 MHz for the |
---|
| 683 | + * maximum datainterval value of 3, at USB full speed, higher for |
---|
| 684 | + * USB high speed, noting that ep->freqmax is in units of |
---|
| 685 | + * frames per packet in Q16.16 format.) |
---|
| 686 | + */ |
---|
| 687 | + maxsize = (((ep->freqmax << ep->datainterval) + 0xffff) >> 16) * |
---|
| 688 | + (frame_bits >> 3); |
---|
| 689 | + if (tx_length_quirk) |
---|
| 690 | + maxsize += sizeof(__le32); /* Space for length descriptor */ |
---|
| 691 | + /* but wMaxPacketSize might reduce this */ |
---|
| 692 | + if (ep->maxpacksize && ep->maxpacksize < maxsize) { |
---|
| 693 | + /* whatever fits into a max. size packet */ |
---|
| 694 | + unsigned int data_maxsize = maxsize = ep->maxpacksize; |
---|
| 695 | + |
---|
| 696 | + if (tx_length_quirk) |
---|
| 697 | + /* Need to remove the length descriptor to calc freq */ |
---|
| 698 | + data_maxsize -= sizeof(__le32); |
---|
| 699 | + ret = ret && (ep->freqmax == (data_maxsize / (frame_bits >> 3)) |
---|
| 700 | + << (16 - ep->datainterval)); |
---|
| 701 | + } |
---|
| 702 | + |
---|
| 703 | + if (ep->fill_max) |
---|
| 704 | + ret = ret && (ep->curpacksize == ep->maxpacksize); |
---|
| 705 | + else |
---|
| 706 | + ret = ret && (ep->curpacksize == maxsize); |
---|
| 707 | + |
---|
| 708 | + if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL) { |
---|
| 709 | + packs_per_ms = 8 >> ep->datainterval; |
---|
| 710 | + max_packs_per_urb = MAX_PACKS_HS; |
---|
| 711 | + } else { |
---|
| 712 | + packs_per_ms = 1; |
---|
| 713 | + max_packs_per_urb = MAX_PACKS; |
---|
| 714 | + } |
---|
| 715 | + if (sync_ep && !snd_usb_endpoint_implicit_feedback_sink(ep)) |
---|
| 716 | + max_packs_per_urb = min(max_packs_per_urb, |
---|
| 717 | + 1U << sync_ep->syncinterval); |
---|
| 718 | + max_packs_per_urb = max(1u, max_packs_per_urb >> ep->datainterval); |
---|
| 719 | + |
---|
| 720 | + /* |
---|
| 721 | + * Capture endpoints need to use small URBs because there's no way |
---|
| 722 | + * to tell in advance where the next period will end, and we don't |
---|
| 723 | + * want the next URB to complete much after the period ends. |
---|
| 724 | + * |
---|
| 725 | + * Playback endpoints with implicit sync much use the same parameters |
---|
| 726 | + * as their corresponding capture endpoint. |
---|
| 727 | + */ |
---|
| 728 | + if (usb_pipein(ep->pipe) || |
---|
| 729 | + snd_usb_endpoint_implicit_feedback_sink(ep)) { |
---|
| 730 | + |
---|
| 731 | + urb_packs = packs_per_ms; |
---|
| 732 | + /* |
---|
| 733 | + * Wireless devices can poll at a max rate of once per 4ms. |
---|
| 734 | + * For dataintervals less than 5, increase the packet count to |
---|
| 735 | + * allow the host controller to use bursting to fill in the |
---|
| 736 | + * gaps. |
---|
| 737 | + */ |
---|
| 738 | + if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_WIRELESS) { |
---|
| 739 | + int interval = ep->datainterval; |
---|
| 740 | + |
---|
| 741 | + while (interval < 5) { |
---|
| 742 | + urb_packs <<= 1; |
---|
| 743 | + ++interval; |
---|
| 744 | + } |
---|
| 745 | + } |
---|
| 746 | + /* make capture URBs <= 1 ms and smaller than a period */ |
---|
| 747 | + urb_packs = min(max_packs_per_urb, urb_packs); |
---|
| 748 | + while (urb_packs > 1 && urb_packs * maxsize >= period_bytes) |
---|
| 749 | + urb_packs >>= 1; |
---|
| 750 | + ret = ret && (ep->nurbs == MAX_URBS); |
---|
| 751 | + |
---|
| 752 | + /* |
---|
| 753 | + * Playback endpoints without implicit sync are adjusted so that |
---|
| 754 | + * a period fits as evenly as possible in the smallest number of |
---|
| 755 | + * URBs. The total number of URBs is adjusted to the size of the |
---|
| 756 | + * ALSA buffer, subject to the MAX_URBS and MAX_QUEUE limits. |
---|
| 757 | + */ |
---|
| 758 | + } else { |
---|
| 759 | + /* determine how small a packet can be */ |
---|
| 760 | + minsize = (ep->freqn >> (16 - ep->datainterval)) * |
---|
| 761 | + (frame_bits >> 3); |
---|
| 762 | + /* with sync from device, assume it can be 12% lower */ |
---|
| 763 | + if (sync_ep) |
---|
| 764 | + minsize -= minsize >> 3; |
---|
| 765 | + minsize = max(minsize, 1u); |
---|
| 766 | + |
---|
| 767 | + /* how many packets will contain an entire ALSA period? */ |
---|
| 768 | + max_packs_per_period = DIV_ROUND_UP(period_bytes, minsize); |
---|
| 769 | + |
---|
| 770 | + /* how many URBs will contain a period? */ |
---|
| 771 | + urbs_per_period = DIV_ROUND_UP(max_packs_per_period, |
---|
| 772 | + max_packs_per_urb); |
---|
| 773 | + /* how many packets are needed in each URB? */ |
---|
| 774 | + urb_packs = DIV_ROUND_UP(max_packs_per_period, urbs_per_period); |
---|
| 775 | + |
---|
| 776 | + /* limit the number of frames in a single URB */ |
---|
| 777 | + ret = ret && (ep->max_urb_frames == |
---|
| 778 | + DIV_ROUND_UP(frames_per_period, urbs_per_period)); |
---|
| 779 | + |
---|
| 780 | + /* try to use enough URBs to contain an entire ALSA buffer */ |
---|
| 781 | + max_urbs = min((unsigned) MAX_URBS, |
---|
| 782 | + MAX_QUEUE * packs_per_ms / urb_packs); |
---|
| 783 | + ret = ret && (ep->nurbs == min(max_urbs, |
---|
| 784 | + urbs_per_period * periods_per_buffer)); |
---|
| 785 | + } |
---|
| 786 | + |
---|
| 787 | + ret = ret && (ep->datainterval == fmt->datainterval); |
---|
| 788 | + ret = ret && (ep->maxpacksize == fmt->maxpacksize); |
---|
| 789 | + ret = ret && |
---|
| 790 | + (ep->fill_max == !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX)); |
---|
| 791 | + |
---|
| 792 | + return ret; |
---|
610 | 793 | } |
---|
611 | 794 | |
---|
612 | 795 | /* |
---|
.. | .. |
---|
816 | 999 | if (!ep->syncbuf) |
---|
817 | 1000 | return -ENOMEM; |
---|
818 | 1001 | |
---|
| 1002 | + ep->nurbs = SYNC_URBS; |
---|
819 | 1003 | for (i = 0; i < SYNC_URBS; i++) { |
---|
820 | 1004 | struct snd_urb_ctx *u = &ep->urb[i]; |
---|
821 | 1005 | u->index = i; |
---|
.. | .. |
---|
834 | 1018 | u->urb->context = u; |
---|
835 | 1019 | u->urb->complete = snd_complete_urb; |
---|
836 | 1020 | } |
---|
837 | | - |
---|
838 | | - ep->nurbs = SYNC_URBS; |
---|
839 | 1021 | |
---|
840 | 1022 | return 0; |
---|
841 | 1023 | |
---|
.. | .. |
---|
874 | 1056 | int err; |
---|
875 | 1057 | |
---|
876 | 1058 | if (ep->use_count != 0) { |
---|
877 | | - usb_audio_warn(ep->chip, |
---|
878 | | - "Unable to change format on ep #%x: already in use\n", |
---|
879 | | - ep->ep_num); |
---|
880 | | - return -EBUSY; |
---|
| 1059 | + bool check = ep->is_implicit_feedback && |
---|
| 1060 | + check_ep_params(ep, pcm_format, |
---|
| 1061 | + channels, period_bytes, |
---|
| 1062 | + period_frames, buffer_periods, |
---|
| 1063 | + fmt, sync_ep); |
---|
| 1064 | + |
---|
| 1065 | + if (!check) { |
---|
| 1066 | + usb_audio_warn(ep->chip, |
---|
| 1067 | + "Unable to change format on ep #%x: already in use\n", |
---|
| 1068 | + ep->ep_num); |
---|
| 1069 | + return -EBUSY; |
---|
| 1070 | + } |
---|
| 1071 | + |
---|
| 1072 | + usb_audio_dbg(ep->chip, |
---|
| 1073 | + "Ep #%x already in use as implicit feedback but format not changed\n", |
---|
| 1074 | + ep->ep_num); |
---|
| 1075 | + return 0; |
---|
881 | 1076 | } |
---|
882 | 1077 | |
---|
883 | 1078 | /* release old buffers, if any */ |
---|
.. | .. |
---|
887 | 1082 | ep->maxpacksize = fmt->maxpacksize; |
---|
888 | 1083 | ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX); |
---|
889 | 1084 | |
---|
890 | | - if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL) |
---|
| 1085 | + if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL) { |
---|
891 | 1086 | ep->freqn = get_usb_full_speed_rate(rate); |
---|
892 | | - else |
---|
| 1087 | + ep->pps = 1000 >> ep->datainterval; |
---|
| 1088 | + } else { |
---|
893 | 1089 | ep->freqn = get_usb_high_speed_rate(rate); |
---|
| 1090 | + ep->pps = 8000 >> ep->datainterval; |
---|
| 1091 | + } |
---|
| 1092 | + |
---|
| 1093 | + ep->sample_rem = rate % ep->pps; |
---|
| 1094 | + ep->packsize[0] = rate / ep->pps; |
---|
| 1095 | + ep->packsize[1] = (rate + (ep->pps - 1)) / ep->pps; |
---|
894 | 1096 | |
---|
895 | 1097 | /* calculate the frequency in 16.16 format */ |
---|
896 | 1098 | ep->freqm = ep->freqn; |
---|
.. | .. |
---|
949 | 1151 | ep->active_mask = 0; |
---|
950 | 1152 | ep->unlink_mask = 0; |
---|
951 | 1153 | ep->phase = 0; |
---|
| 1154 | + ep->sample_accum = 0; |
---|
952 | 1155 | |
---|
953 | 1156 | snd_usb_endpoint_start_quirk(ep); |
---|
954 | 1157 | |
---|