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