aboutsummaryrefslogtreecommitdiffstats
path: root/sound/usb/usx2y/usb_stream.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/usb/usx2y/usb_stream.c')
-rw-r--r--sound/usb/usx2y/usb_stream.c761
1 files changed, 761 insertions, 0 deletions
diff --git a/sound/usb/usx2y/usb_stream.c b/sound/usb/usx2y/usb_stream.c
new file mode 100644
index 000000000000..ff23cc1ce3b9
--- /dev/null
+++ b/sound/usb/usx2y/usb_stream.c
@@ -0,0 +1,761 @@
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
26static 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
33static 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
57check:
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
64static 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
98static 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 */
122static 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 */
131static inline unsigned get_usb_high_speed_rate(unsigned rate)
132{
133 return ((rate << 10) + 62) / 125;
134}
135
136void 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
158struct 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;
235out:
236 return sk->s;
237}
238
239
240/* start */
241
242static 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
257static 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
263static 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
269static 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
279int usb_stream_prepare_playback(struct usb_stream_kernel *sk, struct urb *inurb)
280{
281 struct usb_stream *s = sk->s;
282 struct urb *io;
283 struct usb_iso_packet_descriptor *id, *od;
284 int p, l = 0;
285
286 io = sk->idle_outurb;
287 od = io->iso_frame_desc;
288 io->transfer_buffer_length = 0;
289
290 for (p = 0; s->sync_packet < 0; ++p, ++s->sync_packet) {
291 struct urb *ii = sk->completed_inurb;
292 id = ii->iso_frame_desc +
293 ii->number_of_packets + s->sync_packet;
294 l = id->actual_length;
295
296 od[p].length = l;
297 od[p].offset = io->transfer_buffer_length;
298 io->transfer_buffer_length += l;
299 }
300
301 for (;
302 s->sync_packet < inurb->number_of_packets && p < sk->n_o_ps;
303 ++p, ++s->sync_packet) {
304 l = inurb->iso_frame_desc[s->sync_packet].actual_length;
305
306 if (s->idle_outsize + io->transfer_buffer_length + l >
307 s->period_size)
308 goto check_ok;
309
310 od[p].length = l;
311 od[p].offset = io->transfer_buffer_length;
312 io->transfer_buffer_length += l;
313 }
314
315check_ok:
316 s->sync_packet -= inurb->number_of_packets;
317 if (s->sync_packet < -2 || s->sync_packet > 0) {
318 snd_printk(KERN_WARNING "invalid sync_packet = %i;"
319 " p=%i nop=%i %i %x %x %x > %x\n",
320 s->sync_packet, p, inurb->number_of_packets,
321 s->idle_outsize + io->transfer_buffer_length + l,
322 s->idle_outsize, io->transfer_buffer_length, l,
323 s->period_size);
324 return -1;
325 }
326 if (io->transfer_buffer_length % s->cfg.frame_size) {
327 snd_printk(KERN_WARNING"invalid outsize = %i\n",
328 io->transfer_buffer_length);
329 return -1;
330 }
331 s->idle_outsize += io->transfer_buffer_length - s->period_size;
332 io->number_of_packets = p;
333 if (s->idle_outsize > 0) {
334 snd_printk(KERN_WARNING "idle=%i\n", s->idle_outsize);
335 return -1;
336 }
337 return 0;
338}
339
340static void prepare_inurb(int number_of_packets, struct urb *iu)
341{
342 struct usb_iso_packet_descriptor *id;
343 int p;
344
345 iu->number_of_packets = number_of_packets;
346 id = iu->iso_frame_desc;
347 id->offset = 0;
348 for (p = 0; p < iu->number_of_packets - 1; ++p)
349 id[p + 1].offset = id[p].offset + id[p].length;
350
351 iu->transfer_buffer_length =
352 id[0].length * iu->number_of_packets;
353}
354
355static int submit_urbs(struct usb_stream_kernel *sk,
356 struct urb *inurb, struct urb *outurb)
357{
358 int err;
359 prepare_inurb(sk->idle_outurb->number_of_packets, sk->idle_inurb);
360 err = usb_submit_urb(sk->idle_inurb, GFP_ATOMIC);
361 if (err < 0) {
362 snd_printk(KERN_ERR "%i\n", err);
363 return err;
364 }
365 sk->idle_inurb = sk->completed_inurb;
366 sk->completed_inurb = inurb;
367 err = usb_submit_urb(sk->idle_outurb, GFP_ATOMIC);
368 if (err < 0) {
369 snd_printk(KERN_ERR "%i\n", err);
370 return err;
371 }
372 sk->idle_outurb = sk->completed_outurb;
373 sk->completed_outurb = outurb;
374 return 0;
375}
376
377#ifdef DEBUG_LOOP_BACK
378/*
379 This loop_back() shows how to read/write the period data.
380 */
381static void loop_back(struct usb_stream *s)
382{
383 char *i, *o;
384 int il, ol, l, p;
385 struct urb *iu;
386 struct usb_iso_packet_descriptor *id;
387
388 o = s->playback1st_to;
389 ol = s->playback1st_size;
390 l = 0;
391
392 if (s->insplit_pack >= 0) {
393 iu = sk->idle_inurb;
394 id = iu->iso_frame_desc;
395 p = s->insplit_pack;
396 } else
397 goto second;
398loop:
399 for (; p < iu->number_of_packets && l < s->period_size; ++p) {
400 i = iu->transfer_buffer + id[p].offset;
401 il = id[p].actual_length;
402 if (l + il > s->period_size)
403 il = s->period_size - l;
404 if (il <= ol) {
405 memcpy(o, i, il);
406 o += il;
407 ol -= il;
408 } else {
409 memcpy(o, i, ol);
410 singen_6pack(o, ol);
411 o = s->playback_to;
412 memcpy(o, i + ol, il - ol);
413 o += il - ol;
414 ol = s->period_size - s->playback1st_size;
415 }
416 l += il;
417 }
418 if (iu == sk->completed_inurb) {
419 if (l != s->period_size)
420 printk(KERN_DEBUG"%s:%i %i\n", __func__, __LINE__,
421 l/(int)s->cfg.frame_size);
422
423 return;
424 }
425second:
426 iu = sk->completed_inurb;
427 id = iu->iso_frame_desc;
428 p = 0;
429 goto loop;
430
431}
432#else
433static void loop_back(struct usb_stream *s)
434{
435}
436#endif
437
438static void stream_idle(struct usb_stream_kernel *sk,
439 struct urb *inurb, struct urb *outurb)
440{
441 struct usb_stream *s = sk->s;
442 int l, p;
443 int insize = s->idle_insize;
444 int urb_size = 0;
445
446 s->inpacket_split = s->next_inpacket_split;
447 s->inpacket_split_at = s->next_inpacket_split_at;
448 s->next_inpacket_split = -1;
449 s->next_inpacket_split_at = 0;
450
451 for (p = 0; p < inurb->number_of_packets; ++p) {
452 struct usb_iso_packet_descriptor *id = inurb->iso_frame_desc;
453 l = id[p].actual_length;
454 if (unlikely(l == 0 || id[p].status)) {
455 snd_printk(KERN_WARNING "underrun, status=%u\n",
456 id[p].status);
457 goto err_out;
458 }
459 s->inpacket_head++;
460 s->inpacket_head %= s->inpackets;
461 if (s->inpacket_split == -1)
462 s->inpacket_split = s->inpacket_head;
463
464 s->inpacket[s->inpacket_head].offset =
465 id[p].offset + (inurb->transfer_buffer - (void *)s);
466 s->inpacket[s->inpacket_head].length = l;
467 if (insize + l > s->period_size &&
468 s->next_inpacket_split == -1) {
469 s->next_inpacket_split = s->inpacket_head;
470 s->next_inpacket_split_at = s->period_size - insize;
471 }
472 insize += l;
473 urb_size += l;
474 }
475 s->idle_insize += urb_size - s->period_size;
476 if (s->idle_insize < 0) {
477 snd_printk(KERN_WARNING "%i\n",
478 (s->idle_insize)/(int)s->cfg.frame_size);
479 goto err_out;
480 }
481 s->insize_done += urb_size;
482
483 l = s->idle_outsize;
484 s->outpacket[0].offset = (sk->idle_outurb->transfer_buffer -
485 sk->write_page) - l;
486
487 if (usb_stream_prepare_playback(sk, inurb) < 0)
488 goto err_out;
489
490 s->outpacket[0].length = sk->idle_outurb->transfer_buffer_length + l;
491 s->outpacket[1].offset = sk->completed_outurb->transfer_buffer -
492 sk->write_page;
493
494 if (submit_urbs(sk, inurb, outurb) < 0)
495 goto err_out;
496
497 loop_back(s);
498 s->periods_done++;
499 wake_up_all(&sk->sleep);
500 return;
501err_out:
502 s->state = usb_stream_xrun;
503 wake_up_all(&sk->sleep);
504}
505
506static void i_capture_idle(struct urb *urb)
507{
508 struct usb_stream_kernel *sk = urb->context;
509 if (balance_capture(sk, urb))
510 stream_idle(sk, urb, sk->i_urb);
511}
512
513static void i_playback_idle(struct urb *urb)
514{
515 struct usb_stream_kernel *sk = urb->context;
516 if (balance_playback(sk, urb))
517 stream_idle(sk, sk->i_urb, urb);
518}
519
520static void stream_start(struct usb_stream_kernel *sk,
521 struct urb *inurb, struct urb *outurb)
522{
523 struct usb_stream *s = sk->s;
524 if (s->state >= usb_stream_sync1) {
525 int l, p, max_diff, max_diff_0;
526 int urb_size = 0;
527 unsigned frames_per_packet, min_frames = 0;
528 frames_per_packet = (s->period_size - s->idle_insize);
529 frames_per_packet <<= 8;
530 frames_per_packet /=
531 s->cfg.frame_size * inurb->number_of_packets;
532 frames_per_packet++;
533
534 max_diff_0 = s->cfg.frame_size;
535 if (s->cfg.period_frames >= 256)
536 max_diff_0 <<= 1;
537 if (s->cfg.period_frames >= 1024)
538 max_diff_0 <<= 1;
539 max_diff = max_diff_0;
540 for (p = 0; p < inurb->number_of_packets; ++p) {
541 int diff;
542 l = inurb->iso_frame_desc[p].actual_length;
543 urb_size += l;
544
545 min_frames += frames_per_packet;
546 diff = urb_size -
547 (min_frames >> 8) * s->cfg.frame_size;
548 if (diff < max_diff) {
549 snd_printdd(KERN_DEBUG "%i %i %i %i\n",
550 s->insize_done,
551 urb_size / (int)s->cfg.frame_size,
552 inurb->number_of_packets, diff);
553 max_diff = diff;
554 }
555 }
556 s->idle_insize -= max_diff - max_diff_0;
557 s->idle_insize += urb_size - s->period_size;
558 if (s->idle_insize < 0) {
559 snd_printk("%i %i %i\n",
560 s->idle_insize, urb_size, s->period_size);
561 return;
562 } else if (s->idle_insize == 0) {
563 s->next_inpacket_split =
564 (s->inpacket_head + 1) % s->inpackets;
565 s->next_inpacket_split_at = 0;
566 } else {
567 unsigned split = s->inpacket_head;
568 l = s->idle_insize;
569 while (l > s->inpacket[split].length) {
570 l -= s->inpacket[split].length;
571 if (split == 0)
572 split = s->inpackets - 1;
573 else
574 split--;
575 }
576 s->next_inpacket_split = split;
577 s->next_inpacket_split_at =
578 s->inpacket[split].length - l;
579 }
580
581 s->insize_done += urb_size;
582
583 if (usb_stream_prepare_playback(sk, inurb) < 0)
584 return;
585
586 } else
587 playback_prep_freqn(sk, sk->idle_outurb);
588
589 if (submit_urbs(sk, inurb, outurb) < 0)
590 return;
591
592 if (s->state == usb_stream_sync1 && s->insize_done > 360000) {
593 /* just guesswork ^^^^^^ */
594 s->state = usb_stream_ready;
595 subs_set_complete(sk->inurb, i_capture_idle);
596 subs_set_complete(sk->outurb, i_playback_idle);
597 }
598}
599
600static void i_capture_start(struct urb *urb)
601{
602 struct usb_iso_packet_descriptor *id = urb->iso_frame_desc;
603 struct usb_stream_kernel *sk = urb->context;
604 struct usb_stream *s = sk->s;
605 int p;
606 int empty = 0;
607
608 if (urb->status) {
609 snd_printk(KERN_WARNING "status=%i\n", urb->status);
610 return;
611 }
612
613 for (p = 0; p < urb->number_of_packets; ++p) {
614 int l = id[p].actual_length;
615 if (l < s->cfg.frame_size) {
616 ++empty;
617 if (s->state >= usb_stream_sync0) {
618 snd_printk(KERN_WARNING "%i\n", l);
619 return;
620 }
621 }
622 s->inpacket_head++;
623 s->inpacket_head %= s->inpackets;
624 s->inpacket[s->inpacket_head].offset =
625 id[p].offset + (urb->transfer_buffer - (void *)s);
626 s->inpacket[s->inpacket_head].length = l;
627 }
628#ifdef SHOW_EMPTY
629 if (empty) {
630 printk(KERN_DEBUG"%s:%i: %i", __func__, __LINE__,
631 urb->iso_frame_desc[0].actual_length);
632 for (pack = 1; pack < urb->number_of_packets; ++pack) {
633 int l = urb->iso_frame_desc[pack].actual_length;
634 printk(" %i", l);
635 }
636 printk("\n");
637 }
638#endif
639 if (!empty && s->state < usb_stream_sync1)
640 ++s->state;
641
642 if (balance_capture(sk, urb))
643 stream_start(sk, urb, sk->i_urb);
644}
645
646static void i_playback_start(struct urb *urb)
647{
648 struct usb_stream_kernel *sk = urb->context;
649 if (balance_playback(sk, urb))
650 stream_start(sk, sk->i_urb, urb);
651}
652
653int usb_stream_start(struct usb_stream_kernel *sk)
654{
655 struct usb_stream *s = sk->s;
656 int frame = 0, iters = 0;
657 int u, err;
658 int try = 0;
659
660 if (s->state != usb_stream_stopped)
661 return -EAGAIN;
662
663 subs_set_complete(sk->inurb, i_capture_start);
664 subs_set_complete(sk->outurb, i_playback_start);
665 memset(sk->write_page, 0, s->write_size);
666dotry:
667 s->insize_done = 0;
668 s->idle_insize = 0;
669 s->idle_outsize = 0;
670 s->sync_packet = -1;
671 s->inpacket_head = -1;
672 sk->iso_frame_balance = 0;
673 ++try;
674 for (u = 0; u < 2; u++) {
675 struct urb *inurb = sk->inurb[u];
676 struct urb *outurb = sk->outurb[u];
677 playback_prep_freqn(sk, outurb);
678 inurb->number_of_packets = outurb->number_of_packets;
679 inurb->transfer_buffer_length =
680 inurb->number_of_packets *
681 inurb->iso_frame_desc[0].length;
682 preempt_disable();
683 if (u == 0) {
684 int now;
685 struct usb_device *dev = inurb->dev;
686 frame = usb_get_current_frame_number(dev);
687 do {
688 now = usb_get_current_frame_number(dev);
689 ++iters;
690 } while (now > -1 && now == frame);
691 }
692 err = usb_submit_urb(inurb, GFP_ATOMIC);
693 if (err < 0) {
694 preempt_enable();
695 snd_printk(KERN_ERR"usb_submit_urb(sk->inurb[%i])"
696 " returned %i\n", u, err);
697 return err;
698 }
699 err = usb_submit_urb(outurb, GFP_ATOMIC);
700 if (err < 0) {
701 preempt_enable();
702 snd_printk(KERN_ERR"usb_submit_urb(sk->outurb[%i])"
703 " returned %i\n", u, err);
704 return err;
705 }
706 preempt_enable();
707 if (inurb->start_frame != outurb->start_frame) {
708 snd_printd(KERN_DEBUG
709 "u[%i] start_frames differ in:%u out:%u\n",
710 u, inurb->start_frame, outurb->start_frame);
711 goto check_retry;
712 }
713 }
714 snd_printdd(KERN_DEBUG "%i %i\n", frame, iters);
715 try = 0;
716check_retry:
717 if (try) {
718 usb_stream_stop(sk);
719 if (try < 5) {
720 msleep(1500);
721 snd_printd(KERN_DEBUG "goto dotry;\n");
722 goto dotry;
723 }
724 snd_printk(KERN_WARNING"couldn't start"
725 " all urbs on the same start_frame.\n");
726 return -EFAULT;
727 }
728
729 sk->idle_inurb = sk->inurb[USB_STREAM_NURBS - 2];
730 sk->idle_outurb = sk->outurb[USB_STREAM_NURBS - 2];
731 sk->completed_inurb = sk->inurb[USB_STREAM_NURBS - 1];
732 sk->completed_outurb = sk->outurb[USB_STREAM_NURBS - 1];
733
734/* wait, check */
735 {
736 int wait_ms = 3000;
737 while (s->state != usb_stream_ready && wait_ms > 0) {
738 snd_printdd(KERN_DEBUG "%i\n", s->state);
739 msleep(200);
740 wait_ms -= 200;
741 }
742 }
743
744 return s->state == usb_stream_ready ? 0 : -EFAULT;
745}
746
747
748/* stop */
749
750void usb_stream_stop(struct usb_stream_kernel *sk)
751{
752 int u;
753 if (!sk->s)
754 return;
755 for (u = 0; u < USB_STREAM_NURBS; ++u) {
756 usb_kill_urb(sk->inurb[u]);
757 usb_kill_urb(sk->outurb[u]);
758 }
759 sk->s->state = usb_stream_stopped;
760 msleep(400);
761}