capture.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Line 6 Linux USB driver
  3. *
  4. * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. *
  10. */
  11. #include <linux/slab.h>
  12. #include <sound/core.h>
  13. #include <sound/pcm.h>
  14. #include <sound/pcm_params.h>
  15. #include "capture.h"
  16. #include "driver.h"
  17. #include "pcm.h"
  18. /*
  19. Find a free URB and submit it.
  20. must be called in line6pcm->in.lock context
  21. */
  22. static int submit_audio_in_urb(struct snd_line6_pcm *line6pcm)
  23. {
  24. int index;
  25. int i, urb_size;
  26. int ret;
  27. struct urb *urb_in;
  28. index = find_first_zero_bit(&line6pcm->in.active_urbs,
  29. line6pcm->line6->iso_buffers);
  30. if (index < 0 || index >= line6pcm->line6->iso_buffers) {
  31. dev_err(line6pcm->line6->ifcdev, "no free URB found\n");
  32. return -EINVAL;
  33. }
  34. urb_in = line6pcm->in.urbs[index];
  35. urb_size = 0;
  36. for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
  37. struct usb_iso_packet_descriptor *fin =
  38. &urb_in->iso_frame_desc[i];
  39. fin->offset = urb_size;
  40. fin->length = line6pcm->max_packet_size_in;
  41. urb_size += line6pcm->max_packet_size_in;
  42. }
  43. urb_in->transfer_buffer =
  44. line6pcm->in.buffer +
  45. index * LINE6_ISO_PACKETS * line6pcm->max_packet_size_in;
  46. urb_in->transfer_buffer_length = urb_size;
  47. urb_in->context = line6pcm;
  48. ret = usb_submit_urb(urb_in, GFP_ATOMIC);
  49. if (ret == 0)
  50. set_bit(index, &line6pcm->in.active_urbs);
  51. else
  52. dev_err(line6pcm->line6->ifcdev,
  53. "URB in #%d submission failed (%d)\n", index, ret);
  54. return 0;
  55. }
  56. /*
  57. Submit all currently available capture URBs.
  58. must be called in line6pcm->in.lock context
  59. */
  60. int line6_submit_audio_in_all_urbs(struct snd_line6_pcm *line6pcm)
  61. {
  62. int ret = 0, i;
  63. for (i = 0; i < line6pcm->line6->iso_buffers; ++i) {
  64. ret = submit_audio_in_urb(line6pcm);
  65. if (ret < 0)
  66. break;
  67. }
  68. return ret;
  69. }
  70. /*
  71. Copy data into ALSA capture buffer.
  72. */
  73. void line6_capture_copy(struct snd_line6_pcm *line6pcm, char *fbuf, int fsize)
  74. {
  75. struct snd_pcm_substream *substream =
  76. get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
  77. struct snd_pcm_runtime *runtime = substream->runtime;
  78. const int bytes_per_frame =
  79. line6pcm->properties->bytes_per_channel *
  80. line6pcm->properties->capture_hw.channels_max;
  81. int frames = fsize / bytes_per_frame;
  82. if (runtime == NULL)
  83. return;
  84. if (line6pcm->in.pos_done + frames > runtime->buffer_size) {
  85. /*
  86. The transferred area goes over buffer boundary,
  87. copy two separate chunks.
  88. */
  89. int len;
  90. len = runtime->buffer_size - line6pcm->in.pos_done;
  91. if (len > 0) {
  92. memcpy(runtime->dma_area +
  93. line6pcm->in.pos_done * bytes_per_frame, fbuf,
  94. len * bytes_per_frame);
  95. memcpy(runtime->dma_area, fbuf + len * bytes_per_frame,
  96. (frames - len) * bytes_per_frame);
  97. } else {
  98. /* this is somewhat paranoid */
  99. dev_err(line6pcm->line6->ifcdev,
  100. "driver bug: len = %d\n", len);
  101. }
  102. } else {
  103. /* copy single chunk */
  104. memcpy(runtime->dma_area +
  105. line6pcm->in.pos_done * bytes_per_frame, fbuf, fsize);
  106. }
  107. line6pcm->in.pos_done += frames;
  108. if (line6pcm->in.pos_done >= runtime->buffer_size)
  109. line6pcm->in.pos_done -= runtime->buffer_size;
  110. }
  111. void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length)
  112. {
  113. struct snd_pcm_substream *substream =
  114. get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
  115. line6pcm->in.bytes += length;
  116. if (line6pcm->in.bytes >= line6pcm->in.period) {
  117. line6pcm->in.bytes %= line6pcm->in.period;
  118. spin_unlock(&line6pcm->in.lock);
  119. snd_pcm_period_elapsed(substream);
  120. spin_lock(&line6pcm->in.lock);
  121. }
  122. }
  123. /*
  124. * Callback for completed capture URB.
  125. */
  126. static void audio_in_callback(struct urb *urb)
  127. {
  128. int i, index, length = 0, shutdown = 0;
  129. unsigned long flags;
  130. struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
  131. line6pcm->in.last_frame = urb->start_frame;
  132. /* find index of URB */
  133. for (index = 0; index < line6pcm->line6->iso_buffers; ++index)
  134. if (urb == line6pcm->in.urbs[index])
  135. break;
  136. spin_lock_irqsave(&line6pcm->in.lock, flags);
  137. for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
  138. char *fbuf;
  139. int fsize;
  140. struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
  141. if (fin->status == -EXDEV) {
  142. shutdown = 1;
  143. break;
  144. }
  145. fbuf = urb->transfer_buffer + fin->offset;
  146. fsize = fin->actual_length;
  147. if (fsize > line6pcm->max_packet_size_in) {
  148. dev_err(line6pcm->line6->ifcdev,
  149. "driver and/or device bug: packet too large (%d > %d)\n",
  150. fsize, line6pcm->max_packet_size_in);
  151. }
  152. length += fsize;
  153. BUILD_BUG_ON_MSG(LINE6_ISO_PACKETS != 1,
  154. "The following code assumes LINE6_ISO_PACKETS == 1");
  155. /* TODO:
  156. * Also, if iso_buffers != 2, the prev frame is almost random at
  157. * playback side.
  158. * This needs to be redesigned. It should be "stable", but we may
  159. * experience sync problems on such high-speed configs.
  160. */
  161. line6pcm->prev_fbuf = fbuf;
  162. line6pcm->prev_fsize = fsize /
  163. (line6pcm->properties->bytes_per_channel *
  164. line6pcm->properties->capture_hw.channels_max);
  165. if (!test_bit(LINE6_STREAM_IMPULSE, &line6pcm->in.running) &&
  166. test_bit(LINE6_STREAM_PCM, &line6pcm->in.running) &&
  167. fsize > 0)
  168. line6_capture_copy(line6pcm, fbuf, fsize);
  169. }
  170. clear_bit(index, &line6pcm->in.active_urbs);
  171. if (test_and_clear_bit(index, &line6pcm->in.unlink_urbs))
  172. shutdown = 1;
  173. if (!shutdown) {
  174. submit_audio_in_urb(line6pcm);
  175. if (!test_bit(LINE6_STREAM_IMPULSE, &line6pcm->in.running) &&
  176. test_bit(LINE6_STREAM_PCM, &line6pcm->in.running))
  177. line6_capture_check_period(line6pcm, length);
  178. }
  179. spin_unlock_irqrestore(&line6pcm->in.lock, flags);
  180. }
  181. /* open capture callback */
  182. static int snd_line6_capture_open(struct snd_pcm_substream *substream)
  183. {
  184. int err;
  185. struct snd_pcm_runtime *runtime = substream->runtime;
  186. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  187. err = snd_pcm_hw_constraint_ratdens(runtime, 0,
  188. SNDRV_PCM_HW_PARAM_RATE,
  189. &line6pcm->properties->rates);
  190. if (err < 0)
  191. return err;
  192. line6_pcm_acquire(line6pcm, LINE6_STREAM_CAPTURE_HELPER, false);
  193. runtime->hw = line6pcm->properties->capture_hw;
  194. return 0;
  195. }
  196. /* close capture callback */
  197. static int snd_line6_capture_close(struct snd_pcm_substream *substream)
  198. {
  199. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  200. line6_pcm_release(line6pcm, LINE6_STREAM_CAPTURE_HELPER);
  201. return 0;
  202. }
  203. /* capture operators */
  204. struct snd_pcm_ops snd_line6_capture_ops = {
  205. .open = snd_line6_capture_open,
  206. .close = snd_line6_capture_close,
  207. .ioctl = snd_pcm_lib_ioctl,
  208. .hw_params = snd_line6_hw_params,
  209. .hw_free = snd_line6_hw_free,
  210. .prepare = snd_line6_prepare,
  211. .trigger = snd_line6_trigger,
  212. .pointer = snd_line6_pointer,
  213. };
  214. int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
  215. {
  216. struct usb_line6 *line6 = line6pcm->line6;
  217. int i;
  218. line6pcm->in.urbs = kzalloc(
  219. sizeof(struct urb *) * line6->iso_buffers, GFP_KERNEL);
  220. if (line6pcm->in.urbs == NULL)
  221. return -ENOMEM;
  222. /* create audio URBs and fill in constant values: */
  223. for (i = 0; i < line6->iso_buffers; ++i) {
  224. struct urb *urb;
  225. /* URB for audio in: */
  226. urb = line6pcm->in.urbs[i] =
  227. usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
  228. if (urb == NULL)
  229. return -ENOMEM;
  230. urb->dev = line6->usbdev;
  231. urb->pipe =
  232. usb_rcvisocpipe(line6->usbdev,
  233. line6->properties->ep_audio_r &
  234. USB_ENDPOINT_NUMBER_MASK);
  235. urb->transfer_flags = URB_ISO_ASAP;
  236. urb->start_frame = -1;
  237. urb->number_of_packets = LINE6_ISO_PACKETS;
  238. urb->interval = LINE6_ISO_INTERVAL;
  239. urb->error_count = 0;
  240. urb->complete = audio_in_callback;
  241. }
  242. return 0;
  243. }