tm6000-alsa.c 12 KB

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