snd-go7007.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. unsigned long flags;
  72. spin_lock_irqsave(&gosnd->lock, flags);
  73. gosnd->hw_ptr += frames;
  74. if (gosnd->hw_ptr >= runtime->buffer_size)
  75. gosnd->hw_ptr -= runtime->buffer_size;
  76. gosnd->avail += frames;
  77. spin_unlock_irqrestore(&gosnd->lock, flags);
  78. if (gosnd->w_idx + length > runtime->dma_bytes) {
  79. int cpy = runtime->dma_bytes - gosnd->w_idx;
  80. memcpy(runtime->dma_area + gosnd->w_idx, buf, cpy);
  81. length -= cpy;
  82. buf += cpy;
  83. gosnd->w_idx = 0;
  84. }
  85. memcpy(runtime->dma_area + gosnd->w_idx, buf, length);
  86. gosnd->w_idx += length;
  87. spin_lock_irqsave(&gosnd->lock, flags);
  88. if (gosnd->avail < runtime->period_size) {
  89. spin_unlock_irqrestore(&gosnd->lock, flags);
  90. return;
  91. }
  92. gosnd->avail -= runtime->period_size;
  93. spin_unlock_irqrestore(&gosnd->lock, flags);
  94. if (gosnd->capturing)
  95. snd_pcm_period_elapsed(gosnd->substream);
  96. }
  97. static int go7007_snd_hw_params(struct snd_pcm_substream *substream,
  98. struct snd_pcm_hw_params *hw_params)
  99. {
  100. struct go7007 *go = snd_pcm_substream_chip(substream);
  101. unsigned int bytes;
  102. bytes = params_buffer_bytes(hw_params);
  103. if (substream->runtime->dma_bytes > 0)
  104. vfree(substream->runtime->dma_area);
  105. substream->runtime->dma_bytes = 0;
  106. substream->runtime->dma_area = vmalloc(bytes);
  107. if (substream->runtime->dma_area == NULL)
  108. return -ENOMEM;
  109. substream->runtime->dma_bytes = bytes;
  110. go->audio_deliver = parse_audio_stream_data;
  111. return 0;
  112. }
  113. static int go7007_snd_hw_free(struct snd_pcm_substream *substream)
  114. {
  115. struct go7007 *go = snd_pcm_substream_chip(substream);
  116. go->audio_deliver = NULL;
  117. if (substream->runtime->dma_bytes > 0)
  118. vfree(substream->runtime->dma_area);
  119. substream->runtime->dma_bytes = 0;
  120. return 0;
  121. }
  122. static int go7007_snd_capture_open(struct snd_pcm_substream *substream)
  123. {
  124. struct go7007 *go = snd_pcm_substream_chip(substream);
  125. struct go7007_snd *gosnd = go->snd_context;
  126. unsigned long flags;
  127. int r;
  128. spin_lock_irqsave(&gosnd->lock, flags);
  129. if (gosnd->substream == NULL) {
  130. gosnd->substream = substream;
  131. substream->runtime->hw = go7007_snd_capture_hw;
  132. r = 0;
  133. } else
  134. r = -EBUSY;
  135. spin_unlock_irqrestore(&gosnd->lock, flags);
  136. return r;
  137. }
  138. static int go7007_snd_capture_close(struct snd_pcm_substream *substream)
  139. {
  140. struct go7007 *go = snd_pcm_substream_chip(substream);
  141. struct go7007_snd *gosnd = go->snd_context;
  142. gosnd->substream = NULL;
  143. return 0;
  144. }
  145. static int go7007_snd_pcm_prepare(struct snd_pcm_substream *substream)
  146. {
  147. return 0;
  148. }
  149. static int go7007_snd_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  150. {
  151. struct go7007 *go = snd_pcm_substream_chip(substream);
  152. struct go7007_snd *gosnd = go->snd_context;
  153. switch (cmd) {
  154. case SNDRV_PCM_TRIGGER_START:
  155. /* Just set a flag to indicate we should signal ALSA when
  156. * sound comes in */
  157. gosnd->capturing = 1;
  158. return 0;
  159. case SNDRV_PCM_TRIGGER_STOP:
  160. gosnd->hw_ptr = gosnd->w_idx = gosnd->avail = 0;
  161. gosnd->capturing = 0;
  162. return 0;
  163. default:
  164. return -EINVAL;
  165. }
  166. }
  167. static snd_pcm_uframes_t go7007_snd_pcm_pointer(struct snd_pcm_substream *substream)
  168. {
  169. struct go7007 *go = snd_pcm_substream_chip(substream);
  170. struct go7007_snd *gosnd = go->snd_context;
  171. return gosnd->hw_ptr;
  172. }
  173. static struct page *go7007_snd_pcm_page(struct snd_pcm_substream *substream,
  174. unsigned long offset)
  175. {
  176. return vmalloc_to_page(substream->runtime->dma_area + offset);
  177. }
  178. static const struct snd_pcm_ops go7007_snd_capture_ops = {
  179. .open = go7007_snd_capture_open,
  180. .close = go7007_snd_capture_close,
  181. .ioctl = snd_pcm_lib_ioctl,
  182. .hw_params = go7007_snd_hw_params,
  183. .hw_free = go7007_snd_hw_free,
  184. .prepare = go7007_snd_pcm_prepare,
  185. .trigger = go7007_snd_pcm_trigger,
  186. .pointer = go7007_snd_pcm_pointer,
  187. .page = go7007_snd_pcm_page,
  188. };
  189. static int go7007_snd_free(struct snd_device *device)
  190. {
  191. struct go7007 *go = device->device_data;
  192. kfree(go->snd_context);
  193. go->snd_context = NULL;
  194. return 0;
  195. }
  196. static struct snd_device_ops go7007_snd_device_ops = {
  197. .dev_free = go7007_snd_free,
  198. };
  199. int go7007_snd_init(struct go7007 *go)
  200. {
  201. static int dev;
  202. struct go7007_snd *gosnd;
  203. int ret;
  204. if (dev >= SNDRV_CARDS)
  205. return -ENODEV;
  206. if (!enable[dev]) {
  207. dev++;
  208. return -ENOENT;
  209. }
  210. gosnd = kmalloc(sizeof(struct go7007_snd), GFP_KERNEL);
  211. if (gosnd == NULL)
  212. return -ENOMEM;
  213. spin_lock_init(&gosnd->lock);
  214. gosnd->hw_ptr = gosnd->w_idx = gosnd->avail = 0;
  215. gosnd->capturing = 0;
  216. ret = snd_card_new(go->dev, index[dev], id[dev], THIS_MODULE, 0,
  217. &gosnd->card);
  218. if (ret < 0) {
  219. kfree(gosnd);
  220. return ret;
  221. }
  222. ret = snd_device_new(gosnd->card, SNDRV_DEV_LOWLEVEL, go,
  223. &go7007_snd_device_ops);
  224. if (ret < 0) {
  225. kfree(gosnd);
  226. return ret;
  227. }
  228. ret = snd_pcm_new(gosnd->card, "go7007", 0, 0, 1, &gosnd->pcm);
  229. if (ret < 0) {
  230. snd_card_free(gosnd->card);
  231. kfree(gosnd);
  232. return ret;
  233. }
  234. strlcpy(gosnd->card->driver, "go7007", sizeof(gosnd->card->driver));
  235. strlcpy(gosnd->card->shortname, go->name, sizeof(gosnd->card->driver));
  236. strlcpy(gosnd->card->longname, gosnd->card->shortname,
  237. sizeof(gosnd->card->longname));
  238. gosnd->pcm->private_data = go;
  239. snd_pcm_set_ops(gosnd->pcm, SNDRV_PCM_STREAM_CAPTURE,
  240. &go7007_snd_capture_ops);
  241. ret = snd_card_register(gosnd->card);
  242. if (ret < 0) {
  243. snd_card_free(gosnd->card);
  244. kfree(gosnd);
  245. return ret;
  246. }
  247. gosnd->substream = NULL;
  248. go->snd_context = gosnd;
  249. v4l2_device_get(&go->v4l2_dev);
  250. ++dev;
  251. return 0;
  252. }
  253. EXPORT_SYMBOL(go7007_snd_init);
  254. int go7007_snd_remove(struct go7007 *go)
  255. {
  256. struct go7007_snd *gosnd = go->snd_context;
  257. snd_card_disconnect(gosnd->card);
  258. snd_card_free_when_closed(gosnd->card);
  259. v4l2_device_put(&go->v4l2_dev);
  260. return 0;
  261. }
  262. EXPORT_SYMBOL(go7007_snd_remove);
  263. MODULE_LICENSE("GPL v2");