blob: b375d58871e7ce9f7eb0e63a3f73644984c6c5e1 [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 Zacka60945f2013-08-03 10:50:18 +0200330static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
331 struct usb_device *dev,
332 struct usb_interface_descriptor *altsd,
333 unsigned int attr)
Daniel Macke5779992010-03-04 19:46:13 +0100334{
Eldad Zacka60945f2013-08-03 10:50:18 +0200335 struct usb_host_interface *alts;
Daniel Macke5779992010-03-04 19:46:13 +0100336 struct usb_interface *iface;
Eldad Zacka60945f2013-08-03 10:50:18 +0200337 unsigned int ep;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200338
Eldad Zack914273c2013-08-03 10:50:21 +0200339 /* Implicit feedback sync EPs consumers are always playback EPs */
340 if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
341 return 0;
342
Daniel Mackc75a8a72012-04-12 13:51:14 +0200343 switch (subs->stream->chip->usb_id) {
Eldad Zackca10a7e2012-11-28 23:55:41 +0100344 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500345 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zack914273c2013-08-03 10:50:21 +0200346 ep = 0x81;
347 iface = usb_ifnum_to_if(dev, 3);
Eldad Zackca10a7e2012-11-28 23:55:41 +0100348
Eldad Zack914273c2013-08-03 10:50:21 +0200349 if (!iface || iface->num_altsetting == 0)
350 return -EINVAL;
Eldad Zackca10a7e2012-11-28 23:55:41 +0100351
Eldad Zack914273c2013-08-03 10:50:21 +0200352 alts = &iface->altsetting[1];
353 goto add_sync_ep;
Eldad Zackca10a7e2012-11-28 23:55:41 +0100354 break;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200355 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
356 case USB_ID(0x0763, 0x2081):
Eldad Zack914273c2013-08-03 10:50:21 +0200357 ep = 0x81;
358 iface = usb_ifnum_to_if(dev, 2);
Daniel Mackc75a8a72012-04-12 13:51:14 +0200359
Eldad Zack914273c2013-08-03 10:50:21 +0200360 if (!iface || iface->num_altsetting == 0)
361 return -EINVAL;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200362
Eldad Zack914273c2013-08-03 10:50:21 +0200363 alts = &iface->altsetting[1];
364 goto add_sync_ep;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200365 }
Eldad Zack914273c2013-08-03 10:50:21 +0200366 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100367 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
368 altsd->bInterfaceProtocol == 2 &&
369 altsd->bNumEndpoints == 1 &&
370 USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
371 search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
372 altsd->bAlternateSetting,
373 &alts, &ep) >= 0) {
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100374 goto add_sync_ep;
375 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200376
Eldad Zacka60945f2013-08-03 10:50:18 +0200377 /* No quirk */
378 return 0;
379
380add_sync_ep:
381 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
382 alts, ep, !subs->direction,
Eldad Zack88abb8e2013-08-03 10:51:14 +0200383 SND_USB_ENDPOINT_TYPE_DATA);
Eldad Zacka60945f2013-08-03 10:50:18 +0200384 if (!subs->sync_endpoint)
385 return -EINVAL;
386
387 subs->data_endpoint->sync_master = subs->sync_endpoint;
388
389 return 0;
390}
391
392static int set_sync_endpoint(struct snd_usb_substream *subs,
393 struct audioformat *fmt,
394 struct usb_device *dev,
395 struct usb_host_interface *alts,
396 struct usb_interface_descriptor *altsd)
397{
398 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
399 unsigned int ep, attr;
Eldad Zack95fec882013-08-03 10:50:20 +0200400 bool implicit_fb;
Eldad Zacka60945f2013-08-03 10:50:18 +0200401 int err;
402
403 /* we need a sync pipe in async OUT or adaptive IN mode */
404 /* check the number of EP, since some devices have broken
405 * descriptors which fool us. if it has only one EP,
406 * assume it as adaptive-out or sync-in.
407 */
408 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
409
410 err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
411 if (err < 0)
412 return err;
413
Eldad Zackf34d0652013-08-03 10:50:19 +0200414 if (altsd->bNumEndpoints < 2)
415 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200416
Eldad Zackf34d0652013-08-03 10:50:19 +0200417 if ((is_playback && attr != USB_ENDPOINT_SYNC_ASYNC) ||
418 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
419 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200420
Eldad Zackf34d0652013-08-03 10:50:19 +0200421 /* check sync-pipe endpoint */
422 /* ... and check descriptor size before accessing bSynchAddress
423 because there is a version of the SB Audigy 2 NX firmware lacking
424 the audio fields in the endpoint descriptors */
425 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
426 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zack95fec882013-08-03 10:50:20 +0200427 get_endpoint(alts, 1)->bSynchAddress != 0)) {
Eldad Zackf34d0652013-08-03 10:50:19 +0200428 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
429 dev->devnum, fmt->iface, fmt->altsetting,
430 get_endpoint(alts, 1)->bmAttributes,
431 get_endpoint(alts, 1)->bLength,
432 get_endpoint(alts, 1)->bSynchAddress);
433 return -EINVAL;
Daniel Mackedcd3632012-04-12 13:51:12 +0200434 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200435 ep = get_endpoint(alts, 1)->bEndpointAddress;
Eldad Zack95fec882013-08-03 10:50:20 +0200436 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zackf34d0652013-08-03 10:50:19 +0200437 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
438 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
439 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
440 dev->devnum, fmt->iface, fmt->altsetting,
441 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
442 return -EINVAL;
443 }
444
445 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
446 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
447
448 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
449 alts, ep, !subs->direction,
450 implicit_fb ?
451 SND_USB_ENDPOINT_TYPE_DATA :
452 SND_USB_ENDPOINT_TYPE_SYNC);
453 if (!subs->sync_endpoint)
454 return -EINVAL;
455
456 subs->data_endpoint->sync_master = subs->sync_endpoint;
Daniel Macke5779992010-03-04 19:46:13 +0100457
Eldad Zack71bb64c2013-08-03 10:50:17 +0200458 return 0;
459}
460
461/*
462 * find a matching format and set up the interface
463 */
464static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
465{
466 struct usb_device *dev = subs->dev;
467 struct usb_host_interface *alts;
468 struct usb_interface_descriptor *altsd;
469 struct usb_interface *iface;
470 int err;
471
472 iface = usb_ifnum_to_if(dev, fmt->iface);
473 if (WARN_ON(!iface))
474 return -EINVAL;
475 alts = &iface->altsetting[fmt->altset_idx];
476 altsd = get_iface_desc(alts);
477 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
478 return -EINVAL;
479
480 if (fmt == subs->cur_audiofmt)
481 return 0;
482
483 /* close the old interface */
484 if (subs->interface >= 0 && subs->interface != fmt->iface) {
485 err = usb_set_interface(subs->dev, subs->interface, 0);
486 if (err < 0) {
487 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
488 dev->devnum, fmt->iface, fmt->altsetting, err);
489 return -EIO;
490 }
491 subs->interface = -1;
492 subs->altset_idx = 0;
493 }
494
495 /* set interface */
496 if (subs->interface != fmt->iface ||
497 subs->altset_idx != fmt->altset_idx) {
498 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
499 if (err < 0) {
500 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
501 dev->devnum, fmt->iface, fmt->altsetting, err);
502 return -EIO;
503 }
504 snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
505 fmt->iface, fmt->altsetting);
506 subs->interface = fmt->iface;
507 subs->altset_idx = fmt->altset_idx;
508
509 snd_usb_set_interface_quirk(dev);
510 }
511
512 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
513 alts, fmt->endpoint, subs->direction,
514 SND_USB_ENDPOINT_TYPE_DATA);
515
516 if (!subs->data_endpoint)
517 return -EINVAL;
518
519 err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
520 if (err < 0)
521 return err;
522
Eldad Zackd133f2c2013-08-03 10:50:16 +0200523 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
524 if (err < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100525 return err;
526
527 subs->cur_audiofmt = fmt;
528
529 snd_usb_set_format_quirk(subs, fmt);
530
Daniel Macke5779992010-03-04 19:46:13 +0100531 return 0;
532}
533
534/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100535 * Return the score of matching two audioformats.
536 * Veto the audioformat if:
537 * - It has no channels for some reason.
538 * - Requested PCM format is not supported.
539 * - Requested sample rate is not supported.
540 */
541static int match_endpoint_audioformats(struct audioformat *fp,
542 struct audioformat *match, int rate,
543 snd_pcm_format_t pcm_format)
544{
545 int i;
546 int score = 0;
547
548 if (fp->channels < 1) {
549 snd_printdd("%s: (fmt @%p) no channels\n", __func__, fp);
550 return 0;
551 }
552
Eldad Zack74c34ca2013-04-23 01:00:41 +0200553 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
Eldad Zack0d9741c2012-12-03 20:30:09 +0100554 snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__,
555 fp, pcm_format);
556 return 0;
557 }
558
559 for (i = 0; i < fp->nr_rates; i++) {
560 if (fp->rate_table[i] == rate) {
561 score++;
562 break;
563 }
564 }
565 if (!score) {
566 snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__,
567 fp, rate);
568 return 0;
569 }
570
571 if (fp->channels == match->channels)
572 score++;
573
574 snd_printdd("%s: (fmt @%p) score %d\n", __func__, fp, score);
575
576 return score;
577}
578
579/*
580 * Configure the sync ep using the rate and pcm format of the data ep.
581 */
582static int configure_sync_endpoint(struct snd_usb_substream *subs)
583{
584 int ret;
585 struct audioformat *fp;
586 struct audioformat *sync_fp = NULL;
587 int cur_score = 0;
588 int sync_period_bytes = subs->period_bytes;
589 struct snd_usb_substream *sync_subs =
590 &subs->stream->substream[subs->direction ^ 1];
591
Takashi Iwai31be5422013-01-10 14:06:38 +0100592 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
593 !subs->stream)
594 return snd_usb_endpoint_set_params(subs->sync_endpoint,
595 subs->pcm_format,
596 subs->channels,
597 subs->period_bytes,
598 subs->cur_rate,
599 subs->cur_audiofmt,
600 NULL);
601
Eldad Zack0d9741c2012-12-03 20:30:09 +0100602 /* Try to find the best matching audioformat. */
603 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
604 int score = match_endpoint_audioformats(fp, subs->cur_audiofmt,
605 subs->cur_rate, subs->pcm_format);
606
607 if (score > cur_score) {
608 sync_fp = fp;
609 cur_score = score;
610 }
611 }
612
613 if (unlikely(sync_fp == NULL)) {
614 snd_printk(KERN_ERR "%s: no valid audioformat for sync ep %x found\n",
615 __func__, sync_subs->ep_num);
616 return -EINVAL;
617 }
618
619 /*
620 * Recalculate the period bytes if channel number differ between
621 * data and sync ep audioformat.
622 */
623 if (sync_fp->channels != subs->channels) {
624 sync_period_bytes = (subs->period_bytes / subs->channels) *
625 sync_fp->channels;
626 snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n",
627 __func__, subs->period_bytes, sync_period_bytes);
628 }
629
630 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
631 subs->pcm_format,
632 sync_fp->channels,
633 sync_period_bytes,
634 subs->cur_rate,
635 sync_fp,
636 NULL);
637
638 return ret;
639}
640
641/*
Dylan Reid61a70952012-09-18 09:49:48 -0700642 * configure endpoint params
643 *
644 * called during initial setup and upon resume
645 */
646static int configure_endpoint(struct snd_usb_substream *subs)
647{
648 int ret;
649
Dylan Reid61a70952012-09-18 09:49:48 -0700650 /* format changed */
Takashi Iwaib0db6062012-11-21 08:35:42 +0100651 stop_endpoints(subs, true);
Dylan Reid61a70952012-09-18 09:49:48 -0700652 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
653 subs->pcm_format,
654 subs->channels,
655 subs->period_bytes,
656 subs->cur_rate,
657 subs->cur_audiofmt,
658 subs->sync_endpoint);
659 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200660 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700661
662 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100663 ret = configure_sync_endpoint(subs);
664
Dylan Reid61a70952012-09-18 09:49:48 -0700665 return ret;
666}
667
668/*
Daniel Macke5779992010-03-04 19:46:13 +0100669 * hw_params callback
670 *
671 * allocate a buffer and set the given audio format.
672 *
673 * so far we use a physically linear buffer although packetize transfer
674 * doesn't need a continuous area.
675 * if sg buffer is supported on the later version of alsa, we'll follow
676 * that.
677 */
678static int snd_usb_hw_params(struct snd_pcm_substream *substream,
679 struct snd_pcm_hw_params *hw_params)
680{
681 struct snd_usb_substream *subs = substream->runtime->private_data;
682 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700683 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100684
685 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
686 params_buffer_bytes(hw_params));
687 if (ret < 0)
688 return ret;
689
Dylan Reid61a70952012-09-18 09:49:48 -0700690 subs->pcm_format = params_format(hw_params);
691 subs->period_bytes = params_period_bytes(hw_params);
692 subs->channels = params_channels(hw_params);
693 subs->cur_rate = params_rate(hw_params);
694
695 fmt = find_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100696 if (!fmt) {
697 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700698 subs->pcm_format, subs->cur_rate, subs->channels);
Daniel Macke5779992010-03-04 19:46:13 +0100699 return -EINVAL;
700 }
701
Takashi Iwai34f3c892012-10-15 12:16:02 +0200702 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200703 if (subs->stream->chip->shutdown)
704 ret = -ENODEV;
705 else
706 ret = set_format(subs, fmt);
Takashi Iwai34f3c892012-10-15 12:16:02 +0200707 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200708 if (ret < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100709 return ret;
710
Dylan Reid61a70952012-09-18 09:49:48 -0700711 subs->interface = fmt->iface;
712 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200713 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100714
Dylan Reid61a70952012-09-18 09:49:48 -0700715 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100716}
717
718/*
719 * hw_free callback
720 *
721 * reset the audio format and release the buffer
722 */
723static int snd_usb_hw_free(struct snd_pcm_substream *substream)
724{
725 struct snd_usb_substream *subs = substream->runtime->private_data;
726
727 subs->cur_audiofmt = NULL;
728 subs->cur_rate = 0;
729 subs->period_bytes = 0;
Takashi Iwai34f3c892012-10-15 12:16:02 +0200730 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200731 if (!subs->stream->chip->shutdown) {
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100732 stop_endpoints(subs, true);
Takashi Iwai978520b2012-10-12 15:12:55 +0200733 deactivate_endpoints(subs);
734 }
Takashi Iwai34f3c892012-10-15 12:16:02 +0200735 up_read(&subs->stream->chip->shutdown_rwsem);
Daniel Macke5779992010-03-04 19:46:13 +0100736 return snd_pcm_lib_free_vmalloc_buffer(substream);
737}
738
739/*
740 * prepare callback
741 *
742 * only a few subtle things...
743 */
744static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
745{
746 struct snd_pcm_runtime *runtime = substream->runtime;
747 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700748 struct usb_host_interface *alts;
749 struct usb_interface *iface;
750 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100751
752 if (! subs->cur_audiofmt) {
753 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
754 return -ENXIO;
755 }
756
Takashi Iwai34f3c892012-10-15 12:16:02 +0200757 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200758 if (subs->stream->chip->shutdown) {
759 ret = -ENODEV;
760 goto unlock;
761 }
762 if (snd_BUG_ON(!subs->data_endpoint)) {
763 ret = -EIO;
764 goto unlock;
765 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200766
Takashi Iwaif58161b2012-11-08 08:52:45 +0100767 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
768 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
769
Dylan Reid61a70952012-09-18 09:49:48 -0700770 ret = set_format(subs, subs->cur_audiofmt);
771 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200772 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700773
774 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
775 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
776 ret = snd_usb_init_sample_rate(subs->stream->chip,
777 subs->cur_audiofmt->iface,
778 alts,
779 subs->cur_audiofmt,
780 subs->cur_rate);
781 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200782 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700783
Takashi Iwai384dc0852012-09-18 14:49:31 +0200784 if (subs->need_setup_ep) {
785 ret = configure_endpoint(subs);
786 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200787 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200788 subs->need_setup_ep = false;
789 }
Dylan Reid61a70952012-09-18 09:49:48 -0700790
Daniel Macke5779992010-03-04 19:46:13 +0100791 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +0200792 subs->data_endpoint->maxframesize =
793 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
794 subs->data_endpoint->curframesize =
795 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +0100796
797 /* reset the pointer */
798 subs->hwptr_done = 0;
799 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500800 subs->last_delay = 0;
801 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100802 runtime->delay = 0;
803
Daniel Mackedcd3632012-04-12 13:51:12 +0200804 /* for playback, submit the URBs now; otherwise, the first hwptr_done
805 * updates for all URBs would happen at the same time when starting */
806 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100807 ret = start_endpoints(subs, true);
Daniel Mackedcd3632012-04-12 13:51:12 +0200808
Takashi Iwai978520b2012-10-12 15:12:55 +0200809 unlock:
Takashi Iwai34f3c892012-10-15 12:16:02 +0200810 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200811 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100812}
813
814static struct snd_pcm_hardware snd_usb_hardware =
815{
816 .info = SNDRV_PCM_INFO_MMAP |
817 SNDRV_PCM_INFO_MMAP_VALID |
818 SNDRV_PCM_INFO_BATCH |
819 SNDRV_PCM_INFO_INTERLEAVED |
820 SNDRV_PCM_INFO_BLOCK_TRANSFER |
821 SNDRV_PCM_INFO_PAUSE,
822 .buffer_bytes_max = 1024 * 1024,
823 .period_bytes_min = 64,
824 .period_bytes_max = 512 * 1024,
825 .periods_min = 2,
826 .periods_max = 1024,
827};
828
829static int hw_check_valid_format(struct snd_usb_substream *subs,
830 struct snd_pcm_hw_params *params,
831 struct audioformat *fp)
832{
833 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
834 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
835 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
836 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100837 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100838 unsigned int ptime;
839
840 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100841 snd_mask_none(&check_fmts);
842 check_fmts.bits[0] = (u32)fp->formats;
843 check_fmts.bits[1] = (u32)(fp->formats >> 32);
844 snd_mask_intersect(&check_fmts, fmts);
845 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100846 hwc_debug(" > check: no supported format %d\n", fp->format);
847 return 0;
848 }
849 /* check the channels */
850 if (fp->channels < ct->min || fp->channels > ct->max) {
851 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
852 return 0;
853 }
854 /* check the rate is within the range */
855 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
856 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
857 return 0;
858 }
859 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
860 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
861 return 0;
862 }
863 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +0200864 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100865 ptime = 125 * (1 << fp->datainterval);
866 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
867 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
868 return 0;
869 }
870 }
871 return 1;
872}
873
874static int hw_rule_rate(struct snd_pcm_hw_params *params,
875 struct snd_pcm_hw_rule *rule)
876{
877 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +0200878 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100879 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
880 unsigned int rmin, rmax;
881 int changed;
882
883 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
884 changed = 0;
885 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +0200886 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +0100887 if (!hw_check_valid_format(subs, params, fp))
888 continue;
889 if (changed++) {
890 if (rmin > fp->rate_min)
891 rmin = fp->rate_min;
892 if (rmax < fp->rate_max)
893 rmax = fp->rate_max;
894 } else {
895 rmin = fp->rate_min;
896 rmax = fp->rate_max;
897 }
898 }
899
900 if (!changed) {
901 hwc_debug(" --> get empty\n");
902 it->empty = 1;
903 return -EINVAL;
904 }
905
906 changed = 0;
907 if (it->min < rmin) {
908 it->min = rmin;
909 it->openmin = 0;
910 changed = 1;
911 }
912 if (it->max > rmax) {
913 it->max = rmax;
914 it->openmax = 0;
915 changed = 1;
916 }
917 if (snd_interval_checkempty(it)) {
918 it->empty = 1;
919 return -EINVAL;
920 }
921 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
922 return changed;
923}
924
925
926static int hw_rule_channels(struct snd_pcm_hw_params *params,
927 struct snd_pcm_hw_rule *rule)
928{
929 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +0200930 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100931 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
932 unsigned int rmin, rmax;
933 int changed;
934
935 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
936 changed = 0;
937 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +0200938 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +0100939 if (!hw_check_valid_format(subs, params, fp))
940 continue;
941 if (changed++) {
942 if (rmin > fp->channels)
943 rmin = fp->channels;
944 if (rmax < fp->channels)
945 rmax = fp->channels;
946 } else {
947 rmin = fp->channels;
948 rmax = fp->channels;
949 }
950 }
951
952 if (!changed) {
953 hwc_debug(" --> get empty\n");
954 it->empty = 1;
955 return -EINVAL;
956 }
957
958 changed = 0;
959 if (it->min < rmin) {
960 it->min = rmin;
961 it->openmin = 0;
962 changed = 1;
963 }
964 if (it->max > rmax) {
965 it->max = rmax;
966 it->openmax = 0;
967 changed = 1;
968 }
969 if (snd_interval_checkempty(it)) {
970 it->empty = 1;
971 return -EINVAL;
972 }
973 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
974 return changed;
975}
976
977static int hw_rule_format(struct snd_pcm_hw_params *params,
978 struct snd_pcm_hw_rule *rule)
979{
980 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +0200981 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100982 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
983 u64 fbits;
984 u32 oldbits[2];
985 int changed;
986
987 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
988 fbits = 0;
Eldad Zack88766f02013-04-03 23:18:49 +0200989 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +0100990 if (!hw_check_valid_format(subs, params, fp))
991 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100992 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +0100993 }
994
995 oldbits[0] = fmt->bits[0];
996 oldbits[1] = fmt->bits[1];
997 fmt->bits[0] &= (u32)fbits;
998 fmt->bits[1] &= (u32)(fbits >> 32);
999 if (!fmt->bits[0] && !fmt->bits[1]) {
1000 hwc_debug(" --> get empty\n");
1001 return -EINVAL;
1002 }
1003 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1004 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1005 return changed;
1006}
1007
1008static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1009 struct snd_pcm_hw_rule *rule)
1010{
1011 struct snd_usb_substream *subs = rule->private;
1012 struct audioformat *fp;
1013 struct snd_interval *it;
1014 unsigned char min_datainterval;
1015 unsigned int pmin;
1016 int changed;
1017
1018 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1019 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1020 min_datainterval = 0xff;
1021 list_for_each_entry(fp, &subs->fmt_list, list) {
1022 if (!hw_check_valid_format(subs, params, fp))
1023 continue;
1024 min_datainterval = min(min_datainterval, fp->datainterval);
1025 }
1026 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +02001027 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +01001028 it->empty = 1;
1029 return -EINVAL;
1030 }
1031 pmin = 125 * (1 << min_datainterval);
1032 changed = 0;
1033 if (it->min < pmin) {
1034 it->min = pmin;
1035 it->openmin = 0;
1036 changed = 1;
1037 }
1038 if (snd_interval_checkempty(it)) {
1039 it->empty = 1;
1040 return -EINVAL;
1041 }
1042 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1043 return changed;
1044}
1045
1046/*
1047 * If the device supports unusual bit rates, does the request meet these?
1048 */
1049static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1050 struct snd_usb_substream *subs)
1051{
1052 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001053 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +01001054 int count = 0, needs_knot = 0;
1055 int err;
1056
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +02001057 kfree(subs->rate_list.list);
1058 subs->rate_list.list = NULL;
1059
Daniel Macke5779992010-03-04 19:46:13 +01001060 list_for_each_entry(fp, &subs->fmt_list, list) {
1061 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1062 return 0;
1063 count += fp->nr_rates;
1064 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1065 needs_knot = 1;
1066 }
1067 if (!needs_knot)
1068 return 0;
1069
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001070 subs->rate_list.list = rate_list =
1071 kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +02001072 if (!subs->rate_list.list)
1073 return -ENOMEM;
1074 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +01001075 subs->rate_list.mask = 0;
1076 count = 0;
1077 list_for_each_entry(fp, &subs->fmt_list, list) {
1078 int i;
1079 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001080 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +01001081 }
1082 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1083 &subs->rate_list);
1084 if (err < 0)
1085 return err;
1086
1087 return 0;
1088}
1089
1090
1091/*
1092 * set up the runtime hardware information.
1093 */
1094
1095static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1096{
Eldad Zack88766f02013-04-03 23:18:49 +02001097 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001098 unsigned int pt, ptmin;
1099 int param_period_time_if_needed;
1100 int err;
1101
1102 runtime->hw.formats = subs->formats;
1103
1104 runtime->hw.rate_min = 0x7fffffff;
1105 runtime->hw.rate_max = 0;
1106 runtime->hw.channels_min = 256;
1107 runtime->hw.channels_max = 0;
1108 runtime->hw.rates = 0;
1109 ptmin = UINT_MAX;
1110 /* check min/max rates and channels */
Eldad Zack88766f02013-04-03 23:18:49 +02001111 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001112 runtime->hw.rates |= fp->rates;
1113 if (runtime->hw.rate_min > fp->rate_min)
1114 runtime->hw.rate_min = fp->rate_min;
1115 if (runtime->hw.rate_max < fp->rate_max)
1116 runtime->hw.rate_max = fp->rate_max;
1117 if (runtime->hw.channels_min > fp->channels)
1118 runtime->hw.channels_min = fp->channels;
1119 if (runtime->hw.channels_max < fp->channels)
1120 runtime->hw.channels_max = fp->channels;
1121 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1122 /* FIXME: there might be more than one audio formats... */
1123 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1124 fp->frame_size;
1125 }
1126 pt = 125 * (1 << fp->datainterval);
1127 ptmin = min(ptmin, pt);
1128 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001129 err = snd_usb_autoresume(subs->stream->chip);
1130 if (err < 0)
1131 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001132
1133 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001134 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001135 /* full speed devices have fixed data packet interval */
1136 ptmin = 1000;
1137 if (ptmin == 1000)
1138 /* if period time doesn't go below 1 ms, no rules needed */
1139 param_period_time_if_needed = -1;
1140 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1141 ptmin, UINT_MAX);
1142
1143 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1144 hw_rule_rate, subs,
1145 SNDRV_PCM_HW_PARAM_FORMAT,
1146 SNDRV_PCM_HW_PARAM_CHANNELS,
1147 param_period_time_if_needed,
1148 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001149 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001150 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1151 hw_rule_channels, subs,
1152 SNDRV_PCM_HW_PARAM_FORMAT,
1153 SNDRV_PCM_HW_PARAM_RATE,
1154 param_period_time_if_needed,
1155 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001156 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001157 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1158 hw_rule_format, subs,
1159 SNDRV_PCM_HW_PARAM_RATE,
1160 SNDRV_PCM_HW_PARAM_CHANNELS,
1161 param_period_time_if_needed,
1162 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001163 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001164 if (param_period_time_if_needed >= 0) {
1165 err = snd_pcm_hw_rule_add(runtime, 0,
1166 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1167 hw_rule_period_time, subs,
1168 SNDRV_PCM_HW_PARAM_FORMAT,
1169 SNDRV_PCM_HW_PARAM_CHANNELS,
1170 SNDRV_PCM_HW_PARAM_RATE,
1171 -1);
1172 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001173 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001174 }
1175 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001176 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001177 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +01001178
1179rep_err:
1180 snd_usb_autosuspend(subs->stream->chip);
1181 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001182}
1183
1184static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1185{
1186 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1187 struct snd_pcm_runtime *runtime = substream->runtime;
1188 struct snd_usb_substream *subs = &as->substream[direction];
1189
1190 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001191 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001192 runtime->hw = snd_usb_hardware;
1193 runtime->private_data = subs;
1194 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001195 /* runtime PM is also done there */
Daniel Mackd24f5062013-04-17 00:01:38 +08001196
1197 /* initialize DSD/DOP context */
1198 subs->dsd_dop.byte_idx = 0;
1199 subs->dsd_dop.channel = 0;
1200 subs->dsd_dop.marker = 1;
1201
Daniel Macke5779992010-03-04 19:46:13 +01001202 return setup_hw_info(runtime, subs);
1203}
1204
1205static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1206{
1207 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1208 struct snd_usb_substream *subs = &as->substream[direction];
1209
Takashi Iwaib0db6062012-11-21 08:35:42 +01001210 stop_endpoints(subs, true);
Daniel Mack68e67f42012-07-12 13:08:40 +02001211
1212 if (!as->chip->shutdown && subs->interface >= 0) {
1213 usb_set_interface(subs->dev, subs->interface, 0);
1214 subs->interface = -1;
1215 }
1216
Daniel Macke5779992010-03-04 19:46:13 +01001217 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001218 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001219
Daniel Mack68e67f42012-07-12 13:08:40 +02001220 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001221}
1222
1223/* Since a URB can handle only a single linear buffer, we must use double
1224 * buffering when the data to be transferred overflows the buffer boundary.
1225 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1226 * for all URBs.
1227 */
1228static void retire_capture_urb(struct snd_usb_substream *subs,
1229 struct urb *urb)
1230{
1231 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1232 unsigned int stride, frames, bytes, oldptr;
1233 int i, period_elapsed = 0;
1234 unsigned long flags;
1235 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001236 int current_frame_number;
1237
1238 /* read frame number here, update pointer in critical section */
1239 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001240
1241 stride = runtime->frame_bits >> 3;
1242
1243 for (i = 0; i < urb->number_of_packets; i++) {
Calvin Owens1539d4f2013-04-12 22:33:59 -05001244 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
Daniel Mackedcd3632012-04-12 13:51:12 +02001245 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1246 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1247 // continue;
1248 }
1249 bytes = urb->iso_frame_desc[i].actual_length;
1250 frames = bytes / stride;
1251 if (!subs->txfr_quirk)
1252 bytes = frames * stride;
1253 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001254 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001255 bytes = frames * stride;
1256 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1257 oldbytes, bytes);
1258 }
1259 /* update the current pointer */
1260 spin_lock_irqsave(&subs->lock, flags);
1261 oldptr = subs->hwptr_done;
1262 subs->hwptr_done += bytes;
1263 if (subs->hwptr_done >= runtime->buffer_size * stride)
1264 subs->hwptr_done -= runtime->buffer_size * stride;
1265 frames = (bytes + (oldptr % stride)) / stride;
1266 subs->transfer_done += frames;
1267 if (subs->transfer_done >= runtime->period_size) {
1268 subs->transfer_done -= runtime->period_size;
1269 period_elapsed = 1;
1270 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001271 /* capture delay is by construction limited to one URB,
1272 * reset delays here
1273 */
1274 runtime->delay = subs->last_delay = 0;
1275
1276 /* realign last_frame_number */
1277 subs->last_frame_number = current_frame_number;
1278 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1279
Daniel Mackedcd3632012-04-12 13:51:12 +02001280 spin_unlock_irqrestore(&subs->lock, flags);
1281 /* copy a data chunk */
1282 if (oldptr + bytes > runtime->buffer_size * stride) {
1283 unsigned int bytes1 =
1284 runtime->buffer_size * stride - oldptr;
1285 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1286 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1287 } else {
1288 memcpy(runtime->dma_area + oldptr, cp, bytes);
1289 }
1290 }
1291
1292 if (period_elapsed)
1293 snd_pcm_period_elapsed(subs->pcm_substream);
1294}
1295
Daniel Mackd24f5062013-04-17 00:01:38 +08001296static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1297 struct urb *urb, unsigned int bytes)
1298{
1299 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1300 unsigned int stride = runtime->frame_bits >> 3;
1301 unsigned int dst_idx = 0;
1302 unsigned int src_idx = subs->hwptr_done;
1303 unsigned int wrap = runtime->buffer_size * stride;
1304 u8 *dst = urb->transfer_buffer;
1305 u8 *src = runtime->dma_area;
1306 u8 marker[] = { 0x05, 0xfa };
1307
1308 /*
1309 * The DSP DOP format defines a way to transport DSD samples over
1310 * normal PCM data endpoints. It requires stuffing of marker bytes
1311 * (0x05 and 0xfa, alternating per sample frame), and then expects
1312 * 2 additional bytes of actual payload. The whole frame is stored
1313 * LSB.
1314 *
1315 * Hence, for a stereo transport, the buffer layout looks like this,
1316 * where L refers to left channel samples and R to right.
1317 *
1318 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1319 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1320 * .....
1321 *
1322 */
1323
1324 while (bytes--) {
1325 if (++subs->dsd_dop.byte_idx == 3) {
1326 /* frame boundary? */
1327 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1328 src_idx += 2;
1329 subs->dsd_dop.byte_idx = 0;
1330
1331 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1332 /* alternate the marker */
1333 subs->dsd_dop.marker++;
1334 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1335 subs->dsd_dop.channel = 0;
1336 }
1337 } else {
1338 /* stuff the DSD payload */
1339 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001340
1341 if (subs->cur_audiofmt->dsd_bitrev)
1342 dst[dst_idx++] = bitrev8(src[idx]);
1343 else
1344 dst[dst_idx++] = src[idx];
1345
Daniel Mackd24f5062013-04-17 00:01:38 +08001346 subs->hwptr_done++;
1347 }
1348 }
1349}
1350
Daniel Mackedcd3632012-04-12 13:51:12 +02001351static void prepare_playback_urb(struct snd_usb_substream *subs,
1352 struct urb *urb)
1353{
1354 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001355 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001356 struct snd_urb_ctx *ctx = urb->context;
1357 unsigned int counts, frames, bytes;
1358 int i, stride, period_elapsed = 0;
1359 unsigned long flags;
1360
1361 stride = runtime->frame_bits >> 3;
1362
1363 frames = 0;
1364 urb->number_of_packets = 0;
1365 spin_lock_irqsave(&subs->lock, flags);
1366 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001367 if (ctx->packet_size[i])
1368 counts = ctx->packet_size[i];
1369 else
1370 counts = snd_usb_endpoint_next_packet_size(ep);
1371
Daniel Mackedcd3632012-04-12 13:51:12 +02001372 /* set up descriptor */
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001373 urb->iso_frame_desc[i].offset = frames * ep->stride;
1374 urb->iso_frame_desc[i].length = counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001375 frames += counts;
1376 urb->number_of_packets++;
1377 subs->transfer_done += counts;
1378 if (subs->transfer_done >= runtime->period_size) {
1379 subs->transfer_done -= runtime->period_size;
1380 period_elapsed = 1;
1381 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1382 if (subs->transfer_done > 0) {
1383 /* FIXME: fill-max mode is not
1384 * supported yet */
1385 frames -= subs->transfer_done;
1386 counts -= subs->transfer_done;
1387 urb->iso_frame_desc[i].length =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001388 counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001389 subs->transfer_done = 0;
1390 }
1391 i++;
1392 if (i < ctx->packets) {
1393 /* add a transfer delimiter */
1394 urb->iso_frame_desc[i].offset =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001395 frames * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001396 urb->iso_frame_desc[i].length = 0;
1397 urb->number_of_packets++;
1398 }
1399 break;
1400 }
1401 }
1402 if (period_elapsed &&
Eldad Zack98ae4722013-04-03 23:18:52 +02001403 !snd_usb_endpoint_implicit_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
Daniel Mackedcd3632012-04-12 13:51:12 +02001404 break;
1405 }
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001406 bytes = frames * ep->stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001407
1408 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1409 subs->cur_audiofmt->dsd_dop)) {
1410 fill_playback_urb_dsd_dop(subs, urb, bytes);
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001411 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1412 subs->cur_audiofmt->dsd_bitrev)) {
1413 /* bit-reverse the bytes */
1414 u8 *buf = urb->transfer_buffer;
1415 for (i = 0; i < bytes; i++) {
1416 int idx = (subs->hwptr_done + i)
1417 % (runtime->buffer_size * stride);
1418 buf[i] = bitrev8(runtime->dma_area[idx]);
1419 }
1420
1421 subs->hwptr_done += bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001422 } else {
Daniel Mackd24f5062013-04-17 00:01:38 +08001423 /* usual PCM */
1424 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1425 /* err, the transferred area goes over buffer boundary. */
1426 unsigned int bytes1 =
1427 runtime->buffer_size * stride - subs->hwptr_done;
1428 memcpy(urb->transfer_buffer,
1429 runtime->dma_area + subs->hwptr_done, bytes1);
1430 memcpy(urb->transfer_buffer + bytes1,
1431 runtime->dma_area, bytes - bytes1);
1432 } else {
1433 memcpy(urb->transfer_buffer,
1434 runtime->dma_area + subs->hwptr_done, bytes);
1435 }
1436
1437 subs->hwptr_done += bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001438 }
Daniel Mackd24f5062013-04-17 00:01:38 +08001439
Daniel Mackedcd3632012-04-12 13:51:12 +02001440 if (subs->hwptr_done >= runtime->buffer_size * stride)
1441 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001442
1443 /* update delay with exact number of samples queued */
1444 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001445 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001446 subs->last_delay = runtime->delay;
1447
1448 /* realign last_frame_number */
1449 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1450 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1451
Daniel Mackedcd3632012-04-12 13:51:12 +02001452 spin_unlock_irqrestore(&subs->lock, flags);
1453 urb->transfer_buffer_length = bytes;
1454 if (period_elapsed)
1455 snd_pcm_period_elapsed(subs->pcm_substream);
1456}
1457
1458/*
1459 * process after playback data complete
1460 * - decrease the delay count again
1461 */
1462static void retire_playback_urb(struct snd_usb_substream *subs,
1463 struct urb *urb)
1464{
1465 unsigned long flags;
1466 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001467 struct snd_usb_endpoint *ep = subs->data_endpoint;
1468 int processed = urb->transfer_buffer_length / ep->stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001469 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001470
Takashi Iwai1213a202012-09-06 14:58:00 +02001471 /* ignore the delay accounting when procssed=0 is given, i.e.
1472 * silent payloads are procssed before handling the actual data
1473 */
1474 if (!processed)
1475 return;
1476
Daniel Mackedcd3632012-04-12 13:51:12 +02001477 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001478 if (!subs->last_delay)
1479 goto out; /* short path */
1480
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001481 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1482 /* update delay with exact number of samples played */
1483 if (processed > subs->last_delay)
1484 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001485 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001486 subs->last_delay -= processed;
1487 runtime->delay = subs->last_delay;
1488
1489 /*
1490 * Report when delay estimate is off by more than 2ms.
1491 * The error should be lower than 2ms since the estimate relies
1492 * on two reads of a counter updated every ms.
1493 */
1494 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1495 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1496 est_delay, subs->last_delay);
1497
Takashi Iwai48779a02012-11-23 16:00:37 +01001498 if (!subs->running) {
1499 /* update last_frame_number for delay counting here since
1500 * prepare_playback_urb won't be called during pause
1501 */
1502 subs->last_frame_number =
1503 usb_get_current_frame_number(subs->dev) & 0xff;
1504 }
1505
1506 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001507 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001508}
1509
1510static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1511{
1512 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1513}
1514
1515static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1516{
1517 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1518}
1519
1520static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1521{
1522 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1523}
1524
1525static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1526{
1527 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1528}
1529
Daniel Mackedcd3632012-04-12 13:51:12 +02001530static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1531 int cmd)
1532{
1533 struct snd_usb_substream *subs = substream->runtime->private_data;
1534
1535 switch (cmd) {
1536 case SNDRV_PCM_TRIGGER_START:
1537 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1538 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1539 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001540 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001541 return 0;
1542 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001543 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001544 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001545 return 0;
1546 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1547 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001548 /* keep retire_data_urb for delay calculation */
1549 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001550 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001551 return 0;
1552 }
1553
1554 return -EINVAL;
1555}
1556
Daniel Mackafe25962012-06-16 16:58:04 +02001557static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1558 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001559{
1560 int err;
1561 struct snd_usb_substream *subs = substream->runtime->private_data;
1562
1563 switch (cmd) {
1564 case SNDRV_PCM_TRIGGER_START:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001565 err = start_endpoints(subs, false);
Daniel Mackedcd3632012-04-12 13:51:12 +02001566 if (err < 0)
1567 return err;
1568
1569 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001570 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001571 return 0;
1572 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001573 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001574 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001575 return 0;
1576 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1577 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001578 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001579 return 0;
1580 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1581 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001582 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001583 return 0;
1584 }
1585
1586 return -EINVAL;
1587}
1588
Daniel Macke5779992010-03-04 19:46:13 +01001589static struct snd_pcm_ops snd_usb_playback_ops = {
1590 .open = snd_usb_playback_open,
1591 .close = snd_usb_playback_close,
1592 .ioctl = snd_pcm_lib_ioctl,
1593 .hw_params = snd_usb_hw_params,
1594 .hw_free = snd_usb_hw_free,
1595 .prepare = snd_usb_pcm_prepare,
1596 .trigger = snd_usb_substream_playback_trigger,
1597 .pointer = snd_usb_pcm_pointer,
1598 .page = snd_pcm_lib_get_vmalloc_page,
1599 .mmap = snd_pcm_lib_mmap_vmalloc,
1600};
1601
1602static struct snd_pcm_ops snd_usb_capture_ops = {
1603 .open = snd_usb_capture_open,
1604 .close = snd_usb_capture_close,
1605 .ioctl = snd_pcm_lib_ioctl,
1606 .hw_params = snd_usb_hw_params,
1607 .hw_free = snd_usb_hw_free,
1608 .prepare = snd_usb_pcm_prepare,
1609 .trigger = snd_usb_substream_capture_trigger,
1610 .pointer = snd_usb_pcm_pointer,
1611 .page = snd_pcm_lib_get_vmalloc_page,
1612 .mmap = snd_pcm_lib_mmap_vmalloc,
1613};
1614
1615void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1616{
1617 snd_pcm_set_ops(pcm, stream,
1618 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1619 &snd_usb_playback_ops : &snd_usb_capture_ops);
1620}