snd-go7007.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (C) 2005-2006 Micronas USA Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License (Version 2) as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/delay.h>
  18. #include <linux/sched.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/time.h>
  21. #include <linux/mm.h>
  22. #include <linux/i2c.h>
  23. #include <linux/mutex.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/slab.h>
  26. #include <sound/core.h>
  27. #include <sound/pcm.h>
  28. #include <sound/initval.h>
  29. #include "go7007-priv.h"
  30. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  31. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  32. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  33. module_param_array(index, int, NULL, 0444);
  34. module_param_array(id, charp, NULL, 0444);
  35. module_param_array(enable, bool, NULL, 0444);
  36. MODULE_PARM_DESC(index, "Index value for the go7007 audio driver");
  37. MODULE_PARM_DESC(id, "ID string for the go7007 audio driver");
  38. MODULE_PARM_DESC(enable, "Enable for the go7007 audio driver");
  39. struct go7007_snd {
  40. struct snd_card *card;
  41. struct snd_pcm *pcm;
  42. struct snd_pcm_substream *substream;
  43. spinlock_t lock;
  44. int w_idx;
  45. int hw_ptr;
  46. int avail;
  47. int capturing;
  48. };
  49. static const struct snd_pcm_hardware go7007_snd_capture_hw = {
  50. .info = (SNDRV_PCM_INFO_MMAP |
  51. SNDRV_PCM_INFO_INTERLEAVED |
  52. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  53. SNDRV_PCM_INFO_MMAP_VALID),
  54. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  55. .rates = SNDRV_PCM_RATE_48000,
  56. .rate_min = 48000,
  57. .rate_max = 48000,
  58. .channels_min = 2,
  59. .channels_max = 2,
  60. .buffer_bytes_max = (128*1024),
  61. .period_bytes_min = 4096,
  62. .period_bytes_max = (128*1024),
  63. .periods_min = 1,
  64. .periods_max = 32,
  65. };
  66. static void parse_audio_stream_data(struct go7007 *go, u8 *buf, int length)
  67. {
  68. struct go7007_snd *gosnd = go->snd_context;
  69. struct snd_pcm_runtime *runtime = gosnd->substream->runtime;
  70. int frames = bytes_to_frames(runtime, length);
  71. spin_lock(&gosnd->lock);
  72. gosnd->hw_ptr += frames;
  73. if (gosnd->hw_ptr >= runtime->buffer_size)
  74. gosnd->hw_ptr -= runtime->buffer_size;
  75. gosnd->avail += frames;
  76. spin_unlock(&gosnd->lock);
  77. if (gosnd->w_idx + length > runtime->dma_bytes) {
  78. int cpy = runtime->dma_bytes - gosnd->w_idx;
  79. memcpy(runtime->dma_area + gosnd->w_idx, buf, cpy);
  80. length -= cpy;
  81. buf += cpy;
  82. gosnd->w_idx = 0;
  83. }
  84. memcpy(runtime->dma_area + gosnd->w_idx, buf, length);
  85. gosnd->w_idx += length;
  86. spin_lock(&gosnd->lock);
  87. if (gosnd->avail < runtime->period_size) {
  88. spin_unlock(&gosnd->lock);
  89. return;
  90. }
  91. gosnd->avail -= runtime->period_size;
  92. spin_unlock(&gosnd->lock);
  93. if (gosnd->capturing)
  94. snd_pcm_period_elapsed(gosnd->substream);
  95. }
  96. static int go7007_snd_hw_params(struct snd_pcm_substream *substream,
  97. struct snd_pcm_hw_params *hw_params)
  98. {
  99. struct go7007 *go = snd_pcm_substream_chip(substream);
  100. unsigned int bytes;
  101. bytes = params_buffer_bytes(hw_params);
  102. if (substream->runtime->dma_bytes > 0)
  103. vfree(substream->runtime->dma_area);
  104. substream->runtime->dma_bytes = 0;
  105. substream->runtime->dma_area = vmalloc(bytes);
  106. if (substream->runtime->dma_area == NULL)
  107. return -ENOMEM;
  108. substream->runtime->dma_bytes = bytes;
  109. go->audio_deliver = parse_audio_stream_data;
  110. return 0;
  111. }
  112. static int go7007_snd_hw_free(struct snd_pcm_substream *substream)
  113. {
  114. struct go7007 *go = snd_pcm_substream_chip(substream);
  115. go->audio_deliver = NULL;
  116. if (substream->runtime->dma_bytes > 0)
  117. vfree(substream->runtime->dma_area);
  118. substream->runtime->dma_bytes = 0;
  119. return 0;
  120. }
  121. static int go7007_snd_capture_open(struct snd_pcm_substream *substream)
  122. {
  123. struct go7007 *go = snd_pcm_substream_chip(substream);
  124. struct go7007_snd *gosnd = go->snd_context;
  125. unsigned long flags;
  126. int r;
  127. spin_lock_irqsave(&gosnd->lock, flags);
  128. if (gosnd->substream == NULL) {
  129. gosnd->substream = substream;
  130. substream->runtime->hw = go7007_snd_capture_hw;
  131. r = 0;
  132. } else
  133. r = -EBUSY;
  134. spin_unlock_irqrestore(&gosnd->lock, flags);
  135. return r;
  136. }
  137. static int go7007_snd_capture_close(struct snd_pcm_substream *substream)
  138. {
  139. struct go7007 *go = snd_pcm_substream_chip(substream);
  140. struct go7007_snd *gosnd = go->snd_context;
  141. gosnd->substream = NULL;
  142. return 0;
  143. }
  144. static int go7007_snd_pcm_prepare(struct snd_pcm_substream *substream)
  145. {
  146. return 0;
  147. }
  148. static int go7007_snd_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  149. {
  150. struct go7007 *go = snd_pcm_substream_chip(substream);
  151. struct go7007_snd *gosnd = go->snd_context;
  152. switch (cmd) {
  153. case SNDRV_PCM_TRIGGER_START:
  154. /* Just set a flag to indicate we should signal ALSA when
  155. * sound comes in */
  156. gosnd->capturing = 1;
  157. return 0;
  158. case SNDRV_PCM_TRIGGER_STOP:
  159. gosnd->hw_ptr = gosnd->w_idx = gosnd->avail = 0;
  160. gosnd->capturing = 0;
  161. return 0;
  162. default:
  163. return -EINVAL;
  164. }
  165. }
  166. static snd_pcm_uframes_t go7007_snd_pcm_pointer(struct snd_pcm_substream *substream)
  167. {
  168. struct go7007 *go = snd_pcm_substream_chip(substream);
  169. struct go7007_snd *gosnd = go->snd_context;
  170. return gosnd->hw_ptr;
  171. }
  172. static struct page *go7007_snd_pcm_page(struct snd_pcm_substream *substream,
  173. unsigned long offset)
  174. {
  175. return vmalloc_to_page(substream->runtime->dma_area + offset);
  176. }
  177. static const struct snd_pcm_ops go7007_snd_capture_ops = {
  178. .open = go7007_snd_capture_open,
  179. .close = go7007_snd_capture_close,
  180. .ioctl = snd_pcm_lib_ioctl,
  181. .hw_params = go7007_snd_hw_params,
  182. .hw_free = go7007_snd_hw_free,
  183. .prepare = go7007_snd_pcm_prepare,
  184. .trigger = go7007_snd_pcm_trigger,
  185. .pointer = go7007_snd_pcm_pointer,
  186. .page = go7007_snd_pcm_page,
  187. };
  188. static int go7007_snd_free(struct snd_device *device)
  189. {
  190. struct go7007 *go = device->device_data;
  191. kfree(go->snd_context);
  192. go->snd_context = NULL;
  193. return 0;
  194. }
  195. static struct snd_device_ops go7007_snd_device_ops = {
  196. .dev_free = go7007_snd_free,
  197. };
  198. int go7007_snd_init(struct go7007 *go)
  199. {
  200. static int dev;
  201. struct go7007_snd *gosnd;
  202. int ret = 0;
  203. if (dev >= SNDRV_CARDS)
  204. return -ENODEV;
  205. if (!enable[dev]) {
  206. dev++;
  207. return -ENOENT;
  208. }
  209. gosnd = kmalloc(sizeof(struct go7007_snd), GFP_KERNEL);
  210. if (gosnd == NULL)
  211. return -ENOMEM;
  212. spin_lock_init(&gosnd->lock);
  213. gosnd->hw_ptr = gosnd->w_idx = gosnd->avail = 0;
  214. gosnd->capturing = 0;
  215. ret = snd_card_new(go->dev, index[dev], id[dev], THIS_MODULE, 0,
  216. &gosnd->card);
  217. if (ret < 0)
  218. goto free_snd;
  219. ret = snd_device_new(gosnd->card, SNDRV_DEV_LOWLEVEL, go,
  220. &go7007_snd_device_ops);
  221. if (ret < 0)
  222. goto free_card;
  223. ret = snd_pcm_new(gosnd->card, "go7007", 0, 0, 1, &gosnd->pcm);
  224. if (ret < 0)
  225. goto free_card;
  226. strlcpy(gosnd->card->driver, "go7007", sizeof(gosnd->card->driver));
  227. strlcpy(gosnd->card->shortname, go->name, sizeof(gosnd->card->driver));
  228. strlcpy(gosnd->card->longname, gosnd->card->shortname,
  229. sizeof(gosnd->card->longname));
  230. gosnd->pcm->private_data = go;
  231. snd_pcm_set_ops(gosnd->pcm, SNDRV_PCM_STREAM_CAPTURE,
  232. &go7007_snd_capture_ops);
  233. ret = snd_card_register(gosnd->card);
  234. if (ret < 0)
  235. goto free_card;
  236. gosnd->substream = NULL;
  237. go->snd_context = gosnd;
  238. v4l2_device_get(&go->v4l2_dev);
  239. ++dev;
  240. return 0;
  241. free_card:
  242. snd_card_free(gosnd->card);
  243. free_snd:
  244. kfree(gosnd);
  245. return ret;
  246. }
  247. EXPORT_SYMBOL(go7007_snd_init);
  248. int go7007_snd_remove(struct go7007 *go)
  249. {
  250. struct go7007_snd *gosnd = go->snd_context;
  251. snd_card_disconnect(gosnd->card);
  252. snd_card_free_when_closed(gosnd->card);
  253. v4l2_device_put(&go->v4l2_dev);
  254. return 0;
  255. }
  256. EXPORT_SYMBOL(go7007_snd_remove);
  257. MODULE_LICENSE("GPL v2");