blob: dd8157448bc6e9de9729200ff3ef47b00e29a740 [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 Kumard4101882017-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{
81 struct snd_usb_substream *subs;
82 unsigned int hwptr_done;
83
84 subs = (struct snd_usb_substream *)substream->runtime->private_data;
Takashi Iwai47ab1542015-08-25 16:09:00 +020085 if (atomic_read(&subs->stream->chip->shutdown))
Takashi Iwai978520b2012-10-12 15:12:55 +020086 return SNDRV_PCM_POS_XRUN;
Daniel Macke5779992010-03-04 19:46:13 +010087 spin_lock(&subs->lock);
88 hwptr_done = subs->hwptr_done;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060089 substream->runtime->delay = snd_usb_pcm_delay(subs,
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050090 substream->runtime->rate);
Daniel Macke5779992010-03-04 19:46:13 +010091 spin_unlock(&subs->lock);
92 return hwptr_done / (substream->runtime->frame_bits >> 3);
93}
94
95/*
96 * find a matching audio format
97 */
Dylan Reid61a70952012-09-18 09:49:48 -070098static struct audioformat *find_format(struct snd_usb_substream *subs)
Daniel Macke5779992010-03-04 19:46:13 +010099{
Eldad Zack88766f02013-04-03 23:18:49 +0200100 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100101 struct audioformat *found = NULL;
102 int cur_attr = 0, attr;
103
Eldad Zack88766f02013-04-03 23:18:49 +0200104 list_for_each_entry(fp, &subs->fmt_list, list) {
Eldad Zack74c34ca2013-04-23 01:00:41 +0200105 if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100106 continue;
Dylan Reid61a70952012-09-18 09:49:48 -0700107 if (fp->channels != subs->channels)
Daniel Macke5779992010-03-04 19:46:13 +0100108 continue;
Dylan Reid61a70952012-09-18 09:49:48 -0700109 if (subs->cur_rate < fp->rate_min ||
110 subs->cur_rate > fp->rate_max)
Daniel Macke5779992010-03-04 19:46:13 +0100111 continue;
112 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
113 unsigned int i;
114 for (i = 0; i < fp->nr_rates; i++)
Dylan Reid61a70952012-09-18 09:49:48 -0700115 if (fp->rate_table[i] == subs->cur_rate)
Daniel Macke5779992010-03-04 19:46:13 +0100116 break;
117 if (i >= fp->nr_rates)
118 continue;
119 }
120 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
121 if (! found) {
122 found = fp;
123 cur_attr = attr;
124 continue;
125 }
126 /* avoid async out and adaptive in if the other method
127 * supports the same format.
128 * this is a workaround for the case like
129 * M-audio audiophile USB.
130 */
131 if (attr != cur_attr) {
132 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
133 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
134 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
135 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
136 continue;
137 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
138 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
139 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
140 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
141 found = fp;
142 cur_attr = attr;
143 continue;
144 }
145 }
146 /* find the format with the largest max. packet size */
147 if (fp->maxpacksize > found->maxpacksize) {
148 found = fp;
149 cur_attr = attr;
150 }
151 }
152 return found;
153}
154
Daniel Mack767d75a2010-03-04 19:46:17 +0100155static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
156 struct usb_host_interface *alts,
157 struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +0100158{
Daniel Mack767d75a2010-03-04 19:46:17 +0100159 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100160 unsigned int ep;
161 unsigned char data[1];
162 int err;
163
Takashi Iwai447d6272016-03-15 15:20:58 +0100164 if (get_iface_desc(alts)->bNumEndpoints < 1)
165 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100166 ep = get_endpoint(alts, 0)->bEndpointAddress;
Daniel Mack767d75a2010-03-04 19:46:17 +0100167
Daniel Mack767d75a2010-03-04 19:46:17 +0100168 data[0] = 1;
169 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
170 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
171 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200172 data, sizeof(data))) < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100173 usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
174 iface, ep);
Daniel Mack767d75a2010-03-04 19:46:17 +0100175 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100176 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100177
Daniel Macke5779992010-03-04 19:46:13 +0100178 return 0;
179}
180
Daniel Mack92c25612010-05-26 18:11:39 +0200181static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
182 struct usb_host_interface *alts,
183 struct audioformat *fmt)
184{
185 struct usb_device *dev = chip->dev;
186 unsigned char data[1];
Daniel Mack92c25612010-05-26 18:11:39 +0200187 int err;
188
Daniel Mack92c25612010-05-26 18:11:39 +0200189 data[0] = 1;
190 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
191 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
192 UAC2_EP_CS_PITCH << 8, 0,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200193 data, sizeof(data))) < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100194 usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
195 iface, fmt->altsetting);
Daniel Mack92c25612010-05-26 18:11:39 +0200196 return err;
197 }
198
199 return 0;
200}
201
Daniel Mack767d75a2010-03-04 19:46:17 +0100202/*
Daniel Mack92c25612010-05-26 18:11:39 +0200203 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100204 */
205int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
206 struct usb_host_interface *alts,
207 struct audioformat *fmt)
208{
Daniel Mack92c25612010-05-26 18:11:39 +0200209 /* if endpoint doesn't have pitch control, bail out */
210 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
211 return 0;
212
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100213 switch (fmt->protocol) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100214 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200215 default:
Daniel Mack767d75a2010-03-04 19:46:17 +0100216 return init_pitch_v1(chip, iface, alts, fmt);
217
218 case UAC_VERSION_2:
Daniel Mack92c25612010-05-26 18:11:39 +0200219 return init_pitch_v2(chip, iface, alts, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100220 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100221}
222
Ioan-Adrian Ratiube4e3ae2017-01-05 00:37:46 +0200223static int start_endpoints(struct snd_usb_substream *subs)
Daniel Mackedcd3632012-04-12 13:51:12 +0200224{
225 int err;
226
227 if (!subs->data_endpoint)
228 return -EINVAL;
229
230 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
231 struct snd_usb_endpoint *ep = subs->data_endpoint;
232
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700233 dev_dbg(&subs->dev->dev, "Starting data EP @%pK\n", ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200234
235 ep->data_subs = subs;
Ioan-Adrian Ratiube4e3ae2017-01-05 00:37:46 +0200236 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200237 if (err < 0) {
238 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
239 return err;
240 }
241 }
242
243 if (subs->sync_endpoint &&
244 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
245 struct snd_usb_endpoint *ep = subs->sync_endpoint;
246
Daniel Mack2e4a2632012-08-30 18:52:31 +0200247 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
Eldad Zackdf23a242013-10-06 22:31:13 +0200248 subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
Daniel Mack2e4a2632012-08-30 18:52:31 +0200249 err = usb_set_interface(subs->dev,
250 subs->sync_endpoint->iface,
Eldad Zackdf23a242013-10-06 22:31:13 +0200251 subs->sync_endpoint->altsetting);
Daniel Mack2e4a2632012-08-30 18:52:31 +0200252 if (err < 0) {
Eldad Zack06613f52013-10-06 22:31:11 +0200253 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100254 dev_err(&subs->dev->dev,
255 "%d:%d: cannot set interface (%d)\n",
Daniel Mack2e4a2632012-08-30 18:52:31 +0200256 subs->sync_endpoint->iface,
Eldad Zackdf23a242013-10-06 22:31:13 +0200257 subs->sync_endpoint->altsetting, err);
Daniel Mack2e4a2632012-08-30 18:52:31 +0200258 return -EIO;
259 }
260 }
261
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700262 dev_dbg(&subs->dev->dev, "Starting sync EP @%pK\n", ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200263
264 ep->sync_slave = subs->data_endpoint;
Ioan-Adrian Ratiube4e3ae2017-01-05 00:37:46 +0200265 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200266 if (err < 0) {
267 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
268 return err;
269 }
270 }
271
272 return 0;
273}
274
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100275static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
Daniel Mackedcd3632012-04-12 13:51:12 +0200276{
277 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100278 snd_usb_endpoint_stop(subs->sync_endpoint);
Daniel Mackedcd3632012-04-12 13:51:12 +0200279
280 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100281 snd_usb_endpoint_stop(subs->data_endpoint);
282
283 if (wait) {
284 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
285 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
286 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200287}
288
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100289static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
290 unsigned int altsetting,
291 struct usb_host_interface **alts,
292 unsigned int *ep)
293{
294 struct usb_interface *iface;
295 struct usb_interface_descriptor *altsd;
296 struct usb_endpoint_descriptor *epd;
297
298 iface = usb_ifnum_to_if(dev, ifnum);
299 if (!iface || iface->num_altsetting < altsetting + 1)
300 return -ENOENT;
301 *alts = &iface->altsetting[altsetting];
302 altsd = get_iface_desc(*alts);
303 if (altsd->bAlternateSetting != altsetting ||
304 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
305 (altsd->bInterfaceSubClass != 2 &&
306 altsd->bInterfaceProtocol != 2 ) ||
307 altsd->bNumEndpoints < 1)
308 return -ENOENT;
309 epd = get_endpoint(*alts, 0);
310 if (!usb_endpoint_is_isoc_in(epd) ||
311 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
312 USB_ENDPOINT_USAGE_IMPLICIT_FB)
313 return -ENOENT;
314 *ep = epd->bEndpointAddress;
315 return 0;
316}
317
Eldad Zacka60945f2013-08-03 10:50:18 +0200318static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
319 struct usb_device *dev,
320 struct usb_interface_descriptor *altsd,
321 unsigned int attr)
Daniel Macke5779992010-03-04 19:46:13 +0100322{
Eldad Zacka60945f2013-08-03 10:50:18 +0200323 struct usb_host_interface *alts;
Daniel Macke5779992010-03-04 19:46:13 +0100324 struct usb_interface *iface;
Eldad Zacka60945f2013-08-03 10:50:18 +0200325 unsigned int ep;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200326
Eldad Zack914273c2013-08-03 10:50:21 +0200327 /* Implicit feedback sync EPs consumers are always playback EPs */
328 if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
329 return 0;
330
Daniel Mackc75a8a72012-04-12 13:51:14 +0200331 switch (subs->stream->chip->usb_id) {
Eldad Zackca10a7e2012-11-28 23:55:41 +0100332 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500333 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zack914273c2013-08-03 10:50:21 +0200334 ep = 0x81;
335 iface = usb_ifnum_to_if(dev, 3);
Eldad Zackca10a7e2012-11-28 23:55:41 +0100336
Eldad Zack914273c2013-08-03 10:50:21 +0200337 if (!iface || iface->num_altsetting == 0)
338 return -EINVAL;
Eldad Zackca10a7e2012-11-28 23:55:41 +0100339
Eldad Zack914273c2013-08-03 10:50:21 +0200340 alts = &iface->altsetting[1];
341 goto add_sync_ep;
Eldad Zackca10a7e2012-11-28 23:55:41 +0100342 break;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200343 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
344 case USB_ID(0x0763, 0x2081):
Eldad Zack914273c2013-08-03 10:50:21 +0200345 ep = 0x81;
346 iface = usb_ifnum_to_if(dev, 2);
Daniel Mackc75a8a72012-04-12 13:51:14 +0200347
Eldad Zack914273c2013-08-03 10:50:21 +0200348 if (!iface || iface->num_altsetting == 0)
349 return -EINVAL;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200350
Eldad Zack914273c2013-08-03 10:50:21 +0200351 alts = &iface->altsetting[1];
352 goto add_sync_ep;
Alberto Aguirrec0aac1b2017-04-04 19:32:13 +0000353 case USB_ID(0x2466, 0x8003):
354 ep = 0x86;
355 iface = usb_ifnum_to_if(dev, 2);
356
357 if (!iface || iface->num_altsetting == 0)
358 return -EINVAL;
359
360 alts = &iface->altsetting[1];
361 goto add_sync_ep;
362
Daniel Mackc75a8a72012-04-12 13:51:14 +0200363 }
Eldad Zack914273c2013-08-03 10:50:21 +0200364 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100365 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
366 altsd->bInterfaceProtocol == 2 &&
367 altsd->bNumEndpoints == 1 &&
368 USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
369 search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
370 altsd->bAlternateSetting,
371 &alts, &ep) >= 0) {
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100372 goto add_sync_ep;
373 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200374
Eldad Zacka60945f2013-08-03 10:50:18 +0200375 /* No quirk */
376 return 0;
377
378add_sync_ep:
379 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
380 alts, ep, !subs->direction,
Eldad Zack88abb8e2013-08-03 10:51:14 +0200381 SND_USB_ENDPOINT_TYPE_DATA);
Eldad Zacka60945f2013-08-03 10:50:18 +0200382 if (!subs->sync_endpoint)
383 return -EINVAL;
384
385 subs->data_endpoint->sync_master = subs->sync_endpoint;
386
387 return 0;
388}
389
390static int set_sync_endpoint(struct snd_usb_substream *subs,
391 struct audioformat *fmt,
392 struct usb_device *dev,
393 struct usb_host_interface *alts,
394 struct usb_interface_descriptor *altsd)
395{
396 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
397 unsigned int ep, attr;
Eldad Zack95fec882013-08-03 10:50:20 +0200398 bool implicit_fb;
Eldad Zacka60945f2013-08-03 10:50:18 +0200399 int err;
400
401 /* we need a sync pipe in async OUT or adaptive IN mode */
402 /* check the number of EP, since some devices have broken
403 * descriptors which fool us. if it has only one EP,
404 * assume it as adaptive-out or sync-in.
405 */
406 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
407
Pierre-Louis Bossart63018442015-08-14 17:19:42 -0500408 if ((is_playback && (attr != USB_ENDPOINT_SYNC_ASYNC)) ||
409 (!is_playback && (attr != USB_ENDPOINT_SYNC_ADAPTIVE))) {
410
411 /*
412 * In these modes the notion of sync_endpoint is irrelevant.
413 * Reset pointers to avoid using stale data from previously
414 * used settings, e.g. when configuration and endpoints were
415 * changed
416 */
417
418 subs->sync_endpoint = NULL;
419 subs->data_endpoint->sync_master = NULL;
420 }
421
Eldad Zacka60945f2013-08-03 10:50:18 +0200422 err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
423 if (err < 0)
424 return err;
425
Eldad Zackf34d0652013-08-03 10:50:19 +0200426 if (altsd->bNumEndpoints < 2)
427 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200428
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500429 if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
430 attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
Eldad Zackf34d0652013-08-03 10:50:19 +0200431 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
432 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200433
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500434 /*
435 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
436 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
437 * error fall back to SYNC mode and don't create sync endpoint
438 */
439
Eldad Zackf34d0652013-08-03 10:50:19 +0200440 /* check sync-pipe endpoint */
441 /* ... and check descriptor size before accessing bSynchAddress
442 because there is a version of the SB Audigy 2 NX firmware lacking
443 the audio fields in the endpoint descriptors */
444 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
445 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zack95fec882013-08-03 10:50:20 +0200446 get_endpoint(alts, 1)->bSynchAddress != 0)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100447 dev_err(&dev->dev,
448 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
449 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200450 get_endpoint(alts, 1)->bmAttributes,
451 get_endpoint(alts, 1)->bLength,
452 get_endpoint(alts, 1)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500453 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
454 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200455 return -EINVAL;
Daniel Mackedcd3632012-04-12 13:51:12 +0200456 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200457 ep = get_endpoint(alts, 1)->bEndpointAddress;
Eldad Zack95fec882013-08-03 10:50:20 +0200458 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zackf34d0652013-08-03 10:50:19 +0200459 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
460 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100461 dev_err(&dev->dev,
462 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
463 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200464 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500465 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
466 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200467 return -EINVAL;
468 }
469
470 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
471 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
472
473 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
474 alts, ep, !subs->direction,
475 implicit_fb ?
476 SND_USB_ENDPOINT_TYPE_DATA :
477 SND_USB_ENDPOINT_TYPE_SYNC);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500478 if (!subs->sync_endpoint) {
479 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
480 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200481 return -EINVAL;
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500482 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200483
484 subs->data_endpoint->sync_master = subs->sync_endpoint;
Daniel Macke5779992010-03-04 19:46:13 +0100485
Eldad Zack71bb64c2013-08-03 10:50:17 +0200486 return 0;
487}
488
489/*
490 * find a matching format and set up the interface
491 */
492static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
493{
494 struct usb_device *dev = subs->dev;
495 struct usb_host_interface *alts;
496 struct usb_interface_descriptor *altsd;
497 struct usb_interface *iface;
498 int err;
499
500 iface = usb_ifnum_to_if(dev, fmt->iface);
501 if (WARN_ON(!iface))
502 return -EINVAL;
503 alts = &iface->altsetting[fmt->altset_idx];
504 altsd = get_iface_desc(alts);
505 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
506 return -EINVAL;
507
508 if (fmt == subs->cur_audiofmt)
509 return 0;
510
511 /* close the old interface */
512 if (subs->interface >= 0 && subs->interface != fmt->iface) {
Hemant Kumard4101882017-08-18 18:44:43 -0700513 err = usb_set_interface_timeout(subs->dev, subs->interface, 0,
514 MAX_SETALT_TIMEOUT_MS);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200515 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100516 dev_err(&dev->dev,
517 "%d:%d: return to setting 0 failed (%d)\n",
518 fmt->iface, fmt->altsetting, err);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200519 return -EIO;
520 }
521 subs->interface = -1;
522 subs->altset_idx = 0;
523 }
524
525 /* set interface */
526 if (subs->interface != fmt->iface ||
527 subs->altset_idx != fmt->altset_idx) {
Jurgen Kramer6874daa2014-11-28 17:32:54 +0100528
529 err = snd_usb_select_mode_quirk(subs, fmt);
530 if (err < 0)
531 return -EIO;
532
Hemant Kumard4101882017-08-18 18:44:43 -0700533 err = usb_set_interface_timeout(dev, fmt->iface,
534 fmt->altsetting, MAX_SETALT_TIMEOUT_MS);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200535 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100536 dev_err(&dev->dev,
537 "%d:%d: usb_set_interface failed (%d)\n",
538 fmt->iface, fmt->altsetting, err);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200539 return -EIO;
540 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100541 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
542 fmt->iface, fmt->altsetting);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200543 subs->interface = fmt->iface;
544 subs->altset_idx = fmt->altset_idx;
545
546 snd_usb_set_interface_quirk(dev);
547 }
548
549 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
550 alts, fmt->endpoint, subs->direction,
551 SND_USB_ENDPOINT_TYPE_DATA);
552
553 if (!subs->data_endpoint)
554 return -EINVAL;
555
556 err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
557 if (err < 0)
558 return err;
559
Eldad Zackd133f2c2013-08-03 10:50:16 +0200560 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
561 if (err < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100562 return err;
563
564 subs->cur_audiofmt = fmt;
565
566 snd_usb_set_format_quirk(subs, fmt);
567
Daniel Macke5779992010-03-04 19:46:13 +0100568 return 0;
569}
570
Hemant Kumar9d9dac12016-03-07 14:46:31 -0800571int snd_usb_enable_audio_stream(struct snd_usb_substream *subs,
572 bool enable)
573{
574 struct audioformat *fmt;
575 struct usb_host_interface *alts;
576 struct usb_interface *iface;
577 int ret;
578
579 if (!enable) {
580 if (subs->interface >= 0) {
Hemant Kumard4101882017-08-18 18:44:43 -0700581 usb_set_interface_timeout(subs->dev, subs->interface, 0,
582 MAX_SETALT_TIMEOUT_MS);
Hemant Kumar9d9dac12016-03-07 14:46:31 -0800583 subs->altset_idx = 0;
584 subs->interface = -1;
585 subs->cur_audiofmt = NULL;
586 }
587
588 snd_usb_autosuspend(subs->stream->chip);
589 return 0;
590 }
591
592 snd_usb_autoresume(subs->stream->chip);
593 fmt = find_format(subs);
594 if (!fmt) {
Hemant Kumar43041092016-08-16 15:17:08 -0700595 dev_err(&subs->dev->dev,
Hemant Kumar9d9dac12016-03-07 14:46:31 -0800596 "cannot set format: format = %#x, rate = %d, channels = %d\n",
597 subs->pcm_format, subs->cur_rate, subs->channels);
598 return -EINVAL;
599 }
600
601 subs->altset_idx = 0;
602 subs->interface = -1;
603 if (atomic_read(&subs->stream->chip->shutdown)) {
604 ret = -ENODEV;
605 } else {
606 ret = set_format(subs, fmt);
607 if (ret < 0)
608 return ret;
609
610 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
611 if (!iface) {
612 dev_err(&subs->dev->dev, "Could not get iface %d\n",
613 subs->cur_audiofmt->iface);
614 return -ENODEV;
615 }
616
617 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
618 ret = snd_usb_init_sample_rate(subs->stream->chip,
619 subs->cur_audiofmt->iface,
620 alts,
621 subs->cur_audiofmt,
622 subs->cur_rate);
623 if (ret < 0) {
624 dev_err(&subs->dev->dev, "failed to set rate %d\n",
625 subs->cur_rate);
626 return ret;
627 }
628 }
629
630 subs->interface = fmt->iface;
631 subs->altset_idx = fmt->altset_idx;
632
633 return 0;
634}
635
Daniel Macke5779992010-03-04 19:46:13 +0100636/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100637 * Return the score of matching two audioformats.
638 * Veto the audioformat if:
639 * - It has no channels for some reason.
640 * - Requested PCM format is not supported.
641 * - Requested sample rate is not supported.
642 */
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100643static int match_endpoint_audioformats(struct snd_usb_substream *subs,
644 struct audioformat *fp,
645 struct audioformat *match, int rate,
646 snd_pcm_format_t pcm_format)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100647{
648 int i;
649 int score = 0;
650
651 if (fp->channels < 1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100652 dev_dbg(&subs->dev->dev,
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700653 "%s: (fmt @%pK) no channels\n", __func__, fp);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100654 return 0;
655 }
656
Eldad Zack74c34ca2013-04-23 01:00:41 +0200657 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100658 dev_dbg(&subs->dev->dev,
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700659 "%s: (fmt @%pK) no match for format %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100660 fp, pcm_format);
661 return 0;
662 }
663
664 for (i = 0; i < fp->nr_rates; i++) {
665 if (fp->rate_table[i] == rate) {
666 score++;
667 break;
668 }
669 }
670 if (!score) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100671 dev_dbg(&subs->dev->dev,
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700672 "%s: (fmt @%pK) no match for rate %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100673 fp, rate);
674 return 0;
675 }
676
677 if (fp->channels == match->channels)
678 score++;
679
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100680 dev_dbg(&subs->dev->dev,
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700681 "%s: (fmt @%pK) score %d\n", __func__, fp, score);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100682
683 return score;
684}
685
686/*
687 * Configure the sync ep using the rate and pcm format of the data ep.
688 */
689static int configure_sync_endpoint(struct snd_usb_substream *subs)
690{
691 int ret;
692 struct audioformat *fp;
693 struct audioformat *sync_fp = NULL;
694 int cur_score = 0;
695 int sync_period_bytes = subs->period_bytes;
696 struct snd_usb_substream *sync_subs =
697 &subs->stream->substream[subs->direction ^ 1];
698
Takashi Iwai31be5422013-01-10 14:06:38 +0100699 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
700 !subs->stream)
701 return snd_usb_endpoint_set_params(subs->sync_endpoint,
702 subs->pcm_format,
703 subs->channels,
704 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400705 0, 0,
Takashi Iwai31be5422013-01-10 14:06:38 +0100706 subs->cur_rate,
707 subs->cur_audiofmt,
708 NULL);
709
Eldad Zack0d9741c2012-12-03 20:30:09 +0100710 /* Try to find the best matching audioformat. */
711 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100712 int score = match_endpoint_audioformats(subs,
713 fp, subs->cur_audiofmt,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100714 subs->cur_rate, subs->pcm_format);
715
716 if (score > cur_score) {
717 sync_fp = fp;
718 cur_score = score;
719 }
720 }
721
722 if (unlikely(sync_fp == NULL)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100723 dev_err(&subs->dev->dev,
724 "%s: no valid audioformat for sync ep %x found\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100725 __func__, sync_subs->ep_num);
726 return -EINVAL;
727 }
728
729 /*
730 * Recalculate the period bytes if channel number differ between
731 * data and sync ep audioformat.
732 */
733 if (sync_fp->channels != subs->channels) {
734 sync_period_bytes = (subs->period_bytes / subs->channels) *
735 sync_fp->channels;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100736 dev_dbg(&subs->dev->dev,
737 "%s: adjusted sync ep period bytes (%d -> %d)\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100738 __func__, subs->period_bytes, sync_period_bytes);
739 }
740
741 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
742 subs->pcm_format,
743 sync_fp->channels,
744 sync_period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400745 0, 0,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100746 subs->cur_rate,
747 sync_fp,
748 NULL);
749
750 return ret;
751}
752
753/*
Dylan Reid61a70952012-09-18 09:49:48 -0700754 * configure endpoint params
755 *
756 * called during initial setup and upon resume
757 */
758static int configure_endpoint(struct snd_usb_substream *subs)
759{
760 int ret;
761
Dylan Reid61a70952012-09-18 09:49:48 -0700762 /* format changed */
Takashi Iwaib0db6062012-11-21 08:35:42 +0100763 stop_endpoints(subs, true);
Dylan Reid61a70952012-09-18 09:49:48 -0700764 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
765 subs->pcm_format,
766 subs->channels,
767 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400768 subs->period_frames,
769 subs->buffer_periods,
Dylan Reid61a70952012-09-18 09:49:48 -0700770 subs->cur_rate,
771 subs->cur_audiofmt,
772 subs->sync_endpoint);
773 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200774 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700775
776 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100777 ret = configure_sync_endpoint(subs);
778
Dylan Reid61a70952012-09-18 09:49:48 -0700779 return ret;
780}
781
782/*
Daniel Macke5779992010-03-04 19:46:13 +0100783 * hw_params callback
784 *
785 * allocate a buffer and set the given audio format.
786 *
787 * so far we use a physically linear buffer although packetize transfer
788 * doesn't need a continuous area.
789 * if sg buffer is supported on the later version of alsa, we'll follow
790 * that.
791 */
792static int snd_usb_hw_params(struct snd_pcm_substream *substream,
793 struct snd_pcm_hw_params *hw_params)
794{
795 struct snd_usb_substream *subs = substream->runtime->private_data;
796 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700797 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100798
799 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
800 params_buffer_bytes(hw_params));
801 if (ret < 0)
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300802 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100803
Dylan Reid61a70952012-09-18 09:49:48 -0700804 subs->pcm_format = params_format(hw_params);
805 subs->period_bytes = params_period_bytes(hw_params);
Alan Stern976b6c02013-09-24 15:51:58 -0400806 subs->period_frames = params_period_size(hw_params);
807 subs->buffer_periods = params_periods(hw_params);
Dylan Reid61a70952012-09-18 09:49:48 -0700808 subs->channels = params_channels(hw_params);
809 subs->cur_rate = params_rate(hw_params);
810
811 fmt = find_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100812 if (!fmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100813 dev_dbg(&subs->dev->dev,
814 "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700815 subs->pcm_format, subs->cur_rate, subs->channels);
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300816 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100817 }
818
Takashi Iwai47ab1542015-08-25 16:09:00 +0200819 ret = snd_usb_lock_shutdown(subs->stream->chip);
820 if (ret < 0)
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300821 return ret;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200822 ret = set_format(subs, fmt);
823 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200824 if (ret < 0)
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300825 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100826
Dylan Reid61a70952012-09-18 09:49:48 -0700827 subs->interface = fmt->iface;
828 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200829 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100830
Dylan Reid61a70952012-09-18 09:49:48 -0700831 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100832}
833
834/*
835 * hw_free callback
836 *
837 * reset the audio format and release the buffer
838 */
839static int snd_usb_hw_free(struct snd_pcm_substream *substream)
840{
841 struct snd_usb_substream *subs = substream->runtime->private_data;
842
843 subs->cur_audiofmt = NULL;
844 subs->cur_rate = 0;
845 subs->period_bytes = 0;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200846 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100847 stop_endpoints(subs, true);
Eldad Zack26de5d02013-10-06 22:31:07 +0200848 snd_usb_endpoint_deactivate(subs->sync_endpoint);
849 snd_usb_endpoint_deactivate(subs->data_endpoint);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200850 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200851 }
Daniel Macke5779992010-03-04 19:46:13 +0100852 return snd_pcm_lib_free_vmalloc_buffer(substream);
853}
854
855/*
856 * prepare callback
857 *
858 * only a few subtle things...
859 */
860static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
861{
862 struct snd_pcm_runtime *runtime = substream->runtime;
863 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700864 struct usb_host_interface *alts;
865 struct usb_interface *iface;
866 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100867
868 if (! subs->cur_audiofmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100869 dev_err(&subs->dev->dev, "no format is specified!\n");
Daniel Macke5779992010-03-04 19:46:13 +0100870 return -ENXIO;
871 }
872
Takashi Iwai47ab1542015-08-25 16:09:00 +0200873 ret = snd_usb_lock_shutdown(subs->stream->chip);
874 if (ret < 0)
875 return ret;
Takashi Iwai978520b2012-10-12 15:12:55 +0200876 if (snd_BUG_ON(!subs->data_endpoint)) {
877 ret = -EIO;
878 goto unlock;
879 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200880
Takashi Iwaif58161b2012-11-08 08:52:45 +0100881 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
882 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
883
Dylan Reid61a70952012-09-18 09:49:48 -0700884 ret = set_format(subs, subs->cur_audiofmt);
885 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200886 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700887
888 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
889 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
890 ret = snd_usb_init_sample_rate(subs->stream->chip,
891 subs->cur_audiofmt->iface,
892 alts,
893 subs->cur_audiofmt,
894 subs->cur_rate);
895 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200896 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700897
Takashi Iwai384dc0852012-09-18 14:49:31 +0200898 if (subs->need_setup_ep) {
899 ret = configure_endpoint(subs);
900 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200901 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200902 subs->need_setup_ep = false;
903 }
Dylan Reid61a70952012-09-18 09:49:48 -0700904
Daniel Macke5779992010-03-04 19:46:13 +0100905 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +0200906 subs->data_endpoint->maxframesize =
907 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
908 subs->data_endpoint->curframesize =
909 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +0100910
911 /* reset the pointer */
912 subs->hwptr_done = 0;
913 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500914 subs->last_delay = 0;
915 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100916 runtime->delay = 0;
917
Daniel Mackedcd3632012-04-12 13:51:12 +0200918 /* for playback, submit the URBs now; otherwise, the first hwptr_done
919 * updates for all URBs would happen at the same time when starting */
920 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Ioan-Adrian Ratiube4e3ae2017-01-05 00:37:46 +0200921 ret = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +0200922
Takashi Iwai978520b2012-10-12 15:12:55 +0200923 unlock:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200924 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200925 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100926}
927
928static struct snd_pcm_hardware snd_usb_hardware =
929{
930 .info = SNDRV_PCM_INFO_MMAP |
931 SNDRV_PCM_INFO_MMAP_VALID |
932 SNDRV_PCM_INFO_BATCH |
933 SNDRV_PCM_INFO_INTERLEAVED |
934 SNDRV_PCM_INFO_BLOCK_TRANSFER |
935 SNDRV_PCM_INFO_PAUSE,
936 .buffer_bytes_max = 1024 * 1024,
937 .period_bytes_min = 64,
938 .period_bytes_max = 512 * 1024,
939 .periods_min = 2,
940 .periods_max = 1024,
941};
942
943static int hw_check_valid_format(struct snd_usb_substream *subs,
944 struct snd_pcm_hw_params *params,
945 struct audioformat *fp)
946{
947 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
948 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
949 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
950 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100951 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100952 unsigned int ptime;
953
954 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100955 snd_mask_none(&check_fmts);
956 check_fmts.bits[0] = (u32)fp->formats;
957 check_fmts.bits[1] = (u32)(fp->formats >> 32);
958 snd_mask_intersect(&check_fmts, fmts);
959 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100960 hwc_debug(" > check: no supported format %d\n", fp->format);
961 return 0;
962 }
963 /* check the channels */
964 if (fp->channels < ct->min || fp->channels > ct->max) {
965 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
966 return 0;
967 }
968 /* check the rate is within the range */
969 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
970 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
971 return 0;
972 }
973 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
974 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
975 return 0;
976 }
977 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +0200978 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100979 ptime = 125 * (1 << fp->datainterval);
980 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
981 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
982 return 0;
983 }
984 }
985 return 1;
986}
987
988static int hw_rule_rate(struct snd_pcm_hw_params *params,
989 struct snd_pcm_hw_rule *rule)
990{
991 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +0200992 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100993 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
994 unsigned int rmin, rmax;
995 int changed;
996
997 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
998 changed = 0;
999 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001000 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001001 if (!hw_check_valid_format(subs, params, fp))
1002 continue;
1003 if (changed++) {
1004 if (rmin > fp->rate_min)
1005 rmin = fp->rate_min;
1006 if (rmax < fp->rate_max)
1007 rmax = fp->rate_max;
1008 } else {
1009 rmin = fp->rate_min;
1010 rmax = fp->rate_max;
1011 }
1012 }
1013
1014 if (!changed) {
1015 hwc_debug(" --> get empty\n");
1016 it->empty = 1;
1017 return -EINVAL;
1018 }
1019
1020 changed = 0;
1021 if (it->min < rmin) {
1022 it->min = rmin;
1023 it->openmin = 0;
1024 changed = 1;
1025 }
1026 if (it->max > rmax) {
1027 it->max = rmax;
1028 it->openmax = 0;
1029 changed = 1;
1030 }
1031 if (snd_interval_checkempty(it)) {
1032 it->empty = 1;
1033 return -EINVAL;
1034 }
1035 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1036 return changed;
1037}
1038
1039
1040static int hw_rule_channels(struct snd_pcm_hw_params *params,
1041 struct snd_pcm_hw_rule *rule)
1042{
1043 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001044 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001045 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1046 unsigned int rmin, rmax;
1047 int changed;
1048
1049 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1050 changed = 0;
1051 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001052 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001053 if (!hw_check_valid_format(subs, params, fp))
1054 continue;
1055 if (changed++) {
1056 if (rmin > fp->channels)
1057 rmin = fp->channels;
1058 if (rmax < fp->channels)
1059 rmax = fp->channels;
1060 } else {
1061 rmin = fp->channels;
1062 rmax = fp->channels;
1063 }
1064 }
1065
1066 if (!changed) {
1067 hwc_debug(" --> get empty\n");
1068 it->empty = 1;
1069 return -EINVAL;
1070 }
1071
1072 changed = 0;
1073 if (it->min < rmin) {
1074 it->min = rmin;
1075 it->openmin = 0;
1076 changed = 1;
1077 }
1078 if (it->max > rmax) {
1079 it->max = rmax;
1080 it->openmax = 0;
1081 changed = 1;
1082 }
1083 if (snd_interval_checkempty(it)) {
1084 it->empty = 1;
1085 return -EINVAL;
1086 }
1087 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1088 return changed;
1089}
1090
1091static int hw_rule_format(struct snd_pcm_hw_params *params,
1092 struct snd_pcm_hw_rule *rule)
1093{
1094 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001095 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001096 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1097 u64 fbits;
1098 u32 oldbits[2];
1099 int changed;
1100
1101 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1102 fbits = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001103 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001104 if (!hw_check_valid_format(subs, params, fp))
1105 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001106 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +01001107 }
1108
1109 oldbits[0] = fmt->bits[0];
1110 oldbits[1] = fmt->bits[1];
1111 fmt->bits[0] &= (u32)fbits;
1112 fmt->bits[1] &= (u32)(fbits >> 32);
1113 if (!fmt->bits[0] && !fmt->bits[1]) {
1114 hwc_debug(" --> get empty\n");
1115 return -EINVAL;
1116 }
1117 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1118 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1119 return changed;
1120}
1121
1122static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1123 struct snd_pcm_hw_rule *rule)
1124{
1125 struct snd_usb_substream *subs = rule->private;
1126 struct audioformat *fp;
1127 struct snd_interval *it;
1128 unsigned char min_datainterval;
1129 unsigned int pmin;
1130 int changed;
1131
1132 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1133 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1134 min_datainterval = 0xff;
1135 list_for_each_entry(fp, &subs->fmt_list, list) {
1136 if (!hw_check_valid_format(subs, params, fp))
1137 continue;
1138 min_datainterval = min(min_datainterval, fp->datainterval);
1139 }
1140 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +02001141 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +01001142 it->empty = 1;
1143 return -EINVAL;
1144 }
1145 pmin = 125 * (1 << min_datainterval);
1146 changed = 0;
1147 if (it->min < pmin) {
1148 it->min = pmin;
1149 it->openmin = 0;
1150 changed = 1;
1151 }
1152 if (snd_interval_checkempty(it)) {
1153 it->empty = 1;
1154 return -EINVAL;
1155 }
1156 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1157 return changed;
1158}
1159
1160/*
1161 * If the device supports unusual bit rates, does the request meet these?
1162 */
1163static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1164 struct snd_usb_substream *subs)
1165{
1166 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001167 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +01001168 int count = 0, needs_knot = 0;
1169 int err;
1170
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +02001171 kfree(subs->rate_list.list);
1172 subs->rate_list.list = NULL;
1173
Daniel Macke5779992010-03-04 19:46:13 +01001174 list_for_each_entry(fp, &subs->fmt_list, list) {
1175 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1176 return 0;
1177 count += fp->nr_rates;
1178 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1179 needs_knot = 1;
1180 }
1181 if (!needs_knot)
1182 return 0;
1183
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001184 subs->rate_list.list = rate_list =
1185 kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +02001186 if (!subs->rate_list.list)
1187 return -ENOMEM;
1188 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +01001189 subs->rate_list.mask = 0;
1190 count = 0;
1191 list_for_each_entry(fp, &subs->fmt_list, list) {
1192 int i;
1193 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001194 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +01001195 }
1196 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1197 &subs->rate_list);
1198 if (err < 0)
1199 return err;
1200
1201 return 0;
1202}
1203
1204
1205/*
1206 * set up the runtime hardware information.
1207 */
1208
1209static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1210{
Eldad Zack88766f02013-04-03 23:18:49 +02001211 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001212 unsigned int pt, ptmin;
1213 int param_period_time_if_needed;
1214 int err;
1215
1216 runtime->hw.formats = subs->formats;
1217
1218 runtime->hw.rate_min = 0x7fffffff;
1219 runtime->hw.rate_max = 0;
1220 runtime->hw.channels_min = 256;
1221 runtime->hw.channels_max = 0;
1222 runtime->hw.rates = 0;
1223 ptmin = UINT_MAX;
1224 /* check min/max rates and channels */
Eldad Zack88766f02013-04-03 23:18:49 +02001225 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001226 runtime->hw.rates |= fp->rates;
1227 if (runtime->hw.rate_min > fp->rate_min)
1228 runtime->hw.rate_min = fp->rate_min;
1229 if (runtime->hw.rate_max < fp->rate_max)
1230 runtime->hw.rate_max = fp->rate_max;
1231 if (runtime->hw.channels_min > fp->channels)
1232 runtime->hw.channels_min = fp->channels;
1233 if (runtime->hw.channels_max < fp->channels)
1234 runtime->hw.channels_max = fp->channels;
1235 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1236 /* FIXME: there might be more than one audio formats... */
1237 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1238 fp->frame_size;
1239 }
1240 pt = 125 * (1 << fp->datainterval);
1241 ptmin = min(ptmin, pt);
1242 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001243 err = snd_usb_autoresume(subs->stream->chip);
1244 if (err < 0)
1245 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001246
1247 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001248 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001249 /* full speed devices have fixed data packet interval */
1250 ptmin = 1000;
1251 if (ptmin == 1000)
1252 /* if period time doesn't go below 1 ms, no rules needed */
1253 param_period_time_if_needed = -1;
1254 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1255 ptmin, UINT_MAX);
1256
1257 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1258 hw_rule_rate, subs,
1259 SNDRV_PCM_HW_PARAM_FORMAT,
1260 SNDRV_PCM_HW_PARAM_CHANNELS,
1261 param_period_time_if_needed,
1262 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001263 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001264 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1265 hw_rule_channels, subs,
1266 SNDRV_PCM_HW_PARAM_FORMAT,
1267 SNDRV_PCM_HW_PARAM_RATE,
1268 param_period_time_if_needed,
1269 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001270 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001271 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1272 hw_rule_format, subs,
1273 SNDRV_PCM_HW_PARAM_RATE,
1274 SNDRV_PCM_HW_PARAM_CHANNELS,
1275 param_period_time_if_needed,
1276 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001277 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001278 if (param_period_time_if_needed >= 0) {
1279 err = snd_pcm_hw_rule_add(runtime, 0,
1280 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1281 hw_rule_period_time, subs,
1282 SNDRV_PCM_HW_PARAM_FORMAT,
1283 SNDRV_PCM_HW_PARAM_CHANNELS,
1284 SNDRV_PCM_HW_PARAM_RATE,
1285 -1);
1286 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001287 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001288 }
1289 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001290 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001291 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +01001292
1293rep_err:
1294 snd_usb_autosuspend(subs->stream->chip);
1295 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001296}
1297
1298static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1299{
1300 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1301 struct snd_pcm_runtime *runtime = substream->runtime;
1302 struct snd_usb_substream *subs = &as->substream[direction];
1303
1304 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001305 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001306 runtime->hw = snd_usb_hardware;
1307 runtime->private_data = subs;
1308 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001309 /* runtime PM is also done there */
Daniel Mackd24f5062013-04-17 00:01:38 +08001310
1311 /* initialize DSD/DOP context */
1312 subs->dsd_dop.byte_idx = 0;
1313 subs->dsd_dop.channel = 0;
1314 subs->dsd_dop.marker = 1;
1315
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -03001316 return setup_hw_info(runtime, subs);
Daniel Macke5779992010-03-04 19:46:13 +01001317}
1318
1319static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1320{
1321 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1322 struct snd_usb_substream *subs = &as->substream[direction];
1323
Takashi Iwaib0db6062012-11-21 08:35:42 +01001324 stop_endpoints(subs, true);
Daniel Mack68e67f42012-07-12 13:08:40 +02001325
Takashi Iwai47ab1542015-08-25 16:09:00 +02001326 if (subs->interface >= 0 &&
1327 !snd_usb_lock_shutdown(subs->stream->chip)) {
Daniel Mack68e67f42012-07-12 13:08:40 +02001328 usb_set_interface(subs->dev, subs->interface, 0);
1329 subs->interface = -1;
Takashi Iwai47ab1542015-08-25 16:09:00 +02001330 snd_usb_unlock_shutdown(subs->stream->chip);
Daniel Mack68e67f42012-07-12 13:08:40 +02001331 }
1332
Daniel Macke5779992010-03-04 19:46:13 +01001333 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001334 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001335
Daniel Mack68e67f42012-07-12 13:08:40 +02001336 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001337}
1338
1339/* Since a URB can handle only a single linear buffer, we must use double
1340 * buffering when the data to be transferred overflows the buffer boundary.
1341 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1342 * for all URBs.
1343 */
1344static void retire_capture_urb(struct snd_usb_substream *subs,
1345 struct urb *urb)
1346{
1347 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1348 unsigned int stride, frames, bytes, oldptr;
1349 int i, period_elapsed = 0;
1350 unsigned long flags;
1351 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001352 int current_frame_number;
1353
1354 /* read frame number here, update pointer in critical section */
1355 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001356
1357 stride = runtime->frame_bits >> 3;
1358
1359 for (i = 0; i < urb->number_of_packets; i++) {
Calvin Owens1539d4f2013-04-12 22:33:59 -05001360 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
Daniel Mackedcd3632012-04-12 13:51:12 +02001361 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001362 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1363 i, urb->iso_frame_desc[i].status);
Daniel Mackedcd3632012-04-12 13:51:12 +02001364 // continue;
1365 }
1366 bytes = urb->iso_frame_desc[i].actual_length;
1367 frames = bytes / stride;
1368 if (!subs->txfr_quirk)
1369 bytes = frames * stride;
1370 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001371 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001372 bytes = frames * stride;
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001373 dev_warn(&subs->dev->dev,
1374 "Corrected urb data len. %d->%d\n",
Daniel Mackedcd3632012-04-12 13:51:12 +02001375 oldbytes, bytes);
1376 }
1377 /* update the current pointer */
1378 spin_lock_irqsave(&subs->lock, flags);
1379 oldptr = subs->hwptr_done;
1380 subs->hwptr_done += bytes;
1381 if (subs->hwptr_done >= runtime->buffer_size * stride)
1382 subs->hwptr_done -= runtime->buffer_size * stride;
1383 frames = (bytes + (oldptr % stride)) / stride;
1384 subs->transfer_done += frames;
1385 if (subs->transfer_done >= runtime->period_size) {
1386 subs->transfer_done -= runtime->period_size;
1387 period_elapsed = 1;
1388 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001389 /* capture delay is by construction limited to one URB,
1390 * reset delays here
1391 */
1392 runtime->delay = subs->last_delay = 0;
1393
1394 /* realign last_frame_number */
1395 subs->last_frame_number = current_frame_number;
1396 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1397
Daniel Mackedcd3632012-04-12 13:51:12 +02001398 spin_unlock_irqrestore(&subs->lock, flags);
1399 /* copy a data chunk */
1400 if (oldptr + bytes > runtime->buffer_size * stride) {
1401 unsigned int bytes1 =
1402 runtime->buffer_size * stride - oldptr;
1403 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1404 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1405 } else {
1406 memcpy(runtime->dma_area + oldptr, cp, bytes);
1407 }
1408 }
1409
1410 if (period_elapsed)
1411 snd_pcm_period_elapsed(subs->pcm_substream);
1412}
1413
Daniel Mackd24f5062013-04-17 00:01:38 +08001414static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1415 struct urb *urb, unsigned int bytes)
1416{
1417 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1418 unsigned int stride = runtime->frame_bits >> 3;
1419 unsigned int dst_idx = 0;
1420 unsigned int src_idx = subs->hwptr_done;
1421 unsigned int wrap = runtime->buffer_size * stride;
1422 u8 *dst = urb->transfer_buffer;
1423 u8 *src = runtime->dma_area;
1424 u8 marker[] = { 0x05, 0xfa };
1425
1426 /*
1427 * The DSP DOP format defines a way to transport DSD samples over
1428 * normal PCM data endpoints. It requires stuffing of marker bytes
1429 * (0x05 and 0xfa, alternating per sample frame), and then expects
1430 * 2 additional bytes of actual payload. The whole frame is stored
1431 * LSB.
1432 *
1433 * Hence, for a stereo transport, the buffer layout looks like this,
1434 * where L refers to left channel samples and R to right.
1435 *
1436 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1437 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1438 * .....
1439 *
1440 */
1441
1442 while (bytes--) {
1443 if (++subs->dsd_dop.byte_idx == 3) {
1444 /* frame boundary? */
1445 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1446 src_idx += 2;
1447 subs->dsd_dop.byte_idx = 0;
1448
1449 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1450 /* alternate the marker */
1451 subs->dsd_dop.marker++;
1452 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1453 subs->dsd_dop.channel = 0;
1454 }
1455 } else {
1456 /* stuff the DSD payload */
1457 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001458
1459 if (subs->cur_audiofmt->dsd_bitrev)
1460 dst[dst_idx++] = bitrev8(src[idx]);
1461 else
1462 dst[dst_idx++] = src[idx];
1463
Daniel Mackd24f5062013-04-17 00:01:38 +08001464 subs->hwptr_done++;
1465 }
1466 }
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001467 if (subs->hwptr_done >= runtime->buffer_size * stride)
1468 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001469}
1470
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001471static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1472 int offset, int stride, unsigned int bytes)
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001473{
1474 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1475
1476 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1477 /* err, the transferred area goes over buffer boundary. */
1478 unsigned int bytes1 =
1479 runtime->buffer_size * stride - subs->hwptr_done;
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001480 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001481 runtime->dma_area + subs->hwptr_done, bytes1);
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001482 memcpy(urb->transfer_buffer + offset + bytes1,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001483 runtime->dma_area, bytes - bytes1);
1484 } else {
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001485 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001486 runtime->dma_area + subs->hwptr_done, bytes);
1487 }
1488 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001489 if (subs->hwptr_done >= runtime->buffer_size * stride)
1490 subs->hwptr_done -= runtime->buffer_size * stride;
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001491}
1492
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001493static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1494 struct urb *urb, int stride,
1495 unsigned int bytes)
1496{
1497 __le32 packet_length;
1498 int i;
1499
1500 /* Put __le32 length descriptor at start of each packet. */
1501 for (i = 0; i < urb->number_of_packets; i++) {
1502 unsigned int length = urb->iso_frame_desc[i].length;
1503 unsigned int offset = urb->iso_frame_desc[i].offset;
1504
1505 packet_length = cpu_to_le32(length);
1506 offset += i * sizeof(packet_length);
1507 urb->iso_frame_desc[i].offset = offset;
1508 urb->iso_frame_desc[i].length += sizeof(packet_length);
1509 memcpy(urb->transfer_buffer + offset,
1510 &packet_length, sizeof(packet_length));
1511 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1512 stride, length);
1513 }
1514 /* Adjust transfer size accordingly. */
1515 bytes += urb->number_of_packets * sizeof(packet_length);
1516 return bytes;
1517}
1518
Daniel Mackedcd3632012-04-12 13:51:12 +02001519static void prepare_playback_urb(struct snd_usb_substream *subs,
1520 struct urb *urb)
1521{
1522 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001523 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001524 struct snd_urb_ctx *ctx = urb->context;
1525 unsigned int counts, frames, bytes;
1526 int i, stride, period_elapsed = 0;
1527 unsigned long flags;
1528
1529 stride = runtime->frame_bits >> 3;
1530
1531 frames = 0;
1532 urb->number_of_packets = 0;
1533 spin_lock_irqsave(&subs->lock, flags);
Alan Stern976b6c02013-09-24 15:51:58 -04001534 subs->frame_limit += ep->max_urb_frames;
Daniel Mackedcd3632012-04-12 13:51:12 +02001535 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001536 if (ctx->packet_size[i])
1537 counts = ctx->packet_size[i];
1538 else
1539 counts = snd_usb_endpoint_next_packet_size(ep);
1540
Daniel Mackedcd3632012-04-12 13:51:12 +02001541 /* set up descriptor */
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001542 urb->iso_frame_desc[i].offset = frames * ep->stride;
1543 urb->iso_frame_desc[i].length = counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001544 frames += counts;
1545 urb->number_of_packets++;
1546 subs->transfer_done += counts;
1547 if (subs->transfer_done >= runtime->period_size) {
1548 subs->transfer_done -= runtime->period_size;
Alan Stern976b6c02013-09-24 15:51:58 -04001549 subs->frame_limit = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001550 period_elapsed = 1;
1551 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1552 if (subs->transfer_done > 0) {
1553 /* FIXME: fill-max mode is not
1554 * supported yet */
1555 frames -= subs->transfer_done;
1556 counts -= subs->transfer_done;
1557 urb->iso_frame_desc[i].length =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001558 counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001559 subs->transfer_done = 0;
1560 }
1561 i++;
1562 if (i < ctx->packets) {
1563 /* add a transfer delimiter */
1564 urb->iso_frame_desc[i].offset =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001565 frames * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001566 urb->iso_frame_desc[i].length = 0;
1567 urb->number_of_packets++;
1568 }
1569 break;
1570 }
1571 }
Alan Stern976b6c02013-09-24 15:51:58 -04001572 /* finish at the period boundary or after enough frames */
1573 if ((period_elapsed ||
1574 subs->transfer_done >= subs->frame_limit) &&
1575 !snd_usb_endpoint_implicit_feedback_sink(ep))
Daniel Mackedcd3632012-04-12 13:51:12 +02001576 break;
1577 }
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001578 bytes = frames * ep->stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001579
1580 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1581 subs->cur_audiofmt->dsd_dop)) {
1582 fill_playback_urb_dsd_dop(subs, urb, bytes);
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001583 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1584 subs->cur_audiofmt->dsd_bitrev)) {
1585 /* bit-reverse the bytes */
1586 u8 *buf = urb->transfer_buffer;
1587 for (i = 0; i < bytes; i++) {
1588 int idx = (subs->hwptr_done + i)
1589 % (runtime->buffer_size * stride);
1590 buf[i] = bitrev8(runtime->dma_area[idx]);
1591 }
1592
1593 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001594 if (subs->hwptr_done >= runtime->buffer_size * stride)
1595 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001596 } else {
Daniel Mackd24f5062013-04-17 00:01:38 +08001597 /* usual PCM */
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001598 if (!subs->tx_length_quirk)
1599 copy_to_urb(subs, urb, 0, stride, bytes);
1600 else
1601 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1602 /* bytes is now amount of outgoing data */
Daniel Mackedcd3632012-04-12 13:51:12 +02001603 }
Daniel Mackd24f5062013-04-17 00:01:38 +08001604
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001605 /* update delay with exact number of samples queued */
1606 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001607 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001608 subs->last_delay = runtime->delay;
1609
1610 /* realign last_frame_number */
1611 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1612 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1613
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001614 if (subs->trigger_tstamp_pending_update) {
1615 /* this is the first actual URB submitted,
1616 * update trigger timestamp to reflect actual start time
1617 */
1618 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1619 subs->trigger_tstamp_pending_update = false;
1620 }
1621
Daniel Mackedcd3632012-04-12 13:51:12 +02001622 spin_unlock_irqrestore(&subs->lock, flags);
1623 urb->transfer_buffer_length = bytes;
1624 if (period_elapsed)
1625 snd_pcm_period_elapsed(subs->pcm_substream);
1626}
1627
1628/*
1629 * process after playback data complete
1630 * - decrease the delay count again
1631 */
1632static void retire_playback_urb(struct snd_usb_substream *subs,
1633 struct urb *urb)
1634{
1635 unsigned long flags;
1636 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001637 struct snd_usb_endpoint *ep = subs->data_endpoint;
1638 int processed = urb->transfer_buffer_length / ep->stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001639 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001640
Takashi Iwai1213a202012-09-06 14:58:00 +02001641 /* ignore the delay accounting when procssed=0 is given, i.e.
1642 * silent payloads are procssed before handling the actual data
1643 */
1644 if (!processed)
1645 return;
1646
Daniel Mackedcd3632012-04-12 13:51:12 +02001647 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001648 if (!subs->last_delay)
1649 goto out; /* short path */
1650
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001651 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1652 /* update delay with exact number of samples played */
1653 if (processed > subs->last_delay)
1654 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001655 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001656 subs->last_delay -= processed;
1657 runtime->delay = subs->last_delay;
1658
1659 /*
1660 * Report when delay estimate is off by more than 2ms.
1661 * The error should be lower than 2ms since the estimate relies
1662 * on two reads of a counter updated every ms.
1663 */
Sander Eikelenboomb7a77232014-05-02 15:09:27 +02001664 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1665 dev_dbg_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001666 "delay: estimated %d, actual %d\n",
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001667 est_delay, subs->last_delay);
1668
Takashi Iwai48779a02012-11-23 16:00:37 +01001669 if (!subs->running) {
1670 /* update last_frame_number for delay counting here since
1671 * prepare_playback_urb won't be called during pause
1672 */
1673 subs->last_frame_number =
1674 usb_get_current_frame_number(subs->dev) & 0xff;
1675 }
1676
1677 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001678 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001679}
1680
1681static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1682{
1683 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1684}
1685
1686static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1687{
1688 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1689}
1690
1691static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1692{
1693 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1694}
1695
1696static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1697{
1698 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1699}
1700
Daniel Mackedcd3632012-04-12 13:51:12 +02001701static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1702 int cmd)
1703{
1704 struct snd_usb_substream *subs = substream->runtime->private_data;
1705
1706 switch (cmd) {
1707 case SNDRV_PCM_TRIGGER_START:
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001708 subs->trigger_tstamp_pending_update = true;
Daniel Mackedcd3632012-04-12 13:51:12 +02001709 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1710 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1711 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001712 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001713 return 0;
1714 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001715 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001716 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001717 return 0;
1718 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1719 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001720 /* keep retire_data_urb for delay calculation */
1721 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001722 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001723 return 0;
1724 }
1725
1726 return -EINVAL;
1727}
1728
Daniel Mackafe25962012-06-16 16:58:04 +02001729static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1730 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001731{
1732 int err;
1733 struct snd_usb_substream *subs = substream->runtime->private_data;
1734
1735 switch (cmd) {
1736 case SNDRV_PCM_TRIGGER_START:
Ioan-Adrian Ratiube4e3ae2017-01-05 00:37:46 +02001737 err = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +02001738 if (err < 0)
1739 return err;
1740
1741 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001742 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001743 return 0;
1744 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001745 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001746 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001747 return 0;
1748 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1749 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001750 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001751 return 0;
1752 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1753 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001754 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001755 return 0;
1756 }
1757
1758 return -EINVAL;
1759}
1760
Daniel Macke5779992010-03-04 19:46:13 +01001761static struct snd_pcm_ops snd_usb_playback_ops = {
1762 .open = snd_usb_playback_open,
1763 .close = snd_usb_playback_close,
1764 .ioctl = snd_pcm_lib_ioctl,
1765 .hw_params = snd_usb_hw_params,
1766 .hw_free = snd_usb_hw_free,
1767 .prepare = snd_usb_pcm_prepare,
1768 .trigger = snd_usb_substream_playback_trigger,
1769 .pointer = snd_usb_pcm_pointer,
1770 .page = snd_pcm_lib_get_vmalloc_page,
1771 .mmap = snd_pcm_lib_mmap_vmalloc,
1772};
1773
1774static struct snd_pcm_ops snd_usb_capture_ops = {
1775 .open = snd_usb_capture_open,
1776 .close = snd_usb_capture_close,
1777 .ioctl = snd_pcm_lib_ioctl,
1778 .hw_params = snd_usb_hw_params,
1779 .hw_free = snd_usb_hw_free,
1780 .prepare = snd_usb_pcm_prepare,
1781 .trigger = snd_usb_substream_capture_trigger,
1782 .pointer = snd_usb_pcm_pointer,
1783 .page = snd_pcm_lib_get_vmalloc_page,
1784 .mmap = snd_pcm_lib_mmap_vmalloc,
1785};
1786
1787void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1788{
1789 snd_pcm_set_ops(pcm, stream,
1790 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1791 &snd_usb_playback_ops : &snd_usb_capture_ops);
1792}