blob: 8deeaad10f10caa4827288cc41230ce1cc6eb77e [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
Tejun Heo7b7b9042010-03-30 02:52:29 +090018#include <linux/gfp.h>
Daniel Macke5779992010-03-04 19:46:13 +010019#include <linux/init.h>
20#include <linux/usb.h>
21#include <linux/usb/audio.h>
22
23#include <sound/core.h>
24#include <sound/pcm.h>
25
26#include "usbaudio.h"
27#include "helper.h"
28#include "card.h"
29#include "urb.h"
30#include "pcm.h"
31
32/*
33 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
34 * this will overflow at approx 524 kHz
35 */
36static inline unsigned get_usb_full_speed_rate(unsigned int rate)
37{
38 return ((rate << 13) + 62) / 125;
39}
40
41/*
42 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
43 * this will overflow at approx 4 MHz
44 */
45static inline unsigned get_usb_high_speed_rate(unsigned int rate)
46{
47 return ((rate << 10) + 62) / 125;
48}
49
50/*
51 * unlink active urbs.
52 */
53static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep)
54{
55 struct snd_usb_audio *chip = subs->stream->chip;
56 unsigned int i;
57 int async;
58
59 subs->running = 0;
60
61 if (!force && subs->stream->chip->shutdown) /* to be sure... */
62 return -EBADFD;
63
64 async = !can_sleep && chip->async_unlink;
65
66 if (!async && in_interrupt())
67 return 0;
68
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);
77 }
78 }
79 }
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);
89 }
90 }
91 }
92 }
93 return 0;
94}
95
96
97/*
98 * release a urb data
99 */
100static void release_urb_ctx(struct snd_urb_ctx *u)
101{
102 if (u->urb) {
103 if (u->buffer_size)
Stephen Rothwell3d62e3f2010-04-30 14:44:54 +1000104 usb_free_coherent(u->subs->dev, u->buffer_size,
Daniel Macke5779992010-03-04 19:46:13 +0100105 u->urb->transfer_buffer,
106 u->urb->transfer_dma);
107 usb_free_urb(u->urb);
108 u->urb = NULL;
109 }
110}
111
112/*
113 * wait until all urbs are processed.
114 */
115static int wait_clear_urbs(struct snd_usb_substream *subs)
116{
117 unsigned long end_time = jiffies + msecs_to_jiffies(1000);
118 unsigned int i;
119 int alive;
120
121 do {
122 alive = 0;
123 for (i = 0; i < subs->nurbs; i++) {
124 if (test_bit(i, &subs->active_mask))
125 alive++;
126 }
127 if (subs->syncpipe) {
128 for (i = 0; i < SYNC_URBS; i++) {
129 if (test_bit(i + 16, &subs->active_mask))
130 alive++;
131 }
132 }
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;
140}
141
142/*
143 * release a substream
144 */
145void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force)
146{
147 int i;
148
149 /* stop urbs (to be sure) */
150 deactivate_urbs(subs, force, 1);
151 wait_clear_urbs(subs);
152
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]);
Stephen Rothwell3d62e3f2010-04-30 14:44:54 +1000157 usb_free_coherent(subs->dev, SYNC_URBS * 4,
Daniel Macke5779992010-03-04 19:46:13 +0100158 subs->syncbuf, subs->sync_dma);
159 subs->syncbuf = NULL;
160 subs->nurbs = 0;
161}
162
163/*
164 * complete callback from data urb
165 */
166static void snd_complete_urb(struct urb *urb)
167{
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;
172
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);
181 }
182 }
183}
184
185
186/*
187 * complete callback from sync urb
188 */
189static void snd_complete_sync_urb(struct urb *urb)
190{
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;
195
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);
204 }
205 }
206}
207
208
209/*
210 * initialize a substream for plaback/capture
211 */
212int snd_usb_init_substream_urbs(struct snd_usb_substream *subs,
213 unsigned int period_bytes,
214 unsigned int rate,
215 unsigned int frame_bits)
216{
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;
221
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);
239 }
240 subs->phase = 0;
241
242 if (subs->fill_max)
243 subs->curpacksize = subs->maxpacksize;
244 else
245 subs->curpacksize = maxsize;
246
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700247 if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +0100248 packs_per_ms = 8 >> subs->datainterval;
249 else
250 packs_per_ms = 1;
251
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);
260
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);
279 }
280 } else {
281 while (urb_packs > 1 && urb_packs * maxsize >= period_bytes)
282 urb_packs >>= 1;
283 total_packs = MAX_URBS * urb_packs;
284 }
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
293 */
294 subs->nurbs = 2;
295 }
296
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 =
Stephen Rothwell3d62e3f2010-04-30 14:44:54 +1000311 usb_alloc_coherent(subs->dev, u->buffer_size,
312 GFP_KERNEL, &u->urb->transfer_dma);
Daniel Macke5779992010-03-04 19:46:13 +0100313 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;
320 }
321
322 if (subs->syncpipe) {
323 /* allocate and initialize sync urbs */
Stephen Rothwell3d62e3f2010-04-30 14:44:54 +1000324 subs->syncbuf = usb_alloc_coherent(subs->dev, SYNC_URBS * 4,
Daniel Macke5779992010-03-04 19:46:13 +0100325 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;
346 }
347 }
348 return 0;
349
350out_of_memory:
351 snd_usb_release_substream_urbs(subs, 0);
352 return -ENOMEM;
353}
354
355/*
356 * prepare urb for full speed capture sync pipe
357 *
358 * fill the length and offset of each urb descriptor.
359 * the fixed 10.14 frequency is passed through the pipe.
360 */
361static int prepare_capture_sync_urb(struct snd_usb_substream *subs,
362 struct snd_pcm_runtime *runtime,
363 struct urb *urb)
364{
365 unsigned char *cp = urb->transfer_buffer;
366 struct snd_urb_ctx *ctx = urb->context;
367
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;
375}
376
377/*
378 * prepare urb for high speed capture sync pipe
379 *
380 * fill the length and offset of each urb descriptor.
381 * the fixed 12.13 frequency is passed as 16.16 through the pipe.
382 */
383static int prepare_capture_sync_urb_hs(struct snd_usb_substream *subs,
384 struct snd_pcm_runtime *runtime,
385 struct urb *urb)
386{
387 unsigned char *cp = urb->transfer_buffer;
388 struct snd_urb_ctx *ctx = urb->context;
389
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;
398}
399
400/*
401 * process after capture sync complete
402 * - nothing to do
403 */
404static int retire_capture_sync_urb(struct snd_usb_substream *subs,
405 struct snd_pcm_runtime *runtime,
406 struct urb *urb)
407{
408 return 0;
409}
410
411/*
412 * prepare urb for capture data pipe
413 *
414 * fill the offset and length of each descriptor.
415 *
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.
420 */
421static int prepare_capture_urb(struct snd_usb_substream *subs,
422 struct snd_pcm_runtime *runtime,
423 struct urb *urb)
424{
425 int i, offs;
426 struct snd_urb_ctx *ctx = urb->context;
427
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;
434 }
435 urb->transfer_buffer_length = offs;
436 urb->number_of_packets = ctx->packets;
437 return 0;
438}
439
440/*
441 * process after capture complete
442 *
443 * copy the data from each desctiptor to the pcm buffer, and
444 * update the current position.
445 */
446static int retire_capture_urb(struct snd_usb_substream *subs,
447 struct snd_pcm_runtime *runtime,
448 struct urb *urb)
449{
450 unsigned long flags;
451 unsigned char *cp;
452 int i;
453 unsigned int stride, frames, bytes, oldptr;
454 int period_elapsed = 0;
455
456 stride = runtime->frame_bits >> 3;
457
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;
463 }
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);
475 }
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;
487 }
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);
497 }
498 }
499 if (period_elapsed)
500 snd_pcm_period_elapsed(subs->pcm_substream);
501 return 0;
502}
503
504/*
505 * Process after capture complete when paused. Nothing to do.
506 */
507static int retire_paused_capture_urb(struct snd_usb_substream *subs,
508 struct snd_pcm_runtime *runtime,
509 struct urb *urb)
510{
511 return 0;
512}
513
514
515/*
516 * prepare urb for full speed playback sync pipe
517 *
518 * set up the offset and length to receive the current frequency.
519 */
520
521static int prepare_playback_sync_urb(struct snd_usb_substream *subs,
522 struct snd_pcm_runtime *runtime,
523 struct urb *urb)
524{
525 struct snd_urb_ctx *ctx = urb->context;
526
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;
531}
532
533/*
534 * prepare urb for high speed playback sync pipe
535 *
536 * set up the offset and length to receive the current frequency.
537 */
538
539static int prepare_playback_sync_urb_hs(struct snd_usb_substream *subs,
540 struct snd_pcm_runtime *runtime,
541 struct urb *urb)
542{
543 struct snd_urb_ctx *ctx = urb->context;
544
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;
549}
550
551/*
552 * process after full speed playback sync complete
553 *
554 * retrieve the current 10.14 frequency from pipe, and set it.
555 * the value is referred in prepare_playback_urb().
556 */
557static int retire_playback_sync_urb(struct snd_usb_substream *subs,
558 struct snd_pcm_runtime *runtime,
559 struct urb *urb)
560{
561 unsigned int f;
562 unsigned long flags;
563
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);
571 }
572 }
573
574 return 0;
575}
576
577/*
578 * process after high speed playback sync complete
579 *
580 * retrieve the current 12.13 frequency from pipe, and set it.
581 * the value is referred in prepare_playback_urb().
582 */
583static int retire_playback_sync_urb_hs(struct snd_usb_substream *subs,
584 struct snd_pcm_runtime *runtime,
585 struct urb *urb)
586{
587 unsigned int f;
588 unsigned long flags;
589
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);
597 }
598 }
599
600 return 0;
601}
602
603/*
604 * process after E-Mu 0202/0404/Tracker Pre high speed playback sync complete
605 *
606 * These devices return the number of samples per packet instead of the number
607 * of samples per microframe.
608 */
609static int retire_playback_sync_urb_hs_emu(struct snd_usb_substream *subs,
610 struct snd_pcm_runtime *runtime,
611 struct urb *urb)
612{
613 unsigned int f;
614 unsigned long flags;
615
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);
624 }
625 }
626
627 return 0;
628}
629
630/* determine the number of frames in the next packet */
631static int snd_usb_audio_next_packet_size(struct snd_usb_substream *subs)
632{
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);
639 }
640}
641
642/*
643 * Prepare urb for streaming before playback starts or when paused.
644 *
645 * We don't have any data, so we send silence.
646 */
647static int prepare_nodata_playback_urb(struct snd_usb_substream *subs,
648 struct snd_pcm_runtime *runtime,
649 struct urb *urb)
650{
651 unsigned int i, offs, counts;
652 struct snd_urb_ctx *ctx = urb->context;
653 int stride = runtime->frame_bits >> 3;
654
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;
662 }
663 urb->number_of_packets = ctx->packets;
664 urb->transfer_buffer_length = offs * stride;
665 memset(urb->transfer_buffer,
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100666 runtime->format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0,
Daniel Macke5779992010-03-04 19:46:13 +0100667 offs * stride);
668 return 0;
669}
670
671/*
672 * prepare urb for playback data pipe
673 *
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.
678 */
679static int prepare_playback_urb(struct snd_usb_substream *subs,
680 struct snd_pcm_runtime *runtime,
681 struct urb *urb)
682{
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;
688
689 stride = runtime->frame_bits >> 3;
690
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 /* FIXME: fill-max mode is not
709 * supported yet */
710 frames -= subs->transfer_done;
711 counts -= subs->transfer_done;
712 urb->iso_frame_desc[i].length =
713 counts * stride;
714 subs->transfer_done = 0;
715 }
716 i++;
717 if (i < ctx->packets) {
718 /* add a transfer delimiter */
719 urb->iso_frame_desc[i].offset =
720 frames * stride;
721 urb->iso_frame_desc[i].length = 0;
722 urb->number_of_packets++;
723 }
724 break;
725 }
726 }
727 if (period_elapsed) /* finish at the period boundary */
728 break;
729 }
730 bytes = frames * stride;
731 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
732 /* err, the transferred area goes over buffer boundary. */
733 unsigned int bytes1 =
734 runtime->buffer_size * stride - subs->hwptr_done;
735 memcpy(urb->transfer_buffer,
736 runtime->dma_area + subs->hwptr_done, bytes1);
737 memcpy(urb->transfer_buffer + bytes1,
738 runtime->dma_area, bytes - bytes1);
739 } else {
740 memcpy(urb->transfer_buffer,
741 runtime->dma_area + subs->hwptr_done, bytes);
742 }
743 subs->hwptr_done += bytes;
744 if (subs->hwptr_done >= runtime->buffer_size * stride)
745 subs->hwptr_done -= runtime->buffer_size * stride;
746 runtime->delay += frames;
747 spin_unlock_irqrestore(&subs->lock, flags);
748 urb->transfer_buffer_length = bytes;
749 if (period_elapsed)
750 snd_pcm_period_elapsed(subs->pcm_substream);
751 return 0;
752}
753
754/*
755 * process after playback data complete
756 * - decrease the delay count again
757 */
758static int retire_playback_urb(struct snd_usb_substream *subs,
759 struct snd_pcm_runtime *runtime,
760 struct urb *urb)
761{
762 unsigned long flags;
763 int stride = runtime->frame_bits >> 3;
764 int processed = urb->transfer_buffer_length / stride;
765
766 spin_lock_irqsave(&subs->lock, flags);
767 if (processed > runtime->delay)
768 runtime->delay = 0;
769 else
770 runtime->delay -= processed;
771 spin_unlock_irqrestore(&subs->lock, flags);
772 return 0;
773}
774
775static const char *usb_error_string(int err)
776{
777 switch (err) {
778 case -ENODEV:
779 return "no device";
780 case -ENOENT:
781 return "endpoint not enabled";
782 case -EPIPE:
783 return "endpoint stalled";
784 case -ENOSPC:
785 return "not enough bandwidth";
786 case -ESHUTDOWN:
787 return "device disabled";
788 case -EHOSTUNREACH:
789 return "device suspended";
790 case -EINVAL:
791 case -EAGAIN:
792 case -EFBIG:
793 case -EMSGSIZE:
794 return "internal error";
795 default:
796 return "unknown error";
797 }
798}
799
800/*
801 * set up and start data/sync urbs
802 */
803static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime)
804{
805 unsigned int i;
806 int err;
807
808 if (subs->stream->chip->shutdown)
809 return -EBADFD;
810
811 for (i = 0; i < subs->nurbs; i++) {
812 if (snd_BUG_ON(!subs->dataurb[i].urb))
813 return -EINVAL;
814 if (subs->ops.prepare(subs, runtime, subs->dataurb[i].urb) < 0) {
815 snd_printk(KERN_ERR "cannot prepare datapipe for urb %d\n", i);
816 goto __error;
817 }
818 }
819 if (subs->syncpipe) {
820 for (i = 0; i < SYNC_URBS; i++) {
821 if (snd_BUG_ON(!subs->syncurb[i].urb))
822 return -EINVAL;
823 if (subs->ops.prepare_sync(subs, runtime, subs->syncurb[i].urb) < 0) {
824 snd_printk(KERN_ERR "cannot prepare syncpipe for urb %d\n", i);
825 goto __error;
826 }
827 }
828 }
829
830 subs->active_mask = 0;
831 subs->unlink_mask = 0;
832 subs->running = 1;
833 for (i = 0; i < subs->nurbs; i++) {
834 err = usb_submit_urb(subs->dataurb[i].urb, GFP_ATOMIC);
835 if (err < 0) {
836 snd_printk(KERN_ERR "cannot submit datapipe "
837 "for urb %d, error %d: %s\n",
838 i, err, usb_error_string(err));
839 goto __error;
840 }
841 set_bit(i, &subs->active_mask);
842 }
843 if (subs->syncpipe) {
844 for (i = 0; i < SYNC_URBS; i++) {
845 err = usb_submit_urb(subs->syncurb[i].urb, GFP_ATOMIC);
846 if (err < 0) {
847 snd_printk(KERN_ERR "cannot submit syncpipe "
848 "for urb %d, error %d: %s\n",
849 i, err, usb_error_string(err));
850 goto __error;
851 }
852 set_bit(i + 16, &subs->active_mask);
853 }
854 }
855 return 0;
856
857 __error:
858 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
859 deactivate_urbs(subs, 0, 0);
860 return -EPIPE;
861}
862
863
864/*
865 */
866static struct snd_urb_ops audio_urb_ops[2] = {
867 {
868 .prepare = prepare_nodata_playback_urb,
869 .retire = retire_playback_urb,
870 .prepare_sync = prepare_playback_sync_urb,
871 .retire_sync = retire_playback_sync_urb,
872 },
873 {
874 .prepare = prepare_capture_urb,
875 .retire = retire_capture_urb,
876 .prepare_sync = prepare_capture_sync_urb,
877 .retire_sync = retire_capture_sync_urb,
878 },
879};
880
881static struct snd_urb_ops audio_urb_ops_high_speed[2] = {
882 {
883 .prepare = prepare_nodata_playback_urb,
884 .retire = retire_playback_urb,
885 .prepare_sync = prepare_playback_sync_urb_hs,
886 .retire_sync = retire_playback_sync_urb_hs,
887 },
888 {
889 .prepare = prepare_capture_urb,
890 .retire = retire_capture_urb,
891 .prepare_sync = prepare_capture_sync_urb_hs,
892 .retire_sync = retire_capture_sync_urb,
893 },
894};
895
896/*
897 * initialize the substream instance.
898 */
899
900void snd_usb_init_substream(struct snd_usb_stream *as,
901 int stream, struct audioformat *fp)
902{
903 struct snd_usb_substream *subs = &as->substream[stream];
904
905 INIT_LIST_HEAD(&subs->fmt_list);
906 spin_lock_init(&subs->lock);
907
908 subs->stream = as;
909 subs->direction = stream;
910 subs->dev = as->chip->dev;
911 subs->txfr_quirk = as->chip->txfr_quirk;
912 if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) {
913 subs->ops = audio_urb_ops[stream];
914 } else {
915 subs->ops = audio_urb_ops_high_speed[stream];
916 switch (as->chip->usb_id) {
917 case USB_ID(0x041e, 0x3f02): /* E-Mu 0202 USB */
918 case USB_ID(0x041e, 0x3f04): /* E-Mu 0404 USB */
919 case USB_ID(0x041e, 0x3f0a): /* E-Mu Tracker Pre */
920 subs->ops.retire_sync = retire_playback_sync_urb_hs_emu;
921 break;
Felix Homannfca5bca2010-03-25 11:29:14 +0100922 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra 8 */
923 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
924 subs->ops.prepare_sync = prepare_playback_sync_urb;
925 subs->ops.retire_sync = retire_playback_sync_urb;
926 break;
Daniel Macke5779992010-03-04 19:46:13 +0100927 }
928 }
929
930 snd_usb_set_pcm_ops(as->pcm, stream);
931
932 list_add_tail(&fp->list, &subs->fmt_list);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100933 subs->formats |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +0100934 subs->endpoint = fp->endpoint;
935 subs->num_formats++;
936 subs->fmt_type = fp->fmt_type;
937}
938
939int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd)
940{
941 struct snd_usb_substream *subs = substream->runtime->private_data;
942
943 switch (cmd) {
944 case SNDRV_PCM_TRIGGER_START:
945 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
946 subs->ops.prepare = prepare_playback_urb;
947 return 0;
948 case SNDRV_PCM_TRIGGER_STOP:
949 return deactivate_urbs(subs, 0, 0);
950 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
951 subs->ops.prepare = prepare_nodata_playback_urb;
952 return 0;
953 }
954
955 return -EINVAL;
956}
957
958int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd)
959{
960 struct snd_usb_substream *subs = substream->runtime->private_data;
961
962 switch (cmd) {
963 case SNDRV_PCM_TRIGGER_START:
964 subs->ops.retire = retire_capture_urb;
965 return start_urbs(subs, substream->runtime);
966 case SNDRV_PCM_TRIGGER_STOP:
967 return deactivate_urbs(subs, 0, 0);
968 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
969 subs->ops.retire = retire_paused_capture_urb;
970 return 0;
971 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
972 subs->ops.retire = retire_capture_urb;
973 return 0;
974 }
975
976 return -EINVAL;
977}
978
979int snd_usb_substream_prepare(struct snd_usb_substream *subs,
980 struct snd_pcm_runtime *runtime)
981{
982 /* clear urbs (to be sure) */
983 deactivate_urbs(subs, 0, 1);
984 wait_clear_urbs(subs);
985
986 /* for playback, submit the URBs now; otherwise, the first hwptr_done
987 * updates for all URBs would happen at the same time when starting */
988 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) {
989 subs->ops.prepare = prepare_nodata_playback_urb;
990 return start_urbs(subs, runtime);
991 }
992
993 return 0;
994}
995