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