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