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