blob: b0961ebd71f4bad456c26d787f78f2fd74c22a20 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Daniel Macke5779992010-03-04 19:46:13 +01002/*
Daniel Macke5779992010-03-04 19:46:13 +01003 */
4
5#include <linux/init.h>
Stephen Rothwell9966dda2010-03-29 19:01:48 +11006#include <linux/slab.h>
Daniel Mack44dcbbb2013-04-17 00:01:39 +08007#include <linux/bitrev.h>
Daniel Mackedcd3632012-04-12 13:51:12 +02008#include <linux/ratelimit.h>
Daniel Macke5779992010-03-04 19:46:13 +01009#include <linux/usb.h>
10#include <linux/usb/audio.h>
Daniel Mack7e847892010-03-11 21:13:20 +010011#include <linux/usb/audio-v2.h>
Daniel Macke5779992010-03-04 19:46:13 +010012
13#include <sound/core.h>
14#include <sound/pcm.h>
15#include <sound/pcm_params.h>
16
17#include "usbaudio.h"
18#include "card.h"
19#include "quirks.h"
Daniel Mackc731bc92011-09-14 12:46:57 +020020#include "endpoint.h"
Daniel Macke5779992010-03-04 19:46:13 +010021#include "helper.h"
22#include "pcm.h"
Daniel Mack79f920f2010-05-31 14:51:31 +020023#include "clock.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010024#include "power.h"
Shuah Khan66354f12019-04-01 20:40:22 -040025#include "media.h"
Daniel Macke5779992010-03-04 19:46:13 +010026
Daniel Mackedcd3632012-04-12 13:51:12 +020027#define SUBSTREAM_FLAG_DATA_EP_STARTED 0
28#define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
29
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050030/* return the estimated delay based on USB frame counters */
31snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
32 unsigned int rate)
33{
34 int current_frame_number;
35 int frame_diff;
36 int est_delay;
37
Takashi Iwai48779a02012-11-23 16:00:37 +010038 if (!subs->last_delay)
39 return 0; /* short path */
40
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050041 current_frame_number = usb_get_current_frame_number(subs->dev);
42 /*
43 * HCD implementations use different widths, use lower 8 bits.
44 * The delay will be managed up to 256ms, which is more than
45 * enough
46 */
47 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
48
49 /* Approximation based on number of samples per USB frame (ms),
50 some truncation for 44.1 but the estimate is good enough */
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060051 est_delay = frame_diff * rate / 1000;
52 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
53 est_delay = subs->last_delay - est_delay;
54 else
55 est_delay = subs->last_delay + est_delay;
56
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050057 if (est_delay < 0)
58 est_delay = 0;
59 return est_delay;
60}
61
Daniel Macke5779992010-03-04 19:46:13 +010062/*
63 * return the current pcm pointer. just based on the hwptr_done value.
64 */
65static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
66{
Takashi Iwaie92be812018-05-27 15:09:15 +020067 struct snd_usb_substream *subs = substream->runtime->private_data;
Daniel Macke5779992010-03-04 19:46:13 +010068 unsigned int hwptr_done;
69
Takashi Iwai47ab1542015-08-25 16:09:00 +020070 if (atomic_read(&subs->stream->chip->shutdown))
Takashi Iwai978520b2012-10-12 15:12:55 +020071 return SNDRV_PCM_POS_XRUN;
Daniel Macke5779992010-03-04 19:46:13 +010072 spin_lock(&subs->lock);
73 hwptr_done = subs->hwptr_done;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060074 substream->runtime->delay = snd_usb_pcm_delay(subs,
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050075 substream->runtime->rate);
Daniel Macke5779992010-03-04 19:46:13 +010076 spin_unlock(&subs->lock);
77 return hwptr_done / (substream->runtime->frame_bits >> 3);
78}
79
80/*
81 * find a matching audio format
82 */
Takashi Iwai5a6c3e12020-11-23 09:53:16 +010083static 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)
Daniel Macke5779992010-03-04 19:46:13 +010088{
Eldad Zack88766f02013-04-03 23:18:49 +020089 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +010090 struct audioformat *found = NULL;
91 int cur_attr = 0, attr;
92
Takashi Iwai5a6c3e12020-11-23 09:53:16 +010093 list_for_each_entry(fp, fmt_list_head, list) {
94 if (!(fp->formats & pcm_format_to_bits(format)))
Clemens Ladisch015eb0b2010-03-04 19:46:15 +010095 continue;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +010096 if (fp->channels != channels)
Daniel Macke5779992010-03-04 19:46:13 +010097 continue;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +010098 if (rate < fp->rate_min || rate > fp->rate_max)
Daniel Macke5779992010-03-04 19:46:13 +010099 continue;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100100 if (!(fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
Daniel Macke5779992010-03-04 19:46:13 +0100101 unsigned int i;
102 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100103 if (fp->rate_table[i] == rate)
Daniel Macke5779992010-03-04 19:46:13 +0100104 break;
105 if (i >= fp->nr_rates)
106 continue;
107 }
108 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100109 if (!found) {
Daniel Macke5779992010-03-04 19:46:13 +0100110 found = fp;
111 cur_attr = attr;
112 continue;
113 }
114 /* avoid async out and adaptive in if the other method
115 * supports the same format.
116 * this is a workaround for the case like
117 * M-audio audiophile USB.
118 */
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100119 if (subs && attr != cur_attr) {
Daniel Macke5779992010-03-04 19:46:13 +0100120 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
121 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
122 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
123 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
124 continue;
125 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
126 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
127 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
128 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
129 found = fp;
130 cur_attr = attr;
131 continue;
132 }
133 }
134 /* find the format with the largest max. packet size */
135 if (fp->maxpacksize > found->maxpacksize) {
136 found = fp;
137 cur_attr = attr;
138 }
139 }
140 return found;
141}
142
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100143static struct audioformat *find_substream_format(struct snd_usb_substream *subs)
144{
145 return find_format(&subs->fmt_list, subs->pcm_format, subs->cur_rate,
146 subs->channels, subs);
147}
148
Daniel Mack767d75a2010-03-04 19:46:17 +0100149static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
150 struct usb_host_interface *alts,
151 struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +0100152{
Daniel Mack767d75a2010-03-04 19:46:17 +0100153 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100154 unsigned int ep;
155 unsigned char data[1];
156 int err;
157
Takashi Iwai447d6272016-03-15 15:20:58 +0100158 if (get_iface_desc(alts)->bNumEndpoints < 1)
159 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100160 ep = get_endpoint(alts, 0)->bEndpointAddress;
Daniel Mack767d75a2010-03-04 19:46:17 +0100161
Daniel Mack767d75a2010-03-04 19:46:17 +0100162 data[0] = 1;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200163 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
164 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
165 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
166 data, sizeof(data));
167 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100168 usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
169 iface, ep);
Daniel Mack767d75a2010-03-04 19:46:17 +0100170 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100171 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100172
Daniel Macke5779992010-03-04 19:46:13 +0100173 return 0;
174}
175
Daniel Mack92c25612010-05-26 18:11:39 +0200176static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
177 struct usb_host_interface *alts,
178 struct audioformat *fmt)
179{
180 struct usb_device *dev = chip->dev;
181 unsigned char data[1];
Daniel Mack92c25612010-05-26 18:11:39 +0200182 int err;
183
Daniel Mack92c25612010-05-26 18:11:39 +0200184 data[0] = 1;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200185 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
186 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
187 UAC2_EP_CS_PITCH << 8, 0,
188 data, sizeof(data));
189 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100190 usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
191 iface, fmt->altsetting);
Daniel Mack92c25612010-05-26 18:11:39 +0200192 return err;
193 }
194
195 return 0;
196}
197
Daniel Mack767d75a2010-03-04 19:46:17 +0100198/*
Daniel Mack92c25612010-05-26 18:11:39 +0200199 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100200 */
201int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
202 struct usb_host_interface *alts,
203 struct audioformat *fmt)
204{
Daniel Mack92c25612010-05-26 18:11:39 +0200205 /* if endpoint doesn't have pitch control, bail out */
206 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
207 return 0;
208
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100209 switch (fmt->protocol) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100210 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200211 default:
Daniel Mack767d75a2010-03-04 19:46:17 +0100212 return init_pitch_v1(chip, iface, alts, fmt);
213
214 case UAC_VERSION_2:
Daniel Mack92c25612010-05-26 18:11:39 +0200215 return init_pitch_v2(chip, iface, alts, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100216 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100217}
218
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200219static int start_endpoints(struct snd_usb_substream *subs)
Daniel Mackedcd3632012-04-12 13:51:12 +0200220{
221 int err;
222
223 if (!subs->data_endpoint)
224 return -EINVAL;
225
226 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
227 struct snd_usb_endpoint *ep = subs->data_endpoint;
228
Takashi Iwaie93e8902020-11-23 09:53:13 +0100229 dev_dbg(&subs->dev->dev, "Starting data EP 0x%x\n", ep->ep_num);
Daniel Mackedcd3632012-04-12 13:51:12 +0200230
231 ep->data_subs = subs;
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200232 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200233 if (err < 0) {
234 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
235 return err;
236 }
237 }
238
239 if (subs->sync_endpoint &&
240 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
241 struct snd_usb_endpoint *ep = subs->sync_endpoint;
242
Takashi Iwaie93e8902020-11-23 09:53:13 +0100243 dev_dbg(&subs->dev->dev, "Starting sync EP 0x%x\n", ep->ep_num);
Daniel Mackedcd3632012-04-12 13:51:12 +0200244
245 ep->sync_slave = subs->data_endpoint;
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200246 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200247 if (err < 0) {
248 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
Takashi Iwai18035032020-11-23 09:53:12 +0100249 ep->sync_slave = NULL;
Daniel Mackedcd3632012-04-12 13:51:12 +0200250 return err;
251 }
252 }
253
254 return 0;
255}
256
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100257static void sync_pending_stops(struct snd_usb_substream *subs)
258{
259 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
260 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
261}
262
263static void stop_endpoints(struct snd_usb_substream *subs)
Daniel Mackedcd3632012-04-12 13:51:12 +0200264{
Takashi Iwai18035032020-11-23 09:53:12 +0100265 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
Takashi Iwaie93e8902020-11-23 09:53:13 +0100266 dev_dbg(&subs->dev->dev, "Stopping sync EP 0x%x\n",
267 subs->sync_endpoint->ep_num);
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100268 snd_usb_endpoint_stop(subs->sync_endpoint);
Takashi Iwai18035032020-11-23 09:53:12 +0100269 subs->sync_endpoint->sync_slave = NULL;
270 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200271
Takashi Iwaie93e8902020-11-23 09:53:13 +0100272 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
273 dev_dbg(&subs->dev->dev, "Stopping data EP 0x%x\n",
274 subs->data_endpoint->ep_num);
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100275 snd_usb_endpoint_stop(subs->data_endpoint);
Takashi Iwaie93e8902020-11-23 09:53:13 +0100276 }
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100277}
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100278
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100279/* PCM sync_stop callback */
280static int snd_usb_pcm_sync_stop(struct snd_pcm_substream *substream)
281{
282 struct snd_usb_substream *subs = substream->runtime->private_data;
283
284 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
285 sync_pending_stops(subs);
286 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100287 }
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100288 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +0200289}
290
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100291/* Check whether the given iface:altsetting points to an implicit fb source */
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100292static bool search_generic_implicit_fb(struct snd_usb_audio *chip, int ifnum,
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100293 unsigned int altsetting,
294 struct usb_host_interface **altsp,
295 unsigned int *ep)
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100296{
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100297 struct usb_host_interface *alts;
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100298 struct usb_interface_descriptor *altsd;
299 struct usb_endpoint_descriptor *epd;
300
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100301 alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100302 if (!alts)
303 return false;
304 altsd = get_iface_desc(alts);
305 if (altsd->bInterfaceClass != USB_CLASS_AUDIO ||
306 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING ||
307 altsd->bInterfaceProtocol != UAC_VERSION_2 ||
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100308 altsd->bNumEndpoints < 1)
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100309 return false;
310 epd = get_endpoint(alts, 0);
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100311 if (!usb_endpoint_is_isoc_in(epd) ||
312 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
313 USB_ENDPOINT_USAGE_IMPLICIT_FB)
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100314 return false;
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100315 *ep = epd->bEndpointAddress;
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100316 *altsp = alts;
317 return true;
318}
319
320/* Like the function above, but specific to Roland with vendor class and hack */
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100321static bool search_roland_implicit_fb(struct snd_usb_audio *chip, int ifnum,
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100322 unsigned int altsetting,
323 struct usb_host_interface **altsp,
324 unsigned int *ep)
325{
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100326 struct usb_host_interface *alts;
327 struct usb_interface_descriptor *altsd;
328 struct usb_endpoint_descriptor *epd;
329
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100330 alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100331 if (!alts)
332 return false;
333 altsd = get_iface_desc(alts);
334 if (altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
335 (altsd->bInterfaceSubClass != 2 &&
336 altsd->bInterfaceProtocol != 2) ||
337 altsd->bNumEndpoints < 1)
338 return false;
339 epd = get_endpoint(alts, 0);
340 if (!usb_endpoint_is_isoc_in(epd) ||
341 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
342 USB_ENDPOINT_USAGE_IMPLICIT_FB)
343 return false;
344 *ep = epd->bEndpointAddress;
345 *altsp = alts;
346 return true;
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100347}
348
Manuel Reinhardt2bc16b92019-01-31 15:32:35 +0100349/* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
350 * applies. Returns 1 if a quirk was found.
351 */
Takashi Iwaif6581c02020-11-23 09:53:14 +0100352static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
353 struct audioformat *fmt,
Takashi Iwaif6581c02020-11-23 09:53:14 +0100354 struct usb_host_interface *alts)
Daniel Macke5779992010-03-04 19:46:13 +0100355{
Takashi Iwaif6581c02020-11-23 09:53:14 +0100356 struct usb_device *dev = chip->dev;
357 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100358 struct usb_interface *iface;
Takashi Iwaif6581c02020-11-23 09:53:14 +0100359 unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Eldad Zacka60945f2013-08-03 10:50:18 +0200360 unsigned int ep;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500361 unsigned int ifnum;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200362
Takashi Iwaif6581c02020-11-23 09:53:14 +0100363 switch (chip->usb_id) {
Eldad Zackca10a7e2012-11-28 23:55:41 +0100364 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500365 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Geoffrey D. Bennett0938eca2020-11-04 22:27:17 +1030366 case USB_ID(0x22f0, 0x0006): /* Allen&Heath Qu-16 */
Eldad Zack914273c2013-08-03 10:50:21 +0200367 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500368 ifnum = 3;
369 goto add_sync_ep_from_ifnum;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200370 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
371 case USB_ID(0x0763, 0x2081):
Eldad Zack914273c2013-08-03 10:50:21 +0200372 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500373 ifnum = 2;
374 goto add_sync_ep_from_ifnum;
375 case USB_ID(0x2466, 0x8003): /* Fractal Audio Axe-Fx II */
Geoffrey D. Bennett26201dd2020-11-04 22:37:05 +1030376 case USB_ID(0x0499, 0x172a): /* Yamaha MODX */
Alberto Aguirre17f08b02016-12-08 00:36:48 -0600377 ep = 0x86;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500378 ifnum = 2;
379 goto add_sync_ep_from_ifnum;
Alberto Aguirre91a85612018-04-18 09:35:35 -0500380 case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx III */
381 ep = 0x81;
382 ifnum = 2;
383 goto add_sync_ep_from_ifnum;
Keith Winsteinf15cfca2020-10-25 22:05:47 -0700384 case USB_ID(0x1686, 0xf029): /* Zoom UAC-2 */
385 ep = 0x82;
386 ifnum = 2;
387 goto add_sync_ep_from_ifnum;
Takashi Iwai1a157182019-08-20 08:58:12 +0200388 case USB_ID(0x1397, 0x0001): /* Behringer UFX1604 */
Alberto Aguirre103e9622018-04-18 09:35:34 -0500389 case USB_ID(0x1397, 0x0002): /* Behringer UFX1204 */
Lassi Ylikojola5e35dc02018-02-09 16:51:36 +0200390 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500391 ifnum = 1;
392 goto add_sync_ep_from_ifnum;
Alexander Tsoy2edb84e2020-02-29 18:18:15 +0300393 case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook II/IIc */
394 /* MicroBook IIc */
395 if (altsd->bInterfaceClass == USB_CLASS_AUDIO)
396 return 0;
397
398 /* MicroBook II */
Manuel Reinhardta6340902019-02-28 20:34:04 +0100399 ep = 0x84;
400 ifnum = 0;
401 goto add_sync_ep_from_ifnum;
Alexander Tsoyc2491772020-01-15 18:13:58 +0300402 case USB_ID(0x07fd, 0x0008): /* MOTU M Series */
Laurence Tratt3da87ec2020-06-21 08:50:05 +0100403 case USB_ID(0x31e9, 0x0001): /* Solid State Logic SSL2 */
Laurence Tratte7585db2020-06-12 12:18:07 +0100404 case USB_ID(0x31e9, 0x0002): /* Solid State Logic SSL2+ */
Joshua Sivec7c5b8922020-08-25 18:55:18 +0200405 case USB_ID(0x0499, 0x172f): /* Steinberg UR22C */
Pavel Hofmanb6a1e782020-07-03 12:04:33 +0200406 case USB_ID(0x0d9a, 0x00df): /* RTX6001 */
Alexander Tsoyc2491772020-01-15 18:13:58 +0300407 ep = 0x81;
408 ifnum = 2;
409 goto add_sync_ep_from_ifnum;
Dmitry Panchenko7fccfec2020-06-01 13:22:24 +0300410 case USB_ID(0x2b73, 0x000a): /* Pioneer DJ DJM-900NXS2 */
František Kučera14335d82020-08-25 17:31:13 +0200411 case USB_ID(0x2b73, 0x0017): /* Pioneer DJ DJM-250MK2 */
Dmitry Panchenko7fccfec2020-06-01 13:22:24 +0300412 ep = 0x82;
413 ifnum = 0;
414 goto add_sync_ep_from_ifnum;
Szabolcs Szőke7571b6a2019-10-11 19:19:36 +0200415 case USB_ID(0x0582, 0x01d8): /* BOSS Katana */
416 /* BOSS Katana amplifiers do not need quirks */
417 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200418 }
Alberto Aguirre103e9622018-04-18 09:35:34 -0500419
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100420 /* Generic UAC2 implicit feedback */
421 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
422 altsd->bInterfaceClass == USB_CLASS_AUDIO &&
423 altsd->bInterfaceProtocol == UAC_VERSION_2 &&
424 altsd->bNumEndpoints == 1) {
425 ifnum = altsd->bInterfaceNumber + 1;
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100426 if (search_generic_implicit_fb(chip, ifnum,
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100427 altsd->bAlternateSetting,
428 &alts, &ep))
429 goto add_sync_ep;
430 }
431
432 /* Roland/BOSS implicit feedback with vendor spec class */
Eldad Zack914273c2013-08-03 10:50:21 +0200433 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100434 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
435 altsd->bInterfaceProtocol == 2 &&
436 altsd->bNumEndpoints == 1 &&
Takashi Iwaif6581c02020-11-23 09:53:14 +0100437 USB_ID_VENDOR(chip->usb_id) == 0x0582 /* Roland */) {
438 ifnum = altsd->bInterfaceNumber + 1;
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100439 if (search_roland_implicit_fb(chip, ifnum,
Takashi Iwaif6581c02020-11-23 09:53:14 +0100440 altsd->bAlternateSetting,
441 &alts, &ep))
442 goto add_sync_ep;
443 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200444
Eldad Zacka60945f2013-08-03 10:50:18 +0200445 /* No quirk */
446 return 0;
447
Alberto Aguirre103e9622018-04-18 09:35:34 -0500448add_sync_ep_from_ifnum:
449 iface = usb_ifnum_to_if(dev, ifnum);
450
Johan Hovold5d1b7122020-01-14 09:39:53 +0100451 if (!iface || iface->num_altsetting < 2)
Takashi Iwaif6581c02020-11-23 09:53:14 +0100452 return 0;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500453
454 alts = &iface->altsetting[1];
455
Eldad Zacka60945f2013-08-03 10:50:18 +0200456add_sync_ep:
Takashi Iwaif6581c02020-11-23 09:53:14 +0100457 fmt->sync_ep = ep;
458 fmt->sync_iface = ifnum;
459 fmt->sync_altsetting = alts->desc.bAlternateSetting;
460 fmt->implicit_fb = 1;
461 dev_dbg(&dev->dev, "%d:%d: found implicit_fb sync_ep=%x, iface=%d, alt=%d\n",
462 fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface,
463 fmt->sync_altsetting);
Eldad Zacka60945f2013-08-03 10:50:18 +0200464
Manuel Reinhardt2bc16b92019-01-31 15:32:35 +0100465 return 1;
Eldad Zacka60945f2013-08-03 10:50:18 +0200466}
467
Takashi Iwaif6581c02020-11-23 09:53:14 +0100468int snd_usb_audioformat_set_sync_ep(struct snd_usb_audio *chip,
469 struct audioformat *fmt)
Eldad Zacka60945f2013-08-03 10:50:18 +0200470{
Takashi Iwaif6581c02020-11-23 09:53:14 +0100471 struct usb_device *dev = chip->dev;
Takashi Iwaif6581c02020-11-23 09:53:14 +0100472 struct usb_host_interface *alts;
473 struct usb_interface_descriptor *altsd;
474 unsigned int ep, attr, sync_attr;
475 bool is_playback;
Eldad Zacka60945f2013-08-03 10:50:18 +0200476 int err;
477
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100478 alts = snd_usb_get_host_interface(chip, fmt->iface, fmt->altsetting);
Takashi Iwaif6581c02020-11-23 09:53:14 +0100479 if (!alts)
480 return 0;
481 altsd = get_iface_desc(alts);
482
483 is_playback = !(get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
484 if (is_playback) {
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100485 err = audioformat_implicit_fb_quirk(chip, fmt, alts);
Takashi Iwaif6581c02020-11-23 09:53:14 +0100486 if (err > 0)
487 return 0;
488 }
Manuel Reinhardt2bc16b92019-01-31 15:32:35 +0100489
Eldad Zackf34d0652013-08-03 10:50:19 +0200490 if (altsd->bNumEndpoints < 2)
491 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200492
Takashi Iwaif6581c02020-11-23 09:53:14 +0100493 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500494 if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
495 attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
Eldad Zackf34d0652013-08-03 10:50:19 +0200496 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
497 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200498
Takashi Iwaif6581c02020-11-23 09:53:14 +0100499 sync_attr = get_endpoint(alts, 1)->bmAttributes;
500
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500501 /*
502 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
503 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
504 * error fall back to SYNC mode and don't create sync endpoint
505 */
506
Eldad Zackf34d0652013-08-03 10:50:19 +0200507 /* check sync-pipe endpoint */
508 /* ... and check descriptor size before accessing bSynchAddress
509 because there is a version of the SB Audigy 2 NX firmware lacking
510 the audio fields in the endpoint descriptors */
Takashi Iwaif6581c02020-11-23 09:53:14 +0100511 if ((sync_attr & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
Eldad Zackf34d0652013-08-03 10:50:19 +0200512 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zack95fec882013-08-03 10:50:20 +0200513 get_endpoint(alts, 1)->bSynchAddress != 0)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100514 dev_err(&dev->dev,
515 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
516 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200517 get_endpoint(alts, 1)->bmAttributes,
518 get_endpoint(alts, 1)->bLength,
519 get_endpoint(alts, 1)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500520 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
521 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200522 return -EINVAL;
Daniel Mackedcd3632012-04-12 13:51:12 +0200523 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200524 ep = get_endpoint(alts, 1)->bEndpointAddress;
Eldad Zack95fec882013-08-03 10:50:20 +0200525 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Ard van Breemen1b341212019-08-02 13:52:14 +0200526 get_endpoint(alts, 0)->bSynchAddress != 0 &&
Eldad Zackf34d0652013-08-03 10:50:19 +0200527 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
528 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100529 dev_err(&dev->dev,
530 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
531 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200532 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500533 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
534 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200535 return -EINVAL;
536 }
537
Takashi Iwaif6581c02020-11-23 09:53:14 +0100538 fmt->sync_ep = ep;
539 fmt->sync_iface = altsd->bInterfaceNumber;
540 fmt->sync_altsetting = altsd->bAlternateSetting;
541 if ((sync_attr & USB_ENDPOINT_USAGE_MASK) == USB_ENDPOINT_USAGE_IMPLICIT_FB)
542 fmt->implicit_fb = 1;
543
544 dev_dbg(&dev->dev, "%d:%d: found sync_ep=0x%x, iface=%d, alt=%d, implicit_fb=%d\n",
545 fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface,
546 fmt->sync_altsetting, fmt->implicit_fb);
547
548 return 0;
549}
550
551static int set_sync_endpoint(struct snd_usb_substream *subs,
552 struct audioformat *fmt)
553{
554 struct usb_device *dev = subs->dev;
Takashi Iwaif6581c02020-11-23 09:53:14 +0100555 struct usb_host_interface *alts;
556 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
557 unsigned int ep;
558 int err;
559
560 subs->sync_endpoint = NULL;
561 subs->data_endpoint->sync_master = NULL;
562
563 ep = fmt->sync_ep;
564 if (!ep)
565 return 0;
566
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100567 alts = snd_usb_get_host_interface(subs->stream->chip, fmt->sync_iface,
568 fmt->altsetting);
Takashi Iwaif6581c02020-11-23 09:53:14 +0100569 if (!alts)
570 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200571
Takashi Iwai54cb3192020-11-23 09:53:20 +0100572 subs->sync_endpoint = snd_usb_get_endpoint(subs->stream->chip, ep);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500573 if (!subs->sync_endpoint) {
Takashi Iwaif6581c02020-11-23 09:53:14 +0100574 if (is_playback &&
575 (fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) == USB_ENDPOINT_SYNC_NONE)
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500576 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200577 return -EINVAL;
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500578 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200579
Takashi Iwai54cb3192020-11-23 09:53:20 +0100580 subs->sync_endpoint->iface = fmt->sync_iface;
581 subs->sync_endpoint->altsetting = fmt->sync_altsetting;
Takashi Iwaif6581c02020-11-23 09:53:14 +0100582 subs->sync_endpoint->is_implicit_feedback = fmt->implicit_fb;
Erwin Burema10ce77e2020-05-10 20:29:11 +0200583
Eldad Zackf34d0652013-08-03 10:50:19 +0200584 subs->data_endpoint->sync_master = subs->sync_endpoint;
Daniel Macke5779992010-03-04 19:46:13 +0100585
Takashi Iwai54cb3192020-11-23 09:53:20 +0100586 snd_usb_endpoint_set_syncinterval(subs->stream->chip, subs->sync_endpoint, alts);
587
Takashi Iwai5fd255f2020-11-23 09:53:19 +0100588 if (!subs->sync_endpoint->use_count &&
589 (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
590 subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting)) {
Takashi Iwai4974b792020-11-23 09:53:08 +0100591 err = usb_set_interface(subs->dev,
592 subs->sync_endpoint->iface,
593 subs->sync_endpoint->altsetting);
594 if (err < 0)
595 return err;
596 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
597 subs->sync_endpoint->iface,
598 subs->sync_endpoint->altsetting);
599 snd_usb_set_interface_quirk(dev);
600 }
601
Eldad Zack71bb64c2013-08-03 10:50:17 +0200602 return 0;
603}
604
605/*
606 * find a matching format and set up the interface
607 */
608static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
609{
610 struct usb_device *dev = subs->dev;
611 struct usb_host_interface *alts;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200612 struct usb_interface *iface;
Takashi Iwai5fd255f2020-11-23 09:53:19 +0100613 struct snd_usb_endpoint *ep;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200614 int err;
615
616 iface = usb_ifnum_to_if(dev, fmt->iface);
617 if (WARN_ON(!iface))
618 return -EINVAL;
Takashi Iwaib099b962018-05-02 09:36:28 +0200619 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
Johan Hovold01412542019-12-20 10:31:34 +0100620 if (WARN_ON(!alts))
Eldad Zack71bb64c2013-08-03 10:50:17 +0200621 return -EINVAL;
622
Hui Wang92adc962019-12-18 21:26:50 +0800623 if (fmt == subs->cur_audiofmt && !subs->need_setup_fmt)
Eldad Zack71bb64c2013-08-03 10:50:17 +0200624 return 0;
625
Takashi Iwai5fd255f2020-11-23 09:53:19 +0100626 /* shared EP with implicit fb */
627 if (fmt->implicit_fb && !subs->need_setup_fmt) {
Takashi Iwai54cb3192020-11-23 09:53:20 +0100628 ep = snd_usb_get_endpoint(subs->stream->chip, fmt->endpoint);
Takashi Iwai5fd255f2020-11-23 09:53:19 +0100629 if (ep && ep->use_count > 0)
630 goto add_data_ep;
631 }
632
Eldad Zack71bb64c2013-08-03 10:50:17 +0200633 /* close the old interface */
Hui Wang92adc962019-12-18 21:26:50 +0800634 if (subs->interface >= 0 && (subs->interface != fmt->iface || subs->need_setup_fmt)) {
Takashi Iwai98215052020-11-23 09:53:21 +0100635 err = usb_set_interface(subs->dev, subs->interface, 0);
636 if (err < 0) {
637 dev_err(&dev->dev,
638 "%d:%d: return to setting 0 failed (%d)\n",
639 fmt->iface, fmt->altsetting, err);
640 return -EIO;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200641 }
642 subs->interface = -1;
643 subs->altset_idx = 0;
644 }
645
Hui Wang92adc962019-12-18 21:26:50 +0800646 if (subs->need_setup_fmt)
647 subs->need_setup_fmt = false;
648
Eldad Zack71bb64c2013-08-03 10:50:17 +0200649 /* set interface */
Takashi Iwaib099b962018-05-02 09:36:28 +0200650 if (iface->cur_altsetting != alts) {
Jurgen Kramer6874daa2014-11-28 17:32:54 +0100651 err = snd_usb_select_mode_quirk(subs, fmt);
652 if (err < 0)
653 return -EIO;
654
Eldad Zack71bb64c2013-08-03 10:50:17 +0200655 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
656 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100657 dev_err(&dev->dev,
658 "%d:%d: usb_set_interface failed (%d)\n",
659 fmt->iface, fmt->altsetting, err);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200660 return -EIO;
661 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100662 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
663 fmt->iface, fmt->altsetting);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200664 snd_usb_set_interface_quirk(dev);
665 }
666
Takashi Iwai5fd255f2020-11-23 09:53:19 +0100667 subs->need_setup_ep = true;
668
669 add_data_ep:
Takashi Iwaib099b962018-05-02 09:36:28 +0200670 subs->interface = fmt->iface;
671 subs->altset_idx = fmt->altset_idx;
Takashi Iwai54cb3192020-11-23 09:53:20 +0100672 subs->data_endpoint = snd_usb_get_endpoint(subs->stream->chip,
673 fmt->endpoint);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200674 if (!subs->data_endpoint)
675 return -EINVAL;
Takashi Iwai54cb3192020-11-23 09:53:20 +0100676 subs->data_endpoint->iface = fmt->iface;
677 subs->data_endpoint->altsetting = fmt->altsetting;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200678
Takashi Iwaif6581c02020-11-23 09:53:14 +0100679 err = set_sync_endpoint(subs, fmt);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200680 if (err < 0)
681 return err;
682
Takashi Iwai5fd255f2020-11-23 09:53:19 +0100683 if (subs->need_setup_ep) {
684 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
685 if (err < 0)
686 return err;
687 }
Daniel Macke5779992010-03-04 19:46:13 +0100688
689 subs->cur_audiofmt = fmt;
690
691 snd_usb_set_format_quirk(subs, fmt);
692
Daniel Macke5779992010-03-04 19:46:13 +0100693 return 0;
694}
695
696/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100697 * Return the score of matching two audioformats.
698 * Veto the audioformat if:
699 * - It has no channels for some reason.
700 * - Requested PCM format is not supported.
701 * - Requested sample rate is not supported.
702 */
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100703static int match_endpoint_audioformats(struct snd_usb_substream *subs,
704 struct audioformat *fp,
705 struct audioformat *match, int rate,
706 snd_pcm_format_t pcm_format)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100707{
708 int i;
709 int score = 0;
710
711 if (fp->channels < 1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100712 dev_dbg(&subs->dev->dev,
713 "%s: (fmt @%p) no channels\n", __func__, fp);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100714 return 0;
715 }
716
Eldad Zack74c34ca2013-04-23 01:00:41 +0200717 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100718 dev_dbg(&subs->dev->dev,
719 "%s: (fmt @%p) no match for format %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100720 fp, pcm_format);
721 return 0;
722 }
723
724 for (i = 0; i < fp->nr_rates; i++) {
725 if (fp->rate_table[i] == rate) {
726 score++;
727 break;
728 }
729 }
730 if (!score) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100731 dev_dbg(&subs->dev->dev,
732 "%s: (fmt @%p) no match for rate %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100733 fp, rate);
734 return 0;
735 }
736
737 if (fp->channels == match->channels)
738 score++;
739
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100740 dev_dbg(&subs->dev->dev,
741 "%s: (fmt @%p) score %d\n", __func__, fp, score);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100742
743 return score;
744}
745
746/*
747 * Configure the sync ep using the rate and pcm format of the data ep.
748 */
749static int configure_sync_endpoint(struct snd_usb_substream *subs)
750{
Eldad Zack0d9741c2012-12-03 20:30:09 +0100751 struct audioformat *fp;
752 struct audioformat *sync_fp = NULL;
753 int cur_score = 0;
754 int sync_period_bytes = subs->period_bytes;
755 struct snd_usb_substream *sync_subs =
756 &subs->stream->substream[subs->direction ^ 1];
757
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100758 if (subs->fixed_hw ||
759 !subs->sync_endpoint->is_implicit_feedback) {
760 sync_fp = subs->cur_audiofmt;
761 goto configure;
762 }
763
764 sync_fp = find_format(&sync_subs->fmt_list, subs->pcm_format,
765 subs->cur_rate, subs->channels, NULL);
766 if (sync_fp)
767 goto configure;
Takashi Iwai31be5422013-01-10 14:06:38 +0100768
Eldad Zack0d9741c2012-12-03 20:30:09 +0100769 /* Try to find the best matching audioformat. */
770 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100771 int score = match_endpoint_audioformats(subs,
772 fp, subs->cur_audiofmt,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100773 subs->cur_rate, subs->pcm_format);
774
775 if (score > cur_score) {
776 sync_fp = fp;
777 cur_score = score;
778 }
779 }
780
781 if (unlikely(sync_fp == NULL)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100782 dev_err(&subs->dev->dev,
783 "%s: no valid audioformat for sync ep %x found\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100784 __func__, sync_subs->ep_num);
785 return -EINVAL;
786 }
787
788 /*
789 * Recalculate the period bytes if channel number differ between
790 * data and sync ep audioformat.
791 */
792 if (sync_fp->channels != subs->channels) {
793 sync_period_bytes = (subs->period_bytes / subs->channels) *
794 sync_fp->channels;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100795 dev_dbg(&subs->dev->dev,
796 "%s: adjusted sync ep period bytes (%d -> %d)\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100797 __func__, subs->period_bytes, sync_period_bytes);
798 }
799
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100800 configure:
801 return snd_usb_endpoint_set_params(subs->sync_endpoint,
802 subs->pcm_format,
803 sync_fp->channels,
804 sync_period_bytes,
805 subs->period_frames,
806 subs->buffer_periods,
807 subs->cur_rate,
808 sync_fp,
809 NULL);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100810}
811
812/*
Dylan Reid61a70952012-09-18 09:49:48 -0700813 * configure endpoint params
814 *
815 * called during initial setup and upon resume
816 */
817static int configure_endpoint(struct snd_usb_substream *subs)
818{
819 int ret;
820
Dylan Reid61a70952012-09-18 09:49:48 -0700821 /* format changed */
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100822 stop_endpoints(subs);
823 sync_pending_stops(subs);
Dylan Reid61a70952012-09-18 09:49:48 -0700824 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
825 subs->pcm_format,
826 subs->channels,
827 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400828 subs->period_frames,
829 subs->buffer_periods,
Dylan Reid61a70952012-09-18 09:49:48 -0700830 subs->cur_rate,
831 subs->cur_audiofmt,
832 subs->sync_endpoint);
833 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200834 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700835
836 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100837 ret = configure_sync_endpoint(subs);
838
Dylan Reid61a70952012-09-18 09:49:48 -0700839 return ret;
840}
841
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100842static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state)
843{
844 int ret;
845
846 if (!subs->str_pd)
847 return 0;
848
849 ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
850 if (ret < 0) {
851 dev_err(&subs->dev->dev,
852 "Cannot change Power Domain ID: %d to state: %d. Err: %d\n",
853 subs->str_pd->pd_id, state, ret);
854 return ret;
855 }
856
857 return 0;
858}
859
860int snd_usb_pcm_suspend(struct snd_usb_stream *as)
861{
862 int ret;
863
864 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2);
865 if (ret < 0)
866 return ret;
867
868 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2);
869 if (ret < 0)
870 return ret;
871
872 return 0;
873}
874
875int snd_usb_pcm_resume(struct snd_usb_stream *as)
876{
877 int ret;
878
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100879 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1);
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100880 if (ret < 0)
881 return ret;
882
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100883 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1);
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100884 if (ret < 0)
885 return ret;
886
887 return 0;
888}
889
Dylan Reid61a70952012-09-18 09:49:48 -0700890/*
Daniel Macke5779992010-03-04 19:46:13 +0100891 * hw_params callback
892 *
893 * allocate a buffer and set the given audio format.
894 *
895 * so far we use a physically linear buffer although packetize transfer
896 * doesn't need a continuous area.
897 * if sg buffer is supported on the later version of alsa, we'll follow
898 * that.
899 */
900static int snd_usb_hw_params(struct snd_pcm_substream *substream,
901 struct snd_pcm_hw_params *hw_params)
902{
903 struct snd_usb_substream *subs = substream->runtime->private_data;
904 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700905 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100906
Shuah Khan66354f12019-04-01 20:40:22 -0400907 ret = snd_media_start_pipeline(subs);
908 if (ret)
909 return ret;
910
Dylan Reid61a70952012-09-18 09:49:48 -0700911 subs->pcm_format = params_format(hw_params);
912 subs->period_bytes = params_period_bytes(hw_params);
Alan Stern976b6c02013-09-24 15:51:58 -0400913 subs->period_frames = params_period_size(hw_params);
914 subs->buffer_periods = params_periods(hw_params);
Dylan Reid61a70952012-09-18 09:49:48 -0700915 subs->channels = params_channels(hw_params);
916 subs->cur_rate = params_rate(hw_params);
917
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100918 fmt = find_substream_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100919 if (!fmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100920 dev_dbg(&subs->dev->dev,
921 "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700922 subs->pcm_format, subs->cur_rate, subs->channels);
Shuah Khan66354f12019-04-01 20:40:22 -0400923 ret = -EINVAL;
924 goto stop_pipeline;
Daniel Macke5779992010-03-04 19:46:13 +0100925 }
926
Takashi Iwai47ab1542015-08-25 16:09:00 +0200927 ret = snd_usb_lock_shutdown(subs->stream->chip);
928 if (ret < 0)
Shuah Khan66354f12019-04-01 20:40:22 -0400929 goto stop_pipeline;
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100930
931 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
Takashi Iwai978520b2012-10-12 15:12:55 +0200932 if (ret < 0)
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100933 goto unlock;
934
935 ret = set_format(subs, fmt);
936 if (ret < 0)
937 goto unlock;
Daniel Macke5779992010-03-04 19:46:13 +0100938
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100939 unlock:
940 snd_usb_unlock_shutdown(subs->stream->chip);
Shuah Khan66354f12019-04-01 20:40:22 -0400941 if (ret < 0)
942 goto stop_pipeline;
943 return ret;
944
945 stop_pipeline:
946 snd_media_stop_pipeline(subs);
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100947 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100948}
949
950/*
951 * hw_free callback
952 *
953 * reset the audio format and release the buffer
954 */
955static int snd_usb_hw_free(struct snd_pcm_substream *substream)
956{
957 struct snd_usb_substream *subs = substream->runtime->private_data;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100958 struct snd_usb_audio *chip = subs->stream->chip;
Daniel Macke5779992010-03-04 19:46:13 +0100959
Shuah Khan66354f12019-04-01 20:40:22 -0400960 snd_media_stop_pipeline(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100961 subs->cur_audiofmt = NULL;
962 subs->cur_rate = 0;
963 subs->period_bytes = 0;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100964 if (!snd_usb_lock_shutdown(chip)) {
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100965 stop_endpoints(subs);
966 sync_pending_stops(subs);
Eldad Zack26de5d02013-10-06 22:31:07 +0200967 snd_usb_endpoint_deactivate(subs->sync_endpoint);
968 snd_usb_endpoint_deactivate(subs->data_endpoint);
Takashi Iwai18035032020-11-23 09:53:12 +0100969 if (subs->data_endpoint) {
970 subs->data_endpoint->sync_master = NULL;
971 subs->data_endpoint = NULL;
972 }
973 subs->sync_endpoint = NULL;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100974 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200975 }
Takashi Iwaif274baa2018-05-27 13:01:17 +0200976
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +0100977 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100978}
979
980/*
981 * prepare callback
982 *
983 * only a few subtle things...
984 */
985static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
986{
987 struct snd_pcm_runtime *runtime = substream->runtime;
988 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700989 struct usb_host_interface *alts;
990 struct usb_interface *iface;
991 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100992
993 if (! subs->cur_audiofmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100994 dev_err(&subs->dev->dev, "no format is specified!\n");
Daniel Macke5779992010-03-04 19:46:13 +0100995 return -ENXIO;
996 }
997
Takashi Iwai47ab1542015-08-25 16:09:00 +0200998 ret = snd_usb_lock_shutdown(subs->stream->chip);
999 if (ret < 0)
1000 return ret;
Takashi Iwai978520b2012-10-12 15:12:55 +02001001 if (snd_BUG_ON(!subs->data_endpoint)) {
1002 ret = -EIO;
1003 goto unlock;
1004 }
Daniel Mackedcd3632012-04-12 13:51:12 +02001005
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001006 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
1007 if (ret < 0)
1008 goto unlock;
1009
Dylan Reid61a70952012-09-18 09:49:48 -07001010 ret = set_format(subs, subs->cur_audiofmt);
1011 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +02001012 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -07001013
Takashi Iwai384dc0852012-09-18 14:49:31 +02001014 if (subs->need_setup_ep) {
Daniel Girnus1e2e3fe2016-12-06 14:46:15 +09001015
1016 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
1017 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
1018 ret = snd_usb_init_sample_rate(subs->stream->chip,
1019 subs->cur_audiofmt->iface,
1020 alts,
1021 subs->cur_audiofmt,
1022 subs->cur_rate);
1023 if (ret < 0)
1024 goto unlock;
1025
Takashi Iwai384dc0852012-09-18 14:49:31 +02001026 ret = configure_endpoint(subs);
1027 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +02001028 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +02001029 subs->need_setup_ep = false;
1030 }
Dylan Reid61a70952012-09-18 09:49:48 -07001031
Daniel Macke5779992010-03-04 19:46:13 +01001032 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +02001033 subs->data_endpoint->maxframesize =
1034 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
1035 subs->data_endpoint->curframesize =
1036 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +01001037
1038 /* reset the pointer */
1039 subs->hwptr_done = 0;
1040 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -05001041 subs->last_delay = 0;
1042 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001043 runtime->delay = 0;
1044
Daniel Mackedcd3632012-04-12 13:51:12 +02001045 /* for playback, submit the URBs now; otherwise, the first hwptr_done
1046 * updates for all URBs would happen at the same time when starting */
1047 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +02001048 ret = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +02001049
Takashi Iwai978520b2012-10-12 15:12:55 +02001050 unlock:
Takashi Iwai47ab1542015-08-25 16:09:00 +02001051 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +02001052 return ret;
Daniel Macke5779992010-03-04 19:46:13 +01001053}
1054
Takashi Iwai7ec827b2020-11-23 09:53:18 +01001055/*
1056 * h/w constraints
1057 */
1058
1059#ifdef HW_CONST_DEBUG
1060#define hwc_debug(fmt, args...) pr_debug(fmt, ##args)
1061#else
1062#define hwc_debug(fmt, args...) do { } while(0)
1063#endif
1064
Bhumika Goyalaaffbf72017-08-17 14:45:59 +05301065static const struct snd_pcm_hardware snd_usb_hardware =
Daniel Macke5779992010-03-04 19:46:13 +01001066{
1067 .info = SNDRV_PCM_INFO_MMAP |
1068 SNDRV_PCM_INFO_MMAP_VALID |
1069 SNDRV_PCM_INFO_BATCH |
1070 SNDRV_PCM_INFO_INTERLEAVED |
1071 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1072 SNDRV_PCM_INFO_PAUSE,
1073 .buffer_bytes_max = 1024 * 1024,
1074 .period_bytes_min = 64,
1075 .period_bytes_max = 512 * 1024,
1076 .periods_min = 2,
1077 .periods_max = 1024,
1078};
1079
1080static int hw_check_valid_format(struct snd_usb_substream *subs,
1081 struct snd_pcm_hw_params *params,
1082 struct audioformat *fp)
1083{
1084 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1085 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1086 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1087 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001088 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +01001089 unsigned int ptime;
1090
1091 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001092 snd_mask_none(&check_fmts);
1093 check_fmts.bits[0] = (u32)fp->formats;
1094 check_fmts.bits[1] = (u32)(fp->formats >> 32);
1095 snd_mask_intersect(&check_fmts, fmts);
1096 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +01001097 hwc_debug(" > check: no supported format %d\n", fp->format);
1098 return 0;
1099 }
1100 /* check the channels */
1101 if (fp->channels < ct->min || fp->channels > ct->max) {
1102 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1103 return 0;
1104 }
1105 /* check the rate is within the range */
1106 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1107 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1108 return 0;
1109 }
1110 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1111 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1112 return 0;
1113 }
1114 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +02001115 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +01001116 ptime = 125 * (1 << fp->datainterval);
1117 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
1118 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
1119 return 0;
1120 }
1121 }
1122 return 1;
1123}
1124
Takashi Iwai7726dce2020-11-23 09:53:17 +01001125static int apply_hw_params_minmax(struct snd_interval *it, unsigned int rmin,
1126 unsigned int rmax)
Daniel Macke5779992010-03-04 19:46:13 +01001127{
Daniel Macke5779992010-03-04 19:46:13 +01001128 int changed;
Daniel Macke5779992010-03-04 19:46:13 +01001129
Takashi Iwaibc4e94a2020-11-23 09:53:07 +01001130 if (rmin > rmax) {
Daniel Macke5779992010-03-04 19:46:13 +01001131 hwc_debug(" --> get empty\n");
1132 it->empty = 1;
1133 return -EINVAL;
1134 }
1135
1136 changed = 0;
1137 if (it->min < rmin) {
1138 it->min = rmin;
1139 it->openmin = 0;
1140 changed = 1;
1141 }
1142 if (it->max > rmax) {
1143 it->max = rmax;
1144 it->openmax = 0;
1145 changed = 1;
1146 }
1147 if (snd_interval_checkempty(it)) {
1148 it->empty = 1;
1149 return -EINVAL;
1150 }
1151 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1152 return changed;
1153}
1154
Takashi Iwai7726dce2020-11-23 09:53:17 +01001155static int hw_rule_rate(struct snd_pcm_hw_params *params,
1156 struct snd_pcm_hw_rule *rule)
1157{
1158 struct snd_usb_substream *subs = rule->private;
1159 struct audioformat *fp;
1160 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1161 unsigned int rmin, rmax, r;
1162 int i;
1163
1164 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1165 rmin = UINT_MAX;
1166 rmax = 0;
1167 list_for_each_entry(fp, &subs->fmt_list, list) {
1168 if (!hw_check_valid_format(subs, params, fp))
1169 continue;
1170 if (fp->rate_table && fp->nr_rates) {
1171 for (i = 0; i < fp->nr_rates; i++) {
1172 r = fp->rate_table[i];
1173 if (!snd_interval_test(it, r))
1174 continue;
1175 rmin = min(rmin, r);
1176 rmax = max(rmax, r);
1177 }
1178 } else {
1179 rmin = min(rmin, fp->rate_min);
1180 rmax = max(rmax, fp->rate_max);
1181 }
1182 }
1183
1184 return apply_hw_params_minmax(it, rmin, rmax);
1185}
1186
Daniel Macke5779992010-03-04 19:46:13 +01001187
1188static int hw_rule_channels(struct snd_pcm_hw_params *params,
1189 struct snd_pcm_hw_rule *rule)
1190{
1191 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001192 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001193 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1194 unsigned int rmin, rmax;
Daniel Macke5779992010-03-04 19:46:13 +01001195
1196 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
Takashi Iwai7726dce2020-11-23 09:53:17 +01001197 rmin = UINT_MAX;
1198 rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001199 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001200 if (!hw_check_valid_format(subs, params, fp))
1201 continue;
Takashi Iwai7726dce2020-11-23 09:53:17 +01001202 rmin = min(rmin, fp->channels);
1203 rmax = max(rmax, fp->channels);
Daniel Macke5779992010-03-04 19:46:13 +01001204 }
1205
Takashi Iwai7726dce2020-11-23 09:53:17 +01001206 return apply_hw_params_minmax(it, rmin, rmax);
Daniel Macke5779992010-03-04 19:46:13 +01001207}
1208
1209static int hw_rule_format(struct snd_pcm_hw_params *params,
1210 struct snd_pcm_hw_rule *rule)
1211{
1212 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001213 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001214 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1215 u64 fbits;
1216 u32 oldbits[2];
1217 int changed;
1218
1219 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1220 fbits = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001221 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001222 if (!hw_check_valid_format(subs, params, fp))
1223 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001224 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +01001225 }
1226
1227 oldbits[0] = fmt->bits[0];
1228 oldbits[1] = fmt->bits[1];
1229 fmt->bits[0] &= (u32)fbits;
1230 fmt->bits[1] &= (u32)(fbits >> 32);
1231 if (!fmt->bits[0] && !fmt->bits[1]) {
1232 hwc_debug(" --> get empty\n");
1233 return -EINVAL;
1234 }
1235 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1236 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1237 return changed;
1238}
1239
1240static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1241 struct snd_pcm_hw_rule *rule)
1242{
1243 struct snd_usb_substream *subs = rule->private;
1244 struct audioformat *fp;
1245 struct snd_interval *it;
1246 unsigned char min_datainterval;
1247 unsigned int pmin;
Daniel Macke5779992010-03-04 19:46:13 +01001248
1249 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1250 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1251 min_datainterval = 0xff;
1252 list_for_each_entry(fp, &subs->fmt_list, list) {
1253 if (!hw_check_valid_format(subs, params, fp))
1254 continue;
1255 min_datainterval = min(min_datainterval, fp->datainterval);
1256 }
1257 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +02001258 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +01001259 it->empty = 1;
1260 return -EINVAL;
1261 }
1262 pmin = 125 * (1 << min_datainterval);
Takashi Iwai7726dce2020-11-23 09:53:17 +01001263
1264 return apply_hw_params_minmax(it, pmin, UINT_MAX);
Daniel Macke5779992010-03-04 19:46:13 +01001265}
1266
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001267/* apply PCM hw constraints from the concurrent sync EP */
1268static int apply_hw_constraint_from_sync(struct snd_pcm_runtime *runtime,
1269 struct snd_usb_substream *subs)
1270{
1271 struct snd_usb_audio *chip = subs->stream->chip;
1272 struct snd_usb_endpoint *ep;
1273 struct audioformat *fp;
1274 int err;
1275
1276 subs->fixed_hw = 0;
1277 list_for_each_entry(fp, &subs->fmt_list, list) {
Takashi Iwai54cb3192020-11-23 09:53:20 +01001278 ep = snd_usb_get_endpoint(chip, fp->endpoint);
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001279 if (ep && ep->cur_rate)
1280 goto found;
1281 if (!fp->implicit_fb)
1282 continue;
1283 /* for the implicit fb, check the sync ep as well */
Takashi Iwai54cb3192020-11-23 09:53:20 +01001284 ep = snd_usb_get_endpoint(chip, fp->sync_ep);
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001285 if (ep && ep->cur_rate)
1286 goto found;
1287 }
1288 return 0;
1289
1290 found:
1291 if (!find_format(&subs->fmt_list, ep->cur_format, ep->cur_rate,
1292 ep->cur_channels, NULL)) {
1293 usb_audio_dbg(chip, "EP 0x%x being used, but not applicable\n",
1294 ep->ep_num);
1295 return 0;
1296 }
1297
1298 usb_audio_dbg(chip, "EP 0x%x being used, using fixed params:\n",
1299 ep->ep_num);
1300 usb_audio_dbg(chip, "rate=%d, format=%s, channels=%d, period_size=%d, periods=%d\n",
1301 ep->cur_rate, snd_pcm_format_name(ep->cur_format),
1302 ep->cur_channels, ep->cur_period_frames,
1303 ep->cur_buffer_periods);
1304
1305 runtime->hw.formats = pcm_format_to_bits(ep->cur_format);
1306 runtime->hw.rate_min = runtime->hw.rate_max = ep->cur_rate;
1307 runtime->hw.channels_min = runtime->hw.channels_max =
1308 ep->cur_channels;
1309 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1310 runtime->hw.periods_min = runtime->hw.periods_max =
1311 ep->cur_buffer_periods;
1312 subs->fixed_hw = 1;
1313
1314 err = snd_pcm_hw_constraint_minmax(runtime,
1315 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1316 ep->cur_period_frames,
1317 ep->cur_period_frames);
1318 if (err < 0)
1319 return err;
1320
1321 return 1; /* notify the finding */
1322}
Daniel Macke5779992010-03-04 19:46:13 +01001323
1324/*
1325 * set up the runtime hardware information.
1326 */
1327
1328static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1329{
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001330 struct snd_usb_audio *chip = subs->stream->chip;
Eldad Zack88766f02013-04-03 23:18:49 +02001331 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001332 unsigned int pt, ptmin;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001333 int param_period_time_if_needed = -1;
Daniel Macke5779992010-03-04 19:46:13 +01001334 int err;
1335
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001336 mutex_lock(&chip->mutex);
1337 err = apply_hw_constraint_from_sync(runtime, subs);
1338 mutex_unlock(&chip->mutex);
1339 if (err < 0)
1340 return err;
1341 if (err > 0) /* found the matching? */
1342 goto add_extra_rules;
1343
Daniel Macke5779992010-03-04 19:46:13 +01001344 runtime->hw.formats = subs->formats;
1345
1346 runtime->hw.rate_min = 0x7fffffff;
1347 runtime->hw.rate_max = 0;
1348 runtime->hw.channels_min = 256;
1349 runtime->hw.channels_max = 0;
1350 runtime->hw.rates = 0;
1351 ptmin = UINT_MAX;
1352 /* check min/max rates and channels */
Eldad Zack88766f02013-04-03 23:18:49 +02001353 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001354 runtime->hw.rates |= fp->rates;
1355 if (runtime->hw.rate_min > fp->rate_min)
1356 runtime->hw.rate_min = fp->rate_min;
1357 if (runtime->hw.rate_max < fp->rate_max)
1358 runtime->hw.rate_max = fp->rate_max;
1359 if (runtime->hw.channels_min > fp->channels)
1360 runtime->hw.channels_min = fp->channels;
1361 if (runtime->hw.channels_max < fp->channels)
1362 runtime->hw.channels_max = fp->channels;
1363 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1364 /* FIXME: there might be more than one audio formats... */
1365 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1366 fp->frame_size;
1367 }
1368 pt = 125 * (1 << fp->datainterval);
1369 ptmin = min(ptmin, pt);
1370 }
1371
1372 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001373 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001374 /* full speed devices have fixed data packet interval */
1375 ptmin = 1000;
1376 if (ptmin == 1000)
1377 /* if period time doesn't go below 1 ms, no rules needed */
1378 param_period_time_if_needed = -1;
Daniel Macke5779992010-03-04 19:46:13 +01001379
Takashi Iwaie92be812018-05-27 15:09:15 +02001380 err = snd_pcm_hw_constraint_minmax(runtime,
1381 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1382 ptmin, UINT_MAX);
1383 if (err < 0)
1384 return err;
1385
1386 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1387 hw_rule_rate, subs,
1388 SNDRV_PCM_HW_PARAM_FORMAT,
1389 SNDRV_PCM_HW_PARAM_CHANNELS,
1390 param_period_time_if_needed,
1391 -1);
1392 if (err < 0)
1393 return err;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001394
1395add_extra_rules:
Takashi Iwaie92be812018-05-27 15:09:15 +02001396 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1397 hw_rule_channels, subs,
1398 SNDRV_PCM_HW_PARAM_FORMAT,
1399 SNDRV_PCM_HW_PARAM_RATE,
1400 param_period_time_if_needed,
1401 -1);
1402 if (err < 0)
1403 return err;
1404 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1405 hw_rule_format, subs,
1406 SNDRV_PCM_HW_PARAM_RATE,
1407 SNDRV_PCM_HW_PARAM_CHANNELS,
1408 param_period_time_if_needed,
1409 -1);
1410 if (err < 0)
1411 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001412 if (param_period_time_if_needed >= 0) {
1413 err = snd_pcm_hw_rule_add(runtime, 0,
1414 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1415 hw_rule_period_time, subs,
1416 SNDRV_PCM_HW_PARAM_FORMAT,
1417 SNDRV_PCM_HW_PARAM_CHANNELS,
1418 SNDRV_PCM_HW_PARAM_RATE,
1419 -1);
1420 if (err < 0)
Takashi Iwaie92be812018-05-27 15:09:15 +02001421 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001422 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001423
Takashi Iwai18652112020-11-23 09:53:15 +01001424 return 0;
Daniel Macke5779992010-03-04 19:46:13 +01001425}
1426
Takashi Iwai6fddc792018-05-27 13:59:03 +02001427static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
Daniel Macke5779992010-03-04 19:46:13 +01001428{
Takashi Iwai6fddc792018-05-27 13:59:03 +02001429 int direction = substream->stream;
Daniel Macke5779992010-03-04 19:46:13 +01001430 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1431 struct snd_pcm_runtime *runtime = substream->runtime;
1432 struct snd_usb_substream *subs = &as->substream[direction];
Shuah Khan66354f12019-04-01 20:40:22 -04001433 int ret;
Daniel Macke5779992010-03-04 19:46:13 +01001434
1435 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001436 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001437 runtime->hw = snd_usb_hardware;
1438 runtime->private_data = subs;
1439 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001440 /* runtime PM is also done there */
Daniel Mackd24f5062013-04-17 00:01:38 +08001441
1442 /* initialize DSD/DOP context */
1443 subs->dsd_dop.byte_idx = 0;
1444 subs->dsd_dop.channel = 0;
1445 subs->dsd_dop.marker = 1;
1446
Shuah Khan66354f12019-04-01 20:40:22 -04001447 ret = setup_hw_info(runtime, subs);
Takashi Iwai18652112020-11-23 09:53:15 +01001448 if (ret < 0)
1449 return ret;
1450 ret = snd_usb_autoresume(subs->stream->chip);
1451 if (ret < 0)
1452 return ret;
1453 ret = snd_media_stream_init(subs, as->pcm, direction);
1454 if (ret < 0)
1455 snd_usb_autosuspend(subs->stream->chip);
Shuah Khan66354f12019-04-01 20:40:22 -04001456 return ret;
Daniel Macke5779992010-03-04 19:46:13 +01001457}
1458
Takashi Iwai6fddc792018-05-27 13:59:03 +02001459static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
Daniel Macke5779992010-03-04 19:46:13 +01001460{
Takashi Iwai6fddc792018-05-27 13:59:03 +02001461 int direction = substream->stream;
Daniel Macke5779992010-03-04 19:46:13 +01001462 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1463 struct snd_usb_substream *subs = &as->substream[direction];
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001464 int ret;
Daniel Macke5779992010-03-04 19:46:13 +01001465
Shuah Khan66354f12019-04-01 20:40:22 -04001466 snd_media_stop_pipeline(subs);
Daniel Mack68e67f42012-07-12 13:08:40 +02001467
Takashi Iwai98215052020-11-23 09:53:21 +01001468 if (subs->interface >= 0 &&
Takashi Iwai47ab1542015-08-25 16:09:00 +02001469 !snd_usb_lock_shutdown(subs->stream->chip)) {
Daniel Mack68e67f42012-07-12 13:08:40 +02001470 usb_set_interface(subs->dev, subs->interface, 0);
1471 subs->interface = -1;
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001472 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
Takashi Iwai47ab1542015-08-25 16:09:00 +02001473 snd_usb_unlock_shutdown(subs->stream->chip);
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001474 if (ret < 0)
1475 return ret;
Daniel Mack68e67f42012-07-12 13:08:40 +02001476 }
1477
Daniel Macke5779992010-03-04 19:46:13 +01001478 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001479 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001480
Daniel Mack68e67f42012-07-12 13:08:40 +02001481 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001482}
1483
1484/* Since a URB can handle only a single linear buffer, we must use double
1485 * buffering when the data to be transferred overflows the buffer boundary.
1486 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1487 * for all URBs.
1488 */
1489static void retire_capture_urb(struct snd_usb_substream *subs,
1490 struct urb *urb)
1491{
1492 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1493 unsigned int stride, frames, bytes, oldptr;
1494 int i, period_elapsed = 0;
1495 unsigned long flags;
1496 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001497 int current_frame_number;
1498
1499 /* read frame number here, update pointer in critical section */
1500 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001501
1502 stride = runtime->frame_bits >> 3;
1503
1504 for (i = 0; i < urb->number_of_packets; i++) {
Calvin Owens1539d4f2013-04-12 22:33:59 -05001505 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
Daniel Mackedcd3632012-04-12 13:51:12 +02001506 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001507 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1508 i, urb->iso_frame_desc[i].status);
Daniel Mackedcd3632012-04-12 13:51:12 +02001509 // continue;
1510 }
1511 bytes = urb->iso_frame_desc[i].actual_length;
Hector Martin1b7ecc22020-08-10 17:24:00 +09001512 if (subs->stream_offset_adj > 0) {
1513 unsigned int adj = min(subs->stream_offset_adj, bytes);
1514 cp += adj;
1515 bytes -= adj;
1516 subs->stream_offset_adj -= adj;
1517 }
Daniel Mackedcd3632012-04-12 13:51:12 +02001518 frames = bytes / stride;
1519 if (!subs->txfr_quirk)
1520 bytes = frames * stride;
1521 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001522 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001523 bytes = frames * stride;
Takashi Iwai377a8792018-05-16 20:07:18 +02001524 dev_warn_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001525 "Corrected urb data len. %d->%d\n",
Daniel Mackedcd3632012-04-12 13:51:12 +02001526 oldbytes, bytes);
1527 }
1528 /* update the current pointer */
1529 spin_lock_irqsave(&subs->lock, flags);
1530 oldptr = subs->hwptr_done;
1531 subs->hwptr_done += bytes;
1532 if (subs->hwptr_done >= runtime->buffer_size * stride)
1533 subs->hwptr_done -= runtime->buffer_size * stride;
1534 frames = (bytes + (oldptr % stride)) / stride;
1535 subs->transfer_done += frames;
1536 if (subs->transfer_done >= runtime->period_size) {
1537 subs->transfer_done -= runtime->period_size;
1538 period_elapsed = 1;
1539 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001540 /* capture delay is by construction limited to one URB,
1541 * reset delays here
1542 */
1543 runtime->delay = subs->last_delay = 0;
1544
1545 /* realign last_frame_number */
1546 subs->last_frame_number = current_frame_number;
1547 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1548
Daniel Mackedcd3632012-04-12 13:51:12 +02001549 spin_unlock_irqrestore(&subs->lock, flags);
1550 /* copy a data chunk */
1551 if (oldptr + bytes > runtime->buffer_size * stride) {
1552 unsigned int bytes1 =
1553 runtime->buffer_size * stride - oldptr;
1554 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1555 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1556 } else {
1557 memcpy(runtime->dma_area + oldptr, cp, bytes);
1558 }
1559 }
1560
1561 if (period_elapsed)
1562 snd_pcm_period_elapsed(subs->pcm_substream);
1563}
1564
Daniel Mackd24f5062013-04-17 00:01:38 +08001565static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1566 struct urb *urb, unsigned int bytes)
1567{
1568 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1569 unsigned int stride = runtime->frame_bits >> 3;
1570 unsigned int dst_idx = 0;
1571 unsigned int src_idx = subs->hwptr_done;
1572 unsigned int wrap = runtime->buffer_size * stride;
1573 u8 *dst = urb->transfer_buffer;
1574 u8 *src = runtime->dma_area;
1575 u8 marker[] = { 0x05, 0xfa };
1576
1577 /*
1578 * The DSP DOP format defines a way to transport DSD samples over
1579 * normal PCM data endpoints. It requires stuffing of marker bytes
1580 * (0x05 and 0xfa, alternating per sample frame), and then expects
1581 * 2 additional bytes of actual payload. The whole frame is stored
1582 * LSB.
1583 *
1584 * Hence, for a stereo transport, the buffer layout looks like this,
1585 * where L refers to left channel samples and R to right.
1586 *
1587 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1588 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1589 * .....
1590 *
1591 */
1592
1593 while (bytes--) {
1594 if (++subs->dsd_dop.byte_idx == 3) {
1595 /* frame boundary? */
1596 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1597 src_idx += 2;
1598 subs->dsd_dop.byte_idx = 0;
1599
1600 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1601 /* alternate the marker */
1602 subs->dsd_dop.marker++;
1603 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1604 subs->dsd_dop.channel = 0;
1605 }
1606 } else {
1607 /* stuff the DSD payload */
1608 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001609
1610 if (subs->cur_audiofmt->dsd_bitrev)
1611 dst[dst_idx++] = bitrev8(src[idx]);
1612 else
1613 dst[dst_idx++] = src[idx];
1614
Daniel Mackd24f5062013-04-17 00:01:38 +08001615 subs->hwptr_done++;
1616 }
1617 }
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001618 if (subs->hwptr_done >= runtime->buffer_size * stride)
1619 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001620}
1621
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001622static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1623 int offset, int stride, unsigned int bytes)
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001624{
1625 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1626
1627 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1628 /* err, the transferred area goes over buffer boundary. */
1629 unsigned int bytes1 =
1630 runtime->buffer_size * stride - subs->hwptr_done;
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001631 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001632 runtime->dma_area + subs->hwptr_done, bytes1);
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001633 memcpy(urb->transfer_buffer + offset + bytes1,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001634 runtime->dma_area, bytes - bytes1);
1635 } else {
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001636 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001637 runtime->dma_area + subs->hwptr_done, bytes);
1638 }
1639 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001640 if (subs->hwptr_done >= runtime->buffer_size * stride)
1641 subs->hwptr_done -= runtime->buffer_size * stride;
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001642}
1643
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001644static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1645 struct urb *urb, int stride,
1646 unsigned int bytes)
1647{
1648 __le32 packet_length;
1649 int i;
1650
1651 /* Put __le32 length descriptor at start of each packet. */
1652 for (i = 0; i < urb->number_of_packets; i++) {
1653 unsigned int length = urb->iso_frame_desc[i].length;
1654 unsigned int offset = urb->iso_frame_desc[i].offset;
1655
1656 packet_length = cpu_to_le32(length);
1657 offset += i * sizeof(packet_length);
1658 urb->iso_frame_desc[i].offset = offset;
1659 urb->iso_frame_desc[i].length += sizeof(packet_length);
1660 memcpy(urb->transfer_buffer + offset,
1661 &packet_length, sizeof(packet_length));
1662 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1663 stride, length);
1664 }
1665 /* Adjust transfer size accordingly. */
1666 bytes += urb->number_of_packets * sizeof(packet_length);
1667 return bytes;
1668}
1669
Daniel Mackedcd3632012-04-12 13:51:12 +02001670static void prepare_playback_urb(struct snd_usb_substream *subs,
1671 struct urb *urb)
1672{
1673 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001674 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001675 struct snd_urb_ctx *ctx = urb->context;
1676 unsigned int counts, frames, bytes;
1677 int i, stride, period_elapsed = 0;
1678 unsigned long flags;
1679
1680 stride = runtime->frame_bits >> 3;
1681
1682 frames = 0;
1683 urb->number_of_packets = 0;
1684 spin_lock_irqsave(&subs->lock, flags);
Alan Stern976b6c02013-09-24 15:51:58 -04001685 subs->frame_limit += ep->max_urb_frames;
Daniel Mackedcd3632012-04-12 13:51:12 +02001686 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001687 if (ctx->packet_size[i])
1688 counts = ctx->packet_size[i];
Alexander Tsoyf0bd62b2020-04-24 05:24:48 +03001689 else if (ep->sync_master)
1690 counts = snd_usb_endpoint_slave_next_packet_size(ep);
Daniel Mack245baf92012-08-30 18:52:30 +02001691 else
1692 counts = snd_usb_endpoint_next_packet_size(ep);
1693
Daniel Mackedcd3632012-04-12 13:51:12 +02001694 /* set up descriptor */
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001695 urb->iso_frame_desc[i].offset = frames * ep->stride;
1696 urb->iso_frame_desc[i].length = counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001697 frames += counts;
1698 urb->number_of_packets++;
1699 subs->transfer_done += counts;
1700 if (subs->transfer_done >= runtime->period_size) {
1701 subs->transfer_done -= runtime->period_size;
Alan Stern976b6c02013-09-24 15:51:58 -04001702 subs->frame_limit = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001703 period_elapsed = 1;
1704 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1705 if (subs->transfer_done > 0) {
1706 /* FIXME: fill-max mode is not
1707 * supported yet */
1708 frames -= subs->transfer_done;
1709 counts -= subs->transfer_done;
1710 urb->iso_frame_desc[i].length =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001711 counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001712 subs->transfer_done = 0;
1713 }
1714 i++;
1715 if (i < ctx->packets) {
1716 /* add a transfer delimiter */
1717 urb->iso_frame_desc[i].offset =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001718 frames * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001719 urb->iso_frame_desc[i].length = 0;
1720 urb->number_of_packets++;
1721 }
1722 break;
1723 }
1724 }
Alan Stern976b6c02013-09-24 15:51:58 -04001725 /* finish at the period boundary or after enough frames */
1726 if ((period_elapsed ||
1727 subs->transfer_done >= subs->frame_limit) &&
1728 !snd_usb_endpoint_implicit_feedback_sink(ep))
Daniel Mackedcd3632012-04-12 13:51:12 +02001729 break;
1730 }
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001731 bytes = frames * ep->stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001732
1733 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1734 subs->cur_audiofmt->dsd_dop)) {
1735 fill_playback_urb_dsd_dop(subs, urb, bytes);
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001736 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1737 subs->cur_audiofmt->dsd_bitrev)) {
1738 /* bit-reverse the bytes */
1739 u8 *buf = urb->transfer_buffer;
1740 for (i = 0; i < bytes; i++) {
1741 int idx = (subs->hwptr_done + i)
1742 % (runtime->buffer_size * stride);
1743 buf[i] = bitrev8(runtime->dma_area[idx]);
1744 }
1745
1746 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001747 if (subs->hwptr_done >= runtime->buffer_size * stride)
1748 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001749 } else {
Daniel Mackd24f5062013-04-17 00:01:38 +08001750 /* usual PCM */
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001751 if (!subs->tx_length_quirk)
1752 copy_to_urb(subs, urb, 0, stride, bytes);
1753 else
1754 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1755 /* bytes is now amount of outgoing data */
Daniel Mackedcd3632012-04-12 13:51:12 +02001756 }
Daniel Mackd24f5062013-04-17 00:01:38 +08001757
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001758 /* update delay with exact number of samples queued */
1759 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001760 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001761 subs->last_delay = runtime->delay;
1762
1763 /* realign last_frame_number */
1764 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1765 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1766
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001767 if (subs->trigger_tstamp_pending_update) {
1768 /* this is the first actual URB submitted,
1769 * update trigger timestamp to reflect actual start time
1770 */
1771 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1772 subs->trigger_tstamp_pending_update = false;
1773 }
1774
Daniel Mackedcd3632012-04-12 13:51:12 +02001775 spin_unlock_irqrestore(&subs->lock, flags);
1776 urb->transfer_buffer_length = bytes;
1777 if (period_elapsed)
1778 snd_pcm_period_elapsed(subs->pcm_substream);
1779}
1780
1781/*
1782 * process after playback data complete
1783 * - decrease the delay count again
1784 */
1785static void retire_playback_urb(struct snd_usb_substream *subs,
1786 struct urb *urb)
1787{
1788 unsigned long flags;
1789 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001790 struct snd_usb_endpoint *ep = subs->data_endpoint;
1791 int processed = urb->transfer_buffer_length / ep->stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001792 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001793
Alexander Tsoy5ff40e62020-06-29 06:26:07 +03001794 /* ignore the delay accounting when processed=0 is given, i.e.
1795 * silent payloads are processed before handling the actual data
Takashi Iwai1213a202012-09-06 14:58:00 +02001796 */
1797 if (!processed)
1798 return;
1799
Daniel Mackedcd3632012-04-12 13:51:12 +02001800 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001801 if (!subs->last_delay)
1802 goto out; /* short path */
1803
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001804 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1805 /* update delay with exact number of samples played */
1806 if (processed > subs->last_delay)
1807 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001808 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001809 subs->last_delay -= processed;
1810 runtime->delay = subs->last_delay;
1811
1812 /*
1813 * Report when delay estimate is off by more than 2ms.
1814 * The error should be lower than 2ms since the estimate relies
1815 * on two reads of a counter updated every ms.
1816 */
Sander Eikelenboomb7a77232014-05-02 15:09:27 +02001817 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1818 dev_dbg_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001819 "delay: estimated %d, actual %d\n",
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001820 est_delay, subs->last_delay);
1821
Takashi Iwai48779a02012-11-23 16:00:37 +01001822 if (!subs->running) {
1823 /* update last_frame_number for delay counting here since
1824 * prepare_playback_urb won't be called during pause
1825 */
1826 subs->last_frame_number =
1827 usb_get_current_frame_number(subs->dev) & 0xff;
1828 }
1829
1830 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001831 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001832}
1833
Daniel Mackedcd3632012-04-12 13:51:12 +02001834static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1835 int cmd)
1836{
1837 struct snd_usb_substream *subs = substream->runtime->private_data;
1838
1839 switch (cmd) {
1840 case SNDRV_PCM_TRIGGER_START:
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001841 subs->trigger_tstamp_pending_update = true;
Gustavo A. R. Silvac0dbbda2020-07-08 15:32:36 -05001842 fallthrough;
Daniel Mackedcd3632012-04-12 13:51:12 +02001843 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1844 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1845 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001846 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001847 return 0;
1848 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001849 stop_endpoints(subs);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001850 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001851 return 0;
1852 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1853 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001854 /* keep retire_data_urb for delay calculation */
1855 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001856 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001857 return 0;
Hui Wang92adc962019-12-18 21:26:50 +08001858 case SNDRV_PCM_TRIGGER_SUSPEND:
1859 if (subs->stream->chip->setup_fmt_after_resume_quirk) {
Takashi Iwaia032ff02019-12-18 20:05:39 +01001860 stop_endpoints(subs);
Hui Wang92adc962019-12-18 21:26:50 +08001861 subs->need_setup_fmt = true;
1862 return 0;
1863 }
1864 break;
Daniel Mackedcd3632012-04-12 13:51:12 +02001865 }
1866
1867 return -EINVAL;
1868}
1869
Daniel Mackafe25962012-06-16 16:58:04 +02001870static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1871 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001872{
1873 int err;
1874 struct snd_usb_substream *subs = substream->runtime->private_data;
1875
1876 switch (cmd) {
1877 case SNDRV_PCM_TRIGGER_START:
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +02001878 err = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +02001879 if (err < 0)
1880 return err;
1881
1882 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001883 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001884 return 0;
1885 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001886 stop_endpoints(subs);
Takashi Iwaiff58bbc2020-06-16 14:09:21 +02001887 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001888 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001889 return 0;
1890 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1891 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001892 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001893 return 0;
1894 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1895 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001896 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001897 return 0;
Hui Wang92adc962019-12-18 21:26:50 +08001898 case SNDRV_PCM_TRIGGER_SUSPEND:
1899 if (subs->stream->chip->setup_fmt_after_resume_quirk) {
Takashi Iwaia032ff02019-12-18 20:05:39 +01001900 stop_endpoints(subs);
Hui Wang92adc962019-12-18 21:26:50 +08001901 subs->need_setup_fmt = true;
1902 return 0;
1903 }
1904 break;
Daniel Mackedcd3632012-04-12 13:51:12 +02001905 }
1906
1907 return -EINVAL;
1908}
1909
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301910static const struct snd_pcm_ops snd_usb_playback_ops = {
Takashi Iwai6fddc792018-05-27 13:59:03 +02001911 .open = snd_usb_pcm_open,
1912 .close = snd_usb_pcm_close,
Daniel Macke5779992010-03-04 19:46:13 +01001913 .hw_params = snd_usb_hw_params,
1914 .hw_free = snd_usb_hw_free,
1915 .prepare = snd_usb_pcm_prepare,
1916 .trigger = snd_usb_substream_playback_trigger,
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001917 .sync_stop = snd_usb_pcm_sync_stop,
Daniel Macke5779992010-03-04 19:46:13 +01001918 .pointer = snd_usb_pcm_pointer,
Daniel Macke5779992010-03-04 19:46:13 +01001919};
1920
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301921static const struct snd_pcm_ops snd_usb_capture_ops = {
Takashi Iwai6fddc792018-05-27 13:59:03 +02001922 .open = snd_usb_pcm_open,
1923 .close = snd_usb_pcm_close,
Daniel Macke5779992010-03-04 19:46:13 +01001924 .hw_params = snd_usb_hw_params,
1925 .hw_free = snd_usb_hw_free,
1926 .prepare = snd_usb_pcm_prepare,
1927 .trigger = snd_usb_substream_capture_trigger,
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001928 .sync_stop = snd_usb_pcm_sync_stop,
Daniel Macke5779992010-03-04 19:46:13 +01001929 .pointer = snd_usb_pcm_pointer,
Takashi Iwaif274baa2018-05-27 13:01:17 +02001930};
1931
Daniel Macke5779992010-03-04 19:46:13 +01001932void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1933{
Takashi Iwaif274baa2018-05-27 13:01:17 +02001934 const struct snd_pcm_ops *ops;
1935
Takashi Iwaib315997d2019-11-05 16:18:40 +01001936 ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
Takashi Iwaif274baa2018-05-27 13:01:17 +02001937 &snd_usb_playback_ops : &snd_usb_capture_ops;
Takashi Iwaif274baa2018-05-27 13:01:17 +02001938 snd_pcm_set_ops(pcm, stream, ops);
1939}
1940
1941void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
1942{
1943 struct snd_pcm *pcm = subs->stream->pcm;
1944 struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
1945 struct device *dev = subs->dev->bus->controller;
1946
Takashi Iwaib315997d2019-11-05 16:18:40 +01001947 if (snd_usb_use_vmalloc)
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +01001948 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_VMALLOC,
1949 NULL, 0, 0);
Takashi Iwaib315997d2019-11-05 16:18:40 +01001950 else
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +01001951 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_DEV_SG,
1952 dev, 64*1024, 512*1024);
Daniel Macke5779992010-03-04 19:46:13 +01001953}