capture.c 7.4 KB

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