usbtv-audio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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_hw_params(struct snd_pcm_substream *substream,
  79. struct snd_pcm_hw_params *hw_params)
  80. {
  81. int rv;
  82. struct usbtv *chip = snd_pcm_substream_chip(substream);
  83. rv = snd_pcm_lib_malloc_pages(substream,
  84. params_buffer_bytes(hw_params));
  85. if (rv < 0) {
  86. dev_warn(chip->dev, "pcm audio buffer allocation failure %i\n",
  87. rv);
  88. return rv;
  89. }
  90. return 0;
  91. }
  92. static int snd_usbtv_hw_free(struct snd_pcm_substream *substream)
  93. {
  94. snd_pcm_lib_free_pages(substream);
  95. return 0;
  96. }
  97. static int snd_usbtv_prepare(struct snd_pcm_substream *substream)
  98. {
  99. struct usbtv *chip = snd_pcm_substream_chip(substream);
  100. chip->snd_buffer_pos = 0;
  101. chip->snd_period_pos = 0;
  102. return 0;
  103. }
  104. static void usbtv_audio_urb_received(struct urb *urb)
  105. {
  106. struct usbtv *chip = urb->context;
  107. struct snd_pcm_substream *substream = chip->snd_substream;
  108. struct snd_pcm_runtime *runtime = substream->runtime;
  109. size_t i, frame_bytes, chunk_length, buffer_pos, period_pos;
  110. int period_elapsed;
  111. unsigned long flags;
  112. void *urb_current;
  113. switch (urb->status) {
  114. case 0:
  115. case -ETIMEDOUT:
  116. break;
  117. case -ENOENT:
  118. case -EPROTO:
  119. case -ECONNRESET:
  120. case -ESHUTDOWN:
  121. return;
  122. default:
  123. dev_warn(chip->dev, "unknown audio urb status %i\n",
  124. urb->status);
  125. }
  126. if (!atomic_read(&chip->snd_stream))
  127. return;
  128. frame_bytes = runtime->frame_bits >> 3;
  129. chunk_length = USBTV_CHUNK / frame_bytes;
  130. buffer_pos = chip->snd_buffer_pos;
  131. period_pos = chip->snd_period_pos;
  132. period_elapsed = 0;
  133. for (i = 0; i < urb->actual_length; i += USBTV_CHUNK_SIZE) {
  134. urb_current = urb->transfer_buffer + i + USBTV_AUDIO_HDRSIZE;
  135. if (buffer_pos + chunk_length >= runtime->buffer_size) {
  136. size_t cnt = (runtime->buffer_size - buffer_pos) *
  137. frame_bytes;
  138. memcpy(runtime->dma_area + buffer_pos * frame_bytes,
  139. urb_current, cnt);
  140. memcpy(runtime->dma_area, urb_current + cnt,
  141. chunk_length * frame_bytes - cnt);
  142. } else {
  143. memcpy(runtime->dma_area + buffer_pos * frame_bytes,
  144. urb_current, chunk_length * frame_bytes);
  145. }
  146. buffer_pos += chunk_length;
  147. period_pos += chunk_length;
  148. if (buffer_pos >= runtime->buffer_size)
  149. buffer_pos -= runtime->buffer_size;
  150. if (period_pos >= runtime->period_size) {
  151. period_pos -= runtime->period_size;
  152. period_elapsed = 1;
  153. }
  154. }
  155. snd_pcm_stream_lock_irqsave(substream, flags);
  156. chip->snd_buffer_pos = buffer_pos;
  157. chip->snd_period_pos = period_pos;
  158. snd_pcm_stream_unlock_irqrestore(substream, flags);
  159. if (period_elapsed)
  160. snd_pcm_period_elapsed(substream);
  161. usb_submit_urb(urb, GFP_ATOMIC);
  162. }
  163. static int usbtv_audio_start(struct usbtv *chip)
  164. {
  165. unsigned int pipe;
  166. static const u16 setup[][2] = {
  167. /* These seem to enable the device. */
  168. { USBTV_BASE + 0x0008, 0x0001 },
  169. { USBTV_BASE + 0x01d0, 0x00ff },
  170. { USBTV_BASE + 0x01d9, 0x0002 },
  171. { USBTV_BASE + 0x01da, 0x0013 },
  172. { USBTV_BASE + 0x01db, 0x0012 },
  173. { USBTV_BASE + 0x01e9, 0x0002 },
  174. { USBTV_BASE + 0x01ec, 0x006c },
  175. { USBTV_BASE + 0x0294, 0x0020 },
  176. { USBTV_BASE + 0x0255, 0x00cf },
  177. { USBTV_BASE + 0x0256, 0x0020 },
  178. { USBTV_BASE + 0x01eb, 0x0030 },
  179. { USBTV_BASE + 0x027d, 0x00a6 },
  180. { USBTV_BASE + 0x0280, 0x0011 },
  181. { USBTV_BASE + 0x0281, 0x0040 },
  182. { USBTV_BASE + 0x0282, 0x0011 },
  183. { USBTV_BASE + 0x0283, 0x0040 },
  184. { 0xf891, 0x0010 },
  185. /* this sets the input from composite */
  186. { USBTV_BASE + 0x0284, 0x00aa },
  187. };
  188. chip->snd_bulk_urb = usb_alloc_urb(0, GFP_KERNEL);
  189. if (chip->snd_bulk_urb == NULL)
  190. goto err_alloc_urb;
  191. pipe = usb_rcvbulkpipe(chip->udev, USBTV_AUDIO_ENDP);
  192. chip->snd_bulk_urb->transfer_buffer = kzalloc(
  193. USBTV_AUDIO_URBSIZE, GFP_KERNEL);
  194. if (chip->snd_bulk_urb->transfer_buffer == NULL)
  195. goto err_transfer_buffer;
  196. usb_fill_bulk_urb(chip->snd_bulk_urb, chip->udev, pipe,
  197. chip->snd_bulk_urb->transfer_buffer, USBTV_AUDIO_URBSIZE,
  198. usbtv_audio_urb_received, chip);
  199. /* starting the stream */
  200. usbtv_set_regs(chip, setup, ARRAY_SIZE(setup));
  201. usb_clear_halt(chip->udev, pipe);
  202. usb_submit_urb(chip->snd_bulk_urb, GFP_ATOMIC);
  203. return 0;
  204. err_transfer_buffer:
  205. usb_free_urb(chip->snd_bulk_urb);
  206. chip->snd_bulk_urb = NULL;
  207. err_alloc_urb:
  208. return -ENOMEM;
  209. }
  210. static int usbtv_audio_stop(struct usbtv *chip)
  211. {
  212. static const u16 setup[][2] = {
  213. /* The original windows driver sometimes sends also:
  214. * { USBTV_BASE + 0x00a2, 0x0013 }
  215. * but it seems useless and its real effects are untested at
  216. * the moment.
  217. */
  218. { USBTV_BASE + 0x027d, 0x0000 },
  219. { USBTV_BASE + 0x0280, 0x0010 },
  220. { USBTV_BASE + 0x0282, 0x0010 },
  221. };
  222. if (chip->snd_bulk_urb) {
  223. usb_kill_urb(chip->snd_bulk_urb);
  224. kfree(chip->snd_bulk_urb->transfer_buffer);
  225. usb_free_urb(chip->snd_bulk_urb);
  226. chip->snd_bulk_urb = NULL;
  227. }
  228. usbtv_set_regs(chip, setup, ARRAY_SIZE(setup));
  229. return 0;
  230. }
  231. void usbtv_audio_suspend(struct usbtv *usbtv)
  232. {
  233. if (atomic_read(&usbtv->snd_stream) && usbtv->snd_bulk_urb)
  234. usb_kill_urb(usbtv->snd_bulk_urb);
  235. }
  236. void usbtv_audio_resume(struct usbtv *usbtv)
  237. {
  238. if (atomic_read(&usbtv->snd_stream) && usbtv->snd_bulk_urb)
  239. usb_submit_urb(usbtv->snd_bulk_urb, GFP_ATOMIC);
  240. }
  241. static void snd_usbtv_trigger(struct work_struct *work)
  242. {
  243. struct usbtv *chip = container_of(work, struct usbtv, snd_trigger);
  244. if (!chip->snd)
  245. return;
  246. if (atomic_read(&chip->snd_stream))
  247. usbtv_audio_start(chip);
  248. else
  249. usbtv_audio_stop(chip);
  250. }
  251. static int snd_usbtv_card_trigger(struct snd_pcm_substream *substream, int cmd)
  252. {
  253. struct usbtv *chip = snd_pcm_substream_chip(substream);
  254. switch (cmd) {
  255. case SNDRV_PCM_TRIGGER_START:
  256. case SNDRV_PCM_TRIGGER_RESUME:
  257. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  258. atomic_set(&chip->snd_stream, 1);
  259. break;
  260. case SNDRV_PCM_TRIGGER_STOP:
  261. case SNDRV_PCM_TRIGGER_SUSPEND:
  262. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  263. atomic_set(&chip->snd_stream, 0);
  264. break;
  265. default:
  266. return -EINVAL;
  267. }
  268. schedule_work(&chip->snd_trigger);
  269. return 0;
  270. }
  271. static snd_pcm_uframes_t snd_usbtv_pointer(struct snd_pcm_substream *substream)
  272. {
  273. struct usbtv *chip = snd_pcm_substream_chip(substream);
  274. return chip->snd_buffer_pos;
  275. }
  276. static const struct snd_pcm_ops snd_usbtv_pcm_ops = {
  277. .open = snd_usbtv_pcm_open,
  278. .close = snd_usbtv_pcm_close,
  279. .ioctl = snd_pcm_lib_ioctl,
  280. .hw_params = snd_usbtv_hw_params,
  281. .hw_free = snd_usbtv_hw_free,
  282. .prepare = snd_usbtv_prepare,
  283. .trigger = snd_usbtv_card_trigger,
  284. .pointer = snd_usbtv_pointer,
  285. };
  286. int usbtv_audio_init(struct usbtv *usbtv)
  287. {
  288. int rv;
  289. struct snd_card *card;
  290. struct snd_pcm *pcm;
  291. INIT_WORK(&usbtv->snd_trigger, snd_usbtv_trigger);
  292. atomic_set(&usbtv->snd_stream, 0);
  293. rv = snd_card_new(&usbtv->udev->dev, SNDRV_DEFAULT_IDX1, "usbtv",
  294. THIS_MODULE, 0, &card);
  295. if (rv < 0)
  296. return rv;
  297. strlcpy(card->driver, usbtv->dev->driver->name, sizeof(card->driver));
  298. strlcpy(card->shortname, "usbtv", sizeof(card->shortname));
  299. snprintf(card->longname, sizeof(card->longname),
  300. "USBTV Audio at bus %d device %d", usbtv->udev->bus->busnum,
  301. usbtv->udev->devnum);
  302. snd_card_set_dev(card, usbtv->dev);
  303. usbtv->snd = card;
  304. rv = snd_pcm_new(card, "USBTV Audio", 0, 0, 1, &pcm);
  305. if (rv < 0)
  306. goto err;
  307. strlcpy(pcm->name, "USBTV Audio Input", sizeof(pcm->name));
  308. pcm->info_flags = 0;
  309. pcm->private_data = usbtv;
  310. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usbtv_pcm_ops);
  311. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  312. snd_dma_continuous_data(GFP_KERNEL), USBTV_AUDIO_BUFFER,
  313. USBTV_AUDIO_BUFFER);
  314. rv = snd_card_register(card);
  315. if (rv)
  316. goto err;
  317. return 0;
  318. err:
  319. usbtv->snd = NULL;
  320. snd_card_free(card);
  321. return rv;
  322. }
  323. void usbtv_audio_free(struct usbtv *usbtv)
  324. {
  325. cancel_work_sync(&usbtv->snd_trigger);
  326. if (usbtv->snd && usbtv->udev) {
  327. snd_card_free(usbtv->snd);
  328. usbtv->snd = NULL;
  329. }
  330. }