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>
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;
53 static int deactivate_urbs(struct snd_usb_substream
*subs
, int force
, int can_sleep
)
55 struct snd_usb_audio
*chip
= subs
->stream
->chip
;
61 if (!force
&& subs
->stream
->chip
->shutdown
) /* to be sure... */
64 async
= !can_sleep
&& chip
->async_unlink
;
66 if (!async
&& in_interrupt())
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
;
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
;
100 static void release_urb_ctx(struct snd_urb_ctx
*u
)
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
);
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);
123 for (i
= 0; i
< subs
->nurbs
; i
++) {
124 if (test_bit(i
, &subs
->active_mask
))
127 if (subs
->syncpipe
) {
128 for (i
= 0; i
< SYNC_URBS
; i
++) {
129 if (test_bit(i
+ 16, &subs
->active_mask
))
135 schedule_timeout_uninterruptible(1);
136 } while (time_before(jiffies
, end_time
));
138 snd_printk(KERN_ERR
"timeout: still %d active urbs..\n", alive
);
143 * release a substream
145 void snd_usb_release_substream_urbs(struct snd_usb_substream
*subs
, int force
)
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
;
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
;
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
);
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
;
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
);
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
,
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
);
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
);
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
);
243 subs
->curpacksize
= subs
->maxpacksize
;
245 subs
->curpacksize
= maxsize
;
247 if (snd_usb_get_speed(subs
->dev
) == USB_SPEED_HIGH
)
248 packs_per_ms
= 8 >> subs
->datainterval
;
253 urb_packs
= max(chip
->nrpacks
, 1);
254 urb_packs
= min(urb_packs
, (unsigned int)MAX_PACKS
);
257 urb_packs
*= packs_per_ms
;
259 urb_packs
= min(urb_packs
, 1U << subs
->syncinterval
);
261 /* decide how many packets to be used */
263 unsigned int minsize
, maxpacks
;
264 /* determine how small a packet can be */
265 minsize
= (subs
->freqn
>> (16 - subs
->datainterval
))
267 /* with sync from device, assume it can be 12% lower */
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) {
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
);
281 while (urb_packs
> 1 && urb_packs
* maxsize
>= period_bytes
)
283 total_packs
= MAX_URBS
* urb_packs
;
285 subs
->nurbs
= (total_packs
+ urb_packs
- 1) / urb_packs
;
286 if (subs
->nurbs
> MAX_URBS
) {
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
297 /* allocate and initialize data urbs */
298 for (i
= 0; i
< subs
->nurbs
; i
++) {
299 struct snd_urb_ctx
*u
= &subs
->dataurb
[i
];
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
);
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
)
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
;
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
);
328 for (i
= 0; i
< SYNC_URBS
; i
++) {
329 struct snd_urb_ctx
*u
= &subs
->syncurb
[i
];
333 u
->urb
= usb_alloc_urb(1, GFP_KERNEL
);
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
;
345 u
->urb
->complete
= snd_complete_sync_urb
;
351 snd_usb_release_substream_urbs(subs
, 0);
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
,
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;
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
,
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;
394 cp
[1] = subs
->freqn
>> 8;
395 cp
[2] = subs
->freqn
>> 16;
396 cp
[3] = subs
->freqn
>> 24;
401 * process after capture sync complete
404 static int retire_capture_sync_urb(struct snd_usb_substream
*subs
,
405 struct snd_pcm_runtime
*runtime
,
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
,
426 struct snd_urb_ctx
*ctx
= urb
->context
;
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
;
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
,
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
);
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
;
472 bytes
= frames
* stride
;
473 snd_printdd(KERN_ERR
"Corrected urb data len. %d->%d\n",
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
;
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
);
496 memcpy(runtime
->dma_area
+ oldptr
, cp
, bytes
);
500 snd_pcm_period_elapsed(subs
->pcm_substream
);
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
,
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
,
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;
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
,
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;
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
,
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
);
570 spin_unlock_irqrestore(&subs
->lock
, flags
);
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
,
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
);
596 spin_unlock_irqrestore(&subs
->lock
, flags
);
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
,
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
);
623 spin_unlock_irqrestore(&subs
->lock
, flags
);
630 /* determine the number of frames in the next packet */
631 static int snd_usb_audio_next_packet_size(struct snd_usb_substream
*subs
)
634 return subs
->maxframesize
;
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
,
651 unsigned int i
, offs
, counts
;
652 struct snd_urb_ctx
*ctx
= urb
->context
;
653 int stride
= runtime
->frame_bits
>> 3;
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
;
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,
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
679 static int prepare_playback_urb(struct snd_usb_substream
*subs
,
680 struct snd_pcm_runtime
*runtime
,
684 unsigned int counts
, frames
, bytes
;
686 int period_elapsed
= 0;
687 struct snd_urb_ctx
*ctx
= urb
->context
;
689 stride
= runtime
->frame_bits
>> 3;
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
;
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
;
706 if (subs
->fmt_type
== UAC_FORMAT_TYPE_II
) {
707 if (subs
->transfer_done
> 0) {
708 /* FIXME: fill-max mode is not
710 frames
-= subs
->transfer_done
;
711 counts
-= subs
->transfer_done
;
712 urb
->iso_frame_desc
[i
].length
=
714 subs
->transfer_done
= 0;
717 if (i
< ctx
->packets
) {
718 /* add a transfer delimiter */
719 urb
->iso_frame_desc
[i
].offset
=
721 urb
->iso_frame_desc
[i
].length
= 0;
722 urb
->number_of_packets
++;
727 if (period_elapsed
) /* finish at the period boundary */
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
);
740 memcpy(urb
->transfer_buffer
,
741 runtime
->dma_area
+ subs
->hwptr_done
, bytes
);
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
;
750 snd_pcm_period_elapsed(subs
->pcm_substream
);
755 * process after playback data complete
756 * - decrease the delay count again
758 static int retire_playback_urb(struct snd_usb_substream
*subs
,
759 struct snd_pcm_runtime
*runtime
,
763 int stride
= runtime
->frame_bits
>> 3;
764 int processed
= urb
->transfer_buffer_length
/ stride
;
766 spin_lock_irqsave(&subs
->lock
, flags
);
767 if (processed
> runtime
->delay
)
770 runtime
->delay
-= processed
;
771 spin_unlock_irqrestore(&subs
->lock
, flags
);
775 static const char *usb_error_string(int err
)
781 return "endpoint not enabled";
783 return "endpoint stalled";
785 return "not enough bandwidth";
787 return "device disabled";
789 return "device suspended";
794 return "internal error";
796 return "unknown error";
801 * set up and start data/sync urbs
803 static int start_urbs(struct snd_usb_substream
*subs
, struct snd_pcm_runtime
*runtime
)
808 if (subs
->stream
->chip
->shutdown
)
811 for (i
= 0; i
< subs
->nurbs
; i
++) {
812 if (snd_BUG_ON(!subs
->dataurb
[i
].urb
))
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
);
819 if (subs
->syncpipe
) {
820 for (i
= 0; i
< SYNC_URBS
; i
++) {
821 if (snd_BUG_ON(!subs
->syncurb
[i
].urb
))
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
);
830 subs
->active_mask
= 0;
831 subs
->unlink_mask
= 0;
833 for (i
= 0; i
< subs
->nurbs
; i
++) {
834 err
= usb_submit_urb(subs
->dataurb
[i
].urb
, GFP_ATOMIC
);
836 snd_printk(KERN_ERR
"cannot submit datapipe "
837 "for urb %d, error %d: %s\n",
838 i
, err
, usb_error_string(err
));
841 set_bit(i
, &subs
->active_mask
);
843 if (subs
->syncpipe
) {
844 for (i
= 0; i
< SYNC_URBS
; i
++) {
845 err
= usb_submit_urb(subs
->syncurb
[i
].urb
, GFP_ATOMIC
);
847 snd_printk(KERN_ERR
"cannot submit syncpipe "
848 "for urb %d, error %d: %s\n",
849 i
, err
, usb_error_string(err
));
852 set_bit(i
+ 16, &subs
->active_mask
);
858 // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN);
859 deactivate_urbs(subs
, 0, 0);
866 static struct snd_urb_ops audio_urb_ops
[2] = {
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
,
874 .prepare
= prepare_capture_urb
,
875 .retire
= retire_capture_urb
,
876 .prepare_sync
= prepare_capture_sync_urb
,
877 .retire_sync
= retire_capture_sync_urb
,
881 static struct snd_urb_ops audio_urb_ops_high_speed
[2] = {
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
,
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
,
897 * initialize the substream instance.
900 void snd_usb_init_substream(struct snd_usb_stream
*as
,
901 int stream
, struct audioformat
*fp
)
903 struct snd_usb_substream
*subs
= &as
->substream
[stream
];
905 INIT_LIST_HEAD(&subs
->fmt_list
);
906 spin_lock_init(&subs
->lock
);
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
];
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
;
922 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
;
930 snd_usb_set_pcm_ops(as
->pcm
, stream
);
932 list_add_tail(&fp
->list
, &subs
->fmt_list
);
933 subs
->formats
|= fp
->formats
;
934 subs
->endpoint
= fp
->endpoint
;
936 subs
->fmt_type
= fp
->fmt_type
;
939 int snd_usb_substream_playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
941 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
944 case SNDRV_PCM_TRIGGER_START
:
945 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
946 subs
->ops
.prepare
= prepare_playback_urb
;
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
;
958 int snd_usb_substream_capture_trigger(struct snd_pcm_substream
*substream
, int cmd
)
960 struct snd_usb_substream
*subs
= substream
->runtime
->private_data
;
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
;
971 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
972 subs
->ops
.retire
= retire_capture_urb
;
979 int snd_usb_substream_prepare(struct snd_usb_substream
*subs
,
980 struct snd_pcm_runtime
*runtime
)
982 /* clear urbs (to be sure) */
983 deactivate_urbs(subs
, 0, 1);
984 wait_clear_urbs(subs
);
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
);