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