capture.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 =
  29. find_first_zero_bit(&line6pcm->in.active_urbs, LINE6_ISO_BUFFERS);
  30. if (index < 0 || index >= 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;
  41. urb_size += line6pcm->max_packet_size;
  42. }
  43. urb_in->transfer_buffer =
  44. line6pcm->in.buffer +
  45. index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
  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 < 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 = line6pcm->properties->bytes_per_frame;
  79. int frames = fsize / bytes_per_frame;
  80. if (runtime == NULL)
  81. return;
  82. if (line6pcm->in.pos_done + frames > runtime->buffer_size) {
  83. /*
  84. The transferred area goes over buffer boundary,
  85. copy two separate chunks.
  86. */
  87. int len;
  88. len = runtime->buffer_size - line6pcm->in.pos_done;
  89. if (len > 0) {
  90. memcpy(runtime->dma_area +
  91. line6pcm->in.pos_done * bytes_per_frame, fbuf,
  92. len * bytes_per_frame);
  93. memcpy(runtime->dma_area, fbuf + len * bytes_per_frame,
  94. (frames - len) * bytes_per_frame);
  95. } else {
  96. /* this is somewhat paranoid */
  97. dev_err(line6pcm->line6->ifcdev,
  98. "driver bug: len = %d\n", len);
  99. }
  100. } else {
  101. /* copy single chunk */
  102. memcpy(runtime->dma_area +
  103. line6pcm->in.pos_done * bytes_per_frame, fbuf, fsize);
  104. }
  105. line6pcm->in.pos_done += frames;
  106. if (line6pcm->in.pos_done >= runtime->buffer_size)
  107. line6pcm->in.pos_done -= runtime->buffer_size;
  108. }
  109. void line6_capture_check_period(struct snd_line6_pcm *line6pcm, int length)
  110. {
  111. struct snd_pcm_substream *substream =
  112. get_substream(line6pcm, SNDRV_PCM_STREAM_CAPTURE);
  113. line6pcm->in.bytes += length;
  114. if (line6pcm->in.bytes >= line6pcm->in.period) {
  115. line6pcm->in.bytes %= line6pcm->in.period;
  116. spin_unlock(&line6pcm->in.lock);
  117. snd_pcm_period_elapsed(substream);
  118. spin_lock(&line6pcm->in.lock);
  119. }
  120. }
  121. /*
  122. * Callback for completed capture URB.
  123. */
  124. static void audio_in_callback(struct urb *urb)
  125. {
  126. int i, index, length = 0, shutdown = 0;
  127. unsigned long flags;
  128. struct snd_line6_pcm *line6pcm = (struct snd_line6_pcm *)urb->context;
  129. line6pcm->in.last_frame = urb->start_frame;
  130. /* find index of URB */
  131. for (index = 0; index < LINE6_ISO_BUFFERS; ++index)
  132. if (urb == line6pcm->in.urbs[index])
  133. break;
  134. spin_lock_irqsave(&line6pcm->in.lock, flags);
  135. for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
  136. char *fbuf;
  137. int fsize;
  138. struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
  139. if (fin->status == -EXDEV) {
  140. shutdown = 1;
  141. break;
  142. }
  143. fbuf = urb->transfer_buffer + fin->offset;
  144. fsize = fin->actual_length;
  145. if (fsize > line6pcm->max_packet_size) {
  146. dev_err(line6pcm->line6->ifcdev,
  147. "driver and/or device bug: packet too large (%d > %d)\n",
  148. fsize, line6pcm->max_packet_size);
  149. }
  150. length += fsize;
  151. /* the following assumes LINE6_ISO_PACKETS == 1: */
  152. line6pcm->prev_fbuf = fbuf;
  153. line6pcm->prev_fsize = fsize;
  154. if (!test_bit(LINE6_STREAM_IMPULSE, &line6pcm->in.running) &&
  155. test_bit(LINE6_STREAM_PCM, &line6pcm->in.running) &&
  156. fsize > 0)
  157. line6_capture_copy(line6pcm, fbuf, fsize);
  158. }
  159. clear_bit(index, &line6pcm->in.active_urbs);
  160. if (test_and_clear_bit(index, &line6pcm->in.unlink_urbs))
  161. shutdown = 1;
  162. if (!shutdown) {
  163. submit_audio_in_urb(line6pcm);
  164. if (!test_bit(LINE6_STREAM_IMPULSE, &line6pcm->in.running) &&
  165. test_bit(LINE6_STREAM_PCM, &line6pcm->in.running))
  166. line6_capture_check_period(line6pcm, length);
  167. }
  168. spin_unlock_irqrestore(&line6pcm->in.lock, flags);
  169. }
  170. /* open capture callback */
  171. static int snd_line6_capture_open(struct snd_pcm_substream *substream)
  172. {
  173. int err;
  174. struct snd_pcm_runtime *runtime = substream->runtime;
  175. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  176. err = snd_pcm_hw_constraint_ratdens(runtime, 0,
  177. SNDRV_PCM_HW_PARAM_RATE,
  178. &line6pcm->properties->rates);
  179. if (err < 0)
  180. return err;
  181. runtime->hw = line6pcm->properties->capture_hw;
  182. return 0;
  183. }
  184. /* close capture callback */
  185. static int snd_line6_capture_close(struct snd_pcm_substream *substream)
  186. {
  187. return 0;
  188. }
  189. /* capture operators */
  190. struct snd_pcm_ops snd_line6_capture_ops = {
  191. .open = snd_line6_capture_open,
  192. .close = snd_line6_capture_close,
  193. .ioctl = snd_pcm_lib_ioctl,
  194. .hw_params = snd_line6_hw_params,
  195. .hw_free = snd_line6_hw_free,
  196. .prepare = snd_line6_prepare,
  197. .trigger = snd_line6_trigger,
  198. .pointer = snd_line6_pointer,
  199. };
  200. int line6_create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
  201. {
  202. struct usb_line6 *line6 = line6pcm->line6;
  203. int i;
  204. /* create audio URBs and fill in constant values: */
  205. for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
  206. struct urb *urb;
  207. /* URB for audio in: */
  208. urb = line6pcm->in.urbs[i] =
  209. usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
  210. if (urb == NULL)
  211. return -ENOMEM;
  212. urb->dev = line6->usbdev;
  213. urb->pipe =
  214. usb_rcvisocpipe(line6->usbdev,
  215. line6->properties->ep_audio_r &
  216. USB_ENDPOINT_NUMBER_MASK);
  217. urb->transfer_flags = URB_ISO_ASAP;
  218. urb->start_frame = -1;
  219. urb->number_of_packets = LINE6_ISO_PACKETS;
  220. urb->interval = LINE6_ISO_INTERVAL;
  221. urb->error_count = 0;
  222. urb->complete = audio_in_callback;
  223. }
  224. return 0;
  225. }