usbtv-audio.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Copyright (c) 2013 Federico Simoncelli
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions, and the following disclaimer,
  10. * without modification.
  11. * 2. The name of the author may not be used to endorse or promote products
  12. * derived from this software without specific prior written permission.
  13. *
  14. * Alternatively, this software may be distributed under the terms of the
  15. * GNU General Public License ("GPL").
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*
  30. * Fushicai USBTV007 Audio-Video Grabber Driver
  31. *
  32. * Product web site:
  33. * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
  34. *
  35. * No physical hardware was harmed running Windows during the
  36. * reverse-engineering activity
  37. */
  38. #include <sound/core.h>
  39. #include <sound/initval.h>
  40. #include <sound/ac97_codec.h>
  41. #include <sound/pcm_params.h>
  42. #include "usbtv.h"
  43. static const struct snd_pcm_hardware snd_usbtv_digital_hw = {
  44. .info = SNDRV_PCM_INFO_BATCH |
  45. SNDRV_PCM_INFO_MMAP |
  46. SNDRV_PCM_INFO_INTERLEAVED |
  47. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  48. SNDRV_PCM_INFO_MMAP_VALID,
  49. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  50. .rates = SNDRV_PCM_RATE_48000,
  51. .rate_min = 48000,
  52. .rate_max = 48000,
  53. .channels_min = 2,
  54. .channels_max = 2,
  55. .period_bytes_min = 11059,
  56. .period_bytes_max = 13516,
  57. .periods_min = 2,
  58. .periods_max = 98,
  59. .buffer_bytes_max = 62720 * 8, /* value in usbaudio.c */
  60. };
  61. static int snd_usbtv_pcm_open(struct snd_pcm_substream *substream)
  62. {
  63. struct usbtv *chip = snd_pcm_substream_chip(substream);
  64. struct snd_pcm_runtime *runtime = substream->runtime;
  65. chip->snd_substream = substream;
  66. runtime->hw = snd_usbtv_digital_hw;
  67. return 0;
  68. }
  69. static int snd_usbtv_pcm_close(struct snd_pcm_substream *substream)
  70. {
  71. struct usbtv *chip = snd_pcm_substream_chip(substream);
  72. if (atomic_read(&chip->snd_stream)) {
  73. atomic_set(&chip->snd_stream, 0);
  74. schedule_work(&chip->snd_trigger);
  75. }
  76. return 0;
  77. }
  78. static int snd_usbtv_prepare(struct snd_pcm_substream *substream)
  79. {
  80. struct usbtv *chip = snd_pcm_substream_chip(substream);
  81. chip->snd_buffer_pos = 0;
  82. chip->snd_period_pos = 0;
  83. return 0;
  84. }
  85. static void usbtv_audio_urb_received(struct urb *urb)
  86. {
  87. struct usbtv *chip = urb->context;
  88. struct snd_pcm_substream *substream = chip->snd_substream;
  89. struct snd_pcm_runtime *runtime = substream->runtime;
  90. size_t i, frame_bytes, chunk_length, buffer_pos, period_pos;
  91. int period_elapsed;
  92. unsigned long flags;
  93. void *urb_current;
  94. switch (urb->status) {
  95. case 0:
  96. case -ETIMEDOUT:
  97. break;
  98. case -ENOENT:
  99. case -EPROTO:
  100. case -ECONNRESET:
  101. case -ESHUTDOWN:
  102. return;
  103. default:
  104. dev_warn(chip->dev, "unknown audio urb status %i\n",
  105. urb->status);
  106. }
  107. if (!atomic_read(&chip->snd_stream))
  108. return;
  109. frame_bytes = runtime->frame_bits >> 3;
  110. chunk_length = USBTV_CHUNK / frame_bytes;
  111. buffer_pos = chip->snd_buffer_pos;
  112. period_pos = chip->snd_period_pos;
  113. period_elapsed = 0;
  114. for (i = 0; i < urb->actual_length; i += USBTV_CHUNK_SIZE) {
  115. urb_current = urb->transfer_buffer + i + USBTV_AUDIO_HDRSIZE;
  116. if (buffer_pos + chunk_length >= runtime->buffer_size) {
  117. size_t cnt = (runtime->buffer_size - buffer_pos) *
  118. frame_bytes;
  119. memcpy(runtime->dma_area + buffer_pos * frame_bytes,
  120. urb_current, cnt);
  121. memcpy(runtime->dma_area, urb_current + cnt,
  122. chunk_length * frame_bytes - cnt);
  123. } else {
  124. memcpy(runtime->dma_area + buffer_pos * frame_bytes,
  125. urb_current, chunk_length * frame_bytes);
  126. }
  127. buffer_pos += chunk_length;
  128. period_pos += chunk_length;
  129. if (buffer_pos >= runtime->buffer_size)
  130. buffer_pos -= runtime->buffer_size;
  131. if (period_pos >= runtime->period_size) {
  132. period_pos -= runtime->period_size;
  133. period_elapsed = 1;
  134. }
  135. }
  136. snd_pcm_stream_lock_irqsave(substream, flags);
  137. chip->snd_buffer_pos = buffer_pos;
  138. chip->snd_period_pos = period_pos;
  139. snd_pcm_stream_unlock_irqrestore(substream, flags);
  140. if (period_elapsed)
  141. snd_pcm_period_elapsed(substream);
  142. usb_submit_urb(urb, GFP_ATOMIC);
  143. }
  144. static int usbtv_audio_start(struct usbtv *chip)
  145. {
  146. unsigned int pipe;
  147. static const u16 setup[][2] = {
  148. /* These seem to enable the device. */
  149. { USBTV_BASE + 0x0008, 0x0001 },
  150. { USBTV_BASE + 0x01d0, 0x00ff },
  151. { USBTV_BASE + 0x01d9, 0x0002 },
  152. { USBTV_BASE + 0x01da, 0x0013 },
  153. { USBTV_BASE + 0x01db, 0x0012 },
  154. { USBTV_BASE + 0x01e9, 0x0002 },
  155. { USBTV_BASE + 0x01ec, 0x006c },
  156. { USBTV_BASE + 0x0294, 0x0020 },
  157. { USBTV_BASE + 0x0255, 0x00cf },
  158. { USBTV_BASE + 0x0256, 0x0020 },
  159. { USBTV_BASE + 0x01eb, 0x0030 },
  160. { USBTV_BASE + 0x027d, 0x00a6 },
  161. { USBTV_BASE + 0x0280, 0x0011 },
  162. { USBTV_BASE + 0x0281, 0x0040 },
  163. { USBTV_BASE + 0x0282, 0x0011 },
  164. { USBTV_BASE + 0x0283, 0x0040 },
  165. { 0xf891, 0x0010 },
  166. /* this sets the input from composite */
  167. { USBTV_BASE + 0x0284, 0x00aa },
  168. };
  169. chip->snd_bulk_urb = usb_alloc_urb(0, GFP_KERNEL);
  170. if (chip->snd_bulk_urb == NULL)
  171. goto err_alloc_urb;
  172. pipe = usb_rcvbulkpipe(chip->udev, USBTV_AUDIO_ENDP);
  173. chip->snd_bulk_urb->transfer_buffer = kzalloc(
  174. USBTV_AUDIO_URBSIZE, GFP_KERNEL);
  175. if (chip->snd_bulk_urb->transfer_buffer == NULL)
  176. goto err_transfer_buffer;
  177. usb_fill_bulk_urb(chip->snd_bulk_urb, chip->udev, pipe,
  178. chip->snd_bulk_urb->transfer_buffer, USBTV_AUDIO_URBSIZE,
  179. usbtv_audio_urb_received, chip);
  180. /* starting the stream */
  181. usbtv_set_regs(chip, setup, ARRAY_SIZE(setup));
  182. usb_clear_halt(chip->udev, pipe);
  183. usb_submit_urb(chip->snd_bulk_urb, GFP_ATOMIC);
  184. return 0;
  185. err_transfer_buffer:
  186. usb_free_urb(chip->snd_bulk_urb);
  187. chip->snd_bulk_urb = NULL;
  188. err_alloc_urb:
  189. return -ENOMEM;
  190. }
  191. static int usbtv_audio_stop(struct usbtv *chip)
  192. {
  193. static const u16 setup[][2] = {
  194. /* The original windows driver sometimes sends also:
  195. * { USBTV_BASE + 0x00a2, 0x0013 }
  196. * but it seems useless and its real effects are untested at
  197. * the moment.
  198. */
  199. { USBTV_BASE + 0x027d, 0x0000 },
  200. { USBTV_BASE + 0x0280, 0x0010 },
  201. { USBTV_BASE + 0x0282, 0x0010 },
  202. };
  203. if (chip->snd_bulk_urb) {
  204. usb_kill_urb(chip->snd_bulk_urb);
  205. kfree(chip->snd_bulk_urb->transfer_buffer);
  206. usb_free_urb(chip->snd_bulk_urb);
  207. chip->snd_bulk_urb = NULL;
  208. }
  209. usbtv_set_regs(chip, setup, ARRAY_SIZE(setup));
  210. return 0;
  211. }
  212. void usbtv_audio_suspend(struct usbtv *usbtv)
  213. {
  214. if (atomic_read(&usbtv->snd_stream) && usbtv->snd_bulk_urb)
  215. usb_kill_urb(usbtv->snd_bulk_urb);
  216. }
  217. void usbtv_audio_resume(struct usbtv *usbtv)
  218. {
  219. if (atomic_read(&usbtv->snd_stream) && usbtv->snd_bulk_urb)
  220. usb_submit_urb(usbtv->snd_bulk_urb, GFP_ATOMIC);
  221. }
  222. static void snd_usbtv_trigger(struct work_struct *work)
  223. {
  224. struct usbtv *chip = container_of(work, struct usbtv, snd_trigger);
  225. if (!chip->snd)
  226. return;
  227. if (atomic_read(&chip->snd_stream))
  228. usbtv_audio_start(chip);
  229. else
  230. usbtv_audio_stop(chip);
  231. }
  232. static int snd_usbtv_card_trigger(struct snd_pcm_substream *substream, int cmd)
  233. {
  234. struct usbtv *chip = snd_pcm_substream_chip(substream);
  235. switch (cmd) {
  236. case SNDRV_PCM_TRIGGER_START:
  237. case SNDRV_PCM_TRIGGER_RESUME:
  238. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  239. atomic_set(&chip->snd_stream, 1);
  240. break;
  241. case SNDRV_PCM_TRIGGER_STOP:
  242. case SNDRV_PCM_TRIGGER_SUSPEND:
  243. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  244. atomic_set(&chip->snd_stream, 0);
  245. break;
  246. default:
  247. return -EINVAL;
  248. }
  249. schedule_work(&chip->snd_trigger);
  250. return 0;
  251. }
  252. static snd_pcm_uframes_t snd_usbtv_pointer(struct snd_pcm_substream *substream)
  253. {
  254. struct usbtv *chip = snd_pcm_substream_chip(substream);
  255. return chip->snd_buffer_pos;
  256. }
  257. static const struct snd_pcm_ops snd_usbtv_pcm_ops = {
  258. .open = snd_usbtv_pcm_open,
  259. .close = snd_usbtv_pcm_close,
  260. .prepare = snd_usbtv_prepare,
  261. .trigger = snd_usbtv_card_trigger,
  262. .pointer = snd_usbtv_pointer,
  263. };
  264. int usbtv_audio_init(struct usbtv *usbtv)
  265. {
  266. int rv;
  267. struct snd_card *card;
  268. struct snd_pcm *pcm;
  269. INIT_WORK(&usbtv->snd_trigger, snd_usbtv_trigger);
  270. atomic_set(&usbtv->snd_stream, 0);
  271. rv = snd_card_new(&usbtv->udev->dev, SNDRV_DEFAULT_IDX1, "usbtv",
  272. THIS_MODULE, 0, &card);
  273. if (rv < 0)
  274. return rv;
  275. strscpy(card->driver, usbtv->dev->driver->name, sizeof(card->driver));
  276. strscpy(card->shortname, "usbtv", sizeof(card->shortname));
  277. snprintf(card->longname, sizeof(card->longname),
  278. "USBTV Audio at bus %d device %d", usbtv->udev->bus->busnum,
  279. usbtv->udev->devnum);
  280. snd_card_set_dev(card, usbtv->dev);
  281. usbtv->snd = card;
  282. rv = snd_pcm_new(card, "USBTV Audio", 0, 0, 1, &pcm);
  283. if (rv < 0)
  284. goto err;
  285. strscpy(pcm->name, "USBTV Audio Input", sizeof(pcm->name));
  286. pcm->info_flags = 0;
  287. pcm->private_data = usbtv;
  288. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usbtv_pcm_ops);
  289. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  290. NULL, USBTV_AUDIO_BUFFER, USBTV_AUDIO_BUFFER);
  291. rv = snd_card_register(card);
  292. if (rv)
  293. goto err;
  294. return 0;
  295. err:
  296. usbtv->snd = NULL;
  297. snd_card_free(card);
  298. return rv;
  299. }
  300. void usbtv_audio_free(struct usbtv *usbtv)
  301. {
  302. cancel_work_sync(&usbtv->snd_trigger);
  303. if (usbtv->snd && usbtv->udev) {
  304. snd_card_free_when_closed(usbtv->snd);
  305. usbtv->snd = NULL;
  306. }
  307. }