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