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