blob: e9c339f758616df8353445661be57e9d54010beb [file] [log] [blame]
Daniel Macke5779992010-03-04 19:46:13 +01001/*
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
18#include <linux/init.h>
19#include <linux/usb.h>
20#include <linux/usb/audio.h>
21
22#include <sound/core.h>
23#include <sound/pcm.h>
24
25#include "usbaudio.h"
26#include "helper.h"
27#include "card.h"
28#include "urb.h"
29#include "pcm.h"
30
31/*
32 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
33 * this will overflow at approx 524 kHz
34 */
35static inline unsigned get_usb_full_speed_rate(unsigned int rate)
36{
37 return ((rate << 13) + 62) / 125;
38}
39
40/*
41 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
42 * this will overflow at approx 4 MHz
43 */
44static inline unsigned get_usb_high_speed_rate(unsigned int rate)
45{
46 return ((rate << 10) + 62) / 125;
47}
48
49/*
50 * unlink active urbs.
51 */
52static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
53{
54 struct snd_usb_audio *chip = subs->stream->chip;
55 unsigned int i;
56 int async;
57
58 subs->running = 0;
59
60 if (!force && subs->stream->chip->shutdown) /* to be sure... */
61 return -EBADFD;
62
63 async = !can_sleep && chip->async_unlink;
64
65 if (!async && in_interrupt())
66 return 0;
67
68 for (i = 0; i < subs->nurbs; i++) {
69 if (test_bit(i, &subs->active_mask)) {
70 if (!test_and_set_bit(i, &subs->unlink_mask)) {
71 struct urb *u = subs->dataurb[i].urb;
72 if (async)
73 usb_unlink_urb(u);
74 else
75 usb_kill_urb(u);
76 }
77 }
78 }
79 if (subs->syncpipe) {
80 for (i = 0; i < SYNC_URBS; i++) {
81 if (test_bit(i+16, &subs->active_mask)) {
82 if (!test_and_set_bit(i+16, &subs->unlink_mask)) {
83 struct urb *u = subs->syncurb[i].urb;
84 if (async)
85 usb_unlink_urb(u);
86 else
87 usb_kill_urb(u);
88 }
89 }
90 }
91 }
92 return 0;
93}
94
95
96/*
97 * release a urb data
98 */
99static void release_urb_ctx(struct snd_urb_ctx *u)
100{
101 if (u->urb) {
102 if (u->buffer_size)
103 usb_buffer_free(u->subs->dev, u->buffer_size,
104 u->urb->transfer_buffer,
105 u->urb->transfer_dma);
106 usb_free_urb(u->urb);
107 u->urb = NULL;
108 }
109}
110
111/*
112 * wait until all urbs are processed.
113 */
114static int wait_clear_urbs(struct snd_usb_substream *subs)
115{
116 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
117 unsigned int i;
118 int alive;
119
120 do {
121 alive = 0;
122 for (i = 0; i < subs->nurbs; i++) {
123 if (test_bit(i, &subs->active_mask))
124 alive++;
125 }
126 if (subs->syncpipe) {
127 for (i = 0; i < SYNC_URBS; i++) {
128 if (test_bit(i + 16, &subs->active_mask))
129 alive++;
130 }
131 }
132 if (! alive)
133 break;
134 schedule_timeout_uninterruptible(1);
135 } while (time_before(jiffies, end_time));
136 if (alive)
137 snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
138 return 0;
139}
140
141/*
142 * release a substream
143 */
144void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force)
145{
146 int i;
147
148 /* stop urbs (to be sure) */
149 deactivate_urbs(subs, force, 1);
150 wait_clear_urbs(subs);
151
152 for (i = 0; i < MAX_URBS; i++)
153 release_urb_ctx(&subs->dataurb[i]);
154 for (i = 0; i < SYNC_URBS; i++)
155 release_urb_ctx(&subs->syncurb[i]);
156 usb_buffer_free(subs->dev, SYNC_URBS * 4,
157 subs->syncbuf, subs->sync_dma);
158 subs->syncbuf = NULL;
159 subs->nurbs = 0;
160}
161
162/*
163 * complete callback from data urb
164 */
165static void snd_complete_urb(struct urb *urb)
166{
167 struct snd_urb_ctx *ctx = urb->context;
168 struct snd_usb_substream *subs = ctx->subs;
169 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
170 int err = 0;
171
172 if ((subs->running && subs->ops.retire(subs, substream->runtime, urb)) ||
173 !subs->running || /* can be stopped during retire callback */
174 (err = subs->ops.prepare(subs, substream->runtime, urb)) < 0 ||
175 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
176 clear_bit(ctx->index, &subs->active_mask);
177 if (err < 0) {
178 snd_printd(KERN_ERR "cannot submit urb (err = %d)\n", err);
179 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
180 }
181 }
182}
183
184
185/*
186 * complete callback from sync urb
187 */
188static void snd_complete_sync_urb(struct urb *urb)
189{
190 struct snd_urb_ctx *ctx = urb->context;
191 struct snd_usb_substream *subs = ctx->subs;
192 struct snd_pcm_substream *substream = ctx->subs->pcm_substream;
193 int err = 0;
194
195 if ((subs->running && subs->ops.retire_sync(subs, substream->runtime, urb)) ||
196 !subs->running || /* can be stopped during retire callback */
197 (err = subs->ops.prepare_sync(subs, substream->runtime, urb)) < 0 ||
198 (err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
199 clear_bit(ctx->index + 16, &subs->active_mask);
200 if (err < 0) {
201 snd_printd(KERN_ERR "cannot submit sync urb (err = %d)\n", err);
202 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
203 }
204 }
205}
206
207
208/*
209 * initialize a substream for plaback/capture
210 */
211int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
212 unsigned int period_bytes,
213 unsigned int rate,
214 unsigned int frame_bits)
215{
216 unsigned int maxsize, i;
217 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
218 unsigned int urb_packs, total_packs, packs_per_ms;
219 struct snd_usb_audio *chip = subs->stream->chip;
220
221 /* calculate the frequency in 16.16 format */
222 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL)
223 subs->freqn = get_usb_full_speed_rate(rate);
224 else
225 subs->freqn = get_usb_high_speed_rate(rate);
226 subs->freqm = subs->freqn;
227 /* calculate max. frequency */
228 if (subs->maxpacksize) {
229 /* whatever fits into a max. size packet */
230 maxsize = subs->maxpacksize;
231 subs->freqmax = (maxsize / (frame_bits >> 3))
232 << (16 - subs->datainterval);
233 } else {
234 /* no max. packet size: just take 25% higher than nominal */
235 subs->freqmax = subs->freqn + (subs->freqn >> 2);
236 maxsize = ((subs->freqmax + 0xffff) * (frame_bits >> 3))
237 >> (16 - subs->datainterval);
238 }
239 subs->phase = 0;
240
241 if (subs->fill_max)
242 subs->curpacksize = subs->maxpacksize;
243 else
244 subs->curpacksize = maxsize;
245
246 if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH)
247 packs_per_ms = 8 >> subs->datainterval;
248 else
249 packs_per_ms = 1;
250
251 if (is_playback) {
252 urb_packs = max(chip->nrpacks, 1);
253 urb_packs = min(urb_packs, (unsigned int)MAX_PACKS);
254 } else
255 urb_packs = 1;
256 urb_packs *= packs_per_ms;
257 if (subs->syncpipe)
258 urb_packs = min(urb_packs, 1U << subs->syncinterval);
259
260 /* decide how many packets to be used */
261 if (is_playback) {
262 unsigned int minsize, maxpacks;
263 /* determine how small a packet can be */
264 minsize = (subs->freqn >> (16 - subs->datainterval))
265 * (frame_bits >> 3);
266 /* with sync from device, assume it can be 12% lower */
267 if (subs->syncpipe)
268 minsize -= minsize >> 3;
269 minsize = max(minsize, 1u);
270 total_packs = (period_bytes + minsize - 1) / minsize;
271 /* we need at least two URBs for queueing */
272 if (total_packs < 2) {
273 total_packs = 2;
274 } else {
275 /* and we don't want too long a queue either */
276 maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2);
277 total_packs = min(total_packs, maxpacks);
278 }
279 } else {
280 while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
281 urb_packs >>= 1;
282 total_packs = MAX_URBS * urb_packs;
283 }
284 subs->nurbs = (total_packs + urb_packs - 1) / urb_packs;
285 if (subs->nurbs > MAX_URBS) {
286 /* too much... */
287 subs->nurbs = MAX_URBS;
288 total_packs = MAX_URBS * urb_packs;
289 } else if (subs->nurbs < 2) {
290 /* too little - we need at least two packets
291 * to ensure contiguous playback/capture
292 */
293 subs->nurbs = 2;
294 }
295
296 /* allocate and initialize data urbs */
297 for (i = 0; i < subs->nurbs; i++) {
298 struct snd_urb_ctx *u = &subs->dataurb[i];
299 u->index = i;
300 u->subs = subs;
301 u->packets = (i + 1) * total_packs / subs->nurbs
302 - i * total_packs / subs->nurbs;
303 u->buffer_size = maxsize * u->packets;
304 if (subs->fmt_type == UAC_FORMAT_TYPE_II)
305 u->packets++; /* for transfer delimiter */
306 u->urb = usb_alloc_urb(u->packets, GFP_KERNEL);
307 if (!u->urb)
308 goto out_of_memory;
309 u->urb->transfer_buffer =
310 usb_buffer_alloc(subs->dev, u->buffer_size, GFP_KERNEL,
311 &u->urb->transfer_dma);
312 if (!u->urb->transfer_buffer)
313 goto out_of_memory;
314 u->urb->pipe = subs->datapipe;
315 u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
316 u->urb->interval = 1 << subs->datainterval;
317 u->urb->context = u;
318 u->urb->complete = snd_complete_urb;
319 }
320
321 if (subs->syncpipe) {
322 /* allocate and initialize sync urbs */
323 subs->syncbuf = usb_buffer_alloc(subs->dev, SYNC_URBS * 4,
324 GFP_KERNEL, &subs->sync_dma);
325 if (!subs->syncbuf)
326 goto out_of_memory;
327 for (i = 0; i < SYNC_URBS; i++) {
328 struct snd_urb_ctx *u = &subs->syncurb[i];
329 u->index = i;
330 u->subs = subs;
331 u->packets = 1;
332 u->urb = usb_alloc_urb(1, GFP_KERNEL);
333 if (!u->urb)
334 goto out_of_memory;
335 u->urb->transfer_buffer = subs->syncbuf + i * 4;
336 u->urb->transfer_dma = subs->sync_dma + i * 4;
337 u->urb->transfer_buffer_length = 4;
338 u->urb->pipe = subs->syncpipe;
339 u->urb->transfer_flags = URB_ISO_ASAP |
340 URB_NO_TRANSFER_DMA_MAP;
341 u->urb->number_of_packets = 1;
342 u->urb->interval = 1 << subs->syncinterval;
343 u->urb->context = u;
344 u->urb->complete = snd_complete_sync_urb;
345 }
346 }
347 return 0;
348
349out_of_memory:
350 snd_usb_release_substream_urbs(subs, 0);
351 return -ENOMEM;
352}
353
354/*
355 * prepare urb for full speed capture sync pipe
356 *
357 * fill the length and offset of each urb descriptor.
358 * the fixed 10.14 frequency is passed through the pipe.
359 */
360static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
361 struct snd_pcm_runtime *runtime,
362 struct urb *urb)
363{
364 unsigned char *cp = urb->transfer_buffer;
365 struct snd_urb_ctx *ctx = urb->context;
366
367 urb->dev = ctx->subs->dev; /* we need to set this at each time */
368 urb->iso_frame_desc[0].length = 3;
369 urb->iso_frame_desc[0].offset = 0;
370 cp[0] = subs->freqn >> 2;
371 cp[1] = subs->freqn >> 10;
372 cp[2] = subs->freqn >> 18;
373 return 0;
374}
375
376/*
377 * prepare urb for high speed capture sync pipe
378 *
379 * fill the length and offset of each urb descriptor.
380 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
381 */
382static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
383 struct snd_pcm_runtime *runtime,
384 struct urb *urb)
385{
386 unsigned char *cp = urb->transfer_buffer;
387 struct snd_urb_ctx *ctx = urb->context;
388
389 urb->dev = ctx->subs->dev; /* we need to set this at each time */
390 urb->iso_frame_desc[0].length = 4;
391 urb->iso_frame_desc[0].offset = 0;
392 cp[0] = subs->freqn;
393 cp[1] = subs->freqn >> 8;
394 cp[2] = subs->freqn >> 16;
395 cp[3] = subs->freqn >> 24;
396 return 0;
397}
398
399/*
400 * process after capture sync complete
401 * - nothing to do
402 */
403static int retire_capture_sync_urb(struct snd_usb_substream *subs,
404 struct snd_pcm_runtime *runtime,
405 struct urb *urb)
406{
407 return 0;
408}
409
410/*
411 * prepare urb for capture data pipe
412 *
413 * fill the offset and length of each descriptor.
414 *
415 * we use a temporary buffer to write the captured data.
416 * since the length of written data is determined by host, we cannot
417 * write onto the pcm buffer directly... the data is thus copied
418 * later at complete callback to the global buffer.
419 */
420static int prepare_capture_urb(struct snd_usb_substream *subs,
421 struct snd_pcm_runtime *runtime,
422 struct urb *urb)
423{
424 int i, offs;
425 struct snd_urb_ctx *ctx = urb->context;
426
427 offs = 0;
428 urb->dev = ctx->subs->dev; /* we need to set this at each time */
429 for (i = 0; i < ctx->packets; i++) {
430 urb->iso_frame_desc[i].offset = offs;
431 urb->iso_frame_desc[i].length = subs->curpacksize;
432 offs += subs->curpacksize;
433 }
434 urb->transfer_buffer_length = offs;
435 urb->number_of_packets = ctx->packets;
436 return 0;
437}
438
439/*
440 * process after capture complete
441 *
442 * copy the data from each desctiptor to the pcm buffer, and
443 * update the current position.
444 */
445static int retire_capture_urb(struct snd_usb_substream *subs,
446 struct snd_pcm_runtime *runtime,
447 struct urb *urb)
448{
449 unsigned long flags;
450 unsigned char *cp;
451 int i;
452 unsigned int stride, frames, bytes, oldptr;
453 int period_elapsed = 0;
454
455 stride = runtime->frame_bits >> 3;
456
457 for (i = 0; i < urb->number_of_packets; i++) {
458 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
459 if (urb->iso_frame_desc[i].status) {
460 snd_printd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
461 // continue;
462 }
463 bytes = urb->iso_frame_desc[i].actual_length;
464 frames = bytes / stride;
465 if (!subs->txfr_quirk)
466 bytes = frames * stride;
467 if (bytes % (runtime->sample_bits >> 3) != 0) {
468#ifdef CONFIG_SND_DEBUG_VERBOSE
469 int oldbytes = bytes;
470#endif
471 bytes = frames * stride;
472 snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
473 oldbytes, bytes);
474 }
475 /* update the current pointer */
476 spin_lock_irqsave(&subs->lock, flags);
477 oldptr = subs->hwptr_done;
478 subs->hwptr_done += bytes;
479 if (subs->hwptr_done >= runtime->buffer_size * stride)
480 subs->hwptr_done -= runtime->buffer_size * stride;
481 frames = (bytes + (oldptr % stride)) / stride;
482 subs->transfer_done += frames;
483 if (subs->transfer_done >= runtime->period_size) {
484 subs->transfer_done -= runtime->period_size;
485 period_elapsed = 1;
486 }
487 spin_unlock_irqrestore(&subs->lock, flags);
488 /* copy a data chunk */
489 if (oldptr + bytes > runtime->buffer_size * stride) {
490 unsigned int bytes1 =
491 runtime->buffer_size * stride - oldptr;
492 memcpy(runtime->dma_area + oldptr, cp, bytes1);
493 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
494 } else {
495 memcpy(runtime->dma_area + oldptr, cp, bytes);
496 }
497 }
498 if (period_elapsed)
499 snd_pcm_period_elapsed(subs->pcm_substream);
500 return 0;
501}
502
503/*
504 * Process after capture complete when paused. Nothing to do.
505 */
506static int retire_paused_capture_urb(struct snd_usb_substream *subs,
507 struct snd_pcm_runtime *runtime,
508 struct urb *urb)
509{
510 return 0;
511}
512
513
514/*
515 * prepare urb for full speed playback sync pipe
516 *
517 * set up the offset and length to receive the current frequency.
518 */
519
520static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
521 struct snd_pcm_runtime *runtime,
522 struct urb *urb)
523{
524 struct snd_urb_ctx *ctx = urb->context;
525
526 urb->dev = ctx->subs->dev; /* we need to set this at each time */
527 urb->iso_frame_desc[0].length = 3;
528 urb->iso_frame_desc[0].offset = 0;
529 return 0;
530}
531
532/*
533 * prepare urb for high speed playback sync pipe
534 *
535 * set up the offset and length to receive the current frequency.
536 */
537
538static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs,
539 struct snd_pcm_runtime *runtime,
540 struct urb *urb)
541{
542 struct snd_urb_ctx *ctx = urb->context;
543
544 urb->dev = ctx->subs->dev; /* we need to set this at each time */
545 urb->iso_frame_desc[0].length = 4;
546 urb->iso_frame_desc[0].offset = 0;
547 return 0;
548}
549
550/*
551 * process after full speed playback sync complete
552 *
553 * retrieve the current 10.14 frequency from pipe, and set it.
554 * the value is referred in prepare_playback_urb().
555 */
556static int retire_playback_sync_urb(struct snd_usb_substream *subs,
557 struct snd_pcm_runtime *runtime,
558 struct urb *urb)
559{
560 unsigned int f;
561 unsigned long flags;
562
563 if (urb->iso_frame_desc[0].status == 0 &&
564 urb->iso_frame_desc[0].actual_length == 3) {
565 f = combine_triple((u8*)urb->transfer_buffer) << 2;
566 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
567 spin_lock_irqsave(&subs->lock, flags);
568 subs->freqm = f;
569 spin_unlock_irqrestore(&subs->lock, flags);
570 }
571 }
572
573 return 0;
574}
575
576/*
577 * process after high speed playback sync complete
578 *
579 * retrieve the current 12.13 frequency from pipe, and set it.
580 * the value is referred in prepare_playback_urb().
581 */
582static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs,
583 struct snd_pcm_runtime *runtime,
584 struct urb *urb)
585{
586 unsigned int f;
587 unsigned long flags;
588
589 if (urb->iso_frame_desc[0].status == 0 &&
590 urb->iso_frame_desc[0].actual_length == 4) {
591 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
592 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
593 spin_lock_irqsave(&subs->lock, flags);
594 subs->freqm = f;
595 spin_unlock_irqrestore(&subs->lock, flags);
596 }
597 }
598
599 return 0;
600}
601
602/*
603 * process after E-Mu 0202/0404/Tracker Pre high speed playback sync complete
604 *
605 * These devices return the number of samples per packet instead of the number
606 * of samples per microframe.
607 */
608static int retire_playback_sync_urb_hs_emu(struct snd_usb_substream *subs,
609 struct snd_pcm_runtime *runtime,
610 struct urb *urb)
611{
612 unsigned int f;
613 unsigned long flags;
614
615 if (urb->iso_frame_desc[0].status == 0 &&
616 urb->iso_frame_desc[0].actual_length == 4) {
617 f = combine_quad((u8*)urb->transfer_buffer) & 0x0fffffff;
618 f >>= subs->datainterval;
619 if (f >= subs->freqn - subs->freqn / 8 && f <= subs->freqmax) {
620 spin_lock_irqsave(&subs->lock, flags);
621 subs->freqm = f;
622 spin_unlock_irqrestore(&subs->lock, flags);
623 }
624 }
625
626 return 0;
627}
628
629/* determine the number of frames in the next packet */
630static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
631{
632 if (subs->fill_max)
633 return subs->maxframesize;
634 else {
635 subs->phase = (subs->phase & 0xffff)
636 + (subs->freqm << subs->datainterval);
637 return min(subs->phase >> 16, subs->maxframesize);
638 }
639}
640
641/*
642 * Prepare urb for streaming before playback starts or when paused.
643 *
644 * We don't have any data, so we send silence.
645 */
646static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
647 struct snd_pcm_runtime *runtime,
648 struct urb *urb)
649{
650 unsigned int i, offs, counts;
651 struct snd_urb_ctx *ctx = urb->context;
652 int stride = runtime->frame_bits >> 3;
653
654 offs = 0;
655 urb->dev = ctx->subs->dev;
656 for (i = 0; i < ctx->packets; ++i) {
657 counts = snd_usb_audio_next_packet_size(subs);
658 urb->iso_frame_desc[i].offset = offs * stride;
659 urb->iso_frame_desc[i].length = counts * stride;
660 offs += counts;
661 }
662 urb->number_of_packets = ctx->packets;
663 urb->transfer_buffer_length = offs * stride;
664 memset(urb->transfer_buffer,
665 subs->cur_audiofmt->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
666 offs * stride);
667 return 0;
668}
669
670/*
671 * prepare urb for playback data pipe
672 *
673 * Since a URB can handle only a single linear buffer, we must use double
674 * buffering when the data to be transferred overflows the buffer boundary.
675 * To avoid inconsistencies when updating hwptr_done, we use double buffering
676 * for all URBs.
677 */
678static int prepare_playback_urb(struct snd_usb_substream *subs,
679 struct snd_pcm_runtime *runtime,
680 struct urb *urb)
681{
682 int i, stride;
683 unsigned int counts, frames, bytes;
684 unsigned long flags;
685 int period_elapsed = 0;
686 struct snd_urb_ctx *ctx = urb->context;
687
688 stride = runtime->frame_bits >> 3;
689
690 frames = 0;
691 urb->dev = ctx->subs->dev; /* we need to set this at each time */
692 urb->number_of_packets = 0;
693 spin_lock_irqsave(&subs->lock, flags);
694 for (i = 0; i < ctx->packets; i++) {
695 counts = snd_usb_audio_next_packet_size(subs);
696 /* set up descriptor */
697 urb->iso_frame_desc[i].offset = frames * stride;
698 urb->iso_frame_desc[i].length = counts * stride;
699 frames += counts;
700 urb->number_of_packets++;
701 subs->transfer_done += counts;
702 if (subs->transfer_done >= runtime->period_size) {
703 subs->transfer_done -= runtime->period_size;
704 period_elapsed = 1;
705 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
706 if (subs->transfer_done > 0) {
707 /* FIXME: fill-max mode is not
708 * supported yet */
709 frames -= subs->transfer_done;
710 counts -= subs->transfer_done;
711 urb->iso_frame_desc[i].length =
712 counts * stride;
713 subs->transfer_done = 0;
714 }
715 i++;
716 if (i < ctx->packets) {
717 /* add a transfer delimiter */
718 urb->iso_frame_desc[i].offset =
719 frames * stride;
720 urb->iso_frame_desc[i].length = 0;
721 urb->number_of_packets++;
722 }
723 break;
724 }
725 }
726 if (period_elapsed) /* finish at the period boundary */
727 break;
728 }
729 bytes = frames * stride;
730 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
731 /* err, the transferred area goes over buffer boundary. */
732 unsigned int bytes1 =
733 runtime->buffer_size * stride - subs->hwptr_done;
734 memcpy(urb->transfer_buffer,
735 runtime->dma_area + subs->hwptr_done, bytes1);
736 memcpy(urb->transfer_buffer + bytes1,
737 runtime->dma_area, bytes - bytes1);
738 } else {
739 memcpy(urb->transfer_buffer,
740 runtime->dma_area + subs->hwptr_done, bytes);
741 }
742 subs->hwptr_done += bytes;
743 if (subs->hwptr_done >= runtime->buffer_size * stride)
744 subs->hwptr_done -= runtime->buffer_size * stride;
745 runtime->delay += frames;
746 spin_unlock_irqrestore(&subs->lock, flags);
747 urb->transfer_buffer_length = bytes;
748 if (period_elapsed)
749 snd_pcm_period_elapsed(subs->pcm_substream);
750 return 0;
751}
752
753/*
754 * process after playback data complete
755 * - decrease the delay count again
756 */
757static int retire_playback_urb(struct snd_usb_substream *subs,
758 struct snd_pcm_runtime *runtime,
759 struct urb *urb)
760{
761 unsigned long flags;
762 int stride = runtime->frame_bits >> 3;
763 int processed = urb->transfer_buffer_length / stride;
764
765 spin_lock_irqsave(&subs->lock, flags);
766 if (processed > runtime->delay)
767 runtime->delay = 0;
768 else
769 runtime->delay -= processed;
770 spin_unlock_irqrestore(&subs->lock, flags);
771 return 0;
772}
773
774static const char *usb_error_string(int err)
775{
776 switch (err) {
777 case -ENODEV:
778 return "no device";
779 case -ENOENT:
780 return "endpoint not enabled";
781 case -EPIPE:
782 return "endpoint stalled";
783 case -ENOSPC:
784 return "not enough bandwidth";
785 case -ESHUTDOWN:
786 return "device disabled";
787 case -EHOSTUNREACH:
788 return "device suspended";
789 case -EINVAL:
790 case -EAGAIN:
791 case -EFBIG:
792 case -EMSGSIZE:
793 return "internal error";
794 default:
795 return "unknown error";
796 }
797}
798
799/*
800 * set up and start data/sync urbs
801 */
802static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
803{
804 unsigned int i;
805 int err;
806
807 if (subs->stream->chip->shutdown)
808 return -EBADFD;
809
810 for (i = 0; i < subs->nurbs; i++) {
811 if (snd_BUG_ON(!subs->dataurb[i].urb))
812 return -EINVAL;
813 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
814 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
815 goto __error;
816 }
817 }
818 if (subs->syncpipe) {
819 for (i = 0; i < SYNC_URBS; i++) {
820 if (snd_BUG_ON(!subs->syncurb[i].urb))
821 return -EINVAL;
822 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
823 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
824 goto __error;
825 }
826 }
827 }
828
829 subs->active_mask = 0;
830 subs->unlink_mask = 0;
831 subs->running = 1;
832 for (i = 0; i < subs->nurbs; i++) {
833 err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
834 if (err < 0) {
835 snd_printk(KERN_ERR "cannot submit datapipe "
836 "for urb %d, error %d: %s\n",
837 i, err, usb_error_string(err));
838 goto __error;
839 }
840 set_bit(i, &subs->active_mask);
841 }
842 if (subs->syncpipe) {
843 for (i = 0; i < SYNC_URBS; i++) {
844 err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
845 if (err < 0) {
846 snd_printk(KERN_ERR "cannot submit syncpipe "
847 "for urb %d, error %d: %s\n",
848 i, err, usb_error_string(err));
849 goto __error;
850 }
851 set_bit(i + 16, &subs->active_mask);
852 }
853 }
854 return 0;
855
856 __error:
857 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
858 deactivate_urbs(subs, 0, 0);
859 return -EPIPE;
860}
861
862
863/*
864 */
865static struct snd_urb_ops audio_urb_ops[2] = {
866 {
867 .prepare = prepare_nodata_playback_urb,
868 .retire = retire_playback_urb,
869 .prepare_sync = prepare_playback_sync_urb,
870 .retire_sync = retire_playback_sync_urb,
871 },
872 {
873 .prepare = prepare_capture_urb,
874 .retire = retire_capture_urb,
875 .prepare_sync = prepare_capture_sync_urb,
876 .retire_sync = retire_capture_sync_urb,
877 },
878};
879
880static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
881 {
882 .prepare = prepare_nodata_playback_urb,
883 .retire = retire_playback_urb,
884 .prepare_sync = prepare_playback_sync_urb_hs,
885 .retire_sync = retire_playback_sync_urb_hs,
886 },
887 {
888 .prepare = prepare_capture_urb,
889 .retire = retire_capture_urb,
890 .prepare_sync = prepare_capture_sync_urb_hs,
891 .retire_sync = retire_capture_sync_urb,
892 },
893};
894
895/*
896 * initialize the substream instance.
897 */
898
899void snd_usb_init_substream(struct snd_usb_stream *as,
900 int stream, struct audioformat *fp)
901{
902 struct snd_usb_substream *subs = &as->substream[stream];
903
904 INIT_LIST_HEAD(&subs->fmt_list);
905 spin_lock_init(&subs->lock);
906
907 subs->stream = as;
908 subs->direction = stream;
909 subs->dev = as->chip->dev;
910 subs->txfr_quirk = as->chip->txfr_quirk;
911 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) {
912 subs->ops = audio_urb_ops[stream];
913 } else {
914 subs->ops = audio_urb_ops_high_speed[stream];
915 switch (as->chip->usb_id) {
916 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
917 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
918 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
919 subs->ops.retire_sync = retire_playback_sync_urb_hs_emu;
920 break;
921 }
922 }
923
924 snd_usb_set_pcm_ops(as->pcm, stream);
925
926 list_add_tail(&fp->list, &subs->fmt_list);
927 subs->formats |= 1ULL << fp->format;
928 subs->endpoint = fp->endpoint;
929 subs->num_formats++;
930 subs->fmt_type = fp->fmt_type;
931}
932
933int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd)
934{
935 struct snd_usb_substream *subs = substream->runtime->private_data;
936
937 switch (cmd) {
938 case SNDRV_PCM_TRIGGER_START:
939 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
940 subs->ops.prepare = prepare_playback_urb;
941 return 0;
942 case SNDRV_PCM_TRIGGER_STOP:
943 return deactivate_urbs(subs, 0, 0);
944 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
945 subs->ops.prepare = prepare_nodata_playback_urb;
946 return 0;
947 }
948
949 return -EINVAL;
950}
951
952int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd)
953{
954 struct snd_usb_substream *subs = substream->runtime->private_data;
955
956 switch (cmd) {
957 case SNDRV_PCM_TRIGGER_START:
958 subs->ops.retire = retire_capture_urb;
959 return start_urbs(subs, substream->runtime);
960 case SNDRV_PCM_TRIGGER_STOP:
961 return deactivate_urbs(subs, 0, 0);
962 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
963 subs->ops.retire = retire_paused_capture_urb;
964 return 0;
965 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
966 subs->ops.retire = retire_capture_urb;
967 return 0;
968 }
969
970 return -EINVAL;
971}
972
973int snd_usb_substream_prepare(struct snd_usb_substream *subs,
974 struct snd_pcm_runtime *runtime)
975{
976 /* clear urbs (to be sure) */
977 deactivate_urbs(subs, 0, 1);
978 wait_clear_urbs(subs);
979
980 /* for playback, submit the URBs now; otherwise, the first hwptr_done
981 * updates for all URBs would happen at the same time when starting */
982 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
983 subs->ops.prepare = prepare_nodata_playback_urb;
984 return start_urbs(subs, runtime);
985 }
986
987 return 0;
988}
989