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