2 * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <linux/usb.h>
20 #include <linux/gfp.h>
22 #include "usb_stream.h"
27 static unsigned usb_stream_next_packet_size(struct usb_stream_kernel
*sk
)
29 struct usb_stream
*s
= sk
->s
;
30 sk
->out_phase_peeked
= (sk
->out_phase
& 0xffff) + sk
->freqn
;
31 return (sk
->out_phase_peeked
>> 16) * s
->cfg
.frame_size
;
34 static void playback_prep_freqn(struct usb_stream_kernel
*sk
, struct urb
*urb
)
36 struct usb_stream
*s
= sk
->s
;
39 for (pack
= 0; pack
< sk
->n_o_ps
; pack
++) {
40 int l
= usb_stream_next_packet_size(sk
);
41 if (s
->idle_outsize
+ lb
+ l
> s
->period_size
)
44 sk
->out_phase
= sk
->out_phase_peeked
;
45 urb
->iso_frame_desc
[pack
].offset
= lb
;
46 urb
->iso_frame_desc
[pack
].length
= l
;
49 snd_printdd(KERN_DEBUG
"%i\n", lb
);
52 urb
->number_of_packets
= pack
;
53 urb
->transfer_buffer_length
= lb
;
54 s
->idle_outsize
+= lb
- s
->period_size
;
55 snd_printdd(KERN_DEBUG
"idle=%i ul=%i ps=%i\n", s
->idle_outsize
,
59 static void init_pipe_urbs(struct usb_stream_kernel
*sk
, unsigned use_packsize
,
60 struct urb
**urbs
, char *transfer
,
61 struct usb_device
*dev
, int pipe
)
64 int maxpacket
= use_packsize
?
65 use_packsize
: usb_maxpacket(dev
, pipe
, usb_pipeout(pipe
));
66 int transfer_length
= maxpacket
* sk
->n_o_ps
;
68 for (u
= 0; u
< USB_STREAM_NURBS
;
69 ++u
, transfer
+= transfer_length
) {
70 struct urb
*urb
= urbs
[u
];
71 struct usb_iso_packet_descriptor
*desc
;
72 urb
->transfer_buffer
= transfer
;
75 urb
->number_of_packets
= sk
->n_o_ps
;
78 if (usb_pipeout(pipe
))
81 urb
->transfer_buffer_length
= transfer_length
;
82 desc
= urb
->iso_frame_desc
;
84 desc
->length
= maxpacket
;
85 for (p
= 1; p
< sk
->n_o_ps
; ++p
) {
86 desc
[p
].offset
= desc
[p
- 1].offset
+ maxpacket
;
87 desc
[p
].length
= maxpacket
;
92 static void init_urbs(struct usb_stream_kernel
*sk
, unsigned use_packsize
,
93 struct usb_device
*dev
, int in_pipe
, int out_pipe
)
95 struct usb_stream
*s
= sk
->s
;
96 char *indata
= (char *)s
+ sizeof(*s
) +
97 sizeof(struct usb_stream_packet
) *
101 for (u
= 0; u
< USB_STREAM_NURBS
; ++u
) {
102 sk
->inurb
[u
] = usb_alloc_urb(sk
->n_o_ps
, GFP_KERNEL
);
103 sk
->outurb
[u
] = usb_alloc_urb(sk
->n_o_ps
, GFP_KERNEL
);
106 init_pipe_urbs(sk
, use_packsize
, sk
->inurb
, indata
, dev
, in_pipe
);
107 init_pipe_urbs(sk
, use_packsize
, sk
->outurb
, sk
->write_page
, dev
,
113 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
114 * this will overflow at approx 524 kHz
116 static inline unsigned get_usb_full_speed_rate(unsigned rate
)
118 return ((rate
<< 13) + 62) / 125;
122 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
123 * this will overflow at approx 4 MHz
125 static inline unsigned get_usb_high_speed_rate(unsigned rate
)
127 return ((rate
<< 10) + 62) / 125;
130 void usb_stream_free(struct usb_stream_kernel
*sk
)
132 struct usb_stream
*s
;
135 for (u
= 0; u
< USB_STREAM_NURBS
; ++u
) {
136 usb_free_urb(sk
->inurb
[u
]);
138 usb_free_urb(sk
->outurb
[u
]);
139 sk
->outurb
[u
] = NULL
;
146 free_pages((unsigned long)sk
->write_page
, get_order(s
->write_size
));
147 sk
->write_page
= NULL
;
148 free_pages((unsigned long)s
, get_order(s
->read_size
));
152 struct usb_stream
*usb_stream_new(struct usb_stream_kernel
*sk
,
153 struct usb_device
*dev
,
154 unsigned in_endpoint
, unsigned out_endpoint
,
155 unsigned sample_rate
, unsigned use_packsize
,
156 unsigned period_frames
, unsigned frame_size
)
158 int packets
, max_packsize
;
159 int in_pipe
, out_pipe
;
160 int read_size
= sizeof(struct usb_stream
);
162 int usb_frames
= dev
->speed
== USB_SPEED_HIGH
? 8000 : 1000;
165 in_pipe
= usb_rcvisocpipe(dev
, in_endpoint
);
166 out_pipe
= usb_sndisocpipe(dev
, out_endpoint
);
168 max_packsize
= use_packsize
?
169 use_packsize
: usb_maxpacket(dev
, in_pipe
, 0);
172 t_period = period_frames / sample_rate
173 iso_packs = t_period / t_iso_frame
174 = (period_frames / sample_rate) * (1 / t_iso_frame)
177 packets
= period_frames
* usb_frames
/ sample_rate
+ 1;
179 if (dev
->speed
== USB_SPEED_HIGH
)
180 packets
= (packets
+ 7) & ~7;
182 read_size
+= packets
* USB_STREAM_URBDEPTH
*
183 (max_packsize
+ sizeof(struct usb_stream_packet
));
185 max_packsize
= usb_maxpacket(dev
, out_pipe
, 1);
186 write_size
= max_packsize
* packets
* USB_STREAM_URBDEPTH
;
188 if (read_size
>= 256*PAGE_SIZE
|| write_size
>= 256*PAGE_SIZE
) {
189 snd_printk(KERN_WARNING
"a size exceeds 128*PAGE_SIZE\n");
193 pg
= get_order(read_size
);
194 sk
->s
= (void *) __get_free_pages(GFP_KERNEL
|__GFP_COMP
|__GFP_ZERO
, pg
);
196 snd_printk(KERN_WARNING
"couldn't __get_free_pages()\n");
199 sk
->s
->cfg
.version
= USB_STREAM_INTERFACE_VERSION
;
201 sk
->s
->read_size
= read_size
;
203 sk
->s
->cfg
.sample_rate
= sample_rate
;
204 sk
->s
->cfg
.frame_size
= frame_size
;
205 sk
->n_o_ps
= packets
;
206 sk
->s
->inpackets
= packets
* USB_STREAM_URBDEPTH
;
207 sk
->s
->cfg
.period_frames
= period_frames
;
208 sk
->s
->period_size
= frame_size
* period_frames
;
210 sk
->s
->write_size
= write_size
;
211 pg
= get_order(write_size
);
214 (void *)__get_free_pages(GFP_KERNEL
|__GFP_COMP
|__GFP_ZERO
, pg
);
215 if (!sk
->write_page
) {
216 snd_printk(KERN_WARNING
"couldn't __get_free_pages()\n");
221 /* calculate the frequency in 16.16 format */
222 if (dev
->speed
== USB_SPEED_FULL
)
223 sk
->freqn
= get_usb_full_speed_rate(sample_rate
);
225 sk
->freqn
= get_usb_high_speed_rate(sample_rate
);
227 init_urbs(sk
, use_packsize
, dev
, in_pipe
, out_pipe
);
228 sk
->s
->state
= usb_stream_stopped
;
236 static bool balance_check(struct usb_stream_kernel
*sk
, struct urb
*urb
)
239 if (unlikely(urb
->status
)) {
240 if (urb
->status
!= -ESHUTDOWN
&& urb
->status
!= -ENOENT
)
241 snd_printk(KERN_WARNING
"status=%i\n", urb
->status
);
242 sk
->iso_frame_balance
= 0x7FFFFFFF;
245 r
= sk
->iso_frame_balance
== 0;
251 static bool balance_playback(struct usb_stream_kernel
*sk
, struct urb
*urb
)
253 sk
->iso_frame_balance
+= urb
->number_of_packets
;
254 return balance_check(sk
, urb
);
257 static bool balance_capture(struct usb_stream_kernel
*sk
, struct urb
*urb
)
259 sk
->iso_frame_balance
-= urb
->number_of_packets
;
260 return balance_check(sk
, urb
);
263 static void subs_set_complete(struct urb
**urbs
, void (*complete
)(struct urb
*))
267 for (u
= 0; u
< USB_STREAM_NURBS
; u
++) {
268 struct urb
*urb
= urbs
[u
];
269 urb
->complete
= complete
;
273 static int usb_stream_prepare_playback(struct usb_stream_kernel
*sk
,
276 struct usb_stream
*s
= sk
->s
;
278 struct usb_iso_packet_descriptor
*id
, *od
;
279 int p
= 0, lb
= 0, l
= 0;
281 io
= sk
->idle_outurb
;
282 od
= io
->iso_frame_desc
;
284 for (; s
->sync_packet
< 0; ++p
, ++s
->sync_packet
) {
285 struct urb
*ii
= sk
->completed_inurb
;
286 id
= ii
->iso_frame_desc
+
287 ii
->number_of_packets
+ s
->sync_packet
;
288 l
= id
->actual_length
;
296 s
->sync_packet
< inurb
->number_of_packets
&& p
< sk
->n_o_ps
;
297 ++p
, ++s
->sync_packet
) {
298 l
= inurb
->iso_frame_desc
[s
->sync_packet
].actual_length
;
300 if (s
->idle_outsize
+ lb
+ l
> s
->period_size
)
309 s
->sync_packet
-= inurb
->number_of_packets
;
310 if (unlikely(s
->sync_packet
< -2 || s
->sync_packet
> 0)) {
311 snd_printk(KERN_WARNING
"invalid sync_packet = %i;"
312 " p=%i nop=%i %i %x %x %x > %x\n",
313 s
->sync_packet
, p
, inurb
->number_of_packets
,
314 s
->idle_outsize
+ lb
+ l
,
315 s
->idle_outsize
, lb
, l
,
319 if (unlikely(lb
% s
->cfg
.frame_size
)) {
320 snd_printk(KERN_WARNING
"invalid outsize = %i\n",
324 s
->idle_outsize
+= lb
- s
->period_size
;
325 io
->number_of_packets
= p
;
326 io
->transfer_buffer_length
= lb
;
327 if (s
->idle_outsize
<= 0)
330 snd_printk(KERN_WARNING
"idle=%i\n", s
->idle_outsize
);
334 static void prepare_inurb(int number_of_packets
, struct urb
*iu
)
336 struct usb_iso_packet_descriptor
*id
;
339 iu
->number_of_packets
= number_of_packets
;
340 id
= iu
->iso_frame_desc
;
342 for (p
= 0; p
< iu
->number_of_packets
- 1; ++p
)
343 id
[p
+ 1].offset
= id
[p
].offset
+ id
[p
].length
;
345 iu
->transfer_buffer_length
=
346 id
[0].length
* iu
->number_of_packets
;
349 static int submit_urbs(struct usb_stream_kernel
*sk
,
350 struct urb
*inurb
, struct urb
*outurb
)
353 prepare_inurb(sk
->idle_outurb
->number_of_packets
, sk
->idle_inurb
);
354 err
= usb_submit_urb(sk
->idle_inurb
, GFP_ATOMIC
);
356 snd_printk(KERN_ERR
"%i\n", err
);
359 sk
->idle_inurb
= sk
->completed_inurb
;
360 sk
->completed_inurb
= inurb
;
361 err
= usb_submit_urb(sk
->idle_outurb
, GFP_ATOMIC
);
363 snd_printk(KERN_ERR
"%i\n", err
);
366 sk
->idle_outurb
= sk
->completed_outurb
;
367 sk
->completed_outurb
= outurb
;
371 #ifdef DEBUG_LOOP_BACK
373 This loop_back() shows how to read/write the period data.
375 static void loop_back(struct usb_stream
*s
)
380 struct usb_iso_packet_descriptor
*id
;
382 o
= s
->playback1st_to
;
383 ol
= s
->playback1st_size
;
386 if (s
->insplit_pack
>= 0) {
388 id
= iu
->iso_frame_desc
;
393 for (; p
< iu
->number_of_packets
&& l
< s
->period_size
; ++p
) {
394 i
= iu
->transfer_buffer
+ id
[p
].offset
;
395 il
= id
[p
].actual_length
;
396 if (l
+ il
> s
->period_size
)
397 il
= s
->period_size
- l
;
406 memcpy(o
, i
+ ol
, il
- ol
);
408 ol
= s
->period_size
- s
->playback1st_size
;
412 if (iu
== sk
->completed_inurb
) {
413 if (l
!= s
->period_size
)
414 printk(KERN_DEBUG
"%s:%i %i\n", __func__
, __LINE__
,
415 l
/(int)s
->cfg
.frame_size
);
420 iu
= sk
->completed_inurb
;
421 id
= iu
->iso_frame_desc
;
427 static void loop_back(struct usb_stream
*s
)
432 static void stream_idle(struct usb_stream_kernel
*sk
,
433 struct urb
*inurb
, struct urb
*outurb
)
435 struct usb_stream
*s
= sk
->s
;
437 int insize
= s
->idle_insize
;
440 s
->inpacket_split
= s
->next_inpacket_split
;
441 s
->inpacket_split_at
= s
->next_inpacket_split_at
;
442 s
->next_inpacket_split
= -1;
443 s
->next_inpacket_split_at
= 0;
445 for (p
= 0; p
< inurb
->number_of_packets
; ++p
) {
446 struct usb_iso_packet_descriptor
*id
= inurb
->iso_frame_desc
;
447 l
= id
[p
].actual_length
;
448 if (unlikely(l
== 0 || id
[p
].status
)) {
449 snd_printk(KERN_WARNING
"underrun, status=%u\n",
454 s
->inpacket_head
%= s
->inpackets
;
455 if (s
->inpacket_split
== -1)
456 s
->inpacket_split
= s
->inpacket_head
;
458 s
->inpacket
[s
->inpacket_head
].offset
=
459 id
[p
].offset
+ (inurb
->transfer_buffer
- (void *)s
);
460 s
->inpacket
[s
->inpacket_head
].length
= l
;
461 if (insize
+ l
> s
->period_size
&&
462 s
->next_inpacket_split
== -1) {
463 s
->next_inpacket_split
= s
->inpacket_head
;
464 s
->next_inpacket_split_at
= s
->period_size
- insize
;
469 s
->idle_insize
+= urb_size
- s
->period_size
;
470 if (s
->idle_insize
< 0) {
471 snd_printk(KERN_WARNING
"%i\n",
472 (s
->idle_insize
)/(int)s
->cfg
.frame_size
);
475 s
->insize_done
+= urb_size
;
478 s
->outpacket
[0].offset
= (sk
->idle_outurb
->transfer_buffer
-
481 if (usb_stream_prepare_playback(sk
, inurb
) < 0)
484 s
->outpacket
[0].length
= sk
->idle_outurb
->transfer_buffer_length
+ l
;
485 s
->outpacket
[1].offset
= sk
->completed_outurb
->transfer_buffer
-
488 if (submit_urbs(sk
, inurb
, outurb
) < 0)
493 wake_up_all(&sk
->sleep
);
496 s
->state
= usb_stream_xrun
;
497 wake_up_all(&sk
->sleep
);
500 static void i_capture_idle(struct urb
*urb
)
502 struct usb_stream_kernel
*sk
= urb
->context
;
503 if (balance_capture(sk
, urb
))
504 stream_idle(sk
, urb
, sk
->i_urb
);
507 static void i_playback_idle(struct urb
*urb
)
509 struct usb_stream_kernel
*sk
= urb
->context
;
510 if (balance_playback(sk
, urb
))
511 stream_idle(sk
, sk
->i_urb
, urb
);
514 static void stream_start(struct usb_stream_kernel
*sk
,
515 struct urb
*inurb
, struct urb
*outurb
)
517 struct usb_stream
*s
= sk
->s
;
518 if (s
->state
>= usb_stream_sync1
) {
519 int l
, p
, max_diff
, max_diff_0
;
521 unsigned frames_per_packet
, min_frames
= 0;
522 frames_per_packet
= (s
->period_size
- s
->idle_insize
);
523 frames_per_packet
<<= 8;
525 s
->cfg
.frame_size
* inurb
->number_of_packets
;
528 max_diff_0
= s
->cfg
.frame_size
;
529 if (s
->cfg
.period_frames
>= 256)
531 if (s
->cfg
.period_frames
>= 1024)
533 max_diff
= max_diff_0
;
534 for (p
= 0; p
< inurb
->number_of_packets
; ++p
) {
536 l
= inurb
->iso_frame_desc
[p
].actual_length
;
539 min_frames
+= frames_per_packet
;
541 (min_frames
>> 8) * s
->cfg
.frame_size
;
542 if (diff
< max_diff
) {
543 snd_printdd(KERN_DEBUG
"%i %i %i %i\n",
545 urb_size
/ (int)s
->cfg
.frame_size
,
546 inurb
->number_of_packets
, diff
);
550 s
->idle_insize
-= max_diff
- max_diff_0
;
551 s
->idle_insize
+= urb_size
- s
->period_size
;
552 if (s
->idle_insize
< 0) {
553 snd_printk(KERN_WARNING
"%i %i %i\n",
554 s
->idle_insize
, urb_size
, s
->period_size
);
556 } else if (s
->idle_insize
== 0) {
557 s
->next_inpacket_split
=
558 (s
->inpacket_head
+ 1) % s
->inpackets
;
559 s
->next_inpacket_split_at
= 0;
561 unsigned split
= s
->inpacket_head
;
563 while (l
> s
->inpacket
[split
].length
) {
564 l
-= s
->inpacket
[split
].length
;
566 split
= s
->inpackets
- 1;
570 s
->next_inpacket_split
= split
;
571 s
->next_inpacket_split_at
=
572 s
->inpacket
[split
].length
- l
;
575 s
->insize_done
+= urb_size
;
577 if (usb_stream_prepare_playback(sk
, inurb
) < 0)
581 playback_prep_freqn(sk
, sk
->idle_outurb
);
583 if (submit_urbs(sk
, inurb
, outurb
) < 0)
586 if (s
->state
== usb_stream_sync1
&& s
->insize_done
> 360000) {
587 /* just guesswork ^^^^^^ */
588 s
->state
= usb_stream_ready
;
589 subs_set_complete(sk
->inurb
, i_capture_idle
);
590 subs_set_complete(sk
->outurb
, i_playback_idle
);
594 static void i_capture_start(struct urb
*urb
)
596 struct usb_iso_packet_descriptor
*id
= urb
->iso_frame_desc
;
597 struct usb_stream_kernel
*sk
= urb
->context
;
598 struct usb_stream
*s
= sk
->s
;
603 snd_printk(KERN_WARNING
"status=%i\n", urb
->status
);
607 for (p
= 0; p
< urb
->number_of_packets
; ++p
) {
608 int l
= id
[p
].actual_length
;
609 if (l
< s
->cfg
.frame_size
) {
611 if (s
->state
>= usb_stream_sync0
) {
612 snd_printk(KERN_WARNING
"%i\n", l
);
617 s
->inpacket_head
%= s
->inpackets
;
618 s
->inpacket
[s
->inpacket_head
].offset
=
619 id
[p
].offset
+ (urb
->transfer_buffer
- (void *)s
);
620 s
->inpacket
[s
->inpacket_head
].length
= l
;
624 printk(KERN_DEBUG
"%s:%i: %i", __func__
, __LINE__
,
625 urb
->iso_frame_desc
[0].actual_length
);
626 for (pack
= 1; pack
< urb
->number_of_packets
; ++pack
) {
627 int l
= urb
->iso_frame_desc
[pack
].actual_length
;
633 if (!empty
&& s
->state
< usb_stream_sync1
)
636 if (balance_capture(sk
, urb
))
637 stream_start(sk
, urb
, sk
->i_urb
);
640 static void i_playback_start(struct urb
*urb
)
642 struct usb_stream_kernel
*sk
= urb
->context
;
643 if (balance_playback(sk
, urb
))
644 stream_start(sk
, sk
->i_urb
, urb
);
647 int usb_stream_start(struct usb_stream_kernel
*sk
)
649 struct usb_stream
*s
= sk
->s
;
650 int frame
= 0, iters
= 0;
654 if (s
->state
!= usb_stream_stopped
)
657 subs_set_complete(sk
->inurb
, i_capture_start
);
658 subs_set_complete(sk
->outurb
, i_playback_start
);
659 memset(sk
->write_page
, 0, s
->write_size
);
665 s
->inpacket_head
= -1;
666 sk
->iso_frame_balance
= 0;
668 for (u
= 0; u
< 2; u
++) {
669 struct urb
*inurb
= sk
->inurb
[u
];
670 struct urb
*outurb
= sk
->outurb
[u
];
671 playback_prep_freqn(sk
, outurb
);
672 inurb
->number_of_packets
= outurb
->number_of_packets
;
673 inurb
->transfer_buffer_length
=
674 inurb
->number_of_packets
*
675 inurb
->iso_frame_desc
[0].length
;
679 struct usb_device
*dev
= inurb
->dev
;
680 frame
= usb_get_current_frame_number(dev
);
682 now
= usb_get_current_frame_number(dev
);
684 } while (now
> -1 && now
== frame
);
686 err
= usb_submit_urb(inurb
, GFP_ATOMIC
);
688 snd_printk(KERN_ERR
"usb_submit_urb(sk->inurb[%i])"
689 " returned %i\n", u
, err
);
692 err
= usb_submit_urb(outurb
, GFP_ATOMIC
);
694 snd_printk(KERN_ERR
"usb_submit_urb(sk->outurb[%i])"
695 " returned %i\n", u
, err
);
699 if (inurb
->start_frame
!= outurb
->start_frame
) {
700 snd_printd(KERN_DEBUG
701 "u[%i] start_frames differ in:%u out:%u\n",
702 u
, inurb
->start_frame
, outurb
->start_frame
);
706 snd_printdd(KERN_DEBUG
"%i %i\n", frame
, iters
);
713 snd_printd(KERN_DEBUG
"goto dotry;\n");
716 snd_printk(KERN_WARNING
"couldn't start"
717 " all urbs on the same start_frame.\n");
721 sk
->idle_inurb
= sk
->inurb
[USB_STREAM_NURBS
- 2];
722 sk
->idle_outurb
= sk
->outurb
[USB_STREAM_NURBS
- 2];
723 sk
->completed_inurb
= sk
->inurb
[USB_STREAM_NURBS
- 1];
724 sk
->completed_outurb
= sk
->outurb
[USB_STREAM_NURBS
- 1];
729 while (s
->state
!= usb_stream_ready
&& wait_ms
> 0) {
730 snd_printdd(KERN_DEBUG
"%i\n", s
->state
);
736 return s
->state
== usb_stream_ready
? 0 : -EFAULT
;
742 void usb_stream_stop(struct usb_stream_kernel
*sk
)
747 for (u
= 0; u
< USB_STREAM_NURBS
; ++u
) {
748 usb_kill_urb(sk
->inurb
[u
]);
749 usb_kill_urb(sk
->outurb
[u
]);
751 sk
->s
->state
= usb_stream_stopped
;