Merge commit 'v2.6.30-rc5' into sched/core
[linux-2.6] / drivers / staging / go7007 / snd-go7007.c
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
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/semaphore.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>
35
36 #include "go7007-priv.h"
37
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;
41
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");
48
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;
58 };
59
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,
76 };
77
78 static void parse_audio_stream_data(struct go7007 *go, u8 *buf, int length)
79 {
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);
83
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;
92
93                 memcpy(runtime->dma_area + gosnd->w_idx, buf, cpy);
94                 length -= cpy;
95                 buf += cpy;
96                 gosnd->w_idx = 0;
97         }
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;
104         }
105         gosnd->avail -= runtime->period_size;
106         spin_unlock(&gosnd->lock);
107         if (gosnd->capturing)
108                 snd_pcm_period_elapsed(gosnd->substream);
109 }
110
111 static int go7007_snd_hw_params(struct snd_pcm_substream *substream,
112                                 struct snd_pcm_hw_params *hw_params)
113 {
114         struct go7007 *go = snd_pcm_substream_chip(substream);
115         unsigned int bytes;
116
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;
127 }
128
129 static int go7007_snd_hw_free(struct snd_pcm_substream *substream)
130 {
131         struct go7007 *go = snd_pcm_substream_chip(substream);
132
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;
138 }
139
140 static int go7007_snd_capture_open(struct snd_pcm_substream *substream)
141 {
142         struct go7007 *go = snd_pcm_substream_chip(substream);
143         struct go7007_snd *gosnd = go->snd_context;
144         unsigned long flags;
145         int r;
146
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;
156 }
157
158 static int go7007_snd_capture_close(struct snd_pcm_substream *substream)
159 {
160         struct go7007 *go = snd_pcm_substream_chip(substream);
161         struct go7007_snd *gosnd = go->snd_context;
162
163         gosnd->substream = NULL;
164         return 0;
165 }
166
167 static int go7007_snd_pcm_prepare(struct snd_pcm_substream *substream)
168 {
169         return 0;
170 }
171
172 static int go7007_snd_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
173 {
174         struct go7007 *go = snd_pcm_substream_chip(substream);
175         struct go7007_snd *gosnd = go->snd_context;
176
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;
189         }
190 }
191
192 static snd_pcm_uframes_t go7007_snd_pcm_pointer(struct snd_pcm_substream *substream)
193 {
194         struct go7007 *go = snd_pcm_substream_chip(substream);
195         struct go7007_snd *gosnd = go->snd_context;
196
197         return gosnd->hw_ptr;
198 }
199
200 static struct page *go7007_snd_pcm_page(struct snd_pcm_substream *substream,
201                                         unsigned long offset)
202 {
203         return vmalloc_to_page(substream->runtime->dma_area + offset);
204 }
205
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,
216 };
217
218 static int go7007_snd_free(struct snd_device *device)
219 {
220         struct go7007 *go = device->device_data;
221
222         kfree(go->snd_context);
223         go->snd_context = NULL;
224         if (--go->ref_count == 0)
225                 kfree(go);
226         return 0;
227 }
228
229 static struct snd_device_ops go7007_snd_device_ops = {
230         .dev_free       = go7007_snd_free,
231 };
232
233 int go7007_snd_init(struct go7007 *go)
234 {
235         static int dev;
236         struct go7007_snd *gosnd;
237         int ret = 0;
238
239         if (dev >= SNDRV_CARDS)
240                 return -ENODEV;
241         if (!enable[dev]) {
242                 dev++;
243                 return -ENOENT;
244         }
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;
256         }
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;
262         }
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;
269         }
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));
274
275         gosnd->pcm->private_data = go;
276         snd_pcm_set_ops(gosnd->pcm, SNDRV_PCM_STREAM_CAPTURE,
277                         &go7007_snd_capture_ops);
278
279         ret = snd_card_register(gosnd->card);
280         if (ret < 0) {
281                 snd_card_free(gosnd->card);
282                 kfree(gosnd);
283                 return ret;
284         }
285
286         gosnd->substream = NULL;
287         go->snd_context = gosnd;
288         ++dev;
289         ++go->ref_count;
290
291         return 0;
292 }
293 EXPORT_SYMBOL(go7007_snd_init);
294
295 int go7007_snd_remove(struct go7007 *go)
296 {
297         struct go7007_snd *gosnd = go->snd_context;
298
299         snd_card_disconnect(gosnd->card);
300         snd_card_free_when_closed(gosnd->card);
301         return 0;
302 }
303 EXPORT_SYMBOL(go7007_snd_remove);
304
305 MODULE_LICENSE("GPL v2");