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 int 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))
80 if (usb_urb_ep_type_check(urb))
83 urb->transfer_buffer_length = transfer_length;
84 desc = urb->iso_frame_desc;
86 desc->length = maxpacket;
87 for (p = 1; p < sk->n_o_ps; ++p) {
88 desc[p].offset = desc[p - 1].offset + maxpacket;
89 desc[p].length = maxpacket;
96 static int init_urbs(struct usb_stream_kernel *sk, unsigned use_packsize,
97 struct usb_device *dev, int in_pipe, int out_pipe)
99 struct usb_stream *s = sk->s;
100 char *indata = (char *)s + sizeof(*s) +
101 sizeof(struct usb_stream_packet) *
105 for (u = 0; u < USB_STREAM_NURBS; ++u) {
106 sk->inurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
110 sk->outurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
115 if (init_pipe_urbs(sk, use_packsize, sk->inurb, indata, dev, in_pipe) ||
116 init_pipe_urbs(sk, use_packsize, sk->outurb, sk->write_page, dev,
125 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
126 * this will overflow at approx 524 kHz
128 static inline unsigned get_usb_full_speed_rate(unsigned rate)
130 return ((rate << 13) + 62) / 125;
134 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
135 * this will overflow at approx 4 MHz
137 static inline unsigned get_usb_high_speed_rate(unsigned rate)
139 return ((rate << 10) + 62) / 125;
142 void usb_stream_free(struct usb_stream_kernel *sk)
144 struct usb_stream *s;
147 for (u = 0; u < USB_STREAM_NURBS; ++u) {
148 usb_free_urb(sk->inurb[u]);
150 usb_free_urb(sk->outurb[u]);
151 sk->outurb[u] = NULL;
158 free_pages((unsigned long)sk->write_page, get_order(s->write_size));
159 sk->write_page = NULL;
160 free_pages((unsigned long)s, get_order(s->read_size));
164 struct usb_stream *usb_stream_new(struct usb_stream_kernel *sk,
165 struct usb_device *dev,
166 unsigned in_endpoint, unsigned out_endpoint,
167 unsigned sample_rate, unsigned use_packsize,
168 unsigned period_frames, unsigned frame_size)
170 int packets, max_packsize;
171 int in_pipe, out_pipe;
172 int read_size = sizeof(struct usb_stream);
174 int usb_frames = dev->speed == USB_SPEED_HIGH ? 8000 : 1000;
177 in_pipe = usb_rcvisocpipe(dev, in_endpoint);
178 out_pipe = usb_sndisocpipe(dev, out_endpoint);
180 max_packsize = use_packsize ?
181 use_packsize : usb_maxpacket(dev, in_pipe, 0);
184 t_period = period_frames / sample_rate
185 iso_packs = t_period / t_iso_frame
186 = (period_frames / sample_rate) * (1 / t_iso_frame)
189 packets = period_frames * usb_frames / sample_rate + 1;
191 if (dev->speed == USB_SPEED_HIGH)
192 packets = (packets + 7) & ~7;
194 read_size += packets * USB_STREAM_URBDEPTH *
195 (max_packsize + sizeof(struct usb_stream_packet));
197 max_packsize = usb_maxpacket(dev, out_pipe, 1);
198 write_size = max_packsize * packets * USB_STREAM_URBDEPTH;
200 if (read_size >= 256*PAGE_SIZE || write_size >= 256*PAGE_SIZE) {
201 snd_printk(KERN_WARNING "a size exceeds 128*PAGE_SIZE\n");
205 pg = get_order(read_size);
206 sk->s = (void *) __get_free_pages(GFP_KERNEL|__GFP_COMP|__GFP_ZERO|
209 snd_printk(KERN_WARNING "couldn't __get_free_pages()\n");
212 sk->s->cfg.version = USB_STREAM_INTERFACE_VERSION;
214 sk->s->read_size = read_size;
216 sk->s->cfg.sample_rate = sample_rate;
217 sk->s->cfg.frame_size = frame_size;
218 sk->n_o_ps = packets;
219 sk->s->inpackets = packets * USB_STREAM_URBDEPTH;
220 sk->s->cfg.period_frames = period_frames;
221 sk->s->period_size = frame_size * period_frames;
223 sk->s->write_size = write_size;
224 pg = get_order(write_size);
227 (void *)__get_free_pages(GFP_KERNEL|__GFP_COMP|__GFP_ZERO|
229 if (!sk->write_page) {
230 snd_printk(KERN_WARNING "couldn't __get_free_pages()\n");
235 /* calculate the frequency in 16.16 format */
236 if (dev->speed == USB_SPEED_FULL)
237 sk->freqn = get_usb_full_speed_rate(sample_rate);
239 sk->freqn = get_usb_high_speed_rate(sample_rate);
241 if (init_urbs(sk, use_packsize, dev, in_pipe, out_pipe) < 0) {
246 sk->s->state = usb_stream_stopped;
254 static bool balance_check(struct usb_stream_kernel *sk, struct urb *urb)
257 if (unlikely(urb->status)) {
258 if (urb->status != -ESHUTDOWN && urb->status != -ENOENT)
259 snd_printk(KERN_WARNING "status=%i\n", urb->status);
260 sk->iso_frame_balance = 0x7FFFFFFF;
263 r = sk->iso_frame_balance == 0;
269 static bool balance_playback(struct usb_stream_kernel *sk, struct urb *urb)
271 sk->iso_frame_balance += urb->number_of_packets;
272 return balance_check(sk, urb);
275 static bool balance_capture(struct usb_stream_kernel *sk, struct urb *urb)
277 sk->iso_frame_balance -= urb->number_of_packets;
278 return balance_check(sk, urb);
281 static void subs_set_complete(struct urb **urbs, void (*complete)(struct urb *))
285 for (u = 0; u < USB_STREAM_NURBS; u++) {
286 struct urb *urb = urbs[u];
287 urb->complete = complete;
291 static int usb_stream_prepare_playback(struct usb_stream_kernel *sk,
294 struct usb_stream *s = sk->s;
296 struct usb_iso_packet_descriptor *id, *od;
297 int p = 0, lb = 0, l = 0;
299 io = sk->idle_outurb;
300 od = io->iso_frame_desc;
302 for (; s->sync_packet < 0; ++p, ++s->sync_packet) {
303 struct urb *ii = sk->completed_inurb;
304 id = ii->iso_frame_desc +
305 ii->number_of_packets + s->sync_packet;
306 l = id->actual_length;
314 s->sync_packet < inurb->number_of_packets && p < sk->n_o_ps;
315 ++p, ++s->sync_packet) {
316 l = inurb->iso_frame_desc[s->sync_packet].actual_length;
318 if (s->idle_outsize + lb + l > s->period_size)
327 s->sync_packet -= inurb->number_of_packets;
328 if (unlikely(s->sync_packet < -2 || s->sync_packet > 0)) {
329 snd_printk(KERN_WARNING "invalid sync_packet = %i;"
330 " p=%i nop=%i %i %x %x %x > %x\n",
331 s->sync_packet, p, inurb->number_of_packets,
332 s->idle_outsize + lb + l,
333 s->idle_outsize, lb, l,
337 if (unlikely(lb % s->cfg.frame_size)) {
338 snd_printk(KERN_WARNING"invalid outsize = %i\n",
342 s->idle_outsize += lb - s->period_size;
343 io->number_of_packets = p;
344 io->transfer_buffer_length = lb;
345 if (s->idle_outsize <= 0)
348 snd_printk(KERN_WARNING "idle=%i\n", s->idle_outsize);
352 static void prepare_inurb(int number_of_packets, struct urb *iu)
354 struct usb_iso_packet_descriptor *id;
357 iu->number_of_packets = number_of_packets;
358 id = iu->iso_frame_desc;
360 for (p = 0; p < iu->number_of_packets - 1; ++p)
361 id[p + 1].offset = id[p].offset + id[p].length;
363 iu->transfer_buffer_length =
364 id[0].length * iu->number_of_packets;
367 static int submit_urbs(struct usb_stream_kernel *sk,
368 struct urb *inurb, struct urb *outurb)
371 prepare_inurb(sk->idle_outurb->number_of_packets, sk->idle_inurb);
372 err = usb_submit_urb(sk->idle_inurb, GFP_ATOMIC);
376 sk->idle_inurb = sk->completed_inurb;
377 sk->completed_inurb = inurb;
378 err = usb_submit_urb(sk->idle_outurb, GFP_ATOMIC);
382 sk->idle_outurb = sk->completed_outurb;
383 sk->completed_outurb = outurb;
387 snd_printk(KERN_ERR "%i\n", err);
391 #ifdef DEBUG_LOOP_BACK
393 This loop_back() shows how to read/write the period data.
395 static void loop_back(struct usb_stream *s)
400 struct usb_iso_packet_descriptor *id;
402 o = s->playback1st_to;
403 ol = s->playback1st_size;
406 if (s->insplit_pack >= 0) {
408 id = iu->iso_frame_desc;
413 for (; p < iu->number_of_packets && l < s->period_size; ++p) {
414 i = iu->transfer_buffer + id[p].offset;
415 il = id[p].actual_length;
416 if (l + il > s->period_size)
417 il = s->period_size - l;
426 memcpy(o, i + ol, il - ol);
428 ol = s->period_size - s->playback1st_size;
432 if (iu == sk->completed_inurb) {
433 if (l != s->period_size)
434 printk(KERN_DEBUG"%s:%i %i\n", __func__, __LINE__,
435 l/(int)s->cfg.frame_size);
440 iu = sk->completed_inurb;
441 id = iu->iso_frame_desc;
447 static void loop_back(struct usb_stream *s)
452 static void stream_idle(struct usb_stream_kernel *sk,
453 struct urb *inurb, struct urb *outurb)
455 struct usb_stream *s = sk->s;
457 int insize = s->idle_insize;
460 s->inpacket_split = s->next_inpacket_split;
461 s->inpacket_split_at = s->next_inpacket_split_at;
462 s->next_inpacket_split = -1;
463 s->next_inpacket_split_at = 0;
465 for (p = 0; p < inurb->number_of_packets; ++p) {
466 struct usb_iso_packet_descriptor *id = inurb->iso_frame_desc;
467 l = id[p].actual_length;
468 if (unlikely(l == 0 || id[p].status)) {
469 snd_printk(KERN_WARNING "underrun, status=%u\n",
474 s->inpacket_head %= s->inpackets;
475 if (s->inpacket_split == -1)
476 s->inpacket_split = s->inpacket_head;
478 s->inpacket[s->inpacket_head].offset =
479 id[p].offset + (inurb->transfer_buffer - (void *)s);
480 s->inpacket[s->inpacket_head].length = l;
481 if (insize + l > s->period_size &&
482 s->next_inpacket_split == -1) {
483 s->next_inpacket_split = s->inpacket_head;
484 s->next_inpacket_split_at = s->period_size - insize;
489 s->idle_insize += urb_size - s->period_size;
490 if (s->idle_insize < 0) {
491 snd_printk(KERN_WARNING "%i\n",
492 (s->idle_insize)/(int)s->cfg.frame_size);
495 s->insize_done += urb_size;
498 s->outpacket[0].offset = (sk->idle_outurb->transfer_buffer -
501 if (usb_stream_prepare_playback(sk, inurb) < 0)
504 s->outpacket[0].length = sk->idle_outurb->transfer_buffer_length + l;
505 s->outpacket[1].offset = sk->completed_outurb->transfer_buffer -
508 if (submit_urbs(sk, inurb, outurb) < 0)
513 wake_up_all(&sk->sleep);
516 s->state = usb_stream_xrun;
517 wake_up_all(&sk->sleep);
520 static void i_capture_idle(struct urb *urb)
522 struct usb_stream_kernel *sk = urb->context;
523 if (balance_capture(sk, urb))
524 stream_idle(sk, urb, sk->i_urb);
527 static void i_playback_idle(struct urb *urb)
529 struct usb_stream_kernel *sk = urb->context;
530 if (balance_playback(sk, urb))
531 stream_idle(sk, sk->i_urb, urb);
534 static void stream_start(struct usb_stream_kernel *sk,
535 struct urb *inurb, struct urb *outurb)
537 struct usb_stream *s = sk->s;
538 if (s->state >= usb_stream_sync1) {
539 int l, p, max_diff, max_diff_0;
541 unsigned frames_per_packet, min_frames = 0;
542 frames_per_packet = (s->period_size - s->idle_insize);
543 frames_per_packet <<= 8;
545 s->cfg.frame_size * inurb->number_of_packets;
548 max_diff_0 = s->cfg.frame_size;
549 if (s->cfg.period_frames >= 256)
551 if (s->cfg.period_frames >= 1024)
553 max_diff = max_diff_0;
554 for (p = 0; p < inurb->number_of_packets; ++p) {
556 l = inurb->iso_frame_desc[p].actual_length;
559 min_frames += frames_per_packet;
561 (min_frames >> 8) * s->cfg.frame_size;
562 if (diff < max_diff) {
563 snd_printdd(KERN_DEBUG "%i %i %i %i\n",
565 urb_size / (int)s->cfg.frame_size,
566 inurb->number_of_packets, diff);
570 s->idle_insize -= max_diff - max_diff_0;
571 s->idle_insize += urb_size - s->period_size;
572 if (s->idle_insize < 0) {
573 snd_printk(KERN_WARNING "%i %i %i\n",
574 s->idle_insize, urb_size, s->period_size);
576 } else if (s->idle_insize == 0) {
577 s->next_inpacket_split =
578 (s->inpacket_head + 1) % s->inpackets;
579 s->next_inpacket_split_at = 0;
581 unsigned split = s->inpacket_head;
583 while (l > s->inpacket[split].length) {
584 l -= s->inpacket[split].length;
586 split = s->inpackets - 1;
590 s->next_inpacket_split = split;
591 s->next_inpacket_split_at =
592 s->inpacket[split].length - l;
595 s->insize_done += urb_size;
597 if (usb_stream_prepare_playback(sk, inurb) < 0)
601 playback_prep_freqn(sk, sk->idle_outurb);
603 if (submit_urbs(sk, inurb, outurb) < 0)
606 if (s->state == usb_stream_sync1 && s->insize_done > 360000) {
607 /* just guesswork ^^^^^^ */
608 s->state = usb_stream_ready;
609 subs_set_complete(sk->inurb, i_capture_idle);
610 subs_set_complete(sk->outurb, i_playback_idle);
614 static void i_capture_start(struct urb *urb)
616 struct usb_iso_packet_descriptor *id = urb->iso_frame_desc;
617 struct usb_stream_kernel *sk = urb->context;
618 struct usb_stream *s = sk->s;
623 snd_printk(KERN_WARNING "status=%i\n", urb->status);
627 for (p = 0; p < urb->number_of_packets; ++p) {
628 int l = id[p].actual_length;
629 if (l < s->cfg.frame_size) {
631 if (s->state >= usb_stream_sync0) {
632 snd_printk(KERN_WARNING "%i\n", l);
637 s->inpacket_head %= s->inpackets;
638 s->inpacket[s->inpacket_head].offset =
639 id[p].offset + (urb->transfer_buffer - (void *)s);
640 s->inpacket[s->inpacket_head].length = l;
644 printk(KERN_DEBUG"%s:%i: %i", __func__, __LINE__,
645 urb->iso_frame_desc[0].actual_length);
646 for (pack = 1; pack < urb->number_of_packets; ++pack) {
647 int l = urb->iso_frame_desc[pack].actual_length;
648 printk(KERN_CONT " %i", l);
650 printk(KERN_CONT "\n");
653 if (!empty && s->state < usb_stream_sync1)
656 if (balance_capture(sk, urb))
657 stream_start(sk, urb, sk->i_urb);
660 static void i_playback_start(struct urb *urb)
662 struct usb_stream_kernel *sk = urb->context;
663 if (balance_playback(sk, urb))
664 stream_start(sk, sk->i_urb, urb);
667 int usb_stream_start(struct usb_stream_kernel *sk)
669 struct usb_stream *s = sk->s;
670 int frame = 0, iters = 0;
674 if (s->state != usb_stream_stopped)
677 subs_set_complete(sk->inurb, i_capture_start);
678 subs_set_complete(sk->outurb, i_playback_start);
679 memset(sk->write_page, 0, s->write_size);
685 s->inpacket_head = -1;
686 sk->iso_frame_balance = 0;
688 for (u = 0; u < 2; u++) {
689 struct urb *inurb = sk->inurb[u];
690 struct urb *outurb = sk->outurb[u];
691 playback_prep_freqn(sk, outurb);
692 inurb->number_of_packets = outurb->number_of_packets;
693 inurb->transfer_buffer_length =
694 inurb->number_of_packets *
695 inurb->iso_frame_desc[0].length;
699 struct usb_device *dev = inurb->dev;
700 frame = usb_get_current_frame_number(dev);
702 now = usb_get_current_frame_number(dev);
704 } while (now > -1 && now == frame);
706 err = usb_submit_urb(inurb, GFP_ATOMIC);
708 snd_printk(KERN_ERR"usb_submit_urb(sk->inurb[%i])"
709 " returned %i\n", u, err);
712 err = usb_submit_urb(outurb, GFP_ATOMIC);
714 snd_printk(KERN_ERR"usb_submit_urb(sk->outurb[%i])"
715 " returned %i\n", u, err);
719 if (inurb->start_frame != outurb->start_frame) {
720 snd_printd(KERN_DEBUG
721 "u[%i] start_frames differ in:%u out:%u\n",
722 u, inurb->start_frame, outurb->start_frame);
726 snd_printdd(KERN_DEBUG "%i %i\n", frame, iters);
733 snd_printd(KERN_DEBUG "goto dotry;\n");
736 snd_printk(KERN_WARNING"couldn't start"
737 " all urbs on the same start_frame.\n");
741 sk->idle_inurb = sk->inurb[USB_STREAM_NURBS - 2];
742 sk->idle_outurb = sk->outurb[USB_STREAM_NURBS - 2];
743 sk->completed_inurb = sk->inurb[USB_STREAM_NURBS - 1];
744 sk->completed_outurb = sk->outurb[USB_STREAM_NURBS - 1];
749 while (s->state != usb_stream_ready && wait_ms > 0) {
750 snd_printdd(KERN_DEBUG "%i\n", s->state);
756 return s->state == usb_stream_ready ? 0 : -EFAULT;
762 void usb_stream_stop(struct usb_stream_kernel *sk)
767 for (u = 0; u < USB_STREAM_NURBS; ++u) {
768 usb_kill_urb(sk->inurb[u]);
769 usb_kill_urb(sk->outurb[u]);
771 sk->s->state = usb_stream_stopped;