]> asedeno.scripts.mit.edu Git - linux.git/blob - sound/usb/usx2y/usb_stream.c
221adf68bd0cb7a67f077a32f07bf7ee770a6071
[linux.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 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)
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_buffer = transfer;
73                 urb->dev = dev;
74                 urb->pipe = pipe;
75                 urb->number_of_packets = sk->n_o_ps;
76                 urb->context = sk;
77                 urb->interval = 1;
78                 if (usb_pipeout(pipe))
79                         continue;
80                 if (usb_urb_ep_type_check(urb))
81                         return -EINVAL;
82
83                 urb->transfer_buffer_length = transfer_length;
84                 desc = urb->iso_frame_desc;
85                 desc->offset = 0;
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;
90                 }
91         }
92
93         return 0;
94 }
95
96 static int init_urbs(struct usb_stream_kernel *sk, unsigned use_packsize,
97                       struct usb_device *dev, int in_pipe, int out_pipe)
98 {
99         struct usb_stream       *s = sk->s;
100         char                    *indata = (char *)s + sizeof(*s) +
101                                         sizeof(struct usb_stream_packet) *
102                                         s->inpackets;
103         int                     u;
104
105         for (u = 0; u < USB_STREAM_NURBS; ++u) {
106                 sk->inurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
107                 if (!sk->inurb[u])
108                         return -ENOMEM;
109
110                 sk->outurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
111                 if (!sk->outurb[u])
112                         return -ENOMEM;
113         }
114
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,
117                            out_pipe))
118                 return -EINVAL;
119
120         return 0;
121 }
122
123
124 /*
125  * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
126  * this will overflow at approx 524 kHz
127  */
128 static inline unsigned get_usb_full_speed_rate(unsigned rate)
129 {
130         return ((rate << 13) + 62) / 125;
131 }
132
133 /*
134  * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
135  * this will overflow at approx 4 MHz
136  */
137 static inline unsigned get_usb_high_speed_rate(unsigned rate)
138 {
139         return ((rate << 10) + 62) / 125;
140 }
141
142 void usb_stream_free(struct usb_stream_kernel *sk)
143 {
144         struct usb_stream *s;
145         unsigned u;
146
147         for (u = 0; u < USB_STREAM_NURBS; ++u) {
148                 usb_free_urb(sk->inurb[u]);
149                 sk->inurb[u] = NULL;
150                 usb_free_urb(sk->outurb[u]);
151                 sk->outurb[u] = NULL;
152         }
153
154         s = sk->s;
155         if (!s)
156                 return;
157
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));
161         sk->s = NULL;
162 }
163
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)
169 {
170         int packets, max_packsize;
171         int in_pipe, out_pipe;
172         int read_size = sizeof(struct usb_stream);
173         int write_size;
174         int usb_frames = dev->speed == USB_SPEED_HIGH ? 8000 : 1000;
175         int pg;
176
177         in_pipe = usb_rcvisocpipe(dev, in_endpoint);
178         out_pipe = usb_sndisocpipe(dev, out_endpoint);
179
180         max_packsize = use_packsize ?
181                 use_packsize : usb_maxpacket(dev, in_pipe, 0);
182
183         /*
184                 t_period = period_frames / sample_rate
185                 iso_packs = t_period / t_iso_frame
186                         = (period_frames / sample_rate) * (1 / t_iso_frame)
187         */
188
189         packets = period_frames * usb_frames / sample_rate + 1;
190
191         if (dev->speed == USB_SPEED_HIGH)
192                 packets = (packets + 7) & ~7;
193
194         read_size += packets * USB_STREAM_URBDEPTH *
195                 (max_packsize + sizeof(struct usb_stream_packet));
196
197         max_packsize = usb_maxpacket(dev, out_pipe, 1);
198         write_size = max_packsize * packets * USB_STREAM_URBDEPTH;
199
200         if (read_size >= 256*PAGE_SIZE || write_size >= 256*PAGE_SIZE) {
201                 snd_printk(KERN_WARNING "a size exceeds 128*PAGE_SIZE\n");
202                 goto out;
203         }
204
205         pg = get_order(read_size);
206         sk->s = (void *) __get_free_pages(GFP_KERNEL|__GFP_COMP|__GFP_ZERO|
207                                           __GFP_NOWARN, pg);
208         if (!sk->s) {
209                 snd_printk(KERN_WARNING "couldn't __get_free_pages()\n");
210                 goto out;
211         }
212         sk->s->cfg.version = USB_STREAM_INTERFACE_VERSION;
213
214         sk->s->read_size = read_size;
215
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;
222
223         sk->s->write_size = write_size;
224         pg = get_order(write_size);
225
226         sk->write_page =
227                 (void *)__get_free_pages(GFP_KERNEL|__GFP_COMP|__GFP_ZERO|
228                                          __GFP_NOWARN, pg);
229         if (!sk->write_page) {
230                 snd_printk(KERN_WARNING "couldn't __get_free_pages()\n");
231                 usb_stream_free(sk);
232                 return NULL;
233         }
234
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);
238         else
239                 sk->freqn = get_usb_high_speed_rate(sample_rate);
240
241         if (init_urbs(sk, use_packsize, dev, in_pipe, out_pipe) < 0) {
242                 usb_stream_free(sk);
243                 return NULL;
244         }
245
246         sk->s->state = usb_stream_stopped;
247 out:
248         return sk->s;
249 }
250
251
252 /*                             start                                  */
253
254 static bool balance_check(struct usb_stream_kernel *sk, struct urb *urb)
255 {
256         bool r;
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;
261                 return false;
262         }
263         r = sk->iso_frame_balance == 0;
264         if (!r)
265                 sk->i_urb = urb;
266         return r;
267 }
268
269 static bool balance_playback(struct usb_stream_kernel *sk, struct urb *urb)
270 {
271         sk->iso_frame_balance += urb->number_of_packets;
272         return balance_check(sk, urb);
273 }
274
275 static bool balance_capture(struct usb_stream_kernel *sk, struct urb *urb)
276 {
277         sk->iso_frame_balance -= urb->number_of_packets;
278         return balance_check(sk, urb);
279 }
280
281 static void subs_set_complete(struct urb **urbs, void (*complete)(struct urb *))
282 {
283         int u;
284
285         for (u = 0; u < USB_STREAM_NURBS; u++) {
286                 struct urb *urb = urbs[u];
287                 urb->complete = complete;
288         }
289 }
290
291 static int usb_stream_prepare_playback(struct usb_stream_kernel *sk,
292                 struct urb *inurb)
293 {
294         struct usb_stream *s = sk->s;
295         struct urb *io;
296         struct usb_iso_packet_descriptor *id, *od;
297         int p = 0, lb = 0, l = 0;
298
299         io = sk->idle_outurb;
300         od = io->iso_frame_desc;
301
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;
307
308                 od[p].length = l;
309                 od[p].offset = lb;
310                 lb += l;
311         }
312
313         for (;
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;
317
318                 if (s->idle_outsize + lb + l > s->period_size)
319                         goto check_ok;
320
321                 od[p].length = l;
322                 od[p].offset = lb;
323                 lb += l;
324         }
325
326 check_ok:
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,
334                            s->period_size);
335                 return -1;
336         }
337         if (unlikely(lb % s->cfg.frame_size)) {
338                 snd_printk(KERN_WARNING"invalid outsize = %i\n",
339                            lb);
340                 return -1;
341         }
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)
346                 return 0;
347
348         snd_printk(KERN_WARNING "idle=%i\n", s->idle_outsize);
349         return -1;
350 }
351
352 static void prepare_inurb(int number_of_packets, struct urb *iu)
353 {
354         struct usb_iso_packet_descriptor *id;
355         int p;
356
357         iu->number_of_packets = number_of_packets;
358         id = iu->iso_frame_desc;
359         id->offset = 0;
360         for (p = 0; p < iu->number_of_packets - 1; ++p)
361                 id[p + 1].offset = id[p].offset + id[p].length;
362
363         iu->transfer_buffer_length =
364                 id[0].length * iu->number_of_packets;
365 }
366
367 static int submit_urbs(struct usb_stream_kernel *sk,
368                        struct urb *inurb, struct urb *outurb)
369 {
370         int err;
371         prepare_inurb(sk->idle_outurb->number_of_packets, sk->idle_inurb);
372         err = usb_submit_urb(sk->idle_inurb, GFP_ATOMIC);
373         if (err < 0)
374                 goto report_failure;
375
376         sk->idle_inurb = sk->completed_inurb;
377         sk->completed_inurb = inurb;
378         err = usb_submit_urb(sk->idle_outurb, GFP_ATOMIC);
379         if (err < 0)
380                 goto report_failure;
381
382         sk->idle_outurb = sk->completed_outurb;
383         sk->completed_outurb = outurb;
384         return 0;
385
386 report_failure:
387         snd_printk(KERN_ERR "%i\n", err);
388         return err;
389 }
390
391 #ifdef DEBUG_LOOP_BACK
392 /*
393   This loop_back() shows how to read/write the period data.
394  */
395 static void loop_back(struct usb_stream *s)
396 {
397         char *i, *o;
398         int il, ol, l, p;
399         struct urb *iu;
400         struct usb_iso_packet_descriptor *id;
401
402         o = s->playback1st_to;
403         ol = s->playback1st_size;
404         l = 0;
405
406         if (s->insplit_pack >= 0) {
407                 iu = sk->idle_inurb;
408                 id = iu->iso_frame_desc;
409                 p = s->insplit_pack;
410         } else
411                 goto second;
412 loop:
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;
418                 if (il <= ol) {
419                         memcpy(o, i, il);
420                         o += il;
421                         ol -= il;
422                 } else {
423                         memcpy(o, i, ol);
424                         singen_6pack(o, ol);
425                         o = s->playback_to;
426                         memcpy(o, i + ol, il - ol);
427                         o += il - ol;
428                         ol = s->period_size - s->playback1st_size;
429                 }
430                 l += il;
431         }
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);
436
437                 return;
438         }
439 second:
440         iu = sk->completed_inurb;
441         id = iu->iso_frame_desc;
442         p = 0;
443         goto loop;
444
445 }
446 #else
447 static void loop_back(struct usb_stream *s)
448 {
449 }
450 #endif
451
452 static void stream_idle(struct usb_stream_kernel *sk,
453                         struct urb *inurb, struct urb *outurb)
454 {
455         struct usb_stream *s = sk->s;
456         int l, p;
457         int insize = s->idle_insize;
458         int urb_size = 0;
459
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;
464
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",
470                                    id[p].status);
471                         goto err_out;
472                 }
473                 s->inpacket_head++;
474                 s->inpacket_head %= s->inpackets;
475                 if (s->inpacket_split == -1)
476                         s->inpacket_split = s->inpacket_head;
477
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;
485                 }
486                 insize += l;
487                 urb_size += l;
488         }
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);
493                 goto err_out;
494         }
495         s->insize_done += urb_size;
496
497         l = s->idle_outsize;
498         s->outpacket[0].offset = (sk->idle_outurb->transfer_buffer -
499                                   sk->write_page) - l;
500
501         if (usb_stream_prepare_playback(sk, inurb) < 0)
502                 goto err_out;
503
504         s->outpacket[0].length = sk->idle_outurb->transfer_buffer_length + l;
505         s->outpacket[1].offset = sk->completed_outurb->transfer_buffer -
506                 sk->write_page;
507
508         if (submit_urbs(sk, inurb, outurb) < 0)
509                 goto err_out;
510
511         loop_back(s);
512         s->periods_done++;
513         wake_up_all(&sk->sleep);
514         return;
515 err_out:
516         s->state = usb_stream_xrun;
517         wake_up_all(&sk->sleep);
518 }
519
520 static void i_capture_idle(struct urb *urb)
521 {
522         struct usb_stream_kernel *sk = urb->context;
523         if (balance_capture(sk, urb))
524                 stream_idle(sk, urb, sk->i_urb);
525 }
526
527 static void i_playback_idle(struct urb *urb)
528 {
529         struct usb_stream_kernel *sk = urb->context;
530         if (balance_playback(sk, urb))
531                 stream_idle(sk, sk->i_urb, urb);
532 }
533
534 static void stream_start(struct usb_stream_kernel *sk,
535                          struct urb *inurb, struct urb *outurb)
536 {
537         struct usb_stream *s = sk->s;
538         if (s->state >= usb_stream_sync1) {
539                 int l, p, max_diff, max_diff_0;
540                 int urb_size = 0;
541                 unsigned frames_per_packet, min_frames = 0;
542                 frames_per_packet = (s->period_size - s->idle_insize);
543                 frames_per_packet <<= 8;
544                 frames_per_packet /=
545                         s->cfg.frame_size * inurb->number_of_packets;
546                 frames_per_packet++;
547
548                 max_diff_0 = s->cfg.frame_size;
549                 if (s->cfg.period_frames >= 256)
550                         max_diff_0 <<= 1;
551                 if (s->cfg.period_frames >= 1024)
552                         max_diff_0 <<= 1;
553                 max_diff = max_diff_0;
554                 for (p = 0; p < inurb->number_of_packets; ++p) {
555                         int diff;
556                         l = inurb->iso_frame_desc[p].actual_length;
557                         urb_size += l;
558
559                         min_frames += frames_per_packet;
560                         diff = urb_size -
561                                 (min_frames >> 8) * s->cfg.frame_size;
562                         if (diff < max_diff) {
563                                 snd_printdd(KERN_DEBUG "%i %i %i %i\n",
564                                             s->insize_done,
565                                             urb_size / (int)s->cfg.frame_size,
566                                             inurb->number_of_packets, diff);
567                                 max_diff = diff;
568                         }
569                 }
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);
575                         return;
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;
580                 } else {
581                         unsigned split = s->inpacket_head;
582                         l = s->idle_insize;
583                         while (l > s->inpacket[split].length) {
584                                 l -= s->inpacket[split].length;
585                                 if (split == 0)
586                                         split = s->inpackets - 1;
587                                 else
588                                         split--;
589                         }
590                         s->next_inpacket_split = split;
591                         s->next_inpacket_split_at =
592                                 s->inpacket[split].length - l;
593                 }
594
595                 s->insize_done += urb_size;
596
597                 if (usb_stream_prepare_playback(sk, inurb) < 0)
598                         return;
599
600         } else
601                 playback_prep_freqn(sk, sk->idle_outurb);
602
603         if (submit_urbs(sk, inurb, outurb) < 0)
604                 return;
605
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);
611         }
612 }
613
614 static void i_capture_start(struct urb *urb)
615 {
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;
619         int p;
620         int empty = 0;
621
622         if (urb->status) {
623                 snd_printk(KERN_WARNING "status=%i\n", urb->status);
624                 return;
625         }
626
627         for (p = 0; p < urb->number_of_packets; ++p) {
628                 int l = id[p].actual_length;
629                 if (l < s->cfg.frame_size) {
630                         ++empty;
631                         if (s->state >= usb_stream_sync0) {
632                                 snd_printk(KERN_WARNING "%i\n", l);
633                                 return;
634                         }
635                 }
636                 s->inpacket_head++;
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;
641         }
642 #ifdef SHOW_EMPTY
643         if (empty) {
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);
649                 }
650                 printk(KERN_CONT "\n");
651         }
652 #endif
653         if (!empty && s->state < usb_stream_sync1)
654                 ++s->state;
655
656         if (balance_capture(sk, urb))
657                 stream_start(sk, urb, sk->i_urb);
658 }
659
660 static void i_playback_start(struct urb *urb)
661 {
662         struct usb_stream_kernel *sk = urb->context;
663         if (balance_playback(sk, urb))
664                 stream_start(sk, sk->i_urb, urb);
665 }
666
667 int usb_stream_start(struct usb_stream_kernel *sk)
668 {
669         struct usb_stream *s = sk->s;
670         int frame = 0, iters = 0;
671         int u, err;
672         int try = 0;
673
674         if (s->state != usb_stream_stopped)
675                 return -EAGAIN;
676
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);
680 dotry:
681         s->insize_done = 0;
682         s->idle_insize = 0;
683         s->idle_outsize = 0;
684         s->sync_packet = -1;
685         s->inpacket_head = -1;
686         sk->iso_frame_balance = 0;
687         ++try;
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;
696
697                 if (u == 0) {
698                         int now;
699                         struct usb_device *dev = inurb->dev;
700                         frame = usb_get_current_frame_number(dev);
701                         do {
702                                 now = usb_get_current_frame_number(dev);
703                                 ++iters;
704                         } while (now > -1 && now == frame);
705                 }
706                 err = usb_submit_urb(inurb, GFP_ATOMIC);
707                 if (err < 0) {
708                         snd_printk(KERN_ERR"usb_submit_urb(sk->inurb[%i])"
709                                    " returned %i\n", u, err);
710                         return err;
711                 }
712                 err = usb_submit_urb(outurb, GFP_ATOMIC);
713                 if (err < 0) {
714                         snd_printk(KERN_ERR"usb_submit_urb(sk->outurb[%i])"
715                                    " returned %i\n", u, err);
716                         return err;
717                 }
718
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);
723                         goto check_retry;
724                 }
725         }
726         snd_printdd(KERN_DEBUG "%i %i\n", frame, iters);
727         try = 0;
728 check_retry:
729         if (try) {
730                 usb_stream_stop(sk);
731                 if (try < 5) {
732                         msleep(1500);
733                         snd_printd(KERN_DEBUG "goto dotry;\n");
734                         goto dotry;
735                 }
736                 snd_printk(KERN_WARNING"couldn't start"
737                            " all urbs on the same start_frame.\n");
738                 return -EFAULT;
739         }
740
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];
745
746 /* wait, check */
747         {
748                 int wait_ms = 3000;
749                 while (s->state != usb_stream_ready && wait_ms > 0) {
750                         snd_printdd(KERN_DEBUG "%i\n", s->state);
751                         msleep(200);
752                         wait_ms -= 200;
753                 }
754         }
755
756         return s->state == usb_stream_ready ? 0 : -EFAULT;
757 }
758
759
760 /*                             stop                                   */
761
762 void usb_stream_stop(struct usb_stream_kernel *sk)
763 {
764         int u;
765         if (!sk->s)
766                 return;
767         for (u = 0; u < USB_STREAM_NURBS; ++u) {
768                 usb_kill_urb(sk->inurb[u]);
769                 usb_kill_urb(sk->outurb[u]);
770         }
771         sk->s->state = usb_stream_stopped;
772         msleep(400);
773 }