blob: e24ce7d58d8354161e03d861ee7e887657c1a976 [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 Iwai978520b2012-10-12 15:12:55 +020083 if (subs->stream->chip->shutdown)
84 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
162 ep = get_endpoint(alts, 0)->bEndpointAddress;
Daniel Mack767d75a2010-03-04 19:46:17 +0100163
Daniel Mack767d75a2010-03-04 19:46:17 +0100164 data[0] = 1;
165 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
166 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
167 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200168 data, sizeof(data))) < 0) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100169 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
170 dev->devnum, iface, ep);
171 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100172 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100173
Daniel Macke5779992010-03-04 19:46:13 +0100174 return 0;
175}
176
Daniel Mack92c25612010-05-26 18:11:39 +0200177static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
178 struct usb_host_interface *alts,
179 struct audioformat *fmt)
180{
181 struct usb_device *dev = chip->dev;
182 unsigned char data[1];
Daniel Mack92c25612010-05-26 18:11:39 +0200183 int err;
184
Daniel Mack92c25612010-05-26 18:11:39 +0200185 data[0] = 1;
186 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
187 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
188 UAC2_EP_CS_PITCH << 8, 0,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200189 data, sizeof(data))) < 0) {
Daniel Mack92c25612010-05-26 18:11:39 +0200190 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
191 dev->devnum, iface, fmt->altsetting);
192 return err;
193 }
194
195 return 0;
196}
197
Daniel Mack767d75a2010-03-04 19:46:17 +0100198/*
Daniel Mack92c25612010-05-26 18:11:39 +0200199 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100200 */
201int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
202 struct usb_host_interface *alts,
203 struct audioformat *fmt)
204{
Daniel Mack92c25612010-05-26 18:11:39 +0200205 /* if endpoint doesn't have pitch control, bail out */
206 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
207 return 0;
208
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100209 switch (fmt->protocol) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100210 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200211 default:
Daniel Mack767d75a2010-03-04 19:46:17 +0100212 return init_pitch_v1(chip, iface, alts, fmt);
213
214 case UAC_VERSION_2:
Daniel Mack92c25612010-05-26 18:11:39 +0200215 return init_pitch_v2(chip, iface, alts, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100216 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100217}
218
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100219static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
Daniel Mackedcd3632012-04-12 13:51:12 +0200220{
221 int err;
222
223 if (!subs->data_endpoint)
224 return -EINVAL;
225
226 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
227 struct snd_usb_endpoint *ep = subs->data_endpoint;
228
229 snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep);
230
231 ep->data_subs = subs;
Daniel Mack015618b2012-08-29 13:17:05 +0200232 err = snd_usb_endpoint_start(ep, can_sleep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200233 if (err < 0) {
234 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
235 return err;
236 }
237 }
238
239 if (subs->sync_endpoint &&
240 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
241 struct snd_usb_endpoint *ep = subs->sync_endpoint;
242
Daniel Mack2e4a2632012-08-30 18:52:31 +0200243 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
244 subs->data_endpoint->alt_idx != subs->sync_endpoint->alt_idx) {
245 err = usb_set_interface(subs->dev,
246 subs->sync_endpoint->iface,
247 subs->sync_endpoint->alt_idx);
248 if (err < 0) {
249 snd_printk(KERN_ERR
250 "%d:%d:%d: cannot set interface (%d)\n",
251 subs->dev->devnum,
252 subs->sync_endpoint->iface,
253 subs->sync_endpoint->alt_idx, err);
254 return -EIO;
255 }
256 }
257
Daniel Mackedcd3632012-04-12 13:51:12 +0200258 snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);
259
260 ep->sync_slave = subs->data_endpoint;
Daniel Mack015618b2012-08-29 13:17:05 +0200261 err = snd_usb_endpoint_start(ep, can_sleep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200262 if (err < 0) {
263 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
264 return err;
265 }
266 }
267
268 return 0;
269}
270
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100271static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
Daniel Mackedcd3632012-04-12 13:51:12 +0200272{
273 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100274 snd_usb_endpoint_stop(subs->sync_endpoint);
Daniel Mackedcd3632012-04-12 13:51:12 +0200275
276 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100277 snd_usb_endpoint_stop(subs->data_endpoint);
278
279 if (wait) {
280 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
281 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
282 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200283}
284
Daniel Mackedcd3632012-04-12 13:51:12 +0200285static int deactivate_endpoints(struct snd_usb_substream *subs)
286{
287 int reta, retb;
288
289 reta = snd_usb_endpoint_deactivate(subs->sync_endpoint);
290 retb = snd_usb_endpoint_deactivate(subs->data_endpoint);
291
292 if (reta < 0)
293 return reta;
294
295 if (retb < 0)
296 return retb;
297
298 return 0;
299}
300
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100301static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
302 unsigned int altsetting,
303 struct usb_host_interface **alts,
304 unsigned int *ep)
305{
306 struct usb_interface *iface;
307 struct usb_interface_descriptor *altsd;
308 struct usb_endpoint_descriptor *epd;
309
310 iface = usb_ifnum_to_if(dev, ifnum);
311 if (!iface || iface->num_altsetting < altsetting + 1)
312 return -ENOENT;
313 *alts = &iface->altsetting[altsetting];
314 altsd = get_iface_desc(*alts);
315 if (altsd->bAlternateSetting != altsetting ||
316 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
317 (altsd->bInterfaceSubClass != 2 &&
318 altsd->bInterfaceProtocol != 2 ) ||
319 altsd->bNumEndpoints < 1)
320 return -ENOENT;
321 epd = get_endpoint(*alts, 0);
322 if (!usb_endpoint_is_isoc_in(epd) ||
323 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
324 USB_ENDPOINT_USAGE_IMPLICIT_FB)
325 return -ENOENT;
326 *ep = epd->bEndpointAddress;
327 return 0;
328}
329
Eldad Zack71bb64c2013-08-03 10:50:17 +0200330
331static int set_sync_endpoint(struct snd_usb_substream *subs,
332 struct audioformat *fmt,
333 struct usb_device *dev,
334 struct usb_host_interface *alts,
335 struct usb_interface_descriptor *altsd)
Daniel Macke5779992010-03-04 19:46:13 +0100336{
Daniel Macke5779992010-03-04 19:46:13 +0100337 struct usb_interface *iface;
Daniel Macke5779992010-03-04 19:46:13 +0100338 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
Eldad Zack71bb64c2013-08-03 10:50:17 +0200339 unsigned int ep, attr;
340 int implicit_fb = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100341
342 /* we need a sync pipe in async OUT or adaptive IN mode */
343 /* check the number of EP, since some devices have broken
344 * descriptors which fool us. if it has only one EP,
345 * assume it as adaptive-out or sync-in.
346 */
347 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200348
349 switch (subs->stream->chip->usb_id) {
Eldad Zackca10a7e2012-11-28 23:55:41 +0100350 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500351 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zackca10a7e2012-11-28 23:55:41 +0100352 if (is_playback) {
353 implicit_fb = 1;
354 ep = 0x81;
355 iface = usb_ifnum_to_if(dev, 3);
356
357 if (!iface || iface->num_altsetting == 0)
358 return -EINVAL;
359
360 alts = &iface->altsetting[1];
361 goto add_sync_ep;
362 }
363 break;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200364 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
365 case USB_ID(0x0763, 0x2081):
366 if (is_playback) {
367 implicit_fb = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +0200368 ep = 0x81;
369 iface = usb_ifnum_to_if(dev, 2);
Daniel Mackc75a8a72012-04-12 13:51:14 +0200370
371 if (!iface || iface->num_altsetting == 0)
372 return -EINVAL;
373
Daniel Mackedcd3632012-04-12 13:51:12 +0200374 alts = &iface->altsetting[1];
375 goto add_sync_ep;
376 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200377 }
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100378 if (is_playback &&
379 attr == USB_ENDPOINT_SYNC_ASYNC &&
380 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
381 altsd->bInterfaceProtocol == 2 &&
382 altsd->bNumEndpoints == 1 &&
383 USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
384 search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
385 altsd->bAlternateSetting,
386 &alts, &ep) >= 0) {
387 implicit_fb = 1;
388 goto add_sync_ep;
389 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200390
Daniel Mackc75a8a72012-04-12 13:51:14 +0200391 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
392 (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
393 altsd->bNumEndpoints >= 2) {
Daniel Macke5779992010-03-04 19:46:13 +0100394 /* check sync-pipe endpoint */
395 /* ... and check descriptor size before accessing bSynchAddress
396 because there is a version of the SB Audigy 2 NX firmware lacking
397 the audio fields in the endpoint descriptors */
Eldad Zackfde854b2012-11-28 23:55:32 +0100398 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
Daniel Macke5779992010-03-04 19:46:13 +0100399 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Mackc75a8a72012-04-12 13:51:14 +0200400 get_endpoint(alts, 1)->bSynchAddress != 0 &&
401 !implicit_fb)) {
Daniel Mack7fb75db2012-06-17 13:44:27 +0200402 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
403 dev->devnum, fmt->iface, fmt->altsetting,
404 get_endpoint(alts, 1)->bmAttributes,
405 get_endpoint(alts, 1)->bLength,
406 get_endpoint(alts, 1)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100407 return -EINVAL;
408 }
409 ep = get_endpoint(alts, 1)->bEndpointAddress;
Daniel Mack7fb75db2012-06-17 13:44:27 +0200410 if (!implicit_fb &&
411 get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Daniel Macke5779992010-03-04 19:46:13 +0100412 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
Daniel Mack7fb75db2012-06-17 13:44:27 +0200413 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
414 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
415 dev->devnum, fmt->iface, fmt->altsetting,
416 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Daniel Macke5779992010-03-04 19:46:13 +0100417 return -EINVAL;
418 }
Daniel Mackc75a8a72012-04-12 13:51:14 +0200419
420 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
421 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
422
Daniel Mackedcd3632012-04-12 13:51:12 +0200423add_sync_ep:
424 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
425 alts, ep, !subs->direction,
Daniel Mackc75a8a72012-04-12 13:51:14 +0200426 implicit_fb ?
427 SND_USB_ENDPOINT_TYPE_DATA :
428 SND_USB_ENDPOINT_TYPE_SYNC);
Daniel Mackedcd3632012-04-12 13:51:12 +0200429 if (!subs->sync_endpoint)
430 return -EINVAL;
431
432 subs->data_endpoint->sync_master = subs->sync_endpoint;
433 }
Daniel Macke5779992010-03-04 19:46:13 +0100434
Eldad Zack71bb64c2013-08-03 10:50:17 +0200435 return 0;
436}
437
438/*
439 * find a matching format and set up the interface
440 */
441static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
442{
443 struct usb_device *dev = subs->dev;
444 struct usb_host_interface *alts;
445 struct usb_interface_descriptor *altsd;
446 struct usb_interface *iface;
447 int err;
448
449 iface = usb_ifnum_to_if(dev, fmt->iface);
450 if (WARN_ON(!iface))
451 return -EINVAL;
452 alts = &iface->altsetting[fmt->altset_idx];
453 altsd = get_iface_desc(alts);
454 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
455 return -EINVAL;
456
457 if (fmt == subs->cur_audiofmt)
458 return 0;
459
460 /* close the old interface */
461 if (subs->interface >= 0 && subs->interface != fmt->iface) {
462 err = usb_set_interface(subs->dev, subs->interface, 0);
463 if (err < 0) {
464 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
465 dev->devnum, fmt->iface, fmt->altsetting, err);
466 return -EIO;
467 }
468 subs->interface = -1;
469 subs->altset_idx = 0;
470 }
471
472 /* set interface */
473 if (subs->interface != fmt->iface ||
474 subs->altset_idx != fmt->altset_idx) {
475 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
476 if (err < 0) {
477 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
478 dev->devnum, fmt->iface, fmt->altsetting, err);
479 return -EIO;
480 }
481 snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
482 fmt->iface, fmt->altsetting);
483 subs->interface = fmt->iface;
484 subs->altset_idx = fmt->altset_idx;
485
486 snd_usb_set_interface_quirk(dev);
487 }
488
489 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
490 alts, fmt->endpoint, subs->direction,
491 SND_USB_ENDPOINT_TYPE_DATA);
492
493 if (!subs->data_endpoint)
494 return -EINVAL;
495
496 err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
497 if (err < 0)
498 return err;
499
Eldad Zackd133f2c2013-08-03 10:50:16 +0200500 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
501 if (err < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100502 return err;
503
504 subs->cur_audiofmt = fmt;
505
506 snd_usb_set_format_quirk(subs, fmt);
507
Daniel Macke5779992010-03-04 19:46:13 +0100508 return 0;
509}
510
511/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100512 * Return the score of matching two audioformats.
513 * Veto the audioformat if:
514 * - It has no channels for some reason.
515 * - Requested PCM format is not supported.
516 * - Requested sample rate is not supported.
517 */
518static int match_endpoint_audioformats(struct audioformat *fp,
519 struct audioformat *match, int rate,
520 snd_pcm_format_t pcm_format)
521{
522 int i;
523 int score = 0;
524
525 if (fp->channels < 1) {
526 snd_printdd("%s: (fmt @%p) no channels\n", __func__, fp);
527 return 0;
528 }
529
Eldad Zack74c34ca2013-04-23 01:00:41 +0200530 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
Eldad Zack0d9741c2012-12-03 20:30:09 +0100531 snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__,
532 fp, pcm_format);
533 return 0;
534 }
535
536 for (i = 0; i < fp->nr_rates; i++) {
537 if (fp->rate_table[i] == rate) {
538 score++;
539 break;
540 }
541 }
542 if (!score) {
543 snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__,
544 fp, rate);
545 return 0;
546 }
547
548 if (fp->channels == match->channels)
549 score++;
550
551 snd_printdd("%s: (fmt @%p) score %d\n", __func__, fp, score);
552
553 return score;
554}
555
556/*
557 * Configure the sync ep using the rate and pcm format of the data ep.
558 */
559static int configure_sync_endpoint(struct snd_usb_substream *subs)
560{
561 int ret;
562 struct audioformat *fp;
563 struct audioformat *sync_fp = NULL;
564 int cur_score = 0;
565 int sync_period_bytes = subs->period_bytes;
566 struct snd_usb_substream *sync_subs =
567 &subs->stream->substream[subs->direction ^ 1];
568
Takashi Iwai31be5422013-01-10 14:06:38 +0100569 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
570 !subs->stream)
571 return snd_usb_endpoint_set_params(subs->sync_endpoint,
572 subs->pcm_format,
573 subs->channels,
574 subs->period_bytes,
575 subs->cur_rate,
576 subs->cur_audiofmt,
577 NULL);
578
Eldad Zack0d9741c2012-12-03 20:30:09 +0100579 /* Try to find the best matching audioformat. */
580 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
581 int score = match_endpoint_audioformats(fp, subs->cur_audiofmt,
582 subs->cur_rate, subs->pcm_format);
583
584 if (score > cur_score) {
585 sync_fp = fp;
586 cur_score = score;
587 }
588 }
589
590 if (unlikely(sync_fp == NULL)) {
591 snd_printk(KERN_ERR "%s: no valid audioformat for sync ep %x found\n",
592 __func__, sync_subs->ep_num);
593 return -EINVAL;
594 }
595
596 /*
597 * Recalculate the period bytes if channel number differ between
598 * data and sync ep audioformat.
599 */
600 if (sync_fp->channels != subs->channels) {
601 sync_period_bytes = (subs->period_bytes / subs->channels) *
602 sync_fp->channels;
603 snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n",
604 __func__, subs->period_bytes, sync_period_bytes);
605 }
606
607 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
608 subs->pcm_format,
609 sync_fp->channels,
610 sync_period_bytes,
611 subs->cur_rate,
612 sync_fp,
613 NULL);
614
615 return ret;
616}
617
618/*
Dylan Reid61a70952012-09-18 09:49:48 -0700619 * configure endpoint params
620 *
621 * called during initial setup and upon resume
622 */
623static int configure_endpoint(struct snd_usb_substream *subs)
624{
625 int ret;
626
Dylan Reid61a70952012-09-18 09:49:48 -0700627 /* format changed */
Takashi Iwaib0db6062012-11-21 08:35:42 +0100628 stop_endpoints(subs, true);
Dylan Reid61a70952012-09-18 09:49:48 -0700629 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
630 subs->pcm_format,
631 subs->channels,
632 subs->period_bytes,
633 subs->cur_rate,
634 subs->cur_audiofmt,
635 subs->sync_endpoint);
636 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200637 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700638
639 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100640 ret = configure_sync_endpoint(subs);
641
Dylan Reid61a70952012-09-18 09:49:48 -0700642 return ret;
643}
644
645/*
Daniel Macke5779992010-03-04 19:46:13 +0100646 * hw_params callback
647 *
648 * allocate a buffer and set the given audio format.
649 *
650 * so far we use a physically linear buffer although packetize transfer
651 * doesn't need a continuous area.
652 * if sg buffer is supported on the later version of alsa, we'll follow
653 * that.
654 */
655static int snd_usb_hw_params(struct snd_pcm_substream *substream,
656 struct snd_pcm_hw_params *hw_params)
657{
658 struct snd_usb_substream *subs = substream->runtime->private_data;
659 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700660 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100661
662 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
663 params_buffer_bytes(hw_params));
664 if (ret < 0)
665 return ret;
666
Dylan Reid61a70952012-09-18 09:49:48 -0700667 subs->pcm_format = params_format(hw_params);
668 subs->period_bytes = params_period_bytes(hw_params);
669 subs->channels = params_channels(hw_params);
670 subs->cur_rate = params_rate(hw_params);
671
672 fmt = find_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100673 if (!fmt) {
674 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700675 subs->pcm_format, subs->cur_rate, subs->channels);
Daniel Macke5779992010-03-04 19:46:13 +0100676 return -EINVAL;
677 }
678
Takashi Iwai34f3c892012-10-15 12:16:02 +0200679 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200680 if (subs->stream->chip->shutdown)
681 ret = -ENODEV;
682 else
683 ret = set_format(subs, fmt);
Takashi Iwai34f3c892012-10-15 12:16:02 +0200684 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200685 if (ret < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100686 return ret;
687
Dylan Reid61a70952012-09-18 09:49:48 -0700688 subs->interface = fmt->iface;
689 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200690 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100691
Dylan Reid61a70952012-09-18 09:49:48 -0700692 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100693}
694
695/*
696 * hw_free callback
697 *
698 * reset the audio format and release the buffer
699 */
700static int snd_usb_hw_free(struct snd_pcm_substream *substream)
701{
702 struct snd_usb_substream *subs = substream->runtime->private_data;
703
704 subs->cur_audiofmt = NULL;
705 subs->cur_rate = 0;
706 subs->period_bytes = 0;
Takashi Iwai34f3c892012-10-15 12:16:02 +0200707 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200708 if (!subs->stream->chip->shutdown) {
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100709 stop_endpoints(subs, true);
Takashi Iwai978520b2012-10-12 15:12:55 +0200710 deactivate_endpoints(subs);
711 }
Takashi Iwai34f3c892012-10-15 12:16:02 +0200712 up_read(&subs->stream->chip->shutdown_rwsem);
Daniel Macke5779992010-03-04 19:46:13 +0100713 return snd_pcm_lib_free_vmalloc_buffer(substream);
714}
715
716/*
717 * prepare callback
718 *
719 * only a few subtle things...
720 */
721static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
722{
723 struct snd_pcm_runtime *runtime = substream->runtime;
724 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700725 struct usb_host_interface *alts;
726 struct usb_interface *iface;
727 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100728
729 if (! subs->cur_audiofmt) {
730 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
731 return -ENXIO;
732 }
733
Takashi Iwai34f3c892012-10-15 12:16:02 +0200734 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200735 if (subs->stream->chip->shutdown) {
736 ret = -ENODEV;
737 goto unlock;
738 }
739 if (snd_BUG_ON(!subs->data_endpoint)) {
740 ret = -EIO;
741 goto unlock;
742 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200743
Takashi Iwaif58161b2012-11-08 08:52:45 +0100744 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
745 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
746
Dylan Reid61a70952012-09-18 09:49:48 -0700747 ret = set_format(subs, subs->cur_audiofmt);
748 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200749 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700750
751 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
752 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
753 ret = snd_usb_init_sample_rate(subs->stream->chip,
754 subs->cur_audiofmt->iface,
755 alts,
756 subs->cur_audiofmt,
757 subs->cur_rate);
758 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200759 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700760
Takashi Iwai384dc0852012-09-18 14:49:31 +0200761 if (subs->need_setup_ep) {
762 ret = configure_endpoint(subs);
763 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200764 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200765 subs->need_setup_ep = false;
766 }
Dylan Reid61a70952012-09-18 09:49:48 -0700767
Daniel Macke5779992010-03-04 19:46:13 +0100768 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +0200769 subs->data_endpoint->maxframesize =
770 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
771 subs->data_endpoint->curframesize =
772 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +0100773
774 /* reset the pointer */
775 subs->hwptr_done = 0;
776 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500777 subs->last_delay = 0;
778 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100779 runtime->delay = 0;
780
Daniel Mackedcd3632012-04-12 13:51:12 +0200781 /* for playback, submit the URBs now; otherwise, the first hwptr_done
782 * updates for all URBs would happen at the same time when starting */
783 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100784 ret = start_endpoints(subs, true);
Daniel Mackedcd3632012-04-12 13:51:12 +0200785
Takashi Iwai978520b2012-10-12 15:12:55 +0200786 unlock:
Takashi Iwai34f3c892012-10-15 12:16:02 +0200787 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200788 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100789}
790
791static struct snd_pcm_hardware snd_usb_hardware =
792{
793 .info = SNDRV_PCM_INFO_MMAP |
794 SNDRV_PCM_INFO_MMAP_VALID |
795 SNDRV_PCM_INFO_BATCH |
796 SNDRV_PCM_INFO_INTERLEAVED |
797 SNDRV_PCM_INFO_BLOCK_TRANSFER |
798 SNDRV_PCM_INFO_PAUSE,
799 .buffer_bytes_max = 1024 * 1024,
800 .period_bytes_min = 64,
801 .period_bytes_max = 512 * 1024,
802 .periods_min = 2,
803 .periods_max = 1024,
804};
805
806static int hw_check_valid_format(struct snd_usb_substream *subs,
807 struct snd_pcm_hw_params *params,
808 struct audioformat *fp)
809{
810 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
811 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
812 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
813 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100814 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100815 unsigned int ptime;
816
817 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100818 snd_mask_none(&check_fmts);
819 check_fmts.bits[0] = (u32)fp->formats;
820 check_fmts.bits[1] = (u32)(fp->formats >> 32);
821 snd_mask_intersect(&check_fmts, fmts);
822 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100823 hwc_debug(" > check: no supported format %d\n", fp->format);
824 return 0;
825 }
826 /* check the channels */
827 if (fp->channels < ct->min || fp->channels > ct->max) {
828 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
829 return 0;
830 }
831 /* check the rate is within the range */
832 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
833 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
834 return 0;
835 }
836 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
837 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
838 return 0;
839 }
840 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +0200841 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100842 ptime = 125 * (1 << fp->datainterval);
843 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
844 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
845 return 0;
846 }
847 }
848 return 1;
849}
850
851static int hw_rule_rate(struct snd_pcm_hw_params *params,
852 struct snd_pcm_hw_rule *rule)
853{
854 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +0200855 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100856 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
857 unsigned int rmin, rmax;
858 int changed;
859
860 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
861 changed = 0;
862 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +0200863 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +0100864 if (!hw_check_valid_format(subs, params, fp))
865 continue;
866 if (changed++) {
867 if (rmin > fp->rate_min)
868 rmin = fp->rate_min;
869 if (rmax < fp->rate_max)
870 rmax = fp->rate_max;
871 } else {
872 rmin = fp->rate_min;
873 rmax = fp->rate_max;
874 }
875 }
876
877 if (!changed) {
878 hwc_debug(" --> get empty\n");
879 it->empty = 1;
880 return -EINVAL;
881 }
882
883 changed = 0;
884 if (it->min < rmin) {
885 it->min = rmin;
886 it->openmin = 0;
887 changed = 1;
888 }
889 if (it->max > rmax) {
890 it->max = rmax;
891 it->openmax = 0;
892 changed = 1;
893 }
894 if (snd_interval_checkempty(it)) {
895 it->empty = 1;
896 return -EINVAL;
897 }
898 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
899 return changed;
900}
901
902
903static int hw_rule_channels(struct snd_pcm_hw_params *params,
904 struct snd_pcm_hw_rule *rule)
905{
906 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +0200907 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100908 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
909 unsigned int rmin, rmax;
910 int changed;
911
912 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
913 changed = 0;
914 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +0200915 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +0100916 if (!hw_check_valid_format(subs, params, fp))
917 continue;
918 if (changed++) {
919 if (rmin > fp->channels)
920 rmin = fp->channels;
921 if (rmax < fp->channels)
922 rmax = fp->channels;
923 } else {
924 rmin = fp->channels;
925 rmax = fp->channels;
926 }
927 }
928
929 if (!changed) {
930 hwc_debug(" --> get empty\n");
931 it->empty = 1;
932 return -EINVAL;
933 }
934
935 changed = 0;
936 if (it->min < rmin) {
937 it->min = rmin;
938 it->openmin = 0;
939 changed = 1;
940 }
941 if (it->max > rmax) {
942 it->max = rmax;
943 it->openmax = 0;
944 changed = 1;
945 }
946 if (snd_interval_checkempty(it)) {
947 it->empty = 1;
948 return -EINVAL;
949 }
950 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
951 return changed;
952}
953
954static int hw_rule_format(struct snd_pcm_hw_params *params,
955 struct snd_pcm_hw_rule *rule)
956{
957 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +0200958 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100959 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
960 u64 fbits;
961 u32 oldbits[2];
962 int changed;
963
964 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
965 fbits = 0;
Eldad Zack88766f02013-04-03 23:18:49 +0200966 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +0100967 if (!hw_check_valid_format(subs, params, fp))
968 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100969 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +0100970 }
971
972 oldbits[0] = fmt->bits[0];
973 oldbits[1] = fmt->bits[1];
974 fmt->bits[0] &= (u32)fbits;
975 fmt->bits[1] &= (u32)(fbits >> 32);
976 if (!fmt->bits[0] && !fmt->bits[1]) {
977 hwc_debug(" --> get empty\n");
978 return -EINVAL;
979 }
980 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
981 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
982 return changed;
983}
984
985static int hw_rule_period_time(struct snd_pcm_hw_params *params,
986 struct snd_pcm_hw_rule *rule)
987{
988 struct snd_usb_substream *subs = rule->private;
989 struct audioformat *fp;
990 struct snd_interval *it;
991 unsigned char min_datainterval;
992 unsigned int pmin;
993 int changed;
994
995 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
996 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
997 min_datainterval = 0xff;
998 list_for_each_entry(fp, &subs->fmt_list, list) {
999 if (!hw_check_valid_format(subs, params, fp))
1000 continue;
1001 min_datainterval = min(min_datainterval, fp->datainterval);
1002 }
1003 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +02001004 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +01001005 it->empty = 1;
1006 return -EINVAL;
1007 }
1008 pmin = 125 * (1 << min_datainterval);
1009 changed = 0;
1010 if (it->min < pmin) {
1011 it->min = pmin;
1012 it->openmin = 0;
1013 changed = 1;
1014 }
1015 if (snd_interval_checkempty(it)) {
1016 it->empty = 1;
1017 return -EINVAL;
1018 }
1019 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1020 return changed;
1021}
1022
1023/*
1024 * If the device supports unusual bit rates, does the request meet these?
1025 */
1026static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1027 struct snd_usb_substream *subs)
1028{
1029 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001030 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +01001031 int count = 0, needs_knot = 0;
1032 int err;
1033
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +02001034 kfree(subs->rate_list.list);
1035 subs->rate_list.list = NULL;
1036
Daniel Macke5779992010-03-04 19:46:13 +01001037 list_for_each_entry(fp, &subs->fmt_list, list) {
1038 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1039 return 0;
1040 count += fp->nr_rates;
1041 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1042 needs_knot = 1;
1043 }
1044 if (!needs_knot)
1045 return 0;
1046
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001047 subs->rate_list.list = rate_list =
1048 kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +02001049 if (!subs->rate_list.list)
1050 return -ENOMEM;
1051 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +01001052 subs->rate_list.mask = 0;
1053 count = 0;
1054 list_for_each_entry(fp, &subs->fmt_list, list) {
1055 int i;
1056 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001057 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +01001058 }
1059 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1060 &subs->rate_list);
1061 if (err < 0)
1062 return err;
1063
1064 return 0;
1065}
1066
1067
1068/*
1069 * set up the runtime hardware information.
1070 */
1071
1072static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1073{
Eldad Zack88766f02013-04-03 23:18:49 +02001074 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001075 unsigned int pt, ptmin;
1076 int param_period_time_if_needed;
1077 int err;
1078
1079 runtime->hw.formats = subs->formats;
1080
1081 runtime->hw.rate_min = 0x7fffffff;
1082 runtime->hw.rate_max = 0;
1083 runtime->hw.channels_min = 256;
1084 runtime->hw.channels_max = 0;
1085 runtime->hw.rates = 0;
1086 ptmin = UINT_MAX;
1087 /* check min/max rates and channels */
Eldad Zack88766f02013-04-03 23:18:49 +02001088 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001089 runtime->hw.rates |= fp->rates;
1090 if (runtime->hw.rate_min > fp->rate_min)
1091 runtime->hw.rate_min = fp->rate_min;
1092 if (runtime->hw.rate_max < fp->rate_max)
1093 runtime->hw.rate_max = fp->rate_max;
1094 if (runtime->hw.channels_min > fp->channels)
1095 runtime->hw.channels_min = fp->channels;
1096 if (runtime->hw.channels_max < fp->channels)
1097 runtime->hw.channels_max = fp->channels;
1098 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1099 /* FIXME: there might be more than one audio formats... */
1100 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1101 fp->frame_size;
1102 }
1103 pt = 125 * (1 << fp->datainterval);
1104 ptmin = min(ptmin, pt);
1105 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001106 err = snd_usb_autoresume(subs->stream->chip);
1107 if (err < 0)
1108 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001109
1110 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001111 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001112 /* full speed devices have fixed data packet interval */
1113 ptmin = 1000;
1114 if (ptmin == 1000)
1115 /* if period time doesn't go below 1 ms, no rules needed */
1116 param_period_time_if_needed = -1;
1117 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1118 ptmin, UINT_MAX);
1119
1120 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1121 hw_rule_rate, subs,
1122 SNDRV_PCM_HW_PARAM_FORMAT,
1123 SNDRV_PCM_HW_PARAM_CHANNELS,
1124 param_period_time_if_needed,
1125 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001126 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001127 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1128 hw_rule_channels, subs,
1129 SNDRV_PCM_HW_PARAM_FORMAT,
1130 SNDRV_PCM_HW_PARAM_RATE,
1131 param_period_time_if_needed,
1132 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001133 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001134 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1135 hw_rule_format, subs,
1136 SNDRV_PCM_HW_PARAM_RATE,
1137 SNDRV_PCM_HW_PARAM_CHANNELS,
1138 param_period_time_if_needed,
1139 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001140 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001141 if (param_period_time_if_needed >= 0) {
1142 err = snd_pcm_hw_rule_add(runtime, 0,
1143 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1144 hw_rule_period_time, subs,
1145 SNDRV_PCM_HW_PARAM_FORMAT,
1146 SNDRV_PCM_HW_PARAM_CHANNELS,
1147 SNDRV_PCM_HW_PARAM_RATE,
1148 -1);
1149 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001150 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001151 }
1152 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001153 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001154 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +01001155
1156rep_err:
1157 snd_usb_autosuspend(subs->stream->chip);
1158 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001159}
1160
1161static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1162{
1163 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1164 struct snd_pcm_runtime *runtime = substream->runtime;
1165 struct snd_usb_substream *subs = &as->substream[direction];
1166
1167 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001168 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001169 runtime->hw = snd_usb_hardware;
1170 runtime->private_data = subs;
1171 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001172 /* runtime PM is also done there */
Daniel Mackd24f5062013-04-17 00:01:38 +08001173
1174 /* initialize DSD/DOP context */
1175 subs->dsd_dop.byte_idx = 0;
1176 subs->dsd_dop.channel = 0;
1177 subs->dsd_dop.marker = 1;
1178
Daniel Macke5779992010-03-04 19:46:13 +01001179 return setup_hw_info(runtime, subs);
1180}
1181
1182static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1183{
1184 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1185 struct snd_usb_substream *subs = &as->substream[direction];
1186
Takashi Iwaib0db6062012-11-21 08:35:42 +01001187 stop_endpoints(subs, true);
Daniel Mack68e67f42012-07-12 13:08:40 +02001188
1189 if (!as->chip->shutdown && subs->interface >= 0) {
1190 usb_set_interface(subs->dev, subs->interface, 0);
1191 subs->interface = -1;
1192 }
1193
Daniel Macke5779992010-03-04 19:46:13 +01001194 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001195 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001196
Daniel Mack68e67f42012-07-12 13:08:40 +02001197 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001198}
1199
1200/* Since a URB can handle only a single linear buffer, we must use double
1201 * buffering when the data to be transferred overflows the buffer boundary.
1202 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1203 * for all URBs.
1204 */
1205static void retire_capture_urb(struct snd_usb_substream *subs,
1206 struct urb *urb)
1207{
1208 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1209 unsigned int stride, frames, bytes, oldptr;
1210 int i, period_elapsed = 0;
1211 unsigned long flags;
1212 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001213 int current_frame_number;
1214
1215 /* read frame number here, update pointer in critical section */
1216 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001217
1218 stride = runtime->frame_bits >> 3;
1219
1220 for (i = 0; i < urb->number_of_packets; i++) {
Calvin Owens1539d4f2013-04-12 22:33:59 -05001221 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
Daniel Mackedcd3632012-04-12 13:51:12 +02001222 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1223 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1224 // continue;
1225 }
1226 bytes = urb->iso_frame_desc[i].actual_length;
1227 frames = bytes / stride;
1228 if (!subs->txfr_quirk)
1229 bytes = frames * stride;
1230 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001231 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001232 bytes = frames * stride;
1233 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1234 oldbytes, bytes);
1235 }
1236 /* update the current pointer */
1237 spin_lock_irqsave(&subs->lock, flags);
1238 oldptr = subs->hwptr_done;
1239 subs->hwptr_done += bytes;
1240 if (subs->hwptr_done >= runtime->buffer_size * stride)
1241 subs->hwptr_done -= runtime->buffer_size * stride;
1242 frames = (bytes + (oldptr % stride)) / stride;
1243 subs->transfer_done += frames;
1244 if (subs->transfer_done >= runtime->period_size) {
1245 subs->transfer_done -= runtime->period_size;
1246 period_elapsed = 1;
1247 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001248 /* capture delay is by construction limited to one URB,
1249 * reset delays here
1250 */
1251 runtime->delay = subs->last_delay = 0;
1252
1253 /* realign last_frame_number */
1254 subs->last_frame_number = current_frame_number;
1255 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1256
Daniel Mackedcd3632012-04-12 13:51:12 +02001257 spin_unlock_irqrestore(&subs->lock, flags);
1258 /* copy a data chunk */
1259 if (oldptr + bytes > runtime->buffer_size * stride) {
1260 unsigned int bytes1 =
1261 runtime->buffer_size * stride - oldptr;
1262 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1263 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1264 } else {
1265 memcpy(runtime->dma_area + oldptr, cp, bytes);
1266 }
1267 }
1268
1269 if (period_elapsed)
1270 snd_pcm_period_elapsed(subs->pcm_substream);
1271}
1272
Daniel Mackd24f5062013-04-17 00:01:38 +08001273static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1274 struct urb *urb, unsigned int bytes)
1275{
1276 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1277 unsigned int stride = runtime->frame_bits >> 3;
1278 unsigned int dst_idx = 0;
1279 unsigned int src_idx = subs->hwptr_done;
1280 unsigned int wrap = runtime->buffer_size * stride;
1281 u8 *dst = urb->transfer_buffer;
1282 u8 *src = runtime->dma_area;
1283 u8 marker[] = { 0x05, 0xfa };
1284
1285 /*
1286 * The DSP DOP format defines a way to transport DSD samples over
1287 * normal PCM data endpoints. It requires stuffing of marker bytes
1288 * (0x05 and 0xfa, alternating per sample frame), and then expects
1289 * 2 additional bytes of actual payload. The whole frame is stored
1290 * LSB.
1291 *
1292 * Hence, for a stereo transport, the buffer layout looks like this,
1293 * where L refers to left channel samples and R to right.
1294 *
1295 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1296 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1297 * .....
1298 *
1299 */
1300
1301 while (bytes--) {
1302 if (++subs->dsd_dop.byte_idx == 3) {
1303 /* frame boundary? */
1304 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1305 src_idx += 2;
1306 subs->dsd_dop.byte_idx = 0;
1307
1308 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1309 /* alternate the marker */
1310 subs->dsd_dop.marker++;
1311 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1312 subs->dsd_dop.channel = 0;
1313 }
1314 } else {
1315 /* stuff the DSD payload */
1316 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001317
1318 if (subs->cur_audiofmt->dsd_bitrev)
1319 dst[dst_idx++] = bitrev8(src[idx]);
1320 else
1321 dst[dst_idx++] = src[idx];
1322
Daniel Mackd24f5062013-04-17 00:01:38 +08001323 subs->hwptr_done++;
1324 }
1325 }
1326}
1327
Daniel Mackedcd3632012-04-12 13:51:12 +02001328static void prepare_playback_urb(struct snd_usb_substream *subs,
1329 struct urb *urb)
1330{
1331 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001332 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001333 struct snd_urb_ctx *ctx = urb->context;
1334 unsigned int counts, frames, bytes;
1335 int i, stride, period_elapsed = 0;
1336 unsigned long flags;
1337
1338 stride = runtime->frame_bits >> 3;
1339
1340 frames = 0;
1341 urb->number_of_packets = 0;
1342 spin_lock_irqsave(&subs->lock, flags);
1343 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001344 if (ctx->packet_size[i])
1345 counts = ctx->packet_size[i];
1346 else
1347 counts = snd_usb_endpoint_next_packet_size(ep);
1348
Daniel Mackedcd3632012-04-12 13:51:12 +02001349 /* set up descriptor */
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001350 urb->iso_frame_desc[i].offset = frames * ep->stride;
1351 urb->iso_frame_desc[i].length = counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001352 frames += counts;
1353 urb->number_of_packets++;
1354 subs->transfer_done += counts;
1355 if (subs->transfer_done >= runtime->period_size) {
1356 subs->transfer_done -= runtime->period_size;
1357 period_elapsed = 1;
1358 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1359 if (subs->transfer_done > 0) {
1360 /* FIXME: fill-max mode is not
1361 * supported yet */
1362 frames -= subs->transfer_done;
1363 counts -= subs->transfer_done;
1364 urb->iso_frame_desc[i].length =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001365 counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001366 subs->transfer_done = 0;
1367 }
1368 i++;
1369 if (i < ctx->packets) {
1370 /* add a transfer delimiter */
1371 urb->iso_frame_desc[i].offset =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001372 frames * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001373 urb->iso_frame_desc[i].length = 0;
1374 urb->number_of_packets++;
1375 }
1376 break;
1377 }
1378 }
1379 if (period_elapsed &&
Eldad Zack98ae4722013-04-03 23:18:52 +02001380 !snd_usb_endpoint_implicit_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
Daniel Mackedcd3632012-04-12 13:51:12 +02001381 break;
1382 }
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001383 bytes = frames * ep->stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001384
1385 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1386 subs->cur_audiofmt->dsd_dop)) {
1387 fill_playback_urb_dsd_dop(subs, urb, bytes);
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001388 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1389 subs->cur_audiofmt->dsd_bitrev)) {
1390 /* bit-reverse the bytes */
1391 u8 *buf = urb->transfer_buffer;
1392 for (i = 0; i < bytes; i++) {
1393 int idx = (subs->hwptr_done + i)
1394 % (runtime->buffer_size * stride);
1395 buf[i] = bitrev8(runtime->dma_area[idx]);
1396 }
1397
1398 subs->hwptr_done += bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001399 } else {
Daniel Mackd24f5062013-04-17 00:01:38 +08001400 /* usual PCM */
1401 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1402 /* err, the transferred area goes over buffer boundary. */
1403 unsigned int bytes1 =
1404 runtime->buffer_size * stride - subs->hwptr_done;
1405 memcpy(urb->transfer_buffer,
1406 runtime->dma_area + subs->hwptr_done, bytes1);
1407 memcpy(urb->transfer_buffer + bytes1,
1408 runtime->dma_area, bytes - bytes1);
1409 } else {
1410 memcpy(urb->transfer_buffer,
1411 runtime->dma_area + subs->hwptr_done, bytes);
1412 }
1413
1414 subs->hwptr_done += bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001415 }
Daniel Mackd24f5062013-04-17 00:01:38 +08001416
Daniel Mackedcd3632012-04-12 13:51:12 +02001417 if (subs->hwptr_done >= runtime->buffer_size * stride)
1418 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001419
1420 /* update delay with exact number of samples queued */
1421 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001422 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001423 subs->last_delay = runtime->delay;
1424
1425 /* realign last_frame_number */
1426 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1427 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1428
Daniel Mackedcd3632012-04-12 13:51:12 +02001429 spin_unlock_irqrestore(&subs->lock, flags);
1430 urb->transfer_buffer_length = bytes;
1431 if (period_elapsed)
1432 snd_pcm_period_elapsed(subs->pcm_substream);
1433}
1434
1435/*
1436 * process after playback data complete
1437 * - decrease the delay count again
1438 */
1439static void retire_playback_urb(struct snd_usb_substream *subs,
1440 struct urb *urb)
1441{
1442 unsigned long flags;
1443 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001444 struct snd_usb_endpoint *ep = subs->data_endpoint;
1445 int processed = urb->transfer_buffer_length / ep->stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001446 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001447
Takashi Iwai1213a202012-09-06 14:58:00 +02001448 /* ignore the delay accounting when procssed=0 is given, i.e.
1449 * silent payloads are procssed before handling the actual data
1450 */
1451 if (!processed)
1452 return;
1453
Daniel Mackedcd3632012-04-12 13:51:12 +02001454 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001455 if (!subs->last_delay)
1456 goto out; /* short path */
1457
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001458 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1459 /* update delay with exact number of samples played */
1460 if (processed > subs->last_delay)
1461 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001462 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001463 subs->last_delay -= processed;
1464 runtime->delay = subs->last_delay;
1465
1466 /*
1467 * Report when delay estimate is off by more than 2ms.
1468 * The error should be lower than 2ms since the estimate relies
1469 * on two reads of a counter updated every ms.
1470 */
1471 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1472 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1473 est_delay, subs->last_delay);
1474
Takashi Iwai48779a02012-11-23 16:00:37 +01001475 if (!subs->running) {
1476 /* update last_frame_number for delay counting here since
1477 * prepare_playback_urb won't be called during pause
1478 */
1479 subs->last_frame_number =
1480 usb_get_current_frame_number(subs->dev) & 0xff;
1481 }
1482
1483 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001484 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001485}
1486
1487static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1488{
1489 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1490}
1491
1492static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1493{
1494 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1495}
1496
1497static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1498{
1499 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1500}
1501
1502static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1503{
1504 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1505}
1506
Daniel Mackedcd3632012-04-12 13:51:12 +02001507static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1508 int cmd)
1509{
1510 struct snd_usb_substream *subs = substream->runtime->private_data;
1511
1512 switch (cmd) {
1513 case SNDRV_PCM_TRIGGER_START:
1514 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1515 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1516 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001517 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001518 return 0;
1519 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001520 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001521 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001522 return 0;
1523 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1524 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001525 /* keep retire_data_urb for delay calculation */
1526 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001527 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001528 return 0;
1529 }
1530
1531 return -EINVAL;
1532}
1533
Daniel Mackafe25962012-06-16 16:58:04 +02001534static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1535 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001536{
1537 int err;
1538 struct snd_usb_substream *subs = substream->runtime->private_data;
1539
1540 switch (cmd) {
1541 case SNDRV_PCM_TRIGGER_START:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001542 err = start_endpoints(subs, false);
Daniel Mackedcd3632012-04-12 13:51:12 +02001543 if (err < 0)
1544 return err;
1545
1546 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001547 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001548 return 0;
1549 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001550 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001551 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001552 return 0;
1553 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1554 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001555 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001556 return 0;
1557 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1558 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001559 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001560 return 0;
1561 }
1562
1563 return -EINVAL;
1564}
1565
Daniel Macke5779992010-03-04 19:46:13 +01001566static struct snd_pcm_ops snd_usb_playback_ops = {
1567 .open = snd_usb_playback_open,
1568 .close = snd_usb_playback_close,
1569 .ioctl = snd_pcm_lib_ioctl,
1570 .hw_params = snd_usb_hw_params,
1571 .hw_free = snd_usb_hw_free,
1572 .prepare = snd_usb_pcm_prepare,
1573 .trigger = snd_usb_substream_playback_trigger,
1574 .pointer = snd_usb_pcm_pointer,
1575 .page = snd_pcm_lib_get_vmalloc_page,
1576 .mmap = snd_pcm_lib_mmap_vmalloc,
1577};
1578
1579static struct snd_pcm_ops snd_usb_capture_ops = {
1580 .open = snd_usb_capture_open,
1581 .close = snd_usb_capture_close,
1582 .ioctl = snd_pcm_lib_ioctl,
1583 .hw_params = snd_usb_hw_params,
1584 .hw_free = snd_usb_hw_free,
1585 .prepare = snd_usb_pcm_prepare,
1586 .trigger = snd_usb_substream_capture_trigger,
1587 .pointer = snd_usb_pcm_pointer,
1588 .page = snd_pcm_lib_get_vmalloc_page,
1589 .mmap = snd_pcm_lib_mmap_vmalloc,
1590};
1591
1592void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1593{
1594 snd_pcm_set_ops(pcm, stream,
1595 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1596 &snd_usb_playback_ops : &snd_usb_capture_ops);
1597}