snd-go7007.c 7.9 KB

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