tm6000-alsa.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. *
  3. * Support for audio capture for tm5600/6000/6010
  4. * (c) 2007-2008 Mauro Carvalho Chehab <mchehab@redhat.com>
  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 int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
  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 <mchehab@redhat.com>");
  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_MMAP |
  111. SNDRV_PCM_INFO_INTERLEAVED |
  112. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  113. SNDRV_PCM_INFO_MMAP_VALID,
  114. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  115. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  116. .rate_min = 48000,
  117. .rate_max = 48000,
  118. .channels_min = 2,
  119. .channels_max = 2,
  120. .period_bytes_min = 64,
  121. .period_bytes_max = 12544,
  122. .periods_min = 1,
  123. .periods_max = 98,
  124. .buffer_bytes_max = 62720 * 8,
  125. };
  126. /*
  127. * audio pcm capture open callback
  128. */
  129. static int snd_tm6000_pcm_open(struct snd_pcm_substream *substream)
  130. {
  131. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  132. struct snd_pcm_runtime *runtime = substream->runtime;
  133. int err;
  134. err = snd_pcm_hw_constraint_pow2(runtime, 0,
  135. SNDRV_PCM_HW_PARAM_PERIODS);
  136. if (err < 0)
  137. goto _error;
  138. chip->substream = substream;
  139. runtime->hw = snd_tm6000_digital_hw;
  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_START:
  273. atomic_set(&core->stream_started, 1);
  274. break;
  275. case SNDRV_PCM_TRIGGER_STOP:
  276. atomic_set(&core->stream_started, 0);
  277. break;
  278. default:
  279. err = -EINVAL;
  280. break;
  281. }
  282. schedule_work(&core->wq_trigger);
  283. return err;
  284. }
  285. /*
  286. * pointer callback
  287. */
  288. static snd_pcm_uframes_t snd_tm6000_pointer(struct snd_pcm_substream *substream)
  289. {
  290. struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
  291. return chip->buf_pos;
  292. }
  293. /*
  294. * operators
  295. */
  296. static struct snd_pcm_ops snd_tm6000_pcm_ops = {
  297. .open = snd_tm6000_pcm_open,
  298. .close = snd_tm6000_close,
  299. .ioctl = snd_pcm_lib_ioctl,
  300. .hw_params = snd_tm6000_hw_params,
  301. .hw_free = snd_tm6000_hw_free,
  302. .prepare = snd_tm6000_prepare,
  303. .trigger = snd_tm6000_card_trigger,
  304. .pointer = snd_tm6000_pointer,
  305. };
  306. /*
  307. * create a PCM device
  308. */
  309. /* FIXME: Control interface - How to control volume/mute? */
  310. /****************************************************************************
  311. Basic Flow for Sound Devices
  312. ****************************************************************************/
  313. /*
  314. * Alsa Constructor - Component probe
  315. */
  316. static int tm6000_audio_init(struct tm6000_core *dev)
  317. {
  318. struct snd_card *card;
  319. struct snd_tm6000_card *chip;
  320. int rc;
  321. static int devnr;
  322. char component[14];
  323. struct snd_pcm *pcm;
  324. if (!dev)
  325. return 0;
  326. if (devnr >= SNDRV_CARDS)
  327. return -ENODEV;
  328. if (!enable[devnr])
  329. return -ENOENT;
  330. rc = snd_card_create(index[devnr], "tm6000", THIS_MODULE, 0, &card);
  331. if (rc < 0) {
  332. snd_printk(KERN_ERR "cannot create card instance %d\n", devnr);
  333. return rc;
  334. }
  335. strcpy(card->driver, "tm6000-alsa");
  336. strcpy(card->shortname, "TM5600/60x0");
  337. sprintf(card->longname, "TM5600/60x0 Audio at bus %d device %d",
  338. dev->udev->bus->busnum, dev->udev->devnum);
  339. sprintf(component, "USB%04x:%04x",
  340. le16_to_cpu(dev->udev->descriptor.idVendor),
  341. le16_to_cpu(dev->udev->descriptor.idProduct));
  342. snd_component_add(card, component);
  343. snd_card_set_dev(card, &dev->udev->dev);
  344. chip = kzalloc(sizeof(struct snd_tm6000_card), GFP_KERNEL);
  345. if (!chip) {
  346. rc = -ENOMEM;
  347. goto error;
  348. }
  349. chip->core = dev;
  350. chip->card = card;
  351. dev->adev = chip;
  352. spin_lock_init(&chip->reg_lock);
  353. rc = snd_pcm_new(card, "TM6000 Audio", 0, 0, 1, &pcm);
  354. if (rc < 0)
  355. goto error_chip;
  356. pcm->info_flags = 0;
  357. pcm->private_data = chip;
  358. strcpy(pcm->name, "Trident TM5600/60x0");
  359. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_tm6000_pcm_ops);
  360. INIT_WORK(&dev->wq_trigger, audio_trigger);
  361. rc = snd_card_register(card);
  362. if (rc < 0)
  363. goto error_chip;
  364. dprintk(1, "Registered audio driver for %s\n", card->longname);
  365. return 0;
  366. error_chip:
  367. kfree(chip);
  368. dev->adev = NULL;
  369. error:
  370. snd_card_free(card);
  371. return rc;
  372. }
  373. static int tm6000_audio_fini(struct tm6000_core *dev)
  374. {
  375. struct snd_tm6000_card *chip = dev->adev;
  376. if (!dev)
  377. return 0;
  378. if (!chip)
  379. return 0;
  380. if (!chip->card)
  381. return 0;
  382. snd_card_free(chip->card);
  383. chip->card = NULL;
  384. kfree(chip);
  385. dev->adev = NULL;
  386. return 0;
  387. }
  388. static struct tm6000_ops audio_ops = {
  389. .type = TM6000_AUDIO,
  390. .name = "TM6000 Audio Extension",
  391. .init = tm6000_audio_init,
  392. .fini = tm6000_audio_fini,
  393. .fillbuf = tm6000_fillbuf,
  394. };
  395. static int __init tm6000_alsa_register(void)
  396. {
  397. return tm6000_register_extension(&audio_ops);
  398. }
  399. static void __exit tm6000_alsa_unregister(void)
  400. {
  401. tm6000_unregister_extension(&audio_ops);
  402. }
  403. module_init(tm6000_alsa_register);
  404. module_exit(tm6000_alsa_unregister);