27 static unsigned usb_stream_next_packet_size(
struct usb_stream_kernel *
sk)
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)
39 for (pack = 0; pack < sk->n_o_ps; pack++) {
40 int l = usb_stream_next_packet_size(sk);
44 sk->out_phase = sk->out_phase_peeked;
45 urb->iso_frame_desc[pack].offset = lb;
46 urb->iso_frame_desc[pack].length =
l;
52 urb->number_of_packets = pack;
53 urb->transfer_buffer_length = lb;
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_flags = URB_ISO_ASAP;
73 urb->transfer_buffer = transfer;
76 urb->number_of_packets = sk->n_o_ps;
79 if (usb_pipeout(pipe))
82 urb->transfer_buffer_length = transfer_length;
83 desc = urb->iso_frame_desc;
85 desc->length = maxpacket;
86 for (p = 1; p < sk->n_o_ps; ++
p) {
87 desc[
p].offset = desc[p - 1].offset + maxpacket;
88 desc[
p].length = maxpacket;
93 static void init_urbs(
struct usb_stream_kernel *sk,
unsigned use_packsize,
94 struct usb_device *dev,
int in_pipe,
int out_pipe)
97 char *indata = (
char *)s +
sizeof(*s) +
102 for (u = 0; u < USB_STREAM_NURBS; ++
u) {
107 init_pipe_urbs(sk, use_packsize, sk->inurb, indata, dev, in_pipe);
108 init_pipe_urbs(sk, use_packsize, sk->outurb, sk->write_page, dev,
117 static inline unsigned get_usb_full_speed_rate(
unsigned rate)
119 return ((rate << 13) + 62) / 125;
126 static inline unsigned get_usb_high_speed_rate(
unsigned rate)
128 return ((rate << 10) + 62) / 125;
136 for (u = 0; u < USB_STREAM_NURBS; ++
u) {
140 sk->outurb[
u] =
NULL;
148 sk->write_page =
NULL;
154 struct usb_device *dev,
155 unsigned in_endpoint,
unsigned out_endpoint,
156 unsigned sample_rate,
unsigned use_packsize,
157 unsigned period_frames,
unsigned frame_size)
160 int in_pipe, out_pipe;
166 in_pipe = usb_rcvisocpipe(dev, in_endpoint);
167 out_pipe = usb_sndisocpipe(dev, out_endpoint);
169 max_packsize = use_packsize ?
170 use_packsize : usb_maxpacket(dev, in_pipe, 0);
178 packets = period_frames * usb_frames / sample_rate + 1;
181 packets = (packets + 7) & ~7;
183 read_size += packets * USB_STREAM_URBDEPTH *
186 max_packsize = usb_maxpacket(dev, out_pipe, 1);
187 write_size = max_packsize * packets * USB_STREAM_URBDEPTH;
204 sk->s->cfg.sample_rate = sample_rate;
205 sk->s->cfg.frame_size = frame_size;
207 sk->s->inpackets = packets * USB_STREAM_URBDEPTH;
208 sk->s->cfg.period_frames = period_frames;
209 sk->s->period_size = frame_size * period_frames;
216 if (!sk->write_page) {
224 sk->freqn = get_usb_full_speed_rate(sample_rate);
226 sk->freqn = get_usb_high_speed_rate(sample_rate);
228 init_urbs(sk, use_packsize, dev, in_pipe, out_pipe);
237 static bool balance_check(
struct usb_stream_kernel *sk,
struct urb *urb)
243 sk->iso_frame_balance = 0x7FFFFFFF;
246 r = sk->iso_frame_balance == 0;
252 static bool balance_playback(
struct usb_stream_kernel *sk,
struct urb *urb)
254 sk->iso_frame_balance += urb->number_of_packets;
255 return balance_check(sk, urb);
258 static bool balance_capture(
struct usb_stream_kernel *sk,
struct urb *urb)
260 sk->iso_frame_balance -= urb->number_of_packets;
261 return balance_check(sk, urb);
264 static void subs_set_complete(
struct urb **urbs,
void (*
complete)(
struct urb *))
268 for (u = 0; u < USB_STREAM_NURBS; u++) {
269 struct urb *urb = urbs[
u];
274 static int usb_stream_prepare_playback(
struct usb_stream_kernel *sk,
279 struct usb_iso_packet_descriptor *
id, *od;
280 int p = 0, lb = 0,
l = 0;
282 io = sk->idle_outurb;
283 od = io->iso_frame_desc;
286 struct urb *ii = sk->completed_inurb;
287 id = ii->iso_frame_desc +
289 l =
id->actual_length;
297 s->
sync_packet < inurb->number_of_packets && p < sk->n_o_ps;
299 l = inurb->iso_frame_desc[s->
sync_packet].actual_length;
313 " p=%i nop=%i %i %x %x %x > %x\n",
326 io->number_of_packets =
p;
327 io->transfer_buffer_length = lb;
335 static void prepare_inurb(
int number_of_packets,
struct urb *
iu)
337 struct usb_iso_packet_descriptor *
id;
340 iu->number_of_packets = number_of_packets;
341 id = iu->iso_frame_desc;
343 for (p = 0; p < iu->number_of_packets - 1; ++
p)
346 iu->transfer_buffer_length =
347 id[0].length * iu->number_of_packets;
350 static int submit_urbs(
struct usb_stream_kernel *sk,
351 struct urb *inurb,
struct urb *outurb)
354 prepare_inurb(sk->idle_outurb->number_of_packets, sk->idle_inurb);
360 sk->idle_inurb = sk->completed_inurb;
361 sk->completed_inurb = inurb;
367 sk->idle_outurb = sk->completed_outurb;
368 sk->completed_outurb = outurb;
372 #ifdef DEBUG_LOOP_BACK
381 struct usb_iso_packet_descriptor *
id;
383 o = s->playback1st_to;
384 ol = s->playback1st_size;
387 if (s->insplit_pack >= 0) {
389 id = iu->iso_frame_desc;
394 for (; p < iu->number_of_packets && l < s->
period_size; ++
p) {
395 i = iu->transfer_buffer +
id[
p].offset;
396 il =
id[
p].actual_length;
407 memcpy(o, i + ol, il - ol);
413 if (iu == sk->completed_inurb) {
416 l/(
int)s->
cfg.frame_size);
421 iu = sk->completed_inurb;
422 id = iu->iso_frame_desc;
433 static void stream_idle(
struct usb_stream_kernel *sk,
434 struct urb *inurb,
struct urb *outurb)
446 for (p = 0; p < inurb->number_of_packets; ++
p) {
447 struct usb_iso_packet_descriptor *
id = inurb->iso_frame_desc;
448 l =
id[
p].actual_length;
460 id[
p].offset + (inurb->transfer_buffer - (
void *)s);
479 s->
outpacket[0].offset = (sk->idle_outurb->transfer_buffer -
482 if (usb_stream_prepare_playback(sk, inurb) < 0)
485 s->
outpacket[0].length = sk->idle_outurb->transfer_buffer_length +
l;
486 s->
outpacket[1].offset = sk->completed_outurb->transfer_buffer -
489 if (submit_urbs(sk, inurb, outurb) < 0)
501 static void i_capture_idle(
struct urb *urb)
503 struct usb_stream_kernel *sk = urb->context;
504 if (balance_capture(sk, urb))
508 static void i_playback_idle(
struct urb *urb)
510 struct usb_stream_kernel *sk = urb->context;
511 if (balance_playback(sk, urb))
515 static void stream_start(
struct usb_stream_kernel *sk,
516 struct urb *inurb,
struct urb *outurb)
520 int l,
p, max_diff, max_diff_0;
522 unsigned frames_per_packet, min_frames = 0;
524 frames_per_packet <<= 8;
526 s->
cfg.frame_size * inurb->number_of_packets;
529 max_diff_0 = s->
cfg.frame_size;
530 if (s->
cfg.period_frames >= 256)
532 if (s->
cfg.period_frames >= 1024)
534 max_diff = max_diff_0;
535 for (p = 0; p < inurb->number_of_packets; ++
p) {
537 l = inurb->iso_frame_desc[
p].actual_length;
540 min_frames += frames_per_packet;
542 (min_frames >> 8) * s->
cfg.frame_size;
543 if (diff < max_diff) {
546 urb_size / (
int)s->
cfg.frame_size,
547 inurb->number_of_packets, diff);
564 while (l > s->
inpacket[split].length) {
578 if (usb_stream_prepare_playback(sk, inurb) < 0)
582 playback_prep_freqn(sk, sk->idle_outurb);
584 if (submit_urbs(sk, inurb, outurb) < 0)
590 subs_set_complete(sk->inurb, i_capture_idle);
591 subs_set_complete(sk->outurb, i_playback_idle);
595 static void i_capture_start(
struct urb *urb)
597 struct usb_iso_packet_descriptor *
id = urb->iso_frame_desc;
598 struct usb_stream_kernel *sk = urb->context;
608 for (p = 0; p < urb->number_of_packets; ++
p) {
609 int l =
id[
p].actual_length;
610 if (l < s->
cfg.frame_size) {
620 id[
p].offset + (urb->transfer_buffer - (
void *)s);
626 urb->iso_frame_desc[0].actual_length);
627 for (pack = 1; pack < urb->number_of_packets; ++pack) {
628 int l = urb->iso_frame_desc[pack].actual_length;
637 if (balance_capture(sk, urb))
638 stream_start(sk, urb, sk->i_urb);
641 static void i_playback_start(
struct urb *urb)
643 struct usb_stream_kernel *sk = urb->context;
644 if (balance_playback(sk, urb))
645 stream_start(sk, sk->i_urb, urb);
651 int frame = 0, iters = 0;
658 subs_set_complete(sk->inurb, i_capture_start);
659 subs_set_complete(sk->outurb, i_playback_start);
667 sk->iso_frame_balance = 0;
669 for (u = 0; u < 2; u++) {
670 struct urb *inurb = sk->inurb[
u];
671 struct urb *outurb = sk->outurb[
u];
672 playback_prep_freqn(sk, outurb);
673 inurb->number_of_packets = outurb->number_of_packets;
674 inurb->transfer_buffer_length =
675 inurb->number_of_packets *
676 inurb->iso_frame_desc[0].length;
680 struct usb_device *dev = inurb->dev;
685 }
while (now > -1 && now == frame);
690 " returned %i\n", u, err);
696 " returned %i\n", u, err);
700 if (inurb->start_frame != outurb->start_frame) {
702 "u[%i] start_frames differ in:%u out:%u\n",
703 u, inurb->start_frame, outurb->start_frame);
718 " all urbs on the same start_frame.\n");
722 sk->idle_inurb = sk->inurb[USB_STREAM_NURBS - 2];
723 sk->idle_outurb = sk->outurb[USB_STREAM_NURBS - 2];
724 sk->completed_inurb = sk->inurb[USB_STREAM_NURBS - 1];
725 sk->completed_outurb = sk->outurb[USB_STREAM_NURBS - 1];
748 for (u = 0; u < USB_STREAM_NURBS; ++
u) {