usbtv-audio.c 10 KB

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