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