pandora: defconfig: update
[pandora-kernel.git] / sound / usb / usx2y / usb_stream.c
1 /*
2  * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de>
3  *
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.
8  *
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
12  * for more details.
13  *
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.
17  */
18
19 #include <linux/usb.h>
20 #include <linux/gfp.h>
21
22 #include "usb_stream.h"
23
24
25 /*                             setup                                  */
26
27 static unsigned usb_stream_next_packet_size(struct usb_stream_kernel *sk)
28 {
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;
32 }
33
34 static void playback_prep_freqn(struct usb_stream_kernel *sk, struct urb *urb)
35 {
36         struct usb_stream *s = sk->s;
37         int pack, lb = 0;
38
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)
42                         goto check;
43
44                 sk->out_phase = sk->out_phase_peeked;
45                 urb->iso_frame_desc[pack].offset = lb;
46                 urb->iso_frame_desc[pack].length = l;
47                 lb += l;
48         }
49         snd_printdd(KERN_DEBUG "%i\n", lb);
50
51 check:
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,
56                     lb, s->period_size);
57 }
58
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)
62 {
63         int u, p;
64         int maxpacket = use_packsize ?
65                 use_packsize : usb_maxpacket(dev, pipe, usb_pipeout(pipe));
66         int transfer_length = maxpacket * sk->n_o_ps;
67
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;
74                 urb->dev = dev;
75                 urb->pipe = pipe;
76                 urb->number_of_packets = sk->n_o_ps;
77                 urb->context = sk;
78                 urb->interval = 1;
79                 if (usb_pipeout(pipe))
80                         continue;
81
82                 urb->transfer_buffer_length = transfer_length;
83                 desc = urb->iso_frame_desc;
84                 desc->offset = 0;
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;
89                 }
90         }
91 }
92
93 static void init_urbs(struct usb_stream_kernel *sk, unsigned use_packsize,
94                       struct usb_device *dev, int in_pipe, int out_pipe)
95 {
96         struct usb_stream       *s = sk->s;
97         char                    *indata = (char *)s + sizeof(*s) +
98                                         sizeof(struct usb_stream_packet) *
99                                         s->inpackets;
100         int                     u;
101
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);
105         }
106
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,
109                        out_pipe);
110 }
111
112
113 /*
114  * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
115  * this will overflow at approx 524 kHz
116  */
117 static inline unsigned get_usb_full_speed_rate(unsigned rate)
118 {
119         return ((rate << 13) + 62) / 125;
120 }
121
122 /*
123  * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
124  * this will overflow at approx 4 MHz
125  */
126 static inline unsigned get_usb_high_speed_rate(unsigned rate)
127 {
128         return ((rate << 10) + 62) / 125;
129 }
130
131 void usb_stream_free(struct usb_stream_kernel *sk)
132 {
133         struct usb_stream *s;
134         unsigned u;
135
136         for (u = 0; u < USB_STREAM_NURBS; ++u) {
137                 usb_free_urb(sk->inurb[u]);
138                 sk->inurb[u] = NULL;
139                 usb_free_urb(sk->outurb[u]);
140                 sk->outurb[u] = NULL;
141         }
142
143         s = sk->s;
144         if (!s)
145                 return;
146
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));
150         sk->s = NULL;
151 }
152
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)
158 {
159         int packets, max_packsize;
160         int in_pipe, out_pipe;
161         int read_size = sizeof(struct usb_stream);
162         int write_size;
163         int usb_frames = dev->speed == USB_SPEED_HIGH ? 8000 : 1000;
164         int pg;
165
166         in_pipe = usb_rcvisocpipe(dev, in_endpoint);
167         out_pipe = usb_sndisocpipe(dev, out_endpoint);
168
169         max_packsize = use_packsize ?
170                 use_packsize : usb_maxpacket(dev, in_pipe, 0);
171
172         /*
173                 t_period = period_frames / sample_rate
174                 iso_packs = t_period / t_iso_frame
175                         = (period_frames / sample_rate) * (1 / t_iso_frame)
176         */
177
178         packets = period_frames * usb_frames / sample_rate + 1;
179
180         if (dev->speed == USB_SPEED_HIGH)
181                 packets = (packets + 7) & ~7;
182
183         read_size += packets * USB_STREAM_URBDEPTH *
184                 (max_packsize + sizeof(struct usb_stream_packet));
185
186         max_packsize = usb_maxpacket(dev, out_pipe, 1);
187         write_size = max_packsize * packets * USB_STREAM_URBDEPTH;
188
189         if (read_size >= 256*PAGE_SIZE || write_size >= 256*PAGE_SIZE) {
190                 snd_printk(KERN_WARNING "a size exceeds 128*PAGE_SIZE\n");
191                 goto out;
192         }
193
194         pg = get_order(read_size);
195         sk->s = (void *) __get_free_pages(GFP_KERNEL|__GFP_COMP|__GFP_ZERO|
196                                           __GFP_NOWARN, pg);
197         if (!sk->s) {
198                 snd_printk(KERN_WARNING "couldn't __get_free_pages()\n");
199                 goto out;
200         }
201         sk->s->cfg.version = USB_STREAM_INTERFACE_VERSION;
202
203         sk->s->read_size = read_size;
204
205         sk->s->cfg.sample_rate = sample_rate;
206         sk->s->cfg.frame_size = frame_size;
207         sk->n_o_ps = packets;
208         sk->s->inpackets = packets * USB_STREAM_URBDEPTH;
209         sk->s->cfg.period_frames = period_frames;
210         sk->s->period_size = frame_size * period_frames;
211
212         sk->s->write_size = write_size;
213         pg = get_order(write_size);
214
215         sk->write_page =
216                 (void *)__get_free_pages(GFP_KERNEL|__GFP_COMP|__GFP_ZERO|
217                                          __GFP_NOWARN, pg);
218         if (!sk->write_page) {
219                 snd_printk(KERN_WARNING "couldn't __get_free_pages()\n");
220                 usb_stream_free(sk);
221                 return NULL;
222         }
223
224         /* calculate the frequency in 16.16 format */
225         if (dev->speed == USB_SPEED_FULL)
226                 sk->freqn = get_usb_full_speed_rate(sample_rate);
227         else
228                 sk->freqn = get_usb_high_speed_rate(sample_rate);
229
230         init_urbs(sk, use_packsize, dev, in_pipe, out_pipe);
231         sk->s->state = usb_stream_stopped;
232 out:
233         return sk->s;
234 }
235
236
237 /*                             start                                  */
238
239 static bool balance_check(struct usb_stream_kernel *sk, struct urb *urb)
240 {
241         bool r;
242         if (unlikely(urb->status)) {
243                 if (urb->status != -ESHUTDOWN && urb->status != -ENOENT)
244                         snd_printk(KERN_WARNING "status=%i\n", urb->status);
245                 sk->iso_frame_balance = 0x7FFFFFFF;
246                 return false;
247         }
248         r = sk->iso_frame_balance == 0;
249         if (!r)
250                 sk->i_urb = urb;
251         return r;
252 }
253
254 static bool balance_playback(struct usb_stream_kernel *sk, struct urb *urb)
255 {
256         sk->iso_frame_balance += urb->number_of_packets;
257         return balance_check(sk, urb);
258 }
259
260 static bool balance_capture(struct usb_stream_kernel *sk, struct urb *urb)
261 {
262         sk->iso_frame_balance -= urb->number_of_packets;
263         return balance_check(sk, urb);
264 }
265
266 static void subs_set_complete(struct urb **urbs, void (*complete)(struct urb *))
267 {
268         int u;
269
270         for (u = 0; u < USB_STREAM_NURBS; u++) {
271                 struct urb *urb = urbs[u];
272                 urb->complete = complete;
273         }
274 }
275
276 static int usb_stream_prepare_playback(struct usb_stream_kernel *sk,
277                 struct urb *inurb)
278 {
279         struct usb_stream *s = sk->s;
280         struct urb *io;
281         struct usb_iso_packet_descriptor *id, *od;
282         int p = 0, lb = 0, l = 0;
283
284         io = sk->idle_outurb;
285         od = io->iso_frame_desc;
286
287         for (; s->sync_packet < 0; ++p, ++s->sync_packet) {
288                 struct urb *ii = sk->completed_inurb;
289                 id = ii->iso_frame_desc +
290                         ii->number_of_packets + s->sync_packet;
291                 l = id->actual_length;
292
293                 od[p].length = l;
294                 od[p].offset = lb;
295                 lb += l;
296         }
297
298         for (;
299              s->sync_packet < inurb->number_of_packets && p < sk->n_o_ps;
300              ++p, ++s->sync_packet) {
301                 l = inurb->iso_frame_desc[s->sync_packet].actual_length;
302
303                 if (s->idle_outsize + lb + l > s->period_size)
304                         goto check_ok;
305
306                 od[p].length = l;
307                 od[p].offset = lb;
308                 lb += l;
309         }
310
311 check_ok:
312         s->sync_packet -= inurb->number_of_packets;
313         if (unlikely(s->sync_packet < -2 || s->sync_packet > 0)) {
314                 snd_printk(KERN_WARNING "invalid sync_packet = %i;"
315                            " p=%i nop=%i %i %x %x %x > %x\n",
316                            s->sync_packet, p, inurb->number_of_packets,
317                            s->idle_outsize + lb + l,
318                            s->idle_outsize, lb,  l,
319                            s->period_size);
320                 return -1;
321         }
322         if (unlikely(lb % s->cfg.frame_size)) {
323                 snd_printk(KERN_WARNING"invalid outsize = %i\n",
324                            lb);
325                 return -1;
326         }
327         s->idle_outsize += lb - s->period_size;
328         io->number_of_packets = p;
329         io->transfer_buffer_length = lb;
330         if (s->idle_outsize <= 0)
331                 return 0;
332
333         snd_printk(KERN_WARNING "idle=%i\n", s->idle_outsize);
334         return -1;
335 }
336
337 static void prepare_inurb(int number_of_packets, struct urb *iu)
338 {
339         struct usb_iso_packet_descriptor *id;
340         int p;
341
342         iu->number_of_packets = number_of_packets;
343         id = iu->iso_frame_desc;
344         id->offset = 0;
345         for (p = 0; p < iu->number_of_packets - 1; ++p)
346                 id[p + 1].offset = id[p].offset + id[p].length;
347
348         iu->transfer_buffer_length =
349                 id[0].length * iu->number_of_packets;
350 }
351
352 static int submit_urbs(struct usb_stream_kernel *sk,
353                        struct urb *inurb, struct urb *outurb)
354 {
355         int err;
356         prepare_inurb(sk->idle_outurb->number_of_packets, sk->idle_inurb);
357         err = usb_submit_urb(sk->idle_inurb, GFP_ATOMIC);
358         if (err < 0) {
359                 snd_printk(KERN_ERR "%i\n", err);
360                 return err;
361         }
362         sk->idle_inurb = sk->completed_inurb;
363         sk->completed_inurb = inurb;
364         err = usb_submit_urb(sk->idle_outurb, GFP_ATOMIC);
365         if (err < 0) {
366                 snd_printk(KERN_ERR "%i\n", err);
367                 return err;
368         }
369         sk->idle_outurb = sk->completed_outurb;
370         sk->completed_outurb = outurb;
371         return 0;
372 }
373
374 #ifdef DEBUG_LOOP_BACK
375 /*
376   This loop_back() shows how to read/write the period data.
377  */
378 static void loop_back(struct usb_stream *s)
379 {
380         char *i, *o;
381         int il, ol, l, p;
382         struct urb *iu;
383         struct usb_iso_packet_descriptor *id;
384
385         o = s->playback1st_to;
386         ol = s->playback1st_size;
387         l = 0;
388
389         if (s->insplit_pack >= 0) {
390                 iu = sk->idle_inurb;
391                 id = iu->iso_frame_desc;
392                 p = s->insplit_pack;
393         } else
394                 goto second;
395 loop:
396         for (; p < iu->number_of_packets && l < s->period_size; ++p) {
397                 i = iu->transfer_buffer + id[p].offset;
398                 il = id[p].actual_length;
399                 if (l + il > s->period_size)
400                         il = s->period_size - l;
401                 if (il <= ol) {
402                         memcpy(o, i, il);
403                         o += il;
404                         ol -= il;
405                 } else {
406                         memcpy(o, i, ol);
407                         singen_6pack(o, ol);
408                         o = s->playback_to;
409                         memcpy(o, i + ol, il - ol);
410                         o += il - ol;
411                         ol = s->period_size - s->playback1st_size;
412                 }
413                 l += il;
414         }
415         if (iu == sk->completed_inurb) {
416                 if (l != s->period_size)
417                         printk(KERN_DEBUG"%s:%i %i\n", __func__, __LINE__,
418                                l/(int)s->cfg.frame_size);
419
420                 return;
421         }
422 second:
423         iu = sk->completed_inurb;
424         id = iu->iso_frame_desc;
425         p = 0;
426         goto loop;
427
428 }
429 #else
430 static void loop_back(struct usb_stream *s)
431 {
432 }
433 #endif
434
435 static void stream_idle(struct usb_stream_kernel *sk,
436                         struct urb *inurb, struct urb *outurb)
437 {
438         struct usb_stream *s = sk->s;
439         int l, p;
440         int insize = s->idle_insize;
441         int urb_size = 0;
442
443         s->inpacket_split = s->next_inpacket_split;
444         s->inpacket_split_at = s->next_inpacket_split_at;
445         s->next_inpacket_split = -1;
446         s->next_inpacket_split_at = 0;
447
448         for (p = 0; p < inurb->number_of_packets; ++p) {
449                 struct usb_iso_packet_descriptor *id = inurb->iso_frame_desc;
450                 l = id[p].actual_length;
451                 if (unlikely(l == 0 || id[p].status)) {
452                         snd_printk(KERN_WARNING "underrun, status=%u\n",
453                                    id[p].status);
454                         goto err_out;
455                 }
456                 s->inpacket_head++;
457                 s->inpacket_head %= s->inpackets;
458                 if (s->inpacket_split == -1)
459                         s->inpacket_split = s->inpacket_head;
460
461                 s->inpacket[s->inpacket_head].offset =
462                         id[p].offset + (inurb->transfer_buffer - (void *)s);
463                 s->inpacket[s->inpacket_head].length = l;
464                 if (insize + l > s->period_size &&
465                     s->next_inpacket_split == -1) {
466                         s->next_inpacket_split = s->inpacket_head;
467                         s->next_inpacket_split_at = s->period_size - insize;
468                 }
469                 insize += l;
470                 urb_size += l;
471         }
472         s->idle_insize += urb_size - s->period_size;
473         if (s->idle_insize < 0) {
474                 snd_printk(KERN_WARNING "%i\n",
475                            (s->idle_insize)/(int)s->cfg.frame_size);
476                 goto err_out;
477         }
478         s->insize_done += urb_size;
479
480         l = s->idle_outsize;
481         s->outpacket[0].offset = (sk->idle_outurb->transfer_buffer -
482                                   sk->write_page) - l;
483
484         if (usb_stream_prepare_playback(sk, inurb) < 0)
485                 goto err_out;
486
487         s->outpacket[0].length = sk->idle_outurb->transfer_buffer_length + l;
488         s->outpacket[1].offset = sk->completed_outurb->transfer_buffer -
489                 sk->write_page;
490
491         if (submit_urbs(sk, inurb, outurb) < 0)
492                 goto err_out;
493
494         loop_back(s);
495         s->periods_done++;
496         wake_up_all(&sk->sleep);
497         return;
498 err_out:
499         s->state = usb_stream_xrun;
500         wake_up_all(&sk->sleep);
501 }
502
503 static void i_capture_idle(struct urb *urb)
504 {
505         struct usb_stream_kernel *sk = urb->context;
506         if (balance_capture(sk, urb))
507                 stream_idle(sk, urb, sk->i_urb);
508 }
509
510 static void i_playback_idle(struct urb *urb)
511 {
512         struct usb_stream_kernel *sk = urb->context;
513         if (balance_playback(sk, urb))
514                 stream_idle(sk, sk->i_urb, urb);
515 }
516
517 static void stream_start(struct usb_stream_kernel *sk,
518                          struct urb *inurb, struct urb *outurb)
519 {
520         struct usb_stream *s = sk->s;
521         if (s->state >= usb_stream_sync1) {
522                 int l, p, max_diff, max_diff_0;
523                 int urb_size = 0;
524                 unsigned frames_per_packet, min_frames = 0;
525                 frames_per_packet = (s->period_size - s->idle_insize);
526                 frames_per_packet <<= 8;
527                 frames_per_packet /=
528                         s->cfg.frame_size * inurb->number_of_packets;
529                 frames_per_packet++;
530
531                 max_diff_0 = s->cfg.frame_size;
532                 if (s->cfg.period_frames >= 256)
533                         max_diff_0 <<= 1;
534                 if (s->cfg.period_frames >= 1024)
535                         max_diff_0 <<= 1;
536                 max_diff = max_diff_0;
537                 for (p = 0; p < inurb->number_of_packets; ++p) {
538                         int diff;
539                         l = inurb->iso_frame_desc[p].actual_length;
540                         urb_size += l;
541
542                         min_frames += frames_per_packet;
543                         diff = urb_size -
544                                 (min_frames >> 8) * s->cfg.frame_size;
545                         if (diff < max_diff) {
546                                 snd_printdd(KERN_DEBUG "%i %i %i %i\n",
547                                             s->insize_done,
548                                             urb_size / (int)s->cfg.frame_size,
549                                             inurb->number_of_packets, diff);
550                                 max_diff = diff;
551                         }
552                 }
553                 s->idle_insize -= max_diff - max_diff_0;
554                 s->idle_insize += urb_size - s->period_size;
555                 if (s->idle_insize < 0) {
556                         snd_printk(KERN_WARNING "%i %i %i\n",
557                                    s->idle_insize, urb_size, s->period_size);
558                         return;
559                 } else if (s->idle_insize == 0) {
560                         s->next_inpacket_split =
561                                 (s->inpacket_head + 1) % s->inpackets;
562                         s->next_inpacket_split_at = 0;
563                 } else {
564                         unsigned split = s->inpacket_head;
565                         l = s->idle_insize;
566                         while (l > s->inpacket[split].length) {
567                                 l -= s->inpacket[split].length;
568                                 if (split == 0)
569                                         split = s->inpackets - 1;
570                                 else
571                                         split--;
572                         }
573                         s->next_inpacket_split = split;
574                         s->next_inpacket_split_at =
575                                 s->inpacket[split].length - l;
576                 }
577
578                 s->insize_done += urb_size;
579
580                 if (usb_stream_prepare_playback(sk, inurb) < 0)
581                         return;
582
583         } else
584                 playback_prep_freqn(sk, sk->idle_outurb);
585
586         if (submit_urbs(sk, inurb, outurb) < 0)
587                 return;
588
589         if (s->state == usb_stream_sync1 && s->insize_done > 360000) {
590                 /* just guesswork                            ^^^^^^ */
591                 s->state = usb_stream_ready;
592                 subs_set_complete(sk->inurb, i_capture_idle);
593                 subs_set_complete(sk->outurb, i_playback_idle);
594         }
595 }
596
597 static void i_capture_start(struct urb *urb)
598 {
599         struct usb_iso_packet_descriptor *id = urb->iso_frame_desc;
600         struct usb_stream_kernel *sk = urb->context;
601         struct usb_stream *s = sk->s;
602         int p;
603         int empty = 0;
604
605         if (urb->status) {
606                 snd_printk(KERN_WARNING "status=%i\n", urb->status);
607                 return;
608         }
609
610         for (p = 0; p < urb->number_of_packets; ++p) {
611                 int l = id[p].actual_length;
612                 if (l < s->cfg.frame_size) {
613                         ++empty;
614                         if (s->state >= usb_stream_sync0) {
615                                 snd_printk(KERN_WARNING "%i\n", l);
616                                 return;
617                         }
618                 }
619                 s->inpacket_head++;
620                 s->inpacket_head %= s->inpackets;
621                 s->inpacket[s->inpacket_head].offset =
622                         id[p].offset + (urb->transfer_buffer - (void *)s);
623                 s->inpacket[s->inpacket_head].length = l;
624         }
625 #ifdef SHOW_EMPTY
626         if (empty) {
627                 printk(KERN_DEBUG"%s:%i: %i", __func__, __LINE__,
628                        urb->iso_frame_desc[0].actual_length);
629                 for (pack = 1; pack < urb->number_of_packets; ++pack) {
630                         int l = urb->iso_frame_desc[pack].actual_length;
631                         printk(" %i", l);
632                 }
633                 printk("\n");
634         }
635 #endif
636         if (!empty && s->state < usb_stream_sync1)
637                 ++s->state;
638
639         if (balance_capture(sk, urb))
640                 stream_start(sk, urb, sk->i_urb);
641 }
642
643 static void i_playback_start(struct urb *urb)
644 {
645         struct usb_stream_kernel *sk = urb->context;
646         if (balance_playback(sk, urb))
647                 stream_start(sk, sk->i_urb, urb);
648 }
649
650 int usb_stream_start(struct usb_stream_kernel *sk)
651 {
652         struct usb_stream *s = sk->s;
653         int frame = 0, iters = 0;
654         int u, err;
655         int try = 0;
656
657         if (s->state != usb_stream_stopped)
658                 return -EAGAIN;
659
660         subs_set_complete(sk->inurb, i_capture_start);
661         subs_set_complete(sk->outurb, i_playback_start);
662         memset(sk->write_page, 0, s->write_size);
663 dotry:
664         s->insize_done = 0;
665         s->idle_insize = 0;
666         s->idle_outsize = 0;
667         s->sync_packet = -1;
668         s->inpacket_head = -1;
669         sk->iso_frame_balance = 0;
670         ++try;
671         for (u = 0; u < 2; u++) {
672                 struct urb *inurb = sk->inurb[u];
673                 struct urb *outurb = sk->outurb[u];
674                 playback_prep_freqn(sk, outurb);
675                 inurb->number_of_packets = outurb->number_of_packets;
676                 inurb->transfer_buffer_length =
677                         inurb->number_of_packets *
678                         inurb->iso_frame_desc[0].length;
679
680                 if (u == 0) {
681                         int now;
682                         struct usb_device *dev = inurb->dev;
683                         frame = usb_get_current_frame_number(dev);
684                         do {
685                                 now = usb_get_current_frame_number(dev);
686                                 ++iters;
687                         } while (now > -1 && now == frame);
688                 }
689                 err = usb_submit_urb(inurb, GFP_ATOMIC);
690                 if (err < 0) {
691                         snd_printk(KERN_ERR"usb_submit_urb(sk->inurb[%i])"
692                                    " returned %i\n", u, err);
693                         return err;
694                 }
695                 err = usb_submit_urb(outurb, GFP_ATOMIC);
696                 if (err < 0) {
697                         snd_printk(KERN_ERR"usb_submit_urb(sk->outurb[%i])"
698                                    " returned %i\n", u, err);
699                         return err;
700                 }
701
702                 if (inurb->start_frame != outurb->start_frame) {
703                         snd_printd(KERN_DEBUG
704                                    "u[%i] start_frames differ in:%u out:%u\n",
705                                    u, inurb->start_frame, outurb->start_frame);
706                         goto check_retry;
707                 }
708         }
709         snd_printdd(KERN_DEBUG "%i %i\n", frame, iters);
710         try = 0;
711 check_retry:
712         if (try) {
713                 usb_stream_stop(sk);
714                 if (try < 5) {
715                         msleep(1500);
716                         snd_printd(KERN_DEBUG "goto dotry;\n");
717                         goto dotry;
718                 }
719                 snd_printk(KERN_WARNING"couldn't start"
720                            " all urbs on the same start_frame.\n");
721                 return -EFAULT;
722         }
723
724         sk->idle_inurb = sk->inurb[USB_STREAM_NURBS - 2];
725         sk->idle_outurb = sk->outurb[USB_STREAM_NURBS - 2];
726         sk->completed_inurb = sk->inurb[USB_STREAM_NURBS - 1];
727         sk->completed_outurb = sk->outurb[USB_STREAM_NURBS - 1];
728
729 /* wait, check */
730         {
731                 int wait_ms = 3000;
732                 while (s->state != usb_stream_ready && wait_ms > 0) {
733                         snd_printdd(KERN_DEBUG "%i\n", s->state);
734                         msleep(200);
735                         wait_ms -= 200;
736                 }
737         }
738
739         return s->state == usb_stream_ready ? 0 : -EFAULT;
740 }
741
742
743 /*                             stop                                   */
744
745 void usb_stream_stop(struct usb_stream_kernel *sk)
746 {
747         int u;
748         if (!sk->s)
749                 return;
750         for (u = 0; u < USB_STREAM_NURBS; ++u) {
751                 usb_kill_urb(sk->inurb[u]);
752                 usb_kill_urb(sk->outurb[u]);
753         }
754         sk->s->state = usb_stream_stopped;
755         msleep(400);
756 }