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>
21 #include "usb_stream.h"
26 static unsigned usb_stream_next_packet_size(struct usb_stream_kernel *sk)
28 struct usb_stream *s = sk->s;
29 sk->out_phase_peeked = (sk->out_phase & 0xffff) + sk->freqn;
30 return (sk->out_phase_peeked >> 16) * s->cfg.frame_size;
33 static void playback_prep_freqn(struct usb_stream_kernel *sk, struct urb *urb)
35 struct usb_stream *s = sk->s;
39 urb->iso_frame_desc[0].offset = 0;
40 urb->iso_frame_desc[0].length = usb_stream_next_packet_size(sk);
41 sk->out_phase = sk->out_phase_peeked;
42 urb->transfer_buffer_length = urb->iso_frame_desc[0].length;
44 for (pack = 1; pack < sk->n_o_ps; pack++) {
45 l = usb_stream_next_packet_size(sk);
46 if (s->idle_outsize + urb->transfer_buffer_length + l >
50 sk->out_phase = sk->out_phase_peeked;
51 urb->iso_frame_desc[pack].offset = urb->transfer_buffer_length;
52 urb->iso_frame_desc[pack].length = l;
53 urb->transfer_buffer_length += l;
55 snd_printdd(KERN_DEBUG "%i\n", urb->transfer_buffer_length);
58 urb->number_of_packets = pack;
59 s->idle_outsize += urb->transfer_buffer_length - s->period_size;
60 snd_printdd(KERN_DEBUG "idle=%i ul=%i ps=%i\n", s->idle_outsize,
61 urb->transfer_buffer_length, s->period_size);
64 static void init_pipe_urbs(struct usb_stream_kernel *sk, unsigned use_packsize,
65 struct urb **urbs, char *transfer,
66 struct usb_device *dev, int pipe)
69 int maxpacket = use_packsize ?
70 use_packsize : usb_maxpacket(dev, pipe, usb_pipeout(pipe));
71 int transfer_length = maxpacket * sk->n_o_ps;
73 for (u = 0; u < USB_STREAM_NURBS;
74 ++u, transfer += transfer_length) {
75 struct urb *urb = urbs[u];
76 struct usb_iso_packet_descriptor *desc;
77 urb->transfer_flags = URB_ISO_ASAP;
78 urb->transfer_buffer = transfer;
81 urb->number_of_packets = sk->n_o_ps;
84 if (usb_pipeout(pipe))
87 urb->transfer_buffer_length = transfer_length;
88 desc = urb->iso_frame_desc;
90 desc->length = maxpacket;
91 for (p = 1; p < sk->n_o_ps; ++p) {
92 desc[p].offset = desc[p - 1].offset + maxpacket;
93 desc[p].length = maxpacket;
98 static void init_urbs(struct usb_stream_kernel *sk, unsigned use_packsize,
99 struct usb_device *dev, int in_pipe, int out_pipe)
101 struct usb_stream *s = sk->s;
102 char *indata = (char *)s + sizeof(*s) +
103 sizeof(struct usb_stream_packet) *
107 for (u = 0; u < USB_STREAM_NURBS; ++u) {
108 sk->inurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
109 sk->outurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
112 init_pipe_urbs(sk, use_packsize, sk->inurb, indata, dev, in_pipe);
113 init_pipe_urbs(sk, use_packsize, sk->outurb, sk->write_page, dev,
119 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
120 * this will overflow at approx 524 kHz
122 static inline unsigned get_usb_full_speed_rate(unsigned rate)
124 return ((rate << 13) + 62) / 125;
128 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
129 * this will overflow at approx 4 MHz
131 static inline unsigned get_usb_high_speed_rate(unsigned rate)
133 return ((rate << 10) + 62) / 125;
136 void usb_stream_free(struct usb_stream_kernel *sk)
138 struct usb_stream *s;
141 for (u = 0; u < USB_STREAM_NURBS; ++u) {
142 usb_free_urb(sk->inurb[u]);
144 usb_free_urb(sk->outurb[u]);
145 sk->outurb[u] = NULL;
152 free_pages((unsigned long)sk->write_page, get_order(s->write_size));
153 sk->write_page = NULL;
154 free_pages((unsigned long)s, get_order(s->read_size));
158 struct usb_stream *usb_stream_new(struct usb_stream_kernel *sk,
159 struct usb_device *dev,
160 unsigned in_endpoint, unsigned out_endpoint,
161 unsigned sample_rate, unsigned use_packsize,
162 unsigned period_frames, unsigned frame_size)
164 int packets, max_packsize;
165 int in_pipe, out_pipe;
166 int read_size = sizeof(struct usb_stream);
168 int usb_frames = dev->speed == USB_SPEED_HIGH ? 8000 : 1000;
171 in_pipe = usb_rcvisocpipe(dev, in_endpoint);
172 out_pipe = usb_sndisocpipe(dev, out_endpoint);
174 max_packsize = use_packsize ?
175 use_packsize : usb_maxpacket(dev, in_pipe, 0);
178 t_period = period_frames / sample_rate
179 iso_packs = t_period / t_iso_frame
180 = (period_frames / sample_rate) * (1 / t_iso_frame)
183 packets = period_frames * usb_frames / sample_rate + 1;
185 if (dev->speed == USB_SPEED_HIGH)
186 packets = (packets + 7) & ~7;
188 read_size += packets * USB_STREAM_URBDEPTH *
189 (max_packsize + sizeof(struct usb_stream_packet));
191 max_packsize = usb_maxpacket(dev, out_pipe, 1);
192 write_size = max_packsize * packets * USB_STREAM_URBDEPTH;
194 if (read_size >= 256*PAGE_SIZE || write_size >= 256*PAGE_SIZE) {
195 snd_printk(KERN_WARNING "a size exceeds 128*PAGE_SIZE\n");
199 pg = get_order(read_size);
200 sk->s = (void *) __get_free_pages(GFP_KERNEL|__GFP_COMP|__GFP_ZERO, pg);
202 snd_printk(KERN_WARNING "couldn't __get_free_pages()\n");
205 sk->s->cfg.version = USB_STREAM_INTERFACE_VERSION;
207 sk->s->read_size = read_size;
209 sk->s->cfg.sample_rate = sample_rate;
210 sk->s->cfg.frame_size = frame_size;
211 sk->n_o_ps = packets;
212 sk->s->inpackets = packets * USB_STREAM_URBDEPTH;
213 sk->s->cfg.period_frames = period_frames;
214 sk->s->period_size = frame_size * period_frames;
216 sk->s->write_size = write_size;
217 pg = get_order(write_size);
220 (void *)__get_free_pages(GFP_KERNEL|__GFP_COMP|__GFP_ZERO, pg);
221 if (!sk->write_page) {
222 snd_printk(KERN_WARNING "couldn't __get_free_pages()\n");
227 /* calculate the frequency in 16.16 format */
228 if (dev->speed == USB_SPEED_FULL)
229 sk->freqn = get_usb_full_speed_rate(sample_rate);
231 sk->freqn = get_usb_high_speed_rate(sample_rate);
233 init_urbs(sk, use_packsize, dev, in_pipe, out_pipe);
234 sk->s->state = usb_stream_stopped;
242 static bool balance_check(struct usb_stream_kernel *sk, struct urb *urb)
245 if (unlikely(urb->status)) {
246 if (urb->status != -ESHUTDOWN && urb->status != -ENOENT)
247 snd_printk(KERN_WARNING "status=%i\n", urb->status);
248 sk->iso_frame_balance = 0x7FFFFFFF;
251 r = sk->iso_frame_balance == 0;
257 static bool balance_playback(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 bool balance_capture(struct usb_stream_kernel *sk, struct urb *urb)
265 sk->iso_frame_balance -= urb->number_of_packets;
266 return balance_check(sk, urb);
269 static void subs_set_complete(struct urb **urbs, void (*complete)(struct urb *))
273 for (u = 0; u < USB_STREAM_NURBS; u++) {
274 struct urb *urb = urbs[u];
275 urb->complete = complete;
279 static int usb_stream_prepare_playback(struct usb_stream_kernel *sk,
282 struct usb_stream *s = sk->s;
284 struct usb_iso_packet_descriptor *id, *od;
287 io = sk->idle_outurb;
288 od = io->iso_frame_desc;
289 io->transfer_buffer_length = 0;
291 for (p = 0; s->sync_packet < 0; ++p, ++s->sync_packet) {
292 struct urb *ii = sk->completed_inurb;
293 id = ii->iso_frame_desc +
294 ii->number_of_packets + s->sync_packet;
295 l = id->actual_length;
298 od[p].offset = io->transfer_buffer_length;
299 io->transfer_buffer_length += l;
303 s->sync_packet < inurb->number_of_packets && p < sk->n_o_ps;
304 ++p, ++s->sync_packet) {
305 l = inurb->iso_frame_desc[s->sync_packet].actual_length;
307 if (s->idle_outsize + io->transfer_buffer_length + l >
312 od[p].offset = io->transfer_buffer_length;
313 io->transfer_buffer_length += l;
317 s->sync_packet -= inurb->number_of_packets;
318 if (s->sync_packet < -2 || s->sync_packet > 0) {
319 snd_printk(KERN_WARNING "invalid sync_packet = %i;"
320 " p=%i nop=%i %i %x %x %x > %x\n",
321 s->sync_packet, p, inurb->number_of_packets,
322 s->idle_outsize + io->transfer_buffer_length + l,
323 s->idle_outsize, io->transfer_buffer_length, l,
327 if (io->transfer_buffer_length % s->cfg.frame_size) {
328 snd_printk(KERN_WARNING"invalid outsize = %i\n",
329 io->transfer_buffer_length);
332 s->idle_outsize += io->transfer_buffer_length - s->period_size;
333 io->number_of_packets = p;
334 if (s->idle_outsize > 0) {
335 snd_printk(KERN_WARNING "idle=%i\n", s->idle_outsize);
341 static void prepare_inurb(int number_of_packets, struct urb *iu)
343 struct usb_iso_packet_descriptor *id;
346 iu->number_of_packets = number_of_packets;
347 id = iu->iso_frame_desc;
349 for (p = 0; p < iu->number_of_packets - 1; ++p)
350 id[p + 1].offset = id[p].offset + id[p].length;
352 iu->transfer_buffer_length =
353 id[0].length * iu->number_of_packets;
356 static int submit_urbs(struct usb_stream_kernel *sk,
357 struct urb *inurb, struct urb *outurb)
360 prepare_inurb(sk->idle_outurb->number_of_packets, sk->idle_inurb);
361 err = usb_submit_urb(sk->idle_inurb, GFP_ATOMIC);
363 snd_printk(KERN_ERR "%i\n", err);
366 sk->idle_inurb = sk->completed_inurb;
367 sk->completed_inurb = inurb;
368 err = usb_submit_urb(sk->idle_outurb, GFP_ATOMIC);
370 snd_printk(KERN_ERR "%i\n", err);
373 sk->idle_outurb = sk->completed_outurb;
374 sk->completed_outurb = outurb;
378 #ifdef DEBUG_LOOP_BACK
380 This loop_back() shows how to read/write the period data.
382 static void loop_back(struct usb_stream *s)
387 struct usb_iso_packet_descriptor *id;
389 o = s->playback1st_to;
390 ol = s->playback1st_size;
393 if (s->insplit_pack >= 0) {
395 id = iu->iso_frame_desc;
400 for (; p < iu->number_of_packets && l < s->period_size; ++p) {
401 i = iu->transfer_buffer + id[p].offset;
402 il = id[p].actual_length;
403 if (l + il > s->period_size)
404 il = s->period_size - l;
413 memcpy(o, i + ol, il - ol);
415 ol = s->period_size - s->playback1st_size;
419 if (iu == sk->completed_inurb) {
420 if (l != s->period_size)
421 printk(KERN_DEBUG"%s:%i %i\n", __func__, __LINE__,
422 l/(int)s->cfg.frame_size);
427 iu = sk->completed_inurb;
428 id = iu->iso_frame_desc;
434 static void loop_back(struct usb_stream *s)
439 static void stream_idle(struct usb_stream_kernel *sk,
440 struct urb *inurb, struct urb *outurb)
442 struct usb_stream *s = sk->s;
444 int insize = s->idle_insize;
447 s->inpacket_split = s->next_inpacket_split;
448 s->inpacket_split_at = s->next_inpacket_split_at;
449 s->next_inpacket_split = -1;
450 s->next_inpacket_split_at = 0;
452 for (p = 0; p < inurb->number_of_packets; ++p) {
453 struct usb_iso_packet_descriptor *id = inurb->iso_frame_desc;
454 l = id[p].actual_length;
455 if (unlikely(l == 0 || id[p].status)) {
456 snd_printk(KERN_WARNING "underrun, status=%u\n",
461 s->inpacket_head %= s->inpackets;
462 if (s->inpacket_split == -1)
463 s->inpacket_split = s->inpacket_head;
465 s->inpacket[s->inpacket_head].offset =
466 id[p].offset + (inurb->transfer_buffer - (void *)s);
467 s->inpacket[s->inpacket_head].length = l;
468 if (insize + l > s->period_size &&
469 s->next_inpacket_split == -1) {
470 s->next_inpacket_split = s->inpacket_head;
471 s->next_inpacket_split_at = s->period_size - insize;
476 s->idle_insize += urb_size - s->period_size;
477 if (s->idle_insize < 0) {
478 snd_printk(KERN_WARNING "%i\n",
479 (s->idle_insize)/(int)s->cfg.frame_size);
482 s->insize_done += urb_size;
485 s->outpacket[0].offset = (sk->idle_outurb->transfer_buffer -
488 if (usb_stream_prepare_playback(sk, inurb) < 0)
491 s->outpacket[0].length = sk->idle_outurb->transfer_buffer_length + l;
492 s->outpacket[1].offset = sk->completed_outurb->transfer_buffer -
495 if (submit_urbs(sk, inurb, outurb) < 0)
500 wake_up_all(&sk->sleep);
503 s->state = usb_stream_xrun;
504 wake_up_all(&sk->sleep);
507 static void i_capture_idle(struct urb *urb)
509 struct usb_stream_kernel *sk = urb->context;
510 if (balance_capture(sk, urb))
511 stream_idle(sk, urb, sk->i_urb);
514 static void i_playback_idle(struct urb *urb)
516 struct usb_stream_kernel *sk = urb->context;
517 if (balance_playback(sk, urb))
518 stream_idle(sk, sk->i_urb, urb);
521 static void stream_start(struct usb_stream_kernel *sk,
522 struct urb *inurb, struct urb *outurb)
524 struct usb_stream *s = sk->s;
525 if (s->state >= usb_stream_sync1) {
526 int l, p, max_diff, max_diff_0;
528 unsigned frames_per_packet, min_frames = 0;
529 frames_per_packet = (s->period_size - s->idle_insize);
530 frames_per_packet <<= 8;
532 s->cfg.frame_size * inurb->number_of_packets;
535 max_diff_0 = s->cfg.frame_size;
536 if (s->cfg.period_frames >= 256)
538 if (s->cfg.period_frames >= 1024)
540 max_diff = max_diff_0;
541 for (p = 0; p < inurb->number_of_packets; ++p) {
543 l = inurb->iso_frame_desc[p].actual_length;
546 min_frames += frames_per_packet;
548 (min_frames >> 8) * s->cfg.frame_size;
549 if (diff < max_diff) {
550 snd_printdd(KERN_DEBUG "%i %i %i %i\n",
552 urb_size / (int)s->cfg.frame_size,
553 inurb->number_of_packets, diff);
557 s->idle_insize -= max_diff - max_diff_0;
558 s->idle_insize += urb_size - s->period_size;
559 if (s->idle_insize < 0) {
560 snd_printk(KERN_WARNING "%i %i %i\n",
561 s->idle_insize, urb_size, s->period_size);
563 } else if (s->idle_insize == 0) {
564 s->next_inpacket_split =
565 (s->inpacket_head + 1) % s->inpackets;
566 s->next_inpacket_split_at = 0;
568 unsigned split = s->inpacket_head;
570 while (l > s->inpacket[split].length) {
571 l -= s->inpacket[split].length;
573 split = s->inpackets - 1;
577 s->next_inpacket_split = split;
578 s->next_inpacket_split_at =
579 s->inpacket[split].length - l;
582 s->insize_done += urb_size;
584 if (usb_stream_prepare_playback(sk, inurb) < 0)
588 playback_prep_freqn(sk, sk->idle_outurb);
590 if (submit_urbs(sk, inurb, outurb) < 0)
593 if (s->state == usb_stream_sync1 && s->insize_done > 360000) {
594 /* just guesswork ^^^^^^ */
595 s->state = usb_stream_ready;
596 subs_set_complete(sk->inurb, i_capture_idle);
597 subs_set_complete(sk->outurb, i_playback_idle);
601 static void i_capture_start(struct urb *urb)
603 struct usb_iso_packet_descriptor *id = urb->iso_frame_desc;
604 struct usb_stream_kernel *sk = urb->context;
605 struct usb_stream *s = sk->s;
610 snd_printk(KERN_WARNING "status=%i\n", urb->status);
614 for (p = 0; p < urb->number_of_packets; ++p) {
615 int l = id[p].actual_length;
616 if (l < s->cfg.frame_size) {
618 if (s->state >= usb_stream_sync0) {
619 snd_printk(KERN_WARNING "%i\n", l);
624 s->inpacket_head %= s->inpackets;
625 s->inpacket[s->inpacket_head].offset =
626 id[p].offset + (urb->transfer_buffer - (void *)s);
627 s->inpacket[s->inpacket_head].length = l;
631 printk(KERN_DEBUG"%s:%i: %i", __func__, __LINE__,
632 urb->iso_frame_desc[0].actual_length);
633 for (pack = 1; pack < urb->number_of_packets; ++pack) {
634 int l = urb->iso_frame_desc[pack].actual_length;
640 if (!empty && s->state < usb_stream_sync1)
643 if (balance_capture(sk, urb))
644 stream_start(sk, urb, sk->i_urb);
647 static void i_playback_start(struct urb *urb)
649 struct usb_stream_kernel *sk = urb->context;
650 if (balance_playback(sk, urb))
651 stream_start(sk, sk->i_urb, urb);
654 int usb_stream_start(struct usb_stream_kernel *sk)
656 struct usb_stream *s = sk->s;
657 int frame = 0, iters = 0;
661 if (s->state != usb_stream_stopped)
664 subs_set_complete(sk->inurb, i_capture_start);
665 subs_set_complete(sk->outurb, i_playback_start);
666 memset(sk->write_page, 0, s->write_size);
672 s->inpacket_head = -1;
673 sk->iso_frame_balance = 0;
675 for (u = 0; u < 2; u++) {
676 struct urb *inurb = sk->inurb[u];
677 struct urb *outurb = sk->outurb[u];
678 playback_prep_freqn(sk, outurb);
679 inurb->number_of_packets = outurb->number_of_packets;
680 inurb->transfer_buffer_length =
681 inurb->number_of_packets *
682 inurb->iso_frame_desc[0].length;
686 struct usb_device *dev = inurb->dev;
687 frame = usb_get_current_frame_number(dev);
689 now = usb_get_current_frame_number(dev);
691 } while (now > -1 && now == frame);
693 err = usb_submit_urb(inurb, GFP_ATOMIC);
696 snd_printk(KERN_ERR"usb_submit_urb(sk->inurb[%i])"
697 " returned %i\n", u, err);
700 err = usb_submit_urb(outurb, GFP_ATOMIC);
703 snd_printk(KERN_ERR"usb_submit_urb(sk->outurb[%i])"
704 " returned %i\n", u, err);
708 if (inurb->start_frame != outurb->start_frame) {
709 snd_printd(KERN_DEBUG
710 "u[%i] start_frames differ in:%u out:%u\n",
711 u, inurb->start_frame, outurb->start_frame);
715 snd_printdd(KERN_DEBUG "%i %i\n", frame, iters);
722 snd_printd(KERN_DEBUG "goto dotry;\n");
725 snd_printk(KERN_WARNING"couldn't start"
726 " all urbs on the same start_frame.\n");
730 sk->idle_inurb = sk->inurb[USB_STREAM_NURBS - 2];
731 sk->idle_outurb = sk->outurb[USB_STREAM_NURBS - 2];
732 sk->completed_inurb = sk->inurb[USB_STREAM_NURBS - 1];
733 sk->completed_outurb = sk->outurb[USB_STREAM_NURBS - 1];
738 while (s->state != usb_stream_ready && wait_ms > 0) {
739 snd_printdd(KERN_DEBUG "%i\n", s->state);
745 return s->state == usb_stream_ready ? 0 : -EFAULT;
751 void usb_stream_stop(struct usb_stream_kernel *sk)
756 for (u = 0; u < USB_STREAM_NURBS; ++u) {
757 usb_kill_urb(sk->inurb[u]);
758 usb_kill_urb(sk->outurb[u]);
760 sk->s->state = usb_stream_stopped;