1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ALSA PCM device for the
4 * ALSA interface to ivtv PCM capture streams
6 * Copyright (C) 2009,2012 Andy Walls <awalls@md.metrocast.net>
7 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
9 * Portions of this work were sponsored by ONELAN Limited for the cx18 driver
12 #include "ivtv-driver.h"
13 #include "ivtv-queue.h"
14 #include "ivtv-streams.h"
15 #include "ivtv-fileops.h"
16 #include "ivtv-alsa.h"
17 #include "ivtv-alsa-pcm.h"
19 #include <sound/core.h>
20 #include <sound/pcm.h>
23 static unsigned int pcm_debug;
24 module_param(pcm_debug, int, 0644);
25 MODULE_PARM_DESC(pcm_debug, "enable debug messages for pcm");
27 #define dprintk(fmt, arg...) \
30 pr_info("ivtv-alsa-pcm %s: " fmt, __func__, ##arg); \
33 static const struct snd_pcm_hardware snd_ivtv_hw_capture = {
34 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
36 SNDRV_PCM_INFO_INTERLEAVED |
37 SNDRV_PCM_INFO_MMAP_VALID,
39 .formats = SNDRV_PCM_FMTBIT_S16_LE,
41 .rates = SNDRV_PCM_RATE_48000,
47 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
48 .period_bytes_min = 64, /* 12544/2, */
49 .period_bytes_max = 12544,
51 .periods_max = 98, /* 12544, */
54 static void ivtv_alsa_announce_pcm_data(struct snd_ivtv_card *itvsc,
58 struct snd_pcm_substream *substream;
59 struct snd_pcm_runtime *runtime;
62 int period_elapsed = 0;
65 dprintk("ivtv alsa announce ptr=%p data=%p num_bytes=%zu\n", itvsc,
68 substream = itvsc->capture_pcm_substream;
69 if (substream == NULL) {
70 dprintk("substream was NULL\n");
74 runtime = substream->runtime;
75 if (runtime == NULL) {
76 dprintk("runtime was NULL\n");
80 stride = runtime->frame_bits >> 3;
82 dprintk("stride is zero\n");
86 length = num_bytes / stride;
88 dprintk("%s: length was zero\n", __func__);
92 if (runtime->dma_area == NULL) {
93 dprintk("dma area was NULL - ignoring\n");
97 oldptr = itvsc->hwptr_done_capture;
98 if (oldptr + length >= runtime->buffer_size) {
100 runtime->buffer_size - oldptr;
101 memcpy(runtime->dma_area + oldptr * stride, pcm_data,
103 memcpy(runtime->dma_area, pcm_data + cnt * stride,
104 length * stride - cnt * stride);
106 memcpy(runtime->dma_area + oldptr * stride, pcm_data,
109 snd_pcm_stream_lock(substream);
111 itvsc->hwptr_done_capture += length;
112 if (itvsc->hwptr_done_capture >=
113 runtime->buffer_size)
114 itvsc->hwptr_done_capture -=
115 runtime->buffer_size;
117 itvsc->capture_transfer_done += length;
118 if (itvsc->capture_transfer_done >=
119 runtime->period_size) {
120 itvsc->capture_transfer_done -=
121 runtime->period_size;
125 snd_pcm_stream_unlock(substream);
128 snd_pcm_period_elapsed(substream);
131 static int snd_ivtv_pcm_capture_open(struct snd_pcm_substream *substream)
133 struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
134 struct snd_pcm_runtime *runtime = substream->runtime;
135 struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
136 struct ivtv *itv = to_ivtv(v4l2_dev);
137 struct ivtv_stream *s;
138 struct ivtv_open_id item;
141 /* Instruct the CX2341[56] to start sending packets */
142 snd_ivtv_lock(itvsc);
144 if (ivtv_init_on_first_open(itv)) {
145 snd_ivtv_unlock(itvsc);
149 s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
151 v4l2_fh_init(&item.fh, &s->vdev);
155 /* See if the stream is available */
156 if (ivtv_claim_stream(&item, item.type)) {
157 /* No, it's already in use */
158 v4l2_fh_exit(&item.fh);
159 snd_ivtv_unlock(itvsc);
163 if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) ||
164 test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
165 /* We're already streaming. No additional action required */
166 snd_ivtv_unlock(itvsc);
171 runtime->hw = snd_ivtv_hw_capture;
172 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
173 itvsc->capture_pcm_substream = substream;
174 runtime->private_data = itv;
176 itv->pcm_announce_callback = ivtv_alsa_announce_pcm_data;
178 /* Not currently streaming, so start it up */
179 set_bit(IVTV_F_S_STREAMING, &s->s_flags);
180 ret = ivtv_start_v4l2_encode_stream(s);
181 snd_ivtv_unlock(itvsc);
186 static int snd_ivtv_pcm_capture_close(struct snd_pcm_substream *substream)
188 struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
189 struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
190 struct ivtv *itv = to_ivtv(v4l2_dev);
191 struct ivtv_stream *s;
193 /* Instruct the ivtv to stop sending packets */
194 snd_ivtv_lock(itvsc);
195 s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
196 ivtv_stop_v4l2_encode_stream(s, 0);
197 clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
199 ivtv_release_stream(s);
201 itv->pcm_announce_callback = NULL;
202 snd_ivtv_unlock(itvsc);
207 static int snd_ivtv_pcm_prepare(struct snd_pcm_substream *substream)
209 struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
211 itvsc->hwptr_done_capture = 0;
212 itvsc->capture_transfer_done = 0;
217 static int snd_ivtv_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
223 snd_pcm_uframes_t snd_ivtv_pcm_pointer(struct snd_pcm_substream *substream)
226 snd_pcm_uframes_t hwptr_done;
227 struct snd_ivtv_card *itvsc = snd_pcm_substream_chip(substream);
229 spin_lock_irqsave(&itvsc->slock, flags);
230 hwptr_done = itvsc->hwptr_done_capture;
231 spin_unlock_irqrestore(&itvsc->slock, flags);
236 static const struct snd_pcm_ops snd_ivtv_pcm_capture_ops = {
237 .open = snd_ivtv_pcm_capture_open,
238 .close = snd_ivtv_pcm_capture_close,
239 .prepare = snd_ivtv_pcm_prepare,
240 .trigger = snd_ivtv_pcm_trigger,
241 .pointer = snd_ivtv_pcm_pointer,
244 int snd_ivtv_pcm_create(struct snd_ivtv_card *itvsc)
247 struct snd_card *sc = itvsc->sc;
248 struct v4l2_device *v4l2_dev = itvsc->v4l2_dev;
249 struct ivtv *itv = to_ivtv(v4l2_dev);
252 ret = snd_pcm_new(sc, "CX2341[56] PCM",
253 0, /* PCM device 0, the only one for this card */
254 0, /* 0 playback substreams */
255 1, /* 1 capture substream */
258 IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
263 spin_lock_init(&itvsc->slock);
265 snd_pcm_set_ops(sp, SNDRV_PCM_STREAM_CAPTURE,
266 &snd_ivtv_pcm_capture_ops);
267 snd_pcm_set_managed_buffer_all(sp, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
269 sp->private_data = itvsc;
270 strscpy(sp->name, itv->card_name, sizeof(sp->name));