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