usb_stream.c 19 KB

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