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