ALSA: usb-audio: separate sync endpoint setting from set_format
[deliverable/linux.git] / sound / usb / pcm.c
CommitLineData
e5779998
DM
1/*
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>
9966ddaf 18#include <linux/slab.h>
44dcbbb1 19#include <linux/bitrev.h>
edcd3633 20#include <linux/ratelimit.h>
e5779998
DM
21#include <linux/usb.h>
22#include <linux/usb/audio.h>
7e847894 23#include <linux/usb/audio-v2.h>
e5779998
DM
24
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"
c731bc96 33#include "endpoint.h"
e5779998
DM
34#include "helper.h"
35#include "pcm.h"
79f920fb 36#include "clock.h"
88a8516a 37#include "power.h"
e5779998 38
edcd3633
DM
39#define SUBSTREAM_FLAG_DATA_EP_STARTED 0
40#define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
41
294c4fb8
PLB
42/* 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
48779a0b
TI
50 if (!subs->last_delay)
51 return 0; /* short path */
52
294c4fb8
PLB
53 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 */
e4cc6153
PLB
63 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
294c4fb8
PLB
69 if (est_delay < 0)
70 est_delay = 0;
71 return est_delay;
72}
73
e5779998
DM
74/*
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;
978520b7
TI
83 if (subs->stream->chip->shutdown)
84 return SNDRV_PCM_POS_XRUN;
e5779998
DM
85 spin_lock(&subs->lock);
86 hwptr_done = subs->hwptr_done;
e4cc6153 87 substream->runtime->delay = snd_usb_pcm_delay(subs,
294c4fb8 88 substream->runtime->rate);
e5779998
DM
89 spin_unlock(&subs->lock);
90 return hwptr_done / (substream->runtime->frame_bits >> 3);
91}
92
93/*
94 * find a matching audio format
95 */
61a70950 96static struct audioformat *find_format(struct snd_usb_substream *subs)
e5779998 97{
88766f04 98 struct audioformat *fp;
e5779998
DM
99 struct audioformat *found = NULL;
100 int cur_attr = 0, attr;
101
88766f04 102 list_for_each_entry(fp, &subs->fmt_list, list) {
74c34ca1 103 if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
015eb0b0 104 continue;
61a70950 105 if (fp->channels != subs->channels)
e5779998 106 continue;
61a70950
DR
107 if (subs->cur_rate < fp->rate_min ||
108 subs->cur_rate > fp->rate_max)
e5779998
DM
109 continue;
110 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
111 unsigned int i;
112 for (i = 0; i < fp->nr_rates; i++)
61a70950 113 if (fp->rate_table[i] == subs->cur_rate)
e5779998
DM
114 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
767d75ad
DM
153static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
154 struct usb_host_interface *alts,
155 struct audioformat *fmt)
156{
157 struct usb_device *dev = chip->dev;
158 unsigned int ep;
159 unsigned char data[1];
160 int err;
161
162 ep = get_endpoint(alts, 0)->bEndpointAddress;
163
767d75ad
DM
164 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,
17d900c4 168 data, sizeof(data))) < 0) {
767d75ad
DM
169 snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
170 dev->devnum, iface, ep);
171 return err;
172 }
173
174 return 0;
175}
e5779998 176
92c25611
DM
177static 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];
92c25611
DM
183 int err;
184
92c25611
DM
185 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,
17d900c4 189 data, sizeof(data))) < 0) {
92c25611
DM
190 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
e5779998 198/*
92c25611 199 * initialize the pitch control and sample rate
e5779998 200 */
767d75ad 201int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
e5779998
DM
202 struct usb_host_interface *alts,
203 struct audioformat *fmt)
204{
92c25611
DM
205 /* if endpoint doesn't have pitch control, bail out */
206 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
207 return 0;
208
8f898e92 209 switch (fmt->protocol) {
767d75ad 210 case UAC_VERSION_1:
a2acad82 211 default:
767d75ad
DM
212 return init_pitch_v1(chip, iface, alts, fmt);
213
214 case UAC_VERSION_2:
92c25611 215 return init_pitch_v2(chip, iface, alts, fmt);
767d75ad 216 }
767d75ad
DM
217}
218
a9bb3626 219static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
edcd3633
DM
220{
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;
015618b9 232 err = snd_usb_endpoint_start(ep, can_sleep);
edcd3633
DM
233 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
2e4a263c
DM
243 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
edcd3633
DM
258 snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);
259
260 ep->sync_slave = subs->data_endpoint;
015618b9 261 err = snd_usb_endpoint_start(ep, can_sleep);
edcd3633
DM
262 if (err < 0) {
263 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
264 return err;
265 }
266 }
267
268 return 0;
269}
270
a9bb3626 271static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
edcd3633
DM
272{
273 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
b2eb950d 274 snd_usb_endpoint_stop(subs->sync_endpoint);
edcd3633
DM
275
276 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
b2eb950d
TI
277 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 }
edcd3633
DM
283}
284
edcd3633
DM
285static 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
ba7c2be1
CL
301static 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
71bb64c5
EZ
330
331static int set_sync_endpoint(struct snd_usb_substream *subs,
332 struct audioformat *fmt,
333 struct usb_device *dev,
334 struct usb_host_interface *alts,
335 struct usb_interface_descriptor *altsd)
e5779998 336{
e5779998 337 struct usb_interface *iface;
e5779998 338 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
71bb64c5
EZ
339 unsigned int ep, attr;
340 int implicit_fb = 0;
e5779998
DM
341
342 /* we need a sync pipe in async OUT or adaptive IN mode */
343 /* check the number of EP, since some devices have broken
344 * descriptors which fool us. if it has only one EP,
345 * assume it as adaptive-out or sync-in.
346 */
347 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
c75a8a7a
DM
348
349 switch (subs->stream->chip->usb_id) {
ca10a7eb 350 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
e9a25e04 351 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
ca10a7eb
EZ
352 if (is_playback) {
353 implicit_fb = 1;
354 ep = 0x81;
355 iface = usb_ifnum_to_if(dev, 3);
356
357 if (!iface || iface->num_altsetting == 0)
358 return -EINVAL;
359
360 alts = &iface->altsetting[1];
361 goto add_sync_ep;
362 }
363 break;
c75a8a7a
DM
364 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
365 case USB_ID(0x0763, 0x2081):
366 if (is_playback) {
367 implicit_fb = 1;
edcd3633
DM
368 ep = 0x81;
369 iface = usb_ifnum_to_if(dev, 2);
c75a8a7a
DM
370
371 if (!iface || iface->num_altsetting == 0)
372 return -EINVAL;
373
edcd3633
DM
374 alts = &iface->altsetting[1];
375 goto add_sync_ep;
376 }
c75a8a7a 377 }
ba7c2be1
CL
378 if (is_playback &&
379 attr == USB_ENDPOINT_SYNC_ASYNC &&
380 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
381 altsd->bInterfaceProtocol == 2 &&
382 altsd->bNumEndpoints == 1 &&
383 USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
384 search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
385 altsd->bAlternateSetting,
386 &alts, &ep) >= 0) {
387 implicit_fb = 1;
388 goto add_sync_ep;
389 }
edcd3633 390
c75a8a7a
DM
391 if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) ||
392 (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) &&
393 altsd->bNumEndpoints >= 2) {
e5779998
DM
394 /* check sync-pipe endpoint */
395 /* ... and check descriptor size before accessing bSynchAddress
396 because there is a version of the SB Audigy 2 NX firmware lacking
397 the audio fields in the endpoint descriptors */
fde854bd 398 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
e5779998 399 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
c75a8a7a
DM
400 get_endpoint(alts, 1)->bSynchAddress != 0 &&
401 !implicit_fb)) {
7fb75db1
DM
402 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
403 dev->devnum, fmt->iface, fmt->altsetting,
404 get_endpoint(alts, 1)->bmAttributes,
405 get_endpoint(alts, 1)->bLength,
406 get_endpoint(alts, 1)->bSynchAddress);
e5779998
DM
407 return -EINVAL;
408 }
409 ep = get_endpoint(alts, 1)->bEndpointAddress;
7fb75db1
DM
410 if (!implicit_fb &&
411 get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
e5779998 412 (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
7fb75db1
DM
413 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
414 snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
415 dev->devnum, fmt->iface, fmt->altsetting,
416 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
e5779998
DM
417 return -EINVAL;
418 }
c75a8a7a
DM
419
420 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
421 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
422
edcd3633
DM
423add_sync_ep:
424 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
425 alts, ep, !subs->direction,
c75a8a7a
DM
426 implicit_fb ?
427 SND_USB_ENDPOINT_TYPE_DATA :
428 SND_USB_ENDPOINT_TYPE_SYNC);
edcd3633
DM
429 if (!subs->sync_endpoint)
430 return -EINVAL;
431
432 subs->data_endpoint->sync_master = subs->sync_endpoint;
433 }
e5779998 434
71bb64c5
EZ
435 return 0;
436}
437
438/*
439 * find a matching format and set up the interface
440 */
441static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
442{
443 struct usb_device *dev = subs->dev;
444 struct usb_host_interface *alts;
445 struct usb_interface_descriptor *altsd;
446 struct usb_interface *iface;
447 int err;
448
449 iface = usb_ifnum_to_if(dev, fmt->iface);
450 if (WARN_ON(!iface))
451 return -EINVAL;
452 alts = &iface->altsetting[fmt->altset_idx];
453 altsd = get_iface_desc(alts);
454 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
455 return -EINVAL;
456
457 if (fmt == subs->cur_audiofmt)
458 return 0;
459
460 /* close the old interface */
461 if (subs->interface >= 0 && subs->interface != fmt->iface) {
462 err = usb_set_interface(subs->dev, subs->interface, 0);
463 if (err < 0) {
464 snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
465 dev->devnum, fmt->iface, fmt->altsetting, err);
466 return -EIO;
467 }
468 subs->interface = -1;
469 subs->altset_idx = 0;
470 }
471
472 /* set interface */
473 if (subs->interface != fmt->iface ||
474 subs->altset_idx != fmt->altset_idx) {
475 err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
476 if (err < 0) {
477 snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
478 dev->devnum, fmt->iface, fmt->altsetting, err);
479 return -EIO;
480 }
481 snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
482 fmt->iface, fmt->altsetting);
483 subs->interface = fmt->iface;
484 subs->altset_idx = fmt->altset_idx;
485
486 snd_usb_set_interface_quirk(dev);
487 }
488
489 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
490 alts, fmt->endpoint, subs->direction,
491 SND_USB_ENDPOINT_TYPE_DATA);
492
493 if (!subs->data_endpoint)
494 return -EINVAL;
495
496 err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
497 if (err < 0)
498 return err;
499
d133f2c2
EZ
500 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
501 if (err < 0)
e5779998
DM
502 return err;
503
504 subs->cur_audiofmt = fmt;
505
506 snd_usb_set_format_quirk(subs, fmt);
507
e5779998
DM
508 return 0;
509}
510
0d9741c0
EZ
511/*
512 * Return the score of matching two audioformats.
513 * Veto the audioformat if:
514 * - It has no channels for some reason.
515 * - Requested PCM format is not supported.
516 * - Requested sample rate is not supported.
517 */
518static int match_endpoint_audioformats(struct audioformat *fp,
519 struct audioformat *match, int rate,
520 snd_pcm_format_t pcm_format)
521{
522 int i;
523 int score = 0;
524
525 if (fp->channels < 1) {
526 snd_printdd("%s: (fmt @%p) no channels\n", __func__, fp);
527 return 0;
528 }
529
74c34ca1 530 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
0d9741c0
EZ
531 snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__,
532 fp, pcm_format);
533 return 0;
534 }
535
536 for (i = 0; i < fp->nr_rates; i++) {
537 if (fp->rate_table[i] == rate) {
538 score++;
539 break;
540 }
541 }
542 if (!score) {
543 snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__,
544 fp, rate);
545 return 0;
546 }
547
548 if (fp->channels == match->channels)
549 score++;
550
551 snd_printdd("%s: (fmt @%p) score %d\n", __func__, fp, score);
552
553 return score;
554}
555
556/*
557 * Configure the sync ep using the rate and pcm format of the data ep.
558 */
559static int configure_sync_endpoint(struct snd_usb_substream *subs)
560{
561 int ret;
562 struct audioformat *fp;
563 struct audioformat *sync_fp = NULL;
564 int cur_score = 0;
565 int sync_period_bytes = subs->period_bytes;
566 struct snd_usb_substream *sync_subs =
567 &subs->stream->substream[subs->direction ^ 1];
568
31be5425
TI
569 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
570 !subs->stream)
571 return snd_usb_endpoint_set_params(subs->sync_endpoint,
572 subs->pcm_format,
573 subs->channels,
574 subs->period_bytes,
575 subs->cur_rate,
576 subs->cur_audiofmt,
577 NULL);
578
0d9741c0
EZ
579 /* Try to find the best matching audioformat. */
580 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
581 int score = match_endpoint_audioformats(fp, subs->cur_audiofmt,
582 subs->cur_rate, subs->pcm_format);
583
584 if (score > cur_score) {
585 sync_fp = fp;
586 cur_score = score;
587 }
588 }
589
590 if (unlikely(sync_fp == NULL)) {
591 snd_printk(KERN_ERR "%s: no valid audioformat for sync ep %x found\n",
592 __func__, sync_subs->ep_num);
593 return -EINVAL;
594 }
595
596 /*
597 * Recalculate the period bytes if channel number differ between
598 * data and sync ep audioformat.
599 */
600 if (sync_fp->channels != subs->channels) {
601 sync_period_bytes = (subs->period_bytes / subs->channels) *
602 sync_fp->channels;
603 snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n",
604 __func__, subs->period_bytes, sync_period_bytes);
605 }
606
607 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
608 subs->pcm_format,
609 sync_fp->channels,
610 sync_period_bytes,
611 subs->cur_rate,
612 sync_fp,
613 NULL);
614
615 return ret;
616}
617
61a70950
DR
618/*
619 * configure endpoint params
620 *
621 * called during initial setup and upon resume
622 */
623static int configure_endpoint(struct snd_usb_substream *subs)
624{
625 int ret;
626
61a70950 627 /* format changed */
b0db6063 628 stop_endpoints(subs, true);
61a70950
DR
629 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
630 subs->pcm_format,
631 subs->channels,
632 subs->period_bytes,
633 subs->cur_rate,
634 subs->cur_audiofmt,
635 subs->sync_endpoint);
636 if (ret < 0)
978520b7 637 return ret;
61a70950
DR
638
639 if (subs->sync_endpoint)
0d9741c0
EZ
640 ret = configure_sync_endpoint(subs);
641
61a70950
DR
642 return ret;
643}
644
e5779998
DM
645/*
646 * hw_params callback
647 *
648 * allocate a buffer and set the given audio format.
649 *
650 * so far we use a physically linear buffer although packetize transfer
651 * doesn't need a continuous area.
652 * if sg buffer is supported on the later version of alsa, we'll follow
653 * that.
654 */
655static int snd_usb_hw_params(struct snd_pcm_substream *substream,
656 struct snd_pcm_hw_params *hw_params)
657{
658 struct snd_usb_substream *subs = substream->runtime->private_data;
659 struct audioformat *fmt;
61a70950 660 int ret;
e5779998
DM
661
662 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
663 params_buffer_bytes(hw_params));
664 if (ret < 0)
665 return ret;
666
61a70950
DR
667 subs->pcm_format = params_format(hw_params);
668 subs->period_bytes = params_period_bytes(hw_params);
669 subs->channels = params_channels(hw_params);
670 subs->cur_rate = params_rate(hw_params);
671
672 fmt = find_format(subs);
e5779998
DM
673 if (!fmt) {
674 snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
61a70950 675 subs->pcm_format, subs->cur_rate, subs->channels);
e5779998
DM
676 return -EINVAL;
677 }
678
34f3c89f 679 down_read(&subs->stream->chip->shutdown_rwsem);
978520b7
TI
680 if (subs->stream->chip->shutdown)
681 ret = -ENODEV;
682 else
683 ret = set_format(subs, fmt);
34f3c89f 684 up_read(&subs->stream->chip->shutdown_rwsem);
978520b7 685 if (ret < 0)
e5779998
DM
686 return ret;
687
61a70950
DR
688 subs->interface = fmt->iface;
689 subs->altset_idx = fmt->altset_idx;
384dc085 690 subs->need_setup_ep = true;
715a1705 691
61a70950 692 return 0;
e5779998
DM
693}
694
695/*
696 * hw_free callback
697 *
698 * reset the audio format and release the buffer
699 */
700static int snd_usb_hw_free(struct snd_pcm_substream *substream)
701{
702 struct snd_usb_substream *subs = substream->runtime->private_data;
703
704 subs->cur_audiofmt = NULL;
705 subs->cur_rate = 0;
706 subs->period_bytes = 0;
34f3c89f 707 down_read(&subs->stream->chip->shutdown_rwsem);
978520b7 708 if (!subs->stream->chip->shutdown) {
a9bb3626 709 stop_endpoints(subs, true);
978520b7
TI
710 deactivate_endpoints(subs);
711 }
34f3c89f 712 up_read(&subs->stream->chip->shutdown_rwsem);
e5779998
DM
713 return snd_pcm_lib_free_vmalloc_buffer(substream);
714}
715
716/*
717 * prepare callback
718 *
719 * only a few subtle things...
720 */
721static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
722{
723 struct snd_pcm_runtime *runtime = substream->runtime;
724 struct snd_usb_substream *subs = runtime->private_data;
61a70950
DR
725 struct usb_host_interface *alts;
726 struct usb_interface *iface;
727 int ret;
e5779998
DM
728
729 if (! subs->cur_audiofmt) {
730 snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
731 return -ENXIO;
732 }
733
34f3c89f 734 down_read(&subs->stream->chip->shutdown_rwsem);
978520b7
TI
735 if (subs->stream->chip->shutdown) {
736 ret = -ENODEV;
737 goto unlock;
738 }
739 if (snd_BUG_ON(!subs->data_endpoint)) {
740 ret = -EIO;
741 goto unlock;
742 }
edcd3633 743
f58161ba
TI
744 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
745 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
746
61a70950
DR
747 ret = set_format(subs, subs->cur_audiofmt);
748 if (ret < 0)
978520b7 749 goto unlock;
61a70950
DR
750
751 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
752 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
753 ret = snd_usb_init_sample_rate(subs->stream->chip,
754 subs->cur_audiofmt->iface,
755 alts,
756 subs->cur_audiofmt,
757 subs->cur_rate);
758 if (ret < 0)
978520b7 759 goto unlock;
61a70950 760
384dc085
TI
761 if (subs->need_setup_ep) {
762 ret = configure_endpoint(subs);
763 if (ret < 0)
978520b7 764 goto unlock;
384dc085
TI
765 subs->need_setup_ep = false;
766 }
61a70950 767
e5779998 768 /* some unit conversions in runtime */
edcd3633
DM
769 subs->data_endpoint->maxframesize =
770 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
771 subs->data_endpoint->curframesize =
772 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
e5779998
DM
773
774 /* reset the pointer */
775 subs->hwptr_done = 0;
776 subs->transfer_done = 0;
294c4fb8
PLB
777 subs->last_delay = 0;
778 subs->last_frame_number = 0;
e5779998
DM
779 runtime->delay = 0;
780
edcd3633
DM
781 /* for playback, submit the URBs now; otherwise, the first hwptr_done
782 * updates for all URBs would happen at the same time when starting */
783 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
a9bb3626 784 ret = start_endpoints(subs, true);
edcd3633 785
978520b7 786 unlock:
34f3c89f 787 up_read(&subs->stream->chip->shutdown_rwsem);
978520b7 788 return ret;
e5779998
DM
789}
790
791static struct snd_pcm_hardware snd_usb_hardware =
792{
793 .info = SNDRV_PCM_INFO_MMAP |
794 SNDRV_PCM_INFO_MMAP_VALID |
795 SNDRV_PCM_INFO_BATCH |
796 SNDRV_PCM_INFO_INTERLEAVED |
797 SNDRV_PCM_INFO_BLOCK_TRANSFER |
798 SNDRV_PCM_INFO_PAUSE,
799 .buffer_bytes_max = 1024 * 1024,
800 .period_bytes_min = 64,
801 .period_bytes_max = 512 * 1024,
802 .periods_min = 2,
803 .periods_max = 1024,
804};
805
806static int hw_check_valid_format(struct snd_usb_substream *subs,
807 struct snd_pcm_hw_params *params,
808 struct audioformat *fp)
809{
810 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
811 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
812 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
813 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
015eb0b0 814 struct snd_mask check_fmts;
e5779998
DM
815 unsigned int ptime;
816
817 /* check the format */
015eb0b0
CL
818 snd_mask_none(&check_fmts);
819 check_fmts.bits[0] = (u32)fp->formats;
820 check_fmts.bits[1] = (u32)(fp->formats >> 32);
821 snd_mask_intersect(&check_fmts, fmts);
822 if (snd_mask_empty(&check_fmts)) {
e5779998
DM
823 hwc_debug(" > check: no supported format %d\n", fp->format);
824 return 0;
825 }
826 /* check the channels */
827 if (fp->channels < ct->min || fp->channels > ct->max) {
828 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
829 return 0;
830 }
831 /* check the rate is within the range */
832 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
833 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
834 return 0;
835 }
836 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
837 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
838 return 0;
839 }
840 /* check whether the period time is >= the data packet interval */
978520b7 841 if (subs->speed != USB_SPEED_FULL) {
e5779998
DM
842 ptime = 125 * (1 << fp->datainterval);
843 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
844 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
845 return 0;
846 }
847 }
848 return 1;
849}
850
851static int hw_rule_rate(struct snd_pcm_hw_params *params,
852 struct snd_pcm_hw_rule *rule)
853{
854 struct snd_usb_substream *subs = rule->private;
88766f04 855 struct audioformat *fp;
e5779998
DM
856 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
857 unsigned int rmin, rmax;
858 int changed;
859
860 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
861 changed = 0;
862 rmin = rmax = 0;
88766f04 863 list_for_each_entry(fp, &subs->fmt_list, list) {
e5779998
DM
864 if (!hw_check_valid_format(subs, params, fp))
865 continue;
866 if (changed++) {
867 if (rmin > fp->rate_min)
868 rmin = fp->rate_min;
869 if (rmax < fp->rate_max)
870 rmax = fp->rate_max;
871 } else {
872 rmin = fp->rate_min;
873 rmax = fp->rate_max;
874 }
875 }
876
877 if (!changed) {
878 hwc_debug(" --> get empty\n");
879 it->empty = 1;
880 return -EINVAL;
881 }
882
883 changed = 0;
884 if (it->min < rmin) {
885 it->min = rmin;
886 it->openmin = 0;
887 changed = 1;
888 }
889 if (it->max > rmax) {
890 it->max = rmax;
891 it->openmax = 0;
892 changed = 1;
893 }
894 if (snd_interval_checkempty(it)) {
895 it->empty = 1;
896 return -EINVAL;
897 }
898 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
899 return changed;
900}
901
902
903static int hw_rule_channels(struct snd_pcm_hw_params *params,
904 struct snd_pcm_hw_rule *rule)
905{
906 struct snd_usb_substream *subs = rule->private;
88766f04 907 struct audioformat *fp;
e5779998
DM
908 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
909 unsigned int rmin, rmax;
910 int changed;
911
912 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
913 changed = 0;
914 rmin = rmax = 0;
88766f04 915 list_for_each_entry(fp, &subs->fmt_list, list) {
e5779998
DM
916 if (!hw_check_valid_format(subs, params, fp))
917 continue;
918 if (changed++) {
919 if (rmin > fp->channels)
920 rmin = fp->channels;
921 if (rmax < fp->channels)
922 rmax = fp->channels;
923 } else {
924 rmin = fp->channels;
925 rmax = fp->channels;
926 }
927 }
928
929 if (!changed) {
930 hwc_debug(" --> get empty\n");
931 it->empty = 1;
932 return -EINVAL;
933 }
934
935 changed = 0;
936 if (it->min < rmin) {
937 it->min = rmin;
938 it->openmin = 0;
939 changed = 1;
940 }
941 if (it->max > rmax) {
942 it->max = rmax;
943 it->openmax = 0;
944 changed = 1;
945 }
946 if (snd_interval_checkempty(it)) {
947 it->empty = 1;
948 return -EINVAL;
949 }
950 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
951 return changed;
952}
953
954static int hw_rule_format(struct snd_pcm_hw_params *params,
955 struct snd_pcm_hw_rule *rule)
956{
957 struct snd_usb_substream *subs = rule->private;
88766f04 958 struct audioformat *fp;
e5779998
DM
959 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
960 u64 fbits;
961 u32 oldbits[2];
962 int changed;
963
964 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
965 fbits = 0;
88766f04 966 list_for_each_entry(fp, &subs->fmt_list, list) {
e5779998
DM
967 if (!hw_check_valid_format(subs, params, fp))
968 continue;
015eb0b0 969 fbits |= fp->formats;
e5779998
DM
970 }
971
972 oldbits[0] = fmt->bits[0];
973 oldbits[1] = fmt->bits[1];
974 fmt->bits[0] &= (u32)fbits;
975 fmt->bits[1] &= (u32)(fbits >> 32);
976 if (!fmt->bits[0] && !fmt->bits[1]) {
977 hwc_debug(" --> get empty\n");
978 return -EINVAL;
979 }
980 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
981 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
982 return changed;
983}
984
985static int hw_rule_period_time(struct snd_pcm_hw_params *params,
986 struct snd_pcm_hw_rule *rule)
987{
988 struct snd_usb_substream *subs = rule->private;
989 struct audioformat *fp;
990 struct snd_interval *it;
991 unsigned char min_datainterval;
992 unsigned int pmin;
993 int changed;
994
995 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
996 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
997 min_datainterval = 0xff;
998 list_for_each_entry(fp, &subs->fmt_list, list) {
999 if (!hw_check_valid_format(subs, params, fp))
1000 continue;
1001 min_datainterval = min(min_datainterval, fp->datainterval);
1002 }
1003 if (min_datainterval == 0xff) {
a7ce2e0d 1004 hwc_debug(" --> get empty\n");
e5779998
DM
1005 it->empty = 1;
1006 return -EINVAL;
1007 }
1008 pmin = 125 * (1 << min_datainterval);
1009 changed = 0;
1010 if (it->min < pmin) {
1011 it->min = pmin;
1012 it->openmin = 0;
1013 changed = 1;
1014 }
1015 if (snd_interval_checkempty(it)) {
1016 it->empty = 1;
1017 return -EINVAL;
1018 }
1019 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1020 return changed;
1021}
1022
1023/*
1024 * If the device supports unusual bit rates, does the request meet these?
1025 */
1026static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1027 struct snd_usb_substream *subs)
1028{
1029 struct audioformat *fp;
0717d0f5 1030 int *rate_list;
e5779998
DM
1031 int count = 0, needs_knot = 0;
1032 int err;
1033
5cd5d7c4
CL
1034 kfree(subs->rate_list.list);
1035 subs->rate_list.list = NULL;
1036
e5779998
DM
1037 list_for_each_entry(fp, &subs->fmt_list, list) {
1038 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1039 return 0;
1040 count += fp->nr_rates;
1041 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1042 needs_knot = 1;
1043 }
1044 if (!needs_knot)
1045 return 0;
1046
0717d0f5
TI
1047 subs->rate_list.list = rate_list =
1048 kmalloc(sizeof(int) * count, GFP_KERNEL);
8a8d56b2
JJ
1049 if (!subs->rate_list.list)
1050 return -ENOMEM;
1051 subs->rate_list.count = count;
e5779998
DM
1052 subs->rate_list.mask = 0;
1053 count = 0;
1054 list_for_each_entry(fp, &subs->fmt_list, list) {
1055 int i;
1056 for (i = 0; i < fp->nr_rates; i++)
0717d0f5 1057 rate_list[count++] = fp->rate_table[i];
e5779998
DM
1058 }
1059 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1060 &subs->rate_list);
1061 if (err < 0)
1062 return err;
1063
1064 return 0;
1065}
1066
1067
1068/*
1069 * set up the runtime hardware information.
1070 */
1071
1072static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1073{
88766f04 1074 struct audioformat *fp;
e5779998
DM
1075 unsigned int pt, ptmin;
1076 int param_period_time_if_needed;
1077 int err;
1078
1079 runtime->hw.formats = subs->formats;
1080
1081 runtime->hw.rate_min = 0x7fffffff;
1082 runtime->hw.rate_max = 0;
1083 runtime->hw.channels_min = 256;
1084 runtime->hw.channels_max = 0;
1085 runtime->hw.rates = 0;
1086 ptmin = UINT_MAX;
1087 /* check min/max rates and channels */
88766f04 1088 list_for_each_entry(fp, &subs->fmt_list, list) {
e5779998
DM
1089 runtime->hw.rates |= fp->rates;
1090 if (runtime->hw.rate_min > fp->rate_min)
1091 runtime->hw.rate_min = fp->rate_min;
1092 if (runtime->hw.rate_max < fp->rate_max)
1093 runtime->hw.rate_max = fp->rate_max;
1094 if (runtime->hw.channels_min > fp->channels)
1095 runtime->hw.channels_min = fp->channels;
1096 if (runtime->hw.channels_max < fp->channels)
1097 runtime->hw.channels_max = fp->channels;
1098 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1099 /* FIXME: there might be more than one audio formats... */
1100 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1101 fp->frame_size;
1102 }
1103 pt = 125 * (1 << fp->datainterval);
1104 ptmin = min(ptmin, pt);
1105 }
88a8516a
ON
1106 err = snd_usb_autoresume(subs->stream->chip);
1107 if (err < 0)
1108 return err;
e5779998
DM
1109
1110 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
978520b7 1111 if (subs->speed == USB_SPEED_FULL)
e5779998
DM
1112 /* full speed devices have fixed data packet interval */
1113 ptmin = 1000;
1114 if (ptmin == 1000)
1115 /* if period time doesn't go below 1 ms, no rules needed */
1116 param_period_time_if_needed = -1;
1117 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1118 ptmin, UINT_MAX);
1119
1120 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1121 hw_rule_rate, subs,
1122 SNDRV_PCM_HW_PARAM_FORMAT,
1123 SNDRV_PCM_HW_PARAM_CHANNELS,
1124 param_period_time_if_needed,
1125 -1)) < 0)
88a8516a 1126 goto rep_err;
e5779998
DM
1127 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1128 hw_rule_channels, subs,
1129 SNDRV_PCM_HW_PARAM_FORMAT,
1130 SNDRV_PCM_HW_PARAM_RATE,
1131 param_period_time_if_needed,
1132 -1)) < 0)
88a8516a 1133 goto rep_err;
e5779998
DM
1134 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1135 hw_rule_format, subs,
1136 SNDRV_PCM_HW_PARAM_RATE,
1137 SNDRV_PCM_HW_PARAM_CHANNELS,
1138 param_period_time_if_needed,
1139 -1)) < 0)
88a8516a 1140 goto rep_err;
e5779998
DM
1141 if (param_period_time_if_needed >= 0) {
1142 err = snd_pcm_hw_rule_add(runtime, 0,
1143 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1144 hw_rule_period_time, subs,
1145 SNDRV_PCM_HW_PARAM_FORMAT,
1146 SNDRV_PCM_HW_PARAM_CHANNELS,
1147 SNDRV_PCM_HW_PARAM_RATE,
1148 -1);
1149 if (err < 0)
88a8516a 1150 goto rep_err;
e5779998
DM
1151 }
1152 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
88a8516a 1153 goto rep_err;
e5779998 1154 return 0;
88a8516a
ON
1155
1156rep_err:
1157 snd_usb_autosuspend(subs->stream->chip);
1158 return err;
e5779998
DM
1159}
1160
1161static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1162{
1163 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1164 struct snd_pcm_runtime *runtime = substream->runtime;
1165 struct snd_usb_substream *subs = &as->substream[direction];
1166
1167 subs->interface = -1;
e11b4e0e 1168 subs->altset_idx = 0;
e5779998
DM
1169 runtime->hw = snd_usb_hardware;
1170 runtime->private_data = subs;
1171 subs->pcm_substream = substream;
88a8516a 1172 /* runtime PM is also done there */
d24f5061
DM
1173
1174 /* initialize DSD/DOP context */
1175 subs->dsd_dop.byte_idx = 0;
1176 subs->dsd_dop.channel = 0;
1177 subs->dsd_dop.marker = 1;
1178
e5779998
DM
1179 return setup_hw_info(runtime, subs);
1180}
1181
1182static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1183{
1184 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1185 struct snd_usb_substream *subs = &as->substream[direction];
1186
b0db6063 1187 stop_endpoints(subs, true);
68e67f40
DM
1188
1189 if (!as->chip->shutdown && subs->interface >= 0) {
1190 usb_set_interface(subs->dev, subs->interface, 0);
1191 subs->interface = -1;
1192 }
1193
e5779998 1194 subs->pcm_substream = NULL;
88a8516a 1195 snd_usb_autosuspend(subs->stream->chip);
edcd3633 1196
68e67f40 1197 return 0;
edcd3633
DM
1198}
1199
1200/* Since a URB can handle only a single linear buffer, we must use double
1201 * buffering when the data to be transferred overflows the buffer boundary.
1202 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1203 * for all URBs.
1204 */
1205static void retire_capture_urb(struct snd_usb_substream *subs,
1206 struct urb *urb)
1207{
1208 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1209 unsigned int stride, frames, bytes, oldptr;
1210 int i, period_elapsed = 0;
1211 unsigned long flags;
1212 unsigned char *cp;
e4cc6153
PLB
1213 int current_frame_number;
1214
1215 /* read frame number here, update pointer in critical section */
1216 current_frame_number = usb_get_current_frame_number(subs->dev);
edcd3633
DM
1217
1218 stride = runtime->frame_bits >> 3;
1219
1220 for (i = 0; i < urb->number_of_packets; i++) {
1539d4f8 1221 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
edcd3633
DM
1222 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1223 snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1224 // continue;
1225 }
1226 bytes = urb->iso_frame_desc[i].actual_length;
1227 frames = bytes / stride;
1228 if (!subs->txfr_quirk)
1229 bytes = frames * stride;
1230 if (bytes % (runtime->sample_bits >> 3) != 0) {
edcd3633 1231 int oldbytes = bytes;
edcd3633
DM
1232 bytes = frames * stride;
1233 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1234 oldbytes, bytes);
1235 }
1236 /* update the current pointer */
1237 spin_lock_irqsave(&subs->lock, flags);
1238 oldptr = subs->hwptr_done;
1239 subs->hwptr_done += bytes;
1240 if (subs->hwptr_done >= runtime->buffer_size * stride)
1241 subs->hwptr_done -= runtime->buffer_size * stride;
1242 frames = (bytes + (oldptr % stride)) / stride;
1243 subs->transfer_done += frames;
1244 if (subs->transfer_done >= runtime->period_size) {
1245 subs->transfer_done -= runtime->period_size;
1246 period_elapsed = 1;
1247 }
e4cc6153
PLB
1248 /* capture delay is by construction limited to one URB,
1249 * reset delays here
1250 */
1251 runtime->delay = subs->last_delay = 0;
1252
1253 /* realign last_frame_number */
1254 subs->last_frame_number = current_frame_number;
1255 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1256
edcd3633
DM
1257 spin_unlock_irqrestore(&subs->lock, flags);
1258 /* copy a data chunk */
1259 if (oldptr + bytes > runtime->buffer_size * stride) {
1260 unsigned int bytes1 =
1261 runtime->buffer_size * stride - oldptr;
1262 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1263 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1264 } else {
1265 memcpy(runtime->dma_area + oldptr, cp, bytes);
1266 }
1267 }
1268
1269 if (period_elapsed)
1270 snd_pcm_period_elapsed(subs->pcm_substream);
1271}
1272
d24f5061
DM
1273static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1274 struct urb *urb, unsigned int bytes)
1275{
1276 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1277 unsigned int stride = runtime->frame_bits >> 3;
1278 unsigned int dst_idx = 0;
1279 unsigned int src_idx = subs->hwptr_done;
1280 unsigned int wrap = runtime->buffer_size * stride;
1281 u8 *dst = urb->transfer_buffer;
1282 u8 *src = runtime->dma_area;
1283 u8 marker[] = { 0x05, 0xfa };
1284
1285 /*
1286 * The DSP DOP format defines a way to transport DSD samples over
1287 * normal PCM data endpoints. It requires stuffing of marker bytes
1288 * (0x05 and 0xfa, alternating per sample frame), and then expects
1289 * 2 additional bytes of actual payload. The whole frame is stored
1290 * LSB.
1291 *
1292 * Hence, for a stereo transport, the buffer layout looks like this,
1293 * where L refers to left channel samples and R to right.
1294 *
1295 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1296 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1297 * .....
1298 *
1299 */
1300
1301 while (bytes--) {
1302 if (++subs->dsd_dop.byte_idx == 3) {
1303 /* frame boundary? */
1304 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1305 src_idx += 2;
1306 subs->dsd_dop.byte_idx = 0;
1307
1308 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1309 /* alternate the marker */
1310 subs->dsd_dop.marker++;
1311 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1312 subs->dsd_dop.channel = 0;
1313 }
1314 } else {
1315 /* stuff the DSD payload */
1316 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
44dcbbb1
DM
1317
1318 if (subs->cur_audiofmt->dsd_bitrev)
1319 dst[dst_idx++] = bitrev8(src[idx]);
1320 else
1321 dst[dst_idx++] = src[idx];
1322
d24f5061
DM
1323 subs->hwptr_done++;
1324 }
1325 }
1326}
1327
edcd3633
DM
1328static void prepare_playback_urb(struct snd_usb_substream *subs,
1329 struct urb *urb)
1330{
1331 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
245baf98 1332 struct snd_usb_endpoint *ep = subs->data_endpoint;
edcd3633
DM
1333 struct snd_urb_ctx *ctx = urb->context;
1334 unsigned int counts, frames, bytes;
1335 int i, stride, period_elapsed = 0;
1336 unsigned long flags;
1337
1338 stride = runtime->frame_bits >> 3;
1339
1340 frames = 0;
1341 urb->number_of_packets = 0;
1342 spin_lock_irqsave(&subs->lock, flags);
1343 for (i = 0; i < ctx->packets; i++) {
245baf98
DM
1344 if (ctx->packet_size[i])
1345 counts = ctx->packet_size[i];
1346 else
1347 counts = snd_usb_endpoint_next_packet_size(ep);
1348
edcd3633 1349 /* set up descriptor */
8a2a74d2
DM
1350 urb->iso_frame_desc[i].offset = frames * ep->stride;
1351 urb->iso_frame_desc[i].length = counts * ep->stride;
edcd3633
DM
1352 frames += counts;
1353 urb->number_of_packets++;
1354 subs->transfer_done += counts;
1355 if (subs->transfer_done >= runtime->period_size) {
1356 subs->transfer_done -= runtime->period_size;
1357 period_elapsed = 1;
1358 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1359 if (subs->transfer_done > 0) {
1360 /* FIXME: fill-max mode is not
1361 * supported yet */
1362 frames -= subs->transfer_done;
1363 counts -= subs->transfer_done;
1364 urb->iso_frame_desc[i].length =
8a2a74d2 1365 counts * ep->stride;
edcd3633
DM
1366 subs->transfer_done = 0;
1367 }
1368 i++;
1369 if (i < ctx->packets) {
1370 /* add a transfer delimiter */
1371 urb->iso_frame_desc[i].offset =
8a2a74d2 1372 frames * ep->stride;
edcd3633
DM
1373 urb->iso_frame_desc[i].length = 0;
1374 urb->number_of_packets++;
1375 }
1376 break;
1377 }
1378 }
1379 if (period_elapsed &&
98ae472b 1380 !snd_usb_endpoint_implicit_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
edcd3633
DM
1381 break;
1382 }
8a2a74d2 1383 bytes = frames * ep->stride;
d24f5061
DM
1384
1385 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1386 subs->cur_audiofmt->dsd_dop)) {
1387 fill_playback_urb_dsd_dop(subs, urb, bytes);
44dcbbb1
DM
1388 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1389 subs->cur_audiofmt->dsd_bitrev)) {
1390 /* bit-reverse the bytes */
1391 u8 *buf = urb->transfer_buffer;
1392 for (i = 0; i < bytes; i++) {
1393 int idx = (subs->hwptr_done + i)
1394 % (runtime->buffer_size * stride);
1395 buf[i] = bitrev8(runtime->dma_area[idx]);
1396 }
1397
1398 subs->hwptr_done += bytes;
edcd3633 1399 } else {
d24f5061
DM
1400 /* usual PCM */
1401 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1402 /* err, the transferred area goes over buffer boundary. */
1403 unsigned int bytes1 =
1404 runtime->buffer_size * stride - subs->hwptr_done;
1405 memcpy(urb->transfer_buffer,
1406 runtime->dma_area + subs->hwptr_done, bytes1);
1407 memcpy(urb->transfer_buffer + bytes1,
1408 runtime->dma_area, bytes - bytes1);
1409 } else {
1410 memcpy(urb->transfer_buffer,
1411 runtime->dma_area + subs->hwptr_done, bytes);
1412 }
1413
1414 subs->hwptr_done += bytes;
edcd3633 1415 }
d24f5061 1416
edcd3633
DM
1417 if (subs->hwptr_done >= runtime->buffer_size * stride)
1418 subs->hwptr_done -= runtime->buffer_size * stride;
fbcfbf5f
DM
1419
1420 /* update delay with exact number of samples queued */
1421 runtime->delay = subs->last_delay;
edcd3633 1422 runtime->delay += frames;
fbcfbf5f
DM
1423 subs->last_delay = runtime->delay;
1424
1425 /* realign last_frame_number */
1426 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1427 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1428
edcd3633
DM
1429 spin_unlock_irqrestore(&subs->lock, flags);
1430 urb->transfer_buffer_length = bytes;
1431 if (period_elapsed)
1432 snd_pcm_period_elapsed(subs->pcm_substream);
1433}
1434
1435/*
1436 * process after playback data complete
1437 * - decrease the delay count again
1438 */
1439static void retire_playback_urb(struct snd_usb_substream *subs,
1440 struct urb *urb)
1441{
1442 unsigned long flags;
1443 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
8a2a74d2
DM
1444 struct snd_usb_endpoint *ep = subs->data_endpoint;
1445 int processed = urb->transfer_buffer_length / ep->stride;
fbcfbf5f 1446 int est_delay;
edcd3633 1447
1213a205
TI
1448 /* ignore the delay accounting when procssed=0 is given, i.e.
1449 * silent payloads are procssed before handling the actual data
1450 */
1451 if (!processed)
1452 return;
1453
edcd3633 1454 spin_lock_irqsave(&subs->lock, flags);
48779a0b
TI
1455 if (!subs->last_delay)
1456 goto out; /* short path */
1457
fbcfbf5f
DM
1458 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1459 /* update delay with exact number of samples played */
1460 if (processed > subs->last_delay)
1461 subs->last_delay = 0;
edcd3633 1462 else
fbcfbf5f
DM
1463 subs->last_delay -= processed;
1464 runtime->delay = subs->last_delay;
1465
1466 /*
1467 * Report when delay estimate is off by more than 2ms.
1468 * The error should be lower than 2ms since the estimate relies
1469 * on two reads of a counter updated every ms.
1470 */
1471 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1472 snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1473 est_delay, subs->last_delay);
1474
48779a0b
TI
1475 if (!subs->running) {
1476 /* update last_frame_number for delay counting here since
1477 * prepare_playback_urb won't be called during pause
1478 */
1479 subs->last_frame_number =
1480 usb_get_current_frame_number(subs->dev) & 0xff;
1481 }
1482
1483 out:
edcd3633 1484 spin_unlock_irqrestore(&subs->lock, flags);
e5779998
DM
1485}
1486
1487static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1488{
1489 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1490}
1491
1492static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1493{
1494 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1495}
1496
1497static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1498{
1499 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1500}
1501
1502static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1503{
1504 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1505}
1506
edcd3633
DM
1507static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1508 int cmd)
1509{
1510 struct snd_usb_substream *subs = substream->runtime->private_data;
1511
1512 switch (cmd) {
1513 case SNDRV_PCM_TRIGGER_START:
1514 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1515 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1516 subs->data_endpoint->retire_data_urb = retire_playback_urb;
97f8d3b6 1517 subs->running = 1;
edcd3633
DM
1518 return 0;
1519 case SNDRV_PCM_TRIGGER_STOP:
a9bb3626 1520 stop_endpoints(subs, false);
97f8d3b6 1521 subs->running = 0;
edcd3633
DM
1522 return 0;
1523 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1524 subs->data_endpoint->prepare_data_urb = NULL;
48779a0b
TI
1525 /* keep retire_data_urb for delay calculation */
1526 subs->data_endpoint->retire_data_urb = retire_playback_urb;
97f8d3b6 1527 subs->running = 0;
edcd3633
DM
1528 return 0;
1529 }
1530
1531 return -EINVAL;
1532}
1533
afe25967
DM
1534static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1535 int cmd)
edcd3633
DM
1536{
1537 int err;
1538 struct snd_usb_substream *subs = substream->runtime->private_data;
1539
1540 switch (cmd) {
1541 case SNDRV_PCM_TRIGGER_START:
a9bb3626 1542 err = start_endpoints(subs, false);
edcd3633
DM
1543 if (err < 0)
1544 return err;
1545
1546 subs->data_endpoint->retire_data_urb = retire_capture_urb;
97f8d3b6 1547 subs->running = 1;
edcd3633
DM
1548 return 0;
1549 case SNDRV_PCM_TRIGGER_STOP:
a9bb3626 1550 stop_endpoints(subs, false);
97f8d3b6 1551 subs->running = 0;
edcd3633
DM
1552 return 0;
1553 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1554 subs->data_endpoint->retire_data_urb = NULL;
97f8d3b6 1555 subs->running = 0;
edcd3633
DM
1556 return 0;
1557 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1558 subs->data_endpoint->retire_data_urb = retire_capture_urb;
97f8d3b6 1559 subs->running = 1;
edcd3633
DM
1560 return 0;
1561 }
1562
1563 return -EINVAL;
1564}
1565
e5779998
DM
1566static struct snd_pcm_ops snd_usb_playback_ops = {
1567 .open = snd_usb_playback_open,
1568 .close = snd_usb_playback_close,
1569 .ioctl = snd_pcm_lib_ioctl,
1570 .hw_params = snd_usb_hw_params,
1571 .hw_free = snd_usb_hw_free,
1572 .prepare = snd_usb_pcm_prepare,
1573 .trigger = snd_usb_substream_playback_trigger,
1574 .pointer = snd_usb_pcm_pointer,
1575 .page = snd_pcm_lib_get_vmalloc_page,
1576 .mmap = snd_pcm_lib_mmap_vmalloc,
1577};
1578
1579static struct snd_pcm_ops snd_usb_capture_ops = {
1580 .open = snd_usb_capture_open,
1581 .close = snd_usb_capture_close,
1582 .ioctl = snd_pcm_lib_ioctl,
1583 .hw_params = snd_usb_hw_params,
1584 .hw_free = snd_usb_hw_free,
1585 .prepare = snd_usb_pcm_prepare,
1586 .trigger = snd_usb_substream_capture_trigger,
1587 .pointer = snd_usb_pcm_pointer,
1588 .page = snd_pcm_lib_get_vmalloc_page,
1589 .mmap = snd_pcm_lib_mmap_vmalloc,
1590};
1591
1592void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1593{
1594 snd_pcm_set_ops(pcm, stream,
1595 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1596 &snd_usb_playback_ops : &snd_usb_capture_ops);
1597}
This page took 0.184539 seconds and 5 git commands to generate.