blob: ae7d8a0a0a0a32bae4100c48bccd024ca88f3d09 [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
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050042/* return the estimated delay based on USB frame counters */
43snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
44 unsigned int rate)
45{
46 int current_frame_number;
47 int frame_diff;
48 int est_delay;
49
Takashi Iwai48779a02012-11-23 16:00:37 +010050 if (!subs->last_delay)
51 return 0; /* short path */
52
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050053 current_frame_number = usb_get_current_frame_number(subs->dev);
54 /*
55 * HCD implementations use different widths, use lower 8 bits.
56 * The delay will be managed up to 256ms, which is more than
57 * enough
58 */
59 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
60
61 /* Approximation based on number of samples per USB frame (ms),
62 some truncation for 44.1 but the estimate is good enough */
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060063 est_delay = frame_diff * rate / 1000;
64 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
65 est_delay = subs->last_delay - est_delay;
66 else
67 est_delay = subs->last_delay + est_delay;
68
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050069 if (est_delay < 0)
70 est_delay = 0;
71 return est_delay;
72}
73
Daniel Macke5779992010-03-04 19:46:13 +010074/*
75 * return the current pcm pointer. just based on the hwptr_done value.
76 */
77static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
78{
79 struct snd_usb_substream *subs;
80 unsigned int hwptr_done;
81
82 subs = (struct snd_usb_substream *)substream->runtime->private_data;
Takashi Iwai47ab1542015-08-25 16:09:00 +020083 if (atomic_read(&subs->stream->chip->shutdown))
Takashi Iwai978520b2012-10-12 15:12:55 +020084 return SNDRV_PCM_POS_XRUN;
Daniel Macke5779992010-03-04 19:46:13 +010085 spin_lock(&subs->lock);
86 hwptr_done = subs->hwptr_done;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060087 substream->runtime->delay = snd_usb_pcm_delay(subs,
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050088 substream->runtime->rate);
Daniel Macke5779992010-03-04 19:46:13 +010089 spin_unlock(&subs->lock);
90 return hwptr_done / (substream->runtime->frame_bits >> 3);
91}
92
93/*
94 * find a matching audio format
95 */
Dylan Reid61a70952012-09-18 09:49:48 -070096static struct audioformat *find_format(struct snd_usb_substream *subs)
Daniel Macke5779992010-03-04 19:46:13 +010097{
Eldad Zack88766f02013-04-03 23:18:49 +020098 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +010099 struct audioformat *found = NULL;
100 int cur_attr = 0, attr;
101
Eldad Zack88766f02013-04-03 23:18:49 +0200102 list_for_each_entry(fp, &subs->fmt_list, list) {
Eldad Zack74c34ca2013-04-23 01:00:41 +0200103 if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100104 continue;
Dylan Reid61a70952012-09-18 09:49:48 -0700105 if (fp->channels != subs->channels)
Daniel Macke5779992010-03-04 19:46:13 +0100106 continue;
Dylan Reid61a70952012-09-18 09:49:48 -0700107 if (subs->cur_rate < fp->rate_min ||
108 subs->cur_rate > fp->rate_max)
Daniel Macke5779992010-03-04 19:46:13 +0100109 continue;
110 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
111 unsigned int i;
112 for (i = 0; i < fp->nr_rates; i++)
Dylan Reid61a70952012-09-18 09:49:48 -0700113 if (fp->rate_table[i] == subs->cur_rate)
Daniel Macke5779992010-03-04 19:46:13 +0100114 break;
115 if (i >= fp->nr_rates)
116 continue;
117 }
118 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
119 if (! found) {
120 found = fp;
121 cur_attr = attr;
122 continue;
123 }
124 /* avoid async out and adaptive in if the other method
125 * supports the same format.
126 * this is a workaround for the case like
127 * M-audio audiophile USB.
128 */
129 if (attr != cur_attr) {
130 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
131 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
132 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
133 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
134 continue;
135 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
136 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
137 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
138 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
139 found = fp;
140 cur_attr = attr;
141 continue;
142 }
143 }
144 /* find the format with the largest max. packet size */
145 if (fp->maxpacksize > found->maxpacksize) {
146 found = fp;
147 cur_attr = attr;
148 }
149 }
150 return found;
151}
152
Daniel Mack767d75a2010-03-04 19:46:17 +0100153static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
154 struct usb_host_interface *alts,
155 struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +0100156{
Daniel Mack767d75a2010-03-04 19:46:17 +0100157 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100158 unsigned int ep;
159 unsigned char data[1];
160 int err;
161
Takashi Iwai447d6272016-03-15 15:20:58 +0100162 if (get_iface_desc(alts)->bNumEndpoints < 1)
163 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100164 ep = get_endpoint(alts, 0)->bEndpointAddress;
Daniel Mack767d75a2010-03-04 19:46:17 +0100165
Daniel Mack767d75a2010-03-04 19:46:17 +0100166 data[0] = 1;
167 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
168 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
169 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200170 data, sizeof(data))) < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100171 usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
172 iface, ep);
Daniel Mack767d75a2010-03-04 19:46:17 +0100173 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100174 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100175
Daniel Macke5779992010-03-04 19:46:13 +0100176 return 0;
177}
178
Daniel Mack92c25612010-05-26 18:11:39 +0200179static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
180 struct usb_host_interface *alts,
181 struct audioformat *fmt)
182{
183 struct usb_device *dev = chip->dev;
184 unsigned char data[1];
Daniel Mack92c25612010-05-26 18:11:39 +0200185 int err;
186
Daniel Mack92c25612010-05-26 18:11:39 +0200187 data[0] = 1;
188 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
189 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
190 UAC2_EP_CS_PITCH << 8, 0,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200191 data, sizeof(data))) < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100192 usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
193 iface, fmt->altsetting);
Daniel Mack92c25612010-05-26 18:11:39 +0200194 return err;
195 }
196
197 return 0;
198}
199
Daniel Mack767d75a2010-03-04 19:46:17 +0100200/*
Daniel Mack92c25612010-05-26 18:11:39 +0200201 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100202 */
203int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
204 struct usb_host_interface *alts,
205 struct audioformat *fmt)
206{
Daniel Mack92c25612010-05-26 18:11:39 +0200207 /* if endpoint doesn't have pitch control, bail out */
208 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
209 return 0;
210
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100211 switch (fmt->protocol) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100212 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200213 default:
Daniel Mack767d75a2010-03-04 19:46:17 +0100214 return init_pitch_v1(chip, iface, alts, fmt);
215
216 case UAC_VERSION_2:
Daniel Mack92c25612010-05-26 18:11:39 +0200217 return init_pitch_v2(chip, iface, alts, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100218 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100219}
220
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200221static int start_endpoints(struct snd_usb_substream *subs)
Daniel Mackedcd3632012-04-12 13:51:12 +0200222{
223 int err;
224
225 if (!subs->data_endpoint)
226 return -EINVAL;
227
228 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
229 struct snd_usb_endpoint *ep = subs->data_endpoint;
230
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100231 dev_dbg(&subs->dev->dev, "Starting data EP @%p\n", ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200232
233 ep->data_subs = subs;
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200234 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200235 if (err < 0) {
236 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
237 return err;
238 }
239 }
240
241 if (subs->sync_endpoint &&
242 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
243 struct snd_usb_endpoint *ep = subs->sync_endpoint;
244
Daniel Mack2e4a2632012-08-30 18:52:31 +0200245 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
Eldad Zackdf23a242013-10-06 22:31:13 +0200246 subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
Daniel Mack2e4a2632012-08-30 18:52:31 +0200247 err = usb_set_interface(subs->dev,
248 subs->sync_endpoint->iface,
Eldad Zackdf23a242013-10-06 22:31:13 +0200249 subs->sync_endpoint->altsetting);
Daniel Mack2e4a2632012-08-30 18:52:31 +0200250 if (err < 0) {
Eldad Zack06613f52013-10-06 22:31:11 +0200251 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100252 dev_err(&subs->dev->dev,
253 "%d:%d: cannot set interface (%d)\n",
Daniel Mack2e4a2632012-08-30 18:52:31 +0200254 subs->sync_endpoint->iface,
Eldad Zackdf23a242013-10-06 22:31:13 +0200255 subs->sync_endpoint->altsetting, err);
Daniel Mack2e4a2632012-08-30 18:52:31 +0200256 return -EIO;
257 }
258 }
259
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100260 dev_dbg(&subs->dev->dev, "Starting sync EP @%p\n", ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200261
262 ep->sync_slave = subs->data_endpoint;
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200263 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200264 if (err < 0) {
265 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
266 return err;
267 }
268 }
269
270 return 0;
271}
272
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100273static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
Daniel Mackedcd3632012-04-12 13:51:12 +0200274{
275 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100276 snd_usb_endpoint_stop(subs->sync_endpoint);
Daniel Mackedcd3632012-04-12 13:51:12 +0200277
278 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100279 snd_usb_endpoint_stop(subs->data_endpoint);
280
281 if (wait) {
282 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
283 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
284 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200285}
286
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100287static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
288 unsigned int altsetting,
289 struct usb_host_interface **alts,
290 unsigned int *ep)
291{
292 struct usb_interface *iface;
293 struct usb_interface_descriptor *altsd;
294 struct usb_endpoint_descriptor *epd;
295
296 iface = usb_ifnum_to_if(dev, ifnum);
297 if (!iface || iface->num_altsetting < altsetting + 1)
298 return -ENOENT;
299 *alts = &iface->altsetting[altsetting];
300 altsd = get_iface_desc(*alts);
301 if (altsd->bAlternateSetting != altsetting ||
302 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
303 (altsd->bInterfaceSubClass != 2 &&
304 altsd->bInterfaceProtocol != 2 ) ||
305 altsd->bNumEndpoints < 1)
306 return -ENOENT;
307 epd = get_endpoint(*alts, 0);
308 if (!usb_endpoint_is_isoc_in(epd) ||
309 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
310 USB_ENDPOINT_USAGE_IMPLICIT_FB)
311 return -ENOENT;
312 *ep = epd->bEndpointAddress;
313 return 0;
314}
315
Eldad Zacka60945f2013-08-03 10:50:18 +0200316static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
317 struct usb_device *dev,
318 struct usb_interface_descriptor *altsd,
319 unsigned int attr)
Daniel Macke5779992010-03-04 19:46:13 +0100320{
Eldad Zacka60945f2013-08-03 10:50:18 +0200321 struct usb_host_interface *alts;
Daniel Macke5779992010-03-04 19:46:13 +0100322 struct usb_interface *iface;
Eldad Zacka60945f2013-08-03 10:50:18 +0200323 unsigned int ep;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500324 unsigned int ifnum;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200325
Eldad Zack914273c2013-08-03 10:50:21 +0200326 /* Implicit feedback sync EPs consumers are always playback EPs */
327 if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
328 return 0;
329
Daniel Mackc75a8a72012-04-12 13:51:14 +0200330 switch (subs->stream->chip->usb_id) {
Eldad Zackca10a7e2012-11-28 23:55:41 +0100331 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500332 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zack914273c2013-08-03 10:50:21 +0200333 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500334 ifnum = 3;
335 goto add_sync_ep_from_ifnum;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200336 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
337 case USB_ID(0x0763, 0x2081):
Eldad Zack914273c2013-08-03 10:50:21 +0200338 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500339 ifnum = 2;
340 goto add_sync_ep_from_ifnum;
341 case USB_ID(0x2466, 0x8003): /* Fractal Audio Axe-Fx II */
Alberto Aguirre17f08b02016-12-08 00:36:48 -0600342 ep = 0x86;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500343 ifnum = 2;
344 goto add_sync_ep_from_ifnum;
Alberto Aguirre91a85612018-04-18 09:35:35 -0500345 case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx III */
346 ep = 0x81;
347 ifnum = 2;
348 goto add_sync_ep_from_ifnum;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500349 case USB_ID(0x1397, 0x0002): /* Behringer UFX1204 */
Lassi Ylikojola5e35dc02018-02-09 16:51:36 +0200350 ep = 0x81;
Alberto Aguirre103e9622018-04-18 09:35:34 -0500351 ifnum = 1;
352 goto add_sync_ep_from_ifnum;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200353 }
Alberto Aguirre103e9622018-04-18 09:35:34 -0500354
Eldad Zack914273c2013-08-03 10:50:21 +0200355 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100356 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
357 altsd->bInterfaceProtocol == 2 &&
358 altsd->bNumEndpoints == 1 &&
359 USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
360 search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
361 altsd->bAlternateSetting,
362 &alts, &ep) >= 0) {
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100363 goto add_sync_ep;
364 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200365
Eldad Zacka60945f2013-08-03 10:50:18 +0200366 /* No quirk */
367 return 0;
368
Alberto Aguirre103e9622018-04-18 09:35:34 -0500369add_sync_ep_from_ifnum:
370 iface = usb_ifnum_to_if(dev, ifnum);
371
372 if (!iface || iface->num_altsetting == 0)
373 return -EINVAL;
374
375 alts = &iface->altsetting[1];
376
Eldad Zacka60945f2013-08-03 10:50:18 +0200377add_sync_ep:
378 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
379 alts, ep, !subs->direction,
Eldad Zack88abb8e2013-08-03 10:51:14 +0200380 SND_USB_ENDPOINT_TYPE_DATA);
Eldad Zacka60945f2013-08-03 10:50:18 +0200381 if (!subs->sync_endpoint)
382 return -EINVAL;
383
384 subs->data_endpoint->sync_master = subs->sync_endpoint;
385
386 return 0;
387}
388
389static int set_sync_endpoint(struct snd_usb_substream *subs,
390 struct audioformat *fmt,
391 struct usb_device *dev,
392 struct usb_host_interface *alts,
393 struct usb_interface_descriptor *altsd)
394{
395 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
396 unsigned int ep, attr;
Eldad Zack95fec882013-08-03 10:50:20 +0200397 bool implicit_fb;
Eldad Zacka60945f2013-08-03 10:50:18 +0200398 int err;
399
400 /* we need a sync pipe in async OUT or adaptive IN mode */
401 /* check the number of EP, since some devices have broken
402 * descriptors which fool us. if it has only one EP,
403 * assume it as adaptive-out or sync-in.
404 */
405 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
406
Pierre-Louis Bossart63018442015-08-14 17:19:42 -0500407 if ((is_playback && (attr != USB_ENDPOINT_SYNC_ASYNC)) ||
408 (!is_playback && (attr != USB_ENDPOINT_SYNC_ADAPTIVE))) {
409
410 /*
411 * In these modes the notion of sync_endpoint is irrelevant.
412 * Reset pointers to avoid using stale data from previously
413 * used settings, e.g. when configuration and endpoints were
414 * changed
415 */
416
417 subs->sync_endpoint = NULL;
418 subs->data_endpoint->sync_master = NULL;
419 }
420
Eldad Zacka60945f2013-08-03 10:50:18 +0200421 err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
422 if (err < 0)
423 return err;
424
Eldad Zackf34d0652013-08-03 10:50:19 +0200425 if (altsd->bNumEndpoints < 2)
426 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200427
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500428 if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
429 attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
Eldad Zackf34d0652013-08-03 10:50:19 +0200430 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
431 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200432
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500433 /*
434 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
435 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
436 * error fall back to SYNC mode and don't create sync endpoint
437 */
438
Eldad Zackf34d0652013-08-03 10:50:19 +0200439 /* check sync-pipe endpoint */
440 /* ... and check descriptor size before accessing bSynchAddress
441 because there is a version of the SB Audigy 2 NX firmware lacking
442 the audio fields in the endpoint descriptors */
443 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
444 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zack95fec882013-08-03 10:50:20 +0200445 get_endpoint(alts, 1)->bSynchAddress != 0)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100446 dev_err(&dev->dev,
447 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
448 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200449 get_endpoint(alts, 1)->bmAttributes,
450 get_endpoint(alts, 1)->bLength,
451 get_endpoint(alts, 1)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500452 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
453 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200454 return -EINVAL;
Daniel Mackedcd3632012-04-12 13:51:12 +0200455 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200456 ep = get_endpoint(alts, 1)->bEndpointAddress;
Eldad Zack95fec882013-08-03 10:50:20 +0200457 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zackf34d0652013-08-03 10:50:19 +0200458 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
459 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100460 dev_err(&dev->dev,
461 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
462 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200463 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500464 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
465 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200466 return -EINVAL;
467 }
468
469 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
470 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
471
472 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
473 alts, ep, !subs->direction,
474 implicit_fb ?
475 SND_USB_ENDPOINT_TYPE_DATA :
476 SND_USB_ENDPOINT_TYPE_SYNC);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500477 if (!subs->sync_endpoint) {
478 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
479 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200480 return -EINVAL;
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500481 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200482
483 subs->data_endpoint->sync_master = subs->sync_endpoint;
Daniel Macke5779992010-03-04 19:46:13 +0100484
Eldad Zack71bb64c2013-08-03 10:50:17 +0200485 return 0;
486}
487
488/*
489 * find a matching format and set up the interface
490 */
491static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
492{
493 struct usb_device *dev = subs->dev;
494 struct usb_host_interface *alts;
495 struct usb_interface_descriptor *altsd;
496 struct usb_interface *iface;
497 int err;
498
499 iface = usb_ifnum_to_if(dev, fmt->iface);
500 if (WARN_ON(!iface))
501 return -EINVAL;
Takashi Iwaib099b962018-05-02 09:36:28 +0200502 alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200503 altsd = get_iface_desc(alts);
504 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
505 return -EINVAL;
506
507 if (fmt == subs->cur_audiofmt)
508 return 0;
509
510 /* close the old interface */
511 if (subs->interface >= 0 && subs->interface != fmt->iface) {
512 err = usb_set_interface(subs->dev, subs->interface, 0);
513 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100514 dev_err(&dev->dev,
515 "%d:%d: return to setting 0 failed (%d)\n",
516 fmt->iface, fmt->altsetting, err);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200517 return -EIO;
518 }
519 subs->interface = -1;
520 subs->altset_idx = 0;
521 }
522
523 /* set interface */
Takashi Iwaib099b962018-05-02 09:36:28 +0200524 if (iface->cur_altsetting != alts) {
Jurgen Kramer6874daa2014-11-28 17:32:54 +0100525 err = snd_usb_select_mode_quirk(subs, fmt);
526 if (err < 0)
527 return -EIO;
528
Eldad Zack71bb64c2013-08-03 10:50:17 +0200529 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
530 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100531 dev_err(&dev->dev,
532 "%d:%d: usb_set_interface failed (%d)\n",
533 fmt->iface, fmt->altsetting, err);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200534 return -EIO;
535 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100536 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
537 fmt->iface, fmt->altsetting);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200538 snd_usb_set_interface_quirk(dev);
539 }
540
Takashi Iwaib099b962018-05-02 09:36:28 +0200541 subs->interface = fmt->iface;
542 subs->altset_idx = fmt->altset_idx;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200543 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
544 alts, fmt->endpoint, subs->direction,
545 SND_USB_ENDPOINT_TYPE_DATA);
546
547 if (!subs->data_endpoint)
548 return -EINVAL;
549
550 err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
551 if (err < 0)
552 return err;
553
Eldad Zackd133f2c2013-08-03 10:50:16 +0200554 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
555 if (err < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100556 return err;
557
558 subs->cur_audiofmt = fmt;
559
560 snd_usb_set_format_quirk(subs, fmt);
561
Daniel Macke5779992010-03-04 19:46:13 +0100562 return 0;
563}
564
565/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100566 * Return the score of matching two audioformats.
567 * Veto the audioformat if:
568 * - It has no channels for some reason.
569 * - Requested PCM format is not supported.
570 * - Requested sample rate is not supported.
571 */
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100572static int match_endpoint_audioformats(struct snd_usb_substream *subs,
573 struct audioformat *fp,
574 struct audioformat *match, int rate,
575 snd_pcm_format_t pcm_format)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100576{
577 int i;
578 int score = 0;
579
580 if (fp->channels < 1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100581 dev_dbg(&subs->dev->dev,
582 "%s: (fmt @%p) no channels\n", __func__, fp);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100583 return 0;
584 }
585
Eldad Zack74c34ca2013-04-23 01:00:41 +0200586 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100587 dev_dbg(&subs->dev->dev,
588 "%s: (fmt @%p) no match for format %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100589 fp, pcm_format);
590 return 0;
591 }
592
593 for (i = 0; i < fp->nr_rates; i++) {
594 if (fp->rate_table[i] == rate) {
595 score++;
596 break;
597 }
598 }
599 if (!score) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100600 dev_dbg(&subs->dev->dev,
601 "%s: (fmt @%p) no match for rate %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100602 fp, rate);
603 return 0;
604 }
605
606 if (fp->channels == match->channels)
607 score++;
608
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100609 dev_dbg(&subs->dev->dev,
610 "%s: (fmt @%p) score %d\n", __func__, fp, score);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100611
612 return score;
613}
614
615/*
616 * Configure the sync ep using the rate and pcm format of the data ep.
617 */
618static int configure_sync_endpoint(struct snd_usb_substream *subs)
619{
620 int ret;
621 struct audioformat *fp;
622 struct audioformat *sync_fp = NULL;
623 int cur_score = 0;
624 int sync_period_bytes = subs->period_bytes;
625 struct snd_usb_substream *sync_subs =
626 &subs->stream->substream[subs->direction ^ 1];
627
Takashi Iwai31be5422013-01-10 14:06:38 +0100628 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
629 !subs->stream)
630 return snd_usb_endpoint_set_params(subs->sync_endpoint,
631 subs->pcm_format,
632 subs->channels,
633 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400634 0, 0,
Takashi Iwai31be5422013-01-10 14:06:38 +0100635 subs->cur_rate,
636 subs->cur_audiofmt,
637 NULL);
638
Eldad Zack0d9741c2012-12-03 20:30:09 +0100639 /* Try to find the best matching audioformat. */
640 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100641 int score = match_endpoint_audioformats(subs,
642 fp, subs->cur_audiofmt,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100643 subs->cur_rate, subs->pcm_format);
644
645 if (score > cur_score) {
646 sync_fp = fp;
647 cur_score = score;
648 }
649 }
650
651 if (unlikely(sync_fp == NULL)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100652 dev_err(&subs->dev->dev,
653 "%s: no valid audioformat for sync ep %x found\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100654 __func__, sync_subs->ep_num);
655 return -EINVAL;
656 }
657
658 /*
659 * Recalculate the period bytes if channel number differ between
660 * data and sync ep audioformat.
661 */
662 if (sync_fp->channels != subs->channels) {
663 sync_period_bytes = (subs->period_bytes / subs->channels) *
664 sync_fp->channels;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100665 dev_dbg(&subs->dev->dev,
666 "%s: adjusted sync ep period bytes (%d -> %d)\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100667 __func__, subs->period_bytes, sync_period_bytes);
668 }
669
670 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
671 subs->pcm_format,
672 sync_fp->channels,
673 sync_period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400674 0, 0,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100675 subs->cur_rate,
676 sync_fp,
677 NULL);
678
679 return ret;
680}
681
682/*
Dylan Reid61a70952012-09-18 09:49:48 -0700683 * configure endpoint params
684 *
685 * called during initial setup and upon resume
686 */
687static int configure_endpoint(struct snd_usb_substream *subs)
688{
689 int ret;
690
Dylan Reid61a70952012-09-18 09:49:48 -0700691 /* format changed */
Takashi Iwaib0db6062012-11-21 08:35:42 +0100692 stop_endpoints(subs, true);
Dylan Reid61a70952012-09-18 09:49:48 -0700693 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
694 subs->pcm_format,
695 subs->channels,
696 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400697 subs->period_frames,
698 subs->buffer_periods,
Dylan Reid61a70952012-09-18 09:49:48 -0700699 subs->cur_rate,
700 subs->cur_audiofmt,
701 subs->sync_endpoint);
702 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200703 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700704
705 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100706 ret = configure_sync_endpoint(subs);
707
Dylan Reid61a70952012-09-18 09:49:48 -0700708 return ret;
709}
710
711/*
Daniel Macke5779992010-03-04 19:46:13 +0100712 * hw_params callback
713 *
714 * allocate a buffer and set the given audio format.
715 *
716 * so far we use a physically linear buffer although packetize transfer
717 * doesn't need a continuous area.
718 * if sg buffer is supported on the later version of alsa, we'll follow
719 * that.
720 */
721static int snd_usb_hw_params(struct snd_pcm_substream *substream,
722 struct snd_pcm_hw_params *hw_params)
723{
724 struct snd_usb_substream *subs = substream->runtime->private_data;
725 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700726 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100727
728 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
729 params_buffer_bytes(hw_params));
730 if (ret < 0)
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300731 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100732
Dylan Reid61a70952012-09-18 09:49:48 -0700733 subs->pcm_format = params_format(hw_params);
734 subs->period_bytes = params_period_bytes(hw_params);
Alan Stern976b6c02013-09-24 15:51:58 -0400735 subs->period_frames = params_period_size(hw_params);
736 subs->buffer_periods = params_periods(hw_params);
Dylan Reid61a70952012-09-18 09:49:48 -0700737 subs->channels = params_channels(hw_params);
738 subs->cur_rate = params_rate(hw_params);
739
740 fmt = find_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100741 if (!fmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100742 dev_dbg(&subs->dev->dev,
743 "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700744 subs->pcm_format, subs->cur_rate, subs->channels);
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300745 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100746 }
747
Takashi Iwai47ab1542015-08-25 16:09:00 +0200748 ret = snd_usb_lock_shutdown(subs->stream->chip);
749 if (ret < 0)
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300750 return ret;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200751 ret = set_format(subs, fmt);
752 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200753 if (ret < 0)
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300754 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100755
Dylan Reid61a70952012-09-18 09:49:48 -0700756 subs->interface = fmt->iface;
757 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200758 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100759
Dylan Reid61a70952012-09-18 09:49:48 -0700760 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100761}
762
763/*
764 * hw_free callback
765 *
766 * reset the audio format and release the buffer
767 */
768static int snd_usb_hw_free(struct snd_pcm_substream *substream)
769{
770 struct snd_usb_substream *subs = substream->runtime->private_data;
771
772 subs->cur_audiofmt = NULL;
773 subs->cur_rate = 0;
774 subs->period_bytes = 0;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200775 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100776 stop_endpoints(subs, true);
Eldad Zack26de5d02013-10-06 22:31:07 +0200777 snd_usb_endpoint_deactivate(subs->sync_endpoint);
778 snd_usb_endpoint_deactivate(subs->data_endpoint);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200779 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200780 }
Daniel Macke5779992010-03-04 19:46:13 +0100781 return snd_pcm_lib_free_vmalloc_buffer(substream);
782}
783
784/*
785 * prepare callback
786 *
787 * only a few subtle things...
788 */
789static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
790{
791 struct snd_pcm_runtime *runtime = substream->runtime;
792 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700793 struct usb_host_interface *alts;
794 struct usb_interface *iface;
795 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100796
797 if (! subs->cur_audiofmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100798 dev_err(&subs->dev->dev, "no format is specified!\n");
Daniel Macke5779992010-03-04 19:46:13 +0100799 return -ENXIO;
800 }
801
Takashi Iwai47ab1542015-08-25 16:09:00 +0200802 ret = snd_usb_lock_shutdown(subs->stream->chip);
803 if (ret < 0)
804 return ret;
Takashi Iwai978520b2012-10-12 15:12:55 +0200805 if (snd_BUG_ON(!subs->data_endpoint)) {
806 ret = -EIO;
807 goto unlock;
808 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200809
Takashi Iwaif58161b2012-11-08 08:52:45 +0100810 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
811 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
812
Dylan Reid61a70952012-09-18 09:49:48 -0700813 ret = set_format(subs, subs->cur_audiofmt);
814 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200815 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700816
Takashi Iwai384dc0852012-09-18 14:49:31 +0200817 if (subs->need_setup_ep) {
Daniel Girnus1e2e3fe2016-12-06 14:46:15 +0900818
819 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
820 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
821 ret = snd_usb_init_sample_rate(subs->stream->chip,
822 subs->cur_audiofmt->iface,
823 alts,
824 subs->cur_audiofmt,
825 subs->cur_rate);
826 if (ret < 0)
827 goto unlock;
828
Takashi Iwai384dc0852012-09-18 14:49:31 +0200829 ret = configure_endpoint(subs);
830 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200831 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200832 subs->need_setup_ep = false;
833 }
Dylan Reid61a70952012-09-18 09:49:48 -0700834
Daniel Macke5779992010-03-04 19:46:13 +0100835 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +0200836 subs->data_endpoint->maxframesize =
837 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
838 subs->data_endpoint->curframesize =
839 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +0100840
841 /* reset the pointer */
842 subs->hwptr_done = 0;
843 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500844 subs->last_delay = 0;
845 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100846 runtime->delay = 0;
847
Daniel Mackedcd3632012-04-12 13:51:12 +0200848 /* for playback, submit the URBs now; otherwise, the first hwptr_done
849 * updates for all URBs would happen at the same time when starting */
850 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +0200851 ret = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +0200852
Takashi Iwai978520b2012-10-12 15:12:55 +0200853 unlock:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200854 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200855 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100856}
857
Bhumika Goyalaaffbf72017-08-17 14:45:59 +0530858static const struct snd_pcm_hardware snd_usb_hardware =
Daniel Macke5779992010-03-04 19:46:13 +0100859{
860 .info = SNDRV_PCM_INFO_MMAP |
861 SNDRV_PCM_INFO_MMAP_VALID |
862 SNDRV_PCM_INFO_BATCH |
863 SNDRV_PCM_INFO_INTERLEAVED |
864 SNDRV_PCM_INFO_BLOCK_TRANSFER |
865 SNDRV_PCM_INFO_PAUSE,
866 .buffer_bytes_max = 1024 * 1024,
867 .period_bytes_min = 64,
868 .period_bytes_max = 512 * 1024,
869 .periods_min = 2,
870 .periods_max = 1024,
871};
872
873static int hw_check_valid_format(struct snd_usb_substream *subs,
874 struct snd_pcm_hw_params *params,
875 struct audioformat *fp)
876{
877 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
878 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
879 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
880 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100881 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100882 unsigned int ptime;
883
884 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100885 snd_mask_none(&check_fmts);
886 check_fmts.bits[0] = (u32)fp->formats;
887 check_fmts.bits[1] = (u32)(fp->formats >> 32);
888 snd_mask_intersect(&check_fmts, fmts);
889 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100890 hwc_debug(" > check: no supported format %d\n", fp->format);
891 return 0;
892 }
893 /* check the channels */
894 if (fp->channels < ct->min || fp->channels > ct->max) {
895 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
896 return 0;
897 }
898 /* check the rate is within the range */
899 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
900 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
901 return 0;
902 }
903 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
904 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
905 return 0;
906 }
907 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +0200908 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100909 ptime = 125 * (1 << fp->datainterval);
910 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
911 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
912 return 0;
913 }
914 }
915 return 1;
916}
917
918static int hw_rule_rate(struct snd_pcm_hw_params *params,
919 struct snd_pcm_hw_rule *rule)
920{
921 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +0200922 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100923 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
924 unsigned int rmin, rmax;
925 int changed;
926
927 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
928 changed = 0;
929 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +0200930 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +0100931 if (!hw_check_valid_format(subs, params, fp))
932 continue;
933 if (changed++) {
934 if (rmin > fp->rate_min)
935 rmin = fp->rate_min;
936 if (rmax < fp->rate_max)
937 rmax = fp->rate_max;
938 } else {
939 rmin = fp->rate_min;
940 rmax = fp->rate_max;
941 }
942 }
943
944 if (!changed) {
945 hwc_debug(" --> get empty\n");
946 it->empty = 1;
947 return -EINVAL;
948 }
949
950 changed = 0;
951 if (it->min < rmin) {
952 it->min = rmin;
953 it->openmin = 0;
954 changed = 1;
955 }
956 if (it->max > rmax) {
957 it->max = rmax;
958 it->openmax = 0;
959 changed = 1;
960 }
961 if (snd_interval_checkempty(it)) {
962 it->empty = 1;
963 return -EINVAL;
964 }
965 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
966 return changed;
967}
968
969
970static int hw_rule_channels(struct snd_pcm_hw_params *params,
971 struct snd_pcm_hw_rule *rule)
972{
973 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +0200974 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100975 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
976 unsigned int rmin, rmax;
977 int changed;
978
979 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
980 changed = 0;
981 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +0200982 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +0100983 if (!hw_check_valid_format(subs, params, fp))
984 continue;
985 if (changed++) {
986 if (rmin > fp->channels)
987 rmin = fp->channels;
988 if (rmax < fp->channels)
989 rmax = fp->channels;
990 } else {
991 rmin = fp->channels;
992 rmax = fp->channels;
993 }
994 }
995
996 if (!changed) {
997 hwc_debug(" --> get empty\n");
998 it->empty = 1;
999 return -EINVAL;
1000 }
1001
1002 changed = 0;
1003 if (it->min < rmin) {
1004 it->min = rmin;
1005 it->openmin = 0;
1006 changed = 1;
1007 }
1008 if (it->max > rmax) {
1009 it->max = rmax;
1010 it->openmax = 0;
1011 changed = 1;
1012 }
1013 if (snd_interval_checkempty(it)) {
1014 it->empty = 1;
1015 return -EINVAL;
1016 }
1017 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1018 return changed;
1019}
1020
1021static int hw_rule_format(struct snd_pcm_hw_params *params,
1022 struct snd_pcm_hw_rule *rule)
1023{
1024 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001025 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001026 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1027 u64 fbits;
1028 u32 oldbits[2];
1029 int changed;
1030
1031 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1032 fbits = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001033 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001034 if (!hw_check_valid_format(subs, params, fp))
1035 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001036 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +01001037 }
1038
1039 oldbits[0] = fmt->bits[0];
1040 oldbits[1] = fmt->bits[1];
1041 fmt->bits[0] &= (u32)fbits;
1042 fmt->bits[1] &= (u32)(fbits >> 32);
1043 if (!fmt->bits[0] && !fmt->bits[1]) {
1044 hwc_debug(" --> get empty\n");
1045 return -EINVAL;
1046 }
1047 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1048 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1049 return changed;
1050}
1051
1052static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1053 struct snd_pcm_hw_rule *rule)
1054{
1055 struct snd_usb_substream *subs = rule->private;
1056 struct audioformat *fp;
1057 struct snd_interval *it;
1058 unsigned char min_datainterval;
1059 unsigned int pmin;
1060 int changed;
1061
1062 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1063 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1064 min_datainterval = 0xff;
1065 list_for_each_entry(fp, &subs->fmt_list, list) {
1066 if (!hw_check_valid_format(subs, params, fp))
1067 continue;
1068 min_datainterval = min(min_datainterval, fp->datainterval);
1069 }
1070 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +02001071 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +01001072 it->empty = 1;
1073 return -EINVAL;
1074 }
1075 pmin = 125 * (1 << min_datainterval);
1076 changed = 0;
1077 if (it->min < pmin) {
1078 it->min = pmin;
1079 it->openmin = 0;
1080 changed = 1;
1081 }
1082 if (snd_interval_checkempty(it)) {
1083 it->empty = 1;
1084 return -EINVAL;
1085 }
1086 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1087 return changed;
1088}
1089
1090/*
1091 * If the device supports unusual bit rates, does the request meet these?
1092 */
1093static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1094 struct snd_usb_substream *subs)
1095{
1096 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001097 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +01001098 int count = 0, needs_knot = 0;
1099 int err;
1100
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +02001101 kfree(subs->rate_list.list);
1102 subs->rate_list.list = NULL;
1103
Daniel Macke5779992010-03-04 19:46:13 +01001104 list_for_each_entry(fp, &subs->fmt_list, list) {
1105 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1106 return 0;
1107 count += fp->nr_rates;
1108 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1109 needs_knot = 1;
1110 }
1111 if (!needs_knot)
1112 return 0;
1113
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001114 subs->rate_list.list = rate_list =
1115 kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +02001116 if (!subs->rate_list.list)
1117 return -ENOMEM;
1118 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +01001119 subs->rate_list.mask = 0;
1120 count = 0;
1121 list_for_each_entry(fp, &subs->fmt_list, list) {
1122 int i;
1123 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001124 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +01001125 }
1126 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1127 &subs->rate_list);
1128 if (err < 0)
1129 return err;
1130
1131 return 0;
1132}
1133
1134
1135/*
1136 * set up the runtime hardware information.
1137 */
1138
1139static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1140{
Eldad Zack88766f02013-04-03 23:18:49 +02001141 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001142 unsigned int pt, ptmin;
1143 int param_period_time_if_needed;
1144 int err;
1145
1146 runtime->hw.formats = subs->formats;
1147
1148 runtime->hw.rate_min = 0x7fffffff;
1149 runtime->hw.rate_max = 0;
1150 runtime->hw.channels_min = 256;
1151 runtime->hw.channels_max = 0;
1152 runtime->hw.rates = 0;
1153 ptmin = UINT_MAX;
1154 /* check min/max rates and channels */
Eldad Zack88766f02013-04-03 23:18:49 +02001155 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001156 runtime->hw.rates |= fp->rates;
1157 if (runtime->hw.rate_min > fp->rate_min)
1158 runtime->hw.rate_min = fp->rate_min;
1159 if (runtime->hw.rate_max < fp->rate_max)
1160 runtime->hw.rate_max = fp->rate_max;
1161 if (runtime->hw.channels_min > fp->channels)
1162 runtime->hw.channels_min = fp->channels;
1163 if (runtime->hw.channels_max < fp->channels)
1164 runtime->hw.channels_max = fp->channels;
1165 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1166 /* FIXME: there might be more than one audio formats... */
1167 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1168 fp->frame_size;
1169 }
1170 pt = 125 * (1 << fp->datainterval);
1171 ptmin = min(ptmin, pt);
1172 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001173 err = snd_usb_autoresume(subs->stream->chip);
1174 if (err < 0)
1175 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001176
1177 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001178 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001179 /* full speed devices have fixed data packet interval */
1180 ptmin = 1000;
1181 if (ptmin == 1000)
1182 /* if period time doesn't go below 1 ms, no rules needed */
1183 param_period_time_if_needed = -1;
1184 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1185 ptmin, UINT_MAX);
1186
1187 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1188 hw_rule_rate, subs,
1189 SNDRV_PCM_HW_PARAM_FORMAT,
1190 SNDRV_PCM_HW_PARAM_CHANNELS,
1191 param_period_time_if_needed,
1192 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001193 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001194 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1195 hw_rule_channels, subs,
1196 SNDRV_PCM_HW_PARAM_FORMAT,
1197 SNDRV_PCM_HW_PARAM_RATE,
1198 param_period_time_if_needed,
1199 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001200 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001201 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1202 hw_rule_format, subs,
1203 SNDRV_PCM_HW_PARAM_RATE,
1204 SNDRV_PCM_HW_PARAM_CHANNELS,
1205 param_period_time_if_needed,
1206 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001207 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001208 if (param_period_time_if_needed >= 0) {
1209 err = snd_pcm_hw_rule_add(runtime, 0,
1210 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1211 hw_rule_period_time, subs,
1212 SNDRV_PCM_HW_PARAM_FORMAT,
1213 SNDRV_PCM_HW_PARAM_CHANNELS,
1214 SNDRV_PCM_HW_PARAM_RATE,
1215 -1);
1216 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001217 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001218 }
1219 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001220 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001221 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +01001222
1223rep_err:
1224 snd_usb_autosuspend(subs->stream->chip);
1225 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001226}
1227
1228static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1229{
1230 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1231 struct snd_pcm_runtime *runtime = substream->runtime;
1232 struct snd_usb_substream *subs = &as->substream[direction];
1233
1234 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001235 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001236 runtime->hw = snd_usb_hardware;
1237 runtime->private_data = subs;
1238 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001239 /* runtime PM is also done there */
Daniel Mackd24f5062013-04-17 00:01:38 +08001240
1241 /* initialize DSD/DOP context */
1242 subs->dsd_dop.byte_idx = 0;
1243 subs->dsd_dop.channel = 0;
1244 subs->dsd_dop.marker = 1;
1245
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -03001246 return setup_hw_info(runtime, subs);
Daniel Macke5779992010-03-04 19:46:13 +01001247}
1248
1249static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1250{
1251 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1252 struct snd_usb_substream *subs = &as->substream[direction];
1253
Takashi Iwaib0db6062012-11-21 08:35:42 +01001254 stop_endpoints(subs, true);
Daniel Mack68e67f42012-07-12 13:08:40 +02001255
Takashi Iwai47ab1542015-08-25 16:09:00 +02001256 if (subs->interface >= 0 &&
1257 !snd_usb_lock_shutdown(subs->stream->chip)) {
Daniel Mack68e67f42012-07-12 13:08:40 +02001258 usb_set_interface(subs->dev, subs->interface, 0);
1259 subs->interface = -1;
Takashi Iwai47ab1542015-08-25 16:09:00 +02001260 snd_usb_unlock_shutdown(subs->stream->chip);
Daniel Mack68e67f42012-07-12 13:08:40 +02001261 }
1262
Daniel Macke5779992010-03-04 19:46:13 +01001263 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001264 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001265
Daniel Mack68e67f42012-07-12 13:08:40 +02001266 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001267}
1268
1269/* Since a URB can handle only a single linear buffer, we must use double
1270 * buffering when the data to be transferred overflows the buffer boundary.
1271 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1272 * for all URBs.
1273 */
1274static void retire_capture_urb(struct snd_usb_substream *subs,
1275 struct urb *urb)
1276{
1277 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1278 unsigned int stride, frames, bytes, oldptr;
1279 int i, period_elapsed = 0;
1280 unsigned long flags;
1281 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001282 int current_frame_number;
1283
1284 /* read frame number here, update pointer in critical section */
1285 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001286
1287 stride = runtime->frame_bits >> 3;
1288
1289 for (i = 0; i < urb->number_of_packets; i++) {
Calvin Owens1539d4f2013-04-12 22:33:59 -05001290 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
Daniel Mackedcd3632012-04-12 13:51:12 +02001291 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001292 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1293 i, urb->iso_frame_desc[i].status);
Daniel Mackedcd3632012-04-12 13:51:12 +02001294 // continue;
1295 }
1296 bytes = urb->iso_frame_desc[i].actual_length;
1297 frames = bytes / stride;
1298 if (!subs->txfr_quirk)
1299 bytes = frames * stride;
1300 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001301 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001302 bytes = frames * stride;
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001303 dev_warn(&subs->dev->dev,
1304 "Corrected urb data len. %d->%d\n",
Daniel Mackedcd3632012-04-12 13:51:12 +02001305 oldbytes, bytes);
1306 }
1307 /* update the current pointer */
1308 spin_lock_irqsave(&subs->lock, flags);
1309 oldptr = subs->hwptr_done;
1310 subs->hwptr_done += bytes;
1311 if (subs->hwptr_done >= runtime->buffer_size * stride)
1312 subs->hwptr_done -= runtime->buffer_size * stride;
1313 frames = (bytes + (oldptr % stride)) / stride;
1314 subs->transfer_done += frames;
1315 if (subs->transfer_done >= runtime->period_size) {
1316 subs->transfer_done -= runtime->period_size;
1317 period_elapsed = 1;
1318 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001319 /* capture delay is by construction limited to one URB,
1320 * reset delays here
1321 */
1322 runtime->delay = subs->last_delay = 0;
1323
1324 /* realign last_frame_number */
1325 subs->last_frame_number = current_frame_number;
1326 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1327
Daniel Mackedcd3632012-04-12 13:51:12 +02001328 spin_unlock_irqrestore(&subs->lock, flags);
1329 /* copy a data chunk */
1330 if (oldptr + bytes > runtime->buffer_size * stride) {
1331 unsigned int bytes1 =
1332 runtime->buffer_size * stride - oldptr;
1333 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1334 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1335 } else {
1336 memcpy(runtime->dma_area + oldptr, cp, bytes);
1337 }
1338 }
1339
1340 if (period_elapsed)
1341 snd_pcm_period_elapsed(subs->pcm_substream);
1342}
1343
Daniel Mackd24f5062013-04-17 00:01:38 +08001344static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1345 struct urb *urb, unsigned int bytes)
1346{
1347 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1348 unsigned int stride = runtime->frame_bits >> 3;
1349 unsigned int dst_idx = 0;
1350 unsigned int src_idx = subs->hwptr_done;
1351 unsigned int wrap = runtime->buffer_size * stride;
1352 u8 *dst = urb->transfer_buffer;
1353 u8 *src = runtime->dma_area;
1354 u8 marker[] = { 0x05, 0xfa };
1355
1356 /*
1357 * The DSP DOP format defines a way to transport DSD samples over
1358 * normal PCM data endpoints. It requires stuffing of marker bytes
1359 * (0x05 and 0xfa, alternating per sample frame), and then expects
1360 * 2 additional bytes of actual payload. The whole frame is stored
1361 * LSB.
1362 *
1363 * Hence, for a stereo transport, the buffer layout looks like this,
1364 * where L refers to left channel samples and R to right.
1365 *
1366 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1367 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1368 * .....
1369 *
1370 */
1371
1372 while (bytes--) {
1373 if (++subs->dsd_dop.byte_idx == 3) {
1374 /* frame boundary? */
1375 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1376 src_idx += 2;
1377 subs->dsd_dop.byte_idx = 0;
1378
1379 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1380 /* alternate the marker */
1381 subs->dsd_dop.marker++;
1382 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1383 subs->dsd_dop.channel = 0;
1384 }
1385 } else {
1386 /* stuff the DSD payload */
1387 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001388
1389 if (subs->cur_audiofmt->dsd_bitrev)
1390 dst[dst_idx++] = bitrev8(src[idx]);
1391 else
1392 dst[dst_idx++] = src[idx];
1393
Daniel Mackd24f5062013-04-17 00:01:38 +08001394 subs->hwptr_done++;
1395 }
1396 }
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001397 if (subs->hwptr_done >= runtime->buffer_size * stride)
1398 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001399}
1400
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001401static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1402 int offset, int stride, unsigned int bytes)
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001403{
1404 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1405
1406 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1407 /* err, the transferred area goes over buffer boundary. */
1408 unsigned int bytes1 =
1409 runtime->buffer_size * stride - subs->hwptr_done;
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001410 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001411 runtime->dma_area + subs->hwptr_done, bytes1);
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001412 memcpy(urb->transfer_buffer + offset + bytes1,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001413 runtime->dma_area, bytes - bytes1);
1414 } else {
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001415 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001416 runtime->dma_area + subs->hwptr_done, bytes);
1417 }
1418 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001419 if (subs->hwptr_done >= runtime->buffer_size * stride)
1420 subs->hwptr_done -= runtime->buffer_size * stride;
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001421}
1422
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001423static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1424 struct urb *urb, int stride,
1425 unsigned int bytes)
1426{
1427 __le32 packet_length;
1428 int i;
1429
1430 /* Put __le32 length descriptor at start of each packet. */
1431 for (i = 0; i < urb->number_of_packets; i++) {
1432 unsigned int length = urb->iso_frame_desc[i].length;
1433 unsigned int offset = urb->iso_frame_desc[i].offset;
1434
1435 packet_length = cpu_to_le32(length);
1436 offset += i * sizeof(packet_length);
1437 urb->iso_frame_desc[i].offset = offset;
1438 urb->iso_frame_desc[i].length += sizeof(packet_length);
1439 memcpy(urb->transfer_buffer + offset,
1440 &packet_length, sizeof(packet_length));
1441 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1442 stride, length);
1443 }
1444 /* Adjust transfer size accordingly. */
1445 bytes += urb->number_of_packets * sizeof(packet_length);
1446 return bytes;
1447}
1448
Daniel Mackedcd3632012-04-12 13:51:12 +02001449static void prepare_playback_urb(struct snd_usb_substream *subs,
1450 struct urb *urb)
1451{
1452 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001453 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001454 struct snd_urb_ctx *ctx = urb->context;
1455 unsigned int counts, frames, bytes;
1456 int i, stride, period_elapsed = 0;
1457 unsigned long flags;
1458
1459 stride = runtime->frame_bits >> 3;
1460
1461 frames = 0;
1462 urb->number_of_packets = 0;
1463 spin_lock_irqsave(&subs->lock, flags);
Alan Stern976b6c02013-09-24 15:51:58 -04001464 subs->frame_limit += ep->max_urb_frames;
Daniel Mackedcd3632012-04-12 13:51:12 +02001465 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001466 if (ctx->packet_size[i])
1467 counts = ctx->packet_size[i];
1468 else
1469 counts = snd_usb_endpoint_next_packet_size(ep);
1470
Daniel Mackedcd3632012-04-12 13:51:12 +02001471 /* set up descriptor */
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001472 urb->iso_frame_desc[i].offset = frames * ep->stride;
1473 urb->iso_frame_desc[i].length = counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001474 frames += counts;
1475 urb->number_of_packets++;
1476 subs->transfer_done += counts;
1477 if (subs->transfer_done >= runtime->period_size) {
1478 subs->transfer_done -= runtime->period_size;
Alan Stern976b6c02013-09-24 15:51:58 -04001479 subs->frame_limit = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001480 period_elapsed = 1;
1481 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1482 if (subs->transfer_done > 0) {
1483 /* FIXME: fill-max mode is not
1484 * supported yet */
1485 frames -= subs->transfer_done;
1486 counts -= subs->transfer_done;
1487 urb->iso_frame_desc[i].length =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001488 counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001489 subs->transfer_done = 0;
1490 }
1491 i++;
1492 if (i < ctx->packets) {
1493 /* add a transfer delimiter */
1494 urb->iso_frame_desc[i].offset =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001495 frames * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001496 urb->iso_frame_desc[i].length = 0;
1497 urb->number_of_packets++;
1498 }
1499 break;
1500 }
1501 }
Alan Stern976b6c02013-09-24 15:51:58 -04001502 /* finish at the period boundary or after enough frames */
1503 if ((period_elapsed ||
1504 subs->transfer_done >= subs->frame_limit) &&
1505 !snd_usb_endpoint_implicit_feedback_sink(ep))
Daniel Mackedcd3632012-04-12 13:51:12 +02001506 break;
1507 }
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001508 bytes = frames * ep->stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001509
1510 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1511 subs->cur_audiofmt->dsd_dop)) {
1512 fill_playback_urb_dsd_dop(subs, urb, bytes);
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001513 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1514 subs->cur_audiofmt->dsd_bitrev)) {
1515 /* bit-reverse the bytes */
1516 u8 *buf = urb->transfer_buffer;
1517 for (i = 0; i < bytes; i++) {
1518 int idx = (subs->hwptr_done + i)
1519 % (runtime->buffer_size * stride);
1520 buf[i] = bitrev8(runtime->dma_area[idx]);
1521 }
1522
1523 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001524 if (subs->hwptr_done >= runtime->buffer_size * stride)
1525 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001526 } else {
Daniel Mackd24f5062013-04-17 00:01:38 +08001527 /* usual PCM */
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001528 if (!subs->tx_length_quirk)
1529 copy_to_urb(subs, urb, 0, stride, bytes);
1530 else
1531 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1532 /* bytes is now amount of outgoing data */
Daniel Mackedcd3632012-04-12 13:51:12 +02001533 }
Daniel Mackd24f5062013-04-17 00:01:38 +08001534
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001535 /* update delay with exact number of samples queued */
1536 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001537 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001538 subs->last_delay = runtime->delay;
1539
1540 /* realign last_frame_number */
1541 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1542 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1543
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001544 if (subs->trigger_tstamp_pending_update) {
1545 /* this is the first actual URB submitted,
1546 * update trigger timestamp to reflect actual start time
1547 */
1548 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1549 subs->trigger_tstamp_pending_update = false;
1550 }
1551
Daniel Mackedcd3632012-04-12 13:51:12 +02001552 spin_unlock_irqrestore(&subs->lock, flags);
1553 urb->transfer_buffer_length = bytes;
1554 if (period_elapsed)
1555 snd_pcm_period_elapsed(subs->pcm_substream);
1556}
1557
1558/*
1559 * process after playback data complete
1560 * - decrease the delay count again
1561 */
1562static void retire_playback_urb(struct snd_usb_substream *subs,
1563 struct urb *urb)
1564{
1565 unsigned long flags;
1566 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001567 struct snd_usb_endpoint *ep = subs->data_endpoint;
1568 int processed = urb->transfer_buffer_length / ep->stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001569 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001570
Takashi Iwai1213a202012-09-06 14:58:00 +02001571 /* ignore the delay accounting when procssed=0 is given, i.e.
1572 * silent payloads are procssed before handling the actual data
1573 */
1574 if (!processed)
1575 return;
1576
Daniel Mackedcd3632012-04-12 13:51:12 +02001577 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001578 if (!subs->last_delay)
1579 goto out; /* short path */
1580
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001581 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1582 /* update delay with exact number of samples played */
1583 if (processed > subs->last_delay)
1584 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001585 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001586 subs->last_delay -= processed;
1587 runtime->delay = subs->last_delay;
1588
1589 /*
1590 * Report when delay estimate is off by more than 2ms.
1591 * The error should be lower than 2ms since the estimate relies
1592 * on two reads of a counter updated every ms.
1593 */
Sander Eikelenboomb7a77232014-05-02 15:09:27 +02001594 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1595 dev_dbg_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001596 "delay: estimated %d, actual %d\n",
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001597 est_delay, subs->last_delay);
1598
Takashi Iwai48779a02012-11-23 16:00:37 +01001599 if (!subs->running) {
1600 /* update last_frame_number for delay counting here since
1601 * prepare_playback_urb won't be called during pause
1602 */
1603 subs->last_frame_number =
1604 usb_get_current_frame_number(subs->dev) & 0xff;
1605 }
1606
1607 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001608 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001609}
1610
1611static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1612{
1613 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1614}
1615
1616static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1617{
1618 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1619}
1620
1621static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1622{
1623 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1624}
1625
1626static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1627{
1628 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1629}
1630
Daniel Mackedcd3632012-04-12 13:51:12 +02001631static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1632 int cmd)
1633{
1634 struct snd_usb_substream *subs = substream->runtime->private_data;
1635
1636 switch (cmd) {
1637 case SNDRV_PCM_TRIGGER_START:
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001638 subs->trigger_tstamp_pending_update = true;
Daniel Mackedcd3632012-04-12 13:51:12 +02001639 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1640 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1641 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001642 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001643 return 0;
1644 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001645 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001646 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001647 return 0;
1648 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1649 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001650 /* keep retire_data_urb for delay calculation */
1651 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001652 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001653 return 0;
1654 }
1655
1656 return -EINVAL;
1657}
1658
Daniel Mackafe25962012-06-16 16:58:04 +02001659static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1660 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001661{
1662 int err;
1663 struct snd_usb_substream *subs = substream->runtime->private_data;
1664
1665 switch (cmd) {
1666 case SNDRV_PCM_TRIGGER_START:
Ioan-Adrian Ratiu1d0f9532017-01-05 00:37:46 +02001667 err = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +02001668 if (err < 0)
1669 return err;
1670
1671 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001672 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001673 return 0;
1674 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001675 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001676 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001677 return 0;
1678 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1679 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001680 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001681 return 0;
1682 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1683 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001684 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001685 return 0;
1686 }
1687
1688 return -EINVAL;
1689}
1690
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301691static const struct snd_pcm_ops snd_usb_playback_ops = {
Daniel Macke5779992010-03-04 19:46:13 +01001692 .open = snd_usb_playback_open,
1693 .close = snd_usb_playback_close,
1694 .ioctl = snd_pcm_lib_ioctl,
1695 .hw_params = snd_usb_hw_params,
1696 .hw_free = snd_usb_hw_free,
1697 .prepare = snd_usb_pcm_prepare,
1698 .trigger = snd_usb_substream_playback_trigger,
1699 .pointer = snd_usb_pcm_pointer,
1700 .page = snd_pcm_lib_get_vmalloc_page,
1701 .mmap = snd_pcm_lib_mmap_vmalloc,
1702};
1703
Arvind Yadav31cb1fb2017-08-18 13:15:21 +05301704static const struct snd_pcm_ops snd_usb_capture_ops = {
Daniel Macke5779992010-03-04 19:46:13 +01001705 .open = snd_usb_capture_open,
1706 .close = snd_usb_capture_close,
1707 .ioctl = snd_pcm_lib_ioctl,
1708 .hw_params = snd_usb_hw_params,
1709 .hw_free = snd_usb_hw_free,
1710 .prepare = snd_usb_pcm_prepare,
1711 .trigger = snd_usb_substream_capture_trigger,
1712 .pointer = snd_usb_pcm_pointer,
1713 .page = snd_pcm_lib_get_vmalloc_page,
1714 .mmap = snd_pcm_lib_mmap_vmalloc,
1715};
1716
1717void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1718{
1719 snd_pcm_set_ops(pcm, stream,
1720 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1721 &snd_usb_playback_ops : &snd_usb_capture_ops);
1722}