PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/staging/line6/capture.c

https://bitbucket.org/cyanogenmod/htc-kernel-incrediblec
C | 372 lines | 258 code | 67 blank | 47 comment | 45 complexity | 4f7a1019c2659e2f6d409ec43a371024 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Line6 Linux USB driver - 0.8.0
  3. *
  4. * Copyright (C) 2004-2009 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 "driver.h"
  12. #include <sound/core.h>
  13. #include <sound/pcm.h>
  14. #include <sound/pcm_params.h>
  15. #include "audio.h"
  16. #include "pcm.h"
  17. #include "pod.h"
  18. #include "capture.h"
  19. /*
  20. Find a free URB and submit it.
  21. */
  22. static int submit_audio_in_urb(struct snd_pcm_substream *substream)
  23. {
  24. unsigned int index;
  25. unsigned long flags;
  26. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  27. int i, urb_size;
  28. struct urb *urb_in;
  29. spin_lock_irqsave(&line6pcm->lock_audio_in, flags);
  30. index = find_first_zero_bit(&line6pcm->active_urb_in, LINE6_ISO_BUFFERS);
  31. if (index >= LINE6_ISO_BUFFERS) {
  32. spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
  33. dev_err(s2m(substream), "no free URB found\n");
  34. return -EINVAL;
  35. }
  36. urb_in = line6pcm->urb_audio_in[index];
  37. urb_size = 0;
  38. for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
  39. struct usb_iso_packet_descriptor *fin = &urb_in->iso_frame_desc[i];
  40. fin->offset = urb_size;
  41. fin->length = line6pcm->max_packet_size;
  42. urb_size += line6pcm->max_packet_size;
  43. }
  44. urb_in->transfer_buffer = line6pcm->buffer_in + index * LINE6_ISO_PACKETS * line6pcm->max_packet_size;
  45. urb_in->transfer_buffer_length = urb_size;
  46. urb_in->context = substream;
  47. if (usb_submit_urb(urb_in, GFP_ATOMIC) == 0)
  48. set_bit(index, &line6pcm->active_urb_in);
  49. else
  50. dev_err(s2m(substream), "URB in #%d submission failed\n", index);
  51. spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
  52. return 0;
  53. }
  54. /*
  55. Submit all currently available capture URBs.
  56. */
  57. static int submit_audio_in_all_urbs(struct snd_pcm_substream *substream)
  58. {
  59. int ret, i;
  60. for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
  61. ret = submit_audio_in_urb(substream);
  62. if (ret < 0)
  63. return ret;
  64. }
  65. return 0;
  66. }
  67. /*
  68. Unlink all currently active capture URBs.
  69. */
  70. static void unlink_audio_in_urbs(struct snd_line6_pcm *line6pcm)
  71. {
  72. unsigned int i;
  73. for (i = LINE6_ISO_BUFFERS; i--;) {
  74. if (test_bit(i, &line6pcm->active_urb_in)) {
  75. if (!test_and_set_bit(i, &line6pcm->unlink_urb_in)) {
  76. struct urb *u = line6pcm->urb_audio_in[i];
  77. usb_unlink_urb(u);
  78. }
  79. }
  80. }
  81. }
  82. /*
  83. Wait until unlinking of all currently active capture URBs has been
  84. finished.
  85. */
  86. static void wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
  87. {
  88. int timeout = HZ;
  89. unsigned int i;
  90. int alive;
  91. do {
  92. alive = 0;
  93. for (i = LINE6_ISO_BUFFERS; i--;) {
  94. if (test_bit(i, &line6pcm->active_urb_in))
  95. alive++;
  96. }
  97. if (!alive)
  98. break;
  99. set_current_state(TASK_UNINTERRUPTIBLE);
  100. schedule_timeout(1);
  101. } while (--timeout > 0);
  102. if (alive)
  103. snd_printk(KERN_ERR "timeout: still %d active urbs..\n", alive);
  104. line6pcm->active_urb_in = 0;
  105. line6pcm->unlink_urb_in = 0;
  106. }
  107. /*
  108. Unlink all currently active capture URBs, and wait for finishing.
  109. */
  110. void unlink_wait_clear_audio_in_urbs(struct snd_line6_pcm *line6pcm)
  111. {
  112. unlink_audio_in_urbs(line6pcm);
  113. wait_clear_audio_in_urbs(line6pcm);
  114. }
  115. /*
  116. Callback for completed capture URB.
  117. */
  118. static void audio_in_callback(struct urb *urb)
  119. {
  120. int i, index, length = 0, shutdown = 0;
  121. int frames;
  122. unsigned long flags;
  123. struct snd_pcm_substream *substream = (struct snd_pcm_substream *)urb->context;
  124. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  125. const int bytes_per_frame = line6pcm->properties->bytes_per_frame;
  126. struct snd_pcm_runtime *runtime = substream->runtime;
  127. /* find index of URB */
  128. for (index = 0; index < LINE6_ISO_BUFFERS; ++index)
  129. if (urb == line6pcm->urb_audio_in[index])
  130. break;
  131. #if DO_DUMP_PCM_RECEIVE
  132. for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
  133. struct usb_iso_packet_descriptor *fout = &urb->iso_frame_desc[i];
  134. line6_write_hexdump(line6pcm->line6, 'C', urb->transfer_buffer + fout->offset, fout->length);
  135. }
  136. #endif
  137. spin_lock_irqsave(&line6pcm->lock_audio_in, flags);
  138. for (i = 0; i < LINE6_ISO_PACKETS; ++i) {
  139. char *fbuf;
  140. int fsize;
  141. struct usb_iso_packet_descriptor *fin = &urb->iso_frame_desc[i];
  142. if (fin->status == -18) {
  143. shutdown = 1;
  144. break;
  145. }
  146. fbuf = urb->transfer_buffer + fin->offset;
  147. fsize = fin->actual_length;
  148. length += fsize;
  149. if (fsize > 0) {
  150. frames = fsize / bytes_per_frame;
  151. if (line6pcm->pos_in_done + frames > runtime->buffer_size) {
  152. /*
  153. The transferred area goes over buffer boundary,
  154. copy two separate chunks.
  155. */
  156. int len;
  157. len = runtime->buffer_size - line6pcm->pos_in_done;
  158. if (len > 0) {
  159. memcpy(runtime->dma_area + line6pcm->pos_in_done * bytes_per_frame, fbuf, len * bytes_per_frame);
  160. memcpy(runtime->dma_area, fbuf + len * bytes_per_frame, (frames - len) * bytes_per_frame);
  161. } else
  162. dev_err(s2m(substream), "driver bug: len = %d\n", len); /* this is somewhat paranoid */
  163. } else {
  164. /* copy single chunk */
  165. memcpy(runtime->dma_area + line6pcm->pos_in_done * bytes_per_frame, fbuf, fsize * bytes_per_frame);
  166. }
  167. if ((line6pcm->pos_in_done += frames) >= runtime->buffer_size)
  168. line6pcm->pos_in_done -= runtime->buffer_size;
  169. }
  170. }
  171. clear_bit(index, &line6pcm->active_urb_in);
  172. if (test_bit(index, &line6pcm->unlink_urb_in))
  173. shutdown = 1;
  174. spin_unlock_irqrestore(&line6pcm->lock_audio_in, flags);
  175. if (!shutdown) {
  176. submit_audio_in_urb(substream);
  177. if ((line6pcm->bytes_in += length) >= line6pcm->period_in) {
  178. line6pcm->bytes_in -= line6pcm->period_in;
  179. snd_pcm_period_elapsed(substream);
  180. }
  181. }
  182. }
  183. /* open capture callback */
  184. static int snd_line6_capture_open(struct snd_pcm_substream *substream)
  185. {
  186. int err;
  187. struct snd_pcm_runtime *runtime = substream->runtime;
  188. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  189. err = snd_pcm_hw_constraint_ratdens(runtime, 0,
  190. SNDRV_PCM_HW_PARAM_RATE,
  191. (&line6pcm->properties->snd_line6_rates));
  192. if (err < 0)
  193. return err;
  194. runtime->hw = line6pcm->properties->snd_line6_capture_hw;
  195. return 0;
  196. }
  197. /* close capture callback */
  198. static int snd_line6_capture_close(struct snd_pcm_substream *substream)
  199. {
  200. return 0;
  201. }
  202. /* hw_params capture callback */
  203. static int snd_line6_capture_hw_params(struct snd_pcm_substream *substream,
  204. struct snd_pcm_hw_params *hw_params)
  205. {
  206. int ret;
  207. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  208. /* -- Florian Demski [FD] */
  209. /* don't ask me why, but this fixes the bug on my machine */
  210. if (line6pcm == NULL) {
  211. if (substream->pcm == NULL)
  212. return -ENOMEM;
  213. if (substream->pcm->private_data == NULL)
  214. return -ENOMEM;
  215. substream->private_data = substream->pcm->private_data;
  216. line6pcm = snd_pcm_substream_chip(substream);
  217. }
  218. /* -- [FD] end */
  219. ret = snd_pcm_lib_malloc_pages(substream,
  220. params_buffer_bytes(hw_params));
  221. if (ret < 0)
  222. return ret;
  223. line6pcm->period_in = params_period_bytes(hw_params);
  224. line6pcm->buffer_in = kmalloc(LINE6_ISO_BUFFERS * LINE6_ISO_PACKETS * LINE6_ISO_PACKET_SIZE_MAX, GFP_KERNEL);
  225. if (!line6pcm->buffer_in) {
  226. dev_err(s2m(substream), "cannot malloc buffer_in\n");
  227. return -ENOMEM;
  228. }
  229. return 0;
  230. }
  231. /* hw_free capture callback */
  232. static int snd_line6_capture_hw_free(struct snd_pcm_substream *substream)
  233. {
  234. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  235. unlink_wait_clear_audio_in_urbs(line6pcm);
  236. kfree(line6pcm->buffer_in);
  237. line6pcm->buffer_in = NULL;
  238. return snd_pcm_lib_free_pages(substream);
  239. }
  240. /* trigger callback */
  241. int snd_line6_capture_trigger(struct snd_pcm_substream *substream, int cmd)
  242. {
  243. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  244. int err;
  245. line6pcm->count_in = 0;
  246. switch (cmd) {
  247. case SNDRV_PCM_TRIGGER_START:
  248. if (!test_and_set_bit(BIT_RUNNING_CAPTURE, &line6pcm->flags)) {
  249. err = submit_audio_in_all_urbs(substream);
  250. if (err < 0) {
  251. clear_bit(BIT_RUNNING_CAPTURE, &line6pcm->flags);
  252. return err;
  253. }
  254. }
  255. break;
  256. case SNDRV_PCM_TRIGGER_STOP:
  257. if (test_and_clear_bit(BIT_RUNNING_CAPTURE, &line6pcm->flags))
  258. unlink_audio_in_urbs(line6pcm);
  259. break;
  260. default:
  261. return -EINVAL;
  262. }
  263. return 0;
  264. }
  265. /* capture pointer callback */
  266. static snd_pcm_uframes_t
  267. snd_line6_capture_pointer(struct snd_pcm_substream *substream)
  268. {
  269. struct snd_line6_pcm *line6pcm = snd_pcm_substream_chip(substream);
  270. return line6pcm->pos_in_done;
  271. }
  272. /* capture operators */
  273. struct snd_pcm_ops snd_line6_capture_ops = {
  274. .open = snd_line6_capture_open,
  275. .close = snd_line6_capture_close,
  276. .ioctl = snd_pcm_lib_ioctl,
  277. .hw_params = snd_line6_capture_hw_params,
  278. .hw_free = snd_line6_capture_hw_free,
  279. .prepare = snd_line6_prepare,
  280. .trigger = snd_line6_trigger,
  281. .pointer = snd_line6_capture_pointer,
  282. };
  283. int create_audio_in_urbs(struct snd_line6_pcm *line6pcm)
  284. {
  285. int i;
  286. /* create audio URBs and fill in constant values: */
  287. for (i = 0; i < LINE6_ISO_BUFFERS; ++i) {
  288. struct urb *urb;
  289. /* URB for audio in: */
  290. urb = line6pcm->urb_audio_in[i] = usb_alloc_urb(LINE6_ISO_PACKETS, GFP_KERNEL);
  291. if (urb == NULL) {
  292. dev_err(line6pcm->line6->ifcdev, "Out of memory\n");
  293. return -ENOMEM;
  294. }
  295. urb->dev = line6pcm->line6->usbdev;
  296. urb->pipe = usb_rcvisocpipe(line6pcm->line6->usbdev, line6pcm->ep_audio_read & USB_ENDPOINT_NUMBER_MASK);
  297. urb->transfer_flags = URB_ISO_ASAP;
  298. urb->start_frame = -1;
  299. urb->number_of_packets = LINE6_ISO_PACKETS;
  300. urb->interval = LINE6_ISO_INTERVAL;
  301. urb->error_count = 0;
  302. urb->complete = audio_in_callback;
  303. }
  304. return 0;
  305. }