usb_stream.c 19 KB

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