Full support for Ginger Console
[linux-ginger.git] / drivers / staging / go7007 / snd-go7007.c
blob03c4dfc138a1e08fe5faa31603bc929d84a983fc
1 /*
2 * Copyright (C) 2005-2006 Micronas USA Inc.
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.
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.
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.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/init.h>
22 #include <linux/spinlock.h>
23 #include <linux/delay.h>
24 #include <linux/sched.h>
25 #include <linux/vmalloc.h>
26 #include <linux/time.h>
27 #include <linux/mm.h>
28 #include <linux/i2c.h>
29 #include <linux/mutex.h>
30 #include <linux/uaccess.h>
31 #include <asm/system.h>
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/initval.h>
36 #include "go7007-priv.h"
38 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
39 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
40 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
42 module_param_array(index, int, NULL, 0444);
43 module_param_array(id, charp, NULL, 0444);
44 module_param_array(enable, bool, NULL, 0444);
45 MODULE_PARM_DESC(index, "Index value for the go7007 audio driver");
46 MODULE_PARM_DESC(id, "ID string for the go7007 audio driver");
47 MODULE_PARM_DESC(enable, "Enable for the go7007 audio driver");
49 struct go7007_snd {
50 struct snd_card *card;
51 struct snd_pcm *pcm;
52 struct snd_pcm_substream *substream;
53 spinlock_t lock;
54 int w_idx;
55 int hw_ptr;
56 int avail;
57 int capturing;
60 static struct snd_pcm_hardware go7007_snd_capture_hw = {
61 .info = (SNDRV_PCM_INFO_MMAP |
62 SNDRV_PCM_INFO_INTERLEAVED |
63 SNDRV_PCM_INFO_BLOCK_TRANSFER |
64 SNDRV_PCM_INFO_MMAP_VALID),
65 .formats = SNDRV_PCM_FMTBIT_S16_LE,
66 .rates = SNDRV_PCM_RATE_48000,
67 .rate_min = 48000,
68 .rate_max = 48000,
69 .channels_min = 2,
70 .channels_max = 2,
71 .buffer_bytes_max = (128*1024),
72 .period_bytes_min = 4096,
73 .period_bytes_max = (128*1024),
74 .periods_min = 1,
75 .periods_max = 32,
78 static void parse_audio_stream_data(struct go7007 *go, u8 *buf, int length)
80 struct go7007_snd *gosnd = go->snd_context;
81 struct snd_pcm_runtime *runtime = gosnd->substream->runtime;
82 int frames = bytes_to_frames(runtime, length);
84 spin_lock(&gosnd->lock);
85 gosnd->hw_ptr += frames;
86 if (gosnd->hw_ptr >= runtime->buffer_size)
87 gosnd->hw_ptr -= runtime->buffer_size;
88 gosnd->avail += frames;
89 spin_unlock(&gosnd->lock);
90 if (gosnd->w_idx + length > runtime->dma_bytes) {
91 int cpy = runtime->dma_bytes - gosnd->w_idx;
93 memcpy(runtime->dma_area + gosnd->w_idx, buf, cpy);
94 length -= cpy;
95 buf += cpy;
96 gosnd->w_idx = 0;
98 memcpy(runtime->dma_area + gosnd->w_idx, buf, length);
99 gosnd->w_idx += length;
100 spin_lock(&gosnd->lock);
101 if (gosnd->avail < runtime->period_size) {
102 spin_unlock(&gosnd->lock);
103 return;
105 gosnd->avail -= runtime->period_size;
106 spin_unlock(&gosnd->lock);
107 if (gosnd->capturing)
108 snd_pcm_period_elapsed(gosnd->substream);
111 static int go7007_snd_hw_params(struct snd_pcm_substream *substream,
112 struct snd_pcm_hw_params *hw_params)
114 struct go7007 *go = snd_pcm_substream_chip(substream);
115 unsigned int bytes;
117 bytes = params_buffer_bytes(hw_params);
118 if (substream->runtime->dma_bytes > 0)
119 vfree(substream->runtime->dma_area);
120 substream->runtime->dma_bytes = 0;
121 substream->runtime->dma_area = vmalloc(bytes);
122 if (substream->runtime->dma_area == NULL)
123 return -ENOMEM;
124 substream->runtime->dma_bytes = bytes;
125 go->audio_deliver = parse_audio_stream_data;
126 return 0;
129 static int go7007_snd_hw_free(struct snd_pcm_substream *substream)
131 struct go7007 *go = snd_pcm_substream_chip(substream);
133 go->audio_deliver = NULL;
134 if (substream->runtime->dma_bytes > 0)
135 vfree(substream->runtime->dma_area);
136 substream->runtime->dma_bytes = 0;
137 return 0;
140 static int go7007_snd_capture_open(struct snd_pcm_substream *substream)
142 struct go7007 *go = snd_pcm_substream_chip(substream);
143 struct go7007_snd *gosnd = go->snd_context;
144 unsigned long flags;
145 int r;
147 spin_lock_irqsave(&gosnd->lock, flags);
148 if (gosnd->substream == NULL) {
149 gosnd->substream = substream;
150 substream->runtime->hw = go7007_snd_capture_hw;
151 r = 0;
152 } else
153 r = -EBUSY;
154 spin_unlock_irqrestore(&gosnd->lock, flags);
155 return r;
158 static int go7007_snd_capture_close(struct snd_pcm_substream *substream)
160 struct go7007 *go = snd_pcm_substream_chip(substream);
161 struct go7007_snd *gosnd = go->snd_context;
163 gosnd->substream = NULL;
164 return 0;
167 static int go7007_snd_pcm_prepare(struct snd_pcm_substream *substream)
169 return 0;
172 static int go7007_snd_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
174 struct go7007 *go = snd_pcm_substream_chip(substream);
175 struct go7007_snd *gosnd = go->snd_context;
177 switch (cmd) {
178 case SNDRV_PCM_TRIGGER_START:
179 /* Just set a flag to indicate we should signal ALSA when
180 * sound comes in */
181 gosnd->capturing = 1;
182 return 0;
183 case SNDRV_PCM_TRIGGER_STOP:
184 gosnd->hw_ptr = gosnd->w_idx = gosnd->avail = 0;
185 gosnd->capturing = 0;
186 return 0;
187 default:
188 return -EINVAL;
192 static snd_pcm_uframes_t go7007_snd_pcm_pointer(struct snd_pcm_substream *substream)
194 struct go7007 *go = snd_pcm_substream_chip(substream);
195 struct go7007_snd *gosnd = go->snd_context;
197 return gosnd->hw_ptr;
200 static struct page *go7007_snd_pcm_page(struct snd_pcm_substream *substream,
201 unsigned long offset)
203 return vmalloc_to_page(substream->runtime->dma_area + offset);
206 static struct snd_pcm_ops go7007_snd_capture_ops = {
207 .open = go7007_snd_capture_open,
208 .close = go7007_snd_capture_close,
209 .ioctl = snd_pcm_lib_ioctl,
210 .hw_params = go7007_snd_hw_params,
211 .hw_free = go7007_snd_hw_free,
212 .prepare = go7007_snd_pcm_prepare,
213 .trigger = go7007_snd_pcm_trigger,
214 .pointer = go7007_snd_pcm_pointer,
215 .page = go7007_snd_pcm_page,
218 static int go7007_snd_free(struct snd_device *device)
220 struct go7007 *go = device->device_data;
222 kfree(go->snd_context);
223 go->snd_context = NULL;
224 if (--go->ref_count == 0)
225 kfree(go);
226 return 0;
229 static struct snd_device_ops go7007_snd_device_ops = {
230 .dev_free = go7007_snd_free,
233 int go7007_snd_init(struct go7007 *go)
235 static int dev;
236 struct go7007_snd *gosnd;
237 int ret = 0;
239 if (dev >= SNDRV_CARDS)
240 return -ENODEV;
241 if (!enable[dev]) {
242 dev++;
243 return -ENOENT;
245 gosnd = kmalloc(sizeof(struct go7007_snd), GFP_KERNEL);
246 if (gosnd == NULL)
247 return -ENOMEM;
248 spin_lock_init(&gosnd->lock);
249 gosnd->hw_ptr = gosnd->w_idx = gosnd->avail = 0;
250 gosnd->capturing = 0;
251 ret = snd_card_create(index[dev], id[dev], THIS_MODULE, 0,
252 &gosnd->card);
253 if (ret < 0) {
254 kfree(gosnd);
255 return ret;
257 ret = snd_device_new(gosnd->card, SNDRV_DEV_LOWLEVEL, go,
258 &go7007_snd_device_ops);
259 if (ret < 0) {
260 kfree(gosnd);
261 return ret;
263 snd_card_set_dev(gosnd->card, go->dev);
264 ret = snd_pcm_new(gosnd->card, "go7007", 0, 0, 1, &gosnd->pcm);
265 if (ret < 0) {
266 snd_card_free(gosnd->card);
267 kfree(gosnd);
268 return ret;
270 strncpy(gosnd->card->driver, "go7007", sizeof(gosnd->card->driver));
271 strncpy(gosnd->card->shortname, go->name, sizeof(gosnd->card->driver));
272 strncpy(gosnd->card->longname, gosnd->card->shortname,
273 sizeof(gosnd->card->longname));
275 gosnd->pcm->private_data = go;
276 snd_pcm_set_ops(gosnd->pcm, SNDRV_PCM_STREAM_CAPTURE,
277 &go7007_snd_capture_ops);
279 ret = snd_card_register(gosnd->card);
280 if (ret < 0) {
281 snd_card_free(gosnd->card);
282 kfree(gosnd);
283 return ret;
286 gosnd->substream = NULL;
287 go->snd_context = gosnd;
288 ++dev;
289 ++go->ref_count;
291 return 0;
293 EXPORT_SYMBOL(go7007_snd_init);
295 int go7007_snd_remove(struct go7007 *go)
297 struct go7007_snd *gosnd = go->snd_context;
299 snd_card_disconnect(gosnd->card);
300 snd_card_free_when_closed(gosnd->card);
301 return 0;
303 EXPORT_SYMBOL(go7007_snd_remove);
305 MODULE_LICENSE("GPL v2");