blob: af30e08e3d3b7ec6cf587f2c4897f2febbb1c6c8 [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 Zack71bb64c2013-08-03 10:50:17 +0200337 int implicit_fb = 0;
Eldad Zacka60945f2013-08-03 10:50:18 +0200338 unsigned int ep;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200339
Eldad Zack914273c2013-08-03 10:50:21 +0200340 /* Implicit feedback sync EPs consumers are always playback EPs */
341 if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
342 return 0;
343
Daniel Mackc75a8a72012-04-12 13:51:14 +0200344 switch (subs->stream->chip->usb_id) {
Eldad Zackca10a7e2012-11-28 23:55:41 +0100345 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500346 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zack914273c2013-08-03 10:50:21 +0200347 implicit_fb = 1;
348 ep = 0x81;
349 iface = usb_ifnum_to_if(dev, 3);
Eldad Zackca10a7e2012-11-28 23:55:41 +0100350
Eldad Zack914273c2013-08-03 10:50:21 +0200351 if (!iface || iface->num_altsetting == 0)
352 return -EINVAL;
Eldad Zackca10a7e2012-11-28 23:55:41 +0100353
Eldad Zack914273c2013-08-03 10:50:21 +0200354 alts = &iface->altsetting[1];
355 goto add_sync_ep;
Eldad Zackca10a7e2012-11-28 23:55:41 +0100356 break;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200357 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
358 case USB_ID(0x0763, 0x2081):
Eldad Zack914273c2013-08-03 10:50:21 +0200359 implicit_fb = 1;
360 ep = 0x81;
361 iface = usb_ifnum_to_if(dev, 2);
Daniel Mackc75a8a72012-04-12 13:51:14 +0200362
Eldad Zack914273c2013-08-03 10:50:21 +0200363 if (!iface || iface->num_altsetting == 0)
364 return -EINVAL;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200365
Eldad Zack914273c2013-08-03 10:50:21 +0200366 alts = &iface->altsetting[1];
367 goto add_sync_ep;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200368 }
Eldad Zack914273c2013-08-03 10:50:21 +0200369 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100370 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
371 altsd->bInterfaceProtocol == 2 &&
372 altsd->bNumEndpoints == 1 &&
373 USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
374 search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
375 altsd->bAlternateSetting,
376 &alts, &ep) >= 0) {
377 implicit_fb = 1;
378 goto add_sync_ep;
379 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200380
Eldad Zacka60945f2013-08-03 10:50:18 +0200381 /* No quirk */
382 return 0;
383
384add_sync_ep:
385 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
386 alts, ep, !subs->direction,
387 implicit_fb ?
388 SND_USB_ENDPOINT_TYPE_DATA :
389 SND_USB_ENDPOINT_TYPE_SYNC);
390 if (!subs->sync_endpoint)
391 return -EINVAL;
392
393 subs->data_endpoint->sync_master = subs->sync_endpoint;
394
395 return 0;
396}
397
398static int set_sync_endpoint(struct snd_usb_substream *subs,
399 struct audioformat *fmt,
400 struct usb_device *dev,
401 struct usb_host_interface *alts,
402 struct usb_interface_descriptor *altsd)
403{
404 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
405 unsigned int ep, attr;
Eldad Zack95fec882013-08-03 10:50:20 +0200406 bool implicit_fb;
Eldad Zacka60945f2013-08-03 10:50:18 +0200407 int err;
408
409 /* we need a sync pipe in async OUT or adaptive IN mode */
410 /* check the number of EP, since some devices have broken
411 * descriptors which fool us. if it has only one EP,
412 * assume it as adaptive-out or sync-in.
413 */
414 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
415
416 err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
417 if (err < 0)
418 return err;
419
Eldad Zackf34d0652013-08-03 10:50:19 +0200420 if (altsd->bNumEndpoints < 2)
421 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200422
Eldad Zackf34d0652013-08-03 10:50:19 +0200423 if ((is_playback && attr != USB_ENDPOINT_SYNC_ASYNC) ||
424 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
425 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200426
Eldad Zackf34d0652013-08-03 10:50:19 +0200427 /* check sync-pipe endpoint */
428 /* ... and check descriptor size before accessing bSynchAddress
429 because there is a version of the SB Audigy 2 NX firmware lacking
430 the audio fields in the endpoint descriptors */
431 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
432 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zack95fec882013-08-03 10:50:20 +0200433 get_endpoint(alts, 1)->bSynchAddress != 0)) {
Eldad Zackf34d0652013-08-03 10:50:19 +0200434 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
435 dev->devnum, fmt->iface, fmt->altsetting,
436 get_endpoint(alts, 1)->bmAttributes,
437 get_endpoint(alts, 1)->bLength,
438 get_endpoint(alts, 1)->bSynchAddress);
439 return -EINVAL;
Daniel Mackedcd3632012-04-12 13:51:12 +0200440 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200441 ep = get_endpoint(alts, 1)->bEndpointAddress;
Eldad Zack95fec882013-08-03 10:50:20 +0200442 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zackf34d0652013-08-03 10:50:19 +0200443 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
444 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
445 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
446 dev->devnum, fmt->iface, fmt->altsetting,
447 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
448 return -EINVAL;
449 }
450
451 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
452 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
453
454 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
455 alts, ep, !subs->direction,
456 implicit_fb ?
457 SND_USB_ENDPOINT_TYPE_DATA :
458 SND_USB_ENDPOINT_TYPE_SYNC);
459 if (!subs->sync_endpoint)
460 return -EINVAL;
461
462 subs->data_endpoint->sync_master = subs->sync_endpoint;
Daniel Macke5779992010-03-04 19:46:13 +0100463
Eldad Zack71bb64c2013-08-03 10:50:17 +0200464 return 0;
465}
466
467/*
468 * find a matching format and set up the interface
469 */
470static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
471{
472 struct usb_device *dev = subs->dev;
473 struct usb_host_interface *alts;
474 struct usb_interface_descriptor *altsd;
475 struct usb_interface *iface;
476 int err;
477
478 iface = usb_ifnum_to_if(dev, fmt->iface);
479 if (WARN_ON(!iface))
480 return -EINVAL;
481 alts = &iface->altsetting[fmt->altset_idx];
482 altsd = get_iface_desc(alts);
483 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
484 return -EINVAL;
485
486 if (fmt == subs->cur_audiofmt)
487 return 0;
488
489 /* close the old interface */
490 if (subs->interface >= 0 && subs->interface != fmt->iface) {
491 err = usb_set_interface(subs->dev, subs->interface, 0);
492 if (err < 0) {
493 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
494 dev->devnum, fmt->iface, fmt->altsetting, err);
495 return -EIO;
496 }
497 subs->interface = -1;
498 subs->altset_idx = 0;
499 }
500
501 /* set interface */
502 if (subs->interface != fmt->iface ||
503 subs->altset_idx != fmt->altset_idx) {
504 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
505 if (err < 0) {
506 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
507 dev->devnum, fmt->iface, fmt->altsetting, err);
508 return -EIO;
509 }
510 snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
511 fmt->iface, fmt->altsetting);
512 subs->interface = fmt->iface;
513 subs->altset_idx = fmt->altset_idx;
514
515 snd_usb_set_interface_quirk(dev);
516 }
517
518 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
519 alts, fmt->endpoint, subs->direction,
520 SND_USB_ENDPOINT_TYPE_DATA);
521
522 if (!subs->data_endpoint)
523 return -EINVAL;
524
525 err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
526 if (err < 0)
527 return err;
528
Eldad Zackd133f2c2013-08-03 10:50:16 +0200529 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
530 if (err < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100531 return err;
532
533 subs->cur_audiofmt = fmt;
534
535 snd_usb_set_format_quirk(subs, fmt);
536
Daniel Macke5779992010-03-04 19:46:13 +0100537 return 0;
538}
539
540/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100541 * Return the score of matching two audioformats.
542 * Veto the audioformat if:
543 * - It has no channels for some reason.
544 * - Requested PCM format is not supported.
545 * - Requested sample rate is not supported.
546 */
547static int match_endpoint_audioformats(struct audioformat *fp,
548 struct audioformat *match, int rate,
549 snd_pcm_format_t pcm_format)
550{
551 int i;
552 int score = 0;
553
554 if (fp->channels < 1) {
555 snd_printdd("%s: (fmt @%p) no channels\n", __func__, fp);
556 return 0;
557 }
558
Eldad Zack74c34ca2013-04-23 01:00:41 +0200559 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
Eldad Zack0d9741c2012-12-03 20:30:09 +0100560 snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__,
561 fp, pcm_format);
562 return 0;
563 }
564
565 for (i = 0; i < fp->nr_rates; i++) {
566 if (fp->rate_table[i] == rate) {
567 score++;
568 break;
569 }
570 }
571 if (!score) {
572 snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__,
573 fp, rate);
574 return 0;
575 }
576
577 if (fp->channels == match->channels)
578 score++;
579
580 snd_printdd("%s: (fmt @%p) score %d\n", __func__, fp, score);
581
582 return score;
583}
584
585/*
586 * Configure the sync ep using the rate and pcm format of the data ep.
587 */
588static int configure_sync_endpoint(struct snd_usb_substream *subs)
589{
590 int ret;
591 struct audioformat *fp;
592 struct audioformat *sync_fp = NULL;
593 int cur_score = 0;
594 int sync_period_bytes = subs->period_bytes;
595 struct snd_usb_substream *sync_subs =
596 &subs->stream->substream[subs->direction ^ 1];
597
Takashi Iwai31be5422013-01-10 14:06:38 +0100598 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
599 !subs->stream)
600 return snd_usb_endpoint_set_params(subs->sync_endpoint,
601 subs->pcm_format,
602 subs->channels,
603 subs->period_bytes,
604 subs->cur_rate,
605 subs->cur_audiofmt,
606 NULL);
607
Eldad Zack0d9741c2012-12-03 20:30:09 +0100608 /* Try to find the best matching audioformat. */
609 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
610 int score = match_endpoint_audioformats(fp, subs->cur_audiofmt,
611 subs->cur_rate, subs->pcm_format);
612
613 if (score > cur_score) {
614 sync_fp = fp;
615 cur_score = score;
616 }
617 }
618
619 if (unlikely(sync_fp == NULL)) {
620 snd_printk(KERN_ERR "%s: no valid audioformat for sync ep %x found\n",
621 __func__, sync_subs->ep_num);
622 return -EINVAL;
623 }
624
625 /*
626 * Recalculate the period bytes if channel number differ between
627 * data and sync ep audioformat.
628 */
629 if (sync_fp->channels != subs->channels) {
630 sync_period_bytes = (subs->period_bytes / subs->channels) *
631 sync_fp->channels;
632 snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n",
633 __func__, subs->period_bytes, sync_period_bytes);
634 }
635
636 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
637 subs->pcm_format,
638 sync_fp->channels,
639 sync_period_bytes,
640 subs->cur_rate,
641 sync_fp,
642 NULL);
643
644 return ret;
645}
646
647/*
Dylan Reid61a70952012-09-18 09:49:48 -0700648 * configure endpoint params
649 *
650 * called during initial setup and upon resume
651 */
652static int configure_endpoint(struct snd_usb_substream *subs)
653{
654 int ret;
655
Dylan Reid61a70952012-09-18 09:49:48 -0700656 /* format changed */
Takashi Iwaib0db6062012-11-21 08:35:42 +0100657 stop_endpoints(subs, true);
Dylan Reid61a70952012-09-18 09:49:48 -0700658 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
659 subs->pcm_format,
660 subs->channels,
661 subs->period_bytes,
662 subs->cur_rate,
663 subs->cur_audiofmt,
664 subs->sync_endpoint);
665 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200666 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700667
668 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100669 ret = configure_sync_endpoint(subs);
670
Dylan Reid61a70952012-09-18 09:49:48 -0700671 return ret;
672}
673
674/*
Daniel Macke5779992010-03-04 19:46:13 +0100675 * hw_params callback
676 *
677 * allocate a buffer and set the given audio format.
678 *
679 * so far we use a physically linear buffer although packetize transfer
680 * doesn't need a continuous area.
681 * if sg buffer is supported on the later version of alsa, we'll follow
682 * that.
683 */
684static int snd_usb_hw_params(struct snd_pcm_substream *substream,
685 struct snd_pcm_hw_params *hw_params)
686{
687 struct snd_usb_substream *subs = substream->runtime->private_data;
688 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700689 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100690
691 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
692 params_buffer_bytes(hw_params));
693 if (ret < 0)
694 return ret;
695
Dylan Reid61a70952012-09-18 09:49:48 -0700696 subs->pcm_format = params_format(hw_params);
697 subs->period_bytes = params_period_bytes(hw_params);
698 subs->channels = params_channels(hw_params);
699 subs->cur_rate = params_rate(hw_params);
700
701 fmt = find_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100702 if (!fmt) {
703 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700704 subs->pcm_format, subs->cur_rate, subs->channels);
Daniel Macke5779992010-03-04 19:46:13 +0100705 return -EINVAL;
706 }
707
Takashi Iwai34f3c892012-10-15 12:16:02 +0200708 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200709 if (subs->stream->chip->shutdown)
710 ret = -ENODEV;
711 else
712 ret = set_format(subs, fmt);
Takashi Iwai34f3c892012-10-15 12:16:02 +0200713 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200714 if (ret < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100715 return ret;
716
Dylan Reid61a70952012-09-18 09:49:48 -0700717 subs->interface = fmt->iface;
718 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200719 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100720
Dylan Reid61a70952012-09-18 09:49:48 -0700721 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100722}
723
724/*
725 * hw_free callback
726 *
727 * reset the audio format and release the buffer
728 */
729static int snd_usb_hw_free(struct snd_pcm_substream *substream)
730{
731 struct snd_usb_substream *subs = substream->runtime->private_data;
732
733 subs->cur_audiofmt = NULL;
734 subs->cur_rate = 0;
735 subs->period_bytes = 0;
Takashi Iwai34f3c892012-10-15 12:16:02 +0200736 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200737 if (!subs->stream->chip->shutdown) {
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100738 stop_endpoints(subs, true);
Takashi Iwai978520b2012-10-12 15:12:55 +0200739 deactivate_endpoints(subs);
740 }
Takashi Iwai34f3c892012-10-15 12:16:02 +0200741 up_read(&subs->stream->chip->shutdown_rwsem);
Daniel Macke5779992010-03-04 19:46:13 +0100742 return snd_pcm_lib_free_vmalloc_buffer(substream);
743}
744
745/*
746 * prepare callback
747 *
748 * only a few subtle things...
749 */
750static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
751{
752 struct snd_pcm_runtime *runtime = substream->runtime;
753 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700754 struct usb_host_interface *alts;
755 struct usb_interface *iface;
756 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100757
758 if (! subs->cur_audiofmt) {
759 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
760 return -ENXIO;
761 }
762
Takashi Iwai34f3c892012-10-15 12:16:02 +0200763 down_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200764 if (subs->stream->chip->shutdown) {
765 ret = -ENODEV;
766 goto unlock;
767 }
768 if (snd_BUG_ON(!subs->data_endpoint)) {
769 ret = -EIO;
770 goto unlock;
771 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200772
Takashi Iwaif58161b2012-11-08 08:52:45 +0100773 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
774 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
775
Dylan Reid61a70952012-09-18 09:49:48 -0700776 ret = set_format(subs, subs->cur_audiofmt);
777 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200778 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700779
780 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
781 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
782 ret = snd_usb_init_sample_rate(subs->stream->chip,
783 subs->cur_audiofmt->iface,
784 alts,
785 subs->cur_audiofmt,
786 subs->cur_rate);
787 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200788 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700789
Takashi Iwai384dc0852012-09-18 14:49:31 +0200790 if (subs->need_setup_ep) {
791 ret = configure_endpoint(subs);
792 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200793 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200794 subs->need_setup_ep = false;
795 }
Dylan Reid61a70952012-09-18 09:49:48 -0700796
Daniel Macke5779992010-03-04 19:46:13 +0100797 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +0200798 subs->data_endpoint->maxframesize =
799 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
800 subs->data_endpoint->curframesize =
801 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +0100802
803 /* reset the pointer */
804 subs->hwptr_done = 0;
805 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500806 subs->last_delay = 0;
807 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100808 runtime->delay = 0;
809
Daniel Mackedcd3632012-04-12 13:51:12 +0200810 /* for playback, submit the URBs now; otherwise, the first hwptr_done
811 * updates for all URBs would happen at the same time when starting */
812 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100813 ret = start_endpoints(subs, true);
Daniel Mackedcd3632012-04-12 13:51:12 +0200814
Takashi Iwai978520b2012-10-12 15:12:55 +0200815 unlock:
Takashi Iwai34f3c892012-10-15 12:16:02 +0200816 up_read(&subs->stream->chip->shutdown_rwsem);
Takashi Iwai978520b2012-10-12 15:12:55 +0200817 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100818}
819
820static struct snd_pcm_hardware snd_usb_hardware =
821{
822 .info = SNDRV_PCM_INFO_MMAP |
823 SNDRV_PCM_INFO_MMAP_VALID |
824 SNDRV_PCM_INFO_BATCH |
825 SNDRV_PCM_INFO_INTERLEAVED |
826 SNDRV_PCM_INFO_BLOCK_TRANSFER |
827 SNDRV_PCM_INFO_PAUSE,
828 .buffer_bytes_max = 1024 * 1024,
829 .period_bytes_min = 64,
830 .period_bytes_max = 512 * 1024,
831 .periods_min = 2,
832 .periods_max = 1024,
833};
834
835static int hw_check_valid_format(struct snd_usb_substream *subs,
836 struct snd_pcm_hw_params *params,
837 struct audioformat *fp)
838{
839 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
840 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
841 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
842 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100843 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100844 unsigned int ptime;
845
846 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100847 snd_mask_none(&check_fmts);
848 check_fmts.bits[0] = (u32)fp->formats;
849 check_fmts.bits[1] = (u32)(fp->formats >> 32);
850 snd_mask_intersect(&check_fmts, fmts);
851 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100852 hwc_debug(" > check: no supported format %d\n", fp->format);
853 return 0;
854 }
855 /* check the channels */
856 if (fp->channels < ct->min || fp->channels > ct->max) {
857 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
858 return 0;
859 }
860 /* check the rate is within the range */
861 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
862 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
863 return 0;
864 }
865 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
866 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
867 return 0;
868 }
869 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +0200870 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100871 ptime = 125 * (1 << fp->datainterval);
872 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
873 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
874 return 0;
875 }
876 }
877 return 1;
878}
879
880static int hw_rule_rate(struct snd_pcm_hw_params *params,
881 struct snd_pcm_hw_rule *rule)
882{
883 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +0200884 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100885 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
886 unsigned int rmin, rmax;
887 int changed;
888
889 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
890 changed = 0;
891 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +0200892 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +0100893 if (!hw_check_valid_format(subs, params, fp))
894 continue;
895 if (changed++) {
896 if (rmin > fp->rate_min)
897 rmin = fp->rate_min;
898 if (rmax < fp->rate_max)
899 rmax = fp->rate_max;
900 } else {
901 rmin = fp->rate_min;
902 rmax = fp->rate_max;
903 }
904 }
905
906 if (!changed) {
907 hwc_debug(" --> get empty\n");
908 it->empty = 1;
909 return -EINVAL;
910 }
911
912 changed = 0;
913 if (it->min < rmin) {
914 it->min = rmin;
915 it->openmin = 0;
916 changed = 1;
917 }
918 if (it->max > rmax) {
919 it->max = rmax;
920 it->openmax = 0;
921 changed = 1;
922 }
923 if (snd_interval_checkempty(it)) {
924 it->empty = 1;
925 return -EINVAL;
926 }
927 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
928 return changed;
929}
930
931
932static int hw_rule_channels(struct snd_pcm_hw_params *params,
933 struct snd_pcm_hw_rule *rule)
934{
935 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +0200936 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100937 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
938 unsigned int rmin, rmax;
939 int changed;
940
941 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
942 changed = 0;
943 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +0200944 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +0100945 if (!hw_check_valid_format(subs, params, fp))
946 continue;
947 if (changed++) {
948 if (rmin > fp->channels)
949 rmin = fp->channels;
950 if (rmax < fp->channels)
951 rmax = fp->channels;
952 } else {
953 rmin = fp->channels;
954 rmax = fp->channels;
955 }
956 }
957
958 if (!changed) {
959 hwc_debug(" --> get empty\n");
960 it->empty = 1;
961 return -EINVAL;
962 }
963
964 changed = 0;
965 if (it->min < rmin) {
966 it->min = rmin;
967 it->openmin = 0;
968 changed = 1;
969 }
970 if (it->max > rmax) {
971 it->max = rmax;
972 it->openmax = 0;
973 changed = 1;
974 }
975 if (snd_interval_checkempty(it)) {
976 it->empty = 1;
977 return -EINVAL;
978 }
979 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
980 return changed;
981}
982
983static int hw_rule_format(struct snd_pcm_hw_params *params,
984 struct snd_pcm_hw_rule *rule)
985{
986 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +0200987 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100988 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
989 u64 fbits;
990 u32 oldbits[2];
991 int changed;
992
993 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
994 fbits = 0;
Eldad Zack88766f02013-04-03 23:18:49 +0200995 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +0100996 if (!hw_check_valid_format(subs, params, fp))
997 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100998 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +0100999 }
1000
1001 oldbits[0] = fmt->bits[0];
1002 oldbits[1] = fmt->bits[1];
1003 fmt->bits[0] &= (u32)fbits;
1004 fmt->bits[1] &= (u32)(fbits >> 32);
1005 if (!fmt->bits[0] && !fmt->bits[1]) {
1006 hwc_debug(" --> get empty\n");
1007 return -EINVAL;
1008 }
1009 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1010 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1011 return changed;
1012}
1013
1014static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1015 struct snd_pcm_hw_rule *rule)
1016{
1017 struct snd_usb_substream *subs = rule->private;
1018 struct audioformat *fp;
1019 struct snd_interval *it;
1020 unsigned char min_datainterval;
1021 unsigned int pmin;
1022 int changed;
1023
1024 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1025 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1026 min_datainterval = 0xff;
1027 list_for_each_entry(fp, &subs->fmt_list, list) {
1028 if (!hw_check_valid_format(subs, params, fp))
1029 continue;
1030 min_datainterval = min(min_datainterval, fp->datainterval);
1031 }
1032 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +02001033 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +01001034 it->empty = 1;
1035 return -EINVAL;
1036 }
1037 pmin = 125 * (1 << min_datainterval);
1038 changed = 0;
1039 if (it->min < pmin) {
1040 it->min = pmin;
1041 it->openmin = 0;
1042 changed = 1;
1043 }
1044 if (snd_interval_checkempty(it)) {
1045 it->empty = 1;
1046 return -EINVAL;
1047 }
1048 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1049 return changed;
1050}
1051
1052/*
1053 * If the device supports unusual bit rates, does the request meet these?
1054 */
1055static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1056 struct snd_usb_substream *subs)
1057{
1058 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001059 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +01001060 int count = 0, needs_knot = 0;
1061 int err;
1062
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +02001063 kfree(subs->rate_list.list);
1064 subs->rate_list.list = NULL;
1065
Daniel Macke5779992010-03-04 19:46:13 +01001066 list_for_each_entry(fp, &subs->fmt_list, list) {
1067 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1068 return 0;
1069 count += fp->nr_rates;
1070 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1071 needs_knot = 1;
1072 }
1073 if (!needs_knot)
1074 return 0;
1075
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001076 subs->rate_list.list = rate_list =
1077 kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +02001078 if (!subs->rate_list.list)
1079 return -ENOMEM;
1080 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +01001081 subs->rate_list.mask = 0;
1082 count = 0;
1083 list_for_each_entry(fp, &subs->fmt_list, list) {
1084 int i;
1085 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001086 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +01001087 }
1088 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1089 &subs->rate_list);
1090 if (err < 0)
1091 return err;
1092
1093 return 0;
1094}
1095
1096
1097/*
1098 * set up the runtime hardware information.
1099 */
1100
1101static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1102{
Eldad Zack88766f02013-04-03 23:18:49 +02001103 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001104 unsigned int pt, ptmin;
1105 int param_period_time_if_needed;
1106 int err;
1107
1108 runtime->hw.formats = subs->formats;
1109
1110 runtime->hw.rate_min = 0x7fffffff;
1111 runtime->hw.rate_max = 0;
1112 runtime->hw.channels_min = 256;
1113 runtime->hw.channels_max = 0;
1114 runtime->hw.rates = 0;
1115 ptmin = UINT_MAX;
1116 /* check min/max rates and channels */
Eldad Zack88766f02013-04-03 23:18:49 +02001117 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001118 runtime->hw.rates |= fp->rates;
1119 if (runtime->hw.rate_min > fp->rate_min)
1120 runtime->hw.rate_min = fp->rate_min;
1121 if (runtime->hw.rate_max < fp->rate_max)
1122 runtime->hw.rate_max = fp->rate_max;
1123 if (runtime->hw.channels_min > fp->channels)
1124 runtime->hw.channels_min = fp->channels;
1125 if (runtime->hw.channels_max < fp->channels)
1126 runtime->hw.channels_max = fp->channels;
1127 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1128 /* FIXME: there might be more than one audio formats... */
1129 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1130 fp->frame_size;
1131 }
1132 pt = 125 * (1 << fp->datainterval);
1133 ptmin = min(ptmin, pt);
1134 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001135 err = snd_usb_autoresume(subs->stream->chip);
1136 if (err < 0)
1137 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001138
1139 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001140 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001141 /* full speed devices have fixed data packet interval */
1142 ptmin = 1000;
1143 if (ptmin == 1000)
1144 /* if period time doesn't go below 1 ms, no rules needed */
1145 param_period_time_if_needed = -1;
1146 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1147 ptmin, UINT_MAX);
1148
1149 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1150 hw_rule_rate, subs,
1151 SNDRV_PCM_HW_PARAM_FORMAT,
1152 SNDRV_PCM_HW_PARAM_CHANNELS,
1153 param_period_time_if_needed,
1154 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001155 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001156 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1157 hw_rule_channels, subs,
1158 SNDRV_PCM_HW_PARAM_FORMAT,
1159 SNDRV_PCM_HW_PARAM_RATE,
1160 param_period_time_if_needed,
1161 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001162 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001163 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1164 hw_rule_format, subs,
1165 SNDRV_PCM_HW_PARAM_RATE,
1166 SNDRV_PCM_HW_PARAM_CHANNELS,
1167 param_period_time_if_needed,
1168 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001169 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001170 if (param_period_time_if_needed >= 0) {
1171 err = snd_pcm_hw_rule_add(runtime, 0,
1172 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1173 hw_rule_period_time, subs,
1174 SNDRV_PCM_HW_PARAM_FORMAT,
1175 SNDRV_PCM_HW_PARAM_CHANNELS,
1176 SNDRV_PCM_HW_PARAM_RATE,
1177 -1);
1178 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001179 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001180 }
1181 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001182 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001183 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +01001184
1185rep_err:
1186 snd_usb_autosuspend(subs->stream->chip);
1187 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001188}
1189
1190static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1191{
1192 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1193 struct snd_pcm_runtime *runtime = substream->runtime;
1194 struct snd_usb_substream *subs = &as->substream[direction];
1195
1196 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001197 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001198 runtime->hw = snd_usb_hardware;
1199 runtime->private_data = subs;
1200 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001201 /* runtime PM is also done there */
Daniel Mackd24f5062013-04-17 00:01:38 +08001202
1203 /* initialize DSD/DOP context */
1204 subs->dsd_dop.byte_idx = 0;
1205 subs->dsd_dop.channel = 0;
1206 subs->dsd_dop.marker = 1;
1207
Daniel Macke5779992010-03-04 19:46:13 +01001208 return setup_hw_info(runtime, subs);
1209}
1210
1211static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1212{
1213 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1214 struct snd_usb_substream *subs = &as->substream[direction];
1215
Takashi Iwaib0db6062012-11-21 08:35:42 +01001216 stop_endpoints(subs, true);
Daniel Mack68e67f42012-07-12 13:08:40 +02001217
1218 if (!as->chip->shutdown && subs->interface >= 0) {
1219 usb_set_interface(subs->dev, subs->interface, 0);
1220 subs->interface = -1;
1221 }
1222
Daniel Macke5779992010-03-04 19:46:13 +01001223 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001224 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001225
Daniel Mack68e67f42012-07-12 13:08:40 +02001226 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001227}
1228
1229/* Since a URB can handle only a single linear buffer, we must use double
1230 * buffering when the data to be transferred overflows the buffer boundary.
1231 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1232 * for all URBs.
1233 */
1234static void retire_capture_urb(struct snd_usb_substream *subs,
1235 struct urb *urb)
1236{
1237 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1238 unsigned int stride, frames, bytes, oldptr;
1239 int i, period_elapsed = 0;
1240 unsigned long flags;
1241 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001242 int current_frame_number;
1243
1244 /* read frame number here, update pointer in critical section */
1245 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001246
1247 stride = runtime->frame_bits >> 3;
1248
1249 for (i = 0; i < urb->number_of_packets; i++) {
Calvin Owens1539d4f2013-04-12 22:33:59 -05001250 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
Daniel Mackedcd3632012-04-12 13:51:12 +02001251 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1252 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1253 // continue;
1254 }
1255 bytes = urb->iso_frame_desc[i].actual_length;
1256 frames = bytes / stride;
1257 if (!subs->txfr_quirk)
1258 bytes = frames * stride;
1259 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001260 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001261 bytes = frames * stride;
1262 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1263 oldbytes, bytes);
1264 }
1265 /* update the current pointer */
1266 spin_lock_irqsave(&subs->lock, flags);
1267 oldptr = subs->hwptr_done;
1268 subs->hwptr_done += bytes;
1269 if (subs->hwptr_done >= runtime->buffer_size * stride)
1270 subs->hwptr_done -= runtime->buffer_size * stride;
1271 frames = (bytes + (oldptr % stride)) / stride;
1272 subs->transfer_done += frames;
1273 if (subs->transfer_done >= runtime->period_size) {
1274 subs->transfer_done -= runtime->period_size;
1275 period_elapsed = 1;
1276 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001277 /* capture delay is by construction limited to one URB,
1278 * reset delays here
1279 */
1280 runtime->delay = subs->last_delay = 0;
1281
1282 /* realign last_frame_number */
1283 subs->last_frame_number = current_frame_number;
1284 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1285
Daniel Mackedcd3632012-04-12 13:51:12 +02001286 spin_unlock_irqrestore(&subs->lock, flags);
1287 /* copy a data chunk */
1288 if (oldptr + bytes > runtime->buffer_size * stride) {
1289 unsigned int bytes1 =
1290 runtime->buffer_size * stride - oldptr;
1291 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1292 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1293 } else {
1294 memcpy(runtime->dma_area + oldptr, cp, bytes);
1295 }
1296 }
1297
1298 if (period_elapsed)
1299 snd_pcm_period_elapsed(subs->pcm_substream);
1300}
1301
Daniel Mackd24f5062013-04-17 00:01:38 +08001302static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1303 struct urb *urb, unsigned int bytes)
1304{
1305 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1306 unsigned int stride = runtime->frame_bits >> 3;
1307 unsigned int dst_idx = 0;
1308 unsigned int src_idx = subs->hwptr_done;
1309 unsigned int wrap = runtime->buffer_size * stride;
1310 u8 *dst = urb->transfer_buffer;
1311 u8 *src = runtime->dma_area;
1312 u8 marker[] = { 0x05, 0xfa };
1313
1314 /*
1315 * The DSP DOP format defines a way to transport DSD samples over
1316 * normal PCM data endpoints. It requires stuffing of marker bytes
1317 * (0x05 and 0xfa, alternating per sample frame), and then expects
1318 * 2 additional bytes of actual payload. The whole frame is stored
1319 * LSB.
1320 *
1321 * Hence, for a stereo transport, the buffer layout looks like this,
1322 * where L refers to left channel samples and R to right.
1323 *
1324 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1325 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1326 * .....
1327 *
1328 */
1329
1330 while (bytes--) {
1331 if (++subs->dsd_dop.byte_idx == 3) {
1332 /* frame boundary? */
1333 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1334 src_idx += 2;
1335 subs->dsd_dop.byte_idx = 0;
1336
1337 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1338 /* alternate the marker */
1339 subs->dsd_dop.marker++;
1340 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1341 subs->dsd_dop.channel = 0;
1342 }
1343 } else {
1344 /* stuff the DSD payload */
1345 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001346
1347 if (subs->cur_audiofmt->dsd_bitrev)
1348 dst[dst_idx++] = bitrev8(src[idx]);
1349 else
1350 dst[dst_idx++] = src[idx];
1351
Daniel Mackd24f5062013-04-17 00:01:38 +08001352 subs->hwptr_done++;
1353 }
1354 }
1355}
1356
Daniel Mackedcd3632012-04-12 13:51:12 +02001357static void prepare_playback_urb(struct snd_usb_substream *subs,
1358 struct urb *urb)
1359{
1360 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001361 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001362 struct snd_urb_ctx *ctx = urb->context;
1363 unsigned int counts, frames, bytes;
1364 int i, stride, period_elapsed = 0;
1365 unsigned long flags;
1366
1367 stride = runtime->frame_bits >> 3;
1368
1369 frames = 0;
1370 urb->number_of_packets = 0;
1371 spin_lock_irqsave(&subs->lock, flags);
1372 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001373 if (ctx->packet_size[i])
1374 counts = ctx->packet_size[i];
1375 else
1376 counts = snd_usb_endpoint_next_packet_size(ep);
1377
Daniel Mackedcd3632012-04-12 13:51:12 +02001378 /* set up descriptor */
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001379 urb->iso_frame_desc[i].offset = frames * ep->stride;
1380 urb->iso_frame_desc[i].length = counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001381 frames += counts;
1382 urb->number_of_packets++;
1383 subs->transfer_done += counts;
1384 if (subs->transfer_done >= runtime->period_size) {
1385 subs->transfer_done -= runtime->period_size;
1386 period_elapsed = 1;
1387 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1388 if (subs->transfer_done > 0) {
1389 /* FIXME: fill-max mode is not
1390 * supported yet */
1391 frames -= subs->transfer_done;
1392 counts -= subs->transfer_done;
1393 urb->iso_frame_desc[i].length =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001394 counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001395 subs->transfer_done = 0;
1396 }
1397 i++;
1398 if (i < ctx->packets) {
1399 /* add a transfer delimiter */
1400 urb->iso_frame_desc[i].offset =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001401 frames * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001402 urb->iso_frame_desc[i].length = 0;
1403 urb->number_of_packets++;
1404 }
1405 break;
1406 }
1407 }
1408 if (period_elapsed &&
Eldad Zack98ae4722013-04-03 23:18:52 +02001409 !snd_usb_endpoint_implicit_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
Daniel Mackedcd3632012-04-12 13:51:12 +02001410 break;
1411 }
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001412 bytes = frames * ep->stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001413
1414 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1415 subs->cur_audiofmt->dsd_dop)) {
1416 fill_playback_urb_dsd_dop(subs, urb, bytes);
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001417 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1418 subs->cur_audiofmt->dsd_bitrev)) {
1419 /* bit-reverse the bytes */
1420 u8 *buf = urb->transfer_buffer;
1421 for (i = 0; i < bytes; i++) {
1422 int idx = (subs->hwptr_done + i)
1423 % (runtime->buffer_size * stride);
1424 buf[i] = bitrev8(runtime->dma_area[idx]);
1425 }
1426
1427 subs->hwptr_done += bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001428 } else {
Daniel Mackd24f5062013-04-17 00:01:38 +08001429 /* usual PCM */
1430 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1431 /* err, the transferred area goes over buffer boundary. */
1432 unsigned int bytes1 =
1433 runtime->buffer_size * stride - subs->hwptr_done;
1434 memcpy(urb->transfer_buffer,
1435 runtime->dma_area + subs->hwptr_done, bytes1);
1436 memcpy(urb->transfer_buffer + bytes1,
1437 runtime->dma_area, bytes - bytes1);
1438 } else {
1439 memcpy(urb->transfer_buffer,
1440 runtime->dma_area + subs->hwptr_done, bytes);
1441 }
1442
1443 subs->hwptr_done += bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001444 }
Daniel Mackd24f5062013-04-17 00:01:38 +08001445
Daniel Mackedcd3632012-04-12 13:51:12 +02001446 if (subs->hwptr_done >= runtime->buffer_size * stride)
1447 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001448
1449 /* update delay with exact number of samples queued */
1450 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001451 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001452 subs->last_delay = runtime->delay;
1453
1454 /* realign last_frame_number */
1455 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1456 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1457
Daniel Mackedcd3632012-04-12 13:51:12 +02001458 spin_unlock_irqrestore(&subs->lock, flags);
1459 urb->transfer_buffer_length = bytes;
1460 if (period_elapsed)
1461 snd_pcm_period_elapsed(subs->pcm_substream);
1462}
1463
1464/*
1465 * process after playback data complete
1466 * - decrease the delay count again
1467 */
1468static void retire_playback_urb(struct snd_usb_substream *subs,
1469 struct urb *urb)
1470{
1471 unsigned long flags;
1472 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001473 struct snd_usb_endpoint *ep = subs->data_endpoint;
1474 int processed = urb->transfer_buffer_length / ep->stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001475 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001476
Takashi Iwai1213a202012-09-06 14:58:00 +02001477 /* ignore the delay accounting when procssed=0 is given, i.e.
1478 * silent payloads are procssed before handling the actual data
1479 */
1480 if (!processed)
1481 return;
1482
Daniel Mackedcd3632012-04-12 13:51:12 +02001483 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001484 if (!subs->last_delay)
1485 goto out; /* short path */
1486
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001487 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1488 /* update delay with exact number of samples played */
1489 if (processed > subs->last_delay)
1490 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001491 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001492 subs->last_delay -= processed;
1493 runtime->delay = subs->last_delay;
1494
1495 /*
1496 * Report when delay estimate is off by more than 2ms.
1497 * The error should be lower than 2ms since the estimate relies
1498 * on two reads of a counter updated every ms.
1499 */
1500 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1501 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1502 est_delay, subs->last_delay);
1503
Takashi Iwai48779a02012-11-23 16:00:37 +01001504 if (!subs->running) {
1505 /* update last_frame_number for delay counting here since
1506 * prepare_playback_urb won't be called during pause
1507 */
1508 subs->last_frame_number =
1509 usb_get_current_frame_number(subs->dev) & 0xff;
1510 }
1511
1512 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001513 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001514}
1515
1516static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1517{
1518 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1519}
1520
1521static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1522{
1523 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1524}
1525
1526static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1527{
1528 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1529}
1530
1531static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1532{
1533 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1534}
1535
Daniel Mackedcd3632012-04-12 13:51:12 +02001536static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1537 int cmd)
1538{
1539 struct snd_usb_substream *subs = substream->runtime->private_data;
1540
1541 switch (cmd) {
1542 case SNDRV_PCM_TRIGGER_START:
1543 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1544 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1545 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001546 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001547 return 0;
1548 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001549 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001550 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001551 return 0;
1552 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1553 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001554 /* keep retire_data_urb for delay calculation */
1555 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001556 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001557 return 0;
1558 }
1559
1560 return -EINVAL;
1561}
1562
Daniel Mackafe25962012-06-16 16:58:04 +02001563static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1564 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001565{
1566 int err;
1567 struct snd_usb_substream *subs = substream->runtime->private_data;
1568
1569 switch (cmd) {
1570 case SNDRV_PCM_TRIGGER_START:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001571 err = start_endpoints(subs, false);
Daniel Mackedcd3632012-04-12 13:51:12 +02001572 if (err < 0)
1573 return err;
1574
1575 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001576 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001577 return 0;
1578 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001579 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001580 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001581 return 0;
1582 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1583 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001584 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001585 return 0;
1586 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1587 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001588 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001589 return 0;
1590 }
1591
1592 return -EINVAL;
1593}
1594
Daniel Macke5779992010-03-04 19:46:13 +01001595static struct snd_pcm_ops snd_usb_playback_ops = {
1596 .open = snd_usb_playback_open,
1597 .close = snd_usb_playback_close,
1598 .ioctl = snd_pcm_lib_ioctl,
1599 .hw_params = snd_usb_hw_params,
1600 .hw_free = snd_usb_hw_free,
1601 .prepare = snd_usb_pcm_prepare,
1602 .trigger = snd_usb_substream_playback_trigger,
1603 .pointer = snd_usb_pcm_pointer,
1604 .page = snd_pcm_lib_get_vmalloc_page,
1605 .mmap = snd_pcm_lib_mmap_vmalloc,
1606};
1607
1608static struct snd_pcm_ops snd_usb_capture_ops = {
1609 .open = snd_usb_capture_open,
1610 .close = snd_usb_capture_close,
1611 .ioctl = snd_pcm_lib_ioctl,
1612 .hw_params = snd_usb_hw_params,
1613 .hw_free = snd_usb_hw_free,
1614 .prepare = snd_usb_pcm_prepare,
1615 .trigger = snd_usb_substream_capture_trigger,
1616 .pointer = snd_usb_pcm_pointer,
1617 .page = snd_pcm_lib_get_vmalloc_page,
1618 .mmap = snd_pcm_lib_mmap_vmalloc,
1619};
1620
1621void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1622{
1623 snd_pcm_set_ops(pcm, stream,
1624 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1625 &snd_usb_playback_ops : &snd_usb_capture_ops);
1626}