blob: 95a6a854dc551355c96e0b6337c864557114fa7e [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
Takashi Iwai6aa719d2020-11-23 09:53:36 +0100800 mutex_lock(&chip->mutex);
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100801 subs->cur_audiofmt = fmt;
Takashi Iwai6aa719d2020-11-23 09:53:36 +0100802 mutex_unlock(&chip->mutex);
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100803
804 ret = configure_endpoints(chip, subs);
Dylan Reid61a70952012-09-18 09:49:48 -0700805
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100806 unlock:
Shuah Khan66354f12019-04-01 20:40:22 -0400807 if (ret < 0)
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100808 close_endpoints(chip, subs);
Shuah Khan66354f12019-04-01 20:40:22 -0400809
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100810 snd_usb_unlock_shutdown(chip);
Shuah Khan66354f12019-04-01 20:40:22 -0400811 stop_pipeline:
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100812 if (ret < 0)
813 snd_media_stop_pipeline(subs);
814
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100815 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100816}
817
818/*
819 * hw_free callback
820 *
821 * reset the audio format and release the buffer
822 */
823static int snd_usb_hw_free(struct snd_pcm_substream *substream)
824{
825 struct snd_usb_substream *subs = substream->runtime->private_data;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100826 struct snd_usb_audio *chip = subs->stream->chip;
Daniel Macke5779992010-03-04 19:46:13 +0100827
Shuah Khan66354f12019-04-01 20:40:22 -0400828 snd_media_stop_pipeline(subs);
Takashi Iwai6aa719d2020-11-23 09:53:36 +0100829 mutex_lock(&chip->mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100830 subs->cur_audiofmt = NULL;
Takashi Iwai6aa719d2020-11-23 09:53:36 +0100831 mutex_unlock(&chip->mutex);
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100832 if (!snd_usb_lock_shutdown(chip)) {
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100833 if (stop_endpoints(subs))
834 sync_pending_stops(subs);
835 close_endpoints(chip, subs);
Takashi Iwai5a6c3e12020-11-23 09:53:16 +0100836 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200837 }
Takashi Iwaif274baa2018-05-27 13:01:17 +0200838
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +0100839 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100840}
841
842/*
843 * prepare callback
844 *
845 * only a few subtle things...
846 */
847static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
848{
849 struct snd_pcm_runtime *runtime = substream->runtime;
850 struct snd_usb_substream *subs = runtime->private_data;
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100851 struct snd_usb_audio *chip = subs->stream->chip;
Dylan Reid61a70952012-09-18 09:49:48 -0700852 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100853
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100854 ret = snd_usb_lock_shutdown(chip);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200855 if (ret < 0)
856 return ret;
Takashi Iwai978520b2012-10-12 15:12:55 +0200857 if (snd_BUG_ON(!subs->data_endpoint)) {
858 ret = -EIO;
859 goto unlock;
860 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200861
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100862 ret = configure_endpoints(chip, subs);
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100863 if (ret < 0)
864 goto unlock;
865
Daniel Macke5779992010-03-04 19:46:13 +0100866 /* reset the pointer */
867 subs->hwptr_done = 0;
868 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500869 subs->last_delay = 0;
870 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100871 runtime->delay = 0;
872
Daniel Mackedcd3632012-04-12 13:51:12 +0200873 /* for playback, submit the URBs now; otherwise, the first hwptr_done
874 * updates for all URBs would happen at the same time when starting */
875 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200876 ret = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +0200877
Takashi Iwai978520b2012-10-12 15:12:55 +0200878 unlock:
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100879 snd_usb_unlock_shutdown(chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200880 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100881}
882
Takashi Iwai7ec827b2020-11-23 09:53:18 +0100883/*
884 * h/w constraints
885 */
886
887#ifdef HW_CONST_DEBUG
888#define hwc_debug(fmt, args...) pr_debug(fmt, ##args)
889#else
890#define hwc_debug(fmt, args...) do { } while(0)
891#endif
892
Bhumika Goyalaaffbf72017-08-17 14:45:59 +0530893static const struct snd_pcm_hardware snd_usb_hardware =
Daniel Macke5779992010-03-04 19:46:13 +0100894{
895 .info = SNDRV_PCM_INFO_MMAP |
896 SNDRV_PCM_INFO_MMAP_VALID |
897 SNDRV_PCM_INFO_BATCH |
898 SNDRV_PCM_INFO_INTERLEAVED |
899 SNDRV_PCM_INFO_BLOCK_TRANSFER |
900 SNDRV_PCM_INFO_PAUSE,
Takashi Iwaibf6313a2020-11-23 09:53:31 +0100901 .channels_min = 1,
902 .channels_max = 256,
Daniel Macke5779992010-03-04 19:46:13 +0100903 .buffer_bytes_max = 1024 * 1024,
904 .period_bytes_min = 64,
905 .period_bytes_max = 512 * 1024,
906 .periods_min = 2,
907 .periods_max = 1024,
908};
909
910static int hw_check_valid_format(struct snd_usb_substream *subs,
911 struct snd_pcm_hw_params *params,
Takashi Iwaicab941b2020-11-23 09:53:33 +0100912 const struct audioformat *fp)
Daniel Macke5779992010-03-04 19:46:13 +0100913{
914 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
915 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
916 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
917 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100918 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100919 unsigned int ptime;
920
921 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100922 snd_mask_none(&check_fmts);
923 check_fmts.bits[0] = (u32)fp->formats;
924 check_fmts.bits[1] = (u32)(fp->formats >> 32);
925 snd_mask_intersect(&check_fmts, fmts);
926 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100927 hwc_debug(" > check: no supported format %d\n", fp->format);
928 return 0;
929 }
930 /* check the channels */
931 if (fp->channels < ct->min || fp->channels > ct->max) {
932 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
933 return 0;
934 }
935 /* check the rate is within the range */
936 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
937 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
938 return 0;
939 }
940 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
941 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
942 return 0;
943 }
944 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +0200945 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100946 ptime = 125 * (1 << fp->datainterval);
947 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
948 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
949 return 0;
950 }
951 }
952 return 1;
953}
954
Takashi Iwai7726dce2020-11-23 09:53:17 +0100955static int apply_hw_params_minmax(struct snd_interval *it, unsigned int rmin,
956 unsigned int rmax)
Daniel Macke5779992010-03-04 19:46:13 +0100957{
Daniel Macke5779992010-03-04 19:46:13 +0100958 int changed;
Daniel Macke5779992010-03-04 19:46:13 +0100959
Takashi Iwaibc4e94a2020-11-23 09:53:07 +0100960 if (rmin > rmax) {
Daniel Macke5779992010-03-04 19:46:13 +0100961 hwc_debug(" --> get empty\n");
962 it->empty = 1;
963 return -EINVAL;
964 }
965
966 changed = 0;
967 if (it->min < rmin) {
968 it->min = rmin;
969 it->openmin = 0;
970 changed = 1;
971 }
972 if (it->max > rmax) {
973 it->max = rmax;
974 it->openmax = 0;
975 changed = 1;
976 }
977 if (snd_interval_checkempty(it)) {
978 it->empty = 1;
979 return -EINVAL;
980 }
981 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
982 return changed;
983}
984
Takashi Iwai7726dce2020-11-23 09:53:17 +0100985static int hw_rule_rate(struct snd_pcm_hw_params *params,
986 struct snd_pcm_hw_rule *rule)
987{
988 struct snd_usb_substream *subs = rule->private;
Takashi Iwaicab941b2020-11-23 09:53:33 +0100989 const struct audioformat *fp;
Takashi Iwai7726dce2020-11-23 09:53:17 +0100990 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
991 unsigned int rmin, rmax, r;
992 int i;
993
994 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
995 rmin = UINT_MAX;
996 rmax = 0;
997 list_for_each_entry(fp, &subs->fmt_list, list) {
998 if (!hw_check_valid_format(subs, params, fp))
999 continue;
1000 if (fp->rate_table && fp->nr_rates) {
1001 for (i = 0; i < fp->nr_rates; i++) {
1002 r = fp->rate_table[i];
1003 if (!snd_interval_test(it, r))
1004 continue;
1005 rmin = min(rmin, r);
1006 rmax = max(rmax, r);
1007 }
1008 } else {
1009 rmin = min(rmin, fp->rate_min);
1010 rmax = max(rmax, fp->rate_max);
1011 }
1012 }
1013
1014 return apply_hw_params_minmax(it, rmin, rmax);
1015}
1016
Daniel Macke5779992010-03-04 19:46:13 +01001017
1018static int hw_rule_channels(struct snd_pcm_hw_params *params,
1019 struct snd_pcm_hw_rule *rule)
1020{
1021 struct snd_usb_substream *subs = rule->private;
Takashi Iwaicab941b2020-11-23 09:53:33 +01001022 const struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001023 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1024 unsigned int rmin, rmax;
Daniel Macke5779992010-03-04 19:46:13 +01001025
1026 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
Takashi Iwai7726dce2020-11-23 09:53:17 +01001027 rmin = UINT_MAX;
1028 rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001029 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001030 if (!hw_check_valid_format(subs, params, fp))
1031 continue;
Takashi Iwai7726dce2020-11-23 09:53:17 +01001032 rmin = min(rmin, fp->channels);
1033 rmax = max(rmax, fp->channels);
Daniel Macke5779992010-03-04 19:46:13 +01001034 }
1035
Takashi Iwai7726dce2020-11-23 09:53:17 +01001036 return apply_hw_params_minmax(it, rmin, rmax);
Daniel Macke5779992010-03-04 19:46:13 +01001037}
1038
1039static int hw_rule_format(struct snd_pcm_hw_params *params,
1040 struct snd_pcm_hw_rule *rule)
1041{
1042 struct snd_usb_substream *subs = rule->private;
Takashi Iwaicab941b2020-11-23 09:53:33 +01001043 const struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001044 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1045 u64 fbits;
1046 u32 oldbits[2];
1047 int changed;
1048
1049 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1050 fbits = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001051 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001052 if (!hw_check_valid_format(subs, params, fp))
1053 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001054 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +01001055 }
1056
1057 oldbits[0] = fmt->bits[0];
1058 oldbits[1] = fmt->bits[1];
1059 fmt->bits[0] &= (u32)fbits;
1060 fmt->bits[1] &= (u32)(fbits >> 32);
1061 if (!fmt->bits[0] && !fmt->bits[1]) {
1062 hwc_debug(" --> get empty\n");
1063 return -EINVAL;
1064 }
1065 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1066 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1067 return changed;
1068}
1069
1070static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1071 struct snd_pcm_hw_rule *rule)
1072{
1073 struct snd_usb_substream *subs = rule->private;
Takashi Iwaicab941b2020-11-23 09:53:33 +01001074 const struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001075 struct snd_interval *it;
1076 unsigned char min_datainterval;
1077 unsigned int pmin;
Daniel Macke5779992010-03-04 19:46:13 +01001078
1079 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1080 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1081 min_datainterval = 0xff;
1082 list_for_each_entry(fp, &subs->fmt_list, list) {
1083 if (!hw_check_valid_format(subs, params, fp))
1084 continue;
1085 min_datainterval = min(min_datainterval, fp->datainterval);
1086 }
1087 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +02001088 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +01001089 it->empty = 1;
1090 return -EINVAL;
1091 }
1092 pmin = 125 * (1 << min_datainterval);
Takashi Iwai7726dce2020-11-23 09:53:17 +01001093
1094 return apply_hw_params_minmax(it, pmin, UINT_MAX);
Daniel Macke5779992010-03-04 19:46:13 +01001095}
1096
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001097/* apply PCM hw constraints from the concurrent sync EP */
1098static int apply_hw_constraint_from_sync(struct snd_pcm_runtime *runtime,
1099 struct snd_usb_substream *subs)
1100{
1101 struct snd_usb_audio *chip = subs->stream->chip;
1102 struct snd_usb_endpoint *ep;
Takashi Iwaicab941b2020-11-23 09:53:33 +01001103 const struct audioformat *fp;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001104 int err;
1105
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001106 list_for_each_entry(fp, &subs->fmt_list, list) {
Takashi Iwai54cb3192020-11-23 09:53:20 +01001107 ep = snd_usb_get_endpoint(chip, fp->endpoint);
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001108 if (ep && ep->cur_rate)
1109 goto found;
1110 if (!fp->implicit_fb)
1111 continue;
1112 /* for the implicit fb, check the sync ep as well */
Takashi Iwai54cb3192020-11-23 09:53:20 +01001113 ep = snd_usb_get_endpoint(chip, fp->sync_ep);
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001114 if (ep && ep->cur_rate)
1115 goto found;
1116 }
1117 return 0;
1118
1119 found:
1120 if (!find_format(&subs->fmt_list, ep->cur_format, ep->cur_rate,
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001121 ep->cur_channels, false, NULL)) {
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001122 usb_audio_dbg(chip, "EP 0x%x being used, but not applicable\n",
1123 ep->ep_num);
1124 return 0;
1125 }
1126
1127 usb_audio_dbg(chip, "EP 0x%x being used, using fixed params:\n",
1128 ep->ep_num);
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001129 usb_audio_dbg(chip, "rate=%d, period_size=%d, periods=%d\n",
1130 ep->cur_rate, ep->cur_period_frames,
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001131 ep->cur_buffer_periods);
1132
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001133 runtime->hw.formats = subs->formats;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001134 runtime->hw.rate_min = runtime->hw.rate_max = ep->cur_rate;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001135 runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
1136 runtime->hw.periods_min = runtime->hw.periods_max =
1137 ep->cur_buffer_periods;
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001138
1139 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1140 hw_rule_channels, subs,
1141 SNDRV_PCM_HW_PARAM_FORMAT,
1142 SNDRV_PCM_HW_PARAM_RATE,
1143 -1);
1144 if (err < 0)
1145 return err;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001146
1147 err = snd_pcm_hw_constraint_minmax(runtime,
1148 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1149 ep->cur_period_frames,
1150 ep->cur_period_frames);
1151 if (err < 0)
1152 return err;
1153
1154 return 1; /* notify the finding */
1155}
Daniel Macke5779992010-03-04 19:46:13 +01001156
1157/*
1158 * set up the runtime hardware information.
1159 */
1160
1161static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1162{
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001163 struct snd_usb_audio *chip = subs->stream->chip;
Takashi Iwaicab941b2020-11-23 09:53:33 +01001164 const struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001165 unsigned int pt, ptmin;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001166 int param_period_time_if_needed = -1;
Daniel Macke5779992010-03-04 19:46:13 +01001167 int err;
1168
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001169 mutex_lock(&chip->mutex);
1170 err = apply_hw_constraint_from_sync(runtime, subs);
1171 mutex_unlock(&chip->mutex);
1172 if (err < 0)
1173 return err;
1174 if (err > 0) /* found the matching? */
1175 goto add_extra_rules;
1176
Daniel Macke5779992010-03-04 19:46:13 +01001177 runtime->hw.formats = subs->formats;
1178
1179 runtime->hw.rate_min = 0x7fffffff;
1180 runtime->hw.rate_max = 0;
1181 runtime->hw.channels_min = 256;
1182 runtime->hw.channels_max = 0;
1183 runtime->hw.rates = 0;
1184 ptmin = UINT_MAX;
1185 /* check min/max rates and channels */
Eldad Zack88766f02013-04-03 23:18:49 +02001186 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001187 runtime->hw.rates |= fp->rates;
1188 if (runtime->hw.rate_min > fp->rate_min)
1189 runtime->hw.rate_min = fp->rate_min;
1190 if (runtime->hw.rate_max < fp->rate_max)
1191 runtime->hw.rate_max = fp->rate_max;
1192 if (runtime->hw.channels_min > fp->channels)
1193 runtime->hw.channels_min = fp->channels;
1194 if (runtime->hw.channels_max < fp->channels)
1195 runtime->hw.channels_max = fp->channels;
1196 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1197 /* FIXME: there might be more than one audio formats... */
1198 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1199 fp->frame_size;
1200 }
1201 pt = 125 * (1 << fp->datainterval);
1202 ptmin = min(ptmin, pt);
1203 }
1204
1205 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001206 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001207 /* full speed devices have fixed data packet interval */
1208 ptmin = 1000;
1209 if (ptmin == 1000)
1210 /* if period time doesn't go below 1 ms, no rules needed */
1211 param_period_time_if_needed = -1;
Daniel Macke5779992010-03-04 19:46:13 +01001212
Takashi Iwaie92be812018-05-27 15:09:15 +02001213 err = snd_pcm_hw_constraint_minmax(runtime,
1214 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1215 ptmin, UINT_MAX);
1216 if (err < 0)
1217 return err;
1218
1219 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1220 hw_rule_rate, subs,
1221 SNDRV_PCM_HW_PARAM_FORMAT,
1222 SNDRV_PCM_HW_PARAM_CHANNELS,
1223 param_period_time_if_needed,
1224 -1);
1225 if (err < 0)
1226 return err;
Takashi Iwai5a6c3e12020-11-23 09:53:16 +01001227
1228add_extra_rules:
Takashi Iwaie92be812018-05-27 15:09:15 +02001229 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1230 hw_rule_channels, subs,
1231 SNDRV_PCM_HW_PARAM_FORMAT,
1232 SNDRV_PCM_HW_PARAM_RATE,
1233 param_period_time_if_needed,
1234 -1);
1235 if (err < 0)
1236 return err;
1237 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1238 hw_rule_format, subs,
1239 SNDRV_PCM_HW_PARAM_RATE,
1240 SNDRV_PCM_HW_PARAM_CHANNELS,
1241 param_period_time_if_needed,
1242 -1);
1243 if (err < 0)
1244 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001245 if (param_period_time_if_needed >= 0) {
1246 err = snd_pcm_hw_rule_add(runtime, 0,
1247 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1248 hw_rule_period_time, subs,
1249 SNDRV_PCM_HW_PARAM_FORMAT,
1250 SNDRV_PCM_HW_PARAM_CHANNELS,
1251 SNDRV_PCM_HW_PARAM_RATE,
1252 -1);
1253 if (err < 0)
Takashi Iwaie92be812018-05-27 15:09:15 +02001254 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001255 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001256
Takashi Iwai18652112020-11-23 09:53:15 +01001257 return 0;
Daniel Macke5779992010-03-04 19:46:13 +01001258}
1259
Takashi Iwai6fddc792018-05-27 13:59:03 +02001260static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
Daniel Macke5779992010-03-04 19:46:13 +01001261{
Takashi Iwai6fddc792018-05-27 13:59:03 +02001262 int direction = substream->stream;
Daniel Macke5779992010-03-04 19:46:13 +01001263 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1264 struct snd_pcm_runtime *runtime = substream->runtime;
1265 struct snd_usb_substream *subs = &as->substream[direction];
Shuah Khan66354f12019-04-01 20:40:22 -04001266 int ret;
Daniel Macke5779992010-03-04 19:46:13 +01001267
Daniel Macke5779992010-03-04 19:46:13 +01001268 runtime->hw = snd_usb_hardware;
1269 runtime->private_data = subs;
1270 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001271 /* runtime PM is also done there */
Daniel Mackd24f5062013-04-17 00:01:38 +08001272
1273 /* initialize DSD/DOP context */
1274 subs->dsd_dop.byte_idx = 0;
1275 subs->dsd_dop.channel = 0;
1276 subs->dsd_dop.marker = 1;
1277
Shuah Khan66354f12019-04-01 20:40:22 -04001278 ret = setup_hw_info(runtime, subs);
Takashi Iwai18652112020-11-23 09:53:15 +01001279 if (ret < 0)
1280 return ret;
1281 ret = snd_usb_autoresume(subs->stream->chip);
1282 if (ret < 0)
1283 return ret;
1284 ret = snd_media_stream_init(subs, as->pcm, direction);
1285 if (ret < 0)
1286 snd_usb_autosuspend(subs->stream->chip);
Shuah Khan66354f12019-04-01 20:40:22 -04001287 return ret;
Daniel Macke5779992010-03-04 19:46:13 +01001288}
1289
Takashi Iwai6fddc792018-05-27 13:59:03 +02001290static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
Daniel Macke5779992010-03-04 19:46:13 +01001291{
Takashi Iwai6fddc792018-05-27 13:59:03 +02001292 int direction = substream->stream;
Daniel Macke5779992010-03-04 19:46:13 +01001293 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1294 struct snd_usb_substream *subs = &as->substream[direction];
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001295 int ret;
Daniel Macke5779992010-03-04 19:46:13 +01001296
Shuah Khan66354f12019-04-01 20:40:22 -04001297 snd_media_stop_pipeline(subs);
Daniel Mack68e67f42012-07-12 13:08:40 +02001298
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001299 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001300 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
Takashi Iwai47ab1542015-08-25 16:09:00 +02001301 snd_usb_unlock_shutdown(subs->stream->chip);
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001302 if (ret < 0)
1303 return ret;
Daniel Mack68e67f42012-07-12 13:08:40 +02001304 }
1305
Daniel Macke5779992010-03-04 19:46:13 +01001306 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001307 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001308
Daniel Mack68e67f42012-07-12 13:08:40 +02001309 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001310}
1311
1312/* Since a URB can handle only a single linear buffer, we must use double
1313 * buffering when the data to be transferred overflows the buffer boundary.
1314 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1315 * for all URBs.
1316 */
1317static void retire_capture_urb(struct snd_usb_substream *subs,
1318 struct urb *urb)
1319{
1320 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1321 unsigned int stride, frames, bytes, oldptr;
1322 int i, period_elapsed = 0;
1323 unsigned long flags;
1324 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001325 int current_frame_number;
1326
1327 /* read frame number here, update pointer in critical section */
1328 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001329
1330 stride = runtime->frame_bits >> 3;
1331
1332 for (i = 0; i < urb->number_of_packets; i++) {
Calvin Owens1539d4f2013-04-12 22:33:59 -05001333 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
Daniel Mackedcd3632012-04-12 13:51:12 +02001334 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001335 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1336 i, urb->iso_frame_desc[i].status);
Daniel Mackedcd3632012-04-12 13:51:12 +02001337 // continue;
1338 }
1339 bytes = urb->iso_frame_desc[i].actual_length;
Hector Martin1b7ecc22020-08-10 17:24:00 +09001340 if (subs->stream_offset_adj > 0) {
1341 unsigned int adj = min(subs->stream_offset_adj, bytes);
1342 cp += adj;
1343 bytes -= adj;
1344 subs->stream_offset_adj -= adj;
1345 }
Daniel Mackedcd3632012-04-12 13:51:12 +02001346 frames = bytes / stride;
1347 if (!subs->txfr_quirk)
1348 bytes = frames * stride;
1349 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001350 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001351 bytes = frames * stride;
Takashi Iwai377a8792018-05-16 20:07:18 +02001352 dev_warn_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001353 "Corrected urb data len. %d->%d\n",
Daniel Mackedcd3632012-04-12 13:51:12 +02001354 oldbytes, bytes);
1355 }
1356 /* update the current pointer */
1357 spin_lock_irqsave(&subs->lock, flags);
1358 oldptr = subs->hwptr_done;
1359 subs->hwptr_done += bytes;
1360 if (subs->hwptr_done >= runtime->buffer_size * stride)
1361 subs->hwptr_done -= runtime->buffer_size * stride;
1362 frames = (bytes + (oldptr % stride)) / stride;
1363 subs->transfer_done += frames;
1364 if (subs->transfer_done >= runtime->period_size) {
1365 subs->transfer_done -= runtime->period_size;
1366 period_elapsed = 1;
1367 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001368 /* capture delay is by construction limited to one URB,
1369 * reset delays here
1370 */
1371 runtime->delay = subs->last_delay = 0;
1372
1373 /* realign last_frame_number */
1374 subs->last_frame_number = current_frame_number;
1375 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1376
Daniel Mackedcd3632012-04-12 13:51:12 +02001377 spin_unlock_irqrestore(&subs->lock, flags);
1378 /* copy a data chunk */
1379 if (oldptr + bytes > runtime->buffer_size * stride) {
1380 unsigned int bytes1 =
1381 runtime->buffer_size * stride - oldptr;
1382 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1383 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1384 } else {
1385 memcpy(runtime->dma_area + oldptr, cp, bytes);
1386 }
1387 }
1388
1389 if (period_elapsed)
1390 snd_pcm_period_elapsed(subs->pcm_substream);
1391}
1392
Daniel Mackd24f5062013-04-17 00:01:38 +08001393static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1394 struct urb *urb, unsigned int bytes)
1395{
1396 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1397 unsigned int stride = runtime->frame_bits >> 3;
1398 unsigned int dst_idx = 0;
1399 unsigned int src_idx = subs->hwptr_done;
1400 unsigned int wrap = runtime->buffer_size * stride;
1401 u8 *dst = urb->transfer_buffer;
1402 u8 *src = runtime->dma_area;
1403 u8 marker[] = { 0x05, 0xfa };
1404
1405 /*
1406 * The DSP DOP format defines a way to transport DSD samples over
1407 * normal PCM data endpoints. It requires stuffing of marker bytes
1408 * (0x05 and 0xfa, alternating per sample frame), and then expects
1409 * 2 additional bytes of actual payload. The whole frame is stored
1410 * LSB.
1411 *
1412 * Hence, for a stereo transport, the buffer layout looks like this,
1413 * where L refers to left channel samples and R to right.
1414 *
1415 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1416 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1417 * .....
1418 *
1419 */
1420
1421 while (bytes--) {
1422 if (++subs->dsd_dop.byte_idx == 3) {
1423 /* frame boundary? */
1424 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1425 src_idx += 2;
1426 subs->dsd_dop.byte_idx = 0;
1427
1428 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1429 /* alternate the marker */
1430 subs->dsd_dop.marker++;
1431 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1432 subs->dsd_dop.channel = 0;
1433 }
1434 } else {
1435 /* stuff the DSD payload */
1436 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001437
1438 if (subs->cur_audiofmt->dsd_bitrev)
1439 dst[dst_idx++] = bitrev8(src[idx]);
1440 else
1441 dst[dst_idx++] = src[idx];
1442
Daniel Mackd24f5062013-04-17 00:01:38 +08001443 subs->hwptr_done++;
1444 }
1445 }
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001446 if (subs->hwptr_done >= runtime->buffer_size * stride)
1447 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001448}
1449
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001450static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1451 int offset, int stride, unsigned int bytes)
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001452{
1453 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1454
1455 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1456 /* err, the transferred area goes over buffer boundary. */
1457 unsigned int bytes1 =
1458 runtime->buffer_size * stride - subs->hwptr_done;
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001459 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001460 runtime->dma_area + subs->hwptr_done, bytes1);
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001461 memcpy(urb->transfer_buffer + offset + bytes1,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001462 runtime->dma_area, bytes - bytes1);
1463 } else {
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001464 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001465 runtime->dma_area + subs->hwptr_done, bytes);
1466 }
1467 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001468 if (subs->hwptr_done >= runtime->buffer_size * stride)
1469 subs->hwptr_done -= runtime->buffer_size * stride;
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001470}
1471
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001472static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1473 struct urb *urb, int stride,
1474 unsigned int bytes)
1475{
1476 __le32 packet_length;
1477 int i;
1478
1479 /* Put __le32 length descriptor at start of each packet. */
1480 for (i = 0; i < urb->number_of_packets; i++) {
1481 unsigned int length = urb->iso_frame_desc[i].length;
1482 unsigned int offset = urb->iso_frame_desc[i].offset;
1483
1484 packet_length = cpu_to_le32(length);
1485 offset += i * sizeof(packet_length);
1486 urb->iso_frame_desc[i].offset = offset;
1487 urb->iso_frame_desc[i].length += sizeof(packet_length);
1488 memcpy(urb->transfer_buffer + offset,
1489 &packet_length, sizeof(packet_length));
1490 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1491 stride, length);
1492 }
1493 /* Adjust transfer size accordingly. */
1494 bytes += urb->number_of_packets * sizeof(packet_length);
1495 return bytes;
1496}
1497
Daniel Mackedcd3632012-04-12 13:51:12 +02001498static void prepare_playback_urb(struct snd_usb_substream *subs,
1499 struct urb *urb)
1500{
1501 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001502 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001503 struct snd_urb_ctx *ctx = urb->context;
1504 unsigned int counts, frames, bytes;
1505 int i, stride, period_elapsed = 0;
1506 unsigned long flags;
1507
1508 stride = runtime->frame_bits >> 3;
1509
1510 frames = 0;
1511 urb->number_of_packets = 0;
1512 spin_lock_irqsave(&subs->lock, flags);
Alan Stern976b6c02013-09-24 15:51:58 -04001513 subs->frame_limit += ep->max_urb_frames;
Daniel Mackedcd3632012-04-12 13:51:12 +02001514 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001515 if (ctx->packet_size[i])
1516 counts = ctx->packet_size[i];
Alexander Tsoyf0bd62b2020-04-24 05:24:48 +03001517 else if (ep->sync_master)
1518 counts = snd_usb_endpoint_slave_next_packet_size(ep);
Daniel Mack245baf92012-08-30 18:52:30 +02001519 else
1520 counts = snd_usb_endpoint_next_packet_size(ep);
1521
Daniel Mackedcd3632012-04-12 13:51:12 +02001522 /* set up descriptor */
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001523 urb->iso_frame_desc[i].offset = frames * ep->stride;
1524 urb->iso_frame_desc[i].length = counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001525 frames += counts;
1526 urb->number_of_packets++;
1527 subs->transfer_done += counts;
1528 if (subs->transfer_done >= runtime->period_size) {
1529 subs->transfer_done -= runtime->period_size;
Alan Stern976b6c02013-09-24 15:51:58 -04001530 subs->frame_limit = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001531 period_elapsed = 1;
1532 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1533 if (subs->transfer_done > 0) {
1534 /* FIXME: fill-max mode is not
1535 * supported yet */
1536 frames -= subs->transfer_done;
1537 counts -= subs->transfer_done;
1538 urb->iso_frame_desc[i].length =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001539 counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001540 subs->transfer_done = 0;
1541 }
1542 i++;
1543 if (i < ctx->packets) {
1544 /* add a transfer delimiter */
1545 urb->iso_frame_desc[i].offset =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001546 frames * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001547 urb->iso_frame_desc[i].length = 0;
1548 urb->number_of_packets++;
1549 }
1550 break;
1551 }
1552 }
Alan Stern976b6c02013-09-24 15:51:58 -04001553 /* finish at the period boundary or after enough frames */
1554 if ((period_elapsed ||
1555 subs->transfer_done >= subs->frame_limit) &&
1556 !snd_usb_endpoint_implicit_feedback_sink(ep))
Daniel Mackedcd3632012-04-12 13:51:12 +02001557 break;
1558 }
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001559 bytes = frames * ep->stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001560
Takashi Iwai6aa719d2020-11-23 09:53:36 +01001561 if (unlikely(ep->cur_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
Daniel Mackd24f5062013-04-17 00:01:38 +08001562 subs->cur_audiofmt->dsd_dop)) {
1563 fill_playback_urb_dsd_dop(subs, urb, bytes);
Takashi Iwai6aa719d2020-11-23 09:53:36 +01001564 } else if (unlikely(ep->cur_format == SNDRV_PCM_FORMAT_DSD_U8 &&
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001565 subs->cur_audiofmt->dsd_bitrev)) {
1566 /* bit-reverse the bytes */
1567 u8 *buf = urb->transfer_buffer;
1568 for (i = 0; i < bytes; i++) {
1569 int idx = (subs->hwptr_done + i)
1570 % (runtime->buffer_size * stride);
1571 buf[i] = bitrev8(runtime->dma_area[idx]);
1572 }
1573
1574 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001575 if (subs->hwptr_done >= runtime->buffer_size * stride)
1576 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001577 } else {
Daniel Mackd24f5062013-04-17 00:01:38 +08001578 /* usual PCM */
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001579 if (!subs->tx_length_quirk)
1580 copy_to_urb(subs, urb, 0, stride, bytes);
1581 else
1582 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1583 /* bytes is now amount of outgoing data */
Daniel Mackedcd3632012-04-12 13:51:12 +02001584 }
Daniel Mackd24f5062013-04-17 00:01:38 +08001585
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001586 /* update delay with exact number of samples queued */
1587 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001588 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001589 subs->last_delay = runtime->delay;
1590
1591 /* realign last_frame_number */
1592 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1593 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1594
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001595 if (subs->trigger_tstamp_pending_update) {
1596 /* this is the first actual URB submitted,
1597 * update trigger timestamp to reflect actual start time
1598 */
1599 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1600 subs->trigger_tstamp_pending_update = false;
1601 }
1602
Daniel Mackedcd3632012-04-12 13:51:12 +02001603 spin_unlock_irqrestore(&subs->lock, flags);
1604 urb->transfer_buffer_length = bytes;
1605 if (period_elapsed)
1606 snd_pcm_period_elapsed(subs->pcm_substream);
1607}
1608
1609/*
1610 * process after playback data complete
1611 * - decrease the delay count again
1612 */
1613static void retire_playback_urb(struct snd_usb_substream *subs,
1614 struct urb *urb)
1615{
1616 unsigned long flags;
1617 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001618 struct snd_usb_endpoint *ep = subs->data_endpoint;
1619 int processed = urb->transfer_buffer_length / ep->stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001620 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001621
Alexander Tsoy5ff40e62020-06-29 06:26:07 +03001622 /* ignore the delay accounting when processed=0 is given, i.e.
1623 * silent payloads are processed before handling the actual data
Takashi Iwai1213a202012-09-06 14:58:00 +02001624 */
1625 if (!processed)
1626 return;
1627
Daniel Mackedcd3632012-04-12 13:51:12 +02001628 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001629 if (!subs->last_delay)
1630 goto out; /* short path */
1631
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001632 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1633 /* update delay with exact number of samples played */
1634 if (processed > subs->last_delay)
1635 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001636 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001637 subs->last_delay -= processed;
1638 runtime->delay = subs->last_delay;
1639
1640 /*
1641 * Report when delay estimate is off by more than 2ms.
1642 * The error should be lower than 2ms since the estimate relies
1643 * on two reads of a counter updated every ms.
1644 */
Sander Eikelenboomb7a77232014-05-02 15:09:27 +02001645 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1646 dev_dbg_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001647 "delay: estimated %d, actual %d\n",
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001648 est_delay, subs->last_delay);
1649
Takashi Iwai48779a02012-11-23 16:00:37 +01001650 if (!subs->running) {
1651 /* update last_frame_number for delay counting here since
1652 * prepare_playback_urb won't be called during pause
1653 */
1654 subs->last_frame_number =
1655 usb_get_current_frame_number(subs->dev) & 0xff;
1656 }
1657
1658 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001659 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001660}
1661
Daniel Mackedcd3632012-04-12 13:51:12 +02001662static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1663 int cmd)
1664{
1665 struct snd_usb_substream *subs = substream->runtime->private_data;
1666
1667 switch (cmd) {
1668 case SNDRV_PCM_TRIGGER_START:
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001669 subs->trigger_tstamp_pending_update = true;
Gustavo A. R. Silvac0dbbda2020-07-08 15:32:36 -05001670 fallthrough;
Daniel Mackedcd3632012-04-12 13:51:12 +02001671 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Takashi Iwai96e221f2020-11-23 09:53:28 +01001672 snd_usb_endpoint_set_callback(subs->data_endpoint,
1673 prepare_playback_urb,
1674 retire_playback_urb,
1675 subs);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001676 subs->running = 1;
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001677 dev_dbg(&subs->dev->dev, "%d:%d Start Playback PCM\n",
1678 subs->cur_audiofmt->iface,
1679 subs->cur_audiofmt->altsetting);
Daniel Mackedcd3632012-04-12 13:51:12 +02001680 return 0;
Takashi Iwai75c16b52020-11-23 09:53:29 +01001681 case SNDRV_PCM_TRIGGER_SUSPEND:
Daniel Mackedcd3632012-04-12 13:51:12 +02001682 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001683 stop_endpoints(subs);
Takashi Iwai96e221f2020-11-23 09:53:28 +01001684 snd_usb_endpoint_set_callback(subs->data_endpoint,
1685 NULL, NULL, NULL);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001686 subs->running = 0;
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001687 dev_dbg(&subs->dev->dev, "%d:%d Stop Playback PCM\n",
1688 subs->cur_audiofmt->iface,
1689 subs->cur_audiofmt->altsetting);
Daniel Mackedcd3632012-04-12 13:51:12 +02001690 return 0;
1691 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai48779a02012-11-23 16:00:37 +01001692 /* keep retire_data_urb for delay calculation */
Takashi Iwai96e221f2020-11-23 09:53:28 +01001693 snd_usb_endpoint_set_callback(subs->data_endpoint,
1694 NULL,
1695 retire_playback_urb,
1696 subs);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001697 subs->running = 0;
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001698 dev_dbg(&subs->dev->dev, "%d:%d Pause Playback PCM\n",
1699 subs->cur_audiofmt->iface,
1700 subs->cur_audiofmt->altsetting);
Daniel Mackedcd3632012-04-12 13:51:12 +02001701 return 0;
1702 }
1703
1704 return -EINVAL;
1705}
1706
Daniel Mackafe25962012-06-16 16:58:04 +02001707static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1708 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001709{
1710 int err;
1711 struct snd_usb_substream *subs = substream->runtime->private_data;
1712
1713 switch (cmd) {
1714 case SNDRV_PCM_TRIGGER_START:
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +02001715 err = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +02001716 if (err < 0)
1717 return err;
Takashi Iwai96e221f2020-11-23 09:53:28 +01001718 fallthrough;
1719 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1720 snd_usb_endpoint_set_callback(subs->data_endpoint,
1721 NULL, retire_capture_urb,
1722 subs);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001723 subs->running = 1;
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001724 dev_dbg(&subs->dev->dev, "%d:%d Start Capture PCM\n",
1725 subs->cur_audiofmt->iface,
1726 subs->cur_audiofmt->altsetting);
Daniel Mackedcd3632012-04-12 13:51:12 +02001727 return 0;
Takashi Iwai75c16b52020-11-23 09:53:29 +01001728 case SNDRV_PCM_TRIGGER_SUSPEND:
Daniel Mackedcd3632012-04-12 13:51:12 +02001729 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001730 stop_endpoints(subs);
Takashi Iwai96e221f2020-11-23 09:53:28 +01001731 fallthrough;
Daniel Mackedcd3632012-04-12 13:51:12 +02001732 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Takashi Iwai96e221f2020-11-23 09:53:28 +01001733 snd_usb_endpoint_set_callback(subs->data_endpoint,
1734 NULL, NULL, NULL);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001735 subs->running = 0;
Takashi Iwaibf6313a2020-11-23 09:53:31 +01001736 dev_dbg(&subs->dev->dev, "%d:%d Stop Capture PCM\n",
1737 subs->cur_audiofmt->iface,
1738 subs->cur_audiofmt->altsetting);
Daniel Mackedcd3632012-04-12 13:51:12 +02001739 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001740 }
1741
1742 return -EINVAL;
1743}
1744
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301745static const struct snd_pcm_ops snd_usb_playback_ops = {
Takashi Iwai6fddc792018-05-27 13:59:03 +02001746 .open = snd_usb_pcm_open,
1747 .close = snd_usb_pcm_close,
Daniel Macke5779992010-03-04 19:46:13 +01001748 .hw_params = snd_usb_hw_params,
1749 .hw_free = snd_usb_hw_free,
1750 .prepare = snd_usb_pcm_prepare,
1751 .trigger = snd_usb_substream_playback_trigger,
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001752 .sync_stop = snd_usb_pcm_sync_stop,
Daniel Macke5779992010-03-04 19:46:13 +01001753 .pointer = snd_usb_pcm_pointer,
Daniel Macke5779992010-03-04 19:46:13 +01001754};
1755
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301756static const struct snd_pcm_ops snd_usb_capture_ops = {
Takashi Iwai6fddc792018-05-27 13:59:03 +02001757 .open = snd_usb_pcm_open,
1758 .close = snd_usb_pcm_close,
Daniel Macke5779992010-03-04 19:46:13 +01001759 .hw_params = snd_usb_hw_params,
1760 .hw_free = snd_usb_hw_free,
1761 .prepare = snd_usb_pcm_prepare,
1762 .trigger = snd_usb_substream_capture_trigger,
Takashi Iwaidc5eafe2019-12-10 07:34:54 +01001763 .sync_stop = snd_usb_pcm_sync_stop,
Daniel Macke5779992010-03-04 19:46:13 +01001764 .pointer = snd_usb_pcm_pointer,
Takashi Iwaif274baa2018-05-27 13:01:17 +02001765};
1766
Daniel Macke5779992010-03-04 19:46:13 +01001767void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1768{
Takashi Iwaif274baa2018-05-27 13:01:17 +02001769 const struct snd_pcm_ops *ops;
1770
Takashi Iwaib315997d2019-11-05 16:18:40 +01001771 ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
Takashi Iwaif274baa2018-05-27 13:01:17 +02001772 &snd_usb_playback_ops : &snd_usb_capture_ops;
Takashi Iwaif274baa2018-05-27 13:01:17 +02001773 snd_pcm_set_ops(pcm, stream, ops);
1774}
1775
1776void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
1777{
1778 struct snd_pcm *pcm = subs->stream->pcm;
1779 struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
1780 struct device *dev = subs->dev->bus->controller;
1781
Takashi Iwaib315997d2019-11-05 16:18:40 +01001782 if (snd_usb_use_vmalloc)
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +01001783 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_VMALLOC,
1784 NULL, 0, 0);
Takashi Iwaib315997d2019-11-05 16:18:40 +01001785 else
Takashi Iwai6dd9486ca2019-12-09 10:49:42 +01001786 snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_DEV_SG,
1787 dev, 64*1024, 512*1024);
Daniel Macke5779992010-03-04 19:46:13 +01001788}