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