tm6000-alsa.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. *
  3. * Support for audio capture for tm5600/6000/6010
  4. * (c) 2007-2008 Mauro Carvalho Chehab
  5. *
  6. * Based on cx88-alsa.c
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/device.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/usb.h>
  17. #include <linux/slab.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/delay.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/control.h>
  24. #include <sound/initval.h>
  25. #include "tm6000.h"
  26. #include "tm6000-regs.h"
  27. #undef dprintk
  28. #define dprintk(level, fmt, arg...) do { \
  29. if (debug >= level) \
  30. printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg); \
  31. } while (0)
  32. /****************************************************************************
  33. Module global static vars
  34. ****************************************************************************/
  35. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  36. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  37. module_param_array(enable, bool, NULL, 0444);
  38. MODULE_PARM_DESC(enable, "Enable tm6000x soundcard. default enabled.");
  39. module_param_array(index, int, NULL, 0444);
  40. MODULE_PARM_DESC(index, "Index value for tm6000x capture interface(s).");
  41. /****************************************************************************
  42. Module macros
  43. ****************************************************************************/
  44. MODULE_DESCRIPTION("ALSA driver module for tm5600/tm6000/tm6010 based TV cards");
  45. MODULE_AUTHOR("Mauro Carvalho Chehab");
  46. MODULE_LICENSE("GPL");
  47. MODULE_SUPPORTED_DEVICE("{{Trident,tm5600},{{Trident,tm6000},{{Trident,tm6010}");
  48. static unsigned int debug;
  49. module_param(debug, int, 0644);
  50. MODULE_PARM_DESC(debug, "enable debug messages");
  51. /****************************************************************************
  52. Module specific funtions
  53. ****************************************************************************/
  54. /*
  55. * BOARD Specific: Sets audio DMA
  56. */
  57. static int _tm6000_start_audio_dma(struct snd_tm6000_card *chip)
  58. {
  59. struct tm6000_core *core = chip->core;
  60. dprintk(1, "Starting audio DMA\n");
  61. /* Enables audio */
  62. tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x40, 0x40);
  63. tm6000_set_audio_bitrate(core, 48000);
  64. return 0;
  65. }
  66. /*
  67. * BOARD Specific: Resets audio DMA
  68. */
  69. static int _tm6000_stop_audio_dma(struct snd_tm6000_card *chip)
  70. {
  71. struct tm6000_core *core = chip->core;
  72. dprintk(1, "Stopping audio DMA\n");
  73. /* Disables audio */
  74. tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x00, 0x40);
  75. return 0;
  76. }
  77. static void dsp_buffer_free(struct snd_pcm_substream *substream)
  78. {
  79. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  80. dprintk(2, "Freeing buffer\n");
  81. vfree(substream->runtime->dma_area);
  82. substream->runtime->dma_area = NULL;
  83. substream->runtime->dma_bytes = 0;
  84. }
  85. static int dsp_buffer_alloc(struct snd_pcm_substream *substream, int size)
  86. {
  87. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  88. dprintk(2, "Allocating buffer\n");
  89. if (substream->runtime->dma_area) {
  90. if (substream->runtime->dma_bytes > size)
  91. return 0;
  92. dsp_buffer_free(substream);
  93. }
  94. substream->runtime->dma_area = vmalloc(size);
  95. if (!substream->runtime->dma_area)
  96. return -ENOMEM;
  97. substream->runtime->dma_bytes = size;
  98. return 0;
  99. }
  100. /****************************************************************************
  101. ALSA PCM Interface
  102. ****************************************************************************/
  103. /*
  104. * Digital hardware definition
  105. */
  106. #define DEFAULT_FIFO_SIZE 4096
  107. static struct snd_pcm_hardware snd_tm6000_digital_hw = {
  108. .info = SNDRV_PCM_INFO_BATCH |
  109. SNDRV_PCM_INFO_MMAP |
  110. SNDRV_PCM_INFO_INTERLEAVED |
  111. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  112. SNDRV_PCM_INFO_MMAP_VALID,
  113. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  114. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
  115. .rate_min = 48000,
  116. .rate_max = 48000,
  117. .channels_min = 2,
  118. .channels_max = 2,
  119. .period_bytes_min = 64,
  120. .period_bytes_max = 12544,
  121. .periods_min = 2,
  122. .periods_max = 98,
  123. .buffer_bytes_max = 62720 * 8,
  124. };
  125. /*
  126. * audio pcm capture open callback
  127. */
  128. static int snd_tm6000_pcm_open(struct snd_pcm_substream *substream)
  129. {
  130. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  131. struct snd_pcm_runtime *runtime = substream->runtime;
  132. int err;
  133. err = snd_pcm_hw_constraint_pow2(runtime, 0,
  134. SNDRV_PCM_HW_PARAM_PERIODS);
  135. if (err < 0)
  136. goto _error;
  137. chip->substream = substream;
  138. runtime->hw = snd_tm6000_digital_hw;
  139. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  140. return 0;
  141. _error:
  142. dprintk(1, "Error opening PCM!\n");
  143. return err;
  144. }
  145. /*
  146. * audio close callback
  147. */
  148. static int snd_tm6000_close(struct snd_pcm_substream *substream)
  149. {
  150. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  151. struct tm6000_core *core = chip->core;
  152. if (atomic_read(&core->stream_started) > 0) {
  153. atomic_set(&core->stream_started, 0);
  154. schedule_work(&core->wq_trigger);
  155. }
  156. return 0;
  157. }
  158. static int tm6000_fillbuf(struct tm6000_core *core, char *buf, int size)
  159. {
  160. struct snd_tm6000_card *chip = core->adev;
  161. struct snd_pcm_substream *substream = chip->substream;
  162. struct snd_pcm_runtime *runtime;
  163. int period_elapsed = 0;
  164. unsigned int stride, buf_pos;
  165. int length;
  166. if (atomic_read(&core->stream_started) == 0)
  167. return 0;
  168. if (!size || !substream) {
  169. dprintk(1, "substream was NULL\n");
  170. return -EINVAL;
  171. }
  172. runtime = substream->runtime;
  173. if (!runtime || !runtime->dma_area) {
  174. dprintk(1, "runtime was NULL\n");
  175. return -EINVAL;
  176. }
  177. buf_pos = chip->buf_pos;
  178. stride = runtime->frame_bits >> 3;
  179. if (stride == 0) {
  180. dprintk(1, "stride is zero\n");
  181. return -EINVAL;
  182. }
  183. length = size / stride;
  184. if (length == 0) {
  185. dprintk(1, "%s: length was zero\n", __func__);
  186. return -EINVAL;
  187. }
  188. dprintk(1, "Copying %d bytes at %p[%d] - buf size=%d x %d\n", size,
  189. runtime->dma_area, buf_pos,
  190. (unsigned int)runtime->buffer_size, stride);
  191. if (buf_pos + length >= runtime->buffer_size) {
  192. unsigned int cnt = runtime->buffer_size - buf_pos;
  193. memcpy(runtime->dma_area + buf_pos * stride, buf, cnt * stride);
  194. memcpy(runtime->dma_area, buf + cnt * stride,
  195. length * stride - cnt * stride);
  196. } else
  197. memcpy(runtime->dma_area + buf_pos * stride, buf,
  198. length * stride);
  199. snd_pcm_stream_lock(substream);
  200. chip->buf_pos += length;
  201. if (chip->buf_pos >= runtime->buffer_size)
  202. chip->buf_pos -= runtime->buffer_size;
  203. chip->period_pos += length;
  204. if (chip->period_pos >= runtime->period_size) {
  205. chip->period_pos -= runtime->period_size;
  206. period_elapsed = 1;
  207. }
  208. snd_pcm_stream_unlock(substream);
  209. if (period_elapsed)
  210. snd_pcm_period_elapsed(substream);
  211. return 0;
  212. }
  213. /*
  214. * hw_params callback
  215. */
  216. static int snd_tm6000_hw_params(struct snd_pcm_substream *substream,
  217. struct snd_pcm_hw_params *hw_params)
  218. {
  219. int size, rc;
  220. size = params_period_bytes(hw_params) * params_periods(hw_params);
  221. rc = dsp_buffer_alloc(substream, size);
  222. if (rc < 0)
  223. return rc;
  224. return 0;
  225. }
  226. /*
  227. * hw free callback
  228. */
  229. static int snd_tm6000_hw_free(struct snd_pcm_substream *substream)
  230. {
  231. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  232. struct tm6000_core *core = chip->core;
  233. if (atomic_read(&core->stream_started) > 0) {
  234. atomic_set(&core->stream_started, 0);
  235. schedule_work(&core->wq_trigger);
  236. }
  237. dsp_buffer_free(substream);
  238. return 0;
  239. }
  240. /*
  241. * prepare callback
  242. */
  243. static int snd_tm6000_prepare(struct snd_pcm_substream *substream)
  244. {
  245. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  246. chip->buf_pos = 0;
  247. chip->period_pos = 0;
  248. return 0;
  249. }
  250. /*
  251. * trigger callback
  252. */
  253. static void audio_trigger(struct work_struct *work)
  254. {
  255. struct tm6000_core *core = container_of(work, struct tm6000_core,
  256. wq_trigger);
  257. struct snd_tm6000_card *chip = core->adev;
  258. if (atomic_read(&core->stream_started)) {
  259. dprintk(1, "starting capture");
  260. _tm6000_start_audio_dma(chip);
  261. } else {
  262. dprintk(1, "stopping capture");
  263. _tm6000_stop_audio_dma(chip);
  264. }
  265. }
  266. static int snd_tm6000_card_trigger(struct snd_pcm_substream *substream, int cmd)
  267. {
  268. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  269. struct tm6000_core *core = chip->core;
  270. int err = 0;
  271. switch (cmd) {
  272. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
  273. case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
  274. case SNDRV_PCM_TRIGGER_START:
  275. atomic_set(&core->stream_started, 1);
  276. break;
  277. case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
  278. case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
  279. case SNDRV_PCM_TRIGGER_STOP:
  280. atomic_set(&core->stream_started, 0);
  281. break;
  282. default:
  283. err = -EINVAL;
  284. break;
  285. }
  286. schedule_work(&core->wq_trigger);
  287. return err;
  288. }
  289. /*
  290. * pointer callback
  291. */
  292. static snd_pcm_uframes_t snd_tm6000_pointer(struct snd_pcm_substream *substream)
  293. {
  294. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  295. return chip->buf_pos;
  296. }
  297. static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
  298. unsigned long offset)
  299. {
  300. void *pageptr = subs->runtime->dma_area + offset;
  301. return vmalloc_to_page(pageptr);
  302. }
  303. /*
  304. * operators
  305. */
  306. static const struct snd_pcm_ops snd_tm6000_pcm_ops = {
  307. .open = snd_tm6000_pcm_open,
  308. .close = snd_tm6000_close,
  309. .ioctl = snd_pcm_lib_ioctl,
  310. .hw_params = snd_tm6000_hw_params,
  311. .hw_free = snd_tm6000_hw_free,
  312. .prepare = snd_tm6000_prepare,
  313. .trigger = snd_tm6000_card_trigger,
  314. .pointer = snd_tm6000_pointer,
  315. .page = snd_pcm_get_vmalloc_page,
  316. };
  317. /*
  318. * create a PCM device
  319. */
  320. /* FIXME: Control interface - How to control volume/mute? */
  321. /****************************************************************************
  322. Basic Flow for Sound Devices
  323. ****************************************************************************/
  324. /*
  325. * Alsa Constructor - Component probe
  326. */
  327. static int tm6000_audio_init(struct tm6000_core *dev)
  328. {
  329. struct snd_card *card;
  330. struct snd_tm6000_card *chip;
  331. int rc;
  332. static int devnr;
  333. char component[14];
  334. struct snd_pcm *pcm;
  335. if (!dev)
  336. return 0;
  337. if (devnr >= SNDRV_CARDS)
  338. return -ENODEV;
  339. if (!enable[devnr])
  340. return -ENOENT;
  341. rc = snd_card_new(&dev->udev->dev, index[devnr], "tm6000",
  342. THIS_MODULE, 0, &card);
  343. if (rc < 0) {
  344. snd_printk(KERN_ERR "cannot create card instance %d\n", devnr);
  345. return rc;
  346. }
  347. strcpy(card->driver, "tm6000-alsa");
  348. strcpy(card->shortname, "TM5600/60x0");
  349. sprintf(card->longname, "TM5600/60x0 Audio at bus %d device %d",
  350. dev->udev->bus->busnum, dev->udev->devnum);
  351. sprintf(component, "USB%04x:%04x",
  352. le16_to_cpu(dev->udev->descriptor.idVendor),
  353. le16_to_cpu(dev->udev->descriptor.idProduct));
  354. snd_component_add(card, component);
  355. chip = kzalloc(sizeof(struct snd_tm6000_card), GFP_KERNEL);
  356. if (!chip) {
  357. rc = -ENOMEM;
  358. goto error;
  359. }
  360. chip->core = dev;
  361. chip->card = card;
  362. dev->adev = chip;
  363. spin_lock_init(&chip->reg_lock);
  364. rc = snd_pcm_new(card, "TM6000 Audio", 0, 0, 1, &pcm);
  365. if (rc < 0)
  366. goto error_chip;
  367. pcm->info_flags = 0;
  368. pcm->private_data = chip;
  369. strcpy(pcm->name, "Trident TM5600/60x0");
  370. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_tm6000_pcm_ops);
  371. INIT_WORK(&dev->wq_trigger, audio_trigger);
  372. rc = snd_card_register(card);
  373. if (rc < 0)
  374. goto error_chip;
  375. dprintk(1, "Registered audio driver for %s\n", card->longname);
  376. return 0;
  377. error_chip:
  378. kfree(chip);
  379. dev->adev = NULL;
  380. error:
  381. snd_card_free(card);
  382. return rc;
  383. }
  384. static int tm6000_audio_fini(struct tm6000_core *dev)
  385. {
  386. struct snd_tm6000_card *chip;
  387. if (!dev)
  388. return 0;
  389. chip = dev->adev;
  390. if (!chip)
  391. return 0;
  392. if (!chip->card)
  393. return 0;
  394. snd_card_free(chip->card);
  395. chip->card = NULL;
  396. kfree(chip);
  397. dev->adev = NULL;
  398. return 0;
  399. }
  400. static struct tm6000_ops audio_ops = {
  401. .type = TM6000_AUDIO,
  402. .name = "TM6000 Audio Extension",
  403. .init = tm6000_audio_init,
  404. .fini = tm6000_audio_fini,
  405. .fillbuf = tm6000_fillbuf,
  406. };
  407. static int __init tm6000_alsa_register(void)
  408. {
  409. return tm6000_register_extension(&audio_ops);
  410. }
  411. static void __exit tm6000_alsa_unregister(void)
  412. {
  413. tm6000_unregister_extension(&audio_ops);
  414. }
  415. module_init(tm6000_alsa_register);
  416. module_exit(tm6000_alsa_unregister);