blob: ac6385a4eb70ed63b6beef032f350c578957a688 [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
Takashi Iwai73037c82020-11-23 09:53:26 +0100149static int init_pitch_v1(struct snd_usb_audio *chip,
Daniel Mack767d75a2010-03-04 19:46:17 +0100150 struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +0100151{
Daniel Mack767d75a2010-03-04 19:46:17 +0100152 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100153 unsigned int ep;
154 unsigned char data[1];
155 int err;
156
Takashi Iwai73037c82020-11-23 09:53:26 +0100157 ep = fmt->endpoint;
Daniel Mack767d75a2010-03-04 19:46:17 +0100158
Daniel Mack767d75a2010-03-04 19:46:17 +0100159 data[0] = 1;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200160 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
161 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
162 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
163 data, sizeof(data));
164 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100165 usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
Takashi Iwai73037c82020-11-23 09:53:26 +0100166 fmt->iface, ep);
Daniel Mack767d75a2010-03-04 19:46:17 +0100167 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100168 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100169
Daniel Macke5779992010-03-04 19:46:13 +0100170 return 0;
171}
172
Takashi Iwai73037c82020-11-23 09:53:26 +0100173static int init_pitch_v2(struct snd_usb_audio *chip,
Daniel Mack92c25612010-05-26 18:11:39 +0200174 struct audioformat *fmt)
175{
176 struct usb_device *dev = chip->dev;
177 unsigned char data[1];
Daniel Mack92c25612010-05-26 18:11:39 +0200178 int err;
179
Daniel Mack92c25612010-05-26 18:11:39 +0200180 data[0] = 1;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200181 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
182 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
183 UAC2_EP_CS_PITCH << 8, 0,
184 data, sizeof(data));
185 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100186 usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
Takashi Iwai73037c82020-11-23 09:53:26 +0100187 fmt->iface, fmt->altsetting);
Daniel Mack92c25612010-05-26 18:11:39 +0200188 return err;
189 }
190
191 return 0;
192}
193
Daniel Mack767d75a2010-03-04 19:46:17 +0100194/*
Daniel Mack92c25612010-05-26 18:11:39 +0200195 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100196 */
Takashi Iwai73037c82020-11-23 09:53:26 +0100197int snd_usb_init_pitch(struct snd_usb_audio *chip,
Daniel Mack767d75a2010-03-04 19:46:17 +0100198 struct audioformat *fmt)
199{
Daniel Mack92c25612010-05-26 18:11:39 +0200200 /* if endpoint doesn't have pitch control, bail out */
201 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
202 return 0;
203
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100204 switch (fmt->protocol) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100205 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200206 default:
Takashi Iwai73037c82020-11-23 09:53:26 +0100207 return init_pitch_v1(chip, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100208
209 case UAC_VERSION_2:
Takashi Iwai73037c82020-11-23 09:53:26 +0100210 return init_pitch_v2(chip, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100211 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100212}
213
Takashi Iwai57234bc2020-11-23 09:53:27 +0100214static void stop_endpoints(struct snd_usb_substream *subs)
215{
216 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
217 snd_usb_endpoint_stop(subs->sync_endpoint);
218 subs->sync_endpoint->sync_slave = NULL;
219 }
220
221 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
222 snd_usb_endpoint_stop(subs->data_endpoint);
223}
224
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200225static int start_endpoints(struct snd_usb_substream *subs)
Daniel Mackedcd3632012-04-12 13:51:12 +0200226{
227 int err;
228
229 if (!subs->data_endpoint)
230 return -EINVAL;
231
232 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
233 struct snd_usb_endpoint *ep = subs->data_endpoint;
234
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200235 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200236 if (err < 0) {
237 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
Takashi Iwai57234bc2020-11-23 09:53:27 +0100238 goto error;
Daniel Mackedcd3632012-04-12 13:51:12 +0200239 }
240 }
241
242 if (subs->sync_endpoint &&
243 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
244 struct snd_usb_endpoint *ep = subs->sync_endpoint;
245
Daniel Mackedcd3632012-04-12 13:51:12 +0200246 ep->sync_slave = subs->data_endpoint;
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200247 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200248 if (err < 0) {
249 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
Takashi Iwai18035032020-11-23 09:53:12 +0100250 ep->sync_slave = NULL;
Takashi Iwai57234bc2020-11-23 09:53:27 +0100251 goto error;
Daniel Mackedcd3632012-04-12 13:51:12 +0200252 }
253 }
254
255 return 0;
Takashi Iwai57234bc2020-11-23 09:53:27 +0100256
257 error:
258 stop_endpoints(subs);
259 return err;
Daniel Mackedcd3632012-04-12 13:51:12 +0200260}
261
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100262static void sync_pending_stops(struct snd_usb_substream *subs)
263{
264 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
265 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
266}
267
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100268/* PCM sync_stop callback */
269static int snd_usb_pcm_sync_stop(struct snd_pcm_substream *substream)
270{
271 struct snd_usb_substream *subs = substream->runtime->private_data;
272
273 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
274 sync_pending_stops(subs);
275 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100276 }
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100277 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +0200278}
279
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100280/* Check whether the given iface:altsetting points to an implicit fb source */
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100281static bool search_generic_implicit_fb(struct snd_usb_audio *chip, int ifnum,
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100282 unsigned int altsetting,
283 struct usb_host_interface **altsp,
284 unsigned int *ep)
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100285{
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100286 struct usb_host_interface *alts;
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100287 struct usb_interface_descriptor *altsd;
288 struct usb_endpoint_descriptor *epd;
289
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100290 alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100291 if (!alts)
292 return false;
293 altsd = get_iface_desc(alts);
294 if (altsd->bInterfaceClass != USB_CLASS_AUDIO ||
295 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING ||
296 altsd->bInterfaceProtocol != UAC_VERSION_2 ||
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100297 altsd->bNumEndpoints < 1)
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100298 return false;
299 epd = get_endpoint(alts, 0);
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100300 if (!usb_endpoint_is_isoc_in(epd) ||
301 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
302 USB_ENDPOINT_USAGE_IMPLICIT_FB)
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100303 return false;
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100304 *ep = epd->bEndpointAddress;
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100305 *altsp = alts;
306 return true;
307}
308
309/* Like the function above, but specific to Roland with vendor class and hack */
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100310static bool search_roland_implicit_fb(struct snd_usb_audio *chip, int ifnum,
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100311 unsigned int altsetting,
312 struct usb_host_interface **altsp,
313 unsigned int *ep)
314{
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100315 struct usb_host_interface *alts;
316 struct usb_interface_descriptor *altsd;
317 struct usb_endpoint_descriptor *epd;
318
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100319 alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100320 if (!alts)
321 return false;
322 altsd = get_iface_desc(alts);
323 if (altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
324 (altsd->bInterfaceSubClass != 2 &&
325 altsd->bInterfaceProtocol != 2) ||
326 altsd->bNumEndpoints < 1)
327 return false;
328 epd = get_endpoint(alts, 0);
329 if (!usb_endpoint_is_isoc_in(epd) ||
330 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
331 USB_ENDPOINT_USAGE_IMPLICIT_FB)
332 return false;
333 *ep = epd->bEndpointAddress;
334 *altsp = alts;
335 return true;
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100336}
337
Manuel Reinhardt2bc16b92019-01-31 15:32:35 +0100338/* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
339 * applies. Returns 1 if a quirk was found.
340 */
Takashi Iwaif6581c02020-11-23 09:53:14 +0100341static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
342 struct audioformat *fmt,
Takashi Iwaif6581c02020-11-23 09:53:14 +0100343 struct usb_host_interface *alts)
Daniel Macke5779992010-03-04 19:46:13 +0100344{
Takashi Iwaif6581c02020-11-23 09:53:14 +0100345 struct usb_device *dev = chip->dev;
346 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100347 struct usb_interface *iface;
Takashi Iwaif6581c02020-11-23 09:53:14 +0100348 unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Eldad Zacka60945f2013-08-03 10:50:18 +0200349 unsigned int ep;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500350 unsigned int ifnum;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200351
Takashi Iwaif6581c02020-11-23 09:53:14 +0100352 switch (chip->usb_id) {
Eldad Zackca10a7e2012-11-28 23:55:41 +0100353 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500354 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Geoffrey D. Bennett0938eca2020-11-04 22:27:17 +1030355 case USB_ID(0x22f0, 0x0006): /* Allen&Heath Qu-16 */
Eldad Zack914273c2013-08-03 10:50:21 +0200356 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500357 ifnum = 3;
358 goto add_sync_ep_from_ifnum;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200359 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
360 case USB_ID(0x0763, 0x2081):
Eldad Zack914273c2013-08-03 10:50:21 +0200361 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500362 ifnum = 2;
363 goto add_sync_ep_from_ifnum;
364 case USB_ID(0x2466, 0x8003): /* Fractal Audio Axe-Fx II */
Geoffrey D. Bennett26201dd2020-11-04 22:37:05 +1030365 case USB_ID(0x0499, 0x172a): /* Yamaha MODX */
Alberto Aguirre17f08b02016-12-08 00:36:48 -0600366 ep = 0x86;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500367 ifnum = 2;
368 goto add_sync_ep_from_ifnum;
Alberto Aguirre91a85612018-04-18 09:35:35 -0500369 case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx III */
370 ep = 0x81;
371 ifnum = 2;
372 goto add_sync_ep_from_ifnum;
Keith Winsteinf15cfca2020-10-25 22:05:47 -0700373 case USB_ID(0x1686, 0xf029): /* Zoom UAC-2 */
374 ep = 0x82;
375 ifnum = 2;
376 goto add_sync_ep_from_ifnum;
Takashi Iwai1a157182019-08-20 08:58:12 +0200377 case USB_ID(0x1397, 0x0001): /* Behringer UFX1604 */
Alberto Aguirre103e9622018-04-18 09:35:34 -0500378 case USB_ID(0x1397, 0x0002): /* Behringer UFX1204 */
Lassi Ylikojola5e35dc02018-02-09 16:51:36 +0200379 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500380 ifnum = 1;
381 goto add_sync_ep_from_ifnum;
Alexander Tsoy2edb84e2020-02-29 18:18:15 +0300382 case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook II/IIc */
383 /* MicroBook IIc */
384 if (altsd->bInterfaceClass == USB_CLASS_AUDIO)
385 return 0;
386
387 /* MicroBook II */
Manuel Reinhardta6340902019-02-28 20:34:04 +0100388 ep = 0x84;
389 ifnum = 0;
390 goto add_sync_ep_from_ifnum;
Alexander Tsoyc2491772020-01-15 18:13:58 +0300391 case USB_ID(0x07fd, 0x0008): /* MOTU M Series */
Laurence Tratt3da87ec2020-06-21 08:50:05 +0100392 case USB_ID(0x31e9, 0x0001): /* Solid State Logic SSL2 */
Laurence Tratte7585db2020-06-12 12:18:07 +0100393 case USB_ID(0x31e9, 0x0002): /* Solid State Logic SSL2+ */
Joshua Sivec7c5b8922020-08-25 18:55:18 +0200394 case USB_ID(0x0499, 0x172f): /* Steinberg UR22C */
Pavel Hofmanb6a1e782020-07-03 12:04:33 +0200395 case USB_ID(0x0d9a, 0x00df): /* RTX6001 */
Alexander Tsoyc2491772020-01-15 18:13:58 +0300396 ep = 0x81;
397 ifnum = 2;
398 goto add_sync_ep_from_ifnum;
Dmitry Panchenko7fccfec2020-06-01 13:22:24 +0300399 case USB_ID(0x2b73, 0x000a): /* Pioneer DJ DJM-900NXS2 */
František Kučera14335d82020-08-25 17:31:13 +0200400 case USB_ID(0x2b73, 0x0017): /* Pioneer DJ DJM-250MK2 */
Dmitry Panchenko7fccfec2020-06-01 13:22:24 +0300401 ep = 0x82;
402 ifnum = 0;
403 goto add_sync_ep_from_ifnum;
Szabolcs Szőke7571b6a2019-10-11 19:19:36 +0200404 case USB_ID(0x0582, 0x01d8): /* BOSS Katana */
405 /* BOSS Katana amplifiers do not need quirks */
406 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200407 }
Alberto Aguirre103e9622018-04-18 09:35:34 -0500408
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100409 /* Generic UAC2 implicit feedback */
410 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
411 altsd->bInterfaceClass == USB_CLASS_AUDIO &&
412 altsd->bInterfaceProtocol == UAC_VERSION_2 &&
413 altsd->bNumEndpoints == 1) {
414 ifnum = altsd->bInterfaceNumber + 1;
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100415 if (search_generic_implicit_fb(chip, ifnum,
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100416 altsd->bAlternateSetting,
417 &alts, &ep))
418 goto add_sync_ep;
419 }
420
421 /* Roland/BOSS implicit feedback with vendor spec class */
Eldad Zack914273c2013-08-03 10:50:21 +0200422 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100423 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
424 altsd->bInterfaceProtocol == 2 &&
425 altsd->bNumEndpoints == 1 &&
Takashi Iwaif6581c02020-11-23 09:53:14 +0100426 USB_ID_VENDOR(chip->usb_id) == 0x0582 /* Roland */) {
427 ifnum = altsd->bInterfaceNumber + 1;
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100428 if (search_roland_implicit_fb(chip, ifnum,
Takashi Iwaif6581c02020-11-23 09:53:14 +0100429 altsd->bAlternateSetting,
430 &alts, &ep))
431 goto add_sync_ep;
432 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200433
Eldad Zacka60945f2013-08-03 10:50:18 +0200434 /* No quirk */
435 return 0;
436
Alberto Aguirre103e9622018-04-18 09:35:34 -0500437add_sync_ep_from_ifnum:
438 iface = usb_ifnum_to_if(dev, ifnum);
439
Johan Hovold5d1b7122020-01-14 09:39:53 +0100440 if (!iface || iface->num_altsetting < 2)
Takashi Iwaif6581c02020-11-23 09:53:14 +0100441 return 0;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500442
443 alts = &iface->altsetting[1];
444
Eldad Zacka60945f2013-08-03 10:50:18 +0200445add_sync_ep:
Takashi Iwaif6581c02020-11-23 09:53:14 +0100446 fmt->sync_ep = ep;
447 fmt->sync_iface = ifnum;
448 fmt->sync_altsetting = alts->desc.bAlternateSetting;
449 fmt->implicit_fb = 1;
450 dev_dbg(&dev->dev, "%d:%d: found implicit_fb sync_ep=%x, iface=%d, alt=%d\n",
451 fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface,
452 fmt->sync_altsetting);
Eldad Zacka60945f2013-08-03 10:50:18 +0200453
Manuel Reinhardt2bc16b92019-01-31 15:32:35 +0100454 return 1;
Eldad Zacka60945f2013-08-03 10:50:18 +0200455}
456
Takashi Iwaif6581c02020-11-23 09:53:14 +0100457int snd_usb_audioformat_set_sync_ep(struct snd_usb_audio *chip,
458 struct audioformat *fmt)
Eldad Zacka60945f2013-08-03 10:50:18 +0200459{
Takashi Iwaif6581c02020-11-23 09:53:14 +0100460 struct usb_device *dev = chip->dev;
Takashi Iwaif6581c02020-11-23 09:53:14 +0100461 struct usb_host_interface *alts;
462 struct usb_interface_descriptor *altsd;
463 unsigned int ep, attr, sync_attr;
464 bool is_playback;
Eldad Zacka60945f2013-08-03 10:50:18 +0200465 int err;
466
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100467 alts = snd_usb_get_host_interface(chip, fmt->iface, fmt->altsetting);
Takashi Iwaif6581c02020-11-23 09:53:14 +0100468 if (!alts)
469 return 0;
470 altsd = get_iface_desc(alts);
471
472 is_playback = !(get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
473 if (is_playback) {
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100474 err = audioformat_implicit_fb_quirk(chip, fmt, alts);
Takashi Iwaif6581c02020-11-23 09:53:14 +0100475 if (err > 0)
476 return 0;
477 }
Manuel Reinhardt2bc16b92019-01-31 15:32:35 +0100478
Eldad Zackf34d0652013-08-03 10:50:19 +0200479 if (altsd->bNumEndpoints < 2)
480 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200481
Takashi Iwaif6581c02020-11-23 09:53:14 +0100482 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500483 if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
484 attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
Eldad Zackf34d0652013-08-03 10:50:19 +0200485 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
486 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200487
Takashi Iwaif6581c02020-11-23 09:53:14 +0100488 sync_attr = get_endpoint(alts, 1)->bmAttributes;
489
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500490 /*
491 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
492 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
493 * error fall back to SYNC mode and don't create sync endpoint
494 */
495
Eldad Zackf34d0652013-08-03 10:50:19 +0200496 /* check sync-pipe endpoint */
497 /* ... and check descriptor size before accessing bSynchAddress
498 because there is a version of the SB Audigy 2 NX firmware lacking
499 the audio fields in the endpoint descriptors */
Takashi Iwaif6581c02020-11-23 09:53:14 +0100500 if ((sync_attr & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
Eldad Zackf34d0652013-08-03 10:50:19 +0200501 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zack95fec882013-08-03 10:50:20 +0200502 get_endpoint(alts, 1)->bSynchAddress != 0)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100503 dev_err(&dev->dev,
504 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
505 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200506 get_endpoint(alts, 1)->bmAttributes,
507 get_endpoint(alts, 1)->bLength,
508 get_endpoint(alts, 1)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500509 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
510 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200511 return -EINVAL;
Daniel Mackedcd3632012-04-12 13:51:12 +0200512 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200513 ep = get_endpoint(alts, 1)->bEndpointAddress;
Eldad Zack95fec882013-08-03 10:50:20 +0200514 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Ard van Breemen1b341212019-08-02 13:52:14 +0200515 get_endpoint(alts, 0)->bSynchAddress != 0 &&
Eldad Zackf34d0652013-08-03 10:50:19 +0200516 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
517 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100518 dev_err(&dev->dev,
519 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
520 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200521 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500522 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
523 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200524 return -EINVAL;
525 }
526
Takashi Iwaif6581c02020-11-23 09:53:14 +0100527 fmt->sync_ep = ep;
528 fmt->sync_iface = altsd->bInterfaceNumber;
529 fmt->sync_altsetting = altsd->bAlternateSetting;
530 if ((sync_attr & USB_ENDPOINT_USAGE_MASK) == USB_ENDPOINT_USAGE_IMPLICIT_FB)
531 fmt->implicit_fb = 1;
532
533 dev_dbg(&dev->dev, "%d:%d: found sync_ep=0x%x, iface=%d, alt=%d, implicit_fb=%d\n",
534 fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface,
535 fmt->sync_altsetting, fmt->implicit_fb);
536
537 return 0;
538}
539
540static int set_sync_endpoint(struct snd_usb_substream *subs,
541 struct audioformat *fmt)
542{
543 struct usb_device *dev = subs->dev;
Takashi Iwaif6581c02020-11-23 09:53:14 +0100544 struct usb_host_interface *alts;
Takashi Iwaid767aba2020-11-23 09:53:24 +0100545 struct snd_usb_audio *chip = subs->stream->chip;
Takashi Iwaif6581c02020-11-23 09:53:14 +0100546 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
547 unsigned int ep;
548 int err;
549
550 subs->sync_endpoint = NULL;
551 subs->data_endpoint->sync_master = NULL;
552
553 ep = fmt->sync_ep;
554 if (!ep)
555 return 0;
556
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100557 alts = snd_usb_get_host_interface(subs->stream->chip, fmt->sync_iface,
558 fmt->altsetting);
Takashi Iwaif6581c02020-11-23 09:53:14 +0100559 if (!alts)
560 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200561
Takashi Iwaid767aba2020-11-23 09:53:24 +0100562 subs->sync_endpoint = snd_usb_get_endpoint(chip, ep);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500563 if (!subs->sync_endpoint) {
Takashi Iwaif6581c02020-11-23 09:53:14 +0100564 if (is_playback &&
565 (fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) == USB_ENDPOINT_SYNC_NONE)
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500566 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200567 return -EINVAL;
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500568 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200569
Takashi Iwai54cb3192020-11-23 09:53:20 +0100570 subs->sync_endpoint->iface = fmt->sync_iface;
571 subs->sync_endpoint->altsetting = fmt->sync_altsetting;
Takashi Iwaif6581c02020-11-23 09:53:14 +0100572 subs->sync_endpoint->is_implicit_feedback = fmt->implicit_fb;
Erwin Burema10ce77e2020-05-10 20:29:11 +0200573
Eldad Zackf34d0652013-08-03 10:50:19 +0200574 subs->data_endpoint->sync_master = subs->sync_endpoint;
Daniel Macke5779992010-03-04 19:46:13 +0100575
Takashi Iwai54cb3192020-11-23 09:53:20 +0100576 snd_usb_endpoint_set_syncinterval(subs->stream->chip, subs->sync_endpoint, alts);
577
Takashi Iwai5fd255f2020-11-23 09:53:19 +0100578 if (!subs->sync_endpoint->use_count &&
579 (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
580 subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting)) {
Takashi Iwai4974b792020-11-23 09:53:08 +0100581 err = usb_set_interface(subs->dev,
582 subs->sync_endpoint->iface,
583 subs->sync_endpoint->altsetting);
584 if (err < 0)
585 return err;
586 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
587 subs->sync_endpoint->iface,
588 subs->sync_endpoint->altsetting);
Takashi Iwaid767aba2020-11-23 09:53:24 +0100589 snd_usb_set_interface_quirk(chip);
Takashi Iwai4974b792020-11-23 09:53:08 +0100590 }
591
Eldad Zack71bb64c2013-08-03 10:50:17 +0200592 return 0;
593}
594
595/*
596 * find a matching format and set up the interface
597 */
598static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
599{
600 struct usb_device *dev = subs->dev;
Takashi Iwaid767aba2020-11-23 09:53:24 +0100601 struct snd_usb_audio *chip = subs->stream->chip;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200602 struct usb_host_interface *alts;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200603 struct usb_interface *iface;
Takashi Iwai5fd255f2020-11-23 09:53:19 +0100604 struct snd_usb_endpoint *ep;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200605 int err;
606
607 iface = usb_ifnum_to_if(dev, fmt->iface);
608 if (WARN_ON(!iface))
609 return -EINVAL;
Takashi Iwaib099b962018-05-02 09:36:28 +0200610 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
Johan Hovold01412542019-12-20 10:31:34 +0100611 if (WARN_ON(!alts))
Eldad Zack71bb64c2013-08-03 10:50:17 +0200612 return -EINVAL;
613
Hui Wang92adc962019-12-18 21:26:50 +0800614 if (fmt == subs->cur_audiofmt && !subs->need_setup_fmt)
Eldad Zack71bb64c2013-08-03 10:50:17 +0200615 return 0;
616
Takashi Iwai5fd255f2020-11-23 09:53:19 +0100617 /* shared EP with implicit fb */
618 if (fmt->implicit_fb && !subs->need_setup_fmt) {
Takashi Iwaid767aba2020-11-23 09:53:24 +0100619 ep = snd_usb_get_endpoint(chip, fmt->endpoint);
Takashi Iwai5fd255f2020-11-23 09:53:19 +0100620 if (ep && ep->use_count > 0)
621 goto add_data_ep;
622 }
623
Eldad Zack71bb64c2013-08-03 10:50:17 +0200624 /* close the old interface */
Hui Wang92adc962019-12-18 21:26:50 +0800625 if (subs->interface >= 0 && (subs->interface != fmt->iface || subs->need_setup_fmt)) {
Takashi Iwai98215052020-11-23 09:53:21 +0100626 err = usb_set_interface(subs->dev, subs->interface, 0);
627 if (err < 0) {
628 dev_err(&dev->dev,
629 "%d:%d: return to setting 0 failed (%d)\n",
630 fmt->iface, fmt->altsetting, err);
631 return -EIO;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200632 }
633 subs->interface = -1;
634 subs->altset_idx = 0;
635 }
636
Hui Wang92adc962019-12-18 21:26:50 +0800637 if (subs->need_setup_fmt)
638 subs->need_setup_fmt = false;
639
Eldad Zack71bb64c2013-08-03 10:50:17 +0200640 /* set interface */
Takashi Iwaib099b962018-05-02 09:36:28 +0200641 if (iface->cur_altsetting != alts) {
Takashi Iwaid767aba2020-11-23 09:53:24 +0100642 err = snd_usb_select_mode_quirk(chip, fmt);
Jurgen Kramer6874daa2014-11-28 17:32:54 +0100643 if (err < 0)
644 return -EIO;
645
Eldad Zack71bb64c2013-08-03 10:50:17 +0200646 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
647 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100648 dev_err(&dev->dev,
649 "%d:%d: usb_set_interface failed (%d)\n",
650 fmt->iface, fmt->altsetting, err);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200651 return -EIO;
652 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100653 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
654 fmt->iface, fmt->altsetting);
Takashi Iwaid767aba2020-11-23 09:53:24 +0100655 snd_usb_set_interface_quirk(chip);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200656 }
657
Takashi Iwai5fd255f2020-11-23 09:53:19 +0100658 subs->need_setup_ep = true;
659
660 add_data_ep:
Takashi Iwaib099b962018-05-02 09:36:28 +0200661 subs->interface = fmt->iface;
662 subs->altset_idx = fmt->altset_idx;
Takashi Iwaid767aba2020-11-23 09:53:24 +0100663 subs->data_endpoint = snd_usb_get_endpoint(chip, fmt->endpoint);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200664 if (!subs->data_endpoint)
665 return -EINVAL;
Takashi Iwai54cb3192020-11-23 09:53:20 +0100666 subs->data_endpoint->iface = fmt->iface;
667 subs->data_endpoint->altsetting = fmt->altsetting;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200668
Takashi Iwaif6581c02020-11-23 09:53:14 +0100669 err = set_sync_endpoint(subs, fmt);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200670 if (err < 0)
671 return err;
672
Takashi Iwai5fd255f2020-11-23 09:53:19 +0100673 if (subs->need_setup_ep) {
Takashi Iwai73037c82020-11-23 09:53:26 +0100674 err = snd_usb_init_pitch(chip, fmt);
Takashi Iwai5fd255f2020-11-23 09:53:19 +0100675 if (err < 0)
676 return err;
677 }
Daniel Macke5779992010-03-04 19:46:13 +0100678
679 subs->cur_audiofmt = fmt;
680
681 snd_usb_set_format_quirk(subs, fmt);
682
Daniel Macke5779992010-03-04 19:46:13 +0100683 return 0;
684}
685
686/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100687 * Return the score of matching two audioformats.
688 * Veto the audioformat if:
689 * - It has no channels for some reason.
690 * - Requested PCM format is not supported.
691 * - Requested sample rate is not supported.
692 */
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100693static int match_endpoint_audioformats(struct snd_usb_substream *subs,
694 struct audioformat *fp,
695 struct audioformat *match, int rate,
696 snd_pcm_format_t pcm_format)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100697{
698 int i;
699 int score = 0;
700
701 if (fp->channels < 1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100702 dev_dbg(&subs->dev->dev,
703 "%s: (fmt @%p) no channels\n", __func__, fp);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100704 return 0;
705 }
706
Eldad Zack74c34ca2013-04-23 01:00:41 +0200707 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100708 dev_dbg(&subs->dev->dev,
709 "%s: (fmt @%p) no match for format %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100710 fp, pcm_format);
711 return 0;
712 }
713
714 for (i = 0; i < fp->nr_rates; i++) {
715 if (fp->rate_table[i] == rate) {
716 score++;
717 break;
718 }
719 }
720 if (!score) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100721 dev_dbg(&subs->dev->dev,
722 "%s: (fmt @%p) no match for rate %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100723 fp, rate);
724 return 0;
725 }
726
727 if (fp->channels == match->channels)
728 score++;
729
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100730 dev_dbg(&subs->dev->dev,
731 "%s: (fmt @%p) score %d\n", __func__, fp, score);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100732
733 return score;
734}
735
736/*
737 * Configure the sync ep using the rate and pcm format of the data ep.
738 */
739static int configure_sync_endpoint(struct snd_usb_substream *subs)
740{
Eldad Zack0d9741c2012-12-03 20:30:09 +0100741 struct audioformat *fp;
742 struct audioformat *sync_fp = NULL;
743 int cur_score = 0;
744 int sync_period_bytes = subs->period_bytes;
745 struct snd_usb_substream *sync_subs =
746 &subs->stream->substream[subs->direction ^ 1];
747
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100748 if (subs->fixed_hw ||
749 !subs->sync_endpoint->is_implicit_feedback) {
750 sync_fp = subs->cur_audiofmt;
751 goto configure;
752 }
753
754 sync_fp = find_format(&sync_subs->fmt_list, subs->pcm_format,
755 subs->cur_rate, subs->channels, NULL);
756 if (sync_fp)
757 goto configure;
Takashi Iwai31be5422013-01-10 14:06:38 +0100758
Eldad Zack0d9741c2012-12-03 20:30:09 +0100759 /* Try to find the best matching audioformat. */
760 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100761 int score = match_endpoint_audioformats(subs,
762 fp, subs->cur_audiofmt,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100763 subs->cur_rate, subs->pcm_format);
764
765 if (score > cur_score) {
766 sync_fp = fp;
767 cur_score = score;
768 }
769 }
770
771 if (unlikely(sync_fp == NULL)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100772 dev_err(&subs->dev->dev,
773 "%s: no valid audioformat for sync ep %x found\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100774 __func__, sync_subs->ep_num);
775 return -EINVAL;
776 }
777
778 /*
779 * Recalculate the period bytes if channel number differ between
780 * data and sync ep audioformat.
781 */
782 if (sync_fp->channels != subs->channels) {
783 sync_period_bytes = (subs->period_bytes / subs->channels) *
784 sync_fp->channels;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100785 dev_dbg(&subs->dev->dev,
786 "%s: adjusted sync ep period bytes (%d -> %d)\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100787 __func__, subs->period_bytes, sync_period_bytes);
788 }
789
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100790 configure:
791 return snd_usb_endpoint_set_params(subs->sync_endpoint,
792 subs->pcm_format,
793 sync_fp->channels,
794 sync_period_bytes,
795 subs->period_frames,
796 subs->buffer_periods,
797 subs->cur_rate,
798 sync_fp,
799 NULL);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100800}
801
802/*
Dylan Reid61a70952012-09-18 09:49:48 -0700803 * configure endpoint params
804 *
805 * called during initial setup and upon resume
806 */
807static int configure_endpoint(struct snd_usb_substream *subs)
808{
809 int ret;
810
Dylan Reid61a70952012-09-18 09:49:48 -0700811 /* format changed */
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100812 stop_endpoints(subs);
813 sync_pending_stops(subs);
Dylan Reid61a70952012-09-18 09:49:48 -0700814 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
815 subs->pcm_format,
816 subs->channels,
817 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400818 subs->period_frames,
819 subs->buffer_periods,
Dylan Reid61a70952012-09-18 09:49:48 -0700820 subs->cur_rate,
821 subs->cur_audiofmt,
822 subs->sync_endpoint);
823 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200824 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700825
826 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100827 ret = configure_sync_endpoint(subs);
828
Dylan Reid61a70952012-09-18 09:49:48 -0700829 return ret;
830}
831
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100832static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state)
833{
834 int ret;
835
836 if (!subs->str_pd)
837 return 0;
838
839 ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
840 if (ret < 0) {
841 dev_err(&subs->dev->dev,
842 "Cannot change Power Domain ID: %d to state: %d. Err: %d\n",
843 subs->str_pd->pd_id, state, ret);
844 return ret;
845 }
846
847 return 0;
848}
849
850int snd_usb_pcm_suspend(struct snd_usb_stream *as)
851{
852 int ret;
853
854 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2);
855 if (ret < 0)
856 return ret;
857
858 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2);
859 if (ret < 0)
860 return ret;
861
862 return 0;
863}
864
865int snd_usb_pcm_resume(struct snd_usb_stream *as)
866{
867 int ret;
868
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100869 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1);
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100870 if (ret < 0)
871 return ret;
872
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100873 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1);
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100874 if (ret < 0)
875 return ret;
876
877 return 0;
878}
879
Dylan Reid61a70952012-09-18 09:49:48 -0700880/*
Daniel Macke5779992010-03-04 19:46:13 +0100881 * hw_params callback
882 *
883 * allocate a buffer and set the given audio format.
884 *
885 * so far we use a physically linear buffer although packetize transfer
886 * doesn't need a continuous area.
887 * if sg buffer is supported on the later version of alsa, we'll follow
888 * that.
889 */
890static int snd_usb_hw_params(struct snd_pcm_substream *substream,
891 struct snd_pcm_hw_params *hw_params)
892{
893 struct snd_usb_substream *subs = substream->runtime->private_data;
894 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700895 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100896
Shuah Khan66354f12019-04-01 20:40:22 -0400897 ret = snd_media_start_pipeline(subs);
898 if (ret)
899 return ret;
900
Dylan Reid61a70952012-09-18 09:49:48 -0700901 subs->pcm_format = params_format(hw_params);
902 subs->period_bytes = params_period_bytes(hw_params);
Alan Stern976b6c02013-09-24 15:51:58 -0400903 subs->period_frames = params_period_size(hw_params);
904 subs->buffer_periods = params_periods(hw_params);
Dylan Reid61a70952012-09-18 09:49:48 -0700905 subs->channels = params_channels(hw_params);
906 subs->cur_rate = params_rate(hw_params);
907
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100908 fmt = find_substream_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100909 if (!fmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100910 dev_dbg(&subs->dev->dev,
911 "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700912 subs->pcm_format, subs->cur_rate, subs->channels);
Shuah Khan66354f12019-04-01 20:40:22 -0400913 ret = -EINVAL;
914 goto stop_pipeline;
Daniel Macke5779992010-03-04 19:46:13 +0100915 }
916
Takashi Iwai47ab1542015-08-25 16:09:00 +0200917 ret = snd_usb_lock_shutdown(subs->stream->chip);
918 if (ret < 0)
Shuah Khan66354f12019-04-01 20:40:22 -0400919 goto stop_pipeline;
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100920
921 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
Takashi Iwai978520b2012-10-12 15:12:55 +0200922 if (ret < 0)
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100923 goto unlock;
924
925 ret = set_format(subs, fmt);
926 if (ret < 0)
927 goto unlock;
Daniel Macke5779992010-03-04 19:46:13 +0100928
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100929 unlock:
930 snd_usb_unlock_shutdown(subs->stream->chip);
Shuah Khan66354f12019-04-01 20:40:22 -0400931 if (ret < 0)
932 goto stop_pipeline;
933 return ret;
934
935 stop_pipeline:
936 snd_media_stop_pipeline(subs);
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100937 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100938}
939
940/*
941 * hw_free callback
942 *
943 * reset the audio format and release the buffer
944 */
945static int snd_usb_hw_free(struct snd_pcm_substream *substream)
946{
947 struct snd_usb_substream *subs = substream->runtime->private_data;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100948 struct snd_usb_audio *chip = subs->stream->chip;
Daniel Macke5779992010-03-04 19:46:13 +0100949
Shuah Khan66354f12019-04-01 20:40:22 -0400950 snd_media_stop_pipeline(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100951 subs->cur_audiofmt = NULL;
952 subs->cur_rate = 0;
953 subs->period_bytes = 0;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100954 if (!snd_usb_lock_shutdown(chip)) {
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100955 stop_endpoints(subs);
956 sync_pending_stops(subs);
Eldad Zack26de5d02013-10-06 22:31:07 +0200957 snd_usb_endpoint_deactivate(subs->sync_endpoint);
958 snd_usb_endpoint_deactivate(subs->data_endpoint);
Takashi Iwai18035032020-11-23 09:53:12 +0100959 if (subs->data_endpoint) {
960 subs->data_endpoint->sync_master = NULL;
961 subs->data_endpoint = NULL;
962 }
963 subs->sync_endpoint = NULL;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100964 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200965 }
Takashi Iwaif274baa2018-05-27 13:01:17 +0200966
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +0100967 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100968}
969
970/*
971 * prepare callback
972 *
973 * only a few subtle things...
974 */
975static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
976{
977 struct snd_pcm_runtime *runtime = substream->runtime;
978 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700979 struct usb_host_interface *alts;
980 struct usb_interface *iface;
981 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100982
983 if (! subs->cur_audiofmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100984 dev_err(&subs->dev->dev, "no format is specified!\n");
Daniel Macke5779992010-03-04 19:46:13 +0100985 return -ENXIO;
986 }
987
Takashi Iwai47ab1542015-08-25 16:09:00 +0200988 ret = snd_usb_lock_shutdown(subs->stream->chip);
989 if (ret < 0)
990 return ret;
Takashi Iwai978520b2012-10-12 15:12:55 +0200991 if (snd_BUG_ON(!subs->data_endpoint)) {
992 ret = -EIO;
993 goto unlock;
994 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200995
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100996 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
997 if (ret < 0)
998 goto unlock;
999
Dylan Reid61a70952012-09-18 09:49:48 -07001000 ret = set_format(subs, subs->cur_audiofmt);
1001 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +02001002 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -07001003
Takashi Iwai384dc0852012-09-18 14:49:31 +02001004 if (subs->need_setup_ep) {
Daniel Girnus1e2e3fe2016-12-06 14:46:15 +09001005
1006 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
1007 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
1008 ret = snd_usb_init_sample_rate(subs->stream->chip,
Daniel Girnus1e2e3fe2016-12-06 14:46:15 +09001009 subs->cur_audiofmt,
1010 subs->cur_rate);
1011 if (ret < 0)
1012 goto unlock;
1013
Takashi Iwai384dc0852012-09-18 14:49:31 +02001014 ret = configure_endpoint(subs);
1015 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +02001016 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +02001017 subs->need_setup_ep = false;
1018 }
Dylan Reid61a70952012-09-18 09:49:48 -07001019
Daniel Macke5779992010-03-04 19:46:13 +01001020 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +02001021 subs->data_endpoint->maxframesize =
1022 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
1023 subs->data_endpoint->curframesize =
1024 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +01001025
1026 /* reset the pointer */
1027 subs->hwptr_done = 0;
1028 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -05001029 subs->last_delay = 0;
1030 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001031 runtime->delay = 0;
1032
Daniel Mackedcd3632012-04-12 13:51:12 +02001033 /* for playback, submit the URBs now; otherwise, the first hwptr_done
1034 * updates for all URBs would happen at the same time when starting */
1035 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +02001036 ret = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +02001037
Takashi Iwai978520b2012-10-12 15:12:55 +02001038 unlock:
Takashi Iwai47ab1542015-08-25 16:09:00 +02001039 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +02001040 return ret;
Daniel Macke5779992010-03-04 19:46:13 +01001041}
1042
Takashi Iwai7ec827b2020-11-23 09:53:18 +01001043/*
1044 * h/w constraints
1045 */
1046
1047#ifdef HW_CONST_DEBUG
1048#define hwc_debug(fmt, args...) pr_debug(fmt, ##args)
1049#else
1050#define hwc_debug(fmt, args...) do { } while(0)
1051#endif
1052
Bhumika Goyalaaffbf72017-08-17 14:45:59 +05301053static const struct snd_pcm_hardware snd_usb_hardware =
Daniel Macke5779992010-03-04 19:46:13 +01001054{
1055 .info = SNDRV_PCM_INFO_MMAP |
1056 SNDRV_PCM_INFO_MMAP_VALID |
1057 SNDRV_PCM_INFO_BATCH |
1058 SNDRV_PCM_INFO_INTERLEAVED |
1059 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1060 SNDRV_PCM_INFO_PAUSE,
1061 .buffer_bytes_max = 1024 * 1024,
1062 .period_bytes_min = 64,
1063 .period_bytes_max = 512 * 1024,
1064 .periods_min = 2,
1065 .periods_max = 1024,
1066};
1067
1068static int hw_check_valid_format(struct snd_usb_substream *subs,
1069 struct snd_pcm_hw_params *params,
1070 struct audioformat *fp)
1071{
1072 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1073 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1074 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1075 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001076 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +01001077 unsigned int ptime;
1078
1079 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001080 snd_mask_none(&check_fmts);
1081 check_fmts.bits[0] = (u32)fp->formats;
1082 check_fmts.bits[1] = (u32)(fp->formats >> 32);
1083 snd_mask_intersect(&check_fmts, fmts);
1084 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +01001085 hwc_debug(" > check: no supported format %d\n", fp->format);
1086 return 0;
1087 }
1088 /* check the channels */
1089 if (fp->channels < ct->min || fp->channels > ct->max) {
1090 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1091 return 0;
1092 }
1093 /* check the rate is within the range */
1094 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1095 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1096 return 0;
1097 }
1098 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1099 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1100 return 0;
1101 }
1102 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +02001103 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +01001104 ptime = 125 * (1 << fp->datainterval);
1105 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
1106 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
1107 return 0;
1108 }
1109 }
1110 return 1;
1111}
1112
Takashi Iwai7726dce2020-11-23 09:53:17 +01001113static int apply_hw_params_minmax(struct snd_interval *it, unsigned int rmin,
1114 unsigned int rmax)
Daniel Macke5779992010-03-04 19:46:13 +01001115{
Daniel Macke5779992010-03-04 19:46:13 +01001116 int changed;
Daniel Macke5779992010-03-04 19:46:13 +01001117
Takashi Iwaibc4e94a2020-11-23 09:53:07 +01001118 if (rmin > rmax) {
Daniel Macke5779992010-03-04 19:46:13 +01001119 hwc_debug(" --> get empty\n");
1120 it->empty = 1;
1121 return -EINVAL;
1122 }
1123
1124 changed = 0;
1125 if (it->min < rmin) {
1126 it->min = rmin;
1127 it->openmin = 0;
1128 changed = 1;
1129 }
1130 if (it->max > rmax) {
1131 it->max = rmax;
1132 it->openmax = 0;
1133 changed = 1;
1134 }
1135 if (snd_interval_checkempty(it)) {
1136 it->empty = 1;
1137 return -EINVAL;
1138 }
1139 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1140 return changed;
1141}
1142
Takashi Iwai7726dce2020-11-23 09:53:17 +01001143static int hw_rule_rate(struct snd_pcm_hw_params *params,
1144 struct snd_pcm_hw_rule *rule)
1145{
1146 struct snd_usb_substream *subs = rule->private;
1147 struct audioformat *fp;
1148 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1149 unsigned int rmin, rmax, r;
1150 int i;
1151
1152 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1153 rmin = UINT_MAX;
1154 rmax = 0;
1155 list_for_each_entry(fp, &subs->fmt_list, list) {
1156 if (!hw_check_valid_format(subs, params, fp))
1157 continue;
1158 if (fp->rate_table && fp->nr_rates) {
1159 for (i = 0; i < fp->nr_rates; i++) {
1160 r = fp->rate_table[i];
1161 if (!snd_interval_test(it, r))
1162 continue;
1163 rmin = min(rmin, r);
1164 rmax = max(rmax, r);
1165 }
1166 } else {
1167 rmin = min(rmin, fp->rate_min);
1168 rmax = max(rmax, fp->rate_max);
1169 }
1170 }
1171
1172 return apply_hw_params_minmax(it, rmin, rmax);
1173}
1174
Daniel Macke5779992010-03-04 19:46:13 +01001175
1176static int hw_rule_channels(struct snd_pcm_hw_params *params,
1177 struct snd_pcm_hw_rule *rule)
1178{
1179 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001180 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001181 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1182 unsigned int rmin, rmax;
Daniel Macke5779992010-03-04 19:46:13 +01001183
1184 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
Takashi Iwai7726dce2020-11-23 09:53:17 +01001185 rmin = UINT_MAX;
1186 rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001187 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001188 if (!hw_check_valid_format(subs, params, fp))
1189 continue;
Takashi Iwai7726dce2020-11-23 09:53:17 +01001190 rmin = min(rmin, fp->channels);
1191 rmax = max(rmax, fp->channels);
Daniel Macke5779992010-03-04 19:46:13 +01001192 }
1193
Takashi Iwai7726dce2020-11-23 09:53:17 +01001194 return apply_hw_params_minmax(it, rmin, rmax);
Daniel Macke5779992010-03-04 19:46:13 +01001195}
1196
1197static int hw_rule_format(struct snd_pcm_hw_params *params,
1198 struct snd_pcm_hw_rule *rule)
1199{
1200 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001201 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001202 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1203 u64 fbits;
1204 u32 oldbits[2];
1205 int changed;
1206
1207 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1208 fbits = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001209 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001210 if (!hw_check_valid_format(subs, params, fp))
1211 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001212 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +01001213 }
1214
1215 oldbits[0] = fmt->bits[0];
1216 oldbits[1] = fmt->bits[1];
1217 fmt->bits[0] &= (u32)fbits;
1218 fmt->bits[1] &= (u32)(fbits >> 32);
1219 if (!fmt->bits[0] && !fmt->bits[1]) {
1220 hwc_debug(" --> get empty\n");
1221 return -EINVAL;
1222 }
1223 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1224 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1225 return changed;
1226}
1227
1228static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1229 struct snd_pcm_hw_rule *rule)
1230{
1231 struct snd_usb_substream *subs = rule->private;
1232 struct audioformat *fp;
1233 struct snd_interval *it;
1234 unsigned char min_datainterval;
1235 unsigned int pmin;
Daniel Macke5779992010-03-04 19:46:13 +01001236
1237 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1238 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1239 min_datainterval = 0xff;
1240 list_for_each_entry(fp, &subs->fmt_list, list) {
1241 if (!hw_check_valid_format(subs, params, fp))
1242 continue;
1243 min_datainterval = min(min_datainterval, fp->datainterval);
1244 }
1245 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +02001246 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +01001247 it->empty = 1;
1248 return -EINVAL;
1249 }
1250 pmin = 125 * (1 << min_datainterval);
Takashi Iwai7726dce2020-11-23 09:53:17 +01001251
1252 return apply_hw_params_minmax(it, pmin, UINT_MAX);
Daniel Macke5779992010-03-04 19:46:13 +01001253}
1254
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001255/* apply PCM hw constraints from the concurrent sync EP */
1256static int apply_hw_constraint_from_sync(struct snd_pcm_runtime *runtime,
1257 struct snd_usb_substream *subs)
1258{
1259 struct snd_usb_audio *chip = subs->stream->chip;
1260 struct snd_usb_endpoint *ep;
1261 struct audioformat *fp;
1262 int err;
1263
1264 subs->fixed_hw = 0;
1265 list_for_each_entry(fp, &subs->fmt_list, list) {
Takashi Iwai54cb3192020-11-23 09:53:20 +01001266 ep = snd_usb_get_endpoint(chip, fp->endpoint);
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001267 if (ep && ep->cur_rate)
1268 goto found;
1269 if (!fp->implicit_fb)
1270 continue;
1271 /* for the implicit fb, check the sync ep as well */
Takashi Iwai54cb3192020-11-23 09:53:20 +01001272 ep = snd_usb_get_endpoint(chip, fp->sync_ep);
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001273 if (ep && ep->cur_rate)
1274 goto found;
1275 }
1276 return 0;
1277
1278 found:
1279 if (!find_format(&subs->fmt_list, ep->cur_format, ep->cur_rate,
1280 ep->cur_channels, NULL)) {
1281 usb_audio_dbg(chip, "EP 0x%x being used, but not applicable\n",
1282 ep->ep_num);
1283 return 0;
1284 }
1285
1286 usb_audio_dbg(chip, "EP 0x%x being used, using fixed params:\n",
1287 ep->ep_num);
1288 usb_audio_dbg(chip, "rate=%d, format=%s, channels=%d, period_size=%d, periods=%d\n",
1289 ep->cur_rate, snd_pcm_format_name(ep->cur_format),
1290 ep->cur_channels, ep->cur_period_frames,
1291 ep->cur_buffer_periods);
1292
1293 runtime->hw.formats = pcm_format_to_bits(ep->cur_format);
1294 runtime->hw.rate_min = runtime->hw.rate_max = ep->cur_rate;
1295 runtime->hw.channels_min = runtime->hw.channels_max =
1296 ep->cur_channels;
1297 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1298 runtime->hw.periods_min = runtime->hw.periods_max =
1299 ep->cur_buffer_periods;
1300 subs->fixed_hw = 1;
1301
1302 err = snd_pcm_hw_constraint_minmax(runtime,
1303 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1304 ep->cur_period_frames,
1305 ep->cur_period_frames);
1306 if (err < 0)
1307 return err;
1308
1309 return 1; /* notify the finding */
1310}
Daniel Macke5779992010-03-04 19:46:13 +01001311
1312/*
1313 * set up the runtime hardware information.
1314 */
1315
1316static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1317{
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001318 struct snd_usb_audio *chip = subs->stream->chip;
Eldad Zack88766f02013-04-03 23:18:49 +02001319 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001320 unsigned int pt, ptmin;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001321 int param_period_time_if_needed = -1;
Daniel Macke5779992010-03-04 19:46:13 +01001322 int err;
1323
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001324 mutex_lock(&chip->mutex);
1325 err = apply_hw_constraint_from_sync(runtime, subs);
1326 mutex_unlock(&chip->mutex);
1327 if (err < 0)
1328 return err;
1329 if (err > 0) /* found the matching? */
1330 goto add_extra_rules;
1331
Daniel Macke5779992010-03-04 19:46:13 +01001332 runtime->hw.formats = subs->formats;
1333
1334 runtime->hw.rate_min = 0x7fffffff;
1335 runtime->hw.rate_max = 0;
1336 runtime->hw.channels_min = 256;
1337 runtime->hw.channels_max = 0;
1338 runtime->hw.rates = 0;
1339 ptmin = UINT_MAX;
1340 /* check min/max rates and channels */
Eldad Zack88766f02013-04-03 23:18:49 +02001341 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001342 runtime->hw.rates |= fp->rates;
1343 if (runtime->hw.rate_min > fp->rate_min)
1344 runtime->hw.rate_min = fp->rate_min;
1345 if (runtime->hw.rate_max < fp->rate_max)
1346 runtime->hw.rate_max = fp->rate_max;
1347 if (runtime->hw.channels_min > fp->channels)
1348 runtime->hw.channels_min = fp->channels;
1349 if (runtime->hw.channels_max < fp->channels)
1350 runtime->hw.channels_max = fp->channels;
1351 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1352 /* FIXME: there might be more than one audio formats... */
1353 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1354 fp->frame_size;
1355 }
1356 pt = 125 * (1 << fp->datainterval);
1357 ptmin = min(ptmin, pt);
1358 }
1359
1360 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001361 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001362 /* full speed devices have fixed data packet interval */
1363 ptmin = 1000;
1364 if (ptmin == 1000)
1365 /* if period time doesn't go below 1 ms, no rules needed */
1366 param_period_time_if_needed = -1;
Daniel Macke5779992010-03-04 19:46:13 +01001367
Takashi Iwaie92be812018-05-27 15:09:15 +02001368 err = snd_pcm_hw_constraint_minmax(runtime,
1369 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1370 ptmin, UINT_MAX);
1371 if (err < 0)
1372 return err;
1373
1374 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1375 hw_rule_rate, subs,
1376 SNDRV_PCM_HW_PARAM_FORMAT,
1377 SNDRV_PCM_HW_PARAM_CHANNELS,
1378 param_period_time_if_needed,
1379 -1);
1380 if (err < 0)
1381 return err;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001382
1383add_extra_rules:
Takashi Iwaie92be812018-05-27 15:09:15 +02001384 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1385 hw_rule_channels, subs,
1386 SNDRV_PCM_HW_PARAM_FORMAT,
1387 SNDRV_PCM_HW_PARAM_RATE,
1388 param_period_time_if_needed,
1389 -1);
1390 if (err < 0)
1391 return err;
1392 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1393 hw_rule_format, subs,
1394 SNDRV_PCM_HW_PARAM_RATE,
1395 SNDRV_PCM_HW_PARAM_CHANNELS,
1396 param_period_time_if_needed,
1397 -1);
1398 if (err < 0)
1399 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001400 if (param_period_time_if_needed >= 0) {
1401 err = snd_pcm_hw_rule_add(runtime, 0,
1402 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1403 hw_rule_period_time, subs,
1404 SNDRV_PCM_HW_PARAM_FORMAT,
1405 SNDRV_PCM_HW_PARAM_CHANNELS,
1406 SNDRV_PCM_HW_PARAM_RATE,
1407 -1);
1408 if (err < 0)
Takashi Iwaie92be812018-05-27 15:09:15 +02001409 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001410 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001411
Takashi Iwai18652112020-11-23 09:53:15 +01001412 return 0;
Daniel Macke5779992010-03-04 19:46:13 +01001413}
1414
Takashi Iwai6fddc792018-05-27 13:59:03 +02001415static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
Daniel Macke5779992010-03-04 19:46:13 +01001416{
Takashi Iwai6fddc792018-05-27 13:59:03 +02001417 int direction = substream->stream;
Daniel Macke5779992010-03-04 19:46:13 +01001418 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1419 struct snd_pcm_runtime *runtime = substream->runtime;
1420 struct snd_usb_substream *subs = &as->substream[direction];
Shuah Khan66354f12019-04-01 20:40:22 -04001421 int ret;
Daniel Macke5779992010-03-04 19:46:13 +01001422
1423 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001424 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001425 runtime->hw = snd_usb_hardware;
1426 runtime->private_data = subs;
1427 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001428 /* runtime PM is also done there */
Daniel Mackd24f5062013-04-17 00:01:38 +08001429
1430 /* initialize DSD/DOP context */
1431 subs->dsd_dop.byte_idx = 0;
1432 subs->dsd_dop.channel = 0;
1433 subs->dsd_dop.marker = 1;
1434
Shuah Khan66354f12019-04-01 20:40:22 -04001435 ret = setup_hw_info(runtime, subs);
Takashi Iwai18652112020-11-23 09:53:15 +01001436 if (ret < 0)
1437 return ret;
1438 ret = snd_usb_autoresume(subs->stream->chip);
1439 if (ret < 0)
1440 return ret;
1441 ret = snd_media_stream_init(subs, as->pcm, direction);
1442 if (ret < 0)
1443 snd_usb_autosuspend(subs->stream->chip);
Shuah Khan66354f12019-04-01 20:40:22 -04001444 return ret;
Daniel Macke5779992010-03-04 19:46:13 +01001445}
1446
Takashi Iwai6fddc792018-05-27 13:59:03 +02001447static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
Daniel Macke5779992010-03-04 19:46:13 +01001448{
Takashi Iwai6fddc792018-05-27 13:59:03 +02001449 int direction = substream->stream;
Daniel Macke5779992010-03-04 19:46:13 +01001450 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1451 struct snd_usb_substream *subs = &as->substream[direction];
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001452 int ret;
Daniel Macke5779992010-03-04 19:46:13 +01001453
Shuah Khan66354f12019-04-01 20:40:22 -04001454 snd_media_stop_pipeline(subs);
Daniel Mack68e67f42012-07-12 13:08:40 +02001455
Takashi Iwai98215052020-11-23 09:53:21 +01001456 if (subs->interface >= 0 &&
Takashi Iwai47ab1542015-08-25 16:09:00 +02001457 !snd_usb_lock_shutdown(subs->stream->chip)) {
Daniel Mack68e67f42012-07-12 13:08:40 +02001458 usb_set_interface(subs->dev, subs->interface, 0);
1459 subs->interface = -1;
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001460 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
Takashi Iwai47ab1542015-08-25 16:09:00 +02001461 snd_usb_unlock_shutdown(subs->stream->chip);
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001462 if (ret < 0)
1463 return ret;
Daniel Mack68e67f42012-07-12 13:08:40 +02001464 }
1465
Daniel Macke5779992010-03-04 19:46:13 +01001466 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001467 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001468
Daniel Mack68e67f42012-07-12 13:08:40 +02001469 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001470}
1471
1472/* Since a URB can handle only a single linear buffer, we must use double
1473 * buffering when the data to be transferred overflows the buffer boundary.
1474 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1475 * for all URBs.
1476 */
1477static void retire_capture_urb(struct snd_usb_substream *subs,
1478 struct urb *urb)
1479{
1480 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1481 unsigned int stride, frames, bytes, oldptr;
1482 int i, period_elapsed = 0;
1483 unsigned long flags;
1484 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001485 int current_frame_number;
1486
1487 /* read frame number here, update pointer in critical section */
1488 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001489
1490 stride = runtime->frame_bits >> 3;
1491
1492 for (i = 0; i < urb->number_of_packets; i++) {
Calvin Owens1539d4f2013-04-12 22:33:59 -05001493 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
Daniel Mackedcd3632012-04-12 13:51:12 +02001494 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001495 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1496 i, urb->iso_frame_desc[i].status);
Daniel Mackedcd3632012-04-12 13:51:12 +02001497 // continue;
1498 }
1499 bytes = urb->iso_frame_desc[i].actual_length;
Hector Martin1b7ecc22020-08-10 17:24:00 +09001500 if (subs->stream_offset_adj > 0) {
1501 unsigned int adj = min(subs->stream_offset_adj, bytes);
1502 cp += adj;
1503 bytes -= adj;
1504 subs->stream_offset_adj -= adj;
1505 }
Daniel Mackedcd3632012-04-12 13:51:12 +02001506 frames = bytes / stride;
1507 if (!subs->txfr_quirk)
1508 bytes = frames * stride;
1509 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001510 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001511 bytes = frames * stride;
Takashi Iwai377a8792018-05-16 20:07:18 +02001512 dev_warn_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001513 "Corrected urb data len. %d->%d\n",
Daniel Mackedcd3632012-04-12 13:51:12 +02001514 oldbytes, bytes);
1515 }
1516 /* update the current pointer */
1517 spin_lock_irqsave(&subs->lock, flags);
1518 oldptr = subs->hwptr_done;
1519 subs->hwptr_done += bytes;
1520 if (subs->hwptr_done >= runtime->buffer_size * stride)
1521 subs->hwptr_done -= runtime->buffer_size * stride;
1522 frames = (bytes + (oldptr % stride)) / stride;
1523 subs->transfer_done += frames;
1524 if (subs->transfer_done >= runtime->period_size) {
1525 subs->transfer_done -= runtime->period_size;
1526 period_elapsed = 1;
1527 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001528 /* capture delay is by construction limited to one URB,
1529 * reset delays here
1530 */
1531 runtime->delay = subs->last_delay = 0;
1532
1533 /* realign last_frame_number */
1534 subs->last_frame_number = current_frame_number;
1535 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1536
Daniel Mackedcd3632012-04-12 13:51:12 +02001537 spin_unlock_irqrestore(&subs->lock, flags);
1538 /* copy a data chunk */
1539 if (oldptr + bytes > runtime->buffer_size * stride) {
1540 unsigned int bytes1 =
1541 runtime->buffer_size * stride - oldptr;
1542 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1543 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1544 } else {
1545 memcpy(runtime->dma_area + oldptr, cp, bytes);
1546 }
1547 }
1548
1549 if (period_elapsed)
1550 snd_pcm_period_elapsed(subs->pcm_substream);
1551}
1552
Daniel Mackd24f5062013-04-17 00:01:38 +08001553static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1554 struct urb *urb, unsigned int bytes)
1555{
1556 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1557 unsigned int stride = runtime->frame_bits >> 3;
1558 unsigned int dst_idx = 0;
1559 unsigned int src_idx = subs->hwptr_done;
1560 unsigned int wrap = runtime->buffer_size * stride;
1561 u8 *dst = urb->transfer_buffer;
1562 u8 *src = runtime->dma_area;
1563 u8 marker[] = { 0x05, 0xfa };
1564
1565 /*
1566 * The DSP DOP format defines a way to transport DSD samples over
1567 * normal PCM data endpoints. It requires stuffing of marker bytes
1568 * (0x05 and 0xfa, alternating per sample frame), and then expects
1569 * 2 additional bytes of actual payload. The whole frame is stored
1570 * LSB.
1571 *
1572 * Hence, for a stereo transport, the buffer layout looks like this,
1573 * where L refers to left channel samples and R to right.
1574 *
1575 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1576 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1577 * .....
1578 *
1579 */
1580
1581 while (bytes--) {
1582 if (++subs->dsd_dop.byte_idx == 3) {
1583 /* frame boundary? */
1584 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1585 src_idx += 2;
1586 subs->dsd_dop.byte_idx = 0;
1587
1588 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1589 /* alternate the marker */
1590 subs->dsd_dop.marker++;
1591 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1592 subs->dsd_dop.channel = 0;
1593 }
1594 } else {
1595 /* stuff the DSD payload */
1596 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001597
1598 if (subs->cur_audiofmt->dsd_bitrev)
1599 dst[dst_idx++] = bitrev8(src[idx]);
1600 else
1601 dst[dst_idx++] = src[idx];
1602
Daniel Mackd24f5062013-04-17 00:01:38 +08001603 subs->hwptr_done++;
1604 }
1605 }
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001606 if (subs->hwptr_done >= runtime->buffer_size * stride)
1607 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001608}
1609
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001610static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1611 int offset, int stride, unsigned int bytes)
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001612{
1613 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1614
1615 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1616 /* err, the transferred area goes over buffer boundary. */
1617 unsigned int bytes1 =
1618 runtime->buffer_size * stride - subs->hwptr_done;
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001619 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001620 runtime->dma_area + subs->hwptr_done, bytes1);
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001621 memcpy(urb->transfer_buffer + offset + bytes1,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001622 runtime->dma_area, bytes - bytes1);
1623 } else {
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001624 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001625 runtime->dma_area + subs->hwptr_done, bytes);
1626 }
1627 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001628 if (subs->hwptr_done >= runtime->buffer_size * stride)
1629 subs->hwptr_done -= runtime->buffer_size * stride;
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001630}
1631
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001632static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1633 struct urb *urb, int stride,
1634 unsigned int bytes)
1635{
1636 __le32 packet_length;
1637 int i;
1638
1639 /* Put __le32 length descriptor at start of each packet. */
1640 for (i = 0; i < urb->number_of_packets; i++) {
1641 unsigned int length = urb->iso_frame_desc[i].length;
1642 unsigned int offset = urb->iso_frame_desc[i].offset;
1643
1644 packet_length = cpu_to_le32(length);
1645 offset += i * sizeof(packet_length);
1646 urb->iso_frame_desc[i].offset = offset;
1647 urb->iso_frame_desc[i].length += sizeof(packet_length);
1648 memcpy(urb->transfer_buffer + offset,
1649 &packet_length, sizeof(packet_length));
1650 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1651 stride, length);
1652 }
1653 /* Adjust transfer size accordingly. */
1654 bytes += urb->number_of_packets * sizeof(packet_length);
1655 return bytes;
1656}
1657
Daniel Mackedcd3632012-04-12 13:51:12 +02001658static void prepare_playback_urb(struct snd_usb_substream *subs,
1659 struct urb *urb)
1660{
1661 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001662 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001663 struct snd_urb_ctx *ctx = urb->context;
1664 unsigned int counts, frames, bytes;
1665 int i, stride, period_elapsed = 0;
1666 unsigned long flags;
1667
1668 stride = runtime->frame_bits >> 3;
1669
1670 frames = 0;
1671 urb->number_of_packets = 0;
1672 spin_lock_irqsave(&subs->lock, flags);
Alan Stern976b6c02013-09-24 15:51:58 -04001673 subs->frame_limit += ep->max_urb_frames;
Daniel Mackedcd3632012-04-12 13:51:12 +02001674 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001675 if (ctx->packet_size[i])
1676 counts = ctx->packet_size[i];
Alexander Tsoyf0bd62b2020-04-24 05:24:48 +03001677 else if (ep->sync_master)
1678 counts = snd_usb_endpoint_slave_next_packet_size(ep);
Daniel Mack245baf92012-08-30 18:52:30 +02001679 else
1680 counts = snd_usb_endpoint_next_packet_size(ep);
1681
Daniel Mackedcd3632012-04-12 13:51:12 +02001682 /* set up descriptor */
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001683 urb->iso_frame_desc[i].offset = frames * ep->stride;
1684 urb->iso_frame_desc[i].length = counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001685 frames += counts;
1686 urb->number_of_packets++;
1687 subs->transfer_done += counts;
1688 if (subs->transfer_done >= runtime->period_size) {
1689 subs->transfer_done -= runtime->period_size;
Alan Stern976b6c02013-09-24 15:51:58 -04001690 subs->frame_limit = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001691 period_elapsed = 1;
1692 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1693 if (subs->transfer_done > 0) {
1694 /* FIXME: fill-max mode is not
1695 * supported yet */
1696 frames -= subs->transfer_done;
1697 counts -= subs->transfer_done;
1698 urb->iso_frame_desc[i].length =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001699 counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001700 subs->transfer_done = 0;
1701 }
1702 i++;
1703 if (i < ctx->packets) {
1704 /* add a transfer delimiter */
1705 urb->iso_frame_desc[i].offset =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001706 frames * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001707 urb->iso_frame_desc[i].length = 0;
1708 urb->number_of_packets++;
1709 }
1710 break;
1711 }
1712 }
Alan Stern976b6c02013-09-24 15:51:58 -04001713 /* finish at the period boundary or after enough frames */
1714 if ((period_elapsed ||
1715 subs->transfer_done >= subs->frame_limit) &&
1716 !snd_usb_endpoint_implicit_feedback_sink(ep))
Daniel Mackedcd3632012-04-12 13:51:12 +02001717 break;
1718 }
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001719 bytes = frames * ep->stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001720
1721 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1722 subs->cur_audiofmt->dsd_dop)) {
1723 fill_playback_urb_dsd_dop(subs, urb, bytes);
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001724 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1725 subs->cur_audiofmt->dsd_bitrev)) {
1726 /* bit-reverse the bytes */
1727 u8 *buf = urb->transfer_buffer;
1728 for (i = 0; i < bytes; i++) {
1729 int idx = (subs->hwptr_done + i)
1730 % (runtime->buffer_size * stride);
1731 buf[i] = bitrev8(runtime->dma_area[idx]);
1732 }
1733
1734 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001735 if (subs->hwptr_done >= runtime->buffer_size * stride)
1736 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001737 } else {
Daniel Mackd24f5062013-04-17 00:01:38 +08001738 /* usual PCM */
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001739 if (!subs->tx_length_quirk)
1740 copy_to_urb(subs, urb, 0, stride, bytes);
1741 else
1742 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1743 /* bytes is now amount of outgoing data */
Daniel Mackedcd3632012-04-12 13:51:12 +02001744 }
Daniel Mackd24f5062013-04-17 00:01:38 +08001745
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001746 /* update delay with exact number of samples queued */
1747 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001748 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001749 subs->last_delay = runtime->delay;
1750
1751 /* realign last_frame_number */
1752 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1753 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1754
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001755 if (subs->trigger_tstamp_pending_update) {
1756 /* this is the first actual URB submitted,
1757 * update trigger timestamp to reflect actual start time
1758 */
1759 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1760 subs->trigger_tstamp_pending_update = false;
1761 }
1762
Daniel Mackedcd3632012-04-12 13:51:12 +02001763 spin_unlock_irqrestore(&subs->lock, flags);
1764 urb->transfer_buffer_length = bytes;
1765 if (period_elapsed)
1766 snd_pcm_period_elapsed(subs->pcm_substream);
1767}
1768
1769/*
1770 * process after playback data complete
1771 * - decrease the delay count again
1772 */
1773static void retire_playback_urb(struct snd_usb_substream *subs,
1774 struct urb *urb)
1775{
1776 unsigned long flags;
1777 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001778 struct snd_usb_endpoint *ep = subs->data_endpoint;
1779 int processed = urb->transfer_buffer_length / ep->stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001780 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001781
Alexander Tsoy5ff40e62020-06-29 06:26:07 +03001782 /* ignore the delay accounting when processed=0 is given, i.e.
1783 * silent payloads are processed before handling the actual data
Takashi Iwai1213a202012-09-06 14:58:00 +02001784 */
1785 if (!processed)
1786 return;
1787
Daniel Mackedcd3632012-04-12 13:51:12 +02001788 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001789 if (!subs->last_delay)
1790 goto out; /* short path */
1791
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001792 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1793 /* update delay with exact number of samples played */
1794 if (processed > subs->last_delay)
1795 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001796 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001797 subs->last_delay -= processed;
1798 runtime->delay = subs->last_delay;
1799
1800 /*
1801 * Report when delay estimate is off by more than 2ms.
1802 * The error should be lower than 2ms since the estimate relies
1803 * on two reads of a counter updated every ms.
1804 */
Sander Eikelenboomb7a77232014-05-02 15:09:27 +02001805 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1806 dev_dbg_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001807 "delay: estimated %d, actual %d\n",
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001808 est_delay, subs->last_delay);
1809
Takashi Iwai48779a02012-11-23 16:00:37 +01001810 if (!subs->running) {
1811 /* update last_frame_number for delay counting here since
1812 * prepare_playback_urb won't be called during pause
1813 */
1814 subs->last_frame_number =
1815 usb_get_current_frame_number(subs->dev) & 0xff;
1816 }
1817
1818 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001819 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001820}
1821
Daniel Mackedcd3632012-04-12 13:51:12 +02001822static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1823 int cmd)
1824{
1825 struct snd_usb_substream *subs = substream->runtime->private_data;
1826
1827 switch (cmd) {
1828 case SNDRV_PCM_TRIGGER_START:
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001829 subs->trigger_tstamp_pending_update = true;
Gustavo A. R. Silvac0dbbda2020-07-08 15:32:36 -05001830 fallthrough;
Daniel Mackedcd3632012-04-12 13:51:12 +02001831 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai96e221f2020-11-23 09:53:28 +01001832 snd_usb_endpoint_set_callback(subs->data_endpoint,
1833 prepare_playback_urb,
1834 retire_playback_urb,
1835 subs);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001836 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001837 return 0;
Takashi Iwai75c16b52020-11-23 09:53:29 +01001838 case SNDRV_PCM_TRIGGER_SUSPEND:
1839 subs->need_setup_fmt = true;
1840 fallthrough;
Daniel Mackedcd3632012-04-12 13:51:12 +02001841 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001842 stop_endpoints(subs);
Takashi Iwai96e221f2020-11-23 09:53:28 +01001843 snd_usb_endpoint_set_callback(subs->data_endpoint,
1844 NULL, NULL, NULL);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001845 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001846 return 0;
1847 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai48779a02012-11-23 16:00:37 +01001848 /* keep retire_data_urb for delay calculation */
Takashi Iwai96e221f2020-11-23 09:53:28 +01001849 snd_usb_endpoint_set_callback(subs->data_endpoint,
1850 NULL,
1851 retire_playback_urb,
1852 subs);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001853 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001854 return 0;
1855 }
1856
1857 return -EINVAL;
1858}
1859
Daniel Mackafe25962012-06-16 16:58:04 +02001860static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1861 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001862{
1863 int err;
1864 struct snd_usb_substream *subs = substream->runtime->private_data;
1865
1866 switch (cmd) {
1867 case SNDRV_PCM_TRIGGER_START:
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +02001868 err = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +02001869 if (err < 0)
1870 return err;
Takashi Iwai96e221f2020-11-23 09:53:28 +01001871 fallthrough;
1872 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1873 snd_usb_endpoint_set_callback(subs->data_endpoint,
1874 NULL, retire_capture_urb,
1875 subs);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001876 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001877 return 0;
Takashi Iwai75c16b52020-11-23 09:53:29 +01001878 case SNDRV_PCM_TRIGGER_SUSPEND:
1879 subs->need_setup_fmt = true;
1880 fallthrough;
Daniel Mackedcd3632012-04-12 13:51:12 +02001881 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001882 stop_endpoints(subs);
Takashi Iwai96e221f2020-11-23 09:53:28 +01001883 fallthrough;
Daniel Mackedcd3632012-04-12 13:51:12 +02001884 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai96e221f2020-11-23 09:53:28 +01001885 snd_usb_endpoint_set_callback(subs->data_endpoint,
1886 NULL, NULL, NULL);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001887 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001888 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001889 }
1890
1891 return -EINVAL;
1892}
1893
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301894static const struct snd_pcm_ops snd_usb_playback_ops = {
Takashi Iwai6fddc792018-05-27 13:59:03 +02001895 .open = snd_usb_pcm_open,
1896 .close = snd_usb_pcm_close,
Daniel Macke5779992010-03-04 19:46:13 +01001897 .hw_params = snd_usb_hw_params,
1898 .hw_free = snd_usb_hw_free,
1899 .prepare = snd_usb_pcm_prepare,
1900 .trigger = snd_usb_substream_playback_trigger,
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001901 .sync_stop = snd_usb_pcm_sync_stop,
Daniel Macke5779992010-03-04 19:46:13 +01001902 .pointer = snd_usb_pcm_pointer,
Daniel Macke5779992010-03-04 19:46:13 +01001903};
1904
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301905static const struct snd_pcm_ops snd_usb_capture_ops = {
Takashi Iwai6fddc792018-05-27 13:59:03 +02001906 .open = snd_usb_pcm_open,
1907 .close = snd_usb_pcm_close,
Daniel Macke5779992010-03-04 19:46:13 +01001908 .hw_params = snd_usb_hw_params,
1909 .hw_free = snd_usb_hw_free,
1910 .prepare = snd_usb_pcm_prepare,
1911 .trigger = snd_usb_substream_capture_trigger,
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001912 .sync_stop = snd_usb_pcm_sync_stop,
Daniel Macke5779992010-03-04 19:46:13 +01001913 .pointer = snd_usb_pcm_pointer,
Takashi Iwaif274baa2018-05-27 13:01:17 +02001914};
1915
Daniel Macke5779992010-03-04 19:46:13 +01001916void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1917{
Takashi Iwaif274baa2018-05-27 13:01:17 +02001918 const struct snd_pcm_ops *ops;
1919
Takashi Iwaib315997d2019-11-05 16:18:40 +01001920 ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
Takashi Iwaif274baa2018-05-27 13:01:17 +02001921 &snd_usb_playback_ops : &snd_usb_capture_ops;
Takashi Iwaif274baa2018-05-27 13:01:17 +02001922 snd_pcm_set_ops(pcm, stream, ops);
1923}
1924
1925void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
1926{
1927 struct snd_pcm *pcm = subs->stream->pcm;
1928 struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
1929 struct device *dev = subs->dev->bus->controller;
1930
Takashi Iwaib315997d2019-11-05 16:18:40 +01001931 if (snd_usb_use_vmalloc)
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +01001932 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_VMALLOC,
1933 NULL, 0, 0);
Takashi Iwaib315997d2019-11-05 16:18:40 +01001934 else
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +01001935 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_DEV_SG,
1936 dev, 64*1024, 512*1024);
Daniel Macke5779992010-03-04 19:46:13 +01001937}