1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2007, 2008 Karsten Wiese <fzu@wemgehoertderstaat.de>
4 */
5
6#include <linux/usb.h>
7#include <linux/gfp.h>
8
9#include "usb_stream.h"
10
11
12/*                             setup                                  */
13
14static unsigned usb_stream_next_packet_size(struct usb_stream_kernel *sk)
15{
16	struct usb_stream *s = sk->s;
17	sk->out_phase_peeked = (sk->out_phase & 0xffff) + sk->freqn;
18	return (sk->out_phase_peeked >> 16) * s->cfg.frame_size;
19}
20
21static void playback_prep_freqn(struct usb_stream_kernel *sk, struct urb *urb)
22{
23	struct usb_stream *s = sk->s;
24	int pack, lb = 0;
25
26	for (pack = 0; pack < sk->n_o_ps; pack++) {
27		int l = usb_stream_next_packet_size(sk);
28		if (s->idle_outsize + lb + l > s->period_size)
29			goto check;
30
31		sk->out_phase = sk->out_phase_peeked;
32		urb->iso_frame_desc[pack].offset = lb;
33		urb->iso_frame_desc[pack].length = l;
34		lb += l;
35	}
36	snd_printdd(KERN_DEBUG "%i\n", lb);
37
38check:
39	urb->number_of_packets = pack;
40	urb->transfer_buffer_length = lb;
41	s->idle_outsize += lb - s->period_size;
42	snd_printdd(KERN_DEBUG "idle=%i ul=%i ps=%i\n", s->idle_outsize,
43		    lb, s->period_size);
44}
45
46static int init_pipe_urbs(struct usb_stream_kernel *sk, unsigned use_packsize,
47			   struct urb **urbs, char *transfer,
48			   struct usb_device *dev, int pipe)
49{
50	int u, p;
51	int maxpacket = use_packsize ?
52		use_packsize : usb_maxpacket(dev, pipe, usb_pipeout(pipe));
53	int transfer_length = maxpacket * sk->n_o_ps;
54
55	for (u = 0; u < USB_STREAM_NURBS;
56	     ++u, transfer += transfer_length) {
57		struct urb *urb = urbs[u];
58		struct usb_iso_packet_descriptor *desc;
59		urb->transfer_buffer = transfer;
60		urb->dev = dev;
61		urb->pipe = pipe;
62		urb->number_of_packets = sk->n_o_ps;
63		urb->context = sk;
64		urb->interval = 1;
65		if (usb_pipeout(pipe))
66			continue;
67		if (usb_urb_ep_type_check(urb))
68			return -EINVAL;
69
70		urb->transfer_buffer_length = transfer_length;
71		desc = urb->iso_frame_desc;
72		desc->offset = 0;
73		desc->length = maxpacket;
74		for (p = 1; p < sk->n_o_ps; ++p) {
75			desc[p].offset = desc[p - 1].offset + maxpacket;
76			desc[p].length = maxpacket;
77		}
78	}
79
80	return 0;
81}
82
83static int init_urbs(struct usb_stream_kernel *sk, unsigned use_packsize,
84		      struct usb_device *dev, int in_pipe, int out_pipe)
85{
86	struct usb_stream	*s = sk->s;
87	char			*indata = (char *)s + sizeof(*s) +
88					sizeof(struct usb_stream_packet) *
89					s->inpackets;
90	int			u;
91
92	for (u = 0; u < USB_STREAM_NURBS; ++u) {
93		sk->inurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
94		if (!sk->inurb[u])
95			return -ENOMEM;
96
97		sk->outurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
98		if (!sk->outurb[u])
99			return -ENOMEM;
100	}
101
102	if (init_pipe_urbs(sk, use_packsize, sk->inurb, indata, dev, in_pipe) ||
103	    init_pipe_urbs(sk, use_packsize, sk->outurb, sk->write_page, dev,
104			   out_pipe))
105		return -EINVAL;
106
107	return 0;
108}
109
110
111/*
112 * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
113 * this will overflow at approx 524 kHz
114 */
115static inline unsigned get_usb_full_speed_rate(unsigned rate)
116{
117	return ((rate << 13) + 62) / 125;
118}
119
120/*
121 * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
122 * this will overflow at approx 4 MHz
123 */
124static inline unsigned get_usb_high_speed_rate(unsigned rate)
125{
126	return ((rate << 10) + 62) / 125;
127}
128
129void usb_stream_free(struct usb_stream_kernel *sk)
130{
131	struct usb_stream *s;
132	unsigned u;
133
134	for (u = 0; u < USB_STREAM_NURBS; ++u) {
135		usb_free_urb(sk->inurb[u]);
136		sk->inurb[u] = NULL;
137		usb_free_urb(sk->outurb[u]);
138		sk->outurb[u] = NULL;
139	}
140
141	s = sk->s;
142	if (!s)
143		return;
144
145	if (sk->write_page) {
146		free_pages_exact(sk->write_page, s->write_size);
147		sk->write_page = NULL;
148	}
149
150	free_pages_exact(s, s->read_size);
151	sk->s = NULL;
152}
153
154struct usb_stream *usb_stream_new(struct usb_stream_kernel *sk,
155				  struct usb_device *dev,
156				  unsigned in_endpoint, unsigned out_endpoint,
157				  unsigned sample_rate, unsigned use_packsize,
158				  unsigned period_frames, unsigned frame_size)
159{
160	int packets, max_packsize;
161	int in_pipe, out_pipe;
162	int read_size = sizeof(struct usb_stream);
163	int write_size;
164	int usb_frames = dev->speed == USB_SPEED_HIGH ? 8000 : 1000;
165
166	in_pipe = usb_rcvisocpipe(dev, in_endpoint);
167	out_pipe = usb_sndisocpipe(dev, out_endpoint);
168
169	max_packsize = use_packsize ?
170		use_packsize : usb_maxpacket(dev, in_pipe, 0);
171
172	/*
173		t_period = period_frames / sample_rate
174		iso_packs = t_period / t_iso_frame
175			= (period_frames / sample_rate) * (1 / t_iso_frame)
176	*/
177
178	packets = period_frames * usb_frames / sample_rate + 1;
179
180	if (dev->speed == USB_SPEED_HIGH)
181		packets = (packets + 7) & ~7;
182
183	read_size += packets * USB_STREAM_URBDEPTH *
184		(max_packsize + sizeof(struct usb_stream_packet));
185
186	max_packsize = usb_maxpacket(dev, out_pipe, 1);
187	write_size = max_packsize * packets * USB_STREAM_URBDEPTH;
188
189	if (read_size >= 256*PAGE_SIZE || write_size >= 256*PAGE_SIZE) {
190		snd_printk(KERN_WARNING "a size exceeds 128*PAGE_SIZE\n");
191		goto out;
192	}
193
194	sk->s = alloc_pages_exact(read_size,
195				  GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN);
196	if (!sk->s) {
197		pr_warn("us122l: couldn't allocate read buffer\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
213	sk->write_page = alloc_pages_exact(write_size,
214					   GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN);
215	if (!sk->write_page) {
216		pr_warn("us122l: couldn't allocate write buffer\n");
217		usb_stream_free(sk);
218		return NULL;
219	}
220
221	/* calculate the frequency in 16.16 format */
222	if (dev->speed == USB_SPEED_FULL)
223		sk->freqn = get_usb_full_speed_rate(sample_rate);
224	else
225		sk->freqn = get_usb_high_speed_rate(sample_rate);
226
227	if (init_urbs(sk, use_packsize, dev, in_pipe, out_pipe) < 0) {
228		usb_stream_free(sk);
229		return NULL;
230	}
231
232	sk->s->state = usb_stream_stopped;
233out:
234	return sk->s;
235}
236
237
238/*                             start                                  */
239
240static bool balance_check(struct usb_stream_kernel *sk, struct urb *urb)
241{
242	bool r;
243	if (unlikely(urb->status)) {
244		if (urb->status != -ESHUTDOWN && urb->status != -ENOENT)
245			snd_printk(KERN_WARNING "status=%i\n", urb->status);
246		sk->iso_frame_balance = 0x7FFFFFFF;
247		return false;
248	}
249	r = sk->iso_frame_balance == 0;
250	if (!r)
251		sk->i_urb = urb;
252	return r;
253}
254
255static bool balance_playback(struct usb_stream_kernel *sk, struct urb *urb)
256{
257	sk->iso_frame_balance += urb->number_of_packets;
258	return balance_check(sk, urb);
259}
260
261static bool balance_capture(struct usb_stream_kernel *sk, struct urb *urb)
262{
263	sk->iso_frame_balance -= urb->number_of_packets;
264	return balance_check(sk, urb);
265}
266
267static void subs_set_complete(struct urb **urbs, void (*complete)(struct urb *))
268{
269	int u;
270
271	for (u = 0; u < USB_STREAM_NURBS; u++) {
272		struct urb *urb = urbs[u];
273		urb->complete = complete;
274	}
275}
276
277static int usb_stream_prepare_playback(struct usb_stream_kernel *sk,
278		struct urb *inurb)
279{
280	struct usb_stream *s = sk->s;
281	struct urb *io;
282	struct usb_iso_packet_descriptor *id, *od;
283	int p = 0, lb = 0, l = 0;
284
285	io = sk->idle_outurb;
286	od = io->iso_frame_desc;
287
288	for (; s->sync_packet < 0; ++p, ++s->sync_packet) {
289		struct urb *ii = sk->completed_inurb;
290		id = ii->iso_frame_desc +
291			ii->number_of_packets + s->sync_packet;
292		l = id->actual_length;
293
294		od[p].length = l;
295		od[p].offset = lb;
296		lb += l;
297	}
298
299	for (;
300	     s->sync_packet < inurb->number_of_packets && p < sk->n_o_ps;
301	     ++p, ++s->sync_packet) {
302		l = inurb->iso_frame_desc[s->sync_packet].actual_length;
303
304		if (s->idle_outsize + lb + l > s->period_size)
305			goto check_ok;
306
307		od[p].length = l;
308		od[p].offset = lb;
309		lb += l;
310	}
311
312check_ok:
313	s->sync_packet -= inurb->number_of_packets;
314	if (unlikely(s->sync_packet < -2 || s->sync_packet > 0)) {
315		snd_printk(KERN_WARNING "invalid sync_packet = %i;"
316			   " p=%i nop=%i %i %x %x %x > %x\n",
317			   s->sync_packet, p, inurb->number_of_packets,
318			   s->idle_outsize + lb + l,
319			   s->idle_outsize, lb,  l,
320			   s->period_size);
321		return -1;
322	}
323	if (unlikely(lb % s->cfg.frame_size)) {
324		snd_printk(KERN_WARNING"invalid outsize = %i\n",
325			   lb);
326		return -1;
327	}
328	s->idle_outsize += lb - s->period_size;
329	io->number_of_packets = p;
330	io->transfer_buffer_length = lb;
331	if (s->idle_outsize <= 0)
332		return 0;
333
334	snd_printk(KERN_WARNING "idle=%i\n", s->idle_outsize);
335	return -1;
336}
337
338static void prepare_inurb(int number_of_packets, struct urb *iu)
339{
340	struct usb_iso_packet_descriptor *id;
341	int p;
342
343	iu->number_of_packets = number_of_packets;
344	id = iu->iso_frame_desc;
345	id->offset = 0;
346	for (p = 0; p < iu->number_of_packets - 1; ++p)
347		id[p + 1].offset = id[p].offset + id[p].length;
348
349	iu->transfer_buffer_length =
350		id[0].length * iu->number_of_packets;
351}
352
353static int submit_urbs(struct usb_stream_kernel *sk,
354		       struct urb *inurb, struct urb *outurb)
355{
356	int err;
357	prepare_inurb(sk->idle_outurb->number_of_packets, sk->idle_inurb);
358	err = usb_submit_urb(sk->idle_inurb, GFP_ATOMIC);
359	if (err < 0)
360		goto report_failure;
361
362	sk->idle_inurb = sk->completed_inurb;
363	sk->completed_inurb = inurb;
364	err = usb_submit_urb(sk->idle_outurb, GFP_ATOMIC);
365	if (err < 0)
366		goto report_failure;
367
368	sk->idle_outurb = sk->completed_outurb;
369	sk->completed_outurb = outurb;
370	return 0;
371
372report_failure:
373	snd_printk(KERN_ERR "%i\n", err);
374	return err;
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(KERN_WARNING "%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(KERN_CONT " %i", l);
635		}
636		printk(KERN_CONT "\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
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			snd_printk(KERN_ERR"usb_submit_urb(sk->inurb[%i])"
695				   " returned %i\n", u, err);
696			return err;
697		}
698		err = usb_submit_urb(outurb, GFP_ATOMIC);
699		if (err < 0) {
700			snd_printk(KERN_ERR"usb_submit_urb(sk->outurb[%i])"
701				   " returned %i\n", u, err);
702			return err;
703		}
704
705		if (inurb->start_frame != outurb->start_frame) {
706			snd_printd(KERN_DEBUG
707				   "u[%i] start_frames differ in:%u out:%u\n",
708				   u, inurb->start_frame, outurb->start_frame);
709			goto check_retry;
710		}
711	}
712	snd_printdd(KERN_DEBUG "%i %i\n", frame, iters);
713	try = 0;
714check_retry:
715	if (try) {
716		usb_stream_stop(sk);
717		if (try < 5) {
718			msleep(1500);
719			snd_printd(KERN_DEBUG "goto dotry;\n");
720			goto dotry;
721		}
722		snd_printk(KERN_WARNING"couldn't start"
723			   " all urbs on the same start_frame.\n");
724		return -EFAULT;
725	}
726
727	sk->idle_inurb = sk->inurb[USB_STREAM_NURBS - 2];
728	sk->idle_outurb = sk->outurb[USB_STREAM_NURBS - 2];
729	sk->completed_inurb = sk->inurb[USB_STREAM_NURBS - 1];
730	sk->completed_outurb = sk->outurb[USB_STREAM_NURBS - 1];
731
732/* wait, check */
733	{
734		int wait_ms = 3000;
735		while (s->state != usb_stream_ready && wait_ms > 0) {
736			snd_printdd(KERN_DEBUG "%i\n", s->state);
737			msleep(200);
738			wait_ms -= 200;
739		}
740	}
741
742	return s->state == usb_stream_ready ? 0 : -EFAULT;
743}
744
745
746/*                             stop                                   */
747
748void usb_stream_stop(struct usb_stream_kernel *sk)
749{
750	int u;
751	if (!sk->s)
752		return;
753	for (u = 0; u < USB_STREAM_NURBS; ++u) {
754		usb_kill_urb(sk->inurb[u]);
755		usb_kill_urb(sk->outurb[u]);
756	}
757	sk->s->state = usb_stream_stopped;
758	msleep(400);
759}
760