blob: 78933b6571d064121400800e86ec47cf2344be8c [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"
20#include "debug.h"
Daniel Mackc731bc92011-09-14 12:46:57 +020021#include "endpoint.h"
Daniel Macke5779992010-03-04 19:46:13 +010022#include "helper.h"
23#include "pcm.h"
Daniel Mack79f920f2010-05-31 14:51:31 +020024#include "clock.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010025#include "power.h"
Shuah Khan66354f12019-04-01 20:40:22 -040026#include "media.h"
Daniel Macke5779992010-03-04 19:46:13 +010027
Daniel Mackedcd3632012-04-12 13:51:12 +020028#define SUBSTREAM_FLAG_DATA_EP_STARTED 0
29#define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
30
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050031/* return the estimated delay based on USB frame counters */
32snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
33 unsigned int rate)
34{
35 int current_frame_number;
36 int frame_diff;
37 int est_delay;
38
Takashi Iwai48779a02012-11-23 16:00:37 +010039 if (!subs->last_delay)
40 return 0; /* short path */
41
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050042 current_frame_number = usb_get_current_frame_number(subs->dev);
43 /*
44 * HCD implementations use different widths, use lower 8 bits.
45 * The delay will be managed up to 256ms, which is more than
46 * enough
47 */
48 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
49
50 /* Approximation based on number of samples per USB frame (ms),
51 some truncation for 44.1 but the estimate is good enough */
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060052 est_delay = frame_diff * rate / 1000;
53 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
54 est_delay = subs->last_delay - est_delay;
55 else
56 est_delay = subs->last_delay + est_delay;
57
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050058 if (est_delay < 0)
59 est_delay = 0;
60 return est_delay;
61}
62
Daniel Macke5779992010-03-04 19:46:13 +010063/*
64 * return the current pcm pointer. just based on the hwptr_done value.
65 */
66static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
67{
Takashi Iwaie92be812018-05-27 15:09:15 +020068 struct snd_usb_substream *subs = substream->runtime->private_data;
Daniel Macke5779992010-03-04 19:46:13 +010069 unsigned int hwptr_done;
70
Takashi Iwai47ab1542015-08-25 16:09:00 +020071 if (atomic_read(&subs->stream->chip->shutdown))
Takashi Iwai978520b2012-10-12 15:12:55 +020072 return SNDRV_PCM_POS_XRUN;
Daniel Macke5779992010-03-04 19:46:13 +010073 spin_lock(&subs->lock);
74 hwptr_done = subs->hwptr_done;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060075 substream->runtime->delay = snd_usb_pcm_delay(subs,
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050076 substream->runtime->rate);
Daniel Macke5779992010-03-04 19:46:13 +010077 spin_unlock(&subs->lock);
78 return hwptr_done / (substream->runtime->frame_bits >> 3);
79}
80
81/*
82 * find a matching audio format
83 */
Dylan Reid61a70952012-09-18 09:49:48 -070084static struct audioformat *find_format(struct snd_usb_substream *subs)
Daniel Macke5779992010-03-04 19:46:13 +010085{
Eldad Zack88766f02013-04-03 23:18:49 +020086 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +010087 struct audioformat *found = NULL;
88 int cur_attr = 0, attr;
89
Eldad Zack88766f02013-04-03 23:18:49 +020090 list_for_each_entry(fp, &subs->fmt_list, list) {
Eldad Zack74c34ca2013-04-23 01:00:41 +020091 if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
Clemens Ladisch015eb0b2010-03-04 19:46:15 +010092 continue;
Dylan Reid61a70952012-09-18 09:49:48 -070093 if (fp->channels != subs->channels)
Daniel Macke5779992010-03-04 19:46:13 +010094 continue;
Dylan Reid61a70952012-09-18 09:49:48 -070095 if (subs->cur_rate < fp->rate_min ||
96 subs->cur_rate > fp->rate_max)
Daniel Macke5779992010-03-04 19:46:13 +010097 continue;
98 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
99 unsigned int i;
100 for (i = 0; i < fp->nr_rates; i++)
Dylan Reid61a70952012-09-18 09:49:48 -0700101 if (fp->rate_table[i] == subs->cur_rate)
Daniel Macke5779992010-03-04 19:46:13 +0100102 break;
103 if (i >= fp->nr_rates)
104 continue;
105 }
106 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
107 if (! found) {
108 found = fp;
109 cur_attr = attr;
110 continue;
111 }
112 /* avoid async out and adaptive in if the other method
113 * supports the same format.
114 * this is a workaround for the case like
115 * M-audio audiophile USB.
116 */
117 if (attr != cur_attr) {
118 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
119 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
120 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
121 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
122 continue;
123 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
124 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
125 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
126 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
127 found = fp;
128 cur_attr = attr;
129 continue;
130 }
131 }
132 /* find the format with the largest max. packet size */
133 if (fp->maxpacksize > found->maxpacksize) {
134 found = fp;
135 cur_attr = attr;
136 }
137 }
138 return found;
139}
140
Daniel Mack767d75a2010-03-04 19:46:17 +0100141static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
142 struct usb_host_interface *alts,
143 struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +0100144{
Daniel Mack767d75a2010-03-04 19:46:17 +0100145 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100146 unsigned int ep;
147 unsigned char data[1];
148 int err;
149
Takashi Iwai447d6272016-03-15 15:20:58 +0100150 if (get_iface_desc(alts)->bNumEndpoints < 1)
151 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100152 ep = get_endpoint(alts, 0)->bEndpointAddress;
Daniel Mack767d75a2010-03-04 19:46:17 +0100153
Daniel Mack767d75a2010-03-04 19:46:17 +0100154 data[0] = 1;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200155 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
156 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
157 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
158 data, sizeof(data));
159 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100160 usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
161 iface, ep);
Daniel Mack767d75a2010-03-04 19:46:17 +0100162 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100163 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100164
Daniel Macke5779992010-03-04 19:46:13 +0100165 return 0;
166}
167
Daniel Mack92c25612010-05-26 18:11:39 +0200168static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
169 struct usb_host_interface *alts,
170 struct audioformat *fmt)
171{
172 struct usb_device *dev = chip->dev;
173 unsigned char data[1];
Daniel Mack92c25612010-05-26 18:11:39 +0200174 int err;
175
Daniel Mack92c25612010-05-26 18:11:39 +0200176 data[0] = 1;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200177 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
178 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
179 UAC2_EP_CS_PITCH << 8, 0,
180 data, sizeof(data));
181 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100182 usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
183 iface, fmt->altsetting);
Daniel Mack92c25612010-05-26 18:11:39 +0200184 return err;
185 }
186
187 return 0;
188}
189
Daniel Mack767d75a2010-03-04 19:46:17 +0100190/*
Daniel Mack92c25612010-05-26 18:11:39 +0200191 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100192 */
193int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
194 struct usb_host_interface *alts,
195 struct audioformat *fmt)
196{
Daniel Mack92c25612010-05-26 18:11:39 +0200197 /* if endpoint doesn't have pitch control, bail out */
198 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
199 return 0;
200
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100201 switch (fmt->protocol) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100202 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200203 default:
Daniel Mack767d75a2010-03-04 19:46:17 +0100204 return init_pitch_v1(chip, iface, alts, fmt);
205
206 case UAC_VERSION_2:
Daniel Mack92c25612010-05-26 18:11:39 +0200207 return init_pitch_v2(chip, iface, alts, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100208 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100209}
210
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200211static int start_endpoints(struct snd_usb_substream *subs)
Daniel Mackedcd3632012-04-12 13:51:12 +0200212{
213 int err;
214
215 if (!subs->data_endpoint)
216 return -EINVAL;
217
218 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
219 struct snd_usb_endpoint *ep = subs->data_endpoint;
220
Takashi Iwaie93e8902020-11-23 09:53:13 +0100221 dev_dbg(&subs->dev->dev, "Starting data EP 0x%x\n", ep->ep_num);
Daniel Mackedcd3632012-04-12 13:51:12 +0200222
223 ep->data_subs = subs;
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200224 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200225 if (err < 0) {
226 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
227 return err;
228 }
229 }
230
231 if (subs->sync_endpoint &&
232 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
233 struct snd_usb_endpoint *ep = subs->sync_endpoint;
234
Takashi Iwaie93e8902020-11-23 09:53:13 +0100235 dev_dbg(&subs->dev->dev, "Starting sync EP 0x%x\n", ep->ep_num);
Daniel Mackedcd3632012-04-12 13:51:12 +0200236
237 ep->sync_slave = subs->data_endpoint;
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200238 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200239 if (err < 0) {
240 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
Takashi Iwai18035032020-11-23 09:53:12 +0100241 ep->sync_slave = NULL;
Daniel Mackedcd3632012-04-12 13:51:12 +0200242 return err;
243 }
244 }
245
246 return 0;
247}
248
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100249static void sync_pending_stops(struct snd_usb_substream *subs)
250{
251 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
252 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
253}
254
255static void stop_endpoints(struct snd_usb_substream *subs)
Daniel Mackedcd3632012-04-12 13:51:12 +0200256{
Takashi Iwai18035032020-11-23 09:53:12 +0100257 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
Takashi Iwaie93e8902020-11-23 09:53:13 +0100258 dev_dbg(&subs->dev->dev, "Stopping sync EP 0x%x\n",
259 subs->sync_endpoint->ep_num);
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100260 snd_usb_endpoint_stop(subs->sync_endpoint);
Takashi Iwai18035032020-11-23 09:53:12 +0100261 subs->sync_endpoint->sync_slave = NULL;
262 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200263
Takashi Iwaie93e8902020-11-23 09:53:13 +0100264 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
265 dev_dbg(&subs->dev->dev, "Stopping data EP 0x%x\n",
266 subs->data_endpoint->ep_num);
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100267 snd_usb_endpoint_stop(subs->data_endpoint);
Takashi Iwaie93e8902020-11-23 09:53:13 +0100268 }
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100269}
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100270
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100271/* PCM sync_stop callback */
272static int snd_usb_pcm_sync_stop(struct snd_pcm_substream *substream)
273{
274 struct snd_usb_substream *subs = substream->runtime->private_data;
275
276 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
277 sync_pending_stops(subs);
278 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100279 }
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100280 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +0200281}
282
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100283/* Check whether the given iface:altsetting points to an implicit fb source */
284static bool search_generic_implicit_fb(struct usb_device *dev, int ifnum,
285 unsigned int altsetting,
286 struct usb_host_interface **altsp,
287 unsigned int *ep)
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100288{
289 struct usb_interface *iface;
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100290 struct usb_host_interface *alts;
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100291 struct usb_interface_descriptor *altsd;
292 struct usb_endpoint_descriptor *epd;
293
294 iface = usb_ifnum_to_if(dev, ifnum);
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100295 if (!iface)
296 return false;
297 alts = usb_altnum_to_altsetting(iface, altsetting);
298 if (!alts)
299 return false;
300 altsd = get_iface_desc(alts);
301 if (altsd->bInterfaceClass != USB_CLASS_AUDIO ||
302 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING ||
303 altsd->bInterfaceProtocol != UAC_VERSION_2 ||
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100304 altsd->bNumEndpoints < 1)
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100305 return false;
306 epd = get_endpoint(alts, 0);
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100307 if (!usb_endpoint_is_isoc_in(epd) ||
308 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
309 USB_ENDPOINT_USAGE_IMPLICIT_FB)
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100310 return false;
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100311 *ep = epd->bEndpointAddress;
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100312 *altsp = alts;
313 return true;
314}
315
316/* Like the function above, but specific to Roland with vendor class and hack */
317static bool search_roland_implicit_fb(struct usb_device *dev, int ifnum,
318 unsigned int altsetting,
319 struct usb_host_interface **altsp,
320 unsigned int *ep)
321{
322 struct usb_interface *iface;
323 struct usb_host_interface *alts;
324 struct usb_interface_descriptor *altsd;
325 struct usb_endpoint_descriptor *epd;
326
327 iface = usb_ifnum_to_if(dev, ifnum);
328 if (!iface)
329 return false;
330 alts = usb_altnum_to_altsetting(iface, altsetting);
331 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,
354 struct usb_interface *iface,
355 struct usb_host_interface *alts)
Daniel Macke5779992010-03-04 19:46:13 +0100356{
Takashi Iwaif6581c02020-11-23 09:53:14 +0100357 struct usb_device *dev = chip->dev;
358 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
359 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;
426 if (search_generic_implicit_fb(dev, ifnum,
427 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;
439 if (search_roland_implicit_fb(dev, ifnum,
440 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;
472 struct usb_interface *iface;
473 struct usb_host_interface *alts;
474 struct usb_interface_descriptor *altsd;
475 unsigned int ep, attr, sync_attr;
476 bool is_playback;
Eldad Zacka60945f2013-08-03 10:50:18 +0200477 int err;
478
Takashi Iwaif6581c02020-11-23 09:53:14 +0100479 iface = usb_ifnum_to_if(dev, fmt->iface);
480 if (!iface)
Manuel Reinhardt2bc16b92019-01-31 15:32:35 +0100481 return 0;
Takashi Iwaif6581c02020-11-23 09:53:14 +0100482 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
483 if (!alts)
484 return 0;
485 altsd = get_iface_desc(alts);
486
487 is_playback = !(get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
488 if (is_playback) {
489 err = audioformat_implicit_fb_quirk(chip, fmt, iface, alts);
490 if (err > 0)
491 return 0;
492 }
Manuel Reinhardt2bc16b92019-01-31 15:32:35 +0100493
Eldad Zackf34d0652013-08-03 10:50:19 +0200494 if (altsd->bNumEndpoints < 2)
495 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200496
Takashi Iwaif6581c02020-11-23 09:53:14 +0100497 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500498 if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
499 attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
Eldad Zackf34d0652013-08-03 10:50:19 +0200500 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
501 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200502
Takashi Iwaif6581c02020-11-23 09:53:14 +0100503 sync_attr = get_endpoint(alts, 1)->bmAttributes;
504
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500505 /*
506 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
507 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
508 * error fall back to SYNC mode and don't create sync endpoint
509 */
510
Eldad Zackf34d0652013-08-03 10:50:19 +0200511 /* check sync-pipe endpoint */
512 /* ... and check descriptor size before accessing bSynchAddress
513 because there is a version of the SB Audigy 2 NX firmware lacking
514 the audio fields in the endpoint descriptors */
Takashi Iwaif6581c02020-11-23 09:53:14 +0100515 if ((sync_attr & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
Eldad Zackf34d0652013-08-03 10:50:19 +0200516 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zack95fec882013-08-03 10:50:20 +0200517 get_endpoint(alts, 1)->bSynchAddress != 0)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100518 dev_err(&dev->dev,
519 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
520 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200521 get_endpoint(alts, 1)->bmAttributes,
522 get_endpoint(alts, 1)->bLength,
523 get_endpoint(alts, 1)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500524 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
525 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200526 return -EINVAL;
Daniel Mackedcd3632012-04-12 13:51:12 +0200527 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200528 ep = get_endpoint(alts, 1)->bEndpointAddress;
Eldad Zack95fec882013-08-03 10:50:20 +0200529 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Ard van Breemen1b341212019-08-02 13:52:14 +0200530 get_endpoint(alts, 0)->bSynchAddress != 0 &&
Eldad Zackf34d0652013-08-03 10:50:19 +0200531 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
532 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100533 dev_err(&dev->dev,
534 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
535 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200536 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500537 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
538 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200539 return -EINVAL;
540 }
541
Takashi Iwaif6581c02020-11-23 09:53:14 +0100542 fmt->sync_ep = ep;
543 fmt->sync_iface = altsd->bInterfaceNumber;
544 fmt->sync_altsetting = altsd->bAlternateSetting;
545 if ((sync_attr & USB_ENDPOINT_USAGE_MASK) == USB_ENDPOINT_USAGE_IMPLICIT_FB)
546 fmt->implicit_fb = 1;
547
548 dev_dbg(&dev->dev, "%d:%d: found sync_ep=0x%x, iface=%d, alt=%d, implicit_fb=%d\n",
549 fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface,
550 fmt->sync_altsetting, fmt->implicit_fb);
551
552 return 0;
553}
554
555static int set_sync_endpoint(struct snd_usb_substream *subs,
556 struct audioformat *fmt)
557{
558 struct usb_device *dev = subs->dev;
559 struct usb_interface *iface;
560 struct usb_host_interface *alts;
561 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
562 unsigned int ep;
563 int err;
564
565 subs->sync_endpoint = NULL;
566 subs->data_endpoint->sync_master = NULL;
567
568 ep = fmt->sync_ep;
569 if (!ep)
570 return 0;
571
572 iface = usb_ifnum_to_if(dev, fmt->sync_iface);
573 if (!iface)
574 return 0;
575
576 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
577 if (!alts)
578 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200579
580 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
581 alts, ep, !subs->direction,
Takashi Iwaif6581c02020-11-23 09:53:14 +0100582 fmt->implicit_fb ?
583 SND_USB_ENDPOINT_TYPE_DATA :
584 SND_USB_ENDPOINT_TYPE_SYNC);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500585 if (!subs->sync_endpoint) {
Takashi Iwaif6581c02020-11-23 09:53:14 +0100586 if (is_playback &&
587 (fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) == USB_ENDPOINT_SYNC_NONE)
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500588 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200589 return -EINVAL;
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500590 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200591
Takashi Iwaif6581c02020-11-23 09:53:14 +0100592 subs->sync_endpoint->is_implicit_feedback = fmt->implicit_fb;
Erwin Burema10ce77e2020-05-10 20:29:11 +0200593
Eldad Zackf34d0652013-08-03 10:50:19 +0200594 subs->data_endpoint->sync_master = subs->sync_endpoint;
Daniel Macke5779992010-03-04 19:46:13 +0100595
Takashi Iwai4974b792020-11-23 09:53:08 +0100596 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
597 subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
598 err = usb_set_interface(subs->dev,
599 subs->sync_endpoint->iface,
600 subs->sync_endpoint->altsetting);
601 if (err < 0)
602 return err;
603 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
604 subs->sync_endpoint->iface,
605 subs->sync_endpoint->altsetting);
606 snd_usb_set_interface_quirk(dev);
607 }
608
Eldad Zack71bb64c2013-08-03 10:50:17 +0200609 return 0;
610}
611
612/*
613 * find a matching format and set up the interface
614 */
615static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
616{
617 struct usb_device *dev = subs->dev;
618 struct usb_host_interface *alts;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200619 struct usb_interface *iface;
620 int err;
621
622 iface = usb_ifnum_to_if(dev, fmt->iface);
623 if (WARN_ON(!iface))
624 return -EINVAL;
Takashi Iwaib099b962018-05-02 09:36:28 +0200625 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
Johan Hovold01412542019-12-20 10:31:34 +0100626 if (WARN_ON(!alts))
Eldad Zack71bb64c2013-08-03 10:50:17 +0200627 return -EINVAL;
628
Hui Wang92adc962019-12-18 21:26:50 +0800629 if (fmt == subs->cur_audiofmt && !subs->need_setup_fmt)
Eldad Zack71bb64c2013-08-03 10:50:17 +0200630 return 0;
631
632 /* close the old interface */
Hui Wang92adc962019-12-18 21:26:50 +0800633 if (subs->interface >= 0 && (subs->interface != fmt->iface || subs->need_setup_fmt)) {
Takashi Iwai8a463222018-05-02 10:04:27 +0200634 if (!subs->stream->chip->keep_iface) {
635 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;
641 }
Eldad Zack71bb64c2013-08-03 10:50:17 +0200642 }
643 subs->interface = -1;
644 subs->altset_idx = 0;
645 }
646
Hui Wang92adc962019-12-18 21:26:50 +0800647 if (subs->need_setup_fmt)
648 subs->need_setup_fmt = false;
649
Eldad Zack71bb64c2013-08-03 10:50:17 +0200650 /* set interface */
Takashi Iwaib099b962018-05-02 09:36:28 +0200651 if (iface->cur_altsetting != alts) {
Jurgen Kramer6874daa2014-11-28 17:32:54 +0100652 err = snd_usb_select_mode_quirk(subs, fmt);
653 if (err < 0)
654 return -EIO;
655
Eldad Zack71bb64c2013-08-03 10:50:17 +0200656 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
657 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100658 dev_err(&dev->dev,
659 "%d:%d: usb_set_interface failed (%d)\n",
660 fmt->iface, fmt->altsetting, err);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200661 return -EIO;
662 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100663 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
664 fmt->iface, fmt->altsetting);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200665 snd_usb_set_interface_quirk(dev);
666 }
667
Takashi Iwaib099b962018-05-02 09:36:28 +0200668 subs->interface = fmt->iface;
669 subs->altset_idx = fmt->altset_idx;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200670 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
671 alts, fmt->endpoint, subs->direction,
672 SND_USB_ENDPOINT_TYPE_DATA);
673
674 if (!subs->data_endpoint)
675 return -EINVAL;
676
Takashi Iwaif6581c02020-11-23 09:53:14 +0100677 err = set_sync_endpoint(subs, fmt);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200678 if (err < 0)
679 return err;
680
Eldad Zackd133f2c2013-08-03 10:50:16 +0200681 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
682 if (err < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100683 return err;
684
685 subs->cur_audiofmt = fmt;
686
687 snd_usb_set_format_quirk(subs, fmt);
688
Daniel Macke5779992010-03-04 19:46:13 +0100689 return 0;
690}
691
692/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100693 * Return the score of matching two audioformats.
694 * Veto the audioformat if:
695 * - It has no channels for some reason.
696 * - Requested PCM format is not supported.
697 * - Requested sample rate is not supported.
698 */
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100699static int match_endpoint_audioformats(struct snd_usb_substream *subs,
700 struct audioformat *fp,
701 struct audioformat *match, int rate,
702 snd_pcm_format_t pcm_format)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100703{
704 int i;
705 int score = 0;
706
707 if (fp->channels < 1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100708 dev_dbg(&subs->dev->dev,
709 "%s: (fmt @%p) no channels\n", __func__, fp);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100710 return 0;
711 }
712
Eldad Zack74c34ca2013-04-23 01:00:41 +0200713 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100714 dev_dbg(&subs->dev->dev,
715 "%s: (fmt @%p) no match for format %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100716 fp, pcm_format);
717 return 0;
718 }
719
720 for (i = 0; i < fp->nr_rates; i++) {
721 if (fp->rate_table[i] == rate) {
722 score++;
723 break;
724 }
725 }
726 if (!score) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100727 dev_dbg(&subs->dev->dev,
728 "%s: (fmt @%p) no match for rate %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100729 fp, rate);
730 return 0;
731 }
732
733 if (fp->channels == match->channels)
734 score++;
735
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100736 dev_dbg(&subs->dev->dev,
737 "%s: (fmt @%p) score %d\n", __func__, fp, score);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100738
739 return score;
740}
741
742/*
743 * Configure the sync ep using the rate and pcm format of the data ep.
744 */
745static int configure_sync_endpoint(struct snd_usb_substream *subs)
746{
747 int ret;
748 struct audioformat *fp;
749 struct audioformat *sync_fp = NULL;
750 int cur_score = 0;
751 int sync_period_bytes = subs->period_bytes;
752 struct snd_usb_substream *sync_subs =
753 &subs->stream->substream[subs->direction ^ 1];
754
Takashi Iwai31be5422013-01-10 14:06:38 +0100755 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
756 !subs->stream)
757 return snd_usb_endpoint_set_params(subs->sync_endpoint,
758 subs->pcm_format,
759 subs->channels,
760 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400761 0, 0,
Takashi Iwai31be5422013-01-10 14:06:38 +0100762 subs->cur_rate,
763 subs->cur_audiofmt,
764 NULL);
765
Eldad Zack0d9741c2012-12-03 20:30:09 +0100766 /* Try to find the best matching audioformat. */
767 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100768 int score = match_endpoint_audioformats(subs,
769 fp, subs->cur_audiofmt,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100770 subs->cur_rate, subs->pcm_format);
771
772 if (score > cur_score) {
773 sync_fp = fp;
774 cur_score = score;
775 }
776 }
777
778 if (unlikely(sync_fp == NULL)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100779 dev_err(&subs->dev->dev,
780 "%s: no valid audioformat for sync ep %x found\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100781 __func__, sync_subs->ep_num);
782 return -EINVAL;
783 }
784
785 /*
786 * Recalculate the period bytes if channel number differ between
787 * data and sync ep audioformat.
788 */
789 if (sync_fp->channels != subs->channels) {
790 sync_period_bytes = (subs->period_bytes / subs->channels) *
791 sync_fp->channels;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100792 dev_dbg(&subs->dev->dev,
793 "%s: adjusted sync ep period bytes (%d -> %d)\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100794 __func__, subs->period_bytes, sync_period_bytes);
795 }
796
797 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
798 subs->pcm_format,
799 sync_fp->channels,
800 sync_period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400801 0, 0,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100802 subs->cur_rate,
803 sync_fp,
804 NULL);
805
806 return ret;
807}
808
809/*
Dylan Reid61a70952012-09-18 09:49:48 -0700810 * configure endpoint params
811 *
812 * called during initial setup and upon resume
813 */
814static int configure_endpoint(struct snd_usb_substream *subs)
815{
816 int ret;
817
Dylan Reid61a70952012-09-18 09:49:48 -0700818 /* format changed */
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100819 stop_endpoints(subs);
820 sync_pending_stops(subs);
Dylan Reid61a70952012-09-18 09:49:48 -0700821 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
822 subs->pcm_format,
823 subs->channels,
824 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400825 subs->period_frames,
826 subs->buffer_periods,
Dylan Reid61a70952012-09-18 09:49:48 -0700827 subs->cur_rate,
828 subs->cur_audiofmt,
829 subs->sync_endpoint);
830 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200831 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700832
833 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100834 ret = configure_sync_endpoint(subs);
835
Dylan Reid61a70952012-09-18 09:49:48 -0700836 return ret;
837}
838
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100839static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state)
840{
841 int ret;
842
843 if (!subs->str_pd)
844 return 0;
845
846 ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
847 if (ret < 0) {
848 dev_err(&subs->dev->dev,
849 "Cannot change Power Domain ID: %d to state: %d. Err: %d\n",
850 subs->str_pd->pd_id, state, ret);
851 return ret;
852 }
853
854 return 0;
855}
856
857int snd_usb_pcm_suspend(struct snd_usb_stream *as)
858{
859 int ret;
860
861 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2);
862 if (ret < 0)
863 return ret;
864
865 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2);
866 if (ret < 0)
867 return ret;
868
869 return 0;
870}
871
872int snd_usb_pcm_resume(struct snd_usb_stream *as)
873{
874 int ret;
875
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100876 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1);
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100877 if (ret < 0)
878 return ret;
879
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100880 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1);
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100881 if (ret < 0)
882 return ret;
883
884 return 0;
885}
886
Dylan Reid61a70952012-09-18 09:49:48 -0700887/*
Daniel Macke5779992010-03-04 19:46:13 +0100888 * hw_params callback
889 *
890 * allocate a buffer and set the given audio format.
891 *
892 * so far we use a physically linear buffer although packetize transfer
893 * doesn't need a continuous area.
894 * if sg buffer is supported on the later version of alsa, we'll follow
895 * that.
896 */
897static int snd_usb_hw_params(struct snd_pcm_substream *substream,
898 struct snd_pcm_hw_params *hw_params)
899{
900 struct snd_usb_substream *subs = substream->runtime->private_data;
901 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700902 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100903
Shuah Khan66354f12019-04-01 20:40:22 -0400904 ret = snd_media_start_pipeline(subs);
905 if (ret)
906 return ret;
907
Dylan Reid61a70952012-09-18 09:49:48 -0700908 subs->pcm_format = params_format(hw_params);
909 subs->period_bytes = params_period_bytes(hw_params);
Alan Stern976b6c02013-09-24 15:51:58 -0400910 subs->period_frames = params_period_size(hw_params);
911 subs->buffer_periods = params_periods(hw_params);
Dylan Reid61a70952012-09-18 09:49:48 -0700912 subs->channels = params_channels(hw_params);
913 subs->cur_rate = params_rate(hw_params);
914
915 fmt = find_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100916 if (!fmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100917 dev_dbg(&subs->dev->dev,
918 "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700919 subs->pcm_format, subs->cur_rate, subs->channels);
Shuah Khan66354f12019-04-01 20:40:22 -0400920 ret = -EINVAL;
921 goto stop_pipeline;
Daniel Macke5779992010-03-04 19:46:13 +0100922 }
923
Takashi Iwai47ab1542015-08-25 16:09:00 +0200924 ret = snd_usb_lock_shutdown(subs->stream->chip);
925 if (ret < 0)
Shuah Khan66354f12019-04-01 20:40:22 -0400926 goto stop_pipeline;
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100927
928 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
Takashi Iwai978520b2012-10-12 15:12:55 +0200929 if (ret < 0)
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100930 goto unlock;
931
932 ret = set_format(subs, fmt);
933 if (ret < 0)
934 goto unlock;
Daniel Macke5779992010-03-04 19:46:13 +0100935
Dylan Reid61a70952012-09-18 09:49:48 -0700936 subs->interface = fmt->iface;
937 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200938 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100939
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100940 unlock:
941 snd_usb_unlock_shutdown(subs->stream->chip);
Shuah Khan66354f12019-04-01 20:40:22 -0400942 if (ret < 0)
943 goto stop_pipeline;
944 return ret;
945
946 stop_pipeline:
947 snd_media_stop_pipeline(subs);
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100948 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100949}
950
951/*
952 * hw_free callback
953 *
954 * reset the audio format and release the buffer
955 */
956static int snd_usb_hw_free(struct snd_pcm_substream *substream)
957{
958 struct snd_usb_substream *subs = substream->runtime->private_data;
959
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 Iwai47ab1542015-08-25 16:09:00 +0200964 if (!snd_usb_lock_shutdown(subs->stream->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 Iwai47ab1542015-08-25 16:09:00 +0200974 snd_usb_unlock_shutdown(subs->stream->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
Bhumika Goyalaaffbf72017-08-17 14:45:59 +05301055static const struct snd_pcm_hardware snd_usb_hardware =
Daniel Macke5779992010-03-04 19:46:13 +01001056{
1057 .info = SNDRV_PCM_INFO_MMAP |
1058 SNDRV_PCM_INFO_MMAP_VALID |
1059 SNDRV_PCM_INFO_BATCH |
1060 SNDRV_PCM_INFO_INTERLEAVED |
1061 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1062 SNDRV_PCM_INFO_PAUSE,
1063 .buffer_bytes_max = 1024 * 1024,
1064 .period_bytes_min = 64,
1065 .period_bytes_max = 512 * 1024,
1066 .periods_min = 2,
1067 .periods_max = 1024,
1068};
1069
1070static int hw_check_valid_format(struct snd_usb_substream *subs,
1071 struct snd_pcm_hw_params *params,
1072 struct audioformat *fp)
1073{
1074 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1075 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1076 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1077 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001078 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +01001079 unsigned int ptime;
1080
1081 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001082 snd_mask_none(&check_fmts);
1083 check_fmts.bits[0] = (u32)fp->formats;
1084 check_fmts.bits[1] = (u32)(fp->formats >> 32);
1085 snd_mask_intersect(&check_fmts, fmts);
1086 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +01001087 hwc_debug(" > check: no supported format %d\n", fp->format);
1088 return 0;
1089 }
1090 /* check the channels */
1091 if (fp->channels < ct->min || fp->channels > ct->max) {
1092 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1093 return 0;
1094 }
1095 /* check the rate is within the range */
1096 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1097 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1098 return 0;
1099 }
1100 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1101 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1102 return 0;
1103 }
1104 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +02001105 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +01001106 ptime = 125 * (1 << fp->datainterval);
1107 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
1108 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
1109 return 0;
1110 }
1111 }
1112 return 1;
1113}
1114
1115static int hw_rule_rate(struct snd_pcm_hw_params *params,
1116 struct snd_pcm_hw_rule *rule)
1117{
1118 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001119 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001120 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
Takashi Iwaibc4e94a2020-11-23 09:53:07 +01001121 unsigned int rmin, rmax, r;
Daniel Macke5779992010-03-04 19:46:13 +01001122 int changed;
Takashi Iwaibc4e94a2020-11-23 09:53:07 +01001123 int i;
Daniel Macke5779992010-03-04 19:46:13 +01001124
1125 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
Takashi Iwaibc4e94a2020-11-23 09:53:07 +01001126 rmin = UINT_MAX;
1127 rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001128 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001129 if (!hw_check_valid_format(subs, params, fp))
1130 continue;
Takashi Iwaibc4e94a2020-11-23 09:53:07 +01001131 if (fp->rate_table && fp->nr_rates) {
1132 for (i = 0; i < fp->nr_rates; i++) {
1133 r = fp->rate_table[i];
1134 if (!snd_interval_test(it, r))
1135 continue;
1136 rmin = min(rmin, r);
1137 rmax = max(rmax, r);
1138 }
Daniel Macke5779992010-03-04 19:46:13 +01001139 } else {
Takashi Iwaibc4e94a2020-11-23 09:53:07 +01001140 rmin = min(rmin, fp->rate_min);
1141 rmax = max(rmax, fp->rate_max);
Daniel Macke5779992010-03-04 19:46:13 +01001142 }
1143 }
1144
Takashi Iwaibc4e94a2020-11-23 09:53:07 +01001145 if (rmin > rmax) {
Daniel Macke5779992010-03-04 19:46:13 +01001146 hwc_debug(" --> get empty\n");
1147 it->empty = 1;
1148 return -EINVAL;
1149 }
1150
1151 changed = 0;
1152 if (it->min < rmin) {
1153 it->min = rmin;
1154 it->openmin = 0;
1155 changed = 1;
1156 }
1157 if (it->max > rmax) {
1158 it->max = rmax;
1159 it->openmax = 0;
1160 changed = 1;
1161 }
1162 if (snd_interval_checkempty(it)) {
1163 it->empty = 1;
1164 return -EINVAL;
1165 }
1166 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1167 return changed;
1168}
1169
1170
1171static int hw_rule_channels(struct snd_pcm_hw_params *params,
1172 struct snd_pcm_hw_rule *rule)
1173{
1174 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001175 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001176 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1177 unsigned int rmin, rmax;
1178 int changed;
1179
1180 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1181 changed = 0;
1182 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001183 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001184 if (!hw_check_valid_format(subs, params, fp))
1185 continue;
1186 if (changed++) {
1187 if (rmin > fp->channels)
1188 rmin = fp->channels;
1189 if (rmax < fp->channels)
1190 rmax = fp->channels;
1191 } else {
1192 rmin = fp->channels;
1193 rmax = fp->channels;
1194 }
1195 }
1196
1197 if (!changed) {
1198 hwc_debug(" --> get empty\n");
1199 it->empty = 1;
1200 return -EINVAL;
1201 }
1202
1203 changed = 0;
1204 if (it->min < rmin) {
1205 it->min = rmin;
1206 it->openmin = 0;
1207 changed = 1;
1208 }
1209 if (it->max > rmax) {
1210 it->max = rmax;
1211 it->openmax = 0;
1212 changed = 1;
1213 }
1214 if (snd_interval_checkempty(it)) {
1215 it->empty = 1;
1216 return -EINVAL;
1217 }
1218 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1219 return changed;
1220}
1221
1222static int hw_rule_format(struct snd_pcm_hw_params *params,
1223 struct snd_pcm_hw_rule *rule)
1224{
1225 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001226 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001227 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1228 u64 fbits;
1229 u32 oldbits[2];
1230 int changed;
1231
1232 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1233 fbits = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001234 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001235 if (!hw_check_valid_format(subs, params, fp))
1236 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001237 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +01001238 }
1239
1240 oldbits[0] = fmt->bits[0];
1241 oldbits[1] = fmt->bits[1];
1242 fmt->bits[0] &= (u32)fbits;
1243 fmt->bits[1] &= (u32)(fbits >> 32);
1244 if (!fmt->bits[0] && !fmt->bits[1]) {
1245 hwc_debug(" --> get empty\n");
1246 return -EINVAL;
1247 }
1248 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1249 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1250 return changed;
1251}
1252
1253static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1254 struct snd_pcm_hw_rule *rule)
1255{
1256 struct snd_usb_substream *subs = rule->private;
1257 struct audioformat *fp;
1258 struct snd_interval *it;
1259 unsigned char min_datainterval;
1260 unsigned int pmin;
1261 int changed;
1262
1263 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1264 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1265 min_datainterval = 0xff;
1266 list_for_each_entry(fp, &subs->fmt_list, list) {
1267 if (!hw_check_valid_format(subs, params, fp))
1268 continue;
1269 min_datainterval = min(min_datainterval, fp->datainterval);
1270 }
1271 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +02001272 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +01001273 it->empty = 1;
1274 return -EINVAL;
1275 }
1276 pmin = 125 * (1 << min_datainterval);
1277 changed = 0;
1278 if (it->min < pmin) {
1279 it->min = pmin;
1280 it->openmin = 0;
1281 changed = 1;
1282 }
1283 if (snd_interval_checkempty(it)) {
1284 it->empty = 1;
1285 return -EINVAL;
1286 }
1287 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1288 return changed;
1289}
1290
Daniel Macke5779992010-03-04 19:46:13 +01001291
1292/*
1293 * set up the runtime hardware information.
1294 */
1295
1296static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1297{
Eldad Zack88766f02013-04-03 23:18:49 +02001298 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001299 unsigned int pt, ptmin;
1300 int param_period_time_if_needed;
1301 int err;
1302
1303 runtime->hw.formats = subs->formats;
1304
1305 runtime->hw.rate_min = 0x7fffffff;
1306 runtime->hw.rate_max = 0;
1307 runtime->hw.channels_min = 256;
1308 runtime->hw.channels_max = 0;
1309 runtime->hw.rates = 0;
1310 ptmin = UINT_MAX;
1311 /* check min/max rates and channels */
Eldad Zack88766f02013-04-03 23:18:49 +02001312 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001313 runtime->hw.rates |= fp->rates;
1314 if (runtime->hw.rate_min > fp->rate_min)
1315 runtime->hw.rate_min = fp->rate_min;
1316 if (runtime->hw.rate_max < fp->rate_max)
1317 runtime->hw.rate_max = fp->rate_max;
1318 if (runtime->hw.channels_min > fp->channels)
1319 runtime->hw.channels_min = fp->channels;
1320 if (runtime->hw.channels_max < fp->channels)
1321 runtime->hw.channels_max = fp->channels;
1322 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1323 /* FIXME: there might be more than one audio formats... */
1324 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1325 fp->frame_size;
1326 }
1327 pt = 125 * (1 << fp->datainterval);
1328 ptmin = min(ptmin, pt);
1329 }
1330
1331 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001332 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001333 /* full speed devices have fixed data packet interval */
1334 ptmin = 1000;
1335 if (ptmin == 1000)
1336 /* if period time doesn't go below 1 ms, no rules needed */
1337 param_period_time_if_needed = -1;
Daniel Macke5779992010-03-04 19:46:13 +01001338
Takashi Iwaie92be812018-05-27 15:09:15 +02001339 err = snd_pcm_hw_constraint_minmax(runtime,
1340 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1341 ptmin, UINT_MAX);
1342 if (err < 0)
1343 return err;
1344
1345 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1346 hw_rule_rate, subs,
1347 SNDRV_PCM_HW_PARAM_FORMAT,
1348 SNDRV_PCM_HW_PARAM_CHANNELS,
1349 param_period_time_if_needed,
1350 -1);
1351 if (err < 0)
1352 return err;
1353 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1354 hw_rule_channels, subs,
1355 SNDRV_PCM_HW_PARAM_FORMAT,
1356 SNDRV_PCM_HW_PARAM_RATE,
1357 param_period_time_if_needed,
1358 -1);
1359 if (err < 0)
1360 return err;
1361 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1362 hw_rule_format, subs,
1363 SNDRV_PCM_HW_PARAM_RATE,
1364 SNDRV_PCM_HW_PARAM_CHANNELS,
1365 param_period_time_if_needed,
1366 -1);
1367 if (err < 0)
1368 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001369 if (param_period_time_if_needed >= 0) {
1370 err = snd_pcm_hw_rule_add(runtime, 0,
1371 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1372 hw_rule_period_time, subs,
1373 SNDRV_PCM_HW_PARAM_FORMAT,
1374 SNDRV_PCM_HW_PARAM_CHANNELS,
1375 SNDRV_PCM_HW_PARAM_RATE,
1376 -1);
1377 if (err < 0)
Takashi Iwaie92be812018-05-27 15:09:15 +02001378 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001379 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001380
Takashi Iwai18652112020-11-23 09:53:15 +01001381 return 0;
Daniel Macke5779992010-03-04 19:46:13 +01001382}
1383
Takashi Iwai6fddc792018-05-27 13:59:03 +02001384static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
Daniel Macke5779992010-03-04 19:46:13 +01001385{
Takashi Iwai6fddc792018-05-27 13:59:03 +02001386 int direction = substream->stream;
Daniel Macke5779992010-03-04 19:46:13 +01001387 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1388 struct snd_pcm_runtime *runtime = substream->runtime;
1389 struct snd_usb_substream *subs = &as->substream[direction];
Shuah Khan66354f12019-04-01 20:40:22 -04001390 int ret;
Daniel Macke5779992010-03-04 19:46:13 +01001391
1392 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001393 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001394 runtime->hw = snd_usb_hardware;
1395 runtime->private_data = subs;
1396 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001397 /* runtime PM is also done there */
Daniel Mackd24f5062013-04-17 00:01:38 +08001398
1399 /* initialize DSD/DOP context */
1400 subs->dsd_dop.byte_idx = 0;
1401 subs->dsd_dop.channel = 0;
1402 subs->dsd_dop.marker = 1;
1403
Shuah Khan66354f12019-04-01 20:40:22 -04001404 ret = setup_hw_info(runtime, subs);
Takashi Iwai18652112020-11-23 09:53:15 +01001405 if (ret < 0)
1406 return ret;
1407 ret = snd_usb_autoresume(subs->stream->chip);
1408 if (ret < 0)
1409 return ret;
1410 ret = snd_media_stream_init(subs, as->pcm, direction);
1411 if (ret < 0)
1412 snd_usb_autosuspend(subs->stream->chip);
Shuah Khan66354f12019-04-01 20:40:22 -04001413 return ret;
Daniel Macke5779992010-03-04 19:46:13 +01001414}
1415
Takashi Iwai6fddc792018-05-27 13:59:03 +02001416static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
Daniel Macke5779992010-03-04 19:46:13 +01001417{
Takashi Iwai6fddc792018-05-27 13:59:03 +02001418 int direction = substream->stream;
Daniel Macke5779992010-03-04 19:46:13 +01001419 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1420 struct snd_usb_substream *subs = &as->substream[direction];
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001421 int ret;
Daniel Macke5779992010-03-04 19:46:13 +01001422
Shuah Khan66354f12019-04-01 20:40:22 -04001423 snd_media_stop_pipeline(subs);
Daniel Mack68e67f42012-07-12 13:08:40 +02001424
Takashi Iwai8a463222018-05-02 10:04:27 +02001425 if (!as->chip->keep_iface &&
1426 subs->interface >= 0 &&
Takashi Iwai47ab1542015-08-25 16:09:00 +02001427 !snd_usb_lock_shutdown(subs->stream->chip)) {
Daniel Mack68e67f42012-07-12 13:08:40 +02001428 usb_set_interface(subs->dev, subs->interface, 0);
1429 subs->interface = -1;
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001430 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
Takashi Iwai47ab1542015-08-25 16:09:00 +02001431 snd_usb_unlock_shutdown(subs->stream->chip);
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001432 if (ret < 0)
1433 return ret;
Daniel Mack68e67f42012-07-12 13:08:40 +02001434 }
1435
Daniel Macke5779992010-03-04 19:46:13 +01001436 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001437 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001438
Daniel Mack68e67f42012-07-12 13:08:40 +02001439 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001440}
1441
1442/* Since a URB can handle only a single linear buffer, we must use double
1443 * buffering when the data to be transferred overflows the buffer boundary.
1444 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1445 * for all URBs.
1446 */
1447static void retire_capture_urb(struct snd_usb_substream *subs,
1448 struct urb *urb)
1449{
1450 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1451 unsigned int stride, frames, bytes, oldptr;
1452 int i, period_elapsed = 0;
1453 unsigned long flags;
1454 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001455 int current_frame_number;
1456
1457 /* read frame number here, update pointer in critical section */
1458 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001459
1460 stride = runtime->frame_bits >> 3;
1461
1462 for (i = 0; i < urb->number_of_packets; i++) {
Calvin Owens1539d4f2013-04-12 22:33:59 -05001463 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
Daniel Mackedcd3632012-04-12 13:51:12 +02001464 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001465 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1466 i, urb->iso_frame_desc[i].status);
Daniel Mackedcd3632012-04-12 13:51:12 +02001467 // continue;
1468 }
1469 bytes = urb->iso_frame_desc[i].actual_length;
Hector Martin1b7ecc22020-08-10 17:24:00 +09001470 if (subs->stream_offset_adj > 0) {
1471 unsigned int adj = min(subs->stream_offset_adj, bytes);
1472 cp += adj;
1473 bytes -= adj;
1474 subs->stream_offset_adj -= adj;
1475 }
Daniel Mackedcd3632012-04-12 13:51:12 +02001476 frames = bytes / stride;
1477 if (!subs->txfr_quirk)
1478 bytes = frames * stride;
1479 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001480 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001481 bytes = frames * stride;
Takashi Iwai377a8792018-05-16 20:07:18 +02001482 dev_warn_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001483 "Corrected urb data len. %d->%d\n",
Daniel Mackedcd3632012-04-12 13:51:12 +02001484 oldbytes, bytes);
1485 }
1486 /* update the current pointer */
1487 spin_lock_irqsave(&subs->lock, flags);
1488 oldptr = subs->hwptr_done;
1489 subs->hwptr_done += bytes;
1490 if (subs->hwptr_done >= runtime->buffer_size * stride)
1491 subs->hwptr_done -= runtime->buffer_size * stride;
1492 frames = (bytes + (oldptr % stride)) / stride;
1493 subs->transfer_done += frames;
1494 if (subs->transfer_done >= runtime->period_size) {
1495 subs->transfer_done -= runtime->period_size;
1496 period_elapsed = 1;
1497 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001498 /* capture delay is by construction limited to one URB,
1499 * reset delays here
1500 */
1501 runtime->delay = subs->last_delay = 0;
1502
1503 /* realign last_frame_number */
1504 subs->last_frame_number = current_frame_number;
1505 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1506
Daniel Mackedcd3632012-04-12 13:51:12 +02001507 spin_unlock_irqrestore(&subs->lock, flags);
1508 /* copy a data chunk */
1509 if (oldptr + bytes > runtime->buffer_size * stride) {
1510 unsigned int bytes1 =
1511 runtime->buffer_size * stride - oldptr;
1512 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1513 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1514 } else {
1515 memcpy(runtime->dma_area + oldptr, cp, bytes);
1516 }
1517 }
1518
1519 if (period_elapsed)
1520 snd_pcm_period_elapsed(subs->pcm_substream);
1521}
1522
Daniel Mackd24f5062013-04-17 00:01:38 +08001523static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1524 struct urb *urb, unsigned int bytes)
1525{
1526 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1527 unsigned int stride = runtime->frame_bits >> 3;
1528 unsigned int dst_idx = 0;
1529 unsigned int src_idx = subs->hwptr_done;
1530 unsigned int wrap = runtime->buffer_size * stride;
1531 u8 *dst = urb->transfer_buffer;
1532 u8 *src = runtime->dma_area;
1533 u8 marker[] = { 0x05, 0xfa };
1534
1535 /*
1536 * The DSP DOP format defines a way to transport DSD samples over
1537 * normal PCM data endpoints. It requires stuffing of marker bytes
1538 * (0x05 and 0xfa, alternating per sample frame), and then expects
1539 * 2 additional bytes of actual payload. The whole frame is stored
1540 * LSB.
1541 *
1542 * Hence, for a stereo transport, the buffer layout looks like this,
1543 * where L refers to left channel samples and R to right.
1544 *
1545 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1546 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1547 * .....
1548 *
1549 */
1550
1551 while (bytes--) {
1552 if (++subs->dsd_dop.byte_idx == 3) {
1553 /* frame boundary? */
1554 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1555 src_idx += 2;
1556 subs->dsd_dop.byte_idx = 0;
1557
1558 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1559 /* alternate the marker */
1560 subs->dsd_dop.marker++;
1561 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1562 subs->dsd_dop.channel = 0;
1563 }
1564 } else {
1565 /* stuff the DSD payload */
1566 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001567
1568 if (subs->cur_audiofmt->dsd_bitrev)
1569 dst[dst_idx++] = bitrev8(src[idx]);
1570 else
1571 dst[dst_idx++] = src[idx];
1572
Daniel Mackd24f5062013-04-17 00:01:38 +08001573 subs->hwptr_done++;
1574 }
1575 }
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001576 if (subs->hwptr_done >= runtime->buffer_size * stride)
1577 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001578}
1579
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001580static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1581 int offset, int stride, unsigned int bytes)
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001582{
1583 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1584
1585 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1586 /* err, the transferred area goes over buffer boundary. */
1587 unsigned int bytes1 =
1588 runtime->buffer_size * stride - subs->hwptr_done;
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001589 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001590 runtime->dma_area + subs->hwptr_done, bytes1);
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001591 memcpy(urb->transfer_buffer + offset + bytes1,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001592 runtime->dma_area, bytes - bytes1);
1593 } else {
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001594 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001595 runtime->dma_area + subs->hwptr_done, bytes);
1596 }
1597 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001598 if (subs->hwptr_done >= runtime->buffer_size * stride)
1599 subs->hwptr_done -= runtime->buffer_size * stride;
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001600}
1601
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001602static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1603 struct urb *urb, int stride,
1604 unsigned int bytes)
1605{
1606 __le32 packet_length;
1607 int i;
1608
1609 /* Put __le32 length descriptor at start of each packet. */
1610 for (i = 0; i < urb->number_of_packets; i++) {
1611 unsigned int length = urb->iso_frame_desc[i].length;
1612 unsigned int offset = urb->iso_frame_desc[i].offset;
1613
1614 packet_length = cpu_to_le32(length);
1615 offset += i * sizeof(packet_length);
1616 urb->iso_frame_desc[i].offset = offset;
1617 urb->iso_frame_desc[i].length += sizeof(packet_length);
1618 memcpy(urb->transfer_buffer + offset,
1619 &packet_length, sizeof(packet_length));
1620 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1621 stride, length);
1622 }
1623 /* Adjust transfer size accordingly. */
1624 bytes += urb->number_of_packets * sizeof(packet_length);
1625 return bytes;
1626}
1627
Daniel Mackedcd3632012-04-12 13:51:12 +02001628static void prepare_playback_urb(struct snd_usb_substream *subs,
1629 struct urb *urb)
1630{
1631 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001632 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001633 struct snd_urb_ctx *ctx = urb->context;
1634 unsigned int counts, frames, bytes;
1635 int i, stride, period_elapsed = 0;
1636 unsigned long flags;
1637
1638 stride = runtime->frame_bits >> 3;
1639
1640 frames = 0;
1641 urb->number_of_packets = 0;
1642 spin_lock_irqsave(&subs->lock, flags);
Alan Stern976b6c02013-09-24 15:51:58 -04001643 subs->frame_limit += ep->max_urb_frames;
Daniel Mackedcd3632012-04-12 13:51:12 +02001644 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001645 if (ctx->packet_size[i])
1646 counts = ctx->packet_size[i];
Alexander Tsoyf0bd62b2020-04-24 05:24:48 +03001647 else if (ep->sync_master)
1648 counts = snd_usb_endpoint_slave_next_packet_size(ep);
Daniel Mack245baf92012-08-30 18:52:30 +02001649 else
1650 counts = snd_usb_endpoint_next_packet_size(ep);
1651
Daniel Mackedcd3632012-04-12 13:51:12 +02001652 /* set up descriptor */
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001653 urb->iso_frame_desc[i].offset = frames * ep->stride;
1654 urb->iso_frame_desc[i].length = counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001655 frames += counts;
1656 urb->number_of_packets++;
1657 subs->transfer_done += counts;
1658 if (subs->transfer_done >= runtime->period_size) {
1659 subs->transfer_done -= runtime->period_size;
Alan Stern976b6c02013-09-24 15:51:58 -04001660 subs->frame_limit = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001661 period_elapsed = 1;
1662 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1663 if (subs->transfer_done > 0) {
1664 /* FIXME: fill-max mode is not
1665 * supported yet */
1666 frames -= subs->transfer_done;
1667 counts -= subs->transfer_done;
1668 urb->iso_frame_desc[i].length =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001669 counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001670 subs->transfer_done = 0;
1671 }
1672 i++;
1673 if (i < ctx->packets) {
1674 /* add a transfer delimiter */
1675 urb->iso_frame_desc[i].offset =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001676 frames * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001677 urb->iso_frame_desc[i].length = 0;
1678 urb->number_of_packets++;
1679 }
1680 break;
1681 }
1682 }
Alan Stern976b6c02013-09-24 15:51:58 -04001683 /* finish at the period boundary or after enough frames */
1684 if ((period_elapsed ||
1685 subs->transfer_done >= subs->frame_limit) &&
1686 !snd_usb_endpoint_implicit_feedback_sink(ep))
Daniel Mackedcd3632012-04-12 13:51:12 +02001687 break;
1688 }
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001689 bytes = frames * ep->stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001690
1691 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1692 subs->cur_audiofmt->dsd_dop)) {
1693 fill_playback_urb_dsd_dop(subs, urb, bytes);
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001694 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1695 subs->cur_audiofmt->dsd_bitrev)) {
1696 /* bit-reverse the bytes */
1697 u8 *buf = urb->transfer_buffer;
1698 for (i = 0; i < bytes; i++) {
1699 int idx = (subs->hwptr_done + i)
1700 % (runtime->buffer_size * stride);
1701 buf[i] = bitrev8(runtime->dma_area[idx]);
1702 }
1703
1704 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001705 if (subs->hwptr_done >= runtime->buffer_size * stride)
1706 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001707 } else {
Daniel Mackd24f5062013-04-17 00:01:38 +08001708 /* usual PCM */
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001709 if (!subs->tx_length_quirk)
1710 copy_to_urb(subs, urb, 0, stride, bytes);
1711 else
1712 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1713 /* bytes is now amount of outgoing data */
Daniel Mackedcd3632012-04-12 13:51:12 +02001714 }
Daniel Mackd24f5062013-04-17 00:01:38 +08001715
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001716 /* update delay with exact number of samples queued */
1717 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001718 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001719 subs->last_delay = runtime->delay;
1720
1721 /* realign last_frame_number */
1722 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1723 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1724
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001725 if (subs->trigger_tstamp_pending_update) {
1726 /* this is the first actual URB submitted,
1727 * update trigger timestamp to reflect actual start time
1728 */
1729 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1730 subs->trigger_tstamp_pending_update = false;
1731 }
1732
Daniel Mackedcd3632012-04-12 13:51:12 +02001733 spin_unlock_irqrestore(&subs->lock, flags);
1734 urb->transfer_buffer_length = bytes;
1735 if (period_elapsed)
1736 snd_pcm_period_elapsed(subs->pcm_substream);
1737}
1738
1739/*
1740 * process after playback data complete
1741 * - decrease the delay count again
1742 */
1743static void retire_playback_urb(struct snd_usb_substream *subs,
1744 struct urb *urb)
1745{
1746 unsigned long flags;
1747 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001748 struct snd_usb_endpoint *ep = subs->data_endpoint;
1749 int processed = urb->transfer_buffer_length / ep->stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001750 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001751
Alexander Tsoy5ff40e62020-06-29 06:26:07 +03001752 /* ignore the delay accounting when processed=0 is given, i.e.
1753 * silent payloads are processed before handling the actual data
Takashi Iwai1213a202012-09-06 14:58:00 +02001754 */
1755 if (!processed)
1756 return;
1757
Daniel Mackedcd3632012-04-12 13:51:12 +02001758 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001759 if (!subs->last_delay)
1760 goto out; /* short path */
1761
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001762 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1763 /* update delay with exact number of samples played */
1764 if (processed > subs->last_delay)
1765 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001766 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001767 subs->last_delay -= processed;
1768 runtime->delay = subs->last_delay;
1769
1770 /*
1771 * Report when delay estimate is off by more than 2ms.
1772 * The error should be lower than 2ms since the estimate relies
1773 * on two reads of a counter updated every ms.
1774 */
Sander Eikelenboomb7a77232014-05-02 15:09:27 +02001775 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1776 dev_dbg_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001777 "delay: estimated %d, actual %d\n",
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001778 est_delay, subs->last_delay);
1779
Takashi Iwai48779a02012-11-23 16:00:37 +01001780 if (!subs->running) {
1781 /* update last_frame_number for delay counting here since
1782 * prepare_playback_urb won't be called during pause
1783 */
1784 subs->last_frame_number =
1785 usb_get_current_frame_number(subs->dev) & 0xff;
1786 }
1787
1788 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001789 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001790}
1791
Daniel Mackedcd3632012-04-12 13:51:12 +02001792static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1793 int cmd)
1794{
1795 struct snd_usb_substream *subs = substream->runtime->private_data;
1796
1797 switch (cmd) {
1798 case SNDRV_PCM_TRIGGER_START:
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001799 subs->trigger_tstamp_pending_update = true;
Gustavo A. R. Silvac0dbbda2020-07-08 15:32:36 -05001800 fallthrough;
Daniel Mackedcd3632012-04-12 13:51:12 +02001801 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1802 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1803 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001804 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001805 return 0;
1806 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001807 stop_endpoints(subs);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001808 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001809 return 0;
1810 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1811 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001812 /* keep retire_data_urb for delay calculation */
1813 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001814 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001815 return 0;
Hui Wang92adc962019-12-18 21:26:50 +08001816 case SNDRV_PCM_TRIGGER_SUSPEND:
1817 if (subs->stream->chip->setup_fmt_after_resume_quirk) {
Takashi Iwaia032ff02019-12-18 20:05:39 +01001818 stop_endpoints(subs);
Hui Wang92adc962019-12-18 21:26:50 +08001819 subs->need_setup_fmt = true;
1820 return 0;
1821 }
1822 break;
Daniel Mackedcd3632012-04-12 13:51:12 +02001823 }
1824
1825 return -EINVAL;
1826}
1827
Daniel Mackafe25962012-06-16 16:58:04 +02001828static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1829 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001830{
1831 int err;
1832 struct snd_usb_substream *subs = substream->runtime->private_data;
1833
1834 switch (cmd) {
1835 case SNDRV_PCM_TRIGGER_START:
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +02001836 err = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +02001837 if (err < 0)
1838 return err;
1839
1840 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001841 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001842 return 0;
1843 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001844 stop_endpoints(subs);
Takashi Iwaiff58bbc2020-06-16 14:09:21 +02001845 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001846 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001847 return 0;
1848 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1849 subs->data_endpoint->retire_data_urb = NULL;
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_RELEASE:
1853 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001854 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001855 return 0;
Hui Wang92adc962019-12-18 21:26:50 +08001856 case SNDRV_PCM_TRIGGER_SUSPEND:
1857 if (subs->stream->chip->setup_fmt_after_resume_quirk) {
Takashi Iwaia032ff02019-12-18 20:05:39 +01001858 stop_endpoints(subs);
Hui Wang92adc962019-12-18 21:26:50 +08001859 subs->need_setup_fmt = true;
1860 return 0;
1861 }
1862 break;
Daniel Mackedcd3632012-04-12 13:51:12 +02001863 }
1864
1865 return -EINVAL;
1866}
1867
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301868static const struct snd_pcm_ops snd_usb_playback_ops = {
Takashi Iwai6fddc792018-05-27 13:59:03 +02001869 .open = snd_usb_pcm_open,
1870 .close = snd_usb_pcm_close,
Daniel Macke5779992010-03-04 19:46:13 +01001871 .hw_params = snd_usb_hw_params,
1872 .hw_free = snd_usb_hw_free,
1873 .prepare = snd_usb_pcm_prepare,
1874 .trigger = snd_usb_substream_playback_trigger,
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001875 .sync_stop = snd_usb_pcm_sync_stop,
Daniel Macke5779992010-03-04 19:46:13 +01001876 .pointer = snd_usb_pcm_pointer,
Daniel Macke5779992010-03-04 19:46:13 +01001877};
1878
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301879static const struct snd_pcm_ops snd_usb_capture_ops = {
Takashi Iwai6fddc792018-05-27 13:59:03 +02001880 .open = snd_usb_pcm_open,
1881 .close = snd_usb_pcm_close,
Daniel Macke5779992010-03-04 19:46:13 +01001882 .hw_params = snd_usb_hw_params,
1883 .hw_free = snd_usb_hw_free,
1884 .prepare = snd_usb_pcm_prepare,
1885 .trigger = snd_usb_substream_capture_trigger,
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001886 .sync_stop = snd_usb_pcm_sync_stop,
Daniel Macke5779992010-03-04 19:46:13 +01001887 .pointer = snd_usb_pcm_pointer,
Takashi Iwaif274baa2018-05-27 13:01:17 +02001888};
1889
Daniel Macke5779992010-03-04 19:46:13 +01001890void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1891{
Takashi Iwaif274baa2018-05-27 13:01:17 +02001892 const struct snd_pcm_ops *ops;
1893
Takashi Iwaib315997d2019-11-05 16:18:40 +01001894 ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
Takashi Iwaif274baa2018-05-27 13:01:17 +02001895 &snd_usb_playback_ops : &snd_usb_capture_ops;
Takashi Iwaif274baa2018-05-27 13:01:17 +02001896 snd_pcm_set_ops(pcm, stream, ops);
1897}
1898
1899void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
1900{
1901 struct snd_pcm *pcm = subs->stream->pcm;
1902 struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
1903 struct device *dev = subs->dev->bus->controller;
1904
Takashi Iwaib315997d2019-11-05 16:18:40 +01001905 if (snd_usb_use_vmalloc)
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +01001906 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_VMALLOC,
1907 NULL, 0, 0);
Takashi Iwaib315997d2019-11-05 16:18:40 +01001908 else
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +01001909 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_DEV_SG,
1910 dev, 64*1024, 512*1024);
Daniel Macke5779992010-03-04 19:46:13 +01001911}