usb_stream.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. #include <linux/usb.h>
  19. #include <linux/gfp.h>
  20. #include "usb_stream.h"
  21. /* setup */
  22. static unsigned usb_stream_next_packet_size(struct usb_stream_kernel *sk)
  23. {
  24. struct usb_stream *s = sk->s;
  25. sk->out_phase_peeked = (sk->out_phase & 0xffff) + sk->freqn;
  26. return (sk->out_phase_peeked >> 16) * s->cfg.frame_size;
  27. }
  28. static void playback_prep_freqn(struct usb_stream_kernel *sk, struct urb *urb)
  29. {
  30. struct usb_stream *s = sk->s;
  31. int pack, lb = 0;
  32. for (pack = 0; pack < sk->n_o_ps; pack++) {
  33. int l = usb_stream_next_packet_size(sk);
  34. if (s->idle_outsize + lb + l > s->period_size)
  35. goto check;
  36. sk->out_phase = sk->out_phase_peeked;
  37. urb->iso_frame_desc[pack].offset = lb;
  38. urb->iso_frame_desc[pack].length = l;
  39. lb += l;
  40. }
  41. snd_printdd(KERN_DEBUG "%i\n", lb);
  42. check:
  43. urb->number_of_packets = pack;
  44. urb->transfer_buffer_length = lb;
  45. s->idle_outsize += lb - s->period_size;
  46. snd_printdd(KERN_DEBUG "idle=%i ul=%i ps=%i\n", s->idle_outsize,
  47. lb, s->period_size);
  48. }
  49. static void init_pipe_urbs(struct usb_stream_kernel *sk, unsigned use_packsize,
  50. struct urb **urbs, char *transfer,
  51. struct usb_device *dev, int pipe)
  52. {
  53. int u, p;
  54. int maxpacket = use_packsize ?
  55. use_packsize : usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  56. int transfer_length = maxpacket * sk->n_o_ps;
  57. for (u = 0; u < USB_STREAM_NURBS;
  58. ++u, transfer += transfer_length) {
  59. struct urb *urb = urbs[u];
  60. struct usb_iso_packet_descriptor *desc;
  61. urb->transfer_buffer = transfer;
  62. urb->dev = dev;
  63. urb->pipe = pipe;
  64. urb->number_of_packets = sk->n_o_ps;
  65. urb->context = sk;
  66. urb->interval = 1;
  67. if (usb_pipeout(pipe))
  68. continue;
  69. urb->transfer_buffer_length = transfer_length;
  70. desc = urb->iso_frame_desc;
  71. desc->offset = 0;
  72. desc->length = maxpacket;
  73. for (p = 1; p < sk->n_o_ps; ++p) {
  74. desc[p].offset = desc[p - 1].offset + maxpacket;
  75. desc[p].length = maxpacket;
  76. }
  77. }
  78. }
  79. static void init_urbs(struct usb_stream_kernel *sk, unsigned use_packsize,
  80. struct usb_device *dev, int in_pipe, int out_pipe)
  81. {
  82. struct usb_stream *s = sk->s;
  83. char *indata = (char *)s + sizeof(*s) +
  84. sizeof(struct usb_stream_packet) *
  85. s->inpackets;
  86. int u;
  87. for (u = 0; u < USB_STREAM_NURBS; ++u) {
  88. sk->inurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
  89. sk->outurb[u] = usb_alloc_urb(sk->n_o_ps, GFP_KERNEL);
  90. }
  91. init_pipe_urbs(sk, use_packsize, sk->inurb, indata, dev, in_pipe);
  92. init_pipe_urbs(sk, use_packsize, sk->outurb, sk->write_page, dev,
  93. out_pipe);
  94. }
  95. /*
  96. * convert a sampling rate into our full speed format (fs/1000 in Q16.16)
  97. * this will overflow at approx 524 kHz
  98. */
  99. static inline unsigned get_usb_full_speed_rate(unsigned rate)
  100. {
  101. return ((rate << 13) + 62) / 125;
  102. }
  103. /*
  104. * convert a sampling rate into USB high speed format (fs/8000 in Q16.16)
  105. * this will overflow at approx 4 MHz
  106. */
  107. static inline unsigned get_usb_high_speed_rate(unsigned rate)
  108. {
  109. return ((rate << 10) + 62) / 125;
  110. }
  111. void usb_stream_free(struct usb_stream_kernel *sk)
  112. {
  113. struct usb_stream *s;
  114. unsigned u;
  115. for (u = 0; u < USB_STREAM_NURBS; ++u) {
  116. usb_free_urb(sk->inurb[u]);
  117. sk->inurb[u] = NULL;
  118. usb_free_urb(sk->outurb[u]);
  119. sk->outurb[u] = NULL;
  120. }
  121. s = sk->s;
  122. if (!s)
  123. return;
  124. free_pages((unsigned long)sk->write_page, get_order(s->write_size));
  125. sk->write_page = NULL;
  126. free_pages((unsigned long)s, get_order(s->read_size));
  127. sk->s = NULL;
  128. }
  129. struct usb_stream *usb_stream_new(struct usb_stream_kernel *sk,
  130. struct usb_device *dev,
  131. unsigned in_endpoint, unsigned out_endpoint,
  132. unsigned sample_rate, unsigned use_packsize,
  133. unsigned period_frames, unsigned frame_size)
  134. {
  135. int packets, max_packsize;
  136. int in_pipe, out_pipe;
  137. int read_size = sizeof(struct usb_stream);
  138. int write_size;
  139. int usb_frames = dev->speed == USB_SPEED_HIGH ? 8000 : 1000;
  140. int pg;
  141. in_pipe = usb_rcvisocpipe(dev, in_endpoint);
  142. out_pipe = usb_sndisocpipe(dev, out_endpoint);
  143. max_packsize = use_packsize ?
  144. use_packsize : usb_maxpacket(dev, in_pipe, 0);
  145. /*
  146. t_period = period_frames / sample_rate
  147. iso_packs = t_period / t_iso_frame
  148. = (period_frames / sample_rate) * (1 / t_iso_frame)
  149. */
  150. packets = period_frames * usb_frames / sample_rate + 1;
  151. if (dev->speed == USB_SPEED_HIGH)
  152. packets = (packets + 7) & ~7;
  153. read_size += packets * USB_STREAM_URBDEPTH *
  154. (max_packsize + sizeof(struct usb_stream_packet));
  155. max_packsize = usb_maxpacket(dev, out_pipe, 1);
  156. write_size = max_packsize * packets * USB_STREAM_URBDEPTH;
  157. if (read_size >= 256*PAGE_SIZE || write_size >= 256*PAGE_SIZE) {
  158. snd_printk(KERN_WARNING "a size exceeds 128*PAGE_SIZE\n");
  159. goto out;
  160. }
  161. pg = get_order(read_size);
  162. sk->s = (void *) __get_free_pages(GFP_KERNEL|__GFP_COMP|__GFP_ZERO, pg);
  163. if (!sk->s) {
  164. snd_printk(KERN_WARNING "couldn't __get_free_pages()\n");
  165. goto out;
  166. }
  167. sk->s->cfg.version = USB_STREAM_INTERFACE_VERSION;
  168. sk->s->read_size = read_size;
  169. sk->s->cfg.sample_rate = sample_rate;
  170. sk->s->cfg.frame_size = frame_size;
  171. sk->n_o_ps = packets;
  172. sk->s->inpackets = packets * USB_STREAM_URBDEPTH;
  173. sk->s->cfg.period_frames = period_frames;
  174. sk->s->period_size = frame_size * period_frames;
  175. sk->s->write_size = write_size;
  176. pg = get_order(write_size);
  177. sk->write_page =
  178. (void *)__get_free_pages(GFP_KERNEL|__GFP_COMP|__GFP_ZERO, pg);
  179. if (!sk->write_page) {
  180. snd_printk(KERN_WARNING "couldn't __get_free_pages()\n");
  181. usb_stream_free(sk);
  182. return NULL;
  183. }
  184. /* calculate the frequency in 16.16 format */
  185. if (dev->speed == USB_SPEED_FULL)
  186. sk->freqn = get_usb_full_speed_rate(sample_rate);
  187. else
  188. sk->freqn = get_usb_high_speed_rate(sample_rate);
  189. init_urbs(sk, use_packsize, dev, in_pipe, out_pipe);
  190. sk->s->state = usb_stream_stopped;
  191. out:
  192. return sk->s;
  193. }
  194. /* start */
  195. static bool balance_check(struct usb_stream_kernel *sk, struct urb *urb)
  196. {
  197. bool r;
  198. if (unlikely(urb->status)) {
  199. if (urb->status != -ESHUTDOWN && urb->status != -ENOENT)
  200. snd_printk(KERN_WARNING "status=%i\n", urb->status);
  201. sk->iso_frame_balance = 0x7FFFFFFF;
  202. return false;
  203. }
  204. r = sk->iso_frame_balance == 0;
  205. if (!r)
  206. sk->i_urb = urb;
  207. return r;
  208. }
  209. static bool balance_playback(struct usb_stream_kernel *sk, struct urb *urb)
  210. {
  211. sk->iso_frame_balance += urb->number_of_packets;
  212. return balance_check(sk, urb);
  213. }
  214. static bool balance_capture(struct usb_stream_kernel *sk, struct urb *urb)
  215. {
  216. sk->iso_frame_balance -= urb->number_of_packets;
  217. return balance_check(sk, urb);
  218. }
  219. static void subs_set_complete(struct urb **urbs, void (*complete)(struct urb *))
  220. {
  221. int u;
  222. for (u = 0; u < USB_STREAM_NURBS; u++) {
  223. struct urb *urb = urbs[u];
  224. urb->complete = complete;
  225. }
  226. }
  227. static int usb_stream_prepare_playback(struct usb_stream_kernel *sk,
  228. struct urb *inurb)
  229. {
  230. struct usb_stream *s = sk->s;
  231. struct urb *io;
  232. struct usb_iso_packet_descriptor *id, *od;
  233. int p = 0, lb = 0, l = 0;
  234. io = sk->idle_outurb;
  235. od = io->iso_frame_desc;
  236. for (; s->sync_packet < 0; ++p, ++s->sync_packet) {
  237. struct urb *ii = sk->completed_inurb;
  238. id = ii->iso_frame_desc +
  239. ii->number_of_packets + s->sync_packet;
  240. l = id->actual_length;
  241. od[p].length = l;
  242. od[p].offset = lb;
  243. lb += l;
  244. }
  245. for (;
  246. s->sync_packet < inurb->number_of_packets && p < sk->n_o_ps;
  247. ++p, ++s->sync_packet) {
  248. l = inurb->iso_frame_desc[s->sync_packet].actual_length;
  249. if (s->idle_outsize + lb + l > s->period_size)
  250. goto check_ok;
  251. od[p].length = l;
  252. od[p].offset = lb;
  253. lb += l;
  254. }
  255. check_ok:
  256. s->sync_packet -= inurb->number_of_packets;
  257. if (unlikely(s->sync_packet < -2 || s->sync_packet > 0)) {
  258. snd_printk(KERN_WARNING "invalid sync_packet = %i;"
  259. " p=%i nop=%i %i %x %x %x > %x\n",
  260. s->sync_packet, p, inurb->number_of_packets,
  261. s->idle_outsize + lb + l,
  262. s->idle_outsize, lb, l,
  263. s->period_size);
  264. return -1;
  265. }
  266. if (unlikely(lb % s->cfg.frame_size)) {
  267. snd_printk(KERN_WARNING"invalid outsize = %i\n",
  268. lb);
  269. return -1;
  270. }
  271. s->idle_outsize += lb - s->period_size;
  272. io->number_of_packets = p;
  273. io->transfer_buffer_length = lb;
  274. if (s->idle_outsize <= 0)
  275. return 0;
  276. snd_printk(KERN_WARNING "idle=%i\n", s->idle_outsize);
  277. return -1;
  278. }
  279. static void prepare_inurb(int number_of_packets, struct urb *iu)
  280. {
  281. struct usb_iso_packet_descriptor *id;
  282. int p;
  283. iu->number_of_packets = number_of_packets;
  284. id = iu->iso_frame_desc;
  285. id->offset = 0;
  286. for (p = 0; p < iu->number_of_packets - 1; ++p)
  287. id[p + 1].offset = id[p].offset + id[p].length;
  288. iu->transfer_buffer_length =
  289. id[0].length * iu->number_of_packets;
  290. }
  291. static int submit_urbs(struct usb_stream_kernel *sk,
  292. struct urb *inurb, struct urb *outurb)
  293. {
  294. int err;
  295. prepare_inurb(sk->idle_outurb->number_of_packets, sk->idle_inurb);
  296. err = usb_submit_urb(sk->idle_inurb, GFP_ATOMIC);
  297. if (err < 0) {
  298. snd_printk(KERN_ERR "%i\n", err);
  299. return err;
  300. }
  301. sk->idle_inurb = sk->completed_inurb;
  302. sk->completed_inurb = inurb;
  303. err = usb_submit_urb(sk->idle_outurb, GFP_ATOMIC);
  304. if (err < 0) {
  305. snd_printk(KERN_ERR "%i\n", err);
  306. return err;
  307. }
  308. sk->idle_outurb = sk->completed_outurb;
  309. sk->completed_outurb = outurb;
  310. return 0;
  311. }
  312. #ifdef DEBUG_LOOP_BACK
  313. /*
  314. This loop_back() shows how to read/write the period data.
  315. */
  316. static void loop_back(struct usb_stream *s)
  317. {
  318. char *i, *o;
  319. int il, ol, l, p;
  320. struct urb *iu;
  321. struct usb_iso_packet_descriptor *id;
  322. o = s->playback1st_to;
  323. ol = s->playback1st_size;
  324. l = 0;
  325. if (s->insplit_pack >= 0) {
  326. iu = sk->idle_inurb;
  327. id = iu->iso_frame_desc;
  328. p = s->insplit_pack;
  329. } else
  330. goto second;
  331. loop:
  332. for (; p < iu->number_of_packets && l < s->period_size; ++p) {
  333. i = iu->transfer_buffer + id[p].offset;
  334. il = id[p].actual_length;
  335. if (l + il > s->period_size)
  336. il = s->period_size - l;
  337. if (il <= ol) {
  338. memcpy(o, i, il);
  339. o += il;
  340. ol -= il;
  341. } else {
  342. memcpy(o, i, ol);
  343. singen_6pack(o, ol);
  344. o = s->playback_to;
  345. memcpy(o, i + ol, il - ol);
  346. o += il - ol;
  347. ol = s->period_size - s->playback1st_size;
  348. }
  349. l += il;
  350. }
  351. if (iu == sk->completed_inurb) {
  352. if (l != s->period_size)
  353. printk(KERN_DEBUG"%s:%i %i\n", __func__, __LINE__,
  354. l/(int)s->cfg.frame_size);
  355. return;
  356. }
  357. second:
  358. iu = sk->completed_inurb;
  359. id = iu->iso_frame_desc;
  360. p = 0;
  361. goto loop;
  362. }
  363. #else
  364. static void loop_back(struct usb_stream *s)
  365. {
  366. }
  367. #endif
  368. static void stream_idle(struct usb_stream_kernel *sk,
  369. struct urb *inurb, struct urb *outurb)
  370. {
  371. struct usb_stream *s = sk->s;
  372. int l, p;
  373. int insize = s->idle_insize;
  374. int urb_size = 0;
  375. s->inpacket_split = s->next_inpacket_split;
  376. s->inpacket_split_at = s->next_inpacket_split_at;
  377. s->next_inpacket_split = -1;
  378. s->next_inpacket_split_at = 0;
  379. for (p = 0; p < inurb->number_of_packets; ++p) {
  380. struct usb_iso_packet_descriptor *id = inurb->iso_frame_desc;
  381. l = id[p].actual_length;
  382. if (unlikely(l == 0 || id[p].status)) {
  383. snd_printk(KERN_WARNING "underrun, status=%u\n",
  384. id[p].status);
  385. goto err_out;
  386. }
  387. s->inpacket_head++;
  388. s->inpacket_head %= s->inpackets;
  389. if (s->inpacket_split == -1)
  390. s->inpacket_split = s->inpacket_head;
  391. s->inpacket[s->inpacket_head].offset =
  392. id[p].offset + (inurb->transfer_buffer - (void *)s);
  393. s->inpacket[s->inpacket_head].length = l;
  394. if (insize + l > s->period_size &&
  395. s->next_inpacket_split == -1) {
  396. s->next_inpacket_split = s->inpacket_head;
  397. s->next_inpacket_split_at = s->period_size - insize;
  398. }
  399. insize += l;
  400. urb_size += l;
  401. }
  402. s->idle_insize += urb_size - s->period_size;
  403. if (s->idle_insize < 0) {
  404. snd_printk(KERN_WARNING "%i\n",
  405. (s->idle_insize)/(int)s->cfg.frame_size);
  406. goto err_out;
  407. }
  408. s->insize_done += urb_size;
  409. l = s->idle_outsize;
  410. s->outpacket[0].offset = (sk->idle_outurb->transfer_buffer -
  411. sk->write_page) - l;
  412. if (usb_stream_prepare_playback(sk, inurb) < 0)
  413. goto err_out;
  414. s->outpacket[0].length = sk->idle_outurb->transfer_buffer_length + l;
  415. s->outpacket[1].offset = sk->completed_outurb->transfer_buffer -
  416. sk->write_page;
  417. if (submit_urbs(sk, inurb, outurb) < 0)
  418. goto err_out;
  419. loop_back(s);
  420. s->periods_done++;
  421. wake_up_all(&sk->sleep);
  422. return;
  423. err_out:
  424. s->state = usb_stream_xrun;
  425. wake_up_all(&sk->sleep);
  426. }
  427. static void i_capture_idle(struct urb *urb)
  428. {
  429. struct usb_stream_kernel *sk = urb->context;
  430. if (balance_capture(sk, urb))
  431. stream_idle(sk, urb, sk->i_urb);
  432. }
  433. static void i_playback_idle(struct urb *urb)
  434. {
  435. struct usb_stream_kernel *sk = urb->context;
  436. if (balance_playback(sk, urb))
  437. stream_idle(sk, sk->i_urb, urb);
  438. }
  439. static void stream_start(struct usb_stream_kernel *sk,
  440. struct urb *inurb, struct urb *outurb)
  441. {
  442. struct usb_stream *s = sk->s;
  443. if (s->state >= usb_stream_sync1) {
  444. int l, p, max_diff, max_diff_0;
  445. int urb_size = 0;
  446. unsigned frames_per_packet, min_frames = 0;
  447. frames_per_packet = (s->period_size - s->idle_insize);
  448. frames_per_packet <<= 8;
  449. frames_per_packet /=
  450. s->cfg.frame_size * inurb->number_of_packets;
  451. frames_per_packet++;
  452. max_diff_0 = s->cfg.frame_size;
  453. if (s->cfg.period_frames >= 256)
  454. max_diff_0 <<= 1;
  455. if (s->cfg.period_frames >= 1024)
  456. max_diff_0 <<= 1;
  457. max_diff = max_diff_0;
  458. for (p = 0; p < inurb->number_of_packets; ++p) {
  459. int diff;
  460. l = inurb->iso_frame_desc[p].actual_length;
  461. urb_size += l;
  462. min_frames += frames_per_packet;
  463. diff = urb_size -
  464. (min_frames >> 8) * s->cfg.frame_size;
  465. if (diff < max_diff) {
  466. snd_printdd(KERN_DEBUG "%i %i %i %i\n",
  467. s->insize_done,
  468. urb_size / (int)s->cfg.frame_size,
  469. inurb->number_of_packets, diff);
  470. max_diff = diff;
  471. }
  472. }
  473. s->idle_insize -= max_diff - max_diff_0;
  474. s->idle_insize += urb_size - s->period_size;
  475. if (s->idle_insize < 0) {
  476. snd_printk(KERN_WARNING "%i %i %i\n",
  477. s->idle_insize, urb_size, s->period_size);
  478. return;
  479. } else if (s->idle_insize == 0) {
  480. s->next_inpacket_split =
  481. (s->inpacket_head + 1) % s->inpackets;
  482. s->next_inpacket_split_at = 0;
  483. } else {
  484. unsigned split = s->inpacket_head;
  485. l = s->idle_insize;
  486. while (l > s->inpacket[split].length) {
  487. l -= s->inpacket[split].length;
  488. if (split == 0)
  489. split = s->inpackets - 1;
  490. else
  491. split--;
  492. }
  493. s->next_inpacket_split = split;
  494. s->next_inpacket_split_at =
  495. s->inpacket[split].length - l;
  496. }
  497. s->insize_done += urb_size;
  498. if (usb_stream_prepare_playback(sk, inurb) < 0)
  499. return;
  500. } else
  501. playback_prep_freqn(sk, sk->idle_outurb);
  502. if (submit_urbs(sk, inurb, outurb) < 0)
  503. return;
  504. if (s->state == usb_stream_sync1 && s->insize_done > 360000) {
  505. /* just guesswork ^^^^^^ */
  506. s->state = usb_stream_ready;
  507. subs_set_complete(sk->inurb, i_capture_idle);
  508. subs_set_complete(sk->outurb, i_playback_idle);
  509. }
  510. }
  511. static void i_capture_start(struct urb *urb)
  512. {
  513. struct usb_iso_packet_descriptor *id = urb->iso_frame_desc;
  514. struct usb_stream_kernel *sk = urb->context;
  515. struct usb_stream *s = sk->s;
  516. int p;
  517. int empty = 0;
  518. if (urb->status) {
  519. snd_printk(KERN_WARNING "status=%i\n", urb->status);
  520. return;
  521. }
  522. for (p = 0; p < urb->number_of_packets; ++p) {
  523. int l = id[p].actual_length;
  524. if (l < s->cfg.frame_size) {
  525. ++empty;
  526. if (s->state >= usb_stream_sync0) {
  527. snd_printk(KERN_WARNING "%i\n", l);
  528. return;
  529. }
  530. }
  531. s->inpacket_head++;
  532. s->inpacket_head %= s->inpackets;
  533. s->inpacket[s->inpacket_head].offset =
  534. id[p].offset + (urb->transfer_buffer - (void *)s);
  535. s->inpacket[s->inpacket_head].length = l;
  536. }
  537. #ifdef SHOW_EMPTY
  538. if (empty) {
  539. printk(KERN_DEBUG"%s:%i: %i", __func__, __LINE__,
  540. urb->iso_frame_desc[0].actual_length);
  541. for (pack = 1; pack < urb->number_of_packets; ++pack) {
  542. int l = urb->iso_frame_desc[pack].actual_length;
  543. printk(" %i", l);
  544. }
  545. printk("\n");
  546. }
  547. #endif
  548. if (!empty && s->state < usb_stream_sync1)
  549. ++s->state;
  550. if (balance_capture(sk, urb))
  551. stream_start(sk, urb, sk->i_urb);
  552. }
  553. static void i_playback_start(struct urb *urb)
  554. {
  555. struct usb_stream_kernel *sk = urb->context;
  556. if (balance_playback(sk, urb))
  557. stream_start(sk, sk->i_urb, urb);
  558. }
  559. int usb_stream_start(struct usb_stream_kernel *sk)
  560. {
  561. struct usb_stream *s = sk->s;
  562. int frame = 0, iters = 0;
  563. int u, err;
  564. int try = 0;
  565. if (s->state != usb_stream_stopped)
  566. return -EAGAIN;
  567. subs_set_complete(sk->inurb, i_capture_start);
  568. subs_set_complete(sk->outurb, i_playback_start);
  569. memset(sk->write_page, 0, s->write_size);
  570. dotry:
  571. s->insize_done = 0;
  572. s->idle_insize = 0;
  573. s->idle_outsize = 0;
  574. s->sync_packet = -1;
  575. s->inpacket_head = -1;
  576. sk->iso_frame_balance = 0;
  577. ++try;
  578. for (u = 0; u < 2; u++) {
  579. struct urb *inurb = sk->inurb[u];
  580. struct urb *outurb = sk->outurb[u];
  581. playback_prep_freqn(sk, outurb);
  582. inurb->number_of_packets = outurb->number_of_packets;
  583. inurb->transfer_buffer_length =
  584. inurb->number_of_packets *
  585. inurb->iso_frame_desc[0].length;
  586. if (u == 0) {
  587. int now;
  588. struct usb_device *dev = inurb->dev;
  589. frame = usb_get_current_frame_number(dev);
  590. do {
  591. now = usb_get_current_frame_number(dev);
  592. ++iters;
  593. } while (now > -1 && now == frame);
  594. }
  595. err = usb_submit_urb(inurb, GFP_ATOMIC);
  596. if (err < 0) {
  597. snd_printk(KERN_ERR"usb_submit_urb(sk->inurb[%i])"
  598. " returned %i\n", u, err);
  599. return err;
  600. }
  601. err = usb_submit_urb(outurb, GFP_ATOMIC);
  602. if (err < 0) {
  603. snd_printk(KERN_ERR"usb_submit_urb(sk->outurb[%i])"
  604. " returned %i\n", u, err);
  605. return err;
  606. }
  607. if (inurb->start_frame != outurb->start_frame) {
  608. snd_printd(KERN_DEBUG
  609. "u[%i] start_frames differ in:%u out:%u\n",
  610. u, inurb->start_frame, outurb->start_frame);
  611. goto check_retry;
  612. }
  613. }
  614. snd_printdd(KERN_DEBUG "%i %i\n", frame, iters);
  615. try = 0;
  616. check_retry:
  617. if (try) {
  618. usb_stream_stop(sk);
  619. if (try < 5) {
  620. msleep(1500);
  621. snd_printd(KERN_DEBUG "goto dotry;\n");
  622. goto dotry;
  623. }
  624. snd_printk(KERN_WARNING"couldn't start"
  625. " all urbs on the same start_frame.\n");
  626. return -EFAULT;
  627. }
  628. sk->idle_inurb = sk->inurb[USB_STREAM_NURBS - 2];
  629. sk->idle_outurb = sk->outurb[USB_STREAM_NURBS - 2];
  630. sk->completed_inurb = sk->inurb[USB_STREAM_NURBS - 1];
  631. sk->completed_outurb = sk->outurb[USB_STREAM_NURBS - 1];
  632. /* wait, check */
  633. {
  634. int wait_ms = 3000;
  635. while (s->state != usb_stream_ready && wait_ms > 0) {
  636. snd_printdd(KERN_DEBUG "%i\n", s->state);
  637. msleep(200);
  638. wait_ms -= 200;
  639. }
  640. }
  641. return s->state == usb_stream_ready ? 0 : -EFAULT;
  642. }
  643. /* stop */
  644. void usb_stream_stop(struct usb_stream_kernel *sk)
  645. {
  646. int u;
  647. if (!sk->s)
  648. return;
  649. for (u = 0; u < USB_STREAM_NURBS; ++u) {
  650. usb_kill_urb(sk->inurb[u]);
  651. usb_kill_urb(sk->outurb[u]);
  652. }
  653. sk->s->state = usb_stream_stopped;
  654. msleep(400);
  655. }