blob: d83a6a6ac023dfb7a281c676abbf1027d9069db8 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Daniel Macke5779992010-03-04 19:46:13 +01002/*
Daniel Macke5779992010-03-04 19:46:13 +01003 */
4
5#include <linux/init.h>
Stephen Rothwell9966dda2010-03-29 19:01:48 +11006#include <linux/slab.h>
Daniel Mack44dcbbb2013-04-17 00:01:39 +08007#include <linux/bitrev.h>
Daniel Mackedcd3632012-04-12 13:51:12 +02008#include <linux/ratelimit.h>
Daniel Macke5779992010-03-04 19:46:13 +01009#include <linux/usb.h>
10#include <linux/usb/audio.h>
Daniel Mack7e847892010-03-11 21:13:20 +010011#include <linux/usb/audio-v2.h>
Daniel Macke5779992010-03-04 19:46:13 +010012
13#include <sound/core.h>
14#include <sound/pcm.h>
15#include <sound/pcm_params.h>
16
17#include "usbaudio.h"
18#include "card.h"
19#include "quirks.h"
Daniel Mackc731bc92011-09-14 12:46:57 +020020#include "endpoint.h"
Daniel Macke5779992010-03-04 19:46:13 +010021#include "helper.h"
22#include "pcm.h"
Daniel Mack79f920f2010-05-31 14:51:31 +020023#include "clock.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010024#include "power.h"
Shuah Khan66354f12019-04-01 20:40:22 -040025#include "media.h"
Daniel Macke5779992010-03-04 19:46:13 +010026
Daniel Mackedcd3632012-04-12 13:51:12 +020027#define SUBSTREAM_FLAG_DATA_EP_STARTED 0
28#define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
29
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050030/* return the estimated delay based on USB frame counters */
31snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
32 unsigned int rate)
33{
34 int current_frame_number;
35 int frame_diff;
36 int est_delay;
37
Takashi Iwai48779a02012-11-23 16:00:37 +010038 if (!subs->last_delay)
39 return 0; /* short path */
40
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050041 current_frame_number = usb_get_current_frame_number(subs->dev);
42 /*
43 * HCD implementations use different widths, use lower 8 bits.
44 * The delay will be managed up to 256ms, which is more than
45 * enough
46 */
47 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
48
49 /* Approximation based on number of samples per USB frame (ms),
50 some truncation for 44.1 but the estimate is good enough */
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060051 est_delay = frame_diff * rate / 1000;
52 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
53 est_delay = subs->last_delay - est_delay;
54 else
55 est_delay = subs->last_delay + est_delay;
56
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050057 if (est_delay < 0)
58 est_delay = 0;
59 return est_delay;
60}
61
Daniel Macke5779992010-03-04 19:46:13 +010062/*
63 * return the current pcm pointer. just based on the hwptr_done value.
64 */
65static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
66{
Takashi Iwaie92be812018-05-27 15:09:15 +020067 struct snd_usb_substream *subs = substream->runtime->private_data;
Daniel Macke5779992010-03-04 19:46:13 +010068 unsigned int hwptr_done;
69
Takashi Iwai47ab1542015-08-25 16:09:00 +020070 if (atomic_read(&subs->stream->chip->shutdown))
Takashi Iwai978520b2012-10-12 15:12:55 +020071 return SNDRV_PCM_POS_XRUN;
Daniel Macke5779992010-03-04 19:46:13 +010072 spin_lock(&subs->lock);
73 hwptr_done = subs->hwptr_done;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060074 substream->runtime->delay = snd_usb_pcm_delay(subs,
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050075 substream->runtime->rate);
Daniel Macke5779992010-03-04 19:46:13 +010076 spin_unlock(&subs->lock);
77 return hwptr_done / (substream->runtime->frame_bits >> 3);
78}
79
80/*
81 * find a matching audio format
82 */
Takashi Iwai5a6c3e12020-11-23 09:53:16 +010083static struct audioformat *find_format(struct list_head *fmt_list_head,
84 snd_pcm_format_t format,
85 unsigned int rate,
86 unsigned int channels,
87 struct snd_usb_substream *subs)
Daniel Macke5779992010-03-04 19:46:13 +010088{
Eldad Zack88766f02013-04-03 23:18:49 +020089 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +010090 struct audioformat *found = NULL;
91 int cur_attr = 0, attr;
92
Takashi Iwai5a6c3e12020-11-23 09:53:16 +010093 list_for_each_entry(fp, fmt_list_head, list) {
94 if (!(fp->formats & pcm_format_to_bits(format)))
Clemens Ladisch015eb0b2010-03-04 19:46:15 +010095 continue;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +010096 if (fp->channels != channels)
Daniel Macke5779992010-03-04 19:46:13 +010097 continue;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +010098 if (rate < fp->rate_min || rate > fp->rate_max)
Daniel Macke5779992010-03-04 19:46:13 +010099 continue;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100100 if (!(fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
Daniel Macke5779992010-03-04 19:46:13 +0100101 unsigned int i;
102 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100103 if (fp->rate_table[i] == rate)
Daniel Macke5779992010-03-04 19:46:13 +0100104 break;
105 if (i >= fp->nr_rates)
106 continue;
107 }
108 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100109 if (!found) {
Daniel Macke5779992010-03-04 19:46:13 +0100110 found = fp;
111 cur_attr = attr;
112 continue;
113 }
114 /* avoid async out and adaptive in if the other method
115 * supports the same format.
116 * this is a workaround for the case like
117 * M-audio audiophile USB.
118 */
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100119 if (subs && attr != cur_attr) {
Daniel Macke5779992010-03-04 19:46:13 +0100120 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
121 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
122 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
123 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
124 continue;
125 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
126 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
127 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
128 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
129 found = fp;
130 cur_attr = attr;
131 continue;
132 }
133 }
134 /* find the format with the largest max. packet size */
135 if (fp->maxpacksize > found->maxpacksize) {
136 found = fp;
137 cur_attr = attr;
138 }
139 }
140 return found;
141}
142
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100143static struct audioformat *find_substream_format(struct snd_usb_substream *subs)
144{
145 return find_format(&subs->fmt_list, subs->pcm_format, subs->cur_rate,
146 subs->channels, subs);
147}
148
Daniel Mack767d75a2010-03-04 19:46:17 +0100149static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
150 struct usb_host_interface *alts,
151 struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +0100152{
Daniel Mack767d75a2010-03-04 19:46:17 +0100153 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100154 unsigned int ep;
155 unsigned char data[1];
156 int err;
157
Takashi Iwai447d6272016-03-15 15:20:58 +0100158 if (get_iface_desc(alts)->bNumEndpoints < 1)
159 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100160 ep = get_endpoint(alts, 0)->bEndpointAddress;
Daniel Mack767d75a2010-03-04 19:46:17 +0100161
Daniel Mack767d75a2010-03-04 19:46:17 +0100162 data[0] = 1;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200163 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
164 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
165 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
166 data, sizeof(data));
167 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100168 usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
169 iface, ep);
Daniel Mack767d75a2010-03-04 19:46:17 +0100170 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100171 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100172
Daniel Macke5779992010-03-04 19:46:13 +0100173 return 0;
174}
175
Daniel Mack92c25612010-05-26 18:11:39 +0200176static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
177 struct usb_host_interface *alts,
178 struct audioformat *fmt)
179{
180 struct usb_device *dev = chip->dev;
181 unsigned char data[1];
Daniel Mack92c25612010-05-26 18:11:39 +0200182 int err;
183
Daniel Mack92c25612010-05-26 18:11:39 +0200184 data[0] = 1;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200185 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
186 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
187 UAC2_EP_CS_PITCH << 8, 0,
188 data, sizeof(data));
189 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100190 usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
191 iface, fmt->altsetting);
Daniel Mack92c25612010-05-26 18:11:39 +0200192 return err;
193 }
194
195 return 0;
196}
197
Daniel Mack767d75a2010-03-04 19:46:17 +0100198/*
Daniel Mack92c25612010-05-26 18:11:39 +0200199 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100200 */
201int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
202 struct usb_host_interface *alts,
203 struct audioformat *fmt)
204{
Daniel Mack92c25612010-05-26 18:11:39 +0200205 /* if endpoint doesn't have pitch control, bail out */
206 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
207 return 0;
208
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100209 switch (fmt->protocol) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100210 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200211 default:
Daniel Mack767d75a2010-03-04 19:46:17 +0100212 return init_pitch_v1(chip, iface, alts, fmt);
213
214 case UAC_VERSION_2:
Daniel Mack92c25612010-05-26 18:11:39 +0200215 return init_pitch_v2(chip, iface, alts, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100216 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100217}
218
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200219static int start_endpoints(struct snd_usb_substream *subs)
Daniel Mackedcd3632012-04-12 13:51:12 +0200220{
221 int err;
222
223 if (!subs->data_endpoint)
224 return -EINVAL;
225
226 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
227 struct snd_usb_endpoint *ep = subs->data_endpoint;
228
Takashi Iwaie93e8902020-11-23 09:53:13 +0100229 dev_dbg(&subs->dev->dev, "Starting data EP 0x%x\n", ep->ep_num);
Daniel Mackedcd3632012-04-12 13:51:12 +0200230
231 ep->data_subs = subs;
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200232 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200233 if (err < 0) {
234 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
235 return err;
236 }
237 }
238
239 if (subs->sync_endpoint &&
240 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
241 struct snd_usb_endpoint *ep = subs->sync_endpoint;
242
Takashi Iwaie93e8902020-11-23 09:53:13 +0100243 dev_dbg(&subs->dev->dev, "Starting sync EP 0x%x\n", ep->ep_num);
Daniel Mackedcd3632012-04-12 13:51:12 +0200244
245 ep->sync_slave = subs->data_endpoint;
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200246 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200247 if (err < 0) {
248 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
Takashi Iwai18035032020-11-23 09:53:12 +0100249 ep->sync_slave = NULL;
Daniel Mackedcd3632012-04-12 13:51:12 +0200250 return err;
251 }
252 }
253
254 return 0;
255}
256
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100257static void sync_pending_stops(struct snd_usb_substream *subs)
258{
259 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
260 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
261}
262
263static void stop_endpoints(struct snd_usb_substream *subs)
Daniel Mackedcd3632012-04-12 13:51:12 +0200264{
Takashi Iwai18035032020-11-23 09:53:12 +0100265 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
Takashi Iwaie93e8902020-11-23 09:53:13 +0100266 dev_dbg(&subs->dev->dev, "Stopping sync EP 0x%x\n",
267 subs->sync_endpoint->ep_num);
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100268 snd_usb_endpoint_stop(subs->sync_endpoint);
Takashi Iwai18035032020-11-23 09:53:12 +0100269 subs->sync_endpoint->sync_slave = NULL;
270 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200271
Takashi Iwaie93e8902020-11-23 09:53:13 +0100272 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
273 dev_dbg(&subs->dev->dev, "Stopping data EP 0x%x\n",
274 subs->data_endpoint->ep_num);
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100275 snd_usb_endpoint_stop(subs->data_endpoint);
Takashi Iwaie93e8902020-11-23 09:53:13 +0100276 }
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100277}
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100278
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100279/* PCM sync_stop callback */
280static int snd_usb_pcm_sync_stop(struct snd_pcm_substream *substream)
281{
282 struct snd_usb_substream *subs = substream->runtime->private_data;
283
284 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
285 sync_pending_stops(subs);
286 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100287 }
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100288 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +0200289}
290
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100291/* Check whether the given iface:altsetting points to an implicit fb source */
292static bool search_generic_implicit_fb(struct usb_device *dev, int ifnum,
293 unsigned int altsetting,
294 struct usb_host_interface **altsp,
295 unsigned int *ep)
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100296{
297 struct usb_interface *iface;
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100298 struct usb_host_interface *alts;
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100299 struct usb_interface_descriptor *altsd;
300 struct usb_endpoint_descriptor *epd;
301
302 iface = usb_ifnum_to_if(dev, ifnum);
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100303 if (!iface)
304 return false;
305 alts = usb_altnum_to_altsetting(iface, altsetting);
306 if (!alts)
307 return false;
308 altsd = get_iface_desc(alts);
309 if (altsd->bInterfaceClass != USB_CLASS_AUDIO ||
310 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING ||
311 altsd->bInterfaceProtocol != UAC_VERSION_2 ||
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100312 altsd->bNumEndpoints < 1)
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100313 return false;
314 epd = get_endpoint(alts, 0);
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100315 if (!usb_endpoint_is_isoc_in(epd) ||
316 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
317 USB_ENDPOINT_USAGE_IMPLICIT_FB)
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100318 return false;
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100319 *ep = epd->bEndpointAddress;
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100320 *altsp = alts;
321 return true;
322}
323
324/* Like the function above, but specific to Roland with vendor class and hack */
325static bool search_roland_implicit_fb(struct usb_device *dev, int ifnum,
326 unsigned int altsetting,
327 struct usb_host_interface **altsp,
328 unsigned int *ep)
329{
330 struct usb_interface *iface;
331 struct usb_host_interface *alts;
332 struct usb_interface_descriptor *altsd;
333 struct usb_endpoint_descriptor *epd;
334
335 iface = usb_ifnum_to_if(dev, ifnum);
336 if (!iface)
337 return false;
338 alts = usb_altnum_to_altsetting(iface, altsetting);
339 if (!alts)
340 return false;
341 altsd = get_iface_desc(alts);
342 if (altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
343 (altsd->bInterfaceSubClass != 2 &&
344 altsd->bInterfaceProtocol != 2) ||
345 altsd->bNumEndpoints < 1)
346 return false;
347 epd = get_endpoint(alts, 0);
348 if (!usb_endpoint_is_isoc_in(epd) ||
349 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
350 USB_ENDPOINT_USAGE_IMPLICIT_FB)
351 return false;
352 *ep = epd->bEndpointAddress;
353 *altsp = alts;
354 return true;
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100355}
356
Manuel Reinhardt2bc16b92019-01-31 15:32:35 +0100357/* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
358 * applies. Returns 1 if a quirk was found.
359 */
Takashi Iwaif6581c02020-11-23 09:53:14 +0100360static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
361 struct audioformat *fmt,
362 struct usb_interface *iface,
363 struct usb_host_interface *alts)
Daniel Macke5779992010-03-04 19:46:13 +0100364{
Takashi Iwaif6581c02020-11-23 09:53:14 +0100365 struct usb_device *dev = chip->dev;
366 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
367 unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Eldad Zacka60945f2013-08-03 10:50:18 +0200368 unsigned int ep;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500369 unsigned int ifnum;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200370
Takashi Iwaif6581c02020-11-23 09:53:14 +0100371 switch (chip->usb_id) {
Eldad Zackca10a7e2012-11-28 23:55:41 +0100372 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500373 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Geoffrey D. Bennett0938eca2020-11-04 22:27:17 +1030374 case USB_ID(0x22f0, 0x0006): /* Allen&Heath Qu-16 */
Eldad Zack914273c2013-08-03 10:50:21 +0200375 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500376 ifnum = 3;
377 goto add_sync_ep_from_ifnum;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200378 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
379 case USB_ID(0x0763, 0x2081):
Eldad Zack914273c2013-08-03 10:50:21 +0200380 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500381 ifnum = 2;
382 goto add_sync_ep_from_ifnum;
383 case USB_ID(0x2466, 0x8003): /* Fractal Audio Axe-Fx II */
Geoffrey D. Bennett26201dd2020-11-04 22:37:05 +1030384 case USB_ID(0x0499, 0x172a): /* Yamaha MODX */
Alberto Aguirre17f08b02016-12-08 00:36:48 -0600385 ep = 0x86;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500386 ifnum = 2;
387 goto add_sync_ep_from_ifnum;
Alberto Aguirre91a85612018-04-18 09:35:35 -0500388 case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx III */
389 ep = 0x81;
390 ifnum = 2;
391 goto add_sync_ep_from_ifnum;
Keith Winsteinf15cfca2020-10-25 22:05:47 -0700392 case USB_ID(0x1686, 0xf029): /* Zoom UAC-2 */
393 ep = 0x82;
394 ifnum = 2;
395 goto add_sync_ep_from_ifnum;
Takashi Iwai1a157182019-08-20 08:58:12 +0200396 case USB_ID(0x1397, 0x0001): /* Behringer UFX1604 */
Alberto Aguirre103e9622018-04-18 09:35:34 -0500397 case USB_ID(0x1397, 0x0002): /* Behringer UFX1204 */
Lassi Ylikojola5e35dc02018-02-09 16:51:36 +0200398 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500399 ifnum = 1;
400 goto add_sync_ep_from_ifnum;
Alexander Tsoy2edb84e2020-02-29 18:18:15 +0300401 case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook II/IIc */
402 /* MicroBook IIc */
403 if (altsd->bInterfaceClass == USB_CLASS_AUDIO)
404 return 0;
405
406 /* MicroBook II */
Manuel Reinhardta6340902019-02-28 20:34:04 +0100407 ep = 0x84;
408 ifnum = 0;
409 goto add_sync_ep_from_ifnum;
Alexander Tsoyc2491772020-01-15 18:13:58 +0300410 case USB_ID(0x07fd, 0x0008): /* MOTU M Series */
Laurence Tratt3da87ec2020-06-21 08:50:05 +0100411 case USB_ID(0x31e9, 0x0001): /* Solid State Logic SSL2 */
Laurence Tratte7585db2020-06-12 12:18:07 +0100412 case USB_ID(0x31e9, 0x0002): /* Solid State Logic SSL2+ */
Joshua Sivec7c5b8922020-08-25 18:55:18 +0200413 case USB_ID(0x0499, 0x172f): /* Steinberg UR22C */
Pavel Hofmanb6a1e782020-07-03 12:04:33 +0200414 case USB_ID(0x0d9a, 0x00df): /* RTX6001 */
Alexander Tsoyc2491772020-01-15 18:13:58 +0300415 ep = 0x81;
416 ifnum = 2;
417 goto add_sync_ep_from_ifnum;
Dmitry Panchenko7fccfec2020-06-01 13:22:24 +0300418 case USB_ID(0x2b73, 0x000a): /* Pioneer DJ DJM-900NXS2 */
František Kučera14335d82020-08-25 17:31:13 +0200419 case USB_ID(0x2b73, 0x0017): /* Pioneer DJ DJM-250MK2 */
Dmitry Panchenko7fccfec2020-06-01 13:22:24 +0300420 ep = 0x82;
421 ifnum = 0;
422 goto add_sync_ep_from_ifnum;
Szabolcs Szőke7571b6a2019-10-11 19:19:36 +0200423 case USB_ID(0x0582, 0x01d8): /* BOSS Katana */
424 /* BOSS Katana amplifiers do not need quirks */
425 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200426 }
Alberto Aguirre103e9622018-04-18 09:35:34 -0500427
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100428 /* Generic UAC2 implicit feedback */
429 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
430 altsd->bInterfaceClass == USB_CLASS_AUDIO &&
431 altsd->bInterfaceProtocol == UAC_VERSION_2 &&
432 altsd->bNumEndpoints == 1) {
433 ifnum = altsd->bInterfaceNumber + 1;
434 if (search_generic_implicit_fb(dev, ifnum,
435 altsd->bAlternateSetting,
436 &alts, &ep))
437 goto add_sync_ep;
438 }
439
440 /* Roland/BOSS implicit feedback with vendor spec class */
Eldad Zack914273c2013-08-03 10:50:21 +0200441 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100442 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
443 altsd->bInterfaceProtocol == 2 &&
444 altsd->bNumEndpoints == 1 &&
Takashi Iwaif6581c02020-11-23 09:53:14 +0100445 USB_ID_VENDOR(chip->usb_id) == 0x0582 /* Roland */) {
446 ifnum = altsd->bInterfaceNumber + 1;
447 if (search_roland_implicit_fb(dev, ifnum,
448 altsd->bAlternateSetting,
449 &alts, &ep))
450 goto add_sync_ep;
451 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200452
Eldad Zacka60945f2013-08-03 10:50:18 +0200453 /* No quirk */
454 return 0;
455
Alberto Aguirre103e9622018-04-18 09:35:34 -0500456add_sync_ep_from_ifnum:
457 iface = usb_ifnum_to_if(dev, ifnum);
458
Johan Hovold5d1b7122020-01-14 09:39:53 +0100459 if (!iface || iface->num_altsetting < 2)
Takashi Iwaif6581c02020-11-23 09:53:14 +0100460 return 0;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500461
462 alts = &iface->altsetting[1];
463
Eldad Zacka60945f2013-08-03 10:50:18 +0200464add_sync_ep:
Takashi Iwaif6581c02020-11-23 09:53:14 +0100465 fmt->sync_ep = ep;
466 fmt->sync_iface = ifnum;
467 fmt->sync_altsetting = alts->desc.bAlternateSetting;
468 fmt->implicit_fb = 1;
469 dev_dbg(&dev->dev, "%d:%d: found implicit_fb sync_ep=%x, iface=%d, alt=%d\n",
470 fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface,
471 fmt->sync_altsetting);
Eldad Zacka60945f2013-08-03 10:50:18 +0200472
Manuel Reinhardt2bc16b92019-01-31 15:32:35 +0100473 return 1;
Eldad Zacka60945f2013-08-03 10:50:18 +0200474}
475
Takashi Iwaif6581c02020-11-23 09:53:14 +0100476int snd_usb_audioformat_set_sync_ep(struct snd_usb_audio *chip,
477 struct audioformat *fmt)
Eldad Zacka60945f2013-08-03 10:50:18 +0200478{
Takashi Iwaif6581c02020-11-23 09:53:14 +0100479 struct usb_device *dev = chip->dev;
480 struct usb_interface *iface;
481 struct usb_host_interface *alts;
482 struct usb_interface_descriptor *altsd;
483 unsigned int ep, attr, sync_attr;
484 bool is_playback;
Eldad Zacka60945f2013-08-03 10:50:18 +0200485 int err;
486
Takashi Iwaif6581c02020-11-23 09:53:14 +0100487 iface = usb_ifnum_to_if(dev, fmt->iface);
488 if (!iface)
Manuel Reinhardt2bc16b92019-01-31 15:32:35 +0100489 return 0;
Takashi Iwaif6581c02020-11-23 09:53:14 +0100490 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
491 if (!alts)
492 return 0;
493 altsd = get_iface_desc(alts);
494
495 is_playback = !(get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
496 if (is_playback) {
497 err = audioformat_implicit_fb_quirk(chip, fmt, iface, alts);
498 if (err > 0)
499 return 0;
500 }
Manuel Reinhardt2bc16b92019-01-31 15:32:35 +0100501
Eldad Zackf34d0652013-08-03 10:50:19 +0200502 if (altsd->bNumEndpoints < 2)
503 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200504
Takashi Iwaif6581c02020-11-23 09:53:14 +0100505 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500506 if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
507 attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
Eldad Zackf34d0652013-08-03 10:50:19 +0200508 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
509 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200510
Takashi Iwaif6581c02020-11-23 09:53:14 +0100511 sync_attr = get_endpoint(alts, 1)->bmAttributes;
512
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500513 /*
514 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
515 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
516 * error fall back to SYNC mode and don't create sync endpoint
517 */
518
Eldad Zackf34d0652013-08-03 10:50:19 +0200519 /* check sync-pipe endpoint */
520 /* ... and check descriptor size before accessing bSynchAddress
521 because there is a version of the SB Audigy 2 NX firmware lacking
522 the audio fields in the endpoint descriptors */
Takashi Iwaif6581c02020-11-23 09:53:14 +0100523 if ((sync_attr & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
Eldad Zackf34d0652013-08-03 10:50:19 +0200524 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zack95fec882013-08-03 10:50:20 +0200525 get_endpoint(alts, 1)->bSynchAddress != 0)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100526 dev_err(&dev->dev,
527 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
528 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200529 get_endpoint(alts, 1)->bmAttributes,
530 get_endpoint(alts, 1)->bLength,
531 get_endpoint(alts, 1)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500532 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
533 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200534 return -EINVAL;
Daniel Mackedcd3632012-04-12 13:51:12 +0200535 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200536 ep = get_endpoint(alts, 1)->bEndpointAddress;
Eldad Zack95fec882013-08-03 10:50:20 +0200537 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Ard van Breemen1b341212019-08-02 13:52:14 +0200538 get_endpoint(alts, 0)->bSynchAddress != 0 &&
Eldad Zackf34d0652013-08-03 10:50:19 +0200539 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
540 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100541 dev_err(&dev->dev,
542 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
543 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200544 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500545 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
546 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200547 return -EINVAL;
548 }
549
Takashi Iwaif6581c02020-11-23 09:53:14 +0100550 fmt->sync_ep = ep;
551 fmt->sync_iface = altsd->bInterfaceNumber;
552 fmt->sync_altsetting = altsd->bAlternateSetting;
553 if ((sync_attr & USB_ENDPOINT_USAGE_MASK) == USB_ENDPOINT_USAGE_IMPLICIT_FB)
554 fmt->implicit_fb = 1;
555
556 dev_dbg(&dev->dev, "%d:%d: found sync_ep=0x%x, iface=%d, alt=%d, implicit_fb=%d\n",
557 fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface,
558 fmt->sync_altsetting, fmt->implicit_fb);
559
560 return 0;
561}
562
563static int set_sync_endpoint(struct snd_usb_substream *subs,
564 struct audioformat *fmt)
565{
566 struct usb_device *dev = subs->dev;
567 struct usb_interface *iface;
568 struct usb_host_interface *alts;
569 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
570 unsigned int ep;
571 int err;
572
573 subs->sync_endpoint = NULL;
574 subs->data_endpoint->sync_master = NULL;
575
576 ep = fmt->sync_ep;
577 if (!ep)
578 return 0;
579
580 iface = usb_ifnum_to_if(dev, fmt->sync_iface);
581 if (!iface)
582 return 0;
583
584 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
585 if (!alts)
586 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200587
588 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
589 alts, ep, !subs->direction,
Takashi Iwaif6581c02020-11-23 09:53:14 +0100590 fmt->implicit_fb ?
591 SND_USB_ENDPOINT_TYPE_DATA :
592 SND_USB_ENDPOINT_TYPE_SYNC);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500593 if (!subs->sync_endpoint) {
Takashi Iwaif6581c02020-11-23 09:53:14 +0100594 if (is_playback &&
595 (fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) == USB_ENDPOINT_SYNC_NONE)
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500596 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200597 return -EINVAL;
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500598 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200599
Takashi Iwaif6581c02020-11-23 09:53:14 +0100600 subs->sync_endpoint->is_implicit_feedback = fmt->implicit_fb;
Erwin Burema10ce77e2020-05-10 20:29:11 +0200601
Eldad Zackf34d0652013-08-03 10:50:19 +0200602 subs->data_endpoint->sync_master = subs->sync_endpoint;
Daniel Macke5779992010-03-04 19:46:13 +0100603
Takashi Iwai4974b792020-11-23 09:53:08 +0100604 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
605 subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
606 err = usb_set_interface(subs->dev,
607 subs->sync_endpoint->iface,
608 subs->sync_endpoint->altsetting);
609 if (err < 0)
610 return err;
611 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
612 subs->sync_endpoint->iface,
613 subs->sync_endpoint->altsetting);
614 snd_usb_set_interface_quirk(dev);
615 }
616
Eldad Zack71bb64c2013-08-03 10:50:17 +0200617 return 0;
618}
619
620/*
621 * find a matching format and set up the interface
622 */
623static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
624{
625 struct usb_device *dev = subs->dev;
626 struct usb_host_interface *alts;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200627 struct usb_interface *iface;
628 int err;
629
630 iface = usb_ifnum_to_if(dev, fmt->iface);
631 if (WARN_ON(!iface))
632 return -EINVAL;
Takashi Iwaib099b962018-05-02 09:36:28 +0200633 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
Johan Hovold01412542019-12-20 10:31:34 +0100634 if (WARN_ON(!alts))
Eldad Zack71bb64c2013-08-03 10:50:17 +0200635 return -EINVAL;
636
Hui Wang92adc962019-12-18 21:26:50 +0800637 if (fmt == subs->cur_audiofmt && !subs->need_setup_fmt)
Eldad Zack71bb64c2013-08-03 10:50:17 +0200638 return 0;
639
640 /* close the old interface */
Hui Wang92adc962019-12-18 21:26:50 +0800641 if (subs->interface >= 0 && (subs->interface != fmt->iface || subs->need_setup_fmt)) {
Takashi Iwai8a463222018-05-02 10:04:27 +0200642 if (!subs->stream->chip->keep_iface) {
643 err = usb_set_interface(subs->dev, subs->interface, 0);
644 if (err < 0) {
645 dev_err(&dev->dev,
646 "%d:%d: return to setting 0 failed (%d)\n",
647 fmt->iface, fmt->altsetting, err);
648 return -EIO;
649 }
Eldad Zack71bb64c2013-08-03 10:50:17 +0200650 }
651 subs->interface = -1;
652 subs->altset_idx = 0;
653 }
654
Hui Wang92adc962019-12-18 21:26:50 +0800655 if (subs->need_setup_fmt)
656 subs->need_setup_fmt = false;
657
Eldad Zack71bb64c2013-08-03 10:50:17 +0200658 /* set interface */
Takashi Iwaib099b962018-05-02 09:36:28 +0200659 if (iface->cur_altsetting != alts) {
Jurgen Kramer6874daa2014-11-28 17:32:54 +0100660 err = snd_usb_select_mode_quirk(subs, fmt);
661 if (err < 0)
662 return -EIO;
663
Eldad Zack71bb64c2013-08-03 10:50:17 +0200664 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
665 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100666 dev_err(&dev->dev,
667 "%d:%d: usb_set_interface failed (%d)\n",
668 fmt->iface, fmt->altsetting, err);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200669 return -EIO;
670 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100671 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
672 fmt->iface, fmt->altsetting);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200673 snd_usb_set_interface_quirk(dev);
674 }
675
Takashi Iwaib099b962018-05-02 09:36:28 +0200676 subs->interface = fmt->iface;
677 subs->altset_idx = fmt->altset_idx;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200678 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
679 alts, fmt->endpoint, subs->direction,
680 SND_USB_ENDPOINT_TYPE_DATA);
681
682 if (!subs->data_endpoint)
683 return -EINVAL;
684
Takashi Iwaif6581c02020-11-23 09:53:14 +0100685 err = set_sync_endpoint(subs, fmt);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200686 if (err < 0)
687 return err;
688
Eldad Zackd133f2c2013-08-03 10:50:16 +0200689 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
690 if (err < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100691 return err;
692
693 subs->cur_audiofmt = fmt;
694
695 snd_usb_set_format_quirk(subs, fmt);
696
Daniel Macke5779992010-03-04 19:46:13 +0100697 return 0;
698}
699
700/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100701 * Return the score of matching two audioformats.
702 * Veto the audioformat if:
703 * - It has no channels for some reason.
704 * - Requested PCM format is not supported.
705 * - Requested sample rate is not supported.
706 */
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100707static int match_endpoint_audioformats(struct snd_usb_substream *subs,
708 struct audioformat *fp,
709 struct audioformat *match, int rate,
710 snd_pcm_format_t pcm_format)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100711{
712 int i;
713 int score = 0;
714
715 if (fp->channels < 1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100716 dev_dbg(&subs->dev->dev,
717 "%s: (fmt @%p) no channels\n", __func__, fp);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100718 return 0;
719 }
720
Eldad Zack74c34ca2013-04-23 01:00:41 +0200721 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100722 dev_dbg(&subs->dev->dev,
723 "%s: (fmt @%p) no match for format %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100724 fp, pcm_format);
725 return 0;
726 }
727
728 for (i = 0; i < fp->nr_rates; i++) {
729 if (fp->rate_table[i] == rate) {
730 score++;
731 break;
732 }
733 }
734 if (!score) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100735 dev_dbg(&subs->dev->dev,
736 "%s: (fmt @%p) no match for rate %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100737 fp, rate);
738 return 0;
739 }
740
741 if (fp->channels == match->channels)
742 score++;
743
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100744 dev_dbg(&subs->dev->dev,
745 "%s: (fmt @%p) score %d\n", __func__, fp, score);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100746
747 return score;
748}
749
750/*
751 * Configure the sync ep using the rate and pcm format of the data ep.
752 */
753static int configure_sync_endpoint(struct snd_usb_substream *subs)
754{
Eldad Zack0d9741c2012-12-03 20:30:09 +0100755 struct audioformat *fp;
756 struct audioformat *sync_fp = NULL;
757 int cur_score = 0;
758 int sync_period_bytes = subs->period_bytes;
759 struct snd_usb_substream *sync_subs =
760 &subs->stream->substream[subs->direction ^ 1];
761
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100762 if (subs->fixed_hw ||
763 !subs->sync_endpoint->is_implicit_feedback) {
764 sync_fp = subs->cur_audiofmt;
765 goto configure;
766 }
767
768 sync_fp = find_format(&sync_subs->fmt_list, subs->pcm_format,
769 subs->cur_rate, subs->channels, NULL);
770 if (sync_fp)
771 goto configure;
Takashi Iwai31be5422013-01-10 14:06:38 +0100772
Eldad Zack0d9741c2012-12-03 20:30:09 +0100773 /* Try to find the best matching audioformat. */
774 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100775 int score = match_endpoint_audioformats(subs,
776 fp, subs->cur_audiofmt,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100777 subs->cur_rate, subs->pcm_format);
778
779 if (score > cur_score) {
780 sync_fp = fp;
781 cur_score = score;
782 }
783 }
784
785 if (unlikely(sync_fp == NULL)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100786 dev_err(&subs->dev->dev,
787 "%s: no valid audioformat for sync ep %x found\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100788 __func__, sync_subs->ep_num);
789 return -EINVAL;
790 }
791
792 /*
793 * Recalculate the period bytes if channel number differ between
794 * data and sync ep audioformat.
795 */
796 if (sync_fp->channels != subs->channels) {
797 sync_period_bytes = (subs->period_bytes / subs->channels) *
798 sync_fp->channels;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100799 dev_dbg(&subs->dev->dev,
800 "%s: adjusted sync ep period bytes (%d -> %d)\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100801 __func__, subs->period_bytes, sync_period_bytes);
802 }
803
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100804 configure:
805 return snd_usb_endpoint_set_params(subs->sync_endpoint,
806 subs->pcm_format,
807 sync_fp->channels,
808 sync_period_bytes,
809 subs->period_frames,
810 subs->buffer_periods,
811 subs->cur_rate,
812 sync_fp,
813 NULL);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100814}
815
816/*
Dylan Reid61a70952012-09-18 09:49:48 -0700817 * configure endpoint params
818 *
819 * called during initial setup and upon resume
820 */
821static int configure_endpoint(struct snd_usb_substream *subs)
822{
823 int ret;
824
Dylan Reid61a70952012-09-18 09:49:48 -0700825 /* format changed */
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100826 stop_endpoints(subs);
827 sync_pending_stops(subs);
Dylan Reid61a70952012-09-18 09:49:48 -0700828 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
829 subs->pcm_format,
830 subs->channels,
831 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400832 subs->period_frames,
833 subs->buffer_periods,
Dylan Reid61a70952012-09-18 09:49:48 -0700834 subs->cur_rate,
835 subs->cur_audiofmt,
836 subs->sync_endpoint);
837 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200838 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700839
840 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100841 ret = configure_sync_endpoint(subs);
842
Dylan Reid61a70952012-09-18 09:49:48 -0700843 return ret;
844}
845
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100846static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state)
847{
848 int ret;
849
850 if (!subs->str_pd)
851 return 0;
852
853 ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
854 if (ret < 0) {
855 dev_err(&subs->dev->dev,
856 "Cannot change Power Domain ID: %d to state: %d. Err: %d\n",
857 subs->str_pd->pd_id, state, ret);
858 return ret;
859 }
860
861 return 0;
862}
863
864int snd_usb_pcm_suspend(struct snd_usb_stream *as)
865{
866 int ret;
867
868 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2);
869 if (ret < 0)
870 return ret;
871
872 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2);
873 if (ret < 0)
874 return ret;
875
876 return 0;
877}
878
879int snd_usb_pcm_resume(struct snd_usb_stream *as)
880{
881 int ret;
882
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100883 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1);
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100884 if (ret < 0)
885 return ret;
886
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100887 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1);
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100888 if (ret < 0)
889 return ret;
890
891 return 0;
892}
893
Dylan Reid61a70952012-09-18 09:49:48 -0700894/*
Daniel Macke5779992010-03-04 19:46:13 +0100895 * hw_params callback
896 *
897 * allocate a buffer and set the given audio format.
898 *
899 * so far we use a physically linear buffer although packetize transfer
900 * doesn't need a continuous area.
901 * if sg buffer is supported on the later version of alsa, we'll follow
902 * that.
903 */
904static int snd_usb_hw_params(struct snd_pcm_substream *substream,
905 struct snd_pcm_hw_params *hw_params)
906{
907 struct snd_usb_substream *subs = substream->runtime->private_data;
908 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700909 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100910
Shuah Khan66354f12019-04-01 20:40:22 -0400911 ret = snd_media_start_pipeline(subs);
912 if (ret)
913 return ret;
914
Dylan Reid61a70952012-09-18 09:49:48 -0700915 subs->pcm_format = params_format(hw_params);
916 subs->period_bytes = params_period_bytes(hw_params);
Alan Stern976b6c02013-09-24 15:51:58 -0400917 subs->period_frames = params_period_size(hw_params);
918 subs->buffer_periods = params_periods(hw_params);
Dylan Reid61a70952012-09-18 09:49:48 -0700919 subs->channels = params_channels(hw_params);
920 subs->cur_rate = params_rate(hw_params);
921
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100922 fmt = find_substream_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100923 if (!fmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100924 dev_dbg(&subs->dev->dev,
925 "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700926 subs->pcm_format, subs->cur_rate, subs->channels);
Shuah Khan66354f12019-04-01 20:40:22 -0400927 ret = -EINVAL;
928 goto stop_pipeline;
Daniel Macke5779992010-03-04 19:46:13 +0100929 }
930
Takashi Iwai47ab1542015-08-25 16:09:00 +0200931 ret = snd_usb_lock_shutdown(subs->stream->chip);
932 if (ret < 0)
Shuah Khan66354f12019-04-01 20:40:22 -0400933 goto stop_pipeline;
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100934
935 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
Takashi Iwai978520b2012-10-12 15:12:55 +0200936 if (ret < 0)
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100937 goto unlock;
938
939 ret = set_format(subs, fmt);
940 if (ret < 0)
941 goto unlock;
Daniel Macke5779992010-03-04 19:46:13 +0100942
Dylan Reid61a70952012-09-18 09:49:48 -0700943 subs->interface = fmt->iface;
944 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200945 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100946
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100947 unlock:
948 snd_usb_unlock_shutdown(subs->stream->chip);
Shuah Khan66354f12019-04-01 20:40:22 -0400949 if (ret < 0)
950 goto stop_pipeline;
951 return ret;
952
953 stop_pipeline:
954 snd_media_stop_pipeline(subs);
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100955 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100956}
957
958/*
959 * hw_free callback
960 *
961 * reset the audio format and release the buffer
962 */
963static int snd_usb_hw_free(struct snd_pcm_substream *substream)
964{
965 struct snd_usb_substream *subs = substream->runtime->private_data;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100966 struct snd_usb_audio *chip = subs->stream->chip;
Daniel Macke5779992010-03-04 19:46:13 +0100967
Shuah Khan66354f12019-04-01 20:40:22 -0400968 snd_media_stop_pipeline(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100969 subs->cur_audiofmt = NULL;
970 subs->cur_rate = 0;
971 subs->period_bytes = 0;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100972 if (!snd_usb_lock_shutdown(chip)) {
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100973 stop_endpoints(subs);
974 sync_pending_stops(subs);
Eldad Zack26de5d02013-10-06 22:31:07 +0200975 snd_usb_endpoint_deactivate(subs->sync_endpoint);
976 snd_usb_endpoint_deactivate(subs->data_endpoint);
Takashi Iwai18035032020-11-23 09:53:12 +0100977 if (subs->data_endpoint) {
978 subs->data_endpoint->sync_master = NULL;
979 subs->data_endpoint = NULL;
980 }
981 subs->sync_endpoint = NULL;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100982 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200983 }
Takashi Iwaif274baa2018-05-27 13:01:17 +0200984
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +0100985 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100986}
987
988/*
989 * prepare callback
990 *
991 * only a few subtle things...
992 */
993static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
994{
995 struct snd_pcm_runtime *runtime = substream->runtime;
996 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700997 struct usb_host_interface *alts;
998 struct usb_interface *iface;
999 int ret;
Daniel Macke5779992010-03-04 19:46:13 +01001000
1001 if (! subs->cur_audiofmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001002 dev_err(&subs->dev->dev, "no format is specified!\n");
Daniel Macke5779992010-03-04 19:46:13 +01001003 return -ENXIO;
1004 }
1005
Takashi Iwai47ab1542015-08-25 16:09:00 +02001006 ret = snd_usb_lock_shutdown(subs->stream->chip);
1007 if (ret < 0)
1008 return ret;
Takashi Iwai978520b2012-10-12 15:12:55 +02001009 if (snd_BUG_ON(!subs->data_endpoint)) {
1010 ret = -EIO;
1011 goto unlock;
1012 }
Daniel Mackedcd3632012-04-12 13:51:12 +02001013
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001014 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
1015 if (ret < 0)
1016 goto unlock;
1017
Dylan Reid61a70952012-09-18 09:49:48 -07001018 ret = set_format(subs, subs->cur_audiofmt);
1019 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +02001020 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -07001021
Takashi Iwai384dc0852012-09-18 14:49:31 +02001022 if (subs->need_setup_ep) {
Daniel Girnus1e2e3fe2016-12-06 14:46:15 +09001023
1024 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
1025 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
1026 ret = snd_usb_init_sample_rate(subs->stream->chip,
1027 subs->cur_audiofmt->iface,
1028 alts,
1029 subs->cur_audiofmt,
1030 subs->cur_rate);
1031 if (ret < 0)
1032 goto unlock;
1033
Takashi Iwai384dc0852012-09-18 14:49:31 +02001034 ret = configure_endpoint(subs);
1035 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +02001036 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +02001037 subs->need_setup_ep = false;
1038 }
Dylan Reid61a70952012-09-18 09:49:48 -07001039
Daniel Macke5779992010-03-04 19:46:13 +01001040 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +02001041 subs->data_endpoint->maxframesize =
1042 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
1043 subs->data_endpoint->curframesize =
1044 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +01001045
1046 /* reset the pointer */
1047 subs->hwptr_done = 0;
1048 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -05001049 subs->last_delay = 0;
1050 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001051 runtime->delay = 0;
1052
Daniel Mackedcd3632012-04-12 13:51:12 +02001053 /* for playback, submit the URBs now; otherwise, the first hwptr_done
1054 * updates for all URBs would happen at the same time when starting */
1055 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +02001056 ret = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +02001057
Takashi Iwai978520b2012-10-12 15:12:55 +02001058 unlock:
Takashi Iwai47ab1542015-08-25 16:09:00 +02001059 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +02001060 return ret;
Daniel Macke5779992010-03-04 19:46:13 +01001061}
1062
Takashi Iwai7ec827b2020-11-23 09:53:18 +01001063/*
1064 * h/w constraints
1065 */
1066
1067#ifdef HW_CONST_DEBUG
1068#define hwc_debug(fmt, args...) pr_debug(fmt, ##args)
1069#else
1070#define hwc_debug(fmt, args...) do { } while(0)
1071#endif
1072
Bhumika Goyalaaffbf72017-08-17 14:45:59 +05301073static const struct snd_pcm_hardware snd_usb_hardware =
Daniel Macke5779992010-03-04 19:46:13 +01001074{
1075 .info = SNDRV_PCM_INFO_MMAP |
1076 SNDRV_PCM_INFO_MMAP_VALID |
1077 SNDRV_PCM_INFO_BATCH |
1078 SNDRV_PCM_INFO_INTERLEAVED |
1079 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1080 SNDRV_PCM_INFO_PAUSE,
1081 .buffer_bytes_max = 1024 * 1024,
1082 .period_bytes_min = 64,
1083 .period_bytes_max = 512 * 1024,
1084 .periods_min = 2,
1085 .periods_max = 1024,
1086};
1087
1088static int hw_check_valid_format(struct snd_usb_substream *subs,
1089 struct snd_pcm_hw_params *params,
1090 struct audioformat *fp)
1091{
1092 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1093 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1094 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1095 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001096 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +01001097 unsigned int ptime;
1098
1099 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001100 snd_mask_none(&check_fmts);
1101 check_fmts.bits[0] = (u32)fp->formats;
1102 check_fmts.bits[1] = (u32)(fp->formats >> 32);
1103 snd_mask_intersect(&check_fmts, fmts);
1104 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +01001105 hwc_debug(" > check: no supported format %d\n", fp->format);
1106 return 0;
1107 }
1108 /* check the channels */
1109 if (fp->channels < ct->min || fp->channels > ct->max) {
1110 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1111 return 0;
1112 }
1113 /* check the rate is within the range */
1114 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1115 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1116 return 0;
1117 }
1118 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1119 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1120 return 0;
1121 }
1122 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +02001123 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +01001124 ptime = 125 * (1 << fp->datainterval);
1125 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
1126 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
1127 return 0;
1128 }
1129 }
1130 return 1;
1131}
1132
Takashi Iwai7726dce2020-11-23 09:53:17 +01001133static int apply_hw_params_minmax(struct snd_interval *it, unsigned int rmin,
1134 unsigned int rmax)
Daniel Macke5779992010-03-04 19:46:13 +01001135{
Daniel Macke5779992010-03-04 19:46:13 +01001136 int changed;
Daniel Macke5779992010-03-04 19:46:13 +01001137
Takashi Iwaibc4e94a2020-11-23 09:53:07 +01001138 if (rmin > rmax) {
Daniel Macke5779992010-03-04 19:46:13 +01001139 hwc_debug(" --> get empty\n");
1140 it->empty = 1;
1141 return -EINVAL;
1142 }
1143
1144 changed = 0;
1145 if (it->min < rmin) {
1146 it->min = rmin;
1147 it->openmin = 0;
1148 changed = 1;
1149 }
1150 if (it->max > rmax) {
1151 it->max = rmax;
1152 it->openmax = 0;
1153 changed = 1;
1154 }
1155 if (snd_interval_checkempty(it)) {
1156 it->empty = 1;
1157 return -EINVAL;
1158 }
1159 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1160 return changed;
1161}
1162
Takashi Iwai7726dce2020-11-23 09:53:17 +01001163static int hw_rule_rate(struct snd_pcm_hw_params *params,
1164 struct snd_pcm_hw_rule *rule)
1165{
1166 struct snd_usb_substream *subs = rule->private;
1167 struct audioformat *fp;
1168 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1169 unsigned int rmin, rmax, r;
1170 int i;
1171
1172 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1173 rmin = UINT_MAX;
1174 rmax = 0;
1175 list_for_each_entry(fp, &subs->fmt_list, list) {
1176 if (!hw_check_valid_format(subs, params, fp))
1177 continue;
1178 if (fp->rate_table && fp->nr_rates) {
1179 for (i = 0; i < fp->nr_rates; i++) {
1180 r = fp->rate_table[i];
1181 if (!snd_interval_test(it, r))
1182 continue;
1183 rmin = min(rmin, r);
1184 rmax = max(rmax, r);
1185 }
1186 } else {
1187 rmin = min(rmin, fp->rate_min);
1188 rmax = max(rmax, fp->rate_max);
1189 }
1190 }
1191
1192 return apply_hw_params_minmax(it, rmin, rmax);
1193}
1194
Daniel Macke5779992010-03-04 19:46:13 +01001195
1196static int hw_rule_channels(struct snd_pcm_hw_params *params,
1197 struct snd_pcm_hw_rule *rule)
1198{
1199 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001200 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001201 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1202 unsigned int rmin, rmax;
Daniel Macke5779992010-03-04 19:46:13 +01001203
1204 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
Takashi Iwai7726dce2020-11-23 09:53:17 +01001205 rmin = UINT_MAX;
1206 rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001207 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001208 if (!hw_check_valid_format(subs, params, fp))
1209 continue;
Takashi Iwai7726dce2020-11-23 09:53:17 +01001210 rmin = min(rmin, fp->channels);
1211 rmax = max(rmax, fp->channels);
Daniel Macke5779992010-03-04 19:46:13 +01001212 }
1213
Takashi Iwai7726dce2020-11-23 09:53:17 +01001214 return apply_hw_params_minmax(it, rmin, rmax);
Daniel Macke5779992010-03-04 19:46:13 +01001215}
1216
1217static int hw_rule_format(struct snd_pcm_hw_params *params,
1218 struct snd_pcm_hw_rule *rule)
1219{
1220 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001221 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001222 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1223 u64 fbits;
1224 u32 oldbits[2];
1225 int changed;
1226
1227 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1228 fbits = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001229 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001230 if (!hw_check_valid_format(subs, params, fp))
1231 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001232 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +01001233 }
1234
1235 oldbits[0] = fmt->bits[0];
1236 oldbits[1] = fmt->bits[1];
1237 fmt->bits[0] &= (u32)fbits;
1238 fmt->bits[1] &= (u32)(fbits >> 32);
1239 if (!fmt->bits[0] && !fmt->bits[1]) {
1240 hwc_debug(" --> get empty\n");
1241 return -EINVAL;
1242 }
1243 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1244 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1245 return changed;
1246}
1247
1248static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1249 struct snd_pcm_hw_rule *rule)
1250{
1251 struct snd_usb_substream *subs = rule->private;
1252 struct audioformat *fp;
1253 struct snd_interval *it;
1254 unsigned char min_datainterval;
1255 unsigned int pmin;
Daniel Macke5779992010-03-04 19:46:13 +01001256
1257 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1258 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1259 min_datainterval = 0xff;
1260 list_for_each_entry(fp, &subs->fmt_list, list) {
1261 if (!hw_check_valid_format(subs, params, fp))
1262 continue;
1263 min_datainterval = min(min_datainterval, fp->datainterval);
1264 }
1265 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +02001266 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +01001267 it->empty = 1;
1268 return -EINVAL;
1269 }
1270 pmin = 125 * (1 << min_datainterval);
Takashi Iwai7726dce2020-11-23 09:53:17 +01001271
1272 return apply_hw_params_minmax(it, pmin, UINT_MAX);
Daniel Macke5779992010-03-04 19:46:13 +01001273}
1274
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001275/* apply PCM hw constraints from the concurrent sync EP */
1276static int apply_hw_constraint_from_sync(struct snd_pcm_runtime *runtime,
1277 struct snd_usb_substream *subs)
1278{
1279 struct snd_usb_audio *chip = subs->stream->chip;
1280 struct snd_usb_endpoint *ep;
1281 struct audioformat *fp;
1282 int err;
1283
1284 subs->fixed_hw = 0;
1285 list_for_each_entry(fp, &subs->fmt_list, list) {
1286 ep = snd_usb_get_endpoint(chip, fp->endpoint, fp->iface,
1287 fp->altsetting);
1288 if (ep && ep->cur_rate)
1289 goto found;
1290 if (!fp->implicit_fb)
1291 continue;
1292 /* for the implicit fb, check the sync ep as well */
1293 ep = snd_usb_get_endpoint(chip, fp->sync_ep, fp->sync_iface,
1294 fp->sync_altsetting);
1295 if (ep && ep->cur_rate)
1296 goto found;
1297 }
1298 return 0;
1299
1300 found:
1301 if (!find_format(&subs->fmt_list, ep->cur_format, ep->cur_rate,
1302 ep->cur_channels, NULL)) {
1303 usb_audio_dbg(chip, "EP 0x%x being used, but not applicable\n",
1304 ep->ep_num);
1305 return 0;
1306 }
1307
1308 usb_audio_dbg(chip, "EP 0x%x being used, using fixed params:\n",
1309 ep->ep_num);
1310 usb_audio_dbg(chip, "rate=%d, format=%s, channels=%d, period_size=%d, periods=%d\n",
1311 ep->cur_rate, snd_pcm_format_name(ep->cur_format),
1312 ep->cur_channels, ep->cur_period_frames,
1313 ep->cur_buffer_periods);
1314
1315 runtime->hw.formats = pcm_format_to_bits(ep->cur_format);
1316 runtime->hw.rate_min = runtime->hw.rate_max = ep->cur_rate;
1317 runtime->hw.channels_min = runtime->hw.channels_max =
1318 ep->cur_channels;
1319 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1320 runtime->hw.periods_min = runtime->hw.periods_max =
1321 ep->cur_buffer_periods;
1322 subs->fixed_hw = 1;
1323
1324 err = snd_pcm_hw_constraint_minmax(runtime,
1325 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1326 ep->cur_period_frames,
1327 ep->cur_period_frames);
1328 if (err < 0)
1329 return err;
1330
1331 return 1; /* notify the finding */
1332}
Daniel Macke5779992010-03-04 19:46:13 +01001333
1334/*
1335 * set up the runtime hardware information.
1336 */
1337
1338static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1339{
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001340 struct snd_usb_audio *chip = subs->stream->chip;
Eldad Zack88766f02013-04-03 23:18:49 +02001341 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001342 unsigned int pt, ptmin;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001343 int param_period_time_if_needed = -1;
Daniel Macke5779992010-03-04 19:46:13 +01001344 int err;
1345
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001346 mutex_lock(&chip->mutex);
1347 err = apply_hw_constraint_from_sync(runtime, subs);
1348 mutex_unlock(&chip->mutex);
1349 if (err < 0)
1350 return err;
1351 if (err > 0) /* found the matching? */
1352 goto add_extra_rules;
1353
Daniel Macke5779992010-03-04 19:46:13 +01001354 runtime->hw.formats = subs->formats;
1355
1356 runtime->hw.rate_min = 0x7fffffff;
1357 runtime->hw.rate_max = 0;
1358 runtime->hw.channels_min = 256;
1359 runtime->hw.channels_max = 0;
1360 runtime->hw.rates = 0;
1361 ptmin = UINT_MAX;
1362 /* check min/max rates and channels */
Eldad Zack88766f02013-04-03 23:18:49 +02001363 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001364 runtime->hw.rates |= fp->rates;
1365 if (runtime->hw.rate_min > fp->rate_min)
1366 runtime->hw.rate_min = fp->rate_min;
1367 if (runtime->hw.rate_max < fp->rate_max)
1368 runtime->hw.rate_max = fp->rate_max;
1369 if (runtime->hw.channels_min > fp->channels)
1370 runtime->hw.channels_min = fp->channels;
1371 if (runtime->hw.channels_max < fp->channels)
1372 runtime->hw.channels_max = fp->channels;
1373 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1374 /* FIXME: there might be more than one audio formats... */
1375 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1376 fp->frame_size;
1377 }
1378 pt = 125 * (1 << fp->datainterval);
1379 ptmin = min(ptmin, pt);
1380 }
1381
1382 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001383 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001384 /* full speed devices have fixed data packet interval */
1385 ptmin = 1000;
1386 if (ptmin == 1000)
1387 /* if period time doesn't go below 1 ms, no rules needed */
1388 param_period_time_if_needed = -1;
Daniel Macke5779992010-03-04 19:46:13 +01001389
Takashi Iwaie92be812018-05-27 15:09:15 +02001390 err = snd_pcm_hw_constraint_minmax(runtime,
1391 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1392 ptmin, UINT_MAX);
1393 if (err < 0)
1394 return err;
1395
1396 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1397 hw_rule_rate, subs,
1398 SNDRV_PCM_HW_PARAM_FORMAT,
1399 SNDRV_PCM_HW_PARAM_CHANNELS,
1400 param_period_time_if_needed,
1401 -1);
1402 if (err < 0)
1403 return err;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001404
1405add_extra_rules:
Takashi Iwaie92be812018-05-27 15:09:15 +02001406 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1407 hw_rule_channels, subs,
1408 SNDRV_PCM_HW_PARAM_FORMAT,
1409 SNDRV_PCM_HW_PARAM_RATE,
1410 param_period_time_if_needed,
1411 -1);
1412 if (err < 0)
1413 return err;
1414 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1415 hw_rule_format, subs,
1416 SNDRV_PCM_HW_PARAM_RATE,
1417 SNDRV_PCM_HW_PARAM_CHANNELS,
1418 param_period_time_if_needed,
1419 -1);
1420 if (err < 0)
1421 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001422 if (param_period_time_if_needed >= 0) {
1423 err = snd_pcm_hw_rule_add(runtime, 0,
1424 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1425 hw_rule_period_time, subs,
1426 SNDRV_PCM_HW_PARAM_FORMAT,
1427 SNDRV_PCM_HW_PARAM_CHANNELS,
1428 SNDRV_PCM_HW_PARAM_RATE,
1429 -1);
1430 if (err < 0)
Takashi Iwaie92be812018-05-27 15:09:15 +02001431 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001432 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001433
Takashi Iwai18652112020-11-23 09:53:15 +01001434 return 0;
Daniel Macke5779992010-03-04 19:46:13 +01001435}
1436
Takashi Iwai6fddc792018-05-27 13:59:03 +02001437static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
Daniel Macke5779992010-03-04 19:46:13 +01001438{
Takashi Iwai6fddc792018-05-27 13:59:03 +02001439 int direction = substream->stream;
Daniel Macke5779992010-03-04 19:46:13 +01001440 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1441 struct snd_pcm_runtime *runtime = substream->runtime;
1442 struct snd_usb_substream *subs = &as->substream[direction];
Shuah Khan66354f12019-04-01 20:40:22 -04001443 int ret;
Daniel Macke5779992010-03-04 19:46:13 +01001444
1445 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001446 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001447 runtime->hw = snd_usb_hardware;
1448 runtime->private_data = subs;
1449 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001450 /* runtime PM is also done there */
Daniel Mackd24f5062013-04-17 00:01:38 +08001451
1452 /* initialize DSD/DOP context */
1453 subs->dsd_dop.byte_idx = 0;
1454 subs->dsd_dop.channel = 0;
1455 subs->dsd_dop.marker = 1;
1456
Shuah Khan66354f12019-04-01 20:40:22 -04001457 ret = setup_hw_info(runtime, subs);
Takashi Iwai18652112020-11-23 09:53:15 +01001458 if (ret < 0)
1459 return ret;
1460 ret = snd_usb_autoresume(subs->stream->chip);
1461 if (ret < 0)
1462 return ret;
1463 ret = snd_media_stream_init(subs, as->pcm, direction);
1464 if (ret < 0)
1465 snd_usb_autosuspend(subs->stream->chip);
Shuah Khan66354f12019-04-01 20:40:22 -04001466 return ret;
Daniel Macke5779992010-03-04 19:46:13 +01001467}
1468
Takashi Iwai6fddc792018-05-27 13:59:03 +02001469static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
Daniel Macke5779992010-03-04 19:46:13 +01001470{
Takashi Iwai6fddc792018-05-27 13:59:03 +02001471 int direction = substream->stream;
Daniel Macke5779992010-03-04 19:46:13 +01001472 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1473 struct snd_usb_substream *subs = &as->substream[direction];
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001474 int ret;
Daniel Macke5779992010-03-04 19:46:13 +01001475
Shuah Khan66354f12019-04-01 20:40:22 -04001476 snd_media_stop_pipeline(subs);
Daniel Mack68e67f42012-07-12 13:08:40 +02001477
Takashi Iwai8a463222018-05-02 10:04:27 +02001478 if (!as->chip->keep_iface &&
1479 subs->interface >= 0 &&
Takashi Iwai47ab1542015-08-25 16:09:00 +02001480 !snd_usb_lock_shutdown(subs->stream->chip)) {
Daniel Mack68e67f42012-07-12 13:08:40 +02001481 usb_set_interface(subs->dev, subs->interface, 0);
1482 subs->interface = -1;
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001483 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
Takashi Iwai47ab1542015-08-25 16:09:00 +02001484 snd_usb_unlock_shutdown(subs->stream->chip);
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001485 if (ret < 0)
1486 return ret;
Daniel Mack68e67f42012-07-12 13:08:40 +02001487 }
1488
Daniel Macke5779992010-03-04 19:46:13 +01001489 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001490 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001491
Daniel Mack68e67f42012-07-12 13:08:40 +02001492 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001493}
1494
1495/* Since a URB can handle only a single linear buffer, we must use double
1496 * buffering when the data to be transferred overflows the buffer boundary.
1497 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1498 * for all URBs.
1499 */
1500static void retire_capture_urb(struct snd_usb_substream *subs,
1501 struct urb *urb)
1502{
1503 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1504 unsigned int stride, frames, bytes, oldptr;
1505 int i, period_elapsed = 0;
1506 unsigned long flags;
1507 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001508 int current_frame_number;
1509
1510 /* read frame number here, update pointer in critical section */
1511 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001512
1513 stride = runtime->frame_bits >> 3;
1514
1515 for (i = 0; i < urb->number_of_packets; i++) {
Calvin Owens1539d4f2013-04-12 22:33:59 -05001516 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
Daniel Mackedcd3632012-04-12 13:51:12 +02001517 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001518 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1519 i, urb->iso_frame_desc[i].status);
Daniel Mackedcd3632012-04-12 13:51:12 +02001520 // continue;
1521 }
1522 bytes = urb->iso_frame_desc[i].actual_length;
Hector Martin1b7ecc22020-08-10 17:24:00 +09001523 if (subs->stream_offset_adj > 0) {
1524 unsigned int adj = min(subs->stream_offset_adj, bytes);
1525 cp += adj;
1526 bytes -= adj;
1527 subs->stream_offset_adj -= adj;
1528 }
Daniel Mackedcd3632012-04-12 13:51:12 +02001529 frames = bytes / stride;
1530 if (!subs->txfr_quirk)
1531 bytes = frames * stride;
1532 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001533 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001534 bytes = frames * stride;
Takashi Iwai377a8792018-05-16 20:07:18 +02001535 dev_warn_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001536 "Corrected urb data len. %d->%d\n",
Daniel Mackedcd3632012-04-12 13:51:12 +02001537 oldbytes, bytes);
1538 }
1539 /* update the current pointer */
1540 spin_lock_irqsave(&subs->lock, flags);
1541 oldptr = subs->hwptr_done;
1542 subs->hwptr_done += bytes;
1543 if (subs->hwptr_done >= runtime->buffer_size * stride)
1544 subs->hwptr_done -= runtime->buffer_size * stride;
1545 frames = (bytes + (oldptr % stride)) / stride;
1546 subs->transfer_done += frames;
1547 if (subs->transfer_done >= runtime->period_size) {
1548 subs->transfer_done -= runtime->period_size;
1549 period_elapsed = 1;
1550 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001551 /* capture delay is by construction limited to one URB,
1552 * reset delays here
1553 */
1554 runtime->delay = subs->last_delay = 0;
1555
1556 /* realign last_frame_number */
1557 subs->last_frame_number = current_frame_number;
1558 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1559
Daniel Mackedcd3632012-04-12 13:51:12 +02001560 spin_unlock_irqrestore(&subs->lock, flags);
1561 /* copy a data chunk */
1562 if (oldptr + bytes > runtime->buffer_size * stride) {
1563 unsigned int bytes1 =
1564 runtime->buffer_size * stride - oldptr;
1565 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1566 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1567 } else {
1568 memcpy(runtime->dma_area + oldptr, cp, bytes);
1569 }
1570 }
1571
1572 if (period_elapsed)
1573 snd_pcm_period_elapsed(subs->pcm_substream);
1574}
1575
Daniel Mackd24f5062013-04-17 00:01:38 +08001576static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1577 struct urb *urb, unsigned int bytes)
1578{
1579 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1580 unsigned int stride = runtime->frame_bits >> 3;
1581 unsigned int dst_idx = 0;
1582 unsigned int src_idx = subs->hwptr_done;
1583 unsigned int wrap = runtime->buffer_size * stride;
1584 u8 *dst = urb->transfer_buffer;
1585 u8 *src = runtime->dma_area;
1586 u8 marker[] = { 0x05, 0xfa };
1587
1588 /*
1589 * The DSP DOP format defines a way to transport DSD samples over
1590 * normal PCM data endpoints. It requires stuffing of marker bytes
1591 * (0x05 and 0xfa, alternating per sample frame), and then expects
1592 * 2 additional bytes of actual payload. The whole frame is stored
1593 * LSB.
1594 *
1595 * Hence, for a stereo transport, the buffer layout looks like this,
1596 * where L refers to left channel samples and R to right.
1597 *
1598 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1599 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1600 * .....
1601 *
1602 */
1603
1604 while (bytes--) {
1605 if (++subs->dsd_dop.byte_idx == 3) {
1606 /* frame boundary? */
1607 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1608 src_idx += 2;
1609 subs->dsd_dop.byte_idx = 0;
1610
1611 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1612 /* alternate the marker */
1613 subs->dsd_dop.marker++;
1614 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1615 subs->dsd_dop.channel = 0;
1616 }
1617 } else {
1618 /* stuff the DSD payload */
1619 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001620
1621 if (subs->cur_audiofmt->dsd_bitrev)
1622 dst[dst_idx++] = bitrev8(src[idx]);
1623 else
1624 dst[dst_idx++] = src[idx];
1625
Daniel Mackd24f5062013-04-17 00:01:38 +08001626 subs->hwptr_done++;
1627 }
1628 }
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001629 if (subs->hwptr_done >= runtime->buffer_size * stride)
1630 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001631}
1632
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001633static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1634 int offset, int stride, unsigned int bytes)
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001635{
1636 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1637
1638 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1639 /* err, the transferred area goes over buffer boundary. */
1640 unsigned int bytes1 =
1641 runtime->buffer_size * stride - subs->hwptr_done;
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001642 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001643 runtime->dma_area + subs->hwptr_done, bytes1);
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001644 memcpy(urb->transfer_buffer + offset + bytes1,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001645 runtime->dma_area, bytes - bytes1);
1646 } else {
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001647 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001648 runtime->dma_area + subs->hwptr_done, bytes);
1649 }
1650 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001651 if (subs->hwptr_done >= runtime->buffer_size * stride)
1652 subs->hwptr_done -= runtime->buffer_size * stride;
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001653}
1654
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001655static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1656 struct urb *urb, int stride,
1657 unsigned int bytes)
1658{
1659 __le32 packet_length;
1660 int i;
1661
1662 /* Put __le32 length descriptor at start of each packet. */
1663 for (i = 0; i < urb->number_of_packets; i++) {
1664 unsigned int length = urb->iso_frame_desc[i].length;
1665 unsigned int offset = urb->iso_frame_desc[i].offset;
1666
1667 packet_length = cpu_to_le32(length);
1668 offset += i * sizeof(packet_length);
1669 urb->iso_frame_desc[i].offset = offset;
1670 urb->iso_frame_desc[i].length += sizeof(packet_length);
1671 memcpy(urb->transfer_buffer + offset,
1672 &packet_length, sizeof(packet_length));
1673 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1674 stride, length);
1675 }
1676 /* Adjust transfer size accordingly. */
1677 bytes += urb->number_of_packets * sizeof(packet_length);
1678 return bytes;
1679}
1680
Daniel Mackedcd3632012-04-12 13:51:12 +02001681static void prepare_playback_urb(struct snd_usb_substream *subs,
1682 struct urb *urb)
1683{
1684 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001685 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001686 struct snd_urb_ctx *ctx = urb->context;
1687 unsigned int counts, frames, bytes;
1688 int i, stride, period_elapsed = 0;
1689 unsigned long flags;
1690
1691 stride = runtime->frame_bits >> 3;
1692
1693 frames = 0;
1694 urb->number_of_packets = 0;
1695 spin_lock_irqsave(&subs->lock, flags);
Alan Stern976b6c02013-09-24 15:51:58 -04001696 subs->frame_limit += ep->max_urb_frames;
Daniel Mackedcd3632012-04-12 13:51:12 +02001697 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001698 if (ctx->packet_size[i])
1699 counts = ctx->packet_size[i];
Alexander Tsoyf0bd62b2020-04-24 05:24:48 +03001700 else if (ep->sync_master)
1701 counts = snd_usb_endpoint_slave_next_packet_size(ep);
Daniel Mack245baf92012-08-30 18:52:30 +02001702 else
1703 counts = snd_usb_endpoint_next_packet_size(ep);
1704
Daniel Mackedcd3632012-04-12 13:51:12 +02001705 /* set up descriptor */
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001706 urb->iso_frame_desc[i].offset = frames * ep->stride;
1707 urb->iso_frame_desc[i].length = counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001708 frames += counts;
1709 urb->number_of_packets++;
1710 subs->transfer_done += counts;
1711 if (subs->transfer_done >= runtime->period_size) {
1712 subs->transfer_done -= runtime->period_size;
Alan Stern976b6c02013-09-24 15:51:58 -04001713 subs->frame_limit = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001714 period_elapsed = 1;
1715 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1716 if (subs->transfer_done > 0) {
1717 /* FIXME: fill-max mode is not
1718 * supported yet */
1719 frames -= subs->transfer_done;
1720 counts -= subs->transfer_done;
1721 urb->iso_frame_desc[i].length =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001722 counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001723 subs->transfer_done = 0;
1724 }
1725 i++;
1726 if (i < ctx->packets) {
1727 /* add a transfer delimiter */
1728 urb->iso_frame_desc[i].offset =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001729 frames * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001730 urb->iso_frame_desc[i].length = 0;
1731 urb->number_of_packets++;
1732 }
1733 break;
1734 }
1735 }
Alan Stern976b6c02013-09-24 15:51:58 -04001736 /* finish at the period boundary or after enough frames */
1737 if ((period_elapsed ||
1738 subs->transfer_done >= subs->frame_limit) &&
1739 !snd_usb_endpoint_implicit_feedback_sink(ep))
Daniel Mackedcd3632012-04-12 13:51:12 +02001740 break;
1741 }
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001742 bytes = frames * ep->stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001743
1744 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1745 subs->cur_audiofmt->dsd_dop)) {
1746 fill_playback_urb_dsd_dop(subs, urb, bytes);
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001747 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1748 subs->cur_audiofmt->dsd_bitrev)) {
1749 /* bit-reverse the bytes */
1750 u8 *buf = urb->transfer_buffer;
1751 for (i = 0; i < bytes; i++) {
1752 int idx = (subs->hwptr_done + i)
1753 % (runtime->buffer_size * stride);
1754 buf[i] = bitrev8(runtime->dma_area[idx]);
1755 }
1756
1757 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001758 if (subs->hwptr_done >= runtime->buffer_size * stride)
1759 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001760 } else {
Daniel Mackd24f5062013-04-17 00:01:38 +08001761 /* usual PCM */
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001762 if (!subs->tx_length_quirk)
1763 copy_to_urb(subs, urb, 0, stride, bytes);
1764 else
1765 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1766 /* bytes is now amount of outgoing data */
Daniel Mackedcd3632012-04-12 13:51:12 +02001767 }
Daniel Mackd24f5062013-04-17 00:01:38 +08001768
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001769 /* update delay with exact number of samples queued */
1770 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001771 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001772 subs->last_delay = runtime->delay;
1773
1774 /* realign last_frame_number */
1775 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1776 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1777
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001778 if (subs->trigger_tstamp_pending_update) {
1779 /* this is the first actual URB submitted,
1780 * update trigger timestamp to reflect actual start time
1781 */
1782 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1783 subs->trigger_tstamp_pending_update = false;
1784 }
1785
Daniel Mackedcd3632012-04-12 13:51:12 +02001786 spin_unlock_irqrestore(&subs->lock, flags);
1787 urb->transfer_buffer_length = bytes;
1788 if (period_elapsed)
1789 snd_pcm_period_elapsed(subs->pcm_substream);
1790}
1791
1792/*
1793 * process after playback data complete
1794 * - decrease the delay count again
1795 */
1796static void retire_playback_urb(struct snd_usb_substream *subs,
1797 struct urb *urb)
1798{
1799 unsigned long flags;
1800 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001801 struct snd_usb_endpoint *ep = subs->data_endpoint;
1802 int processed = urb->transfer_buffer_length / ep->stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001803 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001804
Alexander Tsoy5ff40e62020-06-29 06:26:07 +03001805 /* ignore the delay accounting when processed=0 is given, i.e.
1806 * silent payloads are processed before handling the actual data
Takashi Iwai1213a202012-09-06 14:58:00 +02001807 */
1808 if (!processed)
1809 return;
1810
Daniel Mackedcd3632012-04-12 13:51:12 +02001811 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001812 if (!subs->last_delay)
1813 goto out; /* short path */
1814
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001815 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1816 /* update delay with exact number of samples played */
1817 if (processed > subs->last_delay)
1818 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001819 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001820 subs->last_delay -= processed;
1821 runtime->delay = subs->last_delay;
1822
1823 /*
1824 * Report when delay estimate is off by more than 2ms.
1825 * The error should be lower than 2ms since the estimate relies
1826 * on two reads of a counter updated every ms.
1827 */
Sander Eikelenboomb7a77232014-05-02 15:09:27 +02001828 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1829 dev_dbg_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001830 "delay: estimated %d, actual %d\n",
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001831 est_delay, subs->last_delay);
1832
Takashi Iwai48779a02012-11-23 16:00:37 +01001833 if (!subs->running) {
1834 /* update last_frame_number for delay counting here since
1835 * prepare_playback_urb won't be called during pause
1836 */
1837 subs->last_frame_number =
1838 usb_get_current_frame_number(subs->dev) & 0xff;
1839 }
1840
1841 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001842 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001843}
1844
Daniel Mackedcd3632012-04-12 13:51:12 +02001845static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1846 int cmd)
1847{
1848 struct snd_usb_substream *subs = substream->runtime->private_data;
1849
1850 switch (cmd) {
1851 case SNDRV_PCM_TRIGGER_START:
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001852 subs->trigger_tstamp_pending_update = true;
Gustavo A. R. Silvac0dbbda2020-07-08 15:32:36 -05001853 fallthrough;
Daniel Mackedcd3632012-04-12 13:51:12 +02001854 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1855 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1856 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001857 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001858 return 0;
1859 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001860 stop_endpoints(subs);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001861 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001862 return 0;
1863 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1864 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001865 /* keep retire_data_urb for delay calculation */
1866 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001867 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001868 return 0;
Hui Wang92adc962019-12-18 21:26:50 +08001869 case SNDRV_PCM_TRIGGER_SUSPEND:
1870 if (subs->stream->chip->setup_fmt_after_resume_quirk) {
Takashi Iwaia032ff02019-12-18 20:05:39 +01001871 stop_endpoints(subs);
Hui Wang92adc962019-12-18 21:26:50 +08001872 subs->need_setup_fmt = true;
1873 return 0;
1874 }
1875 break;
Daniel Mackedcd3632012-04-12 13:51:12 +02001876 }
1877
1878 return -EINVAL;
1879}
1880
Daniel Mackafe25962012-06-16 16:58:04 +02001881static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1882 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001883{
1884 int err;
1885 struct snd_usb_substream *subs = substream->runtime->private_data;
1886
1887 switch (cmd) {
1888 case SNDRV_PCM_TRIGGER_START:
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +02001889 err = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +02001890 if (err < 0)
1891 return err;
1892
1893 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001894 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001895 return 0;
1896 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001897 stop_endpoints(subs);
Takashi Iwaiff58bbc2020-06-16 14:09:21 +02001898 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001899 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001900 return 0;
1901 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1902 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001903 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001904 return 0;
1905 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1906 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001907 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001908 return 0;
Hui Wang92adc962019-12-18 21:26:50 +08001909 case SNDRV_PCM_TRIGGER_SUSPEND:
1910 if (subs->stream->chip->setup_fmt_after_resume_quirk) {
Takashi Iwaia032ff02019-12-18 20:05:39 +01001911 stop_endpoints(subs);
Hui Wang92adc962019-12-18 21:26:50 +08001912 subs->need_setup_fmt = true;
1913 return 0;
1914 }
1915 break;
Daniel Mackedcd3632012-04-12 13:51:12 +02001916 }
1917
1918 return -EINVAL;
1919}
1920
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301921static const struct snd_pcm_ops snd_usb_playback_ops = {
Takashi Iwai6fddc792018-05-27 13:59:03 +02001922 .open = snd_usb_pcm_open,
1923 .close = snd_usb_pcm_close,
Daniel Macke5779992010-03-04 19:46:13 +01001924 .hw_params = snd_usb_hw_params,
1925 .hw_free = snd_usb_hw_free,
1926 .prepare = snd_usb_pcm_prepare,
1927 .trigger = snd_usb_substream_playback_trigger,
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001928 .sync_stop = snd_usb_pcm_sync_stop,
Daniel Macke5779992010-03-04 19:46:13 +01001929 .pointer = snd_usb_pcm_pointer,
Daniel Macke5779992010-03-04 19:46:13 +01001930};
1931
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301932static const struct snd_pcm_ops snd_usb_capture_ops = {
Takashi Iwai6fddc792018-05-27 13:59:03 +02001933 .open = snd_usb_pcm_open,
1934 .close = snd_usb_pcm_close,
Daniel Macke5779992010-03-04 19:46:13 +01001935 .hw_params = snd_usb_hw_params,
1936 .hw_free = snd_usb_hw_free,
1937 .prepare = snd_usb_pcm_prepare,
1938 .trigger = snd_usb_substream_capture_trigger,
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001939 .sync_stop = snd_usb_pcm_sync_stop,
Daniel Macke5779992010-03-04 19:46:13 +01001940 .pointer = snd_usb_pcm_pointer,
Takashi Iwaif274baa2018-05-27 13:01:17 +02001941};
1942
Daniel Macke5779992010-03-04 19:46:13 +01001943void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1944{
Takashi Iwaif274baa2018-05-27 13:01:17 +02001945 const struct snd_pcm_ops *ops;
1946
Takashi Iwaib315997d2019-11-05 16:18:40 +01001947 ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
Takashi Iwaif274baa2018-05-27 13:01:17 +02001948 &snd_usb_playback_ops : &snd_usb_capture_ops;
Takashi Iwaif274baa2018-05-27 13:01:17 +02001949 snd_pcm_set_ops(pcm, stream, ops);
1950}
1951
1952void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
1953{
1954 struct snd_pcm *pcm = subs->stream->pcm;
1955 struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
1956 struct device *dev = subs->dev->bus->controller;
1957
Takashi Iwaib315997d2019-11-05 16:18:40 +01001958 if (snd_usb_use_vmalloc)
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +01001959 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_VMALLOC,
1960 NULL, 0, 0);
Takashi Iwaib315997d2019-11-05 16:18:40 +01001961 else
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +01001962 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_DEV_SG,
1963 dev, 64*1024, 512*1024);
Daniel Macke5779992010-03-04 19:46:13 +01001964}