0001
0002
0003
0004
0005
0006 #include <linux/kernel.h>
0007 #include <linux/module.h>
0008 #include <linux/moduleparam.h>
0009 #include <linux/spinlock.h>
0010 #include <linux/delay.h>
0011 #include <linux/sched.h>
0012 #include <linux/time.h>
0013 #include <linux/mm.h>
0014 #include <linux/i2c.h>
0015 #include <linux/mutex.h>
0016 #include <linux/uaccess.h>
0017 #include <linux/slab.h>
0018 #include <sound/core.h>
0019 #include <sound/pcm.h>
0020 #include <sound/initval.h>
0021
0022 #include "go7007-priv.h"
0023
0024 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
0025 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
0026 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
0027
0028 module_param_array(index, int, NULL, 0444);
0029 module_param_array(id, charp, NULL, 0444);
0030 module_param_array(enable, bool, NULL, 0444);
0031 MODULE_PARM_DESC(index, "Index value for the go7007 audio driver");
0032 MODULE_PARM_DESC(id, "ID string for the go7007 audio driver");
0033 MODULE_PARM_DESC(enable, "Enable for the go7007 audio driver");
0034
0035 struct go7007_snd {
0036 struct snd_card *card;
0037 struct snd_pcm *pcm;
0038 struct snd_pcm_substream *substream;
0039 spinlock_t lock;
0040 int w_idx;
0041 int hw_ptr;
0042 int avail;
0043 int capturing;
0044 };
0045
0046 static const struct snd_pcm_hardware go7007_snd_capture_hw = {
0047 .info = (SNDRV_PCM_INFO_MMAP |
0048 SNDRV_PCM_INFO_INTERLEAVED |
0049 SNDRV_PCM_INFO_BLOCK_TRANSFER |
0050 SNDRV_PCM_INFO_MMAP_VALID),
0051 .formats = SNDRV_PCM_FMTBIT_S16_LE,
0052 .rates = SNDRV_PCM_RATE_48000,
0053 .rate_min = 48000,
0054 .rate_max = 48000,
0055 .channels_min = 2,
0056 .channels_max = 2,
0057 .buffer_bytes_max = (128*1024),
0058 .period_bytes_min = 4096,
0059 .period_bytes_max = (128*1024),
0060 .periods_min = 1,
0061 .periods_max = 32,
0062 };
0063
0064 static void parse_audio_stream_data(struct go7007 *go, u8 *buf, int length)
0065 {
0066 struct go7007_snd *gosnd = go->snd_context;
0067 struct snd_pcm_runtime *runtime = gosnd->substream->runtime;
0068 int frames = bytes_to_frames(runtime, length);
0069 unsigned long flags;
0070
0071 spin_lock_irqsave(&gosnd->lock, flags);
0072 gosnd->hw_ptr += frames;
0073 if (gosnd->hw_ptr >= runtime->buffer_size)
0074 gosnd->hw_ptr -= runtime->buffer_size;
0075 gosnd->avail += frames;
0076 spin_unlock_irqrestore(&gosnd->lock, flags);
0077 if (gosnd->w_idx + length > runtime->dma_bytes) {
0078 int cpy = runtime->dma_bytes - gosnd->w_idx;
0079
0080 memcpy(runtime->dma_area + gosnd->w_idx, buf, cpy);
0081 length -= cpy;
0082 buf += cpy;
0083 gosnd->w_idx = 0;
0084 }
0085 memcpy(runtime->dma_area + gosnd->w_idx, buf, length);
0086 gosnd->w_idx += length;
0087 spin_lock_irqsave(&gosnd->lock, flags);
0088 if (gosnd->avail < runtime->period_size) {
0089 spin_unlock_irqrestore(&gosnd->lock, flags);
0090 return;
0091 }
0092 gosnd->avail -= runtime->period_size;
0093 spin_unlock_irqrestore(&gosnd->lock, flags);
0094 if (gosnd->capturing)
0095 snd_pcm_period_elapsed(gosnd->substream);
0096 }
0097
0098 static int go7007_snd_hw_params(struct snd_pcm_substream *substream,
0099 struct snd_pcm_hw_params *hw_params)
0100 {
0101 struct go7007 *go = snd_pcm_substream_chip(substream);
0102
0103 go->audio_deliver = parse_audio_stream_data;
0104 return 0;
0105 }
0106
0107 static int go7007_snd_hw_free(struct snd_pcm_substream *substream)
0108 {
0109 struct go7007 *go = snd_pcm_substream_chip(substream);
0110
0111 go->audio_deliver = NULL;
0112 return 0;
0113 }
0114
0115 static int go7007_snd_capture_open(struct snd_pcm_substream *substream)
0116 {
0117 struct go7007 *go = snd_pcm_substream_chip(substream);
0118 struct go7007_snd *gosnd = go->snd_context;
0119 unsigned long flags;
0120 int r;
0121
0122 spin_lock_irqsave(&gosnd->lock, flags);
0123 if (gosnd->substream == NULL) {
0124 gosnd->substream = substream;
0125 substream->runtime->hw = go7007_snd_capture_hw;
0126 r = 0;
0127 } else
0128 r = -EBUSY;
0129 spin_unlock_irqrestore(&gosnd->lock, flags);
0130 return r;
0131 }
0132
0133 static int go7007_snd_capture_close(struct snd_pcm_substream *substream)
0134 {
0135 struct go7007 *go = snd_pcm_substream_chip(substream);
0136 struct go7007_snd *gosnd = go->snd_context;
0137
0138 gosnd->substream = NULL;
0139 return 0;
0140 }
0141
0142 static int go7007_snd_pcm_prepare(struct snd_pcm_substream *substream)
0143 {
0144 return 0;
0145 }
0146
0147 static int go7007_snd_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
0148 {
0149 struct go7007 *go = snd_pcm_substream_chip(substream);
0150 struct go7007_snd *gosnd = go->snd_context;
0151
0152 switch (cmd) {
0153 case SNDRV_PCM_TRIGGER_START:
0154
0155
0156 gosnd->capturing = 1;
0157 return 0;
0158 case SNDRV_PCM_TRIGGER_STOP:
0159 gosnd->hw_ptr = gosnd->w_idx = gosnd->avail = 0;
0160 gosnd->capturing = 0;
0161 return 0;
0162 default:
0163 return -EINVAL;
0164 }
0165 }
0166
0167 static snd_pcm_uframes_t go7007_snd_pcm_pointer(struct snd_pcm_substream *substream)
0168 {
0169 struct go7007 *go = snd_pcm_substream_chip(substream);
0170 struct go7007_snd *gosnd = go->snd_context;
0171
0172 return gosnd->hw_ptr;
0173 }
0174
0175 static const struct snd_pcm_ops go7007_snd_capture_ops = {
0176 .open = go7007_snd_capture_open,
0177 .close = go7007_snd_capture_close,
0178 .hw_params = go7007_snd_hw_params,
0179 .hw_free = go7007_snd_hw_free,
0180 .prepare = go7007_snd_pcm_prepare,
0181 .trigger = go7007_snd_pcm_trigger,
0182 .pointer = go7007_snd_pcm_pointer,
0183 };
0184
0185 static int go7007_snd_free(struct snd_device *device)
0186 {
0187 struct go7007 *go = device->device_data;
0188
0189 kfree(go->snd_context);
0190 go->snd_context = NULL;
0191 return 0;
0192 }
0193
0194 static const struct snd_device_ops go7007_snd_device_ops = {
0195 .dev_free = go7007_snd_free,
0196 };
0197
0198 int go7007_snd_init(struct go7007 *go)
0199 {
0200 static int dev;
0201 struct go7007_snd *gosnd;
0202 int ret;
0203
0204 if (dev >= SNDRV_CARDS)
0205 return -ENODEV;
0206 if (!enable[dev]) {
0207 dev++;
0208 return -ENOENT;
0209 }
0210 gosnd = kmalloc(sizeof(struct go7007_snd), GFP_KERNEL);
0211 if (gosnd == NULL)
0212 return -ENOMEM;
0213 spin_lock_init(&gosnd->lock);
0214 gosnd->hw_ptr = gosnd->w_idx = gosnd->avail = 0;
0215 gosnd->capturing = 0;
0216 ret = snd_card_new(go->dev, index[dev], id[dev], THIS_MODULE, 0,
0217 &gosnd->card);
0218 if (ret < 0)
0219 goto free_snd;
0220
0221 ret = snd_device_new(gosnd->card, SNDRV_DEV_LOWLEVEL, go,
0222 &go7007_snd_device_ops);
0223 if (ret < 0)
0224 goto free_card;
0225
0226 ret = snd_pcm_new(gosnd->card, "go7007", 0, 0, 1, &gosnd->pcm);
0227 if (ret < 0)
0228 goto free_card;
0229
0230 strscpy(gosnd->card->driver, "go7007", sizeof(gosnd->card->driver));
0231 strscpy(gosnd->card->shortname, go->name, sizeof(gosnd->card->shortname));
0232 strscpy(gosnd->card->longname, gosnd->card->shortname,
0233 sizeof(gosnd->card->longname));
0234
0235 gosnd->pcm->private_data = go;
0236 snd_pcm_set_ops(gosnd->pcm, SNDRV_PCM_STREAM_CAPTURE,
0237 &go7007_snd_capture_ops);
0238 snd_pcm_set_managed_buffer_all(gosnd->pcm, SNDRV_DMA_TYPE_VMALLOC,
0239 NULL, 0, 0);
0240
0241 ret = snd_card_register(gosnd->card);
0242 if (ret < 0)
0243 goto free_card;
0244
0245 gosnd->substream = NULL;
0246 go->snd_context = gosnd;
0247 v4l2_device_get(&go->v4l2_dev);
0248 ++dev;
0249
0250 return 0;
0251
0252 free_card:
0253 snd_card_free(gosnd->card);
0254 free_snd:
0255 kfree(gosnd);
0256 return ret;
0257 }
0258 EXPORT_SYMBOL(go7007_snd_init);
0259
0260 int go7007_snd_remove(struct go7007 *go)
0261 {
0262 struct go7007_snd *gosnd = go->snd_context;
0263
0264 snd_card_disconnect(gosnd->card);
0265 snd_card_free_when_closed(gosnd->card);
0266 v4l2_device_put(&go->v4l2_dev);
0267 return 0;
0268 }
0269 EXPORT_SYMBOL(go7007_snd_remove);
0270
0271 MODULE_LICENSE("GPL v2");