blob: fc028492dd1a68145923500abcbf586084614f5f [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 Iwaicab941b2020-11-23 09:53:33 +010083static const struct audioformat *
Takashi Iwaibf6313a2020-11-23 09:53:31 +010084find_format(struct list_head *fmt_list_head, snd_pcm_format_t format,
85 unsigned int rate, unsigned int channels, bool strict_match,
86 struct snd_usb_substream *subs)
Daniel Macke5779992010-03-04 19:46:13 +010087{
Takashi Iwaicab941b2020-11-23 09:53:33 +010088 const struct audioformat *fp;
89 const struct audioformat *found = NULL;
Daniel Macke5779992010-03-04 19:46:13 +010090 int cur_attr = 0, attr;
91
Takashi Iwai5a6c3e12020-11-23 09:53:16 +010092 list_for_each_entry(fp, fmt_list_head, list) {
Takashi Iwaibf6313a2020-11-23 09:53:31 +010093 if (strict_match) {
94 if (!(fp->formats & pcm_format_to_bits(format)))
95 continue;
96 if (fp->channels != channels)
97 continue;
98 }
Takashi Iwai5a6c3e12020-11-23 09:53:16 +010099 if (rate < fp->rate_min || rate > fp->rate_max)
Daniel Macke5779992010-03-04 19:46:13 +0100100 continue;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100101 if (!(fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
Daniel Macke5779992010-03-04 19:46:13 +0100102 unsigned int i;
103 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100104 if (fp->rate_table[i] == rate)
Daniel Macke5779992010-03-04 19:46:13 +0100105 break;
106 if (i >= fp->nr_rates)
107 continue;
108 }
109 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100110 if (!found) {
Daniel Macke5779992010-03-04 19:46:13 +0100111 found = fp;
112 cur_attr = attr;
113 continue;
114 }
115 /* avoid async out and adaptive in if the other method
116 * supports the same format.
117 * this is a workaround for the case like
118 * M-audio audiophile USB.
119 */
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100120 if (subs && attr != cur_attr) {
Daniel Macke5779992010-03-04 19:46:13 +0100121 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
122 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
123 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
124 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
125 continue;
126 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
127 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
128 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
129 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
130 found = fp;
131 cur_attr = attr;
132 continue;
133 }
134 }
135 /* find the format with the largest max. packet size */
136 if (fp->maxpacksize > found->maxpacksize) {
137 found = fp;
138 cur_attr = attr;
139 }
140 }
141 return found;
142}
143
Takashi Iwaicab941b2020-11-23 09:53:33 +0100144static const struct audioformat *
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100145find_substream_format(struct snd_usb_substream *subs,
146 const struct snd_pcm_hw_params *params)
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100147{
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100148 return find_format(&subs->fmt_list, params_format(params),
149 params_rate(params), params_channels(params),
150 true, subs);
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100151}
152
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100153static int init_pitch_v1(struct snd_usb_audio *chip, int ep)
Daniel Macke5779992010-03-04 19:46:13 +0100154{
Daniel Mack767d75a2010-03-04 19:46:17 +0100155 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100156 unsigned char data[1];
157 int err;
158
Daniel Mack767d75a2010-03-04 19:46:17 +0100159 data[0] = 1;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200160 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
161 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
162 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
163 data, sizeof(data));
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100164 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100165}
166
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100167static int init_pitch_v2(struct snd_usb_audio *chip, int ep)
Daniel Mack92c25612010-05-26 18:11:39 +0200168{
169 struct usb_device *dev = chip->dev;
170 unsigned char data[1];
Daniel Mack92c25612010-05-26 18:11:39 +0200171 int err;
172
Daniel Mack92c25612010-05-26 18:11:39 +0200173 data[0] = 1;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200174 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
175 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
176 UAC2_EP_CS_PITCH << 8, 0,
177 data, sizeof(data));
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100178 return err;
Daniel Mack92c25612010-05-26 18:11:39 +0200179}
180
Daniel Mack767d75a2010-03-04 19:46:17 +0100181/*
Daniel Mack92c25612010-05-26 18:11:39 +0200182 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100183 */
Takashi Iwai73037c82020-11-23 09:53:26 +0100184int snd_usb_init_pitch(struct snd_usb_audio *chip,
Takashi Iwaicab941b2020-11-23 09:53:33 +0100185 const struct audioformat *fmt)
Daniel Mack767d75a2010-03-04 19:46:17 +0100186{
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100187 int err;
188
Daniel Mack92c25612010-05-26 18:11:39 +0200189 /* if endpoint doesn't have pitch control, bail out */
190 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
191 return 0;
192
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100193 usb_audio_dbg(chip, "enable PITCH for EP 0x%x\n", fmt->endpoint);
194
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100195 switch (fmt->protocol) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100196 case UAC_VERSION_1:
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100197 err = init_pitch_v1(chip, fmt->endpoint);
198 break;
Daniel Mack767d75a2010-03-04 19:46:17 +0100199 case UAC_VERSION_2:
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100200 err = init_pitch_v2(chip, fmt->endpoint);
201 break;
202 default:
203 return 0;
Daniel Mack767d75a2010-03-04 19:46:17 +0100204 }
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100205
206 if (err < 0) {
207 usb_audio_err(chip, "failed to enable PITCH for EP 0x%x\n",
208 fmt->endpoint);
209 return err;
210 }
211
212 return 0;
Daniel Mack767d75a2010-03-04 19:46:17 +0100213}
214
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100215static bool stop_endpoints(struct snd_usb_substream *subs)
Takashi Iwai57234bc2020-11-23 09:53:27 +0100216{
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100217 bool stopped = 0;
218
Takashi Iwai57234bc2020-11-23 09:53:27 +0100219 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
220 snd_usb_endpoint_stop(subs->sync_endpoint);
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100221 stopped = true;
Takashi Iwai57234bc2020-11-23 09:53:27 +0100222 }
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100223 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
Takashi Iwai57234bc2020-11-23 09:53:27 +0100224 snd_usb_endpoint_stop(subs->data_endpoint);
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100225 stopped = true;
226 }
227 return stopped;
Takashi Iwai57234bc2020-11-23 09:53:27 +0100228}
229
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200230static int start_endpoints(struct snd_usb_substream *subs)
Daniel Mackedcd3632012-04-12 13:51:12 +0200231{
232 int err;
233
234 if (!subs->data_endpoint)
235 return -EINVAL;
236
237 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100238 err = snd_usb_endpoint_start(subs->data_endpoint);
Daniel Mackedcd3632012-04-12 13:51:12 +0200239 if (err < 0) {
240 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
Takashi Iwai57234bc2020-11-23 09:53:27 +0100241 goto error;
Daniel Mackedcd3632012-04-12 13:51:12 +0200242 }
243 }
244
245 if (subs->sync_endpoint &&
246 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100247 err = snd_usb_endpoint_start(subs->sync_endpoint);
Daniel Mackedcd3632012-04-12 13:51:12 +0200248 if (err < 0) {
249 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
Takashi Iwai57234bc2020-11-23 09:53:27 +0100250 goto error;
Daniel Mackedcd3632012-04-12 13:51:12 +0200251 }
252 }
253
254 return 0;
Takashi Iwai57234bc2020-11-23 09:53:27 +0100255
256 error:
257 stop_endpoints(subs);
258 return err;
Daniel Mackedcd3632012-04-12 13:51:12 +0200259}
260
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100261static void sync_pending_stops(struct snd_usb_substream *subs)
262{
263 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
264 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
265}
266
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100267/* PCM sync_stop callback */
268static int snd_usb_pcm_sync_stop(struct snd_pcm_substream *substream)
269{
270 struct snd_usb_substream *subs = substream->runtime->private_data;
271
272 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
273 sync_pending_stops(subs);
274 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100275 }
Takashi Iwaidc5eafe2019-12-10 07:34:54 +0100276 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +0200277}
278
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100279/* Check whether the given iface:altsetting points to an implicit fb source */
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100280static bool search_generic_implicit_fb(struct snd_usb_audio *chip, int ifnum,
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100281 unsigned int altsetting,
282 struct usb_host_interface **altsp,
283 unsigned int *ep)
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100284{
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100285 struct usb_host_interface *alts;
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100286 struct usb_interface_descriptor *altsd;
287 struct usb_endpoint_descriptor *epd;
288
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100289 alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100290 if (!alts)
291 return false;
292 altsd = get_iface_desc(alts);
293 if (altsd->bInterfaceClass != USB_CLASS_AUDIO ||
294 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING ||
295 altsd->bInterfaceProtocol != UAC_VERSION_2 ||
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100296 altsd->bNumEndpoints < 1)
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100297 return false;
298 epd = get_endpoint(alts, 0);
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100299 if (!usb_endpoint_is_isoc_in(epd) ||
300 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
301 USB_ENDPOINT_USAGE_IMPLICIT_FB)
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100302 return false;
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100303 *ep = epd->bEndpointAddress;
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100304 *altsp = alts;
305 return true;
306}
307
308/* Like the function above, but specific to Roland with vendor class and hack */
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100309static bool search_roland_implicit_fb(struct snd_usb_audio *chip, int ifnum,
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100310 unsigned int altsetting,
311 struct usb_host_interface **altsp,
312 unsigned int *ep)
313{
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100314 struct usb_host_interface *alts;
315 struct usb_interface_descriptor *altsd;
316 struct usb_endpoint_descriptor *epd;
317
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100318 alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100319 if (!alts)
320 return false;
321 altsd = get_iface_desc(alts);
322 if (altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
323 (altsd->bInterfaceSubClass != 2 &&
324 altsd->bInterfaceProtocol != 2) ||
325 altsd->bNumEndpoints < 1)
326 return false;
327 epd = get_endpoint(alts, 0);
328 if (!usb_endpoint_is_isoc_in(epd) ||
329 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
330 USB_ENDPOINT_USAGE_IMPLICIT_FB)
331 return false;
332 *ep = epd->bEndpointAddress;
333 *altsp = alts;
334 return true;
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100335}
336
Manuel Reinhardt2bc16b92019-01-31 15:32:35 +0100337/* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
338 * applies. Returns 1 if a quirk was found.
339 */
Takashi Iwaif6581c02020-11-23 09:53:14 +0100340static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
341 struct audioformat *fmt,
Takashi Iwaif6581c02020-11-23 09:53:14 +0100342 struct usb_host_interface *alts)
Daniel Macke5779992010-03-04 19:46:13 +0100343{
Takashi Iwaif6581c02020-11-23 09:53:14 +0100344 struct usb_device *dev = chip->dev;
345 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100346 struct usb_interface *iface;
Takashi Iwaif6581c02020-11-23 09:53:14 +0100347 unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Eldad Zacka60945f2013-08-03 10:50:18 +0200348 unsigned int ep;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500349 unsigned int ifnum;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200350
Takashi Iwaif6581c02020-11-23 09:53:14 +0100351 switch (chip->usb_id) {
Eldad Zackca10a7e2012-11-28 23:55:41 +0100352 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500353 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Geoffrey D. Bennett0938eca2020-11-04 22:27:17 +1030354 case USB_ID(0x22f0, 0x0006): /* Allen&Heath Qu-16 */
Eldad Zack914273c2013-08-03 10:50:21 +0200355 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500356 ifnum = 3;
357 goto add_sync_ep_from_ifnum;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200358 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
359 case USB_ID(0x0763, 0x2081):
Eldad Zack914273c2013-08-03 10:50:21 +0200360 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500361 ifnum = 2;
362 goto add_sync_ep_from_ifnum;
363 case USB_ID(0x2466, 0x8003): /* Fractal Audio Axe-Fx II */
Geoffrey D. Bennett26201dd2020-11-04 22:37:05 +1030364 case USB_ID(0x0499, 0x172a): /* Yamaha MODX */
Alberto Aguirre17f08b02016-12-08 00:36:48 -0600365 ep = 0x86;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500366 ifnum = 2;
367 goto add_sync_ep_from_ifnum;
Alberto Aguirre91a85612018-04-18 09:35:35 -0500368 case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx III */
369 ep = 0x81;
370 ifnum = 2;
371 goto add_sync_ep_from_ifnum;
Keith Winsteinf15cfca2020-10-25 22:05:47 -0700372 case USB_ID(0x1686, 0xf029): /* Zoom UAC-2 */
373 ep = 0x82;
374 ifnum = 2;
375 goto add_sync_ep_from_ifnum;
Takashi Iwai1a157182019-08-20 08:58:12 +0200376 case USB_ID(0x1397, 0x0001): /* Behringer UFX1604 */
Alberto Aguirre103e9622018-04-18 09:35:34 -0500377 case USB_ID(0x1397, 0x0002): /* Behringer UFX1204 */
Lassi Ylikojola5e35dc02018-02-09 16:51:36 +0200378 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500379 ifnum = 1;
380 goto add_sync_ep_from_ifnum;
Alexander Tsoy2edb84e2020-02-29 18:18:15 +0300381 case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook II/IIc */
382 /* MicroBook IIc */
383 if (altsd->bInterfaceClass == USB_CLASS_AUDIO)
384 return 0;
385
386 /* MicroBook II */
Manuel Reinhardta6340902019-02-28 20:34:04 +0100387 ep = 0x84;
388 ifnum = 0;
389 goto add_sync_ep_from_ifnum;
Alexander Tsoyc2491772020-01-15 18:13:58 +0300390 case USB_ID(0x07fd, 0x0008): /* MOTU M Series */
Laurence Tratt3da87ec2020-06-21 08:50:05 +0100391 case USB_ID(0x31e9, 0x0001): /* Solid State Logic SSL2 */
Laurence Tratte7585db2020-06-12 12:18:07 +0100392 case USB_ID(0x31e9, 0x0002): /* Solid State Logic SSL2+ */
Joshua Sivec7c5b8922020-08-25 18:55:18 +0200393 case USB_ID(0x0499, 0x172f): /* Steinberg UR22C */
Pavel Hofmanb6a1e782020-07-03 12:04:33 +0200394 case USB_ID(0x0d9a, 0x00df): /* RTX6001 */
Alexander Tsoyc2491772020-01-15 18:13:58 +0300395 ep = 0x81;
396 ifnum = 2;
397 goto add_sync_ep_from_ifnum;
Dmitry Panchenko7fccfec2020-06-01 13:22:24 +0300398 case USB_ID(0x2b73, 0x000a): /* Pioneer DJ DJM-900NXS2 */
František Kučera14335d82020-08-25 17:31:13 +0200399 case USB_ID(0x2b73, 0x0017): /* Pioneer DJ DJM-250MK2 */
Dmitry Panchenko7fccfec2020-06-01 13:22:24 +0300400 ep = 0x82;
401 ifnum = 0;
402 goto add_sync_ep_from_ifnum;
Szabolcs Szőke7571b6a2019-10-11 19:19:36 +0200403 case USB_ID(0x0582, 0x01d8): /* BOSS Katana */
404 /* BOSS Katana amplifiers do not need quirks */
405 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200406 }
Alberto Aguirre103e9622018-04-18 09:35:34 -0500407
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100408 /* Generic UAC2 implicit feedback */
409 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
410 altsd->bInterfaceClass == USB_CLASS_AUDIO &&
411 altsd->bInterfaceProtocol == UAC_VERSION_2 &&
412 altsd->bNumEndpoints == 1) {
413 ifnum = altsd->bInterfaceNumber + 1;
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100414 if (search_generic_implicit_fb(chip, ifnum,
Takashi Iwai2e43aae2020-11-23 09:53:10 +0100415 altsd->bAlternateSetting,
416 &alts, &ep))
417 goto add_sync_ep;
418 }
419
420 /* Roland/BOSS implicit feedback with vendor spec class */
Eldad Zack914273c2013-08-03 10:50:21 +0200421 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100422 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
423 altsd->bInterfaceProtocol == 2 &&
424 altsd->bNumEndpoints == 1 &&
Takashi Iwaif6581c02020-11-23 09:53:14 +0100425 USB_ID_VENDOR(chip->usb_id) == 0x0582 /* Roland */) {
426 ifnum = altsd->bInterfaceNumber + 1;
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100427 if (search_roland_implicit_fb(chip, ifnum,
Takashi Iwaif6581c02020-11-23 09:53:14 +0100428 altsd->bAlternateSetting,
429 &alts, &ep))
430 goto add_sync_ep;
431 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200432
Eldad Zacka60945f2013-08-03 10:50:18 +0200433 /* No quirk */
434 return 0;
435
Alberto Aguirre103e9622018-04-18 09:35:34 -0500436add_sync_ep_from_ifnum:
437 iface = usb_ifnum_to_if(dev, ifnum);
438
Johan Hovold5d1b7122020-01-14 09:39:53 +0100439 if (!iface || iface->num_altsetting < 2)
Takashi Iwaif6581c02020-11-23 09:53:14 +0100440 return 0;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500441
442 alts = &iface->altsetting[1];
443
Eldad Zacka60945f2013-08-03 10:50:18 +0200444add_sync_ep:
Takashi Iwaif6581c02020-11-23 09:53:14 +0100445 fmt->sync_ep = ep;
446 fmt->sync_iface = ifnum;
447 fmt->sync_altsetting = alts->desc.bAlternateSetting;
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100448 fmt->sync_ep_idx = 0;
Takashi Iwaif6581c02020-11-23 09:53:14 +0100449 fmt->implicit_fb = 1;
450 dev_dbg(&dev->dev, "%d:%d: found implicit_fb sync_ep=%x, iface=%d, alt=%d\n",
451 fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface,
452 fmt->sync_altsetting);
Eldad Zacka60945f2013-08-03 10:50:18 +0200453
Manuel Reinhardt2bc16b92019-01-31 15:32:35 +0100454 return 1;
Eldad Zacka60945f2013-08-03 10:50:18 +0200455}
456
Takashi Iwaif6581c02020-11-23 09:53:14 +0100457int snd_usb_audioformat_set_sync_ep(struct snd_usb_audio *chip,
458 struct audioformat *fmt)
Eldad Zacka60945f2013-08-03 10:50:18 +0200459{
Takashi Iwaif6581c02020-11-23 09:53:14 +0100460 struct usb_device *dev = chip->dev;
Takashi Iwaif6581c02020-11-23 09:53:14 +0100461 struct usb_host_interface *alts;
462 struct usb_interface_descriptor *altsd;
463 unsigned int ep, attr, sync_attr;
464 bool is_playback;
Eldad Zacka60945f2013-08-03 10:50:18 +0200465 int err;
466
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100467 alts = snd_usb_get_host_interface(chip, fmt->iface, fmt->altsetting);
Takashi Iwaif6581c02020-11-23 09:53:14 +0100468 if (!alts)
469 return 0;
470 altsd = get_iface_desc(alts);
471
472 is_playback = !(get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
473 if (is_playback) {
Takashi Iwaie42a09b2020-11-23 09:53:22 +0100474 err = audioformat_implicit_fb_quirk(chip, fmt, alts);
Takashi Iwaif6581c02020-11-23 09:53:14 +0100475 if (err > 0)
476 return 0;
477 }
Manuel Reinhardt2bc16b92019-01-31 15:32:35 +0100478
Eldad Zackf34d0652013-08-03 10:50:19 +0200479 if (altsd->bNumEndpoints < 2)
480 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200481
Takashi Iwaif6581c02020-11-23 09:53:14 +0100482 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500483 if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
484 attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
Eldad Zackf34d0652013-08-03 10:50:19 +0200485 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
486 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200487
Takashi Iwaif6581c02020-11-23 09:53:14 +0100488 sync_attr = get_endpoint(alts, 1)->bmAttributes;
489
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500490 /*
491 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
492 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
493 * error fall back to SYNC mode and don't create sync endpoint
494 */
495
Eldad Zackf34d0652013-08-03 10:50:19 +0200496 /* check sync-pipe endpoint */
497 /* ... and check descriptor size before accessing bSynchAddress
498 because there is a version of the SB Audigy 2 NX firmware lacking
499 the audio fields in the endpoint descriptors */
Takashi Iwaif6581c02020-11-23 09:53:14 +0100500 if ((sync_attr & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
Eldad Zackf34d0652013-08-03 10:50:19 +0200501 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zack95fec882013-08-03 10:50:20 +0200502 get_endpoint(alts, 1)->bSynchAddress != 0)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100503 dev_err(&dev->dev,
504 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
505 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200506 get_endpoint(alts, 1)->bmAttributes,
507 get_endpoint(alts, 1)->bLength,
508 get_endpoint(alts, 1)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500509 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
510 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200511 return -EINVAL;
Daniel Mackedcd3632012-04-12 13:51:12 +0200512 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200513 ep = get_endpoint(alts, 1)->bEndpointAddress;
Eldad Zack95fec882013-08-03 10:50:20 +0200514 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Ard van Breemen1b341212019-08-02 13:52:14 +0200515 get_endpoint(alts, 0)->bSynchAddress != 0 &&
Eldad Zackf34d0652013-08-03 10:50:19 +0200516 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
517 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100518 dev_err(&dev->dev,
519 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
520 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200521 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500522 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
523 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200524 return -EINVAL;
525 }
526
Takashi Iwaif6581c02020-11-23 09:53:14 +0100527 fmt->sync_ep = ep;
528 fmt->sync_iface = altsd->bInterfaceNumber;
529 fmt->sync_altsetting = altsd->bAlternateSetting;
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100530 fmt->sync_ep_idx = 1;
Takashi Iwaif6581c02020-11-23 09:53:14 +0100531 if ((sync_attr & USB_ENDPOINT_USAGE_MASK) == USB_ENDPOINT_USAGE_IMPLICIT_FB)
532 fmt->implicit_fb = 1;
533
534 dev_dbg(&dev->dev, "%d:%d: found sync_ep=0x%x, iface=%d, alt=%d, implicit_fb=%d\n",
535 fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface,
536 fmt->sync_altsetting, fmt->implicit_fb);
537
538 return 0;
539}
540
Daniel Macke5779992010-03-04 19:46:13 +0100541/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100542 * Return the score of matching two audioformats.
543 * Veto the audioformat if:
544 * - It has no channels for some reason.
545 * - Requested PCM format is not supported.
546 * - Requested sample rate is not supported.
547 */
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100548static int match_endpoint_audioformats(struct snd_usb_substream *subs,
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100549 const struct audioformat *fp,
550 int rate, int channels,
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100551 snd_pcm_format_t pcm_format)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100552{
Takashi Iwai61cc2d72020-11-23 09:53:30 +0100553 int i, score;
Eldad Zack0d9741c2012-12-03 20:30:09 +0100554
Takashi Iwai61cc2d72020-11-23 09:53:30 +0100555 if (fp->channels < 1)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100556 return 0;
Eldad Zack0d9741c2012-12-03 20:30:09 +0100557
Takashi Iwai61cc2d72020-11-23 09:53:30 +0100558 if (!(fp->formats & pcm_format_to_bits(pcm_format)))
Eldad Zack0d9741c2012-12-03 20:30:09 +0100559 return 0;
Eldad Zack0d9741c2012-12-03 20:30:09 +0100560
Takashi Iwai61cc2d72020-11-23 09:53:30 +0100561 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
562 if (rate < fp->rate_min || rate > fp->rate_max)
563 return 0;
564 } else {
565 for (i = 0; i < fp->nr_rates; i++) {
566 if (fp->rate_table[i] == rate)
567 break;
Eldad Zack0d9741c2012-12-03 20:30:09 +0100568 }
Takashi Iwai61cc2d72020-11-23 09:53:30 +0100569 if (i >= fp->nr_rates)
570 return 0;
Eldad Zack0d9741c2012-12-03 20:30:09 +0100571 }
572
Takashi Iwai61cc2d72020-11-23 09:53:30 +0100573 score = 1;
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100574 if (fp->channels == channels)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100575 score++;
576
Eldad Zack0d9741c2012-12-03 20:30:09 +0100577 return score;
578}
579
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100580static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state)
581{
582 int ret;
583
584 if (!subs->str_pd)
585 return 0;
586
587 ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
588 if (ret < 0) {
589 dev_err(&subs->dev->dev,
590 "Cannot change Power Domain ID: %d to state: %d. Err: %d\n",
591 subs->str_pd->pd_id, state, ret);
592 return ret;
593 }
594
595 return 0;
596}
597
598int snd_usb_pcm_suspend(struct snd_usb_stream *as)
599{
600 int ret;
601
602 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2);
603 if (ret < 0)
604 return ret;
605
606 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2);
607 if (ret < 0)
608 return ret;
609
610 return 0;
611}
612
613int snd_usb_pcm_resume(struct snd_usb_stream *as)
614{
615 int ret;
616
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100617 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1);
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100618 if (ret < 0)
619 return ret;
620
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100621 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1);
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100622 if (ret < 0)
623 return ret;
624
625 return 0;
626}
627
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100628static struct snd_usb_substream *
629find_matching_substream(struct snd_usb_audio *chip, int stream, int ep_num,
630 int fmt_type)
631{
632 struct snd_usb_stream *as;
633 struct snd_usb_substream *subs;
634
635 list_for_each_entry(as, &chip->pcm_list, list) {
636 subs = &as->substream[stream];
637 if (as->fmt_type == fmt_type && subs->ep_num == ep_num)
638 return subs;
639 }
640
641 return NULL;
642}
643
Takashi Iwaicab941b2020-11-23 09:53:33 +0100644static const struct audioformat *
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100645find_implicit_fb_sync_format(struct snd_usb_audio *chip,
646 const struct audioformat *target,
647 const struct snd_pcm_hw_params *params,
648 int stream)
649{
650 struct snd_usb_substream *subs;
Takashi Iwaicab941b2020-11-23 09:53:33 +0100651 const struct audioformat *fp, *sync_fmt;
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100652 int score, high_score;
653
654 subs = find_matching_substream(chip, stream, target->sync_ep,
655 target->fmt_type);
656 if (!subs)
657 return NULL;
658
659 sync_fmt = NULL;
660 high_score = 0;
661 list_for_each_entry(fp, &subs->fmt_list, list) {
662 score = match_endpoint_audioformats(subs, fp,
663 params_rate(params),
664 params_channels(params),
665 params_format(params));
666 if (score > high_score) {
667 sync_fmt = fp;
668 high_score = score;
669 }
670 }
671
672 return sync_fmt;
673}
674
675static void close_endpoints(struct snd_usb_audio *chip,
676 struct snd_usb_substream *subs)
677{
678 if (subs->data_endpoint) {
679 snd_usb_endpoint_set_sync(chip, subs->data_endpoint, NULL);
680 snd_usb_endpoint_close(chip, subs->data_endpoint);
681 subs->data_endpoint = NULL;
682 }
683
684 if (subs->sync_endpoint) {
685 snd_usb_endpoint_close(chip, subs->sync_endpoint);
686 subs->sync_endpoint = NULL;
687 }
688}
689
690static int configure_endpoints(struct snd_usb_audio *chip,
691 struct snd_usb_substream *subs)
692{
693 int err;
694
695 if (subs->data_endpoint->need_setup) {
696 /* stop any running stream beforehand */
697 if (stop_endpoints(subs))
698 sync_pending_stops(subs);
699 err = snd_usb_endpoint_configure(chip, subs->data_endpoint);
700 if (err < 0)
701 return err;
702 snd_usb_set_format_quirk(subs, subs->cur_audiofmt);
703 }
704
705 if (subs->sync_endpoint) {
706 err = snd_usb_endpoint_configure(chip, subs->sync_endpoint);
707 if (err < 0)
708 return err;
709 }
710
711 return 0;
712}
713
Dylan Reid61a70952012-09-18 09:49:48 -0700714/*
Daniel Macke5779992010-03-04 19:46:13 +0100715 * hw_params callback
716 *
717 * allocate a buffer and set the given audio format.
718 *
719 * so far we use a physically linear buffer although packetize transfer
720 * doesn't need a continuous area.
721 * if sg buffer is supported on the later version of alsa, we'll follow
722 * that.
723 */
724static int snd_usb_hw_params(struct snd_pcm_substream *substream,
725 struct snd_pcm_hw_params *hw_params)
726{
727 struct snd_usb_substream *subs = substream->runtime->private_data;
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100728 struct snd_usb_audio *chip = subs->stream->chip;
Takashi Iwaicab941b2020-11-23 09:53:33 +0100729 const struct audioformat *fmt;
730 const struct audioformat *sync_fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700731 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100732
Shuah Khan66354f12019-04-01 20:40:22 -0400733 ret = snd_media_start_pipeline(subs);
734 if (ret)
735 return ret;
736
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100737 fmt = find_substream_format(subs, hw_params);
738 if (!fmt) {
739 usb_audio_dbg(chip,
740 "cannot find format: format=%s, rate=%d, channels=%d\n",
741 snd_pcm_format_name(params_format(hw_params)),
742 params_rate(hw_params), params_channels(hw_params));
743 ret = -EINVAL;
744 goto stop_pipeline;
745 }
746
747 if (fmt->implicit_fb &&
748 (fmt->iface != fmt->sync_iface ||
749 fmt->altsetting != fmt->sync_altsetting)) {
750 sync_fmt = find_implicit_fb_sync_format(chip, fmt, hw_params,
751 !substream->stream);
752 if (!sync_fmt) {
753 usb_audio_dbg(chip,
754 "cannot find sync format: ep=0x%x, iface=%d:%d, format=%s, rate=%d, channels=%d\n",
755 fmt->sync_ep, fmt->sync_iface,
756 fmt->sync_altsetting,
757 snd_pcm_format_name(params_format(hw_params)),
758 params_rate(hw_params), params_channels(hw_params));
759 ret = -EINVAL;
760 goto stop_pipeline;
761 }
762 } else {
763 sync_fmt = fmt;
764 }
765
766 ret = snd_usb_lock_shutdown(chip);
767 if (ret < 0)
768 goto stop_pipeline;
769
770 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
771 if (ret < 0)
772 goto unlock;
773
774 if (subs->data_endpoint) {
775 if (snd_usb_endpoint_compatible(chip, subs->data_endpoint,
776 fmt, hw_params))
777 goto unlock;
778 close_endpoints(chip, subs);
779 }
780
781 subs->data_endpoint = snd_usb_endpoint_open(chip, fmt, hw_params, false);
782 if (!subs->data_endpoint) {
783 ret = -EINVAL;
784 goto unlock;
785 }
786
787 if (fmt->sync_ep) {
788 subs->sync_endpoint = snd_usb_endpoint_open(chip, sync_fmt,
789 hw_params,
790 fmt == sync_fmt);
791 if (!subs->sync_endpoint) {
792 ret = -EINVAL;
793 goto unlock;
794 }
795
796 snd_usb_endpoint_set_sync(chip, subs->data_endpoint,
797 subs->sync_endpoint);
798 }
799
800 subs->interface = fmt->iface;
801 subs->altset_idx = fmt->altset_idx;
802 subs->cur_audiofmt = fmt;
803
804 ret = configure_endpoints(chip, subs);
805 if (ret < 0)
806 goto unlock;
807
Dylan Reid61a70952012-09-18 09:49:48 -0700808 subs->pcm_format = params_format(hw_params);
809 subs->period_bytes = params_period_bytes(hw_params);
Alan Stern976b6c02013-09-24 15:51:58 -0400810 subs->period_frames = params_period_size(hw_params);
811 subs->buffer_periods = params_periods(hw_params);
Dylan Reid61a70952012-09-18 09:49:48 -0700812 subs->channels = params_channels(hw_params);
813 subs->cur_rate = params_rate(hw_params);
814
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100815 unlock:
Shuah Khan66354f12019-04-01 20:40:22 -0400816 if (ret < 0)
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100817 close_endpoints(chip, subs);
Shuah Khan66354f12019-04-01 20:40:22 -0400818
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100819 snd_usb_unlock_shutdown(chip);
Shuah Khan66354f12019-04-01 20:40:22 -0400820 stop_pipeline:
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100821 if (ret < 0)
822 snd_media_stop_pipeline(subs);
823
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100824 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100825}
826
827/*
828 * hw_free callback
829 *
830 * reset the audio format and release the buffer
831 */
832static int snd_usb_hw_free(struct snd_pcm_substream *substream)
833{
834 struct snd_usb_substream *subs = substream->runtime->private_data;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100835 struct snd_usb_audio *chip = subs->stream->chip;
Daniel Macke5779992010-03-04 19:46:13 +0100836
Shuah Khan66354f12019-04-01 20:40:22 -0400837 snd_media_stop_pipeline(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100838 subs->cur_audiofmt = NULL;
839 subs->cur_rate = 0;
840 subs->period_bytes = 0;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100841 if (!snd_usb_lock_shutdown(chip)) {
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100842 if (stop_endpoints(subs))
843 sync_pending_stops(subs);
844 close_endpoints(chip, subs);
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100845 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200846 }
Takashi Iwaif274baa2018-05-27 13:01:17 +0200847
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +0100848 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100849}
850
851/*
852 * prepare callback
853 *
854 * only a few subtle things...
855 */
856static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
857{
858 struct snd_pcm_runtime *runtime = substream->runtime;
859 struct snd_usb_substream *subs = runtime->private_data;
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100860 struct snd_usb_audio *chip = subs->stream->chip;
Dylan Reid61a70952012-09-18 09:49:48 -0700861 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100862
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100863 ret = snd_usb_lock_shutdown(chip);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200864 if (ret < 0)
865 return ret;
Takashi Iwai978520b2012-10-12 15:12:55 +0200866 if (snd_BUG_ON(!subs->data_endpoint)) {
867 ret = -EIO;
868 goto unlock;
869 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200870
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100871 ret = configure_endpoints(chip, subs);
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100872 if (ret < 0)
873 goto unlock;
874
Daniel Macke5779992010-03-04 19:46:13 +0100875 /* reset the pointer */
876 subs->hwptr_done = 0;
877 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500878 subs->last_delay = 0;
879 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100880 runtime->delay = 0;
881
Daniel Mackedcd3632012-04-12 13:51:12 +0200882 /* for playback, submit the URBs now; otherwise, the first hwptr_done
883 * updates for all URBs would happen at the same time when starting */
884 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200885 ret = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +0200886
Takashi Iwai978520b2012-10-12 15:12:55 +0200887 unlock:
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100888 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200889 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100890}
891
Takashi Iwai7ec827b2020-11-23 09:53:18 +0100892/*
893 * h/w constraints
894 */
895
896#ifdef HW_CONST_DEBUG
897#define hwc_debug(fmt, args...) pr_debug(fmt, ##args)
898#else
899#define hwc_debug(fmt, args...) do { } while(0)
900#endif
901
Bhumika Goyalaaffbf72017-08-17 14:45:59 +0530902static const struct snd_pcm_hardware snd_usb_hardware =
Daniel Macke5779992010-03-04 19:46:13 +0100903{
904 .info = SNDRV_PCM_INFO_MMAP |
905 SNDRV_PCM_INFO_MMAP_VALID |
906 SNDRV_PCM_INFO_BATCH |
907 SNDRV_PCM_INFO_INTERLEAVED |
908 SNDRV_PCM_INFO_BLOCK_TRANSFER |
909 SNDRV_PCM_INFO_PAUSE,
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100910 .channels_min = 1,
911 .channels_max = 256,
Daniel Macke5779992010-03-04 19:46:13 +0100912 .buffer_bytes_max = 1024 * 1024,
913 .period_bytes_min = 64,
914 .period_bytes_max = 512 * 1024,
915 .periods_min = 2,
916 .periods_max = 1024,
917};
918
919static int hw_check_valid_format(struct snd_usb_substream *subs,
920 struct snd_pcm_hw_params *params,
Takashi Iwaicab941b2020-11-23 09:53:33 +0100921 const struct audioformat *fp)
Daniel Macke5779992010-03-04 19:46:13 +0100922{
923 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
924 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
925 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
926 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100927 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100928 unsigned int ptime;
929
930 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100931 snd_mask_none(&check_fmts);
932 check_fmts.bits[0] = (u32)fp->formats;
933 check_fmts.bits[1] = (u32)(fp->formats >> 32);
934 snd_mask_intersect(&check_fmts, fmts);
935 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100936 hwc_debug(" > check: no supported format %d\n", fp->format);
937 return 0;
938 }
939 /* check the channels */
940 if (fp->channels < ct->min || fp->channels > ct->max) {
941 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
942 return 0;
943 }
944 /* check the rate is within the range */
945 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
946 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
947 return 0;
948 }
949 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
950 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
951 return 0;
952 }
953 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +0200954 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100955 ptime = 125 * (1 << fp->datainterval);
956 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
957 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
958 return 0;
959 }
960 }
961 return 1;
962}
963
Takashi Iwai7726dce2020-11-23 09:53:17 +0100964static int apply_hw_params_minmax(struct snd_interval *it, unsigned int rmin,
965 unsigned int rmax)
Daniel Macke5779992010-03-04 19:46:13 +0100966{
Daniel Macke5779992010-03-04 19:46:13 +0100967 int changed;
Daniel Macke5779992010-03-04 19:46:13 +0100968
Takashi Iwaibc4e94a2020-11-23 09:53:07 +0100969 if (rmin > rmax) {
Daniel Macke5779992010-03-04 19:46:13 +0100970 hwc_debug(" --> get empty\n");
971 it->empty = 1;
972 return -EINVAL;
973 }
974
975 changed = 0;
976 if (it->min < rmin) {
977 it->min = rmin;
978 it->openmin = 0;
979 changed = 1;
980 }
981 if (it->max > rmax) {
982 it->max = rmax;
983 it->openmax = 0;
984 changed = 1;
985 }
986 if (snd_interval_checkempty(it)) {
987 it->empty = 1;
988 return -EINVAL;
989 }
990 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
991 return changed;
992}
993
Takashi Iwai7726dce2020-11-23 09:53:17 +0100994static int hw_rule_rate(struct snd_pcm_hw_params *params,
995 struct snd_pcm_hw_rule *rule)
996{
997 struct snd_usb_substream *subs = rule->private;
Takashi Iwaicab941b2020-11-23 09:53:33 +0100998 const struct audioformat *fp;
Takashi Iwai7726dce2020-11-23 09:53:17 +0100999 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1000 unsigned int rmin, rmax, r;
1001 int i;
1002
1003 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1004 rmin = UINT_MAX;
1005 rmax = 0;
1006 list_for_each_entry(fp, &subs->fmt_list, list) {
1007 if (!hw_check_valid_format(subs, params, fp))
1008 continue;
1009 if (fp->rate_table && fp->nr_rates) {
1010 for (i = 0; i < fp->nr_rates; i++) {
1011 r = fp->rate_table[i];
1012 if (!snd_interval_test(it, r))
1013 continue;
1014 rmin = min(rmin, r);
1015 rmax = max(rmax, r);
1016 }
1017 } else {
1018 rmin = min(rmin, fp->rate_min);
1019 rmax = max(rmax, fp->rate_max);
1020 }
1021 }
1022
1023 return apply_hw_params_minmax(it, rmin, rmax);
1024}
1025
Daniel Macke5779992010-03-04 19:46:13 +01001026
1027static int hw_rule_channels(struct snd_pcm_hw_params *params,
1028 struct snd_pcm_hw_rule *rule)
1029{
1030 struct snd_usb_substream *subs = rule->private;
Takashi Iwaicab941b2020-11-23 09:53:33 +01001031 const struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001032 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1033 unsigned int rmin, rmax;
Daniel Macke5779992010-03-04 19:46:13 +01001034
1035 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
Takashi Iwai7726dce2020-11-23 09:53:17 +01001036 rmin = UINT_MAX;
1037 rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001038 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001039 if (!hw_check_valid_format(subs, params, fp))
1040 continue;
Takashi Iwai7726dce2020-11-23 09:53:17 +01001041 rmin = min(rmin, fp->channels);
1042 rmax = max(rmax, fp->channels);
Daniel Macke5779992010-03-04 19:46:13 +01001043 }
1044
Takashi Iwai7726dce2020-11-23 09:53:17 +01001045 return apply_hw_params_minmax(it, rmin, rmax);
Daniel Macke5779992010-03-04 19:46:13 +01001046}
1047
1048static int hw_rule_format(struct snd_pcm_hw_params *params,
1049 struct snd_pcm_hw_rule *rule)
1050{
1051 struct snd_usb_substream *subs = rule->private;
Takashi Iwaicab941b2020-11-23 09:53:33 +01001052 const struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001053 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1054 u64 fbits;
1055 u32 oldbits[2];
1056 int changed;
1057
1058 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1059 fbits = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001060 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001061 if (!hw_check_valid_format(subs, params, fp))
1062 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001063 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +01001064 }
1065
1066 oldbits[0] = fmt->bits[0];
1067 oldbits[1] = fmt->bits[1];
1068 fmt->bits[0] &= (u32)fbits;
1069 fmt->bits[1] &= (u32)(fbits >> 32);
1070 if (!fmt->bits[0] && !fmt->bits[1]) {
1071 hwc_debug(" --> get empty\n");
1072 return -EINVAL;
1073 }
1074 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1075 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1076 return changed;
1077}
1078
1079static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1080 struct snd_pcm_hw_rule *rule)
1081{
1082 struct snd_usb_substream *subs = rule->private;
Takashi Iwaicab941b2020-11-23 09:53:33 +01001083 const struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001084 struct snd_interval *it;
1085 unsigned char min_datainterval;
1086 unsigned int pmin;
Daniel Macke5779992010-03-04 19:46:13 +01001087
1088 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1089 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1090 min_datainterval = 0xff;
1091 list_for_each_entry(fp, &subs->fmt_list, list) {
1092 if (!hw_check_valid_format(subs, params, fp))
1093 continue;
1094 min_datainterval = min(min_datainterval, fp->datainterval);
1095 }
1096 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +02001097 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +01001098 it->empty = 1;
1099 return -EINVAL;
1100 }
1101 pmin = 125 * (1 << min_datainterval);
Takashi Iwai7726dce2020-11-23 09:53:17 +01001102
1103 return apply_hw_params_minmax(it, pmin, UINT_MAX);
Daniel Macke5779992010-03-04 19:46:13 +01001104}
1105
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001106/* apply PCM hw constraints from the concurrent sync EP */
1107static int apply_hw_constraint_from_sync(struct snd_pcm_runtime *runtime,
1108 struct snd_usb_substream *subs)
1109{
1110 struct snd_usb_audio *chip = subs->stream->chip;
1111 struct snd_usb_endpoint *ep;
Takashi Iwaicab941b2020-11-23 09:53:33 +01001112 const struct audioformat *fp;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001113 int err;
1114
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001115 list_for_each_entry(fp, &subs->fmt_list, list) {
Takashi Iwai54cb3192020-11-23 09:53:20 +01001116 ep = snd_usb_get_endpoint(chip, fp->endpoint);
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001117 if (ep && ep->cur_rate)
1118 goto found;
1119 if (!fp->implicit_fb)
1120 continue;
1121 /* for the implicit fb, check the sync ep as well */
Takashi Iwai54cb3192020-11-23 09:53:20 +01001122 ep = snd_usb_get_endpoint(chip, fp->sync_ep);
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001123 if (ep && ep->cur_rate)
1124 goto found;
1125 }
1126 return 0;
1127
1128 found:
1129 if (!find_format(&subs->fmt_list, ep->cur_format, ep->cur_rate,
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001130 ep->cur_channels, false, NULL)) {
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001131 usb_audio_dbg(chip, "EP 0x%x being used, but not applicable\n",
1132 ep->ep_num);
1133 return 0;
1134 }
1135
1136 usb_audio_dbg(chip, "EP 0x%x being used, using fixed params:\n",
1137 ep->ep_num);
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001138 usb_audio_dbg(chip, "rate=%d, period_size=%d, periods=%d\n",
1139 ep->cur_rate, ep->cur_period_frames,
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001140 ep->cur_buffer_periods);
1141
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001142 runtime->hw.formats = subs->formats;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001143 runtime->hw.rate_min = runtime->hw.rate_max = ep->cur_rate;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001144 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1145 runtime->hw.periods_min = runtime->hw.periods_max =
1146 ep->cur_buffer_periods;
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001147
1148 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1149 hw_rule_channels, subs,
1150 SNDRV_PCM_HW_PARAM_FORMAT,
1151 SNDRV_PCM_HW_PARAM_RATE,
1152 -1);
1153 if (err < 0)
1154 return err;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001155
1156 err = snd_pcm_hw_constraint_minmax(runtime,
1157 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1158 ep->cur_period_frames,
1159 ep->cur_period_frames);
1160 if (err < 0)
1161 return err;
1162
1163 return 1; /* notify the finding */
1164}
Daniel Macke5779992010-03-04 19:46:13 +01001165
1166/*
1167 * set up the runtime hardware information.
1168 */
1169
1170static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1171{
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001172 struct snd_usb_audio *chip = subs->stream->chip;
Takashi Iwaicab941b2020-11-23 09:53:33 +01001173 const struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001174 unsigned int pt, ptmin;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001175 int param_period_time_if_needed = -1;
Daniel Macke5779992010-03-04 19:46:13 +01001176 int err;
1177
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001178 mutex_lock(&chip->mutex);
1179 err = apply_hw_constraint_from_sync(runtime, subs);
1180 mutex_unlock(&chip->mutex);
1181 if (err < 0)
1182 return err;
1183 if (err > 0) /* found the matching? */
1184 goto add_extra_rules;
1185
Daniel Macke5779992010-03-04 19:46:13 +01001186 runtime->hw.formats = subs->formats;
1187
1188 runtime->hw.rate_min = 0x7fffffff;
1189 runtime->hw.rate_max = 0;
1190 runtime->hw.channels_min = 256;
1191 runtime->hw.channels_max = 0;
1192 runtime->hw.rates = 0;
1193 ptmin = UINT_MAX;
1194 /* check min/max rates and channels */
Eldad Zack88766f02013-04-03 23:18:49 +02001195 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001196 runtime->hw.rates |= fp->rates;
1197 if (runtime->hw.rate_min > fp->rate_min)
1198 runtime->hw.rate_min = fp->rate_min;
1199 if (runtime->hw.rate_max < fp->rate_max)
1200 runtime->hw.rate_max = fp->rate_max;
1201 if (runtime->hw.channels_min > fp->channels)
1202 runtime->hw.channels_min = fp->channels;
1203 if (runtime->hw.channels_max < fp->channels)
1204 runtime->hw.channels_max = fp->channels;
1205 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1206 /* FIXME: there might be more than one audio formats... */
1207 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1208 fp->frame_size;
1209 }
1210 pt = 125 * (1 << fp->datainterval);
1211 ptmin = min(ptmin, pt);
1212 }
1213
1214 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001215 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001216 /* full speed devices have fixed data packet interval */
1217 ptmin = 1000;
1218 if (ptmin == 1000)
1219 /* if period time doesn't go below 1 ms, no rules needed */
1220 param_period_time_if_needed = -1;
Daniel Macke5779992010-03-04 19:46:13 +01001221
Takashi Iwaie92be812018-05-27 15:09:15 +02001222 err = snd_pcm_hw_constraint_minmax(runtime,
1223 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1224 ptmin, UINT_MAX);
1225 if (err < 0)
1226 return err;
1227
1228 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1229 hw_rule_rate, subs,
1230 SNDRV_PCM_HW_PARAM_FORMAT,
1231 SNDRV_PCM_HW_PARAM_CHANNELS,
1232 param_period_time_if_needed,
1233 -1);
1234 if (err < 0)
1235 return err;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001236
1237add_extra_rules:
Takashi Iwaie92be812018-05-27 15:09:15 +02001238 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1239 hw_rule_channels, subs,
1240 SNDRV_PCM_HW_PARAM_FORMAT,
1241 SNDRV_PCM_HW_PARAM_RATE,
1242 param_period_time_if_needed,
1243 -1);
1244 if (err < 0)
1245 return err;
1246 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1247 hw_rule_format, subs,
1248 SNDRV_PCM_HW_PARAM_RATE,
1249 SNDRV_PCM_HW_PARAM_CHANNELS,
1250 param_period_time_if_needed,
1251 -1);
1252 if (err < 0)
1253 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001254 if (param_period_time_if_needed >= 0) {
1255 err = snd_pcm_hw_rule_add(runtime, 0,
1256 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1257 hw_rule_period_time, subs,
1258 SNDRV_PCM_HW_PARAM_FORMAT,
1259 SNDRV_PCM_HW_PARAM_CHANNELS,
1260 SNDRV_PCM_HW_PARAM_RATE,
1261 -1);
1262 if (err < 0)
Takashi Iwaie92be812018-05-27 15:09:15 +02001263 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001264 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001265
Takashi Iwai18652112020-11-23 09:53:15 +01001266 return 0;
Daniel Macke5779992010-03-04 19:46:13 +01001267}
1268
Takashi Iwai6fddc792018-05-27 13:59:03 +02001269static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
Daniel Macke5779992010-03-04 19:46:13 +01001270{
Takashi Iwai6fddc792018-05-27 13:59:03 +02001271 int direction = substream->stream;
Daniel Macke5779992010-03-04 19:46:13 +01001272 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1273 struct snd_pcm_runtime *runtime = substream->runtime;
1274 struct snd_usb_substream *subs = &as->substream[direction];
Shuah Khan66354f12019-04-01 20:40:22 -04001275 int ret;
Daniel Macke5779992010-03-04 19:46:13 +01001276
1277 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001278 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001279 runtime->hw = snd_usb_hardware;
1280 runtime->private_data = subs;
1281 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001282 /* runtime PM is also done there */
Daniel Mackd24f5062013-04-17 00:01:38 +08001283
1284 /* initialize DSD/DOP context */
1285 subs->dsd_dop.byte_idx = 0;
1286 subs->dsd_dop.channel = 0;
1287 subs->dsd_dop.marker = 1;
1288
Shuah Khan66354f12019-04-01 20:40:22 -04001289 ret = setup_hw_info(runtime, subs);
Takashi Iwai18652112020-11-23 09:53:15 +01001290 if (ret < 0)
1291 return ret;
1292 ret = snd_usb_autoresume(subs->stream->chip);
1293 if (ret < 0)
1294 return ret;
1295 ret = snd_media_stream_init(subs, as->pcm, direction);
1296 if (ret < 0)
1297 snd_usb_autosuspend(subs->stream->chip);
Shuah Khan66354f12019-04-01 20:40:22 -04001298 return ret;
Daniel Macke5779992010-03-04 19:46:13 +01001299}
1300
Takashi Iwai6fddc792018-05-27 13:59:03 +02001301static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
Daniel Macke5779992010-03-04 19:46:13 +01001302{
Takashi Iwai6fddc792018-05-27 13:59:03 +02001303 int direction = substream->stream;
Daniel Macke5779992010-03-04 19:46:13 +01001304 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1305 struct snd_usb_substream *subs = &as->substream[direction];
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001306 int ret;
Daniel Macke5779992010-03-04 19:46:13 +01001307
Shuah Khan66354f12019-04-01 20:40:22 -04001308 snd_media_stop_pipeline(subs);
Daniel Mack68e67f42012-07-12 13:08:40 +02001309
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001310 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
Daniel Mack68e67f42012-07-12 13:08:40 +02001311 subs->interface = -1;
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001312 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
Takashi Iwai47ab1542015-08-25 16:09:00 +02001313 snd_usb_unlock_shutdown(subs->stream->chip);
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001314 if (ret < 0)
1315 return ret;
Daniel Mack68e67f42012-07-12 13:08:40 +02001316 }
1317
Daniel Macke5779992010-03-04 19:46:13 +01001318 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001319 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001320
Daniel Mack68e67f42012-07-12 13:08:40 +02001321 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001322}
1323
1324/* Since a URB can handle only a single linear buffer, we must use double
1325 * buffering when the data to be transferred overflows the buffer boundary.
1326 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1327 * for all URBs.
1328 */
1329static void retire_capture_urb(struct snd_usb_substream *subs,
1330 struct urb *urb)
1331{
1332 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1333 unsigned int stride, frames, bytes, oldptr;
1334 int i, period_elapsed = 0;
1335 unsigned long flags;
1336 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001337 int current_frame_number;
1338
1339 /* read frame number here, update pointer in critical section */
1340 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001341
1342 stride = runtime->frame_bits >> 3;
1343
1344 for (i = 0; i < urb->number_of_packets; i++) {
Calvin Owens1539d4f2013-04-12 22:33:59 -05001345 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
Daniel Mackedcd3632012-04-12 13:51:12 +02001346 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001347 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1348 i, urb->iso_frame_desc[i].status);
Daniel Mackedcd3632012-04-12 13:51:12 +02001349 // continue;
1350 }
1351 bytes = urb->iso_frame_desc[i].actual_length;
Hector Martin1b7ecc22020-08-10 17:24:00 +09001352 if (subs->stream_offset_adj > 0) {
1353 unsigned int adj = min(subs->stream_offset_adj, bytes);
1354 cp += adj;
1355 bytes -= adj;
1356 subs->stream_offset_adj -= adj;
1357 }
Daniel Mackedcd3632012-04-12 13:51:12 +02001358 frames = bytes / stride;
1359 if (!subs->txfr_quirk)
1360 bytes = frames * stride;
1361 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001362 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001363 bytes = frames * stride;
Takashi Iwai377a8792018-05-16 20:07:18 +02001364 dev_warn_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001365 "Corrected urb data len. %d->%d\n",
Daniel Mackedcd3632012-04-12 13:51:12 +02001366 oldbytes, bytes);
1367 }
1368 /* update the current pointer */
1369 spin_lock_irqsave(&subs->lock, flags);
1370 oldptr = subs->hwptr_done;
1371 subs->hwptr_done += bytes;
1372 if (subs->hwptr_done >= runtime->buffer_size * stride)
1373 subs->hwptr_done -= runtime->buffer_size * stride;
1374 frames = (bytes + (oldptr % stride)) / stride;
1375 subs->transfer_done += frames;
1376 if (subs->transfer_done >= runtime->period_size) {
1377 subs->transfer_done -= runtime->period_size;
1378 period_elapsed = 1;
1379 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001380 /* capture delay is by construction limited to one URB,
1381 * reset delays here
1382 */
1383 runtime->delay = subs->last_delay = 0;
1384
1385 /* realign last_frame_number */
1386 subs->last_frame_number = current_frame_number;
1387 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1388
Daniel Mackedcd3632012-04-12 13:51:12 +02001389 spin_unlock_irqrestore(&subs->lock, flags);
1390 /* copy a data chunk */
1391 if (oldptr + bytes > runtime->buffer_size * stride) {
1392 unsigned int bytes1 =
1393 runtime->buffer_size * stride - oldptr;
1394 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1395 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1396 } else {
1397 memcpy(runtime->dma_area + oldptr, cp, bytes);
1398 }
1399 }
1400
1401 if (period_elapsed)
1402 snd_pcm_period_elapsed(subs->pcm_substream);
1403}
1404
Daniel Mackd24f5062013-04-17 00:01:38 +08001405static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1406 struct urb *urb, unsigned int bytes)
1407{
1408 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1409 unsigned int stride = runtime->frame_bits >> 3;
1410 unsigned int dst_idx = 0;
1411 unsigned int src_idx = subs->hwptr_done;
1412 unsigned int wrap = runtime->buffer_size * stride;
1413 u8 *dst = urb->transfer_buffer;
1414 u8 *src = runtime->dma_area;
1415 u8 marker[] = { 0x05, 0xfa };
1416
1417 /*
1418 * The DSP DOP format defines a way to transport DSD samples over
1419 * normal PCM data endpoints. It requires stuffing of marker bytes
1420 * (0x05 and 0xfa, alternating per sample frame), and then expects
1421 * 2 additional bytes of actual payload. The whole frame is stored
1422 * LSB.
1423 *
1424 * Hence, for a stereo transport, the buffer layout looks like this,
1425 * where L refers to left channel samples and R to right.
1426 *
1427 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1428 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1429 * .....
1430 *
1431 */
1432
1433 while (bytes--) {
1434 if (++subs->dsd_dop.byte_idx == 3) {
1435 /* frame boundary? */
1436 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1437 src_idx += 2;
1438 subs->dsd_dop.byte_idx = 0;
1439
1440 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1441 /* alternate the marker */
1442 subs->dsd_dop.marker++;
1443 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1444 subs->dsd_dop.channel = 0;
1445 }
1446 } else {
1447 /* stuff the DSD payload */
1448 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001449
1450 if (subs->cur_audiofmt->dsd_bitrev)
1451 dst[dst_idx++] = bitrev8(src[idx]);
1452 else
1453 dst[dst_idx++] = src[idx];
1454
Daniel Mackd24f5062013-04-17 00:01:38 +08001455 subs->hwptr_done++;
1456 }
1457 }
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001458 if (subs->hwptr_done >= runtime->buffer_size * stride)
1459 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001460}
1461
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001462static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1463 int offset, int stride, unsigned int bytes)
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001464{
1465 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1466
1467 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1468 /* err, the transferred area goes over buffer boundary. */
1469 unsigned int bytes1 =
1470 runtime->buffer_size * stride - subs->hwptr_done;
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001471 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001472 runtime->dma_area + subs->hwptr_done, bytes1);
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001473 memcpy(urb->transfer_buffer + offset + bytes1,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001474 runtime->dma_area, bytes - bytes1);
1475 } else {
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001476 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001477 runtime->dma_area + subs->hwptr_done, bytes);
1478 }
1479 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001480 if (subs->hwptr_done >= runtime->buffer_size * stride)
1481 subs->hwptr_done -= runtime->buffer_size * stride;
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001482}
1483
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001484static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1485 struct urb *urb, int stride,
1486 unsigned int bytes)
1487{
1488 __le32 packet_length;
1489 int i;
1490
1491 /* Put __le32 length descriptor at start of each packet. */
1492 for (i = 0; i < urb->number_of_packets; i++) {
1493 unsigned int length = urb->iso_frame_desc[i].length;
1494 unsigned int offset = urb->iso_frame_desc[i].offset;
1495
1496 packet_length = cpu_to_le32(length);
1497 offset += i * sizeof(packet_length);
1498 urb->iso_frame_desc[i].offset = offset;
1499 urb->iso_frame_desc[i].length += sizeof(packet_length);
1500 memcpy(urb->transfer_buffer + offset,
1501 &packet_length, sizeof(packet_length));
1502 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1503 stride, length);
1504 }
1505 /* Adjust transfer size accordingly. */
1506 bytes += urb->number_of_packets * sizeof(packet_length);
1507 return bytes;
1508}
1509
Daniel Mackedcd3632012-04-12 13:51:12 +02001510static void prepare_playback_urb(struct snd_usb_substream *subs,
1511 struct urb *urb)
1512{
1513 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001514 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001515 struct snd_urb_ctx *ctx = urb->context;
1516 unsigned int counts, frames, bytes;
1517 int i, stride, period_elapsed = 0;
1518 unsigned long flags;
1519
1520 stride = runtime->frame_bits >> 3;
1521
1522 frames = 0;
1523 urb->number_of_packets = 0;
1524 spin_lock_irqsave(&subs->lock, flags);
Alan Stern976b6c02013-09-24 15:51:58 -04001525 subs->frame_limit += ep->max_urb_frames;
Daniel Mackedcd3632012-04-12 13:51:12 +02001526 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001527 if (ctx->packet_size[i])
1528 counts = ctx->packet_size[i];
Alexander Tsoyf0bd62b2020-04-24 05:24:48 +03001529 else if (ep->sync_master)
1530 counts = snd_usb_endpoint_slave_next_packet_size(ep);
Daniel Mack245baf92012-08-30 18:52:30 +02001531 else
1532 counts = snd_usb_endpoint_next_packet_size(ep);
1533
Daniel Mackedcd3632012-04-12 13:51:12 +02001534 /* set up descriptor */
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001535 urb->iso_frame_desc[i].offset = frames * ep->stride;
1536 urb->iso_frame_desc[i].length = counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001537 frames += counts;
1538 urb->number_of_packets++;
1539 subs->transfer_done += counts;
1540 if (subs->transfer_done >= runtime->period_size) {
1541 subs->transfer_done -= runtime->period_size;
Alan Stern976b6c02013-09-24 15:51:58 -04001542 subs->frame_limit = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001543 period_elapsed = 1;
1544 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1545 if (subs->transfer_done > 0) {
1546 /* FIXME: fill-max mode is not
1547 * supported yet */
1548 frames -= subs->transfer_done;
1549 counts -= subs->transfer_done;
1550 urb->iso_frame_desc[i].length =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001551 counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001552 subs->transfer_done = 0;
1553 }
1554 i++;
1555 if (i < ctx->packets) {
1556 /* add a transfer delimiter */
1557 urb->iso_frame_desc[i].offset =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001558 frames * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001559 urb->iso_frame_desc[i].length = 0;
1560 urb->number_of_packets++;
1561 }
1562 break;
1563 }
1564 }
Alan Stern976b6c02013-09-24 15:51:58 -04001565 /* finish at the period boundary or after enough frames */
1566 if ((period_elapsed ||
1567 subs->transfer_done >= subs->frame_limit) &&
1568 !snd_usb_endpoint_implicit_feedback_sink(ep))
Daniel Mackedcd3632012-04-12 13:51:12 +02001569 break;
1570 }
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001571 bytes = frames * ep->stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001572
1573 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1574 subs->cur_audiofmt->dsd_dop)) {
1575 fill_playback_urb_dsd_dop(subs, urb, bytes);
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001576 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1577 subs->cur_audiofmt->dsd_bitrev)) {
1578 /* bit-reverse the bytes */
1579 u8 *buf = urb->transfer_buffer;
1580 for (i = 0; i < bytes; i++) {
1581 int idx = (subs->hwptr_done + i)
1582 % (runtime->buffer_size * stride);
1583 buf[i] = bitrev8(runtime->dma_area[idx]);
1584 }
1585
1586 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001587 if (subs->hwptr_done >= runtime->buffer_size * stride)
1588 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001589 } else {
Daniel Mackd24f5062013-04-17 00:01:38 +08001590 /* usual PCM */
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001591 if (!subs->tx_length_quirk)
1592 copy_to_urb(subs, urb, 0, stride, bytes);
1593 else
1594 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1595 /* bytes is now amount of outgoing data */
Daniel Mackedcd3632012-04-12 13:51:12 +02001596 }
Daniel Mackd24f5062013-04-17 00:01:38 +08001597
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001598 /* update delay with exact number of samples queued */
1599 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001600 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001601 subs->last_delay = runtime->delay;
1602
1603 /* realign last_frame_number */
1604 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1605 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1606
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001607 if (subs->trigger_tstamp_pending_update) {
1608 /* this is the first actual URB submitted,
1609 * update trigger timestamp to reflect actual start time
1610 */
1611 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1612 subs->trigger_tstamp_pending_update = false;
1613 }
1614
Daniel Mackedcd3632012-04-12 13:51:12 +02001615 spin_unlock_irqrestore(&subs->lock, flags);
1616 urb->transfer_buffer_length = bytes;
1617 if (period_elapsed)
1618 snd_pcm_period_elapsed(subs->pcm_substream);
1619}
1620
1621/*
1622 * process after playback data complete
1623 * - decrease the delay count again
1624 */
1625static void retire_playback_urb(struct snd_usb_substream *subs,
1626 struct urb *urb)
1627{
1628 unsigned long flags;
1629 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001630 struct snd_usb_endpoint *ep = subs->data_endpoint;
1631 int processed = urb->transfer_buffer_length / ep->stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001632 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001633
Alexander Tsoy5ff40e62020-06-29 06:26:07 +03001634 /* ignore the delay accounting when processed=0 is given, i.e.
1635 * silent payloads are processed before handling the actual data
Takashi Iwai1213a202012-09-06 14:58:00 +02001636 */
1637 if (!processed)
1638 return;
1639
Daniel Mackedcd3632012-04-12 13:51:12 +02001640 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001641 if (!subs->last_delay)
1642 goto out; /* short path */
1643
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001644 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1645 /* update delay with exact number of samples played */
1646 if (processed > subs->last_delay)
1647 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001648 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001649 subs->last_delay -= processed;
1650 runtime->delay = subs->last_delay;
1651
1652 /*
1653 * Report when delay estimate is off by more than 2ms.
1654 * The error should be lower than 2ms since the estimate relies
1655 * on two reads of a counter updated every ms.
1656 */
Sander Eikelenboomb7a77232014-05-02 15:09:27 +02001657 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1658 dev_dbg_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001659 "delay: estimated %d, actual %d\n",
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001660 est_delay, subs->last_delay);
1661
Takashi Iwai48779a02012-11-23 16:00:37 +01001662 if (!subs->running) {
1663 /* update last_frame_number for delay counting here since
1664 * prepare_playback_urb won't be called during pause
1665 */
1666 subs->last_frame_number =
1667 usb_get_current_frame_number(subs->dev) & 0xff;
1668 }
1669
1670 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001671 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001672}
1673
Daniel Mackedcd3632012-04-12 13:51:12 +02001674static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1675 int cmd)
1676{
1677 struct snd_usb_substream *subs = substream->runtime->private_data;
1678
1679 switch (cmd) {
1680 case SNDRV_PCM_TRIGGER_START:
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001681 subs->trigger_tstamp_pending_update = true;
Gustavo A. R. Silvac0dbbda2020-07-08 15:32:36 -05001682 fallthrough;
Daniel Mackedcd3632012-04-12 13:51:12 +02001683 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai96e221f2020-11-23 09:53:28 +01001684 snd_usb_endpoint_set_callback(subs->data_endpoint,
1685 prepare_playback_urb,
1686 retire_playback_urb,
1687 subs);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001688 subs->running = 1;
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001689 dev_dbg(&subs->dev->dev, "%d:%d Start Playback PCM\n",
1690 subs->cur_audiofmt->iface,
1691 subs->cur_audiofmt->altsetting);
Daniel Mackedcd3632012-04-12 13:51:12 +02001692 return 0;
Takashi Iwai75c16b52020-11-23 09:53:29 +01001693 case SNDRV_PCM_TRIGGER_SUSPEND:
Daniel Mackedcd3632012-04-12 13:51:12 +02001694 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001695 stop_endpoints(subs);
Takashi Iwai96e221f2020-11-23 09:53:28 +01001696 snd_usb_endpoint_set_callback(subs->data_endpoint,
1697 NULL, NULL, NULL);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001698 subs->running = 0;
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001699 dev_dbg(&subs->dev->dev, "%d:%d Stop Playback PCM\n",
1700 subs->cur_audiofmt->iface,
1701 subs->cur_audiofmt->altsetting);
Daniel Mackedcd3632012-04-12 13:51:12 +02001702 return 0;
1703 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai48779a02012-11-23 16:00:37 +01001704 /* keep retire_data_urb for delay calculation */
Takashi Iwai96e221f2020-11-23 09:53:28 +01001705 snd_usb_endpoint_set_callback(subs->data_endpoint,
1706 NULL,
1707 retire_playback_urb,
1708 subs);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001709 subs->running = 0;
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001710 dev_dbg(&subs->dev->dev, "%d:%d Pause Playback PCM\n",
1711 subs->cur_audiofmt->iface,
1712 subs->cur_audiofmt->altsetting);
Daniel Mackedcd3632012-04-12 13:51:12 +02001713 return 0;
1714 }
1715
1716 return -EINVAL;
1717}
1718
Daniel Mackafe25962012-06-16 16:58:04 +02001719static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1720 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001721{
1722 int err;
1723 struct snd_usb_substream *subs = substream->runtime->private_data;
1724
1725 switch (cmd) {
1726 case SNDRV_PCM_TRIGGER_START:
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +02001727 err = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +02001728 if (err < 0)
1729 return err;
Takashi Iwai96e221f2020-11-23 09:53:28 +01001730 fallthrough;
1731 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1732 snd_usb_endpoint_set_callback(subs->data_endpoint,
1733 NULL, retire_capture_urb,
1734 subs);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001735 subs->running = 1;
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001736 dev_dbg(&subs->dev->dev, "%d:%d Start Capture PCM\n",
1737 subs->cur_audiofmt->iface,
1738 subs->cur_audiofmt->altsetting);
Daniel Mackedcd3632012-04-12 13:51:12 +02001739 return 0;
Takashi Iwai75c16b52020-11-23 09:53:29 +01001740 case SNDRV_PCM_TRIGGER_SUSPEND:
Daniel Mackedcd3632012-04-12 13:51:12 +02001741 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001742 stop_endpoints(subs);
Takashi Iwai96e221f2020-11-23 09:53:28 +01001743 fallthrough;
Daniel Mackedcd3632012-04-12 13:51:12 +02001744 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai96e221f2020-11-23 09:53:28 +01001745 snd_usb_endpoint_set_callback(subs->data_endpoint,
1746 NULL, NULL, NULL);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001747 subs->running = 0;
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001748 dev_dbg(&subs->dev->dev, "%d:%d Stop Capture PCM\n",
1749 subs->cur_audiofmt->iface,
1750 subs->cur_audiofmt->altsetting);
Daniel Mackedcd3632012-04-12 13:51:12 +02001751 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001752 }
1753
1754 return -EINVAL;
1755}
1756
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301757static const struct snd_pcm_ops snd_usb_playback_ops = {
Takashi Iwai6fddc792018-05-27 13:59:03 +02001758 .open = snd_usb_pcm_open,
1759 .close = snd_usb_pcm_close,
Daniel Macke5779992010-03-04 19:46:13 +01001760 .hw_params = snd_usb_hw_params,
1761 .hw_free = snd_usb_hw_free,
1762 .prepare = snd_usb_pcm_prepare,
1763 .trigger = snd_usb_substream_playback_trigger,
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001764 .sync_stop = snd_usb_pcm_sync_stop,
Daniel Macke5779992010-03-04 19:46:13 +01001765 .pointer = snd_usb_pcm_pointer,
Daniel Macke5779992010-03-04 19:46:13 +01001766};
1767
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301768static const struct snd_pcm_ops snd_usb_capture_ops = {
Takashi Iwai6fddc792018-05-27 13:59:03 +02001769 .open = snd_usb_pcm_open,
1770 .close = snd_usb_pcm_close,
Daniel Macke5779992010-03-04 19:46:13 +01001771 .hw_params = snd_usb_hw_params,
1772 .hw_free = snd_usb_hw_free,
1773 .prepare = snd_usb_pcm_prepare,
1774 .trigger = snd_usb_substream_capture_trigger,
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001775 .sync_stop = snd_usb_pcm_sync_stop,
Daniel Macke5779992010-03-04 19:46:13 +01001776 .pointer = snd_usb_pcm_pointer,
Takashi Iwaif274baa2018-05-27 13:01:17 +02001777};
1778
Daniel Macke5779992010-03-04 19:46:13 +01001779void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1780{
Takashi Iwaif274baa2018-05-27 13:01:17 +02001781 const struct snd_pcm_ops *ops;
1782
Takashi Iwaib315997d2019-11-05 16:18:40 +01001783 ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
Takashi Iwaif274baa2018-05-27 13:01:17 +02001784 &snd_usb_playback_ops : &snd_usb_capture_ops;
Takashi Iwaif274baa2018-05-27 13:01:17 +02001785 snd_pcm_set_ops(pcm, stream, ops);
1786}
1787
1788void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
1789{
1790 struct snd_pcm *pcm = subs->stream->pcm;
1791 struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
1792 struct device *dev = subs->dev->bus->controller;
1793
Takashi Iwaib315997d2019-11-05 16:18:40 +01001794 if (snd_usb_use_vmalloc)
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +01001795 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_VMALLOC,
1796 NULL, 0, 0);
Takashi Iwaib315997d2019-11-05 16:18:40 +01001797 else
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +01001798 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_DEV_SG,
1799 dev, 64*1024, 512*1024);
Daniel Macke5779992010-03-04 19:46:13 +01001800}