usb_stream.c 19 KB

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