blob: 1e282800e983d46f7d0ec9b72312419d63018839 [file] [log] [blame]
Daniel Macke5779992010-03-04 19:46:13 +01001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17#include <linux/init.h>
Stephen Rothwell9966dda2010-03-29 19:01:48 +110018#include <linux/slab.h>
Daniel Mack44dcbbb2013-04-17 00:01:39 +080019#include <linux/bitrev.h>
Daniel Mackedcd3632012-04-12 13:51:12 +020020#include <linux/ratelimit.h>
Daniel Macke5779992010-03-04 19:46:13 +010021#include <linux/usb.h>
22#include <linux/usb/audio.h>
Daniel Mack7e847892010-03-11 21:13:20 +010023#include <linux/usb/audio-v2.h>
Daniel Macke5779992010-03-04 19:46:13 +010024
25#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28
29#include "usbaudio.h"
30#include "card.h"
31#include "quirks.h"
32#include "debug.h"
Daniel Mackc731bc92011-09-14 12:46:57 +020033#include "endpoint.h"
Daniel Macke5779992010-03-04 19:46:13 +010034#include "helper.h"
35#include "pcm.h"
Daniel Mack79f920f2010-05-31 14:51:31 +020036#include "clock.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010037#include "power.h"
Daniel Macke5779992010-03-04 19:46:13 +010038
Daniel Mackedcd3632012-04-12 13:51:12 +020039#define SUBSTREAM_FLAG_DATA_EP_STARTED 0
40#define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
41
Hemant Kumar85491422017-08-18 18:44:43 -070042#define MAX_SETALT_TIMEOUT_MS 1000
43
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050044/* return the estimated delay based on USB frame counters */
45snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
46 unsigned int rate)
47{
48 int current_frame_number;
49 int frame_diff;
50 int est_delay;
51
Takashi Iwai48779a02012-11-23 16:00:37 +010052 if (!subs->last_delay)
53 return 0; /* short path */
54
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050055 current_frame_number = usb_get_current_frame_number(subs->dev);
56 /*
57 * HCD implementations use different widths, use lower 8 bits.
58 * The delay will be managed up to 256ms, which is more than
59 * enough
60 */
61 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
62
63 /* Approximation based on number of samples per USB frame (ms),
64 some truncation for 44.1 but the estimate is good enough */
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060065 est_delay = frame_diff * rate / 1000;
66 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
67 est_delay = subs->last_delay - est_delay;
68 else
69 est_delay = subs->last_delay + est_delay;
70
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050071 if (est_delay < 0)
72 est_delay = 0;
73 return est_delay;
74}
75
Daniel Macke5779992010-03-04 19:46:13 +010076/*
77 * return the current pcm pointer. just based on the hwptr_done value.
78 */
79static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
80{
Takashi Iwaie92be812018-05-27 15:09:15 +020081 struct snd_usb_substream *subs = substream->runtime->private_data;
Daniel Macke5779992010-03-04 19:46:13 +010082 unsigned int hwptr_done;
83
Takashi Iwai47ab1542015-08-25 16:09:00 +020084 if (atomic_read(&subs->stream->chip->shutdown))
Takashi Iwai978520b2012-10-12 15:12:55 +020085 return SNDRV_PCM_POS_XRUN;
Daniel Macke5779992010-03-04 19:46:13 +010086 spin_lock(&subs->lock);
87 hwptr_done = subs->hwptr_done;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060088 substream->runtime->delay = snd_usb_pcm_delay(subs,
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050089 substream->runtime->rate);
Daniel Macke5779992010-03-04 19:46:13 +010090 spin_unlock(&subs->lock);
91 return hwptr_done / (substream->runtime->frame_bits >> 3);
92}
93
94/*
95 * find a matching audio format
96 */
Dylan Reid61a70952012-09-18 09:49:48 -070097static struct audioformat *find_format(struct snd_usb_substream *subs)
Daniel Macke5779992010-03-04 19:46:13 +010098{
Eldad Zack88766f02013-04-03 23:18:49 +020099 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100100 struct audioformat *found = NULL;
101 int cur_attr = 0, attr;
102
Eldad Zack88766f02013-04-03 23:18:49 +0200103 list_for_each_entry(fp, &subs->fmt_list, list) {
Eldad Zack74c34ca2013-04-23 01:00:41 +0200104 if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100105 continue;
Dylan Reid61a70952012-09-18 09:49:48 -0700106 if (fp->channels != subs->channels)
Daniel Macke5779992010-03-04 19:46:13 +0100107 continue;
Dylan Reid61a70952012-09-18 09:49:48 -0700108 if (subs->cur_rate < fp->rate_min ||
109 subs->cur_rate > fp->rate_max)
Daniel Macke5779992010-03-04 19:46:13 +0100110 continue;
111 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
112 unsigned int i;
113 for (i = 0; i < fp->nr_rates; i++)
Dylan Reid61a70952012-09-18 09:49:48 -0700114 if (fp->rate_table[i] == subs->cur_rate)
Daniel Macke5779992010-03-04 19:46:13 +0100115 break;
116 if (i >= fp->nr_rates)
117 continue;
118 }
119 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
120 if (! found) {
121 found = fp;
122 cur_attr = attr;
123 continue;
124 }
125 /* avoid async out and adaptive in if the other method
126 * supports the same format.
127 * this is a workaround for the case like
128 * M-audio audiophile USB.
129 */
130 if (attr != cur_attr) {
131 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
132 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
133 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
134 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
135 continue;
136 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
137 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
138 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
139 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
140 found = fp;
141 cur_attr = attr;
142 continue;
143 }
144 }
145 /* find the format with the largest max. packet size */
146 if (fp->maxpacksize > found->maxpacksize) {
147 found = fp;
148 cur_attr = attr;
149 }
150 }
151 return found;
152}
153
Hemant Kumarf055b382018-09-13 15:52:42 -0700154/*
155 * find a matching audio format as well as non-zero service interval
156 */
157static struct audioformat *find_format_and_si(struct snd_usb_substream *subs,
158 unsigned int datainterval)
159{
160 unsigned int i;
161 struct audioformat *fp;
162 struct audioformat *found = NULL;
163 int cur_attr = 0, attr;
164
165 list_for_each_entry(fp, &subs->fmt_list, list) {
166 if (datainterval != fp->datainterval)
167 continue;
168 if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
169 continue;
170 if (fp->channels != subs->channels)
171 continue;
172 if (subs->cur_rate < fp->rate_min ||
173 subs->cur_rate > fp->rate_max)
174 continue;
175 if (!(fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
176 for (i = 0; i < fp->nr_rates; i++)
177 if (fp->rate_table[i] == subs->cur_rate)
178 break;
179 if (i >= fp->nr_rates)
180 continue;
181 }
182 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
183 if (!found) {
184 found = fp;
185 cur_attr = attr;
186 continue;
187 }
188 /* avoid async out and adaptive in if the other method
189 * supports the same format.
190 * this is a workaround for the case like
191 * M-audio audiophile USB.
192 */
193 if (attr != cur_attr) {
194 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
195 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
196 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
197 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
198 continue;
199 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
200 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
201 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
202 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
203 found = fp;
204 cur_attr = attr;
205 continue;
206 }
207 }
208 /* find the format with the largest max. packet size */
209 if (fp->maxpacksize > found->maxpacksize) {
210 found = fp;
211 cur_attr = attr;
212 }
213 }
214 return found;
215}
216
Daniel Mack767d75a2010-03-04 19:46:17 +0100217static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
218 struct usb_host_interface *alts,
219 struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +0100220{
Daniel Mack767d75a2010-03-04 19:46:17 +0100221 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100222 unsigned int ep;
223 unsigned char data[1];
224 int err;
225
Takashi Iwai447d6272016-03-15 15:20:58 +0100226 if (get_iface_desc(alts)->bNumEndpoints < 1)
227 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100228 ep = get_endpoint(alts, 0)->bEndpointAddress;
Daniel Mack767d75a2010-03-04 19:46:17 +0100229
Daniel Mack767d75a2010-03-04 19:46:17 +0100230 data[0] = 1;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200231 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
232 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
233 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
234 data, sizeof(data));
235 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100236 usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
237 iface, ep);
Daniel Mack767d75a2010-03-04 19:46:17 +0100238 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100239 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100240
Daniel Macke5779992010-03-04 19:46:13 +0100241 return 0;
242}
243
Daniel Mack92c25612010-05-26 18:11:39 +0200244static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
245 struct usb_host_interface *alts,
246 struct audioformat *fmt)
247{
248 struct usb_device *dev = chip->dev;
249 unsigned char data[1];
Daniel Mack92c25612010-05-26 18:11:39 +0200250 int err;
251
Daniel Mack92c25612010-05-26 18:11:39 +0200252 data[0] = 1;
Takashi Iwaif25ecf82018-05-27 15:18:22 +0200253 err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
254 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
255 UAC2_EP_CS_PITCH << 8, 0,
256 data, sizeof(data));
257 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100258 usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
259 iface, fmt->altsetting);
Daniel Mack92c25612010-05-26 18:11:39 +0200260 return err;
261 }
262
263 return 0;
264}
265
Daniel Mack767d75a2010-03-04 19:46:17 +0100266/*
Daniel Mack92c25612010-05-26 18:11:39 +0200267 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100268 */
269int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
270 struct usb_host_interface *alts,
271 struct audioformat *fmt)
272{
Daniel Mack92c25612010-05-26 18:11:39 +0200273 /* if endpoint doesn't have pitch control, bail out */
274 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
275 return 0;
276
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100277 switch (fmt->protocol) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100278 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200279 default:
Daniel Mack767d75a2010-03-04 19:46:17 +0100280 return init_pitch_v1(chip, iface, alts, fmt);
281
282 case UAC_VERSION_2:
Daniel Mack92c25612010-05-26 18:11:39 +0200283 return init_pitch_v2(chip, iface, alts, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100284 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100285}
286
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200287static int start_endpoints(struct snd_usb_substream *subs)
Daniel Mackedcd3632012-04-12 13:51:12 +0200288{
289 int err;
290
291 if (!subs->data_endpoint)
292 return -EINVAL;
293
294 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
295 struct snd_usb_endpoint *ep = subs->data_endpoint;
296
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100297 dev_dbg(&subs->dev->dev, "Starting data EP @%p\n", ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200298
299 ep->data_subs = subs;
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200300 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200301 if (err < 0) {
302 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
303 return err;
304 }
305 }
306
307 if (subs->sync_endpoint &&
308 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
309 struct snd_usb_endpoint *ep = subs->sync_endpoint;
310
Daniel Mack2e4a2632012-08-30 18:52:31 +0200311 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
Eldad Zackdf23a242013-10-06 22:31:13 +0200312 subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
Daniel Mack2e4a2632012-08-30 18:52:31 +0200313 err = usb_set_interface(subs->dev,
314 subs->sync_endpoint->iface,
Eldad Zackdf23a242013-10-06 22:31:13 +0200315 subs->sync_endpoint->altsetting);
Daniel Mack2e4a2632012-08-30 18:52:31 +0200316 if (err < 0) {
Eldad Zack06613f52013-10-06 22:31:11 +0200317 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100318 dev_err(&subs->dev->dev,
319 "%d:%d: cannot set interface (%d)\n",
Daniel Mack2e4a2632012-08-30 18:52:31 +0200320 subs->sync_endpoint->iface,
Eldad Zackdf23a242013-10-06 22:31:13 +0200321 subs->sync_endpoint->altsetting, err);
Daniel Mack2e4a2632012-08-30 18:52:31 +0200322 return -EIO;
323 }
324 }
325
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100326 dev_dbg(&subs->dev->dev, "Starting sync EP @%p\n", ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200327
328 ep->sync_slave = subs->data_endpoint;
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200329 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200330 if (err < 0) {
331 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
332 return err;
333 }
334 }
335
336 return 0;
337}
338
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100339static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
Daniel Mackedcd3632012-04-12 13:51:12 +0200340{
341 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100342 snd_usb_endpoint_stop(subs->sync_endpoint);
Daniel Mackedcd3632012-04-12 13:51:12 +0200343
344 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100345 snd_usb_endpoint_stop(subs->data_endpoint);
346
347 if (wait) {
348 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
349 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
350 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200351}
352
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100353static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
354 unsigned int altsetting,
355 struct usb_host_interface **alts,
356 unsigned int *ep)
357{
358 struct usb_interface *iface;
359 struct usb_interface_descriptor *altsd;
360 struct usb_endpoint_descriptor *epd;
361
362 iface = usb_ifnum_to_if(dev, ifnum);
363 if (!iface || iface->num_altsetting < altsetting + 1)
364 return -ENOENT;
365 *alts = &iface->altsetting[altsetting];
366 altsd = get_iface_desc(*alts);
367 if (altsd->bAlternateSetting != altsetting ||
368 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
369 (altsd->bInterfaceSubClass != 2 &&
370 altsd->bInterfaceProtocol != 2 ) ||
371 altsd->bNumEndpoints < 1)
372 return -ENOENT;
373 epd = get_endpoint(*alts, 0);
374 if (!usb_endpoint_is_isoc_in(epd) ||
375 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
376 USB_ENDPOINT_USAGE_IMPLICIT_FB)
377 return -ENOENT;
378 *ep = epd->bEndpointAddress;
379 return 0;
380}
381
Manuel Reinhardtbdd44812019-01-31 15:32:35 +0100382/* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
383 * applies. Returns 1 if a quirk was found.
384 */
Eldad Zacka60945f2013-08-03 10:50:18 +0200385static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
386 struct usb_device *dev,
387 struct usb_interface_descriptor *altsd,
388 unsigned int attr)
Daniel Macke5779992010-03-04 19:46:13 +0100389{
Eldad Zacka60945f2013-08-03 10:50:18 +0200390 struct usb_host_interface *alts;
Daniel Macke5779992010-03-04 19:46:13 +0100391 struct usb_interface *iface;
Eldad Zacka60945f2013-08-03 10:50:18 +0200392 unsigned int ep;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500393 unsigned int ifnum;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200394
Eldad Zack914273c2013-08-03 10:50:21 +0200395 /* Implicit feedback sync EPs consumers are always playback EPs */
396 if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
397 return 0;
398
Daniel Mackc75a8a72012-04-12 13:51:14 +0200399 switch (subs->stream->chip->usb_id) {
Eldad Zackca10a7e2012-11-28 23:55:41 +0100400 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500401 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zack914273c2013-08-03 10:50:21 +0200402 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500403 ifnum = 3;
404 goto add_sync_ep_from_ifnum;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200405 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
406 case USB_ID(0x0763, 0x2081):
Eldad Zack914273c2013-08-03 10:50:21 +0200407 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500408 ifnum = 2;
409 goto add_sync_ep_from_ifnum;
410 case USB_ID(0x2466, 0x8003): /* Fractal Audio Axe-Fx II */
Alberto Aguirre17f08b02016-12-08 00:36:48 -0600411 ep = 0x86;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500412 ifnum = 2;
413 goto add_sync_ep_from_ifnum;
Alberto Aguirre91a85612018-04-18 09:35:35 -0500414 case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx III */
415 ep = 0x81;
416 ifnum = 2;
417 goto add_sync_ep_from_ifnum;
Takashi Iwaicbd905d2019-08-20 08:58:12 +0200418 case USB_ID(0x1397, 0x0001): /* Behringer UFX1604 */
Alberto Aguirre103e9622018-04-18 09:35:34 -0500419 case USB_ID(0x1397, 0x0002): /* Behringer UFX1204 */
Lassi Ylikojola5e35dc02018-02-09 16:51:36 +0200420 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500421 ifnum = 1;
422 goto add_sync_ep_from_ifnum;
Szabolcs Szőke7dda7482019-10-11 19:19:36 +0200423 case USB_ID(0x0582, 0x01d8): /* BOSS Katana */
424 /* BOSS Katana amplifiers do not need quirks */
425 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200426 }
Alberto Aguirre103e9622018-04-18 09:35:34 -0500427
Eldad Zack914273c2013-08-03 10:50:21 +0200428 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100429 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
430 altsd->bInterfaceProtocol == 2 &&
431 altsd->bNumEndpoints == 1 &&
432 USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
433 search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
434 altsd->bAlternateSetting,
435 &alts, &ep) >= 0) {
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100436 goto add_sync_ep;
437 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200438
Eldad Zacka60945f2013-08-03 10:50:18 +0200439 /* No quirk */
440 return 0;
441
Alberto Aguirre103e9622018-04-18 09:35:34 -0500442add_sync_ep_from_ifnum:
443 iface = usb_ifnum_to_if(dev, ifnum);
444
445 if (!iface || iface->num_altsetting == 0)
446 return -EINVAL;
447
448 alts = &iface->altsetting[1];
449
Eldad Zacka60945f2013-08-03 10:50:18 +0200450add_sync_ep:
451 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
452 alts, ep, !subs->direction,
Eldad Zack88abb8e2013-08-03 10:51:14 +0200453 SND_USB_ENDPOINT_TYPE_DATA);
Eldad Zacka60945f2013-08-03 10:50:18 +0200454 if (!subs->sync_endpoint)
455 return -EINVAL;
456
457 subs->data_endpoint->sync_master = subs->sync_endpoint;
458
Manuel Reinhardtbdd44812019-01-31 15:32:35 +0100459 return 1;
Eldad Zacka60945f2013-08-03 10:50:18 +0200460}
461
462static int set_sync_endpoint(struct snd_usb_substream *subs,
463 struct audioformat *fmt,
464 struct usb_device *dev,
465 struct usb_host_interface *alts,
466 struct usb_interface_descriptor *altsd)
467{
468 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
469 unsigned int ep, attr;
Eldad Zack95fec882013-08-03 10:50:20 +0200470 bool implicit_fb;
Eldad Zacka60945f2013-08-03 10:50:18 +0200471 int err;
472
473 /* we need a sync pipe in async OUT or adaptive IN mode */
474 /* check the number of EP, since some devices have broken
475 * descriptors which fool us. if it has only one EP,
476 * assume it as adaptive-out or sync-in.
477 */
478 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
479
Pierre-Louis Bossart63018442015-08-14 17:19:42 -0500480 if ((is_playback && (attr != USB_ENDPOINT_SYNC_ASYNC)) ||
481 (!is_playback && (attr != USB_ENDPOINT_SYNC_ADAPTIVE))) {
482
483 /*
484 * In these modes the notion of sync_endpoint is irrelevant.
485 * Reset pointers to avoid using stale data from previously
486 * used settings, e.g. when configuration and endpoints were
487 * changed
488 */
489
490 subs->sync_endpoint = NULL;
491 subs->data_endpoint->sync_master = NULL;
492 }
493
Eldad Zacka60945f2013-08-03 10:50:18 +0200494 err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
495 if (err < 0)
496 return err;
497
Manuel Reinhardtbdd44812019-01-31 15:32:35 +0100498 /* endpoint set by quirk */
499 if (err > 0)
500 return 0;
501
Eldad Zackf34d0652013-08-03 10:50:19 +0200502 if (altsd->bNumEndpoints < 2)
503 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200504
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500505 if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
506 attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
Eldad Zackf34d0652013-08-03 10:50:19 +0200507 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
508 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200509
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500510 /*
511 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
512 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
513 * error fall back to SYNC mode and don't create sync endpoint
514 */
515
Eldad Zackf34d0652013-08-03 10:50:19 +0200516 /* check sync-pipe endpoint */
517 /* ... and check descriptor size before accessing bSynchAddress
518 because there is a version of the SB Audigy 2 NX firmware lacking
519 the audio fields in the endpoint descriptors */
520 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
521 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zack95fec882013-08-03 10:50:20 +0200522 get_endpoint(alts, 1)->bSynchAddress != 0)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100523 dev_err(&dev->dev,
524 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
525 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200526 get_endpoint(alts, 1)->bmAttributes,
527 get_endpoint(alts, 1)->bLength,
528 get_endpoint(alts, 1)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500529 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
530 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200531 return -EINVAL;
Daniel Mackedcd3632012-04-12 13:51:12 +0200532 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200533 ep = get_endpoint(alts, 1)->bEndpointAddress;
Eldad Zack95fec882013-08-03 10:50:20 +0200534 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Ard van Breemenca57eca2019-08-02 13:52:14 +0200535 get_endpoint(alts, 0)->bSynchAddress != 0 &&
Eldad Zackf34d0652013-08-03 10:50:19 +0200536 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
537 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100538 dev_err(&dev->dev,
539 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
540 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200541 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500542 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
543 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200544 return -EINVAL;
545 }
546
547 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
548 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
549
550 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
551 alts, ep, !subs->direction,
552 implicit_fb ?
553 SND_USB_ENDPOINT_TYPE_DATA :
554 SND_USB_ENDPOINT_TYPE_SYNC);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500555 if (!subs->sync_endpoint) {
556 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
557 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200558 return -EINVAL;
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500559 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200560
561 subs->data_endpoint->sync_master = subs->sync_endpoint;
Daniel Macke5779992010-03-04 19:46:13 +0100562
Eldad Zack71bb64c2013-08-03 10:50:17 +0200563 return 0;
564}
565
566/*
567 * find a matching format and set up the interface
568 */
569static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
570{
571 struct usb_device *dev = subs->dev;
572 struct usb_host_interface *alts;
573 struct usb_interface_descriptor *altsd;
574 struct usb_interface *iface;
575 int err;
576
577 iface = usb_ifnum_to_if(dev, fmt->iface);
578 if (WARN_ON(!iface))
579 return -EINVAL;
Takashi Iwaib099b962018-05-02 09:36:28 +0200580 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
Johan Hovold70986b22019-12-20 10:31:34 +0100581 if (WARN_ON(!alts))
Eldad Zack71bb64c2013-08-03 10:50:17 +0200582 return -EINVAL;
Johan Hovold70986b22019-12-20 10:31:34 +0100583 altsd = get_iface_desc(alts);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200584
Hui Wang9884a8d2019-12-18 21:26:50 +0800585 if (fmt == subs->cur_audiofmt && !subs->need_setup_fmt)
Eldad Zack71bb64c2013-08-03 10:50:17 +0200586 return 0;
587
588 /* close the old interface */
Hui Wang9884a8d2019-12-18 21:26:50 +0800589 if (subs->interface >= 0 && (subs->interface != fmt->iface || subs->need_setup_fmt)) {
Takashi Iwai8a463222018-05-02 10:04:27 +0200590 if (!subs->stream->chip->keep_iface) {
Hemant Kumar85491422017-08-18 18:44:43 -0700591 err = usb_set_interface_timeout(subs->dev,
592 subs->interface, 0, MAX_SETALT_TIMEOUT_MS);
Takashi Iwai8a463222018-05-02 10:04:27 +0200593 if (err < 0) {
594 dev_err(&dev->dev,
595 "%d:%d: return to setting 0 failed (%d)\n",
596 fmt->iface, fmt->altsetting, err);
597 return -EIO;
598 }
Eldad Zack71bb64c2013-08-03 10:50:17 +0200599 }
600 subs->interface = -1;
601 subs->altset_idx = 0;
602 }
603
Hui Wang9884a8d2019-12-18 21:26:50 +0800604 if (subs->need_setup_fmt)
605 subs->need_setup_fmt = false;
606
Eldad Zack71bb64c2013-08-03 10:50:17 +0200607 /* set interface */
Takashi Iwaib099b962018-05-02 09:36:28 +0200608 if (iface->cur_altsetting != alts) {
Jurgen Kramer6874daa2014-11-28 17:32:54 +0100609 err = snd_usb_select_mode_quirk(subs, fmt);
610 if (err < 0)
611 return -EIO;
612
Hemant Kumar85491422017-08-18 18:44:43 -0700613 err = usb_set_interface_timeout(dev, fmt->iface,
614 fmt->altsetting, MAX_SETALT_TIMEOUT_MS);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200615 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100616 dev_err(&dev->dev,
617 "%d:%d: usb_set_interface failed (%d)\n",
618 fmt->iface, fmt->altsetting, err);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200619 return -EIO;
620 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100621 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
622 fmt->iface, fmt->altsetting);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200623 snd_usb_set_interface_quirk(dev);
624 }
625
Takashi Iwaib099b962018-05-02 09:36:28 +0200626 subs->interface = fmt->iface;
627 subs->altset_idx = fmt->altset_idx;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200628 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
629 alts, fmt->endpoint, subs->direction,
630 SND_USB_ENDPOINT_TYPE_DATA);
631
632 if (!subs->data_endpoint)
633 return -EINVAL;
634
635 err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
636 if (err < 0)
637 return err;
638
Eldad Zackd133f2c2013-08-03 10:50:16 +0200639 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
640 if (err < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100641 return err;
642
643 subs->cur_audiofmt = fmt;
644
645 snd_usb_set_format_quirk(subs, fmt);
646
Daniel Macke5779992010-03-04 19:46:13 +0100647 return 0;
648}
649
Jack Pham968e51f2020-01-15 16:09:44 -0800650static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state);
651
Hemant Kumarf055b382018-09-13 15:52:42 -0700652int snd_usb_enable_audio_stream(struct snd_usb_substream *subs,
653 int datainterval, bool enable)
654{
655 struct audioformat *fmt;
656 struct usb_host_interface *alts;
657 struct usb_interface *iface;
658 int ret;
659
660 if (!enable) {
661 if (subs->interface >= 0) {
Hemant Kumar85491422017-08-18 18:44:43 -0700662 usb_set_interface_timeout(subs->dev, subs->interface, 0,
663 MAX_SETALT_TIMEOUT_MS);
Hemant Kumarf055b382018-09-13 15:52:42 -0700664 subs->altset_idx = 0;
665 subs->interface = -1;
666 subs->cur_audiofmt = NULL;
667 }
668
669 snd_usb_autosuspend(subs->stream->chip);
670 return 0;
671 }
672
673 snd_usb_autoresume(subs->stream->chip);
Jack Pham968e51f2020-01-15 16:09:44 -0800674
675 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
676 if (ret < 0)
677 return ret;
678
Hemant Kumarf055b382018-09-13 15:52:42 -0700679 if (datainterval != -EINVAL)
680 fmt = find_format_and_si(subs, datainterval);
681 else
682 fmt = find_format(subs);
683 if (!fmt) {
684 dev_err(&subs->dev->dev,
685 "cannot set format: format = %#x, rate = %d, channels = %d\n",
686 subs->pcm_format, subs->cur_rate, subs->channels);
687 return -EINVAL;
688 }
689
690 subs->altset_idx = 0;
691 subs->interface = -1;
692 if (atomic_read(&subs->stream->chip->shutdown)) {
693 ret = -ENODEV;
694 } else {
695 ret = set_format(subs, fmt);
696 if (ret < 0)
697 return ret;
698
699 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
700 if (!iface) {
701 dev_err(&subs->dev->dev, "Could not get iface %d\n",
702 subs->cur_audiofmt->iface);
703 return -ENODEV;
704 }
705
706 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
707 ret = snd_usb_init_sample_rate(subs->stream->chip,
708 subs->cur_audiofmt->iface,
709 alts,
710 subs->cur_audiofmt,
711 subs->cur_rate);
712 if (ret < 0) {
713 dev_err(&subs->dev->dev, "failed to set rate %d\n",
714 subs->cur_rate);
715 return ret;
716 }
717 }
718
719 subs->interface = fmt->iface;
720 subs->altset_idx = fmt->altset_idx;
721
722 return 0;
723}
724
Daniel Macke5779992010-03-04 19:46:13 +0100725/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100726 * Return the score of matching two audioformats.
727 * Veto the audioformat if:
728 * - It has no channels for some reason.
729 * - Requested PCM format is not supported.
730 * - Requested sample rate is not supported.
731 */
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100732static int match_endpoint_audioformats(struct snd_usb_substream *subs,
733 struct audioformat *fp,
734 struct audioformat *match, int rate,
735 snd_pcm_format_t pcm_format)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100736{
737 int i;
738 int score = 0;
739
740 if (fp->channels < 1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100741 dev_dbg(&subs->dev->dev,
742 "%s: (fmt @%p) no channels\n", __func__, fp);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100743 return 0;
744 }
745
Eldad Zack74c34ca2013-04-23 01:00:41 +0200746 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100747 dev_dbg(&subs->dev->dev,
748 "%s: (fmt @%p) no match for format %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100749 fp, pcm_format);
750 return 0;
751 }
752
753 for (i = 0; i < fp->nr_rates; i++) {
754 if (fp->rate_table[i] == rate) {
755 score++;
756 break;
757 }
758 }
759 if (!score) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100760 dev_dbg(&subs->dev->dev,
761 "%s: (fmt @%p) no match for rate %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100762 fp, rate);
763 return 0;
764 }
765
766 if (fp->channels == match->channels)
767 score++;
768
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100769 dev_dbg(&subs->dev->dev,
770 "%s: (fmt @%p) score %d\n", __func__, fp, score);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100771
772 return score;
773}
774
775/*
776 * Configure the sync ep using the rate and pcm format of the data ep.
777 */
778static int configure_sync_endpoint(struct snd_usb_substream *subs)
779{
780 int ret;
781 struct audioformat *fp;
782 struct audioformat *sync_fp = NULL;
783 int cur_score = 0;
784 int sync_period_bytes = subs->period_bytes;
785 struct snd_usb_substream *sync_subs =
786 &subs->stream->substream[subs->direction ^ 1];
787
Takashi Iwai31be5422013-01-10 14:06:38 +0100788 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
789 !subs->stream)
790 return snd_usb_endpoint_set_params(subs->sync_endpoint,
791 subs->pcm_format,
792 subs->channels,
793 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400794 0, 0,
Takashi Iwai31be5422013-01-10 14:06:38 +0100795 subs->cur_rate,
796 subs->cur_audiofmt,
797 NULL);
798
Eldad Zack0d9741c2012-12-03 20:30:09 +0100799 /* Try to find the best matching audioformat. */
800 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100801 int score = match_endpoint_audioformats(subs,
802 fp, subs->cur_audiofmt,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100803 subs->cur_rate, subs->pcm_format);
804
805 if (score > cur_score) {
806 sync_fp = fp;
807 cur_score = score;
808 }
809 }
810
811 if (unlikely(sync_fp == NULL)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100812 dev_err(&subs->dev->dev,
813 "%s: no valid audioformat for sync ep %x found\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100814 __func__, sync_subs->ep_num);
815 return -EINVAL;
816 }
817
818 /*
819 * Recalculate the period bytes if channel number differ between
820 * data and sync ep audioformat.
821 */
822 if (sync_fp->channels != subs->channels) {
823 sync_period_bytes = (subs->period_bytes / subs->channels) *
824 sync_fp->channels;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100825 dev_dbg(&subs->dev->dev,
826 "%s: adjusted sync ep period bytes (%d -> %d)\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100827 __func__, subs->period_bytes, sync_period_bytes);
828 }
829
830 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
831 subs->pcm_format,
832 sync_fp->channels,
833 sync_period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400834 0, 0,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100835 subs->cur_rate,
836 sync_fp,
837 NULL);
838
839 return ret;
840}
841
842/*
Dylan Reid61a70952012-09-18 09:49:48 -0700843 * configure endpoint params
844 *
845 * called during initial setup and upon resume
846 */
847static int configure_endpoint(struct snd_usb_substream *subs)
848{
849 int ret;
850
Dylan Reid61a70952012-09-18 09:49:48 -0700851 /* format changed */
Takashi Iwaib0db6062012-11-21 08:35:42 +0100852 stop_endpoints(subs, true);
Dylan Reid61a70952012-09-18 09:49:48 -0700853 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
854 subs->pcm_format,
855 subs->channels,
856 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400857 subs->period_frames,
858 subs->buffer_periods,
Dylan Reid61a70952012-09-18 09:49:48 -0700859 subs->cur_rate,
860 subs->cur_audiofmt,
861 subs->sync_endpoint);
862 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200863 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700864
865 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100866 ret = configure_sync_endpoint(subs);
867
Dylan Reid61a70952012-09-18 09:49:48 -0700868 return ret;
869}
870
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100871static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state)
872{
873 int ret;
874
875 if (!subs->str_pd)
876 return 0;
877
878 ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
879 if (ret < 0) {
880 dev_err(&subs->dev->dev,
881 "Cannot change Power Domain ID: %d to state: %d. Err: %d\n",
882 subs->str_pd->pd_id, state, ret);
883 return ret;
884 }
885
886 return 0;
887}
888
889int snd_usb_pcm_suspend(struct snd_usb_stream *as)
890{
891 int ret;
892
893 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2);
894 if (ret < 0)
895 return ret;
896
897 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2);
898 if (ret < 0)
899 return ret;
900
901 return 0;
902}
903
904int snd_usb_pcm_resume(struct snd_usb_stream *as)
905{
906 int ret;
907
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100908 ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1);
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100909 if (ret < 0)
910 return ret;
911
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100912 ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1);
Jorge Sanjuan3f59aa12018-07-31 13:28:44 +0100913 if (ret < 0)
914 return ret;
915
916 return 0;
917}
918
Dylan Reid61a70952012-09-18 09:49:48 -0700919/*
Daniel Macke5779992010-03-04 19:46:13 +0100920 * hw_params callback
921 *
922 * allocate a buffer and set the given audio format.
923 *
924 * so far we use a physically linear buffer although packetize transfer
925 * doesn't need a continuous area.
926 * if sg buffer is supported on the later version of alsa, we'll follow
927 * that.
928 */
929static int snd_usb_hw_params(struct snd_pcm_substream *substream,
930 struct snd_pcm_hw_params *hw_params)
931{
932 struct snd_usb_substream *subs = substream->runtime->private_data;
933 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700934 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100935
Takashi Iwaif274baa2018-05-27 13:01:17 +0200936 if (snd_usb_use_vmalloc)
937 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
938 params_buffer_bytes(hw_params));
939 else
940 ret = snd_pcm_lib_malloc_pages(substream,
Daniel Macke5779992010-03-04 19:46:13 +0100941 params_buffer_bytes(hw_params));
942 if (ret < 0)
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300943 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100944
Dylan Reid61a70952012-09-18 09:49:48 -0700945 subs->pcm_format = params_format(hw_params);
946 subs->period_bytes = params_period_bytes(hw_params);
Alan Stern976b6c02013-09-24 15:51:58 -0400947 subs->period_frames = params_period_size(hw_params);
948 subs->buffer_periods = params_periods(hw_params);
Dylan Reid61a70952012-09-18 09:49:48 -0700949 subs->channels = params_channels(hw_params);
950 subs->cur_rate = params_rate(hw_params);
951
952 fmt = find_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100953 if (!fmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100954 dev_dbg(&subs->dev->dev,
955 "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700956 subs->pcm_format, subs->cur_rate, subs->channels);
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300957 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100958 }
959
Takashi Iwai47ab1542015-08-25 16:09:00 +0200960 ret = snd_usb_lock_shutdown(subs->stream->chip);
961 if (ret < 0)
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300962 return ret;
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100963
964 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
Takashi Iwai978520b2012-10-12 15:12:55 +0200965 if (ret < 0)
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100966 goto unlock;
967
968 ret = set_format(subs, fmt);
969 if (ret < 0)
970 goto unlock;
Daniel Macke5779992010-03-04 19:46:13 +0100971
Dylan Reid61a70952012-09-18 09:49:48 -0700972 subs->interface = fmt->iface;
973 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200974 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100975
Jorge Sanjuana0a49592018-07-31 13:28:45 +0100976 unlock:
977 snd_usb_unlock_shutdown(subs->stream->chip);
978 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100979}
980
981/*
982 * hw_free callback
983 *
984 * reset the audio format and release the buffer
985 */
986static int snd_usb_hw_free(struct snd_pcm_substream *substream)
987{
988 struct snd_usb_substream *subs = substream->runtime->private_data;
989
990 subs->cur_audiofmt = NULL;
991 subs->cur_rate = 0;
992 subs->period_bytes = 0;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200993 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100994 stop_endpoints(subs, true);
Eldad Zack26de5d02013-10-06 22:31:07 +0200995 snd_usb_endpoint_deactivate(subs->sync_endpoint);
996 snd_usb_endpoint_deactivate(subs->data_endpoint);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200997 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200998 }
Takashi Iwaif274baa2018-05-27 13:01:17 +0200999
1000 if (snd_usb_use_vmalloc)
1001 return snd_pcm_lib_free_vmalloc_buffer(substream);
1002 else
1003 return snd_pcm_lib_free_pages(substream);
Daniel Macke5779992010-03-04 19:46:13 +01001004}
1005
1006/*
1007 * prepare callback
1008 *
1009 * only a few subtle things...
1010 */
1011static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
1012{
1013 struct snd_pcm_runtime *runtime = substream->runtime;
1014 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -07001015 struct usb_host_interface *alts;
1016 struct usb_interface *iface;
1017 int ret;
Daniel Macke5779992010-03-04 19:46:13 +01001018
1019 if (! subs->cur_audiofmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001020 dev_err(&subs->dev->dev, "no format is specified!\n");
Daniel Macke5779992010-03-04 19:46:13 +01001021 return -ENXIO;
1022 }
1023
Takashi Iwai47ab1542015-08-25 16:09:00 +02001024 ret = snd_usb_lock_shutdown(subs->stream->chip);
1025 if (ret < 0)
1026 return ret;
Takashi Iwai978520b2012-10-12 15:12:55 +02001027 if (snd_BUG_ON(!subs->data_endpoint)) {
1028 ret = -EIO;
1029 goto unlock;
1030 }
Daniel Mackedcd3632012-04-12 13:51:12 +02001031
Takashi Iwaif58161b2012-11-08 08:52:45 +01001032 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
1033 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
1034
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001035 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
1036 if (ret < 0)
1037 goto unlock;
1038
Dylan Reid61a70952012-09-18 09:49:48 -07001039 ret = set_format(subs, subs->cur_audiofmt);
1040 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +02001041 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -07001042
Takashi Iwai384dc0852012-09-18 14:49:31 +02001043 if (subs->need_setup_ep) {
Daniel Girnus1e2e3fe2016-12-06 14:46:15 +09001044
1045 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
1046 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
1047 ret = snd_usb_init_sample_rate(subs->stream->chip,
1048 subs->cur_audiofmt->iface,
1049 alts,
1050 subs->cur_audiofmt,
1051 subs->cur_rate);
1052 if (ret < 0)
1053 goto unlock;
1054
Takashi Iwai384dc0852012-09-18 14:49:31 +02001055 ret = configure_endpoint(subs);
1056 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +02001057 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +02001058 subs->need_setup_ep = false;
1059 }
Dylan Reid61a70952012-09-18 09:49:48 -07001060
Daniel Macke5779992010-03-04 19:46:13 +01001061 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +02001062 subs->data_endpoint->maxframesize =
1063 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
1064 subs->data_endpoint->curframesize =
1065 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +01001066
1067 /* reset the pointer */
1068 subs->hwptr_done = 0;
1069 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -05001070 subs->last_delay = 0;
1071 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001072 runtime->delay = 0;
1073
Daniel Mackedcd3632012-04-12 13:51:12 +02001074 /* for playback, submit the URBs now; otherwise, the first hwptr_done
1075 * updates for all URBs would happen at the same time when starting */
1076 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +02001077 ret = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +02001078
Takashi Iwai978520b2012-10-12 15:12:55 +02001079 unlock:
Takashi Iwai47ab1542015-08-25 16:09:00 +02001080 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +02001081 return ret;
Daniel Macke5779992010-03-04 19:46:13 +01001082}
1083
Bhumika Goyalaaffbf72017-08-17 14:45:59 +05301084static const struct snd_pcm_hardware snd_usb_hardware =
Daniel Macke5779992010-03-04 19:46:13 +01001085{
1086 .info = SNDRV_PCM_INFO_MMAP |
1087 SNDRV_PCM_INFO_MMAP_VALID |
1088 SNDRV_PCM_INFO_BATCH |
1089 SNDRV_PCM_INFO_INTERLEAVED |
1090 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1091 SNDRV_PCM_INFO_PAUSE,
1092 .buffer_bytes_max = 1024 * 1024,
1093 .period_bytes_min = 64,
1094 .period_bytes_max = 512 * 1024,
1095 .periods_min = 2,
1096 .periods_max = 1024,
1097};
1098
1099static int hw_check_valid_format(struct snd_usb_substream *subs,
1100 struct snd_pcm_hw_params *params,
1101 struct audioformat *fp)
1102{
1103 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1104 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1105 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1106 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001107 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +01001108 unsigned int ptime;
1109
1110 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001111 snd_mask_none(&check_fmts);
1112 check_fmts.bits[0] = (u32)fp->formats;
1113 check_fmts.bits[1] = (u32)(fp->formats >> 32);
1114 snd_mask_intersect(&check_fmts, fmts);
1115 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +01001116 hwc_debug(" > check: no supported format %d\n", fp->format);
1117 return 0;
1118 }
1119 /* check the channels */
1120 if (fp->channels < ct->min || fp->channels > ct->max) {
1121 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
1122 return 0;
1123 }
1124 /* check the rate is within the range */
1125 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
1126 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
1127 return 0;
1128 }
1129 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
1130 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
1131 return 0;
1132 }
1133 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +02001134 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +01001135 ptime = 125 * (1 << fp->datainterval);
1136 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
1137 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
1138 return 0;
1139 }
1140 }
1141 return 1;
1142}
1143
1144static int hw_rule_rate(struct snd_pcm_hw_params *params,
1145 struct snd_pcm_hw_rule *rule)
1146{
1147 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001148 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001149 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1150 unsigned int rmin, rmax;
1151 int changed;
1152
1153 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1154 changed = 0;
1155 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001156 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001157 if (!hw_check_valid_format(subs, params, fp))
1158 continue;
1159 if (changed++) {
1160 if (rmin > fp->rate_min)
1161 rmin = fp->rate_min;
1162 if (rmax < fp->rate_max)
1163 rmax = fp->rate_max;
1164 } else {
1165 rmin = fp->rate_min;
1166 rmax = fp->rate_max;
1167 }
1168 }
1169
1170 if (!changed) {
1171 hwc_debug(" --> get empty\n");
1172 it->empty = 1;
1173 return -EINVAL;
1174 }
1175
1176 changed = 0;
1177 if (it->min < rmin) {
1178 it->min = rmin;
1179 it->openmin = 0;
1180 changed = 1;
1181 }
1182 if (it->max > rmax) {
1183 it->max = rmax;
1184 it->openmax = 0;
1185 changed = 1;
1186 }
1187 if (snd_interval_checkempty(it)) {
1188 it->empty = 1;
1189 return -EINVAL;
1190 }
1191 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1192 return changed;
1193}
1194
1195
1196static int hw_rule_channels(struct snd_pcm_hw_params *params,
1197 struct snd_pcm_hw_rule *rule)
1198{
1199 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001200 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001201 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1202 unsigned int rmin, rmax;
1203 int changed;
1204
1205 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1206 changed = 0;
1207 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001208 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001209 if (!hw_check_valid_format(subs, params, fp))
1210 continue;
1211 if (changed++) {
1212 if (rmin > fp->channels)
1213 rmin = fp->channels;
1214 if (rmax < fp->channels)
1215 rmax = fp->channels;
1216 } else {
1217 rmin = fp->channels;
1218 rmax = fp->channels;
1219 }
1220 }
1221
1222 if (!changed) {
1223 hwc_debug(" --> get empty\n");
1224 it->empty = 1;
1225 return -EINVAL;
1226 }
1227
1228 changed = 0;
1229 if (it->min < rmin) {
1230 it->min = rmin;
1231 it->openmin = 0;
1232 changed = 1;
1233 }
1234 if (it->max > rmax) {
1235 it->max = rmax;
1236 it->openmax = 0;
1237 changed = 1;
1238 }
1239 if (snd_interval_checkempty(it)) {
1240 it->empty = 1;
1241 return -EINVAL;
1242 }
1243 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1244 return changed;
1245}
1246
1247static int hw_rule_format(struct snd_pcm_hw_params *params,
1248 struct snd_pcm_hw_rule *rule)
1249{
1250 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001251 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001252 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1253 u64 fbits;
1254 u32 oldbits[2];
1255 int changed;
1256
1257 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1258 fbits = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001259 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001260 if (!hw_check_valid_format(subs, params, fp))
1261 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001262 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +01001263 }
1264
1265 oldbits[0] = fmt->bits[0];
1266 oldbits[1] = fmt->bits[1];
1267 fmt->bits[0] &= (u32)fbits;
1268 fmt->bits[1] &= (u32)(fbits >> 32);
1269 if (!fmt->bits[0] && !fmt->bits[1]) {
1270 hwc_debug(" --> get empty\n");
1271 return -EINVAL;
1272 }
1273 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1274 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1275 return changed;
1276}
1277
1278static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1279 struct snd_pcm_hw_rule *rule)
1280{
1281 struct snd_usb_substream *subs = rule->private;
1282 struct audioformat *fp;
1283 struct snd_interval *it;
1284 unsigned char min_datainterval;
1285 unsigned int pmin;
1286 int changed;
1287
1288 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1289 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1290 min_datainterval = 0xff;
1291 list_for_each_entry(fp, &subs->fmt_list, list) {
1292 if (!hw_check_valid_format(subs, params, fp))
1293 continue;
1294 min_datainterval = min(min_datainterval, fp->datainterval);
1295 }
1296 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +02001297 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +01001298 it->empty = 1;
1299 return -EINVAL;
1300 }
1301 pmin = 125 * (1 << min_datainterval);
1302 changed = 0;
1303 if (it->min < pmin) {
1304 it->min = pmin;
1305 it->openmin = 0;
1306 changed = 1;
1307 }
1308 if (snd_interval_checkempty(it)) {
1309 it->empty = 1;
1310 return -EINVAL;
1311 }
1312 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1313 return changed;
1314}
1315
1316/*
1317 * If the device supports unusual bit rates, does the request meet these?
1318 */
1319static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1320 struct snd_usb_substream *subs)
1321{
1322 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001323 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +01001324 int count = 0, needs_knot = 0;
1325 int err;
1326
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +02001327 kfree(subs->rate_list.list);
1328 subs->rate_list.list = NULL;
1329
Daniel Macke5779992010-03-04 19:46:13 +01001330 list_for_each_entry(fp, &subs->fmt_list, list) {
1331 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1332 return 0;
1333 count += fp->nr_rates;
1334 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1335 needs_knot = 1;
1336 }
1337 if (!needs_knot)
1338 return 0;
1339
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001340 subs->rate_list.list = rate_list =
Kees Cook6da2ec52018-06-12 13:55:00 -07001341 kmalloc_array(count, sizeof(int), GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +02001342 if (!subs->rate_list.list)
1343 return -ENOMEM;
1344 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +01001345 subs->rate_list.mask = 0;
1346 count = 0;
1347 list_for_each_entry(fp, &subs->fmt_list, list) {
1348 int i;
1349 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001350 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +01001351 }
1352 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1353 &subs->rate_list);
1354 if (err < 0)
1355 return err;
1356
1357 return 0;
1358}
1359
1360
1361/*
1362 * set up the runtime hardware information.
1363 */
1364
1365static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1366{
Eldad Zack88766f02013-04-03 23:18:49 +02001367 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001368 unsigned int pt, ptmin;
1369 int param_period_time_if_needed;
1370 int err;
1371
1372 runtime->hw.formats = subs->formats;
1373
1374 runtime->hw.rate_min = 0x7fffffff;
1375 runtime->hw.rate_max = 0;
1376 runtime->hw.channels_min = 256;
1377 runtime->hw.channels_max = 0;
1378 runtime->hw.rates = 0;
1379 ptmin = UINT_MAX;
1380 /* check min/max rates and channels */
Eldad Zack88766f02013-04-03 23:18:49 +02001381 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001382 runtime->hw.rates |= fp->rates;
1383 if (runtime->hw.rate_min > fp->rate_min)
1384 runtime->hw.rate_min = fp->rate_min;
1385 if (runtime->hw.rate_max < fp->rate_max)
1386 runtime->hw.rate_max = fp->rate_max;
1387 if (runtime->hw.channels_min > fp->channels)
1388 runtime->hw.channels_min = fp->channels;
1389 if (runtime->hw.channels_max < fp->channels)
1390 runtime->hw.channels_max = fp->channels;
1391 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1392 /* FIXME: there might be more than one audio formats... */
1393 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1394 fp->frame_size;
1395 }
1396 pt = 125 * (1 << fp->datainterval);
1397 ptmin = min(ptmin, pt);
1398 }
1399
1400 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001401 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001402 /* full speed devices have fixed data packet interval */
1403 ptmin = 1000;
1404 if (ptmin == 1000)
1405 /* if period time doesn't go below 1 ms, no rules needed */
1406 param_period_time_if_needed = -1;
Daniel Macke5779992010-03-04 19:46:13 +01001407
Takashi Iwaie92be812018-05-27 15:09:15 +02001408 err = snd_pcm_hw_constraint_minmax(runtime,
1409 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1410 ptmin, UINT_MAX);
1411 if (err < 0)
1412 return err;
1413
1414 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1415 hw_rule_rate, subs,
1416 SNDRV_PCM_HW_PARAM_FORMAT,
1417 SNDRV_PCM_HW_PARAM_CHANNELS,
1418 param_period_time_if_needed,
1419 -1);
1420 if (err < 0)
1421 return err;
1422 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1423 hw_rule_channels, subs,
1424 SNDRV_PCM_HW_PARAM_FORMAT,
1425 SNDRV_PCM_HW_PARAM_RATE,
1426 param_period_time_if_needed,
1427 -1);
1428 if (err < 0)
1429 return err;
1430 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1431 hw_rule_format, subs,
1432 SNDRV_PCM_HW_PARAM_RATE,
1433 SNDRV_PCM_HW_PARAM_CHANNELS,
1434 param_period_time_if_needed,
1435 -1);
1436 if (err < 0)
1437 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001438 if (param_period_time_if_needed >= 0) {
1439 err = snd_pcm_hw_rule_add(runtime, 0,
1440 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1441 hw_rule_period_time, subs,
1442 SNDRV_PCM_HW_PARAM_FORMAT,
1443 SNDRV_PCM_HW_PARAM_CHANNELS,
1444 SNDRV_PCM_HW_PARAM_RATE,
1445 -1);
1446 if (err < 0)
Takashi Iwaie92be812018-05-27 15:09:15 +02001447 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001448 }
Takashi Iwaie92be812018-05-27 15:09:15 +02001449 err = snd_usb_pcm_check_knot(runtime, subs);
1450 if (err < 0)
1451 return err;
Oliver Neukum88a85162011-03-11 14:51:12 +01001452
Takashi Iwaie92be812018-05-27 15:09:15 +02001453 return snd_usb_autoresume(subs->stream->chip);
Daniel Macke5779992010-03-04 19:46:13 +01001454}
1455
Takashi Iwai6fddc792018-05-27 13:59:03 +02001456static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
Daniel Macke5779992010-03-04 19:46:13 +01001457{
Takashi Iwai6fddc792018-05-27 13:59:03 +02001458 int direction = substream->stream;
Daniel Macke5779992010-03-04 19:46:13 +01001459 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1460 struct snd_pcm_runtime *runtime = substream->runtime;
1461 struct snd_usb_substream *subs = &as->substream[direction];
1462
1463 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001464 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001465 runtime->hw = snd_usb_hardware;
1466 runtime->private_data = subs;
1467 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001468 /* runtime PM is also done there */
Daniel Mackd24f5062013-04-17 00:01:38 +08001469
1470 /* initialize DSD/DOP context */
1471 subs->dsd_dop.byte_idx = 0;
1472 subs->dsd_dop.channel = 0;
1473 subs->dsd_dop.marker = 1;
1474
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -03001475 return setup_hw_info(runtime, subs);
Daniel Macke5779992010-03-04 19:46:13 +01001476}
1477
Takashi Iwai6fddc792018-05-27 13:59:03 +02001478static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
Daniel Macke5779992010-03-04 19:46:13 +01001479{
Takashi Iwai6fddc792018-05-27 13:59:03 +02001480 int direction = substream->stream;
Daniel Macke5779992010-03-04 19:46:13 +01001481 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1482 struct snd_usb_substream *subs = &as->substream[direction];
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001483 int ret;
Daniel Macke5779992010-03-04 19:46:13 +01001484
Takashi Iwaib0db6062012-11-21 08:35:42 +01001485 stop_endpoints(subs, true);
Daniel Mack68e67f42012-07-12 13:08:40 +02001486
Takashi Iwai8a463222018-05-02 10:04:27 +02001487 if (!as->chip->keep_iface &&
1488 subs->interface >= 0 &&
Takashi Iwai47ab1542015-08-25 16:09:00 +02001489 !snd_usb_lock_shutdown(subs->stream->chip)) {
Daniel Mack68e67f42012-07-12 13:08:40 +02001490 usb_set_interface(subs->dev, subs->interface, 0);
1491 subs->interface = -1;
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001492 ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
Takashi Iwai47ab1542015-08-25 16:09:00 +02001493 snd_usb_unlock_shutdown(subs->stream->chip);
Jorge Sanjuana0a49592018-07-31 13:28:45 +01001494 if (ret < 0)
1495 return ret;
Daniel Mack68e67f42012-07-12 13:08:40 +02001496 }
1497
Daniel Macke5779992010-03-04 19:46:13 +01001498 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001499 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001500
Daniel Mack68e67f42012-07-12 13:08:40 +02001501 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001502}
1503
1504/* Since a URB can handle only a single linear buffer, we must use double
1505 * buffering when the data to be transferred overflows the buffer boundary.
1506 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1507 * for all URBs.
1508 */
1509static void retire_capture_urb(struct snd_usb_substream *subs,
1510 struct urb *urb)
1511{
1512 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1513 unsigned int stride, frames, bytes, oldptr;
1514 int i, period_elapsed = 0;
1515 unsigned long flags;
1516 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001517 int current_frame_number;
1518
1519 /* read frame number here, update pointer in critical section */
1520 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001521
1522 stride = runtime->frame_bits >> 3;
1523
1524 for (i = 0; i < urb->number_of_packets; i++) {
Calvin Owens1539d4f2013-04-12 22:33:59 -05001525 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
Daniel Mackedcd3632012-04-12 13:51:12 +02001526 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001527 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1528 i, urb->iso_frame_desc[i].status);
Daniel Mackedcd3632012-04-12 13:51:12 +02001529 // continue;
1530 }
1531 bytes = urb->iso_frame_desc[i].actual_length;
1532 frames = bytes / stride;
1533 if (!subs->txfr_quirk)
1534 bytes = frames * stride;
1535 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001536 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001537 bytes = frames * stride;
Takashi Iwai377a8792018-05-16 20:07:18 +02001538 dev_warn_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001539 "Corrected urb data len. %d->%d\n",
Daniel Mackedcd3632012-04-12 13:51:12 +02001540 oldbytes, bytes);
1541 }
1542 /* update the current pointer */
1543 spin_lock_irqsave(&subs->lock, flags);
1544 oldptr = subs->hwptr_done;
1545 subs->hwptr_done += bytes;
1546 if (subs->hwptr_done >= runtime->buffer_size * stride)
1547 subs->hwptr_done -= runtime->buffer_size * stride;
1548 frames = (bytes + (oldptr % stride)) / stride;
1549 subs->transfer_done += frames;
1550 if (subs->transfer_done >= runtime->period_size) {
1551 subs->transfer_done -= runtime->period_size;
1552 period_elapsed = 1;
1553 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001554 /* capture delay is by construction limited to one URB,
1555 * reset delays here
1556 */
1557 runtime->delay = subs->last_delay = 0;
1558
1559 /* realign last_frame_number */
1560 subs->last_frame_number = current_frame_number;
1561 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1562
Daniel Mackedcd3632012-04-12 13:51:12 +02001563 spin_unlock_irqrestore(&subs->lock, flags);
1564 /* copy a data chunk */
1565 if (oldptr + bytes > runtime->buffer_size * stride) {
1566 unsigned int bytes1 =
1567 runtime->buffer_size * stride - oldptr;
1568 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1569 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1570 } else {
1571 memcpy(runtime->dma_area + oldptr, cp, bytes);
1572 }
1573 }
1574
1575 if (period_elapsed)
1576 snd_pcm_period_elapsed(subs->pcm_substream);
1577}
1578
Daniel Mackd24f5062013-04-17 00:01:38 +08001579static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1580 struct urb *urb, unsigned int bytes)
1581{
1582 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1583 unsigned int stride = runtime->frame_bits >> 3;
1584 unsigned int dst_idx = 0;
1585 unsigned int src_idx = subs->hwptr_done;
1586 unsigned int wrap = runtime->buffer_size * stride;
1587 u8 *dst = urb->transfer_buffer;
1588 u8 *src = runtime->dma_area;
1589 u8 marker[] = { 0x05, 0xfa };
1590
1591 /*
1592 * The DSP DOP format defines a way to transport DSD samples over
1593 * normal PCM data endpoints. It requires stuffing of marker bytes
1594 * (0x05 and 0xfa, alternating per sample frame), and then expects
1595 * 2 additional bytes of actual payload. The whole frame is stored
1596 * LSB.
1597 *
1598 * Hence, for a stereo transport, the buffer layout looks like this,
1599 * where L refers to left channel samples and R to right.
1600 *
1601 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1602 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1603 * .....
1604 *
1605 */
1606
1607 while (bytes--) {
1608 if (++subs->dsd_dop.byte_idx == 3) {
1609 /* frame boundary? */
1610 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1611 src_idx += 2;
1612 subs->dsd_dop.byte_idx = 0;
1613
1614 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1615 /* alternate the marker */
1616 subs->dsd_dop.marker++;
1617 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1618 subs->dsd_dop.channel = 0;
1619 }
1620 } else {
1621 /* stuff the DSD payload */
1622 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001623
1624 if (subs->cur_audiofmt->dsd_bitrev)
1625 dst[dst_idx++] = bitrev8(src[idx]);
1626 else
1627 dst[dst_idx++] = src[idx];
1628
Daniel Mackd24f5062013-04-17 00:01:38 +08001629 subs->hwptr_done++;
1630 }
1631 }
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001632 if (subs->hwptr_done >= runtime->buffer_size * stride)
1633 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001634}
1635
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001636static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1637 int offset, int stride, unsigned int bytes)
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001638{
1639 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1640
1641 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1642 /* err, the transferred area goes over buffer boundary. */
1643 unsigned int bytes1 =
1644 runtime->buffer_size * stride - subs->hwptr_done;
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001645 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001646 runtime->dma_area + subs->hwptr_done, bytes1);
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001647 memcpy(urb->transfer_buffer + offset + bytes1,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001648 runtime->dma_area, bytes - bytes1);
1649 } else {
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001650 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001651 runtime->dma_area + subs->hwptr_done, bytes);
1652 }
1653 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001654 if (subs->hwptr_done >= runtime->buffer_size * stride)
1655 subs->hwptr_done -= runtime->buffer_size * stride;
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001656}
1657
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001658static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1659 struct urb *urb, int stride,
1660 unsigned int bytes)
1661{
1662 __le32 packet_length;
1663 int i;
1664
1665 /* Put __le32 length descriptor at start of each packet. */
1666 for (i = 0; i < urb->number_of_packets; i++) {
1667 unsigned int length = urb->iso_frame_desc[i].length;
1668 unsigned int offset = urb->iso_frame_desc[i].offset;
1669
1670 packet_length = cpu_to_le32(length);
1671 offset += i * sizeof(packet_length);
1672 urb->iso_frame_desc[i].offset = offset;
1673 urb->iso_frame_desc[i].length += sizeof(packet_length);
1674 memcpy(urb->transfer_buffer + offset,
1675 &packet_length, sizeof(packet_length));
1676 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1677 stride, length);
1678 }
1679 /* Adjust transfer size accordingly. */
1680 bytes += urb->number_of_packets * sizeof(packet_length);
1681 return bytes;
1682}
1683
Daniel Mackedcd3632012-04-12 13:51:12 +02001684static void prepare_playback_urb(struct snd_usb_substream *subs,
1685 struct urb *urb)
1686{
1687 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001688 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001689 struct snd_urb_ctx *ctx = urb->context;
1690 unsigned int counts, frames, bytes;
1691 int i, stride, period_elapsed = 0;
1692 unsigned long flags;
1693
1694 stride = runtime->frame_bits >> 3;
1695
1696 frames = 0;
1697 urb->number_of_packets = 0;
1698 spin_lock_irqsave(&subs->lock, flags);
Alan Stern976b6c02013-09-24 15:51:58 -04001699 subs->frame_limit += ep->max_urb_frames;
Daniel Mackedcd3632012-04-12 13:51:12 +02001700 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001701 if (ctx->packet_size[i])
1702 counts = ctx->packet_size[i];
1703 else
1704 counts = snd_usb_endpoint_next_packet_size(ep);
1705
Daniel Mackedcd3632012-04-12 13:51:12 +02001706 /* set up descriptor */
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001707 urb->iso_frame_desc[i].offset = frames * ep->stride;
1708 urb->iso_frame_desc[i].length = counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001709 frames += counts;
1710 urb->number_of_packets++;
1711 subs->transfer_done += counts;
1712 if (subs->transfer_done >= runtime->period_size) {
1713 subs->transfer_done -= runtime->period_size;
Alan Stern976b6c02013-09-24 15:51:58 -04001714 subs->frame_limit = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001715 period_elapsed = 1;
1716 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1717 if (subs->transfer_done > 0) {
1718 /* FIXME: fill-max mode is not
1719 * supported yet */
1720 frames -= subs->transfer_done;
1721 counts -= subs->transfer_done;
1722 urb->iso_frame_desc[i].length =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001723 counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001724 subs->transfer_done = 0;
1725 }
1726 i++;
1727 if (i < ctx->packets) {
1728 /* add a transfer delimiter */
1729 urb->iso_frame_desc[i].offset =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001730 frames * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001731 urb->iso_frame_desc[i].length = 0;
1732 urb->number_of_packets++;
1733 }
1734 break;
1735 }
1736 }
Alan Stern976b6c02013-09-24 15:51:58 -04001737 /* finish at the period boundary or after enough frames */
1738 if ((period_elapsed ||
1739 subs->transfer_done >= subs->frame_limit) &&
1740 !snd_usb_endpoint_implicit_feedback_sink(ep))
Daniel Mackedcd3632012-04-12 13:51:12 +02001741 break;
1742 }
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001743 bytes = frames * ep->stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001744
1745 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1746 subs->cur_audiofmt->dsd_dop)) {
1747 fill_playback_urb_dsd_dop(subs, urb, bytes);
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001748 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1749 subs->cur_audiofmt->dsd_bitrev)) {
1750 /* bit-reverse the bytes */
1751 u8 *buf = urb->transfer_buffer;
1752 for (i = 0; i < bytes; i++) {
1753 int idx = (subs->hwptr_done + i)
1754 % (runtime->buffer_size * stride);
1755 buf[i] = bitrev8(runtime->dma_area[idx]);
1756 }
1757
1758 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001759 if (subs->hwptr_done >= runtime->buffer_size * stride)
1760 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001761 } else {
Daniel Mackd24f5062013-04-17 00:01:38 +08001762 /* usual PCM */
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001763 if (!subs->tx_length_quirk)
1764 copy_to_urb(subs, urb, 0, stride, bytes);
1765 else
1766 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1767 /* bytes is now amount of outgoing data */
Daniel Mackedcd3632012-04-12 13:51:12 +02001768 }
Daniel Mackd24f5062013-04-17 00:01:38 +08001769
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001770 /* update delay with exact number of samples queued */
1771 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001772 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001773 subs->last_delay = runtime->delay;
1774
1775 /* realign last_frame_number */
1776 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1777 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1778
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001779 if (subs->trigger_tstamp_pending_update) {
1780 /* this is the first actual URB submitted,
1781 * update trigger timestamp to reflect actual start time
1782 */
1783 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1784 subs->trigger_tstamp_pending_update = false;
1785 }
1786
Daniel Mackedcd3632012-04-12 13:51:12 +02001787 spin_unlock_irqrestore(&subs->lock, flags);
1788 urb->transfer_buffer_length = bytes;
1789 if (period_elapsed)
1790 snd_pcm_period_elapsed(subs->pcm_substream);
1791}
1792
1793/*
1794 * process after playback data complete
1795 * - decrease the delay count again
1796 */
1797static void retire_playback_urb(struct snd_usb_substream *subs,
1798 struct urb *urb)
1799{
1800 unsigned long flags;
1801 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001802 struct snd_usb_endpoint *ep = subs->data_endpoint;
1803 int processed = urb->transfer_buffer_length / ep->stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001804 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001805
Takashi Iwai1213a202012-09-06 14:58:00 +02001806 /* ignore the delay accounting when procssed=0 is given, i.e.
1807 * silent payloads are procssed before handling the actual data
1808 */
1809 if (!processed)
1810 return;
1811
Daniel Mackedcd3632012-04-12 13:51:12 +02001812 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001813 if (!subs->last_delay)
1814 goto out; /* short path */
1815
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001816 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1817 /* update delay with exact number of samples played */
1818 if (processed > subs->last_delay)
1819 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001820 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001821 subs->last_delay -= processed;
1822 runtime->delay = subs->last_delay;
1823
1824 /*
1825 * Report when delay estimate is off by more than 2ms.
1826 * The error should be lower than 2ms since the estimate relies
1827 * on two reads of a counter updated every ms.
1828 */
Sander Eikelenboomb7a77232014-05-02 15:09:27 +02001829 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1830 dev_dbg_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001831 "delay: estimated %d, actual %d\n",
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001832 est_delay, subs->last_delay);
1833
Takashi Iwai48779a02012-11-23 16:00:37 +01001834 if (!subs->running) {
1835 /* update last_frame_number for delay counting here since
1836 * prepare_playback_urb won't be called during pause
1837 */
1838 subs->last_frame_number =
1839 usb_get_current_frame_number(subs->dev) & 0xff;
1840 }
1841
1842 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001843 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001844}
1845
Daniel Mackedcd3632012-04-12 13:51:12 +02001846static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1847 int cmd)
1848{
1849 struct snd_usb_substream *subs = substream->runtime->private_data;
1850
1851 switch (cmd) {
1852 case SNDRV_PCM_TRIGGER_START:
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001853 subs->trigger_tstamp_pending_update = true;
Gustavo A. R. Silvad5e77fc2018-08-01 11:14:16 -05001854 /* fall through */
Daniel Mackedcd3632012-04-12 13:51:12 +02001855 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1856 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1857 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001858 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001859 return 0;
1860 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001861 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001862 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001863 return 0;
1864 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1865 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001866 /* keep retire_data_urb for delay calculation */
1867 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001868 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001869 return 0;
Hui Wang9884a8d2019-12-18 21:26:50 +08001870 case SNDRV_PCM_TRIGGER_SUSPEND:
1871 if (subs->stream->chip->setup_fmt_after_resume_quirk) {
1872 stop_endpoints(subs, true);
1873 subs->need_setup_fmt = true;
1874 return 0;
1875 }
1876 break;
Daniel Mackedcd3632012-04-12 13:51:12 +02001877 }
1878
1879 return -EINVAL;
1880}
1881
Daniel Mackafe25962012-06-16 16:58:04 +02001882static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1883 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001884{
1885 int err;
1886 struct snd_usb_substream *subs = substream->runtime->private_data;
1887
1888 switch (cmd) {
1889 case SNDRV_PCM_TRIGGER_START:
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +02001890 err = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +02001891 if (err < 0)
1892 return err;
1893
1894 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001895 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001896 return 0;
1897 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001898 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001899 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001900 return 0;
1901 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1902 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001903 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001904 return 0;
1905 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1906 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001907 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001908 return 0;
Hui Wang9884a8d2019-12-18 21:26:50 +08001909 case SNDRV_PCM_TRIGGER_SUSPEND:
1910 if (subs->stream->chip->setup_fmt_after_resume_quirk) {
1911 stop_endpoints(subs, true);
1912 subs->need_setup_fmt = true;
1913 return 0;
1914 }
1915 break;
Daniel Mackedcd3632012-04-12 13:51:12 +02001916 }
1917
1918 return -EINVAL;
1919}
1920
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301921static const struct snd_pcm_ops snd_usb_playback_ops = {
Takashi Iwai6fddc792018-05-27 13:59:03 +02001922 .open = snd_usb_pcm_open,
1923 .close = snd_usb_pcm_close,
Daniel Macke5779992010-03-04 19:46:13 +01001924 .ioctl = snd_pcm_lib_ioctl,
1925 .hw_params = snd_usb_hw_params,
1926 .hw_free = snd_usb_hw_free,
1927 .prepare = snd_usb_pcm_prepare,
1928 .trigger = snd_usb_substream_playback_trigger,
1929 .pointer = snd_usb_pcm_pointer,
1930 .page = snd_pcm_lib_get_vmalloc_page,
Daniel Macke5779992010-03-04 19:46:13 +01001931};
1932
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301933static const struct snd_pcm_ops snd_usb_capture_ops = {
Takashi Iwai6fddc792018-05-27 13:59:03 +02001934 .open = snd_usb_pcm_open,
1935 .close = snd_usb_pcm_close,
Daniel Macke5779992010-03-04 19:46:13 +01001936 .ioctl = snd_pcm_lib_ioctl,
1937 .hw_params = snd_usb_hw_params,
1938 .hw_free = snd_usb_hw_free,
1939 .prepare = snd_usb_pcm_prepare,
1940 .trigger = snd_usb_substream_capture_trigger,
1941 .pointer = snd_usb_pcm_pointer,
1942 .page = snd_pcm_lib_get_vmalloc_page,
Daniel Macke5779992010-03-04 19:46:13 +01001943};
1944
Takashi Iwaif274baa2018-05-27 13:01:17 +02001945static const struct snd_pcm_ops snd_usb_playback_dev_ops = {
1946 .open = snd_usb_pcm_open,
1947 .close = snd_usb_pcm_close,
1948 .ioctl = snd_pcm_lib_ioctl,
1949 .hw_params = snd_usb_hw_params,
1950 .hw_free = snd_usb_hw_free,
1951 .prepare = snd_usb_pcm_prepare,
1952 .trigger = snd_usb_substream_playback_trigger,
1953 .pointer = snd_usb_pcm_pointer,
1954 .page = snd_pcm_sgbuf_ops_page,
1955};
1956
1957static const struct snd_pcm_ops snd_usb_capture_dev_ops = {
1958 .open = snd_usb_pcm_open,
1959 .close = snd_usb_pcm_close,
1960 .ioctl = snd_pcm_lib_ioctl,
1961 .hw_params = snd_usb_hw_params,
1962 .hw_free = snd_usb_hw_free,
1963 .prepare = snd_usb_pcm_prepare,
1964 .trigger = snd_usb_substream_capture_trigger,
1965 .pointer = snd_usb_pcm_pointer,
1966 .page = snd_pcm_sgbuf_ops_page,
1967};
1968
Daniel Macke5779992010-03-04 19:46:13 +01001969void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1970{
Takashi Iwaif274baa2018-05-27 13:01:17 +02001971 const struct snd_pcm_ops *ops;
1972
1973 if (snd_usb_use_vmalloc)
1974 ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
1975 &snd_usb_playback_ops : &snd_usb_capture_ops;
1976 else
1977 ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
1978 &snd_usb_playback_dev_ops : &snd_usb_capture_dev_ops;
1979 snd_pcm_set_ops(pcm, stream, ops);
1980}
1981
1982void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
1983{
1984 struct snd_pcm *pcm = subs->stream->pcm;
1985 struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
1986 struct device *dev = subs->dev->bus->controller;
1987
1988 if (!snd_usb_use_vmalloc)
1989 snd_pcm_lib_preallocate_pages(s, SNDRV_DMA_TYPE_DEV_SG,
1990 dev, 64*1024, 512*1024);
Daniel Macke5779992010-03-04 19:46:13 +01001991}