usb_stream.c 20 KB

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