hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/sound/usb/endpoint.c
....@@ -1,18 +1,5 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
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
- *
163 */
174
185 #include <linux/gfp.h>
....@@ -86,12 +73,13 @@
8673 */
8774 static void release_urb_ctx(struct snd_urb_ctx *u)
8875 {
89
- if (u->buffer_size)
76
+ if (u->urb && u->buffer_size)
9077 usb_free_coherent(u->ep->chip->dev, u->buffer_size,
9178 u->urb->transfer_buffer,
9279 u->urb->transfer_dma);
9380 usb_free_urb(u->urb);
9481 u->urb = NULL;
82
+ u->buffer_size = 0;
9583 }
9684
9785 static const char *usb_error_string(int err)
....@@ -137,12 +125,12 @@
137125
138126 /*
139127 * 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
141129 * determine the number of samples to be sent in the next packet.
142130 *
143
- * For implicit feedback, next_packet_size() is unused.
131
+ * For implicit feedback, slave_next_packet_size() is unused.
144132 */
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)
146134 {
147135 unsigned long flags;
148136 int ret;
....@@ -155,6 +143,29 @@
155143 + (ep->freqm << ep->datainterval);
156144 ret = min(ep->phase >> 16, ep->maxframesize);
157145 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
+ }
158169
159170 return ret;
160171 }
....@@ -203,6 +214,8 @@
203214
204215 if (ctx->packet_size[i])
205216 counts = ctx->packet_size[i];
217
+ else if (ep->sync_master)
218
+ counts = snd_usb_endpoint_slave_next_packet_size(ep);
206219 else
207220 counts = snd_usb_endpoint_next_packet_size(ep);
208221
....@@ -306,7 +319,7 @@
306319
307320 /*
308321 * 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
310323 * or there are no packets that have been prepared, this function does
311324 * nothing.
312325 *
....@@ -323,7 +336,7 @@
323336 while (test_bit(EP_FLAG_RUNNING, &ep->flags)) {
324337
325338 unsigned long flags;
326
- struct snd_usb_packet_info *uninitialized_var(packet);
339
+ struct snd_usb_packet_info *packet;
327340 struct snd_urb_ctx *ctx = NULL;
328341 int err, i;
329342
....@@ -510,6 +523,8 @@
510523
511524 list_add_tail(&ep->list, &chip->ep_list);
512525
526
+ ep->is_implicit_feedback = 0;
527
+
513528 __exit_unlock:
514529 mutex_unlock(&chip->mutex);
515530
....@@ -562,9 +577,6 @@
562577 {
563578 unsigned int i;
564579
565
- if (!force && atomic_read(&ep->chip->shutdown)) /* to be sure... */
566
- return -EBADFD;
567
-
568580 clear_bit(EP_FLAG_RUNNING, &ep->flags);
569581
570582 INIT_LIST_HEAD(&ep->ready_playback_urbs);
....@@ -601,12 +613,183 @@
601613 for (i = 0; i < ep->nurbs; i++)
602614 release_urb_ctx(&ep->urb[i]);
603615
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);
607618
608619 ep->syncbuf = NULL;
609620 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;
610793 }
611794
612795 /*
....@@ -816,6 +999,7 @@
816999 if (!ep->syncbuf)
8171000 return -ENOMEM;
8181001
1002
+ ep->nurbs = SYNC_URBS;
8191003 for (i = 0; i < SYNC_URBS; i++) {
8201004 struct snd_urb_ctx *u = &ep->urb[i];
8211005 u->index = i;
....@@ -834,8 +1018,6 @@
8341018 u->urb->context = u;
8351019 u->urb->complete = snd_complete_urb;
8361020 }
837
-
838
- ep->nurbs = SYNC_URBS;
8391021
8401022 return 0;
8411023
....@@ -874,10 +1056,23 @@
8741056 int err;
8751057
8761058 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;
8811076 }
8821077
8831078 /* release old buffers, if any */
....@@ -887,10 +1082,17 @@
8871082 ep->maxpacksize = fmt->maxpacksize;
8881083 ep->fill_max = !!(fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX);
8891084
890
- if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL)
1085
+ if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL) {
8911086 ep->freqn = get_usb_full_speed_rate(rate);
892
- else
1087
+ ep->pps = 1000 >> ep->datainterval;
1088
+ } else {
8931089 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;
8941096
8951097 /* calculate the frequency in 16.16 format */
8961098 ep->freqm = ep->freqn;
....@@ -949,6 +1151,7 @@
9491151 ep->active_mask = 0;
9501152 ep->unlink_mask = 0;
9511153 ep->phase = 0;
1154
+ ep->sample_accum = 0;
9521155
9531156 snd_usb_endpoint_start_quirk(ep);
9541157