RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / sound / usb / urb.c
blob118d6deb5e3e74f27c238229f4e1b46264398d49
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.
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.
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
18 #include <linux/gfp.h>
19 #include <linux/init.h>
20 #include <linux/usb.h>
21 #include <linux/usb/audio.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
26 #include "usbaudio.h"
27 #include "helper.h"
28 #include "card.h"
29 #include "urb.h"
30 #include "pcm.h"
33 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
34 * this will overflow at approx 524 kHz
36 static inline unsigned get_usb_full_speed_rate(unsigned int rate)
38 return ((rate << 13) + 62) / 125;
42 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
43 * this will overflow at approx 4 MHz
45 static inline unsigned get_usb_high_speed_rate(unsigned int rate)
47 return ((rate << 10) + 62) / 125;
51 * unlink active urbs.
53 static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
55 struct snd_usb_audio *chip = subs->stream->chip;
56 unsigned int i;
57 int async;
59 subs->running = 0;
61 if (!force && subs->stream->chip->shutdown) /* to be sure... */
62 return -EBADFD;
64 async = !can_sleep && chip->async_unlink;
66 if (!async && in_interrupt())
67 return 0;
69 for (i = 0; i < subs->nurbs; i++) {
70 if (test_bit(i, &subs->active_mask)) {
71 if (!test_and_set_bit(i, &subs->unlink_mask)) {
72 struct urb *u = subs->dataurb[i].urb;
73 if (async)
74 usb_unlink_urb(u);
75 else
76 usb_kill_urb(u);
80 if (subs->syncpipe) {
81 for (i = 0; i < SYNC_URBS; i++) {
82 if (test_bit(i+16, &subs->active_mask)) {
83 if (!test_and_set_bit(i+16, &subs->unlink_mask)) {
84 struct urb *u = subs->syncurb[i].urb;
85 if (async)
86 usb_unlink_urb(u);
87 else
88 usb_kill_urb(u);
93 return 0;
98 * release a urb data
100 static void release_urb_ctx(struct snd_urb_ctx *u)
102 if (u->urb) {
103 if (u->buffer_size)
104 usb_free_coherent(u->subs->dev, u->buffer_size,
105 u->urb->transfer_buffer,
106 u->urb->transfer_dma);
107 usb_free_urb(u->urb);
108 u->urb = NULL;
113 * wait until all urbs are processed.
115 static int wait_clear_urbs(struct snd_usb_substream *subs)
117 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
118 unsigned int i;
119 int alive;
121 do {
122 alive = 0;
123 for (i = 0; i < subs->nurbs; i++) {
124 if (test_bit(i, &subs->active_mask))
125 alive++;
127 if (subs->syncpipe) {
128 for (i = 0; i < SYNC_URBS; i++) {
129 if (test_bit(i + 16, &subs->active_mask))
130 alive++;
133 if (! alive)
134 break;
135 schedule_timeout_uninterruptible(1);
136 } while (time_before(jiffies, end_time));
137 if (alive)
138 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
139 return 0;
143 * release a substream
145 void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force)
147 int i;
149 /* stop urbs (to be sure) */
150 deactivate_urbs(subs, force, 1);
151 wait_clear_urbs(subs);
153 for (i = 0; i < MAX_URBS; i++)
154 release_urb_ctx(&subs->dataurb[i]);
155 for (i = 0; i < SYNC_URBS; i++)
156 release_urb_ctx(&subs->syncurb[i]);
157 usb_free_coherent(subs->dev, SYNC_URBS * 4,
158 subs->syncbuf, subs->sync_dma);
159 subs->syncbuf = NULL;
160 subs->nurbs = 0;
164 * complete callback from data urb
166 static void snd_complete_urb(struct urb *urb)
168 struct snd_urb_ctx *ctx = urb->context;
169 struct snd_usb_substream *subs = ctx->subs;
170 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
171 int err = 0;
173 if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
174 !subs->running || /* can be stopped during retire callback */
175 (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
176 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
177 clear_bit(ctx->index, &subs->active_mask);
178 if (err < 0) {
179 snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
180 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
187 * complete callback from sync urb
189 static void snd_complete_sync_urb(struct urb *urb)
191 struct snd_urb_ctx *ctx = urb->context;
192 struct snd_usb_substream *subs = ctx->subs;
193 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
194 int err = 0;
196 if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
197 !subs->running || /* can be stopped during retire callback */
198 (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
199 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
200 clear_bit(ctx->index + 16, &subs->active_mask);
201 if (err < 0) {
202 snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
203 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
210 * initialize a substream for plaback/capture
212 int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
213 unsigned int period_bytes,
214 unsigned int rate,
215 unsigned int frame_bits)
217 unsigned int maxsize, i;
218 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
219 unsigned int urb_packs, total_packs, packs_per_ms;
220 struct snd_usb_audio *chip = subs->stream->chip;
222 /* calculate the frequency in 16.16 format */
223 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
224 subs->freqn = get_usb_full_speed_rate(rate);
225 else
226 subs->freqn = get_usb_high_speed_rate(rate);
227 subs->freqm = subs->freqn;
228 /* calculate max. frequency */
229 if (subs->maxpacksize) {
230 /* whatever fits into a max. size packet */
231 maxsize = subs->maxpacksize;
232 subs->freqmax = (maxsize / (frame_bits >> 3))
233 << (16 - subs->datainterval);
234 } else {
235 /* no max. packet size: just take 25% higher than nominal */
236 subs->freqmax = subs->freqn + (subs->freqn >> 2);
237 maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
238 >> (16 - subs->datainterval);
240 subs->phase = 0;
242 if (subs->fill_max)
243 subs->curpacksize = subs->maxpacksize;
244 else
245 subs->curpacksize = maxsize;
247 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
248 packs_per_ms = 8 >> subs->datainterval;
249 else
250 packs_per_ms = 1;
252 if (is_playback) {
253 urb_packs = max(chip->nrpacks, 1);
254 urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
255 } else
256 urb_packs = 1;
257 urb_packs *= packs_per_ms;
258 if (subs->syncpipe)
259 urb_packs = min(urb_packs, 1U << subs->syncinterval);
261 /* decide how many packets to be used */
262 if (is_playback) {
263 unsigned int minsize, maxpacks;
264 /* determine how small a packet can be */
265 minsize = (subs->freqn >> (16 - subs->datainterval))
266 * (frame_bits >> 3);
267 /* with sync from device, assume it can be 12% lower */
268 if (subs->syncpipe)
269 minsize -= minsize >> 3;
270 minsize = max(minsize, 1u);
271 total_packs = (period_bytes + minsize - 1) / minsize;
272 /* we need at least two URBs for queueing */
273 if (total_packs < 2) {
274 total_packs = 2;
275 } else {
276 /* and we don't want too long a queue either */
277 maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
278 total_packs = min(total_packs, maxpacks);
280 } else {
281 while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
282 urb_packs >>= 1;
283 total_packs = MAX_URBS * urb_packs;
285 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
286 if (subs->nurbs > MAX_URBS) {
287 /* too much... */
288 subs->nurbs = MAX_URBS;
289 total_packs = MAX_URBS * urb_packs;
290 } else if (subs->nurbs < 2) {
291 /* too little - we need at least two packets
292 * to ensure contiguous playback/capture
294 subs->nurbs = 2;
297 /* allocate and initialize data urbs */
298 for (i = 0; i < subs->nurbs; i++) {
299 struct snd_urb_ctx *u = &subs->dataurb[i];
300 u->index = i;
301 u->subs = subs;
302 u->packets = (i + 1) * total_packs / subs->nurbs
303 - i * total_packs / subs->nurbs;
304 u->buffer_size = maxsize * u->packets;
305 if (subs->fmt_type == UAC_FORMAT_TYPE_II)
306 u->packets++; /* for transfer delimiter */
307 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
308 if (!u->urb)
309 goto out_of_memory;
310 u->urb->transfer_buffer =
311 usb_alloc_coherent(subs->dev, u->buffer_size,
312 GFP_KERNEL, &u->urb->transfer_dma);
313 if (!u->urb->transfer_buffer)
314 goto out_of_memory;
315 u->urb->pipe = subs->datapipe;
316 u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
317 u->urb->interval = 1 << subs->datainterval;
318 u->urb->context = u;
319 u->urb->complete = snd_complete_urb;
322 if (subs->syncpipe) {
323 /* allocate and initialize sync urbs */
324 subs->syncbuf = usb_alloc_coherent(subs->dev, SYNC_URBS * 4,
325 GFP_KERNEL, &subs->sync_dma);
326 if (!subs->syncbuf)
327 goto out_of_memory;
328 for (i = 0; i < SYNC_URBS; i++) {
329 struct snd_urb_ctx *u = &subs->syncurb[i];
330 u->index = i;
331 u->subs = subs;
332 u->packets = 1;
333 u->urb = usb_alloc_urb(1, GFP_KERNEL);
334 if (!u->urb)
335 goto out_of_memory;
336 u->urb->transfer_buffer = subs->syncbuf + i * 4;
337 u->urb->transfer_dma = subs->sync_dma + i * 4;
338 u->urb->transfer_buffer_length = 4;
339 u->urb->pipe = subs->syncpipe;
340 u->urb->transfer_flags = URB_ISO_ASAP |
341 URB_NO_TRANSFER_DMA_MAP;
342 u->urb->number_of_packets = 1;
343 u->urb->interval = 1 << subs->syncinterval;
344 u->urb->context = u;
345 u->urb->complete = snd_complete_sync_urb;
348 return 0;
350 out_of_memory:
351 snd_usb_release_substream_urbs(subs, 0);
352 return -ENOMEM;
356 * prepare urb for full speed capture sync pipe
358 * fill the length and offset of each urb descriptor.
359 * the fixed 10.14 frequency is passed through the pipe.
361 static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
362 struct snd_pcm_runtime *runtime,
363 struct urb *urb)
365 unsigned char *cp = urb->transfer_buffer;
366 struct snd_urb_ctx *ctx = urb->context;
368 urb->dev = ctx->subs->dev; /* we need to set this at each time */
369 urb->iso_frame_desc[0].length = 3;
370 urb->iso_frame_desc[0].offset = 0;
371 cp[0] = subs->freqn >> 2;
372 cp[1] = subs->freqn >> 10;
373 cp[2] = subs->freqn >> 18;
374 return 0;
378 * prepare urb for high speed capture sync pipe
380 * fill the length and offset of each urb descriptor.
381 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
383 static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
384 struct snd_pcm_runtime *runtime,
385 struct urb *urb)
387 unsigned char *cp = urb->transfer_buffer;
388 struct snd_urb_ctx *ctx = urb->context;
390 urb->dev = ctx->subs->dev; /* we need to set this at each time */
391 urb->iso_frame_desc[0].length = 4;
392 urb->iso_frame_desc[0].offset = 0;
393 cp[0] = subs->freqn;
394 cp[1] = subs->freqn >> 8;
395 cp[2] = subs->freqn >> 16;
396 cp[3] = subs->freqn >> 24;
397 return 0;
401 * process after capture sync complete
402 * - nothing to do
404 static int retire_capture_sync_urb(struct snd_usb_substream *subs,
405 struct snd_pcm_runtime *runtime,
406 struct urb *urb)
408 return 0;
412 * prepare urb for capture data pipe
414 * fill the offset and length of each descriptor.
416 * we use a temporary buffer to write the captured data.
417 * since the length of written data is determined by host, we cannot
418 * write onto the pcm buffer directly... the data is thus copied
419 * later at complete callback to the global buffer.
421 static int prepare_capture_urb(struct snd_usb_substream *subs,
422 struct snd_pcm_runtime *runtime,
423 struct urb *urb)
425 int i, offs;
426 struct snd_urb_ctx *ctx = urb->context;
428 offs = 0;
429 urb->dev = ctx->subs->dev; /* we need to set this at each time */
430 for (i = 0; i < ctx->packets; i++) {
431 urb->iso_frame_desc[i].offset = offs;
432 urb->iso_frame_desc[i].length = subs->curpacksize;
433 offs += subs->curpacksize;
435 urb->transfer_buffer_length = offs;
436 urb->number_of_packets = ctx->packets;
437 return 0;
441 * process after capture complete
443 * copy the data from each desctiptor to the pcm buffer, and
444 * update the current position.
446 static int retire_capture_urb(struct snd_usb_substream *subs,
447 struct snd_pcm_runtime *runtime,
448 struct urb *urb)
450 unsigned long flags;
451 unsigned char *cp;
452 int i;
453 unsigned int stride, frames, bytes, oldptr;
454 int period_elapsed = 0;
456 stride = runtime->frame_bits >> 3;
458 for (i = 0; i < urb->number_of_packets; i++) {
459 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
460 if (urb->iso_frame_desc[i].status) {
461 snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
462 // continue;
464 bytes = urb->iso_frame_desc[i].actual_length;
465 frames = bytes / stride;
466 if (!subs->txfr_quirk)
467 bytes = frames * stride;
468 if (bytes % (runtime->sample_bits >> 3) != 0) {
469 #ifdef CONFIG_SND_DEBUG_VERBOSE
470 int oldbytes = bytes;
471 #endif
472 bytes = frames * stride;
473 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
474 oldbytes, bytes);
476 /* update the current pointer */
477 spin_lock_irqsave(&subs->lock, flags);
478 oldptr = subs->hwptr_done;
479 subs->hwptr_done += bytes;
480 if (subs->hwptr_done >= runtime->buffer_size * stride)
481 subs->hwptr_done -= runtime->buffer_size * stride;
482 frames = (bytes + (oldptr % stride)) / stride;
483 subs->transfer_done += frames;
484 if (subs->transfer_done >= runtime->period_size) {
485 subs->transfer_done -= runtime->period_size;
486 period_elapsed = 1;
488 spin_unlock_irqrestore(&subs->lock, flags);
489 /* copy a data chunk */
490 if (oldptr + bytes > runtime->buffer_size * stride) {
491 unsigned int bytes1 =
492 runtime->buffer_size * stride - oldptr;
493 memcpy(runtime->dma_area + oldptr, cp, bytes1);
494 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
495 } else {
496 memcpy(runtime->dma_area + oldptr, cp, bytes);
499 if (period_elapsed)
500 snd_pcm_period_elapsed(subs->pcm_substream);
501 return 0;
505 * Process after capture complete when paused. Nothing to do.
507 static int retire_paused_capture_urb(struct snd_usb_substream *subs,
508 struct snd_pcm_runtime *runtime,
509 struct urb *urb)
511 return 0;
516 * prepare urb for full speed playback sync pipe
518 * set up the offset and length to receive the current frequency.
521 static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
522 struct snd_pcm_runtime *runtime,
523 struct urb *urb)
525 struct snd_urb_ctx *ctx = urb->context;
527 urb->dev = ctx->subs->dev; /* we need to set this at each time */
528 urb->iso_frame_desc[0].length = 3;
529 urb->iso_frame_desc[0].offset = 0;
530 return 0;
534 * prepare urb for high speed playback sync pipe
536 * set up the offset and length to receive the current frequency.
539 static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs,
540 struct snd_pcm_runtime *runtime,
541 struct urb *urb)
543 struct snd_urb_ctx *ctx = urb->context;
545 urb->dev = ctx->subs->dev; /* we need to set this at each time */
546 urb->iso_frame_desc[0].length = 4;
547 urb->iso_frame_desc[0].offset = 0;
548 return 0;
552 * process after full speed playback sync complete
554 * retrieve the current 10.14 frequency from pipe, and set it.
555 * the value is referred in prepare_playback_urb().
557 static int retire_playback_sync_urb(struct snd_usb_substream *subs,
558 struct snd_pcm_runtime *runtime,
559 struct urb *urb)
561 unsigned int f;
562 unsigned long flags;
564 if (urb->iso_frame_desc[0].status == 0 &&
565 urb->iso_frame_desc[0].actual_length == 3) {
566 f = combine_triple((u8*)urb->transfer_buffer) << 2;
567 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
568 spin_lock_irqsave(&subs->lock, flags);
569 subs->freqm = f;
570 spin_unlock_irqrestore(&subs->lock, flags);
574 return 0;
578 * process after high speed playback sync complete
580 * retrieve the current 12.13 frequency from pipe, and set it.
581 * the value is referred in prepare_playback_urb().
583 static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs,
584 struct snd_pcm_runtime *runtime,
585 struct urb *urb)
587 unsigned int f;
588 unsigned long flags;
590 if (urb->iso_frame_desc[0].status == 0 &&
591 urb->iso_frame_desc[0].actual_length == 4) {
592 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
593 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
594 spin_lock_irqsave(&subs->lock, flags);
595 subs->freqm = f;
596 spin_unlock_irqrestore(&subs->lock, flags);
600 return 0;
604 * process after E-Mu 0202/0404/Tracker Pre high speed playback sync complete
606 * These devices return the number of samples per packet instead of the number
607 * of samples per microframe.
609 static int retire_playback_sync_urb_hs_emu(struct snd_usb_substream *subs,
610 struct snd_pcm_runtime *runtime,
611 struct urb *urb)
613 unsigned int f;
614 unsigned long flags;
616 if (urb->iso_frame_desc[0].status == 0 &&
617 urb->iso_frame_desc[0].actual_length == 4) {
618 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
619 f >>= subs->datainterval;
620 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
621 spin_lock_irqsave(&subs->lock, flags);
622 subs->freqm = f;
623 spin_unlock_irqrestore(&subs->lock, flags);
627 return 0;
630 /* determine the number of frames in the next packet */
631 static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
633 if (subs->fill_max)
634 return subs->maxframesize;
635 else {
636 subs->phase = (subs->phase & 0xffff)
637 + (subs->freqm << subs->datainterval);
638 return min(subs->phase >> 16, subs->maxframesize);
643 * Prepare urb for streaming before playback starts or when paused.
645 * We don't have any data, so we send silence.
647 static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
648 struct snd_pcm_runtime *runtime,
649 struct urb *urb)
651 unsigned int i, offs, counts;
652 struct snd_urb_ctx *ctx = urb->context;
653 int stride = runtime->frame_bits >> 3;
655 offs = 0;
656 urb->dev = ctx->subs->dev;
657 for (i = 0; i < ctx->packets; ++i) {
658 counts = snd_usb_audio_next_packet_size(subs);
659 urb->iso_frame_desc[i].offset = offs * stride;
660 urb->iso_frame_desc[i].length = counts * stride;
661 offs += counts;
663 urb->number_of_packets = ctx->packets;
664 urb->transfer_buffer_length = offs * stride;
665 memset(urb->transfer_buffer,
666 runtime->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
667 offs * stride);
668 return 0;
672 * prepare urb for playback data pipe
674 * Since a URB can handle only a single linear buffer, we must use double
675 * buffering when the data to be transferred overflows the buffer boundary.
676 * To avoid inconsistencies when updating hwptr_done, we use double buffering
677 * for all URBs.
679 static int prepare_playback_urb(struct snd_usb_substream *subs,
680 struct snd_pcm_runtime *runtime,
681 struct urb *urb)
683 int i, stride;
684 unsigned int counts, frames, bytes;
685 unsigned long flags;
686 int period_elapsed = 0;
687 struct snd_urb_ctx *ctx = urb->context;
689 stride = runtime->frame_bits >> 3;
691 frames = 0;
692 urb->dev = ctx->subs->dev; /* we need to set this at each time */
693 urb->number_of_packets = 0;
694 spin_lock_irqsave(&subs->lock, flags);
695 for (i = 0; i < ctx->packets; i++) {
696 counts = snd_usb_audio_next_packet_size(subs);
697 /* set up descriptor */
698 urb->iso_frame_desc[i].offset = frames * stride;
699 urb->iso_frame_desc[i].length = counts * stride;
700 frames += counts;
701 urb->number_of_packets++;
702 subs->transfer_done += counts;
703 if (subs->transfer_done >= runtime->period_size) {
704 subs->transfer_done -= runtime->period_size;
705 period_elapsed = 1;
706 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
707 if (subs->transfer_done > 0) {
708 frames -= subs->transfer_done;
709 counts -= subs->transfer_done;
710 urb->iso_frame_desc[i].length =
711 counts * stride;
712 subs->transfer_done = 0;
714 i++;
715 if (i < ctx->packets) {
716 /* add a transfer delimiter */
717 urb->iso_frame_desc[i].offset =
718 frames * stride;
719 urb->iso_frame_desc[i].length = 0;
720 urb->number_of_packets++;
722 break;
725 if (period_elapsed) /* finish at the period boundary */
726 break;
728 bytes = frames * stride;
729 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
730 /* err, the transferred area goes over buffer boundary. */
731 unsigned int bytes1 =
732 runtime->buffer_size * stride - subs->hwptr_done;
733 memcpy(urb->transfer_buffer,
734 runtime->dma_area + subs->hwptr_done, bytes1);
735 memcpy(urb->transfer_buffer + bytes1,
736 runtime->dma_area, bytes - bytes1);
737 } else {
738 memcpy(urb->transfer_buffer,
739 runtime->dma_area + subs->hwptr_done, bytes);
741 subs->hwptr_done += bytes;
742 if (subs->hwptr_done >= runtime->buffer_size * stride)
743 subs->hwptr_done -= runtime->buffer_size * stride;
744 runtime->delay += frames;
745 spin_unlock_irqrestore(&subs->lock, flags);
746 urb->transfer_buffer_length = bytes;
747 if (period_elapsed)
748 snd_pcm_period_elapsed(subs->pcm_substream);
749 return 0;
753 * process after playback data complete
754 * - decrease the delay count again
756 static int retire_playback_urb(struct snd_usb_substream *subs,
757 struct snd_pcm_runtime *runtime,
758 struct urb *urb)
760 unsigned long flags;
761 int stride = runtime->frame_bits >> 3;
762 int processed = urb->transfer_buffer_length / stride;
764 spin_lock_irqsave(&subs->lock, flags);
765 if (processed > runtime->delay)
766 runtime->delay = 0;
767 else
768 runtime->delay -= processed;
769 spin_unlock_irqrestore(&subs->lock, flags);
770 return 0;
773 static const char *usb_error_string(int err)
775 switch (err) {
776 case -ENODEV:
777 return "no device";
778 case -ENOENT:
779 return "endpoint not enabled";
780 case -EPIPE:
781 return "endpoint stalled";
782 case -ENOSPC:
783 return "not enough bandwidth";
784 case -ESHUTDOWN:
785 return "device disabled";
786 case -EHOSTUNREACH:
787 return "device suspended";
788 case -EINVAL:
789 case -EAGAIN:
790 case -EFBIG:
791 case -EMSGSIZE:
792 return "internal error";
793 default:
794 return "unknown error";
799 * set up and start data/sync urbs
801 static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
803 unsigned int i;
804 int err;
806 if (subs->stream->chip->shutdown)
807 return -EBADFD;
809 for (i = 0; i < subs->nurbs; i++) {
810 if (snd_BUG_ON(!subs->dataurb[i].urb))
811 return -EINVAL;
812 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
813 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
814 goto __error;
817 if (subs->syncpipe) {
818 for (i = 0; i < SYNC_URBS; i++) {
819 if (snd_BUG_ON(!subs->syncurb[i].urb))
820 return -EINVAL;
821 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
822 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
823 goto __error;
828 subs->active_mask = 0;
829 subs->unlink_mask = 0;
830 subs->running = 1;
831 for (i = 0; i < subs->nurbs; i++) {
832 err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
833 if (err < 0) {
834 snd_printk(KERN_ERR "cannot submit datapipe "
835 "for urb %d, error %d: %s\n",
836 i, err, usb_error_string(err));
837 goto __error;
839 set_bit(i, &subs->active_mask);
841 if (subs->syncpipe) {
842 for (i = 0; i < SYNC_URBS; i++) {
843 err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
844 if (err < 0) {
845 snd_printk(KERN_ERR "cannot submit syncpipe "
846 "for urb %d, error %d: %s\n",
847 i, err, usb_error_string(err));
848 goto __error;
850 set_bit(i + 16, &subs->active_mask);
853 return 0;
855 __error:
856 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
857 deactivate_urbs(subs, 0, 0);
858 return -EPIPE;
864 static struct snd_urb_ops audio_urb_ops[2] = {
866 .prepare = prepare_nodata_playback_urb,
867 .retire = retire_playback_urb,
868 .prepare_sync = prepare_playback_sync_urb,
869 .retire_sync = retire_playback_sync_urb,
872 .prepare = prepare_capture_urb,
873 .retire = retire_capture_urb,
874 .prepare_sync = prepare_capture_sync_urb,
875 .retire_sync = retire_capture_sync_urb,
879 static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
881 .prepare = prepare_nodata_playback_urb,
882 .retire = retire_playback_urb,
883 .prepare_sync = prepare_playback_sync_urb_hs,
884 .retire_sync = retire_playback_sync_urb_hs,
887 .prepare = prepare_capture_urb,
888 .retire = retire_capture_urb,
889 .prepare_sync = prepare_capture_sync_urb_hs,
890 .retire_sync = retire_capture_sync_urb,
895 * initialize the substream instance.
898 void snd_usb_init_substream(struct snd_usb_stream *as,
899 int stream, struct audioformat *fp)
901 struct snd_usb_substream *subs = &as->substream[stream];
903 INIT_LIST_HEAD(&subs->fmt_list);
904 spin_lock_init(&subs->lock);
906 subs->stream = as;
907 subs->direction = stream;
908 subs->dev = as->chip->dev;
909 subs->txfr_quirk = as->chip->txfr_quirk;
910 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) {
911 subs->ops = audio_urb_ops[stream];
912 } else {
913 subs->ops = audio_urb_ops_high_speed[stream];
914 switch (as->chip->usb_id) {
915 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
916 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
917 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
918 subs->ops.retire_sync = retire_playback_sync_urb_hs_emu;
919 break;
920 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra 8 */
921 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
922 subs->ops.prepare_sync = prepare_playback_sync_urb;
923 subs->ops.retire_sync = retire_playback_sync_urb;
924 break;
928 snd_usb_set_pcm_ops(as->pcm, stream);
930 list_add_tail(&fp->list, &subs->fmt_list);
931 subs->formats |= fp->formats;
932 subs->endpoint = fp->endpoint;
933 subs->num_formats++;
934 subs->fmt_type = fp->fmt_type;
937 int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd)
939 struct snd_usb_substream *subs = substream->runtime->private_data;
941 switch (cmd) {
942 case SNDRV_PCM_TRIGGER_START:
943 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
944 subs->ops.prepare = prepare_playback_urb;
945 return 0;
946 case SNDRV_PCM_TRIGGER_STOP:
947 return deactivate_urbs(subs, 0, 0);
948 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
949 subs->ops.prepare = prepare_nodata_playback_urb;
950 return 0;
953 return -EINVAL;
956 int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd)
958 struct snd_usb_substream *subs = substream->runtime->private_data;
960 switch (cmd) {
961 case SNDRV_PCM_TRIGGER_START:
962 subs->ops.retire = retire_capture_urb;
963 return start_urbs(subs, substream->runtime);
964 case SNDRV_PCM_TRIGGER_STOP:
965 return deactivate_urbs(subs, 0, 0);
966 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
967 subs->ops.retire = retire_paused_capture_urb;
968 return 0;
969 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
970 subs->ops.retire = retire_capture_urb;
971 return 0;
974 return -EINVAL;
977 int snd_usb_substream_prepare(struct snd_usb_substream *subs,
978 struct snd_pcm_runtime *runtime)
980 /* clear urbs (to be sure) */
981 deactivate_urbs(subs, 0, 1);
982 wait_clear_urbs(subs);
984 /* for playback, submit the URBs now; otherwise, the first hwptr_done
985 * updates for all URBs would happen at the same time when starting */
986 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
987 subs->ops.prepare = prepare_nodata_playback_urb;
988 return start_urbs(subs, runtime);
991 return 0;