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_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)
96 struct usb_stream *s = sk->s;
97 char *indata = (char *)s + sizeof(*s) +
98 sizeof(struct usb_stream_packet) *
102 for (u = 0; u < USB_STREAM_NURBS; ++u) {
103 sk->inurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
104 sk->outurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
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,
114 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
115 * this will overflow at approx 524 kHz
117 static inline unsigned get_usb_full_speed_rate(unsigned rate)
119 return ((rate << 13) + 62) / 125;
123 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
124 * this will overflow at approx 4 MHz
126 static inline unsigned get_usb_high_speed_rate(unsigned rate)
128 return ((rate << 10) + 62) / 125;
131 void usb_stream_free(struct usb_stream_kernel *sk)
133 struct usb_stream *s;
136 for (u = 0; u < USB_STREAM_NURBS; ++u) {
137 usb_free_urb(sk->inurb[u]);
139 usb_free_urb(sk->outurb[u]);
140 sk->outurb[u] = NULL;
147 free_pages((unsigned long)sk->write_page, get_order(s->write_size));
148 sk->write_page = NULL;
149 free_pages((unsigned long)s, get_order(s->read_size));
153 struct usb_stream *usb_stream_new(struct usb_stream_kernel *sk,
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)
159 int packets, max_packsize;
160 int in_pipe, out_pipe;
161 int read_size = sizeof(struct usb_stream);
163 int usb_frames = dev->speed == USB_SPEED_HIGH ? 8000 : 1000;
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);
173 t_period = period_frames / sample_rate
174 iso_packs = t_period / t_iso_frame
175 = (period_frames / sample_rate) * (1 / t_iso_frame)
178 packets = period_frames * usb_frames / sample_rate + 1;
180 if (dev->speed == USB_SPEED_HIGH)
181 packets = (packets + 7) & ~7;
183 read_size += packets * USB_STREAM_URBDEPTH *
184 (max_packsize + sizeof(struct usb_stream_packet));
186 max_packsize = usb_maxpacket(dev, out_pipe, 1);
187 write_size = max_packsize * packets * USB_STREAM_URBDEPTH;
189 if (read_size >= 256*PAGE_SIZE || write_size >= 256*PAGE_SIZE) {
190 snd_printk(KERN_WARNING "a size exceeds 128*PAGE_SIZE\n");
194 pg = get_order(read_size);
195 sk->s = (void *) __get_free_pages(GFP_KERNEL|__GFP_COMP|__GFP_ZERO, pg);
197 snd_printk(KERN_WARNING "couldn't __get_free_pages()\n");
200 sk->s->cfg.version = USB_STREAM_INTERFACE_VERSION;
202 sk->s->read_size = read_size;
204 sk->s->cfg.sample_rate = sample_rate;
205 sk->s->cfg.frame_size = frame_size;
206 sk->n_o_ps = packets;
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;
211 sk->s->write_size = write_size;
212 pg = get_order(write_size);
215 (void *)__get_free_pages(GFP_KERNEL|__GFP_COMP|__GFP_ZERO, pg);
216 if (!sk->write_page) {
217 snd_printk(KERN_WARNING "couldn't __get_free_pages()\n");
222 /* calculate the frequency in 16.16 format */
223 if (dev->speed == USB_SPEED_FULL)
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);
229 sk->s->state = usb_stream_stopped;
237 static bool balance_check(struct usb_stream_kernel *sk, struct urb *urb)
240 if (unlikely(urb->status)) {
241 if (urb->status != -ESHUTDOWN && urb->status != -ENOENT)
242 snd_printk(KERN_WARNING "status=%i\n", urb->status);
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];
270 urb->complete = complete;
274 static int usb_stream_prepare_playback(struct usb_stream_kernel *sk,
277 struct usb_stream *s = sk->s;
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;
285 for (; s->sync_packet < 0; ++p, ++s->sync_packet) {
286 struct urb *ii = sk->completed_inurb;
287 id = ii->iso_frame_desc +
288 ii->number_of_packets + s->sync_packet;
289 l = id->actual_length;
297 s->sync_packet < inurb->number_of_packets && p < sk->n_o_ps;
298 ++p, ++s->sync_packet) {
299 l = inurb->iso_frame_desc[s->sync_packet].actual_length;
301 if (s->idle_outsize + lb + l > s->period_size)
310 s->sync_packet -= inurb->number_of_packets;
311 if (unlikely(s->sync_packet < -2 || s->sync_packet > 0)) {
312 snd_printk(KERN_WARNING "invalid sync_packet = %i;"
313 " p=%i nop=%i %i %x %x %x > %x\n",
314 s->sync_packet, p, inurb->number_of_packets,
315 s->idle_outsize + lb + l,
316 s->idle_outsize, lb, l,
320 if (unlikely(lb % s->cfg.frame_size)) {
321 snd_printk(KERN_WARNING"invalid outsize = %i\n",
325 s->idle_outsize += lb - s->period_size;
326 io->number_of_packets = p;
327 io->transfer_buffer_length = lb;
328 if (s->idle_outsize <= 0)
331 snd_printk(KERN_WARNING "idle=%i\n", s->idle_outsize);
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)
344 id[p + 1].offset = id[p].offset + id[p].length;
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);
355 err = usb_submit_urb(sk->idle_inurb, GFP_ATOMIC);
357 snd_printk(KERN_ERR "%i\n", err);
360 sk->idle_inurb = sk->completed_inurb;
361 sk->completed_inurb = inurb;
362 err = usb_submit_urb(sk->idle_outurb, GFP_ATOMIC);
364 snd_printk(KERN_ERR "%i\n", err);
367 sk->idle_outurb = sk->completed_outurb;
368 sk->completed_outurb = outurb;
372 #ifdef DEBUG_LOOP_BACK
374 This loop_back() shows how to read/write the period data.
376 static void loop_back(struct usb_stream *s)
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;
397 if (l + il > s->period_size)
398 il = s->period_size - l;
407 memcpy(o, i + ol, il - ol);
409 ol = s->period_size - s->playback1st_size;
413 if (iu == sk->completed_inurb) {
414 if (l != s->period_size)
415 printk(KERN_DEBUG"%s:%i %i\n", __func__, __LINE__,
416 l/(int)s->cfg.frame_size);
421 iu = sk->completed_inurb;
422 id = iu->iso_frame_desc;
428 static void loop_back(struct usb_stream *s)
433 static void stream_idle(struct usb_stream_kernel *sk,
434 struct urb *inurb, struct urb *outurb)
436 struct usb_stream *s = sk->s;
438 int insize = s->idle_insize;
441 s->inpacket_split = s->next_inpacket_split;
442 s->inpacket_split_at = s->next_inpacket_split_at;
443 s->next_inpacket_split = -1;
444 s->next_inpacket_split_at = 0;
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;
449 if (unlikely(l == 0 || id[p].status)) {
450 snd_printk(KERN_WARNING "underrun, status=%u\n",
455 s->inpacket_head %= s->inpackets;
456 if (s->inpacket_split == -1)
457 s->inpacket_split = s->inpacket_head;
459 s->inpacket[s->inpacket_head].offset =
460 id[p].offset + (inurb->transfer_buffer - (void *)s);
461 s->inpacket[s->inpacket_head].length = l;
462 if (insize + l > s->period_size &&
463 s->next_inpacket_split == -1) {
464 s->next_inpacket_split = s->inpacket_head;
465 s->next_inpacket_split_at = s->period_size - insize;
470 s->idle_insize += urb_size - s->period_size;
471 if (s->idle_insize < 0) {
472 snd_printk(KERN_WARNING "%i\n",
473 (s->idle_insize)/(int)s->cfg.frame_size);
476 s->insize_done += urb_size;
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)
494 wake_up_all(&sk->sleep);
497 s->state = usb_stream_xrun;
498 wake_up_all(&sk->sleep);
501 static void i_capture_idle(struct urb *urb)
503 struct usb_stream_kernel *sk = urb->context;
504 if (balance_capture(sk, urb))
505 stream_idle(sk, urb, sk->i_urb);
508 static void i_playback_idle(struct urb *urb)
510 struct usb_stream_kernel *sk = urb->context;
511 if (balance_playback(sk, urb))
512 stream_idle(sk, sk->i_urb, urb);
515 static void stream_start(struct usb_stream_kernel *sk,
516 struct urb *inurb, struct urb *outurb)
518 struct usb_stream *s = sk->s;
519 if (s->state >= usb_stream_sync1) {
520 int l, p, max_diff, max_diff_0;
522 unsigned frames_per_packet, min_frames = 0;
523 frames_per_packet = (s->period_size - s->idle_insize);
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) {
544 snd_printdd(KERN_DEBUG "%i %i %i %i\n",
546 urb_size / (int)s->cfg.frame_size,
547 inurb->number_of_packets, diff);
551 s->idle_insize -= max_diff - max_diff_0;
552 s->idle_insize += urb_size - s->period_size;
553 if (s->idle_insize < 0) {
554 snd_printk(KERN_WARNING "%i %i %i\n",
555 s->idle_insize, urb_size, s->period_size);
557 } else if (s->idle_insize == 0) {
558 s->next_inpacket_split =
559 (s->inpacket_head + 1) % s->inpackets;
560 s->next_inpacket_split_at = 0;
562 unsigned split = s->inpacket_head;
564 while (l > s->inpacket[split].length) {
565 l -= s->inpacket[split].length;
567 split = s->inpackets - 1;
571 s->next_inpacket_split = split;
572 s->next_inpacket_split_at =
573 s->inpacket[split].length - l;
576 s->insize_done += urb_size;
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)
587 if (s->state == usb_stream_sync1 && s->insize_done > 360000) {
588 /* just guesswork ^^^^^^ */
589 s->state = usb_stream_ready;
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;
599 struct usb_stream *s = sk->s;
604 snd_printk(KERN_WARNING "status=%i\n", urb->status);
608 for (p = 0; p < urb->number_of_packets; ++p) {
609 int l = id[p].actual_length;
610 if (l < s->cfg.frame_size) {
612 if (s->state >= usb_stream_sync0) {
613 snd_printk(KERN_WARNING "%i\n", l);
618 s->inpacket_head %= s->inpackets;
619 s->inpacket[s->inpacket_head].offset =
620 id[p].offset + (urb->transfer_buffer - (void *)s);
621 s->inpacket[s->inpacket_head].length = l;
625 printk(KERN_DEBUG"%s:%i: %i", __func__, __LINE__,
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;
634 if (!empty && s->state < usb_stream_sync1)
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);
648 int usb_stream_start(struct usb_stream_kernel *sk)
650 struct usb_stream *s = sk->s;
651 int frame = 0, iters = 0;
655 if (s->state != usb_stream_stopped)
658 subs_set_complete(sk->inurb, i_capture_start);
659 subs_set_complete(sk->outurb, i_playback_start);
660 memset(sk->write_page, 0, s->write_size);
666 s->inpacket_head = -1;
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;
681 frame = usb_get_current_frame_number(dev);
683 now = usb_get_current_frame_number(dev);
685 } while (now > -1 && now == frame);
687 err = usb_submit_urb(inurb, GFP_ATOMIC);
689 snd_printk(KERN_ERR"usb_submit_urb(sk->inurb[%i])"
690 " returned %i\n", u, err);
693 err = usb_submit_urb(outurb, GFP_ATOMIC);
695 snd_printk(KERN_ERR"usb_submit_urb(sk->outurb[%i])"
696 " returned %i\n", u, err);
700 if (inurb->start_frame != outurb->start_frame) {
701 snd_printd(KERN_DEBUG
702 "u[%i] start_frames differ in:%u out:%u\n",
703 u, inurb->start_frame, outurb->start_frame);
707 snd_printdd(KERN_DEBUG "%i %i\n", frame, iters);
714 snd_printd(KERN_DEBUG "goto dotry;\n");
717 snd_printk(KERN_WARNING"couldn't start"
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];
730 while (s->state != usb_stream_ready && wait_ms > 0) {
731 snd_printdd(KERN_DEBUG "%i\n", s->state);
737 return s->state == usb_stream_ready ? 0 : -EFAULT;
743 void usb_stream_stop(struct usb_stream_kernel *sk)
748 for (u = 0; u < USB_STREAM_NURBS; ++u) {
749 usb_kill_urb(sk->inurb[u]);
750 usb_kill_urb(sk->outurb[u]);
752 sk->s->state = usb_stream_stopped;