usb_stream.c 20 KB

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