usbtv-audio.c 9.5 KB

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