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