blob: 6e56df94e34b34ca424e3dee281d08377b1e9cd6 [file] [log] [blame]
Andy Wallsf9b071c2009-06-24 07:26:45 -03001/*
2 * ALSA PCM device for the
3 * ALSA interface to cx18 PCM capture streams
4 *
5 * Copyright (C) 2009 Andy Walls <awalls@radix.net>
Devin Heitmueller9972de92010-01-18 21:29:51 -03006 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
7 *
8 * Portions of this work were sponsored by ONELAN Limited.
Andy Wallsf9b071c2009-06-24 07:26:45 -03009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 * 02111-1307 USA
24 */
25
26#include <linux/init.h>
27#include <linux/kernel.h>
28
29#include <media/v4l2-device.h>
30
31#include <sound/core.h>
32#include <sound/pcm.h>
33
34#include "cx18-driver.h"
Devin Heitmueller9972de92010-01-18 21:29:51 -030035#include "cx18-queue.h"
36#include "cx18-streams.h"
37#include "cx18-fileops.h"
Andy Wallsf9b071c2009-06-24 07:26:45 -030038#include "cx18-alsa.h"
39
Devin Heitmueller71036ef2009-12-20 23:50:02 -030040static unsigned int pcm_debug;
41module_param(pcm_debug, int, 0644);
42MODULE_PARM_DESC(pcm_debug, "enable debug messages for pcm");
Devin Heitmueller9972de92010-01-18 21:29:51 -030043
44#define dprintk(fmt, arg...) do { \
Devin Heitmueller71036ef2009-12-20 23:50:02 -030045 if (pcm_debug) \
46 printk(KERN_INFO "cx18-alsa-pcm %s: " fmt, \
Devin Heitmueller9972de92010-01-18 21:29:51 -030047 __func__, ##arg); \
48 } while (0)
49
Devin Heitmueller9972de92010-01-18 21:29:51 -030050static struct snd_pcm_hardware snd_cx18_hw_capture = {
51 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
52 SNDRV_PCM_INFO_MMAP |
53 SNDRV_PCM_INFO_INTERLEAVED |
54 SNDRV_PCM_INFO_MMAP_VALID,
55
56 .formats = SNDRV_PCM_FMTBIT_S16_LE,
57
Devin Heitmueller94b12d92010-01-07 00:56:14 -030058 .rates = SNDRV_PCM_RATE_48000,
Devin Heitmueller9972de92010-01-18 21:29:51 -030059
60 .rate_min = 48000,
61 .rate_max = 48000,
62 .channels_min = 2,
63 .channels_max = 2,
64 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
65 .period_bytes_min = 64, /* 12544/2, */
66 .period_bytes_max = 12544,
67 .periods_min = 2,
68 .periods_max = 98, /* 12544, */
69};
70
Devin Heitmueller60433e2a2009-12-20 23:53:46 -030071void cx18_alsa_announce_pcm_data(struct snd_cx18_card *cxsc, u8 *pcm_data,
72 size_t num_bytes)
73{
74 struct snd_pcm_substream *substream;
75 struct snd_pcm_runtime *runtime;
76 unsigned int oldptr;
77 unsigned int stride;
78 int period_elapsed = 0;
79 int length;
80
81 dprintk("cx18 alsa announce ptr=%p data=%p num_bytes=%d\n", cxsc,
82 pcm_data, num_bytes);
83
84 substream = cxsc->capture_pcm_substream;
85 if (substream == NULL) {
86 dprintk("substream was NULL\n");
87 return;
88 }
89
90 runtime = substream->runtime;
91 if (runtime == NULL) {
92 dprintk("runtime was NULL\n");
93 return;
94 }
95
96 stride = runtime->frame_bits >> 3;
97 if (stride == 0) {
98 dprintk("stride is zero\n");
99 return;
100 }
101
102 length = num_bytes / stride;
103 if (length == 0) {
104 dprintk("%s: length was zero\n", __func__);
105 return;
106 }
107
108 if (runtime->dma_area == NULL) {
109 dprintk("dma area was NULL - ignoring\n");
110 return;
111 }
112
113 oldptr = cxsc->hwptr_done_capture;
114 if (oldptr + length >= runtime->buffer_size) {
115 unsigned int cnt =
116 runtime->buffer_size - oldptr;
117 memcpy(runtime->dma_area + oldptr * stride, pcm_data,
118 cnt * stride);
119 memcpy(runtime->dma_area, pcm_data + cnt * stride,
120 length * stride - cnt * stride);
121 } else {
122 memcpy(runtime->dma_area + oldptr * stride, pcm_data,
123 length * stride);
124 }
125 snd_pcm_stream_lock(substream);
126
127 cxsc->hwptr_done_capture += length;
128 if (cxsc->hwptr_done_capture >=
129 runtime->buffer_size)
130 cxsc->hwptr_done_capture -=
131 runtime->buffer_size;
132
133 cxsc->capture_transfer_done += length;
134 if (cxsc->capture_transfer_done >=
135 runtime->period_size) {
136 cxsc->capture_transfer_done -=
137 runtime->period_size;
138 period_elapsed = 1;
139 }
140
141 snd_pcm_stream_unlock(substream);
142
143 if (period_elapsed)
144 snd_pcm_period_elapsed(substream);
145}
146
Andy Wallsf9b071c2009-06-24 07:26:45 -0300147static int snd_cx18_pcm_capture_open(struct snd_pcm_substream *substream)
148{
149 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
150 struct snd_pcm_runtime *runtime = substream->runtime;
151 struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
Andy Wallsf9b071c2009-06-24 07:26:45 -0300152 struct cx18 *cx = to_cx18(v4l2_dev);
Devin Heitmueller9972de92010-01-18 21:29:51 -0300153 struct cx18_stream *s;
154 struct cx18_open_id *item;
155 int ret;
156
157 /* Instruct the cx18 to start sending packets */
158 s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
159
160 /* Allocate memory */
161 item = kmalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
Devin Heitmueller780edb72009-12-20 23:15:58 -0300162 if (NULL == item)
Devin Heitmueller9972de92010-01-18 21:29:51 -0300163 return -ENOMEM;
Devin Heitmueller780edb72009-12-20 23:15:58 -0300164
Devin Heitmueller9972de92010-01-18 21:29:51 -0300165 item->cx = cx;
166 item->type = s->type;
167 item->open_id = cx->open_id++;
168
169 /* See if the stream is available */
170 if (cx18_claim_stream(item, item->type)) {
171 /* No, it's already in use */
Devin Heitmueller1a8e0e32009-12-12 17:38:53 -0300172 kfree(item);
Devin Heitmueller9972de92010-01-18 21:29:51 -0300173 return -EBUSY;
174 }
175
176 if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
177 test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
178 /* We're already streaming. No additional action required */
179 return 0;
180 }
181
182
183 runtime->hw = snd_cx18_hw_capture;
184 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
185 cxsc->capture_pcm_substream = substream;
186 runtime->private_data = cx;
187
188 cx->pcm_announce_callback = cx18_alsa_announce_pcm_data;
189
190 /* Not currently streaming, so start it up */
191 set_bit(CX18_F_S_STREAMING, &s->s_flags);
192 ret = cx18_start_v4l2_encode_stream(s);
193
Andy Wallsf9b071c2009-06-24 07:26:45 -0300194 return 0;
195}
196
197static int snd_cx18_pcm_capture_close(struct snd_pcm_substream *substream)
198{
Devin Heitmueller9972de92010-01-18 21:29:51 -0300199 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
200 struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
201 struct cx18 *cx = to_cx18(v4l2_dev);
202 struct cx18_stream *s;
203 int ret;
204
205 /* Instruct the cx18 to stop sending packets */
206 s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
207 ret = cx18_stop_v4l2_encode_stream(s, 0);
208 clear_bit(CX18_F_S_STREAMING, &s->s_flags);
209
210 cx18_release_stream(s);
211
212 cx->pcm_announce_callback = NULL;
213
Andy Wallsf9b071c2009-06-24 07:26:45 -0300214 return 0;
215}
216
217static int snd_cx18_pcm_ioctl(struct snd_pcm_substream *substream,
218 unsigned int cmd, void *arg)
219{
220 return snd_pcm_lib_ioctl(substream, cmd, arg);
221}
222
Devin Heitmueller9972de92010-01-18 21:29:51 -0300223
224static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
225 size_t size)
226{
227 struct snd_pcm_runtime *runtime = subs->runtime;
228
229 dprintk("Allocating vbuffer\n");
230 if (runtime->dma_area) {
231 if (runtime->dma_bytes > size)
232 return 0;
233
234 vfree(runtime->dma_area);
235 }
236 runtime->dma_area = vmalloc(size);
237 if (!runtime->dma_area)
238 return -ENOMEM;
239
240 runtime->dma_bytes = size;
241
242 return 0;
243}
244
Andy Wallsf9b071c2009-06-24 07:26:45 -0300245static int snd_cx18_pcm_hw_params(struct snd_pcm_substream *substream,
246 struct snd_pcm_hw_params *params)
247{
Devin Heitmueller9972de92010-01-18 21:29:51 -0300248 int ret;
249
250 dprintk("%s called\n", __func__);
251
252 ret = snd_pcm_alloc_vmalloc_buffer(substream,
253 params_buffer_bytes(params));
Andy Wallsf9b071c2009-06-24 07:26:45 -0300254 return 0;
255}
256
257static int snd_cx18_pcm_hw_free(struct snd_pcm_substream *substream)
258{
Devin Heitmueller94b12d92010-01-07 00:56:14 -0300259 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
260 unsigned long flags;
261
262 spin_lock_irqsave(&cxsc->slock, flags);
263 if (substream->runtime->dma_area) {
264 dprintk("freeing pcm capture region\n");
265 vfree(substream->runtime->dma_area);
266 substream->runtime->dma_area = NULL;
267 }
268 spin_unlock_irqrestore(&cxsc->slock, flags);
269
Andy Wallsf9b071c2009-06-24 07:26:45 -0300270 return 0;
271}
272
273static int snd_cx18_pcm_prepare(struct snd_pcm_substream *substream)
274{
Devin Heitmueller9972de92010-01-18 21:29:51 -0300275 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
276
277 cxsc->hwptr_done_capture = 0;
278 cxsc->capture_transfer_done = 0;
279
Andy Wallsf9b071c2009-06-24 07:26:45 -0300280 return 0;
281}
282
283static int snd_cx18_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
284{
285 return 0;
286}
287
288static
289snd_pcm_uframes_t snd_cx18_pcm_pointer(struct snd_pcm_substream *substream)
290{
Devin Heitmueller9972de92010-01-18 21:29:51 -0300291 unsigned long flags;
292 snd_pcm_uframes_t hwptr_done;
293 struct snd_cx18_card *cxsc = snd_pcm_substream_chip(substream);
294
295 spin_lock_irqsave(&cxsc->slock, flags);
296 hwptr_done = cxsc->hwptr_done_capture;
297 spin_unlock_irqrestore(&cxsc->slock, flags);
298
299 return hwptr_done;
300}
301
Devin Heitmueller9972de92010-01-18 21:29:51 -0300302static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
303 unsigned long offset)
304{
305 void *pageptr = subs->runtime->dma_area + offset;
306
307 return vmalloc_to_page(pageptr);
Andy Wallsf9b071c2009-06-24 07:26:45 -0300308}
309
310static struct snd_pcm_ops snd_cx18_pcm_capture_ops = {
311 .open = snd_cx18_pcm_capture_open,
312 .close = snd_cx18_pcm_capture_close,
313 .ioctl = snd_cx18_pcm_ioctl,
314 .hw_params = snd_cx18_pcm_hw_params,
315 .hw_free = snd_cx18_pcm_hw_free,
316 .prepare = snd_cx18_pcm_prepare,
317 .trigger = snd_cx18_pcm_trigger,
318 .pointer = snd_cx18_pcm_pointer,
Devin Heitmueller9972de92010-01-18 21:29:51 -0300319 .page = snd_pcm_get_vmalloc_page,
Andy Wallsf9b071c2009-06-24 07:26:45 -0300320};
321
Devin Heitmueller9972de92010-01-18 21:29:51 -0300322int snd_cx18_pcm_create(struct snd_cx18_card *cxsc)
Andy Wallsf9b071c2009-06-24 07:26:45 -0300323{
324 struct snd_pcm *sp;
325 struct snd_card *sc = cxsc->sc;
326 struct v4l2_device *v4l2_dev = cxsc->v4l2_dev;
327 struct cx18 *cx = to_cx18(v4l2_dev);
328 int ret;
329
330 ret = snd_pcm_new(sc, "CX23418 PCM",
331 0, /* PCM device 0, the only one for this card */
332 0, /* 0 playback substreams */
333 1, /* 1 capture substream */
334 &sp);
335 if (ret) {
336 CX18_ALSA_ERR("%s: snd_cx18_pcm_create() failed with err %d\n",
337 __func__, ret);
338 goto err_exit;
339 }
Devin Heitmueller9972de92010-01-18 21:29:51 -0300340
341 spin_lock_init(&cxsc->slock);
342
Devin Heitmueller780edb72009-12-20 23:15:58 -0300343 snd_pcm_set_ops(sp, SNDRV_PCM_STREAM_CAPTURE,
344 &snd_cx18_pcm_capture_ops);
Devin Heitmueller9972de92010-01-18 21:29:51 -0300345 sp->info_flags = 0;
346 sp->private_data = cxsc;
Devin Heitmueller5eb99782009-11-20 02:15:20 -0300347 strlcpy(sp->name, cx->card_name, sizeof(sp->name));
Devin Heitmueller9972de92010-01-18 21:29:51 -0300348
Andy Wallsf9b071c2009-06-24 07:26:45 -0300349 return 0;
350
351err_exit:
352 return ret;
353}